kassambara / rstatix

Pipe-friendly Framework for Basic Statistical Tests in R
https://rpkgs.datanovia.com/rstatix/
448 stars 51 forks source link

Problem with `mutate()` input `data`. #114

Open Ren01230 opened 3 years ago

Ren01230 commented 3 years ago

statistical test run

stat.test <- spleen %>% group_by(group,detail)%>%

  • wilcox_test(value ~ sex, paired = FALSE)

error

Error: Problem with mutate() input data. x missing value where TRUE/FALSE needed :information_source: Input data is map(.data$data, .f, ...).

data for reference

dput(spleen) structure(list(aid = c("A1", "A2", "A3", "A4", "B1", "B2", "B3", "B4", "C1", "C2", "C3", "C4", "D1", "D2", "D3", "D4", "E1", "E2", "E3", "E4", "F1", "F2", "F3", "F4"), tissue = c("sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp"), group = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C", "C", "C"), sex = c("m", "m", "m", "m", "m", "m", "m", "m", "m", "m", "m", "m", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f"), detail = c("mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4"), value = c(0, 0, 0, 0, 0, 0, 0, 0.46, 0, 0, 0, 0, 0, 0, 0, 0.31, 0, 0, 0, 0.47, 0, 0, 0, 0)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame" ), row.names = c(NA, -24L), spec = structure(list(cols = list( aid = structure(list(), class = c("collector_character", "collector")), tissue = structure(list(), class = c("collector_character", "collector")), group = structure(list(), class = c("collector_character", "collector")), sex = structure(list(), class = c("collector_character", "collector")), detail = structure(list(), class = c("collector_character", "collector")), value = structure(list(), class = c("collector_double", "collector"))), default = structure(list(), class = c("collector_guess", "collector")), skip = 1), class = "col_spec"))

BastienBennetot commented 3 years ago

I have the same problem, for me it was because on some grouping there is categories that contains only zeros. A rank test like wilcoxon test would be hardly done in this configuration. It would be nice to correct the function to avoid the grouping that is causing this issues whereas some groupings may be testable

benediktclaus commented 3 years ago

Hi @Ren01230

@BastienBennetot seems to be right with his assumption. If you look at the group-wise examples below, the dependent variable for the third group is 0 in every line and a statistical test for inference doesn't make any sense in this case.

I don't think this is an issue that should be fixed by rstatix. A package can not fix all decisions made by the researcher. Instead, one could benefit from a careful exploratory data analysis and use of statistical tests only when appropriate.

library(tidyverse)
library(rstatix)
#> 
#> Attache Paket: 'rstatix'
#> Das folgende Objekt ist maskiert 'package:stats':
#> 
#>     filter
reprex_data <- structure(list(aid = c("A1", "A2", "A3", "A4", "B1", "B2", "B3",
                       "B4", "C1", "C2", "C3", "C4", "D1", "D2", "D3", "D4", "E1", "E2",
                       "E3", "E4", "F1", "F2", "F3", "F4"), tissue = c("sp", "sp", "sp",
                                                                       "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp",
                                                                       "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp", "sp"),
               group = c("A", "A", "A", "A", "B", "B", "B", "B", "C", "C",
                         "C", "C", "A", "A", "A", "A", "B", "B", "B", "B", "C", "C",
                         "C", "C"), sex = c("m", "m", "m", "m", "m", "m", "m", "m",
                                            "m", "m", "m", "m", "f", "f", "f", "f", "f", "f", "f", "f",
                                            "f", "f", "f", "f"), detail = c("mCD4", "mCD4", "mCD4", "mCD4",
                                                                            "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4",
                                                                            "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4", "mCD4",
                                                                            "mCD4", "mCD4", "mCD4", "mCD4"), value = c(0, 0, 0, 0, 0,
                                                                                                                       0, 0, 0.46, 0, 0, 0, 0, 0, 0, 0, 0.31, 0, 0, 0, 0.47, 0,
                                                                                                                       0, 0, 0)), class = c("spec_tbl_df", "tbl_df", "tbl", "data.frame"
                                                                                                                       ), row.names = c(NA, -24L), spec = structure(list(cols = list(
                                                                                                                         aid = structure(list(), class = c("collector_character",
                                                                                                                                                           "collector")), tissue = structure(list(), class = c("collector_character",
                                                                                                                                                                                                               "collector")), group = structure(list(), class = c("collector_character",
                                                                                                                                                                                                                                                                  "collector")), sex = structure(list(), class = c("collector_character",
                                                                                                                                                                                                                                                                                                                   "collector")), detail = structure(list(), class = c("collector_character",
                                                                                                                                                                                                                                                                                                                                                                       "collector")), value = structure(list(), class = c("collector_double",
                                                                                                                                                                                                                                                                                                                                                                                                                          "collector"))), default = structure(list(), class = c("collector_guess",
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                "collector")), skip = 1), class = "col_spec"))
# Nest the data for quick examination
reprex_data_nested <- reprex_data %>% 
  group_by(group, detail) %>% 
  nest()

# This works just fine
reprex_data_nested %>% 
  pluck("data", 1) %>% 
  wilcox_test(value ~ sex)
#> # A tibble: 1 x 7
#>   .y.   group1 group2    n1    n2 statistic     p
#> * <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl>
#> 1 value f      m          4     4        10 0.453
# This one as well
reprex_data_nested %>% 
  pluck("data", 2) %>% 
  wilcox_test(value ~ sex)
#> # A tibble: 1 x 7
#>   .y.   group1 group2    n1    n2 statistic     p
#> * <chr> <chr>  <chr>  <int> <int>     <dbl> <dbl>
#> 1 value f      m          4     4       8.5     1
# Here, the error occurs
reprex_data_nested %>% 
  pluck("data", 3) %>% 
  wilcox_test(value ~ sex)
#> Error in if (f.lower <= 0) return(mumin): Fehlender Wert, wo TRUE/FALSE nötig ist
# As can be seen, all values are 0
reprex_data_nested %>% 
  pluck("data", 3)
#> # A tibble: 8 x 4
#>   aid   tissue sex   value
#>   <chr> <chr>  <chr> <dbl>
#> 1 C1    sp     m         0
#> 2 C2    sp     m         0
#> 3 C3    sp     m         0
#> 4 C4    sp     m         0
#> 5 F1    sp     f         0
#> 6 F2    sp     f         0
#> 7 F3    sp     f         0
#> 8 F4    sp     f         0

Created on 2021-10-06 by the reprex package (v2.0.1)