library(readxl)
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.2.1 ✔ readr 2.2.0
## ✔ forcats 1.0.1 ✔ stringr 1.6.0
## ✔ lubridate 1.9.5 ✔ tibble 3.3.1
## ✔ purrr 1.2.1 ✔ tidyr 1.3.2
## ── 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
library(patchwork)
df_alturas<- read_xlsx("Alturas.xlsx")
variables_cm <- df_alturas %>%
select(Hombres = `Male Height in Cm`, Mujeres = `Female Height in Cm`)
df_largo <- df_alturas %>%
pivot_longer(
cols = c(`Male Height in Cm`, `Female Height in Cm`, `Male Height in Ft`, `Female Height in Ft`),
names_to = "Variable",
values_to = "Altura"
)
Agrupamos por variable y calculamos Shapiro-Wilk de forma secuencial
shapiro_resultados <- df_largo %>%
group_by(Variable) %>%
summarise(
Estadistico_W = shapiro.test(Altura)$statistic,
p_value = shapiro.test(Altura)$p.value
)
print(shapiro_resultados)
## # A tibble: 4 × 3
## Variable Estadistico_W p_value
## <chr> <dbl> <dbl>
## 1 Female Height in Cm 0.991 0.221
## 2 Female Height in Ft 0.990 0.176
## 3 Male Height in Cm 0.988 0.0852
## 4 Male Height in Ft 0.988 0.0846
Usamos facet_wrap para crear un gráfico por cada una de las 4 variables
fig <- ggplot(df_largo, aes(x = Altura, fill = Variable)) +
geom_histogram(aes(y = after_stat(density)), bins = 15, color = "white", alpha = 0.7) +
geom_density(color = "black", size = 0.8) +
facet_wrap(~ Variable, scales = "free") + # "free" permite que cada variable tenga su propia escala de ejes
labs(
title = "Análisis de Normalidad: Histogramas por Variable",
x = "Valor de la Altura",
y = "Densidad"
) +
theme_minimal() +
theme(legend.position = "none")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once per session.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
fig
Mismo principio de facetas, pero aplicando la geometría de cuantiles de ggplot2
fig <- ggplot(df_largo, aes(sample = Altura, color = Variable)) +
stat_qq(alpha = 0.7) +
stat_qq_line(color = "black", size = 0.8) + # Línea de referencia teórica
facet_wrap(~ Variable, scales = "free") +
labs(
title = "Análisis de Normalidad: Gráficos Cuantil-Cuantil (Q-Q)",
x = "Cuantiles Teóricos",
y = "Cuantiles Muestrales"
) +
theme_minimal() +
theme(legend.position = "none")
fig
print("=== HOMBRES: Prueba Bilateral (Diferente de 175) ===")
## [1] "=== HOMBRES: Prueba Bilateral (Diferente de 175) ==="
t_hombres_bilateral <- t.test(df_alturas$`Male Height in Cm`, mu = 175, alternative = "two.sided")
print(t_hombres_bilateral)
##
## One Sample t-test
##
## data: df_alturas$`Male Height in Cm`
## t = -5.4461, df = 198, p-value = 1.516e-07
## alternative hypothesis: true mean is not equal to 175
## 95 percent confidence interval:
## 172.3971 173.7810
## sample estimates:
## mean of x
## 173.089
CONCLUSION: Me tengo que ir por la alternativa: Hay suficiente evidencia para decir que la media obtenida difiere de 175
print("=== HOMBRES: Prueba Unilateral Derecha (Mayor que 175) ===")
## [1] "=== HOMBRES: Prueba Unilateral Derecha (Mayor que 175) ==="
t_hombres_derecha <- t.test(df_alturas$`Male Height in Cm`, mu = 175, alternative = "greater")
print(t_hombres_derecha)
##
## One Sample t-test
##
## data: df_alturas$`Male Height in Cm`
## t = -5.4461, df = 198, p-value = 1
## alternative hypothesis: true mean is greater than 175
## 95 percent confidence interval:
## 172.5092 Inf
## sample estimates:
## mean of x
## 173.089
CONCLUSION: Me tengo que ir por la nula: No hay suficiente evidencia para decir que la media es mayor a 175.
print("=== MUJERES: Prueba Bilateral (Diferente de 162) ===")
## [1] "=== MUJERES: Prueba Bilateral (Diferente de 162) ==="
t_mujeres_bilateral <- t.test(df_alturas$`Female Height in Cm`, mu = 162, alternative = "two.sided")
print(t_mujeres_bilateral)
##
## One Sample t-test
##
## data: df_alturas$`Female Height in Cm`
## t = -3.6582, df = 198, p-value = 0.0003256
## alternative hypothesis: true mean is not equal to 162
## 95 percent confidence interval:
## 160.3731 161.5128
## sample estimates:
## mean of x
## 160.9429
CONCLUSION: Me tengo que ir por la alternativa: Hay suficiente evidencia para decir que la media es diferente de 162
print("=== MUJERES: Prueba Unilateral Izquierda (Menor que 162) ===")
## [1] "=== MUJERES: Prueba Unilateral Izquierda (Menor que 162) ==="
t_mujeres_izquierda <- t.test(df_alturas$`Female Height in Cm`, mu = 162, alternative = "less")
print(t_mujeres_izquierda)
##
## One Sample t-test
##
## data: df_alturas$`Female Height in Cm`
## t = -3.6582, df = 198, p-value = 0.0001628
## alternative hypothesis: true mean is less than 162
## 95 percent confidence interval:
## -Inf 161.4205
## sample estimates:
## mean of x
## 160.9429
CONCLUSION: Me tengo que ir por la alternativa: Hay suficiente evidencia para decir que la media es menor a 162