R/dist.chi2.R

Defines functions dist.chi2

Documented in dist.chi2

dist.chi2 <- function(X) {
  if(any(sapply(X, FUN = function(x) !is.factor(x)))) stop("variables in X should all be factors")
  X <- dichotom(X, out = "numeric")
  xip = rowSums(X)
  R <- X / xip
  xpp = sum(xip)             # grand total
  m <- xip / xpp             # masses 
  c <- colSums(X) / xpp      # row barycenter
  w = 1/c                    # columns weights
  Rc = t(t(R) - c)           # deviations to barycenter
  Rtilde = t(t(Rc)*sqrt(w))  # weighted R
  S = Rtilde%*%t(Rtilde)     # covariance
  s = diag(S)                # diag of S
  D = (s - S) + t(s-S)       # Chi2 distance matrix
  return(D)
}

Try the GDAtools package in your browser

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

GDAtools documentation built on Oct. 6, 2023, 5:07 p.m.