<jupyter_start><jupyter_text>- Fit a linear regression model with 2+ features of your choice. Get and plot the coefficients.- Use train-test split or leave-one-out cross-validation to get regression metrics: MSE, RMSE, MAE, R^2.- Visualize the plane of best fit in 3D, with 2 features.- Write a short, simple blog post about your elections model. (Or about your pageviews model, whichever you prefer!)- AWESOME BUT DIFFICULT STRETCH GOAL: In your 3D visualization, can you include the actual datapoints, like in this notebook? https://nbviewer.jupyter.org/urls/s3.amazonaws.com/datarobotblog/notebooks/multiple_regression_in_python.ipynb Can you also include the residual lines from the datapoints to the plane of the best fit, like in _An Introduction to Statistical Learning?_<jupyter_code>import pandas as pd
import numpy as np
from sklearn.linear_model import LinearRegression
pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 1000)
columns = ['Year','Incumbent Party Candidate','Other Candidate','Incumbent Party Vote Share']

data = [[1952,"Stevenson","Eisenhower",44.6],
        [1956,"Eisenhower","Stevenson",57.76],
        [1960,"Nixon","Kennedy",49.91],
        [1964,"Johnson","Goldwater",61.34],
        [1968,"Humphrey","Nixon",49.60],
        [1972,"Nixon","McGovern",61.79],
        [1976,"Ford","Carter",48.95],
        [1980,"Carter","Reagan",44.70],
        [1984,"Reagan","Mondale",59.17],
        [1988,"<NAME>.","Dukakis",53.94],
        [1992,"<NAME>.","Clinton",46.55],
        [1996,"Clinton","Dole",54.74],
        [2000,"Gore","Bush, Jr.",50.27],
        [2004,"<NAME>r.","Kerry",51.24],
        [2008,"McCain","Obama",46.32],
        [2012,"Obama","Romney",52.00], 
        [2016,"Clinton","Trump",48.2]]
        
votes = pd.DataFrame(data=data, columns=columns)
votes = votes.set_index('Year')

gdp = pd.read_csv('https://raw.githubusercontent.com/WillHK/DS-Unit-2-Regression-1/master/module3-doing-linear-regression/gdp.csv')
gdp = gdp.drop('Line', axis=1)
gdp = gdp.rename({'Unnamed: 1': 'Year'}, axis=1)
gdp = gdp.T
gdp.columns = gdp.iloc[0]
gdp = gdp.drop(['Year', '1947', '1948', '2017', '2018'], axis=0)
gdp.index = gdp.index.astype('int64')
election_year_gdp = gdp.loc[votes.index.values]
df = pd.concat([votes, election_year_gdp], axis=1, join='outer')
df.columns = df.columns.str.strip()

target = 'Incumbent Party Vote Share'
mean_baseline = [df[target].mean()] * len(df)
df['Mean Baseline'] = df[target].mean()
df['Error'] = df['Mean Baseline'] - df[target]
df['Absolute Error'] = df['Error'].abs()

df = df.drop(['Change in private inventories', 'Net exports of goods and services', 'Addendum:'], axis=1)
cols = ['Incumbent Party Vote Share', 'Gross domestic product', 'Personal consumption expenditures', 'Goods', 'Durable goods', 'Nondurable goods', 'Services', 'Gross private domestic investment', 'Fixed investment', 'Nonresidential', 'Structures', 'Equipment', 'Intellectual property products', 'Residential', 'Exports', 'Goods', 'Services', 'Imports', 'Goods', 'Services', 'Government consumption expenditures and gross investment', 'Federal', 'National defense', 'Nondefense', 'State and local', 'Mean Baseline', 'Error', 'Absolute Error']
for col in cols:
    df[col] = df[col].apply(pd.to_numeric)
df.head()
nonfarm_payroll = pd.read_csv('https://raw.githubusercontent.com/WillHK/DS-Unit-2-Regression-1/master/module3-doing-linear-regression/non-farm-payroll-annual-change.csv', parse_dates=['DATE'])
nonfarm_payroll = nonfarm_payroll.drop(nonfarm_payroll.tail(1).index)
nonfarm_payroll = nonfarm_payroll.rename({"DATE": "Year", 'PAYEMS_PC1': 'pct_change'}, axis=1)
nonfarm_payroll['pct_change'] = pd.to_numeric(nonfarm_payroll['pct_change'])
years = nonfarm_payroll['Year'].dt.year
nonfarm_payroll = nonfarm_payroll.set_index(years)
nonfarm_payroll = nonfarm_payroll.drop('Year', axis=1)
election_year_payroll = nonfarm_payroll.loc[votes.index.values]
df = pd.concat([df, election_year_payroll], axis=1, join='outer')
df
one_off = df.iloc[-1]
df = df.drop(df.tail(1).index)
one_off
from sklearn.linear_model import LinearRegression
model = LinearRegression()
y = 'Incumbent Party Vote Share'
X = ['Gross domestic product, current dollars', 'pct_change']

model.fit(df[X], df[y])
y_pred = model.predict(one_off[X])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16781450872359963, 'fraction_numerical': 0.04315886134067952, 'mean_word_length': 5.601515151515152, 'pattern_counts': {'":': 1, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '141665', 'license': 'MIT', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1492, 'n_words': 332, 'path': 'module4-understanding-ols/module4.ipynb', 'repo_name': 'WillHK/DS-Unit-2-Regression-1', 'size': '4361'}