Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a GLM gamma regression in Python with R like formulas

I am running a GLM regression in Python using statsmodels using the following code. I specifically want to implement a log link function. I am able to write R like formulas using Statsmodels.

The following code successfully runs but throws up a Deprecation Warning. Can anyone suggest how to get rid of this warning. Thanks for the help.

Code:

mod = smf.glm(formula='y ~  C(x1) + C(x2) + C(x3) + x4 + x5', data=data,family=sm.families.Gamma(link=sm.families.links.log))
reg = mod.fit()
print(reg.summary())

Warning: DeprecationWarning: Calling Family(..) with a link class as argument is deprecated. Use an instance of a link class instead.

like image 855
Data Modeler Avatar asked Oct 20 '25 09:10

Data Modeler


1 Answers

I don't think any of the above answers are correct. The correct method, as instructed by the warning, is to use the corresponding instance of the link function inside the bracket:

mod = smf.glm(formula='y ~  C(x1) + C(x2) + C(x3) + x4 + x5', data=data,family=sm.families.Gamma(link=sm.families.links.log()))
reg = mod.fit()
print(reg.summary())
like image 94
Jinhua Wang Avatar answered Oct 21 '25 22:10

Jinhua Wang