Skip to content

Instantly share code, notes, and snippets.

@tslumley
Created November 3, 2024 00:27
Show Gist options
  • Save tslumley/73c913b4a96f466cc99503a54869745e to your computer and use it in GitHub Desktop.
Save tslumley/73c913b4a96f466cc99503a54869745e to your computer and use it in GitHub Desktop.
Survey weighted SE
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,])
@tslumley
Copy link
Author

tslumley commented Nov 3, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment