Tutorials › Exam PA - Predictive Analytics › Nonlinear Patterns and Scatterplot Smoothers

Exam PA · Visualization and Anomaly Detection · Tutorial 149 of 966

Nonlinear Patterns and Scatterplot Smoothers

Use a smooth curve alongside the plotted observations to recognize nonlinear trends, compare them with a straight-line summary, and keep the interpretation descriptive.

Beginner 11 min read

What You'll Learn

  • Distinguish a scatterplot smoother from a straight-line summary and from a predictive model
  • Calculate simple moving-average smooths and interpret their changing direction
  • Understand the local weighted-regression idea behind LOESS
  • Use R to add a LOESS curve to a scatterplot
  • Identify when curvature or a turning point is visible in the plotted trend
  • Avoid treating a descriptive smooth as proof of a causal or predictive relationship

Why Look Beyond a Straight Line?

A scatterplot can show how a quantitative response \(Y\) varies with a quantitative predictor \(X\). A straight line is a compact visual summary, but it can hide an important pattern: the response may rise and then fall, or it may rise at an increasing rate. A smooth curve drawn through the cloud of points can make these changes in direction easier to see.

A smoother is a descriptive aid. It summarizes the shape of the observed relationship without requiring the relationship to be a straight line. It does not, by itself, establish why the pattern occurs, demonstrate causation, or guarantee that the same pattern will hold for new observations. This use of scatterplot smoothers is aligned with the SOA Exam PA (Predictive Analytics) syllabus.

Definition: A scatterplot smoother is a curve estimated from the observed pairs \((x_i,y_i)\) to summarize the central pattern of \(Y\) across values of \(X\). Unlike a straight-line summary, the curve can change slope as \(X\) changes. The plotted observations remain the data; the smooth is an estimated visual summary of them.

Reading a Smooth Curve

Begin with the points, then use the smooth to help describe the broad pattern. A segment that rises from left to right indicates that the smoothed response tends to increase over that range of \(X\); a falling segment indicates a decrease. A bend, or a change from rising to falling, signals that one constant slope may be an inadequate visual summary. These statements describe the pattern in the plotted data, not a guarantee about every observation.

A simple way to understand smoothing is to average nearby responses. For example, with observations ordered by \(x\), a three-point moving average at an interior value \(x_j\) is

$$ \tilde{y}_j=\frac{y_{j-1}+y_j+y_{j+1}}{3}. $$

This local average uses the response at \(x_j\) and its two nearest neighbors in the ordered data. It is a useful hand-calculation device, but it is not the only kind of smoother. In particular, a moving average does not account for differences in the spacing of the \(x\)-values. LOESS, a commonly used scatterplot smoother, instead fits a local regression around each location.

Worked Example: Detecting a Peak That a Line Misses

A hypothetical portfolio study records a response score at seven predictor levels. The observations, already ordered by predictor, are \(y=(2,5,8,10,8,5,2)\) at \(x=0,1,2,3,4,5,6\). Compare the least-squares straight-line summary with three-point moving averages. State the method and the pattern, and identify the mistake of relying only on the line.

Straight-line summary. The mean predictor is \(\bar{x}=3\), and the mean response is \(\bar{y}=40/7\), because the response total is \(2+5+8+10+8+5+2=40\). The numerator of the least-squares slope is \( \sum (x_i-\bar{x})(y_i-\bar{y}) \). Since the response values are symmetric around \(x=3\), the paired products cancel: the contributions at \(x=0\) and \(x=6\), at \(x=1\) and \(x=5\), and at \(x=2\) and \(x=4\) sum to zero; the center contribution is also zero. The denominator is \( \sum (x_i-\bar{x})^2=9+4+1+0+1+4+9=28 \). Thus the slope is \(0/28=0\), and the fitted line is the constant \(40/7\).

Three-point moving averages. At \(x=1\), the mean is \((2+5+8)/3=15/3=5\). At \(x=2\), it is \((5+8+10)/3=23/3\). At \(x=3\), it is \((8+10+8)/3=26/3\). By symmetry, the averages at \(x=4\) and \(x=5\) are \(23/3\) and \(5\). The sequence is therefore \(5,23/3,26/3,23/3,5\): it rises to a central peak and then falls.

Conclusion and check. The straight-line slope is zero, while the local averages clearly rise and then fall. The checks are consistent with the original symmetric values: the smooth is symmetric around \(x=3\), and its maximum, \(26/3\), is centered at the largest observed response. The common mistake is to interpret the zero line slope as evidence of no pattern. The correction is to inspect whether the local trend changes direction.

How LOESS Builds a Local Curve

LOESS (locally estimated scatterplot smoothing) estimates a value near each target \(x_0\) by fitting a regression to observations around that target. For a local linear fit, the intercept at \(x_0\) is the smoothed value. Nearby observations receive more weight than observations farther away. One common distance-weighting rule is the tricube weight: if \(h\) is the distance from \(x_0\) to the edge of its selected neighborhood, an observation at \(x_i\) receives weight

$$ w_i(x_0)= \begin{cases} \left(1-\left(\frac{|x_i-x_0|}{h}\right)^3\right)^3, & |x_i-x_0| \leq h,\\ 0, & |x_i-x_0|>h. \end{cases} $$

The local linear fit chooses coefficients \(\beta_0\) and \(\beta_1\) to minimize the weighted sum of squared residuals. Centering the predictor at \(x_0\) makes \(\beta_0\) the fitted response at that target.

$$ \sum_i w_i(x_0)\left[y_i-\beta_0-\beta_1(x_i-x_0)\right]^2. $$

The chosen neighborhood size controls how local the fit is. In R's `loess`, the `span` setting controls the fraction of observations used in local fitting: a smaller span usually follows more local detail, while a larger span usually gives a smoother, less variable curve. The exact appearance depends on the data and settings. A smooth curve is therefore not a setting-free fact about the data.

Why local fitting works: In a weighted local constant fit, minimizing \( \sum_i w_i(y_i-\beta_0)^2 \) gives a weighted average. Differentiating with respect to \(\beta_0\) gives \( -2\sum_i w_i(y_i-\beta_0)=0 \), so \( \beta_0=\sum_i w_i y_i/\sum_i w_i \). Local linear fitting extends this idea by allowing a slope within the neighborhood. Because the fit is repeated at different \(x_0\) values, those local slopes can vary, and the resulting fitted values can trace curvature.

This description requires observed \(x\)-values around the locations being smoothed and usable response values for the fit. To interpret the smooth as an estimate of a conditional mean, \(E[Y\mid X=x]\) must exist. LOESS does not require a specified normal distribution for \(Y\) to draw a descriptive curve. Its purpose here is to summarize the plotted relationship, not to assert that a particular probability model is correct.

Worked Example: Finding Increasing Curvature

A hypothetical analyst observes responses \(y=(2,4,8,14,22)\) at predictor values \(x=0,1,2,3,4\). Calculate the three interior three-point averages, compare their increments, and check them against the straight-line summary. Explain what the comparison shows.

Local averages. At \(x=1\), the average is \((2+4+8)/3=14/3\). At \(x=2\), it is \((4+8+14)/3=26/3\). At \(x=3\), it is \((8+14+22)/3=44/3\). The change from the first average to the second is \((26-14)/3=4\), and the change from the second to the third is \((44-26)/3=6\). The local averages continue to rise, and their increments increase.

Line comparison. Here \(\bar{x}=2\) and \(\bar{y}=50/5=10\). The least-squares slope numerator is \((-2)(2)+(-1)(4)+(0)(8)+(1)(14)+(2)(22)=-4-4+0+14+44=50\). The denominator is \(4+1+0+1+4=10\), so the slope is \(50/10=5\). The intercept is \(10-5(2)=0\), giving fitted values \(0,5,10,15,20\) at \(x=0,1,2,3,4\).

Check and interpretation. Subtracting the fitted values from the observations gives residuals \(2,-1,-2,-1,2\). The residuals are positive at both ends and negative in the middle, consistent with a curved pattern around the line. The moving averages also show increasing increments. Together, these summaries suggest upward curvature in this small data set; neither proves that the pattern will continue beyond the observed range. A common mistake is to report only the positive slope and miss the changing rate of increase. The check is to compare local behavior across the range, not just the overall direction.

Adding a Smoother in R

In base R, a scatterplot can be paired with a LOESS fit. For a data frame `dat` containing numeric columns `x` and `y`, the following code fits a local linear smoother with a span of 0.75 and draws it over the observations:

R workflow: `fit <- loess(y ~ x, data = dat, span = 0.75, degree = 1)`; then `grid <- data.frame(x = seq(min(dat$x), max(dat$x), length.out = 100))`; then `pred <- predict(fit, newdata = grid)`; finally, `plot(dat$x, dat$y)` followed by `lines(grid$x, pred, col = "blue", lwd = 2)`.

The grid supplies locations at which to draw the estimated curve; it does not add observations to the data. Read the curve alongside the points, and describe only patterns supported by the plotted range. If a straight-line summary is also relevant, compare its shape with the smooth rather than assuming that one must replace the other.

1
Plot the observations.
Check the axes, units, and range before interpreting a curve.
2
Add a smoother.
Choose and report a reasonable smoothing setting, such as the LOESS span used in R.
3
Describe direction and bends.
State where the curve rises, falls, or changes direction, using the predictor range as context.
4
Keep the conclusion descriptive.
Do not interpret the curve alone as evidence of cause, future performance, or a correctly specified model.

Worked Example: A Modest Line Slope Hides a Turning Point

A hypothetical service team records response scores \(y=(3,5,8,10,9,6,4)\) at \(x=1,2,3,4,5,6,7\). Calculate three-point moving averages, then compare their pattern with the least-squares slope. Give a contextual conclusion and check the arithmetic.

Moving averages. At \(x=2\), the average is \((3+5+8)/3=16/3\). At \(x=3\), it is \((5+8+10)/3=23/3\). At \(x=4\), it is \((8+10+9)/3=27/3=9\). At \(x=5\), it is \((10+9+6)/3=25/3\). At \(x=6\), it is \((9+6+4)/3=19/3\). These values rise to a maximum at \(x=4\), then fall.

Line comparison. The predictor mean is \(4\), and the response mean is \(45/7\). The slope numerator is \((-3)(3)+(-2)(5)+(-1)(8)+(0)(10)+(1)(9)+(2)(6)+(3)(4)=6\). The denominator is \(9+4+1+0+1+4+9=28\), so the slope is \(6/28=3/14\), a small positive value. The intercept is \(45/7-(3/14)(4)=39/7\).

Check and interpretation. The moving averages increase from \(16/3\) to \(27/3\), then decrease to \(19/3\). The fitted line has a positive slope because its overall direction is slightly upward, but it does not show the turn in the local pattern. The common mistake is to call the relationship steadily increasing based only on that positive slope. The correction is to report both the overall line direction and the observed rise-then-fall pattern, without claiming a mechanism for the change.

Common Mistakes and Interpretation

  • Treating a smooth as the data. The curve is calculated from the observed points. It does not replace them; inspect both the points and the curve.
  • Calling a descriptive curve a predictive model. A fitted smoother can help reveal shape, but prediction requires additional modeling and validation work.
  • Ignoring the smoothing setting. Different spans can show different levels of detail. State the setting when reporting a LOESS result.
  • Extrapolating the visible shape. A curve drawn over the observed predictor range does not establish what happens outside that range.
  • Reporting only one overall direction. A positive or negative line slope can coexist with a change in direction. Describe meaningful bends in the smooth as well.

A clear interpretation is specific but cautious: for example, “The displayed smooth rises over the lower predictor values and falls over the upper values, suggesting a turning point in the observed range.” This describes the plot. It does not claim that changing the predictor would cause the response to change.

Check Your Understanding

For each problem, calculate the requested summary before interpreting the pattern.

  1. At \(x=0,1,2,3,4\), the responses are \(1,4,9,16,25\). Calculate the three interior three-point moving averages and state whether their successive increases are constant. (Answer: \(14/3, 29/3, 50/3\); the increases are \(5\) and \(7\), so they are not constant and the averages rise at an increasing rate.)
  2. At \(x=1,2,3,4,5\), the responses are \(12,9,7,8,14\). Calculate the three interior moving averages and describe the direction. (Answer: \(28/3, 8, 29/3\); the smooth falls to its middle value, then rises.)
  3. A scatterplot has a positive straight-line slope, but a LOESS curve rises at first and then falls. What should a descriptive summary say, and what should it not claim? (Answer: It should report the positive overall linear direction and the observed rise-then-fall pattern; it should not claim that the predictor causes the response to turn.)
  4. An analyst uses `loess(y ~ x, data = dat, span = 0.30)` and then changes the span to `0.85`. What general change in the curve should the analyst check for? (Answer: The smaller span will generally follow more local detail; the larger span will generally be smoother and show less local variation.)
Clinical Biostats