rr-markdownqwraps2

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. The data.frame contains information of different exposures. 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, e. g. with 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. 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?

Thanks for your help!

Best,
Florian


Solution

  • [SOLVED] Meanwhile, after a lot of research on this topic, I found a convenient and easy way to add a p.values column. 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:

    # 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.

    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. 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).

    If there are other methods to add individual columns to qwraps2::summary_table I would appreciate to see how it is possible.

    Best,
    Florian