-
Notifications
You must be signed in to change notification settings - Fork 0
/
Code.R
400 lines (348 loc) · 13.2 KB
/
Code.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# Setup ----
setwd("D:/RStudio/UL/EC6062 project") # Set working directory
library("car") # Calculate Variance Inflation Factor
library("GGally") # Correlation matrix
library("ggplot2") # Graphs
library("ggthemes") # Theme options for graphs
library("gridExtra") # Combine multiple graphs
library("lmtest") # Calculate standard errors adjusted for heteroskedasticity
library("sandwich") # Heteroscedasticity-consistent covariance matrix estimation
library("skedastic") # Breusch-Pagan and White's tests for heteroskedasticity
library("stargazer") # Regression analysis for multiple models
library("tidyverse") # Better data processing
# Download data ----
salary_potential <-
readr::read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/salary_potential.csv"
)
tuition_cost <-
readr::read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/tuition_cost.csv"
)
diversity_school <-
readr::read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/diversity_school.csv"
)
tuition_income <-
readr::read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/tuition_income.csv"
)
historical_tuition <-
readr::read_csv(
"https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2020/2020-03-10/historical_tuition.csv"
)
state_region <-
readr::read_csv(
"https://raw.githubusercontent.com/cphalpert/census-regions/master/us%20census%20bureau%20regions%20and%20divisions.csv"
)
# Cleaning and combining data
salary_potential <- salary_potential %>%
rename(state = state_name) %>% # Rename "state_name" to state
mutate(# Calculate "average pay" as mean of early and mid career pay
average_pay = (early_career_pay + mid_career_pay) / 2,
.keep = "unused")
tuition_cost <- tuition_cost %>%
mutate(# Calculate "average cost" of university as mean of in state and out state
average_cost = (in_state_total + out_of_state_total) / 2,
.keep = "unused") %>%
select("type", "name", "state", "average_cost") # Select specific columns
diversity_school <- diversity_school %>%
filter(category == "Total Minority") %>% # Select total number of "minority" students for each school
mutate(# Calculate"diversity_percent" as percentage of total students who belong to a "minority" group
diversity_percent = ceiling(enrollment * 100 / total_enrollment)) %>%
select("name", "total_enrollment", "diversity_percent", "state") # Select all columns except "category"
state_region <- state_region %>%
rename("state" = "State") %>%
select(-c("State Code"))
# Combine above datasets using inner joins based on matching name and state
data <- diversity_school %>%
inner_join(tuition_cost, by = c("name", "state")) %>%
inner_join(salary_potential, by = c("name", "state")) %>%
inner_join(state_region, by = "state")
# Data visualization ----
# Plot top 5 states for certain attributes (percentage of students)
g1 <- data %>%
group_by(state) %>% # For each state
summarize(
# Calculate median values
median_inspired_students = median(make_world_better_percent),
median_stem_students = median(stem_percent),
median_minority_students = median(diversity_percent)
) %>%
gather(key = "key", value = "value", -state) %>% # Calculate for each statistic
group_by(key) %>%
slice_max(value, n = 5) %>% # Take top 5 values
ggplot(aes(x = state, y = value, fill = state)) + # Create plot
geom_col() + # Bar plot
theme_economist() + # Set theme
scale_color_economist() + # Set colors
theme(legend.position = "none") + # Remove legend
facet_wrap(~ key, scales = "free") + # Wrap by measured statistic
labs(
# Labels
title = "Top 5 states per statistic",
subtitle = "Bar plot, grouped by feature, ordered alphabetically",
x = "",
y = "Percentage of students"
)
# Plot top 5 states for certain attributes (cost in USD)
g2 <- data %>%
group_by(state) %>%
summarize(
median_total_cost = median(average_cost),
median_average_pay = median(average_pay)
) %>%
gather(key = "key", value = "value", -state) %>%
group_by(key) %>%
slice_max(value, n = 5) %>%
ggplot(aes(x = state, y = value, fill = state)) +
geom_col() +
theme_economist() +
scale_color_economist() +
theme(legend.position = "none") +
facet_wrap(~ key, scales = "free") +
labs(caption = "n = 519",
x = "State (alphabetical)",
y = "Amount (USD)")
# Combine and display above graphs
grid.arrange(g1, g2)
# Calculate and plot correlation matrix
data %>%
select(-c("name", "state", "type", "Region", "Division")) %>% # De-select categorical variables
ggcorr(
# Correlation plot
hjust = 0.75,
label = TRUE,
layout.exp = 2,
high = "#014d64",
mid = "white",
low = "#014d64"
) +
theme_void() + # Colors
theme(
legend.position = "top",
plot.background = element_rect(fill = "#d5e4eb"),
legend.key.width = unit(2, "cm")
) +
labs(# Labels
title = "Correlation matrix of features",
subtitle = "Heat map",
caption = "n = 519")
# Plot historical tuition cost vs post-graduation income bracket data
tut1 <- tuition_income %>%
filter(year %% 2 == 0) %>% # Only even years, prevent crowded graph
ggplot(aes(# Plot graph
y = net_cost,
x = income_lvl,
color = income_lvl)) +
geom_boxplot() + # Specify box plot
facet_grid(rows = vars(year), as.table = FALSE) + # Separate plot for each year
coord_flip() + # Flip horizontally
theme_economist() + # Set theme
scale_color_economist() + # Set colors
theme(legend.position = "none") + # Remove legend
labs(
# Labels
title = "Total university cost vs income bracket",
subtitle = "Box plot, grouped by year",
caption = "n = 110,448",
y = "Total university cost (USD)",
x = "Post-graduation income bracket"
)
# Plot historical tuition data for public and private schools, based on course length
tut2 <- historical_tuition %>%
filter(type != "All Institutions",
# Keep only public and private schools
tuition_type %in% c("4 Year Constant", "2 Year Constant")) %>%
ggplot(aes(# Plot
x = year,
y = tuition_cost,
group = type)) +
geom_point(aes(color = type), size = 2) + # Scatter plot for points
geom_line(aes(color = type), size = 1) + # Line plot
scale_y_continuous(limits = c(0, 42000)) + # Start y-axis at zero
facet_grid(rows = vars(tuition_type)) + # Group plots by tuition type
theme_economist() + # Set theme
scale_color_economist() + # Set colors
labs(
# Labels
title = "Tuition cost by year",
subtitle = "Line plot, grouped by course length",
x = "Year",
y = "Tuition cost (USD)"
)
# Combine and display above graphs
grid.arrange(tut1, tut2)
# Plot tuition cost vs average post graduation salary
kv1 <- data %>%
ggplot(aes(# Plot
x = log(average_cost),
y = log(average_pay))) +
geom_point(aes(color = type)) + # Scatter plot for points
geom_smooth(method = "lm",
formula = y ~ poly(x, 3),
color = "#6794a7") +
theme_economist() + # Set theme
scale_color_economist() + # Set colors
labs(
# Labels
title = "Average pay vs average cost (logarithm) (USD)",
subtitle = "Scatter plot, colored by university type, third degree polynomial estimate",
x = "Average university cost (logarithm) (USD)",
y = "Average post graduation salary (logarithm) (USD)"
)
# Plot geographic region vs average post graduation salary
kv2 <- data %>%
ggplot(aes(
# Plot
x = Region,
y = log(average_pay),
group = Region
)) +
geom_boxplot() + # Scatter plot for points
theme_economist() + # Set theme
scale_color_economist() + # Set colors
labs(
# Labels
title = "Average pay (logarithm) vs Geographic region",
subtitle = "Box plot, colored by university type",
x = "Geographic region",
y = "Average post graduation salary (logarithm) (USD)"
)
# Plot total enrollment vs average post graduation salary
kv3 <- data %>%
ggplot(aes(# Plot
x = log(total_enrollment),
y = log(average_pay))) +
geom_point(aes(color = type)) + # Scatter plot for points
geom_smooth(method = "lm",
formula = y ~ x,
color = "#6794a7") +
theme_economist() + # Set theme
scale_color_economist() + # Set colors
labs(
# Labels
title = "Average pay (logarithm) (USD) vs student body size (logarithm)",
subtitle = "Scatter plot, colored by university type, linear estimate",
x = "student body size (logarithm)",
y = "Average post graduation salary (logarithm) (USD)"
)
# Plot rank vs average post graduation salary
kv4 <-
data %>%
ggplot(aes(
# Plot
x = rank,
y = log(average_pay),
group = rank
)) +
geom_boxplot() + # Scatter plot for points
theme_economist() + # Set theme
scale_color_economist() + # Set colors
labs(
# Labels
title = "Average pay (logarithm) vs university ranking",
subtitle = "Box plot, colored by university type",
x = "University ranking",
y = "Average post graduation salary (logarithm) (USD)"
)
# Combine and display above graphs
grid.arrange(kv1, kv2, kv3, kv4)
# Data analysis ----
# Select data for model
model_data <-
data %>%
select(-c(name, state, Division)) %>% # Exclude name of school and location
mutate_at("type", as.factor) # Convert "type" to dummy variable
# Estimate models
lm_1 <-
lm(
log(average_pay) ~ log(average_cost) +
total_enrollment +
I(make_world_better_percent^2),
data = model_data
)
lm_2 <-
lm(
log(average_pay) ~ log(average_cost) +
total_enrollment +
I(rank^2),
data = model_data
)
lm_3 <-
lm(
log(average_pay) ~ log(average_cost) +
total_enrollment +
rank,
data = model_data
)
lm_4 <-
lm(
log(average_pay) ~ poly(log(average_cost), 3, raw = T) +
log(total_enrollment) +
poly(rank, 2),
data = model_data
)
lm_5 <-
lm(
log(average_pay) ~ poly(log(average_cost), 3, raw = T) +
log(total_enrollment) +
poly(rank, 2) +
Region,
data = model_data
)
lm_6 <-
lm(
log(average_pay) ~ poly(log(average_cost), 3, raw = T) +
log(total_enrollment) +
poly(rank, 2) +
Region +
make_world_better_percent,
data = model_data
)
# View regression analysis for models
independant_variable_names_1 = c("average cost (logarithm)",
"total enrollment",
"make world better \\%",
"rank\\^2",
"rank",
"(Intercept)")
independant_variable_names_2 = c("average cost (logarithm)",
"average cost (logarithm)\\^2",
"average cost (logarithm)\\^3",
"total enrollment (logarithm)",
"rank",
"rank\\^2",
"Region, Northeast",
"Region, South",
"Region, West",
"make world better \\%",
"(Intercept)")
stargazer(lm_1,lm_2,lm_3,
type="latex",
out="./stargazer1.tex",
font.size="tiny",
no.space = TRUE,
dep.var.labels = c("average pay (logarithm)"),
covariate.labels = independant_variable_names_1,
column.labels = c("M1","M2","M3"))
stargazer(lm_4,lm_5,lm_6,
type="latex",
out="./stargazer2.tex",
font.size="tiny",
no.space = TRUE,
dep.var.labels = c("average pay (logarithm)"),
covariate.labels = independant_variable_names_2,
column.labels = c("M4","M5","M6"))
# View model metrics
lm_5 %>% summary()
# Ramsey's RESET test to determine model misspecification
resettest(lm_5)
# Calculate Variance Inflation Factor
vif(lm_5)
# Breusch-Pagan Test for heteroskedasticity
breusch_pagan(lm_5)
# White's Test for heteroskedasticity
white_lm(lm_5)
# Linear model with standard errors adjusted for heteroskedasticity
coeftest(lm_5, vcov = vcovHC(lm_5))