Created
February 1, 2022 23:24
-
-
Save HorridTom/60511a2d1be95f5b9fbd69ebc995bd94 to your computer and use it in GitHub Desktop.
Perturb start and end times keeping duration within same "bins"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
perturb_start_end_los <- function( | |
df, | |
max_shift_size_hours = 24, | |
min_binsize_mins = 15 | |
){ | |
df <- df %>% | |
dplyr::mutate(los = spell_end - spell_start, | |
binstart = 15*floor(as.numeric(los, units = "mins")/15), | |
binend = 15*(floor(as.numeric(los, units = "mins")/15) + 1), | |
start_shift = as.difftime(runif(n = nrow(.), | |
min = -max_shift_size_hours, | |
max = max_shift_size_hours), | |
units = "hours"), | |
spell_start_new = spell_start + start_shift, | |
initial_ed_end_datetime_new = initial_ed_end_datetime + start_shift) %>% | |
dplyr::rowwise() %>% | |
dplyr::mutate(los_new = as.difftime(round(runif(1L, | |
min = binstart + 1, | |
max = binend - 1)), | |
units = "mins")) %>% | |
dplyr::ungroup() %>% | |
dplyr::mutate(spell_end_new = spell_start_new + los_new) | |
df <- df %>% | |
dplyr::select(-spell_start, -spell_end, -initial_ed_end_datetime, -los, | |
-binstart, -binend, -start_shift, -los_new) %>% | |
dplyr::rename(spell_start = spell_start_new, spell_end = spell_end_new, | |
initial_ed_end_datetime = initial_ed_end_datetime_new) | |
return(df) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment