I am using poisson regression and negative binomial regression to fit a dataset. In the meantime, I discovered that the sum of fitted values of poisson regression is exactly the same as the sum of original values, and the sum of fitted values of negative binomial regression is very close to the sum of original values.
I am curious whether this is a coincidence or not. Does poisson regression and negative binomial regression preserve the sum of predicted values? If so, how do they do this?
The dataset that I am perform the fitting is:
> head(example)
N3 N1 w d2
1 157 165 1 1272.929
2 157 233 1 3475.642
3 80 681 5 20867.050
4 635 9281 7 11354.523
5 635 224 3 7782.573
6 635 13708 1 11717.493
The predictors are N1, N3, and d2, while the outcome is w.
> poi_example <- glm(data = example, w ~ log(N1) + log(N3) + log(d2), family = "poisson")
> neg_example <- glm.nb(data = example, w ~ log(N1) + log(N3) + log(d2))
> sum(poi_example$fitted.values)
[1] 2272
> sum(neg_example$fitted.values)
[1] 2271.047
> sum(dat$w)
[1] 2272
As you can see, the sum of fitted values of poisson regression is exactly the same as sum of original values of outcomes (2272), and the sum of fitted values of negative binomial regression is very very close to the sum of original outcomes, but not exactly the same. Another thing, which I didn't show here, is that both model gives terrible fits to the data (the predicted values of each data point deviate by a quite large amount from their original values).
Could anyone tell me what goes on behind the scene where R is training these two models? How do they preserve the sum of predicted values for the outcomes even when the fits are not good?
Thanks