3
$\begingroup$

I have developed a glm Poisson model in R and would like to extract the formula so that I can do the computation in another software language. When I write out the equation I am getting different values from the predict function in R and the math I do manually.

summary(model1)

Call:
glm(formula = time ~ dayPart+ weekPart + NumberOfCustomers, 
    family = poisson(link = log), data = trainDf)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-2.0077  -0.6974  -0.1732   0.6231   2.4361  

Coefficients:
                     Estimate Std. Error z value Pr(>|z|)    
(Intercept)          5.471287   0.018404 297.291  < 2e-16 ***
DAY_PARTNight        0.177905   0.015721  11.317  < 2e-16 ***
DAY_PARTEvening      0.106609   0.020109   5.302 1.15e-07 ***
DAY_PARTAfternoon    0.082161   0.016076   5.111 3.21e-07 ***
WEEK_DESCWEEKEND     0.083837   0.012819   6.540 6.16e-11 ***
NumberOfCustomers    0.069795   0.002081  33.545  < 2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for poisson family taken to be 1)

    Null deviance: 1405.299  on 69  degrees of freedom
Residual deviance:   81.949  on 64  degrees of freedom
AIC: Inf

Number of Fisher Scoring iterations: 3

For example I have this row

Morning WEEKDAY 1 (customer) 255 11.463557 (prediction)

calculate:

exp(5.47)+ (0)(1) + (0)(1) + exp(0.069795)*1 = 238

exp(5.47) - the exp of the intercept since I used a log link function
(0)(1) - The coefficient for Weekday since this is the base for WEEK_DESC
(0)(1) - Since morning is base for time of day categorical variable
exp(0.069795) - slope for number of customers.

What is wrong where I'm getting differences between the predict function result of 255 and my math where I calculate 238?

$\endgroup$
13
$\begingroup$

You need to solve for the (entire) linear predictor first, then exponentiate. Consider:

exp(5.471287 + 0.177905*0 + 0.106609*0 + 0.082161*0 + 0.083837*0 + 0.069795*1)
# [1] 254.9537

You are summing the exponentiation of 5.47 and the exponentiation of 0.069795, instead of exponentiating the sum of 5.47 + 0.069795. Because exponentiation is a nonlinear transformation, $f(x+y)\ne f(x)+f(y)$ (cf., Jensen's inequality).

Because this is a Poisson GLiM with a log link, the exponentiation of the intercept is the expected value when all covariates are zero (and the categorical variables are at the reference level). In addition, the exponentiation of a coefficient yields the multiplicative factor by which the expected value will increase with a one-unit increase in the covariate. Thus, in this case you could equivalently do:

exp(5.471287) * exp(0.069795*1)
# [1] 254.9537

or,

exp(5.471287) * exp(0.069795)^1
# [1] 254.9537

But note that you have multiplied here, not added.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.