forked from ourcodingclub/CC-Linear-mixed-models
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_linear_mixed_models_script.R
97 lines (74 loc) · 3.4 KB
/
my_linear_mixed_models_script.R
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
library(tidyverse)
library(lme4)
library(ggeffects)
load("dragons.RData")
head(dragons)
basic_lm <- lm(testScore ~ bodyLength2, data = dragons)
summary(basic_lm)
dragons$bodyLength2 <- scale(dragons$bodyLength, center = T, scale = T)
ggplot(dragons, aes(bodyLength2, testScore)) +
geom_point() +
geom_smooth(method = "lm")
plot(basic_lm, which = 1)
plot(basic_lm, which = 2)
ggplot(dragons, aes(mountainRange, testScore)) +
geom_boxplot()
(colour_plot <- ggplot(dragons, aes(bodyLength, testScore, colour = mountainRange)) +
geom_point(size = 2) +
theme_classic() +
theme(legend.position = "right"))
(split_plot <- ggplot(aes(bodyLength, testScore), data = dragons) +
geom_point() +
facet_wrap(~ mountainRange) +
xlab("length") +
ylab("test score"))
mountain_lm <- lm(testScore ~ bodyLength2 + mountainRange, data = dragons)
summary(mountain_lm)
# Fixed effect -- predictive variable
# Random effect -- colloquially, the grouping factors
#
# Note: Random effects should have at least five levels. If we had sex as a variable with two levels,
# we would specify it is a fixed, not random effect
mixed_lmer <- lmer(testScore ~ bodyLength2 + (1|mountainRange), data = dragons)
summary(mixed_lmer)
# Nested and (partially) crossed random effects
#
# Nested random effects, the factor appears ONLY within a particular level of another factor
# Crossed random effects, given factor appears in more than one level of another factor (same dragon, multiple mountains)
#
# leafLength ~ treatment + (1|Bad/Plant/Leaf) + (1|Season)
# ^^^^^^^^^^^^^^^^ ^^^^^^^^
# nestedeffects crossed effects
dragons <- within(dragons, sample <- factor(mountainRange:site)) # turning implicit nesting into explicit one
mixed_lmer2 <- lmer(testScore ~ bodyLength2 + (1|mountainRange) + (1|sample), data = dragons)
summary(mixed_lmer2)
(mm_plot <- ggplot(dragons, aes(x = bodyLength, y = testScore, colour = site)) +
facet_wrap(~ mountainRange, nrow = 2) +
geom_point(alpha = 0.5) +
theme_classic() +
geom_line(data = cbind(dragons, pred = predict(mixed_lmer2)), aes(y = pred), linewidth = 0.5) +
theme(panel.spacing = unit(2, "lines"))
)
mixed_ranslope <- lmer(testScore ~ bodyLength2 + (1 + bodyLength2|mountainRange/site), data = dragons)
summary(mixed_ranslope)
(mm_plot <- ggplot(dragons, aes(x = bodyLength, y = testScore, colour = site)) +
facet_wrap(~ mountainRange, nrow = 2) +
geom_point(alpha = 0.5) +
theme_classic() +
geom_line(data = cbind(dragons, pred = predict(mixed_ranslope)), aes(y = pred), linewidth = 0.5) +
theme(panel.spacing = unit(2, "lines"))
)
pred_mm <- ggpredict(mixed_lmer2, terms = c("bodyLength2"))
(ggplot(pred_mm) +
geom_line(aes(x = x, y = predicted)) +
geom_ribbon(aes(x = x, ymin = predicted - std.error, ymax = predicted + std.error),
fill = "lightgrey", alpha = 0.5) +
geom_point(data = dragons,
aes(x = bodyLength2, y = testScore, colour = mountainRange)) +
labs(x = "Body Length (indexed)", y = "Test Score",
title = "Body length does not affect intelligence in dragons") +
theme_minimal()
)
full_lmer <- lmer(testScore ~ bodyLength2 + (1|mountainRange) + (1|mountainRange:site), data = dragons, REML = F)
reduced_lmer <- lmer(testScore ~ 1 + (1|mountainRange) + (1|mountainRange:site), data = dragons, REML = F)
anova(reduced_lmer, full_lmer)