最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

r - gtsummary - Wilcoxon on ordered factor - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question edited Feb 3 at 13:13 Limey 12.6k2 gold badges15 silver badges41 bronze badges asked Feb 3 at 13:07 DriesDries 5244 silver badges25 bronze badges 2
  • The actual documentation is help("wilcox.test"). I think you are misunderstanding something but since I'm not familiar with (or interested in) gtsummary, I can't tell you specifics. All I know is that wilcox.test needs a numeric variable (on the LHS of the formula in wilcox.test.formula or as argument to wilcox.test.default) and you don't select a numeric variable. – Roland Commented Feb 3 at 14:22
  • But the gtsummary-info states that it uses 'as.numeric(variable)' as outcome, which should work with an ordered factor – Dries Commented Feb 3 at 14:47
Add a comment  | 

1 Answer 1

Reset to default 3

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

发布评论

评论列表(0)

  1. 暂无评论