Home > OS >  Is there an alternative way to fit Poisson model in R besides using glm?
Is there an alternative way to fit Poisson model in R besides using glm?

Time:02-15

I get an error when I try to fit Poisson model in a given dataset in R. Am struggling to understand the cause of the error.

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)

I get the error:

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

CodePudding user response:

You might be confused. You can't fit a Poisson to a categorical response. You could fit a Poisson to binary data after converting survival "yes"/"no" to 0/1, but it doesn't really make sense:

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

The sensible thing to do (probably) is to cross-tabulate and use the value e.g.

cc <- as.data.frame(table(titanic))
glm(Freq ~ ., data = cc, family = poisson)
  •  Tags:  
  • r glm
  • Related