Error/warning 与 predict.lm 中新数据定义相关的消息

Error/warning message related to definition of newdata in predict.lm

在使用 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,那么正确的做法是什么?

如果要预测,请始终将 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