Pregunta

Usando una muestra aleatoria de 150 observaciones obtenidas con la semilla 5317, responda la siguiente pregunta: ¿Tienen hombres y mujeres ingresos similares? Repita el ejercicio usando la totalidad de datos disponibles (la población) y compare ambos resultados. Para su respuesta, utilice una forma gráfica adecuada y una o más medidas estadísticas, justificando la selección.

Cargar bibliotecas

library(ggpubr)
## Cargando paquete requerido: ggplot2
library(ggplot2)

Cargando los datos

datos <- read.csv("C:/Users/orion/Downloads/EP03 Datos.csv", 
                 header = TRUE,
                 sep = ";",
                 stringsAsFactors = FALSE)

Seleccion de muestra y calculos

set.seed(5317)

# Muestra aleatoria
indices_muestra <- sample(1:nrow(datos), 150)
muestra_datos <- datos[indices_muestra, ]

# Separando por sexo
hombres_muestra <- muestra_datos[muestra_datos$sexo == "Hombre", ]
mujeres_muestra <- muestra_datos[muestra_datos$sexo == "Mujer", ]

hombres_poblacion <- datos[datos$sexo == "Hombre", ]
mujeres_poblacion <- datos[datos$sexo == "Mujer", ]

# Estadísticas de la muestra
media_hombres_muestra <- mean(hombres_muestra$ytot, na.rm = TRUE)
media_mujeres_muestra <- mean(mujeres_muestra$ytot, na.rm = TRUE)
sd_hombres_muestra <- sd(hombres_muestra$ytot, na.rm = TRUE)
sd_mujeres_muestra <- sd(mujeres_muestra$ytot, na.rm = TRUE)

# Estadísticas de la población
media_hombres_poblacion <- mean(hombres_poblacion$ytot, na.rm = TRUE)
media_mujeres_poblacion <- mean(mujeres_poblacion$ytot, na.rm = TRUE)
sd_hombres_poblacion <- sd(hombres_poblacion$ytot, na.rm = TRUE)
sd_mujeres_poblacion <- sd(mujeres_poblacion$ytot, na.rm = TRUE)

Histograma

par(mfrow = c(2, 2))

hist(hombres_muestra$ytot,
     main = "Ingresos Hombres de la muestra",
     xlab = "Ingresos", ylab = "Frecuencia")
abline(v = media_hombres_muestra, col = "red", lwd = 2)

hist(mujeres_muestra$ytot,
     main = "Ingresos Mujeres de la muestra",
     xlab = "Ingresos", ylab = "Frecuencia")
abline(v = media_mujeres_muestra, col = "red", lwd = 2)

hist(hombres_poblacion$ytot,
     main = "Ingresos Hombres de la población",
     xlab = "Ingresos", ylab = "Frecuencia")
abline(v = media_hombres_poblacion, col = "red", lwd = 2)

hist(mujeres_poblacion$ytot,
     main = "Ingresos Mujeres de la poblacion",
     xlab = "Ingresos", ylab = "Frecuencia")
abline(v = media_mujeres_poblacion, col = "red", lwd = 2)

Gráfico de densidad de la muestra

x_h_m <- seq(min(hombres_muestra$ytot, na.rm = TRUE), 
             max(hombres_muestra$ytot, na.rm = TRUE), length.out = 100)
y_h_m <- dnorm(x_h_m, mean = media_hombres_muestra, sd = sd_hombres_muestra)
densidad_h_m <- data.frame(x = x_h_m, y = y_h_m)

x_m_m <- seq(min(mujeres_muestra$ytot, na.rm = TRUE), 
             max(mujeres_muestra$ytot, na.rm = TRUE), length.out = 100)
y_m_m <- dnorm(x_m_m, mean = media_mujeres_muestra, sd = sd_mujeres_muestra)
densidad_m_m <- data.frame(x = x_m_m, y = y_m_m)

g_muestra <- ggplot(densidad_h_m, aes(x, y)) + 
  geom_line(color = "blue") +
  geom_line(data = densidad_m_m, color = "red") +
  theme_pubr() +
  labs(title = "Comparación de ingresos por sexo segun muestra",
       x = "Ingresos", y = "Densidad") +
  annotate("text", x = max(x_h_m) * 0.8, y = max(y_h_m) * 0.8, 
           label = paste("Media Hombres:", round(media_hombres_muestra, 0)), color = "blue") +
  annotate("text", x = max(x_m_m) * 0.8, y = max(y_m_m) * 0.7, 
           label = paste("Media Mujeres:", round(media_mujeres_muestra, 0)), color = "red")

print(g_muestra)

Gráfico de densidad poblacion total

x_h_p <- seq(min(hombres_poblacion$ytot, na.rm = TRUE), 
             max(hombres_poblacion$ytot, na.rm = TRUE), length.out = 100)
y_h_p <- dnorm(x_h_p, mean = media_hombres_poblacion, sd = sd_hombres_poblacion)
densidad_h_p <- data.frame(x = x_h_p, y = y_h_p)

x_m_p <- seq(min(mujeres_poblacion$ytot, na.rm = TRUE), 
             max(mujeres_poblacion$ytot, na.rm = TRUE), length.out = 100)
y_m_p <- dnorm(x_m_p, mean = media_mujeres_poblacion, sd = sd_mujeres_poblacion)
densidad_m_p <- data.frame(x = x_m_p, y = y_m_p)

g_poblacion <- ggplot(densidad_h_p, aes(x, y)) + 
  geom_line(color = "blue") +
  geom_line(data = densidad_m_p, color = "red") +
  theme_pubr() +
  labs(title = "Comparación de ingresos por sexo segun población",
       x = "Ingresos", y = "Densidad") +
  annotate("text", x = max(x_h_p) * 0.8, y = max(y_h_p) * 0.8, 
           label = paste("Media Hombres:", round(media_hombres_poblacion, 0)), color = "blue") +
  annotate("text", x = max(x_m_p) * 0.8, y = max(y_m_p) * 0.7, 
           label = paste("Media Mujeres:", round(media_mujeres_poblacion, 0)), color = "red")

print(g_poblacion)

Analisis estadistico

t_test_muestra <- t.test(hombres_muestra$ytot, mujeres_muestra$ytot)
t_test_poblacion <- t.test(hombres_poblacion$ytot, mujeres_poblacion$ytot)

# Resultados
print(t_test_muestra)
## 
##  Welch Two Sample t-test
## 
## data:  hombres_muestra$ytot and mujeres_muestra$ytot
## t = 3.7553, df = 76.894, p-value = 0.0003347
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##   311468.9 1014661.8
## sample estimates:
## mean of x mean of y 
## 1013576.0  350510.7
print(t_test_poblacion)
## 
##  Welch Two Sample t-test
## 
## data:  hombres_poblacion$ytot and mujeres_poblacion$ytot
## t = 18.566, df = 22670, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  243240.2 300661.7
## sample estimates:
## mean of x mean of y 
##  754535.5  482584.6