Part 1

Below is the code for the moving average, hanning, smoothing spline and lowess filter. Once I can figure out why the dplR package and the XML package won’t load, or I get to a new computer, the code should run.

require(dplR)
## Loading required package: dplR
#Moving Average
setwd("/Users/shannoncall/Desktop/ESCI597A/Week04/")
noaa <- read.csv("/Users/shannoncall/Desktop/ESCI597A/Week04/noaa.shem.csv")
attach(noaa)
tsp(noaa)
## NULL
noaa.shem.ts<-ts(noaa$Series,start=c(1880,1),frequency=12)
ma50 <- filter(x=noaa.shem.ts, filter=rep(x=1/50,times=50), sides=2)
ma100 <- filter(x=noaa.shem.ts, filter=rep(x=1/100,times=100), sides=2)
ma150 <- filter(x=noaa.shem.ts, filter=rep(x=1/150,times=150), sides=2)

par(tcl=0.5,mar=rep(3,4),mgp=c(1.1,0.1,0),xaxs="i")
plot(noaa.shem.ts,type="l",xlab="Year",ylab="RWI",col="grey")
abline(h=1)
lines(ma50, col = "deeppink", lwd = 2)
lines(ma100, col = "deepskyblue3", lwd = 2)
lines(ma150, col = "darkorange", lwd = 2)
axis(3);axis(4)

Looks like southern hemisphere

Hanning Filter

series<-noaa$Series
date<-noaa$Date
han32 <- hanning(noaa.shem.ts,n=32)
han64 <- hanning(noaa.shem.ts,n=64)
han128 <- hanning(noaa.shem.ts,n=128)
          
par(tcl=0.5,mar=rep(3,4),mgp=c(1.1,0.1,0),xaxs="i")
plot(date,series,type="n",xlab="Year",ylab="RWI",col="grey")
abline(h=1)
lines(date,han32, col = "deeppink", lwd = 2)
lines(date,han64, col = "deepskyblue3", lwd = 2)
lines(date,han128, col = "darkorange", lwd = 2)
axis(3);axis(4)

The hanning filter is much smoother, however we are loosing data at the ends. Let’s use the smoothing spline where we won’t loose the data at the end of the series.

Smoothing Spline

spl32 <- ffcsaps(series,nyrs=32)
spl64 <- ffcsaps(series,nyrs=64)
spl128 <- ffcsaps(series,nyrs=128)
par(tcl=0.5,mar=rep(3,4),mgp=c(1.1,0.1,0),xaxs="i")
plot(date,series,type="l",xlab="Year",ylab="",col="grey")
points(date,series,type="l",col="grey")
abline(h=0)
lines(date,spl32, col = "magenta", lwd = 2)
lines(date,spl64, col = "gold2", lwd = 2)
lines(date,spl128, col = "dodgerblue4", lwd = 1)
axis(4)

Now the trend lines are smooth, but we haven’t lost as much information at the end. They seem to correspond pretty well with the trend in the data.
n<-length(Series)
years<-1:n
f200<-200/n
f200.lo<-lowess(x=Date,y=Series,f=f200)
residual.200<-Series/f200.lo$y

#Lowess
f32 <- 32/years
f32.lo <- lowess(x = years, y = noaa.shem.ts, f = f32)

par(tcl=0.5,mar=rep(3,4),mgp=c(1.1,0.1,0),xaxs="i")
plot(years,noaa.shem.ts,type="l",xlab="Year",ylab="RWI",col="grey")
abline(h=0)
lines(years,f32.lo$y, col = "red", lwd = 2)
axis(3);axis(4)

library(repmis)
githubURL <- "https://github.com/AndyBunn/TeachingData/raw/master/climate_Time_Series_Extravaganza.Rdata"
source_data(githubURL)
## Downloading data from: https://github.com/AndyBunn/TeachingData/raw/master/climate_Time_Series_Extravaganza.Rdata
## SHA-1 hash of the downloaded data file is:
## 5f5b467b42520fdfbb85cfc8a1fbcc45197e0e56
##  [1] "loti.ts"      "loti.zoo"     "ice.ts"       "ice.zoo"     
##  [5] "sealevel.ts"  "sealevel.zoo" "sunspots.ts"  "sunspots.zoo"
##  [9] "enso.ts"      "enso.zoo"     "amo.ts"       "amo.zoo"     
## [13] "pdo.ts"       "pdo.zoo"      "ohc.ts"       "ohc.zoo"     
## [17] "co2.ts"       "co2.zoo"
oceans3 <- cbind(ENSO=enso.ts,PDO=pdo.ts,AMO=amo.ts)
par(tcl=0.5,mar=rep(2.5,4),mgp=c(1.1,0.1,0),xaxs="i")
plot(oceans3,main="Three Ocean Patterns")

spectrum(na.omit(oceans3,log="no"))

op=par(mfrow=c(3,1)) #separ
spectrum(pdo.ts,log="no",span=5,type="h",xlab="Frequency (cycles/yr)", ylab="Spectral Density",main="Three Ocean Patterns")
spectrum(amo.ts,log="no",span=5,type="h",xlab="Frequency (cycles/yr)", ylab="Spectral Density",main="Three Ocean Patterns")
spectrum(enso.ts,log="no",span=5,type="h",xlab="Frequency (cycles/yr)", ylab="Spectral Density",main="Three Ocean Patterns")

par(op)

Aggregating

milk<-read.csv(file.choose(),header=T) attach(milk)

years<-1962:1975 milk.filter<-na.omit(filter(x=Milk$Month,filter=rep(x=1/2,times=3),sides=1))

library(repmis)
githubURL <- "https://github.com/AndyBunn/TeachingData/raw/master/climate_Time_Series_Extravaganza.Rdata"
source_data(githubURL) # will load all the objects.
## Downloading data from: https://github.com/AndyBunn/TeachingData/raw/master/climate_Time_Series_Extravaganza.Rdata
## SHA-1 hash of the downloaded data file is:
## 5f5b467b42520fdfbb85cfc8a1fbcc45197e0e56
##  [1] "loti.ts"      "loti.zoo"     "ice.ts"       "ice.zoo"     
##  [5] "sealevel.ts"  "sealevel.zoo" "sunspots.ts"  "sunspots.zoo"
##  [9] "enso.ts"      "enso.zoo"     "amo.ts"       "amo.zoo"     
## [13] "pdo.ts"       "pdo.zoo"      "ohc.ts"       "ohc.zoo"     
## [17] "co2.ts"       "co2.zoo"
noaa.shem <- read.csv("noaa.shem.csv")
attach(noaa.shem)
## The following objects are masked from noaa:
## 
##     Date, Date.Decimal, Series
noaa.shem.ts<-ts(noaa.shem$Series,start=c(1800,1),end=c(2016,2),frequency=12)

ma32 <- filter(x=noaa.shem.ts, filter=rep(x=1/32,times=32), sides=2)
ma64 <- filter(x=noaa.shem.ts, filter=rep(x=1/64,times=64), sides=2)
ma128 <- filter(x=noaa.shem.ts, filter=rep(x=1/128,times=128), sides=2)
plot(noaa.shem.ts,type="l",xlab="Year",ylab="Southern Hemisphere Temp Anomaly",col="grey")
abline(h=0)
lines(ma32,col="deeppink", lwd=2)
lines(ma64,col="deepskyblue3", lwd=2)
lines(ma128,col="darkorange", lwd=2)