R/count.R

Defines functions count

Documented in count

##' Fisher (or paired sample) randomization test with complete enumerations
##'
##' Carries out the Fisher randomization test or the paired comparison
##' randomization test. It is a convenience function invoked by the fuller
##' function \code{RT1SAMP}.
##'
##' The function calculates the sum of elements in \code{x} as the test
##' statistic; then the full set of \eqn{2^n} elements (\code{n = length(x)})
##' in the randomization distribution of the test statistic are generated by
##' consecutively changing the sign of each element in \code{x}. In this way
##' all possible signs of all elements in \code{x} occur by the time the
##' \eqn{2^n}-th calculation is made. The assumption made is that the
##' significance level of the observed statistic \code{Sobs} is the proportion
##' of randomized values greater than or equal to \code{Sobs}. For a lower tail
##' test, the proportion is incremented if the randomized values of the test
##' statistic are less than or equal \code{Sobs}. For a two-tailed test, the
##' proportion is incremented if the absolute value of the values attained by
##' the randomized test statistic are greater than or equal the absolute value
##' of the observed test statistic.
##'
##' @param x a (non-empty) numeric vector of data values.
##' @param mu an scalar used in Fisher's one-sample randomization test, where
##' only a single sample is concerned rather than paired comparisons, and the
##' question of interest is whether this sample could have come from a
##' population with a mean equal to a particular value \eqn{\mu}.
##' @return The function returns a \code{RT} result object (list).
##' \item{Sobs}{ Observed sum of elements in \code{x} (the observed value of
##' the test statistic) } \item{name}{ A character string giving the name of
##' the test. } \item{P}{ A numeric vector, giving the p-value for the lower-,
##' upper-, and two-tailed tests, respectively } \item{nperm}{ An scalar,
##' namely \eqn{2^n}, the number of elements in the full randomization
##' distribution }
##' @author Jorge Navarro-Alberto
##' @seealso \code{\link{RT1SAMP}}, the main function for Fisher's (or paired
##' sample) randomization test.
##' @references Manly, B.F.J. and Navarro-Alberto, J.A. (2021) Randomization,
##' Bootstrap and Monte Carlo Methods in Biology. 4th Edition. CRC Press.
##' @examples
##'
##' # Example in Manly and Navarro Alberto (2021), Section 5.1
##' Dif <- cornheight[,"Cross"] - cornheight[,"Self"]
##' Results.count <- count(Dif)
##' Results.count
##'
##' @export
count <- function(x, mu=0) {
# Fisher's (paired-sample) RT, by complete enumeration
# Find the length of the observed vector
  n <- length(x)
# Compute the sumD-statistic for the observed data
  sumD0 <- sum(x - mu)
  abs.sumD0 <- abs(sumD0)
  imax <- 2.^n
  nTLG <- rep(0, 3)
  P <- vector(mode="numeric", length=3)
  EPS <- 1e-05
  for (j in 1:imax) {
    # Compute the sumD-statistic
    sumD <- sum(x - mu)
    if (sumD < sumD0 + EPS)
      nTLG[1] <- nTLG[1] + 1
    if (sumD > sumD0 - EPS)
      nTLG[2] <- nTLG[2] + 1
    if (abs(sumD) > abs.sumD0 - EPS)
      nTLG[3] <- nTLG[3] + 1
    for (i in 1:n) {
      if (j%%(2.^(i-1)) == 0) x[i] <- -x[i]
    }
  }
  # Compute the p-value of complete enumerations
  P <- nTLG * 100/imax
  names(P) <- c("Less", "Greater", "Two")
  count.stats <- list(Sobs=sumD0,
                      name="Fisher (or paired sample) randomization test",
                      P = P, nperm = imax)
  return(count.stats)
}
ganava4/rbmc documentation built on April 24, 2022, 12:14 a.m.