I opted for an ARIMA(p,d,q) model, ARIMA is an acronym for AutoRegressive Integrated Moving Average model. \(p\) is the order of the autoregressive part; \(d\) degree of the differencing involved; \(q\) order of the moving average part.
RequireOrInstall <- function(packages,ifquietly=T) {
packages <- strsplit(packages,',')
for(package in packages){
suppressPackageStartupMessages({
if (!require(package,character.only=TRUE)) {
install.packages(package, dep=TRUE)
require(package,character.only=TRUE,quietly=ifquietly)
}})
}
}
RequireOrInstall("quantmod")
from.dat <- as.Date("01/01/08", format="%m/%d/%y")
to.dat <- as.Date("11/28/16", format="%m/%d/%y")
getSymbols("TSLA", src="google", from = from.dat, to = to.dat) # src="google" means google finance dataset
## [1] "TSLA"
head(TSLA)
## TSLA.Open TSLA.High TSLA.Low TSLA.Close TSLA.Volume
## 2010-06-29 19.00 25.00 17.54 23.89 18783276
## 2010-06-30 25.79 30.42 23.30 23.83 17194394
## 2010-07-01 25.00 25.92 20.27 21.96 8229863
## 2010-07-02 23.00 23.10 18.71 19.20 5141807
## 2010-07-06 20.00 20.00 15.83 16.11 6879296
## 2010-07-07 16.40 16.63 14.98 15.80 6924914
# In this case, the 'Volume' data in GOOG has NAs
TSLA <- subset(TSLA, select = TSLA.Close)
# so eliminating 'Volume' solves that for this example.
mTesla <- to.monthly(TSLA) # save as montly time series
#teslaClose <- Cl(mTesla) # take just the closing information
tesla <- Cl(TSLA) # get the close prices
tesla <- c(tesla, ts(189.6, Sys.Date()))
mseries <- ts(mTesla,frequency=12)
x <- tesla
ts1 <- ts(tesla,frequency=12)
# decompose(ts1)
RequireOrInstall("forecast")
teslafit <- auto.arima(tesla, d=NA, D=NA, max.p=5, max.q=10,
max.P=5, max.Q=5, max.order=5, max.d=5, max.D=5,
start.p=2, start.q=2, start.P=1, start.Q=1,
stationary=FALSE, seasonal=TRUE,
ic=c("aicc", "aic", "bic"), stepwise=TRUE, trace=FALSE,
approximation=(length(x)>100 | frequency(x)>12), xreg=NULL,
test=c("kpss","adf","pp"), seasonal.test=c("ocsb","ch"),
allowdrift=TRUE, allowmean=TRUE, lambda=NULL, biasadj=FALSE,
parallel=FALSE, num.cores=2)
teslaprediction <- forecast(teslafit)
teslaprediction
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 1619 189.7025 184.0976 195.3073 181.1306 198.2743
## 1620 189.8050 181.8785 197.7314 177.6825 201.9274
## 1621 189.9074 180.1996 199.6153 175.0605 204.7543
## 1622 190.0099 178.8002 201.2196 172.8662 207.1536
## 1623 190.1124 177.5796 202.6452 170.9451 209.2797
## 1624 190.2149 176.4859 203.9439 169.2182 211.2116
## 1625 190.3174 175.4883 205.1464 167.6383 212.9964
## 1626 190.4198 174.5670 206.2727 166.1749 214.6647
## 1627 190.5223 173.7078 207.3368 164.8067 216.2379
## 1628 190.6248 172.9007 208.3489 163.5182 217.7314
acf(teslaprediction$residuals)
summary(teslafit)
## Series: tesla
## ARIMA(0,1,0) with drift
##
## Coefficients:
## drift
## 0.1025
## s.e. 0.1087
##
## sigma^2 estimated as 19.13: log likelihood=-4679.9
## AIC=9363.81 AICc=9363.81 BIC=9374.58
##
## Training set error measures:
## ME RMSE MAE MPE MAPE MASE
## Training set 0.0000147018 4.370777 2.622472 -0.118101 2.290683 0.9994692
## ACF1
## Training set 0.03093596
We see that the residuals look like white noise. The automatically learned model is ARIMA(0,1,0). Here – autoregressive order is 0 (no lag), d order is 1 (1st order differences) and moving average order 0. Evaluation criteria are “aicc”, “aic”, “bic” and I went without Box-Cox transformation right now. There are a lot of parameters to tune even in ‘auto-arima()’ function.
Approach Taken
The prediction for would be 189.8050, but the confidence intervals are quite wide. I’ll stick with my prediction right now. There is an interesting topic that could be of my interest later after the course Link
The decompose()
function is from the ‘stats’ package