'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.
Solution 1:[1]
use svytable
summary(d.s)
svytable(~married+pens, d.s)
svytable(married~pens, d.s)
svytable(married~., d.s) #with all variable
Solution 2:[2]
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.)
Solution 3:[3]
Use prop.table along with svy.table:
prop.table(svytable((~married, pens),d.s), margin=1)
#margin=1 will give you column percentages
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | s.brunel |
| Solution 2 | Peter Miksza |
| Solution 3 | Cody Melcher |
