Living Conditions and Dropout

Show code
#https://www.ncbi.nlm.nih.gov/pmc/articles/PMC6771712/bin/SIM-38-3896-s002.pdf
# Cumulative incidence using two cause-specific models
cumIncidence.CS <- function(model1,model2,time.max,subdiv,
data.val=data.frame(.NotUsed=NA),alpha=0.05){
time.pts <- seq(0,time.max,le=(subdiv+1))
CstMult <- time.max/(2*subdiv)
CstCI <- qnorm(1-alpha/2)
# Or if you want to take into account the size of the population
# CstCI <- qt(1-alpha/2,df=model£n.obs)
Pred1 <- predict(model1,time.pts,data.val, include.gradient=T)
Pred2 <- predict(model2,time.pts,data.val, include.gradient=T)
ISurv1 <- Pred1$results$hazard*Pred1$results$surv*Pred2$results$surv
CPr.1 <- cumsum(ISurv1+c(0,ISurv1[-subdiv]))*(time.max/(2*subdiv))
ISurv2 <- Pred2$results$hazard*Pred1$results$surv*Pred2$results$surv
CPr.2 <- cumsum(ISurv2+c(0,ISurv2[-subdiv]))*(time.max/(2*subdiv))
ISurvT <- (Pred1$results$hazard+Pred2$results$hazard)*Pred1$results$surv*
Pred2$results$surv
CPrT<- cumsum(ISurvT+c(0,ISurvT[-subdiv]))*(time.max/(2*subdiv))
SurvT <- 1-CPrT
# Confidence intervals
which.td1<- rownames(Pred1$vcov)[-c(1,which(rownames(Pred1$vcov)
%in% model1$names.ph))]
which.ntd1 <- c(rownames(Pred1$vcov)[-c(which(rownames(Pred1$vcov)
%in% model1$names.ph))][1],
rownames(Pred1$vcov)[which(rownames(Pred1$vcov) %in%
model1$names.ph)])
which.td2<- rownames(Pred2$vcov)[-c(1,which(rownames(Pred2$vcov) %in%
model2$names.ph))]
which.ntd2 <- c(rownames(Pred2$vcov)[-c(which(rownames(Pred2$vcov) %in%
model2$names.ph))][1],
rownames(Pred2$vcov)[which(rownames(Pred2$vcov) %in%
model2$names.ph)])
Vcov1 <- model1$vcov[c(which.ntd1,which.td1),c(which.ntd1,which.td1)]
Vcov2 <- model2$vcov[c(which.ntd2,which.td2),c(which.ntd2,which.td2)]
CovMat <- as.matrix(bdiag(Vcov1,Vcov2))
AGrad11 <- (Pred1$grad.loghaz + Pred1$grad.logcum*(log(Pred1$results$surv)))
[,c(which.ntd1,which.td1)]
AGrad12 <- (Pred2$grad.logcum*(log(Pred2$results$surv)))
[,c(which.ntd2,which.td2)]
AGrad21 <- (Pred1$grad.logcum*(log(Pred1$results$surv)))
[,c(which.ntd1,which.td1)]

AGrad22 <- (Pred2$grad.loghaz + Pred2$grad.logcum*(log(Pred2$results$surv)))
[,c(which.ntd2,which.td2)]
AGrad1 <- cbind(AGrad11,AGrad12)
AGrad2 <- cbind(AGrad21,AGrad22)
AGradT <- cbind(AGrad21,AGrad12)
Temp1 <- ISurv1*AGrad1
Denom1 <- (1-CPr.1)*log(1-CPr.1)
BGrad1 <- apply(Temp1,2,function(x) cumsum(x+c(0,x[-subdiv]))*
(time.max/(2*subdiv)))
TMatVar1 <- CovMat%*%t(BGrad1)
Var1 <- sapply(1:subdiv,function(i) BGrad1[i,]%*%TMatVar1[,i])
Var1b <- Var1/((1-CPr.1)*log(1-CPr.1))^2
Temp2 <- ISurv2*AGrad2
Denom2 <- (1-CPr.2)*log(1-CPr.2)^2
BGrad2 <- apply(Temp2,2,function(x) cumsum(x+c(0,x[-subdiv]))*
(time.max/(2*subdiv)))
TMatVar2 <- CovMat%*%t(BGrad2)
Var2 <- sapply(1:subdiv,function(i) BGrad2[i,]%*%TMatVar2[,i])
Var2b <- Var2/((1-CPr.2)*log(1-CPr.2))^2
BGradT <- AGradT/log(SurvT)
TMatVarT <- CovMat%*%t(BGradT)
VarT <- sapply(1:subdiv,function(i) BGradT[i,]%*%TMatVarT[,i])
Transf <- function(x,vx,m){
log(-log(x))+m*CstCI*sqrt(vx)
}
InvTransf <- function(x){
exp(-exp(x))
}
Cr1Lo <- 1-InvTransf(Transf(1-CPr.1,Var1b,-1))
Cr1Up <- 1-InvTransf(Transf(1-CPr.1,Var1b,1))
Cr2Lo <- 1-InvTransf(Transf(1-CPr.2,Var2b,-1))
Cr2Up <- 1-InvTransf(Transf(1-CPr.2,Var2b,1))
CrTLo <- 1-InvTransf(Transf(SurvT,VarT,-1))
CrTUp <- 1-InvTransf(Transf(SurvT,VarT,1))
return(list("time"= time.pts,
"frag"=subdiv,
"CPr.1"=CPr.1,"CPr.2"=CPr.2,"CovMat"=CovMat,
"BGrad1"=BGrad1,"BGrad2"=BGrad2,"CPr1Lo"= Cr1Lo,
"CPr1Up"=Cr1Up,"CPr2Lo"= Cr2Lo,"CPr2Up"=Cr2Up,
"Var1"=Var1, "Var2"=Var2,"Var1b"=Var1b,"Var2b"=Var2b))
}
#Functions for prediction
csprob<-function(data_df, modA, modB, time.max, frag){
results_list<- list()
for (y in 1:nrow(data_df)){
results_list[[y]] <- cumIncidence.CS(modA, modB,time.max,
subdiv=frag,
data.val=data_df[y,])
}
return(results_list)
}
BGrad_func <- function(BGrad.ls,frag,N, p_dim){
BGrad.3da <- array(unlist(BGrad.ls),dim = c(frag,p_dim,N))
BGrad.3d <- aperm(BGrad.3da,dim=c(3,1,2))
NBGrad.ls <- alply(BGrad.3d,3)
return(NBGrad.ls)
}
Var_func <- function (x,w,NBGrad, CovMat){
BGrad<- w%*%NBGrad[[x]]
TMatVar <- CovMat%*%t(BGrad)
res<- BGrad%*%TMatVar
return(res)
}
Transf <- function(x,vx,m){log(-log(x))+m*qnorm(0.975)*sqrt(vx)}
InvTransf <- function(x){exp(-exp(x))}
predict.prob <- function(csprobObj, pop){
CovMat <- csprobObj[[1]]$CovMat
p_dim <- dim(csprobObj[[1]]$CovMat)[1]
N <- length(csprobObj)
frag <- csprobObj[[1]]$frag
if (pop==FALSE){
BGrad1.ls <- lapply(1:N, function(x) csprobObj[[x]]$BGrad1)
BGrad2.ls <- lapply(1:N, function(x) csprobObj[[x]]$BGrad2)
CPr1.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr.1)
CPr2.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr.2)
CPr1Lo.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr1Lo)
CPr1Up.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr1Up)
CPr2Lo.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr2Lo)
CPr2Up.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr2Up)
return(list("time"=csprobObj[[1]]$time,"frag"=frag,
"BGrad1.ls"=BGrad1.ls, "BGrad2.ls"=BGrad2.ls,
"CPr1Lo.df"=CPr1Lo.df,"CPr1Up.df"=CPr1Up.df,
"CPr2Lo.df"=CPr2Lo.df,"CPr2Up.df"=CPr2Up.df,
"CPr1.df"=CPr1.df,"CPr2.df"=CPr2.df))
}
if (pop==TRUE){
w <- matrix(rep(1/N,N), nrow =1)

BGrad1.ls<- lapply(1:N, function(x) csprobObj[[x]]$BGrad1)
BGrad2.ls<- lapply(1:N, function(x) csprobObj[[x]]$BGrad2)
CPr1.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr.1)
CPr2.df <- sapply(1:N, function(x) csprobObj[[x]]$CPr.2)
CPr.1 <- w%*%t(CPr1.df)
CPr.2 <- w%*%t(CPr2.df)
NBGrad1.ls <- BGrad_func(BGrad1.ls,frag,N,p_dim)
NBGrad2.ls <- BGrad_func(BGrad2.ls,frag,N,p_dim)
Var1 <- data.frame(sapply(1:frag, function(x)
Var_func(x,w,NBGrad1.ls, CovMat) ))
Var1b <- Var1/((1-CPr.1)*log(1-CPr.1))^2
CPr1Lo <- 1-InvTransf(Transf(1-CPr.1,Var1b,-1))
CPr1Up <- 1-InvTransf(Transf(1-CPr.1,Var1b,1))
Var2 <- data.frame(sapply(1:frag, function(x)
Var_func(x,w,NBGrad2.ls, CovMat) ))
Var2b <- Var2/((1-CPr.2)*log(1-CPr.2))^2
CPr2Lo <- 1-InvTransf(Transf(1-CPr.2,Var2b,-1))
CPr2Up <- 1-InvTransf(Transf(1-CPr.2,Var2b,1))
return(list("time"=csprobObj[[1]]$time[-1],"frag"=frag,
"BGrad1.ls"=BGrad1.ls, "BGrad2.ls"=BGrad2.ls,
"CPr.1"=CPr.1[1,],"CPr.2"=CPr.2[1,],"NBGrad1.ls"=NBGrad1.ls,
"NBGrad2.ls"=NBGrad2.ls,"Var1"=Var1,"Var2"=Var2,
"Var1b"=Var1b,"Var2b"=Var2b,
"CPr1Lo"=CPr1Lo[,1],"CPr1Up"=CPr1Up[,1],
"CPr2Lo"=CPr2Lo[,1],"CPr2Up"=CPr2Up[,1],
"CPr1.df"=CPr1.df,"CPr2.df"=CPr2.df,"CovMat"=CovMat))
}
}
# Calculate differences between populations
csprobdif<- function(predprob1, predprob2){
N <- ncol(predprob1$CPr1.df)
w<- matrix(rep(1/N,N), nrow =1)
frag <- predprob1$frag
CovMat <- predprob1$CovMat
NBGrad1.ls <- lapply(1:frag, function (x) predprob1$NBGrad1.ls[[x]]-
predprob2$NBGrad1.ls[[x]])
NBGrad2.ls <- lapply(1:frag, function (x) predprob1$NBGrad2.ls[[x]]-
predprob2$NBGrad2.ls[[x]])
CP1.dif_df <- predprob1$CPr1.df-predprob2$CPr1.df
CP2.dif_df <- predprob1$CPr2.df-predprob2$CPr2.df

CP1.dif <-apply(CP1.dif_df,1,mean)
CP2.dif<-apply(CP2.dif_df,1,mean)
ci_form <- function(x,var,m){
x+m*qnorm(0.975)*sqrt(var)
}
Var1 <-data.frame(sapply(1:frag, function(x)
Var_func(x,w,NBGrad1.ls,CovMat)))
Var2 <-data.frame(sapply(1:frag, function(x)
Var_func(x,w,NBGrad2.ls,CovMat)))
CPr1.difLo <- ci_form(CP1.dif,Var1,-1)[,1]
CPr1.difUp <- ci_form(CP1.dif,Var1,+1)[,1]
CPr2.difLo <- ci_form(CP2.dif,Var2,-1)[,1]
CPr2.difUp <- ci_form(CP2.dif,Var2,+1)[,1]
return(list("time"= predprob1$time,
"ProbDif1"=CP1.dif, "ProbDif2"=CP2.dif,
"ProbDif1Lo"=CPr1.difLo,"ProbDif1Up"=CPr1.difUp,
"ProbDif2Lo"=CPr2.difLo,"ProbDif2Up"=CPr2.difUp))
}

Time for this code chunk to run: 0 minutes

Consolidate the database


We generated the database with the new requested variables. Must note that this database already filtererd minors at admission but people that did not reported a valid age (remember differences with Stata).


Show code
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"))) 

Time for this code chunk to run: 0.3 minutes


Updated in 2021 June & August

We only left users in treatments in General Population (PG) and missing values and discarded under 18 years (n= 84,944). Additionally, we recategorized the variable ‘Living with’ (con_quien_vive).


Show code
#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)
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()

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

Time for this code chunk to run: 0 minutes


We ended having 23,979 users.

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.



Total

Show code
#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= "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::kable_classic() %>% 
  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%)
Cocaine hydrochloride 934 (3.90%)
Cocaine paste 1054 (4.40%)
Marijuana 9165 (38.2%)
Other 411 (1.71%)
‘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 (%)

Time for this code chunk to run: 0.1 minutes


Primary substance

Show code
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= "Summary descriptives 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::kable_classic() %>% 
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 2: Summary descriptives 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%)
Cocaine hydrochloride 44 (1.06%) 604 (11.2%) 22 (0.77%) 4 (1.36%) 260 (2.31%)
Cocaine paste 28 (0.68%) 57 (1.06%) 32 (1.11%) 2 (0.68%) 935 (8.29%)
Marijuana 513 (12.4%) 1913 (35.5%) 1490 (51.9%) 121 (41.0%) 5128 (45.5%)
Other 43 (1.04%) 65 (1.21%) 41 (1.43%) 65 (22.0%) 197 (1.75%)
‘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 (%)

Time for this code chunk to run: 0.2 minutes

Living with

Show code
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= "Summary descriptives 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::kable_classic() %>% 
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 3: Summary descriptives 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%)
Cocaine hydrochloride 37 (2.77%) 548 (3.55%) 349 (4.85%)
Cocaine paste 68 (5.09%) 636 (4.12%) 350 (4.87%)
Marijuana 511 (38.3%) 6157 (39.8%) 2497 (34.7%)
Other 26 (1.95%) 250 (1.62%) 135 (1.88%)
‘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 (%)

Time for this code chunk to run: 0.1 minutes

Readmissions

Show code
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 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::kable_classic() %>% 
  kableExtra::scroll_box(width = "100%", height = "600px")
(#tab:tab_mas_8)Table 5. Summary descriptives 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%)
Cocaine hydrochloride 688 (3.72%) 246 (4.47%)
Cocaine paste 667 (3.61%) 387 (7.03%)
Marijuana 6926 (37.5%) 2239 (40.7%)
Other 298 (1.61%) 113 (2.05%)
‘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 (%)

Time for this code chunk to run: 0.4 minutes

Show code
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"))
Figura 1. Días de Tratamiento, según la posición del tratamiento en cada usuario usuario

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

Time for this code chunk to run: 0 minutes

Show code
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"))
Figura 2. Diferencia en Días con el Tratamiento Siguiente, según la posición del tratamiento en cada usuario usuario

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

Time for this code chunk to run: 0 minutes

Show code
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"))

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

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

Show code
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()
}

Time for this code chunk to run: 0 minutes


Imputation (October 2021)


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


Show code
#<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') +
  theme_classic()+
#  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 4: Bar plot of Percentage of Missing Values per Variables at Basline

Show code
  #</div>

Time for this code chunk to run: 0.3 minutes














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.


Show code
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", "dg_trs_cons_sus_or", "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", "sus_ini_mod")#cnt_mod_cie_10_or", 
#prueba2_imp 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

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,
                pmm.k=7,
                num.trees = 1e3, 
                returnOOB=T,
                maxiter= 150,
                verbose = 2, 
                seed = 2125)


time_after_missforest<-Sys.time()

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

Time for this code chunk to run: 0 minutes


Show code
ggplotly(DataExplorer::plot_missing(prueba2,missing_only = T))

Time for this code chunk to run: 0 minutes


Descriptives

Show code
attr(prueba2_imp$numero_de_hijos_mod_joel,"label") <-"Number of Children (Max. Value), adding 1 if pregnant at admission"
attr(prueba2$cnt_mod_cie_10_or,"label") <- "Total count of Psychiatric Diagnostics"
attr(prueba2_imp$escolaridad_rec,"label") <- "Educational Attainment"

table6_imp <- compareGroups::compareGroups(no_group ~ sexo_2+ dg_trs_cons_sus_or+ 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_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,
                                                 dg_trs_cons_sus_or=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,
                                                 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 = cbind.data.frame(prueba2_imp,
                                                  dplyr::select(prueba2,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),
                        #
                                                  dplyr::select(prueba2,sus_principal_mod, cat_ocupacional_corr, cnt_diagnostico_trs_fisico, cnt_otros_probl_at_sm_or, dg_cie_10_rec, abandono_temprano, 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, n_treats, sex_abuse, dom_violence))%>% dplyr::mutate(no_group=1),
                                       include.miss = T,
                                       var.equal=T
)

pvals <- getResults(table6_imp)

restab6_imp <- createTable(table6_imp,show.p.overall = F)
compareGroups::export2md(restab6_imp, size=9, first.strip=T, hide.no="no", position="center",col.names=c("Variables","Total"),
                         format="html",caption= "Summary descriptives")%>%
  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::kable_classic() %>% 
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 4: Summary descriptives
Variables Total
N=23979
Sexo Usuario/Sex of User:
Men 19850 (82.8%)
Women 4129 (17.2%)
Diagnósico de Trastorno por Consumo de Sustancias(d)/Diagnosed of Substance Use Disorder(d):
Drug dependence 17137 (71.5%)
Hazardous consumption 6842 (28.5%)
Educational Attainment:
3-Completed primary school or less 5642 (23.5%)
2-Completed high school or less 14582 (60.8%)
1-More than high school 3755 (15.7%)
Estado Conyugal/Marital Status:
Married/Shared living arrangements 5136 (21.4%)
Separated/Divorced 501 (2.09%)
Single 18312 (76.4%)
Widower 30 (0.13%)
Biopsychosocial Compromise:
1-Mild 2340 (9.76%)
2-Moderate 14110 (58.8%)
3-Severe 7529 (31.4%)
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 11513 (48.0%)
Cocaine hydrochloride 934 (3.90%)
Cocaine paste 1054 (4.40%)
Marijuana 10067 (42.0%)
Other 411 (1.71%)
Starting Substance:
Alcohol 11354 (47.3%)
Cocaine hydrochloride 970 (4.05%)
Marijuana 10157 (42.4%)
Other 398 (1.66%)
Cocaine paste 1100 (4.59%)
freq_cons_sus_prin:
1 day a week or more 1638 (6.83%)
2 to 3 days a week 7099 (29.6%)
4 to 6 days a week 4105 (17.1%)
Daily 9971 (41.6%)
Less than 1 day a week 1166 (4.86%)
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 13903 (58.0%)
Intranasal (powder aspiration) 5575 (23.2%)
Injected Intravenously or Intramuscularly 16 (0.07%)
Oral (drunk or eaten) 4473 (18.7%)
Other 12 (0.05%)
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:
0 9779 (40.8%)
1 8557 (35.7%)
2 3984 (16.6%)
3 1221 (5.09%)
4 or more 438 (1.83%)
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 9288 (38.7%)
PAI 11400 (47.5%)
PR 3291 (13.7%)
Tenure status of households:
Illegal Settlement 213 (0.89%)
Others 697 (2.91%)
Owner/Transferred dwellings/Pays Dividends 7702 (32.1%)
Renting 4094 (17.1%)
Stays temporarily with a relative 11273 (47.0%)
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 Diagnostics:
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 (%)

Time for this code chunk to run: 0 minutes


Living with

Show code
table5_imp <- 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+ numero_de_hijos_mod_joel_bin+ 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_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+ dg_trs_cons_sus_or,
                                       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,
                                                 numero_de_hijos_mod_joel_bin=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,
                                                 dg_trs_cons_sus_or=3,
                                                 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,
                                                 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 = cbind.data.frame(prueba2_imp,
                                                  dplyr::select(prueba2,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),
                        #
                                                  dplyr::select(prueba2,sus_principal_mod, cat_ocupacional_corr, cnt_diagnostico_trs_fisico, cnt_otros_probl_at_sm_or, dg_cie_10_rec, abandono_temprano, 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, n_treats, sex_abuse, dom_violence)) %>% 
                          dplyr::mutate(numero_de_hijos_mod_joel_bin=dplyr::case_when(numero_de_hijos_mod_joel=="0"~"0",
                                                                                 !is.na(numero_de_hijos_mod_joel)~"One or more (or pregnant)")),
                                       include.miss = T,
                                       var.equal=T,
                                       max.xlev = 10,
                                       max.ylev = 10
)#cie_10 cat_ocupacional estatus_ocupacional

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
if(no_mostrar==1){
  
  catVars_after_imp<-
cbind.data.frame(prueba2_imp,
                 dplyr::select(prueba2,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),
                 #
                 dplyr::select(prueba2,sus_principal_mod, cat_ocupacional_corr, cnt_diagnostico_trs_fisico, cnt_otros_probl_at_sm_or, dg_cie_10_rec, abandono_temprano, duplicates_filtered, max_cum_dias_trat_sin_na, max_cum_diff_bet_treat, 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, n_treats, sex_abuse, dom_violence)) %>% 
    dplyr::mutate(numero_de_hijos_mod_joel_bin=dplyr::case_when(numero_de_hijos_mod_joel=="0"~"0",
                                                                !is.na(numero_de_hijos_mod_joel)~"One or more (or pregnant)")) %>% 
    dplyr::select_if(~!is.numeric(.x)) %>% names()
#prueba
cbind.data.frame(prueba2_imp,
                                                  dplyr::select(prueba2,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),
                        #
                                                  dplyr::select(prueba2,sus_principal_mod, cat_ocupacional_corr, cnt_diagnostico_trs_fisico, cnt_otros_probl_at_sm_or, dg_cie_10_rec, abandono_temprano, duplicates_filtered, max_cum_dias_trat_sin_na, max_cum_diff_bet_treat, 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, n_treats, sex_abuse, dom_violence)) %>% 
                          dplyr::mutate(numero_de_hijos_mod_joel_bin=dplyr::case_when(numero_de_hijos_mod_joel=="0"~"0",
                                                                                 !is.na(numero_de_hijos_mod_joel)~"One or more (or pregnant)")) %>% 
    dplyr::select(catVars_after_imp) %>% 
    tidyr::gather(variable,measure, -con_quien_vive_joel) %>% 
    group_by(variable) %>%
    do(chisq.test(.$con_quien_vive_joel, .$measure) %>% broom::tidy()) %>%
    dplyr::mutate(p.value=ifelse(p.value<.001,"<0.001",sprintf("%1.3f",p.value))) %>% 
    dplyr::mutate(statistic=sprintf("%2.2f",statistic)) %>% 
    dplyr::mutate(report=paste0("X²(",parameter,", N=",nrow(prueba2),")=",statistic,ifelse(p.value=="<0.001",p.value, paste0("=",p.value)))) %>% copiar_nombres()
}

if(no_mostrar==1){
  
  invisible("Prueba post-revisión para sacar el wilcox y test de asociaciones")
broom::tidy(wilcox.test(dias_treat_imp_sin_na ~ tipo_de_programa_2, data=CONS_C1_df_dup_SEP_2020_women_miss_after_imp_conservados)) %>% dplyr::mutate(report=paste0("W"))
  
#effect size
broom::tidy(wilcox.test(dias_treat_imp_sin_na ~ tipo_de_programa_2, data=CONS_C1_df_dup_SEP_2020_women_miss_after_imp_conservados))$statistic/sqrt(nrow(CONS_C1_df_dup_SEP_2020_women_miss_after_imp_conservados))
  
broom::tidy(t.test(dias_treat_imp_sin_na ~ tipo_de_programa_2, data=CONS_C1_df_dup_SEP_2020_women_miss_after_imp_conservados)) %>% 
  dplyr::mutate(report=paste0("t(",round(parameter,0),")=",sprintf("%1.2f",statistic),",",ifelse(p.value<0.001,"<0.001", paste0("=",sprinf("f1.3f",p.value)))))
  # t(19) = 3.1, p = .006.
}
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
pvals <- getResults(table5_imp)
#p.adjust(pvals, method = "BH")
restab5_imp <- createTable(table5_imp, show.p.overall = T)
compareGroups::export2md(restab5_imp, 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= "Summary descriptives by With whom they live (with some imputed variables")%>%
  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::kable_classic() %>% 
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 5: Summary descriptives by With whom they live (with some imputed variables
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%)
Educational Attainment: <0.001
3-Completed primary school or less 360 (27.0%) 3335 (21.6%) 1947 (27.1%)
2-Completed high school or less 773 (57.9%) 9457 (61.2%) 4352 (60.5%)
1-More than high school 202 (15.1%) 2663 (17.2%) 890 (12.4%)
Biopsychosocial Compromise: <0.001
1-Mild 92 (6.89%) 1397 (9.04%) 851 (11.8%)
2-Moderate 646 (48.4%) 8958 (58.0%) 4506 (62.7%)
3-Severe 597 (44.7%) 5100 (33.0%) 1832 (25.5%)
Estado Conyugal/Marital Status: .
Married/Shared living arrangements 101 (7.57%) 858 (5.55%) 4177 (58.1%)
Separated/Divorced 55 (4.12%) 361 (2.34%) 85 (1.18%)
Single 1178 (88.2%) 14212 (92.0%) 2922 (40.6%)
Widower 1 (0.07%) 24 (0.16%) 5 (0.07%)
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 649 (48.6%) 7287 (47.1%) 3577 (49.8%)
Cocaine hydrochloride 37 (2.77%) 548 (3.55%) 349 (4.85%)
Cocaine paste 68 (5.09%) 636 (4.12%) 350 (4.87%)
Marijuana 555 (41.6%) 6734 (43.6%) 2778 (38.6%)
Other 26 (1.95%) 250 (1.62%) 135 (1.88%)
Starting Substance: <0.001
Alcohol 643 (48.2%) 7167 (46.4%) 3544 (49.3%)
Cocaine hydrochloride 39 (2.92%) 571 (3.69%) 360 (5.01%)
Marijuana 558 (41.8%) 6805 (44.0%) 2794 (38.9%)
Other 24 (1.80%) 245 (1.59%) 129 (1.79%)
Cocaine paste 71 (5.32%) 667 (4.32%) 362 (5.04%)
freq_cons_sus_prin: <0.001
1 day a week or more 55 (4.12%) 919 (5.95%) 664 (9.24%)
2 to 3 days a week 329 (24.6%) 4359 (28.2%) 2411 (33.5%)
4 to 6 days a week 204 (15.3%) 2737 (17.7%) 1164 (16.2%)
Daily 702 (52.6%) 6807 (44.0%) 2462 (34.2%)
Less than 1 day a week 45 (3.37%) 633 (4.10%) 488 (6.79%)
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%) 9267 (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) 329 (24.6%) 2664 (17.2%) 1480 (20.6%)
Other 1 (0.07%) 7 (0.05%) 4 (0.06%)
Number of Children (Max. Value), adding 1 if pregnant at admission: 0.000
0 586 (43.9%) 8138 (52.7%) 1055 (14.7%)
1 463 (34.7%) 4963 (32.1%) 3131 (43.6%)
2 184 (13.8%) 1731 (11.2%) 2069 (28.8%)
3 70 (5.24%) 477 (3.09%) 674 (9.38%)
4 or more 32 (2.40%) 146 (0.94%) 260 (3.62%)
numero_de_hijos_mod_joel_bin: 0.000
0 586 (43.9%) 8138 (52.7%) 1055 (14.7%)
One or more (or pregnant) 749 (56.1%) 7317 (47.3%) 6134 (85.3%)
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): <0.001
PAB 392 (29.4%) 5514 (35.7%) 3382 (47.0%)
PAI 586 (43.9%) 7514 (48.6%) 3300 (45.9%)
PR 357 (26.7%) 2427 (15.7%) 507 (7.05%)
Tenure status of households: 0.000
Illegal Settlement 75 (5.62%) 48 (0.31%) 90 (1.25%)
Others 95 (7.12%) 466 (3.02%) 136 (1.89%)
Owner/Transferred dwellings/Pays Dividends 298 (22.3%) 5448 (35.3%) 1956 (27.2%)
Renting 642 (48.1%) 1227 (7.94%) 2225 (31.0%)
Stays temporarily with a relative 225 (16.9%) 8266 (53.5%) 2782 (38.7%)
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 Diagnostics: .
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%)
Diagnósico de Trastorno por Consumo de Sustancias(d)/Diagnosed of Substance Use Disorder(d): <0.001
Drug dependence 984 (73.7%) 11282 (73.0%) 4871 (67.8%)
Hazardous consumption 351 (26.3%) 4173 (27.0%) 2318 (32.2%)
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 (%)
Show code
#janitor::tabyl(prueba2_imp$numero_de_hijos_mod_joel)

Time for this code chunk to run: 0.1 minutes

Outcomes

Univariate or Bivariate Analyses


Here, we compared the incidence rate of those who experienced 1, 2, 3, 1 & 2, 1 to 3, 4 or more, and dropouts in the first treatment, between different “living with” patterns.


Show code
invisible("https://cran.r-project.org/web/packages/riskCommunicator/vignettes/Vignette_newbieRusers.html#continuous-exposure-example")

prueba2_imp20<-
cbind.data.frame(dplyr::select(prueba2_imp, -dg_trs_cons_sus_or),
      dplyr::select(prueba2,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),
      dplyr::select(prueba2, fech_ing, fech_egres_imp, sus_principal_mod, cat_ocupacional_corr, cnt_diagnostico_trs_fisico, cnt_otros_probl_at_sm_or, dg_cie_10_rec, abandono_temprano, duplicates_filtered, max_cum_dias_trat_sin_na, max_cum_diff_bet_treat, 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, n_treats, sex_abuse, dom_violence, motivodeegreso_mod_imp, dg_trs_cons_sus_or)) %>% 
  dplyr::mutate(person_years=(as.numeric(as.Date("2019-11-13"))- as.numeric(fech_ing))/365.25,
                #2023-02-02
                event= dplyr::case_when(grepl("Drop-out|Administrative", motivodeegreso_mod_imp)~1, T~0),
                yrs_to_tr_dropout= dplyr::case_when(event==1~ dias_treat_imp_sin_na_1/365.25, T~ (as.numeric(as.Date("2019-11-13"))- as.numeric(fech_ing))/365.25),
                #2023-02-06
                event_cmprsk= dplyr::case_when(grepl("Drop-out|Administrative", motivodeegreso_mod_imp)~1, grepl("Therapeutic", motivodeegreso_mod_imp)~2, T~0),
                yrs_to_tr_dropout_cmprsk= dplyr::case_when(event%in% c(1,2)~ dias_treat_imp_sin_na_1/365.25, T~ (as.numeric(as.Date("2019-11-13"))- as.numeric(fech_ing))/365.25),
                max_cum_dias_trat_sin_na_adj= max_cum_dias_trat_sin_na/person_years,
                duplicates_filtered_adj= duplicates_filtered/person_years)#
#n_treats= readmissiones

tablas_inc<-
rbind(data.frame(biostat3::survRate(Surv(person_years, n_treats=="01") ~ con_quien_vive_joel, data=prueba2_imp20)),
      data.frame(biostat3::survRate(Surv(person_years, n_treats=="02") ~ con_quien_vive_joel, data=prueba2_imp20)),
      data.frame(biostat3::survRate(Surv(person_years, n_treats=="03") ~ con_quien_vive_joel, data=prueba2_imp20)),
      data.frame(biostat3::survRate(Surv(person_years, grepl("01|02",n_treats)) ~ con_quien_vive_joel, data=prueba2_imp20)),
      data.frame(biostat3::survRate(Surv(person_years, grepl("01|02|03",n_treats)) ~ con_quien_vive_joel, data=prueba2_imp20)),
      data.frame(biostat3::survRate(Surv(person_years, n_treats=="04 or more") ~ con_quien_vive_joel, data=prueba2_imp20)),
      data.frame(biostat3::survRate(Surv(person_years, grepl("Drop", motivodeegreso_mod_imp)) ~ con_quien_vive_joel, data=prueba2_imp20)))

data.table::data.table(tablas_inc, keep.rownames = F)%>% 
  dplyr::mutate(rate=round(rate*1000,0),lower=round(lower*1000,0), upper=round(upper*1000,0))%>% 
knitr::kable(format="html",caption= "Summary of incidence rates of events depending on whom patients live with")%>%
  kableExtra::add_footnote(c("Note. Events per 1,000 person-years"), notation = "none")%>%
  kableExtra::kable_classic() %>% 
  kableExtra::group_rows("Only one treatment",1,3)%>% 
  kableExtra::group_rows("Two treatments",4,6)%>%
  kableExtra::group_rows("Three treatment",7,9)%>% 
  kableExtra::group_rows("One & two treatments",10,12)%>%
  kableExtra::group_rows("One, two & three treatments",13,15)%>%
  kableExtra::group_rows("Four or more treatments",16,18)%>% 
  kableExtra::group_rows("Drop-out from the first treatment",19,21)%>% 
  kableExtra::scroll_box(width = "100%", height = "600px")
Table 6: Summary of incidence rates of events depending on whom patients live with
con_quien_vive_joel tstop event rate lower upper
Only one treatment
Alone 6878.207 1034 150 141 160
Family of origin 78666.565 11905 151 149 154
With couple/children 37054.174 5536 149 145 153
Two treatments
Alone 6878.207 206 30 26 34
Family of origin 78666.565 2540 32 31 34
With couple/children 37054.174 1161 31 30 33
Three treatment
Alone 6878.207 71 10 8 13
Family of origin 78666.565 671 9 8 9
With couple/children 37054.174 343 9 8 10
One & two treatments
Alone 6878.207 1240 180 170 191
Family of origin 78666.565 14445 184 181 187
With couple/children 37054.174 6697 181 176 185
One, two & three treatments
Alone 6878.207 1311 191 180 201
Family of origin 78666.565 15116 192 189 195
With couple/children 37054.174 7040 190 186 194
Four or more treatments
Alone 6878.207 24 3 2 5
Family of origin 78666.565 339 4 4 5
With couple/children 37054.174 149 4 3 5
Drop-out from the first treatment
Alone 6878.207 766 111 104 120
Family of origin 78666.565 8691 110 108 113
With couple/children 37054.174 4417 119 116 123
Note. Events per 1,000 person-years

Time for this code chunk to run: 0 minutes


The amount of treatments (duplicates_filtered) and the maximum cumulative days spent in treatment (max_cum_dias_trat_sin_na) by each user were adjusted by dividing them by the years they were available in the study (we subtracted the date of the first admission to the date of the end of the follow-up).


Show code
rbind(
data.table::data.table(finalfit(prueba2_imp20,'max_cum_dias_trat_sin_na_adj', "con_quien_vive_joel"))[,2:5],
data.table::data.table(finalfit(prueba2_imp20,'duplicates_filtered_adj', "con_quien_vive_joel"))[,2:5], fill= T
) %>% 
  knitr::kable(format="html",caption= "Summary of adjusted measures, depending on whom patients live with") %>% 
  kableExtra::kable_classic() %>% 
  kableExtra::group_rows("Max. Cumulative Days of Treatment",1,3)%>% 
  kableExtra::group_rows("Number of Treatments by User (Total)",4,6)%>%
  kableExtra::add_footnote(c("Note. Outcomes were adjusted for person-years", paste0("First regression= ", ff_metrics(lm(max_cum_dias_trat_sin_na_adj~ con_quien_vive_joel, prueba2_imp20))),paste0("Second regression= ",ff_metrics(lm(duplicates_filtered_adj~ con_quien_vive_joel, prueba2_imp20))  
)), notation = "none")%>%
    kableExtra::scroll_box(width = "100%", height = "350px")
Table 7: Summary of adjusted measures, depending on whom patients live with
unit value Coefficient (univariable)
Max. Cumulative Days of Treatment
Alone Mean (sd) 70.6 (93.5)
Family of origin Mean (sd) 80.1 (91.9) 9.49 (4.44 to 14.55, p<0.001)
With couple/children Mean (sd) 75.9 (86.6) 5.26 (-0.02 to 10.55, p=0.051)
Number of Treatments by User (Total)
Alone Mean (sd) 0.5 (1.1)
Family of origin Mean (sd) 0.6 (1.1) 0.01 (-0.05 to 0.08, p=0.636)
With couple/children Mean (sd) 0.5 (1.0) -0.03 (-0.10 to 0.03, p=0.323)
Note. Outcomes were adjusted for person-years
First regression= Number in dataframe = 23979, Number in model = 23979, Missing = 0, Log-likelihood = -142049.04, AIC = 284106.1, R-squared = 0.00087, Adjusted R-squared = 0.00079
Second regression= Number in dataframe = 23979, Number in model = 23979, Missing = 0, Log-likelihood = -36286.27, AIC = 72580.5, R-squared = 0.00038, Adjusted R-squared = 0.00029

Time for this code chunk to run: 0 minutes


We checked the linear model with adjusted cumulative days of treatment


Show code
cat("Autocorrelation?")  
Autocorrelation?
Show code
lmtest::dwtest(prueba2_imp20$max_cum_dias_trat_sin_na_adj~ prueba2_imp20$con_quien_vive_joel)

    Durbin-Watson test

data:  prueba2_imp20$max_cum_dias_trat_sin_na_adj ~ prueba2_imp20$con_quien_vive_joel
DW = 1.9797, p-value = 0.0583
alternative hypothesis: true autocorrelation is greater than 0
Show code
cat("Pattern in residuals?")  
Pattern in residuals?
Show code
plot(residuals(lm(prueba2_imp20$max_cum_dias_trat_sin_na_adj~ prueba2_imp20$con_quien_vive_joel)))
Show code
cat("Are variances equal?")  
Are variances equal?
Show code
car::leveneTest(prueba2_imp20$max_cum_dias_trat_sin_na_adj,as.factor(prueba2_imp20$con_quien_vive_joel), center = mean)
Levene's Test for Homogeneity of Variance (center = mean)
         Df F value    Pr(>F)    
group     2  16.816 5.037e-08 ***
      23976                      
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Show code
#Como el estadístico de Levene arroja un p-valor inferior a 0,05 
#(Pr>F = 0,023) se rechaza la hipótesis nula de igualdad de varianzas. 

mod<-aov(prueba2_imp20$max_cum_dias_trat_sin_na_adj~as.factor(prueba2_imp20$con_quien_vive_joel))
  contraste <- rbind(c(1,1,-2),c(1,-1,0)) #
#Se entrega al programa los contrastes a utilizar: 
    filas<-c("Alone & Family of origin vs With couple/children","Alone vs Family of origin")
    columnas<-c("Alone","Family of origin","With couple/children")

# contraste se definen un vector de números de acuerdo a las hipótesis formuladas
# función glht que tiene como argumento el modelo ANOVA definido como mod, 
# y definir la variable grupal estado_nutricional= contraste. Por último, realizar un summary.
    
    dimnames(contraste)<-list(filas,columnas)

cat("GLHT: User-defined General linear hypothesis")        
GLHT: User-defined General linear hypothesis
Show code
    compara <-multcomp::glht(mod, linfct = multcomp::mcp(`as.factor(prueba2_imp20$con_quien_vive_joel)`= contraste))
    
    summary(compara)

     Simultaneous Tests for General Linear Hypotheses

Multiple Comparisons of Means: User-defined Contrasts


Fit: aov(formula = prueba2_imp20$max_cum_dias_trat_sin_na_adj ~ as.factor(prueba2_imp20$con_quien_vive_joel))

Linear Hypotheses:
                                                      Estimate
Alone & Family of origin vs With couple/children == 0   -1.033
Alone vs Family of origin == 0                          -9.494
                                                      Std. Error
Alone & Family of origin vs With couple/children == 0      3.349
Alone vs Family of origin == 0                             2.581
                                                      t value
Alone & Family of origin vs With couple/children == 0  -0.308
Alone vs Family of origin == 0                         -3.679
                                                      Pr(>|t|)    
Alone & Family of origin vs With couple/children == 0 0.924666    
Alone vs Family of origin == 0                        0.000453 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Adjusted p values reported -- single-step method)
Show code
cat("Not assuming equal variances")            
Not assuming equal variances
Show code
    oneway.test(max_cum_dias_trat_sin_na_adj~as.factor(con_quien_vive_joel), 
                          data = prueba2_imp20, 
                          var.equal=FALSE)

    One-way analysis of means (not assuming equal variances)

data:  max_cum_dias_trat_sin_na_adj and as.factor(con_quien_vive_joel)
F = 10.3, num df = 2.0, denom df = 3573.5, p-value = 3.463e-05
Show code
cat("Tukey HSD")
Tukey HSD
Show code
    TukeyHSD(mod)
  Tukey multiple comparisons of means
    95% family-wise confidence level

Fit: aov(formula = prueba2_imp20$max_cum_dias_trat_sin_na_adj ~ as.factor(prueba2_imp20$con_quien_vive_joel))

$`as.factor(prueba2_imp20$con_quien_vive_joel)`
                                           diff       lwr       upr
Family of origin-Alone                 9.494373  3.445382 15.543364
With couple/children-Alone             5.263452 -1.056002 11.582906
With couple/children-Family of origin -4.230921 -7.258117 -1.203725
                                          p adj
Family of origin-Alone                0.0006866
With couple/children-Alone            0.1243273
With couple/children-Family of origin 0.0030289
Show code
cat("Games-Howell")    
Games-Howell
Show code
posthocTGH(prueba2_imp20$con_quien_vive_joel, 
           y = prueba2_imp20$max_cum_dias_trat_sin_na_adj, method= "games-howell", digits=4)
                         n means variances
Alone                 1335 70.63      8749
Family of origin     15455 80.12      8454
With couple/children  7189 75.89      7501

                                        diff  ci.lo  ci.hi     t
Family of origin-Alone                 9.494  3.243 15.746 3.563
With couple/children-Alone             5.263 -1.202 11.729 1.910
With couple/children-Family of origin -4.231 -7.187 -1.275 3.355
                                         df     p
Family of origin-Alone                 1565 .0011
With couple/children-Alone             1784 .1362
With couple/children-Family of origin 14807 .0023
Show code
#https://www.usabart.nl/eval/5-2%20ANOVA.pdf

Time for this code chunk to run: 0 minutes


Survival Analyses (Feb 2023)

Explore

Show code
#make dummy for each treatment arm
prueba2_imp20$con_quien_vive_joel_fam_or<-as.numeric(prueba2_imp20$con_quien_vive_joel=="Family of origin")
prueba2_imp20$con_quien_vive_joel_alone<-as.numeric(prueba2_imp20$con_quien_vive_joel=="Alone")
prueba2_imp20$con_quien_vive_joel_coup_chil<-as.numeric(prueba2_imp20$con_quien_vive_joel=="With couple/children")

biostat3::survRate(Surv(yrs_to_tr_dropout, event) ~ con_quien_vive_joel, data= prueba2_imp20) %>% 
  #dplyr::mutate(rate=round(rate*1000,0),lower=round(lower*1000,0), upper=round(upper*1000,0))%>% 
  knitr::kable("markdown", caption= "Glimpse of the survival analysis, Years to drop-out in the First Treatment by Living Conditions")
Table 8: Glimpse of the survival analysis, Years to drop-out in the First Treatment by Living Conditions
con_quien_vive_joel tstop event rate lower upper
con_quien_vive_joel=Alone Alone 2182.976 926 0.4241916 0.3973065 0.4524172
con_quien_vive_joel=Family of origin Family of origin 27734.735 10209 0.3680944 0.3609883 0.3753052
con_quien_vive_joel=With couple/children With couple/children 11721.960 5076 0.4330334 0.4212017 0.4451131

Time for this code chunk to run: 0 minutes

Show code
dropout_fit<- survfit(Surv(yrs_to_tr_dropout, event) ~ con_quien_vive_joel, data= prueba2_imp20,
                               type      = "kaplan-meier",
                                error     = "greenwood",
                                conf.type = "log-log") 

survminer::ggsurvplot(dropout_fit,
           #fun = "cumhaz",
           conf.int = TRUE,
           legend.labs = c("Alone", "Family of origin", "With couple/children"), 
           risk.table = "abs_pct",
           #ncensor.plot = TRUE,
           ggtheme = theme_classic(base_size=15),
           risk.table.y.text.col = F,
           risk.table.col="black",
           font.tickslab = c(10),
           risk.table.height = .2,
           risk.table.fontsize = 2.5,
           #break.time.by = 365.25,
           pval = T,
           #ylim=c(0,10),
           legend = c(0.75, 0.8), 
           legend.title="Living conditions",
           xlab= "Time (in years)", 
           #cumevents=T,
           surv.connect = T,
           censor= F
           )
Kaplan-Meier curves by Living conditions

Figure 5: Kaplan-Meier curves by Living conditions

Time for this code chunk to run: 0 minutes

We calculated the restricted mean survival time and lost, considering that proportionality of trends could be violated.

Show code
cat("Living with Family of origin vs. Alone and With couple/children at 6 months")
Living with Family of origin vs. Alone and With couple/children at 6 months
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, as.numeric(prueba2_imp20$con_quien_vive_joel=="Family of origin"), tau=.5)

The truncation time: tau = 0.5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.387 0.001     0.384     0.389
RMST (arm=0) 0.381 0.002     0.378     0.384


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


Between-group contrast 
                      Est. lower .95 upper .95     p
RMST (arm=1)-(arm=0) 0.005     0.002     0.009 0.005
RMST (arm=1)/(arm=0) 1.014     1.004     1.025 0.006
RMTL (arm=1)/(arm=0) 0.954     0.922     0.986 0.005
Show code
cat("Living with Family of origin vs. Alone and With couple/children at 1 year")
Living with Family of origin vs. Alone and With couple/children at 1 year
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, as.numeric(prueba2_imp20$con_quien_vive_joel=="Family of origin"), tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.599 0.003     0.593     0.604
RMST (arm=0) 0.575 0.004     0.568     0.583


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.401 0.003     0.396     0.407
RMTL (arm=0) 0.425 0.004     0.417     0.432


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.024     0.014     0.033 0
RMST (arm=1)/(arm=0) 1.041     1.024     1.058 0
RMTL (arm=1)/(arm=0) 0.945     0.924     0.966 0
Show code
cat("Living with Family of origin vs. Alone and With couple/children at 3 years")
Living with Family of origin vs. Alone and With couple/children at 3 years
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, as.numeric(prueba2_imp20$con_quien_vive_joel=="Family of origin"), tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 1.244 0.010     1.224     1.264
RMST (arm=0) 1.134 0.013     1.108     1.159


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 1.756 0.010     1.736     1.776
RMTL (arm=0) 1.866 0.013     1.841     1.892


Between-group contrast 
                      Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) 0.110     0.078     0.143 0
RMST (arm=1)/(arm=0) 1.097     1.068     1.128 0
RMTL (arm=1)/(arm=0) 0.941     0.924     0.958 0
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Living Alone vs. Family of origin and With couple/children at 6 months")
Living Alone vs. Family of origin and With couple/children at 6 months
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, prueba2_imp20$con_quien_vive_joel_alone, tau=.5)

The truncation time: tau = 0.5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.358 0.004     0.350     0.367
RMST (arm=0) 0.386 0.001     0.384     0.388


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.142 0.004     0.133     0.150
RMTL (arm=0) 0.114 0.001     0.112     0.116


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.028    -0.037    -0.019 0
RMST (arm=1)/(arm=0)  0.927     0.905     0.950 0
RMTL (arm=1)/(arm=0)  1.247     1.170     1.328 0
Show code
cat("Living Alone vs. Family of origin and With couple/children at 1 year")
Living Alone vs. Family of origin and With couple/children at 1 year
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, prueba2_imp20$con_quien_vive_joel_alone, tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.544 0.010     0.524     0.563
RMST (arm=0) 0.593 0.002     0.589     0.598


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.456 0.010     0.437     0.476
RMTL (arm=0) 0.407 0.002     0.402     0.411


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.050    -0.070    -0.029 0
RMST (arm=1)/(arm=0)  0.916     0.883     0.951 0
RMTL (arm=1)/(arm=0)  1.122     1.073     1.173 0
Show code
cat("Living Alone vs. Family of origin and With couple/children at 3 years")
Living Alone vs. Family of origin and With couple/children at 3 years
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, prueba2_imp20$con_quien_vive_joel_alone, tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 1.116 0.034     1.050     1.182
RMST (arm=0) 1.210 0.008     1.194     1.226


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 1.884 0.034     1.818     1.950
RMTL (arm=0) 1.790 0.008     1.774     1.806


Between-group contrast 
                       Est. lower .95 upper .95     p
RMST (arm=1)-(arm=0) -0.094    -0.162    -0.025 0.007
RMST (arm=1)/(arm=0)  0.923     0.868     0.981 0.009
RMTL (arm=1)/(arm=0)  1.052     1.015     1.091 0.006
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("Living with With couple/children(1) vs. Alone and Family of origin at 6 months")
Living with With couple/children(1) vs. Alone and Family of origin at 6 months
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, prueba2_imp20$con_quien_vive_joel_coup_chil, tau=.5)

The truncation time: tau = 0.5  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.385 0.002     0.382     0.389
RMST (arm=0) 0.384 0.001     0.382     0.387


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


Between-group contrast 
                      Est. lower .95 upper .95     p
RMST (arm=1)-(arm=0) 0.001    -0.003     0.005 0.621
RMST (arm=1)/(arm=0) 1.003     0.992     1.013 0.621
RMTL (arm=1)/(arm=0) 0.991     0.957     1.026 0.622
Show code
cat("Living with With couple/children(1) vs. Alone and Family of origin at 1 year")
Living with With couple/children(1) vs. Alone and Family of origin at 1 year
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, prueba2_imp20$con_quien_vive_joel_coup_chil, tau=1)

The truncation time: tau = 1  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 0.581 0.004     0.573     0.589
RMST (arm=0) 0.594 0.003     0.589     0.600


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 0.419 0.004     0.411     0.427
RMTL (arm=0) 0.406 0.003     0.400     0.411


Between-group contrast 
                       Est. lower .95 upper .95     p
RMST (arm=1)-(arm=0) -0.013    -0.023    -0.004 0.007
RMST (arm=1)/(arm=0)  0.978     0.962     0.994 0.007
RMTL (arm=1)/(arm=0)  1.033     1.009     1.057 0.007
Show code
cat("Living with With couple/children(1) vs. Alone and Family of origin at 3 years")
Living with With couple/children(1) vs. Alone and Family of origin at 3 years
Show code
survRM2::rmst2(prueba2_imp20$yrs_to_tr_dropout, prueba2_imp20$event, prueba2_imp20$con_quien_vive_joel_coup_chil, tau=3)

The truncation time: tau = 3  was specified. 

Restricted Mean Survival Time (RMST) by arm 
              Est.    se lower .95 upper .95
RMST (arm=1) 1.137 0.014     1.109     1.164
RMST (arm=0) 1.234 0.010     1.215     1.253


Restricted Mean Time Lost (RMTL) by arm 
              Est.    se lower .95 upper .95
RMTL (arm=1) 1.863 0.014     1.836     1.891
RMTL (arm=0) 1.766 0.010     1.747     1.785


Between-group contrast 
                       Est. lower .95 upper .95 p
RMST (arm=1)-(arm=0) -0.097    -0.131    -0.064 0
RMST (arm=1)/(arm=0)  0.921     0.895     0.948 0
RMTL (arm=1)/(arm=0)  1.055     1.036     1.074 0

Time for this code chunk to run: 0 minutes

Show code
#https://rpubs.com/linpearl89/TTE-RCT
#https://search.r-project.org/CRAN/refmans/adjustedCurves/html/surv_aiptw_pseudo.html
#https://search.r-project.org/CRAN/refmans/adjustedCurves/html/surv_iptw_cox.html
#https://cran.r-project.org/web/packages/RISCA/RISCA.pdf

#2023-04-13
prueba2_imp21<-
dplyr::mutate(prueba2_imp20,numero_de_hijos_mod_joel= as.ordered(numero_de_hijos_mod_joel)) %>% dplyr::mutate(freq_cons_sus_prin=ordered(freq_cons_sus_prin,levels=c("Less than 1 day a week", "1 day a week or more", "2 to 3 days a week","4 to 6 days a week", "Daily"))) %>% 
dplyr::mutate(comorbidity_icd_10=dplyr::case_when(as.character(comorbidity_icd_10)=="Two or more"~"One or more", as.character(comorbidity_icd_10)=="One"~"One or more",T~as.character(comorbidity_icd_10))) %>% 
  dplyr::mutate(comorbidity_icd_10= ordered(comorbidity_icd_10, levels= c("Without psychiatric comorbidity", "Diagnosis unknown (under study)", "One or more")))

coxfit <- coxph(Surv(yrs_to_tr_dropout, event) ~ con_quien_vive_joel+ edad_al_ing+ sexo_2 + escolaridad_rec + sus_ini_mod_mvv + freq_cons_sus_prin + dg_trs_cons_sus_or + sus_principal_mod+ cnt_mod_cie_10_or, ties="efron", data= prueba2_imp21)
   
survminer::ggcoxdiagnostics(coxfit, type = "schoenfeld")

geom_smooth() using formula = ‘y ~ x’

Schoefeld residuals

Figure 6: Schoefeld residuals

Show code
ggsave("prueba_scho_plot.png", dpi=640, height=15, width= 15)

geom_smooth() using formula = ‘y ~ x’

Time for this code chunk to run: 0.2 minutes

Show code
data.frame(cox.zph(coxfit)$table) %>% 
  knitr::kable("markdown", caption= "Schoefeld residuals")
Table 9: Schoefeld residuals
chisq df p
con_quien_vive_joel 67.332753 2 0.0000000
edad_al_ing 35.047371 1 0.0000000
sexo_2 70.464977 1 0.0000000
escolaridad_rec 4.945225 2 0.0843642
sus_ini_mod_mvv 20.126407 4 0.0004715
freq_cons_sus_prin 184.988917 4 0.0000000
dg_trs_cons_sus_or 81.865725 1 0.0000000
sus_principal_mod 54.866124 4 0.0000000
cnt_mod_cie_10_or 792.297047 3 0.0000000
GLOBAL 1106.723007 22 0.0000000

Time for this code chunk to run: 0 minutes

Show code
#If the value of VIF is less than 1: no correlation - If the value of VIF is between 1-5, there is moderate correlation - If the value of VIF is above 5: severe correlation 

#_#_#_#_#_#_#_#_#_
invisible("3.Cox, collinearity")

#Error in Design(data, formula, specials = c("strat", "strata")) : 
#  Variable escolaridad_rec is an ordered factor with non-numeric levels.
# You should set options(contrasts=c("contr.treatment", "contr.treatment"))
#or rms will not work properly.
options(contrasts=c("contr.treatment", "contr.treatment"))
#https://randomeffect.net/post/2021/05/02/the-rms-validate-function/
f1 <- rms::cph(Surv(yrs_to_tr_dropout, event) ~ con_quien_vive_joel + edad_al_ing+ sexo_2 + escolaridad_rec + sus_ini_mod_mvv + freq_cons_sus_prin + dg_trs_cons_sus_or + sus_principal_mod+ cnt_mod_cie_10_or, data= prueba2_imp21, x=T, y=T)
warning("X matrix deemed to be singular; variable freq_cons_sus_prin=Daily --> I deleted it")

cvif <- rms::vif(f1)

#https://www.uvm.edu/~statdhtx/methods8/Supplements/R-Programs/AnovaExample.R
#http://faculty.nps.edu/sebuttre/home/r/contrasts.html
options(contrasts = c("contr.treatment","contr.poly"))  # The default

cvif%>% 
  knitr::kable("markdown", caption= "Variance Inflation Factors in Cox Regressions")
Table 10: Variance Inflation Factors in Cox Regressions
x
con_quien_vive_joel=Family of origin 4.511849
con_quien_vive_joel=With couple/children 4.533197
edad_al_ing 1.082756
sexo_2=Women 1.038919
escolaridad_rec=2-Completed high school or less 1.364214
escolaridad_rec=1-More than high school 1.391441
sus_ini_mod_mvv=Cocaine hydrochloride 1.106617
sus_ini_mod_mvv=Marijuana 1.235633
sus_ini_mod_mvv=Other 1.059899
sus_ini_mod_mvv=Cocaine paste 1.128362
freq_cons_sus_prin=1 day a week or more 2.227068
freq_cons_sus_prin=2 to 3 days a week 5.126104
freq_cons_sus_prin=4 to 6 days a week 3.893858
freq_cons_sus_prin=Daily 5.922771
dg_trs_cons_sus_or=Hazardous consumption 1.143685
sus_principal_mod=Cocaine hydrochloride 2.056801
sus_principal_mod=Marijuana 1.648615
sus_principal_mod=Other 1.100959
sus_principal_mod=Cocaine paste 2.478480
cnt_mod_cie_10_or 1.158332
cnt_mod_cie_10_or=2 1.104976
cnt_mod_cie_10_or=3 1.031117
Show code
    # dplyr::mutate_if(is.numeric,~round(.,2)) %>% 
    # DT::datatable()

Time for this code chunk to run: 0 minutes

Adjusted

We calculated the restricted mean survival time and lost, considering that proportionality of trends could be violated.

Show code
vars<-c("edad_al_ing", "sexo_2", "escolaridad_rec", "sus_ini_mod_mvv", "freq_cons_sus_prin",  "dg_trs_cons_sus_or", "sus_principal_mod", "cnt_mod_cie_10_or")

logit <- nnet::multinom(formula= as.formula(paste("con_quien_vive_joel~", paste(vars, collapse = "+"))), 
              data=prueba2_imp21)
# weights:  66 (42 variable)
initial  value 26343.624070 
iter  10 value 21527.357784
iter  20 value 20799.689702
iter  30 value 19142.487889
iter  40 value 18418.254378
iter  50 value 18262.159206
final  value 18261.746705 
converged
Show code
pred <- predict(logit, type="probs")
prueba2_imp21$weight_mult_a00 <-WeightIt::get_w_from_ps(pred, prueba2_imp21$con_quien_vive_joel, estimand = "ATE", stabilize=T)

invisible("IMPORTANTE, EJEMPLO DE CÓMO SE VEN LOS WEIGHTS")
#https://rydaro.github.io/index.html
# #trim extreme values for stability
# oral60$pr_score_trim <-if_else(oral60$pr_score<.01,.01,oral60$pr_score)
# #now for the high values, retaining the lower trims
# oral60$pr_score_trim <-if_else(oral60$pr_score>.99,.99,oral60$pr_score_trim)

#truncate weights at 1%
prueba2_imp21$weight_tr <- ifelse(prueba2_imp21$weight_mult_a00 < quantile(prueba2_imp21$weight_mult_a00, probs=.01), quantile(prueba2_imp21$weight_mult_a00, probs=.01), prueba2_imp21$weight_mult_a00)
prueba2_imp21$weight_tr <- ifelse(prueba2_imp21$weight_mult_a00 > quantile(prueba2_imp21$weight_mult_a00, probs=.99), quantile(prueba2_imp21$weight_mult_a00, probs=.99), prueba2_imp21$weight_tr)

#truncate weights at 5%
prueba2_imp21$weight_tr2 <- ifelse(prueba2_imp21$weight_mult_a00 < quantile(prueba2_imp21$weight_mult_a00, probs=.05), quantile(prueba2_imp21$weight_mult_a00, probs=.05), prueba2_imp21$weight_mult_a00)
prueba2_imp21$weight_tr2 <- ifelse(prueba2_imp21$weight_mult_a00 > quantile(prueba2_imp21$weight_mult_a00, probs=.95), quantile(prueba2_imp21$weight_mult_a00, probs=.95), prueba2_imp21$weight_tr2)

#truncate weights at 10%
prueba2_imp21$weight_tr3 <- ifelse(prueba2_imp21$weight_mult_a00 < quantile(prueba2_imp21$weight_mult_a00, probs=.1), quantile(prueba2_imp21$weight_mult_a00, probs=.1), prueba2_imp21$weight_mult_a00)
prueba2_imp21$weight_tr3 <- ifelse(prueba2_imp21$weight_mult_a00 > quantile(prueba2_imp21$weight_mult_a00, probs=.9), quantile(prueba2_imp21$weight_mult_a00, probs=.9), prueba2_imp21$weight_tr3)

# AKM RMST adjusted for age
source("https://raw.githubusercontent.com/s-conner/akm-rmst/master/AKM_rmst.R")

cat("===============================================================================")
===============================================================================
Show code
cat("at 6 months (adjusted), not stabilized weights")
at 6 months (adjusted), not stabilized weights
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_mult_a00, tau=.5)




RMST calculated up to tau = 0.5


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.363 0.005
Group Family of origin       0.388 0.001
Group With couple/children   0.378 0.002


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.025 0.005
Groups With couple/children vs. Alone               0.015 0.005
Groups With couple/children vs. Family of origin   -0.010 0.002
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.016  0.035
Groups With couple/children vs. Alone               0.005  0.025
Groups With couple/children vs. Family of origin   -0.015 -0.006
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.004
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.068 0.014
Groups With couple/children vs. Alone                 0.040 0.014
Groups With couple/children vs. Family of origin     -0.027 0.006
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.070 1.042 1.099
Groups With couple/children vs. Alone              1.041 1.013 1.071
Groups With couple/children vs. Family of origin   0.973 0.962 0.985
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.005
Groups With couple/children vs. Family of origin   0.000
Show code
cat("At 1 year (adjusted), not stabilized weights")
At 1 year (adjusted), not stabilized weights
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_mult_a00, tau=1)




RMST calculated up to tau = 1


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.551 0.011
Group Family of origin       0.601 0.003
Group With couple/children   0.567 0.004


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.049 0.012
Groups With couple/children vs. Alone               0.016 0.012
Groups With couple/children vs. Family of origin   -0.034 0.005
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.027  0.072
Groups With couple/children vs. Alone              -0.008  0.039
Groups With couple/children vs. Family of origin   -0.044 -0.023
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.189
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.086 0.021
Groups With couple/children vs. Alone                 0.028 0.022
Groups With couple/children vs. Family of origin     -0.057 0.009
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.089 1.046 1.135
Groups With couple/children vs. Alone              1.029 0.986 1.073
Groups With couple/children vs. Family of origin   0.944 0.927 0.962
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.194
Groups With couple/children vs. Family of origin   0.000
Show code
cat("At 3 years (adjusted), not stabilized weights")
At 3 years (adjusted), not stabilized weights
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_mult_a00, tau=3)




RMST calculated up to tau = 3


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  1.132 0.038
Group Family of origin       1.242 0.010
Group With couple/children   1.108 0.015


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.109 0.039
Groups With couple/children vs. Alone              -0.024 0.041
Groups With couple/children vs. Family of origin   -0.133 0.018
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.032  0.186
Groups With couple/children vs. Alone              -0.104  0.056
Groups With couple/children vs. Family of origin   -0.169 -0.097
                                                       p
Groups Family of origin vs. Alone                  0.005
Groups With couple/children vs. Alone              0.556
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.092 0.034
Groups With couple/children vs. Alone                -0.021 0.036
Groups With couple/children vs. Family of origin     -0.114 0.016
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.097 1.025 1.173
Groups With couple/children vs. Alone              0.979 0.912 1.051
Groups With couple/children vs. Family of origin   0.893 0.865 0.921
                                                       p
Groups Family of origin vs. Alone                  0.007
Groups With couple/children vs. Alone              0.553
Groups With couple/children vs. Family of origin   0.000
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("at 6 months (adjusted), weights stabilized by 1%")
at 6 months (adjusted), weights stabilized by 1%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr, tau=.5)




RMST calculated up to tau = 0.5


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.362 0.005
Group Family of origin       0.388 0.001
Group With couple/children   0.378 0.002


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.026 0.005
Groups With couple/children vs. Alone               0.016 0.005
Groups With couple/children vs. Family of origin   -0.010 0.002
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.016  0.035
Groups With couple/children vs. Alone               0.006  0.026
Groups With couple/children vs. Family of origin   -0.014 -0.006
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.002
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.069 0.013
Groups With couple/children vs. Alone                 0.043 0.014
Groups With couple/children vs. Family of origin     -0.026 0.006
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.071 1.044 1.100
Groups With couple/children vs. Alone              1.044 1.016 1.073
Groups With couple/children vs. Family of origin   0.974 0.963 0.985
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.002
Groups With couple/children vs. Family of origin   0.000
Show code
cat("At 1 year (adjusted), weights stabilized by 1%")
At 1 year (adjusted), weights stabilized by 1%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr, tau=1)




RMST calculated up to tau = 1


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.550 0.011
Group Family of origin       0.601 0.003
Group With couple/children   0.567 0.004


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.050 0.011
Groups With couple/children vs. Alone               0.017 0.012
Groups With couple/children vs. Family of origin   -0.033 0.005
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.028  0.072
Groups With couple/children vs. Alone              -0.006  0.040
Groups With couple/children vs. Family of origin   -0.044 -0.023
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.153
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.087 0.020
Groups With couple/children vs. Alone                 0.030 0.021
Groups With couple/children vs. Family of origin     -0.057 0.009
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.091 1.048 1.136
Groups With couple/children vs. Alone              1.031 0.988 1.075
Groups With couple/children vs. Family of origin   0.945 0.928 0.962
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.158
Groups With couple/children vs. Family of origin   0.000
Show code
cat("At 3 years (adjusted), weights stabilized by 1%")
At 3 years (adjusted), weights stabilized by 1%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr, tau=3)




RMST calculated up to tau = 3


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  1.130 0.037
Group Family of origin       1.242 0.010
Group With couple/children   1.107 0.015


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.111 0.038
Groups With couple/children vs. Alone              -0.023 0.040
Groups With couple/children vs. Family of origin   -0.134 0.018
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.036  0.187
Groups With couple/children vs. Alone              -0.101  0.056
Groups With couple/children vs. Family of origin   -0.170 -0.099
                                                       p
Groups Family of origin vs. Alone                  0.004
Groups With couple/children vs. Alone              0.570
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.094 0.034
Groups With couple/children vs. Alone                -0.020 0.035
Groups With couple/children vs. Family of origin     -0.114 0.016
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.099 1.028 1.174
Groups With couple/children vs. Alone              0.980 0.914 1.050
Groups With couple/children vs. Family of origin   0.892 0.865 0.920
                                                       p
Groups Family of origin vs. Alone                  0.005
Groups With couple/children vs. Alone              0.567
Groups With couple/children vs. Family of origin   0.000
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("at 6 months (adjusted), weights stabilized by 5%")
at 6 months (adjusted), weights stabilized by 5%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr2, tau=.5)




RMST calculated up to tau = 0.5


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.361 0.005
Group Family of origin       0.388 0.001
Group With couple/children   0.379 0.002


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.027 0.005
Groups With couple/children vs. Alone               0.018 0.005
Groups With couple/children vs. Family of origin   -0.009 0.002
                                                      CIL    CIU p
Groups Family of origin vs. Alone                   0.017  0.036 0
Groups With couple/children vs. Alone               0.008  0.028 0
Groups With couple/children vs. Family of origin   -0.013 -0.005 0


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.071 0.013
Groups With couple/children vs. Alone                 0.049 0.014
Groups With couple/children vs. Family of origin     -0.023 0.006
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.074 1.047 1.102
Groups With couple/children vs. Alone              1.050 1.022 1.078
Groups With couple/children vs. Family of origin   0.977 0.967 0.988
                                                   p
Groups Family of origin vs. Alone                  0
Groups With couple/children vs. Alone              0
Groups With couple/children vs. Family of origin   0
Show code
cat("At 1 year (adjusted), weights stabilized by 5%")
At 1 year (adjusted), weights stabilized by 5%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr2, tau=1)




RMST calculated up to tau = 1


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.549 0.011
Group Family of origin       0.600 0.003
Group With couple/children   0.569 0.004


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.051 0.011
Groups With couple/children vs. Alone               0.020 0.011
Groups With couple/children vs. Family of origin   -0.031 0.005
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.030  0.073
Groups With couple/children vs. Alone              -0.002  0.043
Groups With couple/children vs. Family of origin   -0.041 -0.021
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.075
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.090 0.020
Groups With couple/children vs. Alone                 0.036 0.021
Groups With couple/children vs. Family of origin     -0.053 0.009
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.094 1.052 1.137
Groups With couple/children vs. Alone              1.037 0.996 1.080
Groups With couple/children vs. Family of origin   0.948 0.932 0.965
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.079
Groups With couple/children vs. Family of origin   0.000
Show code
cat("At 3 years (adjusted), weights stabilized by 5%")
At 3 years (adjusted), weights stabilized by 5%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr2, tau=3)




RMST calculated up to tau = 3


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  1.126 0.036
Group Family of origin       1.241 0.010
Group With couple/children   1.110 0.015


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.115 0.037
Groups With couple/children vs. Alone              -0.016 0.039
Groups With couple/children vs. Family of origin   -0.130 0.018
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.042  0.188
Groups With couple/children vs. Alone              -0.092  0.060
Groups With couple/children vs. Family of origin   -0.165 -0.096
                                                       p
Groups Family of origin vs. Alone                  0.002
Groups With couple/children vs. Alone              0.687
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.097 0.033
Groups With couple/children vs. Alone                -0.014 0.035
Groups With couple/children vs. Family of origin     -0.111 0.016
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.102 1.033 1.176
Groups With couple/children vs. Alone              0.986 0.922 1.055
Groups With couple/children vs. Family of origin   0.895 0.868 0.923
                                                       p
Groups Family of origin vs. Alone                  0.003
Groups With couple/children vs. Alone              0.686
Groups With couple/children vs. Family of origin   0.000
Show code
cat("===============================================================================")
===============================================================================
Show code
cat("at 6 months (adjusted), weights stabilized by 10%")
at 6 months (adjusted), weights stabilized by 10%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr3, tau=.5)




RMST calculated up to tau = 0.5


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.361 0.005
Group Family of origin       0.388 0.001
Group With couple/children   0.381 0.002


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.027 0.005
Groups With couple/children vs. Alone               0.020 0.005
Groups With couple/children vs. Family of origin   -0.007 0.002
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.018  0.036
Groups With couple/children vs. Alone               0.010  0.029
Groups With couple/children vs. Family of origin   -0.011 -0.003
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.000
Groups With couple/children vs. Family of origin   0.001


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.072 0.013
Groups With couple/children vs. Alone                 0.054 0.013
Groups With couple/children vs. Family of origin     -0.019 0.006
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.075 1.048 1.103
Groups With couple/children vs. Alone              1.055 1.028 1.083
Groups With couple/children vs. Family of origin   0.981 0.971 0.992
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.000
Groups With couple/children vs. Family of origin   0.001
Show code
cat("At 1 year (adjusted), weights stabilized by 10%")
At 1 year (adjusted), weights stabilized by 10%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr3, tau=1)




RMST calculated up to tau = 1


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.547 0.010
Group Family of origin       0.600 0.003
Group With couple/children   0.572 0.004


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.052 0.011
Groups With couple/children vs. Alone               0.024 0.011
Groups With couple/children vs. Family of origin   -0.028 0.005
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.031  0.074
Groups With couple/children vs. Alone               0.002  0.046
Groups With couple/children vs. Family of origin   -0.038 -0.018
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.031
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.091 0.020
Groups With couple/children vs. Alone                 0.043 0.020
Groups With couple/children vs. Family of origin     -0.048 0.009
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.096 1.054 1.139
Groups With couple/children vs. Alone              1.044 1.003 1.087
Groups With couple/children vs. Family of origin   0.953 0.937 0.970
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.033
Groups With couple/children vs. Family of origin   0.000
Show code
cat("At 3 years (adjusted), weights stabilized by 10%")
At 3 years (adjusted), weights stabilized by 10%
Show code
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group= factor(prueba2_imp21$con_quien_vive_joel), weight=prueba2_imp21$weight_tr3, tau=3)




RMST calculated up to tau = 3


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  1.123 0.035
Group Family of origin       1.241 0.010
Group With couple/children   1.116 0.014


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.118 0.037
Groups With couple/children vs. Alone              -0.007 0.038
Groups With couple/children vs. Family of origin   -0.124 0.018
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.046  0.189
Groups With couple/children vs. Alone              -0.081  0.068
Groups With couple/children vs. Family of origin   -0.159 -0.090
                                                       p
Groups Family of origin vs. Alone                  0.001
Groups With couple/children vs. Alone              0.858
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.100 0.032
Groups With couple/children vs. Alone                -0.006 0.034
Groups With couple/children vs. Family of origin     -0.106 0.015
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.105 1.037 1.177
Groups With couple/children vs. Alone              0.994 0.930 1.062
Groups With couple/children vs. Family of origin   0.900 0.873 0.927
                                                       p
Groups Family of origin vs. Alone                  0.002
Groups With couple/children vs. Alone              0.858
Groups With couple/children vs. Family of origin   0.000

Time for this code chunk to run: 0.2 minutes

Show code
#(3) (PDF) Adjusted restricted mean survival times in observational studies. Available from: https://www.researchgate.net/publication/333326572_Adjusted_restricted_mean_survival_times_in_observational_studies [accessed Feb 02 2023].
logit <- glm(formula= as.formula(paste("con_quien_vive_joel_fam_or~", paste(vars, collapse = "+"))), 
              data=prueba2_imp21, family=binomial(link= "logit"))
pred <- predict(logit, type="response")
prueba2_imp21$weight <- prueba2_imp21$con_quien_vive_joel_fam_or/pred + (1 - prueba2_imp21$con_quien_vive_joel_fam_or)/(1-pred)

#https://anthonyongphd.files.wordpress.com/2016/02/emerging-adulthood-2016-thoemmes-40-59.pdf
#A Primer on Inverse Probability of Treatment Weighting and Marginal Structural Models
#truncate weights at 1%
cat("Stabilized weights")

prueba2_imp21$weight_tr <- ifelse(prueba2_imp21$weight < quantile(prueba2_imp21$weight, probs=.01), quantile(prueba2_imp21$weight, probs=.01), prueba2_imp21$weight)
prueba2_imp21$weight_tr <- ifelse(prueba2_imp21$weight > quantile(prueba2_imp21$weight, probs=.99), quantile(prueba2_imp21$weight, probs=.99), prueba2_imp21$weight_tr)

#truncate weights at 5%
prueba2_imp21$weight_tr2 <- ifelse(prueba2_imp21$weight < quantile(prueba2_imp21$weight, probs=.05), quantile(prueba2_imp21$weight, probs=.05), prueba2_imp21$weight)
prueba2_imp21$weight_tr2 <- ifelse(prueba2_imp21$weight > quantile(prueba2_imp21$weight, probs=.95), quantile(prueba2_imp21$weight, probs=.95), prueba2_imp21$weight_tr2)

#truncate weights at 10%
prueba2_imp21$weight_tr3 <- ifelse(prueba2_imp21$weight < quantile(prueba2_imp21$weight, probs=.1), quantile(prueba2_imp21$weight, probs=.1), prueba2_imp21$weight)
prueba2_imp21$weight_tr3 <- ifelse(prueba2_imp21$weight > quantile(prueba2_imp21$weight, probs=.9), quantile(prueba2_imp21$weight, probs=.9), prueba2_imp21$weight_tr3)


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

logit2 <- glm ( as.formula(paste("con_quien_vive_joel_alone~", paste(vars, collapse = "+"))), 
              data=prueba2_imp21, family=binomial(link= "logit"))

pred2 <- predict(logit2, type="response")
prueba2_imp21$weight_a <- prueba2_imp21$con_quien_vive_joel_alone/pred2 + (1 - prueba2_imp21$con_quien_vive_joel_alone)/(1-pred2)

#https://anthonyongphd.files.wordpress.com/2016/02/emerging-adulthood-2016-thoemmes-40-59.pdf
#A Primer on Inverse Probability of Treatment Weighting and Marginal Structural Models
#truncate weights at 1%
prueba2_imp21$weight_tr_a <- ifelse(prueba2_imp21$weight_a < quantile(prueba2_imp21$weight_a, probs=.01), quantile(prueba2_imp21$weight_a, probs=.01), prueba2_imp21$weight_a)
prueba2_imp21$weight_tr_a <- ifelse(prueba2_imp21$weight_a > quantile(prueba2_imp21$weight_a, probs=.99), quantile(prueba2_imp21$weight_a, probs=.99), prueba2_imp21$weight_tr_a)

#truncate weights at 5%
prueba2_imp21$weight_tr2_a <- ifelse(prueba2_imp21$weight_a < quantile(prueba2_imp21$weight_a, probs=.05), quantile(prueba2_imp21$weight, probs=.05), prueba2_imp21$weight)
prueba2_imp21$weight_tr2_a <- ifelse(prueba2_imp21$weight_a > quantile(prueba2_imp21$weight_a, probs=.95), quantile(prueba2_imp21$weight_a, probs=.95), prueba2_imp21$weight_tr2_a)

#truncate weights at 5%
prueba2_imp21$weight_tr3_b <- ifelse(prueba2_imp21$weight_a < quantile(prueba2_imp21$weight_a, probs=.1), quantile(prueba2_imp21$weight_a, probs=.1), prueba2_imp21$weight_b)
prueba2_imp21$weight_tr3_b <- ifelse(prueba2_imp21$weight_a > quantile(prueba2_imp21$weight_a, probs=.9), quantile(prueba2_imp21$weight_a, probs=.9), prueba2_imp21$weight_tr3_b)


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

logit3 <- glm (as.formula(paste("con_quien_vive_joel_coup_chil~", paste(vars, collapse = "+"))), 
              data=prueba2_imp21, family=binomial(link= "logit"))

pred3 <- predict(logit3, type="response")
prueba2_imp21$weight_b <- prueba2_imp21$con_quien_vive_joel_coup_chil/pred + (1 - prueba2_imp21$con_quien_vive_joel_coup_chil)/(1-pred)

#https://anthonyongphd.files.wordpress.com/2016/02/emerging-adulthood-2016-thoemmes-40-59.pdf
#A Primer on Inverse Probability of Treatment Weighting and Marginal Structural Models
#truncate weights at 1%
prueba2_imp21$weight_tr_b <- ifelse(prueba2_imp21$weight_b < quantile(prueba2_imp21$weight_b, probs=.01), quantile(prueba2_imp21$weight_b, probs=.01), prueba2_imp21$weight_b)
prueba2_imp21$weight_tr_b <- ifelse(prueba2_imp21$weight_b > quantile(prueba2_imp21$weight_b, probs=.99), quantile(prueba2_imp21$weight_b, probs=.99), prueba2_imp21$weight_tr_b)

#truncate weights at 5%
prueba2_imp21$weight_tr2_b <- ifelse(prueba2_imp21$weight_b < quantile(prueba2_imp21$weight_b, probs=.05), quantile(prueba2_imp21$weight_b, probs=.05), prueba2_imp21$weight_b)
prueba2_imp21$weight_tr2_b <- ifelse(prueba2_imp21$weight_b > quantile(prueba2_imp21$weight_b, probs=.95), quantile(prueba2_imp21$weight_b, probs=.95), prueba2_imp21$weight_tr2_b)

#truncate weights at 5%
prueba2_imp21$weight_tr3_b <- ifelse(prueba2_imp21$weight_b < quantile(prueba2_imp21$weight_b, probs=.1), quantile(prueba2_imp21$weight_b, probs=.1), prueba2_imp21$weight_b)
prueba2_imp21$weight_tr3_b <- ifelse(prueba2_imp21$weight_b > quantile(prueba2_imp21$weight_b, probs=.9), quantile(prueba2_imp21$weight_b, probs=.9), prueba2_imp21$weight_tr3_b)

# AKM RMST adjusted for age
source("https://raw.githubusercontent.com/s-conner/akm-rmst/master/AKM_rmst.R")

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

cat("Living with Family of origin(1) vs. Alone and With couple/children at 6 months (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_fam_or),weight=prueba2_imp21$weight_tr, tau=1)

cat("Living with Family of origin(1) vs. Alone and With couple/children at 3 years (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_fam_or),weight=prueba2_imp21$weight_tr, tau=3)

cat("Living with Family of origin(1) vs. Alone and With couple/children at 5 years (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_fam_or),weight=prueba2_imp21$weight_tr, tau=5)

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

cat("Living Alone(1) vs. with Family of origin and With couple/children at 6 months (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_alone),weight=prueba2_imp21$weight_tr_a, tau=1)

cat("Living Alone(1) vs. with Family of origin and With couple/children at 3 years (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_alone),weight=prueba2_imp21$weight_tr_a, tau=3)

cat("Living Alone(1) vs. with Family of origin and With couple/children at 5 years (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_alone),weight=prueba2_imp21$weight_tr_a, tau=5)

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

cat("Living with couple/children(1) vs. Family of origin & alone at 6 months (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_coup_chil),weight=prueba2_imp21$weight_tr_b, tau=1)

cat("Living with couple/children(1) vs. Family of origin & alone at 3 years (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_coup_chil),weight=prueba2_imp21$weight_tr_b, tau=3)

cat("Living with couple/children(1) vs. Family of origin & alone at 5 years (adjusted)")
akm_rmst(time=prueba2_imp21$yrs_to_tr_dropout, status=prueba2_imp21$event, group=as.factor(prueba2_imp21$con_quien_vive_joel_coup_chil),weight=prueba2_imp21$weight_tr_b, tau=5)

Time for this code chunk to run: 0 minutes

We assess the covariates’ balance after the weighting

Show code
bal_tab<-
cobalt::bal.tab(con_quien_vive_joel ~ edad_al_ing+ sexo_2 + escolaridad_rec + sus_ini_mod_mvv + freq_cons_sus_prin + dg_trs_cons_sus_or + sus_principal_mod+ cnt_mod_cie_10_or, data = prueba2_imp21,
                weights = list("Multinomial Logistic, not stabilized" = "weight_mult_a00", "Multinomial Logistic, stabilized at 1%"="weight_tr", "Multinomial Logistic, stabilized at 5%"="weight_tr2", "Multinomial Logistic, stabilized at 10%"="weight_tr3"),
                estimand = "ATE",
                which.treat = .all, 
                un = T, 
                 binary = "std", continuous = "std",
                stats = c("mean.diffs", "variance.ratios"))

rbind(cbind(comp="Family of origin vs. alone",bal_tab$Pair.Balance$`Family of origin vs. Alone`$Balance),
      cbind(comp="With couple/children vs. alone",bal_tab$Pair.Balance$`With couple/children vs. Alone`$Balance),
      cbind(comp="With couple/children vs. Family of origin",bal_tab$Pair.Balance$`With couple/children vs. Family of origin`$Balance)) %>%
  data.table::data.table(keep.rownames = T) %>% 
  dplyr::select(comp, rn, everything()) %>% 
  dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Balance Measures")
Table 11: Balance Measures
comp rn Type Diff.Un V.Ratio.Un Diff.Multinomial Logistic, not stabilized V.Ratio.Multinomial Logistic, not stabilized Diff.Multinomial Logistic, stabilized at 1% V.Ratio.Multinomial Logistic, stabilized at 1% Diff.Multinomial Logistic, stabilized at 5% V.Ratio.Multinomial Logistic, stabilized at 5% Diff.Multinomial Logistic, stabilized at 10% V.Ratio.Multinomial Logistic, stabilized at 10%
Family of origin vs. alone edad_al_ing Contin. -0.36 1.05 0.00 0.94 -0.01 0.95 -0.06 0.96 -0.10 0.97
Family of origin vs. alone sexo_2_Women Binary 0.07 0.00 0.02 0.04 0.05
Family of origin vs. alone escolaridad_rec_3-Completed primary school or less Binary -0.12 0.00 0.00 -0.02 -0.04
Family of origin vs. alone escolaridad_rec_2-Completed high school or less Binary 0.07 0.01 0.00 0.02 0.03
Family of origin vs. alone escolaridad_rec_1-More than high school Binary 0.06 -0.02 -0.01 0.00 0.01
Family of origin vs. alone sus_ini_mod_mvv_Alcohol Binary -0.04 -0.02 -0.02 -0.02 -0.02
Family of origin vs. alone sus_ini_mod_mvv_Cocaine hydrochloride Binary 0.04 0.01 0.02 0.03 0.04
Family of origin vs. alone sus_ini_mod_mvv_Marijuana Binary 0.05 0.00 0.00 0.00 0.01
Family of origin vs. alone sus_ini_mod_mvv_Other Binary -0.02 0.01 0.01 0.00 0.00
Family of origin vs. alone sus_ini_mod_mvv_Cocaine paste Binary -0.05 0.03 0.02 0.01 0.00
Family of origin vs. alone freq_cons_sus_prin_Less than 1 day a week Binary 0.03 -0.01 0.00 0.01 0.02
Family of origin vs. alone freq_cons_sus_prin_1 day a week or more Binary 0.07 0.00 0.02 0.04 0.05
Family of origin vs. alone freq_cons_sus_prin_2 to 3 days a week Binary 0.08 0.00 0.00 0.02 0.04
Family of origin vs. alone freq_cons_sus_prin_4 to 6 days a week Binary 0.07 -0.01 -0.01 0.00 0.01
Family of origin vs. alone freq_cons_sus_prin_Daily Binary -0.17 0.01 0.00 -0.04 -0.08
Family of origin vs. alone dg_trs_cons_sus_or_Hazardous consumption Binary 0.02 0.00 0.00 0.01 0.02
Family of origin vs. alone sus_principal_mod_Alcohol Binary -0.19 -0.01 -0.02 -0.05 -0.08
Family of origin vs. alone sus_principal_mod_Cocaine hydrochloride Binary 0.17 -0.03 0.00 0.04 0.08
Family of origin vs. alone sus_principal_mod_Marijuana Binary 0.07 -0.01 -0.02 -0.01 0.00
Family of origin vs. alone sus_principal_mod_Other Binary 0.02 -0.02 -0.02 -0.01 -0.01
Family of origin vs. alone sus_principal_mod_Cocaine paste Binary -0.03 0.04 0.04 0.02 0.00
Family of origin vs. alone cnt_mod_cie_10_or_0 Binary 0.06 0.02 0.02 0.04 0.05
Family of origin vs. alone cnt_mod_cie_10_or_1 Binary -0.07 -0.02 -0.02 -0.04 -0.05
Family of origin vs. alone cnt_mod_cie_10_or_2 Binary 0.02 0.01 0.01 0.01 0.01
Family of origin vs. alone cnt_mod_cie_10_or_3 Binary 0.00 -0.01 -0.01 -0.01 -0.01
With couple/children vs. alone edad_al_ing1 Contin. 0.09 0.86 0.01 0.91 0.02 0.90 0.04 0.87 0.05 0.86
With couple/children vs. alone sexo_2_Women1 Binary 0.29 0.01 0.03 0.09 0.14
With couple/children vs. alone escolaridad_rec_3-Completed primary school or less1 Binary 0.00 0.00 0.00 0.00 0.00
With couple/children vs. alone escolaridad_rec_2-Completed high school or less1 Binary 0.05 0.01 0.01 0.01 0.03
With couple/children vs. alone escolaridad_rec_1-More than high school1 Binary -0.08 -0.01 -0.01 -0.02 -0.04
With couple/children vs. alone sus_ini_mod_mvv_Alcohol1 Binary 0.02 -0.01 -0.01 0.00 0.00
With couple/children vs. alone sus_ini_mod_mvv_Cocaine hydrochloride1 Binary 0.11 0.01 0.02 0.04 0.06
With couple/children vs. alone sus_ini_mod_mvv_Marijuana1 Binary -0.06 -0.01 -0.01 -0.02 -0.03
With couple/children vs. alone sus_ini_mod_mvv_Other1 Binary 0.00 0.01 0.01 0.01 0.00
With couple/children vs. alone sus_ini_mod_mvv_Cocaine paste1 Binary -0.01 0.03 0.03 0.02 0.01
With couple/children vs. alone freq_cons_sus_prin_Less than 1 day a week1 Binary 0.16 0.00 0.01 0.04 0.07
With couple/children vs. alone freq_cons_sus_prin_1 day a week or more1 Binary 0.21 0.00 0.02 0.07 0.10
With couple/children vs. alone freq_cons_sus_prin_2 to 3 days a week1 Binary 0.20 0.00 0.00 0.03 0.07
With couple/children vs. alone freq_cons_sus_prin_4 to 6 days a week1 Binary 0.02 -0.01 -0.01 0.00 0.00
With couple/children vs. alone freq_cons_sus_prin_Daily1 Binary -0.37 0.01 -0.02 -0.08 -0.15
With couple/children vs. alone dg_trs_cons_sus_or_Hazardous consumption1 Binary 0.13 0.00 0.01 0.03 0.06
With couple/children vs. alone sus_principal_mod_Alcohol1 Binary -0.10 0.00 -0.01 -0.03 -0.05
With couple/children vs. alone sus_principal_mod_Cocaine hydrochloride1 Binary 0.26 -0.03 0.00 0.06 0.11
With couple/children vs. alone sus_principal_mod_Marijuana1 Binary -0.05 -0.03 -0.04 -0.05 -0.05
With couple/children vs. alone sus_principal_mod_Other1 Binary -0.04 -0.03 -0.03 -0.03 -0.03
With couple/children vs. alone sus_principal_mod_Cocaine paste1 Binary -0.09 0.04 0.04 0.01 -0.01
With couple/children vs. alone cnt_mod_cie_10_or_01 Binary 0.19 0.02 0.03 0.06 0.09
With couple/children vs. alone cnt_mod_cie_10_or_11 Binary -0.18 -0.02 -0.03 -0.05 -0.08
With couple/children vs. alone cnt_mod_cie_10_or_21 Binary -0.02 0.00 0.00 -0.01 -0.02
With couple/children vs. alone cnt_mod_cie_10_or_31 Binary -0.03 -0.01 -0.02 -0.02 -0.03
With couple/children vs. Family of origin edad_al_ing2 Contin. 0.45 0.82 0.01 0.97 0.03 0.94 0.09 0.91 0.15 0.89
With couple/children vs. Family of origin sexo_2_Women2 Binary 0.22 0.01 0.01 0.05 0.09
With couple/children vs. Family of origin escolaridad_rec_3-Completed primary school or less2 Binary 0.13 0.00 0.00 0.02 0.04
With couple/children vs. Family of origin escolaridad_rec_2-Completed high school or less2 Binary -0.01 0.00 0.00 0.00 -0.01
With couple/children vs. Family of origin escolaridad_rec_1-More than high school2 Binary -0.14 0.00 -0.01 -0.03 -0.04
With couple/children vs. Family of origin sus_ini_mod_mvv_Alcohol2 Binary 0.06 0.01 0.01 0.02 0.02
With couple/children vs. Family of origin sus_ini_mod_mvv_Cocaine hydrochloride2 Binary 0.07 0.00 0.00 0.01 0.02
With couple/children vs. Family of origin sus_ini_mod_mvv_Marijuana2 Binary -0.10 -0.01 -0.01 -0.03 -0.04
With couple/children vs. Family of origin sus_ini_mod_mvv_Other2 Binary 0.02 0.00 0.00 0.00 0.00
With couple/children vs. Family of origin sus_ini_mod_mvv_Cocaine paste2 Binary 0.03 0.00 0.00 0.01 0.01
With couple/children vs. Family of origin freq_cons_sus_prin_Less than 1 day a week2 Binary 0.13 0.00 0.01 0.03 0.05
With couple/children vs. Family of origin freq_cons_sus_prin_1 day a week or more2 Binary 0.13 0.00 0.01 0.02 0.05
With couple/children vs. Family of origin freq_cons_sus_prin_2 to 3 days a week2 Binary 0.12 0.00 0.00 0.02 0.04
With couple/children vs. Family of origin freq_cons_sus_prin_4 to 6 days a week2 Binary -0.04 0.00 0.00 0.00 -0.01
With couple/children vs. Family of origin freq_cons_sus_prin_Daily2 Binary -0.20 0.00 -0.01 -0.04 -0.07
With couple/children vs. Family of origin dg_trs_cons_sus_or_Hazardous consumption2 Binary 0.12 0.00 0.00 0.02 0.04
With couple/children vs. Family of origin sus_principal_mod_Alcohol2 Binary 0.09 0.01 0.01 0.02 0.03
With couple/children vs. Family of origin sus_principal_mod_Cocaine hydrochloride2 Binary 0.08 0.00 0.01 0.02 0.03
With couple/children vs. Family of origin sus_principal_mod_Marijuana2 Binary -0.11 -0.01 -0.02 -0.03 -0.04
With couple/children vs. Family of origin sus_principal_mod_Other2 Binary -0.06 -0.01 -0.02 -0.02 -0.03
With couple/children vs. Family of origin sus_principal_mod_Cocaine paste2 Binary -0.06 0.00 0.00 -0.01 -0.02
With couple/children vs. Family of origin cnt_mod_cie_10_or_02 Binary 0.12 0.00 0.01 0.02 0.04
With couple/children vs. Family of origin cnt_mod_cie_10_or_12 Binary -0.11 0.00 0.00 -0.02 -0.04
With couple/children vs. Family of origin cnt_mod_cie_10_or_22 Binary -0.05 -0.01 -0.01 -0.02 -0.02
With couple/children vs. Family of origin cnt_mod_cie_10_or_32 Binary -0.03 0.00 -0.01 -0.01 -0.01

Time for this code chunk to run: 0 minutes

Show code
rbind(cbind(comp="Family of origin vs. alone",bal_tab$Pair.Balance$`Family of origin vs. Alone`$Balance),
      cbind(comp="With couple/children vs. alone",bal_tab$Pair.Balance$`With couple/children vs. Alone`$Balance),
      cbind(comp="With couple/children vs. Family of origin",bal_tab$Pair.Balance$`With couple/children vs. Family of origin`$Balance)) %>%
    data.table::data.table(keep.rownames = T) %>% 
    dplyr::select(comp, rn, everything()) %>% 
    dplyr::mutate_if(is.numeric, ~abs(round(.,2))) %>% 
    dplyr::group_by(comp) %>% 
    dplyr::summarise_if(is.numeric, ~list(max= max(.,na.rm=T))) %>% t() %>% 
  janitor::row_to_names(row_number = 1) %>%
  knitr::kable("markdown", caption="Balance Measures (absolute measures)")
Table 12: Balance Measures (absolute measures)
Family of origin vs. alone With couple/children vs. alone With couple/children vs. Family of origin
Diff.Un 0.36 0.37 0.45
V.Ratio.Un 1.05 0.86 0.82
Diff.Multinomial Logistic, not stabilized 0.04 0.04 0.01
V.Ratio.Multinomial Logistic, not stabilized 0.94 0.91 0.97
Diff.Multinomial Logistic, stabilized at 1% 0.04 0.04 0.03
V.Ratio.Multinomial Logistic, stabilized at 1% 0.95 0.9 0.94
Diff.Multinomial Logistic, stabilized at 5% 0.06 0.09 0.09
V.Ratio.Multinomial Logistic, stabilized at 5% 0.96 0.87 0.91
Diff.Multinomial Logistic, stabilized at 10% 0.1 0.15 0.15
V.Ratio.Multinomial Logistic, stabilized at 10% 0.97 0.86 0.89
Show code
#Diff.Multinomial Logistic, not stabilized is the best weight_mult_a00

Time for this code chunk to run: 0 minutes

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

dplyr::bind_rows(cbind(comp="Family of origin vs. alone",bal_tab$Pair.Balance$`Family of origin vs. Alone`$Observations),
      cbind(comp="With couple/children vs. alone",bal_tab$Pair.Balance$`With couple/children vs. Alone`$Observations),
      cbind(comp="With couple/children vs. Family of origin",bal_tab$Pair.Balance$`With couple/children vs. Family of origin`$Observations)) %>% 
  data.table::data.table(keep.rownames = T) %>% 
  dplyr::select(comp, rn, everything()) %>% 
  dplyr::mutate_if(is.numeric, ~round(.,2)) %>%
  #dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Effective sample sizes")
Table 13: Effective sample sizes
comp rn Alone Family of origin With couple/children
Family of origin vs. alone All…1 1335.00 15455.00
Family of origin vs. alone Multinomial Logistic, not stabilized…2 1091.03 14731.59
Family of origin vs. alone Multinomial Logistic, stabilized at 1%…3 1130.41 14750.96
Family of origin vs. alone Multinomial Logistic, stabilized at 5%…4 1195.12 14861.87
Family of origin vs. alone Multinomial Logistic, stabilized at 10%…5 1248.04 14976.33
With couple/children vs. alone All…6 1335.00 7189.00
With couple/children vs. alone Multinomial Logistic, not stabilized…7 1091.03 5969.68
With couple/children vs. alone Multinomial Logistic, stabilized at 1%…8 1130.41 6159.63
With couple/children vs. alone Multinomial Logistic, stabilized at 5%…9 1195.12 6487.32
With couple/children vs. alone Multinomial Logistic, stabilized at 10%…10 1248.04 6741.12
With couple/children vs. Family of origin All…11 15455.00 7189.00
With couple/children vs. Family of origin Multinomial Logistic, not stabilized…12 14731.59 5969.68
With couple/children vs. Family of origin Multinomial Logistic, stabilized at 1%…13 14750.96 6159.63
With couple/children vs. Family of origin Multinomial Logistic, stabilized at 5%…14 14861.87 6487.32
With couple/children vs. Family of origin Multinomial Logistic, stabilized at 10%…15 14976.33 6741.12

Time for this code chunk to run: 0 minutes

The best model was weight_mult_a00 (stabilized and not truncated). Despite the groups With couple/children and Alone suffered the second greater lost in terms of effective sample sizes (from 7189 to 5970 and from 1335 to 1091, respectively).


Alternative analyses

We tested weigths that account for a multinomial response using Propensity score weighting using Bayesian additive regression trees (BART) (weight_mult_a0), multinomial regression (weight_mult_a), and Generalized Boosted Regression Modeling with multinomial response on the average effect on the treated or ATE (weight_mult_b), weights with multiple treatments or ATO (Li, & Li, 2019) (weight_mult_c) and weights to simultaneously compare three treatment groups or ATM (Yoshida et al., 2007) (weight_mult_d).

Show code
#load(paste0(dirname(rstudioapi::getSourceEditorContext()$path),"/__analisis_joel.RData"))
prueba2_imp22<-prueba2_imp21 %>% dplyr::mutate(con_quien_vive_joel= factor(con_quien_vive_joel)) # %>%  dplyr::mutate(freq_cons_sus_prin=ordered(freq_cons_sus_prin,levels=c("Less than 1 day a week", "1 day a week or more", "2 to 3 days a week","4 to 6 days a week", "Daily")))
#, numero_de_hijos_mod_joel= as.ordered(numero_de_hijos_mod_joel)) %>%
#A multi-category example using GBM predicted probabilities
library(caret)

Loading required package: lattice

Attaching package: ‘caret’

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

sensitivity

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

cluster

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

lift
Show code
set.seed(2125)
W1 <- weightit(as.formula(paste("con_quien_vive_joel~", paste(vars, collapse = "+"))),
               data=prueba2_imp22, stabilize = T,
                method = "bart", estimand = "ATE", int=T,
                use.mlogit = FALSE)
prueba2_imp22$weight_mult_a0 <-W1$weights

W2 <- weightit(as.formula(paste("con_quien_vive_joel~", paste(vars, collapse = "+"))),
               data=prueba2_imp22, stabilize = T,
                method = "ps", estimand = "ATE", int=T,
                use.mlogit = FALSE)
prueba2_imp22$weight_mult_a <-W2$weights

#Here, we'll set multinomial distribution, 10 cross-validation fold, and 200 trees.
#learning rate (shrinkage) of 0.001. This is a very small learning rate and typically requires a large number of trees to find the minimum MSE. 
#find the better model
#https://www.webpages.uidaho.edu/~stevel/517/Gradient%20Boosting%20Machines%20at%20UC.html
set.seed(2125)
mod_gbm = gbm::gbm(as.formula(paste("con_quien_vive_joel~", paste(vars, collapse = "+"))),
              data = prueba2_imp22,
              distribution = "multinomial",
              cv.folds = 10,
              shrinkage = .001,
              n.minobsinnode = 10,
              n.trees = 5e3)
#gbm.perf(mod_gbm, method = "cv") weightit.fit

ps.multi <- drop(predict(mod_gbm, type = "response", n.trees = 5e3))
#https://ngreifer.github.io/WeightIt/reference/method_ps.html
#https://search.r-project.org/CRAN/refmans/WeightIt/html/get_w_from_ps.html
prueba2_imp22$weight_mult_b <- WeightIt::get_w_from_ps(ps.multi, prueba2_imp22$con_quien_vive_joel, estimand = "ATE", stabilize=T)
prueba2_imp22$weight_mult_c <- WeightIt::get_w_from_ps(ps.multi, prueba2_imp22$con_quien_vive_joel, estimand = "ATO", stabilize=T)
prueba2_imp22$weight_mult_d <- WeightIt::get_w_from_ps(ps.multi, prueba2_imp22$con_quien_vive_joel, estimand = "ATM", stabilize=T)

# Multinomial Treatments
# 
# - estimand = "ATO"
# Li, F., & Li, F. (2019). Propensity score weighting for causal inference with multiple treatments. The Annals of Applied Statistics, 13(4), 2389<U+2013>2415. 10.1214/19-AOAS1282
# 
# - estimand = "ATM"

Time for this code chunk to run: 0.1 minutes

We assess the covariates’ balance after the weighting

Show code
bal_tab2<-
cobalt::bal.tab(con_quien_vive_joel ~ edad_al_ing+ sexo_2 + escolaridad_rec + sus_ini_mod_mvv + freq_cons_sus_prin + dg_trs_cons_sus_or + sus_principal_mod+ cnt_mod_cie_10_or, data = prueba2_imp22,
                weights = list("Multinomial Logistic, stabilized but not truncated" = "weight_mult_a00", "Multinomial Logistic, stabilized at 1%"="weight_tr", "Multinomial Logistic, stabilized at 5%"="weight_tr2", "Multinomial Logistic, stabilized at 10%"="weight_tr3", "Bayesian additive regression trees (BART)"="weight_mult_a0", "Propensity Scores (WeightIt)"="weight_mult_a", "Generalized Boosted Regression Modeling with multinomial response, ATE"="weight_mult_b", "Generalized Boosted Regression Modeling with multinomial response, ATO"="weight_mult_c", "Generalized Boosted Regression Modeling with multinomial response, ATM"="weight_mult_d"),
                estimand = "ATE",
                which.treat = .all,
                un = T, 
                thresholds = c(cor = .2),
                 binary = "std", continuous = "std",
                stats = c("mean.diffs", "variance.ratios"))


rbind(cbind(comp="Family of origin vs. alone",bal_tab2$Pair.Balance$`Family of origin vs. Alone`$Balance),
      cbind(comp="With couple/children vs. alone",bal_tab2$Pair.Balance$`With couple/children vs. Alone`$Balance),
      cbind(comp="With couple/children vs. Family of origin",bal_tab2$Pair.Balance$`With couple/children vs. Family of origin`$Balance)) %>% 
  data.table::data.table(keep.rownames = T) %>% 
  dplyr::select(comp, rn, everything()) %>% 
  dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Balance Measures (multinomial response weights)")
Table 14: Balance Measures (multinomial response weights)
comp rn Type Diff.Un V.Ratio.Un Diff.Multinomial Logistic, stabilized but not truncated V.Ratio.Multinomial Logistic, stabilized but not truncated Diff.Multinomial Logistic, stabilized at 1% V.Ratio.Multinomial Logistic, stabilized at 1% Diff.Multinomial Logistic, stabilized at 5% V.Ratio.Multinomial Logistic, stabilized at 5% Diff.Multinomial Logistic, stabilized at 10% V.Ratio.Multinomial Logistic, stabilized at 10% Diff.Bayesian additive regression trees (BART) V.Ratio.Bayesian additive regression trees (BART) Diff.Propensity Scores (WeightIt) V.Ratio.Propensity Scores (WeightIt) Diff.Generalized Boosted Regression Modeling with multinomial response, ATE V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATE Diff.Generalized Boosted Regression Modeling with multinomial response, ATO V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATO Diff.Generalized Boosted Regression Modeling with multinomial response, ATM V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATM
Family of origin vs. alone edad_al_ing Contin. -0.36 1.05 0.00 0.94 -0.01 0.95 -0.06 0.96 -0.10 0.97 -0.02 1.01 0.00 0.96 -0.05 0.99 -0.03 0.99 -0.03 0.99
Family of origin vs. alone sexo_2_Women Binary 0.07 0.00 0.02 0.04 0.05 0.01 0.00 0.06 0.05 0.05
Family of origin vs. alone escolaridad_rec_3-Completed primary school or less Binary -0.12 0.00 0.00 -0.02 -0.04 0.00 0.00 -0.03 -0.03 -0.03
Family of origin vs. alone escolaridad_rec_2-Completed high school or less Binary 0.07 0.01 0.00 0.02 0.03 0.01 0.01 0.04 0.03 0.03
Family of origin vs. alone escolaridad_rec_1-More than high school Binary 0.06 -0.02 -0.01 0.00 0.01 -0.01 -0.02 -0.01 0.00 0.00
Family of origin vs. alone sus_ini_mod_mvv_Alcohol Binary -0.04 -0.02 -0.02 -0.02 -0.02 -0.01 -0.03 -0.02 -0.01 0.00
Family of origin vs. alone sus_ini_mod_mvv_Cocaine hydrochloride Binary 0.04 0.01 0.02 0.03 0.04 0.03 0.01 0.04 0.03 0.03
Family of origin vs. alone sus_ini_mod_mvv_Marijuana Binary 0.05 0.00 0.00 0.00 0.01 -0.01 0.01 0.00 0.00 0.00
Family of origin vs. alone sus_ini_mod_mvv_Other Binary -0.02 0.01 0.01 0.00 0.00 0.02 0.01 0.00 -0.01 -0.01
Family of origin vs. alone sus_ini_mod_mvv_Cocaine paste Binary -0.05 0.03 0.02 0.01 0.00 0.02 0.03 -0.01 -0.02 -0.03
Family of origin vs. alone freq_cons_sus_prin_Less than 1 day a week Binary 0.03 -0.01 0.00 0.01 0.02 0.01 -0.01 0.01 0.01 0.01
Family of origin vs. alone freq_cons_sus_prin_1 day a week or more Binary 0.07 0.00 0.02 0.04 0.05 0.01 0.00 0.06 0.05 0.04
Family of origin vs. alone freq_cons_sus_prin_2 to 3 days a week Binary 0.08 0.00 0.00 0.02 0.04 0.00 0.00 0.01 0.01 0.01
Family of origin vs. alone freq_cons_sus_prin_4 to 6 days a week Binary 0.07 -0.01 -0.01 0.00 0.01 -0.01 -0.01 0.00 0.01 0.01
Family of origin vs. alone freq_cons_sus_prin_Daily Binary -0.17 0.01 0.00 -0.04 -0.08 0.00 0.01 -0.05 -0.04 -0.05
Family of origin vs. alone dg_trs_cons_sus_or_Hazardous consumption Binary 0.02 0.00 0.00 0.01 0.02 0.01 0.00 0.00 -0.01 -0.01
Family of origin vs. alone sus_principal_mod_Alcohol Binary -0.19 -0.01 -0.02 -0.05 -0.08 -0.01 -0.01 -0.07 -0.07 -0.07
Family of origin vs. alone sus_principal_mod_Cocaine hydrochloride Binary 0.17 -0.03 0.00 0.04 0.08 -0.02 -0.03 0.05 0.06 0.06
Family of origin vs. alone sus_principal_mod_Marijuana Binary 0.07 -0.01 -0.02 -0.01 0.00 -0.01 -0.02 -0.01 -0.01 -0.01
Family of origin vs. alone sus_principal_mod_Other Binary 0.02 -0.02 -0.02 -0.01 -0.01 0.01 -0.02 0.01 0.01 0.01
Family of origin vs. alone sus_principal_mod_Cocaine paste Binary -0.03 0.04 0.04 0.02 0.00 0.03 0.05 0.02 0.01 0.01
Family of origin vs. alone cnt_mod_cie_10_or_0 Binary 0.06 0.02 0.02 0.04 0.05 0.00 0.02 0.05 0.04 0.04
Family of origin vs. alone cnt_mod_cie_10_or_1 Binary -0.07 -0.02 -0.02 -0.04 -0.05 -0.01 -0.02 -0.06 -0.04 -0.04
Family of origin vs. alone cnt_mod_cie_10_or_2 Binary 0.02 0.01 0.01 0.01 0.01 0.02 0.01 0.03 0.02 0.02
Family of origin vs. alone cnt_mod_cie_10_or_3 Binary 0.00 -0.01 -0.01 -0.01 -0.01 -0.03 -0.01 -0.02 -0.01 -0.01
With couple/children vs. alone edad_al_ing1 Contin. 0.09 0.86 0.01 0.91 0.02 0.90 0.04 0.87 0.05 0.86 -0.01 1.01 0.01 0.92 0.01 0.97 0.00 0.98 0.00 0.98
With couple/children vs. alone sexo_2_Women1 Binary 0.29 0.01 0.03 0.09 0.14 0.01 0.00 0.12 0.11 0.11
With couple/children vs. alone escolaridad_rec_3-Completed primary school or less1 Binary 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.01 0.00
With couple/children vs. alone escolaridad_rec_2-Completed high school or less1 Binary 0.05 0.01 0.01 0.01 0.03 0.01 0.01 0.03 0.03 0.04
With couple/children vs. alone escolaridad_rec_1-More than high school1 Binary -0.08 -0.01 -0.01 -0.02 -0.04 -0.01 -0.01 -0.06 -0.06 -0.05
With couple/children vs. alone sus_ini_mod_mvv_Alcohol1 Binary 0.02 -0.01 -0.01 0.00 0.00 -0.01 -0.01 0.01 0.02 0.03
With couple/children vs. alone sus_ini_mod_mvv_Cocaine hydrochloride1 Binary 0.11 0.01 0.02 0.04 0.06 0.03 0.01 0.08 0.07 0.06
With couple/children vs. alone sus_ini_mod_mvv_Marijuana1 Binary -0.06 -0.01 -0.01 -0.02 -0.03 -0.02 0.00 -0.05 -0.04 -0.05
With couple/children vs. alone sus_ini_mod_mvv_Other1 Binary 0.00 0.01 0.01 0.01 0.00 0.02 0.01 0.01 0.00 0.00
With couple/children vs. alone sus_ini_mod_mvv_Cocaine paste1 Binary -0.01 0.03 0.03 0.02 0.01 0.02 0.03 0.01 0.00 -0.01
With couple/children vs. alone freq_cons_sus_prin_Less than 1 day a week1 Binary 0.16 0.00 0.01 0.04 0.07 0.02 0.00 0.05 0.04 0.04
With couple/children vs. alone freq_cons_sus_prin_1 day a week or more1 Binary 0.21 0.00 0.02 0.07 0.10 0.01 0.00 0.09 0.07 0.07
With couple/children vs. alone freq_cons_sus_prin_2 to 3 days a week1 Binary 0.20 0.00 0.00 0.03 0.07 0.00 0.00 0.03 0.03 0.04
With couple/children vs. alone freq_cons_sus_prin_4 to 6 days a week1 Binary 0.02 -0.01 -0.01 0.00 0.00 -0.01 -0.01 0.01 0.02 0.03
With couple/children vs. alone freq_cons_sus_prin_Daily1 Binary -0.37 0.01 -0.02 -0.08 -0.15 -0.01 0.01 -0.09 -0.10 -0.11
With couple/children vs. alone dg_trs_cons_sus_or_Hazardous consumption1 Binary 0.13 0.00 0.01 0.03 0.06 0.01 0.00 0.05 0.04 0.04
With couple/children vs. alone sus_principal_mod_Alcohol1 Binary -0.10 0.00 -0.01 -0.03 -0.05 -0.01 0.00 -0.03 -0.03 -0.02
With couple/children vs. alone sus_principal_mod_Cocaine hydrochloride1 Binary 0.26 -0.03 0.00 0.06 0.11 -0.02 -0.03 0.09 0.09 0.09
With couple/children vs. alone sus_principal_mod_Marijuana1 Binary -0.05 -0.03 -0.04 -0.05 -0.05 -0.02 -0.03 -0.04 -0.02 -0.02
With couple/children vs. alone sus_principal_mod_Other1 Binary -0.04 -0.03 -0.03 -0.03 -0.03 -0.01 -0.03 -0.04 -0.03 -0.03
With couple/children vs. alone sus_principal_mod_Cocaine paste1 Binary -0.09 0.04 0.04 0.01 -0.01 0.04 0.05 -0.01 -0.03 -0.04
With couple/children vs. alone cnt_mod_cie_10_or_01 Binary 0.19 0.02 0.03 0.06 0.09 0.00 0.02 0.10 0.09 0.09
With couple/children vs. alone cnt_mod_cie_10_or_11 Binary -0.18 -0.02 -0.03 -0.05 -0.08 0.00 -0.01 -0.09 -0.08 -0.08
With couple/children vs. alone cnt_mod_cie_10_or_21 Binary -0.02 0.00 0.00 -0.01 -0.02 0.01 0.00 -0.02 -0.02 -0.02
With couple/children vs. alone cnt_mod_cie_10_or_31 Binary -0.03 -0.01 -0.02 -0.02 -0.03 -0.05 -0.02 -0.04 -0.04 -0.03
With couple/children vs. Family of origin edad_al_ing2 Contin. 0.45 0.82 0.01 0.97 0.03 0.94 0.09 0.91 0.15 0.89 0.01 1.01 0.01 0.96 0.06 0.98 0.03 0.99 0.03 0.99
With couple/children vs. Family of origin sexo_2_Women2 Binary 0.22 0.01 0.01 0.05 0.09 0.00 0.01 0.05 0.06 0.06
With couple/children vs. Family of origin escolaridad_rec_3-Completed primary school or less2 Binary 0.13 0.00 0.00 0.02 0.04 0.00 0.00 0.05 0.04 0.03
With couple/children vs. Family of origin escolaridad_rec_2-Completed high school or less2 Binary -0.01 0.00 0.00 0.00 -0.01 0.00 0.00 0.00 0.00 0.01
With couple/children vs. Family of origin escolaridad_rec_1-More than high school2 Binary -0.14 0.00 -0.01 -0.03 -0.04 -0.01 0.00 -0.06 -0.05 -0.05
With couple/children vs. Family of origin sus_ini_mod_mvv_Alcohol2 Binary 0.06 0.01 0.01 0.02 0.02 0.00 0.01 0.02 0.02 0.03
With couple/children vs. Family of origin sus_ini_mod_mvv_Cocaine hydrochloride2 Binary 0.07 0.00 0.00 0.01 0.02 0.00 0.00 0.04 0.03 0.03
With couple/children vs. Family of origin sus_ini_mod_mvv_Marijuana2 Binary -0.10 -0.01 -0.01 -0.03 -0.04 -0.01 -0.01 -0.05 -0.05 -0.05
With couple/children vs. Family of origin sus_ini_mod_mvv_Other2 Binary 0.02 0.00 0.00 0.00 0.00 0.00 0.00 0.01 0.01 0.01
With couple/children vs. Family of origin sus_ini_mod_mvv_Cocaine paste2 Binary 0.03 0.00 0.00 0.01 0.01 0.00 0.00 0.02 0.02 0.02
With couple/children vs. Family of origin freq_cons_sus_prin_Less than 1 day a week2 Binary 0.13 0.00 0.01 0.03 0.05 0.01 0.01 0.03 0.03 0.03
With couple/children vs. Family of origin freq_cons_sus_prin_1 day a week or more2 Binary 0.13 0.00 0.01 0.02 0.05 0.00 0.00 0.03 0.03 0.03
With couple/children vs. Family of origin freq_cons_sus_prin_2 to 3 days a week2 Binary 0.12 0.00 0.00 0.02 0.04 0.00 0.00 0.01 0.02 0.02
With couple/children vs. Family of origin freq_cons_sus_prin_4 to 6 days a week2 Binary -0.04 0.00 0.00 0.00 -0.01 0.00 0.00 0.01 0.01 0.01
With couple/children vs. Family of origin freq_cons_sus_prin_Daily2 Binary -0.20 0.00 -0.01 -0.04 -0.07 0.00 0.00 -0.05 -0.06 -0.06
With couple/children vs. Family of origin dg_trs_cons_sus_or_Hazardous consumption2 Binary 0.12 0.00 0.00 0.02 0.04 0.00 0.00 0.05 0.05 0.05
With couple/children vs. Family of origin sus_principal_mod_Alcohol2 Binary 0.09 0.01 0.01 0.02 0.03 0.00 0.01 0.04 0.04 0.05
With couple/children vs. Family of origin sus_principal_mod_Cocaine hydrochloride2 Binary 0.08 0.00 0.01 0.02 0.03 0.00 0.00 0.03 0.03 0.03
With couple/children vs. Family of origin sus_principal_mod_Marijuana2 Binary -0.11 -0.01 -0.02 -0.03 -0.04 -0.01 -0.01 -0.03 -0.01 -0.01
With couple/children vs. Family of origin sus_principal_mod_Other2 Binary -0.06 -0.01 -0.02 -0.02 -0.03 -0.01 -0.01 -0.05 -0.04 -0.04
With couple/children vs. Family of origin sus_principal_mod_Cocaine paste2 Binary -0.06 0.00 0.00 -0.01 -0.02 0.00 0.00 -0.03 -0.05 -0.05
With couple/children vs. Family of origin cnt_mod_cie_10_or_02 Binary 0.12 0.00 0.01 0.02 0.04 0.00 0.00 0.05 0.05 0.05
With couple/children vs. Family of origin cnt_mod_cie_10_or_12 Binary -0.11 0.00 0.00 -0.02 -0.04 0.00 0.00 -0.04 -0.04 -0.04
With couple/children vs. Family of origin cnt_mod_cie_10_or_22 Binary -0.05 -0.01 -0.01 -0.02 -0.02 -0.01 -0.01 -0.04 -0.04 -0.04
With couple/children vs. Family of origin cnt_mod_cie_10_or_32 Binary -0.03 0.00 -0.01 -0.01 -0.01 -0.02 0.00 -0.03 -0.02 -0.02
Show code
  # knitr::kable("html", caption="Balance Measures") %>% 
  #      kableExtra::kable_classic()

Time for this code chunk to run: 0.1 minutes

Show code
rbind(cbind(comp="Family of origin vs. alone",bal_tab2$Pair.Balance$`Family of origin vs. Alone`$Balance),
      cbind(comp="With couple/children vs. alone",bal_tab2$Pair.Balance$`With couple/children vs. Alone`$Balance),
      cbind(comp="With couple/children vs. Family of origin",bal_tab2$Pair.Balance$`With couple/children vs. Family of origin`$Balance)) %>% 
    data.table::data.table(keep.rownames = T) %>% 
    dplyr::select(comp, rn, everything()) %>% 
    dplyr::mutate_if(is.numeric, ~abs(round(.,2))) %>% 
    dplyr::group_by(comp) %>% 
    dplyr::summarise_if(is.numeric, ~list(max= max(.,na.rm=T))) %>% t() %>% 
  janitor::row_to_names(row_number = 1) %>%
  knitr::kable("markdown", caption="Balance Measures (multinomial weights) (absolute measures)")
Table 15: Balance Measures (multinomial weights) (absolute measures)
Family of origin vs. alone With couple/children vs. alone With couple/children vs. Family of origin
Diff.Un 0.36 0.37 0.45
V.Ratio.Un 1.05 0.86 0.82
Diff.Multinomial Logistic, stabilized but not truncated 0.04 0.04 0.01
V.Ratio.Multinomial Logistic, stabilized but not truncated 0.94 0.91 0.97
Diff.Multinomial Logistic, stabilized at 1% 0.04 0.04 0.03
V.Ratio.Multinomial Logistic, stabilized at 1% 0.95 0.9 0.94
Diff.Multinomial Logistic, stabilized at 5% 0.06 0.09 0.09
V.Ratio.Multinomial Logistic, stabilized at 5% 0.96 0.87 0.91
Diff.Multinomial Logistic, stabilized at 10% 0.1 0.15 0.15
V.Ratio.Multinomial Logistic, stabilized at 10% 0.97 0.86 0.89
Diff.Bayesian additive regression trees (BART) 0.03 0.05 0.02
V.Ratio.Bayesian additive regression trees (BART) 1.01 1.01 1.01
Diff.Propensity Scores (WeightIt) 0.05 0.05 0.01
V.Ratio.Propensity Scores (WeightIt) 0.96 0.92 0.96
Diff.Generalized Boosted Regression Modeling with multinomial response, ATE 0.07 0.12 0.06
V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATE 0.99 0.97 0.98
Diff.Generalized Boosted Regression Modeling with multinomial response, ATO 0.07 0.11 0.06
V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATO 0.99 0.98 0.99
Diff.Generalized Boosted Regression Modeling with multinomial response, ATM 0.07 0.11 0.06
V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATM 0.99 0.98 0.99
Show code
#Diff.Multinomial Logistic, not stabilized is the best weight_mult_a00

Time for this code chunk to run: 0 minutes

Show code
rbind(cbind(comp="Family of origin vs. alone",bal_tab2$Pair.Balance$`Family of origin vs. Alone`$Balance),
      cbind(comp="With couple/children vs. alone",bal_tab2$Pair.Balance$`With couple/children vs. Alone`$Balance),
      cbind(comp="With couple/children vs. Family of origin",bal_tab2$Pair.Balance$`With couple/children vs. Family of origin`$Balance)) %>% 
    data.table::data.table(keep.rownames = T) %>% 
    dplyr::select(comp, rn, everything()) %>% 
    dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
    dplyr::summarise_if(is.numeric, ~list(min= min(.,na.rm=T), max= max(.,na.rm=T))) %>% t() %>%
  knitr::kable("markdown", caption="Balance Measures (multinomial response weights)", col.names=c("min", "max"))
Table 16: Balance Measures (multinomial response weights)
min max
Diff.Un -0.37 0.45
V.Ratio.Un 0.82 1.05
Diff.Multinomial Logistic, stabilized but not truncated -0.03 0.04
V.Ratio.Multinomial Logistic, stabilized but not truncated 0.91 0.97
Diff.Multinomial Logistic, stabilized at 1% -0.04 0.04
V.Ratio.Multinomial Logistic, stabilized at 1% 0.9 0.95
Diff.Multinomial Logistic, stabilized at 5% -0.08 0.09
V.Ratio.Multinomial Logistic, stabilized at 5% 0.87 0.96
Diff.Multinomial Logistic, stabilized at 10% -0.15 0.15
V.Ratio.Multinomial Logistic, stabilized at 10% 0.86 0.97
Diff.Bayesian additive regression trees (BART) -0.05 0.04
V.Ratio.Bayesian additive regression trees (BART) 1.01 1.01
Diff.Propensity Scores (WeightIt) -0.03 0.05
V.Ratio.Propensity Scores (WeightIt) 0.92 0.96
Diff.Generalized Boosted Regression Modeling with multinomial response, ATE -0.09 0.12
V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATE 0.97 0.99
Diff.Generalized Boosted Regression Modeling with multinomial response, ATO -0.1 0.11
V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATO 0.98 0.99
Diff.Generalized Boosted Regression Modeling with multinomial response, ATM -0.11 0.11
V.Ratio.Generalized Boosted Regression Modeling with multinomial response, ATM 0.98 0.99

Time for this code chunk to run: 0 minutes

The bayesian additive regression trees (weight_mult_a0) resembles classic multinomial logistic regression with stablized but not truncated? weights (weight_mult_a00) in terms of SMDs, but not in variance ratios (continuous measures).

Show code
bal_tab2$Observations %>% 
  #dplyr::mutate_if(is.numeric, ~round(.,2)) %>% 
  knitr::kable("markdown", caption="Effective sample sizes")

Table: (#tab:post-rmst2-alt-an)Effective sample sizes

Show code
dplyr::bind_rows(cbind(comp="Family of origin vs. alone",bal_tab2$Pair.Balance$`Family of origin vs. Alone`$Observations),
      cbind(comp="With couple/children vs. alone",bal_tab2$Pair.Balance$`With couple/children vs. Alone`$Observations),
      cbind(comp="With couple/children vs. Family of origin",bal_tab2$Pair.Balance$`With couple/children vs. Family of origin`$Observations)) %>% 
  data.table::data.table(keep.rownames = T) %>% 
  knitr::kable("markdown", caption="Effective sample sizes")
Table 17: Effective sample sizes
rn comp Alone Family of origin With couple/children
All…1 Family of origin vs. alone 1335.000 15455.00
Multinomial Logistic, stabilized but not truncated…2 Family of origin vs. alone 1091.035 14731.59
Multinomial Logistic, stabilized at 1%…3 Family of origin vs. alone 1130.412 14750.96
Multinomial Logistic, stabilized at 5%…4 Family of origin vs. alone 1195.124 14861.87
Multinomial Logistic, stabilized at 10%…5 Family of origin vs. alone 1248.035 14976.33
Bayesian additive regression trees (BART)…6 Family of origin vs. alone 1080.698 14819.13
Propensity Scores (WeightIt)…7 Family of origin vs. alone 1087.535 14753.89
Generalized Boosted Regression Modeling with multinomial response, ATE…8 Family of origin vs. alone 1213.564 14999.03
Generalized Boosted Regression Modeling with multinomial response, ATO…9 Family of origin vs. alone 1327.858 13531.08
Generalized Boosted Regression Modeling with multinomial response, ATM…10 Family of origin vs. alone 1335.000 13171.37
All…11 With couple/children vs. alone 1335.000 7189.000
Multinomial Logistic, stabilized but not truncated…12 With couple/children vs. alone 1091.035 5969.678
Multinomial Logistic, stabilized at 1%…13 With couple/children vs. alone 1130.412 6159.632
Multinomial Logistic, stabilized at 5%…14 With couple/children vs. alone 1195.124 6487.316
Multinomial Logistic, stabilized at 10%…15 With couple/children vs. alone 1248.035 6741.116
Bayesian additive regression trees (BART)…16 With couple/children vs. alone 1080.698 5753.688
Propensity Scores (WeightIt)…17 With couple/children vs. alone 1087.535 5971.800
Generalized Boosted Regression Modeling with multinomial response, ATE…18 With couple/children vs. alone 1213.564 6340.959
Generalized Boosted Regression Modeling with multinomial response, ATO…19 With couple/children vs. alone 1327.858 6414.802
Generalized Boosted Regression Modeling with multinomial response, ATM…20 With couple/children vs. alone 1335.000 6115.144
All…21 With couple/children vs. Family of origin 15455.00 7189.000
Multinomial Logistic, stabilized but not truncated…22 With couple/children vs. Family of origin 14731.59 5969.678
Multinomial Logistic, stabilized at 1%…23 With couple/children vs. Family of origin 14750.96 6159.632
Multinomial Logistic, stabilized at 5%…24 With couple/children vs. Family of origin 14861.87 6487.316
Multinomial Logistic, stabilized at 10%…25 With couple/children vs. Family of origin 14976.33 6741.116
Bayesian additive regression trees (BART)…26 With couple/children vs. Family of origin 14819.13 5753.688
Propensity Scores (WeightIt)…27 With couple/children vs. Family of origin 14753.89 5971.800
Generalized Boosted Regression Modeling with multinomial response, ATE…28 With couple/children vs. Family of origin 14999.03 6340.959
Generalized Boosted Regression Modeling with multinomial response, ATO…29 With couple/children vs. Family of origin 13531.08 6414.802
Generalized Boosted Regression Modeling with multinomial response, ATM…30 With couple/children vs. Family of origin 13171.37 6115.144

Time for this code chunk to run: 0 minutes

If BART model were selected, we could observe that the group With couple/children and Alone suffered the greater lost in terms of effective sample sizes (from 7189 to 5757 and from 1335 to 1086, respectively). We estimated the restricted survival times using both weights.

Show code
#(3) (PDF) Adjusted restricted mean survival times in observational studies. Available from: https://www.researchgate.net/publication/333326572_Adjusted_restricted_mean_survival_times_in_observational_studies [accessed Feb 02 2023].
#weight_mult_a00 weight_mult_a0

cat("Multinomial logistic, stabilized weights but not truncated?")
Multinomial logistic, stabilized weights but not truncated?
Show code
akm_rmst(time=prueba2_imp22$yrs_to_tr_dropout, status=prueba2_imp22$event, group=prueba2_imp22$con_quien_vive_joel,weight=prueba2_imp22$weight_mult_a00, tau=.5)




RMST calculated up to tau = 0.5


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.363 0.005
Group Family of origin       0.388 0.001
Group With couple/children   0.378 0.002


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.025 0.005
Groups With couple/children vs. Alone               0.015 0.005
Groups With couple/children vs. Family of origin   -0.010 0.002
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.016  0.035
Groups With couple/children vs. Alone               0.005  0.025
Groups With couple/children vs. Family of origin   -0.015 -0.006
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.004
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.068 0.014
Groups With couple/children vs. Alone                 0.040 0.014
Groups With couple/children vs. Family of origin     -0.027 0.006
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.070 1.042 1.099
Groups With couple/children vs. Alone              1.041 1.013 1.071
Groups With couple/children vs. Family of origin   0.973 0.962 0.985
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.005
Groups With couple/children vs. Family of origin   0.000
Show code
akm_rmst(time=prueba2_imp22$yrs_to_tr_dropout, status=prueba2_imp22$event, group=prueba2_imp22$con_quien_vive_joel,weight=prueba2_imp22$weight_mult_a00, tau=1)




RMST calculated up to tau = 1


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.551 0.011
Group Family of origin       0.601 0.003
Group With couple/children   0.567 0.004


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.049 0.012
Groups With couple/children vs. Alone               0.016 0.012
Groups With couple/children vs. Family of origin   -0.034 0.005
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.027  0.072
Groups With couple/children vs. Alone              -0.008  0.039
Groups With couple/children vs. Family of origin   -0.044 -0.023
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.189
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.086 0.021
Groups With couple/children vs. Alone                 0.028 0.022
Groups With couple/children vs. Family of origin     -0.057 0.009
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.089 1.046 1.135
Groups With couple/children vs. Alone              1.029 0.986 1.073
Groups With couple/children vs. Family of origin   0.944 0.927 0.962
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.194
Groups With couple/children vs. Family of origin   0.000
Show code
akm_rmst(time=prueba2_imp22$yrs_to_tr_dropout, status=prueba2_imp22$event, group=prueba2_imp22$con_quien_vive_joel,weight=prueba2_imp22$weight_mult_a00, tau=3)




RMST calculated up to tau = 3


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  1.132 0.038
Group Family of origin       1.242 0.010
Group With couple/children   1.108 0.015


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.109 0.039
Groups With couple/children vs. Alone              -0.024 0.041
Groups With couple/children vs. Family of origin   -0.133 0.018
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.032  0.186
Groups With couple/children vs. Alone              -0.104  0.056
Groups With couple/children vs. Family of origin   -0.169 -0.097
                                                       p
Groups Family of origin vs. Alone                  0.005
Groups With couple/children vs. Alone              0.556
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.092 0.034
Groups With couple/children vs. Alone                -0.021 0.036
Groups With couple/children vs. Family of origin     -0.114 0.016
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.097 1.025 1.173
Groups With couple/children vs. Alone              0.979 0.912 1.051
Groups With couple/children vs. Family of origin   0.893 0.865 0.921
                                                       p
Groups Family of origin vs. Alone                  0.007
Groups With couple/children vs. Alone              0.553
Groups With couple/children vs. Family of origin   0.000
Show code
cat("BART weights")
BART weights
Show code
akm_rmst(time=prueba2_imp22$yrs_to_tr_dropout, status=prueba2_imp22$event, group=prueba2_imp22$con_quien_vive_joel,weight=prueba2_imp22$weight_mult_a0, tau=.5)




RMST calculated up to tau = 0.5


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.362 0.005
Group Family of origin       0.388 0.001
Group With couple/children   0.378 0.002


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.026 0.005
Groups With couple/children vs. Alone               0.015 0.005
Groups With couple/children vs. Family of origin   -0.011 0.002
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.016  0.035
Groups With couple/children vs. Alone               0.005  0.025
Groups With couple/children vs. Family of origin   -0.015 -0.006
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.004
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.068 0.014
Groups With couple/children vs. Alone                 0.041 0.014
Groups With couple/children vs. Family of origin     -0.028 0.006
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.071 1.042 1.100
Groups With couple/children vs. Alone              1.041 1.013 1.071
Groups With couple/children vs. Family of origin   0.973 0.961 0.984
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.005
Groups With couple/children vs. Family of origin   0.000
Show code
akm_rmst(time=prueba2_imp22$yrs_to_tr_dropout, status=prueba2_imp22$event, group=prueba2_imp22$con_quien_vive_joel,weight=prueba2_imp22$weight_mult_a0, tau=1)




RMST calculated up to tau = 1


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  0.550 0.011
Group Family of origin       0.601 0.003
Group With couple/children   0.566 0.005


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.051 0.012
Groups With couple/children vs. Alone               0.016 0.012
Groups With couple/children vs. Family of origin   -0.034 0.005
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.028  0.073
Groups With couple/children vs. Alone              -0.007  0.040
Groups With couple/children vs. Family of origin   -0.045 -0.024
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.173
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.088 0.021
Groups With couple/children vs. Alone                 0.029 0.022
Groups With couple/children vs. Family of origin     -0.059 0.009
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.092 1.048 1.138
Groups With couple/children vs. Alone              1.030 0.987 1.075
Groups With couple/children vs. Family of origin   0.943 0.926 0.961
                                                       p
Groups Family of origin vs. Alone                  0.000
Groups With couple/children vs. Alone              0.178
Groups With couple/children vs. Family of origin   0.000
Show code
akm_rmst(time=prueba2_imp22$yrs_to_tr_dropout, status=prueba2_imp22$event, group=prueba2_imp22$con_quien_vive_joel,weight=prueba2_imp22$weight_mult_a0, tau=3)




RMST calculated up to tau = 3


Restricted Mean Survival Time (RMST) per Group 

                              RMST    SE
Group Alone                  1.128 0.038
Group Family of origin       1.242 0.010
Group With couple/children   1.105 0.015


Restricted Mean Survival Time (RMST) Differences 

                                                     Est.    SE
Groups Family of origin vs. Alone                   0.114 0.039
Groups With couple/children vs. Alone              -0.023 0.041
Groups With couple/children vs. Family of origin   -0.136 0.019
                                                      CIL    CIU
Groups Family of origin vs. Alone                   0.037  0.191
Groups With couple/children vs. Alone              -0.103  0.057
Groups With couple/children vs. Family of origin   -0.173 -0.100
                                                       p
Groups Family of origin vs. Alone                  0.004
Groups With couple/children vs. Alone              0.578
Groups With couple/children vs. Family of origin   0.000


Restricted Mean Survival Time (RMST) Ratios 

                                                   Log Est.    SE
Groups Family of origin vs. Alone                     0.096 0.035
Groups With couple/children vs. Alone                -0.020 0.036
Groups With couple/children vs. Family of origin     -0.116 0.016
                                                    Est.   CIL   CIU
Groups Family of origin vs. Alone                  1.101 1.029 1.178
Groups With couple/children vs. Alone              0.980 0.912 1.052
Groups With couple/children vs. Family of origin   0.890 0.862 0.919
                                                       p
Groups Family of origin vs. Alone                  0.005
Groups With couple/children vs. Alone              0.575
Groups With couple/children vs. Family of origin   0.000

Time for this code chunk to run: 0.1 minutes

Show code
#Asymptotic variances are calculated using the equations given in Xie and Liu (2005). It is also recommended to use stabilized weights by using stabilize=TRUE (the default value). More information can be found in the cited literature.

invisible(" Error in `stop_subscript()`:
# ! Can't subset columns that don't exist.
# x Column `vars` doesn't exist.")

logit2_adj_curv <- nnet::multinom(formula= con_quien_vive_joel + edad_al_ing+ sexo_2 + escolaridad_rec + sus_ini_mod_mvv + freq_cons_sus_prin + dg_trs_cons_sus_or + sus_principal_mod+ cnt_mod_cie_10_or, data=prueba2_imp22)

cat("========================================================")
#https://github.com/RobinDenz1/adjustedCurves
#https://cran.rstudio.com/web/packages/adjustedCurves/vignettes/introduction.html

# Those weights are used in a weighted version of the Kaplan-Meier estimator proposed by Xie and Liu (2005)
adjsurv <- adjustedCurves::adjustedsurv(data=prueba2_imp22,
                        variable="con_quien_vive_joel",
                        ev_time="yrs_to_tr_dropout",
                        event="event",
                        method="iptw_km",
                        treatment_model=logit2_adj_curv, stabilize=T,
                        bootstrap=TRUE, n_boot=500)
adjustedCurves::plot_curve_diff(adjsurv, group_1= "Family of origin", group_2="Alone", type="points")
adjustedCurves::plot_curve_diff(adjsurv, group_1= "Family of origin", group_2="With couple/children", type="points")

adjustedCurves::adjusted_curve_test(adjsurv, from=.5, to=.51)
adjustedCurves::adjusted_curve_test(adjsurv, from=1, to=1.01)
adjustedCurves::adjusted_curve_test(adjsurv, from=2.90, to=2.99)

cat("========================================================")
#https://cran.r-project.org/web/packages/cmprskcoxmsm/cmprskcoxmsm.pdf

adjcif <- cmprskcoxmsm::adjustedcif(data=prueba2_imp22,
                      variable="con_quien_vive_joel",
                      ev_time="yrs_to_tr_dropout_cmprsk",
                      event="event_cmprsk",
                      method="iptw",
                      treatment_model=logit2_adj_curv,
                      cause=1,
                      ties='efron' ,
                      conf_int=TRUE)

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

adjusted_rmtl(adjcif, to=0.5, conf_int=T)
prueba2_imp22$Trt<-prueba2_imp22$con_quien_vive_joel
prueba2_imp22$ipw_ate_stab <- prueba2_imp22$weight_mult_a
cif_est(data=prueba2_imp22,
        "yrs_to_tr_dropout_cmprsk", time2 = NULL,
        Event.var="event_cmprsk", Events=c(1,2), cif.event=1,
        weight.type="Stabilized",
        ties='efron',
        risktab = TRUE, risk.time = NULL)

invisible("no se obtienen intervalos de confianza de hrcomprisk y toma como 8 hrs con 20 rep")

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

#Exposure does not have 2 levels
#https://cran.r-project.org/web/packages/hrcomprisk/hrcomprisk.pdf
#https://www.rdocumentation.org/packages/hrcomprisk/versions/0.1.1/topics/hrcomprisk
#https://cran.r-project.org/web/packages/hrcomprisk/vignettes/hrcomprisk.html
prueba<-hrcomprisk::npcrest(df=prueba2_imp22, exit=yrs_to_tr_dropout_cmprsk, ipwvars= vars, event=event_cmprsk, exposure=con_quien_vive_joel_fam_or, rep=2e1, maxtime=3, print.attr=T)
#CIoinc_comp= Value of the unexposed (denoted by “o”) composite cumulative incidence at the given time.
#CIxinc_comp= Value of the exposed (denoted by “x”) composite cumulative incidence at the given time.
#CIoinc_1= Value of the unexposed cumulative incidence of event 1 at the given time.
#CIxinc_1=  Value of the exposed cumulative incidence of event 1 at the given time.
# R_1= Sub-hazard ratio/Cause-specific hazard ratio for event 1.
# R_2= Sub-hazard ratio/Cause-specific hazard ratio for event 2.
invisible("no se obtienen intervalos de confianza de hrcomprisk y toma como 8 hrs con 20 rep")

#con_quien_vive_joel_fam_or con_quien_vive_joel_coup_chil con_quien_vive_joel_alone
# 
# bootCRCumInc(
# df= prueba2_imp22,
# exit=yrs_to_tr_dropout_cmprsk,
# event=event_cmprsk,
# exposure=con_quien_vive_joel_fam_or,
# entry = NULL,
# weights = ipw_ate_stab,
# ipwvars = NULL,
# rep = 0,
# print.attr = T,
# seed = 54321
# )


cat("========================================================")
#https://github.com/Lesly1031/AdjKM.CIF/
#https://lesly1031.github.io/AdjKM.CIF/
library(AdjKM.CIF)
prueba2_imp22$Trt_num<-as.numeric(prueba2_imp22$con_quien_vive_joel)

sus_principal_mod
cnt_mod_cie_10_or              
sus_ini_mod                    

result1_boot <- AdjKM.CIF::boot_ci_adj_cif(boot_n = 2, ci_cut = c(0.025, 0.975), data = data.matrix(prueba2_imp22), time = "yrs_to_tr_dropout_cmprsk", status = "event_cmprsk", group = "Trt_num", covlist = vars, event_code = 1, stratified= "No", reference_group =NULL)
# Error in solve.default(h, z[[2]]) : 
#   system is computationally singular: reciprocal condition number = 5.60127e-19
#no aguanta predictores categóricos
#si uso data.matrix, se pitea
# Error in .cif_prob(data = data, coxout = coxout, base_res = CIF0, event_code = event_code,  : 
#   dims [product 674] do not match the length of object [675]
# In addition: Warning message:
# In adjcif + cif :
#   longer object length is not a multiple of shorter object length

Time for this code chunk to run: 0 minutes

We used the kaplan-meier curves with survey-weighting, according to a commentary made by Thomas Lumley.

Show code
#Rader, Kevin Andrew. 2014. Methods for Analyzing Survival and Binary Data in Complex Surveys. Doctoral dissertation, Harvard University.http://nrs.harvard.edu/urn-3:HUL.InstRepos:12274283

#load(paste0(dirname(rstudioapi::getSourceEditorContext()$path),"/__analisis_joel.RData"))
library(survey)
# survey::svylogrank
#First, you can use the survey::svylogrank function, as @IRTFM suggests. This will treat the weights as sampling weights, but I think that's ok with the robust standard errors that svylogrank uses.

design_weights <- survey::svydesign(ids = ~1, data = dplyr::mutate(prueba2_imp22, yrs_to_tr_dropout= dplyr::case_when(yrs_to_tr_dropout==0~0.0001,T~yrs_to_tr_dropout)), weights = ~weight_mult_a0)
# Error in .logrank(formula, design, rho, gamma, ...) : 


invisible("Bootstrap weights were tighten than default")
set.seed(2125)
boot_design<-as.svrepdesign (design_weights,
                        type="bootstrap",replicates=3000)
options(survey.lonely.psu="adjust")

# survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel,
#                    #con_quien_vive_joel, 
#                    design=boot_design, rho=0, gamma=0)
 
# #glmsvy3 <- svyglm(HRSOCUP~SEX+NIV_INS+E_CON+POS_OCU+EDA, design=boot_design)
# summary(glmsvy3, df.resid = degf(boot_design))
# confint(glmsvy3, level = 0.95, method = c("Wald"))


# survey::svykm(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel,
#                         #con_quien_vive_joel, 
#                         design=boot_design, rho=0, gamma=0)
invisible("If bootdesign: Error in rowsum(cbind(s, 1) * w, t): dims [producto 2670] no coincide con la longitud del objeto [4005000]")

system.time({
s1<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel_alone,
                        #con_quien_vive_joel, 
                        design=boot_design, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   3.23    0.58    3.83 
Show code
system.time({
s2<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel_coup_chil,
                        #con_quien_vive_joel, 
                        design=boot_design, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   3.03    0.61    3.64 
Show code
system.time({
s3<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel_fam_or,
                        #con_quien_vive_joel, 
                        design=boot_design, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   3.08    0.66    3.73 
Show code
system.time({
s4<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel,
                        #con_quien_vive_joel, 
                        design=boot_design, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   4.94    0.53    5.47 
Show code
cat("Alone (1) vs. With couple/children and Family of origin, BART weights")
Alone (1) vs. With couple/children and Family of origin, BART weights
Show code
s1
[[1]]
        score       se        z          p
[1,] 80.58734 31.99392 2.518833 0.01177446

[[2]]
     Chisq          p 
6.34451856 0.01177446 

attr(,"class")
[1] "svylogrank"
Show code
cat("With couple/children(1) vs. Alone and Family of origin, BART weights")
With couple/children(1) vs. Alone and Family of origin, BART weights
Show code
s2
[[1]]
        score       se        z            p
[1,] 406.6517 62.34043 6.523081 6.887744e-11

[[2]]
       Chisq            p 
4.255059e+01 6.887744e-11 

attr(,"class")
[1] "svylogrank"
Show code
cat("Family of origin(1) vs. Alone and With couple/children, BART weights")
Family of origin(1) vs. Alone and With couple/children, BART weights
Show code
s3
[[1]]
        score       se         z            p
[1,] -487.239 65.74659 -7.410864 1.254796e-13

[[2]]
       Chisq            p 
5.492090e+01 1.254796e-13 

attr(,"class")
[1] "svylogrank"
Show code
cat("Every level")
Every level
Show code
s4
[[1]]
         score       se         z            p
[1,] -487.2390 65.74659 -7.410864 1.254796e-13
[2,]  406.6517 62.34043  6.523081 6.887744e-11

[[2]]
       Chisq            p 
5.492408e+01 1.184100e-12 

attr(,"class")
[1] "svylogrank"

Time for this code chunk to run: 0.1 minutes

Show code
s5<-survey::svykm(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel, se=T,
              #con_quien_vive_joel, 
              design=boot_design, rho=0, gamma=0)
warning("Error: la memoria está agotada (se alcanzó el límite?)")
s5

Time for this code chunk to run: 0 minutes

Show code
plot(survey::svykm(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel,
              #con_quien_vive_joel, 
              design=design_weights, rho=0, gamma=0), lwd=2, pars=list(lty=c(1,2,3),col=c("purple","forestgreen", "red")))
legend(8, 1, legend=c("Alone", "Family of origin", "With couple/children"), 
       fill = c("purple","forestgreen", "red"), lty=c(1,2,3))
Kaplan meier plots (survey weighted BART)

Figure 7: Kaplan meier plots (survey weighted BART)

Show code
#svykm -->
# Error: cannot allocate vector of size 3.1 Gb
# Timing stopped at: 6913 368.1 7453

Time for this code chunk to run: 0 minutes

Now the same but using Stabilized but not truncated? weights.

Show code
#Rader, Kevin Andrew. 2014. Methods for Analyzing Survival and Binary Data in Complex Surveys. Doctoral dissertation, Harvard University.http://nrs.harvard.edu/urn-3:HUL.InstRepos:12274283

#load(paste0(dirname(rstudioapi::getSourceEditorContext()$path),"/__analisis_joel.RData"))
library(survey)
# survey::svylogrank
#First, you can use the survey::svylogrank function, as @IRTFM suggests. This will treat the weights as sampling weights, but I think that's ok with the robust standard errors that svylogrank uses.

design_weights2 <- survey::svydesign(ids = ~1, data = dplyr::mutate(prueba2_imp22, yrs_to_tr_dropout= dplyr::case_when(yrs_to_tr_dropout==0~0.0001,T~yrs_to_tr_dropout)), weights = ~weight_mult_a00)
# Error in .logrank(formula, design, rho, gamma, ...) : 


invisible("Bootstrap weights were tighten than default")
set.seed(2125)
boot_design2<-as.svrepdesign (design_weights2,
                        type="bootstrap",replicates=3000)
options(survey.lonely.psu="adjust")

system.time({
s12<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel_alone,
                        #con_quien_vive_joel, 
                        design=boot_design2, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   3.25    0.47    3.72 
Show code
system.time({
s22<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel_coup_chil,
                        #con_quien_vive_joel, 
                        design=boot_design2, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   2.94    0.51    3.47 
Show code
system.time({
s32<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel_fam_or,
                        #con_quien_vive_joel, 
                        design=boot_design2, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   2.95    0.55    3.50 
Show code
system.time({
s42<- survey::svylogrank(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel,
                        #con_quien_vive_joel, 
                        design=boot_design2, rho=0, gamma=0) #tslow , se=T
#primero es familia de origen vs. el resto
#alone vs. el resto da -41.77638 60.81932 -0.6868932 0.49215
#couple/children vs. el resto da .003 
})
   user  system elapsed 
   4.78    0.59    5.38 
Show code
cat("Alone (1) vs. With couple/children and Family of origin")
Alone (1) vs. With couple/children and Family of origin
Show code
s12
[[1]]
        score       se        z         p
[1,] 80.46478 32.87059 2.447926 0.0143681

[[2]]
    Chisq         p 
5.9923431 0.0143681 

attr(,"class")
[1] "svylogrank"
Show code
cat("With couple/children(1) vs. Alone and Family of origin")
With couple/children(1) vs. Alone and Family of origin
Show code
s22
[[1]]
      score       se        z            p
[1,] 399.06 61.98542 6.437965 1.210862e-10

[[2]]
       Chisq            p 
4.144739e+01 1.210862e-10 

attr(,"class")
[1] "svylogrank"
Show code
cat("Family of origin(1) vs. Alone and With couple/children")
Family of origin(1) vs. Alone and With couple/children
Show code
s32
[[1]]
         score       se         z            p
[1,] -479.5247 65.57665 -7.312432 2.623508e-13

[[2]]
       Chisq            p 
5.347166e+01 2.623508e-13 

attr(,"class")
[1] "svylogrank"
Show code
cat("Every level")
Every level
Show code
s42
[[1]]
         score       se         z            p
[1,] -479.5247 65.57665 -7.312432 2.623508e-13
[2,]  399.0600 61.98542  6.437965 1.210862e-10

[[2]]
       Chisq            p 
5.350166e+01 2.411364e-12 

attr(,"class")
[1] "svylogrank"

Time for this code chunk to run: 0.1 minutes

Show code
plot(survey::svykm(Surv(time=yrs_to_tr_dropout,event=event)~con_quien_vive_joel,
                   #con_quien_vive_joel, 
              design=design_weights2, rho=0, gamma=0), lwd=2, pars=list(lty=c(1,2,3),col=c("purple","forestgreen", "red")))
legend(8, 1, legend=c("Alone", "Family of origin", "With couple/children"), 
       fill = c("purple","forestgreen", "red"), lty=c(1,2,3))
Kaplan meier plots (survey weighted, multinomial logistic)

Figure 8: Kaplan meier plots (survey weighted, multinomial logistic)

Show code
#svykm -->
# Error: cannot allocate vector of size 3.1 Gb
# Timing stopped at: 6913 368.1 7453

Time for this code chunk to run: 0 minutes


Export

Show code
vector_var_names<-c("hash_key", "edad_al_ing", "fech_ing", "fech_egres_imp", "event", "diff_bet_treat", "tipo_de_programa_2", "abandono_temprano", "motivodeegreso_mod_imp")
  
prueba2_imp21%>%
  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")) %>% 
  #dplyr::left_join(prueba2_imp2[,c("hash_key","edad_al_ing","duplicates_filtered_adj","person_years","max_cum_dias_trat_sin_na_adj")], by=c("hash_key"="hash_key", "edad_al_ing"="edad_al_ing")) %>% 
    rio::export(file = paste0(dirname(rstudioapi::getSourceEditorContext()$path),"/CONS_C1_df_dup_SEP_2020_joel_feb_2023.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")))
          }
}  

Time for this code chunk to run: 0 minutes


Flowchart

Show code
tab1_lab<- paste0('Original C1 Dataset \n(n = ', formatC(nrow(CONS_C1), format='f', big.mark=',', digits=0), ';\nusers: ',formatC(CONS_C1%>% dplyr::distinct(HASH_KEY)%>% nrow(), format='f', big.mark=',', digits=0),')')
tab2_lab<- paste0('C1 Dataset \n(n = ', formatC(nrow(CONS_C1_df_dup_SEP_2020), format='f', big.mark=',', digits=0), ';\nusers: ',formatC(CONS_C1_df_dup_SEP_2020%>% dplyr::distinct(hash_key)%>% nrow(), format='f', big.mark=',', digits=0),')')

tab1_5_lab_1<- paste0('(n=', 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) %>% nrow(), '; users= ',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) %>% dplyr::distinct(hash_key)%>% nrow(), ')')
tab1_5_lab_2<- paste0('(n=', prueba%>% dplyr::filter(edad_al_ing_grupos!="18-29"|is.na(edad_al_ing_grupos)) %>% dplyr::distinct(hash_key) %>%  nrow() %>% format(big.mark=","),'; users=', prueba%>% dplyr::filter(edad_al_ing_grupos!="18-29"|is.na(edad_al_ing_grupos)) %>% nrow() %>% format(big.mark=","),')')
  
tab1_5_lab<- paste0('&#8226; Discarded cases with no age at admission or ages below 18 years old ',  tab1_5_lab_1,'\\\\\\l&#8226; Discarded ages different than between 18-29 years old at admission ',tab1_5_lab_2,'\\\\\\l')

tab4_lab<- paste0('C1 Dataset on Young Adults \n(n = ',format(nrow(prueba2), big.mark=","),')')#, formatC(nrow(CONS_C1_df_dup_SEP_2020_match_miss_after_imp_conservados), format='f', big.mark=',', digits=0), ';\nusers: ',formatC(CONS_C1_df_dup_SEP_2020_match_miss_after_imp_conservados%>% dplyr::distinct(hash_key)%>% nrow(), format='f', big.mark=',', digits=0),')')

tab3_5_lab<- paste0('C1 Dataset \n(n = ')#, formatC(nrow(CONS_C1_df_dup_SEP_2020_match_miss_after_imp_descartados), format='f', big.mark=',', digits=0), ';\nusers: ',formatC(CONS_C1_df_dup_SEP_2020_match_miss_after_imp_descartados%>% dplyr::distinct(hash_key)%>% nrow(), format='f', big.mark=',', digits=0),')')

tab5_lab<- paste0('&#8226; Imputed values of users with at least 1 missing value (n= ',prueba2 %>%
  rowwise %>% dplyr::mutate_at(.vars = vars(vector_variables),.funs = ~ifelse(is.na(.), 1, 0)) %>% 
  dplyr::ungroup() %>% dplyr::select(any_of(vector_variables)) %>% dplyr::mutate(total_na=rowSums(., na.rm=T)) %>% dplyr::filter(total_na>0) %>% nrow() %>% format(big.mark=","),'; ', round(prueba2 %>%
  rowwise %>% dplyr::mutate_at(.vars = vars(vector_variables),.funs = ~ifelse(is.na(.), 1, 0)) %>% 
  dplyr::ungroup() %>% dplyr::select(any_of(vector_variables)) %>% dplyr::mutate(total_na=rowSums(., na.rm=T)) %>% dplyr::filter(total_na>0) %>% nrow()/nrow(prueba2),2)*100 ,'%)\\\\\\l&#8226; Substance use onset age and Initial substance were the variables with more missing values and had ', round(parse_number(data.frame(miss_val_bar)[1,3]),0),'% each one \\\\\\l')


lab_tab<- paste0("  Result of the\nimputed dataset\n(n= ",format(nrow(prueba2_imp21),big.mark=","),")")#,table(table(t_id_1)) %>% formatC(big.mark = ","),"; No. of controls: ",table(table(c_id_1))%>% formatC(big.mark = ","))

#https://stackoverflow.com/questions/46750364/diagrammer-and-graphviz
#https://mikeyharper.uk/flowcharts-in-r-using-diagrammer/
#http://blog.nguyenvq.com/blog/2012/05/29/better-decision-tree-graphics-for-rpart-via-party-and-partykit/
#http://blog.nguyenvq.com/blog/2014/01/17/skeleton-to-create-fast-automatic-tree-diagrams-using-r-and-graphviz/
#https://cran.r-project.org/web/packages/DiagrammeR/vignettes/graphviz-mermaid.html
#https://stackoverflow.com/questions/39133058/how-to-use-graphviz-graphs-in-diagrammer-for-r
#https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781789802566/1/ch01lvl1sec21/creating-diagrams-via-the-diagrammer-package
#https://justlegal.be/2019/05/using-flowcharts-to-display-legal-procedures/
# paste0("No. of treatments: ",table(table(t_id_1)) %>% formatC(big.mark = ","),"; No. of controls: ",table(table(c_id_1))%>% formatC(big.mark = ","))
#
library(DiagrammeR) #⋉
flowchrt_p1<-
grViz("digraph flowchart {
      # node definitions with substituted label text
      node [fontname = Times, shape = rectangle,fontsize = 9]        
      tab1 [label = '@@1']
      tab2 [label = '@@2']
      tab3 [label = '&#8226;Duplicated entries\\l&#8226;Intermediate events of treatment (continuous referrals)\\l',fontsize = 7]
      tab4 [label = '@@4']
      blank [label = '', width = 0.0001, height = 0.0001]
      blank2 [label = '', width = 0.0001, height = 0.0001]
      blank3 [label = '', width = 0.0001, height = 0.0001]
      tab5 [label = '@@3',fontsize = 7]
      tab6 [label= '@@7']
      tab7 [label = '@@5',fontsize = 7]
      
      # edge definitions with the node IDs
      tab1 -> blank [arrowhead = none,label='  Data wrangling and normalization process',fontsize = 8];
      blank -> tab3
      blank -> tab2
      tab2 -> blank2 [arrowhead = none];
      blank2 -> tab5 
      blank2 -> tab4 [label='  Result of the exclussion criteria',fontsize = 8];
      tab4 -> blank3 [arrowhead= none];
      blank3-> tab7
      blank3 -> tab6 [label='',fontsize = 8];
            subgraph {
              rank = same; tab3; blank;
            }
            subgraph {
              rank = same; tab5; blank2;
            }
            subgraph {
              rank = same; tab7; blank3;
            }
      }

      [1]:  tab1_lab
      [2]:  tab2_lab
      [3]:  tab1_5_lab
      [4]:  tab4_lab
      [5]:  tab5_lab
      [6]:  ''
      [7]:  lab_tab
      ", width = 1200,
        height = 900)

DPI = 1200
WidthCM = 11
HeightCM = 8

flowchrt_p1
Show code
flowchrt_p1 %>%
  export_svg %>% charToRaw %>% rsvg_pdf("_flowchart_joel.pdf")
flowchrt_p1 %>% export_svg()%>%charToRaw %>% rsvg(width = WidthCM *(DPI/2.54), height = HeightCM *(DPI/2.54)) %>% png::writePNG("_flowchart_joel.png")

htmlwidgets::saveWidget(flowchrt_p1, "_flowchart_joel.html")
webshot::webshot("_flowchart_joel.html", "_flowchart_joel_corr.png",vwidth = 1200, vheight = 900, zoom = 3)

Time for this code chunk to run: 0 minutes


Session Info

April 14, 2023

Show code
Sys.getenv("R_LIBS_USER")
[1] "C:/Users/CISS Fondecyt/OneDrive/Documentos/R/win-library/4.1"
Show code
rstudioapi::getSourceEditorContext()
Document Context: 
- id:        'B23D4C4C'
- path:      'C:/Users/CISS Fondecyt/Mi unidad/Alvacast/SISTRAT 2019 (github)/joel/pres_apr_23_joel.Rmd'
- contents:  <737 rows>
Document Selection:
- [503, 1] -- [503, 1]: ''
Show code
save.image("__analisis_joel.RData")

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

sesion_info <- devtools::session_info()
dplyr::select(
  tibble::as_tibble(sesion_info$packages),
  c(package, loadedversion, source)
) %>% 
  DT::datatable(filter = 'top', colnames = c('Row number' =1,'Variable' = 2, 'Percentage'= 3),
              caption = htmltools::tags$caption(
        style = 'caption-side: top; text-align: left;',
        '', htmltools::em('Paquetes estadísticos utilizados')),
      options=list(
initComplete = htmlwidgets::JS(
      "function(settings, json) {",
      "$(this.api().tables().body()).css({'font-size': '80%'});",
      "}")))

Time for this code chunk to run: 0.1 minutes