FourierStuff/R/chisquare.test.R

43 lines
1.4 KiB
R
Raw Normal View History

2014-02-20 01:35:43 +00:00
# Pearson's Chi-Squared test based on the characteristic function
#
# Author: François Pelletier
2014-03-15 15:41:07 +00:00
#
# LGPL 3.0
2014-02-20 01:35:43 +00:00
###############################################################################
#' Pearson's Chi-Squared test based on the characteristic function
2014-03-15 15:41:07 +00:00
#' @param datahist histogram object of the data
2014-02-22 18:27:24 +00:00
#' @param FUN Characteristic function (integral) or
#' Saddlepoint distribution approximation (saddlepoint)
#' @param ... FUN arguments
2014-02-20 01:35:43 +00:00
#' @param alpha tolerance level
2014-02-22 18:27:24 +00:00
#' @param method Method to approximate the distribution function. "integral" or "saddlepoint"
2014-02-20 01:35:43 +00:00
#'
#' @return A list containing the chi-square statistic,
#' degree of freedom, hypothesis reject boolean and p.value
#' @export chisquare.test
2014-02-20 01:35:43 +00:00
#' @author François Pelletier
2014-03-15 15:41:07 +00:00
chisquare.test <- function(datahist,FUN,...,alpha=0.05,method="integral")
2014-02-20 01:35:43 +00:00
{
# Compute expected values for each histogram breaks using the characteristic function
2014-03-15 15:41:07 +00:00
classes <- datahist$breaks
observed <- datahist$counts
2014-02-22 18:27:24 +00:00
if(method=="integral")
{
expected <- diff(cftocdf(classes,FUN,...)*
2014-03-15 15:41:07 +00:00
sum(observed))
2014-02-22 18:27:24 +00:00
}
else if(method=="saddlepoint")
{
expected <- diff(FUN(classes,...)*
2014-03-15 15:41:07 +00:00
sum(observed))
2014-02-22 18:27:24 +00:00
}
2014-02-20 01:35:43 +00:00
# Compute the test statistic using chi-square distribution
2014-03-15 15:41:07 +00:00
chisquare.stat<-sum((observed-expected)^2/expected)
df<-length(classes)-2
p.value <- pchisq(chisquare.stat,df,lower.tail=FALSE)
2014-02-20 01:35:43 +00:00
# Create the return list
2014-03-15 15:41:07 +00:00
list(chisquare.stat=chisquare.stat,df=df,p.value=p.value)
2014-02-20 01:35:43 +00:00
}