R/kSymmetryTest.R

#' kSymmetryTest
#'
#' Performs a pdf symmetry test for given data based on the estimated kernel densities and the permutation test.
#'
#' @param x A numeric vector of data.
#' @param around The desired value to check if the density is symmetric around. Must be one of 'mean', 'median', or a real number.
#' @param ... Futher parameters to be passed to \link[ktest]{kTest}.
#'
#' @return A list containing:
#'
#' - densities: The estimated densities for each group.
#'
#' - labels: The group labels.
#'
#' - ca: Common area between the (data - point) and (point - data) densities.
#'
#' - pvalue: The p-value generated by the permutation test.
#'
#' @export
#'
#' @examples x = rnorm(100)
#' test = kSymmetryTest(x)
#' print(test)
#' plot(test)

kSymmetryTest = function(x, around = 'median', ...) {

  if(!is.numeric(x)) {

    stop('x must be a numeric vector of data.')

  }

  if(around != 'median' & around != 'mean' & !is.numeric(around)) {

    stop('around must be one of "mean", "median", or a real number.')

  }

  if(around == 'median') {

    around_ = median(x)

  }

  if(around == 'mean') {

    around_ = mean(x)

  }

  if(is.numeric(around)) {

    around_ = around

  }

  a = x - around_

  b = around_ - x

  output = kTest(list(a, b), ...)

  output$labels = c('data - point', 'point - data')

  output$around = around

  class(output) = c('kSymmetryTest', 'densities')

  return(output)
}


#' @export

print.kSymmetryTest = function(output) {

  cat('\n kSymmetryTest results: \n\n')

  cat(paste('- Common area between densities:', round(output$ca, 4)))

  if(!is.null(output$pvalue)) {

    cat(paste0('\n\n- p-value for H0 (Density is symmetric around ', output$around, '): ', round(output$pvalue, 4),'\n\n'))

  }
}
vcoscrato/ktest documentation built on May 9, 2019, 10:01 p.m.