I would like to find the weighted frequencies with their SE's for all the variables in my data.
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))
design <- svydesign(ids=~1, data=df, weights=~weight)
To get the weighted freqs for one variable:
svymean(~interaction(married), design)
mean SE
interaction(married)0 0.089147 0.0717
interaction(married)1 0.910853 0.0717
My actual data set is large, and I want to run this on ALL the variables at once.
vars <- c("sex","married","pens")
I tried this, but it throws an error.
svymean(~interaction(reformulate(vars)), design)
Error in unique.default(x, nmax = nmax) :
unique() applies only to vectors
I can also do this--but running svytable does not help me, because I need the SE's.
for(i in seq_along(vars)){
print(prop.table(svytable(bquote(~.(as.name(vars[i]))), design)))
}
EDIT
I want to run svymean(~interaction(var), design) on each variable in my df separately. So instead of having to run this many times like so:
svymean(~interaction(married), design)
svymean(~interaction(sex), design)
svymean(~interaction(pen), design)
I want to able to loop through this command for each variable in my vectored list of names (vars).
Any suggestions!??