Example 2: Using the 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.

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