-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathTutorial2-ProperSpecification.Rmd
83 lines (59 loc) · 2.12 KB
/
Tutorial2-ProperSpecification.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
---
title: "Tutorial2-ProperSpecificationofREmodels"
author: "Jared Knowles"
date: "Saturday, May 17, 2014"
output: html_document
---
<style>
p
{
max-width:550px;
}
</style>
```{r setup, echo=FALSE, error=FALSE, message=FALSE, eval=TRUE, results='hide'}
library(knitr)
opts_chunk$set(dev='svg', fig.width=6, fig.height=6, echo=TRUE,
message=FALSE, error=FALSE, warning=FALSE)
```
```{r loadandviewdata}
library(lme4) # load library
library(arm) # convenience functions for regression in R
lmm.data <- read.table("http://bayes.acs.unt.edu:8083/BayesContent/class/Jon/R_SC/Module9/lmm.data.txt",
header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE)
#summary(lmm.data)
head(lmm.data)
```
## Fully Interacted Models
```{r fullinteract}
classEff <- lmList(extro ~ open + agree + social | class, data = lmm.data)
schoolEff <- lmList(extro ~ open + agree + social | school, data = lmm.data)
```
## Non-nested Classes
```{r lmer2}
MLexamp.7 <- lmer(extro ~ open + agree + social + (1|school) + (1|class), data=lmm.data)
display(MLexamp.7)
```
## Nested group effects
And finally, we can fit nested group effect terms through the following syntax:
```{r lmer3}
MLexamp.8 <- lmer(extro ~ open + agree + social + (1|school/class), data=lmm.data)
display(MLexamp.8)
```
Here the `(1|school/class)` says that we want to fit a mixed effect term for varying
intercepts `1|` by schools, and for classes that are nested within schools.
```{r}
MLexamp2 <- update(MLexamp1, .~ . + (1|class)) # add a intercept for class
MLexamp2 <- lmer(extro ~ open + agree + social + (1|class) + (1|school),
data = lmm.data)
exactRLRT(m = MLexamp1, mA=MLexamp2,
m0 = )
```
## Non-correlated random effects
```{r lmer4}
MLexampUnCor <- lmer(extro ~ open + agree + social + (1|school/class) + (0 + open|school/class),
data = lmm.data)
MLexampCor <- lmer(extro ~ open + agree + social + (1+open|school/class),
data = lmm.data)
VarCorr(MLexampUnCor)
VarCorr(MLexampCor)
```