【问题标题】:How to automatically know the selected variable in stepAIC in R?如何自动知道 R 中 stepAIC 中选定的变量?
【发布时间】:2017-04-02 15:22:27
【问题描述】:

运行“stepAIC”后,我得到以下结果。

 fit1=lm(y~ x1+x2+x3+x4+x5)
 fit2=stepAIC(fit1)
 coef=fit2$coefficients

 >coef
 >intercept, x1,  x3,  x5
       5      1,    3,   5

我有另一个预测向量 z=(z1,...,z5)。因为它是 for 循环的一部分,所以我想使用 'coef %*% z' 自动计算预测 y。

我可以想出两种方法来实现它:
1.将stepAIC的未选择系数设为0;所以而不是

   coef=c(5,1,3,5)

我有

   coef=c(5,1,0,3,0,5)
  1. 找到选定的系数并找到对应的“z_i”。

我不知道如何实现这一点。任何帮助,将不胜感激。提前致谢。

【问题讨论】:

  • 将你想要预测的数据传递给predict方法;它将提取正确的变量(如果它们的名称相同)

标签: r regression linear-regression lm


【解决方案1】:

step 方法/设施只返回另一个lm 对象,因此您可以应用任何通用函数,包括predict

predict(fit2, newdata = a.data.frame)

如果最终目标不是预测,而是如您的问题标题所述,请使用attr(terms(fit2), 'term.labels')

【讨论】:

    猜你喜欢
    • 2022-06-28
    • 2017-09-09
    • 2016-02-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 2015-06-06
    • 1970-01-01
    • 2015-09-28
    相关资源
    最近更新 更多