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


22

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

> 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

由于缺少重复项,因此将时间视为连续变量。列“处理”分别显示案例和控制数据。

首先,我将模型“ value = time * treat”与“ lm”拟合为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    

时间和待遇的价值并不重要。

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

 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                 

时间和治疗的p值已更改。

对于线性回归,如果我是对的,则意味着时间和待遇对价值无重大影响,而对于方差分析,这意味着时间和待遇对价值无重大影响。

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


3
您可能需要查找不同种类的平方和。具体来说,我相信线性回归会返回III型平方和,而方差分析会返回另一种类型。
假设正常时间2012年

3
如果您保存的结果lm,并aov可以检查它们产生相同的配合; 例如,将其残差与residuals函数进行比较或检查其系数($coefficients在两种情况下均为时隙)。
ub

Answers:


18

lm()和aov()的拟合度相同,但是报告不同。给定所有其他变量,t检验是所讨论变量的边际影响。F测试是顺序的-因此,它们在没有拦截的情况下测试时间的重要性,在没有拦截和时间的情况下进行测试,以及在以上所有条件下的交互作用下,测试时间的重要性。

假设您对治疗的重要性感兴趣,我建议您拟合两个模型,一个有模型,一个没有模型,将两个模型都放在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 

感谢您的详尽解释,它使我想起了ANCOVA(协方差分析)。ANCOVA的第一步是测试分类因子和协变量之间的相互作用,以了解它们在两种情况下是否具有相同的斜率。这与我在这里所做的非常相似。在ANCOVA中,由于交互作用是中的最后一项,因此它在t检验和F检验中给出了相同的交互作用p值aov


2

上面的两个答案很好,但是我想再加一点。可以从这里收集到另一个信息。

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值相同;这是因为在这种简单模型的情况下,


不幸的是,这个答案看起来还没有完成。仍然为该链接+1,并且提及效果是由于不同的编码方案。
变形虫说莫妮卡(Monica)恢复

2
还应该补充一点summary(lm)anova(lm)如果没有交互项,也不会总是给出相同的结果。碰巧的是,在这些数据中timetreat它们是正交的,因此类型I(顺序)和III(边际)的平方和得出相同的结果。
变形虫说莫妮卡(Monica)恢复

2
  • 差异与级联模型的类型成对比较有关。
  • 另外,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
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.