Learn R Programming

MonotonicityTest (version 1.3)

create_kernel_plot: Generate Kernel Plot

Description

Creates a scatter plot of the input vectors \(X\) and \(Y\), and overlays a Nadaraya-Watson kernel regression curve using the specified bandwidth.

Usage

create_kernel_plot(X, Y, bandwidth = bw.nrd(X) * (length(X)^-0.1), nrows = 4)

Value

A ggplot object containing the scatter plot(s) with the kernel regression curve(s). If a vector of bandwidths is supplied, the plots are put into a grid using faceting.

Arguments

X

Vector of x values.

Y

Vector of y values.

bandwidth

Kernel bandwidth used for the Nadaraya-Watson estimator. Can be a single numeric value or a vector of bandwidths. Default is calculated as bw.nrd(X) * (length(X) ^ -0.1).

nrows

Number of rows in the facet grid if multiple bandwidths are provided. Does not do anything if only a single bandwidth value is provided. Default is 4.

References

Nadaraya, E. A. (1964). On estimating regression. Theory of Probability and Its Applications, 9(1), 141–142.

Watson, G. S. (1964). Smooth estimates of regression functions. Sankhyā: The Indian Journal of Statistics, Series A, 359-372.

Examples

Run this code
# Example 1: Basic plot on quadratic function
seed <- 42
set.seed(seed)
X <- runif(500)
Y <- X ^ 2 + rnorm(500, sd = 0.1)
plot <- create_kernel_plot(X, Y, bandwidth = bw.nrd(X) * (length(X) ^ -0.1))

Run the code above in your browser using DataLab