Thomas Haslwanter
unread,May 11, 2013, 9:08:52 AM5/11/13Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to pystat...@googlegroups.com
In trying to reproduce the code results from Dobson & Barnett "An
Introduction to GLMs" in Python, I have run into an inconsistency between
the results from R and from Python. While the results for logistic
regression with statsmodels match the R-results for the logit and probit
link functions, the results for the cloglog link are inconsistent.
I have looked at the Python code in statsmodels, and it seems correct to me,
so I am a bit dumbfounded.
Also, the statsmodels link only works for "cloglog", but crashes for
"CLogLog".
Python Code:
------------
'''Solution to Exercise 7.3.1 (Beetle Mortality) in Dobson & Bartlett:
"Introduction to GLMs"
author: thomas haslwanter
date:Â Â may 2013
ver:Â Â Â 1.0
'''
# Standard libraries
import numpy as np
import pandas as pd
import statsmodels.api as sm
  Â
if __name__ == '__main__':
   data = np.array([[1.6907, 59, 6],
       [1.7242, 60, 13],
       [1.7552, 62, 18],
       [1.7842, 56, 28],
       [1.8113, 63, 52],
       [1.8369, 59, 53],
       [1.861, 62, 61 ],
       [1.8839, 60, 60]])
  Â
   df = pd.DataFrame(data)
   df.columns = ['x', 'n', 'y']
  Â
   # fit the model
   df['tested'] = df['n']
   df['killed'] = df['y']
   df['survived'] = df['tested'] - df['killed']
  Â
   model_logit = sm.GLM.from_formula('survived + killed ~ x', data=df, family=sm.families.Binomial(sm.families.links.logit)).fit()
   print 'Logit:'
   print df['n']*(1-model_logit.fittedvalues)
  Â
   model_cll = sm.GLM.from_formula('survived + killed ~ x', data=df, family=sm.families.Binomial(sm.families.links.cloglog)).fit()
   print 'CLogLog:'
   print df['n']*(1-model_cll.fittedvalues)
Python Results:
---------------
Logit:
0Â Â Â Â 3.457461
1Â Â Â Â 9.841672
2Â Â Â 22.451378
3Â Â Â 33.897635
4Â Â Â 50.095822
5Â Â Â 53.290913
6Â Â Â 59.222159
7Â Â Â 58.742961
CLogLog:
0Â Â Â Â 2.365465
1Â Â Â 12.557569
2Â Â Â 27.787944
3Â Â Â 36.431779
4Â Â Â 49.561865
5Â Â Â 51.380562
6Â Â Â 57.100959
7Â Â Â 57.057706
R code:
-------
y=c(6,13,18,28,52,53,61,60)
n=c(59,60,62,56,63,59,62,60)
x=c(1.6907,1.7242,1.7552,1.7842,1.8113,1.8369,1.8610,1.8839)
n_y=n-y
beetle.mat=cbind(y,n_y)
res.glm1=glm(beetle.mat~x, family=binomial(link="logit"))
res.glm3=glm(beetle.mat~x, family=binomial(link="cloglog"))
fitted.values(res.glm1)*n
fitted.values(res.glm3)*n
R results:
----------
> fitted.values(res.glm1)*n
       1        2        3        4        5        6        7        8
 3.457461 9.841672 22.451378 33.897635 50.095822 53.290913 59.222159 58.742961
> fitted.values(res.glm3)*n
      1       2       3       4       5       6       7       8
 5.58945 11.28068 20.95422 30.36944 47.77642 54.14273 61.11331 59.94723