使用 ANOVA 進行模型比較
比較模型並不容易。 有許多方法可用,但超出本課範圍,例如模型選擇(如 AIC)。
變異數分析(ANOVA)是比較 lmer 模型的一個基本選項。
ANOVA 用來檢驗一個模型是否比另一個模型解釋了更多的變異。
它是透過檢視模型所解釋的變異量來達成。
例如,你可以檢查在馬里蘭州,Year 是否能預測 Crime。
做法是先建立只包含 County 作為隨機效應的虛無模型(null model),以及一個包含 Year 的年度模型。
接著可以使用 anova() 函式比較這兩個模型。
如果 Year 解釋了顯著的變異量,則 P 值會小於你事先設定的門檻(通常是 0.05)。
本練習屬於課程
R 的階層式與混合效果模型
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build the Null model with only County as a random-effect
null_model <- lmer(Crime ~ (1 | ___) , data = md_crime)
# Build the Year2 model with Year2 as a fixed and random slope and County as the random-effect
year_model <- lmer(Crime ~ ___ + (1 + ___ | ___) , data = md_crime)
# Compare null_model and year_model using an anova
anova(___, ___)