Extracting model estimates and data structure
Humboldt-Universität zu Berlin
2024-07-09
Today we will…
df_lifetime <- readr::read_csv(here::here("data", "tidy_data_lifetime_pilot.csv"),
# for special characters
locale = readr::locale(encoding = "latin1")
) |>
mutate_if(is.character,as.factor) |> # all character variables as factor
filter(type == "critical", # only critical trials
px != "px3") # this participant had lots of 0's for some reason
lifetime
and tense
) to be factorsPP
and living
are -0,5
SF
and dead
are +0,5
fitting a straight line to data
for an introduction see webbook for my course Regression for Linguists, e.g., Ch. 1 Understanding straight lines
fixed-effects only multiple regression:
fit_
or mod_
for models more generallylm_
or glm_
to specify the function used to produce the modelsummary()
Call:
lm(formula = log(fp) ~ tense * lifetime, data = df_lifetime,
subset = region == "verb" & ff > 0)
Residuals:
Min 1Q Median 3Q Max
-1.18141 -0.34282 0.00058 0.26923 1.38822
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 5.63477 0.01877 300.193 < 2e-16 ***
tense1 0.03357 0.03754 0.894 0.37158
lifetime1 -0.10130 0.03754 -2.698 0.00718 **
tense1:lifetime1 -0.08320 0.07508 -1.108 0.26828
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 0.4374 on 539 degrees of freedom
Multiple R-squared: 0.01712, Adjusted R-squared: 0.01165
F-statistic: 3.129 on 3 and 539 DF, p-value: 0.02538
lme4
objects contains lots of information
nobs(model)
will tell you how many data points were included in the modelformula(model)
will give you the model formula.qmd
script), output (results printed in e.g., HTML our PDF output), and the actual model all stored and retrievable (.rds
files).rds
file extension)
saveRDS(object, filename)
writes an R object as RDS filereadRDS(filename)
reads it ineval: false
echo: false
broom
broom
package also contains useful functions for reporting lme4
models
broom::tidy()
produces the model estimates for our fixed effects# A tibble: 4 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) 5.63 0.0188 300. 0
2 tense1 0.0336 0.0375 0.894 0.372
3 lifetime1 -0.101 0.0375 -2.70 0.00718
4 tense1:lifetime1 -0.0832 0.0751 -1.11 0.268
broom::tidy()
, knitr::kable()
, and kableExtra::kable_styling()
term | estimate | std.error | statistic | p.value |
---|---|---|---|---|
(Intercept) | 5.63 | 0.02 | 300.19 | < .001 |
tense1 | 0.03 | 0.04 | 0.89 | 0.372 |
lifetime1 | -0.10 | 0.04 | -2.70 | 0.007 |
tense1:lifetime1 | -0.08 | 0.08 | -1.11 | 0.268 |
knitr
and kableExtra
functions to produce a publication-ready tablelmer()
lm()
models[1] 543
log(fp) ~ tense * lifetime + (1 | px) + (1 | item_id)
px
7
item_id
80
summary()
Linear mixed model fit by REML ['lmerMod']
Formula: log(fp) ~ tense * lifetime + (1 | px) + (1 | item_id)
Data: df_lifetime
Subset: region == "verb" & fp > 0
REML criterion at convergence: 582.5
Scaled residuals:
Min 1Q Median 3Q Max
-2.99792 -0.67159 0.02895 0.67995 2.67790
Random effects:
Groups Name Variance Std.Dev.
item_id (Intercept) 0.007012 0.08374
px (Intercept) 0.034124 0.18473
Residual 0.154885 0.39355
Number of obs: 543, groups: item_id, 80; px, 7
Fixed effects:
Estimate Std. Error t value
(Intercept) 5.63161 0.07245 77.736
tense1 0.03794 0.03389 1.120
lifetime1 -0.10308 0.03387 -3.044
tense1:lifetime1 -0.09587 0.06778 -1.414
Correlation of Fixed Effects:
(Intr) tense1 liftm1
tense1 -0.001
lifetime1 0.000 0.013
tens1:lftm1 0.003 0.002 -0.002
broom.mixed
broom.mixed
package also contains useful functions for reporting lme4
mixed models
broom.mixed::tidy()
produces the model estimates for fixed and random effectseffects = "fixed"
effects = "ran_pars"
tidy()
# A tibble: 7 × 6
effect group term estimate std.error statistic
<chr> <chr> <chr> <dbl> <dbl> <dbl>
1 fixed <NA> (Intercept) 5.63 0.0724 77.7
2 fixed <NA> tense1 0.0379 0.0339 1.12
3 fixed <NA> lifetime1 -0.103 0.0339 -3.04
4 fixed <NA> tense1:lifetime1 -0.0959 0.0678 -1.41
5 ran_pars item_id sd__(Intercept) 0.0837 NA NA
6 ran_pars px sd__(Intercept) 0.185 NA NA
7 ran_pars Residual sd__Observation 0.394 NA NA
lmerTest
lme4
doesn’t do it for linear modelslmerTest
package, which also has lme4
# A tibble: 7 × 8
effect group term estimate std.error statistic df p.value
<chr> <chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
1 fixed <NA> (Intercept) 5.63 0.0724 77.7 6.20 1.65e-10
2 fixed <NA> tense1 0.0379 0.0339 1.12 471. 2.63e- 1
3 fixed <NA> lifetime1 -0.103 0.0339 -3.04 468. 2.47e- 3
4 fixed <NA> tense1:lifeti… -0.0959 0.0678 -1.41 471. 1.58e- 1
5 ran_pars item_id sd__(Intercep… 0.0837 NA NA NA NA
6 ran_pars px sd__(Intercep… 0.185 NA NA NA NA
7 ran_pars Residual sd__Observati… 0.394 NA NA NA NA
knitr
and kableExtra
for:
broom.mixed::tidy()
, knitr::kable()
, and kableExtra::kable_styling()
effect | group | term | estimate | std.error | statistic | df | p.value |
---|---|---|---|---|---|---|---|
fixed | NA | (Intercept) | 5.63 | 0.07 | 77.74 | 6.20 | < .001 |
fixed | NA | tense1 | 0.04 | 0.03 | 1.12 | 470.73 | 0.263 |
fixed | NA | lifetime1 | -0.10 | 0.03 | -3.04 | 468.30 | 0.002 |
fixed | NA | tense1:lifetime1 | -0.10 | 0.07 | -1.41 | 470.76 | 0.158 |
ran_pars | item_id | sd__(Intercept) | 0.08 | NA | NA | NA | NA |
ran_pars | px | sd__(Intercept) | 0.18 | NA | NA | NA | NA |
ran_pars | Residual | sd__Observation | 0.39 | NA | NA | NA | NA |
SessionInfo()
also takes care of this)To cite lme4 in publications use:
Douglas Bates, Martin Maechler, Ben Bolker, Steve Walker (2015).
Fitting Linear Mixed-Effects Models Using lme4. Journal of
Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01.
A BibTeX entry for LaTeX users is
@Article{,
title = {Fitting Linear Mixed-Effects Models Using {lme4}},
author = {Douglas Bates and Martin M{\"a}chler and Ben Bolker and Steve Walker},
journal = {Journal of Statistical Software},
year = {2015},
volume = {67},
number = {1},
pages = {1--48},
doi = {10.18637/jss.v067.i01},
}
To cite lme4 in publications use:
Douglas Bates, Martin Maechler, Ben Bolker, Steve Walker (2015).
Fitting Linear Mixed-Effects Models Using lme4. Journal of
Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01.
A BibTeX entry for LaTeX users is
@Article{,
title = {Fitting Linear Mixed-Effects Models Using {lme4}},
author = {Douglas Bates and Martin M{\"a}chler and Ben Bolker and Steve Walker},
journal = {Journal of Statistical Software},
year = {2015},
volume = {67},
number = {1},
pages = {1--48},
doi = {10.18637/jss.v067.i01},
}
.bib
file, add a citation reference key (e.g., bates_fitting_2015
), and cite it in-text with @bates_fitting_2015
, which will be formatted as Bates, Mächler, et al. (2015)
rbbt
Linear mixed models were fit using the
lmerTest
package which produces the Satterwaite approximation for degrees of freedom to calculate p-values (Kuznetsova et al., 2017).
{r, echo = FALSE}
, which is slightly different than the Quarto #| eval: false
chunk option syntaxA linear mixed model was fit to log-transformed first-pass reading times, with `r summary(lmerTest_lifetime)$ngrps["px"]` participants and `r summary(lmerTest_lifetime)$ngrps["item_id"] items`. The final model formula was `log(fp) ~ tense * lifetime + (1 | px) + (1 | item_id)`.
An effect of *lifetime* was found in first-pass reading times at the verb region (Est = `r signif(tidy_lmerTest_lifetime$estimate[tidy_lmer_lifetime$term=="lifetime1"],2)`, *t* = `r signif(tidy_lmerTest_lifetime$statistic[tidy_lmer_lifetime$term=="lifetime1"],2)`, *p* = `r signif(tidy_lmerTest_lifetime$p.value[tidy_lmer_lifetime$term=="lifetime1"],2)`). A significant effect of *tense* was not found found (Est = `r signif(tidy_lmerTest_lifetime$estimate[tidy_lmer_lifetime$term=="tense1"],2)`, *t* = `r signif(tidy_lmerTest_lifetime$statistic[tidy_lmer_lifetime$term=="tense1"],2)`, *p* = `r signif(tidy_lmerTest_lifetime$p.value[tidy_lmer_lifetime$term=="tense1"],2)`), nor an interaction of *lifetime* and *tense* (Est = `r signif(tidy_lmerTest_lifetime$estimate[tidy_lmer_lifetime$term=="tense1:lifetime1"],2)`, *t* = `r signif(tidy_lmerTest_lifetime$statistic[tidy_lmer_lifetime$term=="tense1:lifetime1"],2)`, *p* = `r signif(tidy_lmerTest_lifetime$p.value[tidy_lmer_lifetime$term=="tense1:lifetime1"],2)`).
A linear mixed model was fit to log-transformed first-pass reading times, with 7 participants and 80 items. The final model formula was
log(fp) ~ tense * lifetime + (1 | px) + (1 | item_id)
.An effect of lifetime was found in first-pass reading times at the verb region (Est = -0.1, t = -3, p = 0.0025). A significant effect of tense was not found found (Est = 0.038, t = 1.1, p = 0.26), nor an interaction of lifetime and tense (Est = -0.096, t = -1.4, p = 0.16).
Today we…
R version 4.4.0 (2024-04-24)
Platform: aarch64-apple-darwin20
Running under: macOS Ventura 13.2.1
Matrix products: default
BLAS: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRblas.0.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/lib/libRlapack.dylib; LAPACK version 3.12.0
attached base packages:
[1] stats graphics grDevices datasets utils methods base
other attached packages:
[1] lmerTest_3.1-3 brms_2.21.5 Rcpp_1.0.12
[4] broom.mixed_0.2.9.5 broom_1.0.6 lme4_1.1-35.3
[7] Matrix_1.7-0 lubridate_1.9.3 forcats_1.0.0
[10] stringr_1.5.1 dplyr_1.1.4 purrr_1.0.2
[13] readr_2.1.5 tidyr_1.3.1 tibble_3.2.1
[16] ggplot2_3.5.1 tidyverse_2.0.0
loaded via a namespace (and not attached):
[1] tidyselect_1.2.1 viridisLite_0.4.2 loo_2.7.0
[4] fastmap_1.2.0 tensorA_0.36.2.1 pacman_0.5.1
[7] digest_0.6.35 timechange_0.3.0 estimability_1.5.1
[10] lifecycle_1.0.4 magrittr_2.0.3 posterior_1.5.0
[13] compiler_4.4.0 rlang_1.1.4 tools_4.4.0
[16] utf8_1.2.4 yaml_2.3.8 knitr_1.47
[19] bridgesampling_1.1-2 bit_4.0.5 here_1.0.1
[22] xml2_1.3.6 abind_1.4-5 numDeriv_2016.8-1.1
[25] withr_3.0.0 grid_4.4.0 fansi_1.0.6
[28] xtable_1.8-4 colorspace_2.1-0 future_1.33.2
[31] globals_0.16.3 emmeans_1.10.2 scales_1.3.0
[34] MASS_7.3-60.2 cli_3.6.2 mvtnorm_1.2-5
[37] rmarkdown_2.27 crayon_1.5.2 generics_0.1.3
[40] RcppParallel_5.1.7 rstudioapi_0.16.0 tzdb_0.4.0
[43] minqa_1.2.7 splines_4.4.0 bayesplot_1.11.1
[46] parallel_4.4.0 matrixStats_1.3.0 vctrs_0.6.5
[49] boot_1.3-30 jsonlite_1.8.8 hms_1.1.3
[52] bit64_4.0.5 listenv_0.9.1 systemfonts_1.1.0
[55] glue_1.7.0 parallelly_1.37.1 nloptr_2.0.3
[58] codetools_0.2-20 distributional_0.4.0 stringi_1.8.4
[61] gtable_0.3.5 munsell_0.5.1 furrr_0.3.1
[64] pillar_1.9.0 htmltools_0.5.8.1 Brobdingnag_1.2-9
[67] R6_2.5.1 rprojroot_2.0.4 kableExtra_1.4.0
[70] vroom_1.6.5 evaluate_0.23 lattice_0.22-6
[73] highr_0.11 backports_1.5.0 renv_1.0.7
[76] rstantools_2.4.0 svglite_2.1.3 coda_0.19-4.1
[79] nlme_3.1-165 checkmate_2.3.1 xfun_0.44
[82] pkgconfig_2.0.3