Question 1
SOI0710 <- read.csv("SOI2007-10.csv")
library("TTR")
sma = SMA(SOI0710$SOI,5)
SOI0710 <- cbind(SOI0710, runningMeans=c(NA,NA,sma[5:48],NA,NA))
plot(SOI0710$SOI,type="o", xaxt="n", col="BLUE",
xlab="Months from 2007", ylab="Troup SOI")
axis(1, at=c(0,13,25,37),
labels=c("Jan 2007","Jan 2008","Jan 2009","Jan 2010"))
abline(h=0)
lines(SOI0710$runningMeans, type="l", col="RED")
legend("top", legend = c("Monthly Values", "Running means"), col = c("BLUE", "RED"), lty = 1, pch = c(1, NA), bty="n", horiz=TRUE)
As the running mean calculated lowers the effects of monthly, short term, weather fluctuations, it has smaller peaks and fluctuations, making it overall less erratic and more smooth, in order to represent the larger scale fluctuations.
Question 2
## Read the data file "sst34_1997to2002.csv" into an R dataframe called ONIs
## and check it.
ONIs <- read.csv("sst34_1997to2002.csv")
# str(ONIs)
# head(ONIs)
## Look at the basic bar plot, not distinguishing different phases, and with
## a 1-unit space between bars.
barplot(ONIs$Anomaly, space=1, names=ONIs$Median.Month, cex.names = 0)
## Check the cut() function, then use it to create & check a new variable
## (phase) which uses the Anomaly value to classify each ONI into its phase.
# ?cut
ONIs$phase=with(ONIs, cut(Anomaly, breaks=c(-10,-0.5,0.5,10),
labels=c("La Nina","Neutral","El Nino")))
# str(ONIs)
## Specify the colours which will be associated with the numbers 1,2,3, so
## we can then use the factor level numbers for phase to specify the bar
## colours.
palette(c("green","lightgray","red"))
## Draw the plot with bar colours defined by the palette command.
barplot(ONIs$Anomaly, space=1, ylim=c(-2,3), col=as.numeric(ONIs$phase),
las=1, ylab="SOI Anomaly", main="ONI & El Nino phases between 1997
and 2002")
## Draw a horizontal line with 0 intercept to show the zero SOI.
abline(h=0)
## Specify the x-axis to have tick marks marking the start of each year, and
## label the ticks.
## Note that because we put a 1-unit space between each bar, there are 2 units
## per month & 24 units per year.
axis(1, at=c(0.5,24.5,48.5,72.5,96.5,120.5,144.5),
labels=c("Jan 1997","Jan 1998","Jan 1999",
"Jan 2000", "Jan 2001", "Jan 2002",
"Jan 2003"))
## Add a legend, note the fill colours are still defined by the palette command
## earlier.
legend("bottomright",legend=c("La Nina", "Neutral", "El Nino"), fill=1:3,
bty="n")
## Put a border around the plot.
box()
We see a reversal in El Nino conditions to La Nina in May to July during 1998 followed by another more gradual switch to El Nino again in May 2002. This represents the general oscillations we would expect, and how El Nino is generally quickly followed by La Nina.
In July to September during 2000, La Nina conditions weaken to neutral and it appear, potentially indicating the end of the La Nina phase, but they strengthen again, for 5 more months. This shows the importance of observing longer term trends, and the utility of features such as running means.