Ajout de la fonction chisquare.test

This commit is contained in:
François Pelletier 2014-02-19 20:35:43 -05:00
parent 7ef76de319
commit 9f17d7a2f7
4 changed files with 65 additions and 17 deletions

View file

@ -6,25 +6,25 @@
#' Get distribution function from characteristic function
#'
#' @param grid Distribution function evaluation points
#' @param char.fun Vectorized characteristic function
#' @param n Amount of discretization points
#' @param min Lower bound for distribution function
#' @param max Upper bound for distribution function
#' @param param Characteristic function parameters
#' @param wmin Lower bound for transform variate
#' @param wmax Upper bound for transform variate
#' @return Distribution function values evaluated on [min,max] range
#' @author François Pelletier
cftocdf <- function(char.fun,n,min,max,param,wmin=0,wmax=50,MSwindows=FALSE)
cftocdf <- function(grid,char.fun,...,wmin=0,wmax=50,MSwindows=FALSE)
{
grid <- seq(from=min,to=max,length.out=n)
integrand <- function(w,x,char.fun,param,wmax=50) (1-w/wmax)*
Im(exp(-1i*w*x)*char.fun(w,param)) / w
n <- length(grid)
# Integral in the Gil-Pelaez Ttheorem
integrand <- function(w,x,char.fun,...,wmax=50) (1-w/wmax)*
Im(exp(-1i*w*x)*char.fun(w,...)) / w
# Integrate for each grid point using parallel computation if available
if(!MSwindows)
{
unlist(mclapply(grid,
function(x) 1/2-1/pi*
integrate(integrand,wmin,wmax,x,char.fun,param)$value))
integrate(integrand,wmin,wmax,x,char.fun,...)$value))
}
else
{
@ -32,7 +32,7 @@ cftocdf <- function(char.fun,n,min,max,param,wmin=0,wmax=50,MSwindows=FALSE)
Fx <- numeric(n)
for(i in 1:n)
{
Fx[i] <- 1/2-1/pi*integrate(integrand,wmin,wmax,x,char.fun,param)$value
Fx[i] <- 1/2-1/pi*integrate(integrand,wmin,wmax,x,char.fun,...)$value
}
}
}

26
R/chisquare.test.R Normal file
View file

@ -0,0 +1,26 @@
# Pearson's Chi-Squared test based on the characteristic function
#
# Author: François Pelletier
###############################################################################
#' Pearson's Chi-Squared test based on the characteristic function
#' @param DATA.hist histogram object of the data
#' @param char.fun Characteristic function
#' @param ... Characteristic function arguments
#' @param alpha tolerance level
#'
#' @return A list containing the chi-square statistic,
#' degree of freedom, hypothesis reject boolean and p.value
#' @author François Pelletier
chisquare.test <- function(DATA.hist,char.fun,...,alpha=0.05)
{
# Compute expected values for each histogram breaks using the characteristic function
expected <- diff(cftocdf(classes <- DATA.hist$breaks,char.fun,...)*
sum(observed <- DATA.hist$counts))
# Compute the test statistic using chi-square distribution
p.value <- pchisq(chisquare.stat<-sum((observed-expected)^2/expected),
df<-length(classes)-2,lower.tail=FALSE)
# Create the return list
list(chisquare.stat=chisquare.stat,df=df,reject=p.value<alpha,p.value=p.value)
}

View file

@ -2,18 +2,13 @@
\alias{cftocdf}
\title{Get distribution function from characteristic function}
\usage{
cftocdf(char.fun, n, min, max, param, wmin = 0, wmax = 50,
MSwindows = FALSE)
cftocdf(grid, char.fun, ..., wmin = 0, wmax = 50, MSwindows = FALSE)
}
\arguments{
\item{grid}{Distribution function evaluation points}
\item{char.fun}{Vectorized characteristic function}
\item{n}{Amount of discretization points}
\item{min}{Lower bound for distribution function}
\item{max}{Upper bound for distribution function}
\item{param}{Characteristic function parameters}
\item{wmin}{Lower bound for transform variate}

27
man/chisquare.test.Rd Normal file
View file

@ -0,0 +1,27 @@
\name{chisquare.test}
\alias{chisquare.test}
\title{Pearson's Chi-Squared test based on the characteristic function}
\usage{
chisquare.test(DATA.hist, char.fun, ..., alpha = 0.05)
}
\arguments{
\item{DATA.hist}{histogram object of the data}
\item{char.fun}{Characteristic function}
\item{...}{Characteristic function arguments}
\item{alpha}{tolerance level}
}
\value{
A list containing the chi-square statistic, degree of
freedom, hypothesis reject boolean and p.value
}
\description{
Pearson's Chi-Squared test based on the characteristic
function
}
\author{
François Pelletier
}