# 1.2 Linear Filtering of Time Series # Example 1.2.1. (Unemployed Females Data). The series of monthly # unemployed females between ages 16 and 19 in the United States # from January 1961 to December 1985 (in thousands) is female <- read.table("female.txt") temp <- data.matrix(female) # Start making a ts object temp <- t(temp) female <- ts(temp[1:length(temp)],start=c(1961,1),freq=12) # End making a ts object plot(female) female.ma.17 <- filter(female,filter=rep(1/17,17)) lines(female.ma.17,col="red") # Example 1.2.2 unemployed1 <- read.table("unemployed1.txt") colnames(unemployed1) <- c("month","t","unemplyd") unemployed1 unemp <- ts(unemployed1$unemplyd,start=c(1975,7),freq=12) unemp.star <- filter(unemp,filter=c(0.5,rep(1,11),0.5)/12) d <- unemp - unemp.star d decompose(unemp) # Example 1.2.3. (Temperatures Data). The monthly average temperatures # near Wuerzburg, Germany were recorded from the 1st of # January 1995 to the 31st of December 2004 temperature <- read.table("temperatures.txt") temperature <- ts(temperature[,1],start=c(1995,1),freq=12) plot(temperature) decom <- decompose(temperature) lines(decom$trend,col="red") # Example 1.2.3a (Unemployment1 Data) Applying X-12 to the data unemployed1 <- read.table("unemployed1.txt") unemployed1 <- ts(unemployed1[,3],start=c(1975,7),freq=12) plot(unemployed1) decom.unemp <- decompose(unemployed1) lines(decom.unemp$trend,col="red") # Try after installing X-12-ARIMA from http://www.census.gov/srd/www/x12a/ # and istalling the package x12 library(x12) x12() # Local Polynomial Fit stl(unemployed1,s.window="periodic") plot(stl(unemployed1,s.window="periodic")) # Example 1.2.7. (Electricity Data). The following plots show the # total annual output of electricity production in Germany between 1955 # and 1979 in millions of kilowatt-hours. electric <- read.table("electric.txt") temp <- data.matrix(electric[,-(1:2)]) # Start making a ts object temp <- t(temp) electric <- ts(temp[1:length(temp)],start=c(1955,1),freq=12) # End making a ts object plot(electric) electric.sum <- read.table("electric.txt")[,-(1:2)] electric.sum <- data.matrix(electric.sum) electric.sum <- rowSums(electric.sum) plot(electric.sum,type="l") plot(diff(electric.sum),type="l") plot(diff(diff(electric.sum)),type="l") elec <- ts(electric.sum,start=1955) plot(elec) plot(filter(elec,side=1,filter=c(1,-1)),ylab="diff.elec") plot(filter(elec,side=1,filter=c(1,-2,1)),ylab="diff.diff.elec")