-
Notifications
You must be signed in to change notification settings - Fork 3
/
arima.R
30 lines (24 loc) · 976 Bytes
/
arima.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
# Author: Shalin Shah
library(forecast)
set.seed(314)
library(ggplot2)
data <- read.table("Desktop/data/TSLA.csv", sep=',', header=T)
measure <- data$Close
# the last 54 days are for testing
measure_test <- measure[200:253]
# The first 199 days are for training
measure <- measure[1:199]
# Fit an ARIMA model
fit <- arima(ts(measure), c(10, 1, 5))
fore <- forecast(fit, 54)
mean(abs(fore$mean - measure_test)) # MAE
cor(fore$mean, measure_test) # Correlation (square it to get R-squared)
p <- ggplot()
p <- p + geom_line(aes(x=1:199, y=measure, color="Original Data"))
p <- p + geom_line(aes(x=200:253, y=measure_test, color="Original Data TEST"))
p <- p + geom_line(aes(x=200:253, y=fore$mean, color="Predicted using Arima"))
p <- p + ylab("Value of the Stock")
p <- p + xlab("Time in Days")
p <- p + ggtitle("Stock Price Prediction using Arima")
p <- p + geom_ribbon(aes(x=c(200:253), y = fore$mean, ymin=fore$lower[,2], ymax=fore$upper[,2]), linetype=2, alpha=0.1)
p