模型包含多边形项时的新预测

时间:2018-11-24 22:20:07

标签: r regression linear-regression prediction predict

我试图手工预测y的新数据向量x_new。 “手动”是指不使用predict函数(我的实际模型是mcmc不接受的predict对象)。像这样的简单模型就可以了:

lm1 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length, data=iris)
x_new <- c(1, 1.4, 3.2, 5.2)
y <- x_new %*% lm1$coef

但是我不确定模型看起来如何时:

lm2 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + poly(Sepal.Length,3), data=iris)

我究竟如何使用poly()变量中的参数?

1 个答案:

答案 0 :(得分:1)

您需要设置poly(., raw=TRUE),以便它使用原始多项式而不是正交多项式。比较一下,现在它们得出相同的系数:

lm2 <- lm(Petal.Width ~ Petal.Length + Sepal.Width + Sepal.Length +
            I(Sepal.Length^2) + I(Sepal.Length^3), data=iris)
lm3 <- lm(Petal.Width ~ Petal.Length +  poly(Sepal.Length, 3, raw=TRUE), 
          data=iris)

> coef(lm2)
      (Intercept)      Petal.Length       Sepal.Width      Sepal.Length I(Sepal.Length^2) I(Sepal.Length^3) 
      10.22126962        0.50889848        0.22999328       -5.81536464        0.98349473       -0.05626378 
> coef(lm3)
                       (Intercept)                       Petal.Length                        Sepal.Width poly(Sepal.Length, 3, raw = TRUE)1 
                       10.22126962                         0.50889848                         0.22999328                        -5.81536464 
poly(Sepal.Length, 3, raw = TRUE)2 poly(Sepal.Length, 3, raw = TRUE)3 
                        0.98349473                        -0.05626378