Search code examples
python-3.xscipystatisticspoisson

Sum of residuals of scipy regression model


I am going through a stats workbook with python, there is a practice hands on question on which i am stuck. Its related to Poisson regression and here is the problem statement:-

Perform the following tasks:

  1. Load the R data set Insurance from MASS package and Capture the data as pandas data frame
  2. Build a Poisson regression model with a log of an independent variable, Holders and dependent variable Claims.
  3. Fit the model with data.
  4. Find the sum of residuals.

I am stuck with point 4 above. Can anyone help with this step?

Here is what i have done so far :-

import statsmodels.api as sm
import statsmodels.formula.api as smf
import numpy as np
df = sm.datasets.get_rdataset('Insurance', package='MASS', cache=False).data
poisson_model = smf.poisson('np.log(Holders) ~ -1 + Claims', df)
poisson_result = poisson_model.fit()
print(poisson_result.summary())

Here is the output so far :-

Now how to get sum of residuals?


Solution

  • np.sum(poisson_result.resid)

    works fine

    You have used the wrong variables to build the poisson model as pointed out by Karthikeyan. Use this instead,

    poisson_model = smf.poisson('Claims ~ np.log(Holders)',df)