Jesus Esteban
Jesus Esteban

Reputation: 41

tbl_svysummary unexpected format for p_unweighted

I would like to include weighted and unweighted n/N, and their respective proportions in a gtsummary table . According the help of gtsummary, both p and unweighted p will be formatted as percentage

See <https://www.danieldsjoberg.com/gtsummary/reference/tbl_svysummary.html>

, but I was unable to obtain the "p unweighted" formatted as percentage.

This is the reprex using data from Academic Performance Dataset from survey package.

require(survey)
data(api)

dstrat <- svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc
                    ,variables = apistrat[,c('pcttest','growth','awards')])
dstrat <- svydesign(id=~1,strata=~stype, weights=~pw, data=apistrat, fpc=~fpc ,variables = apistrat[,c('pcttest','growth','awards')])

gtsummary::tbl_svysummary(data=dstrat ,type=list(pcttest~'continuous',growth~'continuous',awards~'categorical') ,statistic = list( all_continuous()~c("{mean} ({sd})")

                        ,all_categorical()~c("{n}/{N} ({p}%) ; {n_unweighted}/{N_unweighted} ({p_unweighted}%)")
                        
                      )

)

This is the table output.

gtsummary table output Am I missing something?

Thank you.

R version 4.1.2 (2021-11-01)

packageVersion('survey') [1] ‘4.1.1’

packageVersion('gtsummary') [1] ‘1.5.2’

Upvotes: 1

Views: 202

Answers (1)

Daniel D. Sjoberg
Daniel D. Sjoberg

Reputation: 11719

You've found a bug in the default behavior of the unweighted proportion. Thank you for reporting! You can follow this GitHub Issue to track the resolution https://github.com/ddsjoberg/gtsummary/issues/1181

In the meantime, this code will get you nicely formatted tables.

library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.5.2'

data(api, package = "survey")

dstrat <- 
  survey::svydesign(id = ~1, strata = ~stype, 
                    weights = ~pw, data = apistrat, fpc = ~fpc, 
                    variables = apistrat[, c("pcttest", "growth", "awards")])

gtsummary::tbl_svysummary(
  data = dstrat, 
  include = awards,
  type = awards ~ "categorical", 
  statistic = all_categorical() ~ "{n}/{N} ({p}%) ; {n_unweighted}/{N_unweighted} ({p_unweighted}%)",
  digits = all_categorical() ~ list(0, 0, 0, 0, 0, style_percent)
) %>%
  as_kable()
Characteristic N = 6,194
awards
No 2,236/6,194 (36%) ; 87/200 (44%)
Yes 3,958/6,194 (64%) ; 113/200 (56%)

Created on 2022-03-21 by the reprex package (v2.0.1)

Upvotes: 0

Related Questions