[英]R_lme4_mixed-effects_modelling_SPSSexample_replication_cross-platform
你好 stackoverflow 社区! 我是混合效果建模 (MEM) 或mixed-models的新手。 为了更好地理解 MEM,我决定复制Howard J. Seltman 博士的教科书《实验设计与分析》中r ( lme4包)中的两个例子。 教材中作者用SPSS解决了两个例子,并收录了相关的output表。
Model 1,称为 [tag:video game example],模型“试验和分数之间的线性关系,每个年龄组都有单独的截距和斜率,并包括每个受试者的随机截距。 ”
视频游戏示例的数据可在以下链接获得: https://www.stat.cmu.edu/~hseltman/309/Book/data/MMvideo.txt
model 1 output 表格可在第 1页找到。 教科书的370/382 (实际书籍/pdf 书籍)也在下面链接(或看图片): https://www.stat.cmu.edu/~hseltman/309/Book/Book.pdf
我的 model 1(视频游戏示例)是:
lmer(score ~ trial + (1|id) + (1+agegrp|agegrp), data=data)
其中,试验是固定效应。
(1|id) 是随机的每个主题截距。
(1+agegrp|agegrp) 是每个年龄组的随机斜率和随机截距。
model 1 返回错误: boundary (singular) fit: see help('isSingular')
Model 2,称为 [tag:classroom example],包括“对 stdTest、年级和治疗组的主要影响”和“随机效应(截距)以解释学校与学校之间的差异,这些差异会导致学生分数之间的相关性学校。 ”课堂示例数据的链接如下: https://www.stat.cmu.edu/~hseltman/309/Book/data/schools.txt
model 2 output 表可在第 10页找到。 教科书的377/391 (实际书籍/pdf 书籍)也在下面链接(或看图片):[https://www.stat.cmu.edu/~hseltman/309/Book/Book.pdf]
我的model 2(课堂例子)是:
lmer(score ~ stdTest + grade + treatment + (1|student) + (1|student:classroom), data=data)
其中,stdTest、年级水平和治疗组是固定效应。
(1|student) 是随机效应(拦截)。
(1|student:classroom) 适用于嵌套在学校内的学生。
model 2 返回错误:每个分组因子的水平数必须<观察数(问题:student, classroom:student )
有人可以帮我 model 这两个例子正确地产生所需的输出吗?
我认为您以错误的方式指定了模型。 这看起来像是复制这两个模型的方法:
library(lme4)
library(lmerTest)
library(dplyr)
dat <- rio::import("https://www.stat.cmu.edu/~hseltman/309/Book/data/MMvideo.txt")
dat <- dat %>%
mutate(agegrp = factor(agegrp, levels=c("(40,50]", "(20,30]", "(30,40]")))
m1 <- lmer(score ~ agegrp*trial + (1|id) , data=dat)
summary(m1)
#> Linear mixed model fit by REML. t-tests use Satterthwaite's method [
#> lmerModLmerTest]
#> Formula: score ~ agegrp * trial + (1 | id)
#> Data: dat
#>
#> REML criterion at convergence: 708.4
#>
#> Scaled residuals:
#> Min 1Q Median 3Q Max
#> -2.39575 -0.54403 0.07855 0.65601 2.02271
#>
#> Random effects:
#> Groups Name Variance Std.Dev.
#> id (Intercept) 6.457 2.541
#> Residual 4.633 2.152
#> Number of obs: 150, groups: id, 28
#>
#> Fixed effects:
#> Estimate Std. Error df t value Pr(>|t|)
#> (Intercept) 14.0223 1.1097 55.4281 12.637 < 2e-16 ***
#> agegrp(20,30] -7.2586 1.5704 72.9807 -4.622 1.60e-05 ***
#> agegrp(30,40] -3.4887 1.4510 64.2373 -2.404 0.0191 *
#> trial 3.3150 0.2152 118.8662 15.401 < 2e-16 ***
#> agegrp(20,30]:trial 3.7988 0.3229 118.8662 11.766 < 2e-16 ***
#> agegrp(30,40]:trial 2.1433 0.2914 118.8662 7.354 2.68e-11 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Correlation of Fixed Effects:
#> (Intr) ag(20,30] ag(30,40] trial a(20,30]:
#> aggr(20,30] -0.707
#> aggr(30,40] -0.765 0.617
#> trial -0.582 0.411 0.445
#> agg(20,30]: 0.388 -0.617 -0.297 -0.667
#> agg(30,40]: 0.430 -0.304 -0.603 -0.739 0.492
dat2 <- rio::import("https://www.stat.cmu.edu/~hseltman/309/Book/data/schools.txt")
dat2 <- dat2 %>%
mutate(grade =factor(grade, levels=c(5,3)),
treatment = factor(treatment, levels=c(1,0)))
m2 <- lmer(score ~ grade + treatment + stdTest + (1|classroom), data=dat2)
summary(m2)
#> Linear mixed model fit by REML. t-tests use Satterthwaite's method [
#> lmerModLmerTest]
#> Formula: score ~ grade + treatment + stdTest + (1 | classroom)
#> Data: dat2
#>
#> REML criterion at convergence: 3023.7
#>
#> Scaled residuals:
#> Min 1Q Median 3Q Max
#> -3.02847 -0.68306 0.03838 0.64510 2.94562
#>
#> Random effects:
#> Groups Name Variance Std.Dev.
#> classroom (Intercept) 10.05 3.170
#> Residual 25.87 5.086
#> Number of obs: 490, groups: classroom, 20
#>
#> Fixed effects:
#> Estimate Std. Error df t value Pr(>|t|)
#> (Intercept) -23.0943 6.8025 15.9160 -3.395 0.003722 **
#> grade3 -5.9424 1.6566 16.0861 -3.587 0.002447 **
#> treatment0 1.7941 1.6351 16.0676 1.097 0.288698
#> stdTest 0.4438 0.0879 15.8672 5.049 0.000122 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Correlation of Fixed Effects:
#> (Intr) grade3 trtmn0
#> grade3 -0.246
#> treatment0 0.007 -0.410
#> stdTest -0.985 0.179 -0.079
由reprex package (v2.0.1) 创建于 2022-12-04
需要注意的一点是 R 中的默认行为是以因子的第一水平作为参考。 在书中的例子中,最后一层是参考,所以你必须在上面的数据管理中明确这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.