在 R 中使用 anova() 函数比较两个模型

机器算法验证 r 方差分析
2022-01-19 12:10:34

从文档中anova()

当给定一系列对象时,'anova' 按照指定的顺序对模型进行相互测试......

相互测试模型意味着什么?为什么顺序很重要?

这是GenABEL 教程中的一个示例:

    >  modelAdd = lm(qt~as.numeric(snp1))
    >  modelDom = lm(qt~I(as.numeric(snp1)>=2))
    >  modelRec = lm(qt~I(as.numeric(snp1)>=3))
     anova(modelAdd, modelGen, test="Chisq")
    Analysis of Variance Table

    Model 1: qt ~ as.numeric(snp1)
    Model 2: qt ~ snp1
      Res.Df  RSS Df Sum of Sq Pr(>Chi)
    1   2372 2320                      
    2   2371 2320  1    0.0489     0.82
     anova(modelDom, modelGen, test="Chisq")
    Analysis of Variance Table

    Model 1: qt ~ I(as.numeric(snp1) >= 2)
    Model 2: qt ~ snp1
      Res.Df  RSS Df Sum of Sq Pr(>Chi)
    1   2372 2322                      
    2   2371 2320  1      1.77     0.18
     anova(modelRec, modelGen, test="Chisq")
    Analysis of Variance Table

    Model 1: qt ~ I(as.numeric(snp1) >= 3)
    Model 2: qt ~ snp1
      Res.Df  RSS Df Sum of Sq Pr(>Chi)  
    1   2372 2324                        
    2   2371 2320  1      3.53    0.057 .
    ---
    Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

我如何解释这个输出?

1个回答

当您使用anova(lm.1,lm.2,test="Chisq")时,它会执行卡方检验来比较lm.1lm.2(即它检验残差平方和的减少是否具有统计显着性)。lm.1请注意,这仅在并且lm.2是嵌套模型时才有意义。

例如,在您使用的第一个方差分析中,检验的 p 值为 0.82。这意味着拟合模型“modelAdd”与modelGen在水平上没有显着差异α=0.05. 然而,使用第三个方差分析中的 p 值,模型“modelRec”与模型“modelGen”显着不同α=0.1.

还可以查看线性模型拟合的 ANOVA