##############Estimating total and mean in survey data################# ###stratified random sampling library(survey) library(foreign) data(hospital) hospital dstr <- svydesign(id = ~1, strata = ~oblevel, fpc = ~tothosp, data = hospital) smean<-svymean(~births, dstr) smean confint(smean, level=.95) stotal<-svytotal(~births, dstr) stotal confint(stotal, level=.95) svyby(~births, ~oblevel, dstr, svymean, keep.var = TRUE) svyby(~births, ~oblevel, dstr, svytotal, keep.var = TRUE) svyquantile(~births, dstr, c(0.25,0.5,0.75), ci=TRUE) #####one stage cluster sampling ex.data<-read.table(file="W:/teaching/stat579/data/gpa.txt", header=T) design<-svydesign(id=~suit,weight=~wt,fpc=~rep(100,20),nest=TRUE,data=ex.data) summary(design) svytotal(~gpa,design) smean<-svymean(~gpa,design) confint(smean,level=.95) anova(~gpa,design) #####two-stage cluster sampling # The Academic Performance Index is computed for all California # schools based on standardized testing of students. # The data sets contain information for all schools with at least 100 students # and for various probability samples of the data. # apipop contains the entire population. # apistrat contains a sample stratified by stype. # apiclus1 contains a cluster sample of school districts. # apiclus2 contains a two-stage cluster sample of schools within districts. data(api) help(api) clu2design<-svydesign(id=~dnum+snum,fpc=~fpc1+fpc2,data=apiclus2) dimnames(apiclus2)[2] summary(clu2design) svytotal(~enroll,clu2design,na.rm=TRUE) smean<-svymean(~enroll,clu2design,na.rm=TRUE)