r - 除了使用glm之外,还有其他方法可以在R中拟合泊松模型吗?

标签 r glm

当我尝试在 R 中的给定数据集中拟合泊松模型时,出现错误。我很难理解错误的原因。

library(COUNT)  # Titanic dataset
data("titanic")
library(tidyverse)

# Number of missing values
titanic %>%
  map_int(~sum(is.na(.)))


# Fit the Poisson regression model

poifit <- glm(survived ~ class, family = poisson, data = titanic)


titanic2 <- titanic %>%
  mutate(across(.cols = everything(), ~as.factor(.)))

poifit2 <- glm(survived ~ class, family = poisson, data = titanic2)

我收到错误:

Warning in Ops.factor(y, 0) : ‘<’ not meaningful for factors
Error in if (any(y < 0)) stop("negative values not allowed for the 'Poisson' family") : 
  missing value where TRUE/FALSE needed

最佳答案

你可能会感到困惑。您无法将泊松拟合到绝对响应。在将生存"is"/“否”转换为 0/1 后,您可以将泊松拟合到二进制数据,但这实际上没有意义:

glm(as.numeric(survived=="no") ~ class, family = poisson, data = titanic)

明智的做法(可能)是交叉制表并使用该值,例如

cc <- as.data.frame(table(titanic))
glm(Freq ~ ., data = cc, family = poisson)

关于r - 除了使用glm之外,还有其他方法可以在R中拟合泊松模型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71117739/

相关文章:

r - 如何用因子子集()?

r - 如何在同一数据子集上更新 `lm` 或 `glm` 模型?

r - glmer : Error in eval(expr, envir, enclos) 中的错误:找不到有效的起始 > 值:请指定一些

r - R 中的 Bigglm : Limitations and Improvements of source code (E. g。调用 Fortran)

r - 将空条添加到(百分比)条形图(从长数据格式生成)

arrays - 由于n-1维数组且没有循环,如何从n维数组中提取n-1维数组?

r - data.table 避免回收

r - logLik.lm() : Why does R use (p + 1) instead of p for degree of freedom?

R Highcharter : tooltip customization

r - 如何从多个特定索引范围返回多行?