我创建了一个函数,可以在我输入的模型中运行交叉验证:
# vectors to hold results
master_modelname <- vector()
master_modelmea <- vector()
# model coefficients
master_sink <- attr(mini_kitchensink$terms,"term.labels")
master_stepF <- attr(stepF$terms,"term.labels")
master_stepB <- attr(stepB$terms,"term.labels")
# folds
myKfolds <- function(preds, dataset, num_folds=5, dependant=c("loss")) {
# create vectors for holding predictions and actuals from k folds iterations
cv_prediction <- data.frame()
testsetCopy <- data.frame()
kmeas <- vector()
folds <- createFolds(dataset[,dependant], k=num_folds)
for ( f in folds ) {
ktrain <- dataset[-f,]
ktest <- dataset[f,]
kmodel <- lm(paste(dependant,"~", paste(preds, collapse="+"),sep=""), data=ktrain)
predictions <- predict(kmodel, interval="prediction", newdata=ktest)
temp <- as.data.frame(predictions)
cv_prediction <- rbind(cv_prediction, temp)
testsetCopy <- rbind(testsetCopy, ktest)
errors <- ktest[,dependant] - as.numeric(predictions[,"fit"])
kmeas <- c(kmeas,mean(abs(errors)))
}
master_modelname <- c(master_modelname, "bla")
master_modelmea <- c(master_modelmea, mean(kmeas))
}
调用函数后:
myKfolds(master_stepB, ptrain, num_folds=5)
它在大约10分钟后运行。
但我的矢量master_modelname
和master_modelmea
是空的。
但是,如果我只是突出显示并运行我的函数的最后两行,它可以工作,这两个向量现在具有值'bla'和单个数字mean(kmeas)
我必须“强制运行”函数中的最后两行才能获得预期的结果。那么这是否意味着由于某种原因我的函数的两行没有出于某种原因运行?