在考虑变量之间的相互作用的情况下,为什么线性回归和方差分析会给出不同pp

机器算法验证 r 回归 统计学意义 方差分析 p 值
2022-01-25 11:16:47

我试图使用回归模型拟合一个时间序列数据(没有重复)。数据如下所示:

> xx.2
          value time treat
    1  8.788269    1     0
    2  7.964719    6     0
    3  8.204051   12     0
    4  9.041368   24     0
    5  8.181555   48     0
    6  8.041419   96     0
    7  7.992336  144     0
    8  7.948658    1     1
    9  8.090211    6     1
    10 8.031459   12     1
    11 8.118308   24     1
    12 7.699051   48     1
    13 7.537120   96     1
    14 7.268570  144     1

由于缺乏重复,我将时间视为连续变量。“处理”列分别显示病例和对照数据。

首先,我用“lm”拟合模型“value = time*treat” R

summary(lm(value~time*treat,data=xx.2))

Call:
lm(formula = value ~ time * treat, data = xx.2)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.50627 -0.12345  0.00296  0.04124  0.63785 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  8.493476   0.156345  54.325 1.08e-13 ***
time        -0.003748   0.002277  -1.646   0.1307    
treat       -0.411271   0.221106  -1.860   0.0925 .  
time:treat  -0.001938   0.003220  -0.602   0.5606    

time 和 Treat 的 pvalue 不显着。

使用方差分析时,我得到了不同的结果:

 summary(aov(value~time*treat,data=xx.2))
            Df Sum Sq Mean Sq F value Pr(>F)  
time         1 0.7726  0.7726   8.586 0.0150 *
treat        1 0.8852  0.8852   9.837 0.0106 *
time:treat   1 0.0326  0.0326   0.362 0.5606  
Residuals   10 0.8998  0.0900                 

时间和待遇的 pvalue 发生了变化。

对于线性回归,如果我是对的,这意味着时间和待遇对价值没有显着影响,但对于 ANOVA,这意味着时间和待遇对价值有显着影响。

有人可以向我解释为什么这两种方法有区别,使用哪一种?

4个回答

Peter Ellis 的回答非常好,但还有一点需要说明。检验统计量(及其值)是对是否的检验打印输出上的检验是添加的变量是否显着降低了残差平方和。tpβ=0Fanova()

检验与顺序无关,而检验则不是。因此,彼得建议您以不同的顺序尝试变量。在一个测试中显着的变量也可能在另一个测试中不显着(反之亦然)。tF

我的感觉(欢迎其他贡献者纠正我)是,当您尝试预测现象时(如在系统应用程序中),您最感兴趣的是用最少的预测变量减少方差,因此想要anova()结果。但是,如果您试图确定的边际效应,您将最关心您感兴趣的特定的重要性,并且所有其他变量将仅控制您的同行评审员试图找到的替代解释。Xyβ

lm() 和 aov() 的拟合相同,但报告不同。考虑到所有其他变量的存在,t 检验是相关变量的边际影响。F 检验是连续的 - 因此它们在仅存在截距的情况下测试时间的重要性,在仅存在截距和时间的情况下测试处理的重要性,以及在存在上述所有情况下的交互作用。

假设您对treat 的重要性感兴趣,我建议您拟合两个模型,一个有,一个没有,通过将两个模型放在anova() 中比较两者,并使用该F 检验。这将同时测试处理和交互。

考虑以下:

> xx.2 <- as.data.frame(matrix(c(8.788269, 1, 0,
+ 7.964719, 6, 0,
+ 8.204051, 12, 0,
+ 9.041368, 24, 0,
+ 8.181555, 48, 0,
+ 8.041419, 96, 0,
+ 7.992336, 144, 0,
+ 7.948658, 1, 1,
+ 8.090211, 6, 1,
+ 8.031459, 12, 1,
+ 8.118308, 24, 1,
+ 7.699051, 48, 1,
+ 7.537120, 96, 1,
+ 7.268570, 144, 1), byrow=T, ncol=3))
> names(xx.2) <- c("value", "time", "treat")
> 
> mod1 <- lm(value~time*treat, data=xx.2)
> anova(mod1)
Analysis of Variance Table

Response: value
           Df  Sum Sq Mean Sq F value  Pr(>F)  
time        1 0.77259 0.77259  8.5858 0.01504 *
treat       1 0.88520 0.88520  9.8372 0.01057 *
time:treat  1 0.03260 0.03260  0.3623 0.56064  
Residuals  10 0.89985 0.08998                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
> mod2 <- aov(value~time*treat, data=xx.2)
> anova(mod2)
Analysis of Variance Table

Response: value
           Df  Sum Sq Mean Sq F value  Pr(>F)  
time        1 0.77259 0.77259  8.5858 0.01504 *
treat       1 0.88520 0.88520  9.8372 0.01057 *
time:treat  1 0.03260 0.03260  0.3623 0.56064  
Residuals  10 0.89985 0.08998                  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
> summary(mod2)
            Df Sum Sq Mean Sq F value Pr(>F)  
time         1 0.7726  0.7726   8.586 0.0150 *
treat        1 0.8852  0.8852   9.837 0.0106 *
time:treat   1 0.0326  0.0326   0.362 0.5606  
Residuals   10 0.8998  0.0900                 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 
> summary(mod1)

Call:
lm(formula = value ~ time * treat, data = xx.2)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.50627 -0.12345  0.00296  0.04124  0.63785 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  8.493476   0.156345  54.325 1.08e-13 ***
time        -0.003748   0.002277  -1.646   0.1307    
treat       -0.411271   0.221106  -1.860   0.0925 .  
time:treat  -0.001938   0.003220  -0.602   0.5606    
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.3 on 10 degrees of freedom
Multiple R-squared: 0.6526,     Adjusted R-squared: 0.5484 
F-statistic: 6.262 on 3 and 10 DF,  p-value: 0.01154 

以上两个答案很好,但我想我会补充一点。可以从这里收集到另一块信息。

当您lm()使用交互项报告结果时,您会说:“当时间设置为基值 1 时,治疗 1 与治疗 0 不同(beta != 0, p=0.0925) ”。anova()结果(如前所述)忽略任何其他变量,只关注方差差异。

您可以通过删除交互项并使用仅具有两个主要影响 ( m1 ) 的简单模型来证明这一点:

> m1 = lm(value~time+treat,data=dat)
> summary(m1)

Call:
lm(formula = value ~ time + treat, data = dat)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.54627 -0.10533 -0.04574  0.11975  0.61528 

Coefficients:
             Estimate Std. Error t value Pr(>|t|)    
(Intercept)  8.539293   0.132545  64.426 1.56e-15 ***
time        -0.004717   0.001562  -3.019  0.01168 *  
treat       -0.502906   0.155626  -3.232  0.00799 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.2911 on 11 degrees of freedom
Multiple R-squared:   0.64, Adjusted R-squared:  0.5746 
F-statistic: 9.778 on 2 and 11 DF,  p-value: 0.003627

> anova(m1)
Analysis of Variance Table

Response: value
          Df  Sum Sq Mean Sq F value   Pr(>F)   
time       1 0.77259 0.77259  9.1142 0.011677 * 
treat      1 0.88520 0.88520 10.4426 0.007994 **
Residuals 11 0.93245 0.08477                    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

在这种情况下,我们看到报告的 p 值是相同的;那是因为在这个更简单的模型中,

  • 差异与级联模型的类型成对比较有关。
  • 此外,aov() 函数在如何选择自由度方面存在问题。它似乎混合了两个概念:1)逐步比较的平方和,2)整体情况的自由度。

问题再现

> data <- list(value = c (8.788269,7.964719,8.204051,9.041368,8.181555,8.0414149,7.992336,7.948658,8.090211,8.031459,8.118308,7.699051,7.537120,7.268570), time = c(1,6,12,24,48,96,144,1,6,12,24,48,96,144), treat = c(0,0,0,0,0,0,0,1,1,1,1,1,1,1) )
> summary( lm(value ~ treat*time, data=data) )
> summary( aov(value ~ 1 + treat + time + I(treat*time),data=data) )

解释中使用的一些模型

#all linear models used in the explanation below
> model_0                      <- lm(value ~ 1, data)
> model_time                   <- lm(value ~ 1 + time, data)
> model_treat                  <- lm(value ~ 1 + treat, data)
> model_interaction            <- lm(value ~ 1 + I(treat*time), data)
> model_treat_time             <- lm(value ~ 1 + treat + time, data)
> model_treat_interaction      <- lm(value ~ 1 + treat + I(treat*time), data)
> model_time_interaction       <- lm(value ~ 1 + time + I(treat*time), data)
> model_treat_time_interaction <- lm(value ~ 1 + time + treat + I(treat*time), data)

LM T_TEST 如何工作以及与 F-TEST 相关

# the t-test with the estimator and it's variance, mean square error, is
# related to the F test of pairwise comparison of models by dropping 1
# model parameter

> anova(model_treat_time_interaction, model_time_interaction)

Analysis of Variance Table

Model 1: value ~ 1 + time + treat + I(treat * time)
Model 2: value ~ 1 + time + I(treat * time)
  Res.Df     RSS Df Sum of Sq      F  Pr(>F)  
1     10 0.89985                              
2     11 1.21118 -1  -0.31133 3.4598 0.09251 .
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

> anova(model_treat_time_interaction, model_treat_interaction)

Analysis of Variance Table

Model 1: value ~ 1 + time + treat + I(treat * time)
Model 2: value ~ 1 + treat + I(treat * time)
  Res.Df     RSS Df Sum of Sq      F Pr(>F)
1     10 0.89985                           
2     11 1.14374 -1   -0.2439 2.7104 0.1307

> anova(model_treat_time_interaction, model_treat_time)

Analysis of Variance Table

Model 1: value ~ 1 + time + treat + I(treat * time)
Model 2: value ~ 1 + treat + time
  Res.Df     RSS Df Sum of Sq      F Pr(>F)
1     10 0.89985                           
2     11 0.93245 -1 -0.032599 0.3623 0.5606

> # which is the same as
> drop1(model_treat_time_interaction, scope  = ~time+treat+I(treat*time), test="F")

Single term deletions

Model:
value ~ 1 + time + treat + I(treat * time)
                Df Sum of Sq     RSS     AIC F value  Pr(>F)  
<none>                       0.89985 -30.424                  
time             1  0.243896 1.14374 -29.067  2.7104 0.13072  
treat            1  0.311333 1.21118 -28.264  3.4598 0.09251 .
I(treat * time)  1  0.032599 0.93245 -31.926  0.3623 0.56064  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

AOV 如何工作并在 F 测试中选择 DF

> #the aov function makes stepwise additions/drops
> 
> #first the time, then treat, then the interaction
> anova(model_0, model_time)

Analysis of Variance Table

Model 1: value ~ 1
Model 2: value ~ 1 + time
  Res.Df    RSS Df Sum of Sq      F  Pr(>F)  
1     13 2.5902                              
2     12 1.8176  1    0.7726 5.1006 0.04333 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

> anova(model_time, model_treat_time)

Analysis of Variance Table

Model 1: value ~ 1 + time
Model 2: value ~ 1 + treat + time
  Res.Df     RSS Df Sum of Sq      F   Pr(>F)   
1     12 1.81764                                
2     11 0.93245  1    0.8852 10.443 0.007994 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

> anova(model_treat_time, model_treat_time_interaction)

Analysis of Variance Table

Model 1: value ~ 1 + treat + time
Model 2: value ~ 1 + time + treat + I(treat * time)
  Res.Df     RSS Df Sum of Sq      F Pr(>F)
1     11 0.93245                           
2     10 0.89985  1  0.032599 0.3623 0.5606

> 
> # note that the sum of squares for within model variation is the same
> # but the F values and p-values are not the same because the aov 
> # function somehow chooses to use the degrees of freedom in the 
> # complete model in all stepwise changes
>

重要的提示

> # Although the p and F values do not exactly match, it is this effect
> # of order and selection of cascading or not in model comparisons. 
> # An important note to make is that the comparisons are made by 
> # stepwise additions and changing the order of variables has an 
> # influence on the outcome!
>
> # Additional note changing the order of 'treat' and 'time' has no 
> # effect because they are not correlated

> summary( aov(value ~ 1 + treat + time +I(treat*time), data=data) )

        Df Sum Sq Mean Sq F value Pr(>F)  
treat            1 0.8852  0.8852   9.837 0.0106 *
time             1 0.7726  0.7726   8.586 0.0150 *
I(treat * time)  1 0.0326  0.0326   0.362 0.5606  
Residuals       10 0.8998  0.0900                 
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

> summary( aov(value ~ 1 + I(treat*time) + treat + time, data=data) )

                Df Sum Sq Mean Sq F value  Pr(>F)   
I(treat * time)  1 1.3144  1.3144  14.606 0.00336 **
treat            1 0.1321  0.1321   1.469 0.25343   
time             1 0.2439  0.2439   2.710 0.13072   
Residuals       10 0.8998  0.0900                   
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

> # This is an often forgotten quirck 
> # best is to use manual comparisons such that you know
> # and understand your hypotheses
> # (which is often forgotten in the click and
> #     point anova modelling tools)
> #
> # anova(model1, model2) 
> #     or use 
> # stepAIC from the MASS library