• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

使用(gtsummary)tbl_svysummaary 函数显示survey.design 对象的置信区间?

用户头像
it1352
帮助1

问题说明

我正在使用 tbl_svysummary() 函数从 {survey} 包创建的survey.design 对象生成汇总统计表.使用 add_stat() 函数的方法.但是,我在尝试使用 add_stat() 函数时遇到错误.

I am using the tbl_svysummary() function for producing summary statistics tables from survey.design objects created by the {survey} package. The way one would go about it using the add_stat() function. However, I am facing an error when trying to use the add_stat() function.

ci <- function(vv1, vv2, dsgn) {
  svyby( as.formula( paste0( "~" , vv1)) , by = as.formula( paste0( "~" , vv2)), DHSdesign, svyciprop, vartype="ci")
}

tbl_svysummary_ex2 <-
  survey::svydesign(id= folate$EA_ID, strata=NULL,
                    weights = folate$weight,
                    data = folate) %>%
  tbl_svysummary(by = "folate_deficiency",
                 percent = "row", include = c(folate_deficiency,
                                              age_group,Region_Name, serum_folate,rbc_folate)) %>% 
 add_stat(
    fns = everything() ~ "Ci",
    location = "level",
    header = "**95% CI**"
  ) %>%
  modify_footnote(everything() ~ NA)

希望有人能帮帮我.

正确答案

#1

您正在寻找的示例表以及我可以在我的机器上运行的代码将有助于您准确找到您正在寻找的内容为了.我在下面创建了一个可重复的示例,我认为它至少可以让您接近您正在寻找的内容?

An example table of what you're looking for along with code that I can run on my machine would have been helpful to get you exactly what you're looking for. I've created a reproducible example below that I think gets you at least close to what you're looking for?

library(gtsummary)
library(survey)

svy_trial <-
  svydesign(~1, data = trial %>% select(trt, response, death), weights = ~1) 

ci <- function(variable, by, data, ...) {
  svyby(as.formula( paste0( "~" , variable)) , by = as.formula( paste0( "~" , by)), data, svyciprop, vartype="ci") %>%
    tibble::as_tibble() %>%
    dplyr::mutate_at(vars(ci_l, ci_u), ~style_number(., scale = 100) %>% paste0("%")) %>%
    dplyr::mutate(ci = stringr::str_glue("{ci_l}, {ci_u}")) %>%
    dplyr::select(all_of(c(by, "ci"))) %>%
    tidyr::pivot_wider(names_from = all_of(by), values_from = ci) %>%
    set_names(paste0("add_stat_", seq_len(ncol(.))))
}

ci("response", "trt", svy_trial)
#> # A tibble: 1 x 2
#>   add_stat_1 add_stat_2
#>   <glue>     <glue>    
#> 1 21%, 40%   25%, 44%  

svy_trial %>%
  tbl_svysummary(by = "trt", missing = "no") %>%
  add_stat(everything() ~ "ci") %>%
  modify_table_body(
    dplyr::relocate, add_stat_1, .after = stat_1
  ) %>%
  modify_header(starts_with("add_stat_") ~ "**95% CI**") %>%
  modify_footnote(everything() ~ NA)

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfhfbf
系列文章
更多 icon
同类精品
更多 icon
继续加载