简体   繁体   中英

R predict() function returning too many values

I am using the r predict function, and it is returning more values than I expected it too. I created a linear model for the data to predict MDC from PKWH, MDT, and MDT2, then I created new data for input values into the predict function. The original data for utility has 24 values for each column of MDC, PKWH, MDT, and MDT2.

    fit2 <- lm(MDC ~ MDT + MDT2 + PKWH*(1 + MDT + MDT2), data = 
    utility)
    predict <- predict(fit2, data = data.frame(PKWH = 9, MDT = 75, MDT2 
    = 5625))

I expected the predict() function to produce 1 predicted value for the inputs of PKWH = 9 | MDT = 75 | MDT2 = 5625, but it gave me these 24 values.

           1        2        3        4        5        6        7 
    56.67781 51.66653 45.05200 42.12583 38.98647 38.80904 42.60033 
           8        9       10       11       12       13       14 
    46.86545 49.51928 54.15163 61.54441 68.00122 49.17722 45.27917 
          15       16       17       18       19       20       21 
    42.88154 40.93468 38.39330 37.80963 39.47550 41.58780 42.94447 
          22       23       24 
    46.25884 49.27053 53.98732 

Also, when I plug the new input values to calculate the predicted value using the coefficients from the linear model, I get 55.42165 which is not found on the list of the 24 values from the predict() function.

first, I wouldn't name your result predict - you want to save that for the function. You need

predicted_data <- predict(fit2, newdata = data.frame(PKWH = 9, MDT = 75, MDT2 
= 5625))

It's not throwing an error because predict has a catch-all ( ... ) at the end where input to data is heading, but it's giving you the predictions for the data you fit the model with.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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