From Predictions to Explanations¶
Agenda¶
- Theory
- Presentation & Discussion
- Code
- Modeling & Exporting Regression Tables
- Application
- Interpreting Regression Coefficients
Theory¶
Code¶
We will build a regression model to examine how political knowledge affects affective polarization. We will also learn how to export the regression results to Markdown (& LaTeX).
Building the Regression Model¶
We will use ordinary least squares (OLS) regression to model the relationship between affective polarization and political knowledge.
import pandas as pd
import statsmodels.formula.api as sm
df = pd.read_csv("../data/clean_anes.csv")
# Define the regression formula
formula = "affective_polarization ~ political_knowledge_scale + age + sex + education + ideology"
# Fit the regression model
model = sm.ols(formula=formula, data=df).fit()
print(model.summary())
Exporting to Mardown or LaTeX¶
We can use statsmodels
to export our regression table to LaTeX format.
from statsmodels.iolib.summary2 import summary_col
latex_output = summary_col([model]).as_latex()
print(latex_output)
This output can be directly included in your academic papers written in markdown to present the regression results clearly and professionally.
Application¶
Let's head to Github and open our codespace (text editor)