MATH1318 Time Series Ass02

Student names, numbers

Student name Student number
KongHung Li 4130479

Input data

## # A tibble: 6 × 4
##   `ASX price` `Gold price` `Crude Oil (Brent)_USD/bbl` `Copper_USD/tonne`
##         <dbl>        <dbl>                       <dbl>              <dbl>
## 1       2935.         612.                        31.3               1650
## 2       2778.         603.                        32.6               1682
## 3       2849.         566.                        30.3               1656
## 4       2971.         539.                        25.0               1588
## 5       2980.         549.                        25.8               1651
## 6       3000.         536.                        27.6               1685

descriptive analysis

descriptive_analysis(asxTs, title = "ASX Price", data_variable = "ASX Price", data_time = "Time(Month)")
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    2778    4325    4929    4813    5448    6779 
## [1] "Sd : 894.030093829884"

descriptive_analysis(goldTs, title = "Gold Price", data_variable = "Gold Price", data_time = "Time(Month)")
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   520.9   825.9  1363.6  1202.0  1546.8  1776.3 
## [1] "Sd : 401.229120349339"

descriptive_analysis(oilTs, title = "Crude Oil Price", data_variable = "Crude Oil Price", data_time = "Time(Month)")
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   25.02   47.23   70.80   73.63  106.98  133.90 
## [1] "Sd : 30.0894213015542"

descriptive_analysis(goldTs, title = "Copper Price", data_variable = "Copper Price", data_time = "Time(Month)")
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   520.9   825.9  1363.6  1202.0  1546.8  1776.3 
## [1] "Sd : 401.229120349339"

Appendix: All Code Used in This Report

knitr::opts_chunk$set(echo = TRUE)
rm(list = ls())

# ---- Load Libraries ----
library(TSA)       # time series analysis
library(dplyr)     # %>% usage        
library(readr)     # read csv file
library(ggplot2)
library(forecast)
library(lmtest)
library(FSAdata)
library(tseries)
library(tidyverse)
library(kableExtra)

library(x12)
library(dLagM)
library(expsmooth)
library(dynlm)

library(car)




# Add your names, numbers and percentage of your contribution here.

na<- c("KongHung Li")
no<- c("4130479")


s<- data.frame(cbind(na,no))
colnames(s)<- c("Student name", "Student number")

s %>% kbl() %>%
  kable_classic(full_width = F, html_font = "Cambria")



ASX_data <- read_csv("ASX_data.csv")
head(ASX_data)
#plot(ASX_data$`Gold price`)


asxTs <- ts(ASX_data$`ASX price`, start = c(2004, 1), frequency = 12) # monthly data
goldTs <- ts(ASX_data$`Gold price`, start = c(2004, 1), frequency = 12) # monthly data
oilTs <- ts(ASX_data$`Crude Oil (Brent)_USD/bbl`, start = c(2004,1), frequency = 12)
copperTs <- ts(ASX_data$`Copper_USD/tonne`, start = c(2004, 1), frequency = 12)




acf_pacf_test <- function(dataTs, title){
  if(!is.ts(dataTs)) stop("Input must be a time series object")
  if(!is.character(title)) stop("Title must be a character string")
  
  
  acf(dataTs, main = paste("ACF of", title))
  
  pacf(dataTs, main = paste("PACF of", title))

}

descriptive_analysis <- function(dataTs, title, data_variable, data_time){
  # Validation
  if(!is.ts(dataTs)) stop("Input must be a time series object")
  if(!is.character(title)) stop("Title must be a character string")
  if(!is.character(data_variable)) stop("yaxis must be a character string")
  if(!is.character(data_time)) stop("xaxis must be a character string")
  
  
  # statistical summary
  summary_table <- summary(dataTs)
  print(summary_table)
  print( paste("Sd :", as.character(sd(dataTs))))
  
  par(mfrow = c(1,1))
  
  # graphical presentation of the time series
  plot(dataTs, main = paste(title, 'Time Series'), ylab = data_variable, col = "blue")
  
  
  par(mfrow = c(2,2))
  
  # histogram  
  hist(dataTs, col = "darkblue", 
       main = paste("Histogram of" , title),
       xlab = "Price (USD)",
       freq = FALSE)
  
  # density plot ~ histogram
  lines(density(dataTs), col = "red", lwd = 2)
  
  
  # scatter plot of consecutive time period, see relationships
  Y_t <- dataTs[2:length(dataTs)]
  Y_t_1 <- dataTs[1:(length(dataTs) - 1)]
  
  ## Create a scatter plot
  plot(Y_t_1, Y_t,
       xlab = expression(Y[t-1]),
       ylab = expression(Y[t]),
       main = "Scatter Plot of Y[t] vs Y[t-1]",
       col = "blue", pch = 19)
  model <- lm(Y_t ~ Y_t_1)

  # Add regression line
  abline(model, col = "red", lwd = 2)
  
  
  # acf & pacf plot
  acf_pacf_test(dataTs, title)
}




descriptive_analysis(asxTs, title = "ASX Price", data_variable = "ASX Price", data_time = "Time(Month)")


descriptive_analysis(goldTs, title = "Gold Price", data_variable = "Gold Price", data_time = "Time(Month)")



descriptive_analysis(oilTs, title = "Crude Oil Price", data_variable = "Crude Oil Price", data_time = "Time(Month)")


descriptive_analysis(goldTs, title = "Copper Price", data_variable = "Copper Price", data_time = "Time(Month)")