Skip to content

Instantly share code, notes, and snippets.

@jburroni
Created February 21, 2015 19:00
Show Gist options
  • Save jburroni/ac52b4f92ec2266165fc to your computer and use it in GitHub Desktop.
Save jburroni/ac52b4f92ec2266165fc to your computer and use it in GitHub Desktop.
test with strata 2
{
"metadata": {
"name": "pruebas con strata-Copy0"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from statsmodels import iolib\n",
"import pandas\n",
"import statsmodels.api as sm\n",
"from stats import pscore_match as ps\n",
"from stats.weightstats import CompareMeans, DescrStatsW"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 15
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nsw = pandas.DataFrame(iolib.genfromdta('datasets/dehejiawahha/DW2002.dta'))"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 16
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"nsw['re742'] = nsw.re74*nsw.re74\n",
"nsw['re752'] = nsw.re75*nsw.re75\n",
"nsw['u74'] = nsw.re74==0\n",
"nsw['blacku74'] = nsw.black*nsw.u74\n",
"nsw['age2'] = nsw.age*nsw.age\n",
"nsw['educ2'] = nsw.educ*nsw.educ"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 17
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"reload(ps)\n",
"psm = ps.PropensityScoreMatch(nsw.t, nsw.ix[:, ['age', 'black', 'blacku74', 'educ', 'hispanic', 'married', 'nodegree', 're74', 're742', 're75', 're752', 'age2', 'educ2']] , nsw.re78)\n",
"psm.result = ps.PScoreMatchResult()\n",
"psm.fit()\n",
"psm.matching_algo.treatment_effect()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Optimization terminated successfully.\n",
" Current function value: 204.929500\n",
" Iterations 12\n",
" Logit Regression Results \n",
"==============================================================================\n",
"Dep. Variable: t No. Observations: 2675\n",
"Model: Logit Df Residuals: 2661\n",
"Method: MLE Df Model: 13\n",
"Date: Wed, 05 Jun 2013 Pseudo R-squ.: 0.6953\n",
"Time: 21:34:53 Log-Likelihood: -204.93\n",
"converged: True LL-Null: -672.65\n",
" LLR p-value: 1.266e-191\n",
"==============================================================================\n",
" coef std err z P>|z| [95.0% Conf. Int.]\n",
"------------------------------------------------------------------------------\n",
"const -7.5525 2.452 -3.080 0.002 -12.358 -2.747\n",
"age 0.3306 0.120 2.747 0.006 0.095 0.566\n",
"black 1.1330 0.352 3.218 0.001 0.443 1.823\n",
"blacku74 2.1370 0.427 5.000 0.000 1.299 2.975\n",
"educ 0.8248 0.353 2.334 0.020 0.132 1.517\n",
"hispanic 1.9628 0.567 3.459 0.001 0.851 3.075\n",
"married -1.8841 0.299 -6.292 0.000 -2.471 -1.297\n",
"nodegree 0.1300 0.428 0.303 0.762 -0.710 0.970\n",
"re74 -0.0001 3.55e-05 -2.948 0.003 -0.000 -3.51e-05\n",
"re742 2.358e-09 6.57e-10 3.587 0.000 1.07e-09 3.65e-09\n",
"re75 -0.0002 4.15e-05 -5.228 0.000 -0.000 -0.000\n",
"re752 1.581e-10 6.68e-10 0.237 0.813 -1.15e-09 1.47e-09\n",
"age2 -0.0063 0.002 -3.417 0.001 -0.010 -0.003\n",
"educ2 -0.0483 0.019 -2.597 0.009 -0.085 -0.012\n",
"=============================================================================="
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"Using common support: [0.00065257083663255262, 0.97352552084949706]\n",
"propensity scores description\n",
"count 1325.000000\n",
"mean 0.135093\n",
"std 0.270380\n",
"min 0.000653\n",
"25% 0.002434\n",
"50% 0.010941\n",
"75% 0.075511\n",
"max 0.973526\n",
"dtype: float64\n",
"found: 7 strata\n",
"stratum min, half, max: [0.00065257083663255262, 0.025230358137203934, 0.049808145437775314]\n",
"Treated, Control:[7, 915]\n",
"stratum min, half, max: [0.050088462245043805, 0.074691212740685187, 0.099293963236326568]\n",
"Treated, Control:[4, 103]\n",
"stratum min, half, max: [0.10033940036371894, 0.14953426501854977, 0.19872912967338061]\n",
"Treated, Control:[7, 53]\n",
"stratum min, half, max: [0.20329229347261113, 0.2959311921449404, 0.38857009081726968]\n",
"Treated, Control:[27, 41]\n",
"stratum min, half, max: [0.4032958069875725, 0.50058277028928755, 0.59786973359100271]\n",
"Treated, Control:[20, 14]"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"stratum min, half, max: [0.60380421246483551, 0.70112359871626562, 0.79844298496769572]\n",
"Treated, Control:[22, 13]\n",
"stratum min, half, max: [0.80193318262128876, 0.88772935173539291, 0.97352552084949706]\n",
"Treated, Control:[92, 7]\n"
]
},
{
"output_type": "pyout",
"prompt_number": 41,
"text": [
"2210.2181771560754"
]
}
],
"prompt_number": 41
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"psm.treatment_effect()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 34,
"text": [
"2210.2181771560754"
]
}
],
"prompt_number": 34
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"[strat.describe() for strat in psm.matching_algo.strata]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"stratum min, half, max: [0.00065257083663255262, 0.025230358137203934, 0.049808145437775314]\n",
"Treated, Control:[7, 915]\n",
"stratum min, half, max: [0.050088462245043805, 0.074691212740685187, 0.099293963236326568]\n",
"Treated, Control:[4, 103]\n",
"stratum min, half, max: [0.10033940036371894, 0.14953426501854977, 0.19872912967338061]\n",
"Treated, Control:[7, 53]\n",
"stratum min, half, max: [0.20329229347261113, 0.2959311921449404, 0.38857009081726968]\n",
"Treated, Control:[27, 41]\n",
"stratum min, half, max: [0.4032958069875725, 0.50058277028928755, 0.59786973359100271]\n",
"Treated, Control:[20, 14]\n",
"stratum min, half, max: [0.60380421246483551, 0.70112359871626562, 0.79844298496769572]\n",
"Treated, Control:[22, 13]\n",
"stratum min, half, max: [0.80193318262128876, 0.88772935173539291, 0.97352552084949706]\n",
"Treated, Control:[92, 7]\n"
]
},
{
"output_type": "pyout",
"prompt_number": 38,
"text": [
"[None, None, None, None, None, None, None]"
]
}
],
"prompt_number": 38
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"psm.matching_algo.strata[0]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "pyout",
"prompt_number": 32,
"text": [
"<stats.pscore_match.Stratum at 0x1090329d0>"
]
}
],
"prompt_number": 32
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment