-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
case-crossover example vignette (#71)
Co-authored-by: erikarasnick <[email protected]>
- Loading branch information
1 parent
6b455c1
commit 73c738c
Showing
4 changed files
with
88 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ Version: 0.2.0 | |
Authors@R: c( | ||
person("Cole", "Brokamp", , "[email protected]", role = c("aut", "cre"), | ||
comment = c(ORCID = "0000-0002-0289-3151")), | ||
person("Erika", "Manning", role = "aut"), | ||
person("Qing", "Duan", role = "aut") | ||
) | ||
URL: https://github.com/geomarker-io/appc, http://geomarker.io/appc/ | ||
|
@@ -35,6 +36,7 @@ Suggests: | |
ggplot2, | ||
hexbin, | ||
knitr, | ||
lubridate, | ||
lwgeom, | ||
mappp, | ||
readr, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: "Case-Crossover Example" | ||
output: rmarkdown::html_vignette | ||
vignette: > | ||
%\VignetteIndexEntry{Case-Crossover Example} | ||
%\VignetteEncoding{UTF-8} | ||
%\VignetteEngine{knitr::rmarkdown} | ||
editor_options: | ||
chunk_output_type: console | ||
--- | ||
|
||
```{r, include = FALSE} | ||
knitr::opts_chunk$set( | ||
collapse = TRUE, | ||
comment = "#>" | ||
) | ||
``` | ||
|
||
```{r setup} | ||
library(appc) | ||
library(dplyr, warn.conflicts = FALSE) | ||
``` | ||
|
||
This example details how to use the appc package to add air pollution exposure estimates for exact locations and dates defined by geocoded coordinates and a case date. For this example workflow, we will simulate 20 random locations in Wayne County, Michigan and case dates between 2019 and 2022, but in actuality this can be any set of geocoded `lat` and `lon` columns with corresponding dates. | ||
|
||
```{r} | ||
#| warnings: false | ||
#| messages: false | ||
d <- | ||
tigris::counties("MI", year = 2021, progress_bar = FALSE) |> | ||
suppressWarnings() |> | ||
filter(GEOID == 26163) |> | ||
sf::st_sample(20) |> | ||
sf::st_coordinates() |> | ||
tibble::as_tibble() |> | ||
rename(lat = Y, lon = X) |> | ||
mutate(case_date = sample(seq(as.Date("2019-01-01"), as.Date("2022-12-31"), by = 1), size = 20)) |> | ||
mutate(id = 1:20) |> | ||
relocate(id) | ||
d | ||
``` | ||
|
||
For this example, we want to estimate the fine particulate matter on the days of the case, as well as control dates. Here, we define these control dates using time stratification on year, month, and day-of-week, and use the `purrr` package to create a list-col of dates for each location in our example data: | ||
|
||
```{r} | ||
dates_seq <- seq(min(d$case_date) - 31, max(d$case_date) + 31, by = 1) | ||
make_control_dates <- function(case_date) { | ||
dates_seq[lubridate::year(dates_seq) == lubridate::year(case_date) & | ||
lubridate::month(dates_seq) == lubridate::month(case_date) & | ||
lubridate::wday(dates_seq) == lubridate::wday(case_date) & | ||
dates_seq != case_date] | ||
} | ||
d <- d |> | ||
rowwise() |> | ||
mutate(dates = purrr::map(case_date, make_control_dates)) |> | ||
ungroup() | ||
d | ||
``` | ||
|
||
Next, we will use the `lon` and `lat` columns to create the s2 geohash: | ||
|
||
```{r} | ||
d <- d |> dplyr::mutate(s2 = s2::as_s2_cell(s2::s2_geog_point(lon, lat))) | ||
d | ||
``` | ||
|
||
Then we can directly use the `s2` and `dates` columns to add temperature and humidity using the `get_narr_data()` function and PM2.5 using the `predict_pm25()` function: | ||
|
||
```{r} | ||
d <- d |> dplyr::mutate(temperature = get_narr_data(s2, dates, "air.2m"), | ||
humidity = get_narr_data(s2, dates, "rhum.2m")) | ||
d <- d |> dplyr::mutate(pm25 = predict_pm25(s2, dates)) | ||
d | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters