[英]2-way ANOVA, is R comparing the correct data?
Year Location AVGCover
2010 1 0.1
2010 1 0.5
2010 1 1
2010 2 0.75
2010 2 0.8
2010 2 1.6
2010 3 1.1
2010 3 0.5
2010 3 0.6
2011 1 0.2
2011 1 0.2
2011 1 0.3
2011 2 0.5
2011 2 0.7
2011 2 0.4
2011 3 0.6
2011 3 0.1
2011 3 0
我制作了數據集的一小部分,它查看了2年中3個位置的平均百分比覆蓋率。 我相信我將需要進行2次方差分析作為統計測試,但是,我遇到了一些麻煩。 到目前為止,這是我的代碼:
anova(mod1 <- lm(df$AVGCover ~ df$Location + df$Year + df$Location *
df$Year)
pairwise.t.test(df$AVGCover, df$Year, p.adj = "none")
pairwise.t.test(df$AVGCover, df$Location, p.adj = "none")
具體來說,我希望查看2010年和2011年的位置1的比較,但是當我運行pairwise.t.test R時,僅比較位置1和位置2等。我想確保我的R代碼正在尋找在進行我想要的比較時,但是我通常不確定,因此希望能有所幫助。
我的方差分析輸出的最后一句話是我的df = 1,我不確定情況是否如此。 我要去哪里錯了?
您應該使用正確的結構創建數據。 Year和Location顯然是離散的,即R因子。 並且您應該使用R公式界面。 公式AVGCover ~ Location * Year
包括所有主要影響:
txt <- "Year Location AVGCover
2010 1 0.1
2010 1 0.5
2010 1 1
2010 2 0.75
2010 2 0.8
2010 2 1.6
2010 3 1.1
2010 3 0.5
2010 3 0.6
2011 1 0.2
2011 1 0.2
2011 1 0.3
2011 2 0.5
2011 2 0.7
2011 2 0.4
2011 3 0.6
2011 3 0.1
2011 3 0"
dfrm <- read.table(text=txt, header=TRUE, colClasses=c("factor", "factor", "numeric") )
注意,我沒有使用df
作為名稱,因為那是F分布密度的函數名稱。
anova(mod1 <- lm(AVGCover ~ Location * Year, data=dfrm))
Analysis of Variance Table
Response: AVGCover
Df Sum Sq Mean Sq F value Pr(>F)
Location 2 0.54361 0.27181 2.4555 0.12767
Year 1 0.86681 0.86681 7.8306 0.01609 *
Location:Year 2 0.04361 0.02181 0.1970 0.82380
Residuals 12 1.32833 0.11069
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
>
> pairwise.t.test(dfrm$AVGCover, dfrm$Year, p.adj = "none")
Pairwise comparisons using t tests with pooled SD
data: dfrm$AVGCover and dfrm$Year
2010
2011 0.016
P value adjustment method: none
> pairwise.t.test(dfrm$AVGCover, dfrm$Location, p.adj = "none")
為了獲得估計差異的大小,您需要傳遞與公式RHS上的IV具有相同名稱(和類)的數據框:
predict(mod1, newdata =data.frame(Location=factor(1), Year=factor(c(2010,2011)) ) )
# returns
1 2
0.5333333 0.2333333
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.