Learn R Programming

rddensity (version 1.0)

rdplotdensity: Density Plot

Description

rdplotdensity constructs density plot near the cutoff. It is based on the local polynomial density estimator proposed in Cattaneo, Jansson and Ma (2019).

Companion command: rddensity for manipulation testing. A companion Stata package is described in Cattaneo, Jansson and Ma (2018).

Related Stata and R packages useful for inference in regression discontinuity (RD) designs are described at https://sites.google.com/site/rdpackages.

Usage

rdplotdensity(rdd, X, plotRange = NULL, plotN = 10,
  plotGrid = c("es", "qs"), alpha = 0.05, type = NULL,
  CItype = NULL, title = "", xlabel = "", ylabel = "",
  lty = NULL, lwd = NULL, lcol = NULL, pty = NULL, pwd = NULL,
  pcol = NULL, CIshade = NULL, CIcol = NULL, legendTitle = NULL,
  legendGroups = NULL)

Arguments

rdd

Object returned by rddensity

X

Numeric vector or one dimensional matrix / data frame, the running variable.

plotRange

Numeric, specifies the lower and upper bound for density plot. By default it is three bandwidths around the cutoff.

plotN

Numeric, specifies the number of grid points used for density plot on each side. If more than one is provided, they will be applied to the two sides accordingly. By default 10 points are used on each side.

plotGrid

String, specifies the position of grid points. Can be either evenly spaced (default, "es"), or quantile spaced ("qs").

alpha

Numeric scalar between 0 and 1, the significance level for plotting confidence regions. If more than one is provided, they will be applied to the two sides accordingly.

type

String, one of "line" (default), "points" or "both", how the point estimates are plotted. If more than one is provided, they will be applied to the two sides accordingly.

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 the two sides accordingly.

title, xlabel, ylabel

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

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 the two sides 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 the two sides 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 the two sides 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 the two sides 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 the two sides 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 the two sides accordingly.

CIshade

Numeric, 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 the two sides accordingly.

CIcol

color for 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 the two sides accordingly.

legendTitle

String, title of legend.

legendGroups

String Vector, group names used in legend.

Value

Estl, Estr

Matrices containing estimation results on the two side, with (1) grid (grid points), (2) bw (bandwidths), (3) nh (effective/local sample sizes), (4) f_p (point estimates with p-th order local polynomial), (5) f_q (point estimates with q-th order local polynomial, only if option q is nonzero), (6) se_p (standard error corresponding to f_p), and (7) se_q (standard error corresponding to f_q).

Estplot

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

References

M.D. Cattaneo, M. Jansson and X. Ma. (2018). Manipulation Testing based on Density Discontinuity. Stata Journal 18(1): 234-261.

M.D. Cattaneo, M. Jansson and X. Ma. (2019). Simple Local Polynomial Density Estimators. Journal of the American Statistical Association, forthcoming.

See Also

rddensity

Examples

Run this code
# NOT RUN {
# Generate a random sample with a discontinuous density
set.seed(42)
x <- rnorm(2000, mean = -0.5); x[x>0] <- x[x>0] * 2

# Estimation
rdd <- rddensity(X = x)

# Plot
plot <- rdplotdensity(rdd, x, plotRange = c(-2, 2), plotN = 25)
# }

Run the code above in your browser using DataLab