连续权重变量的 gtsummary tbl_stack 和 tbl_svysummary 问题
Problem with gtsummary tbl_stack and tbl_svysummary for continuous weight variables
问题:
我正在尝试为体重普查的连续变量创建表格 table。我想用分类变量 c("Pobre Extremo", "Pobre No Extremo")
来分隔这个连续变量的均值。所以我想要的 table 看起来像这样(数字是随机的):
这是过去一年的 table,所以当我 运行 我的新年语法 运行 进入这个问题时 table:
我已经验证了是否是标记数据的问题,但似乎那边没问题。
目前 table 的语法:
data %>%
as_label() %>%
as_survey_design(weight = fac500a) %>%
tbl_strata2(
strata = pobreza,
~ .x %>%
tbl_svysummary(
by = ocupinf,
include = ing_cap,
missing = "no",
statistic = list(all_continuous() ~ "{mean}"),
label = list(ing_cap = .y)
) %>%
modify_header(all_stat_cols() ~ "**{level}**") %>%
add_overall(., col_label = "Total"),
.combine_with = "tbl_stack",
.combine_args = list(group_header = NULL)
) %>%
modify_table_body(
~ .x %>%
mutate(variable = "pobreza", row_type = "level") %>%
tibble::add_row(
row_type = "label",
variable = "pobreza",
label = "pobreza",
.before = 1L
)
) %>%
modify_column_indent(columns = label, rows = row_type == "level") %>%
bold_labels() %>%
modify_footnote(all_stat_cols() ~ "ing_cap: Mean")
这是我的数据:
我运行函数dput()
:
structure(list(ing_cap = structure(c(3153.9033203125, 3153.9033203125,
3153.9033203125, 3153.9033203125, 2420.76844618056, 1920.38439941406,
1920.38439941406, 2773.28385416667, 3264.26846590909, 3264.26846590909,
4211.30403645833, 3283.44856770833, 3481.44609375, 3481.44609375,
6730.44587053571, 6730.44587053571, 6730.44587053571, 3571.64485677083,
3571.64485677083, 6990.048828125, 6990.048828125, 6990.048828125,
6374, 6374, 6374, 3532.215625, 5201.203125, 5201.203125, 4516.51395089286,
4516.51395089286, 10841.1783854167, 10841.1783854167, 10841.1783854167,
6081.54609375, 6081.54609375, 6081.54609375, 6081.54609375, 3669.76139322917,
3669.76139322917, 3669.76139322917, 3460.72778320312, 5076.026953125,
2791.3478515625, 5264.654296875, 5264.654296875, 3697.99633789062,
3697.99633789062, 5197.804296875, 4063.18391927083, 3975.13313802083,
3975.13313802083, 6428.6640625, 6428.6640625, 5685.87834821429,
5685.87834821429, 5685.87834821429, 5685.87834821429, 5737.10049715909,
5737.10049715909, 4644, 4644, 4644, 5124.6, 5124.6, 1830.73010253906,
3699.47631835938, 8673.4296875, 8673.4296875, 8673.4296875, 3629.81884765625,
3629.81884765625, 6726.15364583333, 6726.15364583333, 6726.15364583333,
5125.9521484375, 5125.9521484375, 7991.56591796875, 7991.56591796875,
8089.87926136364, 8089.87926136364, 8089.87926136364, 8089.87926136364,
2730.0802734375, 13985.8271484375, 13985.8271484375, 13985.8271484375,
13985.8271484375, 5944.7998046875, 5944.7998046875, 5944.7998046875,
2476.12651909722, 2476.12651909722, 2476.12651909722, 2476.12651909722,
13624, 7012.70654296875, 7012.70654296875, 7012.70654296875,
7012.70654296875, 6648.0015625), label = "Ingreso per cápita en el hogar", class = c("haven_labelled_spss",
"haven_labelled", "vctrs_vctr", "double")), fac500a = structure(c(354.4443359375,
269.111877441406, 467.653961181641, 467.653961181641, 345.380615234375,
1201.30834960938, 1262.73962402344, 1383.26965332031, 1191.63061523438,
935.718688964844, 769.666625976562, 1235.62524414062, 391.513061523438,
341.510711669922, 391.513061523438, 287.484558105469, 287.484558105469,
334.339538574219, 291.639129638672, 262.072875976562, 251.327713012695,
251.327713012695, 347.6591796875, 229.84504699707, 255.283050537109,
221.039138793945, 258.418426513672, 329.677368164062, 217.956893920898,
242.079177856445, 420.243377685547, 536.125610351562, 467.653961181641,
258.418426513672, 247.823104858398, 242.079177856445, 247.823104858398,
536.125610351562, 403.013153076172, 393.672302246094, 334.339538574219,
345.244873046875, 506.461639404297, 376.046264648438, 417.665008544922,
178.606018066406, 218.673873901367, 334.339538574219, 167.823974609375,
267.186492919922, 316.787017822266, 446.065185546875, 294.903411865234,
287.572387695312, 329.677368164062, 247.823104858398, 258.418426513672,
404.189147949219, 404.189147949219, 267.218231201172, 404.189147949219,
352.567840576172, 216.117523193359, 323.650573730469, 287.572387695312,
329.677368164062, 329.677368164062, 287.572387695312, 217.956893920898,
329.677368164062, 287.572387695312, 287.572387695312, 329.677368164062,
242.079177856445, 334.339538574219, 291.639129638672, 262.072875976562,
245.502563476562, 221.039138793945, 291.639129638672, 245.502563476562,
245.502563476562, 334.339538574219, 205.848175048828, 234.055160522461,
228.630340576172, 311.361968994141, 205.848175048828, 234.055160522461,
271.596160888672, 221.039138793945, 245.502563476562, 251.327713012695,
251.327713012695, 258.418426513672, 217.956893920898, 258.418426513672,
242.079177856445, 329.677368164062, 355.819549560547), label = "Factor de Expansión de Empleo/Ingresos proyecciones CPV-2007", format.spss = "F8.2"),
ocupinf = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2,
1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1,
1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1,
1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1,
1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2), label = "Situación de informalidad (ocup.principal)", format.spss = "F8.2", labels = c(`empleo informal` = 1,
`empleo formal` = 2), class = c("haven_labelled", "vctrs_vctr",
"double")), pobreza = structure(c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), label = "pobreza", format.spss = "F8.2", labels = c(`Pobre Extremo` = 1,
`Pobre No Extremo` = 2, `No Pobre` = 3), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -100L), class = c("tbl_df",
"tbl", "data.frame"))
如果您发现任何见解,请告诉我。
正如@DanielD.Sjoberg 在评论中指出的那样,有必要明确指出行中的每个变量都是连续变量。所以在代码中加入type = everything() ~ "continuous"
:
data %>%
as_label() %>%
as_survey_design(weight = fac500a) %>%
tbl_strata2(
strata = pobreza,
~ .x %>%
tbl_svysummary(
by = ocupinf,
include = ing_cap,
missing = "no",
type = everything() ~ "continuous",
statistic = list(all_continuous() ~ "{mean}"),
label = list(ing_cap = .y)
) %>%
modify_header(all_stat_cols() ~ "**{level}**") %>%
add_overall(., col_label = "Total"),
.combine_with = "tbl_stack",
.combine_args = list(group_header = NULL)
) %>%
modify_table_body(
~ .x %>%
mutate(variable = "pobreza", row_type = "level") %>%
tibble::add_row(
row_type = "label",
variable = "pobreza",
label = "pobreza",
.before = 1L
)
) %>%
modify_column_indent(columns = label, rows = row_type == "level") %>%
bold_labels() %>%
modify_footnote(all_stat_cols() ~ "ing_cap: Mean")
问题:
我正在尝试为体重普查的连续变量创建表格 table。我想用分类变量 c("Pobre Extremo", "Pobre No Extremo")
来分隔这个连续变量的均值。所以我想要的 table 看起来像这样(数字是随机的):
这是过去一年的 table,所以当我 运行 我的新年语法 运行 进入这个问题时 table:
我已经验证了是否是标记数据的问题,但似乎那边没问题。
目前 table 的语法:
data %>%
as_label() %>%
as_survey_design(weight = fac500a) %>%
tbl_strata2(
strata = pobreza,
~ .x %>%
tbl_svysummary(
by = ocupinf,
include = ing_cap,
missing = "no",
statistic = list(all_continuous() ~ "{mean}"),
label = list(ing_cap = .y)
) %>%
modify_header(all_stat_cols() ~ "**{level}**") %>%
add_overall(., col_label = "Total"),
.combine_with = "tbl_stack",
.combine_args = list(group_header = NULL)
) %>%
modify_table_body(
~ .x %>%
mutate(variable = "pobreza", row_type = "level") %>%
tibble::add_row(
row_type = "label",
variable = "pobreza",
label = "pobreza",
.before = 1L
)
) %>%
modify_column_indent(columns = label, rows = row_type == "level") %>%
bold_labels() %>%
modify_footnote(all_stat_cols() ~ "ing_cap: Mean")
这是我的数据:
我运行函数dput()
:
structure(list(ing_cap = structure(c(3153.9033203125, 3153.9033203125,
3153.9033203125, 3153.9033203125, 2420.76844618056, 1920.38439941406,
1920.38439941406, 2773.28385416667, 3264.26846590909, 3264.26846590909,
4211.30403645833, 3283.44856770833, 3481.44609375, 3481.44609375,
6730.44587053571, 6730.44587053571, 6730.44587053571, 3571.64485677083,
3571.64485677083, 6990.048828125, 6990.048828125, 6990.048828125,
6374, 6374, 6374, 3532.215625, 5201.203125, 5201.203125, 4516.51395089286,
4516.51395089286, 10841.1783854167, 10841.1783854167, 10841.1783854167,
6081.54609375, 6081.54609375, 6081.54609375, 6081.54609375, 3669.76139322917,
3669.76139322917, 3669.76139322917, 3460.72778320312, 5076.026953125,
2791.3478515625, 5264.654296875, 5264.654296875, 3697.99633789062,
3697.99633789062, 5197.804296875, 4063.18391927083, 3975.13313802083,
3975.13313802083, 6428.6640625, 6428.6640625, 5685.87834821429,
5685.87834821429, 5685.87834821429, 5685.87834821429, 5737.10049715909,
5737.10049715909, 4644, 4644, 4644, 5124.6, 5124.6, 1830.73010253906,
3699.47631835938, 8673.4296875, 8673.4296875, 8673.4296875, 3629.81884765625,
3629.81884765625, 6726.15364583333, 6726.15364583333, 6726.15364583333,
5125.9521484375, 5125.9521484375, 7991.56591796875, 7991.56591796875,
8089.87926136364, 8089.87926136364, 8089.87926136364, 8089.87926136364,
2730.0802734375, 13985.8271484375, 13985.8271484375, 13985.8271484375,
13985.8271484375, 5944.7998046875, 5944.7998046875, 5944.7998046875,
2476.12651909722, 2476.12651909722, 2476.12651909722, 2476.12651909722,
13624, 7012.70654296875, 7012.70654296875, 7012.70654296875,
7012.70654296875, 6648.0015625), label = "Ingreso per cápita en el hogar", class = c("haven_labelled_spss",
"haven_labelled", "vctrs_vctr", "double")), fac500a = structure(c(354.4443359375,
269.111877441406, 467.653961181641, 467.653961181641, 345.380615234375,
1201.30834960938, 1262.73962402344, 1383.26965332031, 1191.63061523438,
935.718688964844, 769.666625976562, 1235.62524414062, 391.513061523438,
341.510711669922, 391.513061523438, 287.484558105469, 287.484558105469,
334.339538574219, 291.639129638672, 262.072875976562, 251.327713012695,
251.327713012695, 347.6591796875, 229.84504699707, 255.283050537109,
221.039138793945, 258.418426513672, 329.677368164062, 217.956893920898,
242.079177856445, 420.243377685547, 536.125610351562, 467.653961181641,
258.418426513672, 247.823104858398, 242.079177856445, 247.823104858398,
536.125610351562, 403.013153076172, 393.672302246094, 334.339538574219,
345.244873046875, 506.461639404297, 376.046264648438, 417.665008544922,
178.606018066406, 218.673873901367, 334.339538574219, 167.823974609375,
267.186492919922, 316.787017822266, 446.065185546875, 294.903411865234,
287.572387695312, 329.677368164062, 247.823104858398, 258.418426513672,
404.189147949219, 404.189147949219, 267.218231201172, 404.189147949219,
352.567840576172, 216.117523193359, 323.650573730469, 287.572387695312,
329.677368164062, 329.677368164062, 287.572387695312, 217.956893920898,
329.677368164062, 287.572387695312, 287.572387695312, 329.677368164062,
242.079177856445, 334.339538574219, 291.639129638672, 262.072875976562,
245.502563476562, 221.039138793945, 291.639129638672, 245.502563476562,
245.502563476562, 334.339538574219, 205.848175048828, 234.055160522461,
228.630340576172, 311.361968994141, 205.848175048828, 234.055160522461,
271.596160888672, 221.039138793945, 245.502563476562, 251.327713012695,
251.327713012695, 258.418426513672, 217.956893920898, 258.418426513672,
242.079177856445, 329.677368164062, 355.819549560547), label = "Factor de Expansión de Empleo/Ingresos proyecciones CPV-2007", format.spss = "F8.2"),
ocupinf = structure(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2,
1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 1, 1,
1, 1, 1, 2, 1, 1, 1, 2, 2, 1, 2, 1, 1, 1, 1, 2, 2, 1, 1,
1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1,
1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2), label = "Situación de informalidad (ocup.principal)", format.spss = "F8.2", labels = c(`empleo informal` = 1,
`empleo formal` = 2), class = c("haven_labelled", "vctrs_vctr",
"double")), pobreza = structure(c(1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2), label = "pobreza", format.spss = "F8.2", labels = c(`Pobre Extremo` = 1,
`Pobre No Extremo` = 2, `No Pobre` = 3), class = c("haven_labelled",
"vctrs_vctr", "double"))), row.names = c(NA, -100L), class = c("tbl_df",
"tbl", "data.frame"))
如果您发现任何见解,请告诉我。
正如@DanielD.Sjoberg 在评论中指出的那样,有必要明确指出行中的每个变量都是连续变量。所以在代码中加入type = everything() ~ "continuous"
:
data %>%
as_label() %>%
as_survey_design(weight = fac500a) %>%
tbl_strata2(
strata = pobreza,
~ .x %>%
tbl_svysummary(
by = ocupinf,
include = ing_cap,
missing = "no",
type = everything() ~ "continuous",
statistic = list(all_continuous() ~ "{mean}"),
label = list(ing_cap = .y)
) %>%
modify_header(all_stat_cols() ~ "**{level}**") %>%
add_overall(., col_label = "Total"),
.combine_with = "tbl_stack",
.combine_args = list(group_header = NULL)
) %>%
modify_table_body(
~ .x %>%
mutate(variable = "pobreza", row_type = "level") %>%
tibble::add_row(
row_type = "label",
variable = "pobreza",
label = "pobreza",
.before = 1L
)
) %>%
modify_column_indent(columns = label, rows = row_type == "level") %>%
bold_labels() %>%
modify_footnote(all_stat_cols() ~ "ing_cap: Mean")