-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGLM2020_Chapter09_CleanedUp.R
286 lines (225 loc) · 9.3 KB
/
GLM2020_Chapter09_CleanedUp.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#Assignments in this doc:
#Chapter 9: 2 Level Longitudinal Data
"""
Construct Figures: 9.2, 9.4, 9.7
Replicate analyses in:
§9.5.2 and §9.5.3.
Construct Figures: 9.18 - Model C (in §9.6.1)
Replicate analyses in: §9.6.3
"""
#Chapter 9: Longitudinal 2-level models
### Construct Figures: 9.2, 9.4, 9.7 Use R and SAS (proc mixed) to replicate analyses in: §9.5.2 and §9.5.3.
#################
#Prep Data / Early Chapter Work
#################
#We read in the data:
mydata <- read.csv("https://raw.githubusercontent.com/proback/BYSH/master/data/chart_wide_condense.csv")
#Take a look at it
#View(mydata)
names(mydata)
##########################
#Data Org in 9.3
##########################
#The book uses the lmer function so we do too:
#install.packages("lme4")
library(lme4)
#We reshape the data from WIDE to LONG
#We tell it which columns to collapse
longdata <- reshape(mydata, varying = c("MathAvgScore.0", "MathAvgScore.1", "MathAvgScore.2"),
timevar = "year08", idvar = "schoolid", direction = "long", sep = ".")
#View(longdata)
library(ggplot2)
#To make plots we generate means by school
means <- aggregate(MathAvgScore ~ schoolid, longdata, mean)
#View(means)
names(means) <- c("schoolid", "MeanScore")
library(dplyr)
leftdata <- left_join(means, longdata, by='schoolid')
#View(leftdata)
#We grab just the columns we want to work with
names(leftdata)
select<-c("schoolid", "MeanScore", "MathAvgScore", "urban", "charter")
selectedcols <- leftdata[,select]
names(selectedcols)
#View(selectedcols)
#For labeling our graphs we turn binary columns into factors
selectedcols$urbanbox <- factor(selectedcols$urban,
labels = c("rural", "urban"))
selectedcols$charterbox <- factor(selectedcols$charter,
labels = c("public non-charter", "charter"))
#And we make sure that it worked:
#View(selectedcols)
#We make Figure 9.2 the box plots
#First Box Plot
selectedcols$charterboxorder <- ordered(selectedcols$charterbox, levels = c("charter", "public non-charter"))
p <- ggplot(selectedcols, aes(y = as.factor(charterboxorder), x = MeanScore)) +
geom_boxplot()
p <- p + xlab("Mean Math Scores by School") +
ylab(" ") +
ggtitle("(a)")
#Reshape the graph viewer in RStudio to desired proportions
p
#Second Box Plot
p <- ggplot(selectedcols, aes(y = as.factor(urbanbox), x = MeanScore)) +
geom_boxplot()
p <- p + xlab("Mean Math Scores by School") +
ylab(" ") +
ggtitle("(b)")
p
#Lattice plot
#We use the long data set again, since we don't want means
#View(longdata)
#We want all the data from "the first" 24 schools so we get that list
theschoools <- levels(longdata$schoolid)
theschoools <- theschoools[1:24]
#and subset our data from those 24
mysubset95 <- longdata[ which(longdata$schoolid %in% theschoools), ]
#View(mysubset95)
#And make the plot
q <- ggplot(mysubset95, aes(x=year08, y = MathAvgScore)) +
geom_line() +
geom_point() +
facet_wrap(~ schoolid, ncol=6) +
ylab("Math Score") +
xlab("Years since 2008") +
ggtitle(" ")+
theme(
strip.background = element_blank(),
strip.text.x = element_blank()
)
q
#Making figure 9.7
#Which uses a random sample from public schools and from charter schools
#We set up the charter factor and order it as in the graph
longdata$charterbox <- factor(longdata$charter,
labels = c("public non-charter", "charter"))
longdata$charterboxorder <- ordered(longdata$charterbox, levels = c("charter", "public non-charter"))
#We take a random sample of 60 from each category
longcharter <- longdata[ which(longdata$charter == 1), ]
longpublic <- longdata[ which(longdata$charter == 0), ]
schools_c <- unique(longcharter$schoolid)
schools_c <- schools_c[sample(length(schools_c),60)]
schools_p <- unique(longpublic$schoolid)
schools_p <- schools_p[sample(length(schools_p),60)]
#Combine them and filter it down
schools_r <- unlist(list(schools_c, schools_p))
longdata97 <- longdata[ which(longdata$schoolid %in% schools_r), ]
#And we make the plot
pq <- ggplot(longdata97, aes(x=year08, y=MathAvgScore)) +
geom_line(alpha = 0.5) + guides(colour=FALSE) + xlab("Observation Time Point") +
ylab("Y") + aes(colour = factor(schoolid))+
facet_wrap(~ charterboxorder, ncol=2) +
ylab("Math Score") +
xlab("Years since 2008")
pq +
geom_smooth(method="loess", colour="black", se=TRUE)
##########################
#Use R . . . to replicate analyses in: §9.5.2
##########################
names(longdata)
#We buld our multilevel model, much like last time
multilevels <- lmer(formula = MathAvgScore ~ year08 + (year08 | schoolid), data=longdata)
summary(multilevels)
##########################
#Use R . . . to replicate analyses in: §9.5.3.
##########################
#We buld our quadratic time model
#Engineer a couple of new columns
longdata$timec <- longdata$year08 - 1
longdata$timec2 <- longdata$timec*longdata$timec
#View(longdata)
quadlevels <- lmer(formula = MathAvgScore ~ timec + timec2 + (1 | schoolid), data=longdata)
summary(quadlevels)
#################
#Construct Figures: 9.18 - Model C (in §9.6.1)
#Replicate analyses in: §9.6.3
#################
#We read in the data:
#mydata <- read.csv("https://raw.githubusercontent.com/proback/BYSH/master/data/chart_wide_condense.csv")
#Take a look at it
#View(mydata)
names(mydata)
##########################
#Data Org in 9.3, retained from above, still needed
##########################
#The book uses the lmer function so we do too:
library(lme4)
library(ggplot2)
library(dplyr)
#We reshape the data from WIDE to LONG
#We tell it which columns to collapse
longdata <- reshape(mydata, varying = c("MathAvgScore.0", "MathAvgScore.1", "MathAvgScore.2"),
timevar = "year08", idvar = "schoolid", direction = "long", sep = ".")
#View(longdata)
#We fit models B and C for figure 9.18
#For labeling our graphs we turn binary columns into factors
longdata$urbanbox <- factor(longdata$urban,
labels = c("rural", "urban"))
longdata$charterbox <- factor(longdata$charter,
labels = c("public non-charter", "charter"))
names(longdata)
#We buld our 9.6 model C, a composite of Level One and Level Two models:
multilevels <- lmer(formula = MathAvgScore ~ charter + year08 + charter:year08 + (year08 | schoolid),
data=longdata)
summary(multilevels)
#We also need Model B from last time for the figure:
longdata$timec <- longdata$year08 - 1
longdata$timec2 <- longdata$timec*longdata$timec
#View(longdata)
quadlevels <- lmer(formula = MathAvgScore ~ timec + timec2 + (1 | schoolid), data=longdata)
summary(quadlevels)
#We fashion some data to predict on for a curve to graph
#Give it the necessary structure:
preddata <- longdata[0,]
#generate the data:
for (yeari in 0:2){
#unconditional
newrow <- data.frame(1, 12345, "WalterWinchell", 1, 0, .5, .5, .5, yeari, 666, "fixthis", "fixthis", 0, 0)
names(newrow) <- names(preddata)
preddata <- rbind(preddata, newrow)
#public
newrow <- data.frame(1, 12345, "WalterWinchell", 0, 0, .5, .5, .5, yeari, 666, "fixthis", "fixthis", 0, 0)
names(newrow) <- names(preddata)
preddata <- rbind(preddata, newrow)
#charter
newrow <- data.frame(1, 12345, "WalterWinchell", 0, 1, .5, .5, .5, yeari, 666, "fixthis", "fixthis", 0, 0)
names(newrow) <- names(preddata)
preddata <- rbind(preddata, newrow)
}
#and for graphing:
preddata$urbanbox <- factor(preddata$urban,
labels = c("rural", "urban"))
preddata$charterbox <- factor(preddata$charter,
labels = c("public non-charter", "charter"))
preddata$timec <- preddata$year08 - 1
preddata$timec2 <- preddata$timec*preddata$timec
preddata$charterboxorder <- ordered(preddata$charterbox, levels = c("charter", "public non-charter"))
#We have a look:
#View(preddata)
#We predict the values to graph for the appropriate rows:
B_res <- predict(quadlevels, preddata, allow.new.levels = TRUE)
C_res <- predict(multilevels, preddata, allow.new.levels = TRUE)
relevantresults <- c(B_res[1], C_res[2], C_res[3], B_res[4], C_res[5], C_res[6], B_res[7], C_res[8], C_res[9])
#And affix them to our data set
preddata <- cbind(preddata, results = relevantresults)
preddata <- cbind(preddata, which_model = c(rep(c("B", "C", "C"), 3)))
#and take a look:
#View(preddata)
#And we make the plot:
pq <- ggplot(preddata, aes(x=year08, y=results, group=charterboxorder)) +
geom_line(aes(linetype=charterboxorder)) + guides(colour=FALSE) +
xlab("Years since 2008") +
ylab("Predicted Math Score") +
aes(colour = factor(schoolid))+
facet_wrap(~ which_model, labeller = "label_both", strip.position = "top") +
ggtitle("Fitted growth curves for Models B and C") +
ylim(639, 661) +
labs(shape="", linetype='')+
geom_point(color="black", size=3, aes(shape=charterboxorder))
pq
#And the analysis from 9.6.3 is here:
#Model F
modelF <- lmer(formula = MathAvgScore ~ charter + year08 + schPctfree + schPctsped + urban +
urban:year08 + schPctsped:year08 + charter:year08 + (year08 | schoolid),
data=longdata)
summary(modelF)