如何使用带有svymean参数的survey::svyby()函数和gtsummary::tbl_svysummary函数获得相同的结果?

xxslljrj  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(276)

我想用svymean参数,用tbl_svysummary()函数重现与svyby相同的结果。这是可能的吗?我尝试使用参数#statistic =(all_categorical()~“{mean}({sd})”)。但它不起作用。有帮助吗?

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})")
                            )

svyby的结果是:

我尝试使用参数statistic = (all_categorical() ~ "{mean} ({sd})"),但它不起作用。

qnakjoqk

qnakjoqk1#

为了在行上得到相同的结果,就像svyby函数一样,我使用了参数{percent =“row”}和{statistic = all_categorical()~“{p}"},如下所示:

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

唯一的区别是,在svyby中,值是按比例的,而tbl_svysummary函数是按百分比的。此外,我没有de se(stardart error)列,但这是可以的。结果是:

相关问题