glmnet 4.0 negative binomial regression: "Error in seq.default(log(lambda_max), log(lambda_max * lambda.min.ratio), : 'from' must be a finite number"

Viewed 1102

I am trying to run a negative binomial regression using the glmnet 4.0 package. I have implemented the regression using code from the section entitled 'Fitting Other GLMs' of this webpage. However, I keep getting the following error:

Error in seq.default(log(lambda_max), log(lambda_max * lambda.min.ratio), : 'from' must be a finite number

I haven’t been able to find examples of other people experiencing this error in the past. I think maybe because it is specific to this new version of the package?

Below is an example which should reproduce the error. This is not the data I have been using for my analysis and is simply for example purposes.

library(eventdataR)
library(glmnet)
library(MASS)

df <- subset(traffic_fines, activity == "Create Fine" | activity == "Add penalty" )
df <- df[,c(4,6,7,9,13,14,18)]
df$resource <- as.numeric(df$resource)
dfm <- as.matrix(df[,-3])

newfit <- glmnet(dfm, df$amount, family = negative.binomial(theta = 5))

Does anyone know why this error might be occurring and what I can do to stop it?

1 Answers

In the example you provided, there are no rows with no NAs,

table(complete.cases(df))

FALSE 
14635

If we chose some other columns:

df <- subset(traffic_fines, activity == "Create Fine" | activity == "Add penalty" )
df <- df[,c("points","article","amount","resource")]
df = df[complete.cases(df),]
df$resource <- as.numeric(df$resource)
dfm <- as.matrix(df[,-3])

It will run

newfit <- glmnet(dfm, df$amount, family = negative.binomial(theta = 5))

newfit

Call:  glmnet(x = dfm, y = df$amount, family = negative.binomial(theta = 5)) 

   Df  %Dev  Lambda
1   0  0.00 0.46180
2   1  8.23 0.42070
3   1 14.92 0.38340
4   1 20.42 0.34930
Related