開始使用免費開始

探索多層級:班級與學校

在上一個練習中,你使用的簡單線性模型沒有考慮到資料的結構。學生在班級中學習,而班級隸屬於學校,這代表同一個班級裡的學生並非彼此獨立。一種解法是將資料彙總,對每個層級取平均。不過,用來彙總資料的方法可能很關鍵,特別是在群組很小或大小不一時。

在這個練習中,你會用三種不同方式彙總數學成績成長(mathgain)。完成摘要後,你會在各層級上檢視線性模型。

本練習屬於課程

R 的階層式與混合效果模型

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Summarize the student data at the classroom level
class_data <-
    student_data %>%
    group_by(classid, schoolid) %>%
    summarize(mathgain_class = mean(mathgain),
              mathknow_class = mean(mathknow),
              n_class = n(), .groups = "keep")

# Model the math gain with the student-level data
lm(___ ~ ___, data = ___)

# Model the math gain with the classroom-level data
lm(___ ~ ___, data = ___)
編輯並執行程式碼