可变长度与随机森林不同

Variable lengths differ with random forest

我是 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)

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