{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Mission730Solutions.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "# Purpose of Notebook\n",
        "\n",
        "The purpose of this notebook is to offer as a an example answer to the Guided Project for Linear Regression in Python course. Since the choice of model predictors is up to the student, results can differ. Use this solution as a guide to how to structure your own answer."
      ],
      "metadata": {
        "id": "OsSvDQ0Y3yNk"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "import pandas as pd\n",
        "import numpy as np\n",
        "from sklearn.linear_model import LinearRegression\n",
        "from sklearn.metrics import mean_squared_error, r2_score\n",
        "from sklearn.model_selection import train_test_split"
      ],
      "metadata": {
        "id": "lmpsg5fi4t8d"
      },
      "execution_count": 1,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "# Load in the insurance dataset\n",
        "insurance = pd.read_csv(\"insurance.csv\")"
      ],
      "metadata": {
        "id": "y6aqBHg-5cJT"
      },
      "execution_count": 2,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Exploring The Dataset"
      ],
      "metadata": {
        "id": "wS1GbH1g7ta5"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Columns in the dataset\n",
        "insurance.columns"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "JuqpqwPd5l6d",
        "outputId": "470c6b14-184b-4489-e151-0a6cfe235d08"
      },
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['age', 'sex', 'bmi', 'children', 'smoker', 'region', 'charges'], dtype='object')"
            ]
          },
          "metadata": {},
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "The `charges` column is our outcome, while everything else are the potential predictors to use in the model"
      ],
      "metadata": {
        "id": "M5zR7HAr8tsb"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "insurance.hist(\"charges\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 316
        },
        "id": "SMObDGAlOci0",
        "outputId": "82673471-fdbe-48fe-fc14-5974fab5f95f"
      },
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7fdf38c164d0>]],\n",
              "      dtype=object)"
            ]
          },
          "metadata": {},
          "execution_count": 4
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 7488 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "The `charges` column is highly skewed to the right. Extremely costly insurance charges are more common than extremely small ones. This makes it unlikely that the errors in the model will truly be centered at zero. It might be worth it to log-transform the outcome."
      ],
      "metadata": {
        "id": "5rSOAfQHOr_8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "insurance[\"log_charges\"] = np.log2(insurance[\"charges\"])\n",
        "\n",
        "insurance.hist(\"log_charges\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 316
        },
        "id": "FG2IyzGFPHF2",
        "outputId": "c0f8e3d8-57fd-4467-e164-6797960c871f"
      },
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[<matplotlib.axes._subplots.AxesSubplot object at 0x7fdf38b11ad0>]],\n",
              "      dtype=object)"
            ]
          },
          "metadata": {},
          "execution_count": 5
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 6936 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "The log-transformed `charges` values are more centered, which is what we wanted. This makes it more likely that the errors will be unbiased."
      ],
      "metadata": {
        "id": "sFiFbsCUQHv8"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Checking the correlation between the continuous columns in the insurance data\n",
        "insurance.corr()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 206
        },
        "id": "uSRq96HH8AAR",
        "outputId": "ace3b1fc-0cbc-4cce-b57e-38b26b1dbfe7"
      },
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "                  age       bmi  children   charges  log_charges\n",
              "age          1.000000  0.109272  0.042469  0.299008     0.527834\n",
              "bmi          0.109272  1.000000  0.012759  0.198341     0.132669\n",
              "children     0.042469  0.012759  1.000000  0.067998     0.161336\n",
              "charges      0.299008  0.198341  0.067998  1.000000     0.892964\n",
              "log_charges  0.527834  0.132669  0.161336  0.892964     1.000000"
            ],
            "text/html": [
              "\n",
              "  <div id=\"df-22fe3727-a635-41c7-b467-156b800a2867\">\n",
              "    <div class=\"colab-df-container\">\n",
              "      <div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>age</th>\n",
              "      <th>bmi</th>\n",
              "      <th>children</th>\n",
              "      <th>charges</th>\n",
              "      <th>log_charges</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>age</th>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.109272</td>\n",
              "      <td>0.042469</td>\n",
              "      <td>0.299008</td>\n",
              "      <td>0.527834</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>bmi</th>\n",
              "      <td>0.109272</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.012759</td>\n",
              "      <td>0.198341</td>\n",
              "      <td>0.132669</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>children</th>\n",
              "      <td>0.042469</td>\n",
              "      <td>0.012759</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.067998</td>\n",
              "      <td>0.161336</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>charges</th>\n",
              "      <td>0.299008</td>\n",
              "      <td>0.198341</td>\n",
              "      <td>0.067998</td>\n",
              "      <td>1.000000</td>\n",
              "      <td>0.892964</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>log_charges</th>\n",
              "      <td>0.527834</td>\n",
              "      <td>0.132669</td>\n",
              "      <td>0.161336</td>\n",
              "      <td>0.892964</td>\n",
              "      <td>1.000000</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>\n",
              "      <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-22fe3727-a635-41c7-b467-156b800a2867')\"\n",
              "              title=\"Convert this dataframe to an interactive table.\"\n",
              "              style=\"display:none;\">\n",
              "        \n",
              "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
              "       width=\"24px\">\n",
              "    <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n",
              "    <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n",
              "  </svg>\n",
              "      </button>\n",
              "      \n",
              "  <style>\n",
              "    .colab-df-container {\n",
              "      display:flex;\n",
              "      flex-wrap:wrap;\n",
              "      gap: 12px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert {\n",
              "      background-color: #E8F0FE;\n",
              "      border: none;\n",
              "      border-radius: 50%;\n",
              "      cursor: pointer;\n",
              "      display: none;\n",
              "      fill: #1967D2;\n",
              "      height: 32px;\n",
              "      padding: 0 0 0 0;\n",
              "      width: 32px;\n",
              "    }\n",
              "\n",
              "    .colab-df-convert:hover {\n",
              "      background-color: #E2EBFA;\n",
              "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
              "      fill: #174EA6;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert {\n",
              "      background-color: #3B4455;\n",
              "      fill: #D2E3FC;\n",
              "    }\n",
              "\n",
              "    [theme=dark] .colab-df-convert:hover {\n",
              "      background-color: #434B5C;\n",
              "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
              "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
              "      fill: #FFFFFF;\n",
              "    }\n",
              "  </style>\n",
              "\n",
              "      <script>\n",
              "        const buttonEl =\n",
              "          document.querySelector('#df-22fe3727-a635-41c7-b467-156b800a2867 button.colab-df-convert');\n",
              "        buttonEl.style.display =\n",
              "          google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
              "\n",
              "        async function convertToInteractive(key) {\n",
              "          const element = document.querySelector('#df-22fe3727-a635-41c7-b467-156b800a2867');\n",
              "          const dataTable =\n",
              "            await google.colab.kernel.invokeFunction('convertToInteractive',\n",
              "                                                     [key], {});\n",
              "          if (!dataTable) return;\n",
              "\n",
              "          const docLinkHtml = 'Like what you see? Visit the ' +\n",
              "            '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
              "            + ' to learn more about interactive tables.';\n",
              "          element.innerHTML = '';\n",
              "          dataTable['output_type'] = 'display_data';\n",
              "          await google.colab.output.renderOutput(dataTable, element);\n",
              "          const docLink = document.createElement('div');\n",
              "          docLink.innerHTML = docLinkHtml;\n",
              "          element.appendChild(docLink);\n",
              "        }\n",
              "      </script>\n",
              "    </div>\n",
              "  </div>\n",
              "  "
            ]
          },
          "metadata": {},
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Comments on correlation\n",
        "\n",
        "`age` has 30% correlation with `charges`, `bmi` has 19.8% correlation, and number of children has 6.7% correlation."
      ],
      "metadata": {
        "id": "-ghJAB_59B9Z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "insurance.boxplot(column = [\"log_charges\"], by = \"sex\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 373
        },
        "id": "amZ_91bU8352",
        "outputId": "dec66ac2-8799-432a-9aa5-344d5a6eaac6"
      },
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py:1376: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
            "  X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7fdf385f8690>"
            ]
          },
          "metadata": {},
          "execution_count": 7
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 8364 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "insurance.boxplot(column = [\"log_charges\"], by = \"smoker\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 373
        },
        "id": "9_gIr-7AJ1Uo",
        "outputId": "61cfc34b-4718-429c-ff27-2e9ee9e39d22"
      },
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py:1376: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
            "  X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7fdf385c4510>"
            ]
          },
          "metadata": {},
          "execution_count": 8
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 8696 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "insurance.boxplot(column = [\"log_charges\"], by = \"region\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 373
        },
        "id": "Z5-n30hmKH_Y",
        "outputId": "aeabd663-0781-432c-954f-7ce33dc5c4f0"
      },
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py:1376: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
            "  X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7fdf38466110>"
            ]
          },
          "metadata": {},
          "execution_count": 9
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 9984 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "insurance.boxplot(column = [\"log_charges\"], by = \"region\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 373
        },
        "id": "lVogyvIOKK6o",
        "outputId": "30499162-adaf-4260-f26d-b650759d4b73"
      },
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stderr",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/matplotlib/cbook/__init__.py:1376: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray.\n",
            "  X = np.atleast_1d(X.T if isinstance(X, np.ndarray) else np.asarray(X))\n"
          ]
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7fdf385da190>"
            ]
          },
          "metadata": {},
          "execution_count": 10
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 9984 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Comments on plots\n",
        "\n",
        "Males seem to have a wider distribution of charges compared to women. Smokers have much higher costs than non-smokers. There doesn't seem tobe many appreciable differences between regions. "
      ],
      "metadata": {
        "id": "EcQUy2RaKPNh"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Dividing The Data\n",
        "\n",
        "Based on the univariate relationships shown above, `age`, `bmi` and `smoker` are positively associated with higher `charges`. We'll include these predictors in our final model."
      ],
      "metadata": {
        "id": "Q4f6SIK8Kv-k"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Splitting the data up into a training and test set\n",
        "insurance[\"is_smoker\"] = (insurance[\"smoker\"] == \"yes\")\n",
        "X = insurance[[\"age\", \"bmi\", \"is_smoker\"]]\n",
        "y = insurance[\"log_charges\"]\n",
        "\n",
        "# 75% for training set, 25% for test set\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25, \n",
        "                                                    random_state = 1)"
      ],
      "metadata": {
        "id": "lihP2kTALKL-"
      },
      "execution_count": 11,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Build The Model"
      ],
      "metadata": {
        "id": "YE-XKUr_428I"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Training and checking model performance on training set\n",
        "insurance_model = LinearRegression()\n",
        "insurance_model.fit(X_train, y_train)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jCHcD-PcLfxu",
        "outputId": "93957439-f797-4f85-e5fc-1149ae619dd0"
      },
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "LinearRegression()"
            ]
          },
          "metadata": {},
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        ""
      ],
      "metadata": {
        "id": "r9pR9V2TOYdT"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Get predicted values by model\n",
        "y_pred = insurance_model.predict(X_train)\n",
        "\n",
        "# MSE on the log scale for the insurance charges\n",
        "mean_squared_error(y_train, y_pred)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "WY4gnJJpMqNN",
        "outputId": "ac7676db-6ccf-4b35-aa30-d81905dac57d"
      },
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.4546665339270644"
            ]
          },
          "metadata": {},
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# MSE on the original scale for the insurance charges\n",
        "np.exp(mean_squared_error(y_train, y_pred))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "YvUvASEyQ0aN",
        "outputId": "5f99b5a4-317f-47cc-93b1-8d22464612a6"
      },
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1.575647870310887"
            ]
          },
          "metadata": {},
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Coefficient of determination\n",
        "r2_score(y_train, y_pred)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tOFi1oxFRFPk",
        "outputId": "351d9bd3-2b78-4ff0-fec0-f5ca559b5d1c"
      },
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.7421118855283421"
            ]
          },
          "metadata": {},
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "## Comments\n",
        "\n",
        "The training MSE for the model is 0.454 and is 1.57 on the original scale. The $R^2$ indicates that the model can explain 74% of the variation in the log-insurance charges. These preliminary results are promising, but we must remember that these are optimistic values."
      ],
      "metadata": {
        "id": "70BciwrTQucW"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Residual Diagnostics"
      ],
      "metadata": {
        "id": "Op2iO4grSZBZ"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Quick visual check of residuals\n",
        "check = pd.DataFrame()\n",
        "check[\"residuals\"] = y_train - y_pred\n",
        "check[\"fitted\"] = y_pred\n",
        "\n",
        "check.plot.scatter(x = \"fitted\", y = \"residuals\")"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 296
        },
        "id": "dLCBNJMuNb-x",
        "outputId": "626a7509-6890-4932-cd42-8b443001d99f"
      },
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "<matplotlib.axes._subplots.AxesSubplot at 0x7fdf383015d0>"
            ]
          },
          "metadata": {},
          "execution_count": 16
        },
        {
          "output_type": "display_data",
          "data": {
            "text/plain": [
              "<Figure size 432x288 with 1 Axes>"
            ],
            "image/png": "[encoded data, 25304 characters]\n"
          },
          "metadata": {
            "needs_background": "light"
          }
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "The residuals suggest some violations to the assumptions of linear regression. As fitted values get larger, the residuals trend downward. We expect an even band, centered around zero. This does not necessarily make the model predictions unusable, but it puts into question the linear regression assumptions."
      ],
      "metadata": {
        "id": "PW19C6k5TgUS"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Interpreting The Model"
      ],
      "metadata": {
        "id": "2NpUAgXgUI0Z"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Getting the non-intercept coefficients\n",
        "insurance_model.coef_"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "YmfjKyIjSqr-",
        "outputId": "7e388b83-ffb1-4116-c711-7aabea9312d5"
      },
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([0.04892865, 0.01523672, 2.23063344])"
            ]
          },
          "metadata": {},
          "execution_count": 17
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "- A year increase in the subject is associated with a 0.04 increase in the log charges, holding smoking status and bmi constant. About a 4% increase in the charges on the regular scale.\n",
        "- A unit increase in the subject BMI is associated with a 0.01 increase in the log charges, holding smoking status and age constant.  About a 1.5% increase in the charges on the regular scale.\n",
        "- A smoker is associated with a 2.23 increase in the log charges, holding age and bmi constant. About a 930% increase in the charges on the regular scale.\n",
        "\n",
        "Note: we are not concerned about if these changes are *statistically significant*, so we don't know if these associations are truly non-zero. Our primary goal is prediction."
      ],
      "metadata": {
        "id": "guf4YyvQVSfq"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Final Model Evaluation"
      ],
      "metadata": {
        "id": "aYOK6CucWXBB"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "# Getting MSE on test model\n",
        "test_pred = insurance_model.predict(X_test)\n",
        "\n",
        "mean_squared_error(y_test, test_pred)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "tXrcpejXUNDv",
        "outputId": "fbbab0a6-4446-4b35-8626-dc4a5d47dd76"
      },
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "0.4355350875308211"
            ]
          },
          "metadata": {},
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "# Putting the outcome (in log-terms) back into the original scale\n",
        "np.exp(mean_squared_error(y_test, test_pred))"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "NjH8j9ZPXDHv",
        "outputId": "001d7c0a-70db-4be3-8a1d-84521e71ac8b"
      },
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "1.545789970635098"
            ]
          },
          "metadata": {},
          "execution_count": 20
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "# Drawing Conclusions\n",
        "\n",
        "The test MSE was about 0.435, while the training MSE was about 0.454. In this case, the two errors match up pretty well, so we can conclude that the model is not overfit. The residuals suggest that the model is predicting much lower costs for subjects who were actually charged much higher. Therefore the model struggles with these higher costs. As a whole, the model predictions are too conservative. \n",
        "\n",
        "We might improve the model by including more complex terms in the regression, such as interactions or quadratic terms. "
      ],
      "metadata": {
        "id": "iZcGg2tgWpGx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        ""
      ],
      "metadata": {
        "id": "sMKpzdDG5B7_"
      },
      "execution_count": 19,
      "outputs": []
    }
  ]
}