glmfit equivalent of matlab in python

659 views
Skip to first unread message

nitesh ranjan

unread,
Jan 20, 2015, 1:03:05 PM1/20/15
to pystat...@googlegroups.com
Hi all,
Iam new to python and trying to replicate my codes from matlab to python. For one of my statistical analysis in matlab i use the following code

[b, dev, stats ] = glmfit(U, A, 'binomial', 'link', 'logit');
%calculate fit-function
[yfit, yhi, ylo] = glmval(b,U,'logit', stats);

Where "U" is a measurement and "A" is a binary response to this measurement. Both U and A are vectors. "yfit" is a fit function on the measured data and yhi and ylo gives teh confidence interval.  

I am trying to get transformed in python. What i manage was

glm_binom = sm.GLM(A,U, family=sm.families.Binomial())

glm_binom_fit=glm_binom.fit()


I think this gives me a fit on "U" and "A". How can i get the fit function after this step and calculate my confidence interval.

Thanks
Nitesh

josef...@gmail.com

unread,
Jan 20, 2015, 1:18:44 PM1/20/15
to pystatsmodels
IIUC

The fitted values, i.e. probabilities, for the explanatory variables are available as glm_binom_fit.fittedvalues
(aside discrete.Logit doesn't have the probabilities in fittedvalues)

For new explanatory variables, you can use glm_binom_fit.predict(new_exog)  (plus if needed offset and exposure)

confidence interval for the prediction of the mean  (i.e. probabilities) are currently not available in a predict function.

I have a PR for this that will be merged for 0.7.

To work around for the confidence interval, you could use t_test to get the confidence interval for the linear prediction and transform them with the link.inverse   (that will be the default endpoint transformation confidence intervals, which are supposed to be better than delta method confidence intervals)

roughly, IIRC

tt = glm_binom_fit.t_test(U)      where U is your exog, or you use a new exog
ci_lin = tt.conf_int()              # possible to specify alpha
ci = glm_binom.link.inverse(ci_lin)      # link is attached to model


Josef
 

Thanks
Nitesh

josef...@gmail.com

unread,
Jan 20, 2015, 1:26:25 PM1/20/15
to pystatsmodels
a qualifier: calculating the confidence interval for the predicted mean this way cannot handle offset and exposure.

Josef
 


Josef
 

Thanks
Nitesh


nitesh ranjan

unread,
Jan 20, 2015, 2:06:40 PM1/20/15
to pystat...@googlegroups.com

thanks josef..i will try to implemt to it...hopefully it should work...

Thanks
Nitesh
Reply all
Reply to author
Forward
0 new messages