<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Discrete Choice Models Overview
Step1:  Data
Load data from Spector and Mazzeo (1980). Examples follow Greene's Econometric Analysis Ch. 21 (5th Edition).
Step2:  Inspect the data
Step3:  Linear Probability Model (OLS)
Step4:  Logit Model
Step5:  Marginal Effects
Step6:  As in all the discrete data models presented below, we can print a nice summary of results
Step7:  Probit Model
Step8:  Multinomial Logit
Load data from the American National Election Studies
Step9:  Inspect the data
Step10:  Fit MNL model
Step11:  Poisson
Load the Rand data. Note that this example is similar to Cameron and Trivedi's Microeconometrics Table 20.5, but it is slightly different because of minor changes in the data.
Step12:  Fit Poisson model
Step13:  Negative Binomial
The negative binomial model gives slightly different results.
Step14:  Alternative solvers
The default method for fitting discrete data MLE models is Newton-Raphson. You can use other solvers by using the method argument
<ASSISTANT_TASK:>
Python Code:
import numpy as np
import statsmodels.api as sm
Explanation: Discrete Choice Models Overview
End of explanation
spector_data = sm.datasets.spector.load()
spector_data.exog = sm.add_constant(spector_data.exog, prepend=False)
Explanation: Data
Load data from Spector and Mazzeo (1980). Examples follow Greene's Econometric Analysis Ch. 21 (5th Edition).
End of explanation
print(spector_data.exog.head())
print(spector_data.endog.head())
Explanation: Inspect the data:
End of explanation
lpm_mod = sm.OLS(spector_data.endog, spector_data.exog)
lpm_res = lpm_mod.fit()
print("Parameters: ", lpm_res.params[:-1])
Explanation: Linear Probability Model (OLS)
End of explanation
logit_mod = sm.Logit(spector_data.endog, spector_data.exog)
logit_res = logit_mod.fit(disp=0)
print("Parameters: ", logit_res.params)
Explanation: Logit Model
End of explanation
margeff = logit_res.get_margeff()
print(margeff.summary())
Explanation: Marginal Effects
End of explanation
print(logit_res.summary())
Explanation: As in all the discrete data models presented below, we can print a nice summary of results:
End of explanation
probit_mod = sm.Probit(spector_data.endog, spector_data.exog)
probit_res = probit_mod.fit()
probit_margeff = probit_res.get_margeff()
print("Parameters: ", probit_res.params)
print("Marginal effects: ")
print(probit_margeff.summary())
Explanation: Probit Model
End of explanation
anes_data = sm.datasets.anes96.load()
anes_exog = anes_data.exog
anes_exog = sm.add_constant(anes_exog)
Explanation: Multinomial Logit
Load data from the American National Election Studies:
End of explanation
print(anes_data.exog.head())
print(anes_data.endog.head())
Explanation: Inspect the data:
End of explanation
mlogit_mod = sm.MNLogit(anes_data.endog, anes_exog)
mlogit_res = mlogit_mod.fit()
print(mlogit_res.params)
Explanation: Fit MNL model:
End of explanation
rand_data = sm.datasets.randhie.load()
rand_exog = rand_data.exog
rand_exog = sm.add_constant(rand_exog, prepend=False)
Explanation: Poisson
Load the Rand data. Note that this example is similar to Cameron and Trivedi's Microeconometrics Table 20.5, but it is slightly different because of minor changes in the data.
End of explanation
poisson_mod = sm.Poisson(rand_data.endog, rand_exog)
poisson_res = poisson_mod.fit(method="newton")
print(poisson_res.summary())
Explanation: Fit Poisson model:
End of explanation
mod_nbin = sm.NegativeBinomial(rand_data.endog, rand_exog)
res_nbin = mod_nbin.fit(disp=False)
print(res_nbin.summary())
Explanation: Negative Binomial
The negative binomial model gives slightly different results.
End of explanation
mlogit_res = mlogit_mod.fit(method="bfgs", maxiter=250)
print(mlogit_res.summary())
Explanation: Alternative solvers
The default method for fitting discrete data MLE models is Newton-Raphson. You can use other solvers by using the method argument:
End of explanation
<END_TASK>