In this recipe, we will develop a step-by-step 2-year forecast of the Fiat-Chrysler Automotive stock price.
This task will be accomplished by applying the Arima modeling technique to FCA stock time series.
Arima (Autoregressive integrated moving average) models basically involve the estimation of an autoregressive model and a moving average, employed to estimate both the stochastic part and the underlying trend.
This recipe is mainly based on the tseries
package and forecast
package, the first for Arima model fitting and the second for prediction of future values. We will also need the quantmod
package in order to download stock data from Yahoo Finance.
We therefore need to install and load these three packages:
install.packages(c("tseries","forecast","quantmod")) library(tseries) library(forecast) library(quantmod)
sp500 <- new.env() stocks <- getSymbols(c("FCA"), env = sp500, from = as.Date("2015-01-01"), to = as.Date("2015-10-31")) FCA <- sp500$FCA FCA <- get("FCA",envir = sp500) FCA <- with(sp500, FCA) FCA <- as.data.frame(FCA) FCA <- FCA$FCA.Adjusted
time_series <- 100 * diff(log(FCA))
time_series_train <- time_series[1:(0.9 * length(time_series))] # Train dataset
arima_fit <- arima(time_series_train, order = c(2, 0, 2)) arima_forecast <- forecast(arima_fit)
plot(arima_forecast, main = "ARMA forecasts FCA returns")
Let's take a look at the following graph: