calculate_airPb()
and
calculate_scaling_factors()
and manually applying scaling
factors to air lead estimates.d <- tibble::tribble(
~id, ~lon, ~lat,
809089L, -84.69127387, 39.24710734,
813233L, -84.47798287, 39.12005904,
814881L, -84.47123583, 39.2631309,
799697L, -84.41741798, 39.18541228,
799698L, -84.41395064, 39.18322447
)
my_dates <- data.frame(start_date = as.Date(c("2010-01-08", "2012-06-08", "2010-01-09", "2015-04-09", "2010-01-10")),
end_date = as.Date(c("2010-02-08", "2012-07-08", "2010-02-09", "2015-05-09", "2010-02-10")))
d %>%
mutate(airPb = calculate_airPb(. , return.LU.vars = FALSE),
scaling_factors = calculate_scaling_factors(my_dates),
scaled_airPb = airPb * scaling_factors)
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Warning: There were 2 warnings in `mutate()`.
#> The first warning was:
#> ℹ In argument: `airPb = calculate_airPb(., return.LU.vars = FALSE)`.
#> Caused by warning in `.local()`:
#> ! Transforming SpatialPoints to the crs of the Raster
#> ℹ Run `dplyr::last_dplyr_warnings()` to see the 1 remaining warning.
#> # A tibble: 5 × 6
#> id lon lat airPb scaling_factors scaled_airPb
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 809089 -84.7 39.2 1.29 0.588 0.757
#> 2 813233 -84.5 39.1 1.14 0.187 0.213
#> 3 814881 -84.5 39.3 0.934 0.441 0.412
#> 4 799697 -84.4 39.2 1.03 0.401 0.415
#> 5 799698 -84.4 39.2 1.05 0.441 0.463
add_scaled_airPb()
wrapper
function to automatically apply scaling factors to air lead
estimates.A common use case is calculating monthly exposures. For example, we may have a pair of coordinates recorded annually for each participant. In the data below, we have 2 unique ids, with lat/lon recorded once per year.
d <- tibble::tribble(
~id, ~lon, ~lat, ~date,
809089L, -84.69127387, 39.24710734, as.Date("2010-01-08"),
809089L, -84.69127387, 39.24710734, as.Date("2011-01-08"),
809089L, -84.69127387, 39.24710734, as.Date("2012-01-08"),
799697L, -84.41741798, 39.18541228, as.Date("2011-01-10"),
799697L, -84.41741798, 39.18541228, as.Date("2012-02-10")
)
We want to scale the air lead measurements to monthly exposures
between these dates, but we need start_date
and
end_date
columns that represent the monthy time periods we
want to average over.
d <- d %>%
mutate(from = date,
to = from + lubridate::years(1)) %>%
group_by(id, date) %>%
nest() %>%
mutate(dates = map(data, ~seq.Date(from = .x$from,
to = .x$to,
by = '3 months'))) %>%
unnest(cols=c('data', 'dates')) %>%
dplyr::select(-from, -to) %>%
rename(start_date = dates) %>%
mutate(end_date = lead(start_date)) %>%
filter(!is.na(end_date)) %>%
ungroup()
d %>%
mutate(scaled_airPb = add_scaled_airPb(.))
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> old-style crs object detected; please recreate object with a recent sf::st_crs()
#> Warning: There was 1 warning in `mutate()`.
#> ℹ In argument: `scaled_airPb = add_scaled_airPb(.)`.
#> Caused by warning in `.local()`:
#> ! Transforming SpatialPoints to the crs of the Raster
#> # A tibble: 20 × 7
#> id date lon lat start_date end_date scaled_airPb
#> <int> <date> <dbl> <dbl> <date> <date> <dbl>
#> 1 809089 2010-01-08 -84.7 39.2 2010-01-08 2010-04-08 0.549
#> 2 809089 2010-01-08 -84.7 39.2 2010-04-08 2010-07-08 0.535
#> 3 809089 2010-01-08 -84.7 39.2 2010-07-08 2010-10-08 1.07
#> 4 809089 2010-01-08 -84.7 39.2 2010-10-08 2011-01-08 0.765
#> 5 809089 2011-01-08 -84.7 39.2 2011-01-08 2011-04-08 0.380
#> 6 809089 2011-01-08 -84.7 39.2 2011-04-08 2011-07-08 0.495
#> 7 809089 2011-01-08 -84.7 39.2 2011-07-08 2011-10-08 0.612
#> 8 809089 2011-01-08 -84.7 39.2 2011-10-08 2012-01-08 0.441
#> 9 809089 2012-01-08 -84.7 39.2 2012-01-08 2012-04-08 0.538
#> 10 809089 2012-01-08 -84.7 39.2 2012-04-08 2012-07-08 0.389
#> 11 809089 2012-01-08 -84.7 39.2 2012-07-08 2012-10-08 0.263
#> 12 809089 2012-01-08 -84.7 39.2 2012-10-08 2013-01-08 0.567
#> 13 799697 2011-01-10 -84.4 39.2 2011-01-10 2011-04-10 0.292
#> 14 799697 2011-01-10 -84.4 39.2 2011-04-10 2011-07-10 0.405
#> 15 799697 2011-01-10 -84.4 39.2 2011-07-10 2011-10-10 0.536
#> 16 799697 2011-01-10 -84.4 39.2 2011-10-10 2012-01-10 0.317
#> 17 799697 2012-02-10 -84.4 39.2 2012-02-10 2012-05-10 0.361
#> 18 799697 2012-02-10 -84.4 39.2 2012-05-10 2012-08-10 0.178
#> 19 799697 2012-02-10 -84.4 39.2 2012-08-10 2012-11-10 0.260
#> 20 799697 2012-02-10 -84.4 39.2 2012-11-10 2013-02-10 0.691