0
我在此之前詢問過此問題: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), ]
我想運行幾個隨機森林algoritms,看看哪一個性能最佳。所以我基本上想要做的是:
#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,
任何想法,我怎麼能得到這個工作?