-
-
Save tslumley/73c913b4a96f466cc99503a54869745e to your computer and use it in GitHub Desktop.
Survey weighted SE
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(survey) | |
population<-data.frame(y=1:1000, | |
strata=rep(1:3,c(100,200,700)), | |
popsizes=rep(c(100,200,700),c(100,200,700)) | |
) | |
with(population, by(y,strata,var)) | |
sampsizes<-c("1"=10,"2"=30,"3"=50) | |
one.sim<-function(){ | |
in_sample<-stratsample(as.character(population$strata), sampsizes) | |
the_sample<-population[in_sample,] | |
des<-svydesign(id=~1, strat=~strata, fpc=~popsizes,data=the_sample) | |
m<-svymean(~y,des) | |
c(mean=coef(m),SE=SE(m)) | |
} | |
set.seed(2024-11-3) | |
r<-replicate(10000, one.sim()) | |
sd(r[1,]) | |
median(r[2,]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS: if you don't have R handy, the answers are 19.36173 for the simulation SD and 19.39484 for the median estimated SE