-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_RepeatedMeasuresMEM
72 lines (66 loc) · 1.73 KB
/
4_RepeatedMeasuresMEM
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
#
##
###
# Run a standard, non-paired t-test
t.test(y[treat == "before"], y[treat == "after"], paired = F)
# Run a standard, paired t-test
t.test(y[treat == "before"], y[treat == "after"], paired = T)
#
##
###
library(lmerTest)
# Run the paired-test like before
t.test(y[treat == "before"], y[treat == "after"], paired = T)
# Run a repeated-measures ANOVA
# treat is the fixed effect and x is the random intercept
anova(lmer(y ~ treat + (1|x)))
#
##
###
# Plot the data
ggplot(data = sleepstudy) +
geom_line(aes(x = Days, y = Reaction, group = Subject)) +
stat_smooth(aes(x = Days, y = Reaction),
method = 'lm', size = 3, se = FALSE)
#
##
###
# Build a lm
lm(Reaction ~ Days, data = sleepstudy)
# Build a lmer
lmer(Reaction ~ Days + (1 | Subject), data = sleepstudy)
#
##
###
# Run an anova
anova(lmerOut)
# Look at the regression coefficients
summary(lmerOut)
#
##
###
#plot hate crimes in NY by Year, grouped by County
ggplot(data = hate, aes(x = Year, y = TotalIncidents, group = County)) +
geom_line() + geom_smooth(method = 'lm', se = FALSE)
#
##
###
# Load lmerTest
library(lmerTest)
# glmer with raw Year
glmer(TotalIncidents ~ Year + (Year|County),
data = hate, family = "poisson")
# glmer with scaled Year, Year2
glmerOut <- glmer(TotalIncidents ~ Year2 + (Year2|County),
data = hate, family = "poisson")
summary(glmerOut)
#
##
###
# Extract and manipulate data
countyTrend <- ranef(glmerOut)$County
countyTrend$county <- factor(row.names(countyTrend), levels = row.names(countyTrend)[order(countyTrend$Year2)])
# Plot results
ggplot(data = countyTrend, aes(x = county, y = Year2)) + geom_point() +
coord_flip() + ylab("Change in hate crimes per year") +
xlab("County")