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 
## 1264.59723764    0.01129727 1014.78012188

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.6115939        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.3433042

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: 305 x 4
## # Groups:   symbol, split [305]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1      0.877           0.960
##  2 ETH        1      0.756           0.860
##  3 EOS        1      0.820           0.885
##  4 LTC        1      0.756           0.776
##  5 BSV        1      0.589           0.859
##  6 ADA        1      0.859           0.736
##  7 ZEC        1      0.570           0.864
##  8 HT         1      0.0795          0.952
##  9 TRX        1      0.0182          0.946
## 10 XMR        1      0.684           0.856
## # ... with 295 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: 305 x 6
## # Groups:   symbol, split [305]
##     symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>       <dbl>          <dbl>
##   1 BTC        1       0.343            1.47   0.877             0.960  
##   2 ETH        1       0.426            2.20   0.756             0.860  
##   3 EOS        1       0.346            1.92   0.820             0.885  
##   4 LTC        1       0.581            1.79   0.756             0.776  
##   5 BSV        1       0.515            1.69   0.589             0.859  
##   6 ADA        1       0.606            1.68   0.859             0.736  
##   7 ZEC        1       2.60             4.67   0.570             0.864  
##   8 HT         1       1.10             0.388  0.0795            0.952  
##   9 TRX        1       1.08             0.987  0.0182            0.946  
##  10 XMR        1       0.529            1.95   0.684             0.856  
##  11 KNC        1       1.45             1.30   0.755             0.724  
##  12 ZRX        1       0.673            1.87   0.541             0.929  
##  13 BAT        1       0.596            1.78   0.812             0.907  
##  14 BNT        1       0.604            0.619  0.425             0.964  
##  15 CROOLD     1       6.22             2.05   0.233             0.825  
##  16 MANA       1       1.15             3.65   0.823             0.613  
##  17 DGB        1       1.78             2.85   0.211             0.820  
##  18 ENJ        1       3.80             4.45   0.0000327         0.353  
##  19 XEM        1       0.731            1.56   0.531             0.936  
##  20 BTG        1       0.501            1.93   0.726             0.841  
##  21 ARDR       1       1.43             2.91   0.0230            0.849  
##  22 KMD        1       4.16             0.735  0.0113            0.946  
##  23 ELF        1       8.01             5.32   0.0000181         0.494  
##  24 NEXO       1       0.883            2.10   0.766             0.882  
##  25 VSYS       1       0.887            3.73   0.801             0.788  
##  26 CHZ        1       2.00             2.27   0.00216           0.982  
##  27 BRD        1       6.56             1.03   0.00316           0.138  
##  28 CKB        1       3.05             3.01   0.803             0.946  
##  29 DCR        1       5.40             1.60   0.0602            0.817  
##  30 WAXP       1       2.12             0.744  0.277             0.972  
##  31 VIB        1       3.60             2.85   0.289             0.911  
##  32 DENT       1       0.467            2.54   0.911             0.872  
##  33 LEO        1       4.07             0.907  0.0634            0.364  
##  34 RCN        1       0.964            9.22   0.888             0.673  
##  35 COCOS      1      65.4              2.97   0.0204            0.623  
##  36 ETP        1       2.68             1.44   0.00488           0.997  
##  37 AVA        1       0.631            2.96   0.811             0.706  
##  38 APPC       1     NaN                8.54  NA                 0.0478 
##  39 NAV        1       2.49             1.26   0.550             0.844  
##  40 JST        1       3.72             1.69   0.242             0.764  
##  41 ZKS        1       1.94             8.70   0.383             0.411  
##  42 BAL        1       0.739            2.63   0.654             0.903  
##  43 ORN        1       4.88             4.21   0.188             0.469  
##  44 SXP        1       1.24             2.04   0.270             0.909  
##  45 INJ        1       2.20             4.35   0.00639           0.957  
##  46 CELR       1       1.74             1.64   0.687             0.916  
##  47 CRPT       1       3.07             4.92   0.0485            0.331  
##  48 XCH        1       0.413            1.45   0.695             0.667  
##  49 ICP        1       0.566            1.84   0.779             0.845  
##  50 TXA        1      36.8              1.46   0.918             0.438  
##  51 1INCH      1       0.399            3.03   0.834             0.723  
##  52 ONG        1       3.73             1.18   0.755             0.198  
##  53 NCT        1       2.21             5.37   0.754             0.566  
##  54 ETC        1       0.765            0.760  0.559             0.00129
##  55 BTRST      1       3.22             4.10   0.142             0.656  
##  56 NEAR       1       0.766            1.80   0.810             0.969  
##  57 DASH       1       0.610            2.90   0.717             0.822  
##  58 DODO       1       1.28             2.44   0.302             0.942  
##  59 MAID       1     NaN                0.920 NA                 0.913  
##  60 DODO       2       0.770            2.44   0.288             0.942  
##  61 BAL        2       3.66             2.63   0.0607            0.903  
##  62 SXP        2       4.29             2.04   0.934             0.909  
##  63 INJ        2       3.94             4.35   0.0213            0.957  
##  64 BTRST      2      10.1              4.10   0.0736            0.656  
##  65 DASH       2       1.40             2.90   0.960             0.822  
##  66 CRPT       2       8.87             4.92   0.517             0.331  
##  67 ZKS        2       8.96             8.70   0.235             0.411  
##  68 ELF        2       6.43             5.32   0.0905            0.494  
##  69 ETC        2       4.64             0.760  0.144             0.00129
##  70 ETP        2       5.00             1.44   0.102             0.997  
##  71 DGB        2       0.664            2.85   0.920             0.820  
##  72 DCR        2       1.41             1.60   0.132             0.817  
##  73 CKB        2       7.36             3.01   0.00116           0.946  
##  74 KNC        2       4.40             1.30   0.307             0.724  
##  75 LEO        2       5.13             0.907  0.369             0.364  
##  76 ZRX        2       2.35             1.87   0.619             0.929  
##  77 XMR        2       0.740            1.95   0.659             0.856  
##  78 BAT        2       2.00             1.78   0.165             0.907  
##  79 VSYS       2       2.76             3.73   0.00873           0.788  
##  80 WAXP       2       4.03             0.744  0.563             0.972  
##  81 1INCH      2       4.75             3.03   0.0779            0.723  
##  82 NEAR       2       1.49             1.80   0.890             0.969  
##  83 BTC        2       0.414            1.47   0.868             0.960  
##  84 ETH        2       0.690            2.20   0.688             0.860  
##  85 EOS        2       1.47             1.92   0.696             0.885  
##  86 LTC        2       1.11             1.79   0.599             0.776  
##  87 ADA        2       0.869            1.68   0.540             0.736  
##  88 BSV        2       2.23             1.69   0.502             0.859  
##  89 TRX        2       0.679            0.987  0.840             0.946  
##  90 ZEC        2       1.06             4.67   0.555             0.864  
##  91 HT         2       0.801            0.388  0.943             0.952  
##  92 BNT        2       0.812            0.619  0.794             0.964  
##  93 MANA       2       0.721            3.65   0.919             0.613  
##  94 CROOLD     2       1.99             2.05   0.0345            0.825  
##  95 ENJ        2       1.92             4.45   0.214             0.353  
##  96 XEM        2       0.574            1.56   0.917             0.936  
##  97 ARDR       2       3.84             2.91   0.508             0.849  
##  98 NEXO       2       1.50             2.10   0.637             0.882  
##  99 CHZ        2       4.44             2.27   0.000112          0.982  
## 100 DENT       2       1.77             2.54   0.187             0.872  
## 101 RCN        2       2.32             9.22   0.966             0.673  
## 102 AVA        2       4.29             2.96   0.110             0.706  
## 103 JST        2       6.51             1.69   0.0180            0.764  
## 104 NAV        2       0.170            1.26   0.00118           0.844  
## 105 CELR       2       3.14             1.64   0.00798           0.916  
## 106 XCH        2       1.88             1.45   0.189             0.667  
## 107 ICP        2       6.18             1.84   0.00162           0.845  
## 108 TXA        2     101.               1.46   0.112             0.438  
## 109 ONG        2       0.781            1.18   0.446             0.198  
## 110 NCT        2       5.06             5.37   0.00119           0.566  
## 111 BTG        2       2.24             1.93   0.250             0.841  
## 112 COCOS      2       0.260            2.97   0.243             0.623  
## 113 KMD        2       2.94             0.735  0.0121            0.946  
## 114 BRD        2       6.30             1.03   0.0173            0.138  
## 115 ORN        2       4.21             4.21   0.0617            0.469  
## 116 MAID       2       0.790            0.920  0.773             0.913  
## 117 VIB        2       2.97             2.85   0.699             0.911  
## 118 KRRX       1       2.63             2.09   0.602             0.204  
## 119 APPC       2       3.06             8.54   0.928             0.0478 
## 120 DODO       3       2.67             2.44   0.530             0.942  
## 121 BAL        3       1.64             2.63   0.699             0.903  
## 122 SXP        3       0.840            2.04   0.279             0.909  
## 123 INJ        3       1.14             4.35   0.248             0.957  
## 124 BTRST      3       9.88             4.10   0.0514            0.656  
## 125 CRPT       3       3.18             4.92   0.265             0.331  
## 126 DASH       3       0.635            2.90   0.385             0.822  
## 127 ELF        3       0.908            5.32   0.317             0.494  
## 128 ETC        3       0.912            0.760  0.889             0.00129
## 129 ZKS        3       3.45             8.70   0.0564            0.411  
## 130 ETP        3       3.48             1.44   0.228             0.997  
## 131 CKB        3       1.75             3.01   0.745             0.946  
## 132 ZRX        3       1.02             1.87   0.774             0.929  
## 133 LEO        3       1.95             0.907  0.0335            0.364  
## 134 XMR        3       1.14             1.95   0.0459            0.856  
## 135 BAT        3       2.31             1.78   0.00189           0.907  
## 136 1INCH      3       1.31             3.03   0.641             0.723  
## 137 NEAR       3       2.18             1.80   0.916             0.969  
## 138 WAXP       3       0.620            0.744  0.929             0.972  
## 139 BTC        3       0.875            1.47   0.00000650        0.960  
## 140 ETH        3       0.624            2.20   0.0110            0.860  
## 141 EOS        3       1.09             1.92   0.184             0.885  
## 142 LTC        3       1.06             1.79   0.00335           0.776  
## 143 ADA        3       2.18             1.68   0.412             0.736  
## 144 BSV        3       0.545            1.69   0.335             0.859  
## 145 TRX        3       0.735            0.987  0.177             0.946  
## 146 ZEC        3       0.657            4.67   0.803             0.864  
## 147 HT         3       0.261            0.388  0.302             0.952  
## 148 BNT        3       1.17             0.619  0.104             0.964  
## 149 MANA       3       1.74             3.65   0.116             0.613  
## 150 CROOLD     3       1.64             2.05   0.483             0.825  
## 151 ENJ        3       2.57             4.45   0.0113            0.353  
## 152 XEM        3       1.01             1.56   0.601             0.936  
## 153 BTG        3       0.969            1.93   0.122             0.841  
## 154 ARDR       3       1.32             2.91   0.459             0.849  
## 155 NEXO       3       1.21             2.10   0.411             0.882  
## 156 VSYS       3       2.51             3.73   0.0418            0.788  
## 157 CHZ        3       1.60             2.27   0.0648            0.982  
## 158 DENT       3       2.99             2.54   0.714             0.872  
## 159 RCN        3       3.63             9.22   0.935             0.673  
## 160 AVA        3       1.12             2.96   0.0495            0.706  
## 161 NAV        3       0.160            1.26   0.188             0.844  
## 162 JST        3       3.11             1.69   0.364             0.764  
## 163 CELR       3       1.19             1.64   0.655             0.916  
## 164 XCH        3       0.769            1.45   0.735             0.667  
## 165 ICP        3       2.35             1.84   0.144             0.845  
## 166 TXA        3     142.               1.46   0.229             0.438  
## 167 ONG        3       0.957            1.18   0.622             0.198  
## 168 NCT        3       2.50             5.37   0.505             0.566  
## 169 BRD        3       2.59             1.03   0.923             0.138  
## 170 DCR        3       1.99             1.60   0.0173            0.817  
## 171 COCOS      3       9.30             2.97   0.657             0.623  
## 172 LAZIO      1       1.40             9.03   0.000198          0.831  
## 173 KNC        3       1.41             1.30   0.135             0.724  
## 174 KMD        3       1.17             0.735  0.528             0.946  
## 175 ORN        3       9.97             4.21   0.101             0.469  
## 176 KRRX       2       5.13             2.09   0.595             0.204  
## 177 DGB        3       1.13             2.85   0.00651           0.820  
## 178 MAID       3       0.913            0.920  0.425             0.913  
## 179 VIB        3       3.46             2.85   0.735             0.911  
## 180 APPC       3      95.0              8.54   0.242             0.0478 
## 181 LAZIO      2       1.11             9.03   0.0121            0.831  
## 182 BAL        4       0.729            2.63   0.603             0.903  
## 183 SXP        4       1.55             2.04   0.0310            0.909  
## 184 INJ        4       1.20             4.35   0.221             0.957  
## 185 BTRST      4       1.14             4.10   0.0615            0.656  
## 186 CRPT       4       3.07             4.92   0.00765           0.331  
## 187 DASH       4       1.34             2.90   0.657             0.822  
## 188 KRRX       3       1.87             2.09   0.380             0.204  
## 189 ETC        4       1.22             0.760  0.00165           0.00129
## 190 ELF        4     NaN                5.32  NA                 0.494  
## 191 ETP        4       0.856            1.44   0.395             0.997  
## 192 ZKS        4      11.4              8.70   0.000852          0.411  
## 193 BRD        4       7.66             1.03   0.321             0.138  
## 194 1INCH      4       0.768            3.03   0.915             0.723  
## 195 XMR        4       0.767            1.95   0.890             0.856  
## 196 BAT        4       1.74             1.78   0.698             0.907  
## 197 WAXP       4       1.50             0.744  0.715             0.972  
## 198 NEAR       4       1.41             1.80   0.451             0.969  
## 199 VSYS       4       1.77             3.73   0.509             0.788  
## 200 BTC        4       0.495            1.47   0.897             0.960  
## 201 ETH        4       0.664            2.20   0.942             0.860  
## 202 EOS        4       0.541            1.92   0.918             0.885  
## 203 LTC        4       0.927            1.79   0.738             0.776  
## 204 ADA        4       0.555            1.68   0.930             0.736  
## 205 BSV        4       1.20             1.69   0.567             0.859  
## 206 TRX        4       0.657            0.987  0.846             0.946  
## 207 ZEC        4       0.638            4.67   0.794             0.864  
## 208 HT         4       0.483            0.388  0.692             0.952  
## 209 BNT        4       0.736            0.619  0.823             0.964  
## 210 CROOLD     4       0.874            2.05   0.957             0.825  
## 211 MANA       4       0.710            3.65   0.943             0.613  
## 212 ENJ        4       1.18             4.45   0.838             0.353  
## 213 XEM        4       1.08             1.56   0.526             0.936  
## 214 BTG        4       0.831            1.93   0.873             0.841  
## 215 ARDR       4       7.14             2.91   0.00983           0.849  
## 216 NEXO       4       1.37             2.10   0.812             0.882  
## 217 CHZ        4       1.30             2.27   0.741             0.982  
## 218 DENT       4       1.28             2.54   0.739             0.872  
## 219 RCN        4       1.16             9.22   0.997             0.673  
## 220 AVA        4       0.933            2.96   0.883             0.706  
## 221 NAV        4       0.188            1.26   0.946             0.844  
## 222 JST        4       3.38             1.69   0.0838            0.764  
## 223 TXA        4       0.0387           1.46   0.999             0.438  
## 224 ONG        4       3.68             1.18   0.187             0.198  
## 225 ICP        4       3.89             1.84   0.575             0.845  
## 226 NCT        4       2.17             5.37   0.584             0.566  
## 227 XCH        4       9.27             1.45   0.0270            0.667  
## 228 CELR       4       2.16             1.64   0.620             0.916  
## 229 COCOS      4       1.55             2.97   0.839             0.623  
## 230 LEO        4       5.27             0.907  0.807             0.364  
## 231 ORN        4       4.87             4.21   0.981             0.469  
## 232 KNC        4       2.41             1.30   0.0779            0.724  
## 233 KMD        4       1.15             0.735  0.744             0.946  
## 234 CKB        4       1.63             3.01   0.527             0.946  
## 235 ZRX        4       2.15             1.87   0.614             0.929  
## 236 DCR        4       0.942            1.60   0.980             0.817  
## 237 DGB        4       0.836            2.85   0.704             0.820  
## 238 MAID       4       1.92             0.920  0.188             0.913  
## 239 VIB        4       9.29             2.85   0.189             0.911  
## 240 LAZIO      3       0.471            9.03   0.304             0.831  
## 241 APPC       4       1.55             8.54   0.933             0.0478 
## 242 DODO       4       2.14             2.44   0.0215            0.942  
## 243 KRRX       4       0.742            2.09   0.781             0.204  
## 244 ETC        5      NA                0.760 NA                 0.00129
## 245 LAZIO      4       0.864            9.03   0.286             0.831  
## 246 ZKS        5      NA                8.70  NA                 0.411  
## 247 ETP        5      NA                1.44  NA                 0.997  
## 248 BRD        5      NA                1.03  NA                 0.138  
## 249 1INCH      5      NA                3.03  NA                 0.723  
## 250 XMR        5      NA                1.95  NA                 0.856  
## 251 WAXP       5      NA                0.744 NA                 0.972  
## 252 NEAR       5      NA                1.80  NA                 0.969  
## 253 BAT        5      NA                1.78  NA                 0.907  
## 254 BTG        5      NA                1.93  NA                 0.841  
## 255 VSYS       5      NA                3.73  NA                 0.788  
## 256 COCOS      5      NA                2.97  NA                 0.623  
## 257 BTC        5      NA                1.47  NA                 0.960  
## 258 EOS        5      NA                1.92  NA                 0.885  
## 259 ETH        5      NA                2.20  NA                 0.860  
## 260 LTC        5      NA                1.79  NA                 0.776  
## 261 BSV        5      NA                1.69  NA                 0.859  
## 262 ADA        5      NA                1.68  NA                 0.736  
## 263 ZEC        5      NA                4.67  NA                 0.864  
## 264 HT         5      NA                0.388 NA                 0.952  
## 265 TRX        5      NA                0.987 NA                 0.946  
## 266 BNT        5      NA                0.619 NA                 0.964  
## 267 MANA       5      NA                3.65  NA                 0.613  
## 268 CROOLD     5      NA                2.05  NA                 0.825  
## 269 ENJ        5      NA                4.45  NA                 0.353  
## 270 XEM        5      NA                1.56  NA                 0.936  
## 271 ARDR       5      NA                2.91  NA                 0.849  
## 272 NEXO       5      NA                2.10  NA                 0.882  
## 273 CHZ        5      NA                2.27  NA                 0.982  
## 274 DENT       5      NA                2.54  NA                 0.872  
## 275 RCN        5      NA                9.22  NA                 0.673  
## 276 LEO        5      NA                0.907 NA                 0.364  
## 277 AVA        5      NA                2.96  NA                 0.706  
## 278 NAV        5      NA                1.26  NA                 0.844  
## 279 JST        5      NA                1.69  NA                 0.764  
## 280 XCH        5      NA                1.45  NA                 0.667  
## 281 TXA        5      NA                1.46  NA                 0.438  
## 282 NCT        5      NA                5.37  NA                 0.566  
## 283 ONG        5      NA                1.18  NA                 0.198  
## 284 CELR       5      NA                1.64  NA                 0.916  
## 285 ICP        5      NA                1.84  NA                 0.845  
## 286 KMD        5      NA                0.735 NA                 0.946  
## 287 CKB        5      NA                3.01  NA                 0.946  
## 288 KNC        5      NA                1.30  NA                 0.724  
## 289 ZRX        5      NA                1.87  NA                 0.929  
## 290 DCR        5      NA                1.60  NA                 0.817  
## 291 DGB        5      NA                2.85  NA                 0.820  
## 292 ORN        5      NA                4.21  NA                 0.469  
## 293 ELF        5      NA                5.32  NA                 0.494  
## 294 MAID       5      NA                0.920 NA                 0.913  
## 295 APPC       5      NA                8.54  NA                 0.0478 
## 296 VIB        5      NA                2.85  NA                 0.911  
## 297 DASH       5      NA                2.90  NA                 0.822  
## 298 BTRST      5      NA                4.10  NA                 0.656  
## 299 INJ        5      NA                4.35  NA                 0.957  
## 300 BAL        5      NA                2.63  NA                 0.903  
## 301 SXP        5      NA                2.04  NA                 0.909  
## 302 CRPT       5      NA                4.92  NA                 0.331  
## 303 DODO       5      NA                2.44  NA                 0.942  
## 304 KRRX       5      NA                2.09  NA                 0.204  
## 305 LAZIO      5      NA                9.03  NA                 0.831

Out of 305 groups, 94 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: 305 x 6
## # Groups:   symbol, split [305]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        0.343      0.877            1.47           0.960
##  2 ETH        1        0.426      0.756            2.20           0.860
##  3 EOS        1        0.346      0.820            1.92           0.885
##  4 LTC        1        0.581      0.756            1.79           0.776
##  5 BSV        1        0.515      0.589            1.69           0.859
##  6 ADA        1        0.606      0.859            1.68           0.736
##  7 ZEC        1        2.60       0.570            4.67           0.864
##  8 HT         1        1.10       0.0795           0.388          0.952
##  9 TRX        1        1.08       0.0182           0.987          0.946
## 10 XMR        1        0.529      0.684            1.95           0.856
## # ... with 295 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: 305 x 3
## # Groups:   symbol [61]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.532
##  2 ETH    lm    0.601
##  3 EOS    lm    0.861
##  4 LTC    lm    0.920
##  5 BSV    lm    1.12 
##  6 ADA    lm    1.05 
##  7 ZEC    lm    1.24 
##  8 HT     lm    0.661
##  9 TRX    lm    0.789
## 10 XMR    lm    0.793
## # ... with 295 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: 305 x 3
## # Groups:   symbol [61]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    1.47 
##  2 ETH    lm    2.20 
##  3 EOS    lm    1.92 
##  4 LTC    lm    1.79 
##  5 BSV    lm    1.69 
##  6 ADA    lm    1.68 
##  7 ZEC    lm    4.67 
##  8 HT     lm    0.388
##  9 TRX    lm    0.987
## 10 XMR    lm    1.95 
## # ... with 295 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: 305 x 3
## # Groups:   symbol [61]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.660
##  2 ETH    lm    0.599
##  3 EOS    lm    0.654
##  4 LTC    lm    0.524
##  5 BSV    lm    0.498
##  6 ADA    lm    0.685
##  7 ZEC    lm    0.681
##  8 HT     lm    0.504
##  9 TRX    lm    0.470
## 10 XMR    lm    0.570
## # ... with 295 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: 305 x 3
## # Groups:   symbol [61]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.960
##  2 ETH    lm    0.860
##  3 EOS    lm    0.885
##  4 LTC    lm    0.776
##  5 BSV    lm    0.859
##  6 ADA    lm    0.736
##  7 ZEC    lm    0.864
##  8 HT     lm    0.952
##  9 TRX    lm    0.946
## 10 XMR    lm    0.856
## # ... with 295 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')
## Error in github_update_head(board, branch, commit_result$sha): Failed to update branch master: Reference cannot be updated

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.