Created
April 3, 2016 15:45
-
-
Save sechilds/993f1658f52367a472c5a91fed78bc41 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", | |
"import statsmodels.api as sm\n", | |
"import patsy" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"_ = sm.datasets.get_rdataset(\"Arthritis\", \"vcd\", cache=\".cache\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"dta = sm.datasets.get_rdataset(\"Arthritis\", \"vcd\", cache=\".cache\")" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"None 42\n", | |
"Marked 28\n", | |
"Some 14\n", | |
"Name: Improved, dtype: int64" | |
] | |
}, | |
"execution_count": 10, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dta.data.Improved.value_counts()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"df = dta.data" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"df['B_Improved'] = df['Improved'].map({'Some': True,\n", | |
" 'Marked': True,\n", | |
" 'None': False\n", | |
" })" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>ID</th>\n", | |
" <th>Treatment</th>\n", | |
" <th>Sex</th>\n", | |
" <th>Age</th>\n", | |
" <th>Improved</th>\n", | |
" <th>B_Improved</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>0</th>\n", | |
" <td>57</td>\n", | |
" <td>Treated</td>\n", | |
" <td>Male</td>\n", | |
" <td>27</td>\n", | |
" <td>Some</td>\n", | |
" <td>True</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1</th>\n", | |
" <td>46</td>\n", | |
" <td>Treated</td>\n", | |
" <td>Male</td>\n", | |
" <td>29</td>\n", | |
" <td>None</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>2</th>\n", | |
" <td>77</td>\n", | |
" <td>Treated</td>\n", | |
" <td>Male</td>\n", | |
" <td>30</td>\n", | |
" <td>None</td>\n", | |
" <td>False</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>3</th>\n", | |
" <td>17</td>\n", | |
" <td>Treated</td>\n", | |
" <td>Male</td>\n", | |
" <td>32</td>\n", | |
" <td>Marked</td>\n", | |
" <td>True</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>4</th>\n", | |
" <td>36</td>\n", | |
" <td>Treated</td>\n", | |
" <td>Male</td>\n", | |
" <td>46</td>\n", | |
" <td>Marked</td>\n", | |
" <td>True</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" ID Treatment Sex Age Improved B_Improved\n", | |
"0 57 Treated Male 27 Some True\n", | |
"1 46 Treated Male 29 None False\n", | |
"2 77 Treated Male 30 None False\n", | |
"3 17 Treated Male 32 Marked True\n", | |
"4 36 Treated Male 46 Marked True" | |
] | |
}, | |
"execution_count": 16, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df.head()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"X = patsy.dmatrix(\"Treatment + Sex + Age\", data=df, return_type='dataframe')\n", | |
"Y = df.B_Improved" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 37, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"logit_mod = sm.Logit(Y, X)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Optimization terminated successfully.\n", | |
" Current function value: 0.547993\n", | |
" Iterations 6\n" | |
] | |
} | |
], | |
"source": [ | |
"results = logit_mod.fit()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table class=\"simpletable\">\n", | |
"<caption>Logit Regression Results</caption>\n", | |
"<tr>\n", | |
" <th>Dep. Variable:</th> <td>B_Improved</td> <th> No. Observations: </th> <td> 84</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Model:</th> <td>Logit</td> <th> Df Residuals: </th> <td> 80</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Method:</th> <td>MLE</td> <th> Df Model: </th> <td> 3</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Date:</th> <td>Sun, 03 Apr 2016</td> <th> Pseudo R-squ.: </th> <td>0.2094</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Time:</th> <td>09:24:52</td> <th> Log-Likelihood: </th> <td> -46.031</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>converged:</th> <td>True</td> <th> LL-Null: </th> <td> -58.224</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th> </th> <td> </td> <th> LLR p-value: </th> <td>2.075e-05</td>\n", | |
"</tr>\n", | |
"</table>\n", | |
"<table class=\"simpletable\">\n", | |
"<tr>\n", | |
" <td></td> <th>coef</th> <th>std err</th> <th>z</th> <th>P>|z|</th> <th>[95.0% Conf. Int.]</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Intercept</th> <td> -3.0155</td> <td> 1.168</td> <td> -2.582</td> <td> 0.010</td> <td> -5.304 -0.727</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Treatment[T.Treated]</th> <td> 1.7598</td> <td> 0.536</td> <td> 3.280</td> <td> 0.001</td> <td> 0.708 2.811</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Sex[T.Male]</th> <td> -1.4878</td> <td> 0.595</td> <td> -2.502</td> <td> 0.012</td> <td> -2.654 -0.322</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Age</th> <td> 0.0487</td> <td> 0.021</td> <td> 2.359</td> <td> 0.018</td> <td> 0.008 0.089</td>\n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"<class 'statsmodels.iolib.summary.Summary'>\n", | |
"\"\"\"\n", | |
" Logit Regression Results \n", | |
"==============================================================================\n", | |
"Dep. Variable: B_Improved No. Observations: 84\n", | |
"Model: Logit Df Residuals: 80\n", | |
"Method: MLE Df Model: 3\n", | |
"Date: Sun, 03 Apr 2016 Pseudo R-squ.: 0.2094\n", | |
"Time: 09:24:52 Log-Likelihood: -46.031\n", | |
"converged: True LL-Null: -58.224\n", | |
" LLR p-value: 2.075e-05\n", | |
"========================================================================================\n", | |
" coef std err z P>|z| [95.0% Conf. Int.]\n", | |
"----------------------------------------------------------------------------------------\n", | |
"Intercept -3.0155 1.168 -2.582 0.010 -5.304 -0.727\n", | |
"Treatment[T.Treated] 1.7598 0.536 3.280 0.001 0.708 2.811\n", | |
"Sex[T.Male] -1.4878 0.595 -2.502 0.012 -2.654 -0.322\n", | |
"Age 0.0487 0.021 2.359 0.018 0.008 0.089\n", | |
"========================================================================================\n", | |
"\"\"\"" | |
] | |
}, | |
"execution_count": 39, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"results.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 40, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table class=\"simpletable\">\n", | |
"<caption>Logit Marginal Effects</caption>\n", | |
"<tr>\n", | |
" <th>Dep. Variable:</th> <td>B_Improved</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Method:</th> <td>dydx</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>At:</th> <td>overall</td> \n", | |
"</tr>\n", | |
"</table>\n", | |
"<table class=\"simpletable\">\n", | |
"<tr>\n", | |
" <th></th> <th>dy/dx</th> <th>std err</th> <th>z</th> <th>P>|z|</th> <th>[95.0% Conf. Int.]</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Treatment[T.Treated]</th> <td> 0.3238</td> <td> 0.074</td> <td> 4.403</td> <td> 0.000</td> <td> 0.180 0.468</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Sex[T.Male]</th> <td> -0.2738</td> <td> 0.095</td> <td> -2.878</td> <td> 0.004</td> <td> -0.460 -0.087</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Age</th> <td> 0.0090</td> <td> 0.003</td> <td> 2.689</td> <td> 0.007</td> <td> 0.002 0.016</td>\n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"<class 'statsmodels.iolib.summary.Summary'>\n", | |
"\"\"\"\n", | |
" Logit Marginal Effects \n", | |
"=====================================\n", | |
"Dep. Variable: B_Improved\n", | |
"Method: dydx\n", | |
"At: overall\n", | |
"========================================================================================\n", | |
" dy/dx std err z P>|z| [95.0% Conf. Int.]\n", | |
"----------------------------------------------------------------------------------------\n", | |
"Treatment[T.Treated] 0.3238 0.074 4.403 0.000 0.180 0.468\n", | |
"Sex[T.Male] -0.2738 0.095 -2.878 0.004 -0.460 -0.087\n", | |
"Age 0.0090 0.003 2.689 0.007 0.002 0.016\n", | |
"========================================================================================\n", | |
"\"\"\"" | |
] | |
}, | |
"execution_count": 40, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"results.get_margeff().summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 41, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"Y = df.Improved" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 42, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"mlogit_mod = sm.MNLogit(Y, X)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Optimization terminated successfully.\n", | |
" Current function value: 0.850209\n", | |
" Iterations 6\n" | |
] | |
} | |
], | |
"source": [ | |
"results = mlogit_mod.fit()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table class=\"simpletable\">\n", | |
"<caption>MNLogit Regression Results</caption>\n", | |
"<tr>\n", | |
" <th>Dep. Variable:</th> <td>Improved</td> <th> No. Observations: </th> <td> 84</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Model:</th> <td>MNLogit</td> <th> Df Residuals: </th> <td> 76</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Method:</th> <td>MLE</td> <th> Df Model: </th> <td> 6</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Date:</th> <td>Sun, 03 Apr 2016</td> <th> Pseudo R-squ.: </th> <td>0.1594</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Time:</th> <td>09:26:50</td> <th> Log-Likelihood: </th> <td> -71.418</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>converged:</th> <td>True</td> <th> LL-Null: </th> <td> -84.958</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th> </th> <td> </td> <th> LLR p-value: </th> <td>0.0001398</td>\n", | |
"</tr>\n", | |
"</table>\n", | |
"<table class=\"simpletable\">\n", | |
"<tr>\n", | |
" <th>Improved=None</th> <th>coef</th> <th>std err</th> <th>z</th> <th>P>|z|</th> <th>[95.0% Conf. Int.]</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Intercept</th> <td> 3.6465</td> <td> 1.334</td> <td> 2.733</td> <td> 0.006</td> <td> 1.031 6.262</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Treatment[T.Treated]</th> <td> -2.1447</td> <td> 0.609</td> <td> -3.524</td> <td> 0.000</td> <td> -3.338 -0.952</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Sex[T.Male]</th> <td> 1.3900</td> <td> 0.656</td> <td> 2.118</td> <td> 0.034</td> <td> 0.104 2.677</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Age</th> <td> -0.0475</td> <td> 0.023</td> <td> -2.054</td> <td> 0.040</td> <td> -0.093 -0.002</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Improved=Some</th> <th>coef</th> <th>std err</th> <th>z</th> <th>P>|z|</th> <th>[95.0% Conf. Int.]</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Intercept</th> <td> -0.1695</td> <td> 1.800</td> <td> -0.094</td> <td> 0.925</td> <td> -3.697 3.358</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Treatment[T.Treated]</th> <td> -1.0687</td> <td> 0.698</td> <td> -1.530</td> <td> 0.126</td> <td> -2.438 0.300</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Sex[T.Male]</th> <td> -0.3026</td> <td> 0.908</td> <td> -0.333</td> <td> 0.739</td> <td> -2.082 1.477</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Age</th> <td> 0.0036</td> <td> 0.031</td> <td> 0.117</td> <td> 0.907</td> <td> -0.056 0.063</td>\n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"<class 'statsmodels.iolib.summary.Summary'>\n", | |
"\"\"\"\n", | |
" MNLogit Regression Results \n", | |
"==============================================================================\n", | |
"Dep. Variable: Improved No. Observations: 84\n", | |
"Model: MNLogit Df Residuals: 76\n", | |
"Method: MLE Df Model: 6\n", | |
"Date: Sun, 03 Apr 2016 Pseudo R-squ.: 0.1594\n", | |
"Time: 09:26:50 Log-Likelihood: -71.418\n", | |
"converged: True LL-Null: -84.958\n", | |
" LLR p-value: 0.0001398\n", | |
"========================================================================================\n", | |
" Improved=None coef std err z P>|z| [95.0% Conf. Int.]\n", | |
"----------------------------------------------------------------------------------------\n", | |
"Intercept 3.6465 1.334 2.733 0.006 1.031 6.262\n", | |
"Treatment[T.Treated] -2.1447 0.609 -3.524 0.000 -3.338 -0.952\n", | |
"Sex[T.Male] 1.3900 0.656 2.118 0.034 0.104 2.677\n", | |
"Age -0.0475 0.023 -2.054 0.040 -0.093 -0.002\n", | |
"----------------------------------------------------------------------------------------\n", | |
" Improved=Some coef std err z P>|z| [95.0% Conf. Int.]\n", | |
"----------------------------------------------------------------------------------------\n", | |
"Intercept -0.1695 1.800 -0.094 0.925 -3.697 3.358\n", | |
"Treatment[T.Treated] -1.0687 0.698 -1.530 0.126 -2.438 0.300\n", | |
"Sex[T.Male] -0.3026 0.908 -0.333 0.739 -2.082 1.477\n", | |
"Age 0.0036 0.031 0.117 0.907 -0.056 0.063\n", | |
"========================================================================================\n", | |
"\"\"\"" | |
] | |
}, | |
"execution_count": 44, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"results.summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 45, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<table class=\"simpletable\">\n", | |
"<caption>MNLogit Marginal Effects</caption>\n", | |
"<tr>\n", | |
" <th>Dep. Variable:</th> <td>Improved</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Method:</th> <td>dydx</td> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>At:</th> <td>overall</td>\n", | |
"</tr>\n", | |
"</table>\n", | |
"<table class=\"simpletable\">\n", | |
"<tr>\n", | |
" <th>Improved=Marked</th> <th>dy/dx</th> <th>std err</th> <th>z</th> <th>P>|z|</th> <th>[95.0% Conf. Int.]</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Treatment[T.Treated]</th> <td> 0.3151</td> <td> 0.073</td> <td> 4.306</td> <td> 0.000</td> <td> 0.172 0.459</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Sex[T.Male]</th> <td> -0.1406</td> <td> 0.106</td> <td> -1.329</td> <td> 0.184</td> <td> -0.348 0.067</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Age</th> <td> 0.0052</td> <td> 0.004</td> <td> 1.414</td> <td> 0.157</td> <td> -0.002 0.012</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Improved=None</th> <th>dy/dx</th> <th>std err</th> <th>z</th> <th>P>|z|</th> <th>[95.0% Conf. Int.]</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Treatment[T.Treated]</th> <td> -0.3210</td> <td> 0.074</td> <td> -4.351</td> <td> 0.000</td> <td> -0.466 -0.176</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Sex[T.Male]</th> <td> 0.2767</td> <td> 0.096</td> <td> 2.876</td> <td> 0.004</td> <td> 0.088 0.465</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Age</th> <td> -0.0090</td> <td> 0.003</td> <td> -2.696</td> <td> 0.007</td> <td> -0.016 -0.002</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Improved=Some</th> <th>dy/dx</th> <th>std err</th> <th>z</th> <th>P>|z|</th> <th>[95.0% Conf. Int.]</th> \n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Treatment[T.Treated]</th> <td> 0.0059</td> <td> 0.070</td> <td> 0.084</td> <td> 0.933</td> <td> -0.131 0.142</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Sex[T.Male]</th> <td> -0.1360</td> <td> 0.106</td> <td> -1.288</td> <td> 0.198</td> <td> -0.343 0.071</td>\n", | |
"</tr>\n", | |
"<tr>\n", | |
" <th>Age</th> <td> 0.0038</td> <td> 0.003</td> <td> 1.081</td> <td> 0.280</td> <td> -0.003 0.011</td>\n", | |
"</tr>\n", | |
"</table>" | |
], | |
"text/plain": [ | |
"<class 'statsmodels.iolib.summary.Summary'>\n", | |
"\"\"\"\n", | |
" MNLogit Marginal Effects \n", | |
"=====================================\n", | |
"Dep. Variable: Improved\n", | |
"Method: dydx\n", | |
"At: overall\n", | |
"========================================================================================\n", | |
" Improved=Marked dy/dx std err z P>|z| [95.0% Conf. Int.]\n", | |
"----------------------------------------------------------------------------------------\n", | |
"Treatment[T.Treated] 0.3151 0.073 4.306 0.000 0.172 0.459\n", | |
"Sex[T.Male] -0.1406 0.106 -1.329 0.184 -0.348 0.067\n", | |
"Age 0.0052 0.004 1.414 0.157 -0.002 0.012\n", | |
"----------------------------------------------------------------------------------------\n", | |
" Improved=None dy/dx std err z P>|z| [95.0% Conf. Int.]\n", | |
"----------------------------------------------------------------------------------------\n", | |
"Treatment[T.Treated] -0.3210 0.074 -4.351 0.000 -0.466 -0.176\n", | |
"Sex[T.Male] 0.2767 0.096 2.876 0.004 0.088 0.465\n", | |
"Age -0.0090 0.003 -2.696 0.007 -0.016 -0.002\n", | |
"----------------------------------------------------------------------------------------\n", | |
" Improved=Some dy/dx std err z P>|z| [95.0% Conf. Int.]\n", | |
"----------------------------------------------------------------------------------------\n", | |
"Treatment[T.Treated] 0.0059 0.070 0.084 0.933 -0.131 0.142\n", | |
"Sex[T.Male] -0.1360 0.106 -1.288 0.198 -0.343 0.071\n", | |
"Age 0.0038 0.003 1.081 0.280 -0.003 0.011\n", | |
"========================================================================================\n", | |
"\"\"\"" | |
] | |
}, | |
"execution_count": 45, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"results.get_margeff().summary()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 47, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"df.to_stata('arthritus.dta')" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" . logit B_Improved treat sex Age\n", | |
" \n", | |
" Iteration 0: log likelihood = -58.224363 \n", | |
" Iteration 1: log likelihood = -46.120282 \n", | |
" Iteration 2: log likelihood = -46.031647 \n", | |
" Iteration 3: log likelihood = -46.031402 \n", | |
" Iteration 4: log likelihood = -46.031402 \n", | |
" \n", | |
" Logistic regression Number of obs = 84\n", | |
" LR chi2(3) = 24.39\n", | |
" Prob > chi2 = 0.0000\n", | |
" Log likelihood = -46.031402 Pseudo R2 = 0.2094\n", | |
" \n", | |
" ------------------------------------------------------------------------------\n", | |
" B_Improved | Coef. Std. Err. z P>|z| [95% Conf. Interval]\n", | |
" -------------+----------------------------------------------------------------\n", | |
" treat | 1.759804 .5364955 3.28 0.001 .7082924 2.811316\n", | |
" sex | -1.487831 .5947694 -2.50 0.012 -2.653558 -.3221046\n", | |
" Age | .0487465 .0206629 2.36 0.018 .0082479 .0892452\n", | |
" _cons | -3.287434 1.449412 -2.27 0.023 -6.12823 -.446638\n", | |
" ------------------------------------------------------------------------------\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" . margins, dydx(*)\n", | |
" \n", | |
" Average marginal effects Number of obs = 84\n", | |
" Model VCE : OIM\n", | |
" \n", | |
" Expression : Pr(B_Improved), predict()\n", | |
" dy/dx w.r.t. : treat sex Age\n", | |
" \n", | |
" ------------------------------------------------------------------------------\n", | |
" | Delta-method\n", | |
" | dy/dx Std. Err. z P>|z| [95% Conf. Interval]\n", | |
" -------------+----------------------------------------------------------------\n", | |
" treat | .3238183 .0735499 4.40 0.000 .1796631 .4679735\n", | |
" sex | -.273773 .0951228 -2.88 0.004 -.4602103 -.0873358\n", | |
" Age | .0089698 .003336 2.69 0.007 .0024312 .0155083\n", | |
" ------------------------------------------------------------------------------\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" . mlogit improve treat sex Age\n", | |
"\n", | |
" Iteration 0: log likelihood = -84.957958 \n", | |
" Iteration 1: log likelihood = -71.577377 \n", | |
" Iteration 2: log likelihood = -71.418045 \n", | |
" Iteration 3: log likelihood = -71.417555 \n", | |
" Iteration 4: log likelihood = -71.417555 \n", | |
"\n", | |
" Multinomial logistic regression Number of obs = 84\n", | |
" LR chi2(6) = 27.08\n", | |
" Prob > chi2 = 0.0001\n", | |
" Log likelihood = -71.417555 Pseudo R2 = 0.1594\n", | |
"\n", | |
" ------------------------------------------------------------------------------\n", | |
" improve | Coef. Std. Err. z P>|z| [95% Conf. Interval]\n", | |
" -------------+----------------------------------------------------------------\n", | |
" Marked |\n", | |
" treat | 2.144733 .6086598 3.52 0.000 .9517818 3.337684\n", | |
" sex | -1.390033 .6563779 -2.12 0.034 -2.67651 -.1035561\n", | |
" Age | .047497 .0231275 2.05 0.040 .0021678 .0928261\n", | |
" _cons | -4.401247 1.678671 -2.62 0.009 -7.691383 -1.111111\n", | |
" -------------+----------------------------------------------------------------\n", | |
" None | (base outcome)\n", | |
" -------------+----------------------------------------------------------------\n", | |
" Some |\n", | |
" treat | 1.07607 .6902589 1.56 0.119 -.2768126 2.428953\n", | |
" sex | -1.692597 .8710234 -1.94 0.052 -3.399771 .0145777\n", | |
" Age | .0510761 .0288193 1.77 0.076 -.0054088 .107561\n", | |
" _cons | -3.199487 2.012402 -1.59 0.112 -7.143722 .7447486\n", | |
" ------------------------------------------------------------------------------\n" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
" . margins, dydx(*)\n", | |
"\n", | |
" Average marginal effects Number of obs = 84\n", | |
" Model VCE : OIM\n", | |
"\n", | |
" Expression : Pr(improve==Marked), predict()\n", | |
" dy/dx w.r.t. : treat sex Age\n", | |
"\n", | |
" ------------------------------------------------------------------------------\n", | |
" | Delta-method\n", | |
" | dy/dx Std. Err. z P>|z| [95% Conf. Interval]\n", | |
" -------------+----------------------------------------------------------------\n", | |
" treat | .3151456 .0731874 4.31 0.000 .1717009 .4585903\n", | |
" sex | -.1406108 .1058278 -1.33 0.184 -.3480295 .0668079\n", | |
" Age | .0052369 .0037041 1.41 0.157 -.0020229 .0124967\n", | |
" ------------------------------------------------------------------------------\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.1" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment