I am attempting to produce a table of weighted mean survey scores by categorical variables in gtsummary using the tbl_svysummary
function.
How do I do this using survey weights, using the tbl_svysummary
function? I've gone through the two examples that are close on here with solutions provided by Daniel Sjoberg, but I just don't really understand what is happening with them. I can do this by transposing the by
and include
arguments in tbl_svysummary
, but then I have a whole new problem of the table being the wrong way round.
Here's some sample data:
dat <- structure(list(uuid = c("p41112019021430", "p41222013024584",
"p41212017017560", "p41212017011700", "p41212019022003", "p41212019133026",
"p41212017014434", "p41112019023063", "p41212019077561", "p41212017050030"
), age_cat = structure(c(3L, 1L, 4L, 2L, 2L, 3L, 3L, 2L, 1L,
4L), levels = c("18-24", "25-44", "45-64", "65-74", "75+"), class = "factor"),
cvh_score = c(6, 4, 1, 3, 0, 2, 3, 2, 6, 1), weights = c(p41112019021430 = 0.360602284454939,
p41222013024584 = 5.00004172246093, p41212017017560 = 0.276025143197602,
p41212017011700 = 1.55086389757734, p41212019022003 = 2.20669366738008,
p41212019133026 = 0.878664071962474, p41212017014434 = 1.15252329666968,
p41112019023063 = 1.51638372307208, p41212019077561 = 2.1408232841115,
p41212017050030 = 0.282529671403006)), row.names = c(NA,
-10L), class = "data.frame")
dat_svy <- dat |>
srvyr::as_survey_design(
ids = uuid,
weights = weights
)
I can achieve what I am looking for without weights using the tbl_continuous
function like follows:
tbl_01 <- gtsummary::tbl_continuous(
dat,
variable = cvh_score,
include = c(age_cat),
statistic = list(
everything() ~ "{mean} ({sd})"
)
)
tbl_01
which gives me the following:
How do I do this with weights?