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>
19> ## lonely PSUs by design
20> library(survey)
21
22Attaching package: 'survey'
23
24The following object is masked from 'package:graphics':
25
26    dotchart
27
28> data(api)
29> ## not certainty PSUs by fpc
30> ds<-svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = apiclus1)
31> summary(ds)
32Stratified Independent Sampling design (with replacement)
33svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = apiclus1)
34Probabilities:
35   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
360.02954 0.02954 0.02954 0.02954 0.02954 0.02954
37Stratum Sizes:
38           61 135 178 197 255 406 413 437 448 510 568 637 716 778 815
39obs        13  34   4  13  16   2   1   4  12  21   9  11  37   2   4
40design.PSU 13  34   4  13  16   2   1   4  12  21   9  11  37   2   4
41actual.PSU 13  34   4  13  16   2   1   4  12  21   9  11  37   2   4
42Data variables:
43 [1] "cds"      "stype"    "name"     "sname"    "snum"     "dname"
44 [7] "dnum"     "cname"    "cnum"     "flag"     "pcttest"  "api00"
45[13] "api99"    "target"   "growth"   "sch.wide" "comp.imp" "both"
46[19] "awards"   "meals"    "ell"      "yr.rnd"   "mobility" "acs.k3"
47[25] "acs.46"   "acs.core" "pct.resp" "not.hsg"  "hsg"      "some.col"
48[31] "col.grad" "grad.sch" "avg.ed"   "full"     "emer"     "enroll"
49[37] "api.stu"  "fpc"      "pw"
50>
51> options(survey.lonely.psu="fail")
52> try(svymean(~api00,ds))
53Error in onestrat(x[index, , drop = FALSE], clusters[index], nPSU[index][1],  :
54  Stratum (413) has only one PSU at stage 1
55> try(svymean(~api00, as.svrepdesign(ds)))
56Error in jknweights(design$strata[, 1], design$cluster[, 1], fpc = fpc,  :
57  Stratum413has only one PSU
58> options(survey.lonely.psu="remove")
59> svymean(~api00,ds)
60        mean     SE
61api00 644.17 5.8058
62> svymean(~api00, as.svrepdesign(ds))
63        mean     SE
64api00 644.17 5.8058
65> options(survey.lonely.psu="certainty")
66> svymean(~api00,ds)
67        mean     SE
68api00 644.17 5.8058
69> svymean(~api00, as.svrepdesign(ds))
70        mean     SE
71api00 644.17 5.8058
72> options(survey.lonely.psu="adjust")
73> svymean(~api00,ds)
74        mean     SE
75api00 644.17 5.8281
76> svymean(~api00, as.svrepdesign(ds))
77        mean     SE
78api00 644.17 5.8267
79> options(survey.lonely.psu="average")
80> svymean(~api00,ds)
81        mean     SE
82api00 644.17 6.0096
83> svymean(~api00, as.svrepdesign(ds))
84        mean     SE
85api00 644.17 5.8217
86>
87> ## fpc specified
88> fpc<-ifelse(apiclus1$dnum==413, 1,1000)
89> ds<-svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = apiclus1,fpc=fpc)
90> summary(ds)
91Stratified Independent Sampling design
92svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = apiclus1,
93    fpc = fpc)
94Probabilities:
95   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
960.02954 0.02954 0.02954 0.02954 0.02954 0.02954
97Stratum Sizes:
98           61 135 178 197 255 406 413 437 448 510 568 637 716 778 815
99obs        13  34   4  13  16   2   1   4  12  21   9  11  37   2   4
100design.PSU 13  34   4  13  16   2   1   4  12  21   9  11  37   2   4
101actual.PSU 13  34   4  13  16   2   1   4  12  21   9  11  37   2   4
102Population stratum sizes (PSUs):
103 135  178  197  255  406  413  437  448  510  568   61  637  716  778  815
1041000 1000 1000 1000 1000    1 1000 1000 1000 1000 1000 1000 1000 1000 1000
105Data variables:
106 [1] "cds"      "stype"    "name"     "sname"    "snum"     "dname"
107 [7] "dnum"     "cname"    "cnum"     "flag"     "pcttest"  "api00"
108[13] "api99"    "target"   "growth"   "sch.wide" "comp.imp" "both"
109[19] "awards"   "meals"    "ell"      "yr.rnd"   "mobility" "acs.k3"
110[25] "acs.46"   "acs.core" "pct.resp" "not.hsg"  "hsg"      "some.col"
111[31] "col.grad" "grad.sch" "avg.ed"   "full"     "emer"     "enroll"
112[37] "api.stu"  "fpc"      "pw"
113>
114> options(survey.lonely.psu="fail")
115> try(svymean(~api00,ds))
116        mean     SE
117api00 644.17 5.7344
118> svymean(~api00, as.svrepdesign(ds))
119        mean     SE
120api00 644.17 5.7344
121> options(survey.lonely.psu="remove")
122> svymean(~api00,ds)
123        mean     SE
124api00 644.17 5.7344
125> svymean(~api00, as.svrepdesign(ds))
126        mean     SE
127api00 644.17 5.7344
128> options(survey.lonely.psu="certainty")
129> svymean(~api00,ds)
130        mean     SE
131api00 644.17 5.7344
132> svymean(~api00, as.svrepdesign(ds))
133        mean     SE
134api00 644.17 5.7344
135> options(survey.lonely.psu="adjust")
136> svymean(~api00,ds)
137        mean     SE
138api00 644.17 5.7344
139> svymean(~api00, as.svrepdesign(ds))
140        mean     SE
141api00 644.17 5.7344
142> options(survey.lonely.psu="average")
143> svymean(~api00,ds)
144        mean     SE
145api00 644.17 5.7344
146> svymean(~api00, as.svrepdesign(ds))
147        mean     SE
148api00 644.17 5.7501
149>
150> rs<-as.svrepdesign(ds)
151> svytotal(~api00,rs)
152        total    SE
153api00 3989986 35616
154> SE(svytotal(~api00,subset(rs, dnum==413)))==0
155[1] TRUE
156>
157> ## lonely PSUs after subsetting
158> ds<-svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = subset(apiclus1,dnum !=413))
159> ds1<-ds[-31,]
160> summary(ds1)
161Stratified Independent Sampling design (with replacement)
162svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = subset(apiclus1,
163    dnum != 413))
164Probabilities:
165   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
1660.02954 0.02954 0.02954 0.02954 0.02954 0.02954
167Stratum Sizes:
168           61 135 178 197 255 406 437 448 510 568 637 716 778 815
169obs        13  34   4  13  16   1   4  12  21   9  11  37   2   4
170design.PSU 13  34   4  13  16   2   4  12  21   9  11  37   2   4
171actual.PSU 13  34   4  13  16   1   4  12  21   9  11  37   2   4
172Data variables:
173 [1] "cds"      "stype"    "name"     "sname"    "snum"     "dname"
174 [7] "dnum"     "cname"    "cnum"     "flag"     "pcttest"  "api00"
175[13] "api99"    "target"   "growth"   "sch.wide" "comp.imp" "both"
176[19] "awards"   "meals"    "ell"      "yr.rnd"   "mobility" "acs.k3"
177[25] "acs.46"   "acs.core" "pct.resp" "not.hsg"  "hsg"      "some.col"
178[31] "col.grad" "grad.sch" "avg.ed"   "full"     "emer"     "enroll"
179[37] "api.stu"  "fpc"      "pw"
180>
181> options(survey.lonely.psu="fail")
182> svymean(~api00,ds1)
183        mean     SE
184api00 645.14 5.8909
185> options(survey.lonely.psu="remove")
186> svymean(~api00,ds1)
187        mean     SE
188api00 645.14 5.8909
189> options(survey.lonely.psu="certainty")
190> svymean(~api00,ds1)
191        mean     SE
192api00 645.14 5.8909
193> options(survey.lonely.psu="adjust")
194> svymean(~api00,ds1)
195        mean     SE
196api00 645.14 5.8909
197> options(survey.lonely.psu="average")
198> svymean(~api00,ds1)
199        mean     SE
200api00 645.14 5.8909
201>
202> ## with adjustment
203> options(survey.adjust.domain.lonely=TRUE)
204> ds<-svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = subset(apiclus1,dnum !=413))
205> ds1<-ds[-31,]
206> summary(ds1)
207Stratified Independent Sampling design (with replacement)
208svydesign(id = ~1, weights = ~pw, strata = ~dnum, data = subset(apiclus1,
209    dnum != 413))
210Probabilities:
211   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
2120.02954 0.02954 0.02954 0.02954 0.02954 0.02954
213Stratum Sizes:
214           61 135 178 197 255 406 437 448 510 568 637 716 778 815
215obs        13  34   4  13  16   1   4  12  21   9  11  37   2   4
216design.PSU 13  34   4  13  16   2   4  12  21   9  11  37   2   4
217actual.PSU 13  34   4  13  16   1   4  12  21   9  11  37   2   4
218Data variables:
219 [1] "cds"      "stype"    "name"     "sname"    "snum"     "dname"
220 [7] "dnum"     "cname"    "cnum"     "flag"     "pcttest"  "api00"
221[13] "api99"    "target"   "growth"   "sch.wide" "comp.imp" "both"
222[19] "awards"   "meals"    "ell"      "yr.rnd"   "mobility" "acs.k3"
223[25] "acs.46"   "acs.core" "pct.resp" "not.hsg"  "hsg"      "some.col"
224[31] "col.grad" "grad.sch" "avg.ed"   "full"     "emer"     "enroll"
225[37] "api.stu"  "fpc"      "pw"
226>
227> options(survey.lonely.psu="fail")
228> try(svymean(~api00,ds1))
229        mean     SE
230api00 645.14 5.8909
231Warning message:
232In onestrat(x[index, , drop = FALSE], clusters[index], nPSU[index][1],  :
233  Stratum (406) has only one PSU at stage 1
234> options(survey.lonely.psu="remove")
235> svymean(~api00,ds1)
236        mean     SE
237api00 645.14 5.8909
238Warning message:
239In onestrat(x[index, , drop = FALSE], clusters[index], nPSU[index][1],  :
240  Stratum (406) has only one PSU at stage 1
241> options(survey.lonely.psu="certainty")
242> svymean(~api00,ds1)
243        mean     SE
244api00 645.14 5.8909
245Warning message:
246In onestrat(x[index, , drop = FALSE], clusters[index], nPSU[index][1],  :
247  Stratum (406) has only one PSU at stage 1
248> options(survey.lonely.psu="adjust")
249> svymean(~api00,ds1)
250        mean     SE
251api00 645.14 5.9119
252Warning message:
253In onestrat(x[index, , drop = FALSE], clusters[index], nPSU[index][1],  :
254  Stratum (406) has only one PSU at stage 1
255> options(survey.lonely.psu="average")
256> svymean(~api00,ds1)
257        mean     SE
258api00 645.14 6.0914
259Warning message:
260In onestrat(x[index, , drop = FALSE], clusters[index], nPSU[index][1],  :
261  Stratum (406) has only one PSU at stage 1
262>
263> proc.time()
264   user  system elapsed
265  0.449   0.028   0.484
266