Skip to contents

Imputed date ranges are calculated as the midpoints between the leading and lagging dates, except for the start of the first date range and the end of the last date range, which are used directly.

Usage

impute_date_ranges(x, start_early = 0, end_late = 0)

Arguments

x

a (chronologically sorted) Date vector

start_early

start the first imputed date range this many days early (coerced to integer)

end_late

end the last imputed date range this many days late (coerced to integer)

Value

a list of start and end Date vectors for the imputed ranges for each input date in x

Details

Use this function to impute an effective date range for a set of addresses or location identifiers that were collected on unrelated days. For example, residential addresses collected during a specific healthcare encounter do not reflect when a patient actually changed addresses. Imputing address date ranges for linking to other (spatio)temporal data ensures non-differential exposure misclassification error with respect to the changing exposures associated with each address.

Examples

impute_date_ranges(c("2024-01-01", "2024-03-17", "2024-09-21"),
  start_early = 30, end_late = 60
)
#> $start
#> [1] "2023-12-02" "2024-02-08" "2024-06-19"
#> 
#> $end
#> [1] "2024-02-08" "2024-06-19" "2024-11-20"
#> 

# use within a data.frame with multiple individuals
tibble::tribble(
  ~id, ~encounter, ~date,
  "A", 1, "2024-01-01",
  "A", 2, "2024-03-17",
  "A", 3, "2024-09-21",
  "B", 1, "2023-11-29",
  "B", 2, "2024-09-22",
  "B", 3, "2024-09-29"
) |>
  dplyr::mutate(
    imputed_start_date = impute_date_ranges(date)$start,
    imputed_end_date = impute_date_ranges(date)$end,
    .by = "id"
  )
#> # A tibble: 6 × 5
#>   id    encounter date       imputed_start_date imputed_end_date
#>   <chr>     <dbl> <chr>      <date>             <date>          
#> 1 A             1 2024-01-01 2024-01-01         2024-02-08      
#> 2 A             2 2024-03-17 2024-02-08         2024-06-19      
#> 3 A             3 2024-09-21 2024-06-19         2024-09-21      
#> 4 B             1 2023-11-29 2023-11-29         2024-04-26      
#> 5 B             2 2024-09-22 2024-04-26         2024-09-25      
#> 6 B             3 2024-09-29 2024-09-25         2024-09-29