-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_GeneralizedLinearMEM
111 lines (100 loc) · 2.88 KB
/
3_GeneralizedLinearMEM
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
# Fit a glm using data in a long format
fitLong <- glm(mortality ~ dose, data = dfLong, family = "binomial")
summary(fitLong)
# Fit a glm using data in a short format with two columns
fitShort <- glm(cbind(mortality, survival) ~ dose, data = dfShort, family = "binomial")
summary(fitShort)
# Fit a glm using data in a short format with weights
fitShortP <- glm(mortalityP ~ dose, data = dfShort, weights = nReps, family = "binomial")
summary(fitShortP)
#
##
###
# Fit the linear model
summary(lm(y ~ x))
# Fit the generalized linear model
summary(glm(y ~ x, family = "poisson"))
#
##
###
# Plot the data using jittered points and the default stat_smooth
ggplot(data = dfLong, aes(x = dose, y = mortality)) +
geom_jitter(height = 0.05, width = 0.1) +
stat_smooth(fill = 'pink', color = 'red')
# Plot the data using jittered points and the the glm stat_smooth
ggplot(data = dfLong, aes(x = dose, y = mortality)) +
geom_jitter(height = 0.05, width = 0.1) +
stat_smooth(method = 'glm',
method.args = list(family = "binomial"))
#
##
###
# Load lmerTest
library(lmerTest)
# Fit glmerOut and look at its summary
glmerOut <- glmer(mortality ~ dose + (1 | replicate), family = 'binomial', data = df)
summary(glmerOut)
#
##
###
# Load lmerTest
library(lmerTest)
# Fit the model and look at its summary
modelOut <- glmer(cbind(Purchases, Pass) ~ friend + ranking + (1 | city), data = allData, family = 'binomial')
summary(modelOut)
# Compare outputs to a lmer model
summary(lmer(Purchases/(Purchases + Pass) ~ friend + ranking + (1 | city), data = allData))
#
##
###
# Run the code to see how to calculate odds ratios
summary(modelOut)
exp(fixef(modelOut)[2])
exp(confint(modelOut)[3,])
#
##
###
# Load lmerTest
library(lmerTest)
# Fit a Poisson glmer
summary(glmer(clicks ~ webpage + (1 | group), family = 'poisson', data = userGroups))
#
##
###
# Load lmerTest
library(lmerTest)
# Age goes before year
# Age and year as fixed effects, then year as a random effect of county
modelOut <- glmer(count ~ age + year + (year|county), family = 'poisson',
data = ILdata)
summary(modelOut)
#
##
###
# Extract out fixed effects
fixef(modelOut)
# Extract out random effects
ranef(modelOut)
# Run code to see one method for plotting the data
ggplot(data = ILdata2, aes(x = year, y = count, group = county)) +
geom_line() +
facet_grid(age ~ .) +
stat_smooth(method = 'glm',
method.args = list(family = "poisson"), se = FALSE,
alpha = 0.5) +
theme_minimal()
#
##
###
# Extract out fixed effects
fixef(modelOut)
# Extract out random effects
ranef(modelOut)
# Run code to see one method for plotting the data
ggplot(data = ILdata2, aes(x = year, y = count, group = county)) +
geom_line() +
facet_grid(age ~ . ) +
stat_smooth(method = 'glm',
method.args = list(family = "poisson"), se = FALSE,
alpha = 0.5) +
theme_minimal()