《R語言實(shí)戰(zhàn)》自學(xué)筆記61-重復(fù)測量方差分析

9.6 重復(fù)測量方差分析

所謂重復(fù)測量方差分析,即受試者被測量不止一次。本節(jié)重點(diǎn)關(guān)注含一個組內(nèi)和一個組間因子的重復(fù)測量方差分析(這是一個常見的設(shè)計(jì))。
以下為R語言實(shí)戰(zhàn)示例浓恶。基礎(chǔ)安裝包中的CO2數(shù)據(jù)集包含了北方和南方牧草類植物Echinochloa crus-galli(Potvin结笨,Lechowicz包晰,Tardif,1990)的寒冷容忍度研究結(jié)果炕吸,在某濃度二氧化碳的環(huán)境中伐憾,對寒帶植物與非寒帶植物的光合作用率進(jìn)行了比較。研究所用植物一半來自于加拿大的魁北克省赫模,另一半來自美國的密西西比州树肃。因變量是二氧化碳吸收量(uptake),單位為ml/L瀑罗,自變量是植物類型Type(魁北克VS密西西比州)和七種水平(95~1000 umol/m^2 sec)的二氧化碳濃度(conc)胸嘴。另外,Type是組間因子廓脆,conc是組內(nèi)因子筛谚。

CO2
##    Plant        Type  Treatment conc uptake
## 1    Qn1      Quebec nonchilled   95   16.0
## 2    Qn1      Quebec nonchilled  175   30.4
## 3    Qn1      Quebec nonchilled  250   34.8
## 4    Qn1      Quebec nonchilled  350   37.2
## 5    Qn1      Quebec nonchilled  500   35.3
## 6    Qn1      Quebec nonchilled  675   39.2
## 7    Qn1      Quebec nonchilled 1000   39.7
## 8    Qn2      Quebec nonchilled   95   13.6
## 9    Qn2      Quebec nonchilled  175   27.3
## 10   Qn2      Quebec nonchilled  250   37.1
## 11   Qn2      Quebec nonchilled  350   41.8
## 12   Qn2      Quebec nonchilled  500   40.6
## 13   Qn2      Quebec nonchilled  675   41.4
## 14   Qn2      Quebec nonchilled 1000   44.3
## 15   Qn3      Quebec nonchilled   95   16.2
## 16   Qn3      Quebec nonchilled  175   32.4
## 17   Qn3      Quebec nonchilled  250   40.3
## 18   Qn3      Quebec nonchilled  350   42.1
## 19   Qn3      Quebec nonchilled  500   42.9
## 20   Qn3      Quebec nonchilled  675   43.9
## 21   Qn3      Quebec nonchilled 1000   45.5
## 22   Qc1      Quebec    chilled   95   14.2
## 23   Qc1      Quebec    chilled  175   24.1
## 24   Qc1      Quebec    chilled  250   30.3
## 25   Qc1      Quebec    chilled  350   34.6
## 26   Qc1      Quebec    chilled  500   32.5
## 27   Qc1      Quebec    chilled  675   35.4
## 28   Qc1      Quebec    chilled 1000   38.7
## 29   Qc2      Quebec    chilled   95    9.3
## 30   Qc2      Quebec    chilled  175   27.3
## 31   Qc2      Quebec    chilled  250   35.0
## 32   Qc2      Quebec    chilled  350   38.8
## 33   Qc2      Quebec    chilled  500   38.6
## 34   Qc2      Quebec    chilled  675   37.5
## 35   Qc2      Quebec    chilled 1000   42.4
## 36   Qc3      Quebec    chilled   95   15.1
## 37   Qc3      Quebec    chilled  175   21.0
## 38   Qc3      Quebec    chilled  250   38.1
## 39   Qc3      Quebec    chilled  350   34.0
## 40   Qc3      Quebec    chilled  500   38.9
## 41   Qc3      Quebec    chilled  675   39.6
## 42   Qc3      Quebec    chilled 1000   41.4
## 43   Mn1 Mississippi nonchilled   95   10.6
## 44   Mn1 Mississippi nonchilled  175   19.2
## 45   Mn1 Mississippi nonchilled  250   26.2
## 46   Mn1 Mississippi nonchilled  350   30.0
## 47   Mn1 Mississippi nonchilled  500   30.9
## 48   Mn1 Mississippi nonchilled  675   32.4
## 49   Mn1 Mississippi nonchilled 1000   35.5
## 50   Mn2 Mississippi nonchilled   95   12.0
## 51   Mn2 Mississippi nonchilled  175   22.0
## 52   Mn2 Mississippi nonchilled  250   30.6
## 53   Mn2 Mississippi nonchilled  350   31.8
## 54   Mn2 Mississippi nonchilled  500   32.4
## 55   Mn2 Mississippi nonchilled  675   31.1
## 56   Mn2 Mississippi nonchilled 1000   31.5
## 57   Mn3 Mississippi nonchilled   95   11.3
## 58   Mn3 Mississippi nonchilled  175   19.4
## 59   Mn3 Mississippi nonchilled  250   25.8
## 60   Mn3 Mississippi nonchilled  350   27.9
## 61   Mn3 Mississippi nonchilled  500   28.5
## 62   Mn3 Mississippi nonchilled  675   28.1
## 63   Mn3 Mississippi nonchilled 1000   27.8
## 64   Mc1 Mississippi    chilled   95   10.5
## 65   Mc1 Mississippi    chilled  175   14.9
## 66   Mc1 Mississippi    chilled  250   18.1
## 67   Mc1 Mississippi    chilled  350   18.9
## 68   Mc1 Mississippi    chilled  500   19.5
## 69   Mc1 Mississippi    chilled  675   22.2
## 70   Mc1 Mississippi    chilled 1000   21.9
## 71   Mc2 Mississippi    chilled   95    7.7
## 72   Mc2 Mississippi    chilled  175   11.4
## 73   Mc2 Mississippi    chilled  250   12.3
## 74   Mc2 Mississippi    chilled  350   13.0
## 75   Mc2 Mississippi    chilled  500   12.5
## 76   Mc2 Mississippi    chilled  675   13.7
## 77   Mc2 Mississippi    chilled 1000   14.4
## 78   Mc3 Mississippi    chilled   95   10.6
## 79   Mc3 Mississippi    chilled  175   18.0
## 80   Mc3 Mississippi    chilled  250   17.9
## 81   Mc3 Mississippi    chilled  350   17.9
## 82   Mc3 Mississippi    chilled  500   17.9
## 83   Mc3 Mississippi    chilled  675   18.9
## 84   Mc3 Mississippi    chilled 1000   19.9
CO2$conc <- as.factor(CO2$conc)
w1b1 <- subset(CO2, Treatment == 'chilled')
fit16 <- aov(uptake ~ (conc*Type) + Error(Plant/conc), w1b1)
summary(fit16)
## 
## Error: Plant
##           Df Sum Sq Mean Sq F value  Pr(>F)   
## Type       1 2667.2  2667.2   60.41 0.00148 **
## Residuals  4  176.6    44.1                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: Plant:conc
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## conc       6 1472.4  245.40   52.52 1.26e-12 ***
## conc:Type  6  428.8   71.47   15.30 3.75e-07 ***
## Residuals 24  112.1    4.67                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
par(las=2)
par=(mar=c(10, 4, 4, 2))
with(w1b1, interaction.plot(conc, Type, uptake, type="b", col=c("red", "blue"), pch=c(16, 18), main="Interaction Plot for Plant Type and Concentraction"))
image.png
boxplot(uptake ~ Type*conc, data=w1b1, col=(c("gold", "green")), main = "Chilled Quebec and Mississippi Plants", ylab = "Carbon dioxide uptake rate (umol/m^2 sec)")
image.png

百度學(xué)習(xí)了一下磁玉,重復(fù)測量方差分析的R格式:model=aov(Y ~ B * W + Error(Subject/W))停忿,其中B是組間因子,W是組內(nèi)因子蚊伞,subject是實(shí)驗(yàn)對象的ID席赂。

下面自己構(gòu)建一個數(shù)據(jù)集df7演示學(xué)習(xí),數(shù)據(jù)包括兩個N水平(N200,N300)时迫,每個氮水平下分別在5個時期測定兩個品種5株的biomass颅停。這里N是組間因子,T是組內(nèi)因子掠拳。

df7 <- read.table(file = "D:/Documents/R wd/df7.csv", header = T, sep = ",") # 數(shù)據(jù)導(dǎo)入癞揉。
df7 # 查看數(shù)據(jù)。
##    Plant   N  t1  t2   t3   t4   t5
## 1     Q1 200 0.1 4.5  9.9 11.0 15.2
## 2     Q1 200 0.2 4.2  8.1 12.2 15.0
## 3     Q1 200 0.1 4.0  8.3 11.8 15.1
## 4     Q1 200 0.2 4.7  7.2 12.8 16.0
## 5     Q1 200 0.1 4.5  7.1 11.5 15.4
## 6     Q2 200 0.3 4.6  8.0 12.7 15.8
## 7     Q2 200 0.1 4.8  9.6 12.5 15.4
## 8     Q2 200 0.3 5.3  9.2 13.0 14.8
## 9     Q2 200 0.4 5.5  9.5 13.5 14.9
## 10    Q2 200 0.2 5.2 10.0 13.8 15.0
## 11    M1 300 0.8 8.6 15.0 19.5 23.5
## 12    M1 300 0.9 8.8 14.7 19.6 23.8
## 13    M1 300 0.7 9.0 16.5 19.4 23.4
## 14    M1 300 0.8 8.4 15.2 18.8 25.1
## 15    M1 300 0.7 8.2 16.5 18.6 25.0
## 16    M2 300 0.6 8.1 14.8 18.0 24.8
## 17    M2 300 0.9 7.9 15.8 19.0 24.7
## 18    M2 300 1.0 7.5 16.8 18.7 24.3
## 19    M2 300 0.8 7.8 14.6 18.6 24.9
## 20    M2 300 0.9 7.7 14.2 18.7 25.0
attach(df7) # 將數(shù)據(jù)集df7加入搜索路徑溺欧。
library(reshape2) # 調(diào)用reshape2包喊熟。
w2b2 <- melt(df7,id = c("N","Plant"),variable.name = "T", value.name="Biomass") # 寬格式數(shù)據(jù)變?yōu)殚L格式。
w2b2 # 查看數(shù)據(jù)姐刁。
##       N Plant  T Biomass
## 1   200    Q1 t1     0.1
## 2   200    Q1 t1     0.2
## 3   200    Q1 t1     0.1
## 4   200    Q1 t1     0.2
## 5   200    Q1 t1     0.1
## 6   200    Q2 t1     0.3
## 7   200    Q2 t1     0.1
## 8   200    Q2 t1     0.3
## 9   200    Q2 t1     0.4
## 10  200    Q2 t1     0.2
## 11  300    M1 t1     0.8
## 12  300    M1 t1     0.9
## 13  300    M1 t1     0.7
## 14  300    M1 t1     0.8
## 15  300    M1 t1     0.7
## 16  300    M2 t1     0.6
## 17  300    M2 t1     0.9
## 18  300    M2 t1     1.0
## 19  300    M2 t1     0.8
## 20  300    M2 t1     0.9
## 21  200    Q1 t2     4.5
## 22  200    Q1 t2     4.2
## 23  200    Q1 t2     4.0
## 24  200    Q1 t2     4.7
## 25  200    Q1 t2     4.5
## 26  200    Q2 t2     4.6
## 27  200    Q2 t2     4.8
## 28  200    Q2 t2     5.3
## 29  200    Q2 t2     5.5
## 30  200    Q2 t2     5.2
## 31  300    M1 t2     8.6
## 32  300    M1 t2     8.8
## 33  300    M1 t2     9.0
## 34  300    M1 t2     8.4
## 35  300    M1 t2     8.2
## 36  300    M2 t2     8.1
## 37  300    M2 t2     7.9
## 38  300    M2 t2     7.5
## 39  300    M2 t2     7.8
## 40  300    M2 t2     7.7
## 41  200    Q1 t3     9.9
## 42  200    Q1 t3     8.1
## 43  200    Q1 t3     8.3
## 44  200    Q1 t3     7.2
## 45  200    Q1 t3     7.1
## 46  200    Q2 t3     8.0
## 47  200    Q2 t3     9.6
## 48  200    Q2 t3     9.2
## 49  200    Q2 t3     9.5
## 50  200    Q2 t3    10.0
## 51  300    M1 t3    15.0
## 52  300    M1 t3    14.7
## 53  300    M1 t3    16.5
## 54  300    M1 t3    15.2
## 55  300    M1 t3    16.5
## 56  300    M2 t3    14.8
## 57  300    M2 t3    15.8
## 58  300    M2 t3    16.8
## 59  300    M2 t3    14.6
## 60  300    M2 t3    14.2
## 61  200    Q1 t4    11.0
## 62  200    Q1 t4    12.2
## 63  200    Q1 t4    11.8
## 64  200    Q1 t4    12.8
## 65  200    Q1 t4    11.5
## 66  200    Q2 t4    12.7
## 67  200    Q2 t4    12.5
## 68  200    Q2 t4    13.0
## 69  200    Q2 t4    13.5
## 70  200    Q2 t4    13.8
## 71  300    M1 t4    19.5
## 72  300    M1 t4    19.6
## 73  300    M1 t4    19.4
## 74  300    M1 t4    18.8
## 75  300    M1 t4    18.6
## 76  300    M2 t4    18.0
## 77  300    M2 t4    19.0
## 78  300    M2 t4    18.7
## 79  300    M2 t4    18.6
## 80  300    M2 t4    18.7
## 81  200    Q1 t5    15.2
## 82  200    Q1 t5    15.0
## 83  200    Q1 t5    15.1
## 84  200    Q1 t5    16.0
## 85  200    Q1 t5    15.4
## 86  200    Q2 t5    15.8
## 87  200    Q2 t5    15.4
## 88  200    Q2 t5    14.8
## 89  200    Q2 t5    14.9
## 90  200    Q2 t5    15.0
## 91  300    M1 t5    23.5
## 92  300    M1 t5    23.8
## 93  300    M1 t5    23.4
## 94  300    M1 t5    25.1
## 95  300    M1 t5    25.0
## 96  300    M2 t5    24.8
## 97  300    M2 t5    24.7
## 98  300    M2 t5    24.3
## 99  300    M2 t5    24.9
## 100 300    M2 t5    25.0
str(w2b2) # 查看數(shù)據(jù)結(jié)構(gòu)芥牌。
## 'data.frame':    100 obs. of  4 variables:
##  $ N      : int  200 200 200 200 200 200 200 200 200 200 ...
##  $ Plant  : chr  "Q1" "Q1" "Q1" "Q1" ...
##  $ T      : Factor w/ 5 levels "t1","t2","t3",..: 1 1 1 1 1 1 1 1 1 1 ...
##  $ Biomass: num  0.1 0.2 0.1 0.2 0.1 0.3 0.1 0.3 0.4 0.2 ...
w2b2$N <- as.factor(w2b2$N) # df7數(shù)據(jù)集中N轉(zhuǎn)為因子。
w2b2$T <- as.factor(w2b2$T) # df7數(shù)據(jù)集中T轉(zhuǎn)為因子聂使。
fit17 <- aov(Biomass ~ T*N + Error(Plant/T), w2b2) # 構(gòu)建模型壁拉。
summary(fit17)  # 返回結(jié)果谬俄。
## 
## Error: Plant
##           Df Sum Sq Mean Sq F value  Pr(>F)   
## N          1  697.0   697.0   267.9 0.00371 **
## Residuals  2    5.2     2.6                   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: Plant:T
##           Df Sum Sq Mean Sq F value   Pr(>F)    
## T          4   4643  1160.7 1366.71 2.28e-11 ***
## T:N        4    219    54.7   64.36 4.03e-06 ***
## Residuals  8      7     0.8                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Error: Within
##           Df Sum Sq Mean Sq F value Pr(>F)
## Residuals 80  25.16  0.3145
par(las=2) # las 參數(shù)控制x軸和y軸的刻度線上的標(biāo)簽與兩條軸的方向,可選值為0,1,2,3弃理,默認(rèn)值0表示總是平行于坐標(biāo)軸溃论;1表示總是水平方向;2表示總是垂直于坐標(biāo)軸痘昌;3表示總是垂直方向蔬芥。
par(mar=c(10,4,4,2)) # 設(shè)置圖形空白邊界行數(shù),mar=c(bottom,left,top,right)控汉,缺省為mar=c(5.1,4.1,4.1,2.1)
with(w2b2, interaction.plot(T,N,Biomass, type = "b", col = c("red","blue"), pch = c(16,18), main = "Interaction Plot for Nitrogen and Time")) # 結(jié)果可視化笔诵。
image.png
boxplot(Biomass ~ N*T, data=w2b2, col=(c("gold","green")),
        main="Nitrogen and Time", 
        ylab="Biomass") # 結(jié)果可視化。
image.png

參考資料:

  1. 《R語言實(shí)戰(zhàn)》(中文版)姑子,人民郵電出版社乎婿,2013.
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市街佑,隨后出現(xiàn)的幾起案子谢翎,更是在濱河造成了極大的恐慌,老刑警劉巖沐旨,帶你破解...
    沈念sama閱讀 217,277評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件森逮,死亡現(xiàn)場離奇詭異,居然都是意外死亡磁携,警方通過查閱死者的電腦和手機(jī)褒侧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谊迄,“玉大人闷供,你說我怎么就攤上這事⊥撑担” “怎么了歪脏?”我有些...
    開封第一講書人閱讀 163,624評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長粮呢。 經(jīng)常有香客問我婿失,道長,這世上最難降的妖魔是什么啄寡? 我笑而不...
    開封第一講書人閱讀 58,356評論 1 293
  • 正文 為了忘掉前任豪硅,我火速辦了婚禮,結(jié)果婚禮上这难,老公的妹妹穿的比我還像新娘舟误。我一直安慰自己,他們只是感情好姻乓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評論 6 392
  • 文/花漫 我一把揭開白布嵌溢。 她就那樣靜靜地躺著眯牧,像睡著了一般。 火紅的嫁衣襯著肌膚如雪赖草。 梳的紋絲不亂的頭發(fā)上学少,一...
    開封第一講書人閱讀 51,292評論 1 301
  • 那天,我揣著相機(jī)與錄音秧骑,去河邊找鬼版确。 笑死,一個胖子當(dāng)著我的面吹牛乎折,可吹牛的內(nèi)容都是我干的绒疗。 我是一名探鬼主播,決...
    沈念sama閱讀 40,135評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼骂澄,長吁一口氣:“原來是場噩夢啊……” “哼吓蘑!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起坟冲,我...
    開封第一講書人閱讀 38,992評論 0 275
  • 序言:老撾萬榮一對情侶失蹤磨镶,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后健提,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體琳猫,經(jīng)...
    沈念sama閱讀 45,429評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評論 3 334
  • 正文 我和宋清朗相戀三年私痹,在試婚紗的時候發(fā)現(xiàn)自己被綠了脐嫂。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,785評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡侄榴,死狀恐怖雹锣,靈堂內(nèi)的尸體忽然破棺而出网沾,到底是詐尸還是另有隱情癞蚕,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評論 5 345
  • 正文 年R本政府宣布辉哥,位于F島的核電站桦山,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏醋旦。R本人自食惡果不足惜恒水,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望饲齐。 院中可真熱鬧钉凌,春花似錦、人聲如沸捂人。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至酸纲,卻和暖如春捣鲸,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背闽坡。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評論 1 269
  • 我被黑心中介騙來泰國打工栽惶, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人疾嗅。 一個月前我還...
    沈念sama閱讀 47,891評論 2 370
  • 正文 我出身青樓外厂,卻偏偏與公主長得像,于是被迫代替她去往敵國和親代承。 傳聞我的和親對象是個殘疾皇子酣衷,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評論 2 354

推薦閱讀更多精彩內(nèi)容