June 2007
Beginner to intermediate
950 pages
27h 8m
English
The simplest way of seeing pattern in time series data is to plot the moving average. A useful summary statistic is the three-point moving average:
![]()
The function ma3 will compute the three-point moving average for any input vector x:
ma3<-function (x) {
y<-numeric(length(x)-2)
for (i in 2:(length(x)-1)) {
y[i]<-(x[i-1]+x[i]+x[i+1])/3
}
y }
A time series of mean monthly temperatures will illustrate the use of the moving average:
temperature<-read.table("c:\\temp\\temp.txt",header=T) attach(temperature) tm<-ma3(temps) plot(temps) lines(tm[2:158])

The seasonal pattern of temperature change over the 13 years of the data is clear. Note that a moving average can never capture the maxima or minima of a series (because they are averaged away). Note also that the three-point moving average is undefined for the first and last points in the series.
Read now
Unlock full access