{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "eb1c8b12",
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-21T02:24:44.231568Z",
"start_time": "2023-03-21T02:23:26.764221Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" age | \n",
" black | \n",
" clerical | \n",
" construc | \n",
" educ | \n",
" earns74 | \n",
" gdhlth | \n",
" inlf | \n",
" smsa | \n",
" lhrwage | \n",
" ... | \n",
" spwrk75 | \n",
" totwrk | \n",
" union | \n",
" worknrm | \n",
" workscnd | \n",
" exper | \n",
" yngkid | \n",
" yrsmarr | \n",
" hrwage | \n",
" agesq | \n",
"
\n",
" \n",
" \n",
" \n",
" 1 | \n",
" 32 | \n",
" 0 | \n",
" 0.00000 | \n",
" 0.00000 | \n",
" 12 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 1.95586 | \n",
" ... | \n",
" 0 | \n",
" 3438 | \n",
" 0 | \n",
" 3438 | \n",
" 0 | \n",
" 14 | \n",
" 0 | \n",
" 13 | \n",
" 7.07000 | \n",
" 1024 | \n",
"
\n",
" \n",
" 2 | \n",
" 31 | \n",
" 0 | \n",
" 0.00000 | \n",
" 0.00000 | \n",
" 14 | \n",
" 9500 | \n",
" 1 | \n",
" 1 | \n",
" 0 | \n",
" 0.35767 | \n",
" ... | \n",
" 0 | \n",
" 5020 | \n",
" 0 | \n",
" 5020 | \n",
" 0 | \n",
" 11 | \n",
" 0 | \n",
" 0 | \n",
" 1.43000 | \n",
" 961 | \n",
"
\n",
" \n",
" 3 | \n",
" 44 | \n",
" 0 | \n",
" 0.00000 | \n",
" 0.00000 | \n",
" 17 | \n",
" 42500 | \n",
" 1 | \n",
" 1 | \n",
" 1 | \n",
" 3.02189 | \n",
" ... | \n",
" 1 | \n",
" 2815 | \n",
" 0 | \n",
" 2815 | \n",
" 0 | \n",
" 21 | \n",
" 0 | \n",
" 0 | \n",
" 20.53000 | \n",
" 1936 | \n",
"
\n",
" \n",
"
\n",
"
3 rows × 30 columns
\n",
"
"
],
"text/plain": [
" age black clerical construc educ earns74 gdhlth inlf smsa lhrwage \\\n",
"1 32 0 0.00000 0.00000 12 0 0 1 0 1.95586 \n",
"2 31 0 0.00000 0.00000 14 9500 1 1 0 0.35767 \n",
"3 44 0 0.00000 0.00000 17 42500 1 1 1 3.02189 \n",
"\n",
" ... spwrk75 totwrk union worknrm workscnd exper yngkid yrsmarr \\\n",
"1 ... 0 3438 0 3438 0 14 0 13 \n",
"2 ... 0 5020 0 5020 0 11 0 0 \n",
"3 ... 1 2815 0 2815 0 21 0 0 \n",
"\n",
" hrwage agesq \n",
"1 7.07000 1024 \n",
"2 1.43000 961 \n",
"3 20.53000 1936 \n",
"\n",
"[3 rows x 30 columns]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pandas as pd\n",
"\n",
"pd.set_option(\"display.precision\", 3)\n",
"pd.set_option('display.float_format', lambda x: '%.5f' % x)\n",
"\n",
"import warnings\n",
"\n",
"import janitor\n",
"import numpy as np\n",
"import pingouin as pg\n",
"\n",
"warnings.filterwarnings('ignore')\n",
"\n",
"_url = \"https://vincentarelbundock.github.io/Rdatasets/csv/wooldridge/sleep75.csv\"\n",
"drop_var = ['case', 'leis1', 'leis2', 'leis3']\n",
"df = (pd.read_csv(_url, index_col=0)\n",
" .drop(drop_var, axis=1)\n",
"# .assign(lspsepay=lambda df: np.log1p(df.spsepay))\n",
" )\n",
"df.head(3)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fde0505a",
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-21T02:24:44.263338Z",
"start_time": "2023-03-21T02:24:44.233567Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" var | \n",
" group | \n",
" label | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" age | \n",
" age | \n",
" in years | \n",
"
\n",
" \n",
" 1 | \n",
" black | \n",
" other factors | \n",
" =1 if black | \n",
"
\n",
" \n",
" 2 | \n",
" clerical | \n",
" occupation | \n",
" =1 if clerical worker | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" var group label\n",
"0 age age in years\n",
"1 black other factors =1 if black\n",
"2 clerical occupation =1 if clerical worker"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Prep variable lablels (fold cell)\n",
"# varlabels: http://fmwww.bc.edu/ec-p/data/wooldridge/sleep75.des\n",
"df_label = (pd.read_csv('data/sleep75-des.csv', encoding=\"ISO-8859-1\")\n",
" .assign(label=lambda df: df['des'].str.encode('ascii', 'ignore').str.decode('ascii'))\n",
" .drop(['des'], axis=1)\n",
" .set_index('var')\n",
" .drop(drop_var)\n",
" .reset_index()\n",
" )\n",
"\n",
"df_label.head(3)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e1a383bf",
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-21T02:24:46.077018Z",
"start_time": "2023-03-21T02:24:44.264768Z"
},
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" var | \n",
" coef | \n",
" se | \n",
" T | \n",
" pval | \n",
" r2 | \n",
" adj_r2 | \n",
" ll | \n",
" hl | \n",
" model | \n",
" group | \n",
" label | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" age | \n",
" 0.99489 | \n",
" 1.96925 | \n",
" 0.50521 | \n",
" 0.61362 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -2.87382 | \n",
" 4.86360 | \n",
" all | \n",
" age | \n",
" in years | \n",
"
\n",
" \n",
" 1 | \n",
" age | \n",
" 1.48289 | \n",
" 2.88546 | \n",
" 0.51392 | \n",
" 0.60772 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -4.19724 | \n",
" 7.16302 | \n",
" men | \n",
" age | \n",
" in years | \n",
"
\n",
" \n",
" 2 | \n",
" age | \n",
" 0.95320 | \n",
" 2.94321 | \n",
" 0.32386 | \n",
" 0.74634 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -4.84672 | \n",
" 6.75312 | \n",
" women | \n",
" age | \n",
" in years | \n",
"
\n",
" \n",
" 3 | \n",
" age | \n",
" 22.63402 | \n",
" 15.49532 | \n",
" 1.46070 | \n",
" 0.14931 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -8.36124 | \n",
" 53.62927 | \n",
" young kids | \n",
" age | \n",
" in years | \n",
"
\n",
" \n",
" 4 | \n",
" black | \n",
" -84.79661 | \n",
" 82.15012 | \n",
" -1.03222 | \n",
" 0.30245 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -246.18572 | \n",
" 76.59249 | \n",
" all | \n",
" other factors | \n",
" =1 if black | \n",
"
\n",
" \n",
" 5 | \n",
" black | \n",
" -132.96711 | \n",
" 132.58117 | \n",
" -1.00291 | \n",
" 0.31678 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -393.95764 | \n",
" 128.02342 | \n",
" men | \n",
" other factors | \n",
" =1 if black | \n",
"
\n",
" \n",
" 6 | \n",
" black | \n",
" -68.35063 | \n",
" 108.90175 | \n",
" -0.62764 | \n",
" 0.53088 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -282.95362 | \n",
" 146.25236 | \n",
" women | \n",
" other factors | \n",
" =1 if black | \n",
"
\n",
" \n",
" 7 | \n",
" black | \n",
" -119.74104 | \n",
" 296.43295 | \n",
" -0.40394 | \n",
" 0.68769 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -712.69523 | \n",
" 473.21315 | \n",
" young kids | \n",
" other factors | \n",
" =1 if black | \n",
"
\n",
" \n",
" 8 | \n",
" clerical | \n",
" 22.50583 | \n",
" 48.96326 | \n",
" 0.45965 | \n",
" 0.64596 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -73.68559 | \n",
" 118.69725 | \n",
" all | \n",
" occupation | \n",
" =1 if clerical worker | \n",
"
\n",
" \n",
" 9 | \n",
" clerical | \n",
" -229.27596 | \n",
" 102.88822 | \n",
" -2.22840 | \n",
" 0.02665 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -431.81492 | \n",
" -26.73700 | \n",
" men | \n",
" occupation | \n",
" =1 if clerical worker | \n",
"
\n",
" \n",
" 10 | \n",
" clerical | \n",
" 106.50726 | \n",
" 61.12732 | \n",
" 1.74238 | \n",
" 0.08281 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -13.95091 | \n",
" 226.96543 | \n",
" women | \n",
" occupation | \n",
" =1 if clerical worker | \n",
"
\n",
" \n",
" 11 | \n",
" clerical | \n",
" -239.90257 | \n",
" 211.76020 | \n",
" -1.13290 | \n",
" 0.26176 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -663.48604 | \n",
" 183.68089 | \n",
" young kids | \n",
" occupation | \n",
" =1 if clerical worker | \n",
"
\n",
" \n",
" 12 | \n",
" construc | \n",
" 113.92956 | \n",
" 105.04514 | \n",
" 1.08458 | \n",
" 0.27861 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -92.43824 | \n",
" 320.29736 | \n",
" all | \n",
" occupation | \n",
" =1 if construction worker | \n",
"
\n",
" \n",
" 13 | \n",
" construc | \n",
" 65.69606 | \n",
" 107.88921 | \n",
" 0.60892 | \n",
" 0.54307 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -146.68751 | \n",
" 278.07963 | \n",
" men | \n",
" occupation | \n",
" =1 if construction worker | \n",
"
\n",
" \n",
" 14 | \n",
" construc | \n",
" 361.75643 | \n",
" 428.26483 | \n",
" 0.84470 | \n",
" 0.39918 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -482.18695 | \n",
" 1205.69980 | \n",
" women | \n",
" occupation | \n",
" =1 if construction worker | \n",
"
\n",
" \n",
" 15 | \n",
" construc | \n",
" -186.41520 | \n",
" 358.22350 | \n",
" -0.52039 | \n",
" 0.60471 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -902.96889 | \n",
" 530.13849 | \n",
" young kids | \n",
" occupation | \n",
" =1 if construction worker | \n",
"
\n",
" \n",
" 16 | \n",
" educ | \n",
" -7.21056 | \n",
" 7.51674 | \n",
" -0.95927 | \n",
" 0.33787 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -21.97766 | \n",
" 7.55654 | \n",
" all | \n",
" labor factors | \n",
" years of schooling | \n",
"
\n",
" \n",
" 17 | \n",
" educ | \n",
" -7.54542 | \n",
" 9.72034 | \n",
" -0.77625 | \n",
" 0.43826 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -26.68024 | \n",
" 11.58940 | \n",
" men | \n",
" labor factors | \n",
" years of schooling | \n",
"
\n",
" \n",
" 18 | \n",
" educ | \n",
" -6.27840 | \n",
" 11.86988 | \n",
" -0.52894 | \n",
" 0.59737 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -29.66932 | \n",
" 17.11252 | \n",
" women | \n",
" labor factors | \n",
" years of schooling | \n",
"
\n",
" \n",
" 19 | \n",
" educ | \n",
" -20.97212 | \n",
" 26.97403 | \n",
" -0.77749 | \n",
" 0.43992 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -74.92821 | \n",
" 32.98398 | \n",
" young kids | \n",
" labor factors | \n",
" years of schooling | \n",
"
\n",
" \n",
" 20 | \n",
" gdhlth | \n",
" -82.58043 | \n",
" 58.20472 | \n",
" -1.41879 | \n",
" 0.15656 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -196.92727 | \n",
" 31.76641 | \n",
" all | \n",
" health factors | \n",
" =1 if in good or excel. health | \n",
"
\n",
" \n",
" 21 | \n",
" gdhlth | \n",
" -188.28451 | \n",
" 80.47654 | \n",
" -2.33962 | \n",
" 0.02001 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -346.70531 | \n",
" -29.86371 | \n",
" men | \n",
" health factors | \n",
" =1 if in good or excel. health | \n",
"
\n",
" \n",
" 22 | \n",
" gdhlth | \n",
" -24.49158 | \n",
" 87.71687 | \n",
" -0.27921 | \n",
" 0.78034 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -197.34740 | \n",
" 148.36424 | \n",
" women | \n",
" health factors | \n",
" =1 if in good or excel. health | \n",
"
\n",
" \n",
" 23 | \n",
" gdhlth | \n",
" -141.81498 | \n",
" 203.21302 | \n",
" -0.69786 | \n",
" 0.48796 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -548.30154 | \n",
" 264.67159 | \n",
" young kids | \n",
" health factors | \n",
" =1 if in good or excel. health | \n",
"
\n",
" \n",
" 24 | \n",
" lhrwage | \n",
" 21.60570 | \n",
" 31.98930 | \n",
" 0.67540 | \n",
" 0.49972 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -41.23930 | \n",
" 84.45070 | \n",
" all | \n",
" labor factors | \n",
" log hourly wage | \n",
"
\n",
" \n",
" 25 | \n",
" lhrwage | \n",
" 3.54475 | \n",
" 47.04592 | \n",
" 0.07535 | \n",
" 0.93999 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -89.06675 | \n",
" 96.15625 | \n",
" men | \n",
" labor factors | \n",
" log hourly wage | \n",
"
\n",
" \n",
" 26 | \n",
" lhrwage | \n",
" -22.58048 | \n",
" 53.78232 | \n",
" -0.41985 | \n",
" 0.67500 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -128.56451 | \n",
" 83.40355 | \n",
" women | \n",
" labor factors | \n",
" log hourly wage | \n",
"
\n",
" \n",
" 27 | \n",
" lhrwage | \n",
" 35.60932 | \n",
" 122.32374 | \n",
" 0.29111 | \n",
" 0.77197 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -209.07459 | \n",
" 280.29322 | \n",
" young kids | \n",
" labor factors | \n",
" log hourly wage | \n",
"
\n",
" \n",
" 28 | \n",
" prot | \n",
" -9.24943 | \n",
" 39.77146 | \n",
" -0.23256 | \n",
" 0.81619 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -87.38298 | \n",
" 68.88412 | \n",
" all | \n",
" other factors | \n",
" =1 if Protestant | \n",
"
\n",
" \n",
" 29 | \n",
" prot | \n",
" -20.96733 | \n",
" 52.20106 | \n",
" -0.40166 | \n",
" 0.68824 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -123.72689 | \n",
" 81.79223 | \n",
" men | \n",
" other factors | \n",
" =1 if Protestant | \n",
"
\n",
" \n",
" 30 | \n",
" prot | \n",
" 1.06664 | \n",
" 61.89805 | \n",
" 0.01723 | \n",
" 0.98627 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -120.91033 | \n",
" 123.04361 | \n",
" women | \n",
" other factors | \n",
" =1 if Protestant | \n",
"
\n",
" \n",
" 31 | \n",
" prot | \n",
" -24.17312 | \n",
" 127.56650 | \n",
" -0.18949 | \n",
" 0.85035 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -279.34411 | \n",
" 230.99786 | \n",
" young kids | \n",
" other factors | \n",
" =1 if Protestant | \n",
"
\n",
" \n",
" 32 | \n",
" selfe | \n",
" -21.27418 | \n",
" 63.30392 | \n",
" -0.33606 | \n",
" 0.73696 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -145.63872 | \n",
" 103.09036 | \n",
" all | \n",
" labor factors | \n",
" =1 if self employed | \n",
"
\n",
" \n",
" 33 | \n",
" selfe | \n",
" 3.48409 | \n",
" 77.58571 | \n",
" 0.04491 | \n",
" 0.96421 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -149.24600 | \n",
" 156.21419 | \n",
" men | \n",
" labor factors | \n",
" =1 if self employed | \n",
"
\n",
" \n",
" 34 | \n",
" selfe | \n",
" -90.68594 | \n",
" 112.55650 | \n",
" -0.80569 | \n",
" 0.42127 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -312.49100 | \n",
" 131.11913 | \n",
" women | \n",
" labor factors | \n",
" =1 if self employed | \n",
"
\n",
" \n",
" 35 | \n",
" selfe | \n",
" -370.95651 | \n",
" 241.15347 | \n",
" -1.53826 | \n",
" 0.12924 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -853.33528 | \n",
" 111.42226 | \n",
" young kids | \n",
" labor factors | \n",
" =1 if self employed | \n",
"
\n",
" \n",
" 36 | \n",
" smsa | \n",
" -40.65356 | \n",
" 39.50772 | \n",
" -1.02900 | \n",
" 0.30396 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -118.26897 | \n",
" 36.96186 | \n",
" all | \n",
" area of residence | \n",
" =1 if live in smsa | \n",
"
\n",
" \n",
" 37 | \n",
" smsa | \n",
" -27.63143 | \n",
" 52.41859 | \n",
" -0.52713 | \n",
" 0.59852 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -130.81921 | \n",
" 75.55635 | \n",
" men | \n",
" area of residence | \n",
" =1 if live in smsa | \n",
"
\n",
" \n",
" 38 | \n",
" smsa | \n",
" -57.14640 | \n",
" 61.52103 | \n",
" -0.92889 | \n",
" 0.35394 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -178.38042 | \n",
" 64.08762 | \n",
" women | \n",
" area of residence | \n",
" =1 if live in smsa | \n",
"
\n",
" \n",
" 39 | \n",
" smsa | \n",
" 43.42957 | \n",
" 138.68309 | \n",
" 0.31316 | \n",
" 0.75525 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -233.97791 | \n",
" 320.83705 | \n",
" young kids | \n",
" area of residence | \n",
" =1 if live in smsa | \n",
"
\n",
" \n",
" 40 | \n",
" south | \n",
" 82.48698 | \n",
" 46.37757 | \n",
" 1.77860 | \n",
" 0.07589 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -8.62469 | \n",
" 173.59864 | \n",
" all | \n",
" area of residence | \n",
" =1 if live in south | \n",
"
\n",
" \n",
" 41 | \n",
" south | \n",
" 79.40169 | \n",
" 64.43983 | \n",
" 1.23218 | \n",
" 0.21892 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -47.45031 | \n",
" 206.25369 | \n",
" men | \n",
" area of residence | \n",
" =1 if live in south | \n",
"
\n",
" \n",
" 42 | \n",
" south | \n",
" 114.62293 | \n",
" 69.30049 | \n",
" 1.65400 | \n",
" 0.09953 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -21.94138 | \n",
" 251.18725 | \n",
" women | \n",
" area of residence | \n",
" =1 if live in south | \n",
"
\n",
" \n",
" 43 | \n",
" south | \n",
" 71.85138 | \n",
" 136.01011 | \n",
" 0.52828 | \n",
" 0.59925 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -200.20935 | \n",
" 343.91211 | \n",
" young kids | \n",
" area of residence | \n",
" =1 if live in south | \n",
"
\n",
" \n",
" 44 | \n",
" spsepay | \n",
" -0.00101 | \n",
" 0.00265 | \n",
" -0.37974 | \n",
" 0.70430 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -0.00621 | \n",
" 0.00420 | \n",
" all | \n",
" other factors | \n",
" spousal wage income | \n",
"
\n",
" \n",
" 45 | \n",
" spsepay | \n",
" 0.00307 | \n",
" 0.00492 | \n",
" 0.62334 | \n",
" 0.53357 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -0.00662 | \n",
" 0.01276 | \n",
" men | \n",
" other factors | \n",
" spousal wage income | \n",
"
\n",
" \n",
" 46 | \n",
" spsepay | \n",
" -0.00051 | \n",
" 0.00362 | \n",
" -0.14032 | \n",
" 0.88853 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -0.00764 | \n",
" 0.00663 | \n",
" women | \n",
" other factors | \n",
" spousal wage income | \n",
"
\n",
" \n",
" 47 | \n",
" spsepay | \n",
" -0.00470 | \n",
" 0.01144 | \n",
" -0.41085 | \n",
" 0.68265 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -0.02759 | \n",
" 0.01819 | \n",
" young kids | \n",
" other factors | \n",
" spousal wage income | \n",
"
\n",
" \n",
" 48 | \n",
" totwrk | \n",
" -0.15192 | \n",
" 0.02037 | \n",
" -7.45943 | \n",
" 0.00000 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -0.19193 | \n",
" -0.11191 | \n",
" all | \n",
" labor factors | \n",
" mins worked per week | \n",
"
\n",
" \n",
" 49 | \n",
" totwrk | \n",
" -0.19101 | \n",
" 0.02977 | \n",
" -6.41715 | \n",
" 0.00000 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -0.24960 | \n",
" -0.13241 | \n",
" men | \n",
" labor factors | \n",
" mins worked per week | \n",
"
\n",
" \n",
" 50 | \n",
" totwrk | \n",
" -0.12551 | \n",
" 0.03271 | \n",
" -3.83678 | \n",
" 0.00016 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -0.18998 | \n",
" -0.06105 | \n",
" women | \n",
" labor factors | \n",
" mins worked per week | \n",
"
\n",
" \n",
" 51 | \n",
" totwrk | \n",
" -0.11299 | \n",
" 0.07636 | \n",
" -1.47971 | \n",
" 0.14418 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -0.26574 | \n",
" 0.03975 | \n",
" young kids | \n",
" labor factors | \n",
" mins worked per week | \n",
"
\n",
" \n",
" 52 | \n",
" yrsmarr | \n",
" -0.07596 | \n",
" 2.00936 | \n",
" -0.03780 | \n",
" 0.96986 | \n",
" 0.12729 | \n",
" 0.10366 | \n",
" -4.02346 | \n",
" 3.87155 | \n",
" all | \n",
" family factors | \n",
" years married | \n",
"
\n",
" \n",
" 53 | \n",
" yrsmarr | \n",
" 0.22412 | \n",
" 2.93479 | \n",
" 0.07637 | \n",
" 0.93918 | \n",
" 0.18038 | \n",
" 0.13911 | \n",
" -5.55312 | \n",
" 6.00135 | \n",
" men | \n",
" family factors | \n",
" years married | \n",
"
\n",
" \n",
" 54 | \n",
" yrsmarr | \n",
" -1.41414 | \n",
" 2.93198 | \n",
" -0.48231 | \n",
" 0.63005 | \n",
" 0.10911 | \n",
" 0.05342 | \n",
" -7.19194 | \n",
" 4.36366 | \n",
" women | \n",
" family factors | \n",
" years married | \n",
"
\n",
" \n",
" 55 | \n",
" yrsmarr | \n",
" -31.21114 | \n",
" 16.91401 | \n",
" -1.84528 | \n",
" 0.06993 | \n",
" 0.17815 | \n",
" -0.01362 | \n",
" -65.04420 | \n",
" 2.62192 | \n",
" young kids | \n",
" family factors | \n",
" years married | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" var coef se T pval r2 adj_r2 \\\n",
"0 age 0.99489 1.96925 0.50521 0.61362 0.12729 0.10366 \n",
"1 age 1.48289 2.88546 0.51392 0.60772 0.18038 0.13911 \n",
"2 age 0.95320 2.94321 0.32386 0.74634 0.10911 0.05342 \n",
"3 age 22.63402 15.49532 1.46070 0.14931 0.17815 -0.01362 \n",
"4 black -84.79661 82.15012 -1.03222 0.30245 0.12729 0.10366 \n",
"5 black -132.96711 132.58117 -1.00291 0.31678 0.18038 0.13911 \n",
"6 black -68.35063 108.90175 -0.62764 0.53088 0.10911 0.05342 \n",
"7 black -119.74104 296.43295 -0.40394 0.68769 0.17815 -0.01362 \n",
"8 clerical 22.50583 48.96326 0.45965 0.64596 0.12729 0.10366 \n",
"9 clerical -229.27596 102.88822 -2.22840 0.02665 0.18038 0.13911 \n",
"10 clerical 106.50726 61.12732 1.74238 0.08281 0.10911 0.05342 \n",
"11 clerical -239.90257 211.76020 -1.13290 0.26176 0.17815 -0.01362 \n",
"12 construc 113.92956 105.04514 1.08458 0.27861 0.12729 0.10366 \n",
"13 construc 65.69606 107.88921 0.60892 0.54307 0.18038 0.13911 \n",
"14 construc 361.75643 428.26483 0.84470 0.39918 0.10911 0.05342 \n",
"15 construc -186.41520 358.22350 -0.52039 0.60471 0.17815 -0.01362 \n",
"16 educ -7.21056 7.51674 -0.95927 0.33787 0.12729 0.10366 \n",
"17 educ -7.54542 9.72034 -0.77625 0.43826 0.18038 0.13911 \n",
"18 educ -6.27840 11.86988 -0.52894 0.59737 0.10911 0.05342 \n",
"19 educ -20.97212 26.97403 -0.77749 0.43992 0.17815 -0.01362 \n",
"20 gdhlth -82.58043 58.20472 -1.41879 0.15656 0.12729 0.10366 \n",
"21 gdhlth -188.28451 80.47654 -2.33962 0.02001 0.18038 0.13911 \n",
"22 gdhlth -24.49158 87.71687 -0.27921 0.78034 0.10911 0.05342 \n",
"23 gdhlth -141.81498 203.21302 -0.69786 0.48796 0.17815 -0.01362 \n",
"24 lhrwage 21.60570 31.98930 0.67540 0.49972 0.12729 0.10366 \n",
"25 lhrwage 3.54475 47.04592 0.07535 0.93999 0.18038 0.13911 \n",
"26 lhrwage -22.58048 53.78232 -0.41985 0.67500 0.10911 0.05342 \n",
"27 lhrwage 35.60932 122.32374 0.29111 0.77197 0.17815 -0.01362 \n",
"28 prot -9.24943 39.77146 -0.23256 0.81619 0.12729 0.10366 \n",
"29 prot -20.96733 52.20106 -0.40166 0.68824 0.18038 0.13911 \n",
"30 prot 1.06664 61.89805 0.01723 0.98627 0.10911 0.05342 \n",
"31 prot -24.17312 127.56650 -0.18949 0.85035 0.17815 -0.01362 \n",
"32 selfe -21.27418 63.30392 -0.33606 0.73696 0.12729 0.10366 \n",
"33 selfe 3.48409 77.58571 0.04491 0.96421 0.18038 0.13911 \n",
"34 selfe -90.68594 112.55650 -0.80569 0.42127 0.10911 0.05342 \n",
"35 selfe -370.95651 241.15347 -1.53826 0.12924 0.17815 -0.01362 \n",
"36 smsa -40.65356 39.50772 -1.02900 0.30396 0.12729 0.10366 \n",
"37 smsa -27.63143 52.41859 -0.52713 0.59852 0.18038 0.13911 \n",
"38 smsa -57.14640 61.52103 -0.92889 0.35394 0.10911 0.05342 \n",
"39 smsa 43.42957 138.68309 0.31316 0.75525 0.17815 -0.01362 \n",
"40 south 82.48698 46.37757 1.77860 0.07589 0.12729 0.10366 \n",
"41 south 79.40169 64.43983 1.23218 0.21892 0.18038 0.13911 \n",
"42 south 114.62293 69.30049 1.65400 0.09953 0.10911 0.05342 \n",
"43 south 71.85138 136.01011 0.52828 0.59925 0.17815 -0.01362 \n",
"44 spsepay -0.00101 0.00265 -0.37974 0.70430 0.12729 0.10366 \n",
"45 spsepay 0.00307 0.00492 0.62334 0.53357 0.18038 0.13911 \n",
"46 spsepay -0.00051 0.00362 -0.14032 0.88853 0.10911 0.05342 \n",
"47 spsepay -0.00470 0.01144 -0.41085 0.68265 0.17815 -0.01362 \n",
"48 totwrk -0.15192 0.02037 -7.45943 0.00000 0.12729 0.10366 \n",
"49 totwrk -0.19101 0.02977 -6.41715 0.00000 0.18038 0.13911 \n",
"50 totwrk -0.12551 0.03271 -3.83678 0.00016 0.10911 0.05342 \n",
"51 totwrk -0.11299 0.07636 -1.47971 0.14418 0.17815 -0.01362 \n",
"52 yrsmarr -0.07596 2.00936 -0.03780 0.96986 0.12729 0.10366 \n",
"53 yrsmarr 0.22412 2.93479 0.07637 0.93918 0.18038 0.13911 \n",
"54 yrsmarr -1.41414 2.93198 -0.48231 0.63005 0.10911 0.05342 \n",
"55 yrsmarr -31.21114 16.91401 -1.84528 0.06993 0.17815 -0.01362 \n",
"\n",
" ll hl model group \\\n",
"0 -2.87382 4.86360 all age \n",
"1 -4.19724 7.16302 men age \n",
"2 -4.84672 6.75312 women age \n",
"3 -8.36124 53.62927 young kids age \n",
"4 -246.18572 76.59249 all other factors \n",
"5 -393.95764 128.02342 men other factors \n",
"6 -282.95362 146.25236 women other factors \n",
"7 -712.69523 473.21315 young kids other factors \n",
"8 -73.68559 118.69725 all occupation \n",
"9 -431.81492 -26.73700 men occupation \n",
"10 -13.95091 226.96543 women occupation \n",
"11 -663.48604 183.68089 young kids occupation \n",
"12 -92.43824 320.29736 all occupation \n",
"13 -146.68751 278.07963 men occupation \n",
"14 -482.18695 1205.69980 women occupation \n",
"15 -902.96889 530.13849 young kids occupation \n",
"16 -21.97766 7.55654 all labor factors \n",
"17 -26.68024 11.58940 men labor factors \n",
"18 -29.66932 17.11252 women labor factors \n",
"19 -74.92821 32.98398 young kids labor factors \n",
"20 -196.92727 31.76641 all health factors \n",
"21 -346.70531 -29.86371 men health factors \n",
"22 -197.34740 148.36424 women health factors \n",
"23 -548.30154 264.67159 young kids health factors \n",
"24 -41.23930 84.45070 all labor factors \n",
"25 -89.06675 96.15625 men labor factors \n",
"26 -128.56451 83.40355 women labor factors \n",
"27 -209.07459 280.29322 young kids labor factors \n",
"28 -87.38298 68.88412 all other factors \n",
"29 -123.72689 81.79223 men other factors \n",
"30 -120.91033 123.04361 women other factors \n",
"31 -279.34411 230.99786 young kids other factors \n",
"32 -145.63872 103.09036 all labor factors \n",
"33 -149.24600 156.21419 men labor factors \n",
"34 -312.49100 131.11913 women labor factors \n",
"35 -853.33528 111.42226 young kids labor factors \n",
"36 -118.26897 36.96186 all area of residence \n",
"37 -130.81921 75.55635 men area of residence \n",
"38 -178.38042 64.08762 women area of residence \n",
"39 -233.97791 320.83705 young kids area of residence \n",
"40 -8.62469 173.59864 all area of residence \n",
"41 -47.45031 206.25369 men area of residence \n",
"42 -21.94138 251.18725 women area of residence \n",
"43 -200.20935 343.91211 young kids area of residence \n",
"44 -0.00621 0.00420 all other factors \n",
"45 -0.00662 0.01276 men other factors \n",
"46 -0.00764 0.00663 women other factors \n",
"47 -0.02759 0.01819 young kids other factors \n",
"48 -0.19193 -0.11191 all labor factors \n",
"49 -0.24960 -0.13241 men labor factors \n",
"50 -0.18998 -0.06105 women labor factors \n",
"51 -0.26574 0.03975 young kids labor factors \n",
"52 -4.02346 3.87155 all family factors \n",
"53 -5.55312 6.00135 men family factors \n",
"54 -7.19194 4.36366 women family factors \n",
"55 -65.04420 2.62192 young kids family factors \n",
"\n",
" label \n",
"0 in years \n",
"1 in years \n",
"2 in years \n",
"3 in years \n",
"4 =1 if black \n",
"5 =1 if black \n",
"6 =1 if black \n",
"7 =1 if black \n",
"8 =1 if clerical worker \n",
"9 =1 if clerical worker \n",
"10 =1 if clerical worker \n",
"11 =1 if clerical worker \n",
"12 =1 if construction worker \n",
"13 =1 if construction worker \n",
"14 =1 if construction worker \n",
"15 =1 if construction worker \n",
"16 years of schooling \n",
"17 years of schooling \n",
"18 years of schooling \n",
"19 years of schooling \n",
"20 =1 if in good or excel. health \n",
"21 =1 if in good or excel. health \n",
"22 =1 if in good or excel. health \n",
"23 =1 if in good or excel. health \n",
"24 log hourly wage \n",
"25 log hourly wage \n",
"26 log hourly wage \n",
"27 log hourly wage \n",
"28 =1 if Protestant \n",
"29 =1 if Protestant \n",
"30 =1 if Protestant \n",
"31 =1 if Protestant \n",
"32 =1 if self employed \n",
"33 =1 if self employed \n",
"34 =1 if self employed \n",
"35 =1 if self employed \n",
"36 =1 if live in smsa \n",
"37 =1 if live in smsa \n",
"38 =1 if live in smsa \n",
"39 =1 if live in smsa \n",
"40 =1 if live in south \n",
"41 =1 if live in south \n",
"42 =1 if live in south \n",
"43 =1 if live in south \n",
"44 spousal wage income \n",
"45 spousal wage income \n",
"46 spousal wage income \n",
"47 spousal wage income \n",
"48 mins worked per week \n",
"49 mins worked per week \n",
"50 mins worked per week \n",
"51 mins worked per week \n",
"52 years married \n",
"53 years married \n",
"54 years married \n",
"55 years married "
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"stdopts = {'relimp': False, 'remove_na': True}\n",
"x = [\n",
" \"age\",\n",
" \"black\",\n",
" \"clerical\",\n",
" \"construc\",\n",
" \"educ\",\n",
" \"gdhlth\",\n",
" \"inlf\",\n",
" \"smsa\",\n",
" \"lhrwage\",\n",
" \"prot\",\n",
" \"selfe\",\n",
" \"south\",\n",
" \"spsepay\",\n",
" \"totwrk\",\n",
" \"yrsmarr\",\n",
"]\n",
"lm_all = (pg.linear_regression(df[x], df['sleep'], **stdopts)\n",
" .assign(model='all')\n",
" )\n",
"lm_male = (pg.linear_regression(df.query('male==1')[x], df.query('male==1')['sleep'], **stdopts)\n",
" .assign(model='men')\n",
" )\n",
"lm_female = (pg.linear_regression(df.query('male==0')[x], df.query('male==0')['sleep'], **stdopts)\n",
" .assign(model='women')\n",
" )\n",
"lm_kids = (pg.linear_regression(df.query('yngkid==1')[x], df.query('yngkid==1')['sleep'], **stdopts)\n",
" .assign(model='young kids')\n",
" )\n",
"df_results = (pd.concat([lm_all, lm_female, lm_male, lm_kids])\n",
" .query('names!=\"Intercept\"')\n",
" .reset_index(drop=True)\n",
" # Get labels\n",
" .rename_column('names', 'var')\n",
" .merge(df_label, how='left', on='var', validate='m:1')\n",
" .sort_values(['var', 'model', 'group'])\n",
" .reset_index(drop=True)\n",
" # Tidy up columns\n",
" .rename_column(\"CI[2.5%]\", \"ll\")\n",
" .rename_column(\"CI[97.5%]\", \"hl\")\n",
" )\n",
"df_results"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f0cbd40e",
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-21T02:24:46.188049Z",
"start_time": "2023-03-21T02:24:46.079021Z"
}
},
"outputs": [],
"source": [
"df_results.to_csv('../examples/data/sleep-mmodel.csv', index=False)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "d2283891",
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-21T02:24:46.204047Z",
"start_time": "2023-03-21T02:24:46.190050Z"
}
},
"outputs": [],
"source": [
"# _cols = ['var', 'label', 'coef', 'model', 'group', 'pval', 'll', 'hl']\n",
"# df_results[_cols].head(6).to_markdown()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ef007948",
"metadata": {},
"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.9.12"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 5
}