{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "WvMflodMjQKe"
   },
   "source": [
    "# <font color=green size=10>CURSO DE ESTATÍSTICA - PARTE 4</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "cVgsZ_E3jQKg"
   },
   "source": [
    "# <font color=green>1 CONHECENDO OS DADOS</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "TDjqYYNojQKh"
   },
   "source": [
    "## <font color=green>1.1 Dataset do projeto</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "iN1gIbaKjQKh"
   },
   "source": [
    "### Pesquisa Nacional por Amostra de Domicílios - 2015\n",
    "\n",
    "A <b>Pesquisa Nacional por Amostra de Domicílios - PNAD</b> investiga anualmente, de forma permanente, características gerais da população, de educação, trabalho, rendimento e habitação e outras, com periodicidade variável, de acordo com as necessidades de informação para o país, como as características sobre migração, fecundidade, nupcialidade, saúde, segurança alimentar, entre outros temas. O levantamento dessas estatísticas constitui, ao longo dos 49 anos de realização da pesquisa, um importante instrumento para formulação, validação e avaliação de políticas orientadas para o desenvolvimento socioeconômico e a melhoria das condições de vida no Brasil."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "U6V7br1RjQKi"
   },
   "source": [
    "### Fonte dos Dados\n",
    "\n",
    "https://ww2.ibge.gov.br/home/estatistica/populacao/trabalhoerendimento/pnad2015/microdados.shtm"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "r0Fe_qnzjQKj"
   },
   "source": [
    "### Variáveis utilizadas\n",
    "\n",
    "> ### Renda\n",
    "> ***\n",
    "\n",
    "Rendimento mensal do trabalho principal para pessoas de 10 anos ou mais de idade.\n",
    "\n",
    "> ### Idade\n",
    "> ***\n",
    "\n",
    "Idade do morador na data de referência em anos.\n",
    "\n",
    "> ### Altura (elaboração própria)\n",
    "> ***\n",
    "\n",
    "Altura do morador em metros.\n",
    "\n",
    "> ### UF\n",
    "> ***\n",
    "\n",
    "|Código|Descrição|\n",
    "|---|---|\n",
    "|11|Rondônia|\n",
    "|12|Acre|\n",
    "|13|Amazonas|\n",
    "|14|Roraima|\n",
    "|15|Pará|\n",
    "|16|Amapá|\n",
    "|17|Tocantins|\n",
    "|21|Maranhão|\n",
    "|22|Piauí|\n",
    "|23|Ceará|\n",
    "|24|Rio Grande do Norte|\n",
    "|25|Paraíba|\n",
    "|26|Pernambuco|\n",
    "|27|Alagoas|\n",
    "|28|Sergipe|\n",
    "|29|Bahia|\n",
    "|31|Minas Gerais|\n",
    "|32|Espírito Santo|\n",
    "|33|Rio de Janeiro|\n",
    "|35|São Paulo|\n",
    "|41|Paraná|\n",
    "|42|Santa Catarina|\n",
    "|43|Rio Grande do Sul|\n",
    "|50|Mato Grosso do Sul|\n",
    "|51|Mato Grosso|\n",
    "|52|Goiás|\n",
    "|53|Distrito Federal|\n",
    "\n",
    "> ### Sexo\t\n",
    "> ***\n",
    "\n",
    "|Código|Descrição|\n",
    "|---|---|\n",
    "|0|Masculino|\n",
    "|1|Feminino|\n",
    "\n",
    "> ### Anos de Estudo\n",
    "> ***\n",
    "\n",
    "|Código|Descrição|\n",
    "|---|---|\n",
    "|1|Sem instrução e menos de 1 ano|\n",
    "|2|1 ano|\n",
    "|3|2 anos|\n",
    "|4|3 anos|\n",
    "|5|4 anos|\n",
    "|6|5 anos|\n",
    "|7|6 anos|\n",
    "|8|7 anos|\n",
    "|9|8 anos|\n",
    "|10|9 anos|\n",
    "|11|10 anos|\n",
    "|12|11 anos|\n",
    "|13|12 anos|\n",
    "|14|13 anos|\n",
    "|15|14 anos|\n",
    "|16|15 anos ou mais|\n",
    "|17|Não determinados| \n",
    "||Não aplicável|\n",
    "\n",
    "> ### Cor\n",
    "> ***\n",
    "\n",
    "|Código|Descrição|\n",
    "|---|---|\n",
    "|0|Indígena|\n",
    "|2|Branca|\n",
    "|4|Preta|\n",
    "|6|Amarela|\n",
    "|8|Parda|\n",
    "|9|Sem declaração|"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OK1IQH5cX7Gb"
   },
   "source": [
    "#### <font color='red'>Observação</font>\n",
    "***\n",
    "> Os seguintes tratamentos foram realizados nos dados originais:\n",
    "> 1. Foram eliminados os registros onde a <b>Renda</b> era inválida (999 999 999 999);\n",
    "> 2. Foram eliminados os registros onde a <b>Renda</b> era missing;\n",
    "> 3. Foram considerados somente os registros das <b>Pessoas de Referência</b> de cada domicílio (responsável pelo domicílio)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "_mFnZ15pcmu1"
   },
   "source": [
    "### Solução do problema com dependência do statsmodels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 217
    },
    "colab_type": "code",
    "id": "m5QYUoH4cnp1",
    "outputId": "ae1b7302-bf47-42a7-a627-95af2790f810"
   },
   "outputs": [],
   "source": [
    "# Retire o comentário da linha de código abaixo caso precise fazer a correção\n",
    "\n",
    "# !pip install scipy==1.2 --upgrade"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "MAO1aY6GcajB"
   },
   "source": [
    "### Importando bibliotecas\n",
    "\n",
    "https://pandas.pydata.org/\n",
    "\n",
    "https://www.numpy.org/\n",
    "\n",
    "https://seaborn.pydata.org/"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "8QgOpE-wjQKl"
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "8Rw70V6ojQKk"
   },
   "source": [
    "### Lendo o dataset do projeto"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ufRZ7TLwjQKo"
   },
   "outputs": [],
   "source": [
    "dados = pd.read_csv('dados.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "GsC_WhLFjQKq",
    "outputId": "864c083b-7233-49f5-a7bf-94c31169f30f",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>UF</th>\n",
       "      <th>Sexo</th>\n",
       "      <th>Idade</th>\n",
       "      <th>Cor</th>\n",
       "      <th>Anos de Estudo</th>\n",
       "      <th>Renda</th>\n",
       "      <th>Altura</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>23</td>\n",
       "      <td>8</td>\n",
       "      <td>12</td>\n",
       "      <td>800</td>\n",
       "      <td>1.603808</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>23</td>\n",
       "      <td>2</td>\n",
       "      <td>12</td>\n",
       "      <td>1150</td>\n",
       "      <td>1.739790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>35</td>\n",
       "      <td>8</td>\n",
       "      <td>15</td>\n",
       "      <td>880</td>\n",
       "      <td>1.760444</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>11</td>\n",
       "      <td>0</td>\n",
       "      <td>46</td>\n",
       "      <td>2</td>\n",
       "      <td>6</td>\n",
       "      <td>3500</td>\n",
       "      <td>1.783158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>11</td>\n",
       "      <td>1</td>\n",
       "      <td>47</td>\n",
       "      <td>8</td>\n",
       "      <td>9</td>\n",
       "      <td>150</td>\n",
       "      <td>1.690631</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   UF  Sexo  Idade  Cor  Anos de Estudo  Renda    Altura\n",
       "0  11     0     23    8              12    800  1.603808\n",
       "1  11     1     23    2              12   1150  1.739790\n",
       "2  11     1     35    8              15    880  1.760444\n",
       "3  11     0     46    2               6   3500  1.783158\n",
       "4  11     1     47    8               9    150  1.690631"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dados.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "I8SZvx_GcajO"
   },
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "dPTT675fcajP"
   },
   "source": [
    "# <font color=green>2 RODANDO UMA REGRESSÃO LINEAR</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "yRxm2BrFcajQ"
   },
   "source": [
    "## Dataset de exemplo\n",
    "> ### $Y$ = Gasto das famílias\n",
    "> ### $X$ = Renda das Famílias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "TvENegfwcajQ"
   },
   "outputs": [],
   "source": [
    "dataset = {\n",
    "    'Y': [3011, 1305, 1879, 2654, 2849, 1068, 2892, 2543, 3074, 849, 2184, 2943, 1357, 2755, 2163, 3099, 1600, 353, 1778, 740, 2129, 3302, 2412, 2683, 2515, 2395, 2292, 1000, 600, 1864, 3027, 1978, 2791, 1982, 900, 1964, 1247, 3067, 700, 1500, 3110, 2644, 1378, 2601, 501, 1292, 2125, 1431, 2260, 1770],\n",
    "    'X': [9714, 3728, 6062, 8845, 8378, 3338, 8507, 7947, 9915, 1632, 6825, 8918, 4100, 9184, 6180, 9997, 4500, 1069, 5925, 2466, 6083, 9712, 7780, 8383, 7185, 7483, 7640, 2100, 2000, 6012, 8902, 5345, 8210, 5662, 2700, 6546, 2900, 9894, 1500, 5000, 8885, 8813, 3446, 7881, 1164, 3401, 6641, 3329, 6648, 4800]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "HfeWvtFWcajT",
    "outputId": "74959df4-b6bb-488a-a98e-fe77f9393415"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X\n",
       "0  3011  9714\n",
       "1  1305  3728\n",
       "2  1879  6062\n",
       "3  2654  8845\n",
       "4  2849  8378"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset = pd.DataFrame(dataset)\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "NGPOPV1VcajX",
    "outputId": "801add58-89f8-4a20-8218-fa7108f5ac7a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(50, 2)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.shape"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "y0Lt1C-Ccaja"
   },
   "source": [
    "### Estatísticas descritivas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 297
    },
    "colab_type": "code",
    "id": "POZusgEccajb",
    "outputId": "43d3c550-b52f-48bd-d4a5-32f0b634abed"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>50.000000</td>\n",
       "      <td>50.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2011.120000</td>\n",
       "      <td>6065.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>817.214648</td>\n",
       "      <td>2707.187812</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>353.000000</td>\n",
       "      <td>1069.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>1362.250000</td>\n",
       "      <td>3516.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2127.000000</td>\n",
       "      <td>6363.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2675.750000</td>\n",
       "      <td>8381.750000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>3302.000000</td>\n",
       "      <td>9997.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                 Y            X\n",
       "count    50.000000    50.000000\n",
       "mean   2011.120000  6065.500000\n",
       "std     817.214648  2707.187812\n",
       "min     353.000000  1069.000000\n",
       "25%    1362.250000  3516.500000\n",
       "50%    2127.000000  6363.000000\n",
       "75%    2675.750000  8381.750000\n",
       "max    3302.000000  9997.000000"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "tVozByhrcajd"
   },
   "source": [
    "### Análise gráfica\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.boxplot.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 432
    },
    "colab_type": "code",
    "id": "xPtAowUacajf",
    "outputId": "999d7778-2e6f-4889-c6ae-230c3f3118ea"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x239ad6dc5f8>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 9520 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.boxplot(data=dataset, orient='h', width=0.5)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Box plot', fontsize=20)\n",
    "ax.set_xlabel('Reais (R$)', fontsize=16)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Fg0MIFfgcaji"
   },
   "source": [
    "https://seaborn.pydata.org/generated/seaborn.lmplot.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 474
    },
    "colab_type": "code",
    "id": "s2ZFx_INcajj",
    "outputId": "cd551661-024c-4c0c-8a24-8548eb8ef207"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x239ada0cb70>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 45028 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.lmplot(x=\"X\", y=\"Y\", data=dataset)\n",
    "ax.fig.set_size_inches(12, 6)\n",
    "ax.fig.suptitle('Reta de Regressão - Gasto X Renda', fontsize=16, y=1.02)\n",
    "ax.set_xlabels(\"Renda das Famílias\", fontsize=14)\n",
    "ax.set_ylabels(\"Gasto das Famílias\", fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "c2OpdF85cajl"
   },
   "source": [
    "### Análise da correlação\n",
    "\n",
    "https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.corr.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 111
    },
    "colab_type": "code",
    "id": "eQEiUOdccajm",
    "outputId": "020089c6-ebe5-4afc-d887-2fb96d1dd90e",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Y</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.984835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>X</th>\n",
       "      <td>0.984835</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Y         X\n",
       "Y  1.000000  0.984835\n",
       "X  0.984835  1.000000"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.corr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "cPOlHTg2cajo"
   },
   "source": [
    "### Modelo de regressão linear simples\n",
    "\n",
    "https://www.statsmodels.org/stable/regression.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "SidSGHeQcajo"
   },
   "outputs": [],
   "source": [
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 73
    },
    "colab_type": "code",
    "id": "iBQFGkf_cajr",
    "outputId": "7ec1f5e1-c161-4e2b-d7d8-1fc9c2328662"
   },
   "outputs": [],
   "source": [
    "Y = dataset.Y\n",
    "X = sm.add_constant(dataset.X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 125
    },
    "colab_type": "code",
    "id": "PdS9cDXqcajt",
    "outputId": "aa952c5e-ad21-47e9-81a9-04bf78cd224b",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    3011\n",
       "1    1305\n",
       "2    1879\n",
       "3    2654\n",
       "4    2849\n",
       "Name: Y, dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "lBrVRfVfcajv",
    "outputId": "0813c19d-8a54-4cde-bb3a-9cee9d47d030",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>const</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>9714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>6062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>8845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>8378</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   const     X\n",
       "0    1.0  9714\n",
       "1    1.0  3728\n",
       "2    1.0  6062\n",
       "3    1.0  8845\n",
       "4    1.0  8378"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Dkg9S5tWcajy"
   },
   "source": [
    "### Estimando o modelo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "9O0dhVTfcajz"
   },
   "outputs": [],
   "source": [
    "resultado_regressao = sm.OLS(Y, X).fit()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 503
    },
    "colab_type": "code",
    "id": "8W1oGgNecaj0",
    "outputId": "aa4b6d35-1512-4cc2-e502-368cc81a21b2"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      Y   R-squared:                       0.970\n",
      "Model:                            OLS   Adj. R-squared:                  0.969\n",
      "Method:                 Least Squares   F-statistic:                     1547.\n",
      "Date:                Mon, 29 Jul 2019   Prob (F-statistic):           3.56e-38\n",
      "Time:                        10:40:34   Log-Likelihood:                -318.16\n",
      "No. Observations:                  50   AIC:                             640.3\n",
      "Df Residuals:                      48   BIC:                             644.1\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const        207.9033     50.127      4.148      0.000     107.117     308.690\n",
      "X              0.2973      0.008     39.328      0.000       0.282       0.312\n",
      "==============================================================================\n",
      "Omnibus:                       22.833   Durbin-Watson:                   2.408\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):                3.915\n",
      "Skew:                           0.073   Prob(JB):                        0.141\n",
      "Kurtosis:                       1.637   Cond. No.                     1.64e+04\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "[2] The condition number is large, 1.64e+04. This might indicate that there are\n",
      "strong multicollinearity or other numerical problems.\n"
     ]
    }
   ],
   "source": [
    "print(resultado_regressao.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "eP2wm9fBcaj2"
   },
   "source": [
    "### Obtendo as previsões dentro da amostra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1599
    },
    "colab_type": "code",
    "id": "cbuzGLB-caj2",
    "outputId": "5f069bbe-aa37-4317-a186-01b4a64ed728",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1068</td>\n",
       "      <td>3338</td>\n",
       "      <td>1200.259640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2892</td>\n",
       "      <td>8507</td>\n",
       "      <td>2736.955222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2543</td>\n",
       "      <td>7947</td>\n",
       "      <td>2570.472435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3074</td>\n",
       "      <td>9915</td>\n",
       "      <td>3155.540515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>849</td>\n",
       "      <td>1632</td>\n",
       "      <td>693.081721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2184</td>\n",
       "      <td>6825</td>\n",
       "      <td>2236.912280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2943</td>\n",
       "      <td>8918</td>\n",
       "      <td>2859.141696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1357</td>\n",
       "      <td>4100</td>\n",
       "      <td>1426.795147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2755</td>\n",
       "      <td>9184</td>\n",
       "      <td>2938.221020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2163</td>\n",
       "      <td>6180</td>\n",
       "      <td>2045.159784</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>3099</td>\n",
       "      <td>9997</td>\n",
       "      <td>3179.918352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1600</td>\n",
       "      <td>4500</td>\n",
       "      <td>1545.711423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>353</td>\n",
       "      <td>1069</td>\n",
       "      <td>525.707062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1778</td>\n",
       "      <td>5925</td>\n",
       "      <td>1969.350658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>740</td>\n",
       "      <td>2466</td>\n",
       "      <td>941.022158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2129</td>\n",
       "      <td>6083</td>\n",
       "      <td>2016.322587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>3302</td>\n",
       "      <td>9712</td>\n",
       "      <td>3095.190505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2412</td>\n",
       "      <td>7780</td>\n",
       "      <td>2520.824890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2683</td>\n",
       "      <td>8383</td>\n",
       "      <td>2700.091176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2515</td>\n",
       "      <td>7185</td>\n",
       "      <td>2343.936929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2395</td>\n",
       "      <td>7483</td>\n",
       "      <td>2432.529554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>2292</td>\n",
       "      <td>7640</td>\n",
       "      <td>2479.204193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1000</td>\n",
       "      <td>2100</td>\n",
       "      <td>832.213765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>600</td>\n",
       "      <td>2000</td>\n",
       "      <td>802.484696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1864</td>\n",
       "      <td>6012</td>\n",
       "      <td>1995.214948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>3027</td>\n",
       "      <td>8902</td>\n",
       "      <td>2854.385045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>1978</td>\n",
       "      <td>5345</td>\n",
       "      <td>1796.922057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>2791</td>\n",
       "      <td>8210</td>\n",
       "      <td>2648.659887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>1982</td>\n",
       "      <td>5662</td>\n",
       "      <td>1891.163206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>900</td>\n",
       "      <td>2700</td>\n",
       "      <td>1010.588179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>1964</td>\n",
       "      <td>6546</td>\n",
       "      <td>2153.968177</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>1247</td>\n",
       "      <td>2900</td>\n",
       "      <td>1070.046318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>3067</td>\n",
       "      <td>9894</td>\n",
       "      <td>3149.297410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>700</td>\n",
       "      <td>1500</td>\n",
       "      <td>653.839350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>1500</td>\n",
       "      <td>5000</td>\n",
       "      <td>1694.356769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>3110</td>\n",
       "      <td>8885</td>\n",
       "      <td>2849.331103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>2644</td>\n",
       "      <td>8813</td>\n",
       "      <td>2827.926174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>1378</td>\n",
       "      <td>3446</td>\n",
       "      <td>1232.367035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>2601</td>\n",
       "      <td>7881</td>\n",
       "      <td>2550.851249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>501</td>\n",
       "      <td>1164</td>\n",
       "      <td>553.949678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>1292</td>\n",
       "      <td>3401</td>\n",
       "      <td>1218.988954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>2125</td>\n",
       "      <td>6641</td>\n",
       "      <td>2182.210793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>1431</td>\n",
       "      <td>3329</td>\n",
       "      <td>1197.584024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>2260</td>\n",
       "      <td>6648</td>\n",
       "      <td>2184.291828</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>1770</td>\n",
       "      <td>4800</td>\n",
       "      <td>1634.898631</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Y     X   Y_previsto\n",
       "0   3011  9714  3095.785086\n",
       "1   1305  3728  1316.203010\n",
       "2   1879  6062  2010.079483\n",
       "3   2654  8845  2837.439476\n",
       "4   2849  8378  2698.604723\n",
       "5   1068  3338  1200.259640\n",
       "6   2892  8507  2736.955222\n",
       "7   2543  7947  2570.472435\n",
       "8   3074  9915  3155.540515\n",
       "9    849  1632   693.081721\n",
       "10  2184  6825  2236.912280\n",
       "11  2943  8918  2859.141696\n",
       "12  1357  4100  1426.795147\n",
       "13  2755  9184  2938.221020\n",
       "14  2163  6180  2045.159784\n",
       "15  3099  9997  3179.918352\n",
       "16  1600  4500  1545.711423\n",
       "17   353  1069   525.707062\n",
       "18  1778  5925  1969.350658\n",
       "19   740  2466   941.022158\n",
       "20  2129  6083  2016.322587\n",
       "21  3302  9712  3095.190505\n",
       "22  2412  7780  2520.824890\n",
       "23  2683  8383  2700.091176\n",
       "24  2515  7185  2343.936929\n",
       "25  2395  7483  2432.529554\n",
       "26  2292  7640  2479.204193\n",
       "27  1000  2100   832.213765\n",
       "28   600  2000   802.484696\n",
       "29  1864  6012  1995.214948\n",
       "30  3027  8902  2854.385045\n",
       "31  1978  5345  1796.922057\n",
       "32  2791  8210  2648.659887\n",
       "33  1982  5662  1891.163206\n",
       "34   900  2700  1010.588179\n",
       "35  1964  6546  2153.968177\n",
       "36  1247  2900  1070.046318\n",
       "37  3067  9894  3149.297410\n",
       "38   700  1500   653.839350\n",
       "39  1500  5000  1694.356769\n",
       "40  3110  8885  2849.331103\n",
       "41  2644  8813  2827.926174\n",
       "42  1378  3446  1232.367035\n",
       "43  2601  7881  2550.851249\n",
       "44   501  1164   553.949678\n",
       "45  1292  3401  1218.988954\n",
       "46  2125  6641  2182.210793\n",
       "47  1431  3329  1197.584024\n",
       "48  2260  6648  2184.291828\n",
       "49  1770  4800  1634.898631"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['Y_previsto'] = resultado_regressao.predict()\n",
    "dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "QTCRxEClcaj-"
   },
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "DU3ADw3fdeS1"
   },
   "source": [
    "# <font color=green>3 CORRELAÇÃO</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "_8zdNQvrcakA"
   },
   "source": [
    "## <font color=green>3.1 Covariância</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "E5xyRLiYcakB"
   },
   "source": [
    "A covariância, ou variância conjunta, é a medida do grau de interdependência (ou inter-relação) numérica entre duas variáveis. É definida da seguinte maneira:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "z66qdhEPcakB"
   },
   "source": [
    "### Covariância populacional\n",
    "\n",
    "# $$\\sigma_{xy} = \\frac 1n\\sum_{i=1}^{n}(X_i-\\mu_x)(Y_i-\\mu_y)$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Q5Z7pL4ncakB"
   },
   "source": [
    "### Covariância amostral\n",
    "\n",
    "# $$S_{xy} = \\frac 1{n-1}\\sum_{i=1}^{n}(X_i-\\bar{X})(Y_i-\\bar{Y})$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "jL1SrReTcakC"
   },
   "source": [
    "### Gerando uma amostra aleatória para facilitar o entendimento"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "ulha75EgcakC"
   },
   "outputs": [],
   "source": [
    "amostra = dados.query('Renda < 5000').sample(n = 20, random_state = 101)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ZssTPkXGcakD"
   },
   "source": [
    "### Obtendo a matriz de covariância"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 173
    },
    "colab_type": "code",
    "id": "hU6Ca7k3cakE",
    "outputId": "a0890859-c040-4d87-deed-7bcd6ce5c8b4"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Idade</th>\n",
       "      <th>Renda</th>\n",
       "      <th>Anos de Estudo</th>\n",
       "      <th>Altura</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Idade</th>\n",
       "      <td>134.410526</td>\n",
       "      <td>-4947.968421</td>\n",
       "      <td>-16.410526</td>\n",
       "      <td>0.016506</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Renda</th>\n",
       "      <td>-4947.968421</td>\n",
       "      <td>764668.326316</td>\n",
       "      <td>2209.021053</td>\n",
       "      <td>-7.138236</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Anos de Estudo</th>\n",
       "      <td>-16.410526</td>\n",
       "      <td>2209.021053</td>\n",
       "      <td>16.936842</td>\n",
       "      <td>0.111281</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Altura</th>\n",
       "      <td>0.016506</td>\n",
       "      <td>-7.138236</td>\n",
       "      <td>0.111281</td>\n",
       "      <td>0.003354</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                      Idade          Renda  Anos de Estudo    Altura\n",
       "Idade            134.410526   -4947.968421      -16.410526  0.016506\n",
       "Renda          -4947.968421  764668.326316     2209.021053 -7.138236\n",
       "Anos de Estudo   -16.410526    2209.021053       16.936842  0.111281\n",
       "Altura             0.016506      -7.138236        0.111281  0.003354"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amostra[['Idade', 'Renda', 'Anos de Estudo', 'Altura']].cov()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "dodkzGyBcakG"
   },
   "source": [
    "### Identificando as variâncias na diagonal principal da matriz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "iG3mpmBfcakH",
    "outputId": "cf9885ba-996a-4254-85b3-e190a31556f3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "134.4105263157895"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "amostra.Idade.var()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "9I8MVJAscakK"
   },
   "source": [
    "## <font color=green>3.2 Interpretação da Covariância</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "_H2Y3PVZcakK"
   },
   "source": [
    "Valor de $S_{xy}$ positivo indica uma associação linear positiva entre x e y, ou seja, à medida que o valor de x aumenta, o valor de y também aumenta. Neste caso, podemos ver na figura abaixo que os pontos que têm a maior influência sobre $S_{xy}$ devem estar nos quadrantes I e III.\n",
    "\n",
    "Se o valor de $S_{xy}$ for negativo temos um indicativo de associação linear negativa entre x e y, ou seja, à medida que x aumenta, o valor de y diminui. Neste caso, podemos ver na figura abaixo que os pontos que têm a maior influência sobre $S_{xy}$ devem estar nos quadrantes II e IV.\n",
    "\n",
    "Finalmente, se os pontos estiverem uniformemente distribuídos pelos quadrantes, o valor de $S_{xy}$ se aproximará de zero, indicando que não existe nenhuma associação linear entre x e y.\n",
    "\n",
    "<img src=\"https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img026.png\" width=50%>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "frhw7Uc8cakL"
   },
   "source": [
    "### Verificando a existência de uma associação linear negativa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 410
    },
    "colab_type": "code",
    "id": "Xb__N-ZlcakL",
    "outputId": "3600e9a4-5f77-4291-8640-fc0af0b4bb70"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.LineCollection at 0x239aedaf3c8>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 13560 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = amostra.Renda\n",
    "y = amostra.Idade\n",
    "\n",
    "ax = sns.scatterplot(x, y)\n",
    "ax.figure.set_size_inches(10, 6)\n",
    "ax.hlines(y = y.mean(), xmin = x.min(), xmax = x.max(), colors='black', linestyles='dashed')\n",
    "ax.vlines(x = x.mean(), ymin = y.min(), ymax = y.max(), colors='black', linestyles='dashed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "es2CobcCcakQ"
   },
   "source": [
    "### Verificando a existência de uma associação linear positiva"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 410
    },
    "colab_type": "code",
    "id": "eLc5JmYCcakQ",
    "outputId": "0b7b1dd4-82aa-4a50-9635-ad16f88371c6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.LineCollection at 0x239aed236a0>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 13540 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = amostra.Renda\n",
    "y = amostra['Anos de Estudo']\n",
    "\n",
    "ax = sns.scatterplot(x, y)\n",
    "ax.figure.set_size_inches(10, 6)\n",
    "ax.hlines(y = y.mean(), xmin = x.min(), xmax = x.max(), colors='black', linestyles='dashed')\n",
    "ax.vlines(x = x.mean(), ymin = y.min(), ymax = y.max(), colors='black', linestyles='dashed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "mYYlKQeHcakS"
   },
   "source": [
    "### Verificando a inexistência de uma associação linear entre as variáveis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 410
    },
    "colab_type": "code",
    "id": "R0S1AiBlcakS",
    "outputId": "c6897cb3-a3d5-44a3-8a5c-ec58dd257aad"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.LineCollection at 0x239af233978>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14968 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = amostra.Idade\n",
    "y = amostra.Altura\n",
    "\n",
    "ax = sns.scatterplot(x, y)\n",
    "ax.figure.set_size_inches(10, 6)\n",
    "ax.hlines(y = y.mean(), xmin = x.min(), xmax = x.max(), colors='black', linestyles='dashed')\n",
    "ax.vlines(x = x.mean(), ymin = y.min(), ymax = y.max(), colors='black', linestyles='dashed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "zgSmQYrHcakb"
   },
   "source": [
    "### <font color='red'>Observação Importante:</font>\n",
    "> Pelo que foi apresentado pode parecer que valores elevados, tanto positivos quanto negativos, para a covariância indicam relações lineares fortes entre as variáveis envolvidas. No entanto, um problema quando se usa a covariância como uma medida da intensidade da relação linear é que o valor da covariância depende das unidades de medida para x e y.\n",
    "> \n",
    "> Uma medida da relação entre duas variáveis que não é afetada pelas unidades de medida para x e y é o coeficiente de correlação que veremos no próximo tópico."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "oPAxUkyQcakb"
   },
   "source": [
    "## <font color=green>3.3 Coeficiente de correlação de Pearson</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "6TYCLh0Hcakf"
   },
   "source": [
    "É obtido dividindo-se a covariância da população ou amostra pelo produto do desvio padrão populacional ou amostral de x pelo desvio padrão populacional ou amostral de y.\n",
    "\n",
    "O coeficiente de correlação varia de -1 a +1. Valores que se aproximam de -1 ou +1 indicam uma relação linear forte. Quanto mais próxima a correlação estiver de zero, mais fraca será a relação.\n",
    "\n",
    "Um ponto importante é que o coeficiente de correlação é uma medida de associação linear e não necessariamente de causação. Uma correlação alta entre duas variáveis não significa, necessariamente, que variações em uma delas provocará alterações na outra."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OgecrWGdcakf"
   },
   "source": [
    "### Coeficiente de correlação de Pearson - dados populacionais\n",
    "\n",
    "# $$\\rho_{xy} = \\frac{\\sigma_{xy}}{\\sigma_{x}\\sigma_{y}}$$\n",
    "\n",
    "### Coeficiente de correlação de Pearson - dados amostrais\n",
    "\n",
    "# $$r_{xy} = \\frac{s_{xy}}{s_{x}s_{y}}$$\n",
    "\n",
    "Onde\n",
    "\n",
    "$\\sigma_{xy}$ = covariância populacional entre x e y\n",
    "\n",
    "$s_{xy}$ = covariância amostral entre x e y\n",
    "\n",
    "$\\sigma_{x}$ e $\\sigma_{y}$ = desvios padrão populacionais de x e y, respectivamente\n",
    "\n",
    "$s_{x}$ e $s_{y}$ = desvios padrão amostrais de x e y, respectivamente"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "jPk2i26acakg"
   },
   "source": [
    "### Obtendo $s_{xy}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 111
    },
    "colab_type": "code",
    "id": "lc6MNkgQcakg",
    "outputId": "02da9ed9-ebf3-4e90-b546-c300d7503b65"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Altura</th>\n",
       "      <th>Renda</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Altura</th>\n",
       "      <td>0.006355</td>\n",
       "      <td>-1.078964e+00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Renda</th>\n",
       "      <td>-1.078964</td>\n",
       "      <td>1.104491e+07</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Altura         Renda\n",
       "Altura  0.006355 -1.078964e+00\n",
       "Renda  -1.078964  1.104491e+07"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_xy = dados[['Altura', 'Renda']].cov()\n",
    "s_xy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "_8KNoxdhcakj",
    "outputId": "57108a7a-db55-49a3-e554-2b51c94f58b7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1.0789639314252724"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_xy= s_xy.Altura.loc['Renda']\n",
    "s_xy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "sDybJtCMcakl"
   },
   "source": [
    "### Obtendo $s_x$ e $s_y$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "LTEEbMIzcakl"
   },
   "outputs": [],
   "source": [
    "s_x = dados.Altura.std()\n",
    "s_y = dados.Renda.std()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "VVpQ5QcCcakm"
   },
   "source": [
    "### Obtendo o coeficiente de correlação $r_{xy}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "TuPWxVgccakq",
    "outputId": "76d8e637-5a9c-4362-f069-fd7ce8a5fe03"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-0.004072608143399515"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r_xy = s_xy / ( s_x * s_y)\n",
    "r_xy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "miyTUNqpcakr"
   },
   "source": [
    "### Obtendo uma matriz de correlação com o Pandas"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 111
    },
    "colab_type": "code",
    "id": "SjQUE6wVcakt",
    "outputId": "4ccd6e52-c99a-4d79-d582-17ce1dc07b53"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Altura</th>\n",
       "      <th>Renda</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Altura</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>-0.004073</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Renda</th>\n",
       "      <td>-0.004073</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Altura     Renda\n",
       "Altura  1.000000 -0.004073\n",
       "Renda  -0.004073  1.000000"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dados[['Altura', 'Renda']].corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 111
    },
    "colab_type": "code",
    "id": "r-Iasce4cakv",
    "outputId": "83ae245a-9895-4fec-f37c-627c77250369"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Y</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.984835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>X</th>\n",
       "      <td>0.984835</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Y         X\n",
       "Y  1.000000  0.984835\n",
       "X  0.984835  1.000000"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset[['Y', 'X']].corr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 410
    },
    "colab_type": "code",
    "id": "0Zddu4ajcakx",
    "outputId": "283f8695-7440-439b-f7ad-0da9c0514f2e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.LineCollection at 0x239af28c128>"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15376 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = amostra.Renda\n",
    "y = amostra.Altura\n",
    "\n",
    "ax = sns.scatterplot(x, y)\n",
    "ax.figure.set_size_inches(10, 6)\n",
    "ax.hlines(y = y.mean(), xmin = x.min(), xmax = x.max(), colors='black', linestyles='dashed')\n",
    "ax.vlines(x = x.mean(), ymin = y.min(), ymax = y.max(), colors='black', linestyles='dashed')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 410
    },
    "colab_type": "code",
    "id": "iMNkYpnhcak0",
    "outputId": "0ac13998-74b4-4da5-e939-54dcc577a52e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.collections.LineCollection at 0x239af30bac8>"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 14200 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "x = dataset.Y\n",
    "y = dataset.X\n",
    "\n",
    "ax = sns.scatterplot(x, y)\n",
    "ax.figure.set_size_inches(10, 6)\n",
    "ax.hlines(y = y.mean(), xmin = x.min(), xmax = x.max(), colors='black', linestyles='dashed')\n",
    "ax.vlines(x = x.mean(), ymin = y.min(), ymax = y.max(), colors='black', linestyles='dashed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "HSF3lXxzcak3"
   },
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "glWc43l2hLnL"
   },
   "source": [
    "# <font color=green>4 REGRESSÃO LINEAR</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "T0nLJKtLcak8"
   },
   "source": [
    "A análise de regressão diz respeito ao estudo da dependência de uma variável (a variável dependente) em relação a uma ou mais variáveis, as variáveis explicativas ou independentes, visando estimar e/ou prever o valor médio da primeira em termos dos valores conhecidos ou fixados das segundas.\n",
    "\n",
    "#### Terminologia\n",
    "\n",
    "> A variável que é prevista é conhecida como variável dependente (*Y*).\n",
    "\n",
    "> A variável utilizada para prever o valor da variável dependente é conhecida como variável independente (*X*).\n",
    "\n",
    "Em nosso treinamento vamos abordar o tipo mais simples de análise de regressão que considera uma variável dependente e apenas uma variável independente onde a relação entre as variáveis se aproxima de uma linha reta."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "jIXc6LA7cak9"
   },
   "source": [
    "## <font color=green>4.1 Regressão linear simples</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "bIhWKDuK75k2"
   },
   "source": [
    "### Função consumo\n",
    "\n",
    "Um economista famoso do passado concluiu em um de seus estudos que os indivíduos tendem a aumentar o seu consumo conforme sua renda aumenta. Logicamente esta teoria ganhou um pouco mais de complexidade, mas vamos utilizar sua forma mais simples para entender o procedimento de análise de regressão com a seguninte formulação:\n",
    "\n",
    "# $$Y_i = \\beta_1 + \\beta_2X_i$$\n",
    "\n",
    "Onde\n",
    "\n",
    "- $Y_i$ é o gasto das famílias ou consumo das famílias\n",
    "\n",
    "- $X_i$ é a renda disponível\n",
    "\n",
    "- $\\beta_1$ é conhecido como intercepto (no modelo teórico acima é conhecido como consumo autônomo, ou seja, o consumo quando o rendimento é zero)\n",
    "\n",
    "- $\\beta_2$ é o coefiente angular (no modelo teórico é a propensão marginal a consumir)\n",
    "\n",
    "Em uma análise de regressão linear simples, o interesse está em estimar a função de regressão populacional como a apresentada acima, ou seja, estimar os valores dos parâmetros $\\beta_1$ e $\\beta_2$ com base nas observações de $Y$ e $X$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "1kkZ2NxPcak-"
   },
   "source": [
    "### Carregando o dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "-um71iXqcak-"
   },
   "outputs": [],
   "source": [
    "dataset = {\n",
    "    'Y': [3011, 1305, 1879, 2654, 2849, 1068, 2892, 2543, 3074, 849, 2184, 2943, 1357, 2755, 2163, 3099, 1600, 353, 1778, 740, 2129, 3302, 2412, 2683, 2515, 2395, 2292, 1000, 600, 1864, 3027, 1978, 2791, 1982, 900, 1964, 1247, 3067, 700, 1500, 3110, 2644, 1378, 2601, 501, 1292, 2125, 1431, 2260, 1770],\n",
    "    'X': [9714, 3728, 6062, 8845, 8378, 3338, 8507, 7947, 9915, 1632, 6825, 8918, 4100, 9184, 6180, 9997, 4500, 1069, 5925, 2466, 6083, 9712, 7780, 8383, 7185, 7483, 7640, 2100, 2000, 6012, 8902, 5345, 8210, 5662, 2700, 6546, 2900, 9894, 1500, 5000, 8885, 8813, 3446, 7881, 1164, 3401, 6641, 3329, 6648, 4800]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "eVisdrGdcak_",
    "outputId": "4987998b-7679-42f3-dc9b-28b3b1fa7e24",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X\n",
       "0  3011  9714\n",
       "1  1305  3728\n",
       "2  1879  6062\n",
       "3  2654  8845\n",
       "4  2849  8378"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset = pd.DataFrame(dataset)\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "FnwGB3f1calB"
   },
   "source": [
    "### Identificando a relação entre as variáveis\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.lmplot.html\n",
    "\n",
    "Plota a reta de regressão entre duas variáveis juntamente com a dispersão entre elas."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 474
    },
    "colab_type": "code",
    "id": "DI16LyC7calB",
    "outputId": "5576d35b-6e22-4c05-d754-6285edeb9436"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<seaborn.axisgrid.FacetGrid at 0x239af3647f0>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 45336 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.lmplot(x=\"X\", y=\"Y\", data=dataset)\n",
    "ax.fig.set_size_inches(12, 6)\n",
    "ax.fig.suptitle('Reta de Regressão - Gasto X Renda', fontsize=16, y=1.02)\n",
    "ax.set_xlabels(\"Renda das Famílias\", fontsize=14)\n",
    "ax.set_ylabels(\"Gasto das Famílias\", fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5_PIOCAv75k9"
   },
   "source": [
    "### Matriz de correlação"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 111
    },
    "colab_type": "code",
    "id": "R7Zr6nuy75k-",
    "outputId": "4fa8db39-0b93-42c4-8c61-315102699102"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Y</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>0.984835</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>X</th>\n",
       "      <td>0.984835</td>\n",
       "      <td>1.000000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          Y         X\n",
       "Y  1.000000  0.984835\n",
       "X  0.984835  1.000000"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.corr()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "lzD2VpXccak9"
   },
   "source": [
    "### Função de regressão populacional\n",
    "\n",
    "A equação formulada acima é conhecida como **função de regressão populacional (FRP)** e em alguns livros é também representada da seguinte forma:\n",
    "\n",
    "# $$E(Y|X_i) = \\beta_1 + \\beta_2X_i$$\n",
    "\n",
    "Ela afirma que o valor esperado da distribuição de $Y$, dado $X_i$, tem uma relação funcional com $X_i$, isto é, a resposta média de $Y$ varia com $X$. Os coeficientes $\\beta_1$ e $\\beta_2$ são conhecidos como coeficientes de regressão e também como intercepto e coeficiente angular, respectivamente.\n",
    "\n",
    "<img src=\"https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img027.png\" width=50%>\n",
    "\n",
    "O gráfico de dispersão acima deixa claro que com o aumento da renda familiar, em média, as despesas de consumo das famílias aumentam, mas não no mesmo nível. O que podemos dizer é que para um nível de renda $X_i$ as despesas médias de consumo de uma\n",
    "família agrupam-se em torno do consumo médio de todas as famílias deste nível $X_i$, isto é, em torno de sua esperança condicional $E(Y|X_i)$. Portanto, podemos expressar o desvio individual de $Y_i$ em torno de seu valor esperado da seguinte forma:\n",
    "\n",
    "# $$u_i = Y_i - E(Y|X_i)$$\n",
    "\n",
    "ou\n",
    "\n",
    "# $$Y_i = E(Y|X_i) + u_i$$\n",
    "\n",
    "onde o desvio $u_i$ é uma variável aleatória não-observável que assume valores positivos ou negativos. Esta variável é também conhecida como termo de erro estocástico.\n",
    "\n",
    "Observe que a podemos representar a FRP em sua forma estocástica da seguinte maneira:\n",
    "\n",
    "# $$Y_i = \\beta_1 + \\beta_2X_i + u_i$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "yOR8pKHkcak9"
   },
   "source": [
    "## <font color=green>4.2 O método de mínimos quadrados</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "M-iKUvPlcak-"
   },
   "source": [
    "Considere a seguinte **função de regressão da população (FRP)** para duas variáveis:\n",
    "\n",
    "# $$Y_i = \\beta_1 + \\beta_2X_i + u_i$$\n",
    "\n",
    "Como a **FRP** não pode ser obtida de forma direta, precisamos estimá-la através da **função de regressão amostral (FRA)**.\n",
    "\n",
    "# $$Y_i = \\hat{\\beta}_1 + \\hat{\\beta}_2X_i + \\hat{u}_i$$\n",
    "# $$Y_i = \\hat{Y}_i + \\hat{u}_i$$\n",
    "\n",
    "onde $\\hat{Y}_i$ é o valor estimado de $Y_i$.\n",
    "\n",
    "A partir da equação acima podemos expressar o erro da seguinte maneira:\n",
    "\n",
    "# $$\\hat{u}_i = Y_i - \\hat{Y}_i$$\n",
    "# $$\\hat{u}_i = Y_i - \\hat{\\beta}_1 - \\hat{\\beta}_2X_i$$\n",
    "\n",
    "A ideia é determinar **FRA** de forma que fique o mais próximo possível do valor observado de $Y$. Intuitivamente uma forma interessante de fazer isso seria determinar **FRA** de maneira que a soma dos resíduos seja a menor possível.\n",
    "\n",
    "# $$\\sum\\hat{u}_i = \\sum{(Y_i - \\hat{Y}_i)}$$\n",
    "\n",
    "Avaliando a figura abaixo se pode verificar que talvez o critério de minimizar a soma dos resíduos não seja a melhor abordagem para solucionar o problema.\n",
    "\n",
    "<img src=\"https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img015.png\" width=50%>\n",
    "\n",
    "O critério de minimização da soma dos resíduos assume que todos os resíduos tem o mesmo peso no somatório, o que nem sempre se comprova. Em nosso exemplo os resíduos $\\hat{u}_1$ e $\\hat{u}_4$ encontram-se bem mais afastados da **FRA** que $\\hat{u}_2$ e $\\hat{u}_3$. Isso acarreta na possibilidade de que a soma dos $\\hat{u}_i$'s seja bastante pequena e até mesmo nula, mesmo que os resíduos estejam muito dispersos em relação a reta de regressão.\n",
    "\n",
    "Para evitar tal problema adotou-se o critério de minimização da soma dos quadrados dos resíduos que é conhecido como **Método de Mínimos Quadrados**.\n",
    "\n",
    "# $$\\sum\\hat{u}_i^2 = \\sum{(Y_i - \\hat{Y}_i)^2}$$\n",
    "# $$\\sum\\hat{u}_i^2 = \\sum{(Y_i - \\hat{\\beta}_1 - \\hat{\\beta}_2X_i)^2}$$\n",
    "\n",
    "Após um procedimento de diferenciação, algumas simplificações e manipulações algébricas obtemos os **estimadores de mínimos quadrados**.\n",
    "\n",
    "# $$\n",
    "\\begin{equation}\n",
    "\\boxed{\\hat{\\beta}_2 = \\frac{n\\sum{X_iY_i} - \\sum{X_i}\\sum{Y_i}}{n\\sum{X_i^2} - (\\sum{X_i})^2}}\n",
    "\\end{equation}\n",
    "$$\n",
    "\n",
    "# $$\n",
    "\\begin{equation}\n",
    "\\boxed{\n",
    "\\begin{eqnarray}\n",
    "\\hat{\\beta}_1 &=& \\frac{\\sum{X_i^2}\\sum{Y_i} - \\sum{X_i}\\sum{X_iY_i}}{n\\sum{X_i^2} - (\\sum{X_i})^2}\\\\\n",
    "&=& \\bar{Y} - \\hat{\\beta}_2\\bar{X}\n",
    "\\end{eqnarray}\n",
    "}\n",
    "\\end{equation}\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "bIsppET0cak-"
   },
   "source": [
    "## <font color=green>4.3 Estimadores de mínimos quadrados ordinários</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "HTFJyCNCcalE"
   },
   "source": [
    "### Obter $n$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "ZcHOhSNRcalE",
    "outputId": "8a21ac4b-34d6-4139-cef4-6a3a79dd240f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n = len(dataset)\n",
    "n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "2R8QzULvcalF"
   },
   "source": [
    "### Obter $\\sum{Y}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "wfY4FHLgcalG",
    "outputId": "7034961e-e69c-424b-8a81-ef3443856811"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "100556"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_Y = dataset.Y.sum()\n",
    "SOMA_Y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "dpWo8F-fcalI"
   },
   "source": [
    "### Obter $\\sum{X}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "vgRIhNl9calI",
    "outputId": "58ef8ded-cbde-440a-a2ed-4f6c1e1b49a3"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "303275"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_X = dataset.X.sum()\n",
    "SOMA_X"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ZOrXXyygcalJ"
   },
   "source": [
    "### Obter $\\sum{X^2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "boXBQUIJcalK",
    "outputId": "4c12972c-6ba0-43e7-b6f2-92692723a752",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>X2</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>94361796</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>13897984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>36747844</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>78234025</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>70190884</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X        X2\n",
       "0  3011  9714  94361796\n",
       "1  1305  3728  13897984\n",
       "2  1879  6062  36747844\n",
       "3  2654  8845  78234025\n",
       "4  2849  8378  70190884"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['X2'] = dataset.X ** 2\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "ce-I43picalL",
    "outputId": "da5633b5-6ec6-4b44-e4cf-c7dabe6f351d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2198628939"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_X2 = dataset.X2.sum()\n",
    "SOMA_X2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "TAiTZKfOcalM",
    "outputId": "c4b00b99-d659-4306-d3e1-1177e24bd193"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2198628939"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_X2 = dataset.X.apply(lambda x: x**2).sum()\n",
    "SOMA_X2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ls1COzpxcalN"
   },
   "source": [
    "### Obter $\\sum{Y^2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "gYc_poUmcalN",
    "outputId": "b8a0cc3e-6c2f-41cf-aaba-6e3ca3246c74"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "234954332"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_Y2 = dataset.Y.apply(lambda y: y**2).sum()\n",
    "SOMA_Y2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "UsCtTk3pcalP"
   },
   "source": [
    "### Obter $\\sum{XY}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "Mw4pfCAEcalP",
    "outputId": "b2542e6c-a49d-4f55-bec6-b1f12abfad21",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>X2</th>\n",
       "      <th>XY</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>94361796</td>\n",
       "      <td>29248854</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>13897984</td>\n",
       "      <td>4865040</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>36747844</td>\n",
       "      <td>11390498</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>78234025</td>\n",
       "      <td>23474630</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>70190884</td>\n",
       "      <td>23868922</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X        X2        XY\n",
       "0  3011  9714  94361796  29248854\n",
       "1  1305  3728  13897984   4865040\n",
       "2  1879  6062  36747844  11390498\n",
       "3  2654  8845  78234025  23474630\n",
       "4  2849  8378  70190884  23868922"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['XY'] = dataset.X * dataset.Y\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "s1NNuoM4calQ",
    "outputId": "cc080b68-6635-47c4-bc48-157801759ea6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "716683794"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_XY = dataset.XY.sum()\n",
    "SOMA_XY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "fwAWf3SScalR",
    "outputId": "4dff48a5-a69a-47b4-e7d2-09a0ea60756c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "716683794"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_XY = dataset.apply(lambda data: data.X * data.Y, axis = 1).sum()\n",
    "SOMA_XY"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "_6-qQG_ucalS",
    "outputId": "f4eaf5ce-1f26-4812-9455-17e5a18f9c9e"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X\n",
       "0  3011  9714\n",
       "1  1305  3728\n",
       "2  1879  6062\n",
       "3  2654  8845\n",
       "4  2849  8378"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.drop(['X2', 'XY'], axis = 1, inplace = True)\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "EX-oAn2rcalU"
   },
   "source": [
    "### Obter $\\hat{\\beta}_2$\n",
    "\n",
    "# $$\\hat{\\beta}_2 = \\frac{n\\sum{X_iY_i} - \\sum{X_i}\\sum{Y_i}}{n\\sum{X_i^2} - (\\sum{X_i})^2}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "UY-nWn7LcalU",
    "outputId": "de545732-ffe4-4095-f42d-0c3bf6680fbb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.29729069099372424"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "numerador = n * SOMA_XY - SOMA_X * SOMA_Y\n",
    "denominador = n * SOMA_X2 - (SOMA_X)**2\n",
    "beta_2 = numerador / denominador\n",
    "beta_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "v4QCFagscalW"
   },
   "source": [
    "### Obter $\\hat{\\beta}_1$\n",
    "\n",
    "# $$\n",
    "\\begin{eqnarray}\n",
    "\\hat{\\beta}_1 &=& \\frac{\\sum{X_i^2}\\sum{Y_i} - \\sum{X_i}\\sum{X_iY_i}}{n\\sum{X_i^2} - (\\sum{X_i})^2}\\\\\n",
    "&=& \\bar{Y} - \\hat{\\beta}_2\\bar{X}\n",
    "\\end{eqnarray}\n",
    "$$ "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "-Gjc1ra5calW",
    "outputId": "a469f5b7-eb58-4891-e618-6959ad1a6d39"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "207.90331377756547"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "beta_1 = dataset.Y.mean() - beta_2 * dataset.X.mean()\n",
    "beta_1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "SjcncMd1calY"
   },
   "source": [
    "### Obtendo a estimativa dos parâmetros com o StatsModels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Ud6O4f-qcalY"
   },
   "source": [
    "### Importando a biblioteca\n",
    "https://www.statsmodels.org/stable/index.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "QnYZ2T-7calZ"
   },
   "outputs": [],
   "source": [
    "import statsmodels.api as sm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 73
    },
    "colab_type": "code",
    "id": "sUlIFQh-calb",
    "outputId": "6278704b-b319-4d83-97da-74b5eccd66ec"
   },
   "outputs": [],
   "source": [
    "Y = dataset.Y\n",
    "X = sm.add_constant(dataset.X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 125
    },
    "colab_type": "code",
    "id": "6Npgg9NNcald",
    "outputId": "ddb0fe1f-f4a5-47c6-f957-2f3d23b86765",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0    3011\n",
       "1    1305\n",
       "2    1879\n",
       "3    2654\n",
       "4    2849\n",
       "Name: Y, dtype: int64"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "eDgJXjCAcale",
    "outputId": "f306e7c0-5af3-4f45-cfe7-59b948470d80",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>const</th>\n",
       "      <th>X</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.0</td>\n",
       "      <td>9714</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.0</td>\n",
       "      <td>3728</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.0</td>\n",
       "      <td>6062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1.0</td>\n",
       "      <td>8845</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1.0</td>\n",
       "      <td>8378</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   const     X\n",
       "0    1.0  9714\n",
       "1    1.0  3728\n",
       "2    1.0  6062\n",
       "3    1.0  8845\n",
       "4    1.0  8378"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OZs1ykL_calh"
   },
   "source": [
    "### Estimando o modelo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "CGGritStcalh"
   },
   "outputs": [],
   "source": [
    "resultado_regressao = sm.OLS(Y, X, missing='drop').fit()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "pyVnbv7vcali"
   },
   "source": [
    "### Visualizando os parâmetros estimados"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "Ye2sF_ygcalj",
    "outputId": "d0e2d875-011a-4b71-8ff8-de0ace39e866"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "207.90331377756547"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "beta_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "5RGcqao9call",
    "outputId": "f39c311b-499b-4317-d0ed-5cb7c488739a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.29729069099372424"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "beta_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 71
    },
    "colab_type": "code",
    "id": "RUScqQ1dcalm",
    "outputId": "3a6a0609-1ca3-45c1-9a7d-15108f072def"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "const    207.903314\n",
       "X          0.297291\n",
       "dtype: float64"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.params"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "9geqX6FScaln",
    "outputId": "44cdde13-3662-49c7-84e9-ef4159ef9ec6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "207.9033137775661"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "beta_1 = resultado_regressao.params[0]\n",
    "beta_1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "U238WwEVcalo",
    "outputId": "f305705c-639c-417b-e930-f5adaaccd216"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.29729069099372407"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "beta_2 = resultado_regressao.params[1]\n",
    "beta_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "POcFkp72calp"
   },
   "source": [
    "### Intervalo de confiança para os parâmetros estimados"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 111
    },
    "colab_type": "code",
    "id": "-VrPhUV8calp",
    "outputId": "48df0d6d-8ef8-4a6f-9f14-d45e0b8ab341"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>0</th>\n",
       "      <th>1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>const</th>\n",
       "      <td>107.116864</td>\n",
       "      <td>308.689763</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>X</th>\n",
       "      <td>0.282092</td>\n",
       "      <td>0.312490</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                0           1\n",
       "const  107.116864  308.689763\n",
       "X        0.282092    0.312490"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.conf_int(alpha=0.05)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Je8U449lcalq"
   },
   "source": [
    "## <font color=green>4.4 Obtendo previsões</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "PNPQFFsrcalq"
   },
   "source": [
    "# $$\\hat{Y}_i = 207,9033 + 0,2973X_i$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "qNpJclcrcalr"
   },
   "source": [
    "### Previsões dentro da amostra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 359
    },
    "colab_type": "code",
    "id": "fqOX7ssAcalr",
    "outputId": "aa8eafbb-24ad-44e5-c85a-47f9c31e41b3",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1068</td>\n",
       "      <td>3338</td>\n",
       "      <td>1200.259640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2892</td>\n",
       "      <td>8507</td>\n",
       "      <td>2736.955222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2543</td>\n",
       "      <td>7947</td>\n",
       "      <td>2570.472435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3074</td>\n",
       "      <td>9915</td>\n",
       "      <td>3155.540515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>849</td>\n",
       "      <td>1632</td>\n",
       "      <td>693.081721</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X   Y_previsto\n",
       "0  3011  9714  3095.785086\n",
       "1  1305  3728  1316.203010\n",
       "2  1879  6062  2010.079483\n",
       "3  2654  8845  2837.439476\n",
       "4  2849  8378  2698.604723\n",
       "5  1068  3338  1200.259640\n",
       "6  2892  8507  2736.955222\n",
       "7  2543  7947  2570.472435\n",
       "8  3074  9915  3155.540515\n",
       "9   849  1632   693.081721"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['Y_previsto'] = beta_1 + beta_2 * dataset.X\n",
    "dataset.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "hRRoxxAwcals"
   },
   "source": [
    "### Utilizando o statsmodels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "qLhv0yTgcals",
    "outputId": "43f540ac-006f-4e1c-adab-8a9e8844cef5",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "      <th>Y_previsto_statsmodels</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "      <td>3095.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "      <td>1316.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "      <td>2010.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "      <td>2837.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "      <td>2698.604723</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X   Y_previsto  Y_previsto_statsmodels\n",
       "0  3011  9714  3095.785086             3095.785086\n",
       "1  1305  3728  1316.203010             1316.203010\n",
       "2  1879  6062  2010.079483             2010.079483\n",
       "3  2654  8845  2837.439476             2837.439476\n",
       "4  2849  8378  2698.604723             2698.604723"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['Y_previsto_statsmodels'] = resultado_regressao.predict()\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1599
    },
    "colab_type": "code",
    "id": "FctIV05Kcalu",
    "outputId": "3482ccfd-9b1d-4dd4-d2de-cdc3926cd28b",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1068</td>\n",
       "      <td>3338</td>\n",
       "      <td>1200.259640</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>2892</td>\n",
       "      <td>8507</td>\n",
       "      <td>2736.955222</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>2543</td>\n",
       "      <td>7947</td>\n",
       "      <td>2570.472435</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>3074</td>\n",
       "      <td>9915</td>\n",
       "      <td>3155.540515</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>849</td>\n",
       "      <td>1632</td>\n",
       "      <td>693.081721</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>2184</td>\n",
       "      <td>6825</td>\n",
       "      <td>2236.912280</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>2943</td>\n",
       "      <td>8918</td>\n",
       "      <td>2859.141696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>1357</td>\n",
       "      <td>4100</td>\n",
       "      <td>1426.795147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>2755</td>\n",
       "      <td>9184</td>\n",
       "      <td>2938.221020</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>2163</td>\n",
       "      <td>6180</td>\n",
       "      <td>2045.159784</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>3099</td>\n",
       "      <td>9997</td>\n",
       "      <td>3179.918352</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>1600</td>\n",
       "      <td>4500</td>\n",
       "      <td>1545.711423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>353</td>\n",
       "      <td>1069</td>\n",
       "      <td>525.707062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>1778</td>\n",
       "      <td>5925</td>\n",
       "      <td>1969.350658</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>740</td>\n",
       "      <td>2466</td>\n",
       "      <td>941.022158</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>2129</td>\n",
       "      <td>6083</td>\n",
       "      <td>2016.322587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>3302</td>\n",
       "      <td>9712</td>\n",
       "      <td>3095.190505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>2412</td>\n",
       "      <td>7780</td>\n",
       "      <td>2520.824890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>2683</td>\n",
       "      <td>8383</td>\n",
       "      <td>2700.091176</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>2515</td>\n",
       "      <td>7185</td>\n",
       "      <td>2343.936929</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>2395</td>\n",
       "      <td>7483</td>\n",
       "      <td>2432.529554</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>2292</td>\n",
       "      <td>7640</td>\n",
       "      <td>2479.204193</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>1000</td>\n",
       "      <td>2100</td>\n",
       "      <td>832.213765</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>600</td>\n",
       "      <td>2000</td>\n",
       "      <td>802.484696</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>1864</td>\n",
       "      <td>6012</td>\n",
       "      <td>1995.214948</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>3027</td>\n",
       "      <td>8902</td>\n",
       "      <td>2854.385045</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>1978</td>\n",
       "      <td>5345</td>\n",
       "      <td>1796.922057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>2791</td>\n",
       "      <td>8210</td>\n",
       "      <td>2648.659887</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>33</th>\n",
       "      <td>1982</td>\n",
       "      <td>5662</td>\n",
       "      <td>1891.163206</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>34</th>\n",
       "      <td>900</td>\n",
       "      <td>2700</td>\n",
       "      <td>1010.588179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>35</th>\n",
       "      <td>1964</td>\n",
       "      <td>6546</td>\n",
       "      <td>2153.968177</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>36</th>\n",
       "      <td>1247</td>\n",
       "      <td>2900</td>\n",
       "      <td>1070.046318</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>37</th>\n",
       "      <td>3067</td>\n",
       "      <td>9894</td>\n",
       "      <td>3149.297410</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>700</td>\n",
       "      <td>1500</td>\n",
       "      <td>653.839350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>39</th>\n",
       "      <td>1500</td>\n",
       "      <td>5000</td>\n",
       "      <td>1694.356769</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>3110</td>\n",
       "      <td>8885</td>\n",
       "      <td>2849.331103</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>41</th>\n",
       "      <td>2644</td>\n",
       "      <td>8813</td>\n",
       "      <td>2827.926174</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>42</th>\n",
       "      <td>1378</td>\n",
       "      <td>3446</td>\n",
       "      <td>1232.367035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>43</th>\n",
       "      <td>2601</td>\n",
       "      <td>7881</td>\n",
       "      <td>2550.851249</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>44</th>\n",
       "      <td>501</td>\n",
       "      <td>1164</td>\n",
       "      <td>553.949678</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>1292</td>\n",
       "      <td>3401</td>\n",
       "      <td>1218.988954</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>46</th>\n",
       "      <td>2125</td>\n",
       "      <td>6641</td>\n",
       "      <td>2182.210793</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>47</th>\n",
       "      <td>1431</td>\n",
       "      <td>3329</td>\n",
       "      <td>1197.584024</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>48</th>\n",
       "      <td>2260</td>\n",
       "      <td>6648</td>\n",
       "      <td>2184.291828</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>49</th>\n",
       "      <td>1770</td>\n",
       "      <td>4800</td>\n",
       "      <td>1634.898631</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       Y     X   Y_previsto\n",
       "0   3011  9714  3095.785086\n",
       "1   1305  3728  1316.203010\n",
       "2   1879  6062  2010.079483\n",
       "3   2654  8845  2837.439476\n",
       "4   2849  8378  2698.604723\n",
       "5   1068  3338  1200.259640\n",
       "6   2892  8507  2736.955222\n",
       "7   2543  7947  2570.472435\n",
       "8   3074  9915  3155.540515\n",
       "9    849  1632   693.081721\n",
       "10  2184  6825  2236.912280\n",
       "11  2943  8918  2859.141696\n",
       "12  1357  4100  1426.795147\n",
       "13  2755  9184  2938.221020\n",
       "14  2163  6180  2045.159784\n",
       "15  3099  9997  3179.918352\n",
       "16  1600  4500  1545.711423\n",
       "17   353  1069   525.707062\n",
       "18  1778  5925  1969.350658\n",
       "19   740  2466   941.022158\n",
       "20  2129  6083  2016.322587\n",
       "21  3302  9712  3095.190505\n",
       "22  2412  7780  2520.824890\n",
       "23  2683  8383  2700.091176\n",
       "24  2515  7185  2343.936929\n",
       "25  2395  7483  2432.529554\n",
       "26  2292  7640  2479.204193\n",
       "27  1000  2100   832.213765\n",
       "28   600  2000   802.484696\n",
       "29  1864  6012  1995.214948\n",
       "30  3027  8902  2854.385045\n",
       "31  1978  5345  1796.922057\n",
       "32  2791  8210  2648.659887\n",
       "33  1982  5662  1891.163206\n",
       "34   900  2700  1010.588179\n",
       "35  1964  6546  2153.968177\n",
       "36  1247  2900  1070.046318\n",
       "37  3067  9894  3149.297410\n",
       "38   700  1500   653.839350\n",
       "39  1500  5000  1694.356769\n",
       "40  3110  8885  2849.331103\n",
       "41  2644  8813  2827.926174\n",
       "42  1378  3446  1232.367035\n",
       "43  2601  7881  2550.851249\n",
       "44   501  1164   553.949678\n",
       "45  1292  3401  1218.988954\n",
       "46  2125  6641  2182.210793\n",
       "47  1431  3329  1197.584024\n",
       "48  2260  6648  2184.291828\n",
       "49  1770  4800  1634.898631"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.drop(['Y_previsto_statsmodels'], axis = 1, inplace = True)\n",
    "dataset"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OZBs0luwcalv"
   },
   "source": [
    "### Estimando o 'Gasto das Famílias' fora da amostra"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "4gXSqx41calv"
   },
   "outputs": [],
   "source": [
    "def prever(x):\n",
    "  return beta_1 + beta_2 * x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "RyBwrPlQcalx",
    "outputId": "235af62b-8be5-4cb6-8b65-c3f5b1f5bb7e"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2440.5564031404338"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "prever(7510)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "3P0ZtB-Qcalz"
   },
   "source": [
    "### Estimando o 'Gasto das Famílias' fora da amostra via StatsModels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "DpDu_yiecal0",
    "outputId": "85d74e32-cb57-49d8-b582-cfab3d4cf210"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2440.5564031404338"
      ]
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.predict([1, 7510])[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "NfEjm65Gcal2"
   },
   "source": [
    "## <font color=green>4.5 Resíduos</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "RVAQCkvH75mH"
   },
   "source": [
    "Como vimos anteriormente, o resíduo da i-ésima observação é a diferença entre o valor observado de nossa variável dependente ($Y_i$) e o valor estimado da variável dependente ($\\hat{Y}_i$)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "EAGrrY7xcal3"
   },
   "source": [
    "# $$\\hat{u}_i = Y_i - \\hat{Y}_i$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "QVekZtNU75mH"
   },
   "source": [
    "Em outras palavras, $\\hat{u}_i$ é o erro obtido ao se utilizar a equação de regressão estimada para prever o valor da variável dependente."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "oiB2JiOLcal3",
    "outputId": "64f5f10e-4462-4336-bbbf-8eba676f234d",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "      <th>u</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "      <td>-84.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "      <td>-11.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "      <td>-131.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "      <td>-183.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "      <td>150.395277</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X   Y_previsto           u\n",
       "0  3011  9714  3095.785086  -84.785086\n",
       "1  1305  3728  1316.203010  -11.203010\n",
       "2  1879  6062  2010.079483 -131.079483\n",
       "3  2654  8845  2837.439476 -183.439476\n",
       "4  2849  8378  2698.604723  150.395277"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['u'] = dataset.Y - dataset.Y_previsto\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "_rtZhoU8cal5",
    "outputId": "c608100a-fb45-4765-e581-b37cf315b78e"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "      <th>u</th>\n",
       "      <th>Residuos</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "      <td>-84.785086</td>\n",
       "      <td>-84.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "      <td>-11.203010</td>\n",
       "      <td>-11.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "      <td>-131.079483</td>\n",
       "      <td>-131.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "      <td>-183.439476</td>\n",
       "      <td>-183.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "      <td>150.395277</td>\n",
       "      <td>150.395277</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X   Y_previsto           u    Residuos\n",
       "0  3011  9714  3095.785086  -84.785086  -84.785086\n",
       "1  1305  3728  1316.203010  -11.203010  -11.203010\n",
       "2  1879  6062  2010.079483 -131.079483 -131.079483\n",
       "3  2654  8845  2837.439476 -183.439476 -183.439476\n",
       "4  2849  8378  2698.604723  150.395277  150.395277"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset['Residuos'] = resultado_regressao.resid\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "GbnSv-B-cal6",
    "outputId": "d04dd941-1936-443e-fb8d-a050babc1ccd",
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "      <th>Residuos</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "      <td>-84.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "      <td>-11.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "      <td>-131.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "      <td>-183.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "      <td>150.395277</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X   Y_previsto    Residuos\n",
       "0  3011  9714  3095.785086  -84.785086\n",
       "1  1305  3728  1316.203010  -11.203010\n",
       "2  1879  6062  2010.079483 -131.079483\n",
       "3  2654  8845  2837.439476 -183.439476\n",
       "4  2849  8378  2698.604723  150.395277"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.drop(['u'], axis = 1, inplace = True)\n",
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "HQZQyrCzmHha",
    "outputId": "3f7b4cdb-9ab8-4a81-9e40-68f9da4675bf"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.479705578181893e-13"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.Residuos.mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "2FUvXCAW75mL"
   },
   "source": [
    "## <font color=green>4.6 Suposições sobre o termo de erro $u$</font>\n",
    "***\n",
    "\n",
    "### 1. O termo de erro $u$ é uma variável aleatória com média igual a zero: $E(u) = 0$\n",
    "### 2. A variância de $u$ é a mesma para todos os valores de $X$\n",
    "### 3. Os valores de $u$ são independentes\n",
    "### 4. O termo de erro $u$ é uma variável aleatória normalmente distribuída."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "7mFDWvhFcal7"
   },
   "source": [
    "### Plotando os resíduos do modelo\n",
    "\n",
    "https://seaborn.pydata.org/generated/seaborn.scatterplot.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 431
    },
    "colab_type": "code",
    "id": "r3RhXDBccal8",
    "outputId": "1db64403-88b8-4e89-9eaf-e817184af365"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x239af447da0>"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18300 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.scatterplot(x=dataset.X, y=dataset.Residuos)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Resíduos vs Variável Independente', fontsize=18)\n",
    "ax.set_xlabel('X', fontsize=14)\n",
    "ax.set_ylabel('Resíduos', fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 431
    },
    "colab_type": "code",
    "id": "6An7Ir6275mN",
    "outputId": "5218dc56-a6fd-4495-a354-0ba0b44eb2ba"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x239af48c780>"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 18012 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.scatterplot(x=dataset.Y_previsto, y=dataset.Residuos)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Resíduos vs Y_Previsto', fontsize=18)\n",
    "ax.set_xlabel('Y_Previsto', fontsize=14)\n",
    "ax.set_ylabel('Resíduos', fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "72hPAKNKcamD"
   },
   "source": [
    "### Hipótese de variância constante"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "dm_pfD0jcamE"
   },
   "source": [
    "<img src=\"https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img028.jpg\" width=80%>\n",
    "Fonte: Econometria Básica - 5ª edição - Gujarati e Porter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 431
    },
    "colab_type": "code",
    "id": "lCys_bu_camE",
    "outputId": "1637649e-41ed-40af-a004-dcfb5c9ee89f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x239af3f1518>"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 20012 characters]\n",
      "text/plain": [
       "<Figure size 864x432 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "ax = sns.scatterplot(x=dataset.Y_previsto, y=dataset.Residuos**2)\n",
    "ax.figure.set_size_inches(12, 6)\n",
    "ax.set_title('Resíduos² vs Y_Previsto', fontsize=18)\n",
    "ax.set_xlabel('Y_Previsto', fontsize=14)\n",
    "ax.set_ylabel('Resíduos²', fontsize=14)\n",
    "ax"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "d3X2NkIicamv"
   },
   "source": [
    "## <font color=green>4.7 O coeficiente de determinação $R^2$</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "02AXBeBrcamw"
   },
   "source": [
    "O **coeficiente de determinação R²** é uma medida resumida que diz quanto a linha de regressão amostral se ajusta aos dados. Pode ser obtido a partir da seguinte fórmula:\n",
    "\n",
    "# $$R^2 = \\frac{\\big[\\sum{(Y_i - \\bar{Y})(\\hat{Y}_i - \\bar{Y})}\\big]^2}{\\sum{(Y_i - \\bar{Y}})^2 \\sum{(\\hat{Y}_i - \\bar{Y}})^2}$$\n",
    "\n",
    "Sabemos que o i-ésimo resíduo representa o erro de usarmos $\\hat{Y}_i$ para estimar $Y_i$. A soma dos quadrados desses resíduos é o valor que é minimizado pelo método dos mínimos quadrados. Esse valor pode ser representado da seguinte forma:\n",
    "\n",
    "# $$SQE = \\sum{(Y_i - \\hat{Y}_i)^2}$$\n",
    "\n",
    "O valor da SQE é uma medida do erro de se usar a equação de regressão estimada para estimar os valores da variável dependente na amostra.\n",
    "\n",
    "Outro componente que podemos medir é a soma dos quadrados total (SQT) que representa a medida do erro envolvido no uso da média ($\\bar{Y}$) para fazer as estimativas. A SQT pode ser representada da forma abaixo:\n",
    "\n",
    "# $$SQT = \\sum{(Y_i - \\bar{Y})^2}$$\n",
    "\n",
    "Para quantificar o quanto os valores estimados ($\\hat{Y}_i$) se afastam da média ($\\bar{Y}$) podemos obter mais uma soma de quadrados. Essa soma é chamada de soma dos quadrados da regressão (SQR) e é representada pela seguinte fórmula:\n",
    "\n",
    "# $$SQR = \\sum{(\\hat{Y}_i - \\bar{Y})^2}$$\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "R5QuIfRCoT_W"
   },
   "source": [
    "### Soma do quadrados do erros (SQE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 204
    },
    "colab_type": "code",
    "id": "mjDVczwGcamw",
    "outputId": "52fbebca-5ec6-4b93-f4ce-e15da89ffb0f"
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<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>Y</th>\n",
       "      <th>X</th>\n",
       "      <th>Y_previsto</th>\n",
       "      <th>Residuos</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3011</td>\n",
       "      <td>9714</td>\n",
       "      <td>3095.785086</td>\n",
       "      <td>-84.785086</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1305</td>\n",
       "      <td>3728</td>\n",
       "      <td>1316.203010</td>\n",
       "      <td>-11.203010</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1879</td>\n",
       "      <td>6062</td>\n",
       "      <td>2010.079483</td>\n",
       "      <td>-131.079483</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2654</td>\n",
       "      <td>8845</td>\n",
       "      <td>2837.439476</td>\n",
       "      <td>-183.439476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2849</td>\n",
       "      <td>8378</td>\n",
       "      <td>2698.604723</td>\n",
       "      <td>150.395277</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Y     X   Y_previsto    Residuos\n",
       "0  3011  9714  3095.785086  -84.785086\n",
       "1  1305  3728  1316.203010  -11.203010\n",
       "2  1879  6062  2010.079483 -131.079483\n",
       "3  2654  8845  2837.439476 -183.439476\n",
       "4  2849  8378  2698.604723  150.395277"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dataset.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "fJ9ANPnIcamx",
    "outputId": "6798f1b9-1a61-40ff-e81a-0edeb2fe815c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "984986.0375191915"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SQE = dataset.Residuos.apply(lambda u: u**2).sum()\n",
    "SQE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "EzaZHZas75mT",
    "outputId": "924dfc13-8cea-446a-9e92-845a557e92e7"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "984986.0375191915"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Utilizando o ssr (sum of squared residuals) do statsmodels\n",
    "resultado_regressao.ssr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ciYucebH75mU"
   },
   "source": [
    "### Soma do quadrados total (SQT)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "8nBBr4HE75mV",
    "outputId": "97502af2-b60f-4f4e-c3a4-9b8f91c09435"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32724149.279999997"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SQT = dataset.Y.apply(lambda y: (y - dataset.Y.mean())**2).sum()\n",
    "SQT"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "2faHnARf75mW"
   },
   "source": [
    "### Soma do quadrados da regressão (SQR)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "sOIQrg2q75mW",
    "outputId": "fbd02601-bece-4e2b-91b1-fbea1a505d69"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31739163.242480762"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SQR = dataset.Y_previsto.apply(lambda y: (y - dataset.Y.mean())**2).sum()\n",
    "SQR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "6Z_ZWswM75mX",
    "outputId": "641dbb9f-76c9-4bca-b137-6352d3ae9a99"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31739163.242480807"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Utilizando o ess (explained sum of squares) do statsmodels\n",
    "resultado_regressao.ess"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "qzIqboEg75mY"
   },
   "source": [
    "### Relação entre as somas de quadrados\n",
    "\n",
    "# $$SQT = SQR + SQE$$\n",
    "\n",
    "Onde,\n",
    "\n",
    "\n",
    "### $SQE = \\sum{(Y_i - \\hat{Y}_i)^2}$\n",
    "\n",
    "### $SQT = \\sum{(Y_i - \\bar{Y})^2}$\n",
    "\n",
    "### $SQR = \\sum{(\\hat{Y}_i - \\bar{Y})^2}$\n",
    "\n",
    "\n",
    "<img src=\"https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img029.png\" width=50%>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OMz_pGr375ma"
   },
   "source": [
    "### Coeficiente de determinação (R²)\n",
    "\n",
    "A regressão estimada teria um ajuste perfeito se todos os valores da variável dependente ($Y_i$) estiverem sobre a reta de regressão estimada. Quando isso ocorre temos $Y_i - \\hat{Y}_i = 0$ para todas as observações, o que resulta em uma $SQE = 0$. Como $SQT=SQR+SQE$, é possível deduzir que para termos um ajuste perfeito $SQT$ deve ser igual a $SQR$ e a razão entre estas medidas ($\\frac{SQR}{SQT}$) deve ser igual a 1.\n",
    "\n",
    "Quando o ajuste não é tão perfeito nota-se que a $SQE$ apresenta valores maiores o que faz a razão $\\frac{SQR}{SQT}$ diminuir, e em uma situação de total imperfeição do ajuste teríamos uma $SQR=0$ e $SQE=SQT$ levando a $\\frac{SQR}{SQT}=0$.\n",
    "\n",
    "A razão $\\frac{SQR}{SQT}$, que assume valores entre 0 e 1, é utilizada como medida de eficiência do ajuste da equação de regressão estimada. Essa medida é conhecida como coeficiente de determinação (R²).\n",
    "\n",
    "# $$R^2 = \\frac{SQR}{SQT}$$\n",
    "\n",
    "## <font color='red'>Observação</font>\n",
    "> Somente com a utilização do R² não é possível tirar conclusões sobre a relação entre $Y$ e $X$ ser ou não ser estatisticamente\n",
    "significativa. Este tipo de afirmação deve basear-se em considerações que envolvem o tamanho da amostra e as propriedades da distribuição amostral dos estimadores mínimos quadrados."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "dKUngOqP75ma",
    "outputId": "3c4ac69b-310a-4393-fbe5-dc39476c5f5f"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9699003317369284"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "R2 = SQR / SQT\n",
    "R2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "oFowDxPB75mb",
    "outputId": "6019f00c-818b-4f1f-ba9e-8267ff394c4d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9699003317369297"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.rsquared"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "BwQR3YxTcam0"
   },
   "source": [
    "## <font color=green>4.8 Testes aplicados a modelos de regressão</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "eGZZaMp4td9Y"
   },
   "source": [
    "Como vimos, em uma regressão linear simples, a média da variável dependente ($Y$) é uma função linear da variável independente ($X$):\n",
    "\n",
    "# $$Y_i = \\beta_1 + \\beta_2X_i$$\n",
    "\n",
    "Se o valor de $\\beta_2$ for zero podemos verificar que o valor médio de $Y$ não depende do valor de $X$ e, portanto, concluímos que $X$ e $Y$ não estão linearmente relacionados. De forma alternativa, se o valor de $\\beta_2$ não for igual a zero, concluímos que as duas variáveis estão relacionadas.\n",
    "\n",
    "Para testar se a relação de regressão é significativa, é preciso realizar um **teste de hipóteses** para determinar se o valor de $\\beta_2$ é zero. Antes de realizar este tipo de teste precisamos obter uma estimativa para $\\sigma^2$ (variância do erro)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ePVflM9mtd9Z"
   },
   "source": [
    "### Output do modelo de regressão estimado"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 503
    },
    "colab_type": "code",
    "id": "xZu92Yaztd9Z",
    "outputId": "26c0e659-1a7a-49ce-aa2b-de49009b7c69"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      Y   R-squared:                       0.970\n",
      "Model:                            OLS   Adj. R-squared:                  0.969\n",
      "Method:                 Least Squares   F-statistic:                     1547.\n",
      "Date:                Mon, 29 Jul 2019   Prob (F-statistic):           3.56e-38\n",
      "Time:                        10:40:37   Log-Likelihood:                -318.16\n",
      "No. Observations:                  50   AIC:                             640.3\n",
      "Df Residuals:                      48   BIC:                             644.1\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const        207.9033     50.127      4.148      0.000     107.117     308.690\n",
      "X              0.2973      0.008     39.328      0.000       0.282       0.312\n",
      "==============================================================================\n",
      "Omnibus:                       22.833   Durbin-Watson:                   2.408\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):                3.915\n",
      "Skew:                           0.073   Prob(JB):                        0.141\n",
      "Kurtosis:                       1.637   Cond. No.                     1.64e+04\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "[2] The condition number is large, 1.64e+04. This might indicate that there are\n",
      "strong multicollinearity or other numerical problems.\n"
     ]
    }
   ],
   "source": [
    "print(resultado_regressao.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "imBNBULStd9a"
   },
   "source": [
    "### Erro quadrático médio - estimativa de $\\sigma^2$\n",
    "\n",
    "Lembre-se que a **soma dos quadrados do erros (SQE)** é uma medida numérica da variabilidade dos dados observados em torno da reta de regressão estimada. Todas as somas de quadrados, estudadas anteriormente, estão associadas a um determinado número de graus de liberdade. No caso da SQE, como devem ser estimados dois parâmetros, temos $n-2$ graus de liberdade.\n",
    "\n",
    "O **erro quadrático médio**, representado pela equação abaixo, pode ser obtido dividindo-se a SQE por $n-2$ graus de liberdade.\n",
    "\n",
    "# $$EQM = \\frac{SQE}{n-2}$$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "aDxRwFWJtd9a",
    "outputId": "8fa2a4ab-defc-494b-942a-6bf8dab1d41a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "984986.0375191915"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SQE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "qg5nZub3CEFx",
    "outputId": "54ad264b-c919-4c12-9928-b4c1d7d49def"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "50"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "TXfeyZwvtd9b",
    "outputId": "7c2245e2-a436-4b9b-9e5f-0a19bb4baf3d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20520.54244831649"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "EQM = SQE / (n - 2)\n",
    "EQM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "CF28rNuKtd9d",
    "outputId": "ba2d0b4e-65cd-46ae-be25-e5add14e3508"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20520.54244831649"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "EQM = resultado_regressao.mse_resid\n",
    "EQM"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "3xHqA9Gicam0"
   },
   "source": [
    "### Teste de hipótese para nulidade do coeficiente angular"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "QpUUlnKw9Ac_"
   },
   "source": [
    "Considere o seguinte modelo de regressão linear simples:\n",
    "\n",
    "# $$Y_i = \\beta_1 + \\beta_2X_i + u_i$$\n",
    "\n",
    "Se as variáveis $Y$ e $X$ são linearmente relacionadas, espera-se que $\\beta_2$ seja diferente de zero. Para testar esta hipótese formulamos um teste de hipótese com a seguinte especificação de hipóteses nula e alternativa:\n",
    "\n",
    "## $H_0: \\beta_2 = 0$\n",
    "## $H_1: \\beta_2 \\neq 0$\n",
    "\n",
    "Caso $H_0$ seja rejeitada, concluiremos que existe uma relação linear estatisticamente significativa entre as duas variáveis.\n",
    "\n",
    "Considere agora que $b_1$ e $b_2$ são nossos estimadores de mínimos quadrados. Considere que a distribuição amostral de $b_2$ segue uma normal e também as seguintes propriedades:\n",
    "\n",
    "# $$E(b_2) = \\beta_2$$\n",
    "# $$\\sigma_{b_2} = \\frac{\\sigma}{\\sqrt{\\sum{(X_i - \\bar{X})^2}}}$$\n",
    "\n",
    "Como não conhecemos o valor de $\\sigma$, utilizamos $s$ como estimativa:\n",
    "\n",
    "# $$s_{b_2} = \\frac{s}{\\sqrt{\\sum{(X_i - \\bar{X})^2}}}$$\n",
    "\n",
    "Note que o valor esperado de $b_2$ é $\\beta_2$, logo, $b_2$ é um estimador não viesado de $\\beta_2$. Abaixo temos a estatística de teste (t) que segue uma distribuição t de Student com $n-2$ graus de liberdade.\n",
    "\n",
    "# $$t = \\frac{b_2 - \\beta_2}{s_{b_2}}$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "hupnxGgFtd9g"
   },
   "source": [
    "### Calculando $s$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "9r87ZchPcam2",
    "outputId": "fdf0b518-98c1-43b4-db6f-fb54d90dd4b1"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "143.24993001155877"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = np.sqrt(resultado_regressao.mse_resid)\n",
    "s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "mr85iUdatd9q"
   },
   "source": [
    "### Calculando $\\sum{(X_i - \\bar{X})^2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "exNBEriWtd9q",
    "outputId": "b3a86e81-7724-46fa-d3f0-d61d14492042"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "359114426.5"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "SOMA_DESVIO2 = dataset.X.apply(lambda x: (x - dataset.X.mean())**2).sum()\n",
    "SOMA_DESVIO2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "mEuMAudxtd9u"
   },
   "source": [
    "### Calculando $s_{b_2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "uwr9JWN_td9u",
    "outputId": "87a6cb7e-33a9-43f8-9e60-c9dcc0cc3bfb"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.00755923753376846"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s_beta_2 = s / np.sqrt(SOMA_DESVIO2)\n",
    "s_beta_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "ZXe59tB-td9w"
   },
   "source": [
    "### Determinando as áreas de aceitação e rejeição de $H_0$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "sYjgmVgUtd9w"
   },
   "outputs": [],
   "source": [
    "from scipy.stats import t as t_student"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "cuvV9yC-td9x"
   },
   "source": [
    "![Região de Aceitação](https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img031.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "z9tmldlUtd9y"
   },
   "source": [
    "### Níveis de confiança e significância"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "x6imEPp0td9y"
   },
   "outputs": [],
   "source": [
    "confianca = 0.95\n",
    "significancia = 1 - confianca"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "VFJGcIRGD-ai",
    "outputId": "ad270885-02cd-43ef-ed3a-8100832af959"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48.0"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "graus_de_liberdade = resultado_regressao.df_resid\n",
    "graus_de_liberdade"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Xb6DWLiTtd9z"
   },
   "source": [
    "### Obtendo $t_{\\alpha/2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "yVEsPYPitd9z",
    "outputId": "afef1285-e590-472e-c64e-bdeeaea2c19b"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.975"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "probabilidade = (0.5 + (confianca / 2))\n",
    "probabilidade"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "_Cfvwj7Ktd91",
    "outputId": "f8a33b6a-2364-4492-fbeb-23c17498fa84"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0106347546964454"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t_alpha_2 = t_student.ppf(probabilidade, graus_de_liberdade)\n",
    "t_alpha_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "oW11PFl4td94"
   },
   "source": [
    "![Região de Aceitação](https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img032.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "Sjj0cgC7td94"
   },
   "source": [
    "### Obtendo $t = \\frac{b_2 - \\beta_2}{s_{b_2}}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "Db8oVrEWtd94",
    "outputId": "711431b8-c1a8-4d27-d697-427096dee61c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "39.328131926755"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t = (beta_2 - 0) / s_beta_2\n",
    "t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "h-Tm7-Qstd95",
    "outputId": "cc627fdf-8c48-4da1-9a3a-ab8b9ceb38e6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "39.328131926755"
      ]
     },
     "execution_count": 96,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.tvalues[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "jOvLKfvitd97"
   },
   "source": [
    "![Região de Aceitação](https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img033.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "lU747h33td97"
   },
   "source": [
    "### Etapas e regras de decisão do teste t de significância dos parâmetros"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "kDHBbSAitd97"
   },
   "source": [
    "<img src=\"https://caelum-online-public.s3.amazonaws.com/1273-estatistica-parte4/01/img030.png\" width=90%>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "-Il6O2H6td98"
   },
   "source": [
    "### <font color='red'>Critério do valor crítico</font>\n",
    "\n",
    "> ### Teste Bicaudal\n",
    "> ### Rejeitar $H_0$ se $t \\leq -t_{\\alpha / 2}$ ou se $t \\geq t_{\\alpha / 2}$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "24_4-073td99",
    "outputId": "df923628-eed0-4efe-a23c-2ff9c14d7151"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t <= -t_alpha_2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "jya2Bqvktd9-",
    "outputId": "afb1e753-5f48-477c-939b-5b52bcbe1fb5"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "t >= t_alpha_2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "dW7cJcnQtd9_"
   },
   "source": [
    "### <font color='red'>Critério do $p-valor$</font>\n",
    "\n",
    "> ### Teste Bicaudal\n",
    "> ### Rejeitar $H_0$ se o valor $p\\leq\\alpha$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "LJQ3BBpBtd9_",
    "outputId": "1e76d3a4-34c7-4920-87b7-8155d6156922"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.555543228586039e-38"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor = 2 * (t_student.sf(t, graus_de_liberdade))\n",
    "p_valor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "gpVuM3Gvtd-B",
    "outputId": "e6fcf48b-3839-49ee-bef3-2b2fb52ab9f9"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.555543228586039e-38"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor = resultado_regressao.pvalues[1]\n",
    "p_valor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "Qtmjl1oBtd-C",
    "outputId": "4157f6d5-c16d-4c30-952c-82817a25a422"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor <= significancia"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "EP5tgnmttd-D"
   },
   "source": [
    "### <font color='green'>Conclusão: Rejeitamos $H_0$ e concluímos que existe uma relação significativa entre as duas variáveis.</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 503
    },
    "colab_type": "code",
    "id": "HMaewwFXtd-D",
    "outputId": "8d4bb708-00ea-4d92-aaa7-d8d754a0e73a"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      Y   R-squared:                       0.970\n",
      "Model:                            OLS   Adj. R-squared:                  0.969\n",
      "Method:                 Least Squares   F-statistic:                     1547.\n",
      "Date:                Mon, 29 Jul 2019   Prob (F-statistic):           3.56e-38\n",
      "Time:                        10:40:37   Log-Likelihood:                -318.16\n",
      "No. Observations:                  50   AIC:                             640.3\n",
      "Df Residuals:                      48   BIC:                             644.1\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const        207.9033     50.127      4.148      0.000     107.117     308.690\n",
      "X              0.2973      0.008     39.328      0.000       0.282       0.312\n",
      "==============================================================================\n",
      "Omnibus:                       22.833   Durbin-Watson:                   2.408\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):                3.915\n",
      "Skew:                           0.073   Prob(JB):                        0.141\n",
      "Kurtosis:                       1.637   Cond. No.                     1.64e+04\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "[2] The condition number is large, 1.64e+04. This might indicate that there are\n",
      "strong multicollinearity or other numerical problems.\n"
     ]
    }
   ],
   "source": [
    "print(resultado_regressao.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "HTW4vH03td-E"
   },
   "source": [
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "fEAeu55gtd-E"
   },
   "source": [
    "### Teste F"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "boZIhWUmtd-E"
   },
   "source": [
    "O teste F também é uma ferramenta para testar a significância na regressão. Baseado na distribuição F de probabilidade, o teste F é utilizado para verificar a significância global na regressão, isto é, em uma regressão múltipla, onde existe mais de uma variável independente, o teste F verifica a nulidade de todos os parâmetros do modelo conjuntamente.\n",
    "\n",
    "Em nosso caso (regressão linear simples) ele fornece a mesma conclusão obtida com o teste t.\n",
    "\n",
    "**Hipóteses:**\n",
    "\n",
    "## $H_0: \\beta_2 = 0$\n",
    "## $H_0: \\beta_2 \\neq 0$\n",
    "\n",
    "**Estatística de teste:**\n",
    "\n",
    "# $$F = \\frac{\\frac{SQR}{k}}{\\frac{SQE}{n-k-1}}$$\n",
    "\n",
    "Onde,\n",
    "\n",
    "$SQR$ - soma dos quadrados da regressão\n",
    "\n",
    "$SQE$ - soma dos quadrados dos erros\n",
    "\n",
    "$k$ - total de variáveis independentes ($X$)\n",
    "\n",
    "**Regras de rejeição de $H_0$:**\n",
    "\n",
    "**Critério do valor crítico** $\\rightarrow$ Rejeitar se $F \\geq F_{\\alpha}$\n",
    "\n",
    "Onde,\n",
    "\n",
    "$F_{\\alpha}$ - baseia-se na distribuição F com $k$ graus de liberdade no numerador e $n-k-1$ no denominador.\n",
    "\n",
    "**Critério do p-valor** $\\rightarrow$ Rejeitar se $p-valor \\leq \\alpha$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "gT3WGPkNtd-E"
   },
   "source": [
    "### Calculando a estatística de teste ($F$)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "kGfJb7gVtd-E",
    "outputId": "9b34f5b1-7d78-4517-8312-f3d15e03d495"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "31739163.242480807"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.mse_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "v-ano9jmtd-H",
    "outputId": "6862bb3b-9d90-4c4d-c1d1-cfbed8ca3f67"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "20520.54244831649"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.mse_resid"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "9vtS-VQ2td-I",
    "outputId": "499ecc2b-55b5-4bac-e7e7-8c96cd5ad8c4"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1546.7019608482472"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "F = resultado_regressao.mse_model / resultado_regressao.mse_resid\n",
    "F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "w8yfK5k1td-K",
    "outputId": "1db698d3-dc70-42a0-f5bc-2018f01e9d10"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1546.7019608482472"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.fvalue"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "OEzKwQYztd-K"
   },
   "source": [
    "### Obtendo o p-valor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "XTl2Y4u5td-K",
    "outputId": "15bac85a-aca4-45e9-dedd-fb6b1f700c1d"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5555432285860227e-38"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "resultado_regressao.f_pvalue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "OWswoJrHtd-M"
   },
   "outputs": [],
   "source": [
    "from scipy.stats import f"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "GH127n5atd-O",
    "outputId": "b20fd8f7-f9a7-4c1a-891c-770b722080ed"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5555432285860227e-38"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor = f.sf(F, 1, 48)\n",
    "p_valor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "TmkXMaKktd-P",
    "outputId": "ca089d23-7d19-4cd6-ef9e-bd0c721557ae"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor <= 0.05"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "jy32sWqYtd-S"
   },
   "source": [
    "# <font color=green>5 EXTRAS</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "MpTZNPXgtd-S"
   },
   "source": [
    "## <font color=green>5.1 Outros testes</font>\n",
    "***"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 503
    },
    "colab_type": "code",
    "id": "dP3-Wwp9td-S",
    "outputId": "6d9a0a47-fb7d-4ab4-a8e5-05268f95ff0e"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                            OLS Regression Results                            \n",
      "==============================================================================\n",
      "Dep. Variable:                      Y   R-squared:                       0.970\n",
      "Model:                            OLS   Adj. R-squared:                  0.969\n",
      "Method:                 Least Squares   F-statistic:                     1547.\n",
      "Date:                Mon, 29 Jul 2019   Prob (F-statistic):           3.56e-38\n",
      "Time:                        10:40:37   Log-Likelihood:                -318.16\n",
      "No. Observations:                  50   AIC:                             640.3\n",
      "Df Residuals:                      48   BIC:                             644.1\n",
      "Df Model:                           1                                         \n",
      "Covariance Type:            nonrobust                                         \n",
      "==============================================================================\n",
      "                 coef    std err          t      P>|t|      [0.025      0.975]\n",
      "------------------------------------------------------------------------------\n",
      "const        207.9033     50.127      4.148      0.000     107.117     308.690\n",
      "X              0.2973      0.008     39.328      0.000       0.282       0.312\n",
      "==============================================================================\n",
      "Omnibus:                       22.833   Durbin-Watson:                   2.408\n",
      "Prob(Omnibus):                  0.000   Jarque-Bera (JB):                3.915\n",
      "Skew:                           0.073   Prob(JB):                        0.141\n",
      "Kurtosis:                       1.637   Cond. No.                     1.64e+04\n",
      "==============================================================================\n",
      "\n",
      "Warnings:\n",
      "[1] Standard Errors assume that the covariance matrix of the errors is correctly specified.\n",
      "[2] The condition number is large, 1.64e+04. This might indicate that there are\n",
      "strong multicollinearity or other numerical problems.\n"
     ]
    }
   ],
   "source": [
    "print(resultado_regressao.summary())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "c60PkV_Std-U"
   },
   "source": [
    "### Normalidade dos resíduos - Omnibus\n",
    "\n",
    "> ### $H_0:$ Os dados se distribuem como uma normal\n",
    "\n",
    "https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.normaltest.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "-Q6xvpfstd-U",
    "outputId": "6d7bf1c2-7f60-418f-f41d-33de9973092c"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.1012451024595026e-05\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import normaltest\n",
    "\n",
    "statistic, p_valor = normaltest(dataset.Residuos)\n",
    "print(p_valor)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "zrxNEHpstd-W",
    "outputId": "ed990421-f93a-42cb-ea41-f753e0c3b313"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor <= 0.05"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "trrfvTbQtd-X"
   },
   "source": [
    "https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.probplot.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 295
    },
    "colab_type": "code",
    "id": "Rv_9YHwutd-Z",
    "outputId": "0e8ce5bc-edac-4789-fc60-6c3fc711cfc4"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17272 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "from scipy.stats import probplot\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "(_, (_, _, _)) = probplot(dataset.Residuos, plot = plt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 287
    },
    "colab_type": "code",
    "id": "dYwjwYdMtd-b",
    "outputId": "3802f308-d173-468f-dce6-6b12b1206848"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x239af5c3b70>"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 6924 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "dados.Altura.hist(bins=50)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 295
    },
    "colab_type": "code",
    "id": "I5AOPc2vtd-c",
    "outputId": "c3beb4db-147a-4eb9-8cf3-c12bb07625a8"
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16328 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "(_, (_, _, _)) = probplot(dados.Altura, plot = plt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "5ilwY6HQtd-d"
   },
   "source": [
    "### Verificando a simetria\n",
    "\n",
    "https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.skew.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "6EuSXLHZtd-d",
    "outputId": "515fb460-7cfb-4fe8-838d-ddf00ce08842"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.07306920116444167"
      ]
     },
     "execution_count": 117,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.stats import skew\n",
    "\n",
    "S = skew(dataset.Residuos)\n",
    "S"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "D52bUcYStd-g"
   },
   "source": [
    "### Verificando a curtose\n",
    "\n",
    "https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.kurtosis.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "B_Zcx_cNtd-h",
    "outputId": "b8895129-bb7f-4c8c-d2ea-a634dc322a0c"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.6369621580686464"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.stats import kurtosis\n",
    "\n",
    "C = 3 + kurtosis(dataset.Residuos)\n",
    "C"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "19Z7h_Jhtd-h"
   },
   "source": [
    "### Normalidade dos resíduos - Jarque-Bera (statsmodels)\n",
    "\n",
    "> ### $H_0:$ Os dados se distribuem como uma normal\n",
    "\n",
    "> ### Estatística de teste\n",
    "> ### $$JB = \\frac{n}{6}(S^2 + \\frac{1}{4}(C - 3)^2)$$\n",
    "> #### Onde,\n",
    "> #### $n$ - número de observações\n",
    "> #### $S$ - Coeficiente de assimetria\n",
    "> #### $C$ - Coeficiente de curtose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "r356qjpOtd-h",
    "outputId": "d0ebbba7-7a33-4bfb-99c7-5211f2f93315"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.9150595649419175"
      ]
     },
     "execution_count": 119,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JB = (n / 6.) * (S ** 2 + (1 / 4.) * (C - 3) ** 2)\n",
    "JB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "aK8Lvwvbtd-i",
    "outputId": "9e31cdd7-d4e3-43ee-b441-7162d6ad37a8"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1412068019728567"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.stats import chi2\n",
    "\n",
    "p_valor = chi2.sf(JB, 2)\n",
    "p_valor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "4vvZL2V5td-j",
    "outputId": "a27ae907-9f42-423d-9384-2822a2603b09"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 121,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor <= 0.05"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "colab_type": "text",
    "id": "C7GvNaJztd-j"
   },
   "source": [
    "### Normalidade dos resíduos - Jarque-Bera (Correção)\n",
    "\n",
    "> ### $H_0:$ Os dados se distribuem como uma normal\n",
    "\n",
    "> ### Estatística de teste\n",
    "> ### $$JB = \\frac{n-k}{6}(S^2 + \\frac{1}{4}(C - 3)^2)$$\n",
    "> #### Onde,\n",
    "> #### $n$ - número de observações\n",
    "> #### $k$ - número de parâmetros do modelo\n",
    "> #### $S$ - Coeficiente de assimetria\n",
    "> #### $C$ - Coeficiente de curtose"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "Xy-1QQn-td-j",
    "outputId": "782ffa46-9baf-4f4a-f399-a8f945b50b3a"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23.412056198352666"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JB = (n - 1 / 6.) * (S ** 2 + (1 / 4.) * (C - 3) ** 2)\n",
    "JB"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "ynNMLEO3td-k",
    "outputId": "1de176b1-f661-45df-9a48-af792f19e3b6"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8.243973584285523e-06"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from scipy.stats import chi2\n",
    "\n",
    "p_valor = chi2.sf(JB, 2)\n",
    "p_valor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 35
    },
    "colab_type": "code",
    "id": "9gRlfnxLtd-l",
    "outputId": "c951a686-a0e7-44ea-d900-c7324150fba4"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 124,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p_valor <= 0.05"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "colab": {},
    "colab_type": "code",
    "id": "Bspj3u5vtd-n"
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "colab": {
   "name": "Curso_de_Estatística_Parte_4.ipynb",
   "provenance": [],
   "version": "0.3.2"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
