Fit a line with LOESS in R
Unfortunately the data are not available anymore, but an easier way how to fit a non-parametric line (Locally Weighted Scatterplot Smoothing or just a LOESS if you want) is to use following code:
scatter.smooth(y ~ x, span = 2/3, degree = 2)
Note that you can play with parameters span
and degree
to get arbitrary smoothness.
May be is to late, but you have options with ggplot (and dplyr). First if you want only plot a loess line over points, you can try:
library(ggplot2)load(url("https://www.dropbox.com/s/ud32tbptyvjsnp4/data.R?dl=1"))ggplot(data, aes(x, y)) + geom_point() +geom_smooth(method = "loess", se = FALSE)
Other way, is by predict()
function using a loess fit. For instance I used dplyr functions to add predictions to new column called "loess"
:
library(dplyr) data %>% mutate(loess = predict(loess(y ~ x, data = data))) %>% ggplot(aes(x, y)) + geom_point(color = "grey50") + geom_line(aes(y = loess))
Update: Added line of code to load the example data provided Update2: Correction on geom_smoot()
function name acoording @phi comment