Skip to content

Instantly share code, notes, and snippets.

@tillahoffmann
Created October 25, 2017 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tillahoffmann/e48dbc3546c418561263d181b2bda694 to your computer and use it in GitHub Desktop.
Save tillahoffmann/e48dbc3546c418561263d181b2bda694 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"from matplotlib import pyplot as plt\n",
"import collections\n",
"import scipy.stats\n",
"import numpy as np\n",
"%matplotlib inline\n",
"\n",
"WEIGHTED = False"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BHPS wave: b\n",
"Contingency table\n",
"[[ 8753 2934]\n",
" [ 2279 11487]]\n",
"Odds ratio: 15.037, p-value: 0.000000\n",
"\n",
"BHPS wave: d\n",
"Contingency table\n",
"[[ 8474 2820]\n",
" [ 2080 11205]]\n",
"Odds ratio: 16.188, p-value: 0.000000\n",
"\n",
"BHPS wave: f\n",
"Contingency table\n",
"[[ 8496 3200]\n",
" [ 2260 11467]]\n",
"Odds ratio: 13.471, p-value: 0.000000\n",
"\n",
"BHPS wave: h\n",
"Contingency table\n",
"[[ 9573 3441]\n",
" [ 2415 13347]]\n",
"Odds ratio: 15.376, p-value: 0.000000\n",
"\n",
"BHPS wave: j\n",
"Contingency table\n",
"[[13731 4632]\n",
" [ 3309 19077]]\n",
"Odds ratio: 17.090, p-value: 0.000000\n",
"\n",
"BHPS wave: l\n",
"Contingency table\n",
"[[14268 4831]\n",
" [ 3502 19633]]\n",
"Odds ratio: 16.558, p-value: 0.000000\n",
"\n",
"BHPS wave: n\n",
"Contingency table\n",
"[[13325 4521]\n",
" [ 3316 18672]]\n",
"Odds ratio: 16.596, p-value: 0.000000\n",
"\n",
"BHPS wave: p\n",
"Contingency table\n",
"[[12731 4291]\n",
" [ 3122 17905]]\n",
"Odds ratio: 17.016, p-value: 0.000000\n",
"\n",
"BHPS wave: r\n",
"Contingency table\n",
"[[11626 4044]\n",
" [ 2948 16500]]\n",
"Odds ratio: 16.091, p-value: 0.000000\n",
"\n"
]
}
],
"source": [
"codes = 'bdfhjlnpr'\n",
"for code in codes:\n",
" nominations = collections.defaultdict(lambda: 0)\n",
" \n",
" bhps = pd.read_stata('../data/bhps/%sindresp.dta' % code, convert_categoricals=False)\n",
" for _, row in bhps.iterrows():\n",
" sex = row['%ssex' % code]\n",
" weight = row['%sxrwght' % code]\n",
" for i in range(1, 4):\n",
" nominations[(sex, row['%snetsx%d' % (code, i)])] += weight if WEIGHTED else 1\n",
" \n",
" # Construct the cross tabulation\n",
" table = [[nominations[(1, 1)], nominations[(1, 2)]],\n",
" [nominations[(2, 1)], nominations[(2, 2)]]]\n",
"\n",
" print(\"BHPS wave:\", code)\n",
" print(\"Contingency table\")\n",
" print(np.round(table).astype(int))\n",
" print(\"Odds ratio: %.3f, p-value: %f\" % scipy.stats.fisher_exact(table))\n",
" print()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Understanding Society wave: c\n",
"Contingency table\n",
"[[36226 9144]\n",
" [ 7773 51412]]\n",
"Odds ratio: 26.204, p-value: 0.000000\n",
"\n",
"Understanding Society wave: f\n",
"Contingency table\n",
"[[34125 7847]\n",
" [ 6211 47765]]\n",
"Odds ratio: 33.444, p-value: 0.000000\n",
"\n"
]
}
],
"source": [
"codes = 'cf'\n",
"for code in codes:\n",
" nominations = collections.defaultdict(lambda: 0)\n",
" \n",
" us = pd.read_stata('../data/understanding_society/%s_indresp.dta' % code, convert_categoricals=False)\n",
" for _, row in us.iterrows():\n",
" sex = row['%s_sex' % code]\n",
" weight = row['%s_indscub_xw' % code]\n",
" for i in range(1, 4):\n",
" nominations[(sex, row['%s_netsx_%d' % (code, i)])] += weight if WEIGHTED else 1\n",
" \n",
" # Construct the cross tabulation\n",
" table = [[nominations[(1, 1)], nominations[(1, 2)]],\n",
" [nominations[(2, 1)], nominations[(2, 2)]]]\n",
"\n",
" print(\"Understanding Society wave:\", code)\n",
" print(\"Contingency table\")\n",
" print(np.round(table).astype(int))\n",
" print(\"Odds ratio: %.3f, p-value: %f\" % scipy.stats.fisher_exact(table))\n",
" print()"
]
}
],
"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.6.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment