# Verificar archivos en el directorio
list.files()
## [1] "ññ.html"                                                                 
## [2] "rsconnect"                                                               
## [3] "TALLER-SEGUNDO-CORTE-ESTADIASTICA-MAURO-PEÑA-Y-MARIA-LUCIA-RUDAS....html"
## [4] "TALLER SEGUNDO CORTE ESTADIASTICA MAURO PEÑA Y MARIA LUCIA RUDAS....Rmd" 
## [5] "TALLER SEGUNDO CORTE ESTADIASTICA MAURO PEÑA Y MARIA LUCIA RUDAS.Rmd"    
## [6] "TALLER3-MAURO-PEÑA--MARIA-LUCIA-RUDAS-fv.Rmd"                            
## [7] "TALLER3 MAURO PEÑA- MARIA LUCIA RUDAS fv.Rmd"                            
## [8] "ttt.html"                                                                
## [9] "ttt.Rmd"
datos <- read.csv(file.choose(), sep = ",", header = TRUE)

Parte 1: Análisis Descriptivo

Resumen de variables numéricas

summary(datos)
##     Ciudad             Ingresos        Satisfecho  
##  Length:100         Min.   : 71.62   Min.   :0.00  
##  Class :character   1st Qu.: 88.83   1st Qu.:0.00  
##  Mode  :character   Median :110.92   Median :1.00  
##                     Mean   :114.24   Mean   :0.54  
##                     3rd Qu.:139.97   3rd Qu.:1.00  
##                     Max.   :159.63   Max.   :1.00

Frecuencia de variables categóricas

table(datos$Ciudad)
## 
## Ciudad A Ciudad B 
##       55       45

Histograma de ingresos

ggplot(datos, aes(x = Ingresos)) +
  geom_histogram(binwidth = 10, fill = "steelblue", color = "black") +
  labs(title = "Distribución de Ingresos", x = "Ingresos", y = "Frecuencia")

Gráfico de barras de ciudades

ggplot(datos, aes(x = Ciudad)) +
  geom_bar(fill = "darkorange") +
  labs(title = "Frecuencia de Ciudades", x = "Ciudad", y = "Frecuencia")

Boxplot de ingresos

ggplot(datos, aes(y = Ingresos)) +
  geom_boxplot(fill = "lightgreen") +
  labs(title = "Boxplot de Ingresos", y = "Ingresos")

Parte 2: Estimaciones Estadísticas

Intervalo de confianza para la media

media <- mean(datos$Ingresos)
error <- sd(datos$Ingresos) / sqrt(nrow(datos))
ic_media <- c(media - 1.96 * error, media + 1.96 * error)
ic_media
## [1] 108.7603 119.7111

Intervalo de confianza para la proporción

prop <- mean(datos$Satisfecho == 1)
error_prop <- sqrt(prop * (1 - prop) / nrow(datos))
ic_prop <- c(prop - 1.96 * error_prop, prop + 1.96 * error_prop)
ic_prop
## [1] 0.4423141 0.6376859

Intervalo de confianza para la varianza

varianza <- var(datos$Ingresos)
n <- nrow(datos)
ic_varianza <- c((n-1)*varianza/qchisq(0.975, n-1), (n-1)*varianza/qchisq(0.025, n-1))
ic_varianza
## [1]  601.6131 1053.1517

Diferencia de medias (Ciudad A vs Ciudad B)

grupo1 <- filter(datos, Ciudad == "Ciudad A")
grupo2 <- filter(datos, Ciudad == "Ciudad B")
t.test(grupo1$Ingresos, grupo2$Ingresos)
## 
##  Welch Two Sample t-test
## 
## data:  grupo1$Ingresos and grupo2$Ingresos
## t = -0.66312, df = 86.806, p-value = 0.509
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -15.168320   7.579351
## sample estimates:
## mean of x mean of y 
##  112.5282  116.3227

Diferencia de proporciones

tabla_satisfechos <- table(datos$Ciudad, datos$Satisfecho)
prop.test(x = c(tabla_satisfechos["Ciudad A", "1"], tabla_satisfechos["Ciudad B", "1"]),
          n = c(sum(tabla_satisfechos["Ciudad A", ]), sum(tabla_satisfechos["Ciudad B", ])))
## 
##  2-sample test for equality of proportions with continuity correction
## 
## data:  c(tabla_satisfechos["Ciudad A", "1"], tabla_satisfechos["Ciudad B", "1"]) out of c(sum(tabla_satisfechos["Ciudad A", ]), sum(tabla_satisfechos["Ciudad B", ]))
## X-squared = 1.2752, df = 1, p-value = 0.2588
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.08182847  0.34849514
## sample estimates:
##    prop 1    prop 2 
## 0.6000000 0.4666667

Razón de varianzas

var.test(grupo1$Ingresos, grupo2$Ingresos)
## 
##  F test to compare two variances
## 
## data:  grupo1$Ingresos and grupo2$Ingresos
## F = 0.72525, num df = 54, denom df = 44, p-value = 0.2601
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.4068728 1.2713078
## sample estimates:
## ratio of variances 
##           0.725247

Parte 3: Pruebas de Hipótesis

Prueba de hipótesis para la media

t.test(datos$Ingresos, mu = 110)
## 
##  One Sample t-test
## 
## data:  datos$Ingresos
## t = 1.5162, df = 99, p-value = 0.1326
## alternative hypothesis: true mean is not equal to 110
## 95 percent confidence interval:
##  108.6926 119.7788
## sample estimates:
## mean of x 
##  114.2357

Prueba de hipótesis para una proporción

prop.test(sum(datos$Satisfecho == 1), nrow(datos), p = 0.5)
## 
##  1-sample proportions test with continuity correction
## 
## data:  sum(datos$Satisfecho == 1) out of nrow(datos), null probability 0.5
## X-squared = 0.49, df = 1, p-value = 0.4839
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
##  0.4377639 0.6391280
## sample estimates:
##    p 
## 0.54

Prueba de hipótesis para la varianza

chi_inf <- (n - 1) * varianza / qchisq(0.975, df = n - 1)
chi_sup <- (n - 1) * varianza / qchisq(0.025, df = n - 1)
c(chi_inf, chi_sup)
## [1]  601.6131 1053.1517