我正在尝试使用以下格式的数据框列名生成公式:
d ~ x1 + x2 + x3 + x4
来自以下示例数据集:
a = c(1,2,3)
b = c(2,4,6)
c = c(1,3,5)
d = c(9,8,7)
x1 = c(1,2,3)
x2 = c(2,4,6)
x3 = c(1,3,5)
x4 = c(9,8,7)
df = data.frame(a,b,c,d,x1,x2,x3,x4)
至于我已经尝试过的事情:
我知道我可以使用以下方法仅对我需要的列进行子集
predictors = names(df[5:8])
response = names(df[4])
尽管如此,我尝试将这些纳入公式的努力都失败了
如何将预测变量和响应变量组合成以下格式:
d ~ x1 + x2 + x3 + x4
我最终想要将此公式输入randomForest函数。
答案 0 :(得分:3)
我们可以使用randomForest
的默认方法(而不是公式方法)来避免整个问题:
randomForest(df[5:8], df[[4]])
或就问题中定义的predictors
和response
而言:
randomForest(df[predictors], df[[response]])
正如randomForest help file的 Note 部分所述,此处使用的默认方法具有比公式方法更好的性能的额外优势。