In [1]: import numpy as np
In [2]: import statsmodels.api as sm
Load the data from Spector and Mazzeo (1980). Examples follow Greene’s Econometric Analysis Ch. 21 (5th Edition).
In [3]: spector_data = sm.datasets.spector.load()
In [4]: spector_data.exog = sm.add_constant(spector_data.exog, prepend=False)
Linear Probability Model using OLS
In [5]: lpm_mod = sm.OLS(spector_data.endog, spector_data.exog)
In [6]: lpm_res = lpm_mod.fit()
Logit Model
In [7]: logit_mod = sm.Logit(spector_data.endog, spector_data.exog)
In [8]: logit_res = logit_mod.fit()
Optimization terminated successfully.
Current function value: 12.889634
Iterations 7
Probit Model
In [9]: probit_mod = sm.Probit(spector_data.endog, spector_data.exog)
In [10]: probit_res = probit_mod.fit()
Optimization terminated successfully.
Current function value: 12.818804
Iterations 6
This example is based on Greene Table 21.1 5th Edition Linear Model Parameters
In [11]: print lpm_res.params
[ 0.46385168 0.01049512 0.37855479 -1.49801712]
Logit Model Parameters
In [12]: print logit_res.params
[ 2.82611259 0.09515766 2.37868766 -13.02134686]
Probit Model Parameters
In [13]: print probit_res.params
[ 1.62581004 0.05172895 1.42633234 -7.45231965]
Linear Model Parameters
In [14]: print lpm_res.params[:-1]
[ 0.46385168 0.01049512 0.37855479]
Logit Model Marginal Effects
In [15]: print logit_res.margeff()
[ 0.36258083 0.01220841 0.3051777 ]
Probit Model Marginal Effects
In [16]: print probit_res.margeff()
[ 0.36078629 0.01147926 0.31651986]
Multinomial Logit Example using American National Election Studies Data
In [17]: anes_data = sm.datasets.anes96.load()
In [18]: anes_exog = anes_data.exog
In [19]: anes_exog[:,0] = np.log(anes_exog[:,0] + .1)
In [20]: anes_exog = np.column_stack((anes_exog[:,0],anes_exog[:,2],anes_exog[:,5:8]))
In [21]: anes_exog = sm.add_constant(anes_exog, prepend=False)
In [22]: mlogit_mod = sm.MNLogit(anes_data.endog, anes_exog)
In [23]: mlogit_res = mlogit_mod.fit()
Optimization terminated successfully.
Current function value: 1461.922747
Iterations 7
The default method for the fit is Newton-Raphson However, you can use other solvers
In [24]: mlogit_res = mlogit_mod.fit(method='bfgs', maxiter=100)
Warning: Desired error not necessarily achieved due to precision loss.
Current function value: nan
Iterations: 1
Function evaluations: 42
Gradient evaluations: 42
Poisson model This is similar to Cameron and Trivedi’s Microeconometrics Table 20.5; however, the data differs slightly from theirs
In [25]: rand_data = sm.datasets.randhie.load()
In [26]: rand_exog = rand_data.exog.view(float).reshape(len(rand_data.exog), -1)
In [27]: rand_exog = sm.add_constant(rand_exog, prepend=False)
In [28]: poisson_mod = sm.Poisson(rand_data.endog, rand_exog)
In [29]: poisson_res = poisson_mod.fit(method="newton")
Optimization terminated successfully.
Current function value: 62419.588564
Iterations 12
In [30]: print poisson_res.summary()
Poisson Regression Results
==============================================================================
Dep. Variable: y No. Observations: 20190
Model: Poisson Df Residuals: 20180
Method: MLE Df Model: 9
Date: Sat, 27 Aug 2016 Pseudo R-squ.: 0.06343
Time: 10:00:49 Log-Likelihood: -62420.
converged: True LL-Null: -66647.
LLR p-value: 0.000
==============================================================================
coef std err z P>|z| [95.0% Conf. Int.]
------------------------------------------------------------------------------
x1 -0.0525 0.003 -18.216 0.000 -0.058 -0.047
x2 -0.2471 0.011 -23.272 0.000 -0.268 -0.226
x3 0.0353 0.002 19.302 0.000 0.032 0.039
x4 -0.0346 0.002 -21.439 0.000 -0.038 -0.031
x5 0.2717 0.012 22.200 0.000 0.248 0.296
x6 0.0339 0.001 60.098 0.000 0.033 0.035
x7 -0.0126 0.009 -1.366 0.172 -0.031 0.005
x8 0.0541 0.015 3.531 0.000 0.024 0.084
x9 0.2061 0.026 7.843 0.000 0.155 0.258
const 0.7004 0.011 62.741 0.000 0.678 0.722
==============================================================================