I am running into an error trying to train a caret model with method='glmnet.' My data is continuous and I am trying to do a LASSO regression, but the existence of a 0 for one of the observations is causing the error "Metric RMSE not applicable for classification models." When I remove the 0 observation, everything works fine. The code below reproduces the error.


x_t = matrix(rnorm(100*20), 100, 20)
colnames(x_t) = paste("X", 1:ncol(x_t), sep="")

y_t = matrix(rnorm(100))
y_t[30] = 0

sample_lasso = caret::train(method="glmnet", x=x_t, y=y_t) #throws error: Metric RMSE not applicable for classification models
sample_lasso = caret::train(method="glmnet", x=x_t[-30, ], y=y_t[-30]) #works as expected

Any help would be much appreciated. It's quite strange, as caret seemingly recognizes this is a regression problem by automatically setting metric="RMSE." Thanks in advance!


1 Answer 1


The zero value is not causing the error; setting y_t[30] to any other numeric value yields the same error. If you supply a matrix (y_t) to the y argument, that causes an error. In your second attempt, you turned the matrix into a vector (y_t[-30] will turn the matrix into a vector and remove the 30th element). E.g., try:

> class(y_t)
[1] "matrix" "array" 
> class(y_t[-30])
[1] "numeric"
> sample_lasso <- train(method = "glmnet", x = x_t, y = y_t[ , 1]) # works as expected

As an aside, make sure to set the random seed before you generate data and fit models depending on cross validation.

  • $\begingroup$ Thank you so much! This fixed my error. I was instinctively turning my response variable into a column vector as some of the other models I'm working with required it. I really appreciate your fast and concise response! $\endgroup$
    – Andrew
    Apr 13, 2021 at 5:04
  • $\begingroup$ @Andrew You're welcome! If you accept the answer, the question can be marked as resolved. $\endgroup$ Apr 13, 2021 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.