hybrid_test: Tests for the goodness-of-fit problem via twosample tests

View source: R/hybrid_test.R

hybrid_testR Documentation

Tests for the goodness-of-fit problem via twosample tests

Description

This function runs a number of goodness-of-fit tests in the case where data can be generated under the null hypothesis but its cdf can not be calculated. A large Monte Carlo data set is generated under the null hypothesis, and then the twosample tests of the package R2sample are run on the data and the Monte Carlo data set. p values are found via the parametric bootstrap.

Usage

hybrid_test(
  x,
  vals = NA,
  rnull,
  phat = function(x) -99,
  nMC = 1,
  TS,
  TSextra,
  B = 5000,
  seed = NULL,
  maxProcessor,
  doMethods = "all",
  SuppressMessages = FALSE
)

Arguments

x

data set: a numeric vector for continuous data, a vector of counts for discrete data.

vals

=NA, values of discrete random variable, or NA if data is continuous.

rnull

routine to generate data under the null hypothesis, with no arguments or one argument, the parameters.

phat

=function(x) -99, function to estimate parameters from the data, if needed.

nMC

=1, sample size of Monte Carlo data set. If nMC<=10 the sample size used will be nMC times the sample size of x.

TS

user supplied function to find test statistics, if any. It has to have the format required by R2sample::twosample_test.

TSextra

(optional) list passed to TS, if needed.

B

=5000, number of simulation runs. If B=0 the routine returns the test statistics.

seed

=NULL (optional) seed for simulations, to aid reproducibility.

maxProcessor

number of processors to use in parallel processing.

doMethods

="all", a vector of codes for the methods to include or all of them.

SuppressMessages

=FALSE, print informative messages?

Details

If parameters are estimated, the Monte Carlo data set is generated with the parameters estimated from the data. In each bootstrap run a new data set is generated with these parameters, the parameters are estimated again from this new data set, and the Monte Carlo data set is generated with those estimates.

If the cdf under the null hypothesis can be calculated, gof_test should be used instead, as it generally has higher power.

The chi square tests use the usual chi square approximation and treat the Monte Carlo data as an independent sample, so their p values are only approximate if parameters are estimated.

Requires the package R2sample.

For details on the usage of this routine consult the vignette with vignette("Rgof","Rgof")

Value

An object of class "Rgof_test", as returned by gof_test, with components results, statistics, p.values, parameters and metadata (which also has hybrid=TRUE and nMC, the size of the Monte Carlo data set).

Examples

# All examples are run with B=10 and maxProcessor=1 to pass CRAN checks.
# (they need R2sample, in a version newer than 5.0.0)
if(requireNamespace("R2sample", quietly=TRUE) &&
   utils::packageVersion("R2sample") > "5.0.0") {
  # Test whether data comes from a standard normal distribution.
  rnull=function() rnorm(100)
  x=rnull()
  hybrid_test(x, rnull=rnull, B=20, maxProcessor=1)
  # Test whether data comes from a normal distribution,
  # with mean and standard deviation estimated.
  rnull=function(p) rnorm(100, p[1], p[2])
  phat=function(x) c(mean(x), sd(x))
  x=rnull(c(0, 1))
  hybrid_test(x, rnull=rnull, phat=phat, B=10, maxProcessor=1)
  # Discrete data: test whether data comes from a binomial(10, p) distribution,
  # with p estimated
  vals=0:10
  rnull=function(p) table(c(0:10, rbinom(1000, 10, p)))-1
  phat=function(x) mean(rep(0:10, x))/10
  x=rnull(0.5)
  hybrid_test(x, vals, rnull, phat, B=10, maxProcessor=1)
}

Rgof documentation built on Nov. 21, 2026, 5:06 p.m.