尝试使用 R 中的 MASS 包进行 运行 负二项式回归

Attempting to run a negative binomial regression using the MASS package in R

我正在尝试 运行 对以下项进行负二项式回归:

df <- structure(list(Year = c("2018", "2018", "2018", "2018", "2018", 
"2018", "2018", "2018", "2018", "2018", "2018", "2018", "2019", 
"2019", "2019", "2019", "2019", "2019"), Month = c("1", "10", 
"11", "12", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", 
"3", "4", "5", "6"), count = c(109L, 91L, 73L, 74L, 94L, 113L, 
92L, 100L, 114L, 111L, 106L, 86L, 116L, 92L, 94L, 84L, 78L, 98L
), year_mon = c("2018 - 1", "2018 - 10", "2018 - 11", "2018 - 12", 
"2018 - 2", "2018 - 3", "2018 - 4", "2018 - 5", "2018 - 6", "2018 - 7", 
"2018 - 8", "2018 - 9", "2019 - 1", "2019 - 2", "2019 - 3", "2019 - 4", 
"2019 - 5", "2019 - 6")), row.names = c(NA, -18L), groups = structure(list(
    Year = c("2018", "2019"), .rows = structure(list(1:12, 13:18), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

我假设这是除了泊松回归之外最好的回归技术,但我 运行 以下....

library(MASS)
summary(glm.nb(count ~ year_mon, data=df))

..并得到这个错误...

Error in while ((it <- it + 1) < limit && abs(del) > eps) { : 
        missing value where TRUE/FALSE neededError in while ((it <- it + 1) < limit && abs(del) > eps) { : 
                missing value where TRUE/FALSE needed

不确定我到底做错了什么。为此,我并不完全依赖于 Negative Binom,但我希望另一个模型可以与泊松进行比较,这看起来很合适。

正如@rawr 所说,您需要将预测变量转换为某种数值:否则分类预测变量的每个级别都有一个点。这有效,例如:

glm.nb(count~as.numeric(factor(year_mon)), data=df)

...虽然首先修改数据框中的变量(或在数据框中创建一个新变量)而不是即时进行转换可能 better/more 可读