| npdeneqtest | R Documentation |
npdeneqtest implements a consistent integrated squared
difference test for equality of densities as described in Li, Maasoumi,
and Racine (2009).
npdeneqtest(x = NULL,
y = NULL,
bw.x = NULL,
bw.y = NULL,
B = 399,
random.seed = 42,
...)
These arguments identify the two samples and any supplied bandwidths.
bw.x, bw.y |
optional common bandwidth specification. If neither is supplied, one
bandwidth is selected on the pooled sample by least-squares cross-validation
( |
x, y |
data frames for the two samples for which one wishes to test equality of densities. The variables in each data frame must be the same (i.e. have identical names). |
These arguments control bootstrap replication and reproducibility settings.
B |
an integer value specifying the number of bootstrap
replications to use. Defaults to |
random.seed |
Temporary caller-state restoration requires a certifiable initialized state; see np_rng_scope. an integer used to seed R's random number generator. This is to ensure replicability. Defaults to 42. |
Further arguments are passed to the bandwidth-selection routines used by the test.
... |
additional arguments supplied to specify the bandwidth
type, kernel types, and so on. These selection arguments are used only
when both |
Documentation guide: see np.kernels for kernels, np.options for global options, and plot for plotting options.
npdeneqtest computes the integrated squared density difference
between the estimated densities/probabilities of two samples having
identical variables/datatypes. See Li, Maasoumi, and Racine (2009) for
details.
The same bandwidth is held fixed in both samples and every bootstrap
replication. Automatic selection uses one pooled-sample search, followed by
a harmonic-mean reference-size adjustment. Write
N=n_1+n_2 and n_H=2/(1/n_1+1/n_2), where the sample sizes count
complete observations. Retaining the pooled spread estimates, continuous
bandwidths are multiplied by (N/n_H)^{1/(2P+q)} and categorical
bandwidths by (N/n_H)^{2/(2P+q)}, following the scale-factor convention
of npudensbw. Here P is the continuous kernel order
(two by default), and q is the number of continuous variables.
For the default second-order kernel the continuous exponent is
1/(4+q). Categorical parameters cannot exceed their kernel's permitted
maximum: an adjusted value above that maximum is set to the maximum and a
warning names the affected variables.
Under a common density, the leading variance coefficient of the equally
weighted average of the two density-estimation risks is
(1/n_1+1/n_2)/2=1/n_H. This motivates the common reference size;
it is not a claim of optimal test power or exact finite-sample calibration.
The mixed-data and higher-order adjustments use the existing bandwidth
scaling convention. This rule differs from the archived first-sample
simulation implementation and from unadjusted pooled selection.
To use an unadjusted pooled bandwidth, construct it with
npudensbw(dat = rbind(x, y), bwmethod = "cv.ls") and supply it as
bw.x. Explicit bw.x/bw.y are never rescaled.
Likewise, bws = ..., bandwidth.compute = FALSE in ...
remains a manual specification, with scale factors interpreted using
x's complete observations rather than the pool.
Categorical coordinates use the union of the declared levels in both samples
and supplied bandwidth objects, including unused levels. The same domain
and effective kernel metadata are retained in every contraction and bootstrap
draw. Ordered domains must have an unambiguous common order; otherwise declare
the same ordered levels in both samples. Automatic selection uses both
samples with this common metadata.
A raw numeric bandwidth retains the default raw kernel-sum conventions;
a density bandwidth object retains its selected density-kernel conventions,
including normalized ordered Li–Racine masses. Selection arguments in
... do not override an explicitly supplied bandwidth specification.
With fixed, unbounded bandwidths, pooled bootstrap samples are represented by multiplicities on their common support and processed in bounded groups. Multi-response kernel sums then reuse the fixed geometry for the first- and second-power contractions without constructing a dense kernel matrix. Boundary-normalized and nearest-neighbour specifications are not supported by this density-equality statistic.
The result includes total.time, the elapsed wall-clock time in
seconds for the test call, displayed by summary.
npdeneqtest returns an object of type deneqtest with the
following components
Tn |
the (standardized) statistic |
In |
the (unstandardized) statistic |
Tn.bootstrap |
contains the bootstrap replications of |
In.bootstrap |
contains the bootstrap replications of |
Tn.P |
the P-value of the |
In.P |
the P-value of the |
boot.num |
number of bootstrap replications |
summary supports object of type deneqtest.
The bootstrap upper-tail p-value is the proportion of bootstrap statistics greater than or equal to the observed statistic. Ties count as at least as extreme, giving the conservative tie convention for discrete statistics. This is the ordinary bootstrap proportion, without an add-one correction; a zero is displayed as ‘0 (0 of B)’, with the actual number of draws.
npdeneqtest compares two distributions by estimating the
integrated squared difference between their density or probability
functions. The two samples must have matching variables and data
types because the statistic compares like with like.
For book-length background, see Li and Racine (2007), Chapter 12 Model Specification Tests, especially Section 12.2, and Chapter 13 Nonsmoothing Tests, especially Section 13.2. For density and mixed-data density context, see Racine (2019), Chapters 2 and 3.
If you are using data of mixed types, then it is advisable to use the
data.frame function to construct your input data and not
cbind, since cbind will typically not work as
intended on mixed data types and will coerce the data to the same
type.
It is crucial that both data frames have the same variable names.
Incomplete rows are omitted independently from each sample before bandwidth selection, bootstrap resampling and computation of the statistic. Factor levels are retained. Each sample must contain at least two complete observations. Supplying one bandwidth reuses it for both samples; it is not reselected when the other bandwidth argument is missing.
Tristen Hayfield tristen.hayfield@gmail.com, Jeffrey S. Racine racinej@mcmaster.ca
Li, Q. and E. Maasoumi and J.S. Racine (2009), “A Nonparametric Test for Equality of Distributions with Mixed Categorical and Continuous Data,” Journal of Econometrics, 148, pp 186-200.
np.kernels, np.options, plot
npdeptest,npsdeptest,npsymtest,npunitest
## Not run:
set.seed(1234)
## Distributions are equal
n <- 250
sample.A <- data.frame(x=rnorm(n))
sample.B <- data.frame(x=rnorm(n))
npdeneqtest(sample.A,sample.B)
if (interactive()) Sys.sleep(5)
## Distributions are unequal
sample.A <- data.frame(x=rnorm(n))
sample.B <- data.frame(x=rchisq(n,df=5))
npdeneqtest(sample.A,sample.B)
## Mixed datatypes, distributions are equal
sample.A <- data.frame(a=rnorm(n),b=factor(rbinom(n,2,.5)))
sample.B <- data.frame(a=rnorm(n),b=factor(rbinom(n,2,.5)))
npdeneqtest(sample.A,sample.B)
if (interactive()) Sys.sleep(5)
## Mixed datatypes, distributions are unequal
sample.A <- data.frame(a=rnorm(n),b=factor(rbinom(n,2,.5)))
sample.B <- data.frame(a=rnorm(n,sd=10),b=factor(rbinom(n,2,.25)))
npdeneqtest(sample.A,sample.B)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.