Learn R Programming

truncgof (version 0.6-0)

mctest: Monte-Carlo simulation based GoF test

Description

Performs Monte-Carlo based Goodness-of-Fit tests. mctest is called by the GoF tests defined in this package. For internal use only.

Usage

mctest(x, distn, parm, H, sim, tol, STATISTIC, estfun)

Arguments

x
numerical vector of data values
distn
character string specifying the null distribution
parm
parameters of the null distribution
H
a treshold value
sim
maximum number of szenarios within the Monte-Carlo-Simulation
tol
if the difference of two subsequent p-value calculations is lower than tol the Monte-Carlo simulation stops
STATISTIC
function of the test statistic
estfun
an function as character string or NA, see details.

Value

named list of
TS
value of the test statistic for x
p.value
Monte-Carlo simulation based p-value of the statistic
sim
number of simulated szenarios in the Monte-Carlo simulation

Details

From the fitted null distribution mctest draws samples each with length of the observed sample x and with treshold H. The random numbers are taken from the conditional distribution with support $[H, +Inf)$. The maximum number of samples is specified by sim. For each of these samples the conditional distribution is fitted and the statistic given in STATISTIC is calculated. The p-value is the proportion of times the sample statistics values exceed the statistic value of the observed sample.

For each szenario sample mctest uses a Maximum-likelihood fitting of the distribution distn as default. This is done by direct optimization of the log-likelihood function using optim.

Alternativly the fitting parameters for the szenario samples might be estimated with a user-specified function assigned in estfun. It must be a function with argument x (and H if desired) which can be parsed. The return value of the evaluated function must be a list with the parameters which should be fitted. Inside mctest the evaluation of estfun is performed with H as assigned in the call of mctest and x the szenario sample.

By assigning a function to estfun, the fitting procedure can be done faster and more appropriate to a given problem. The 'evir' package for example defines a function gpd to fit the Generalized Pareto Model. To start a test it is more reasonable to set estfun = gpd(x, y), where y must be a defined numeric value.

References

Chernobay, A., Rachev, S., Fabozzi, F. (2005), Composites goodness-of-fit tests for left-truncated loss samples, Tech. rep., University of Calivornia Santa Barbara

Ross, S. M. (2002), Simulation, 3rd Edition, Academic Press. Pages 205-208.

See Also

ad2.test, ad2up.test, w2.test for quadratic class GoF tests and ks.test, v.test, adup.test, ad.test for supremum class GoF tests.

Examples

Run this code
set.seed(123)
treshold <- 10
xc <- rgamma(100, 20, 2)    # complete sample
xt <- xc[xc > treshold]     # left truncated sample

## function for parameter fitting
estimate <- function(x, H){
    cgamma <- cdens("pgamma", H)
    ll <- function(p, y) {
        res <- -sum(do.call("cgamma", list(c(y), p[1], p[2], log = TRUE)))
        if (!is.finite(res)) return(-log(.Machine$double.xmin)*length(x))
        return(res)
    }
    est <- optim(c(1,1), ll, y = x, lower = c(.Machine$double.eps, 0), 
                 method = "L-BFGS-B")
    as.list(est$par)
}

fit <- estimate(xt, treshold)
cat("fitting parameters:", unlist(fit), "\n")

## calculate p-value with fitting algorithm defined in 'mctest' ...
ad2up.test(xt, "pgamma", fit, H = treshold,  estfun = NA, tol = 1e-02)

## ... or with the function 'estimate'
ad2up.test(xt, "pgamma", fit, H = treshold, estfun = "estimate(x, H)", 
           tol = 1e-02)      

## not run:
## if the 'evir' package is loaded:
## ad.test(xt, "pgpd", list(2,3), H = treshold,  
##         estfun = "as.list(gpd(x, 0)$par.ests)", tol = 1e-02)

Run the code above in your browser using DataLab