# The first time you use R (and perhaps after that as well) you will want to enter these three commands: install.packages("astsa") require(astsa) help(package = astsa) # birth is an included series # If the times were not alrready included one could add them: # birth = ts(birth, frequency = 12, start = 1948) # to get years, rather than months, as the units ## Compute a centred moving average: ma = filter(birth, sides=2, c(.5, rep(1,11), .5)/12) # moving average ## To better undersand what this command does, look at the vector c(.5, rep(1,11), .5): c(.5, rep(1,11), .5) ## Make some plots: par(mfrow=c(2,1)) # A 2 by 1 panel of plots plot(birth, main="births") plot(ma, ylim=c(min(birth),max(birth)), main="moving average") # now try this: win.graph() par(mfrow=c(2,1)) ts.plot(birth, ma, lty=2:1, col=1:2, lwd=1:2, main="births with moving average superimposed") residuals = birth - ma plot(residuals, main = "residuals from moving average")