Linear regression by least squares

Statistical Thinking in Python (Part 2)

Justin Bois

Lecturer at the California Institute of Technology

2008 US swing state election results

ch1-2.002.png

1 Data retrieved from Data.gov (https://www.data.gov/)
Statistical Thinking in Python (Part 2)

2008 US swing state election results

ch1-2.004.png

1 Data retrieved from Data.gov (https://www.data.gov/)
Statistical Thinking in Python (Part 2)

2008 US swing state election results

ch1-2.007.png

1 Data retrieved from Data.gov (https://www.data.gov/)
Statistical Thinking in Python (Part 2)

2008 US swing state election results

ch1-2.008.png

1 Data retrieved from Data.gov (https://www.data.gov/)
Statistical Thinking in Python (Part 2)

Residuals

ch1-2.010.png

1 Data retrieved from Data.gov (https://www.data.gov/)
Statistical Thinking in Python (Part 2)

Least squares

  • The process of finding the parameters for which the sum of the squares of the residuals is minimal
Statistical Thinking in Python (Part 2)

Least squares with np.polyfit()

slope, intercept = np.polyfit(total_votes,
                              dem_share, 1)

slope
4.0370717009465555e-05
intercept
40.113911968641744
Statistical Thinking in Python (Part 2)

Let's practice!

Statistical Thinking in Python (Part 2)

Preparing Video For Download...