[英]R_lme4_mixed-effects_modelling_SPSSexample_replication_cross-platform
Hello stackoverflow community!你好 stackoverflow 社区! I am new to mixed-effects modelling (MEM) or mixed-models .
我是混合效果建模 (MEM) 或mixed-models的新手。 In order to gain a better understanding of MEM, I decided to replicate two examples in r ( lme4 package) from the textbook "Experimental Design and Analysis" by Dr. Howard J. Seltman .
为了更好地理解 MEM,我决定复制Howard J. Seltman 博士的教科书《实验设计与分析》中r ( lme4包)中的两个例子。 In the textbook, the author used SPSS to solve the two examples and included the relevant output tables.
教材中作者用SPSS解决了两个例子,并收录了相关的output表。
Model 1, referred to as [tag:video game example], models " the linear relationship between trial and score with separate intercepts and slopes for each age group, and including a random per-subject intercept. " Model 1,称为 [tag:video game example],模型“试验和分数之间的线性关系,每个年龄组都有单独的截距和斜率,并包括每个受试者的随机截距。 ”
The data for the video game example is available at the link below : https://www.stat.cmu.edu/~hseltman/309/Book/data/MMvideo.txt视频游戏示例的数据可在以下链接获得: https://www.stat.cmu.edu/~hseltman/309/Book/data/MMvideo.txt
The model 1 output tables are found on the page no. model 1 output 表格可在第 1页找到。 370/382 (actual book/pdf book) of the textbook which is also linked below (or see image): https://www.stat.cmu.edu/~hseltman/309/Book/Book.pdf
教科书的370/382 (实际书籍/pdf 书籍)也在下面链接(或看图片): https://www.stat.cmu.edu/~hseltman/309/Book/Book.pdf
My model 1 ( video game example ) is:我的 model 1(视频游戏示例)是:
lmer(score ~ trial + (1|id) + (1+agegrp|agegrp), data=data)
where, trial is a fixed-effect.其中,试验是固定效应。
(1|id) is a random per-subject intercept. (1|id) 是随机的每个主题截距。
(1+agegrp|agegrp) is a random slope and random intercept for each age group. (1+agegrp|agegrp) 是每个年龄组的随机斜率和随机截距。
The model 1 returns an error: boundary (singular) fit: see help('isSingular') model 1 返回错误: boundary (singular) fit: see help('isSingular')
Model 2, referred to as [tag:classroom example], includes " main effects for stdTest, grade level, and treatment group " and " random effect (intercept) to account for school to school differences that induces correlation among scores for students within a school. " Link for the classroom example data is included below: https://www.stat.cmu.edu/~hseltman/309/Book/data/schools.txt Model 2,称为 [tag:classroom example],包括“对 stdTest、年级和治疗组的主要影响”和“随机效应(截距)以解释学校与学校之间的差异,这些差异会导致学生分数之间的相关性学校。 ”课堂示例数据的链接如下: https://www.stat.cmu.edu/~hseltman/309/Book/data/schools.txt
The model 2 output tables are found on the page no. model 2 output 表可在第 10页找到。 377/391 (actual book/pdf book) of the textbook which is also linked below (or see image): [https://www.stat.cmu.edu/~hseltman/309/Book/Book.pdf]
教科书的377/391 (实际书籍/pdf 书籍)也在下面链接(或看图片):[https://www.stat.cmu.edu/~hseltman/309/Book/Book.pdf]
My model 2 ( classroom example ) is:我的model 2(课堂例子)是:
lmer(score ~ stdTest + grade + treatment + (1|student) + (1|student:classroom), data=data)
where, stdTest, grade level, and treatment group are the fixed-effect.其中,stdTest、年级水平和治疗组是固定效应。
(1|student) is a random effect (intercept). (1|student) 是随机效应(拦截)。
(1|student:classroom) for students nested within a school. (1|student:classroom) 适用于嵌套在学校内的学生。
The model 2 returns an error: number of levels of each grouping factor must be < number of observations (problems: student, classroom:student ) model 2 返回错误:每个分组因子的水平数必须<观察数(问题:student, classroom:student )
Could someone please help me model these two examples correctly to produce the desired outputs?有人可以帮我 model 这两个例子正确地产生所需的输出吗?
I think you had the models specified the wrong way.我认为您以错误的方式指定了模型。 This looks like the way to replicate those two models:
这看起来像是复制这两个模型的方法:
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
Created on 2022-12-04 by the reprex package (v2.0.1)由reprex package (v2.0.1) 创建于 2022-12-04
One thing to note is that the default behaviour in R is to have the first level of the factor be the reference.需要注意的一点是 R 中的默认行为是以因子的第一水平作为参考。 In the book examples, the last level was the reference, so you have to make that explicit in the data managing as above.
在书中的例子中,最后一层是参考,所以你必须在上面的数据管理中明确这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.