## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
## Learn more about sjPlot with 'browseVignettes("sjPlot")'.
## 
## 
## Adjuntando el paquete: 'kableExtra'
## 
## 
## The following object is masked from 'package:dplyr':
## 
##     group_rows
df_train= read_csv("C:/Users/Jao/Documents/Doc/Cursos/Maestria Efectividad Clinica/2/4 Logistica/TH/train_TH.csv")
## Rows: 824 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (36): Died, age, gender, hypertension, a_fib, CHD, diabetes, anemia, dep...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Normalizacion de datos Inversion de los valores 0 y 1 de “hypertension”, “diabetes”, “anemia”, “depression”, “hyperlipemia”, “ckd”, “copd”

library(dplyr)
library(tableone)
library(knitr)
library(kableExtra)

# Convertir nombres a minúscula y reemplazar espacios por "_"
names(df_train) <- tolower(names(df_train))
names(df_train) <- gsub(" ", "_", names(df_train))

# Convertir 'gender' a factor binario
df_train <- df_train %>%
  mutate(gender = as.factor(ifelse(gender == "1", 1, 0)))

# --- Tabla 1 original (antes de invertir) ---
tabla_1_orig <- CreateTableOne(vars = colnames(df_train), strata = "died", data = df_train)
kableone(tabla_1_orig, caption = "Tabla 1 original") %>%
  kable_styling(full_width = FALSE)
Tabla 1 original
0 1 p test
n 716 108
died (mean (SD)) 0.00 (0.00) 1.00 (0.00) <0.001
age (mean (SD)) 73.76 (13.01) 75.98 (14.01) 0.102
gender = 1 (%) 334 (46.6) 56 (51.9) 0.365
hypertension (mean (SD)) 0.73 (0.45) 0.62 (0.49) 0.021
a_fib (mean (SD)) 0.44 (0.50) 0.59 (0.49) 0.002
chd (mean (SD)) 0.09 (0.29) 0.09 (0.29) 0.989
diabetes (mean (SD)) 0.43 (0.50) 0.39 (0.49) 0.389
anemia (mean (SD)) 0.37 (0.48) 0.20 (0.40) 0.001
depression (mean (SD)) 0.13 (0.33) 0.07 (0.26) 0.123
hyperlipemia (mean (SD)) 0.39 (0.49) 0.36 (0.48) 0.589
ckd (mean (SD)) 0.39 (0.49) 0.22 (0.42) 0.001
copd (mean (SD)) 0.09 (0.29) 0.03 (0.17) 0.029
heart_rate (mean (SD)) 83.80 (15.83) 88.24 (14.68) 0.006
systolic_blood_pressure (mean (SD)) 119.21 (17.05) 110.39 (16.75) <0.001
diastolic_blood_pressure (mean (SD)) 60.00 (10.84) 57.67 (9.21) 0.034
respiratory_rate (mean (SD)) 20.51 (3.93) 21.68 (4.27) 0.005
temperature (mean (SD)) 36.70 (0.57) 36.48 (0.73) <0.001
sp_o2 (mean (SD)) 96.35 (2.14) 95.90 (3.01) 0.054
urine_output (mean (SD)) 1999.65 (1271.35) 1251.19 (1042.46) <0.001
hematocrit (mean (SD)) 31.91 (5.11) 31.97 (5.43) 0.913
leucocyte (mean (SD)) 10.37 (4.52) 13.84 (7.96) <0.001
platelets (mean (SD)) 248.72 (108.03) 214.03 (133.44) 0.003
inr (mean (SD)) 1.58 (0.75) 2.02 (1.40) <0.001
nt-probnp (mean (SD)) 10814.14 (12818.97) 15554.25 (14867.09) <0.001
creatinine (mean (SD)) 1.60 (1.21) 1.84 (1.20) 0.046
urea_nitrogen (mean (SD)) 34.83 (20.81) 48.71 (28.71) <0.001
glucose (mean (SD)) 148.52 (50.57) 151.15 (52.98) 0.617
blood_potassium (mean (SD)) 4.15 (0.38) 4.34 (0.58) <0.001
blood_sodium (mean (SD)) 139.02 (3.97) 138.18 (5.35) 0.052
blood_calcium (mean (SD)) 8.54 (0.55) 8.23 (0.63) <0.001
chloride (mean (SD)) 102.12 (5.28) 103.36 (6.04) 0.026
magnesium_ion (mean (SD)) 2.10 (0.24) 2.18 (0.31) 0.004
ph (mean (SD)) 7.38 (0.06) 7.36 (0.08) <0.001
bicarbonate (mean (SD)) 27.41 (5.01) 23.48 (5.32) <0.001
lactic_acid (mean (SD)) 1.71 (0.75) 2.40 (1.36) <0.001
pco2 (mean (SD)) 45.49 (11.54) 43.16 (12.22) 0.053
# --- Invertir variables específicas directamente en df_train ---
cols_to_invert <- c("hypertension", "diabetes", "anemia", "depression", "hyperlipemia", "ckd", "copd")

df_train[cols_to_invert] <- lapply(df_train[cols_to_invert], function(x) {
  x_num <- as.numeric(as.character(x))  # Convertir a numérico (por si son factores)
  x_inv <- ifelse(x_num == 1, 0, 1)
  return(as.factor(x_inv))  # Guardar como factor
})

# Convertir otras variables binarias a factor (por si no estaban)
df_train <- df_train %>%
  mutate(across(c(died, a_fib, chd), as.factor))

# Cambiar nombre de columna si existe
names(df_train)[names(df_train) == "nt-probnp"] <- "nt_probnp"

# --- Tabla 1 modificada ---
tabla_1_mod <- CreateTableOne(vars = colnames(df_train), strata = "died", data = df_train)
kableone(tabla_1_mod, caption = "Tabla 1 modificada (comorbilidades corregidas)") %>%
  kable_styling(full_width = FALSE)
Tabla 1 modificada (comorbilidades corregidas)
0 1 p test
n 716 108
died = 1 (%) 0 ( 0.0) 108 (100.0) <0.001
age (mean (SD)) 73.76 (13.01) 75.98 (14.01) 0.102
gender = 1 (%) 334 (46.6) 56 ( 51.9) 0.365
hypertension = 1 (%) 195 (27.2) 41 ( 38.0) 0.029
a_fib = 1 (%) 313 (43.7) 64 ( 59.3) 0.004
chd = 1 (%) 66 ( 9.2) 10 ( 9.3) 1.000
diabetes = 1 (%) 406 (56.7) 66 ( 61.1) 0.448
anemia = 1 (%) 450 (62.8) 86 ( 79.6) 0.001
depression = 1 (%) 626 (87.4) 100 ( 92.6) 0.166
hyperlipemia = 1 (%) 438 (61.2) 69 ( 63.9) 0.664
ckd = 1 (%) 440 (61.5) 84 ( 77.8) 0.001
copd = 1 (%) 652 (91.1) 105 ( 97.2) 0.046
heart_rate (mean (SD)) 83.80 (15.83) 88.24 (14.68) 0.006
systolic_blood_pressure (mean (SD)) 119.21 (17.05) 110.39 (16.75) <0.001
diastolic_blood_pressure (mean (SD)) 60.00 (10.84) 57.67 (9.21) 0.034
respiratory_rate (mean (SD)) 20.51 (3.93) 21.68 (4.27) 0.005
temperature (mean (SD)) 36.70 (0.57) 36.48 (0.73) <0.001
sp_o2 (mean (SD)) 96.35 (2.14) 95.90 (3.01) 0.054
urine_output (mean (SD)) 1999.65 (1271.35) 1251.19 (1042.46) <0.001
hematocrit (mean (SD)) 31.91 (5.11) 31.97 (5.43) 0.913
leucocyte (mean (SD)) 10.37 (4.52) 13.84 (7.96) <0.001
platelets (mean (SD)) 248.72 (108.03) 214.03 (133.44) 0.003
inr (mean (SD)) 1.58 (0.75) 2.02 (1.40) <0.001
nt_probnp (mean (SD)) 10814.14 (12818.97) 15554.25 (14867.09) <0.001
creatinine (mean (SD)) 1.60 (1.21) 1.84 (1.20) 0.046
urea_nitrogen (mean (SD)) 34.83 (20.81) 48.71 (28.71) <0.001
glucose (mean (SD)) 148.52 (50.57) 151.15 (52.98) 0.617
blood_potassium (mean (SD)) 4.15 (0.38) 4.34 (0.58) <0.001
blood_sodium (mean (SD)) 139.02 (3.97) 138.18 (5.35) 0.052
blood_calcium (mean (SD)) 8.54 (0.55) 8.23 (0.63) <0.001
chloride (mean (SD)) 102.12 (5.28) 103.36 (6.04) 0.026
magnesium_ion (mean (SD)) 2.10 (0.24) 2.18 (0.31) 0.004
ph (mean (SD)) 7.38 (0.06) 7.36 (0.08) <0.001
bicarbonate (mean (SD)) 27.41 (5.01) 23.48 (5.32) <0.001
lactic_acid (mean (SD)) 1.71 (0.75) 2.40 (1.36) <0.001
pco2 (mean (SD)) 45.49 (11.54) 43.16 (12.22) 0.053

Evaluar NA

# Total de NA en todo el dataframe
total_na <- sum(is.na(df_train))
cat("Total de valores NA en el dataframe:", total_na, "\n\n")
## Total de valores NA en el dataframe: 0
# NA por variable
na_por_variable <- colSums(is.na(df_train))
print(na_por_variable)
##                     died                      age                   gender 
##                        0                        0                        0 
##             hypertension                    a_fib                      chd 
##                        0                        0                        0 
##                 diabetes                   anemia               depression 
##                        0                        0                        0 
##             hyperlipemia                      ckd                     copd 
##                        0                        0                        0 
##               heart_rate  systolic_blood_pressure diastolic_blood_pressure 
##                        0                        0                        0 
##         respiratory_rate              temperature                    sp_o2 
##                        0                        0                        0 
##             urine_output               hematocrit                leucocyte 
##                        0                        0                        0 
##                platelets                      inr                nt_probnp 
##                        0                        0                        0 
##               creatinine            urea_nitrogen                  glucose 
##                        0                        0                        0 
##          blood_potassium             blood_sodium            blood_calcium 
##                        0                        0                        0 
##                 chloride            magnesium_ion                       ph 
##                        0                        0                        0 
##              bicarbonate              lactic_acid                     pco2 
##                        0                        0                        0

Tabla 1

#ordenar por signficancia p

tabla_1p <- print(tabla_1_mod, printToggle = FALSE)

# Convertir matriz a data.frame
tabla_1p_df <- as.data.frame(tabla_1p)

# Ordenar por p-valor
tabla_1p_ordenada <- tabla_1p_df %>%
  mutate(
    p_num = gsub("<0\\.001", "0.0009", p),      # reemplaza "<0.001" por un valor numérico muy bajo
    p_num = suppressWarnings(as.numeric(p_num))
  ) %>%
  arrange(p_num)

# Mostrar
knitr::kable(tabla_1p_ordenada, caption = "Tabla 1: Ordenada por valor p")
Tabla 1: Ordenada por valor p
0 1 p test p_num
died = 1 (%) 0 ( 0.0) 108 (100.0) <0.001 0.0009
systolic_blood_pressure (mean (SD)) 119.21 (17.05) 110.39 (16.75) <0.001 0.0009
temperature (mean (SD)) 36.70 (0.57) 36.48 (0.73) <0.001 0.0009
urine_output (mean (SD)) 1999.65 (1271.35) 1251.19 (1042.46) <0.001 0.0009
leucocyte (mean (SD)) 10.37 (4.52) 13.84 (7.96) <0.001 0.0009
inr (mean (SD)) 1.58 (0.75) 2.02 (1.40) <0.001 0.0009
nt_probnp (mean (SD)) 10814.14 (12818.97) 15554.25 (14867.09) <0.001 0.0009
urea_nitrogen (mean (SD)) 34.83 (20.81) 48.71 (28.71) <0.001 0.0009
blood_potassium (mean (SD)) 4.15 (0.38) 4.34 (0.58) <0.001 0.0009
blood_calcium (mean (SD)) 8.54 (0.55) 8.23 (0.63) <0.001 0.0009
ph (mean (SD)) 7.38 (0.06) 7.36 (0.08) <0.001 0.0009
bicarbonate (mean (SD)) 27.41 (5.01) 23.48 (5.32) <0.001 0.0009
lactic_acid (mean (SD)) 1.71 (0.75) 2.40 (1.36) <0.001 0.0009
anemia = 1 (%) 450 (62.8) 86 ( 79.6) 0.001 0.0010
ckd = 1 (%) 440 (61.5) 84 ( 77.8) 0.001 0.0010
platelets (mean (SD)) 248.72 (108.03) 214.03 (133.44) 0.003 0.0030
a_fib = 1 (%) 313 (43.7) 64 ( 59.3) 0.004 0.0040
magnesium_ion (mean (SD)) 2.10 (0.24) 2.18 (0.31) 0.004 0.0040
respiratory_rate (mean (SD)) 20.51 (3.93) 21.68 (4.27) 0.005 0.0050
heart_rate (mean (SD)) 83.80 (15.83) 88.24 (14.68) 0.006 0.0060
chloride (mean (SD)) 102.12 (5.28) 103.36 (6.04) 0.026 0.0260
hypertension = 1 (%) 195 (27.2) 41 ( 38.0) 0.029 0.0290
diastolic_blood_pressure (mean (SD)) 60.00 (10.84) 57.67 (9.21) 0.034 0.0340
copd = 1 (%) 652 (91.1) 105 ( 97.2) 0.046 0.0460
creatinine (mean (SD)) 1.60 (1.21) 1.84 (1.20) 0.046 0.0460
blood_sodium (mean (SD)) 139.02 (3.97) 138.18 (5.35) 0.052 0.0520
pco2 (mean (SD)) 45.49 (11.54) 43.16 (12.22) 0.053 0.0530
sp_o2 (mean (SD)) 96.35 (2.14) 95.90 (3.01) 0.054 0.0540
age (mean (SD)) 73.76 (13.01) 75.98 (14.01) 0.102 0.1020
depression = 1 (%) 626 (87.4) 100 ( 92.6) 0.166 0.1660
gender = 1 (%) 334 (46.6) 56 ( 51.9) 0.365 0.3650
diabetes = 1 (%) 406 (56.7) 66 ( 61.1) 0.448 0.4480
glucose (mean (SD)) 148.52 (50.57) 151.15 (52.98) 0.617 0.6170
hyperlipemia = 1 (%) 438 (61.2) 69 ( 63.9) 0.664 0.6640
hematocrit (mean (SD)) 31.91 (5.11) 31.97 (5.43) 0.913 0.9130
chd = 1 (%) 66 ( 9.2) 10 ( 9.3) 1.000 1.0000
n 716 108 NA

Analisis bivariado

library(gtsummary)
library(dplyr)

# Crear tabla univariada con OR y p ordenados
tabla_bi <- df_train %>%
  tbl_uvregression(
    method = glm,
    y = died,
    method.args = list(family = binomial),
    exponentiate = TRUE
  ) %>%
  bold_p() %>%
  bold_labels()

knitr::kable(tabla_bi)
Characteristic N OR 95% CI p-value
age 824 1.01 1.00, 1.03 0.10
gender 824 NA NA NA
0 NA NA NA NA
1 NA 1.23 0.82, 1.85 0.3
hypertension 824 NA NA NA
0 NA NA NA NA
1 NA 1.63 1.07, 2.48 0.022
a_fib 824 NA NA NA
0 NA NA NA NA
1 NA 1.87 1.25, 2.84 0.003
chd 824 NA NA NA
0 NA NA NA NA
1 NA 1.00 0.47, 1.94 >0.9
diabetes 824 NA NA NA
0 NA NA NA NA
1 NA 1.20 0.80, 1.83 0.4
anemia 824 NA NA NA
0 NA NA NA NA
1 NA 2.31 1.44, 3.86 <0.001
depression 824 NA NA NA
0 NA NA NA NA
1 NA 1.80 0.90, 4.13 0.13
hyperlipemia 824 NA NA NA
0 NA NA NA NA
1 NA 1.12 0.74, 1.72 0.6
ckd 824 NA NA NA
0 NA NA NA NA
1 NA 2.20 1.38, 3.61 0.001
copd 824 NA NA NA
0 NA NA NA NA
1 NA 3.44 1.25, 14.2 0.040
heart_rate 824 1.02 1.00, 1.03 0.007
systolic_blood_pressure 824 0.97 0.95, 0.98 <0.001
diastolic_blood_pressure 824 0.98 0.96, 1.00 0.034
respiratory_rate 824 1.07 1.02, 1.13 0.005
temperature 824 0.55 0.39, 0.77 <0.001
sp_o2 824 0.92 0.85, 1.00 0.057
urine_output 824 1.00 1.00, 1.00 <0.001
hematocrit 824 1.00 0.96, 1.04 >0.9
leucocyte 824 1.11 1.07, 1.15 <0.001
platelets 824 1.00 0.99, 1.00 0.003
inr 824 1.54 1.28, 1.86 <0.001
nt_probnp 824 1.00 1.00, 1.00 <0.001
creatinine 824 1.15 0.99, 1.32 0.051
urea_nitrogen 824 1.02 1.01, 1.03 <0.001
glucose 824 1.00 1.00, 1.00 0.6
blood_potassium 824 2.63 1.70, 4.09 <0.001
blood_sodium 824 0.96 0.91, 1.00 0.053
blood_calcium 824 0.36 0.25, 0.53 <0.001
chloride 824 1.04 1.01, 1.09 0.026
magnesium_ion 824 2.90 1.37, 6.11 0.005
ph 824 0.00 0.00, 0.03 <0.001
bicarbonate 824 0.84 0.80, 0.88 <0.001
lactic_acid 824 1.97 1.62, 2.42 <0.001
pco2 824 0.98 0.96, 1.00 0.053
# Convertir a tibble y ordenar por p-valor REAL
tabla_bi_df <- as_tibble(tabla_bi, col_labels = TRUE) 

#exportar archivo a excel

#library(openxlsx) 
#write.xlsx(tabla_bi_df,file = "p_values_invert.xlsx", sheetName = "Hoja1")

#library(openxlsx) #write.xlsx(tabla_bi_df,file = “p_values.xlsx”, sheetName = “Hoja1”)

Analizar correlacion

col_num = sapply(df_train, is.numeric)
df_num = df_train[,col_num]
matriz_corr = cor(df_num, method = "spearman")

library(formattable)
#3. Convertir la matriz de correlación a un data.frame para usar formattable
#    También redondeamos para una mejor lectura.
matriz_corr_df <- as.data.frame(round(matriz_corr, 3))
knitr::kable(matriz_corr_df)
age heart_rate systolic_blood_pressure diastolic_blood_pressure respiratory_rate temperature sp_o2 urine_output hematocrit leucocyte platelets inr nt_probnp creatinine urea_nitrogen glucose blood_potassium blood_sodium blood_calcium chloride magnesium_ion ph bicarbonate lactic_acid pco2
age 1.000 -0.186 -0.056 -0.319 0.041 -0.174 0.054 -0.179 0.058 0.040 -0.016 0.070 0.120 0.019 0.107 -0.099 -0.071 0.105 -0.026 0.149 0.075 0.093 -0.071 0.080 -0.132
heart_rate -0.186 1.000 -0.155 0.337 0.322 0.140 -0.110 -0.023 -0.006 0.145 0.092 0.039 -0.013 -0.228 -0.187 0.053 -0.037 -0.072 -0.092 0.031 -0.077 0.008 -0.078 0.163 -0.113
systolic_blood_pressure -0.056 -0.155 1.000 0.333 -0.074 0.095 -0.050 0.249 0.018 -0.085 0.083 -0.273 -0.151 0.005 -0.030 0.087 -0.026 0.116 0.132 -0.012 -0.042 0.074 0.128 -0.168 0.113
diastolic_blood_pressure -0.319 0.337 0.333 1.000 0.128 0.011 -0.106 0.186 0.281 -0.053 0.075 -0.043 -0.050 -0.094 -0.121 0.097 -0.008 0.019 0.130 -0.065 -0.024 0.088 0.066 0.077 -0.021
respiratory_rate 0.041 0.322 -0.074 0.128 1.000 0.093 -0.240 0.041 0.110 0.150 0.110 0.049 0.039 -0.088 -0.021 0.087 -0.031 -0.024 0.028 -0.068 0.002 0.057 0.000 0.133 -0.049
temperature -0.174 0.140 0.095 0.011 0.093 1.000 0.030 0.171 -0.095 0.018 -0.003 -0.072 -0.188 -0.133 -0.202 -0.057 -0.182 0.075 -0.116 0.106 -0.099 0.129 0.021 -0.066 -0.014
sp_o2 0.054 -0.110 -0.050 -0.106 -0.240 0.030 1.000 -0.080 -0.243 -0.013 -0.086 0.060 0.181 0.076 0.027 -0.057 -0.068 -0.022 -0.146 0.162 -0.046 0.017 -0.187 0.065 -0.167
urine_output -0.179 -0.023 0.249 0.186 0.041 0.171 -0.080 1.000 0.120 -0.144 0.039 -0.095 -0.268 -0.149 -0.194 0.027 -0.113 0.012 0.173 -0.124 -0.058 0.169 0.237 -0.134 0.097
hematocrit 0.058 -0.006 0.018 0.281 0.110 -0.095 -0.243 0.120 1.000 0.070 -0.047 -0.024 -0.189 -0.181 -0.138 0.038 0.022 0.089 0.283 -0.125 0.098 0.013 0.195 0.127 0.099
leucocyte 0.040 0.145 -0.085 -0.053 0.150 0.018 -0.013 -0.144 0.070 1.000 0.269 0.084 0.095 -0.008 0.079 0.119 0.001 -0.052 -0.086 0.020 0.088 -0.037 -0.151 0.261 -0.116
platelets -0.016 0.092 0.083 0.075 0.110 -0.003 -0.086 0.039 -0.047 0.269 1.000 -0.013 -0.016 -0.182 -0.176 0.065 -0.001 -0.064 0.112 -0.140 -0.015 0.114 0.132 -0.045 0.022
inr 0.070 0.039 -0.273 -0.043 0.049 -0.072 0.060 -0.095 -0.024 0.084 -0.013 1.000 0.190 0.086 0.071 -0.085 -0.009 -0.064 -0.080 0.010 0.051 0.100 -0.098 0.203 -0.167
nt_probnp 0.120 -0.013 -0.151 -0.050 0.039 -0.188 0.181 -0.268 -0.189 0.095 -0.016 0.190 1.000 0.442 0.440 -0.024 0.096 -0.040 -0.107 0.050 0.093 -0.065 -0.288 0.107 -0.210
creatinine 0.019 -0.228 0.005 -0.094 -0.088 -0.133 0.076 -0.149 -0.181 -0.008 -0.182 0.086 0.442 1.000 0.781 0.064 0.322 -0.048 0.040 -0.003 0.255 -0.230 -0.320 0.003 -0.099
urea_nitrogen 0.107 -0.187 -0.030 -0.121 -0.021 -0.202 0.027 -0.194 -0.138 0.079 -0.176 0.071 0.440 0.781 1.000 0.150 0.370 0.032 0.078 0.017 0.382 -0.207 -0.238 0.015 -0.055
glucose -0.099 0.053 0.087 0.097 0.087 -0.057 -0.057 0.027 0.038 0.119 0.065 -0.085 -0.024 0.064 0.150 1.000 0.110 -0.005 0.085 -0.037 0.106 -0.025 0.000 0.193 -0.014
blood_potassium -0.071 -0.037 -0.026 -0.008 -0.031 -0.182 -0.068 -0.113 0.022 0.001 -0.001 -0.009 0.096 0.322 0.370 0.110 1.000 -0.216 0.110 -0.119 0.187 -0.393 -0.109 0.020 0.098
blood_sodium 0.105 -0.072 0.116 0.019 -0.024 0.075 -0.022 0.012 0.089 -0.052 -0.064 -0.064 -0.040 -0.048 0.032 -0.005 -0.216 1.000 0.070 0.566 0.095 -0.044 0.191 -0.151 0.202
blood_calcium -0.026 -0.092 0.132 0.130 0.028 -0.116 -0.146 0.173 0.283 -0.086 0.112 -0.080 -0.107 0.040 0.078 0.085 0.110 0.070 1.000 -0.254 0.156 0.067 0.288 -0.071 0.179
chloride 0.149 0.031 -0.012 -0.065 -0.068 0.106 0.162 -0.124 -0.125 0.020 -0.140 0.010 0.050 -0.003 0.017 -0.037 -0.119 0.566 -0.254 1.000 -0.031 -0.096 -0.533 0.037 -0.359
magnesium_ion 0.075 -0.077 -0.042 -0.024 0.002 -0.099 -0.046 -0.058 0.098 0.088 -0.015 0.051 0.093 0.255 0.382 0.106 0.187 0.095 0.156 -0.031 1.000 -0.026 0.008 0.030 0.028
ph 0.093 0.008 0.074 0.088 0.057 0.129 0.017 0.169 0.013 -0.037 0.114 0.100 -0.065 -0.230 -0.207 -0.025 -0.393 -0.044 0.067 -0.096 -0.026 1.000 0.131 0.064 -0.409
bicarbonate -0.071 -0.078 0.128 0.066 0.000 0.021 -0.187 0.237 0.195 -0.151 0.132 -0.098 -0.288 -0.320 -0.238 0.000 -0.109 0.191 0.288 -0.533 0.008 0.131 1.000 -0.281 0.704
lactic_acid 0.080 0.163 -0.168 0.077 0.133 -0.066 0.065 -0.134 0.127 0.261 -0.045 0.203 0.107 0.003 0.015 0.193 0.020 -0.151 -0.071 0.037 0.030 0.064 -0.281 1.000 -0.332
pco2 -0.132 -0.113 0.113 -0.021 -0.049 -0.014 -0.167 0.097 0.099 -0.116 0.022 -0.167 -0.210 -0.099 -0.055 -0.014 0.098 0.202 0.179 -0.359 0.028 -0.409 0.704 -0.332 1.000
#ver correlacion en forma grafica entre ac lactico y bicarbonato
library(ggplot2)

ggplot(df_train, aes(x = bicarbonate, y = lactic_acid)) +
  geom_point(alpha = 0.6) +  # puntos de dispersión
  geom_smooth(method = "lm", se = TRUE, color = "blue") +  # línea de regresión lineal
  theme_minimal() +
  labs(
    title = "Correlación entre ac lactico y bicarbonato",
    x = "Ac Lactico",
    y = "bicarboanto"
  )
## `geom_smooth()` using formula = 'y ~ x'

cor(df_train$bicarbonate, df_train$lactic_acid, method = "spearman", use = "complete.obs")
## [1] -0.2812888

Evaluar linealidad entre variables continuas y logit died

df_train <- df_train %>%
  mutate(died_num = as.numeric(as.character(died))) 

#Este gráfico muestra la proporción de muertes (died == 1) en cada uno de los 5 quintiles de la variable continua bicarbonate
#Bicarbonato
df_cont <- df_train %>%
  mutate(bicarbonate_q = ntile(bicarbonate, 5)) #Divide la variable continua bicarbonate en 5 grupos (quintiles) según su distribución. Crea una nueva variable categórica bicarbonate_q.

df_cont %>%
  group_by(bicarbonate_q) %>%
  summarise(tar = mean(as.numeric(died) == 1)) %>% #Divide la variable continua bicarbonate en 5 grupos (quintiles) según su distribución. Crea una nueva variable categórica bicarbonate_q.
  ggplot(aes(x = bicarbonate_q, y = tar)) +
  geom_point() +
  geom_smooth(method = "lm", color = "blue") #Agrega una línea de tendencia lineal (no el logit) entre quintiles y tasa de muerte.
## `geom_smooth()` using formula = 'y ~ x'

# Graficar bicarbonato con logit de died
# Modelo logístico: logit(died) ~ bicarbonate
modelo_logit <- glm(died ~ bicarbonate, data = df_train, family = "binomial")

# Agregar columna con valores de logit estimado (type = "link")
df_train <- df_train %>%
  mutate(logit = predict(modelo_logit, type = "link"))

# Graficar logit vs. bicarbonato
library(ggplot2)

ggplot(df_train, aes(x = bicarbonate, y = logit)) +
  geom_point(alpha = 0.3, color = "darkgray") +
  geom_smooth(method = "loess", color = "blue", se = TRUE) +
  labs(
    title = "Relación entre Bicarbonato y Logit de Muerte",
    x = "Bicarbonato",
    y = "Logit (log-odds) estimado de muerte"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

# Grafico de logit de died con bicarbonate divido en 5 categorias

library(dplyr)
library(ggplot2)

# 1. Crear variable de quintiles
df_train <- df_train %>%
  mutate(bicarbonate_q = ntile(bicarbonate, 5))

# 2. Ajustar modelo con quintiles como variable categórica
modelo_quintiles <- glm(died ~ factor(bicarbonate_q), data = df_train, family = "binomial")

# 3. Obtener logits estimados
df_train <- df_train %>%
  mutate(logit = predict(modelo_quintiles, type = "link"))

# 4. Graficar logit estimado por quintil
ggplot(df_train, aes(x = factor(bicarbonate_q), y = logit)) +
  geom_boxplot(fill = "skyblue", color = "black") +
  stat_summary(fun = mean, geom = "point", color = "red", size = 2) +
  labs(
    title = "Logit (log-odds) estimado por quintil de Bicarbonato",
    x = "Quintil de Bicarbonato",
    y = "Logit (log-odds) estimado de muerte"
  ) +
  theme_minimal()

#Urine output
df_cont <- df_train %>%
  mutate(urine_output_q = ntile(urine_output, 5))

df_cont %>%
  group_by(urine_output_q) %>%
  summarise(tar = mean(as.numeric(died) == 1)) %>%
  ggplot(aes(x = urine_output_q, y = tar)) +
  geom_point() +
  geom_smooth(method = "lm", color = "blue")
## `geom_smooth()` using formula = 'y ~ x'

#leucocyte
df_cont <- df_train %>%
  mutate(leucocyte_q = ntile(leucocyte, 5))

df_cont %>%
  group_by(leucocyte_q) %>%
  summarise(tar = mean(as.numeric(died) == 1)) %>%
  ggplot(aes(x = leucocyte_q, y = tar)) +
  geom_point() +
  geom_smooth(method = "lm", color = "blue")
## `geom_smooth()` using formula = 'y ~ x'

#Funcion para probar linealidad entre ouctome y multiples variables

library(dplyr)
library(ggplot2)

evaluar_linealidad_logit <- function(df, outcome, variables, n_grupos = 5) {
  # Selecciona variables numéricas de la lista
  vars_num <- variables[sapply(df[variables], is.numeric)]
  
  for (var in vars_num) {
    # Crear grupos por ntile
    df_cont <- df %>%
      mutate(
        !!paste0(var, "_q") := ntile(.data[[var]], n_grupos)
      )
    
    # Calcular proporción de outcome == 1 en cada grupo
    resumen <- df_cont %>%
      group_by(.data[[paste0(var, "_q")]]) %>%
      summarise(
        proporcion = mean(as.numeric(as.character(.data[[outcome]])) == 1, na.rm = TRUE),
        valor_medio = mean(.data[[var]], na.rm = TRUE),
        .groups = "drop"
      )
    
    # Gráfico
    p <- ggplot(resumen, aes(x = valor_medio, y = proporcion)) +
      geom_point() +
      geom_smooth(method = "lm", color = "blue") +
      labs(
        title = paste("Evaluación de linealidad para", var),
        x = paste("Valor medio de", var, "(por grupo)"),
        y = paste("Proporción de", outcome, "=1")
      ) +
      theme_minimal()
    
    print(p)
  }
}

#Extraer todas las variables numericas del dataframe para luego probar linealidad

obtener_variables_numericas <- function(df) {
  nombres_num <- names(df_train)[sapply(df, is.numeric)]
  return(nombres_num)
}

var_numericas = obtener_variables_numericas((df_train))

#Evaluar linealidad

evaluar_linealidad_logit(df_train, "died", var_numericas)
## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

## `geom_smooth()` using formula = 'y ~ x'

#Evaluar linealidad entre log odds y variables numericas

library(ggplot2)
library(dplyr)

# 1. Crear variable de quintiles
df_train <- df_train %>%
  mutate(pco2_q = ntile(pco2, 5))

# 2. Ajustar modelo con quintiles como variable categórica
modelo_quintiles_pco2 <- glm(died ~ factor(pco2_q), data = df_train, family = "binomial")

# 3. Obtener logits estimados
df_train <- df_train %>%
  mutate(logit = predict(modelo_quintiles, type = "link"))

# 4. Graficar logit estimado por quintil
ggplot(df_train, aes(x = factor(pco2_q), y = logit)) +
  geom_boxplot(fill = "skyblue", color = "black") +
  stat_summary(fun = mean, geom = "point", color = "red", size = 2) +
  labs(
    title = "Logit (log-odds) estimado por quintil de pco2",
    x = "Quintil de pco2",
    y = "Logit (log-odds) estimado de muerte"
  ) +
  theme_minimal()

#Evaluar linealidad entre logodds de died y edad

library(dplyr)
library(ggplot2)

# Crear deciles de edad
df_train <- df_train %>%
  mutate(age_group = cut(age, breaks = quantile(age, probs = seq(0, 1, 0.1), na.rm = TRUE),
                         include.lowest = TRUE))
df_train$died <- as.numeric(as.character(df_train$died))

# Calcular proporción de muertos y log-odds por grupo
df_logodds <- df_train %>%
  group_by(age_group) %>%
  summarise(
    mean_age = mean(age),
    prop = mean(died),
    .groups = "drop"
  ) %>%
  filter(prop > 0 & prop < 1) %>%  # evitar log(0) y log(1)
  mutate(logodds = log(prop / (1 - prop)))

# Graficar
ggplot(df_logodds, aes(x = mean_age, y = logodds)) +
  geom_point() +
  geom_smooth(method = "lm", se = TRUE, color = "blue") +
  labs(
    title = "Log-odds de muerte por edad (agrupada)",
    x = "Edad media por grupo",
    y = "Log-odds"
  ) +
  theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'

#Evaluar termino cuadratico

mod_lineal <- glm(died ~ hematocrit, family = binomial(), data = df_train)
mod_cuadratico <- glm(died ~ hematocrit + I(hematocrit^2), family = binomial(), data = df_train)

anova(mod_lineal, mod_cuadratico, test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ hematocrit
## Model 2: died ~ hematocrit + I(hematocrit^2)
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       822     640.09                     
## 2       821     639.49  1  0.59703   0.4397

#Evaluar interaccion

mod_int_1 = glm(died~bicarbonate*copd, family = binomial(), data = df_train)
tab_model(mod_int_1)
  died
Predictors Odds Ratios CI p
(Intercept) 15.81 0.02 – 22864.51 0.412
bicarbonate 0.81 0.60 – 1.02 0.104
copd [1] 0.72 0.00 – 616.33 0.924
bicarbonate × copd [1] 1.04 0.82 – 1.41 0.742
Observations 824
R2 Tjur 0.089
mod_int_2 = glm(died~lactic_acid*copd, family = binomial(), data = df_train)
tab_model(mod_int_2)
  died
Predictors Odds Ratios CI p
(Intercept) 0.00 0.00 – 0.05 <0.001
lactic acid 3.02 1.08 – 9.62 0.034
copd [1] 10.36 0.76 – 492.62 0.135
lactic acid × copd [1] 0.64 0.20 – 1.84 0.406
Observations 824
R2 Tjur 0.081
mod_int_3 = glm(died~ph*copd, family = binomial(), data = df_train)
tab_model(mod_int_3)
  died
Predictors Odds Ratios CI p
(Intercept) 2048809606409573632.00 0.00 – 294518962312628190266240246408002248626204882080208686020200044264460828684404.00 0.537
ph 0.00 0.00 – 383667.38 0.508
copd [1] 154438.04 0.00 – 7210074106610681251040082242048246046000620440628402044626404884604.00 0.863
ph × copd [1] 0.24 0.00 – 40552765.23 0.880
Observations 824
R2 Tjur 0.038
mod_int_4 = glm(died~nt_probnp*chd, family = binomial(), data = df_train)
tab_model(mod_int_4)
  died
Predictors Odds Ratios CI p
(Intercept) 0.11 0.08 – 0.15 <0.001
nt probnp 1.00 1.00 – 1.00 0.002
chd [1] 0.89 0.31 – 2.21 0.821
nt probnp × chd [1] 1.00 1.00 – 1.00 0.820
Observations 824
R2 Tjur 0.014
#Ninguna interaccion significativa

Calculo de max cant de variables por cant de eventos

# outcome: vector binario (0/1)
# evento = 1

# Contar cantidad de eventos
eventos <- sum(df_train$died == 1)

# Regla de 10 eventos por variable
max_variables <- floor(eventos / 10)

cat("Cantidad máxima de variables recomendada:", max_variables, "\n")
## Cantidad máxima de variables recomendada: 10

Otra forma a traves de un paquete

#Armado de modelo

modf_0 = glm(died~bicarbonate, family = binomial(), data = df_train)
tab_model(modf_0,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.84
(0.80 – 0.88)
0.02 <0.001
Observations 824
Deviance 580.368
log-Likelihood -290.184
modf_1 = glm(died~bicarbonate+lactic_acid, family = binomial(), data = df_train)
tab_model(modf_1,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.86
(0.82 – 0.91)
0.02 <0.001
lactic_acid 1.65
(1.33 – 2.05)
0.18 <0.001
Observations 824
Deviance 558.757
log-Likelihood -279.378
anova(modf_0,modf_1,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate
## Model 2: died ~ bicarbonate + lactic_acid
##   Resid. Df Resid. Dev Df Deviance  Pr(>Chi)    
## 1       822     580.37                          
## 2       821     558.76  1   21.611 3.339e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_2 = glm(died~bicarbonate+lactic_acid+urine_output, family = binomial(), data = df_train)
tab_model(modf_2,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.88
(0.84 – 0.93)
0.02 <0.001
lactic_acid 1.59
(1.27 – 1.99)
0.18 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 <0.001
Observations 824
Deviance 540.339
log-Likelihood -270.169
anova(modf_1,modf_2,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid
## Model 2: died ~ bicarbonate + lactic_acid + urine_output
##   Resid. Df Resid. Dev Df Deviance  Pr(>Chi)    
## 1       821     558.76                          
## 2       820     540.34  1   18.418 1.774e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_3 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte, family = binomial(), data = df_train)
tab_model(modf_3,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.89
(0.84 – 0.94)
0.02 <0.001
lactic_acid 1.49
(1.19 – 1.88)
0.17 0.001
leucocyte 1.07
(1.03 – 1.11)
0.02 0.001
urine_output 1.00
(1.00 – 1.00)
0.00 <0.001
Observations 824
Deviance 529.233
log-Likelihood -264.616
anova(modf_2,modf_3,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte
##   Resid. Df Resid. Dev Df Deviance  Pr(>Chi)    
## 1       820     540.34                          
## 2       819     529.23  1   11.106 0.0008605 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_4 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen, family = binomial(), data = df_train)
tab_model(modf_4,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.91
(0.86 – 0.96)
0.02 0.001
lactic_acid 1.56
(1.24 – 1.98)
0.19 <0.001
leucocyte 1.06
(1.02 – 1.11)
0.02 0.002
urea_nitrogen 1.01
(1.01 – 1.02)
0.00 0.002
urine_output 1.00
(1.00 – 1.00)
0.00 0.002
Observations 824
Deviance 519.530
log-Likelihood -259.765
anova(modf_3,modf_4,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)   
## 1       819     529.23                        
## 2       818     519.53  1    9.703  0.00184 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_5 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium, family = binomial(), data = df_train)
tab_model(modf_5,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.93
(0.88 – 0.99)
0.03 0.017
blood_calcium 0.51
(0.33 – 0.77)
0.11 0.002
lactic_acid 1.60
(1.26 – 2.04)
0.20 <0.001
leucocyte 1.06
(1.02 – 1.10)
0.02 0.004
urea_nitrogen 1.02
(1.01 – 1.03)
0.00 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.006
Observations 824
Deviance 509.353
log-Likelihood -254.677
anova(modf_4,modf_5,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)   
## 1       818     519.53                        
## 2       817     509.35  1   10.177 0.001422 **
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_6 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure, family = binomial(), data = df_train)
tab_model(modf_6,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.94
(0.89 – 0.99)
0.03 0.021
blood_calcium 0.53
(0.34 – 0.80)
0.11 0.003
lactic_acid 1.54
(1.21 – 1.97)
0.19 <0.001
leucocyte 1.06
(1.02 – 1.10)
0.02 0.005
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.016
urea_nitrogen 1.02
(1.01 – 1.03)
0.00 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.030
Observations 824
Deviance 503.108
log-Likelihood -251.554
anova(modf_5,modf_6,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)  
## 1       817     509.35                       
## 2       816     503.11  1    6.245  0.01245 *
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_7 = glm(died~bicarbonate+lactic_acid++urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+blood_potassium, family = binomial(), data = df_train)
tab_model(modf_7,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
bicarbonate 0.94
(0.89 – 0.99)
0.03 0.027
blood_calcium 0.52
(0.33 – 0.79)
0.11 0.003
blood_potassium 1.40
(0.80 – 2.44)
0.40 0.239
lactic_acid 1.51
(1.18 – 1.94)
0.19 0.001
leucocyte 1.06
(1.02 – 1.10)
0.02 0.005
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.017
urea_nitrogen 1.02
(1.01 – 1.03)
0.01 0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.036
Observations 824
Deviance 501.722
log-Likelihood -250.861
anova(modf_6,modf_7,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure
## Model 2: died ~ bicarbonate + lactic_acid + +urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     blood_potassium
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       816     503.11                     
## 2       815     501.72  1   1.3864    0.239
#Elimina potasio (t7)


modf_8 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia, family = binomial(), data = df_train)
tab_model(modf_8,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.66
(1.56 – 4.73)
0.75 0.001
bicarbonate 0.94
(0.89 – 0.99)
0.03 0.021
blood_calcium 0.49
(0.31 – 0.75)
0.11 0.001
lactic_acid 1.48
(1.16 – 1.89)
0.18 0.002
leucocyte 1.06
(1.02 – 1.10)
0.02 0.007
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.015
urea_nitrogen 1.02
(1.01 – 1.03)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.024
Observations 824
Deviance 489.726
log-Likelihood -244.863
anova(modf_6,modf_8,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)    
## 1       816     503.11                         
## 2       815     489.73  1   13.382 0.000254 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_9 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+temperature, family = binomial(), data = df_train)
tab_model(modf_9,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.64
(1.55 – 4.71)
0.75 0.001
bicarbonate 0.94
(0.88 – 0.99)
0.03 0.018
blood_calcium 0.48
(0.31 – 0.74)
0.11 0.001
lactic_acid 1.45
(1.14 – 1.86)
0.18 0.003
leucocyte 1.06
(1.02 – 1.10)
0.02 0.007
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.019
temperature 0.77
(0.53 – 1.13)
0.15 0.184
urea_nitrogen 1.02
(1.01 – 1.03)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.040
Observations 824
Deviance 487.942
log-Likelihood -243.971
anova(modf_8,modf_9,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + temperature
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       815     489.73                     
## 2       814     487.94  1   1.7837   0.1817
#Elimina temperatura


modf_10 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+inr, family = binomial(), data = df_train)
tab_model(modf_10,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.65
(1.55 – 4.73)
0.75 0.001
bicarbonate 0.94
(0.89 – 0.99)
0.03 0.022
blood_calcium 0.50
(0.32 – 0.77)
0.11 0.002
inr 1.17
(0.93 – 1.49)
0.14 0.184
lactic_acid 1.43
(1.11 – 1.84)
0.18 0.006
leucocyte 1.06
(1.02 – 1.10)
0.02 0.006
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.027
urea_nitrogen 1.02
(1.01 – 1.03)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.024
Observations 824
Deviance 487.948
log-Likelihood -243.974
anova(modf_8,modf_10,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + inr
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       815     489.73                     
## 2       814     487.95  1   1.7779   0.1824
#Elimina inr


modf_11 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd, family = binomial(), data = df_train)
tab_model(modf_11,show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.46
(1.42 – 4.43)
0.71 0.002
bicarbonate 0.93
(0.88 – 0.98)
0.03 0.014
blood_calcium 0.52
(0.33 – 0.82)
0.12 0.006
ckd1 4.38
(2.33 – 8.68)
1.47 <0.001
lactic_acid 1.34
(1.04 – 1.73)
0.17 0.024
leucocyte 1.05
(1.00 – 1.09)
0.02 0.031
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.019
urea_nitrogen 1.03
(1.02 – 1.04)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.020
Observations 824
Deviance 466.909
log-Likelihood -233.454
anova(modf_8,modf_11,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd
##   Resid. Df Resid. Dev Df Deviance  Pr(>Chi)    
## 1       815     489.73                          
## 2       814     466.91  1   22.817 1.782e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
modf_12 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd+a_fib, family = "binomial", data = df_train)
tab_model(modf_12, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
a_fib1 1.26
(0.78 – 2.06)
0.31 0.347
anemia1 2.41
(1.39 – 4.35)
0.70 0.002
bicarbonate 0.93
(0.88 – 0.99)
0.03 0.015
blood_calcium 0.51
(0.32 – 0.80)
0.12 0.004
ckd1 4.40
(2.34 – 8.69)
1.47 <0.001
lactic_acid 1.34
(1.04 – 1.73)
0.17 0.024
leucocyte 1.05
(1.00 – 1.09)
0.02 0.038
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.025
urea_nitrogen 1.03
(1.02 – 1.04)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.025
Observations 824
Deviance 466.022
log-Likelihood -233.011
anova(modf_11,modf_12,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd + a_fib
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       814     466.91                     
## 2       813     466.02  1  0.88672   0.3464
#Elimina a_fib


modf_13 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd+platelets, family = "binomial", data = df_train)
tab_model(modf_13, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.38
(1.37 – 4.31)
0.69 0.003
bicarbonate 0.93
(0.88 – 0.99)
0.03 0.016
blood_calcium 0.54
(0.34 – 0.85)
0.13 0.009
ckd1 4.34
(2.31 – 8.57)
1.45 <0.001
lactic_acid 1.31
(1.02 – 1.70)
0.17 0.040
leucocyte 1.06
(1.01 – 1.10)
0.02 0.013
platelets 1.00
(1.00 – 1.00)
0.00 0.179
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.031
urea_nitrogen 1.03
(1.02 – 1.04)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.017
Observations 824
Deviance 465.021
log-Likelihood -232.510
anova(modf_11,modf_13,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd + platelets
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       814     466.91                     
## 2       813     465.02  1   1.8885   0.1694
#Elimina platelets


modf_14 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd+respiratory_rate, family = "binomial", data = df_train)
tab_model(modf_14, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.55
(1.46 – 4.62)
0.74 0.001
bicarbonate 0.93
(0.88 – 0.98)
0.03 0.014
blood_calcium 0.50
(0.31 – 0.79)
0.12 0.003
ckd1 4.16
(2.21 – 8.24)
1.39 <0.001
lactic_acid 1.32
(1.02 – 1.71)
0.17 0.034
leucocyte 1.04
(1.00 – 1.09)
0.02 0.065
respiratory_rate 1.05
(0.99 – 1.12)
0.03 0.071
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.025
urea_nitrogen 1.03
(1.02 – 1.04)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.016
Observations 824
Deviance 463.697
log-Likelihood -231.848
anova(modf_11,modf_14,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd + respiratory_rate
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)  
## 1       814     466.91                       
## 2       813     463.70  1    3.212   0.0731 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Eliminia respiratory_rate


modf_15 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd+magnesium_ion, family = "binomial", data = df_train)
tab_model(modf_15, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.52
(1.45 – 4.53)
0.73 0.001
bicarbonate 0.92
(0.87 – 0.98)
0.03 0.007
blood_calcium 0.48
(0.30 – 0.77)
0.12 0.003
ckd1 4.25
(2.27 – 8.37)
1.41 <0.001
lactic_acid 1.32
(1.02 – 1.71)
0.17 0.033
leucocyte 1.05
(1.00 – 1.09)
0.02 0.030
magnesium_ion 2.55
(0.95 – 6.55)
1.25 0.056
systolic_blood_pressure 0.98
(0.96 – 1.00)
0.01 0.015
urea_nitrogen 1.03
(1.02 – 1.04)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.028
Observations 824
Deviance 463.417
log-Likelihood -231.708
anova(modf_11,modf_15,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd + magnesium_ion
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)  
## 1       814     466.91                       
## 2       813     463.42  1   3.4924  0.06165 .
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Elimina magnesium_ion pero al limite


modf_16 = glm(died~bicarbonate+lactic_acid+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd+heart_rate, family = "binomial", data = df_train)
tab_model(modf_16, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
anemia1 2.51
(1.44 – 4.53)
0.73 0.002
bicarbonate 0.93
(0.88 – 0.99)
0.03 0.018
blood_calcium 0.52
(0.33 – 0.82)
0.12 0.006
ckd1 4.27
(2.26 – 8.49)
1.44 <0.001
heart_rate 1.01
(1.00 – 1.03)
0.01 0.176
lactic_acid 1.32
(1.02 – 1.71)
0.17 0.034
leucocyte 1.04
(1.00 – 1.09)
0.02 0.043
systolic_blood_pressure 0.98
(0.97 – 1.00)
0.01 0.025
urea_nitrogen 1.03
(1.02 – 1.05)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.017
Observations 824
Deviance 465.083
log-Likelihood -232.541
anova(modf_11,modf_16,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd
## Model 2: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd + heart_rate
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       814     466.91                     
## 2       813     465.08  1   1.8262   0.1766
#Elimina heart_rate



#Dado que se limita a 10 variables y edad y genero a pesar de poca significancia deben ser incluidas se elimina ac lactico
mod_final = glm(died~bicarbonate+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd+age+gender, family = "binomial", data = df_train)
tab_model(mod_final, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
age 1.01
(0.99 – 1.03)
0.01 0.535
anemia1 2.58
(1.49 – 4.63)
0.74 0.001
bicarbonate 0.92
(0.87 – 0.97)
0.03 0.002
blood_calcium 0.53
(0.33 – 0.83)
0.12 0.007
ckd1 4.85
(2.60 – 9.52)
1.60 <0.001
gender1 1.00
(0.61 – 1.64)
0.25 0.988
leucocyte 1.05
(1.01 – 1.10)
0.02 0.010
systolic_blood_pressure 0.98
(0.96 – 0.99)
0.01 0.009
urea_nitrogen 1.03
(1.02 – 1.04)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.022
Observations 824
Deviance 471.757
log-Likelihood -235.878
anova(modf_11,mod_final,test = "LRT")
## Analysis of Deviance Table
## 
## Model 1: died ~ bicarbonate + lactic_acid + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd
## Model 2: died ~ bicarbonate + urine_output + leucocyte + urea_nitrogen + 
##     blood_calcium + systolic_blood_pressure + anemia + ckd + 
##     age + gender
##   Resid. Df Resid. Dev Df Deviance Pr(>Chi)
## 1       814     466.91                     
## 2       813     471.76  1  -4.8477
knitr::asis_output(tab_model(mod_final, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)$knitr)
  died
Predictors Odds Ratios std. Error p
age 1.01
(0.99 – 1.03)
0.01 0.535
anemia1 2.58
(1.49 – 4.63)
0.74 0.001
bicarbonate 0.92
(0.87 – 0.97)
0.03 0.002
blood_calcium 0.53
(0.33 – 0.83)
0.12 0.007
ckd1 4.85
(2.60 – 9.52)
1.60 <0.001
gender1 1.00
(0.61 – 1.64)
0.25 0.988
leucocyte 1.05
(1.01 – 1.10)
0.02 0.010
systolic_blood_pressure 0.98
(0.96 – 0.99)
0.01 0.009
urea_nitrogen 1.03
(1.02 – 1.04)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.022
Observations 824
Deviance 471.757
log-Likelihood -235.878
anova(mod_final, test = "LRT")
## Analysis of Deviance Table
## 
## Model: binomial, link: logit
## 
## Response: died
## 
## Terms added sequentially (first to last)
## 
## 
##                         Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                                      823     640.10              
## bicarbonate              1   59.735       822     580.37 1.085e-14 ***
## urine_output             1   22.956       821     557.41 1.657e-06 ***
## leucocyte                1   16.109       820     541.30 5.981e-05 ***
## urea_nitrogen            1    7.400       819     533.90  0.006521 ** 
## blood_calcium            1    9.318       818     524.59  0.002269 ** 
## systolic_blood_pressure  1    8.780       817     515.81  0.003046 ** 
## anemia                   1   16.074       816     499.73 6.092e-05 ***
## ckd                      1   27.585       815     472.15 1.504e-07 ***
## age                      1    0.390       814     471.76  0.532297    
## gender                   1    0.000       813     471.76  0.988015    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
summary(mod_final)
## 
## Call:
## glm(formula = died ~ bicarbonate + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia + ckd + age + gender, family = "binomial", data = df_train)
## 
## Coefficients:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)              4.4666236  2.1719048   2.057 0.039730 *  
## bicarbonate             -0.0879388  0.0278183  -3.161 0.001571 ** 
## urine_output            -0.0003119  0.0001357  -2.298 0.021580 *  
## leucocyte                0.0527075  0.0203896   2.585 0.009737 ** 
## urea_nitrogen            0.0313721  0.0059619   5.262 1.42e-07 ***
## blood_calcium           -0.6295374  0.2324172  -2.709 0.006756 ** 
## systolic_blood_pressure -0.0213411  0.0081300  -2.625 0.008665 ** 
## anemia1                  0.9464715  0.2874952   3.292 0.000994 ***
## ckd1                     1.5790738  0.3295667   4.791 1.66e-06 ***
## age                      0.0059081  0.0095197   0.621 0.534852    
## gender1                  0.0037680  0.2508406   0.015 0.988015    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 640.10  on 823  degrees of freedom
## Residual deviance: 471.76  on 813  degrees of freedom
## AIC: 493.76
## 
## Number of Fisher Scoring iterations: 6

Confundidores

mod_final_conf = glm(died~bicarbonate+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia+ckd+age+gender+copd, family = "binomial", data = df_train)
tab_model(mod_final_conf, show.se = T, show.dev = T, show.loglik = T, show.intercept = F,show.stat = F, show.reflvl = T, digits = 2, digits.p = 3, show.r2 = F,collapse.ci = T)
  died
Predictors Odds Ratios std. Error p
age 1.01
(0.99 – 1.03)
0.01 0.480
anemia1 2.64
(1.53 – 4.77)
0.77 0.001
bicarbonate 0.92
(0.87 – 0.98)
0.03 0.005
blood_calcium 0.53
(0.33 – 0.82)
0.12 0.006
ckd1 5.17
(2.77 – 10.17)
1.71 <0.001
copd1 3.95
(1.24 – 18.28)
2.64 0.040
gender1 1.07
(0.65 – 1.75)
0.27 0.800
leucocyte 1.06
(1.01 – 1.10)
0.02 0.008
systolic_blood_pressure 0.98
(0.96 – 1.00)
0.01 0.012
urea_nitrogen 1.03
(1.02 – 1.05)
0.01 <0.001
urine_output 1.00
(1.00 – 1.00)
0.00 0.017
Observations 824
Deviance 466.104
log-Likelihood -233.052
anova(mod_final_conf, test = "LRT")
## Analysis of Deviance Table
## 
## Model: binomial, link: logit
## 
## Response: died
## 
## Terms added sequentially (first to last)
## 
## 
##                         Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                                      823     640.10              
## bicarbonate              1   59.735       822     580.37 1.085e-14 ***
## urine_output             1   22.956       821     557.41 1.657e-06 ***
## leucocyte                1   16.109       820     541.30 5.981e-05 ***
## urea_nitrogen            1    7.400       819     533.90  0.006521 ** 
## blood_calcium            1    9.318       818     524.59  0.002269 ** 
## systolic_blood_pressure  1    8.780       817     515.81  0.003046 ** 
## anemia                   1   16.074       816     499.73 6.092e-05 ***
## ckd                      1   27.585       815     472.15 1.504e-07 ***
## age                      1    0.390       814     471.76  0.532297    
## gender                   1    0.000       813     471.76  0.988015    
## copd                     1    5.652       812     466.10  0.017433 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
tab_model(mod_final, mod_final_conf,
          show.ci = TRUE,
          show.p = TRUE,
          dv.labels = c("Modelo original ", "Modelo COPD"),
          title = "Comparación de coeficientes para evaluar efecto confundidor de COPD")
Comparación de coeficientes para evaluar efecto confundidor de COPD
  Modelo original Modelo COPD
Predictors Odds Ratios CI p Odds Ratios CI p
(Intercept) 87.06 0.00 – Inf 0.040 15.30 0.00 – Inf 0.240
bicarbonate 0.92 0.00 – Inf 0.002 0.92 0.00 – Inf 0.005
urine output 1.00 0.00 – Inf 0.022 1.00 0.00 – Inf 0.017
leucocyte 1.05 0.00 – Inf 0.010 1.06 0.00 – Inf 0.008
urea nitrogen 1.03 0.00 – Inf <0.001 1.03 0.00 – Inf <0.001
blood calcium 0.53 0.00 – Inf 0.007 0.53 0.00 – Inf 0.006
systolic blood pressure 0.98 0.00 – Inf 0.009 0.98 0.00 – Inf 0.012
anemia [1] 2.58 0.00 – Inf 0.001 2.64 0.00 – Inf 0.001
ckd [1] 4.85 0.00 – Inf <0.001 5.17 0.00 – Inf <0.001
age 1.01 0.00 – Inf 0.535 1.01 0.00 – Inf 0.480
gender [1] 1.00 0.00 – Inf 0.988 1.07 0.00 – Inf 0.800
copd [1] 3.95 0.00 – Inf 0.040
Observations 824 824
R2 Tjur 0.259 0.268

Los predictores ckd se modifican un 6.6% y gender un 7%. No existe efecto confundidor.

Interacciones

mod_final_int = glm(died~bicarbonate+urine_output+leucocyte+urea_nitrogen+blood_calcium+systolic_blood_pressure+anemia*copd+ckd+age+gender, family = "binomial", data = df_train)
tab_mod_final = tab_model(mod_final_int,
          transform = NULL,
          show.se = TRUE,
          show.dev = TRUE,
          show.loglik = TRUE,
          show.intercept = FALSE,
          show.stat = FALSE,
          show.reflvl = TRUE,
          digits = 2,
          digits.p = 3,
          show.r2 = FALSE,
          collapse.ci = TRUE)
anova(mod_final_int, test = "LRT")
## Analysis of Deviance Table
## 
## Model: binomial, link: logit
## 
## Response: died
## 
## Terms added sequentially (first to last)
## 
## 
##                         Df Deviance Resid. Df Resid. Dev  Pr(>Chi)    
## NULL                                      823     640.10              
## bicarbonate              1   59.735       822     580.37 1.085e-14 ***
## urine_output             1   22.956       821     557.41 1.657e-06 ***
## leucocyte                1   16.109       820     541.30 5.981e-05 ***
## urea_nitrogen            1    7.400       819     533.90  0.006521 ** 
## blood_calcium            1    9.318       818     524.59  0.002269 ** 
## systolic_blood_pressure  1    8.780       817     515.81  0.003046 ** 
## anemia                   1   16.074       816     499.73 6.092e-05 ***
## copd                     1    3.506       815     496.23  0.061139 .  
## ckd                      1   29.566       814     466.66 5.405e-08 ***
## age                      1    0.491       813     466.17  0.483587    
## gender                   1    0.064       812     466.10  0.799618    
## anemia:copd              1    1.275       811     464.83  0.258884    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# Crear columna combinada para representar los grupos de interacción
df_train <- df_train %>%
  mutate(anemia_copd = interaction(anemia, copd, sep = "_"))

# Gráfico 1: log-odds por edad y grupo de interacción (todos)
p1 <- df_train %>%
  mutate(logodds = predict(mod_final_int, newdata = ., type = "link")) %>%
  ggplot(aes(x = age, y = logodds, color = anemia_copd)) +
  geom_point(alpha = 0.5) +
  geom_smooth(method = "loess", se = FALSE) +
  ylab("Log-odds de muerte") +
  ggtitle("Modelo con interacción: anemia * COPD") +
  theme_minimal() +
  labs(color = "Anemia_COPD")
print(p1)
## `geom_smooth()` using formula = 'y ~ x'

Se observa cierta interaccion entre anemia y copd.

Evaluacion por subgrupo de pacientes copd, con y sin anemia

# Filtrar solo pacientes con EPOC
df_copd <- subset(df_train, copd == 1)

# Subgrupo: pacientes con EPOC y ANEMIA
df_copd_anemia_yes <- subset(df_copd, anemia == 1)

mod_copd_anemia_yes <- glm(died ~ bicarbonate + urine_output + leucocyte + urea_nitrogen +
                             blood_calcium + systolic_blood_pressure + ckd + age + gender,
                           family = "binomial", data = df_copd_anemia_yes)

summary(mod_copd_anemia_yes)
## 
## Call:
## glm(formula = died ~ bicarbonate + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     ckd + age + gender, family = "binomial", data = df_copd_anemia_yes)
## 
## Coefficients:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)              5.1075969  2.5696802   1.988 0.046852 *  
## bicarbonate             -0.0735620  0.0315926  -2.328 0.019888 *  
## urine_output            -0.0003535  0.0001552  -2.278 0.022739 *  
## leucocyte                0.0440281  0.0231721   1.900 0.057426 .  
## urea_nitrogen            0.0304819  0.0073251   4.161 3.16e-05 ***
## blood_calcium           -0.7276781  0.2686014  -2.709 0.006746 ** 
## systolic_blood_pressure -0.0197650  0.0090918  -2.174 0.029710 *  
## ckd1                     1.2671332  0.3579892   3.540 0.000401 ***
## age                      0.0207763  0.0117758   1.764 0.077678 .  
## gender1                 -0.0070442  0.2904980  -0.024 0.980654    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 448.98  on 489  degrees of freedom
## Residual deviance: 337.10  on 480  degrees of freedom
## AIC: 357.1
## 
## Number of Fisher Scoring iterations: 5
# Subgrupo: pacientes con EPOC y SIN ANEMIA
df_copd_anemia_no <- subset(df_copd, anemia == 0)

mod_copd_anemia_no <- glm(died ~ bicarbonate + urine_output + leucocyte + urea_nitrogen +
                            blood_calcium + systolic_blood_pressure + ckd + age + gender,
                          family = "binomial", data = df_copd_anemia_no)

summary(mod_copd_anemia_no)
## 
## Call:
## glm(formula = died ~ bicarbonate + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     ckd + age + gender, family = "binomial", data = df_copd_anemia_no)
## 
## Coefficients:
##                           Estimate Std. Error z value Pr(>|z|)    
## (Intercept)              4.396e+00  4.923e+00   0.893 0.371844    
## bicarbonate             -1.043e-01  7.140e-02  -1.460 0.144215    
## urine_output             4.692e-06  3.143e-04   0.015 0.988089    
## leucocyte                1.106e-01  5.130e-02   2.155 0.031139 *  
## urea_nitrogen            4.433e-02  1.257e-02   3.526 0.000422 ***
## blood_calcium           -7.521e-01  4.990e-01  -1.507 0.131737    
## systolic_blood_pressure -2.370e-02  2.168e-02  -1.093 0.274408    
## ckd1                     3.150e+00  8.759e-01   3.596 0.000323 ***
## age                     -1.304e-02  1.763e-02  -0.740 0.459595    
## gender1                  4.033e-01  5.656e-01   0.713 0.475807    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 147.098  on 266  degrees of freedom
## Residual deviance:  97.582  on 257  degrees of freedom
## AIC: 117.58
## 
## Number of Fisher Scoring iterations: 7

Calibracion con el modf_5 da los mejores resultados con trampa, se uso utilizando solo los coeficientes que estan contenidos tanto en el test como train. el modf_14 es el modelo que mejor performance tenia previo al test

#Creacion de un dataframe con predichos y observados

mod_pred = as.data.frame(predict(mod_final_int, df_train, type = "response")) %>% rename(pred=`predict(mod_final_int, df_train, type = "response")`)
mod_pred$obs <- as.numeric(as.character(df_train$died))

#HL test

library(vcdExtra)
## Cargando paquete requerido: vcd
## Cargando paquete requerido: grid
## Cargando paquete requerido: gnm
## 
## Adjuntando el paquete: 'vcdExtra'
## The following object is masked from 'package:dplyr':
## 
##     summarise
h_l = HLtest(mod_final_int, 5)
hl_df <- cbind(as.data.frame(h_l$table), indice = as.factor(c(1,2,3,4,5,6,7,8,9,10)))
## Warning in data.frame(..., check.names = FALSE): los nombres de fila se
## encontraron a partir de una variable corta y se descartaron
h_l
## Hosmer and Lemeshow Goodness-of-Fit Test 
## 
## Call:
## glm(formula = died ~ bicarbonate + urine_output + leucocyte + 
##     urea_nitrogen + blood_calcium + systolic_blood_pressure + 
##     anemia * copd + ckd + age + gender, family = "binomial", 
##     data = df_train)
##  ChiSquare df   P_value
##   1.618739  3 0.6551484
ggplot(hl_df, aes(x=indice))+geom_point(aes(y=exp, color = "blue"))+geom_point(aes(y=obs, color= "red"))+scale_color_manual(labels = c("Esperado", "Observado"), values = c("blue", "red"))

Slope % Intercept

library(CalibrationCurves)
## Cargando paquete requerido: rms
## Cargando paquete requerido: Hmisc
## 
## Adjuntando el paquete: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
s_i = valProbggplot(mod_pred$pred,mod_pred$obs)
s_i$ggPlot

Curva ROC

library(pROC)
## Type 'citation("pROC")' for a citation.
## 
## Adjuntando el paquete: 'pROC'
## The following objects are masked from 'package:stats':
## 
##     cov, smooth, var
roc_train<-roc(mod_pred$obs, mod_pred$pred)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
plot(roc_train, main = "Curva ROC", col = "red", lwd = 3)

text(0.8, 0.2, paste("Area bajo la Curva ROC =", round(roc_train$auc, 3)), adj = c(0, 1))

Todos los puntos de cortes

library(pROC)
library(dplyr)
library(knitr)

# Curva ROC
roc_obj <- roc(mod_pred$obs, mod_pred$pred, levels = c(0, 1), direction = "<")  # corregí el level de 2 a 1 si tu evento positivo es 1
SENS  <- roc_obj$sensitivities
ESPEC <- roc_obj$specificities
CUTOFF <- roc_obj$thresholds

# Tabla con sensibilidad, especificidad y umbrales
d <- cbind(SENS, ESPEC, CUTOFF)
kable(d)
SENS ESPEC CUTOFF
1.0000000 0.0000000 -Inf
1.0000000 0.0013966 0.0008688
1.0000000 0.0027933 0.0013888
1.0000000 0.0041899 0.0014137
1.0000000 0.0055866 0.0015177
1.0000000 0.0069832 0.0016665
1.0000000 0.0083799 0.0017711
1.0000000 0.0097765 0.0018624
1.0000000 0.0111732 0.0019241
1.0000000 0.0125698 0.0020186
1.0000000 0.0139665 0.0021339
1.0000000 0.0153631 0.0022334
1.0000000 0.0167598 0.0024711
1.0000000 0.0181564 0.0027013
1.0000000 0.0195531 0.0028074
1.0000000 0.0209497 0.0030788
1.0000000 0.0223464 0.0034219
1.0000000 0.0237430 0.0035695
1.0000000 0.0251397 0.0036107
1.0000000 0.0265363 0.0036354
1.0000000 0.0279330 0.0036911
1.0000000 0.0293296 0.0037756
1.0000000 0.0307263 0.0038234
1.0000000 0.0321229 0.0038467
1.0000000 0.0335196 0.0040256
1.0000000 0.0349162 0.0041854
1.0000000 0.0363128 0.0043065
1.0000000 0.0377095 0.0045049
1.0000000 0.0391061 0.0045924
1.0000000 0.0405028 0.0046530
1.0000000 0.0418994 0.0048111
1.0000000 0.0432961 0.0049729
1.0000000 0.0446927 0.0051768
1.0000000 0.0460894 0.0053762
1.0000000 0.0474860 0.0055494
1.0000000 0.0488827 0.0057004
1.0000000 0.0502793 0.0057405
1.0000000 0.0516760 0.0058103
1.0000000 0.0530726 0.0058717
1.0000000 0.0544693 0.0058818
1.0000000 0.0558659 0.0059236
1.0000000 0.0572626 0.0059785
1.0000000 0.0586592 0.0060630
1.0000000 0.0600559 0.0062350
1.0000000 0.0614525 0.0063627
1.0000000 0.0628492 0.0064129
1.0000000 0.0642458 0.0066503
1.0000000 0.0656425 0.0069525
1.0000000 0.0670391 0.0070485
1.0000000 0.0684358 0.0070544
1.0000000 0.0698324 0.0070805
1.0000000 0.0712291 0.0071143
1.0000000 0.0726257 0.0072261
1.0000000 0.0740223 0.0073743
1.0000000 0.0754190 0.0074354
1.0000000 0.0768156 0.0074493
1.0000000 0.0782123 0.0074849
1.0000000 0.0796089 0.0075308
1.0000000 0.0810056 0.0076565
1.0000000 0.0824022 0.0079598
1.0000000 0.0837989 0.0082331
1.0000000 0.0851955 0.0083675
1.0000000 0.0865922 0.0084241
1.0000000 0.0879888 0.0085483
1.0000000 0.0893855 0.0087038
1.0000000 0.0907821 0.0088773
1.0000000 0.0921788 0.0090975
1.0000000 0.0935754 0.0092404
1.0000000 0.0949721 0.0093082
0.9907407 0.0949721 0.0093437
0.9907407 0.0963687 0.0093933
0.9907407 0.0977654 0.0094587
0.9907407 0.0991620 0.0095032
0.9907407 0.1005587 0.0095223
0.9907407 0.1019553 0.0095492
0.9907407 0.1033520 0.0096354
0.9907407 0.1047486 0.0098102
0.9907407 0.1061453 0.0099561
0.9907407 0.1075419 0.0100392
0.9907407 0.1089385 0.0101175
0.9907407 0.1103352 0.0102374
0.9907407 0.1117318 0.0103364
0.9907407 0.1131285 0.0104884
0.9907407 0.1145251 0.0106851
0.9907407 0.1159218 0.0108370
0.9907407 0.1173184 0.0109838
0.9907407 0.1187151 0.0110465
0.9907407 0.1201117 0.0110518
0.9907407 0.1215084 0.0110592
0.9907407 0.1229050 0.0110648
0.9907407 0.1243017 0.0110909
0.9907407 0.1256983 0.0112364
0.9907407 0.1270950 0.0114493
0.9907407 0.1284916 0.0115894
0.9907407 0.1298883 0.0118225
0.9907407 0.1312849 0.0120168
0.9907407 0.1326816 0.0120306
0.9907407 0.1340782 0.0120561
0.9907407 0.1354749 0.0121470
0.9907407 0.1368715 0.0122191
0.9907407 0.1382682 0.0122684
0.9907407 0.1396648 0.0124197
0.9907407 0.1410615 0.0125620
0.9907407 0.1424581 0.0126038
0.9907407 0.1438547 0.0126795
0.9907407 0.1452514 0.0127607
0.9907407 0.1466480 0.0127789
0.9907407 0.1480447 0.0127894
0.9907407 0.1494413 0.0127949
0.9907407 0.1508380 0.0129418
0.9907407 0.1522346 0.0130906
0.9907407 0.1536313 0.0132484
0.9907407 0.1550279 0.0134420
0.9907407 0.1564246 0.0134928
0.9907407 0.1578212 0.0135686
0.9907407 0.1592179 0.0136771
0.9907407 0.1606145 0.0137931
0.9907407 0.1620112 0.0138980
0.9907407 0.1634078 0.0139650
0.9907407 0.1648045 0.0140641
0.9907407 0.1662011 0.0142645
0.9907407 0.1675978 0.0144230
0.9907407 0.1689944 0.0144899
0.9907407 0.1703911 0.0146002
0.9814815 0.1703911 0.0147117
0.9814815 0.1717877 0.0147737
0.9814815 0.1731844 0.0148008
0.9814815 0.1745810 0.0148477
0.9722222 0.1745810 0.0149489
0.9722222 0.1759777 0.0152062
0.9722222 0.1773743 0.0154589
0.9722222 0.1787709 0.0155382
0.9722222 0.1801676 0.0155678
0.9722222 0.1815642 0.0156579
0.9722222 0.1829609 0.0158184
0.9722222 0.1843575 0.0159820
0.9722222 0.1857542 0.0160760
0.9722222 0.1871508 0.0161497
0.9722222 0.1885475 0.0162235
0.9722222 0.1899441 0.0162843
0.9722222 0.1913408 0.0163946
0.9722222 0.1927374 0.0165152
0.9722222 0.1941341 0.0165802
0.9722222 0.1955307 0.0166111
0.9722222 0.1969274 0.0166423
0.9722222 0.1983240 0.0167967
0.9722222 0.1997207 0.0169975
0.9722222 0.2011173 0.0170585
0.9722222 0.2025140 0.0171033
0.9722222 0.2039106 0.0172277
0.9722222 0.2053073 0.0173835
0.9722222 0.2067039 0.0174655
0.9722222 0.2081006 0.0175592
0.9722222 0.2094972 0.0177059
0.9722222 0.2108939 0.0177929
0.9722222 0.2122905 0.0178639
0.9722222 0.2136872 0.0179522
0.9722222 0.2150838 0.0181240
0.9722222 0.2164804 0.0185436
0.9722222 0.2178771 0.0188636
0.9722222 0.2192737 0.0189315
0.9722222 0.2206704 0.0191261
0.9722222 0.2220670 0.0193503
0.9722222 0.2234637 0.0194771
0.9722222 0.2248603 0.0196179
0.9722222 0.2262570 0.0197557
0.9722222 0.2276536 0.0199807
0.9722222 0.2290503 0.0201704
0.9722222 0.2304469 0.0203016
0.9722222 0.2318436 0.0204953
0.9722222 0.2332402 0.0207716
0.9722222 0.2346369 0.0210294
0.9722222 0.2360335 0.0211433
0.9722222 0.2374302 0.0213062
0.9722222 0.2388268 0.0214284
0.9722222 0.2402235 0.0216126
0.9722222 0.2416201 0.0218449
0.9722222 0.2430168 0.0219313
0.9722222 0.2444134 0.0219686
0.9722222 0.2458101 0.0220580
0.9722222 0.2472067 0.0221663
0.9722222 0.2486034 0.0222709
0.9722222 0.2500000 0.0225956
0.9722222 0.2513966 0.0229465
0.9722222 0.2527933 0.0231427
0.9722222 0.2541899 0.0232582
0.9722222 0.2555866 0.0233548
0.9722222 0.2569832 0.0234327
0.9722222 0.2583799 0.0234779
0.9722222 0.2597765 0.0235384
0.9722222 0.2611732 0.0235728
0.9722222 0.2625698 0.0236424
0.9722222 0.2639665 0.0237592
0.9722222 0.2653631 0.0238872
0.9722222 0.2667598 0.0240519
0.9722222 0.2681564 0.0241994
0.9722222 0.2695531 0.0242601
0.9722222 0.2709497 0.0243021
0.9722222 0.2723464 0.0244612
0.9722222 0.2737430 0.0245897
0.9722222 0.2751397 0.0247290
0.9722222 0.2765363 0.0249480
0.9722222 0.2779330 0.0250656
0.9722222 0.2793296 0.0251830
0.9722222 0.2807263 0.0253019
0.9722222 0.2821229 0.0254333
0.9722222 0.2835196 0.0255331
0.9722222 0.2849162 0.0255856
0.9722222 0.2863128 0.0257272
0.9722222 0.2877095 0.0259131
0.9722222 0.2891061 0.0261553
0.9722222 0.2905028 0.0263356
0.9722222 0.2918994 0.0263970
0.9722222 0.2932961 0.0264359
0.9722222 0.2946927 0.0266826
0.9722222 0.2960894 0.0270665
0.9722222 0.2974860 0.0272333
0.9722222 0.2988827 0.0272998
0.9722222 0.3002793 0.0273890
0.9722222 0.3016760 0.0274973
0.9722222 0.3030726 0.0276085
0.9722222 0.3044693 0.0279560
0.9722222 0.3058659 0.0282845
0.9722222 0.3072626 0.0283970
0.9722222 0.3086592 0.0285317
0.9722222 0.3100559 0.0286550
0.9722222 0.3114525 0.0287334
0.9722222 0.3128492 0.0287553
0.9722222 0.3142458 0.0288683
0.9722222 0.3156425 0.0290023
0.9722222 0.3170391 0.0292273
0.9722222 0.3184358 0.0294997
0.9722222 0.3198324 0.0296028
0.9722222 0.3212291 0.0296271
0.9722222 0.3226257 0.0297238
0.9722222 0.3240223 0.0298434
0.9722222 0.3254190 0.0300218
0.9722222 0.3268156 0.0301833
0.9722222 0.3282123 0.0302506
0.9722222 0.3296089 0.0305772
0.9722222 0.3310056 0.0308618
0.9722222 0.3324022 0.0308942
0.9722222 0.3337989 0.0310036
0.9722222 0.3351955 0.0311999
0.9722222 0.3365922 0.0315157
0.9722222 0.3379888 0.0317316
0.9722222 0.3393855 0.0317437
0.9722222 0.3407821 0.0318001
0.9722222 0.3421788 0.0318602
0.9722222 0.3435754 0.0319043
0.9722222 0.3449721 0.0319612
0.9722222 0.3463687 0.0320029
0.9722222 0.3477654 0.0320958
0.9722222 0.3491620 0.0321688
0.9722222 0.3505587 0.0325289
0.9722222 0.3519553 0.0329287
0.9722222 0.3533520 0.0329832
0.9722222 0.3547486 0.0330084
0.9722222 0.3561453 0.0330373
0.9722222 0.3575419 0.0331526
0.9722222 0.3589385 0.0332606
0.9722222 0.3603352 0.0333733
0.9722222 0.3617318 0.0334975
0.9722222 0.3631285 0.0336182
0.9722222 0.3645251 0.0337565
0.9722222 0.3659218 0.0338500
0.9722222 0.3673184 0.0339041
0.9722222 0.3687151 0.0339916
0.9722222 0.3701117 0.0342625
0.9722222 0.3715084 0.0344846
0.9722222 0.3729050 0.0347094
0.9722222 0.3743017 0.0349811
0.9722222 0.3756983 0.0351202
0.9722222 0.3770950 0.0354880
0.9722222 0.3784916 0.0358705
0.9722222 0.3798883 0.0359535
0.9722222 0.3812849 0.0360423
0.9722222 0.3826816 0.0361484
0.9722222 0.3840782 0.0361890
0.9722222 0.3854749 0.0362582
0.9722222 0.3868715 0.0364467
0.9722222 0.3882682 0.0365800
0.9722222 0.3896648 0.0366380
0.9722222 0.3910615 0.0367623
0.9722222 0.3924581 0.0370454
0.9722222 0.3938547 0.0372874
0.9722222 0.3952514 0.0373186
0.9722222 0.3966480 0.0373750
0.9722222 0.3980447 0.0374985
0.9722222 0.3994413 0.0376248
0.9722222 0.4008380 0.0377618
0.9722222 0.4022346 0.0378412
0.9722222 0.4036313 0.0379021
0.9722222 0.4050279 0.0379963
0.9722222 0.4064246 0.0380711
0.9629630 0.4064246 0.0383966
0.9629630 0.4078212 0.0388293
0.9629630 0.4092179 0.0390815
0.9537037 0.4092179 0.0394671
0.9537037 0.4106145 0.0397506
0.9537037 0.4120112 0.0398670
0.9537037 0.4134078 0.0400341
0.9537037 0.4148045 0.0400946
0.9537037 0.4162011 0.0404803
0.9537037 0.4175978 0.0408725
0.9537037 0.4189944 0.0410674
0.9537037 0.4203911 0.0412698
0.9537037 0.4217877 0.0413174
0.9537037 0.4231844 0.0413633
0.9537037 0.4245810 0.0415141
0.9537037 0.4259777 0.0416785
0.9537037 0.4273743 0.0417551
0.9537037 0.4287709 0.0419681
0.9537037 0.4301676 0.0421386
0.9537037 0.4315642 0.0422680
0.9444444 0.4315642 0.0424324
0.9444444 0.4329609 0.0424874
0.9444444 0.4343575 0.0425097
0.9444444 0.4357542 0.0426317
0.9351852 0.4357542 0.0427695
0.9351852 0.4371508 0.0430615
0.9351852 0.4385475 0.0434078
0.9351852 0.4399441 0.0435081
0.9351852 0.4413408 0.0436729
0.9351852 0.4427374 0.0441423
0.9351852 0.4441341 0.0447898
0.9351852 0.4455307 0.0452535
0.9351852 0.4469274 0.0454439
0.9351852 0.4483240 0.0457693
0.9351852 0.4497207 0.0462263
0.9351852 0.4511173 0.0466542
0.9351852 0.4525140 0.0469824
0.9259259 0.4525140 0.0471719
0.9259259 0.4539106 0.0473169
0.9259259 0.4553073 0.0474414
0.9259259 0.4567039 0.0475789
0.9259259 0.4581006 0.0476396
0.9259259 0.4594972 0.0477044
0.9259259 0.4608939 0.0477688
0.9166667 0.4608939 0.0478330
0.9166667 0.4622905 0.0479531
0.9166667 0.4636872 0.0480277
0.9166667 0.4650838 0.0480664
0.9166667 0.4664804 0.0481872
0.9166667 0.4678771 0.0483022
0.9166667 0.4692737 0.0483444
0.9166667 0.4706704 0.0483713
0.9166667 0.4720670 0.0484775
0.9166667 0.4734637 0.0486079
0.9166667 0.4748603 0.0486802
0.9166667 0.4762570 0.0487259
0.9166667 0.4776536 0.0488105
0.9166667 0.4790503 0.0488836
0.9074074 0.4790503 0.0490175
0.9074074 0.4804469 0.0491955
0.9074074 0.4818436 0.0493514
0.9074074 0.4832402 0.0496650
0.9074074 0.4846369 0.0500567
0.9074074 0.4860335 0.0502752
0.9074074 0.4874302 0.0505938
0.9074074 0.4888268 0.0510346
0.9074074 0.4902235 0.0512303
0.9074074 0.4916201 0.0513485
0.9074074 0.4930168 0.0518156
0.8981481 0.4930168 0.0523030
0.8981481 0.4944134 0.0524754
0.8981481 0.4958101 0.0527871
0.8981481 0.4972067 0.0530401
0.8981481 0.4986034 0.0530586
0.8981481 0.5000000 0.0531357
0.8981481 0.5013966 0.0532378
0.8981481 0.5027933 0.0533938
0.8981481 0.5041899 0.0535378
0.8981481 0.5055866 0.0535544
0.8981481 0.5069832 0.0535754
0.8981481 0.5083799 0.0536766
0.8981481 0.5097765 0.0537771
0.8981481 0.5111732 0.0540487
0.8981481 0.5125698 0.0544273
0.8981481 0.5139665 0.0547461
0.8981481 0.5153631 0.0551852
0.8981481 0.5167598 0.0554988
0.8981481 0.5181564 0.0558580
0.8981481 0.5195531 0.0561719
0.8981481 0.5209497 0.0562773
0.8981481 0.5223464 0.0564223
0.8981481 0.5237430 0.0565707
0.8981481 0.5251397 0.0568636
0.8981481 0.5265363 0.0571352
0.8981481 0.5279330 0.0573284
0.8981481 0.5293296 0.0575810
0.8981481 0.5307263 0.0578009
0.8981481 0.5321229 0.0579526
0.8981481 0.5335196 0.0584667
0.8981481 0.5349162 0.0589832
0.8981481 0.5363128 0.0591311
0.8981481 0.5377095 0.0593376
0.8981481 0.5391061 0.0594583
0.8981481 0.5405028 0.0594748
0.8981481 0.5418994 0.0596145
0.8981481 0.5432961 0.0599193
0.8981481 0.5446927 0.0601100
0.8981481 0.5460894 0.0601783
0.8981481 0.5474860 0.0602419
0.8981481 0.5488827 0.0602641
0.8981481 0.5502793 0.0605756
0.8981481 0.5516760 0.0609633
0.8981481 0.5530726 0.0610552
0.8981481 0.5544693 0.0611558
0.8981481 0.5558659 0.0613582
0.8981481 0.5572626 0.0616925
0.8981481 0.5586592 0.0620704
0.8981481 0.5600559 0.0622469
0.8981481 0.5614525 0.0622895
0.8981481 0.5628492 0.0625605
0.8981481 0.5642458 0.0629989
0.8981481 0.5656425 0.0637709
0.8888889 0.5656425 0.0644051
0.8888889 0.5670391 0.0644546
0.8888889 0.5684358 0.0645938
0.8888889 0.5698324 0.0647918
0.8796296 0.5698324 0.0650292
0.8796296 0.5712291 0.0654125
0.8796296 0.5726257 0.0656559
0.8796296 0.5740223 0.0659588
0.8796296 0.5754190 0.0667190
0.8796296 0.5768156 0.0672523
0.8796296 0.5782123 0.0674548
0.8796296 0.5796089 0.0676609
0.8796296 0.5810056 0.0681005
0.8796296 0.5824022 0.0684972
0.8796296 0.5837989 0.0686969
0.8796296 0.5851955 0.0688989
0.8796296 0.5865922 0.0689290
0.8796296 0.5879888 0.0689591
0.8796296 0.5893855 0.0693071
0.8796296 0.5907821 0.0698862
0.8796296 0.5921788 0.0701461
0.8796296 0.5935754 0.0704302
0.8796296 0.5949721 0.0709905
0.8796296 0.5963687 0.0712931
0.8796296 0.5977654 0.0713612
0.8703704 0.5977654 0.0715771
0.8703704 0.5991620 0.0721381
0.8703704 0.6005587 0.0726982
0.8703704 0.6019553 0.0730219
0.8703704 0.6033520 0.0734188
0.8703704 0.6047486 0.0737771
0.8703704 0.6061453 0.0742041
0.8703704 0.6075419 0.0745882
0.8703704 0.6089385 0.0747371
0.8703704 0.6103352 0.0748213
0.8703704 0.6117318 0.0752705
0.8703704 0.6131285 0.0757098
0.8703704 0.6145251 0.0758061
0.8703704 0.6159218 0.0759760
0.8703704 0.6173184 0.0767391
0.8703704 0.6187151 0.0774364
0.8703704 0.6201117 0.0777257
0.8703704 0.6215084 0.0780737
0.8611111 0.6215084 0.0781998
0.8611111 0.6229050 0.0783165
0.8611111 0.6243017 0.0786304
0.8611111 0.6256983 0.0790678
0.8518519 0.6256983 0.0795240
0.8518519 0.6270950 0.0800115
0.8518519 0.6284916 0.0807350
0.8518519 0.6298883 0.0817151
0.8518519 0.6312849 0.0823131
0.8518519 0.6326816 0.0827349
0.8518519 0.6340782 0.0831757
0.8518519 0.6354749 0.0834304
0.8518519 0.6368715 0.0836338
0.8518519 0.6382682 0.0837651
0.8518519 0.6396648 0.0838392
0.8518519 0.6410615 0.0840092
0.8518519 0.6424581 0.0842267
0.8518519 0.6438547 0.0843887
0.8518519 0.6452514 0.0845048
0.8518519 0.6466480 0.0849147
0.8518519 0.6480447 0.0855755
0.8518519 0.6494413 0.0859963
0.8518519 0.6508380 0.0864426
0.8518519 0.6522346 0.0867992
0.8518519 0.6536313 0.0870269
0.8518519 0.6550279 0.0874078
0.8518519 0.6564246 0.0878200
0.8518519 0.6578212 0.0881736
0.8518519 0.6592179 0.0886035
0.8518519 0.6606145 0.0889381
0.8518519 0.6620112 0.0893219
0.8518519 0.6634078 0.0897423
0.8518519 0.6648045 0.0899987
0.8518519 0.6662011 0.0903829
0.8518519 0.6675978 0.0906564
0.8518519 0.6689944 0.0911025
0.8425926 0.6689944 0.0915099
0.8425926 0.6703911 0.0916834
0.8425926 0.6717877 0.0918698
0.8425926 0.6731844 0.0920022
0.8425926 0.6745810 0.0922559
0.8425926 0.6759777 0.0924155
0.8425926 0.6773743 0.0926469
0.8333333 0.6773743 0.0930024
0.8333333 0.6787709 0.0933769
0.8333333 0.6801676 0.0941264
0.8333333 0.6815642 0.0951330
0.8240741 0.6815642 0.0956321
0.8240741 0.6829609 0.0958227
0.8240741 0.6843575 0.0961760
0.8240741 0.6857542 0.0963730
0.8240741 0.6871508 0.0966859
0.8240741 0.6885475 0.0972537
0.8240741 0.6899441 0.0976439
0.8240741 0.6913408 0.0980512
0.8240741 0.6927374 0.0983727
0.8148148 0.6927374 0.0984496
0.8148148 0.6941341 0.0988134
0.8148148 0.6955307 0.0993056
0.8148148 0.6969274 0.0996812
0.8148148 0.6983240 0.1000472
0.8148148 0.6997207 0.1003295
0.8055556 0.6997207 0.1005155
0.8055556 0.7011173 0.1012613
0.8055556 0.7025140 0.1019996
0.8055556 0.7039106 0.1022507
0.8055556 0.7053073 0.1024426
0.8055556 0.7067039 0.1026384
0.8055556 0.7081006 0.1031565
0.8055556 0.7094972 0.1036006
0.8055556 0.7108939 0.1037059
0.8055556 0.7122905 0.1039492
0.8055556 0.7136872 0.1043826
0.8055556 0.7150838 0.1047677
0.8055556 0.7164804 0.1058233
0.8055556 0.7178771 0.1073347
0.8055556 0.7192737 0.1080565
0.8055556 0.7206704 0.1081484
0.8055556 0.7220670 0.1085132
0.8055556 0.7234637 0.1092143
0.8055556 0.7248603 0.1098504
0.8055556 0.7262570 0.1105074
0.8055556 0.7276536 0.1110878
0.7962963 0.7276536 0.1113432
0.7962963 0.7290503 0.1113972
0.7962963 0.7304469 0.1120640
0.7962963 0.7318436 0.1131333
0.7962963 0.7332402 0.1139529
0.7962963 0.7346369 0.1148997
0.7962963 0.7360335 0.1155458
0.7962963 0.7374302 0.1156736
0.7962963 0.7388268 0.1162170
0.7962963 0.7402235 0.1169363
0.7962963 0.7416201 0.1171958
0.7962963 0.7430168 0.1174806
0.7962963 0.7444134 0.1180308
0.7962963 0.7458101 0.1186289
0.7962963 0.7472067 0.1190477
0.7962963 0.7486034 0.1193953
0.7870370 0.7486034 0.1197182
0.7777778 0.7486034 0.1200640
0.7777778 0.7500000 0.1204031
0.7777778 0.7513966 0.1207565
0.7777778 0.7527933 0.1211330
0.7777778 0.7541899 0.1216793
0.7777778 0.7555866 0.1223469
0.7777778 0.7569832 0.1226264
0.7777778 0.7583799 0.1226747
0.7777778 0.7597765 0.1227955
0.7777778 0.7611732 0.1229304
0.7777778 0.7625698 0.1236604
0.7777778 0.7639665 0.1249702
0.7777778 0.7653631 0.1257359
0.7685185 0.7653631 0.1259257
0.7685185 0.7667598 0.1269547
0.7685185 0.7681564 0.1284756
0.7685185 0.7695531 0.1295533
0.7685185 0.7709497 0.1305428
0.7685185 0.7723464 0.1312286
0.7685185 0.7737430 0.1321440
0.7685185 0.7751397 0.1330715
0.7592593 0.7751397 0.1333333
0.7592593 0.7765363 0.1335135
0.7592593 0.7779330 0.1339985
0.7592593 0.7793296 0.1344980
0.7592593 0.7807263 0.1348710
0.7592593 0.7821229 0.1359648
0.7592593 0.7835196 0.1369764
0.7500000 0.7835196 0.1373461
0.7500000 0.7849162 0.1378303
0.7500000 0.7863128 0.1383501
0.7500000 0.7877095 0.1388764
0.7500000 0.7891061 0.1393606
0.7500000 0.7905028 0.1398287
0.7407407 0.7905028 0.1414091
0.7407407 0.7918994 0.1432855
0.7407407 0.7932961 0.1443093
0.7407407 0.7946927 0.1453580
0.7407407 0.7960894 0.1460544
0.7407407 0.7974860 0.1467358
0.7407407 0.7988827 0.1474945
0.7407407 0.8002793 0.1477246
0.7407407 0.8016760 0.1478818
0.7407407 0.8030726 0.1482541
0.7407407 0.8044693 0.1487398
0.7407407 0.8058659 0.1491274
0.7314815 0.8058659 0.1495682
0.7314815 0.8072626 0.1501868
0.7314815 0.8086592 0.1505534
0.7222222 0.8086592 0.1507106
0.7222222 0.8100559 0.1508983
0.7222222 0.8114525 0.1510487
0.7222222 0.8128492 0.1512207
0.7222222 0.8142458 0.1515890
0.7222222 0.8156425 0.1520018
0.7222222 0.8170391 0.1522057
0.7222222 0.8184358 0.1523578
0.7222222 0.8198324 0.1525609
0.7222222 0.8212291 0.1527824
0.7222222 0.8226257 0.1535215
0.7222222 0.8240223 0.1546267
0.7129630 0.8240223 0.1554551
0.7129630 0.8254190 0.1559101
0.7129630 0.8268156 0.1564317
0.7129630 0.8282123 0.1570936
0.7129630 0.8296089 0.1575436
0.7129630 0.8310056 0.1578065
0.7129630 0.8324022 0.1583106
0.7037037 0.8324022 0.1590759
0.7037037 0.8337989 0.1595510
0.7037037 0.8351955 0.1598798
0.7037037 0.8365922 0.1606860
0.6944444 0.8365922 0.1614407
0.6944444 0.8379888 0.1616768
0.6944444 0.8393855 0.1620059
0.6944444 0.8407821 0.1623629
0.6944444 0.8421788 0.1632283
0.6944444 0.8435754 0.1639838
0.6944444 0.8449721 0.1640667
0.6851852 0.8449721 0.1643634
0.6851852 0.8463687 0.1661067
0.6851852 0.8477654 0.1679790
0.6851852 0.8491620 0.1689600
0.6851852 0.8505587 0.1709087
0.6851852 0.8519553 0.1729417
0.6851852 0.8533520 0.1741851
0.6759259 0.8533520 0.1748263
0.6759259 0.8547486 0.1751885
0.6759259 0.8561453 0.1757783
0.6666667 0.8561453 0.1767604
0.6666667 0.8575419 0.1793312
0.6666667 0.8589385 0.1823905
0.6574074 0.8589385 0.1837482
0.6574074 0.8603352 0.1843920
0.6574074 0.8617318 0.1850399
0.6481481 0.8617318 0.1857891
0.6481481 0.8631285 0.1864825
0.6481481 0.8645251 0.1878095
0.6481481 0.8659218 0.1889947
0.6481481 0.8673184 0.1891113
0.6481481 0.8687151 0.1897340
0.6481481 0.8701117 0.1903456
0.6481481 0.8715084 0.1904548
0.6388889 0.8715084 0.1914161
0.6388889 0.8729050 0.1924242
0.6388889 0.8743017 0.1926555
0.6388889 0.8756983 0.1931956
0.6388889 0.8770950 0.1938274
0.6388889 0.8784916 0.1950008
0.6388889 0.8798883 0.1960556
0.6388889 0.8812849 0.1973901
0.6388889 0.8826816 0.1991969
0.6388889 0.8840782 0.1998974
0.6388889 0.8854749 0.2004172
0.6296296 0.8854749 0.2008593
0.6296296 0.8868715 0.2012307
0.6296296 0.8882682 0.2029558
0.6203704 0.8882682 0.2047614
0.6203704 0.8896648 0.2052738
0.6111111 0.8896648 0.2055369
0.6111111 0.8910615 0.2062723
0.6111111 0.8924581 0.2079041
0.6111111 0.8938547 0.2091087
0.6111111 0.8952514 0.2098006
0.6111111 0.8966480 0.2106960
0.6111111 0.8980447 0.2139765
0.6111111 0.8994413 0.2171708
0.6018519 0.8994413 0.2176947
0.5925926 0.8994413 0.2181878
0.5833333 0.8994413 0.2198474
0.5833333 0.9008380 0.2215417
0.5740741 0.9008380 0.2221039
0.5648148 0.9008380 0.2229813
0.5648148 0.9022346 0.2237938
0.5648148 0.9036313 0.2248540
0.5648148 0.9050279 0.2257670
0.5555556 0.9050279 0.2261111
0.5555556 0.9064246 0.2284686
0.5555556 0.9078212 0.2329539
0.5555556 0.9092179 0.2361065
0.5462963 0.9092179 0.2369705
0.5370370 0.9092179 0.2372211
0.5277778 0.9092179 0.2374417
0.5277778 0.9106145 0.2386815
0.5277778 0.9120112 0.2430545
0.5277778 0.9134078 0.2465205
0.5277778 0.9148045 0.2494507
0.5277778 0.9162011 0.2526567
0.5277778 0.9175978 0.2534997
0.5277778 0.9189944 0.2544573
0.5277778 0.9203911 0.2561339
0.5277778 0.9217877 0.2584237
0.5277778 0.9231844 0.2597460
0.5185185 0.9231844 0.2603782
0.5185185 0.9245810 0.2643123
0.5185185 0.9259777 0.2687211
0.5092593 0.9259777 0.2697884
0.5000000 0.9259777 0.2701825
0.5000000 0.9273743 0.2711228
0.5000000 0.9287709 0.2723353
0.5000000 0.9301676 0.2734644
0.5000000 0.9315642 0.2768050
0.5000000 0.9329609 0.2853779
0.4907407 0.9329609 0.2914711
0.4907407 0.9343575 0.2920553
0.4907407 0.9357542 0.2931980
0.4907407 0.9371508 0.2946597
0.4907407 0.9385475 0.2958902
0.4907407 0.9399441 0.2974609
0.4814815 0.9399441 0.2985195
0.4814815 0.9413408 0.3000659
0.4722222 0.9413408 0.3026118
0.4629630 0.9413408 0.3039893
0.4629630 0.9427374 0.3095438
0.4629630 0.9441341 0.3220069
0.4537037 0.9441341 0.3327477
0.4537037 0.9455307 0.3404020
0.4537037 0.9469274 0.3464195
0.4537037 0.9483240 0.3486306
0.4444444 0.9483240 0.3494033
0.4444444 0.9497207 0.3527569
0.4351852 0.9497207 0.3579288
0.4259259 0.9497207 0.3621542
0.4166667 0.9497207 0.3680675
0.4166667 0.9511173 0.3729504
0.4166667 0.9525140 0.3781757
0.4166667 0.9539106 0.3845159
0.4166667 0.9553073 0.3865879
0.4074074 0.9553073 0.3882345
0.4074074 0.9567039 0.3899204
0.3981481 0.9567039 0.3901364
0.3981481 0.9581006 0.3931435
0.3981481 0.9594972 0.3962147
0.3981481 0.9608939 0.3976991
0.3888889 0.9608939 0.4030935
0.3796296 0.9608939 0.4093054
0.3703704 0.9608939 0.4118364
0.3703704 0.9622905 0.4149312
0.3703704 0.9636872 0.4186219
0.3703704 0.9650838 0.4229924
0.3703704 0.9664804 0.4324225
0.3703704 0.9678771 0.4487757
0.3611111 0.9678771 0.4599320
0.3611111 0.9692737 0.4613873
0.3611111 0.9706704 0.4650671
0.3611111 0.9720670 0.4685497
0.3518519 0.9720670 0.4708527
0.3518519 0.9734637 0.4731799
0.3518519 0.9748603 0.4745162
0.3518519 0.9762570 0.4759544
0.3518519 0.9776536 0.4783528
0.3518519 0.9790503 0.4803212
0.3518519 0.9804469 0.4832669
0.3518519 0.9818436 0.4912425
0.3425926 0.9818436 0.5041313
0.3425926 0.9832402 0.5126560
0.3425926 0.9846369 0.5137519
0.3333333 0.9846369 0.5154423
0.3240741 0.9846369 0.5282255
0.3148148 0.9846369 0.5403718
0.3055556 0.9846369 0.5539332
0.2962963 0.9846369 0.5709395
0.2870370 0.9846369 0.5760400
0.2777778 0.9846369 0.5783543
0.2685185 0.9846369 0.5822892
0.2592593 0.9846369 0.5865460
0.2500000 0.9846369 0.5968281
0.2407407 0.9846369 0.6059342
0.2314815 0.9846369 0.6095807
0.2314815 0.9860335 0.6167654
0.2222222 0.9860335 0.6236671
0.2222222 0.9874302 0.6284977
0.2129630 0.9874302 0.6310702
0.2037037 0.9874302 0.6336050
0.2037037 0.9888268 0.6388388
0.2037037 0.9902235 0.6442627
0.1944444 0.9902235 0.6499224
0.1851852 0.9902235 0.6542850
0.1851852 0.9916201 0.6563378
0.1851852 0.9930168 0.6587183
0.1759259 0.9930168 0.6604950
0.1666667 0.9930168 0.6664063
0.1574074 0.9930168 0.6723634
0.1574074 0.9944134 0.6734775
0.1481481 0.9944134 0.6745615
0.1388889 0.9944134 0.6809349
0.1296296 0.9944134 0.6950148
0.1203704 0.9944134 0.7058355
0.1111111 0.9944134 0.7108626
0.1018519 0.9944134 0.7211504
0.0925926 0.9944134 0.7340106
0.0925926 0.9958101 0.7515602
0.0833333 0.9958101 0.7653827
0.0833333 0.9972067 0.7730689
0.0740741 0.9972067 0.8001195
0.0740741 0.9986034 0.8299471
0.0740741 1.0000000 0.8430824
0.0648148 1.0000000 0.8578174
0.0555556 1.0000000 0.8695353
0.0462963 1.0000000 0.8725204
0.0370370 1.0000000 0.8853640
0.0277778 1.0000000 0.9047274
0.0185185 1.0000000 0.9370472
0.0092593 1.0000000 0.9676218
0.0000000 1.0000000 Inf
# Elegir punto de corte
cutoff_val <- 0.2
idx <- which.min(abs(CUTOFF - cutoff_val))

cat("Sensibilidad (ROC):", round(SENS[idx], 3), "\n")
## Sensibilidad (ROC): 0.639
cat("Especificidad (ROC):", round(ESPEC[idx], 3), "\n")
## Especificidad (ROC): 0.884
# Matriz de confusión manual
mod_pred0 <- mod_pred %>%
  mutate(prediccion = ifelse(pred >= cutoff_val, 1, 0))

conf_mat <- table(Predicted = mod_pred0$prediccion, Actual = mod_pred0$obs)

# Extraer valores de la matriz
TP <- conf_mat["1", "1"]
FP <- conf_mat["1", "0"]
TN <- conf_mat["0", "0"]
FN <- conf_mat["0", "1"]

# Métricas manuales
sens_manual <- TP / (TP + FN)
spec_manual <- TN / (TN + FP)
vpp <- TP / (TP + FP)  # Valor Predictivo Positivo
vpn <- TN / (TN + FN)  # Valor Predictivo Negativo

# Mostrar resultados
cat("Sensibilidad (manual):", round(sens_manual, 3), "\n")
## Sensibilidad (manual): 0.639
cat("Especificidad (manual):", round(spec_manual, 3), "\n")
## Especificidad (manual): 0.884
cat("Valor Predictivo Positivo (VPP):", round(vpp, 3), "\n")
## Valor Predictivo Positivo (VPP): 0.454
cat("Valor Predictivo Negativo (VPN):", round(vpn, 3), "\n")
## Valor Predictivo Negativo (VPN): 0.942

Base de datos prueba

df_test= read_csv("C:/Users/Jao/Documents/Doc/Cursos/Maestria Efectividad Clinica/2/4 Logistica/TH/test_TH.csv")
## Rows: 352 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (36): Died, age, gender, hypertension, a_fib, CHD, diabetes, anemia, dep...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Convertir nombres de columnas a minúscula
names(df_test) <- tolower(names(df_test))

# Aplicar la función
names(df_test) <- tolower(names(df_test))
names(df_test) <- gsub(" ", "_", names(df_test))

# Convertir 'gender' en factor binario
df_test <- df_test %>%
  mutate(gender = as.factor(ifelse(gender == "1", 1, 0)))

# Columnas a invertir
cols_to_invert <- c("hypertension", "diabetes", "anemia", "depression", "hyperlipemia", "ckd", "copd")

# Invertir valores (corrigiendo el error de x_num)
df_test[cols_to_invert] <- lapply(df_test[cols_to_invert], function(x) {
  x_num <- as.numeric(as.character(x))  # CORRECCIÓN: convertir x a número primero
  x_inv <- ifelse(x_num == 1, 0, 1)
  return(x_inv)
})

# Convertir a factor las variables binarias
df_test <- df_test %>%
  mutate(across(c(hypertension, a_fib, chd, diabetes, anemia, depression, hyperlipemia, ckd, copd), as.factor))


# Cambiar nombre nt-probnp
names(df_test)[which(names(df_test) == "nt-probnp")] = "nt_probnp"


# Total de NA en todo el dataframe
total_na <- sum(is.na(df_test))
cat("Total de valores NA en el dataframe:", total_na, "\n\n")
## Total de valores NA en el dataframe: 0
# NA por variable
na_por_variable <- colSums(is.na(df_test))
print(na_por_variable)
##                     died                      age                   gender 
##                        0                        0                        0 
##             hypertension                    a_fib                      chd 
##                        0                        0                        0 
##                 diabetes                   anemia               depression 
##                        0                        0                        0 
##             hyperlipemia                      ckd                     copd 
##                        0                        0                        0 
##               heart_rate  systolic_blood_pressure diastolic_blood_pressure 
##                        0                        0                        0 
##         respiratory_rate              temperature                    sp_o2 
##                        0                        0                        0 
##             urine_output               hematocrit                leucocyte 
##                        0                        0                        0 
##                platelets                      inr                nt_probnp 
##                        0                        0                        0 
##               creatinine            urea_nitrogen                  glucose 
##                        0                        0                        0 
##          blood_potassium             blood_sodium            blood_calcium 
##                        0                        0                        0 
##                 chloride            magnesium_ion                       ph 
##                        0                        0                        0 
##              bicarbonate              lactic_acid                     pco2 
##                        0                        0                        0
tabla_1_testeo = CreateTableOne(vars = colnames(df_test), strata = "died", data = df_test)
kableone(tabla_1_testeo)
0 1 p test
n 301 51
died (mean (SD)) 0.00 (0.00) 1.00 (0.00) <0.001
age (mean (SD)) 73.58 (14.46) 76.78 (11.48) 0.133
gender = 1 (%) 144 (47.8) 24 (47.1) 1.000
hypertension = 1 (%) 79 (26.2) 17 (33.3) 0.378
a_fib = 1 (%) 126 (41.9) 28 (54.9) 0.113
chd = 1 (%) 23 ( 7.6) 2 ( 3.9) 0.508
diabetes = 1 (%) 173 (57.5) 36 (70.6) 0.108
anemia = 1 (%) 203 (67.4) 38 (74.5) 0.400
depression = 1 (%) 262 (87.0) 48 (94.1) 0.227
hyperlipemia = 1 (%) 182 (60.5) 40 (78.4) 0.021
ckd = 1 (%) 185 (61.5) 38 (74.5) 0.103
copd = 1 (%) 283 (94.0) 47 (92.2) 0.845
heart_rate (mean (SD)) 83.26 (16.25) 93.22 (15.88) <0.001
systolic_blood_pressure (mean (SD)) 118.26 (17.71) 115.60 (15.96) 0.316
diastolic_blood_pressure (mean (SD)) 59.47 (10.97) 56.01 (8.07) 0.032
respiratory_rate (mean (SD)) 20.77 (3.83) 22.67 (4.67) 0.002
temperature (mean (SD)) 36.70 (0.63) 36.64 (0.63) 0.532
sp_o2 (mean (SD)) 96.36 (2.12) 95.75 (3.29) 0.085
urine_output (mean (SD)) 1904.89 (1233.09) 1502.63 (1296.06) 0.033
hematocrit (mean (SD)) 32.02 (5.40) 31.11 (4.91) 0.258
leucocyte (mean (SD)) 10.08 (4.98) 12.68 (6.13) 0.001
platelets (mean (SD)) 237.75 (114.09) 220.83 (122.38) 0.333
inr (mean (SD)) 1.57 (0.68) 1.75 (0.92) 0.098
nt_probnp (mean (SD)) 9352.68 (11483.52) 13942.09 (19531.85) 0.020
creatinine (mean (SD)) 1.67 (1.50) 1.68 (0.91) 0.953
urea_nitrogen (mean (SD)) 33.84 (18.70) 44.99 (26.39) <0.001
glucose (mean (SD)) 146.73 (49.35) 157.40 (65.83) 0.176
blood_potassium (mean (SD)) 4.17 (0.40) 4.27 (0.41) 0.077
blood_sodium (mean (SD)) 138.96 (4.02) 138.24 (4.28) 0.239
blood_calcium (mean (SD)) 8.54 (0.56) 8.25 (0.60) 0.001
chloride (mean (SD)) 102.27 (5.05) 102.54 (5.95) 0.733
magnesium_ion (mean (SD)) 2.14 (0.25) 2.15 (0.23) 0.773
ph (mean (SD)) 7.38 (0.06) 7.36 (0.07) 0.007
bicarbonate (mean (SD)) 27.26 (4.92) 25.11 (5.51) 0.005
lactic_acid (mean (SD)) 1.76 (0.89) 2.36 (1.45) <0.001
pco2 (mean (SD)) 45.51 (11.63) 45.22 (12.31) 0.867

Mostrar ambas tablas 1 comparativas si son dos poblaciones diferentes

library(tableone)
library(kableExtra)

# 1. Variables a usar (excluimos la de estratificación)
vars <- setdiff(colnames(df_train), "died")

# 2. Crear tabla para df_train
tabla_train <- CreateTableOne(vars = vars, strata = "died", data = df_train, test = TRUE)
tabla_train_mat <- print(tabla_train, printToggle = FALSE, noSpaces = TRUE)

# 3. Crear tabla para df_test
tabla_test <- CreateTableOne(vars = vars, strata = "died", data = df_test, test = TRUE)
## Warning in ModuleReturnVarsExist(vars, data): The data frame does not have:
## died_num logit bicarbonate_q pco2_q age_group anemia_copd Dropped
tabla_test_mat <- print(tabla_test, printToggle = FALSE, noSpaces = TRUE)

# 4. Combinar las tablas (usamos solo columnas "0", "1", y "p")
tabla_combinada <- cbind(
  "Train_0" = tabla_train_mat[, "0"],
  "Train_1" = tabla_train_mat[, "1"],
  "Train_p" = tabla_train_mat[, "p"],
  "Test_0"  = tabla_test_mat[, "0"],
  "Test_1"  = tabla_test_mat[, "1"],
  "Test_p"  = tabla_test_mat[, "p"]
)
## Warning in cbind(Train_0 = tabla_train_mat[, "0"], Train_1 = tabla_train_mat[,
## : number of rows of result is not a multiple of vector length (arg 4)
# 5. Mostrar
kable(tabla_combinada, caption = "Tabla 1 - Comparación de características entre df_train y df_test (con p-valores)") %>%
  kable_styling(full_width = FALSE, bootstrap_options = c("striped", "hover"))
Tabla 1 - Comparación de características entre df_train y df_test (con p-valores)
Train_0 Train_1 Train_p Test_0 Test_1 Test_p
n 716 108 301 51
age (mean (SD)) 73.76 (13.01) 75.98 (14.01) 0.102 73.58 (14.46) 76.78 (11.48) 0.133
gender = 1 (%) 334 (46.6) 56 (51.9) 0.365 144 (47.8) 24 (47.1) 1.000
hypertension = 1 (%) 195 (27.2) 41 (38.0) 0.029 79 (26.2) 17 (33.3) 0.378
a_fib = 1 (%) 313 (43.7) 64 (59.3) 0.004 126 (41.9) 28 (54.9) 0.113
chd = 1 (%) 66 (9.2) 10 (9.3) 1.000 23 (7.6) 2 (3.9) 0.508
diabetes = 1 (%) 406 (56.7) 66 (61.1) 0.448 173 (57.5) 36 (70.6) 0.108
anemia = 1 (%) 450 (62.8) 86 (79.6) 0.001 203 (67.4) 38 (74.5) 0.400
depression = 1 (%) 626 (87.4) 100 (92.6) 0.166 262 (87.0) 48 (94.1) 0.227
hyperlipemia = 1 (%) 438 (61.2) 69 (63.9) 0.664 182 (60.5) 40 (78.4) 0.021
ckd = 1 (%) 440 (61.5) 84 (77.8) 0.001 185 (61.5) 38 (74.5) 0.103
copd = 1 (%) 652 (91.1) 105 (97.2) 0.046 283 (94.0) 47 (92.2) 0.845
heart_rate (mean (SD)) 83.80 (15.83) 88.24 (14.68) 0.006 83.26 (16.25) 93.22 (15.88) <0.001
systolic_blood_pressure (mean (SD)) 119.21 (17.05) 110.39 (16.75) <0.001 118.26 (17.71) 115.60 (15.96) 0.316
diastolic_blood_pressure (mean (SD)) 60.00 (10.84) 57.67 (9.21) 0.034 59.47 (10.97) 56.01 (8.07) 0.032
respiratory_rate (mean (SD)) 20.51 (3.93) 21.68 (4.27) 0.005 20.77 (3.83) 22.67 (4.67) 0.002
temperature (mean (SD)) 36.70 (0.57) 36.48 (0.73) <0.001 36.70 (0.63) 36.64 (0.63) 0.532
sp_o2 (mean (SD)) 96.35 (2.14) 95.90 (3.01) 0.054 96.36 (2.12) 95.75 (3.29) 0.085
urine_output (mean (SD)) 1999.65 (1271.35) 1251.19 (1042.46) <0.001 1904.89 (1233.09) 1502.63 (1296.06) 0.033
hematocrit (mean (SD)) 31.91 (5.11) 31.97 (5.43) 0.913 32.02 (5.40) 31.11 (4.91) 0.258
leucocyte (mean (SD)) 10.37 (4.52) 13.84 (7.96) <0.001 10.08 (4.98) 12.68 (6.13) 0.001
platelets (mean (SD)) 248.72 (108.03) 214.03 (133.44) 0.003 237.75 (114.09) 220.83 (122.38) 0.333
inr (mean (SD)) 1.58 (0.75) 2.02 (1.40) <0.001 1.57 (0.68) 1.75 (0.92) 0.098
nt_probnp (mean (SD)) 10814.14 (12818.97) 15554.25 (14867.09) <0.001 9352.68 (11483.52) 13942.09 (19531.85) 0.020
creatinine (mean (SD)) 1.60 (1.21) 1.84 (1.20) 0.046 1.67 (1.50) 1.68 (0.91) 0.953
urea_nitrogen (mean (SD)) 34.83 (20.81) 48.71 (28.71) <0.001 33.84 (18.70) 44.99 (26.39) <0.001
glucose (mean (SD)) 148.52 (50.57) 151.15 (52.98) 0.617 146.73 (49.35) 157.40 (65.83) 0.176
blood_potassium (mean (SD)) 4.15 (0.38) 4.34 (0.58) <0.001 4.17 (0.40) 4.27 (0.41) 0.077
blood_sodium (mean (SD)) 139.02 (3.97) 138.18 (5.35) 0.052 138.96 (4.02) 138.24 (4.28) 0.239
blood_calcium (mean (SD)) 8.54 (0.55) 8.23 (0.63) <0.001 8.54 (0.56) 8.25 (0.60) 0.001
chloride (mean (SD)) 102.12 (5.28) 103.36 (6.04) 0.026 102.27 (5.05) 102.54 (5.95) 0.733
magnesium_ion (mean (SD)) 2.10 (0.24) 2.18 (0.31) 0.004 2.14 (0.25) 2.15 (0.23) 0.773
ph (mean (SD)) 7.38 (0.06) 7.36 (0.08) <0.001 7.38 (0.06) 7.36 (0.07) 0.007
bicarbonate (mean (SD)) 27.41 (5.01) 23.48 (5.32) <0.001 27.26 (4.92) 25.11 (5.51) 0.005
lactic_acid (mean (SD)) 1.71 (0.75) 2.40 (1.36) <0.001 1.76 (0.89) 2.36 (1.45) <0.001
pco2 (mean (SD)) 45.49 (11.54) 43.16 (12.22) 0.053 45.51 (11.63) 45.22 (12.31) 0.867
died_num (mean (SD)) 0.00 (0.00) 1.00 (0.00) <0.001 301 51
logit (mean (SD)) -2.19 (0.73) -1.57 (0.80) <0.001 73.58 (14.46) 76.78 (11.48) 0.133
bicarbonate_q (mean (SD)) 3.13 (1.38) 2.10 (1.33) <0.001 144 (47.8) 24 (47.1) 1.000
pco2_q (mean (SD)) 3.05 (1.41) 2.65 (1.42) 0.006 79 (26.2) 17 (33.3) 0.378
age_group (%) 0.031 126 (41.9) 28 (54.9) 0.113
[19,55] 71 (9.9) 16 (14.8) 23 (7.6) 2 (3.9) 0.508
(55,63] 84 (11.7) 3 (2.8) 173 (57.5) 36 (70.6) 0.108
(63,67] 69 (9.6) 5 (4.6) 203 (67.4) 38 (74.5) 0.400
(67,72] 74 (10.3) 10 (9.3) 262 (87.0) 48 (94.1) 0.227
(72,77] 79 (11.0) 11 (10.2) 182 (60.5) 40 (78.4) 0.021
(77,80] 65 (9.1) 14 (13.0) 185 (61.5) 38 (74.5) 0.103
(80,83] 68 (9.5) 9 (8.3) 283 (94.0) 47 (92.2) 0.845
(83,86] 76 (10.6) 13 (12.0) 83.26 (16.25) 93.22 (15.88) <0.001
(86,89] 114 (15.9) 21 (19.4) 118.26 (17.71) 115.60 (15.96) 0.316
(89,99] 16 (2.2) 6 (5.6) 59.47 (10.97) 56.01 (8.07) 0.032
anemia_copd (%) <0.001 20.77 (3.83) 22.67 (4.67) 0.002
0_0 20 (2.8) 1 (0.9) 36.70 (0.63) 36.64 (0.63) 0.532
1_0 44 (6.1) 2 (1.9) 96.36 (2.12) 95.75 (3.29) 0.085
0_1 246 (34.4) 21 (19.4) 1904.89 (1233.09) 1502.63 (1296.06) 0.033
1_1 406 (56.7) 84 (77.8) 32.02 (5.40) 31.11 (4.91) 0.258
library(tableone)
library(kableExtra)

# 1. Agregamos columna que identifique grupo a cada base
df_train$grupo <- "Entrenamiento"
df_test$grupo <- "Testeo"

# 2. Unimos las dos bases (solo variables comunes)
vars <- intersect(colnames(df_train), colnames(df_test))
vars <- setdiff(vars, "grupo")  # Excluimos variable grupo para comparar

df_all <- rbind(
  df_train[, c(vars, "grupo")],
  df_test[, c(vars, "grupo")]
)

# 3. Creamos Tabla 1 comparando por "grupo"
tabla_grupos <- CreateTableOne(vars = vars, strata = "grupo", data = df_all, test = TRUE)

# 4. Imprimimos la tabla (con p-valor incluido)
tabla_grupos_mat <- print(tabla_grupos, printToggle = FALSE, noSpaces = TRUE)

# 5. Mostrar con kable
kable(tabla_grupos_mat, caption = "Comparación de características entre df_train y df_test") %>%
  kable_styling(full_width = FALSE, bootstrap_options = c("striped", "hover"))
Comparación de características entre df_train y df_test
Entrenamiento Testeo p test
n 824 352
died (mean (SD)) 0.13 (0.34) 0.14 (0.35) 0.526
age (mean (SD)) 74.05 (13.15) 74.04 (14.10) 0.993
gender = 1 (%) 390 (47.3) 168 (47.7) 0.951
hypertension = 1 (%) 236 (28.6) 96 (27.3) 0.684
a_fib = 1 (%) 377 (45.8) 154 (43.8) 0.570
chd = 1 (%) 76 (9.2) 25 (7.1) 0.282
diabetes = 1 (%) 472 (57.3) 209 (59.4) 0.548
anemia = 1 (%) 536 (65.0) 241 (68.5) 0.286
depression = 1 (%) 726 (88.1) 310 (88.1) 1.000
hyperlipemia = 1 (%) 507 (61.5) 222 (63.1) 0.665
ckd = 1 (%) 524 (63.6) 223 (63.4) 0.990
copd = 1 (%) 757 (91.9) 330 (93.8) 0.319
heart_rate (mean (SD)) 84.38 (15.75) 84.71 (16.55) 0.748
systolic_blood_pressure (mean (SD)) 118.05 (17.26) 117.87 (17.47) 0.872
diastolic_blood_pressure (mean (SD)) 59.69 (10.66) 58.97 (10.66) 0.287
respiratory_rate (mean (SD)) 20.66 (3.99) 21.05 (4.01) 0.131
temperature (mean (SD)) 36.67 (0.60) 36.69 (0.63) 0.636
sp_o2 (mean (SD)) 96.29 (2.28) 96.27 (2.33) 0.868
urine_output (mean (SD)) 1901.55 (1268.62) 1846.61 (1248.59) 0.494
hematocrit (mean (SD)) 31.92 (5.15) 31.89 (5.34) 0.926
leucocyte (mean (SD)) 10.82 (5.23) 10.46 (5.23) 0.274
platelets (mean (SD)) 244.18 (112.21) 235.30 (115.30) 0.218
inr (mean (SD)) 1.64 (0.87) 1.59 (0.72) 0.425
nt_probnp (mean (SD)) 11435.41 (13193.21) 10017.62 (13025.79) 0.091
creatinine (mean (SD)) 1.63 (1.21) 1.67 (1.43) 0.602
urea_nitrogen (mean (SD)) 36.65 (22.48) 35.46 (20.34) 0.392
glucose (mean (SD)) 148.87 (50.87) 148.28 (52.09) 0.856
blood_potassium (mean (SD)) 4.17 (0.42) 4.18 (0.40) 0.804
blood_sodium (mean (SD)) 138.91 (4.19) 138.86 (4.06) 0.833
blood_calcium (mean (SD)) 8.50 (0.57) 8.50 (0.58) 0.992
chloride (mean (SD)) 102.28 (5.40) 102.31 (5.18) 0.941
magnesium_ion (mean (SD)) 2.11 (0.25) 2.14 (0.25) 0.119
ph (mean (SD)) 7.38 (0.06) 7.38 (0.06) 0.506
bicarbonate (mean (SD)) 26.90 (5.22) 26.94 (5.06) 0.889
lactic_acid (mean (SD)) 1.80 (0.88) 1.85 (1.01) 0.400
pco2 (mean (SD)) 45.18 (11.65) 45.47 (11.71) 0.698

Testeo

mod_pred_test = as.data.frame(predict(mod_final_int, newdata = df_test, type = "response")) %>% rename(pred=`predict(mod_final_int, newdata = df_test, type = "response")`)
mod_pred_test$obs <- df_test$died

**H*L**

library(ResourceSelection)
## ResourceSelection 0.3-6   2023-06-27
library(dplyr)
library(ggplot2)

# 1. Predecir sobre df_test
df_test$pred <- predict(mod_final_int, newdata = df_test, type = "response")

# 2. Asegurarse de que outcome esté bien codificado (0 y 1, no factor)
# Si es factor, convertirlo
df_test$obs <- as.numeric(as.character(df_test$died))

# 3. Test de Hosmer-Lemeshow (usando `hoslem.test`)
hl_test <- hoslem.test(df_test$obs, df_test$pred, g = 5)

# 4. Mostrar resultados
print(hl_test)
## 
##  Hosmer and Lemeshow goodness of fit (GOF) test
## 
## data:  df_test$obs, df_test$pred
## X-squared = 22.896, df = 3, p-value = 4.244e-05

Slope & Intercept

library(CalibrationCurves)
s_i = valProbggplot(mod_pred_test$pred,mod_pred_test$obs)
s_i$ggPlot

Curva ROC

library(pROC)
roc_test<-roc(mod_pred_test$obs, mod_pred_test$pred)
## Setting levels: control = 0, case = 1
## Setting direction: controls < cases
plot(roc_test, main = "Curva ROC", col = "red", lwd = 3)

text(0.8, 0.2, paste("Area bajo la Curva ROC testeo =", round(roc_test$auc, 3)), adj = c(0, 1))

Comparar coeficientes en train y test si estan contenidos en los intervalos de confianza

library(kableExtra)

modelo_test <- glm(formula(mod_final_int), data = df_test, family = binomial)

# Coeficientes e IC del modelo entrenado
coef_train <- coef(mod_final_int)
ic_train <- confint(mod_final_int)
## Waiting for profiling to be done...
# Coeficientes del modelo test
coef_test <- coef(modelo_test)

# Lógica para marcar FALSE en rojo
dentro_ic95 <- coef_test >= ic_train[, 1] & coef_test <= ic_train[, 2]
resaltado <- ifelse(dentro_ic95, 
                    "TRUE", 
                    cell_spec("FALSE", color = "red", bold = TRUE))

# Armar tabla de comparación
comparacion_coef <- data.frame(
  `B (Train)` = round(coef_train, 3),
  `IC95` = paste0(round(ic_train[, 1], 3), " – ", round(ic_train[, 2], 3)),
  `B (Test)` = round(coef_test, 3),
  `Test dentro del IC95 del Train` = resaltado,
  check.names = FALSE
)

# Mostrar tabla con formato
comparacion_coef %>%
  kbl(caption = "Comparación de coeficientes entre los modelos ajustados en las muestras de entrenamiento y validación", align = "c", escape = FALSE) %>%
  kable_styling(full_width = FALSE, position = "center")
Comparación de coeficientes entre los modelos ajustados en las muestras de entrenamiento y validación
B (Train) IC95 B (Test) Test dentro del IC95 del Train
(Intercept) 3.842 -1.03 – 8.512 2.074 TRUE
bicarbonate -0.080 -0.136 – -0.026 -0.019 FALSE
urine_output 0.000 -0.001 – 0 0.000 TRUE
leucocyte 0.056 0.016 – 0.097 0.049 TRUE
urea_nitrogen 0.032 0.021 – 0.045 0.026 TRUE
blood_calcium -0.633 -1.101 – -0.186 -0.755 TRUE
systolic_blood_pressure -0.020 -0.037 – -0.005 0.001 FALSE
anemia1 -0.633 -3.18 – 2.544 -0.125 TRUE
copd1 0.103 -1.645 – 3.049 -0.589 TRUE
ckd1 1.658 1.032 – 2.336 0.957 FALSE
age 0.007 -0.011 – 0.026 0.017 TRUE
gender1 0.063 -0.436 – 0.56 0.141 TRUE
anemia1:copd1 1.665 -1.553 – 4.277 0.560 TRUE

Optimismo

library(pROC)

library(pROC)

# Curvas ROC para entrenamiento y testeo
roc_train <- roc(mod_pred$obs, mod_pred$pred, direction = "<")
## Setting levels: control = 0, case = 1
roc_test  <- roc(mod_pred_test$obs, mod_pred_test$pred, direction = "<")
## Setting levels: control = 0, case = 1
# Graficar curva de entrenamiento (azul)
plot(roc_train, col = "blue", lwd = 2, main = "Optimismo: curvas ROC Entrenamiento vs Testeo")

# Agregar curva de testeo (rojo)
lines(roc_test, col = "red", lwd = 2)

# AUCs
auc_train <- auc(roc_train)
auc_test <- auc(roc_test)

# Leyenda
legend("bottomright",
       legend = c(
         paste("Entrenamiento (AUC =", round(auc_train, 3), ")"),
         paste("Testeo (AUC =", round(auc_test, 3), ")")
       ),
       col = c("blue", "red"), lwd = 2)

# Línea de no discriminación
abline(a = 0, b = 1, lty = 2, col = "gray")

library(pROC)
library(dplyr)
library(knitr)

# Curva ROC
roc_obj_test <- roc(mod_pred_test$obs, mod_pred_test$pred, levels = c(0, 1), direction = "<")  # corregí el level de 2 a 1 si tu evento positivo es 1
SENS  <- roc_obj_test$sensitivities
ESPEC <- roc_obj_test$specificities
CUTOFF <- roc_obj_test$thresholds

# Tabla con sensibilidad, especificidad y umbrales
d <- cbind(SENS, ESPEC, CUTOFF)
kable(d)
SENS ESPEC CUTOFF
1.0000000 0.0000000 -Inf
1.0000000 0.0033223 0.0006697
1.0000000 0.0066445 0.0018987
1.0000000 0.0099668 0.0028935
1.0000000 0.0132890 0.0031013
1.0000000 0.0166113 0.0031874
1.0000000 0.0199336 0.0033970
1.0000000 0.0232558 0.0036075
1.0000000 0.0265781 0.0037498
1.0000000 0.0299003 0.0039210
1.0000000 0.0332226 0.0040264
1.0000000 0.0365449 0.0041749
1.0000000 0.0398671 0.0044154
1.0000000 0.0431894 0.0045711
1.0000000 0.0465116 0.0047214
1.0000000 0.0498339 0.0048808
1.0000000 0.0531561 0.0050086
1.0000000 0.0564784 0.0051314
1.0000000 0.0598007 0.0051758
1.0000000 0.0631229 0.0057633
1.0000000 0.0664452 0.0065346
1.0000000 0.0697674 0.0070989
1.0000000 0.0730897 0.0074619
1.0000000 0.0764120 0.0075238
1.0000000 0.0797342 0.0077484
1.0000000 0.0830565 0.0080099
1.0000000 0.0863787 0.0082844
1.0000000 0.0897010 0.0086369
1.0000000 0.0930233 0.0089252
0.9803922 0.0930233 0.0090638
0.9803922 0.0963455 0.0091452
0.9803922 0.0996678 0.0092618
0.9607843 0.0996678 0.0093518
0.9607843 0.1029900 0.0094036
0.9607843 0.1063123 0.0094187
0.9607843 0.1096346 0.0094678
0.9607843 0.1129568 0.0096124
0.9607843 0.1162791 0.0098755
0.9607843 0.1196013 0.0100521
0.9607843 0.1229236 0.0101989
0.9607843 0.1262458 0.0104247
0.9607843 0.1295681 0.0106167
0.9607843 0.1328904 0.0108384
0.9607843 0.1362126 0.0110156
0.9607843 0.1395349 0.0111047
0.9607843 0.1428571 0.0111783
0.9607843 0.1461794 0.0113127
0.9607843 0.1495017 0.0114745
0.9607843 0.1528239 0.0116486
0.9607843 0.1561462 0.0118090
0.9607843 0.1594684 0.0118783
0.9607843 0.1627907 0.0119661
0.9607843 0.1661130 0.0121400
0.9607843 0.1694352 0.0122621
0.9607843 0.1727575 0.0123260
0.9607843 0.1760797 0.0125270
0.9607843 0.1794020 0.0127382
0.9607843 0.1827243 0.0133523
0.9607843 0.1860465 0.0139353
0.9607843 0.1893688 0.0140673
0.9411765 0.1893688 0.0142367
0.9411765 0.1926910 0.0145978
0.9411765 0.1960133 0.0152327
0.9411765 0.1993355 0.0156976
0.9411765 0.2026578 0.0159061
0.9411765 0.2059801 0.0161066
0.9411765 0.2093023 0.0162832
0.9411765 0.2126246 0.0164320
0.9411765 0.2159468 0.0167337
0.9411765 0.2192691 0.0171230
0.9411765 0.2225914 0.0174062
0.9411765 0.2259136 0.0177431
0.9411765 0.2292359 0.0181363
0.9411765 0.2325581 0.0183148
0.9411765 0.2358804 0.0187626
0.9411765 0.2392027 0.0195061
0.9411765 0.2425249 0.0199493
0.9411765 0.2458472 0.0208036
0.9215686 0.2458472 0.0217176
0.9215686 0.2491694 0.0221636
0.9215686 0.2524917 0.0224869
0.9215686 0.2558140 0.0225598
0.9215686 0.2591362 0.0229440
0.9215686 0.2624585 0.0235276
0.9215686 0.2657807 0.0238408
0.9215686 0.2691030 0.0240034
0.9019608 0.2691030 0.0241264
0.9019608 0.2724252 0.0243432
0.9019608 0.2757475 0.0247344
0.9019608 0.2790698 0.0254783
0.9019608 0.2823920 0.0266556
0.8823529 0.2823920 0.0273367
0.8627451 0.2823920 0.0274027
0.8627451 0.2857143 0.0275641
0.8627451 0.2890365 0.0291581
0.8627451 0.2923588 0.0308870
0.8627451 0.2956811 0.0313872
0.8627451 0.2990033 0.0317540
0.8627451 0.3023256 0.0321871
0.8627451 0.3056478 0.0327443
0.8627451 0.3089701 0.0331779
0.8627451 0.3122924 0.0334742
0.8627451 0.3156146 0.0336459
0.8627451 0.3189369 0.0337397
0.8627451 0.3222591 0.0340724
0.8627451 0.3255814 0.0344454
0.8431373 0.3255814 0.0345286
0.8431373 0.3289037 0.0347000
0.8431373 0.3322259 0.0350958
0.8235294 0.3322259 0.0354591
0.8235294 0.3355482 0.0356209
0.8235294 0.3388704 0.0360737
0.8235294 0.3421927 0.0365431
0.8235294 0.3455150 0.0375349
0.8235294 0.3488372 0.0385378
0.8235294 0.3521595 0.0386621
0.8235294 0.3554817 0.0387427
0.8235294 0.3588040 0.0388348
0.8235294 0.3621262 0.0389021
0.8235294 0.3654485 0.0389414
0.8235294 0.3687708 0.0389665
0.8235294 0.3720930 0.0389957
0.8235294 0.3754153 0.0391012
0.8235294 0.3787375 0.0395306
0.8235294 0.3820598 0.0400720
0.8235294 0.3853821 0.0405463
0.8235294 0.3887043 0.0409054
0.8235294 0.3920266 0.0410592
0.8235294 0.3953488 0.0415408
0.8235294 0.3986711 0.0420902
0.8235294 0.4019934 0.0426373
0.8235294 0.4053156 0.0431736
0.8039216 0.4053156 0.0436964
0.8039216 0.4086379 0.0444852
0.8039216 0.4119601 0.0451967
0.8039216 0.4152824 0.0459803
0.8039216 0.4186047 0.0464729
0.8039216 0.4219269 0.0466719
0.8039216 0.4252492 0.0469204
0.8039216 0.4285714 0.0471795
0.8039216 0.4318937 0.0482317
0.8039216 0.4352159 0.0496509
0.8039216 0.4385382 0.0505852
0.8039216 0.4418605 0.0510440
0.8039216 0.4451827 0.0513486
0.8039216 0.4485050 0.0516321
0.8039216 0.4518272 0.0518188
0.8039216 0.4551495 0.0524418
0.8039216 0.4584718 0.0530790
0.8039216 0.4617940 0.0536581
0.8039216 0.4651163 0.0542043
0.8039216 0.4684385 0.0547520
0.8039216 0.4717608 0.0553497
0.7843137 0.4717608 0.0555392
0.7647059 0.4717608 0.0563684
0.7647059 0.4750831 0.0572254
0.7647059 0.4784053 0.0575194
0.7647059 0.4817276 0.0584818
0.7647059 0.4850498 0.0592419
0.7647059 0.4883721 0.0593094
0.7647059 0.4916944 0.0594862
0.7647059 0.4950166 0.0597554
0.7647059 0.4983389 0.0603254
0.7647059 0.5016611 0.0611299
0.7647059 0.5049834 0.0615857
0.7647059 0.5083056 0.0617986
0.7647059 0.5116279 0.0620822
0.7647059 0.5149502 0.0622572
0.7647059 0.5182724 0.0626807
0.7450980 0.5182724 0.0633472
0.7450980 0.5215947 0.0643667
0.7450980 0.5249169 0.0656844
0.7254902 0.5249169 0.0662916
0.7254902 0.5282392 0.0668232
0.7254902 0.5315615 0.0674331
0.7254902 0.5348837 0.0678069
0.7254902 0.5382060 0.0685609
0.7254902 0.5415282 0.0694541
0.7254902 0.5448505 0.0703198
0.7254902 0.5481728 0.0720935
0.7254902 0.5514950 0.0737082
0.7254902 0.5548173 0.0742014
0.7254902 0.5581395 0.0744489
0.7058824 0.5581395 0.0749420
0.7058824 0.5614618 0.0759008
0.7058824 0.5647841 0.0764485
0.7058824 0.5681063 0.0764517
0.6862745 0.5681063 0.0766272
0.6862745 0.5714286 0.0769424
0.6862745 0.5747508 0.0771564
0.6862745 0.5780731 0.0773129
0.6862745 0.5813953 0.0775322
0.6862745 0.5847176 0.0782950
0.6862745 0.5880399 0.0789686
0.6862745 0.5913621 0.0791587
0.6862745 0.5946844 0.0796864
0.6862745 0.5980066 0.0802908
0.6862745 0.6013289 0.0808296
0.6862745 0.6046512 0.0813231
0.6862745 0.6079734 0.0819913
0.6862745 0.6112957 0.0827288
0.6666667 0.6112957 0.0837176
0.6470588 0.6112957 0.0849227
0.6470588 0.6146179 0.0859557
0.6470588 0.6179402 0.0865881
0.6470588 0.6212625 0.0867594
0.6470588 0.6245847 0.0874249
0.6470588 0.6279070 0.0883586
0.6470588 0.6312292 0.0890085
0.6274510 0.6312292 0.0906823
0.6274510 0.6345515 0.0925418
0.6274510 0.6378738 0.0931656
0.6274510 0.6411960 0.0936169
0.6274510 0.6445183 0.0944417
0.6078431 0.6445183 0.0953467
0.6078431 0.6478405 0.0957851
0.6078431 0.6511628 0.0961296
0.6078431 0.6544850 0.0975012
0.6078431 0.6578073 0.0996724
0.6078431 0.6611296 0.1011320
0.6078431 0.6644518 0.1020696
0.6078431 0.6677741 0.1029883
0.6078431 0.6710963 0.1039216
0.6078431 0.6744186 0.1050752
0.6078431 0.6777409 0.1066915
0.6078431 0.6810631 0.1080279
0.5882353 0.6810631 0.1094498
0.5882353 0.6843854 0.1107354
0.5882353 0.6877076 0.1113042
0.5882353 0.6910299 0.1118378
0.5882353 0.6943522 0.1123211
0.5882353 0.6976744 0.1134014
0.5882353 0.7009967 0.1145322
0.5882353 0.7043189 0.1154636
0.5882353 0.7076412 0.1166167
0.5882353 0.7109635 0.1188091
0.5882353 0.7142857 0.1204424
0.5882353 0.7176080 0.1207964
0.5882353 0.7209302 0.1217960
0.5882353 0.7242525 0.1227994
0.5686275 0.7242525 0.1238072
0.5686275 0.7275748 0.1245830
0.5686275 0.7308970 0.1251730
0.5686275 0.7342193 0.1275374
0.5686275 0.7375415 0.1299426
0.5686275 0.7408638 0.1308707
0.5686275 0.7441860 0.1319663
0.5686275 0.7475083 0.1337558
0.5686275 0.7508306 0.1350815
0.5686275 0.7541528 0.1354572
0.5686275 0.7574751 0.1365199
0.5686275 0.7607973 0.1401068
0.5686275 0.7641196 0.1428595
0.5686275 0.7674419 0.1436218
0.5686275 0.7707641 0.1456370
0.5490196 0.7707641 0.1489569
0.5490196 0.7740864 0.1510305
0.5490196 0.7774086 0.1519543
0.5490196 0.7807309 0.1528522
0.5294118 0.7807309 0.1538547
0.5294118 0.7840532 0.1553728
0.5294118 0.7873754 0.1571199
0.5098039 0.7873754 0.1587614
0.5098039 0.7906977 0.1601783
0.5098039 0.7940199 0.1620035
0.5098039 0.7973422 0.1632503
0.5098039 0.8006645 0.1659941
0.5098039 0.8039867 0.1702712
0.5098039 0.8073090 0.1723311
0.5098039 0.8106312 0.1750208
0.4901961 0.8106312 0.1782816
0.4901961 0.8139535 0.1793545
0.4901961 0.8172757 0.1809613
0.4901961 0.8205980 0.1831977
0.4901961 0.8239203 0.1850795
0.4901961 0.8272425 0.1887424
0.4901961 0.8305648 0.1921729
0.4901961 0.8338870 0.1938847
0.4705882 0.8338870 0.1951107
0.4705882 0.8372093 0.1959306
0.4705882 0.8405316 0.2012398
0.4705882 0.8438538 0.2060884
0.4705882 0.8471761 0.2063760
0.4705882 0.8504983 0.2066889
0.4705882 0.8538206 0.2085457
0.4705882 0.8571429 0.2145743
0.4509804 0.8571429 0.2204765
0.4509804 0.8604651 0.2244497
0.4509804 0.8637874 0.2272744
0.4313725 0.8637874 0.2286395
0.4313725 0.8671096 0.2301419
0.4313725 0.8704319 0.2312194
0.4313725 0.8737542 0.2351818
0.4313725 0.8770764 0.2390212
0.4313725 0.8803987 0.2409361
0.4117647 0.8803987 0.2425567
0.4117647 0.8837209 0.2453946
0.4117647 0.8870432 0.2510134
0.4117647 0.8903654 0.2577031
0.3921569 0.8903654 0.2633644
0.3725490 0.8903654 0.2664178
0.3725490 0.8936877 0.2676448
0.3725490 0.8970100 0.2689722
0.3725490 0.9003322 0.2718732
0.3725490 0.9036545 0.2737804
0.3529412 0.9036545 0.2768944
0.3529412 0.9069767 0.2799960
0.3529412 0.9102990 0.2817435
0.3529412 0.9136213 0.2837210
0.3529412 0.9169435 0.2862671
0.3333333 0.9169435 0.2902311
0.3333333 0.9202658 0.2937288
0.3333333 0.9235880 0.2977666
0.3333333 0.9269103 0.3089466
0.3333333 0.9302326 0.3180384
0.3333333 0.9335548 0.3241486
0.3137255 0.9335548 0.3317701
0.3137255 0.9368771 0.3349842
0.3137255 0.9401993 0.3405156
0.2941176 0.9401993 0.3497579
0.2941176 0.9435216 0.3608292
0.2941176 0.9468439 0.3689117
0.2941176 0.9501661 0.3724141
0.2745098 0.9501661 0.3759333
0.2745098 0.9534884 0.3903937
0.2745098 0.9568106 0.4078925
0.2549020 0.9568106 0.4129910
0.2352941 0.9568106 0.4197760
0.2352941 0.9601329 0.4324888
0.2156863 0.9601329 0.4459284
0.2156863 0.9634551 0.4542337
0.2156863 0.9667774 0.4598269
0.1960784 0.9667774 0.4785203
0.1960784 0.9700997 0.5013915
0.1960784 0.9734219 0.5240036
0.1764706 0.9734219 0.5398136
0.1568627 0.9734219 0.5431187
0.1568627 0.9767442 0.5491985
0.1568627 0.9800664 0.5613330
0.1568627 0.9833887 0.5704168
0.1568627 0.9867110 0.5754019
0.1372549 0.9867110 0.5887381
0.1372549 0.9900332 0.6080586
0.1372549 0.9933555 0.6326949
0.1372549 0.9966777 0.6509173
0.1176471 0.9966777 0.6761477
0.0980392 0.9966777 0.7162796
0.0784314 0.9966777 0.7389048
0.0784314 1.0000000 0.7695786
0.0588235 1.0000000 0.8303756
0.0392157 1.0000000 0.8739064
0.0196078 1.0000000 0.9015379
0.0000000 1.0000000 Inf
# Elegir punto de corte
cutoff_val <- 0.2
idx <- which.min(abs(CUTOFF - cutoff_val))

cat("Sensibilidad (ROC):", round(SENS[idx], 3), "\n")
## Sensibilidad (ROC): 0.471
cat("Especificidad (ROC):", round(ESPEC[idx], 3), "\n")
## Especificidad (ROC): 0.841
# Matriz de confusión manual
mod_pred_test0 <- mod_pred_test %>%
  mutate(prediccion = ifelse(pred >= cutoff_val, 1, 0))

conf_mat <- table(Predicted = mod_pred_test0$prediccion, Actual = mod_pred_test0$obs)

# Extraer valores de la matriz
TP <- conf_mat["1", "1"]
FP <- conf_mat["1", "0"]
TN <- conf_mat["0", "0"]
FN <- conf_mat["0", "1"]

# Métricas manuales
sens_manual <- TP / (TP + FN)
spec_manual <- TN / (TN + FP)
vpp <- TP / (TP + FP)  # Valor Predictivo Positivo
vpn <- TN / (TN + FN)  # Valor Predictivo Negativo

# Mostrar resultados
cat("Sensibilidad (manual):", round(sens_manual, 3), "\n")
## Sensibilidad (manual): 0.471
cat("Especificidad (manual):", round(spec_manual, 3), "\n")
## Especificidad (manual): 0.841
cat("Valor Predictivo Positivo (VPP):", round(vpp, 3), "\n")
## Valor Predictivo Positivo (VPP): 0.333
cat("Valor Predictivo Negativo (VPN):", round(vpn, 3), "\n")
## Valor Predictivo Negativo (VPN): 0.904