我正在尝试使用功能作为响应变量来运行 RF。我无法通过变量传递字符串以用作 RF 中的响应。首先,我尝试在通过变量作为响应传递的字符串上运行 RF,我得到一个“向量长度不同的错误”。在此之后,我尝试输入实际的字符串(特征)作为响应,它工作正常。你能解释一下为什么可变长度不同吗?谢谢。
> colnames(Data[1])
[1] "feature1"
> rf.file = randomForest(formula =colnames(Data[1])~ ., data = Data, proximity = T, importance = T, ntree = 500, nodesize = 3)
Error in model.frame.default(formula = colnames(Data[1]) ~ ., :
variable lengths differ (found for 'feature1')
Enter a frame number, or 0 to exit
1: randomForest(formula = colnames(Data[1]) ~ ., data = Data, proximity = T, importance = T, ntree = 500, nodesize = 3)
2: randomForest.formula(formula = colnames(Data[1]) ~ ., data = brainDataTrim, proximity = T, importance = T, ntree = 500, nodesize = 3)
3: eval(m, parent.frame())
4: eval(expr, envir, enclos)
5: model.frame(formula = colnames(Data[1]) ~ ., data = Data, na.action = function (object, ...)
6: model.frame.default(formula = colnames(Data[1]) ~ ., data = Data, na.action = function (object, ...)
Selection: 0
> rf.file = randomForest(formula =feature1~ ., data = Data, proximity = T, importance = T, ntree = 500, nodesize = 3)
> rf.file
Call:
randomForest(formula = feature1 ~ ., data = Data, proximity = T, importance = T, ntree = 500, nodesize = 3)
Type of random forest: regression
Number of trees: 500
No. of variables tried at each split: 3
Mean of squared residuals: 0.1536834
% Var explained: 34.21
>