-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcode_along_answers.txt
125 lines (80 loc) · 2.8 KB
/
code_along_answers.txt
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Mixed-Effect/Multilevel Modeling with R
Code along answers
## CODE ALONG 1
1. Visualize the data using ggplot2. Group by id.
```{r}
ggplot(d3) +
aes(x = x, y = y, color = id) +
geom_point() +
geom_line()
```
2. Model y as a function of x with a random intercept conditional on id. Call the model `me3`.
```{r}
me3 <- lmer(y ~ x + (1|id), data = d3)
```
3. Check the constant variance assumption for the observations. What do you think?
```{r}
plot(me3)
```
4. Now fit a model that assumes the subject exerts a random effect on both the intercept and slope, using the formula `y ~ x + (x|id)`. Call the model `me4`.
```{r}
me4 <- lmer(y ~ x + (x|id), data = d3)
```
5. Check the assumption of constant variance for the observation random effect for `me4`. What do you think?
```{r}
plot(me4)
```
6. View the coefficients of `me4`. Notice each subject has their own intercept and slope.
```{r}
coef(me4)
```
## CODE ALONG 2
1. Let's fit a model with an interaction between treat and weeks, and let's leave the intercept conditional on subject. Call the model `lmm2`
```{r}
lmm2 <- lmer(wt ~ treat * weeks + (1 | subject), data=ratdrink)
summary(lmm2, corr = FALSE)
```
2. Is this model "good"? Check the Residual plot.
```{r}
plot(lmm2)
```
3. Create an effect plot using ggeffects.
```{r}
ggpredict(lmm2, terms = c("weeks", "treat")) |>
plot(ci = FALSE, add.data = TRUE)
```
4. what's the expected mean weight of a rat at week 4 for each treatment?
```{r}
g <- unique(ratdrink$treat)
predict(lmm2, newdata = data.frame(weeks = 4, treat = g),
re.form = NA)
```
## CODE ALONG 3
1. Update model lmm4 with a non-linear effect on weeks using `poly(weeks, 2)`. This is a second degree polynomial. Call the model `lmm5`.
```{r}
lmm5 <- lmer(wt ~ poly(weeks, 2) * treat + (1 | subject) +
(0 + weeks | subject), data = ratdrink)
```
2. How does this model compare to `lmm4`, which has a simpl linear effect on weeks?
```{r}
anova(lmm5, lmm4)
```
## CODE ALONG 4
What's the difference in expected weight at week 4 versus week 3 for rats on thiouracil? Use `bootMer()` to calculate a confidence interval on the difference using the `lmm3` model.
Here's how we get one estimate using `predict()`
```{r}
nd <- data.frame(treat = "thiouracil", weeks = 3:4)
# diff(x1, x2) = x2 - x1
diff(predict(lmm3, newdata = nd, re.form = NA))
```
Now use `bootMer()` to perform this 200 times using newly fitted models from simulated responses. Save the result to `b.out2`. When done, use the quantile function to get an approximate confidence interval.
```{r}
b.out2 <- bootMer(lmm5,
FUN = function(x){
diff(predict(x, newdata = nd, re.form = NA))
},
nsim = 200, .progress = "txt")
```
```{r}
quantile(b.out2$t, probs = c(0.025, 0.975))
```