Weighted LOESS (LOcal regrESSion)

LOcal regrESSion (LOESS) or LOcally WEighted Scatter-plot Smoother (LOWESS)

LOESS or LOWESS is a nonparametric technique to fit a smooth curve through points in a scatter plot. This approach uses locally estimated linear regression at its core.

The following code illustrates how to include a loess line using the ggplot2 package.

library(ggplot2)

set.seed(1)
x <- rnorm(60)
y <- c(rnorm(40), 10, rnorm(19))
df <- data.frame(x=x, y=y)

## Without weights
ggplot(data=df, aes(x=x, y=y)) + geom_point() + 
 geom_smooth(method=loess, legend=FALSE)

Furthermore your can plot loess smoothing weighting the observations as follows:

weights <- c(rep(1, 40), 20, rep(1, 19))
df$weights <- weights


ggplot(data=df, aes(x=x, y=y, size=weights, weight=weights)) + geom_point() + 
 geom_smooth(method=loess, legend=TRUE)

Related

ge badge-light" href="/tags/ggplot2/">ggplot2
Avatar
Thiyanga S. Talagala
PhD in Statistics