0
$\begingroup$

I developed a very simple Ridge Regression with glmnet, the R package. When I use the plot.glmnet() function I encounter strange errors:

  plot.glmnet(ridge_model, xvar = "lambda", label = TRUE)

      Error in if(nr ==1) {: argument is of length zero

This error is related to IF statements and loops. It does not even seem related to the graph. Even when I use this simplest coding that should work according to the documentation

plot.glmnet(ridge_model)

I still get the same error.

Below I am representing a more complete picture of the script I have used:

library(glmnet)
library(Matrix)
library(foreach)

attach(data)

x = as.matrix(data[, c(3,2)])
y = as.matrix(data[, c(1)])

# Setting up the cross validation of a Ridge Regression
set.seed(101)
cv.glmnet(x, y, type.measure = "mse", alpha = 0)

# Plotting the graph of the Lambda values vs. the MSEs.  This works just fine 
ridge1 <- cv.glmnet(x, y, type.measure = "mse", alpha = 0)
plot.cv.glmnet(ridge1, main = "Log(Lambda) vs MSE")

# Attempting to extract a graph of coefficients.  
# Either simple or more complex coding generates same error.
plot.glmnet(ridge1) 
plot.glmnet(ridge1, xvar="lambda", label = TRUE)

$\endgroup$
  • $\begingroup$ try xvar=lambda (i.e. without the quotation marks) $\endgroup$ – I'mAnAccountantIKnowAlotOfMath Apr 5 '19 at 3:03
  • $\begingroup$ xvar=lambda generates the error: object 'lambda' not found. $\endgroup$ – Sympa Apr 5 '19 at 18:03
  • $\begingroup$ We're going to need a lot more information on your script to be able to figure this one out. The error is almost certainly due to something "behind the scenes" in plot.glmnet() creating the graph, and seems like an input validation condition where "nr" means "number of rows" or something similar. Can you include some code used to generate ridge_model? $\endgroup$ – Upper_Case Apr 5 '19 at 19:10
  • $\begingroup$ Is there anyway to share an Excel file that represents the data and an R/R Studio script file? The mentioned files are actually pretty small. But, just sharing the codes in absence of the actual data that is too big to impart in just code rows, the review of this situation is a bit difficult. I would think or hope that the error I have encountered is rather common. But, I don't know that for sure. $\endgroup$ – Sympa Apr 6 '19 at 0:18
  • $\begingroup$ @Sympa The problem is that the error is not very specific. I've already offered my best guess about what's going on, but that's as far as the information you've posted can take us. The data itself is not very useful, because the error is at least as likely to be due to your R code as it is to be a problem with the underlying data. I'd even say moreso, because the problem seems to be with the ridge_model object, which you were able to generate from your data. In either case, the R script will be vastly more useful than your data set. $\endgroup$ – Upper_Case Apr 8 '19 at 17:42

Your Answer

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

Browse other questions tagged or ask your own question.