###load and visualize time series data.

setwd("~/R_TRAINING")
crude_oil_prices=read.csv("murban.csv")
View(crude_oil_prices)

###convert to time series

ts_data=ts(crude_oil_prices$crude_oil_prices,start = c(2003,1),frequency = 12)
head(ts_data,36)
 [1] 29.60 31.70 29.10 25.00 25.85 26.80 27.75 28.85 26.95 29.10 29.60 30.05
[13] 31.05 30.90 33.15 33.80 37.05 35.95 37.35 41.55 41.39 42.80 39.65 38.50
[25] 42.10 44.00 50.95 52.55 49.80 55.45 57.10 60.95 61.05 58.00 54.95 56.90

##plot the time sreies

plot(ts_data,main="time series data",col="blue",lwd=2)

#####test for acf and pacf

par(mfrow=c(1,2))   
acf(ts_data,main="acf plot")   
pacf(ts_data,main="pacf plot")  

par(mfrow=c(1,1))

#check stationarity

library(tseries)
adf.test(ts_data) 

    Augmented Dickey-Fuller Test

data:  ts_data
Dickey-Fuller = -2.5885, Lag order = 6, p-value = 0.328
alternative hypothesis: stationary

####fit an arima process

library(forecast)
model=auto.arima(ts_data)
summary(model)
Series: ts_data 
ARIMA(1,1,0) 

Coefficients:
         ar1
      0.4001
s.e.  0.0578

sigma^2 = 33.12:  log likelihood = -795.02
AIC=1594.04   AICc=1594.08   BIC=1601.09

Training set error measures:
                    ME     RMSE      MAE        MPE     MAPE      MASE
Training set 0.1076132 5.732362 4.387159 0.06247943 6.884854 0.2160971
                    ACF1
Training set 0.009670956

#####focasting and evaluation ##model diagnostic

tsdiag(model)  

checkresiduals(model) 


    Ljung-Box test

data:  Residuals from ARIMA(1,1,0)
Q* = 25.535, df = 23, p-value = 0.3233

Model df: 1.   Total lags used: 24

###forecasting

forecast_values=forecast(model,h=12)  

head(forecast_values)
$method
[1] "ARIMA(1,1,0)"

$model
Series: ts_data 
ARIMA(1,1,0) 

Coefficients:
         ar1
      0.4001
s.e.  0.0578

sigma^2 = 33.12:  log likelihood = -795.02
AIC=1594.04   AICc=1594.08   BIC=1601.09

$level
[1] 80 95

$mean
          Jan      Feb      Mar      Apr      May      Jun      Jul      Aug
2024 76.63118 75.68333 75.30406 75.15230 75.09157 75.06728 75.05755 75.05366
          Sep      Oct      Nov      Dec
2024 75.05211 75.05148 75.05123 75.05113

$lower
              80%      95%
Jan 2024 69.25554 65.35111
Feb 2024 62.99297 56.27509
Mar 2024 58.17293 49.10426
Apr 2024 54.24764 43.18138
May 2024 50.90273 38.09793
Jun 2024 47.95561 33.60356
Jul 2024 45.29638 29.54177
Aug 2024 42.85569 25.81111
Sep 2024 40.58747 22.34300
Oct 2024 38.45968 19.08915
Nov 2024 36.44906 16.01430
Dec 2024 34.53813 13.09183

$upper
               80%       95%
Jan 2024  84.00683  87.91126
Feb 2024  88.37370  95.09157
Mar 2024  92.43519 101.50386
Apr 2024  96.05696 107.12321
May 2024  99.28041 112.08521
Jun 2024 102.17894 116.53099
Jul 2024 104.81872 120.57333
Aug 2024 107.25163 124.29621
Sep 2024 109.51674 127.76121
Oct 2024 111.64328 131.01381
Nov 2024 113.65341 134.08817
Dec 2024 115.56414 137.01043

##plot the forecastd values

plot(forecast_values,main ="arima forecast",col = "blue")