Search code examples
rdateanalysislubridate

calculating timeframe accuracy in R


I am trying to look at prediction accuracy related to timeframes related to hospital discharge.

For example, I think Mr. Smith will be discharged within 3-7 days, which would mean he could any day from 11/9-11/13 would be correct. If he discharges in 2 days, I would say I was 1 day off and if he discharges within 10 days, I was 3 days off...

Is there any good method to do this using dplyr, base R, and lubridate? TIA. Sample data is at the link:

Sample data


Solution

  • A possible solution would be to express your need in a case_when.

    library(dplyr) 
    df %>%
      dplyr::mutate(DIF = case_when(discharge_calender_date < discharge_prediction_lower_bound ~ discharge_calender_date - discharge_prediction_lower_bound,
                                    discharge_calender_date <= discharge_prediction_upper_bound ~ 0,
                                    TRUE ~ discharge_calender_date - discharge_prediction_upper_bound))
    

    This way you get a negative value if the patient left before the lower bound, zero if he left within the prediction and a positive result if he left after the prediction.