简体   繁体   English

可变长度与随机森林不同

[英]Variable lengths differ with random forest

I'm really new to R and I want to make a random forest.我对 R 真的很陌生,我想制作一个随机森林。 However I keep getting the same error-但是我不断收到同样的错误-

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

I know this issue has been solved in another topic by constructing a formula from strings with as.我知道这个问题已经在另一个主题中通过使用as. formula but I have really no idea how to do it.公式,但我真的不知道该怎么做。 Can you help me please?你能帮我吗? Thank you.谢谢你。

#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)

Well what your error is, is that your independent variable is Rating from the df dataframe, but you selected data = training .那么您的错误是,您的自变量是来自df dataframe 的Rating ,但您选择了data = training This means that your random forest should take data from 2 different dataframes, which isn't possible.这意味着您的随机森林应该从 2 个不同的数据帧中获取数据,这是不可能的。 I guess that randomForest(Rating ~ Customer_type, data = training) would work.我猜randomForest(Rating ~ Customer_type, data = training)会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM