Search code examples
rsurvey

r survey package - no confidence interval in svyby


I have a survey data and I want to generate proportion with confidence interval with svyby function but I could not get the confidence interval and I only get the proportion.

library(haven)
library(survey)
Data_STEPs2016 \<- read_dta("Downloads/New Data/Data_STEPs2016.dta")
steps1395 \<- svydesign(id=Data_STEPs2016$code,weights = Data_STEPs2016$weight_Q,data = Data_STEPs2016)
svyby(\~t2,\~i07,design = steps1395, FUN = svyciprop, keep.var = FALSE)

The result I get is something like this:

svyby(\~t2,\~i07,design = steps1395, FUN = svyciprop, keep.var = FALSE)

i07  statistic
0    0 0.14332878
1    1 0.11922225
2    2 0.10394157
3    3 0.12091622
4    4 0.14260478
5    5 0.10390742
6    6 0.09138277
7    7 0.09750685
8    8 0.07722408
9    9 0.07022950
10  10 0.10392619
11  11 0.06465898
12  12 0.12214870
13  13 0.11607801
14  14 0.11861874
15  15 0.09637536
16  16 0.05414722
17  17 0.07987077
18  18 0.08505400
19  19 0.09966016
20  20 0.09301776
21  21 0.07819750
22  22 0.07396172
23  23 0.10893622
24  24 0.12906301
26  26 0.13013662
27  27 0.05522789
28  28 0.05326462
29  29 0.05739153
30  30 0.12571282

I did get answer with the following code :

svytable(\~t2+i07,steps1395)
prop.table(svytable(\~t2+i07,steps1395),margin = 1)
confint(prop.table(svytable(\~t2+i07,steps1395),margin = 1))
svyciprop(\~t2,steps1395,method = "likelihood",level = 0.95)`

But I need proportion by groups such as gender or location of living. I also read the manual but in the svyby parts there is example of svyciprop as the function in the svyby.


Solution

  • svyby has an option to show confidence intervals. For example, with one of the built-in example datasets

    > svyby(~I(ell>50), ~stype, dclus1, svyciprop,vartype="ci")
      stype I(ell > 50)        ci_l      ci_u
    E     E  0.11805556 0.066048776 0.2021490
    H     H  0.07142857 0.005409131 0.5210736
    M     M  0.04000000 0.003850809 0.3099199
    

    To check: this is what you get for just the first subset by hand

    > svyciprop(~I(ell>50), subset(dclus1,stype=="E"))
                       2.5% 97.5%
    I(ell > 50) 0.118 0.066   0.2