topepo / caret

caret (Classification And Regression Training) R package that contains misc functions for training and plotting classification and regression models
http://topepo.github.io/caret/index.html
1.61k stars 634 forks source link

rqPen changes in version 3.1.x - `rqPen::rq.lasso.fit()` and `rqPen::rq.nc.fit()` should be `rqPen::rq.pen()` #1340

Open ghost opened 1 year ago

ghost commented 1 year ago

Error sources:

Error description:

The current fit functions for models using the rqPen package ("rqlasso" and "rqnc" are using the rqPen 3.0 fitting method:

# From rqlasso.R
fit = function(x, y, wts, param, lev, last, classProbs, ...) {
  rqPen::rq.lasso.fit(as.matrix(x), y, lambda = param$lambda, ...)
}
# From rqnc.R
fit = function(x, y, wts, param, lev, last, classProbs, ...) {
  rqPen::rq.nc.fit(as.matrix(x), y,
                          lambda = param$lambda,
                          penalty = as.character(param$penalty), ...)
}

(From rqPen NEWS, 2023-02-20): Older functions that were deprecated in rqPen 3.0 are no longer exported. Big changes are rq.pen() should be used instead of rq.lasso.fit() or rq.nc.fit(). Similarly rq.group.pen() should be used instead of rq.group.fit(). Finally rq.pen.cv() and rq.group.pen.cv() should be used instead of cv.rq.pen() and cv.rq.group.pen().

Reproducible Example:


set.seed(123) # for reproducibility

# Create data
n <- 100 # number of observations
p <- 5 # number of predictors

# Generate predictors
X <- matrix(rnorm(n*p), n, p)
colnames(X) <- paste0("X", 1:p)

# Generate response variable
beta <- rnorm(p)
y <- X %*% beta + rnorm(n)

# Combine predictors and response into a data.frame
df <- data.frame(y = y, X)

# Fit using quantile regression with LASSO regularization 
fit <- caret::train(
    y ~ ., 
    data = df, 
    method = "rqlasso", # Also "rqnc", which also uses `rqPen::rq.pen`
    trControl = caret::trainControl(method="cv", number=5),
    preProcess = c("center", "scale"),
    metric = "RMSE"
)

## 1: `rq.lasso.fit()` was deprecated in rqPen 3.0.
## ℹ Please use `rq.pen()` instead.
sessionInfo()

## R version 4.2.0 (2022-04-22 ucrt)
## Platform: x86_64-w64-mingw32/x64 (64-bit)
## Running under: Windows 10 x64 (build 22621)
## 
## Matrix products: default
## 
## locale:
## [1] LC_COLLATE=English_United States.utf8 
## [2] LC_CTYPE=English_United States.utf8   
## [3] LC_MONETARY=English_United States.utf8
## [4] LC_NUMERIC=C                          
## [5] LC_TIME=English_United States.utf8    
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods  
## [7] base     
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.10        lattice_0.21-8     rbibutils_2.2.13  
##  [4] hqreg_1.4          MASS_7.3-60        grid_4.2.0        
##  [7] plyr_1.8.8         MatrixModels_0.5-1 lifecycle_1.0.3   
## [10] hrqglas_1.1.0      Rdpack_2.4         rlang_1.1.0       
## [13] cli_3.6.1          SparseM_1.81       data.table_1.14.8 
## [16] rstudioapi_0.14    Matrix_1.5-4.1     splines_4.2.0     
## [19] tools_4.2.0        rqPen_3.1.3        survival_3.5-5    
## [22] parallel_4.2.0     compiler_4.2.0     quantreg_5.95     

Solution

Either install an older version of rqPen (i.e., devtools::install_version("rqPen", version = "3.0.1", repos = "http://cran.us.r-project.org")), or else update rqPen::rq.lasso.fit() and rqPen::rq.nc.fit() to rqPen::rq.pen():

fit = function(x, y, wts, param, lev, last, classProbs, ...) {
  rqPen::rq.pen(as.matrix(x), y, lambda = param$lambda, ...)
}