plot.lpdensity: Plot Method for Local Polynomial Density Estimation and...

View source: R/lpdensity_methods.R

plot.lpdensityR Documentation

Plot Method for Local Polynomial Density Estimation and Inference

Description

The plot method for local polynomial density objects.

Usage

## S3 method for class 'lpdensity'
plot(
  ...,
  alpha = NULL,
  type = NULL,
  lty = NULL,
  lwd = NULL,
  lcol = NULL,
  pty = NULL,
  pwd = NULL,
  pcol = NULL,
  grid = NULL,
  CItype = NULL,
  CIuniform = FALSE,
  CIsimul = 2000,
  CIshade = NULL,
  CIcol = NULL,
  hist = FALSE,
  histData = NULL,
  histBreaks = NULL,
  histFillCol = 3,
  histFillShade = 0.2,
  histLineCol = "white",
  title = NULL,
  xlabel = NULL,
  ylabel = NULL,
  legendTitle = NULL,
  legendGroups = NULL
)

Arguments

...

Class "lpdensity" object, obtained from calling lpdensity.

alpha

Numeric scalar between 0 and 1, specifies the significance level for plotting confidence intervals/bands. If more than one is provided, they will be applied to each data series accordingly.

type

String, one of "line" (default), "points" and "both", specifies how the point estimates are plotted. If more than one is provided, they will be applied to each data series accordingly.

lty

Line type for point estimates, only effective if type is "line" or "both". 1 for solid line, 2 for dashed line, 3 for dotted line. For other options, see the instructions for ggplot2 or par. If more than one is provided, they will be applied to each data series accordingly.

lwd

Line width for point estimates, only effective if type is "line" or "both". Should be strictly positive. For other options, see the instructions for ggplot2 or par. If more than one is provided, they will be applied to each data series accordingly.

lcol

Line color for point estimates, only effective if type is "line" or "both". 1 for black, 2 for red, 3 for green, 4 for blue. For other options, see the instructions for ggplot2 or par. If more than one is provided, they will be applied to each data series accordingly.

pty

Scatter plot type for point estimates, only effective if type is "points" or "both". For options, see the instructions for ggplot2 or par. If more than one is provided, they will be applied to each data series accordingly.

pwd

Scatter plot size for point estimates, only effective if type is "points" or "both". Should be strictly positive. If more than one is provided, they will be applied to each data series accordingly.

pcol

Scatter plot color for point estimates, only effective if type is "points" or "both". 1 for black, 2 for red, 3 for green, 4 for blue. For other options, see the instructions for ggplot2 or par. If more than one is provided, they will be applied to each data series accordingly.

grid

Numeric vector, specifies a subset of grid points to plot point estimates. This option is effective only if type is "points" or "both"; or if CItype is "ebar" or "all".

CItype

String, one of "region" (shaded region, default), "line" (dashed lines), "ebar" (error bars), "all" (all of the previous) or "none" (no confidence region), how the confidence region should be plotted. If more than one is provided, they will be applied to each data series accordingly.

CIuniform

TRUE or FALSE (default), plotting either pointwise confidence intervals (FALSE) or uniform confidence bands (TRUE).

CIsimul

Positive integer, specifies the number of simulations used to construct critical values (default is 2000). This option is ignored if CIuniform=FALSE.

CIshade

Numeric, specifies the opaqueness of the confidence region, should be between 0 (transparent) and 1. Default is 0.2. If more than one is provided, they will be applied to each data series accordingly.

CIcol

Color of the confidence region. 1 for black, 2 for red, 3 for green, 4 for blue. For other options, see the instructions for ggplot2 or par. If more than one is provided, they will be applied to each data series accordingly.

hist

TRUE or FALSE (default), specifies whether a histogram should be added to the background.

histData

Numeric vector, specifies the data used to construct the histogram plot.

histBreaks

Numeric vector, specifies the breakpoints between histogram cells.

histFillCol

Color of the histogram cells.

histFillShade

Opaqueness of the histogram cells, should be between 0 (transparent) and 1. Default is 0.2.

histLineCol

Color of the histogram lines.

title, xlabel, ylabel

Strings, specifies the title of the plot and labels for the x- and y-axis.

legendTitle

String, specifies the legend title.

legendGroups

String vector, specifies the group names used in legend.

Value

A stadnard ggplot object is returned, hence can be used for further customization.

Author(s)

Matias D. Cattaneo, Princeton University. cattaneo@princeton.edu.

Michael Jansson, University of California Berkeley. mjansson@econ.berkeley.edu.

Xinwei Ma (maintainer), University of California San Diego. x1ma@ucsd.edu.

See Also

lpdensity for local polynomial density estimation.

Supported methods: coef.lpdensity, confint.lpdensity, plot.lpdensity, print.lpdensity, summary.lpdensity, vcov.lpdensity.

Examples

# Generate a random sample
set.seed(42); X <- rnorm(2000)

# Generate a density discontinuity at 0
X <- X - 0.5; X[X>0] <- X[X>0] * 2

# Density estimation, left of 0 (scaled by the relative sample size)
est1 <- lpdensity(data = X[X<=0], grid = seq(-2.5, 0, 0.05), bwselect = "imse-dpi",
  scale = sum(X<=0)/length(X))
# Density estimation, right of 0 (scaled by the relative sample size)
est2 <- lpdensity(data = X[X>0],  grid = seq(0, 2, 0.05), bwselect = "imse-dpi",
  scale = sum(X>0)/length(X))

# Plot
plot(est1, est2, legendTitle="My Plot", legendGroups=c("Left", "Right"))

# Plot uniform confidence bands
set.seed(42) # fix the seed for simulating critical values
plot(est1, est2, legendTitle="My Plot", legendGroups=c("Left", "Right"), CIuniform=TRUE)

# Adding a histogram to the background
plot(est1, est2, legendTitle="My Plot", legendGroups=c("Left", "Right"),
  hist=TRUE, histBreaks=seq(-2.4, 2, 0.2), histData=X)

# Plot point estimates for a subset of evaluation points
plot(est1, est2, legendTitle="My Plot", legendGroups=c("Left", "Right"),
  type="both", CItype="all", grid=seq(-2, 2, 0.5))

lpdensity documentation built on Jan. 22, 2023, 1:39 a.m.