Está en la página 1de 12

Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.

org/0000-0002-3962-5433

Tamaño de la muestra con R Studio


TABLA DE CONTENIDOS
1. Cree un archivo de R Mark Down con la siguiente nomenglatura, Trabajo_Final_Primer Apellido_Primer Nombre . 1
2. Importe la base de datos “Segunda-vuelta-1.sav”........................................................................................................ 1
3. A partir de la base de datos “Segunda-vuelta-1.sav” ................................................................................................... 3
3.1. Establezca el identificador para cada observación (ID= Provincia + Cantón) ............................................................ 3
3.2. Calcule el número de Nh ............................................................................................................................................ 4
3.3. Genere una base de datos con información del número de Nh acuerdo con el ID y calcule el tamaño de muestra
nh en cada Nh (considere un tamaño de muestra global de 2500).................................................................................. 4
3.4. Seleccione un M.A.S en cada estrato con nh proporcional al tamaño del estrato .................................................... 8
3.5. Calcule la estimación para la proporción de votos de cada candidato a presidente del Ecuador. Considere la
estimación a través de estimadores de razón combinado. ............................................................................................ 10

1. Cree un archivo de R Mark Down con la siguiente nomenglatura,


Trabajo_Final_Primer Apellido_Primer Nombre

2. Importe la base de datos “Segunda-vuelta-1.sav”


library(haven)
Segunda_vuelta_1 <- read_sav("C:/Users/Usuario1/Desktop/Maestría/Módulo 9 Técnicas del
muestreo/tarea final/Segunda-vuelta-1.sav")
View(Segunda_vuelta_1)
# Se transforma los votos de los candidatos que están en filas
# para que consten a nivel de columnas

library(tidyr)
datos=Segunda_vuelta_1 %>% pivot_wider(id_cols=c(DIGNIDAD_NOMBRE,
PROVINCIA_CODIGO, PROVINCIA_NOMBRE,
CIRCUNSCRIPCION_CODIGO, CIRCUNSCRIPCION_NOMBRE,
CANTON_CODIGO, CANTON_NOMBRE,
PARROQUIA_CODIGO, PARROQUIA_NOMBRE,
JUNTA_SEXO, SUFRAGANTES, BLANCOS, NULOS) , names_from = CANDIDATO_CODIGO_RESULTADO
S, values_from=VOTOS)

names(datos)

## [1] "DIGNIDAD_NOMBRE" "PROVINCIA_CODIGO" "PROVINCIA_NOMBRE"


## [4] "CIRCUNSCRIPCION_CODIGO" "CIRCUNSCRIPCION_NOMBRE" "CANTON_CODIGO"
## [7] "CANTON_NOMBRE" "PARROQUIA_CODIGO" "PARROQUIA_NOMBRE"

pág. 1
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

## [10] "JUNTA_SEXO" "SUFRAGANTES" "BLANCOS"


## [13] "NULOS" "10001021" "10001030"

str(datos)

## tibble [2,610 x 15] (S3: tbl_df/tbl/data.frame)


## $ DIGNIDAD_NOMBRE : dbl+lbl [1:2610] 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1,...
## ..@ format.spss : chr "F1.0"
## ..@ display_width: int 47
## ..@ labels : Named num [1:4] 1 7 8 9
## .. ..- attr(*, "names")= chr [1:4] "PRESIDENTA/E Y VICEPRESIDENTA/E" "ASAMBLEÍST
AS PROVINCIALES Y DEL EXTERIOR" "PARLAMENTARIOS ANDINOS" "ASAMBLEÍSTAS NACIONALES"
## $ PROVINCIA_CODIGO : num [1:2610] 1 1 1 1 1 1 1 1 1 1 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## $ PROVINCIA_NOMBRE : dbl+lbl [1:2610] 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1,...
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 13
## ..@ labels : Named num [1:27] 1 2 3 4 5 6 7 8 9 10 ...
## .. ..- attr(*, "names")= chr [1:27] "AZUAY" "BOLIVAR" "CAÑAR" "CARCHI" ...
## $ CIRCUNSCRIPCION_CODIGO: num [1:2610] 0 0 0 0 0 0 0 0 0 0 ...
## ..- attr(*, "format.spss")= chr "F1.0"
## ..- attr(*, "display_width")= int 7
## $ CIRCUNSCRIPCION_NOMBRE: dbl+lbl [1:2610] 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 1...
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 34
## ..@ labels : Named num [1:34] 10 20 30 40 50 60 70 80 91 92 ...
## .. ..- attr(*, "names")= chr [1:34] "-" "-" "-" "-" ...
## $ CANTON_CODIGO : num [1:2610] 260 260 260 260 260 260 260 260 260 260 ...
## ..- attr(*, "format.spss")= chr "F3.0"
## ..- attr(*, "display_width")= int 5
## $ CANTON_NOMBRE : dbl+lbl [1:2610] 260, 260, 260, 260, 260, 260, 260, 260,
260, 260, 260...
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 10
## ..@ labels : Named num [1:261] 5 10 11 15 20 21 25 30 31 35 ...
## .. ..- attr(*, "names")= chr [1:261] "TULCAN" "MONTUFAR" "ALEMANIA" "ESPEJO" ...
## $ PARROQUIA_CODIGO : num [1:2610] 285 285 730 730 845 845 860 860 905 905 ...
## ..- attr(*, "format.spss")= chr "F4.0"
## ..- attr(*, "display_width")= int 6
## $ PARROQUIA_NOMBRE : dbl+lbl [1:2610] 285, 285, 730, 730, 845, 845, 86
0, 860, 905,...
## ..@ format.spss : chr "F8.0"
## ..@ display_width: int 14
## ..@ labels : Named num [1:1299] 5 10 15 20 25 30 35 40 45 50 ...
## .. ..- attr(*, "names")= chr [1:1299] "ABANIN" "ABDON CALDERON" "ABDON CALDERON
/ LA UNION" "ACHUPALLAS" ...
## $ JUNTA_SEXO : chr [1:2610] "F" "M" "F" "M" ...
## ..- attr(*, "format.spss")= chr "A1"
## ..- attr(*, "display_width")= int 6
## $ SUFRAGANTES : num [1:2610] 9592 8370 2548 1917 498 ...

pág. 2
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

## ..- attr(*, "format.spss")= chr "F8.0"


## ..- attr(*, "display_width")= int 17
## $ BLANCOS : num [1:2610] 259 234 118 58 30 33 31 12 48 25 ...
## ..- attr(*, "format.spss")= chr "F8.0"
## ..- attr(*, "display_width")= int 13
## $ NULOS : num [1:2610] 3424 2553 1315 940 179 ...
## ..- attr(*, "format.spss")= chr "F8.0"
## ..- attr(*, "display_width")= int 11
## $ 10001021 : num [1:2610] 3172 3036 583 494 111 ...
## ..- attr(*, "format.spss")= chr "F8.0"
## ..- attr(*, "display_width")= int 21
## $ 10001030 : num [1:2610] 2735 2546 532 425 178 ...
## ..- attr(*, "format.spss")= chr "F8.0"
## ..- attr(*, "display_width")= int 21

# Se rectifican los nombres de la variables añadidas

names(datos)[14]<-"GL" # Guillermo Lasso


names(datos)[15]<-"AA" # Andrés Araúz

# Se añade una columna con el total de los votos debido a que al sumar los votos de
los candidatos, blancos y nulos no coincide con el total de sufragantes

datos$Total = rowSums (datos[ , 12:15])

3. A partir de la base de datos “Segunda-vuelta-1.sav”

3.1. Establezca el identificador para cada observación (ID= Provincia + Cantón)


N<-length(datos$PROVINCIA_CODIGO)
n<-2500 #tamaño de la muestra global

x<-datos$PROVINCIA_CODIGO
y<-datos$CANTON_CODIGO

# Para crear un solo indentificador se crea "estr"


# Se trabaja con el identificador del Provincia y del Cantón

estr<-x*1000+y
head(estr) # primeros 6 registros

## [1] 1260 1260 1260 1260 1260 1260

tail(estr) # últimos 6 registros

## [1] 28391 28391 28401 28401 28441 28441

pág. 3
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

3.2. Calcule el número de Nh


# Ayuda para el muestreo aleatorio simple
library(sampling)

# Para una mejor enumeración se enumera el identificador


estrato<-cleanstrata(estr)
head(estrato)

## [1] 1 1 1 1 1 1

tail(estrato)

## [1] 259 259 260 260 261 261

# Se forman 261 estratos

# Se incluye la nueva variable a la base de datos


base_c<-cbind(datos,estrato)

# Tamaño de población por estrato.

# Se saca la población para cada estrato


Nh<-tabulate(estrato)
Nh

## [1] 72 18 6 16 14 8 4 4 4 4 8 10 6 4 2 22 10 14
## [19] 4 2 2 4 24 24 2 10 6 2 4 22 14 10 8 12 4 30
## [37] 14 12 8 8 10 10 2 32 22 12 6 20 10 14 2 2 16 24
## [55] 20 20 18 10 10 2 8 10 12 10 4 4 26 12 32 18 12 26
## [73] 10 2 4 6 45 8 14 2 8 6 16 10 2 2 2 6 2 4
## [91] 6 2 2 2 2 2 2 2 24 22 20 12 8 12 12 14 38 22
## [109] 18 4 10 14 10 12 10 14 6 8 6 10 16 2 6 4 6 4
## [127] 22 10 2 4 2 4 2 6 34 12 2 14 23 8 2 12 9 2
## [145] 4 18 6 2 10 12 6 6 8 2 6 18 20 12 14 8 6 10
## [163] 4 2 4 10 10 12 16 2 12 8 28 6 4 4 133 16 16 10
## [181] 12 4 2 2 4 54 20 18 10 8 6 2 4 8 16 12 10 6
## [199] 6 8 6 6 6 6 4 16 6 10 12 12 8 8 12 24 18 12
## [217] 28 8 14 16 2 2 2 2 2 2 6 12 2 2 2 2 6 2
## [235] 2 2 2 2 2 2 4 22 2 2 4 4 2 2 2 2 2 2
## [253] 4 2 2 4 2 2 2 2 2

# Para corroborar el total de observaciones se suma las poblaciones de cada estrato y


debe dar el total
sum(Nh)

## [1] 2610

3.3. Genere una base de datos con información del número de Nh acuerdo con el
ID y calcule el tamaño de muestra nh en cada Nh (considere un tamaño de muestra
global de 2500)
# Afijación proporcional
(wh<-Nh/N)
pág. 4
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

## [1] 0.0275862069 0.0068965517 0.0022988506 0.0061302682 0.0053639847


## [6] 0.0030651341 0.0015325670 0.0015325670 0.0015325670 0.0015325670
## [11] 0.0030651341 0.0038314176 0.0022988506 0.0015325670 0.0007662835
## [16] 0.0084291188 0.0038314176 0.0053639847 0.0015325670 0.0007662835
## [21] 0.0007662835 0.0015325670 0.0091954023 0.0091954023 0.0007662835
## [26] 0.0038314176 0.0022988506 0.0007662835 0.0015325670 0.0084291188
## [31] 0.0053639847 0.0038314176 0.0030651341 0.0045977011 0.0015325670
## [36] 0.0114942529 0.0053639847 0.0045977011 0.0030651341 0.0030651341
## [41] 0.0038314176 0.0038314176 0.0007662835 0.0122605364 0.0084291188
## [46] 0.0045977011 0.0022988506 0.0076628352 0.0038314176 0.0053639847
## [51] 0.0007662835 0.0007662835 0.0061302682 0.0091954023 0.0076628352
## [56] 0.0076628352 0.0068965517 0.0038314176 0.0038314176 0.0007662835
## [61] 0.0030651341 0.0038314176 0.0045977011 0.0038314176 0.0015325670
## [66] 0.0015325670 0.0099616858 0.0045977011 0.0122605364 0.0068965517
## [71] 0.0045977011 0.0099616858 0.0038314176 0.0007662835 0.0015325670
## [76] 0.0022988506 0.0172413793 0.0030651341 0.0053639847 0.0007662835
## [81] 0.0030651341 0.0022988506 0.0061302682 0.0038314176 0.0007662835
## [86] 0.0007662835 0.0007662835 0.0022988506 0.0007662835 0.0015325670
## [91] 0.0022988506 0.0007662835 0.0007662835 0.0007662835 0.0007662835
## [96] 0.0007662835 0.0007662835 0.0007662835 0.0091954023 0.0084291188
## [101] 0.0076628352 0.0045977011 0.0030651341 0.0045977011 0.0045977011
## [106] 0.0053639847 0.0145593870 0.0084291188 0.0068965517 0.0015325670
## [111] 0.0038314176 0.0053639847 0.0038314176 0.0045977011 0.0038314176
## [116] 0.0053639847 0.0022988506 0.0030651341 0.0022988506 0.0038314176
## [121] 0.0061302682 0.0007662835 0.0022988506 0.0015325670 0.0022988506
## [126] 0.0015325670 0.0084291188 0.0038314176 0.0007662835 0.0015325670
## [131] 0.0007662835 0.0015325670 0.0007662835 0.0022988506 0.0130268199
## [136] 0.0045977011 0.0007662835 0.0053639847 0.0088122605 0.0030651341
## [141] 0.0007662835 0.0045977011 0.0034482759 0.0007662835 0.0015325670
## [146] 0.0068965517 0.0022988506 0.0007662835 0.0038314176 0.0045977011
## [151] 0.0022988506 0.0022988506 0.0030651341 0.0007662835 0.0022988506
## [156] 0.0068965517 0.0076628352 0.0045977011 0.0053639847 0.0030651341
## [161] 0.0022988506 0.0038314176 0.0015325670 0.0007662835 0.0015325670
## [166] 0.0038314176 0.0038314176 0.0045977011 0.0061302682 0.0007662835
## [171] 0.0045977011 0.0030651341 0.0107279693 0.0022988506 0.0015325670
## [176] 0.0015325670 0.0509578544 0.0061302682 0.0061302682 0.0038314176
## [181] 0.0045977011 0.0015325670 0.0007662835 0.0007662835 0.0015325670
## [186] 0.0206896552 0.0076628352 0.0068965517 0.0038314176 0.0030651341
## [191] 0.0022988506 0.0007662835 0.0015325670 0.0030651341 0.0061302682
## [196] 0.0045977011 0.0038314176 0.0022988506 0.0022988506 0.0030651341
## [201] 0.0022988506 0.0022988506 0.0022988506 0.0022988506 0.0015325670
## [206] 0.0061302682 0.0022988506 0.0038314176 0.0045977011 0.0045977011
## [211] 0.0030651341 0.0030651341 0.0045977011 0.0091954023 0.0068965517
## [216] 0.0045977011 0.0107279693 0.0030651341 0.0053639847 0.0061302682
## [221] 0.0007662835 0.0007662835 0.0007662835 0.0007662835 0.0007662835
## [226] 0.0007662835 0.0022988506 0.0045977011 0.0007662835 0.0007662835
## [231] 0.0007662835 0.0007662835 0.0022988506 0.0007662835 0.0007662835
## [236] 0.0007662835 0.0007662835 0.0007662835 0.0007662835 0.0007662835
## [241] 0.0015325670 0.0084291188 0.0007662835 0.0007662835 0.0015325670
## [246] 0.0015325670 0.0007662835 0.0007662835 0.0007662835 0.0007662835
## [251] 0.0007662835 0.0007662835 0.0015325670 0.0007662835 0.0007662835
## [256] 0.0015325670 0.0007662835 0.0007662835 0.0007662835 0.0007662835
## [261] 0.0007662835
pág. 5
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

# Como comprobación el resultado debe sumar 1


sum(wh)

## [1] 1

rh<-wh*n
rh

## [1] 68.965517 17.241379 5.747126 15.325670 13.409962 7.662835


## [7] 3.831418 3.831418 3.831418 3.831418 7.662835 9.578544
## [13] 5.747126 3.831418 1.915709 21.072797 9.578544 13.409962
## [19] 3.831418 1.915709 1.915709 3.831418 22.988506 22.988506
## [25] 1.915709 9.578544 5.747126 1.915709 3.831418 21.072797
## [31] 13.409962 9.578544 7.662835 11.494253 3.831418 28.735632
## [37] 13.409962 11.494253 7.662835 7.662835 9.578544 9.578544
## [43] 1.915709 30.651341 21.072797 11.494253 5.747126 19.157088
## [49] 9.578544 13.409962 1.915709 1.915709 15.325670 22.988506
## [55] 19.157088 19.157088 17.241379 9.578544 9.578544 1.915709
## [61] 7.662835 9.578544 11.494253 9.578544 3.831418 3.831418
## [67] 24.904215 11.494253 30.651341 17.241379 11.494253 24.904215
## [73] 9.578544 1.915709 3.831418 5.747126 43.103448 7.662835
## [79] 13.409962 1.915709 7.662835 5.747126 15.325670 9.578544
## [85] 1.915709 1.915709 1.915709 5.747126 1.915709 3.831418
## [91] 5.747126 1.915709 1.915709 1.915709 1.915709 1.915709
## [97] 1.915709 1.915709 22.988506 21.072797 19.157088 11.494253
## [103] 7.662835 11.494253 11.494253 13.409962 36.398467 21.072797
## [109] 17.241379 3.831418 9.578544 13.409962 9.578544 11.494253
## [115] 9.578544 13.409962 5.747126 7.662835 5.747126 9.578544
## [121] 15.325670 1.915709 5.747126 3.831418 5.747126 3.831418
## [127] 21.072797 9.578544 1.915709 3.831418 1.915709 3.831418
## [133] 1.915709 5.747126 32.567050 11.494253 1.915709 13.409962
## [139] 22.030651 7.662835 1.915709 11.494253 8.620690 1.915709
## [145] 3.831418 17.241379 5.747126 1.915709 9.578544 11.494253
## [151] 5.747126 5.747126 7.662835 1.915709 5.747126 17.241379
## [157] 19.157088 11.494253 13.409962 7.662835 5.747126 9.578544
## [163] 3.831418 1.915709 3.831418 9.578544 9.578544 11.494253
## [169] 15.325670 1.915709 11.494253 7.662835 26.819923 5.747126
## [175] 3.831418 3.831418 127.394636 15.325670 15.325670 9.578544
## [181] 11.494253 3.831418 1.915709 1.915709 3.831418 51.724138
## [187] 19.157088 17.241379 9.578544 7.662835 5.747126 1.915709
## [193] 3.831418 7.662835 15.325670 11.494253 9.578544 5.747126
## [199] 5.747126 7.662835 5.747126 5.747126 5.747126 5.747126
## [205] 3.831418 15.325670 5.747126 9.578544 11.494253 11.494253
## [211] 7.662835 7.662835 11.494253 22.988506 17.241379 11.494253
## [217] 26.819923 7.662835 13.409962 15.325670 1.915709 1.915709
## [223] 1.915709 1.915709 1.915709 1.915709 5.747126 11.494253
## [229] 1.915709 1.915709 1.915709 1.915709 5.747126 1.915709
## [235] 1.915709 1.915709 1.915709 1.915709 1.915709 1.915709
## [241] 3.831418 21.072797 1.915709 1.915709 3.831418 3.831418
## [247] 1.915709 1.915709 1.915709 1.915709 1.915709 1.915709
## [253] 3.831418 1.915709 1.915709 3.831418 1.915709 1.915709
## [259] 1.915709 1.915709 1.915709

sum(rh)
pág. 6
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

## [1] 2500

# Se necesita variable cuantitativa de tipo discreto


nh<-round(rh)
nh

## [1] 69 17 6 15 13 8 4 4 4 4 8 10 6 4 2 21 10 13
## [19] 4 2 2 4 23 23 2 10 6 2 4 21 13 10 8 11 4 29
## [37] 13 11 8 8 10 10 2 31 21 11 6 19 10 13 2 2 15 23
## [55] 19 19 17 10 10 2 8 10 11 10 4 4 25 11 31 17 11 25
## [73] 10 2 4 6 43 8 13 2 8 6 15 10 2 2 2 6 2 4
## [91] 6 2 2 2 2 2 2 2 23 21 19 11 8 11 11 13 36 21
## [109] 17 4 10 13 10 11 10 13 6 8 6 10 15 2 6 4 6 4
## [127] 21 10 2 4 2 4 2 6 33 11 2 13 22 8 2 11 9 2
## [145] 4 17 6 2 10 11 6 6 8 2 6 17 19 11 13 8 6 10
## [163] 4 2 4 10 10 11 15 2 11 8 27 6 4 4 127 15 15 10
## [181] 11 4 2 2 4 52 19 17 10 8 6 2 4 8 15 11 10 6
## [199] 6 8 6 6 6 6 4 15 6 10 11 11 8 8 11 23 17 11
## [217] 27 8 13 15 2 2 2 2 2 2 6 11 2 2 2 2 6 2
## [235] 2 2 2 2 2 2 4 21 2 2 4 4 2 2 2 2 2 2
## [253] 4 2 2 4 2 2 2 2 2

# Se confirma que se tengan las 2500 muestras pero por efecto de redondeo toma 2515
(sum(nh))

## [1] 2515

hh<-1:261
resultado<-as.data.frame(cbind(hh,Nh,nh))
sum(resultado$Nh) # Tamaño del estrato

## [1] 2610

sum(resultado$nh) # Tamaño de la muestra por estrato

## [1] 2515

# Se crea una nueva base con las variables calculadas

base_global<-cbind(base_c,resultado[base_c$estrato,2],
resultado[base_c$estrato,3])

# Se rectifican los nombres de la variables añadidas

names(base_global)[18]<-"Nh" # Población por estrato


names(base_global)[19]<-"nh" # Tamaño de la muestra por estrato

pág. 7
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

3.4. Seleccione un M.A.S en cada estrato con nh proporcional al tamaño del


estrato
# Se calcula el factor de expansión
# factor de expansión es el inverso dy e la probabilidad de selección

nh<-resultado$nh
n<-sum(nh)
summary(nh)

## Min. 1st Qu. Median Mean 3rd Qu. Max.


## 2.000 2.000 8.000 9.636 11.000 127.000

Nh <- resultado$Nh
strat <- base_global$estrato

# Calcular la probabilidad proporcional al tamaño


# install.packages("pps")
library(pps)

# Muestreo estratificado aleatorio


mm <- stratsrs(strat,nh)
muestra <- base_global[mm,]
#Ya se obtuvo la muestra aleatoria

fexp<-muestra$Nh/muestra$nh

muestra<-cbind(muestra,fexp)
sum(muestra$fexp)

## [1] 2610

frec<-table(muestra$estrato)
sum(frec==nh)

## [1] 261

# M.A.S. por estrato

library(sampling)

# Se hace un muestreo estratificado con la función strata


st<-sampling:::strata(base_global, c("estrato"), size = c(nh),
method = "srswor")

# Los estimadores puede ser por la media, totales, varianza o de proporciones

# El factor de expansión permite extrapolar para toda la población. Es un ponderador


para poder concluir para toda la población

# Se saca la muestra de cada uno de los estratos

muestra1<-getdata(base_global,st)

pág. 8
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

fexp1<-1/muestra1$Prob
sum(fexp1)

## [1] 2610

muestra1 <- cbind(muestra1,fexp1)


head(muestra1)

## DIGNIDAD_NOMBRE PROVINCIA_CODIGO PROVINCIA_NOMBRE CIRCUNSCRIPCION_CODIGO


## 1 1 1 1 0
## 2 1 1 1 0
## 3 1 1 1 0
## 4 1 1 1 0
## 5 1 1 1 0
## 6 1 1 1 0
## CIRCUNSCRIPCION_NOMBRE CANTON_CODIGO CANTON_NOMBRE PARROQUIA_CODIGO
## 1 10 260 260 285
## 2 10 260 260 285
## 3 10 260 260 730
## 4 10 260 260 730
## 5 10 260 260 845
## 6 10 260 260 845
## PARROQUIA_NOMBRE JUNTA_SEXO SUFRAGANTES BLANCOS NULOS GL AA Total Nh nh
## 1 285 F 9592 259 3424 3172 2735 9590 72 69
## 2 285 M 8370 234 2553 3036 2546 8369 72 69
## 3 730 F 2548 118 1315 583 532 2548 72 69
## 4 730 M 1917 58 940 494 425 1917 72 69
## 5 845 F 498 30 179 111 178 498 72 69
## 6 845 M 494 33 231 73 157 494 72 69
## estrato ID_unit Prob Stratum fexp1
## 1 1 1 0.9583333 1 1.043478
## 2 1 2 0.9583333 1 1.043478
## 3 1 3 0.9583333 1 1.043478
## 4 1 4 0.9583333 1 1.043478
## 5 1 5 0.9583333 1 1.043478
## 6 1 6 0.9583333 1 1.043478

nh1<-tabulate(muestra1$estrato)
table(muestra1$estrato)

##
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
## 69 17 6 15 13 8 4 4 4 4 8 10 6 4 2 21 10 13 4 2
## 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
## 2 4 23 23 2 10 6 2 4 21 13 10 8 11 4 29 13 11 8 8
## 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
## 10 10 2 31 21 11 6 19 10 13 2 2 15 23 19 19 17 10 10 2
## 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
## 8 10 11 10 4 4 25 11 31 17 11 25 10 2 4 6 43 8 13 2
## 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
## 8 6 15 10 2 2 2 6 2 4 6 2 2 2 2 2 2 2 23 21
## 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
## 19 11 8 11 11 13 36 21 17 4 10 13 10 11 10 13 6 8 6 10
## 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
pág. 9
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

## 15 2 6 4 6 4 21 10 2 4 2 4 2 6 33 11 2 13 22 8
## 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
## 2 11 9 2 4 17 6 2 10 11 6 6 8 2 6 17 19 11 13 8
## 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## 6 10 4 2 4 10 10 11 15 2 11 8 27 6 4 4 127 15 15 10
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
## 11 4 2 2 4 52 19 17 10 8 6 2 4 8 15 11 10 6 6 8
## 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
## 6 6 6 6 4 15 6 10 11 11 8 8 11 23 17 11 27 8 13 15
## 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
## 2 2 2 2 2 2 6 11 2 2 2 2 6 2 2 2 2 2 2 2
## 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
## 4 21 2 2 4 4 2 2 2 2 2 2 4 2 2 4 2 2 2 2
## 261
## 2

sum(nh==nh1)

## [1] 261

3.5. Calcule la estimación para la proporción de votos de cada candidato a


presidente del Ecuador. Considere la estimación a través de estimadores de razón
combinado.
# Estimación de la proporción de los votos de los candidatos a presidencia

attach(muestra)

## The following objects are masked _by_ .GlobalEnv:


##
## estrato, fexp, nh, Nh

head(muestra,1)

## DIGNIDAD_NOMBRE PROVINCIA_CODIGO PROVINCIA_NOMBRE CIRCUNSCRIPCION_CODIGO


## 7 1 1 1 0
## CIRCUNSCRIPCION_NOMBRE CANTON_CODIGO CANTON_NOMBRE PARROQUIA_CODIGO
## 7 10 260 260 860
## PARROQUIA_NOMBRE JUNTA_SEXO SUFRAGANTES BLANCOS NULOS GL AA Total estrato
## 7 860 F 1018 31 460 252 275 1018 1
## Nh nh fexp
## 7 72 69 1.043478

# tamaño de la poblacion
N <- sum(fexp)

# tamaño de muestra
n <- length(PROVINCIA_CODIGO)

# número de estrato
L <- max(estrato)
pág. 10
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

# Estimación de la proporción de los votos

attach(muestra)

## The following objects are masked _by_ .GlobalEnv:


##
## estrato, fexp, nh, Nh

## The following objects are masked from muestra (pos = 3):


##
## AA, BLANCOS, CANTON_CODIGO, CANTON_NOMBRE, CIRCUNSCRIPCION_CODIGO,
## CIRCUNSCRIPCION_NOMBRE, DIGNIDAD_NOMBRE, estrato, fexp, GL,
## JUNTA_SEXO, nh, Nh, NULOS, PARROQUIA_CODIGO, PARROQUIA_NOMBRE,
## PROVINCIA_CODIGO, PROVINCIA_NOMBRE, SUFRAGANTES, Total

# La unidad de análisis es al individuo votante


# La unidad de observación son los recintos

N<-sum(fexp)
n<-length(PROVINCIA_CODIGO)
L<-max(estrato)

library(survey)

## Loading required package: grid

## Loading required package: Matrix

##
## Attaching package: 'Matrix'

## The following objects are masked from 'package:tidyr':


##
## expand, pack, unpack

## Loading required package: survival

##
## Attaching package: 'survival'

## The following objects are masked from 'package:sampling':


##
## cluster, strata

##
## Attaching package: 'survey'

## The following object is masked from 'package:graphics':


##
## dotchart

# Definiendo el diseño de la muestra sin reemplazo


# fpc (finete population correction) (población finita)
# Especificamos los estratos
# La función trabaja con un diseño de la muestra
pág. 11
Por: Mario Orlando Suárez Ibujés Fecha: 28/03/2024 https://orcid.org/0000-0002-3962-5433

# Se crea un objeto con el diseño muestral

disin <- svydesign(~1,strata=muestra$estrato,


data=muestra,weights
=muestra$fexp,
fpc=muestra$Nh)

# Se muestran los ratios


# Se calculan las estimaciones puntuales

# Estimación de la proporción de razón


# GL/TOTAL DE VOTOS

# El porcentaje del total de votos del candidato Guillermo Lasso GL

svyratio(~GL,~Total,design=disin)

## Ratio estimator: svyratio.survey.design2(~GL, ~Total, design = disin)


## Ratios=
## Total
## GL 0.4289678
## SEs=
## Total
## GL 0.0009193102

# Estimación de la proporción de los votos del candidato Andrés Araúz

svyratio(~AA,~Total,design=disin)

## Ratio estimator: svyratio.survey.design2(~AA, ~Total, design = disin)


## Ratios=
## Total
## AA 0.392787
## SEs=
## Total
## AA 0.0008773851

# PARÁMETRO (Para corroborar)

sum(datos$GL)/sum(datos$Total) #Guillermo Lasso

## [1] 0.4300069

sum(datos$AA)/sum(datos$Total) #Andrés Araúz

## [1] 0.3912294

#Análisis
Los valores reales 0.430069 de Guillermo Lasso y 0.3912294 de Andrés Araúz son muy
similares a las razones obtenidas de 0.4289678 y 0.392787 para Guillermo Lasso y Andrés
Araúz, respectivamente, lo que conllevaría a interpretar que la estimación realizada es
correcta, y que Guillermo Lasso tendría una mayor votación que Andrés Araúz.

pág. 12

También podría gustarte