协方差分析 | ANCOVA (Analysis of Covariance) | R代码

协方差分析是方差分析、回归分析和协方差的结合体。

我觉得这种分析思想非常实用,尤其是对confounder云集的生物学数据。

 

回顾:

什么是协方差?co-vary

协方差和相关性?standardize

 

协方差分析最经典的一个例子就是GWAS中移除SNP中的人种因素。

If you are worried about leaving out covariates you could regress out them first and analyse the residuals against the Snps.
在实验设计中,协变量是独立变量,实验者不能操纵,但仍影响实验结果。
我想知道温度对于降水量的影响,但是海拔高度、经纬度、当地湿度等变量也会影响降水量。那么,在我的研究中,温度就是自变量,降水量是应变量,而海拔高度、经纬度和当地湿度就是协变量。

 

> input <- mtcars[,c("am","mpg","hp")]
> print(head(input))
                  am  mpg  hp
Mazda RX4          1 21.0 110
Mazda RX4 Wag      1 21.0 110
Datsun 710         1 22.8  93
Hornet 4 Drive     0 21.4 110
Hornet Sportabout  0 18.7 175
Valiant            0 18.1 105
> # Get the dataset.
> input <- mtcars
> # Create the regression model.
> result <- aov(mpg~hp*am,data = input)
> print(summary(result))
            Df Sum Sq Mean Sq F value   Pr(>F)    
hp           1  678.4   678.4  77.391 1.50e-09 ***
am           1  202.2   202.2  23.072 4.75e-05 ***
hp:am        1    0.0     0.0   0.001    0.981    
Residuals   28  245.4     8.8                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> # Get the dataset.
> input <- mtcars
> # Create the regression model.
> result <- aov(mpg~hp+am,data = input)
> print(summary(result))
            Df Sum Sq Mean Sq F value   Pr(>F)    
hp           1  678.4   678.4   80.15 7.63e-10 ***
am           1  202.2   202.2   23.89 3.46e-05 ***
Residuals   29  245.4     8.5                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
> # Get the dataset.
> input <- mtcars
> # Create the regression models.
> result1 <- aov(mpg~hp*am,data = input)
> result2 <- aov(mpg~hp+am,data = input)
> # Compare the two models.
> print(anova(result1,result2))
Analysis of Variance Table

Model 1: mpg ~ hp * am
Model 2: mpg ~ hp + am
  Res.Df    RSS Df  Sum of Sq     F Pr(>F)
1     28 245.43                           
2     29 245.44 -1 -0.0052515 6e-04 0.9806

  

 

参考:

R - Analysis of Covariance

Analysis of Covariance (ANCOVA) easily explained

Analysis of Covariance (ANCOVA) with Two Groups

 

posted @ 2018-05-09 12:33  Life·Intelligence  阅读(9955)  评论(0编辑  收藏  举报
TOP