setup
library(data.table)
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.1 ✔ stringr 1.5.1
## ✔ lubridate 1.9.4 ✔ tibble 3.3.0
## ✔ purrr 1.1.0 ✔ tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::between() masks data.table::between()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::first() masks data.table::first()
## ✖ lubridate::hour() masks data.table::hour()
## ✖ lubridate::isoweek() masks data.table::isoweek()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::last() masks data.table::last()
## ✖ lubridate::mday() masks data.table::mday()
## ✖ lubridate::minute() masks data.table::minute()
## ✖ lubridate::month() masks data.table::month()
## ✖ lubridate::quarter() masks data.table::quarter()
## ✖ lubridate::second() masks data.table::second()
## ✖ purrr::transpose() masks data.table::transpose()
## ✖ lubridate::wday() masks data.table::wday()
## ✖ lubridate::week() masks data.table::week()
## ✖ lubridate::yday() masks data.table::yday()
## ✖ lubridate::year() masks data.table::year()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(TTR)
Exercise 1: Insert the graph you produced in your workshop this week from exercise 6 in Chapter 7 that has the SOI values plotted with the running 5 monthly mean SOI superimposed.
SOIdata <- read_csv("/Users/benjaminrowe/Library/CloudStorage/OneDrive-JamesCookUniversity/UniversityOD/2025/Trimester3/SC1109/Workshop/SOI2007-10.csv")
## Rows: 48 Columns: 3
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (3): Year, Month, SOI
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
SOIdata2 <- SOIdata %>% mutate(MonthContinuum = seq(1,48))
SOIdata3 <- SOIdata2 %>% mutate(RunningMeans = SMA(SOIdata2$SOI,n=5))
ggplot(data=SOIdata3)+
geom_line(aes(x=MonthContinuum, y=SOI, colour = "SOI"))+
geom_line(aes(x=MonthContinuum, y=RunningMeans, colour = "Running Mean"))+
labs(colour = "Legend")
## Warning: Removed 4 rows containing missing values or values outside the scale range
## (`geom_line()`).
The curve for running mean is “smoother” in the sense that it is not as sharply affected between months.
Exercise 2: Insert the graph you produced in your workshop this week from exercise 7 in Chapter 7.
ONIdata <- read_csv("/Users/benjaminrowe/Library/CloudStorage/OneDrive-JamesCookUniversity/UniversityOD/2025/Trimester3/SC1109/Workshop/sst34_1997to2002.csv")
## Rows: 73 Columns: 4
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (2): Median Month, MONTHS
## dbl (2): Anomaly, YEAR
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#run following lines at once
ONIdata$phase=with(ONIdata, cut(Anomaly, breaks=c(-10,-0.5,0.5,10), labels=c("La Nina", "Neutral", "El Nino")))
palette(c("green", "lightgrey", "red"))
barplot(ONIdata$Anomaly, space=1, ylim=c(-2,3), col=as.numeric(ONIdata$phase), las=1, ylab="SOI Anomaly", main="ONI and El Nino Phases Between 1997 and 2002")
abline(h=0)
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"))
legend("bottomright", legend=c("La Nina", "Neutral", "El Nino"), fill=1:3, bty="n")
box()
Changes between La Nina and El Nino are always gradual and occurr over a period of time rather than month to month. After 5 years the graph seems to reurn to where it started, entering El Nino again. This means the pattern could likely repeat over many years. El Nino -> La Nina -> El Nino -> La Nina