forked from uvastatlab/statistical_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnon_linear_mlm_with_comparisons.Rmd
More file actions
64 lines (45 loc) · 1.41 KB
/
non_linear_mlm_with_comparisons.Rmd
File metadata and controls
64 lines (45 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
---
title: "post-hoc comparison with non-linear models"
author: "Clay Ford"
date: "2024-07-15"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Get some data
Heights of Boys in Oxford. Data comes from the {nlme} package. This data frame contains the following columns:
- Subject: an ordered factor giving a unique identifier for each boy in the experiment
- age: a numeric vector giving the standardized age (dimensionless)
- height: a numeric vector giving the height of the boy (cm)
```{r}
data(Oxboys, package = "nlme")
```
## quick plot
Height obviously appears to be associated with age.
```{r}
library(ggplot2)
ggplot(Oxboys) +
aes(age, height) +
geom_line(aes(group = Subject)) +
geom_smooth(se = FALSE)
```
## fit a non-linear model
Model height as a function of age. A non-linear model is probably not necessary here, but we'll do it anyway.
```{r message=FALSE}
library(lme4)
m <- lmer(height ~ poly(age, 2) + (1 | Subject), data = Oxboys)
summary(m, corr = F)
```
## create a simple effect plot
```{r message=FALSE}
library(ggeffects)
plot(ggpredict(m, terms = "age [all]"))
```
## and now do post-hoc comparison
Compare height at standardized age of 0.5 to -0.5. Notice we need to specify the age values to compare.
```{r message=FALSE}
library(emmeans)
emm <- emmeans(m, specs = "age", at = list(age = c(0.5, -0.5)))
contrast(emm, method = "pairwise")
```