feature request for `PMCMRplus`
IndrajeetPatil opened this issue · comments
Indrajeet Patil commented
# setup
set.seed(123)
library(tidyverse)
library(PMCMRplus)
# custom function
PMCMR_to_tidy <- function(mod, ...) {
# custom function to convert from a matrix to a tidy dataframe
matrix_to_tidy <- function(m, col_name = "value") {
result <-
data.frame(
group1 = rep(rownames(m), each = ncol(m)),
group2 = rep(colnames(m), times = nrow(m)),
col3 = as.numeric(base::t(m)),
stringsAsFactors = FALSE
)
names(result)[3] <- col_name
stats::na.omit(result)
}
# combine all components of the object in a single dataframe
tibble::as_tibble(dplyr::bind_cols(
dplyr::full_join(
matrix_to_tidy(mod$statistic, col_name = "statistic"),
matrix_to_tidy(mod$p.value, col_name = "p.value"),
by = c("group1", "group2")
),
purrr::flatten_dfr(list(mod$parameter)),
tibble::tibble(method = mod$method),
tibble::tibble(distribution = mod$dist),
tibble::tibble(p.adjust.method = mod$p.adjust.method)
))
}
# example PMCMRplus object
ans <- kwAllPairsConoverTest(count ~ spray, data = InsectSprays, p.adjust.method = "single-step")
#> Warning in kwAllPairsConoverTest.default(c(10, 7, 20, 14, 14, 12, 10, 23, : Ties
#> are present. Quantiles were corrected for ties.
# using the function
PMCMR_to_tidy(ans)
#> # A tibble: 15 x 9
#> group1 group2 statistic p.value nmeans df method distribution
#> <chr> <chr> <dbl> <dbl> <int> <int> <chr> <chr>
#> 1 B A 0.890 9.88e- 1 6 66 Conov~ q
#> 2 C A -13.6 0. 6 66 Conov~ q
#> 3 C B -14.5 0. 6 66 Conov~ q
#> 4 D A -8.87 4.61e- 7 6 66 Conov~ q
#> 5 D B -9.76 3.61e- 8 6 66 Conov~ q
#> 6 D C 4.71 1.70e- 2 6 66 Conov~ q
#> 7 E A -11.0 1.12e- 9 6 66 Conov~ q
#> 8 E B -11.8 7.57e-11 6 66 Conov~ q
#> 9 E C 2.63 4.37e- 1 6 66 Conov~ q
#> 10 E D -2.09 6.81e- 1 6 66 Conov~ q
#> 11 F A 1.15 9.64e- 1 6 66 Conov~ q
#> 12 F B 0.264 1.00e+ 0 6 66 Conov~ q
#> 13 F C 14.7 0. 6 66 Conov~ q
#> 14 F D 10.0 1.68e- 8 6 66 Conov~ q
#> 15 F E 12.1 3.05e-11 6 66 Conov~ q
#> # ... with 1 more variable: p.adjust.method <chr>
Created on 2020-10-14 by the reprex package (v0.3.0.9001)
Indrajeet Patil commented
Indrajeet Patil commented
Keeping this open until this is actually adapted in the current package:
# function body
PMCMR_to_tibble <- function(mod, ...) {
dplyr::select(PMCMRplus::toTidy(mod), -dplyr::contains("method")) %>%
dplyr::rename(.data = ., group2 = group1, group1 = group2)
}