Kernel Consistent Density Equality Test with Mixed Data Types
np.deneqtest.Rdnpdeneqtest implements a consistent integrated squared
difference test for equality of densities as described in Li, Maasoumi,
and Racine (2009).
Arguments
Data, Bandwidth Inputs And Formula Interface
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 (
bwmethod = "cv.ls", overridable through...), then rescaled to the harmonic-mean sample size as described below. If only one is supplied it is reused for both samples without a search. If both are supplied their effective bandwidths, kernels and support specifications must agree; incompatible specifications are rejected. This test requires fixed bandwidths and symmetric kernels without boundary normalization. Nearest-neighbour bandwidths, beta kernels and finite continuous normalization bounds are rejected: the test's directed cross sum and variance formula assume symmetry. Unnormalized Gaussian, Epanechnikov and uniform kernels remain supported, including mixed continuous/categorical data. An active ordered Racine–Li–Yan kernel is also rejected because it is asymmetric and has a nonconstant self-diagonal. Other estimators' kernel support is unchanged.- 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).
- B
an integer value specifying the number of bootstrap replications to use. Defaults to
399.- random.seed
an integer used to seed R's random number generator. This is to ensure replicability. Defaults to 42.
Additional Arguments
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
bw.xandbw.yare absent; they do not modify a supplied bandwidth specification. To do this, you may specify any ofbwscaling,bwtype,ckertype,ckerorder,ukertype,okertype.
Bootstrap P-Values
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 reported zero is possible with a finite number of replications.
Value
npdeneqtest returns an object of type deneqtest with the
following components
- Tn
the (standardized) statistic
Tn- In
the (unstandardized) statistic
In- Tn.bootstrap
contains the bootstrap replications of
Tn- In.bootstrap
contains the bootstrap replications of
In- Tn.P
the P-value of the
Tnstatistic- In.P
the P-value of the
Instatistic- boot.num
number of bootstrap replications
summary supports object of type deneqtest.
Book And Method Pointers
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.
References
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.
Author
Tristen Hayfield tristen.hayfield@gmail.com, Jeffrey S. Racine racinej@mcmaster.ca
Details
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.
Usage Issues
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.
Examples
if (FALSE) { # \dontrun{
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,B=99)
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,B=99)
## 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,B=99)
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,B=99)
} # }