R/chisq.test2.R

Defines functions chisq.test2

Documented in chisq.test2

#' A chisq.test2 Function
#'
#' chisq.test2 para no aplicar corrección de Yates en el paquete compareGroups.
#' @export chisq.test2
#' @keywords chisquare test comparegroups yates correction

chisq.test2 <- function(obj, chisq.test.perm, correct = FALSE ){
    if (any(dim(obj) < 2) || is.null(dim(obj)) || sum(rowSums(obj) > 0) < 2 ||
        sum(colSums(obj) > 0) < 2)
      return(NaN)
    obj <- obj[,colSums(obj) > 0] # erase columns full of zeros.
    expect <- outer(rowSums(obj),colSums(obj))/sum(obj)
    if (any(expect < 5)) {
      if (chisq.test.perm)
        test <- try(chisq.test(obj, simulate.p.value = TRUE, correct = correct), silent = TRUE)
      else
        test <- try(fisher.test(obj),silent = TRUE)
    } else {
      test <- try(chisq.test(obj, correct = correct),silent = TRUE)
    }
    if (inherits(test,"try-error")) {
      test <- try(chisq.test(obj, simulate.p.value = TRUE, correct = correct),silent = TRUE)
    }
    if (inherits(test,"try-error"))
      return(NaN)
    ans <- test$p.value
    ans
  }
miriamMota/mmotaF documentation built on Jan. 12, 2023, 4 a.m.