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 
## 5683.0019629    0.1717414 4718.9179477

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.01509599         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] 1.770848

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: 375 x 4
## # Groups:   symbol, split [375]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1   0.698              0.779
##  2 ETH        1   0.937              0.900
##  3 EOS        1   0.896              0.897
##  4 LTC        1   0.932              0.887
##  5 ADA        1   0.0000672          0.278
##  6 BSV        1   0.930              0.540
##  7 ZEC        1   0.224              0.885
##  8 TRX        1   0.611              0.890
##  9 HT         1   0.329              0.971
## 10 XMR        1   0.0977             0.954
## # ... with 365 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: 375 x 6
## # Groups:   symbol, split [375]
##     symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>       <dbl>          <dbl>
##   1 BTC        1       1.77            0.278   0.698            0.779   
##   2 ETH        1       0.574           0.326   0.937            0.900   
##   3 EOS        1       2.51            0.290   0.896            0.897   
##   4 LTC        1       2.31            0.290   0.932            0.887   
##   5 ADA        1       2.08            0.567   0.0000672        0.278   
##   6 BSV        1       2.37            0.458   0.930            0.540   
##   7 ZEC        1       3.54            0.421   0.224            0.885   
##   8 TRX        1       1.41            0.194   0.611            0.890   
##   9 HT         1       1.77            0.240   0.329            0.971   
##  10 XMR        1       2.42            0.192   0.0977           0.954   
##  11 KNC        1       2.61            0.919   0.807            0.865   
##  12 ZRX        1       4.70            0.416   0.204            0.811   
##  13 BAT        1       1.84            0.430   0.874            0.687   
##  14 BNT        1       1.76            0.591   0.844            0.379   
##  15 MANA       1       3.14            0.726   0.0110           0.814   
##  16 CROOLD     1       3.39            0.682   0.144            0.599   
##  17 DGB        1       9.39            0.337   0.00206          0.848   
##  18 ENJ        1       2.41            0.404   0.724            0.906   
##  19 XEM        1       3.12            0.382   0.892            0.755   
##  20 BTG        1       3.63            0.313   0.0216           0.909   
##  21 ARDR       1       2.47            1.12    0.848            0.275   
##  22 KMD        1       5.11            0.503   0.438            0.702   
##  23 ELF        1       5.01            3.31    0.0560           0.992   
##  24 NEXO       1       0.903           0.643   0.966            0.928   
##  25 CHZ        1       4.25            0.344   0.0209           0.866   
##  26 CKB        1       7.83            0.539   0.203            0.990   
##  27 BRD        1       3.10           10.9     0.594            0.0525  
##  28 DCR        1       4.28            1.75    0.756            0.0475  
##  29 VIB        1      12.2            31.0     0.493            0.00204 
##  30 WAXP       1       1.62            0.590   0.834            0.585   
##  31 RCN        1       2.09            5.07    0.961            0.992   
##  32 LEO        1       0.400           1.54    0.280            0.00208 
##  33 COCOS      1       2.55            2.87    0.304            0.769   
##  34 AVA        1       1.77            0.260   0.654            0.891   
##  35 CND        1      39.5             8.17    0.0125           0.315   
##  36 NAV        1       1.66            3.09    0.684            0.394   
##  37 JST        1       8.67            0.950   0.119            0.909   
##  38 BAL        1       1.31            0.586   0.365            0.755   
##  39 MAID       1       4.80            0.636   0.402            0.786   
##  40 SXP        1       1.09            0.834   0.0356           0.587   
##  41 INJ        1       1.42            0.436   0.274            0.767   
##  42 CELR       1      13.1             1.35    0.0471           0.00550 
##  43 CRPT       1       3.58           84.1     0.379            0.720   
##  44 ICP        1       4.83            0.873   0.758            0.102   
##  45 XCH        1       8.08            0.575   0.000771         0.474   
##  46 ZYN        1       1.08            2.96    0.863            0.00941 
##  47 TXA        1      73.4            15.7     0.0347           1.00    
##  48 1INCH      1       2.22            0.603   0.887            0.632   
##  49 NCT        1       1.27            2.24    0.752            0.174   
##  50 ETC        1       0.803           0.555   0.571            0.788   
##  51 BTRST      1       9.19            2.25    0.0950           0.746   
##  52 NEAR       1       1.48            0.399   0.852            0.870   
##  53 DASH       1       0.540           0.577   0.518            0.803   
##  54 DODO       1       1.48            0.984   0.00417          0.179   
##  55 KRRX       1       0.928           0.467   0.0251           0.0329  
##  56 DODO       2       1.79            0.984   0.0572           0.179   
##  57 CRPT       2       2.96           84.1     0.222            0.720   
##  58 ETC        2       0.930           0.555   0.284            0.788   
##  59 SXP        2       1.18            0.834   0.214            0.587   
##  60 ZYN        2       3.05            2.96    0.0762           0.00941 
##  61 BAL        2       1.87            0.586   0.0569           0.755   
##  62 INJ        2       1.69            0.436   0.199            0.767   
##  63 BTRST      2      18.9             2.25    0.00521          0.746   
##  64 ELF        2       3.61            3.31    0.188            0.992   
##  65 DASH       2       1.01            0.577   0.308            0.803   
##  66 CND        2     NaN               8.17   NA                0.315   
##  67 CKB        2       1.93            0.539   0.115            0.990   
##  68 NAV        2       0.106           3.09    0.350            0.394   
##  69 ZRX        2       0.626           0.416   0.618            0.811   
##  70 BRD        2       4.86           10.9     0.371            0.0525  
##  71 LEO        2       7.77            1.54    0.132            0.00208 
##  72 BTC        2       0.446           0.278   0.641            0.779   
##  73 ETH        2       0.486           0.326   0.696            0.900   
##  74 EOS        2       1.13            0.290   0.00000636       0.897   
##  75 LTC        2       0.534           0.290   0.840            0.887   
##  76 ADA        2       0.689           0.567   0.321            0.278   
##  77 BSV        2       0.551           0.458   0.619            0.540   
##  78 TRX        2       0.693           0.194   0.831            0.890   
##  79 ZEC        2       0.932           0.421   0.313            0.885   
##  80 HT         2       0.507           0.240   0.604            0.971   
##  81 XMR        2       0.610           0.192   0.774            0.954   
##  82 BAT        2       1.23            0.430   0.373            0.687   
##  83 BNT        2       0.437           0.591   0.728            0.379   
##  84 MANA       2       0.929           0.726   0.622            0.814   
##  85 CROOLD     2       0.690           0.682   0.589            0.599   
##  86 ENJ        2       0.683           0.404   0.592            0.906   
##  87 XEM        2       0.479           0.382   0.696            0.755   
##  88 BTG        2       0.515           0.313   0.443            0.909   
##  89 KMD        2       3.69            0.503   0.000470         0.702   
##  90 ARDR       2       1.25            1.12    0.0693           0.275   
##  91 NEXO       2       2.12            0.643   0.000104         0.928   
##  92 CHZ        2       0.810           0.344   0.574            0.866   
##  93 RCN        2       0.883           5.07    0.849            0.992   
##  94 AVA        2       1.72            0.260   0.000330         0.891   
##  95 JST        2       6.55            0.950   0.421            0.909   
##  96 1INCH      2       1.23            0.603   0.196            0.632   
##  97 WAXP       2       1.60            0.590   0.113            0.585   
##  98 CELR       2       1.43            1.35    0.727            0.00550 
##  99 ICP        2       0.623           0.873   0.935            0.102   
## 100 XCH        2       0.707           0.575   0.684            0.474   
## 101 TXA        2      14.1            15.7     0.0100           1.00    
## 102 NCT        2       1.25            2.24    0.956            0.174   
## 103 NEAR       2       1.03            0.399   0.495            0.870   
## 104 COCOS      2       1.98            2.87    0.529            0.769   
## 105 KNC        2       1.08            0.919   0.0370           0.865   
## 106 DCR        2       3.88            1.75    0.164            0.0475  
## 107 MC         1       6.01            0.695   0.0938           0.745   
## 108 BICO       1       3.40            1.52    0.0230           0.253   
## 109 VIB        2       2.26           31.0     0.416            0.00204 
## 110 DGB        2       0.832           0.337   0.203            0.848   
## 111 MAID       2       0.773           0.636   0.653            0.786   
## 112 RGT        1       2.42            0.758   0.182            0.323   
## 113 LAZIO      1       1.93            0.841   0.0744           0.308   
## 114 KRRX       2       8.51            0.467   0.000199         0.0329  
## 115 CRPT       3       3.64           84.1     0.0268           0.720   
## 116 ZYN        3       0.492           2.96    0.729            0.00941 
## 117 SXP        3       1.50            0.834   0.656            0.587   
## 118 INJ        3       1.21            0.436   0.855            0.767   
## 119 BTRST      3       0.937           2.25    0.108            0.746   
## 120 BAL        3       1.59            0.586   0.657            0.755   
## 121 MC         2       2.73            0.695   0.0734           0.745   
## 122 BICO       2       2.08            1.52    0.673            0.253   
## 123 DASH       3       0.952           0.577   0.849            0.803   
## 124 ETC        3       2.14            0.555   0.693            0.788   
## 125 RGT        2       1.38            0.758   0.0395           0.323   
## 126 BRD        3       4.99           10.9     0.351            0.0525  
## 127 ELF        3       0.670           3.31    0.744            0.992   
## 128 NAV        3       8.15            3.09    0.00230          0.394   
## 129 BTG        3       0.651           0.313   0.349            0.909   
## 130 1INCH      3       0.485           0.603   0.801            0.632   
## 131 BTC        3       0.540           0.278   0.262            0.779   
## 132 ETH        3       0.865           0.326   0.0482           0.900   
## 133 EOS        3       0.745           0.290   0.316            0.897   
## 134 LTC        3       0.554           0.290   0.213            0.887   
## 135 BSV        3       0.536           0.458   0.0458           0.540   
## 136 ADA        3       0.318           0.567   0.819            0.278   
## 137 ZEC        3       0.731           0.421   0.213            0.885   
## 138 HT         3       0.531           0.240   0.00219          0.971   
## 139 TRX        3       0.571           0.194   0.0824           0.890   
## 140 XMR        3       0.686           0.192   0.420            0.954   
## 141 BAT        3       0.548           0.430   0.834            0.687   
## 142 BNT        3       0.386           0.591   0.683            0.379   
## 143 CROOLD     3       0.366           0.682   0.760            0.599   
## 144 MANA       3       0.636           0.726   0.347            0.814   
## 145 ENJ        3       0.630           0.404   0.720            0.906   
## 146 XEM        3       0.575           0.382   0.525            0.755   
## 147 KMD        3       0.572           0.503   0.441            0.702   
## 148 ARDR       3       1.93            1.12    0.869            0.275   
## 149 NEXO       3       0.640           0.643   0.889            0.928   
## 150 CHZ        3       0.412           0.344   0.882            0.866   
## 151 RCN        3       1.63            5.07    0.988            0.992   
## 152 AVA        3       0.541           0.260   0.246            0.891   
## 153 JST        3       4.50            0.950   0.703            0.909   
## 154 WAXP       3       0.967           0.590   0.460            0.585   
## 155 CELR       3       0.717           1.35    0.553            0.00550 
## 156 ICP        3       0.628           0.873   0.917            0.102   
## 157 XCH        3       0.547           0.575   0.221            0.474   
## 158 TXA        3     134.             15.7     0.0000141        1.00    
## 159 NCT        3       1.56            2.24    0.405            0.174   
## 160 COCOS      3       2.03            2.87    0.0211           0.769   
## 161 NEAR       3       0.506           0.399   0.773            0.870   
## 162 LEO        3       0.0174          1.54    0.235            0.00208 
## 163 ZRX        3       0.809           0.416   0.855            0.811   
## 164 KNC        3       0.632           0.919   0.811            0.865   
## 165 CKB        3       1.53            0.539   0.00977          0.990   
## 166 LAZIO      2       0.630           0.841   0.729            0.308   
## 167 DCR        3       0.395           1.75    0.716            0.0475  
## 168 VIB        3       4.69           31.0     0.0194           0.00204 
## 169 CND        3      13.0             8.17    0.119            0.315   
## 170 C20        1       4.13            8.26    0.609            0.137   
## 171 MTLX       1       0.712           1.11    0.0663           0.0203  
## 172 FIDA       1       5.71            3.13    0.771            0.437   
## 173 TOMO       1      15.4             0.0742  0.00605          0.000126
## 174 SPI        1       2.27            2.15    0.168            0.138   
## 175 DYDX       1       2.67            1.80    0.0982           0.120   
## 176 GODS       1       2.71            0.578   0.0848           0.669   
## 177 ABBC       1       1.00            2.02    0.108            0.881   
## 178 SNX        1       4.07            1.48    0.539            0.124   
## 179 ALPACA     1      34.4             0.751   0.000145         0.00108 
## 180 IOST       1       3.67            0.623   0.544            0.165   
## 181 IOTA       1       4.71            1.18    0.206            0.0186  
## 182 VGX        1      12.7             2.03    0.0856           0.361   
## 183 RIF        1       5.29            1.81    0.0261           0.0524  
## 184 ATOM       1       3.31            0.793   0.0891           0.474   
## 185 ALCX       1       3.30           12.3     0.0708           0.0855  
## 186 DGB        3       0.735           0.337   0.485            0.848   
## 187 MAID       3       0.850           0.636   0.293            0.786   
## 188 KRRX       3       1.65            0.467   0.612            0.0329  
## 189 DODO       3       9.18            0.984   0.000392         0.179   
## 190 IOTA       2       5.14            1.18    0.220            0.0186  
## 191 MTLX       2       1.07            1.11    0.234            0.0203  
## 192 FIDA       2      12.4             3.13    0.827            0.437   
## 193 TOMO       2       2.07            0.0742  0.686            0.000126
## 194 DYDX       2       5.40            1.80    0.229            0.120   
## 195 GODS       2       3.69            0.578   0.00499          0.669   
## 196 ABBC       2       4.79            2.02    0.669            0.881   
## 197 ALPACA     2       4.31            0.751   0.542            0.00108 
## 198 ATOM       2       2.47            0.793   0.265            0.474   
## 199 IOST       2       2.49            0.623   0.00142          0.165   
## 200 VGX        2       1.73            2.03    0.107            0.361   
## 201 RIF        2       3.23            1.81    0.0968           0.0524  
## 202 ALCX       2       3.59           12.3     0.668            0.0855  
## 203 SNX        2       3.25            1.48    0.471            0.124   
## 204 C20        2       2.94            8.26    0.0215           0.137   
## 205 SPI        2       2.00            2.15    0.539            0.138   
## 206 ETC        4       0.426           0.555   0.299            0.788   
## 207 LAZIO      3       1.97            0.841   0.0805           0.308   
## 208 ELF        4       0.858           3.31    0.155            0.992   
## 209 BRD        4       0.777          10.9     0.989            0.0525  
## 210 NAV        4       4.58            3.09    0.000632         0.394   
## 211 1INCH      4       0.678           0.603   0.754            0.632   
## 212 BTC        4       0.265           0.278   0.722            0.779   
## 213 ETH        4       0.249           0.326   0.900            0.900   
## 214 EOS        4       0.484           0.290   0.603            0.897   
## 215 LTC        4       0.882           0.290   0.308            0.887   
## 216 ADA        4       0.954           0.567   0.866            0.278   
## 217 BSV        4       0.410           0.458   0.616            0.540   
## 218 TRX        4       0.493           0.194   0.445            0.890   
## 219 ZEC        4       1.05            0.421   0.0967           0.885   
## 220 HT         4       0.500           0.240   0.512            0.971   
## 221 BNT        4       0.504           0.591   0.868            0.379   
## 222 MANA       4       1.05            0.726   0.487            0.814   
## 223 CROOLD     4       0.470           0.682   0.392            0.599   
## 224 ENJ        4       0.852           0.404   0.647            0.906   
## 225 XEM        4       0.544           0.382   0.635            0.755   
## 226 BTG        4       0.636           0.313   0.645            0.909   
## 227 ARDR       4       0.695           1.12    0.503            0.275   
## 228 KMD        4       0.468           0.503   0.575            0.702   
## 229 NEXO       4       0.761           0.643   0.328            0.928   
## 230 CHZ        4       0.724           0.344   0.287            0.866   
## 231 RCN        4       4.42            5.07    0.968            0.992   
## 232 AVA        4       0.410           0.260   0.731            0.891   
## 233 JST        4       5.31            0.950   0.0212           0.909   
## 234 XMR        4       0.663           0.192   0.763            0.954   
## 235 WAXP       4       0.669           0.590   0.694            0.585   
## 236 COCOS      4       0.786           2.87    0.985            0.769   
## 237 XCH        4       0.871           0.575   0.690            0.474   
## 238 TXA        4       2.69           15.7     0.0552           1.00    
## 239 NCT        4      33.4             2.24    0.226            0.174   
## 240 CELR       4       1.90            1.35    0.507            0.00550 
## 241 ICP        4       1.71            0.873   0.260            0.102   
## 242 BAT        4       0.595           0.430   0.880            0.687   
## 243 NEAR       4       1.25            0.399   0.812            0.870   
## 244 LEO        4       2.13            1.54    0.000710         0.00208 
## 245 KNC        4       2.36            0.919   0.0861           0.865   
## 246 ZRX        4       0.597           0.416   0.848            0.811   
## 247 MC         3       1.38            0.695   0.574            0.745   
## 248 DCR        4      18.8             1.75    0.000148         0.0475  
## 249 BICO       3       1.22            1.52    0.148            0.253   
## 250 CKB        4       3.57            0.539   0.608            0.990   
## 251 VIB        4       0.739          31.0     0.217            0.00204 
## 252 MAID       4       0.557           0.636   0.421            0.786   
## 253 DGB        4       0.761           0.337   0.690            0.848   
## 254 CND        4       5.92            8.17    0.938            0.315   
## 255 RGT        3      33.2             0.758   0.00464          0.323   
## 256 CRPT       4      21.4            84.1     0.0518           0.720   
## 257 DASH       4       1.25            0.577   0.00497          0.803   
## 258 ZYN        4      16.9             2.96    0.187            0.00941 
## 259 SXP        4       0.762           0.834   0.582            0.587   
## 260 BTRST      4       1.40            2.25    0.455            0.746   
## 261 INJ        4       1.56            0.436   0.0464           0.767   
## 262 BAL        4       0.432           0.586   0.776            0.755   
## 263 IOTA       3       1.24            1.18    0.142            0.0186  
## 264 ABBC       3       3.88            2.02    0.0320           0.881   
## 265 DYDX       3       0.924           1.80    0.522            0.120   
## 266 FIDA       3       1.58            3.13    0.0187           0.437   
## 267 TOMO       3       1.83            0.0742  0.00132          0.000126
## 268 IOST       3       0.334           0.623   0.488            0.165   
## 269 RIF        3       1.62            1.81    0.0665           0.0524  
## 270 MTLX       3       0.492           1.11    0.840            0.0203  
## 271 ALPACA     3       0.727           0.751   0.00781          0.00108 
## 272 ALCX       3       1.04           12.3     0.162            0.0855  
## 273 VGX        3       1.38            2.03    0.00811          0.361   
## 274 ATOM       3       1.42            0.793   0.511            0.474   
## 275 SNX        3       0.442           1.48    0.532            0.124   
## 276 C20        3       0.692           8.26    0.0542           0.137   
## 277 GODS       3       1.85            0.578   0.728            0.669   
## 278 SPI        3      10.9             2.15    0.952            0.138   
## 279 KRRX       4       1.61            0.467   0.110            0.0329  
## 280 DODO       4       2.43            0.984   0.0682           0.179   
## 281 LAZIO      4       1.65            0.841   0.864            0.308   
## 282 MC         4      12.0             0.695   0.846            0.745   
## 283 BICO       4     129.              1.52    0.443            0.253   
## 284 ETC        5      NA               0.555  NA                0.788   
## 285 RGT        4       3.58            0.758   0.666            0.323   
## 286 ABBC       4       1.75            2.02    0.787            0.881   
## 287 DYDX       4       6.93            1.80    0.187            0.120   
## 288 FIDA       4      10.6             3.13    0.00435          0.437   
## 289 TOMO       4       3.43            0.0742  0.0635           0.000126
## 290 IOST       4       7.42            0.623   0.0109           0.165   
## 291 RIF        4      11.8             1.81    0.460            0.0524  
## 292 ALPACA     4       5.80            0.751   0.00966          0.00108 
## 293 ALCX       4       6.38           12.3     0.0154           0.0855  
## 294 VGX        4       4.14            2.03    0.302            0.361   
## 295 ATOM       4       3.69            0.793   0.0964           0.474   
## 296 MTLX       4       1.68            1.11    0.531            0.0203  
## 297 SNX        4       4.24            1.48    0.0780           0.124   
## 298 C20        4       5.30            8.26    0.0704           0.137   
## 299 SPI        4       3.85            2.15    0.373            0.138   
## 300 GODS       4       4.55            0.578   0.590            0.669   
## 301 IOTA       4       3.28            1.18    0.190            0.0186  
## 302 BRD        5      NA              10.9    NA                0.0525  
## 303 NAV        5      NA               3.09   NA                0.394   
## 304 DCR        5      NA               1.75   NA                0.0475  
## 305 CND        5      NA               8.17   NA                0.315   
## 306 1INCH      5      NA               0.603  NA                0.632   
## 307 BTC        5      NA               0.278  NA                0.779   
## 308 ETH        5      NA               0.326  NA                0.900   
## 309 EOS        5      NA               0.290  NA                0.897   
## 310 LTC        5      NA               0.290  NA                0.887   
## 311 BSV        5      NA               0.458  NA                0.540   
## 312 ADA        5      NA               0.567  NA                0.278   
## 313 ZEC        5      NA               0.421  NA                0.885   
## 314 HT         5      NA               0.240  NA                0.971   
## 315 TRX        5      NA               0.194  NA                0.890   
## 316 BNT        5      NA               0.591  NA                0.379   
## 317 CROOLD     5      NA               0.682  NA                0.599   
## 318 MANA       5      NA               0.726  NA                0.814   
## 319 ENJ        5      NA               0.404  NA                0.906   
## 320 XEM        5      NA               0.382  NA                0.755   
## 321 BTG        5      NA               0.313  NA                0.909   
## 322 ARDR       5      NA               1.12   NA                0.275   
## 323 NEXO       5      NA               0.643  NA                0.928   
## 324 CHZ        5      NA               0.344  NA                0.866   
## 325 RCN        5      NA               5.07   NA                0.992   
## 326 AVA        5      NA               0.260  NA                0.891   
## 327 JST        5      NA               0.950  NA                0.909   
## 328 KMD        5      NA               0.503  NA                0.702   
## 329 WAXP       5      NA               0.590  NA                0.585   
## 330 COCOS      5      NA               2.87   NA                0.769   
## 331 XCH        5      NA               0.575  NA                0.474   
## 332 TXA        5      NA              15.7    NA                1.00    
## 333 NCT        5      NA               2.24   NA                0.174   
## 334 CELR       5      NA               1.35   NA                0.00550 
## 335 ICP        5      NA               0.873  NA                0.102   
## 336 XMR        5      NA               0.192  NA                0.954   
## 337 BAT        5      NA               0.430  NA                0.687   
## 338 NEAR       5      NA               0.399  NA                0.870   
## 339 LEO        5      NA               1.54   NA                0.00208 
## 340 KNC        5      NA               0.919  NA                0.865   
## 341 ZRX        5      NA               0.416  NA                0.811   
## 342 CKB        5      NA               0.539  NA                0.990   
## 343 ELF        5      NA               3.31   NA                0.992   
## 344 VIB        5      NA              31.0    NA                0.00204 
## 345 MAID       5      NA               0.636  NA                0.786   
## 346 DGB        5      NA               0.337  NA                0.848   
## 347 CRPT       5      NA              84.1    NA                0.720   
## 348 DASH       5      NA               0.577  NA                0.803   
## 349 SXP        5      NA               0.834  NA                0.587   
## 350 BTRST      5      NA               2.25   NA                0.746   
## 351 INJ        5      NA               0.436  NA                0.767   
## 352 BAL        5      NA               0.586  NA                0.755   
## 353 ZYN        5      NA               2.96   NA                0.00941 
## 354 KRRX       5      NA               0.467  NA                0.0329  
## 355 DODO       5      NA               0.984  NA                0.179   
## 356 LAZIO      5      NA               0.841  NA                0.308   
## 357 MC         5      NA               0.695  NA                0.745   
## 358 BICO       5      NA               1.52   NA                0.253   
## 359 RGT        5      NA               0.758  NA                0.323   
## 360 ABBC       5      NA               2.02   NA                0.881   
## 361 DYDX       5      NA               1.80   NA                0.120   
## 362 FIDA       5      NA               3.13   NA                0.437   
## 363 TOMO       5      NA               0.0742 NA                0.000126
## 364 IOST       5      NA               0.623  NA                0.165   
## 365 RIF        5      NA               1.81   NA                0.0524  
## 366 ALPACA     5      NA               0.751  NA                0.00108 
## 367 ALCX       5      NA              12.3    NA                0.0855  
## 368 VGX        5      NA               2.03   NA                0.361   
## 369 MTLX       5      NA               1.11   NA                0.0203  
## 370 SNX        5      NA               1.48   NA                0.124   
## 371 SPI        5      NA               2.15   NA                0.138   
## 372 ATOM       5      NA               0.793  NA                0.474   
## 373 C20        5      NA               8.26   NA                0.137   
## 374 GODS       5      NA               0.578  NA                0.669   
## 375 IOTA       5      NA               1.18   NA                0.0186

Out of 375 groups, 214 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: 375 x 6
## # Groups:   symbol, split [375]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        1.77    0.698               0.278          0.779
##  2 ETH        1        0.574   0.937               0.326          0.900
##  3 EOS        1        2.51    0.896               0.290          0.897
##  4 LTC        1        2.31    0.932               0.290          0.887
##  5 ADA        1        2.08    0.0000672           0.567          0.278
##  6 BSV        1        2.37    0.930               0.458          0.540
##  7 ZEC        1        3.54    0.224               0.421          0.885
##  8 TRX        1        1.41    0.611               0.194          0.890
##  9 HT         1        1.77    0.329               0.240          0.971
## 10 XMR        1        2.42    0.0977              0.192          0.954
## # ... with 365 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: 375 x 3
## # Groups:   symbol [75]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.755
##  2 ETH    lm    0.544
##  3 EOS    lm    1.22 
##  4 LTC    lm    1.07 
##  5 ADA    lm    1.01 
##  6 BSV    lm    0.966
##  7 ZEC    lm    1.56 
##  8 TRX    lm    0.791
##  9 HT     lm    0.827
## 10 XMR    lm    1.10 
## # ... with 365 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: 375 x 3
## # Groups:   symbol [75]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.278
##  2 ETH    lm    0.326
##  3 EOS    lm    0.290
##  4 LTC    lm    0.290
##  5 ADA    lm    0.567
##  6 BSV    lm    0.458
##  7 ZEC    lm    0.421
##  8 TRX    lm    0.194
##  9 HT     lm    0.240
## 10 XMR    lm    0.192
## # ... with 365 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: 375 x 3
## # Groups:   symbol [75]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.581
##  2 ETH    lm    0.645
##  3 EOS    lm    0.454
##  4 LTC    lm    0.573
##  5 ADA    lm    0.502
##  6 BSV    lm    0.553
##  7 ZEC    lm    0.212
##  8 TRX    lm    0.492
##  9 HT     lm    0.362
## 10 XMR    lm    0.514
## # ... with 365 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: 375 x 3
## # Groups:   symbol [75]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.779
##  2 ETH    lm    0.900
##  3 EOS    lm    0.897
##  4 LTC    lm    0.887
##  5 ADA    lm    0.278
##  6 BSV    lm    0.540
##  7 ZEC    lm    0.885
##  8 TRX    lm    0.890
##  9 HT     lm    0.971
## 10 XMR    lm    0.954
## # ... with 365 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.