-
Notifications
You must be signed in to change notification settings - Fork 8
/
nested_fully-crossed_cross-classified_models.Rmd
74 lines (53 loc) · 2.36 KB
/
nested_fully-crossed_cross-classified_models.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
---
title: "Nested and Crossed Random Effects Models"
author: "Daniel Lüdecke"
date: "26 3 2019"
output: html_document
---
```{r setup, include=FALSE,echo=FALSE}
library(knitr)
knitr::opts_chunk$set(
echo = TRUE,
collapse = TRUE,
warning = FALSE,
comment = "#>",
dev = "png"
)
```
# Nested and Crossed Random Effects Models
Say we have a model with a dependent variable **DV**, independent variable **IV** and groups as random effects (**Cluster**, **Subject**). The **IV** varies across **Cluster** and **Subject**.
Is this a nested, fully crossed or cross-classified design?
## Nested design
The key distinction is whether each **Subject** receives a completely different **Cluster** set. If this is the case the design is _nested_, which simply means: _not crossed_.
```{r eval=FALSE}
lmer(DV ~ IV + (1 + IV | Cluster / Subject), data = ...)
```
which expands to...
```{r eval=FALSE}
lmer(DV ~ IV + (1 + IV | Cluster ) + (1 + IV | Cluster:Subject), data = ...)
```
## Fully-crossed or cross-classified models
If each **Subject** receives the same **Cluster** (i.e. subjects appear in all clusters), it is a _fully crossed_ random factors design. If there is some mixture it is _cross-classified_. The appropriate model notation for a crossed design would be:
```{r eval=FALSE}
lmer(DV ~ IV + (1 + IV | Cluster) + (1 + IV | Subject), data = ...)
```
## Easily check if group factors are nested or crossed
You can use the [**sjmisc**-package](https://strengejacke.github.io/sjmisc/) to check whether group factors are (fully) crossed, nested or cross-classified.
`is_cross_classified()` returns `TRUE`, so a cross-classified design would be appropriate for this random effects structure.
```{r}
# data with cross-classified distribution of "cluster" and "subject"
data <- data.frame(
cluster = rep(1:5, each = 3),
subject = c(1,2,3, 1, 2, 4, 1, 2, 3, 1, 2, 5, 1, 2, 4)
)
# the table output indicates that data is not nested, but also not fully crossed
table(data)
# check nesting / crossing of group factors
library(sjmisc)
is_nested(data$cluster, data$subject)
is_crossed(data$cluster, data$subject)
is_cross_classified(data$cluster, data$subject)
```
# References
* Related post: https://www.researchgate.net/post/Multilevel_modelling_in_R
* See also: https://stats.stackexchange.com/a/228814/54740