从调查对象创建汇总统计的分层 table(枢轴 table)
Create a stratified table (pivot table) of summary statistics from a survey object
我在通过 gtsummary::tbl_svysummary.
驱动创建具有子级别(两级分层;嵌套)的摘要 table 时遇到问题
我已经使用以下变量创建了对象 survey::svydesign:性别、国家/地区、结果、n(加权)。我设法创建了 table,但是我无法按性别创建额外的层。我已经把我想要的表格结构放在下面了。
我什至可以生成单独的 tables(通过过滤器嵌套每个国家)并堆叠它们。但是,有21个国家,我想要一个更理性的策略。
拜托,任何建议...
代码
# dataframe
df <-
data.frame(
Country = c("Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3"),
Result = c("A", "B", "C",
"B", "C", "A",
"C", "A", "B",
"A", "B", "C"),
Gender = c("M", "M", "M",
"W", "W", "W",
"M", "M", "M",
"W", "W", "W"),
n = c(583, 607, 1217,
487, 1100, 820,
178, 304, 367,
223,444,112))
df
# Create a weighted survey design object
df_survey <- survey::svydesign(~1,
data = df,
weights = ~n)
# Table
Table <- df_survey %>%
tbl_svysummary(by = Result,
percent = "row") %>%
add_overall(last = TRUE,
col_label = "**n(row)**") %>%
modify_header(label = "",
stat_by = "**{level}**") %>%
bold_labels() %>%
italicize_levels() %>%
italicize_labels()
Table
这是一个非常接近您提供的模型的示例 table。您可以使用 as_flex_table()
将输出转换为 flextable 以使其更相似。
library(tidyverse)
library(gtsummary)
df <-
tibble(
Country = c("Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3"),
Result = c("A", "B", "C",
"B", "C", "A",
"C", "A", "B",
"A", "B", "C"),
Gender = c("M", "M", "M",
"W", "W", "W",
"M", "M", "M",
"W", "W", "W"),
n = c(583, 607, 1217,
487, 1100, 820,
178, 304, 367,
223,444,112))
# Create a weighted survey design object
df_survey <- survey::svydesign(~1,
data = df,
weights = ~n)
df_results <-
tibble(Country = unique(df_survey$variables$Country)) %>%
rowwise() %>%
mutate(
# subset the design object within each country
design = df_survey[df_survey$variables$Country %in% Country, ] %>% list(),
# construct gtsummary table within each stratum
tbl =
design %>%
tbl_svysummary(by = Result,
percent = "row",
include = -Country) %>%
add_overall(last = TRUE,
col_label = "**n(row)**") %>%
modify_header(label = "",
stat_by = "**{level}**") %>%
modify_footnote(everything() ~ NA) %>%
italicize_levels() %>%
italicize_labels() %>%
list()
)
# stack tables
tbl <-
tbl_stack(
tbls = df_results$tbl,
group_header = df_results$Country
)
我在通过 gtsummary::tbl_svysummary.
驱动创建具有子级别(两级分层;嵌套)的摘要 table 时遇到问题我已经使用以下变量创建了对象 survey::svydesign:性别、国家/地区、结果、n(加权)。我设法创建了 table,但是我无法按性别创建额外的层。我已经把我想要的表格结构放在下面了。
我什至可以生成单独的 tables(通过过滤器嵌套每个国家)并堆叠它们。但是,有21个国家,我想要一个更理性的策略。
拜托,任何建议...
代码
# dataframe
df <-
data.frame(
Country = c("Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3"),
Result = c("A", "B", "C",
"B", "C", "A",
"C", "A", "B",
"A", "B", "C"),
Gender = c("M", "M", "M",
"W", "W", "W",
"M", "M", "M",
"W", "W", "W"),
n = c(583, 607, 1217,
487, 1100, 820,
178, 304, 367,
223,444,112))
df
# Create a weighted survey design object
df_survey <- survey::svydesign(~1,
data = df,
weights = ~n)
# Table
Table <- df_survey %>%
tbl_svysummary(by = Result,
percent = "row") %>%
add_overall(last = TRUE,
col_label = "**n(row)**") %>%
modify_header(label = "",
stat_by = "**{level}**") %>%
bold_labels() %>%
italicize_levels() %>%
italicize_labels()
Table
这是一个非常接近您提供的模型的示例 table。您可以使用 as_flex_table()
将输出转换为 flextable 以使其更相似。
library(tidyverse)
library(gtsummary)
df <-
tibble(
Country = c("Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3",
"Country 1", "Country 2", "Country 3"),
Result = c("A", "B", "C",
"B", "C", "A",
"C", "A", "B",
"A", "B", "C"),
Gender = c("M", "M", "M",
"W", "W", "W",
"M", "M", "M",
"W", "W", "W"),
n = c(583, 607, 1217,
487, 1100, 820,
178, 304, 367,
223,444,112))
# Create a weighted survey design object
df_survey <- survey::svydesign(~1,
data = df,
weights = ~n)
df_results <-
tibble(Country = unique(df_survey$variables$Country)) %>%
rowwise() %>%
mutate(
# subset the design object within each country
design = df_survey[df_survey$variables$Country %in% Country, ] %>% list(),
# construct gtsummary table within each stratum
tbl =
design %>%
tbl_svysummary(by = Result,
percent = "row",
include = -Country) %>%
add_overall(last = TRUE,
col_label = "**n(row)**") %>%
modify_header(label = "",
stat_by = "**{level}**") %>%
modify_footnote(everything() ~ NA) %>%
italicize_levels() %>%
italicize_labels() %>%
list()
)
# stack tables
tbl <-
tbl_stack(
tbls = df_results$tbl,
group_header = df_results$Country
)