Sisse
Sisse

Reputation: 291

Applying a loess smoothing to a time series

I would like to smooth a time curve, that I have plotted, by applying a loess function, but I can't get it to work. An example:

mydat <- runif(50)
day1 <- as.POSIXct("2012-07-13", tz = "UTC")
day2 <- day1 + 49*3600*24
pdays <- seq(day1, day2, by = "days")
lo <- loess(mydat ~ pdays)

I get the following message:

Error: NA/NaN/Inf in foreign function call (arg 2)

Is it possible to apply a loess smoothing to a time series

Any help or guidance is greatly appreciated!

Upvotes: 6

Views: 11358

Answers (1)

plannapus
plannapus

Reputation: 18749

I think the idea here is to convert your time series in a numerical form (using as.numeric) so you can perform the operation.

mydat <- runif(50)
day1 <- as.POSIXct("2012-07-13", tz = "UTC")
day2 <- day1 + 49*3600*24
pdays <- seq(day1, day2, by = "days")
lo <- loess(mydat ~ as.numeric(pdays))

# And then if you want to plot the result:
plot(pdays,mydat)
lines(pdays, lo$fitted)

Upvotes: 15

Related Questions