-
Notifications
You must be signed in to change notification settings - Fork 0
/
_083_arima_model.R
executable file
·55 lines (43 loc) · 1.54 KB
/
_083_arima_model.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# Filename: _083_arima_model.R
# Title: Arima Model in R
# Author: Raghava | GitHub: @raghavtwenty
# Date Created: August 4, 2024 | Last Updated: August 4, 2024
# Language: R | Version: 4.4.0
# Install and load necessary packages
install.packages("readxl")
install.packages("forecast")
install.packages("ggplot2")
install.packages("tseries")
# Load required library
library(readxl)
library(forecast)
library(ggplot2)
library(tseries)
# Read the data from the .xlsx file
file_path <- "datasets/arima_data.xlsx"
gross <- read_excel(file_path)
# Time series object conversion
grosstime <- ts(gross$`Gross price`, start = min(gross$Year), frequency = 1)
# Plot the time series data
plot(grosstime, main = "Gross Price Over Time", xlab = "Year", ylab = "Gross Price")
# ACF and PACF plots
acf(as.numeric(grosstime), main = "ACF of Gross Price")
pacf(as.numeric(grosstime), main = "PACF of Gross Price")
# Augmented Dickey-Fuller Test
adf_result <- adf.test(grosstime)
print(adf_result)
# ARIMA model
grossmodel <- auto.arima(grosstime, ic = "aic", trace = TRUE)
summary(grossmodel)
# Plot residuals ACF and PACF
acf(residuals(grossmodel), main = "ACF of ARIMA Residuals")
pacf(residuals(grossmodel), main = "PACF of ARIMA Residuals")
# Forecast the next 10 years
mygrossforecast <- forecast(grossmodel, level = c(95), h = 10)
plot(mygrossforecast)
# Ljung-Box test on residuals
lb_test_3 <- Box.test(residuals(grossmodel), lag = 3, type = "Ljung-Box")
lb_test_6 <- Box.test(residuals(grossmodel), lag = 6, type = "Ljung-Box")
# Final Show
print(lb_test_3)
print(lb_test_6)