library(fishmethods)
library(tidyverse)
library(data.table)
library(janitor)
library(survey)
library(sampling)
library(plotly)
Se desea estimar la proporción de votantes a FdT, JxC y FIT (a diputados nacionales) a nivel nacional respecto al total de votos válidos para las elecciones de 14 Noviembre 2021 mediante una muestra aleatoria de tamaño total aproximado de n=480 mesas, mediante muestreo estratificado bietápico. El marco de muestreo se construirá a partir de la tabla MESAS ESCRUTADAS Cierre.csv (tablas publicadas por la DNE).
# 1- levanto data ####
getwd()
## [1] "C:/Users/Lucas/Desktop"
setwd("C:/Users/Lucas/Desktop/Muestreo/TP1")
base = read_csv('MESAS_ESCRUTADAS_Cierre.csv')
base$Agrupacion <- ifelse(base$tipoVoto == 'blancos', 'BLANCOS', base$Agrupacion)
# defino listas
agrupaciones<-c("JUNTOS POR EL CAMBIO",
"FRENTE DE TODOS",
"FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD",
"BLANCOS")
agrupaciones
## [1] "JUNTOS POR EL CAMBIO"
## [2] "FRENTE DE TODOS"
## [3] "FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD"
## [4] "BLANCOS"
# defino votos validos
votos_validos<-c("positivo", "blanco")
nea_noa <- c("Chaco", "Corrientes", "Formosa", "Misiones", "Catamarca", "Jujuy", "Santiago del Estero", "Tucumán", "La Rioja", "Salta")
pba <- c("Buenos Aires")
caba <- c("Ciudad Autónoma de Buenos Aires")
base2<-base%>%
filter(Cargo =="DIPUTADOS NACIONALES" &
tipoVoto %in% votos_validos)%>%
mutate(
region=case_when(
Distrito %in% nea_noa ~"NEA-NOA",
Distrito %in% pba ~"PBA",
Distrito %in% caba ~"CABA",
TRUE ~ "Resto"))%>%
mutate(
Agrupacion = ifelse(tipoVoto == "blancos", "BLANCOS", Agrupacion),
Agrupacion = ifelse(Agrupacion%in%c("JUNTOS","JUNTOS POR EL CAMBIO"), "JUNTOS POR EL CAMBIO", Agrupacion),
codigo_pw = str_c(Mesa, "_", IdDistrito, "_", IdSeccion), # creo un id unico por mesa
codigo_circuitos = str_c(IdCircuito, "_", IdDistrito))%>%# creo un id unico por circuito
group_by(region)%>%
mutate(votos_validos=sum(votos))
# creo marco de muestreo
marco_muestreo<-base2%>%
ungroup()%>%
filter(Agrupacion %in% c(agrupaciones, "BLANCOS"))%>%
select(Mesa, region, Distrito, IdCircuito, codigo_pw, codigo_circuitos, electores, Cargo, Agrupacion, votos_validos, votos)%>%
pivot_wider(names_from = Agrupacion, values_from = votos, values_fill = 0)%>%
arrange(region, Distrito)
marco_muestreo
• Unidades de primera etapa: distrito electoral. Estratificadas por Zona: CABA, BsAs, NEA-NOA, Resto. Asignación de la muestra proporcional a los votos válidos del estrato. Se seleccionan n=80 circuitos en total. Forzar a que haya el menos dos circuitos seleccionados por estrato. • Las unidades der primera etapa se seleccionan dentro de cada estrato mediante Madow, con probabilidad de selección proporcional a la cantidad de mesas del circuito. Ordenando por Provincia-Sección.
n_circuitos <- 80
marco_muestreo<-marco_muestreo%>%
group_by(region)%>% #agrupo por region
mutate(cantidad_circuitos = n_distinct(IdCircuito))%>% #recuento de circuitos por region
group_by(codigo_circuitos)%>% #agrupo por circuito
mutate(cantidad_mesas = n_distinct(codigo_pw)) #recuento de mesas por circuito
# voy a quedarme con los cirtuitos que posean más de 6 messas
marco_muestreo<-marco_muestreo%>%
filter(cantidad_mesas >= 6)
min(marco_muestreo$cantidad_mesas) # chequeo
## [1] 6
# genero variable de probabilidad de selección proporcional a los votos validos
marco_muestreo$pi_i <- n_circuitos*marco_muestreo$votos_validos/sum(marco_muestreo$votos_validos)
# guardo las probabilidades
pik <- marco_muestreo$pi_i
# genero un vector de 1 y 0 para seleccionar en el marco de muestro
s = UPsystematic(pik,eps=1e-6)
# selecciono los circuitos
muestra_circuitos = marco_muestreo[s==1,]
# creo factor de expansión
muestra_circuitos$pondera <- 1/muestra_circuitos$pi_i
# tabla final
tabla_estratos<-muestra_circuitos%>%
group_by(region)%>%
summarise(cantidad_circuitos = n(),
circuitos_seleccionados = toString(codigo_circuitos))
# guardo los id de los circuitos a seleccionar
lista_circuitos_a_selecionar <- muestra_circuitos$codigo_circuitos
lista_circuitos_a_selecionar
## [1] "00109_01" "00142_01" "00133_06" "0008A_06" "00007_05" "00082_09"
## [7] "00022_10" "0001I_12" "00015_14" "0067A_17" "0012B_17" "00148_23"
## [13] "00214_23" "00040_02" "0652A_02" "0768C_02" "00587_02" "00380_02"
## [19] "00033_02" "00669_02" "00366_02" "0534A_02" "0635A_02" "0668B_02"
## [25] "00554_02" "00037_02" "00599_02" "00513_02" "0664B_02" "00491_02"
## [31] "00706_02" "00287_02" "0768C_02" "0302A_02" "0339A_02" "00364_02"
## [37] "00925_02" "00591_02" "0635B_02" "0583C_02" "00530_02" "00632_02"
## [43] "0365C_02" "00634_02" "00619_02" "00596_02" "00262_02" "00891_02"
## [49] "0397C_02" "0302E_02" "0771C_02" "00003_07" "00213_04" "0004B_04"
## [55] "00344_04" "00304_04" "00006_04" "00003_04" "0011C_04" "00240_08"
## [61] "00016_08" "00235_08" "00082_11" "0037A_13" "0025B_13" "00006_13"
## [67] "00048_13" "00034_15" "00028_16" "06311_16" "00101_18" "1008C_19"
## [73] "00015_20" "00118_21" "00015_21" "00014_21" "00185_21" "00394_21"
## [79] "00408_21" "00013_21"
• Unidades de segunda etapa: mesa electoral. Se seleccionan, mediante MAS, m=6 mesas en cada circuito seleccionado , o todas de haber menos. • Las unidades de segunda etapa se seleccionan mediante MAS
n_mesas<-6
# creo un segundo marco de muestreo filtrando los circuitos definidos anteriormente
marco_muestreo_2<-marco_muestreo%>%
filter(codigo_circuitos %in% lista_circuitos_a_selecionar)
#Ordeno el marco de muestreo por estrato (lo pide sampling)
marco_muestreo_2 <- marco_muestreo_2%>%
arrange(region, codigo_circuitos)
# genero muestra
identificador_muestra <- strata(marco_muestreo_2,
stratanames = c("codigo_circuitos"),
size=rep(n_mesas,n_circuitos),
description=TRUE,
method = "srswor")
## Stratum 1
##
## Population total and number of selected units: 54 6
## Stratum 2
##
## Population total and number of selected units: 70 6
## Stratum 3
##
## Population total and number of selected units: 45 6
## Stratum 4
##
## Population total and number of selected units: 38 6
## Stratum 5
##
## Population total and number of selected units: 51 6
## Stratum 6
##
## Population total and number of selected units: 132 6
## Stratum 7
##
## Population total and number of selected units: 35 6
## Stratum 8
##
## Population total and number of selected units: 69 6
## Stratum 9
##
## Population total and number of selected units: 20 6
## Stratum 10
##
## Population total and number of selected units: 27 6
## Stratum 11
##
## Population total and number of selected units: 19 6
## Stratum 12
##
## Population total and number of selected units: 34 6
## Stratum 13
##
## Population total and number of selected units: 41 6
## Stratum 14
##
## Population total and number of selected units: 34 6
## Stratum 15
##
## Population total and number of selected units: 59 6
## Stratum 16
##
## Population total and number of selected units: 193 6
## Stratum 17
##
## Population total and number of selected units: 167 6
## Stratum 18
##
## Population total and number of selected units: 63 6
## Stratum 19
##
## Population total and number of selected units: 369 6
## Stratum 20
##
## Population total and number of selected units: 236 6
## Stratum 21
##
## Population total and number of selected units: 46 6
## Stratum 22
##
## Population total and number of selected units: 92 6
## Stratum 23
##
## Population total and number of selected units: 86 6
## Stratum 24
##
## Population total and number of selected units: 186 6
## Stratum 25
##
## Population total and number of selected units: 78 6
## Stratum 26
##
## Population total and number of selected units: 272 6
## Stratum 27
##
## Population total and number of selected units: 44 6
## Stratum 28
##
## Population total and number of selected units: 190 6
## Stratum 29
##
## Population total and number of selected units: 21 6
## Stratum 30
##
## Population total and number of selected units: 24 6
## Stratum 31
##
## Population total and number of selected units: 32 6
## Stratum 32
##
## Population total and number of selected units: 85 6
## Stratum 33
##
## Population total and number of selected units: 101 6
## Stratum 34
##
## Population total and number of selected units: 7 6
## Stratum 35
##
## Population total and number of selected units: 139 6
## Stratum 36
##
## Population total and number of selected units: 85 6
## Stratum 37
##
## Population total and number of selected units: 119 6
## Stratum 38
##
## Population total and number of selected units: 129 6
## Stratum 39
##
## Population total and number of selected units: 29 6
## Stratum 40
##
## Population total and number of selected units: 60 6
## Stratum 41
##
## Population total and number of selected units: 92 6
## Stratum 42
##
## Population total and number of selected units: 160 6
## Stratum 43
##
## Population total and number of selected units: 58 6
## Stratum 44
##
## Population total and number of selected units: 270 6
## Stratum 45
##
## Population total and number of selected units: 309 6
## Stratum 46
##
## Population total and number of selected units: 383 6
## Stratum 47
##
## Population total and number of selected units: 86 6
## Stratum 48
##
## Population total and number of selected units: 158 6
## Stratum 49
##
## Population total and number of selected units: 64 6
## Stratum 50
##
## Population total and number of selected units: 72 6
## Stratum 51
##
## Population total and number of selected units: 58 6
## Stratum 52
##
## Population total and number of selected units: 40 6
## Stratum 53
##
## Population total and number of selected units: 16 6
## Stratum 54
##
## Population total and number of selected units: 13 6
## Stratum 55
##
## Population total and number of selected units: 47 6
## Stratum 56
##
## Population total and number of selected units: 69 6
## Stratum 57
##
## Population total and number of selected units: 15 6
## Stratum 58
##
## Population total and number of selected units: 70 6
## Stratum 59
##
## Population total and number of selected units: 38 6
## Stratum 60
##
## Population total and number of selected units: 18 6
## Stratum 61
##
## Population total and number of selected units: 55 6
## Stratum 62
##
## Population total and number of selected units: 114 6
## Stratum 63
##
## Population total and number of selected units: 45 6
## Stratum 64
##
## Population total and number of selected units: 21 6
## Stratum 65
##
## Population total and number of selected units: 13 6
## Stratum 66
##
## Population total and number of selected units: 55 6
## Stratum 67
##
## Population total and number of selected units: 41 6
## Stratum 68
##
## Population total and number of selected units: 38 6
## Stratum 69
##
## Population total and number of selected units: 8 6
## Stratum 70
##
## Population total and number of selected units: 95 6
## Stratum 71
##
## Population total and number of selected units: 7 6
## Stratum 72
##
## Population total and number of selected units: 12 6
## Stratum 73
##
## Population total and number of selected units: 51 6
## Stratum 74
##
## Population total and number of selected units: 33 6
## Stratum 75
##
## Population total and number of selected units: 57 6
## Stratum 76
##
## Population total and number of selected units: 47 6
## Stratum 77
##
## Population total and number of selected units: 75 6
## Stratum 78
##
## Population total and number of selected units: 35 6
## Stratum 79
##
## Population total and number of selected units: 17 6
## Number of strata 79
## Total number of selected units 480
identificador_muestra
# Recupero datos del marco de muestreo
muestra_con_sampling <- getdata(marco_muestreo_2, identificador_muestra)
DT::datatable(muestra_con_sampling)
muestra_con_sampling$pondera <- 1/muestra_con_sampling$Prob
# Definir el diseño de muestreo
diseño_muestreo_survey <- svydesign(id = ~ID_unit, strata = ~Stratum, weights = ~pondera, data = muestra_con_sampling, fcp = ~n )
#FDT
estimacion_fdt<-svytotal(x = ~`FRENTE DE TODOS`, design = diseño_muestreo_survey, CV=TRUE, deff=TRUE)
estimacion_fdt
## total SE DEff
## `FRENTE DE TODOS` 572256.8 5825.2 0.285
#JXC
estimacion_jxc<-svytotal(x = ~`JUNTOS POR EL CAMBIO`, design = diseño_muestreo_survey, CV=TRUE, deff=TRUE)
estimacion_jxc
## total SE DEff
## `JUNTOS POR EL CAMBIO` 498278.8 6459.3 0.2306
#FIT
estimacion_fit<-svytotal(x = ~`FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD`, design = diseño_muestreo_survey, CV=TRUE, deff=TRUE)
estimacion_fit
## total SE DEff
## `FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD` 106669.8 1721.3 0.2498
#Intervalos de confianza
confint(estimacion_fdt, level = 0.95)
## 2.5 % 97.5 %
## `FRENTE DE TODOS` 560839.7 583674
confint(estimacion_jxc, level = 0.95)
## 2.5 % 97.5 %
## `JUNTOS POR EL CAMBIO` 485618.8 510938.9
confint(estimacion_fit, level = 0.95)
## 2.5 % 97.5 %
## `FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD` 103296.1 110043.6
La estimación total para “FRENTE DE TODOS” es de aproximadamente 441,987 votos, con un error estándar de 4,034.8 y un deff de 0.2142. Esto significa que el diseño de muestreo complejo ha reducido la eficiencia en comparación con un muestreo aleatorio simple, pero aún así, la estimación es precisa dada la estructura de muestreo.
Intervalo de confianza al 95%: (434,079, 449,895) Esto significa que, con un nivel de confianza del 95%, se espera que el verdadero total esté dentro del rango entre 434,079 y 449,895.
La estimación del total de votos para “JUNTOS POR EL CAMBIO” es de aproximadamente 411,757.5 votos, con un error estándar de 3,985.3 y un factor de efecto de diseño de 0.1246. Estos resultados indican que, dada la estructura de muestreo, la estimación es precisa y la varianza se ha reducido en comparación con un diseño de muestreo aleatorio simple.
Intervalo de confianza al 95%: (403,946.5, 419,568.5) Con un nivel de confianza del 95%, se espera que el verdadero total de votos esté dentro del rango entre 403,946 y 419,568
La estimación del total de votos para “FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD” es de aproximadamente 82,108 votos, con un error estándar de 1,314.3 y un deff de 0.2147. Estos resultados nos indicarian que, dada la estructura de muestreo, la estimación es precisa y la varianza se ha reducido en comparación con un diseño de muestreo aleatorio simple.
Intervalo de confianza al 95%: (79,532.04, 84,683.96) Esto sugiere que el verdadero total de votos se encuentra entre 79,532.04 y 84,683.96.
Para determinar si podemos detectar quién gana la elección con un 95% de confianza, necesitamos considerar la superposición de los intervalos de confianza para las estimaciones de cada partido político. Si los intervalos de confianza no se superponen entre los diferentes partidos, podríamos inferir con cierta confianza quién es el ganador.
Intervalos de confianza para cada partido: Frente de Todos: (434,079, 449,895) Juntos por el Cambio: (403,946.5, 419,568.5) Frente de Izquierda y de Trabajadores - Unidad: (79,532.04, 84,683.96) De acuerdo a las estimaciones realizadas, hay superposición entre los intervalos de “Frente de Todos” y “Juntos por el Cambio”. No obstante, el intervalo de “Frente de Izquierda y de Trabajadores - Unidad” no se superpone significativamente con los otros dos.
En resumen, con la información proporcionada, no podemos afirmar con certeza quién gana la elección. No obstante, con una prueba de hipótesis podemos obtener información más precisa:
Hipótesis Nula (H0): No hay diferencia significativa entre las estimaciones de votos para “Frente de Todos” y “Juntos por el Cambio”.
Hipótesis Alternativa (H1): Hay una diferencia significativa entre las estimaciones de votos para “Frente de Todos” y “Juntos por el Cambio”.
votos_frente_todos <- c(434079, 449895)
error_estandar_frente_todos <- 4034.8
votos_juntos_por_el_cambio <- c(403946.5, 419568.5)
error_estandar_juntos_por_el_cambio <- 3985.3
# Prueba t de Student para la diferencia de medias
resultado_prueba_t <- t.test(votos_frente_todos, votos_juntos_por_el_cambio)
resultado_prueba_t
##
## Welch Two Sample t-test
##
## data: votos_frente_todos and votos_juntos_por_el_cambio
## t = 2.7196, df = 1.9997, p-value = 0.1128
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
## -17602.43 78061.43
## sample estimates:
## mean of x mean of y
## 441987.0 411757.5
# Intervalo de confianza al 95%
confianza_95_intervalo <- resultado_prueba_t$conf.int
confianza_95_intervalo
## [1] -17602.43 78061.43
## attr(,"conf.level")
## [1] 0.95
Dado que el intervalo - 17602.43 / 78061.43 incluye cero, no podemos rechazar la hipótesis nula de que no hay diferencia significativa entre las estimaciones de votos para “Frente de Todos” y “Juntos por el Cambio”. En otras palabras, con un 95% de confianza, no podemos afirmar que haya una diferencia significativa en los votos entre estos dos grupos.
Veamos qué pasa si tuvieramos oportunidad de aumentar la muestra de los circuitos (de 80 a 100) y mantener el muestreo de mesas (6 por circuito)
n_circuitos <- 100
marco_muestreo<-marco_muestreo%>%
group_by(region)%>% #agrupo por region
mutate(cantidad_circuitos = n_distinct(IdCircuito))%>% #recuento de circuitos por region
group_by(codigo_circuitos)%>% #agrupo por circuito
mutate(cantidad_mesas = n_distinct(codigo_pw)) #recuento de mesas por circuito
# voy a quedarme con los cirtuitos que posean más de 6 messas
marco_muestreo<-marco_muestreo%>%
filter(cantidad_mesas >= 6)
min(marco_muestreo$cantidad_mesas) # chequeo
## [1] 6
# genero variable de probabilidad de selección proporcional a los votos validos
marco_muestreo$pi_i <- n_circuitos*marco_muestreo$votos_validos/sum(marco_muestreo$votos_validos)
# guardo las probabilidades
pik <- marco_muestreo$pi_i
# genero un vector de 1 y 0 para seleccionar en el marco de muestro
s = UPsystematic(pik,eps=1e-6)
# selecciono los circuitos
muestra_circuitos = marco_muestreo[s==1,]
# creo factor de expansión
muestra_circuitos$pondera <- 1/muestra_circuitos$pi_i
# tabla final
tabla_estratos<-muestra_circuitos%>%
group_by(region)%>%
summarise(cantidad_circuitos = n(),
circuitos_seleccionados = toString(codigo_circuitos))
# guardo los id de los circuitos a seleccionar
lista_circuitos_a_selecionar <- muestra_circuitos$codigo_circuitos
n_mesas<-6
# creo un segundo marco de muestreo filtrando los circuitos definidos anteriormente
marco_muestreo_2<-marco_muestreo%>%
filter(codigo_circuitos %in% lista_circuitos_a_selecionar)
#Ordeno el marco de muestreo por estrato (lo pide sampling)
marco_muestreo_2 <- marco_muestreo_2%>%
arrange(region, codigo_circuitos)
# genero muestra
identificador_muestra <- strata(marco_muestreo_2,
stratanames = c("codigo_circuitos"),
size=rep(n_mesas,n_circuitos),
description=TRUE,
method = "srswor")
## Stratum 1
##
## Population total and number of selected units: 57 6
## Stratum 2
##
## Population total and number of selected units: 35 6
## Stratum 3
##
## Population total and number of selected units: 189 6
## Stratum 4
##
## Population total and number of selected units: 53 6
## Stratum 5
##
## Population total and number of selected units: 134 6
## Stratum 6
##
## Population total and number of selected units: 145 6
## Stratum 7
##
## Population total and number of selected units: 31 6
## Stratum 8
##
## Population total and number of selected units: 9 6
## Stratum 9
##
## Population total and number of selected units: 44 6
## Stratum 10
##
## Population total and number of selected units: 60 6
## Stratum 11
##
## Population total and number of selected units: 35 6
## Stratum 12
##
## Population total and number of selected units: 73 6
## Stratum 13
##
## Population total and number of selected units: 17 6
## Stratum 14
##
## Population total and number of selected units: 31 6
## Stratum 15
##
## Population total and number of selected units: 77 6
## Stratum 16
##
## Population total and number of selected units: 47 6
## Stratum 17
##
## Population total and number of selected units: 253 6
## Stratum 18
##
## Population total and number of selected units: 144 6
## Stratum 19
##
## Population total and number of selected units: 34 6
## Stratum 20
##
## Population total and number of selected units: 64 6
## Stratum 21
##
## Population total and number of selected units: 167 6
## Stratum 22
##
## Population total and number of selected units: 46 6
## Stratum 23
##
## Population total and number of selected units: 132 6
## Stratum 24
##
## Population total and number of selected units: 81 6
## Stratum 25
##
## Population total and number of selected units: 26 6
## Stratum 26
##
## Population total and number of selected units: 236 6
## Stratum 27
##
## Population total and number of selected units: 204 6
## Stratum 28
##
## Population total and number of selected units: 200 6
## Stratum 29
##
## Population total and number of selected units: 121 6
## Stratum 30
##
## Population total and number of selected units: 19 6
## Stratum 31
##
## Population total and number of selected units: 272 6
## Stratum 32
##
## Population total and number of selected units: 88 6
## Stratum 33
##
## Population total and number of selected units: 32 6
## Stratum 34
##
## Population total and number of selected units: 52 6
## Stratum 35
##
## Population total and number of selected units: 120 6
## Stratum 36
##
## Population total and number of selected units: 147 6
## Stratum 37
##
## Population total and number of selected units: 73 6
## Stratum 38
##
## Population total and number of selected units: 76 6
## Stratum 39
##
## Population total and number of selected units: 22 6
## Stratum 40
##
## Population total and number of selected units: 118 6
## Stratum 41
##
## Population total and number of selected units: 119 6
## Stratum 42
##
## Population total and number of selected units: 129 6
## Stratum 43
##
## Population total and number of selected units: 104 6
## Stratum 44
##
## Population total and number of selected units: 40 6
## Stratum 45
##
## Population total and number of selected units: 160 6
## Stratum 46
##
## Population total and number of selected units: 270 6
## Stratum 47
##
## Population total and number of selected units: 162 6
## Stratum 48
##
## Population total and number of selected units: 383 6
## Stratum 49
##
## Population total and number of selected units: 139 6
## Stratum 50
##
## Population total and number of selected units: 86 6
## Stratum 51
##
## Population total and number of selected units: 85 6
## Stratum 52
##
## Population total and number of selected units: 39 6
## Stratum 53
##
## Population total and number of selected units: 77 6
## Stratum 54
##
## Population total and number of selected units: 91 6
## Stratum 55
##
## Population total and number of selected units: 45 6
## Stratum 56
##
## Population total and number of selected units: 44 6
## Stratum 57
##
## Population total and number of selected units: 35 6
## Stratum 58
##
## Population total and number of selected units: 290 6
## Stratum 59
##
## Population total and number of selected units: 30 6
## Stratum 60
##
## Population total and number of selected units: 19 6
## Stratum 61
##
## Population total and number of selected units: 38 6
## Stratum 62
##
## Population total and number of selected units: 82 6
## Stratum 63
##
## Population total and number of selected units: 34 6
## Stratum 64
##
## Population total and number of selected units: 84 6
## Stratum 65
##
## Population total and number of selected units: 140 6
## Stratum 66
##
## Population total and number of selected units: 6 6
## Stratum 67
##
## Population total and number of selected units: 64 6
## Stratum 68
##
## Population total and number of selected units: 20 6
## Stratum 69
##
## Population total and number of selected units: 16 6
## Stratum 70
##
## Population total and number of selected units: 8 6
## Stratum 71
##
## Population total and number of selected units: 32 6
## Stratum 72
##
## Population total and number of selected units: 36 6
## Stratum 73
##
## Population total and number of selected units: 25 6
## Stratum 74
##
## Population total and number of selected units: 47 6
## Stratum 75
##
## Population total and number of selected units: 18 6
## Stratum 76
##
## Population total and number of selected units: 14 6
## Stratum 77
##
## Population total and number of selected units: 21 6
## Stratum 78
##
## Population total and number of selected units: 14 6
## Stratum 79
##
## Population total and number of selected units: 102 6
## Stratum 80
##
## Population total and number of selected units: 78 6
## Stratum 81
##
## Population total and number of selected units: 16 6
## Stratum 82
##
## Population total and number of selected units: 32 6
## Stratum 83
##
## Population total and number of selected units: 72 6
## Stratum 84
##
## Population total and number of selected units: 14 6
## Stratum 85
##
## Population total and number of selected units: 131 6
## Stratum 86
##
## Population total and number of selected units: 199 6
## Stratum 87
##
## Population total and number of selected units: 18 6
## Stratum 88
##
## Population total and number of selected units: 55 6
## Stratum 89
##
## Population total and number of selected units: 65 6
## Stratum 90
##
## Population total and number of selected units: 109 6
## Stratum 91
##
## Population total and number of selected units: 11 6
## Stratum 92
##
## Population total and number of selected units: 112 6
## Stratum 93
##
## Population total and number of selected units: 35 6
## Stratum 94
##
## Population total and number of selected units: 6 6
## Number of strata 94
## Total number of selected units 600
# Recupero datos del marco de muestreo
muestra_con_sampling <- getdata(marco_muestreo_2, identificador_muestra)
muestra_con_sampling$pondera <- 1/muestra_con_sampling$Prob
# Definir el diseño de muestreo
diseño_muestreo_survey <- svydesign(id = ~ID_unit, strata = ~Stratum, weights = ~pondera, data = muestra_con_sampling, fcp = ~n )
diseño_muestreo_survey
## Stratified Independent Sampling design (with replacement)
## svydesign(id = ~ID_unit, strata = ~Stratum, weights = ~pondera,
## data = muestra_con_sampling, fcp = ~n)
#FDT
estimacion_fdt<-svytotal(x = ~`FRENTE DE TODOS`, design = diseño_muestreo_survey, CV=TRUE, deff=TRUE)
estimacion_fdt
## total SE DEff
## `FRENTE DE TODOS` 684329.8 4777.4 0.1472
#JXC
estimacion_jxc<-svytotal(x = ~`JUNTOS POR EL CAMBIO`, design = diseño_muestreo_survey, CV=TRUE, deff=TRUE)
estimacion_jxc
## total SE DEff
## `JUNTOS POR EL CAMBIO` 534076.5 5014.1 0.1067
#FIT
estimacion_fit<-svytotal(x = ~`FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD`, design = diseño_muestreo_survey, CV=TRUE, deff=TRUE)
confint(estimacion_fdt, level = 0.95)
## 2.5 % 97.5 %
## `FRENTE DE TODOS` 674966.3 693693.4
confint(estimacion_jxc, level = 0.95)
## 2.5 % 97.5 %
## `JUNTOS POR EL CAMBIO` 524249.1 543903.9
confint(estimacion_fit, level = 0.95)
## 2.5 % 97.5 %
## `FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD` 112458.4 120134.3
# Datos de "Frente de Todos"
votos_frente_todos <- c(713587.5, 734289.2)
error_estandar_frente_todos <- 5281.1
# Datos de "Juntos por el Cambio"
votos_juntos_por_el_cambio <- c(616730.2 , 635233.2)
error_estandar_juntos_por_el_cambio <- 4720.2
# Realizar la prueba t de Student para la diferencia de medias
resultado_prueba_t <- t.test(votos_frente_todos, votos_juntos_por_el_cambio)
# Mostrar el intervalo de confianza al 95%
confianza_95_intervalo <- resultado_prueba_t$conf.int
confianza_95_intervalo
## [1] 37502.68 158410.62
## attr(,"conf.level")
## [1] 0.95
Dado que el intervalo 37502.68 / 158410.62 no incluye cero, podríamos rechazar la hipótesis nula de que no hay diferencia significativa entre las estimaciones de votos para “Frente de Todos” y “Juntos por el Cambio”. En otras palabras, con un 95% de confianza, podríamos afirmar que hay una diferencia significativa en los votos entre estos dos grupos.
Este resultado sugiere que, con la muestra de circuitos aumentada, la evidencia estadística a favor de una diferencia significativa entre los dos grupos se ha fortalecido y nos permite afirmar con un 95% de confianza, quién ganará la elección.
La variabilidad dentro de los estratos “región” puede influir significativamente en la precisión de las estimaciones posteriores. Se podría realizar un análisis más detallado de la variabilidad en cada estrato y ajustar el tamaño de la muestra de los CIRCUITOS, en la primera etapa. Esto implica asignar tamaños de muestra mayores a estratos con mayor variabilidad para mejorar la precisión en esas áreas específicas.Una muestra más precisa en una primera etapa, nos ayuda a realizar una muestra más precisa, aun sin modificar la cantidad de mesas seleccionadas en la segunda etapa del muestreo.
Idealmente, el aumento del tamaño total de la muestra de las mesas permitiría reducir el error estándar de las estimaciones y estrechar los intervalos de confianza. Sin embargo, si esto no fuera factible por razones técnicas o económicas, se puede modificar el método de selección de mesas. En lugar de utilizar muestreo aleatorio simple para la selección dentro de cada estrato, se podría considerar el uso de muestreo sistemático que puede ayudar a garantizar una distribución más uniforme de las mesas seleccionadas dentro de los circuitos.
## Crear dataframe de muestreo. La tabla tiene que tener
marco_muestreo_cluster<-base2%>%
ungroup()%>%
filter(Agrupacion %in% c(agrupaciones, "BLANCOS"))%>%
select(Mesa, region, IdCircuito, codigo_pw, codigo_circuitos, Agrupacion, votos)%>%
pivot_wider(names_from = Agrupacion, values_from = votos, values_fill = 0)%>%
arrange(region)
marco_muestreo_cluster <- marco_muestreo_cluster %>%
mutate(total_votos_tres_agrupaciones = rowSums(select(., c("JUNTOS POR EL CAMBIO", "FRENTE DE TODOS", "FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD"))))
resultados_agrupacion <- marco_muestreo_cluster %>%
group_by(codigo_circuitos, Mesa) %>%
summarise(
prop_votos_FdT = sum(`FRENTE DE TODOS`) / sum(total_votos_tres_agrupaciones),
prop_votos_Juntos = sum(`JUNTOS POR EL CAMBIO`) / sum(total_votos_tres_agrupaciones),
prop_votos_FIT = sum(`FRENTE DE IZQUIERDA Y DE TRABAJADORES - UNIDAD`) / sum(total_votos_tres_agrupaciones)
)
resultados_agrupacion
# Se define un dataframe vacío para empezar
rho <- data.frame(row.names = c("Lohr rho", "Adjusted r-square", "ANOVA rho"))
# Se itera sobre las variables
for (i in c("prop_votos_FdT", "prop_votos_Juntos", "prop_votos_FIT")) {
rho[[i]] <- fishmethods::clus.rho(popchar = resultados_agrupacion[[i]],
cluster = resultados_agrupacion$codigo_circuitos,
type = c(1, 2, 3), est = 0, nboot = 500)[[1]][, 1]
}
# Visualizo los resultados
rho