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 
## 1225.33676684    0.09136506 1016.78998531

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.4703826        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.057799

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: 395 x 4
## # Groups:   symbol, split [395]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1       0.709        0.983  
##  2 ETH        1       0.366        0.917  
##  3 EOS        1       0.489        0.841  
##  4 LTC        1       0.322        0.179  
##  5 BSV        1       0.722        0.436  
##  6 ADA        1       0.212        0.00110
##  7 ZEC        1       0.729        0.922  
##  8 HT         1       0.307        0.569  
##  9 TRX        1       0.396        0.873  
## 10 KNC        1       0.913        0.868  
## # ... with 385 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: 395 x 6
## # Groups:   symbol, split [395]
##     symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>       <dbl>          <dbl>
##   1 BTC        1       1.06             0.415   0.709            0.983  
##   2 ETH        1       0.932            0.556   0.366            0.917  
##   3 EOS        1       1.54             1.52    0.489            0.841  
##   4 LTC        1       1.09             2.33    0.322            0.179  
##   5 BSV        1       1.12             2.96    0.722            0.436  
##   6 ADA        1       1.26             3.21    0.212            0.00110
##   7 ZEC        1       0.633            1.04    0.729            0.922  
##   8 HT         1       1.32             2.19    0.307            0.569  
##   9 TRX        1       3.77             1.08    0.396            0.873  
##  10 KNC        1       0.938            1.70    0.913            0.868  
##  11 ZRX        1       3.13             1.30    0.181            0.976  
##  12 BAT        1       0.859            2.09    0.345            0.571  
##  13 BNT        1       1.26             1.28    0.517            0.501  
##  14 MANA       1       1.17             1.90    0.267            0.879  
##  15 CRO        1       0.905            0.759   0.752            0.926  
##  16 DGB        1       3.53             3.03    0.279            0.598  
##  17 ENJ        1       1.20             0.717   0.810            0.937  
##  18 BTM        1       2.88             3.39    0.426            0.140  
##  19 XEM        1       1.51             1.79    0.000190         0.422  
##  20 ARDR       1       1.84             1.34    0.559            0.928  
##  21 ELF        1       2.46             1.65    0.738            0.786  
##  22 KMD        1       4.03             1.95    0.195            0.694  
##  23 NEXO       1       1.24             2.24    0.577            0.156  
##  24 VSYS       1       5.12             8.71    0.00199          0.620  
##  25 CHZ        1       1.77             2.27    0.168            0.836  
##  26 BRD        1      11.9              3.74    0.345            0.00411
##  27 DCR        1       1.69             1.15    0.318            0.595  
##  28 IQ         1       6.96             6.94    0.134            0.596  
##  29 VIB        1       7.43             1.87    0.691            0.680  
##  30 WAXP       1       1.64             0.999   0.00573          0.899  
##  31 OAX        1       3.78             6.08    0.00880          0.444  
##  32 ETP        1       2.04             5.99    0.155            0.156  
##  33 AVA        1       1.07             1.28    0.479            0.881  
##  34 APPC       1       5.36             8.75    0.338            0.484  
##  35 NAV        1       8.02             0.769   0.0589           0.998  
##  36 JST        1       2.28             1.73    0.233            0.604  
##  37 SMART      1       1.81             1.64    0.645            0.429  
##  38 SRN        1       2.05             3.24    0.133            0.998  
##  39 XUC        1      17.7              2.62    0.602            0.0565 
##  40 BCD        1       3.02             4.80    0.0198           0.595  
##  41 NEO        1       1.06             1.19    0.694            0.896  
##  42 ICX        1       2.63             2.32    0.0720           0.604  
##  43 BCHA       1       1.46             2.25    0.702            0.0407 
##  44 CRV        1       1.78             3.95    0.717            0.0334 
##  45 CCE        1       5.89             9.24    0.702            0.161  
##  46 INJ        1       1.77             1.87    0.828            0.498  
##  47 SUN        1       0.814            1.54    0.839            0.475  
##  48 FUN        1       8.05             2.70    0.234            0.222  
##  49 MKR        1       1.01             4.53    0.549            0.00301
##  50 UNI        1       0.913            1.27    0.904            0.843  
##  51 LEVL       1      10.5              3.37    0.0149           0.746  
##  52 UMA        1       1.66             1.75    0.102            0.808  
##  53 SUSHI      1       1.80             1.02    0.743            0.936  
##  54 DOGE       1     147.               8.30    0.0193           0.506  
##  55 TON        1       0.505            0.911   0.862            0.362  
##  56 XVG        1       1.58             2.51    0.487            0.831  
##  57 IOST       1       1.91             1.91    0.0773           0.797  
##  58 AMB        1       3.11             8.23    0.00377          0.145  
##  59 ASP        1       3.38             1.31    0.270            0.868  
##  60 POA        1       9.97            12.2     0.484            0.419  
##  61 STX        1      13.0              3.82    0.0000218        0.0365 
##  62 ETN        1       1.77             2.18    0.438            0.850  
##  63 CVC        1       1.34             1.61    0.110            0.926  
##  64 FTM        1       9.04             2.44    0.362            0.327  
##  65 XMR        1       0.517            1.68    0.752            0.295  
##  66 BTG        1       1.93             1.70    0.268            0.620  
##  67 CKB        1      64.4              5.16    0.156            0.649  
##  68 GOLDR      1       2.75             1.46    0.256            0.579  
##  69 MBL        1      40.5              7.52    0.0170           0.00176
##  70 XTZ        1       0.711            2.85    0.335            0.0978 
##  71 XUC        2     NaN                2.62   NA                0.0565 
##  72 POA        2       2.21            12.2     0.343            0.419  
##  73 IOST       2       1.61             1.91    0.452            0.797  
##  74 AMB        2       3.09             8.23    0.0594           0.145  
##  75 BCD        2       1.59             4.80    0.0675           0.595  
##  76 MKR        2       4.37             4.53    0.0461           0.00301
##  77 UMA        2       4.45             1.75    0.156            0.808  
##  78 CVC        2       5.51             1.61    0.000933         0.926  
##  79 NEO        2       0.662            1.19    0.639            0.896  
##  80 XVG        2       1.27             2.51    0.527            0.831  
##  81 ICX        2       2.53             2.32    0.0182           0.604  
##  82 GOLDR      2      10.3              1.46    0.996            0.579  
##  83 STX        2       2.30             3.82    0.0949           0.0365 
##  84 ETN        2       0.958            2.18    0.00235          0.850  
##  85 FUN        2     NaN                2.70   NA                0.222  
##  86 VSYS       2       3.52             8.71    0.121            0.620  
##  87 APPC       2       0.200            8.75    0.914            0.484  
##  88 OAX        2       1.34             6.08    0.0442           0.444  
##  89 DOGE       2       1.40             8.30    0.507            0.506  
##  90 SRN        2       2.73             3.24    0.748            0.998  
##  91 CHZ        2       3.25             2.27    0.663            0.836  
##  92 MANA       2       1.08             1.90    0.825            0.879  
##  93 CKB        2       4.00             5.16    0.779            0.649  
##  94 DCR        2       1.90             1.15    0.579            0.595  
##  95 ETH        2       0.811            0.556   0.871            0.917  
##  96 KNC        2       0.930            1.70    0.843            0.868  
##  97 CRO        2       0.721            0.759   0.975            0.926  
##  98 BTG        2       5.51             1.70    0.000391         0.620  
##  99 ARDR       2       3.75             1.34    0.189            0.928  
## 100 BRD        2       2.04             3.74    0.0900           0.00411
## 101 EOS        2       0.729            1.52    0.888            0.841  
## 102 LTC        2       1.33             2.33    0.516            0.179  
## 103 BSV        2       1.87             2.96    0.0956           0.436  
## 104 ADA        2       0.668            3.21    0.943            0.00110
## 105 ZEC        2       1.62             1.04    0.537            0.922  
## 106 BNT        2       1.22             1.28    0.929            0.501  
## 107 BTM        2       1.74             3.39    0.0421           0.140  
## 108 XEM        2       0.763            1.79    0.964            0.422  
## 109 NEXO       2       1.63             2.24    0.878            0.156  
## 110 BTC        2       0.617            0.415   0.776            0.983  
## 111 ENJ        2       0.889            0.717   0.831            0.937  
## 112 WAXP       2       1.65             0.999   0.624            0.899  
## 113 AVA        2       1.19             1.28    0.691            0.881  
## 114 NAV        2       7.69             0.769   0.715            0.998  
## 115 INJ        2       1.03             1.87    0.887            0.498  
## 116 SUN        2       1.07             1.54    0.765            0.475  
## 117 UNI        2       1.71             1.27    0.977            0.843  
## 118 SUSHI      2       1.36             1.02    0.690            0.936  
## 119 FTM        2       2.01             2.44    0.936            0.327  
## 120 DGB        2       3.36             3.03    0.227            0.598  
## 121 KMD        2       2.06             1.95    0.355            0.694  
## 122 JST        2       1.48             1.73    0.899            0.604  
## 123 XMR        2       1.79             1.68    0.665            0.295  
## 124 ELF        2       1.66             1.65    0.766            0.786  
## 125 TON        2       1.39             0.911   0.864            0.362  
## 126 CCE        2       1.17             9.24    0.519            0.161  
## 127 ASP        2       1.63             1.31    0.245            0.868  
## 128 BAT        2       1.08             2.09    0.747            0.571  
## 129 CRV        2       0.997            3.95    0.848            0.0334 
## 130 TRX        2       0.757            1.08    0.891            0.873  
## 131 HT         2       1.74             2.19    0.926            0.569  
## 132 ZRX        2       0.611            1.30    0.851            0.976  
## 133 ETP        2       0.835            5.99    0.847            0.156  
## 134 VIB        2       2.62             1.87    0.0449           0.680  
## 135 SMART      2       1.82             1.64    0.335            0.429  
## 136 LEVL       2       3.68             3.37    0.0257           0.746  
## 137 XTZ        2       1.58             2.85    0.00112          0.0978 
## 138 MBL        2       2.84             7.52    0.378            0.00176
## 139 BCHA       2       1.69             2.25    0.600            0.0407 
## 140 XVG        3       7.59             2.51    0.269            0.831  
## 141 THETA      1       1.98             3.59    0.00694          0.219  
## 142 CBC        1    1964.               1.38    0.000482         0.971  
## 143 YFI        1       2.45             1.39    0.197            0.640  
## 144 SBD        1       0.0695           4.43    0.0368           0.136  
## 145 EDG        1       9.95             5.03    0.00178          0.171  
## 146 SPC        1       6.15             3.40    0.000524         0.00164
## 147 RIF        1       7.42             1.81    0.00518          0.904  
## 148 VET        1       2.17             5.66    0.0254           0.161  
## 149 DDR        1       1.54             3.16    0.803            0.564  
## 150 IQ         2       2.27             6.94    0.000493         0.596  
## 151 VSYS       3       0.771            8.71    0.578            0.620  
## 152 APPC       3       7.84             8.75    0.184            0.484  
## 153 OAX        3       1.99             6.08    0.872            0.444  
## 154 SRN        3       1.65             3.24    0.452            0.998  
## 155 VET        2       2.65             5.66    0.0824           0.161  
## 156 BTM        3       0.620            3.39    0.933            0.140  
## 157 CCE        3       2.34             9.24    0.676            0.161  
## 158 CKB        3       2.98             5.16    0.355            0.649  
## 159 CHZ        3       3.38             2.27    0.537            0.836  
## 160 ETH        3       0.785            0.556   0.542            0.917  
## 161 BRD        3       2.41             3.74    0.160            0.00411
## 162 CRO        3       1.75             0.759   0.000215         0.926  
## 163 DCR        3       1.85             1.15    0.00536          0.595  
## 164 EOS        3       0.807            1.52    0.379            0.841  
## 165 LTC        3       0.819            2.33    0.641            0.179  
## 166 BSV        3       0.485            2.96    0.469            0.436  
## 167 ADA        3       0.584            3.21    0.806            0.00110
## 168 ZEC        3       0.875            1.04    0.789            0.922  
## 169 KNC        3       1.36             1.70    0.148            0.868  
## 170 XEM        3       1.05             1.79    0.926            0.422  
## 171 ARDR       3       1.39             1.34    0.288            0.928  
## 172 NEXO       3       1.26             2.24    0.363            0.156  
## 173 BTC        3       0.694            0.415   0.805            0.983  
## 174 DGB        3       0.812            3.03    0.666            0.598  
## 175 ENJ        3       4.21             0.717   0.480            0.937  
## 176 ELF        3       2.06             1.65    0.801            0.786  
## 177 KMD        3       1.07             1.95    0.682            0.694  
## 178 WAXP       3       1.40             0.999   0.907            0.899  
## 179 AVA        3       2.86             1.28    0.102            0.881  
## 180 NAV        3       6.61             0.769   0.526            0.998  
## 181 FTM        3       1.40             2.44    0.644            0.327  
## 182 SUSHI      3       1.43             1.02    0.212            0.936  
## 183 INJ        3       1.16             1.87    0.397            0.498  
## 184 MANA       3       1.25             1.90    0.917            0.879  
## 185 BTG        3       2.23             1.70    0.0610           0.620  
## 186 TON        3       0.862            0.911   0.0335           0.362  
## 187 ASP        3       1.93             1.31    0.322            0.868  
## 188 CRV        3       1.21             3.95    0.523            0.0334 
## 189 UNI        3       0.816            1.27    0.762            0.843  
## 190 TRX        3       0.699            1.08    0.283            0.873  
## 191 ZRX        3       0.837            1.30    0.253            0.976  
## 192 BNT        3       1.16             1.28    0.390            0.501  
## 193 VIB        3       2.25             1.87    0.998            0.680  
## 194 SUN        3       4.59             1.54    0.249            0.475  
## 195 XMR        3       0.817            1.68    0.424            0.295  
## 196 JST        3       1.29             1.73    0.115            0.604  
## 197 BAT        3       0.831            2.09    0.911            0.571  
## 198 HT         3       0.908            2.19    0.667            0.569  
## 199 SMART      3       1.28             1.64    0.00538          0.429  
## 200 ETP        3       1.62             5.99    0.448            0.156  
## 201 SPC        2       3.22             3.40    0.147            0.00164
## 202 DDR        2       7.29             3.16    0.759            0.564  
## 203 MBL        3       5.20             7.52    0.0331           0.00176
## 204 THETA      2       3.34             3.59    0.219            0.219  
## 205 CBC        2       1.39             1.38    0.451            0.971  
## 206 RIF        2       2.07             1.81    0.000921         0.904  
## 207 YFI        2       2.22             1.39    0.453            0.640  
## 208 DOGE       3       0.934            8.30    0.527            0.506  
## 209 SBD        2       7.53             4.43    0.606            0.136  
## 210 LEVL       3       4.16             3.37    0.719            0.746  
## 211 EDG        2       4.44             5.03    0.162            0.171  
## 212 BCHA       3       2.52             2.25    0.315            0.0407 
## 213 VSYS       4      11.7              8.71    0.00725          0.620  
## 214 IQ         3    1422.               6.94    0.000228         0.596  
## 215 SPC        3       9.72             3.40    0.943            0.00164
## 216 CBC        3       1.93             1.38    0.879            0.971  
## 217 RIF        3       1.46             1.81    0.161            0.904  
## 218 THETA      3       1.03             3.59    0.768            0.219  
## 219 YFI        3       0.648            1.39    0.457            0.640  
## 220 DDR        3       2.20             3.16    0.0876           0.564  
## 221 APPC       4       2.88             8.75    0.412            0.484  
## 222 SBD        3       3.24             4.43    0.657            0.136  
## 223 SRN        4       3.20             3.24    0.107            0.998  
## 224 CVC        3       1.50             1.61    0.344            0.926  
## 225 BCD        3       1.98             4.80    0.0698           0.595  
## 226 UMA        3       0.959            1.75    0.488            0.808  
## 227 NEO        3       0.573            1.19    0.880            0.896  
## 228 ICX        3       1.18             2.32    0.541            0.604  
## 229 MKR        3       0.899            4.53    0.762            0.00301
## 230 EDG        3       3.42             5.03    0.310            0.171  
## 231 AMB        3       5.41             8.23    0.0600           0.145  
## 232 CCE        4       1.90             9.24    0.608            0.161  
## 233 BTM        4       0.792            3.39    0.931            0.140  
## 234 IOST       3       3.36             1.91    0.364            0.797  
## 235 XUC        3       8.09             2.62    0.296            0.0565 
## 236 GOLDR      3       2.22             1.46    0.864            0.579  
## 237 POA        3       0.487           12.2     0.731            0.419  
## 238 ETN        3       1.97             2.18    0.526            0.850  
## 239 STX        3       4.05             3.82    0.0105           0.0365 
## 240 SMART      4       1.17             1.64    0.312            0.429  
## 241 CKB        4       1.72             5.16    0.847            0.649  
## 242 DGB        4       0.746            3.03    0.858            0.598  
## 243 ETH        4       0.336            0.556   0.963            0.917  
## 244 BRD        4       2.95             3.74    0.131            0.00411
## 245 ELF        4       2.61             1.65    0.803            0.786  
## 246 KNC        4       0.788            1.70    0.829            0.868  
## 247 EOS        4       0.750            1.52    0.214            0.841  
## 248 LTC        4       0.232            2.33    0.957            0.179  
## 249 ADA        4       0.572            3.21    0.373            0.00110
## 250 BSV        4       0.593            2.96    0.211            0.436  
## 251 ZEC        4       0.890            1.04    0.768            0.922  
## 252 XEM        4       1.06             1.79    0.575            0.422  
## 253 KMD        4       1.69             1.95    0.813            0.694  
## 254 NEXO       4       1.32             2.24    0.434            0.156  
## 255 CRV        4       1.67             3.95    0.781            0.0334 
## 256 BTC        4       0.422            0.415   0.867            0.983  
## 257 CRO        4       0.582            0.759   0.719            0.926  
## 258 ENJ        4       1.17             0.717   0.527            0.937  
## 259 ARDR       4       2.12             1.34    0.913            0.928  
## 260 WAXP       4       1.76             0.999   0.750            0.899  
## 261 AVA        4       1.92             1.28    0.793            0.881  
## 262 NAV        4       2.91             0.769   0.534            0.998  
## 263 ASP        4       1.23             1.31    0.892            0.868  
## 264 SUSHI      4       0.749            1.02    0.444            0.936  
## 265 FTM        4       1.43             2.44    0.815            0.327  
## 266 INJ        4       1.18             1.87    0.930            0.498  
## 267 BTG        4       1.08             1.70    0.792            0.620  
## 268 UNI        4       1.29             1.27    0.0266           0.843  
## 269 TRX        4       0.874            1.08    0.416            0.873  
## 270 ZRX        4       1.16             1.30    0.863            0.976  
## 271 BNT        4       0.479            1.28    0.636            0.501  
## 272 TON        4       0.647            0.911   0.569            0.362  
## 273 XMR        4       0.474            1.68    0.924            0.295  
## 274 HT         4       2.11             2.19    0.451            0.569  
## 275 MANA       4       1.52             1.90    0.861            0.879  
## 276 DCR        4       0.724            1.15    0.0602           0.595  
## 277 JST        4       2.77             1.73    0.573            0.604  
## 278 CHZ        4       1.81             2.27    0.0518           0.836  
## 279 SUN        4       1.20             1.54    0.802            0.475  
## 280 VIB        4       2.78             1.87    0.493            0.680  
## 281 BAT        4       0.647            2.09    0.850            0.571  
## 282 ETP        4       2.70             5.99    0.726            0.156  
## 283 FUN        3       7.07             2.70    0.0575           0.222  
## 284 OAX        4       1.61             6.08    0.514            0.444  
## 285 DOGE       4       0.314            8.30    0.623            0.506  
## 286 MBL        4       6.16             7.52    0.486            0.00176
## 287 BCHA       4       1.43             2.25    0.439            0.0407 
## 288 LEVL       4       0.897            3.37    0.272            0.746  
## 289 XTZ        3       1.96             2.85    0.141            0.0978 
## 290 VET        3       1.42             5.66    0.0352           0.161  
## 291 IQ         4       9.08             6.94    0.583            0.596  
## 292 SPC        4       3.72             3.40    0.0154           0.00164
## 293 CBC        4       0.947            1.38    0.722            0.971  
## 294 RIF        4       0.878            1.81    0.268            0.904  
## 295 THETA      4       1.61             3.59    0.0653           0.219  
## 296 YFI        4       1.11             1.39    0.166            0.640  
## 297 SBD        4       5.40             4.43    0.752            0.136  
## 298 DDR        4       6.60             3.16    0.799            0.564  
## 299 VSYS       5      NA                8.71   NA                0.620  
## 300 CVC        4       3.26             1.61    0.211            0.926  
## 301 BCD        4       3.46             4.80    0.727            0.595  
## 302 UMA        4       0.771            1.75    0.777            0.808  
## 303 EDG        4       4.14             5.03    0.557            0.171  
## 304 NEO        4       1.06             1.19    0.577            0.896  
## 305 MKR        4       0.868            4.53    0.716            0.00301
## 306 ICX        4       0.959            2.32    0.701            0.604  
## 307 AMB        4       9.02             8.23    0.228            0.145  
## 308 XVG        4       2.01             2.51    0.410            0.831  
## 309 IOST       4       1.09             1.91    0.452            0.797  
## 310 GOLDR      4       4.08             1.46    0.000560         0.579  
## 311 STX        4       5.64             3.82    0.393            0.0365 
## 312 ETN        4       2.65             2.18    0.215            0.850  
## 313 POA        4       0.880           12.2     0.912            0.419  
## 314 FUN        4       4.08             2.70    0.579            0.222  
## 315 XUC        4      13.9              2.62    0.0971           0.0565 
## 316 XTZ        4       3.04             2.85    0.0507           0.0978 
## 317 SRN        5      NA                3.24   NA                0.998  
## 318 VET        4       2.09             5.66    0.664            0.161  
## 319 BTM        5      NA                3.39   NA                0.140  
## 320 CCE        5      NA                9.24   NA                0.161  
## 321 SMART      5      NA                1.64   NA                0.429  
## 322 DGB        5      NA                3.03   NA                0.598  
## 323 ELF        5      NA                1.65   NA                0.786  
## 324 ETH        5      NA                0.556  NA                0.917  
## 325 CRV        5      NA                3.95   NA                0.0334 
## 326 HT         5      NA                2.19   NA                0.569  
## 327 KNC        5      NA                1.70   NA                0.868  
## 328 KMD        5      NA                1.95   NA                0.694  
## 329 EOS        5      NA                1.52   NA                0.841  
## 330 LTC        5      NA                2.33   NA                0.179  
## 331 BSV        5      NA                2.96   NA                0.436  
## 332 ADA        5      NA                3.21   NA                0.00110
## 333 ZEC        5      NA                1.04   NA                0.922  
## 334 CRO        5      NA                0.759  NA                0.926  
## 335 XEM        5      NA                1.79   NA                0.422  
## 336 BTG        5      NA                1.70   NA                0.620  
## 337 NEXO       5      NA                2.24   NA                0.156  
## 338 ASP        5      NA                1.31   NA                0.868  
## 339 BTC        5      NA                0.415  NA                0.983  
## 340 ZRX        5      NA                1.30   NA                0.976  
## 341 ENJ        5      NA                0.717  NA                0.937  
## 342 CKB        5      NA                5.16   NA                0.649  
## 343 BRD        5      NA                3.74   NA                0.00411
## 344 WAXP       5      NA                0.999  NA                0.899  
## 345 AVA        5      NA                1.28   NA                0.881  
## 346 SUSHI      5      NA                1.02   NA                0.936  
## 347 FTM        5      NA                2.44   NA                0.327  
## 348 TON        5      NA                0.911  NA                0.362  
## 349 INJ        5      NA                1.87   NA                0.498  
## 350 TRX        5      NA                1.08   NA                0.873  
## 351 XMR        5      NA                1.68   NA                0.295  
## 352 BNT        5      NA                1.28   NA                0.501  
## 353 MANA       5      NA                1.90   NA                0.879  
## 354 DCR        5      NA                1.15   NA                0.595  
## 355 NAV        5      NA                0.769  NA                0.998  
## 356 UNI        5      NA                1.27   NA                0.843  
## 357 ARDR       5      NA                1.34   NA                0.928  
## 358 JST        5      NA                1.73   NA                0.604  
## 359 SUN        5      NA                1.54   NA                0.475  
## 360 BAT        5      NA                2.09   NA                0.571  
## 361 CHZ        5      NA                2.27   NA                0.836  
## 362 ETP        5      NA                5.99   NA                0.156  
## 363 DOGE       5      NA                8.30   NA                0.506  
## 364 OAX        5      NA                6.08   NA                0.444  
## 365 BCHA       5      NA                2.25   NA                0.0407 
## 366 MBL        5      NA                7.52   NA                0.00176
## 367 VIB        5      NA                1.87   NA                0.680  
## 368 APPC       5      NA                8.75   NA                0.484  
## 369 LEVL       5      NA                3.37   NA                0.746  
## 370 IQ         5      NA                6.94   NA                0.596  
## 371 SPC        5      NA                3.40   NA                0.00164
## 372 CBC        5      NA                1.38   NA                0.971  
## 373 RIF        5      NA                1.81   NA                0.904  
## 374 YFI        5      NA                1.39   NA                0.640  
## 375 THETA      5      NA                3.59   NA                0.219  
## 376 GOLDR      5      NA                1.46   NA                0.579  
## 377 EDG        5      NA                5.03   NA                0.171  
## 378 SBD        5      NA                4.43   NA                0.136  
## 379 CVC        5      NA                1.61   NA                0.926  
## 380 BCD        5      NA                4.80   NA                0.595  
## 381 NEO        5      NA                1.19   NA                0.896  
## 382 UMA        5      NA                1.75   NA                0.808  
## 383 MKR        5      NA                4.53   NA                0.00301
## 384 IOST       5      NA                1.91   NA                0.797  
## 385 ICX        5      NA                2.32   NA                0.604  
## 386 AMB        5      NA                8.23   NA                0.145  
## 387 DDR        5      NA                3.16   NA                0.564  
## 388 XVG        5      NA                2.51   NA                0.831  
## 389 STX        5      NA                3.82   NA                0.0365 
## 390 ETN        5      NA                2.18   NA                0.850  
## 391 POA        5      NA               12.2    NA                0.419  
## 392 FUN        5      NA                2.70   NA                0.222  
## 393 XTZ        5      NA                2.85   NA                0.0978 
## 394 XUC        5      NA                2.62   NA                0.0565 
## 395 VET        5      NA                5.66   NA                0.161

Out of 395 groups, 117 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: 395 x 6
## # Groups:   symbol, split [395]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        1.06        0.709           0.415        0.983  
##  2 ETH        1        0.932       0.366           0.556        0.917  
##  3 EOS        1        1.54        0.489           1.52         0.841  
##  4 LTC        1        1.09        0.322           2.33         0.179  
##  5 BSV        1        1.12        0.722           2.96         0.436  
##  6 ADA        1        1.26        0.212           3.21         0.00110
##  7 ZEC        1        0.633       0.729           1.04         0.922  
##  8 HT         1        1.32        0.307           2.19         0.569  
##  9 TRX        1        3.77        0.396           1.08         0.873  
## 10 KNC        1        0.938       0.913           1.70         0.868  
## # ... with 385 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: 395 x 3
## # Groups:   symbol [79]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.698
##  2 ETH    lm    0.716
##  3 EOS    lm    0.957
##  4 LTC    lm    0.868
##  5 BSV    lm    1.02 
##  6 ADA    lm    0.772
##  7 ZEC    lm    1.00 
##  8 HT     lm    1.52 
##  9 TRX    lm    1.52 
## 10 KNC    lm    1.00 
## # ... with 385 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: 395 x 3
## # Groups:   symbol [79]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.415
##  2 ETH    lm    0.556
##  3 EOS    lm    1.52 
##  4 LTC    lm    2.33 
##  5 BSV    lm    2.96 
##  6 ADA    lm    3.21 
##  7 ZEC    lm    1.04 
##  8 HT     lm    2.19 
##  9 TRX    lm    1.08 
## 10 KNC    lm    1.70 
## # ... with 385 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: 395 x 3
## # Groups:   symbol [79]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.790
##  2 ETH    lm    0.685
##  3 EOS    lm    0.493
##  4 LTC    lm    0.609
##  5 BSV    lm    0.374
##  6 ADA    lm    0.584
##  7 ZEC    lm    0.706
##  8 HT     lm    0.588
##  9 TRX    lm    0.497
## 10 KNC    lm    0.683
## # ... with 385 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: 395 x 3
## # Groups:   symbol [79]
##    symbol model     rsq
##    <chr>  <chr>   <dbl>
##  1 BTC    lm    0.983  
##  2 ETH    lm    0.917  
##  3 EOS    lm    0.841  
##  4 LTC    lm    0.179  
##  5 BSV    lm    0.436  
##  6 ADA    lm    0.00110
##  7 ZEC    lm    0.922  
##  8 HT     lm    0.569  
##  9 TRX    lm    0.873  
## 10 KNC    lm    0.868  
## # ... with 385 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.