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 
## 158.717716521   0.007071908 144.486422732

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.1732787        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 ($).

This step is particularly tedious, but it is important. As with the rest of this tutorial, try to understand what we are doing and why even if you find the code overwhelming. All we are doing in this “Adjust Prices” section is we are adjusting all of the prices to be represented as percentage change between observations, which will allow us to draw a fair comparison of the metrics across all cryptocurrencies, which would not be possible using the prices themselves. If you want to skip the tedious steps and want to see the performance of the models visualized, click here to skip ahead.

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:

cryptodata_nested <- mutate(cryptodata_nested,
                            lm_test_predictions = ifelse(split < 5,
                                                         map(lm_test_predictions, standardize_perc_change),
                                                         NA),
                            # Holdout for all splits
                            lm_holdout_predictions = map(lm_holdout_predictions, standardize_perc_change))

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:

actuals_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)
}
cryptodata_nested <- mutate(cryptodata_nested,
                            actuals_test = ifelse(split < 5,
                                             map(actuals_test, actuals_perc_change),
                                             NA),
                            actuals_holdout = map(actuals_holdout, actuals_perc_change))

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.2381645

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:

select(cryptodata_nested, lm_rsq_test, lm_rsq_holdout)
## # A tibble: 365 x 4
## # Groups:   symbol, split [365]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1       0.479        0.305  
##  2 ETH        1       0.512        0.127  
##  3 EOS        1       0.616        0.164  
##  4 LTC        1       0.129        0.353  
##  5 ADA        1       0.361        0.0314 
##  6 BSV        1       0.727        0.222  
##  7 ZEC        1       0.797        0.00834
##  8 HT         1       0.250        0.551  
##  9 TRX        1       0.700        0.156  
## 10 XMR        1       0.735        0.633  
## # ... with 355 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).

print(select(cryptodata_nested, lm_rmse_test, lm_rmse_holdout, lm_rsq_test, lm_rsq_holdout), n=500)
## # A tibble: 365 x 6
## # Groups:   symbol, split [365]
##     symbol split lm_rmse_test lm_rmse_holdout   lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>         <dbl>          <dbl>
##   1 BTC        1        0.238           0.391  0.479           0.305      
##   2 ETH        1        0.397           0.934  0.512           0.127      
##   3 EOS        1        0.610           0.670  0.616           0.164      
##   4 LTC        1        0.520           0.534  0.129           0.353      
##   5 ADA        1        0.251           0.674  0.361           0.0314     
##   6 BSV        1        0.690           0.518  0.727           0.222      
##   7 ZEC        1        0.282           0.743  0.797           0.00834    
##   8 HT         1        0.872           1.07   0.250           0.551      
##   9 TRX        1        0.236           0.557  0.700           0.156      
##  10 XMR        1        0.603           0.457  0.735           0.633      
##  11 KNC        1        0.889           1.80   0.113           0.145      
##  12 ZRX        1        0.526           2.47   0.282           0.144      
##  13 BAT        1        0.572           2.04   0.134           0.314      
##  14 BNT        1        0.487           0.983  0.466           0.0133     
##  15 MANA       1        0.496           0.661  0.0875          0.700      
##  16 ENJ        1        0.383           0.724  0.516           0.442      
##  17 XEM        1        0.354           1.06   0.558           0.00240    
##  18 BTG        1        0.565           0.646  0.369           0.182      
##  19 KMD        1        0.774           0.927  0.0639          0.00617    
##  20 ARDR       1        0.435           2.18   0.538           0.164      
##  21 ELF        1        0.438           1.42   0.432           0.00247    
##  22 NEXO       1        0.561           0.721  0.303           0.212      
##  23 CHZ        1        0.326           1.66   0.522           0.0664     
##  24 DCR        1        0.616           0.633  0.295           0.480      
##  25 VIB        1        1.08            0.935  0.0875          0.664      
##  26 AVA        1        0.383           0.744  0.489           0.0101     
##  27 GMTT       1        3.02           61.1    0.491           0.843      
##  28 CROOLD     1        1.12            0.867  0.0350          0.00160    
##  29 DODO       1        0.646           1.38   0.0198          0.553      
##  30 DYDX       1        1.48            3.47   0.00737         0.00274    
##  31 1INCH      1        1.82            2.23   0.0197          0.295      
##  32 BAKE       1        0.518           1.92   0.151           0.832      
##  33 DOT        1        0.528           1.58   0.0456          0.0708     
##  34 TOMO       1        4.47            0.108  0.000179       NA          
##  35 QTUM       1        0.557           0.888  0.658           0.0520     
##  36 GODS       1        1.52          377.     0.0602          0.000101   
##  37 IOTA       1        0.428           1.07   0.417           0.000000254
##  38 NEAR       1        0.481           0.867  0.159           0.0501     
##  39 AMB        1        0.701           0.901  0.440           0.165      
##  40 LSK        1        0.483           1.44   0.455           0.318      
##  41 PERP       1        0.991           1.96   0.318           0.168      
##  42 RIF        1        1.66            1.03   0.508           0.00254    
##  43 ATOM       1        0.432           0.785  0.497           0.0931     
##  44 ALICE      1        0.259           0.658  0.720           0.270      
##  45 MTLX       1        1.16          NaN      0.000159       NA          
##  46 KRRX       1        7.30            3.61   0.390           0.0889     
##  47 PLI        1        1.35            1.23   0.375           0.400      
##  48 XDC        1        0.987           0.440  0.0389          0.0825     
##  49 BDX        1        0.320           0.358  0.913           0.253      
##  50 SNX        1        1.30            2.11   0.0000518       0.00376    
##  51 THETA      1        0.386           1.14   0.359           0.000134   
##  52 RUNE       1        0.487           0.794  0.331           0.451      
##  53 KLAY       1        0.536           0.614  0.797           0.0712     
##  54 XCH        1        0.292           0.433  0.410           0.631      
##  55 BAL        1        0.489           1.10   0.304           0.0473     
##  56 REN        1        0.450           2.13   0.909           0.489      
##  57 ALCX       1        0.330           0.330  0.387          NA          
##  58 VGX        1      812.              8.27   0.0117          0.269      
##  59 GLMR       1        0.546          12.1    0.551           0.00234    
##  60 SAND       1        0.475           0.996  0.106           0.00955    
##  61 ANKR       1        0.755           0.779  0.134           0.177      
##  62 BCH        1        0.426           0.789  0.0539          0.423      
##  63 FTT        1        3.04            1.76   0.506           0.147      
##  64 GMT        1        0.587           3.04   0.000603        0.00462    
##  65 T          1        3.80            0.801  0.242           0.594      
##  66 DASH       1        0.815           0.416  0.390           0.711      
##  67 SXP        1        0.544           1.37   0.0000000761    0.133      
##  68 ETHW       1        0.628           3.80   0.186           0.722      
##  69 LAZIO      1        0.826         104.     0.157           0.000503   
##  70 GAS        1        0.920           0.719  0.318           0.226      
##  71 FIDA       1        4.26           28.2    0.381           0.0000439  
##  72 APT        1        0.415           1.30   0.263           0.201      
##  73 MXC        1       60.4           166.     0.190           0.00820    
##  74 TOMO       2        2.11            0.108  0.000193       NA          
##  75 GMTT       2        3.40           61.1    0.0000133       0.843      
##  76 ETHW       2        2.60            3.80   0.213           0.722      
##  77 HT         2        1.52            1.07   0.00881         0.551      
##  78 MTLX       2        2.43          NaN      0.814          NA          
##  79 ELF        2        1.13            1.42   0.218           0.00247    
##  80 PERP       2        0.281           1.96   0.875           0.168      
##  81 FTT        2        4.13            1.76   0.378           0.147      
##  82 REN        2        3.05            2.13   0.890           0.489      
##  83 BDX        2        0.207           0.358  0.918           0.253      
##  84 BAL        2        0.262           1.10   0.886           0.0473     
##  85 VGX        2       12.1             8.27   0.356           0.269      
##  86 BTC        2        0.137           0.391  0.758           0.305      
##  87 BSV        2        0.596           0.518  0.0375          0.222      
##  88 ARDR       2        1.46            2.18   0.730           0.164      
##  89 DCR        2        0.383           0.633  0.454           0.480      
##  90 XCH        2        0.175           0.433  0.612           0.631      
##  91 VIB        2        0.368           0.935  0.611           0.664      
##  92 DODO       2        0.455           1.38   0.210           0.553      
##  93 AMB        2        0.619           0.901  0.714           0.165      
##  94 LSK        2        1.66            1.44   0.0425          0.318      
##  95 XDC        2        1.34            0.440  0.000956        0.0825     
##  96 LTC        2        0.131           0.534  0.932           0.353      
##  97 ADA        2        0.462           0.674  0.506           0.0314     
##  98 ZEC        2        0.399           0.743  0.839           0.00834    
##  99 TRX        2        0.310           0.557  0.107           0.156      
## 100 KNC        2        0.217           1.80   0.880           0.145      
## 101 ZRX        2        0.176           2.47   0.879           0.144      
## 102 BAT        2        0.208           2.04   0.871           0.314      
## 103 MANA       2        0.772           0.661  0.681           0.700      
## 104 ENJ        2        0.262           0.724  0.837           0.442      
## 105 BTG        2        0.215           0.646  0.558           0.182      
## 106 KMD        2        0.387           0.927  0.618           0.00617    
## 107 NEXO       2        0.418           0.721  0.687           0.212      
## 108 CHZ        2        1.05            1.66   0.00362         0.0664     
## 109 AVA        2        0.455           0.744  0.497           0.0101     
## 110 CROOLD     2        1.28            0.867  0.105           0.00160    
## 111 QTUM       2        0.290           0.888  0.782           0.0520     
## 112 IOTA       2        0.147           1.07   0.951           0.000000254
## 113 T          2        0.629           0.801  0.829           0.594      
## 114 SXP        2        0.552           1.37   0.741           0.133      
## 115 ETH        2        0.164           0.934  0.878           0.127      
## 116 EOS        2        0.345           0.670  0.479           0.164      
## 117 DYDX       2        1.04            3.47   0.484           0.00274    
## 118 1INCH      2        0.241           2.23   0.673           0.295      
## 119 BAKE       2        1.00            1.92   0.448           0.832      
## 120 DOT        2        0.511           1.58   0.539           0.0708     
## 121 GODS       2      402.            377.     0.000328        0.000101   
## 122 NEAR       2        0.673           0.867  0.363           0.0501     
## 123 RIF        2        0.698           1.03   0.167           0.00254    
## 124 ATOM       2        0.167           0.785  0.881           0.0931     
## 125 ALICE      2        0.809           0.658  0.0398          0.270      
## 126 SNX        2        0.325           2.11   0.828           0.00376    
## 127 THETA      2        0.383           1.14   0.307           0.000134   
## 128 RUNE       2        0.255           0.794  0.754           0.451      
## 129 GLMR       2        0.386          12.1    0.416           0.00234    
## 130 SAND       2        0.777           0.996  0.210           0.00955    
## 131 ANKR       2        0.427           0.779  0.486           0.177      
## 132 BCH        2        0.356           0.789  0.161           0.423      
## 133 GMT        2        0.437           3.04   0.595           0.00462    
## 134 DASH       2        0.296           0.416  0.890           0.711      
## 135 LAZIO      2        4.89          104.     0.0455          0.000503   
## 136 PLI        2        1.23            1.23   0.458           0.400      
## 137 XEM        2        0.223           1.06   0.623           0.00240    
## 138 KLAY       2        0.142           0.614  0.906           0.0712     
## 139 XMR        2        0.459           0.457  0.497           0.633      
## 140 GAS        2        0.118           0.719  0.898           0.226      
## 141 ALCX       2        0.385           0.330  0.497          NA          
## 142 BNT        2        0.243           0.983  0.754           0.0133     
## 143 KRRX       2        1.56            3.61   0.876           0.0889     
## 144 APT        2        1.96            1.30   0.562           0.201      
## 145 FIDA       2        2.87           28.2    0.160           0.0000439  
## 146 MXC        2        5.13          166.     0.0297          0.00820    
## 147 APT        3        1.20            1.30   0.301           0.201      
## 148 TOMO       3        3.17            0.108  0.565          NA          
## 149 ETHW       3        1.10            3.80   0.141           0.722      
## 150 HT         3        0.500           1.07   0.286           0.551      
## 151 MTLX       3        4.52          NaN      0.272          NA          
## 152 GMTT       3       13.8            61.1    0.203           0.843      
## 153 PERP       3        0.899           1.96   0.247           0.168      
## 154 FTT        3        1.98            1.76   0.0790          0.147      
## 155 REN        3        0.345           2.13   0.572           0.489      
## 156 BDX        3        0.536           0.358  0.439           0.253      
## 157 ALCX       3        0.523           0.330  0.167          NA          
## 158 VGX        3        8.84            8.27   0.144           0.269      
## 159 BAL        3        0.179           1.10   0.843           0.0473     
## 160 BTC        3        0.172           0.391  0.0225          0.305      
## 161 ARDR       3        0.372           2.18   0.175           0.164      
## 162 XCH        3        0.168           0.433  0.899           0.631      
## 163 DODO       3        0.261           1.38   0.749           0.553      
## 164 AMB        3        1.07            0.901  0.000315        0.165      
## 165 LSK        3        0.580           1.44   0.111           0.318      
## 166 CROOLD     3       11.2             0.867  0.134           0.00160    
## 167 QTUM       3        0.605           0.888  0.0219          0.0520     
## 168 IOTA       3        0.249           1.07   0.714           0.000000254
## 169 T          3        0.664           0.801  0.224           0.594      
## 170 LTC        3        0.288           0.534  0.762           0.353      
## 171 ADA        3        0.604           0.674  0.369           0.0314     
## 172 ZEC        3        0.685           0.743  0.512           0.00834    
## 173 TRX        3        0.631           0.557  0.756           0.156      
## 174 KNC        3        0.322           1.80   0.698           0.145      
## 175 ZRX        3        0.629           2.47   0.0334          0.144      
## 176 BAT        3        0.409           2.04   0.533           0.314      
## 177 MANA       3        1.22            0.661  0.0252          0.700      
## 178 ENJ        3        0.337           0.724  0.770           0.442      
## 179 BTG        3        1.44            0.646  0.261           0.182      
## 180 KMD        3        0.445           0.927  0.0449          0.00617    
## 181 NEXO       3        0.920           0.721  0.427           0.212      
## 182 CHZ        3        0.644           1.66   0.460           0.0664     
## 183 AVA        3        0.684           0.744  0.193           0.0101     
## 184 SXP        3        0.225           1.37   0.825           0.133      
## 185 EOS        3        0.311           0.670  0.757           0.164      
## 186 ETH        3        0.387           0.934  0.0421          0.127      
## 187 DYDX       3        0.949           3.47   0.214           0.00274    
## 188 1INCH      3        0.330           2.23   0.388           0.295      
## 189 BAKE       3      106.              1.92   0.102           0.832      
## 190 DOT        3        0.470           1.58   0.442           0.0708     
## 191 GODS       3        1.31          377.     0.00906         0.000101   
## 192 NEAR       3        3.28            0.867  0.306           0.0501     
## 193 RIF        3        0.400           1.03   0.202           0.00254    
## 194 ATOM       3        0.964           0.785  0.000638        0.0931     
## 195 ALICE      3        0.514           0.658  0.349           0.270      
## 196 SNX        3        0.689           2.11   0.704           0.00376    
## 197 THETA      3        0.511           1.14   0.0243          0.000134   
## 198 RUNE       3        0.202           0.794  0.747           0.451      
## 199 GLMR       3        0.233          12.1    0.908           0.00234    
## 200 SAND       3        0.654           0.996  0.680           0.00955    
## 201 ANKR       3        0.227           0.779  0.859           0.177      
## 202 BCH        3        0.268           0.789  0.712           0.423      
## 203 GMT        3        0.216           3.04   0.901           0.00462    
## 204 DASH       3        1.32            0.416  0.000416        0.711      
## 205 LAZIO      3        8.19          104.     0.659           0.000503   
## 206 XDC        3        0.253           0.440  0.884           0.0825     
## 207 PLI        3        1.18            1.23   0.287           0.400      
## 208 BSV        3        0.412           0.518  0.388           0.222      
## 209 XEM        3        0.291           1.06   0.345           0.00240    
## 210 DCR        3        0.249           0.633  0.682           0.480      
## 211 KLAY       3        0.283           0.614  0.618           0.0712     
## 212 XMR        3        0.369           0.457  0.715           0.633      
## 213 GAS        3        0.298           0.719  0.631           0.226      
## 214 VIB        3        1.13            0.935  0.197           0.664      
## 215 BNT        3        0.388           0.983  0.179           0.0133     
## 216 FIDA       3        0.892          28.2    0.172           0.0000439  
## 217 ELF        3        0.400           1.42   0.128           0.00247    
## 218 KRRX       3        3.23            3.61   0.0668          0.0889     
## 219 MXC        3        1.48          166.     0.0126          0.00820    
## 220 APT        4     2388.              1.30   0.985           0.201      
## 221 TOMO       4        2.42            0.108  0.000713       NA          
## 222 ETHW       4        1.19            3.80   0.208           0.722      
## 223 HT         4        1.12            1.07   0.0236          0.551      
## 224 MTLX       4        1.03          NaN      0.294          NA          
## 225 ALCX       4        3.11            0.330  0.992          NA          
## 226 PERP       4        2.88            1.96   0.269           0.168      
## 227 GMTT       4       25.9            61.1    0.693           0.843      
## 228 FTT        4        0.582           1.76   0.719           0.147      
## 229 BDX        4       72.3             0.358  0.816           0.253      
## 230 BAL        4        0.423           1.10   0.327           0.0473     
## 231 XCH        4        0.780           0.433  0.948           0.631      
## 232 BTC        4        0.824           0.391  0.761           0.305      
## 233 ARDR       4       16.3             2.18   0.444           0.164      
## 234 DODO       4        0.439           1.38   0.941           0.553      
## 235 LSK        4        0.852           1.44   0.0255          0.318      
## 236 CROOLD     4        4.66            0.867  0.303           0.00160    
## 237 QTUM       4        0.363           0.888  0.939           0.0520     
## 238 IOTA       4        0.752           1.07   0.620           0.000000254
## 239 AMB        4        0.511           0.901  0.941           0.165      
## 240 REN        4        0.482           2.13   0.913           0.489      
## 241 T          4        0.725           0.801  0.917           0.594      
## 242 SXP        4        0.966           1.37   0.317           0.133      
## 243 LTC        4        0.668           0.534  0.547           0.353      
## 244 ADA        4        0.969           0.674  0.651           0.0314     
## 245 ZEC        4        0.911           0.743  0.772           0.00834    
## 246 TRX        4        0.217           0.557  0.834           0.156      
## 247 KNC        4        0.552           1.80   0.842           0.145      
## 248 ZRX        4        0.507           2.47   0.879           0.144      
## 249 BAT        4        0.606           2.04   0.624           0.314      
## 250 MANA       4        0.935           0.661  0.807           0.700      
## 251 ENJ        4        1.13            0.724  0.261           0.442      
## 252 BTG        4        0.438           0.646  0.815           0.182      
## 253 KMD        4        1.07            0.927  0.374           0.00617    
## 254 NEXO       4        2.21            0.721  0.00236         0.212      
## 255 CHZ        4        0.671           1.66   0.600           0.0664     
## 256 AVA        4        0.228           0.744  0.941           0.0101     
## 257 XDC        4        0.682           0.440  0.131           0.0825     
## 258 DASH       4        0.601           0.416  0.781           0.711      
## 259 ETH        4        0.612           0.934  0.698           0.127      
## 260 EOS        4        0.502           0.670  0.773           0.164      
## 261 DYDX       4        0.972           3.47   0.952           0.00274    
## 262 1INCH      4        0.470           2.23   0.928           0.295      
## 263 BAKE       4       72.3             1.92   0.170           0.832      
## 264 DOT        4        0.376           1.58   0.929           0.0708     
## 265 GODS       4        2.03          377.     0.216           0.000101   
## 266 NEAR       4        0.632           0.867  0.923           0.0501     
## 267 RIF        4        0.953           1.03   0.381           0.00254    
## 268 ATOM       4        0.627           0.785  0.828           0.0931     
## 269 ALICE      4        0.458           0.658  0.933           0.270      
## 270 SNX        4        0.999           2.11   0.857           0.00376    
## 271 THETA      4        0.797           1.14   0.831           0.000134   
## 272 RUNE       4        0.401           0.794  0.905           0.451      
## 273 GLMR       4        5.87           12.1    0.725           0.00234    
## 274 SAND       4        0.603           0.996  0.920           0.00955    
## 275 ANKR       4        1.09            0.779  0.485           0.177      
## 276 BCH        4        0.376           0.789  0.862           0.423      
## 277 GMT        4        1.04            3.04   0.861           0.00462    
## 278 LAZIO      4        0.583         104.     0.634           0.000503   
## 279 PLI        4        1.34            1.23   0.0255          0.400      
## 280 KLAY       4        0.483           0.614  0.783           0.0712     
## 281 DCR        4        0.429           0.633  0.936           0.480      
## 282 GAS        4        1.56            0.719  0.538           0.226      
## 283 BSV        4        0.514           0.518  0.638           0.222      
## 284 VIB        4       11.2             0.935  0.0171          0.664      
## 285 XEM        4        0.430           1.06   0.868           0.00240    
## 286 XMR        4        1.20            0.457  0.651           0.633      
## 287 BNT        4        0.387           0.983  0.910           0.0133     
## 288 VGX        4        3.36            8.27   0.0760          0.269      
## 289 FIDA       4        1.16           28.2    0.181           0.0000439  
## 290 ELF        4        0.347           1.42   0.539           0.00247    
## 291 KRRX       4        2.25            3.61   0.0317          0.0889     
## 292 MXC        4      833.            166.     0.0330          0.00820    
## 293 TOMO       5       NA               0.108 NA              NA          
## 294 MTLX       5       NA             NaN     NA              NA          
## 295 PERP       5       NA               1.96  NA               0.168      
## 296 GMTT       5       NA              61.1   NA               0.843      
## 297 BDX        5       NA               0.358 NA               0.253      
## 298 ALCX       5       NA               0.330 NA              NA          
## 299 CROOLD     5       NA               0.867 NA               0.00160    
## 300 QTUM       5       NA               0.888 NA               0.0520     
## 301 IOTA       5       NA               1.07  NA               0.000000254
## 302 T          5       NA               0.801 NA               0.594      
## 303 XDC        5       NA               0.440 NA               0.0825     
## 304 SXP        5       NA               1.37  NA               0.133      
## 305 DODO       5       NA               1.38  NA               0.553      
## 306 DASH       5       NA               0.416 NA               0.711      
## 307 LTC        5       NA               0.534 NA               0.353      
## 308 ADA        5       NA               0.674 NA               0.0314     
## 309 ZEC        5       NA               0.743 NA               0.00834    
## 310 TRX        5       NA               0.557 NA               0.156      
## 311 KNC        5       NA               1.80  NA               0.145      
## 312 ZRX        5       NA               2.47  NA               0.144      
## 313 BAT        5       NA               2.04  NA               0.314      
## 314 MANA       5       NA               0.661 NA               0.700      
## 315 ENJ        5       NA               0.724 NA               0.442      
## 316 BTG        5       NA               0.646 NA               0.182      
## 317 KMD        5       NA               0.927 NA               0.00617    
## 318 NEXO       5       NA               0.721 NA               0.212      
## 319 CHZ        5       NA               1.66  NA               0.0664     
## 320 AVA        5       NA               0.744 NA               0.0101     
## 321 LSK        5       NA               1.44  NA               0.318      
## 322 KLAY       5       NA               0.614 NA               0.0712     
## 323 BTC        5       NA               0.391 NA               0.305      
## 324 ETH        5       NA               0.934 NA               0.127      
## 325 EOS        5       NA               0.670 NA               0.164      
## 326 BSV        5       NA               0.518 NA               0.222      
## 327 XMR        5       NA               0.457 NA               0.633      
## 328 ARDR       5       NA               2.18  NA               0.164      
## 329 DCR        5       NA               0.633 NA               0.480      
## 330 DYDX       5       NA               3.47  NA               0.00274    
## 331 1INCH      5       NA               2.23  NA               0.295      
## 332 BAKE       5       NA               1.92  NA               0.832      
## 333 DOT        5       NA               1.58  NA               0.0708     
## 334 GODS       5       NA             377.    NA               0.000101   
## 335 NEAR       5       NA               0.867 NA               0.0501     
## 336 RIF        5       NA               1.03  NA               0.00254    
## 337 ATOM       5       NA               0.785 NA               0.0931     
## 338 ALICE      5       NA               0.658 NA               0.270      
## 339 PLI        5       NA               1.23  NA               0.400      
## 340 SNX        5       NA               2.11  NA               0.00376    
## 341 THETA      5       NA               1.14  NA               0.000134   
## 342 RUNE       5       NA               0.794 NA               0.451      
## 343 XCH        5       NA               0.433 NA               0.631      
## 344 GLMR       5       NA              12.1   NA               0.00234    
## 345 SAND       5       NA               0.996 NA               0.00955    
## 346 ANKR       5       NA               0.779 NA               0.177      
## 347 BCH        5       NA               0.789 NA               0.423      
## 348 GMT        5       NA               3.04  NA               0.00462    
## 349 LAZIO      5       NA             104.    NA               0.000503   
## 350 VIB        5       NA               0.935 NA               0.664      
## 351 GAS        5       NA               0.719 NA               0.226      
## 352 AMB        5       NA               0.901 NA               0.165      
## 353 ETHW       5       NA               3.80  NA               0.722      
## 354 XEM        5       NA               1.06  NA               0.00240    
## 355 BAL        5       NA               1.10  NA               0.0473     
## 356 REN        5       NA               2.13  NA               0.489      
## 357 BNT        5       NA               0.983 NA               0.0133     
## 358 VGX        5       NA               8.27  NA               0.269      
## 359 FTT        5       NA               1.76  NA               0.147      
## 360 FIDA       5       NA              28.2   NA               0.0000439  
## 361 ELF        5       NA               1.42  NA               0.00247    
## 362 HT         5       NA               1.07  NA               0.551      
## 363 KRRX       5       NA               3.61  NA               0.0889     
## 364 MXC        5       NA             166.    NA               0.00820    
## 365 APT        5       NA               1.30  NA               0.201

Out of 365 groups, 72 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:

select(cryptodata_nested, lm_rmse_test, lm_rsq_test, lm_rmse_holdout, lm_rsq_holdout)
## # A tibble: 365 x 6
## # Groups:   symbol, split [365]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        0.238       0.479           0.391        0.305  
##  2 ETH        1        0.397       0.512           0.934        0.127  
##  3 EOS        1        0.610       0.616           0.670        0.164  
##  4 LTC        1        0.520       0.129           0.534        0.353  
##  5 ADA        1        0.251       0.361           0.674        0.0314 
##  6 BSV        1        0.690       0.727           0.518        0.222  
##  7 ZEC        1        0.282       0.797           0.743        0.00834
##  8 HT         1        0.872       0.250           1.07         0.551  
##  9 TRX        1        0.236       0.700           0.557        0.156  
## 10 XMR        1        0.603       0.735           0.457        0.633  
## # ... with 355 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:

cryptodata_metrics <- group_by(select(ungroup(cryptodata_nested),-split),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: 365 x 3
## # Groups:   symbol [73]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.343
##  2 ETH    lm    0.390
##  3 EOS    lm    0.442
##  4 LTC    lm    0.402
##  5 ADA    lm    0.572
##  6 BSV    lm    0.553
##  7 ZEC    lm    0.569
##  8 HT     lm    1.00 
##  9 TRX    lm    0.348
## 10 XMR    lm    0.657
## # ... with 355 more rows

Now tag the results as having been for the test set:

rmse_test$eval_set <- 'test'

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: 365 x 3
## # Groups:   symbol [73]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.391
##  2 ETH    lm    0.934
##  3 EOS    lm    0.670
##  4 LTC    lm    0.534
##  5 ADA    lm    0.674
##  6 BSV    lm    0.518
##  7 ZEC    lm    0.743
##  8 HT     lm    1.07 
##  9 TRX    lm    0.557
## 10 XMR    lm    0.457
## # ... with 355 more rows

Now tag the results as having been for the holdout set:

rmse_holdout$eval_set <- 'holdout'

8.5.3 Union Results

Now we can union() the results to stack the rows from the two datasets on top of each other:

rmse_scores <- union(rmse_test, rmse_holdout)

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: 365 x 3
## # Groups:   symbol [73]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.505
##  2 ETH    lm    0.533
##  3 EOS    lm    0.656
##  4 LTC    lm    0.593
##  5 ADA    lm    0.472
##  6 BSV    lm    0.448
##  7 ZEC    lm    0.730
##  8 HT     lm    0.142
##  9 TRX    lm    0.599
## 10 XMR    lm    0.649
## # ... with 355 more rows

Now tag the results as having been for the test set

rsq_test$eval_set <- 'test'

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: 365 x 3
## # Groups:   symbol [73]
##    symbol model     rsq
##    <chr>  <chr>   <dbl>
##  1 BTC    lm    0.305  
##  2 ETH    lm    0.127  
##  3 EOS    lm    0.164  
##  4 LTC    lm    0.353  
##  5 ADA    lm    0.0314 
##  6 BSV    lm    0.222  
##  7 ZEC    lm    0.00834
##  8 HT     lm    0.551  
##  9 TRX    lm    0.156  
## 10 XMR    lm    0.633  
## # ... with 355 more rows

Now tag the results as having been for the holdout set:

rsq_holdout$eval_set <- 'holdout'

8.6.3 Union Results

rsq_scores <- union(rsq_test, rsq_holdout)

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.1 RMSE Visualization

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:

plot_scores <- merge(rmse_scores, rsq_scores)

8.7.2.2 Plot Results

Now we can plot the results on a chart:

ggplot(plot_scores, aes(x=rsq, y=rmse, color = model)) +
  geom_point() +
  ylim(c(0,10))

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):

# register board
board_register("github", repo = "predictcrypto/pins", token=pins_key)
# Add current date time
plot_scores$last_refreshed <- Sys.time()
# pin data
pin(plot_scores, board='github', name='crypto_tutorial_results_latest')

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).

The app shown above also has a button to Show Code. If you were to show the code and copy and paste it into an RStudio session on your computer into a file with the .Rmd file extension and you then Knit the file, the same exact app should show up on your computer, no logins or setup outside of the packages required for the code to run; RStudio should automatically prompt you to install packages that are not currently installed on your computer.

8.9 Visualizations - Historical Metrics

We can pull the same data into this R session using the pin_get() function:

metrics_historical <- pin_get(name = "full_metrics")

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.1.2 Plot RMSE by Model

ggplot(best_models, aes(model, rmse, fill = eval_set)) + 
  geom_bar(stat = "identity", position = 'dodge') +
  ggtitle('RMSE by Model', 'Comparing Test and Holdout')

8.9.1.3 Plot \(R^2\) by Model

ggplot(best_models, aes(model, rsq, fill = eval_set)) + 
  geom_bar(stat = "identity", position = 'dodge') +
  ggtitle('R^2 by Model', 'Comparing Test and Holdout')

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.2 Plot RMSE

Remember, for RMSE the lower the score, the more accurate the models were.

ggplot(accuracy_over_time, aes(x = date_utc, y = rmse, group = 1)) +
  # Plot RMSE over time
  geom_point(color = 'red', size = 2) +
  geom_line(color = 'red', size = 1)

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.