与predict.lm中newdata的定义相关的错误/警告消息

时间:2015-07-08 07:47:36

标签: r least-squares predict

在使用predict.lm时,我要么收到错误信息,要么收到错误的解决方案,而我正在尝试了解可能导致错误的原因。

在此处发布我的问题之前,我已经阅读了几个类似于我的问题的解决方案,如此处的example所示。但是,解决这些问题的方法似乎并没有在这里工作,我试图找出原因以及如何解决它。

为了最好地解释我的问题,请考虑以下MWE:

#------------------------------
# Fit least squares model
#------------------------------

data(mtcars)
a     <- mtcars$mpg
x     <- data.matrix(cbind(mtcars$wt, mtcars$hp))
xTest <- x[2,]  # We will use this for prediction later
fitCar <-lm(a ~ x) 

#------------------------------
# Prediction for x = xTest
#------------------------------

# Method 1 (doesn't work) 
yPred <- predict(fitCar, newdata = data.frame(x = xTest) , interval="confidence")
Error: variable 'x' was fitted with type "nmatrix.2" but type "numeric" was supplied

# Method 2 (works, but as you may observe, it is incorrect) 
yPred <- predict(fitCar, newdata = data.frame(xTest) , interval="confidence")

fit       lwr      upr
1  23.572329 22.456232 24.68843
2  22.583483 21.516224 23.65074
3  25.275819 23.974405 26.57723
4  21.265020 20.109318 22.42072
....
....
Warning message:
'newdata' had 2 rows but variables found have 32 rows 

问题:鉴于我们想要找到与xTest相对应的yPred,那么这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

如果您想预测,请始终将data.frame传递给lm

a     <- mtcars$mpg
x     <- data.matrix(cbind(mtcars$wt, mtcars$hp))
DF <- data.frame(a, x)
xTest <- x[2,]  # We will use this for prediction later
fitCar <-lm(a ~ ., data = DF) 

yPred <- predict(fitCar, newdata = data.frame(X1 = xTest[1], X2 = xTest[2]) , interval="confidence")
#       fit      lwr      upr
#1 22.58348 21.51622 23.65074