Created
February 19, 2023 06:02
-
-
Save evandroamparo/11356d789249f8580854ab0308faa2c5 to your computer and use it in GitHub Desktop.
Linear regression - Coefficient estimates
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyNZR6LJpXhDHaD7/Gz2Le6n", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/evandroamparo/11356d789249f8580854ab0308faa2c5/linear-regression-coefficient-estimates.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 67, | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/", | |
| "height": 424 | |
| }, | |
| "id": "VxmQnR8Jw5Tw", | |
| "outputId": "96410c31-811e-4b4d-c2d6-211d4346daaf" | |
| }, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| " Unnamed: 0 TV radio newspaper sales\n", | |
| "0 1 230.1 37.8 69.2 22.1\n", | |
| "1 2 44.5 39.3 45.1 10.4\n", | |
| "2 3 17.2 45.9 69.3 9.3\n", | |
| "3 4 151.5 41.3 58.5 18.5\n", | |
| "4 5 180.8 10.8 58.4 12.9\n", | |
| ".. ... ... ... ... ...\n", | |
| "195 196 38.2 3.7 13.8 7.6\n", | |
| "196 197 94.2 4.9 8.1 9.7\n", | |
| "197 198 177.0 9.3 6.4 12.8\n", | |
| "198 199 283.6 42.0 66.2 25.5\n", | |
| "199 200 232.1 8.6 8.7 13.4\n", | |
| "\n", | |
| "[200 rows x 5 columns]" | |
| ], | |
| "text/html": [ | |
| "\n", | |
| " <div id=\"df-74d9ada8-28a7-4d6e-b7c7-5861b6e9655f\">\n", | |
| " <div class=\"colab-df-container\">\n", | |
| " <div>\n", | |
| "<style scoped>\n", | |
| " .dataframe tbody tr th:only-of-type {\n", | |
| " vertical-align: middle;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe tbody tr th {\n", | |
| " vertical-align: top;\n", | |
| " }\n", | |
| "\n", | |
| " .dataframe thead th {\n", | |
| " text-align: right;\n", | |
| " }\n", | |
| "</style>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>Unnamed: 0</th>\n", | |
| " <th>TV</th>\n", | |
| " <th>radio</th>\n", | |
| " <th>newspaper</th>\n", | |
| " <th>sales</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>1</td>\n", | |
| " <td>230.1</td>\n", | |
| " <td>37.8</td>\n", | |
| " <td>69.2</td>\n", | |
| " <td>22.1</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>2</td>\n", | |
| " <td>44.5</td>\n", | |
| " <td>39.3</td>\n", | |
| " <td>45.1</td>\n", | |
| " <td>10.4</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>3</td>\n", | |
| " <td>17.2</td>\n", | |
| " <td>45.9</td>\n", | |
| " <td>69.3</td>\n", | |
| " <td>9.3</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>3</th>\n", | |
| " <td>4</td>\n", | |
| " <td>151.5</td>\n", | |
| " <td>41.3</td>\n", | |
| " <td>58.5</td>\n", | |
| " <td>18.5</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>4</th>\n", | |
| " <td>5</td>\n", | |
| " <td>180.8</td>\n", | |
| " <td>10.8</td>\n", | |
| " <td>58.4</td>\n", | |
| " <td>12.9</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>...</th>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " <td>...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>195</th>\n", | |
| " <td>196</td>\n", | |
| " <td>38.2</td>\n", | |
| " <td>3.7</td>\n", | |
| " <td>13.8</td>\n", | |
| " <td>7.6</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>196</th>\n", | |
| " <td>197</td>\n", | |
| " <td>94.2</td>\n", | |
| " <td>4.9</td>\n", | |
| " <td>8.1</td>\n", | |
| " <td>9.7</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>197</th>\n", | |
| " <td>198</td>\n", | |
| " <td>177.0</td>\n", | |
| " <td>9.3</td>\n", | |
| " <td>6.4</td>\n", | |
| " <td>12.8</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>198</th>\n", | |
| " <td>199</td>\n", | |
| " <td>283.6</td>\n", | |
| " <td>42.0</td>\n", | |
| " <td>66.2</td>\n", | |
| " <td>25.5</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>199</th>\n", | |
| " <td>200</td>\n", | |
| " <td>232.1</td>\n", | |
| " <td>8.6</td>\n", | |
| " <td>8.7</td>\n", | |
| " <td>13.4</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "<p>200 rows × 5 columns</p>\n", | |
| "</div>\n", | |
| " <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-74d9ada8-28a7-4d6e-b7c7-5861b6e9655f')\"\n", | |
| " title=\"Convert this dataframe to an interactive table.\"\n", | |
| " style=\"display:none;\">\n", | |
| " \n", | |
| " <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n", | |
| " width=\"24px\">\n", | |
| " <path d=\"M0 0h24v24H0V0z\" fill=\"none\"/>\n", | |
| " <path d=\"M18.56 5.44l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94zm-11 1L8.5 8.5l.94-2.06 2.06-.94-2.06-.94L8.5 2.5l-.94 2.06-2.06.94zm10 10l.94 2.06.94-2.06 2.06-.94-2.06-.94-.94-2.06-.94 2.06-2.06.94z\"/><path d=\"M17.41 7.96l-1.37-1.37c-.4-.4-.92-.59-1.43-.59-.52 0-1.04.2-1.43.59L10.3 9.45l-7.72 7.72c-.78.78-.78 2.05 0 2.83L4 21.41c.39.39.9.59 1.41.59.51 0 1.02-.2 1.41-.59l7.78-7.78 2.81-2.81c.8-.78.8-2.07 0-2.86zM5.41 20L4 18.59l7.72-7.72 1.47 1.35L5.41 20z\"/>\n", | |
| " </svg>\n", | |
| " </button>\n", | |
| " \n", | |
| " <style>\n", | |
| " .colab-df-container {\n", | |
| " display:flex;\n", | |
| " flex-wrap:wrap;\n", | |
| " gap: 12px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert {\n", | |
| " background-color: #E8F0FE;\n", | |
| " border: none;\n", | |
| " border-radius: 50%;\n", | |
| " cursor: pointer;\n", | |
| " display: none;\n", | |
| " fill: #1967D2;\n", | |
| " height: 32px;\n", | |
| " padding: 0 0 0 0;\n", | |
| " width: 32px;\n", | |
| " }\n", | |
| "\n", | |
| " .colab-df-convert:hover {\n", | |
| " background-color: #E2EBFA;\n", | |
| " box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n", | |
| " fill: #174EA6;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert {\n", | |
| " background-color: #3B4455;\n", | |
| " fill: #D2E3FC;\n", | |
| " }\n", | |
| "\n", | |
| " [theme=dark] .colab-df-convert:hover {\n", | |
| " background-color: #434B5C;\n", | |
| " box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n", | |
| " filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n", | |
| " fill: #FFFFFF;\n", | |
| " }\n", | |
| " </style>\n", | |
| "\n", | |
| " <script>\n", | |
| " const buttonEl =\n", | |
| " document.querySelector('#df-74d9ada8-28a7-4d6e-b7c7-5861b6e9655f button.colab-df-convert');\n", | |
| " buttonEl.style.display =\n", | |
| " google.colab.kernel.accessAllowed ? 'block' : 'none';\n", | |
| "\n", | |
| " async function convertToInteractive(key) {\n", | |
| " const element = document.querySelector('#df-74d9ada8-28a7-4d6e-b7c7-5861b6e9655f');\n", | |
| " const dataTable =\n", | |
| " await google.colab.kernel.invokeFunction('convertToInteractive',\n", | |
| " [key], {});\n", | |
| " if (!dataTable) return;\n", | |
| "\n", | |
| " const docLinkHtml = 'Like what you see? Visit the ' +\n", | |
| " '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n", | |
| " + ' to learn more about interactive tables.';\n", | |
| " element.innerHTML = '';\n", | |
| " dataTable['output_type'] = 'display_data';\n", | |
| " await google.colab.output.renderOutput(dataTable, element);\n", | |
| " const docLink = document.createElement('div');\n", | |
| " docLink.innerHTML = docLinkHtml;\n", | |
| " element.appendChild(docLink);\n", | |
| " }\n", | |
| " </script>\n", | |
| " </div>\n", | |
| " </div>\n", | |
| " " | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 67 | |
| } | |
| ], | |
| "source": [ | |
| "import pandas as pd\n", | |
| "\n", | |
| "data = pd.read_csv('https://www.statlearning.com/s/Advertising.csv')\n", | |
| "\n", | |
| "data" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "X = data['TV']\n", | |
| "\n", | |
| "Y = data['sales']\n", | |
| "\n", | |
| "X.mean()" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "Gpkb9hZfCa7M", | |
| "outputId": "252b7aba-a3a3-44e4-9ec9-6d5a3f6f769f" | |
| }, | |
| "execution_count": 68, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "147.0425" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 68 | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "#Linear regression\n", | |
| "\n", | |
| "## Coefficient estimates\n", | |
| "\n", | |
| "" | |
| ], | |
| "metadata": { | |
| "id": "4higIj6GObKs" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def beta1(x, y):\n", | |
| " mean_x = X.mean()\n", | |
| " mean_y = Y.mean()\n", | |
| " return ((X - mean_x) * (Y - mean_y)).sum() / ((X - mean_x) ** 2).sum()\n", | |
| "\n", | |
| "def beta0(X, Y):\n", | |
| " return Y.mean() - beta1(X, Y) * X.mean()\n", | |
| "\n", | |
| "print('b0 = ', beta0(X, Y))\n", | |
| "print('b1 = ', beta1(X, Y))\n" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "JaFhhfKWJHja", | |
| "outputId": "64bf6dcb-eed4-4469-df60-0315563db22c" | |
| }, | |
| "execution_count": 74, | |
| "outputs": [ | |
| { | |
| "output_type": "stream", | |
| "name": "stdout", | |
| "text": [ | |
| "b0 = 7.0325935491276965\n", | |
| "b1 = 0.047536640433019736\n" | |
| ] | |
| } | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "b0 = beta0(X, Y)\n", | |
| "b1 = beta1(X, Y)\n", | |
| "\n", | |
| "sales = b0 + b1 * X\n", | |
| "\n", | |
| "sales" | |
| ], | |
| "metadata": { | |
| "colab": { | |
| "base_uri": "https://localhost:8080/" | |
| }, | |
| "id": "WUcDBTceNWtn", | |
| "outputId": "5d19ca7c-f133-463e-88b8-7035797c509f" | |
| }, | |
| "execution_count": 70, | |
| "outputs": [ | |
| { | |
| "output_type": "execute_result", | |
| "data": { | |
| "text/plain": [ | |
| "0 17.970775\n", | |
| "1 9.147974\n", | |
| "2 7.850224\n", | |
| "3 14.234395\n", | |
| "4 15.627218\n", | |
| " ... \n", | |
| "195 8.848493\n", | |
| "196 11.510545\n", | |
| "197 15.446579\n", | |
| "198 20.513985\n", | |
| "199 18.065848\n", | |
| "Name: TV, Length: 200, dtype: float64" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "execution_count": 70 | |
| } | |
| ] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment