Calculate an empirical confidence region for two variables, and optionally overlay the smooth polygon on a scatterplot.
conf2d(x, ...)# S3 method for formula
conf2d(formula, data, subset, ...)
# S3 method for default
conf2d(x, y, level=0.95, n=200, method="wand", shape=1, smooth=50,
       plot=TRUE, add=FALSE, xlab=NULL, ylab=NULL, col.points="gray",
       col="black", lwd=2, ...)
conf2d_int(x, y, surf, level, n)  # internal function
List containing five elements:
x coordinates defining the region.
y coordinates defining the region.
logical vector indicating which of the original data coordinates are inside the region.
area inside the region.
actual proportion of points inside the region.
a vector of x values, or a data frame whose first two columns contain the x and y values.
a vector of y values.
a formula, such as y~x.
a data.frame, matrix, or list from
    which the variables in formula should be taken.
an optional vector specifying a subset of observations to be used.
the proportion of points that should be inside the region.
the number of regions to evaluate, before choosing the region
    that matches level best.
kernel smoothing function to use: "wand" or
    "mass".
a bandwidth scaling factor, affecting the polygon shape.
the number of bins (scalar or vector of length 2), affecting the polygon smoothness.
whether to plot a scatterplot and overlay the region as a polygon.
whether to add a polygon to an existing plot.
a label for the x axis.
a label for the y axis.
color of points.
color of polygon.
line width of polygon.
further arguments passed to plot and
    polygon.
a list whose first three elements are x coordinates, y coordinates, and a surface matrix.
Arni Magnusson and Julian Burgos, based on an earlier function by Gregory R. Warnes.
This function constructs a large number (n) of smooth polygons,
  and then chooses the polygon that comes closest to containing a given
  proportion (level) of the total points.
The default method="wand" calls the
  bkde2D kernel smoother from the
  KernSmooth package, while method="mass" calls
  kde2d from the MASS package.
The conf2d function calls bkde2D or kde2d to
  compute a smooth surface from x and y. If users already
  have a smoothed surface to work from, the internal conf2d_int
  can be used directly to find the empirical confidence region that
  matches level best.
quantile is the corresponding univariate equivalent.
The distfree.cr package uses a different smoothing algorithm to calculate bivariate empirical confidence regions.
ci2d in the gplots package is a predecessor of
  conf2d.
freq2d calculates a discrete frequency distribution for
  two continuous variables.
r2d2-package gives an overview of the package.
conf2d(Ushape)$prop
conf2d(saithe, pch=16, cex=1.2, col.points=rgb(0,0,0,0.1), lwd=3)
# First surface, then region
plot(saithe, col="gray")
surf <- MASS::kde2d(saithe$Bio, saithe$HR, h=0.25, n=100)
region <- conf2d_int(saithe$Bio, saithe$HR, surf, level=0.95, n=200)
polygon(region, lwd=2)
Run the code above in your browser using DataLab