SVEMnet Vignette

Andrew T. Karl

December 07, 2024

Version

version 1.2.1

Summary

SVEMnet implements Self-Validated Ensemble Models (SVEM, Lemkus et al. 2021) and the SVEM whole model test (Karl 2024) using Elastic Net regression via the glmnet package Friedman et al. (2010). This vignette provides an overview of the package’s functionality and usage.

Preface - Note from the author

The motivation to create the SVEMnet package was primarily to have a personal sandbox to explore SVEM performance in different scenarios and with various modifications to its structure. I did not originally intend to publish it, but after having used it for a while I believe it could be useful to others.

As noted in the documentation, I used GPT o1-preview to help form the code structure of the package and to code the Roxygen structure of the documentation. The SVEM significance test R code comes from the supplementary material of Karl (2024). I wrote that code by hand and validated each step (not including the creation of the SVEM predictions) against corresponding results in JMP (the supplementary material of Karl (2024) provides the matching JSL script). For the SVEMnet() code, assuming only a single value of alpha for glmnet is being tested, the heart of the SVEM code is simply

#partial code for illustration of the SVEM loop
coef_matrix <- matrix(NA, nrow = nBoot, ncol = p + 1)
 for (i in 1:nBoot) {
      U <- runif(n)
      w_train <- -log(U)
      w_valid <- -log(1 - U)
      #match glmnet normalization of training weight vector
      w_train <- w_train * (n / sum(w_train))
      w_valid <- w_valid * (n / sum(w_valid))
      glmnet(
          X, y_numeric,
          alpha = alpha,
          weights = w_train,
          intercept = TRUE,
          standardize = standardize,
          maxit = 1e6,
          nlambda = 500
      )
      predict(fit, newx = X)
      val_errors <- colSums(w_valid * (y_numeric - pred_valid)^2)
      k_values <- fit$df
      n_obs <- length(y_numeric)
      aic_values <- n_obs * log(val_errors / n_obs) + 2 * k_values
         # Choose lambda
      if (objective == "wSSE") {
        idx_min <- which.min(val_errors)
        lambda_opt <- fit$lambda[idx_min]
        val_error <- val_errors[idx_min]
      } else if (objective == "wAIC") {
        idx_min <- which.min(aic_values)
        lambda_opt <- fit$lambda[idx_min]
        val_error <- aic_values[idx_min]
      }
      coef_matrix[i, ] <- as.vector(coef(fit, s = lambda_opt))
}

However, to get this to a stable implementation that includes error and warning handling and structure to pass to S3 methods for predict(), coef(), plot(), etc, it was only practical for me to utilize help from GPT o1-preview. I simply would not have taken the time to add that structure otherwise, and my implementation would have been inferior. I reviewed any of the code that was generated from this tool before integrating it, and corrected its occasional mistakes. If someone would like to create a purely human-written set of code for a similar purpose, let me know and I will be happy to add links to your package and a description to the SVEMnet documentation.

SVEMnet Example 1

library(SVEMnet)

# Example data
data <- iris
svem_model <- SVEMnet(Sepal.Length ~ ., data = data, nBoot = 300)
coef(svem_model)
##                   Percent of Bootstraps Nonzero
## Sepal.Width                           1.0000000
## Petal.Length                          1.0000000
## Speciesvirginica                      0.9333333
## Petal.Width                           0.9166667
## Speciesversicolor                     0.9066667

Generate a plot of actual versus predicted values:

plot(svem_model)

Predict outcomes for new data using the predict() function:

predictions <- predict(svem_model, data)
print(predictions)
##        1        2        3        4        5        6        7        8 
## 5.006624 4.741843 4.774083 4.868471 5.059580 5.383770 4.925820 5.027340 
##        9       10       11       12       13       14       15       16 
## 4.688886 4.896319 5.186208 5.101012 4.769690 4.548674 5.124060 5.501206 
##       17       18       19       20       21       22       23       24 
## 5.089080 4.978776 5.358661 5.211317 5.174684 5.130513 4.764891 5.038184 
##       25       26       27       28       29       30       31       32 
## 5.322029 4.889187 5.045316 5.080296 4.953667 4.995100 4.942143 4.971644 
##       33       34       35       36       37       38       39       40 
## 5.425881 5.377317 4.868471 4.700410 4.932951 5.087428 4.668170 5.027340 
##       41       42       43       44       45       46       47       48 
## 4.905103 4.269629 4.774083 5.042577 5.478158 4.713995 5.312837 4.847755 
##       49       50       51       52       53       54       55       56 
## 5.186208 4.900711 6.468361 6.293169 6.534901 5.503897 6.155016 6.137039 
##       57       58       59       60       61       62       63       64 
## 6.465621 5.124691 6.263668 5.614202 5.060211 5.966239 5.534485 6.309492 
##       65       66       67       68       69       70       71       72 
## 5.526945 6.194388 6.187256 5.872938 5.763606 5.591833 6.430642 5.768678 
##       73       74       75       76       77       78       79       80 
## 6.217164 6.312232 6.042651 6.141432 6.330208 6.499922 6.134300 5.377948 
##       81       82       83       84       85       86       87       88 
## 5.465205 5.419380 5.669898 6.442573 6.187256 6.371233 6.387557 5.798586 
##       89       90       91       92       93       94       95       96 
## 5.948263 5.609809 5.985303 6.288776 5.690614 5.071735 5.863066 6.049783 
##       97       98       99      100      101      102      103      104 
## 5.968979 6.042651 4.928783 5.842350 6.965208 6.151508 6.844059 6.653630 
##      105      106      107      108      109      110      111      112 
## 6.742539 7.359765 5.659258 7.169336 6.589150 7.197749 6.388441 6.298853 
##      113      114      115      116      117      118      119      120 
## 6.549370 5.944075 6.065225 6.452242 6.632914 7.829239 7.313261 5.924446 
##      121      122      123      124      125      126      127      128 
## 6.746931 6.029272 7.355373 6.032012 6.855583 7.107188 6.011295 6.190880 
##      129      130      131      132      133      134      135      136 
## 6.517130 6.909627 6.941187 7.663918 6.489282 6.315856 6.606153 6.935708 
##      137      138      139      140      141      142      143      144 
## 6.751323 6.685870 6.117208 6.528654 6.592455 6.251941 6.151508 6.894276 
##      145      146      147      148      149      150 
## 6.744192 6.272657 5.971923 6.356201 6.631827 6.338225

Whole Model Significance Testing

This is the serial version of the significance test. It is slower but the code is less complicated to read than the faster parallel version.

test_result <- svem_significance_test(Sepal.Length ~ ., data = data)
print(test_result)
plot(test_result)
SVEM Significance Test p-value:
[1] 0
Whole model test result

Whole model test result

Note that there is a parallelized version that runs much faster

test_result <- svem_significance_test_parallel(Sepal.Length ~ ., data = data)
print(test_result)
plot(test_result)
SVEM Significance Test p-value:
[1] 0

SVEMnet Example 2

# Simulate data
set.seed(1)
n <- 25
X1 <- runif(n)
X2 <- runif(n)
X3 <- runif(n)
X4 <- runif(n)
X5 <- runif(n)

#y only depends on X1 and X2
y <- 1 + X1 +  X2 + X1 * X2 + X1^2 + rnorm(n)
data <- data.frame(y, X1, X2, X3, X4, X5)

# Perform the SVEM significance test
test_result <- svem_significance_test_parallel(
  y ~ (X1 + X2 + X3)^2 + I(X1^2) + I(X2^2) + I(X3^2),
  data = data

)

# View the p-value
print(test_result)
SVEM Significance Test p-value:
[1] 0.009399093


test_result2 <- svem_significance_test_parallel(
  y ~ (X1 + X2 )^2 + I(X1^2) + I(X2^2),
  data = data
)

# View the p-value
print(test_result2)
SVEM Significance Test p-value:
[1] 0.006475736

#note that the response does not depend on X4 or X5
test_result3 <- svem_significance_test_parallel(
  y ~ (X4 + X5)^2 + I(X4^2) + I(X5^2),
  data = data
)

# View the p-value
print(test_result3)
SVEM Significance Test p-value:
[1] 0.8968502

# Plot the Mahalanobis distances
plot(test_result,test_result2,test_result3)
Whole Model Test Results for Example 2

Whole Model Test Results for Example 2

Simulations to select SVEMnet settings

There are many particular scenarios that we might be interested in focusing on in order to optimize SVEMnet settings. Perhaps a certain number of factors with a certain number of interactions, etc. However, when setting a default for a software, we want it to work well over a wide range of scenarios that might be encountered.

Our simulations target a response surface model in p factors. For a selected density \(d\in[0,1]\), n_active <- max(1, floor(p * d)) of the \(\frac{(p+1)(p+2)}{2}\) parameters in the RSM are set to rexp(1)-rexp(1). There are n points in the Latin hypercube design. This is not an endorsement of the Latin hypercube method for designed experiments: it is merely used as a quick way to generate space filling points for the simulation. It would also be possible to run the simulation using optimal designs or other space filling approaches (such as Fast Flexible Filling, Jones and Lekivetz (2014)). However, for supersaturated settings (where \(n<\frac{(p+1)(p+2)}{2}\)) the optimal designs would require additional work to specify, and that is not needed for this simulation.

The models are trained on n observations and compared to an independent test set with n_holdout observations.

# Define vectors for p, d, n, sd
p_values <- seq(3, 6, 1)        # Number of parameters
d_values <- seq(0.1, 0.9, 0.1)  # Density (proportion of active parameters)
n_values <- seq(15, 50, 5)      # Number of design points
sd_values <- c(.25,0.5, 1, 1.5)       # Standard deviations of noise

nSim <- 20                  # Number of simulations per setting
n_holdout <- 1000               # Number of holdout points

# Create a grid of all combinations of p, d, n, sd
param_grid <- expand.grid(p = p_values, d = d_values, n = n_values, sd = sd_values)

Simulation 1

First we compare the log root mean squared error (LRMSE) on the holdout set for four different models corresponding to the combinations of objective={"wAIC","wSSE"} and debias={TRUE,FALSE}. Lemkus (2021) uses objective={"wSSE"} and debias=FALSE. JMP uses objective={"wSSE"} and debias=TRUE. Based on the simulations below, SVEMnet defaults to objective={"wAIC"} and debias=FALSE. Note that this is not a commentary on JMP’s settings or a statement about globally optimal SVEM settings. These are simply the combinations that SVEMnet seems to work best with over the tested scenarios.

The script for this simulation is available here. Note that this script was generated with GPT o1-preview. The first plot shows the test LRMSE for each of the four models.
LRMSE for {debias}x{objective}

LRMSE for {debias}x{objective}

The next plot shows the residuals of LRMSE after removing the mean LRMSE of the four models over each simulation. This generates a paired comparison. Notice that the model using objective="wAIC" and debias=FALSE performs best in SVEMnet.
Paired LRMSE for {debias}x{objective}

Paired LRMSE for {debias}x{objective}

Simulation 2

The second simulation compares performance across the weight_scheme argument of SVEMnet. weight_scheme="Identity" corresponds to the single-shot (traditional) Lasso (when glmnet_alpha=1) fit on the training data. It is fit with nBoot=1. weight_scheme="FWR" corresponds to fractional weight regression (Xu et al. (2020)) and uses the same exponential weights for the training data as weight_scheme="SVEM", but it uses the exact same weights for validation and does not compute anti-correlated validation weights as SVEM does (Lemkus et al. (2021)). SVEM and Identity are used with nBoot=200 and all models are fit with objective="wAIC" and debias=FALSE.

The script for this simulation is available here. Note that this script was generated with GPT o1-preview. The first plot shows the test LRMSE for each of the three models.

LRMSE for different weight_scheme

LRMSE for different weight_scheme

The next plot shows the residuals of LRMSE after removing the mean LRMSE of the three models over each simulation. This generates a paired comparison. Notice that SVEM outperforms the single-shot AIC lasso and fractional weight regression. It is somewhat surprising that the single-shot AIC lasso outperforms the FWR lasso, but this could have to do with the wide range of settings included in the simulation. For example, when p=6 there are 28 parameters in the RSM, and when d=0.9, 25 of them are active. Some of the simulations include as few as 15 runs, so this is an extreme case of fitting a supersaturated design where a larger-than-expected proportion of the parameters are active. Interested readers are encouraged to modify the simulation code to focus on scenarios of more personal interest, perhaps focusing on less extreme situations.
Residual LRMSE for different weight_scheme

Residual LRMSE for different weight_scheme

References and Citations

  1. Lemkus, T., Gotwalt, C., Ramsey, P., & Weese, M. L. (2021). Self-Validated Ensemble Models for Elastic Net Regression.
    Chemometrics and Intelligent Laboratory Systems, 219, 104439.
    DOI: 10.1016/j.chemolab.2021.104439

  2. Karl, A. T. (2024). A Randomized Permutation Whole-Model Test for SVEM.
    Chemometrics and Intelligent Laboratory Systems, 249, 105122.
    DOI: 10.1016/j.chemolab.2024.105122

  3. Friedman, J. H., Hastie, T., & Tibshirani, R. (2010). Regularization Paths for Generalized Linear Models via Coordinate Descent.
    Journal of Statistical Software, 33(1), 1–22.
    DOI: 10.18637/jss.v033.i01

  4. Gotwalt, C., & Ramsey, P. (2018). Model Validation Strategies for Designed Experiments Using Bootstrapping Techniques With Applications to Biopharmaceuticals.
    JMP Discovery Conference.
    Link

  5. Ramsey, P., Gaudard, M., & Levin, W. (2021). Accelerating Innovation with Space-Filling Mixture Designs, Neural Networks, and SVEM.
    JMP Discovery Conference.
    Link

  6. Ramsey, P., & Gotwalt, C. (2018). Model Validation Strategies for Designed Experiments Using Bootstrapping Techniques With Applications to Biopharmaceuticals.
    JMP Discovery Summit Europe.
    Link

  7. Ramsey, P., Levin, W., Lemkus, T., & Gotwalt, C. (2021). SVEM: A Paradigm Shift in Design and Analysis of Experiments.
    JMP Discovery Summit Europe.
    Link

  8. Ramsey, P., & McNeill, P. (2023). CMC, SVEM, Neural Networks, DOE, and Complexity: It’s All About Prediction.
    JMP Discovery Conference.

  9. Karl, A., Wisnowski, J., & Rushing, H. (2022). JMP Pro 17 Remedies for Practical Struggles with Mixture Experiments.
    JMP Discovery Conference.
    Link

  10. Xu, L., Gotwalt, C., Hong, Y., King, C. B., & Meeker, W. Q. (2020). Applications of the Fractional-Random-Weight Bootstrap.
    The American Statistician, 74(4), 345–358.
    Link

  11. Karl, A. T. (2024). SVEMnet: Self-Validated Ensemble Models with Elastic Net Regression.
    R package version 1.1.1.

  12. JMP Help Documentation Overview of Self-Validated Ensemble Models.
    Link