# Data preparation for Poisson regression: use of individual data

Most texts I have read about Poisson-regression assumes that the data is available in an already grouped form, i.e. counts are given for each unique covariate combination. For instance, we have (in R)

DataGrouped<-data.frame(Gender=as.factor(c("M","F")),Counts=c(6,2))
DataGrouped
Gender Counts
1      M      6
2      F      2


thus we can use

glm(Counts~Gender,data=DataGrouped,family=poisson)


to run the Poisson-regression.

However, often we have individual-level data, such as

DataIndividual<-data.frame(PatientID=1:8,Gender=as.factor(c(rep("M",6),rep("F",2))))
DataIndividual
PatientID  Gender
1         1      M
2         2      M
3         3      M
4         4      M
5         5      M
6         6      M
7         7      F
8         8      F


which is clearly identical to the above database.

The question is: how can I run the Poisson-regression on such individual-level database?

Of course, I am aware that I could simply do the counting myself, for example with

glm(Freq~Var1,data=data.frame(table(DataIndividual$Gender)),family=poisson)  but I am interested in whether it is possible without an explicit, manual counting. Especially, whether it is possible to somehow interface DataIndividual directly to glm. • Yes, you can. There is an example here Aug 6, 2014 at 10:36 • @PeterFlom : Well, maybe I am overlooking something, but I couldn't find the example there... The only use of glm I found was glm(num_awards ~ prog + math, family = "poisson", data = p), but here, p is just the grouped data I was speaking of (i.e. already the counts are given), see p$num_awards. My question addresses a situation where we don't have - for instance - num_awards==6, but rather 6 rows with the same StudentID. Aug 6, 2014 at 10:49
• No, p is not grouped. It is one id per line. Try p <- read.csv("http://www.ats.ucla.edu/stat/data/poisson_sim.csv") head(p) Aug 6, 2014 at 11:53
• In that case you will have to modify your data frame prior to using glm. This question should be reworded and posted to StackOverflow where programming questions are asked Aug 6, 2014 at 12:52
• Yes - in other circumstances you may in fact have a truncated Poisson distribution, where you don't know how many zero counts there are. Another potential complication might be where you have the same patient ID coded as male in one row & female in another. I just wanted to point out why, IMO, glm shouldn't be trying to sort out these sort of issues for you. Aug 6, 2014 at 14:04

And, secondary, one should not expect glm (or equivalents in other software than R) to do the tallying. Data can come in many different formats, and (re)arranging the data is a proper format tidying the data should be done prior to analysis. The complications caused by data formats (some discussed in the comments) are orthogonal to the complications of analysis, and should be tackled as a different problem.