本文介绍了为不同的随机森林训练算法创建循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 for 循环来创建各种随机森林模型.我已经将我想在不同模型中使用的变量存储在一个名为 list 的列表中:

Im trying to write a for loop to create various random forest models. I've stored the variables I would like to use in the different models in a list called list:

 list <- c("EXPG1 + EXPG2", "EXPG1 + EXPG2 + distance")

然后我尝试遍历它创建的预测.我最终想要实现的是:

Then I try to loop over it created predictions. What I finally want to achieve is this:

modFit1 <- train(won ~ EXPG1 + EXPG2, data=training, method="rf", prox=TRUE)
modFit2 <- train(won ~ EXPG1 + EXPG2 + distance, data=training, method="rf", prox=TRUE)

但是,我在尝试实现此目标时遇到了一些问题.

I have some issues trying to accomplish this however.

这不起作用:

modFit1 <- train(won ~ list[1], data=training, method="rf", prox=TRUE)

而且这似乎也不起作用:

And also this doesnot seem to do the trick:

for (R in modfits) {

  modfit <- paste0("won ~ ", R, ", data=training, method=\"rf\", prox=\"TRUE")
 train(modfit)

}

对出了什么问题有任何想法吗?

Any thoughts on what goes wrong?

推荐答案

先创建一个空列表来存储模型

Create an empty list to store the model in first

results <- vector('list',2)
list <- c("EXPG1 + EXPG2", "EXPG1 + EXPG2 + distance")

for (i in 1:2){
  results[i] <- train(won ~ list[i], data=training, method="rf", prox=TRUE)
}

然后你应该可以在 results[[1]]

这篇关于为不同的随机森林训练算法创建循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 18:50