【问题标题】:New predictions when model contains a poly term模型包含多项式时的新预测
【发布时间】:2019-04-27 00:28:32
【问题描述】:

我正在尝试手动预测 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() 变量的参数?

【问题讨论】:

    标签: r regression linear-regression prediction predict


    【解决方案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 
    

    【讨论】:

      猜你喜欢
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 1970-01-01
      • 2019-07-29
      • 2021-06-22
      • 1970-01-01
      • 2021-05-18
      相关资源
      最近更新 更多