Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中自动知道stepAIC中选择的变量?_R_Regression_Linear Regression_Lm - Fatal编程技术网

如何在R中自动知道stepAIC中选择的变量?

如何在R中自动知道stepAIC中选择的变量?,r,regression,linear-regression,lm,R,Regression,Linear Regression,Lm,运行“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的未选择系

运行“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)
  • 找到选定的系数并找到相应的“z_i”

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

    步骤
    方法/工具只返回另一个
    lm
    对象,因此您可以对其应用任何通用函数,包括
    预测

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


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

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