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 
## 2110.82827730    0.02794387 1637.73148718

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.1481044        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.5128067

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: 390 x 4
## # Groups:   symbol, split [390]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1      0.753           0.952
##  2 ETH        1      0.700           0.900
##  3 EOS        1      0.137           0.905
##  4 LTC        1      0.390           0.756
##  5 ADA        1      0.661           0.834
##  6 BSV        1      0.557           0.792
##  7 TRX        1      0.680           0.807
##  8 ZEC        1      0.929           0.948
##  9 HT         1      0.0194          0.718
## 10 KNC        1      0.835           0.928
## # ... with 380 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: 390 x 6
## # Groups:   symbol, split [390]
##     symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>       <dbl>          <dbl>
##   1 BTC        1        0.513           0.532   0.753           0.952   
##   2 ETH        1        1.33            1.10    0.700           0.900   
##   3 EOS        1        2.62            1.84    0.137           0.905   
##   4 LTC        1        1.78            2.25    0.390           0.756   
##   5 ADA        1        1.18            1.25    0.661           0.834   
##   6 BSV        1        2.76            2.51    0.557           0.792   
##   7 TRX        1        1.62            1.91    0.680           0.807   
##   8 ZEC        1        0.568           1.47    0.929           0.948   
##   9 HT         1        2.62            2.97    0.0194          0.718   
##  10 KNC        1        0.692           1.77    0.835           0.928   
##  11 XMR        1        1.31            1.33    0.326           0.875   
##  12 ZRX        1        0.514           2.31    0.959           0.879   
##  13 BAT        1        1.52            1.77    0.374           0.865   
##  14 BNT        1        1.08            1.15    0.815           0.901   
##  15 CRO        1        1.60            2.20    0.457           0.321   
##  16 MANA       1        1.75            2.13    0.447           0.926   
##  17 DGB        1        3.74            2.34    0.0267          0.821   
##  18 ENJ        1        0.600           2.39    0.844           0.932   
##  19 BTM        1        1.62            0.797   0.729           0.671   
##  20 XEM        1        1.14            2.50    0.0848          0.894   
##  21 ARDR       1      729.              1.70    0.00306         0.864   
##  22 KMD        1        1.25            1.15    0.662           0.969   
##  23 ELF        1        1.83            1.73    0.137           0.919   
##  24 NEXO       1        2.69            2.38    0.772           0.764   
##  25 MBL        1       11.2             3.97    0.0735          0.722   
##  26 VSYS       1       28.5             4.73    0.0869          0.00990 
##  27 CHZ        1        2.06            1.43    0.163           0.897   
##  28 CKB        1        2.63            2.60    0.684           0.864   
##  29 BRD        1        2.85            2.94    0.00680         0.00435 
##  30 DCR        1        0.700           2.30    0.777           0.326   
##  31 IQ         1        6.68            7.34    0.132           0.544   
##  32 WAXP       1        0.717           2.93    0.795           0.888   
##  33 OAX        1        1.00            2.11    0.564           0.594   
##  34 VIB        1        2.93            3.00    0.290           0.767   
##  35 DENT       1        2.84            3.09    0.0480          0.424   
##  36 RCN        1        6.21            7.60    0.498           0.632   
##  37 LEO        1        6.14            5.75    0.000822        0.176   
##  38 ETP        1        5.06            1.44    0.630           0.165   
##  39 AVA        1        1.18            2.94    0.450           0.454   
##  40 APPC       1        6.60            3.79    0.390           0.188   
##  41 CND        1        7.07            5.33    0.0559          0.160   
##  42 JST        1        1.61            2.82    0.740           0.505   
##  43 SMART      1        2.02            4.12    0.0723          1       
##  44 SRN        1        4.37            7.81    0.971           0.557   
##  45 DOGE       1        4.20            2.01    0.784           0.667   
##  46 IOST       1        2.92            3.86    0.343           0.669   
##  47 SNX        1        1.87            1.37    0.716           0.934   
##  48 ZIL        1        0.974           2.09    0.648           0.884   
##  49 CBC        1      109.              1.02    0.000115        0.105   
##  50 NXT        1        1.15            2.89    0.860           0.880   
##  51 GASP       1        0.914           2.24    0.473           0.619   
##  52 ASP        1        0.835           2.19    0.864           0.699   
##  53 YFI        1        0.734           0.873   0.871           0.908   
##  54 CCE        1        2.20            7.26    0.737           0.119   
##  55 ETC        1        0.569           1.47    0.985           0.706   
##  56 UNO        1        0.973           0.257   0.899           0.647   
##  57 FIL        1        0.940           1.29    0.735           0.946   
##  58 MKR        1        2.99            1.04    0.598           0.969   
##  59 CRV        1        3.55            1.93    0.0400          0.902   
##  60 BZRX       1        1.30            2.74    0.495           0.971   
##  61 XTZ        1        1.78            1.88    0.212           0.789   
##  62 BCD        1        3.55            3.03    0.515           0.102   
##  63 EVX        1        8.35            2.03    0.317           0.00195 
##  64 EURS       1        0.422           0.688   0.00346         0.744   
##  65 TV         1        0.904           1.67    0.115           0.0336  
##  66 UNI        1        3.96            1.60    0.0685          0.881   
##  67 SNC        1        0.871           3.20    0.00855         0.843   
##  68 LEVL       1        2.40            3.58    0.364           0.542   
##  69 SUSHI      1        2.22            0.813   0.0000849       0.937   
##  70 PPC        1        1.43            1.55    0.778           0.736   
##  71 FTM        1        1.05            1.33    0.375           0.877   
##  72 DGTX       1        2.08            4.73    0.361           0.559   
##  73 BCHA       1        2.39            1.67    0.0121          0.0798  
##  74 INJ        1        0.744           1.59    0.594           0.516   
##  75 BTG        1        1.87            4.01    0.146           0.000939
##  76 BMC        1        1.88           11.6     0.0711          0.0320  
##  77 COTI       1        1.88            3.42    0.000515        0.534   
##  78 BRD        2        5.10            2.94    0.00213         0.00435 
##  79 VIB        2        3.23            3.00    0.601           0.767   
##  80 OAX        2        3.79            2.11    0.000764        0.594   
##  81 RCN        2       10.0             7.60    0.0231          0.632   
##  82 EVX        2       15.1             2.03    0.132           0.00195 
##  83 LEVL       2        2.70            3.58    0.00880         0.542   
##  84 IQ         2        6.61            7.34    0.600           0.544   
##  85 PPC        2        2.16            1.55    0.524           0.736   
##  86 EURS       2        0.864           0.688   0.0909          0.744   
##  87 TV         2        0.884           1.67    0.0265          0.0336  
##  88 DGTX       2        1.89            4.73    0.280           0.559   
##  89 LEO        2        4.11            5.75    0.110           0.176   
##  90 SNC        2        1.73            3.20    0.00842         0.843   
##  91 BNT        2        0.687           1.15    0.506           0.901   
##  92 KMD        2        1.28            1.15    0.403           0.969   
##  93 APPC       2        9.64            3.79    0.0162          0.188   
##  94 JST        2        6.05            2.82    0.374           0.505   
##  95 NXT        2        1.24            2.89    0.844           0.880   
##  96 LTC        2        0.525           2.25    0.957           0.756   
##  97 ADA        2        1.17            1.25    0.173           0.834   
##  98 ZRX        2        1.02            2.31    0.721           0.879   
##  99 MBL        2        3.03            3.97    0.178           0.722   
## 100 ZIL        2        0.803           2.09    0.633           0.884   
## 101 ASP        2        0.986           2.19    0.719           0.699   
## 102 BTC        2        0.415           0.532   0.909           0.952   
## 103 EOS        2        0.763           1.84    0.836           0.905   
## 104 BSV        2        1.01            2.51    0.959           0.792   
## 105 TRX        2        0.601           1.91    0.724           0.807   
## 106 XMR        2        0.713           1.33    0.304           0.875   
## 107 BAT        2        0.967           1.77    0.480           0.865   
## 108 MANA       2        2.02            2.13    0.138           0.926   
## 109 ENJ        2        1.02            2.39    0.469           0.932   
## 110 XEM        2        0.385           2.50    0.900           0.894   
## 111 ARDR       2        1.12            1.70    0.740           0.864   
## 112 WAXP       2        1.03            2.93    0.753           0.888   
## 113 ETP        2        1.34            1.44    0.239           0.165   
## 114 CBC        2        0.646           1.02    0.696           0.105   
## 115 MKR        2        1.68            1.04    0.549           0.969   
## 116 ETH        2        0.283           1.10    0.933           0.900   
## 117 ZEC        2        0.746           1.47    0.565           0.948   
## 118 KNC        2        1.06            1.77    0.477           0.928   
## 119 CRO        2        0.338           2.20    0.744           0.321   
## 120 NEXO       2        0.949           2.38    0.115           0.764   
## 121 CHZ        2        0.865           1.43    0.773           0.897   
## 122 DENT       2        1.64            3.09    0.457           0.424   
## 123 AVA        2        0.727           2.94    0.668           0.454   
## 124 DOGE       2        1.31            2.01    0.829           0.667   
## 125 GASP       2        0.935           2.24    0.721           0.619   
## 126 YFI        2        0.810           0.873   0.0835          0.908   
## 127 ETC        2        1.61            1.47    0.890           0.706   
## 128 BZRX       2        1.64            2.74    0.301           0.971   
## 129 UNI        2        1.06            1.60    0.570           0.881   
## 130 SUSHI      2        0.958           0.813   0.470           0.937   
## 131 FTM        2        3.42            1.33    0.154           0.877   
## 132 INJ        2        2.20            1.59    0.190           0.516   
## 133 BTG        2        1.02            4.01    0.0914          0.000939
## 134 DCR        2        7.28            2.30    0.182           0.326   
## 135 IOST       2        0.932           3.86    0.451           0.669   
## 136 CRV        2        1.39            1.93    0.230           0.902   
## 137 BCD        2        2.50            3.03    0.135           0.102   
## 138 COTI       2        2.41            3.42    0.139           0.534   
## 139 ELF        2        1.08            1.73    0.706           0.919   
## 140 CKB        2        5.93            2.60    0.332           0.864   
## 141 SMART      2        0.678           4.12    0.814           1       
## 142 SNX        2        0.778           1.37    0.121           0.934   
## 143 BTM        2        0.827           0.797   0.503           0.671   
## 144 FIL        2        0.622           1.29    0.736           0.946   
## 145 HT         2        5.44            2.97    0.0263          0.718   
## 146 XTZ        2        0.713           1.88    0.698           0.789   
## 147 CND        2        1.14            5.33    0.155           0.160   
## 148 BCHA       2        0.982           1.67    0.263           0.0798  
## 149 DGB        2        2.81            2.34    0.367           0.821   
## 150 COCOS      1        0.128           2.59    0.00444         0.345   
## 151 CCE        2        2.90            7.26    0.00162         0.119   
## 152 SRN        2        3.13            7.81    0.0940          0.557   
## 153 UNO        2        2.21            0.257   0.608           0.647   
## 154 BMC        2        2.76           11.6     0.000484        0.0320  
## 155 VSYS       2        2.33            4.73    0.000567        0.00990 
## 156 BRD        3        7.35            2.94    0.109           0.00435 
## 157 VIB        3       11.5             3.00    0.778           0.767   
## 158 OAX        3        8.24            2.11    0.0555          0.594   
## 159 RCN        3        6.23            7.60    0.312           0.632   
## 160 COCOS      2        5.34            2.59    0.0733          0.345   
## 161 EVX        3       11.1             2.03    0.130           0.00195 
## 162 SMART      3        3.46            4.12    0.107           1       
## 163 APPC       3       14.5             3.79    0.136           0.188   
## 164 PPC        3       17.6             1.55    0.814           0.736   
## 165 LEVL       3        2.26            3.58    0.0719          0.542   
## 166 DGTX       3        9.90            4.73    0.199           0.559   
## 167 IQ         3        3.24            7.34    0.774           0.544   
## 168 EURS       3        0.209           0.688   0.390           0.744   
## 169 NXT        3        3.19            2.89    0.856           0.880   
## 170 LEO        3        2.96            5.75    0.697           0.176   
## 171 SNC        3        6.93            3.20    0.0516          0.843   
## 172 BAT        3        1.70            1.77    0.449           0.865   
## 173 JST        3        4.76            2.82    0.206           0.505   
## 174 ETP        3        1.60            1.44    0.664           0.165   
## 175 ASP        3        2.71            2.19    0.0420          0.699   
## 176 LTC        3        2.73            2.25    0.111           0.756   
## 177 ADA        3        2.74            1.25    0.540           0.834   
## 178 ZRX        3        1.50            2.31    0.538           0.879   
## 179 KMD        3        2.86            1.15    0.231           0.969   
## 180 ZIL        3        1.93            2.09    0.125           0.884   
## 181 KNC        3        2.59            1.77    0.331           0.928   
## 182 XMR        3        3.81            1.33    0.518           0.875   
## 183 ARDR       3        3.09            1.70    0.427           0.864   
## 184 ELF        3        3.03            1.73    0.000931        0.919   
## 185 WAXP       3        6.36            2.93    0.488           0.888   
## 186 CBC        3        1.88            1.02    0.811           0.105   
## 187 TV         3        2.03            1.67    0.462           0.0336  
## 188 BTC        3        1.64            0.532   0.711           0.952   
## 189 ETH        3        2.80            1.10    0.628           0.900   
## 190 EOS        3        2.73            1.84    0.855           0.905   
## 191 BSV        3        2.79            2.51    0.366           0.792   
## 192 TRX        3        2.18            1.91    0.539           0.807   
## 193 BNT        3        2.64            1.15    0.252           0.901   
## 194 MANA       3        2.41            2.13    0.556           0.926   
## 195 ENJ        3        2.33            2.39    0.527           0.932   
## 196 XEM        3        2.42            2.50    0.462           0.894   
## 197 DOGE       3        4.45            2.01    0.436           0.667   
## 198 ETC        3        1.66            1.47    0.965           0.706   
## 199 ZEC        3        3.02            1.47    0.437           0.948   
## 200 NEXO       3        7.76            2.38    0.0514          0.764   
## 201 CHZ        3        2.08            1.43    0.478           0.897   
## 202 DCR        3        2.43            2.30    0.0110          0.326   
## 203 DENT       3        2.32            3.09    0.416           0.424   
## 204 AVA        3        3.02            2.94    0.605           0.454   
## 205 GASP       3        2.00            2.24    0.700           0.619   
## 206 YFI        3        2.08            0.873   0.501           0.908   
## 207 MKR        3        3.81            1.04    0.0750          0.969   
## 208 CRV        3        1.80            1.93    0.712           0.902   
## 209 BZRX       3        4.89            2.74    0.173           0.971   
## 210 BCD        3        2.82            3.03    0.737           0.102   
## 211 UNI        3        2.92            1.60    0.147           0.881   
## 212 SUSHI      3        2.91            0.813   0.442           0.937   
## 213 FTM        3        2.32            1.33    0.442           0.877   
## 214 INJ        3        2.86            1.59    0.000748        0.516   
## 215 HT         3        6.05            2.97    0.664           0.718   
## 216 CRO        3        1.68            2.20    0.530           0.321   
## 217 BTG        3        3.31            4.01    0.112           0.000939
## 218 COTI       3        2.45            3.42    0.847           0.534   
## 219 BTM        3        2.40            0.797   0.407           0.671   
## 220 CKB        3        3.17            2.60    0.432           0.864   
## 221 IOST       3        1.34            3.86    0.853           0.669   
## 222 MBL        3        5.82            3.97    0.0696          0.722   
## 223 SNX        3        3.49            1.37    0.753           0.934   
## 224 BCHA       3        6.94            1.67    0.101           0.0798  
## 225 XTZ        3        2.07            1.88    0.423           0.789   
## 226 FIL        3        2.62            1.29    0.735           0.946   
## 227 DGB        3        5.34            2.34    0.955           0.821   
## 228 UNO        3        0.533           0.257   0.656           0.647   
## 229 CCE        3       85.3             7.26    0.0126          0.119   
## 230 SRN        3       26.6             7.81    0.0495          0.557   
## 231 BMC        3        1.35           11.6     0.00105         0.0320  
## 232 CND        3        5.02            5.33    0.734           0.160   
## 233 VSYS       3        6.52            4.73    0.456           0.00990 
## 234 BRD        4       13.4             2.94   NA               0.00435 
## 235 COCOS      3        4.72            2.59    0.124           0.345   
## 236 RCN        4       10.0             7.60    0.479           0.632   
## 237 EVX        4        2.43            2.03    0.00725         0.00195 
## 238 VIB        4        1.46            3.00    0.0955          0.767   
## 239 SMART      4        2.99            4.12   NA               1       
## 240 PPC        4        0.941           1.55    0.189           0.736   
## 241 APPC       4        2.86            3.79    0.375           0.188   
## 242 IQ         4        2.26            7.34    0.0735          0.544   
## 243 LEVL       4        3.51            3.58    0.0415          0.542   
## 244 DGTX       4        7.60            4.73    0.741           0.559   
## 245 TV         4        7.80            1.67    0.288           0.0336  
## 246 SNC        4        5.13            3.20    0.0974          0.843   
## 247 ETP        4        1.20            1.44    0.00616         0.165   
## 248 MBL        4        8.47            3.97    0.000312        0.722   
## 249 JST        4        0.955           2.82    0.560           0.505   
## 250 BAT        4        0.962           1.77    0.789           0.865   
## 251 BTM        4        1.04            0.797   0.00323         0.671   
## 252 ELF        4        0.972           1.73    0.467           0.919   
## 253 ADA        4        1.37            1.25    0.00484         0.834   
## 254 ZRX        4        1.64            2.31    0.673           0.879   
## 255 KMD        4        1.54            1.15    0.0238          0.969   
## 256 UNO        4        0.765           0.257   0.00467         0.647   
## 257 ZIL        4        1.78            2.09    0.664           0.884   
## 258 XMR        4        2.30            1.33    0.454           0.875   
## 259 BNT        4        1.55            1.15    0.562           0.901   
## 260 WAXP       4        1.26            2.93    0.0166          0.888   
## 261 CBC        4        0.696           1.02    0.403           0.105   
## 262 ETC        4        1.28            1.47    0.679           0.706   
## 263 ETH        4        0.831           1.10    0.773           0.900   
## 264 LTC        4        1.05            2.25    0.0979          0.756   
## 265 KNC        4        3.89            1.77    0.290           0.928   
## 266 ARDR       4        1.29            1.70    0.0706          0.864   
## 267 LEO        4        9.69            5.75    0.186           0.176   
## 268 ASP        4        1.45            2.19    0.117           0.699   
## 269 BTC        4        0.661           0.532   0.250           0.952   
## 270 EOS        4        0.995           1.84    0.222           0.905   
## 271 BSV        4        1.05            2.51    0.147           0.792   
## 272 TRX        4        0.673           1.91    0.629           0.807   
## 273 MANA       4        0.913           2.13    0.595           0.926   
## 274 ENJ        4        1.13            2.39    0.527           0.932   
## 275 XEM        4        0.784           2.50    0.565           0.894   
## 276 MKR        4        1.17            1.04    0.428           0.969   
## 277 DOGE       4        2.76            2.01    0.401           0.667   
## 278 BCD        4        3.18            3.03    0.00110         0.102   
## 279 CRV        4        2.61            1.93    0.791           0.902   
## 280 YFI        4        0.807           0.873   0.646           0.908   
## 281 ZEC        4        1.29            1.47    0.218           0.948   
## 282 BTG        4        0.917           4.01    0.0938          0.000939
## 283 NEXO       4        1.80            2.38    0.550           0.764   
## 284 CHZ        4        1.04            1.43    0.351           0.897   
## 285 DCR        4        1.31            2.30    0.753           0.326   
## 286 DENT       4        1.14            3.09    0.603           0.424   
## 287 AVA        4        1.83            2.94    0.116           0.454   
## 288 FTM        4        1.69            1.33    0.362           0.877   
## 289 SUSHI      4        1.41            0.813   0.123           0.937   
## 290 GASP       4        1.16            2.24    0.367           0.619   
## 291 BZRX       4        1.64            2.74    0.315           0.971   
## 292 UNI        4        1.06            1.60    0.352           0.881   
## 293 COTI       4        1.41            3.42    0.580           0.534   
## 294 INJ        4        1.79            1.59    0.230           0.516   
## 295 CRO        4        0.657           2.20    0.389           0.321   
## 296 CKB        4        2.96            2.60    0.535           0.864   
## 297 IOST       4        1.44            3.86    0.246           0.669   
## 298 XTZ        4        1.06            1.88    0.125           0.789   
## 299 NXT        4        1.55            2.89    0.630           0.880   
## 300 FIL        4        2.80            1.29    0.425           0.946   
## 301 DGB        4        1.09            2.34    0.553           0.821   
## 302 HT         4        4.47            2.97    0.691           0.718   
## 303 SNX        4        1.41            1.37    0.195           0.934   
## 304 BMC        4        7.52           11.6     0.0229          0.0320  
## 305 BCHA       4        1.31            1.67    0.420           0.0798  
## 306 EURS       4        0.186           0.688   0.563           0.744   
## 307 OAX        4        5.50            2.11    0.643           0.594   
## 308 CCE        4        4.27            7.26    0.507           0.119   
## 309 SRN        4        4.25            7.81    0.375           0.557   
## 310 CND        4        3.32            5.33    0.0321          0.160   
## 311 VSYS       4        1.45            4.73    0.216           0.00990 
## 312 COCOS      4        3.39            2.59    0.0135          0.345   
## 313 BRD        5       NA               2.94   NA               0.00435 
## 314 SMART      5       NA               4.12   NA               1       
## 315 TV         5       NA               1.67   NA               0.0336  
## 316 DGTX       5       NA               4.73   NA               0.559   
## 317 MBL        5       NA               3.97   NA               0.722   
## 318 EVX        5       NA               2.03   NA               0.00195 
## 319 UNO        5       NA               0.257  NA               0.647   
## 320 BTM        5       NA               0.797  NA               0.671   
## 321 IQ         5       NA               7.34   NA               0.544   
## 322 ZIL        5       NA               2.09   NA               0.884   
## 323 ZRX        5       NA               2.31   NA               0.879   
## 324 CBC        5       NA               1.02   NA               0.105   
## 325 ETH        5       NA               1.10   NA               0.900   
## 326 LTC        5       NA               2.25   NA               0.756   
## 327 ADA        5       NA               1.25   NA               0.834   
## 328 XMR        5       NA               1.33   NA               0.875   
## 329 WAXP       5       NA               2.93   NA               0.888   
## 330 ETC        5       NA               1.47   NA               0.706   
## 331 BMC        5       NA              11.6    NA               0.0320  
## 332 BNT        5       NA               1.15   NA               0.901   
## 333 ARDR       5       NA               1.70   NA               0.864   
## 334 ETP        5       NA               1.44   NA               0.165   
## 335 DOGE       5       NA               2.01   NA               0.667   
## 336 BCD        5       NA               3.03   NA               0.102   
## 337 CRV        5       NA               1.93   NA               0.902   
## 338 YFI        5       NA               0.873  NA               0.908   
## 339 ASP        5       NA               2.19   NA               0.699   
## 340 BTC        5       NA               0.532  NA               0.952   
## 341 EOS        5       NA               1.84   NA               0.905   
## 342 BSV        5       NA               2.51   NA               0.792   
## 343 MANA       5       NA               2.13   NA               0.926   
## 344 ENJ        5       NA               2.39   NA               0.932   
## 345 XEM        5       NA               2.50   NA               0.894   
## 346 KMD        5       NA               1.15   NA               0.969   
## 347 DCR        5       NA               2.30   NA               0.326   
## 348 MKR        5       NA               1.04   NA               0.969   
## 349 XTZ        5       NA               1.88   NA               0.789   
## 350 COTI       5       NA               3.42   NA               0.534   
## 351 ZEC        5       NA               1.47   NA               0.948   
## 352 TRX        5       NA               1.91   NA               0.807   
## 353 CRO        5       NA               2.20   NA               0.321   
## 354 BTG        5       NA               4.01   NA               0.000939
## 355 NEXO       5       NA               2.38   NA               0.764   
## 356 CHZ        5       NA               1.43   NA               0.897   
## 357 CKB        5       NA               2.60   NA               0.864   
## 358 DENT       5       NA               3.09   NA               0.424   
## 359 AVA        5       NA               2.94   NA               0.454   
## 360 FTM        5       NA               1.33   NA               0.877   
## 361 SUSHI      5       NA               0.813  NA               0.937   
## 362 BZRX       5       NA               2.74   NA               0.971   
## 363 GASP       5       NA               2.24   NA               0.619   
## 364 IOST       5       NA               3.86   NA               0.669   
## 365 NXT        5       NA               2.89   NA               0.880   
## 366 UNI        5       NA               1.60   NA               0.881   
## 367 INJ        5       NA               1.59   NA               0.516   
## 368 DGB        5       NA               2.34   NA               0.821   
## 369 JST        5       NA               2.82   NA               0.505   
## 370 HT         5       NA               2.97   NA               0.718   
## 371 KNC        5       NA               1.77   NA               0.928   
## 372 BAT        5       NA               1.77   NA               0.865   
## 373 FIL        5       NA               1.29   NA               0.946   
## 374 SNX        5       NA               1.37   NA               0.934   
## 375 ELF        5       NA               1.73   NA               0.919   
## 376 LEO        5       NA               5.75   NA               0.176   
## 377 SNC        5       NA               3.20   NA               0.843   
## 378 PPC        5       NA               1.55   NA               0.736   
## 379 EURS       5       NA               0.688  NA               0.744   
## 380 BCHA       5       NA               1.67   NA               0.0798  
## 381 LEVL       5       NA               3.58   NA               0.542   
## 382 APPC       5       NA               3.79   NA               0.188   
## 383 CCE        5       NA               7.26   NA               0.119   
## 384 VSYS       5       NA               4.73   NA               0.00990 
## 385 RCN        5       NA               7.60   NA               0.632   
## 386 CND        5       NA               5.33   NA               0.160   
## 387 VIB        5       NA               3.00   NA               0.767   
## 388 SRN        5       NA               7.81   NA               0.557   
## 389 OAX        5       NA               2.11   NA               0.594   
## 390 COCOS      5       NA               2.59   NA               0.345

Out of 390 groups, 136 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: 390 x 6
## # Groups:   symbol, split [390]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        0.513      0.753            0.532          0.952
##  2 ETH        1        1.33       0.700            1.10           0.900
##  3 EOS        1        2.62       0.137            1.84           0.905
##  4 LTC        1        1.78       0.390            2.25           0.756
##  5 ADA        1        1.18       0.661            1.25           0.834
##  6 BSV        1        2.76       0.557            2.51           0.792
##  7 TRX        1        1.62       0.680            1.91           0.807
##  8 ZEC        1        0.568      0.929            1.47           0.948
##  9 HT         1        2.62       0.0194           2.97           0.718
## 10 KNC        1        0.692      0.835            1.77           0.928
## # ... with 380 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: 390 x 3
## # Groups:   symbol [78]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.807
##  2 ETH    lm    1.31 
##  3 EOS    lm    1.78 
##  4 LTC    lm    1.52 
##  5 ADA    lm    1.61 
##  6 BSV    lm    1.90 
##  7 TRX    lm    1.27 
##  8 ZEC    lm    1.41 
##  9 HT     lm    4.65 
## 10 KNC    lm    2.06 
## # ... with 380 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: 390 x 3
## # Groups:   symbol [78]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.532
##  2 ETH    lm    1.10 
##  3 EOS    lm    1.84 
##  4 LTC    lm    2.25 
##  5 ADA    lm    1.25 
##  6 BSV    lm    2.51 
##  7 TRX    lm    1.91 
##  8 ZEC    lm    1.47 
##  9 HT     lm    2.97 
## 10 KNC    lm    1.77 
## # ... with 380 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: 390 x 3
## # Groups:   symbol [78]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.656
##  2 ETH    lm    0.758
##  3 EOS    lm    0.513
##  4 LTC    lm    0.389
##  5 ADA    lm    0.344
##  6 BSV    lm    0.507
##  7 TRX    lm    0.643
##  8 ZEC    lm    0.537
##  9 HT     lm    0.350
## 10 KNC    lm    0.483
## # ... with 380 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: 390 x 3
## # Groups:   symbol [78]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.952
##  2 ETH    lm    0.900
##  3 EOS    lm    0.905
##  4 LTC    lm    0.756
##  5 ADA    lm    0.834
##  6 BSV    lm    0.792
##  7 TRX    lm    0.807
##  8 ZEC    lm    0.948
##  9 HT     lm    0.718
## 10 KNC    lm    0.928
## # ... with 380 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.