Analisis de divorsio
if(!require(RColorBrewer)) install.packages('RColorBrewer')
## Loading required package: RColorBrewer
require(RColorBrewer)
if(!require(dplyr)) install.packages('dplyr')
## Loading required package: dplyr
## Warning: package 'dplyr' was built under R version 4.2.3
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
require(dplyr)
if(!require(ggplot2)) install.packages('ggplot2')
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.2.3
require(ggplot2)
## lectura de datos
d <- read.csv("https://www.dropbox.com/s/a77erydd1ip6lnr/divorce.csv?dl=1",
header = TRUE, sep = ',')
#knitr::kable(d)
total_participants <- nrow(d)
divorced_cases <- d %>% filter(class == 1)
total_divorced <- nrow(divorced_cases)
# Imprimir los resultados
cat("Total de participantes en el estudio:", total_participants, "\n")
## Total de participantes en el estudio: 170
cat("Total de individuos que se divorciaron:", total_divorced, "\n")
## Total de individuos que se divorciaron: 84
preguntas <- paste0("x", 1:54)
frecuencias <- d %>%
group_by(class) %>%
summarise(across(all_of(preguntas), ~ mean(.))) %>%
mutate(across(all_of(preguntas), ~ . / sum(.)))
knitr::kable(frecuencias)
| class | x1 | x2 | x3 | x4 | x5 | x6 | x7 | x8 | x9 | x10 | x11 | x12 | x13 | x14 | x15 | x16 | x17 | x18 | x19 | x20 | x21 | x22 | x23 | x24 | x25 | x26 | x27 | x28 | x29 | x30 | x31 | x32 | x33 | x34 | x35 | x36 | x37 | x38 | x39 | x40 | x41 | x42 | x43 | x44 | x45 | x46 | x47 | x48 | x49 | x50 | x51 | x52 | x53 | x54 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | 0.1102532 | 0.1395001 | 0.1798365 | 0.0893369 | 0.0335791 | 0.2475594 | 0.0116311 | 0.0435438 | 0.0197017 | 0.1279485 | 0.0579357 | 0.1185161 | 0.1634241 | 0.091655 | 0.073292 | 0.0545505 | 0.0522042 | 0.0303085 | 0.0420526 | 0.0236442 | 0.0538727 | 0.0276619 | 0.0285077 | 0.0875952 | 0.102508 | 0.0657338 | 0.0534194 | 0.0308208 | 0.0307861 | 0.0654746 | 0.1957155 | 0.176553 | 0.0796903 | 0.1456542 | 0.030976 | 0.0107362 | 0.1491026 | 0.0929341 | 0.1352524 | 0.0553603 | 0.1184645 | 0.2328278 | 0.3597797 | 0.136595 | 0.3344549 | 0.3815291 | 0.2725123 | 0.372168 | 0.2669749 | 0.2833333 | 0.3249833 | 0.3103618 | 0.2631288 | 0.1691814 |
| 1 | 0.8897468 | 0.8604999 | 0.8201635 | 0.9106631 | 0.9664209 | 0.7524406 | 0.9883689 | 0.9564562 | 0.9802983 | 0.8720515 | 0.9420643 | 0.8814839 | 0.8365759 | 0.908345 | 0.926708 | 0.9454495 | 0.9477958 | 0.9696915 | 0.9579474 | 0.9763558 | 0.9461273 | 0.9723381 | 0.9714923 | 0.9124048 | 0.897492 | 0.9342662 | 0.9465806 | 0.9691792 | 0.9692139 | 0.9345254 | 0.8042845 | 0.823447 | 0.9203097 | 0.8543458 | 0.969024 | 0.9892638 | 0.8508974 | 0.9070659 | 0.8647476 | 0.9446397 | 0.8815355 | 0.7671722 | 0.6402203 | 0.863405 | 0.6655451 | 0.6184709 | 0.7274877 | 0.627832 | 0.7330251 | 0.7166667 | 0.6750167 | 0.6896382 | 0.7368712 | 0.8308186 |
ggplot(data = frecuencias) +
geom_bar(aes(x = "", y = x1, fill = factor(class)), stat = "identity", position = "fill") +
geom_bar(aes(x = "", y = x2, fill = factor(class)), stat = "identity", position = "fill") +
# Repetir la línea anterior para todas las columnas x3, x4, ... x54 ...
scale_fill_manual(values = c("#66A61E", "#7570B3")) +
labs(x = NULL, y = "Frecuencia Relativa", fill = "Estado") +
ggtitle("Frecuencia de Respuestas por Grupo (Divorciados vs. No Divorciados)") +
theme_minimal() +
theme(legend.position = "bottom") +
theme(axis.text.x = element_blank())
results <- lapply(paste0("x", 1:54), function(variable) {
contingency_table <- table(d$class, d[[variable]])
chi_squared_test <- chisq.test(contingency_table)
data.frame(
variable = variable,
statistic = chi_squared_test$statistic,
p_value = chi_squared_test$p.value
)
})
# Combinar los resultados en un data frame
results_df <- do.call(rbind, results)
# Ordenar los resultados por valor p
results_df <- results_df %>% arrange(p_value)
knitr::kable(results_df)
| variable | statistic | p_value | |
|---|---|---|---|
| X-squared17 | x18 | 159.64920 | 0e+00 |
| X-squared19 | x20 | 156.44751 | 0e+00 |
| X-squared39 | x40 | 156.36136 | 0e+00 |
| X-squared16 | x17 | 155.78616 | 0e+00 |
| X-squared18 | x19 | 155.36940 | 0e+00 |
| X-squared10 | x11 | 155.06539 | 0e+00 |
| X-squared8 | x9 | 152.71189 | 0e+00 |
| X-squared15 | x16 | 151.50748 | 0e+00 |
| X-squared25 | x26 | 148.29700 | 0e+00 |
| X-squared14 | x15 | 146.48549 | 0e+00 |
| X-squared28 | x29 | 146.13345 | 0e+00 |
| X-squared29 | x30 | 145.76120 | 0e+00 |
| X-squared38 | x39 | 145.42385 | 0e+00 |
| X-squared11 | x12 | 145.32969 | 0e+00 |
| X-squared24 | x25 | 144.05990 | 0e+00 |
| X-squared13 | x14 | 143.99941 | 0e+00 |
| X-squared35 | x36 | 143.13264 | 0e+00 |
| X-squared4 | x5 | 142.43521 | 0e+00 |
| X-squared40 | x41 | 140.95076 | 0e+00 |
| X-squared26 | x27 | 140.38263 | 0e+00 |
| X-squared27 | x28 | 139.28987 | 0e+00 |
| X-squared7 | x8 | 139.14776 | 0e+00 |
| X-squared3 | x4 | 139.05667 | 0e+00 |
| X-squared20 | x21 | 139.04247 | 0e+00 |
| X-squared37 | x38 | 138.22477 | 0e+00 |
| X-squared36 | x37 | 135.40063 | 0e+00 |
| X-squared | x1 | 133.63965 | 0e+00 |
| X-squared43 | x44 | 133.47049 | 0e+00 |
| X-squared12 | x13 | 132.90903 | 0e+00 |
| X-squared21 | x22 | 131.32444 | 0e+00 |
| X-squared34 | x35 | 131.03231 | 0e+00 |
| X-squared9 | x10 | 131.00120 | 0e+00 |
| X-squared23 | x24 | 128.97937 | 0e+00 |
| X-squared32 | x33 | 128.67471 | 0e+00 |
| X-squared1 | x2 | 127.47975 | 0e+00 |
| X-squared30 | x31 | 124.86215 | 0e+00 |
| X-squared22 | x23 | 124.73659 | 0e+00 |
| X-squared33 | x34 | 124.44454 | 0e+00 |
| X-squared31 | x32 | 122.64678 | 0e+00 |
| X-squared53 | x54 | 118.27635 | 0e+00 |
| X-squared2 | x3 | 116.20221 | 0e+00 |
| X-squared49 | x50 | 108.99300 | 0e+00 |
| X-squared41 | x42 | 100.92593 | 0e+00 |
| X-squared50 | x51 | 97.04007 | 0e+00 |
| X-squared48 | x49 | 95.47681 | 0e+00 |
| X-squared52 | x53 | 88.93651 | 0e+00 |
| X-squared6 | x7 | 79.59150 | 0e+00 |
| X-squared47 | x48 | 76.43169 | 0e+00 |
| X-squared46 | x47 | 74.97654 | 0e+00 |
| X-squared51 | x52 | 74.86155 | 0e+00 |
| X-squared42 | x43 | 64.37419 | 0e+00 |
| X-squared44 | x45 | 55.12227 | 0e+00 |
| X-squared5 | x6 | 41.25243 | 0e+00 |
| X-squared45 | x46 | 37.22272 | 2e-07 |
chi cuadrado o valor P mas bajo
variable_mas_asociada <- results_df$variable[1]
print("Variable más asociada:")
## [1] "Variable más asociada:"
print(variable_mas_asociada)
## [1] "x18"
ggplot(results_df, aes(x = reorder(variable, p_value), y = p_value)) +
geom_bar(stat = "identity", fill = "#1B9E77", width = 0.5) +
labs(x = "Variable", y = "Valor p", title = "Asociacion entre Variables y Clase") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 8),
axis.text.y = element_text(size = 8),
axis.title = element_text(size = 10),
plot.title = element_text(size = 12, face = "bold"),
legend.position = "none")