{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Weighted Generalized Linear Models"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:24.048783Z",
"iopub.status.busy": "2021-02-02T06:51:24.048136Z",
"iopub.status.idle": "2021-02-02T06:51:25.206399Z",
"shell.execute_reply": "2021-02-02T06:51:25.204891Z"
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import statsmodels.formula.api as smf\n",
"import statsmodels.api as sm"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Weighted GLM: Poisson response data\n",
"\n",
"### Load data\n",
"\n",
"In this example, we'll use the affair dataset using a handful of exogenous variables to predict the extra-marital affair rate. \n",
"\n",
"Weights will be generated to show that `freq_weights` are equivalent to repeating records of data. On the other hand, `var_weights` is equivalent to aggregating data. "
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.211487Z",
"iopub.status.busy": "2021-02-02T06:51:25.210152Z",
"iopub.status.idle": "2021-02-02T06:51:25.219062Z",
"shell.execute_reply": "2021-02-02T06:51:25.220362Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"::\n",
"\n",
" Number of observations: 6366\n",
" Number of variables: 9\n",
" Variable name definitions:\n",
"\n",
" rate_marriage : How rate marriage, 1 = very poor, 2 = poor, 3 = fair,\n",
" 4 = good, 5 = very good\n",
" age : Age\n",
" yrs_married : No. years married. Interval approximations. See\n",
" original paper for detailed explanation.\n",
" children : No. children\n",
" religious : How relgious, 1 = not, 2 = mildly, 3 = fairly,\n",
" 4 = strongly\n",
" educ : Level of education, 9 = grade school, 12 = high\n",
" school, 14 = some college, 16 = college graduate,\n",
" 17 = some graduate school, 20 = advanced degree\n",
" occupation : 1 = student, 2 = farming, agriculture; semi-skilled,\n",
" or unskilled worker; 3 = white-colloar; 4 = teacher\n",
" counselor social worker, nurse; artist, writers;\n",
" technician, skilled worker, 5 = managerial,\n",
" administrative, business, 6 = professional with\n",
" advanced degree\n",
" occupation_husb : Husband's occupation. Same as occupation.\n",
" affairs : measure of time spent in extramarital affairs\n",
"\n",
" See the original paper for more details.\n",
"\n"
]
}
],
"source": [
"print(sm.datasets.fair.NOTE)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load the data into a pandas dataframe."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.224627Z",
"iopub.status.busy": "2021-02-02T06:51:25.223823Z",
"iopub.status.idle": "2021-02-02T06:51:25.242661Z",
"shell.execute_reply": "2021-02-02T06:51:25.243699Z"
}
},
"outputs": [],
"source": [
"data = sm.datasets.fair.load_pandas().data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The dependent (endogenous) variable is ``affairs``"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.248316Z",
"iopub.status.busy": "2021-02-02T06:51:25.246929Z",
"iopub.status.idle": "2021-02-02T06:51:25.316272Z",
"shell.execute_reply": "2021-02-02T06:51:25.317274Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" rate_marriage | \n",
" age | \n",
" yrs_married | \n",
" children | \n",
" religious | \n",
" educ | \n",
" occupation | \n",
" occupation_husb | \n",
" affairs | \n",
"
\n",
" \n",
" \n",
" \n",
" count | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
" 6366.000000 | \n",
"
\n",
" \n",
" mean | \n",
" 4.109645 | \n",
" 29.082862 | \n",
" 9.009425 | \n",
" 1.396874 | \n",
" 2.426170 | \n",
" 14.209865 | \n",
" 3.424128 | \n",
" 3.850141 | \n",
" 0.705374 | \n",
"
\n",
" \n",
" std | \n",
" 0.961430 | \n",
" 6.847882 | \n",
" 7.280120 | \n",
" 1.433471 | \n",
" 0.878369 | \n",
" 2.178003 | \n",
" 0.942399 | \n",
" 1.346435 | \n",
" 2.203374 | \n",
"
\n",
" \n",
" min | \n",
" 1.000000 | \n",
" 17.500000 | \n",
" 0.500000 | \n",
" 0.000000 | \n",
" 1.000000 | \n",
" 9.000000 | \n",
" 1.000000 | \n",
" 1.000000 | \n",
" 0.000000 | \n",
"
\n",
" \n",
" 25% | \n",
" 4.000000 | \n",
" 22.000000 | \n",
" 2.500000 | \n",
" 0.000000 | \n",
" 2.000000 | \n",
" 12.000000 | \n",
" 3.000000 | \n",
" 3.000000 | \n",
" 0.000000 | \n",
"
\n",
" \n",
" 50% | \n",
" 4.000000 | \n",
" 27.000000 | \n",
" 6.000000 | \n",
" 1.000000 | \n",
" 2.000000 | \n",
" 14.000000 | \n",
" 3.000000 | \n",
" 4.000000 | \n",
" 0.000000 | \n",
"
\n",
" \n",
" 75% | \n",
" 5.000000 | \n",
" 32.000000 | \n",
" 16.500000 | \n",
" 2.000000 | \n",
" 3.000000 | \n",
" 16.000000 | \n",
" 4.000000 | \n",
" 5.000000 | \n",
" 0.484848 | \n",
"
\n",
" \n",
" max | \n",
" 5.000000 | \n",
" 42.000000 | \n",
" 23.000000 | \n",
" 5.500000 | \n",
" 4.000000 | \n",
" 20.000000 | \n",
" 6.000000 | \n",
" 6.000000 | \n",
" 57.599991 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" rate_marriage age yrs_married children religious \\\n",
"count 6366.000000 6366.000000 6366.000000 6366.000000 6366.000000 \n",
"mean 4.109645 29.082862 9.009425 1.396874 2.426170 \n",
"std 0.961430 6.847882 7.280120 1.433471 0.878369 \n",
"min 1.000000 17.500000 0.500000 0.000000 1.000000 \n",
"25% 4.000000 22.000000 2.500000 0.000000 2.000000 \n",
"50% 4.000000 27.000000 6.000000 1.000000 2.000000 \n",
"75% 5.000000 32.000000 16.500000 2.000000 3.000000 \n",
"max 5.000000 42.000000 23.000000 5.500000 4.000000 \n",
"\n",
" educ occupation occupation_husb affairs \n",
"count 6366.000000 6366.000000 6366.000000 6366.000000 \n",
"mean 14.209865 3.424128 3.850141 0.705374 \n",
"std 2.178003 0.942399 1.346435 2.203374 \n",
"min 9.000000 1.000000 1.000000 0.000000 \n",
"25% 12.000000 3.000000 3.000000 0.000000 \n",
"50% 14.000000 3.000000 4.000000 0.000000 \n",
"75% 16.000000 4.000000 5.000000 0.484848 \n",
"max 20.000000 6.000000 6.000000 57.599991 "
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data.describe()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.322099Z",
"iopub.status.busy": "2021-02-02T06:51:25.320621Z",
"iopub.status.idle": "2021-02-02T06:51:25.343175Z",
"shell.execute_reply": "2021-02-02T06:51:25.344170Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" rate_marriage | \n",
" age | \n",
" yrs_married | \n",
" children | \n",
" religious | \n",
" educ | \n",
" occupation | \n",
" occupation_husb | \n",
" affairs | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 3.0 | \n",
" 32.0 | \n",
" 9.0 | \n",
" 3.0 | \n",
" 3.0 | \n",
" 17.0 | \n",
" 2.0 | \n",
" 5.0 | \n",
" 0.111111 | \n",
"
\n",
" \n",
" 1 | \n",
" 3.0 | \n",
" 27.0 | \n",
" 13.0 | \n",
" 3.0 | \n",
" 1.0 | \n",
" 14.0 | \n",
" 3.0 | \n",
" 4.0 | \n",
" 3.230769 | \n",
"
\n",
" \n",
" 2 | \n",
" 4.0 | \n",
" 22.0 | \n",
" 2.5 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 16.0 | \n",
" 3.0 | \n",
" 5.0 | \n",
" 1.400000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" rate_marriage age yrs_married children religious educ occupation \\\n",
"0 3.0 32.0 9.0 3.0 3.0 17.0 2.0 \n",
"1 3.0 27.0 13.0 3.0 1.0 14.0 3.0 \n",
"2 4.0 22.0 2.5 0.0 1.0 16.0 3.0 \n",
"\n",
" occupation_husb affairs \n",
"0 5.0 0.111111 \n",
"1 4.0 3.230769 \n",
"2 5.0 1.400000 "
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data[:3]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the following we will work mostly with Poisson. While using decimal affairs works, we convert them to integers to have a count distribution."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.348523Z",
"iopub.status.busy": "2021-02-02T06:51:25.347175Z",
"iopub.status.idle": "2021-02-02T06:51:25.370114Z",
"shell.execute_reply": "2021-02-02T06:51:25.371079Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" rate_marriage | \n",
" age | \n",
" yrs_married | \n",
" children | \n",
" religious | \n",
" educ | \n",
" occupation | \n",
" occupation_husb | \n",
" affairs | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 3.0 | \n",
" 32.0 | \n",
" 9.0 | \n",
" 3.0 | \n",
" 3.0 | \n",
" 17.0 | \n",
" 2.0 | \n",
" 5.0 | \n",
" 1.0 | \n",
"
\n",
" \n",
" 1 | \n",
" 3.0 | \n",
" 27.0 | \n",
" 13.0 | \n",
" 3.0 | \n",
" 1.0 | \n",
" 14.0 | \n",
" 3.0 | \n",
" 4.0 | \n",
" 4.0 | \n",
"
\n",
" \n",
" 2 | \n",
" 4.0 | \n",
" 22.0 | \n",
" 2.5 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 16.0 | \n",
" 3.0 | \n",
" 5.0 | \n",
" 2.0 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" rate_marriage age yrs_married children religious educ occupation \\\n",
"0 3.0 32.0 9.0 3.0 3.0 17.0 2.0 \n",
"1 3.0 27.0 13.0 3.0 1.0 14.0 3.0 \n",
"2 4.0 22.0 2.5 0.0 1.0 16.0 3.0 \n",
"\n",
" occupation_husb affairs \n",
"0 5.0 1.0 \n",
"1 4.0 4.0 \n",
"2 5.0 2.0 "
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data[\"affairs\"] = np.ceil(data[\"affairs\"])\n",
"data[:3]"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.375678Z",
"iopub.status.busy": "2021-02-02T06:51:25.374256Z",
"iopub.status.idle": "2021-02-02T06:51:25.384045Z",
"shell.execute_reply": "2021-02-02T06:51:25.385108Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"0.6775054979579014"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(data[\"affairs\"] == 0).mean()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.389329Z",
"iopub.status.busy": "2021-02-02T06:51:25.387998Z",
"iopub.status.idle": "2021-02-02T06:51:25.396704Z",
"shell.execute_reply": "2021-02-02T06:51:25.397679Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([4313, 934, 488, 180, 130, 172, 7, 21, 67, 2, 0,\n",
" 0, 17, 0, 0, 0, 3, 12, 8, 0, 0, 0,\n",
" 0, 0, 2, 2, 2, 3, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0,\n",
" 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,\n",
" 0, 0, 0, 1])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.bincount(data[\"affairs\"].astype(int))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Condensing and Aggregating observations\n",
"\n",
"We have 6366 observations in our original dataset. When we consider only some selected variables, then we have fewer unique observations. In the following we combine observations in two ways, first we combine observations that have values for all variables identical, and secondly we combine observations that have the same explanatory variables."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dataset with unique observations\n",
"\n",
"We use pandas's groupby to combine identical observations and create a new variable `freq` that count how many observation have the values in the corresponding row."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.402116Z",
"iopub.status.busy": "2021-02-02T06:51:25.400727Z",
"iopub.status.idle": "2021-02-02T06:51:25.433823Z",
"shell.execute_reply": "2021-02-02T06:51:25.434776Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(476, 5)"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" affairs | \n",
" rate_marriage | \n",
" age | \n",
" yrs_married | \n",
" freq | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 17.5 | \n",
" 0.5 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 22.0 | \n",
" 2.5 | \n",
" 3 | \n",
"
\n",
" \n",
" 2 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 27.0 | \n",
" 2.5 | \n",
" 1 | \n",
"
\n",
" \n",
" 3 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 27.0 | \n",
" 6.0 | \n",
" 5 | \n",
"
\n",
" \n",
" 4 | \n",
" 0.0 | \n",
" 1.0 | \n",
" 27.0 | \n",
" 9.0 | \n",
" 1 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" affairs rate_marriage age yrs_married freq\n",
"0 0.0 1.0 17.5 0.5 1\n",
"1 0.0 1.0 22.0 2.5 3\n",
"2 0.0 1.0 27.0 2.5 1\n",
"3 0.0 1.0 27.0 6.0 5\n",
"4 0.0 1.0 27.0 9.0 1"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data2 = data.copy()\n",
"data2['const'] = 1\n",
"dc = data2['affairs rate_marriage age yrs_married const'.split()].groupby('affairs rate_marriage age yrs_married'.split()).count()\n",
"dc.reset_index(inplace=True)\n",
"dc.rename(columns={'const': 'freq'}, inplace=True)\n",
"print(dc.shape)\n",
"dc.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Dataset with unique explanatory variables (exog)\n",
"\n",
"For the next dataset we combine observations that have the same values of the explanatory variables. However, because the response variable can differ among combined observations, we compute the mean and the sum of the response variable for all combined observations.\n",
"\n",
"We use again pandas ``groupby`` to combine observations and to create the new variables. We also flatten the ``MultiIndex`` into a simple index."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.439276Z",
"iopub.status.busy": "2021-02-02T06:51:25.437707Z",
"iopub.status.idle": "2021-02-02T06:51:25.475537Z",
"shell.execute_reply": "2021-02-02T06:51:25.476520Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(130, 6)\n"
]
},
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" rate_marriage | \n",
" age | \n",
" yrs_married | \n",
" affairs_mean | \n",
" affairs_sum | \n",
" affairs_count | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1.0 | \n",
" 17.5 | \n",
" 0.5 | \n",
" 0.000000 | \n",
" 0.0 | \n",
" 1 | \n",
"
\n",
" \n",
" 1 | \n",
" 1.0 | \n",
" 22.0 | \n",
" 2.5 | \n",
" 3.900000 | \n",
" 39.0 | \n",
" 10 | \n",
"
\n",
" \n",
" 2 | \n",
" 1.0 | \n",
" 27.0 | \n",
" 2.5 | \n",
" 3.400000 | \n",
" 17.0 | \n",
" 5 | \n",
"
\n",
" \n",
" 3 | \n",
" 1.0 | \n",
" 27.0 | \n",
" 6.0 | \n",
" 0.900000 | \n",
" 9.0 | \n",
" 10 | \n",
"
\n",
" \n",
" 4 | \n",
" 1.0 | \n",
" 27.0 | \n",
" 9.0 | \n",
" 1.333333 | \n",
" 4.0 | \n",
" 3 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" rate_marriage age yrs_married affairs_mean affairs_sum affairs_count\n",
"0 1.0 17.5 0.5 0.000000 0.0 1\n",
"1 1.0 22.0 2.5 3.900000 39.0 10\n",
"2 1.0 27.0 2.5 3.400000 17.0 5\n",
"3 1.0 27.0 6.0 0.900000 9.0 10\n",
"4 1.0 27.0 9.0 1.333333 4.0 3"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"gr = data['affairs rate_marriage age yrs_married'.split()].groupby('rate_marriage age yrs_married'.split())\n",
"df_a = gr.agg(['mean', 'sum','count'])\n",
"def merge_tuple(tpl):\n",
" if isinstance(tpl, tuple) and len(tpl) > 1:\n",
" return \"_\".join(map(str, tpl))\n",
" else:\n",
" return tpl\n",
" \n",
"df_a.columns = df_a.columns.map(merge_tuple)\n",
"df_a.reset_index(inplace=True)\n",
"print(df_a.shape)\n",
"df_a.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After combining observations with have a dataframe `dc` with 467 unique observations, and a dataframe `df_a` with 130 observations with unique values of the explanatory variables."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.480903Z",
"iopub.status.busy": "2021-02-02T06:51:25.479539Z",
"iopub.status.idle": "2021-02-02T06:51:25.489608Z",
"shell.execute_reply": "2021-02-02T06:51:25.490590Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"number of rows: \n",
"original, with unique observations, with unique exog\n"
]
},
{
"data": {
"text/plain": [
"(6366, 476, 130)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"print('number of rows: \\noriginal, with unique observations, with unique exog')\n",
"data.shape[0], dc.shape[0], df_a.shape[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Analysis\n",
"\n",
"In the following, we compare the GLM-Poisson results of the original data with models of the combined observations where the multiplicity or aggregation is given by weights or exposure.\n",
"\n",
"\n",
"### original data"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.495114Z",
"iopub.status.busy": "2021-02-02T06:51:25.493609Z",
"iopub.status.idle": "2021-02-02T06:51:25.551115Z",
"shell.execute_reply": "2021-02-02T06:51:25.552111Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs No. Observations: 6366\n",
"Model: GLM Df Residuals: 6362\n",
"Model Family: Poisson Df Model: 3\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -10351.\n",
"Date: Tue, 02 Feb 2021 Deviance: 15375.\n",
"Time: 06:51:25 Pearson chi2: 3.23e+04\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.7155 0.107 25.294 0.000 2.505 2.926\n",
"rate_marriage -0.4952 0.012 -41.702 0.000 -0.518 -0.472\n",
"age -0.0299 0.004 -6.691 0.000 -0.039 -0.021\n",
"yrs_married -0.0108 0.004 -2.507 0.012 -0.019 -0.002\n",
"=================================================================================\n"
]
}
],
"source": [
"glm = smf.glm('affairs ~ rate_marriage + age + yrs_married',\n",
" data=data, family=sm.families.Poisson())\n",
"res_o = glm.fit()\n",
"print(res_o.summary())"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.556843Z",
"iopub.status.busy": "2021-02-02T06:51:25.555354Z",
"iopub.status.idle": "2021-02-02T06:51:25.563754Z",
"shell.execute_reply": "2021-02-02T06:51:25.564743Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"5.078702313363305"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_o.pearson_chi2 / res_o.df_resid"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### condensed data (unique observations with frequencies)\n",
"\n",
"Combining identical observations and using frequency weights to take into account the multiplicity of observations produces exactly the same results. Some results attribute will differ when we want to have information about the observation and not about the aggregate of all identical observations. For example, residuals do not take ``freq_weights`` into account."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.569317Z",
"iopub.status.busy": "2021-02-02T06:51:25.567806Z",
"iopub.status.idle": "2021-02-02T06:51:25.599353Z",
"shell.execute_reply": "2021-02-02T06:51:25.600425Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs No. Observations: 476\n",
"Model: GLM Df Residuals: 6362\n",
"Model Family: Poisson Df Model: 3\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -10351.\n",
"Date: Tue, 02 Feb 2021 Deviance: 15375.\n",
"Time: 06:51:25 Pearson chi2: 3.23e+04\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.7155 0.107 25.294 0.000 2.505 2.926\n",
"rate_marriage -0.4952 0.012 -41.702 0.000 -0.518 -0.472\n",
"age -0.0299 0.004 -6.691 0.000 -0.039 -0.021\n",
"yrs_married -0.0108 0.004 -2.507 0.012 -0.019 -0.002\n",
"=================================================================================\n"
]
}
],
"source": [
"glm = smf.glm('affairs ~ rate_marriage + age + yrs_married',\n",
" data=dc, family=sm.families.Poisson(), freq_weights=np.asarray(dc['freq']))\n",
"res_f = glm.fit()\n",
"print(res_f.summary())"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.604875Z",
"iopub.status.busy": "2021-02-02T06:51:25.603509Z",
"iopub.status.idle": "2021-02-02T06:51:25.611411Z",
"shell.execute_reply": "2021-02-02T06:51:25.612381Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"5.078702313363218"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_f.pearson_chi2 / res_f.df_resid"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### condensed using ``var_weights`` instead of ``freq_weights``\n",
"\n",
"Next, we compare ``var_weights`` to ``freq_weights``. It is a common practice to incorporate ``var_weights`` when the endogenous variable reflects averages and not identical observations.\n",
"I do not see a theoretical reason why it produces the same results (in general).\n",
"\n",
"This produces the same results but ``df_resid`` differs the ``freq_weights`` example because ``var_weights`` do not change the number of effective observations. \n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.617087Z",
"iopub.status.busy": "2021-02-02T06:51:25.615699Z",
"iopub.status.idle": "2021-02-02T06:51:25.646239Z",
"shell.execute_reply": "2021-02-02T06:51:25.647191Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs No. Observations: 476\n",
"Model: GLM Df Residuals: 472\n",
"Model Family: Poisson Df Model: 3\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -10351.\n",
"Date: Tue, 02 Feb 2021 Deviance: 15375.\n",
"Time: 06:51:25 Pearson chi2: 3.23e+04\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.7155 0.107 25.294 0.000 2.505 2.926\n",
"rate_marriage -0.4952 0.012 -41.702 0.000 -0.518 -0.472\n",
"age -0.0299 0.004 -6.691 0.000 -0.039 -0.021\n",
"yrs_married -0.0108 0.004 -2.507 0.012 -0.019 -0.002\n",
"=================================================================================\n"
]
}
],
"source": [
"glm = smf.glm('affairs ~ rate_marriage + age + yrs_married',\n",
" data=dc, family=sm.families.Poisson(), var_weights=np.asarray(dc['freq']))\n",
"res_fv = glm.fit()\n",
"print(res_fv.summary())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dispersion computed from the results is incorrect because of wrong ``df_resid``. \n",
"It is correct if we use the original ``df_resid``."
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.651531Z",
"iopub.status.busy": "2021-02-02T06:51:25.650205Z",
"iopub.status.idle": "2021-02-02T06:51:25.658107Z",
"shell.execute_reply": "2021-02-02T06:51:25.659132Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(68.45488160512032, 5.078702313363218)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_fv.pearson_chi2 / res_fv.df_resid, res_f.pearson_chi2 / res_f.df_resid"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### aggregated or averaged data (unique values of explanatory variables)\n",
"\n",
"For these cases we combine observations that have the same values of the explanatory variables. The corresponding response variable is either a sum or an average.\n",
"\n",
"#### using ``exposure``\n",
"\n",
"If our dependent variable is the sum of the responses of all combined observations, then under the Poisson assumption the distribution remains the same but we have varying `exposure` given by the number of individuals that are represented by one aggregated observation.\n",
"\n",
"The parameter estimates and covariance of parameters are the same with the original data, but log-likelihood, deviance and Pearson chi-squared differ"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.663329Z",
"iopub.status.busy": "2021-02-02T06:51:25.661972Z",
"iopub.status.idle": "2021-02-02T06:51:25.692260Z",
"shell.execute_reply": "2021-02-02T06:51:25.693220Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs_sum No. Observations: 130\n",
"Model: GLM Df Residuals: 126\n",
"Model Family: Poisson Df Model: 3\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -740.75\n",
"Date: Tue, 02 Feb 2021 Deviance: 967.46\n",
"Time: 06:51:25 Pearson chi2: 926.\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.7155 0.107 25.294 0.000 2.505 2.926\n",
"rate_marriage -0.4952 0.012 -41.702 0.000 -0.518 -0.472\n",
"age -0.0299 0.004 -6.691 0.000 -0.039 -0.021\n",
"yrs_married -0.0108 0.004 -2.507 0.012 -0.019 -0.002\n",
"=================================================================================\n"
]
}
],
"source": [
"glm = smf.glm('affairs_sum ~ rate_marriage + age + yrs_married',\n",
" data=df_a, family=sm.families.Poisson(), exposure=np.asarray(df_a['affairs_count']))\n",
"res_e = glm.fit()\n",
"print(res_e.summary())"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.697497Z",
"iopub.status.busy": "2021-02-02T06:51:25.696154Z",
"iopub.status.idle": "2021-02-02T06:51:25.703931Z",
"shell.execute_reply": "2021-02-02T06:51:25.704881Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"7.350789109179492"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_e.pearson_chi2 / res_e.df_resid"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### using var_weights\n",
"\n",
"We can also use the mean of all combined values of the dependent variable. In this case the variance will be related to the inverse of the total exposure reflected by one combined observation."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.709137Z",
"iopub.status.busy": "2021-02-02T06:51:25.707765Z",
"iopub.status.idle": "2021-02-02T06:51:25.736048Z",
"shell.execute_reply": "2021-02-02T06:51:25.737072Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs_mean No. Observations: 130\n",
"Model: GLM Df Residuals: 126\n",
"Model Family: Poisson Df Model: 3\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -5954.2\n",
"Date: Tue, 02 Feb 2021 Deviance: 967.46\n",
"Time: 06:51:25 Pearson chi2: 926.\n",
"No. Iterations: 5 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.7155 0.107 25.294 0.000 2.505 2.926\n",
"rate_marriage -0.4952 0.012 -41.702 0.000 -0.518 -0.472\n",
"age -0.0299 0.004 -6.691 0.000 -0.039 -0.021\n",
"yrs_married -0.0108 0.004 -2.507 0.012 -0.019 -0.002\n",
"=================================================================================\n"
]
}
],
"source": [
"glm = smf.glm('affairs_mean ~ rate_marriage + age + yrs_married',\n",
" data=df_a, family=sm.families.Poisson(), var_weights=np.asarray(df_a['affairs_count']))\n",
"res_a = glm.fit()\n",
"print(res_a.summary())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Comparison\n",
"\n",
"We saw in the summary prints above that ``params`` and ``cov_params`` with associated Wald inference agree across versions. We summarize this in the following comparing individual results attributes across versions.\n",
"\n",
"Parameter estimates `params`, standard errors of the parameters `bse` and `pvalues` of the parameters for the tests that the parameters are zeros all agree. However, the likelihood and goodness-of-fit statistics, `llf`, `deviance` and `pearson_chi2` only partially agree. Specifically, the aggregated version do not agree with the results using the original data.\n",
"\n",
"**Warning**: The behavior of `llf`, `deviance` and `pearson_chi2` might still change in future versions.\n",
"\n",
"Both the sum and average of the response variable for unique values of the explanatory variables have a proper likelihood interpretation. However, this interpretation is not reflected in these three statistics. Computationally this might be due to missing adjustments when aggregated data is used. However, theoretically we can think in these cases, especially for `var_weights` of the misspecified case when likelihood analysis is inappropriate and the results should be interpreted as quasi-likelihood estimates. There is an ambiguity in the definition of ``var_weights`` because they can be used for averages with correctly specified likelihood as well as for variance adjustments in the quasi-likelihood case. We are currently not trying to match the likelihood specification. However, in the next section we show that likelihood ratio type tests still produce the same result for all aggregation versions when we assume that the underlying model is correctly specified."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.744606Z",
"iopub.status.busy": "2021-02-02T06:51:25.739969Z",
"iopub.status.idle": "2021-02-02T06:51:25.749981Z",
"shell.execute_reply": "2021-02-02T06:51:25.749325Z"
}
},
"outputs": [],
"source": [
"results_all = [res_o, res_f, res_e, res_a]\n",
"names = 'res_o res_f res_e res_a'.split()"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.765827Z",
"iopub.status.busy": "2021-02-02T06:51:25.764892Z",
"iopub.status.idle": "2021-02-02T06:51:25.771810Z",
"shell.execute_reply": "2021-02-02T06:51:25.771145Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" res_o | \n",
" res_f | \n",
" res_e | \n",
" res_a | \n",
"
\n",
" \n",
" \n",
" \n",
" Intercept | \n",
" 2.715533 | \n",
" 2.715533 | \n",
" 2.715533 | \n",
" 2.715533 | \n",
"
\n",
" \n",
" rate_marriage | \n",
" -0.495180 | \n",
" -0.495180 | \n",
" -0.495180 | \n",
" -0.495180 | \n",
"
\n",
" \n",
" age | \n",
" -0.029914 | \n",
" -0.029914 | \n",
" -0.029914 | \n",
" -0.029914 | \n",
"
\n",
" \n",
" yrs_married | \n",
" -0.010763 | \n",
" -0.010763 | \n",
" -0.010763 | \n",
" -0.010763 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" res_o res_f res_e res_a\n",
"Intercept 2.715533 2.715533 2.715533 2.715533\n",
"rate_marriage -0.495180 -0.495180 -0.495180 -0.495180\n",
"age -0.029914 -0.029914 -0.029914 -0.029914\n",
"yrs_married -0.010763 -0.010763 -0.010763 -0.010763"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.concat([r.params for r in results_all], axis=1, keys=names)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.787294Z",
"iopub.status.busy": "2021-02-02T06:51:25.786618Z",
"iopub.status.idle": "2021-02-02T06:51:25.792766Z",
"shell.execute_reply": "2021-02-02T06:51:25.796063Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" res_o | \n",
" res_f | \n",
" res_e | \n",
" res_a | \n",
"
\n",
" \n",
" \n",
" \n",
" Intercept | \n",
" 0.107360 | \n",
" 0.107360 | \n",
" 0.107360 | \n",
" 0.107360 | \n",
"
\n",
" \n",
" rate_marriage | \n",
" 0.011874 | \n",
" 0.011874 | \n",
" 0.011874 | \n",
" 0.011874 | \n",
"
\n",
" \n",
" age | \n",
" 0.004471 | \n",
" 0.004471 | \n",
" 0.004471 | \n",
" 0.004471 | \n",
"
\n",
" \n",
" yrs_married | \n",
" 0.004294 | \n",
" 0.004294 | \n",
" 0.004294 | \n",
" 0.004294 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" res_o res_f res_e res_a\n",
"Intercept 0.107360 0.107360 0.107360 0.107360\n",
"rate_marriage 0.011874 0.011874 0.011874 0.011874\n",
"age 0.004471 0.004471 0.004471 0.004471\n",
"yrs_married 0.004294 0.004294 0.004294 0.004294"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.concat([r.bse for r in results_all], axis=1, keys=names)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.812298Z",
"iopub.status.busy": "2021-02-02T06:51:25.811531Z",
"iopub.status.idle": "2021-02-02T06:51:25.817766Z",
"shell.execute_reply": "2021-02-02T06:51:25.818828Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" res_o | \n",
" res_f | \n",
" res_e | \n",
" res_a | \n",
"
\n",
" \n",
" \n",
" \n",
" Intercept | \n",
" 3.756282e-141 | \n",
" 3.756280e-141 | \n",
" 3.756282e-141 | \n",
" 3.756282e-141 | \n",
"
\n",
" \n",
" rate_marriage | \n",
" 0.000000e+00 | \n",
" 0.000000e+00 | \n",
" 0.000000e+00 | \n",
" 0.000000e+00 | \n",
"
\n",
" \n",
" age | \n",
" 2.221918e-11 | \n",
" 2.221918e-11 | \n",
" 2.221918e-11 | \n",
" 2.221918e-11 | \n",
"
\n",
" \n",
" yrs_married | \n",
" 1.219200e-02 | \n",
" 1.219200e-02 | \n",
" 1.219200e-02 | \n",
" 1.219200e-02 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" res_o res_f res_e res_a\n",
"Intercept 3.756282e-141 3.756280e-141 3.756282e-141 3.756282e-141\n",
"rate_marriage 0.000000e+00 0.000000e+00 0.000000e+00 0.000000e+00\n",
"age 2.221918e-11 2.221918e-11 2.221918e-11 2.221918e-11\n",
"yrs_married 1.219200e-02 1.219200e-02 1.219200e-02 1.219200e-02"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.concat([r.pvalues for r in results_all], axis=1, keys=names)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.831857Z",
"iopub.status.busy": "2021-02-02T06:51:25.831032Z",
"iopub.status.idle": "2021-02-02T06:51:25.844616Z",
"shell.execute_reply": "2021-02-02T06:51:25.845660Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" res_o | \n",
" res_f | \n",
" res_e | \n",
" res_a | \n",
"
\n",
" \n",
" \n",
" \n",
" llf | \n",
" -10350.913296 | \n",
" -10350.913296 | \n",
" -740.748534 | \n",
" -5954.219866 | \n",
"
\n",
" \n",
" deviance | \n",
" 15374.679054 | \n",
" 15374.679054 | \n",
" 967.455734 | \n",
" 967.455734 | \n",
"
\n",
" \n",
" pearson chi2 | \n",
" 32310.704118 | \n",
" 32310.704118 | \n",
" 926.199428 | \n",
" 926.199428 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" res_o res_f res_e res_a\n",
"llf -10350.913296 -10350.913296 -740.748534 -5954.219866\n",
"deviance 15374.679054 15374.679054 967.455734 967.455734\n",
"pearson chi2 32310.704118 32310.704118 926.199428 926.199428"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame(np.column_stack([[r.llf, r.deviance, r.pearson_chi2] for r in results_all]), \n",
" columns=names, index=['llf', 'deviance', 'pearson chi2'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Likelihood Ratio type tests\n",
"\n",
"We saw above that likelihood and related statistics do not agree between the aggregated and original, individual data. We illustrate in the following that likelihood ratio test and difference in deviance agree across versions, however Pearson chi-squared does not.\n",
"\n",
"As before: This is not sufficiently clear yet and could change.\n",
"\n",
"As a test case we drop the `age` variable and compute the likelihood ratio type statistics as difference between reduced or constrained and full or unconstrained model."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### original observations and frequency weights"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.858628Z",
"iopub.status.busy": "2021-02-02T06:51:25.855966Z",
"iopub.status.idle": "2021-02-02T06:51:25.902431Z",
"shell.execute_reply": "2021-02-02T06:51:25.903351Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(52.91343161821351, 45.726693322507344, -22.863346661253672)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"glm = smf.glm('affairs ~ rate_marriage + yrs_married',\n",
" data=data, family=sm.families.Poisson())\n",
"res_o2 = glm.fit()\n",
"#print(res_f2.summary())\n",
"res_o2.pearson_chi2 - res_o.pearson_chi2, res_o2.deviance - res_o.deviance, res_o2.llf - res_o.llf"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.917778Z",
"iopub.status.busy": "2021-02-02T06:51:25.915458Z",
"iopub.status.idle": "2021-02-02T06:51:25.931222Z",
"shell.execute_reply": "2021-02-02T06:51:25.932287Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(52.913431618562754, 45.726693322507344, -22.863346661253672)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"glm = smf.glm('affairs ~ rate_marriage + yrs_married',\n",
" data=dc, family=sm.families.Poisson(), freq_weights=np.asarray(dc['freq']))\n",
"res_f2 = glm.fit()\n",
"#print(res_f2.summary())\n",
"res_f2.pearson_chi2 - res_f.pearson_chi2, res_f2.deviance - res_f.deviance, res_f2.llf - res_f.llf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### aggregated data: ``exposure`` and ``var_weights``\n",
"\n",
"Note: LR test agrees with original observations, ``pearson_chi2`` differs and has the wrong sign."
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.948275Z",
"iopub.status.busy": "2021-02-02T06:51:25.946362Z",
"iopub.status.idle": "2021-02-02T06:51:25.962108Z",
"shell.execute_reply": "2021-02-02T06:51:25.962688Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(-31.61852752510049, 45.72669332250621, -22.863346661252876)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"glm = smf.glm('affairs_sum ~ rate_marriage + yrs_married',\n",
" data=df_a, family=sm.families.Poisson(), exposure=np.asarray(df_a['affairs_count']))\n",
"res_e2 = glm.fit()\n",
"res_e2.pearson_chi2 - res_e.pearson_chi2, res_e2.deviance - res_e.deviance, res_e2.llf - res_e.llf"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.965638Z",
"iopub.status.busy": "2021-02-02T06:51:25.964909Z",
"iopub.status.idle": "2021-02-02T06:51:25.985764Z",
"shell.execute_reply": "2021-02-02T06:51:25.986487Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(-31.618527525105605, 45.726693322506094, -22.86334666125458)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"glm = smf.glm('affairs_mean ~ rate_marriage + yrs_married',\n",
" data=df_a, family=sm.families.Poisson(), var_weights=np.asarray(df_a['affairs_count']))\n",
"res_a2 = glm.fit()\n",
"res_a2.pearson_chi2 - res_a.pearson_chi2, res_a2.deviance - res_a.deviance, res_a2.llf - res_a.llf"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Investigating Pearson chi-square statistic\n",
"\n",
"First, we do some sanity checks that there are no basic bugs in the computation of `pearson_chi2` and `resid_pearson`. "
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:25.993265Z",
"iopub.status.busy": "2021-02-02T06:51:25.992700Z",
"iopub.status.idle": "2021-02-02T06:51:25.995291Z",
"shell.execute_reply": "2021-02-02T06:51:25.995817Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(894.5809002315154, 926.1994277566159, 894.5809002315154, 926.1994277566159)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_e2.pearson_chi2, res_e.pearson_chi2, (res_e2.resid_pearson**2).sum(), (res_e.resid_pearson**2).sum()"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.005478Z",
"iopub.status.busy": "2021-02-02T06:51:26.004523Z",
"iopub.status.idle": "2021-02-02T06:51:26.010325Z",
"shell.execute_reply": "2021-02-02T06:51:26.011234Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(-3.515546828006896e-13,\n",
" array([ 5.42753476, 46.42940306, 19.98971769, 38.50138978, 11.18341883]),\n",
" array([ 5.42753476, 46.42940306, 19.98971769, 38.50138978, 11.18341883]))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_e._results.resid_response.mean(), res_e.model.family.variance(res_e.mu)[:5], res_e.mu[:5]"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.016551Z",
"iopub.status.busy": "2021-02-02T06:51:26.015840Z",
"iopub.status.idle": "2021-02-02T06:51:26.022529Z",
"shell.execute_reply": "2021-02-02T06:51:26.021888Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"926.1994277566159"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(res_e._results.resid_response**2 / res_e.model.family.variance(res_e.mu)).sum()"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.029254Z",
"iopub.status.busy": "2021-02-02T06:51:26.028502Z",
"iopub.status.idle": "2021-02-02T06:51:26.034194Z",
"shell.execute_reply": "2021-02-02T06:51:26.035123Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(1.7708911260482497e-14,\n",
" array([ 4.77165474, 44.4026604 , 22.2013302 , 39.14749309, 10.54229538]),\n",
" array([ 4.77165474, 44.4026604 , 22.2013302 , 39.14749309, 10.54229538]))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_e2._results.resid_response.mean(), res_e2.model.family.variance(res_e2.mu)[:5], res_e2.mu[:5]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.040844Z",
"iopub.status.busy": "2021-02-02T06:51:26.040106Z",
"iopub.status.idle": "2021-02-02T06:51:26.045784Z",
"shell.execute_reply": "2021-02-02T06:51:26.046937Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"894.5809002315154"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(res_e2._results.resid_response**2 / res_e2.model.family.variance(res_e2.mu)).sum()"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.052797Z",
"iopub.status.busy": "2021-02-02T06:51:26.051754Z",
"iopub.status.idle": "2021-02-02T06:51:26.058673Z",
"shell.execute_reply": "2021-02-02T06:51:26.058047Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(51204.85737832323, 47104.64779595925)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(res_e2._results.resid_response**2).sum(), (res_e._results.resid_response**2).sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"One possible reason for the incorrect sign is that we are subtracting quadratic terms that are divided by different denominators. In some related cases, the recommendation in the literature is to use a common denominator. We can compare pearson chi-squared statistic using the same variance assumption in the full and reduced model. \n",
"\n",
"In this case we obtain the same pearson chi2 scaled difference between reduced and full model across all versions. (Issue [#3616](https://github.com/statsmodels/statsmodels/issues/3616) is intended to track this further.)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.064964Z",
"iopub.status.busy": "2021-02-02T06:51:26.063784Z",
"iopub.status.idle": "2021-02-02T06:51:26.070170Z",
"shell.execute_reply": "2021-02-02T06:51:26.071100Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"44.43314175121918"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((res_e2._results.resid_response**2 - res_e._results.resid_response**2) / res_e2.model.family.variance(res_e2.mu)).sum()"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.077383Z",
"iopub.status.busy": "2021-02-02T06:51:26.076328Z",
"iopub.status.idle": "2021-02-02T06:51:26.082352Z",
"shell.execute_reply": "2021-02-02T06:51:26.083405Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"44.433141751219324"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((res_a2._results.resid_response**2 - res_a._results.resid_response**2) / res_a2.model.family.variance(res_a2.mu)\n",
"* res_a2.model.var_weights).sum()"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.089607Z",
"iopub.status.busy": "2021-02-02T06:51:26.088860Z",
"iopub.status.idle": "2021-02-02T06:51:26.094622Z",
"shell.execute_reply": "2021-02-02T06:51:26.095653Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"44.43314175121836"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((res_f2._results.resid_response**2 - res_f._results.resid_response**2) / res_f2.model.family.variance(res_f2.mu) \n",
" * res_f2.model.freq_weights).sum()"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.101405Z",
"iopub.status.busy": "2021-02-02T06:51:26.100122Z",
"iopub.status.idle": "2021-02-02T06:51:26.107651Z",
"shell.execute_reply": "2021-02-02T06:51:26.108557Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"44.43314175121945"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((res_o2._results.resid_response**2 - res_o._results.resid_response**2) / res_o2.model.family.variance(res_o2.mu)).sum()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Remainder\n",
"\n",
"The remainder of the notebook just contains some additional checks and can be ignored."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.115108Z",
"iopub.status.busy": "2021-02-02T06:51:26.114038Z",
"iopub.status.idle": "2021-02-02T06:51:26.120757Z",
"shell.execute_reply": "2021-02-02T06:51:26.120061Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(array([ 1., 10., 5., 10., 3.]), array([ 1, 10, 5, 10, 3]))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.exp(res_e2.model.exposure)[:5], np.asarray(df_a['affairs_count'])[:5]"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.126775Z",
"iopub.status.busy": "2021-02-02T06:51:26.125800Z",
"iopub.status.idle": "2021-02-02T06:51:26.131503Z",
"shell.execute_reply": "2021-02-02T06:51:26.132441Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"-9.66481794585679"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_e2.resid_pearson.sum() - res_e.resid_pearson.sum()"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.137611Z",
"iopub.status.busy": "2021-02-02T06:51:26.136816Z",
"iopub.status.idle": "2021-02-02T06:51:26.143063Z",
"shell.execute_reply": "2021-02-02T06:51:26.142341Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 4.77165474, 44.4026604 , 22.2013302 , 39.14749309, 10.54229538])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_e2.mu[:5]"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.149132Z",
"iopub.status.busy": "2021-02-02T06:51:26.148434Z",
"iopub.status.idle": "2021-02-02T06:51:26.154218Z",
"shell.execute_reply": "2021-02-02T06:51:26.155127Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(894.5809002315151, 926.1994277566207, -42.3472071351883, -32.68238918932777)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_a2.pearson_chi2, res_a.pearson_chi2, res_a2.resid_pearson.sum(), res_a.resid_pearson.sum()"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.160566Z",
"iopub.status.busy": "2021-02-02T06:51:26.159879Z",
"iopub.status.idle": "2021-02-02T06:51:26.165214Z",
"shell.execute_reply": "2021-02-02T06:51:26.166120Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"894.5809002315151"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((res_a2._results.resid_response**2) / res_a2.model.family.variance(res_a2.mu) * res_a2.model.var_weights).sum()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.171962Z",
"iopub.status.busy": "2021-02-02T06:51:26.170946Z",
"iopub.status.idle": "2021-02-02T06:51:26.176545Z",
"shell.execute_reply": "2021-02-02T06:51:26.177443Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"926.1994277566207"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((res_a._results.resid_response**2) / res_a.model.family.variance(res_a.mu) * res_a.model.var_weights).sum()"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.182983Z",
"iopub.status.busy": "2021-02-02T06:51:26.182301Z",
"iopub.status.idle": "2021-02-02T06:51:26.188410Z",
"shell.execute_reply": "2021-02-02T06:51:26.189318Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"850.1477584802958"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((res_a._results.resid_response**2) / res_a.model.family.variance(res_a2.mu) * res_a.model.var_weights).sum()"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.195192Z",
"iopub.status.busy": "2021-02-02T06:51:26.194270Z",
"iopub.status.idle": "2021-02-02T06:51:26.199867Z",
"shell.execute_reply": "2021-02-02T06:51:26.200727Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(array([ 0., 39., 17., 9., 4.]), array([ 0., 39., 17., 9., 4.]))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_e.model.endog[:5], res_e2.model.endog[:5]"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.206425Z",
"iopub.status.busy": "2021-02-02T06:51:26.205715Z",
"iopub.status.idle": "2021-02-02T06:51:26.210986Z",
"shell.execute_reply": "2021-02-02T06:51:26.211932Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(array([0. , 3.9 , 3.4 , 0.9 , 1.33333333]),\n",
" array([0. , 3.9 , 3.4 , 0.9 , 1.33333333]))"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_a.model.endog[:5], res_a2.model.endog[:5]"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.217486Z",
"iopub.status.busy": "2021-02-02T06:51:26.216798Z",
"iopub.status.idle": "2021-02-02T06:51:26.222372Z",
"shell.execute_reply": "2021-02-02T06:51:26.223330Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 39., 17., 9., 4.])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_a2.model.endog[:5] * np.exp(res_e2.model.exposure)[:5]"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.228562Z",
"iopub.status.busy": "2021-02-02T06:51:26.227870Z",
"iopub.status.idle": "2021-02-02T06:51:26.233245Z",
"shell.execute_reply": "2021-02-02T06:51:26.234159Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([ 0., 39., 17., 9., 4.])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_a2.model.endog[:5] * res_a2.model.var_weights[:5]"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.238571Z",
"iopub.status.busy": "2021-02-02T06:51:26.237219Z",
"iopub.status.idle": "2021-02-02T06:51:26.245267Z",
"shell.execute_reply": "2021-02-02T06:51:26.246287Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"(1.8390448369994542e-07, 6.931421143170174e-08)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from scipy import stats\n",
"stats.chi2.sf(27.19530754604785, 1), stats.chi2.sf(29.083798806764687, 1)"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.250043Z",
"iopub.status.busy": "2021-02-02T06:51:26.248808Z",
"iopub.status.idle": "2021-02-02T06:51:26.257663Z",
"shell.execute_reply": "2021-02-02T06:51:26.258526Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Intercept 3.756282e-141\n",
"rate_marriage 0.000000e+00\n",
"age 2.221918e-11\n",
"yrs_married 1.219200e-02\n",
"dtype: float64"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"res_o.pvalues"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.262658Z",
"iopub.status.busy": "2021-02-02T06:51:26.261433Z",
"iopub.status.idle": "2021-02-02T06:51:26.278684Z",
"shell.execute_reply": "2021-02-02T06:51:26.279670Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs_sum No. Observations: 130\n",
"Model: GLM Df Residuals: 127\n",
"Model Family: Poisson Df Model: 2\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -763.61\n",
"Date: Tue, 02 Feb 2021 Deviance: 1013.2\n",
"Time: 06:51:26 Pearson chi2: 895.\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.0754 0.050 41.512 0.000 1.977 2.173\n",
"rate_marriage -0.4947 0.012 -41.743 0.000 -0.518 -0.471\n",
"yrs_married -0.0360 0.002 -17.542 0.000 -0.040 -0.032\n",
"=================================================================================\n",
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs_sum No. Observations: 130\n",
"Model: GLM Df Residuals: 126\n",
"Model Family: Poisson Df Model: 3\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -740.75\n",
"Date: Tue, 02 Feb 2021 Deviance: 967.46\n",
"Time: 06:51:26 Pearson chi2: 926.\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.7155 0.107 25.294 0.000 2.505 2.926\n",
"rate_marriage -0.4952 0.012 -41.702 0.000 -0.518 -0.472\n",
"age -0.0299 0.004 -6.691 0.000 -0.039 -0.021\n",
"yrs_married -0.0108 0.004 -2.507 0.012 -0.019 -0.002\n",
"=================================================================================\n"
]
}
],
"source": [
"print(res_e2.summary())\n",
"print(res_e.summary())"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {
"collapsed": false,
"execution": {
"iopub.execute_input": "2021-02-02T06:51:26.283611Z",
"iopub.status.busy": "2021-02-02T06:51:26.282376Z",
"iopub.status.idle": "2021-02-02T06:51:26.299672Z",
"shell.execute_reply": "2021-02-02T06:51:26.300563Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs No. Observations: 476\n",
"Model: GLM Df Residuals: 6363\n",
"Model Family: Poisson Df Model: 2\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -10374.\n",
"Date: Tue, 02 Feb 2021 Deviance: 15420.\n",
"Time: 06:51:26 Pearson chi2: 3.24e+04\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.0754 0.050 41.512 0.000 1.977 2.173\n",
"rate_marriage -0.4947 0.012 -41.743 0.000 -0.518 -0.471\n",
"yrs_married -0.0360 0.002 -17.542 0.000 -0.040 -0.032\n",
"=================================================================================\n",
" Generalized Linear Model Regression Results \n",
"==============================================================================\n",
"Dep. Variable: affairs No. Observations: 476\n",
"Model: GLM Df Residuals: 6362\n",
"Model Family: Poisson Df Model: 3\n",
"Link Function: log Scale: 1.0000\n",
"Method: IRLS Log-Likelihood: -10351.\n",
"Date: Tue, 02 Feb 2021 Deviance: 15375.\n",
"Time: 06:51:26 Pearson chi2: 3.23e+04\n",
"No. Iterations: 6 \n",
"Covariance Type: nonrobust \n",
"=================================================================================\n",
" coef std err z P>|z| [0.025 0.975]\n",
"---------------------------------------------------------------------------------\n",
"Intercept 2.7155 0.107 25.294 0.000 2.505 2.926\n",
"rate_marriage -0.4952 0.012 -41.702 0.000 -0.518 -0.472\n",
"age -0.0299 0.004 -6.691 0.000 -0.039 -0.021\n",
"yrs_married -0.0108 0.004 -2.507 0.012 -0.019 -0.002\n",
"=================================================================================\n"
]
}
],
"source": [
"print(res_f2.summary())\n",
"print(res_f.summary())"
]
}
],
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 1
}