predict lm function in R (multiple linear regression)

前端 未结 1 1836
长发绾君心
长发绾君心 2021-01-26 10:55

I did a multiple linear regression in R using the function lm and I want to use it to predict several values. So I\'m trying to use the function predict(). Here is

1条回答
  •  盖世英雄少女心
    2021-01-26 11:54

    The problem is you defined v as a new, distinct variable from t when you fit your model. R doesn't remember how a variable was created so it doesn't know that v is a function of t when you fit the model. So when you go to predict values, it uses the existing values of v which would have a different length than the new values of t you are specifying.

    Instead you want to fit

    new <- data.frame(t=c(10, 20, 30))
    LinReg <- lm(p ~ log(t) + I(1/t))
    Pred <- predict(LinReg, new, interval="confidence")
    

    If you did want v to be a completely independent variable, then you would need to supply values for v as well in your new data.frame in order to predict p.

    0 讨论(0)
提交回复
热议问题