glmnet 4.0 负二项式回归:"Error in seq.default(log(lambda_max), log(lambda_max * lambda.min.ratio), : 'from' must be a finite number"

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"

我正在尝试 运行 使用 glmnet 4.0 package. I have implemented the regression using code from the section entitled 'Fitting Other GLMs' of this webpage 进行负二项式回归。但是,我不断收到以下错误:

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

我无法找到其他人过去遇到此错误的示例。我想也许是因为它特定于这个新版本的软件包?

下面是一个应该重现错误的示例。这不是我一直用于分析的数据,仅用于示例目的。

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))

有谁知道为什么会发生此错误以及我可以采取什么措施来阻止它?

在您提供的示例中,没有不带 NA 的行,

table(complete.cases(df))

FALSE 
14635

如果我们选择其他一些列:

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])

会运行

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