Section - 8 Evaluate Model Performance
Now we get to see the results of our hard work! There are some additional data preparation steps we need to take before we can visualize the results in aggregate; if you are just looking for the charts showing the results they are shown later on in the “Visualize Results” section below.
8.1 Summarizing models
Because we know what really happened for the target variable in the test data we used in the previous step, we can get a good idea of how good the model performed on a dataset it has never seen before. We do this to avoid overfitting, which is the idea that the model may work really well on the training data we provided, but not on the new data that we want to predictions on. If the performance on the test set is good, that is a good sign. If the data is split into several subsets and each subset has consistent results for the training and test datasets, that is an even better sign the model may perform as expected.
The first row of the data is for the BTC cryptocurrency for the split number 1. For this row of data (and all others), we made predictions for the test_data using a linear regression model and saved the results in the lm_test_predictions column. The models were trained on the train_data and had not yet seen the results from the test_data, so how accurate was the model in its predictions on this data?
8.1.1 MAE
Each individual prediction can be compared to the observation of what actually happened, and for each prediction we can calculate the error between the two. We can then take all of the values for the error of the prediction relative to the actual observations, and summarize the performance as a Mean Absolute Error (MAE) of those values, which gives us a single value to use as an indicator of the accuracy of the model. The higher the MAE score, the higher the error, meaning the model performs worse when the value is larger.
8.1.2 RMSE
A common metric to evaluate the performance of a model is the Root Mean Square Error, which is similar to the MAE but squares and then takes the square root of the values. An interesting implication of this, is that the RMSE will always be larger or equal to the MAE, where a large degree of error on a single observation would get penalized more by the RMSE. The higher the RMSE value, the worse the performance of the model, and can range from 0 to infinity, meaning there is no defined limit on the amount of error you could have (unlike the next metric).
8.1.3 R Squared
The \(R^2\), also known as the coefficient of determination, is a measure that describes the strength in the correlation between the predictions made and the actual results. A value of 1.0 would mean that the predictions made were exactly identical to the actual results. A perfect score is usually concerning because even a great model shouldn’t be exactly 100% accurate and usually indicates a mistake was made that gave away the results to the model and would not perform nearly as good when put into practice in the real world, but in the case of the \(R^2\) the higher the score (from 0 to 1) the better.
8.1.4 Get Metrics
We can return the RMSE and \(R^2\) metrics for the BTC cryptocurrency and the split number 1 by using the postResample() function from the caret package:
postResample(pred = cryptodata_nested$lm_test_predictions[[1]],
obs = cryptodata_nested$test_data[[1]]$target_price_24h)
## RMSE Rsquared MAE
## NA 0.0120576 NA
We can extract the first element to return the RMSE metric, and the second element for the R Squared (R^2) metric. We are using [[1]]
to extract the first element of the lm_test_predictions and test_data and compare the predictions to the actual value of the target_price24h column.
This model used the earliest subset of the data available for the cryptocurrency. How does the same model used to predict this older subset of the data perform when applied to the most recent subset of the data from the holdout?
We can get the same summary of results comparing the lm_holdout_predictions to what actually happened to the target_price_24h column of the actual holdout_data:
postResample(pred = cryptodata_nested$lm_holdout_predictions[[1]],
obs = cryptodata_nested$holdout_data[[1]]$target_price_24h)
## RMSE Rsquared MAE
## NA 0.002035134 NA
The result above may show a value of NA for the RMSE metric. We will explain and resolve the issue later on.
8.1.5 Comparing Metrics
Why not just pick one metric and stick to it? We certainly could, but these two metrics complement each other. For example, if we had a model that always predicts a 0% price change for the time period, the model may have a low error but it wouldn’t actually be very informative in the direction or magnitude of those movements and the predictions and actuals would not be very correlated with each other which would lead to a low \(R^2\) value. We are using both because it helps paint a more complete picture in this sense, and depending on the task you may want to use a different set of metrics to evaluate the performance. It is also worth mentioning that if your target variable you are predicting is either 0 or 1, this would be a classification problem where different metrics become more appropriate to use.
These are indicators that should be taken with a grain of salt individually, but comparing the results across many different models for the same cryptocurrency can help us determine which models work best for the problem, and then comparing those results across many cryptocurrencies can help us understand which cryptocurrencies we can predict with the most accuracy.
Before we can draw these comparisons however, we will need to “standardize” the values to create a fair comparison across all dataasets.
8.2 Data Prep - Adjust Prices
Because cryptocurrencies can vary dramatically in their prices with some trading in the tens of thousands of dollars and others trading for less than a cent, we need to make sure to standardize the RMSE columns to provide a fair comparison for the metric.
Therefore, before using the postResample()
function, let’s convert both the predictions and the target to be the % change in price over the 24 hour period, rather than the change in price ($).
8.2.1 Add Last Price
In order to convert the first prediction made to be a percentage, we need to know the previous price, which would be the last observation from the train data. Therefore, let’s make a function to add the last_price_train column and append it to the predictions made so we can calculate the % change of the first element relative to the last observation in the train data, before later removing the value not associated with the predictions:
last_train_price <- function(train_data, predictions){
c(tail(train_data$price_usd,1), predictions)
}
We will first perform all steps on the linear regression models to make the code a little more digestible, and we will then perform the same steps for the rest of the models.
8.2.1.1 Test
Overwrite the old predictions for the first 4 splits of the test data using the new function created above:
cryptodata_nested <- mutate(cryptodata_nested,
lm_test_predictions = ifelse(split < 5,
map2(train_data, lm_test_predictions, last_train_price),
NA))
The mutate() function is used to create the new column lm_test_predictions assigning the value only for the first 4 splits where the test data would actually exist (the 5th being the holdout set) using the ifelse() function.
8.2.1.2 Holdout
Do the same but for the holdout now. For the holdout we need to take the last price point of the 5th split:
cryptodata_nested_holdout <- mutate(filter(cryptodata_nested, split == 5),
lm_holdout_predictions = map2(train_data, lm_holdout_predictions, last_train_price))
Now join the holdout data to all rows based on the cryptocurrency symbol alone:
cryptodata_nested <- left_join(cryptodata_nested,
select(cryptodata_nested_holdout, symbol, lm_holdout_predictions),
by='symbol')
# Remove unwanted columns
cryptodata_nested <- select(cryptodata_nested, -lm_holdout_predictions.x, -split.y)
# Rename the columns kept
cryptodata_nested <- rename(cryptodata_nested,
lm_holdout_predictions = 'lm_holdout_predictions.y',
split = 'split.x')
# Reset the correct grouping structure
cryptodata_nested <- group_by(cryptodata_nested, symbol, split)
8.2.2 Convert to Percentage Change
Now we have everything we need to accurately calculate the percentage change between observations including the first one. Let’s make a new function to calculate the percentage change:
standardize_perc_change <- function(predictions){
results <- (diff(c(lag(predictions, 1), predictions)) / lag(predictions, 1))*100
# Exclude the first element, next element will be % change of first prediction
tail(head(results, length(predictions)), length(predictions)-1)
}
Overwrite the old predictions with the new predictions adjusted as a percentage now:
8.2.3 Actuals
Now do the same thing to the actual prices. Let’s make a new column called actuals containing the real price values (rather than the predicted ones):
actuals_create <- function(train_data, test_data){
c(tail(train_data$price_usd,1), as.numeric(unlist(select(test_data, price_usd))))
}
Use the new function to create the new column actuals:
cryptodata_nested <- mutate(cryptodata_nested,
actuals_test = ifelse(split < 5,
map2(train_data, test_data, actuals_create),
NA))
8.2.3.1 Holdout
Again, for the holdout we need the price from the training data of the 5th split to perform the first calculation:
cryptodata_nested_holdout <- mutate(filter(cryptodata_nested, split == 5),
actuals_holdout = map2(train_data, holdout_data, actuals_create))
Join the holdout data to all rows based on the cryptocurrency symbol alone:
cryptodata_nested <- left_join(cryptodata_nested,
select(cryptodata_nested_holdout, symbol, actuals_holdout),
by='symbol')
# Remove unwanted columns
cryptodata_nested <- select(cryptodata_nested, -split.y)
# Rename the columns kept
cryptodata_nested <- rename(cryptodata_nested, split = 'split.x')
# Reset the correct grouping structure
cryptodata_nested <- group_by(cryptodata_nested, symbol, split)
8.2.4 Actuals as % Change
Now we can convert the new actuals to express the price_usd as a % change relative to the previous value using adapting the function from earlier:
8.3 Review Summary Statistics
Now that we standardized the price to show the percentage change relative to the previous period instead of the price in dollars, we can actually compare the summary statistics across all cryptocurrencies and have it be a fair comparison.
Let’s get the same statistic as we did at the beginning of this section, but this time on the standardized values. This time to calculate the RMSE error metric let’s use the rmse() function from the hydroGOF package because it allows us to set the na.rm = T
parameter, and otherwise one NA value would return NA for the overall RMSE:
hydroGOF::rmse(cryptodata_nested$lm_test_predictions[[1]],
cryptodata_nested$actuals_test[[1]],
na.rm=T)
## [1] 0.9869445
8.3.1 Calculate R^2
Now we can do the same for the R Squared metric using the same postResample() function that we used at the start of this section:
evaluate_preds_rsq <- function(predictions, actuals){
postResample(pred = predictions, obs = actuals)[[2]]
}
cryptodata_nested <- mutate(cryptodata_nested,
lm_rsq_test = unlist(ifelse(split < 5,
map2(lm_test_predictions, actuals_test, evaluate_preds_rsq),
NA)),
lm_rsq_holdout = unlist(map2(lm_holdout_predictions, actuals_holdout, evaluate_preds_rsq)))
Look at the results:
## # A tibble: 260 x 4
## # Groups: symbol, split [260]
## symbol split lm_rsq_test lm_rsq_holdout
## <chr> <dbl> <dbl> <dbl>
## 1 BTC 1 0.00773 0.911
## 2 ETH 1 0.0154 0.795
## 3 EOS 1 0.301 0.593
## 4 LTC 1 0.0122 0.183
## 5 BSV 1 0.161 0.00195
## 6 ADA 1 0.444 0.239
## 7 TRX 1 0.815 0.579
## 8 ZEC 1 0.272 0.705
## 9 XMR 1 0.950 0.739
## 10 KNC 1 0.0273 0.895
## # ... with 250 more rows
8.3.2 Calculate RMSE
Similarly let’s make a function to get the RMSE metric for all models:
evaluate_preds_rmse <- function(predictions, actuals){
hydroGOF::rmse(predictions, actuals, na.rm=T)
}
Now we can use the map2() function to use it to get the RMSE metric for both the test data and the holdout:
cryptodata_nested <- mutate(cryptodata_nested,
lm_rmse_test = unlist(ifelse(split < 5,
map2(lm_test_predictions, actuals_test, evaluate_preds_rmse),
NA)),
lm_rmse_holdout = unlist(map2(lm_holdout_predictions, actuals_holdout, evaluate_preds_rmse)))
Look at the results. Wrapping them in print(n=500)
overwrites the behavior to only give a preview of the data so we can view the full results (up to 500 observations).
## # A tibble: 260 x 6
## # Groups: symbol, split [260]
## symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 BTC 1 0.987 0.0761 0.00773 0.911
## 2 ETH 1 0.836 0.133 0.0154 0.795
## 3 EOS 1 0.946 1.01 0.301 0.593
## 4 LTC 1 1.27 0.698 0.0122 0.183
## 5 BSV 1 4.55 0.841 0.161 0.00195
## 6 ADA 1 0.806 0.518 0.444 0.239
## 7 TRX 1 0.209 0.194 0.815 0.579
## 8 ZEC 1 1.73 1.34 0.272 0.705
## 9 XMR 1 1.35 0.280 0.950 0.739
## 10 KNC 1 5.08 0.563 0.0273 0.895
## 11 ZRX 1 2.31 0.499 0.00793 0.601
## 12 BAT 1 2.26 0.665 0.0247 0.784
## 13 BNT 1 1.58 2.24 0.00587 0.798
## 14 MANA 1 2.29 1.41 0.00538 0.825
## 15 ENJ 1 2.37 0.808 0.0558 0.330
## 16 BTG 1 1.60 0.811 0.0131 0.0979
## 17 ELF 1 0.370 0.226 0.654 0.858
## 18 NEXO 1 0.316 0.906 0.879 0.0835
## 19 CHZ 1 2.21 0.930 0.0224 0.622
## 20 AVA 1 1.70 0.359 0.0105 0.622
## 21 GMTT 1 6.46 96.2 0.00536 0.00577
## 22 ID 1 1.57 0.758 0.0289 0.963
## 23 DYDX 1 1.42 0.560 0.301 0.198
## 24 1INCH 1 2.37 1.17 0.0333 0.0116
## 25 OXT 1 0.594 1.33 0.523 0.694
## 26 SKL 1 1.97 0.491 0.0488 0.886
## 27 DOT 1 1.74 0.744 0.112 0.191
## 28 CTC 1 3.01 0.820 0.366 0.546
## 29 LQTY 1 0.789 0.491 0.750 0.936
## 30 TOMO 1 1.61 0.833 0.250 0.665
## 31 GODS 1 5.37 1.73 0.235 0.305
## 32 IOTA 1 1.87 0.737 0.00747 0.236
## 33 PERP 1 1.93 0.623 0.0604 0.257
## 34 STETH 1 0.782 0.332 0.0569 0.513
## 35 RIF 1 1.61 0.407 0.425 0.196
## 36 APT 1 1.88 0.507 0.0389 0.896
## 37 ATOM 1 1.28 0.575 0.0848 0.617
## 38 MTLX 1 0.543 9.39 0.00401 0.0401
## 39 ANT 1 0.402 0.728 0.920 0.485
## 40 BCUG 1 8.17 0.142 0.910 0.931
## 41 ARB 1 0.962 0.239 0.564 0.888
## 42 XDC 1 3.43 3.96 0.0999 0.0120
## 43 THETA 1 1.93 0.498 0.0504 0.727
## 44 XCH 1 16461. 0.265 0.000122 0.393
## 45 AGIX 1 1.80 1.06 0.0584 0.00160
## 46 REN 1 1.73 1.19 0.377 0.0481
## 47 VGX 1 1.45 1.06 0.533 0.160
## 48 GMT 1 2.70 1.24 0.0374 0.537
## 49 APFC 1 2.70 0.282 0.0331 0.614
## 50 DASH 1 1.52 0.416 0.204 0.939
## 51 ETHW 1 3.21 1.18 0.000192 0.369
## 52 LAZIO 1 1.03 0.387 0.395 0.874
## 53 ETHW 2 1.36 1.18 0.0442 0.369
## 54 LAZIO 2 0.688 0.387 0.150 0.874
## 55 ID 2 1.03 0.758 0.179 0.963
## 56 GMTT 2 1.23 96.2 0.600 0.00577
## 57 CTC 2 2.01 0.820 0.626 0.546
## 58 BNT 2 0.652 2.24 0.114 0.798
## 59 GODS 2 0.801 1.73 0.791 0.305
## 60 XDC 2 1.11 3.96 0.191 0.0120
## 61 BTC 2 0.269 0.0761 0.00557 0.911
## 62 ETH 2 0.346 0.133 0.249 0.795
## 63 EOS 2 0.544 1.01 0.576 0.593
## 64 LTC 2 0.367 0.698 0.673 0.183
## 65 BSV 2 0.475 0.841 0.829 0.00195
## 66 ADA 2 0.523 0.518 0.0729 0.239
## 67 ZEC 2 0.810 1.34 0.595 0.705
## 68 TRX 2 0.296 0.194 0.646 0.579
## 69 KNC 2 0.502 0.563 0.123 0.895
## 70 ZRX 2 0.508 0.499 0.491 0.601
## 71 BAT 2 0.550 0.665 0.310 0.784
## 72 MANA 2 0.573 1.41 0.0942 0.825
## 73 ENJ 2 0.678 0.808 0.129 0.330
## 74 BTG 2 0.486 0.811 0.778 0.0979
## 75 ELF 2 0.324 0.226 0.0386 0.858
## 76 NEXO 2 2.62 0.906 0.0568 0.0835
## 77 CHZ 2 0.603 0.930 0.00201 0.622
## 78 AVA 2 0.982 0.359 0.0185 0.622
## 79 DYDX 2 0.683 0.560 0.187 0.198
## 80 1INCH 2 0.665 1.17 0.0112 0.0116
## 81 OXT 2 2.74 1.33 0.0158 0.694
## 82 SKL 2 0.848 0.491 0.0180 0.886
## 83 DOT 2 0.463 0.744 0.0152 0.191
## 84 LQTY 2 0.803 0.491 0.0906 0.936
## 85 TOMO 2 0.492 0.833 0.919 0.665
## 86 IOTA 2 0.332 0.737 0.535 0.236
## 87 PERP 2 0.380 0.623 0.413 0.257
## 88 STETH 2 0.488 0.332 0.403 0.513
## 89 RIF 2 1.29 0.407 0.247 0.196
## 90 APT 2 0.702 0.507 0.268 0.896
## 91 ATOM 2 0.544 0.575 0.0641 0.617
## 92 ANT 2 1.12 0.728 0.203 0.485
## 93 ARB 2 0.703 0.239 0.368 0.888
## 94 XCH 2 0.506 0.265 0.509 0.393
## 95 BCUG 2 1.32 0.142 0.0662 0.931
## 96 THETA 2 0.799 0.498 0.00314 0.727
## 97 AGIX 2 0.864 1.06 0.112 0.00160
## 98 REN 2 0.821 1.19 0.0644 0.0481
## 99 GMT 2 0.968 1.24 0.0110 0.537
## 100 DASH 2 0.660 0.416 0.117 0.939
## 101 APFC 2 1.45 0.282 0.00651 0.614
## 102 MTLX 2 0.411 9.39 0.343 0.0401
## 103 XMR 2 0.494 0.280 0.000218 0.739
## 104 VGX 2 1.61 1.06 0.0728 0.160
## 105 GMTT 3 1.01 96.2 0.0405 0.00577
## 106 ETHW 3 1.13 1.18 0.0140 0.369
## 107 LAZIO 3 0.443 0.387 0.0636 0.874
## 108 ID 3 0.945 0.758 0.0613 0.963
## 109 BNT 3 0.843 2.24 0.0372 0.798
## 110 GODS 3 0.801 1.73 0.794 0.305
## 111 XDC 3 5.42 3.96 0.000188 0.0120
## 112 XCH 3 0.261 0.265 0.301 0.393
## 113 BTC 3 0.400 0.0761 0.00163 0.911
## 114 ETH 3 0.357 0.133 0.00164 0.795
## 115 EOS 3 0.568 1.01 0.691 0.593
## 116 LTC 3 0.564 0.698 0.331 0.183
## 117 BSV 3 0.608 0.841 0.334 0.00195
## 118 ADA 3 0.632 0.518 0.391 0.239
## 119 ZEC 3 0.406 1.34 0.730 0.705
## 120 TRX 3 0.131 0.194 0.803 0.579
## 121 KNC 3 0.334 0.563 0.825 0.895
## 122 ZRX 3 1.37 0.499 0.000698 0.601
## 123 BAT 3 0.808 0.665 0.0995 0.784
## 124 MANA 3 0.414 1.41 0.444 0.825
## 125 ENJ 3 0.532 0.808 0.311 0.330
## 126 ELF 3 0.301 0.226 0.306 0.858
## 127 NEXO 3 4.15 0.906 0.218 0.0835
## 128 CHZ 3 0.557 0.930 0.144 0.622
## 129 AVA 3 1.20 0.359 0.436 0.622
## 130 1INCH 3 1.01 1.17 0.436 0.0116
## 131 DYDX 3 0.684 0.560 0.726 0.198
## 132 OXT 3 0.538 1.33 0.150 0.694
## 133 SKL 3 0.617 0.491 0.226 0.886
## 134 DOT 3 0.754 0.744 0.613 0.191
## 135 LQTY 3 0.532 0.491 0.688 0.936
## 136 TOMO 3 1.05 0.833 0.0521 0.665
## 137 IOTA 3 0.499 0.737 0.122 0.236
## 138 PERP 3 0.601 0.623 0.189 0.257
## 139 STETH 3 0.444 0.332 0.446 0.513
## 140 RIF 3 0.857 0.407 0.0412 0.196
## 141 APT 3 0.417 0.507 0.404 0.896
## 142 ATOM 3 0.563 0.575 0.238 0.617
## 143 ANT 3 0.629 0.728 0.514 0.485
## 144 ARB 3 0.595 0.239 0.495 0.888
## 145 BCUG 3 0.228 0.142 0.0108 0.931
## 146 THETA 3 0.532 0.498 0.761 0.727
## 147 AGIX 3 0.630 1.06 0.285 0.00160
## 148 REN 3 0.568 1.19 0.329 0.0481
## 149 GMT 3 1.31 1.24 0.0440 0.537
## 150 DASH 3 0.710 0.416 0.224 0.939
## 151 BTG 3 1.27 0.811 0.107 0.0979
## 152 MTLX 3 0.183 9.39 0.193 0.0401
## 153 APFC 3 0.338 0.282 0.769 0.614
## 154 XMR 3 0.578 0.280 0.0886 0.739
## 155 VGX 3 11.3 1.06 0.0975 0.160
## 156 CTC 3 0.484 0.820 0.292 0.546
## 157 GMTT 4 0.271 96.2 0.282 0.00577
## 158 ETHW 4 0.520 1.18 0.543 0.369
## 159 LAZIO 4 1.06 0.387 0.263 0.874
## 160 BNT 4 0.672 2.24 0.00975 0.798
## 161 XCH 4 0.266 0.265 0.412 0.393
## 162 BTC 4 0.252 0.0761 0.910 0.911
## 163 ETH 4 0.349 0.133 0.184 0.795
## 164 LTC 4 0.590 0.698 0.827 0.183
## 165 BSV 4 0.969 0.841 0.795 0.00195
## 166 ADA 4 0.590 0.518 0.534 0.239
## 167 TRX 4 0.893 0.194 0.155 0.579
## 168 ZEC 4 0.354 1.34 0.622 0.705
## 169 KNC 4 0.795 0.563 0.465 0.895
## 170 ZRX 4 0.607 0.499 0.445 0.601
## 171 BAT 4 0.656 0.665 0.273 0.784
## 172 MANA 4 0.402 1.41 0.780 0.825
## 173 ENJ 4 0.545 0.808 0.228 0.330
## 174 ELF 4 0.189 0.226 0.567 0.858
## 175 NEXO 4 3.75 0.906 0.0206 0.0835
## 176 CHZ 4 0.395 0.930 0.215 0.622
## 177 AVA 4 0.779 0.359 0.211 0.622
## 178 DYDX 4 1.02 0.560 0.556 0.198
## 179 1INCH 4 0.446 1.17 0.428 0.0116
## 180 OXT 4 1.96 1.33 0.0615 0.694
## 181 SKL 4 0.590 0.491 0.346 0.886
## 182 LQTY 4 0.856 0.491 0.0240 0.936
## 183 TOMO 4 0.767 0.833 0.169 0.665
## 184 IOTA 4 0.259 0.737 0.771 0.236
## 185 PERP 4 0.437 0.623 0.758 0.257
## 186 STETH 4 0.289 0.332 0.433 0.513
## 187 RIF 4 0.644 0.407 0.408 0.196
## 188 APT 4 0.332 0.507 0.687 0.896
## 189 ATOM 4 0.367 0.575 0.676 0.617
## 190 ANT 4 0.540 0.728 0.582 0.485
## 191 ARB 4 0.433 0.239 0.715 0.888
## 192 EOS 4 0.327 1.01 0.794 0.593
## 193 DOT 4 0.434 0.744 0.106 0.191
## 194 GODS 4 0.695 1.73 0.815 0.305
## 195 BCUG 4 2.27 0.142 0.510 0.931
## 196 THETA 4 1.15 0.498 0.181 0.727
## 197 AGIX 4 0.513 1.06 0.678 0.00160
## 198 REN 4 0.223 1.19 0.873 0.0481
## 199 GMT 4 0.316 1.24 0.681 0.537
## 200 DASH 4 0.282 0.416 0.763 0.939
## 201 MTLX 4 0.105 9.39 0.676 0.0401
## 202 BTG 4 7.77 0.811 0.000911 0.0979
## 203 XDC 4 5.06 3.96 0.245 0.0120
## 204 APFC 4 0.294 0.282 0.755 0.614
## 205 ID 4 0.614 0.758 0.0140 0.963
## 206 XMR 4 0.178 0.280 0.808 0.739
## 207 VGX 4 5.85 1.06 0.617 0.160
## 208 CTC 4 1.98 0.820 0.0796 0.546
## 209 GMTT 5 NA 96.2 NA 0.00577
## 210 ETHW 5 NA 1.18 NA 0.369
## 211 LAZIO 5 NA 0.387 NA 0.874
## 212 BNT 5 NA 2.24 NA 0.798
## 213 BTC 5 NA 0.0761 NA 0.911
## 214 ETH 5 NA 0.133 NA 0.795
## 215 LTC 5 NA 0.698 NA 0.183
## 216 BSV 5 NA 0.841 NA 0.00195
## 217 ADA 5 NA 0.518 NA 0.239
## 218 ZEC 5 NA 1.34 NA 0.705
## 219 TRX 5 NA 0.194 NA 0.579
## 220 KNC 5 NA 0.563 NA 0.895
## 221 ZRX 5 NA 0.499 NA 0.601
## 222 BAT 5 NA 0.665 NA 0.784
## 223 MANA 5 NA 1.41 NA 0.825
## 224 ENJ 5 NA 0.808 NA 0.330
## 225 ELF 5 NA 0.226 NA 0.858
## 226 CHZ 5 NA 0.930 NA 0.622
## 227 AVA 5 NA 0.359 NA 0.622
## 228 DYDX 5 NA 0.560 NA 0.198
## 229 1INCH 5 NA 1.17 NA 0.0116
## 230 OXT 5 NA 1.33 NA 0.694
## 231 SKL 5 NA 0.491 NA 0.886
## 232 TOMO 5 NA 0.833 NA 0.665
## 233 IOTA 5 NA 0.737 NA 0.236
## 234 PERP 5 NA 0.623 NA 0.257
## 235 STETH 5 NA 0.332 NA 0.513
## 236 RIF 5 NA 0.407 NA 0.196
## 237 APT 5 NA 0.507 NA 0.896
## 238 ATOM 5 NA 0.575 NA 0.617
## 239 MTLX 5 NA 9.39 NA 0.0401
## 240 ANT 5 NA 0.728 NA 0.485
## 241 ARB 5 NA 0.239 NA 0.888
## 242 EOS 5 NA 1.01 NA 0.593
## 243 DOT 5 NA 0.744 NA 0.191
## 244 LQTY 5 NA 0.491 NA 0.936
## 245 BCUG 5 NA 0.142 NA 0.931
## 246 THETA 5 NA 0.498 NA 0.727
## 247 AGIX 5 NA 1.06 NA 0.00160
## 248 REN 5 NA 1.19 NA 0.0481
## 249 GMT 5 NA 1.24 NA 0.537
## 250 DASH 5 NA 0.416 NA 0.939
## 251 BTG 5 NA 0.811 NA 0.0979
## 252 XDC 5 NA 3.96 NA 0.0120
## 253 XCH 5 NA 0.265 NA 0.393
## 254 APFC 5 NA 0.282 NA 0.614
## 255 NEXO 5 NA 0.906 NA 0.0835
## 256 GODS 5 NA 1.73 NA 0.305
## 257 CTC 5 NA 0.820 NA 0.546
## 258 ID 5 NA 0.758 NA 0.963
## 259 VGX 5 NA 1.06 NA 0.160
## 260 XMR 5 NA 0.280 NA 0.739
Out of 260 groups, 123 had an equal or lower RMSE score for the holdout than the test set.
8.4 Adjust Prices - All Models
Let’s repeat the same steps that we outlined above for all models.
8.4.1 Add Last Price
cryptodata_nested <- mutate(cryptodata_nested,
# XGBoost:
xgb_test_predictions = ifelse(split < 5,
map2(train_data, xgb_test_predictions, last_train_price),
NA),
# Neural Network:
nnet_test_predictions = ifelse(split < 5,
map2(train_data, nnet_test_predictions, last_train_price),
NA),
# Random Forest:
rf_test_predictions = ifelse(split < 5,
map2(train_data, rf_test_predictions, last_train_price),
NA),
# PCR:
pcr_test_predictions = ifelse(split < 5,
map2(train_data, pcr_test_predictions, last_train_price),
NA))
8.4.1.0.1 Holdout
cryptodata_nested_holdout <- mutate(filter(cryptodata_nested, split == 5),
# XGBoost:
xgb_holdout_predictions = map2(train_data, xgb_holdout_predictions, last_train_price),
# Neural Network:
nnet_holdout_predictions = map2(train_data, nnet_holdout_predictions, last_train_price),
# Random Forest:
rf_holdout_predictions = map2(train_data, rf_holdout_predictions, last_train_price),
# PCR:
pcr_holdout_predictions = map2(train_data, pcr_holdout_predictions, last_train_price))
Join the holdout data to all rows based on the cryptocurrency symbol alone:
cryptodata_nested <- left_join(cryptodata_nested,
select(cryptodata_nested_holdout, symbol,
xgb_holdout_predictions, nnet_holdout_predictions,
rf_holdout_predictions, pcr_holdout_predictions),
by='symbol')
# Remove unwanted columns
cryptodata_nested <- select(cryptodata_nested, -xgb_holdout_predictions.x,
-nnet_holdout_predictions.x,-rf_holdout_predictions.x,
-pcr_holdout_predictions.x, -split.y)
# Rename the columns kept
cryptodata_nested <- rename(cryptodata_nested,
xgb_holdout_predictions = 'xgb_holdout_predictions.y',
nnet_holdout_predictions = 'nnet_holdout_predictions.y',
rf_holdout_predictions = 'rf_holdout_predictions.y',
pcr_holdout_predictions = 'pcr_holdout_predictions.y',
split = 'split.x')
# Reset the correct grouping structure
cryptodata_nested <- group_by(cryptodata_nested, symbol, split)
8.4.2 Convert to % Change
Overwrite the old predictions with the new predictions adjusted as a percentage now:
cryptodata_nested <- mutate(cryptodata_nested,
# XGBoost:
xgb_test_predictions = ifelse(split < 5,
map(xgb_test_predictions, standardize_perc_change),
NA),
# holdout - all splits
xgb_holdout_predictions = map(xgb_holdout_predictions, standardize_perc_change),
# nnet:
nnet_test_predictions = ifelse(split < 5,
map(nnet_test_predictions, standardize_perc_change),
NA),
# holdout - all splits
nnet_holdout_predictions = map(nnet_holdout_predictions, standardize_perc_change),
# Random Forest:
rf_test_predictions = ifelse(split < 5,
map(rf_test_predictions, standardize_perc_change),
NA),
# holdout - all splits
rf_holdout_predictions = map(rf_holdout_predictions, standardize_perc_change),
# PCR:
pcr_test_predictions = ifelse(split < 5,
map(pcr_test_predictions, standardize_perc_change),
NA),
# holdout - all splits
pcr_holdout_predictions = map(pcr_holdout_predictions, standardize_perc_change))
8.4.3 Add Metrics
Add the RMSE and \(R^2\) metrics:
cryptodata_nested <- mutate(cryptodata_nested,
# XGBoost - RMSE - Test
xgb_rmse_test = unlist(ifelse(split < 5,
map2(xgb_test_predictions, actuals_test, evaluate_preds_rmse),
NA)),
# And holdout:
xgb_rmse_holdout = unlist(map2(xgb_holdout_predictions, actuals_holdout ,evaluate_preds_rmse)),
# XGBoost - R^2 - Test
xgb_rsq_test = unlist(ifelse(split < 5,
map2(xgb_test_predictions, actuals_test, evaluate_preds_rsq),
NA)),
# And holdout:
xgb_rsq_holdout = unlist(map2(xgb_holdout_predictions, actuals_holdout, evaluate_preds_rsq)),
# Neural Network - RMSE - Test
nnet_rmse_test = unlist(ifelse(split < 5,
map2(nnet_test_predictions, actuals_test, evaluate_preds_rmse),
NA)),
# And holdout:
nnet_rmse_holdout = unlist(map2(nnet_holdout_predictions, actuals_holdout, evaluate_preds_rmse)),
# Neural Network - R^2 - Test
nnet_rsq_test = unlist(ifelse(split < 5,
map2(nnet_test_predictions, actuals_test, evaluate_preds_rsq),
NA)),
# And holdout:
nnet_rsq_holdout = unlist(map2(nnet_holdout_predictions, actuals_holdout, evaluate_preds_rsq)),
# Random Forest - RMSE - Test
rf_rmse_test = unlist(ifelse(split < 5,
map2(rf_test_predictions, actuals_test, evaluate_preds_rmse),
NA)),
# And holdout:
rf_rmse_holdout = unlist(map2(rf_holdout_predictions, actuals_holdout, evaluate_preds_rmse)),
# Random Forest - R^2 - Test
rf_rsq_test = unlist(ifelse(split < 5,
map2(rf_test_predictions, actuals_test, evaluate_preds_rsq),
NA)),
# And holdout:
rf_rsq_holdout = unlist(map2(rf_holdout_predictions, actuals_holdout, evaluate_preds_rsq)),
# PCR - RMSE - Test
pcr_rmse_test = unlist(ifelse(split < 5,
map2(pcr_test_predictions, actuals_test, evaluate_preds_rmse),
NA)),
# And holdout:
pcr_rmse_holdout = unlist(map2(pcr_holdout_predictions, actuals_holdout, evaluate_preds_rmse)),
# PCR - R^2 - Test
pcr_rsq_test = unlist(ifelse(split < 5,
map2(pcr_test_predictions, actuals_test, evaluate_preds_rsq),
NA)),
# And holdout:
pcr_rsq_holdout = unlist(map2(pcr_holdout_predictions, actuals_holdout, evaluate_preds_rsq)))
Now we have RMSE and \(R^2\) values for every model created for every cryptocurrency and split of the data:
## # A tibble: 260 x 6
## # Groups: symbol, split [260]
## symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
## <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 BTC 1 0.987 0.00773 0.0761 0.911
## 2 ETH 1 0.836 0.0154 0.133 0.795
## 3 EOS 1 0.946 0.301 1.01 0.593
## 4 LTC 1 1.27 0.0122 0.698 0.183
## 5 BSV 1 4.55 0.161 0.841 0.00195
## 6 ADA 1 0.806 0.444 0.518 0.239
## 7 TRX 1 0.209 0.815 0.194 0.579
## 8 ZEC 1 1.73 0.272 1.34 0.705
## 9 XMR 1 1.35 0.950 0.280 0.739
## 10 KNC 1 5.08 0.0273 0.563 0.895
## # ... with 250 more rows
Only the results for the linear regression model are shown. There are equivalent columns for the XGBoost, neural network, random forest and PCR models.
8.5 Evaluate Metrics Across Splits
Next, let’s evaluate the metrics across all splits and keeping moving along with the model validation plan as was originally outlined. Let’s create a new dataset called [cryptodata_metrics][splits]{style=“color: blue;”} that is not grouped by the split column and is instead only grouped by the symbol:
8.5.1 Evaluate RMSE Test
Now for each model we can create a new column giving the average RMSE for the 4 cross-validation test splits:
rmse_test <- mutate(cryptodata_metrics,
lm = mean(lm_rmse_test, na.rm = T),
xgb = mean(xgb_rmse_test, na.rm = T),
nnet = mean(nnet_rmse_test, na.rm = T),
rf = mean(rf_rmse_test, na.rm = T),
pcr = mean(pcr_rmse_test, na.rm = T))
Now we can use the gather() function to summarize the columns as rows:
rmse_test <- unique(gather(select(rmse_test, lm:pcr), 'model', 'rmse', -symbol))
# Show results
rmse_test
## # A tibble: 260 x 3
## # Groups: symbol [52]
## symbol model rmse
## <chr> <chr> <dbl>
## 1 BTC lm 0.477
## 2 ETH lm 0.472
## 3 EOS lm 0.596
## 4 LTC lm 0.698
## 5 BSV lm 1.65
## 6 ADA lm 0.638
## 7 TRX lm 0.383
## 8 ZEC lm 0.825
## 9 XMR lm 0.650
## 10 KNC lm 1.68
## # ... with 250 more rows
Now tag the results as having been for the test set:
8.5.2 Holdout
Now repeat the same process for the holdout set:
rmse_holdout <- mutate(cryptodata_metrics,
lm = mean(lm_rmse_holdout, na.rm = T),
xgb = mean(xgb_rmse_holdout, na.rm = T),
nnet = mean(nnet_rmse_holdout, na.rm = T),
rf = mean(rf_rmse_holdout, na.rm = T),
pcr = mean(pcr_rmse_holdout, na.rm = T))
Again, use the gather() function to summarize the columns as rows:
rmse_holdout <- unique(gather(select(rmse_holdout, lm:pcr), 'model', 'rmse', -symbol))
# Show results
rmse_holdout
## # A tibble: 260 x 3
## # Groups: symbol [52]
## symbol model rmse
## <chr> <chr> <dbl>
## 1 BTC lm 0.0761
## 2 ETH lm 0.133
## 3 EOS lm 1.01
## 4 LTC lm 0.698
## 5 BSV lm 0.841
## 6 ADA lm 0.518
## 7 TRX lm 0.194
## 8 ZEC lm 1.34
## 9 XMR lm 0.280
## 10 KNC lm 0.563
## # ... with 250 more rows
Now tag the results as having been for the holdout set:
8.6 Evaluate R^2
Now let’s repeat the same steps we took for the RMSE metrics above for the \(R^2\) metric as well.
8.6.1 Test
For each model again we will create a new column giving the average \(R^2\) for the 4 cross-validation test splits:
rsq_test <- mutate(cryptodata_metrics,
lm = mean(lm_rsq_test, na.rm = T),
xgb = mean(xgb_rsq_test, na.rm = T),
nnet = mean(nnet_rsq_test, na.rm = T),
rf = mean(rf_rsq_test, na.rm = T),
pcr = mean(pcr_rsq_test, na.rm = T))
Now we can use the gather() function to summarize the columns as rows:
rsq_test <- unique(gather(select(rsq_test, lm:pcr), 'model', 'rsq', -symbol))
# Show results
rsq_test
## # A tibble: 260 x 3
## # Groups: symbol [52]
## symbol model rsq
## <chr> <chr> <dbl>
## 1 BTC lm 0.231
## 2 ETH lm 0.112
## 3 EOS lm 0.591
## 4 LTC lm 0.461
## 5 BSV lm 0.530
## 6 ADA lm 0.360
## 7 TRX lm 0.605
## 8 ZEC lm 0.555
## 9 XMR lm 0.462
## 10 KNC lm 0.360
## # ... with 250 more rows
Now tag the results as having been for the test set
8.6.2 Holdout
Do the same and calculate the averages for the holdout sets:
rsq_holdout <- mutate(cryptodata_metrics,
lm = mean(lm_rsq_holdout, na.rm = T),
xgb = mean(xgb_rsq_holdout, na.rm = T),
nnet = mean(nnet_rsq_holdout, na.rm = T),
rf = mean(rf_rsq_holdout, na.rm = T),
pcr = mean(pcr_rsq_holdout, na.rm = T))
Now we can use the gather() function to summarize the columns as rows:
rsq_holdout <- unique(gather(select(rsq_holdout, lm:pcr), 'model', 'rsq', -symbol))
# Show results
rsq_holdout
## # A tibble: 260 x 3
## # Groups: symbol [52]
## symbol model rsq
## <chr> <chr> <dbl>
## 1 BTC lm 0.911
## 2 ETH lm 0.795
## 3 EOS lm 0.593
## 4 LTC lm 0.183
## 5 BSV lm 0.00195
## 6 ADA lm 0.239
## 7 TRX lm 0.579
## 8 ZEC lm 0.705
## 9 XMR lm 0.739
## 10 KNC lm 0.895
## # ... with 250 more rows
Now tag the results as having been for the holdout set:
8.7 Visualize Results
Now we can take the same tools we learned in the Visualization section from earlier and visualize the results of the models.
8.7.2 Both
Now we have everything we need to use the two metrics to compare the results.
8.7.2.1 Join Datasets
First join the two objects rmse_scores and rsq_scores into the new object **plot_scores:
8.7.2.2 Plot Results
Now we can plot the results on a chart:
Running the same code wrapped in the ggplotly() function from the plotly package (as we have already done) we can make the chart interactive. Try hovering over the points on the chart with your mouse.
ggplotly(ggplot(plot_scores, aes(x=rsq, y=rmse, color = model, symbol = symbol)) +
geom_point() +
ylim(c(0,10)),
tooltip = c("model", "symbol", "rmse", "rsq"))
The additional tooltip argument was passed to ggpltoly() to specify the label when hovering over the individual points.
8.7.3 Results by the Cryptocurrency
We can use the facet_wrap() function from ggplot2 to create an individual chart for each cryptocurrency:
ggplot(plot_scores, aes(x=rsq, y=rmse, color = model)) +
geom_point() +
geom_smooth() +
ylim(c(0,10)) +
facet_wrap(~symbol)
Every 12 hours once this document reaches this point, the results are saved to GitHub using the pins package (which we used to read in the data at the start), and a separate script running on a different server creates the complete dataset in our database over time. You won’t be able to run the code shown below (nor do you have a reason to):
8.8 Interactive Dashboard
Use the interactive app below to explore the results over time by the individual cryptocurrency. Use the filters on the left sidebar to visualize the results you are interested in:
If you have trouble viewing the embedded dashboard you can open it here instead: https://predictcrypto.shinyapps.io/tutorial_latest_model_summary/
The default view shows the holdout results for all models. Another interesting comparison to make is between the holdout and the test set for fewer models (2 is ideal).
8.9 Visualizations - Historical Metrics
We can pull the same data into this R session using the pin_get()
function:
The data is limited to metrics for runs from the past 30 days and includes new data every 12 hours. Using the tools we used in the data prep section, we can answer a couple more questions.
8.9.1 Best Models
Overall, which model has the best metrics for all runs from the last 30 days?
8.9.1.1 Summarize the data
# First create grouped data
best_models <- group_by(metrics_historical, model, eval_set)
# Now summarize the data
best_models <- summarize(best_models,
rmse = mean(rmse, na.rm=T),
rsq = mean(rsq, na.rm=T))
# Show results
best_models
## # A tibble: 10 x 4
## # Groups: model [5]
## model eval_set rmse rsq
## <chr> <chr> <dbl> <dbl>
## 1 lm holdout 15.5 0.506
## 2 lm test 4.07 0.478
## 3 nnet holdout 4.31 0.149
## 4 nnet test 4.63 0.164
## 5 pcr holdout 2.72 0.252
## 6 pcr test 2.92 0.278
## 7 rf holdout 3.96 0.114
## 8 rf test 3.83 0.129
## 9 xgb holdout 5.01 0.0693
## 10 xgb test 4.56 0.0885
8.9.2 Most Predictable Cryptocurrency
Overall, which cryptocurrency has the best metrics for all runs from the last 30 days?
8.9.2.1 Summarize the data
# First create grouped data
predictable_cryptos <- group_by(metrics_historical, symbol, eval_set)
# Now summarize the data
predictable_cryptos <- summarize(predictable_cryptos,
rmse = mean(rmse, na.rm=T),
rsq = mean(rsq, na.rm=T))
# Arrange from most predictable (according to R^2) to least
predictable_cryptos <- arrange(predictable_cryptos, desc(rsq))
# Show results
predictable_cryptos
## # A tibble: 178 x 4
## # Groups: symbol [89]
## symbol eval_set rmse rsq
## <chr> <chr> <dbl> <dbl>
## 1 NAV test 3.30 0.434
## 2 POA holdout 4.60 0.423
## 3 CUR holdout 6.09 0.410
## 4 CND test 1.84 0.374
## 5 CND holdout 5.24 0.360
## 6 SEELE holdout 8.88 0.355
## 7 ADXN test 9.26 0.348
## 8 RCN test 5.03 0.337
## 9 BTC test 1.32 0.331
## 10 SUN holdout 3.17 0.330
## # ... with 168 more rows
Show the top 15 most predictable cryptocurrencies (according to the \(R^2\)) using the formattable
package (Ren and Russell 2016) to color code the cells:
formattable(head(predictable_cryptos ,15),
list(rmse = color_tile("#71CA97", "red"),
rsq = color_tile("firebrick1", "#71CA97")))
symbol | eval_set | rmse | rsq |
---|---|---|---|
NAV | test | 3.299791 | 0.4338237 |
POA | holdout | 4.596582 | 0.4229192 |
CUR | holdout | 6.088416 | 0.4098434 |
CND | test | 1.835020 | 0.3737691 |
CND | holdout | 5.238670 | 0.3601346 |
SEELE | holdout | 8.876745 | 0.3548372 |
ADXN | test | 9.263022 | 0.3478368 |
RCN | test | 5.030197 | 0.3367737 |
BTC | test | 1.321379 | 0.3307333 |
SUN | holdout | 3.172350 | 0.3299285 |
AAB | test | 39.511003 | 0.3262746 |
ETH | test | 1.721290 | 0.3197170 |
LTC | test | 2.102832 | 0.3189001 |
LEO | test | 1.695632 | 0.3166553 |
RCN | holdout | 7.564985 | 0.3130917 |
8.9.3 Accuracy Over Time
8.9.3.1 Summarize the data
# First create grouped data
accuracy_over_time <- group_by(metrics_historical, date_utc)
# Now summarize the data
accuracy_over_time <- summarize(accuracy_over_time,
rmse = mean(rmse, na.rm=T),
rsq = mean(rsq, na.rm=T))
# Ungroup data
accuracy_over_time <- ungroup(accuracy_over_time)
# Convert date/time
accuracy_over_time$date_utc <- anytime(accuracy_over_time$date_utc)
# Show results
accuracy_over_time
## # A tibble: 30 x 3
## date_utc rmse rsq
## <dttm> <dbl> <dbl>
## 1 2021-01-12 00:00:00 4.05 0.241
## 2 2021-01-13 00:00:00 4.29 0.236
## 3 2021-01-14 00:00:00 4.06 0.251
## 4 2021-01-16 00:00:00 4.25 0.214
## 5 2021-01-17 00:00:00 3.78 0.199
## 6 2021-01-18 00:00:00 3.88 0.212
## 7 2021-01-19 00:00:00 3.86 0.204
## 8 2021-01-20 00:00:00 4.65 0.207
## 9 2021-01-21 00:00:00 3.41 0.222
## 10 2021-01-22 00:00:00 4.05 0.231
## # ... with 20 more rows
8.9.3.3 Plot R^2
For the R^2 recall that we are looking at the correlation between the predictions made and what actually happened, so the higher the score the better, with a maximum score of 1 that would mean the predictions were 100% correlated with each other and therefore identical.
ggplot(accuracy_over_time, aes(x = date_utc, y = rsq, group = 1)) +
# Plot R^2 over time
geom_point(aes(x = date_utc, y = rsq), color = 'dark green', size = 2) +
geom_line(aes(x = date_utc, y = rsq), color = 'dark green', size = 1)
Refer back to the interactive dashboard to take a more specific subset of results instead of the aggregate analysis shown above.
References
Ren, Kun, and Kenton Russell. 2016. Formattable: Create Formattable Data Structures. https://CRAN.R-project.org/package=formattable.