-
Notifications
You must be signed in to change notification settings - Fork 1
/
Answer-mixedmodels2.Rmd
154 lines (124 loc) · 4.61 KB
/
Answer-mixedmodels2.Rmd
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
title: "Answers to exercises \"Dependent data: Mixed effect models\" part 2"
output:
github_document:
toc: true
toc_depth: 2
---
# Exercise 1
## 1a
```{r}
library(lme4)
mm2.1 <- nlme::Milk[nlme::Milk$Time<8,]
fit.m1 <- lmer(protein~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(1+Time|Cow),data = mm2.1)
fit.m2 <- lmer(protein~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(1|Cow),data = mm2.1)
fit.m3 <- lmer(protein~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(0+Time|Cow),data = mm2.1)
AIC(fit.m1,fit.m2,fit.m3)
```
## 1b
```{r}
fit.m2 <- lmer(protein~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(1|Cow),data = mm2.1)
fit.m3 <- lmer(protein~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(0+Time|Cow),data = mm2.1)
AIC(fit.m1,fit.m2,fit.m3)
```
So the model with the random intercepts and the random slopes fits the data best.
## 1c
```{r}
fit.m4 <- lmer(protein~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(1+Time|Cow),REML=FALSE, data = mm2.1)
drop1(fit.m1)
```
Interaction is not needed in the model.
```{r}
fit.m5 <- lmer(protein~factor(Time)+(Diet)+(1+Time|Cow),
REML=FALSE,control = lmerControl(optimizer ="Nelder_Mead"),
data = mm2.1)
drop1(fit.m5)
```
## 1d
Now there is a diet effect. (Nelder_mead is used to avoid convergence problems)
# Exercise 2
## 2a
```{r}
mm2.2 <- nlme::BodyWeight
?nlme::BodyWeight
```
```{r}
library(ggplot2)
ggplot(mm2.2,aes(x=Time,y=weight,group=Rat))+geom_line(aes(color=Diet))
```
## 2b
```{r}
fit.r1 <- lmer(weight~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(1+Time|Rat),control = lmerControl(optimizer ="Nelder_Mead"),
data = mm2.2)
fit.r2 <- lmer(weight~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(1|Rat),control = lmerControl(optimizer ="Nelder_Mead"),
data = mm2.2)
fit.r3 <- lmer(weight~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(0+Time|Rat),control = lmerControl(optimizer ="Nelder_Mead"),
data = mm2.2)
AIC(fit.r1,fit.r2,fit.r3)
```
Random slopes and random intercepts are needed in the model.
## 2c
```{r}
fit.r4 <- lmer(weight~factor(Time)+factor(Diet)+factor(Diet):factor(Time)+
(1+Time|Rat),control = lmerControl(optimizer ="Nelder_Mead"),
REML=FALSE,data = mm2.2)
drop1(fit.r4)
```
So the interaction is needed. Fit now the nested version:
```{r}
fit.r5 <- lmer(weight~factor(Time)+factor(Diet):factor(Time)+
(1+Time|Rat),control = lmerControl(optimizer ="Nelder_Mead"),
REML=FALSE,data = mm2.2)
summary(fit.r5)
```
## 2d
A model linear mixed effect model with random rat and random time effects was used to analyse the weights.
Model reduction was done wit Akaike's information criterion.
The fixed efffect were diet, time and there interaction.
First using REML the random affect part was determined. Then using maximum likelihood the fixed effect part was examined.
THe diet-time interaction is neede in the model. A nested version of this model showed that there was a difference between diet1 and 2 and btween diet 1 and 3 and that these differences increaed in time.
# Exercise 3
## 3a
```{r}
mm2.3 <- read.csv("osteochon.csv",header=TRUE)
ocfit.1 <- glm(oc~factor(father)+factor(ground)+height,family=binomial,data = mm2.3)
summary(ocfit.1)
```
## 3b
Some standard errors are very large indicating a lack of information. That is there are father with daughters who have no oc.
## 3c
```{r}
mm2.3$sc.height <- mm2.3$height-mean(mm2.3$height)
ocfit.2 <- glmer(oc~factor(ground)+sc.height+(1|father),family=binomial,data = mm2.3)
AIC(ocfit.1,ocfit.2)
summary(ocfit.2)
```
For the fixed effect model the AIC is 369.4 and for the random effect model this is 358.4 adifference of 11, so the random effect model is much better. The estimates and standard errors for height ar quite similar.
## 3d
The observations within a father are now correlated on the logit scale. The standard deviation between the fathers on the logit scale is .377.
# Exercise 4
## 4a
```{r}
mm.gt <- grouseticks
fitgt <- glmer(TICKS~YEAR+cHEIGHT+(1|BROOD)+(1|INDEX),data = mm.gt,family="poisson")
AIC(fitgt)
```
The AIC is 1794.04. The AIC of the previous fit was 2766.89. So this model fits the data much better.
```{r}
mm.gt <- data.frame(mm.gt,res=residuals(fitgt))
ggplot(mm.gt,aes(sample=res))+
stat_qq()+stat_qq_line(color="red")+
labs(y="Residuals",
title="Normal Probability plot")+
theme_bw()
```
This residual plot also looks much better.