library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lme4)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## 
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
repo <- "https://raw.githubusercontent.com/langcog/experimentology/main"
sgf <- read_csv(file.path(repo, "data/tidyverse/stiller_scales_data.csv")) |>
  mutate(age_group = cut(age, 2:5, include.lowest = TRUE), 
         condition = condition |>
           fct_recode("Control" = "No Label", "Experimental" = "Label"))
## Rows: 588 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (3): subid, item, condition
## dbl (2): correct, age
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
sgf$age_centered <- scale(sgf$age, center = TRUE, scale = FALSE)
sgf$condition <- fct_relevel(sgf$condition, "Control")

mod <- glmer(correct ~ age_centered * condition + (1|subid) + (1|item), 
             family = "binomial", data = sgf)

summary(mod)
## Generalized linear mixed model fit by maximum likelihood (Laplace
##   Approximation) [glmerMod]
##  Family: binomial  ( logit )
## Formula: correct ~ age_centered * condition + (1 | subid) + (1 | item)
##    Data: sgf
## 
##      AIC      BIC   logLik deviance df.resid 
##    657.3    683.6   -322.7    645.3      582 
## 
## Scaled residuals: 
##     Min      1Q  Median      3Q     Max 
## -2.4195 -0.5688 -0.3756  0.6515  2.5418 
## 
## Random effects:
##  Groups Name        Variance Std.Dev.
##  subid  (Intercept) 0.05200  0.2280  
##  item   (Intercept) 0.07483  0.2735  
## Number of obs: 588, groups:  subid, 147; item, 4
## 
## Fixed effects:
##                                    Estimate Std. Error z value Pr(>|z|)    
## (Intercept)                         -1.4582     0.2156  -6.762 1.36e-11 ***
## age_centered                        -0.3767     0.1891  -1.992 0.046417 *  
## conditionExperimental                2.2613     0.2246  10.069  < 2e-16 ***
## age_centered:conditionExperimental   0.9237     0.2566   3.600 0.000318 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Correlation of Fixed Effects:
##             (Intr) ag_cnt cndtnE
## age_centerd  0.180              
## cndtnExprmn -0.617 -0.185       
## ag_cntrd:cE -0.155 -0.743  0.208