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 
## 385.305358   0.482546 308.977722

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.434784       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.3194313

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: 260 x 4
## # Groups:   symbol, split [260]
##    symbol split lm_rsq_test lm_rsq_holdout
##    <chr>  <dbl>       <dbl>          <dbl>
##  1 BTC        1       0.732          0.748
##  2 ETH        1       0.647          0.563
##  3 EOS        1       0.737          0.289
##  4 LTC        1       0.379          0.523
##  5 ADA        1       0.655          0.652
##  6 BSV        1       0.149          0.508
##  7 HT         1       0.499          0.860
##  8 TRX        1       0.546          0.443
##  9 ZEC        1       0.785          0.288
## 10 KNC        1       0.191          0.541
## # ... with 250 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: 260 x 6
## # Groups:   symbol, split [260]
##     symbol split lm_rmse_test lm_rmse_holdout lm_rsq_test lm_rsq_holdout
##     <chr>  <dbl>        <dbl>           <dbl>       <dbl>          <dbl>
##   1 BTC        1        0.319           0.602    0.732          0.748   
##   2 ETH        1        0.432           1.37     0.647          0.563   
##   3 EOS        1        0.322           1.60     0.737          0.289   
##   4 LTC        1        0.739           1.08     0.379          0.523   
##   5 ADA        1        0.641           1.80     0.655          0.652   
##   6 BSV        1        0.570           0.842    0.149          0.508   
##   7 HT         1        0.461           1.30     0.499          0.860   
##   8 TRX        1        0.400           1.45     0.546          0.443   
##   9 ZEC        1        0.349           1.70     0.785          0.288   
##  10 KNC        1        0.752           1.29     0.191          0.541   
##  11 XMR        1        0.580           0.986    0.860          0.717   
##  12 ZRX        1        0.790           2.29     0.412          0.0877  
##  13 BAT        1        0.662           1.33     0.350          0.672   
##  14 BNT        1        0.661           0.812    0.734          0.721   
##  15 CRO        1        0.402           1.44     0.491          0.201   
##  16 MANA       1        0.537           1.88     0.640          0.347   
##  17 DGB        1        0.490           1.38     0.690          0.496   
##  18 ENJ        1        0.774           2.76     0.259          0.348   
##  19 BTM        1        0.580           1.78     0.481          0.230   
##  20 STORJ      1        1.51          110.       0.0344         0.000243
##  21 XEM        1        0.901           2.36     0.924          0.586   
##  22 ARDR       1        0.510           1.57     0.766          0.925   
##  23 BTG        1        0.649           1.06     0.555          0.680   
##  24 KMD        1        0.639           1.34     0.563          0.755   
##  25 ELF        1        0.974           1.11     0.857          0.734   
##  26 NEXO       1        1.36            0.996    0.510          0.742   
##  27 CHZ        1        0.711           1.48     0.331          0.663   
##  28 CKB        1        0.342           6.53     0.912          0.545   
##  29 BRD        1        0.922           2.35     0.318          0.397   
##  30 DCR        1        1.29            0.799    0.416          0.817   
##  31 VIB        1        3.69            1.90     0.164          0.0762  
##  32 WAXP       1        0.481           1.46     0.798          0.284   
##  33 OAX        1        2.54            6.59     0.842          0.254   
##  34 AVA        1        0.764           2.97     0.765          0.125   
##  35 ETP        1        1.71            1.35     0.0759         0.439   
##  36 NAV        1        1.40            3.17     0.972          0.0108  
##  37 JST        1        1.12            1.62     0.0479         0.555   
##  38 SRN        1        0.954           2.50     0.416          0.573   
##  39 LEVL       1        3.80            7.62     0.000655       0.249   
##  40 FTM        1        0.878          25.8      0.520          0.170   
##  41 ASP        1        2.36            4.30     0.0966         0.311   
##  42 CRV        1        1.91            1.26     0.371          0.668   
##  43 SUSHI      1        1.45            3.00     0.796          0.282   
##  44 UNI        1        1.26            0.928    0.236          0.948   
##  45 CUR        1        2.66            4.50     0.996          0.729   
##  46 FTT        1        1.51            1.63     0.583          0.174   
##  47 DOGE       1        1.28            1.58     0.802          0.136   
##  48 BCHA       1        2.73            1.86     0.353          0.694   
##  49 STORJ      2        2.50          110.       0.0598         0.000243
##  50 FTT        2        1.28            1.63     0.0700         0.174   
##  51 VIB        2      NaN               1.90    NA              0.0762  
##  52 LEVL       2       11.1             7.62     0.230          0.249   
##  53 BAT        2        0.645           1.33     0.444          0.672   
##  54 HT         2        1.29            1.30     0.826          0.860   
##  55 CKB        2        0.735           6.53     0.280          0.545   
##  56 ETP        2        1.05            1.35     0.221          0.439   
##  57 OAX        2        3.30            6.59     0.241          0.254   
##  58 TRX        2        0.754           1.45     0.250          0.443   
##  59 ZRX        2        0.759           2.29     0.373          0.0877  
##  60 DGB        2        1.30            1.38     0.319          0.496   
##  61 XMR        2        1.72            0.986    0.882          0.717   
##  62 MANA       2        0.736           1.88     0.571          0.347   
##  63 KMD        2        0.974           1.34     0.412          0.755   
##  64 ELF        2        0.699           1.11     0.610          0.734   
##  65 CHZ        2        3.06            1.48     0.815          0.663   
##  66 CRO        2        0.503           1.44     0.776          0.201   
##  67 BTG        2        0.591           1.06     0.397          0.680   
##  68 ARDR       2        1.46            1.57     0.855          0.925   
##  69 JST        2        0.900           1.62     0.563          0.555   
##  70 SRN        2       16.5             2.50     0.153          0.573   
##  71 FTM        2        0.934          25.8      0.442          0.170   
##  72 BTC        2        0.218           0.602    0.967          0.748   
##  73 EOS        2        0.797           1.60     0.344          0.289   
##  74 ETH        2        0.674           1.37     0.434          0.563   
##  75 BSV        2        0.590           0.842    0.278          0.508   
##  76 ADA        2        1.02            1.80     0.827          0.652   
##  77 BNT        2        0.653           0.812    0.511          0.721   
##  78 ENJ        2        1.25            2.76     0.405          0.348   
##  79 NEXO       2        1.18            0.996    0.711          0.742   
##  80 BRD        2        9.23            2.35     0.334          0.397   
##  81 DCR        2        1.09            0.799    0.803          0.817   
##  82 WAXP       2        0.960           1.46     0.421          0.284   
##  83 NAV        2        2.21            3.17     0.837          0.0108  
##  84 XEM        2        1.36            2.36     0.622          0.586   
##  85 LTC        2        0.294           1.08     0.961          0.523   
##  86 ZEC        2        0.969           1.70     0.653          0.288   
##  87 AVA        2        0.673           2.97     0.776          0.125   
##  88 UNI        2        1.73            0.928    0.654          0.948   
##  89 ASP        2        1.68            4.30     0.751          0.311   
##  90 BTM        2        0.444           1.78     0.739          0.230   
##  91 SUSHI      2        1.60            3.00     0.722          0.282   
##  92 CUR        2       18.9             4.50     0.00144        0.729   
##  93 KNC        2        0.619           1.29     0.619          0.541   
##  94 CRV        2        2.80            1.26     0.105          0.668   
##  95 TON        1        1.84            0.214    0.0180         0.737   
##  96 DOGE       2       11.8             1.58     0.763          0.136   
##  97 FTT        3      NaN               1.63    NA              0.174   
##  98 SUN        1        1.39            2.03     0.632          0.110   
##  99 BCHA       2        2.17            1.86     0.512          0.694   
## 100 INJ        1        2.46            1.60     0.141          0.437   
## 101 STORJ      3        2.49          110.       0.798          0.000243
## 102 TON        2        0.760           0.214    0.787          0.737   
## 103 LEVL       3        7.37            7.62     0.227          0.249   
## 104 BAT        3        1.13            1.33     0.641          0.672   
## 105 ETP        3        1.40            1.35     0.434          0.439   
## 106 HT         3        0.902           1.30     0.400          0.860   
## 107 CUR        3        1.10            4.50     0.860          0.729   
## 108 OAX        3        0.151           6.59     0.576          0.254   
## 109 DGB        3        1.90            1.38     0.118          0.496   
## 110 ZRX        3        2.61            2.29     0.379          0.0877  
## 111 MANA       3        0.961           1.88     0.825          0.347   
## 112 JST        3        0.542           1.62     0.903          0.555   
## 113 TRX        3        1.04            1.45     0.550          0.443   
## 114 BTG        3        2.02            1.06     0.716          0.680   
## 115 ELF        3        1.47            1.11     0.776          0.734   
## 116 CRO        3        0.558           1.44     0.885          0.201   
## 117 ARDR       3        1.38            1.57     0.635          0.925   
## 118 XMR        3        1.80            0.986    0.899          0.717   
## 119 BNT        3        0.872           0.812    0.804          0.721   
## 120 ENJ        3        2.34            2.76     0.807          0.348   
## 121 CHZ        3        1.49            1.48     0.544          0.663   
## 122 DCR        3        0.590           0.799    0.883          0.817   
## 123 BTC        3        1.10            0.602    0.958          0.748   
## 124 ETH        3        1.11            1.37     0.795          0.563   
## 125 EOS        3        1.07            1.60     0.372          0.289   
## 126 ADA        3        0.877           1.80     0.915          0.652   
## 127 BSV        3        1.24            0.842    0.665          0.508   
## 128 KMD        3        1.56            1.34     0.657          0.755   
## 129 NEXO       3        1.53            0.996    0.433          0.742   
## 130 BRD        3        6.17            2.35     0.315          0.397   
## 131 WAXP       3        1.84            1.46     0.734          0.284   
## 132 NAV        3        3.90            3.17     0.439          0.0108  
## 133 XEM        3        0.733           2.36     0.863          0.586   
## 134 LTC        3        1.38            1.08     0.600          0.523   
## 135 ZEC        3        0.673           1.70     0.943          0.288   
## 136 AVA        3        1.35            2.97     0.844          0.125   
## 137 UNI        3        1.43            0.928    0.739          0.948   
## 138 FTM        3        1.56           25.8      0.524          0.170   
## 139 ASP        3        3.09            4.30     0.584          0.311   
## 140 BTM        3        1.15            1.78     0.705          0.230   
## 141 SUSHI      3        1.41            3.00     0.781          0.282   
## 142 CRV        3        4.77            1.26     0.326          0.668   
## 143 CKB        3        1.11            6.53     0.966          0.545   
## 144 SRN        3        9.37            2.50     0.0250         0.573   
## 145 KNC        3        2.08            1.29     0.573          0.541   
## 146 VIB        3        6.87            1.90     0.314          0.0762  
## 147 HYDRA      1        5.70            6.65     0.0508         0.00237 
## 148 DOGE       3        1.18            1.58     0.717          0.136   
## 149 SUN        2        2.80            2.03     0.250          0.110   
## 150 INJ        2        3.60            1.60     0.402          0.437   
## 151 BCHA       3        4.28            1.86     0.00889        0.694   
## 152 STORJ      4        3.63          110.       1              0.000243
## 153 HYDRA      2        1.38            6.65     0.931          0.00237 
## 154 TON        3        0.606           0.214    0.888          0.737   
## 155 CUR        4        4.37            4.50     0.501          0.729   
## 156 SUN        3        0.940           2.03     0.565          0.110   
## 157 BAT        4        3.95            1.33     0.0899         0.672   
## 158 INJ        3        3.57            1.60     0.644          0.437   
## 159 OAX        4        4.61            6.59     0.156          0.254   
## 160 MANA       4        3.77            1.88     0.328          0.347   
## 161 HT         4        1.35            1.30     0.383          0.860   
## 162 CHZ        4        1.41            1.48     0.454          0.663   
## 163 JST        4        1.73            1.62     0.673          0.555   
## 164 BNT        4        0.670           0.812    0.924          0.721   
## 165 CRO        4        0.890           1.44     0.789          0.201   
## 166 ARDR       4        1.70            1.57     0.430          0.925   
## 167 ELF        4        1.31            1.11     0.543          0.734   
## 168 DCR        4        2.01            0.799    0.753          0.817   
## 169 EOS        4        1.05            1.60     0.915          0.289   
## 170 ETH        4        0.616           1.37     0.812          0.563   
## 171 ADA        4        3.12            1.80     0.766          0.652   
## 172 BSV        4        1.14            0.842    0.882          0.508   
## 173 TRX        4        1.50            1.45     0.848          0.443   
## 174 ENJ        4        1.02            2.76     0.800          0.348   
## 175 NEXO       4        1.07            0.996    0.842          0.742   
## 176 BRD        4        3.25            2.35     0.212          0.397   
## 177 WAXP       4        1.13            1.46     0.220          0.284   
## 178 ETP        4        1.37            1.35     0.897          0.439   
## 179 NAV        4        8.40            3.17     0.252          0.0108  
## 180 ZEC        4        0.909           1.70     0.966          0.288   
## 181 BTG        4        0.879           1.06     0.826          0.680   
## 182 XEM        4        0.756           2.36     0.950          0.586   
## 183 BTC        4        0.878           0.602    0.756          0.748   
## 184 LTC        4        0.669           1.08     0.891          0.523   
## 185 XMR        4        0.547           0.986    0.906          0.717   
## 186 AVA        4        1.28            2.97     0.459          0.125   
## 187 DGB        4        0.984           1.38     0.939          0.496   
## 188 UNI        4        1.54            0.928    0.347          0.948   
## 189 ZRX        4        5.96            2.29     0.153          0.0877  
## 190 KMD        4        1.14            1.34     0.466          0.755   
## 191 ASP        4        1.13            4.30     0.881          0.311   
## 192 FTM        4        1.89           25.8      0.732          0.170   
## 193 SUSHI      4        1.18            3.00     0.670          0.282   
## 194 LEVL       4        7.99            7.62     0.437          0.249   
## 195 BTM        4        0.921           1.78     0.890          0.230   
## 196 CRV        4        0.850           1.26     0.761          0.668   
## 197 DOGE       4        2.89            1.58     0.747          0.136   
## 198 CKB        4       10.8             6.53     0.905          0.545   
## 199 BCHA       4        2.61            1.86     0.376          0.694   
## 200 KNC        4        2.08            1.29     0.654          0.541   
## 201 SRN        4        1.93            2.50     0.349          0.573   
## 202 VIB        4        2.10            1.90     0.622          0.0762  
## 203 HYDRA      3        3.86            6.65     0.569          0.00237 
## 204 TON        4        1.21            0.214    0.186          0.737   
## 205 STORJ      5       NA             110.      NA              0.000243
## 206 SUN        4        2.94            2.03     0.344          0.110   
## 207 INJ        4        1.67            1.60     0.847          0.437   
## 208 HYDRA      4        2.27            6.65     0.0526         0.00237 
## 209 OAX        5       NA               6.59    NA              0.254   
## 210 HT         5       NA               1.30    NA              0.860   
## 211 BAT        5       NA               1.33    NA              0.672   
## 212 CHZ        5       NA               1.48    NA              0.663   
## 213 DCR        5       NA               0.799   NA              0.817   
## 214 MANA       5       NA               1.88    NA              0.347   
## 215 CRO        5       NA               1.44    NA              0.201   
## 216 ARDR       5       NA               1.57    NA              0.925   
## 217 BNT        5       NA               0.812   NA              0.721   
## 218 ENJ        5       NA               2.76    NA              0.348   
## 219 BTG        5       NA               1.06    NA              0.680   
## 220 ETH        5       NA               1.37    NA              0.563   
## 221 EOS        5       NA               1.60    NA              0.289   
## 222 ADA        5       NA               1.80    NA              0.652   
## 223 BSV        5       NA               0.842   NA              0.508   
## 224 ZEC        5       NA               1.70    NA              0.288   
## 225 NEXO       5       NA               0.996   NA              0.742   
## 226 BRD        5       NA               2.35    NA              0.397   
## 227 WAXP       5       NA               1.46    NA              0.284   
## 228 NAV        5       NA               3.17    NA              0.0108  
## 229 TRX        5       NA               1.45    NA              0.443   
## 230 XEM        5       NA               2.36    NA              0.586   
## 231 ELF        5       NA               1.11    NA              0.734   
## 232 AVA        5       NA               2.97    NA              0.125   
## 233 UNI        5       NA               0.928   NA              0.948   
## 234 BTC        5       NA               0.602   NA              0.748   
## 235 LTC        5       NA               1.08    NA              0.523   
## 236 ETP        5       NA               1.35    NA              0.439   
## 237 JST        5       NA               1.62    NA              0.555   
## 238 ZRX        5       NA               2.29    NA              0.0877  
## 239 DGB        5       NA               1.38    NA              0.496   
## 240 KMD        5       NA               1.34    NA              0.755   
## 241 XMR        5       NA               0.986   NA              0.717   
## 242 FTM        5       NA              25.8     NA              0.170   
## 243 ASP        5       NA               4.30    NA              0.311   
## 244 SUSHI      5       NA               3.00    NA              0.282   
## 245 CRV        5       NA               1.26    NA              0.668   
## 246 BTM        5       NA               1.78    NA              0.230   
## 247 CKB        5       NA               6.53    NA              0.545   
## 248 FTT        4        5.80            1.63     0.798          0.174   
## 249 LEVL       5       NA               7.62    NA              0.249   
## 250 SRN        5       NA               2.50    NA              0.573   
## 251 KNC        5       NA               1.29    NA              0.541   
## 252 CUR        5       NA               4.50    NA              0.729   
## 253 DOGE       5       NA               1.58    NA              0.136   
## 254 VIB        5       NA               1.90    NA              0.0762  
## 255 BCHA       5       NA               1.86    NA              0.694   
## 256 TON        5       NA               0.214   NA              0.737   
## 257 SUN        5       NA               2.03    NA              0.110   
## 258 INJ        5       NA               1.60    NA              0.437   
## 259 HYDRA      5       NA               6.65    NA              0.00237 
## 260 FTT        5       NA               1.63    NA              0.174

Out of 260 groups, 75 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: 260 x 6
## # Groups:   symbol, split [260]
##    symbol split lm_rmse_test lm_rsq_test lm_rmse_holdout lm_rsq_holdout
##    <chr>  <dbl>        <dbl>       <dbl>           <dbl>          <dbl>
##  1 BTC        1        0.319       0.732           0.602          0.748
##  2 ETH        1        0.432       0.647           1.37           0.563
##  3 EOS        1        0.322       0.737           1.60           0.289
##  4 LTC        1        0.739       0.379           1.08           0.523
##  5 ADA        1        0.641       0.655           1.80           0.652
##  6 BSV        1        0.570       0.149           0.842          0.508
##  7 HT         1        0.461       0.499           1.30           0.860
##  8 TRX        1        0.400       0.546           1.45           0.443
##  9 ZEC        1        0.349       0.785           1.70           0.288
## 10 KNC        1        0.752       0.191           1.29           0.541
## # ... with 250 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: 260 x 3
## # Groups:   symbol [52]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.629
##  2 ETH    lm    0.707
##  3 EOS    lm    0.809
##  4 LTC    lm    0.770
##  5 ADA    lm    1.42 
##  6 BSV    lm    0.884
##  7 HT     lm    1.00 
##  8 TRX    lm    0.924
##  9 ZEC    lm    0.725
## 10 KNC    lm    1.38 
## # ... with 250 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: 260 x 3
## # Groups:   symbol [52]
##    symbol model  rmse
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.602
##  2 ETH    lm    1.37 
##  3 EOS    lm    1.60 
##  4 LTC    lm    1.08 
##  5 ADA    lm    1.80 
##  6 BSV    lm    0.842
##  7 HT     lm    1.30 
##  8 TRX    lm    1.45 
##  9 ZEC    lm    1.70 
## 10 KNC    lm    1.29 
## # ... with 250 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: 260 x 3
## # Groups:   symbol [52]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.853
##  2 ETH    lm    0.672
##  3 EOS    lm    0.592
##  4 LTC    lm    0.708
##  5 ADA    lm    0.791
##  6 BSV    lm    0.493
##  7 HT     lm    0.527
##  8 TRX    lm    0.548
##  9 ZEC    lm    0.837
## 10 KNC    lm    0.509
## # ... with 250 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: 260 x 3
## # Groups:   symbol [52]
##    symbol model   rsq
##    <chr>  <chr> <dbl>
##  1 BTC    lm    0.748
##  2 ETH    lm    0.563
##  3 EOS    lm    0.289
##  4 LTC    lm    0.523
##  5 ADA    lm    0.652
##  6 BSV    lm    0.508
##  7 HT     lm    0.860
##  8 TRX    lm    0.443
##  9 ZEC    lm    0.288
## 10 KNC    lm    0.541
## # ... with 250 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.