Modeling Aluminum Price

load nessassary pakages

library(tidyverse)            
## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.4     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.0
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library('ggplot2')
library('forecast')
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library('tseries')
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

prepare data to model

convert Date to data format

daily_data = read.csv('D:/Files/PALUMUSDM.csv', header=TRUE, stringsAsFactors=FALSE)

daily_data$DATE = as.Date(daily_data$DATE)

convert the values to time series format

count_ts = ts(daily_data[, c('PALUMUSDM')])

clean time series

daily_data$clean_cnt = tsclean(count_ts)

moving avreage to to 7 step

daily_data$cnt_ma = ma(daily_data$clean_cnt, order=7) 

consider the frequency of data 30 with remove the NA values

count_ma = ts(na.omit(daily_data$cnt_ma), frequency=30)

fitting the model

automatic fitting the model using function (auto.arima)

fit<-auto.arima(count_ma ,trace = T)
## 
##  Fitting models using approximations to speed things up...
## 
##  ARIMA(2,1,2)(1,0,1)[30] with drift         : 3076.692
##  ARIMA(0,1,0)            with drift         : 3715.338
##  ARIMA(1,1,0)(1,0,0)[30] with drift         : 3121.07
##  ARIMA(0,1,1)(0,0,1)[30] with drift         : 3385.945
##  ARIMA(0,1,0)                               : 3713.363
##  ARIMA(2,1,2)(0,0,1)[30] with drift         : 3087.885
##  ARIMA(2,1,2)(1,0,0)[30] with drift         : 3074.601
##  ARIMA(2,1,2)            with drift         : 3085.806
##  ARIMA(2,1,2)(2,0,0)[30] with drift         : 3099.666
##  ARIMA(2,1,2)(2,0,1)[30] with drift         : 3100.984
##  ARIMA(1,1,2)(1,0,0)[30] with drift         : 3090.467
##  ARIMA(2,1,1)(1,0,0)[30] with drift         : 3073.441
##  ARIMA(2,1,1)            with drift         : 3084.096
##  ARIMA(2,1,1)(2,0,0)[30] with drift         : 3085.864
##  ARIMA(2,1,1)(1,0,1)[30] with drift         : 3075.519
##  ARIMA(2,1,1)(0,0,1)[30] with drift         : 3086.164
##  ARIMA(2,1,1)(2,0,1)[30] with drift         : 3087.358
##  ARIMA(1,1,1)(1,0,0)[30] with drift         : 3090.087
##  ARIMA(2,1,0)(1,0,0)[30] with drift         : 3085.307
##  ARIMA(3,1,1)(1,0,0)[30] with drift         : 3087.706
##  ARIMA(3,1,0)(1,0,0)[30] with drift         : 3086.277
##  ARIMA(3,1,2)(1,0,0)[30] with drift         : 3077.81
##  ARIMA(2,1,1)(1,0,0)[30]                    : 3071.706
##  ARIMA(2,1,1)                               : 3082.041
##  ARIMA(2,1,1)(2,0,0)[30]                    : 3083.809
##  ARIMA(2,1,1)(1,0,1)[30]                    : 3073.774
##  ARIMA(2,1,1)(0,0,1)[30]                    : 3084.097
##  ARIMA(2,1,1)(2,0,1)[30]                    : 3085.282
##  ARIMA(1,1,1)(1,0,0)[30]                    : 3088.188
##  ARIMA(2,1,0)(1,0,0)[30]                    : 3083.45
##  ARIMA(3,1,1)(1,0,0)[30]                    : 3085.983
##  ARIMA(2,1,2)(1,0,0)[30]                    : 3072.866
##  ARIMA(1,1,0)(1,0,0)[30]                    : 3119.187
##  ARIMA(1,1,2)(1,0,0)[30]                    : 3088.555
##  ARIMA(3,1,0)(1,0,0)[30]                    : 3084.428
##  ARIMA(3,1,2)(1,0,0)[30]                    : Inf
## 
##  Now re-fitting the best model(s) without approximations...
## 
##  ARIMA(2,1,1)(1,0,0)[30]                    : 3093.265
## 
##  Best model: ARIMA(2,1,1)(1,0,0)[30]

forecast using the suggested model for 30 points

fcast <- forecast(fit, h=30)

prepare the data from model to plot

get data from (cast) object to add the fitted and actual values in data frame

dattoplot<-data.frame(fitted = fcast$fitted, actual = fcast$x )
dattoplot<-dattoplot%>% add_column(act = daily_data$PALUMUSDM[c(4:368)],data=daily_data$DATE[c(4:368)])
dattoplot<-dattoplot%>% add_column(forecast = NA , up = NA , lw = NA) 
dattoplot<- dattoplot%>% add_row( forecast= fcast$mean,up = fcast$upper , lw = fcast$lower)
dattoplot$up <- as.data.frame(dattoplot$up)$"95%"
dattoplot$lw <- as.data.frame(dattoplot$lw)$"95%"
dattoplot$data<-as.Date(dattoplot$data)
dattoplot$data[c(366:395)]<-seq(as.Date("2020-12-01"),by="month",length.out = 30)

plot the modle

final <-ggplot() + 
  geom_line(data = dattoplot, aes(x = data, y = act, colour = "actual"))+
  geom_line(data = dattoplot, aes(x = data, y = fitted,   colour = "fitted value"))+
  geom_line(data = dattoplot, aes(x = data, y = forecast,   colour = "forcast value"))+
  geom_line(data = dattoplot, aes(x = data, y = up,   colour = "upper value"))+
  geom_line(data = dattoplot, aes(x = data, y = lw,   colour = "lower value"))+ xlab("Date") + ylab("Pric")
final
## Warning: Removed 30 row(s) containing missing values (geom_path).

## Warning: Removed 30 row(s) containing missing values (geom_path).
## Warning: Removed 365 row(s) containing missing values (geom_path).

## Warning: Removed 365 row(s) containing missing values (geom_path).

## Warning: Removed 365 row(s) containing missing values (geom_path).

Make the plot interactive

ggplotly(final)

:)