Last class, we talked about different ways to smooth time series. This smoothing allows people to see the overall trend in a time series compared to the daily ups and downs of data, i.e. noise.

As such smoothing is also called denoising and it is a process to make trends pop out on plots. It is not the best since some methods of inference work better.

An easy kind of smoothing is the moving average.

Ex. Time series Yt can be smoothed to (Y(t-1)^t * Yt^t * Y(t+1)) weights= (.333,.333,.333)

To show the moving average smoothing method, I will use the package astsa and the dataset globtemp. This dataset includes the global mean land-ocean temperature deviations (from 1951-1980 average), measured in degrees centigrade, for the years 1880-2015.

To first see the moving average method in work we must see what the dataset looked like originally unsmoothed and compare it to its smoothed graph.

library(astsa)
## Warning: package 'astsa' was built under R version 3.4.3
data("globtemp")
plot(globtemp, type="o", ylab="Global Temperature Deviations")

out <- filter(globtemp, sides=2, filter=rep(1/3,3)) # moving average
par(mfrow=c(2,1))
plot.ts(globtemp, main="unsmoothed temp")
plot.ts(out, , main="smoothed temp")

As we can see the unsmoothed graph contains a lot more data points or noise than the model smoothed using moving average. The smoothed graph gives us a clue to the overall trend of the model, such as it being upward sloping.

We can also do this with other data such as what was shown below.

# Now, what do we need to do to smooth
# Johnson and Johnson quarterly earnings?
plot.ts(jj, type="o", ylab="Quarterly Earnings per Share", main="Unsmoothed")

out2 <- filter(jj, sides=2, filter=rep(1/3,3))
plot.ts(out2, type="o", ylab="Quarterly Earnings per Share",main="Smoothed")

Once again we can see the unsmoothed graph contains a lot more data points or noise than the model smoothed using moving average. The smoothed graph gives us a clue to the overall trend of the model, such as it being upward sloping.

Other Smoothers

There are many different types of smoothing. Some of these are the weighted moving average, Normal Kernal Smoother (lines(ksmooth(time(soi),soi,“normal”,)), Lowess (localized regression based on k-nearest neighbors), and Spline (localized regression based on points within a fixed width interval in time)

Weighted Moving Average

Moving average smoothing can be down with different weights. We can change the weights because moving average uses main data points and those around it and we can give move value to the main data point by add weights like shown below into the filter.

w <- c(1, 2, 1)/3
fw <- filter(globtemp, sides = 2, filter = w)
plot(globtemp, main = "Unsmoothed")

plot(fw, main = "Smoothed")

As we can see weighted moving average and moving average are very simular.

Normal Kernal Smoother

To use this smoothing, we use the code ksmooth(x, y, kernel=“normal”, bandwidth) . The x and y indicate what our x and y values are and we can find those in our data set. The bandwidth is how many points on either side of the main data point we want to be such as 4. This is shown below:

plot(globtemp)
lines(ksmooth(time(globtemp), globtemp, kernel = "normal", bandwidth = 4), lwd=5, col=6)

Lowess Smoothing

Lowess smoothing uses datapoints near the main data point to smooth the time series. We can do Lowess smoothing by using the code lowess(dataset, f), where f is our bandwidth.

plot(globtemp)
lines(lowess(globtemp, f=.4), lwd=5, col=6)

Spline Smoothing

Spline Smoothing is localized regression based on points within a fixed width interval in time. We can do spline smoothing using the code smooth.spline(x, y, spar).

plot(globtemp)
lines(smooth.spline(time(globtemp), globtemp, spar = .4), lwd=5, col = 6)

Conclusion

Smoothing of the time series datasets makes it easy to find overall trends and see if there are any cyclical trends present. Thus they are very useful to datasets that use time as a predictor variable.