Overview

7 Number go up (or down)! Correlation and linear regression

Linear regression and correlation are introduced as essential tools for understanding and predicting relationships between variables. The chapter begins by showing how a scatterplot can reveal whether two variables move together in a roughly straight-line pattern, and it emphasizes that this relationship may be positive or negative without being causal. Pearson correlation is presented as the standard numerical summary of linear association, with values ranging from -1 to 1, and as a hypothesis test that can be paired with a p-value to assess whether the observed relationship is likely to have occurred by chance.

The discussion then turns to linear regression, which fits a line to data so that one variable can be used to predict another. Using a sports drink example, the chapter explains slope, intercept, residuals, and the sum of squares as the main ingredients of fitting the best line. It shows that libraries can compute these values directly, but also walks through the underlying idea: choose the line that minimizes the squared differences between predicted and actual values. The chapter also introduces R-squared and RMSE as ways to judge how well the model captures variation in the data.

Finally, the text stresses the limits of these methods. Pearson correlation and linear regression rely on assumptions such as linearity, continuity, normality, steady variance, and the absence of extreme outliers, so plots and diagnostics are necessary before trusting the numbers. The chapter warns that correlation is not causation and that extrapolation beyond the observed data range can be misleading. It closes by broadening the view to multiple regression and a real-world bird strike example, showing that even when a pattern seems present, the relationship may be nonlinear or poorly suited to a linear model, and that good analysis requires skepticism, context, and careful validation.

A scatterplot showing 10 data points relating weather temperature to sports drink sales. The points seem to follow a “line” and have an upward trend pattern: as temperature increases, so do sales.
Fitting a line through these data points to visualize a linear relationship in the data
Different Pearson correlation values with different datasets. Note how a strong positive correlation (close to 1) or negative correlation (close to -1) has data that more closely resembles a line.
Different Pearson correlation values and p-values. Notice how the sample size and data dispersion affect the p-value.
Anscombe’s quartet, showing four different datasets that have the same correlation coefficient and linear regression, yet have very different shapes
Tyler Vigen’s database of spurious correlations includes a finding that the number of people who drowned by falling into a pool correlates with the number of films Nicolas Cage appeared in.
Using matplotlib to show a linear regression against our data
A 3D plot showing the sum of squares for different m and b values. We are trying to find the m and b values at the lowest point in this valley.
A histogram of airplane height vs. number of bird strikes
Bird strike incidents binned in 100-foot increments for HEIGHT, with a linear regression applied
Attempting a linear regression after a logarithmic transformation

Summary

  • A Pearson correlation quantifies the degree to which two datasets have a linear relationship, assuming conditions such as homoscedasticity and the absence of outliers are maintained.
  • A p-value can be used to quantify the statistical significance of a Pearson correlation coefficient.
  • A linear regression is similar to but distinct from a Pearson correlation, as it is used to predict a dependent variable from an independent variable (like predicting sports drink sales for a given temperature).
  • The least sum of squares is the most often used metric for fitting a linear regression, and understanding squared residuals is key to understanding error metrics, such as the root mean squared error (RMSE), the coefficient of determination, and the bias/variance tradeoff.
  • Be mindful not to assume that a correlation means causation, and do not extrapolate trends outside the range of data you have.
  • Be mindful when selecting your predictors, the variables you are hypothesizing for your model. Do not take correlation and p-value metrics at face value, and always plot your data to make sure your metrics are not misleading you.
  • You can try transformations on your data to make it suitable for linear correlation or regression, but be mindful not to torture your data, and be willing to abandon your hypotheses.

FAQ

What is the Pearson correlation coefficient used for?The Pearson correlation coefficient measures the strength and direction of a linear relationship between two variables. Its value ranges from -1 to 1: values near 1 indicate a strong positive linear relationship, values near -1 indicate a strong negative linear relationship, and values near 0 indicate little or no linear relationship.
What does a positive correlation mean?A positive correlation means that as one variable increases, the other tends to increase as well. For example, higher temperatures may be associated with higher sports drink sales.
What does a negative correlation mean?A negative correlation means that as one variable increases, the other tends to decrease. For example, more hours on social media may be associated with lower happiness scores.
Does a negative correlation mean there is no relationship?No. A negative correlation still indicates a relationship, just in the opposite direction. “No correlation” means there is no clear linear relationship between the variables.
What is the null hypothesis when testing correlation?The null hypothesis is that the two variables are not linearly correlated, meaning the true correlation is 0. The alternative hypothesis is that there is a significant linear correlation.
What assumptions should be met for Pearson correlation?Key assumptions include a linear relationship, continuous variables, approximate normality, steady variance across x values (homoscedasticity), no severe outliers, and independent data points.
What does the p-value from Pearson correlation tell us?The p-value estimates how likely it is to observe a Pearson correlation at least as extreme as the one calculated if the true relationship were uncorrelated. A very small p-value suggests the observed correlation is unlikely to be due to chance.
What is simple linear regression?Simple linear regression fits a straight line to two variables, one independent variable x and one dependent variable y, so that the model can predict y from x using an equation like y = mx + b.
What do the slope and intercept mean in linear regression?The slope tells how much y changes for each 1-unit increase in x. The intercept is the predicted y value when x = 0, though that value is not always meaningful in practice.
What is the difference between interpolation and extrapolation?Interpolation means making predictions within the range of data you already observed. Extrapolation means predicting outside that range, which is often unreliable and can lead to misleading results.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Statistics ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Statistics ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Grokking Statistics ebook for free