Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 478
AlphaBetaGamma Avatar asked Oct 30 '25 13:10

AlphaBetaGamma


2 Answers

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)

like image 126
Karthikeyan C.N. Avatar answered Nov 01 '25 03:11

Karthikeyan C.N.


Try below code for Fresco play

import statsmodels.api as sm
import statsmodels.formula.api as smf
import pandas as pd
import numpy as np
df_insurance=sm.datasets.get_rdataset("Insurance","MASS")
df_data=df_insurance.data
insurance_model=smf.poisson('Claims ~ np.log(Holders)', df_data).fit()
print(np.cumsum(insurance_model.resid))
like image 25
Suman Avatar answered Nov 01 '25 03:11

Suman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!