Simulado población

set.seed(123)
poblacion <- rnorm(n = 1000, mean = 45.2, sd = 11.2)

Extracción de una muestra

set.seed(123)
muestra <- sample(x = poblacion, size = 100, replace = TRUE)
mean(muestra)
## [1] 43.75853

Etapas

Juego de hipótesis

  • Queremos contrastar si el promedio de la muestra es igual o diferente a 45.2

\[H_0: \mu = 45.2 \\ H_1: \mu \neq 45.2\]

Nivel de significancia

  • El nivel de significancia \(\alpha = 0.05\), es decir, que tenemos un nivel de confianza del 95%.

Calcular el valor P

  • En este caso vamos a implementar la prueba t-student para contrastar el juego de hipótesis.
t.test(x = muestra,
       mu = 45.2,
       alternative = "two.sided",
       conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  muestra
## t = -1.2649, df = 99, p-value = 0.2089
## alternative hypothesis: true mean is not equal to 45.2
## 95 percent confidence interval:
##  41.49741 46.01965
## sample estimates:
## mean of x 
##  43.75853

Extracción 100 muestras y cÔlculo de intervalos de confianza

Prueba t-student

library(tidyverse)
library(broom)
set.seed(123)
prueba <- tibble(muestra_num = 1:100) %>% 
  mutate(muestra = map(.x = muestra_num, .f = ~sample(
    x = poblacion,
    size = 100,
    replace = TRUE
  )),
  estimado = map(.x = muestra, .f = ~t.test(
    x = .x,
    mu = 45.2,
    alternative = "two.sided",
    conf.level = 0.95
  )),
  estimado_tidy = map(.x = estimado, .f = tidy)) %>% 
  unnest(estimado_tidy)

prueba
## # A tibble: 100 Ɨ 11
##    muestra_num muestra     estimado estimate statistic p.value parameter conf.low
##          <int> <list>      <list>      <dbl>     <dbl>   <dbl>     <dbl>    <dbl>
##  1           1 <dbl [100]> <htest>      43.8    -1.26   0.209         99     41.5
##  2           2 <dbl [100]> <htest>      46.5     1.24   0.218         99     44.4
##  3           3 <dbl [100]> <htest>      47.2     1.77   0.0795        99     45.0
##  4           4 <dbl [100]> <htest>      46.2     1.10   0.272         99     44.4
##  5           5 <dbl [100]> <htest>      45.5     0.254  0.800         99     43.4
##  6           6 <dbl [100]> <htest>      44.8    -0.392  0.696         99     42.6
##  7           7 <dbl [100]> <htest>      46.0     0.776  0.440         99     43.9
##  8           8 <dbl [100]> <htest>      47.5     2.04   0.0442        99     45.3
##  9           9 <dbl [100]> <htest>      45.8     0.546  0.586         99     43.5
## 10          10 <dbl [100]> <htest>      46.7     1.25   0.213         99     44.3
## # … with 90 more rows, and 3 more variables: conf.high <dbl>, method <chr>,
## #   alternative <chr>

GrƔfico de intervalos de confianza

prueba %>% 
  ggplot(mapping = aes(x = muestra_num, y = estimate)) +
  geom_point() +
  geom_errorbar(mapping = aes(ymin = conf.low, ymax = conf.high)) +
  geom_hline(yintercept = 45.2, color = "red", lty = 2)

Valores P de las 100 pruebas t-student

prueba %>% 
  ggplot(mapping = aes(x = muestra_num, y = p.value)) +
  geom_point() +
  geom_hline(yintercept = 0.05, color = "red", lty = 2)