library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.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(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(tidycensus)
library(mapview)
library(viridis)
## Loading required package: viridisLite
library(knitr)
library(leaflet)
library(stringr)
options(tigris_use_cache = TRUE)
census_api_key("a275ba9891413d121ab90a8c65ceec32b0a3c166")
## To install your API key for use in future sessions, run this function with `install = TRUE`.

Variable a trabajar (ingreso)

census_us_county_income <- get_acs(geography = "county", variables = "B19013_001", 
                            shift_geo = TRUE, geometry = TRUE)
## Getting data from the 2018-2022 5-year ACS
## Warning: The `shift_geo` argument is deprecated and will be removed in a future
## release. We recommend using `tigris::shift_geometry()` instead.
## Using feature geometry obtained from the albersusa package
## Please note: Alaska and Hawaii are being shifted and are not to scale.
## old-style crs object detected; please recreate object with a recent sf::st_crs()

Distribución poblacional del ingreso

ggplot(data = census_us_county_income) + 
  geom_sf(aes(fill = estimate), color = NA) + 
  coord_sf(datum = NA) + 
  theme_minimal() + 
  scale_fill_viridis_c()

## Muestra aleatoria

sample_us_county_income <- sample_frac(tbl = census_us_county_income, size = 0.2)

Intervalos de confianza

+Para la media \(\mu\) con varianza conocida (\(\sigma^2\)=281.357.623 \({dolares}^2\))

\[ \left(\bar{x}_{n}-z_{\frac{\alpha}{2}}\sqrt{\frac{{\sigma^2}}{n}};\bar{x}_{n}+z_{\frac{\alpha}{2}}\sqrt{\frac{{\sigma^2}}{n}}\right) \] Con base en la muestra aleatoria generada calcule un intervalo de confianza para la media poblacional de la variable ingreso, basándose en la muestra aleatoria generada (sample_us_county_income) del 90% - 95% - 99%

sigma <- 281357623  # Varianza conocida
alpha_90 <- 0.10
alpha_95 <- 0.05
alpha_99 <- 0.01
n <- nrow(sample_us_county_income)
x_bar <- mean(sample_us_county_income$estimate)

\[ \left(\bar{x}_{n}-z_{\frac{\alpha}{2}}\sqrt{\frac{{s^2}}{n}};\bar{x}_{n}+z_{\frac{\alpha}{2}}\sqrt{\frac{{s^2}}{n}}\right) \]

Con base en la muestra aleatoria generada calcule un intervalo de confianza para la media poblacional de la variable ingreso, basándose en la muestra aleatoria generada (sample_us_county_income) del 90% - 95% - 99&

s <- sd(sample_us_county_income$estimate)
t_90 <- qt(1 - alpha_90 / 2, df = n - 1)
t_95 <- qt(1 - alpha_95 / 2, df = n - 1)
t_99 <- qt(1 - alpha_99 / 2, df = n - 1)
IC_90_unknown <- c(x_bar - t_90 * (s / sqrt(n)), x_bar + t_90 * (s / sqrt(n)))
IC_95_unknown <- c(x_bar - t_95 * (s / sqrt(n)), x_bar + t_95 * (s / sqrt(n)))
IC_99_unknown <- c(x_bar - t_99 * (s / sqrt(n)), x_bar + t_99 * (s / sqrt(n)))

\[ \left(\frac{({n}-1)s^2}{{\chi}_{n-1,1-\frac{\alpha}{2}}^{2}};\frac{({n}-1)s^2}{{\chi}_{n-1,\frac{\alpha}{2}}^{2}}\right) \]

Con base en la muestra aleatoria generada calcule un intervalo de confianza para la varianza poblacional de la variable ingreso, basándose en la muestra aleatoria generada (sample_us_county_income) del 90% - 95% - 99%

chi2_90_low <- qchisq(1 - alpha_90 / 2, df = n - 1)
chi2_90_high <- qchisq(alpha_90 / 2, df = n - 1)
chi2_95_low <- qchisq(1 - alpha_95 / 2, df = n - 1)
chi2_95_high <- qchisq(alpha_95 / 2, df = n - 1)
chi2_99_low <- qchisq(1 - alpha_99 / 2, df = n - 1)
chi2_99_high <- qchisq(alpha_99 / 2, df = n - 1)
IC_var_90 <- c(((n - 1) * s^2) / chi2_90_high, ((n - 1) * s^2) / chi2_90_low)
IC_var_95 <- c(((n - 1) * s^2) / chi2_95_high, ((n - 1) * s^2) / chi2_95_low)
IC_var_99 <- c(((n - 1) * s^2) / chi2_99_high, ((n - 1) * s^2) / chi2_99_low)