Revision 775c7e1be4c58aaf8adccdd2b92d07aa9cdc265f authored by Matthias Templ on 14 January 2020, 05:10:03 UTC, committed by cran-robot on 14 January 2020, 05:10:03 UTC
1 parent d761b2f
Raw File
ind2x2.R
#' Independence 2x2 compositional table
#' 
#' Estimates the expected frequencies from an 2x2 table under the 
#' null hypotheses of independence.
#' 
#' @param x a 2x2 table
#' @param margin if multidimensional table (larger than 2-dimensional), 
#' then the margin determines on which dimension the independene tables should be estimated.
#' @param pTabMethod \sQuote{classical} that is function \code{prop.table()} from package base or method \dQuote{half} that add 1/2 to each cell
#' to avoid zero problems.
#' @author Kamila Facevicova, Matthias Templ
#' @return The independence table(s) with either relative or absolute frequencies.
#' @references 
#' Facevicova, K., Hron, K., Todorov, V., Guo, D., Templ, M. (2014).
#' Logratio approach to statistical analysis of 2x2 compositional tables.
#' \emph{Journal of Applied Statistics}, 41 (5), 944--958.
#' @export
#' @examples 
#' data(employment) 
#' ind2x2(employment)
ind2x2 <- function(x, margin=3, pTabMethod = c("dirichlet", "half", "classical")){
  ## Matthias Templ, TU WIEN, 17.01.2013
  pTabMethod <- match.arg(pTabMethod)
  ind2tab <- function(x){
    ## Matthias Templ, TU WIEN, 17.01.2013
    if(!all.equal(dim(x), c(2,2))) stop("ind2.table is for 2x2 tables.")
    xind <- x
    xind[1,1] <- x[1,1]*(x[2,1]*x[1,2])^(1/2) 	
    xind[2,1] <- x[2,1]*(x[1,1]*x[2,2])^(1/2)
    xind[1,2] <- x[1,2]*(x[1,1]*x[2,2])^(1/2)
    xind[2,2] <- x[2,2]*(x[2,1]*x[1,2])^(1/2)
    pTab(xind, method = pTabMethod)
  }
  dn <- dimnames(x)
  if(length(dim(x)) == 2){
    res <- ind2tab(x)
  } else {
    res <- apply(x, margin, ind2tab)
    res <- array(res, dim=c(2,2,ncol(res)))
  }
  dimnames(res) <- dn
  class(res) <- "ind2x2"
  res	
}
back to top