14.5 Type I, Type II, and Type III ANOVAs
It turns out that there is not just one way to calculate ANOVAs. In fact, there are three different types - called, Type 1, 2, and 3 (or Type I, II and III). These types differ in how they calculate variability (specifically the sums of of squares
). If your data is relatively balanced
, meaning that there are relatively equal numbers of observations in each group, then all three types will give you the same answer. However, if your data are unbalanced
, meaning that some groups of data have many more observations than others, then you need to use Type II (2) or Type III (3).
事實證明,不只有一種方法可以計算方差撑刺。事實上,有三種不同的類型-稱為類型1芬膝、類型2和類型3(或類型I、II和III)形娇。這些類型在計算變異性的方式上有所不同(具體地說锰霜,就是“平方和”)。
均衡設計(balanced design)是指 每個實驗條件下的被試量相等桐早。否則就是非均衡設計(unbalanced design)癣缅。
如果你的數據相對比較平衡,也就是說每組的觀察次數相對相等哄酝,那么這三種類型的數據都會給你相同的答案友存。但是,如果您的數據是“不平衡的”炫七,即某些數據組的觀測值比其他組多得多爬立,那么您需要使用類型II(2)或類型III(3)。
-
類型I方法(序貫型) 計算ANOVA效應:舉例如下:
y~A+B+A:B
A對y的影響万哪;A不做調整
控制A時侠驯,B對y的影響;B根據A調整
控制A和B的主效應時奕巍,A與B的交互效應吟策。A:B交互項根據A和B調整。
R中的aov()
默認使用的是類型I的止。 -
類型II(分層型)
效應根據同水平或低水平的效應做調整檩坚。
A根據B調整,B根據A調整诅福,A:B交互項同時根據A和B調整匾委。 -
類型III(邊界型)
每個效應根據模型其他各效應做相應調整。
A根據B和A:B做調整氓润,A:B交互項根據A和B調整赂乐。
SPSS 和 SAS 默認使用的是類型III。如果要在R中得到相同的結果咖气,需要使用類型III挨措。aov()
函數默認使用的是類型I的方法,若要使用類型III崩溪,需要用car包中的Anova()函數浅役,具體可參考help(Anova,package="car")
。
樣本大小越不平衡伶唯,效應項的順序對結果的影響越大觉既。一般來說,越基礎性的效應越需要放在表達式前面。具體來講:
首先是協變量瞪讼,然后是主效應岭参,接著是雙因素的交互項,再接著是三因素的交互項尝艘,以此類推。
對于主效應姿染,越基礎性的效應越應放在表達式前面背亥。
The standard aov()
function in base-R uses Type I sums of squares. Therefore, it is only appropriate when your data are balanced. If your data are unbalanced, you should conduct an ANOVA with Type II or Type III sums of squares. To do this, you can use the Anova()
function in the car
package. The Anova()
function has an argument called type
that allows you to specify the type of ANOVA you want to calculate.
R中的標準aov()
函數使用第一類平方和。因此悬赏,只有當您的數據達到平衡時狡汉,它才是合適的。如果你的數據不平衡闽颇,你應該用II型平方和或III型平方和進行方差分析盾戴。要做到這一點,可以使用car
包中的Anova()
函數兵多。Anova()
函數有一個名為type
的參數尖啡,允許您指定要計算的方差分析的類型。
In the next code chunk, I’ll calculate 3 separate ANOVAs from the poopdeck data using the three different types. First, I’ll create a regression object with lm()
. As you’ll see, the Anova()
function requires you to enter a regression object as the main argument, and not
a formula and dataset. That is, you need to first create a regression object from the data with lm()
(or glm()
), and then enter that object into the Anova()
function. You can also do the same thing with the standard aov()
function`.
在下一個代碼塊中剩膘,我將使用三種不同的類型從poopdeck數據計算3個獨立的ANOVA衅斩。首先,我將使用lm()
創(chuàng)建一個回歸對象怠褐。正如您將看到的畏梆,Anova()
函數要求您輸入回歸對象作為主參數,而不是輸入公式和數據集奈懒。也就是說奠涌,您需要先使用lm()
函數(或glm()
)從數據創(chuàng)建一個回歸對象,然后將該對象輸入到Anova()
函數中磷杏。標準的aov()
函數也可以做同樣的事情溜畅。
# Step 1: Calculate regression object with lm()
time.lm <- lm(formula = time ~ type + cleaner,
data = poopdeck)
Now that I’ve created the regression object time.lm
, I can calculate the three different types of ANOVAs by entering the object as the main argument to either aov()
for a Type I ANOVA, or Anova()
in the car package for a Type II or Type III ANOVA:
現在我已經創(chuàng)建了回歸對象time.lm
,我可以通過將該對象作為主參數輸入到aov
(對于類型I ANOVA)或者car包(對于類型II或類型III ANOVA)中的Anova()
參數來計算三種不同類型的ANOVA:
# Type I ANOVA - aov()
time.I.aov <- aov(time.lm)
# Type II ANOVA - Anova(type = 2)
time.II.aov <- car::Anova(time.lm, type = 2)
# Type III ANOVA - Anova(type = 3)
time.III.aov <- car::Anova(time.lm, type = 3)
As it happens, the data in the poopdeck dataframe are perfectly balanced (so we’ll get exactly the same result for each ANOVA type. However, if they were not balanced, then we should not use the Type I ANOVA calculated with the aov() function.
碰巧的是茴丰,poopdeck數據幀中的數據是完全平衡的(因此达皿,對于每種ANOVA類型,我們都會得到完全相同的結果贿肩。但是峦椰,如果它們不平衡,那么我們就不應該使用使用aov()
函數計算的I型方差分析汰规。
要查看數據是否平衡汤功,可以使用以下函數:
To see if your data are balanced, you can use the function:
# Are observations in the poopdeck data balanced?
with(poopdeck,
table(cleaner, type))
## type
## cleaner parrot shark
## a 100 100
## b 100 100
## c 100 100
As you can see, in the poopdeck data, the observations are perfectly balanced, so it doesn’t matter which type of ANOVA we use to analyse the data.
正如你所看到的,在poopdeck數據中溜哮,觀測數據是完全平衡的滔金,所以我們使用哪種類型的方差分析來分析數據并不重要色解。
有關不同類型的更多詳細信息,請查看https://mcfromnz.wordpress.com/2011/03/02/anova-type-iiiiii-ss-explained/.
For more detail on the different types, check out https://mcfromnz.wordpress.com/2011/03/02/anova-type-iiiiii-ss-explained/.
參考資料:https://bookdown.org/ndphillips/YaRrr/type-i-type-ii-and-type-iii-anovas.html