1. Load Libraries
library(quantmod)
## Loading required package: xts
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(forecast)
library(tseries)
2. Download Data from Yahoo Finance
ticker <- "ENEL.MI"
data <- getSymbols(ticker, src="yahoo", auto.assign=FALSE)
price <- data[,6]
head(price)
## ENEL.MI.Adjusted
## 2007-01-02 2.199554
## 2007-01-03 2.205154
## 2007-01-04 2.195354
## 2007-01-05 2.179952
## 2007-01-08 2.160351
## 2007-01-09 2.174352
3. Compute Monthly Log Returns
monthly_price <- to.monthly(price, indexAt="lastof", drop.time=TRUE)
monthly_close <- monthly_price[,4]
returns <- diff(log(monthly_close)) * 100
returns <- na.omit(returns)
head(returns)
## price.Close
## 2007-02-28 -2.810044
## 2007-03-31 1.446132
## 2007-04-30 4.097166
## 2007-05-31 1.309536
## 2007-06-30 -2.320569
## 2007-07-31 -5.013916
y <- ts(as.numeric(returns), frequency=12)
4. Plot Monthly Returns
plot(y, main="Monthly Log Returns", ylab="Return (%)")

5. Descriptive Statistics
mean(y)
## [1] 0.6538942
sd(y)
## [1] 6.39916
var(y)
## [1] 40.94925
6. T-Test for Zero Mean
t.test(y)
##
## One Sample t-test
##
## data: y
## t = 1.5463, df = 228, p-value = 0.1234
## alternative hypothesis: true mean is not equal to 0
## 95 percent confidence interval:
## -0.1793356 1.4871239
## sample estimates:
## mean of x
## 0.6538942
7. Stationarity Test (ADF Test)
adf.test(y)
## Warning in adf.test(y): p-value smaller than printed p-value
##
## Augmented Dickey-Fuller Test
##
## data: y
## Dickey-Fuller = -5.5855, Lag order = 6, p-value = 0.01
## alternative hypothesis: stationary
8. Fit ARIMA Model
fit <- auto.arima(y, seasonal=FALSE)
summary(fit)
## Series: y
## ARIMA(0,0,0) with non-zero mean
##
## Coefficients:
## mean
## 0.6539
## s.e. 0.4219
##
## sigma^2 = 40.95: log likelihood = -749.5
## AIC=1503 AICc=1503.05 BIC=1509.86
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 4.695399e-13 6.385173 5.032642 108.5337 113.9605 0.6785761
## ACF1
## Training set 0.05160885
9. Residual Diagnostics
checkresiduals(fit)

##
## Ljung-Box test
##
## data: Residuals from ARIMA(0,0,0) with non-zero mean
## Q* = 10.372, df = 24, p-value = 0.9928
##
## Model df: 0. Total lags used: 24
10. Forecast 12 Months Ahead
fc <- forecast(fit, h=12)
plot(fc, main="ARIMA Forecast")

11. Forecast Accuracy
accuracy(fc)
## ME RMSE MAE MPE MAPE MASE
## Training set 4.695399e-13 6.385173 5.032642 108.5337 113.9605 0.6785761
## ACF1
## Training set 0.05160885