Skip to contents

Avoids the boundary bias of Nadaraya-Watson by fitting a local linear model at each evaluation point.

Usage

local_linear(x, y, x_eval, bandwidth, return_slope = FALSE)

Arguments

x

Numeric covariate vector.

y

Numeric outcome vector.

x_eval

Evaluation grid.

bandwidth

Positive bandwidth.

return_slope

Logical; if TRUE, also return local slopes.

Value

If return_slope = FALSE, a numeric vector of fitted values; otherwise a list with y_hat and beta_hat.

References

Fan, J. and Gijbels, I. (1996). Local Polynomial Modelling and Its Applications. Chapman and Hall.

Examples

set.seed(1)
x <- sort(runif(40, -2, 2))
y <- sin(x) + 0.2 * rnorm(40)
x_eval <- seq(-1.5, 1.5, length.out = 20)
fit <- local_linear(x, y, x_eval, bandwidth = 0.5)
fit
#>  [1] -0.97869188 -0.94633326 -0.88674797 -0.80646929 -0.71074566 -0.60208635
#>  [7] -0.48087670 -0.34765148 -0.20538891 -0.05888982  0.08774416  0.23179844
#> [13]  0.37085836  0.50171509  0.62031169  0.72340628  0.81042555  0.88285863
#> [19]  0.94215614  0.98846407