Search code examples
rplotloess

How to fit a smooth curve on a plot with very few points in R


I have just 4 data points:

points = c(60, 46, 46, 60)

that "want" to describe a parabola. Evidently, though, I can't find a way to make it smooth; instead, I end up with the boxy plot in red below using code along these lines:

plot(points, ylim=c(40,60), pch = 20, col = 2, cex = 2)
fit = loess(points ~ c(1:4), bw=nrd0, na.rm=T)
lines(predict(fit), lwd=2, col= 2)

I wonder if there is any way of making the corners smooth, so that it looks more like the blue line...

enter image description here


Solution

  • As stated in the message you got, loess is not happy with so little points. But you can get a nice curve using spline:

    points = c(60, 46, 46, 60)
    plot(points, ylim=c(40,60), pch = 20, col = 2, cex = 2)
    lines(spline(1:4, points, n=100))
    

    enter image description here