Smoothing

In class, we learned about the different types of smoothing for time series. Smoothing eliminates most of the noise in our data allowing us to clearly see the trend.

To demonstrate this, I will be using the globtemp data found in the astsa library.

Original data

We can began by plotting our original data.

library(astsa)
## Warning: package 'astsa' was built under R version 3.4.3
data(globtemp)
plot(globtemp)

As you can see there is a fair amount of noise in the data set. This noise doesn’t stop us from seeing the overall trend. However, this trend will be much easier to see after smoothing the data.

Smoothed data

Moving Average

The moving average method uses filtered data. First we smooth the data by using the filter(dataset, sides, filter) function. For sides you use a 1 if you only want past values in the time series or 2 if you want past and future. Next, filter will be used for weighting the data in the average and for the number of data points you want to average. For this example I used sides=2 and filer=rep(1/3,3) so that all points have the same weight. We will use the par() function to see the graphs side by side.

filter<- filter(globtemp, sides=2, filter=rep(1/3,3))
par(mfrow=c(2,1))
plot(globtemp, main="original")
plot(filter, main="smoothed")

The smoothed data makes it easier for us to see the trend.

Weighted Average

The next method we can use is weighted average to give the data points different weights. We will give the point itself the most weight and the past and future weights will be even. The only thing that changes is the filter.

weights <- c(1, 2, 1)/3
filterWeights <- filter(globtemp, sides=2, filter=weights)
par(mfrow=c(2,1))
plot(globtemp, main="original")
plot(filterWeights, main="Smoothed with Weighted Average")

We can see that the smoothed time series plot looks similar to what we got before.

Kernel Smoothing

Another smoothing technique we can use is kernel smoothing. For this technique, we can use the ksmooth(x, y, kernel, bandwidth) function. For x we will put in time(globtemp) and globtemp in for y. We will set kernel=“normal” and bandwidth=2 to use data points on both sides of the data point.

plot(globtemp)
lines(ksmooth(time(globtemp), globtemp, kernel= "normal", bandwidth = 2),lwd=2, col=2)

Lowess Smoother

The lowess method uses the function lowess(dataset, f) function. F can be set to however smooth you want the line, i used .03.

plot(globtemp)
lines(lowess(globtemp, f=.03), lwd=2, col=3)

This one is a lot more smooth then our previous attempts. Again, we can easily change that by setting f to equal a different level.

Spline

For our last smoothing method spline we use the smooth.spline(x, y, spar) function. For x we can enter time(globtemp) and for y we can enter globtemp. For the smoothing parameter I will use .1.

plot(globtemp)
lines(smooth.spline(time(globtemp), globtemp, spar=.1), lwd=2, col=4)

This plot has a little more noise then our previous one, however we can adjust spar to change that.

Conclusion

Smoothing is an important component of time series analyses. Smoothing allows us to clearly see the overall trend without all the extra noise of our data. The only thing we have to be careful with is smoothing the data too much that we lose relevant data.