You are on page 1of 5

2140807_STA651_Practical_Test_1.

R
hp

2024-01-31
# Load necessary libraries
library(datasets)
library(forecast)

## Registered S3 method overwritten by 'quantmod':


## method from
## as.zoo.data.frame zoo

# Load data
data("EuStockMarkets")

# Selecting the DAX index from EuStockMarkets dataset


dax <- EuStockMarkets[, "DAX"]

# 1. Comment on the structure of the data based on the time series


components:
# Time series plot
ts.plot(dax, main = "Time Series Plot of DAX Index")
# Autocorrelation function plot
acf(dax)
# 2. Discuss the mathematical model for the chosen data sets. Is the data
stationary in nature? Comment:
# The ACF plot shows significant autocorrelation, suggesting non-
stationarity. An ARIMA model can be used to capture the temporal
dependencies.

# 3. Calculate the moving average of order 3 and 5 and comment on the same:
# Moving average smoothing
ma3 <- ma(dax, order = 3) # Moving average smoothing of order 3
ma5 <- ma(dax, order = 5) # Moving average smoothing of order 5

# Plotting the moving averages


par(mfrow = c(1, 2))
ts.plot(ma3, main = "3-Point Moving Average")
ts.plot(ma5, main = "5-Point Moving Average")
# Comment: Moving averages smooth out short-term fluctuations, revealing
underlying trends.

# 4. Fit a suitable predictive model for the DAX index:


# Fit ARIMA model
fit <- auto.arima(dax)

## Error in polyroot(c(1, -testvec)) : root finding code failed

# Summary of the ARIMA model


summary(fit)

## Series: dax
## ARIMA(5,2,0)
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5
## -0.8187 -0.6631 -0.5053 -0.3444 -0.2231
## s.e. 0.0228 0.0287 0.0306 0.0289 0.0231
##
## sigma^2 = 1235: log likelihood = -9248
## AIC=18507.99 AICc=18508.04 BIC=18541.15
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0380591 35.07988 22.21839 0.00115049 0.816835 0.04042528
## ACF1
## Training set -0.03152913

# Obtain predicted values for the next 5 data points


predicted_values <- forecast(fit, h = 5)

# Print predicted values


print(predicted_values)

## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 1998.650 5477.071 5432.030 5522.113 5408.186 5545.957
## 1998.654 5455.437 5385.726 5525.148 5348.823 5562.051
## 1998.658 5438.930 5345.840 5532.020 5296.561 5581.299
## 1998.662 5470.902 5353.454 5588.349 5291.281 5650.522
## 1998.665 5478.529 5334.560 5622.498 5258.347 5698.710

You might also like