首页 > 解决方案 > 如何在 R 中使用 qwraps2 在新行中添加组比较结果

问题描述

我正在关注 Peter DeWitt关于 qwraps2 和 summary_table的精彩教程,但无法进一步取得进展。

到目前为止,这是我的数据和代码:

data(mtcars)

  mtcars2 <- dplyr::mutate(mtcars,
                cyl_factor = factor(cyl,
                                    levels = c(6, 4, 8),
                                    labels = paste(c(6, 4, 8), "cylinders")),
                cyl_character = paste(cyl, "cylinders"),
                gear_factor = factor(gear,
                                     levels = c(3, 4, 5),
                                     labels = paste(c(3, 4, 5), "gears")))

new_summary <- mtcars2 %>%
  dplyr::select(.data$mpg, .data$wt, .data$gear_factor) %>%
  qsummary(.)

by_cyl <- mtcars2 %>%
  dplyr::group_by(.data$cyl_factor) %>%
  summary_table(., new_summary)

在本教程中,他计算了组比较的 p 值并将 p 值添加到表中的新列中。我想通过添加更多比较结果(科恩的 d 和 95% CI,以及 p 值)来扩展这一点。然后我想将这些结果添加到每个变量下的新行中,而不是作为每个变量旁边的新列)。因此,我希望输出看起来像这样(我已经为组比较测试编造了数字):

                 6 cylinders (N = 7)    4 cylinders (N = 11)    8 cylinders (N = 14)
mpg           
   minimum      17.80                   21.40                   10.40
   median (IQR) 19.70 (18.65, 21.00)    26.00 (22.80, 30.40)    15.20 (14.40, 16.25)
   mean (sd)    19.74 ± 1.45            26.66 ± 4.51            15.10 ± 2.56
   maximum      21.40                   33.90                   19.20
   comparison   d = 0.87, 95% CI [0.80, 0.94], p    = 0.001
wt            
   minimum      2.62                    1.51                    3.17
   median (IQR) 3.21 (2.82, 3.44)       2.20 (1.88, 2.62)       3.75 (3.53, 4.01)
   mean (sd)    3.12 ± 0.36             2.29 ± 0.57             4.00 ± 0.76
   maximum      3.46                    3.19                    5.42
   comparison   d = 0.87, 95% CI [0.80, 0.94], p    = 0.001

所以我有两个问题:

  1. 如何在表格中添加一行并填充一些内容

  2. 如何进行组比较测试,以正确的格式输入,然后输入表格?

我的主要问题是问题1,我现在很困惑。如果我能得到帮助来解决它,我也许可以通过摆弄 DeWitt 的 mpvals 示例来自己解决问题 2。虽然我也很乐意在问题 2 上获得帮助。

到目前为止,我已尝试在 qsummary() 中添加一个空白行,但无法使其正常工作。我尝试操作由 summary_table 创建的字符矩阵,但不知道如何操作它。任何帮助表示赞赏!

标签: rqwraps2

解决方案


向每个行组添加比较行不是 qwraps2::summary_table直接支持的。这是因为该问题与 Markdown 的限制以及支持所有不同方法在 LaTeX 中实现跨表的多列的复杂性有关。

使用qwraps2::summary_table生成主表是一个很好的起点。构建输出表本身将需要一些其他包。

随着 qwraps2 0.5.0 版本的发布,mtcars2 数据是一个导出的数据集,不需要显式构建。

library(qwraps2)
options(qwraps2_markup = "markdown")

summaries <- qsummary(mtcars2[, c("mpg", "wt", "gear_factor")])

by_cyl <-
  summary_table(mtcars2, summaries = summaries, by = "cyl_factor")

请注意,输出summary_table是一个字符矩阵。

str(by_cyl)
#>  'qwraps2_summary_table' chr [1:11, 1:3] "17.80" "19.70 (18.65, 21.00)" ...
#>  - attr(*, "dimnames")=List of 2
#>   ..$ : chr [1:11] "minimum" "median (IQR)" "mean (sd)" "maximum" ...
#>   ..$ : chr [1:3] "6 cylinders (N = 7)" "4 cylinders (N = 11)" "8 cylinders (N = 14)"
#>  - attr(*, "rgroups")= Named int [1:3] 4 4 3
#>   ..- attr(*, "names")= chr [1:3] "mpg" "wt" "gear_factor"

我将报告而不是 Choen 的 D,F 统计量和 p 值形成方差分析。

mpg_comp <-
  paste(extract_fstat(lm(mpg ~ cyl_factor, data = mtcars2)),
        extract_fpvalue(lm(mpg ~ cyl_factor, data = mtcars2)),
        collapse = ", ")

wt_comp <-
  paste(extract_fstat(lm(wt ~ cyl_factor, data = mtcars2)),
        extract_fpvalue(lm(wt ~ cyl_factor, data = mtcars2)),
        collapse = ", ")

mpg_comp
#> [1] "$F_{2, 29} = 39.70$ *P* < 0.0001"
wt_comp
#> [1] "$F_{2, 29} = 22.91$ *P* < 0.0001"

对于构建表,有很多选择。在降价中跨越多列并非易事。不同风格的降价会以不同的方式呈现表格。有些会支持多列跨越,而其他风格则不会。

对于降价表,我建议使用报告比较的新列。对于by_cyl表格,我会将 F stat 和 p 值放在报告平均值的行上。这将统计测试和结果放在与汇总统计相关的行上。

by_cyl2 <- cbind(by_cyl, "comparison" = "&nbsp;")
by_cyl2[grepl("mean", rownames(by_cyl2)), "comparison"] <- c(mpg_comp, wt_comp)

by_cyl2
#> 
#> 
#> |                             |6 cylinders (N = 7)  |4 cylinders (N = 11) |8 cylinders (N = 14) |comparison                       |
#> |:----------------------------|:--------------------|:--------------------|:--------------------|:--------------------------------|
#> |**mpg**                      |&nbsp;&nbsp;         |&nbsp;&nbsp;         |&nbsp;&nbsp;         |&nbsp;&nbsp;                     |
#> |&nbsp;&nbsp; minimum         |17.80                |21.40                |10.40                |&nbsp;                           |
#> |&nbsp;&nbsp; median (IQR)    |19.70 (18.65, 21.00) |26.00 (22.80, 30.40) |15.20 (14.40, 16.25) |&nbsp;                           |
#> |&nbsp;&nbsp; mean (sd)       |19.74 &plusmn; 1.45  |26.66 &plusmn; 4.51  |15.10 &plusmn; 2.56  |$F_{2, 29} = 39.70$ *P* < 0.0001 |
#> |&nbsp;&nbsp; maximum         |21.40                |33.90                |19.20                |&nbsp;                           |
#> |**wt**                       |&nbsp;&nbsp;         |&nbsp;&nbsp;         |&nbsp;&nbsp;         |&nbsp;&nbsp;                     |
#> |&nbsp;&nbsp; minimum         |2.62                 |1.51                 |3.17                 |&nbsp;                           |
#> |&nbsp;&nbsp; median (IQR)    |3.21 (2.82, 3.44)    |2.20 (1.89, 2.62)    |3.75 (3.53, 4.01)    |&nbsp;                           |
#> |&nbsp;&nbsp; mean (sd)       |3.12 &plusmn; 0.36   |2.29 &plusmn; 0.57   |4.00 &plusmn; 0.76   |$F_{2, 29} = 22.91$ *P* < 0.0001 |
#> |&nbsp;&nbsp; maximum         |3.46                 |3.19                 |5.42                 |&nbsp;                           |
#> |**gear_factor**              |&nbsp;&nbsp;         |&nbsp;&nbsp;         |&nbsp;&nbsp;         |&nbsp;&nbsp;                     |
#> |&nbsp;&nbsp; 3 forward gears |2 (29)               |1 (9)                |12 (86)              |&nbsp;                           |
#> |&nbsp;&nbsp; 4 forward gears |4 (57)               |8 (73)               |0 (0)                |&nbsp;                           |
#> |&nbsp;&nbsp; 5 forward gears |1 (14)               |2 (18)               |2 (14)               |&nbsp;                           |

相反,如果比较是一个新行,我喜欢使用摘要添加一个空白行然后将比较添加到空白行的想法。

summaries[[1]] <- c(summaries[[1]], "comparison" = ~ qwraps2::frmt(""))
summaries[[2]] <- c(summaries[[2]], "comparison" = ~ qwraps2::frmt(""))

by_cyl3 <- summary_table(mtcars2, summaries, by = "cyl_factor")

by_cyl3[grepl("comparison", rownames(by_cyl3)), 1] <- c(mpg_comp, wt_comp)

by_cyl3
#> 
#> 
#> |                             |6 cylinders (N = 7)              |4 cylinders (N = 11) |8 cylinders (N = 14) |
#> |:----------------------------|:--------------------------------|:--------------------|:--------------------|
#> |**mpg**                      |&nbsp;&nbsp;                     |&nbsp;&nbsp;         |&nbsp;&nbsp;         |
#> |&nbsp;&nbsp; minimum         |17.80                            |21.40                |10.40                |
#> |&nbsp;&nbsp; median (IQR)    |19.70 (18.65, 21.00)             |26.00 (22.80, 30.40) |15.20 (14.40, 16.25) |
#> |&nbsp;&nbsp; mean (sd)       |19.74 &plusmn; 1.45              |26.66 &plusmn; 4.51  |15.10 &plusmn; 2.56  |
#> |&nbsp;&nbsp; maximum         |21.40                            |33.90                |19.20                |
#> |&nbsp;&nbsp; comparison      |$F_{2, 29} = 39.70$ *P* < 0.0001 |                     |                     |
#> |**wt**                       |&nbsp;&nbsp;                     |&nbsp;&nbsp;         |&nbsp;&nbsp;         |
#> |&nbsp;&nbsp; minimum         |2.62                             |1.51                 |3.17                 |
#> |&nbsp;&nbsp; median (IQR)    |3.21 (2.82, 3.44)                |2.20 (1.89, 2.62)    |3.75 (3.53, 4.01)    |
#> |&nbsp;&nbsp; mean (sd)       |3.12 &plusmn; 0.36               |2.29 &plusmn; 0.57   |4.00 &plusmn; 0.76   |
#> |&nbsp;&nbsp; maximum         |3.46                             |3.19                 |5.42                 |
#> |&nbsp;&nbsp; comparison      |$F_{2, 29} = 22.91$ *P* < 0.0001 |                     |                     |
#> |**gear_factor**              |&nbsp;&nbsp;                     |&nbsp;&nbsp;         |&nbsp;&nbsp;         |
#> |&nbsp;&nbsp; 3 forward gears |2 (29)                           |1 (9)                |12 (86)              |
#> |&nbsp;&nbsp; 4 forward gears |4 (57)                           |8 (73)               |0 (0)                |
#> |&nbsp;&nbsp; 5 forward gears |1 (14)                           |2 (18)               |2 (14)               |

这不涉及跨越多个列。至少据我所知,这个问题并不重要。我会根据目标文件格式使用不同的工具和方法。如果我要构建一个 .pdf,我将使用 LaTeX,而不是 markdown,并\multicolumn{}{}{}明确使用。如果目标输出是 html,我会明确地构建一个 html 表。 htmlTable是一个很好的包。在构建 .docx 或其他 Office 样式的输出时 ,有一些兼容性选项可能会有所帮助。reprex 包于 2020-09-15 创建(v0.3.0)

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       macOS Catalina 10.15.6      
#>  system   x86_64, darwin17.0          
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       America/Denver              
#>  date     2020-09-15                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date       lib source        
#>  assertthat    0.2.1   2019-03-21 [1] CRAN (R 4.0.0)
#>  backports     1.1.9   2020-08-24 [1] CRAN (R 4.0.2)
#>  callr         3.4.4   2020-09-07 [1] CRAN (R 4.0.2)
#>  cli           2.0.2   2020-02-28 [1] CRAN (R 4.0.0)
#>  crayon        1.3.4   2017-09-16 [1] CRAN (R 4.0.0)
#>  desc          1.2.0   2018-05-01 [1] CRAN (R 4.0.0)
#>  devtools      2.3.1   2020-07-21 [1] CRAN (R 4.0.2)
#>  digest        0.6.25  2020-02-23 [1] CRAN (R 4.0.0)
#>  ellipsis      0.3.1   2020-05-15 [1] CRAN (R 4.0.0)
#>  evaluate      0.14    2019-05-28 [1] CRAN (R 4.0.0)
#>  fansi         0.4.1   2020-01-08 [1] CRAN (R 4.0.0)
#>  fs            1.5.0   2020-07-31 [1] CRAN (R 4.0.2)
#>  glue          1.4.2   2020-08-27 [1] CRAN (R 4.0.2)
#>  highr         0.8     2019-03-20 [1] CRAN (R 4.0.0)
#>  htmltools     0.5.0   2020-06-16 [1] CRAN (R 4.0.0)
#>  knitr         1.29    2020-06-23 [1] CRAN (R 4.0.0)
#>  magrittr      1.5     2014-11-22 [1] CRAN (R 4.0.0)
#>  memoise       1.1.0   2017-04-21 [1] CRAN (R 4.0.0)
#>  pkgbuild      1.1.0   2020-07-13 [1] CRAN (R 4.0.2)
#>  pkgload       1.1.0   2020-05-29 [1] CRAN (R 4.0.0)
#>  prettyunits   1.1.1   2020-01-24 [1] CRAN (R 4.0.0)
#>  processx      3.4.4   2020-09-03 [1] CRAN (R 4.0.2)
#>  ps            1.3.4   2020-08-11 [1] CRAN (R 4.0.2)
#>  qwraps2     * 0.5.0   2020-09-14 [1] local         
#>  R6            2.4.1   2019-11-12 [1] CRAN (R 4.0.0)
#>  Rcpp          1.0.5   2020-07-06 [1] CRAN (R 4.0.0)
#>  remotes       2.2.0   2020-07-21 [1] CRAN (R 4.0.2)
#>  rlang         0.4.7   2020-07-09 [1] CRAN (R 4.0.2)
#>  rmarkdown     2.3     2020-06-18 [1] CRAN (R 4.0.0)
#>  rprojroot     1.3-2   2018-01-03 [1] CRAN (R 4.0.0)
#>  sessioninfo   1.1.1   2018-11-05 [1] CRAN (R 4.0.0)
#>  stringi       1.5.3   2020-09-09 [1] CRAN (R 4.0.2)
#>  stringr       1.4.0   2019-02-10 [1] CRAN (R 4.0.0)
#>  testthat      2.3.2   2020-03-02 [1] CRAN (R 4.0.0)
#>  usethis       1.6.1   2020-04-29 [1] CRAN (R 4.0.0)
#>  withr         2.2.0   2020-04-20 [1] CRAN (R 4.0.0)
#>  xfun          0.17    2020-09-09 [1] CRAN (R 4.0.2)
#>  yaml          2.2.1   2020-02-01 [1] CRAN (R 4.0.0)
#> 
#> [1] /Library/Frameworks/R.framework/Versions/4.0/Resources/library

推荐阅读