1

I believe that this plot should look like a smooth curve, but my plot is showing straight connected lines. Can someone point out my error?

Y <- c(0,2,-1,4)
t = c(1:4)
plot(t,Y)
c = smooth.spline(t,Y,lambda=1)
lines(t,c$y)

Thank you!

Johnny
  • 59
  • 5
  • I am guessing you don't have enough datapoints. Use the spar argument for smoothing. See related, possible duplicate post: https://stackoverflow.com/q/3480388/680068 – zx8754 May 11 '23 at 07:33
  • @Johnny did my response below resolve your problem? If so, please up vote my response! :) – Andy May 11 '23 at 14:49

1 Answers1

0

You can solve this issue by using the loess() command.

Here you go:


Y <- c(0,2,-1,4)
t = c(1:4)
plot(t,Y)
lo <- loess(Y~t)
lines(predict(lo),col="blue", lwd=1.5)

enter image description here

Alternatively, you can use the qplot() function, found in library(ggplot2)

library(ggplot2)
qplot(t,Y, geom='smooth', span =0.5)

enter image description here

Andy
  • 413
  • 2
  • 15