| npcopula | R Documentation |
npcopula estimates a mixed-data kernel copula distribution or
copula density. It can be called with an existing unconditional
distribution/density bandwidth object, or with a one-sided formula in
which case the appropriate bandwidth object is selected internally.
npcopula(bws, ...)
## S3 method for class 'formula'
npcopula(bws,
data = NULL,
u = NULL,
target = c("distribution", "density"),
evaluation = c("grid", "sample"),
neval = 30,
n.quasi.inv = 1000,
er.quasi.inv = 1,
...)
## Default S3 method:
npcopula(bws,
data,
u = NULL,
target = NULL,
evaluation = c("sample", "grid"),
neval = 30,
n.quasi.inv = 1000,
er.quasi.inv = 1,
...)
## S3 method for class 'npcopula'
predict(object,
newdata = NULL,
u = NULL,
se.fit = FALSE,
output = c("vector", "object", "data"),
...)
## S3 method for class 'npcopula'
plot(x,
perspective = TRUE,
view = c("rotate", "fixed", "surface", "contour", "image",
"empirical", "all"),
renderer = c("base", "rgl"),
errors = c("none", "bootstrap", "asymptotic"),
band = c("pointwise", "pmzsd", "bonferroni",
"simultaneous", "all"),
alpha = 0.05,
bootstrap = c("inid", "fixed", "geom"),
B = 1999,
center = c("estimate", "bias-corrected"),
boot_control = np_boot_control(),
output = c("plot", "data", "plot-data", "both"),
legend = TRUE,
theta = 0.0,
phi = 20.0,
xlab = "u1",
ylab = "u2",
zlab = NULL,
main = NULL,
col = NULL,
border = "black",
zlim = NULL,
...)
These arguments identify the bandwidth specification, source data, and formula route.
bws |
bandwidth specification or one-sided formula. A bandwidth
specification is either an unconditional distribution bandwidth
object returned by |
data |
data frame containing the variables used to construct |
These arguments control whether a copula distribution or density is estimated and where it is evaluated.
er.quasi.inv |
fraction passed to |
evaluation |
evaluation route used when |
n.quasi.inv |
number of grid points used to compute each marginal quasi-inverse
when |
newdata |
optional prediction data for |
neval |
number of probability values per margin in the automatically
generated two-dimensional grid used when |
target |
target used when |
u |
optional matrix or data frame of marginal probability values in
|
object |
an object of class |
se.fit |
logical value. If |
These arguments control how a two-dimensional grid copula object is displayed.
x |
an object of class |
border |
border color for surface facets and interval wireframes when
|
col |
optional surface or image colors. If omitted, |
legend |
logical or legend-control value used for |
main, xlab, ylab, zlab |
plot titles and axis labels. |
output |
return mode. For |
perspective |
logical value. If |
phi, theta |
viewing angles passed to |
renderer |
plotting renderer for surface displays. |
view |
display type for grid output. The default |
zlim |
optional z-axis limits for surface displays. |
These arguments add asymptotic or bootstrap intervals to two-dimensional surface plots.
alpha |
nominal size used for asymptotic or bootstrap intervals when
|
B |
number of bootstrap replications when |
band |
interval type for plotted surfaces. Supported values are
|
bootstrap |
bootstrap resampling method used when |
boot_control |
optional |
center |
centering convention for bootstrap intervals, either
|
errors |
interval route for |
Further arguments are passed to the bandwidth-selection counterpart, prediction/evaluation route, or graphics renderer as appropriate.
... |
additional arguments supplied to |
Documentation guide: see np.kernels for kernels,
np.options for global options, and plot
for plotting options.
npcopula computes the nonparametric copula distribution or
copula density using marginal quasi-inversion. For the distribution
target, Sklar's theorem gives
C(u_1,\ldots,u_d)
=
H(F_1^{-1}(u_1),\ldots,F_d^{-1}(u_d)),
where H is the joint distribution and F_j^{-1} is the
quasi-inverse of marginal distribution F_j. For the density
target, the estimated copula density is
c(u)
=
\frac{f(x_u)}
{\prod_{j=1}^d f_j(x_{u,j})},
\quad x_{u,j}=F_j^{-1}(u_j),
with numerator and marginal denominators estimated using the selected mixed-data kernel bandwidths.
If u is provided, expand.grid is called on
u. As the dimension increases this can become unwieldy because
a grid with m points in each of d margins has
m^d rows. Therefore the formula route automatically generates a
probability grid only for two-dimensional copulas. For
higher-dimensional copulas, supply u explicitly or use
evaluation="sample".
The ‘quasi-inverse’ is computed via Definition 2.3.6 from
Nelsen (2006). An equi-quantile grid on the data range of length
n.quasi.inv/2 is combined with an equi-spaced grid on the data
range extended by er.quasi.inv; the sorted union forms the
grid used for marginal inversion. If requested probability values lie
outside the attainable estimated marginal distribution range, they
are reset to the nearest attainable endpoint. Inspect the returned
u columns when endpoint behavior matters.
The plot.npcopula method supports base persp,
contour, and image displays for
two-dimensional grid output. Surface plots use the package-wide
viridis default palette, detailed perspective ticks, and the same
default viewing angles and base-graphics rotation cadence as the
other surface plot methods. renderer="rgl" requests the shared
interactive rgl surface renderer, using the same default-angle
remapping used by the other package surface plots. For mixed ordered
margins, grid displays are drawn against the requested probability
grid, while the returned u columns retain the attainable
marginal probability values produced by quasi-inversion. The
"empirical" view plots empirical copula coordinates, and
"all" gives a base-graphics four-panel diagnostic display.
For grid surfaces, plot.npcopula can add asymptotic or
bootstrap intervals. Distribution-copula asymptotic intervals use the
joint distribution standard error evaluated at the marginal
quasi-inverse grid. Density-copula asymptotic intervals use the
plug-in delta-method denominator correction corresponding to
c(u)=f(x_u)/\prod_j f_j(x_{u,j}). Bootstrap intervals resample
rows and recompute the plotted copula surface on the same probability
grid; band="all" overlays transparent pointwise,
simultaneous, and Bonferroni wireframes.
npcopula returns an object of class "npcopula". The main
components are:
copula |
estimated copula distribution value or copula density value. |
u1, u2, ... |
marginal probability coordinates associated with the sample realizations or evaluation grid. |
x, y, ... |
marginal quasi-inverse coordinates corresponding to the requested probability grid when grid evaluation is used. |
bws |
selected unconditional distribution or density bandwidth object. |
eval |
data frame containing the copula values, probability coordinates, and
quasi-inverse coordinates. |
copulaerr |
asymptotic or bootstrap standard-error slot. The fitted object stores
|
The source data, target, evaluation route, grid dimensions, and timing
metadata are retained as list components. The functions
fitted, predict, se,
summary, as.data.frame, and
plot support "npcopula" objects.
The copula distribution target is
C(u)=H(F_1^{-1}(u_1),\ldots,F_d^{-1}(u_d)); the copula density
target is c(u)=f(x_u)/\prod_j f_j(x_{u,j}). The mixed-data
kernel implementation follows Racine (2015), with quasi-inversion in
the sense of Nelsen (2006). For the underlying mixed-data density and
distribution estimators, see Li and Racine (2007), Chapter 1
Density Estimation, Chapter 3 Kernel Estimation with
Mixed Data, and Racine (2019), Chapter 2 Continuous Density
and Cumulative Distribution Functions.
Use a data.frame rather than cbind for
mixed data so that ordered variables remain ordered. Unordered
factors are not valid for copula estimation in this implementation.
Jeffrey S. Racine racinej@mcmaster.ca
Li, Q. and J.S. Racine (2007), Nonparametric Econometrics: Theory and Practice, Princeton University Press.
Nelsen, R.B. (2006), An Introduction to Copulas, Second Edition, Springer.
Racine, J.S. (2015), “Mixed Data Kernel Copulas”, Empirical Economics, 48, 37–59.
Racine, J.S. (2019), An Introduction to the Advanced Theory and Practice of Nonparametric Econometrics: A Replicable Approach Using R.
npudistbw, npudist,
npudensbw, npudens,
np.kernels, np.options,
plot
## Not run:
library("MASS")
## Example 1: bivariate mixed data, continuous x and ordered y.
set.seed(42)
n <- 1000
n.eval <- 30
rho <- 0.99
mu <- c(0, 0)
Sigma <- matrix(c(1, rho, rho, 1), 2, 2)
xy <- mvrnorm(n = n, mu = mu, Sigma = Sigma)
mydat <- data.frame(
x = xy[, 1],
y = ordered(as.integer(cut(xy[, 2],
quantile(xy[, 2], seq(0, 1, by = .1)),
include.lowest = TRUE)) - 1)
)
grid.seq <- seq(0, 1, length.out = n.eval)
grid.dat <- data.frame(u1 = grid.seq, u2 = grid.seq)
## Estimate the copula distribution from an npudistbw() object.
bw.cdf <- npudistbw(~ x + y, data = mydat, nmulti = 1)
copula <- npcopula(bws = bw.cdf, data = mydat, u = grid.dat)
summary(copula)
## Native plotting replaces the older manual contour(), persp(), and
## empirical scatterplot calls.
plot(copula, perspective = FALSE, view = "contour")
plot(copula, perspective = FALSE, view = "image")
plot(copula, view = "fixed", zlim = c(0, 1))
if (requireNamespace("rgl", quietly = TRUE))
plot(copula, view = "fixed", renderer = "rgl", zlim = c(0, 1))
plot(copula)
## Plot empirical copula coordinates from the retained sample data.
plot(copula, view = "empirical")
## Or request the four-panel base-graphics diagnostic display.
plot(copula, view = "all")
## Estimate and plot the copula density from an npudensbw() object.
bw.pdf <- npudensbw(~ x + y, data = mydat, nmulti = 1)
copula.dens <- npcopula(bws = bw.pdf, data = mydat, u = grid.dat)
summary(copula.dens)
plot(copula.dens, view = "fixed")
if (requireNamespace("rgl", quietly = TRUE))
plot(copula.dens, view = "fixed", renderer = "rgl")
plot(copula.dens)
## Intervals are available for two-dimensional grid surfaces.
plot(copula, errors = "asymptotic", band = "pointwise")
plot(copula, errors = "bootstrap", bootstrap = "inid", B = 399,
band = "pointwise")
## Prediction evaluates the retained bandwidth object on a supplied
## probability grid.
predict(copula, u = data.frame(x = c(0.25, 0.75),
y = c(0.25, 0.75)))
predict(copula, newdata = data.frame(u1 = c(0.25, 0.75),
u2 = c(0.25, 0.75)))
## Example 2: bivariate continuous data.
set.seed(42)
n <- 1000
n.eval <- 30
rho <- 0.99
mu <- c(0, 0)
Sigma <- matrix(c(1, rho, rho, 1), 2, 2)
xy <- mvrnorm(n = n, mu = mu, Sigma = Sigma)
mydat <- data.frame(x = xy[, 1], y = xy[, 2])
grid.seq <- seq(0, 1, length.out = n.eval)
grid.dat <- data.frame(u1 = grid.seq, u2 = grid.seq)
bw.cdf <- npudistbw(~ x + y, data = mydat, nmulti = 1)
copula <- npcopula(bws = bw.cdf, data = mydat, u = grid.dat)
summary(copula)
plot(copula, perspective = FALSE, view = "contour")
plot(copula, perspective = FALSE, view = "image")
plot(copula, view = "fixed", zlim = c(0, 1))
if (requireNamespace("rgl", quietly = TRUE))
plot(copula, view = "fixed", renderer = "rgl", zlim = c(0, 1))
plot(copula)
plot(copula, view = "empirical")
plot(copula, view = "all")
bw.pdf <- npudensbw(~ x + y, data = mydat, nmulti = 1)
copula.dens <- npcopula(bws = bw.pdf, data = mydat, u = grid.dat)
summary(copula.dens)
plot(copula.dens, view = "fixed", zlim = c(0, 40))
if (requireNamespace("rgl", quietly = TRUE))
plot(copula.dens, view = "fixed", renderer = "rgl",
zlim = c(0, 40))
plot(copula.dens, zlim = c(0, 40))
## The formula interface is a shorter route when bandwidths do not need
## to be reused explicitly.
copula.short <- npcopula(~ x + y, data = mydat, neval = n.eval,
nmulti = 1)
plot(copula.short, view = "all")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.