Search code examples
rleast-squarespredict

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


While using predict.lm, I am either getting an error message or an incorrect solution, and I am trying to understand what might be causing that.

Before posting my problem here, I have read several solutions to problems similar to mine, as shown in an example here. However, the approach suggested in solutions to those problems doesn't appear to work here and I am trying to find out why and how to fix it.

To best explain my problem, consider the following 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 

Question: Given that we want to find yPred corresponding to xTest, what might be the right way of doing that?


Solution

  • Always pass a data.frame to lm if you want to predict:

    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