Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Error in rep(1,N): invalid 'times' argument when using glmnet

Tags:

r

glmnet

cv.glmnet(x=anscombe$x1, y=anscombe$y1, family= "binomial", 
          type.measure = "class", alpha = 1, nlambda = 100)

This error happened:

Error in rep(1, N) : invalid 'times' argument

like image 381
Zhenyu Zhang Avatar asked Nov 16 '25 15:11

Zhenyu Zhang


1 Answers

If you look at the data, your dependent is continuous so it should be gaussian with mse as measure:

head(anscombe,3)
  x1 x2 x3 x4   y1   y2    y3   y4
1 10 10 10  8 8.04 9.14  7.46 6.58
2  8  8  8  8 6.95 8.14  6.77 5.76
3 13 13 13  8 7.58 8.74 12.74 7.71

The error comes about because you provided a vector when the function expects a matrix, and it doesn't make sense to input 1 variable for glmnet, you are better off doing a regression. If we force it, you get an error, which pretty much sums it up:

cv.glmnet(x=as.matrix(anscombe$x1,ncol=1), y=anscombe$y1, family= "gaussian", 
          type.measure = "mse", alpha = 1, nlambda = 100)

Error in glmnet(x, y, weights = weights, offset = offset, lambda = lambda,  : 
  x should be a matrix with 2 or more columns

If you use more than 1, it works:

cv.glmnet(x=as.matrix(anscombe[,c("x1","x2")]), y=anscombe$y1, family= "gaussian", 
           type.measure = "mse", alpha = 1, nlambda = 100)
like image 60
StupidWolf Avatar answered Nov 18 '25 03:11

StupidWolf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!