bivar_add_cont2g <- function(lista, tabla, row, var_code, suffix = "2g", digits = 3, test_map = c( "Welch T test" = "T de Welch", "Student T test" = "T de Student", "Mann-W-U test" = "U de Mann-Whitney" )) { # Seleccionar la fila (por indice numerico o por nombre en Variable) if (is.numeric(row)) { r <- tabla[row, , drop = FALSE] } else { r <- tabla[tabla$Variable == row, , drop = FALSE] } if (nrow(r) != 1L) { stop("Se esperaba una unica fila para esa variable en 'tabla'.") } if (!"Significant_test" %in% names(r)) { stop("La tabla no tiene la columna 'Significant_test'.") } test_en <- as.character(r$Significant_test)[1L] # Mapear a etiqueta en español (si existe en el mapa) test_es <- unname(test_map[test_en]) if (is.na(test_es)) test_es <- test_en # Elegir el p segun el test significativo if (test_en %in% c("Welch T test", "Student T test")) { p_val <- prp(r$P_T_Test) } else if (test_en == "Mann-W-U test") { p_val <- prp(r$P_Mann_Whitney) } else if (test_en == "None"){ p_val <- ifelse( r$P_Shapiro_Resid > 0.05, prp(r$P_T_Test), prp(r$P_Mann_Whitney) ) } else { p_val <- NA } est <- r$Diff_Means ci_l <- r$CI_Lower ci_u <- r$CI_Upper result <- case_when( as.numeric(p_val) > 0.05 ~ "sin diferencias estadísticamente significativas", TRUE | NA ~ "diferencias estadísticamente significativas" ) if (as.numeric(p_val) > 0.05){ test_es <- case_when( r$P_Shapiro_Resid > 0.05 & r$P_Levene > 0.05 ~ "T de Student", r$P_Shapiro_Resid > 0.05 & r$P_Levene < 0.05 ~ "T de Welch", r$P_Shapiro_Resid < 0.05 ~ "U de Mann Withney" ) } if (is.numeric(est)) est <- round(est, digits) if (is.numeric(ci_l)) ci_l <- round(ci_l, digits) if (is.numeric(ci_u)) ci_u <- round(ci_u, digits) # Llenar la lista lista[[paste0("test_", var_code)]] <- test_es lista[[paste0("p_", var_code, "_", suffix)]] <- p_val lista[[paste0("est_", var_code, "_", suffix)]] <- est lista[[paste0("cil_", var_code, "_", suffix)]] <- ci_l lista[[paste0("ciu_", var_code, "_", suffix)]] <- ci_u lista[[paste0("res_", var_code, "_", suffix)]] <- result lista }