协方差分析 | 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.
在实验设计中,协变量是独立变量,实验者不能操纵,但仍影响实验结果。
我想知道温度对于降水量的影响,但是海拔高度、经纬度、当地湿度等变量也会影响降水量。那么,在我的研究中,温度就是自变量,降水量是应变量,而海拔高度、经纬度和当地湿度就是协变量。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | > 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 |
参考:
Analysis of Covariance (ANCOVA) easily explained
Analysis of Covariance (ANCOVA) with Two Groups
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步