Linear Regression

October 27, 2026

Linear Model with Single Predictor


Goal: Estimate Democracy score (\(\hat{y}_i\)) of a country given level of GDP per capita (\(x_i\)).


Or: Estimate relationship between GDP per capita and democracy.

Linear Model with Single Predictor

Estimate Model


Fit model using base R lm() with formula syntax


mod_fit <- lm(lib_dem ~ log_wealth, data = modelData)


View results with summary()


\[\widehat{Democracy}_{i} = 0.01 + 0.13 \times loggdppc_{i}\]

summary(mod_fit)

Call:
lm(formula = lib_dem ~ log_wealth, data = modelData)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.58981 -0.14940  0.04111  0.18360  0.41129 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 0.008248   0.047553   0.173    0.862    
log_wealth  0.129576   0.014481   8.948 5.66e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 0.2171 on 172 degrees of freedom
  (5 observations deleted due to missingness)
Multiple R-squared:  0.3176,    Adjusted R-squared:  0.3137 
F-statistic: 80.07 on 1 and 172 DF,  p-value: 5.657e-16

Interpretation?


\[\widehat{Democracy}_{i} = 0.01 + 0.13 \times loggdppc_{i}\]

Question


How do we get the “best” values for the slope and intercept?

How would you draw the “best” line?

How would you draw the “best” line?

Least squares regression


  • Remember the residual is the difference between the actual value and the predicted value
  • The regression line minimizes the sum of squared residuals.

Least squares regression


  • Residual for each point is: \(e_i = y_i - \hat{y}_i\)

  • Least squares regression line minimizes \(\sum_{i = 1}^n e_i^2\).

  • Why do we square the residual?
  • Why not take absolute value?

    • Principle: larger penalty for residuals further away
    • Math: makes the math easier and some nice properties (not our concern here…)

Least squares regression

Very Simple Example

What should the slope and intercept be?

Example

\(\hat{y} = 0 + 1x\)

Example

What is the sum of squared residuals?

Example

What is sum of squared residuals for \(\hat{y} = 0 + 0x\)?

Example

What is sum of squared residuals for \(\hat{y} = 0 + 0x\)?

(1-0)^2 + (2-0)^2 + (3-0)^2
[1] 14

Example

What is sum of squared residuals for \(\hat{y} = 0 + 2x\)?

Example

What is sum of squared residuals for \(\hat{y} = 0 + 2x\)?

(1-2)^2 + (2-4)^2 + (3-6)^2
[1] 14

One more…

What is sum of squared residuals for \(\hat{y} = 0 - 1x\)?

One more…

What is sum of squared residuals for \(\hat{y} = 0 - 1x\)?

(1+1)^2 + (2+2)^2 + (3+3)^2
[1] 56

Cost Function

Sum of Squared Residuals as function of possible values of \(b_1\)

Least Squares Regression


  • When we estimate a least squares regression, it is looking for the line that minimizes sum of squared residuals

  • In the simple example, I set \(b_0 = 0\) to make it easier. More complicated when searching for combination of \(b_0\) and \(b_1\) that minimize, but same basic idea

Least Squares Regression


  • There is a way to solve for this analytically for linear regression (i.e., by doing math…)

    – They made us do this in grad school…

  • In machine learning, people also use gradient descent algorithm in which the computer searches over possible combinations of \(b_0\) and \(b_1\) until it settles on the lowest point.

Least Squares Regression

Least Squares Regression

Your Turn


Are democracies less corrupt?


  • V-Dem includes a Political Corruption Index, which aggregates corruption in a number of spheres (see codebook for details).

  • The variable name is: v2x_corr : lower values mean less corruption

Your Turn


Are democracies less corrupt?


  • Filter the V-Dem data to only include the year 2019
  • Make a scatterplot to visualize the relationship between democracy (X) and corruption (Y) (use the v2x_libdem variable for democracy)
  • Fit a linear model
  • Interpret results for the slope and intercept
  • For a country with the average (mean) level of democracy, what is the predicted level of corruption?

Create Your Own Model


  • What is a theory that you would like to test with V-Dem data?
  • What is the dependent variable?
  • What is the independent variable?
  • Map out steps to wrangle the data and fit a regression model
  • What do you expect to find?
  • Now go ahead and wrangle the data
  • Fit the model
  • Interpret the coefficients and their significance
  • Did the results match your expectations?