当我尝试在 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/