如何使用tbl_svysummary确保舍入均匀

问题描述 投票:0回答:1

我正在尝试使用

survey
对象生成描述性统计表
gtsummary
包。我想根据受访者是否投票给特朗普来纳入描述性统计数据的差异。我不知道如何控制
add_differences()
产生的位数,以便它与表的其余部分匹配。

这是一些示例代码:

# Load necessary packages
library(survey)
library(dplyr)
library(gtsummary)

# Create a sample data frame with a weight variable
set.seed(123)
# Create a sample data frame
set.seed(123)
df <- data.frame(
  age_rs = runif(100, 18, 80),  # rescaled age variable
  income_by_20_rs = runif(100, 0, 1),  # rescaled income variable
  physical_health_poor = runif(100, 0, 1),  # continuous physical health variable
  all_religion1 = runif(100, 0, 1),  # continuous religious status variable
  racial_consciousness = runif(100, 0, 1),  # rescaled racial consciousness
  number_discrim_areas_rs = runif(100, 0, 5),  # number of discrimination areas (rescaled)
  vote_trump = factor(sample(c(0, 1), 100, replace = TRUE), labels = c("No", "Yes")),
  weights = runif(100, 0.5, 2)  # weight variable ranging between 0.5 and 2
)

# Create survey design object with weights
df_design <- svydesign(ids = ~1, data = df, weights = ~weights)

# Make table using gtsummary 
tbl1 <- tbl_svysummary(
  data = df_design, 
  by = "vote_trump", 
  statistic = list(all_continuous() ~ "{mean} ({sd})"), 
  include = c(age_rs, income_by_20_rs, physical_health_poor, all_religion1),
  label = list(age_rs = "Age", 
               income_by_20_rs = "Income", 
               physical_health_poor = "Poor Physical Health", 
               all_religion1 = "Religious Devotion"),
  digits = list(all_continuous() ~ c(3,3)),
  missing = "no",
  type = list(c(age_rs, income_by_20_rs, physical_health_poor, all_religion1) ~ "continuous") 
  ) %>% 
  add_difference() 

tbl1

不幸的是,我不知道如何舍入差值和 p 值列,以便它们与描述性统计数据匹配。我也想去掉 CI 栏。

下面是表格的图片:

enter image description here

r gtsummary
1个回答
0
投票

您可以使用

add_difference(estimate_fun)
add_difference(pvalue_fun)
参数来更改对估计值和 p 值进行舍入/格式化的函数。最后,您可以使用
modify_column_hide()
隐藏打印的列。

library(gtsummary)

# Create a sample data frame with a weight variable
set.seed(123)
df <- data.frame(
  age_rs = runif(100, 18, 80),  # rescaled age variable
  income_by_20_rs = runif(100, 0, 1),  # rescaled income variable
  physical_health_poor = runif(100, 0, 1),  # continuous physical health variable
  all_religion1 = runif(100, 0, 1),  # continuous religious status variable
  racial_consciousness = runif(100, 0, 1),  # rescaled racial consciousness
  number_discrim_areas_rs = runif(100, 0, 5),  # number of discrimination areas (rescaled)
  vote_trump = factor(sample(c(0, 1), 100, replace = TRUE), labels = c("No", "Yes")),
  weights = runif(100, 0.5, 2)  # weight variable ranging between 0.5 and 2
)

# Create survey design object with weights
df_design <- survey::svydesign(ids = ~1, data = df, weights = ~weights)

# Make table using gtsummary 
tbl1 <- tbl_svysummary(
  data = df_design, 
  by = "vote_trump", 
  statistic = list(all_continuous() ~ "{mean} ({sd})"), 
  include = c(age_rs, income_by_20_rs, physical_health_poor, all_religion1),
  digits = list(all_continuous() ~ c(3,3)),
  missing = "no",
) |> 
  add_difference(
    # round estimates to 3 decimal places
    estimate_fun = everything() ~ label_style_number(digits = 3),
    # round pvalues to 3 decimal places
    pvalue_fun = label_style_pvalue(digits = 3)
  ) |> 
  # hide the CI: you can run `show_header_names(tbl)` to see the underlying column names
  modify_column_hide(columns = c(conf.low, conf.high))

as_kable(tbl1) # convert to kable to display on stackoverflow
特点 N = 56 N = 67 区别 p 值
年龄_rs 48.274 (17.960) 50.115 (17.450) 1.842 0.625
收入_by_20_rs 0.563 (0.299) 0.481 (0.242) -0.082 0.171
身体健康状况不佳 0.437 (0.276) 0.519 (0.302) 0.082 0.179
所有_宗教1 0.449 (0.279) 0.536 (0.297) 0.088 0.151

创建于 2024 年 12 月 30 日,使用 reprex v2.1.1

© www.soinside.com 2019 - 2024. All rights reserved.