Reputation: 53
I'm interested in using confidence intervals calculated from the survey package, as is done here:
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
df <- svyciprop(~I(ell==0), dclus1, method="li")
str(df)
I am having trouble assigning this to an object for later use, though, and it seems as if the only way I can get it out is by copying and pasting, which can't possibly be true.
How do I create a dataframe from this object so that I can use the confidence intervals elsewhere?
I would like an output table that looks like this:
Variable | Value | Estimate | Low_ci | Hi_ci |
---|---|---|---|---|
ell | 0 | 0.02185 | 0.000664 | 0.11 |
ell | 1 | 0.0273 | 0.0034 | 0.09 |
Upvotes: 2
Views: 374
Reputation: 2765
You can extract the point estimate with as.numeric
and the confidence interval with confint
> data.frame(p=as.numeric(df), ci=confint(df))
p ci.2.5. ci.97.5.
I(ell == 0) 0.02185792 0.0006639212 0.1077784
I don't know what you mean about a data frame with a row for ell==1
: ell
is not a binary variable: it's a percentage, with quite a lot of zeros
> summary(apiclus1$ell)
Min. 1st Qu. Median Mean 3rd Qu. Max.
0.00 16.00 26.00 27.61 37.00 73.00
If you had a set of confidence intervals to compute, you might use svyby
to get them in a data frame. For example, the proportion of ell==0
by school type:
> svyby(~I(ell==0),~stype, design=dclus1, svyciprop,vartype="ci")
stype I(ell == 0) ci_l ci_u
E E 0.01388889 0.001383745 0.1252329
H H 0.07142857 0.010052692 0.3681672
M M 0.04000000 0.003850809 0.3099199
> is.data.frame(svyby(~I(ell==0),~stype, design=dclus1, svyciprop,vartype="ci"))
[1] TRUE
Upvotes: 1