Let’s take DATE and Adjusted Price variables
mydata <- read.csv("ocbc.csv")
str(mydata)
## 'data.frame': 751 obs. of 7 variables:
## $ Date : Factor w/ 751 levels "1/10/2017","1/10/2018",..: 580 587 590 593 595 596 602 605 608 612 ...
## $ Open : num 10.2 10.4 10.4 10.4 10.3 ...
## $ High : num 10.3 10.4 10.4 10.4 10.4 ...
## $ Low : num 10.2 10.3 10.4 10.3 10.3 ...
## $ Close : num 10.3 10.4 10.4 10.3 10.4 ...
## $ Adj.Close: num 9.29 9.35 9.31 9.3 9.37 ...
## $ Volume : int 5817500 5037600 3915700 2517100 6111900 2759600 6103800 8404500 4301500 4724400 ...
Some samples of actual price
tail(mydata)
# convert to proper date format
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
ds <- as.Date(parse_date_time(mydata$Date, "mdy"))
y <- log(mydata$Adj.Close)
df <- data.frame(ds,y)
# display graph
library(ggplot2)
qplot(ds,y, data=df)

released by facebook
library(prophet)
## Loading required package: Rcpp
m <- prophet(df)
## Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
## Initial log joint probability = -3.32159
## Optimization terminated normally:
## Convergence detected: relative gradient magnitude is below tolerance
#prediction
# future <- make_future_dataframe(m, periods = 365) #365 days.
future <- make_future_dataframe(m, periods = 7) #gambler won't wait 365 days, want to see next 7 days
forecast <-predict(m, future)
tail(forecast[c('ds', 'yhat', 'yhat_lower', 'yhat_upper')])
Convert the log price back to original stock price. Yhat is the predicted price. What is the forecasted stock price on 2018 July 20th ?
exp(2.448888) # took the last date.
## [1] 11.57547
Predicted 30 days price
future <- make_future_dataframe(m, periods = 30)
forecast <-predict(m, future)
tail(forecast[c('ds', 'yhat', 'yhat_lower', 'yhat_upper')],30)
Plot Forecast
plot(m, forecast)

prophet_plot_components(m, forecast)
