R/chisq_TX.R

Defines functions chisq_TX

Documented in chisq_TX

chisq_TX <-
function(data, Ns, yates) {
  stopifnot(is.logical(yates))
  
  if (!is.null(data)) {
    x <- data[1,1]
    y <- data[1,2]
  } else {
    x <- rep(0:Ns[1], each=(Ns[2]+1))
    y <- rep.int(0:Ns[2], Ns[1]+1)
  }
  N <- sum(Ns)
  # Note: when Yates is false, this statistic is equivalent to the squared z-pooled statistic
  if (!yates) {
    numerator <- N*(x*(Ns[2]-y) - y*(Ns[1]-x))^2
  } else {
    sqTerm <- abs(x*(Ns[2]-y) - y*(Ns[1]-x)) - 1/2*N
    sqTerm[sqTerm < 0] <- 0
    numerator <- N*(sqTerm)^2
  }
  denominator <- Ns[1]*Ns[2]*(x+y)*(N-x-y)
  
  TX <- numerator / denominator
  TX[numerator == 0 & denominator == 0] <- 0
  return(cbind(x, y, TX, deparse.level=0))
}

Try the Exact package in your browser

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

Exact documentation built on Sept. 26, 2022, 1:05 a.m.