{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples and Exercises from Think Stats, 2nd Edition\n", "\n", "http://thinkstats2.com\n", "\n", "Copyright 2016 Allen B. Downey\n", "\n", "MIT License: https://opensource.org/licenses/MIT\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from __future__ import print_function, division\n", "\n", "%matplotlib inline\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "import random\n", "\n", "import thinkstats2\n", "import thinkplot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multiple regression\n", "\n", "Let's load up the NSFG data again." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import first\n", "\n", "live, firsts, others = first.MakeFrames()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's birth weight as a function of mother's age (which we saw in the previous chapter)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
OLS Regression Results
Dep. Variable: totalwgt_lb R-squared: 0.005
Model: OLS Adj. R-squared: 0.005
Method: Least Squares F-statistic: 43.02
Date: Mon, 25 Feb 2019 Prob (F-statistic): 5.72e-11
Time: 16:34:15 Log-Likelihood: -15897.
No. Observations: 9038 AIC: 3.180e+04
Df Residuals: 9036 BIC: 3.181e+04
Df Model: 1
Covariance Type: nonrobust
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err t P>|t| [0.025 0.975]
Intercept 6.8304 0.068 100.470 0.000 6.697 6.964
agepreg 0.0175 0.003 6.559 0.000 0.012 0.023
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Omnibus: 1024.052 Durbin-Watson: 1.618
Prob(Omnibus): 0.000 Jarque-Bera (JB): 3081.833
Skew: -0.601 Prob(JB): 0.00
Kurtosis: 5.596 Cond. No. 118.


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified." ], "text/plain": [ "\n", "\"\"\"\n", " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: totalwgt_lb R-squared: 0.005\n", "Model: OLS Adj. R-squared: 0.005\n", "Method: Least Squares F-statistic: 43.02\n", "Date: Mon, 25 Feb 2019 Prob (F-statistic): 5.72e-11\n", "Time: 16:34:15 Log-Likelihood: -15897.\n", "No. Observations: 9038 AIC: 3.180e+04\n", "Df Residuals: 9036 BIC: 3.181e+04\n", "Df Model: 1 \n", "Covariance Type: nonrobust \n", "==============================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "Intercept 6.8304 0.068 100.470 0.000 6.697 6.964\n", "agepreg 0.0175 0.003 6.559 0.000 0.012 0.023\n", "==============================================================================\n", "Omnibus: 1024.052 Durbin-Watson: 1.618\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 3081.833\n", "Skew: -0.601 Prob(JB): 0.00\n", "Kurtosis: 5.596 Cond. No. 118.\n", "==============================================================================\n", "\n", "Warnings:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", "\"\"\"" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import statsmodels.formula.api as smf\n", "\n", "formula = 'totalwgt_lb ~ agepreg'\n", "model = smf.ols(formula, data=live)\n", "results = model.fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can extract the parameters." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(6.830396973311047, 0.017453851471802877)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "inter = results.params['Intercept']\n", "slope = results.params['agepreg']\n", "inter, slope" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the p-value of the slope estimate." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5.722947107312786e-11" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "slope_pvalue = results.pvalues['agepreg']\n", "slope_pvalue" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the coefficient of determination." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.004738115474710369" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "results.rsquared" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The difference in birth weight between first babies and others." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.12476118453549034" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff_weight = firsts.totalwgt_lb.mean() - others.totalwgt_lb.mean()\n", "diff_weight" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The difference in age between mothers of first babies and others." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-3.5864347661500275" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "diff_age = firsts.agepreg.mean() - others.agepreg.mean()\n", "diff_age" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The age difference plausibly explains about half of the difference in weight." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "-0.06259709972169267" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "slope * diff_age" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Running a single regression with a categorical variable, `isfirst`:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
OLS Regression Results
Dep. Variable: totalwgt_lb R-squared: 0.002
Model: OLS Adj. R-squared: 0.002
Method: Least Squares F-statistic: 17.74
Date: Mon, 25 Feb 2019 Prob (F-statistic): 2.55e-05
Time: 16:34:16 Log-Likelihood: -15909.
No. Observations: 9038 AIC: 3.182e+04
Df Residuals: 9036 BIC: 3.184e+04
Df Model: 1
Covariance Type: nonrobust
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err t P>|t| [0.025 0.975]
Intercept 7.3259 0.021 356.007 0.000 7.286 7.366
isfirst[T.True] -0.1248 0.030 -4.212 0.000 -0.183 -0.067
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Omnibus: 988.919 Durbin-Watson: 1.613
Prob(Omnibus): 0.000 Jarque-Bera (JB): 2897.107
Skew: -0.589 Prob(JB): 0.00
Kurtosis: 5.511 Cond. No. 2.58


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified." ], "text/plain": [ "\n", "\"\"\"\n", " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: totalwgt_lb R-squared: 0.002\n", "Model: OLS Adj. R-squared: 0.002\n", "Method: Least Squares F-statistic: 17.74\n", "Date: Mon, 25 Feb 2019 Prob (F-statistic): 2.55e-05\n", "Time: 16:34:16 Log-Likelihood: -15909.\n", "No. Observations: 9038 AIC: 3.182e+04\n", "Df Residuals: 9036 BIC: 3.184e+04\n", "Df Model: 1 \n", "Covariance Type: nonrobust \n", "===================================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "-----------------------------------------------------------------------------------\n", "Intercept 7.3259 0.021 356.007 0.000 7.286 7.366\n", "isfirst[T.True] -0.1248 0.030 -4.212 0.000 -0.183 -0.067\n", "==============================================================================\n", "Omnibus: 988.919 Durbin-Watson: 1.613\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 2897.107\n", "Skew: -0.589 Prob(JB): 0.00\n", "Kurtosis: 5.511 Cond. No. 2.58\n", "==============================================================================\n", "\n", "Warnings:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", "\"\"\"" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "live['isfirst'] = live.birthord == 1\n", "formula = 'totalwgt_lb ~ isfirst'\n", "results = smf.ols(formula, data=live).fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now finally running a multiple regression:" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
OLS Regression Results
Dep. Variable: totalwgt_lb R-squared: 0.005
Model: OLS Adj. R-squared: 0.005
Method: Least Squares F-statistic: 24.02
Date: Mon, 25 Feb 2019 Prob (F-statistic): 3.95e-11
Time: 16:34:16 Log-Likelihood: -15894.
No. Observations: 9038 AIC: 3.179e+04
Df Residuals: 9035 BIC: 3.182e+04
Df Model: 2
Covariance Type: nonrobust
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err t P>|t| [0.025 0.975]
Intercept 6.9142 0.078 89.073 0.000 6.762 7.066
isfirst[T.True] -0.0698 0.031 -2.236 0.025 -0.131 -0.009
agepreg 0.0154 0.003 5.499 0.000 0.010 0.021
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Omnibus: 1019.945 Durbin-Watson: 1.618
Prob(Omnibus): 0.000 Jarque-Bera (JB): 3063.682
Skew: -0.599 Prob(JB): 0.00
Kurtosis: 5.588 Cond. No. 137.


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified." ], "text/plain": [ "\n", "\"\"\"\n", " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: totalwgt_lb R-squared: 0.005\n", "Model: OLS Adj. R-squared: 0.005\n", "Method: Least Squares F-statistic: 24.02\n", "Date: Mon, 25 Feb 2019 Prob (F-statistic): 3.95e-11\n", "Time: 16:34:16 Log-Likelihood: -15894.\n", "No. Observations: 9038 AIC: 3.179e+04\n", "Df Residuals: 9035 BIC: 3.182e+04\n", "Df Model: 2 \n", "Covariance Type: nonrobust \n", "===================================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "-----------------------------------------------------------------------------------\n", "Intercept 6.9142 0.078 89.073 0.000 6.762 7.066\n", "isfirst[T.True] -0.0698 0.031 -2.236 0.025 -0.131 -0.009\n", "agepreg 0.0154 0.003 5.499 0.000 0.010 0.021\n", "==============================================================================\n", "Omnibus: 1019.945 Durbin-Watson: 1.618\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 3063.682\n", "Skew: -0.599 Prob(JB): 0.00\n", "Kurtosis: 5.588 Cond. No. 137.\n", "==============================================================================\n", "\n", "Warnings:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", "\"\"\"" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formula = 'totalwgt_lb ~ isfirst + agepreg'\n", "results = smf.ols(formula, data=live).fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As expected, when we control for mother's age, the apparent difference due to `isfirst` is cut in half.\n", "\n", "If we add age squared, we can control for a quadratic relationship between age and weight." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
OLS Regression Results
Dep. Variable: totalwgt_lb R-squared: 0.007
Model: OLS Adj. R-squared: 0.007
Method: Least Squares F-statistic: 22.64
Date: Mon, 25 Feb 2019 Prob (F-statistic): 1.35e-14
Time: 16:34:16 Log-Likelihood: -15884.
No. Observations: 9038 AIC: 3.178e+04
Df Residuals: 9034 BIC: 3.181e+04
Df Model: 3
Covariance Type: nonrobust
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err t P>|t| [0.025 0.975]
Intercept 5.6923 0.286 19.937 0.000 5.133 6.252
isfirst[T.True] -0.0504 0.031 -1.602 0.109 -0.112 0.011
agepreg 0.1124 0.022 5.113 0.000 0.069 0.155
agepreg2 -0.0018 0.000 -4.447 0.000 -0.003 -0.001
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Omnibus: 1007.149 Durbin-Watson: 1.616
Prob(Omnibus): 0.000 Jarque-Bera (JB): 3003.343
Skew: -0.594 Prob(JB): 0.00
Kurtosis: 5.562 Cond. No. 1.39e+04


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.
[2] The condition number is large, 1.39e+04. This might indicate that there are
strong multicollinearity or other numerical problems." ], "text/plain": [ "\n", "\"\"\"\n", " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: totalwgt_lb R-squared: 0.007\n", "Model: OLS Adj. R-squared: 0.007\n", "Method: Least Squares F-statistic: 22.64\n", "Date: Mon, 25 Feb 2019 Prob (F-statistic): 1.35e-14\n", "Time: 16:34:16 Log-Likelihood: -15884.\n", "No. Observations: 9038 AIC: 3.178e+04\n", "Df Residuals: 9034 BIC: 3.181e+04\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "===================================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "-----------------------------------------------------------------------------------\n", "Intercept 5.6923 0.286 19.937 0.000 5.133 6.252\n", "isfirst[T.True] -0.0504 0.031 -1.602 0.109 -0.112 0.011\n", "agepreg 0.1124 0.022 5.113 0.000 0.069 0.155\n", "agepreg2 -0.0018 0.000 -4.447 0.000 -0.003 -0.001\n", "==============================================================================\n", "Omnibus: 1007.149 Durbin-Watson: 1.616\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 3003.343\n", "Skew: -0.594 Prob(JB): 0.00\n", "Kurtosis: 5.562 Cond. No. 1.39e+04\n", "==============================================================================\n", "\n", "Warnings:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", "[2] The condition number is large, 1.39e+04. This might indicate that there are\n", "strong multicollinearity or other numerical problems.\n", "\"\"\"" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "live['agepreg2'] = live.agepreg**2\n", "formula = 'totalwgt_lb ~ isfirst + agepreg + agepreg2'\n", "results = smf.ols(formula, data=live).fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "When we do that, the apparent effect of `isfirst` gets even smaller, and is no longer statistically significant.\n", "\n", "These results suggest that the apparent difference in weight between first babies and others might be explained by difference in mothers' ages, at least in part." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Mining\n", "\n", "We can use `join` to combine variables from the preganancy and respondent tables." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "import nsfg\n", "\n", "live = live[live.prglngth>30]\n", "resp = nsfg.ReadFemResp()\n", "resp.index = resp.caseid\n", "join = live.join(resp, on='caseid', rsuffix='_r')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And we can search for variables with explanatory power.\n", "\n", "Because we don't clean most of the variables, we are probably missing some good ones." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "import patsy\n", "\n", "def GoMining(df):\n", " \"\"\"Searches for variables that predict birth weight.\n", "\n", " df: DataFrame of pregnancy records\n", "\n", " returns: list of (rsquared, variable name) pairs\n", " \"\"\"\n", " variables = []\n", " for name in df.columns:\n", " try:\n", " if df[name].var() < 1e-7:\n", " continue\n", "\n", " formula = 'totalwgt_lb ~ agepreg + ' + name\n", " \n", " # The following seems to be required in some environments\n", " # formula = formula.encode('ascii')\n", "\n", " model = smf.ols(formula, data=df)\n", " if model.nobs < len(df)/2:\n", " continue\n", "\n", " results = model.fit()\n", " except (ValueError, TypeError):\n", " continue\n", "\n", " variables.append((results.rsquared, name))\n", "\n", " return variables" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "variables = GoMining(join)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following functions report the variables with the highest values of $R^2$." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "import re\n", "\n", "def ReadVariables():\n", " \"\"\"Reads Stata dictionary files for NSFG data.\n", "\n", " returns: DataFrame that maps variables names to descriptions\n", " \"\"\"\n", " vars1 = thinkstats2.ReadStataDct('2002FemPreg.dct').variables\n", " vars2 = thinkstats2.ReadStataDct('2002FemResp.dct').variables\n", "\n", " all_vars = vars1.append(vars2)\n", " all_vars.index = all_vars.name\n", " return all_vars\n", "\n", "def MiningReport(variables, n=30):\n", " \"\"\"Prints variables with the highest R^2.\n", "\n", " t: list of (R^2, variable name) pairs\n", " n: number of pairs to print\n", " \"\"\"\n", " all_vars = ReadVariables()\n", "\n", " variables.sort(reverse=True)\n", " for r2, name in variables[:n]:\n", " key = re.sub('_r$', '', name)\n", " try:\n", " desc = all_vars.loc[key].desc\n", " if isinstance(desc, pd.Series):\n", " desc = desc[0]\n", " print(name, r2, desc)\n", " except (KeyError, IndexError):\n", " print(name, r2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Some of the variables that do well are not useful for prediction because they are not known ahead of time." ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "totalwgt_lb 1.0\n", "birthwgt_lb 0.9498127305978009 BD-3 BIRTHWEIGHT IN POUNDS - 1ST BABY FROM THIS PREGNANCY\n", "lbw1 0.30082407844707704 LOW BIRTHWEIGHT - BABY 1\n", "prglngth 0.13012519488625052 DURATION OF COMPLETED PREGNANCY IN WEEKS\n", "wksgest 0.12340041363361054 GESTATIONAL LENGTH OF COMPLETED PREGNANCY (IN WEEKS)\n", "agecon 0.10203149928155986 AGE AT TIME OF CONCEPTION\n", "mosgest 0.027144274639579802 GESTATIONAL LENGTH OF COMPLETED PREGNANCY (IN MONTHS)\n", "babysex 0.01855092529394209 BD-2 SEX OF 1ST LIVEBORN BABY FROM THIS PREGNANCY\n", "race_r 0.016199503586253217\n", "race 0.016199503586253217\n", "nbrnaliv 0.016017752709788113 BC-2 NUMBER OF BABIES BORN ALIVE FROM THIS PREGNANCY\n", "paydu 0.014003795578114597 IB-10 CURRENT LIVING QUARTERS OWNED/RENTED, ETC\n", "rmarout03 0.013430066465713209 INFORMAL MARITAL STATUS WHEN PREGNANCY ENDED - 3RD\n", "birthwgt_oz 0.013102457615706053 BD-3 BIRTHWEIGHT IN OUNCES - 1ST BABY FROM THIS PREGNANCY\n", "anynurse 0.012529022541810764 BH-1 WHETHER R BREASTFED THIS CHILD AT ALL - 1ST FROM THIS PREG\n", "bfeedwks 0.012193688404495862 DURATION OF BREASTFEEDING IN WEEKS\n", "totincr 0.011870069031173491 TOTAL INCOME OF R'S FAMILY\n", "marout03 0.011807801994374811 FORMAL MARITAL STATUS WHEN PREGNANCY ENDED - 3RD\n", "marcon03 0.011752599354395654 FORMAL MARITAL STATUS WHEN PREGNANCY BEGAN - 3RD\n", "cebow 0.011437770919637047 NUMBER OF CHILDREN BORN OUT OF WEDLOCK\n", "rmarout01 0.011407737138640073 INFORMAL MARITAL STATUS WHEN PREGNANCY ENDED - 1ST\n", "rmarout6 0.011354138472805642 INFORMAL MARITAL STATUS AT PREGNANCY OUTCOME - 6 CATEGORIES\n", "marout01 0.011269357246806555 FORMAL MARITAL STATUS WHEN PREGNANCY ENDED - 1ST\n", "hisprace_r 0.011238349302030715\n", "hisprace 0.011238349302030715\n", "mar1diss 0.010961563590751733 MONTHS BTW/1ST MARRIAGE & DISSOLUTION (OR INTERVIEW)\n", "fmarcon5 0.0106049646842995 FORMAL MARITAL STATUS AT CONCEPTION - 5 CATEGORIES\n", "rmarout02 0.0105469132065652 INFORMAL MARITAL STATUS WHEN PREGNANCY ENDED - 2ND\n", "marcon02 0.010481401795534362 FORMAL MARITAL STATUS WHEN PREGNANCY BEGAN - 2ND\n", "fmarout5 0.010461691367377068 FORMAL MARITAL STATUS AT PREGNANCY OUTCOME\n" ] } ], "source": [ "MiningReport(variables)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Combining the variables that seem to have the most explanatory power." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
OLS Regression Results
Dep. Variable: totalwgt_lb R-squared: 0.060
Model: OLS Adj. R-squared: 0.059
Method: Least Squares F-statistic: 79.98
Date: Mon, 25 Feb 2019 Prob (F-statistic): 4.86e-113
Time: 16:35:03 Log-Likelihood: -14295.
No. Observations: 8781 AIC: 2.861e+04
Df Residuals: 8773 BIC: 2.866e+04
Df Model: 7
Covariance Type: nonrobust
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err t P>|t| [0.025 0.975]
Intercept 6.6303 0.065 102.223 0.000 6.503 6.757
C(race)[T.2] 0.3570 0.032 11.215 0.000 0.295 0.419
C(race)[T.3] 0.2665 0.051 5.175 0.000 0.166 0.367
babysex == 1[T.True] 0.2952 0.026 11.216 0.000 0.244 0.347
nbrnaliv > 1[T.True] -1.3783 0.108 -12.771 0.000 -1.590 -1.167
paydu == 1[T.True] 0.1196 0.031 3.861 0.000 0.059 0.180
agepreg 0.0074 0.003 2.921 0.004 0.002 0.012
totincr 0.0122 0.004 3.110 0.002 0.005 0.020
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Omnibus: 398.813 Durbin-Watson: 1.604
Prob(Omnibus): 0.000 Jarque-Bera (JB): 1388.362
Skew: -0.037 Prob(JB): 3.32e-302
Kurtosis: 4.947 Cond. No. 221.


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified." ], "text/plain": [ "\n", "\"\"\"\n", " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: totalwgt_lb R-squared: 0.060\n", "Model: OLS Adj. R-squared: 0.059\n", "Method: Least Squares F-statistic: 79.98\n", "Date: Mon, 25 Feb 2019 Prob (F-statistic): 4.86e-113\n", "Time: 16:35:03 Log-Likelihood: -14295.\n", "No. Observations: 8781 AIC: 2.861e+04\n", "Df Residuals: 8773 BIC: 2.866e+04\n", "Df Model: 7 \n", "Covariance Type: nonrobust \n", "========================================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "----------------------------------------------------------------------------------------\n", "Intercept 6.6303 0.065 102.223 0.000 6.503 6.757\n", "C(race)[T.2] 0.3570 0.032 11.215 0.000 0.295 0.419\n", "C(race)[T.3] 0.2665 0.051 5.175 0.000 0.166 0.367\n", "babysex == 1[T.True] 0.2952 0.026 11.216 0.000 0.244 0.347\n", "nbrnaliv > 1[T.True] -1.3783 0.108 -12.771 0.000 -1.590 -1.167\n", "paydu == 1[T.True] 0.1196 0.031 3.861 0.000 0.059 0.180\n", "agepreg 0.0074 0.003 2.921 0.004 0.002 0.012\n", "totincr 0.0122 0.004 3.110 0.002 0.005 0.020\n", "==============================================================================\n", "Omnibus: 398.813 Durbin-Watson: 1.604\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 1388.362\n", "Skew: -0.037 Prob(JB): 3.32e-302\n", "Kurtosis: 4.947 Cond. No. 221.\n", "==============================================================================\n", "\n", "Warnings:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", "\"\"\"" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formula = ('totalwgt_lb ~ agepreg + C(race) + babysex==1 + '\n", " 'nbrnaliv>1 + paydu==1 + totincr')\n", "results = smf.ols(formula, data=join).fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Logistic regression\n", "\n", "Example: suppose we are trying to predict `y` using explanatory variables `x1` and `x2`." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "y = np.array([0, 1, 0, 1])\n", "x1 = np.array([0, 0, 0, 1])\n", "x2 = np.array([0, 1, 1, 1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "According to the logit model the log odds for the $i$th element of $y$ is\n", "\n", "$\\log o = \\beta_0 + \\beta_1 x_1 + \\beta_2 x_2 $\n", "\n", "So let's start with an arbitrary guess about the elements of $\\beta$:\n", "\n" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [], "source": [ "beta = [-1.5, 2.8, 1.1]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Plugging in the model, we get log odds." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([-1.5, -0.4, -0.4, 2.4])" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "log_o = beta[0] + beta[1] * x1 + beta[2] * x2\n", "log_o" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Which we can convert to odds." ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([ 0.22313016, 0.67032005, 0.67032005, 11.02317638])" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "o = np.exp(log_o)\n", "o" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And then convert to probabilities." ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.18242552, 0.40131234, 0.40131234, 0.9168273 ])" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "p = o / (o+1)\n", "p" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The likelihoods of the actual outcomes are $p$ where $y$ is 1 and $1-p$ where $y$ is 0. " ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([0.81757448, 0.40131234, 0.59868766, 0.9168273 ])" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "likes = np.where(y, p, 1-p)\n", "likes" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The likelihood of $y$ given $\\beta$ is the product of `likes`:" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.1800933529673034" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "like = np.prod(likes)\n", "like" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Logistic regression works by searching for the values in $\\beta$ that maximize `like`.\n", "\n", "Here's an example using variables in the NSFG respondent file to predict whether a baby will be a boy or a girl." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "import first\n", "live, firsts, others = first.MakeFrames()\n", "live = live[live.prglngth>30]\n", "live['boy'] = (live.babysex==1).astype(int)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The mother's age seems to have a small effect." ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimization terminated successfully.\n", " Current function value: 0.693015\n", " Iterations 3\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Logit Regression Results
Dep. Variable: boy No. Observations: 8884
Model: Logit Df Residuals: 8882
Method: MLE Df Model: 1
Date: Mon, 25 Feb 2019 Pseudo R-squ.: 6.144e-06
Time: 16:35:05 Log-Likelihood: -6156.7
converged: True LL-Null: -6156.8
LLR p-value: 0.7833
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err z P>|z| [0.025 0.975]
Intercept 0.0058 0.098 0.059 0.953 -0.185 0.197
agepreg 0.0010 0.004 0.275 0.783 -0.006 0.009
" ], "text/plain": [ "\n", "\"\"\"\n", " Logit Regression Results \n", "==============================================================================\n", "Dep. Variable: boy No. Observations: 8884\n", "Model: Logit Df Residuals: 8882\n", "Method: MLE Df Model: 1\n", "Date: Mon, 25 Feb 2019 Pseudo R-squ.: 6.144e-06\n", "Time: 16:35:05 Log-Likelihood: -6156.7\n", "converged: True LL-Null: -6156.8\n", " LLR p-value: 0.7833\n", "==============================================================================\n", " coef std err z P>|z| [0.025 0.975]\n", "------------------------------------------------------------------------------\n", "Intercept 0.0058 0.098 0.059 0.953 -0.185 0.197\n", "agepreg 0.0010 0.004 0.275 0.783 -0.006 0.009\n", "==============================================================================\n", "\"\"\"" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "model = smf.logit('boy ~ agepreg', data=live)\n", "results = model.fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here are the variables that seemed most promising." ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Optimization terminated successfully.\n", " Current function value: 0.692944\n", " Iterations 3\n" ] }, { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Logit Regression Results
Dep. Variable: boy No. Observations: 8782
Model: Logit Df Residuals: 8776
Method: MLE Df Model: 5
Date: Mon, 25 Feb 2019 Pseudo R-squ.: 0.0001440
Time: 16:35:05 Log-Likelihood: -6085.4
converged: True LL-Null: -6086.3
LLR p-value: 0.8822
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err z P>|z| [0.025 0.975]
Intercept -0.0301 0.104 -0.290 0.772 -0.234 0.173
C(race)[T.2] -0.0224 0.051 -0.439 0.660 -0.122 0.077
C(race)[T.3] -0.0005 0.083 -0.005 0.996 -0.163 0.162
agepreg -0.0027 0.006 -0.484 0.629 -0.014 0.008
hpagelb 0.0047 0.004 1.112 0.266 -0.004 0.013
birthord 0.0050 0.022 0.227 0.821 -0.038 0.048
" ], "text/plain": [ "\n", "\"\"\"\n", " Logit Regression Results \n", "==============================================================================\n", "Dep. Variable: boy No. Observations: 8782\n", "Model: Logit Df Residuals: 8776\n", "Method: MLE Df Model: 5\n", "Date: Mon, 25 Feb 2019 Pseudo R-squ.: 0.0001440\n", "Time: 16:35:05 Log-Likelihood: -6085.4\n", "converged: True LL-Null: -6086.3\n", " LLR p-value: 0.8822\n", "================================================================================\n", " coef std err z P>|z| [0.025 0.975]\n", "--------------------------------------------------------------------------------\n", "Intercept -0.0301 0.104 -0.290 0.772 -0.234 0.173\n", "C(race)[T.2] -0.0224 0.051 -0.439 0.660 -0.122 0.077\n", "C(race)[T.3] -0.0005 0.083 -0.005 0.996 -0.163 0.162\n", "agepreg -0.0027 0.006 -0.484 0.629 -0.014 0.008\n", "hpagelb 0.0047 0.004 1.112 0.266 -0.004 0.013\n", "birthord 0.0050 0.022 0.227 0.821 -0.038 0.048\n", "================================================================================\n", "\"\"\"" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "formula = 'boy ~ agepreg + hpagelb + birthord + C(race)'\n", "model = smf.logit(formula, data=live)\n", "results = model.fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make a prediction, we have to extract the exogenous and endogenous variables." ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [], "source": [ "endog = pd.DataFrame(model.endog, columns=[model.endog_names])\n", "exog = pd.DataFrame(model.exog, columns=model.exog_names)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The baseline prediction strategy is to guess \"boy\". In that case, we're right almost 51% of the time." ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.507173764518333" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "actual = endog['boy']\n", "baseline = actual.mean()\n", "baseline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If we use the previous model, we can compute the number of predictions we get right." ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(3944.0, 548.0)" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "predict = (results.predict() >= 0.5)\n", "true_pos = predict * actual\n", "true_neg = (1 - predict) * (1 - actual)\n", "sum(true_pos), sum(true_neg)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And the accuracy, which is slightly higher than the baseline." ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0.5115007970849464" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "acc = (sum(true_pos) + sum(true_neg)) / len(actual)\n", "acc" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To make a prediction for an individual, we have to get their information into a `DataFrame`." ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "0 0.513091\n", "dtype: float64" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "columns = ['agepreg', 'hpagelb', 'birthord', 'race']\n", "new = pd.DataFrame([[35, 39, 3, 2]], columns=columns)\n", "y = results.predict(new)\n", "y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This person has a 51% chance of having a boy (according to the model)." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## Exercises" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "**Exercise:** Suppose one of your co-workers is expecting a baby and you are participating in an office pool to predict the date of birth. Assuming that bets are placed during the 30th week of pregnancy, what variables could you use to make the best prediction? You should limit yourself to variables that are known before the birth, and likely to be available to the people in the pool." ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [], "source": [ "import first\n", "live, firsts, others = first.MakeFrames()\n", "live = live[live.prglngth>30]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The following are the only variables I found that have a statistically significant effect on pregnancy length." ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
OLS Regression Results
Dep. Variable: prglngth R-squared: 0.011
Model: OLS Adj. R-squared: 0.011
Method: Least Squares F-statistic: 34.28
Date: Mon, 25 Feb 2019 Prob (F-statistic): 5.09e-22
Time: 16:35:08 Log-Likelihood: -18247.
No. Observations: 8884 AIC: 3.650e+04
Df Residuals: 8880 BIC: 3.653e+04
Df Model: 3
Covariance Type: nonrobust
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
coef std err t P>|t| [0.025 0.975]
Intercept 38.7617 0.039 1006.410 0.000 38.686 38.837
birthord == 1[T.True] 0.1015 0.040 2.528 0.011 0.023 0.180
race == 2[T.True] 0.1390 0.042 3.311 0.001 0.057 0.221
nbrnaliv > 1[T.True] -1.4944 0.164 -9.086 0.000 -1.817 -1.172
\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "
Omnibus: 1587.470 Durbin-Watson: 1.619
Prob(Omnibus): 0.000 Jarque-Bera (JB): 6160.751
Skew: -0.852 Prob(JB): 0.00
Kurtosis: 6.707 Cond. No. 10.9


Warnings:
[1] Standard Errors assume that the covariance matrix of the errors is correctly specified." ], "text/plain": [ "\n", "\"\"\"\n", " OLS Regression Results \n", "==============================================================================\n", "Dep. Variable: prglngth R-squared: 0.011\n", "Model: OLS Adj. R-squared: 0.011\n", "Method: Least Squares F-statistic: 34.28\n", "Date: Mon, 25 Feb 2019 Prob (F-statistic): 5.09e-22\n", "Time: 16:35:08 Log-Likelihood: -18247.\n", "No. Observations: 8884 AIC: 3.650e+04\n", "Df Residuals: 8880 BIC: 3.653e+04\n", "Df Model: 3 \n", "Covariance Type: nonrobust \n", "=========================================================================================\n", " coef std err t P>|t| [0.025 0.975]\n", "-----------------------------------------------------------------------------------------\n", "Intercept 38.7617 0.039 1006.410 0.000 38.686 38.837\n", "birthord == 1[T.True] 0.1015 0.040 2.528 0.011 0.023 0.180\n", "race == 2[T.True] 0.1390 0.042 3.311 0.001 0.057 0.221\n", "nbrnaliv > 1[T.True] -1.4944 0.164 -9.086 0.000 -1.817 -1.172\n", "==============================================================================\n", "Omnibus: 1587.470 Durbin-Watson: 1.619\n", "Prob(Omnibus): 0.000 Jarque-Bera (JB): 6160.751\n", "Skew: -0.852 Prob(JB): 0.00\n", "Kurtosis: 6.707 Cond. No. 10.9\n", "==============================================================================\n", "\n", "Warnings:\n", "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n", "\"\"\"" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import statsmodels.formula.api as smf\n", "model = smf.ols('prglngth ~ birthord==1 + race==2 + nbrnaliv>1', data=live)\n", "results = model.fit()\n", "results.summary()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** The Trivers-Willard hypothesis suggests that for many mammals the sex ratio depends on “maternal condition”; that is, factors like the mother’s age, size, health, and social status. See https://en.wikipedia.org/wiki/Trivers-Willard_hypothesis\n", "\n", "Some studies have shown this effect among humans, but results are mixed. In this chapter we tested some variables related to these factors, but didn’t find any with a statistically significant effect on sex ratio.\n", "\n", "As an exercise, use a data mining approach to test the other variables in the pregnancy and respondent files. Can you find any factors with a substantial effect?" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "import regression\n", "join = regression.JoinFemResp(live)" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** If the quantity you want to predict is a count, you can use Poisson regression, which is implemented in StatsModels with a function called `poisson`. It works the same way as `ols` and `logit`. As an exercise, let’s use it to predict how many children a woman has born; in the NSFG dataset, this variable is called `numbabes`.\n", "\n", "Suppose you meet a woman who is 35 years old, black, and a college graduate whose annual household income exceeds $75,000. How many children would you predict she has born?" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we can predict the number of children for a woman who is 35 years old, black, and a college\n", "graduate whose annual household income exceeds $75,000" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Exercise:** If the quantity you want to predict is categorical, you can use multinomial logistic regression, which is implemented in StatsModels with a function called `mnlogit`. As an exercise, let’s use it to guess whether a woman is married, cohabitating, widowed, divorced, separated, or never married; in the NSFG dataset, marital status is encoded in a variable called `rmarital`.\n", "\n", "Suppose you meet a woman who is 25 years old, white, and a high school graduate whose annual household income is about $45,000. What is the probability that she is married, cohabitating, etc?" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Make a prediction for a woman who is 25 years old, white, and a high\n", "school graduate whose annual household income is about $45,000." ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "# Solution goes here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] }, { "cell_type": "code", "execution_count": null, "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.7.2" } }, "nbformat": 4, "nbformat_minor": 1 }