-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprediction.R
More file actions
167 lines (101 loc) · 3.49 KB
/
prediction.R
File metadata and controls
167 lines (101 loc) · 3.49 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
install.packages(c("httr", "jsonlite", "tidyverse", "forecast", "prophet"))
library(httr)
library(jsonlite)
library(tidyverse)
install.packages("dplyr")
library(dplyr)
install.packages("ggplot2")
install.packages("forecast")
library(ggplot2)
library(forecast)
url <- "https://api.binance.com/api/v3/klines?symbol=BTCUSDT&interval=1h&limit=100"
response <- GET(url)
data <- fromJSON(content(response, "text"))
data <- fromJSON(content(response, "text"))
crypto_prices <- data %>%
as.data.frame() %>%
select(1, 2) %>%
rename(timestamp = V1, price = V2) %>%
mutate(timestamp = as.POSIXct(timestamp / 1000, origin = "1970-01-01"),
price = as.numeric(price))
str(data)
View(data)
crypto_prices <- data %>%
as.data.frame() %>%
select(1, 2) %>%
rename(timestamp = V1, price = V2) %>%
mutate(
timestamp = as.numeric(timestamp) / 1000,
timestamp = as.POSIXct(timestamp, origin = "1970-01-01"),
price = as.numeric(price)
)
str(data)
library(ggplot2)
ggplot(crypto_prices, aes(x = timestamp, y = price)) +
geom_line(color = "blue") +
labs(title = "Bitcoin Price Over Time", x = "Time", y = "Price (USDT)") +
theme_minimal()
library(forecast)
crypto_ts <- ts(crypto_prices$price, frequency = 24)
forecast::autoplot(crypto_ts) +
labs(title = "Bitcoin Price Time Series", x = "Time", y = "Price")
print(crypto_ts)
class(crypto_ts)
library(forecast)
arima_model <- auto.arima(crypto_ts)
summary(arima_model)
forecasted <- forecast(arima_model, h = 24)
print(forecasted)
autoplot(forecasted) +
labs(title = "Bitcoin Price Prediction (ARIMA)", x = "Time", y = "Price") +
theme_minimal()
autoplot(forecasted) +
labs(title = "Bitcoin Price Prediction (ARIMA)", x = "Time", y = "Price") +
theme_light()
forecasted <- forecast(arima_model, h = 74)
print(forecasted)
autoplot(forecasted) +
labs(title = "Bitcoin Price Prediction (ARIMA)", x = "Time", y = "Price") +
theme_dark()
accuracy(forecasted)
checkresiduals(arima_model)
checkresiduals(arima_model) #Line not working for some reason, not showing the histogram it is supposed to plot
res <- residuals(arima_model)
hist(res, main = "Residual Histogram", col = "lightblue", breaks = 20)
acf(res, main = "Residual Autocorrelation")
Box.test(res, lag = 10, type = "Ljung-Box")
#further fine tuning the ARIMA model
library(forecast)
arima_tuned <- auto.arima(crypto_ts,
stepwise = FALSE,
approximation = FALSE,
seasonal = TRUE)
summary(arima_tuned)
aic_old <- AIC(arima_model)
aic_new <- AIC(arima_tuned)
print(paste("Old ARIMA AIC:", aic_old))
print(paste("New ARIMA AIC:", aic_new))
print(paste("AIC Improvement:", aic_old - aic_new))
print(accuracy(forecast_old))
print(accuracy(forecast_new))
arima_manual <- Arima(crypto_ts, order = c(2,1,2), seasonal = c(1,0,1))
summary(arima_manual)
AIC(arima_manual)
forecast_final <- forecast(arima_manual, h = 24)
autoplot(forecast_final) +
labs(title = "Final ARIMA Model Forecast", x = "Time", y = "Price") +
theme_minimal()
library(forecast)
arima_old <- auto.arima(crypto_ts, order = c(0,1,0), seasonal = c(0,0,1)) # Your old model
summary(arima_old)
forecast_old <- forecast(arima_old, h = 24)
forecast_old <- forecast(arima_old, h = 24)
accuracy(forecast_old)
print(arima_model)
accuracy(forecast_final)
old_acc <- accuracy(forecast_old)
new_acc <- accuracy(forecast_final)
print("Old Model Accuracy:")
print(old_acc)
print("New Model Accuracy:")
print(new_acc)