One-way ANCOVA with R Example

Data: WOW_data


1.獨立樣本單因子共變異數分析(One-way ANCOVA)

  某研究者本來想探討三個種族(ethnic)之間的閱讀表現(reading_3)是否存在著差異,但後來發現二年級時學生自評的師生衝突(CO_S_2)可能是一個重要影響因子,因此,研究者利用統計手法來排除學生自評師生衝突因子的干擾後,針對三種種族來進行閱讀表現的均值比較。
  

用R讀取剛剛的CSV檔,並將此資料命名為acov_raw選取欲分析變項並將資料命名為acov

acov_raw    <- read.csv("D:/104/ML_R/WOW_data.csv", header=TRUE, sep=",")
acov<-acov_raw[c(3,12,21)]
head(acov)
##   ethnic reading_3   CO_S_2
## 1 Human        492 1.000000
## 2 Human        494 1.000000
## 3 Human        523 1.333333
## 4 Human        476 1.833333
## 5 Human        515 1.166667
## 6 Human        450 2.166667

進行組內迴歸係數同質檢定

mod1    <- aov(reading_3 ~ CO_S_2*ethnic ,data=acov)
summary(mod1)
##                Df Sum Sq Mean Sq F value  Pr(>F)    
## CO_S_2          1    509     509   1.614   0.205    
## ethnic          2  10293    5146  16.333 2.9e-07 ***
## CO_S_2:ethnic   2    267     134   0.424   0.655    
## Residuals     187  58922     315                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

進行ANCOVA分析

mod2    <- aov(reading_3 ~ CO_S_2+ethnic,data=acov)
summary(mod2)
##              Df Sum Sq Mean Sq F value   Pr(>F)    
## CO_S_2        1    509     509   1.624    0.204    
## ethnic        2  10293    5146  16.433 2.63e-07 ***
## Residuals   189  59189     313                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

原始平均數

aggregate(reading_3 ~ethnic ,mean ,data=acov)
##    ethnic reading_3
## 1  Human   505.5287
## 2    Orc   486.0476
## 3 Undead   499.6094

調整後的平均數

library(effects)
adj.mean<-effect("ethnic",mod2)
data.frame(adj.mean)
##    ethnic      fit       se    lower    upper
## 1  Human  505.4801 1.901756 501.7287 509.2315
## 2    Orc  486.1919 2.757910 480.7516 491.6321
## 3 Undead  499.5808 2.213402 495.2147 503.9470

進行事後比較

library(multcomp)
postHocs <-glht(mod2, linfct=mcp(ethnic="Tukey"))
summary(postHocs)
## 
##   Simultaneous Tests for General Linear Hypotheses
## 
## Multiple Comparisons of Means: Tukey Contrasts
## 
## 
## Fit: aov(formula = reading_3 ~ CO_S_2 + ethnic, data = acov)
## 
## Linear Hypotheses:
##                       Estimate Std. Error t value Pr(>|t|)    
## Orc  - Human  == 0     -19.288      3.365  -5.732   <0.001 ***
## Undead  - Human  == 0   -5.899      2.915  -2.024    0.108    
## Undead  - Orc  == 0     13.389      3.545   3.777   <0.001 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## (Adjusted p values reported -- single-step method)

繪製平均數圖

plot(adj.mean)

繪製迴歸線及散佈圖

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:MASS':
## 
##     select
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
p1<-ggplot(acov, aes(CO_S_2, reading_3)) + facet_grid(. ~ ethnic)+ geom_point()  + stat_smooth(method="lm")
ggplotly(p1)
p2<-ggplot(acov, aes(x = CO_S_2, y = reading_3, color = ethnic)) + geom_point()  + stat_smooth(method="lm")
ggplotly(p2)

結論

單因子ANCOVA變異數分析表

 組內迴歸係數同質檢定結果顯示出:共變項(X)與自變項(Y)的交互作用未達顯著水準 (p=.655),所以各組的迴歸係數同質(各組的斜率可以視為相同;共變項與應變項之線性關係在自變項內皆為一致)。三種種族的閱讀成績均值為486.05,499.61,505.53,而其調整後的後測平均值為486.19, 499.58, 505.48。由單因子ANCOVA變異數分析表可知,三種種族的學生,其閱讀成績並不完全相等(F=16.433, p = .001)。
由事後比較表與剖面圖可以發現,除了種族Human跟種族Undead並無顯著差異外,其餘兩兩種族之均值差異皆達顯著水準(Human=Undead>Orc)


date: “2016年1月18日,第一版”

author: “邱浩恩”