今天介紹一下單因素方差分析可視化的内容,主要是實作如下圖:
分組平均值+标準差
1. 資料
library(agricolae)
data(sweetpotato)
head(sweetpotato)
str(sweetpotato)
試驗描述:
這些資料與在秘魯南部塔克納省進行的一項實驗相符。研究了兩種病毒(Spfmv和Spcsv)的作用。處理方法如下:CC(Spcsv)=甘薯褪綠矮稈,FF(Spfmv)=羽狀斑駁,FC(Spfmv y Spcsv)=病毒複合物和OO(見證)健康植物。每個小區種植甘薯50株,共12個小區。每次治療重複3次,實驗結束時評估總重量(千克)。病毒通過插條傳播,插條在田間播種。
2. 方差分析
mod1 = aov(yield~virus,data=sweetpotato)
summary(mod1)
可以看出,不同病毒之間達到極顯著。可以進行多重比較。
3. 多重比較
re = LSD.test(mod1,"virus")
re
結果:
$statistics
MSerror Df Mean CV t.value LSD
22.48917 8 27.625 17.1666 2.306004 8.928965
$parameters
test p.ajusted name.t ntr alpha
Fisher-LSD none virus 4 0.05
$means
yield std r LCL UCL Min Max Q25 Q50 Q75
cc 24.40000 3.609709 3 18.086268 30.71373 21.7 28.5 22.35 23.0 25.75
fc 12.86667 2.159475 3 6.552935 19.18040 10.6 14.9 11.85 13.1 14.00
ff 36.33333 7.333030 3 30.019601 42.64707 28.0 41.8 33.60 39.2 40.50
oo 36.90000 4.300000 3 30.586268 43.21373 32.1 40.4 35.15 38.2 39.30
$comparison
NULL
$groups
yield groups
oo 36.90000 a
ff 36.33333 a
cc 24.40000 b
fc 12.86667 c
attr(,"class")
[1] "group"
4. 多重比較可視化
re1 = re$groups
re1
# 計算品種标準誤
xx = aggregate(yield ~ virus, sweetpotato,sd)
names(xx) = c("virus","sd")
xx
re2 = re1 %>% mutate(virus = rownames(re1)) %>% inner_join(.,xx,by="virus")
re2
# 作圖
## 做直方圖
re2 %>% ggplot(aes(virus,yield)) + geom_col(aes(fill = virus), width=.4) +
geom_errorbar(aes(ymax = yield + sd, ymin = yield - sd),width = .1,size=.5)+
geom_text(aes(label = groups,y = yield + sd +1.5)) + theme(panel.grid = element_blank(), panel.background = element_rect(color = "black",fill = "transparent"))
5. 完整代碼
library(agricolae)
data(sweetpotato)
mod1 = aov(yield~virus,data=sweetpotato)
summary(mod1)
re = LSD.test(mod1,"virus")
re
re1 = re$groups
re1
# 計算品種标準誤
xx = aggregate(yield ~ virus, sweetpotato,sd)
names(xx) = c("virus","sd")
xx
re2 = re1 %>% mutate(virus = rownames(re1)) %>% inner_join(.,xx,by="virus")
re2
# 作圖
## 做直方圖
re2 %>% ggplot(aes(virus,yield)) + geom_col(aes(fill = virus), width=.4) +
geom_errorbar(aes(ymax = yield + sd, ymin = yield - sd),width = .1,size=.5)+
geom_text(aes(label = groups,y = yield + sd +1.5)) + theme(panel.grid = element_blank(), panel.background = element_rect(color = "black",fill = "transparent"))
6. R語言太難?來用Genstat吧
6.1 導入資料
6.2 選擇方差分析模型
Analysis of variance
Variate: yield
Source of variation d.f. s.s. m.s. v.r. F pr.
virus 3 1170.21 390.07 17.34 <.001
Residual 8 179.91 22.49
Total 11 1350.12
Message: the following units have large residuals.
*units* 9 -8.3 s.e. 3.9
Tables of means
Variate: yield
Grand mean 27.6
virus cc fc ff oo
24.4 12.9 36.3 36.9
Standard errors of differences of means
Table virus
rep. 3
d.f. 8
s.e.d. 3.87
Least significant differences of means (5% level)
Table virus
rep. 3
d.f. 8
l.s.d. 8.93
6.3 多重比較
Fisher's protected least significant difference test
virus
Mean
oo 36.90 a
ff 36.33 a
cc 24.40 b
fc 12.87 c