R/kruskaltest.R

Defines functions kruskaltests

Documented in kruskaltests

#[export]
kruskaltest <- function (x, ina, logged = FALSE) {
    n <- length(x)
    R <- Rfast::Rank(x)
    ni <- tabulate(ina)
	ni <- ni[ni > 0]
    up <- sum( Rfast::group(R, ina)^2/ni ) - n * (n + 1)^2/4
    down <- sum(R^2) - n * (n + 1)^2/4
    stat <- (n - 1) * up/down
    pvalue <- pchisq(stat, length(ni) - 1, lower.tail = FALSE, 
        log.p = logged)
    res <- c(stat, pvalue)
    names(res) <- c("stat", "p-value")
    res
}



#[export]
kruskaltests <- function(x, ina, logged = FALSE) { 
  n <- length(ina)
  R <- Rfast::colRanks(x)
  ni <- tabulate(ina)
  ni <- ni[ni > 0]
  up <- Rfast::colsums( rowsum(R, ina)^2 / ni ) - n * (n + 1)^2 / 4
  down <- Rfast::colsums(R^2) - n * (n + 1)^2 / 4
  stat <- (n - 1) * up / down
  pvalue <- pchisq(stat, length(ni) - 1, lower.tail = FALSE, log.p = logged) 
  cbind(stat, pvalue)
}

Try the Rfast package in your browser

Any scripts or data that you put into this service are public.

Rfast documentation built on Nov. 9, 2023, 5:06 p.m.