R for 循环遍历 randomForest
R for loop over randomForest
我有一个包含 9 个输入变量和 1 个输出变量的 R 数据框。我想使用每个单独的输入找到 randomForest 的准确性,并将它们添加到列表中。为此,我需要遍历公式列表,如下面的代码所示:
library(randomForest)
library(caret)
formulas = c(target ~ age, target ~ sex, target ~ cp,
target ~ trestbps, target ~ chol, target ~ fbs,
target ~ restecg, target ~ ca, target ~ thal)
test_idx = sample(dim(df)[1], 60)
test_data = df[test_idx, ]
train_data = df[-test_idx, ]
accuracies = rep(NA, 9)
for (i in 1:length(formulas)){
rf_model = randomForest(formulas[i], data=train_data)
prediction = predict(rf_model, newdata=test_data, type="response")
acc = confusionMatrix(test_data$target, prediction)$overall[1]
accuracies[i] = acc
}
我运行出错了,
Error in if (n==0) stop("data (x) has 0 rows") : argument is of
length zero calls: ... eval -> eval -> randomForest -> randomForest.default
Execution halted
错误与传递给randomForest
的formulas[i]
参数有关,当我输入公式名称作为参数时(例如rf_model = randomForest(target ~ age, data=train_data)
,没有错误。
还有其他方法可以遍历 randomForest 吗?
谢谢!
由于你没有提供任何数据,我使用的是鸢尾花数据集。您必须对代码进行 2 处更改才能使其成为 运行。首先,使用 list
存储公式,其次,在 for 循环中使用 formulas[[i]]
。您可以使用以下代码
library(randomForest)
library(caret)
df <- iris
formulas = list(Species ~ Sepal.Length, Species ~ Petal.Length, Species ~ Petal.Width,
Species ~ Sepal.Width)
test_idx = sample(dim(df)[1], 60)
test_data = df[test_idx, ]
train_data = df[-test_idx, ]
accuracies = rep(NA, 4)
for (i in 1:length(formulas)){
rf_model = randomForest(formulas[[i]], data=train_data)
prediction = predict(rf_model, newdata=test_data, type="response")
acc = confusionMatrix(test_data$Species, prediction)$overall[1]
accuracies[i] = acc
}
#> 0.7000000 0.9166667 0.9166667 0.5000000
我有一个包含 9 个输入变量和 1 个输出变量的 R 数据框。我想使用每个单独的输入找到 randomForest 的准确性,并将它们添加到列表中。为此,我需要遍历公式列表,如下面的代码所示:
library(randomForest)
library(caret)
formulas = c(target ~ age, target ~ sex, target ~ cp,
target ~ trestbps, target ~ chol, target ~ fbs,
target ~ restecg, target ~ ca, target ~ thal)
test_idx = sample(dim(df)[1], 60)
test_data = df[test_idx, ]
train_data = df[-test_idx, ]
accuracies = rep(NA, 9)
for (i in 1:length(formulas)){
rf_model = randomForest(formulas[i], data=train_data)
prediction = predict(rf_model, newdata=test_data, type="response")
acc = confusionMatrix(test_data$target, prediction)$overall[1]
accuracies[i] = acc
}
我运行出错了,
Error in if (n==0) stop("data (x) has 0 rows") : argument is of length zero calls: ... eval -> eval -> randomForest -> randomForest.default Execution halted
错误与传递给randomForest
的formulas[i]
参数有关,当我输入公式名称作为参数时(例如rf_model = randomForest(target ~ age, data=train_data)
,没有错误。
还有其他方法可以遍历 randomForest 吗?
谢谢!
由于你没有提供任何数据,我使用的是鸢尾花数据集。您必须对代码进行 2 处更改才能使其成为 运行。首先,使用 list
存储公式,其次,在 for 循环中使用 formulas[[i]]
。您可以使用以下代码
library(randomForest)
library(caret)
df <- iris
formulas = list(Species ~ Sepal.Length, Species ~ Petal.Length, Species ~ Petal.Width,
Species ~ Sepal.Width)
test_idx = sample(dim(df)[1], 60)
test_data = df[test_idx, ]
train_data = df[-test_idx, ]
accuracies = rep(NA, 4)
for (i in 1:length(formulas)){
rf_model = randomForest(formulas[[i]], data=train_data)
prediction = predict(rf_model, newdata=test_data, type="response")
acc = confusionMatrix(test_data$Species, prediction)$overall[1]
accuracies[i] = acc
}
#> 0.7000000 0.9166667 0.9166667 0.5000000