Search code examples
rgtsummary

Tbl_Summary Chi Squared Testing


I have created a summary table for some data using tbl_summary(). The table sums numerical values in columns grouped by a factor, producing a 2x2 table.

I'd like to use tlb_summary()'s built-in statistics to calculate the p value using a Chi Squared test, however I can't tell if this is possible. Using the add_p() line gives me a p value for each row, which is incorrect:

library(gtsummary)
library(tidyverse)

test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
                   "correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
                   "incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))

test %>%
  tbl_summary(
    by = With_assistant,
    type = list(c(correct_answers, incorrect_answers) ~ "continuous"),
    statistic = list(c(correct_answers, incorrect_answers) ~ "{sum}") 
  ) %>%
add_p(test = everything () ~ "chisq.test")


Solution

  • Would you be okay with adding the result of a chisq.test() to the tbl object as a note?

    library(gtsummary)
    library(tidyverse)
    
    test <- data.frame("With_assistant" = c(TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE),
                       "correct_answers" = c(2,4,5,6,1,2,7,2,1,2,3),
                       "incorrect_answers" = c(1,2,1,5,3,1,2,5,3,2,4))
    
    t <- test %>% 
      group_by(With_assistant) %>% 
      summarize(sum_cor = sum(correct_answers), 
                sum_inc =sum(incorrect_answers)) 
    
    # A tibble: 2 × 3
      With_assistant sum_cor sum_inc
      <lgl>            <dbl>   <dbl>
    1 FALSE               15      15
    2 TRUE                20      14
    
    chi<- chisq.test(t)
    
    test %>%
      tbl_summary(
        by = With_assistant,
        type = list(c(correct_answers, incorrect_answers) ~ "continuous"),
        statistic = list(c(correct_answers, incorrect_answers) ~ "{sum}") 
      ) %>%
      modify_footnote(all_stat_cols() ~ paste0("Chi Square ", round(chi$statistic,2), " with p = ", round(chi$p.value, 2)))
    

    enter image description here

    1.37 is the real test statistic of your four numbers from the contingency table.