R/mutate_model_output.R

Defines functions mutate_model_output

Documented in mutate_model_output

#' This function processes the model output
#' and creates new variables that are needed for metrics of interest. The
#' output from this function is another dataframe / tibble.
#'
#' @param df Dataframe Output from COVIDmodel. The input data frame must have a row that represents
#'   each experiment and
#'   time point within the experiment. The columns must contain the parameter combinations that
#'   were used for each experiment and time point, as well as basic model output showing
#'   the numbers in each compartment at a given time.
#' @param pop_size Integer The size of the modeled population
#'
#' @param start_date Date This is the start date for the local epidemic and it is used for adding a
#'   date column to the output.
#'
#' @param  report_lag Integer This is the number of days we assume pass between the infection and
#'   when it is first reported.
#'
#' @param pop_prop Vector This vector contains population proportions for sub-populations
#'
#' @export
#'
#' @importFrom magrittr %>%

mutate_model_output <- function(df, pop_size, start_date = NULL, report_lag = 0, pop_prop) {

  # used the calculate fractions without creating NaN values.
  # return NA for 0/0
  fraction <- function(a, b)
  {
    ifelse(b > 1e-10, a/b, NA)
  }

  # Do we need to create a date variable
  if(!is.null(start_date)){

  df <- df %>%
    dplyr::mutate(date = start_date + time + report_lag)

  }

  # Create vars that apply to each row
  df1 <- df %>%
    dplyr::mutate(r_2d = r_2u,
                  r_md = r_mu,
                  c_12d = c_12u,
                  c_1md = c_1mu,
                  c_1sd = c_1su,
                  eta_d = eta_u,
                  delta_sd = delta_su,
                  b_a = 0,
                  d_e = 0,
                  alpha_2 = a_2d * d_2 + a_2u * r_2d,
                  alpha_m = a_md * d_m + 1 * r_md,
                  chi_e = c_e1u + d_e,
                  chi_u = c_12u + c_1mu + c_1su + d_1,
                  chi_d = c_12d + c_1md + c_1sd,
                  xi_2 = r_2u + d_2,
                  xi_m = r_mu + d_m,
                  zeta_u = eta_u + d_s + delta_su,
                  zeta_d = eta_d + delta_sd,
                  K = a_sd * c_1sd * c_e1u * d_1 * r_2d * r_md + a_sd * c_1sd * d_e * r_2d * r_md * chi_u,
                  psi = a_1d * r_2d * r_md + a_2d * c_12d * r_md + a_md * c_1md * r_2d,
                  G = a_1d * d_1 * r_2d * r_md + a_1u * chi_d * r_2d * r_md + a_2d * c_12d * d_1 * r_md + a_md * c_1md * d_1 * r_2d,
                  # A = ((((d_e * psi * chi_u + c_e1u * G) * zeta_u +
                  #          a_su * c_1su * c_e1u * chi_d * r_2d * r_md) * zeta_d +
                  #         K * zeta_u  + a_sd * c_1su * c_e1u * chi_d * d_s * r_2d * r_md) * xi_m +
                  #        c_e1u * c_1mu * r_2d * alpha_m * chi_d * zeta_u * zeta_d) * xi_2 +
                  #   c_e1u * c_12u * r_md * alpha_2 * chi_d * xi_m * zeta_u * zeta_d,
                  #B = r_2d * r_md * chi_e * chi_u * chi_d * xi_2 * xi_m * zeta_u * zeta_d,
                  #R0 = (b_a + b_b) * A / B,
                  #Reff = FOIadjust * R0 * (S / pop_size),
                  AllInfections1 = E_u1 + I_1u1 + I_2u1 + I_mu1 + I_su1 + E_d1 + I_1d1 + I_2d1 + I_md1 + I_sd1 + H1 + P1 + C1,
                  AllInfections2 = E_u2 + I_1u2 + I_2u2 + I_mu2 + I_su2 + E_d2 + I_1d2 + I_2d2 + I_md2 + I_sd2 + H2 + P2 + C2,
                  AllInfections3 = E_u3 + I_1u3 + I_2u3 + I_mu3 + I_su3 + E_d3 + I_1d3 + I_2d3 + I_md3 + I_sd3 + H3 + P3 + C3,
                  AllInfections = AllInfections1 + AllInfections2 + AllInfections3,
                  ActiveInfections1 = I_1u1 + I_2u1 + I_mu1 + I_su1 + I_1d1 + I_2d1 + I_md1 + I_sd1,
                  ActiveInfections2 = I_1u2 + I_2u2 + I_mu2 + I_su2 + I_1d2 + I_2d2 + I_md2 + I_sd2,
                  ActiveInfections3 = I_1u3 + I_2u3 + I_mu3 + I_su3 + I_1d3 + I_2d3 + I_md3 + I_sd3,
                  ActiveInfections = ActiveInfections1 + ActiveInfections2 + ActiveInfections3,
                  SymptKnownAsymptInfections1 = I_mu1 + I_su1 + I_1d1 + I_2d1 + I_md1 + I_sd1,
                  SymptKnownAsymptInfections2 = I_mu2 + I_su2 + I_1d2 + I_2d2 + I_md2 + I_sd2,
                  SymptKnownAsymptInfections3 = I_mu3 + I_su3 + I_1d3 + I_2d3 + I_md3 + I_sd3,
                  SymptKnownAsymptInfections = SymptKnownAsymptInfections1 + SymptKnownAsymptInfections2 + SymptKnownAsymptInfections3,
                  SymptKnownInfections1 = I_md1 + I_sd1,
                  SymptKnownInfections2 = I_md2 + I_sd2,
                  SymptKnownInfections3 = I_md3 + I_sd3,
                  SymptKnownInfections = SymptKnownInfections1 + SymptKnownInfections2 + SymptKnownInfections3,
                  SymptUnknownInfections1 = I_mu1 + I_su1,
                  SymptUnknownInfections2 = I_mu2 + I_su2,
                  SymptUnknownInfections3 = I_mu3 + I_su3,
                  SymptUnknownInfections = SymptUnknownInfections1 + SymptUnknownInfections2 + SymptUnknownInfections3,
                  AsymptKnownInfections1 = I_1d1 + I_2d1,
                  AsymptKnownInfections2 = I_1d2 + I_2d2,
                  AsymptKnownInfections3 = I_1d3 + I_2d3,
                  AsymptKnownInfections = AsymptKnownInfections1 + AsymptKnownInfections2 + AsymptKnownInfections3,
                  AsymptUnknownInfections1 = I_1u1 + I_2u1,
                  AsymptUnknownInfections2 = I_1u2 + I_2u2,
                  AsymptUnknownInfections3 = I_1u3 + I_2u3,
                  AsymptUnknownInfections = AsymptUnknownInfections1 + AsymptUnknownInfections2 + AsymptUnknownInfections3,
                  SymptInfections1 = I_mu1 + I_md1 + I_su1 + I_sd1,
                  SymptInfections2 = I_mu2 + I_md2 + I_su2 + I_sd2,
                  SymptInfections3 = I_mu3 + I_md3 + I_su3 + I_sd3,
                  SymptInfections = SymptInfections1 + SymptInfections2 + SymptInfections3,
                  AsymptInfections1 = I_1u1 + I_2u1 + I_1d1 + I_2d1,
                  AsymptInfections2 = I_1u2 + I_2u2 + I_1d2 + I_2d2,
                  AsymptInfections3 = I_1u3 + I_2u3 + I_1d3 + I_2d3,
                  AsymptInfections = AsymptInfections1 + AsymptInfections2 + AsymptInfections3,
                  KnownInfections1 = I_1d1 + I_2d1 + I_md1 + I_sd1,
                  KnownInfections2 = I_1d2 + I_2d2 + I_md2 + I_sd2,
                  KnownInfections3 = I_1d3 + I_2d3 + I_md3 + I_sd3,
                  KnownInfections = KnownInfections1 + KnownInfections2 + KnownInfections3,
                  UnknownInfections1 = I_1u1 + I_2u1 + I_mu1 + I_su1,
                  UnknownInfections2 = I_1u2 + I_2u2 + I_mu2 + I_su2,
                  UnknownInfections3 = I_1u3 + I_2u3 + I_mu3 + I_su3,
                  UnknownInfections = UnknownInfections1 + UnknownInfections2 + UnknownInfections3,
                  SevereKnownMildInfections1 = I_sd1 + I_su1 + I_md1,
                  SevereKnownMildInfections2 = I_sd2 + I_su2 + I_md2,
                  SevereKnownMildInfections3 = I_sd3 + I_su3 + I_md3,
                  SevereKnownMildInfections = SevereKnownMildInfections1 + SevereKnownMildInfections2 + SevereKnownMildInfections3,
                  SevereInfections1 = I_sd1 + I_su1,
                  SevereInfections2 = I_sd2 + I_su2,
                  SevereInfections3 = I_sd3 + I_su3,
                  SevereInfections = SevereInfections1 + SevereInfections2 + SevereInfections3,
                  Hospitalizations1 = H1 + P1 + C1,
                  Hospitalizations2 = H2 + P2 + C2,
                  Hospitalizations3 = H3 + P3 + C3,
                  Hospitalizations = Hospitalizations1 + Hospitalizations2 + Hospitalizations3,
                  Hosp_I_sd1 = I_sd1 + Hospitalizations1,
                  Hosp_I_sd2 = I_sd2 + Hospitalizations2,
                  Hosp_I_sd3 = I_sd3 + Hospitalizations3,
                  Hosp_I_sd = Hosp_I_sd1 + Hosp_I_sd2 + Hosp_I_sd3,
                  Hosp_SevereInfections1 = SevereInfections1 + Hospitalizations1,
                  Hosp_SevereInfections2 = SevereInfections2 + Hospitalizations2,
                  Hosp_SevereInfections3 = SevereInfections3 + Hospitalizations3,
                  Hosp_SevereInfections = Hosp_SevereInfections1 + Hosp_SevereInfections2 + Hosp_SevereInfections3,
                  Hosp_SevereKnownMildInfections1 = SevereKnownMildInfections1 + Hospitalizations1,
                  Hosp_SevereKnownMildInfections2 = SevereKnownMildInfections2 + Hospitalizations2,
                  Hosp_SevereKnownMildInfections3 = SevereKnownMildInfections3 + Hospitalizations3,
                  Hosp_SevereKnownMildInfections = Hosp_SevereKnownMildInfections1 + Hosp_SevereKnownMildInfections2 + Hosp_SevereKnownMildInfections3,
                  Hosp_SymptInfections1 = SymptInfections1 + Hospitalizations1,
                  Hosp_SymptInfections2 = SymptInfections2 + Hospitalizations2,
                  Hosp_SymptInfections3 = SymptInfections3 + Hospitalizations3,
                  Hosp_SymptInfections = Hosp_SymptInfections1 + Hosp_SymptInfections2 + Hosp_SymptInfections3,
                  Hosp_SymptKnownAsymptInfections1 = SymptKnownAsymptInfections1 + Hospitalizations1,
                  Hosp_SymptKnownAsymptInfections2 = SymptKnownAsymptInfections2 + Hospitalizations2,
                  Hosp_SymptKnownAsymptInfections3 = SymptKnownAsymptInfections3 + Hospitalizations3,
                  Hosp_SymptKnownAsymptInfections = Hosp_SymptKnownAsymptInfections1 + Hosp_SymptKnownAsymptInfections2 + Hosp_SymptKnownAsymptInfections3,
                  Hosp_ActiveInfections1 = ActiveInfections1 + Hospitalizations1,
                  Hosp_ActiveInfections2 = ActiveInfections2 + Hospitalizations2,
                  Hosp_ActiveInfections3 = ActiveInfections3 + Hospitalizations3,
                  Hosp_ActiveInfections = Hosp_ActiveInfections1 + Hosp_ActiveInfections2 + Hosp_ActiveInfections3,
                  Hosp_SymptKnownInfections1 = SymptKnownInfections1 + Hospitalizations1,
                  Hosp_SymptKnownInfections2 = SymptKnownInfections2 + Hospitalizations2,
                  Hosp_SymptKnownInfections3 = SymptKnownInfections3 + Hospitalizations3,
                  Hosp_SymptKnownInfections = Hosp_SymptKnownInfections1 + Hosp_SymptKnownInfections2 + Hosp_SymptKnownInfections3,
                  hosp_nonicu1 = H1 + P1,
                  hosp_nonicu2 = H2 + P2,
                  hosp_nonicu3 = H3 + P3,
                  hosp_nonicu = hosp_nonicu1 + hosp_nonicu2 + hosp_nonicu3,
                  deaths_hosp1 = D_h1 + D_c1,
                  deaths_hosp2 = D_h2 + D_c2,
                  deaths_hosp3 = D_h3 + D_c3,
                  deaths_hosp = deaths_hosp1 + deaths_hosp2 + deaths_hosp3,
                  NotWorking1 = KnownInfections1 + Hospitalizations1,
                  NotWorking2 = KnownInfections2 + Hospitalizations2,
                  NotWorking3 = KnownInfections3 + Hospitalizations3,
                  NotWorking = NotWorking1 + NotWorking2 + NotWorking3,
                  ReturnWork_cumul_flow1 = R_2d1 + R_md1 + R_c1 + R_h1,
                  ReturnWork_cumul_flow2 = R_2d2 + R_md2 + R_c2 + R_h2,
                  ReturnWork_cumul_flow3 = R_2d3 + R_md3 + R_c3 + R_h3,
                  ReturnWork_cumul_flow = ReturnWork_cumul_flow1 + ReturnWork_cumul_flow2 + ReturnWork_cumul_flow3,
                  AllDeaths1 = D_s1 + D_h1 + D_c1,
                  AllDeaths2 = D_s2 + D_h2 + D_c2,
                  AllDeaths3 = D_s3 + D_h3 + D_c3,
                  AllDeaths = AllDeaths1 + AllDeaths2 + AllDeaths3,
                  ContribAll = ContribAll1 + ContribAll2 + ContribAll3,
                  ContribNonSympt = ContribNonSympt1 + ContribNonSympt2 + ContribNonSympt3,
                  ConfirmedCases = ConfirmedCases1 + ConfirmedCases2 + ConfirmedCases3,
                  eta_d_cumul_flow = eta_d_cumul_flow1 + eta_d_cumul_flow2 + eta_d_cumul_flow3,
                  eta_u_cumul_flow = eta_u_cumul_flow1 + eta_u_cumul_flow2 + eta_u_cumul_flow3,
                  r_h_cumul_flow = r_h_cumul_flow1 + r_h_cumul_flow2 + r_h_cumul_flow3,
                  delta_h_cumul_flow = delta_h_cumul_flow1 + delta_h_cumul_flow2 + delta_h_cumul_flow3,
                  theta_cumul_flow = theta_cumul_flow1 + theta_cumul_flow2 + theta_cumul_flow3,
                  Symp_diagnozed_cumul_flow = Symp_diagnozed_cumul_flow1 + Symp_diagnozed_cumul_flow2 + Symp_diagnozed_cumul_flow3,
                  Asymp_diagnozed_cumul_flow = Asymp_diagnozed_cumul_flow1 + Asymp_diagnozed_cumul_flow2 + Asymp_diagnozed_cumul_flow3,
                  Symp_inf_cumul_flow = Symp_inf_cumul_flow1 + Symp_inf_cumul_flow2 + Symp_inf_cumul_flow3,
                  ReturnWork_cumul_flow = ReturnWork_cumul_flow1 + ReturnWork_cumul_flow2 + ReturnWork_cumul_flow3,
                  H = H1 + H2 + H3,
                  P = P1 + P2 + P3,
                  C = C1 + C2 + C3,
                  I_sd = I_sd1 + I_sd2 + I_sd3,
                  AllVaccinations1 = Vaccination_dose1_flow1 + Vaccination_fully_flow1,
                  AllVaccinations2 = Vaccination_dose1_flow2 + Vaccination_fully_flow2,
                  AllVaccinations3 = Vaccination_dose1_flow3 + Vaccination_fully_flow3,
                  AllVaccinations = AllVaccinations1 + AllVaccinations2 + AllVaccinations3,
                  Dose1Vaccinated = Vaccination_dose1_flow1 + Vaccination_dose1_flow2 + Vaccination_dose1_flow3,
                  FullyVaccinated = Vaccination_fully_flow1 + Vaccination_fully_flow2 + Vaccination_fully_flow3,
                  Prevalence1 = ActiveInfections1 / (pop_prop[1] * pop_size),
                  Prevalence2 = ActiveInfections2 / (pop_prop[2] * pop_size),
                  Prevalence3 = ActiveInfections3 / (pop_prop[3] * pop_size),
                  Prevalence = ActiveInfections / pop_size,
                  Exposure1 = ContribAll1 / (pop_prop[1] * pop_size),
                  Exposure2 = ContribAll2 / (pop_prop[2] * pop_size),
                  Exposure3 = ContribAll3 / (pop_prop[3] * pop_size),
                  Exposure = ContribAll / pop_size,
                  Susceptible1 = S1 / (pop_prop[1] * pop_size),
                  Susceptible2 = S2 / (pop_prop[2] * pop_size),
                  Susceptible3 = S3 / (pop_prop[3] * pop_size),
                  Susceptible = (S1 + S2 + S3) / pop_size,
                  FracSymptKnown1 = fraction(SymptKnownInfections1, ActiveInfections1),
                  FracSymptKnown2 = fraction(SymptKnownInfections2, ActiveInfections2),
                  FracSymptKnown3 = fraction(SymptKnownInfections3, ActiveInfections3),
                  FracSymptKnown = fraction(SymptKnownInfections, ActiveInfections),
                  FracSymptUnknown1 = fraction(SymptUnknownInfections1, ActiveInfections1),
                  FracSymptUnknown2 = fraction(SymptUnknownInfections2, ActiveInfections2),
                  FracSymptUnknown3 = fraction(SymptUnknownInfections3, ActiveInfections3),
                  FracSymptUnknown = fraction(SymptUnknownInfections, ActiveInfections),
                  FracAsymptKnown1 = fraction(AsymptKnownInfections1, ActiveInfections1),
                  FracAsymptKnown2 = fraction(AsymptKnownInfections2, ActiveInfections2),
                  FracAsymptKnown3 = fraction(AsymptKnownInfections3, ActiveInfections3),
                  FracAsymptKnown = fraction(AsymptKnownInfections, ActiveInfections),
                  FracAsymptUnknown1 = fraction(AsymptUnknownInfections1, ActiveInfections1),
                  FracAsymptUnknown2 = fraction(AsymptUnknownInfections2, ActiveInfections2),
                  FracAsymptUnknown3 = fraction(AsymptUnknownInfections3, ActiveInfections3),
                  FracAsymptUnknown = fraction(AsymptUnknownInfections, ActiveInfections),
                  FracHospSymptKnown1 = fraction(Hosp_SymptKnownInfections1, Hosp_SymptInfections1),
                  FracHospSymptKnown2 = fraction(Hosp_SymptKnownInfections2, Hosp_SymptInfections2),
                  FracHospSymptKnown3 = fraction(Hosp_SymptKnownInfections3, Hosp_SymptInfections3),
                  FracHospSymptKnown = fraction(Hosp_SymptKnownInfections, Hosp_SymptInfections),
                  idf1 = fraction(KnownInfections1 + H1 + P1 + C1, ActiveInfections1 + H1 + P1 + C1),
                  idf2 = fraction(KnownInfections2 + H2 + P2 + C2, ActiveInfections2 + H2 + P2 + C2),
                  idf3 = fraction(KnownInfections3 + H3 + P3 + C3, ActiveInfections3 + H3 + P3 + C3),
                  idf = fraction(KnownInfections + H + P + C, ActiveInfections + H + P + C),
                  ifr1 = fraction(AllDeaths1, ContribAll1),
                  ifr2 = fraction(AllDeaths2, ContribAll2),
                  ifr3 = fraction(AllDeaths3, ContribAll3),
                  ifr = fraction(AllDeaths, ContribAll), # All deaths at a point in time / Cumulative Incidence (all people who have been infected)
                  cfr1 = fraction(AllDeaths1, ConfirmedCases1), # Cum deaths at a point in time / all cumulative detected cases
                  cfr2 = fraction(AllDeaths2, ConfirmedCases2),
                  cfr3 = fraction(AllDeaths3, ConfirmedCases3),
                  cfr = fraction(AllDeaths, ConfirmedCases))

  # Create vars that apply to each experiment
  df2 <- df1 %>%
    dplyr::arrange(experiment, time) %>%
    dplyr::group_by(experiment) %>%
    dplyr::mutate(AllDailyInfections1 = ContribAll1 - dplyr::lag(ContribAll1, default = 0),
                  AllDailyInfections2 = ContribAll2 - dplyr::lag(ContribAll2, default = 0),
                  AllDailyInfections3 = ContribAll3 - dplyr::lag(ContribAll3, default = 0),
                  AllDailyInfections = ContribAll - dplyr::lag(ContribAll, default = 0),
                  NonSymptDailyInfections1 = ContribNonSympt1 - dplyr::lag(ContribNonSympt1, default = 0),
                  NonSymptDailyInfections2 = ContribNonSympt2 - dplyr::lag(ContribNonSympt2, default = 0),
                  NonSymptDailyInfections3 = ContribNonSympt3 - dplyr::lag(ContribNonSympt3, default = 0),
                  NonSymptDailyInfections = ContribNonSympt - dplyr::lag(ContribNonSympt, default = 0),
                  RelContribNonSympt1 = fraction(NonSymptDailyInfections1, AllDailyInfections1),
                  RelContribNonSympt2 = fraction(NonSymptDailyInfections2, AllDailyInfections2),
                  RelContribNonSympt3 = fraction(NonSymptDailyInfections3, AllDailyInfections3),
                  RelContribNonSympt = fraction(NonSymptDailyInfections, AllDailyInfections),
                  NewCases1 = ConfirmedCases1 - dplyr::lag(ConfirmedCases1, default = 0),
                  NewCases2 = ConfirmedCases2 - dplyr::lag(ConfirmedCases2, default = 0),
                  NewCases3 = ConfirmedCases3 - dplyr::lag(ConfirmedCases3, default = 0),
                  NewCases = ConfirmedCases - dplyr::lag(ConfirmedCases, default = 0),
                  NewDeaths1 = AllDeaths1 - dplyr::lag(AllDeaths1, default = 0),
                  NewDeaths2 = AllDeaths2 - dplyr::lag(AllDeaths2, default = 0),
                  NewDeaths3 = AllDeaths3 - dplyr::lag(AllDeaths3, default = 0),
                  NewDeaths = AllDeaths - dplyr::lag(AllDeaths, default = 0),
                  NewVaccinations1 = AllVaccinations1 - dplyr::lag(AllVaccinations1, default = 0),
                  NewVaccinations2 = AllVaccinations2 - dplyr::lag(AllVaccinations2, default = 0),
                  NewVaccinations3 = AllVaccinations3 - dplyr::lag(AllVaccinations3, default = 0),
                  NewVaccinations = AllVaccinations - dplyr::lag(AllVaccinations, default = 0),
                  NewDose1Vaccinated = Dose1Vaccinated - dplyr::lag(Dose1Vaccinated, default = 0),
                  NewFullyVaccinated = FullyVaccinated - dplyr::lag(FullyVaccinated, default = 0),
                  eta_d_flow1 = eta_d_cumul_flow1 - dplyr::lag(eta_d_cumul_flow1, default = 0),
                  eta_d_flow2 = eta_d_cumul_flow2 - dplyr::lag(eta_d_cumul_flow2, default = 0),
                  eta_d_flow3 = eta_d_cumul_flow3 - dplyr::lag(eta_d_cumul_flow3, default = 0),
                  eta_d_flow = eta_d_cumul_flow - dplyr::lag(eta_d_cumul_flow, default = 0),
                  eta_u_flow1 = eta_u_cumul_flow1 - dplyr::lag(eta_u_cumul_flow1, default = 0),
                  eta_u_flow2 = eta_u_cumul_flow2 - dplyr::lag(eta_u_cumul_flow2, default = 0),
                  eta_u_flow3 = eta_u_cumul_flow3 - dplyr::lag(eta_u_cumul_flow3, default = 0),
                  eta_u_flow = eta_u_cumul_flow - dplyr::lag(eta_u_cumul_flow, default = 0),
                  r_h_flow1 = r_h_cumul_flow1 - dplyr::lag(r_h_cumul_flow1, default = 0),
                  r_h_flow2 = r_h_cumul_flow2 - dplyr::lag(r_h_cumul_flow2, default = 0),
                  r_h_flow3 = r_h_cumul_flow3 - dplyr::lag(r_h_cumul_flow3, default = 0),
                  r_h_flow = r_h_cumul_flow - dplyr::lag(r_h_cumul_flow, default = 0),
                  delta_h_flow1 = delta_h_cumul_flow1 - dplyr::lag(delta_h_cumul_flow1, default = 0),
                  delta_h_flow2 = delta_h_cumul_flow2 - dplyr::lag(delta_h_cumul_flow2, default = 0),
                  delta_h_flow3 = delta_h_cumul_flow3 - dplyr::lag(delta_h_cumul_flow3, default = 0),
                  delta_h_flow = delta_h_cumul_flow - dplyr::lag(delta_h_cumul_flow, default = 0),
                  theta_flow1 = theta_cumul_flow1 - dplyr::lag(theta_cumul_flow1, default = 0),
                  theta_flow2 = theta_cumul_flow2 - dplyr::lag(theta_cumul_flow2, default = 0),
                  theta_flow3 = theta_cumul_flow3 - dplyr::lag(theta_cumul_flow3, default = 0),
                  theta_flow = theta_cumul_flow - dplyr::lag(theta_cumul_flow, default = 0),
                  Symp_diagnozed_flow1 = Symp_diagnozed_cumul_flow1 - dplyr::lag(Symp_diagnozed_cumul_flow1, default = 0),
                  Symp_diagnozed_flow2 = Symp_diagnozed_cumul_flow2 - dplyr::lag(Symp_diagnozed_cumul_flow2, default = 0),
                  Symp_diagnozed_flow3 = Symp_diagnozed_cumul_flow3 - dplyr::lag(Symp_diagnozed_cumul_flow3, default = 0),
                  Symp_diagnozed_flow = Symp_diagnozed_cumul_flow - dplyr::lag(Symp_diagnozed_cumul_flow, default = 0),
                  Asymp_diagnozed_flow1 = Asymp_diagnozed_cumul_flow1 - dplyr::lag(Asymp_diagnozed_cumul_flow1, default = 0),
                  Asymp_diagnozed_flow2 = Asymp_diagnozed_cumul_flow2 - dplyr::lag(Asymp_diagnozed_cumul_flow2, default = 0),
                  Asymp_diagnozed_flow3 = Asymp_diagnozed_cumul_flow3 - dplyr::lag(Asymp_diagnozed_cumul_flow3, default = 0),
                  Asymp_diagnozed_flow = Asymp_diagnozed_cumul_flow - dplyr::lag(Asymp_diagnozed_cumul_flow, default = 0),
                  Symp_inf_flow1 = Symp_inf_cumul_flow1 - dplyr::lag(Symp_inf_cumul_flow1, default = 0),
                  Symp_inf_flow2 = Symp_inf_cumul_flow2 - dplyr::lag(Symp_inf_cumul_flow2, default = 0),
                  Symp_inf_flow3 = Symp_inf_cumul_flow3 - dplyr::lag(Symp_inf_cumul_flow3, default = 0),
                  Symp_inf_flow = Symp_inf_cumul_flow - dplyr::lag(Symp_inf_cumul_flow, default = 0),
                  ReturnWork_flow1 = ReturnWork_cumul_flow1 - dplyr::lag(ReturnWork_cumul_flow1, default = 0),
                  ReturnWork_flow2 = ReturnWork_cumul_flow2 - dplyr::lag(ReturnWork_cumul_flow2, default = 0),
                  ReturnWork_flow3 = ReturnWork_cumul_flow3 - dplyr::lag(ReturnWork_cumul_flow3, default = 0),
                  ReturnWork_flow = ReturnWork_cumul_flow - dplyr::lag(ReturnWork_cumul_flow, default = 0))

  # Create vars that apply to each time point, by summarized by experiment
  df3 <- df2 %>%
    dplyr::group_by(time) %>%
    dplyr::mutate(dplyr::across(.cols = c(ConfirmedCases, ConfirmedCases1, ConfirmedCases2, ConfirmedCases3,
                                          NewCases, NewCases1, NewCases2, NewCases3,
                                          ActiveInfections, ActiveInfections1, ActiveInfections2, ActiveInfections3,
                                          Prevalence, Prevalence1, Prevalence2, Prevalence3,
                                          Exposure, Exposure1, Exposure2, Exposure3,
                                          Susceptible, Susceptible1, Susceptible2, Susceptible3,
                                          SevereInfections, SevereInfections1, SevereInfections2, SevereInfections3,
                                          I_sd, I_sd1, I_sd2, I_sd3,
                                          Hospitalizations, Hospitalizations1, Hospitalizations2, Hospitalizations3,
                                          C, C1, C2, C3,
                                          SymptInfections, SymptInfections1, SymptInfections2, SymptInfections3,
                                          AsymptInfections, AsymptInfections1, AsymptInfections2, AsymptInfections3,
                                          NotWorking, NotWorking1, NotWorking2, NotWorking3,
                                          KnownInfections, KnownInfections1, KnownInfections2, KnownInfections3,
                                          SymptKnownInfections, SymptKnownInfections1, SymptKnownInfections2, SymptKnownInfections3,
                                          AsymptKnownInfections, AsymptKnownInfections1, AsymptKnownInfections2, AsymptKnownInfections3,
                                          AllDeaths, AllDeaths1, AllDeaths2, AllDeaths3,
                                          NewDeaths, NewDeaths1, NewDeaths2, NewDeaths3,
                                          NewVaccinations, NewVaccinations1, NewVaccinations2, NewVaccinations3,
                                          FullyVaccinated, Dose1Vaccinated,
                                          NewDose1Vaccinated,
                                          NewFullyVaccinated,
                                          AllVaccinations, AllVaccinations1, AllVaccinations2, AllVaccinations3,
                                          Hosp_I_sd, Hosp_I_sd1, Hosp_I_sd2, Hosp_I_sd3,
                                          Hosp_SevereInfections, Hosp_SevereInfections1, Hosp_SevereInfections2, Hosp_SevereInfections3,
                                          Hosp_SevereKnownMildInfections, Hosp_SevereKnownMildInfections1, Hosp_SevereKnownMildInfections2, Hosp_SevereKnownMildInfections3,
                                          Hosp_SymptInfections, Hosp_SymptInfections1, Hosp_SymptInfections2, Hosp_SymptInfections3,
                                          Hosp_SymptKnownAsymptInfections, Hosp_SymptKnownAsymptInfections1, Hosp_SymptKnownAsymptInfections2, Hosp_SymptKnownAsymptInfections3,
                                          Hosp_ActiveInfections, Hosp_ActiveInfections1, Hosp_ActiveInfections2, Hosp_ActiveInfections3,
                                          Hosp_SymptKnownInfections, Hosp_SymptKnownInfections1, Hosp_SymptKnownInfections2, Hosp_SymptKnownInfections3,
                                          hosp_nonicu, hosp_nonicu1, hosp_nonicu2, hosp_nonicu3,
                                          deaths_hosp, deaths_hosp1, deaths_hosp2, deaths_hosp3,
                                          eta_d_flow, eta_d_flow1, eta_d_flow2, eta_d_flow2,
                                          eta_u_flow, eta_u_flow1, eta_u_flow2, eta_u_flow3,
                                          r_h_flow, r_h_flow1, r_h_flow2, r_h_flow3,
                                          delta_h_flow, delta_h_flow1, delta_h_flow2, delta_h_flow3,
                                          theta_flow, theta_flow1, theta_flow2, theta_flow3,
                                          Symp_diagnozed_flow, Symp_diagnozed_flow1, Symp_diagnozed_flow2, Symp_diagnozed_flow3,
                                          Asymp_diagnozed_flow, Asymp_diagnozed_flow1, Asymp_diagnozed_flow2, Asymp_diagnozed_flow3,
                                          Symp_inf_flow, Symp_inf_flow1, Symp_inf_flow2, Symp_inf_flow3,
                                          ReturnWork_flow, ReturnWork_flow1, ReturnWork_flow2, ReturnWork_flow3),
                  .fns = list(mean = mean, min = min, max = max),
                  .names = "{col}_{fn}")) %>%
    dplyr::ungroup()

  #Prepend parameters with a "par_"
  df4 <- df3 %>%
    dplyr::rename_with(function(x){paste0("par_", x)}, a_1d:upsilon | r_2d:d_e)

  return(df4)

}
wimmyteam/conisi documentation built on Oct. 30, 2021, 4:11 p.m.