Skip to contents

In observational data, the date associated with an address is often the date of the observation instead of the date of a change in address. For each interior observation, the imputed start date is the midpoint between that observation date and the previous observation date. The imputed end date is the next imputed start date. The first start date and last end date are optionally extended by start_early and end_late.

Usage

impute_date_ranges(x, start_early = 0L, end_late = 0L, expand = FALSE)

Arguments

x

a chronologically sorted Date vector

start_early

start the first imputed date range this many days early as a length-one integer

end_late

end the last imputed date range this many days late as a length-one integer

expand

logical; if TRUE, return a list of daily Date sequences, one for each input date, instead of the default start and end vectors.

Value

a list of start and end Date vectors for the imputed ranges for each input date in x, or, if expand = TRUE, a list of Date vectors where each element is the daily sequence from the imputed start date to the imputed end date.

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 can reduce reliance on observation dates alone and may help avoid differential assumptions about when address changes occurred.

Examples

impute_date_ranges(as.Date(c("2024-01-01", "2024-03-17", "2024-09-21")))
#> $start
#> [1] "2024-01-01" "2024-02-08" "2024-06-19"
#> 
#> $end
#> [1] "2024-02-08" "2024-06-19" "2024-09-21"
#> 

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

impute_date_ranges(
  as.Date(c("2024-01-01", "2024-03-17", "2024-09-21")),
  expand = TRUE
)
#> [[1]]
#>  [1] "2024-01-01" "2024-01-02" "2024-01-03" "2024-01-04" "2024-01-05"
#>  [6] "2024-01-06" "2024-01-07" "2024-01-08" "2024-01-09" "2024-01-10"
#> [11] "2024-01-11" "2024-01-12" "2024-01-13" "2024-01-14" "2024-01-15"
#> [16] "2024-01-16" "2024-01-17" "2024-01-18" "2024-01-19" "2024-01-20"
#> [21] "2024-01-21" "2024-01-22" "2024-01-23" "2024-01-24" "2024-01-25"
#> [26] "2024-01-26" "2024-01-27" "2024-01-28" "2024-01-29" "2024-01-30"
#> [31] "2024-01-31" "2024-02-01" "2024-02-02" "2024-02-03" "2024-02-04"
#> [36] "2024-02-05" "2024-02-06" "2024-02-07" "2024-02-08"
#> 
#> [[2]]
#>   [1] "2024-02-08" "2024-02-09" "2024-02-10" "2024-02-11" "2024-02-12"
#>   [6] "2024-02-13" "2024-02-14" "2024-02-15" "2024-02-16" "2024-02-17"
#>  [11] "2024-02-18" "2024-02-19" "2024-02-20" "2024-02-21" "2024-02-22"
#>  [16] "2024-02-23" "2024-02-24" "2024-02-25" "2024-02-26" "2024-02-27"
#>  [21] "2024-02-28" "2024-02-29" "2024-03-01" "2024-03-02" "2024-03-03"
#>  [26] "2024-03-04" "2024-03-05" "2024-03-06" "2024-03-07" "2024-03-08"
#>  [31] "2024-03-09" "2024-03-10" "2024-03-11" "2024-03-12" "2024-03-13"
#>  [36] "2024-03-14" "2024-03-15" "2024-03-16" "2024-03-17" "2024-03-18"
#>  [41] "2024-03-19" "2024-03-20" "2024-03-21" "2024-03-22" "2024-03-23"
#>  [46] "2024-03-24" "2024-03-25" "2024-03-26" "2024-03-27" "2024-03-28"
#>  [51] "2024-03-29" "2024-03-30" "2024-03-31" "2024-04-01" "2024-04-02"
#>  [56] "2024-04-03" "2024-04-04" "2024-04-05" "2024-04-06" "2024-04-07"
#>  [61] "2024-04-08" "2024-04-09" "2024-04-10" "2024-04-11" "2024-04-12"
#>  [66] "2024-04-13" "2024-04-14" "2024-04-15" "2024-04-16" "2024-04-17"
#>  [71] "2024-04-18" "2024-04-19" "2024-04-20" "2024-04-21" "2024-04-22"
#>  [76] "2024-04-23" "2024-04-24" "2024-04-25" "2024-04-26" "2024-04-27"
#>  [81] "2024-04-28" "2024-04-29" "2024-04-30" "2024-05-01" "2024-05-02"
#>  [86] "2024-05-03" "2024-05-04" "2024-05-05" "2024-05-06" "2024-05-07"
#>  [91] "2024-05-08" "2024-05-09" "2024-05-10" "2024-05-11" "2024-05-12"
#>  [96] "2024-05-13" "2024-05-14" "2024-05-15" "2024-05-16" "2024-05-17"
#> [101] "2024-05-18" "2024-05-19" "2024-05-20" "2024-05-21" "2024-05-22"
#> [106] "2024-05-23" "2024-05-24" "2024-05-25" "2024-05-26" "2024-05-27"
#> [111] "2024-05-28" "2024-05-29" "2024-05-30" "2024-05-31" "2024-06-01"
#> [116] "2024-06-02" "2024-06-03" "2024-06-04" "2024-06-05" "2024-06-06"
#> [121] "2024-06-07" "2024-06-08" "2024-06-09" "2024-06-10" "2024-06-11"
#> [126] "2024-06-12" "2024-06-13" "2024-06-14" "2024-06-15" "2024-06-16"
#> [131] "2024-06-17" "2024-06-18" "2024-06-19"
#> 
#> [[3]]
#>  [1] "2024-06-19" "2024-06-20" "2024-06-21" "2024-06-22" "2024-06-23"
#>  [6] "2024-06-24" "2024-06-25" "2024-06-26" "2024-06-27" "2024-06-28"
#> [11] "2024-06-29" "2024-06-30" "2024-07-01" "2024-07-02" "2024-07-03"
#> [16] "2024-07-04" "2024-07-05" "2024-07-06" "2024-07-07" "2024-07-08"
#> [21] "2024-07-09" "2024-07-10" "2024-07-11" "2024-07-12" "2024-07-13"
#> [26] "2024-07-14" "2024-07-15" "2024-07-16" "2024-07-17" "2024-07-18"
#> [31] "2024-07-19" "2024-07-20" "2024-07-21" "2024-07-22" "2024-07-23"
#> [36] "2024-07-24" "2024-07-25" "2024-07-26" "2024-07-27" "2024-07-28"
#> [41] "2024-07-29" "2024-07-30" "2024-07-31" "2024-08-01" "2024-08-02"
#> [46] "2024-08-03" "2024-08-04" "2024-08-05" "2024-08-06" "2024-08-07"
#> [51] "2024-08-08" "2024-08-09" "2024-08-10" "2024-08-11" "2024-08-12"
#> [56] "2024-08-13" "2024-08-14" "2024-08-15" "2024-08-16" "2024-08-17"
#> [61] "2024-08-18" "2024-08-19" "2024-08-20" "2024-08-21" "2024-08-22"
#> [66] "2024-08-23" "2024-08-24" "2024-08-25" "2024-08-26" "2024-08-27"
#> [71] "2024-08-28" "2024-08-29" "2024-08-30" "2024-08-31" "2024-09-01"
#> [76] "2024-09-02" "2024-09-03" "2024-09-04" "2024-09-05" "2024-09-06"
#> [81] "2024-09-07" "2024-09-08" "2024-09-09" "2024-09-10" "2024-09-11"
#> [86] "2024-09-12" "2024-09-13" "2024-09-14" "2024-09-15" "2024-09-16"
#> [91] "2024-09-17" "2024-09-18" "2024-09-19" "2024-09-20" "2024-09-21"
#> 

# use within a data.frame with multiple individuals
encounters <- tibble::tibble(
  id = rep(c("A", "B"), each = 3),
  encounter = rep(1:3, 2),
  date = as.Date(c(
    "2024-01-01", "2024-03-17", "2024-09-21",
    "2023-11-29", "2024-09-22", "2024-09-29"
  ))
)

encounters |>
  dplyr::arrange(id, date) |>
  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>     <int> <date>     <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      

# add a list-column of daily dates for each individual's encounters
encounters |>
  dplyr::arrange(id, date) |>
  dplyr::mutate(
    dates = impute_date_ranges(date, expand = TRUE),
    .by = "id"
  )
#> # A tibble: 6 × 4
#>   id    encounter date       dates       
#>   <chr>     <int> <date>     <list>      
#> 1 A             1 2024-01-01 <date [39]> 
#> 2 A             2 2024-03-17 <date [133]>
#> 3 A             3 2024-09-21 <date [95]> 
#> 4 B             1 2023-11-29 <date [150]>
#> 5 B             2 2024-09-22 <date [153]>
#> 6 B             3 2024-09-29 <date [5]>