r_rabbit
r_rabbit

Reputation: 121

How to obtain the same results using the survey::svyby() function with the svymean argument and the gtsummary::tbl_svysummary function?

I would like to reproduce the same result as svyby, with the svymean argument, with the tbl_svysummary() function. It is possible? I tried using the argument #statistic = (all_categorical() ~ "{mean} ({sd})"). But it did not work. Any help?

library(survey)
library(gtsummary)

data(api)
dclus1 <- svydesign(id=~dnum, weights=~pw, data=apiclus1, fpc=~fpc)

svyby(~both, ~stype, dclus1, svymean)

dclus1 %>% 
  gtsummary::tbl_svysummary(by = both,  
                            include =  stype, 
                            #statistic = (all_categorical() ~ "{mean} ({sd})")
                            )

The result of svyby is:

enter image description here

I tried using the argument statistic = (all_categorical() ~ "{mean} ({sd})"), but it did not work.

Upvotes: 0

Views: 278

Answers (1)

r_rabbit
r_rabbit

Reputation: 121

To get the same result on the rows, just like the svyby function, I used the argument {percent = "row"} and {statistic = all_categorical() ~ "{p}"}, as the following:

dclus1 %>% 
  gtsummary::tbl_svysummary(by = both,  
                            include =  stype, 
                            percent = "row",
                            digits = list(stype ~ 1),
                            statistic = all_categorical() ~ "{p}")  

The only difference is that in svyby the values are in proportion and with the tbl_svysummary function it is in percentage. Also I don't have de se (stardart error) columns, but that is ok. The result was:

enter image description here

Upvotes: 0

Related Questions