goftest/0000755000176200001440000000000014127535702011733 5ustar liggesusersgoftest/NAMESPACE0000755000176200001440000000025713115213326013150 0ustar liggesusersexport("ad.test", "pAD", "qAD") export("cvm.test", "pCvM", "qCvM") export("recogniseCdf") useDynLib(goftest, .registration=TRUE, .fixes="Cgof") importFrom("stats", "uniroot") goftest/man/0000755000176200001440000000000014127521037012502 5ustar liggesusersgoftest/man/recogniseCdf.Rd0000755000176200001440000000206613305702127015371 0ustar liggesusers\name{recogniseCdf} \alias{recogniseCdf} \title{ Explanatory Name of Distribution Function } \description{ Recognises many standard cumulative distribution functions and returns a string describing the distribution. } \usage{ recogniseCdf(s="punif") } \arguments{ \item{s}{ A single character string giving the \emph{name} of an \R function that calculates cumulative probabilities. } } \details{ The list of recognised distribution functions includes all those available in the \pkg{stats} package and in \pkg{goftest}. By convention, the name of a cumulative distribution function begins with the letter \code{p}. For example, \code{punif} is the cumulative distribution function of the uniform distribution. The initial letter \code{p} can be omitted in the function \code{recogniseCdf}. } \value{ Character string, or \code{NULL} if the name is not recognised. } \author{ Adrian Baddeley. } \seealso{ \code{\link{pAD}} } \examples{ recogniseCdf("punif") recogniseCdf("unif") recogniseCdf("pt") } \keyword{distribution} goftest/man/cvm.test.Rd0000755000176200001440000000607713305727666014566 0ustar liggesusers\name{cvm.test} \alias{cvm.test} \title{ Cramer-Von Mises Test of Goodness-of-Fit } \description{ Performs the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test of goodness-of-fit to a specified continuous univariate probability distribution. } \usage{ cvm.test(x, null = "punif", ..., estimated=FALSE, nullname) } \arguments{ \item{x}{ Numeric vector of data values. } \item{null}{ A function, or a character string giving the name of a function, to compute the cumulative distribution function for the null distribution. } \item{\dots}{ Additional arguments for the cumulative distribution function. } \item{estimated}{ Logical value indicating whether the parameters of the distribution were estimated using the data \code{x} (composite null hypothesis), or were fixed in advance (simple null hypothesis, the default). } \item{nullname}{ Optional character string describing the null distribution. The default is \code{"uniform distribution"}. } } \details{ This command performs the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test of goodness-of-fit to the distribution specified by the argument \code{null}. It is assumed that the values in \code{x} are independent and identically distributed random values, with some cumulative distribution function \eqn{F}. The null hypothesis is that \eqn{F} is the function specified by the argument \code{null}, while the alternative hypothesis is that \eqn{F} is some other function. By default, the test assumes that all the parameters of the null distribution are known in advance (a \emph{simple} null hypothesis). This test does not account for the effect of estimating the parameters. If the parameters of the distribution were estimated (that is, if they were calculated from the same data \code{x}), then this should be indicated by setting the argument \code{estimated=TRUE}. The test will then use the method of Braun (1980) to adjust for the effect of parameter estimation. Note that Braun's method involves randomly dividing the data into two equally-sized subsets, so the \eqn{p}-value is not exactly the same if the test is repeated. This technique is expected to work well when the number of observations in \code{x} is large. } \value{ An object of class \code{"htest"} representing the result of the hypothesis test. } \references{ Braun, H. (1980) A simple method for testing goodness-of-fit in the presence of nuisance parameters. \emph{Journal of the Royal Statistical Society} \bold{42}, 53--63. \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo}, S. and Faraway, J.J. (1996) The exact and asymptotic distributions of \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises statistics. \emph{Journal of the Royal Statistical Society, Series B} \bold{58}, 221--234. } \author{ Adrian Baddeley. } \seealso{ \code{\link{pCvM}} for the null distribution of the test statistic. } \examples{ x <- rnorm(10, mean=2, sd=1) cvm.test(x, "pnorm", mean=2, sd=1) cvm.test(x, "pnorm", mean=mean(x), sd=sd(x), estimated=TRUE) } \keyword{htest} goftest/man/ad.test.Rd0000755000176200001440000000653314127520763014352 0ustar liggesusers\name{ad.test} \alias{ad.test} \title{ Anderson-Darling Test of Goodness-of-Fit } \description{ Performs the Anderson-Darling test of goodness-of-fit to a specified continuous univariate probability distribution. } \usage{ ad.test(x, null = "punif", ..., estimated=FALSE, nullname) } \arguments{ \item{x}{ Numeric vector of data values. } \item{null}{ A function, or a character string giving the name of a function, to compute the cumulative distribution function for the null distribution. } \item{\dots}{ Additional arguments for the cumulative distribution function. } \item{estimated}{ Logical value indicating whether the parameters of the distribution were estimated using the data \code{x} (composite null hypothesis), or were fixed in advance (simple null hypothesis, the default). } \item{nullname}{ Optional character string describing the null distribution. The default is \code{"uniform distribution"}. } } \details{ This command performs the Anderson-Darling test of goodness-of-fit to the distribution specified by the argument \code{null}. It is assumed that the values in \code{x} are independent and identically distributed random values, with some cumulative distribution function \eqn{F}. The null hypothesis is that \eqn{F} is the function specified by the argument \code{null}, while the alternative hypothesis is that \eqn{F} is some other function. By default, the test assumes that all the parameters of the null distribution are known in advance (a \emph{simple} null hypothesis). This test does not account for the effect of estimating the parameters. If the parameters of the distribution were estimated (that is, if they were calculated from the same data \code{x}), then this should be indicated by setting the argument \code{estimated=TRUE}. The test will then use the method of Braun (1980) to adjust for the effect of parameter estimation. Note that Braun's method involves randomly dividing the data into two equally-sized subsets, so the \eqn{p}-value is not exactly the same if the test is repeated. This technique is expected to work well when the number of observations in \code{x} is large. } \value{ An object of class \code{"htest"} representing the result of the hypothesis test. } \references{ Anderson, T.W. and Darling, D.A. (1952) Asymptotic theory of certain 'goodness-of-fit' criteria based on stochastic processes. \emph{Annals of Mathematical Statistics} \bold{23}, 193--212. Anderson, T.W. and Darling, D.A. (1954) A test of goodness of fit. \emph{Journal of the American Statistical Association} \bold{49}, 765--769. Braun, H. (1980) A simple method for testing goodness-of-fit in the presence of nuisance parameters. \emph{Journal of the Royal Statistical Society} \bold{42}, 53--63. Marsaglia, G. and Marsaglia, J. (2004) Evaluating the Anderson-Darling Distribution. \emph{Journal of Statistical Software} \bold{9} (2), 1--5. February 2004. \doi{10.18637/jss.v009.i02} } \author{ Original C code by George Marsaglia and John Marsaglia. \R interface by Adrian Baddeley. } \seealso{ \code{\link{pAD}} for the null distribution of the test statistic. } \examples{ x <- rnorm(10, mean=2, sd=1) ad.test(x, "pnorm", mean=2, sd=1) ad.test(x, "pnorm", mean=mean(x), sd=sd(x), estimated=TRUE) } \keyword{htest} goftest/man/goftest-package.Rd0000755000176200001440000000546514127521004016044 0ustar liggesusers\name{goftest-package} \alias{goftest-package} \alias{goftest} \docType{package} \title{ Classical Goodness-of-Fit Tests } \description{ \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises and Anderson-Darling tests of goodness-of-fit for continuous univariate distributions, using modern algorithms to compute the null distributions. } \details{ The \pkg{goftest} package contains implementations of the classical \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises and Anderson-Darling tests of goodness-of-fit for continuous univariate distributions. The \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test is performed by \code{\link{cvm.test}}. The cumulative distribution function of the null distribution of the test statistic is computed by \code{\link{pCvM}} using the algorithm of \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo} and Faraway (1996). The quantiles are computed by \code{\link{qCvM}} by root-finding. The Anderson-Darling test is performed by \code{\link{ad.test}}. The cumulative distribution function of the null distribution of the test statistic is computed by \code{\link{pAD}} using the algorithm of Marsaglia and Marsaglia (2004). The quantiles are computed by \code{\link{qAD}} by root-finding. By default, each test assumes that the parameters of the null distribution are known (a \emph{simple} null hypothesis). If the parameters were estimated (calculated from the data) then the user should set \code{estimated=TRUE} which uses the method of Braun (1980) to adjust for the effect of estimating the parameters from the data. } \author{ Adrian Baddeley, Julian Faraway, John Marsaglia, George Marsaglia. Maintainer: Adrian Baddeley } \references{ Braun, H. (1980) A simple method for testing goodness-of-fit in the presence of nuisance parameters. \emph{Journal of the Royal Statistical Society} \bold{42}, 53--63. \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo}, S. and Faraway, J.J. (1996) The exact and asymptotic distributions of \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises statistics. \emph{Journal of the Royal Statistical Society, Series B} \bold{58}, 221--234. Marsaglia, G. and Marsaglia, J. (2004) Evaluating the Anderson-Darling Distribution. \emph{Journal of Statistical Software} \bold{9} (2), 1--5. February 2004. \doi{10.18637/jss.v009.i02} } \keyword{package} \keyword{htest} \seealso{ \code{\link[stats]{ks.test}} } \examples{ x <- rnorm(30, mean=2, sd=1) # default behaviour: parameters fixed: simple null hypothesis cvm.test(x, "pnorm", mean=2, sd=1) ad.test(x, "pnorm", mean=2, sd=1) # parameters estimated: composite null hypothesis mu <- mean(x) sigma <- sd(x) cvm.test(x, "pnorm", mean=mu, sd=sigma, estimated=TRUE) ad.test(x, "pnorm", mean=mu, sd=sigma, estimated=TRUE) } goftest/man/pAD.Rd0000755000176200001440000000443014127520772013446 0ustar liggesusers\name{pAD} \alias{pAD} \alias{qAD} \title{ Null Distribution of Anderson-Darling Test Statistic } \description{ \code{pAD} computes the cumulative distribution function, and \code{qAD} computes the quantile function, of the null distribution of the Anderson-Darling test statistic. } \usage{ pAD(q, n = Inf, lower.tail = TRUE, fast=TRUE) qAD(p, n = Inf, lower.tail = TRUE, fast=TRUE) } \arguments{ \item{q}{ Numeric vector of quantiles (values for which the cumulative probability is required). } \item{p}{ Numeric vector of probabilities. } \item{n}{ Integer. Sample size for the Anderson-Darling test. } \item{lower.tail}{ Logical. If \code{TRUE} (the default), probabilities are \eqn{P(X \le q)}{P(X <= q)}, and otherwise they are \eqn{P(X > q)}. } \item{fast}{ Logical value indicating whether to use a fast algorithm or a slower, more accurate algorithm, in the case \code{n=Inf}. } } \details{ \code{pAD} uses the algorithms and C code described in Marsaglia and Marsaglia (2004). \code{qAD} uses \code{\link[stats]{uniroot}} to find the quantiles. The argument \code{fast} applies only when \code{n=Inf} and determines whether the asymptotic distribution is approximated using the faster algorithm \code{adinf} (accurate to 4-5 places) or the slower algorithm \code{ADinf} (accurate to 11 places) described in Marsaglia and Marsaglia (2004). } \value{ A numeric vector of the same length as \code{p} or \code{q}. } \references{ Anderson, T.W. and Darling, D.A. (1952) Asymptotic theory of certain 'goodness-of-fit' criteria based on stochastic processes. \emph{Annals of Mathematical Statistics} \bold{23}, 193--212. Anderson, T.W. and Darling, D.A. (1954) A test of goodness of fit. \emph{Journal of the American Statistical Association} \bold{49}, 765--769. Marsaglia, G. and Marsaglia, J. (2004) Evaluating the Anderson-Darling Distribution. \emph{Journal of Statistical Software} \bold{9} (2), 1--5. February 2004. \doi{10.18637/jss.v009.i02} } \author{ Original C code by G. and J. Marsaglia. \R interface by Adrian Baddeley. } \seealso{ \code{\link{ad.test}} } \examples{ pAD(1.1, n=5) pAD(1.1) pAD(1.1, fast=FALSE) qAD(0.5, n=5) qAD(0.5) } \keyword{distribution} \keyword{htest} goftest/man/RCS/0000755000176200001440000000000013305715027013132 5ustar liggesusersgoftest/man/RCS/pAD.Rd,v0000755000176200001440000000552213115213326014330 0ustar liggesusershead 1.3; access; symbols; locks adrian:1.3; strict; comment @# @; 1.3 date 2014.06.09.08.37.37; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.09.05.02.24; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.09.04.40.36; author adrian; state Exp; branches; next ; desc @@ 1.3 log @minor @ text @\name{pAD} \alias{pAD} \alias{qAD} \title{ Null Distribution of Anderson-Darling Test Statistic } \description{ \code{pAD} computes the cumulative distribution function, and \code{qAD} computes the quantile function, of the null distribution of the Anderson-Darling test statistic. } \usage{ pAD(q, n = Inf, lower.tail = TRUE, fast=TRUE) qAD(p, n = Inf, lower.tail = TRUE, fast=TRUE) } \arguments{ \item{q}{ Numeric vector of quantiles (values for which the cumulative probability is required). } \item{p}{ Numeric vector of probabilities. } \item{n}{ Integer. Sample size for the Anderson-Darling test. } \item{lower.tail}{ Logical. If \code{TRUE} (the default), probabilities are \eqn{P(X \le q)}{P(X <= q)}, and otherwise they are \eqn{P(X > q)}. } \item{fast}{ Logical value indicating whether to use a fast algorithm or a slower, more accurate algorithm, in the case \code{n=Inf}. } } \details{ \code{pAD} uses the algorithms and C code described in Marsaglia and Marsaglia (2004). \code{qAD} uses \code{\link[stats]{uniroot}} to find the quantiles. The argument \code{fast} applies only when \code{n=Inf} and determines whether the asymptotic distribution is approximated using the faster algorithm \code{adinf} (accurate to 4-5 places) or the slower algorithm \code{ADinf} (accurate to 11 places) described in Marsaglia and Marsaglia (2004). } \value{ A numeric vector of the same length as \code{p} or \code{q}. } \references{ Anderson, T.W. and Darling, D.A. (1952) Asymptotic theory of certain 'goodness-of-fit' criteria based on stochastic processes. \emph{Annals of Mathematical Statistics} \bold{23}, 193--212. Anderson, T.W. and Darling, D.A. (1954) A test of goodness of fit. \emph{Journal of the American Statistical Association} \bold{49}, 765--769. Marsaglia, G. and Marsaglia, J. (2004) Evaluating the Anderson-Darling Distribution. \emph{Journal of Statistical Software} \bold{9} (2), 1--5. February 2004. \url{http://www.jstatsoft.org/v09/i02} } \author{ Original C code by G. and J. Marsaglia. \R interface by Adrian Baddeley. } \seealso{ \code{\link{ad.test}} } \examples{ pAD(1.1, n=5) pAD(1.1) pAD(1.1, fast=FALSE) qAD(0.5, n=5) qAD(0.5) } \keyword{distribution} \keyword{htest} @ 1.2 log @minor @ text @d14 2 a15 2 pAD(q, n = Inf, lower.tail = TRUE) qAD(p, n = Inf, lower.tail = TRUE) d33 4 d44 6 d78 1 d80 3 a82 1 pAD(1.1, n=5) a83 1 qAD(0.5, n=5) @ 1.1 log @Initial revision @ text @d45 9 @ goftest/man/RCS/pCvM.Rd,v0000755000176200001440000000372613115213326014535 0ustar liggesusershead 1.1; access; symbols; locks adrian:1.1; strict; comment @# @; 1.1 date 2014.06.08.11.23.35; author adrian; state Exp; branches; next ; desc @@ 1.1 log @Initial revision @ text @\name{pcramer} \alias{pcramer} \alias{qcramer} \title{ Null Distribution of Cramer-von Mises Test Statistic } \description{ \code{pcramer} computes the cumulative distribution function, and \code{qcramer} computes the quantile function, of the null distribution of the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test statistic. } \usage{ pcramer(q, n = Inf, lower.tail = TRUE) qcramer(p, n = Inf, lower.tail = TRUE) } \arguments{ \item{q}{ Numeric vector of quantiles (values for which the cumulative probability is required). } \item{p}{ Numeric vector of probabilities. } \item{n}{ Integer. Sample size for the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test. } \item{lower.tail}{ Logical. If \code{TRUE} (the default), probabilities are \eqn{P(X \le q)}{P(X <= q)}, and otherwise they are \eqn{P(X > q)}. } } \details{ For finite \code{n} the cumulative distribution function is approximated by the first order expansion \eqn{V(x) + \psi_1(x)/n}{V(x) + psi1(x)/n}, equation (1.8) of \ifelse{latex}{\out{Cs\"org\"o}}{Csorgo} and Faraway (1996). \code{qcramer} uses \code{\link[stats]{uniroot}} to find the quantiles. } \value{ A numeric vector of the same length as \code{p} or \code{q}. } \references{ \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo}, S. and Faraway, J.J. (1996) The exact and asymptotic distributions of \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises statistics. \emph{Journal of the Royal Statistical Society, Series B} \bold{58}, 221--234. } \author{ Original Matlab code by Julian Faraway, translated to \R by Adrian Baddeley. } \seealso{ \code{\link{cvm.test}} } \examples{ pcramer(1.1) pcramer(1.1, n=5) qcramer(0.5) qcramer(0.5, n=5) } \keyword{distribution} \keyword{htest} @ goftest/man/RCS/goftest-package.Rd,v0000755000176200001440000001055713305715027016742 0ustar liggesusershead 1.6; access; symbols; locks adrian:1.6; strict; comment @# @; 1.6 date 2018.06.06.08.23.51; author adrian; state Exp; branches; next 1.5; 1.5 date 2018.06.06.08.22.11; author adrian; state Exp; branches; next 1.4; 1.4 date 2018.03.29.13.57.19; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.09.08.37.32; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.09.05.19.07; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.09.05.11.00; author adrian; state Exp; branches; next ; desc @@ 1.6 log @Summary: more stuff @ text @\name{goftest-package} \alias{goftest-package} \alias{goftest} \docType{package} \title{ Classical Goodness-of-Fit Tests } \description{ \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises and Anderson-Darling tests of goodness-of-fit for continuous univariate distributions, using modern algorithms to compute the null distributions. } \details{ The \pkg{goftest} package contains implementations of the classical \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises and Anderson-Darling tests of goodness-of-fit for continuous univariate distributions. The \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test is performed by \code{\link{cvm.test}}. The cumulative distribution function of the null distribution of the test statistic is computed by \code{\link{pCvM}} using the algorithm of \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo} and Faraway (1996). The quantiles are computed by \code{\link{qCvM}} by root-finding. The Anderson-Darling test is performed by \code{\link{ad.test}}. The cumulative distribution function of the null distribution of the test statistic is computed by \code{\link{pAD}} using the algorithm of Marsaglia and Marsaglia (2004). The quantiles are computed by \code{\link{qAD}} by root-finding. By default, each test assumes that the parameters of the null distribution are known (a \emph{simple} null hypothesis). If the parameters were estimated (calculated from the data) then the user should set \code{estimated=TRUE} which uses the method of Braun (1980) to adjust for the effect of estimating the parameters from the data. } \author{ Adrian Baddeley, Julian Faraway, John Marsaglia, George Marsaglia. Maintainer: Adrian Baddeley } \references{ Braun, H. (1980) A simple method for testing goodness-of-fit in the presence of nuisance parameters. \emph{Journal of the Royal Statistical Society} \bold{42}, 53--63. \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo}, S. and Faraway, J.J. (1996) The exact and asymptotic distributions of \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises statistics. \emph{Journal of the Royal Statistical Society, Series B} \bold{58}, 221--234. Marsaglia, G. and Marsaglia, J. (2004) Evaluating the Anderson-Darling Distribution. \emph{Journal of Statistical Software} \bold{9} (2), 1--5. February 2004. \url{http://www.jstatsoft.org/v09/i02} } \keyword{package} \keyword{htest} \seealso{ \code{\link[stats]{ks.test}} } \examples{ x <- rnorm(30, mean=2, sd=1) # default behaviour: parameters fixed: simple null hypothesis cvm.test(x, "pnorm", mean=2, sd=1) ad.test(x, "pnorm", mean=2, sd=1) # parameters estimated: composite null hypothesis mu <- mean(x) sigma <- sd(x) cvm.test(x, "pnorm", mean=mu, sd=sigma, estimated=TRUE) ad.test(x, "pnorm", mean=mu, sd=sigma, estimated=TRUE) } @ 1.5 log @Summary: more stuff @ text @d37 3 a39 1 However, a method is provided to adjust for the effect of d48 5 @ 1.4 log @Summary: added message about simple null @ text @d35 4 a38 3 It is assumed that all the parameters of the null distribution are known (a \emph{simple} null hypothesis). The calculation does not account for the effect of estimating the parameters. d64 2 a65 1 x <- rnorm(10, mean=2, sd=1) d68 5 @ 1.3 log @minor @ text @d34 4 @ 1.2 log @prettified. @ text @d22 2 a23 1 function of the null distribution is computed by \code{\link{pCvM}} d30 2 a31 1 function of the null distribution is computed by \code{\link{pAD}} d59 3 a61 2 x <- rnorm(10) cvm.test(x, "pnorm") @ 1.1 log @Initial revision @ text @d15 17 a31 7 \tabular{ll}{ Package: \tab goftest\cr Type: \tab Package\cr Version: \tab 1.0-0\cr Date: \tab 2014-06-08\cr License: \tab GPL\cr } d34 1 a34 1 Adrian Baddeley, Julian Faraway, John Marsaglia, George Marsaglia @ goftest/man/RCS/cvm.test.Rd,v0000755000176200001440000001023313305714052015425 0ustar liggesusershead 1.6; access; symbols; locks adrian:1.6; strict; comment @# @; 1.6 date 2018.06.06.08.15.38; author adrian; state Exp; branches; next 1.5; 1.5 date 2018.06.06.08.04.55; author adrian; state Exp; branches; next 1.4; 1.4 date 2018.03.29.13.57.31; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.09.08.37.47; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.09.04.42.32; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.08.11.23.23; author adrian; state Exp; branches; next ; desc @@ 1.6 log @Summary: minor @ text @\name{cvm.test} \alias{cvm.test} \title{ Cramer-Von Mises Test of Goodness-of-Fit } \description{ Performs the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test of goodness-of-fit to a specified continuous univariate probability distribution. } \usage{ cvm.test(x, null = "punif", ..., estimated=FALSE, nullname) } \arguments{ \item{x}{ Numeric vector of data values. } \item{null}{ A function, or a character string giving the name of a function, to compute the cumulative distribution function for the null distribution. } \item{\dots}{ Additional arguments for the cumulative distribution function. } \item{estimated}{ Logical value indicating whether the parameters of the distribution were estimated using the data \code{x} (composite null hypothesis), or were fixed in advance (simple null hypothesis, the default). } \item{nullname}{ Optional character string describing the null distribution. The default is \code{"uniform distribution"}. } } \details{ This command performs the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test of goodness-of-fit to the distribution specified by the argument \code{null}. It is assumed that the values in \code{x} are independent and identically distributed random values, with some cumulative distribution function \eqn{F}. The null hypothesis is that \eqn{F} is the function specified by the argument \code{null}, while the alternative hypothesis is that \eqn{F} is some other function. By default, the test assumes that all the parameters of the null distribution are known in advance (a \emph{simple} null hypothesis). This test does not account for the effect of estimating the parameters. If the parameters of the distribution were estimated (that is, if they were calculated from the same data \code{x}), then this should be indicated by setting the argument \code{estimated=TRUE}. The test will then use the adjustment method of Braun (1980) to allow for parameter estimation. Braun's method involves randomly dividing the data into two equally-sized subsets, so the \eqn{p}-value is not the same if the test is repeated. } \value{ An object of class \code{"htest"} representing the result of the hypothesis test. } \references{ Braun, H. (1980) A simple method for testing goodness-of-fit in the presence of nuisance parameters. \emph{Journal of the Royal Statistical Society} \bold{42}, 53--63. \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo}, S. and Faraway, J.J. (1996) The exact and asymptotic distributions of \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises statistics. \emph{Journal of the Royal Statistical Society, Series B} \bold{58}, 221--234. } \author{ Adrian Baddeley. } \seealso{ \code{\link{pCvM}} for the null distribution of the test statistic. } \examples{ x <- rnorm(10, mean=2, sd=1) cvm.test(x, "pnorm", mean=2, sd=1) cvm.test(x, "pnorm", mean=mean(x), sd=sd(x), estimated=TRUE) } \keyword{htest} @ 1.5 log @Summary: new argument @ text @d56 3 a58 1 to allow for parameter estimation. @ 1.4 log @Summary: added message about simple null @ text @d13 1 a13 1 cvm.test(x, null = "punif", ..., nullname) d27 5 d48 9 a56 3 This version of the test assumes that all the parameters of the null distribution are known (a \emph{simple} null hypothesis). It does not account for the effect of estimating the parameters. d63 5 d83 1 @ 1.3 log @minor @ text @d42 4 @ 1.2 log @name change @ text @d61 2 a62 2 x <- runif(10) cvm.test(x) @ 1.1 log @Initial revision @ text @d58 1 a58 1 \code{\link{pcramer}} for the null distribution of the test statistic. @ goftest/man/RCS/ad.test.Rd,v0000755000176200001440000001073413305714047015236 0ustar liggesusershead 1.5; access; symbols; locks adrian:1.5; strict; comment @# @; 1.5 date 2018.06.06.08.15.34; author adrian; state Exp; branches; next 1.4; 1.4 date 2018.06.06.08.05.03; author adrian; state Exp; branches; next 1.3; 1.3 date 2018.03.29.13.57.43; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.09.08.37.42; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.09.04.48.21; author adrian; state Exp; branches; next ; desc @@ 1.5 log @Summary: minor @ text @\name{ad.test} \alias{ad.test} \title{ Anderson-Darling Test of Goodness-of-Fit } \description{ Performs the Anderson-Darling test of goodness-of-fit to a specified continuous univariate probability distribution. } \usage{ ad.test(x, null = "punif", ..., estimated=FALSE, nullname) } \arguments{ \item{x}{ Numeric vector of data values. } \item{null}{ A function, or a character string giving the name of a function, to compute the cumulative distribution function for the null distribution. } \item{\dots}{ Additional arguments for the cumulative distribution function. } \item{estimated}{ Logical value indicating whether the parameters of the distribution were estimated using the data \code{x} (composite null hypothesis), or were fixed in advance (simple null hypothesis, the default). } \item{nullname}{ Optional character string describing the null distribution. The default is \code{"uniform distribution"}. } } \details{ This command performs the Anderson-Darling test of goodness-of-fit to the distribution specified by the argument \code{null}. It is assumed that the values in \code{x} are independent and identically distributed random values, with some cumulative distribution function \eqn{F}. The null hypothesis is that \eqn{F} is the function specified by the argument \code{null}, while the alternative hypothesis is that \eqn{F} is some other function. By default, the test assumes that all the parameters of the null distribution are known in advance (a \emph{simple} null hypothesis). This test does not account for the effect of estimating the parameters. If the parameters of the distribution were estimated (that is, if they were calculated from the same data \code{x}), then this should be indicated by setting the argument \code{estimated=TRUE}. The test will then use the adjustment method of Braun (1980) to allow for parameter estimation. Braun's method involves randomly dividing the data into two equally-sized subsets, so the \eqn{p}-value is not the same if the test is repeated. } \value{ An object of class \code{"htest"} representing the result of the hypothesis test. } \references{ Anderson, T.W. and Darling, D.A. (1952) Asymptotic theory of certain 'goodness-of-fit' criteria based on stochastic processes. \emph{Annals of Mathematical Statistics} \bold{23}, 193--212. Anderson, T.W. and Darling, D.A. (1954) A test of goodness of fit. \emph{Journal of the American Statistical Association} \bold{49}, 765--769. Braun, H. (1980) A simple method for testing goodness-of-fit in the presence of nuisance parameters. \emph{Journal of the Royal Statistical Society} \bold{42}, 53--63. Marsaglia, G. and Marsaglia, J. (2004) Evaluating the Anderson-Darling Distribution. \emph{Journal of Statistical Software} \bold{9} (2), 1--5. February 2004. \url{http://www.jstatsoft.org/v09/i02} } \author{ Original C code by George Marsaglia and John Marsaglia. \R interface by Adrian Baddeley. } \seealso{ \code{\link{pAD}} for the null distribution of the test statistic. } \examples{ x <- rnorm(10, mean=2, sd=1) ad.test(x, "pnorm", mean=2, sd=1) ad.test(x, "pnorm", mean=mean(x), sd=sd(x), estimated=TRUE) } \keyword{htest} @ 1.4 log @Summary: new argument @ text @d54 3 a56 1 to allow for parameter estimation. @ 1.3 log @Summary: added message about simple null @ text @d12 1 a12 1 ad.test(x, null = "punif", ..., nullname) d26 5 d46 9 a54 3 This version of the test assumes that all the parameters of the null distribution are known (a \emph{simple} null hypothesis). It does not account for the effect of estimating the parameters. d70 5 d91 1 @ 1.2 log @minor @ text @d40 4 @ 1.1 log @Initial revision @ text @d47 1 a47 1 Asymptotic theory of certain `goodness-of-fit’ criteria based d53 1 a53 1 \emph{Journal of the American Statistical Association} \bold{49}, 765-–769. d62 1 a62 1 C code by G. and J. Marsaglia. d69 2 a70 2 x <- runif(10) ad.test(x) a72 1 @ goftest/man/pCvM.Rd0000755000176200001440000000336313115213326013641 0ustar liggesusers\name{pCvM} \alias{pCvM} \alias{qCvM} \title{ Null Distribution of Cramer-von Mises Test Statistic } \description{ \code{pCvM} computes the cumulative distribution function, and \code{qCvM} computes the quantile function, of the null distribution of the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test statistic. } \usage{ pCvM(q, n = Inf, lower.tail = TRUE) qCvM(p, n = Inf, lower.tail = TRUE) } \arguments{ \item{q}{ Numeric vector of quantiles (values for which the cumulative probability is required). } \item{p}{ Numeric vector of probabilities. } \item{n}{ Integer. Sample size for the \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises test. } \item{lower.tail}{ Logical. If \code{TRUE} (the default), probabilities are \eqn{P(X \le q)}{P(X <= q)}, and otherwise they are \eqn{P(X > q)}. } } \details{ For finite \code{n} the cumulative distribution function is approximated by the first order expansion \eqn{V(x) + \psi_1(x)/n}{V(x) + psi1(x)/n}, equation (1.8) of \ifelse{latex}{\out{Cs\"org\"o}}{Csorgo} and Faraway (1996). \code{qCvM} uses \code{\link[stats]{uniroot}} to find the quantiles. } \value{ A numeric vector of the same length as \code{p} or \code{q}. } \references{ \ifelse{latex}{\out{Cs\"org\H{o}}}{Csorgo}, S. and Faraway, J.J. (1996) The exact and asymptotic distributions of \ifelse{latex}{\out{Cram\'er}}{Cramer}-von Mises statistics. \emph{Journal of the Royal Statistical Society, Series B} \bold{58}, 221--234. } \author{ Original Matlab code by Julian Faraway, translated to \R by Adrian Baddeley. } \seealso{ \code{\link{cvm.test}} } \examples{ pCvM(1.1, n=5) pCvM(1.1) qCvM(0.5, n=5) qCvM(0.5) } \keyword{distribution} \keyword{htest} goftest/DESCRIPTION0000755000176200001440000000175414127535702013453 0ustar liggesusersPackage: goftest Type: Package Title: Classical Goodness-of-Fit Tests for Univariate Distributions Version: 1.2-3 Date: 2021-10-07 Authors@R: c(person("Julian", "Faraway", role = "aut"), person("George", "Marsaglia", role = "aut"), person("John", "Marsaglia", role = "aut"), person("Adrian", "Baddeley", role = c("aut", "cre"), email = "Adrian.Baddeley@curtin.edu.au")) Depends: R (>= 3.3) Imports: stats Description: Cramer-Von Mises and Anderson-Darling tests of goodness-of-fit for continuous univariate distributions, using efficient algorithms. URL: https://github.com/baddstats/goftest BugReports: https://github.com/baddstats/goftest/issues License: GPL (>= 2) NeedsCompilation: yes Packaged: 2021-10-07 07:30:39 UTC; adrian Author: Julian Faraway [aut], George Marsaglia [aut], John Marsaglia [aut], Adrian Baddeley [aut, cre] Maintainer: Adrian Baddeley Repository: CRAN Date/Publication: 2021-10-07 09:20:02 UTC goftest/build/0000755000176200001440000000000014127521037013026 5ustar liggesusersgoftest/build/partial.rdb0000644000176200001440000003015414127521037015156 0ustar liggesusers}rIg2c7 4&A/7a1t\[v R0o>ƾ}}} l֩2%u]"߱U9ɓSk}}Z/<"Ex?y8〖pP/f[մ~'?W=_Ó7]3SB}5K}WROB ?}:{!*EjVky/]5^Ar`_?kyD.?oTjMVqX^6ZĹ?W-W涽_wjfXXS,r;+s{.b[sPKvŖo97"8ĽR6E}o@4g^{ooed}bx䧷1br'oRB5QP!Dud^/÷UJu_opI~YqR@x$kcVp\ۚ};%k0{=e;Ԭ <}GTj)IԼm>'`:!Ih>3!Zk;eUv["#*jU}CZUF6YeṶs4`[UӪ5,z2^+*%zȊ[u̝ZմiH/诔5|p Y!bL8m=F\=?yNdBTaVTbklde >3˞מn|IEK3%2 ~\}/} (D9gd3&(r]vEŸ}XFrBT){U+Y`Bv,U}K5;ƠB)S=0}V֬bf()m_C8u-epd>)di0D ʗ脬a;!Tjlf(0}묰;zj8bD/ 3?SN.@ohQ!~"]T//JjjZIx.:Fh Aj‡J""=hls@R`CK8앃,@%bSQss$FxEM^x${bѤy!UuCY$迄j @o.%Yskc{//jȨ*]'/ < lb'0(5x$-9=yLTP`@ *^U6p˥~\ ti&4c4{X B<(('7oMLlN>3Q v1jMo- 6Mqz2)+*tgv[&L}5?zeZL/~Э]\) kp7yF8 &b7I.=&&jnr(O ]8z,q/yRViE!T:9mBN}Azp̠fuҥJᘘus]k Q^%)7ޤhI]΁|IssBxME{ފ(:'Z?"YʼnH4PjG"v)Љ9c\J3[NK*@甩L|NrO75Nx-ZxqDX^SLgV ?ȡDT}haRv`Vk 23PnKƉz&Q{+۠'ămc"*K]nBnJ׆éG<6v x۳nz#&.b}7Q+6U5(Sov&vW@ߒ՗^o-!zj8yZI5 |tGr?zH+#"n_N֞O/n( brx>&\,-}QTfnӠb 1`q{g'*тd!<0k-@iJxsreEdh@,pBgo+1lKErpC$|`/07B`׼!dåF[.OvF;yQ`1Qn^= //@t ŘЇި@c\O]Sq\zd"$~xC\JŢKηm:xS)H'3}VF&MG05ut Z}y?<5-;u޴ %4!Ac)4{3,Vss"MGYlz~~._˰f NRszQmw!] w:D5ݫ/^ojܴ>إ^ LEE漇d"fisi܁͌5uͿz.ѭx.x;]@hBG+qb I txM7AoJksp_5-b siaMPn| a#. Gz8FŠNL؛pMRࣰoi/땡AZEف"OϷ1;->M8cXNɝ, &9.qӱ xei&*y1!l$ Љ"FU7KQynX>S&IhGMт#BTsy)kAIE{%*nk&O6d+V?L.@˟.pio"('*n_$px5W* ~EX!!wȲ~[쀟{a: b,j8j ׵ ޸PH4h{c6K_~C \̘+7NJb3nU3Hj%wuuu3s6''g~e]7~Vf..vIˍ1L筿~^_&[XM I?Q=_eͦnWwun{1k;{sPGU ڞ.zkZS@+"{Ѽe8έ/qS&\4׼]vJcƱ@*,ҾBU66wKθOc3NL`aSo~S; 7VbW;d;7w͸)>=]DQ۲ 71ON?"M4Euy:c8pMv3hV^tfiO{_zREUIĤw6!01LkbA=3HGvuAj6Zg(7-7K.{'R(|S%@TCy5F_+zW|3kۚ}W^2?z| g. & z1GYm%?}:'[?ԂC<6߳-Cp

umZXS,r;+s{.ɃްU+ڇo'iMoOOGZXa,W 1w['_&/ O_>Ѡӧ/) "ʸrr)*cO{%c,f⺻橤}\-+߽(]?<nWnzHwX;ּ~";/ATRd/|񷖪1Q:gO,TP蜌FePo !ZF{,>y'z8Vl7sj5.^Cf? %ge·8bW]2a?r]8#Z}xԱr2H'( 7.j8:Qˇ\[ 20=nR^+ڰM*8%jE$9CfA8 (gLk;9ү&ģwA'/BHJP/a@'J&D=Ƒ𖛟_CD, \WA'{1!Ն@WkMЛerbCpbΆإ@_輳!v)EWOI/ˠC 8hxz vnxvk+Bu^i^rP8ptA\wF8#)JOAf=᠀ W@'5Ϙ:$,&^ɑ@9sor0:&w{(i,ґ-ɱ݃!$,3D5:chR5 ʴWLd|,Z)HNz]_;>ش%Rr7h~ߏ@)4hu[VbV3 ~DX=+rMW_!n_续~`xTRMRguzQig߀F WI.}V*2ר6V+j#4Ǚf%܄5NjjƘ>}́I+m웞%عDo 60^h~n6NſԼ^",5 UzQT[,Buð lrM݊oonPdz܅nmGmFSG"ľC< À8⑬'5Lϴdʴ ׸=qvpn[FeJ^iZ"_Qe7QR,I/wf f޶,Zer-kYq>!822,z&QrPE u {PZ623*]^'*ZyNzCN25zOa] ދ v͡qoۇoiiTH@:?#v)bCt݊V`9+ IONtmPk@xiiu g⒰!IA"KnmF`k5͟gQĈGriqs=we-QX\"ŝxw׸.#٭2 /,tAR@u <ęĆ8%`1MnAW{g&<ǽ,ܔI GKJ4Scǩ!#(zm5<*b72T"@swur[)[7O_nlm)` + qJ y1J N$R?y*f?l@ދ~Bwgz^(S!jELZ{$U>o;-}5V&܌*鏱jH EUP-8?>tZ;1\+æ.vn" KvY6cz-ÂmYA.obRODh^f5!Q oB#Zr.dz =}lǒ[Aj.(y s 䝛+ЉNٝvmZ4v`&qj7W}ʪ&(K'@'Z!e5 14@9OWؓyO`4DiO{80q?3{[KRy n ұië Y\mVRX4>3:c ON7V|_KH/|IΥ v[>^KT. "c&/?:QDr=^^pBb?!Ux/t>RAHA!_Vh Ė kchx2ͶM5ST054!wH)ťaHT<-S w-)eš(aC<">f' ~rzءPP> f4ˑTH.\[\7#jF$C>ܠ/(KZkac?ٍ3֔ҨQУ7%n4P)כM)g5S5ybg!:vQ鹨=*uBϰC|$(3Ǡa|,϶ni_cjbc^6>,ƞ[V 'KyY j h0:BlύNW:&(D;&Ȭ.mZ3(gcE; 1T<F5A&}C䩨 JsG\,MɎq]2F˲(Tm5ttC -4A"v6 IgHv.f8c!sp5173gz0R #&@5F9R`j][|=l~^x>w_Z9|i 7IO˪8!IQޣKDCPPIgQ#O'IoA\^}Ya>"YWg϶식> !Tj??mlB.WL|1/rIkĚ@@x5q=oq!.j*! /גr\!4:AaaK&H##gq!AOwx΀V0#;彡xàY.T)aJ8Uz5]V G?P@4pta/IYTz2zQBpAAeD%Ė]kra۝irśmܲM6mMd( pR"%Y&O M`tF~[kЛpp TRy۾plw, `tVRИm%٢D~@V*IJRg&c4FLPYB8 zT?j$(vt!NYHxtUJWltH"V]rtOgH%}Љ6( \LW9#)Ɠ`HM35lAӳ0\D!Zl`>F$NX v,!B?jUMq'YNV kn8zFZ[t}fY W8LnR .`6)`>^&U8),ׁ7@' .5|RK"n~$-kLlt`Ba&55y-E{]n&3oۂ2}?x/e/^0ġCi3jK'laU rBSXϴ"N{&bw83JC(d zYBRKB@/o@'"K1nOk:s 3(\'d&}#Y+Y}z$EĦ {h//G|*hn[RuOlzx}nv˹YżyAƎ|ץKqŋ-V¾Ngik &V 9S :f\߰,DѽӺ783*'7 q1y] -{Ⱥ$ʺ!E_XL Pjj6b9 p 63l~eqvva~A5$uz5 xT4s[v~h_I{$g՚ 鹖MRn]0ƅ/B§@-kmMW8ģk/$gZMV7Ux{wgg]n$WڏQXuN+Y7 )at~| qYI40"kz6K#wooCIlZn{b.mbwnQ&k:I\ڲ'H>kVeA<oB93e(k:,5Y19լ$'D'JeMW_=Qt"l$k2:5]p\c[HWҬ靵Y*5Zz$YÏYIߺE="yo1ˬ|!&LA˔zT53S} YqjLLx$یv=fpq07|mL 4Vy|Tq1Qޟߎk̴cԋEd +:}Wk}ۉhsM˷08/\yjΘ ObsJM s 6CQ6f`@_~;!9ģGS 3Q|QQ W#0yFӱ |v>ìecǤ:]K.D #include #include // for NULL #include void ADprobExactInf(double *, int *, double *); void ADprobN(double *, int *, int *, double *); void ADprobApproxInf(double *, int *, double *); void ADtestR(double *, int *, double *, double *); static const R_CMethodDef CEntries[] = { {"ADprobExactInf", (DL_FUNC) &ADprobExactInf, 3}, {"ADprobN", (DL_FUNC) &ADprobN, 4}, {"ADprobApproxInf", (DL_FUNC) &ADprobApproxInf, 3}, {"ADtestR", (DL_FUNC) &ADtestR, 4}, {NULL, NULL, 0} }; static const R_CallMethodDef CallEntries[] = { {NULL, NULL, 0} }; void R_init_goftest(DllInfo *dll) { R_registerRoutines(dll, CEntries, CallEntries, NULL, NULL); R_useDynamicSymbols(dll, FALSE); } goftest/src/AnDarl.c0000755000176200001440000000732413257117256014043 0ustar liggesusers/* AnDarl.c $Revision: 1.2 $ $Date: 2018/03/29 08:07:08 $ Original C code by G. and J. Marsaglia R interface by Adrian Baddeley */ #include /* Anderson-Darling test for uniformity. Given an ordered set x_10. */ double adinf(double z) { if(z<2.) return ( exp(-1.2337141/z)/sqrt(z) )*( 2.00012+(.247105- (.0649821- (.0347962- (.011672-.00168691*z) *z)*z)*z)*z); /* max |error| < .000002 for z<2, (p=.90816...) */ return exp( -exp(1.0776-(2.30695-(.43424-(.082433-(.008056 -.0003146*z) *z)*z)*z)*z)); /* max |error|<.0000008 for 4.8) return (-130.2137+ (745.2337- (1705.091- (1950.646- (1116.360-255.7844*x)*x)*x)*x)*x)/n; c=.01265+.1757/n; if(x.8) { v=(-130.2137+(745.2337-(1705.091-(1950.646-(1116.360-255.7844*x) *x)*x)*x)*x)/n; return x+v; } c=.01265+.1757/n; if(x /* Anderson-Darling test for uniformity. Given an ordered set x_10. */ double adinf(double z) { if(z<2.) return ( exp(-1.2337141/z)/sqrt(z) )*( 2.00012+(.247105- (.0649821- (.0347962- (.011672-.00168691*z) *z)*z)*z)*z); /* max |error| < .000002 for z<2, (p=.90816...) */ return exp( -exp(1.0776-(2.30695-(.43424-(.082433-(.008056 -.0003146*z) *z)*z)*z)*z)); /* max |error|<.0000008 for 4.8) return (-130.2137+ (745.2337- (1705.091- (1950.646- (1116.360-255.7844*x)*x)*x)*x)*x)/n; c=.01265+.1757/n; if(x.8) { v=(-130.2137+(745.2337-(1705.091-(1950.646-(1116.360-255.7844*x) *x)*x)*x)*x)/n; return x+v; } c=.01265+.1757/n; if(x double ADinf(double z); /* A procedure for evaluating the limiting distribution of the Anderson-Darling statistic A_n=-n-(1/n)[ln(x_1(1-x_n)+3ln(x_2(1-x_{n-1})+5ln(x_3(1-x_{n-2})+... +(2n-1)ln(x_n(1-x_1))] where x_1infty} Pr[A_n150.) return 0.; a=2.22144146907918*exp(-t)/sqrt(t); b=3.93740248643060*2.*cPhi(sqrt(2*t));/* initialization requires cPhi */ /*if you have erfc(), replace 2*cPhi(sqrt(2*t)) with erfc(sqrt(t))*/ r=z*.125; f=a+b*r; for(i=1;i<200;i++) { c=((i-.5-t)*b+t*a)/i; a=b; b=c; r*=z/(8*i+8); if(fabs(r)<1e-40 || fabs(c)<1.e-40) return f; fnew=f+c*r; if(f==fnew) return f; f=fnew; } return f; } double ADinf(double z){ int j; double ad,adnew,r; if(z<.01) return 0.; /* avoids exponent limits; ADinf(.01)=.528e-52 */ r=1./z; ad=r*ADf(z,0); for(j=1;j<100;j++){ r*=(.5-j)/j; adnew=ad+(4*j+1)*r*ADf(z,j); if(ad==adnew) {return ad;} ad=adnew; } return ad; } /* Complementary normal distribution function cPhi(x) = integral from x to infinity of phi(x)=exp(-.5*t^2)/sqrt(2*pi) 13-15 digit accuracy for abs(x)<16. Stores R(0),R(2),R(4),...,R(16), with cPhi(x)=R(x)*phi(x), phi normal density, then uses Taylor series for R(z+h)=R(z)+hR'(z)+(1/2)h^2R''(z)+... with -10) ? s: 1-s); } } /* end i loop */ /* If not converged, return last estimate */ return ((x>0) ? s: 1-s); } /* R interface */ void ADprobExactInf(double *a, int *na, double *prob) { int i, m; m = *na; for(i = 0; i < m; i++) prob[i] = ADinf(a[i]); } @ goftest/src/ADinf.c0000755000176200001440000000622413115213326013645 0ustar liggesusers/* ADinf.c $Revision: 1.1 $ $Date: 2014/06/09 10:18:10 $ Original C code by G. and J. Marsaglia R interface by Adrian Baddeley */ #include double ADinf(double z); /* A procedure for evaluating the limiting distribution of the Anderson-Darling statistic A_n=-n-(1/n)[ln(x_1(1-x_n)+3ln(x_2(1-x_{n-1})+5ln(x_3(1-x_{n-2})+... +(2n-1)ln(x_n(1-x_1))] where x_1infty} Pr[A_n150.) return 0.; a=2.22144146907918*exp(-t)/sqrt(t); b=3.93740248643060*2.*cPhi(sqrt(2*t));/* initialization requires cPhi */ /*if you have erfc(), replace 2*cPhi(sqrt(2*t)) with erfc(sqrt(t))*/ r=z*.125; f=a+b*r; for(i=1;i<200;i++) { c=((i-.5-t)*b+t*a)/i; a=b; b=c; r*=z/(8*i+8); if(fabs(r)<1e-40 || fabs(c)<1.e-40) return f; fnew=f+c*r; if(f==fnew) return f; f=fnew; } return f; } double ADinf(double z){ int j; double ad,adnew,r; if(z<.01) return 0.; /* avoids exponent limits; ADinf(.01)=.528e-52 */ r=1./z; ad=r*ADf(z,0); for(j=1;j<100;j++){ r*=(.5-j)/j; adnew=ad+(4*j+1)*r*ADf(z,j); if(ad==adnew) {return ad;} ad=adnew; } return ad; } /* Complementary normal distribution function cPhi(x) = integral from x to infinity of phi(x)=exp(-.5*t^2)/sqrt(2*pi) 13-15 digit accuracy for abs(x)<16. Stores R(0),R(2),R(4),...,R(16), with cPhi(x)=R(x)*phi(x), phi normal density, then uses Taylor series for R(z+h)=R(z)+hR'(z)+(1/2)h^2R''(z)+... with -10) ? s: 1-s); } } /* end i loop */ /* If not converged, return last estimate */ return ((x>0) ? s: 1-s); } /* R interface */ void ADprobExactInf(double *a, int *na, double *prob) { int i, m; m = *na; for(i = 0; i < m; i++) prob[i] = ADinf(a[i]); } goftest/R/0000755000176200001440000000000014127521037012130 5ustar liggesusersgoftest/R/oldRCS/0000755000176200001440000000000013115213326013251 5ustar liggesusersgoftest/R/oldRCS/cvmtest.R,v0000755000176200001440000000551413115213326015333 0ustar liggesusershead 1.4; access; symbols; locks adrian:1.4; strict; comment @# @; 1.4 date 2014.06.24.02.13.27; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.24.01.54.26; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.08.11.32.51; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.08.11.05.58; author adrian; state Exp; branches; next ; desc @@ 1.4 log @polished output @ text @## ## cvmtest.R ## ## Cramer-von Mises test ## ## $Revision: 1.3 $ $Date: 2014/06/24 01:54:26 $ ## cvm.test <- function(x, null="punif", ..., nullname) { xname <- deparse(substitute(x)) nulltext <- deparse(substitute(null)) if(is.character(null)) nulltext <- null if(missing(nullname) || is.null(nullname)) { reco <- recogniseCdf(nulltext) nullname <- if(!is.null(reco)) reco else paste("distribution", sQuote(nulltext)) } stopifnot(is.numeric(x)) x <- as.vector(x) n <- length(x) F0 <- if(is.function(null)) null else if(is.character(null)) get(null, mode="function") else stop("Argument 'null' should be a function, or the name of a function") U <- F0(x, ...) if(any(U < 0 | U > 1)) stop("null distribution function returned values outside [0,1]") U <- sort(U) k <- seq_len(n) omega2 <- 1/(12 * n) + sum((U - (2*k - 1)/(2*n))^2) PVAL <- pCvM(omega2, n=n, lower.tail=FALSE) names(omega2) <- "omega2" METHOD <- c("Cramer-von Mises test of goodness-of-fit", paste("Null hypothesis:", nullname)) extras <- list(...) parnames <- intersect(names(extras), names(formals(F0))) if(length(parnames) > 0) { pars <- extras[parnames] pard <- character(0) for(i in seq_along(parnames)) pard[i] <- paste(parnames[i], "=", paste(pars[[i]], collapse=" ")) pard <- paste("with", ngettext(length(pard), "parameter", "parameters"), " ", paste(pard, collapse=", ")) METHOD <- c(METHOD, pard) } out <- list(statistic = omega2, p.value = PVAL, method = METHOD, data.name = xname) class(out) <- "htest" return(out) } @ 1.3 log @recognises standard distributions @ text @d6 1 a6 1 ## $Revision: 1.2 $ $Date: 2014/06/08 11:32:51 $ d33 14 a46 1 paste("to", nullname)) @ 1.2 log @minor @ text @d6 1 a6 1 ## $Revision: 1.1 $ $Date: 2014/06/08 11:05:58 $ d13 3 a15 2 if(missing(nullname)) nullname <- if(identical(null, "punif")) "uniform distribution" else d17 1 d30 1 a30 1 PVAL <- pcvm(omega2, n=n, lower.tail=FALSE) d32 2 a33 2 METHOD <- paste("Cramer-von Mises test of", nullname) ALTERN <- paste("Not the", nullname) a35 1 alternative = ALTERN, @ 1.1 log @Initial revision @ text @d6 1 a6 1 ## $Revision$ $Date$ d10 2 a11 2 xname <- short.deparse(substitute(x)) nulltext <- short.deparse(substitute(null)) @ goftest/R/oldRCS/andarl.R,v0000755000176200001440000001241113115213326015101 0ustar liggesusershead 1.6; access; symbols; locks adrian:1.6; strict; comment @# @; 1.6 date 2014.06.24.02.12.20; author adrian; state Exp; branches; next 1.5; 1.5 date 2014.06.24.01.54.16; author adrian; state Exp; branches; next 1.4; 1.4 date 2014.06.09.05.07.09; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.09.05.02.30; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.09.04.34.57; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.09.04.26.35; author adrian; state Exp; branches; next ; desc @@ 1.6 log @polished output @ text @## ## andarl.R ## ## Anderson-Darling test and null distribution ## ## $Revision: 1.5 $ $Date: 2014/06/24 01:54:16 $ ## ad.test <- function(x, null="punif", ..., nullname) { xname <- deparse(substitute(x)) nulltext <- deparse(substitute(null)) if(is.character(null)) nulltext <- null if(missing(nullname) || is.null(nullname)) { reco <- recogniseCdf(nulltext) nullname <- if(!is.null(reco)) reco else paste("distribution", sQuote(nulltext)) } stopifnot(is.numeric(x)) x <- as.vector(x) n <- length(x) F0 <- if(is.function(null)) null else if(is.character(null)) get(null, mode="function") else stop("Argument 'null' should be a function, or the name of a function") U <- F0(x, ...) if(any(U < 0 | U > 1)) stop("null distribution function returned values outside [0,1]") U <- sort(U) k <- seq_len(n) ## call Marsaglia C code z <- .C("ADtestR", x = as.double(U), n = as.integer(n), adstat = as.double(numeric(1)), pvalue = as.double(numeric(1)) ) STATISTIC <- z$adstat names(STATISTIC) <- "An" PVAL <- z$pvalue METHOD <- c("Anderson-Darling test of goodness-of-fit", paste("Null hypothesis:", nullname)) extras <- list(...) parnames <- intersect(names(extras), names(formals(F0))) if(length(parnames) > 0) { pars <- extras[parnames] pard <- character(0) for(i in seq_along(parnames)) pard[i] <- paste(parnames[i], "=", paste(pars[[i]], collapse=" ")) pard <- paste("with", ngettext(length(pard), "parameter", "parameters"), " ", paste(pard, collapse=", ")) METHOD <- c(METHOD, pard) } out <- list(statistic = STATISTIC, p.value = PVAL, method = METHOD, data.name = xname) class(out) <- "htest" return(out) } pAD <- function(q, n=Inf, lower.tail=TRUE, fast=TRUE) { q <- as.numeric(q) p <- rep(NA_real_, length(q)) if(any(ones <- is.infinite(q) & (q == Inf))) p[ones] <- 1 if(any(zeroes <- (is.finite(q) & q <= 0) | (is.infinite(q) & (q == -Inf)))) p[zeroes] <- 0 ok <- is.finite(q) & (q > 0) nok <- sum(ok) if(nok > 0) { if(is.finite(n)) { z <- .C("ADprobN", a = as.double(q[ok]), na = as.integer(nok), nsample = as.integer(n), prob = as.double(numeric(nok)) ) p[ok] <- z$prob } else if(fast) { ## fast version adinf() z <- .C("ADprobApproxInf", a = as.double(q[ok]), na = as.integer(nok), prob = as.double(numeric(nok)) ) p[ok] <- z$prob } else { ## slow, accurate version ADinf() z <- .C("ADprobExactInf", a = as.double(q[ok]), na = as.integer(nok), prob = as.double(numeric(nok)) ) p[ok] <- z$prob } } if(!lower.tail) p <- 1 - p return(p) } qAD <- local({ f <- function(x, N, P, Fast) { pAD(x, N, fast=Fast) - P } qAD <- function(p, n=Inf, lower.tail=TRUE, fast=TRUE) { ## quantiles of null distribution of Anderson-Darling test statistic stopifnot(all(p >= 0)) stopifnot(all(p <= 1)) if(!lower.tail) p <- 1-p ans <- rep(NA_real_, length(p)) for(i in which(p >= 0 & p < 1)) ans[i] <- uniroot(f, c(0, 1), N=n, P=p[i], Fast=fast, extendInt="up")$root return(ans) } qAD }) @ 1.5 log @recognises standard distributions @ text @d6 1 a6 1 ## $Revision: 1.4 $ $Date: 2014/06/09 05:07:09 $ d39 15 a53 2 METHOD <- paste("Anderson-Darling test of", nullname) ALTERN <- paste("Not the", nullname) a55 1 alternative = ALTERN, @ 1.4 log @tweak @ text @d6 1 a6 1 ## $Revision: 1.3 $ $Date: 2014/06/09 05:02:30 $ d13 3 a15 2 if(missing(nullname)) nullname <- if(identical(null, "punif")) "uniform distribution" else d17 1 d50 1 a50 1 pAD <- function(q, n=Inf, lower.tail=TRUE) { d68 8 d77 2 a78 1 z <- .C("ADprobInf", d85 1 d94 2 a95 2 f <- function(x, N, P) { pAD(x, N) - P d98 1 a98 1 qAD <- function(p, n=Inf, lower.tail=TRUE) { d105 1 a105 1 ans[i] <- uniroot(f, c(0, 1), N=n, P=p[i], extendInt="up")$root @ 1.3 log @buglet fix @ text @d6 1 a6 1 ## $Revision: 1.2 $ $Date: 2014/06/09 04:34:57 $ d35 1 d91 2 a92 1 for(i in which(p > 0 & p < 1)) @ 1.2 log @tweaked @ text @d6 1 a6 1 ## $Revision: 1.1 $ $Date: 2014/06/09 04:26:35 $ d52 1 a52 1 if(any(zeroes <- (is.finite(q) & q < 0) | (is.infinite(q) & (q == -Inf)))) d54 1 a54 1 ok <- is.finite(q) & (q >= 0) @ 1.1 log @Initial revision @ text @d6 1 a6 1 ## $Revision$ $Date$ d47 1 a47 1 pAnDarl <- function(q, n=Inf, lower.tail=TRUE) { d78 23 @ goftest/R/oldRCS/recog.R,v0000755000176200001440000000420313115213326014737 0ustar liggesusershead 1.4; access; symbols; locks adrian:1.4; strict; comment @# @; 1.4 date 2014.06.24.02.13.35; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.24.01.55.53; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.24.01.49.05; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.24.01.48.14; author adrian; state Exp; branches; next ; desc @@ 1.4 log @neatened @ text @## recog.R ## ## $Revision: 1.3 $ $Date: 2014/06/24 01:55:53 $ ## recogniseCdf <- function(s="punif") { if(!is.character(s) || length(s) != 1) return(NULL) if(nchar(s) <= 1 || substr(s,1,1) != "p") return(NULL) root <- substr(s, 2, nchar(s)) a <- switch(root, beta = "beta", binom = "binomial", birthday = "birthday coincidence", cauchy = "Cauchy", chisq = "chi-squared", exp = "exponential", f = "F", gamma = "Gamma", geom = "geometric", hyper = "hypergeometric", lnorm = "log-normal", logis = "logistic", nbinom = "negative binomial", norm = "Normal", pois = "Poisson", t = "Student's t", tukey = "Tukey (Studentized range)", unif = "uniform", weibull = "Weibull", NULL) if(!is.null(a)) return(paste(a, "distribution")) b <- switch(root, AD = "Anderson-Darling", CvM = "Cramer-von Mises", wilcox = "Wilcoxon Rank Sum", NULL) if(!is.null(b)) return(paste("null distribution of", b, "Test Statistic")) return(NULL) } @ 1.3 log @minor @ text @d3 1 a3 1 ## $Revision: 1.2 $ $Date: 2014/06/24 01:49:05 $ d6 1 a6 1 recogniseCdf <- function(s) { @ 1.2 log @bug fix @ text @d3 1 a3 1 ## $Revision: 1.1 $ $Date: 2014/06/24 01:48:14 $ d35 1 a35 1 CvM = "Cramer-von Mises" @ 1.1 log @Initial revision @ text @d3 1 a3 1 ## $Revision$ $Date$ d8 1 a8 1 if(substr(s,1,1) != "p" || nchar(s) > 1) return(NULL) @ goftest/R/oldRCS/cramer.R,v0000755000176200001440000001015413115213326015113 0ustar liggesusershead 1.2; access; symbols; locks adrian:1.2; strict; comment @# @; 1.2 date 2014.06.09.04.34.49; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.08.10.20.20; author adrian; state Exp; branches; next ; desc @@ 1.2 log @renamed @ text @## ## cramer.R ## ## Distribution of the Cramer-Von Mises test statistic ## ## $Revision: 1.1 $ $Date: 2014/06/08 10:20:20 $ ## ## .................................................................. ## ## From Matlab code written by Julian Faraway (faraway@@umich.edu) ## Translated to R by Adrian Baddeley ## ## Reference: S. Csorgo and J.J. Faraway, ## The exact and asymptotic distributions of Cramer-von Mises statistics ## Journal of the Royal Statistical Society, Series B ## 58 (1996) 221-234. ## pCvM <- local({ ## all functions are vectorised D2 <- function(x) { z <- (x^2)/4 b <- besselK(x=z, nu=1/4) + besselK(x=z, nu=3/4) b * sqrt((x^3)/(8*pi)) } D3 <- function(x) { z <- (x^2)/4 b <- 2*besselK(z, nu=1/4) + 3*besselK(z, nu=3/4) - besselK(z, nu=5/4) b * sqrt((x^5)/(32 * pi)) } ED2 <- function(x) { exp(-(x^2)/4) * D2(x) } ED3 <- function(x) { exp(-(x^2)/4) * D3(x) } Ak <- function(k, x) { twosqrtx <- 2 * sqrt(x) x34 <- x^(3/4) x54 <- x^(5/4) (2*k+1)*gamma(k+1/2)*ED2((4*k+3)/twosqrtx)/(9*x34) + gamma(k+1/2)*ED3((4*k+1)/twosqrtx)/(72*x54) + 2*(2*k+3)*gamma(k+3/2)*ED3((4*k+5)/twosqrtx)/(12*x54) + 7*(2*k+1)*gamma(k+1/2)*ED2((4*k+1)/twosqrtx)/(144*x34) + 7*(2*k+1)*gamma(k+1/2)*ED2((4*k+5)/twosqrtx)/(144*x34) } psi1 <- function(x) { ## Leading term in expansion of small-sample cdf of Cramer-Von Mises m <- length(x) tot <- numeric(m) active <- rep(TRUE, m) for(k in 0:20) { z <- -Ak(k,x[active])/(pi*factorial(k)) tot[active] <- tot[active] + z active[active] <- (abs(z) >= 1e-7) if(!any(active)) break } return(tot + Vinf(x)/12) } Vinf <- function(x) { ## cdf of asymptotic distribution of Cramer-von Mises m <- length(x) tot <- numeric(m) active <- rep(TRUE, m) for(k in 0:10) { q <- (4*k+1)^2/(16*x[active]) z <- ((-1)^k)*choose(-1/2,k)*sqrt(4*k+1)* exp(-q)*besselK(q, nu=1/4)/sqrt(x[active]) tot[active] <- tot[active] + z active[active] <- (abs(z) >= 1e-7) if(!any(active)) break } return(tot/pi) } Vn <- function(x, n) { ## cdf of small-sample distribution of Cramer-von Mises statistic ## First order approximation, Csorgo and Faraway equation (1.8) Vinf(x) + psi1(x)/n } pCvM <- function(q, n=Inf, lower.tail=TRUE) { ## cdf of null distribution of Cramer-von Mises test statistic if(is.finite(n)) { lower <- 1/(12 * n) upper <- n/3 } else { lower <- 0 upper <- Inf } m <- length(q) p <- numeric(m) unknown <- rep(TRUE, m) if(any(zeroes <- (q <= lower))) { p[zeroes] <- 0 unknown[zeroes] <- FALSE } if(any(ones <- (q >= upper))) { p[ones] <- 1 unknown[ones] <- FALSE } if(any(unknown)) p[unknown] <- if(is.infinite(n)) Vinf(q[unknown]) else Vn(q[unknown], n) return(if(lower.tail) p else 1-p) } pCvM }) qCvM <- local({ f <- function(x, N, P) { pCvM(x, N) - P } qCvM <- function(p, n=Inf, lower.tail=TRUE) { ## quantiles of null distribution of Cramer-von Mises test statistic stopifnot(all(p >= 0)) stopifnot(all(p <= 1)) if(!lower.tail) p <- 1-p lower <- if(is.finite(n)) (1/(12 * n)) else 0 upper <- if(is.finite(n)) n/3 else Inf ans <- numeric(length(p)) ans[p == 0] <- lower ans[p == 1] <- upper for(i in which(p > 0 & p < 1)) ans[i] <- uniroot(f, c(lower, 1), N=n, P=p[i], extendInt="up")$root return(ans) } qCvM }) @ 1.1 log @Initial revision @ text @d6 1 a6 1 ## $Revision$ $Date$ d19 1 a19 1 pcramer <- local({ d85 1 a85 1 pcramer <- function(q, n=Inf, lower.tail=TRUE) { d110 1 a110 1 pcramer d113 1 a113 1 qcramer <- local({ d116 1 a116 1 pcramer(x, N) - P d119 1 a119 1 qcramer <- function(p, n=Inf, lower.tail=TRUE) { d134 1 a134 1 qcramer @ goftest/R/recog.R0000755000176200001440000000407513305705013013356 0ustar liggesusers## recog.R ## ## $Revision: 1.4 $ $Date: 2014/06/24 02:13:35 $ ## recogniseCdf <- function(s="punif") { if(!is.character(s) || length(s) != 1 || nchar(s) == 0) return(NULL) #' strip off the leading 'p' if present root <- if(substr(s,1,1) == "p") substr(s, 2, nchar(s)) else s a <- switch(root, beta = "beta", binom = "binomial", birthday = "birthday coincidence", cauchy = "Cauchy", chisq = "chi-squared", exp = "exponential", f = "F", gamma = "Gamma", geom = "geometric", hyper = "hypergeometric", lnorm = "log-normal", logis = "logistic", nbinom = "negative binomial", norm = "Normal", pois = "Poisson", t = "Student's t", tukey = "Tukey (Studentized range)", unif = "uniform", weibull = "Weibull", NULL) if(!is.null(a)) return(paste(a, "distribution")) b <- switch(root, AD = "Anderson-Darling", CvM = "Cramer-von Mises", wilcox = "Wilcoxon Rank Sum", NULL) if(!is.null(b)) return(paste("null distribution of", b, "Test Statistic")) return(NULL) } #' not exported getfunky <- function(fname) { a <- mget(fname, mode="function", ifnotfound=list(NULL), inherits=TRUE)[[1]] return(a) } getCdf <- function(s="punif", fatal=TRUE) { sname <- deparse(substitute(s), nlines=1L) if(is.function(s)) return(s) if(is.character(s) && length(s) == 1 && nchar(s) > 0) { #' first try adding a leading 'p' (to catch the case s="t") if(substr(s,1,1) != "p") { f <- getfunky(paste0("p", s)) if(is.function(f)) return(f) } f <- getfunky(s) if(is.function(f)) return(f) } if(fatal) stop(paste("Argument", sQuote(sname), "should be a function, or the name of a function"), call.=FALSE) return(NULL) } goftest/R/cvmtest.R0000755000176200001440000000435013306101303013731 0ustar liggesusers## ## cvmtest.R ## ## Cramer-von Mises test ## ## $Revision: 1.7 $ $Date: 2018/06/06 08:25:46 $ ## cvm.test <- function(x, null="punif", ..., estimated=FALSE, nullname) { xname <- deparse(substitute(x)) nulltext <- deparse(substitute(null)) if(is.character(null)) nulltext <- null if(missing(nullname) || is.null(nullname)) { reco <- recogniseCdf(nulltext) nullname <- if(!is.null(reco)) reco else paste("distribution", sQuote(nulltext)) } stopifnot(is.numeric(x)) x <- as.vector(x) F0 <- getCdf(null) U <- F0(x, ...) n <- length(U) if(any(U < 0 | U > 1)) stop("null distribution function returned values outside [0,1]") #' perform test if(!estimated || n <= 4) { #' simple null hypothesis z <- simpleCvMtest(U) ADJUST <- NULL } else { #' composite - use Braun (1980) m <- round(sqrt(n)) z <- braun(U, simpleCvMtest, m=m) ADJUST <- paste("Braun's adjustment using", m, "groups") } PVAL <- z$pvalue STATISTIC <- z$statistic names(STATISTIC) <- z$statname #' dress up METHOD <- c("Cramer-von Mises test of goodness-of-fit", ADJUST, paste("Null hypothesis:", nullname)) extras <- list(...) parnames <- intersect(names(extras), names(formals(F0))) if(length(parnames) > 0) { pars <- extras[parnames] pard <- character(0) for(i in seq_along(parnames)) pard[i] <- paste(parnames[i], "=", paste(pars[[i]], collapse=" ")) pard <- paste("with", ngettext(length(pard), "parameter", "parameters"), " ", paste(pard, collapse=", ")) METHOD <- c(METHOD, pard) } coda <- paste("Parameters assumed to", if(estimated) "have been estimated from data" else "be fixed") METHOD <- c(METHOD, coda) out <- list(statistic = STATISTIC, p.value = PVAL, method = METHOD, data.name = xname) class(out) <- "htest" return(out) } #' not exported simpleCvMtest <- function(U) { U <- sort(U) n <- length(U) k <- seq_len(n) omega2 <- 1/(12 * n) + sum((U - (2*k - 1)/(2*n))^2) pvalue <- pCvM(omega2, n=n, lower.tail=FALSE) return(list(statistic=omega2, pvalue=pvalue, statname="omega2")) } goftest/R/cramer.R0000755000176200001440000001114213567353154013540 0ustar liggesusers## ## cramer.R ## ## Distribution of the Cramer-Von Mises test statistic ## ## $Revision: 1.9 $ $Date: 2019/11/27 01:50:20 $ ## ## .................................................................. ## ## From Matlab code written by Julian Faraway (faraway@umich.edu) ## Translated to R by Adrian Baddeley ## ## Reference: S. Csorgo and J.J. Faraway, ## The exact and asymptotic distributions of Cramer-von Mises statistics ## Journal of the Royal Statistical Society, Series B ## 58 (1996) 221-234. ## pCvM <- local({ ## all functions are vectorised D2 <- function(x) { z <- (x^2)/4 b <- besselK(x=z, nu=1/4) + besselK(x=z, nu=3/4) b * sqrt((x^3)/(8*pi)) } D3 <- function(x) { z <- (x^2)/4 b <- 2*besselK(z, nu=1/4) + 3*besselK(z, nu=3/4) - besselK(z, nu=5/4) b * sqrt((x^5)/(32 * pi)) } ED2 <- function(x) { exp(-(x^2)/4) * D2(x) } ED3 <- function(x) { exp(-(x^2)/4) * D3(x) } Ak <- function(k, x) { #' original code (transliterated from Matlab) for reference twosqrtx <- 2 * sqrt(x) x34 <- x^(3/4) x54 <- x^(5/4) (2*k+1)*gamma(k+1/2)*ED2((4*k+3)/twosqrtx)/(9*x34) + gamma(k+1/2)*ED3((4*k+1)/twosqrtx)/(72*x54) + 2*(2*k+3)*gamma(k+3/2)*ED3((4*k+5)/twosqrtx)/(12*x54) + 7*(2*k+1)*gamma(k+1/2)*ED2((4*k+1)/twosqrtx)/(144*x34) + 7*(2*k+1)*gamma(k+1/2)*ED2((4*k+5)/twosqrtx)/(144*x34) } AkOnFk <- function(k, x) { #' calculates A(k, x)/factorial(k) #' Adrian Baddeley, 26 nov 2019 twosqrtx <- 2 * sqrt(x) fk1x <- (4*k+1)/twosqrtx fk3x <- (4*k+3)/twosqrtx fk5x <- (4*k+5)/twosqrtx x34 <- x^(3/4) x54 <- x^(5/4) #'evaluate gamma(k+1/2)/factorial(k) = gamma(k+1/2)/gamma(k+1) gf <- if(k < 100) { gamma(k+1/2)/factorial(k) } else if(k <= 1e15) { exp(lgamma(k+1/2)-lgamma(k+1)) } else exp(-10*k) gf * ( ED3(fk1x)/(72*x54) + (2*k+1) * ( ED2(fk3x)/(9*x34) + (2*k+3)*ED3(fk5x)/(12*x54) + 7*(ED2(fk1x)+ED2(fk5x))/(144*x34) ) ) } psi1 <- function(x) { ## Leading term in expansion of small-sample cdf of Cramer-Von Mises m <- length(x) tot <- numeric(m) active <- rep(TRUE, m) for(k in 0:200) { ## WAS: z <- -Ak(k,x[active])/(pi*factorial(k)) z <- -AkOnFk(k,x[active])/pi tot[active] <- tot[active] + z active[active] <- (abs(z) >= 1e-9) if((k > 20) && (ok <- !any(active))) break } if(!ok) warning("Series did not converge after 200 iterations (small sample cdf)", call.=FALSE) return(tot + Vinf(x)/12) } Vinf <- function(x) { ## cdf of asymptotic distribution of Cramer-von Mises m <- length(x) tot <- numeric(m) active <- rep(TRUE, m) for(k in 0:200) { q <- (4*k+1)^2/(16*x[active]) z <- ((-1)^k)*choose(-1/2,k)*sqrt(4*k+1)* exp(-q)*besselK(q, nu=1/4)/sqrt(x[active]) tot[active] <- tot[active] + z active[active] <- (abs(z) >= 1e-9) if((k > 10) && (ok <- !any(active))) break } if(!ok) warning("Series did not converge after 200 iterations (asymptotic cdf)", call.=FALSE) return(tot/pi) } Vn <- function(x, n) { ## cdf of small-sample distribution of Cramer-von Mises statistic ## First order approximation, Csorgo and Faraway equation (1.8) Vinf(x) + psi1(x)/n } pCvM <- function(q, n=Inf, lower.tail=TRUE) { ## cdf of null distribution of Cramer-von Mises test statistic nn <- min(100, n) lower <- 1/(12 * nn) upper <- nn/3 m <- length(q) p <- numeric(m) unknown <- rep(TRUE, m) if(any(zeroes <- (q <= lower))) { p[zeroes] <- 0 unknown[zeroes] <- FALSE } if(any(ones <- (q >= upper))) { p[ones] <- 1 unknown[ones] <- FALSE } if(any(unknown)) p[unknown] <- if(is.infinite(n)) Vinf(q[unknown]) else Vn(q[unknown], n) p[p < 2e-10] <- 0 p[(1-p) < 2e-10] <- 1 return(if(lower.tail) p else 1-p) } pCvM }) qCvM <- local({ f <- function(x, N, P) { pCvM(x, N) - P } qCvM <- function(p, n=Inf, lower.tail=TRUE) { ## quantiles of null distribution of Cramer-von Mises test statistic stopifnot(all(p >= 0)) stopifnot(all(p <= 1)) if(!lower.tail) p <- 1-p lower <- if(is.finite(n)) (1/(12 * n)) else 0 upper <- if(is.finite(n)) n/3 else Inf ans <- numeric(length(p)) small <- (p <= 2e-10) large <- (1-p <= 2e-10) ans[small] <- lower ans[large] <- upper for(i in which(!small & !large)) ans[i] <- uniroot(f, c(lower, 1), N=n, P=p[i], extendInt="up")$root return(ans) } qCvM }) goftest/R/braun.R0000644000176200001440000000130113306100267013351 0ustar liggesusers#' #' braun.R #' #' Braun (1980) method for composite null hypothesis #' braun <- function(U, simpletest, m) { n <- length(U) if(n < 2 * m) stop("Unsufficient data for Braun's method") #' split data into m groups group <- factor(sample(seq_len(n) %% m)) #' apply the simple-null test to each subset zz <- by(data=U, INDICES=group, FUN=simpletest, simplify=FALSE) statistics <- sapply(zz, getElement, "statistic") pvalues <- sapply(zz, getElement, "pvalue") statname <- zz[[1]]$statname #' combine statistic <- max(statistics) pvalue <- 1 - (1 - min(pvalues))^m statname <- paste0(statname, "max") return(list(statistic=statistic, pvalue=pvalue, statname=statname)) } goftest/R/andarl.R0000755000176200001440000001000013306101334013476 0ustar liggesusers## ## andarl.R ## ## Anderson-Darling test and null distribution ## ## $Revision: 1.10 $ $Date: 2018/06/06 08:25:51 $ ## ad.test <- function(x, null="punif", ..., estimated=FALSE, nullname) { xname <- deparse(substitute(x)) nulltext <- deparse(substitute(null)) if(is.character(null)) nulltext <- null if(missing(nullname) || is.null(nullname)) { reco <- recogniseCdf(nulltext) nullname <- if(!is.null(reco)) reco else paste("distribution", sQuote(nulltext)) } stopifnot(is.numeric(x)) x <- as.vector(x) n <- length(x) F0 <- getCdf(null) U <- F0(x, ...) if(any(U < 0 | U > 1)) stop("null distribution function returned values outside [0,1]") #' perform test if(!estimated || n <= 4) { #' simple null hypothesis z <- simpleADtest(U) ADJUST <- NULL } else { #' composite - use Braun (1980) m <- round(sqrt(n)) z <- braun(U, simpleADtest, m=m) ADJUST <- paste("Braun's adjustment using", m, "groups") } PVAL <- z$pvalue STATISTIC <- z$statistic names(STATISTIC) <- z$statname #' dress up METHOD <- c("Anderson-Darling test of goodness-of-fit", ADJUST, paste("Null hypothesis:", nullname)) extras <- list(...) parnames <- intersect(names(extras), names(formals(F0))) if(length(parnames) > 0) { pars <- extras[parnames] pard <- character(length(parnames)) for(i in seq_along(parnames)) pard[i] <- paste(parnames[i], "=", paste(pars[[i]], collapse=" ")) pard <- paste("with", ngettext(length(pard), "parameter", "parameters"), " ", paste(pard, collapse=", ")) METHOD <- c(METHOD, pard) } coda <- paste("Parameters assumed to", if(estimated) "have been estimated from data" else "be fixed") METHOD <- c(METHOD, coda) out <- list(statistic = STATISTIC, p.value = PVAL, method = METHOD, data.name = xname) class(out) <- "htest" return(out) } simpleADtest <- function(U) { ## Internal: call Marsaglia C code U <- sort(U) n <- length(U) z <- .C(CgofADtestR, x = as.double(U), n = as.integer(n), adstat = as.double(numeric(1)), pvalue = as.double(numeric(1)), PACKAGE="goftest" ) return(list(statistic=z$adstat, pvalue=z$pvalue, statname="An")) } pAD <- function(q, n=Inf, lower.tail=TRUE, fast=TRUE) { q <- as.numeric(q) p <- rep(NA_real_, length(q)) if(any(ones <- is.infinite(q) & (q == Inf))) p[ones] <- 1 if(any(zeroes <- (is.finite(q) & q <= 0) | (is.infinite(q) & (q == -Inf)))) p[zeroes] <- 0 ok <- is.finite(q) & (q > 0) nok <- sum(ok) if(nok > 0) { if(is.finite(n)) { z <- .C(CgofADprobN, a = as.double(q[ok]), na = as.integer(nok), nsample = as.integer(n), prob = as.double(numeric(nok)), PACKAGE="goftest") p[ok] <- z$prob } else if(fast) { ## fast version adinf() z <- .C(CgofADprobApproxInf, a = as.double(q[ok]), na = as.integer(nok), prob = as.double(numeric(nok)), PACKAGE="goftest") p[ok] <- z$prob } else { ## slow, accurate version ADinf() z <- .C(CgofADprobExactInf, a = as.double(q[ok]), na = as.integer(nok), prob = as.double(numeric(nok)), PACKAGE="goftest") p[ok] <- z$prob } } if(!lower.tail) p <- 1 - p return(p) } qAD <- local({ f <- function(x, N, P, Fast) { pAD(x, N, fast=Fast) - P } qAD <- function(p, n=Inf, lower.tail=TRUE, fast=TRUE) { ## quantiles of null distribution of Anderson-Darling test statistic stopifnot(all(p >= 0)) stopifnot(all(p <= 1)) if(!lower.tail) p <- 1-p ans <- rep(NA_real_, length(p)) for(i in which(p >= 0 & p < 1)) ans[i] <- uniroot(f, c(0, 1), N=n, P=p[i], Fast=fast, extendInt="up")$root return(ans) } qAD }) goftest/R/RCS/0000755000176200001440000000000013567353154012571 5ustar liggesusersgoftest/R/RCS/cvmtest.R,v0000755000176200001440000001125213305715213014633 0ustar liggesusershead 1.7; access; symbols; locks adrian:1.7; strict; comment @# @; 1.7 date 2018.06.06.08.25.46; author adrian; state Exp; branches; next 1.6; 1.6 date 2018.06.06.08.09.39; author adrian; state Exp; branches; next 1.5; 1.5 date 2018.06.06.08.05.35; author adrian; state Exp; branches; next 1.4; 1.4 date 2014.06.24.02.13.27; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.24.01.54.26; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.08.11.32.51; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.08.11.05.58; author adrian; state Exp; branches; next ; desc @@ 1.7 log @Summary: tweak @ text @## ## cvmtest.R ## ## Cramer-von Mises test ## ## $Revision: 1.6 $ $Date: 2018/06/06 08:09:39 $ ## cvm.test <- function(x, null="punif", ..., estimated=FALSE, nullname) { xname <- deparse(substitute(x)) nulltext <- deparse(substitute(null)) if(is.character(null)) nulltext <- null if(missing(nullname) || is.null(nullname)) { reco <- recogniseCdf(nulltext) nullname <- if(!is.null(reco)) reco else paste("distribution", sQuote(nulltext)) } stopifnot(is.numeric(x)) x <- as.vector(x) n <- length(x) F0 <- getCdf(null) U <- F0(x, ...) if(any(U < 0 | U > 1)) stop("null distribution function returned values outside [0,1]") if(!estimated || n <= 4) { #' simple null hypothesis z <- do.goftest.CvM(U) PVAL <- z$pvalue STATISTIC <- z$omega2 names(STATISTIC) <- "omega2" } else { #' composite - use Braun (1980) first <- sample(n, ceiling(n/2), replace=TRUE) z1 <- do.goftest.CvM(U[first]) z2 <- do.goftest.CvM(U[-first]) PVAL <- 1 - (1 - z1$pvalue) * (1 - z2$pvalue) STATISTIC <- max(z1$omega2, z2$omega2) names(STATISTIC) <- "omega2max" } METHOD <- c("Cramer-von Mises test of goodness-of-fit", if(estimated) "(with Braun's adjustment)" else NULL, paste("Null hypothesis:", nullname)) extras <- list(...) parnames <- intersect(names(extras), names(formals(F0))) if(length(parnames) > 0) { pars <- extras[parnames] pard <- character(0) for(i in seq_along(parnames)) pard[i] <- paste(parnames[i], "=", paste(pars[[i]], collapse=" ")) pard <- paste("with", ngettext(length(pard), "parameter", "parameters"), " ", paste(pard, collapse=", ")) METHOD <- c(METHOD, pard) } coda <- paste("Parameters assumed to", if(estimated) "have been estimated from data" else "be fixed") METHOD <- c(METHOD, coda) out <- list(statistic = STATISTIC, p.value = PVAL, method = METHOD, data.name = xname) class(out) <- "htest" return(out) } #' not exported do.goftest.CvM <- function(U) { U <- sort(U) n <- length(U) k <- seq_len(n) omega2 <- 1/(12 * n) + sum((U - (2*k - 1)/(2*n))^2) pvalue <- pCvM(omega2, n=n, lower.tail=FALSE) return(list(omega2=omega2, pvalue=pvalue)) } @ 1.6 log @Summary: d'oh @ text @d6 1 a6 1 ## $Revision: 1.5 $ $Date: 2018/06/06 08:05:35 $ d58 2 a59 2 coda <- paste("Parameters assumed to be", if(estimated) "estimated from data" else "fixed") @ 1.5 log @Summary: handles composite case using Braun 1980 @ text @d6 1 a6 1 ## $Revision: 1.4 $ $Date: 2014/06/24 02:13:27 $ d59 1 a59 1 if(!estimated) "estimated from data" else "fixed") d62 1 a62 1 out <- list(statistic = omega2, @ 1.4 log @polished output @ text @d6 1 a6 1 ## $Revision: 1.3 $ $Date: 2014/06/24 01:54:26 $ d9 1 a9 1 cvm.test <- function(x, null="punif", ..., nullname) { d21 1 a21 3 F0 <- if(is.function(null)) null else if(is.character(null)) get(null, mode="function") else stop("Argument 'null' should be a function, or the name of a function") d25 15 a39 5 U <- sort(U) k <- seq_len(n) omega2 <- 1/(12 * n) + sum((U - (2*k - 1)/(2*n))^2) PVAL <- pCvM(omega2, n=n, lower.tail=FALSE) names(omega2) <- "omega2" d41 1 d43 1 d57 5 d70 10 @ 1.3 log @recognises standard distributions @ text @d6 1 a6 1 ## $Revision: 1.2 $ $Date: 2014/06/08 11:32:51 $ d33 14 a46 1 paste("to", nullname)) @ 1.2 log @minor @ text @d6 1 a6 1 ## $Revision: 1.1 $ $Date: 2014/06/08 11:05:58 $ d13 3 a15 2 if(missing(nullname)) nullname <- if(identical(null, "punif")) "uniform distribution" else d17 1 d30 1 a30 1 PVAL <- pcvm(omega2, n=n, lower.tail=FALSE) d32 2 a33 2 METHOD <- paste("Cramer-von Mises test of", nullname) ALTERN <- paste("Not the", nullname) a35 1 alternative = ALTERN, @ 1.1 log @Initial revision @ text @d6 1 a6 1 ## $Revision$ $Date$ d10 2 a11 2 xname <- short.deparse(substitute(x)) nulltext <- short.deparse(substitute(null)) @ goftest/R/RCS/andarl.R,v0000755000176200001440000001756313305715220014420 0ustar liggesusershead 1.10; access; symbols; locks adrian:1.10; strict; comment @# @; 1.10 date 2018.06.06.08.25.51; author adrian; state Exp; branches; next 1.9; 1.9 date 2018.06.06.08.10.11; author adrian; state Exp; branches; next 1.8; 1.8 date 2018.06.06.08.05.21; author adrian; state Exp; branches; next 1.7; 1.7 date 2018.03.29.13.51.49; author adrian; state Exp; branches; next 1.6; 1.6 date 2014.06.24.02.12.20; author adrian; state Exp; branches; next 1.5; 1.5 date 2014.06.24.01.54.16; author adrian; state Exp; branches; next 1.4; 1.4 date 2014.06.09.05.07.09; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.09.05.02.30; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.09.04.34.57; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.09.04.26.35; author adrian; state Exp; branches; next ; desc @@ 1.10 log @Summary: tweak @ text @## ## andarl.R ## ## Anderson-Darling test and null distribution ## ## $Revision: 1.9 $ $Date: 2018/06/06 08:10:11 $ ## ad.test <- function(x, null="punif", ..., estimated=FALSE, nullname) { xname <- deparse(substitute(x)) nulltext <- deparse(substitute(null)) if(is.character(null)) nulltext <- null if(missing(nullname) || is.null(nullname)) { reco <- recogniseCdf(nulltext) nullname <- if(!is.null(reco)) reco else paste("distribution", sQuote(nulltext)) } stopifnot(is.numeric(x)) x <- as.vector(x) n <- length(x) F0 <- getCdf(null) U <- F0(x, ...) if(any(U < 0 | U > 1)) stop("null distribution function returned values outside [0,1]") if(!estimated || n <= 4) { #' simple null hypothesis z <- do.goftest.AD(U) PVAL <- z$pvalue STATISTIC <- z$adstat names(STATISTIC) <- "An" } else { #' composite - use Braun (1980) first <- sample(n, ceiling(n/2), replace=TRUE) z1 <- do.goftest.AD(U[first]) z2 <- do.goftest.AD(U[-first]) STATISTIC <- max(z1$adstat, z2$adstat) names(STATISTIC) <- "AnMax" PVAL <- 1 - (1 - z1$pvalue) * (1 - z2$pvalue) } METHOD <- c("Anderson-Darling test of goodness-of-fit", if(estimated) "(with Braun's adjustment)" else NULL, paste("Null hypothesis:", nullname)) extras <- list(...) parnames <- intersect(names(extras), names(formals(F0))) if(length(parnames) > 0) { pars <- extras[parnames] pard <- character(length(parnames)) for(i in seq_along(parnames)) pard[i] <- paste(parnames[i], "=", paste(pars[[i]], collapse=" ")) pard <- paste("with", ngettext(length(pard), "parameter", "parameters"), " ", paste(pard, collapse=", ")) METHOD <- c(METHOD, pard) } coda <- paste("Parameters assumed to", if(estimated) "have been estimated from data" else "be fixed") METHOD <- c(METHOD, coda) out <- list(statistic = STATISTIC, p.value = PVAL, method = METHOD, data.name = xname) class(out) <- "htest" return(out) } do.goftest.AD <- function(U) { ## Internal: call Marsaglia C code U <- sort(U) n <- length(U) z <- .C(CgofADtestR, x = as.double(U), n = as.integer(n), adstat = as.double(numeric(1)), pvalue = as.double(numeric(1)), PACKAGE="goftest" ) return(z[c("adstat", "pvalue")]) } pAD <- function(q, n=Inf, lower.tail=TRUE, fast=TRUE) { q <- as.numeric(q) p <- rep(NA_real_, length(q)) if(any(ones <- is.infinite(q) & (q == Inf))) p[ones] <- 1 if(any(zeroes <- (is.finite(q) & q <= 0) | (is.infinite(q) & (q == -Inf)))) p[zeroes] <- 0 ok <- is.finite(q) & (q > 0) nok <- sum(ok) if(nok > 0) { if(is.finite(n)) { z <- .C(CgofADprobN, a = as.double(q[ok]), na = as.integer(nok), nsample = as.integer(n), prob = as.double(numeric(nok)), PACKAGE="goftest") p[ok] <- z$prob } else if(fast) { ## fast version adinf() z <- .C(CgofADprobApproxInf, a = as.double(q[ok]), na = as.integer(nok), prob = as.double(numeric(nok)), PACKAGE="goftest") p[ok] <- z$prob } else { ## slow, accurate version ADinf() z <- .C(CgofADprobExactInf, a = as.double(q[ok]), na = as.integer(nok), prob = as.double(numeric(nok)), PACKAGE="goftest") p[ok] <- z$prob } } if(!lower.tail) p <- 1 - p return(p) } qAD <- local({ f <- function(x, N, P, Fast) { pAD(x, N, fast=Fast) - P } qAD <- function(p, n=Inf, lower.tail=TRUE, fast=TRUE) { ## quantiles of null distribution of Anderson-Darling test statistic stopifnot(all(p >= 0)) stopifnot(all(p <= 1)) if(!lower.tail) p <- 1-p ans <- rep(NA_real_, length(p)) for(i in which(p >= 0 & p < 1)) ans[i] <- uniroot(f, c(0, 1), N=n, P=p[i], Fast=fast, extendInt="up")$root return(ans) } qAD }) @ 1.9 log @Summary: d'oh @ text @d6 1 a6 1 ## $Revision: 1.8 $ $Date: 2018/06/06 08:05:21 $ d58 2 a59 2 coda <- paste("Parameters assumed to be", if(estimated) "estimated from data" else "fixed") @ 1.8 log @Summary: handles composite case using Braun 1980 @ text @d6 1 a6 1 ## $Revision: 1.7 $ $Date: 2018/03/29 13:51:49 $ d59 1 a59 1 if(!estimated) "estimated from data" else "fixed") @ 1.7 log @Summary: removed unused variable @ text @d6 1 a6 1 ## $Revision: 1.6 $ $Date: 2014/06/24 02:12:20 $ d9 1 a9 1 ad.test <- function(x, null="punif", ..., nullname) { d21 1 a21 3 F0 <- if(is.function(null)) null else if(is.character(null)) get(null, mode="function") else stop("Argument 'null' should be a function, or the name of a function") d25 15 a39 12 U <- sort(U) ## call Marsaglia C code z <- .C(CgofADtestR, x = as.double(U), n = as.integer(n), adstat = as.double(numeric(1)), pvalue = as.double(numeric(1)), PACKAGE="goftest" ) STATISTIC <- z$adstat names(STATISTIC) <- "An" PVAL <- z$pvalue d41 1 d43 1 d48 1 a48 1 pard <- character(0) d57 5 d68 14 @ 1.6 log @polished output @ text @d6 1 a6 1 ## $Revision: 1.5 $ $Date: 2014/06/24 01:54:16 $ a27 1 k <- seq_len(n) d29 1 a29 1 z <- .C("ADtestR", d33 2 a34 1 pvalue = as.double(numeric(1)) d73 1 a73 1 z <- .C("ADprobN", d77 2 a78 2 prob = as.double(numeric(nok)) ) d82 1 a82 1 z <- .C("ADprobApproxInf", d85 2 a86 2 prob = as.double(numeric(nok)) ) d90 1 a90 1 z <- .C("ADprobExactInf", d93 2 a94 2 prob = as.double(numeric(nok)) ) @ 1.5 log @recognises standard distributions @ text @d6 1 a6 1 ## $Revision: 1.4 $ $Date: 2014/06/09 05:07:09 $ d39 15 a53 2 METHOD <- paste("Anderson-Darling test of", nullname) ALTERN <- paste("Not the", nullname) a55 1 alternative = ALTERN, @ 1.4 log @tweak @ text @d6 1 a6 1 ## $Revision: 1.3 $ $Date: 2014/06/09 05:02:30 $ d13 3 a15 2 if(missing(nullname)) nullname <- if(identical(null, "punif")) "uniform distribution" else d17 1 d50 1 a50 1 pAD <- function(q, n=Inf, lower.tail=TRUE) { d68 8 d77 2 a78 1 z <- .C("ADprobInf", d85 1 d94 2 a95 2 f <- function(x, N, P) { pAD(x, N) - P d98 1 a98 1 qAD <- function(p, n=Inf, lower.tail=TRUE) { d105 1 a105 1 ans[i] <- uniroot(f, c(0, 1), N=n, P=p[i], extendInt="up")$root @ 1.3 log @buglet fix @ text @d6 1 a6 1 ## $Revision: 1.2 $ $Date: 2014/06/09 04:34:57 $ d35 1 d91 2 a92 1 for(i in which(p > 0 & p < 1)) @ 1.2 log @tweaked @ text @d6 1 a6 1 ## $Revision: 1.1 $ $Date: 2014/06/09 04:26:35 $ d52 1 a52 1 if(any(zeroes <- (is.finite(q) & q < 0) | (is.infinite(q) & (q == -Inf)))) d54 1 a54 1 ok <- is.finite(q) & (q >= 0) @ 1.1 log @Initial revision @ text @d6 1 a6 1 ## $Revision$ $Date$ d47 1 a47 1 pAnDarl <- function(q, n=Inf, lower.tail=TRUE) { d78 23 @ goftest/R/RCS/recog.R,v0000755000176200001440000000420313065440240014241 0ustar liggesusershead 1.4; access; symbols; locks adrian:1.4; strict; comment @# @; 1.4 date 2014.06.24.02.13.35; author adrian; state Exp; branches; next 1.3; 1.3 date 2014.06.24.01.55.53; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.24.01.49.05; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.24.01.48.14; author adrian; state Exp; branches; next ; desc @@ 1.4 log @neatened @ text @## recog.R ## ## $Revision: 1.3 $ $Date: 2014/06/24 01:55:53 $ ## recogniseCdf <- function(s="punif") { if(!is.character(s) || length(s) != 1) return(NULL) if(nchar(s) <= 1 || substr(s,1,1) != "p") return(NULL) root <- substr(s, 2, nchar(s)) a <- switch(root, beta = "beta", binom = "binomial", birthday = "birthday coincidence", cauchy = "Cauchy", chisq = "chi-squared", exp = "exponential", f = "F", gamma = "Gamma", geom = "geometric", hyper = "hypergeometric", lnorm = "log-normal", logis = "logistic", nbinom = "negative binomial", norm = "Normal", pois = "Poisson", t = "Student's t", tukey = "Tukey (Studentized range)", unif = "uniform", weibull = "Weibull", NULL) if(!is.null(a)) return(paste(a, "distribution")) b <- switch(root, AD = "Anderson-Darling", CvM = "Cramer-von Mises", wilcox = "Wilcoxon Rank Sum", NULL) if(!is.null(b)) return(paste("null distribution of", b, "Test Statistic")) return(NULL) } @ 1.3 log @minor @ text @d3 1 a3 1 ## $Revision: 1.2 $ $Date: 2014/06/24 01:49:05 $ d6 1 a6 1 recogniseCdf <- function(s) { @ 1.2 log @bug fix @ text @d3 1 a3 1 ## $Revision: 1.1 $ $Date: 2014/06/24 01:48:14 $ d35 1 a35 1 CvM = "Cramer-von Mises" @ 1.1 log @Initial revision @ text @d3 1 a3 1 ## $Revision$ $Date$ d8 1 a8 1 if(substr(s,1,1) != "p" || nchar(s) > 1) return(NULL) @ goftest/R/RCS/cramer.R,v0000755000176200001440000001631713567353154014442 0ustar liggesusershead 1.9; access; symbols; locks adrian:1.9; strict; comment @# @; 1.9 date 2019.11.27.01.50.20; author adrian; state Exp; branches; next 1.8; 1.8 date 2019.11.26.04.05.19; author adrian; state Exp; branches; next 1.7; 1.7 date 2019.11.26.03.59.06; author adrian; state Exp; branches; next 1.6; 1.6 date 2019.11.26.03.55.16; author adrian; state Exp; branches; next 1.5; 1.5 date 2019.11.26.03.49.24; author adrian; state Exp; branches; next 1.4; 1.4 date 2019.11.26.03.31.49; author adrian; state Exp; branches; next 1.3; 1.3 date 2019.11.26.03.29.57; author adrian; state Exp; branches; next 1.2; 1.2 date 2014.06.09.04.34.49; author adrian; state Exp; branches; next 1.1; 1.1 date 2014.06.08.10.20.20; author adrian; state Exp; branches; next ; desc @@ 1.9 log @Summary: changed epsilon @ text @## ## cramer.R ## ## Distribution of the Cramer-Von Mises test statistic ## ## $Revision: 1.8 $ $Date: 2019/11/26 04:05:19 $ ## ## .................................................................. ## ## From Matlab code written by Julian Faraway (faraway@@umich.edu) ## Translated to R by Adrian Baddeley ## ## Reference: S. Csorgo and J.J. Faraway, ## The exact and asymptotic distributions of Cramer-von Mises statistics ## Journal of the Royal Statistical Society, Series B ## 58 (1996) 221-234. ## pCvM <- local({ ## all functions are vectorised D2 <- function(x) { z <- (x^2)/4 b <- besselK(x=z, nu=1/4) + besselK(x=z, nu=3/4) b * sqrt((x^3)/(8*pi)) } D3 <- function(x) { z <- (x^2)/4 b <- 2*besselK(z, nu=1/4) + 3*besselK(z, nu=3/4) - besselK(z, nu=5/4) b * sqrt((x^5)/(32 * pi)) } ED2 <- function(x) { exp(-(x^2)/4) * D2(x) } ED3 <- function(x) { exp(-(x^2)/4) * D3(x) } Ak <- function(k, x) { #' original code (transliterated from Matlab) for reference twosqrtx <- 2 * sqrt(x) x34 <- x^(3/4) x54 <- x^(5/4) (2*k+1)*gamma(k+1/2)*ED2((4*k+3)/twosqrtx)/(9*x34) + gamma(k+1/2)*ED3((4*k+1)/twosqrtx)/(72*x54) + 2*(2*k+3)*gamma(k+3/2)*ED3((4*k+5)/twosqrtx)/(12*x54) + 7*(2*k+1)*gamma(k+1/2)*ED2((4*k+1)/twosqrtx)/(144*x34) + 7*(2*k+1)*gamma(k+1/2)*ED2((4*k+5)/twosqrtx)/(144*x34) } AkOnFk <- function(k, x) { #' calculates A(k, x)/factorial(k) #' Adrian Baddeley, 26 nov 2019 twosqrtx <- 2 * sqrt(x) fk1x <- (4*k+1)/twosqrtx fk3x <- (4*k+3)/twosqrtx fk5x <- (4*k+5)/twosqrtx x34 <- x^(3/4) x54 <- x^(5/4) #'evaluate gamma(k+1/2)/factorial(k) = gamma(k+1/2)/gamma(k+1) gf <- if(k < 100) { gamma(k+1/2)/factorial(k) } else if(k <= 1e15) { exp(lgamma(k+1/2)-lgamma(k+1)) } else exp(-10*k) gf * ( ED3(fk1x)/(72*x54) + (2*k+1) * ( ED2(fk3x)/(9*x34) + (2*k+3)*ED3(fk5x)/(12*x54) + 7*(ED2(fk1x)+ED2(fk5x))/(144*x34) ) ) } psi1 <- function(x) { ## Leading term in expansion of small-sample cdf of Cramer-Von Mises m <- length(x) tot <- numeric(m) active <- rep(TRUE, m) for(k in 0:200) { ## WAS: z <- -Ak(k,x[active])/(pi*factorial(k)) z <- -AkOnFk(k,x[active])/pi tot[active] <- tot[active] + z active[active] <- (abs(z) >= 1e-9) if((k > 20) && (ok <- !any(active))) break } if(!ok) warning("Series did not converge after 200 iterations (small sample cdf)", call.=FALSE) return(tot + Vinf(x)/12) } Vinf <- function(x) { ## cdf of asymptotic distribution of Cramer-von Mises m <- length(x) tot <- numeric(m) active <- rep(TRUE, m) for(k in 0:200) { q <- (4*k+1)^2/(16*x[active]) z <- ((-1)^k)*choose(-1/2,k)*sqrt(4*k+1)* exp(-q)*besselK(q, nu=1/4)/sqrt(x[active]) tot[active] <- tot[active] + z active[active] <- (abs(z) >= 1e-9) if((k > 10) && (ok <- !any(active))) break } if(!ok) warning("Series did not converge after 200 iterations (asymptotic cdf)", call.=FALSE) return(tot/pi) } Vn <- function(x, n) { ## cdf of small-sample distribution of Cramer-von Mises statistic ## First order approximation, Csorgo and Faraway equation (1.8) Vinf(x) + psi1(x)/n } pCvM <- function(q, n=Inf, lower.tail=TRUE) { ## cdf of null distribution of Cramer-von Mises test statistic nn <- min(100, n) lower <- 1/(12 * nn) upper <- nn/3 m <- length(q) p <- numeric(m) unknown <- rep(TRUE, m) if(any(zeroes <- (q <= lower))) { p[zeroes] <- 0 unknown[zeroes] <- FALSE } if(any(ones <- (q >= upper))) { p[ones] <- 1 unknown[ones] <- FALSE } if(any(unknown)) p[unknown] <- if(is.infinite(n)) Vinf(q[unknown]) else Vn(q[unknown], n) p[p < 2e-10] <- 0 p[(1-p) < 2e-10] <- 1 return(if(lower.tail) p else 1-p) } pCvM }) qCvM <- local({ f <- function(x, N, P) { pCvM(x, N) - P } qCvM <- function(p, n=Inf, lower.tail=TRUE) { ## quantiles of null distribution of Cramer-von Mises test statistic stopifnot(all(p >= 0)) stopifnot(all(p <= 1)) if(!lower.tail) p <- 1-p lower <- if(is.finite(n)) (1/(12 * n)) else 0 upper <- if(is.finite(n)) n/3 else Inf ans <- numeric(length(p)) small <- (p <= 2e-10) large <- (1-p <= 2e-10) ans[small] <- lower ans[large] <- upper for(i in which(!small & !large)) ans[i] <- uniroot(f, c(lower, 1), N=n, P=p[i], extendInt="up")$root return(ans) } qCvM }) @ 1.8 log @Summary: more tweaks @ text @d6 1 a6 1 ## $Revision: 1.7 $ $Date: 2019/11/26 03:59:06 $ d136 2 a137 2 p[p < 2e-11] <- 0 p[(1-p) < 2e-11] <- 1 d158 2 a159 2 small <- (p <= 2e-11) large <- (1-p <= 2e-11) @ 1.7 log @Summary: more tweaks @ text @d6 1 a6 1 ## $Revision: 1.6 $ $Date: 2019/11/26 03:55:16 $ d85 1 a85 1 if(ok <- !any(active)) break d104 1 a104 1 if(ok <- !any(active)) break @ 1.6 log @Summary: more numerical stabilisation @ text @d6 1 a6 1 ## $Revision: 1.5 $ $Date: 2019/11/26 03:49:24 $ d158 5 a162 3 ans[p == 0] <- lower ans[p == 1] <- upper for(i in which(p > 0 & p < 1)) @ 1.5 log @Summary: more tweaks @ text @d6 1 a6 1 ## $Revision: 1.4 $ $Date: 2019/11/26 03:31:49 $ d136 2 @ 1.4 log @Summary: d'oh @ text @d6 1 a6 1 ## $Revision: 1.3 $ $Date: 2019/11/26 03:29:57 $ d84 1 a84 1 active[active] <- (abs(z) >= 1e-7) d103 1 a103 1 active[active] <- (abs(z) >= 1e-7) d120 3 a122 7 if(is.finite(n)) { lower <- 1/(12 * n) upper <- n/3 } else { lower <- 0 upper <- Inf } @ 1.3 log @Summary: bug fixes @ text @d6 1 a6 1 ## $Revision: 1.2 $ $Date: 2014/06/09 04:34:49 $ d71 1 @ 1.2 log @renamed @ text @d6 1 a6 1 ## $Revision: 1.1 $ $Date: 2014/06/08 10:20:20 $ d39 1 d50 24 d79 3 a81 2 for(k in 0:20) { z <- -Ak(k,x[active])/(pi*factorial(k)) d84 1 a84 1 if(!any(active)) break d86 3 d97 1 a97 1 for(k in 0:10) { d103 1 a103 1 if(!any(active)) break d105 3 @ 1.1 log @Initial revision @ text @d6 1 a6 1 ## $Revision$ $Date$ d19 1 a19 1 pcramer <- local({ d85 1 a85 1 pcramer <- function(q, n=Inf, lower.tail=TRUE) { d110 1 a110 1 pcramer d113 1 a113 1 qcramer <- local({ d116 1 a116 1 pcramer(x, N) - P d119 1 a119 1 qcramer <- function(p, n=Inf, lower.tail=TRUE) { d134 1 a134 1 qcramer @ goftest/MD50000644000176200001440000000316514127535702012250 0ustar liggesusers29111628fe1b2d270ef5df0c5ff3c6c5 *DESCRIPTION 15b924eea35078a33ce9a9597f352439 *NAMESPACE e45a60eb1d479da9631e0dd3a45d9ec1 *R/RCS/andarl.R,v d7a9c3d741437b87b272318a38e01fd7 *R/RCS/cramer.R,v f149bfb2b89d8c70e8807f2a5357ee48 *R/RCS/cvmtest.R,v 56ce831b8afd6f55b7f7180a470226c1 *R/RCS/recog.R,v 2d55fdb30747e83b2380d5bf777b1898 *R/andarl.R e967698c8e63feae74fefd362e996d6d *R/braun.R e2fbe5802aff0bcb1ff080f9ce8dbf65 *R/cramer.R 723adf9bac297d8d04daba9b6df77847 *R/cvmtest.R d3146be4396e0b2db19377e00d110794 *R/oldRCS/andarl.R,v 8052568d7f91b67ab5e8794f559b4cab *R/oldRCS/cramer.R,v 2c68dc762655bbc1958556cb1d84fa55 *R/oldRCS/cvmtest.R,v 56ce831b8afd6f55b7f7180a470226c1 *R/oldRCS/recog.R,v b5dc530d801f347fcc103ba9a9ef2bd6 *R/recog.R 29b8a1f246a7963b8d8e9cb77543f0c2 *build/partial.rdb 4ca09aee4f6ec8772a37f287a23a503a *man/RCS/ad.test.Rd,v 8c8471c1ed64c58a562fa42ffcd35b6b *man/RCS/cvm.test.Rd,v 62bc401099f8d0a7522fb4d24b5f71a4 *man/RCS/goftest-package.Rd,v 6e322a80bcf915758cf6b34edfe8c55c *man/RCS/pAD.Rd,v d0f7bb029253ac0d213e7f7be7f21690 *man/RCS/pCvM.Rd,v b26573082916c8dc121ae0610198cfcc *man/ad.test.Rd c4a9a49bd5eb2b0bc0c0c2d26eed4929 *man/cvm.test.Rd aeefaa4a55958c09f7f8cc3ad4dc2eae *man/goftest-package.Rd d31fca48d10c5dd9e63be3955c14412d *man/pAD.Rd b6294a6cc54d82ceb166326a40f07b4c *man/pCvM.Rd a4f4486d8a6e4b08d73001ef3134c2a2 *man/recogniseCdf.Rd ca3270c176ce09cd15243bf4df918aa9 *src/ADinf.c d7fa22cf49d25bc1ceb8fa5b02dc5aa2 *src/AnDarl.c db80933a3060fbea79e3fee3c0e04278 *src/RCS/ADinf.c,v ba414da1b88f5baa526cf93958359949 *src/RCS/AnDarl.c,v a197e1a03ae0ab8761473c5ad9bc9508 *src/init.c 84f17e3621a4165a4d306993459dfaa8 *tests/all.R