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!
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.
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.
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.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With