[英]Add p-value column in qwraps::summary_table
I want to make a little summary table for my colleagues in R-Markdown using qwraps::summary_table
.我想使用qwraps::summary_table
为 R-Markdown 的同事制作一个小汇总表。 The data.frame contains information of different exposures. data.frame 包含不同曝光的信息。 All the variables are coded as binary.所有变量都编码为二进制。
library(qwraps2)
library(dplyr)
pop <- rbinom(n = 1000, size = 1, prob = runif(n = 10, min = 0, max = 1))
exp <- rbinom(n = 1000, size = 1, prob = .5)
ID <- c(1:500)
therapy <- factor(sample(x = pop, size = 500, replace = TRUE), labels = c("Control", "Intervention"))
exp_1 <- sample(x = exp, size = 500, replace = TRUE)
exp_2 <- sample(x = exp, size = 500, replace = TRUE)
exp_3 <- sample(x = exp, size = 500, replace = TRUE)
exp_4 <- sample(x = exp, size = 500, replace = TRUE)
df <- data.frame(ID, exp_1, exp_2, exp_3, exp_4, therapy)
head(df)
In the next step, I create a simple summary table as follows.在下一步中,我将创建一个简单的汇总表,如下所示。 In the table I want to have the groups (control vs. intervention) as columns and the exposures as rows:在表中,我希望将组(控制与干预)作为列,将暴露作为行:
my_summary <-
list(list("Exposure 1" = ~ n_perc(exp_1 %in% 1),
"Exposure 2" = ~ n_perc(exp_2 %in% 1),
"Exposure 3" = ~ n_perc(exp_3 %in% 1),
"Exposure 4" = ~ n_perc(exp_4 %in% 1))
)
my_table <- summary_table(group_by(df, therapy), my_summary)
my_table
In the next step I wanted to add a further column containing p-values for the group differences between control and intervention group, eg with fisher.test
.在下一步中,我想添加另一列,其中包含控制组和干预组之间组差异的 p 值,例如fisher.test
。 I read in ?qwraps::summary_table
that cbind
is a suitable method for class qwraps2_summary_table
, but to be honest, I'm struggling with it.我在?qwraps::summary_table
中cbind
是 class qwraps2_summary_table
的合适方法,但老实说,我正在努力解决它。 I tried different ways but failed, unfortunately.我尝试了不同的方法,但不幸的是失败了。
Is there a convenient way to add individual columns via qwraps::summary_table
especially p-values according to the grouped columns?有没有一种方便的方法可以通过qwraps::summary_table
添加单个列,尤其是根据分组列添加 p 值?
Thanks for your help!谢谢你的帮助!
Best,最好的,
Florian弗洛里安
[SOLVED] Meanwhile, after a lot of research on this topic, I found a convenient and easy way to add a p.values column. [已解决] 同时,在对该主题进行了大量研究之后,我发现了一种添加 p.values 列的便捷方法。 Maybe it is not the smartest solution, but worked, at least for me.也许这不是最聪明的解决方案,但至少对我来说是有效的。
First I calculated the p.values with a function, which extracts the p.values from the returned output of fisher.test
and stored them in an object, in my case a simple numeric vector:首先,我使用 function 计算 p.values,它从 Fisher.test 返回的 output 中提取fisher.test
,并将它们存储在 object 中,例如一个简单的向量:
# write function to extract fishers.test
fisher.pvalue <- function(x) {
value <- fisher.test(x)$p.value
return(value)
}
# fisher test/generate pvalues
p.vals <- round(sapply(list(
table(df$exp_1, df$therapy),
table(df$exp_2, df$therapy),
table(df$exp_3, df$therapy),
table(df$exp_4, df$therapy)), fisher.pvalue), digits = 2)
In the following step I simply added an empty table column called P-Values
and added the p.vals
to the column cells.在接下来的步骤中,我只是添加了一个名为P-Values
的空表列,并将p.vals
添加到列单元格中。
overall_table <- cbind(my_table, "P-Value" = "") # create empty column
overall_table[9:12] <- p.vals # add vals to empty column
# overall_table <- cbind(my_table, "P-Value" = p.vals) works the same way in one line of code
overall_table
In my case, I simply looked for the corresponding cell indices in overall_table
(for P-Values
= 9:12) and filled them using base syntax.就我而言,我只是在overall_table
中查找相应的单元格索引(对于P-Values
= 9:12)并使用基本语法填充它们。 In the vignette of qwraps2
( https://cran.r-project.org/web/packages/qwraps2/vignettes/summary-statistics.html ), the author used regular expressions to identify the right cells (see section 3.2).在qwraps2
的小插图( https://cran.r-project.org/web/packages/qwraps2/vignettes/summary-statistics.html )中,作者使用正则表达式来识别正确的单元格(参见第 3.2 节)。
If there are other methods to add individual columns to qwraps2::summary_table
I would appreciate to see how it is possible.如果有其他方法可以将单个列添加到qwraps2::summary_table
我会很高兴看到它是如何可能的。
Best,最好的,
Florian弗洛里安
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.