-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline-example.Rmd
76 lines (60 loc) · 1.95 KB
/
timeline-example.Rmd
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
---
title: "Timeline Example"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Timeline Example}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{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 time periods defined by geocoded coordinates and a "key" date. For this example workflow, we will simulate 20 random locations in Wayne County, Michigan and dates of birth during 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(dob = sample(seq(as.Date("2023-01-01"), as.Date("2023-12-31"), by = 1), size = 20))
d
```
For this example, we want to estimate the average fine particulate matter from 90 days prior to birth until the date of birth. We define these dates and create a list-col of dates for each location in our example data:
```{r}
d <- d |>
mutate(
start_date = dob - 90,
end_date = dob
) |>
rowwise() |>
mutate(dates = list(seq(start_date, end_date, by = 1))) |>
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
```
Directly use the `s2` and `dates` columns to call the `predict_pm25()` function:
```{r}
d <- d |> dplyr::mutate(pm25 = predict_pm25(s2, dates))
d
```
With daily exposures, we could average fine particulate matter throughout the study period:
```{r}
d |>
mutate(mean_pm25 = purrr::map_dbl(pm25, \(.) mean(.$pm25)))
```