我之前已经问过这个问题:Creating a loop for different random forest training algoritms但是还没有得到正确答案。因此,有一个更可重复的例子的另一种尝试。
我有以下数据集:
train <- read.csv(url("http://s3.amazonaws.com/assets.datacamp.com/course/Kaggle/train.csv"))
test <- read.csv(url("http://s3.amazonaws.com/assets.datacamp.com/course/Kaggle/test.csv"))
train <- train[complete.cases(train), ]
我想运行几个随机的森林算法来查看哪一个表现最好。所以我基本上想做的是:
#predict based on Pclass
fit <- randomForest(as.factor(Survived) ~ Pclass, data=train, importance=TRUE, ntree=2000)
Prediction <- predict(fit, test)
#fetch accuracy
#predict based on Pclass and Sex
fit <- randomForest(as.factor(Survived) ~ Pclass + Sex, data=train, importance=TRUE, ntree=2000)
Prediction <- predict(fit, test)
#fetch accuracy
我想创建某种循环,以便我可以将所有值存储在列表中然后循环它。像这样:
list <- c(Pclass, Pclass + Sex)
for (R in list) {
modfit <- paste0("won ~ ", R, ", data=training, method=\"rf\", prox=\"TRUE")
modfit <- as.formula(modfit)
train(modfit)
}
但上面的代码不起作用。它给了我以下错误:
Error in parse(text = x, keep.source = FALSE) :
<text>:1:13: unexpected ','
1: won ~ Pclass,
关于如何让这个工作的任何想法?
答案 0 :(得分:0)
for (R in list) {
modfit <- paste0("won ~ ", R, "data=training, method=\"rf\", prox=\"TRUE")
modfit <- as.formula(modfit)
train(modfit)
}
您可能在数据=培训之前有一个逗号,而不需要在那里