Consolidate the database


Se genera la base de datos con las nuevas variables solicitadas. Conste que esta base de datos ya filtraba a los menores de edad pero no a aquellos que no reportaron edad válida.


invisible("84,936 x 254 # 2020-10-25")
invisible("84,944 x 277 # 2020-12-20")

prueba<-
CONS_C1_df_dup_SEP_2020 %>% 
dplyr::group_by(hash_key) %>% 
  dplyr::mutate(menor_edad=dplyr::case_when(edad_al_ing<18~1,TRUE~0),menor_edad=sum(menor_edad,na.rm=T)) %>% 
  dplyr::ungroup() %>% 
  dplyr::filter(edad_al_ing>=18| is.na(edad_al_ing)) %>%  #Sólo así llegamos a 109,642 casos, igual que en STATA
  
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(dup2=row_number()) %>% 
  dplyr::mutate(duplicates_filtered2=n()) %>% 
  
  dplyr::mutate(max_cum_dias_trat_sin_na=max(cum_dias_trat_sin_na, na.rm=T)) %>% 
  dplyr::mutate(max_cum_diff_bet_treat=max(cum_diff_bet_treat, na.rm=T)) %>% 
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#Formatear días de tratamiento y diferencia en días excliyendo a los casos a los que le borramos un tratamiento menor a 18.
#local vars `" "diff_bet_treat_" "dias_treat_imp_sin_na_" "cum_diff_bet_treat_" "cum_dias_trat_sin_na_" "mean_cum_dias_trat_sin_na_" "mean_cum_diff_bet_treat_" "'   
  dplyr::ungroup() %>% 
  dplyr::filter(dup2==1) %>%   #84,936 x 254 # 2020-10-25, 
  dplyr::mutate(cum_dias_trat_sin_na_1= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_2,TRUE~cum_dias_trat_sin_na_1)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_2= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_3,TRUE~cum_dias_trat_sin_na_2)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_3= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_4,TRUE~cum_dias_trat_sin_na_3)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_4= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_5,TRUE~cum_dias_trat_sin_na_4)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_5= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_6,TRUE~cum_dias_trat_sin_na_5)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_6= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_7,TRUE~cum_dias_trat_sin_na_6)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_7= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_8,TRUE~cum_dias_trat_sin_na_7)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_8= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_9,TRUE~cum_dias_trat_sin_na_8)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_9= dplyr::case_when(menor_edad>0~cum_dias_trat_sin_na_10,TRUE~cum_dias_trat_sin_na_9)) %>% 
  dplyr::mutate(cum_dias_trat_sin_na_10= dplyr::case_when(menor_edad>0~NA_real_,TRUE~cum_dias_trat_sin_na_10)) %>% 
  
  dplyr::mutate(cum_diff_bet_treat_1= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_2,TRUE~cum_diff_bet_treat_1)) %>% 
  dplyr::mutate(cum_diff_bet_treat_2= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_3,TRUE~cum_diff_bet_treat_2)) %>% 
  dplyr::mutate(cum_diff_bet_treat_3= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_4,TRUE~cum_diff_bet_treat_3)) %>% 
  dplyr::mutate(cum_diff_bet_treat_4= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_5,TRUE~cum_diff_bet_treat_4)) %>% 
  dplyr::mutate(cum_diff_bet_treat_5= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_6,TRUE~cum_diff_bet_treat_5)) %>% 
  dplyr::mutate(cum_diff_bet_treat_6= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_7,TRUE~cum_diff_bet_treat_6)) %>% 
  dplyr::mutate(cum_diff_bet_treat_7= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_8,TRUE~cum_diff_bet_treat_7)) %>% 
  dplyr::mutate(cum_diff_bet_treat_8= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_9,TRUE~cum_diff_bet_treat_8)) %>% 
  dplyr::mutate(cum_diff_bet_treat_9= dplyr::case_when(menor_edad>0~cum_diff_bet_treat_10,TRUE~cum_diff_bet_treat_9)) %>% 
  dplyr::mutate(cum_diff_bet_treat_10= dplyr::case_when(menor_edad>0~NA_real_,TRUE~cum_diff_bet_treat_10)) %>%
  
  dplyr::mutate(tipo_de_plan_2_1= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_2),TRUE~as.character(tipo_de_plan_2_1))) %>% 
  dplyr::mutate(tipo_de_plan_2_2= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_3),TRUE~as.character(tipo_de_plan_2_2))) %>% 
  dplyr::mutate(tipo_de_plan_2_3= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_4),TRUE~as.character(tipo_de_plan_2_3))) %>% 
  dplyr::mutate(tipo_de_plan_2_4= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_5),TRUE~as.character(tipo_de_plan_2_4))) %>% 
  dplyr::mutate(tipo_de_plan_2_5= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_6),TRUE~as.character(tipo_de_plan_2_5))) %>% 
  dplyr::mutate(tipo_de_plan_2_6= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_7),TRUE~as.character(tipo_de_plan_2_6))) %>% 
  dplyr::mutate(tipo_de_plan_2_7= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_8),TRUE~as.character(tipo_de_plan_2_7))) %>% 
  dplyr::mutate(tipo_de_plan_2_8= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_9),TRUE~as.character(tipo_de_plan_2_8))) %>% 
  dplyr::mutate(tipo_de_plan_2_9= dplyr::case_when(menor_edad>0~as.character(tipo_de_plan_2_10),TRUE~as.character(tipo_de_plan_2_9))) %>% 
  dplyr::mutate(tipo_de_plan_2_10= dplyr::case_when(menor_edad>0~NA_character_,TRUE~as.character(tipo_de_plan_2_10))) %>%
  
  dplyr::mutate(motivodeegreso_mod_imp_1= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_2),TRUE~as.character(motivodeegreso_mod_imp_1))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_2= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_3),TRUE~as.character(motivodeegreso_mod_imp_2))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_3= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_4),TRUE~as.character(motivodeegreso_mod_imp_3))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_4= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_5),TRUE~as.character(motivodeegreso_mod_imp_4))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_5= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_6),TRUE~as.character(motivodeegreso_mod_imp_5))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_6= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_7),TRUE~as.character(motivodeegreso_mod_imp_6))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_7= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_8),TRUE~as.character(motivodeegreso_mod_imp_7))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_8= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_9),TRUE~as.character(motivodeegreso_mod_imp_8))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_9= dplyr::case_when(menor_edad>0~as.character(motivodeegreso_mod_imp_10),TRUE~as.character(motivodeegreso_mod_imp_9))) %>% 
  dplyr::mutate(motivodeegreso_mod_imp_10= dplyr::case_when(menor_edad>0~NA_character_,TRUE~as.character(motivodeegreso_mod_imp_10))) %>%
  
  dplyr::mutate(mean_cum_dias_trat_sin_na_1= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_2,TRUE~mean_cum_dias_trat_sin_na_1)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_2= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_3,TRUE~mean_cum_dias_trat_sin_na_2)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_3= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_4,TRUE~mean_cum_dias_trat_sin_na_3)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_4= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_5,TRUE~mean_cum_dias_trat_sin_na_4)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_5= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_6,TRUE~mean_cum_dias_trat_sin_na_5)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_6= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_7,TRUE~mean_cum_dias_trat_sin_na_6)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_7= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_8,TRUE~mean_cum_dias_trat_sin_na_7)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_8= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_9,TRUE~mean_cum_dias_trat_sin_na_8)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_9= dplyr::case_when(menor_edad>0~mean_cum_dias_trat_sin_na_10,TRUE~mean_cum_dias_trat_sin_na_9)) %>% 
  dplyr::mutate(mean_cum_dias_trat_sin_na_10= dplyr::case_when(menor_edad>0~NA_real_,TRUE~mean_cum_dias_trat_sin_na_10)) %>%
    
  dplyr::mutate(mean_cum_diff_bet_treat_1= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_2,TRUE~mean_cum_diff_bet_treat_1)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_2= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_3,TRUE~mean_cum_diff_bet_treat_2)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_3= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_4,TRUE~mean_cum_diff_bet_treat_3)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_4= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_5,TRUE~mean_cum_diff_bet_treat_4)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_5= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_6,TRUE~mean_cum_diff_bet_treat_5)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_6= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_7,TRUE~mean_cum_diff_bet_treat_6)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_7= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_8,TRUE~mean_cum_diff_bet_treat_7)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_8= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_9,TRUE~mean_cum_diff_bet_treat_8)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_9= dplyr::case_when(menor_edad>0~mean_cum_diff_bet_treat_10,TRUE~mean_cum_diff_bet_treat_9)) %>% 
  dplyr::mutate(mean_cum_diff_bet_treat_10= dplyr::case_when(menor_edad>0~NA_real_,TRUE~mean_cum_diff_bet_treat_10)) %>%
    
  dplyr::mutate(diff_bet_treat_1= dplyr::case_when(menor_edad>0~diff_bet_treat_2,TRUE~diff_bet_treat_1)) %>% 
  dplyr::mutate(diff_bet_treat_2= dplyr::case_when(menor_edad>0~diff_bet_treat_3,TRUE~diff_bet_treat_2)) %>% 
  dplyr::mutate(diff_bet_treat_3= dplyr::case_when(menor_edad>0~diff_bet_treat_4,TRUE~diff_bet_treat_3)) %>% 
  dplyr::mutate(diff_bet_treat_4= dplyr::case_when(menor_edad>0~diff_bet_treat_5,TRUE~diff_bet_treat_4)) %>% 
  dplyr::mutate(diff_bet_treat_5= dplyr::case_when(menor_edad>0~diff_bet_treat_6,TRUE~diff_bet_treat_5)) %>% 
  dplyr::mutate(diff_bet_treat_6= dplyr::case_when(menor_edad>0~diff_bet_treat_7,TRUE~diff_bet_treat_6)) %>% 
  dplyr::mutate(diff_bet_treat_7= dplyr::case_when(menor_edad>0~diff_bet_treat_8,TRUE~diff_bet_treat_7)) %>% 
  dplyr::mutate(diff_bet_treat_8= dplyr::case_when(menor_edad>0~diff_bet_treat_9,TRUE~diff_bet_treat_8)) %>% 
  dplyr::mutate(diff_bet_treat_9= dplyr::case_when(menor_edad>0~diff_bet_treat_10,TRUE~diff_bet_treat_9)) %>% 
  dplyr::mutate(diff_bet_treat_10= dplyr::case_when(menor_edad>0~NA_real_,TRUE~diff_bet_treat_10)) %>%
      
  dplyr::mutate(dias_treat_imp_sin_na_1= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_2,TRUE~dias_treat_imp_sin_na_1)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_2= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_3,TRUE~dias_treat_imp_sin_na_2)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_3= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_4,TRUE~dias_treat_imp_sin_na_3)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_4= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_5,TRUE~dias_treat_imp_sin_na_4)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_5= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_6,TRUE~dias_treat_imp_sin_na_5)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_6= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_7,TRUE~dias_treat_imp_sin_na_6)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_7= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_8,TRUE~dias_treat_imp_sin_na_7)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_8= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_9,TRUE~dias_treat_imp_sin_na_8)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_9= dplyr::case_when(menor_edad>0~dias_treat_imp_sin_na_10,TRUE~dias_treat_imp_sin_na_9)) %>% 
  dplyr::mutate(dias_treat_imp_sin_na_10= dplyr::case_when(menor_edad>0~NA_real_,TRUE~dias_treat_imp_sin_na_10)) %>%

# diff y dias treat  
  dplyr::mutate(dias_treat_imp_sin_na_four = rowMeans(dplyr::select(., dias_treat_imp_sin_na_4, dias_treat_imp_sin_na_5, dias_treat_imp_sin_na_6, dias_treat_imp_sin_na_7, dias_treat_imp_sin_na_8, dias_treat_imp_sin_na_9, dias_treat_imp_sin_na_10), na.rm=T)) %>% #,na.rm=F
  
  dplyr::mutate(diff_bet_treat_four = rowMeans(dplyr::select(., diff_bet_treat_4, diff_bet_treat_5, diff_bet_treat_6, diff_bet_treat_7, diff_bet_treat_8, diff_bet_treat_9, diff_bet_treat_10), na.rm=T)) %>% #,na.rm=F
# cum
  dplyr::mutate(cum_dias_trat_sin_na_four = rowMeans(dplyr::select(., cum_dias_trat_sin_na_4, cum_dias_trat_sin_na_5, cum_dias_trat_sin_na_6, cum_dias_trat_sin_na_7, cum_dias_trat_sin_na_8, cum_dias_trat_sin_na_9, cum_dias_trat_sin_na_10), na.rm=T)) %>% #,na.rm=F
  
  dplyr::mutate(cum_diff_bet_treat_four = rowMeans(dplyr::select(., cum_diff_bet_treat_4, cum_diff_bet_treat_5, cum_diff_bet_treat_6, cum_diff_bet_treat_7, cum_diff_bet_treat_8, cum_diff_bet_treat_9, cum_diff_bet_treat_10), na.rm=T)) %>% #,na.rm=F
# mean cum
  dplyr::mutate(mean_cum_dias_trat_sin_na_four = rowMeans(dplyr::select(., mean_cum_dias_trat_sin_na_4, mean_cum_dias_trat_sin_na_5, mean_cum_dias_trat_sin_na_6, mean_cum_dias_trat_sin_na_7, mean_cum_dias_trat_sin_na_8, mean_cum_dias_trat_sin_na_9, mean_cum_dias_trat_sin_na_10), na.rm=T)) %>% #,na.rm=F
  
  dplyr::mutate(mean_cum_diff_bet_treat_four = rowMeans(dplyr::select(., mean_cum_diff_bet_treat_4, mean_cum_diff_bet_treat_5, mean_cum_diff_bet_treat_6, mean_cum_diff_bet_treat_7, mean_cum_diff_bet_treat_8, mean_cum_diff_bet_treat_9, mean_cum_diff_bet_treat_10), na.rm=T)) %>% #,na.rm=F
  # tipo de plana
  dplyr::mutate(tipo_de_plan_2_mod=dplyr::case_when(grepl("PAB",tipo_de_plan_2)~"PAB",
                                                    grepl("PAI",tipo_de_plan_2)~"PAI",
                                                    grepl("PR",tipo_de_plan_2)~"PR",
                                                    TRUE~NA_character_)) %>% 
  dplyr::mutate(tipo_de_plan_2_mod=factor(tipo_de_plan_2_mod)) %>% 
  dplyr::mutate(estatus_ocupacional= dplyr::case_when(!is.na(cat_ocupacional)&!is.na(estatus_ocupacional)~"Empleado",
                                                      TRUE~as.character(estatus_ocupacional)))%>% 
  dplyr::mutate(estatus_ocupacional= as.factor(estatus_ocupacional))%>% 
  dplyr::mutate(cnt_mod_cie_10_dg_cons_sus_or= dplyr::case_when(as.character(dg_trs_cons_sus_or)=="Drug dependence"~dg_total_cie_10+1,
                                                    TRUE~dg_total_cie_10))%>% 
  dplyr::mutate(freq_cons_sus_prin= dplyr::case_when(as.character(freq_cons_sus_prin)=="Did not use"~"Less than 1 day a week",
                                                     TRUE~as.character(freq_cons_sus_prin)))%>% 
  dplyr::mutate(freq_cons_sus_prin= as.factor(freq_cons_sus_prin)) %>% 
  #edad más 65
  dplyr::mutate(mas_65= dplyr::case_when(edad_al_ing>65~1,
                                                     TRUE~0))%>% 
  dplyr::mutate(mas_65= as.factor(mas_65)) %>% 
  dplyr::mutate(comorbidity_icd_10=dplyr::case_when(dg_total_cie_10>=2~ "Two or more",
                                                    dg_total_cie_10==1~ "One",
                                                    as.character(dg_cie_10_rec)=="Diagnosis unknown (under study)"~"Diagnosis unknown (under study)",
                                                    as.character(dg_cie_10_rec)=="Without psychiatric comorbidity"~"Without psychiatric comorbidity")) %>%
  dplyr::mutate(comorbidity_icd_10=as.factor(comorbidity_icd_10)) %>% 
 # dplyr::select(-menor_edad) %>% 
  dplyr::mutate(no_group=1) %>% 
  dplyr::mutate(had_readm=dplyr::case_when(duplicates_filtered2>1~1,
                                           TRUE~0)) %>% 
  dplyr::mutate(n_treats=factor(dplyr::case_when(duplicates_filtered2>3~"04 or more",
                                duplicates_filtered2==3~"03",
                                duplicates_filtered2==2~"02",
                                duplicates_filtered2==1~"01"))) 


Updated in 2021 June & August

We only left users in treatments in General Population (PG) and missing values. Additionally, we recategorized the variable ‘Living with’ (con_quien_vive).


#janitor::tabyl(CONS_C1_df_dup_SEP_2020$otros_probl_at_sm_or)
#round(prop.table(table(CONS_C1_df_dup_SEP_2020$otros_probl_at_sm_or)),3)
#With      With                 Other Alone  With            With  
#relatives couple and children               children only   couple only
#38105        21806             7275   8026   3191           6540 

library(readr)
## Warning: package 'readr' was built under R version 4.0.5
prueba2<-
prueba %>% 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
  #AUG 2021: 
  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")) %>% 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
    #janitor::tabyl(con_quien_vive, con_quien_vive_rec)
    dplyr::filter(!grepl("M-",tipo_de_plan_2)) %>% 
    #janitor::tabyl(embarazo)
    #No    Si  <NA> 
    #76081   496    89 
    #prueba2 %>% janitor::tabyl(embarazo,tiene_menores_de_edad_a_cargo)
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
  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::filter(edad_al_ing_grupos=="18-29") %>% 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
    dplyr::mutate(dom_violence=factor(dplyr::case_when(
      grepl("Violencia Intrafamiliar$",otros_probl_at_sm_or, ignore.case=T)~1,
      is.na(otros_probl_at_sm_or)~NA_real_,
      T~0),levels=c(0,1),labels=c("No domestic violence","Domestic violence"))) %>% 
    dplyr::mutate(sex_abuse=factor(dplyr::case_when(
      grepl("Abuso Sexual",otros_probl_at_sm_or, ignore.case=T)~1,
      is.na(otros_probl_at_sm_or)~NA_real_,
      T~0),levels=c(0,1),labels=c("No sexual abuse","Sexual abuse"))) %>% 
  dplyr::filter(!grepl("Others",con_quien_vive_joel)) %>% 
#5 de agosto, paso de 26,236 a 23,979, descartando 2,257 casos que tienen "otros".
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
  #TENENCIA VIVIENDA
  #Allegado, Arrienda, Cedida, Ocupación Irregular, Otros, Paga dividendo, Propia
#vive transitoriamente en casa ajena
  dplyr::mutate(tenencia_de_la_vivienda_mod=
                  factor(dplyr::case_when(tenencia_de_la_vivienda_mod=="Allegado"~"Stays temporarily with a relative",
                                 tenencia_de_la_vivienda_mod=="Arrienda"~"Renting",
                                 tenencia_de_la_vivienda_mod=="Cedida"~"Owner/Transferred dwellings/Pays Dividends",
                                 tenencia_de_la_vivienda_mod=="Ocupación Irregular"~"Illegal Settlement",
                                 tenencia_de_la_vivienda_mod=="Otros"~"Others",
                                 tenencia_de_la_vivienda_mod=="Paga dividendo"~"Owner/Transferred dwellings/Pays Dividends",
                                 tenencia_de_la_vivienda_mod=="Propia"~"Owner/Transferred dwellings/Pays Dividends",
                                 T~NA_character_))) %>% 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:   
#:#:#:#:#:#:#:#:#:#DAR ESTRUCTURA ORDINAL#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
  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(compromiso_biopsicosocial=parse_factor(as.character(compromiso_biopsicosocial),levels=c('1-Mild', '2-Moderate','3-Severe'), ordered =T,trim_ws=F,include_na =F)) %>% #, locale=locale(encoding = "Latin1")
    dplyr::mutate(comorbidity_icd_10=parse_factor(as.character(comorbidity_icd_10),levels=c('Without psychiatric comorbidity', 'Diagnosis unknown (under study)','One','Two or more'), ordered =T,trim_ws=F,include_na =F)) %>% #, locale=locale(encoding = "Latin1")
    dplyr::mutate(cnt_mod_cie_10_or=parse_factor(as.character(cnt_mod_cie_10_or),levels=c('0', '1','2','3'), ordered =T,trim_ws=F,include_na =F)) %>%   #, locale=locale(encoding = "Latin1")
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#dplyr::mutate(edad_al_ing_grupos=if_else(edad_al_ing_grupos=='18-29','<18-29',as.character(edad_al_ing_grupos),NA_character_)) %>%
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#FRECUENCIA CONSUMO: Regroup 1 día a la semana y No usó en el último mes
dplyr::mutate(freq_cons_sus_prin=if_else(freq_cons_sus_prin=='Did not use','Less than 1 day a week',as.character(freq_cons_sus_prin),NA_character_)) 
#"escolaridad_rec", "freq_cons_sus_prin", "num_otras_sus_mod", "numero_de_hijos_mod_joel", "cnt_mod_cie_10_or", "comorbidity_icd_10", "compromiso_biopsicosocial"    
  
  
#numero_de_hijos_mod #Número de Hijos (Valor Max.)/Number of Children (Max. Value)
#hijos_trat_res #Tiene Hijos en Ingreso a Tratamiento Residencial del Último Registro/Have Children in Residential Treatment of the Last Entry
#prueba2 %>% janitor::tabyl(numero_de_hijos_mod,embarazo)     
#prueba2 %>% janitor::tabyl(numero_de_hijos_mod,hijos_trat_res) #me parece que está bien, no hay contradicciones
#prueba2 %>% janitor::tabyl(con_quien_vive,con_quien_vive_joel)
#prueba2 %>% janitor::tabyl(con_quien_vive,con_quien_vive_joel) %>% copiar_nombres()

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
prueba2%>%
  dplyr::arrange(hash_key, fech_ing)%>% 
  rio::export(file = paste0(gsub("analisis_joel.Rmd","",path),"CONS_C1_df_dup_SEP_2020_joel_2021.dta"))


In the data base for 2021, only includes general population or mixed-gender programs at baseline. The variable whether the respondent had children at admission to a residential treatment had many missing values (16%). In the cases that is feasible to have children, it shows 3% of missing values.

Must note that there are patients that may respond that they have children in treatment and respond that they have more than a child. But there can be people that did not report children and having children in residential treatments.


  • ‘Starting substance’ (sus_ini_mod_mvv)= Most vulnerable value.
  • ‘Number of children’(numero_de_hijos_mod_joel)= If a user was pregnant, we added one children into the count of the number of children, because of the sparsity of pregnancy status (5.22%).
  • ‘Primary or main substance’(sus_principal_mod)= We used the primary substance at admission of the largest treatment for continuous treatments. Every substance was chosen from the largest treatment, excepting cases where the value was not available in the largest treatment.
  • ‘Tenure status of households’ (tenencia_de_la_vivienda_mod)= Three categories were collapsed into one single condition: Owner (28.66%), with Transferred dwellings (3.76%) and Pays Dividend (1.79%).
  • ‘Educational Attainment’ (escolaridad_rec)= We selected the most vulnerable category among continuous treatments for the same user.
  • ‘Consumption frequency of primary or main substance’(freq_cons_sus_prin)= Among the categories of the Frequency of Drug Consumption, we grouped Less than 1 day a week (3.16%) with Did not use in the last 30 days (1.80%)


Total

#browse hash_key duplicates_filtered2 cum_diff_bet_treat dup2_real diff_bet_treat if hash_key=="0737aacbb7efdd418f7a37ce3386ce5e"|hash_key=="07668f2d3e4f6beb7975e43ee96eac80"

#diff_bet_treat_five+ cum_dias_trat_sin_na_five+ cum_diff_bet_treat_five+
#    mean_cum_dias_trat_sin_na_five+ mean_cum_diff_bet_treat_five,

attr(prueba$n_treats,"label") <- "No. of treatments with 18+ at admission between 2010 and 2019"
attr(prueba$comorbidity_icd_10,"label") <- "Comorbidity ICD-10 (with amount of different diagnosis)"
attr(prueba$dias_treat_imp_sin_na_four,"label") <- "Days of Treatment (Fourth or those that follow)"
attr(prueba$diff_bet_treat_four,"label") <- "Days of Difference Between Treatments (Fifth treatment or those that folow)"
attr(prueba$cum_dias_trat_sin_na_four,"label") <- "Cumulative Days of Treatment (Fourth or those that follow)"
attr(prueba$cum_diff_bet_treat_four,"label") <- "Cumulative Difference Between Treatments (Fifth or those that follow)" #hash_key 
attr(prueba$mean_cum_dias_trat_sin_na_four,"label") <- "Average Cumulative Days of Treatment (Fourth or those that follow)"
attr(prueba$mean_cum_diff_bet_treat_four,"label") <- "Average Cumulative Difference Between Treatments (Fifth or those that follow)"
attr(prueba$cnt_mod_cie_10_dg_cons_sus_or,"label") <- "Total count of Psychiatric & Drug dependence Diagnostics"
attr(prueba$max_cum_diff_bet_treat,"label") <- "Max. Cumulative Difference Between Treatments"
attr(prueba$max_cum_dias_trat_sin_na,"label") <- "Max. Cumulative Days of Treatment"
attr(prueba$tipo_de_plan_2_mod,"label") <- "Type of Plan (Independently of the Program)"
attr(prueba$condicion_ocupacional_corr,"label") <- 'Occupational Status Corrected(f)'
attr(prueba$cat_ocupacional_corr,"label") <- 'Occupational Category Corrected(f)'
attr(prueba$compromiso_biopsicosocial,"label") <- 'Biopsychosocial Compromise'
attr(prueba$tenencia_de_la_vivienda_mod,"label") <- 'Tenure status of households'
#attr(prueba$,"label")<-"Starting Substance"
attr(prueba$sus_principal_mod,"label")<-"Starting Substance"

attr(prueba$cum_dias_trat_sin_na_1,"label") <- "Cum. Days of Treatment (1st Treatment)"
attr(prueba$cum_dias_trat_sin_na_2,"label") <- "Cum. Days of Treatment (2nd Treatment)"
attr(prueba$cum_dias_trat_sin_na_3,"label") <- "Cum. Days of Treatment (3rd Treatment)"
attr(prueba$cum_dias_trat_sin_na_4,"label") <- "Cum. Days of Treatment (4th Treatment)"
attr(prueba$cum_dias_trat_sin_na_5,"label") <- "Cum. Days of Treatment (5th Treatment)"
attr(prueba$cum_dias_trat_sin_na_6,"label") <- "Cum. Days of Treatment (6th Treatment)"
attr(prueba$cum_dias_trat_sin_na_7,"label") <- "Cum. Days of Treatment (7th Treatment)"
attr(prueba$cum_dias_trat_sin_na_8,"label") <- "Cum. Days of Treatment (8th Treatment)"
attr(prueba$cum_dias_trat_sin_na_9,"label") <- "Cum. Days of Treatment (9th Treatment)"
attr(prueba$cum_dias_trat_sin_na_10,"label") <-"Cum. Days of Treatment (10th Treatment)"
attr(prueba$cum_diff_bet_treat_1,"label") <- "Cum. Diff Between Treatments (1st Treatment)"
attr(prueba$cum_diff_bet_treat_2,"label") <- "Cum. Diff Between Treatments (2nd Treatment)"
attr(prueba$cum_diff_bet_treat_3,"label") <- "Cum. Diff Between Treatments (3rd Treatment)"
attr(prueba$cum_diff_bet_treat_4,"label") <- "Cum. Diff Between Treatments (4th Treatment)"
attr(prueba$cum_diff_bet_treat_5,"label") <- "Cum. Diff Between Treatments (5th Treatment)"
attr(prueba$cum_diff_bet_treat_6,"label") <- "Cum. Diff Between Treatments (6th Treatment)"
attr(prueba$cum_diff_bet_treat_7,"label") <- "Cum. Diff Between Treatments (7th Treatment)"
attr(prueba$cum_diff_bet_treat_8,"label") <- "Cum. Diff Between Treatments (8th Treatment)"
attr(prueba$cum_diff_bet_treat_9,"label") <- "Cum. Diff Between Treatments (9th Treatment)"
attr(prueba$cum_diff_bet_treat_10,"label") <-"Cum. Diff Between Treatments (10th Treatment)"

attr(prueba2$n_treats,"label") <- "No. of treatments with 18+ at admission between 2010 and 2019"
attr(prueba2$comorbidity_icd_10,"label") <- "Comorbidity ICD-10 (with amount of different diagnosis)"
attr(prueba2$dias_treat_imp_sin_na_four,"label") <- "Days of Treatment (Fourth or those that follow)"
attr(prueba2$diff_bet_treat_four,"label") <- "Days of Difference Between Treatments (Fifth treatment or those that folow)"
attr(prueba2$cum_dias_trat_sin_na_four,"label") <- "Cumulative Days of Treatment (Fourth or those that follow)"
attr(prueba2$cum_diff_bet_treat_four,"label") <- "Cumulative Difference Between Treatments (Fifth or those that follow)" #hash_key 
attr(prueba2$mean_cum_dias_trat_sin_na_four,"label") <- "Average Cumulative Days of Treatment (Fourth or those that follow)"
attr(prueba2$mean_cum_diff_bet_treat_four,"label") <- "Average Cumulative Difference Between Treatments (Fifth or those that follow)"
attr(prueba2$cnt_mod_cie_10_dg_cons_sus_or,"label") <- "Total count of Psychiatric & Drug dependence Diagnostics"
attr(prueba2$max_cum_diff_bet_treat,"label") <- "Max. Cumulative Difference Between Treatments"
attr(prueba2$max_cum_dias_trat_sin_na,"label") <- "Max. Cumulative Days of Treatment"
attr(prueba2$tipo_de_plan_2_mod,"label") <- "Type of Plan (Independently of the Program)"
attr(prueba2$condicion_ocupacional_corr,"label") <- 'Occupational Status Corrected(f)'
attr(prueba2$cat_ocupacional_corr,"label") <- 'Occupational Category Corrected(f)'
attr(prueba2$con_quien_vive_joel,"label") <- 'Whom you live with(cohabitation status) (Recoded) (f)'
attr(prueba2$compromiso_biopsicosocial,"label") <- 'Biopsychosocial Compromise'
attr(prueba2$tenencia_de_la_vivienda_mod,"label") <- 'Tenure status of households'
attr(prueba2$sus_ini_mod_mvv,"label")<-"Starting Substance"

attr(prueba2$cum_dias_trat_sin_na_1,"label") <- "Cum. Days of Treatment (1st Treatment)"
attr(prueba2$cum_dias_trat_sin_na_2,"label") <- "Cum. Days of Treatment (2nd Treatment)"
attr(prueba2$cum_dias_trat_sin_na_3,"label") <- "Cum. Days of Treatment (3rd Treatment)"
attr(prueba2$cum_dias_trat_sin_na_4,"label") <- "Cum. Days of Treatment (4th Treatment)"
attr(prueba2$cum_dias_trat_sin_na_5,"label") <- "Cum. Days of Treatment (5th Treatment)"
attr(prueba2$cum_dias_trat_sin_na_6,"label") <- "Cum. Days of Treatment (6th Treatment)"
attr(prueba2$cum_dias_trat_sin_na_7,"label") <- "Cum. Days of Treatment (7th Treatment)"
attr(prueba2$cum_dias_trat_sin_na_8,"label") <- "Cum. Days of Treatment (8th Treatment)"
attr(prueba2$cum_dias_trat_sin_na_9,"label") <- "Cum. Days of Treatment (9th Treatment)"
attr(prueba2$cum_dias_trat_sin_na_10,"label") <-"Cum. Days of Treatment (10th Treatment)"

attr(prueba2$dias_treat_imp_sin_na_1,"label") <- "Days of Treatment (1st Treatment)"
attr(prueba2$dias_treat_imp_sin_na_2,"label") <- "Days of Treatment (2nd Treatment)"
attr(prueba2$dias_treat_imp_sin_na_3,"label") <- "Days of Treatment (3rd Treatment)"
attr(prueba2$dias_treat_imp_sin_na_4,"label") <- "Days of Treatment (4th Treatment)"
attr(prueba2$dias_treat_imp_sin_na_5,"label") <- "Days of Treatment (5th Treatment)"
attr(prueba2$dias_treat_imp_sin_na_6,"label") <- "Days of Treatment (6th Treatment)"
attr(prueba2$dias_treat_imp_sin_na_7,"label") <- "Days of Treatment (7th Treatment)"
attr(prueba2$dias_treat_imp_sin_na_8,"label") <- "Days of Treatment (8th Treatment)"
attr(prueba2$dias_treat_imp_sin_na_9,"label") <- "Days of Treatment (9th Treatment)"
attr(prueba2$dias_treat_imp_sin_na_10,"label") <-"Days of Treatment (10th Treatment)"

attr(prueba2$cum_diff_bet_treat_1,"label") <- "Cum. Diff Between Treatments (1st Treatment)"
attr(prueba2$cum_diff_bet_treat_2,"label") <- "Cum. Diff Between Treatments (2nd Treatment)"
attr(prueba2$cum_diff_bet_treat_3,"label") <- "Cum. Diff Between Treatments (3rd Treatment)"
attr(prueba2$cum_diff_bet_treat_4,"label") <- "Cum. Diff Between Treatments (4th Treatment)"
attr(prueba2$cum_diff_bet_treat_5,"label") <- "Cum. Diff Between Treatments (5th Treatment)"
attr(prueba2$cum_diff_bet_treat_6,"label") <- "Cum. Diff Between Treatments (6th Treatment)"
attr(prueba2$cum_diff_bet_treat_7,"label") <- "Cum. Diff Between Treatments (7th Treatment)"
attr(prueba2$cum_diff_bet_treat_8,"label") <- "Cum. Diff Between Treatments (8th Treatment)"
attr(prueba2$cum_diff_bet_treat_9,"label") <- "Cum. Diff Between Treatments (9th Treatment)"
attr(prueba2$cum_diff_bet_treat_10,"label") <-"Cum. Diff Between Treatments (10th Treatment)"
attr(prueba2$numero_de_hijos_mod_joel,"label") <-"Number of Children (Max. Value), adding 1 if pregnant at admission"
attr(prueba2$sex_abuse,"label") <-"Sexual abuse"
attr(prueba2$dom_violence,"label") <-"Domestic violence"
attr(prueba2$freq_cons_sus_prin,"label") <-"Frequency of drug use in the primary substance"

#n_treats mean_cum_diff_bet_treat_four mean_cum_diff_bet_treat_four cum_diff_bet_treat_four cum_dias_trat_sin_na_four diff_bet_treat_four dias_treat_imp_sin_na_four cnt_mod_cie_10_dg_cons_sus_or max_cum_diff_bet_treat max_cum_dias_trat_sin_na cum_diff_bet_treat_10 cum_dias_trat_sin_na_10 tipo_de_plan_2_mod


library(compareGroups)
table3 <- compareGroups::compareGroups(no_group ~ sexo_2+ escolaridad_rec+ estado_conyugal_2+ compromiso_biopsicosocial+ edad_ini_cons+ edad_al_ing+ sus_ini_mod+ sus_ini_mod_mvv+ freq_cons_sus_prin+ via_adm_sus_prin_act+ con_quien_vive_joel+ numero_de_hijos_mod_joel+ condicion_ocupacional_corr+ cat_ocupacional_corr+ abandono_temprano+ dg_cie_10_rec+ dias_treat_imp_sin_na+ cnt_diagnostico_trs_fisico+ cnt_otros_probl_at_sm_or+ tipo_de_plan_2_mod+ tenencia_de_la_vivienda_mod+ cum_dias_trat_sin_na_1+ cum_dias_trat_sin_na_2+ cum_dias_trat_sin_na_3+ cum_dias_trat_sin_na_4+ cum_dias_trat_sin_na_5+ cum_dias_trat_sin_na_6+ cum_dias_trat_sin_na_7+ cum_dias_trat_sin_na_8+ cum_dias_trat_sin_na_9+ cum_dias_trat_sin_na_10+ dias_treat_imp_sin_na_1+ dias_treat_imp_sin_na_2+ dias_treat_imp_sin_na_3+ dias_treat_imp_sin_na_4+ dias_treat_imp_sin_na_5+ dias_treat_imp_sin_na_6+ dias_treat_imp_sin_na_7+ dias_treat_imp_sin_na_8+ dias_treat_imp_sin_na_9+ dias_treat_imp_sin_na_10+ cum_diff_bet_treat_1+ cum_diff_bet_treat_2+ cum_diff_bet_treat_3+ cum_diff_bet_treat_4+ cum_diff_bet_treat_5+ cum_diff_bet_treat_6+ cum_diff_bet_treat_7+ cum_diff_bet_treat_8+ cum_diff_bet_treat_9+ cum_diff_bet_treat_10+ duplicates_filtered+ max_cum_dias_trat_sin_na+ max_cum_diff_bet_treat+ cnt_mod_cie_10_dg_cons_sus_or+ cnt_mod_cie_10_or+ dg_total_cie_10+ dias_treat_imp_sin_na_four+ diff_bet_treat_four+ cum_dias_trat_sin_na_four+ cum_diff_bet_treat_four+ mean_cum_dias_trat_sin_na_four+ mean_cum_diff_bet_treat_four+ comorbidity_icd_10+ n_treats+ sex_abuse+ dom_violence,
                                       method= c(sexo_2=3,
                                                 escolaridad_rec=3,
                                                 estado_conyugal_2=3,
                                                 compromiso_biopsicosocial=2,
                                                 edad_ini_cons=2,
                                                 edad_al_ing=2,
                                                 sus_ini_mod=3,
                                                 sus_ini_mod_mvv=3,
                                                 freq_cons_sus_prin=3,
                                                 via_adm_sus_prin_act=3,
                                                 con_quien_vive_joel=3,
                                                 numero_de_hijos_mod_joel=2,
                                                 condicion_ocupacional_corr=3,
                                                 cat_ocupacional_corr=3,
                                                 abandono_temprano=3,
                                                 dg_cie_10_rec=3,
                                                 dias_treat_imp_sin_na=2,
                                                 cnt_mod_cie_10_or=3,
                                                 cnt_diagnostico_trs_fisico=2,
                                                 cnt_otros_probl_at_sm_or=2,
                                                 tipo_de_plan_2_mod=3,
                                                 tenencia_de_la_vivienda_mod=2,
                                                 cum_dias_trat_sin_na_1= 2,
                                                 cum_dias_trat_sin_na_2= 2, 
                                                 cum_dias_trat_sin_na_3= 2, 
                                                 cum_dias_trat_sin_na_4= 2, 
                                                 cum_dias_trat_sin_na_5= 2, 
                                                 cum_dias_trat_sin_na_6= 2, 
                                                 cum_dias_trat_sin_na_7= 2, 
                                                 cum_dias_trat_sin_na_8= 2, 
                                                 cum_dias_trat_sin_na_9= 2, 
                                                 cum_dias_trat_sin_na_10=2, 
                                                 dias_treat_imp_sin_na_1= 2,
                                                 dias_treat_imp_sin_na_2= 2, 
                                                 dias_treat_imp_sin_na_3= 2, 
                                                 dias_treat_imp_sin_na_4= 2, 
                                                 dias_treat_imp_sin_na_5= 2, 
                                                 dias_treat_imp_sin_na_6= 2, 
                                                 dias_treat_imp_sin_na_7= 2, 
                                                 dias_treat_imp_sin_na_8= 2, 
                                                 dias_treat_imp_sin_na_9= 2, 
                                                 dias_treat_imp_sin_na_10=2, 
                                                 cum_diff_bet_treat_1= 2, 
                                                 cum_diff_bet_treat_2= 2, 
                                                 cum_diff_bet_treat_3= 2, 
                                                 cum_diff_bet_treat_4= 2, 
                                                 cum_diff_bet_treat_5= 2, 
                                                 cum_diff_bet_treat_6= 2, 
                                                 cum_diff_bet_treat_7= 2, 
                                                 cum_diff_bet_treat_8= 2, 
                                                 cum_diff_bet_treat_9= 2, 
                                                 cum_diff_bet_treat_10= 2,
                                                 duplicates_filtered= 3,
                                                 max_cum_dias_trat_sin_na= 2,
                                                 max_cum_diff_bet_treat= 2,
                                                 cnt_mod_cie_10_dg_cons_sus_or= 2,
                                                 dg_total_cie_10 = 3,
                                                 comorbidity_icd_10 = 3,
                                                 dias_treat_imp_sin_na_four = 2,
                                                 diff_bet_treat_four = 2,
                                                 cum_dias_trat_sin_na_four = 2,
                                                 cum_diff_bet_treat_four = 2,
                                                 mean_cum_dias_trat_sin_na_four = 2,
                                                 mean_cum_diff_bet_treat_four = 2,
                                                 n_treats = 3,
                                                 sex_abuse = 3,
                                                 dom_violence = 3
                                                 ),
                                       data = prueba2,
                                       include.miss = T,
                                       var.equal=T
                                       
)#cie_10 cat_ocupacional estatus_ocupacional

pvals <- getResults(table3)
#p.adjust(pvals, method = "BH")
restab3 <- createTable(table3,show.p.overall = F)
compareGroups::export2md(restab3, size=9, first.strip=T, hide.no="no", position="center",col.names=c("Variables","Total"),
                         format="html",caption= "Table 1. Summary descriptives table")%>%
  kableExtra::add_footnote(c("Note. Variables of C1 dataset had to be standardized before comparison;", "Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;", "Categorical variables are presented as number (%)"), notation = "none")%>%
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 1. Summary descriptives table
Variables Total
N=23979
Sexo Usuario/Sex of User:
Men 19850 (82.8%)
Women 4129 (17.2%)
escolaridad_rec:
3-Completed primary school or less 5634 (23.5%)
2-Completed high school or less 14518 (60.5%)
1-More than high school 3751 (15.6%)
‘Missing’ 76 (0.32%)
Estado Conyugal/Marital Status:
Married/Shared living arrangements 5127 (21.4%)
Separated/Divorced 501 (2.09%)
Single 18294 (76.3%)
Widower 30 (0.13%)
‘Missing’ 27 (0.11%)
Biopsychosocial Compromise:
1-Mild 2240 (9.34%)
2-Moderate 13949 (58.2%)
3-Severe 7352 (30.7%)
‘Missing’ 438 (1.83%)
Edad de Inicio de Consumo/Age of Onset of Drug Use 15.0 [13.0;16.0]
Edad a la Fecha de Ingreso a Tratamiento (numérico continuo) (Primera Entrada)/Age at Admission to Treatment (First Entry) 25.4 [22.6;27.7]
Sustancia de Inicio (Sólo más frecuentes)/Starting Substance (Only more frequent):
Alcohol 10341 (43.1%)
Cocaína 934 (3.90%)
Marihuana 9165 (38.2%)
Otros 411 (1.71%)
Pasta Base 1054 (4.40%)
‘Missing’ 2074 (8.65%)
Starting Substance:
Alcohol 10182 (42.5%)
Cocaine hydrochloride 970 (4.05%)
Marijuana 9255 (38.6%)
Other 398 (1.66%)
Cocaine paste 1100 (4.59%)
‘Missing’ 2074 (8.65%)
Frequency of drug use in the primary substance:
1 day a week or more 1616 (6.74%)
2 to 3 days a week 7059 (29.4%)
4 to 6 days a week 4105 (17.1%)
Daily 9911 (41.3%)
Less than 1 day a week 1166 (4.86%)
‘Missing’ 122 (0.51%)
Vía de Administración de la Sustancia Principal (Se aplicaron criterios de limpieza)(f)/Route of Administration of the Primary or Main Substance (Tidy)(f):
Smoked or Pulmonary Aspiration 13902 (58.0%)
Intranasal (powder aspiration) 5575 (23.2%)
Injected Intravenously or Intramuscularly 16 (0.07%)
Oral (drunk or eaten) 4470 (18.6%)
Other 12 (0.05%)
‘Missing’ 4 (0.02%)
Whom you live with(cohabitation status) (Recoded) (f):
Alone 1335 (5.57%)
Family of origin 15455 (64.5%)
With couple/children 7189 (30.0%)
Number of Children (Max. Value), adding 1 if pregnant at admission 1.00 [0.00;1.00]
Occupational Status Corrected(f):
Employed 10423 (43.5%)
Inactive 2019 (8.42%)
Looking for a job for the first time 97 (0.40%)
No activity 1333 (5.56%)
Not seeking for work 216 (0.90%)
Unemployed 9891 (41.2%)
Occupational Category Corrected(f):
Employer 322 (1.34%)
Other 222 (0.93%)
Salaried 7039 (29.4%)
Self-employed 1934 (8.07%)
Unpaid family labour 65 (0.27%)
Volunteer worker 39 (0.16%)
‘Missing’ 14358 (59.9%)
Abandono temprano(<3 meses)/ Early Drop-out(<3 months):
Mayor o igual a 90 días 17383 (72.5%)
Menos de 90 días 6596 (27.5%)
Diagnóstico CIE-10 (1 o más)(Recodificado)/Psychiatric Diagnoses (ICD-10)(one or more)(Recoded):
Without psychiatric comorbidity 9259 (38.6%)
Diagnosis unknown (under study) 5198 (21.7%)
With psychiatric comorbidity 9522 (39.7%)
Días de Tratamiento (valores perdidos en la fecha de egreso se reemplazaron por la diferencia con 2019-11-13)/Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13) 147 [84.0;254]
Recuento de Diagnóstico de Trastorno Físico/Count of Physical Disorder 0.00 [0.00;0.00]
Recuento de Otros Problemas de Atención Vinculados a Salud Mental/Count of Other problems linked to Mental Health 0.00 [0.00;1.00]
Type of Plan (Independently of the Program):
PAB 9277 (38.7%)
PAI 11390 (47.5%)
PR 3286 (13.7%)
‘Missing’ 26 (0.11%)
Tenure status of households:
Illegal Settlement 191 (0.80%)
Others 562 (2.34%)
Owner/Transferred dwellings/Pays Dividends 7502 (31.3%)
Renting 3872 (16.1%)
Stays temporarily with a relative 10646 (44.4%)
‘Missing’ 1206 (5.03%)
Cum. Days of Treatment (1st Treatment) 147 [84.0;254]
Cum. Days of Treatment (2nd Treatment) 318 [207;489]
Cum. Days of Treatment (3rd Treatment) 485 [335;704]
Cum. Days of Treatment (4th Treatment) 638 [433;897]
Cum. Days of Treatment (5th Treatment) 805 [550;1041]
Cum. Days of Treatment (6th Treatment) 944 [716;1152]
Cum. Days of Treatment (7th Treatment) 1076 [888;1279]
Cum. Days of Treatment (8th Treatment) 1192 [1152;1232]
Cum. Days of Treatment (9th Treatment) 1403 [1403;1403]
Cum. Days of Treatment (10th Treatment) 1622 [1622;1622]
Days of Treatment (1st Treatment) 147 [84.0;254]
Days of Treatment (2nd Treatment) 137 [77.0;239]
Days of Treatment (3rd Treatment) 134 [73.0;237]
Days of Treatment (4th Treatment) 126 [70.0;224]
Days of Treatment (5th Treatment) 143 [67.0;260]
Days of Treatment (6th Treatment) 145 [76.8;199]
Days of Treatment (7th Treatment) 120 [23.0;175]
Days of Treatment (8th Treatment) 40.0 [29.5;84.5]
Days of Treatment (9th Treatment) 211 [211;211]
Days of Treatment (10th Treatment) 219 [219;219]
Cum. Diff Between Treatments (1st Treatment) 0.00 [0.00;0.00]
Cum. Diff Between Treatments (2nd Treatment) 801 [405;1390]
Cum. Diff Between Treatments (3rd Treatment) 1155 [686;1680]
Cum. Diff Between Treatments (4th Treatment) 1341 [886;1972]
Cum. Diff Between Treatments (5th Treatment) 1458 [1047;1970]
Cum. Diff Between Treatments (6th Treatment) 1509 [1160;2294]
Cum. Diff Between Treatments (7th Treatment) 1188 [1184;1422]
Cum. Diff Between Treatments (8th Treatment) 1706 [1706;1706]
Cum. Diff Between Treatments (9th Treatment) 1944 [1944;1944]
Cum. Diff Between Treatments (10th Treatment) .
Número de Tratamientos por HASH (Total)/Number of Treatments by User (Total):
1 18472 (77.0%)
2 3908 (16.3%)
3 1087 (4.53%)
4 347 (1.45%)
5 111 (0.46%)
6 37 (0.15%)
7 14 (0.06%)
8 2 (0.01%)
10 1 (0.00%)
Max. Cumulative Days of Treatment 182 [98.0;332]
Max. Cumulative Difference Between Treatments 0.00 [0.00;0.00]
Total count of Psychiatric & Drug dependence Diagnostics 1.00 [1.00;2.00]
cnt_mod_cie_10_or:
0 9259 (38.6%)
1 14314 (59.7%)
2 365 (1.52%)
3 41 (0.17%)
Conteo de Diagnósticos CIE-10(sólo diagnósticos)/Count of ICD-10 Diagnostics(only diagnoses):
0 14457 (60.3%)
1 9116 (38.0%)
2 365 (1.52%)
3 41 (0.17%)
Days of Treatment (Fourth or those that follow) 138 [78.8;227]
Days of Difference Between Treatments (Fifth treatment or those that folow) 277 [140;485]
Cumulative Days of Treatment (Fourth or those that follow) 692 [460;936]
Cumulative Difference Between Treatments (Fifth or those that follow) 1412 [1031;1995]
Average Cumulative Days of Treatment (Fourth or those that follow) 163 [110;221]
Average Cumulative Difference Between Treatments (Fifth or those that follow) 340 [236;488]
Comorbidity ICD-10 (with amount of different diagnosis):
Without psychiatric comorbidity 9259 (38.6%)
Diagnosis unknown (under study) 5198 (21.7%)
One 9116 (38.0%)
Two or more 406 (1.69%)
No. of treatments with 18+ at admission between 2010 and 2019:
01 18475 (77.0%)
02 3907 (16.3%)
03 1085 (4.52%)
04 or more 512 (2.14%)
Sexual abuse:
No sexual abuse 18880 (78.7%)
Sexual abuse 289 (1.21%)
‘Missing’ 4810 (20.1%)
Domestic violence:
No domestic violence 14304 (59.7%)
Domestic violence 4865 (20.3%)
‘Missing’ 4810 (20.1%)
Note. Variables of C1 dataset had to be standardized before comparison;
Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;
Categorical variables are presented as number (%)


Primary substance

table4 <- compareGroups::compareGroups(sus_principal_mod ~ sexo_2+ escolaridad_rec+ compromiso_biopsicosocial+ estado_conyugal_2+ edad_ini_cons+ edad_al_ing+ sus_ini_mod+ sus_ini_mod_mvv+ freq_cons_sus_prin+ via_adm_sus_prin_act+ con_quien_vive_joel+ numero_de_hijos_mod_joel+ condicion_ocupacional_corr+ cat_ocupacional_corr+ abandono_temprano+ dg_cie_10_rec+ dias_treat_imp_sin_na+ cnt_diagnostico_trs_fisico+ cnt_otros_probl_at_sm_or+ tipo_de_plan_2_mod+ tenencia_de_la_vivienda_mod+ cum_dias_trat_sin_na_1+ cum_dias_trat_sin_na_2+ cum_dias_trat_sin_na_3+ cum_dias_trat_sin_na_4+ cum_dias_trat_sin_na_5+ cum_dias_trat_sin_na_6+ cum_dias_trat_sin_na_7+ cum_dias_trat_sin_na_8+ cum_dias_trat_sin_na_9+ cum_dias_trat_sin_na_10+ dias_treat_imp_sin_na_1+ dias_treat_imp_sin_na_2+ dias_treat_imp_sin_na_3+ dias_treat_imp_sin_na_4+ dias_treat_imp_sin_na_5+ dias_treat_imp_sin_na_6+ dias_treat_imp_sin_na_7+ dias_treat_imp_sin_na_8+ dias_treat_imp_sin_na_9+ dias_treat_imp_sin_na_10+ cum_diff_bet_treat_1+ cum_diff_bet_treat_2+ cum_diff_bet_treat_3+ cum_diff_bet_treat_4+ cum_diff_bet_treat_5+ cum_diff_bet_treat_6+ cum_diff_bet_treat_7+ cum_diff_bet_treat_8+ cum_diff_bet_treat_9+ cum_diff_bet_treat_10+ duplicates_filtered+ max_cum_dias_trat_sin_na+ max_cum_diff_bet_treat+ cnt_mod_cie_10_dg_cons_sus_or+ cnt_mod_cie_10_or+ dg_total_cie_10+ dias_treat_imp_sin_na_four+ diff_bet_treat_four+ cum_dias_trat_sin_na_four+ cum_diff_bet_treat_four+ mean_cum_dias_trat_sin_na_four+ mean_cum_diff_bet_treat_four+ comorbidity_icd_10+ n_treats+ sex_abuse+ dom_violence,
                                       method= c(
                                                 sexo_2=3,
                                                 escolaridad_rec=3,
                                                 compromiso_biopsicosocial=2,
                                                 estado_conyugal_2=3,
                                                 edad_ini_cons=2,
                                                 edad_al_ing=2,
                                                 sus_ini_mod=3,
                                                 sus_ini_mod_mvv=3,
                                                 freq_cons_sus_prin=3,
                                                 via_adm_sus_prin_act=3,
                                                 con_quien_vive_joel=3,
                                                 numero_de_hijos_mod_joel=2,
                                                 condicion_ocupacional_corr=3,
                                                 cat_ocupacional_corr=3,
                                                 abandono_temprano=3,
                                                 dg_cie_10_rec=3,
                                                 dias_treat_imp_sin_na=2,
                                                 cnt_mod_cie_10_or=3,
                                                 cnt_diagnostico_trs_fisico=2,
                                                 cnt_otros_probl_at_sm_or=2,
                                                 tipo_de_plan_2_mod=3,
                                                 tenencia_de_la_vivienda_mod=2,
                                                 cum_dias_trat_sin_na_1= 2,
                                                 cum_dias_trat_sin_na_2= 2, 
                                                 cum_dias_trat_sin_na_3= 2, 
                                                 cum_dias_trat_sin_na_4= 2, 
                                                 cum_dias_trat_sin_na_5= 2, 
                                                 cum_dias_trat_sin_na_6= 2, 
                                                 cum_dias_trat_sin_na_7= 2, 
                                                 cum_dias_trat_sin_na_8= 2, 
                                                 cum_dias_trat_sin_na_9= 2, 
                                                 cum_dias_trat_sin_na_10=2, 
                                                 dias_treat_imp_sin_na_1= 2,
                                                 dias_treat_imp_sin_na_2= 2, 
                                                 dias_treat_imp_sin_na_3= 2, 
                                                 dias_treat_imp_sin_na_4= 2, 
                                                 dias_treat_imp_sin_na_5= 2, 
                                                 dias_treat_imp_sin_na_6= 2, 
                                                 dias_treat_imp_sin_na_7= 2, 
                                                 dias_treat_imp_sin_na_8= 2, 
                                                 dias_treat_imp_sin_na_9= 2, 
                                                 dias_treat_imp_sin_na_10=2,                                                  
                                                 cum_diff_bet_treat_1= 2, 
                                                 cum_diff_bet_treat_2= 2, 
                                                 cum_diff_bet_treat_3= 2, 
                                                 cum_diff_bet_treat_4= 2, 
                                                 cum_diff_bet_treat_5= 2, 
                                                 cum_diff_bet_treat_6= 2, 
                                                 cum_diff_bet_treat_7= 2, 
                                                 cum_diff_bet_treat_8= 2, 
                                                 cum_diff_bet_treat_9= 2, 
                                                 cum_diff_bet_treat_10= 2,
                                                 duplicates_filtered=3,
                                                 max_cum_dias_trat_sin_na= 2,
                                                 max_cum_diff_bet_treat= 2,
                                                 cnt_mod_cie_10_dg_cons_sus_or= 3,
                                                 dg_total_cie_10 = 3,
                                                 comorbidity_icd_10 = 3,
                                                 dias_treat_imp_sin_na_four = 2,
                                                 diff_bet_treat_four = 2,
                                                 cum_dias_trat_sin_na_four = 2,
                                                 cum_diff_bet_treat_four = 2,
                                                 mean_cum_dias_trat_sin_na_four = 2,
                                                 mean_cum_diff_bet_treat_four = 2,
                                                 n_treats = 3,
                                                 sex_abuse = 3,
                                                 dom_violence= 3
                                       ),
                                       data = prueba2,
                                       include.miss = T,
                                       var.equal=T
)#cie_10 cat_ocupacional estatus_ocupacional

pvals <- getResults(table4)
#p.adjust(pvals, method = "BH")
restab4 <- createTable(table4, show.p.overall = T)
compareGroups::export2md(restab4, size=9, first.strip=T, hide.no="no", position="center",
                         format="html",caption= "Table 2. Summary descriptives table by Primary Substance at Admission")%>%
  kableExtra::add_footnote(c("Note. Variables of C1 dataset had to be standardized before comparison;", "Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;", "Categorical variables are presented as number (%)"), notation = "none")%>%
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 2. Summary descriptives table by Primary Substance at Admission
Alcohol Cocaine hydrochloride Marijuana Other Cocaine paste p.overall
N=4142 N=5394 N=2872 N=295 N=11276
Sexo Usuario/Sex of User: <0.001
Men 3321 (80.2%) 4474 (82.9%) 2356 (82.0%) 223 (75.6%) 9476 (84.0%)
Women 821 (19.8%) 920 (17.1%) 516 (18.0%) 72 (24.4%) 1800 (16.0%)
escolaridad_rec: .
3-Completed primary school or less 900 (21.7%) 873 (16.2%) 536 (18.7%) 62 (21.0%) 3263 (28.9%)
2-Completed high school or less 2376 (57.4%) 3441 (63.8%) 1714 (59.7%) 152 (51.5%) 6835 (60.6%)
1-More than high school 852 (20.6%) 1063 (19.7%) 614 (21.4%) 79 (26.8%) 1143 (10.1%)
‘Missing’ 14 (0.34%) 17 (0.32%) 8 (0.28%) 2 (0.68%) 35 (0.31%)
Biopsychosocial Compromise: <0.001
1-Mild 647 (15.6%) 538 (9.97%) 442 (15.4%) 19 (6.44%) 594 (5.27%)
2-Moderate 2601 (62.8%) 3339 (61.9%) 1795 (62.5%) 156 (52.9%) 6058 (53.7%)
3-Severe 814 (19.7%) 1406 (26.1%) 560 (19.5%) 115 (39.0%) 4457 (39.5%)
‘Missing’ 80 (1.93%) 111 (2.06%) 75 (2.61%) 5 (1.69%) 167 (1.48%)
Estado Conyugal/Marital Status: .
Married/Shared living arrangements 1019 (24.6%) 1350 (25.0%) 465 (16.2%) 36 (12.2%) 2257 (20.0%)
Separated/Divorced 108 (2.61%) 128 (2.37%) 48 (1.67%) 8 (2.71%) 209 (1.85%)
Single 3005 (72.5%) 3908 (72.5%) 2352 (81.9%) 250 (84.7%) 8779 (77.9%)
Widower 5 (0.12%) 3 (0.06%) 4 (0.14%) 1 (0.34%) 17 (0.15%)
‘Missing’ 5 (0.12%) 5 (0.09%) 3 (0.10%) 0 (0.00%) 14 (0.12%)
Edad de Inicio de Consumo/Age of Onset of Drug Use 15.0 [13.0;16.0] 15.0 [13.0;16.0] 15.0 [13.0;16.0] 14.0 [13.0;16.0] 14.0 [13.0;16.0] <0.001
Edad a la Fecha de Ingreso a Tratamiento (numérico continuo) (Primera Entrada)/Age at Admission to Treatment (First Entry) 25.7 [23.1;27.9] 25.6 [23.0;27.8] 23.6 [21.2;26.5] 24.3 [21.1;27.8] 25.5 [22.8;27.8] <0.001
Sustancia de Inicio (Sólo más frecuentes)/Starting Substance (Only more frequent): 0.000
Alcohol 3256 (78.6%) 2366 (43.9%) 982 (34.2%) 79 (26.8%) 3658 (32.4%)
Cocaína 44 (1.06%) 604 (11.2%) 22 (0.77%) 4 (1.36%) 260 (2.31%)
Marihuana 513 (12.4%) 1913 (35.5%) 1490 (51.9%) 121 (41.0%) 5128 (45.5%)
Otros 43 (1.04%) 65 (1.21%) 41 (1.43%) 65 (22.0%) 197 (1.75%)
Pasta Base 28 (0.68%) 57 (1.06%) 32 (1.11%) 2 (0.68%) 935 (8.29%)
‘Missing’ 258 (6.23%) 389 (7.21%) 305 (10.6%) 24 (8.14%) 1098 (9.74%)
Starting Substance: .
Alcohol 3249 (78.4%) 2335 (43.3%) 973 (33.9%) 79 (26.8%) 3546 (31.4%)
Cocaine hydrochloride 45 (1.09%) 627 (11.6%) 24 (0.84%) 4 (1.36%) 270 (2.39%)
Marijuana 520 (12.6%) 1922 (35.6%) 1498 (52.2%) 121 (41.0%) 5194 (46.1%)
Other 41 (0.99%) 63 (1.17%) 39 (1.36%) 65 (22.0%) 190 (1.68%)
Cocaine paste 29 (0.70%) 58 (1.08%) 33 (1.15%) 2 (0.68%) 978 (8.67%)
‘Missing’ 258 (6.23%) 389 (7.21%) 305 (10.6%) 24 (8.14%) 1098 (9.74%)
Frequency of drug use in the primary substance: .
1 day a week or more 384 (9.27%) 502 (9.31%) 125 (4.35%) 14 (4.75%) 591 (5.24%)
2 to 3 days a week 1817 (43.9%) 1934 (35.9%) 639 (22.2%) 52 (17.6%) 2617 (23.2%)
4 to 6 days a week 758 (18.3%) 941 (17.4%) 416 (14.5%) 42 (14.2%) 1948 (17.3%)
Daily 964 (23.3%) 1630 (30.2%) 1579 (55.0%) 166 (56.3%) 5572 (49.4%)
Less than 1 day a week 196 (4.73%) 354 (6.56%) 99 (3.45%) 19 (6.44%) 498 (4.42%)
‘Missing’ 23 (0.56%) 33 (0.61%) 14 (0.49%) 2 (0.68%) 50 (0.44%)
Vía de Administración de la Sustancia Principal (Se aplicaron criterios de limpieza)(f)/Route of Administration of the Primary or Main Substance (Tidy)(f): .
Smoked or Pulmonary Aspiration 0 (0.00%) 0 (0.00%) 2872 (100%) 34 (11.5%) 10996 (97.5%)
Intranasal (powder aspiration) 0 (0.00%) 5394 (100%) 0 (0.00%) 14 (4.75%) 167 (1.48%)
Injected Intravenously or Intramuscularly 0 (0.00%) 0 (0.00%) 0 (0.00%) 16 (5.42%) 0 (0.00%)
Oral (drunk or eaten) 4142 (100%) 0 (0.00%) 0 (0.00%) 225 (76.3%) 103 (0.91%)
Other 0 (0.00%) 0 (0.00%) 0 (0.00%) 5 (1.69%) 7 (0.06%)
‘Missing’ 0 (0.00%) 0 (0.00%) 0 (0.00%) 1 (0.34%) 3 (0.03%)
Whom you live with(cohabitation status) (Recoded) (f): <0.001
Alone 311 (7.51%) 199 (3.69%) 148 (5.15%) 16 (5.42%) 661 (5.86%)
Family of origin 2434 (58.8%) 3384 (62.7%) 2031 (70.7%) 222 (75.3%) 7384 (65.5%)
With couple/children 1397 (33.7%) 1811 (33.6%) 693 (24.1%) 57 (19.3%) 3231 (28.7%)
Number of Children (Max. Value), adding 1 if pregnant at admission 1.00 [0.00;1.00] 1.00 [0.00;1.00] 0.00 [0.00;1.00] 0.00 [0.00;1.00] 1.00 [0.00;2.00] <0.001
Occupational Status Corrected(f): .
Employed 2371 (57.2%) 2833 (52.5%) 1284 (44.7%) 96 (32.5%) 3839 (34.0%)
Inactive 410 (9.90%) 368 (6.82%) 418 (14.6%) 43 (14.6%) 780 (6.92%)
Looking for a job for the first time 16 (0.39%) 14 (0.26%) 22 (0.77%) 2 (0.68%) 43 (0.38%)
No activity 163 (3.94%) 226 (4.19%) 200 (6.96%) 27 (9.15%) 717 (6.36%)
Not seeking for work 20 (0.48%) 48 (0.89%) 24 (0.84%) 5 (1.69%) 119 (1.06%)
Unemployed 1162 (28.1%) 1905 (35.3%) 924 (32.2%) 122 (41.4%) 5778 (51.2%)
Occupational Category Corrected(f): .
Employer 88 (2.12%) 83 (1.54%) 34 (1.18%) 2 (0.68%) 115 (1.02%)
Other 48 (1.16%) 46 (0.85%) 30 (1.04%) 4 (1.36%) 94 (0.83%)
Salaried 1618 (39.1%) 1929 (35.8%) 878 (30.6%) 50 (16.9%) 2564 (22.7%)
Self-employed 399 (9.63%) 559 (10.4%) 217 (7.56%) 23 (7.80%) 736 (6.53%)
Unpaid family labour 5 (0.12%) 16 (0.30%) 11 (0.38%) 0 (0.00%) 33 (0.29%)
Volunteer worker 7 (0.17%) 8 (0.15%) 9 (0.31%) 1 (0.34%) 14 (0.12%)
‘Missing’ 1977 (47.7%) 2753 (51.0%) 1693 (58.9%) 215 (72.9%) 7720 (68.5%)
Abandono temprano(<3 meses)/ Early Drop-out(<3 months): <0.001
Mayor o igual a 90 días 3214 (77.6%) 3998 (74.1%) 2184 (76.0%) 221 (74.9%) 7766 (68.9%)
Menos de 90 días 928 (22.4%) 1396 (25.9%) 688 (24.0%) 74 (25.1%) 3510 (31.1%)
Diagnóstico CIE-10 (1 o más)(Recodificado)/Psychiatric Diagnoses (ICD-10)(one or more)(Recoded): <0.001
Without psychiatric comorbidity 1652 (39.9%) 2156 (40.0%) 1100 (38.3%) 75 (25.4%) 4276 (37.9%)
Diagnosis unknown (under study) 720 (17.4%) 1141 (21.2%) 568 (19.8%) 59 (20.0%) 2710 (24.0%)
With psychiatric comorbidity 1770 (42.7%) 2097 (38.9%) 1204 (41.9%) 161 (54.6%) 4290 (38.0%)
Días de Tratamiento (valores perdidos en la fecha de egreso se reemplazaron por la diferencia con 2019-11-13)/Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13) 165 [96.0;281] 150 [87.0;251] 157 [91.0;274] 161 [89.0;298] 135 [75.8;240] <0.001
Recuento de Diagnóstico de Trastorno Físico/Count of Physical Disorder 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.005
Recuento de Otros Problemas de Atención Vinculados a Salud Mental/Count of Other problems linked to Mental Health 0.00 [0.00;1.00] 0.00 [0.00;1.00] 0.00 [0.00;1.00] 0.00 [0.00;1.00] 0.00 [0.00;1.00] <0.001
Type of Plan (Independently of the Program): .
PAB 2120 (51.2%) 2321 (43.0%) 1380 (48.1%) 88 (29.8%) 3368 (29.9%)
PAI 1811 (43.7%) 2605 (48.3%) 1331 (46.3%) 175 (59.3%) 5468 (48.5%)
PR 208 (5.02%) 465 (8.62%) 157 (5.47%) 31 (10.5%) 2425 (21.5%)
‘Missing’ 3 (0.07%) 3 (0.06%) 4 (0.14%) 1 (0.34%) 15 (0.13%)
Tenure status of households: .
Illegal Settlement 18 (0.43%) 22 (0.41%) 22 (0.77%) 3 (1.02%) 126 (1.12%)
Others 88 (2.12%) 109 (2.02%) 90 (3.13%) 4 (1.36%) 271 (2.40%)
Owner/Transferred dwellings/Pays Dividends 1403 (33.9%) 1454 (27.0%) 945 (32.9%) 90 (30.5%) 3610 (32.0%)
Renting 821 (19.8%) 941 (17.4%) 445 (15.5%) 53 (18.0%) 1612 (14.3%)
Stays temporarily with a relative 1648 (39.8%) 2647 (49.1%) 1216 (42.3%) 135 (45.8%) 5000 (44.3%)
‘Missing’ 164 (3.96%) 221 (4.10%) 154 (5.36%) 10 (3.39%) 657 (5.83%)
Cum. Days of Treatment (1st Treatment) 165 [96.0;281] 150 [87.2;251] 157 [91.0;274] 161 [89.0;300] 135 [76.0;240] <0.001
Cum. Days of Treatment (2nd Treatment) 367 [235;577] 320 [212;491] 351 [239;520] 402 [288;592] 305 [196;468] <0.001
Cum. Days of Treatment (3rd Treatment) 550 [383;784] 483 [344;704] 500 [384;676] 600 [411;820] 473 [320;690] 0.013
Cum. Days of Treatment (4th Treatment) 810 [573;994] 659 [378;840] 595 [437;780] 913 [766;1162] 608 [432;874] 0.013
Cum. Days of Treatment (5th Treatment) 597 [430;972] 854 [506;1000] 866 [776;925] 2000 [2000;2000] 786 [554;1067] 0.364
Cum. Days of Treatment (6th Treatment) 1022 [669;1087] 803 [727;1080] 984 [940;995] 2230 [2230;2230] 949 [714;1175] 0.471
Cum. Days of Treatment (7th Treatment) 876 [778;975] 887 [581;957] 1348 [1348;1348] . [.;.] 1178 [1029;1300] 0.101
Cum. Days of Treatment (8th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 1192 [1152;1232] .
Cum. Days of Treatment (9th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 1403 [1403;1403] .
Cum. Days of Treatment (10th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 1622 [1622;1622] .
Days of Treatment (1st Treatment) 165 [96.0;281] 150 [87.0;251] 157 [91.0;274] 161 [89.0;298] 135 [75.8;240] <0.001
Days of Treatment (2nd Treatment) 160 [92.0;278] 139 [79.0;241] 147 [80.2;262] 215 [106;307] 129 [72.0;224] <0.001
Days of Treatment (3rd Treatment) 135 [81.0;232] 140 [75.0;223] 138 [73.2;251] 108 [33.0;189] 133 [72.0;239] 0.881
Days of Treatment (4th Treatment) 152 [70.2;336] 124 [61.5;217] 128 [80.5;206] 75.0 [46.5;195] 124 [70.2;215] 0.505
Days of Treatment (5th Treatment) 182 [76.8;232] 151 [74.5;226] 330 [213;388] 588 [588;588] 141 [63.0;254] 0.268
Days of Treatment (6th Treatment) 67.0 [64.0;175] 85.0 [36.5;180] 144 [130;195] 230 [230;230] 154 [106;198] 0.373
Days of Treatment (7th Treatment) 31.0 [20.5;41.5] 175 [99.0;215] 364 [364;364] . [.;.] 120 [28.0;146] 0.223
Days of Treatment (8th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 40.0 [29.5;84.5] .
Days of Treatment (9th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 211 [211;211] .
Days of Treatment (10th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 219 [219;219] .
Cum. Diff Between Treatments (1st Treatment) 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;74.0] <0.001
Cum. Diff Between Treatments (2nd Treatment) 812 [413;1310] 799 [405;1420] 950 [533;1661] 947 [733;1511] 774 [392;1366] 0.120
Cum. Diff Between Treatments (3rd Treatment) 1075 [645;1639] 1221 [794;1732] 1209 [696;1821] 1274 [858;1290] 1146 [680;1667] 0.867
Cum. Diff Between Treatments (4th Treatment) 1424 [1098;2117] 1141 [888;2078] 1428 [1187;1640] 1398 [1398;1398] 1384 [880;1978] 0.971
Cum. Diff Between Treatments (5th Treatment) 1695 [960;1700] 1497 [1072;2131] 1446 [1432;1668] 1415 [1415;1415] 1470 [1062;1992] 0.992
Cum. Diff Between Treatments (6th Treatment) 1628 [1295;1961] 2411 [1953;2594] 1827 [1827;1827] . [.;.] 1287 [1088;1906] 0.452
Cum. Diff Between Treatments (7th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 1188 [1184;1422] .
Cum. Diff Between Treatments (8th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 1706 [1706;1706] .
Cum. Diff Between Treatments (9th Treatment) . [.;.] . [.;.] . [.;.] . [.;.] 1944 [1944;1944] .
Cum. Diff Between Treatments (10th Treatment) . . . . . .
Número de Tratamientos por HASH (Total)/Number of Treatments by User (Total): .
1 3435 (82.9%) 4367 (81.0%) 2422 (84.3%) 246 (83.4%) 8002 (71.0%)
2 537 (13.0%) 781 (14.5%) 328 (11.4%) 35 (11.9%) 2227 (19.7%)
3 116 (2.80%) 176 (3.26%) 91 (3.17%) 11 (3.73%) 693 (6.15%)
4 46 (1.11%) 43 (0.80%) 25 (0.87%) 2 (0.68%) 231 (2.05%)
5 3 (0.07%) 17 (0.32%) 3 (0.10%) 0 (0.00%) 88 (0.78%)
6 3 (0.07%) 7 (0.13%) 2 (0.07%) 1 (0.34%) 24 (0.21%)
7 2 (0.05%) 3 (0.06%) 1 (0.03%) 0 (0.00%) 8 (0.07%)
8 0 (0.00%) 0 (0.00%) 0 (0.00%) 0 (0.00%) 2 (0.02%)
10 0 (0.00%) 0 (0.00%) 0 (0.00%) 0 (0.00%) 1 (0.01%)
Max. Cumulative Days of Treatment 194 [106;350] 181 [99.0;313] 182 [98.0;331] 200 [95.0;337] 179 [93.0;336] <0.001
Max. Cumulative Difference Between Treatments 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;123] <0.001
Total count of Psychiatric & Drug dependence Diagnostics: .
0 1108 (26.8%) 954 (17.7%) 719 (25.0%) 33 (11.2%) 1450 (12.9%)
1 1965 (47.4%) 2914 (54.0%) 1377 (47.9%) 138 (46.8%) 6303 (55.9%)
2 1003 (24.2%) 1438 (26.7%) 745 (25.9%) 115 (39.0%) 3376 (29.9%)
3 60 (1.45%) 75 (1.39%) 28 (0.97%) 9 (3.05%) 137 (1.21%)
4 6 (0.14%) 13 (0.24%) 3 (0.10%) 0 (0.00%) 10 (0.09%)
cnt_mod_cie_10_or: .
0 1652 (39.9%) 2156 (40.0%) 1100 (38.3%) 75 (25.4%) 4276 (37.9%)
1 2405 (58.1%) 3139 (58.2%) 1729 (60.2%) 207 (70.2%) 6834 (60.6%)
2 74 (1.79%) 84 (1.56%) 38 (1.32%) 13 (4.41%) 156 (1.38%)
3 11 (0.27%) 15 (0.28%) 5 (0.17%) 0 (0.00%) 10 (0.09%)
Conteo de Diagnósticos CIE-10(sólo diagnósticos)/Count of ICD-10 Diagnostics(only diagnoses): .
0 2372 (57.3%) 3297 (61.1%) 1668 (58.1%) 134 (45.4%) 6986 (62.0%)
1 1685 (40.7%) 1998 (37.0%) 1161 (40.4%) 148 (50.2%) 4124 (36.6%)
2 74 (1.79%) 84 (1.56%) 38 (1.32%) 13 (4.41%) 156 (1.38%)
3 11 (0.27%) 15 (0.28%) 5 (0.17%) 0 (0.00%) 10 (0.09%)
Days of Treatment (Fourth or those that follow) 149 [74.8;322] 138 [74.0;218] 144 [99.0;218] 75.0 [46.5;226] 136 [79.1;223] 0.719
Days of Difference Between Treatments (Fifth treatment or those that folow) 308 [170;552] 340 [171;558] 330 [138;365] 70.5 [70.5;70.5] 256 [132;483] 0.695
Cumulative Days of Treatment (Fourth or those that follow) 831 [585;1012] 676 [412;902] 683 [499;809] 913 [766;1397] 684 [449;929] 0.047
Cumulative Difference Between Treatments (Fifth or those that follow) 1694 [1127;2134] 1370 [1032;2078] 1520 [1385;1640] 1406 [1406;1406] 1411 [1018;2019] 0.940
Average Cumulative Days of Treatment (Fourth or those that follow) 202 [144;248] 161 [98.3;201] 149 [125;192] 228 [191;302] 159 [108;217] 0.014
Average Cumulative Difference Between Treatments (Fifth or those that follow) 356 [272;518] 295 [241;493] 347 [296;407] 316 [316;316] 341 [228;483] 0.990
Comorbidity ICD-10 (with amount of different diagnosis): .
Without psychiatric comorbidity 1652 (39.9%) 2156 (40.0%) 1100 (38.3%) 75 (25.4%) 4276 (37.9%)
Diagnosis unknown (under study) 720 (17.4%) 1141 (21.2%) 568 (19.8%) 59 (20.0%) 2710 (24.0%)
One 1685 (40.7%) 1998 (37.0%) 1161 (40.4%) 148 (50.2%) 4124 (36.6%)
Two or more 85 (2.05%) 99 (1.84%) 43 (1.50%) 13 (4.41%) 166 (1.47%)
No. of treatments with 18+ at admission between 2010 and 2019: <0.001
01 3435 (82.9%) 4368 (81.0%) 2422 (84.3%) 246 (83.4%) 8004 (71.0%)
02 537 (13.0%) 781 (14.5%) 328 (11.4%) 36 (12.2%) 2225 (19.7%)
03 116 (2.80%) 175 (3.24%) 91 (3.17%) 10 (3.39%) 693 (6.15%)
04 or more 54 (1.30%) 70 (1.30%) 31 (1.08%) 3 (1.02%) 354 (3.14%)
Sexual abuse: .
No sexual abuse 3147 (76.0%) 4124 (76.5%) 2263 (78.8%) 227 (76.9%) 9119 (80.9%)
Sexual abuse 60 (1.45%) 65 (1.21%) 23 (0.80%) 6 (2.03%) 135 (1.20%)
‘Missing’ 935 (22.6%) 1205 (22.3%) 586 (20.4%) 62 (21.0%) 2022 (17.9%)
Domestic violence: <0.001
No domestic violence 2278 (55.0%) 3228 (59.8%) 1764 (61.4%) 161 (54.6%) 6873 (61.0%)
Domestic violence 929 (22.4%) 961 (17.8%) 522 (18.2%) 72 (24.4%) 2381 (21.1%)
‘Missing’ 935 (22.6%) 1205 (22.3%) 586 (20.4%) 62 (21.0%) 2022 (17.9%)
Note. Variables of C1 dataset had to be standardized before comparison;
Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;
Categorical variables are presented as number (%)

Living with

table5 <- compareGroups::compareGroups(con_quien_vive_joel ~ sus_principal_mod+ sexo_2+ escolaridad_rec+ compromiso_biopsicosocial+ estado_conyugal_2+ edad_ini_cons+ edad_al_ing+ sus_ini_mod+ sus_ini_mod_mvv+ freq_cons_sus_prin+ via_adm_sus_prin_act+ numero_de_hijos_mod_joel+ condicion_ocupacional_corr+ cat_ocupacional_corr+ abandono_temprano+ dg_cie_10_rec+ dias_treat_imp_sin_na+ cnt_diagnostico_trs_fisico+ cnt_otros_probl_at_sm_or+  tipo_de_plan_2_mod+ tenencia_de_la_vivienda_mod+ cum_dias_trat_sin_na_1+ cum_dias_trat_sin_na_2+ cum_dias_trat_sin_na_3+ cum_dias_trat_sin_na_4+ cum_dias_trat_sin_na_5+ cum_dias_trat_sin_na_6+ cum_dias_trat_sin_na_7+ cum_dias_trat_sin_na_8+ cum_dias_trat_sin_na_9+ cum_dias_trat_sin_na_10+ dias_treat_imp_sin_na_1+ dias_treat_imp_sin_na_2+ dias_treat_imp_sin_na_3+ dias_treat_imp_sin_na_4+ dias_treat_imp_sin_na_5+ dias_treat_imp_sin_na_6+ dias_treat_imp_sin_na_7+ dias_treat_imp_sin_na_8+ dias_treat_imp_sin_na_9+ dias_treat_imp_sin_na_10+ cum_diff_bet_treat_1+cum_diff_bet_treat_2+ cum_diff_bet_treat_3+ cum_diff_bet_treat_4+ cum_diff_bet_treat_5+ cum_diff_bet_treat_6+ cum_diff_bet_treat_7+ cum_diff_bet_treat_8+ cum_diff_bet_treat_9+ cum_diff_bet_treat_10+ duplicates_filtered+max_cum_dias_trat_sin_na+ max_cum_diff_bet_treat+ cnt_mod_cie_10_dg_cons_sus_or+ cnt_mod_cie_10_or+ dg_total_cie_10+dias_treat_imp_sin_na_four+ diff_bet_treat_four+ cum_dias_trat_sin_na_four+ cum_diff_bet_treat_four+ mean_cum_dias_trat_sin_na_four+ mean_cum_diff_bet_treat_four+ comorbidity_icd_10+ n_treats+ sex_abuse+ dom_violence,
                                       method= c(sus_principal_mod=3,
                                                 sexo_2=3,
                                                 escolaridad_rec=3,
                                                 compromiso_biopsicosocial=2,
                                                 estado_conyugal_2=3,
                                                 edad_ini_cons=2,
                                                 edad_al_ing=2,
                                                 sus_ini_mod=3,
                                                 sus_ini_mod_mvv=3,
                                                 freq_cons_sus_prin=3,
                                                 via_adm_sus_prin_act=3,
                                                 numero_de_hijos_mod_joel=2,
                                                 condicion_ocupacional_corr=3,
                                                 cat_ocupacional_corr=3,
                                                 abandono_temprano=3,
                                                 dg_cie_10_rec=3,
                                                 dias_treat_imp_sin_na=2,
                                                 cnt_mod_cie_10_or=3,
                                                 cnt_diagnostico_trs_fisico=2,
                                                 cnt_otros_probl_at_sm_or=2,
                                                 tipo_de_plan_2_mod=3,
                                                 tenencia_de_la_vivienda_mod=2,
                                                 cum_dias_trat_sin_na_1= 2,
                                                 cum_dias_trat_sin_na_2= 2, 
                                                 cum_dias_trat_sin_na_3= 2, 
                                                 cum_dias_trat_sin_na_4= 2, 
                                                 cum_dias_trat_sin_na_5= 2, 
                                                 cum_dias_trat_sin_na_6= 2, 
                                                 cum_dias_trat_sin_na_7= 2, 
                                                 cum_dias_trat_sin_na_8= 2, 
                                                 cum_dias_trat_sin_na_9= 2, 
                                                 cum_dias_trat_sin_na_10=2, 
                                                 dias_treat_imp_sin_na_1= 2,
                                                 dias_treat_imp_sin_na_2= 2, 
                                                 dias_treat_imp_sin_na_3= 2, 
                                                 dias_treat_imp_sin_na_4= 2, 
                                                 dias_treat_imp_sin_na_5= 2, 
                                                 dias_treat_imp_sin_na_6= 2, 
                                                 dias_treat_imp_sin_na_7= 2, 
                                                 dias_treat_imp_sin_na_8= 2, 
                                                 dias_treat_imp_sin_na_9= 2, 
                                                 dias_treat_imp_sin_na_10=2,                                                  
                                                 cum_diff_bet_treat_1= 2, 
                                                 cum_diff_bet_treat_2= 2, 
                                                 cum_diff_bet_treat_3= 2, 
                                                 cum_diff_bet_treat_4= 2, 
                                                 cum_diff_bet_treat_5= 2, 
                                                 cum_diff_bet_treat_6= 2, 
                                                 cum_diff_bet_treat_7= 2, 
                                                 cum_diff_bet_treat_8= 2, 
                                                 cum_diff_bet_treat_9= 2, 
                                                 cum_diff_bet_treat_10= 2,
                                                 duplicates_filtered=3,
                                                 max_cum_dias_trat_sin_na= 2,
                                                 max_cum_diff_bet_treat= 2,
                                                 cnt_mod_cie_10_dg_cons_sus_or= 3,
                                                 dg_total_cie_10 = 3,
                                                 comorbidity_icd_10 = 3,
                                                 dias_treat_imp_sin_na_four = 2,
                                                 diff_bet_treat_four = 2,
                                                 cum_dias_trat_sin_na_four = 2,
                                                 cum_diff_bet_treat_four = 2,
                                                 mean_cum_dias_trat_sin_na_four = 2,
                                                 mean_cum_diff_bet_treat_four = 2,
                                                 n_treats = 3,
                                                 sex_abuse = 3,
                                                 dom_violence = 3
                                       ),
                                       data = prueba2,
                                       include.miss = T,
                                       var.equal=T,
                                       max.xlev = 10,
                                       max.ylev = 10
)#cie_10 cat_ocupacional estatus_ocupacional

pvals <- getResults(table5)
#p.adjust(pvals, method = "BH")
restab5 <- createTable(table5, show.p.overall = T)
compareGroups::export2md(restab5, size=9, first.strip=T, hide.no="no", position="center",col.names=c("Variables","Alone","Family of origin", "With couple", "P-value"),
                         format="html",caption= "Table 4. Summary descriptives table by With whom they live")%>%
  kableExtra::add_footnote(c("Note. Variables of C1 dataset had to be standardized before comparison;", "Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;", "Categorical variables are presented as number (%)"), notation = "none")%>%
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 4. Summary descriptives table by With whom they live
Variables Alone Family of origin With couple P-value
N=1335 N=15455 N=7189
Sustancia Principal de Consumo (Sólo más frecuentes)(f)/Primary or Main Substance of Consumption at Admission (Only more frequent)(f): <0.001
Alcohol 311 (23.3%) 2434 (15.7%) 1397 (19.4%)
Cocaine hydrochloride 199 (14.9%) 3384 (21.9%) 1811 (25.2%)
Marijuana 148 (11.1%) 2031 (13.1%) 693 (9.64%)
Other 16 (1.20%) 222 (1.44%) 57 (0.79%)
Cocaine paste 661 (49.5%) 7384 (47.8%) 3231 (44.9%)
Sexo Usuario/Sex of User: <0.001
Men 1171 (87.7%) 13154 (85.1%) 5525 (76.9%)
Women 164 (12.3%) 2301 (14.9%) 1664 (23.1%)
escolaridad_rec: .
3-Completed primary school or less 359 (26.9%) 3333 (21.6%) 1942 (27.0%)
2-Completed high school or less 767 (57.5%) 9420 (61.0%) 4331 (60.2%)
1-More than high school 201 (15.1%) 2660 (17.2%) 890 (12.4%)
‘Missing’ 8 (0.60%) 42 (0.27%) 26 (0.36%)
Biopsychosocial Compromise: <0.001
1-Mild 92 (6.89%) 1328 (8.59%) 820 (11.4%)
2-Moderate 642 (48.1%) 8856 (57.3%) 4451 (61.9%)
3-Severe 588 (44.0%) 4988 (32.3%) 1776 (24.7%)
‘Missing’ 13 (0.97%) 283 (1.83%) 142 (1.98%)
Estado Conyugal/Marital Status: .
Married/Shared living arrangements 101 (7.57%) 858 (5.55%) 4168 (58.0%)
Separated/Divorced 55 (4.12%) 361 (2.34%) 85 (1.18%)
Single 1171 (87.7%) 14203 (91.9%) 2920 (40.6%)
Widower 1 (0.07%) 24 (0.16%) 5 (0.07%)
‘Missing’ 7 (0.52%) 9 (0.06%) 11 (0.15%)
Edad de Inicio de Consumo/Age of Onset of Drug Use 14.0 [13.0;16.0] 15.0 [13.0;16.0] 15.0 [13.0;16.0] <0.001
Edad a la Fecha de Ingreso a Tratamiento (numérico continuo) (Primera Entrada)/Age at Admission to Treatment (First Entry) 26.3 [23.5;28.3] 24.7 [22.0;27.3] 26.4 [24.1;28.3] <0.001
Sustancia de Inicio (Sólo más frecuentes)/Starting Substance (Only more frequent): <0.001
Alcohol 587 (44.0%) 6505 (42.1%) 3249 (45.2%)
Cocaína 37 (2.77%) 548 (3.55%) 349 (4.85%)
Marihuana 511 (38.3%) 6157 (39.8%) 2497 (34.7%)
Otros 26 (1.95%) 250 (1.62%) 135 (1.88%)
Pasta Base 68 (5.09%) 636 (4.12%) 350 (4.87%)
‘Missing’ 106 (7.94%) 1359 (8.79%) 609 (8.47%)
Starting Substance: <0.001
Alcohol 581 (43.5%) 6385 (41.3%) 3216 (44.7%)
Cocaine hydrochloride 39 (2.92%) 571 (3.69%) 360 (5.01%)
Marijuana 514 (38.5%) 6228 (40.3%) 2513 (35.0%)
Other 24 (1.80%) 245 (1.59%) 129 (1.79%)
Cocaine paste 71 (5.32%) 667 (4.32%) 362 (5.04%)
‘Missing’ 106 (7.94%) 1359 (8.79%) 609 (8.47%)
Frequency of drug use in the primary substance: <0.001
1 day a week or more 55 (4.12%) 904 (5.85%) 657 (9.14%)
2 to 3 days a week 328 (24.6%) 4334 (28.0%) 2397 (33.3%)
4 to 6 days a week 204 (15.3%) 2737 (17.7%) 1164 (16.2%)
Daily 699 (52.4%) 6768 (43.8%) 2444 (34.0%)
Less than 1 day a week 45 (3.37%) 633 (4.10%) 488 (6.79%)
‘Missing’ 4 (0.30%) 79 (0.51%) 39 (0.54%)
Vía de Administración de la Sustancia Principal (Se aplicaron criterios de limpieza)(f)/Route of Administration of the Primary or Main Substance (Tidy)(f): .
Smoked or Pulmonary Aspiration 798 (59.8%) 9266 (60.0%) 3838 (53.4%)
Intranasal (powder aspiration) 205 (15.4%) 3505 (22.7%) 1865 (25.9%)
Injected Intravenously or Intramuscularly 2 (0.15%) 12 (0.08%) 2 (0.03%)
Oral (drunk or eaten) 328 (24.6%) 2662 (17.2%) 1480 (20.6%)
Other 1 (0.07%) 7 (0.05%) 4 (0.06%)
‘Missing’ 1 (0.07%) 3 (0.02%) 0 (0.00%)
Number of Children (Max. Value), adding 1 if pregnant at admission 1.00 [0.00;1.00] 0.00 [0.00;1.00] 1.00 [1.00;2.00] 0.000
Occupational Status Corrected(f): <0.001
Employed 648 (48.5%) 5601 (36.2%) 4174 (58.1%)
Inactive 57 (4.27%) 1231 (7.97%) 731 (10.2%)
Looking for a job for the first time 3 (0.22%) 78 (0.50%) 16 (0.22%)
No activity 86 (6.44%) 1015 (6.57%) 232 (3.23%)
Not seeking for work 34 (2.55%) 154 (1.00%) 28 (0.39%)
Unemployed 507 (38.0%) 7376 (47.7%) 2008 (27.9%)
Occupational Category Corrected(f): .
Employer 14 (1.05%) 163 (1.05%) 145 (2.02%)
Other 17 (1.27%) 128 (0.83%) 77 (1.07%)
Salaried 432 (32.4%) 3833 (24.8%) 2774 (38.6%)
Self-employed 119 (8.91%) 953 (6.17%) 862 (12.0%)
Unpaid family labour 1 (0.07%) 52 (0.34%) 12 (0.17%)
Volunteer worker 2 (0.15%) 25 (0.16%) 12 (0.17%)
‘Missing’ 750 (56.2%) 10301 (66.7%) 3307 (46.0%)
Abandono temprano(<3 meses)/ Early Drop-out(<3 months): <0.001
Mayor o igual a 90 días 865 (64.8%) 11229 (72.7%) 5289 (73.6%)
Menos de 90 días 470 (35.2%) 4226 (27.3%) 1900 (26.4%)
Diagnóstico CIE-10 (1 o más)(Recodificado)/Psychiatric Diagnoses (ICD-10)(one or more)(Recoded): <0.001
Without psychiatric comorbidity 453 (33.9%) 5718 (37.0%) 3088 (43.0%)
Diagnosis unknown (under study) 346 (25.9%) 3322 (21.5%) 1530 (21.3%)
With psychiatric comorbidity 536 (40.1%) 6415 (41.5%) 2571 (35.8%)
Días de Tratamiento (valores perdidos en la fecha de egreso se reemplazaron por la diferencia con 2019-11-13)/Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13) 125 [68.0;223] 148 [84.0;257] 147 [86.0;253] <0.001
Recuento de Diagnóstico de Trastorno Físico/Count of Physical Disorder 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.015
Recuento de Otros Problemas de Atención Vinculados a Salud Mental/Count of Other problems linked to Mental Health 0.00 [0.00;1.00] 0.00 [0.00;1.00] 0.00 [0.00;1.00] <0.001
Type of Plan (Independently of the Program): .
PAB 391 (29.3%) 5506 (35.6%) 3380 (47.0%)
PAI 586 (43.9%) 7508 (48.6%) 3296 (45.8%)
PR 356 (26.7%) 2423 (15.7%) 507 (7.05%)
‘Missing’ 2 (0.15%) 18 (0.12%) 6 (0.08%)
Tenure status of households: 0.000
Illegal Settlement 53 (3.97%) 48 (0.31%) 90 (1.25%)
Others 74 (5.54%) 374 (2.42%) 114 (1.59%)
Owner/Transferred dwellings/Pays Dividends 258 (19.3%) 5345 (34.6%) 1899 (26.4%)
Renting 555 (41.6%) 1154 (7.47%) 2163 (30.1%)
Stays temporarily with a relative 145 (10.9%) 7879 (51.0%) 2622 (36.5%)
‘Missing’ 250 (18.7%) 655 (4.24%) 301 (4.19%)
Cum. Days of Treatment (1st Treatment) 125 [68.0;224] 148 [84.0;257] 147 [86.0;253] <0.001
Cum. Days of Treatment (2nd Treatment) 285 [174;423] 320 [206;493] 326 [218;489] <0.001
Cum. Days of Treatment (3rd Treatment) 463 [260;644] 474 [326;700] 522 [361;730] 0.008
Cum. Days of Treatment (4th Treatment) 526 [339;755] 633 [426;878] 671 [486;941] 0.059
Cum. Days of Treatment (5th Treatment) 690 [532;1120] 798 [541;1047] 876 [576;1021] 0.604
Cum. Days of Treatment (6th Treatment) 460 [356;565] 897 [712;1153] 1006 [868;1144] 0.106
Cum. Days of Treatment (7th Treatment) 477 [376;578] 1165 [993;1331] 1076 [1074;1254] 0.100
Cum. Days of Treatment (8th Treatment) . [.;.] 1152 [1131;1172] 1273 [1273;1273] 0.221
Cum. Days of Treatment (9th Treatment) . [.;.] 1403 [1403;1403] . [.;.] .
Cum. Days of Treatment (10th Treatment) . [.;.] 1622 [1622;1622] . [.;.] .
Days of Treatment (1st Treatment) 125 [68.0;223] 148 [84.0;257] 147 [86.0;253] <0.001
Days of Treatment (2nd Treatment) 119 [57.0;204] 137 [76.0;239] 139 [83.0;245] <0.001
Days of Treatment (3rd Treatment) 127 [71.0;220] 127 [70.0;229] 148 [84.8;246] 0.020
Days of Treatment (4th Treatment) 143 [57.2;248] 125 [70.0;215] 131 [71.0;231] 0.997
Days of Treatment (5th Treatment) 199 [139;335] 138 [67.8;254] 145 [59.2;260] 0.567
Days of Treatment (6th Treatment) 28.0 [17.5;38.5] 149 [121;195] 116 [68.5;205] 0.121
Days of Treatment (7th Treatment) 16.5 [13.2;19.8] 169 [58.5;244] 53.0 [52.0;120] 0.075
Days of Treatment (8th Treatment) . [.;.] 84.5 [62.2;107] 19.0 [19.0;19.0] 0.221
Days of Treatment (9th Treatment) . [.;.] 211 [211;211] . [.;.] .
Days of Treatment (10th Treatment) . [.;.] 219 [219;219] . [.;.] .
Cum. Diff Between Treatments (1st Treatment) 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.812
Cum. Diff Between Treatments (2nd Treatment) 715 [380;1315] 779 [396;1355] 872 [441;1492] 0.075
Cum. Diff Between Treatments (3rd Treatment) 920 [405;1812] 1128 [674;1648] 1235 [770;1697] 0.436
Cum. Diff Between Treatments (4th Treatment) 1290 [1044;2121] 1196 [843;1722] 1448 [1149;2190] 0.034
Cum. Diff Between Treatments (5th Treatment) 2020 [1858;2182] 1470 [1091;1768] 1418 [944;2384] 0.513
Cum. Diff Between Treatments (6th Treatment) 2536 [2415;2657] 1576 [1244;2085] 1221 [1015;1287] 0.174
Cum. Diff Between Treatments (7th Treatment) . [.;.] 1418 [1300;1537] 1188 [1188;1188] 1.000
Cum. Diff Between Treatments (8th Treatment) . [.;.] 1706 [1706;1706] . [.;.] .
Cum. Diff Between Treatments (9th Treatment) . [.;.] 1944 [1944;1944] . [.;.] .
Cum. Diff Between Treatments (10th Treatment) . . . .
Número de Tratamientos por HASH (Total)/Number of Treatments by User (Total): .
1 1033 (77.4%) 11903 (77.0%) 5536 (77.0%)
2 207 (15.5%) 2540 (16.4%) 1161 (16.1%)
3 71 (5.32%) 673 (4.35%) 343 (4.77%)
4 15 (1.12%) 233 (1.51%) 99 (1.38%)
5 7 (0.52%) 69 (0.45%) 35 (0.49%)
6 0 (0.00%) 27 (0.17%) 10 (0.14%)
7 2 (0.15%) 8 (0.05%) 4 (0.06%)
8 0 (0.00%) 1 (0.01%) 1 (0.01%)
10 0 (0.00%) 1 (0.01%) 0 (0.00%)
Max. Cumulative Days of Treatment 154 [80.0;304] 183 [98.0;332] 186 [100;338] <0.001
Max. Cumulative Difference Between Treatments 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.883
Total count of Psychiatric & Drug dependence Diagnostics: .
0 224 (16.8%) 2493 (16.1%) 1547 (21.5%)
1 695 (52.1%) 8175 (52.9%) 3827 (53.2%)
2 399 (29.9%) 4536 (29.3%) 1742 (24.2%)
3 16 (1.20%) 226 (1.46%) 67 (0.93%)
4 1 (0.07%) 25 (0.16%) 6 (0.08%)
cnt_mod_cie_10_or: .
0 453 (33.9%) 5718 (37.0%) 3088 (43.0%)
1 860 (64.4%) 9441 (61.1%) 4013 (55.8%)
2 19 (1.42%) 264 (1.71%) 82 (1.14%)
3 3 (0.22%) 32 (0.21%) 6 (0.08%)
Conteo de Diagnósticos CIE-10(sólo diagnósticos)/Count of ICD-10 Diagnostics(only diagnoses): .
0 799 (59.9%) 9040 (58.5%) 4618 (64.2%)
1 514 (38.5%) 6119 (39.6%) 2483 (34.5%)
2 19 (1.42%) 264 (1.71%) 82 (1.14%)
3 3 (0.22%) 32 (0.21%) 6 (0.08%)
Days of Treatment (Fourth or those that follow) 149 [51.8;234] 138 [77.8;225] 137 [85.4;236] 0.966
Days of Difference Between Treatments (Fifth treatment or those that folow) 464 [238;588] 258 [112;416] 288 [164;682] 0.064
Cumulative Days of Treatment (Fourth or those that follow) 590 [364;936] 683 [454;932] 727 [518;999] 0.083
Cumulative Difference Between Treatments (Fifth or those that follow) 1760 [1044;2121] 1345 [943;1759] 1518 [1174;2252] 0.081
Average Cumulative Days of Treatment (Fourth or those that follow) 133 [91.0;209] 162 [107;218] 171 [116;229] 0.080
Average Cumulative Difference Between Treatments (Fifth or those that follow) 348 [261;497] 312 [222;424] 373 [286;544] 0.064
Comorbidity ICD-10 (with amount of different diagnosis): <0.001
Without psychiatric comorbidity 453 (33.9%) 5718 (37.0%) 3088 (43.0%)
Diagnosis unknown (under study) 346 (25.9%) 3322 (21.5%) 1530 (21.3%)
One 514 (38.5%) 6119 (39.6%) 2483 (34.5%)
Two or more 22 (1.65%) 296 (1.92%) 88 (1.22%)
No. of treatments with 18+ at admission between 2010 and 2019: 0.426
01 1034 (77.5%) 11905 (77.0%) 5536 (77.0%)
02 206 (15.4%) 2540 (16.4%) 1161 (16.1%)
03 71 (5.32%) 671 (4.34%) 343 (4.77%)
04 or more 24 (1.80%) 339 (2.19%) 149 (2.07%)
Sexual abuse: 0.034
No sexual abuse 1053 (78.9%) 12207 (79.0%) 5620 (78.2%)
Sexual abuse 27 (2.02%) 175 (1.13%) 87 (1.21%)
‘Missing’ 255 (19.1%) 3073 (19.9%) 1482 (20.6%)
Domestic violence: <0.001
No domestic violence 745 (55.8%) 9509 (61.5%) 4050 (56.3%)
Domestic violence 335 (25.1%) 2873 (18.6%) 1657 (23.0%)
‘Missing’ 255 (19.1%) 3073 (19.9%) 1482 (20.6%)
Note. Variables of C1 dataset had to be standardized before comparison;
Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;
Categorical variables are presented as number (%)

Readmissions

table6 <- compareGroups::compareGroups(had_readm ~ sexo_2+ escolaridad_rec+ compromiso_biopsicosocial+ estado_conyugal_2+ edad_ini_cons+ edad_al_ing+ sus_ini_mod+ sus_ini_mod_mvv+ freq_cons_sus_prin+ via_adm_sus_prin_act+ con_quien_vive_joel+ numero_de_hijos_mod_joel+ condicion_ocupacional_corr+ cat_ocupacional_corr+ abandono_temprano+ dg_cie_10_rec+ dias_treat_imp_sin_na+ cnt_diagnostico_trs_fisico+ cnt_otros_probl_at_sm_or+ tipo_de_plan_2_mod+ tenencia_de_la_vivienda_mod+ cum_dias_trat_sin_na_1+ cum_dias_trat_sin_na_2+ cum_dias_trat_sin_na_3+ cum_dias_trat_sin_na_4+ cum_dias_trat_sin_na_5+ cum_dias_trat_sin_na_6+ cum_dias_trat_sin_na_7+ cum_dias_trat_sin_na_8+ cum_dias_trat_sin_na_9+ cum_dias_trat_sin_na_10+ dias_treat_imp_sin_na_1+ dias_treat_imp_sin_na_2+ dias_treat_imp_sin_na_3+ dias_treat_imp_sin_na_4+ dias_treat_imp_sin_na_5+ dias_treat_imp_sin_na_6+ dias_treat_imp_sin_na_7+ dias_treat_imp_sin_na_8+ dias_treat_imp_sin_na_9+ dias_treat_imp_sin_na_10+ cum_diff_bet_treat_1+cum_diff_bet_treat_2+ cum_diff_bet_treat_3+ cum_diff_bet_treat_4+ cum_diff_bet_treat_5+ cum_diff_bet_treat_6+ cum_diff_bet_treat_7+ cum_diff_bet_treat_8+ cum_diff_bet_treat_9+ cum_diff_bet_treat_10+ duplicates_filtered+ max_cum_dias_trat_sin_na+ max_cum_diff_bet_treat+ cnt_mod_cie_10_dg_cons_sus_or+ cnt_mod_cie_10_or+ dg_total_cie_10+dias_treat_imp_sin_na_four+ diff_bet_treat_four+ cum_dias_trat_sin_na_four+ cum_diff_bet_treat_four+ mean_cum_dias_trat_sin_na_four+ mean_cum_diff_bet_treat_four+ comorbidity_icd_10+ n_treats+ sex_abuse+ dom_violence,
                                       method= c(
                                                 sexo_2=3,
                                                 escolaridad_rec=3,
                                                 compromiso_biopsicosocial=2,
                                                 estado_conyugal_2=3,
                                                 edad_ini_cons=2,
                                                 edad_al_ing=2,
                                                 sus_ini_mod=3,
                                                 sus_ini_mod_mvv=3,
                                                 freq_cons_sus_prin=3,
                                                 via_adm_sus_prin_act=3,
                                                 con_quien_vive_joel=3,
                                                 numero_de_hijos_mod_joel=2,
                                                 condicion_ocupacional_corr=3,
                                                 cat_ocupacional_corr=3,
                                                 abandono_temprano=3,
                                                 dg_cie_10_rec=3,
                                                 dias_treat_imp_sin_na=2,
                                                 cnt_mod_cie_10_or=3,
                                                 cnt_diagnostico_trs_fisico=2,
                                                 cnt_otros_probl_at_sm_or=2,
                                                 tipo_de_plan_2_mod=3,
                                                 tenencia_de_la_vivienda_mod=2,
                                                 cum_dias_trat_sin_na_1= 2,
                                                 cum_dias_trat_sin_na_2= 2, 
                                                 cum_dias_trat_sin_na_3= 2, 
                                                 cum_dias_trat_sin_na_4= 2, 
                                                 cum_dias_trat_sin_na_5= 2, 
                                                 cum_dias_trat_sin_na_6= 2, 
                                                 cum_dias_trat_sin_na_7= 2, 
                                                 cum_dias_trat_sin_na_8= 2, 
                                                 cum_dias_trat_sin_na_9= 2, 
                                                 cum_dias_trat_sin_na_10=2, 
                                                 dias_treat_imp_sin_na_1= 2,
                                                 dias_treat_imp_sin_na_2= 2, 
                                                 dias_treat_imp_sin_na_3= 2, 
                                                 dias_treat_imp_sin_na_4= 2, 
                                                 dias_treat_imp_sin_na_5= 2, 
                                                 dias_treat_imp_sin_na_6= 2, 
                                                 dias_treat_imp_sin_na_7= 2, 
                                                 dias_treat_imp_sin_na_8= 2, 
                                                 dias_treat_imp_sin_na_9= 2, 
                                                 dias_treat_imp_sin_na_10=2,                                                  
                                                 cum_diff_bet_treat_1= 2, 
                                                 cum_diff_bet_treat_2= 2, 
                                                 cum_diff_bet_treat_3= 2, 
                                                 cum_diff_bet_treat_4= 2, 
                                                 cum_diff_bet_treat_5= 2, 
                                                 cum_diff_bet_treat_6= 2, 
                                                 cum_diff_bet_treat_7= 2, 
                                                 cum_diff_bet_treat_8= 2, 
                                                 cum_diff_bet_treat_9= 2, 
                                                 cum_diff_bet_treat_10= 2,
                                                 duplicates_filtered=3,
                                                 max_cum_dias_trat_sin_na= 2,
                                                 max_cum_diff_bet_treat= 2,
                                                 cnt_mod_cie_10_dg_cons_sus_or= 3,
                                                 dg_total_cie_10 = 3,
                                                 comorbidity_icd_10 = 3,
                                                 dias_treat_imp_sin_na_four = 2,
                                                 diff_bet_treat_four = 2,
                                                 cum_dias_trat_sin_na_four = 2,
                                                 cum_diff_bet_treat_four = 2,
                                                 mean_cum_dias_trat_sin_na_four = 2,
                                                 mean_cum_diff_bet_treat_four = 2,
                                                 n_treats = 3,
                                                 sex_abuse = 3,
                                                 dom_violence = 3
                                       ),
                                       data = prueba2,
                                       include.miss = T,
                                       var.equal=T
)#cie_10 cat_ocupacional estatus_ocupacional

pvals <- getResults(table6)
#p.adjust(pvals, method = "BH")
restab6 <- createTable(table6, show.p.overall = T)
compareGroups::export2md(restab6, size=9, first.strip=T, hide.no="no", position="center",col.names=c("Variables","Had no Readmissions","Had Readmissions", "P-value"),
                         format="html",caption= "Table 5. Summary descriptives table by Readmissions")%>%
  kableExtra::add_footnote(c("Note. Variables of C1 dataset had to be standardized before comparison;", "Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;", "Categorical variables are presented as number (%)"), notation = "none")%>%
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 5. Summary descriptives table by Readmissions
Variables Had no Readmissions Had Readmissions P-value
N=18475 N=5504
Sexo Usuario/Sex of User: 0.001
Men 15373 (83.2%) 4477 (81.3%)
Women 3102 (16.8%) 1027 (18.7%)
escolaridad_rec: <0.001
3-Completed primary school or less 4166 (22.5%) 1468 (26.7%)
2-Completed high school or less 11046 (59.8%) 3472 (63.1%)
1-More than high school 3202 (17.3%) 549 (9.97%)
‘Missing’ 61 (0.33%) 15 (0.27%)
Biopsychosocial Compromise: <0.001
1-Mild 1840 (9.96%) 400 (7.27%)
2-Moderate 11058 (59.9%) 2891 (52.5%)
3-Severe 5244 (28.4%) 2108 (38.3%)
‘Missing’ 333 (1.80%) 105 (1.91%)
Estado Conyugal/Marital Status: 0.105
Married/Shared living arrangements 3918 (21.2%) 1209 (22.0%)
Separated/Divorced 397 (2.15%) 104 (1.89%)
Single 14121 (76.4%) 4173 (75.8%)
Widower 18 (0.10%) 12 (0.22%)
‘Missing’ 21 (0.11%) 6 (0.11%)
Edad de Inicio de Consumo/Age of Onset of Drug Use 15.0 [13.0;16.0] 14.0 [13.0;16.0] 0.088
Edad a la Fecha de Ingreso a Tratamiento (numérico continuo) (Primera Entrada)/Age at Admission to Treatment (First Entry) 25.3 [22.5;27.7] 25.5 [22.8;27.8] <0.001
Sustancia de Inicio (Sólo más frecuentes)/Starting Substance (Only more frequent): <0.001
Alcohol 7890 (42.7%) 2451 (44.5%)
Cocaína 688 (3.72%) 246 (4.47%)
Marihuana 6926 (37.5%) 2239 (40.7%)
Otros 298 (1.61%) 113 (2.05%)
Pasta Base 667 (3.61%) 387 (7.03%)
‘Missing’ 2006 (10.9%) 68 (1.24%)
Starting Substance: <0.001
Alcohol 7839 (42.4%) 2343 (42.6%)
Cocaine hydrochloride 703 (3.81%) 267 (4.85%)
Marijuana 6951 (37.6%) 2304 (41.9%)
Other 295 (1.60%) 103 (1.87%)
Cocaine paste 681 (3.69%) 419 (7.61%)
‘Missing’ 2006 (10.9%) 68 (1.24%)
Frequency of drug use in the primary substance: 0.003
1 day a week or more 1272 (6.88%) 344 (6.25%)
2 to 3 days a week 5493 (29.7%) 1566 (28.5%)
4 to 6 days a week 3140 (17.0%) 965 (17.5%)
Daily 7539 (40.8%) 2372 (43.1%)
Less than 1 day a week 935 (5.06%) 231 (4.20%)
‘Missing’ 96 (0.52%) 26 (0.47%)
Vía de Administración de la Sustancia Principal (Se aplicaron criterios de limpieza)(f)/Route of Administration of the Primary or Main Substance (Tidy)(f): .
Smoked or Pulmonary Aspiration 10250 (55.5%) 3652 (66.4%)
Intranasal (powder aspiration) 4504 (24.4%) 1071 (19.5%)
Injected Intravenously or Intramuscularly 15 (0.08%) 1 (0.02%)
Oral (drunk or eaten) 3695 (20.0%) 775 (14.1%)
Other 7 (0.04%) 5 (0.09%)
‘Missing’ 4 (0.02%) 0 (0.00%)
Whom you live with(cohabitation status) (Recoded) (f): 0.935
Alone 1034 (5.60%) 301 (5.47%)
Family of origin 11905 (64.4%) 3550 (64.5%)
With couple/children 5536 (30.0%) 1653 (30.0%)
Number of Children (Max. Value), adding 1 if pregnant at admission 1.00 [0.00;1.00] 1.00 [0.00;2.00] <0.001
Occupational Status Corrected(f): <0.001
Employed 8278 (44.8%) 2145 (39.0%)
Inactive 1562 (8.45%) 457 (8.30%)
Looking for a job for the first time 72 (0.39%) 25 (0.45%)
No activity 1021 (5.53%) 312 (5.67%)
Not seeking for work 157 (0.85%) 59 (1.07%)
Unemployed 7385 (40.0%) 2506 (45.5%)
Occupational Category Corrected(f): <0.001
Employer 256 (1.39%) 66 (1.20%)
Other 176 (0.95%) 46 (0.84%)
Salaried 5527 (29.9%) 1512 (27.5%)
Self-employed 1584 (8.57%) 350 (6.36%)
Unpaid family labour 50 (0.27%) 15 (0.27%)
Volunteer worker 30 (0.16%) 9 (0.16%)
‘Missing’ 10852 (58.7%) 3506 (63.7%)
Abandono temprano(<3 meses)/ Early Drop-out(<3 months): 0.547
Mayor o igual a 90 días 13375 (72.4%) 4008 (72.8%)
Menos de 90 días 5100 (27.6%) 1496 (27.2%)
Diagnóstico CIE-10 (1 o más)(Recodificado)/Psychiatric Diagnoses (ICD-10)(one or more)(Recoded): 0.134
Without psychiatric comorbidity 7191 (38.9%) 2068 (37.6%)
Diagnosis unknown (under study) 4006 (21.7%) 1192 (21.7%)
With psychiatric comorbidity 7278 (39.4%) 2244 (40.8%)
Días de Tratamiento (valores perdidos en la fecha de egreso se reemplazaron por la diferencia con 2019-11-13)/Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13) 146 [84.0;254] 148 [85.0;254] 0.399
Recuento de Diagnóstico de Trastorno Físico/Count of Physical Disorder 0.00 [0.00;0.00] 0.00 [0.00;0.00] 0.949
Recuento de Otros Problemas de Atención Vinculados a Salud Mental/Count of Other problems linked to Mental Health 0.00 [0.00;1.00] 0.00 [0.00;1.00] <0.001
Type of Plan (Independently of the Program): <0.001
PAB 7376 (39.9%) 1901 (34.5%)
PAI 8901 (48.2%) 2489 (45.2%)
PR 2184 (11.8%) 1102 (20.0%)
‘Missing’ 14 (0.08%) 12 (0.22%)
Tenure status of households: 0.056
Illegal Settlement 145 (0.78%) 46 (0.84%)
Others 424 (2.29%) 138 (2.51%)
Owner/Transferred dwellings/Pays Dividends 5782 (31.3%) 1720 (31.2%)
Renting 2999 (16.2%) 873 (15.9%)
Stays temporarily with a relative 8239 (44.6%) 2407 (43.7%)
‘Missing’ 886 (4.80%) 320 (5.81%)
Cum. Days of Treatment (1st Treatment) 146 [84.0;254] 148 [85.0;255] 0.380
Cum. Days of Treatment (2nd Treatment) . [.;.] 318 [207;489] .
Cum. Days of Treatment (3rd Treatment) . [.;.] 485 [335;704] .
Cum. Days of Treatment (4th Treatment) . [.;.] 638 [433;897] .
Cum. Days of Treatment (5th Treatment) . [.;.] 805 [550;1041] .
Cum. Days of Treatment (6th Treatment) . [.;.] 944 [716;1152] .
Cum. Days of Treatment (7th Treatment) . [.;.] 1076 [888;1279] .
Cum. Days of Treatment (8th Treatment) . [.;.] 1192 [1152;1232] .
Cum. Days of Treatment (9th Treatment) . [.;.] 1403 [1403;1403] .
Cum. Days of Treatment (10th Treatment) . [.;.] 1622 [1622;1622] .
Days of Treatment (1st Treatment) 146 [84.0;254] 148 [85.0;254] 0.399
Days of Treatment (2nd Treatment) . [.;.] 137 [77.0;239] .
Days of Treatment (3rd Treatment) . [.;.] 134 [73.0;237] .
Days of Treatment (4th Treatment) . [.;.] 126 [70.0;224] .
Days of Treatment (5th Treatment) . [.;.] 143 [67.0;260] .
Days of Treatment (6th Treatment) . [.;.] 145 [76.8;199] .
Days of Treatment (7th Treatment) . [.;.] 120 [23.0;175] .
Days of Treatment (8th Treatment) . [.;.] 40.0 [29.5;84.5] .
Days of Treatment (9th Treatment) . [.;.] 211 [211;211] .
Days of Treatment (10th Treatment) . [.;.] 219 [219;219] .
Cum. Diff Between Treatments (1st Treatment) 0.00 [0.00;0.00] 407 [149;926] 0.000
Cum. Diff Between Treatments (2nd Treatment) . [.;.] 801 [405;1390] .
Cum. Diff Between Treatments (3rd Treatment) . [.;.] 1155 [686;1680] .
Cum. Diff Between Treatments (4th Treatment) . [.;.] 1341 [886;1972] .
Cum. Diff Between Treatments (5th Treatment) . [.;.] 1458 [1047;1970] .
Cum. Diff Between Treatments (6th Treatment) . [.;.] 1509 [1160;2294] .
Cum. Diff Between Treatments (7th Treatment) . [.;.] 1188 [1184;1422] .
Cum. Diff Between Treatments (8th Treatment) . [.;.] 1706 [1706;1706] .
Cum. Diff Between Treatments (9th Treatment) . [.;.] 1944 [1944;1944] .
Cum. Diff Between Treatments (10th Treatment) . . .
Número de Tratamientos por HASH (Total)/Number of Treatments by User (Total): .
1 18472 (100.0%) 0 (0.00%)
2 3 (0.02%) 3905 (70.9%)
3 0 (0.00%) 1087 (19.7%)
4 0 (0.00%) 347 (6.30%)
5 0 (0.00%) 111 (2.02%)
6 0 (0.00%) 37 (0.67%)
7 0 (0.00%) 14 (0.25%)
8 0 (0.00%) 2 (0.04%)
10 0 (0.00%) 1 (0.02%)
Max. Cumulative Days of Treatment 146 [84.0;254] 380 [239;583] 0.000
Max. Cumulative Difference Between Treatments 0.00 [0.00;0.00] 630 [232;1268] 0.000
Total count of Psychiatric & Drug dependence Diagnostics: <0.001
0 3463 (18.7%) 801 (14.6%)
1 9719 (52.6%) 2978 (54.1%)
2 5036 (27.3%) 1641 (29.8%)
3 232 (1.26%) 77 (1.40%)
4 25 (0.14%) 7 (0.13%)
cnt_mod_cie_10_or: 0.238
0 7191 (38.9%) 2068 (37.6%)
1 10972 (59.4%) 3342 (60.7%)
2 278 (1.50%) 87 (1.58%)
3 34 (0.18%) 7 (0.13%)
Conteo de Diagnósticos CIE-10(sólo diagnósticos)/Count of ICD-10 Diagnostics(only diagnoses): 0.226
0 11197 (60.6%) 3260 (59.2%)
1 6966 (37.7%) 2150 (39.1%)
2 278 (1.50%) 87 (1.58%)
3 34 (0.18%) 7 (0.13%)
Days of Treatment (Fourth or those that follow) . [.;.] 138 [78.8;227] .
Days of Difference Between Treatments (Fifth treatment or those that folow) . [.;.] 277 [140;485] .
Cumulative Days of Treatment (Fourth or those that follow) . [.;.] 692 [460;936] .
Cumulative Difference Between Treatments (Fifth or those that follow) . [.;.] 1412 [1031;1995] .
Average Cumulative Days of Treatment (Fourth or those that follow) . [.;.] 163 [110;221] .
Average Cumulative Difference Between Treatments (Fifth or those that follow) . [.;.] 340 [236;488] .
Comorbidity ICD-10 (with amount of different diagnosis): 0.254
Without psychiatric comorbidity 7191 (38.9%) 2068 (37.6%)
Diagnosis unknown (under study) 4006 (21.7%) 1192 (21.7%)
One 6966 (37.7%) 2150 (39.1%)
Two or more 312 (1.69%) 94 (1.71%)
No. of treatments with 18+ at admission between 2010 and 2019: 0.000
01 18475 (100%) 0 (0.00%)
02 0 (0.00%) 3907 (71.0%)
03 0 (0.00%) 1085 (19.7%)
04 or more 0 (0.00%) 512 (9.30%)
Sexual abuse: <0.001
No sexual abuse 14413 (78.0%) 4467 (81.2%)
Sexual abuse 203 (1.10%) 86 (1.56%)
‘Missing’ 3859 (20.9%) 951 (17.3%)
Domestic violence: <0.001
No domestic violence 10974 (59.4%) 3330 (60.5%)
Domestic violence 3642 (19.7%) 1223 (22.2%)
‘Missing’ 3859 (20.9%) 951 (17.3%)
Note. Variables of C1 dataset had to be standardized before comparison;
Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;
Categorical variables are presented as number (%)
library(ggplot2)

jpg_path<-rstudioapi::getSourceEditorContext()$path
if (grepl("CISS Fondecyt",jpg_path)==T){
    jpg_path<-paste0("C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2019 (github)/")
  } else if (grepl("andre",jpg_path)==T){
    jpg_path<-paste0('C:/Users/andre/Desktop/SUD_CL/')
  } else if (grepl("E:",jpg_path)==T){
    jpg_path<-paste0("E:/Mi unidad/Alvacast/SISTRAT 2019 (github)/")
  } else {
    jpg_path<-paste0("G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/")
  }


prueba2 %>% 
 # dplyr::group_by(hash_key)%>%
 # dplyr::mutate(n_hash=n())%>% 
  #dplyr::filter(n_hash>1)%>%
 # slice(1) %>% 
 # ungroup() %>% 
  dplyr::select(dias_treat_imp_sin_na_1,dias_treat_imp_sin_na_2,dias_treat_imp_sin_na_3,dias_treat_imp_sin_na_four) %>%  #mean_cum_dias_trat_sin_na_1
  tidyr::gather(option,value) %>%
  dplyr::mutate(option=dplyr::case_when(option=="dias_treat_imp_sin_na_1"~"01",
                                        option=="dias_treat_imp_sin_na_2"~"02",
                                        option=="dias_treat_imp_sin_na_3"~"03",
                                        option=="dias_treat_imp_sin_na_four"~"04 or more")) %>% 
  dplyr::mutate(option=factor(option)) %>% 
  dplyr::group_by(option) %>% 
  dplyr::mutate(count = length(na.omit(value))) %>% 
  dplyr::ungroup() %>% 
  ggplot(aes(x = option, y=value,group= option)) +
      stat_summary(fun = mean, geom="bar",alpha=.8, na.rm = T)+
    stat_summary(fun = median, geom="point", na.rm = T)+
  stat_summary(fun = median,
                   fun.min = function(x) quantile(x,.25, na.rm = T), 
               fun.max = function(x) quantile(x,.75, na.rm = T), 
               geom = "errorbar", width = 0.5)+
  geom_label(inherit.aes = FALSE, data = . %>% group_by(option) %>% slice(1), 
          aes(label = paste0(count, " Obs."), x = option), y = -0.5)+
  geom_label(inherit.aes = FALSE, data = . %>% group_by(option) %>% slice(1), 
          aes(label = paste0(count, " Obs."), x = option), y = -0.5)+
  theme_bw()+
  theme(plot.caption = element_text(hjust = 0, face= "italic",size=9))+
  #geom_bar(stat = "identity")+
    #geom_errorbar() +
  labs(x="Number of Treatment of Each User", y="Days in Treatment",caption=paste0("Note. Bars=Means, Dots= Medians, Error bars= Percentiles 25 and 75"))
## Warning: attributes are not identical across measure variables;
## they will be dropped
## Warning: Removed 64324 rows containing non-finite values (stat_summary).
Figura 1. Días de Tratamiento, según la posición del tratamiento en cada usuario usuario

Figura 1. Días de Tratamiento, según la posición del tratamiento en cada usuario usuario

library(ggplot2)
prueba2 %>% 
 # dplyr::group_by(hash_key)%>%
 # dplyr::mutate(n_hash=n())%>% 
  #dplyr::filter(n_hash>1)%>%
 # slice(1) %>% 
 # ungroup() %>% 
  dplyr::select(diff_bet_treat_1,diff_bet_treat_2,diff_bet_treat_3,diff_bet_treat_four) %>%  #mean_cum_dias_trat_sin_na_1
  tidyr::gather(option,value) %>%
  dplyr::mutate(option=dplyr::case_when(option=="diff_bet_treat_1"~"01",
                                        option=="diff_bet_treat_2"~"02",
                                        option=="diff_bet_treat_3"~"03",
                                        option=="diff_bet_treat_four"~"04 or more")) %>%
  dplyr::mutate(option=factor(option)) %>% 
  dplyr::group_by(option) %>% 
  dplyr::mutate(count = length(na.omit(value))) %>% 
  dplyr::ungroup() %>% 
  ggplot(aes(x = option, y=value,group= option)) +
      stat_summary(fun = mean, geom="bar",alpha=.8, na.rm = T)+
    stat_summary(fun = median, geom="point", na.rm = T)+
  stat_summary(fun = median,
                   fun.min = function(x) quantile(x,.25, na.rm = T), 
                   fun.max = function(x) quantile(x,.75, na.rm = T), 
                   geom = "errorbar", width = 0.5)+
  #geom_label(inherit.aes = F,aes(x = option, y=value, label = max(count, na.rm=T)), vjust = -0.5)+  
  geom_label(inherit.aes = FALSE, data = . %>% group_by(option) %>% slice(1), 
            aes(label = paste0(count, " Obs."), x = option), y = -0.5)+
  theme_bw()+
  theme(plot.caption = element_text(hjust = 0, face= "italic",size=9))+
  #geom_bar(stat = "identity")+
    #geom_errorbar() +
  labs(x="Number of Treatment of Each User", y="Difference (in days) with the posterior treatment",caption=paste0("Note. Bars=Means, Dots= Medians, Error bars= Percentiles 25 and 75"))
## Warning: attributes are not identical across measure variables;
## they will be dropped
## Warning: Removed 88138 rows containing non-finite values (stat_summary).
Figura 2. Diferencia en Días con el Tratamiento Siguiente, según la posición del tratamiento en cada usuario usuario

Figura 2. Diferencia en Días con el Tratamiento Siguiente, según la posición del tratamiento en cada usuario usuario

library(ggplot2)
f3<-
prueba2 %>% 
 # dplyr::group_by(hash_key)%>%
 # dplyr::mutate(n_hash=n())%>% 
  #dplyr::filter(n_hash>1)%>%
 # slice(1) %>% 
 # ungroup() %>% 
  dplyr::select(con_quien_vive_joel,dias_treat_imp_sin_na_1,dias_treat_imp_sin_na_2,dias_treat_imp_sin_na_3,dias_treat_imp_sin_na_four) %>%  #mean_cum_dias_trat_sin_na_1con_quien_vive_joel
  tidyr::gather(option,value,-con_quien_vive_joel) %>%
  dplyr::mutate(option=dplyr::case_when(option=="dias_treat_imp_sin_na_1"~"01",
                                        option=="dias_treat_imp_sin_na_2"~"02",
                                        option=="dias_treat_imp_sin_na_3"~"03",
                                        option=="dias_treat_imp_sin_na_four"~"04 or more")) %>% 
  dplyr::mutate(option=factor(option)) %>% 
  dplyr::group_by(con_quien_vive_joel,option) %>% 
  dplyr::mutate(count = length(na.omit(value))) %>% 
  dplyr::ungroup() %>% 
  ggplot(aes(x = option, y=value,group= option)) +
      stat_summary(fun = mean, geom="bar",alpha=.8, na.rm = T)+
    stat_summary(fun = median, geom="point", na.rm = T)+
  stat_summary(fun = median,
                   fun.min = function(x) quantile(x,.25, na.rm = T), 
               fun.max = function(x) quantile(x,.75, na.rm = T), 
               geom = "errorbar", width = 0.5)+
  geom_label(inherit.aes = FALSE, data = . %>% group_by(option,con_quien_vive_joel) %>% slice(1), 
          aes(label = paste0("n=",count), x = option), y = -0.5)+
  geom_label(inherit.aes = FALSE, data = . %>% group_by(option,con_quien_vive_joel) %>% slice(1), 
          aes(label = paste0("n=",count), x = option), y = -0.5)+
  facet_wrap(~con_quien_vive_joel)+
  theme_bw()+
  theme(plot.caption = element_text(hjust = 0, face= "italic",size=9))+
  #geom_bar(stat = "identity")+
    #geom_errorbar() +
  labs(x="Number of Treatment of Each User", y="Days in Treatment",caption=paste0("Note. Bars=Means, Dots= Medians, Error bars= Percentiles 25 and 75"))
## Warning: attributes are not identical across measure variables;
## they will be dropped
f3
## Warning: Removed 64324 rows containing non-finite values (stat_summary).
Figura 3. Días de Tratamiento, según la posición del tratamiento en cada usuario usuario

Figura 3. Días de Tratamiento, según la posición del tratamiento en cada usuario usuario

no_mostrar=0


if(no_mostrar==1){
jpeg(paste0(jpg_path,"fig3_joel.jpg"), height=10, width= 10, res= 320, units = "in")
f3
dev.off()
}


Imputation (October 2021)


We generated a plot to see all the missing values in the sample.


#<div style="border: 1px solid #ddd; padding: 5px; overflow-y: scroll; height:400px; overflow-x: scroll; width:100%">
library(dplyr)
library(ggplot2)

# 
# sexo_2=3,
# escolaridad_rec=3,   76 (0.32%)
# estado_conyugal_2=3, 27 (0.11%)
# edad_ini_sus_prin=2, 2755 (11,5%)
# edad_al_ing=2,
# sus_ini_mod=3, 2074 (8.65%)
# sus_ini_mod_mvv=3, 2074 (8.65%)
# freq_cons_sus_prin=3, 122 (0.51%)
# via_adm_sus_prin_act=3, 4 (0.02%)
# con_quien_vive_joel=3, 
# numero_de_hijos_mod_joel=2, 293 (1,2%)
# condicion_ocupacional_corr=3, 14358 (59.9%)
# cat_ocupacional_corr=3, 14358 (59.9%)
# abandono_temprano=3,
# dg_cie_10_rec=3,
# dias_treat_imp_sin_na=2,
# cnt_mod_cie_10_or=3,
# cnt_diagnostico_trs_fisico=2,
# cnt_otros_probl_at_sm_or=2,
# tipo_de_plan_2_mod=3, 26 (0.11%)
# cnt_mod_cie_10_dg_cons_sus_or= 3,
# dg_total_cie_10 = 3,
# comorbidity_icd_10 = 3,
# compromiso_biopsicosocial 438 (1.83%)
# tenencia_de_la_vivienda_mod 1206 (5.03%)


vector_variables<-
c("sexo_2", "escolaridad_rec", "estado_conyugal_2", "edad_ini_cons", "sus_ini_mod_mvv", "freq_cons_sus_prin", "condicion_ocupacional_corr", "via_adm_sus_prin_act", "con_quien_vive_joel", "numero_de_hijos_mod_joel", "condicion_ocupacional_corr", "dias_treat_imp_sin_na", "cnt_mod_cie_10_or", "tipo_de_plan_2_mod", "comorbidity_icd_10", "compromiso_biopsicosocial", "tenencia_de_la_vivienda_mod","tipo_centro","macrozona","compromiso_biopsicosocial")

missing.values<-
prueba2 %>%
  rowwise %>%
  dplyr::mutate_at(.vars = vars(vector_variables),
                   .funs = ~ifelse(is.na(.), 1, 0)) %>% 
  dplyr::ungroup() %>% 
  dplyr::summarise_at(vars(vector_variables),~sum(.))
#t(missing.values)

miss_val_bar<-
data.table::melt(missing.values) %>% 
    mutate(perc=scales::percent(value/nrow(prueba2))) %>% 
    arrange(desc(perc))

plot_miss<-
missing.values %>%
  data.table::melt() %>%  #condicion_ocupacional_corr
  dplyr::filter(!variable %in% c("row", "hash_key", "dias_treat_imp_sin_na", "dup")) %>% 
  dplyr::mutate(perc= value/sum(nrow(prueba2))) %>% 
  dplyr::mutate(label_text= paste0("Variable= ",variable,"<br>n= ",value,"<br>",scales::percent(round(perc,3)))) %>%
  dplyr::mutate(perc=perc*100) %>% 
  ggplot() +
  geom_bar(aes(x=factor(variable), y=perc,label= label_text), stat = 'identity') +
  sjPlot::theme_sjplot()+
#  scale_y_continuous(limits=c(0,1), labels=percent)+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, size=9))+
  labs(x=NULL, y="% of Missing Values", caption=paste0("Nota. Percentage of missing values (n= ",format(nrow(prueba2),big.mark=","),")"))

  ggplotly(plot_miss, tooltip = c("label_text"))%>% layout(xaxis= list(showticklabels = T), height = 600, width=800) %>%   layout(yaxis = list(tickformat='%',  range = c(0, 15)))

Figure 3. Bar plot of Percentage of Missing Values per Variables at Basline

  #</div>














From the figure above, we could see that the Age of Onset of Drug Use (edad_ini_cons), Starting substance (sus_ini_mod_mvv), the Substance Tenure status of households (tenencia_de_la_vivienda_mod) and the Biopsychosocial Involvement (compromiso_biopsicosocial) had a proportion of missing values, but no greater than 5%. This is why we imputed these values under MAR assumption.


vector_variables_only_for_imputation<-
c("row","hash_key", "edad_al_ing", "sexo_2", "escolaridad_rec", "estado_conyugal_2", "edad_ini_cons", "sus_ini_mod_mvv", "freq_cons_sus_prin", "condicion_ocupacional_corr", "via_adm_sus_prin_act", "con_quien_vive_joel", "numero_de_hijos_mod_joel", "condicion_ocupacional_corr", "dias_treat_imp_sin_na", "tipo_de_plan_2_mod", "comorbidity_icd_10", "compromiso_biopsicosocial", "tenencia_de_la_vivienda_mod", "tipo_centro", "macrozona", "compromiso_biopsicosocial")#cnt_mod_cie_10_or", 

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

prueba2_miss<-
prueba2 %>% 
    #dplyr::group_by(hash_key) %>% 
    #dplyr::mutate(rn=row_number()) %>% 
    #dplyr::ungroup() %>% 
  
  #:#:#:#:#:#:#:#:#:#:#:
  # ORDINALIZAR LAS VARIABLES ORDINALES: 
  dplyr::select_(.dots = vector_variables_only_for_imputation) %>% 
  dplyr::mutate(numero_de_hijos_mod_joel=dplyr::case_when(numero_de_hijos_mod_joel>3~"4 or more",
                                                          T~as.character(numero_de_hijos_mod_joel))) %>% 
  #dplyr::select(-hash_key) %>% 
  as.data.frame()
  
#CONS_C1_df_dup_SEP_2020 %>% janitor::tabyl(evaluacindelprocesoteraputico) 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
time_before_missforest<-Sys.time()


# for (var in colnames(prueba2_miss)) {
#   attr(prueba2_miss[[deparse(as.name(var))]], "label") <- NULL
# }


library(missRanger)
set.seed(2125)
prueba2_imp <- missRanger(
                prueba2_miss, 
                formula = . ~ . - row - hash_key,
                num.trees = 200, 
                returnOOB=T,
                maxiter=50,
                verbose = 2, 
                seed = 2125)


time_after_missforest<-Sys.time()

paste0("Time in imputation process: ");time_after_missforest-time_before_missforest


ggplotly(DataExplorer::plot_missing(prueba2,missing_only = T))
Figure 4. Plot of missing values in the original sample

Figure 4. Plot of missing values in the original sample

Figure 4. Plot of missing values in the original sample


Export

#table(prueba2$macrozona,exclude=NULL)
vector_var_names<-c("hash_key", "edad_al_ing", "fech_ing", "fech_egres_imp", "event", "diff_bet_treat",
                    "tipo_de_programa_2", "abandono_temprano")
  
prueba2_imp%>%
  dplyr::arrange(hash_key, edad_al_ing)%>% 
  dplyr::left_join(prueba2[,vector_var_names], by=c("hash_key"="hash_key", "edad_al_ing"="edad_al_ing")) %>% 
    rio::export(file = paste0(gsub("analisis_joel2.Rmd","",path),"CONS_C1_df_dup_SEP_2020_joel_oct_2021.dta"))

if(no_mostrar==1){
          #23,979
        missing.values2<-
            prueba2 %>%
            rowwise %>%
            dplyr::mutate_all(~ifelse(is.na(.), 1, 0)) %>% 
            dplyr::ungroup() %>% 
            dplyr::summarise_all(~sum(.))
        
        invisible("Para ver los nombres de las variables")
        for(i in 1:length(prueba)){
        print(paste0(names(prueba)[[i]],"= ",attr(prueba[[i]],"label")))
          }
}  


Session Info

Sys.getenv("R_LIBS_USER")
## [1] "C:/Users/CISS Fondecyt/OneDrive/Documentos/R/win-library/4.0"
rstudioapi::getSourceEditorContext()
## Document Context: 
## - id:        'FBC93C63'
## - path:      'C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2019 (github)/analisis_joel2.Rmd'
## - contents:  <1139 rows>
## Document Selection:
## - [1115, 60] -- [1115, 60]: ''
sessionInfo()
## R version 4.0.2 (2020-06-22)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 19042)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=Spanish_Chile.1252  LC_CTYPE=Spanish_Chile.1252   
## [3] LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C                  
## [5] LC_TIME=Spanish_Chile.1252    
## 
## attached base packages:
## [1] stats     graphics  grDevices datasets  utils     methods   base     
## 
## other attached packages:
##  [1] missRanger_2.1.3    readr_2.0.1         DataExplorer_0.8.2 
##  [4] rio_0.5.27          data.table_1.14.2   Amelia_1.8.0       
##  [7] Rcpp_1.0.7          plotly_4.9.4.1      compareGroups_4.5.1
## [10] ggplot2_3.3.5       dplyr_1.0.7        
## 
## loaded via a namespace (and not attached):
##   [1] minqa_1.2.4            TH.data_1.0-10         colorspace_1.4-1      
##   [4] ggsignif_0.6.2         ellipsis_0.3.2         sjlabelled_1.1.8      
##   [7] estimability_1.3       snakecase_0.11.0       flextable_0.6.7       
##  [10] parameters_0.14.0      base64enc_0.1-3        rstudioapi_0.13       
##  [13] mice_3.13.0            ggpubr_0.4.0           farver_2.0.3          
##  [16] fansi_0.4.1            mvtnorm_1.1-2          lubridate_1.7.10      
##  [19] ranger_0.13.1          xml2_1.3.2             codetools_0.2-16      
##  [22] splines_4.0.2          knitr_1.33             sjmisc_2.8.7          
##  [25] jsonlite_1.7.2         nloptr_1.2.2.2         ggeffects_1.1.1       
##  [28] broom_0.7.9            km.ci_0.5-2            effectsize_0.4.5      
##  [31] compiler_4.0.2         httr_1.4.2             sjstats_0.18.1        
##  [34] emmeans_1.6.3          backports_1.1.7        assertthat_0.2.1      
##  [37] Matrix_1.2-18          lazyeval_0.2.2         cli_3.0.1             
##  [40] htmltools_0.5.1.1      tools_4.0.2            igraph_1.2.6          
##  [43] coda_0.19-4            gtable_0.3.0           glue_1.4.1            
##  [46] reshape2_1.4.4         carData_3.0-4          cellranger_1.1.0      
##  [49] jquerylib_0.1.4        vctrs_0.3.8            sjPlot_2.8.9          
##  [52] writexl_1.3            nlme_3.1-148           svglite_2.0.0         
##  [55] crosstalk_1.1.1        insight_0.14.3         xfun_0.25             
##  [58] stringr_1.4.0          networkD3_0.4          lme4_1.1-27.1         
##  [61] openxlsx_4.2.4         rvest_1.0.1            lifecycle_1.0.0       
##  [64] rstatix_0.7.0          MASS_7.3-51.6          zoo_1.8-9             
##  [67] scales_1.1.1           hms_1.1.0              parallel_4.0.2        
##  [70] sandwich_3.0-1         HardyWeinberg_1.7.2    yaml_2.2.1            
##  [73] curl_4.3               gridExtra_2.3          KMsurv_0.1-5          
##  [76] gdtools_0.2.3          sass_0.4.0             stringi_1.4.6         
##  [79] bayestestR_0.10.5      highr_0.9              boot_1.3-28           
##  [82] zip_2.2.0              truncnorm_1.0-8        chron_2.3-56          
##  [85] rlang_0.4.11           pkgconfig_2.0.3        systemfonts_1.0.2     
##  [88] Rsolnp_1.16            ggiraph_0.7.10         evaluate_0.14         
##  [91] lattice_0.20-41        purrr_0.3.4            htmlwidgets_1.5.3.9000
##  [94] labeling_0.4.2         tidyselect_1.1.1       plyr_1.8.6            
##  [97] magrittr_2.0.1         R6_2.5.1               generics_0.1.0        
## [100] multcomp_1.4-17        DBI_1.1.1              pillar_1.6.2          
## [103] haven_2.4.3            foreign_0.8-81         withr_2.4.2           
## [106] datawizard_0.2.0       survival_3.1-12        abind_1.4-5           
## [109] performance_0.7.3      tibble_3.0.3           modelr_0.1.8          
## [112] janitor_2.1.0          crayon_1.4.1           car_3.0-11            
## [115] survMisc_0.5.5         uuid_0.1-4             utf8_1.1.4            
## [118] tzdb_0.1.2             rmarkdown_2.10         officer_0.3.19        
## [121] grid_4.0.2             readxl_1.3.1           FNN_1.1.3             
## [124] forcats_0.5.1          digest_0.6.25          webshot_0.5.2         
## [127] xtable_1.8-4           tidyr_1.1.3            munsell_0.5.0         
## [130] viridisLite_0.4.0      kableExtra_1.3.4       survminer_0.4.9       
## [133] bslib_0.2.5.1
save.image("__analisis_joel.RData")
#load("E:/Mi unidad/Alvacast/SISTRAT 2019 (github)/__analisis_joel.RData")

unlink("*_cache", recursive = T, force = T, expand = TRUE)