Installing Packages
#install.packages("survey")
#install.packages("sampling")
#install.packages("writexl")
Loading Packages
library(readxl)
## Warning: package 'readxl' was built under R version 4.5.3
library(survey)
## Warning: package 'survey' was built under R version 4.5.3
## Loading required package: grid
## Loading required package: Matrix
## Loading required package: survival
##
## Attaching package: 'survey'
## The following object is masked from 'package:graphics':
##
## dotchart
library(sampling)
## Warning: package 'sampling' was built under R version 4.5.3
##
## Attaching package: 'sampling'
## The following objects are masked from 'package:survival':
##
## cluster, strata
Reading Data
base<-readRDS(file = "Marco.rds")
Creating the Bogotá Dataset
base_bogota<-subset(base,base$COLE_COD_MCPIO_UBICACION==11001)
Creating the Academic School Variable
base_bogota$academico=ifelse(base_bogota$COLE_CARACTER=="ACADÉMICO",1,0)
Sample Size Calculation
tipo="p"
N=nrow(base_bogota)
alpha=0.05
p=0.5
e=0.05
deff=2
n.mas=function(tipo,N,s,e,p,alpha){
if(tipo=="t"){n=round((qnorm(1-alpha/2)^2*N^2*s^2)/(e^2+qnorm(1-alpha/2)^2*N*s^2),0)}
if(tipo=="t"){return(n)}
if(tipo=="m"){n=round((qnorm(1-alpha/2)^2*s^2)/(e^2+(qnorm(1-alpha/2)^2*s^2/N)),0)}
if(tipo=="m"){return(n)}
if(tipo=="p"){n=round((qnorm(1-alpha/2)^2*(N/(N-1))*p*(1-p))/(e^2+(qnorm(1-alpha/2)^2*(N/(N-1))*p*(1-p)*(1/N))),0)
if(tipo=="p"){return(n)}
}}
r<-n.mas(tipo,N,s,e,p,alpha)*deff
Sample Selection
s.masr=function(base,r,seed){
N=nrow(base)
set.seed(seed)
base=base[sample(c(1:nrow(base_bogota)),r,replace = T),]
base$pik=1-(1-1/N)^r
return(base)
}
r=764
seed=123
N=nrow(base_bogota)
muestra.r=s.masr(base_bogota,r,seed)
Creating the “dsgn” Objec
muestra.r$Fexp=1/muestra.r$pik
dsgn=svydesign(id=~1,data=muestra.r,weights=~Fexp)
Estimation of the total number of academic schools in Bogotá
salida=function(est,alpha){
est=as.data.frame(est)
names(est)[2]="se"
est$cv=100*(est$se/est[,1])
est$ic_low=est[,1]-qnorm(1-alpha/2)*est$se
est$ic_upp=est[,1]+qnorm(1-alpha/2)*est$se
return(round(est,2))
}
(est.r=svytotal(~academico,dsgn,deff=T))
## total SE DEff
## academico 69381.4 1121.5 1.0093
alpha=0.05
(tabla=salida(est.r,alpha))
## total se deff cv ic_low ic_upp
## academico 69381.38 1121.53 1.01 1.62 67183.21 71579.55
Global Average Score Estimation
(est1.r=svymean(~PUNT_GLOBAL,dsgn,deff=T))
## mean SE DEff
## PUNT_GLOBAL 265.4411 1.6835 1.0093
(tabla=salida(est1.r,alpha))
## mean se deff cv ic_low ic_upp
## PUNT_GLOBAL 265.44 1.68 1.01 0.63 262.14 268.74
Estimation of the Proportion
(est2.r=svymean(~DESEMP_INGLES,dsgn,deff=T))
## mean SE DEff
## DESEMP_INGLESA- 0.3062827 0.0166875 1.0093
## DESEMP_INGLESA1 0.3311518 0.0170379 1.0093
## DESEMP_INGLESA2 0.2290576 0.0152132 1.0093
## DESEMP_INGLESB+ 0.0261780 0.0057802 1.0093
## DESEMP_INGLESB1 0.1073298 0.0112058 1.0093
(tabla=salida(est2.r,alpha))
## mean se deff cv ic_low ic_upp
## DESEMP_INGLESA- 0.31 0.02 1.01 5.45 0.27 0.34
## DESEMP_INGLESA1 0.33 0.02 1.01 5.15 0.30 0.36
## DESEMP_INGLESA2 0.23 0.02 1.01 6.64 0.20 0.26
## DESEMP_INGLESB+ 0.03 0.01 1.01 22.08 0.01 0.04
## DESEMP_INGLESB1 0.11 0.01 1.01 10.44 0.09 0.13
Parameter Estimation for Multiple Variables
(est3.r=svymean(~PUNT_GLOBAL+PUNT_LECTURA_CRITICA+PUNT_MATEMATICAS+PUNT_C_NATURALES+
PUNT_SOCIALES_CIUDADANAS+PUNT_INGLES,dsgn))
## mean SE
## PUNT_GLOBAL 265.441 1.6835
## PUNT_LECTURA_CRITICA 55.577 0.3389
## PUNT_MATEMATICAS 54.719 0.3861
## PUNT_C_NATURALES 51.563 0.3586
## PUNT_SOCIALES_CIUDADANAS 50.157 0.4217
## PUNT_INGLES 54.051 0.4346
(tabla=salida(est3.r,alpha))
## mean se cv ic_low ic_upp
## PUNT_GLOBAL 265.44 1.68 0.63 262.14 268.74
## PUNT_LECTURA_CRITICA 55.58 0.34 0.61 54.91 56.24
## PUNT_MATEMATICAS 54.72 0.39 0.71 53.96 55.48
## PUNT_C_NATURALES 51.56 0.36 0.70 50.86 52.27
## PUNT_SOCIALES_CIUDADANAS 50.16 0.42 0.84 49.33 50.98
## PUNT_INGLES 54.05 0.43 0.80 53.20 54.90