1library(survey) 2data(election) 3 4dpps<- svydesign(id=~1, weights=~wt, fpc=~p, data=election_pps, pps="brewer") 5dppswr <-svydesign(id=~1, weights=~wt, data=election_pps) 6svytotal(~Bush+Kerry+Nader, dpps) 7svytotal(~Bush+Kerry+Nader, dppswr) 8 9##subsets 10svytotal(~Bush+Kerry+Nader, subset(dpps, Nader>0)) 11 12##multistage: should agree with STRS analysis 13data(api) 14dclus2<-svydesign(id = ~dnum + snum, fpc = ~fpc1 + fpc2, data = apiclus2) 15dclus2pps<-svydesign(id = ~dnum + snum, fpc = ~I(40/fpc1) + I(pmin(1,5/fpc2)), data = apiclus2) 16 17all.equal(svytotal(~sch.wide,dclus2), svytotal(~sch.wide,dclus2pps)) 18all.equal(svymean(~sch.wide,dclus2), svymean(~sch.wide,dclus2pps)) 19all.equal(svytotal(~enroll,dclus2), svytotal(~enroll,dclus2pps)) 20 21## the new without-replacement methods 22data(election) 23dpps_br<- svydesign(id=~1, fpc=~p, data=election_pps, pps="brewer") 24dpps_ov<- svydesign(id=~1, fpc=~p, data=election_pps, pps="overton") 25dpps_hr<- svydesign(id=~1, fpc=~p, data=election_pps, pps=HR(sum(election$p^2)/40)) 26dpps_hr1<- svydesign(id=~1, fpc=~p, data=election_pps, pps=HR()) 27dpps_ht<- svydesign(id=~1, fpc=~p, data=election_pps, pps=ppsmat(election_jointprob)) 28## Yates-Grundy type 29dpps_yg<- svydesign(id=~1, fpc=~p, data=election_pps, pps=ppsmat(election_jointprob),variance="YG") 30dpps_hryg<- svydesign(id=~1, fpc=~p, data=election_pps, pps=HR(sum(election$p^2)/40),variance="YG") 31 32## The with-replacement approximation 33svytotal(~Bush+Kerry+Nader, dpps_ht) 34svytotal(~Bush+Kerry+Nader, dpps_yg) 35svytotal(~Bush+Kerry+Nader, dpps_hr) 36svytotal(~Bush+Kerry+Nader, dpps_hryg) 37svytotal(~Bush+Kerry+Nader, dpps_hr1) 38svytotal(~Bush+Kerry+Nader, dpps_br) 39svytotal(~Bush+Kerry+Nader, dpps_ov) 40 41## subsets 42svytotal(~Bush+Kerry+Nader, subset(dpps_ht, Nader>0)) 43svytotal(~Bush+Kerry+Nader, subset(dpps_hryg, Nader>0)) 44 45## counts 46svyby(~Bush+Kerry+Nader,~I(Nader>0), unwtd.count,design=dpps_ht) 47