# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: 'Python 3.6.8 64-bit (''venv'': venv)'
#     language: python
#     name: python36864bitvenvvenve269670d94154a47aa0ba5a4660e02de
# ---

# # Regression and Other Stories: Elections Economy

# Predicting presidential vote share from the economy. See Chapters 1, 7, 8, 9, and 22 in [Regression and Other Stories](https://avehtari.github.io/ROS-Examples/) by **<NAME>**, **<NAME>**, **<NAME>**.

# **Import libraries**

import pandas as pd
import numpy as np
import altair as alt
# import pystan 
import statsmodels.api as sm
alt.themes.enable('fivethirtyeight')

# **Load data**

hibbs = pd.read_table('datasets/ElectionsEconomy/data/hibbs.dat', sep=' ')

hibbs["growth-prop"] = hibbs["growth"]/100
hibbs["vote-prop"] = hibbs["vote"]/100

hibbs.head()

# +
points = alt.Chart(hibbs).mark_text().encode(
    x=alt.X("growth-prop", 
            axis= alt.Axis(format="%", title="Average recent growth in personal income")),
    y=alt.Y("vote-prop", 
            axis= alt.Axis(format="%", title="Incumbent party's vote share"),
            scale=alt.Scale(zero=False)), 
    text='year')

line = alt.Chart(hibbs).mark_rule(opacity=0.1).encode(alt.YValue(200.1), size=alt.value(0.5))

chart = points + line
chart
# -

# ## Linear regression

# +
y = hibbs["vote"]
X = hibbs["growth"]
X = sm.add_constant(X)

mod1 = sm.OLS(y ,X).fit()
# -

print(mod1.summary())

chart | points.mark_circle() + points.transform_regression("growth-prop", "vote-prop").mark_line() + line

StarcoderdataJupyterScriptsDedupFiltered