I have an array with dates as indices which I'm plotting. I'd like to plot a LOESS curve along with it. However, the input for loess is a formula. Is there a good way to define a formula from array index to value which I can then give to the loess function?
Check out the help page for loess()
- it has a couple of examples of specifying the formula. Basically, you need to put your data into a data.frame
object with variables given appropriate names, then the formula will be y ~ x
, where x
and y
are the names of the variables you want on the x- and y-axis, respectively.
I prefer the function lowess()
, which is a faster, simpler alternative. It has fewer adjustable parameters than loess()
but it just as good in many applications.
Here are some links describing the differences between the two functions.
Below is a simple example for both loess()
and lowess()
## create an example data set
x <- sort(rpois(100,10) + rnorm(100,0,2))
y <- x^2 + rnorm(100,0,7)
df <- data.frame(x = x,y = y)
plot(x,y)
## fit a lowess and plot it
l.fit1 <- lowess(x,y,f = 0.3)
lines(l.fit1, col = 2,lwd = 2)
## fit a loess and plot it
l.fit2 <- loess(y ~ x, data = df)
lines(x,predict(l.fit2,x), col = 3,lwd = 2)
There are a number of ways to do what you want; the simplest might be to use 'scatter.smooth' which more or less packs everything you need (data plot, loess fit, and curve plot) in a single functional call.
data(AirPassengers) # a monthly time series supplied w/ base R install
scatter.smooth( x=1:length(AP),
y = as.vector(AP),
pch=20, # this line and lines below are just aesthetics
col="orange",
lty="dotted",
lwd=1.5,
xlab="")