-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRYPTO PREDICTION.R
556 lines (434 loc) · 16.1 KB
/
CRYPTO PREDICTION.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# Loading required libraries
library(tidyverse)
library(caret)
library(randomForest)
library(rpart)
library(class)
library(Metrics)
# Reading the datasets
#data_1 <- read.csv("C:/Users/turningpointKS/Downloads/archive/dataset.csv")
#head(data_1)
#unique(data_1$new_coins)
dataset2 <- read.csv("C:/Users/turningpointKS/Downloads/archive/dataset.csv")
head(dataset2)
dataset2 <- dataset2[, -1] # Removing the first column
unique(dataset2$crypto_name)
view(dataset2)
# Sampling the dataframe for the main coins
selected_coins <- c("Bitcoin", "Dogecoin", "XRP", "Tether")
new_data <- dataset2[dataset2$crypto_name %in% selected_coins, ]
head(new_data)
# Checking for missing values
colSums(is.na(new_data))
# Visualizing data for each coin
# Bitcoin
btc <- subset(new_data, crypto_name == "Bitcoin")
btc$date <- as.Date(btc$date)
head(btc)
# Visualizing BTC close by year
plot(btc$date, btc$close, type = "l", xlab = "Year", ylab = "Close market price", main = "BTC close by year")
library(ggplot2)
btc$year <- as.numeric(format(btc$date, "%Y"))
btc$date <- as.Date(btc$date, format = "%d-%b-%Y")
# Plotting with ggplot2
ggplot(btc, aes(x = date, y = close)) +
geom_line(color = "blue") +
labs(x = "Year", y = "Close market price", title = "BTC close by year") +
scale_x_date(date_labels = "%Y", date_breaks = "1 year") +
theme_minimal()
# Dogecoin
doge <- subset(new_data, crypto_name == "Dogecoin")
doge$date <- as.Date(doge$date)
head(doge)
# Visualizing Dogecoin close by year
plot(doge$date, doge$close, type = "l", xlab = "Year", ylab = "Close market price", main = "Dogecoin close by year")
# XRP
xrp <- subset(new_data, crypto_name == "XRP")
xrp$date <- as.Date(xrp$date)
head(xrp)
# Visualizing XRP close by year
plot(xrp$date, xrp$close, type = "l", xlab = "Year", ylab = "Close market price", main = "XRP close by year")
# Tether
usdt <- subset(new_data, crypto_name == "Tether")
usdt$date <- as.Date(usdt$date)
head(usdt)
# Visualizing Tether close by year
plot(usdt$date, usdt$close, type = "l", xlab = "Year", ylab = "Close market price", main = "USDT close by year")
# Distribution of data for each coin
# Select only numeric columns
numeric_data <- btc[, sapply(btc, is.numeric)]
# Plot histograms for each numeric column
par(mfrow=c(3,3)) # Adjust the layout if needed
for (col in colnames(numeric_data)) {
hist(numeric_data[[col]], main = col)
}
# For Tether (usdt)
# Select only numeric columns
numeric_data_usdt <- usdt[, sapply(usdt, is.numeric)]
# Plot histograms for each numeric column
par(mfrow=c(3,3)) # Adjust the layout if needed
for (col in colnames(numeric_data_usdt)) {
hist(numeric_data_usdt[[col]], main = col)
}
# For Dogecoin (doge)
# Select only numeric columns
numeric_data_doge <- doge[, sapply(doge, is.numeric)]
# Plot histograms for each numeric column
par(mfrow=c(3,3)) # Adjust the layout if needed
for (col in colnames(numeric_data_doge)) {
hist(numeric_data_doge[[col]], main = col)
}
# For XRP (xrp)
# Select only numeric columns
numeric_data_xrp <- xrp[, sapply(xrp, is.numeric)]
# Plot histograms for each numeric column
par(mfrow=c(3,3)) # Adjust the layout if needed
for (col in colnames(numeric_data_xrp)) {
hist(numeric_data_xrp[[col]], main = col)
}
# Correlation matrix for the four coins
# Select only numeric columns in btc
numeric_btc <- btc[, sapply(btc, is.numeric)]
correlation_1 <- cor(numeric_btc)
# Select only numeric columns in usdt
numeric_usdt <- usdt[, sapply(usdt, is.numeric)]
# Compute correlation matrix
correlation_usdt <- cor(numeric_usdt)
# Select only numeric columns in doge
numeric_doge <- doge[, sapply(doge, is.numeric)]
# Compute correlation matrix
correlation_doge <- cor(numeric_doge)
# Select only numeric columns in xrp
numeric_xrp <- xrp[, sapply(xrp, is.numeric)]
# Compute correlation matrix
correlation_xrp <- cor(numeric_xrp)
# Plot heatmap for BTC
heatmap(correlation_1,
main = "Correlation Heatmap - BTC",
xlab = "Variables",
ylab = "Variables")
# Plot heatmap for USDT
heatmap(correlation_usdt,
main = "Correlation Heatmap - USDT",
xlab = "Variables",
ylab = "Variables")
# Plot heatmap for DOGE
heatmap(correlation_doge,
main = "Correlation Heatmap - DOGE",
xlab = "Variables",
ylab = "Variables")
# Plot heatmap for XRP
heatmap(correlation_xrp,
main = "Correlation Heatmap - XRP",
xlab = "Variables",
ylab = "Variables")
# Data splitting and normalization
# Processing each of the coins for model building
# BTC
btc <- btc[, -c(1, 9)]
# Assuming you have a CSV file, adjust the file path accordingly
btc <- read.csv("C:/Users/turningpointKS/Downloads/archive/dataset.csv")
# Check the structure of btc
str(btc)
# Assuming marketCap contains the names of cryptocurrencies
selected_coins <- c("Bitcoin", "Dogecoin", "XRP", "Tether")
# Add the crypto_name column based on selected_coins
btc$crypto_name <- ifelse(btc$marketCap %in% selected_coins, btc$marketCap, NA)
# Convert crypto_name to numeric if it's a factor
if (is.factor(btc$crypto_name)) {
btc$crypto_name <- as.numeric(as.factor(btc$crypto_name))
}
# Dogecoin
doge <- doge[, -c(1, 9)]
doge$crypto_name <- as.numeric(doge$crypto_name)
# XRP
xrp <- xrp[, -c(1, 9)]
xrp$crypto_name <- as.numeric(xrp$crypto_name)
# Tether
usdt <- usdt[, -c(1, 9)]
usdt$crypto_name <- as.numeric(usdt$crypto_name)
# FILTER COINS
coins <- c("Bitcoin", "Dogecoin", "XRP", "Tether")
new_data <- filter(dataset2, crypto_name %in% coins)
# BTC
btc <- filter(new_data, crypto_name == "Bitcoin") %>%
select(-timestamp) %>%
mutate(date = as.Date(date))
# DOGE
doge <- filter(new_data, crypto_name == "Dogecoin") %>%
select(-timestamp) %>%
mutate(date = as.Date(date))
# XRP
xrp <- filter(new_data, crypto_name == "XRP") %>%
select(-timestamp) %>%
mutate(date = as.Date(date))
# USDT
usdt <- filter(new_data, crypto_name == "Tether") %>%
select(-timestamp) %>%
mutate(date = as.Date(date))
# ENCODE CRYPTO NAME
btc$crypto_name <- as.factor(btc$crypto_name)
doge$crypto_name <- as.factor(doge$crypto_name)
xrp$crypto_name <- as.factor(xrp$crypto_name)
usdt$crypto_name <- as.factor(usdt$crypto_name)
# SPLIT DATA
set.seed(123)
btc_train <- sample(1:nrow(btc), 0.7*nrow(btc))
btc_test <- setdiff(1:nrow(btc), btc_train)
doge_train <- sample(1:nrow(doge), 0.7*nrow(doge))
doge_test <- setdiff(1:nrow(doge), doge_train)
xrp_train <- sample(1:nrow(xrp), 0.7*nrow(xrp))
xrp_test <- setdiff(1:nrow(xrp), xrp_train)
usdt_train <- sample(1:nrow(usdt), 0.7*nrow(usdt))
usdt_test <- setdiff(1:nrow(usdt), usdt_train)
# doge MODELS
X_train <- doge[doge_train, -4]
y_train <- doge[doge_train, 4]
X_test <- doge[doge_train, -4]
y_test <- doge[doge_train, 4]
# Rabdom forest:
rf_model <- randomForest(y_train ~ ., data = X_train, ntree = 300)
rf_pred <- predict(rf_model, X_test)
print("Random Forest Metrics for doge:")
print(mae(y_test, rf_pred))
print(mse(y_test, rf_pred))
print(R2(y_test, rf_pred))
# Linear Regression Model
# Check for factors with only one level
summary(X_train)
# Remove factors with only one level or convert them to numeric if appropriate
X_train <- X_train[, sapply(X_train, function(x) length(unique(x)) > 1)]
lm_model <- lm(y_train ~ ., data = X_train)
lm_pred <- predict(lm_model, X_test)
print("Linear Regression Metrics for doge:")
print(mae(y_test, lm_pred))
print(mse(y_test, lm_pred))
print(R2(y_test, lm_pred))
# Decision Tree
tree_model <- rpart(y_train ~ ., data = X_train)
tree_pred <- predict(tree_model, X_test)
print(mae(y_test, tree_pred))
print(mse(y_test, tree_pred))
print(R2(y_test, tree_pred))
##KNN
# Split data into train/test
set.seed(123)
#train_index <- createDataPartition(doge$close, p=0.7, list=FALSE)
#train <- data[train_index, ]
#test <- data[-train_index, ]
normalize<- function(x){
return((x-min(x))/ (max(x)-min(x)))
}
# Ensure that all columns contain numeric data
dataset2[, 2:8] <- lapply(dataset2[, 2:8], as.numeric)
# Check for non-numeric columns
non_numeric_columns <- sapply(dataset2[, 2:8], function(x) any(!is.numeric(x)))
# If there are non-numeric columns, you need to handle them appropriately
if (any(non_numeric_columns)) {
print("There are non-numeric columns in the dataset.")
# Handle non-numeric columns based on your data characteristics
} else {
# Normalize numeric columns
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
datasub <- as.data.frame(lapply(dataset2[, 2:8], normalize))
}
# Combine predictors and response variable into one data frame
train_data <- cbind(X_train, y_train)
# Define the column names for the predictors and the response variable
colnames(train_data) <- c(colnames(X_train), "close")
train_data <- train_data[, !names(train_data) %in% c("crypto_name")]
# Train the KNN model
knn_model <- train(close ~ ., data = train_data, method = "knn")
# Print the model
print(knn_model)
knn_pred <- predict(knn_model, X_test)
print(mae(y_test, knn_pred))
print(mse(y_test, knn_pred))
print(R2(y_test, knn_pred))
# BTC MODELS
X_train <- btc[btc_train, -4]
y_train <- btc[btc_train, 4]
X_test <- btc[btc_train, -4]
y_test <- btc[btc_train, 4]
# Rabdom forest:
rf_model <- randomForest(y_train ~ ., data = X_train, ntree = 300)
rf_pred <- predict(rf_model, X_test)
print("Random Forest Metrics for btc:")
print(mae(y_test, rf_pred))
print(mse(y_test, rf_pred))
print(R2(y_test, rf_pred))
# Linear Regression Model
# Check for factors with only one level
summary(X_train)
# Remove factors with only one level or convert them to numeric if appropriate
X_train <- X_train[, sapply(X_train, function(x) length(unique(x)) > 1)]
lm_model <- lm(y_train ~ ., data = X_train)
lm_pred <- predict(lm_model, X_test)
print("Linear Regression Metrics for doge:")
print(mae(y_test, lm_pred))
print(mse(y_test, lm_pred))
print(R2(y_test, lm_pred))
# Decision Tree
tree_model <- rpart(y_train ~ ., data = X_train)
tree_pred <- predict(tree_model, X_test)
print(mae(y_test, tree_pred))
print(mse(y_test, tree_pred))
print(R2(y_test, tree_pred))
##KNN
normalize<- function(x){
return((x-min(x))/ (max(x)-min(x)))
}
# Ensure that all columns contain numeric data
dataset2[, 2:8] <- lapply(dataset2[, 2:8], as.numeric)
# Check for non-numeric columns
non_numeric_columns <- sapply(dataset2[, 2:8], function(x) any(!is.numeric(x)))
# If there are non-numeric columns, you need to handle them appropriately
if (any(non_numeric_columns)) {
print("There are non-numeric columns in the dataset.")
# Handle non-numeric columns based on your data characteristics
} else {
# Normalize numeric columns
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
datasub <- as.data.frame(lapply(dataset2[, 2:8], normalize))
}
# Combine predictors and response variable into one data frame
train_data <- cbind(X_train, y_train)
# Define the column names for the predictors and the response variable
colnames(train_data) <- c(colnames(X_train), "close")
train_data <- train_data[, !names(train_data) %in% c("crypto_name")]
# Train the KNN model
knn_model <- train(close ~ ., data = train_data, method = "knn")
# Print the model
print(knn_model)
knn_pred <- predict(knn_model, X_test)
print(mae(y_test, knn_pred))
print(mse(y_test, knn_pred))
print(R2(y_test, knn_pred))
## USDT
# usdt MODELS
X_train <- usdt[usdt_train, -4]
y_train <- usdt[usdt_train, 4]
X_test <- usdt[usdt_train, -4]
y_test <- usdt[usdt_train, 4]
# Rabdom forest:
rf_model <- randomForest(y_train ~ ., data = X_train, ntree = 300)
rf_pred <- predict(rf_model, X_test)
print("Random Forest Metrics for usdt:")
print(mae(y_test, rf_pred))
print(mse(y_test, rf_pred))
print(R2(y_test, rf_pred))
# Linear Regression Model
# Check for factors with only one level
summary(X_train)
# Remove factors with only one level or convert them to numeric if appropriate
X_train <- X_train[, sapply(X_train, function(x) length(unique(x)) > 1)]
lm_model <- lm(y_train ~ ., data = X_train)
lm_pred <- predict(lm_model, X_test)
print("Linear Regression Metrics for usdt:")
print(mae(y_test, lm_pred))
print(mse(y_test, lm_pred))
print(R2(y_test, lm_pred))
# Decision Tree
tree_model <- rpart(y_train ~ ., data = X_train)
tree_pred <- predict(tree_model, X_test)
print(mae(y_test, tree_pred))
print(mse(y_test, tree_pred))
print(R2(y_test, tree_pred))
##KNN
normalize<- function(x){
return((x-min(x))/ (max(x)-min(x)))
}
# Ensure that all columns contain numeric data
dataset2[, 2:8] <- lapply(dataset2[, 2:8], as.numeric)
# Check for non-numeric columns
non_numeric_columns <- sapply(dataset2[, 2:8], function(x) any(!is.numeric(x)))
# If there are non-numeric columns, you need to handle them appropriately
if (any(non_numeric_columns)) {
print("There are non-numeric columns in the dataset.")
# Handle non-numeric columns based on your data characteristics
} else {
# Normalize numeric columns
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
datasub <- as.data.frame(lapply(dataset2[, 2:8], normalize))
}
# Combine predictors and response variable into one data frame
train_data <- cbind(X_train, y_train)
# Define the column names for the predictors and the response variable
colnames(train_data) <- c(colnames(X_train), "close")
train_data <- train_data[, !names(train_data) %in% c("crypto_name")]
# Train the KNN model
knn_model <- train(close ~ ., data = train_data, method = "knn")
# Print the model
print(knn_model)
knn_pred <- predict(knn_model, X_test)
print(mae(y_test, knn_pred))
print(mse(y_test, knn_pred))
print(R2(y_test, knn_pred))
## xrp
# xrp MODELS
X_train <- xrp[xrp_train, -4]
y_train <- xrp[xrp_train, 4]
X_test <- xrp[xrp_train, -4]
y_test <- xrp[xrp_train, 4]
# Rabdom forest:
rf_model <- randomForest(y_train ~ ., data = X_train, ntree = 300)
rf_pred <- predict(rf_model, X_test)
print("Random Forest Metrics for xrp:")
print(mae(y_test, rf_pred))
print(mse(y_test, rf_pred))
print(R2(y_test, rf_pred))
# Linear Regression Model
# Check for factors with only one level
summary(X_train)
# Remove factors with only one level or convert them to numeric if appropriate
X_train <- X_train[, sapply(X_train, function(x) length(unique(x)) > 1)]
lm_model <- lm(y_train ~ ., data = X_train)
lm_pred <- predict(lm_model, X_test)
print("Linear Regression Metrics for xrp:")
print(mae(y_test, lm_pred))
print(mse(y_test, lm_pred))
print(R2(y_test, lm_pred))
# Decision Tree
tree_model <- rpart(y_train ~ ., data = X_train)
tree_pred <- predict(tree_model, X_test)
print(mae(y_test, tree_pred))
print(mse(y_test, tree_pred))
print(R2(y_test, tree_pred))
##KNN
normalize<- function(x){
return((x-min(x))/ (max(x)-min(x)))
}
# Ensure that all columns contain numeric data
dataset2[, 2:8] <- lapply(dataset2[, 2:8], as.numeric)
# Check for non-numeric columns
non_numeric_columns <- sapply(dataset2[, 2:8], function(x) any(!is.numeric(x)))
# If there are non-numeric columns, you need to handle them appropriately
if (any(non_numeric_columns)) {
print("There are non-numeric columns in the dataset.")
# Handle non-numeric columns based on your data characteristics
} else {
# Normalize numeric columns
normalize <- function(x) {
return((x - min(x)) / (max(x) - min(x)))
}
datasub <- as.data.frame(lapply(dataset2[, 2:8], normalize))
}
# Combine predictors and response variable into one data frame
train_data <- cbind(X_train, y_train)
# Define the column names for the predictors and the response variable
colnames(train_data) <- c(colnames(X_train), "close")
train_data <- train_data[, !names(train_data) %in% c("crypto_name")]
# Train the KNN model
knn_model <- train(close ~ ., data = train_data, method = "knn")
# Print the model
print(knn_model)
knn_pred <- predict(knn_model, X_test)
print(mae(y_test, knn_pred))
print(mse(y_test, knn_pred))
print(R2(y_test, knn_pred))