-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchicago_pollution_modeling.R
375 lines (315 loc) · 12.5 KB
/
chicago_pollution_modeling.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
# Filename: chicago_pollution_modeling.R
# Copyright (c) University of Washington
# License: MIT https://opensource.org/licenses/MIT (See LICENSE file.)
# Repository: https://github.com/deohs/coders
# Authors: Rachel Shaffer, Cooper Schumacher, and Brian High
#
# Automate "many models" with tidyverse using Chicago pollution data.
# Compare base-R and tidyverse (pipes, dplyr, broom and purrr) approaches.
# Compare single-core and parallel processing execution times with mclapply().
# Clear workspace of all objects and unload all extra (non-base) packages.
rm(list = ls(all = TRUE))
if (!is.null(sessionInfo()$otherPkgs)) {
res <- suppressWarnings(lapply(
paste('package:', names(sessionInfo()$otherPkgs), sep = ""),
detach, character.only = TRUE, unload = TRUE, force = TRUE))
}
# Load packages.
if (!require(pacman)) {
install.packages('pacman', repos = 'http://cran.us.r-project.org')
}
pacman::p_load(dlnm, ThermIndex, magrittr, tibble, dplyr, tidyr, rsample,
broom, purrr, modelr, parallel, pryr)
# Get data
data(chicagoNMMAPS)
df <- chicagoNMMAPS
names(df)
# A data frame with 5114 observations on the following 14 variables.
#
# date: Date in the period 1987-2000.
# time: The sequence of observations
# year: Year
# month: Month (numeric)
# doy: Day of the year
# dow: Day of the week (factor)
# death: Counts of all cause mortality excluding accident
# cvd: Cardiovascular Deaths
# resp: Respiratory Deaths
# temp: Mean temperature (in Celsius degrees)
# dptp: Dew point temperature
# rhum: Mean relative humidity
# pm10: PM10
# o3: Ozone
# Calculate humidex
df %<>% mutate(hmdx = humidex(temp, rhum))
# Make vector of outcomes
outcomes <- c("death", "cvd", "resp")
# Make vector of exposures
exposures <- c("pm10", "o3")
# Make formula of outcomes associated w/exposures for each possible combination
base_models <- paste0(outcomes, " ~ ", rep(exposures, each = length(outcomes)))
# Make a list of covariates
covariates_list <- list(
model1 = c("temp"),
model2 = c("temp", "dptp"),
model3 = c("temp", "dptp", "rhum"),
model4 = c("temp", "dptp", "rhum", "hmdx"),
model5 = c("temp", "dptp", "rhum", "hmdx", "dow")
)
# Transfer from list to format separated by "+"
covariates <- unlist(lapply(covariates_list,
function(x) paste(x, collapse = " + ")))
# Develop all combinations of exposures, outcomes, covariates
formula_list_char <- as.list(
paste0(rep(base_models, each = length(covariates)), " + ", covariates))
# Create a list of formulas
formula_list <- lapply(formula_list_char, as.formula)
# Create boostrap samples
nBoot <- 10
# ----------------------------------------------------------------------
# Compare different ways to get the bootrap samples
# ----------------------------------------------------------------------
# Create boot.samples with sample() function, with replacement, using for()
set.seed(12345)
boot.samples <- list()
system.time(
for(i in 1:nBoot){
# Take sample with replacement
boot.samples[[i]] <- df[sample(1:nrow(df)[1], replace = TRUE), ]
}
)
# For nBoot = 100...
# user system elapsed
# 0.823 0.044 0.872
# Create boot.samples with sample() function, with replacement, using lapply()
set.seed(12345)
system.time(
boot.samples2 <- lapply(1:nBoot, function(x) {
df[sample(1:nrow(df), replace = TRUE), ]
})
)
# For nBoot = 100...
# user system elapsed
# 0.780 0.033 0.877
all.equal(boot.samples, boot.samples2)
# Create boot.samples with sample() function, with replacement using map()
set.seed(12345)
system.time(
boot.samples2 <- 1:nBoot %>% map(~df[sample(1:nrow(df), replace = TRUE), ])
)
# For nBoot = 100...
# user system elapsed
# 0.692 0.026 0.723
all.equal(boot.samples, boot.samples2)
# Compare use of sample() to create boot.samples with rsample::bootstraps()
set.seed(12345)
system.time(
boot.samples2 <- df %>% bootstraps(times = nBoot) %>% pull(splits) %>%
map(analysis)
)
# For nBoot = 100...
# user system elapsed
# 0.679 0.000 0.683
all.equal(boot.samples, boot.samples2)
# Test results: All four variations take about the same amount of time to
# run, though the two tidyverse versions are slightly faster.
# Extract summaries from bootstrapped samples
# Define alpha
alpha <- 0.05
# Define a function to extract model results
get_boot_results <- function(.data, .formula, .weights = NULL) {
out <- list()
out$formula <- .formula
.formula <- as.formula(.formula)
coefs <- as.data.frame(matrix(unlist(lapply(.data, function(y)
lm(.formula, y, weights = .weights)$coef)),
nrow = length(seq_along(.data)), byrow = T))
colnames(coefs) <- names(lm(.formula, .data[[1]])$coef)
est <- apply(coefs, 2, mean)
LCI <- apply(coefs, 2, function(z) quantile(z, alpha / 2))
UCI <- apply(coefs, 2, function(z) quantile(z, 1 - alpha / 2))
out$estimates <- data.frame(cbind(estimate = est, LCI = LCI, UCI = UCI))
out
}
# ----------------------------------------------------------------------
# Compare getting model results with only one core versus multiple cores
# ----------------------------------------------------------------------
# Get model results for all models
system.time(
boot.results <-
lapply(formula_list_char,
function(f) get_boot_results(boot.samples, f))
)
# For nBoot = 100...
# user system elapsed
# 19.934 0.139 20.083
# Define number of cores
num_cores <- 4
# Get model results for all models - repeat using parallel processing
system.time(
boot.results.mc <-
mclapply(formula_list_char,
function(f) get_boot_results(boot.samples, f),
mc.cores = num_cores)
)
# For nBoot = 100...
# user system elapsed
# 9.911 7.111 8.910
all.equal(boot.results, boot.results.mc)
# TRUE
# Test results: Multicore processing was about twice as fast as single
# core processing.
# Display structure of results list
#source("plot_lib.R")
#plot_list_structure(boot.results[1:2])
# ----------------------------------------------------------------------
# Compare two functions to convert results list into a dataframe
# ----------------------------------------------------------------------
# Convert to single dataframe with columns: model, variable, estimate, LCI, UCI
res_to_df <- function(.data) {
df <- do.call("rbind", lapply(.data, function(x) {
df <- x$estimates
df$variable <- rownames(df)
df$model <- x$formula
df[, c("model", "variable", "estimate", "LCI", "UCI")]
}))
df[order(df$model, df$variable),]
}
# Convert to single dataframe with columns: model, variable, estimate, LCI, UCI
res_to_df2 <- function(.data) {
.data %>% lapply(function(x) {
x$estimates %>%
rownames_to_column(var = "variable") %>%
mutate(model = x$formula) %>%
select(model, variable, estimate, LCI, UCI)
}) %>%
bind_rows() %>%
arrange(model, variable)
}
# Filter to only keep rows where variable contains the string "pm10"
system.time(
df_raw_filtered <-
res_to_df(boot.results) %>% filter(grepl("pm", variable))
)
# For nBoot = 100...
# user system elapsed
# 0.024 0.000 0.024
system.time(
df_raw_filtered2 <-
res_to_df2(boot.results) %>% filter(grepl("pm", variable))
)
# For nBoot = 100...
# user system elapsed
# 0.185 0.000 0.189
all.equal(df_raw_filtered, df_raw_filtered2)
# TRUE
df_raw_filtered
# Test results: The base-R version was about 8 times faster than the
# tidyverse version.
# ----------------------------------------------------------------------------
# Compare the processing time of the whole procedure, single-core vs. parallel
# ----------------------------------------------------------------------------
# Filter to only keep rows where variable contains the string "pm10"
system.time(
df_raw_filtered <-
res_to_df(lapply(formula_list_char, function(f) {
get_boot_results(boot.samples, f) })) %>%
filter(grepl("pm", variable))
)
# For nBoot = 100...
# user system elapsed
# 19.164 0.437 19.623
# Filter to only keep rows where variable contains the string "pm10"
system.time(
df_raw_filtered2 <-
res_to_df(mclapply(formula_list_char, function(f) {
get_boot_results(boot.samples, f) }, mc.cores = num_cores)) %>%
filter(grepl("pm", variable))
)
# For nBoot = 100...
# user system elapsed
# 11.699 86.454 52.250
all.equal(df_raw_filtered, df_raw_filtered2)
# TRUE
# Test results: The multicore version is about twice as slow as the
# single core version. (Note: In other tests, before we added wupport for
# weights, we observed the reverse. TODO: Improve implementation of weights.)
# TODO: Test with weights, for example...
# system.time(
# df_raw_filtered <-
# res_to_df(lapply(formula_list_char, function(f) {
# get_boot_results(boot.samples, f,
# .weights = runif(nrow(df), min = 0, max = 1)) })) %>%
# filter(grepl("pm", variable))
# )
# ---------------------------------------------------------------------------
# Compare with using purrr and broom
# ---------------------------------------------------------------------------
# Fit models and calculate confidence intervals in mean of terms of interest
boot_results_tidy <- function(.data, .formulas, .var = '', .weights = NULL,
.times = 10, .alpha = 0.05) {
.data %>% bootstraps(times = .times) %>%
mutate(results = map(splits,
~fit_with(.x, lm, .formulas, weights = .weights)),
coef = map(results, ~lapply(.x, tidy)),
model = lapply(1:.times, function(x) as.character(.formulas))) %>%
select(model, coef) %>%
unnest(c(model, coef)) %>% unnest(c(model, coef)) %>%
filter(grepl(.var, term)) %>% group_by(model, term) %>%
summarize(beta = mean(estimate),
LCI = quantile(estimate, .alpha / 2),
UCI = quantile(estimate, 1 - .alpha / 2)) %>%
rename(variable = term, estimate = beta) %>% as.data.frame()
}
# Fit models and calculate confidence intervals on mean of estimate of pm10
set.seed(12345)
system.time(df_raw_filtered3 <-
boot_results_tidy(.data = df, .formulas = formula_list,
.var = 'pm', .times = nBoot))
# For nBoot = 100...
# user system elapsed
# 62.287 1.350 63.643
# Compare with results from previous approach
all.equal(df_raw_filtered, df_raw_filtered3)
# Test results: The base-R version is about four times as fast as the
# tidyverse version.
# ---------------------------------------------------------------------------
# Compare with using purrr and broom, single-core vs. parallel
# ---------------------------------------------------------------------------
# Fit models and calculate confidence intervals in mean of terms of interest
boot_results_tidy_mc <- function(.data, .formulas, .var = '',
.weights = rep(1, nrow(.data)),
.times = 10, .alpha = 0.05, mc.cores = 1) {
.data %>% bootstraps(times = .times) %>%
mutate(results = map(splits, ~mcmapply(
lm, formula = .formulas, MoreArgs = list(data = .x, weights = .weights)),
mc.cores = mc.cores),
coef = map(results, ~mclapply(.x, tidy, mc.cores = mc.cores)),
model = lapply(1:.times, function(x) as.character(.formulas))) %>%
select(model, coef) %>%
unnest(c(model, coef)) %>% unnest(c(model, coef)) %>%
filter(grepl(.var, term)) %>% group_by(model, term) %>%
summarize(beta = mean(estimate),
LCI = quantile(estimate, .alpha / 2),
UCI = quantile(estimate, 1 - .alpha / 2)) %>%
rename(variable = term, estimate = beta) %>% as.data.frame()
}
# Fit models and calculate confidence intervals on mean of estimate of pm10
set.seed(12345)
system.time(df_raw_filtered3 <-
boot_results_tidy_mc(.data = df, .formulas = formula_list,
.var = 'pm', .times = nBoot,
mc.cores = num_cores))
# For nBoot = 100...
# user system elapsed
# 153.944 484.831 352.020
# Compare with results from previous approach
all.equal(df_raw_filtered, df_raw_filtered3)
# Test results: The single-core tidyverse version is about 2-1/2 times faster
# than the multicore tidyverse version. The multicore base-R version is almost
# 7 times faster than the multicore tidyverse version. (Note: Before we added
# support for weights, the multicore base-R version was 25 times faster than
# this version, and this version was twice as fast as it is now with support
# for weights. TODO: Improve performance when using weights.)
# Find object size of output in memory
object_size(df_raw_filtered3)