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 
## 2541.51107673278    0.00007081014 2082.74005199927

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.02819775         NA

The result above may show a value of NA for the RMSE metric. We will explain and resolve the issue later on.

8.1.5 Comparing Metrics

Why not just pick one metric and stick to it? We certainly could, but these two metrics complement each other. For example, if we had a model that always predicts a 0% price change for the time period, the model may have a low error but it wouldn’t actually be very informative in the direction or magnitude of those movements and the predictions and actuals would not be very correlated with each other which would lead to a low \(R^2\) value. We are using both because it helps paint a more complete picture in this sense, and depending on the task you may want to use a different set of metrics to evaluate the performance. It is also worth mentioning that if your target variable you are predicting is either 0 or 1, this would be a classification problem where different metrics become more appropriate to use.

These are indicators that should be taken with a grain of salt individually, but comparing the results across many different models for the same cryptocurrency can help us determine which models work best for the problem, and then comparing those results across many cryptocurrencies can help us understand which cryptocurrencies we can predict with the most accuracy.

Before we can draw these comparisons however, we will need to “standardize” the values to create a fair comparison across all dataasets.

8.2 Data Prep - Adjust Prices

Because cryptocurrencies can vary dramatically in their prices with some trading in the tens of thousands of dollars and others trading for less than a cent, we need to make sure to standardize the RMSE columns to provide a fair comparison for the metric.

Therefore, before using the postResample() function, let’s convert both the predictions and the target to be the % change in price over the 24 hour period, rather than the change in price ($).

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

8.2.1 Add Last Price

In order to convert the first prediction made to be a percentage, we need to know the previous price, which would be the last observation from the train data. Therefore, let’s make a function to add the last_price_train column and append it to the predictions made so we can calculate the % change of the first element relative to the last observation in the train data, before later removing the value not associated with the predictions:

last_train_price <- function(train_data, predictions){
  c(tail(train_data$price_usd,1), predictions)
}

We will first perform all steps on the linear regression models to make the code a little more digestible, and we will then perform the same steps for the rest of the models.

8.2.1.1 Test

Overwrite the old predictions for the first 4 splits of the test data using the new function created above:

cryptodata_nested <- mutate(cryptodata_nested,
                            lm_test_predictions = ifelse(split < 5,
                                                         map2(train_data, lm_test_predictions, last_train_price),
                                                         NA))

The mutate() function is used to create the new column lm_test_predictions assigning the value only for the first 4 splits where the test data would actually exist (the 5th being the holdout set) using the ifelse() function.

8.2.1.2 Holdout

Do the same but for the holdout now. For the holdout we need to take the last price point of the 5th split:

cryptodata_nested_holdout <- mutate(filter(cryptodata_nested, split == 5),
                                    lm_holdout_predictions = map2(train_data, lm_holdout_predictions, last_train_price))

Now join the holdout data to all rows based on the cryptocurrency symbol alone:

cryptodata_nested <- left_join(cryptodata_nested, 
                               select(cryptodata_nested_holdout, symbol, lm_holdout_predictions),
                               by='symbol')
# Remove unwanted columns
cryptodata_nested <- select(cryptodata_nested, -lm_holdout_predictions.x, -split.y)
# Rename the columns kept
cryptodata_nested <- rename(cryptodata_nested, 
                            lm_holdout_predictions = 'lm_holdout_predictions.y',
                            split = 'split.x')
# Reset the correct grouping structure
cryptodata_nested <- group_by(cryptodata_nested, symbol, split)

8.2.2 Convert to Percentage Change

Now we have everything we need to accurately calculate the percentage change between observations including the first one. Let’s make a new function to calculate the percentage change:

standardize_perc_change <- function(predictions){
  results <- (diff(c(lag(predictions, 1), predictions)) / lag(predictions, 1))*100
  # Exclude the first element, next element will be % change of first prediction
  tail(head(results, length(predictions)), length(predictions)-1)
}

Overwrite the old predictions with the new predictions adjusted as a percentage now:

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

8.2.3 Actuals

Now do the same thing to the actual prices. Let’s make a new column called actuals containing the real price values (rather than the predicted ones):

actuals_create <- function(train_data, test_data){
  c(tail(train_data$price_usd,1), as.numeric(unlist(select(test_data, price_usd))))
}

Use the new function to create the new column actuals:

cryptodata_nested <- mutate(cryptodata_nested,
                            actuals_test = ifelse(split < 5,
                                             map2(train_data, test_data, actuals_create),
                                             NA))

8.2.3.1 Holdout

Again, for the holdout we need the price from the training data of the 5th split to perform the first calculation:

cryptodata_nested_holdout <- mutate(filter(cryptodata_nested, split == 5),
                                    actuals_holdout = map2(train_data, holdout_data, actuals_create))

Join the holdout data to all rows based on the cryptocurrency symbol alone:

cryptodata_nested <- left_join(cryptodata_nested, 
                               select(cryptodata_nested_holdout, symbol, actuals_holdout),
                               by='symbol')
# Remove unwanted columns
cryptodata_nested <- select(cryptodata_nested, -split.y)
# Rename the columns kept
cryptodata_nested <- rename(cryptodata_nested, split = 'split.x')
# Reset the correct grouping structure
cryptodata_nested <- group_by(cryptodata_nested, symbol, split)

8.2.4 Actuals as % Change

Now we can convert the new actuals to express the price_usd as a % change relative to the previous value using adapting the function from earlier:

actuals_perc_change <- function(predictions){
  results <- (diff(c(lag(predictions, 1), predictions)) / lag(predictions, 1))*100
  # Exclude the first element, next element will be % change of first prediction
  tail(head(results, length(predictions)), length(predictions)-1)
}
cryptodata_nested <- mutate(cryptodata_nested,
                            actuals_test = ifelse(split < 5,
                                             map(actuals_test, actuals_perc_change),
                                             NA),
                            actuals_holdout = map(actuals_holdout, actuals_perc_change))

8.3 Review Summary Statistics

Now that we standardized the price to show the percentage change relative to the previous period instead of the price in dollars, we can actually compare the summary statistics across all cryptocurrencies and have it be a fair comparison.

Let’s get the same statistic as we did at the beginning of this section, but this time on the standardized values. This time to calculate the RMSE error metric let’s use the rmse() function from the hydroGOF package because it allows us to set the na.rm = T parameter, and otherwise one NA value would return NA for the overall RMSE:

hydroGOF::rmse(cryptodata_nested$lm_test_predictions[[1]], 
               cryptodata_nested$actuals_test[[1]], 
               na.rm=T)
## [1] 0.8664259

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: 570 x 4
## # Groups:   symbol, split [570]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1       0.512         0.758 
##  2 ETH        1       0.404         0.0665
##  3 EOS        1       0.794         0.681 
##  4 LTC        1       0.576         0.757 
##  5 ADA        1       0.724         0.737 
##  6 BSV        1       0.168         0.885 
##  7 ZEC        1       0.923         0.743 
##  8 HT         1       0.390         0.347 
##  9 TRX        1       0.213         0.789 
## 10 KNC        1       0.710         0.644 
## # ... with 560 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: 570 x 6
## # Groups:   symbol, split [570]
##     symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>       <dbl>          <dbl>
##   1 BTC        1        0.866           1.82   0.512           0.758    
##   2 ETH        1        1.64            2.35   0.404           0.0665   
##   3 EOS        1        0.690           2.31   0.794           0.681    
##   4 LTC        1        1.04            2.01   0.576           0.757    
##   5 ADA        1        1.80            1.76   0.724           0.737    
##   6 BSV        1        1.64            1.56   0.168           0.885    
##   7 ZEC        1        0.511           2.19   0.923           0.743    
##   8 HT         1        2.57            3.40   0.390           0.347    
##   9 TRX        1        1.91            1.24   0.213           0.789    
##  10 KNC        1        1.02            2.38   0.710           0.644    
##  11 XMR        1        0.932           1.49   0.744           0.846    
##  12 SEELE      1        1.87            1.81   0.222           0.224    
##  13 ZRX        1        2.11            2.12   0.0381          0.759    
##  14 BAT        1        2.16            1.16   0.606           0.906    
##  15 BNT        1        0.653           2.70   0.850           0.174    
##  16 CRO        1        1.51            1.27   0.189           0.744    
##  17 MANA       1        1.02            2.48   0.857           0.116    
##  18 DGB        1        1.82            2.25   0.317           0.884    
##  19 ENJ        1        2.52            2.62   0.380           0.258    
##  20 BTM        1        0.745           1.00   0.928           0.845    
##  21 XEM        1        2.70            1.64   0.546           0.841    
##  22 BTG        1        2.00            0.895  0.00607         0.944    
##  23 ARDR       1        2.27            2.02   0.650           0.775    
##  24 KMD        1        1.66            1.87   0.434           0.871    
##  25 ELF        1        1.81            5.80   0.478           0.360    
##  26 NEXO       1        0.990           1.62   0.704           0.524    
##  27 VSYS       1        2.18            5.03   0.223           0.390    
##  28 CHZ        1        1.95            3.09   0.755           0.0657   
##  29 CKB        1        7.90            2.72   0.420           0.603    
##  30 BRD        1        2.78            6.28   0.215           0.0131   
##  31 DCR        1        1.06            2.55   0.734           0.178    
##  32 VIB        1        2.21            7.09   0.0873          0.201    
##  33 WAXP       1        1.52            8.07   0.166           0.0404   
##  34 DENT       1        1.80            2.84   0.898           0.700    
##  35 OAX        1        5.25            6.12   0.534           0.206    
##  36 RCN        1        3.30            4.95   0.376           0.566    
##  37 ETP        1        1.55            4.60   0.398           0.847    
##  38 AVA        1        2.84            2.25   0.316           0.255    
##  39 APPC       1        1.08           10.3    0.932           0.212    
##  40 NAV        1        4.59            1.93   0.00553         0.563    
##  41 SMART      1        1.30            2.21   0.608           0.469    
##  42 JST        1        2.40            7.68   0.257           0.553    
##  43 SRN        1        3.43            7.41   0.398           0.0315   
##  44 TON        1        0.195           0.672  0.654           0.373    
##  45 SUN        1        2.49            2.14   0.286           0.305    
##  46 LEVL       1        7.93            3.63   0.131           0.184    
##  47 THETA      1        1.43            2.58   0.419           0.774    
##  48 CBC        1        2.42            2.32   0.277           0.326    
##  49 DDR        1       11.4             9.15   0.0691          0.0140   
##  50 FTM        1      257.              2.68   0.00138         0.422    
##  51 ASP        1        4.63            2.90   0.287           0.622    
##  52 YFI        1        1.91            3.51   0.294           0.276    
##  53 SBD        1       12.3             1.21   0.00000974      0.387    
##  54 HUB        1       17.2             4.01   0.283           0.403    
##  55 EDG        1        4.50            4.05   0.106           0.312    
##  56 SPC        1        6.39            2.01   0.162           0.208    
##  57 CRV        1        1.31            1.77   0.627           0.840    
##  58 SUSHI      1        2.94            1.94   0.208           0.608    
##  59 RIF        1        1.58            3.78   0.297           0.409    
##  60 BCHA       1        2.06            1.36   0.530           0.671    
##  61 UNI        1        0.844           1.90   0.921           0.361    
##  62 SUB        1        5.97            9.13   0.105           0.254    
##  63 CCE        1        2.70            2.52   0.205           0.0160   
##  64 INJ        1        2.45            2.25   0.151           0.657    
##  65 VET        1        1.74            2.54   0.688           0.428    
##  66 REX        1       13.4             5.94   0.00107         0.309    
##  67 IQ         1        1.78            2.87   0.694           0.115    
##  68 CND        1       10.2             3.13   0.0229          0.587    
##  69 ACT        1        4.09            5.41   0.506           0.482    
##  70 DOGE       1        1.26            2.16   0.164           0.750    
##  71 SWRV       1        1.20            9.98   0.160           0.000750 
##  72 OAX        2        0.147           6.12   0.178           0.206    
##  73 VSYS       2        0.857           5.03   0.991           0.390    
##  74 VIB        2        2.58            7.09   0.0866          0.201    
##  75 SRN        2        1.27            7.41   0.692           0.0315   
##  76 REX        2        2.97            5.94   0.266           0.309    
##  77 RCN        2      NaN               4.95  NA               0.566    
##  78 HUB        2        7.98            4.01   0.990           0.403    
##  79 SPC        2        0.724           2.01   0.747           0.208    
##  80 CND        2        1.02            3.13   0.980           0.587    
##  81 SUB        2       34.1             9.13   0.0183          0.254    
##  82 CCE        2        4.73            2.52   0.0294          0.0160   
##  83 SMART      2        1.72            2.21   0.647           0.469    
##  84 BTM        2        1.23            1.00   0.627           0.845    
##  85 NAV        2        5.71            1.93   0.0668          0.563    
##  86 DDR        2        2.09            9.15   0.526           0.0140   
##  87 DGB        2        1.05            2.25   0.858           0.884    
##  88 BRD        2        1.16            6.28   0.630           0.0131   
##  89 CKB        2        2.52            2.72   0.490           0.603    
##  90 ELF        2        1.37            5.80   0.810           0.360    
##  91 ETH        2        0.318           2.35   0.762           0.0665   
##  92 LTC        2        0.705           2.01   0.306           0.757    
##  93 ZEC        2        0.564           2.19   0.955           0.743    
##  94 XMR        2        0.331           1.49   0.860           0.846    
##  95 KNC        2        2.94            2.38   0.619           0.644    
##  96 ZRX        2        1.99            2.12   0.0475          0.759    
##  97 KMD        2        1.88            1.87   0.797           0.871    
##  98 DENT       2        1.77            2.84   0.930           0.700    
##  99 BTC        2        0.283           1.82   0.922           0.758    
## 100 EOS        2        1.23            2.31   0.229           0.681    
## 101 ADA        2        1.75            1.76   0.253           0.737    
## 102 BSV        2        0.890           1.56   0.532           0.885    
## 103 TRX        2        1.87            1.24   0.914           0.789    
## 104 CRO        2        0.489           1.27   0.937           0.744    
## 105 ENJ        2        2.57            2.62   0.0447          0.258    
## 106 XEM        2        1.08            1.64   0.427           0.841    
## 107 ARDR       2        1.83            2.02   0.230           0.775    
## 108 NEXO       2        0.423           1.62   0.809           0.524    
## 109 WAXP       2        1.31            8.07   0.744           0.0404   
## 110 AVA        2        1.24            2.25   0.782           0.255    
## 111 JST        2        1.63            7.68   0.890           0.553    
## 112 THETA      2        0.680           2.58   0.899           0.774    
## 113 FTM        2        1.43            2.68   0.849           0.422    
## 114 ASP        2        1.28            2.90   0.517           0.622    
## 115 CRV        2        3.96            1.77   0.263           0.840    
## 116 SUSHI      2        0.601           1.94   0.611           0.608    
## 117 INJ        2        0.799           2.25   0.824           0.657    
## 118 CBC        2        0.846           2.32   0.494           0.326    
## 119 RIF        2        0.765           3.78   0.707           0.409    
## 120 UNI        2        0.868           1.90   0.703           0.361    
## 121 BNT        2        0.588           2.70   0.818           0.174    
## 122 BTG        2        0.435           0.895  0.823           0.944    
## 123 YFI        2        0.562           3.51   0.685           0.276    
## 124 MANA       2        4.53            2.48   0.287           0.116    
## 125 ETP        2        1.34            4.60   0.251           0.847    
## 126 TON        2        1.62            0.672  0.275           0.373    
## 127 HT         2        0.690           3.40   0.207           0.347    
## 128 DCR        2        0.392           2.55   0.720           0.178    
## 129 BAT        2        3.15            1.16   0.806           0.906    
## 130 SUN        2        1.50            2.14   0.740           0.305    
## 131 BCHA       2        1.48            1.36   0.000741        0.671    
## 132 CHZ        2        1.34            3.09   0.718           0.0657   
## 133 SBD        2        1.90            1.21   0.908           0.387    
## 134 LEVL       2        1.81            3.63   0.246           0.184    
## 135 APPC       2        0.350          10.3    0.968           0.212    
## 136 SEELE      2       18.4             1.81   0.0493          0.224    
## 137 DOGE       2        0.386           2.16   0.700           0.750    
## 138 EDG        2        4.85            4.05   0.376           0.312    
## 139 ACT        2       15.4             5.41   0.0383          0.482    
## 140 IQ         2        5.79            2.87   0.439           0.115    
## 141 SNX        1        1.36            3.76   0.181           0.553    
## 142 DDRT       1        0.978           6.11   0.270           0.0176   
## 143 ZIL        1        1.06            1.93   0.765           0.0510   
## 144 DASH       1        1.25            2.13   0.366           0.0825   
## 145 NXT        1        3.41            4.03   0.122           0.227    
## 146 GASP       1        0.897           1.66   0.811           0.667    
## 147 MAID       1        2.40            2.81   0.141           0.546    
## 148 ETC        1        1.54            0.886  0.0625          0.888    
## 149 UNO        1        2.31            0.653  0.501           0.653    
## 150 GOLDR      1        1.49            5.30   0.00357         0.0385   
## 151 CVC        1        1.50            1.89   0.375           0.259    
## 152 MKR        1        0.873           3.29   0.748           0.352    
## 153 BZRX       1        1.61            3.93   0.738           0.0000739
## 154 STRAT      1        1.98            6.45   0.581           0.587    
## 155 XTZ        1        0.722           1.56   0.894           0.144    
## 156 BCD        1        2.43            3.11   0.00266         0.531    
## 157 EVX        1        3.52           11.9    0.00500         0.0130   
## 158 SXP        1        3.07            3.11   0.632           0.197    
## 159 STX        1        8.51            2.23   0.0000985       0.858    
## 160 XUC        1        8.09            1.17   0.296           0.669    
## 161 BNB        1        0.892           2.53   0.321           0.115    
## 162 PPC        1        1.79            4.55   0.603           0.175    
## 163 BMC        1        3.61            7.81   0.561           0.350    
## 164 NEAR       1        1.93            3.00   0.0723          0.0121   
## 165 ETN        1        1.75            4.68   0.337           0.191    
## 166 FUN        1        5.30            1.54   0.109           0.000472 
## 167 AMB        1        4.56            6.19   0.119           0.103    
## 168 COTI       1        2.71            4.37   0.462           0.00787  
## 169 NEO        1        0.646           1.80   0.809           0.149    
## 170 ICX        1        1.16            3.60   0.737           0.125    
## 171 GLM        1        1.28            1.31   0.209           0.565    
## 172 XVG        1        1.91            2.32   0.336           0.401    
## 173 BYTZ       1        9.74            4.17   0.341           0.0396   
## 174 UMA        1        0.984           1.97   0.484           0.338    
## 175 TOMO       1        1.74            3.40   0.199           0.00520  
## 176 DGTX       1     2642.              7.30   0.000761        0.289    
## 177 BCH        1        0.717           1.55   0.597           0.711    
## 178 CLO        1        2.03            2.92   0.260           0.551    
## 179 IQN        1        0.407           0.262  0.109           0.300    
## 180 POA        1        1.51            7.86   0.0369          0.227    
## 181 IOST       1        2.64            1.13   0.257           0.589    
## 182 FIL        1        4.15            5.47   0.223           0.431    
## 183 SNC        1        3.31            8.04   0.500           0.0154   
## 184 SWRV       2       16.2             9.98   0.000795        0.000750 
## 185 VSYS       3      NaN               5.03  NA               0.390    
## 186 VET        2        0.619           2.54   0.749           0.428    
## 187 CND        3        1.10            3.13   0.0118          0.587    
## 188 REX        3        8.25            5.94   0.00000676      0.309    
## 189 SRN        3       12.6             7.41   0.801           0.0315   
## 190 CCE        3        5.29            2.52   0.0432          0.0160   
## 191 BTM        3        2.99            1.00   0.0561          0.845    
## 192 VIB        3        1.54            7.09   0.174           0.201    
## 193 NAV        3        2.69            1.93   0.218           0.563    
## 194 SMART      3        3.89            2.21   0.941           0.469    
## 195 SUB        3        3.18            9.13   0.0928          0.254    
## 196 SBD        3        4.32            1.21   0.506           0.387    
## 197 BCHA       3        2.23            1.36   0.674           0.671    
## 198 DGB        3        0.988           2.25   0.714           0.884    
## 199 SPC        3        3.30            2.01   0.559           0.208    
## 200 BRD        3        2.77            6.28   0.303           0.0131   
## 201 OAX        3        0.879           6.12   0.247           0.206    
## 202 ELF        3        1.92            5.80   0.841           0.360    
## 203 KNC        3        0.558           2.38   0.917           0.644    
## 204 LTC        3        1.18            2.01   0.567           0.757    
## 205 ZEC        3        1.09            2.19   0.756           0.743    
## 206 HT         3        1.29            3.40   0.267           0.347    
## 207 XMR        3        0.907           1.49   0.313           0.846    
## 208 KMD        3        2.29            1.87   0.860           0.871    
## 209 DENT       3        3.01            2.84   0.489           0.700    
## 210 ETH        3        0.743           2.35   0.624           0.0665   
## 211 ZRX        3        1.24            2.12   0.482           0.759    
## 212 ASP        3        1.69            2.90   0.455           0.622    
## 213 CRV        3        0.762           1.77   0.840           0.840    
## 214 SUSHI      3        0.827           1.94   0.786           0.608    
## 215 FTM        3        1.83            2.68   0.533           0.422    
## 216 HUB        3       10.9             4.01   0.0803          0.403    
## 217 INJ        3        1.08            2.25   0.472           0.657    
## 218 THETA      3        1.07            2.58   0.541           0.774    
## 219 BTC        3        0.396           1.82   0.577           0.758    
## 220 EOS        3        1.59            2.31   0.545           0.681    
## 221 ADA        3        0.830           1.76   0.631           0.737    
## 222 BSV        3        0.459           1.56   0.928           0.885    
## 223 TRX        3        4.14            1.24   0.187           0.789    
## 224 BNT        3        2.04            2.70   0.386           0.174    
## 225 CRO        3        1.09            1.27   0.317           0.744    
## 226 ENJ        3        4.17            2.62   0.126           0.258    
## 227 XEM        3        1.11            1.64   0.825           0.841    
## 228 ARDR       3        1.56            2.02   0.850           0.775    
## 229 NEXO       3        0.743           1.62   0.669           0.524    
## 230 WAXP       3        2.79            8.07   0.761           0.0404   
## 231 AVA        3        1.10            2.25   0.427           0.255    
## 232 JST        3        4.39            7.68   0.278           0.553    
## 233 CBC        3        1.71            2.32   0.922           0.326    
## 234 RIF        3        1.49            3.78   0.387           0.409    
## 235 UNI        3        0.558           1.90   0.831           0.361    
## 236 ETP        3        2.07            4.60   0.765           0.847    
## 237 YFI        3        1.40            3.51   0.680           0.276    
## 238 CKB        3        1.53            2.72   0.647           0.603    
## 239 TON        3        1.08            0.672  0.238           0.373    
## 240 BTG        3        1.99            0.895  0.973           0.944    
## 241 MANA       3        1.06            2.48   0.360           0.116    
## 242 BAT        3        0.978           1.16   0.557           0.906    
## 243 DCR        3        1.32            2.55   0.0475          0.178    
## 244 SUN        3        4.01            2.14   0.500           0.305    
## 245 CHZ        3        2.26            3.09   0.255           0.0657   
## 246 EDG        3        4.29            4.05   0.703           0.312    
## 247 BMC        2      NaN               7.81  NA               0.350    
## 248 ACT        3       11.1             5.41   0.0719          0.482    
## 249 DOGE       3        0.958           2.16   0.701           0.750    
## 250 DDR        3        5.64            9.15   0.210           0.0140   
## 251 POA        2        4.23            7.86   0.0110          0.227    
## 252 DDRT       2        8.91            6.11   0.000252        0.0176   
## 253 LEVL       3        3.55            3.63   0.343           0.184    
## 254 UNO        2        0.632           0.653  0.821           0.653    
## 255 IQN        2        0.718           0.262  0.376           0.300    
## 256 SEELE      3        2.16            1.81   0.356           0.224    
## 257 GLM        2        2.51            1.31   0.697           0.565    
## 258 BYTZ       2        3.83            4.17   0.0487          0.0396   
## 259 NXT        2        0.463           4.03   0.878           0.227    
## 260 MAID       2        2.11            2.81   0.942           0.546    
## 261 FIL        2        4.00            5.47   0.290           0.431    
## 262 CLO        2        1.43            2.92   0.573           0.551    
## 263 ETC        2        0.752           0.886  0.488           0.888    
## 264 EVX        2        7.65           11.9    0.209           0.0130   
## 265 PPC        2        0.986           4.55   0.765           0.175    
## 266 COTI       2        2.03            4.37   0.0423          0.00787  
## 267 UMA        2        0.695           1.97   0.793           0.338    
## 268 ZIL        2        1.39            1.93   0.406           0.0510   
## 269 DASH       2        0.891           2.13   0.757           0.0825   
## 270 GASP       2        1.56            1.66   0.762           0.667    
## 271 CVC        2        3.81            1.89   0.161           0.259    
## 272 MKR        2        0.486           3.29   0.905           0.352    
## 273 BZRX       2        1.91            3.93   0.490           0.0000739
## 274 BCD        2        3.53            3.11   0.742           0.531    
## 275 SXP        2        2.50            3.11   0.756           0.197    
## 276 BNB        2        0.890           2.53   0.731           0.115    
## 277 NEAR       2        1.13            3.00   0.403           0.0121   
## 278 NEO        2        0.859           1.80   0.751           0.149    
## 279 TOMO       2        1.51            3.40   0.825           0.00520  
## 280 BCH        2        0.634           1.55   0.794           0.711    
## 281 GOLDR      2        2.75            5.30   0.274           0.0385   
## 282 ETN        2        2.54            4.68   0.114           0.191    
## 283 AMB        2        8.81            6.19   0.302           0.103    
## 284 IOST       2        1.14            1.13   0.348           0.589    
## 285 STX        2        5.81            2.23   0.226           0.858    
## 286 ICX        2        1.26            3.60   0.486           0.125    
## 287 FUN        2        4.13            1.54   0.540           0.000472 
## 288 XTZ        2        1.13            1.56   0.255           0.144    
## 289 RCN        3        3.82            4.95   0.159           0.566    
## 290 XVG        2        2.18            2.32   0.285           0.401    
## 291 SNC        2        1.47            8.04   0.626           0.0154   
## 292 SNX        2        1.33            3.76   0.563           0.553    
## 293 DGTX       2        4.72            7.30   0.306           0.289    
## 294 XUC        2       12.6             1.17   0.0169          0.669    
## 295 IQ         3        4.09            2.87   0.257           0.115    
## 296 STRAT      2        0.480           6.45   0.914           0.587    
## 297 APPC       3       13.1            10.3    0.323           0.212    
## 298 SWRV       3        3.05            9.98   0.0609          0.000750 
## 299 VET        3        1.27            2.54   0.891           0.428    
## 300 POA        3        0.925           7.86   0.952           0.227    
## 301 SRN        4        5.96            7.41   0.583           0.0315   
## 302 DDRT       3        4.24            6.11   0.0145          0.0176   
## 303 CND        4        1.95            3.13   0.126           0.587    
## 304 IQN        3        0.302           0.262  0.961           0.300    
## 305 NXT        3        1.17            4.03   0.907           0.227    
## 306 GOLDR      3        1.08            5.30   0.291           0.0385   
## 307 CLO        3        4.48            2.92   0.0539          0.551    
## 308 EVX        3       10.5            11.9    0.122           0.0130   
## 309 PPC        3        1.50            4.55   0.788           0.175    
## 310 ZIL        3        2.51            1.93   0.0713          0.0510   
## 311 DASH       3        2.95            2.13   0.219           0.0825   
## 312 GASP       3        0.672           1.66   0.817           0.667    
## 313 CVC        3        1.53            1.89   0.791           0.259    
## 314 FIL        3        2.04            5.47   0.200           0.431    
## 315 MKR        3        3.35            3.29   0.161           0.352    
## 316 BZRX       3        3.02            3.93   0.592           0.0000739
## 317 BCD        3        5.50            3.11   0.611           0.531    
## 318 BNB        3        0.968           2.53   0.833           0.115    
## 319 NEAR       3        4.27            3.00   0.000883        0.0121   
## 320 AMB        3        7.92            6.19   0.0825          0.103    
## 321 NEO        3        2.56            1.80   0.912           0.149    
## 322 UMA        3        1.32            1.97   0.868           0.338    
## 323 TOMO       3        2.69            3.40   0.852           0.00520  
## 324 BCH        3        5.04            1.55   0.384           0.711    
## 325 IOST       3        1.54            1.13   0.801           0.589    
## 326 ETC        3        3.80            0.886  0.879           0.888    
## 327 SXP        3        3.30            3.11   0.288           0.197    
## 328 STX        3        3.12            2.23   0.206           0.858    
## 329 ETN        3        2.88            4.68   0.615           0.191    
## 330 COTI       3        2.96            4.37   0.753           0.00787  
## 331 FUN        3        1.98            1.54   0.526           0.000472 
## 332 ICX        3        2.29            3.60   0.164           0.125    
## 333 XTZ        3        2.48            1.56   0.535           0.144    
## 334 BYTZ       3        7.14            4.17   0.516           0.0396   
## 335 UNO        3        1.92            0.653  0.0659          0.653    
## 336 SNX        3        2.05            3.76   0.528           0.553    
## 337 SNC        3        2.21            8.04   0.0453          0.0154   
## 338 MAID       3        1.65            2.81   0.164           0.546    
## 339 XVG        3        4.43            2.32   0.498           0.401    
## 340 CCE        4       11.6             2.52   0.00260         0.0160   
## 341 OAX        4        4.52            6.12   0.399           0.206    
## 342 GLM        3        2.69            1.31   0.653           0.565    
## 343 SUB        4        5.04            9.13   0.366           0.254    
## 344 VSYS       4        1.95            5.03   0.00359         0.390    
## 345 NAV        4        1.30            1.93   0.241           0.563    
## 346 VIB        4        4.25            7.09   0.493           0.201    
## 347 SBD        4        4.29            1.21   0.505           0.387    
## 348 HUB        4        3.07            4.01   0.144           0.403    
## 349 BCHA       4        1.13            1.36   0.274           0.671    
## 350 EDG        4        2.63            4.05   1               0.312    
## 351 DGTX       3        3.30            7.30   0.0970          0.289    
## 352 BTM        4        2.02            1.00   0.605           0.845    
## 353 REX        4        0.747           5.94   0.0535          0.309    
## 354 ACT        4        4.09            5.41   0.830           0.482    
## 355 BRD        4        4.56            6.28   0.0755          0.0131   
## 356 ELF        4        1.22            5.80   0.869           0.360    
## 357 LTC        4        1.62            2.01   0.333           0.757    
## 358 XMR        4        2.13            1.49   0.469           0.846    
## 359 KMD        4        2.18            1.87   0.917           0.871    
## 360 ZEC        4        1.74            2.19   0.521           0.743    
## 361 ZRX        4        1.06            2.12   0.827           0.759    
## 362 BNT        4        0.651           2.70   0.793           0.174    
## 363 CRO        4        1.21            1.27   0.841           0.744    
## 364 DENT       4        4.59            2.84   0.571           0.700    
## 365 ETP        4        1.39            4.60   0.649           0.847    
## 366 ASP        4        0.812           2.90   0.894           0.622    
## 367 SUSHI      4        1.51            1.94   0.610           0.608    
## 368 FTM        4        2.54            2.68   0.791           0.422    
## 369 INJ        4        4.96            2.25   0.0229          0.657    
## 370 THETA      4        1.58            2.58   0.820           0.774    
## 371 ETH        4        0.869           2.35   0.796           0.0665   
## 372 CBC        4        2.59            2.32   0.533           0.326    
## 373 CRV        4        1.43            1.77   0.562           0.840    
## 374 RIF        4        1.78            3.78   0.728           0.409    
## 375 UNI        4        1.41            1.90   0.713           0.361    
## 376 BTC        4        0.362           1.82   0.939           0.758    
## 377 EOS        4        2.42            2.31   0.387           0.681    
## 378 ADA        4        1.92            1.76   0.433           0.737    
## 379 BSV        4        1.62            1.56   0.658           0.885    
## 380 TRX        4        2.04            1.24   0.567           0.789    
## 381 HT         4        1.02            3.40   0.568           0.347    
## 382 KNC        4        1.72            2.38   0.484           0.644    
## 383 DGB        4        3.64            2.25   0.0406          0.884    
## 384 ENJ        4        1.19            2.62   0.887           0.258    
## 385 XEM        4        2.54            1.64   0.306           0.841    
## 386 NEXO       4        0.898           1.62   0.734           0.524    
## 387 WAXP       4        1.65            8.07   0.837           0.0404   
## 388 AVA        4        1.74            2.25   0.702           0.255    
## 389 JST        4        1.59            7.68   0.794           0.553    
## 390 ARDR       4        3.37            2.02   0.445           0.775    
## 391 YFI        4        1.98            3.51   0.139           0.276    
## 392 TON        4        0.930           0.672  0.191           0.373    
## 393 XUC        3        1.98            1.17   0.622           0.669    
## 394 MANA       4        1.30            2.48   0.689           0.116    
## 395 BTG        4        2.85            0.895  0.0103          0.944    
## 396 SUN        4        1.07            2.14   0.852           0.305    
## 397 BAT        4        1.13            1.16   0.649           0.906    
## 398 CHZ        4        1.74            3.09   0.692           0.0657   
## 399 DCR        4        7.96            2.55   0.106           0.178    
## 400 SPC        4        2.64            2.01   0.106           0.208    
## 401 CKB        4        7.45            2.72   0.394           0.603    
## 402 STRAT      3        1.70            6.45   0.785           0.587    
## 403 LEVL       4        5.04            3.63   0.294           0.184    
## 404 SMART      4        1.11            2.21   0.722           0.469    
## 405 DOGE       4        1.70            2.16   0.569           0.750    
## 406 BMC        3        1.14            7.81   0.140           0.350    
## 407 DDR        4        3.99            9.15   0.553           0.0140   
## 408 SEELE      4        4.21            1.81   0.172           0.224    
## 409 RCN        4       11.7             4.95   0.377           0.566    
## 410 IQ         4        5.17            2.87   0.0103          0.115    
## 411 APPC       4       13.4            10.3    0.00330         0.212    
## 412 SWRV       4      NaN               9.98  NA               0.000750 
## 413 POA        4        6.56            7.86   0.0652          0.227    
## 414 VET        4        1.93            2.54   0.000782        0.428    
## 415 DDRT       4        3.36            6.11   0.259           0.0176   
## 416 IQN        4        0.575           0.262  0.456           0.300    
## 417 NXT        4        1.20            4.03   0.904           0.227    
## 418 CLO        4        3.37            2.92   0.152           0.551    
## 419 EVX        4       13.2            11.9    0.109           0.0130   
## 420 AMB        4        2.29            6.19   0.0839          0.103    
## 421 ZIL        4        1.18            1.93   0.509           0.0510   
## 422 UMA        4        0.812           1.97   0.731           0.338    
## 423 DASH       4        1.12            2.13   0.677           0.0825   
## 424 GASP       4        1.13            1.66   0.776           0.667    
## 425 ETC        4        1.78            0.886  0.924           0.888    
## 426 CVC        4        0.784           1.89   0.736           0.259    
## 427 MKR        4        1.72            3.29   0.587           0.352    
## 428 BZRX       4        1.80            3.93   0.248           0.0000739
## 429 SXP        4        1.95            3.11   0.366           0.197    
## 430 STX        4        5.08            2.23   0.0613          0.858    
## 431 BNB        4        1.28            2.53   0.106           0.115    
## 432 PPC        4        2.29            4.55   0.00238         0.175    
## 433 NEAR       4        1.18            3.00   0.261           0.0121   
## 434 COTI       4        0.781           4.37   0.672           0.00787  
## 435 NEO        4        1.61            1.80   0.675           0.149    
## 436 TOMO       4        0.975           3.40   0.832           0.00520  
## 437 BCH        4        1.01            1.55   0.856           0.711    
## 438 ETN        4        2.30            4.68   0.256           0.191    
## 439 IOST       4        0.718           1.13   0.616           0.589    
## 440 BCD        4        1.68            3.11   0.204           0.531    
## 441 FUN        4        2.16            1.54   0.602           0.000472 
## 442 ICX        4        1.05            3.60   0.790           0.125    
## 443 BYTZ       4        2.77            4.17   0.00713         0.0396   
## 444 FIL        4        0.828           5.47   0.591           0.431    
## 445 SNX        4        0.265           3.76   0.905           0.553    
## 446 SNC        4        1.65            8.04   0.0349          0.0154   
## 447 XVG        4        1.07            2.32   0.919           0.401    
## 448 MAID       4        0.503           2.81   0.881           0.546    
## 449 XTZ        4        0.717           1.56   0.865           0.144    
## 450 GOLDR      4        1.90            5.30   0.902           0.0385   
## 451 UNO        4        2.26            0.653  0.504           0.653    
## 452 XUC        4        1.38            1.17   0.404           0.669    
## 453 BMC        4        3.65            7.81   0.000228        0.350    
## 454 GLM        4        1.24            1.31   0.668           0.565    
## 455 DGTX       4        2.38            7.30   0.299           0.289    
## 456 STRAT      4        1.83            6.45   0.688           0.587    
## 457 CND        5       NA               3.13  NA               0.587    
## 458 OAX        5       NA               6.12  NA               0.206    
## 459 SRN        5       NA               7.41  NA               0.0315   
## 460 VIB        5       NA               7.09  NA               0.201    
## 461 ACT        5       NA               5.41  NA               0.482    
## 462 SUB        5       NA               9.13  NA               0.254    
## 463 SBD        5       NA               1.21  NA               0.387    
## 464 NAV        5       NA               1.93  NA               0.563    
## 465 CCE        5       NA               2.52  NA               0.0160   
## 466 BCHA       5       NA               1.36  NA               0.671    
## 467 BRD        5       NA               6.28  NA               0.0131   
## 468 HUB        5       NA               4.01  NA               0.403    
## 469 POA        5       NA               7.86  NA               0.227    
## 470 LTC        5       NA               2.01  NA               0.757    
## 471 HT         5       NA               3.40  NA               0.347    
## 472 ZEC        5       NA               2.19  NA               0.743    
## 473 XMR        5       NA               1.49  NA               0.846    
## 474 ZRX        5       NA               2.12  NA               0.759    
## 475 DENT       5       NA               2.84  NA               0.700    
## 476 ASP        5       NA               2.90  NA               0.622    
## 477 REX        5       NA               5.94  NA               0.309    
## 478 SUSHI      5       NA               1.94  NA               0.608    
## 479 FTM        5       NA               2.68  NA               0.422    
## 480 INJ        5       NA               2.25  NA               0.657    
## 481 THETA      5       NA               2.58  NA               0.774    
## 482 KNC        5       NA               2.38  NA               0.644    
## 483 BAT        5       NA               1.16  NA               0.906    
## 484 KMD        5       NA               1.87  NA               0.871    
## 485 ELF        5       NA               5.80  NA               0.360    
## 486 CBC        5       NA               2.32  NA               0.326    
## 487 RIF        5       NA               3.78  NA               0.409    
## 488 UNI        5       NA               1.90  NA               0.361    
## 489 ETH        5       NA               2.35  NA               0.0665   
## 490 BNT        5       NA               2.70  NA               0.174    
## 491 CRO        5       NA               1.27  NA               0.744    
## 492 ARDR       5       NA               2.02  NA               0.775    
## 493 CRV        5       NA               1.77  NA               0.840    
## 494 BTC        5       NA               1.82  NA               0.758    
## 495 EOS        5       NA               2.31  NA               0.681    
## 496 ADA        5       NA               1.76  NA               0.737    
## 497 BSV        5       NA               1.56  NA               0.885    
## 498 TRX        5       NA               1.24  NA               0.789    
## 499 ENJ        5       NA               2.62  NA               0.258    
## 500 BTM        5       NA               1.00  NA               0.845    
## # ... with 70 more rows

Out of 570 groups, 142 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: 570 x 6
## # Groups:   symbol, split [570]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        0.866       0.512            1.82         0.758 
##  2 ETH        1        1.64        0.404            2.35         0.0665
##  3 EOS        1        0.690       0.794            2.31         0.681 
##  4 LTC        1        1.04        0.576            2.01         0.757 
##  5 ADA        1        1.80        0.724            1.76         0.737 
##  6 BSV        1        1.64        0.168            1.56         0.885 
##  7 ZEC        1        0.511       0.923            2.19         0.743 
##  8 HT         1        2.57        0.390            3.40         0.347 
##  9 TRX        1        1.91        0.213            1.24         0.789 
## 10 KNC        1        1.02        0.710            2.38         0.644 
## # ... with 560 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: 570 x 3
## # Groups:   symbol [114]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.477
##  2 ETH    lm    0.892
##  3 EOS    lm    1.48 
##  4 LTC    lm    1.14 
##  5 ADA    lm    1.58 
##  6 BSV    lm    1.15 
##  7 ZEC    lm    0.976
##  8 HT     lm    1.39 
##  9 TRX    lm    2.49 
## 10 KNC    lm    1.56 
## # ... with 560 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: 570 x 3
## # Groups:   symbol [114]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm     1.82
##  2 ETH    lm     2.35
##  3 EOS    lm     2.31
##  4 LTC    lm     2.01
##  5 ADA    lm     1.76
##  6 BSV    lm     1.56
##  7 ZEC    lm     2.19
##  8 HT     lm     3.40
##  9 TRX    lm     1.24
## 10 KNC    lm     2.38
## # ... with 560 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: 570 x 3
## # Groups:   symbol [114]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.738
##  2 ETH    lm    0.647
##  3 EOS    lm    0.489
##  4 LTC    lm    0.445
##  5 ADA    lm    0.510
##  6 BSV    lm    0.572
##  7 ZEC    lm    0.789
##  8 HT     lm    0.358
##  9 TRX    lm    0.470
## 10 KNC    lm    0.683
## # ... with 560 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: 570 x 3
## # Groups:   symbol [114]
##    symbol model    rsq
##    <chr>  <chr>  <dbl>
##  1 BTC    lm    0.758 
##  2 ETH    lm    0.0665
##  3 EOS    lm    0.681 
##  4 LTC    lm    0.757 
##  5 ADA    lm    0.737 
##  6 BSV    lm    0.885 
##  7 ZEC    lm    0.743 
##  8 HT     lm    0.347 
##  9 TRX    lm    0.789 
## 10 KNC    lm    0.644 
## # ... with 560 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.