forked from ashattock/epi50
-
Notifications
You must be signed in to change notification settings - Fork 4
/
covariates.R
199 lines (161 loc) · 5.81 KB
/
covariates.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
###########################################################
# COVARIATES
#
# Prepare covariates from various sources for use in regression
# modelling. That is, for geographical imputation, and also
# for inferring drivers of impact.
#
###########################################################
# ---------------------------------------------------------
# Parent function for preparing all covariate data
# ---------------------------------------------------------
prepare_covariates = function() {
message(" > Regression covariates")
# Download Gapminder data (if necessary)
download_gapminder()
# Format all covariates from the various sources
c1 = covariates_gapminder()
c2 = covariates_unicef()
# Concatenate and interpolate
covariates_dt = c(c1, c2) %>%
# Expand to temporal scope and interpolate trends...
interpolate_covariates() %>%
rbindlist() %>%
# Tidy up...
select(metric, country, year, value) %>%
arrange(metric, country, year)
# Save in tables cache
save_table(covariates_dt, "regression_covariates")
}
# ---------------------------------------------------------
# Download all Gapminder data from github
# ---------------------------------------------------------
download_gapminder = function() {
# Details of Gapminder data we're interested in
gapminder_dict = table("gapminder_dict")
# Destination of downloaded files - may or may not exist
files = paste0(o$pth$gapminder, gapminder_dict$var, ".rds")
# Skip process if all files have already been downloaded
if (all(file.exists(files)) && !o$force_download_gapminder)
return()
# ---- Download all files ----
message(" - Downloading data")
# Construct base URL for Gapminder github data files
gapminder_url = paste0(
"https://raw.githubusercontent.com/open-numbers/",
"ddf--gapminder--systema_globalis/",
"master/countries-etc-datapoints/",
"ddf--datapoints--[name]--by--geo--time.csv")
# Iterate through metrics of interest
for (i in seq_row(gapminder_dict)) {
# File name (Gapminder convention)
data_var = gapminder_dict$file[i]
# Adapt URL for this specific metric
data_url = str_replace(
string = gapminder_url,
pattern = "\\[name\\]",
replacement = data_var)
# Load the data and briefly format
data = read_csv(data_url, show_col_types = FALSE) %>%
rename(value = !!data_var) %>%
mutate(metric = data_var) %>%
as.data.table()
# Save rds file locally for easy re-loading
saveRDS(data, file = files[i])
}
}
# ---------------------------------------------------------
# Prepare covariates from Gapminder
# ---------------------------------------------------------
covariates_gapminder = function() {
# Gapminder dictionary
gapminder_dict = table("gapminder_dict")
# Convert to named vector
vars = setNames(
gapminder_dict$var,
gapminder_dict$file)
# All previously downloaded gapminder files
files = paste0(o$pth$gapminder, vars, ".rds")
# Load and format gapminder data
covariates = lapply(files, readRDS) %>%
rbindlist() %>%
# Recode variables...
mutate(metric = recode(metric, !!!vars)) %>%
# Recode countries
mutate(country = toupper(geo)) %>%
filter(country %in% all_countries()) %>%
# Data ten years prior EPI to capture historical effect...
mutate(year = as.integer(time)) %>%
filter(year >= min(o$years) - 10,
year <= max(o$years)) %>%
# Tidy up...
select(country, year, value, metric) %>%
split(f = .$metric)
return(covariates)
}
# ---------------------------------------------------------
# Prepare covariates from UNICEF
# ---------------------------------------------------------
covariates_unicef = function() {
# Initiate covariates list
covariates = list()
# ---- Stunting ----
# Read in UNICEF stunting data
covariates$stunting =
fread(paste0(o$pth$input, "unicef_stunting.csv")) %>%
# Format column names...
setnames(names(.), tolower(names(.))) %>%
rename(country = "iso code") %>%
# Reduce down to values of interest...
filter(country %in% all_countries(),
indicator == "Stunting",
estimate == "Point Estimate") %>%
select(country, matches("^[0-9]+")) %>%
# Melt to long format...
pivot_longer(cols = -country,
names_to = "year") %>%
# Remove NAs...
replace_with_na(list(value = "-")) %>%
filter(!is.na(value)) %>%
# Format values...
mutate(value = as.numeric(value),
year = as.integer(year),
metric = "stunting") %>%
as.data.table()
return(covariates)
}
# ---------------------------------------------------------
# Interpolate timeseries trends for all metrics
# ---------------------------------------------------------
interpolate_covariates = function(covariates) {
message(" - Interpolating timeseries trends")
# Function to perform interpolation
interpolate_fn = function(data) {
# Expand years to data limit and interpolate trends
interp_data = data %>%
# Normalise...
mutate(value = value / max(value)) %>%
# Expand to complete temporal scope...
complete(country, year = min(year) : max(o$years)) %>%
# Interpolate timeseries trends...
as_tsibble(index = year, key = country) %>%
interp_ts_trend() %>%
# Tidy up...
mutate(metric = unique(data$metric)) %>%
as.data.table()
return(interp_data)
}
# Interpolate metrics in parallel
if (o$parallel$interp)
interp_list = mclapply(
X = covariates,
FUN = interpolate_fn,
mc.cores = o$n_cores,
mc.preschedule = FALSE)
# Interpolate metrics consecutively
if (!o$parallel$interp)
interp_list = lapply(
X = covariates,
FUN = interpolate_fn)
return(interp_list)
}