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 
## 1079.4357965    0.2367524 1034.1500793

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.01068421         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.375074

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: 405 x 4
## # Groups:   symbol, split [405]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1      0.783         0.0197 
##  2 ETH        1      0.797         0.0319 
##  3 EOS        1      0.243         0.684  
##  4 LTC        1      0.781         0.548  
##  5 BSV        1      0.689         0.116  
##  6 ADA        1      0.226         0.0126 
##  7 ZEC        1      0.126         0.774  
##  8 HT         1      0.0253        0.467  
##  9 TRX        1      0.218         0.00784
## 10 KNC        1      0.413         0.204  
## # ... with 395 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: 405 x 6
## # Groups:   symbol, split [405]
##     symbol split lm_rmse_test lm_rmse_holdout   lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>         <dbl>          <dbl>
##   1 BTC        1      1.38            3.80     0.783             0.0197   
##   2 ETH        1      0.717           3.40     0.797             0.0319   
##   3 EOS        1      1.91            1.65     0.243             0.684    
##   4 LTC        1      0.871           2.74     0.781             0.548    
##   5 BSV        1      0.388           4.93     0.689             0.116    
##   6 ADA        1      0.670           2.67     0.226             0.0126   
##   7 ZEC        1      0.913           1.92     0.126             0.774    
##   8 HT         1      1.08           13.8      0.0253            0.467    
##   9 TRX        1      0.427           2.25     0.218             0.00784  
##  10 KNC        1      0.790           7.93     0.413             0.204    
##  11 XMR        1      0.900           2.05     0.348             0.562    
##  12 ZRX        1      0.542           4.24     0.669             0.0654   
##  13 BAT        1      0.822           1.15     0.348             0.854    
##  14 BNT        1      1.35            3.92     0.170             0.0116   
##  15 MANA       1      1.08            4.64     0.0265            0.0276   
##  16 ENJ        1      0.955           4.35     0.0299            0.0196   
##  17 XEM        1      0.658           3.63     0.497             0.0000240
##  18 BTG        1      0.951           2.34     0.495             0.349    
##  19 ARDR       1      1.49          150.       0.326             0.166    
##  20 KMD        1      1.11            5.39     0.516             0.286    
##  21 NEXO       1      0.917           3.92     0.150             0.0308   
##  22 CHZ        1      1.40            4.85     0.00000503        0.391    
##  23 DCR        1      1.81            3.46     0.230             0.00918  
##  24 VIB        1      7.80            2.73     0.111             0.583    
##  25 WAXP       1      1.45            5.43     0.0977            0.00768  
##  26 AVA        1      1.44            3.12     0.000481          0.284    
##  27 GMTT       1      1.90            7.02     0.582             0.127    
##  28 ABBC       1      1.70            4.33     0.0531            0.0302   
##  29 CROOLD     1      1.54            2.71     0.102             0.450    
##  30 GAS        1      2.50            2.56     0.753             0.134    
##  31 DODO       1     10.2             4.71     0.0268            0.640    
##  32 DYDX       1      2.02            3.24     0.00581           0.265    
##  33 1INCH      1      1.28            1.87     0.0306            0.129    
##  34 BAKE       1      1.16            3.34     0.468             0.451    
##  35 SKL        1      3.00           14.7      0.349             0.0106   
##  36 DOT        1      0.813           2.09     0.162             0.401    
##  37 LINA       1      0.847           3.32     0.468             0.265    
##  38 FIDA       1     10.1             5.43     0.000183          0.857    
##  39 QTUM       1      0.893           4.15     0.0824            0.0457   
##  40 ATA        1      8.08          NaN        0.0779           NA        
##  41 IOTA       1      0.887           2.97     0.0959            0.0132   
##  42 NEAR       1      0.717           5.21     0.628             0.0568   
##  43 AMB        1      7.22            3.64     0.123             0.339    
##  44 LSK        1      1.22            4.49     0.00283           0.154    
##  45 PERP       1      1.40            4.81     0.0241            0.122    
##  46 RIF        1      1.07            5.04     0.657             0.0459   
##  47 UMA        1      1.52            1.63     0.147             0.475    
##  48 ATOM       1      1.01            3.15     0.735             0.147    
##  49 ALICE      1      0.821           4.64     0.166             0.0306   
##  50 MTLX       1      0.816           0.766    0.241             0.837    
##  51 PLI        1      0.648           1.47     0.255             0.900    
##  52 XDC        1      0.380           0.784    0.549             0.690    
##  53 MC         1     19.5             4.26     0.236             0.0604   
##  54 BDX        1      0.491           0.564    0.0576            0.794    
##  55 TRIBE      1      8.49           10.9      0.284             0.467    
##  56 SNX        1      2.20            2.75     0.116             0.609    
##  57 USG        1      1.11            0.00270  0.000714          0.345    
##  58 THETA      1      1.19            1.81     0.0280            0.563    
##  59 RUNE       1      1.75            4.35     0.000507          0.638    
##  60 KLAY       1      1.51            2.56     0.135             0.432    
##  61 XCH        1      0.723           1.44     0.633             0.696    
##  62 BAL        1      2.51            1.92     0.0994            0.342    
##  63 REN        1      1.19            4.72     0.175             0.000407 
##  64 VGX        1      0.326           9.14     0.863             0.649    
##  65 SAND       1      1.08            4.64     0.0735            0.0297   
##  66 ANKR       1      0.757           3.17     0.538             0.314    
##  67 BCH        1      0.572           1.87     0.638             0.363    
##  68 FTT        1      0.830         103.       0.572             0.0463   
##  69 GMT        1      1.40            3.11     0.636             0.638    
##  70 MIR        1      3.16            3.76     0.306             0.118    
##  71 T          1      3.59            7.33     0.0174            0.0409   
##  72 DASH       1      1.37            2.52     0.0379            0.642    
##  73 SXP        1      1.19            6.61     0.00387           0.171    
##  74 SFP        1      2.22            1.92     0.0106            0.642    
##  75 BICO       1      1.19            1.42     0.0744            0.715    
##  76 EURS       1      0.237           0.0103   0.543            NA        
##  77 NMR        1      0.807           3.80     0.284             0.0000804
##  78 CELR       1      5.81            3.98     0.986             0.178    
##  79 ICP        1      0.845           3.41     0.687             0.00996  
##  80 COCOS      1      2.51            1.24     0.920             0.834    
##  81 VGX        2      5.83            9.14     0.0582            0.649    
##  82 BNT        2      0.927           3.92     0.656             0.0116   
##  83 ATA        2      9.06          NaN        0.000473         NA        
##  84 VIB        2      5.78            2.73     0.0205            0.583    
##  85 EURS       2      0.274           0.0103   0.424            NA        
##  86 AMB        2      0.914           3.64     0.308             0.339    
##  87 CELR       2      3.29            3.98     0.00663           0.178    
##  88 FIDA       2      0.703           5.43     0.000814          0.857    
##  89 UMA        2      4.97            1.63     0.695             0.475    
##  90 USG        2      0.00436         0.00270  0.184             0.345    
##  91 XCH        2      0.632           1.44     0.0310            0.696    
##  92 XMR        2      0.880           2.05     0.469             0.562    
##  93 MC         2      8.35            4.26     0.155             0.0604   
##  94 SKL        2      1.41           14.7      0.398             0.0106   
##  95 KMD        2      0.796           5.39     0.428             0.286    
##  96 HT         2      0.544          13.8      0.291             0.467    
##  97 GMTT       2      1.71            7.02     0.347             0.127    
##  98 BICO       2      0.767           1.42     0.334             0.715    
##  99 XEM        2      0.728           3.63     0.330             0.0000240
## 100 BTG        2      0.709           2.34     0.563             0.349    
## 101 BAL        2      0.752           1.92     0.0866            0.342    
## 102 BTC        2      1.06            3.80     0.0640            0.0197   
## 103 ETH        2      0.981           3.40     0.187             0.0319   
## 104 EOS        2      0.930           1.65     0.186             0.684    
## 105 LTC        2      0.470           2.74     0.580             0.548    
## 106 BSV        2      0.593           4.93     0.597             0.116    
## 107 ADA        2      0.555           2.67     0.711             0.0126   
## 108 ZEC        2      1.03            1.92     0.597             0.774    
## 109 TRX        2      0.409           2.25     0.159             0.00784  
## 110 KNC        2      0.608           7.93     0.490             0.204    
## 111 ZRX        2      0.591           4.24     0.477             0.0654   
## 112 BAT        2      0.451           1.15     0.542             0.854    
## 113 MANA       2      0.541           4.64     0.161             0.0276   
## 114 ENJ        2      0.584           4.35     0.417             0.0196   
## 115 ARDR       2      3.19          150.       0.00278           0.166    
## 116 NEXO       2      0.768           3.92     0.320             0.0308   
## 117 CHZ        2      0.847           4.85     0.888             0.391    
## 118 DCR        2      1.29            3.46     0.171             0.00918  
## 119 AVA        2      0.676           3.12     0.461             0.284    
## 120 CROOLD     2      0.737           2.71     0.662             0.450    
## 121 DODO       2      0.643           4.71     0.292             0.640    
## 122 DYDX       2      0.855           3.24     0.571             0.265    
## 123 1INCH      2      0.956           1.87     0.626             0.129    
## 124 BAKE       2      1.38            3.34     0.0595            0.451    
## 125 DOT        2      0.563           2.09     0.643             0.401    
## 126 LINA       2      1.02            3.32     0.0430            0.265    
## 127 QTUM       2      0.975           4.15     0.134             0.0457   
## 128 IOTA       2      0.618           2.97     0.783             0.0132   
## 129 PERP       2      1.42            4.81     0.183             0.122    
## 130 RIF        2      0.775           5.04     0.214             0.0459   
## 131 PLI        2      1.03            1.47     0.370             0.900    
## 132 KLAY       2      0.671           2.56     0.461             0.432    
## 133 SAND       2      0.762           4.64     0.134             0.0297   
## 134 ANKR       2      0.650           3.17     0.321             0.314    
## 135 ICP        2      0.941           3.41     0.233             0.00996  
## 136 NEAR       2      0.614           5.21     0.571             0.0568   
## 137 LSK        2      0.450           4.49     0.628             0.154    
## 138 ATOM       2      0.599           3.15     0.893             0.147    
## 139 ALICE      2      0.655           4.64     0.288             0.0306   
## 140 MTLX       2      0.627           0.766    0.658             0.837    
## 141 XDC        2      0.236           0.784    0.855             0.690    
## 142 BDX        2      0.773           0.564    0.0504            0.794    
## 143 SNX        2      0.682           2.75     0.473             0.609    
## 144 TRIBE      2      0.987          10.9      0.142             0.467    
## 145 THETA      2      0.604           1.81     0.520             0.563    
## 146 RUNE       2      0.594           4.35     0.419             0.638    
## 147 REN        2      0.887           4.72     0.311             0.000407 
## 148 FTT        2      0.959         103.       0.0798            0.0463   
## 149 BCH        2      0.671           1.87     0.439             0.363    
## 150 GMT        2      2.28            3.11     0.360             0.638    
## 151 SXP        2      0.875           6.61     0.248             0.171    
## 152 SFP        2      2.49            1.92     0.0489            0.642    
## 153 NMR        2      0.496           3.80     0.352             0.0000804
## 154 WAXP       2      0.441           5.43     0.248             0.00768  
## 155 GAS        2      0.669           2.56     0.0411            0.134    
## 156 T          2      0.425           7.33     0.00291           0.0409   
## 157 DASH       2      0.493           2.52     0.249             0.642    
## 158 ETHW       1     31.7             7.57     0.571             0.530    
## 159 ABBC       2      0.629           4.33     0.00525           0.0302   
## 160 MIR        2      0.657           3.76     0.637             0.118    
## 161 COCOS      2      1.57            1.24     1                 0.834    
## 162 VGX        3     21.1             9.14     0.00809           0.649    
## 163 ATA        3      8.32          NaN        0.725            NA        
## 164 EURS       3      0.137           0.0103   0.759            NA        
## 165 VIB        3      3.03            2.73     0.411             0.583    
## 166 ETHW       2      3.14            7.57     0.778             0.530    
## 167 FIDA       3     25.6             5.43     0.254             0.857    
## 168 UMA        3      1.14            1.63     0.128             0.475    
## 169 USG        3      0.0553          0.00270  0.888             0.345    
## 170 AMB        3      3.67            3.64     0.757             0.339    
## 171 GMTT       3      0.797           7.02     0.131             0.127    
## 172 SKL        3      7.81           14.7      0.702             0.0106   
## 173 MC         3     13.8             4.26     0.458             0.0604   
## 174 BICO       3      1.02            1.42     0.0450            0.715    
## 175 KMD        3      0.824           5.39     0.601             0.286    
## 176 XMR        3      0.362           2.05     0.461             0.562    
## 177 XCH        3      0.559           1.44     0.625             0.696    
## 178 BAL        3      0.495           1.92     0.527             0.342    
## 179 BTG        3      2.29            2.34     0.00386           0.349    
## 180 TRX        3      0.646           2.25     0.0621            0.00784  
## 181 XEM        3      0.698           3.63     0.122             0.0000240
## 182 PERP       3      0.834           4.81     0.183             0.122    
## 183 PLI        3      0.661           1.47     0.476             0.900    
## 184 SAND       3      0.432           4.64     0.759             0.0297   
## 185 BTC        3      0.360           3.80     0.807             0.0197   
## 186 ETH        3      0.480           3.40     0.719             0.0319   
## 187 EOS        3      1.39            1.65     0.109             0.684    
## 188 LTC        3      0.502           2.74     0.503             0.548    
## 189 BSV        3      0.613           4.93     0.576             0.116    
## 190 ADA        3      0.907           2.67     0.585             0.0126   
## 191 ZEC        3      0.419           1.92     0.735             0.774    
## 192 KNC        3      2.67            7.93     0.596             0.204    
## 193 ZRX        3      1.36            4.24     0.0623            0.0654   
## 194 BAT        3      0.329           1.15     0.908             0.854    
## 195 MANA       3      0.566           4.64     0.582             0.0276   
## 196 ENJ        3      0.434           4.35     0.716             0.0196   
## 197 ARDR       3     13.0           150.       0.516             0.166    
## 198 NEXO       3      0.533           3.92     0.658             0.0308   
## 199 CHZ        3      0.855           4.85     0.538             0.391    
## 200 DCR        3      0.458           3.46     0.659             0.00918  
## 201 AVA        3      0.786           3.12     0.261             0.284    
## 202 CROOLD     3      1.17            2.71     0.0325            0.450    
## 203 DODO       3      0.954           4.71     0.338             0.640    
## 204 DYDX       3      1.13            3.24     0.888             0.265    
## 205 1INCH      3      0.401           1.87     0.482             0.129    
## 206 BAKE       3      0.536           3.34     0.477             0.451    
## 207 DOT        3      0.690           2.09     0.187             0.401    
## 208 LINA       3      0.754           3.32     0.270             0.265    
## 209 QTUM       3      0.644           4.15     0.454             0.0457   
## 210 IOTA       3      0.653           2.97     0.193             0.0132   
## 211 NEAR       3      0.722           5.21     0.653             0.0568   
## 212 RIF        3      0.521           5.04     0.371             0.0459   
## 213 ATOM       3      1.16            3.15     0.102             0.147    
## 214 ALICE      3      0.931           4.64     0.404             0.0306   
## 215 MTLX       3      1.44            0.766    0.414             0.837    
## 216 XDC        3      0.319           0.784    0.624             0.690    
## 217 BDX        3      0.507           0.564    0.390             0.794    
## 218 TRIBE      3      0.391          10.9      0.586             0.467    
## 219 SNX        3      0.713           2.75     0.717             0.609    
## 220 THETA      3      0.538           1.81     0.516             0.563    
## 221 RUNE       3      0.606           4.35     0.584             0.638    
## 222 KLAY       3      0.679           2.56     0.874             0.432    
## 223 ANKR       3      1.40            3.17     0.294             0.314    
## 224 BCH        3      0.353           1.87     0.791             0.363    
## 225 FTT        3      0.316         103.       0.803             0.0463   
## 226 GMT        3      1.77            3.11     0.297             0.638    
## 227 SXP        3      1.25            6.61     0.498             0.171    
## 228 SFP        3      4.64            1.92     0.537             0.642    
## 229 NMR        3      2.15            3.80     0.0276            0.0000804
## 230 ICP        3      5.65            3.41     0.140             0.00996  
## 231 REN        3      0.574           4.72     0.551             0.000407 
## 232 T          3      1.58            7.33     0.0115            0.0409   
## 233 WAXP       3      1.06            5.43     0.0203            0.00768  
## 234 GAS        3      0.887           2.56     0.0294            0.134    
## 235 LSK        3      0.908           4.49     0.459             0.154    
## 236 HT         3      2.92           13.8      0.807             0.467    
## 237 ABBC       3      2.44            4.33     0.0000000262      0.0302   
## 238 DASH       3      0.540           2.52     0.421             0.642    
## 239 MIR        3      2.25            3.76     0.290             0.118    
## 240 VGX        4    NaN               9.14    NA                 0.649    
## 241 CELR       3      1.47            3.98     0.540             0.178    
## 242 COCOS      3      1.94            1.24     0.144             0.834    
## 243 ATA        4      6.23          NaN        0.0919           NA        
## 244 BNT        3      1.07            3.92     0.181             0.0116   
## 245 ETHW       3      1.18            7.57     0.728             0.530    
## 246 EURS       4      0.111           0.0103   0.702            NA        
## 247 USG        4      0.0137          0.00270  0.870             0.345    
## 248 AMB        4      1.25            3.64     0.303             0.339    
## 249 SKL        4      0.819          14.7      0.0166            0.0106   
## 250 BICO       4      0.714           1.42     0.245             0.715    
## 251 KMD        4      0.379           5.39     0.336             0.286    
## 252 FIDA       4      1.93            5.43     0.149             0.857    
## 253 GMTT       4      5.70            7.02     0.111             0.127    
## 254 XMR        4      0.215           2.05     0.782             0.562    
## 255 BAL        4      0.613           1.92     0.872             0.342    
## 256 BTG        4      3.40            2.34     0.0590            0.349    
## 257 XEM        4      0.250           3.63     0.920             0.0000240
## 258 PERP       4      0.797           4.81     0.166             0.122    
## 259 PLI        4      2.02            1.47     0.295             0.900    
## 260 SAND       4      0.345           4.64     0.811             0.0297   
## 261 BTC        4      0.515           3.80     0.691             0.0197   
## 262 ETH        4      1.37            3.40     0.892             0.0319   
## 263 EOS        4      0.886           1.65     0.560             0.684    
## 264 LTC        4      0.932           2.74     0.405             0.548    
## 265 BSV        4      0.392           4.93     0.406             0.116    
## 266 ADA        4      0.676           2.67     0.781             0.0126   
## 267 ZEC        4      0.553           1.92     0.153             0.774    
## 268 TRX        4      0.361           2.25     0.308             0.00784  
## 269 KNC        4      0.314           7.93     0.890             0.204    
## 270 ZRX        4      0.524           4.24     0.634             0.0654   
## 271 BAT        4      0.453           1.15     0.711             0.854    
## 272 MANA       4      0.425           4.64     0.686             0.0276   
## 273 ENJ        4      0.415           4.35     0.743             0.0196   
## 274 ARDR       4      0.938         150.       0.00335           0.166    
## 275 NEXO       4      0.405           3.92     0.848             0.0308   
## 276 CHZ        4      1.05            4.85     0.526             0.391    
## 277 DCR        4      0.537           3.46     0.579             0.00918  
## 278 AVA        4      0.313           3.12     0.730             0.284    
## 279 CROOLD     4      0.995           2.71     0.643             0.450    
## 280 DODO       4      0.861           4.71     0.00156           0.640    
## 281 DYDX       4      1.15            3.24     0.535             0.265    
## 282 1INCH      4      0.563           1.87     0.585             0.129    
## 283 BAKE       4      0.531           3.34     0.442             0.451    
## 284 DOT        4      0.598           2.09     0.750             0.401    
## 285 LINA       4      0.609           3.32     0.619             0.265    
## 286 QTUM       4      0.654           4.15     0.322             0.0457   
## 287 IOTA       4      0.496           2.97     0.261             0.0132   
## 288 RIF        4      0.764           5.04     0.176             0.0459   
## 289 UMA        4      0.364           1.63     0.527             0.475    
## 290 KLAY       4      3.31            2.56     0.256             0.432    
## 291 ANKR       4      0.370           3.17     0.654             0.314    
## 292 ICP        4      0.574           3.41     0.611             0.00996  
## 293 NEAR       4      0.523           5.21     0.540             0.0568   
## 294 ATOM       4      0.864           3.15     0.592             0.147    
## 295 ALICE      4      0.394           4.64     0.819             0.0306   
## 296 MTLX       4      5.13            0.766    0.182             0.837    
## 297 XDC        4      0.194           0.784    0.847             0.690    
## 298 BDX        4      0.274           0.564    0.802             0.794    
## 299 TRIBE      4      2.16           10.9      0.508             0.467    
## 300 SNX        4      0.609           2.75     0.812             0.609    
## 301 THETA      4      0.449           1.81     0.739             0.563    
## 302 RUNE       4      0.529           4.35     0.664             0.638    
## 303 FTT        4      0.332         103.       0.866             0.0463   
## 304 BCH        4      0.500           1.87     0.727             0.363    
## 305 GMT        4      0.500           3.11     0.852             0.638    
## 306 SXP        4      0.493           6.61     0.692             0.171    
## 307 SFP        4      0.629           1.92     0.427             0.642    
## 308 NMR        4      0.528           3.80     0.00115           0.0000804
## 309 GAS        4      5.52            2.56     0.00505           0.134    
## 310 REN        4      0.906           4.72     0.388             0.000407 
## 311 LSK        4      0.702           4.49     0.0604            0.154    
## 312 XCH        4      0.453           1.44     0.112             0.696    
## 313 HT         4      1.83           13.8      0.518             0.467    
## 314 T          4      0.285           7.33     0.800             0.0409   
## 315 DASH       4      0.716           2.52     0.431             0.642    
## 316 WAXP       4      0.333           5.43     0.830             0.00768  
## 317 MIR        4      0.463           3.76     0.686             0.118    
## 318 ABBC       4      1.78            4.33     0.647             0.0302   
## 319 MC         4      1.38            4.26     0.670             0.0604   
## 320 VIB        4      1.07            2.73     0.934             0.583    
## 321 CELR       4      0.875           3.98     0.122             0.178    
## 322 COCOS      4      0.451           1.24     0.0182            0.834    
## 323 ATA        5     NA             NaN       NA                NA        
## 324 VGX        5     NA               9.14    NA                 0.649    
## 325 ETHW       4      0.880           7.57     0.625             0.530    
## 326 BNT        4      1.43            3.92     0.0101            0.0116   
## 327 EURS       5     NA               0.0103  NA                NA        
## 328 USG        5     NA               0.00270 NA                 0.345    
## 329 SKL        5     NA              14.7     NA                 0.0106   
## 330 XMR        5     NA               2.05    NA                 0.562    
## 331 BAL        5     NA               1.92    NA                 0.342    
## 332 BICO       5     NA               1.42    NA                 0.715    
## 333 KMD        5     NA               5.39    NA                 0.286    
## 334 XCH        5     NA               1.44    NA                 0.696    
## 335 XEM        5     NA               3.63    NA                 0.0000240
## 336 BTG        5     NA               2.34    NA                 0.349    
## 337 TRX        5     NA               2.25    NA                 0.00784  
## 338 KLAY       5     NA               2.56    NA                 0.432    
## 339 ANKR       5     NA               3.17    NA                 0.314    
## 340 ICP        5     NA               3.41    NA                 0.00996  
## 341 BTC        5     NA               3.80    NA                 0.0197   
## 342 ETH        5     NA               3.40    NA                 0.0319   
## 343 LTC        5     NA               2.74    NA                 0.548    
## 344 BSV        5     NA               4.93    NA                 0.116    
## 345 ADA        5     NA               2.67    NA                 0.0126   
## 346 ZEC        5     NA               1.92    NA                 0.774    
## 347 KNC        5     NA               7.93    NA                 0.204    
## 348 ZRX        5     NA               4.24    NA                 0.0654   
## 349 BAT        5     NA               1.15    NA                 0.854    
## 350 MANA       5     NA               4.64    NA                 0.0276   
## 351 ENJ        5     NA               4.35    NA                 0.0196   
## 352 ARDR       5     NA             150.      NA                 0.166    
## 353 NEXO       5     NA               3.92    NA                 0.0308   
## 354 CHZ        5     NA               4.85    NA                 0.391    
## 355 DCR        5     NA               3.46    NA                 0.00918  
## 356 AVA        5     NA               3.12    NA                 0.284    
## 357 CROOLD     5     NA               2.71    NA                 0.450    
## 358 DODO       5     NA               4.71    NA                 0.640    
## 359 DYDX       5     NA               3.24    NA                 0.265    
## 360 1INCH      5     NA               1.87    NA                 0.129    
## 361 BAKE       5     NA               3.34    NA                 0.451    
## 362 DOT        5     NA               2.09    NA                 0.401    
## 363 LINA       5     NA               3.32    NA                 0.265    
## 364 QTUM       5     NA               4.15    NA                 0.0457   
## 365 IOTA       5     NA               2.97    NA                 0.0132   
## 366 RIF        5     NA               5.04    NA                 0.0459   
## 367 UMA        5     NA               1.63    NA                 0.475    
## 368 PLI        5     NA               1.47    NA                 0.900    
## 369 EOS        5     NA               1.65    NA                 0.684    
## 370 NEAR       5     NA               5.21    NA                 0.0568   
## 371 ATOM       5     NA               3.15    NA                 0.147    
## 372 ALICE      5     NA               4.64    NA                 0.0306   
## 373 MTLX       5     NA               0.766   NA                 0.837    
## 374 XDC        5     NA               0.784   NA                 0.690    
## 375 BDX        5     NA               0.564   NA                 0.794    
## 376 SNX        5     NA               2.75    NA                 0.609    
## 377 TRIBE      5     NA              10.9     NA                 0.467    
## 378 THETA      5     NA               1.81    NA                 0.563    
## 379 RUNE       5     NA               4.35    NA                 0.638    
## 380 SAND       5     NA               4.64    NA                 0.0297   
## 381 FTT        5     NA             103.      NA                 0.0463   
## 382 BCH        5     NA               1.87    NA                 0.363    
## 383 GMT        5     NA               3.11    NA                 0.638    
## 384 SXP        5     NA               6.61    NA                 0.171    
## 385 SFP        5     NA               1.92    NA                 0.642    
## 386 NMR        5     NA               3.80    NA                 0.0000804
## 387 HT         5     NA              13.8     NA                 0.467    
## 388 LSK        5     NA               4.49    NA                 0.154    
## 389 PERP       5     NA               4.81    NA                 0.122    
## 390 REN        5     NA               4.72    NA                 0.000407 
## 391 T          5     NA               7.33    NA                 0.0409   
## 392 GAS        5     NA               2.56    NA                 0.134    
## 393 MIR        5     NA               3.76    NA                 0.118    
## 394 WAXP       5     NA               5.43    NA                 0.00768  
## 395 DASH       5     NA               2.52    NA                 0.642    
## 396 CELR       5     NA               3.98    NA                 0.178    
## 397 GMTT       5     NA               7.02    NA                 0.127    
## 398 MC         5     NA               4.26    NA                 0.0604   
## 399 FIDA       5     NA               5.43    NA                 0.857    
## 400 AMB        5     NA               3.64    NA                 0.339    
## 401 COCOS      5     NA               1.24    NA                 0.834    
## 402 VIB        5     NA               2.73    NA                 0.583    
## 403 ABBC       5     NA               4.33    NA                 0.0302   
## 404 ETHW       5     NA               7.57    NA                 0.530    
## 405 BNT        5     NA               3.92    NA                 0.0116

Out of 405 groups, 40 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: 405 x 6
## # Groups:   symbol, split [405]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        1.38       0.783             3.80        0.0197 
##  2 ETH        1        0.717      0.797             3.40        0.0319 
##  3 EOS        1        1.91       0.243             1.65        0.684  
##  4 LTC        1        0.871      0.781             2.74        0.548  
##  5 BSV        1        0.388      0.689             4.93        0.116  
##  6 ADA        1        0.670      0.226             2.67        0.0126 
##  7 ZEC        1        0.913      0.126             1.92        0.774  
##  8 HT         1        1.08       0.0253           13.8         0.467  
##  9 TRX        1        0.427      0.218             2.25        0.00784
## 10 KNC        1        0.790      0.413             7.93        0.204  
## # ... with 395 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: 405 x 3
## # Groups:   symbol [81]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.828
##  2 ETH    lm    0.887
##  3 EOS    lm    1.28 
##  4 LTC    lm    0.694
##  5 BSV    lm    0.496
##  6 ADA    lm    0.702
##  7 ZEC    lm    0.729
##  8 HT     lm    1.59 
##  9 TRX    lm    0.461
## 10 KNC    lm    1.10 
## # ... with 395 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: 405 x 3
## # Groups:   symbol [81]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm     3.80
##  2 ETH    lm     3.40
##  3 EOS    lm     1.65
##  4 LTC    lm     2.74
##  5 BSV    lm     4.93
##  6 ADA    lm     2.67
##  7 ZEC    lm     1.92
##  8 HT     lm    13.8 
##  9 TRX    lm     2.25
## 10 KNC    lm     7.93
## # ... with 395 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: 405 x 3
## # Groups:   symbol [81]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.586
##  2 ETH    lm    0.649
##  3 EOS    lm    0.275
##  4 LTC    lm    0.567
##  5 BSV    lm    0.567
##  6 ADA    lm    0.576
##  7 ZEC    lm    0.403
##  8 HT     lm    0.411
##  9 TRX    lm    0.187
## 10 KNC    lm    0.597
## # ... with 395 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: 405 x 3
## # Groups:   symbol [81]
##    symbol model     rsq
##    <chr>  <chr>   <dbl>
##  1 BTC    lm    0.0197 
##  2 ETH    lm    0.0319 
##  3 EOS    lm    0.684  
##  4 LTC    lm    0.548  
##  5 BSV    lm    0.116  
##  6 ADA    lm    0.0126 
##  7 ZEC    lm    0.774  
##  8 HT     lm    0.467  
##  9 TRX    lm    0.00784
## 10 KNC    lm    0.204  
## # ... with 395 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.