Skip to content

Commit

Permalink
updates to lmer 1 and 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasgerstenberg committed Feb 23, 2024
1 parent 65dcd03 commit 92fd114
Show file tree
Hide file tree
Showing 54 changed files with 812 additions and 928 deletions.
4 changes: 2 additions & 2 deletions 17-linear_mixed_effects_models1.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ fit.augmented = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.original)
# compare models
# note: the lmer model has to be supplied first
# note: the lmer model has to be entered as the first argument
anova(fit.augmented, fit.compact)
```

Yes, the likelihood of the data given the linear mixed effects model is significantly higher compared to its likelihood given the linear model.
Yes, the linear mixed effects model explains the data better than the linear model.

## Additional resources

Expand Down
20 changes: 12 additions & 8 deletions 18-linear_mixed_effects_models2.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,10 @@ df.complete_pooling = fit.complete_pooling %>%
df.no_pooling = df.sleep %>%
group_by(subject) %>%
nest(data = c(days, reaction)) %>%
mutate(fit = map(data, ~ lm(reaction ~ days, data = .)),
augment = map(fit, augment)) %>%
mutate(fit = map(.x = data,
.f = ~ lm(reaction ~ days, data = .x)),
augment = map(.x = fit,
.f = ~ augment(.x))) %>%
unnest(c(augment)) %>%
ungroup() %>%
clean_names() %>%
Expand Down Expand Up @@ -400,8 +402,10 @@ df.partial_pooling = fit.random_intercept_slope %>%
df.plot = df.sleep %>%
group_by(subject) %>%
nest(data = c(days, reaction)) %>%
mutate(fit = map(data, ~ lm(reaction ~ days, data = .)),
tidy = map(fit, tidy)) %>%
mutate(fit = map(.x = data,
.f = ~ lm(reaction ~ days, data = .x)),
tidy = map(.x = fit,
.f = ~ tidy(.x))) %>%
unnest(c(tidy)) %>%
select(subject, term, estimate) %>%
pivot_wider(names_from = term,
Expand Down Expand Up @@ -517,7 +521,7 @@ Let's fit a model to this data now and take a look at the summary output:
```{r}
# fit model
fit.mixed = lmer(formula = value ~ 1 + condition + (1 | participant),
data = df.mixed)
data = df.mixed)
summary(fit.mixed)
```
Expand Down Expand Up @@ -687,7 +691,8 @@ n_observations = 10
slope = -10
sd_error = 0.4
sd_participant = 5
intercept = rnorm(n_participants, sd = sd_participant) %>% sort()
intercept = rnorm(n_participants, sd = sd_participant) %>%
sort()
df.simpson = tibble(x = runif(n_participants * n_observations, min = 0, max = 1)) %>%
arrange(x) %>%
Expand Down Expand Up @@ -753,7 +758,7 @@ Let's fit a linear mixed effects model with random intercepts:

```{r}
fit.lmer = lmer(formula = y ~ 1 + x + (1 | participant),
data = df.simpson)
data = df.simpson)
fit.lmer %>%
summary()
Expand All @@ -762,7 +767,6 @@ fit.lmer %>%
As we can see, the fixed effect for `x` is now negative!

```{r}
fit.lmer %>%
augment() %>%
clean_names() %>%
Expand Down
44 changes: 1 addition & 43 deletions docs/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<meta name="author" content="Tobias Gerstenberg" />


<meta name="date" content="2024-02-16" />
<meta name="date" content="2024-02-22" />

<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
Expand Down Expand Up @@ -51,12 +51,6 @@
<link href="libs/anchor-sections-1.1.0/anchor-sections.css" rel="stylesheet" />
<link href="libs/anchor-sections-1.1.0/anchor-sections-hash.css" rel="stylesheet" />
<script src="libs/anchor-sections-1.1.0/anchor-sections.js"></script>
<script src="libs/htmlwidgets-1.6.4/htmlwidgets.js"></script>
<script src="libs/viz-1.8.2/viz.js"></script>
<link href="libs/DiagrammeR-styles-0.2/styles.css" rel="stylesheet" />
<script src="libs/grViz-binding-1.0.10/grViz.js"></script>
<script src="libs/kePrint-0.0.1/kePrint.js"></script>
<link href="libs/lightable-0.0.1/lightable.css" rel="stylesheet" />


<style type="text/css">
Expand Down Expand Up @@ -128,28 +122,6 @@

div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
</style>
<style type="text/css">
/* Used with Pandoc 2.11+ new --citeproc when CSL is used */
div.csl-bib-body { }
div.csl-entry {
clear: both;
}
.hanging div.csl-entry {
margin-left:2em;
text-indent:-2em;
}
div.csl-left-margin {
min-width:2em;
float:left;
}
div.csl-right-inline {
margin-left:2em;
padding-left:1em;
}
div.csl-indent {
margin-left: 2em;
}
</style>

<link rel="stylesheet" href="style.css" type="text/css" />
</head>
Expand Down Expand Up @@ -929,20 +901,6 @@ <h1>Page not found</h1>
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
var src = "true";
if (src === "" || src === "true") src = "https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.9/latest.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:")
if (/^https?:/.test(src))
src = src.replace(/^https?:/, '');
script.src = src;
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>

</html>
Loading

0 comments on commit 92fd114

Please sign in to comment.