lm的lm预测中的警告 [英] warning in lm prediction for r
问题描述
collection <- data.frame(col1=X1,col2=X2,col3=X3,col4=X4)
k <- 5
ind <- sample(seq(1,k), length(X1), replace=TRUE)
test_ind = which(ind==1)
train<-collection[-test_ind,]
fit<-lm(X1~poly(X2,2,raw=T)+X3+X4+X2:X3,data=train)
model1_resid<-predict(fit,collection[test_ind,2:4])
警告信息: 'newdata'有105行,但是找到的变量有444行
Warning message: 'newdata' had 105 rows but variables found have 444 rows
顺便说一句:length(test_ind)为105,nrow(train)= 444
BTW: length(test_ind) is 105 and nrow(train)=444
我计划运行交叉验证,但是上面的代码会生成警告,在进入lm函数之前,我已经按照该论坛中的其他帖子进行了子集设置,为什么仍然存在警告?任何人都可以指出错误吗?谢谢
I plan to run cross validation, but the above code generates the warning, I already followed other posts in this forum to do subsetting before I enter the lm function, why there is still warning? Anyone can point out the bug? Thanks
推荐答案
我认为您需要使用相同的变量名称,因此,如果要使用第2、3、4列进行预测,则名称应为X1, X2,X3(用于模型)(而不是col2,col3和col4).
I think you need to use the same variable names, so if you want to use columns 2,3,4 for your prediction, the names shoult be X1, X2, X3 as they are used for the model (not col2, col3 and col4 as you have).
在预测调用之前尝试例如colnames(collection)= c("X0","X1","X2","X3"),它应该可以工作(尽管我不知道您是否真的想使用col2,col3和col4进行预测).
Try for example colnames(collection) = c("X0", "X1", "X2", "X3") before the predict call and it should work (although I don't understand if you really wanted to use col2, col3 and col4 for predicting).
这篇关于lm的lm预测中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!