使用 randomForest() 命令创建循环
Creating a loop using the randomForest() command
所以我有一个称为“公式”的向量向量:它包含 31 个潜在模型(1 个输出变量和 5 个潜在解释变量)。
$V1
[1] "Edible ~ CapShape"
$V2
[1] "Edible ~ CapSurface"
$V3
[1] "Edible ~ CapColor"
$V4
[1] "Edible ~ Odor"
$V5
[1] "Edible ~ Height"
$V6
[1] "Edible ~ CapShape + CapSurface"
$V7
[1] "Edible ~ CapShape + CapColor"
$V8
[1] "Edible ~ CapShape + Odor"
$V9
[1] "Edible ~ CapShape + Height"
$V10
[1] "Edible ~ CapSurface + CapColor"
$V11
[1] "Edible ~ CapSurface + Odor"
$V12
[1] "Edible ~ CapSurface + Height"
$V13
[1] "Edible ~ CapColor + Odor"
$V14
[1] "Edible ~ CapColor + Height"
$V15
[1] "Edible ~ Odor + Height"
$V16
[1] "Edible ~ CapShape + CapSurface + CapColor"
$V17
[1] "Edible ~ CapShape + CapSurface + Odor"
$V18
[1] "Edible ~ CapShape + CapSurface + Height"
$V19
[1] "Edible ~ CapShape + CapColor + Odor"
$V20
[1] "Edible ~ CapShape + CapColor + Height"
$V21
[1] "Edible ~ CapShape + Odor + Height"
$V22
[1] "Edible ~ CapSurface + CapColor + Odor"
$V23
[1] "Edible ~ CapSurface + CapColor + Height"
$V24
[1] "Edible ~ CapSurface + Odor + Height"
$V25
[1] "Edible ~ CapColor + Odor + Height"
$V26
[1] "Edible ~ CapShape + CapSurface + CapColor + Odor"
$V27
[1] "Edible ~ CapShape + CapSurface + CapColor + Height"
$V28
[1] "Edible ~ CapShape + CapSurface + Odor + Height"
$V29
[1] "Edible ~ CapShape + CapColor + Odor + Height"
$V30
[1] "Edible ~ CapSurface + CapColor + Odor + Height"
$V31
[1] "Edible ~ CapShape + CapSurface + CapColor + Odor + Height"
我试图通过 randomForest() 命令循环公式向量中的每个模型,因此我可以生成 31 种不同的随机森林模型,到目前为止我尝试创建的循环是:
for (i in 1:length(formulas)){
current_model = randomForest(formula = formulas[i], data = mushrooms)
}
但是,这一直返回错误:
Error in nrow(x) : argument "x" is missing, with no default
数据“蘑菇”包含作为列标题一部分的公式中的变量,我想知道我可以做什么以便 randomForest() 函数识别公式中的模型?例如,有没有一种方法可以将这个向量的向量转换为单个向量,这样行得通吗?
formulas
中的list
个元素是字符串。我们可以将它们转换为 formula
class 并且它应该可以工作。 current_model
应该是一个 list
对象,用于存储模型元素,而不是在每次迭代时仅由同一对象重新更新。在 OP 的代码中,如果它起作用,'current_model' returns 只有最后一个模型
current_model <- vector('list', length(formulas))
for(i in seq_along(formulas)) {
current_model[[i]] <- randomForest(formula = as.formula(formulas[i]),
data = mushrooms)
}
所以我有一个称为“公式”的向量向量:它包含 31 个潜在模型(1 个输出变量和 5 个潜在解释变量)。
$V1
[1] "Edible ~ CapShape"
$V2
[1] "Edible ~ CapSurface"
$V3
[1] "Edible ~ CapColor"
$V4
[1] "Edible ~ Odor"
$V5
[1] "Edible ~ Height"
$V6
[1] "Edible ~ CapShape + CapSurface"
$V7
[1] "Edible ~ CapShape + CapColor"
$V8
[1] "Edible ~ CapShape + Odor"
$V9
[1] "Edible ~ CapShape + Height"
$V10
[1] "Edible ~ CapSurface + CapColor"
$V11
[1] "Edible ~ CapSurface + Odor"
$V12
[1] "Edible ~ CapSurface + Height"
$V13
[1] "Edible ~ CapColor + Odor"
$V14
[1] "Edible ~ CapColor + Height"
$V15
[1] "Edible ~ Odor + Height"
$V16
[1] "Edible ~ CapShape + CapSurface + CapColor"
$V17
[1] "Edible ~ CapShape + CapSurface + Odor"
$V18
[1] "Edible ~ CapShape + CapSurface + Height"
$V19
[1] "Edible ~ CapShape + CapColor + Odor"
$V20
[1] "Edible ~ CapShape + CapColor + Height"
$V21
[1] "Edible ~ CapShape + Odor + Height"
$V22
[1] "Edible ~ CapSurface + CapColor + Odor"
$V23
[1] "Edible ~ CapSurface + CapColor + Height"
$V24
[1] "Edible ~ CapSurface + Odor + Height"
$V25
[1] "Edible ~ CapColor + Odor + Height"
$V26
[1] "Edible ~ CapShape + CapSurface + CapColor + Odor"
$V27
[1] "Edible ~ CapShape + CapSurface + CapColor + Height"
$V28
[1] "Edible ~ CapShape + CapSurface + Odor + Height"
$V29
[1] "Edible ~ CapShape + CapColor + Odor + Height"
$V30
[1] "Edible ~ CapSurface + CapColor + Odor + Height"
$V31
[1] "Edible ~ CapShape + CapSurface + CapColor + Odor + Height"
我试图通过 randomForest() 命令循环公式向量中的每个模型,因此我可以生成 31 种不同的随机森林模型,到目前为止我尝试创建的循环是:
for (i in 1:length(formulas)){
current_model = randomForest(formula = formulas[i], data = mushrooms)
}
但是,这一直返回错误:
Error in nrow(x) : argument "x" is missing, with no default
数据“蘑菇”包含作为列标题一部分的公式中的变量,我想知道我可以做什么以便 randomForest() 函数识别公式中的模型?例如,有没有一种方法可以将这个向量的向量转换为单个向量,这样行得通吗?
formulas
中的list
个元素是字符串。我们可以将它们转换为 formula
class 并且它应该可以工作。 current_model
应该是一个 list
对象,用于存储模型元素,而不是在每次迭代时仅由同一对象重新更新。在 OP 的代码中,如果它起作用,'current_model' returns 只有最后一个模型
current_model <- vector('list', length(formulas))
for(i in seq_along(formulas)) {
current_model[[i]] <- randomForest(formula = as.formula(formulas[i]),
data = mushrooms)
}