/
refresh_covid19mobility_google.R
343 lines (317 loc) · 11.8 KB
/
refresh_covid19mobility_google.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
#' Get Google Mobility Data at the Country Level
#'
#' @description From Google:
#' "Each Community Mobility Report dataset is presented by location
#' and highlights the percent change in visits to places like grocery
#' stores and parks within a geographic area.
#'
#' Location accuracy and the understanding of categorized places
#' varies from region to region, so we don’t recommend using this
#' data to compare changes between countries, or between regions
#' with different characteristics (e.g. rural versus urban areas).
#'
#' Changes for each day are compared to a baseline value for that
#' day of the week: The baseline is the median value, for the corresponding day
#' of the week, during the 5-week period Jan 3–Feb 6, 2020.
#' The datasets show trends over several months with the most
#' recent data representing approximately 2-3 days ago—this is how
#' long it takes to produce the datasets."
#'
#' Data represents changes from baseline visits for the following types
#' of locations visited:
#'
#' * retail and recreation
#' * grocery and pharmacy
#' * parks
#' * transit stations
#' * workplaces
#' * residential
#'
#' @return A tibble meeting the Covid19R Project data standard. Columns include:
#'
#' * date - The date in YYYY-MM-DD form
#' * location - The name of the location as provided by the data source.
#' * location_type - The type of location using the covid19R controlled vocabulary.
#' * location_code - A standardized location code using a national or international standard. In this case, FIPS state or county codes. See https://en.wikipedia.org/wiki/Federal_Information_Processing_Standard_state_code and https://en.wikipedia.org/wiki/FIPS_county_code for more
#' * location_code_type The type of standardized location code being used according to the covid19R controlled vocabulary. Here we use `iso_3166_2`
#' * data_type - the type of data in that given row. See description.
#' * value - number of cases of each data type
#'
#' @export
#' @references Google Covid-19 Mobility Reports \url{https://www.google.com/covid19/mobility/}
#' @references The Covid19R Project \url{https://covid19r.github.io/documentation/}
#'
#' @examples
#' \donttest{
#' covid19mobility_google_country <- refresh_covid19mobility_google_country()
#'
#' head(covid19mobility_google_country)
#' }
#'
refresh_covid19mobility_google_country <- function() {
# read in
gmob <- read_google_mobility() %>%
# filter to nations only
dplyr::filter(is.na(sub_region_1))
gmob %>%
dplyr::select(-sub_region_1, sub_region_2) %>%
dplyr::rename(location = country_region) %>%
dplyr::mutate(
location_code_type = "iso_3166_2",
location_type = "country"
) %>%
covid_19_r_format() %>%
dplyr::mutate(location_code = ifelse(location == "Namibia", "NA", location_code)) # agh!
}
#' Get Google Mobility Data at the State of Subdivision Level
#'
#' @description From Google:
#' "Each Community Mobility Report dataset is presented by location
#' and highlights the percent change in visits to places like grocery
#' stores and parks within a geographic area.
#'
#' Location accuracy and the understanding of categorized places
#' varies from region to region, so we don’t recommend using this
#' data to compare changes between countries, or between regions
#' with different characteristics (e.g. rural versus urban areas).
#'
#' Changes for each day are compared to a baseline value for that
#' day of the week: The baseline is the median value, for the corresponding day
#' of the week, during the 5-week period Jan 3–Feb 6, 2020.
#' The datasets show trends over several months with the most
#' recent data representing approximately 2-3 days ago—this is how
#' long it takes to produce the datasets."
#'
#' Data represents changes from baseline visits for the following types
#' of locations visited:
#'
#' * retail and recreation
#' * grocery and pharmacy
#' * parks
#' * transit stations
#' * workplaces
#' * residential
#'
#' @return A tibble meeting the Covid19R Project data standard. Columns include:
#'
#' * date - The date in YYYY-MM-DD form
#' * location - The name of the location as provided by the data source.
#' * location_type - The type of location using the covid19R controlled vocabulary.
#' * location_code - A standardized location code using a national or international standard. In this case, FIPS state or county codes. See https://en.wikipedia.org/wiki/Federal_Information_Processing_Standard_state_code and https://en.wikipedia.org/wiki/FIPS_county_code for more
#' * location_code_type The type of standardized location code being used according to the covid19R controlled vocabulary. Here we use `iso_3166_2`
#' * data_type - the type of data in that given row. See description.
#' * value - number of cases of each data type
#'
#' @export
#' @references Google Covid-19 Mobility Reports \url{https://www.google.com/covid19/mobility/}
#' @references The Covid19R Project \url{https://covid19r.github.io/documentation/}
#'
#' @examples
#' \donttest{
#' covid19mobility_google_subregions <- refresh_covid19mobility_google_subregions()
#'
#' head(covid19mobility_google_subregions)
#' }
#'
refresh_covid19mobility_google_subregions <- function() {
# read in
gmob <- read_google_mobility() %>%
# filter to subregions
dplyr::filter(
!is.na(sub_region_1),
is.na(sub_region_2)
)
# put puerto rico in properly
pr <- gmob %>%
# filter to subregions
dplyr::filter(country_region == "Puerto Rico") %>%
dplyr::select(-location_code) %>%
dplyr::mutate(
sub_region_2 = sub_region_1,
sub_region_1 = "Puerto Rico",
country_region = "United States"
) %>%
dplyr::filter(is.na(sub_region_2))
gmob <- dplyr::bind_rows(
gmob %>% dplyr::filter(country_region != "Puerto Rico"),
pr
)
# make a clean subregion_1
gmob <- dplyr::left_join(
gmob,
goog_subdivision_lut(gmob)
)
# subregion_codes
subdivision_codes <- load_subdivisions() %>%
dplyr::mutate(
subdivision_name = gsub("^Al ", "", subdivision_name),
subdivision_name = gsub("s lan$", "", subdivision_name),
subdivision_name = tolower(subdivision_name)
) %>%
dplyr::group_by(code, country_code, subdivision_name) %>%
dplyr::slice(1L) %>%
dplyr::ungroup()
gmob_joined <- dplyr::left_join(gmob,
subdivision_codes,
by = c(
"location_code" = "country_code",
"subregion_1_clean" = "subdivision_name"
)
)
# return
gmob_joined <- gmob_joined %>%
dplyr::rename(
country_code = location_code,
location = sub_region_1,
location_code = code
) %>%
dplyr::mutate(
location_type = "state",
location_code_type = "iso_3166_2"
)
gmob_joined %>% covid_19_r_format()
}
#' Get Google Mobility Data for US States
#'
#' @description From Google:
#' "Each Community Mobility Report dataset is presented by location
#' and highlights the percent change in visits to places like grocery
#' stores and parks within a geographic area.
#'
#' Location accuracy and the understanding of categorized places
#' varies from region to region, so we don’t recommend using this
#' data to compare changes between countries, or between regions
#' with different characteristics (e.g. rural versus urban areas).
#'
#' Changes for each day are compared to a baseline value for that
#' day of the week: The baseline is the median value, for the corresponding day
#' of the week, during the 5-week period Jan 3–Feb 6, 2020.
#' The datasets show trends over several months with the most
#' recent data representing approximately 2-3 days ago—this is how
#' long it takes to produce the datasets."
#'
#' Data represents changes from baseline visits for the following types
#' of locations visited:
#'
#' * retail and recreation
#' * grocery and pharmacy
#' * parks
#' * transit stations
#' * workplaces
#' * residential
#'
#' @return A tibble meeting the Covid19R Project data standard. Columns include:
#'
#' * date - The date in YYYY-MM-DD form
#' * location - The name of the location as provided by the data source.
#' * location_type - The type of location using the covid19R controlled vocabulary.
#' * location_code - A standardized location code using a national or international standard. In this case, FIPS state or county codes. See https://en.wikipedia.org/wiki/Federal_Information_Processing_Standard_state_code and https://en.wikipedia.org/wiki/FIPS_county_code for more
#' * location_code_type The type of standardized location code being used according to the covid19R controlled vocabulary. Here we use `iso_3166_2`
#' * data_type - the type of data in that given row. See description.
#' * value - number of cases of each data type
#'
#' @export
#' @references Google Covid-19 Mobility Reports \url{https://www.google.com/covid19/mobility/}
#' @references The Covid19R Project \url{https://covid19r.github.io/documentation/}
#'
#' @examples
#' \donttest{
#' covid19mobility_google_us_counties <- refresh_covid19mobility_google_us_counties()
#'
#' head(covid19mobility_google_us_counties)
#' }
#'
refresh_covid19mobility_google_us_counties <- function() {
# read in
gmob <- read_google_mobility() %>%
# filter to nations only
dplyr::filter(
!is.na(sub_region_1),
!is.na(sub_region_2)
) %>%
dplyr::select(-location_code)
# get puerto rico
pr <- read_google_mobility() %>%
# filter to subregions
dplyr::filter(country_region == "Puerto Rico") %>%
dplyr::select(-location_code) %>%
dplyr::mutate(
sub_region_2 = sub_region_1,
sub_region_1 = "Puerto Rico",
country_region = "United States"
) %>%
dplyr::filter(!is.na(sub_region_2))
# combine and make a clean column
gmob <- dplyr::bind_rows(gmob, pr)
# make a counties lookup table
suppressMessages(
counties_lut <- goog_counties_lut(gmob, load_fips())
)
# combine state/county
suppressMessages(
gmob_joined <- dplyr::left_join(
gmob,
counties_lut
)
)
# reshape and return
gmob_joined %>%
dplyr::rename(
state = sub_region_1,
location = sub_region_2
) %>%
dplyr::mutate(
location_type = "county",
location_code_type = "fips_code"
) %>%
covid_19_r_format()
}
read_google_mobility <- function() {
url <- "https://www.gstatic.com/covid19/mobility/Global_Mobility_Report.csv"
gmob <- readr::read_csv(url,
na = c("", "N/A"), # col_types = "ccccDiiiiii")
col_types = readr::cols(
country_region_code = readr::col_character(),
country_region = readr::col_character(),
sub_region_1 = readr::col_character(),
sub_region_2 = readr::col_character(),
iso_3166_2_code = readr::col_character(),
census_fips_code = readr::col_character(),
date = readr::col_date(),
retail_and_recreation_percent_change_from_baseline = readr::col_integer(),
grocery_and_pharmacy_percent_change_from_baseline = readr::col_integer(),
parks_percent_change_from_baseline = readr::col_integer(),
transit_stations_percent_change_from_baseline = readr::col_integer(),
workplaces_percent_change_from_baseline = readr::col_integer(),
residential_percent_change_from_baseline = readr::col_integer()
)
)
gmob_longer <- gmob %>%
dplyr::rename(location_code = country_region_code) %>%
tidyr::pivot_longer(
cols = retail_and_recreation_percent_change_from_baseline:residential_percent_change_from_baseline,
names_to = "data_type",
values_to = "value"
)
gmob_longer
}
covid_19_r_format <- . %>%
janitor::clean_names() %>%
dplyr::select(
any_of(
c(
"date",
"location",
"location_type",
"location_code",
"location_code_type",
"state",
"data_type",
"value"
)
)
) %>%
dplyr::mutate(
data_type = gsub("_from_baseline", "", data_type),
data_type = gsub("_percent_change", "_perc_ch", data_type)
)