首页 > 解决方案 > 可变长度与随机森林不同

问题描述

我对 R 真的很陌生,我想制作一个随机森林。但是我不断收到同样的错误-

Error in model.frame.default, lengths of variables differ.

我知道这个问题已经通过使用公式从字符串构造公式在另一个主题中得到解决,as.但我真的不知道该怎么做。你能帮我吗?谢谢你。

#A vector that has random sample of training values (70% & 30% samples)
index = sample(2,nrow(df), replace = TRUE, prob=c(0.7,0.3)) 

#Training Date 
training = df[index==1,]

#Testing data
testing = df[index==2,]

#Random forest model 
RFM = randomForest(df$Rating~., df$Customer_type, data = training)

标签: rrandom-forest

解决方案


那么你的错误是,你的自变量Rating来自df数据框,但你选择了data = training. 这意味着您的随机森林应该从 2 个不同的数据帧中获取数据,这是不可能的。我想那randomForest(Rating ~ Customer_type, data = training)会奏效。


推荐阅读