library(ecat)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(tidyr)
calculate_ecat()
and calculate_scaling_factors()
and manually applying scaling factors to ECAT 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(unscaled_ecat = calculate_ecat(. , return.LU.vars = FALSE),
scaling_factors = calculate_scaling_factors(my_dates),
scaled_ecat = unscaled_ecat * scaling_factors)
#> Loading required package: raster
#> Loading required package: sp
#>
#> Attaching package: 'raster'
#> The following object is masked from 'package:tidyr':
#>
#> extract
#> The following object is masked from 'package:dplyr':
#>
#> select
#> Warning in get_elevation(locations): The `raster` package has been attached
#> to the global environment, masking dplyr::select()
#> # A tibble: 5 x 6
#> id lon lat unscaled_ecat scaling_factors scaled_ecat
#> <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 809089 -84.7 39.2 0.302 0.989 0.299
#> 2 813233 -84.5 39.1 0.740 1.05 0.779
#> 3 814881 -84.5 39.3 0.479 0.945 0.452
#> 4 799697 -84.4 39.2 0.355 1.09 0.388
#> 5 799698 -84.4 39.2 0.339 0.945 0.320
add_scaled_ecat()
wrapper function to automatically apply scaling factors to ECAT 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 ecat measurements to monthly exposures between these dates, but we need start_date
and end_date
columns that represent the monthly time periods we want to average over.
d <- d %>%
mutate(from = date,
to = from + lubridate::years(1)) %>%
group_by(id, date) %>%
nest() %>%
mutate(dates = purrr::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()
The entire tibble with columns called id
, lon
, lat
, start_date
, and end_date
are passed as the only argument to the add_scaled_ecat
function.
d %>%
mutate(scaled_ecat = add_scaled_ecat(.))
#> Warning in get_elevation(locations): The `raster` package has been attached
#> to the global environment, masking dplyr::select()
#> # A tibble: 20 x 7
#> id date lon lat start_date end_date scaled_ecat
#> <int> <date> <dbl> <dbl> <date> <date> <dbl>
#> 1 809089 2010-01-08 -84.7 39.2 2010-01-08 2010-04-08 0.303
#> 2 809089 2010-01-08 -84.7 39.2 2010-04-08 2010-07-08 0.297
#> 3 809089 2010-01-08 -84.7 39.2 2010-07-08 2010-10-08 0.363
#> 4 809089 2010-01-08 -84.7 39.2 2010-10-08 2011-01-08 0.405
#> 5 809089 2011-01-08 -84.7 39.2 2011-01-08 2011-04-08 0.226
#> 6 809089 2011-01-08 -84.7 39.2 2011-04-08 2011-07-08 0.319
#> 7 809089 2011-01-08 -84.7 39.2 2011-07-08 2011-10-08 0.347
#> 8 809089 2011-01-08 -84.7 39.2 2011-10-08 2012-01-08 0.315
#> 9 809089 2012-01-08 -84.7 39.2 2012-01-08 2012-04-08 0.298
#> 10 809089 2012-01-08 -84.7 39.2 2012-04-08 2012-07-08 0.322
#> 11 809089 2012-01-08 -84.7 39.2 2012-07-08 2012-10-08 0.306
#> 12 809089 2012-01-08 -84.7 39.2 2012-10-08 2013-01-08 0.331
#> 13 799697 2011-01-10 -84.4 39.2 2011-01-10 2011-04-10 0.274
#> 14 799697 2011-01-10 -84.4 39.2 2011-04-10 2011-07-10 0.371
#> 15 799697 2011-01-10 -84.4 39.2 2011-07-10 2011-10-10 0.411
#> 16 799697 2011-01-10 -84.4 39.2 2011-10-10 2012-01-10 0.374
#> 17 799697 2012-02-10 -84.4 39.2 2012-02-10 2012-05-10 0.355
#> 18 799697 2012-02-10 -84.4 39.2 2012-05-10 2012-08-10 0.361
#> 19 799697 2012-02-10 -84.4 39.2 2012-08-10 2012-11-10 0.357
#> 20 799697 2012-02-10 -84.4 39.2 2012-11-10 2013-02-10 0.373