1. Cargar librerias
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(mosaic)
## Warning: package 'mosaic' was built under R version 4.0.3
## Registered S3 method overwritten by 'mosaic':
## method from
## fortify.SpatialPolygonsDataFrame ggplot2
##
## The 'mosaic' package masks several functions from core packages in order to add
## additional features. The original behavior of these functions should not be affected by this.
##
## Attaching package: 'mosaic'
## The following object is masked from 'package:Matrix':
##
## mean
## The following object is masked from 'package:ggplot2':
##
## stat
## The following objects are masked from 'package:dplyr':
##
## count, do, tally
## The following objects are masked from 'package:stats':
##
## binom.test, cor, cor.test, cov, fivenum, IQR, median, prop.test,
## quantile, sd, t.test, var
## The following objects are masked from 'package:base':
##
## max, mean, min, prod, range, sample, sum
library(readr)
library(ggplot2) # Para gráficos
library(knitr) # Para formateo de datos
library(fdth) # Para tablas de frecuencias
##
## Attaching package: 'fdth'
## The following objects are masked from 'package:mosaic':
##
## sd, var
## The following objects are masked from 'package:stats':
##
## sd, var
library(gtools) # Para combinaciones y permutaciones
##
## Attaching package: 'gtools'
## The following object is masked from 'package:mosaic':
##
## logit
library(Rmpfr) # Para factoriales de números muy grandes
## Warning: package 'Rmpfr' was built under R version 4.0.3
## Loading required package: gmp
## Warning: package 'gmp' was built under R version 4.0.3
##
## Attaching package: 'gmp'
## The following object is masked from 'package:mosaic':
##
## factorize
## The following objects are masked from 'package:Matrix':
##
## crossprod, tcrossprod
## The following objects are masked from 'package:base':
##
## %*%, apply, crossprod, matrix, tcrossprod
## C code of R package 'Rmpfr': GMP using 64 bits per limb
##
## Attaching package: 'Rmpfr'
## The following object is masked from 'package:gmp':
##
## outer
## The following objects are masked from 'package:stats':
##
## dbinom, dgamma, dnbinom, dnorm, dpois, pnorm
## The following objects are masked from 'package:base':
##
## cbind, pmax, pmin, rbind
2. Cargar datos
N <- 1000000;
edad.poblacion <- round(rnorm(N, mean = 35, sd = 5), 0)
summary(edad.poblacion)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 13 32 35 35 38 61
paste("El valor de edad de una pobacion. Los primeros cincuenta valores ...")
## [1] "El valor de edad de una pobacion. Los primeros cincuenta valores ..."
head(edad.poblacion, 50)
## [1] 35 34 21 33 41 23 44 31 45 32 39 42 34 35 32 33 30 35 39 34 41 37 35 35 33
## [26] 37 33 37 32 32 28 36 45 42 34 50 35 35 35 29 37 30 46 29 39 27 35 33 29 37
paste("El valor de edad de una pobacion. Los últimos cincuenta valores ...")
## [1] "El valor de edad de una pobacion. Los últimos cincuenta valores ..."
tail(edad.poblacion, 50)
## [1] 36 37 35 36 31 36 34 30 34 38 34 39 36 38 20 35 41 36 28 43 28 34 29 42 32
## [26] 43 41 37 44 37 22 36 31 29 30 33 36 36 39 32 38 42 29 40 38 39 31 36 45 41
media.pob <- mean(edad.poblacion)
desv.std <- sd(edad.poblacion)
paste("Los parámetros de la media y desviación estándard de la población")
## [1] "Los parámetros de la media y desviación estándard de la población"
media.pob; desv.std
## [1] 34.99536
## [1] 5.011074
Visualizando la población y la muestra
hist(edad.poblacion, main = "Histrograma de la edad de la población")

hist(muestras$m1, main = "Histrograma de la edad de la muestra 1", ylab = "Edades", xlab="Observacaiones")

hist(muestras$m2, main = "Histrograma de la edad de la muestra 2", ylab = "Edades", xlab="Observacaiones")

hist(muestras$m3, main = "Histrograma de la edad de la muestra 3", ylab = "Edades", xlab="Observacaiones")

hist(muestras$m4, main = "Histrograma de la edad de la muestra 4", ylab = "Edades", xlab="Observacaiones")

hist(muestras$m5, main = "Histrograma de la edad de la muestra 5", ylab = "Edades", xlab="Observacaiones")

options(scipen = 999) # Para mostrar notación normal y no científica en el valor de N: 1e+06
N; n;
## [1] 1000000
## [1] 500
options(scipen = 0) # Regresa a notación numérica normal
summary(edad.poblacion)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 13 32 35 35 38 61
media.pob; desv.std
## [1] 34.99536
## [1] 5.011074
N <- 10; n <- 2
edad.poblacion <- round(rnorm(N, mean = 35, sd = 5), 0)
edad.poblacion
## [1] 37 44 43 30 39 38 40 39 28 38
media.pob <- mean(edad.poblacion)
desv.std <- sd(edad.poblacion)
media.pob; desv.std
## [1] 37.6
## [1] 5.059644
n.combinaciones <- factorialMpfr(N) / (factorialMpfr(n) * (factorialMpfr(N-n)))
as.integer(n.combinaciones)
## [1] 45
muestras <- cbind(1:as.integer(n.combinaciones))
muestras <- cbind(muestras, combinations(N, n, 1:N))
muestras <- cbind(muestras, edad.poblacion[muestras[,2]], edad.poblacion[muestras[,3]])
medias <- 0
error <- 0
for(i in 1:as.integer(n.combinaciones)) {
medias[i] <- mean(muestras[i,c(4,5)])
error[i] <- medias[i] - media.pob
}
muestras <- cbind(muestras, medias)
muestras <- cbind(muestras, media.pob)
muestras <- cbind(muestras, error)
muestras <- data.frame(muestras)
colnames(muestras) <- c("Muestra", "Pos.1", "Pos.2", "Valor.1", "Valor.2", "Media muestra", "Media pob.", "Error")
kable(muestras, caption = "Las muestras")
Las muestras
| 1 |
1 |
2 |
37 |
44 |
40.5 |
37.6 |
2.9 |
| 2 |
1 |
3 |
37 |
43 |
40.0 |
37.6 |
2.4 |
| 3 |
1 |
4 |
37 |
30 |
33.5 |
37.6 |
-4.1 |
| 4 |
1 |
5 |
37 |
39 |
38.0 |
37.6 |
0.4 |
| 5 |
1 |
6 |
37 |
38 |
37.5 |
37.6 |
-0.1 |
| 6 |
1 |
7 |
37 |
40 |
38.5 |
37.6 |
0.9 |
| 7 |
1 |
8 |
37 |
39 |
38.0 |
37.6 |
0.4 |
| 8 |
1 |
9 |
37 |
28 |
32.5 |
37.6 |
-5.1 |
| 9 |
1 |
10 |
37 |
38 |
37.5 |
37.6 |
-0.1 |
| 10 |
2 |
3 |
44 |
43 |
43.5 |
37.6 |
5.9 |
| 11 |
2 |
4 |
44 |
30 |
37.0 |
37.6 |
-0.6 |
| 12 |
2 |
5 |
44 |
39 |
41.5 |
37.6 |
3.9 |
| 13 |
2 |
6 |
44 |
38 |
41.0 |
37.6 |
3.4 |
| 14 |
2 |
7 |
44 |
40 |
42.0 |
37.6 |
4.4 |
| 15 |
2 |
8 |
44 |
39 |
41.5 |
37.6 |
3.9 |
| 16 |
2 |
9 |
44 |
28 |
36.0 |
37.6 |
-1.6 |
| 17 |
2 |
10 |
44 |
38 |
41.0 |
37.6 |
3.4 |
| 18 |
3 |
4 |
43 |
30 |
36.5 |
37.6 |
-1.1 |
| 19 |
3 |
5 |
43 |
39 |
41.0 |
37.6 |
3.4 |
| 20 |
3 |
6 |
43 |
38 |
40.5 |
37.6 |
2.9 |
| 21 |
3 |
7 |
43 |
40 |
41.5 |
37.6 |
3.9 |
| 22 |
3 |
8 |
43 |
39 |
41.0 |
37.6 |
3.4 |
| 23 |
3 |
9 |
43 |
28 |
35.5 |
37.6 |
-2.1 |
| 24 |
3 |
10 |
43 |
38 |
40.5 |
37.6 |
2.9 |
| 25 |
4 |
5 |
30 |
39 |
34.5 |
37.6 |
-3.1 |
| 26 |
4 |
6 |
30 |
38 |
34.0 |
37.6 |
-3.6 |
| 27 |
4 |
7 |
30 |
40 |
35.0 |
37.6 |
-2.6 |
| 28 |
4 |
8 |
30 |
39 |
34.5 |
37.6 |
-3.1 |
| 29 |
4 |
9 |
30 |
28 |
29.0 |
37.6 |
-8.6 |
| 30 |
4 |
10 |
30 |
38 |
34.0 |
37.6 |
-3.6 |
| 31 |
5 |
6 |
39 |
38 |
38.5 |
37.6 |
0.9 |
| 32 |
5 |
7 |
39 |
40 |
39.5 |
37.6 |
1.9 |
| 33 |
5 |
8 |
39 |
39 |
39.0 |
37.6 |
1.4 |
| 34 |
5 |
9 |
39 |
28 |
33.5 |
37.6 |
-4.1 |
| 35 |
5 |
10 |
39 |
38 |
38.5 |
37.6 |
0.9 |
| 36 |
6 |
7 |
38 |
40 |
39.0 |
37.6 |
1.4 |
| 37 |
6 |
8 |
38 |
39 |
38.5 |
37.6 |
0.9 |
| 38 |
6 |
9 |
38 |
28 |
33.0 |
37.6 |
-4.6 |
| 39 |
6 |
10 |
38 |
38 |
38.0 |
37.6 |
0.4 |
| 40 |
7 |
8 |
40 |
39 |
39.5 |
37.6 |
1.9 |
| 41 |
7 |
9 |
40 |
28 |
34.0 |
37.6 |
-3.6 |
| 42 |
7 |
10 |
40 |
38 |
39.0 |
37.6 |
1.4 |
| 43 |
8 |
9 |
39 |
28 |
33.5 |
37.6 |
-4.1 |
| 44 |
8 |
10 |
39 |
38 |
38.5 |
37.6 |
0.9 |
| 45 |
9 |
10 |
28 |
38 |
33.0 |
37.6 |
-4.6 |
paste("La media poblacional es: ", media.pob, " y la media de la edad de la distribución muestral es: ", mean(muestras$`Media muestra`))
## [1] "La media poblacional es: 37.6 y la media de la edad de la distribución muestral es: 37.6"