rstatgtsummarygtrstatix

Friedman test using gtsummary


I'm trying to perform frideman test inside and add_p() function added to tbl_summary() The problems is that frideman test assumes complete blocks

So I added it as a function to exclued uncomplete blocks

library(tidyverse)
library(gtsummary)
library(rstatix)

set.seed(123)
df <- data.frame(
  patient = c(1,1,1,2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,8,8,8,9,9,9,10,10,10),
  round = c(1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3),
  q1 = rnorm(30, mean = 10, sd = 2),
  q3 = c(rnorm(25, mean = 10, sd = 2), NA, NA, NA, NA, NA)
)

df1_clean %>%
  select(patient, round, q1, q3) %>%
  tbl_summary(
    by = round,
    type = list(
      q1 ~ "continuous2",
      q3 ~ "continuous2"
    ),
    include = - patient
  ) %>%
  add_p(
    all_continuous2() ~ function(x) {
        df1_clean %>%
            group_by(patient) %>%  
            filter(!any(is.na(x))) %>%  
            mutate(patient = factor(patient)) %>% 
            ungroup() %>% 
            friedman_test(
                formula = as.formula(as.formula(paste0(x, " ~ round | patient")))
            ) %>% tidy() 
    }
  )

` So I got this error:

The following errors were returned during add_p(): ✖ For variable q1 (round) and "p.value" statistic: unused arguments (data = list(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10), c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), c(9.33132389566044, 9.78888018241105, 8.53898065438434, 13.8100871698174, 10.6652434629408, 10.4612672809989, 6.61627517023187, 11.3195837990987, 7.95275282224058, 8.21695685129141, 11.8366823420433, 9.09459870698354, 6.50325543999364, 13.5398082197787, 5.24518614921496, 11.1456230597192, 12.0344984981692, 8.73806426667893, 10.8885741028231, 10.8782607767771, 12.081246305829, 10.968198775905, 9.51023244181495, 11.8319841158804, 11.6012447130195, 8.12686193172842, 7.19842513200854, 10.3205550799864, 9.45207525044964, 8.02892177487541), c(10.1678613590301, 7.36000694702619, 10.3224527026524, 8.75014322491616, 11.914328548385, 14.8489782823231, 8.16804151262642, 12.115328341886, 11.6502994555366, 9.85961155138928, 9.09270725188597, 13.150615413675, 5.98908436352751, 8.71361041681267, 7.12631311268445, 12.7906268778922, 9.61859313471029, 8.95065760106066, 16.3680889481327, 9.89992546422474, 9.11250137626695, 10.5997305002723, 6.86315075849006, 10.9806052853441, 9.80767359784007, NA, NA, NA, NA, NA)), variable = "q1", by = "round", group = character(0), type = "continuous2", test.args = NULL, adj.vars = character(0), conf.level = 0.95, continuous_variable = NULL) ✖ For variable q3 (round) and "p.value" statistic: unused arguments (data = list(c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10), c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3), c(9.33132389566044, 9.78888018241105, 8.53898065438434, 13.8100871698174, 10.6652434629408, 10.4612672809989, 6.61627517023187, 11.3195837990987, 7.95275282224058, 8.21695685129141, 11.8366823420433, 9.09459870698354, 6.50325543999364, 13.5398082197787, 5.24518614921496, 11.1456230597192, 12.0344984981692, 8.73806426667893, 10.8885741028231, 10.8782607767771, 12.081246305829, 10.968198775905, 9.51023244181495, 11.8319841158804, 11.6012447130195, 8.12686193172842, 7.19842513200854, 10.3205550799864, 9.45207525044964, 8.02892177487541), c(10.1678613590301, 7.36000694702619, 10.3224527026524, 8.75014322491616, 11.914328548385, 14.8489782823231, 8.16804151262642, 12.115328341886, 11.6502994555366, 9.85961155138928, 9.09270725188597, 13.150615413675, 5.98908436352751, 8.71361041681267, 7.12631311268445, 12.7906268778922, 9.61859313471029, 8.95065760106066, 16.3680889481327, 9.89992546422474, 9.11250137626695, 10.5997305002723, 6.86315075849006, 10.9806052853441, 9.80767359784007, NA, NA, NA, NA, NA)), variable = "q3", by = "round", group = character(0), type = "continuous2", test.args = NULL, adj.vars = character(0), conf.level = 0.95, continuous_variable = NULL)


Solution

  • There are several issues with your code. First, by default gtsummary will pass a bunch of arguments to a custom test function (see here). For that reason you have to include ... as a function argument to prevent the unused arguments error. Second, even after the fixing that you will get an error argument "x" is missing as the name of the variable is passed via the argument variable=. But then a third error will pop up and finally a fourth.

    Instead here is a working solution where I moved the custom test function outside of the gtsummary pipeline for easier debugging and which uses the friedman.test function from stats instead of rstatix::friedman_test (You can use the latter but then you don't need to call tidy and rename the column names of the returned df so that they correspond to the ones expected by add_p):

    library(tidyverse)
    library(gtsummary)
    
    fried_test <- function(data, variable, by, ...) {
      data %>%
        group_by(patient) %>%
        filter(!any(is.na(.data[[variable]]))) %>%
        mutate(patient = factor(patient)) %>%
        ungroup() %>%
        friedman.test(
          formula = reformulate(
            response = variable,
            termlabels = paste0(by, "| patient")
          ),
          data = .
        ) |>
        tidy()
    }
    
    df1_clean %>%
      select(patient, round, q1, q3) %>%
      tbl_summary(
        by = round,
        type = list(
          q1 ~ "continuous2",
          q3 ~ "continuous2"
        ),
        include = -patient
      ) %>%
      add_p(
        all_continuous2() ~ "fried_test"
      )
    

    enter image description here