Here load all the functions and parameters needed

library and dataset needed

library(tidyverse)
library(ggplot2)
library(viridis)
library(Rcpp)
library(dampack)
library(DT)
library(lhs)
load("data/tb_mortality_model.Rdata")
load("result/simulated.RData")

The distribution calculation function

betapar <- function(tgt) {
  tgt <- as.numeric(tgt)
  mn <- tgt[1]; cir <- (tgt[3]-tgt[2])
  xopt <- function(xx,mn=mn,cir=cir) {
    cir2 <- qbeta(c(1,39)/40,xx*(mn/(1-mn)),xx); 
    cir2 <- cir2[2]-cir2[1]
    sum((cir2-cir)^2) }
  zz <- optimize(xopt,c(0.2,100000),mn=mn,cir=cir)$minimum
  bp <- c(zz*(mn/(1-mn)),zz)
  if(sum(abs(bp-1))<0.2) { c(1,1) } else { bp }
}
gammapar <- function(tgt) {
  tgt <- as.numeric(tgt)
  mn <- tgt[1]; cir <- (tgt[3]-tgt[2])
  xopt <- function(b,mn,cir) {
    cir2 <- qgamma(c(1,39)/40,mn*b,b); 
    cir2 <- cir2[2]-cir2[1]
    (cir2-cir)^2 }
  zz <- optimize(xopt,c(0.1,100000),mn=mn,cir=cir)$minimum
  gp = c(zz*mn,zz) }

The model function

# for each input is binary either 1 or 0 exlcude the age and sex
# test tst-0, igra-1
# notest if test-0, no test -1
# us = 1 in us, us =0 non us
generate_p2 <- function(n_age_start, gender, hiv, dia, gas, renal, immu, trans, can, bmi,kid, smoke, ltc, test, notest, prison, homeless, us, t4r, t3hp,t1hp, t6h, ttb, d_s, d_lf, d_ls, d_l0, cycle_length=1, cycle_n){
  
  #time age dependent base mortality rate
  v_r_mort_by_age <- mor%>%
  filter(sex==gender) %>% 
  filter(age >= n_age_start & age < 100) %>%
  select(monthly_rate) %>% 
  as.matrix()
  v_r_HDage = rep(v_r_mort_by_age, each = 12)
  
  # calculate the TB mortality
  tb_r_mort_by_age <-  mor%>%
  filter(sex==gender) %>% 
  filter(age >= n_age_start & age < 100) %>%
  select(tb_mortality_rate ) %>% 
  as.matrix()
  v_tb_HDage = rep(tb_r_mort_by_age, each = 12) 
  
  reactivation_data <- tibble(
  age_group = c("6-14", "15-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75+"),
  rate= c(sim_tb_0_14[cycle_n], sim_tb_15_24[cycle_n], sim_tb_25_34[cycle_n], sim_tb_35_44[cycle_n], sim_tb_45_54[cycle_n], sim_tb_55_64[cycle_n], sim_tb_65_74[cycle_n], sim_tb_75[cycle_n])
)


# Expand the age groups to cover ages 0-100

age_range <- 0:100
ltbi_reacti <- tibble(age = age_range)

ltbi_reacti<- ltbi_reacti %>%
  mutate(
    rate = case_when(
      age <= 14 ~ reactivation_data$rate[reactivation_data$age_group == "6-14" ],
      age <= 24 ~ reactivation_data$rate[reactivation_data$age_group == "15-24" ],
      age <= 34 ~ reactivation_data$rate[reactivation_data$age_group == "25-34" ],
      age <= 44 ~ reactivation_data$rate[reactivation_data$age_group == "35-44"],
      age <= 54 ~ reactivation_data$rate[reactivation_data$age_group == "45-54"],
      age <= 64 ~ reactivation_data$rate[reactivation_data$age_group == "55-64" ],
      age <= 74 ~ reactivation_data$rate[reactivation_data$age_group == "65-74"],
      age >= 75 ~ reactivation_data$rate[reactivation_data$age_group == "75+"]
    )
  )

  #calculate the TB reactivation rate
   ltbi_react_by_age <-  ltbi_reacti%>%
  filter(age >= n_age_start & age < 100) %>%
  select(rate ) %>% 
  as.matrix()
  v_ltbi_HDage = rep(ltbi_react_by_age, each = 12)  
  
      
  # the risk factor    
  r_SD = v_r_HDage*(1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1))

  r_LFD = v_r_HDage*(1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1))
  r_LFI = (v_ltbi_HDage/100/12) * (1+hiv*(sim_i_hiv[cycle_n]-1)+dia*(sim_i_dia[cycle_n]-1)+gas*(sim_i_gas[cycle_n]-1)+renal*(sim_i_renal[cycle_n]-1)+immu*(sim_i_immu[cycle_n]-1)+trans*(sim_i_trans[cycle_n]-1)+can*(sim_i_can[cycle_n]-1)+bmi*(sim_i_bmi[cycle_n]-1)+kid*(sim_i_kid[cycle_n]-1)+smoke*(sim_i_smok[cycle_n]-1))
  
  r_LSD = v_r_HDage * (1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1))
 # r_LSL0 = 0.03 constant monthly rate of becoming L0 when LS
  r_LSI = (v_ltbi_HDage/100/12)  *(1+hiv*(sim_i_hiv[cycle_n]-1)+dia*(sim_i_dia[cycle_n]-1)+gas*(sim_i_gas[cycle_n]-1)+renal*(sim_i_renal[cycle_n]-1)+immu*(sim_i_immu[cycle_n]-1)+trans*(sim_i_trans[cycle_n]-1)+can*(sim_i_can[cycle_n]-1)+bmi*(sim_i_bmi[cycle_n]-1)+kid*(sim_i_kid[cycle_n]-1)+smoke*(sim_i_smok[cycle_n]-1))
  
  r_L0D = v_r_HDage*(1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1)) 
  
  #need update for false treatment
  r_StD = v_r_HDage* (1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1)) 

  r_LFtD = v_r_HDage *(1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1)) 
  
  r_LStD = v_r_HDage * (1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1))  

  r_L0tD = v_r_HDage * (1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1)) 


  r_ID = v_tb_HDage * (1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1)) # need update with time dependent equation, is side effect/
  
  r_IIt = 12/4/12
  
  r_ItR = 12/8/12
  
  r_ItD = v_tb_HDage* (1+ hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1)) 

  r_RD = v_r_HDage* (1+hiv*(sim_d_HIV[cycle_n]-1)+dia*(sim_d_dia[cycle_n]-1)+gas*(sim_d_gas[cycle_n]-1)+renal*(sim_d_renal[cycle_n]-1)+immu*(sim_d_immu[cycle_n]-1)+trans*(sim_d_trans[cycle_n]-1)+can*(sim_d_can[cycle_n]-1)+bmi*(sim_d_bmi[cycle_n]-1)+kid*(sim_d_kid[cycle_n]-1)+smoke*(sim_d_smok[cycle_n]-1)+ ltc*(sim_d_ltc[cycle_n]-1)+ homeless*(sim_d_homeless[cycle_n]-1))
  


  p_values = list(
    # the initial distribution should be proportion
    ini_S= (1-notest)*d_s*(1-
                   (1- (hiv*us)*sim_tst_spe_us_hiv[cycle_n] 
      -(1-hiv)*us*sim_tst_spe_us[cycle_n] 
      -hiv*(1-us)*sim_tst_spe_nus_hiv[cycle_n] 
      -(1-hiv)*(1-us)*sim_tst_spe_nus[cycle_n])*0.762*(1-test)
      - (1- (hiv*us)*sim_igra_spe_us_hiv[cycle_n] 
         -(1-hiv)*us*sim_igra_spe_us[cycle_n] 
         -hiv*(1-us)*sim_igra_spe_nus_hiv[cycle_n] 
         -(1-hiv)*(1-us)*sim_igra_spe_nus[cycle_n])*0.762*test) + notest*d_s,
     ini_LF = (1-notest) *d_lf *(1-( (hiv*us)*sim_tst_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_tst_sen_us[cycle_n] +hiv*(1-us)*sim_tst_sen_nus_hiv[cycle_n] +(1-hiv)*(1-us)*sim_tst_sen_nus[cycle_n])*0.762*(1-test) - ((hiv*us)*sim_igra_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_igra_sen_us[cycle_n] +hiv*(1-us)*sim_igra_sen_nus_hiv[cycle_n] + (1-hiv)*(1-us)*sim_igra_sen_nus[cycle_n])*0.762*test) + notest*d_lf,
    ini_LS =(1-notest) * d_ls *(1-( (hiv*us)*sim_tst_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_tst_sen_us[cycle_n] +hiv*(1-us)*sim_tst_sen_nus_hiv[cycle_n] +(1-hiv)*(1-us)*sim_tst_sen_nus[cycle_n])*0.762*(1-test) - ((hiv*us)*sim_igra_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_igra_sen_us[cycle_n] +hiv*(1-us)*sim_igra_sen_nus_hiv[cycle_n] + (1-hiv)*(1-us)*sim_igra_sen_nus[cycle_n])*0.762*test)+ notest*d_ls,
    ini_L0 = (1-notest) * d_l0 *(1-( (hiv*us)*sim_tst_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_tst_sen_us[cycle_n] +hiv*(1-us)*sim_tst_sen_nus_hiv[cycle_n] +(1-hiv)*(1-us)*sim_tst_sen_nus[cycle_n])*0.762*(1-test) - ((hiv*us)*sim_igra_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_igra_sen_us[cycle_n] +hiv*(1-us)*sim_igra_sen_nus_hiv[cycle_n] + (1-hiv)*(1-us)*sim_igra_sen_nus[cycle_n])*0.762*test)+ notest*d_l0,
    
    # before the cycle, first go through the test and then based on test result go to treatment
    # only false positive from S will go to St
    # if test 
    ini_St =(1-notest) * d_s*((1-(hiv*us)*sim_tst_spe_us_hiv[cycle_n] -(1-hiv)*us*sim_tst_spe_us[cycle_n] -hiv*(1-us)*sim_tst_spe_nus_hiv[cycle_n] -(1-hiv)*(1-us)*sim_tst_spe_nus[cycle_n])*0.762*(1-test) + (1- (hiv*us)*sim_igra_spe_us_hiv[cycle_n] -(1-hiv)*us*sim_igra_spe_us[cycle_n] -hiv*(1-us)*sim_igra_spe_nus_hiv[cycle_n] -(1-hiv)*(1-us)*sim_igra_spe_nus[cycle_n])*0.762*test),
    # only true positive from L will go to Lt
    ini_LFt=(1-notest) * d_lf *(((hiv*us)*sim_tst_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_tst_sen_us[cycle_n] +hiv*(1-us)*sim_tst_sen_nus_hiv[cycle_n] +(1-hiv)*(1-us)*sim_tst_sen_nus[cycle_n])*0.762*(1-test) + ((hiv*us)*sim_igra_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_igra_sen_us[cycle_n] +hiv*(1-us)*sim_igra_sen_nus_hiv[cycle_n] + (1-hiv)*(1-us)*sim_igra_sen_nus[cycle_n])*0.762*test),
   
    ini_LSt =(1-notest) * d_ls *(( (hiv*us)*sim_tst_sen_us_hiv +(1-hiv)*us*sim_tst_sen_us[cycle_n] +hiv*(1-us)*sim_tst_sen_nus_hiv[cycle_n] +(1-hiv)*(1-us)*sim_tst_sen_nus[cycle_n])*0.762*(1-test) + ((hiv*us)*sim_igra_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_igra_sen_us[cycle_n] +hiv*(1-us)*sim_igra_sen_nus_hiv[cycle_n] + (1-hiv)*(1-us)*sim_igra_sen_nus[cycle_n])*0.762*test),
    
    ini_L0t =(1-notest) * d_l0 *(( (hiv*us)*sim_tst_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_tst_sen_us[cycle_n] +hiv*(1-us)*sim_tst_sen_nus_hiv[cycle_n] +(1-hiv)*(1-us)*sim_tst_sen_nus[cycle_n])*0.762*(1-test) + ((hiv*us)*sim_igra_sen_us_hiv[cycle_n] +(1-hiv)*us*sim_igra_sen_us[cycle_n] +hiv*(1-us)*sim_igra_sen_nus_hiv[cycle_n] + (1-hiv)*(1-us)*sim_igra_sen_nus[cycle_n])*0.762*test),
    
    ini_I= 1-d_s-d_ls-d_lf-d_l0,
    ini_It = 0, 
    ini_R=0,
   ini_D=0,
   
  p_SD = 1 - exp( - r_SD * cycle_length),
  #p_SSt = 0
  
  p_LFD = (1- exp(-(r_LFD)*cycle_length)),
  #p_LFLFt = 0
  p_LFI = (1- exp(-(r_LFI)*cycle_length)),
  
  p_LSD = (1-exp(-(r_LSD)*cycle_length)),
  #p_LSLSt = 0 
  
  p_LSL0 = ((0.03/12)/((0.03/12)+r_LSI))*(1-exp(-((0.03/12)+r_LSI)*cycle_length)),
  p_LSI =(r_LSI/((0.03/12)+r_LSI))*(1-exp(-((0.03/12)+r_LSI)*cycle_length)),
  
  p_L0D = 1 -exp(-r_L0D*cycle_length),
  #P_L0L0t = 0
  
  p_StD = (1 -exp(-r_StD*cycle_length)) + sim_d_4R[cycle_n]*sim_s_4R[cycle_n]*t4r +sim_d_3HP[cycle_n]*sim_s_3HP[cycle_n]*t3hp + sim_d_1HP[cycle_n]*sim_s_1HP[cycle_n]*t1hp+sim_d_6H[cycle_n]*sim_s_6H[cycle_n]*t6h ,
  # p_StS = 0/1  # if we assume complete treatment rationally, then no one would go back. After treatment, all go back to S
  
  p_LFtD = (1 -exp(-r_LFtD*cycle_length)) + sim_d_4R[cycle_n]*sim_s_4R[cycle_n]*t4r +sim_d_3HP[cycle_n]*sim_s_3HP[cycle_n]*t3hp + sim_d_1HP[cycle_n]*sim_s_1HP[cycle_n]*t1hp+sim_d_6H[cycle_n]*sim_s_6H[cycle_n]*t6h,# if complete cycle
  p_LFtL0 = sim_e_4R[cycle_n]*t4r + sim_e_3HP[cycle_n]*t3hp + sim_e_1HP[cycle_n]*t1hp + sim_e_6H[cycle_n]*t6h,
  p_LFtLF = 1- (sim_e_4R[cycle_n]*t4r + sim_e_3HP[cycle_n]*t3hp + sim_e_1HP[cycle_n]*t1hp + sim_e_6H[cycle_n]*t6h), # if complete cycle
  
  p_LStD = (1 -exp(-r_LStD*cycle_length)) + sim_d_4R[cycle_n]*sim_s_4R[cycle_n]*t4r +sim_d_3HP[cycle_n]*sim_s_3HP[cycle_n]*t3hp + sim_d_1HP[cycle_n]*sim_s_1HP[cycle_n]*t1hp+sim_d_6H[cycle_n]*sim_s_6H[cycle_n]*t6h,
  p_LStL0 = sim_e_4R[cycle_n]*t4r + sim_e_3HP[cycle_n]*t3hp + sim_e_1HP[cycle_n]*t1hp + sim_e_6H[cycle_n]*t6h,# if complete cycle
  p_LStLS = 1- (sim_e_4R[cycle_n]*t4r + sim_e_3HP[cycle_n]*t3hp + sim_e_1HP[cycle_n]*t1hp + sim_e_6H[cycle_n]*t6h), # if complete cycle
  
  p_L0tD = (1 -exp(-r_L0tD*cycle_length)) + sim_d_4R[cycle_n]*sim_s_4R[cycle_n]*t4r +sim_d_3HP[cycle_n]*sim_s_3HP[cycle_n]*t3hp + sim_d_1HP[cycle_n]*sim_s_1HP[cycle_n]*t1hp+sim_d_6H[cycle_n]*sim_s_6H[cycle_n]*t6h,
  p_L0tL0 = 1, #if complete cycle,
  
  p_ID = (1 -exp(-(r_ID)*cycle_length)),

  p_IR = 0,# assumed, need update # after 6 cycle also assumed
  p_IIt =(1 -exp(-(r_IIt)*cycle_length)), 
  
  p_ItD = (1 -exp(-(r_ItD)*cycle_length)),
  p_ItR = (1 -exp(-(r_ItR)*cycle_length)), 
  p_ItI = 0, #Assume stick to the treatment
 
  
  p_RD = 1-exp(-r_RD*cycle_length),
  
  p_tcom=sim_p_tcom[cycle_n],
  
  tb_r_mort_by_age= tb_r_mort_by_age,
  
  r_ID=r_ID,
  r_ItD = r_ItD,
  r_ItR = r_ItR
  
  
  
  

  #p_DD = 1
  )
  cat("The original tb mortaliry rate")
  print(v_tb_HDage)
  cat("The incidence state mortality risk factor")
   print(r_ID)
   plot(r_ID)
   cat("The incidence treatment state mortality risk factor")
   print(r_ItD)
   plot(r_ItD)
   cat("Ensure the risk factor is 1")
   print(mean(r_ID/v_tb_HDage))
   cat("The treatmetn completion rate(monthly)")
   print(r_ItR)
   cat("The treatmetn initiation rate(monthly)")
   print(r_IIt)
  cat("The incidence state mortality probability")
   print(p_values[["p_ID"]])
   plot(p_values[["p_ID"]])
  cat("The incidence treatment state mortality probability")
  print(p_values[["p_ItD"]])
   plot(p_values[["p_ItD"]])
   
  return (p_values)
  

}

RCPP function

cppFunction('
List updateProbabilies_v1 (int n_age_init, int n_age_max, NumericVector p_values, 
                               double t4r, double t3hp, double t1hp, double t6h, 
                               double t_4R, double t_3HP, double t_1HP, double t_6H, 
                               std::vector<double> v_p_SS, std::vector<double> v_p_SD, 
                               std::vector<double> a_P_LFLF,  
                               std::vector<double> a_P_LSLS,  
                               std::vector<double> a_P_L0L0, std::vector<double> a_P_LSL0, 
                               double a_P_com,std::vector<double> a_P_LFtL0,
                               std::vector<double> a_P_LFtLF, std::vector<double> a_P_LStL0,
                               std::vector<double> a_P_LStLS,
                               std::vector<double> a_P_II, std::vector<double> a_P_LFI, 
                               std::vector<double> a_P_LSI, std::vector<double> a_P_ItI,
                               std::vector<double> a_P_ItIt, std::vector<double> a_P_IIt, 
                               std::vector<double> a_P_RR,std::vector<double> a_P_IR, 
                               std::vector<double> a_P_ItR, double a_P_DD, std::vector<double> a_P_LFD, 
                              std::vector<double> a_P_LSD, std::vector<double> a_P_L0D, 
                               std::vector<double> a_P_StD, std::vector<double> a_P_LFtD, 
                               std::vector<double> a_P_LStD, std::vector<double> a_P_L0tD, 
                               std::vector<double> a_P_ID, std::vector<double> a_P_ItD, std::vector<double> a_P_RD, double cycle_length, double n_state) {
  int n_cycles = (n_age_max - n_age_init) / cycle_length * 12; 
  
  double m_M_native[n_cycles][12]; 
  double lifein[n_cycles][3];


 
  for (int j = 0; j < n_state; ++j) {
    m_M_native[0][j] = p_values[j]; 
  }
  
  lifein [0][0]= 0;
  lifein [0][1]= 0;
  lifein [0][2]= 0;
  
  double ltbi_tcycle = t4r*4 + t3hp*3+ t1hp*1 + t6h*6; 
  //Rcpp::Rcout << "ltbi_tcycle: " << ltbi_tcycle << std::endl;
  if (ltbi_tcycle == 0) {
        for (int cycle = 0; cycle < n_cycles-1; cycle++) {
            m_M_native[cycle+1][0] = m_M_native[cycle][0] * v_p_SS[cycle]; // Update for S
            m_M_native[cycle+1][1] = m_M_native[cycle][1] * a_P_LFLF[cycle]; // Update for LF
            m_M_native[cycle+1][2] = m_M_native[cycle][2] * a_P_LSLS[cycle]; // Update for LS
            m_M_native[cycle+1][3] = m_M_native[cycle][3] * a_P_L0L0[cycle] + m_M_native[cycle][2] * a_P_LSL0[cycle]; // Update for L0
           m_M_native[cycle+1][4] = m_M_native[cycle][4] - m_M_native[cycle][4]*a_P_StD[cycle];
  //LFt
  m_M_native[cycle+1][5] = m_M_native[cycle][5]- m_M_native[cycle][5]*a_P_LFtD[cycle];
  //LSt
  m_M_native[cycle+1][6] = m_M_native[cycle][6]-m_M_native[cycle][6]*a_P_LStD[cycle];
  //L0t
  m_M_native[cycle+1][7] = m_M_native[cycle][7]-m_M_native[cycle][7]*a_P_L0tD[cycle];
  //I
  m_M_native[cycle+1][8] =  m_M_native[cycle][8]* a_P_II[cycle] +m_M_native[cycle][1]*a_P_LFI[cycle]+m_M_native[cycle][2]*a_P_LSI[cycle]+m_M_native[cycle][9]*a_P_ItI[cycle];
  //It
  m_M_native[cycle+1][9] = m_M_native[cycle][8]*a_P_IIt[cycle] + m_M_native[cycle][9]*a_P_ItIt[cycle] ;
  //R
  m_M_native[cycle+1][10] = m_M_native[cycle][10]*a_P_RR[cycle] +m_M_native[cycle][8]*a_P_IR[cycle]+ m_M_native[cycle][9]*a_P_ItR[cycle];
  //D
  m_M_native[cycle+1][11] = m_M_native[cycle][11]*a_P_DD+ m_M_native[cycle][0]*v_p_SD[cycle]+m_M_native[cycle][1]*a_P_LFD[cycle]
  +m_M_native[cycle][2]*a_P_LSD[cycle]+ m_M_native[cycle][3]*a_P_L0D[cycle] + m_M_native[cycle][4]*a_P_StD[cycle] +
  m_M_native[cycle][5]*a_P_LFtD[cycle]+ m_M_native[cycle][6]*a_P_LStD[cycle]+ m_M_native[cycle][7]*a_P_L0tD[cycle]+
  m_M_native[cycle][8]*a_P_ID[cycle]+ m_M_native[cycle][9]*a_P_ItD[cycle]+ m_M_native[cycle][10]*a_P_RD[cycle];
  
  // transition proportion
  //Cumulative life time incidence
  lifein[cycle + 1][0] = m_M_native[cycle][1] * a_P_LFI[cycle] + m_M_native[cycle][2] * a_P_LSI[cycle];
 //Cumulative mortality
  lifein[cycle + 1][1] = m_M_native[cycle][8] *a_P_ID[cycle];
  lifein[cycle + 1][2] =  m_M_native[cycle][9] *a_P_ItD[cycle];
        }
    } else  {
    for (int cycle = 0; cycle < 1; cycle++) {
           m_M_native[cycle+1][0] = m_M_native[cycle][0] * v_p_SS[cycle]; // Update for S
            m_M_native[cycle+1][1] = m_M_native[cycle][1] * a_P_LFLF[cycle]; // Update for LF
            m_M_native[cycle+1][2] = m_M_native[cycle][2] * a_P_LSLS[cycle]; // Update for LS
            m_M_native[cycle+1][3] = m_M_native[cycle][3] * a_P_L0L0[cycle] + m_M_native[cycle][2] * a_P_LSL0[cycle]; // Update for L0
           m_M_native[cycle+1][4] = m_M_native[cycle][4] - m_M_native[cycle][4]*a_P_StD[cycle];
  //LFt
  m_M_native[cycle+1][5] = m_M_native[cycle][5]- m_M_native[cycle][5]*a_P_LFtD[cycle];
  //LSt
  m_M_native[cycle+1][6] = m_M_native[cycle][6]-m_M_native[cycle][6]*a_P_LStD[cycle];
  //L0t
  m_M_native[cycle+1][7] = m_M_native[cycle][7]-m_M_native[cycle][7]*a_P_L0tD[cycle];
  // I
  m_M_native[cycle+1][8] = m_M_native[cycle][8]* a_P_II[cycle]+ m_M_native[cycle][1]*a_P_LFI[cycle]+m_M_native[cycle][2]*a_P_LSI[cycle]+m_M_native[cycle][9]*a_P_ItI[cycle];
  //It
  m_M_native[cycle+1][9] =  m_M_native[cycle][8]*a_P_IIt[cycle]+ m_M_native[cycle][9]*a_P_ItIt[cycle] ;
  //R
  m_M_native[cycle+1][10] = m_M_native[cycle][10]*a_P_RR[cycle] +m_M_native[cycle][8]*a_P_IR[cycle]+ m_M_native[cycle][9]*a_P_ItR[cycle];
  //D
  m_M_native[cycle+1][11] = m_M_native[cycle][11]*a_P_DD+ m_M_native[cycle][0]*v_p_SD[cycle]+m_M_native[cycle][1]*a_P_LFD[cycle]
  +m_M_native[cycle][2]*a_P_LSD[cycle]+ m_M_native[cycle][3]*a_P_L0D[cycle] + m_M_native[cycle][4]*a_P_StD[cycle] +
  m_M_native[cycle][5]*a_P_LFtD[cycle]+ m_M_native[cycle][6]*a_P_LStD[cycle]+ m_M_native[cycle][7]*a_P_L0tD[cycle]+
  m_M_native[cycle][8]*a_P_ID[cycle]+ m_M_native[cycle][9]*a_P_ItD[cycle]+ m_M_native[cycle][10]*a_P_RD[cycle];
  // transition proportion
  //Cumulative life time incidence
  lifein[cycle + 1][0] = m_M_native[cycle][1] * a_P_LFI[cycle] + m_M_native[cycle][2] * a_P_LSI[cycle];
 //Cumulative mortality
  lifein[cycle + 1][1] = m_M_native[cycle][8] *a_P_ID[cycle];
  lifein[cycle + 1][2] =  m_M_native[cycle][9] *a_P_ItD[cycle];
        }
     for (int cycle = 1; cycle < 2; cycle++) {
           m_M_native[cycle+1][0] = m_M_native[cycle][0] * v_p_SS[cycle]+  (m_M_native[cycle][4] - m_M_native[cycle][4]*a_P_StD[cycle])*(1-a_P_com); // Update for S
            m_M_native[cycle+1][1] = m_M_native[cycle][1] * a_P_LFLF[cycle]+(m_M_native[cycle][5]- m_M_native[cycle][5]*a_P_LFtD[cycle])*(1-a_P_com); // Update for LF
            m_M_native[cycle+1][2] = m_M_native[cycle][2] * a_P_LSLS[cycle] + (m_M_native[cycle][6]-m_M_native[cycle][6]*a_P_LStD[cycle])*(1-a_P_com); // Update for LS
            m_M_native[cycle+1][3] = m_M_native[cycle][3] * a_P_L0L0[cycle] + m_M_native[cycle][2] * a_P_LSL0[cycle] + (m_M_native[cycle][7]-m_M_native[cycle][7]*a_P_L0tD[cycle])*(1-a_P_com); // Update for L0
           m_M_native[cycle+1][4] = (m_M_native[cycle][4] - m_M_native[cycle][4]*a_P_StD[cycle])*a_P_com;
  //LFt
  m_M_native[cycle+1][5] = (m_M_native[cycle][5]- m_M_native[cycle][5]*a_P_LFtD[cycle])*a_P_com;
  //LSt
  m_M_native[cycle+1][6] = (m_M_native[cycle][6]-m_M_native[cycle][6]*a_P_LStD[cycle])*a_P_com;
  //L0t
  m_M_native[cycle+1][7] = (m_M_native[cycle][7]-m_M_native[cycle][7]*a_P_L0tD[cycle])*a_P_com;
  // I
  m_M_native[cycle+1][8] = m_M_native[cycle][8]* a_P_II[cycle]+ m_M_native[cycle][1]*a_P_LFI[cycle]+m_M_native[cycle][2]*a_P_LSI[cycle]+m_M_native[cycle][9]*a_P_ItI[cycle];
  //It
  m_M_native[cycle+1][9] =  m_M_native[cycle][8]*a_P_IIt[cycle]+ m_M_native[cycle][9]*a_P_ItIt[cycle] ;
  //R
  m_M_native[cycle+1][10] = m_M_native[cycle][10]*a_P_RR[cycle] +m_M_native[cycle][8]*a_P_IR[cycle]+ m_M_native[cycle][9]*a_P_ItR[cycle];
  //D
  m_M_native[cycle+1][11] = m_M_native[cycle][11]*a_P_DD+ m_M_native[cycle][0]*v_p_SD[cycle]+m_M_native[cycle][1]*a_P_LFD[cycle]
  +m_M_native[cycle][2]*a_P_LSD[cycle]+ m_M_native[cycle][3]*a_P_L0D[cycle] + m_M_native[cycle][4]*a_P_StD[cycle] +
  m_M_native[cycle][5]*a_P_LFtD[cycle]+ m_M_native[cycle][6]*a_P_LStD[cycle]+ m_M_native[cycle][7]*a_P_L0tD[cycle]+
  m_M_native[cycle][8]*a_P_ID[cycle]+ m_M_native[cycle][9]*a_P_ItD[cycle]+ m_M_native[cycle][10]*a_P_RD[cycle];
  // transition proportion
  //Cumulative life time incidence
  lifein[cycle + 1][0] = m_M_native[cycle][1] * a_P_LFI[cycle] + m_M_native[cycle][2] * a_P_LSI[cycle];
 //Cumulative mortality
  lifein[cycle + 1][1] = m_M_native[cycle][8] *a_P_ID[cycle];
  lifein[cycle + 1][2] =  m_M_native[cycle][9] *a_P_ItD[cycle];
     }
    
        for (int cycle = 2; cycle < ltbi_tcycle; cycle++) {
           m_M_native[cycle+1][0] = m_M_native[cycle][0] * v_p_SS[cycle]; // Update for S
            m_M_native[cycle+1][1] = m_M_native[cycle][1] * a_P_LFLF[cycle]; // Update for LF
            m_M_native[cycle+1][2] = m_M_native[cycle][2] * a_P_LSLS[cycle]; // Update for LS
            m_M_native[cycle+1][3] = m_M_native[cycle][3] * a_P_L0L0[cycle] + m_M_native[cycle][2] * a_P_LSL0[cycle]; // Update for L0
           m_M_native[cycle+1][4] = m_M_native[cycle][4] - m_M_native[cycle][4]*a_P_StD[cycle];
  //LFt
  m_M_native[cycle+1][5] = m_M_native[cycle][5]- m_M_native[cycle][5]*a_P_LFtD[cycle];
  //LSt
  m_M_native[cycle+1][6] = m_M_native[cycle][6]-m_M_native[cycle][6]*a_P_LStD[cycle];
  //L0t
  m_M_native[cycle+1][7] = m_M_native[cycle][7]-m_M_native[cycle][7]*a_P_L0tD[cycle];
  // I
  m_M_native[cycle+1][8] = m_M_native[cycle][8]* a_P_II[cycle]+ m_M_native[cycle][1]*a_P_LFI[cycle]+m_M_native[cycle][2]*a_P_LSI[cycle]+m_M_native[cycle][9]*a_P_ItI[cycle];
  //It
  m_M_native[cycle+1][9] =  m_M_native[cycle][8]*a_P_IIt[cycle]+ m_M_native[cycle][9]*a_P_ItIt[cycle] ;
  //R
  m_M_native[cycle+1][10] = m_M_native[cycle][10]*a_P_RR[cycle] +m_M_native[cycle][8]*a_P_IR[cycle]+ m_M_native[cycle][9]*a_P_ItR[cycle];
  //D
  m_M_native[cycle+1][11] = m_M_native[cycle][11]*a_P_DD+ m_M_native[cycle][0]*v_p_SD[cycle]+m_M_native[cycle][1]*a_P_LFD[cycle]
  +m_M_native[cycle][2]*a_P_LSD[cycle]+ m_M_native[cycle][3]*a_P_L0D[cycle] + m_M_native[cycle][4]*a_P_StD[cycle] +
  m_M_native[cycle][5]*a_P_LFtD[cycle]+ m_M_native[cycle][6]*a_P_LStD[cycle]+ m_M_native[cycle][7]*a_P_L0tD[cycle]+
  m_M_native[cycle][8]*a_P_ID[cycle]+ m_M_native[cycle][9]*a_P_ItD[cycle]+ m_M_native[cycle][10]*a_P_RD[cycle];
  // transition proportion
  //Cumulative life time incidence
  lifein[cycle + 1][0] = m_M_native[cycle][1] * a_P_LFI[cycle] + m_M_native[cycle][2] * a_P_LSI[cycle];
 //Cumulative mortality
  lifein[cycle + 1][1] = m_M_native[cycle][8] *a_P_ID[cycle];
  lifein[cycle + 1][2] =  m_M_native[cycle][9] *a_P_ItD[cycle];
        }
        for (int cycle = ltbi_tcycle; cycle < n_cycles-1; cycle++) {
           m_M_native[cycle+1][0] = m_M_native[cycle][0] * v_p_SS[cycle] +m_M_native[cycle][4] ; // Update for S
            m_M_native[cycle+1][1] = m_M_native[cycle][1] * a_P_LFLF[cycle] + m_M_native[cycle][5]*a_P_LFtLF[cycle]; // Update for LF
            m_M_native[cycle+1][2] = m_M_native[cycle][2] * a_P_LSLS[cycle] + m_M_native[cycle][6]*a_P_LStLS[cycle]; // Update for LS
            m_M_native[cycle+1][3] = m_M_native[cycle][3] * a_P_L0L0[cycle] + m_M_native[cycle][2] * a_P_LSL0[cycle] +m_M_native[cycle][5]*a_P_LFtL0[cycle] + m_M_native[cycle][6]*a_P_LStL0[cycle] + m_M_native[cycle][7] ; // Update for L0
           m_M_native[cycle+1][4] = 0;//update for St
  //LFt
  m_M_native[cycle+1][5] = 0;
  //LSt
  m_M_native[cycle+1][6] = 0;
  //L0t
  m_M_native[cycle+1][7] = 0;
  //I
  m_M_native[cycle+1][8] =  m_M_native[cycle][8]* a_P_II[cycle]+ m_M_native[cycle][1]*a_P_LFI[cycle]+m_M_native[cycle][2]*a_P_LSI[cycle]+m_M_native[cycle][9]*a_P_ItI[cycle];
  //It
  m_M_native[cycle+1][9] = m_M_native[cycle][8]*a_P_IIt[cycle]+ m_M_native[cycle][9]*a_P_ItIt[cycle] ;
  //R
  m_M_native[cycle+1][10] = m_M_native[cycle][10]*a_P_RR[cycle] +m_M_native[cycle][8]*a_P_IR[cycle]+ m_M_native[cycle][9]*a_P_ItR[cycle];
  //D
  m_M_native[cycle+1][11] = m_M_native[cycle][11]*a_P_DD+ m_M_native[cycle][0]*v_p_SD[cycle]+m_M_native[cycle][1]*a_P_LFD[cycle]
  +m_M_native[cycle][2]*a_P_LSD[cycle]+ m_M_native[cycle][3]*a_P_L0D[cycle] + m_M_native[cycle][4]*a_P_StD[cycle] +
  m_M_native[cycle][5]*a_P_LFtD[cycle]+ m_M_native[cycle][6]*a_P_LStD[cycle]+ m_M_native[cycle][7]*a_P_L0tD[cycle]+
  m_M_native[cycle][8]*a_P_ID[cycle]+ m_M_native[cycle][9]*a_P_ItD[cycle]+ m_M_native[cycle][10]*a_P_RD[cycle];
    // transition proportion
  //Cumulative life time incidence
  lifein[cycle + 1][0] = m_M_native[cycle][1] * a_P_LFI[cycle] + m_M_native[cycle][2] * a_P_LSI[cycle];
 //Cumulative mortality
  lifein[cycle + 1][1] = m_M_native[cycle][8] *a_P_ID[cycle];
  lifein[cycle + 1][2] =  m_M_native[cycle][9] *a_P_ItD[cycle];
        }}
       
    
  NumericMatrix m_M(n_cycles,n_state);
  NumericMatrix transit(n_cycles,3);
  
  for(int i = 0; i < n_cycles-1; ++i) {
    for(int j = 0; j < n_state; ++j) {
      m_M(i, j) = m_M_native[i][j];
    }
    for(int j = 0; j < 3; ++j) {
      transit(i, j) = lifein[i][j];
    }
    
  }
  
  double case_averted = (m_M(ltbi_tcycle,5) + m_M(ltbi_tcycle,6) + m_M(ltbi_tcycle,7));
  
  return List::create(Named("m_M") = m_M, Named("transit") = transit, Named("case_averted") = case_averted);

}')

Main function

main_model = function(age_init, gender, hiv, dia, gas, renal, immu, trans, can, bmi, kid, smoke, ltc, test, prison, homeless, us, t4r, t3hp, t1hp, t6h, ttb, notest, d_s, d_lf, d_ls, d_l0, d_i, cycle_length, cycle_n){
  p_values = generate_p2(n_age_start =age_init, gender=gender, hiv=hiv, dia=dia, gas=gas, renal=renal, immu=immu, trans=trans, can=can, bmi=bmi, kid=kid, smoke=smoke, ltc=ltc, test=test, prison=prison, homeless=homeless, us=us, t4r=t4r, t3hp=t3hp,t1hp=t1hp, t6h=t6h, ttb=ttb,notest=notest,d_s=d_s, d_lf=d_lf, d_ls=d_ls, d_l0=d_l0,  cycle_length=1, cycle_n = cycle_n)
  p_values_vector= unlist(p_values)
  # from s
  v_p_SS<- (1 - p_values[["p_SD"]]) 
  v_p_SSt <- 0
  v_p_SD <- p_values[["p_SD"]]
  # From LS 
  a_P_LSLSt <- 0
  a_P_LSL0 <- (1 - p_values[["p_LSD"]]) * p_values [["p_LSL0"]]
  a_P_LSI <- (1 - p_values[["p_LSD"]]) * p_values [["p_LSI"]]
  a_P_LSLS <- (1 - p_values[["p_LSD"]]) * (1-(p_values [["p_LSI"]] + p_values [["p_LSL0"]]))
  a_P_LSD <- p_values[["p_LSD"]]
  # From LF
  a_P_LFLFt <- 0
  a_P_LFI <- (1 - p_values[["p_LFD"]]) * p_values [["p_LFI"]]
  a_P_LFLF <- (1 - p_values[["p_LFD"]]) * (1-p_values [["p_LFI"]])
  a_P_LFD <- p_values[["p_LFD"]]
  # From L0
  a_P_L0L0t <-0
  a_P_L0L0 <- (1 - p_values[["p_L0D"]]) 
  a_P_L0D <- p_values[["p_L0D"]]
  # From St
  a_P_StS <- 1
  a_P_StSt <- (1-p_values[["p_StD"]])*(1-p_values[["p_StS"]])
  a_P_StD <- p_values[["p_StD"]]
  # From LSt
  a_P_LStLS <- (1 - p_values[["p_LStD"]]) * p_values [["p_LStLS"]]
  
  
  a_P_LStL0 <- (1 - p_values[["p_LStD"]]) * p_values [["p_LStL0"]]
  a_P_LStLSt <- 1
  a_P_LStD <- p_values[["p_LStD"]]
  # From LFt
  a_P_LFtLF <- (1 - p_values[["p_LFtD"]]) * p_values [["p_LFtLF"]]
  a_P_LFtL0 <- (1 - p_values[["p_LFtD"]]) * p_values [["p_LFtL0"]]
  a_P_LFtLFt <- 1
  a_P_LFtD <- p_values[["p_LFtD"]]
  # From L0t
  a_P_L0tL0 <- (1 - p_values[["p_L0tD"]]) * p_values [["p_L0tL0"]]
  a_P_L0tL0t <- 1
  a_P_L0tD <- p_values[["p_L0tD"]]
  # From I
  a_P_II <- (1 - p_values[["p_ID"]]) * (1-p_values [["p_IIt"]]-p_values [["p_IR"]])
  a_P_IIt <- (1 - p_values[["p_ID"]]) * p_values [["p_IIt"]]
  a_P_IR <- (1 - p_values[["p_ID"]]) * p_values [["p_IR"]]
  a_P_ID <- p_values[["p_ID"]]
  cat("The I to D transition probability")
  print(a_P_ID)
  # From It
  a_P_ItIt <- (1 - p_values[["p_ItD"]])*(1-p_values [["p_ItI"]]-p_values [["p_ItR"]])
  a_P_ItI <- (1 - p_values[["p_ItD"]]) * p_values [["p_ItI"]]
  a_P_ItR <- (1 - p_values[["p_ItD"]]) * p_values [["p_ItR"]]
  a_P_ItD <- p_values[["p_ItD"]]
  cat("The It to D transition probability")
  print(a_P_ItD)
  # From R
  a_P_RR <- (1-p_values[["p_RD"]])
  a_P_RD <- p_values[["p_RD"]]
  # From D 
  a_P_DD<- 1
  
  a_P_com <- p_values[["p_tcom"]]
  
  result <- updateProbabilies_v1 (n_age_init=age_init, 100, p_values_vector, 
                               t4r=t4r, t3hp=t3hp, t1hp=t1hp, t6h=t6h,
                               t_4R, t_3HP,  t_1HP, t_6H, 
                               v_p_SS,  v_p_SD, 
                               a_P_LFLF,
                               a_P_LSLS, 
                               a_P_L0L0, a_P_LSL0, 
                               a_P_com,a_P_LFtL0,
                               a_P_LFtLF, a_P_LStL0,
                              a_P_LStLS,
                               a_P_II,  a_P_LFI, 
                               a_P_LSI, a_P_ItI,
                                a_P_ItIt,a_P_IIt, 
                                a_P_RR,  a_P_IR, 
                                a_P_ItR, a_P_DD, a_P_LFD, 
                               a_P_LSD,  a_P_L0D, 
                                a_P_StD,a_P_LFtD, 
                              a_P_LStD,  a_P_L0tD, 
                                a_P_ID,  a_P_ItD, a_P_RD,
                               cycle_length=1, n_states)
  return (result)
    
}
# treatment duration
t_4R = 4
t_3HP = 3
t_1HP = 1
t_6H = 6
# Utilities
u_S <- 1 # montly utility of being Healthy 
u_LS <- 1# montly utility of being LS
u_LF <- 1 # montly untilty of being LF
u_L0 <- 1  # montly untilty of being L0
u_St <- 1
u_St_tox <- 0.75
u_LSt <- 1 # annual utility of being LSt
u_LSt_tox <- 0.75
u_LFt <- 1 # annual untilty of being LFt
u_LFt_tox <- 0.75
u_L0t <- 1  # annual untilty of being L0
u_L0t_tox <- 0.75
u_I <- 0.76 # annual utility of being TB incidence
u_It_tox <- 0.76
u_D <- 0   # annual utility of being dead
u_R <- 0.9
u_It <-  0.76
v_names_states <- c("S", "LF", "LS", "L0",  "St", "LFt", "LSt", "L0t","I", "It", "R", "D")
n_states <- length(v_names_states)

Test and chek

c=  main_model(age_init = 60, gender=1, hiv=0, dia=0, gas=0, renal=0, immu=0, trans=0, can=0, bmi=0, kid=0, smoke=0, ltc=0, test=1, prison=0, homeless=0, us=1, t4r=1, t3hp=0,t1hp=0, t6h=0, ttb=0,notest=0,d_s=0, d_lf=1, d_ls=0, d_l0=0, d_i=0, cycle_length=1, cycle_n = 1)
## The original tb mortaliry rate  [1] 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668
##   [7] 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668
##  [13] 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052
##  [19] 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052
##  [25] 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932
##  [31] 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932
##  [37] 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332
##  [43] 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332
##  [49] 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715
##  [55] 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715
##  [61] 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412
##  [67] 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412
##  [73] 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079
##  [79] 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079
##  [85] 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539
##  [91] 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539
##  [97] 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262
## [103] 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262
## [109] 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433
## [115] 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433
## [121] 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244
## [127] 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244
## [133] 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972
## [139] 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972
## [145] 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648
## [151] 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648
## [157] 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279
## [163] 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279
## [169] 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375
## [175] 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375
## [181] 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804
## [187] 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804
## [193] 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333
## [199] 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333
## [205] 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008
## [211] 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008
## [217] 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437
## [223] 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437
## [229] 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312
## [235] 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312
## [241] 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274
## [247] 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274
## [253] 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970
## [259] 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970
## [265] 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888
## [271] 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888
## [277] 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747
## [283] 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747
## [289] 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827
## [295] 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827
## [301] 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043
## [307] 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043
## [313] 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666
## [319] 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666
## [325] 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292
## [331] 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292
## [337] 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329
## [343] 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329
## [349] 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116
## [355] 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116
## [361] 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292
## [367] 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292
## [373] 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519
## [379] 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519
## [385] 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151
## [391] 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151
## [397] 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912
## [403] 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912
## [409] 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583
## [415] 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583
## [421] 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706
## [427] 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706
## [433] 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359
## [439] 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359
## [445] 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997
## [451] 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997
## [457] 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412
## [463] 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412
## [469] 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799
## [475] 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799
## The incidence state mortality risk factor  [1] 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668
##   [7] 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668
##  [13] 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052
##  [19] 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052
##  [25] 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932
##  [31] 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932
##  [37] 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332
##  [43] 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332
##  [49] 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715
##  [55] 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715
##  [61] 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412
##  [67] 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412
##  [73] 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079
##  [79] 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079
##  [85] 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539
##  [91] 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539
##  [97] 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262
## [103] 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262
## [109] 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433
## [115] 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433
## [121] 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244
## [127] 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244
## [133] 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972
## [139] 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972
## [145] 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648
## [151] 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648
## [157] 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279
## [163] 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279
## [169] 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375
## [175] 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375
## [181] 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804
## [187] 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804
## [193] 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333
## [199] 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333
## [205] 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008
## [211] 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008
## [217] 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437
## [223] 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437
## [229] 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312
## [235] 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312
## [241] 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274
## [247] 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274
## [253] 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970
## [259] 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970
## [265] 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888
## [271] 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888
## [277] 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747
## [283] 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747
## [289] 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827
## [295] 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827
## [301] 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043
## [307] 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043
## [313] 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666
## [319] 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666
## [325] 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292
## [331] 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292
## [337] 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329
## [343] 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329
## [349] 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116
## [355] 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116
## [361] 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292
## [367] 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292
## [373] 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519
## [379] 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519
## [385] 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151
## [391] 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151
## [397] 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912
## [403] 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912
## [409] 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583
## [415] 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583
## [421] 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706
## [427] 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706
## [433] 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359
## [439] 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359
## [445] 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997
## [451] 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997
## [457] 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412
## [463] 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412
## [469] 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799
## [475] 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799

## The incidence treatment state mortality risk factor  [1] 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668
##   [7] 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668 0.01475668
##  [13] 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052
##  [19] 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052 0.01592052
##  [25] 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932
##  [31] 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932 0.01708932
##  [37] 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332
##  [43] 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332 0.01824332
##  [49] 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715
##  [55] 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715 0.01941715
##  [61] 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412
##  [67] 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412 0.01373412
##  [73] 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079
##  [79] 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079 0.01473079
##  [85] 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539
##  [91] 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539 0.01576539
##  [97] 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262
## [103] 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262 0.01685262
## [109] 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433
## [115] 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433 0.01799433
## [121] 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244
## [127] 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244 0.01920244
## [133] 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972
## [139] 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972 0.02061972
## [145] 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648
## [151] 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648 0.02203648
## [157] 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279
## [163] 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279 0.02440279
## [169] 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375
## [175] 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375 0.02654375
## [181] 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804
## [187] 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804 0.01403804
## [193] 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333
## [199] 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333 0.01538333
## [205] 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008
## [211] 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008 0.01711008
## [217] 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437
## [223] 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437 0.01887437
## [229] 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312
## [235] 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312 0.02086312
## [241] 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274
## [247] 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274 0.02300274
## [253] 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970
## [259] 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970 0.02549970
## [265] 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888
## [271] 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888 0.02843888
## [277] 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747
## [283] 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747 0.03171747
## [289] 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827
## [295] 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827 0.03549827
## [301] 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043
## [307] 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043 0.06605043
## [313] 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666
## [319] 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666 0.07378666
## [325] 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292
## [331] 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292 0.08312292
## [337] 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329
## [343] 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329 0.09346329
## [349] 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116
## [355] 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116 0.10487116
## [361] 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292
## [367] 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292 0.11740292
## [373] 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519
## [379] 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519 0.13110519
## [385] 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151
## [391] 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151 0.14601151
## [397] 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912
## [403] 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912 0.16213912
## [409] 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583
## [415] 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583 0.17948583
## [421] 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706
## [427] 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706 0.19802706
## [433] 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359
## [439] 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359 0.21771359
## [445] 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997
## [451] 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997 0.23846997
## [457] 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412
## [463] 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412 0.26019412
## [469] 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799
## [475] 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799 0.28275799

## Ensure the risk factor is 1[1] 1
## The treatmetn completion rate(monthly)[1] 0.125
## The treatmetn initiation rate(monthly)[1] 0.25
## The incidence state mortality probability  [1] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##   [7] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##  [13] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [19] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [25] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [31] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [37] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [43] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [49] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [55] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [61] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [67] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [73] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [79] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [85] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [91] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [97] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [103] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [109] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [115] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [121] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [127] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [133] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [139] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [145] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [151] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [157] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [163] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [169] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [175] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [181] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [187] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [193] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [199] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [205] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [211] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [217] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [223] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [229] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [235] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [241] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [247] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [253] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [259] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [265] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [271] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [277] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [283] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [289] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [295] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [301] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [307] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [313] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [319] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [325] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [331] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [337] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [343] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [349] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [355] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [361] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [367] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [373] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [379] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [385] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [391] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [397] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [403] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [409] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [415] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [421] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [427] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [433] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [439] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [445] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [451] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [457] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [463] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [469] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783
## [475] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783

## The incidence treatment state mortality probability  [1] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##   [7] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##  [13] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [19] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [25] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [31] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [37] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [43] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [49] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [55] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [61] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [67] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [73] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [79] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [85] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [91] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [97] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [103] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [109] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [115] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [121] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [127] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [133] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [139] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [145] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [151] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [157] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [163] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [169] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [175] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [181] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [187] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [193] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [199] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [205] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [211] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [217] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [223] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [229] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [235] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [241] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [247] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [253] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [259] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [265] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [271] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [277] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [283] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [289] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [295] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [301] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [307] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [313] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [319] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [325] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [331] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [337] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [343] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [349] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [355] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [361] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [367] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [373] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [379] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [385] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [391] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [397] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [403] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [409] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [415] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [421] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [427] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [433] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [439] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [445] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [451] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [457] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [463] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [469] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783
## [475] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783

## The I to D transition probability  [1] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##   [7] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##  [13] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [19] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [25] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [31] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [37] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [43] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [49] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [55] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [61] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [67] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [73] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [79] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [85] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [91] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [97] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [103] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [109] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [115] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [121] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [127] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [133] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [139] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [145] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [151] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [157] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [163] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [169] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [175] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [181] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [187] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [193] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [199] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [205] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [211] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [217] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [223] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [229] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [235] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [241] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [247] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [253] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [259] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [265] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [271] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [277] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [283] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [289] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [295] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [301] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [307] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [313] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [319] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [325] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [331] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [337] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [343] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [349] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [355] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [361] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [367] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [373] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [379] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [385] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [391] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [397] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [403] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [409] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [415] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [421] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [427] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [433] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [439] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [445] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [451] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [457] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [463] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [469] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783
## [475] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783
## The It to D transition probability  [1] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##   [7] 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834 0.01464834
##  [13] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [19] 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446 0.01579446
##  [25] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [31] 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412 0.01694412
##  [37] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [43] 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792 0.01807792
##  [49] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [55] 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985 0.01922985
##  [61] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [67] 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023 0.01364023
##  [73] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [79] 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282 0.01462282
##  [85] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [91] 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177 0.01564177
##  [97] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [103] 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141 0.01671141
## [109] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [115] 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340 0.01783340
## [121] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [127] 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925 0.01901925
## [133] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [139] 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858 0.02040858
## [145] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [151] 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545 0.02179545
## [157] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [163] 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745 0.02410745
## [169] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [175] 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457 0.02619457
## [181] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [187] 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997 0.01393997
## [193] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [199] 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561 0.01526561
## [205] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [211] 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453 0.01696453
## [217] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [223] 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737 0.01869737
## [229] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [235] 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699 0.02064699
## [241] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [247] 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019 0.02274019
## [253] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [259] 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733 0.02517733
## [265] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [271] 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830 0.02803830
## [277] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [283] 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974 0.03121974
## [289] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [295] 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560 0.03487560
## [301] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [307] 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634 0.06391634
## [313] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [319] 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016 0.07113016
## [325] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [331] 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198 0.07976198
## [337] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [343] 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855 0.08922855
## [349] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [355] 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947 0.09955947
## [361] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [367] 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317 0.11077317
## [373] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [379] 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449 0.12287449
## [385] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [391] 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224 0.13585224
## [397] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [403] 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710 0.14967710
## [409] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [415] 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021 0.16430021
## [421] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [427] 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234 0.17965234
## [433] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [439] 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422 0.19564422
## [445] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [451] 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765 0.21216765
## [457] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [463] 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808 0.22909808
## [469] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783
## [475] 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783 0.24629783
transitc=c$transit

The transitc has three column, column 1: incidence, column 2: the mortality in Incidence state, column 3: the mortality in Incidence treatment state.

datatable(
data = transitc,
caption = "Table 1: the outcome (column 1: incidence, column 2: I state death, column 3: It state death)",
filter = "top",
style = "bootstrap4"
)

The incidence plot

plot(transitc[,1])

The CFR calculation

cat("The incidence is: ", sum(transitc[,1]), "\n")
## The incidence is:  0.004564769
cat("The mortality of I is: ", sum(transitc[,2]), "\n")
## The mortality of I is:  0.0005400072
cat("The mortality of It is: ", sum(transitc[,3]), "\n")
## The mortality of It is:  0.0007433464
cat("The CFR of I is: ", sum(transitc[,2])/sum(transitc[,1]), "\n")
## The CFR of I is:  0.1182989
cat("The CFR of It is: ", sum(transitc[,3])/sum(transitc[,1]), "\n")
## The CFR of It is:  0.1628442
cat("The CFR  is: ", (sum(transitc[,3])+ sum(transitc[,2]))/sum(transitc[,1]), "\n")
## The CFR  is:  0.2811432

The average age of have incidence is

age_vector <- seq(from = 60, by = 1/12, length.out = nrow(transitc))
weighted_sum_ages <- sum(transitc[,1] * age_vector)
sum_weights <- sum(transitc[,1])
average_age_incidence <- weighted_sum_ages / sum_weights

# Print the result
print(average_age_incidence)
## [1] 74.64691

The tb overall mortality rate at incidence age is:

print(mor[mor$age == 74&mor$sex ==1, "tb_mortality_rate"]*12)
##   tb_mortality_rate
## 1         0.3185251

The CFR at incidence age is about 21%.