How to calculate proportion using survey package?

12.4k Views Asked by At

This is just a very simple question but I just cant find the right function to use from the web and books.

this is an example I got from one of the post here.

df <- data.frame(sex = c('F', 'M', 'F', 'M', 'M', 'M', 'F', 'F'),
             married = c(1,1,1,1,0,0,1,1),
             pens = c(0, 1, 1, 1, 1, 1, 0, 0),
             weight = c(1.12, 0.55, 1.1, 0.6, 0.23, 0.23, 0.66, 0.67))

d.s <- svydesign(ids=~1, data=df, weights=~weight)

I want to calculate the percentage variables such as married and calculate the standard error too?

also I want to do crosstab of married and pens and get the standard error of the resulting proportion?

how do i do that?

i tried svymean but it would treat the numeric values as integers instead of factors.

3

There are 3 best solutions below

1
s.brunel On

use svytable

summary(d.s)
svytable(~married+pens, d.s)
svytable(married~pens, d.s)
svytable(married~., d.s) #with all variable
2
Peter Miksza On

On approach is using interaction in your formula with svymean or svytotal.

This should give you proportions of responses for each category as well as standard errors.

svymean(~interaction(married, pens), d.s.)

This should give you frequencies for each category as well as standard errors.

svytotal(~interaction(married, pens), d.s.)
2
Cody Melcher On

Use prop.table along with svy.table:

prop.table(svytable((~married, pens),d.s), margin=1)

#margin=1 will give you column percentages