From 6c70fd08663fcb67782773deb033928a37075ebd Mon Sep 17 00:00:00 2001 From: Tobias Gerstenberg Date: Mon, 26 Feb 2024 09:40:53 -0800 Subject: [PATCH] updates lmer --- 19-linear_mixed_effects_models3.Rmd | 64 +--- 20-linear_mixed_effects_models4.Rmd | 10 +- docs/404.html | 31 +- docs/linear-mixed-effects-models-3.html | 404 ++++++++++-------------- docs/reference-keys.txt | 2 + docs/search_index.json | 2 +- 6 files changed, 206 insertions(+), 307 deletions(-) diff --git a/19-linear_mixed_effects_models3.Rmd b/19-linear_mixed_effects_models3.Rmd index 6d8084d..32c8b1c 100644 --- a/19-linear_mixed_effects_models3.Rmd +++ b/19-linear_mixed_effects_models3.Rmd @@ -4,7 +4,7 @@ - Pitfalls in fitting `lmers()`s (and what to do about it). - Understanding `lmer()` syntax even better. -- ANOVA vs. Lmer +- ANOVA vs. lmer ## Load packages and set plotting theme @@ -42,53 +42,12 @@ options(dplyr.summarise.inform = F) ## Load data sets -### Sleep data - -```{r} -# load sleepstudy data set -df.sleep = sleepstudy %>% - as_tibble() %>% - clean_names() %>% - mutate(subject = as.character(subject)) %>% - select(subject, days, reaction) - -# add two fake participants (with missing data) -df.sleep = df.sleep %>% - bind_rows(tibble(subject = "374", - days = 0:1, - reaction = c(286, 288)), - tibble(subject = "373", - days = 0, - reaction = 245)) -``` - ### Reasoning data ```{r} df.reasoning = sk2011.1 ``` -### Weight loss data - -```{r} -data("weightloss", package = "datarium") - -# Modify it to have three-way mixed design -df.weightloss = weightloss %>% - mutate(id = rep(1:24, 2)) %>% - pivot_longer(cols = t1:t3, - names_to = "timepoint", - values_to = "score") %>% - arrange(id) -``` - -### Politness data - -```{r} -df.politeness = read_csv("data/politeness_data.csv") %>% - mutate(scenario = as.factor(scenario)) -``` - ## Understanding the lmer() syntax Here is an overview of how to specify different kinds of linear mixed effects models. @@ -99,7 +58,7 @@ tibble(formula = c("`dv ~ x1 + (1 | g)`", "`dv ~ x1 + (x1 | g)`", "`dv ~ x1 + (x1 || g)`", "`dv ~ x1 + (1 | school) + (1 | teacher)`", - "`dv ~ x1 + (1 | school/teacher)`"), + "`dv ~ x1 + (1 | school) + (1 | school:teacher)`"), description = c("Random intercept for each level of `g`", "Random slope for each level of `g`", "Correlated random slope and intercept for each level of `g`", @@ -111,7 +70,7 @@ tibble(formula = c("`dv ~ x1 + (1 | g)`", Note that this `(1 | school/teacher)` is equivalent to `(1 | school) + (1 | teacher:school)` (see [here](https://stats.stackexchange.com/questions/228800/crossed-vs-nested-random-effects-how-do-they-differ-and-how-are-they-specified)). -## ANOVA vs. Lmer +## ANOVA vs. lmer ### Between subjects ANOVA @@ -128,7 +87,7 @@ aov_ez(id = "id", Looks like there was no main effect of `instruction` on participants' responses. -An alternative route for getting at the same test, would be via combining `lm()` with `Anova()` (as we've done before in class). +An alternative route for getting at the same test, would be via combining `lm()` with `joint_tests()` (as we've done before in class). ```{r} lm(formula = response ~ instruction, @@ -153,10 +112,11 @@ aov_ez(id = "id", filter(instruction == "probabilistic")) ``` -For the linear model route, given that we have repeated observations from the same participants, we need to use `lmer()`. The repeated measures anova has the random effect structure as shown below: +For the linear model route, given that we have repeated observations from the same participants, we need to use `lmer()`. The repeated measures ANOVA has the random effect structure as shown below: ```{r} -lmer(formula = response ~ validity * plausibility + (1 | id) + (1 | validity:id) + (1 | plausibility:id), +lmer(formula = response ~ 1 + validity * plausibility + (1 | id) + + (1 | id:validity) + (1 | id:plausibility), data = df.reasoning %>% filter(instruction == "probabilistic") %>% group_by(id, validity, plausibility) %>% @@ -183,15 +143,21 @@ aov_ez(id = "id", with the `lmer()` route: ```{r} -lmer(formula = response ~ instruction * validity * plausibility + (1 | id) + (1 | validity:id) + (1 | plausibility:id), +lmer(formula = response ~ instruction * validity * plausibility + (1 | id) + + (1 | id:validity) + (1 | id:plausibility), data = df.reasoning %>% group_by(id, validity, plausibility, instruction) %>% summarize(response = mean(response))) %>% joint_tests() ``` - Here, both routes yield the same results. +## Additional resources + +### Readings + +- [Nested and crossed random effects in lme4](https://www.muscardinus.be/statistics/nested.html) + ## Session info Information about this R session including which version of R was used, and what packages were loaded. diff --git a/20-linear_mixed_effects_models4.Rmd b/20-linear_mixed_effects_models4.Rmd index 5f64937..89a17ae 100644 --- a/20-linear_mixed_effects_models4.Rmd +++ b/20-linear_mixed_effects_models4.Rmd @@ -228,11 +228,14 @@ ggplot(data = df.politeness, We fit the model and compute the contrasts. ```{r, message=FALSE} -fit = lmer(formula = frequency ~ 1 + attitude * gender + (1 | subject) + (1 | scenario), +fit = lmer(formula = frequency ~ 1 + attitude * gender + (1 + attitude | subject) + (1 | scenario), data = df.politeness) fit %>% - emmeans(specs = pairwise ~ attitude + gender, + joint_tests() + +fit %>% + emmeans(specs = pairwise ~ attitude + gender, adjust = "none") ``` @@ -492,7 +495,8 @@ df.boot = df.sleep %>% bootstrap(n = 100, id = "id") %>% mutate(fit = map(.x = strap, - .f = ~ lm(formula = reaction ~ 1 + days, data = .)), + .f = ~ lm(formula = reaction ~ 1 + days, + data = .x)), tidy = map(.x = fit, .f = tidy)) %>% unnest(tidy) %>% diff --git a/docs/404.html b/docs/404.html index b965585..ec07cb4 100644 --- a/docs/404.html +++ b/docs/404.html @@ -23,7 +23,7 @@ - + @@ -621,19 +621,20 @@
  • 19.2 Load packages and set plotting theme
  • 19.3 Load data sets
  • 19.4 Understanding the lmer() syntax
  • -
  • 19.5 ANOVA vs. Lmer +
  • 19.5 ANOVA vs. lmer
  • -
  • 19.6 Session info
  • +
  • 19.6 Additional resources +
  • +
  • 19.7 Session info
  • 20 Linear mixed effects models 4
  • -
  • 23.8 Additional resources
  • +
  • 23.8 Additional resources
  • 23.9 Session info
  • 24 Bayesian data analysis 3 @@ -771,7 +772,7 @@
  • 24.6.4 Gaussian regression (unequal variance)
  • 24.6.5 Model comparison
  • -
  • 24.7 Additional resources
  • +
  • 24.7 Additional resources
  • 24.8 Session info
  • 25 Model assumptions @@ -786,7 +787,7 @@
  • 25.3.4 Non-parametric tests
  • 25.3.5 Bootstrapping regressions
  • -
  • 25.4 Additional resources
  • +
  • 25.4 Additional resources
  • 25.5 Session info
  • 26 Reporting statistics @@ -803,7 +804,7 @@
  • -
  • 26.3 Additional resources +
  • 26.3 Additional resources
  • diff --git a/docs/linear-mixed-effects-models-3.html b/docs/linear-mixed-effects-models-3.html index 1cc8a5e..2b27f11 100644 --- a/docs/linear-mixed-effects-models-3.html +++ b/docs/linear-mixed-effects-models-3.html @@ -23,7 +23,7 @@ - + @@ -51,24 +51,11 @@ - - - - - - - - - - - - - - @@ -328,36 +292,36 @@
  • 5.8 Session info
  • -
  • 6 Probability and causality +
  • 6 Probability
  • 7 Simulation 1
  • -
  • 7.4 Bayesian inference with the normal distribution +
  • 7.4 Pinguin exercise +
  • +
  • 7.5 Bayesian inference with the normal distribution
  • -
  • 7.5 Additional resources +
  • 7.6 Additional resources
  • -
  • 7.6 Session info
  • +
  • 7.7 Session info
  • 8 Simulation 2
  • 8.5 Additional resources
  • 8.6 Session info
  • @@ -425,7 +393,7 @@
  • 9.3 Hypothesis testing: “One-sample t-test”
  • 9.4 Building a sampling distribution of PRE
  • -
  • 9.5 Misc
  • +
  • 9.5 Misc
  • 9.6 Additional resources
  • @@ -478,7 +446,7 @@
  • 11.8 Additional resources
  • 11.9 Session info
  • 11.10 References
  • @@ -509,7 +477,7 @@
  • 12.7 Additional resources
  • 12.8 Session info
  • @@ -529,7 +497,7 @@
  • 13.6 Additional resources
  • 13.7 Session info
  • @@ -552,7 +520,7 @@
  • 14.8 Session info
  • @@ -607,7 +575,7 @@
  • 16.9 Session info
  • @@ -653,19 +621,20 @@
  • 19.2 Load packages and set plotting theme
  • 19.3 Load data sets
  • 19.4 Understanding the lmer() syntax
  • -
  • 19.5 ANOVA vs. Lmer +
  • 19.5 ANOVA vs. lmer
  • -
  • 19.6 Session info
  • +
  • 19.6 Additional resources +
  • +
  • 19.7 Session info
  • 20 Linear mixed effects models 4
  • -
  • 23.8 Additional resources
  • +
  • 23.8 Additional resources
  • 23.9 Session info
  • 24 Bayesian data analysis 3 @@ -803,7 +772,7 @@
  • 24.6.4 Gaussian regression (unequal variance)
  • 24.6.5 Model comparison
  • -
  • 24.7 Additional resources
  • +
  • 24.7 Additional resources
  • 24.8 Session info
  • 25 Model assumptions @@ -818,7 +787,7 @@
  • 25.3.4 Non-parametric tests
  • 25.3.5 Bootstrapping regressions
  • -
  • 25.4 Additional resources
  • +
  • 25.4 Additional resources
  • 25.5 Session info
  • 26 Reporting statistics @@ -835,7 +804,11 @@
  • -
  • 26.3 Session info
  • +
  • 26.3 Additional resources +
  • +
  • 26.4 Session info
  • 27 Cheatsheets