price <- read.csv("BBNI.csv")
price
Because in time series variable is using time and y value, so I will select date and closing price for creating model
library(dplyr)
price <- price %>%
select(Date,Close)
price
As I can see on the table above that data type of Date should be change from chr to date. Data type of column Close price should also be changed from chr to float. I can change the data type as follows:
library(lubridate)
price$Date <- ymd(price$Date)
price$Close <- as.double(price$Close)
price$Day <- wday((price$Date))
price$Day.Name <- wday((price$Date),label=TRUE)
price
The table is missing Saturday and Sunday value, so we can do padding on the data frame
library(padr)
price <- price %>%
pad(interval = "day")
price
I should fill NA value with Closing price value on Friday using fill function
library(tidyr)
price <- price %>%
fill(Close) %>%
mutate(Day = wday(Date)) %>%
mutate(Day.Name=wday(Date,label = TRUE))
price
Make sure there is no missing value in the dataframe price
colSums(is.na(price))
## Date Close Day Day.Name
## 0 0 0 0
Just making sure if the data is well order by using arrange function as below:
# arrange data based on date
price <- price %>%
arrange(Date)
Now I should change data type to time series by using function ‘ts()’. I will have some parameter inside the data ts: 1. data= y value that want to be observed is Close price 2. start= start time from time series data is 18 March 2020 3. frequency= how many data in 1 season
library(forecast)
price_ts <- ts(price$Close,start=ymd("2020-03-18"),frequency = 28)
autoplot(price_ts,series="Actual")
## Decompose The function of decompose is to get time series component, which are: 1. Trend is describing about general data patterns tend to go up or down. If the trend still has a pattern (fluctuation) it means that there is still a pattern that has not been caught properly. 2. Seasonal is a seasonal pattern that forms a repeating pattern over a fixed period of time 3. Error is decribing about patterns that can not be caught in trends and seasonal
# decompose
price_dc <- decompose(x = price_ts)
# visualize decomposition result
price_dc %>% autoplot()
# Cross Validation
# your code here
# 2 tahun terakhir untuk test
price_test <- tail(price_ts, 28)
# sisanya adalah data train
price_train <- head(price_ts, length(price_ts) - length(price_test))
# your code here
price_train %>%
decompose() %>%
autoplot()
trend = UPtrend seasonality = yes
model= Holt-Winters
# your code here
model_hw <- HoltWinters(x = price_train)
# your code here
library(tseries)
model_arima <- stlm(price_train,method = "arima")
# your code here
model_hw_forecast <- forecast(object = model_hw, h = 28)
model_arima_forecast <- forecast(object=model_arima, h=28)
# model evaluation
# menghitungkan error saat memprediksi data train dan data test
accuracy(model_hw_forecast$mean, price_test)
## ME RMSE MAE MPE MAPE ACF1 Theil's U
## Test set 38.22911 129.2349 104.4943 0.6053103 1.710254 0.7044968 1.354475
# menghitungkan error saat memprediksi data test
accuracy(model_arima_forecast$mean, price_test)
## ME RMSE MAE MPE MAPE ACF1 Theil's U
## Test set 28.02444 122.1333 105.6219 0.436419 1.729722 0.6982617 1.278567
library(forecast)
library(TTR)
library(fpp)
library(tseries)
library(TSstudio)
# visualize forecast result
test_forecast(actual = price_ts, forecast.obj = model_hw_forecast, train = price_train, test = price_test)
# visualize forecast result
test_forecast(actual = price_ts, forecast.obj = model_arima_forecast, train = price_train, test = price_test)
# Conclusion Predict price in the next 1month (28days). 28+28=56 I will use model holt winters because it has smaller MAPE than model arima
# your code here
model_hw_forecast_2months <- forecast(object = model_hw, h = 56)
plot(model_hw_forecast_2months$mean)
Conclusion is BBNI price will go to 6300 at data number 47. Data number 1 until 28 is a datatest until March 21st,2021. So it means that BBNI will touch ~6300 in the next 47-28=19 days after March 21st,2021.
BBNI will touch 6300 at around April 9th, 2021
(Data is using stock price of BBNI)
model_hw_forecast_2months$mean
## Time Series:
## Start = c(18351, 4)
## End = c(18353, 3)
## Frequency = 28
## [1] 6015.586 6039.569 6027.020 6040.771 6036.995 5998.900 5945.288 5909.045
## [9] 5858.515 5962.136 5925.157 5953.977 5863.486 5947.525 6008.987 6054.488
## [17] 6121.488 6173.870 6228.150 6167.782 6093.319 6057.475 6028.361 6030.154
## [25] 6027.296 6079.463 6119.127 6090.654 6106.240 6130.223 6117.674 6131.425
## [33] 6127.649 6089.554 6035.942 5999.699 5949.169 6052.790 6015.811 6044.631
## [41] 5954.140 6038.179 6099.641 6145.143 6212.142 6264.524 6318.804 6258.436
## [49] 6183.973 6148.129 6119.016 6120.808 6117.950 6170.117 6209.782 6181.308