R/hwe_chisq.R

Defines functions hwe_chisq

Documented in hwe_chisq

#' @title Hardy-Weinberg Equilibrium Test
#'
#' @description Tests for HWE for a table of counts of (aa, Aa, AA)
#'
#' @param counts Table of counts
#'
#' @export

hwe_chisq <- function(counts) {
  freq <- c(0,1,2)
  n <- sum(counts)
  p <- sum(counts * freq) / (n*2)

  q <- 1 - p
  pvec <- c(q^2, 2*p*q, p^2)
  E <- n * pvec

  chisq <- sum((counts-E)^2 / E)
  p <- pchisq(chisq, 1, lower.tail=F)
  return(p)
}
mattwarkentin/genetools documentation built on Nov. 4, 2019, 6:19 p.m.