Chilean prosecutor’s office Data merge (Step 5)

Show code
script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"
Show code
 $(document).ready(function() {
    $('body').prepend('<div class=\"zoomDiv\"><img src=\"\" class=\"zoomImg\"></div>');
    // onClick function for all plots (img's)
    $('img:not(.zoomImg)').click(function() {
      $('.zoomImg').attr('src', $(this).attr('src')).css({width: '100%'});
      $('.zoomDiv').css({opacity: '1', width: 'auto', border: '1px solid white', borderRadius: '5px', position: 'fixed', top: '50%', left: '50%', marginRight: '-50%', transform: 'translate(-50%, -50%)', boxShadow: '0px 0px 50px #888888', zIndex: '50', overflow: 'auto', maxHeight: '100%'});
    });
    // onClick function for zoomImg
    $('img.zoomImg').click(function() {
      $('.zoomDiv').css({opacity: '0', width: '0%'}); 
    });
  });
  
Show code
<script src="hideOutput.js"></script> 
Show code
$(document).ready(function() {    
    $chunks = $('.fold');    
    $chunks.each(function () {      // add button to source code chunks     
    if ( $(this).hasClass('s') ) {       
        $('pre.r', this).prepend("<div class=\"showopt\">Show Source</div><br style=\"line-height:22px;\"/>");
            $('pre.r', this).children('code').attr('class', 'folded');     
            }      // add button to output chunks     
        if ( $(this).hasClass('o') ) {       
            $('pre:not(.r)', this).has('code').prepend("<div class=\"showopt\">Show Output</div><br style=\"line-height:22px;\"/>");       
            $('pre:not(.r)', this).children('code:not(r)').addClass('folded');        // add button to plots       
            $(this).find('img').wrap('<pre class=\"plot\"></pre>');       
            $('pre.plot', this).prepend("<div class=\"showopt\">Show Plot</div><br style=\"line-height:22px;\"/>");       
            $('pre.plot', this).children('img').addClass('folded');      
            }   
});    // hide all chunks when document is loaded   
    $('.folded').css('display', 'none')    // function to toggle the visibility   
    $('.showopt').click(function() {     
            var label = $(this).html();     
            if (label.indexOf("Show") >= 0) {       
                $(this).html(label.replace("Show", "Hide"));     
            } else {
              $(this).html(label.replace("Hide", "Show"));     
            }     
    $(this).siblings('code, img').slideToggle('fast', 'swing');   
    }); 
}); 


Compare Stata weights

Show code
     # tryCatch(load(paste0(sub("2019","2022",sub("SUD_CL","",path)),"15.RData")),
     #          error= load(paste0(sub("2019","2022",sub("SUD_CL","",path)),"/15.RData")))
mariel_stata_sentence<-rio::import("mariel_nov_22.dta") #no prison
mariel_stata_prison<-rio::import("mariel_nov_22_2.dta") # prison

#rpdf10_m_stag_ten_viv rpdf10_m_nostag_or_ing_num_hij gomp_m_nostag_ten_viv gomp_m_nostag_or_ing_num_hij rpdf6_m_nostag_ten_viv rpdf6_m_nostag_or_ing_num_hij rpdf10_m_stag_ten_viv rpdf10_m_stag_or_ing_num_hij gomp_m_stag_ten_viv gomp_m_stag_or_ing_num_hij rpdf6_m_stag_ten_viv rpdf6_m_stag_or_ing_num_hij

vec_vars_stata<-
c("event", "motivodeegreso_mod_imp_rec2", "edad_al_ing_1", "edad_ini_cons", "sex_enc", "esc_rec", "sus_prin_mod", "fr_sus_prin", "comp_biosoc", "ten_viv", "dg_cie_10_rec", "sud_severity_icd10", "macrozone", "policonsumo", "n_off_vio", "n_off_acq", "n_off_sud", "clas")

warning(paste0("The sample fell from ",format(nrow(mariel_stata_sentence), big.mark=",")," to ",format(nrow(mariel_stata_sentence[complete.cases(mariel_stata_sentence[, c(vec_vars_stata)]),]), big.mark=","), " observations without missing data in the variables of interest, and to ", format(nrow(mariel_stata_sentence[complete.cases(mariel_stata_sentence[, c(vec_vars_stata, "rpdf10_m_stag_ten_viv", "rpdf10_m_nostag_or_ing_num_hij", "gomp_m_nostag_ten_viv", "gomp_m_nostag_or_ing_num_hij", "rpdf6_m_nostag_ten_viv", "rpdf6_m_nostag_or_ing_num_hij")]),]), big.mark=","), " if erased also the rows that possess missing values in the weights. We only eliminated missing data in the treatment weights (n=", format(nrow(mariel_stata_sentence[complete.cases(mariel_stata_sentence[, c("rpdf10_m_stag_ten_viv", "rpdf10_m_nostag_or_ing_num_hij", "gomp_m_nostag_ten_viv", "gomp_m_nostag_or_ing_num_hij", "rpdf6_m_nostag_ten_viv", "rpdf6_m_nostag_or_ing_num_hij")]),]), big.mark=","),")"))


mariel_stata_sentence<-
  mariel_stata_sentence %>% 
  dplyr::mutate(across(c("sex_enc","esc_rec", "sus_prin_mod", "fr_sus_prin", "comp_biosoc", "ten_viv", "dg_cie_10_rec", "sud_severity_icd10", "macrozone", "policonsumo", "n_off_vio", "n_off_acq", "n_off_sud", "clas", "motivodeegreso_mod_imp_rec3"),~as.factor(.)))
  

bal_tab2<-
cobalt::bal.tab(motivodeegreso_mod_imp_rec2 ~ edad_al_ing_1+ edad_ini_cons + sex_enc + esc_rec + sus_prin_mod + fr_sus_prin + comp_biosoc+ ten_viv + dg_cie_10_rec + sud_severity_icd10+ macrozone + policonsumo+ n_off_vio + n_off_acq+ n_off_sud + clas, data = mariel_stata_sentence[complete.cases(mariel_stata_sentence[, c("rpdf10_m_stag_ten_viv", "rpdf10_m_nostag_or_ing_num_hij", "gomp_m_nostag_ten_viv", "gomp_m_nostag_or_ing_num_hij", "rpdf6_m_nostag_ten_viv", "rpdf6_m_nostag_or_ing_num_hij")]),],
                weights = list("RP(df=10)+ Tenure status households" = "rpdf10_m_stag_ten_viv", "RP(df=10)+ Motive of admission & No. of children"="rpdf10_m_nostag_or_ing_num_hij", "Gompertz+ Tenure status households"="gomp_m_nostag_ten_viv", "Gompertz+ Motive of admission & No. of children"="gomp_m_nostag_or_ing_num_hij", "RP(df=6)+ Tenure status households"="rpdf6_m_nostag_ten_viv", "RP(df=6)+ Motive of admission & No. of children"="rpdf6_m_nostag_or_ing_num_hij"),
                estimand = "ATE",
                which.treat = .all,
                un = T, 
                thresholds = c(cor = .2),
                 binary = "std", continuous = "std",
                stats = c("mean.diffs", "variance.ratios"))
invisible("Error: NAs are not allowed in the weights.")

bal_tab2$Balance %>% 
  dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Balance Measures (multinomial response weights)")
Table 1: Balance Measures (multinomial response weights)
Type Diff.Un V.Ratio.Un Diff.RP(df=10)+ Tenure status households V.Ratio.RP(df=10)+ Tenure status households Diff.RP(df=10)+ Motive of admission & No. of children V.Ratio.RP(df=10)+ Motive of admission & No. of children Diff.Gompertz+ Tenure status households V.Ratio.Gompertz+ Tenure status households Diff.Gompertz+ Motive of admission & No. of children V.Ratio.Gompertz+ Motive of admission & No. of children Diff.RP(df=6)+ Tenure status households V.Ratio.RP(df=6)+ Tenure status households Diff.RP(df=6)+ Motive of admission & No. of children V.Ratio.RP(df=6)+ Motive of admission & No. of children
edad_al_ing_1 Contin. -0.39 0.76 0.00 0.98 0.00 0.99 0.00 0.98 0.00 0.99 0.00 0.98 0.00 0.99
edad_ini_cons Contin. -0.17 0.67 0.00 1.02 0.00 1.03 0.00 1.02 0.00 1.03 0.00 1.02 0.00 1.03
sex_enc_2 Binary -0.08 0.00 0.00 0.00 0.00 0.00 0.00
esc_rec_1 Binary -0.16 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
esc_rec_2 Binary 0.06 0.03 0.02 0.03 0.02 0.03 0.02
esc_rec_3 Binary 0.07 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
sus_prin_mod_1 Binary -0.32 -0.05 -0.04 -0.05 -0.04 -0.05 -0.04
sus_prin_mod_2 Binary 0.07 0.02 0.01 0.02 0.01 0.02 0.01
sus_prin_mod_3 Binary 0.30 0.10 0.09 0.10 0.09 0.10 0.09
sus_prin_mod_4 Binary -0.01 -0.09 -0.08 -0.09 -0.08 -0.09 -0.08
sus_prin_mod_5 Binary -0.08 -0.07 -0.06 -0.07 -0.06 -0.07 -0.06
fr_sus_prin_1 Binary -0.06 -0.04 -0.04 -0.04 -0.04 -0.04 -0.04
fr_sus_prin_2 Binary -0.01 0.02 0.02 0.02 0.02 0.02 0.02
fr_sus_prin_3 Binary 0.04 0.04 0.04 0.04 0.04 0.04 0.04
fr_sus_prin_4 Binary 0.03 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
fr_sus_prin_5 Binary -0.05 -0.03 -0.03 -0.03 -0.03 -0.03 -0.03
comp_biosoc_1 Binary -0.09 -0.03 -0.03 -0.03 -0.03 -0.03 -0.03
comp_biosoc_2 Binary 0.01 0.05 0.05 0.05 0.05 0.05 0.05
comp_biosoc_3 Binary 0.05 -0.03 -0.03 -0.03 -0.03 -0.03 -0.03
ten_viv_1 Binary 0.01 0.02 0.00 0.02 0.00 0.02 0.00
ten_viv_2 Binary 0.02 0.03 0.02 0.03 0.02 0.03 0.02
ten_viv_3 Binary -0.14 -0.03 -0.06 -0.03 -0.06 -0.03 -0.06
ten_viv_4 Binary 0.00 0.02 0.01 0.02 0.01 0.02 0.01
ten_viv_5 Binary 0.13 0.00 0.04 0.00 0.04 0.00 0.04
dg_cie_10_rec_1 Binary -0.24 -0.22 -0.23 -0.22 -0.23 -0.22 -0.23
dg_cie_10_rec_2 Binary 0.73 0.70 0.70 0.70 0.70 0.70 0.70
dg_cie_10_rec_3 Binary -0.23 -0.24 -0.23 -0.24 -0.23 -0.24 -0.23
sud_severity_icd10_2 Binary -0.08 0.00 0.01 0.00 0.01 0.00 0.01
macrozone_1 Binary 0.15 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
macrozone_2 Binary 0.01 0.04 0.04 0.04 0.04 0.04 0.04
macrozone_3 Binary -0.22 -0.03 -0.02 -0.03 -0.02 -0.03 -0.02
policonsumo Binary 0.24 0.00 0.00 0.00 0.00 0.00 0.00
n_off_vio Binary 0.11 0.00 0.01 0.00 0.01 0.00 0.01
n_off_acq Binary 0.23 0.00 0.00 0.00 0.00 0.00 0.00
n_off_sud Binary 0.09 0.00 0.00 0.00 0.00 0.00 0.00
clas_1 Binary -0.04 0.01 0.01 0.01 0.01 0.01 0.01
clas_2 Binary -0.10 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
clas_3 Binary 0.10 0.01 0.00 0.01 0.00 0.01 0.00
Show code
  #knitr::kable("html", caption="Balance Measures") %>% 
  #   kableExtra::kable_classic_2()

invisible("differences in dg_cie10_rec")

Time for this code chunk to run: 0.2 minutes

Show code
#The “effective sample size” (ESS) is a measure of the sample size a non-weighted sample would have to have to achieve the same level of precision as the weighted sample (Ridgeway 2006).
bal_tab2$Observations %>% 
  #dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Effective sample sizes")
Table 2: Effective sample sizes
Control Treated
All 16257.00 42963.00
RP(df=10)+ Tenure status households 13994.79 41806.23
RP(df=10)+ Motive of admission & No. of children 13906.60 41689.88
Gompertz+ Tenure status households 13994.79 41806.23
Gompertz+ Motive of admission & No. of children 13906.60 41689.88
RP(df=6)+ Tenure status households 13994.79 41806.23
RP(df=6)+ Motive of admission & No. of children 13906.60 41689.88

Time for this code chunk to run: 0 minutes

Now we compared against multi-valued treatments format.

Show code
bal_tab2b<-
cobalt::bal.tab(motivodeegreso_mod_imp_rec3 ~ edad_al_ing_1+ edad_ini_cons + sex_enc + esc_rec + sus_prin_mod + fr_sus_prin + comp_biosoc+ ten_viv + dg_cie_10_rec + sud_severity_icd10+ macrozone + policonsumo+ n_off_vio + n_off_acq+ n_off_sud + clas, data = mariel_stata_sentence[complete.cases(mariel_stata_sentence[, c("motivodeegreso_mod_imp_rec", "rpdf10_m_stag_ten_viv", "rpdf10_m_nostag_or_ing_num_hij", "gomp_m_nostag_ten_viv", "gomp_m_nostag_or_ing_num_hij", "rpdf6_m_nostag_ten_viv", "rpdf6_m_nostag_or_ing_num_hij")]),],
                weights = list("RP(df=10)+ Tenure status households" = "rpdf10_m_stag_ten_viv", "RP(df=10)+ Motive of admission & No. of children"="rpdf10_m_nostag_or_ing_num_hij", "Gompertz+ Tenure status households"="gomp_m_nostag_ten_viv", "Gompertz+ Motive of admission & No. of children"="gomp_m_nostag_or_ing_num_hij", "RP(df=6)+ Tenure status households"="rpdf6_m_nostag_ten_viv", "RP(df=6)+ Motive of admission & No. of children"="rpdf6_m_nostag_or_ing_num_hij"),
                estimand = "ATE",
                which.treat = .all,
                un = T, 
                thresholds = c(cor = .2),
                 binary = "std", continuous = "std",
                stats = c("mean.diffs", "variance.ratios"))
invisible("Error: The treatment must have at least two unique values.")

rbind(data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab2b$Pair.Balance$`2 vs. 1`$Balance), keep.rownames=T),
      data.table::data.table(cbind(comp="Late Dropout vs. Tr. completion",bal_tab2b$Pair.Balance$`3 vs. 1`$Balance), keep.rownames=T),
      data.table::data.table(cbind(comp="Late vs. Early Dropout",bal_tab2b$Pair.Balance$`3 vs. 2`$Balance), keep.rownames=T)) %>% 
  data.table::data.table(keep.rownames = T) %>% 
  dplyr::select(comp, rn, everything()) %>% 
  dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Balance Measures (multinomial response weights)")
Table 3: Balance Measures (multinomial response weights)
comp rn Type Diff.Un V.Ratio.Un Diff.RP(df=10)+ Tenure status households V.Ratio.RP(df=10)+ Tenure status households Diff.RP(df=10)+ Motive of admission & No. of children V.Ratio.RP(df=10)+ Motive of admission & No. of children Diff.Gompertz+ Tenure status households V.Ratio.Gompertz+ Tenure status households Diff.Gompertz+ Motive of admission & No. of children V.Ratio.Gompertz+ Motive of admission & No. of children Diff.RP(df=6)+ Tenure status households V.Ratio.RP(df=6)+ Tenure status households Diff.RP(df=6)+ Motive of admission & No. of children V.Ratio.RP(df=6)+ Motive of admission & No. of children
Early Dropout vs. Tr. completion edad_al_ing_1 Contin. -0.50 0.71 -0.11 0.91 -0.12 0.93 -0.11 0.91 -0.12 0.93 -0.11 0.91 -0.12 0.93
Early Dropout vs. Tr. completion edad_ini_cons Contin. -0.22 0.61 -0.05 0.92 -0.05 0.92 -0.05 0.92 -0.05 0.92 -0.05 0.92 -0.05 0.92
Early Dropout vs. Tr. completion sex_enc_2 Binary -0.09 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
Early Dropout vs. Tr. completion esc_rec_1 Binary -0.19 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05
Early Dropout vs. Tr. completion esc_rec_2 Binary 0.07 0.04 0.04 0.04 0.04 0.04 0.04
Early Dropout vs. Tr. completion esc_rec_3 Binary 0.08 0.00 0.00 0.00 0.00 0.00 0.00
Early Dropout vs. Tr. completion sus_prin_mod_1 Binary -0.43 -0.16 -0.15 -0.16 -0.15 -0.16 -0.15
Early Dropout vs. Tr. completion sus_prin_mod_2 Binary 0.05 0.01 0.00 0.01 0.00 0.01 0.00
Early Dropout vs. Tr. completion sus_prin_mod_3 Binary 0.42 0.23 0.22 0.23 0.22 0.23 0.22
Early Dropout vs. Tr. completion sus_prin_mod_4 Binary -0.03 -0.11 -0.10 -0.11 -0.10 -0.11 -0.10
Early Dropout vs. Tr. completion sus_prin_mod_5 Binary -0.10 -0.09 -0.08 -0.09 -0.08 -0.09 -0.08
Early Dropout vs. Tr. completion fr_sus_prin_1 Binary -0.11 -0.09 -0.09 -0.09 -0.09 -0.09 -0.09
Early Dropout vs. Tr. completion fr_sus_prin_2 Binary -0.08 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05
Early Dropout vs. Tr. completion fr_sus_prin_3 Binary 0.04 0.04 0.04 0.04 0.04 0.04 0.04
Early Dropout vs. Tr. completion fr_sus_prin_4 Binary 0.14 0.10 0.10 0.10 0.10 0.10 0.10
Early Dropout vs. Tr. completion fr_sus_prin_5 Binary -0.10 -0.08 -0.08 -0.08 -0.08 -0.08 -0.08
Early Dropout vs. Tr. completion comp_biosoc_1 Binary -0.14 -0.08 -0.08 -0.08 -0.08 -0.08 -0.08
Early Dropout vs. Tr. completion comp_biosoc_2 Binary -0.06 -0.02 -0.01 -0.02 -0.01 -0.02 -0.01
Early Dropout vs. Tr. completion comp_biosoc_3 Binary 0.15 0.07 0.07 0.07 0.07 0.07 0.07
Early Dropout vs. Tr. completion ten_viv_1 Binary 0.04 0.05 0.03 0.05 0.03 0.05 0.03
Early Dropout vs. Tr. completion ten_viv_2 Binary 0.01 0.02 0.01 0.02 0.01 0.02 0.01
Early Dropout vs. Tr. completion ten_viv_3 Binary -0.17 -0.06 -0.09 -0.06 -0.09 -0.06 -0.09
Early Dropout vs. Tr. completion ten_viv_4 Binary 0.02 0.03 0.03 0.03 0.03 0.03 0.03
Early Dropout vs. Tr. completion ten_viv_5 Binary 0.14 0.02 0.06 0.02 0.06 0.02 0.06
Early Dropout vs. Tr. completion dg_cie_10_rec_1 Binary -0.64 -0.62 -0.63 -0.62 -0.63 -0.62 -0.63
Early Dropout vs. Tr. completion dg_cie_10_rec_2 Binary 1.76 1.75 1.75 1.75 1.75 1.75 1.75
Early Dropout vs. Tr. completion dg_cie_10_rec_3 Binary -0.63 -0.64 -0.63 -0.64 -0.63 -0.64 -0.63
Early Dropout vs. Tr. completion sud_severity_icd10_2 Binary -0.15 -0.07 -0.07 -0.07 -0.07 -0.07 -0.07
Early Dropout vs. Tr. completion macrozone_1 Binary 0.09 -0.08 -0.08 -0.08 -0.08 -0.08 -0.08
Early Dropout vs. Tr. completion macrozone_2 Binary 0.12 0.15 0.14 0.15 0.14 0.15 0.14
Early Dropout vs. Tr. completion macrozone_3 Binary -0.27 -0.07 -0.06 -0.07 -0.06 -0.07 -0.06
Early Dropout vs. Tr. completion policonsumo Binary 0.30 0.06 0.06 0.06 0.06 0.06 0.06
Early Dropout vs. Tr. completion n_off_vio Binary 0.14 0.04 0.04 0.04 0.04 0.04 0.04
Early Dropout vs. Tr. completion n_off_acq Binary 0.31 0.09 0.09 0.09 0.09 0.09 0.09
Early Dropout vs. Tr. completion n_off_sud Binary 0.14 0.06 0.06 0.06 0.06 0.06 0.06
Early Dropout vs. Tr. completion clas_1 Binary -0.08 -0.03 -0.03 -0.03 -0.03 -0.03 -0.03
Early Dropout vs. Tr. completion clas_2 Binary -0.17 -0.09 -0.09 -0.09 -0.09 -0.09 -0.09
Early Dropout vs. Tr. completion clas_3 Binary 0.18 0.09 0.09 0.09 0.09 0.09 0.09
Late Dropout vs. Tr. completion edad_al_ing_1 Contin. -0.35 0.78 0.05 1.00 0.04 1.01 0.05 1.00 0.04 1.01 0.05 1.00 0.04 1.01
Late Dropout vs. Tr. completion edad_ini_cons Contin. -0.16 0.70 0.02 1.06 0.03 1.08 0.02 1.06 0.03 1.08 0.02 1.06 0.03 1.08
Late Dropout vs. Tr. completion sex_enc_2 Binary -0.08 0.01 0.01 0.01 0.01 0.01 0.01
Late Dropout vs. Tr. completion esc_rec_1 Binary -0.15 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
Late Dropout vs. Tr. completion esc_rec_2 Binary 0.05 0.02 0.02 0.02 0.02 0.02 0.02
Late Dropout vs. Tr. completion esc_rec_3 Binary 0.06 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
Late Dropout vs. Tr. completion sus_prin_mod_1 Binary -0.29 0.00 0.00 0.00 0.00 0.00 0.00
Late Dropout vs. Tr. completion sus_prin_mod_2 Binary 0.08 0.02 0.02 0.02 0.02 0.02 0.02
Late Dropout vs. Tr. completion sus_prin_mod_3 Binary 0.24 0.04 0.04 0.04 0.04 0.04 0.04
Late Dropout vs. Tr. completion sus_prin_mod_4 Binary 0.00 -0.09 -0.07 -0.09 -0.07 -0.09 -0.07
Late Dropout vs. Tr. completion sus_prin_mod_5 Binary -0.09 -0.07 -0.06 -0.07 -0.06 -0.07 -0.06
Late Dropout vs. Tr. completion fr_sus_prin_1 Binary -0.04 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
Late Dropout vs. Tr. completion fr_sus_prin_2 Binary 0.02 0.05 0.05 0.05 0.05 0.05 0.05
Late Dropout vs. Tr. completion fr_sus_prin_3 Binary 0.04 0.04 0.04 0.04 0.04 0.04 0.04
Late Dropout vs. Tr. completion fr_sus_prin_4 Binary -0.02 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05
Late Dropout vs. Tr. completion fr_sus_prin_5 Binary -0.03 -0.01 -0.02 -0.01 -0.02 -0.01 -0.02
Late Dropout vs. Tr. completion comp_biosoc_1 Binary -0.07 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
Late Dropout vs. Tr. completion comp_biosoc_2 Binary 0.04 0.08 0.08 0.08 0.08 0.08 0.08
Late Dropout vs. Tr. completion comp_biosoc_3 Binary 0.00 -0.08 -0.08 -0.08 -0.08 -0.08 -0.08
Late Dropout vs. Tr. completion ten_viv_1 Binary 0.00 0.00 -0.01 0.00 -0.01 0.00 -0.01
Late Dropout vs. Tr. completion ten_viv_2 Binary 0.02 0.03 0.02 0.03 0.02 0.03 0.02
Late Dropout vs. Tr. completion ten_viv_3 Binary -0.13 -0.02 -0.05 -0.02 -0.05 -0.02 -0.05
Late Dropout vs. Tr. completion ten_viv_4 Binary 0.00 0.01 0.01 0.01 0.01 0.01 0.01
Late Dropout vs. Tr. completion ten_viv_5 Binary 0.12 0.00 0.03 0.00 0.03 0.00 0.03
Late Dropout vs. Tr. completion dg_cie_10_rec_1 Binary -0.09 -0.07 -0.07 -0.07 -0.07 -0.07 -0.07
Late Dropout vs. Tr. completion dg_cie_10_rec_2 Binary 0.22 0.20 0.20 0.20 0.20 0.20 0.20
Late Dropout vs. Tr. completion dg_cie_10_rec_3 Binary -0.07 -0.08 -0.07 -0.08 -0.07 -0.08 -0.07
Late Dropout vs. Tr. completion sud_severity_icd10_2 Binary -0.05 0.03 0.04 0.03 0.04 0.03 0.04
Late Dropout vs. Tr. completion macrozone_1 Binary 0.18 0.01 0.01 0.01 0.01 0.01 0.01
Late Dropout vs. Tr. completion macrozone_2 Binary -0.04 0.00 -0.01 0.00 -0.01 0.00 -0.01
Late Dropout vs. Tr. completion macrozone_3 Binary -0.22 -0.01 0.00 -0.01 0.00 -0.01 0.00
Late Dropout vs. Tr. completion policonsumo Binary 0.22 -0.03 -0.03 -0.03 -0.03 -0.03 -0.03
Late Dropout vs. Tr. completion n_off_vio Binary 0.09 -0.02 -0.01 -0.02 -0.01 -0.02 -0.01
Late Dropout vs. Tr. completion n_off_acq Binary 0.18 -0.04 -0.04 -0.04 -0.04 -0.04 -0.04
Late Dropout vs. Tr. completion n_off_sud Binary 0.06 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
Late Dropout vs. Tr. completion clas_1 Binary -0.03 0.03 0.03 0.03 0.03 0.03 0.03
Late Dropout vs. Tr. completion clas_2 Binary -0.07 0.01 0.01 0.01 0.01 0.01 0.01
Late Dropout vs. Tr. completion clas_3 Binary 0.07 -0.03 -0.03 -0.03 -0.03 -0.03 -0.03
Late vs. Early Dropout edad_al_ing_1 Contin. 0.15 1.11 0.16 1.09 0.16 1.09 0.16 1.09 0.16 1.09 0.16 1.09 0.16 1.09
Late vs. Early Dropout edad_ini_cons Contin. 0.07 1.15 0.07 1.16 0.08 1.17 0.07 1.16 0.08 1.17 0.07 1.16 0.08 1.17
Late vs. Early Dropout sex_enc_2 Binary 0.01 0.02 0.02 0.02 0.02 0.02 0.02
Late vs. Early Dropout esc_rec_1 Binary 0.04 0.04 0.04 0.04 0.04 0.04 0.04
Late vs. Early Dropout esc_rec_2 Binary -0.01 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
Late vs. Early Dropout esc_rec_3 Binary -0.02 -0.01 -0.01 -0.01 -0.01 -0.01 -0.01
Late vs. Early Dropout sus_prin_mod_1 Binary 0.14 0.16 0.16 0.16 0.16 0.16 0.16
Late vs. Early Dropout sus_prin_mod_2 Binary 0.02 0.02 0.02 0.02 0.02 0.02 0.02
Late vs. Early Dropout sus_prin_mod_3 Binary -0.18 -0.18 -0.18 -0.18 -0.18 -0.18 -0.18
Late vs. Early Dropout sus_prin_mod_4 Binary 0.03 0.03 0.03 0.03 0.03 0.03 0.03
Late vs. Early Dropout sus_prin_mod_5 Binary 0.01 0.01 0.01 0.01 0.01 0.01 0.01
Late vs. Early Dropout fr_sus_prin_1 Binary 0.07 0.07 0.07 0.07 0.07 0.07 0.07
Late vs. Early Dropout fr_sus_prin_2 Binary 0.10 0.10 0.10 0.10 0.10 0.10 0.10
Late vs. Early Dropout fr_sus_prin_3 Binary 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Late vs. Early Dropout fr_sus_prin_4 Binary -0.15 -0.15 -0.15 -0.15 -0.15 -0.15 -0.15
Late vs. Early Dropout fr_sus_prin_5 Binary 0.07 0.07 0.07 0.07 0.07 0.07 0.07
Late vs. Early Dropout comp_biosoc_1 Binary 0.07 0.08 0.08 0.08 0.08 0.08 0.08
Late vs. Early Dropout comp_biosoc_2 Binary 0.10 0.09 0.09 0.09 0.09 0.09 0.09
Late vs. Early Dropout comp_biosoc_3 Binary -0.15 -0.15 -0.15 -0.15 -0.15 -0.15 -0.15
Late vs. Early Dropout ten_viv_1 Binary -0.04 -0.04 -0.04 -0.04 -0.04 -0.04 -0.04
Late vs. Early Dropout ten_viv_2 Binary 0.01 0.01 0.01 0.01 0.01 0.01 0.01
Late vs. Early Dropout ten_viv_3 Binary 0.04 0.04 0.04 0.04 0.04 0.04 0.04
Late vs. Early Dropout ten_viv_4 Binary -0.02 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
Late vs. Early Dropout ten_viv_5 Binary -0.02 -0.02 -0.02 -0.02 -0.02 -0.02 -0.02
Late vs. Early Dropout dg_cie_10_rec_1 Binary 0.55 0.55 0.55 0.55 0.55 0.55 0.55
Late vs. Early Dropout dg_cie_10_rec_2 Binary -1.55 -1.55 -1.55 -1.55 -1.55 -1.55 -1.55
Late vs. Early Dropout dg_cie_10_rec_3 Binary 0.56 0.56 0.56 0.56 0.56 0.56 0.56
Late vs. Early Dropout sud_severity_icd10_2 Binary 0.11 0.11 0.11 0.11 0.11 0.11 0.11
Late vs. Early Dropout macrozone_1 Binary 0.09 0.09 0.08 0.09 0.08 0.09 0.08
Late vs. Early Dropout macrozone_2 Binary -0.15 -0.16 -0.16 -0.16 -0.16 -0.16 -0.16
Late vs. Early Dropout macrozone_3 Binary 0.05 0.07 0.07 0.07 0.07 0.07 0.07
Late vs. Early Dropout policonsumo Binary -0.07 -0.09 -0.09 -0.09 -0.09 -0.09 -0.09
Late vs. Early Dropout n_off_vio Binary -0.06 -0.05 -0.05 -0.05 -0.05 -0.05 -0.05
Late vs. Early Dropout n_off_acq Binary -0.14 -0.13 -0.13 -0.13 -0.13 -0.13 -0.13
Late vs. Early Dropout n_off_sud Binary -0.08 -0.08 -0.08 -0.08 -0.08 -0.08 -0.08
Late vs. Early Dropout clas_1 Binary 0.06 0.06 0.06 0.06 0.06 0.06 0.06
Late vs. Early Dropout clas_2 Binary 0.09 0.10 0.10 0.10 0.10 0.10 0.10
Late vs. Early Dropout clas_3 Binary -0.11 -0.12 -0.12 -0.12 -0.12 -0.12 -0.12
Show code
 #  knitr::kable("html", caption="Balance Measures") %>% 
   #   kableExtra::kable_classic_2()

invisible("0= completar; 1= no-completar;policonsumo looks not balanced, the number of previous acquisitive offenses, and South")

Time for this code chunk to run: 0.1 minutes

Show code
rbind(cbind(comp="Early Dropout vs. Tr. completion",bal_tab2b$Pair.Balance$`2 vs. 1`$Balance),
      cbind(comp="Late Dropout vs. Tr. completion",bal_tab2b$Pair.Balance$`3 vs. 1`$Balance),
      cbind(comp="Late vs. Early Dropout",bal_tab2b$Pair.Balance$`3 vs. 2`$Balance)) %>% 
    data.table::data.table(keep.rownames = T) %>% 
    dplyr::select(comp, rn, everything()) %>% 
    dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
    dplyr::summarise_if(is.numeric, ~list(min= min(.,na.rm=T), max= max(.,na.rm=T))) %>% t() %>%
  knitr::kable("markdown", caption="Balance Measures (multinomial response weights)", col.names=c("min", "max"))
Table 4: Balance Measures (multinomial response weights)
min max
Diff.Un -1.55 1.76
V.Ratio.Un 0.61 1.15
Diff.RP(df=10)+ Tenure status households -1.55 1.75
V.Ratio.RP(df=10)+ Tenure status households 0.91 1.16
Diff.RP(df=10)+ Motive of admission & No. of children -1.55 1.75
V.Ratio.RP(df=10)+ Motive of admission & No. of children 0.92 1.17
Diff.Gompertz+ Tenure status households -1.55 1.75
V.Ratio.Gompertz+ Tenure status households 0.91 1.16
Diff.Gompertz+ Motive of admission & No. of children -1.55 1.75
V.Ratio.Gompertz+ Motive of admission & No. of children 0.92 1.17
Diff.RP(df=6)+ Tenure status households -1.55 1.75
V.Ratio.RP(df=6)+ Tenure status households 0.91 1.16
Diff.RP(df=6)+ Motive of admission & No. of children -1.55 1.75
V.Ratio.RP(df=6)+ Motive of admission & No. of children 0.92 1.17
Show code
 #  knitr::kable("html", caption="Balance Measures (multinomial response weights)") %>% 
   #   kableExtra::kable_classic_2()

Time for this code chunk to run: 0 minutes

Show code
#The “effective sample size” (ESS) is a measure of the sample size a non-weighted sample would have to have to achieve the same level of precision as the weighted sample (Ridgeway 2006).

dplyr::bind_rows(data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab2b$Pair.Balance$`2 vs. 1`$Observations),keep.rownames = T), data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab2b$Pair.Balance$`3 vs. 1`$Observations),keep.rownames = T), data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab2b$Pair.Balance$`3 vs. 2`$Observations),keep.rownames = T)) %>% 
  #dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Effective sample sizes")
Table 5: Effective sample sizes
rn comp 1 2 3
All Early Dropout vs. Tr. completion 16257.00 13219.00
RP(df=10)+ Tenure status households Early Dropout vs. Tr. completion 13994.79 12886.51
RP(df=10)+ Motive of admission & No. of children Early Dropout vs. Tr. completion 13906.60 12859.10
Gompertz+ Tenure status households Early Dropout vs. Tr. completion 13994.79 12886.51
Gompertz+ Motive of admission & No. of children Early Dropout vs. Tr. completion 13906.60 12859.10
RP(df=6)+ Tenure status households Early Dropout vs. Tr. completion 13994.79 12886.51
RP(df=6)+ Motive of admission & No. of children Early Dropout vs. Tr. completion 13906.60 12859.10
All Early Dropout vs. Tr. completion 16257.00 29744.00
RP(df=10)+ Tenure status households Early Dropout vs. Tr. completion 13994.79 28926.83
RP(df=10)+ Motive of admission & No. of children Early Dropout vs. Tr. completion 13906.60 28839.63
Gompertz+ Tenure status households Early Dropout vs. Tr. completion 13994.79 28926.83
Gompertz+ Motive of admission & No. of children Early Dropout vs. Tr. completion 13906.60 28839.63
RP(df=6)+ Tenure status households Early Dropout vs. Tr. completion 13994.79 28926.83
RP(df=6)+ Motive of admission & No. of children Early Dropout vs. Tr. completion 13906.60 28839.63
All Early Dropout vs. Tr. completion 13219.00 29744.00
RP(df=10)+ Tenure status households Early Dropout vs. Tr. completion 12886.51 28926.83
RP(df=10)+ Motive of admission & No. of children Early Dropout vs. Tr. completion 12859.10 28839.63
Gompertz+ Tenure status households Early Dropout vs. Tr. completion 12886.51 28926.83
Gompertz+ Motive of admission & No. of children Early Dropout vs. Tr. completion 12859.10 28839.63
RP(df=6)+ Tenure status households Early Dropout vs. Tr. completion 12886.51 28926.83
RP(df=6)+ Motive of admission & No. of children Early Dropout vs. Tr. completion 12859.10 28839.63

Time for this code chunk to run: 0 minutes


Bring other databases

Show code
#http://observatorio.ministeriodesarrollosocial.gob.cl/pobreza-comunal-2020

Comunas_PNDR <- readxl::read_excel("Clasificacion-comunas-PNDR.xlsx")%>% 
  dplyr::mutate(cod= dplyr::case_when(as.character(cod_com)=="16101"~"8401",
                                      as.character(cod_com)=="16102"~"8402",
                                      as.character(cod_com)=="16103"~"8406",
                                      as.character(cod_com)=="16104"~"8407",
                                      as.character(cod_com)=="16105"~"8410",
                                      as.character(cod_com)=="16106"~"8411",
                                      as.character(cod_com)=="16107"~"8413",
                                      as.character(cod_com)=="16108"~"8418",
                                      as.character(cod_com)=="16109"~"8421",
                                      as.character(cod_com)=="16201"~"8414",
                                      as.character(cod_com)=="16202"~"8403",
                                      as.character(cod_com)=="16203"~"8404",
                                      as.character(cod_com)=="16204"~"8408",
                                      as.character(cod_com)=="16205"~"8412",
                                      as.character(cod_com)=="16206"~"8415",
                                      as.character(cod_com)=="16207"~"8420",
                                      as.character(cod_com)=="16301"~"8416",
                                      as.character(cod_com)=="16302"~"8405",
                                      as.character(cod_com)=="16303"~"8409",
                                      as.character(cod_com)=="16304"~"8417",
                                      as.character(cod_com)=="16305"~"8419",
                                      T~ as.character(cod_com)
                                      ))

#http://observatorio.ministeriodesarrollosocial.gob.cl/pobreza-comunal-2011
pobr_mult_2020<-readxl::read_excel("Estimaciones_de_Tasa_de_Pobreza_por_Ingresos_por_Comunas_2020_revisada2022_09.xlsx", skip=1) %>% dplyr::mutate(anio=2020) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2019<-readxl::read_excel("Estimaciones_de_Tasa_de_Pobreza_por_Ingresos_por_Comunas_2020_revisada2022_09.xlsx", skip=1) %>% dplyr::mutate(anio=2019) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2018<-readxl::read_excel("PLANILLA_Estimaciones_comunales_tasa_pobreza_por_ingresos_multidimensional_2017.xlsx", skip=1) %>% dplyr::mutate(anio=2018) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2017<-readxl::read_excel("PLANILLA_Estimaciones_comunales_tasa_pobreza_por_ingresos_multidimensional_2017.xlsx", skip=1) %>% dplyr::mutate(anio=2017) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2016<-readxl::read_excel("PLANILLA_Estimaciones_comunales_tasa_pobreza_por_ingresos_multidimensional_2015.xlsx", skip=1) %>% dplyr::mutate(anio=2016) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2015<-readxl::read_excel("PLANILLA_Estimaciones_comunales_tasa_pobreza_por_ingresos_multidimensional_2015.xlsx", skip=1) %>% dplyr::mutate(anio=2015) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2014<-readxl::read_excel("PLANILLA_Estimaciones_comunales_tasa_pobreza_por_ingresos_2013.xlsx", skip=1)%>% dplyr::mutate(anio=2014) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2013<-readxl::read_excel("PLANILLA_Estimaciones_comunales_tasa_pobreza_por_ingresos_2013.xlsx", skip=1)%>% dplyr::mutate(anio=2013) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2012<-readxl::read_excel("Estimacion_tasa_de_pobreza_comunal_2011_(nueva _metodologia).xlsx", skip=1)%>% dplyr::mutate(anio=2012) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2011<-readxl::read_excel("Estimacion_tasa_de_pobreza_comunal_2011_(nueva _metodologia).xlsx", skip=1)%>% dplyr::mutate(anio=2011) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2010<-readxl::read_excel("Estimacion_tasa_de_pobreza_comunal_2011_(nueva _metodologia).xlsx", skip=1)%>% dplyr::mutate(anio=2010) %>% dplyr::rename_at(vars( contains("Porcentaje de") ), ~"porc_pobr") %>% dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)
pobr_mult_2009<-readxl::read_excel("PobrezaporComunas_SAE_20092011.xlsx", skip=3)%>% dplyr::mutate(anio=2009) %>% dplyr::select(anio, everything()) %>% dplyr::select(1:5) %>%  dplyr::rename_at(vars( contains("Incidencia pobreza") ), ~"porc_pobr") %>% dplyr::rename("Código"=2, "Nombre comuna"=3) %>%  dplyr::mutate(Región=rep("")) %>%  dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)

New names: * -> `...1` * -> ...2 * Límite inferior IC -> Límite inferior IC...3 * Incidencia pobreza -> Incidencia pobreza...4 * Límite superior IC -> Límite superior IC...5 * Límite inferior IC -> Límite inferior IC...6 * Incidencia pobreza -> Incidencia pobreza...7 * Límite superior IC -> Límite superior IC...8 * -> `...9` * -> ...10

Show code
pobr_mult_2008<-readxl::read_excel("PobrezaporComunas_SAE_20092011.xlsx", skip=3)%>% dplyr::mutate(anio=2008) %>% dplyr::select(anio, everything()) %>% dplyr::select(1:5) %>%  dplyr::rename_at(vars( contains("Incidencia pobreza") ), ~"porc_pobr") %>% dplyr::rename("Código"=2, "Nombre comuna"=3) %>%  dplyr::mutate(Región=rep("")) %>%  dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)

New names: * -> `...1` * -> ...2 * Límite inferior IC -> Límite inferior IC...3 * Incidencia pobreza -> Incidencia pobreza...4 * Límite superior IC -> Límite superior IC...5 * Límite inferior IC -> Límite inferior IC...6 * Incidencia pobreza -> Incidencia pobreza...7 * Límite superior IC -> Límite superior IC...8 * -> `...9` * -> ...10

Show code
pobr_mult_2007<-readxl::read_excel("PobrezaporComunas_SAE_20092011.xlsx", skip=3)%>% dplyr::mutate(anio=2007) %>% dplyr::select(anio, everything()) %>% dplyr::select(1:5) %>%  dplyr::rename_at(vars( contains("Incidencia pobreza") ), ~"porc_pobr") %>% dplyr::rename("Código"=2, "Nombre comuna"=3) %>%  dplyr::mutate(Región=rep("")) %>%  dplyr::select(anio, Código, Región, `Nombre comuna`, porc_pobr)

New names: * -> `...1` * -> ...2 * Límite inferior IC -> Límite inferior IC...3 * Incidencia pobreza -> Incidencia pobreza...4 * Límite superior IC -> Límite superior IC...5 * Límite inferior IC -> Límite inferior IC...6 * Incidencia pobreza -> Incidencia pobreza...7 * Límite superior IC -> Límite superior IC...8 * -> `...9` * -> ...10

Show code
# replace comuna= "16103" if strpos(strlower(comuna),"8406")>0
# replace comuna= "16104" if strpos(strlower(comuna),"8407")>0
# replace comuna= "16105" if strpos(strlower(comuna),"8410")>0
# replace comuna= "16106" if strpos(strlower(comuna),"8411")>0
# replace comuna= "16107" if strpos(strlower(comuna),"8413")>0
# replace comuna= "16108" if strpos(strlower(comuna),"8418")>0
# replace comuna= "16109" if strpos(strlower(comuna),"8421")>0
# replace comuna= "16201" if strpos(strlower(comuna),"8414")>0
# replace comuna= "16202" if strpos(strlower(comuna),"8403")>0
# replace comuna= "16203" if strpos(strlower(comuna),"8404")>0
# replace comuna= "16204" if strpos(strlower(comuna),"8408")>0
# replace comuna= "16205" if strpos(strlower(comuna),"8412")>0
# replace comuna= "16206" if strpos(strlower(comuna),"8415")>0
# replace comuna= "16207" if strpos(strlower(comuna),"8420")>0
# replace comuna= "16301" if strpos(strlower(comuna),"8416")>0
# replace comuna= "16302" if strpos(strlower(comuna),"8405")>0
# replace comuna= "16303" if strpos(strlower(comuna),"8409")>0
# replace comuna= "16304" if strpos(strlower(comuna),"8417")>0
# replace comuna= "16305" if strpos(strlower(comuna),"8419")>0
pobr_mult_2007_2020<-
rbind.data.frame(pobr_mult_2007, pobr_mult_2008, pobr_mult_2009, pobr_mult_2010, pobr_mult_2011, pobr_mult_2012, pobr_mult_2013, pobr_mult_2014, pobr_mult_2015, pobr_mult_2016, pobr_mult_2017, pobr_mult_2018, pobr_mult_2019, pobr_mult_2020) %>% 
  dplyr::mutate(cod= dplyr::case_when(Código=="16101"~"8401",
                                      Código=="16102"~"8402",
                                      Código=="16103"~"8406",
                                      Código=="16104"~"8407",
                                      Código=="16105"~"8410",
                                      Código=="16106"~"8411",
                                      Código=="16107"~"8413",
                                      Código=="16108"~"8418",
                                      Código=="16109"~"8421",
                                      Código=="16201"~"8414",
                                      Código=="16202"~"8403",
                                      Código=="16203"~"8404",
                                      Código=="16204"~"8408",
                                      Código=="16205"~"8412",
                                      Código=="16206"~"8415",
                                      Código=="16207"~"8420",
                                      Código=="16301"~"8416",
                                      Código=="16302"~"8405",
                                      Código=="16303"~"8409",
                                      Código=="16304"~"8417",
                                      Código=="16305"~"8419",
                                      T~ Código
                                      ))


#2023-02-01:classifications by municipality
class_centros <- readxl::read_excel("_ig_borquez/class_centros.xlsx")%>%
    tidylog::left_join(CONS_C1_df_dup_SEP_2020[,c("id_centro","nombre_centro")], by=c("nombre_centro_1" ="nombre_centro"))%>%
    dplyr::group_by(id_centro)%>%
    slice(1)%>% 
    dplyr::ungroup()%>%
    dplyr::mutate(id_centro=dplyr::case_when(grepl("Allende",nombre_centro_1) & is.na(id_centro)~ "475",  T~ as.character(id_centro)))%>%
    dplyr::group_by(id_centro)%>%
    slice(1)

left_join: added one column (id_centro) > rows only in x 30 > rows only in y ( 4,611) > matched rows 105,145 (includes duplicates) > ========= > rows total 105,175

Show code
    #dplyr::filter(grepl("Allende",nombre_centro_1))


Base_fiscalia_v14<-
Base_fiscalia_v13 %>% 
  dplyr::mutate(comuna_residencia_cod_rec= as.character(readr::parse_number(comuna_residencia_cod)), anio_ing_tr= lubridate::epiyear(fech_ing)) %>% #glimpse()
  dplyr::left_join(pobr_mult_2007_2020[,c("anio", "cod","porc_pobr")], by= c("comuna_residencia_cod_rec"="cod", "anio_ing_tr"="anio")) %>% 
  dplyr::left_join(Comunas_PNDR[,c("cod", "Clasificación")], by= c("comuna_residencia_cod_rec"="cod"))%>%
  #2023-02-01
  dplyr::left_join(class_centros[,c("id_centro", "nombre_centro_1", "classification")], by= "id_centro")%>%
    purrr::when(nrow(.)>nrow(Base_fiscalia_v13) ~ stop("More cases in the new database"), ~.) %>% 
  dplyr::mutate(age_offending_imp= dplyr::case_when(age_offending_imp-edad_al_egres_imp<=0~ age_offending_imp+ 0.0001,T~age_offending_imp), event_r= ifelse(!is.na(offender_d),1,0)) %>% 
  dplyr::mutate(freq_cons_sus_prin= dplyr::case_when(as.character(freq_cons_sus_prin)=="Did not use"~"Less than 1 day a week", T~ as.character(freq_cons_sus_prin)))%>%
  dplyr::mutate(escolaridad_rec=parse_factor(as.character(escolaridad_rec),levels=c('3-Completed primary school or less', '2-Completed high school or less', '1-More than high school'), ordered =T,trim_ws=T,include_na =F, locale=locale(encoding = "Latin1"))) %>%  
  dplyr::mutate(freq_cons_sus_prin=parse_factor(as.character(freq_cons_sus_prin),levels=c('Less than 1 day a week','2 to 3 days a week','4 to 6 days a week','1 day a week or more','Daily'), ordered =T,trim_ws=F,include_na =F)) %>% #, locale=locale(encoding = "Latin1")
  dplyr::mutate(across(c("motivodeegreso_mod_imp_rec","sus_principal_mod", "origen_ingreso_mod", "tenencia_de_la_vivienda_mod", "condicion_ocupacional_corr", "dg_cie_10_rec", "macrozona", "n_off_vio", "n_off_acq", "n_off_sud", "n_off_oth", "classification", "tr_modality"),~as.factor(.)))%>% 
  dplyr::mutate(via_adm_sus_prin_act= factor(dplyr::case_when(via_adm_sus_prin_act=="Injected Intravenously or Intramuscularly"~ "Other",T~via_adm_sus_prin_act))) %>% 
#TO CHECK IF SOME MUNICIPALLITIES DID NOT JOIN
  #dplyr::filter(is.na(porc_pobr)) %>% dplyr::select(comuna_residencia_cod, anio_ing_tr)
  dplyr::mutate(con_quien_vive_joel=dplyr::case_when(
    grepl("Solo$",con_quien_vive, ignore.case=T)~"Alone",
    grepl("Con abuelos",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("Con hermanos",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("Con la madre \\(sola\\)",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("Con otro pariente",con_quien_vive, ignore.case=T)~"Others",
    grepl("con hijos y padres o familia",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("con la pareja y padres o familia de origen",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("con padres o familia de origen",con_quien_vive, ignore.case=T)~"Family of origin",
    #2021-10-01
    grepl("Únicamente con hijos",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("Únicamente con pareja",con_quien_vive, ignore.case=T)~"With couple/children",
    #2021-10-01
    grepl("Con la Pareja, Hijos y Padres o Familia de Origen",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("Hijos y Padres o Familia de Origen",con_quien_vive, ignore.case=T)~"Family of origin",
    #2021-10-01
    grepl("Únicamente con la pareja e hijos",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("Con amigos",con_quien_vive, ignore.case=T)~"Others",
    grepl("Con otro NO pariente",con_quien_vive, ignore.case=T)~"Others",
    grepl("*Otros$",con_quien_vive, ignore.case=T)~"Others")) %>% 
  dplyr::left_join(subset(CONS_C1_df_dup_SEP_2020, dup==1, c("hash_key","embarazo")), by= "hash_key")%>%
  dplyr::mutate(numero_de_hijos_mod_joel=dplyr::case_when(grepl("Si$",embarazo, ignore.case=T)~as.integer(numero_de_hijos_mod+1),T~as.integer(numero_de_hijos_mod)))  %>% 
  dplyr::mutate(ano_nac_corr=as.numeric(stringr::str_sub(as.character(fech_nac_rec),1,4))) %>% 
  dplyr::mutate(num_hijos_mod_joel_bin=dplyr::if_else(numero_de_hijos_mod_joel>0, 1, 0)) %>%
  dplyr::mutate(across(c("dg_fis_otr_cond_fis_ries_vit", "dg_fis_pat_ges_intrau", "dg_fis_hep_cro", "dg_fis_hep_alc", "dg_fis_enf_som", "dg_fis_otr_cond_fis", "dg_fis_hep_alc", "dg_fis_ets", "dg_fis_card"), ~as.numeric(.)-1)) %>%
  dplyr::mutate(dg_fis_total = rowSums(dplyr::select(., c("dg_fis_otr_cond_fis_ries_vit", "dg_fis_pat_ges_intrau", "dg_fis_hep_cro", "dg_fis_hep_alc", "dg_fis_enf_som", "dg_fis_otr_cond_fis", "dg_fis_hep_alc", "dg_fis_ets", "dg_fis_card")))) %>% 
  dplyr::mutate(cnt_dg_trs_fis= dplyr::case_when(dg_fis_total>=1~ "One or more", as.character(dg_fis_in_study)== "Presence" & dg_fis_total== 0~"Diagnosis unknown (under study)", dg_fis_total==0~"Without physical comorbidity")) %>% #janitor::tabyl(cnt_dg_trs_fis)
  dplyr::mutate(fis_comorbidity_icd_10=parse_factor(as.character(cnt_dg_trs_fis),levels=c('Without physical comorbidity', 'Diagnosis unknown (under study)','One or more'), ordered =T,trim_ws=F,include_na =F)) #, locale=locale(encoding = "Latin1") 

Base_fiscalia_v14_pris<-
Base_fiscalia_v13_pris %>% 
  dplyr::mutate(comuna_residencia_cod_rec= as.character(readr::parse_number(comuna_residencia_cod)), anio_ing_tr= lubridate::epiyear(fech_ing)) %>% #glimpse()
  dplyr::left_join(pobr_mult_2007_2020[,c("anio", "cod","porc_pobr")], by= c("comuna_residencia_cod_rec"="cod", "anio_ing_tr"="anio")) %>% 
  dplyr::left_join(Comunas_PNDR[,c("cod", "Clasificación")], by= c("comuna_residencia_cod_rec"="cod"))%>%
  #2023-02-01
  dplyr::left_join(class_centros[,c("id_centro", "nombre_centro_1", "classification")], by= "id_centro")%>%
    purrr::when(nrow(.)>nrow(Base_fiscalia_v13) ~ stop("More cases in the new database"), ~.)%>% 
  dplyr::mutate(age_offending_imp= dplyr::case_when(age_offending_imp-edad_al_egres_imp<=0~ age_offending_imp+ 0.0001,T~age_offending_imp), event_r= ifelse(!is.na(offender_d),1,0)) %>% 
  dplyr::mutate(freq_cons_sus_prin= dplyr::case_when(as.character(freq_cons_sus_prin)=="Did not use"~"Less than 1 day a week", T~ as.character(freq_cons_sus_prin)))%>%
  dplyr::mutate(escolaridad_rec=parse_factor(as.character(escolaridad_rec),levels=c('3-Completed primary school or less', '2-Completed high school or less', '1-More than high school'), ordered =T,trim_ws=T,include_na =F, locale=locale(encoding = "Latin1"))) %>%  
  dplyr::mutate(freq_cons_sus_prin=parse_factor(as.character(freq_cons_sus_prin),levels=c('Less than 1 day a week','2 to 3 days a week','4 to 6 days a week','1 day a week or more','Daily'), ordered =T,trim_ws=F,include_na =F)) %>% #, locale=locale(encoding = "Latin1")
  dplyr::mutate(freq_cons_sus_prin= as.factor(freq_cons_sus_prin)) %>% 
  dplyr::mutate(across(c("motivodeegreso_mod_imp_rec","sus_principal_mod", "origen_ingreso_mod", "tenencia_de_la_vivienda_mod", "condicion_ocupacional_corr", "dg_cie_10_rec", "macrozona", "n_off_vio", "n_off_acq", "n_off_sud", "n_off_oth", "classification", "tr_modality"),~as.factor(.))) %>% 
  dplyr::mutate(via_adm_sus_prin_act= factor(dplyr::case_when(via_adm_sus_prin_act=="Injected Intravenously or Intramuscularly"~ "Other",T~via_adm_sus_prin_act))) %>% 
#TO CHECK IF SOME MUNICIPALLITIES DID NOT JOIN
  #dplyr::filter(is.na(porc_pobr)) %>% dplyr::select(comuna_residencia_cod, anio_ing_tr)  
  dplyr::mutate(con_quien_vive_joel=dplyr::case_when(
    grepl("Solo$",con_quien_vive, ignore.case=T)~"Alone",
    grepl("Con abuelos",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("Con hermanos",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("Con la madre \\(sola\\)",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("Con otro pariente",con_quien_vive, ignore.case=T)~"Others",
    grepl("con hijos y padres o familia",con_quien_vive, ignore.case=T)~"Family of origin",
    grepl("con la pareja y padres o familia de origen",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("con padres o familia de origen",con_quien_vive, ignore.case=T)~"Family of origin",
    #2021-10-01
    grepl("Únicamente con hijos",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("Únicamente con pareja",con_quien_vive, ignore.case=T)~"With couple/children",
    #2021-10-01
    grepl("Con la Pareja, Hijos y Padres o Familia de Origen",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("Hijos y Padres o Familia de Origen",con_quien_vive, ignore.case=T)~"Family of origin",
    #2021-10-01
    grepl("Únicamente con la pareja e hijos",con_quien_vive, ignore.case=T)~"With couple/children",
    grepl("Con amigos",con_quien_vive, ignore.case=T)~"Others",
    grepl("Con otro NO pariente",con_quien_vive, ignore.case=T)~"Others",
    grepl("*Otros$",con_quien_vive, ignore.case=T)~"Others")) %>% 
  dplyr::left_join(subset(CONS_C1_df_dup_SEP_2020, dup==1, c("hash_key","embarazo")), by= "hash_key")%>%
  dplyr::mutate(numero_de_hijos_mod_joel=dplyr::case_when(grepl("Si$",embarazo, ignore.case=T)~as.integer(numero_de_hijos_mod+1),T~as.integer(numero_de_hijos_mod)))  %>% 
  dplyr::mutate(ano_nac_corr=as.numeric(stringr::str_sub(as.character(fech_nac_rec),1,4))) %>% 
  dplyr::mutate(num_hijos_mod_joel_bin=dplyr::if_else(numero_de_hijos_mod_joel>0, 1, 0)) %>%
  dplyr::mutate(across(c("dg_fis_otr_cond_fis_ries_vit", "dg_fis_pat_ges_intrau", "dg_fis_hep_cro", "dg_fis_hep_alc", "dg_fis_enf_som", "dg_fis_otr_cond_fis", "dg_fis_hep_alc", "dg_fis_ets", "dg_fis_card"), ~as.numeric(.)-1)) %>%
  dplyr::mutate(dg_fis_total = rowSums(dplyr::select(., c("dg_fis_otr_cond_fis_ries_vit", "dg_fis_pat_ges_intrau", "dg_fis_hep_cro", "dg_fis_hep_alc", "dg_fis_enf_som", "dg_fis_otr_cond_fis", "dg_fis_ets", "dg_fis_card")))) %>% 
  dplyr::mutate(cnt_dg_trs_fis= dplyr::case_when(dg_fis_total>=1~ "One or more", as.character(dg_fis_in_study)== "Presence" & dg_fis_total== 0~"Diagnosis unknown (under study)", dg_fis_total==0~"Without physical comorbidity")) %>% #janitor::tabyl(cnt_dg_trs_fis)
  dplyr::mutate(fis_comorbidity_icd_10=parse_factor(as.character(cnt_dg_trs_fis),levels=c('Without physical comorbidity', 'Diagnosis unknown (under study)','One or more'), ordered =T,trim_ws=F,include_na =F)) #, locale=locale(encoding = "Latin1") 
                                          
  
#Base_fiscalia_v14 %>% dplyr::filter(dplyr::case_when(age_offending_imp-edad_al_egres_imp<=0~T,T~F))

#Private Therapeutic Community, ref
Base_fiscalia_v14$clas_r <- relevel(factor(Base_fiscalia_v14$Clasificación), ref = "Urbana")
Base_fiscalia_v14_pris$clas_r <- relevel(factor(Base_fiscalia_v14$Clasificación), ref = "Urbana")
Base_fiscalia_v14$via_adm_sus_prin_act <- relevel(factor(Base_fiscalia_v14$via_adm_sus_prin_act), ref = "Oral (drunk or eaten)")
Base_fiscalia_v14_pris$via_adm_sus_prin_act <- relevel(factor(Base_fiscalia_v14_pris$via_adm_sus_prin_act), ref = "Oral (drunk or eaten)")

Base_fiscalia_v14$yrs_to_condemn<- Base_fiscalia_v14$age_offending_imp - Base_fiscalia_v14$edad_al_egres_imp
Base_fiscalia_v14_pris$yrs_to_pris<- Base_fiscalia_v14_pris$age_offending_imp - Base_fiscalia_v14_pris$edad_al_egres_imp

#2022-02-27
Base_fiscalia_v14$clas_centers_r <- Base_fiscalia_v14$classification
Base_fiscalia_v14$classification<- NULL
Base_fiscalia_v14_pris$clas_centers_r <- Base_fiscalia_v14_pris$classification
Base_fiscalia_v14_pris$classification<- NULL

cat_vars_dg_fis<-
c("dg_fis_anemia", "dg_fis_card", "dg_fis_in_study", "dg_fis_enf_som", "dg_fis_ets", "dg_fis_hep_alc", "dg_fis_hep_b", "dg_fis_hep_cro", "dg_fis_inf", "dg_fis_otr_cond_fis_ries_vit", "dg_fis_otr_cond_fis", "dg_fis_pat_buc", "dg_fis_pat_ges_intrau", "dg_fis_trau_sec")

#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_


invisible("to see if there ar imbalances in the categories")
#lapply(preds2, function(p) {round(prop.table(table(as.tibble(Base_fiscalia_v14_pris)[p], exclude=NULL)),3)*100})
  #numero_de_hijos_mod_joel con_quien_vive_joel

attr(Base_fiscalia_v14$motivodeegreso_mod_imp_rec,"label") <- "Motive of Discharge"
attr(Base_fiscalia_v14$tr_modality,"label") <- "Treatment Modality"
attr(Base_fiscalia_v14$edad_al_ing_1,"label") <- "Age (admission to treatment)"
attr(Base_fiscalia_v14$sex,"label") <- "Sex"
attr(Base_fiscalia_v14$edad_ini_cons,"label") <- "Age of Onset of Substance Use"
attr(Base_fiscalia_v14$escolaridad_rec,"label") <- "Educational Attainment"
attr(Base_fiscalia_v14$sus_principal_mod,"label") <- "Primary Substance (admission to treatment)"
attr(Base_fiscalia_v14$freq_cons_sus_prin,"label") <- "Frequency of Substance Use (Primary Substance)"
attr(Base_fiscalia_v14$condicion_ocupacional_corr,"label") <- "Corrected Occupational Status (f)"
attr(Base_fiscalia_v14$policonsumo,"label") <- "Co-occurring Substance Use Disorders (Polysubstance use)"
attr(Base_fiscalia_v14$num_hijos_mod_joel_bin,"label") <- "Number of Children (dichotomized)"
attr(Base_fiscalia_v14$tenencia_de_la_vivienda_mod,"label") <- "Housing Situation (Tenure Status)"
attr(Base_fiscalia_v14$macrozona,"label") <- "Macro Administrative Zone in Chile"
attr(Base_fiscalia_v14$n_off_vio,"label") <- "Violent Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14$n_off_acq,"label") <- "Acquisitive Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14$n_off_sud,"label") <- "Substance-Related Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14$n_off_oth,"label") <- "Other Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14$dg_cie_10_rec,"label") <- "Psychiatric Comorbidity (ICD-10)"
attr(Base_fiscalia_v14$dg_trs_cons_sus_or,"label") <- "SUD Severity (Dependence status)"
attr(Base_fiscalia_v14$clas_r,"label") <- "Socioeconomic Classification"
attr(Base_fiscalia_v14$porc_pobr,"label") <- "Percentage of people in poverty"
attr(Base_fiscalia_v14$sus_ini_mod_mvv,"label") <- "Primary Substance (initial diagnosis)"
attr(Base_fiscalia_v14$ano_nac_corr,"label") <- "Corrected birth year"
attr(Base_fiscalia_v14$con_quien_vive_joel,"label") <- "Cohabitation status (Recoded) (f)"
attr(Base_fiscalia_v14$fis_comorbidity_icd_10,"label") <- "Physical Comorbidity (ICD-10)"


attr(Base_fiscalia_v14_pris$motivodeegreso_mod_imp_rec,"label") <- "Motive of Discharge"
attr(Base_fiscalia_v14_pris$tr_modality,"label") <- "Treatment Modality"
attr(Base_fiscalia_v14_pris$edad_al_ing_1,"label") <- "Age (admission to treatment)"
attr(Base_fiscalia_v14_pris$sex,"label") <- "Sex"
attr(Base_fiscalia_v14_pris$edad_ini_cons,"label") <- "Age of Onset of Substance Use"
attr(Base_fiscalia_v14_pris$escolaridad_rec,"label") <- "Educational Attainment"
attr(Base_fiscalia_v14_pris$sus_principal_mod,"label") <- "Primary Substance (admission to treatment)"
attr(Base_fiscalia_v14_pris$freq_cons_sus_prin,"label") <- "Frequency of Substance Use (Primary Substance)"
attr(Base_fiscalia_v14_pris$condicion_ocupacional_corr,"label") <- "Corrected Occupational Status (f)"
attr(Base_fiscalia_v14_pris$policonsumo,"label") <- "Co-occurring Substance Use Disorders (Polysubstance use)"
attr(Base_fiscalia_v14_pris$num_hijos_mod_joel_bin,"label") <- "Number of Children (dichotomized)"
attr(Base_fiscalia_v14_pris$tenencia_de_la_vivienda_mod,"label") <- "Housing Situation (Tenure Status)"
attr(Base_fiscalia_v14_pris$macrozona,"label") <- "Macro Administrative Zone in Chile"
attr(Base_fiscalia_v14_pris$n_off_vio,"label") <- "Violent Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14_pris$n_off_acq,"label") <- "Acquisitive Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14_pris$n_off_sud,"label") <- "Substance-Related Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14_pris$n_off_oth,"label") <- "Other Criminal Offenses (Pre-Treatment)"
attr(Base_fiscalia_v14_pris$dg_cie_10_rec,"label") <- "Psychiatric Comorbidity (ICD-10)"
attr(Base_fiscalia_v14_pris$dg_trs_cons_sus_or,"label") <- "SUD Severity (Dependence status)"
attr(Base_fiscalia_v14_pris$clas_r,"label") <- "Socioeconomic Classification"
attr(Base_fiscalia_v14_pris$porc_pobr,"label") <- "Percentage of people in poverty"
attr(Base_fiscalia_v14_pris$sus_ini_mod_mvv,"label") <- "Primary Substance (initial diagnosis)"
attr(Base_fiscalia_v14_pris$ano_nac_corr,"label") <- "Corrected birth year"
attr(Base_fiscalia_v14_pris$con_quien_vive_joel,"label") <- "Cohabitation status (Recoded) (f)"
attr(Base_fiscalia_v14_pris$fis_comorbidity_icd_10,"label") <- "Physical Comorbidity (ICD-10)"

#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_

rbind(
  cbind(cat="Admission to baseline treatment age",
        Base_fiscalia_v13 %>% 
          dplyr::summarise(min = as.Date(min(unclass(fech_ing_num_1), na.rm=T), origin = "1970-01-01"),
           p025=as.Date(quantile(unclass(fech_ing_num_1), .025, na.rm=T), origin = "1970-01-01"),
           p25=as.Date(quantile(unclass(fech_ing_num_1), .25, na.rm=T), origin = "1970-01-01"),
           p50=as.Date(quantile(unclass(fech_ing_num_1), .5, na.rm=T), origin = "1970-01-01"),
           p75=as.Date(quantile(unclass(fech_ing_num_1), .75, na.rm=T), origin = "1970-01-01"),
           p975=as.Date(quantile(unclass(fech_ing_num_1), .975, na.rm=T), origin = "1970-01-01"),
           max = as.Date(max(unclass(fech_ing_num_1), na.rm=T), origin = "1970-01-01"))),
  cbind(cat="Date of birth (corrected)",
        Base_fiscalia_v13 %>% 
          dplyr::summarise(min = as.Date(min(unclass(fech_nac_rec), na.rm=T), origin = "1970-01-01"),
           p025=as.Date(quantile(unclass(fech_nac_rec), .025, na.rm=T), origin = "1970-01-01"),
           p25=as.Date(quantile(unclass(fech_nac_rec), .25, na.rm=T), origin = "1970-01-01"),
           p50=as.Date(quantile(unclass(fech_nac_rec), .5, na.rm=T), origin = "1970-01-01"),
           p75=as.Date(quantile(unclass(fech_nac_rec), .75, na.rm=T), origin = "1970-01-01"),
           p975=as.Date(quantile(unclass(fech_nac_rec), .975, na.rm=T), origin = "1970-01-01"),
           max = as.Date(max(unclass(fech_nac_rec), na.rm=T), origin = "1970-01-01")))) %>% 
  knitr::kable(format="html",caption= "Summary of Dates (after correcting dates)") %>% #,col.names=c("Variables","Residential", "Ambulatory", "p-value")) %>% 
  kableExtra::kable_classic(bootstrap_options = c("striped", "hover","condensed"),font_size= 12) 
(#tab:bring_db1)Summary of Dates (after correcting dates)
cat min p025 p25 p50 p75 p975 max
Admission to baseline treatment age 2007-04-18 2009-10-05 2012-08-01 2014-11-13 2016-12-06 2019-01-31 2019-10-21
Date of birth (corrected) 1929-03-20 1954-08-16 1971-09-14 1980-07-15 1987-03-16 1995-11-23 2004-11-20

Time for this code chunk to run: 0.4 minutes


Explore

Crude RMST & RMSTL

We calculated the restricted mean survival time and lost, considering hazards did not seem to be proportional. We considered people with complete data (n= 70854) of the total sample (n= 70863).

Show code
#Treatment completion Treatment non-completion (Early)  Treatment non-completion (Late) 
yrs_to_condemn_comp_cases<-
Base_fiscalia_v14[complete.cases(Base_fiscalia_v14[,c("motivodeegreso_mod_imp_rec", "yrs_to_condemn", "event_r")]),"yrs_to_condemn"]
event_r_comp_cases<-
Base_fiscalia_v14[complete.cases(Base_fiscalia_v14[,c("motivodeegreso_mod_imp_rec", "yrs_to_condemn", "event_r")]),"event_r"]


motivo_egreso_comp_cases <- Base_fiscalia_v14[complete.cases(Base_fiscalia_v14[,c("motivodeegreso_mod_imp_rec", "yrs_to_condemn", "event_r")]),"motivodeegreso_mod_imp_rec"]
tr_comp_comp_cases<-ifelse(motivo_egreso_comp_cases=="Treatment completion",1,0)
tr_early_drop_comp_cases<-ifelse(motivo_egreso_comp_cases=="Treatment non-completion (Early)",1,0)

tr_late_drop_comp_cases<-ifelse(motivo_egreso_comp_cases=="Treatment non-completion (Late)",1,0)



#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
cat("===============================================================================")
===============================================================================
Show code
cat("Treatment completion vs. Late and Early Dropouts at 3 months")
Treatment completion vs. Late and Early Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases, event_r_comp_cases, tr_comp_comp_cases, tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.325  0     0.324     0.325
RMST (arm=0) 0.317  0     0.316     0.317


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.005  0     0.005     0.006
RMTL (arm=0) 0.013  0     0.013     0.014


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.008     0.007     0.009 0
RMST (arm=1)/(arm=0) 1.025     1.023     1.027 0
RMTL (arm=1)/(arm=0) 0.396     0.360     0.437 0
Show code
cat("Treatment completion vs. Late and Early Dropouts at 1 year")
Treatment completion vs. Late and Early Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases, event_r_comp_cases, tr_comp_comp_cases, tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.956 0.001     0.953     0.958
RMST (arm=0) 0.902 0.001     0.900     0.904


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.044 0.001     0.042     0.047
RMTL (arm=0) 0.098 0.001     0.096     0.100


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.053     0.050     0.057 0
RMST (arm=1)/(arm=0) 1.059     1.056     1.063 0
RMTL (arm=1)/(arm=0) 0.454     0.428     0.482 0
Show code
cat("Treatment completion vs. Late and Early Dropouts at 3 years")
Treatment completion vs. Late and Early Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases, event_r_comp_cases, tr_comp_comp_cases, tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.676 0.006     2.665     2.688
RMST (arm=0) 2.387 0.005     2.378     2.396


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.324 0.006     0.312     0.335
RMTL (arm=0) 0.613 0.005     0.604     0.622


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.289     0.275     0.304 0
RMST (arm=1)/(arm=0) 1.121     1.115     1.128 0
RMTL (arm=1)/(arm=0) 0.528     0.508     0.549 0
Show code
cat("Treatment completion vs. Late and Early Dropouts at 5 years")
Treatment completion vs. Late and Early Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases, event_r_comp_cases, tr_comp_comp_cases, tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 4.228 0.012     4.205     4.252
RMST (arm=0) 3.644 0.009     3.627     3.661


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.772 0.012     0.748     0.795
RMTL (arm=0) 1.356 0.009     1.339     1.373


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.585     0.556     0.613 0
RMST (arm=1)/(arm=0) 1.160     1.152     1.169 0
RMTL (arm=1)/(arm=0) 0.569     0.551     0.588 0
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Treatment completion vs. Late Dropouts at 3 months")
Treatment completion vs. Late Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.325  0     0.324     0.325
RMST (arm=0) 0.318  0     0.317     0.319


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.005  0     0.005     0.006
RMTL (arm=0) 0.012  0     0.011     0.013


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.007     0.006     0.007 0
RMST (arm=1)/(arm=0) 1.021     1.019     1.023 0
RMTL (arm=1)/(arm=0) 0.439     0.396     0.485 0
Show code
cat("Treatment completion vs. Late Dropouts at 1 year")
Treatment completion vs. Late Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.956 0.001     0.953     0.958
RMST (arm=0) 0.910 0.001     0.908     0.913


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.044 0.001     0.042     0.047
RMTL (arm=0) 0.090 0.001     0.087     0.092


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.045     0.042     0.049 0
RMST (arm=1)/(arm=0) 1.050     1.046     1.054 0
RMTL (arm=1)/(arm=0) 0.496     0.467     0.527 0
Show code
cat("Treatment completion vs. Late Dropouts at 3 years")
Treatment completion vs. Late Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.676 0.006     2.665     2.688
RMST (arm=0) 2.426 0.005     2.416     2.437


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.324 0.006     0.312     0.335
RMTL (arm=0) 0.574 0.005     0.563     0.584


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.250     0.235     0.266 0
RMST (arm=1)/(arm=0) 1.103     1.096     1.110 0
RMTL (arm=1)/(arm=0) 0.564     0.542     0.587 0
Show code
cat("Treatment completion vs. Late Dropouts at 5 years")
Treatment completion vs. Late Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Early",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 4.228 0.012     4.205     4.252
RMST (arm=0) 3.719 0.010     3.699     3.739


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.772 0.012     0.748     0.795
RMTL (arm=0) 1.281 0.010     1.261     1.301


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.510     0.479     0.540 0
RMST (arm=1)/(arm=0) 1.137     1.128     1.146 0
RMTL (arm=1)/(arm=0) 0.602     0.582     0.623 0
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Treatment completion vs. Early Dropouts at 3 months")
Treatment completion vs. Early Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.325  0     0.324     0.325
RMST (arm=0) 0.314  0     0.313     0.315


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.005  0     0.005     0.006
RMTL (arm=0) 0.016  0     0.015     0.017


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.011     0.010     0.012 0
RMST (arm=1)/(arm=0) 1.035     1.031     1.038 0
RMTL (arm=1)/(arm=0) 0.326     0.292     0.363 0
Show code
cat("Treatment completion vs. Early Dropouts at 1 year")
Treatment completion vs. Early Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.956 0.001     0.953     0.958
RMST (arm=0) 0.883 0.002     0.879     0.888


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.044 0.001     0.042     0.047
RMTL (arm=0) 0.117 0.002     0.112     0.121


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.072     0.067     0.077 0
RMST (arm=1)/(arm=0) 1.082     1.076     1.088 0
RMTL (arm=1)/(arm=0) 0.381     0.357     0.407 0
Show code
cat("Treatment completion vs. Early Dropouts at 3 years")
Treatment completion vs. Early Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.676 0.006     2.665     2.688
RMST (arm=0) 2.296 0.009     2.279     2.314


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.324 0.006     0.312     0.335
RMTL (arm=0) 0.704 0.009     0.686     0.721


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.380     0.359     0.400 0
RMST (arm=1)/(arm=0) 1.165     1.155     1.175 0
RMTL (arm=1)/(arm=0) 0.460     0.441     0.480 0
Show code
cat("Treatment completion vs. Early Dropouts at 5 years")
Treatment completion vs. Early Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases[!grepl("Late",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 4.228 0.012     4.205     4.252
RMST (arm=0) 3.471 0.016     3.439     3.502


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.772 0.012     0.748     0.795
RMTL (arm=0) 1.529 0.016     1.498     1.561


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.758     0.718     0.797 0
RMST (arm=1)/(arm=0) 1.218     1.205     1.231 0
RMTL (arm=1)/(arm=0) 0.505     0.486     0.523 0
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Late vs. Early Dropouts at 3 months")
Late vs. Early Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.314  0     0.313     0.315
RMST (arm=0) 0.318  0     0.317     0.319


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.016  0     0.015     0.017
RMTL (arm=0) 0.012  0     0.011     0.013


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.004    -0.005    -0.003 0
RMST (arm=1)/(arm=0)  0.987     0.984     0.990 0
RMTL (arm=1)/(arm=0)  1.347     1.253     1.448 0
Show code
cat("Late vs. Early Dropouts at 1 year")
Late vs. Early Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.883 0.002     0.879     0.888
RMST (arm=0) 0.910 0.001     0.908     0.913


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.117 0.002     0.112     0.121
RMTL (arm=0) 0.090 0.001     0.087     0.092


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.027    -0.032    -0.022 0
RMST (arm=1)/(arm=0)  0.970     0.965     0.976 0
RMTL (arm=1)/(arm=0)  1.302     1.244     1.362 0
Show code
cat("Late vs. Early Dropouts at 3 years")
Late vs. Early Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.296 0.009     2.279     2.314
RMST (arm=0) 2.426 0.005     2.416     2.437


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.704 0.009     0.686     0.721
RMTL (arm=0) 0.574 0.005     0.563     0.584


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.130    -0.150    -0.110 0
RMST (arm=1)/(arm=0)  0.947     0.938     0.955 0
RMTL (arm=1)/(arm=0)  1.226     1.189     1.264 0
Show code
cat("Late vs. Early Dropouts at 5 years")
Late vs. Early Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_condemn_comp_cases$yrs_to_condemn[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases$event_r[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases[grepl("non",motivo_egreso_comp_cases$motivodeegreso_mod_imp_rec),1], tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 3.471 0.016     3.439     3.502
RMST (arm=0) 3.719 0.010     3.699     3.739


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 1.529 0.016     1.498     1.561
RMTL (arm=0) 1.281 0.010     1.261     1.301


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.248    -0.286    -0.211 0
RMST (arm=1)/(arm=0)  0.933     0.923     0.943 0
RMTL (arm=1)/(arm=0)  1.194     1.163     1.225 0

Time for this code chunk to run: 0.1 minutes

Show code
#Treatment completion Treatment non-completion (Early)  Treatment non-completion (Late) 
yrs_to_pris_comp_cases<-
Base_fiscalia_v14_pris[complete.cases(Base_fiscalia_v14_pris[,c("motivodeegreso_mod_imp_rec", "yrs_to_pris", "event_r")]),"yrs_to_pris"]
event_r_comp_cases_pris<-
Base_fiscalia_v14_pris[complete.cases(Base_fiscalia_v14_pris[,c("motivodeegreso_mod_imp_rec", "yrs_to_pris", "event_r")]),"event_r"]


motivo_egreso_comp_cases_pris <- Base_fiscalia_v14_pris[complete.cases(Base_fiscalia_v14_pris[,c("motivodeegreso_mod_imp_rec", "yrs_to_pris", "event_r")]),"motivodeegreso_mod_imp_rec"]
tr_comp_comp_cases_pris<-ifelse(motivo_egreso_comp_cases_pris=="Treatment completion",1,0)
tr_early_drop_comp_cases_pris<-ifelse(motivo_egreso_comp_cases_pris=="Treatment non-completion (Early)",1,0)

tr_late_drop_comp_cases_pris<-ifelse(motivo_egreso_comp_cases_pris=="Treatment non-completion (Late)",1,0)



#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
cat("===============================================================================")
===============================================================================
Show code
cat("Treatment completion vs. Late and Early Dropouts at 3 months")
Treatment completion vs. Late and Early Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_pris_comp_cases, event_r_comp_cases_pris, tr_comp_comp_cases_pris, tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.329  0     0.329     0.329
RMST (arm=0) 0.327  0     0.327     0.328


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.001  0     0.001     0.001
RMTL (arm=0) 0.003  0     0.002     0.003


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.002     0.002     0.002 0
RMST (arm=1)/(arm=0) 1.006     1.005     1.006 0
RMTL (arm=1)/(arm=0) 0.284     0.219     0.368 0
Show code
cat("Treatment completion vs. Late and Early Dropouts at 1 year")
Treatment completion vs. Late and Early Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_pris_comp_cases, event_r_comp_cases_pris, tr_comp_comp_cases_pris, tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.994 0.000     0.993     0.995
RMST (arm=0) 0.981 0.001     0.980     0.982


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.006 0.000     0.005     0.007
RMTL (arm=0) 0.019 0.001     0.018     0.020


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.013     0.011     0.014 0
RMST (arm=1)/(arm=0) 1.013     1.012     1.014 0
RMTL (arm=1)/(arm=0) 0.317     0.270     0.373 0
Show code
cat("Treatment completion vs. Late and Early Dropouts at 3 years")
Treatment completion vs. Late and Early Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases, event_r_comp_cases_pris, tr_comp_comp_cases_pris, tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.954 0.002     2.950     2.959
RMST (arm=0) 2.874 0.002     2.869     2.878


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.046 0.002     0.041     0.050
RMTL (arm=0) 0.126 0.002     0.122     0.131


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.080     0.074     0.087 0
RMST (arm=1)/(arm=0) 1.028     1.026     1.030 0
RMTL (arm=1)/(arm=0) 0.364     0.328     0.405 0
Show code
cat("Treatment completion vs. Late and Early Dropouts at 5 years")
Treatment completion vs. Late and Early Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases, event_r_comp_cases_pris, tr_comp_comp_cases_pris, tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 4.880 0.005     4.870     4.890
RMST (arm=0) 4.703 0.005     4.694     4.712


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.120 0.005     0.110     0.130
RMTL (arm=0) 0.297 0.005     0.288     0.306


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.177     0.163     0.190 0
RMST (arm=1)/(arm=0) 1.038     1.035     1.041 0
RMTL (arm=1)/(arm=0) 0.404     0.370     0.442 0
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Treatment completion vs. Late Dropouts at 3 months")
Treatment completion vs. Late Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.329  0     0.329     0.329
RMST (arm=0) 0.328  0     0.328     0.328


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.001  0     0.001     0.001
RMTL (arm=0) 0.002  0     0.002     0.002


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.001     0.001     0.002 0
RMST (arm=1)/(arm=0) 1.004     1.003     1.005 0
RMTL (arm=1)/(arm=0) 0.337     0.257     0.440 0
Show code
cat("Treatment completion vs. Late Dropouts at 1 year")
Treatment completion vs. Late Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.994 0.000     0.993     0.995
RMST (arm=0) 0.984 0.001     0.983     0.985


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.006 0.000     0.005     0.007
RMTL (arm=0) 0.016 0.001     0.015     0.017


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.010     0.009     0.011 0
RMST (arm=1)/(arm=0) 1.010     1.009     1.012 0
RMTL (arm=1)/(arm=0) 0.373     0.315     0.442 0
Show code
cat("Treatment completion vs. Late Dropouts at 3 years")
Treatment completion vs. Late Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.954 0.002     2.950     2.959
RMST (arm=0) 2.891 0.003     2.886     2.896


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.046 0.002     0.041     0.050
RMTL (arm=0) 0.109 0.003     0.104     0.114


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.063     0.056     0.070 0
RMST (arm=1)/(arm=0) 1.022     1.019     1.024 0
RMTL (arm=1)/(arm=0) 0.422     0.378     0.471 0
Show code
cat("Treatment completion vs. Late Dropouts at 5 years")
Treatment completion vs. Late Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Early",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 4.880 0.005     4.870     4.890
RMST (arm=0) 4.741 0.005     4.731     4.752


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.120 0.005     0.110     0.130
RMTL (arm=0) 0.259 0.005     0.248     0.269


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.139     0.124     0.153 0
RMST (arm=1)/(arm=0) 1.029     1.026     1.032 0
RMTL (arm=1)/(arm=0) 0.464     0.423     0.508 0
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Treatment completion vs. Early Dropouts at 3 months")
Treatment completion vs. Early Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.329  0     0.329     0.329
RMST (arm=0) 0.327  0     0.326     0.327


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.001  0     0.001     0.001
RMTL (arm=0) 0.003  0     0.003     0.004


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.003     0.002     0.003 0
RMST (arm=1)/(arm=0) 1.008     1.007     1.010 0
RMTL (arm=1)/(arm=0) 0.210     0.159     0.277 0
Show code
cat("Treatment completion vs. Early Dropouts at 1 year")
Treatment completion vs. Early Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.994 0.000     0.993     0.995
RMST (arm=0) 0.975 0.001     0.973     0.977


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.006 0.000     0.005     0.007
RMTL (arm=0) 0.025 0.001     0.023     0.027


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.019     0.017     0.021 0
RMST (arm=1)/(arm=0) 1.020     1.017     1.022 0
RMTL (arm=1)/(arm=0) 0.237     0.199     0.282 0
Show code
cat("Treatment completion vs. Early Dropouts at 3 years")
Treatment completion vs. Early Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.954 0.002     2.950     2.959
RMST (arm=0) 2.834 0.005     2.825     2.844


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.046 0.002     0.041     0.050
RMTL (arm=0) 0.166 0.005     0.156     0.175


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.120     0.109     0.130 0
RMST (arm=1)/(arm=0) 1.042     1.038     1.046 0
RMTL (arm=1)/(arm=0) 0.277     0.247     0.310 0
Show code
cat("Treatment completion vs. Early Dropouts at 5 years")
Treatment completion vs. Early Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_comp_comp_cases_pris[!grepl("Late",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 4.880 0.005     4.870     4.890
RMST (arm=0) 4.615 0.010     4.596     4.634


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.120 0.005     0.110     0.130
RMTL (arm=0) 0.385 0.010     0.366     0.404


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.265     0.243     0.286 0
RMST (arm=1)/(arm=0) 1.057     1.053     1.062 0
RMTL (arm=1)/(arm=0) 0.312     0.283     0.343 0
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Late vs. Early Dropouts at 3 months")
Late vs. Early Dropouts at 3 months
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=.33)

The truncation time: tau = 0.33  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est. se lower .95 upper .95
RMST (arm=1) 0.327  0     0.326     0.327
RMST (arm=0) 0.328  0     0.328     0.328


Restricted Mean Time Lost (RMTL) by arm 
              Est. se lower .95 upper .95
RMTL (arm=1) 0.003  0     0.003     0.004
RMTL (arm=0) 0.002  0     0.002     0.002


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.001    -0.002    -0.001 0
RMST (arm=1)/(arm=0)  0.996     0.995     0.998 0
RMTL (arm=1)/(arm=0)  1.604     1.357     1.895 0
Show code
cat("Late vs. Early Dropouts at 1 year")
Late vs. Early Dropouts at 1 year
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.975 0.001     0.973     0.977
RMST (arm=0) 0.984 0.001     0.983     0.985


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.025 0.001     0.023     0.027
RMTL (arm=0) 0.016 0.001     0.015     0.017


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.009    -0.011    -0.007 0
RMST (arm=1)/(arm=0)  0.991     0.988     0.993 0
RMTL (arm=1)/(arm=0)  1.576     1.415     1.756 0
Show code
cat("Late vs. Early Dropouts at 3 years")
Late vs. Early Dropouts at 3 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 2.834 0.005     2.825     2.844
RMST (arm=0) 2.891 0.003     2.886     2.896


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.166 0.005     0.156     0.175
RMTL (arm=0) 0.109 0.003     0.104     0.114


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.057    -0.068    -0.046 0
RMST (arm=1)/(arm=0)  0.980     0.977     0.984 0
RMTL (arm=1)/(arm=0)  1.524     1.416     1.640 0
Show code
cat("Late vs. Early Dropouts at 5 years")
Late vs. Early Dropouts at 5 years
Show code
survRM2::rmst2(yrs_to_pris_comp_cases$yrs_to_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               event_r_comp_cases_pris$event_r[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec)], 
               tr_early_drop_comp_cases_pris[grepl("non",motivo_egreso_comp_cases_pris$motivodeegreso_mod_imp_rec),1], tau=5)

The truncation time: tau = 5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 4.615 0.010     4.596     4.634
RMST (arm=0) 4.741 0.005     4.731     4.752


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.385 0.010     0.366     0.404
RMTL (arm=0) 0.259 0.005     0.248     0.269


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.126    -0.147    -0.105 0
RMST (arm=1)/(arm=0)  0.973     0.969     0.978 0
RMTL (arm=1)/(arm=0)  1.487     1.396     1.584 0

Time for this code chunk to run: 0.1 minutes


Linearity

We checked the proportionality and linearity of covariates.

Show code
preds2<- c("motivodeegreso_mod_imp_rec", "tr_modality", "edad_al_ing_1", "sex", "edad_ini_cons", "escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin", "condicion_ocupacional_corr", "policonsumo", "num_hijos_mod_joel_bin", "tenencia_de_la_vivienda_mod", "macrozona", "n_off_vio", "n_off_acq",  "n_off_sud", "n_off_oth", "clas_r", "dg_cie_10_rec", "dg_trs_cons_sus_or", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", "fis_comorbidity_icd_10")

vars_cov<-c("motivodeegreso_mod_imp_rec", "tr_modality", "edad_al_ing_1", "sex", "edad_ini_cons", "escolaridad_rec", "sus_principal_mod", "freq_cons_sus_prin", "condicion_ocupacional_corr", "policonsumo", "num_hijos_mod_joel_bin", "tenencia_de_la_vivienda_mod", "macrozona", "n_off_vio", "n_off_acq",  "n_off_sud", "n_off_oth", "dg_cie_10_rec", "dg_trs_cons_sus_or", "clas_r", "porc_pobr", "sus_ini_mod_mvv", "ano_nac_corr", "con_quien_vive_joel", "fis_comorbidity_icd_10")

#numero_de_hijos_mod_joel ano_nac_corr con_quien_vive_joel

coxfit_more_var1 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec +", paste(setdiff(vars_cov,""), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)#[1:250,]
#n_prev_off + 
coxfit_more_var05 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec +", paste(setdiff(vars_cov,""), collapse = "+"))), ties="efron", data= Base_fiscalia_v14 %>% dplyr::mutate(edad_al_ing_1= VGAM::yeo.johnson(Base_fiscalia_v14$edad_al_ing_1, .5)))#[1:250,]


#*If you are not directly interested in the relations of x1 or x2 to outcome and are simply controlling for them to analyze your predictor of main interest, then you don't have to worry much about the initially confusing display of p-values and hazard ratios for each of the terms associated with the splines.

coxfit_more_var2 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,3) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

coxfit_more_var3 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,4) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

coxfit_more_var4 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,5) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

coxfit_more_var5 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,6) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

coxfit_more_var6 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

coxfit_more_var7 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1,df= 2) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

coxfit_more_var8 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1,df= 3) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

coxfit_more_var9 <- coxph(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1,df= 4) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14)

anova(coxfit_more_var05, coxfit_more_var1, coxfit_more_var2, coxfit_more_var3, coxfit_more_var4, coxfit_more_var5, coxfit_more_var6, coxfit_more_var7, coxfit_more_var8, coxfit_more_var9)
Analysis of Deviance Table
 Cox model: response is  Surv(yrs_to_condemn, event_r)
 Model  1: ~ motivodeegreso_mod_imp_rec + motivodeegreso_mod_imp_rec + tr_modality + edad_al_ing_1 + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  2: ~ motivodeegreso_mod_imp_rec + motivodeegreso_mod_imp_rec + tr_modality + edad_al_ing_1 + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  3: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 3) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  4: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  5: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 5) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  6: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 6) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  7: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  8: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 2) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  9: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 3) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 10: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
    loglik    Chisq Df Pr(>|Chi|)    
1  -181801                           
2  -181641 320.3479  0  < 2.2e-16 ***
3  -181629  23.4948  1  1.253e-06 ***
4  -181625   9.1213  1   0.002527 ** 
5  -181624   0.5719  1   0.449508    
6  -181624   0.9666  1   0.325525    
7  -181641  34.1546  4  6.927e-07 ***
8  -181628  26.4845  1  2.657e-07 ***
9  -181624   7.2392  1   0.007133 ** 
10 -181624   0.7403  1   0.389550    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Show code
anova(coxfit_more_var3, coxfit_more_var4, coxfit_more_var5,  coxfit_more_var8, coxfit_more_var9)
Analysis of Deviance Table
 Cox model: response is  Surv(yrs_to_condemn, event_r)
 Model 1: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 2: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 5) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 3: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 6) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 4: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 3) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 5: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
   loglik  Chisq Df Pr(>|Chi|)
1 -181625                     
2 -181624 0.5719  1     0.4495
3 -181624 0.9666  1     0.3255
4 -181624 0.4309  2     0.8062
5 -181624 0.7403  1     0.3896
Show code
invisible("coxfit_more_var3 , rcs 4, the best")

# Dimitris Rizopoulos (https://stats.stackexchange.com/users/219012/dimitris-rizopoulos), Check log-linearity of a continuous variable to predict survival, URL (version: 2018-10-08): https://stats.stackexchange.com/q/370353

# https://www.drizopoulos.com/courses/EMC/EP03.pdf

# Create an effect plot to depict the relationship
ND <- with(Base_fiscalia_v14, 
data.frame(edad_al_ing_1 = seq(min(edad_al_ing_1),  max(edad_al_ing_1), length.out = 50),
            motivodeegreso_mod_imp_rec=rep(Mode(motivodeegreso_mod_imp_rec),50),
            #edad_al_ing_1=rep(mean(edad_al_ing_1, na.rm=T))
            tr_modality= rep(Mode(tr_modality),50),
            sex= rep(Mode(sex),50),
            edad_ini_cons=rep(quantile(edad_ini_cons, .5, na.rm=T),50),
            escolaridad_rec= rep(Mode(escolaridad_rec),50),
            sus_principal_mod= rep(Mode(sus_principal_mod),50),
            freq_cons_sus_prin= rep(Mode(freq_cons_sus_prin),50),
            condicion_ocupacional_corr= rep(Mode(condicion_ocupacional_corr),50),
            policonsumo= rep(Mode(policonsumo),50),
            num_hijos_mod_joel_bin= rep(Mode(num_hijos_mod_joel_bin),50),
            tenencia_de_la_vivienda_mod= rep(Mode(tenencia_de_la_vivienda_mod),50),
            macrozona = rep(Mode(macrozona),50),
            #n_prev_off= rep(quantile(n_prev_off, .5, na.rm=T),50),
            n_off_vio= rep(Mode(n_off_vio),50),
            n_off_acq= rep(Mode(n_off_acq),50),
            n_off_sud= rep(Mode(n_off_sud),50),
            n_off_oth= rep(Mode(n_off_oth),50),
            dg_cie_10_rec = rep(Mode(dg_cie_10_rec),50),
            dg_trs_cons_sus_or = rep(Mode(dg_trs_cons_sus_or),50),
            clas_r= rep(Mode(clas_r),50),
            porc_pobr=rep(quantile(porc_pobr, .5, na.rm=T),50),
            sus_ini_mod_mvv=rep(Mode(sus_ini_mod_mvv),50),
            ano_nac_corr=rep(quantile(ano_nac_corr, .5, na.rm=T),50),
            con_quien_vive_joel=rep(Mode(con_quien_vive_joel),50),
            fis_comorbidity_icd_10=rep(Mode(fis_comorbidity_icd_10),50)
))

prs <- predict(coxfit_more_var3, newdata = ND, type = "lp", se.fit = TRUE)
ND$pred <- prs[[1]]
ND$se <- prs[[2]]
ND$lo <- ND$pred - 1.96 * ND$se
ND$up <- ND$pred + 1.96 * ND$se

xyplot(pred + lo + up ~ edad_al_ing_1, data = ND,
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Admission age")
Show code
prs2 <- predict(coxfit_more_var3, newdata = ND%>% dplyr::mutate(edad_ini_cons= seq(min(Base_fiscalia_v14$edad_ini_cons,na.rm=T),  max(Base_fiscalia_v14$edad_ini_cons,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14$edad_al_ing_1, .5, na.rm=T))), type = "lp", se.fit = TRUE)
ND$pred2 <- prs2[[1]]
ND$se2 <- prs2[[2]]
ND$lo2 <- ND$pred2 - 1.96 * ND$se2
ND$up2 <- ND$pred2 + 1.96 * ND$se2

xyplot(pred2 + lo2 + up2 ~ edad_ini_cons, data = ND%>% dplyr::mutate(edad_ini_cons= seq(min(Base_fiscalia_v14$edad_ini_cons,na.rm=T),  max(Base_fiscalia_v14$edad_ini_cons,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14$edad_al_ing_1, .5, na.rm=T))),
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Substance use onset age")
Show code
prs3 <- predict(coxfit_more_var3, newdata = ND%>% dplyr::mutate(ano_nac_corr= seq(min(Base_fiscalia_v14$ano_nac_corr,na.rm=T),  max(Base_fiscalia_v14$ano_nac_corr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14$edad_al_ing_1, .5, na.rm=T))), type = "lp", se.fit = TRUE)
ND$pred3 <- prs3[[1]]
ND$se3 <- prs3[[2]]
ND$lo3 <- ND$pred3 - 1.96 * ND$se3
ND$up3 <- ND$pred3 + 1.96 * ND$se3

xyplot(pred3 + lo3 + up3 ~ ano_nac_corr, data = ND%>% dplyr::mutate(ano_nac_corr= seq(min(Base_fiscalia_v14$ano_nac_corr,na.rm=T),  max(Base_fiscalia_v14$ano_nac_corr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14$edad_al_ing_1, .5, na.rm=T))),
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Year of birth")
Show code
prs4 <- predict(coxfit_more_var3, newdata = ND%>% dplyr::mutate(porc_pobr= seq(min(Base_fiscalia_v14$porc_pobr,na.rm=T),  max(Base_fiscalia_v14$porc_pobr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14$edad_al_ing_1, .5, na.rm=T))), type = "lp", se.fit = TRUE)
ND$pred4 <- prs4[[1]]
ND$se4 <- prs4[[2]]
ND$lo4 <- ND$pred4 - 1.96 * ND$se4
ND$up4 <- ND$pred4 + 1.96 * ND$se4

xyplot(pred4 + lo4 + up4 ~ porc_pobr, data = ND%>% dplyr::mutate(porc_pobr= seq(min(Base_fiscalia_v14$porc_pobr,na.rm=T),  max(Base_fiscalia_v14$porc_pobr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14$edad_al_ing_1, .5, na.rm=T))),
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Percentage of poverty")

Time for this code chunk to run: 0.4 minutes

Show code
#http://www.sthda.com/english/wiki/cox-model-assumptions
invisible("omitted")
invisible("Martingale residual to assess nonlinearity, does not allow ns or rcs")
# 
# #https://github.com/kassambara/survminer/issues/357
 res.cox_edad_ing <- coxph(Surv(yrs_to_condemn,  event_r) ~ edad_al_ing_1+ log(edad_al_ing_1) + I(edad_al_ing_1^2) + I(log(edad_al_ing_1)^2) + I(sqrt(edad_al_ing_1)), data = Base_fiscalia_v14)
 
 
 ggcoxfunctional(res.cox_edad_ing,  data = Base_fiscalia_v14%>% dplyr::filter(!is.na(edad_al_ing_1)), point.col = "blue", point.alpha = 0.5)
 
cat("______________________________________________________________________") 
 
# 
# #to avoid errors, we deleted missing values
# 
 res.cox_porc_pobr <- coxph(Surv(yrs_to_condemn,  event_r) ~ porc_pobr + log(porc_pobr) + I(porc_pobr^2) + I(log(porc_pobr)^2) + I(sqrt(porc_pobr)), data = Base_fiscalia_v14%>% dplyr::filter(!is.na(porc_pobr)))
# 
 ggcoxfunctional(res.cox_porc_pobr,  data = Base_fiscalia_v14%>% dplyr::filter(!is.na(porc_pobr)), point.col = "blue", point.alpha = 0.5)

cat("______________________________________________________________________")
  
#to avoid errors, we replaced 0s with 0.0001
res.cox_dias_treat <- coxph(Surv(yrs_to_condemn,  event_r) ~ dias_treat_imp_sin_na_1 + log(dias_treat_imp_sin_na_1) + I(log(dias_treat_imp_sin_na_1)^2) + I(dias_treat_imp_sin_na_1^2) +  I(sqrt(dias_treat_imp_sin_na_1)), data = Base_fiscalia_v14%>% dplyr::filter(!is.na(dias_treat_imp_sin_na_1)) %>% dplyr::mutate(dias_treat_imp_sin_na_1= dplyr::case_when(dias_treat_imp_sin_na_1==0~.0001,T~dias_treat_imp_sin_na_1)))

ggcoxfunctional(res.cox_dias_treat,  data = Base_fiscalia_v14%>% dplyr::filter(!is.na(dias_treat_imp_sin_na_1)) %>% dplyr::mutate(dias_treat_imp_sin_na_1= dplyr::case_when(dias_treat_imp_sin_na_1==0~.0001,T~dias_treat_imp_sin_na_1)), point.col = "blue", point.alpha = 0.5)

cat("______________________________________________________________________")

res.cox_num_hijos <- coxph(Surv(yrs_to_condemn,  event_r) ~ numero_de_hijos_mod + log(numero_de_hijos_mod) + I(log(numero_de_hijos_mod)^2) + I(numero_de_hijos_mod^2) +  I(sqrt(numero_de_hijos_mod)), data = Base_fiscalia_v14%>% dplyr::filter(!is.na(numero_de_hijos_mod)) %>% dplyr::mutate(numero_de_hijos_mod= dplyr::case_when(numero_de_hijos_mod==0~.0001,T~as.numeric(numero_de_hijos_mod))))

ggcoxfunctional(res.cox_num_hijos,  data =  Base_fiscalia_v14%>% dplyr::filter(!is.na(numero_de_hijos_mod)) %>% dplyr::mutate(numero_de_hijos_mod= dplyr::case_when(numero_de_hijos_mod==0~.0001,T~as.numeric(numero_de_hijos_mod))), point.col = "blue", point.alpha = 0.5)

cat("______________________________________________________________________")

res.cox_n_prev_off <- coxph(Surv(yrs_to_condemn,  event_r) ~ n_prev_off + log(n_prev_off) + I(log(n_prev_off)^2) + I(n_prev_off^2) +  I(sqrt(n_prev_off)), data = Base_fiscalia_v14%>% dplyr::filter(!is.na(n_prev_off)) %>% dplyr::mutate(n_prev_off= dplyr::case_when(n_prev_off==0~.0001,T~as.numeric(n_prev_off))))

ggcoxfunctional(res.cox_n_prev_off,  data =  Base_fiscalia_v14%>% dplyr::filter(!is.na(n_prev_off)) %>% dplyr::mutate(n_prev_off= dplyr::case_when(n_prev_off==0~.0001,T~as.numeric(n_prev_off))), point.col = "blue", point.alpha = 0.5)

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
check_PH <- cox.zph(coxfit_more_var3, transform="km")
data.frame(check_PH$table)%>% 
  knitr::kable("markdown", caption= "Schoefeld residuals") #sigue alto

Time for this code chunk to run: 0 minutes

Show code
check_PH <- cox.zph(coxfit_more_var3, transform="km")
data.frame(check_PH$table)%>% 
  knitr::kable("markdown", caption= "Schoefeld residuals")
Table 6: Schoefeld residuals
chisq df p
motivodeegreso_mod_imp_rec 28.4599124 2 0.0000007
rcs(edad_al_ing_1, 4) 8.6739796 3 0.0339547
tr_modality 4.6696811 1 0.0306996
sex 0.1317724 1 0.7166014
edad_ini_cons 0.0079184 1 0.9290937
escolaridad_rec 2.7041999 2 0.2586964
sus_principal_mod 9.9440294 4 0.0413811
freq_cons_sus_prin 25.0291742 4 0.0000496
condicion_ocupacional_corr 24.4967792 5 0.0001742
policonsumo 0.0101998 1 0.9195552
num_hijos_mod_joel_bin 1.3474156 1 0.2457304
tenencia_de_la_vivienda_mod 3.9330721 4 0.4151393
macrozona 2.8460039 2 0.2409895
n_off_vio 8.3855458 1 0.0037822
n_off_acq 131.1704988 1 0.0000000
n_off_sud 5.9081309 1 0.0150712
n_off_oth 16.8663623 1 0.0000401
dg_cie_10_rec 6.7457321 2 0.0342912
dg_trs_cons_sus_or 1.6669248 1 0.1966709
clas_r 2.8567199 2 0.2397017
porc_pobr 0.0189494 1 0.8905116
sus_ini_mod_mvv 3.1737346 4 0.5291837
ano_nac_corr 0.0503375 1 0.8224771
con_quien_vive_joel 11.7247773 3 0.0083880
fis_comorbidity_icd_10 1.3600074 2 0.5066151
GLOBAL 228.0054617 51 0.0000000

Time for this code chunk to run: 0 minutes

Show code
#numero_de_hijos_mod_joel ano_nac_corr con_quien_vive_joel

coxfit_more_var21 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec +", paste(setdiff(vars_cov,""), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)#[1:250,]
#n_prev_off + 
coxfit_more_var205 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec +", paste(setdiff(vars_cov,""), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris %>% dplyr::mutate(edad_al_ing_1= VGAM::yeo.johnson(Base_fiscalia_v14$edad_al_ing_1, .5)))#[1:250,]


#*If you are not directly interested in the relations of x1 or x2 to outcome and are simply controlling for them to analyze your predictor of main interest, then you don't have to worry much about the initially confusing display of p-values and hazard ratios for each of the terms associated with the splines.

coxfit_more_var22 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,3) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

coxfit_more_var23 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,4) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

coxfit_more_var24 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,5) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

coxfit_more_var25 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,6) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

coxfit_more_var26 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

coxfit_more_var27 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1,df= 2) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

coxfit_more_var28 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1,df= 3) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

coxfit_more_var29 <- coxph(as.formula(paste("Surv(yrs_to_pris,  event_r) ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1,df= 4) +", paste(setdiff(vars_cov,"edad_al_ing_1"), collapse = "+"))), ties="efron", data= Base_fiscalia_v14_pris)

anova(coxfit_more_var205, coxfit_more_var21, coxfit_more_var22, coxfit_more_var23, coxfit_more_var24, coxfit_more_var25, coxfit_more_var26, coxfit_more_var27, coxfit_more_var28, coxfit_more_var29)
Analysis of Deviance Table
 Cox model: response is  Surv(yrs_to_pris, event_r)
 Model  1: ~ motivodeegreso_mod_imp_rec + motivodeegreso_mod_imp_rec + tr_modality + edad_al_ing_1 + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  2: ~ motivodeegreso_mod_imp_rec + motivodeegreso_mod_imp_rec + tr_modality + edad_al_ing_1 + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  3: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 3) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  4: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  5: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 5) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  6: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 6) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  7: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  8: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 2) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model  9: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 3) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 10: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
   loglik    Chisq Df Pr(>|Chi|)    
1  -39826                           
2  -39768 117.1452  0  < 2.2e-16 ***
3  -39754  27.9776  1  1.227e-07 ***
4  -39752   3.0226  1    0.08211 .  
5  -39752   0.2509  1    0.61641    
6  -39749   5.8079  1    0.01595 *  
7  -39768  36.5571  4  2.222e-07 ***
8  -39754  26.5960  1  2.508e-07 ***
9  -39753   3.3076  1    0.06896 .  
10 -39752   0.2305  1    0.63116    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Show code
anova(coxfit_more_var23, coxfit_more_var25, coxfit_more_var29,coxfit_more_var28)
Analysis of Deviance Table
 Cox model: response is  Surv(yrs_to_pris, event_r)
 Model 1: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 2: ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1, 6) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 3: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 4) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
 Model 4: ~ motivodeegreso_mod_imp_rec + ns(edad_al_ing_1, df = 3) + motivodeegreso_mod_imp_rec + tr_modality + sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10
  loglik  Chisq Df Pr(>|Chi|)  
1 -39752                       
2 -39749 5.5569  2    0.06213 .
3 -39752 6.4229  1    0.01127 *
4 -39753 0.2305  1    0.63116  
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Show code
invisible("coxfit_more_var25 , rcs 6, the best; but for linearity, coxfit_more_var23")

# Dimitris Rizopoulos (https://stats.stackexchange.com/users/219012/dimitris-rizopoulos), Check log-linearity of a continuous variable to predict survival, URL (version: 2018-10-08): https://stats.stackexchange.com/q/370353

# Create an effect plot to depict the relationship
ND_pris <- with(Base_fiscalia_v14_pris, 
data.frame(edad_al_ing_1 = seq(min(edad_al_ing_1),  max(edad_al_ing_1), length.out = 50),
            motivodeegreso_mod_imp_rec=rep(Mode(motivodeegreso_mod_imp_rec),50),
            #edad_al_ing_1=rep(mean(edad_al_ing_1, na.rm=T))
            tr_modality= rep(Mode(tr_modality),50),
            sex= rep(Mode(sex),50),
            edad_ini_cons=rep(quantile(edad_ini_cons, .5, na.rm=T),50),
            escolaridad_rec= rep(Mode(escolaridad_rec),50),
            sus_principal_mod= rep(Mode(sus_principal_mod),50),
            freq_cons_sus_prin= rep(Mode(freq_cons_sus_prin),50),
            condicion_ocupacional_corr= rep(Mode(condicion_ocupacional_corr),50),
            policonsumo= rep(Mode(policonsumo),50),
            num_hijos_mod_joel_bin= rep(Mode(num_hijos_mod_joel_bin),50),
            tenencia_de_la_vivienda_mod= rep(Mode(tenencia_de_la_vivienda_mod),50),
            macrozona = rep(Mode(macrozona),50),
            #n_prev_off= rep(quantile(n_prev_off, .5, na.rm=T),50),
            n_off_vio= rep(Mode(n_off_vio),50),
            n_off_acq= rep(Mode(n_off_acq),50),
            n_off_sud= rep(Mode(n_off_sud),50),
            n_off_oth= rep(Mode(n_off_oth),50),
            dg_cie_10_rec = rep(Mode(dg_cie_10_rec),50),
            dg_trs_cons_sus_or = rep(Mode(dg_trs_cons_sus_or),50),
            clas_r= rep(Mode(clas_r),50),
            porc_pobr=rep(quantile(porc_pobr, .5, na.rm=T),50),
            sus_ini_mod_mvv=rep(Mode(sus_ini_mod_mvv),50),
            ano_nac_corr=rep(quantile(ano_nac_corr, .5, na.rm=T),50),
            con_quien_vive_joel=rep(Mode(con_quien_vive_joel),50),
            fis_comorbidity_icd_10=rep(Mode(fis_comorbidity_icd_10),50)
))

prs_pris <- predict(coxfit_more_var23, newdata = ND_pris, type = "lp", se.fit = TRUE)
ND_pris$pred <- prs_pris[[1]]
ND_pris$se <- prs_pris[[2]]
ND_pris$lo <- ND_pris$pred - 1.96 * ND_pris$se
ND_pris$up <- ND_pris$pred + 1.96 * ND_pris$se

xyplot(pred + lo + up ~ edad_al_ing_1, data = ND_pris,
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Admission age")
Show code
prs2 <- predict(coxfit_more_var23, newdata = ND_pris%>% dplyr::mutate(edad_ini_cons= seq(min(Base_fiscalia_v14_pris$edad_ini_cons,na.rm=T),  max(Base_fiscalia_v14_pris$edad_ini_cons,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14_pris$edad_al_ing_1, .5, na.rm=T))), type = "lp", se.fit = TRUE)
ND_pris$pred2 <- prs2[[1]]
ND_pris$se2 <- prs2[[2]]
ND_pris$lo2 <- ND_pris$pred2 - 1.96 * ND_pris$se2
ND_pris$up2 <- ND_pris$pred2 + 1.96 * ND_pris$se2

xyplot(pred2 + lo2 + up2 ~ edad_ini_cons, data = ND_pris%>% dplyr::mutate(edad_ini_cons= seq(min(Base_fiscalia_v14_pris$edad_ini_cons,na.rm=T),  max(Base_fiscalia_v14_pris$edad_ini_cons,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14_pris$edad_al_ing_1, .5, na.rm=T))),
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Substance use onset age")
Show code
prs3 <- predict(coxfit_more_var23, newdata = ND_pris%>% dplyr::mutate(ano_nac_corr= seq(min(Base_fiscalia_v14_pris$ano_nac_corr,na.rm=T),  max(Base_fiscalia_v14_pris$ano_nac_corr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14_pris$edad_al_ing_1, .5, na.rm=T))), type = "lp", se.fit = TRUE)
ND_pris$pred3 <- prs3[[1]]
ND_pris$se3 <- prs3[[2]]
ND_pris$lo3 <- ND_pris$pred3 - 1.96 * ND_pris$se3
ND_pris$up3 <- ND_pris$pred3 + 1.96 * ND_pris$se3

xyplot(pred3 + lo3 + up3 ~ ano_nac_corr, data = ND_pris%>% dplyr::mutate(ano_nac_corr= seq(min(Base_fiscalia_v14_pris$ano_nac_corr,na.rm=T),  max(Base_fiscalia_v14_pris$ano_nac_corr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14_pris$edad_al_ing_1, .5, na.rm=T))),
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Year of birth")
Show code
prs4 <- predict(coxfit_more_var23, newdata = ND_pris%>% dplyr::mutate(porc_pobr= seq(min(Base_fiscalia_v14_pris$porc_pobr,na.rm=T),  max(Base_fiscalia_v14_pris$porc_pobr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14_pris$edad_al_ing_1, .5, na.rm=T))), type = "lp", se.fit = TRUE)
ND_pris$pred4 <- prs4[[1]]
ND_pris$se4 <- prs4[[2]]
ND_pris$lo4 <- ND_pris$pred4 - 1.96 * ND_pris$se4
ND_pris$up4 <- ND_pris$pred4 + 1.96 * ND_pris$se4

xyplot(pred4 + lo4 + up4 ~ porc_pobr, data = ND_pris%>% dplyr::mutate(porc_pobr= seq(min(Base_fiscalia_v14_pris$porc_pobr,na.rm=T),  max(Base_fiscalia_v14_pris$porc_pobr,na.rm=T), length.out = 50))%>% dplyr::mutate(`rcs(edad_al_ing_1, 4)edad_al_ing_1`= log(quantile(Base_fiscalia_v14_pris$edad_al_ing_1, .5, na.rm=T))),
       type = "l", col = "black", lwd = 2, lty = c(1, 2, 2),
       abline = list(h = 0, lty = 2, lwd = 2, col = "red"),
       xlab = "Risk Factor", ylab = "log Hazard Ratio", main="Percentage of poverty")

Time for this code chunk to run: 0.4 minutes

Show code
check_PH_pris <- cox.zph(coxfit_more_var23, transform="km")
data.frame(check_PH_pris$table)%>% 
  knitr::kable("markdown", caption= "Schoefeld residuals (prison)")
Table 7: Schoefeld residuals (prison)
chisq df p
motivodeegreso_mod_imp_rec 10.6217000 2 0.0049377
rcs(edad_al_ing_1, 4) 7.1673488 3 0.0667508
tr_modality 0.3747307 1 0.5404368
sex 0.3234527 1 0.5695400
edad_ini_cons 8.3956460 1 0.0037612
escolaridad_rec 9.1528430 2 0.0102917
sus_principal_mod 6.3557274 4 0.1741108
freq_cons_sus_prin 14.2030735 4 0.0066744
condicion_ocupacional_corr 11.0015826 5 0.0513486
policonsumo 2.1221488 1 0.1451827
num_hijos_mod_joel_bin 0.0604707 1 0.8057536
tenencia_de_la_vivienda_mod 0.5902843 4 0.9641372
macrozona 6.9003178 2 0.0317406
n_off_vio 18.7513966 1 0.0000149
n_off_acq 54.9577108 1 0.0000000
n_off_sud 5.0019251 1 0.0253191
n_off_oth 23.5580661 1 0.0000012
dg_cie_10_rec 4.0544209 2 0.1317024
dg_trs_cons_sus_or 4.3349584 1 0.0373373
clas_r 3.5636370 2 0.1683318
porc_pobr 1.0689267 1 0.3011880
sus_ini_mod_mvv 6.6207039 4 0.1573422
ano_nac_corr 0.9535141 1 0.3288264
con_quien_vive_joel 9.3129256 3 0.0254071
fis_comorbidity_icd_10 3.3120123 2 0.1908999
GLOBAL 178.1378997 51 0.0000000

Time for this code chunk to run: 0 minutes


Kaplan-Meier

Show code
#Bradburn, M., Clark, T., Love, S. et al. Survival Analysis Part III: Multivariate data analysis – choosing a model and assessing its adequacy and fit. Br J Cancer 89, 605–611 (2003). https://doi.org/10.1038/sj.bjc.6601120

KM_fit <- survfit(Surv(age_offending_imp-edad_al_egres_imp, event_r) ~ 
    strata(motivodeegreso_mod_imp_rec), data = Base_fiscalia_v14)

par(mfrow = c(1, 2))

plot(KM_fit, xlab = "Years", ylab = "Survival", col = 2:4, main="Survival")
#legend("topright", levels(factor(Base_fiscalia_v14$motivodeegreso_mod_imp_rec)), lty = 1, col = 2:4, bty = "n")

plot(KM_fit, fun = function (s) -log(-log(s)), xlab = "Years", 
    ylab = "-log(- log(Survival))", col = 2:4, main="-Log(−log(survival)) ")
legend("topright", levels(factor(Base_fiscalia_v14$motivodeegreso_mod_imp_rec)), lty = 1, col = 2:4, bty = "n")
Kaplan-Meier (Cond. Sentence)

Figure 1: Kaplan-Meier (Cond. Sentence)

Time for this code chunk to run: 0 minutes

Show code
KM_fit_pris <- survfit(Surv(age_offending_imp-edad_al_egres_imp, event_r) ~ 
    strata(motivodeegreso_mod_imp_rec), data = Base_fiscalia_v14_pris)

par(mfrow = c(1, 2))

plot(KM_fit_pris, xlab = "Years", ylab = "Survival", col = 2:4, main="Survival")
#legend("topright", levels(factor(Base_fiscalia_v14$motivodeegreso_mod_imp_rec)), lty = 1, col = 2:4, bty = "n")

plot(KM_fit_pris, fun = function (s) -log(-log(s)), xlab = "Years", 
    ylab = "-log(- log(Survival))", col = 2:4, main="-Log(−log(survival)) ")
legend("topright", levels(factor(Base_fiscalia_v14_pris$motivodeegreso_mod_imp_rec)), lty = 1, col = 2:4, bty = "n")
Kaplan-Meier (Prison)

Figure 2: Kaplan-Meier (Prison)

Time for this code chunk to run: 0 minutes


Show code
par(mfrow = c(2, 1))

plot(check_PH, var = 1, ylab= "Beta(t) for Treatment outcome", col = "red", lwd = 2, main= "Cond. Sentence", xlab="")
abline(h = coef(coxfit_more_var3)[1], col = "red", lwd = 2)

plot(check_PH_pris, var = 1, ylab= "Beta(t) for Treatment outcome", col = "red", lwd = 2, main="Prison")
abline(h = coef(coxfit_more_var23)[1], col = "red", lwd = 2)
Schoefeld residuals, Treatment Outcome

Figure 3: Schoefeld residuals, Treatment Outcome

Time for this code chunk to run: 0 minutes

Time for this code chunk to run: 0 minutes

Show code
cat("(==============================# EXPERIMENTAL, COXED ======================================)")
invisible("http://dx.doi.org/10.7910/DVN/ELT9VD")
invisible("https://cran.r-project.org/web/packages/coxed/coxed.pdf")
invisible("https://cran.r-project.org/web/packages/coxed/vignettes/coxed.html")

system.time({
npsf1<-coxed(coxfit_more_var8, method="npsf")
})
warning("Error: cannot allocate vector of size 18.3 Gb")
#https://search.r-project.org/CRAN/refmans/pec/html/predictSurvProb.html
system.time({
npsf2<-coxed(coxfit_more_var8, method="npsf", bootstrap = T)
})
system.time({
coxed_gam<-coxed(coxfit_more_var8, method="gam",bootstrap = TRUE, B=100,
  newdata= dplyr::mutate(Base_fiscalia_v14, motivodeegreso_mod_imp_rec="Treatment completion"), 
  newdata2= dplyr::mutate(Base_fiscalia_v14,motivodeegreso_mod_imp_rec="Treatment non-completion (Early)"),
  newdata3= dplyr::mutate(Base_fiscalia_v14,motivodeegreso_mod_imp_rec="Treatment non-completion (Late)")) #newdata3= dplyr::mutate(motivodeegreso_mod_imp_rec="Treatment non-completion (Late)")
}) 
invisible("Inclusion of censored cases increases mean duration by more than 25%. Consider using method='npsf' instead")

cat("(====================================================================)")

invisible("https://cran.r-project.org/web/packages/randomForestSRC/randomForestSRC.pdf")

follic.obj <- randomForestSRC::rfsrc(as.formula(paste("Surv(yrs_to_condemn,  event_r) ~ ", paste(vars_cov, collapse = "+ "))), Base_fiscalia_v14_mod %>% dplyr::mutate(cat_ocupacional_corr= factor(cat_ocupacional_corr), dg_trs_cons_sus_or= factor(dg_trs_cons_sus_or)), nsplit = 3, ntree = 100)

p.v <- plot.variable(follic.obj, target = 2)
p.v$plots.per.page <- 1


invisible("Error in finalizeData(c(subj.names, yvar.names, xvar.names), data, na.action,  : 
  data types cannot be character: please convert all characters to factors")

Time for this code chunk to run: 0 minutes

Show code
# 
cat("(==============================# EXPERIMENTAL, Survex ======================================)")
require("mboost")

prueba_stata<-rio::import("mariel_feb_23.dta")

my_vector <- c("motivodeegreso_mod_imp_rec", "tr_mod2", "edad_al_ing_1", "sex_dum2", "edad_ini_cons", "esc1", "esc2", "sus_prin2", "sus_prin3", "sus_prin4", "sus_prin5", "fr_cons_sus_prin2", "fr_cons_sus_prin3", "fr_cons_sus_prin4", "fr_cons_sus_prin5", "cond_ocu2", "cond_ocu3", "cond_ocu4", "cond_ocu5", "cond_ocu6", "policonsumo", "num_hij2", "tenviv1", "tenviv2", "tenviv4", "tenviv5", "mzone2", "mzone3", "n_off_vio", "n_off_acq", "n_off_sud", "n_off_oth", "rural2", "rural3", "porc_pobr", "susini2", "susini3", "susini4", "susini5", "dg_fis_anemia", "dg_fis_card", "dg_fis_enf_som", "dg_fis_ets", "dg_fis_hep_alc", "dg_fis_hep_b", "dg_fis_hep_cro", "dg_fis_inf", "dg_fis_otr_cond_fis_ries_vit", "dg_fis_otr_cond_fis", "dg_fis_pat_buc", "dg_fis_pat_ges_intrau", "dg_fis_trau_sec")

# create a model
#ranger(as.formula(paste("Surv(diff, event)~ ", paste(my_vector, collapse = "+"))), data = prueba_stata[complete.cases(prueba_stata[,my_vector]),])
model <- 
  glmboost(as.formula(paste("Surv(diff, event)~ ", paste(my_vector, collapse = "+"))),
  data = prueba_stata, family = CoxPH(), center = TRUE, control=boost_control(mstop = 500))

#Computes the predicted survivor function for a Cox proportional hazards model.
pred_boost_cph<-survFit(model)

plot(varimp(model), blorder = "rev_alphabetical")

Time for this code chunk to run: 0 minutes


Missing data

Show code
set.seed(2125)
Base_fiscalia_v14_miss <-
subset(Base_fiscalia_v14, select= c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec", "offender_d", "age_offending_imp", "edad_al_egres_imp")) %>% 
  dplyr::arrange(hash_key, fech_ing_num_1) %>% 
   missRanger::missRanger(
                formula = .  -offender_d - age_offending_imp ~ . - hash_key -offender_d - age_offending_imp,
                num.trees = 200, 
                pmm.k = 3,                
                returnOOB=T,
                maxiter= 50,
                verbose = 2, 
                seed = 2125)

Missing value imputation by random forests

  Variables to impute:      motivodeegreso_mod_imp_rec, tr_modality, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, con_quien_vive_joel
  Variables used to impute: motivodeegreso_mod_imp_rec, tr_modality, edad_al_ing_1, sex, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, policonsumo, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, n_off_vio, n_off_acq, n_off_sud, n_off_oth, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, fis_comorbidity_icd_10, fech_ing_num_1, fech_nac_rec, edad_al_egres_imp
    ss_pr_  cndc__  dg____  cn_q__  clas_r  prc_pb  mtv___  macrzn  tr_mdl  esclr_  frq___  nm____  tn____  ss_n__  edd_n_
iter 1: 0.4715  0.4586  0.2777  0.4710  0.1801  0.5762  0.3832  0.1647  0.1253  0.4401  0.5063  0.7866  0.4508  0.3615  0.7205  
iter 2: 0.0008  0.0054  0.0000  0.0000  0.0000  0.0159  0.0000  0.0000  0.0000  0.0000  0.0003  0.0027  0.0022  0.0004  0.0293  
iter 3: 0.0015  0.0053  0.0000  0.0000  0.0000  0.0156  0.0000  0.0000  0.0000  0.0000  0.0004  0.0023  0.0019  0.0002  0.0294  
iter 4: 0.0007  0.0049  0.0000  0.0000  0.0000  0.0151  0.0000  0.0000  0.0000  0.0000  0.0003  0.0028  0.0018  0.0006  0.0293  
iter 5: 0.0013  0.0053  0.0000  0.0000  0.0000  0.0158  0.0000  0.0000  0.0000  0.0000  0.0006  0.0024  0.0024  0.0005  0.0285  
Show code
set.seed(2125)
  Base_fiscalia_v14_pris_miss <-
subset(Base_fiscalia_v14_pris, select= c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec", "offender_d", "age_offending_imp", "edad_al_egres_imp")) %>% 
     missRanger::missRanger(
                formula = .  -offender_d - age_offending_imp ~ . - hash_key -offender_d - age_offending_imp,
                num.trees = 200, 
                pmm.k = 3,
                returnOOB=T,
                maxiter= 50,
                verbose = 2,
                seed = 2125)

Missing value imputation by random forests

  Variables to impute:      motivodeegreso_mod_imp_rec, tr_modality, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, con_quien_vive_joel
  Variables used to impute: motivodeegreso_mod_imp_rec, tr_modality, edad_al_ing_1, sex, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, policonsumo, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, n_off_vio, n_off_acq, n_off_sud, n_off_oth, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, fis_comorbidity_icd_10, fech_ing_num_1, fech_nac_rec, edad_al_egres_imp
    ss_pr_  cndc__  dg____  cn_q__  clas_r  prc_pb  mtv___  macrzn  tr_mdl  esclr_  frq___  nm____  tn____  ss_n__  edd_n_
iter 1: 0.4715  0.4586  0.2777  0.4710  0.1801  0.5762  0.3832  0.1647  0.1253  0.4401  0.5063  0.7866  0.4508  0.3615  0.7205  
iter 2: 0.0008  0.0054  0.0000  0.0000  0.0000  0.0159  0.0000  0.0000  0.0000  0.0000  0.0003  0.0027  0.0022  0.0004  0.0293  
iter 3: 0.0015  0.0053  0.0000  0.0000  0.0000  0.0156  0.0000  0.0000  0.0000  0.0000  0.0004  0.0023  0.0019  0.0002  0.0294  
iter 4: 0.0007  0.0049  0.0000  0.0000  0.0000  0.0151  0.0000  0.0000  0.0000  0.0000  0.0003  0.0028  0.0018  0.0006  0.0293  
iter 5: 0.0013  0.0053  0.0000  0.0000  0.0000  0.0158  0.0000  0.0000  0.0000  0.0000  0.0006  0.0024  0.0024  0.0005  0.0285  
Show code
set.seed(2125)
Base_fiscalia_v14_miss2 <-
subset(Base_fiscalia_v14, select= c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec", "offender_d", "age_offending_imp", "edad_al_egres_imp")) %>% 
  dplyr::arrange(hash_key, fech_ing_num_1) %>% 
  dplyr::mutate(dg_cie_10_rec= dplyr::case_when(as.character(dg_cie_10_rec)=="Diagnosis unknown (under study)"~NA_character_,T~as.character(dg_cie_10_rec)))%>%
  #code to multiple response in  physical comorbidity (deprecated)
  #dplyr::mutate(SUM = rowSums(dplyr::mutate_all(dplyr::select(., dplyr::starts_with("dg_fis")),~as.numeric(.)-1))) %>% 
  #dplyr::mutate(across(dplyr::starts_with("dg_fis"), ~factor(dplyr::case_when(as.character(dg_fis_in_study)=="Presence" & SUM==1 ~NA_character_,T~as.character(.))))) %>%
  dplyr::mutate(fis_comorbidity_icd_10= dplyr::case_when(as.character(fis_comorbidity_icd_10)=="Diagnosis unknown (under study)"~NA_character_,T~as.character(fis_comorbidity_icd_10)))%>% 
  missRanger::missRanger(
                formula = .  -offender_d - age_offending_imp ~ . - hash_key -offender_d - age_offending_imp,
                num.trees = 200, 
                pmm.k = 3,                
                returnOOB=T,
                maxiter= 50,
                verbose = 2, 
                seed = 2125)

Missing value imputation by random forests

  Variables to impute:      motivodeegreso_mod_imp_rec, tr_modality, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, con_quien_vive_joel, fis_comorbidity_icd_10
  Variables used to impute: motivodeegreso_mod_imp_rec, tr_modality, edad_al_ing_1, sex, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, policonsumo, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, n_off_vio, n_off_acq, n_off_sud, n_off_oth, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, fis_comorbidity_icd_10, fech_ing_num_1, fech_nac_rec, edad_al_egres_imp
    ss_pr_  cndc__  dg____  cn_q__  clas_r  prc_pb  mtv___  macrzn  tr_mdl  esclr_  frq___  nm____  tn____  ss_n__  edd_n_  d__10_  f___10
iter 1: 0.4703  0.4618  0.2807  0.4606  0.1783  0.5871  0.4417  0.1553  0.1279  0.4440  0.5091  0.7899  0.4529  0.3628  0.7179  0.3865  0.1358  
iter 2: 0.0009  0.0056  0.0000  0.0000  0.0000  0.0170  0.0000  0.0000  0.0000  0.0000  0.0004  0.0023  0.0022  0.0004  0.0301  0.0000  0.0000  
iter 3: 0.0013  0.0058  0.0000  0.0000  0.0000  0.0139  0.0000  0.0000  0.0000  0.0000  0.0003  0.0028  0.0022  0.0001  0.0286  0.0000  0.0000  
iter 4: 0.0015  0.0054  0.0000  0.0000  0.0000  0.0147  0.0000  0.0000  0.0000  0.0000  0.0004  0.0024  0.0020  0.0003  0.0281  0.0000  0.0000  
iter 5: 0.0015  0.0052  0.0000  0.0000  0.0000  0.0161  0.0000  0.0000  0.0000  0.0000  0.0005  0.0026  0.0024  0.0004  0.0278  0.0000  0.0000  
Show code
set.seed(2125)
  Base_fiscalia_v14_pris_miss2 <-
subset(Base_fiscalia_v14_pris, select= c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec", "offender_d", "age_offending_imp", "edad_al_egres_imp")) %>% 
  dplyr::arrange(hash_key, fech_ing_num_1) %>% 
  dplyr::mutate(dg_cie_10_rec= dplyr::case_when(as.character(dg_cie_10_rec)=="Diagnosis unknown (under study)"~NA_character_,T~as.character(dg_cie_10_rec)))%>%
  # dplyr::mutate(SUM = rowSums(dplyr::mutate_all(dplyr::select(., dplyr::starts_with("dg_fis")),~as.numeric(.)-1))) %>% 
  # dplyr::mutate(across(dplyr::starts_with("dg_fis"), ~factor(dplyr::case_when(as.character(dg_fis_in_study)=="Presence" & SUM==1 ~NA_character_,T~as.character(.))))) %>%
  dplyr::mutate(fis_comorbidity_icd_10= dplyr::case_when(as.character(fis_comorbidity_icd_10)=="Diagnosis unknown (under study)"~NA_character_,T~as.character(fis_comorbidity_icd_10)))%>% 
   missRanger::missRanger(
                formula = .  -offender_d - age_offending_imp ~ . - hash_key -offender_d - age_offending_imp,
                num.trees = 200, 
                pmm.k = 3,                
                returnOOB=T,
                maxiter= 50,
                verbose = 2, 
                seed = 2125)

Missing value imputation by random forests

  Variables to impute:      motivodeegreso_mod_imp_rec, tr_modality, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, con_quien_vive_joel, fis_comorbidity_icd_10
  Variables used to impute: motivodeegreso_mod_imp_rec, tr_modality, edad_al_ing_1, sex, edad_ini_cons, escolaridad_rec, sus_principal_mod, freq_cons_sus_prin, condicion_ocupacional_corr, policonsumo, num_hijos_mod_joel_bin, tenencia_de_la_vivienda_mod, macrozona, n_off_vio, n_off_acq, n_off_sud, n_off_oth, dg_cie_10_rec, dg_trs_cons_sus_or, clas_r, porc_pobr, sus_ini_mod_mvv, ano_nac_corr, con_quien_vive_joel, fis_comorbidity_icd_10, fech_ing_num_1, fech_nac_rec, edad_al_egres_imp
    ss_pr_  cndc__  dg____  cn_q__  clas_r  prc_pb  mtv___  macrzn  tr_mdl  esclr_  frq___  nm____  tn____  ss_n__  edd_n_  d__10_  f___10
iter 1: 0.4703  0.4618  0.2807  0.4606  0.1783  0.5871  0.4417  0.1553  0.1279  0.4440  0.5091  0.7899  0.4529  0.3628  0.7179  0.3865  0.1358  
iter 2: 0.0009  0.0056  0.0000  0.0000  0.0000  0.0170  0.0000  0.0000  0.0000  0.0000  0.0004  0.0023  0.0022  0.0004  0.0301  0.0000  0.0000  
iter 3: 0.0013  0.0058  0.0000  0.0000  0.0000  0.0139  0.0000  0.0000  0.0000  0.0000  0.0003  0.0028  0.0022  0.0001  0.0286  0.0000  0.0000  
iter 4: 0.0015  0.0054  0.0000  0.0000  0.0000  0.0147  0.0000  0.0000  0.0000  0.0000  0.0004  0.0024  0.0020  0.0003  0.0281  0.0000  0.0000  
iter 5: 0.0015  0.0052  0.0000  0.0000  0.0000  0.0161  0.0000  0.0000  0.0000  0.0000  0.0005  0.0026  0.0024  0.0004  0.0278  0.0000  0.0000  

Time for this code chunk to run: 0.4 minutes


Export

Show code
#2023-01-01 we had a problem with edad_al_ing_fmt. This variable predict better (it had lower amount of missing data and was in wide format)

#cont_vars_desc[1]<-ifelse(cont_vars_desc[1]=="edad_al_ing_fmt","edad_al_ing_1",cont_vars_desc[1])

library(missRanger)
#attr(Base_fiscalia_v14$tipo_centro,"label") <- "Type of Center"

subset(Base_fiscalia_v14, select= c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec", "offender_d", "age_offending_imp", "edad_al_egres_imp")) %>% 
  dplyr::arrange(hash_key, fech_ing_num_1)  %>% 
  dplyr::mutate(age_at_censor_date=lubridate::time_length(lubridate::interval(fech_nac_rec, as.Date("2019-11-13")),unit="years")) %>% 
  data.table::data.table() %>% 
  rio::export(file = paste0("fiscalia_mariel_feb_2023_match_SENDA.dta"))

subset(Base_fiscalia_v14_pris, select= c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec", "offender_d", "age_offending_imp", "edad_al_egres_imp")) %>% 
  dplyr::arrange(hash_key, fech_ing_num_1) %>%   
  dplyr::mutate(age_at_censor_date=lubridate::time_length(lubridate::interval(fech_nac_rec, as.Date("2019-11-13")),unit="years")) %>% 
  data.table::data.table() %>% 
  rio::export(file = paste0("fiscalia_mariel_feb_2023_match_SENDA_pris.dta"))


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
# IMPUTATION
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

Base_fiscalia_v14_miss %>% 
  dplyr::mutate(age_at_censor_date=lubridate::time_length(lubridate::interval(fech_nac_rec, as.Date("2019-11-13")),unit="years")) %>% 
  data.table::data.table() %>% 
  rio::export(file = paste0("fiscalia_mariel_feb_2023_match_SENDA_miss.dta"))

  
Base_fiscalia_v14_pris_miss %>%   
  dplyr::mutate(age_at_censor_date=lubridate::time_length(lubridate::interval(fech_nac_rec, as.Date("2019-11-13")),unit="years")) %>% 
  data.table::data.table() %>% 
  rio::export(file = paste0("fiscalia_mariel_feb_2023_match_SENDA_miss_pris.dta"))


Base_fiscalia_v14_miss2 %>% 
  dplyr::mutate(age_at_censor_date=lubridate::time_length(lubridate::interval(fech_nac_rec, as.Date("2019-11-13")),unit="years")) %>% 
  data.table::data.table() %>% 
  rio::export(file = paste0("fiscalia_mariel_feb_2023_match_SENDA_miss2.dta"))

  
Base_fiscalia_v14_pris_miss2 %>%   
  dplyr::mutate(age_at_censor_date=lubridate::time_length(lubridate::interval(fech_nac_rec, as.Date("2019-11-13")),unit="years")) %>% 
  data.table::data.table() %>% 
  rio::export(file = paste0("fiscalia_mariel_feb_2023_match_SENDA_miss_pris2.dta"))

Time for this code chunk to run: 0.1 minutes



Characteristics

Show code
# Treatment status (Early dropout/Late dropout/Treatment completion) 

as.data.frame.TableOne <- function(x, ...) {capture.output(print(x,
                          showAllLevels = TRUE, ...) -> x)
  y <- as.data.frame(x)
  y$characteristic <- dplyr::na_if(rownames(x), "")
  y <- y %>%
  fill(characteristic, .direction = "down") %>%
  select(characteristic, everything())
  rownames(y) <- NULL
  y}

tbone_desc_merge5<-
CreateTableOne(vars=c(setdiff(vars_cov, "motivodeegreso_mod_imp_rec"), "offender_d"), data=  subset(Base_fiscalia_v14, select=c(vars_cov, "offender_d"), subset=!is.na(motivodeegreso_mod_imp_rec)), factorVars = setdiff(vars_cov, c("motivodeegreso_mod_imp_rec","edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr")), smd=T, strata="motivodeegreso_mod_imp_rec", addOverall = T, includeNA=T, test=T)

as.data.frame.TableOne(tbone_desc_merge5, smd=T, nonnormal= T)%>% 
  dplyr::mutate(char2=characteristic) %>% 
  tidyr::fill(char2) %>% 
  dplyr::select(char2,everything()) %>% 
  dplyr::mutate(level=ifelse(is.na(level),"[Missing]",level)) %>% 
  dplyr::mutate(char2=dplyr::case_when(characteristic=="NA"~NA_character_,T~as.character(characteristic))) %>% 
  format_cells(1, 1:length(names(.)), "bold") %>%
  dplyr::select(-1) %>% 
  knitr::kable(size=10, format="markdown",caption= "Summary descriptives, Condemnatory sentence(1), Found as an imputed (YES), by Baseline Treatment Status", escape=T)

fill: changed 56 values (68%) of ‘characteristic’ (56 fewer NA)

Table 8: Summary descriptives, Condemnatory sentence(1), Found as an imputed (YES), by Baseline Treatment Status
characteristic level Overall Treatment completion Treatment non-completion (Early) Treatment non-completion (Late) p test SMD
n 70854 19276 15797 35781
tr_modality (%) Ambulatory 60395 (85.2) 15605 (81.0) 12661 (80.1) 32129 (89.8) <0.001 0.184
tr_modality (%) Residential 10391 (14.7) 3656 (19.0) 3115 (19.7) 3620 (10.1)
tr_modality (%) [Missing] 68 ( 0.1) 15 ( 0.1) 21 ( 0.1) 32 ( 0.1)
edad_al_ing_1 (median [IQR]) 34.06 [27.39, 42.91] 37.55 [29.58, 47.19] 31.91 [25.95, 39.67] 33.49 [27.12, 41.90] <0.001 nonnorm 0.323
sex (%) Men 54042 (76.3) 14232 (73.8) 12242 (77.5) 27568 (77.0) <0.001 0.057
sex (%) Women 16812 (23.7) 5044 (26.2) 3555 (22.5) 8213 (23.0)
edad_ini_cons (median [IQR]) 15.00 [14.00, 18.00] 16.00 [14.00, 18.00] 15.00 [13.00, 17.00] 15.00 [14.00, 18.00] <0.001 nonnorm 0.147
escolaridad_rec (%) 3-Completed primary school or less 20245 (28.6) 4996 (25.9) 4801 (30.4) 10448 (29.2) <0.001 0.134
escolaridad_rec (%) 2-Completed high school or less 39034 (55.1) 10272 (53.3) 8852 (56.0) 19910 (55.6)
escolaridad_rec (%) 1-More than high school 11259 (15.9) 3926 (20.4) 2073 (13.1) 5260 (14.7)
escolaridad_rec (%) [Missing] 316 ( 0.4) 82 ( 0.4) 71 ( 0.4) 163 ( 0.5)
sus_principal_mod (%) Alcohol 23860 (33.7) 8520 (44.2) 3967 (25.1) 11373 (31.8) <0.001 0.326
sus_principal_mod (%) Cocaine hydrochloride 13241 (18.7) 3279 (17.0) 2891 (18.3) 7071 (19.8)
sus_principal_mod (%) Cocaine paste 27788 (39.2) 5635 (29.2) 7810 (49.4) 14343 (40.1)
sus_principal_mod (%) Marijuana 4747 ( 6.7) 1326 ( 6.9) 937 ( 5.9) 2484 ( 6.9)
sus_principal_mod (%) Other 1217 ( 1.7) 516 ( 2.7) 192 ( 1.2) 509 ( 1.4)
sus_principal_mod (%) [Missing] 1 ( 0.0) 0 ( 0.0) 0 ( 0.0) 1 ( 0.0)
freq_cons_sus_prin (%) Less than 1 day a week 3495 ( 4.9) 1062 ( 5.5) 578 ( 3.7) 1855 ( 5.2) <0.001 0.145
freq_cons_sus_prin (%) 2 to 3 days a week 20060 (28.3) 5502 (28.5) 3906 (24.7) 10652 (29.8)
freq_cons_sus_prin (%) 4 to 6 days a week 11611 (16.4) 3035 (15.7) 2620 (16.6) 5956 (16.6)
freq_cons_sus_prin (%) 1 day a week or more 4780 ( 6.7) 1488 ( 7.7) 795 ( 5.0) 2497 ( 7.0)
freq_cons_sus_prin (%) Daily 30553 (43.1) 8101 (42.0) 7812 (49.5) 14640 (40.9)
freq_cons_sus_prin (%) [Missing] 355 ( 0.5) 88 ( 0.5) 86 ( 0.5) 181 ( 0.5)
condicion_ocupacional_corr (%) Employed 35364 (49.9) 9788 (50.8) 6878 (43.5) 18698 (52.3) <0.001 0.182
condicion_ocupacional_corr (%) Inactive 7168 (10.1) 2363 (12.3) 1356 ( 8.6) 3449 ( 9.6)
condicion_ocupacional_corr (%) Looking for a job for the first time 159 ( 0.2) 49 ( 0.3) 37 ( 0.2) 73 ( 0.2)
condicion_ocupacional_corr (%) No activity 3558 ( 5.0) 986 ( 5.1) 1060 ( 6.7) 1512 ( 4.2)
condicion_ocupacional_corr (%) Not seeking for work 712 ( 1.0) 214 ( 1.1) 203 ( 1.3) 295 ( 0.8)
condicion_ocupacional_corr (%) Unemployed 23892 (33.7) 5876 (30.5) 6263 (39.6) 11753 (32.8)
condicion_ocupacional_corr (%) [Missing] 1 ( 0.0) 0 ( 0.0) 0 ( 0.0) 1 ( 0.0)
policonsumo (%) 0 18443 (26.0) 6403 (33.2) 3387 (21.4) 8653 (24.2) <0.001 0.178
policonsumo (%) 1 52411 (74.0) 12873 (66.8) 12410 (78.6) 27128 (75.8)
num_hijos_mod_joel_bin (%) 0 16427 (23.2) 4448 (23.1) 3807 (24.1) 8172 (22.8) 0.009 0.024
num_hijos_mod_joel_bin (%) 1 53823 (76.0) 14668 (76.1) 11873 (75.2) 27282 (76.2)
num_hijos_mod_joel_bin (%) [Missing] 604 ( 0.9) 160 ( 0.8) 117 ( 0.7) 327 ( 0.9)
tenencia_de_la_vivienda_mod (%) Illegal Settlement 749 ( 1.1) 193 ( 1.0) 212 ( 1.3) 344 ( 1.0) <0.001 0.162
tenencia_de_la_vivienda_mod (%) Others 2003 ( 2.8) 518 ( 2.7) 428 ( 2.7) 1057 ( 3.0)
tenencia_de_la_vivienda_mod (%) Owner/Transferred dwellings/Pays Dividends 24812 (35.0) 7724 (40.1) 4955 (31.4) 12133 (33.9)
tenencia_de_la_vivienda_mod (%) Renting 12091 (17.1) 3283 (17.0) 2703 (17.1) 6105 (17.1)
tenencia_de_la_vivienda_mod (%) Stays temporarily with a relative 27141 (38.3) 6674 (34.6) 6209 (39.3) 14258 (39.8)
tenencia_de_la_vivienda_mod (%) [Missing] 4058 ( 5.7) 884 ( 4.6) 1290 ( 8.2) 1884 ( 5.3)
macrozona (%) Center 53678 (75.8) 13616 (70.6) 11817 (74.8) 28245 (78.9) <0.001 0.216
macrozona (%) North 10486 (14.8) 2933 (15.2) 2955 (18.7) 4598 (12.9)
macrozona (%) South 6675 ( 9.4) 2724 (14.1) 1014 ( 6.4) 2937 ( 8.2)
macrozona (%) [Missing] 15 ( 0.0) 3 ( 0.0) 11 ( 0.1) 1 ( 0.0)
n_off_vio (%) 0 58938 (83.2) 16582 (86.0) 12704 (80.4) 29652 (82.9) <0.001 0.100
n_off_vio (%) 1 11916 (16.8) 2694 (14.0) 3093 (19.6) 6129 (17.1)
n_off_acq (%) 0 58081 (82.0) 16984 (88.1) 11968 (75.8) 29129 (81.4) <0.001 0.217
n_off_acq (%) 1 12773 (18.0) 2292 (11.9) 3829 (24.2) 6652 (18.6)
n_off_sud (%) 0 58421 (82.5) 16373 (84.9) 12466 (78.9) 29582 (82.7) <0.001 0.105
n_off_sud (%) 1 12433 (17.5) 2903 (15.1) 3331 (21.1) 6199 (17.3)
n_off_oth (%) 0 58594 (82.7) 16678 (86.5) 12390 (78.4) 29526 (82.5) <0.001 0.143
n_off_oth (%) 1 12260 (17.3) 2598 (13.5) 3407 (21.6) 6255 (17.5)
dg_cie_10_rec (%) Without psychiatric comorbidity 27921 (39.4) 9251 (48.0) 2945 (18.6) 15725 (43.9) <0.001 1.073
dg_cie_10_rec (%) Diagnosis unknown (under study) 13273 (18.7) 353 ( 1.8) 9505 (60.2) 3415 ( 9.5)
dg_cie_10_rec (%) With psychiatric comorbidity 29660 (41.9) 9672 (50.2) 3347 (21.2) 16641 (46.5)
dg_trs_cons_sus_or (%) Drug dependence 51160 (72.2) 13401 (69.5) 12105 (76.6) 25654 (71.7) <0.001 0.107
dg_trs_cons_sus_or (%) Hazardous consumption 19693 (27.8) 5875 (30.5) 3692 (23.4) 10126 (28.3)
dg_trs_cons_sus_or (%) [Missing] 1 ( 0.0) 0 ( 0.0) 0 ( 0.0) 1 ( 0.0)
clas_r (%) Urbana 58268 (82.2) 15287 (79.3) 13598 (86.1) 29383 (82.1) <0.001 0.126
clas_r (%) Mixta 6834 ( 9.6) 2069 (10.7) 1288 ( 8.2) 3477 ( 9.7)
clas_r (%) Rural 5750 ( 8.1) 1920 (10.0) 909 ( 5.8) 2921 ( 8.2)
clas_r (%) [Missing] 2 ( 0.0) 0 ( 0.0) 2 ( 0.0) 0 ( 0.0)
porc_pobr (median [IQR]) 0.12 [0.07, 0.17] 0.11 [0.07, 0.17] 0.11 [0.07, 0.15] 0.12 [0.08, 0.17] <0.001 nonnorm 0.111
sus_ini_mod_mvv (%) Alcohol 38408 (54.2) 11793 (61.2) 7626 (48.3) 18989 (53.1) <0.001 0.233
sus_ini_mod_mvv (%) Cocaine hydrochloride 2605 ( 3.7) 566 ( 2.9) 649 ( 4.1) 1390 ( 3.9)
sus_ini_mod_mvv (%) Cocaine paste 3311 ( 4.7) 631 ( 3.3) 1041 ( 6.6) 1639 ( 4.6)
sus_ini_mod_mvv (%) Marijuana 19138 (27.0) 4123 (21.4) 5132 (32.5) 9883 (27.6)
sus_ini_mod_mvv (%) Other 1606 ( 2.3) 480 ( 2.5) 378 ( 2.4) 748 ( 2.1)
sus_ini_mod_mvv (%) [Missing] 5786 ( 8.2) 1683 ( 8.7) 971 ( 6.1) 3132 ( 8.8)
ano_nac_corr (median [IQR]) 1980.00 [1971.00, 1987.00] 1976.00 [1967.00, 1984.00] 1982.00 [1975.00, 1988.00] 1980.00 [1972.00, 1987.00] <0.001 nonnorm 0.341
con_quien_vive_joel (%) Alone 6686 ( 9.4) 2015 (10.5) 1636 (10.4) 3035 ( 8.5) <0.001 0.104
con_quien_vive_joel (%) Family of origin 29336 (41.4) 7453 (38.7) 6843 (43.3) 15040 (42.0)
con_quien_vive_joel (%) Others 6109 ( 8.6) 1611 ( 8.4) 1502 ( 9.5) 2996 ( 8.4)
con_quien_vive_joel (%) With couple/children 28722 (40.5) 8197 (42.5) 5816 (36.8) 14709 (41.1)
con_quien_vive_joel (%) [Missing] 1 ( 0.0) 0 ( 0.0) 0 ( 0.0) 1 ( 0.0)
fis_comorbidity_icd_10 (%) Without physical comorbidity 28048 (39.6) 7939 (41.2) 6005 (38.0) 14104 (39.4) <0.001 0.110
fis_comorbidity_icd_10 (%) Diagnosis unknown (under study) 38392 (54.2) 9804 (50.9) 9053 (57.3) 19535 (54.6)
fis_comorbidity_icd_10 (%) One or more 4414 ( 6.2) 1533 ( 8.0) 739 ( 4.7) 2142 ( 6.0)
offender_d (%) SI 22281 (31.4) 3825 (19.8) 6130 (38.8) 12326 (34.4) <0.001 0.283
offender_d (%) [Missing] 48573 (68.6) 15451 (80.2) 9667 (61.2) 23455 (65.6)
Show code
#kable(size=10, format="html",caption= "Summary descriptives, by Baseline Treatment Status") %>%     kableExtra::kable_classic()

Time for this code chunk to run: 0 minutes

Show code
# Treatment status (Early dropout/Late dropout/Treatment completion) 

tbone_desc_merge52<-
CreateTableOne(vars=c(setdiff(vars_cov, "motivodeegreso_mod_imp_rec"), "offender_d"), data=  subset(Base_fiscalia_v14, select=c(vars_cov, "offender_d"), subset=!is.na(motivodeegreso_mod_imp_rec)), factorVars = setdiff(vars_cov, c("motivodeegreso_mod_imp_rec","edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr")), smd=T, strata="motivodeegreso_mod_imp_rec", addOverall = T, includeNA=T, test=T)

tbone_desc_merge53<-
CreateTableOne(vars=c(setdiff(vars_cov, "motivodeegreso_mod_imp_rec"), "offender_d"), data=  subset(Base_fiscalia_v14_pris, select=c(vars_cov, "offender_d"), subset=!is.na(motivodeegreso_mod_imp_rec)), factorVars = setdiff(vars_cov, c("motivodeegreso_mod_imp_rec","edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr")), smd=T, strata="motivodeegreso_mod_imp_rec", addOverall = T, includeNA=T, test=T)

bind_rows(
(as.data.frame.TableOne(tbone_desc_merge52, smd=T, nonnormal= T)%>% 
  dplyr::mutate(char2=`characteristic`) %>% 
  tidyr::fill(char2) %>% 
  dplyr::select(char2,everything()) %>% 
  dplyr::mutate(level=ifelse(is.na(level),"[Missing]",level)) %>% 
  dplyr::mutate(char2=dplyr::case_when(characteristic=="NA"~NA_character_,T~as.character(characteristic))) %>% 
  format_cells(1, 1:length(names(.)), "bold")),
subset(as.data.frame.TableOne(tbone_desc_merge53, smd=T, nonnormal= T),subset= grepl("offender",characteristic))) %>% 
  dplyr::select(-1) %>% 
  rio::export("tableone_grant_22_23.xlsx")

fill: changed 56 values (68%) of ‘characteristic’ (56 fewer NA) fill: changed 56 values (68%) of ‘characteristic’ (56 fewer NA)

Show code
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
if(no_mostrar==0){
Base_fiscalia_v14_filt<- subset(Base_fiscalia_v14, select=c(vars_cov, "offender_d"), subset=!is.na(motivodeegreso_mod_imp_rec))
Base_fiscalia_v14_pris_filt<- subset(Base_fiscalia_v14_pris, select=c(vars_cov, "offender_d"), subset=!is.na(motivodeegreso_mod_imp_rec))

  
CreateTableOne(vars=c(setdiff(vars_cov, "motivodeegreso_mod_imp_rec"), "offender_d"), data=  Base_fiscalia_v14_filt, factorVars = setdiff(vars_cov, c("motivodeegreso_mod_imp_rec","edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr")), smd=T, strata="motivodeegreso_mod_imp_rec", addOverall = T, includeNA=T, test=T)  
  
Base_fiscalia_v14_filt%>% 
    dplyr::select(setdiff(vars_cov, c("edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr"))) %>% 
    tidyr::gather(variable,measure, -motivodeegreso_mod_imp_rec) %>% 
    dplyr::group_by(variable) %>%
    dplyr::do(cbind.data.frame(broom::tidy(chisq.test(.$motivodeegreso_mod_imp_rec, .$measure)),broom::tidy(sum(table(.$motivodeegreso_mod_imp_rec, .$measure))))
              ) %>% 
  #casos completos
#chisq.test(table(Base_fiscalia_v14_filt$motivodeegreso_mod_imp_rec, Base_fiscalia_v14_filt$sus_principal_mod, exclude=NULL))
    dplyr::mutate(p.value=ifelse(p.value<.001,"<0.001",sprintf("%1.3f",p.value))) %>% 
    dplyr::mutate(statistic=sprintf("%2.0f",statistic)) %>% 
    dplyr::mutate(report=paste0("X²(",parameter,", ",x,")=",statistic,"; p", ifelse(p.value=="<0.001",p.value, paste0("=",p.value)))) %>%
    dplyr::mutate(report=sub("0\\.","0,",sub("\\.",",",report)))%>%
    rio::export("tableone_grant_22_23_3_alt.xlsx")

 chisq.test(Base_fiscalia_v14_filt$motivodeegreso_mod_imp_rec, is.na(Base_fiscalia_v14_filt$offender_d)) 
 chisq.test(Base_fiscalia_v14_filt$motivodeegreso_mod_imp_rec, is.na(Base_fiscalia_v14_filt$offender_d)) 

Base_fiscalia_v14_filt %>% 
    dplyr::select(setdiff(vars_cov, c("edad_al_ing_1", "edad_ini_cons","ano_nac_corr", "porc_pobr"))) %>% 
    tidyr::gather(variable,measure, -motivodeegreso_mod_imp_rec) %>% 
    dplyr::group_by(variable) %>%
    dplyr::do(fisher.test(table(.$motivodeegreso_mod_imp_rec, .$measure, exclude=NULL), workspace = 2e10, simulate.p.value = T, B=1e5) %>% broom::tidy()) %>% 
    rio::export("tableone_grant_22_23_2_alt.xlsx")

fisher.test(table(Base_fiscalia_v14_filt$motivodeegreso_mod_imp_rec, 
            is.na(Base_fiscalia_v14_filt$offender_d),exclude=NULL), workspace = 2e10, simulate.p.value = T, B=1e5) 
fisher.test(table(Base_fiscalia_v14_pris_filt$motivodeegreso_mod_imp_rec, 
            is.na(Base_fiscalia_v14_pris_filt$offender_d),exclude=NULL), workspace = 2e10, simulate.p.value = T, B=1e5) 

# Kurksal Wallis
paste0("H(",kruskal.test(edad_al_ing_1 ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$parameter,")=",
       round(kruskal.test(edad_al_ing_1 ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$statistic,1), ", p=",
       round(kruskal.test(edad_al_ing_1 ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$p.value,3))

paste0("H(",kruskal.test(edad_ini_cons ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$parameter,")=",
       round(kruskal.test(edad_ini_cons ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$statistic,1), ", p=",
       round(kruskal.test(edad_ini_cons ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$p.value,3)) 

paste0("H(",kruskal.test(ano_nac_corr ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$parameter,")=",
       round(kruskal.test(ano_nac_corr ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$statistic,1), ", p=",
       round(kruskal.test(ano_nac_corr ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$p.value,3)) 

paste0("H(",kruskal.test(porc_pobr ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$parameter,")=",
       round(kruskal.test(porc_pobr ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$statistic,1), ", p=",
       round(kruskal.test(porc_pobr ~ motivodeegreso_mod_imp_rec, data=Base_fiscalia_v14_filt)$p.value,3)) 
}

Error in eval(expr, envir, enclos): objeto ‘no_mostrar’ no encontrado

Time for this code chunk to run: 0.1 minutes

Summary of Results

Show code
require(XML); require(RCurl); require(rlist)
prob_imp<-rvest::read_html("prob_condsent_m1_main.html") %>% rvest::html_table()
rmst_imp<-rvest::read_html("rmst_condsent_m1_main.html") %>% rvest::html_table()
prob_imp_diff<-rvest::read_html("prob_condsent_m1_main_diff.html") %>% rvest::html_table()
rmst_imp_diff<-rvest::read_html("rmst_condsent_m1_main_diff.html") %>% rvest::html_table()

prob_imp_tab<-
prob_imp %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_lci,",",Comp_uci,")"), Early_ci = paste0(Early," (", Early_lci,",",Early_uci,")"), Late_ci = paste0(Late," (", Late_lci,",",Late_uci,")")) %>% 
  dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>%
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

prob_imp_diff_tab<-
prob_imp_diff %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths")) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

rmst_imp_tab<-
rmst_imp %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate(t=readr::parse_number(Var.1)) %>% 
  dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.3f",t-readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_uci,",",Comp_lci,")"), Early_ci= paste0(Early," (", Early_uci,",",Early_lci,")"), Late_ci= paste0(Late," (", Late_uci,",",Late_lci,")")) %>%
  dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

rmst_imp_diff_tab<-
rmst_imp_diff %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.3f",-readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths")) 

rbindlist(list(list(c("Probs.",rep("",0))),cbind.data.frame(prob_imp_tab,prob_imp_diff_tab[,-1]),
               list(c("RMTL",rep("",0))), cbind.data.frame(rmst_imp_tab,rmst_imp_diff_tab[,-1])), fill=T) %>% 
    #dplyr::slice(-2,-3,-4,-5,-6,-7,-14,-15,-16,-17,-18,-19) %>%        
  dplyr::filter(nchar(Time)>=2) %>% 
  #knitr::kable("markdown", size=3, col.names= c("Time", "Complete Tr.","Late Disch.", "Early Disch.", "Comp. vs Late", "Comp. vs Early", "Early vs Late"), caption= "Offending with Condemnatory Sentence")
kbl(format = 'markdown',
        escape = FALSE,
        col.names= c("Time", "Complete Tr.","Late Disch.", "Early Disch.", "Comp. vs Late", "Comp. vs Early", "Early vs Late"), 
    caption= "Offending with Condemnatory Sentence")
Table 9: Offending with Condemnatory Sentence
Time Complete Tr. Late Disch. Early Disch. Comp. vs Late Comp. vs Early Early vs Late
Probs.
3_yrs 79.4 (78.8,80.0) 70.9 (70.4,71.3) 69.0 (68.3,69.7) -8.6 (-9.3,-7.9) -10.4 (-11.4,-9.5) 1.9 (1.0,2.7)
5_yrs 73.4 (72.7,74.2) 63.9 (63.4,64.5) 62.2 (61.4,63.1) -9.5 (-10.3,-8.7) -11.2 (-12.3,-10.1) 1.7 (0.8,2.7)
RMTL
3_yrs 0.378 (0.365,0.390) 0.558 (0.548,0.567) 0.606 (0.590,0.621) 0.180 (0.195,0.164) 0.228 (0.248,0.207) -0.048 (-0.029,-0.067)
5_yrs 0.828 (0.803,0.852) 1.193 (1.175,1.212) 1.278 (1.248,1.308) 0.366 (0.395,0.336) 0.450 (0.491,0.410) -0.085 (-0.049,-0.121)

Time for this code chunk to run: 0.1 minutes

Show code
prob_imp_pris<-rvest::read_html("prob_prison_m1_main.html") %>% rvest::html_table()
rmst_imp_pris<-rvest::read_html("rmst_prison_m1_main.html") %>% rvest::html_table()
prob_imp_diff_pris<-rvest::read_html("prob_prison_m1_main_diff.html") %>% rvest::html_table()
rmst_imp_diff_pris<-rvest::read_html("rmst_prison_m1_main_diff.html") %>% rvest::html_table()

prob_imp_pris_tab<-
prob_imp_pris %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_lci,",",Comp_uci,")"), Early_ci = paste0(Early," (", Early_lci,",",Early_uci,")"), Late_ci = paste0(Late," (", Late_lci,",",Late_uci,")")) %>% 
  dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

prob_imp_diff_pris_tab<-
prob_imp_diff_pris %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>%
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths")) 

rmst_imp_pris_tab<-
rmst_imp_pris %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate(t=readr::parse_number(Var.1)) %>% 
  dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.3f",t-readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_uci,",",Comp_lci,")"), Early_ci= paste0(Early," (", Early_uci,",",Early_lci,")"), Late_ci= paste0(Late," (", Late_uci,",",Late_lci,")")) %>%
  dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

rmst_imp_diff_pris_tab<-
rmst_imp_diff_pris %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.3f",-readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

rbindlist(
  list(list(c("Probs.",rep("",0))),cbind.data.frame(prob_imp_pris_tab,prob_imp_diff_pris_tab[,-1]),   list(c("RMTL",rep("",0))),cbind.data.frame(rmst_imp_pris_tab,rmst_imp_diff_pris_tab[,-1])), 
          fill=T) %>% 
    dplyr::filter(nchar(Time)>=2)  %>% #%>% rio::export("_tab_mariel_results.xlsx")
kbl(format = 'markdown',
        escape = FALSE,
        col.names= c("Time", "Complete Tr.","Late Disch.", "Early Disch.", "Comp. vs Late", "Comp. vs Early", "Early vs Late"), 
    caption= "Offending with imprisonment")
Table 10: Offending with imprisonment
Time Complete Tr. Late Disch. Early Disch. Comp. vs Late Comp. vs Early Early vs Late
Probs.
3_yrs 96.4 (96.1,96.7) 94.3 (94.0,94.5) 93.2 (92.8,93.6) -2.2 (-2.5,-1.8) -3.2 (-3.7,-2.8) 1.1 (0.6,1.5)
5_yrs 94.9 (94.5,95.3) 92.3 (92.0,92.6) 91.0 (90.5,91.4) -2.6 (-3.1,-2.2) -4.0 (-4.6,-3.3) 1.3 (0.8,1.9)
RMTL
3_yrs 0.069 (0.064,0.075) 0.111 (0.107,0.116) 0.133 (0.125,0.140) 0.042 (0.049,0.035) 0.063 (0.073,0.054) -0.022 (-0.013,-0.031)
5_yrs 0.122 (0.110,0.135) 0.214 (0.204,0.223) 0.260 (0.245,0.276) 0.091 (0.107,0.076) 0.138 (0.158,0.117) -0.046 (-0.027,-0.065)

Time for this code chunk to run: 0 minutes

Show code
require(XML); require(RCurl); require(rlist)
prob_imp_cc<-rvest::read_html("prob_condsent_m0_main.html") %>% rvest::html_table()
rmst_imp_cc<-rvest::read_html("rmst_condsent_m0_main.html") %>% rvest::html_table()
prob_imp_diff_cc<-rvest::read_html("prob_condsent_m0_main_diff.html") %>% rvest::html_table()
rmst_imp_diff_cc<-rvest::read_html("rmst_condsent_m0_main_diff.html") %>% rvest::html_table()

prob_imp_tab_cc<-
prob_imp_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_lci,",",Comp_uci,")"), Early_ci = paste0(Early," (", Early_lci,",",Early_uci,")"), Late_ci = paste0(Late," (", Late_lci,",",Late_uci,")")) %>% 
  dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

prob_imp_diff_tab_cc<-
prob_imp_diff_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths")) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

rmst_imp_tab_cc<-
rmst_imp_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate(t=readr::parse_number(Var.1)) %>% 
  dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.3f",t-readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_uci,",",Comp_lci,")"), Early_ci= paste0(Early," (", Early_uci,",",Early_lci,")"), Late_ci= paste0(Late," (", Late_uci,",",Late_lci,")")) %>%
  dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

rmst_imp_diff_tab_cc<-
rmst_imp_diff_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.3f",-readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths")) 

rbindlist(
  list(list(c("Probs.",rep("",0))),cbind.data.frame(prob_imp_tab_cc,prob_imp_diff_tab_cc[,-1]),
  list(c("RMTL",rep("",0))), cbind.data.frame(rmst_imp_tab_cc,rmst_imp_diff_tab_cc[,-1])), fill=T)%>% 
    #dplyr::slice(-2,-3,-4,-5,-6,-7,-14,-15,-16,-17,-18,-19) %>%        
  dplyr::filter(nchar(Time)>=2) %>% 
  #knitr::kable("markdown", size=3, col.names= c("Time", "Complete Tr.","Late Disch.", "Early Disch.", "Comp. vs Late", "Comp. vs Early", "Early vs Late"), caption= "Offending with Condemnatory Sentence")
kbl(format = 'markdown',
        escape = FALSE,
        col.names= c("Time", "Complete Tr.","Late Disch.", "Early Disch.", "Comp. vs Late", "Comp. vs Early", "Early vs Late"), 
    caption= "Offending with Condemnatory Sentence (complete cases)")
Table 11: Offending with Condemnatory Sentence (complete cases)
Time Complete Tr. Late Disch. Early Disch. Comp. vs Late Comp. vs Early Early vs Late
Probs.
3_yrs 80.1 (79.5,80.8) 71.9 (71.5,72.4) 70.3 (69.5,71.0) -8.2 (-9.0,-7.4) -9.9 (-10.9,-8.8) 1.7 (0.7,2.6)
5_yrs 74.6 (73.8,75.4) 65.5 (65.0,66.1) 64.0 (63.2,64.9) -9.1 (-10.0,-8.2) -10.5 (-11.8,-9.3) 1.5 (0.4,2.5)
RMTL
3_yrs 0.371 (0.358,0.384) 0.542 (0.531,0.552) 0.585 (0.568,0.602) 0.171 (0.187,0.154) 0.214 (0.236,0.192) -0.044 (-0.023,-0.064)
5_yrs 0.872 (0.846,0.897) 1.212 (1.193,1.231) 1.286 (1.254,1.318) 0.340 (0.372,0.309) 0.414 (0.457,0.371) -0.074 (-0.036,-0.112)

Time for this code chunk to run: 0 minutes

Show code
prob_imp_pris_cc<-rvest::read_html("prob_prison_m0_main.html") %>% rvest::html_table()
rmst_imp_pris_cc<-rvest::read_html("rmst_prison_m0_main.html") %>% rvest::html_table()
prob_imp_diff_pris_cc<-rvest::read_html("prob_prison_m0_main_diff.html") %>% rvest::html_table()
rmst_imp_diff_pris_cc<-rvest::read_html("rmst_prison_m0_main_diff.html") %>% rvest::html_table()

prob_imp_pris_tab_cc<-
prob_imp_pris_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_lci,",",Comp_uci,")"), Early_ci = paste0(Early," (", Early_lci,",",Early_uci,")"), Late_ci = paste0(Late," (", Late_lci,",",Late_uci,")")) %>% 
  dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

prob_imp_diff_pris_tab_cc<-
prob_imp_diff_pris_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.1f",readr::parse_number(.))) %>%
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths")) 

rmst_imp_pris_tab_cc<-
rmst_imp_pris_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  plyr::mutate(t=readr::parse_number(Var.1)) %>% 
dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.3f",t-readr::parse_number(.))) %>% 
     dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_ci= paste0(Comp," (", Comp_uci,",",Comp_lci,")"), Early_ci= paste0(Early," (", Early_uci,",",Early_lci,")"), Late_ci= paste0(Late," (", Late_uci,",",Late_lci,")")) %>%
     dplyr::select(Time, Comp_ci, Late_ci, Early_ci) %>% 
     dplyr::filter(!Time %in% c("3_mths","6_mths"))

rmst_imp_diff_pris_tab_cc<-
rmst_imp_diff_pris_cc %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>% 
  dplyr::mutate_at(2:ncol(.), ~sprintf("%1.3f",-readr::parse_number(.))) %>% 
  dplyr::rename("Time" = 1) %>% slice(5:6) %>% mutate(Comp_Early_ci= paste0(Comp_Early," (", Comp_Early_lci,",",Comp_Early_uci,")"), Comp_Late_ci = paste0(Comp_Late," (", Comp_Late_lci,",",Comp_Late_uci,")"), Early_Late_ci = paste0(Early_Late," (", Early_Late_lci,",",Early_Late_uci,")")) %>% 
  dplyr::select(Time, Comp_Late_ci, Comp_Early_ci, Early_Late_ci) %>% 
  dplyr::filter(!Time %in% c("3_mths","6_mths"))

rbindlist(
  list(list(c("Probs.",rep("",0))),cbind.data.frame(prob_imp_pris_tab_cc,prob_imp_diff_pris_tab_cc[,-1]), 
  list(c("RMTL",rep("",0))),cbind.data.frame(rmst_imp_pris_tab_cc,rmst_imp_diff_pris_tab_cc[,-1])),
  fill=T) %>% 
    dplyr::filter(nchar(Time)>=2)  %>% #rio::export("_tab_mariel_results.xlsx")
kbl(format = 'markdown',
        escape = FALSE,
        col.names= c("Time", "Complete Tr.","Late Disch.", "Early Disch.", "Comp. vs Late", "Comp. vs Early", "Early vs Late"), 
    caption= "Offending with imprisonment (complete cases)")
Table 12: Offending with imprisonment (complete cases)
Time Complete Tr. Late Disch. Early Disch. Comp. vs Late Comp. vs Early Early vs Late
Probs.
3_yrs 96.6 (96.3,96.9) 94.5 (94.3,94.7) 93.6 (93.2,94.0) -2.1 (-2.5,-1.7) -3.1 (-3.6,-2.5) 0.9 (0.4,1.4)
5_yrs 95.3 (94.9,95.7) 92.7 (92.4,93.1) 91.6 (91.2,92.1) -2.6 (-3.1,-2.1) -3.7 (-4.3,-3.0) 1.1 (0.5,1.7)
RMTL
3_yrs 0.200 (0.194,0.205) 0.238 (0.233,0.242) 0.255 (0.247,0.262) 0.041 (0.049,0.033) 0.059 (0.070,0.049) -0.018 (-0.009,-0.028)
5_yrs 0.209 (0.197,0.222) 0.296 (0.286,0.306) 0.335 (0.318,0.351) 0.087 (0.103,0.071) 0.125 (0.147,0.104) -0.038 (-0.018,-0.058)

Time for this code chunk to run: 0 minutes

Show code
require(cowplot)
require(gridExtra)
require(coin)
require(magick)
require(pdftools)
fig1 <- image_read_pdf('h_m_ns_rp6_stdif_s2_m1.pdf')%>%
    image_border("white", "80x20")%>%
    image_annotate("A)", 
                   location = "+18+0", 
                   size = 35, 
                   gravity = "northwest", 
                   boxcolor = "transparent", 
                   font = "Arial", 
                   strokecolor = "black")

fig2 <- image_read_pdf('h_m_ns_rp6_stdif_rmst_m1.pdf')%>%
    image_border("white", "80x20")%>%
    image_annotate("B)", 
                   location = "+18+0", 
                   size = 35, 
                   gravity = "northwest", 
                   boxcolor = "transparent", 
                   font = "Arial", 
                   strokecolor = "black")
fig2rmtl <- image_read_pdf('_figs/h_m_ns_rp6_stdif_rmtl_m1.pdf')%>%
    image_border("white", "80x20")%>%
    image_annotate("B)", 
                   location = "+18+0", 
                   size = 35, 
                   gravity = "northwest", 
                   boxcolor = "transparent", 
                   font = "Arial", 
                   strokecolor = "black")
fig3 <- image_read_pdf('h_m_ns_rp6_stdif_s2_pris_m1.pdf')%>%
    image_border("white", "80x20")%>%
    image_annotate("C)", 
                   location = "+18+0", 
                   size = 35, 
                   gravity = "northwest", 
                   boxcolor = "transparent", 
                   font = "Arial", 
                   strokecolor = "black")
fig4 <- image_read_pdf('h_m_ns_rp6_stdif_rmst_pris_m1.pdf')%>%
    image_border("white", "80x20")%>%
    image_annotate("D)", 
                   location = "+18+0", 
                   size = 35, 
                   gravity = "northwest", 
                   boxcolor = "transparent", 
                   font = "Arial", 
                   strokecolor = "black")
fig4rmtl <- image_read_pdf('_figs/h_m_ns_rp6_stdif_rmtl_pris_m1.pdf')%>%
    image_border("white", "80x20")%>%
    image_annotate("D)", 
                   location = "+18+0", 
                   size = 35, 
                   gravity = "northwest", 
                   boxcolor = "transparent", 
                   font = "Arial", 
                   strokecolor = "black")
#https://fondecytacc.github.io/nDP/analisis_mariel_feb_2023_stata_m1.html
#https://fondecytacc.github.io/nDP/analisis_mariel_feb_2023_stata_pris_m1.html
#ggsave(plot= p4,filename= "./_figs/SER2023_test.jpg", dpi=740, height = 16*0.7, width =20*0.7)
image_montage(c(fig1, fig2, fig3, fig4), tile = '2x2', geometry = 'x500', gravity = "Center")#
Show code
image_write(image_montage(c(fig1, fig2, fig3, fig4), tile = '2x2', geometry = 'x500', gravity = "Center"), path = "montage.png", format = "png", density = "300x300")
image_write(image_montage(c(fig1, fig2, fig3, fig4), tile = '2x2', geometry = 'x500', gravity = "Center"), path = "montage.pdf", format = "pdf", density = "300x300")

image_montage(c(fig1, fig2rmtl, fig3, fig4rmtl), tile = '2x2', geometry = 'x500', gravity = "Center")#
Show code
image_write(image_montage(c(fig1, fig2rmtl, fig3, fig4rmtl), tile = '2x2', geometry = 'x500', gravity = "Center"), path = "montage_rmtl.png", format = "png", density = "300x300")
image_write(image_montage(c(fig1, fig2rmtl, fig3, fig4rmtl), tile = '2x2', geometry = 'x500', gravity = "Center"), path = "montage_rmtl.pdf", format = "pdf", density = "300x300")

Time for this code chunk to run: 0.1 minutes

Show code
# In this model, the term dxb: is typically used to indicate a time-dependent covariate, 
#which is a covariate whose effect changes over time. For example, if you are studying the survival of 
#patients after surgery, a time-dependent covariate might be the patient's age at the time of surgery.
# 
# On the other hand, xb: is typically used to denote time-independent covariates, which are covariates 
#whose effect does not change over time. An example of a time-independent covariate might
#be the patient's gender.

tab1<-rvest::read_html("mat_tab1.html") %>% rvest::html_table() %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>%   dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.2f",readr::parse_number(.))) %>% dplyr::filter(dplyr::case_when(b!="NA"~T,T~F)) %>% 
  dplyr::mutate(pvalue=dplyr::case_when(readr::parse_number(pvalue)<0.001~"<0.001",T~sprintf("%1.3f",readr::parse_number(pvalue))))
tab2<-rvest::read_html("mat_tab2.html")  %>% rvest::html_table() %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>%   dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.2f",readr::parse_number(.))) %>% dplyr::filter(dplyr::case_when(b!="NA"~T,T~F)) %>% 
  dplyr::mutate(pvalue=dplyr::case_when(readr::parse_number(pvalue)<0.001~"<0.001",T~sprintf("%1.3f",readr::parse_number(pvalue))))
tab3<-rvest::read_html("mat_tab3.html")  %>% rvest::html_table() %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>%   dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.2f",readr::parse_number(.))) %>% dplyr::filter(dplyr::case_when(b!="NA"~T,T~F)) %>% 
  dplyr::mutate(pvalue=dplyr::case_when(readr::parse_number(pvalue)<0.001~"<0.001",T~sprintf("%1.3f",readr::parse_number(pvalue))))
tab4<-rvest::read_html("mat_tab4.html")  %>% rvest::html_table() %>% as.data.table() %>% janitor::row_to_names(3) %>% data.frame() %>%   dplyr::mutate_at(2:(ncol(.)-1), ~sprintf("%1.2f",readr::parse_number(.))) %>% dplyr::filter(dplyr::case_when(b!="NA"~T,T~F)) %>% 
  dplyr::mutate(pvalue=dplyr::case_when(readr::parse_number(pvalue)<0.001~"<0.001",T~sprintf("%1.3f",readr::parse_number(pvalue))))

tab1 %>% 
  dplyr::left_join(`tab2`,by="Var.1") %>% 
  dplyr::left_join(`tab3`,by="Var.1") %>% 
  dplyr::left_join(`tab4`,by="Var.1") %>% 
  dplyr::mutate(`Var.1`=str_replace_all(`Var.1`, "^_d_rcs", "time-dependent: knot ")) %>% 
  dplyr::mutate(`Var.1`=str_replace_all(`Var.1`, "^_rcs", "time-independent: knot ")) %>% 
  dplyr::mutate(`Var.1`=str_replace_all(`Var.1`, "knot _mot_egr_early1", "Early discharge")) %>% 
  dplyr::mutate(`Var.1`=str_replace_all(`Var.1`, "knot _mot_egr_late1", "Late discharge")) %>% 
  dplyr::mutate(`Var.1`=str_replace_all(`Var.1`, "^mot_egr_early$", "Early discharge")) %>% 
  dplyr::mutate(`Var.1`=str_replace_all(`Var.1`, "^mot_egr_late$", "Late discharge")) %>%  
  dplyr::mutate(across(everything(), ~replace_na(., ""))) %>%
  #rio::export("hrs_intramural2223.xlsx")
  knitr::kable("markdown", caption="sdasd", col.names=c("Term",rep(c("Estimate", "95%CI lo", "95%CI up", "P-value"),4)))
Table 13: sdasd
Term Estimate 95%CI lo 95%CI up P-value Estimate 95%CI lo 95%CI up P-value Estimate 95%CI lo 95%CI up P-value Estimate 95%CI lo 95%CI up P-value
Early discharge 1.74 1.66 1.83 <0.001 1.99 1.79 2.22 <0.001 1.73 1.63 1.83 <0.001 2.01 1.78 2.28 <0.001
Late discharge 1.58 1.52 1.65 <0.001 1.65 1.51 1.81 <0.001 1.58 1.51 1.65 <0.001 1.69 1.52 1.88 <0.001
time-independent: knot 1 2.66 2.59 2.73 <0.001 2.18 2.07 2.30 <0.001 2.63 2.56 2.71 <0.001 2.20 2.07 2.34 <0.001
time-independent: knot 2 1.11 1.10 1.12 <0.001 1.07 1.06 1.09 <0.001 1.10 1.09 1.12 <0.001 1.07 1.05 1.08 <0.001
time-independent: knot 3 1.05 1.04 1.06 <0.001 1.03 1.02 1.05 <0.001 1.04 1.03 1.05 <0.001 1.03 1.02 1.05 <0.001
time-independent: knot 4 1.02 1.02 1.03 <0.001 1.02 1.01 1.03 <0.001 1.02 1.02 1.03 <0.001 1.02 1.01 1.03 <0.001
time-independent: knot 5 1.01 1.01 1.02 <0.001 1.01 1.01 1.02 <0.001 1.01 1.01 1.02 <0.001 1.01 1.00 1.02 0.001
time-independent: knot 6 1.01 1.01 1.01 <0.001 1.01 1.01 1.01 <0.001 1.01 1.00 1.01 <0.001 1.01 1.00 1.01 <0.001
time-independent: knot 7 1.01 1.01 1.01 <0.001
time-independent: knot 8 1.00 1.00 1.01 <0.001
time-independent: Early discharge 0.91 0.88 0.94 <0.001 0.90 0.85 0.95 <0.001 0.91 0.87 0.94 <0.001 0.89 0.83 0.96 0.001
time-independent: Late discharge 0.94 0.92 0.97 <0.001 0.92 0.87 0.97 0.004 0.94 0.91 0.97 <0.001 0.91 0.85 0.98 0.008
time-dependent: knot 1 0.98 0.95 1.00 <0.001 0.78 0.73 0.83 <0.001 0.97 0.94 1.00 <0.001 0.79 0.73 0.85 <0.001
time-dependent: knot 2 0.11 0.10 0.12 <0.001 0.07 0.06 0.08 <0.001 0.10 0.09 0.11 <0.001 0.06 0.05 0.08 <0.001
time-dependent: knot 3 0.05 0.04 0.05 <0.001 0.03 0.02 0.04 <0.001 0.04 0.03 0.05 <0.001 0.03 0.02 0.05 <0.001
time-dependent: knot 4 0.02 0.02 0.03 <0.001 0.02 0.01 0.03 <0.001 0.02 0.02 0.02 <0.001 0.02 0.01 0.03 <0.001
time-dependent: knot 5 0.01 0.01 0.02 <0.001 0.01 0.01 0.02 <0.001 0.01 0.01 0.02 <0.001 0.01 0.00 0.02 0.001
time-dependent: knot 6 0.01 0.01 0.01 <0.001 0.01 0.01 0.01 <0.001 0.01 0.00 0.01 <0.001 0.01 0.00 0.01 <0.001
time-dependent: knot 7 0.01 0.01 0.01 <0.001
time-dependent: knot 8 0.00 0.00 0.01 <0.001
time-dependent: Early discharge -0.10 -0.13 -0.07 <0.001 -0.11 -0.17 -0.05 <0.001 -0.10 -0.13 -0.06 <0.001 -0.11 -0.18 -0.04 0.001
time-dependent: Late discharge -0.06 -0.09 -0.03 <0.001 -0.08 -0.14 -0.03 0.004 -0.06 -0.09 -0.03 <0.001 -0.09 -0.16 -0.02 0.008

Time for this code chunk to run: 0 minutes


Sensitivity analyses

Show code
invisible("
coxph(formula = as.formula(paste('Surv(yrs_to_condemn,  event_r) ~ motivodeegreso_mod_imp_rec + rcs(edad_al_ing_1,4) +', 
     paste(setdiff(vars_cov, 'edad_al_ing_1'), collapse = '+'))), 
     data = Base_fiscalia_v14, ties = 'efron')
")

vars <- c("tr_mod2", "sex_dum2", "edad_ini_cons", "esc1", "esc2", 
          "sus_prin2", "sus_prin3", "sus_prin4", "sus_prin5", 
          "fr_cons_sus_prin2", "fr_cons_sus_prin3", "fr_cons_sus_prin4", "fr_cons_sus_prin5", 
          "cond_ocu2", "cond_ocu3", "cond_ocu4", "cond_ocu5", "cond_ocu6", 
          "policonsumo", "num_hij2", "tenviv1", "tenviv2", "tenviv4", "tenviv5", 
          "mzone2", "mzone3", "n_off_vio", "n_off_acq", "n_off_sud", "n_off_oth", 
          "psy_com2", "psy_com3", "dep2", "rural2", "rural3", 
          "porc_pobr", "susini2", "susini3", "susini4", "susini5", 
          "ano_nac_corr", "cohab2", "cohab3", "cohab4", 
          "fis_com2", "fis_com3", "rc_x1", "rc_x2", "rc_x3")

statadf_main          <- rio::import("mariel_feb_23.dta")
statadf_main_cc       <-  statadf_main[complete.cases(statadf_main[, c(vars,"motivodeegreso_mod_imp_rec")]),] %>% dplyr::mutate(motivodeegreso_mod_imp_rec=factor(motivodeegreso_mod_imp_rec)) #%>% dplyr::mutate(across(vars,~factor(.)))
# statadf_main_l        <- rio::import("mariel_feb_23_late.dta")
# statadf_main_e        <- rio::import("mariel_feb_23_early.dta")
# statadf_main_e_l      <- rio::import("mariel_feb_23_early_late.dta")

statadf_main_pris     <- rio::import("mariel_feb_23_2.dta")
statadf_main_pris_cc  <- statadf_main_pris[complete.cases(statadf_main_pris[, c(vars,"motivodeegreso_mod_imp_rec")]),] %>% dplyr::mutate(motivodeegreso_mod_imp_rec=factor(motivodeegreso_mod_imp_rec)) 
# statadf_main_pris_l   <- rio::import("mariel_feb_23_2_late.dta")
# statadf_main_pris_e   <- rio::import("mariel_feb_23_2_early.dta")
# statadf_main_pris_e_l <- rio::import("mariel_feb_23_2_early_late.dta")
statadf_miss          <- rio::import("mariel_feb_23_m1.dta")%>% dplyr::mutate(motivodeegreso_mod_imp_rec=factor(motivodeegreso_mod_imp_rec)) #%>%
statadf_miss_pris     <- rio::import("mariel_feb_23_2_m1.dta")%>% dplyr::mutate(motivodeegreso_mod_imp_rec=factor(motivodeegreso_mod_imp_rec)) #%>%

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
# 
# motivodeegreso_mod_imp_rec ~ tr_mod2+ sex_dum2+ edad_ini_cons+ esc1+ esc2+ sus_prin2+ sus_prin3+ sus_prin4+ sus_prin5+ fr_cons_sus_prin2+ fr_cons_sus_prin3+ fr_cons_sus_prin4+ fr_cons_sus_prin5+ cond_ocu2+ cond_ocu3+ cond_ocu4+ cond_ocu5+ cond_ocu6+ policonsumo+ num_hij2+ tenviv1+ tenviv2+ tenviv4+ tenviv5+ mzone2+ mzone3+ n_off_vio+ n_off_acq+ n_off_sud+ n_off_oth+ psy_com2+ psy_com3+ dep2+ rural2+ rural3+ porc_pobr+ susini2+ susini3+ susini4+ susini5+ ano_nac_corr+ cohab2+ cohab3+ cohab4+ fis_com2+ fis_com3+ rc_x1+ rc_x2+ rc_x3, data = statadf_main_cc

set.seed(2125)
W1 <- WeightIt::weightit(as.formula(paste("motivodeegreso_mod_imp_rec~", paste(vars, collapse = "+"))), data=statadf_miss, method = "bart", estimand = "ATE", use.mlogit = FALSE)
statadf_miss$weight_mult_b <- WeightIt::get_w_from_ps(W1$weights, statadf_miss$motivodeegreso_mod_imp_rec, estimand = "ATE", stabilize=T)
set.seed(2125)
W2 <- WeightIt::weightit(as.formula(paste("motivodeegreso_mod_imp_rec~", paste(vars, collapse = "+"))), data=statadf_miss_pris, method = "bart", estimand = "ATE", use.mlogit = FALSE)
statadf_miss_pris$weight_mult_b <- WeightIt::get_w_from_ps(W2$weights, statadf_miss_pris$motivodeegreso_mod_imp_rec, estimand = "ATE", stabilize=T)

# Surv(yrs_to_condemn, event_r) ~ motivodeegreso_mod_imp_rec + 
#     rcs(edad_al_ing_1, 4) + motivodeegreso_mod_imp_rec + tr_modality + 
#     sex + edad_ini_cons + escolaridad_rec + sus_principal_mod + 
#     freq_cons_sus_prin + condicion_ocupacional_corr + policonsumo + 
#     num_hijos_mod_joel_bin + tenencia_de_la_vivienda_mod + macrozona + 
#     n_off_vio + n_off_acq + n_off_sud + n_off_oth + dg_cie_10_rec + 
#     dg_trs_cons_sus_or + clas_r + porc_pobr + sus_ini_mod_mvv + 
#     ano_nac_corr + con_quien_vive_joel + fis_comorbidity_icd_10

Time for this code chunk to run: 0.2 minutes

Show code
bal_tab_2023_res_1<-
cobalt::bal.tab(motivodeegreso_mod_imp_rec ~ tr_mod2+ sex_dum2+ edad_ini_cons+ esc1+ esc2+ sus_prin2+ sus_prin3+ sus_prin4+ sus_prin5+ fr_cons_sus_prin2+ fr_cons_sus_prin3+ fr_cons_sus_prin4+ fr_cons_sus_prin5+ cond_ocu2+ cond_ocu3+ cond_ocu4+ cond_ocu5+ cond_ocu6+ policonsumo+ num_hij2+ tenviv1+ tenviv2+ tenviv4+ tenviv5+ mzone2+ mzone3+ n_off_vio+ n_off_acq+ n_off_sud+ n_off_oth+ psy_com2+ psy_com3+ dep2+ rural2+ rural3+ porc_pobr+ susini2+ susini3+ susini4+ susini5+ ano_nac_corr+ cohab2+ cohab3+ cohab4+ fis_com2+ fis_com3+ rc_x1+ rc_x2+ rc_x3, data = statadf_miss,
                weights = list("Beyasian Additive Regression Trees" = "weight_mult_b"),
                estimand = "ATE",
                which.treat = .all,
                un = T, 
                thresholds = c(cor = .2),
                 binary = "std", continuous = "std",
                stats = c("mean.diffs", "variance.ratios"))

bal_tab_2023_res_2<-
cobalt::bal.tab(motivodeegreso_mod_imp_rec ~ tr_mod2+ sex_dum2+ edad_ini_cons+ esc1+ esc2+ sus_prin2+ sus_prin3+ sus_prin4+ sus_prin5+ fr_cons_sus_prin2+ fr_cons_sus_prin3+ fr_cons_sus_prin4+ fr_cons_sus_prin5+ cond_ocu2+ cond_ocu3+ cond_ocu4+ cond_ocu5+ cond_ocu6+ policonsumo+ num_hij2+ tenviv1+ tenviv2+ tenviv4+ tenviv5+ mzone2+ mzone3+ n_off_vio+ n_off_acq+ n_off_sud+ n_off_oth+ psy_com2+ psy_com3+ dep2+ rural2+ rural3+ porc_pobr+ susini2+ susini3+ susini4+ susini5+ ano_nac_corr+ cohab2+ cohab3+ cohab4+ fis_com2+ fis_com3+ rc_x1+ rc_x2+ rc_x3, data = statadf_miss_pris,
                weights = list("Beyasian Additive Regression Trees" = "weight_mult_b"),
                estimand = "ATE",
                which.treat = .all,
                un = T, 
                thresholds = c(cor = .2),
                 binary = "std", continuous = "std",
                stats = c("mean.diffs", "variance.ratios"))

cbind.data.frame(type=c(rep("Cond. sentence",3), rep("Imprisonment",3)),
rbind(data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab_2023_res_1$Pair.Balance$`2 vs. 1`$Balance), keep.rownames=T),
      data.table::data.table(cbind(comp="Late Dropout vs. Tr. completion",bal_tab_2023_res_1$Pair.Balance$`3 vs. 1`$Balance), keep.rownames=T),
      data.table::data.table(cbind(comp="Late vs. Early Dropout",bal_tab_2023_res_1$Pair.Balance$`3 vs. 2`$Balance), keep.rownames=T),
      data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab_2023_res_2$Pair.Balance$`2 vs. 1`$Balance), keep.rownames=T),
      data.table::data.table(cbind(comp="Late Dropout vs. Tr. completion",bal_tab_2023_res_2$Pair.Balance$`3 vs. 1`$Balance), keep.rownames=T),
      data.table::data.table(cbind(comp="Late vs. Early Dropout",bal_tab_2023_res_2$Pair.Balance$`3 vs. 2`$Balance), keep.rownames=T))) %>% 
  #data.table::data.table(keep.rownames = T) %>% 
  dplyr::select(type, comp, rn, everything()) %>%
  dplyr::arrange(type, comp, -Diff.Adj) %>% 
  dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Balance Measures (multinomial response BART weights)")
Table 14: Balance Measures (multinomial response BART weights)
type comp rn Type Diff.Un V.Ratio.Un Diff.Adj V.Ratio.Adj
Cond. sentence Early Dropout vs. Tr. completion psy_com2 Binary 1.72 2.61
Cond. sentence Early Dropout vs. Tr. completion ano_nac_corr Contin. 0.52 0.72 0.80 0.63
Cond. sentence Early Dropout vs. Tr. completion sus_prin3 Binary 0.42 0.62
Cond. sentence Early Dropout vs. Tr. completion n_off_acq_2 Binary 0.32 0.48
Cond. sentence Early Dropout vs. Tr. completion policonsumo Binary 0.27 0.43
Cond. sentence Early Dropout vs. Tr. completion susini4 Binary 0.25 0.38
Cond. sentence Early Dropout vs. Tr. completion n_off_oth_2 Binary 0.21 0.33
Cond. sentence Early Dropout vs. Tr. completion cond_ocu6 Binary 0.19 0.28
Cond. sentence Early Dropout vs. Tr. completion fis_com2 Binary 0.13 0.27
Cond. sentence Early Dropout vs. Tr. completion n_off_sud_2 Binary 0.16 0.25
Cond. sentence Early Dropout vs. Tr. completion susini3 Binary 0.16 0.23
Cond. sentence Early Dropout vs. Tr. completion n_off_vio_2 Binary 0.15 0.23
Cond. sentence Early Dropout vs. Tr. completion mzone2 Binary 0.10 0.22
Cond. sentence Early Dropout vs. Tr. completion dep2 Binary 0.16 0.22
Cond. sentence Early Dropout vs. Tr. completion fr_cons_sus_prin5 Binary 0.15 0.20
Cond. sentence Early Dropout vs. Tr. completion tenviv5 Binary 0.11 0.17
Cond. sentence Early Dropout vs. Tr. completion cohab2 Binary 0.09 0.15
Cond. sentence Early Dropout vs. Tr. completion esc1 Binary 0.10 0.14
Cond. sentence Early Dropout vs. Tr. completion cond_ocu4 Binary 0.07 0.11
Cond. sentence Early Dropout vs. Tr. completion esc2 Binary 0.06 0.10
Cond. sentence Early Dropout vs. Tr. completion cohab3 Binary 0.04 0.06
Cond. sentence Early Dropout vs. Tr. completion tenviv4 Binary 0.04 0.06
Cond. sentence Early Dropout vs. Tr. completion sus_prin2 Binary 0.03 0.06
Cond. sentence Early Dropout vs. Tr. completion tenviv1 Binary 0.04 0.06
Cond. sentence Early Dropout vs. Tr. completion fr_cons_sus_prin4 Binary 0.02 0.03
Cond. sentence Early Dropout vs. Tr. completion susini2 Binary 0.02 0.02
Cond. sentence Early Dropout vs. Tr. completion tenviv2 Binary 0.00 0.01
Cond. sentence Early Dropout vs. Tr. completion cond_ocu5 Binary 0.02 0.01
Cond. sentence Early Dropout vs. Tr. completion cond_ocu3 Binary 0.00 -0.01
Cond. sentence Early Dropout vs. Tr. completion susini5 Binary -0.01 -0.03
Cond. sentence Early Dropout vs. Tr. completion tr_mod2 Binary 0.02 -0.03
Cond. sentence Early Dropout vs. Tr. completion sus_prin4 Binary -0.04 -0.04
Cond. sentence Early Dropout vs. Tr. completion num_hij2 Binary -0.03 -0.04
Cond. sentence Early Dropout vs. Tr. completion fr_cons_sus_prin3 Binary -0.08 -0.09
Cond. sentence Early Dropout vs. Tr. completion porc_pobr Contin. -0.08 0.85 -0.12 0.80
Cond. sentence Early Dropout vs. Tr. completion rural2 Binary -0.09 -0.12
Cond. sentence Early Dropout vs. Tr. completion sex_dum2 Binary -0.09 -0.13
Cond. sentence Early Dropout vs. Tr. completion fr_cons_sus_prin2 Binary -0.11 -0.17
Cond. sentence Early Dropout vs. Tr. completion cohab4 Binary -0.12 -0.18
Cond. sentence Early Dropout vs. Tr. completion sus_prin5 Binary -0.11 -0.19
Cond. sentence Early Dropout vs. Tr. completion cond_ocu2 Binary -0.12 -0.20
Cond. sentence Early Dropout vs. Tr. completion rural3 Binary -0.16 -0.20
Cond. sentence Early Dropout vs. Tr. completion fis_com3 Binary -0.14 -0.22
Cond. sentence Early Dropout vs. Tr. completion edad_ini_cons Contin. -0.23 0.59 -0.36 0.46
Cond. sentence Early Dropout vs. Tr. completion mzone3 Binary -0.26 -0.43
Cond. sentence Early Dropout vs. Tr. completion rc_x3 Contin. -0.43 0.42 -0.70 0.28
Cond. sentence Early Dropout vs. Tr. completion rc_x2 Contin. -0.46 0.46 -0.74 0.32
Cond. sentence Early Dropout vs. Tr. completion rc_x1 Contin. -0.49 0.70 -0.76 0.61
Cond. sentence Early Dropout vs. Tr. completion psy_com3 Binary -0.62 -0.95
Cond. sentence Late Dropout vs. Tr. completion ano_nac_corr Contin. 0.31 0.78 0.57 0.67
Cond. sentence Late Dropout vs. Tr. completion sus_prin3 Binary 0.23 0.39
Cond. sentence Late Dropout vs. Tr. completion policonsumo Binary 0.21 0.39
Cond. sentence Late Dropout vs. Tr. completion n_off_acq_2 Binary 0.18 0.29
Cond. sentence Late Dropout vs. Tr. completion susini4 Binary 0.15 0.25
Cond. sentence Late Dropout vs. Tr. completion tenviv5 Binary 0.11 0.20
Cond. sentence Late Dropout vs. Tr. completion n_off_oth_2 Binary 0.11 0.17
Cond. sentence Late Dropout vs. Tr. completion porc_pobr Contin. 0.09 0.98 0.16 0.96
Cond. sentence Late Dropout vs. Tr. completion psy_com2 Binary 0.23 0.15
Cond. sentence Late Dropout vs. Tr. completion n_off_vio_2 Binary 0.08 0.14
Cond. sentence Late Dropout vs. Tr. completion sus_prin2 Binary 0.07 0.14
Cond. sentence Late Dropout vs. Tr. completion cohab2 Binary 0.07 0.13
Cond. sentence Late Dropout vs. Tr. completion fis_com2 Binary 0.08 0.12
Cond. sentence Late Dropout vs. Tr. completion esc1 Binary 0.07 0.11
Cond. sentence Late Dropout vs. Tr. completion susini3 Binary 0.06 0.10
Cond. sentence Late Dropout vs. Tr. completion n_off_sud_2 Binary 0.06 0.10
Cond. sentence Late Dropout vs. Tr. completion esc2 Binary 0.05 0.10
Cond. sentence Late Dropout vs. Tr. completion cond_ocu6 Binary 0.05 0.08
Cond. sentence Late Dropout vs. Tr. completion dep2 Binary 0.05 0.08
Cond. sentence Late Dropout vs. Tr. completion susini2 Binary 0.04 0.07
Cond. sentence Late Dropout vs. Tr. completion fr_cons_sus_prin3 Binary 0.03 0.06
Cond. sentence Late Dropout vs. Tr. completion fr_cons_sus_prin4 Binary 0.02 0.04
Cond. sentence Late Dropout vs. Tr. completion tenviv2 Binary 0.01 0.02
Cond. sentence Late Dropout vs. Tr. completion sus_prin4 Binary 0.00 0.02
Cond. sentence Late Dropout vs. Tr. completion tenviv4 Binary 0.01 0.01
Cond. sentence Late Dropout vs. Tr. completion num_hij2 Binary 0.01 0.00
Cond. sentence Late Dropout vs. Tr. completion cohab3 Binary 0.00 0.00
Cond. sentence Late Dropout vs. Tr. completion tenviv1 Binary -0.01 -0.01
Cond. sentence Late Dropout vs. Tr. completion cond_ocu3 Binary -0.01 -0.02
Cond. sentence Late Dropout vs. Tr. completion cond_ocu5 Binary -0.03 -0.05
Cond. sentence Late Dropout vs. Tr. completion psy_com3 Binary -0.08 -0.05
Cond. sentence Late Dropout vs. Tr. completion fr_cons_sus_prin5 Binary -0.02 -0.05
Cond. sentence Late Dropout vs. Tr. completion fr_cons_sus_prin2 Binary -0.03 -0.05
Cond. sentence Late Dropout vs. Tr. completion cohab4 Binary -0.03 -0.05
Cond. sentence Late Dropout vs. Tr. completion rural2 Binary -0.03 -0.06
Cond. sentence Late Dropout vs. Tr. completion susini5 Binary -0.03 -0.06
Cond. sentence Late Dropout vs. Tr. completion cond_ocu4 Binary -0.04 -0.07
Cond. sentence Late Dropout vs. Tr. completion mzone2 Binary -0.07 -0.09
Cond. sentence Late Dropout vs. Tr. completion rural3 Binary -0.07 -0.12
Cond. sentence Late Dropout vs. Tr. completion sex_dum2 Binary -0.08 -0.13
Cond. sentence Late Dropout vs. Tr. completion fis_com3 Binary -0.08 -0.15
Cond. sentence Late Dropout vs. Tr. completion cond_ocu2 Binary -0.09 -0.16
Cond. sentence Late Dropout vs. Tr. completion sus_prin5 Binary -0.10 -0.18
Cond. sentence Late Dropout vs. Tr. completion edad_ini_cons Contin. -0.16 0.69 -0.28 0.52
Cond. sentence Late Dropout vs. Tr. completion mzone3 Binary -0.20 -0.39
Cond. sentence Late Dropout vs. Tr. completion tr_mod2 Binary -0.24 -0.43
Cond. sentence Late Dropout vs. Tr. completion rc_x3 Contin. -0.33 0.55 -0.62 0.35
Cond. sentence Late Dropout vs. Tr. completion rc_x1 Contin. -0.34 0.78 -0.62 0.65
Cond. sentence Late Dropout vs. Tr. completion rc_x2 Contin. -0.34 0.59 -0.64 0.40
Cond. sentence Late vs. Early Dropout psy_com3 Binary 0.54 0.89
Cond. sentence Late vs. Early Dropout porc_pobr Contin. 0.17 1.15 0.27 1.20
Cond. sentence Late vs. Early Dropout fr_cons_sus_prin3 Binary 0.11 0.15
Cond. sentence Late vs. Early Dropout rc_x1 Contin. 0.15 1.11 0.14 1.07
Cond. sentence Late vs. Early Dropout cohab4 Binary 0.09 0.12
Cond. sentence Late vs. Early Dropout fr_cons_sus_prin2 Binary 0.08 0.12
Cond. sentence Late vs. Early Dropout rc_x2 Contin. 0.12 1.27 0.10 1.23
Cond. sentence Late vs. Early Dropout rc_x3 Contin. 0.11 1.31 0.08 1.26
Cond. sentence Late vs. Early Dropout edad_ini_cons Contin. 0.08 1.17 0.08 1.13
Cond. sentence Late vs. Early Dropout sus_prin2 Binary 0.04 0.08
Cond. sentence Late vs. Early Dropout rural3 Binary 0.09 0.08
Cond. sentence Late vs. Early Dropout fis_com3 Binary 0.05 0.07
Cond. sentence Late vs. Early Dropout rural2 Binary 0.05 0.07
Cond. sentence Late vs. Early Dropout sus_prin4 Binary 0.04 0.06
Cond. sentence Late vs. Early Dropout num_hij2 Binary 0.03 0.04
Cond. sentence Late vs. Early Dropout susini2 Binary 0.02 0.04
Cond. sentence Late vs. Early Dropout cond_ocu2 Binary 0.03 0.04
Cond. sentence Late vs. Early Dropout mzone3 Binary 0.06 0.04
Cond. sentence Late vs. Early Dropout tenviv5 Binary 0.00 0.03
Cond. sentence Late vs. Early Dropout tenviv2 Binary 0.01 0.01
Cond. sentence Late vs. Early Dropout fr_cons_sus_prin4 Binary 0.00 0.01
Cond. sentence Late vs. Early Dropout sus_prin5 Binary 0.02 0.01
Cond. sentence Late vs. Early Dropout esc2 Binary -0.01 0.00
Cond. sentence Late vs. Early Dropout sex_dum2 Binary 0.01 0.00
Cond. sentence Late vs. Early Dropout cond_ocu3 Binary -0.01 -0.01
Cond. sentence Late vs. Early Dropout cohab2 Binary -0.03 -0.02
Cond. sentence Late vs. Early Dropout esc1 Binary -0.03 -0.03
Cond. sentence Late vs. Early Dropout susini5 Binary -0.02 -0.03
Cond. sentence Late vs. Early Dropout policonsumo Binary -0.06 -0.04
Cond. sentence Late vs. Early Dropout cond_ocu5 Binary -0.04 -0.06
Cond. sentence Late vs. Early Dropout tenviv4 Binary -0.03 -0.06
Cond. sentence Late vs. Early Dropout cohab3 Binary -0.04 -0.06
Cond. sentence Late vs. Early Dropout tenviv1 Binary -0.04 -0.07
Cond. sentence Late vs. Early Dropout n_off_vio_2 Binary -0.07 -0.09
Cond. sentence Late vs. Early Dropout susini4 Binary -0.11 -0.12
Cond. sentence Late vs. Early Dropout susini3 Binary -0.09 -0.13
Cond. sentence Late vs. Early Dropout dep2 Binary -0.11 -0.14
Cond. sentence Late vs. Early Dropout n_off_sud_2 Binary -0.10 -0.15
Cond. sentence Late vs. Early Dropout fis_com2 Binary -0.05 -0.15
Cond. sentence Late vs. Early Dropout n_off_oth_2 Binary -0.11 -0.16
Cond. sentence Late vs. Early Dropout cond_ocu4 Binary -0.11 -0.18
Cond. sentence Late vs. Early Dropout n_off_acq_2 Binary -0.15 -0.19
Cond. sentence Late vs. Early Dropout cond_ocu6 Binary -0.14 -0.19
Cond. sentence Late vs. Early Dropout ano_nac_corr Contin. -0.20 1.09 -0.22 1.06
Cond. sentence Late vs. Early Dropout sus_prin3 Binary -0.19 -0.23
Cond. sentence Late vs. Early Dropout fr_cons_sus_prin5 Binary -0.17 -0.25
Cond. sentence Late vs. Early Dropout mzone2 Binary -0.16 -0.31
Cond. sentence Late vs. Early Dropout tr_mod2 Binary -0.26 -0.40
Cond. sentence Late vs. Early Dropout psy_com2 Binary -1.50 -2.47
Imprisonment Early Dropout vs. Tr. completion psy_com2 Binary 1.72 2.61
Imprisonment Early Dropout vs. Tr. completion ano_nac_corr Contin. 0.52 0.72 0.80 0.63
Imprisonment Early Dropout vs. Tr. completion sus_prin3 Binary 0.42 0.62
Imprisonment Early Dropout vs. Tr. completion n_off_acq_2 Binary 0.32 0.48
Imprisonment Early Dropout vs. Tr. completion policonsumo Binary 0.27 0.43
Imprisonment Early Dropout vs. Tr. completion susini4 Binary 0.25 0.37
Imprisonment Early Dropout vs. Tr. completion n_off_oth_2 Binary 0.21 0.33
Imprisonment Early Dropout vs. Tr. completion cond_ocu6 Binary 0.19 0.27
Imprisonment Early Dropout vs. Tr. completion fis_com2 Binary 0.13 0.27
Imprisonment Early Dropout vs. Tr. completion n_off_sud_2 Binary 0.16 0.25
Imprisonment Early Dropout vs. Tr. completion susini3 Binary 0.16 0.23
Imprisonment Early Dropout vs. Tr. completion n_off_vio_2 Binary 0.15 0.23
Imprisonment Early Dropout vs. Tr. completion mzone2 Binary 0.10 0.22
Imprisonment Early Dropout vs. Tr. completion dep2 Binary 0.16 0.22
Imprisonment Early Dropout vs. Tr. completion fr_cons_sus_prin5 Binary 0.15 0.20
Imprisonment Early Dropout vs. Tr. completion tenviv5 Binary 0.11 0.17
Imprisonment Early Dropout vs. Tr. completion cohab2 Binary 0.09 0.15
Imprisonment Early Dropout vs. Tr. completion esc1 Binary 0.10 0.14
Imprisonment Early Dropout vs. Tr. completion cond_ocu4 Binary 0.07 0.11
Imprisonment Early Dropout vs. Tr. completion esc2 Binary 0.06 0.10
Imprisonment Early Dropout vs. Tr. completion cohab3 Binary 0.04 0.06
Imprisonment Early Dropout vs. Tr. completion tenviv4 Binary 0.04 0.06
Imprisonment Early Dropout vs. Tr. completion sus_prin2 Binary 0.03 0.06
Imprisonment Early Dropout vs. Tr. completion tenviv1 Binary 0.04 0.06
Imprisonment Early Dropout vs. Tr. completion fr_cons_sus_prin4 Binary 0.02 0.03
Imprisonment Early Dropout vs. Tr. completion susini2 Binary 0.02 0.02
Imprisonment Early Dropout vs. Tr. completion tenviv2 Binary 0.00 0.01
Imprisonment Early Dropout vs. Tr. completion cond_ocu5 Binary 0.02 0.01
Imprisonment Early Dropout vs. Tr. completion cond_ocu3 Binary 0.00 -0.01
Imprisonment Early Dropout vs. Tr. completion susini5 Binary -0.01 -0.03
Imprisonment Early Dropout vs. Tr. completion tr_mod2 Binary 0.02 -0.03
Imprisonment Early Dropout vs. Tr. completion sus_prin4 Binary -0.04 -0.04
Imprisonment Early Dropout vs. Tr. completion num_hij2 Binary -0.03 -0.04
Imprisonment Early Dropout vs. Tr. completion fr_cons_sus_prin3 Binary -0.08 -0.09
Imprisonment Early Dropout vs. Tr. completion porc_pobr Contin. -0.08 0.85 -0.11 0.80
Imprisonment Early Dropout vs. Tr. completion sex_dum2 Binary -0.09 -0.13
Imprisonment Early Dropout vs. Tr. completion rural2 Binary -0.09 -0.13
Imprisonment Early Dropout vs. Tr. completion fr_cons_sus_prin2 Binary -0.11 -0.17
Imprisonment Early Dropout vs. Tr. completion cohab4 Binary -0.12 -0.18
Imprisonment Early Dropout vs. Tr. completion sus_prin5 Binary -0.11 -0.19
Imprisonment Early Dropout vs. Tr. completion cond_ocu2 Binary -0.12 -0.19
Imprisonment Early Dropout vs. Tr. completion rural3 Binary -0.16 -0.20
Imprisonment Early Dropout vs. Tr. completion fis_com3 Binary -0.14 -0.22
Imprisonment Early Dropout vs. Tr. completion edad_ini_cons Contin. -0.23 0.59 -0.36 0.46
Imprisonment Early Dropout vs. Tr. completion mzone3 Binary -0.26 -0.43
Imprisonment Early Dropout vs. Tr. completion rc_x3 Contin. -0.43 0.42 -0.71 0.28
Imprisonment Early Dropout vs. Tr. completion rc_x2 Contin. -0.46 0.46 -0.74 0.33
Imprisonment Early Dropout vs. Tr. completion rc_x1 Contin. -0.49 0.70 -0.76 0.61
Imprisonment Early Dropout vs. Tr. completion psy_com3 Binary -0.62 -0.94
Imprisonment Late Dropout vs. Tr. completion ano_nac_corr Contin. 0.31 0.78 0.57 0.67
Imprisonment Late Dropout vs. Tr. completion sus_prin3 Binary 0.23 0.39
Imprisonment Late Dropout vs. Tr. completion policonsumo Binary 0.21 0.38
Imprisonment Late Dropout vs. Tr. completion n_off_acq_2 Binary 0.18 0.29
Imprisonment Late Dropout vs. Tr. completion susini4 Binary 0.15 0.25
Imprisonment Late Dropout vs. Tr. completion tenviv5 Binary 0.11 0.20
Imprisonment Late Dropout vs. Tr. completion n_off_oth_2 Binary 0.11 0.17
Imprisonment Late Dropout vs. Tr. completion porc_pobr Contin. 0.09 0.98 0.16 0.97
Imprisonment Late Dropout vs. Tr. completion psy_com2 Binary 0.23 0.15
Imprisonment Late Dropout vs. Tr. completion n_off_vio_2 Binary 0.08 0.14
Imprisonment Late Dropout vs. Tr. completion sus_prin2 Binary 0.07 0.14
Imprisonment Late Dropout vs. Tr. completion cohab2 Binary 0.07 0.13
Imprisonment Late Dropout vs. Tr. completion fis_com2 Binary 0.08 0.12
Imprisonment Late Dropout vs. Tr. completion esc1 Binary 0.07 0.11
Imprisonment Late Dropout vs. Tr. completion susini3 Binary 0.06 0.10
Imprisonment Late Dropout vs. Tr. completion n_off_sud_2 Binary 0.06 0.10
Imprisonment Late Dropout vs. Tr. completion esc2 Binary 0.05 0.10
Imprisonment Late Dropout vs. Tr. completion cond_ocu6 Binary 0.05 0.08
Imprisonment Late Dropout vs. Tr. completion dep2 Binary 0.05 0.08
Imprisonment Late Dropout vs. Tr. completion susini2 Binary 0.04 0.07
Imprisonment Late Dropout vs. Tr. completion fr_cons_sus_prin3 Binary 0.03 0.06
Imprisonment Late Dropout vs. Tr. completion fr_cons_sus_prin4 Binary 0.02 0.04
Imprisonment Late Dropout vs. Tr. completion tenviv2 Binary 0.01 0.02
Imprisonment Late Dropout vs. Tr. completion sus_prin4 Binary 0.00 0.02
Imprisonment Late Dropout vs. Tr. completion tenviv4 Binary 0.01 0.01
Imprisonment Late Dropout vs. Tr. completion num_hij2 Binary 0.01 0.00
Imprisonment Late Dropout vs. Tr. completion cohab3 Binary 0.00 0.00
Imprisonment Late Dropout vs. Tr. completion tenviv1 Binary -0.01 -0.01
Imprisonment Late Dropout vs. Tr. completion cond_ocu3 Binary -0.01 -0.02
Imprisonment Late Dropout vs. Tr. completion cond_ocu5 Binary -0.03 -0.05
Imprisonment Late Dropout vs. Tr. completion psy_com3 Binary -0.08 -0.05
Imprisonment Late Dropout vs. Tr. completion fr_cons_sus_prin5 Binary -0.02 -0.05
Imprisonment Late Dropout vs. Tr. completion fr_cons_sus_prin2 Binary -0.03 -0.05
Imprisonment Late Dropout vs. Tr. completion cohab4 Binary -0.03 -0.06
Imprisonment Late Dropout vs. Tr. completion rural2 Binary -0.03 -0.06
Imprisonment Late Dropout vs. Tr. completion susini5 Binary -0.03 -0.06
Imprisonment Late Dropout vs. Tr. completion cond_ocu4 Binary -0.04 -0.07
Imprisonment Late Dropout vs. Tr. completion mzone2 Binary -0.07 -0.09
Imprisonment Late Dropout vs. Tr. completion rural3 Binary -0.07 -0.12
Imprisonment Late Dropout vs. Tr. completion sex_dum2 Binary -0.08 -0.13
Imprisonment Late Dropout vs. Tr. completion fis_com3 Binary -0.08 -0.15
Imprisonment Late Dropout vs. Tr. completion cond_ocu2 Binary -0.09 -0.16
Imprisonment Late Dropout vs. Tr. completion sus_prin5 Binary -0.10 -0.18
Imprisonment Late Dropout vs. Tr. completion edad_ini_cons Contin. -0.16 0.69 -0.28 0.51
Imprisonment Late Dropout vs. Tr. completion mzone3 Binary -0.20 -0.39
Imprisonment Late Dropout vs. Tr. completion tr_mod2 Binary -0.24 -0.43
Imprisonment Late Dropout vs. Tr. completion rc_x3 Contin. -0.33 0.55 -0.62 0.35
Imprisonment Late Dropout vs. Tr. completion rc_x1 Contin. -0.34 0.78 -0.62 0.66
Imprisonment Late Dropout vs. Tr. completion rc_x2 Contin. -0.34 0.59 -0.64 0.40
Imprisonment Late vs. Early Dropout psy_com3 Binary 0.54 0.90
Imprisonment Late vs. Early Dropout porc_pobr Contin. 0.17 1.15 0.27 1.21
Imprisonment Late vs. Early Dropout fr_cons_sus_prin3 Binary 0.11 0.15
Imprisonment Late vs. Early Dropout rc_x1 Contin. 0.15 1.11 0.14 1.07
Imprisonment Late vs. Early Dropout cohab4 Binary 0.09 0.12
Imprisonment Late vs. Early Dropout fr_cons_sus_prin2 Binary 0.08 0.12
Imprisonment Late vs. Early Dropout rc_x2 Contin. 0.12 1.27 0.10 1.23
Imprisonment Late vs. Early Dropout rc_x3 Contin. 0.11 1.31 0.08 1.27
Imprisonment Late vs. Early Dropout edad_ini_cons Contin. 0.08 1.17 0.08 1.13
Imprisonment Late vs. Early Dropout rural3 Binary 0.09 0.08
Imprisonment Late vs. Early Dropout sus_prin2 Binary 0.04 0.08
Imprisonment Late vs. Early Dropout fis_com3 Binary 0.05 0.07
Imprisonment Late vs. Early Dropout rural2 Binary 0.05 0.07
Imprisonment Late vs. Early Dropout sus_prin4 Binary 0.04 0.05
Imprisonment Late vs. Early Dropout num_hij2 Binary 0.03 0.04
Imprisonment Late vs. Early Dropout susini2 Binary 0.02 0.04
Imprisonment Late vs. Early Dropout cond_ocu2 Binary 0.03 0.04
Imprisonment Late vs. Early Dropout mzone3 Binary 0.06 0.04
Imprisonment Late vs. Early Dropout tenviv5 Binary 0.00 0.03
Imprisonment Late vs. Early Dropout tenviv2 Binary 0.01 0.01
Imprisonment Late vs. Early Dropout sus_prin5 Binary 0.02 0.01
Imprisonment Late vs. Early Dropout fr_cons_sus_prin4 Binary 0.00 0.01
Imprisonment Late vs. Early Dropout esc2 Binary -0.01 0.00
Imprisonment Late vs. Early Dropout sex_dum2 Binary 0.01 0.00
Imprisonment Late vs. Early Dropout cond_ocu3 Binary -0.01 -0.01
Imprisonment Late vs. Early Dropout cohab2 Binary -0.03 -0.02
Imprisonment Late vs. Early Dropout esc1 Binary -0.03 -0.03
Imprisonment Late vs. Early Dropout susini5 Binary -0.02 -0.03
Imprisonment Late vs. Early Dropout policonsumo Binary -0.06 -0.04
Imprisonment Late vs. Early Dropout tenviv4 Binary -0.03 -0.05
Imprisonment Late vs. Early Dropout cond_ocu5 Binary -0.04 -0.06
Imprisonment Late vs. Early Dropout cohab3 Binary -0.04 -0.06
Imprisonment Late vs. Early Dropout tenviv1 Binary -0.04 -0.07
Imprisonment Late vs. Early Dropout n_off_vio_2 Binary -0.07 -0.09
Imprisonment Late vs. Early Dropout susini4 Binary -0.11 -0.12
Imprisonment Late vs. Early Dropout susini3 Binary -0.09 -0.13
Imprisonment Late vs. Early Dropout dep2 Binary -0.11 -0.14
Imprisonment Late vs. Early Dropout n_off_sud_2 Binary -0.10 -0.15
Imprisonment Late vs. Early Dropout fis_com2 Binary -0.05 -0.15
Imprisonment Late vs. Early Dropout n_off_oth_2 Binary -0.11 -0.15
Imprisonment Late vs. Early Dropout cond_ocu4 Binary -0.11 -0.18
Imprisonment Late vs. Early Dropout n_off_acq_2 Binary -0.15 -0.19
Imprisonment Late vs. Early Dropout cond_ocu6 Binary -0.14 -0.19
Imprisonment Late vs. Early Dropout ano_nac_corr Contin. -0.20 1.09 -0.23 1.06
Imprisonment Late vs. Early Dropout sus_prin3 Binary -0.19 -0.23
Imprisonment Late vs. Early Dropout fr_cons_sus_prin5 Binary -0.17 -0.25
Imprisonment Late vs. Early Dropout mzone2 Binary -0.16 -0.31
Imprisonment Late vs. Early Dropout tr_mod2 Binary -0.26 -0.40
Imprisonment Late vs. Early Dropout psy_com2 Binary -1.50 -2.46
Show code
 #  knitr::kable("html", caption="Balance Measures") %>% 
   #   kableExtra::kable_classic_2()

invisible("0= completar; 1= no-completar;policonsumo looks not balanced, the number of previous acquisitive offenses, and South")

Time for this code chunk to run: 0.3 minutes

Show code
cbind.data.frame(type=c(rep("Cond. sentence",3), rep("Imprisonment",3)),
rbind(cbind(comp="Early Dropout vs. Tr. completion",
            bal_tab_2023_res_1$Pair.Balance$`2 vs. 1`$Balance),
      cbind(comp="Late Dropout vs. Tr. completion",
            bal_tab_2023_res_1$Pair.Balance$`3 vs. 1`$Balance),
      cbind(comp="Late vs. Early Dropout",
            bal_tab_2023_res_1$Pair.Balance$`3 vs. 2`$Balance),
      cbind(comp="Early Dropout vs. Tr. completion",
            bal_tab_2023_res_2$Pair.Balance$`2 vs. 1`$Balance),
      cbind(comp="Late Dropout vs. Tr. completion",
            bal_tab_2023_res_2$Pair.Balance$`3 vs. 1`$Balance),
      cbind(comp="Late vs. Early Dropout",
            bal_tab_2023_res_2$Pair.Balance$`3 vs. 2`$Balance))) %>% 
    data.table::data.table(keep.rownames = T) %>% 
    dplyr::select(type, comp, rn, everything()) %>% 
    dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
    dplyr::group_by(type, comp) %>% 
    dplyr::summarise_if(is.numeric, ~list(min= min(.,na.rm=T), max= max(.,na.rm=T)))%>%
  ungroup() %>% 
  knitr::kable("markdown", caption="Balance Measures (multinomial response BART weights)", col.names=c("Type","Comparison","Diff. Unstandardized","V.Ratio Unstandardized","Diff.Adjusted","V.Ratio.Adjusted"))
Table 15: Balance Measures (multinomial response BART weights)
Type Comparison Diff. Unstandardized V.Ratio Unstandardized Diff.Adjusted V.Ratio.Adjusted
Cond. sentence Early Dropout vs. Tr. completion -0.62 0.42 -0.95 0.28
Cond. sentence Early Dropout vs. Tr. completion 1.72 0.85 2.61 0.8
Cond. sentence Late Dropout vs. Tr. completion -0.34 0.55 -0.64 0.35
Cond. sentence Late Dropout vs. Tr. completion 0.31 0.98 0.57 0.96
Cond. sentence Late vs. Early Dropout -1.5 1.09 -2.47 1.06
Cond. sentence Late vs. Early Dropout 0.54 1.31 0.89 1.26
Imprisonment Early Dropout vs. Tr. completion -0.62 0.42 -0.94 0.28
Imprisonment Early Dropout vs. Tr. completion 1.72 0.85 2.61 0.8
Imprisonment Late Dropout vs. Tr. completion -0.34 0.55 -0.64 0.35
Imprisonment Late Dropout vs. Tr. completion 0.31 0.98 0.57 0.97
Imprisonment Late vs. Early Dropout -1.5 1.09 -2.46 1.06
Imprisonment Late vs. Early Dropout 0.54 1.31 0.9 1.27
Show code
 #  knitr::kable("html", caption="Balance Measures (multinomial response weights)") %>% 
   #   kableExtra::kable_classic_2()

Time for this code chunk to run: 0 minutes

Show code
#The “effective sample size” (ESS) is a measure of the sample size a non-weighted sample would have to have to achieve the same level of precision as the weighted sample (Ridgeway 2006).
cbind.data.frame(type=c(rep("Cond.sentence",6), rep("Imprisonmente",6)),
dplyr::bind_rows(
data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab_2023_res_1$Pair.Balance$`2 vs. 1`$Observations),keep.rownames = T), data.table::data.table(cbind(comp="Late Dropout vs. Tr. completion",bal_tab_2023_res_1$Pair.Balance$`3 vs. 1`$Observations),keep.rownames = T), data.table::data.table(cbind(comp="Late vs. Early Dropout",bal_tab_2023_res_1$Pair.Balance$`3 vs. 2`$Observations),keep.rownames = T),
data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab_2023_res_2$Pair.Balance$`2 vs. 1`$Observations),keep.rownames = T), data.table::data.table(cbind(comp="Early Dropout vs. Tr. completion",bal_tab_2023_res_2$Pair.Balance$`3 vs. 1`$Observations),keep.rownames = T), data.table::data.table(cbind(comp="Late vs. Early Dropout",bal_tab_2023_res_2$Pair.Balance$`3 vs. 2`$Observations),keep.rownames = T)))%>% 
  #dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Effective sample sizes", col.names = c("Type", "Adj", "Comparison", "Tr. completion", "Early Dropout", "Late Dropout"))
Table 16: Effective sample sizes
Type Adj Comparison Tr. completion Early Dropout Late Dropout
Cond.sentence Unadjusted Early Dropout vs. Tr. completion 19277.00 15797.00
Cond.sentence Adjusted Early Dropout vs. Tr. completion 16894.60 11475.61
Cond.sentence Unadjusted Late Dropout vs. Tr. completion 19277.00 35789.00
Cond.sentence Adjusted Late Dropout vs. Tr. completion 16894.60 33799.41
Cond.sentence Unadjusted Late vs. Early Dropout 15797.00 35789.00
Cond.sentence Adjusted Late vs. Early Dropout 11475.61 33799.41
Imprisonmente Unadjusted Early Dropout vs. Tr. completion 19277.00 15797.00
Imprisonmente Adjusted Early Dropout vs. Tr. completion 16892.12 11487.69
Imprisonmente Unadjusted Early Dropout vs. Tr. completion 19277.00 35789.00
Imprisonmente Adjusted Early Dropout vs. Tr. completion 16892.12 33803.37
Imprisonmente Unadjusted Late vs. Early Dropout 15797.00 35789.00
Imprisonmente Adjusted Late vs. Early Dropout 11487.69 33803.37

Time for this code chunk to run: 0 minutes

Show code
sub1_statadf_miss<- subset(statadf_miss,subset= motivodeegreso_mod_imp_rec!=3) |> dplyr::mutate(motivodeegreso_mod_imp_rec= ifelse(motivodeegreso_mod_imp_rec==2,1,0))|> slice(1:5e5)|>data.table::data.table()
require(RMSTSens)
results.approx11 <- RMSTSens(time="diff", status="event", exposure="motivodeegreso_mod_imp_rec", level.exposed="1", ps="weight_mult_b", data=sub1_statadf_miss, methods="Optim", use.multicore=TRUE, n.core=parallel::detectCores()-2, lambda=c(1,1.25,1.5), tau=1, ini.par=1, verbose=FALSE)
invisible("If the censoring rate is greater than 0.7, the direct optimization method can be used as an alternative because it is implemented as fast as the approximate optimization method.")
results.approx11

results.approx13 <- RMSTSens(time="diff", status="event", exposure="motivodeegreso_mod_imp_rec", level.exposed="1", ps="weight_mult_b", data=sub1_statadf_miss, methods="Optim", use.multicore=TRUE, n.core=parallel::detectCores()-2, lambda=c(1,1.25,1.5), tau=3, ini.par=1, verbose=FALSE)
invisible("If the censoring rate is greater than 0.7, the direct optimization method can be used as an alternative because it is implemented as fast as the approximate optimization method.")
results.approx13

results.approx15 <- RMSTSens(time="diff", status="event", exposure="motivodeegreso_mod_imp_rec", level.exposed="1", ps="weight_mult_b", data=sub1_statadf_miss, methods="Optim", use.multicore=TRUE, n.core=parallel::detectCores()-2, lambda=c(1,1.25,1.5), tau=5, ini.par=1, verbose=FALSE)
invisible("If the censoring rate is greater than 0.7, the direct optimization method can be used as an alternative because it is implemented as fast as the approximate optimization method.")
results.approx15

results.approx21 <- RMSTSens(time="diff", status="event", exposure="motivodeegreso_mod_imp_rec", level.exposed="1", ps="weight_mult_b", data=sub1_statadf_miss, methods="Optim", use.multicore=TRUE, n.core=parallel::detectCores()-2, lambda=c(1,1.25,1.5), tau=1, ini.par=1, verbose=FALSE)
invisible("If the censoring rate is greater than 0.7, the direct optimization method can be used as an alternative because it is implemented as fast as the approximate optimization method.")
results.approx21

results.approx23 <- RMSTSens(time="diff", status="event", exposure="motivodeegreso_mod_imp_rec", level.exposed="1", ps="weight_mult_b", data=sub1_statadf_miss, methods="Optim", use.multicore=TRUE, n.core=parallel::detectCores()-2, lambda=c(1,1.25,1.5), tau=3, ini.par=1, verbose=FALSE)
invisible("If the censoring rate is greater than 0.7, the direct optimization method can be used as an alternative because it is implemented as fast as the approximate optimization method.")
results.approx23

results.approx25 <- RMSTSens(time="diff", status="event", exposure="motivodeegreso_mod_imp_rec", level.exposed="1", ps="weight_mult_b", data=sub1_statadf_miss, methods="Optim", use.multicore=TRUE, n.core=parallel::detectCores()-2, lambda=c(1,1.25,1.5), tau=5, ini.par=1, verbose=FALSE)
invisible("If the censoring rate is greater than 0.7, the direct optimization method can be used as an alternative because it is implemented as fast as the approximate optimization method.")
results.approx25

Time for this code chunk to run: 0 minutes

Show code
ifelse(!require("coxphw"), install.packages("coxphw"),library("coxphw"))

Loading required package: coxphw

[1] "coxphw"
Show code
system.time({
pwp_pris<-
  coxphw::coxphw(Surv(diff,event==1)  ~ mot_egr_early+ mot_egr_late+ tr_mod2+ sex_dum2+ edad_ini_cons+ esc1+ esc2+ sus_prin2+ sus_prin3+ sus_prin4+ sus_prin5+ fr_cons_sus_prin2+ fr_cons_sus_prin3+ fr_cons_sus_prin4+ fr_cons_sus_prin5+ cond_ocu2+ cond_ocu3+ cond_ocu4+ cond_ocu5+ cond_ocu6+ policonsumo+ num_hij2+ tenviv1+ tenviv2+ tenviv4+ tenviv5+ mzone2+ mzone3+ n_off_vio+ n_off_acq+ n_off_sud+ n_off_oth+ psy_com2+ psy_com3+ dep2+ rural2+ rural3+ porc_pobr+ susini2+ susini3+ susini4+ susini5+ ano_nac_corr+ cohab2+ cohab3+ cohab4+ fis_com2+ fis_com3+ rc_x1+ rc_x2+ rc_x3, data = statadf_miss_pris, template="AHR",  robust=T) #, clusterid=statadf_miss_pris$hash_key
#  ~ motivodeegreso_mod_imp_rec+ tr_mod2+ sex_dum2+ edad_ini_cons+ esc1+ esc2+ sus_prin2+ sus_prin3+ sus_prin4+ sus_prin5+ fr_cons_sus_prin2+ fr_cons_sus_prin3+ fr_cons_sus_prin4+ fr_cons_sus_prin5+ cond_ocu2+ cond_ocu3+ cond_ocu4+ cond_ocu5+ cond_ocu6+ policonsumo+ num_hij2+ tenviv1+ tenviv2+ tenviv4+ tenviv5+ mzone2+ mzone3+ n_off_vio+ n_off_acq+ n_off_sud+ n_off_oth+ psy_com2+ psy_com3+ dep2+ rural2+ rural3+ porc_pobr+ susini2+ susini3+ susini4+ susini5+ ano_nac_corr+ cohab2+ cohab3+ cohab4+ fis_com2+ fis_com3+ rc_x1+ rc_x2+ rc_x3, data = statadf_miss_pris
})
   user  system elapsed 
 511.78   55.90  568.75 
Show code
system.time({
pwp_condemn<-
  coxphw::coxphw(Surv(diff,event==1)  ~ mot_egr_early+ mot_egr_late+ tr_mod2+ sex_dum2+ edad_ini_cons+ esc1+ esc2+ sus_prin2+ sus_prin3+ sus_prin4+ sus_prin5+ fr_cons_sus_prin2+ fr_cons_sus_prin3+ fr_cons_sus_prin4+ fr_cons_sus_prin5+ cond_ocu2+ cond_ocu3+ cond_ocu4+ cond_ocu5+ cond_ocu6+ policonsumo+ num_hij2+ tenviv1+ tenviv2+ tenviv4+ tenviv5+ mzone2+ mzone3+ n_off_vio+ n_off_acq+ n_off_sud+ n_off_oth+ psy_com2+ psy_com3+ dep2+ rural2+ rural3+ porc_pobr+ susini2+ susini3+ susini4+ susini5+ ano_nac_corr+ cohab2+ cohab3+ cohab4+ fis_com2+ fis_com3+ rc_x1+ rc_x2+ rc_x3, data = statadf_miss, template="AHR",  robust=T) #, clusterid=statadf_miss_pris$hash_key
#  ~ motivodeegreso_mod_imp_rec+ tr_mod2+ sex_dum2+ edad_ini_cons+ esc1+ esc2+ sus_prin2+ sus_prin3+ sus_prin4+ sus_prin5+ fr_cons_sus_prin2+ fr_cons_sus_prin3+ fr_cons_sus_prin4+ fr_cons_sus_prin5+ cond_ocu2+ cond_ocu3+ cond_ocu4+ cond_ocu5+ cond_ocu6+ policonsumo+ num_hij2+ tenviv1+ tenviv2+ tenviv4+ tenviv5+ mzone2+ mzone3+ n_off_vio+ n_off_acq+ n_off_sud+ n_off_oth+ psy_com2+ psy_com3+ dep2+ rural2+ rural3+ porc_pobr+ susini2+ susini3+ susini4+ susini5+ ano_nac_corr+ cohab2+ cohab3+ cohab4+ fis_com2+ fis_com3+ rc_x1+ rc_x2+ rc_x3, data = statadf_miss_pris
})
   user  system elapsed 
1086.83   54.81 1143.95 
Show code
#pris
#sent
coxphw_coefs<-
rbind.data.frame(
cbind.data.frame(type="Condemnatory Sentence", coef= coef(pwp_condemn)[c(1:2)],coxphw:::confint.coxphw(pwp_condemn)[c(1:2),]),
cbind.data.frame(type="Imprisonment",coef= c(coef(pwp_pris)[c(1:2)]),coxphw:::confint.coxphw(pwp_pris)[c(1:2),])
) 

coxphw_coefs%>% 
  dplyr::mutate_if(is.numeric,~exp(.)) %>% 
  knitr::kable("markdown", caption="Averaged hazard ratios", digits=2)
Table 17: Averaged hazard ratios
type coef 2.5 % 97.5 %
mot_egr_early Condemnatory Sentence 1.70 1.59 1.81
mot_egr_late Condemnatory Sentence 1.54 1.46 1.62
mot_egr_early1 Imprisonment 1.82 1.57 2.11
mot_egr_late1 Imprisonment 1.48 1.31 1.66

Time for this code chunk to run: 0.5 minutes

Show code
#early
#pwp_pris, mot_egr_early 1.8193382  1.5723899  2.1050704  ==== 3.040261 2.521084      NA
#pwp_condemn, mot_egr_early  1.6981313  1.5911785  1.8122731 ==== 2.239818 2.101310       NA

#late
#pwp_pris, mot_egr_late  1.4784520  1.3134946  1.6641257 ==== 2.319504 1.955190       NA
#pwp_condemn, mot_egr_late   1.5377467  1.4629155  1.6164056  ====  2.029950 1.927147       NA
require(EValue)

Loading required package: EValue

Registered S3 methods overwritten by ‘EValue’: method from print.estimate lava summary.estimate lava

Attaching package: ‘EValue’

The following object is masked from ‘package:survey’:

HR
Show code
evalues.RR(est = exp(coxphw_coefs[3,2]), 
           lo = exp(coxphw_coefs[3,3]), hi= exp(coxphw_coefs[3,4]), rare=T)
            point    lower   upper
RR       1.819338 1.572390 2.10507
E-values 3.040261 2.521084      NA
Show code
#E-values 3.040261 2.521084      NA
evalues.RR(est = toRR(HR(exp(coxphw_coefs[1,2]), rare = FALSE)), 
           lo = toRR(HR(exp(coxphw_coefs[1,3]), rare = FALSE)), 
           hi = toRR(HR(exp(coxphw_coefs[1,4]), rare = FALSE)), 
           rare=T)
            point    lower    upper
RR       1.441756 1.378716 1.507518
E-values 2.239818 2.101310       NA
Show code
#RR       1.441756 1.378716 1.507518
#2.239818 2.101310       NA
evalues.RR(est = exp(coxphw_coefs[4,2]), 
           lo = exp(coxphw_coefs[4,3]), 
           hi = exp(coxphw_coefs[4,4]), rare=T)
            point    lower    upper
RR       1.478452 1.313495 1.664126
E-values 2.319504 1.955190       NA
Show code
# 2.319504 1.955190       NA
evalues.RR(est = toRR(HR(exp(coxphw_coefs[2,2]), rare = FALSE)), 
           lo = toRR(HR(exp(coxphw_coefs[2,3]), rare = FALSE)), 
           hi = toRR(HR(exp(coxphw_coefs[2,4]) , rare = FALSE)), 
           rare=T)
            point    lower    upper
RR       1.346677 1.301161 1.393714
E-values 2.029949 1.927147       NA
Show code
# RR       1.346677 1.301161 1.393714
# E-values 2.029950 1.927147       NA

Time for this code chunk to run: 0 minutes


Session Info

Show code
message(Sys.getenv("R_LIBS_USER"))

C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/renv/library/R-4.1/x86_64-w64-mingw32;C:/Users/CISS Fondecyt/AppData/Local/Temp/RtmpsP2zac/renv-system-library

Show code
[1] "2023-06-18"
Show code
message(paste0("Editor context: ", path))

Editor context: C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)

Show code
#load(paste0(dirname(rstudioapi::getSourceEditorContext()$path),"/15.RData"))

if (grepl("CISS Fondecyt",rstudioapi::getSourceEditorContext()$path)==T){
    save.image("C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2022 (github)/15.RData")
  } else if (grepl("andre",rstudioapi::getSourceEditorContext()$path)==T){
    save.image("C:/Users/andre/Desktop/SUD_CL/15.RData")
  } else if (grepl("E:",rstudioapi::getSourceEditorContext()$path)==T){
    save.image("E:/Mi unidad/Alvacast/SISTRAT 2022 (github)/15.RData")
  } else {
     tryCatch(save.image(paste0(sub("2019","2022",sub("SUD_CL","",path)),"15.RData")),
              error= save.image(paste0(sub("2019","2022",sub("SUD_CL","",path)),"/15.RData")))
  }

sesion_info <- devtools::session_info()
dplyr::select(
  tibble::as_tibble(sesion_info$packages),
  c(package, loadedversion, source)
) %>% 
  DT::datatable(filter = 'top', colnames = c('Row number' =1,'Variable' = 2, 'Percentage'= 3),
              caption = htmltools::tags$caption(
        style = 'caption-side: top; text-align: left;',
        '', htmltools::em('Packages')),
      options=list(
initComplete = htmlwidgets::JS(
        "function(settings, json) {",
        "$(this.api().tables().body()).css({
            'font-family': 'Helvetica Neue',
            'font-size': '50%', 
            'code-inline-font-size': '15%', 
            'white-space': 'nowrap',
            'line-height': '0.75em',
            'min-height': '0.5em'
            });",#;
        "}")))

Time for this code chunk to run: 0 minutes


June 18, 2023


Show code
#put name of the file
file<- "fiscalia_mariel_feb_2023_match_SENDA.dta"

subset(Base_fiscalia_v14, select= c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec")) %>% 

export_lab_stata_merge<-
  tibble::rownames_to_column(data.frame(Hmisc::label(dplyr::select(Base_fiscalia_v14,  all_of(c("hash_key", vars_cov, "fech_ing_num_1", "fech_nac_rec"))))))%>% data.frame() %>%
  
  dplyr::rename("code" = !!names(.[1]), "label" = !!names(.[2]))%>% data.frame()%>%
  dplyr::mutate(first= "cap noi label variable")%>%
  dplyr::mutate(final= paste0(first, " ",code,' "',label,'"'))%>%
  dplyr::select(-code,-label,-first)%>%
  dplyr::rename("*clear all"="final") %>%
  rbind(paste0('cap noi save "', gsub('/', '\\', path, fixed=T),'\\',file,'", replace'))%>%
  rbind(paste0('cap noi save "', gsub('/', '\\', path, fixed=T),'\\',file,'", replace'))

rbind(paste0('cap noi use "', gsub('/', '\\', path, fixed=T),'\\',file,'", clear'),export_lab_stata_merge) %>% knitr::kable("markdown")

write.table(rbind(paste0('cap noi use "', gsub('/', '\\', path, fixed=T),'\\',file,'", clear'),export_lab_stata_merge), file = paste0(path,"/_label_var_to_stata_feb2023.do"), sep = "",row.names = FALSE, quote = FALSE, fileEncoding="UTF-8")

Time for this code chunk to run: 0 minutes


Show code
*should be in the same folder of the .Rmd to work
cap noi do _label_var_to_stata_jan2023_mod
Error in running command st

Time for this code chunk to run: 0 minutes