1 2R version 3.1.0 (2014-04-10) -- "Spring Dance" 3Copyright (C) 2014 The R Foundation for Statistical Computing 4Platform: x86_64-apple-darwin13.1.0 (64-bit) 5 6R is free software and comes with ABSOLUTELY NO WARRANTY. 7You are welcome to redistribute it under certain conditions. 8Type 'license()' or 'licence()' for distribution details. 9 10R is a collaborative project with many contributors. 11Type 'contributors()' for more information and 12'citation()' on how to cite R or R packages in publications. 13 14Type 'demo()' for some demos, 'help()' for on-line help, or 15'help.start()' for an HTML browser interface to help. 16Type 'q()' to quit R. 17 18> library(survey) 19 20Attaching package: 'survey' 21 22The following object is masked from 'package:graphics': 23 24 dotchart 25 26> data(election) 27> 28> dpps<- svydesign(id=~1, weights=~wt, fpc=~p, data=election_pps, pps="brewer") 29> dppswr <-svydesign(id=~1, weights=~wt, data=election_pps) 30> svytotal(~Bush+Kerry+Nader, dpps) 31 total SE 32Bush 64518472 2447629 33Kerry 51202102 2450787 34Nader 478530 102420 35> svytotal(~Bush+Kerry+Nader, dppswr) 36 total SE 37Bush 64518472 2671455 38Kerry 51202102 2679433 39Nader 478530 105303 40> 41> ##subsets 42> svytotal(~Bush+Kerry+Nader, subset(dpps, Nader>0)) 43 total SE 44Bush 34944285 5399833 45Kerry 25581714 4028434 46Nader 478530 102420 47> 48> ##multistage: should agree with STRS analysis 49> data(api) 50> dclus2<-svydesign(id = ~dnum + snum, fpc = ~fpc1 + fpc2, data = apiclus2) 51> dclus2pps<-svydesign(id = ~dnum + snum, fpc = ~I(40/fpc1) + I(pmin(1,5/fpc2)), data = apiclus2) 52> 53> all.equal(svytotal(~sch.wide,dclus2), svytotal(~sch.wide,dclus2pps)) 54[1] TRUE 55> all.equal(svymean(~sch.wide,dclus2), svymean(~sch.wide,dclus2pps)) 56[1] TRUE 57> all.equal(svytotal(~enroll,dclus2), svytotal(~enroll,dclus2pps)) 58[1] TRUE 59> 60> ## the new without-replacement methods 61> data(election) 62> dpps_br<- svydesign(id=~1, fpc=~p, data=election_pps, pps="brewer") 63> dpps_ov<- svydesign(id=~1, fpc=~p, data=election_pps, pps="overton") 64> dpps_hr<- svydesign(id=~1, fpc=~p, data=election_pps, pps=HR(sum(election$p^2)/40)) 65> dpps_hr1<- svydesign(id=~1, fpc=~p, data=election_pps, pps=HR()) 66> dpps_ht<- svydesign(id=~1, fpc=~p, data=election_pps, pps=ppsmat(election_jointprob)) 67> ## Yates-Grundy type 68> dpps_yg<- svydesign(id=~1, fpc=~p, data=election_pps, pps=ppsmat(election_jointprob),variance="YG") 69> dpps_hryg<- svydesign(id=~1, fpc=~p, data=election_pps, pps=HR(sum(election$p^2)/40),variance="YG") 70> 71> ## The with-replacement approximation 72> svytotal(~Bush+Kerry+Nader, dpps_ht) 73 total SE 74Bush 64518472 2604404 75Kerry 51202102 2523712 76Nader 478530 102326 77> svytotal(~Bush+Kerry+Nader, dpps_yg) 78 total SE 79Bush 64518472 2406526 80Kerry 51202102 2408091 81Nader 478530 101664 82> svytotal(~Bush+Kerry+Nader, dpps_hr) 83 total SE 84Bush 64518472 2624662 85Kerry 51202102 2525222 86Nader 478530 102793 87> svytotal(~Bush+Kerry+Nader, dpps_hryg) 88 total SE 89Bush 64518472 2436738 90Kerry 51202102 2439845 91Nader 478530 102016 92> svytotal(~Bush+Kerry+Nader, dpps_hr1) 93 total SE 94Bush 64518472 2472753 95Kerry 51202102 2426842 96Nader 478530 102595 97> svytotal(~Bush+Kerry+Nader, dpps_br) 98 total SE 99Bush 64518472 2447629 100Kerry 51202102 2450787 101Nader 478530 102420 102> svytotal(~Bush+Kerry+Nader, dpps_ov) 103 total SE 104Bush 64518472 2939608 105Kerry 51202102 1964632 106Nader 478530 104373 107> 108> ## subsets 109> svytotal(~Bush+Kerry+Nader, subset(dpps_ht, Nader>0)) 110 total SE 111Bush 34944285 5406348 112Kerry 25581714 4047741 113Nader 478530 102326 114> svytotal(~Bush+Kerry+Nader, subset(dpps_hryg, Nader>0)) 115 total SE 116Bush 34944285 5377659 117Kerry 25581714 4010908 118Nader 478530 102016 119> 120> ## counts 121> svyby(~Bush+Kerry+Nader,~I(Nader>0), unwtd.count,design=dpps_ht) 122 I(Nader > 0) counts se 123FALSE FALSE 19 0 124TRUE TRUE 21 0 125> 126> proc.time() 127 user system elapsed 128 1.946 0.058 2.017 129