-
Notifications
You must be signed in to change notification settings - Fork 1
/
R-bios6643-L09.Rmd
executable file
·151 lines (102 loc) · 4.53 KB
/
R-bios6643-L09.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
---
title: "BIOS6643. L9 Specifying LMM through $G_i$ and $R_i$ structures"
cheauthor: "EJC"
-- date: ""
header-includes:
- \usepackage{amsmath}
- \usepackage{float}
output: pdf_document
---
\newcommand{\bi}{\begin{itemize}}
\newcommand{\ei}{\end{itemize}}
\newcommand{\itt}{\item}
```{r setup, include=FALSE, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
<!-- -->
```{r installp, echo = FALSE, eval=T, include=F}
library(dplyr)
library(tidyverse)
library(ggplot2)
library(nlme)
library(lme4)
```
# Dental study
## Description
- The orthodontic study data of Potthoff and Roy (1964).
- World famous data set that is used to introduce features of longitudinal data modeling and analysis
- A study was conducted involving 27 children, 16 boys and 11 girls
- For each child, the distance (mm) from the center of the pituitary to the pterygomaxillary fissure was measured at ages 8, 10, 12, and 14 years of age
- The pterygomaxillary fissure is a vertical opening in the human skull.
- **Objectives** of the study included:
- Determine if distances over time are larger on average for boys than for girls
- Determine if the rate of change of distance over time is different for boys and girls.
```{r eval=T, echo = T, include=TRUE}
# Read in the data
dat.den <- read.table("/Users/juarezce/Documents/OneDrive - The University of Colorado Denver/BIOS6643/BIOS6643_Notes/data/dental.txt")
dat.den <- dat.den[,2:5] # remove the first column
colnames(dat.den) <- c("id","age","distance","gender")
# Total number of individuals
m <- max(dat.den$id)
head(dat.den, 2)
table(dat.den$gender)
```
## Some descriptives:
```{r eval=T, echo = F, fig.height=3.7, include=T}
################################################################################################
###
dat.den <- within(dat.den, { id <- factor(id)
gender <- factor(gender,levels=0:1,labels=c("Girls","Boys"))
})
ggplot(dat.den, aes(x = age, y = distance, group = id)) +
geom_line(aes(color = gender), alpha = 0.5) +
theme_classic() +
theme(legend.position = "top") +
ylab("Distance")
```
## a. Common G matrix for both genders with random intercept and slopes
```{r eval=T, echo = T, include=T}
# (a) Common G matrix for both genders, default diagonal within-child
# covariance matrix R_i with same variance sigma^2 for each
# gender.
dental.lme.a <- lme(distance ~ -1 + gender + age:gender,data=dat.den,
random = ~ age | id,method="ML")
summary(dental.lme.a)
beta.a <- fixed.effects(dental.lme.a) # beta, also fixef(dental.lme.a)
b.a <- random.effects(dental.lme.a) # posterior modes bi, also ranef(dental.lme.a)
sebeta.a <- summary(dental.lme.a)$tTable[,"Std.Error"]
## Recall we can get the var-cov of fixed coefficients using 'varFix'
## dental.lme.a$varFix
G.a <- getVarCov(dental.lme.a, type="random.effects") # G matrix
sigma2.a <- dental.lme.a$sigma^2 # sigma^2
V.a <- getVarCov(dental.lme.a, type="marginal", individual=1) # V_i
R.a <- getVarCov(dental.lme.a, type="conditional", individual=1) # R_i
G.a
V.a
R.a
```
## b. Common G matrix with random intercepts and slopes, diagonal within-child covariance matrix R_i with different variance for each gender
```{r eval=T, echo = T, include=T}
dental.lme.b <- lme(distance ~ -1 + gender + age:gender,data=dat.den,
random = ~ age | id, weights = varIdent(form = ~ 1 | gender),
method="ML")
beta.b <- fixed.effects(dental.lme.b) # beta
sebeta.model.b <- sqrt(diag(dental.lme.b$varFix))
b.b <- random.effects(dental.lme.b) # posterior modes bi
G.b <- getVarCov(dental.lme.b, type="random.effects") # G matrix
R.b.1 <- getVarCov(dental.lme.b,type="conditional",individual=1) # R_1; first girl
R.b.12 <- getVarCov(dental.lme.b,type="conditional",individual=12) # R_12; first boy
G.b
R.b.1
R.b.12
V.b.1 <- getVarCov(dental.lme.b,type="marginal",individual=1) # V_1 (girl)
V.b.12 <- getVarCov(dental.lme.b,type="marginal",individual=12) # V_12 (boy)
V.b.1
V.b.12
```
## c. Fit a common G matrix for both genders, and common within-child AR(1)
## d. Fit a common G matrix for both genders, common within-child AR(1) exponential correlation
## Compare models
## Refit final model using REML. Obtain population (PA) and subject-specific (SS) fitted values and residuals.
**Note**: You may use *fitted(fit,level=0:1)* and *residuals(fit,level=0:1)*, where level=0 gives PA, level = 1 gives SS (default), and level = 0:1 gives both.
# Fit a LMM with random intercept and slopes using lme4:lmer