#' Chi-sqaure post-hoc testing
#'
#' Perform Bonferroni-corrected chi-sqaure post-hoc testing on a table or matrix.
#' @param mat A table or matrix
#' @param alpha Alpha significance value. Defaults to 0.05
#' @usage chisq.mc(matrix, alpha = 0.05)
#' @export
#' @examples chisq.mc(table(mtcars$gear, mtcars$cyl), alpha = 0.05)
chisq.mc <- function(mat, alpha = 0.05) {
if(is.null(rownames(mat))) stop("Matrix must have row names")
comparisons <- combn(rownames(mat), 2, simplify = F)
out <- do.call("rbind", lapply(comparisons, function(ii) {
nam <- paste(ii, collapse = "-")
chi <- tryCatch(chisq.test(mat[ii, ]),
error = function(e) e,
warning = function(w) w)
if(is(chi,"warning")) {
chi <- fisher.test(mat[ii, ], simulate.p.value = TRUE)
}
X2 <- ifelse(is.null(chi$statistic), NA, signif(chi$statistic, 4))
p <- signif(chi$p.value * length(comparisons), 4)
p <- ifelse(p > 1, 1, p)
sig <- ifelse(p > alpha, "", "*")
out <- cbind(nam, X2, p, sig)
rownames(out) <- ""
return(out)
}))
colnames(out) <- c("Comparison", "X2", "p.value", "Signif")
out <- data.frame(out)
return(out)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.