library(readxl)
## Warning: package 'readxl' was built under R version 4.4.3
# Cargar datos
datos <- read_excel("C:/Users/Usuario/Desktop/EPECIALIZACION EN ESTADISTICA/Taller 3.xlsx")

# Ver las primeras filas
head(datos)
## # A tibble: 6 × 8
##   `No Empleados` Estrato `Ingreso Neto` `No de Personas a Cargo`
##            <dbl>   <dbl>          <dbl> <chr>                   
## 1              1       1         895212 Ninguna                 
## 2              2       1         822988 Ninguna                 
## 3              3       1         812288 Ninguna                 
## 4              4       1         627853 Ninguna                 
## 5              5       1         673359 Ninguna                 
## 6              6       1         696733 Ninguna                 
## # ℹ 4 more variables: `Gastos Familar (sinarriendo)` <dbl>,
## #   `Gastos de Arriendo` <dbl>, `Gastos / Ingresos` <dbl>,
## #   `Arriendos/ Ingreso` <dbl>
table(datos$Estrato)  # Revisa cuántos conglomerados hay
## 
##  1  2  3 
## 50 50 50
set.seed(123)  # Para reproducibilidad

# Seleccionar aleatoriamente 3 estratos de los existentes
estratos_muestra <- sample(unique(datos$Estrato), size = 3)

# Filtrar la muestra
muestra <- datos[datos$Estrato %in% estratos_muestra, ]

SMLV_2020 <- 877803

# Promedio del ingreso
media_ingreso <- mean(muestra$`Ingreso Neto`, na.rm = TRUE)

# Porcentaje de personas por debajo del SMLV
porcentaje_bajo_smlv <- mean(muestra$`Ingreso Neto` < SMLV_2020, na.rm = TRUE) * 100

media_ingreso
## [1] 1119099
porcentaje_bajo_smlv
## [1] 32
# IC para el promedio
ic_media <- t.test(muestra$`Ingreso Neto`, conf.level = 0.95)$conf.int

# IC para la proporción
prop <- mean(muestra$`Ingreso Neto` < SMLV_2020)
n <- nrow(muestra)
error <- qnorm(0.975) * sqrt((prop*(1 - prop))/n)
ic_prop <- c(prop - error, prop + error) * 100

ic_media
## [1] 1057734 1180465
## attr(,"conf.level")
## [1] 0.95
ic_prop
## [1] 24.53496 39.46504
t.test(muestra$`Ingreso Neto`, mu = SMLV_2020, alternative = "less")
## 
##  One Sample t-test
## 
## data:  muestra$`Ingreso Neto`
## t = 7.7699, df = 149, p-value = 1
## alternative hypothesis: true mean is less than 877803
## 95 percent confidence interval:
##     -Inf 1170500
## sample estimates:
## mean of x 
##   1119099
# Asegúrate de tener cargado el paquete ggplot2
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
# Suponiendo que ya tienes cargado el archivo con la variable `Ingreso Neto`
# datos <- read_excel("Taller 3.xlsx")

# Filtrar ingresos no nulos
ingresos <- datos$`Ingreso Neto`
ingresos <- ingresos[!is.na(ingresos)]

# Calcular media y desviación estándar
media <- mean(ingresos)
sd <- sd(ingresos)

# Crear un dataframe para graficar la curva normal
x_vals <- seq(media - 4*sd, media + 4*sd, length.out = 1000)
densidad <- dnorm(x_vals, mean = media, sd = sd)
df_densidad <- data.frame(x = x_vals, y = densidad)

# Crear la gráfica
ggplot(df_densidad, aes(x = x, y = y)) +
  geom_line(color = "blue", size = 1) +
  geom_vline(xintercept = media, color = "red", linetype = "dashed", size = 1) +
  geom_vline(xintercept = 877803, color = "green", linetype = "dotted", size = 1) +
  labs(title = "Campana de Gauss - Ingreso Neto de Contratistas",
       x = "Ingreso Neto", y = "Densidad") +
  annotate("text", x = media, y = max(densidad), label = paste("Media =", round(media, 0)), vjust = -1, color = "red") +
  annotate("text", x = 877803, y = max(densidad)/2, label = "SMLV 2020 = $877.803", vjust = -1, color = "green") +
  theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.