How to extract attributes values from svyciprop object?
Asked Answered
A

2

6

How can I extract attributes from svyciprop object below to a data.frame:

library(survey)
api <- read.dta(file = "http://www.ats.ucla.edu/stat/stata/library/apipop.dta")
data(api)
dclus1 <- svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
prop.ci <- svyciprop(~I(ell==0), dclus1, method="li")

printing

prop.ci

Yields:

> prop.ci

                         2.5% 97.5%
I(ell == 0) 0.021858 0.000664  0.11

str(prop.ci)
> str(prop.ci)
  Class 'svyciprop'  atomic [1:1] 0.0219
    ..- attr(*, "var")= num [1, 1] 0.000512
    .. ..- attr(*, "dimnames")=List of 2
    .. .. ..$ : chr "as.numeric(I(ell == 0))"
    .. .. ..$ : chr "as.numeric(I(ell == 0))"
    ..- attr(*, "ci")= Named num [1:2] 0.000664 0.107778
    .. ..- attr(*, "names")= chr [1:2] "2.5%" "97.5%"
Aut answered 23/12, 2014 at 11:44 Comment(3)
What attributes do you need?Maryalice
Proportion and confidence intervals (2.5% and 97.5%) each seperate, which are: 0.021858, 0.000664 and 0.107778Aut
Why didn't the package authors simply make the output a vector already? rbinding a ton of these is a pain.Footwear
F
8

You can use the following commands to extract the proportion and the confidence interval from the object prop.ci:

# the proportion
as.vector(prop.ci)
# [1] 0.02185792

# the confidence interval
attr(prop.ci, "ci")
#         2.5%        97.5% 
# 0.0006639212 0.1077784084 

If you want to access the values of the confidence interval separately, you can use vector indexing:

ci <- attr(prop.ci, "ci")
ci[1]
#         2.5% 
# 0.0006639212 
ci[2]
#     97.5% 
# 0.1077784 
Floatable answered 23/12, 2014 at 11:57 Comment(0)
E
3

in case it's easier to remember..

# everything
prop.ci

# extract just the proportion
prop.ci[ 1 ]

# extract the confidence interval
confint( prop.ci )

# lower bound
confint( prop.ci )[ 1 ]

# upper bound
confint( prop.ci )[ 2 ]
Expense answered 25/12, 2014 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.