6  Drawing regression lines and equations

We make a figure that draws a regression line on a scatter plot and writes in the equation (y = ax + b), the coefficient of determination R² and the p-value. We use the relationship between bill length and bill depth.

6.1 Regression line and confidence band

If you specify nothing, geom_smooth() draws a curve such as local regression (loess). When you want a straight line, always specify method = "lm".

By default, the band of seaborn’s regplot() is a 95% confidence interval computed by bootstrapping (random numbers). Make it reproducible with seed=.

It looks as though longer bills are shallower (sloping down to the right). Let’s compute the equation of this line and write it on the figure.

6.2 Computing the equation, R² and p-value

scipy.stats.linregress() returns the slope, intercept, correlation coefficient and p-value of a simple linear regression with one variable. For multiple variables, use statsmodels (see below).

The result is y = −0.085x + 20.89, R² = 0.055, p = 1.1 × 10⁻⁵.

6.2.1 Turning the equation into a string: watch the sign

When the intercept is negative, a naive paste gives “y = 0.2x + -5.3”. So we write a function that switches between “+” and “−” depending on the sign. While we’re at it, we replace the hyphen (-) with the mathematical minus sign (−, U+2212).

6.3 Writing the equation on the figure: “data coordinates” vs “panel coordinates”

There are two ways of thinking about specifying the position of text.

  • Data coordinates: specified by axis values, such as x = 55 mm, y = 21 mm. When the data change, the text may stick out or overlap the points
  • Panel coordinates: specified as a fraction of the plot frame, such as “5% in from the top left of the panel”. It can be placed in the same spot for any data

For annotations like equations, placing them in panel coordinates is convenient.

In ggplot2, specifying -Inf / Inf as a position gives the edge of the panel. Use hjust and vjust (the anchor point of the text; 0 is left/bottom, 1 is right/top) to pull it inward.

In matplotlib, specifying transform=ax.transAxes makes the position panel coordinates (0 to 1). ha and va are the anchor point of the text. Coordinate transforms are covered in detail in Chapter 9.

6.4 Formatting math: superscripts and italics

To make the “2” in R² a superscript or to italicise variables, use math formatting.

With parse = TRUE, the label is interpreted as R’s plotmath (its syntax for math). ^ makes a superscript, italic() italics, == an equals sign, %*% ×, ~ a space, and * concatenates.

In matplotlib, enclosing text in $...$ makes it mathtext (a LaTeX-like syntax for math). ^ makes a superscript, \times gives ×, and {} delimits a group. Japanese text can’t be used inside $...$, so write any Japanese outside the $.

6.5 Drawing a line and equation per group

Now let’s colour by species.

To offset the equations one line at a time, vjust is increased with the row number (vjust is an offset in units of text height). To keep the equations off the points, expansion() adds headroom at the top of the vertical axis.

6.6 Simpson’s paradox

The line for all 3 species combined (black dashed) slopes down (slope −0.085), yet within every species it slopes up (slopes 0.18–0.22).

  • Within a species, individuals with longer bills have deeper bills
  • But Gentoo is a species with “long, shallow” bills, so when the 3 species are combined it looks like a downward slope

If you draw a regression line without separating the groups, even the direction of the relationship can be reversed (descriptive statistics hands-on Chapter 1, confounding hands-on). If you include species in the model, you can estimate the slope within species.

The species-adjusted slope is 0.20, close to the per-species slopes.

6.7 Confidence bands and prediction bands

The band of geom_smooth() and regplot() is the 95% confidence interval of the regression line (the mean). A prediction interval, which shows “where the value of one new penguin is likely to fall”, is much wider.

ImportantChapter summary
  • Straight lines: geom_smooth(method = "lm") in ggplot2 (a curve if not specified), regplot() in seaborn (the band is bootstrapped)
  • Compute the equation yourself and turn it into a string. Watch the sign of a negative intercept
  • Place annotations in panel coordinates (ggplot2: -Inf/Inf with hjust/vjust; matplotlib: transform=ax.transAxes)
  • Superscripts and italics: R’s plotmath (parse = TRUE), matplotlib’s mathtext ($...$)
  • Per-group lines and the overall line can point in opposite directions (Simpson’s paradox)
  • The band is the confidence interval of the line. The range of individual values is the prediction interval