Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying a loess smoothing to a time series

Tags:

r

loess

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!

like image 965
Sisse Avatar asked Jul 13 '12 15:07

Sisse


People also ask

How do you smooth a time series plot?

When there is a seasonal pattern in your data and you want to remove it, set the length of your moving average to equal the pattern's length. If there is no seasonal pattern in your data, choose a length that makes sense. Longer lengths will produce smoother lines.

What is loess smoothing method?

LOWESS (Locally Weighted Scatterplot Smoothing), sometimes called LOESS (locally weighted smoothing), is a popular tool used in regression analysis that creates a smooth line through a timeplot or scatter plot to help you to see relationship between variables and foresee trends.

Can loess be used for prediction?

loess must use the data originally used to fit the loess model to compute the predictions. If you fit the loess model using the data argument, then the data set given by data should not be changed between the fit and the prediction.

What does loess smooth do in R?

Loess Smooths Loess smoothing is a process by which many statistical softwares do smoothing. In ggplot2 this should be done when you have less than 1000 points, otherwise it can be time consuming. As you can see with the code we just add method="loess" into the geom_smooth() layer.


1 Answers

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)
like image 102
plannapus Avatar answered Oct 20 '22 12:10

plannapus