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 
## 1827.23771363    0.08571352 1676.74099795

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.04194342         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.9356534

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: 445 x 4
## # Groups:   symbol, split [445]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1       0.761          0.496
##  2 ETH        1       0.564          0.519
##  3 EOS        1       0.715          0.811
##  4 LTC        1       0.767          0.709
##  5 BSV        1       0.104          0.418
##  6 ADA        1       0.503          0.499
##  7 ZEC        1       0.625          0.356
##  8 TRX        1       0.382          0.643
##  9 HT         1       0.447          0.195
## 10 XMR        1       0.773          0.120
## # ... with 435 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: 445 x 6
## # Groups:   symbol, split [445]
##     symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>       <dbl>          <dbl>
##   1 BTC        1       0.936           0.481     0.761          0.496   
##   2 ETH        1       1.47            0.725     0.564          0.519   
##   3 EOS        1       0.798           0.547     0.715          0.811   
##   4 LTC        1       1.19            0.591     0.767          0.709   
##   5 BSV        1       2.22            0.567     0.104          0.418   
##   6 ADA        1       1.10            0.884     0.503          0.499   
##   7 ZEC        1       1.23            0.726     0.625          0.356   
##   8 TRX        1       1.09            0.327     0.382          0.643   
##   9 HT         1       1.41            0.855     0.447          0.195   
##  10 XMR        1       0.692           0.877     0.773          0.120   
##  11 KNC        1       1.41            0.529     0.514          0.732   
##  12 ZRX        1       1.27            0.355     0.647          0.901   
##  13 BAT        1       1.30            0.570     0.514          0.722   
##  14 BNT        1       4.97            0.677     0.0254         0.569   
##  15 MANA       1       1.18            0.586     0.318          0.571   
##  16 DGB        1       1.54            0.908     0.251          0.413   
##  17 ENJ        1       1.20            0.625     0.554          0.596   
##  18 XEM        1       1.28            0.326     0.344          0.856   
##  19 BTG        1       1.12            0.447     0.675          0.811   
##  20 KMD        1       1.40            1.00      0.222          0.149   
##  21 ARDR       1       0.815           1.31      0.685          0.204   
##  22 NEXO       1       1.57            1.48      0.484          0.0721  
##  23 CHZ        1       1.08            1.29      0.518          0.868   
##  24 BRD        1      29.9            10.5       0.0437         0.0270  
##  25 DCR        1       1.53            1.62      0.153          0.0421  
##  26 WAXP       1       1.04            0.354     0.601          0.776   
##  27 OAX        1      24.6             5.49      0.548          0.00555 
##  28 LEO        1       2.08            0.606     0.0758         0.714   
##  29 AVA        1       1.02            0.934     0.735          0.397   
##  30 GMTT       1       2.19            0.846     0.371          0.474   
##  31 CROOLD     1       0.846           0.550     0.741          0.473   
##  32 ARNM       1       3.16            7.91      0.917          0.757   
##  33 GAS        1       1.95            1.22      0.221          0.202   
##  34 LEVL       1      17.1             2.89      0.0632         0.470   
##  35 DODO       1       1.81            1.08      0.235          0.0535  
##  36 DYDX       1       1.42            1.79      0.640          0.0262  
##  37 1INCH      1       1.43            0.822     0.788          0.642   
##  38 BAKE       1       1.32            0.971     0.492          0.423   
##  39 BTRST      1      13.3             8.21      0.631          0.00152 
##  40 SKL        1       1.14            1.09      0.606          0.531   
##  41 DOT        1       1.36            0.841     0.499          0.605   
##  42 LINA       1       1.27            0.926     0.542          0.514   
##  43 TOMO       1       1.28            0.952     0.632          0.451   
##  44 QTUM       1       0.883           0.484     0.701          0.767   
##  45 ATA        1       2.64            1.18      0.509          0.705   
##  46 IOTA       1       1.23            0.480     0.458          0.746   
##  47 TORN       1       1.17            0.985     0.439          0.910   
##  48 IOST       1       1.32            0.450     0.356          0.702   
##  49 NEAR       1       1.16            0.938     0.513          0.495   
##  50 ZKS        1       8.62          149.        0.656          0.166   
##  51 LSK        1       1.17            0.391     0.449          0.802   
##  52 PERP       1       1.42            1.58      0.365          0.326   
##  53 RIF        1       0.835           0.618     0.896          0.676   
##  54 UMA        1       1.81            2.62      0.295          0.224   
##  55 ATOM       1       1.25            0.699     0.631          0.539   
##  56 ALICE      1       1.30            0.609     0.621          0.774   
##  57 MTLX       1       1.40            3.88      0.185          0.0911  
##  58 G999       1       4.32          NaN         0.00143       NA       
##  59 XDC        1       1.39            0.302     0.346          0.760   
##  60 MC         1       7.28            8.70      0.0408         0.0403  
##  61 BDX        1       1.23            0.246     0.308          0.878   
##  62 TRIBE      1       0.641           1.04      0.854          0.129   
##  63 SNX        1       2.48            1.10      0.904          0.504   
##  64 THETA      1       1.67            0.577     0.405          0.824   
##  65 RUNE       1       1.40            1.29      0.570          0.243   
##  66 KLAY       1       0.885           0.425     0.538          0.732   
##  67 BAL        1       1.22            1.35      0.643          0.175   
##  68 XCH        1       2.42            0.609     0.803          0.200   
##  69 REN        1       1.85            0.966     0.628          0.487   
##  70 ALCX       1       5.00            1.26      0.137          0.174   
##  71 VGX        1       2.07            8.75      0.499          0.546   
##  72 SAND       1       0.863           0.864     0.688          0.422   
##  73 ANKR       1       1.36            4.42      0.558          0.559   
##  74 FTT        1       1.06            0.734     0.753          0.157   
##  75 BCH        1       1.51            0.610     0.324          0.674   
##  76 CRPT       1       7.85            1.24      0.524          0.221   
##  77 MIR        1       1.85            1.45      0.123          0.000902
##  78 GMT        1       1.50            1.91      0.783          0.0422  
##  79 DASH       1       1.12            0.628     0.392          0.741   
##  80 SXP        1       1.48            0.388     0.553          0.873   
##  81 SFP        1       1.63            5.61      0.177          0.521   
##  82 BICO       1       1.52            0.941     0.225          0.376   
##  83 EURS       1       0.0494          0.0985    0.584          0.693   
##  84 NMR        1       1.49            1.24      0.333          0.508   
##  85 CELR       1       1.83            1.28      0.290          0.169   
##  86 ICP        1       1.41            1.26      0.101          0.0942  
##  87 VIB        1      48.9            19.3       0.237          0.429   
##  88 SPI        1       4.50            3.42      0.0323         0.00890 
##  89 INJ        1      14.1            17.4       0.133          0.769   
##  90 ZKS        2      15.7           149.        0.113          0.166   
##  91 SPI        2       3.17            3.42      0.0167         0.00890 
##  92 BTRST      2      17.0             8.21      0.345          0.00152 
##  93 GAS        2       1.42            1.22      0.241          0.202   
##  94 ALCX       2       1.94            1.26      0.685          0.174   
##  95 G999       2       3.21          NaN         0.0383        NA       
##  96 KMD        2       2.12            1.00      0.820          0.149   
##  97 VGX        2      14.0             8.75      0.881          0.546   
##  98 CRPT       2       4.99            1.24      0.406          0.221   
##  99 LSK        2       0.695           0.391     0.305          0.802   
## 100 DASH       2       0.711           0.628     0.610          0.741   
## 101 DCR        2       0.505           1.62      0.730          0.0421  
## 102 OAX        2       6.40            5.49      0.0812         0.00555 
## 103 LEVL       2       2.44            2.89      0.0125         0.470   
## 104 UMA        2       1.88            2.62      0.201          0.224   
## 105 BTC        2       0.257           0.481     0.936          0.496   
## 106 ETH        2       0.431           0.725     0.914          0.519   
## 107 EOS        2       0.400           0.547     0.870          0.811   
## 108 LTC        2       0.384           0.591     0.650          0.709   
## 109 BSV        2       0.619           0.567     0.790          0.418   
## 110 ADA        2       0.482           0.884     0.725          0.499   
## 111 ZEC        2       0.424           0.726     0.910          0.356   
## 112 HT         2       0.714           0.855     0.492          0.195   
## 113 TRX        2       0.508           0.327     0.805          0.643   
## 114 KNC        2       0.788           0.529     0.566          0.732   
## 115 ZRX        2       0.452           0.355     0.698          0.901   
## 116 BAT        2       0.642           0.570     0.447          0.722   
## 117 BNT        2       1.89            0.677     0.167          0.569   
## 118 MANA       2       0.756           0.586     0.706          0.571   
## 119 DGB        2       0.661           0.908     0.678          0.413   
## 120 ENJ        2       0.812           0.625     0.730          0.596   
## 121 XEM        2       3.36            0.326     0.482          0.856   
## 122 BTG        2       0.439           0.447     0.836          0.811   
## 123 ARDR       2       0.855           1.31      0.505          0.204   
## 124 NEXO       2       0.766           1.48      0.808          0.0721  
## 125 CHZ        2       0.547           1.29      0.820          0.868   
## 126 LEO        2       0.910           0.606     0.00538        0.714   
## 127 AVA        2       0.924           0.934     0.419          0.397   
## 128 CROOLD     2       0.411           0.550     0.820          0.473   
## 129 ARNM       2       5.28            7.91      0.879          0.757   
## 130 DODO       2       1.27            1.08      0.294          0.0535  
## 131 DYDX       2       2.12            1.79      0.812          0.0262  
## 132 1INCH      2       1.13            0.822     0.103          0.642   
## 133 BAKE       2       1.32            0.971     0.710          0.423   
## 134 SKL        2       0.879           1.09      0.611          0.531   
## 135 DOT        2       0.369           0.841     0.886          0.605   
## 136 LINA       2       0.549           0.926     0.848          0.514   
## 137 QTUM       2       0.495           0.484     0.735          0.767   
## 138 TOMO       2       0.613           0.952     0.746          0.451   
## 139 ATA        2       0.665           1.18      0.511          0.705   
## 140 IOTA       2       0.594           0.480     0.687          0.746   
## 141 TORN       2       2.63            0.985     0.0304         0.910   
## 142 IOST       2       0.520           0.450     0.724          0.702   
## 143 NEAR       2       0.620           0.938     0.806          0.495   
## 144 PERP       2       2.58            1.58      0.422          0.326   
## 145 RIF        2       0.148           0.618     0.903          0.676   
## 146 ATOM       2       0.763           0.699     0.697          0.539   
## 147 ALICE      2       0.781           0.609     0.660          0.774   
## 148 MTLX       2       3.69            3.88      0.252          0.0911  
## 149 XDC        2       0.371           0.302     0.916          0.760   
## 150 MC         2       6.60            8.70      0.00563        0.0403  
## 151 BDX        2       0.332           0.246     0.644          0.878   
## 152 TRIBE      2       1.01            1.04      0.0249         0.129   
## 153 SNX        2       1.13            1.10      0.818          0.504   
## 154 THETA      2       0.639           0.577     0.806          0.824   
## 155 RUNE       2       0.553           1.29      0.954          0.243   
## 156 KLAY       2       0.445           0.425     0.832          0.732   
## 157 ANKR       2       0.470           4.42      0.717          0.559   
## 158 XMR        2       0.461           0.877     0.777          0.120   
## 159 WAXP       2       4.55            0.354     0.449          0.776   
## 160 BAL        2       0.543           1.35      0.863          0.175   
## 161 XCH        2       1.01            0.609     0.0630         0.200   
## 162 REN        2       1.53            0.966     0.724          0.487   
## 163 SAND       2       0.570           0.864     0.943          0.422   
## 164 BCH        2       0.466           0.610     0.788          0.674   
## 165 FTT        2       0.296           0.734     0.910          0.157   
## 166 GMT        2       0.751           1.91      0.912          0.0422  
## 167 MIR        2       0.835           1.45      0.534          0.000902
## 168 SFP        2       0.669           5.61      0.705          0.521   
## 169 BICO       2       1.44            0.941     0.277          0.376   
## 170 EURS       2       0.0892          0.0985    0.195          0.693   
## 171 CELR       2       0.983           1.28      0.728          0.169   
## 172 VIB        2      37.0            19.3       0.578          0.429   
## 173 SXP        2       0.432           0.388     0.863          0.873   
## 174 NMR        2      14.1             1.24      0.00397        0.508   
## 175 GMTT       2      12.1             0.846     0.0569         0.474   
## 176 BRD        2       8.36           10.5       0.144          0.0270  
## 177 ICP        2       2.91            1.26      0.0539         0.0942  
## 178 ZKS        3      13.1           149.        0.0522         0.166   
## 179 SPI        3       2.00            3.42      0.870          0.00890 
## 180 BTRST      3      11.6             8.21      0.0404         0.00152 
## 181 GAS        3       0.627           1.22      0.689          0.202   
## 182 ALCX       3       4.35            1.26      0.551          0.174   
## 183 G999       3      10.4           NaN         0.213         NA       
## 184 KMD        3      11.0             1.00      0.0376         0.149   
## 185 OAX        3      13.5             5.49      0.413          0.00555 
## 186 VGX        3      31.2             8.75      0.000307       0.546   
## 187 WAXP       3       0.623           0.354     0.727          0.776   
## 188 DASH       3       0.578           0.628     0.835          0.741   
## 189 DCR        3       1.99            1.62      0.0372         0.0421  
## 190 LSK        3       0.612           0.391     0.528          0.802   
## 191 VIB        3      29.5            19.3       0.0299         0.429   
## 192 UMA        3       0.407           2.62      0.655          0.224   
## 193 ANKR       3       0.917           4.42      0.562          0.559   
## 194 BTC        3       0.596           0.481     0.840          0.496   
## 195 ETH        3       1.59            0.725     0.803          0.519   
## 196 EOS        3       0.678           0.547     0.691          0.811   
## 197 LTC        3       1.48            0.591     0.138          0.709   
## 198 ADA        3       0.759           0.884     0.773          0.499   
## 199 BSV        3       0.428           0.567     0.875          0.418   
## 200 ZEC        3       0.870           0.726     0.614          0.356   
## 201 HT         3       0.611           0.855     0.689          0.195   
## 202 TRX        3       0.310           0.327     0.762          0.643   
## 203 KNC        3       2.09            0.529     0.378          0.732   
## 204 XMR        3       0.819           0.877     0.450          0.120   
## 205 ZRX        3       0.724           0.355     0.702          0.901   
## 206 BAT        3       0.697           0.570     0.788          0.722   
## 207 BNT        3       1.44            0.677     0.447          0.569   
## 208 MANA       3       1.05            0.586     0.626          0.571   
## 209 DGB        3       1.50            0.908     0.418          0.413   
## 210 ENJ        3       0.832           0.625     0.821          0.596   
## 211 BTG        3       1.46            0.447     0.804          0.811   
## 212 ARDR       3       0.717           1.31      0.488          0.204   
## 213 NEXO       3       1.56            1.48      0.583          0.0721  
## 214 CHZ        3       0.958           1.29      0.494          0.868   
## 215 LEO        3       5.79            0.606     0.000545       0.714   
## 216 AVA        3       6.85            0.934     0.000426       0.397   
## 217 CROOLD     3       0.853           0.550     0.704          0.473   
## 218 ARNM       3       5.79            7.91      0.0146         0.757   
## 219 DODO       3       0.788           1.08      0.768          0.0535  
## 220 DYDX       3       0.675           1.79      0.820          0.0262  
## 221 1INCH      3       1.49            0.822     0.518          0.642   
## 222 BAKE       3       1.01            0.971     0.638          0.423   
## 223 SKL        3       2.43            1.09      0.0211         0.531   
## 224 DOT        3       1.20            0.841     0.669          0.605   
## 225 LINA       3       0.755           0.926     0.729          0.514   
## 226 TOMO       3       1.42            0.952     0.775          0.451   
## 227 QTUM       3       0.810           0.484     0.839          0.767   
## 228 ATA        3       1.58            1.18      0.315          0.705   
## 229 IOTA       3       0.656           0.480     0.777          0.746   
## 230 TORN       3       1.48            0.985     0.455          0.910   
## 231 IOST       3       0.394           0.450     0.858          0.702   
## 232 NEAR       3       1.47            0.938     0.821          0.495   
## 233 PERP       3       1.57            1.58      0.815          0.326   
## 234 RIF        3       3.93            0.618     0.952          0.676   
## 235 ATOM       3       0.917           0.699     0.457          0.539   
## 236 ALICE      3       0.903           0.609     0.642          0.774   
## 237 MTLX       3       2.51            3.88      0.00150        0.0911  
## 238 XDC        3       1.93            0.302     0.0155         0.760   
## 239 MC         3      13.4             8.70      0.154          0.0403  
## 240 BDX        3       0.713           0.246     0.496          0.878   
## 241 SNX        3       1.22            1.10      0.427          0.504   
## 242 TRIBE      3       0.568           1.04      0.110          0.129   
## 243 THETA      3       0.685           0.577     0.791          0.824   
## 244 RUNE       3       1.38            1.29      0.830          0.243   
## 245 KLAY       3       0.399           0.425     0.912          0.732   
## 246 XEM        3       0.619           0.326     0.785          0.856   
## 247 BAL        3       1.60            1.35      0.550          0.175   
## 248 XCH        3       0.398           0.609     0.914          0.200   
## 249 REN        3       1.63            0.966     0.265          0.487   
## 250 SAND       3       1.74            0.864     0.282          0.422   
## 251 FTT        3       1.05            0.734     0.306          0.157   
## 252 BCH        3       0.805           0.610     0.758          0.674   
## 253 GMT        3       0.847           1.91      0.894          0.0422  
## 254 MIR        3       0.672           1.45      0.598          0.000902
## 255 SFP        3       0.827           5.61      0.706          0.521   
## 256 BICO       3       1.39            0.941     0.385          0.376   
## 257 EURS       3       0.0758          0.0985    0.624          0.693   
## 258 SXP        3       0.760           0.388     0.686          0.873   
## 259 CELR       3       1.67            1.28      0.307          0.169   
## 260 NMR        3       0.853           1.24      0.328          0.508   
## 261 INJ        2      10.9            17.4       0.0259         0.769   
## 262 GMTT       3       2.53            0.846     0.277          0.474   
## 263 CRPT       3       9.78            1.24      0.827          0.221   
## 264 ZKS        4       7.29          149.        0.267          0.166   
## 265 BRD        3      15.0            10.5       0.614          0.0270  
## 266 LEVL       3       8.09            2.89      0.117          0.470   
## 267 ICP        3       1.00            1.26      0.714          0.0942  
## 268 SPI        4       3.73            3.42      0.151          0.00890 
## 269 BTRST      4     NaN               8.21     NA              0.00152 
## 270 GAS        4       1.51            1.22      0.237          0.202   
## 271 INJ        3       7.19           17.4       0.00959        0.769   
## 272 ALCX       4       4.31            1.26      0.696          0.174   
## 273 G999       4       2.38          NaN         0.852         NA       
## 274 KMD        4       1.73            1.00      0.0225         0.149   
## 275 OAX        4       2.89            5.49      0.194          0.00555 
## 276 WAXP       4       1.95            0.354     0.335          0.776   
## 277 VIB        4      30.1            19.3       0.0883         0.429   
## 278 DCR        4       0.702           1.62      0.852          0.0421  
## 279 LSK        4       2.25            0.391     0.964          0.802   
## 280 DASH       4       1.32            0.628     0.625          0.741   
## 281 VGX        4       3.35            8.75      0.0418         0.546   
## 282 ANKR       4       1.01            4.42      0.533          0.559   
## 283 XMR        4       0.438           0.877     0.709          0.120   
## 284 KLAY       4       0.682           0.425     0.820          0.732   
## 285 BTC        4       0.741           0.481     0.675          0.496   
## 286 ETH        4       0.741           0.725     0.789          0.519   
## 287 LTC        4       0.882           0.591     0.793          0.709   
## 288 BSV        4       1.05            0.567     0.784          0.418   
## 289 ADA        4       0.644           0.884     0.680          0.499   
## 290 ZEC        4       0.903           0.726     0.799          0.356   
## 291 HT         4       0.276           0.855     0.826          0.195   
## 292 TRX        4       0.499           0.327     0.636          0.643   
## 293 KNC        4       2.66            0.529     0.673          0.732   
## 294 ZRX        4       4.34            0.355     0.331          0.901   
## 295 BAT        4       1.02            0.570     0.686          0.722   
## 296 BNT        4       1.27            0.677     0.0788         0.569   
## 297 MANA       4       0.991           0.586     0.567          0.571   
## 298 DGB        4       1.07            0.908     0.593          0.413   
## 299 ENJ        4       1.19            0.625     0.418          0.596   
## 300 BTG        4       2.89            0.447     0.297          0.811   
## 301 ARDR       4       2.70            1.31      0.00109        0.204   
## 302 NEXO       4       1.04            1.48      0.363          0.0721  
## 303 CHZ        4       2.23            1.29      0.446          0.868   
## 304 LEO        4       1.07            0.606     0.00595        0.714   
## 305 AVA        4       6.62            0.934     0.796          0.397   
## 306 CROOLD     4       1.43            0.550     0.000863       0.473   
## 307 ARNM       4       3.55            7.91      0.0402         0.757   
## 308 DODO       4       2.21            1.08      0.609          0.0535  
## 309 DYDX       4       2.06            1.79      0.497          0.0262  
## 310 1INCH      4       2.05            0.822     0.708          0.642   
## 311 BAKE       4       1.78            0.971     0.380          0.423   
## 312 SKL        4       1.58            1.09      0.533          0.531   
## 313 DOT        4       2.61            0.841     0.712          0.605   
## 314 LINA       4       1.99            0.926     0.353          0.514   
## 315 QTUM       4       3.50            0.484     0.0383         0.767   
## 316 TOMO       4       0.857           0.952     0.580          0.451   
## 317 IOTA       4       1.70            0.480     0.496          0.746   
## 318 TORN       4       1.04            0.985     0.870          0.910   
## 319 IOST       4       1.21            0.450     0.411          0.702   
## 320 NEAR       4       0.670           0.938     0.819          0.495   
## 321 PERP       4       1.24            1.58      0.703          0.326   
## 322 RIF        4       1.64            0.618     0.304          0.676   
## 323 ATOM       4       1.42            0.699     0.442          0.539   
## 324 ALICE      4       3.82            0.609     0.418          0.774   
## 325 MTLX       4       1.92            3.88      0.648          0.0911  
## 326 XDC        4       0.573           0.302     0.710          0.760   
## 327 MC         4       9.95            8.70      0.0424         0.0403  
## 328 BDX        4       0.314           0.246     0.928          0.878   
## 329 SNX        4       2.39            1.10      0.207          0.504   
## 330 TRIBE      4       0.617           1.04      0.574          0.129   
## 331 THETA      4       1.18            0.577     0.620          0.824   
## 332 RUNE       4       0.825           1.29      0.765          0.243   
## 333 XEM        4       0.876           0.326     0.494          0.856   
## 334 ATA        4       2.87            1.18      0.533          0.705   
## 335 UMA        4       1.12            2.62      0.369          0.224   
## 336 BAL        4       2.13            1.35      0.431          0.175   
## 337 XCH        4       0.742           0.609     0.543          0.200   
## 338 REN        4       0.438           0.966     0.935          0.487   
## 339 SAND       4       0.553           0.864     0.847          0.422   
## 340 FTT        4       1.40            0.734     0.761          0.157   
## 341 BCH        4       1.41            0.610     0.418          0.674   
## 342 MIR        4       3.41            1.45      0.000678       0.000902
## 343 GMT        4       0.808           1.91      0.838          0.0422  
## 344 SXP        4       1.64            0.388     0.303          0.873   
## 345 SFP        4       2.61            5.61      0.987          0.521   
## 346 BICO       4       2.58            0.941     0.472          0.376   
## 347 EURS       4       0.0919          0.0985    0.0366         0.693   
## 348 EOS        4       1.29            0.547     0.575          0.811   
## 349 GMTT       4       0.377           0.846     0.897          0.474   
## 350 CELR       4       3.08            1.28      0.297          0.169   
## 351 NMR        4       6.97            1.24      0.162          0.508   
## 352 CRPT       4       8.38            1.24      0.0111         0.221   
## 353 BRD        4       2.67           10.5       0.0480         0.0270  
## 354 LEVL       4       7.77            2.89      0.402          0.470   
## 355 ICP        4       4.42            1.26      0.401          0.0942  
## 356 ZKS        5      NA             149.       NA              0.166   
## 357 INJ        4       1.43           17.4       0.202          0.769   
## 358 GAS        5      NA               1.22     NA              0.202   
## 359 G999       5      NA             NaN        NA             NA       
## 360 ALCX       5      NA               1.26     NA              0.174   
## 361 KMD        5      NA               1.00     NA              0.149   
## 362 CRPT       5      NA               1.24     NA              0.221   
## 363 DCR        5      NA               1.62     NA              0.0421  
## 364 LSK        5      NA               0.391    NA              0.802   
## 365 GMTT       5      NA               0.846    NA              0.474   
## 366 VGX        5      NA               8.75     NA              0.546   
## 367 OAX        5      NA               5.49     NA              0.00555 
## 368 DASH       5      NA               0.628    NA              0.741   
## 369 VIB        5      NA              19.3      NA              0.429   
## 370 ANKR       5      NA               4.42     NA              0.559   
## 371 XMR        5      NA               0.877    NA              0.120   
## 372 KLAY       5      NA               0.425    NA              0.732   
## 373 BTC        5      NA               0.481    NA              0.496   
## 374 ETH        5      NA               0.725    NA              0.519   
## 375 LTC        5      NA               0.591    NA              0.709   
## 376 ADA        5      NA               0.884    NA              0.499   
## 377 BSV        5      NA               0.567    NA              0.418   
## 378 ZEC        5      NA               0.726    NA              0.356   
## 379 HT         5      NA               0.855    NA              0.195   
## 380 TRX        5      NA               0.327    NA              0.643   
## 381 KNC        5      NA               0.529    NA              0.732   
## 382 ZRX        5      NA               0.355    NA              0.901   
## 383 BAT        5      NA               0.570    NA              0.722   
## 384 BNT        5      NA               0.677    NA              0.569   
## 385 MANA       5      NA               0.586    NA              0.571   
## 386 DGB        5      NA               0.908    NA              0.413   
## 387 ENJ        5      NA               0.625    NA              0.596   
## 388 BTG        5      NA               0.447    NA              0.811   
## 389 ARDR       5      NA               1.31     NA              0.204   
## 390 NEXO       5      NA               1.48     NA              0.0721  
## 391 CHZ        5      NA               1.29     NA              0.868   
## 392 LEO        5      NA               0.606    NA              0.714   
## 393 AVA        5      NA               0.934    NA              0.397   
## 394 CROOLD     5      NA               0.550    NA              0.473   
## 395 ARNM       5      NA               7.91     NA              0.757   
## 396 DODO       5      NA               1.08     NA              0.0535  
## 397 DYDX       5      NA               1.79     NA              0.0262  
## 398 1INCH      5      NA               0.822    NA              0.642   
## 399 BAKE       5      NA               0.971    NA              0.423   
## 400 SKL        5      NA               1.09     NA              0.531   
## 401 LINA       5      NA               0.926    NA              0.514   
## 402 QTUM       5      NA               0.484    NA              0.767   
## 403 TOMO       5      NA               0.952    NA              0.451   
## 404 IOTA       5      NA               0.480    NA              0.746   
## 405 TORN       5      NA               0.985    NA              0.910   
## 406 IOST       5      NA               0.450    NA              0.702   
## 407 NEAR       5      NA               0.938    NA              0.495   
## 408 PERP       5      NA               1.58     NA              0.326   
## 409 RIF        5      NA               0.618    NA              0.676   
## 410 UMA        5      NA               2.62     NA              0.224   
## 411 ATOM       5      NA               0.699    NA              0.539   
## 412 ALICE      5      NA               0.609    NA              0.774   
## 413 MTLX       5      NA               3.88     NA              0.0911  
## 414 XDC        5      NA               0.302    NA              0.760   
## 415 MC         5      NA               8.70     NA              0.0403  
## 416 BDX        5      NA               0.246    NA              0.878   
## 417 TRIBE      5      NA               1.04     NA              0.129   
## 418 SNX        5      NA               1.10     NA              0.504   
## 419 THETA      5      NA               0.577    NA              0.824   
## 420 SXP        5      NA               0.388    NA              0.873   
## 421 XEM        5      NA               0.326    NA              0.856   
## 422 DOT        5      NA               0.841    NA              0.605   
## 423 ATA        5      NA               1.18     NA              0.705   
## 424 RUNE       5      NA               1.29     NA              0.243   
## 425 XCH        5      NA               0.609    NA              0.200   
## 426 BAL        5      NA               1.35     NA              0.175   
## 427 REN        5      NA               0.966    NA              0.487   
## 428 SAND       5      NA               0.864    NA              0.422   
## 429 BCH        5      NA               0.610    NA              0.674   
## 430 FTT        5      NA               0.734    NA              0.157   
## 431 MIR        5      NA               1.45     NA              0.000902
## 432 GMT        5      NA               1.91     NA              0.0422  
## 433 SFP        5      NA               5.61     NA              0.521   
## 434 BICO       5      NA               0.941    NA              0.376   
## 435 EURS       5      NA               0.0985   NA              0.693   
## 436 CELR       5      NA               1.28     NA              0.169   
## 437 EOS        5      NA               0.547    NA              0.811   
## 438 WAXP       5      NA               0.354    NA              0.776   
## 439 NMR        5      NA               1.24     NA              0.508   
## 440 BRD        5      NA              10.5      NA              0.0270  
## 441 LEVL       5      NA               2.89     NA              0.470   
## 442 INJ        5      NA              17.4      NA              0.769   
## 443 ICP        5      NA               1.26     NA              0.0942  
## 444 BTRST      5      NA               8.21     NA              0.00152 
## 445 SPI        5      NA               3.42     NA              0.00890

Out of 445 groups, 232 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: 445 x 6
## # Groups:   symbol, split [445]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        0.936       0.761           0.481          0.496
##  2 ETH        1        1.47        0.564           0.725          0.519
##  3 EOS        1        0.798       0.715           0.547          0.811
##  4 LTC        1        1.19        0.767           0.591          0.709
##  5 BSV        1        2.22        0.104           0.567          0.418
##  6 ADA        1        1.10        0.503           0.884          0.499
##  7 ZEC        1        1.23        0.625           0.726          0.356
##  8 TRX        1        1.09        0.382           0.327          0.643
##  9 HT         1        1.41        0.447           0.855          0.195
## 10 XMR        1        0.692       0.773           0.877          0.120
## # ... with 435 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: 445 x 3
## # Groups:   symbol [89]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.632
##  2 ETH    lm    1.06 
##  3 EOS    lm    0.792
##  4 LTC    lm    0.984
##  5 BSV    lm    1.08 
##  6 ADA    lm    0.746
##  7 ZEC    lm    0.857
##  8 TRX    lm    0.602
##  9 HT     lm    0.753
## 10 XMR    lm    0.602
## # ... with 435 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: 445 x 3
## # Groups:   symbol [89]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.481
##  2 ETH    lm    0.725
##  3 EOS    lm    0.547
##  4 LTC    lm    0.591
##  5 BSV    lm    0.567
##  6 ADA    lm    0.884
##  7 ZEC    lm    0.726
##  8 TRX    lm    0.327
##  9 HT     lm    0.855
## 10 XMR    lm    0.877
## # ... with 435 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: 445 x 3
## # Groups:   symbol [89]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.803
##  2 ETH    lm    0.768
##  3 EOS    lm    0.713
##  4 LTC    lm    0.587
##  5 BSV    lm    0.638
##  6 ADA    lm    0.670
##  7 ZEC    lm    0.737
##  8 TRX    lm    0.646
##  9 HT     lm    0.613
## 10 XMR    lm    0.677
## # ... with 435 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: 445 x 3
## # Groups:   symbol [89]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.496
##  2 ETH    lm    0.519
##  3 EOS    lm    0.811
##  4 LTC    lm    0.709
##  5 BSV    lm    0.418
##  6 ADA    lm    0.499
##  7 ZEC    lm    0.356
##  8 TRX    lm    0.643
##  9 HT     lm    0.195
## 10 XMR    lm    0.120
## # ... with 435 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.