dixi
dixi

Reputation: 710

How to calculate proportion using survey package?

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.

Upvotes: 8

Views: 13821

Answers (3)

Cody Melcher
Cody Melcher

Reputation: 51

Use prop.table along with svy.table:

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

#margin=1 will give you column percentages

Upvotes: 5

Peter Miksza
Peter Miksza

Reputation: 409

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.)

Upvotes: 6

s.brunel
s.brunel

Reputation: 1043

use svytable

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

Upvotes: 6

Related Questions