1 1. Carga de datos

datos <- read_csv("/Users/adrianaholguin/Desktop/Tabla tesis armoniosa.csv") %>%
  clean_names()
## New names:
## Rows: 42 Columns: 105
## ── Column specification
## ──────────────────────────────────────────────────────── Delimiter: "," dbl
## (105): Sujeto, 1. Edad, 2. Sexo, 3. Estado civil, 4. Trabaja, 5. Residen...
## ℹ 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.
## • `` -> `...16`
## • `` -> `...17`
## • `` -> `...18`
## • `` -> `...19`
## • `` -> `...20`
## • `` -> `...24`
## • `` -> `...25`
## • `` -> `...26`
## • `` -> `...27`
## • `` -> `...29`
## • `` -> `...30`
## • `` -> `...31`
## • `` -> `...32`
## • `` -> `...34`
## • `` -> `...35`
## • `` -> `...36`
## • `` -> `...37`
## • `` -> `...38`
## • `` -> `...40`
## • `` -> `...41`
## • `` -> `...42`
## • `` -> `...43`
## • `` -> `...44`
## • `` -> `...46`
## • `` -> `...47`
## • `` -> `...48`
## • `` -> `...51`
## • `` -> `...52`
## • `` -> `...53`
## • `` -> `...56`
## • `` -> `...57`
## • `` -> `...58`
## • `` -> `...61`
## • `` -> `...62`
## • `` -> `...63`
## • `` -> `...64`
## • `` -> `...65`

2 2. Preparación de datos

# Renombrar variable de participación
datos <- datos %>% 
  rename(ha_realizado_voluntariado = x15_ha_realizado_v) %>%
  mutate(ha_realizado_voluntariado = factor(ha_realizado_voluntariado, labels = c("No", "Sí")))

# Separar grupos
voluntarios <- datos %>% filter(ha_realizado_voluntariado == "Sí")
no_voluntarios <- datos %>% filter(ha_realizado_voluntariado == "No")

# Selección de ítems de prosocialidad A y B
prosocial_a <- voluntarios %>% select(starts_with("a1_"), starts_with("a2_"), starts_with("a3_"),
                                      starts_with("a4_"), starts_with("a5_"), starts_with("a6_"),
                                      starts_with("a7_"), starts_with("a8_"), starts_with("a9_"),
                                      starts_with("a10_"), starts_with("a11_"), starts_with("a12_"),
                                      starts_with("a13_"), starts_with("a14_"), starts_with("a15_"),
                                      starts_with("a16_"))

prosocial_b <- voluntarios %>% select(starts_with("b1_"), starts_with("b2_"), starts_with("b3_"),
                                      starts_with("b4_"), starts_with("b5_"), starts_with("b6_"),
                                      starts_with("b7_"), starts_with("b8_"), starts_with("b9_"),
                                      starts_with("b10_"), starts_with("b11_"), starts_with("b12_"),
                                      starts_with("b13_"), starts_with("b14_"), starts_with("b15_"),
                                      starts_with("b16_"))

3 3. Estadística descriptiva

sociodemo <- datos %>%
  select(x2_sexo, x3_estado_civil, x4_trabaja, x5_residente, x6_vive_con, x8_tipo_de_familia, x10_nivel_socioeconomico)
# Crear objeto sociodemo
sociodemo <- datos %>%
  select(x2_sexo, x3_estado_civil, x4_trabaja, x5_residente, x6_vive_con, x8_tipo_de_familia, x10_nivel_socioeconomico)

# Seleccionar las variables sociodemográficas
sociodemo <- datos %>%
  select(x2_sexo, x3_estado_civil, x4_trabaja, x5_residente, x6_vive_con, x8_tipo_de_familia, x10_nivel_socioeconomico)

# Iterar por cada variable
for (variable in names(sociodemo)) {
  
  cat("### 📌", variable, "\n\n")
  
  valores <- sociodemo[[variable]]
  tabla <- table(valores)
  porcentaje <- round(prop.table(tabla) * 100, 1)
  
  df <- data.frame(
    Respuesta = names(tabla),
    Frecuencia = as.vector(tabla),
    Porcentaje = as.vector(porcentaje)
  )
  
  print(kable(df, caption = paste("Distribución de", variable)))
  cat("\n\n")
}

3.1 📌 x2_sexo

Distribución de x2_sexo
Respuesta Frecuencia Porcentaje
1 33 78.6
2 9 21.4

3.2 📌 x3_estado_civil

Distribución de x3_estado_civil
Respuesta Frecuencia Porcentaje
1 41 97.6
3 1 2.4

3.3 📌 x4_trabaja

Distribución de x4_trabaja
Respuesta Frecuencia Porcentaje
0 20 47.6
1 22 52.4

3.4 📌 x5_residente

Distribución de x5_residente
Respuesta Frecuencia Porcentaje
1 19 45.2
2 23 54.8

3.5 📌 x6_vive_con

Distribución de x6_vive_con
Respuesta Frecuencia Porcentaje
1 24 57.1
2 16 38.1
3 2 4.8

3.6 📌 x8_tipo_de_familia

Distribución de x8_tipo_de_familia
Respuesta Frecuencia Porcentaje
1 29 69.0
2 6 14.3
3 7 16.7

3.7 📌 x10_nivel_socioeconomico

Distribución de x10_nivel_socioeconomico
Respuesta Frecuencia Porcentaje
2 38 90.5
3 4 9.5

4 4. Comparación de prosocialidad

# Cálculo de dimensiones específicas
voluntarios <- voluntarios %>%
  mutate(
    pre_ayudar = a_ayudar,
    post_ayudar = b_ayudar,
    pre_empatia = a_empatia,
    post_empatia = b_empatia,
    pre_compartir = a_compartir,
    post_compartir = b_compartir,
    pre_total = a_total,
    post_total = b_total
  )

# Función para aplicar test
comparar_pares <- function(pre, post) {
  if (shapiro.test(pre)$p.value > 0.05 & shapiro.test(post)$p.value > 0.05) {
    test <- t.test(pre, post, paired = TRUE)
    tipo <- "t de Student"
  } else {
    test <- wilcox.test(pre, post, paired = TRUE)
    tipo <- "Wilcoxon"
  }
  list(test = test, tipo = tipo)
}

# Ejecutar comparaciones
res_ayudar <- comparar_pares(voluntarios$pre_ayudar, voluntarios$post_ayudar)
res_empatia <- comparar_pares(voluntarios$pre_empatia, voluntarios$post_empatia)
## Warning in wilcox.test.default(pre, post, paired = TRUE): cannot compute exact
## p-value with ties
## Warning in wilcox.test.default(pre, post, paired = TRUE): cannot compute exact
## p-value with zeroes
res_compartir <- comparar_pares(voluntarios$pre_compartir, voluntarios$post_compartir)
## Warning in wilcox.test.default(pre, post, paired = TRUE): cannot compute exact
## p-value with ties
## Warning in wilcox.test.default(pre, post, paired = TRUE): cannot compute exact
## p-value with zeroes
res_total <- comparar_pares(voluntarios$pre_total, voluntarios$post_total)

# Resultados
cat("
🔹 AYUDAR - Prueba:", res_ayudar$tipo, "
")
## 
## 🔹 AYUDAR - Prueba: t de Student
print(res_ayudar$test)
## 
##  Paired t-test
## 
## data:  pre and post
## t = -12.288, df = 24, p-value = 7.635e-12
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  -7.054464 -5.025536
## sample estimates:
## mean difference 
##           -6.04
cat("
🔹 EMPATÍA - Prueba:", res_empatia$tipo, "
")
## 
## 🔹 EMPATÍA - Prueba: Wilcoxon
print(res_empatia$test)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  pre and post
## V = 132.5, p-value = 0.3024
## alternative hypothesis: true location shift is not equal to 0
cat("
🔹 COMPARTIR - Prueba:", res_compartir$tipo, "
")
## 
## 🔹 COMPARTIR - Prueba: Wilcoxon
print(res_compartir$test)
## 
##  Wilcoxon signed rank test with continuity correction
## 
## data:  pre and post
## V = 86.5, p-value = 0.3379
## alternative hypothesis: true location shift is not equal to 0
cat("
🔹 TOTAL - Prueba:", res_total$tipo, "
")
## 
## 🔹 TOTAL - Prueba: t de Student
print(res_total$test)
## 
##  Paired t-test
## 
## data:  pre and post
## t = -5.8783, df = 24, p-value = 4.599e-06
## alternative hypothesis: true mean difference is not equal to 0
## 95 percent confidence interval:
##  -7.187876 -3.452124
## sample estimates:
## mean difference 
##           -5.32

voluntarios <- voluntarios %>% mutate( pretest = rowSums(select(., starts_with(“a”)), na.rm = TRUE), postest = rowSums(select(., starts_with(“b”)), na.rm = TRUE) )

voluntarios <- voluntarios %>%
  mutate(
    pretest = rowSums(select(., starts_with("a")), na.rm = TRUE),
    postest = rowSums(select(., starts_with("b")), na.rm = TRUE)
  )


# 1. Verificar normalidad
shapiro_pre <- shapiro.test(voluntarios$pretest)
shapiro_post <- shapiro.test(voluntarios$postest)

# 2. Aplicar prueba según normalidad
if (shapiro_pre$p.value > 0.05 & shapiro_post$p.value > 0.05) {
  prueba_resultado <- t.test(voluntarios$pretest, voluntarios$postest, paired = TRUE)
  prueba_tipo <- "t de Student (pareada)"
} else {
  prueba_resultado <- wilcox.test(voluntarios$pretest, voluntarios$postest, paired = TRUE)
  prueba_tipo <- "Wilcoxon (no paramétrica)"
}
## Warning in wilcox.test.default(voluntarios$pretest, voluntarios$postest, :
## cannot compute exact p-value with ties
## Warning in wilcox.test.default(voluntarios$pretest, voluntarios$postest, :
## cannot compute exact p-value with zeroes

mensaje <- paste0( “🧪 Tipo de prueba aplicada:”, prueba_tipo, “📊 Resultado:”, capture.output(prueba_resultado) %>% paste(collapse = “”) )

cat(mensaje)

5 5. Visualización

# Crear pretest y postest como sumas válidas
voluntarios <- voluntarios %>%
  mutate(
    pretest = rowSums(select(., starts_with("a")), na.rm = TRUE),
    postest = rowSums(select(., starts_with("b")), na.rm = TRUE)
  )
library(ggplot2)

voluntarios %>%
  mutate(id = row_number()) %>%  # Crear ID por fila
  pivot_longer(cols = c(pretest, postest), names_to = "momento", values_to = "puntaje") %>%
  ggplot(aes(x = momento, y = puntaje, group = id)) +
  geom_line(alpha = 0.4, color = "#2C3E50") +
  geom_point(size = 2, color = "#2980B9") +
  labs(title = "Cambio individual en prosocialidad",
       x = "Momento de medición", y = "Puntaje total") +
  theme_minimal()

voluntarios %>%
  pivot_longer(cols = c(pretest, postest), names_to = "momento", values_to = "puntaje") %>%
  ggplot(aes(x = momento, y = puntaje, fill = momento)) +
  geom_boxplot(alpha = 0.6) +
  scale_fill_manual(values = c(pretest = "#E74C3C", postest = "#3498DB")) +
  labs(title = "Distribución de prosocialidad: pretest vs postest",
       x = "Momento", y = "Puntaje total") +
  theme_minimal()