Skip to content

Instantly share code, notes, and snippets.

@leeper
Created May 18, 2020 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeper/4e6e6ae258ffdffce65d543eb01fc8f7 to your computer and use it in GitHub Desktop.
Save leeper/4e6e6ae258ffdffce65d543eb01fc8f7 to your computer and use it in GitHub Desktop.
# setup
library("survey")
data("api")
apiclus1$proportion <- apiclus1$pcttest/100
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
# confint(svymean(...) == svyciprop(..., method = "me")
confint(svymean(~ proportion, dclus1))
## 2.5 % 97.5 %
## proportion 0.9852196 0.9943432
svyciprop(~ proportion, dclus1, method="me")
## 2.5% 97.5%
## proportion 0.990 0.985 0.99
# but svyciprop(..., method = "lo") returns "logit" CIs
svyciprop(~ proportion, dclus1, method="lo")
## 2.5% 97.5%
## proportion 0.990 0.983 0.99
# for subsets, we can use svyby()
svyby(~ proportion, ~ stype, dclus1, FUN = svymean, vartype = "ci")
## stype proportion ci_l ci_u
## E E 0.9921528 0.9891645 0.9951411
## H H 0.9671429 0.9471201 0.9871656
## M M 0.9888000 0.9802725 0.9973275
# which is the same as svyciprop() on a subset()
svyciprop(~ proportion, subset(dclus1, stype == "E"), method="me")
## 2.5% 97.5%
## proportion 0.992 0.989 1
svyciprop(~ proportion, subset(dclus1, stype == "H"), method="me")
## 2.5% 97.5%
## proportion 0.967 0.943 0.99
svyciprop(~ proportion, subset(dclus1, stype == "M"), method="me")
## 2.5% 97.5%
## proportion 0.989 0.979 1
# but if I want svyciprop() confidence intervals on subsets, can I get them with svyby()?
# seems not. it's not this:
svyby(~ proportion, ~ stype, dclus1, FUN = svyciprop)
## stype proportion se.as.numeric(proportion)
## E E 0.9921528 0.001524666
## H H 0.9671429 0.010215856
## M M 0.9888000 0.004350837
@andrigerber
Copy link

I think it'll work like this:

svyby(~ proportion, ~ stype, dclus1, FUN = svyciprop, vartype="ci")

Cheers, maybe a little late.

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