我有一个关于根据预测函数存储我的预测函数的问题,以便稍后我可以在循环中调用它们。下面我附上了一些训练不同模型的示例代码,然后根据他们预测的内容重命名这些模型。我想做的是能够稍后在循环中调用这些预测模型。
library(Cubist)
library(e1071)
data(mtcars)
`%ni%`<-Negate(`%in%`)
dep_vars<-c('mpg','cyl')
for(dep_var in dep_vars) {
in_vars<-names(mtcars)[names(mtcars) %ni% dep_var]
x<-mtcars[,in_vars]
model1 <- cubist(x = x, y = mtcars[,dep_var], committees = 20,neighbors=2)
fm <- as.formula(paste(dep_var," ~", paste(in_vars, collapse = "+")))
model2 <- svm(fm, data = mtcars, cross = 10)
assign(paste0('model1_',dep_var),model1)
assign(paste0('model2_',dep_var),model2)
}
for(dep_var in dep_vars) {
pred1 <- predict(model1, newdata=predict_data) # rather than model1 i need it to be model1_dep_var
pred2 <- predict(model2, newdata=predict_data) # same here
}