8

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.

Jaap
  • 81,064
  • 34
  • 182
  • 193
dixi
  • 680
  • 1
  • 13
  • 27
  • 5
    `d.s <- update( d.s , married = factor( married ) ) ; svymean( ~ married , d.s )` – Anthony Damico Jan 03 '17 at 09:50
  • 3
    The answer I want to accept is from Anthony Damico. How do I accept it as an answer when it is just a comment? – dixi Jan 14 '17 at 16:34
  • Just commenting to see if @AnthonyDamico would like to post his comment as an answer. It is the most straightforward alternative to calculate dispersion measures for proportions with `survey`. – David Jorquera May 22 '20 at 23:18

3 Answers3

5

use svytable

summary(d.s)
svytable(~married+pens, d.s)
svytable(married~pens, d.s)
svytable(married~., d.s) #with all variable
s.brunel
  • 1,003
  • 10
  • 24
4

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.)
Peter Miksza
  • 347
  • 3
  • 11
3

Use prop.table along with svy.table:

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

#margin=1 will give you column percentages