Latar Belakang

Bitcoin adalah sebuah uang elektronik yang dibuat pada tahun 2009 oleh Satoshi Nakamoto. Akhir-akhir ini, Bitcoin sangat diminati oleh masyarakat. Banyak masyarakat yang secara buta membeli dan menjual Bitcoin tanpa memahami harga Bitcoin. Ini menyebabkan banyak orang mengalami kerugian bahkan bangkrut karena terobsesi dengan Bitcoin. Oleh karena itu perlu adanya peramalan harga Bitcoin di masa depan untuk mengedukasi masyarakat agar tidak terjadi kerugian berlebih. Dengan mengetahui prediksi harga bitcoin, kita bisa menentukan strategi yang tepat kapan menjual atau membeli Bitcoin untuk mendapatkan keuntungan.

Package yang Digunakan

library("dLagM")#bisa otomatis timeseries datanya
## Warning: package 'dLagM' was built under R version 4.2.1
## Loading required package: nardl
## Warning: package 'nardl' was built under R version 4.2.1
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Loading required package: dynlm
## Warning: package 'dynlm' was built under R version 4.2.1
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library("dynlm") #data harus timeseries
library("MLmetrics")
## Warning: package 'MLmetrics' was built under R version 4.2.1
## 
## Attaching package: 'MLmetrics'
## The following object is masked from 'package:dLagM':
## 
##     MAPE
## The following object is masked from 'package:base':
## 
##     Recall
library("lmtest")
library("anytime")
## Warning: package 'anytime' was built under R version 4.2.1
library("ggplot2")
library("anytime")
library("bsts")
## Warning: package 'bsts' was built under R version 4.2.1
## Loading required package: BoomSpikeSlab
## Warning: package 'BoomSpikeSlab' was built under R version 4.2.1
## Loading required package: Boom
## Warning: package 'Boom' was built under R version 4.2.1
## Loading required package: MASS
## 
## Attaching package: 'Boom'
## The following object is masked from 'package:stats':
## 
##     rWishart
## 
## Attaching package: 'BoomSpikeSlab'
## The following object is masked from 'package:stats':
## 
##     knots
## Loading required package: xts
## Warning: package 'xts' was built under R version 4.2.1
## 
## Attaching package: 'bsts'
## The following object is masked from 'package:BoomSpikeSlab':
## 
##     SuggestBurn
library("car")
## Warning: package 'car' was built under R version 4.2.1
## Loading required package: carData
#library("caret")
library("forecast")
## Warning: package 'forecast' was built under R version 4.2.1
## 
## Attaching package: 'forecast'
## The following object is masked from 'package:dLagM':
## 
##     forecast
library("keras")
## Warning: package 'keras' was built under R version 4.2.1
library("MCMCpack")
## Warning: package 'MCMCpack' was built under R version 4.2.1
## Loading required package: coda
## Warning: package 'coda' was built under R version 4.2.1
## 
## Attaching package: 'coda'
## The following object is masked from 'package:Boom':
## 
##     thin
## ##
## ## Markov Chain Monte Carlo Package (MCMCpack)
## ## Copyright (C) 2003-2022 Andrew D. Martin, Kevin M. Quinn, and Jong Hee Park
## ##
## ## Support provided by the U.S. National Science Foundation
## ## (Grants SES-0350646 and SES-0350613)
## ##
## 
## Attaching package: 'MCMCpack'
## The following objects are masked from 'package:Boom':
## 
##     ddirichlet, dinvgamma, rdirichlet, rinvgamma
library("smooth")
## Warning: package 'smooth' was built under R version 4.2.1
## Loading required package: greybox
## Warning: package 'greybox' was built under R version 4.2.1
## Package "greybox", v1.0.5 loaded.
## 
## Attaching package: 'greybox'
## The following objects are masked from 'package:MLmetrics':
## 
##     MAE, MAPE, MSE
## The following objects are masked from 'package:dLagM':
## 
##     forecast, GMRAE, MAPE, MASE
## This is package "smooth", v3.1.6
## Any thought or suggestions about the package? Have you found a bug? File an issue on github: https://github.com/config-i1/smooth/issues
library("tensorflow")
## Warning: package 'tensorflow' was built under R version 4.2.1
library("tseries")
## Warning: package 'tseries' was built under R version 4.2.1
library("TTR")
## Warning: package 'TTR' was built under R version 4.2.1
## 
## Attaching package: 'TTR'
## The following object is masked from 'package:smooth':
## 
##     lags

Data

test <- read.csv("C:/5 2022-2023/Metode Peramalan Deret Waktu/K4/test.csv")
train <- read.csv("C:/5 2022-2023/Metode Peramalan Deret Waktu/K4/train.csv")
View(test)
View(train)
test_data <- test[,2]
train_data <- train[,5]
testdata <- test[,2]
##Convert Data
train$Date <- as.Date(anytime(train$Date))
test$Date <- as.Date(anytime(test$Date))
train$Volume <- gsub(",", "", train$Volume)
train$Market.Cap <- gsub(",", "", train$Market.Cap)
train$Market.Cap <- as.numeric(train$Market.Cap)
train$Volume <- as.numeric(train$Volume)
## Warning: NAs introduced by coercion
#Difference between high and low on each day
a <- matrix(c(0), nrow = 0, ncol = 1)
for(i in 1:nrow(train)){
  a <- rbind(a, train[i,3] - train[i,4])
  i <- i + 1
}
train <- cbind(train,a)


#Volume has missing values#
#Data Manipulation#
fifty_avg <- round(mean(train$Volume[train$a < 50], na.rm = TRUE), digits = 2)
hun_avg <- round(mean(train$Volume[train$a > 50 & train$a < 100], na.rm = TRUE), digits = 2)
hf_avg <- round(mean(train$Volume[train$a > 100 & train$a < 150], na.rm = TRUE), digits = 2)
th_avg <- round(mean(train$Volume[train$a > 150 & train$a < 350], na.rm = TRUE), digits = 2)
for(i in 1:nrow(train)){
  if(is.na(train[i,6])){
    if(train$a[i] < 50){
      train$Volume[i] <- fifty_avg
    } else if(train$a[i] < 100){
      train$Volume[i] <- hun_avg
    } else if(train$a[i] < 150){
      train$Volume[i] <- hf_avg
    } else if(train$a[i] < 350){
      train$Volume[i] <- th_avg
    }else
      print("Uncaught Title")
  }
}
train <- train[, - 8] #Removing column 8
ggplot(train, aes(Date, Close)) + geom_line() + scale_x_date("year") + ylim(0,10000) + ylab("Closing Price")

## Convert Data ke Bentuk Time Series

#Convert data set to time series
Train <- xts(train[, -1], order.by = as.POSIXct(train$Date)) 
tsr <- ts(Train[,4], frequency = 365.25,start = c(2013,4,27))
plot(Train$Close,type='l',lwd = 1.5,col='red', ylim = c(0,10000), main = "Bitcoin Closing Price")

#checking for trends and seasonality
dects <- decompose(tsr) #Obtaining the trends and seasonality
plot(dects)

Pemulusan dengan DES

holtt <-  holt(Train[1289:1655,'Close'], type = "additive", damped = F) #holt forecast values
holtf <- forecast(holtt, h = 10)
holtdf <- as.data.frame(holtf)
plot(holtf, ylim = c(0,10000)) 

holtfdf <- cbind(test, holtdf[,1])
accuracy(holtdf[,1], testdata)
##                 ME     RMSE      MAE      MPE     MAPE
## Test set -412.8678 717.7703 580.2766 -6.76652 8.946933
ggplot() + geom_line(data = holtfdf, aes(Date, holtfdf[,2]), color = "blue") + geom_line(data = holtfdf, aes(Date, holtfdf[,3]), color = "Dark Red")

## Eksponensial Triple Smoothing Untuk melihat apakah data bisa diramal sekaligus untuk pemulusan

ETS <- ets((Train[,'Close'])) # ETS forecast values
ETSf <- forecast(ETS, h = 10)
etsdf <- as.data.frame(ETSf)
plot(forecast(ETS, h = 10), ylim = c(0,10000)) #ETS forecast plot works perfectly

etsp <- predict(ETS, n.ahead = 10, prediction.interval = T, level = 0.95)
accuracy(etsdf[,1], testdata)
##                 ME     RMSE      MAE       MPE     MAPE
## Test set -428.4575 725.6215 584.3209 -6.989635 9.021145

ARIMA Forecasting

tsdf <- diff(Train[,4], lag = 2)
tsdf <- tsdf[!is.na(tsdf)]
adf.test(tsdf)
## Warning in adf.test(tsdf): p-value smaller than printed p-value
## 
##  Augmented Dickey-Fuller Test
## 
## data:  tsdf
## Dickey-Fuller = -9.7528, Lag order = 11, p-value = 0.01
## alternative hypothesis: stationary
plot(tsdf, type = 1, ylim = c(-1000, 1000))

#ACF AND PACF plots
acf(tsdf)

pacf(tsdf)

gege <- arima(Train[,4], order = c(4,2,11))
gegef <- as.data.frame(forecast(gege, h = 10))
accuracy(gegef[,1], testdata)
##                 ME    RMSE      MAE       MPE     MAPE
## Test set -398.2041 694.748 565.1911 -6.525565 8.703098
gegefct <- cbind(test, gegef[,1])
plot(forecast(gege, h = 10), ylim = c(0,10000))

ggplot() + geom_line(data = gegefct, aes(Date, gegefct[,2]), color = "blue") + geom_line(data = gegefct, aes(Date, gegefct[,3]), color = "Dark Red")

Sumber :

https://www.kaggle.com/code/ara0303/forecasting-of-bitcoin-prices/notebook