By updating gtsummary, I ran into an issue. It used to be possible to ask for a wilcoxon-test for an ordered-factor variable. That should be possible, as the documentation says:
wilcox.test(
as.numeric(variable) ~ as.factor(by),
data = data,
conf.int = TRUE,
conf.level = conf.level,
...
)
After the update (2.0.4) that no longer seems to work.
testdf <- data.frame(
out = sample(LETTERS[1:4], 100, replace = TRUE),
group = sample(LETTERS[1:2],100, replace = TRUE)
) %>%
mutate(
orderedfac = factor(out, levels = LETTERS[1:4], ordered = TRUE),
outnum = as.numeric(orderedfac)
)
tbl_summary(
testdf %>% select(group, orderedfac), by = group) %>%
add_p(test = list(orderedfac = "wilcox.test")
)
results in error : 'x' must be numeric
Anyone knows a workaround?
In the 2.0 release of gtsummary, we took a step back from doing pre-processing on user data before calculating any test results, so you will need to convert your column to a numeric.
If that is, for some reason, not an option for you, you can write a custom Wilcox test. Example below!
library(gtsummary)
# create my own wilcox test function
my_wilcox <- function(data, by, variable, ...) {
data[[variable]] <- as.numeric(data[[variable]])
cardx::ard_stats_wilcox_test(data = data, by = all_of(by), variables = all_of(variable))
}
trial |>
dplyr::mutate(trt = factor(trt, ordered = TRUE)) |>
tbl_summary(
by = trt,
include = age
) |>
add_p(test = all_continuous() ~ my_wilcox) |>
as_kable() # convert to kable to display on SO
Characteristic | Drug A N = 98 | Drug B N = 102 | p-value |
---|---|---|---|
Age | 46 (37, 60) | 48 (39, 56) | 0.7 |
Unknown | 7 | 4 |
Created on 2025-02-03 with reprex v2.1.1