2
我嘗試使用下面的代碼來計算SSB和SSW「手動」:計算ANOVA用手
> data.soy
protein amount
1 cereal 5
2 cereal 17
3 cereal 12
4 cereal 10
5 cereal 4
6 energy 19
7 energy 10
8 energy 9
9 energy 7
10 energy 5
11 veggie 25
12 veggie 15
13 veggie 12
14 veggie 9
15 veggie 8
> soy.fit<-lm(amount~protein, data=data.soy)
> anova(soy.fit)
Analysis of Variance Table
Response: amount
Df Sum Sq Mean Sq F value Pr(>F)
protein 3 55.53 18.511 0.4916 0.6953
Residuals 11 414.20 37.655
> n1<-length(cereal)
> n1
[1] 5
> n2<-length(energy)
> n2
[1] 5
> n3<-length(veggy)
> n3
[1] 5
> m<-mean((cereal+energy)+(cereal+veggy))
> m
[1] 43
> s<-sd((cereal+energy)+(cereal+veggy))
> s
[1] 15.11622
> m1<-mean(cereal)
> m1
[1] 9.6
> m2<-mean(energy)
> m2
[1] 10
> m3<-mean(veggy)
> m3
[1] 13.8
> overallm<-(((n1*m1)+(n2*m2)+(n3*m3))/(n1+n2+n3))
> overallm
[1] 11.13333
> SSB<-((n1*(m1-overallm)^2)+(n2*(m2-overallm)^2)+(n3*(m3-overallm)^2))
> SSB
[1] 53.73333
但是,當我使用anova()
功能檢查了我的答案,它說55.53。我做錯了什麼,或者是一個四捨五入的問題或什麼?
歡迎。作爲一種好的做法,你能給出一個最小的工作示例(例如'輸入'數據集或向量)嗎?你也可以發佈用於生成anova結果的代碼嗎? – dardisco
當然!剛添加它 – user3093083