0
$\begingroup$

I'm just exploring Generalized Linear Models for the first time, and trying to see if I can correctly fit the simplest model I can think of. So I'm generating random values from some NegativeBinomial distribution and then fitting it with a GLM.

np.random.seed(5555555)
weeks = np.arange(np.datetime64("2020-01-01"),np.datetime64("2022-12-01"),np.timedelta64(1,'W'))
counts = pd.Series(nbinom.rvs(10, .5, size=weeks.shape[0]))
counts.index=weeks
obs, last = counts[0:-1], counts[-1:]

X = np.array(obs.index.values.tolist())
X = np.stack((X, np.ones(X.shape[0])),axis=1)
X.shape
y = obs
mdl = GLM(y, X, family=NegativeBinomialFamily())
results = mdl.fit()
results.summary()

I would think that the slope for my one independent variable of 'date' should just be 0.

But this code always gives me a very small positive value:

 Generalized Linear Model Regression Results Dep. Variable:     y   No. Observations:   152


Model:  GLM     Df Residuals:   151
Model Family:   NegativeBinomial    Df Model:   0
Link Function:  Log     Scale:  1.0000
Method:     IRLS    Log-Likelihood:     -504.15
Date:   Thu, 04 May 2023    Deviance:   28.334
Time:   10:51:39    Pearson chi2:   24.8
No. Iterations:     4   Pseudo R-squ. (CS):     -0.001740


Covariance Type:    nonrobust

coef  std           err         z       P>|z|   [0.025  0.975]
x1    1.396e-18     5.25e-20    26.594  0.000   1.29e-18    1.5e-18
const   8.594e-37   3.23e-38    26.594  0.000   7.96e-37    9.23e-37

What's going on here? It looks like the model is very confident the model has an itty bitty positive slope.

Am I setting up the model wrong? Am I interpreting the output wrong?

$\endgroup$
14
  • $\begingroup$ Do you mean the 1.405e-18? $\endgroup$
    – Dave
    Commented May 4, 2023 at 14:43
  • $\begingroup$ yes, that's what I mean. shouldn't that be 0? Or maybe something small because of random sampling, but 0 should be in the confidence interval? $\endgroup$
    – BlueHarp
    Commented May 4, 2023 at 14:48
  • 1
    $\begingroup$ @kuku You confuse size with precision. That p-value has a representational precision of 52 bits, just as any IEEE double precision number does provided it's not too tiny (around $10^{-308}$). It likely has some imprecision introduced by the numerical algorithm, but generally we can expect 8 - 13 decimal digits of precision in any p-value regardless of its size. This value is not "equivalent to zero" for all practical purposes, because sometimes such purposes combine that value with others in ways that make even such a tiny value of material importance. $\endgroup$
    – whuber
    Commented May 4, 2023 at 15:26
  • 3
    $\begingroup$ Nate, by expressing your question in code you make it inaccessible to anyone not conversant with the language and willing to run your code. That's a severe limitation of the audience. It would be better to explain what you want the code to do and to state explicitly, clearly, and fully why you think its output might be incorrect or surprising. $\endgroup$
    – whuber
    Commented May 4, 2023 at 15:28
  • 1
    $\begingroup$ @whuber It's not clear to me that a p-value will actually have the precision you state; typically an optimization algorithm will stop at some precision far short of that (for good reason), and subsequently calculations that depend on that optimization will not have any higher precision. Consequently we might just be dealing with the particular conditions under which the GLM function decides to stop when trying to optimize parameter estimates. $\endgroup$
    – Glen_b
    Commented May 4, 2023 at 23:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.