find weighted frequency & se’s for vector of variables

We could use paste to create the formula

out1 <- svymean(as.formula(paste0('~interaction(', toString(vars), ')')), design)

out1
#                                         mean     SE
#interaction(sex, married, pens)F.0.0 0.000000 0.0000
#interaction(sex, married, pens)M.0.0 0.000000 0.0000
#interaction(sex, married, pens)F.1.0 0.474806 0.2109
#interaction(sex, married, pens)M.1.0 0.000000 0.0000
#interaction(sex, married, pens)F.0.1 0.000000 0.0000
#interaction(sex, married, pens)M.0.1 0.089147 0.0717
#interaction(sex, married, pens)F.1.1 0.213178 0.1945
#interaction(sex, married, pens)M.1.1 0.222868 0.1567

testing with manual entry

out2 <- svymean(~interaction(sex,married, pens), design)
identical(out1, out2)
#[1] TRUE

Update

To do this separately, we can use lapply

outlst1 <- lapply(vars, function(x) 
    svymean(as.formula(paste0('~interaction(', x, ')')), design))

outlst1
#[[1]]
#                     mean     SE
#interaction(sex)F 0.68798 0.1721
#interaction(sex)M 0.31202 0.1721

#[[2]]
#                          mean     SE
#interaction(married)0 0.089147 0.0717
#interaction(married)1 0.910853 0.0717

#[[3]]
#                      mean     SE
#interaction(pens)0 0.47481 0.2109
#interaction(pens)1 0.52519 0.2109

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top