Before you start
You will be able to
- Transform y with a natural log, interpret the coefficients as percentage changes (approximately 100b, exactly 100(eᵇ − 1)) and back-transform a prediction and its interval.
- Use polynomial and hinge terms to follow a curve or a threshold, and choose between them with residual plots and fit statistics.
- Calculate leverage, studentized residuals and Cook’s distance, and use them to tell outliers, high-leverage points and influential points apart.
- Handle errors and genuine extremes correctly: check the source, correct, report with and without, and never delete a point because it spoils a result.
Review first
- Assumptions and diagnostics — The residual plots and tests that show when a model needs changing, the build-up term, and the funnel this lesson repairs.
- Categorical predictors and interactions — Dummy variables, used here for the festival days.
- Curves, thresholds and restricted ranges — The threshold in the peak wait and the hinge transformation, met there as correlations.
- Outliers and influence — The footfall slip and the festival days, and leave-one-out influence on a correlation.
Fictional case A fictional coffee business founded by Ana Lim. It starts as a catering and barista-training service, opens its own cafés and an online shop for beans, and plans a canned cold brew.
The idea
Fictional case
The diagnostics left Ana with three problems. Harbourside’s footfall model has a funnel: busy days vary by more people than quiet days, so a single prediction interval is wrong for both. The peak wait doesn’t rise in a straight line with the load on the baristas. And her raw log still contains the slip she found in B05, Millbrook’s 14 September footfall typed as 2,566 instead of 256, along with five festival days that are real but extreme. Each calls for a different response.
Logs: when effects are percentages
Footfall grows by percentages. A weekend adds a share of an ordinary day’s trade, not a fixed number of people, and the same is true of a warm day or a festival. When effects multiply like this, a model of footfall itself fits a straight-line sum to a product, and the residuals fan out as the level rises. Taking the natural log transformation of y turns products into sums: if footfall = base × weekend effect × weather effect, then ln(footfall) = ln(base) + ln(weekend effect) + ln(weather effect), which is exactly the form a regression fits.
Ana refits Harbourside’s model with ln(footfall) as y and the same predictors as the last lesson’s final model (the high, rain, weekend, festival and the opening build-up, 0 on 1 March rising to 1 on 29 June). The funnel goes: Breusch–Pagan falls from LM = 67.8 in levels to 5.4 in logs, a p-value of 0.37. The time order stays clean too: the residuals’ monthly averages all lie within ±0.017 and Durbin–Watson is 1.97. The build-up term matters here as much as in levels. With a straight-line day count in its place, the log model’s residuals average −0.052 in March, +0.036 in June and −0.027 in December: an arch, because a straight line assumes the build-up never stopped.
Reading a log model’s coefficients
In a model for , a slope b is the change in ln y for a one-unit change in x, holding the other predictors fixed. Since a change of b in a log multiplies y by , the slope means a change of %. For small b this is close to 100b%: the slope for the day’s high, 0.0109, means about 1.1% more people per °F warmer, on average over the year’s range of temperatures, holding rain, the weekend, festivals and the build-up fixed. For larger b the shortcut fails. The weekend coefficient, 0.469, does not mean 47% more people: , so weekend days have about 60% more footfall than weekdays with the same weather. The festival coefficient, 0.891, means times an ordinary weekend day’s footfall.
These are the numbers the café’s fictional log was built from, recovered: footfall at Harbourside was generated with 1.1% more per °F near 60°F (not exactly a constant percentage: the planted rise is about 1.7% per °F on the log’s coldest day, 28°F, and 0.8% on its hottest, 94°F, so 1.1% is an average), a weekend base 61% above the weekday one, festival days at 2.3 times normal (inside the coefficient’s 95% interval, 2.23 to 2.66) and a build-up from 72% to full trade over four months. The levels model could not express any of these as single numbers, because in levels the weekend adds more people on a warm day than on a cold one.
Bending a line: polynomial and hinge terms
The peak wait rises with the load, the peak transactions per barista hour, but not in a straight line. B05 found the shape: waits barely change up to a load of about 7 and climb ever faster beyond it. A regression can follow a curve by adding a transformed copy of x as another predictor. A polynomial regression adds x², and perhaps x³. A hinge transformation adds max(0, x − 7), which is zero up to the threshold and rises after it; squaring it, max(0, x − 7)², lets the rise steepen.
Across 612 café-days the straight line accounts for 68.9% of the variation in waits, the quadratic 94.3% and the squared hinge 96.1%. The squared hinge’s coefficient, 0.226 minutes per squared transaction above 7, is close to the 0.22 the fictional data were built with. At a load of 10 the model predicts a wait of 3.7 minutes, at 12 a wait of 7.3. The quadratic’s good R² hides a fault the figure shows: a parabola must turn somewhere, and this one has its minimum at a load of 6.0, so it predicts that waits rise again as the load falls, 4.0 minutes at a load of 2, where the 11 days with a load below 4 averaged 1.8.
Influence: the rows that move a regression
In B05 one mistyped footfall cut the correlation between footfall and transactions from 0.981 to 0.679. In a regression the same day does more visible damage. With the slip, the least-squares slope of transactions on footfall across all 612 café-days is 0.260 transactions per person through the door; corrected, it is 0.560. One row among 612 more than halved the slope.
Three numbers say why. The slip’s footfall, 2,566, lies so far from the mean that its leverage (in regression) is 0.551: more than half of its own fitted value is set by its own y. Its residual from the line fitted with it is −583.5 transactions, and its studentized residual, the residual divided by the standard deviation it should have, is −23.8 (−91.2 when the standard deviation is estimated without it). Cook’s distance, which combines the two into the change in all the fitted values when the row is left out, is 349; the conventional point at which to worry is 1. A point that is far out in x and far from the line is influential; a point that is only one of those may not be.
In depth
The log transformation in detail
Logs need y > 0; for counts that can be zero, B02’s is a common workaround, though its coefficients no longer read as percentages. Logging y changes three things at once: the form (effects multiply), the spread (errors proportional to the level become constant in logs) and the shape of the errors (a right skew in y often becomes symmetric in ln y). Check all three after the change; here the residual standard deviation in logs, 0.098, means a typical day is within about 10% of its predicted footfall, busy or quiet.
Logs can go on x instead, or on both. With as the predictor, a 1% rise in x goes with a change of about b/100 in y. With logs on both sides, b is an elasticity: a 1% rise in x goes with about b% change in y. Choose the scale from how the effect works (fixed amounts or percentages) and from the residual plots, not from which version has the larger R².
Back-transforming a prediction
A log model predicts . Exponentiating gives , which estimates the median footfall for that day, not the mean, because the exponential stretches the upper half of the errors more than the lower. To estimate the mean, multiply by a correction: Duan’s smearing factor, the average of over the log model’s residuals, or if the errors are normal. For Harbourside both are 1.005, a 0.5% correction, small because s is small; with s = 0.5 it would be 13%.
Prediction intervals back-transform directly: exponentiate the two limits. The result is not symmetric about the prediction, and it widens with the level, which is the point. For a dry 75°F Saturday after the build-up the log model predicts a median of 227 people, with a 95% prediction interval from 187 to 276. For a dry 45°F Tuesday it predicts 103, from 85 to 124. The levels model gives each day the same ±33 people, too narrow for the Saturday and too wide for the Tuesday.
Polynomial and hinge terms
| Model | Terms besides the load | R² | s (min) | AIC |
|---|---|---|---|---|
| Straight line | none | 0.689 | 1.11 | 1,867 |
| Quadratic | load² | 0.943 | 0.48 | 835 |
| Hinge at 7 | max(0, load − 7) | 0.857 | 0.75 | 1,395 |
| Squared hinge at 7 | max(0, load − 7)² | 0.961 | 0.39 | 596 |
Lower AIC is better for models of the same y on the same rows. All four have three or fewer coefficients.
A polynomial is quick to try, but its shape is global: a squared term bends the whole curve, including where the data are flat, and higher powers swing wildly at the ends of the data. A hinge is local: it leaves the line alone below the threshold and changes it only above. The price is choosing the threshold. Ana’s came from B05’s bands, chosen before this fit; trying thresholds from 5 to 9 gives R² from 0.930 to 0.961, highest at 7, so the data agree. Choosing a threshold by searching many values and picking the best fit overstates how well the model will do on new data; if you search, say so, and check on data not used to choose it.
Interpret transformed terms through predictions, not single coefficients. In the squared-hinge model the effect of one more transaction per barista hour depends on where you start: below 7 it is the load slope, −0.005 minutes (indistinguishable from zero); at a load of 10 it is minutes. The squared term on its own is not “the effect of load”.
Leverage
Leverage measures how far a row’s predictor values are from the centre of all the rows’. With one predictor, . Leverages lie between 1/n and 1 and average p/n, where p counts the coefficients; a common rule of thumb flags rows above 2p/n (here 2 × 2 ÷ 612 = 0.0065). For the slip, , 84 times the flag. With several predictors leverage comes from the hat matrix, but the idea is the same: an unusual combination of x values, such as a festival day on a weekend with heavy rain, can have high leverage even if no single value is extreme.
Studentized residuals
Residuals of high-leverage rows are small by construction, because the line is pulled towards them. Dividing by the standard deviation each residual should have puts them on one scale: the internally studentized residual is . The externally studentized (deleted) version, , uses s from the fit without row i, so a large error can’t hide by inflating s; it follows a t distribution on n − p − 1 df if the model is right. Penn State’s regression notes call a row with an externally studentized residual beyond ±3 an outlier; ±2 is a common, looser convention for a first look, especially in small samples. Treat both as warning flags, not rules.
Cook’s distance
Cook’s distance asks how much all the fitted values would move if row i were left out, scaled by the model’s precision: . It is large only when both the residual and the leverage are. Rules of thumb vary: Penn State’s notes suggest a closer look above 0.5 and call values above 1 quite likely influential; others use 4/n. The most useful check is visual, a value that stands far apart from the rest.
The festival days show why both parts matter. In a log model of Harbourside’s footfall without a festival term, the five festival days have studentized residuals from 5.0 to 7.4, extreme outliers. But they are ordinary weekend days in their predictor values, with leverages from 0.015 to 0.024, and their Cook’s distances are 0.08 to 0.20: above the 4/n line (0.013), far below 0.5. They still matter: all five fell on weekends, and leaving them in without a term raises the weekend coefficient from 0.469 to 0.521 (60% to 68% more people). Once corrected, the most influential rows in the footfall-and-transactions regression are the same festival days (29 May has the largest Cook’s distance, 0.56): their footfall is high, and they sit below the pooled line because Harbourside converts about 47% of visitors where Millbrook converts 55%, a sign that the pooled model needs a café term.
What to do with an unusual point
- Check it against the source. The door counter’s export showed 256 for 14 September: an error. Correct it, and record the change.
- If it is real, ask whether the model is missing something. The festival days are real and different. A festival dummy gives them their own coefficient; the other coefficients are then almost exactly those from leaving the five days out (weekend 0.4691 either way), and the model still describes them.
- Report with and without. When a real point moves a conclusion and no term explains it, show both fits and say which conclusions depend on it.
- Never delete a point because it spoils the result, and don’t delete automatically by a cut-off. Rules such as |t| > 3 or D > 1 tell you where to look, not what to remove.
Formulas
Percentage change from a coefficient in a model for ln y
- The slope for x in the model for ln y, holding the other predictors fixedper unit of x
- Percentage change in y for a one-unit rise in x%
Assumes. The model for ln y is right; the change is in the median of y (and in the mean, if the error spread doesn’t change with x).
In Excel: =EXP(b)-1; for a change of d units, =EXP(d*b)-1. The shortcut 100b is within 0.5 percentage points while |b| ≤ 0.1.
Back-transforming a log prediction
- The log model’s predictionlog units
- Estimated median of yunits of y
- The log model’s residualslog units
- Estimated mean of y (Duan’s smearing estimate)units of y
- Number of rows in the log model
Assumes. The errors in logs have the same distribution for every row.
With normal errors the factor is about e^(s²/2). Prediction interval: exponentiate both limits.
A squared hinge term
- The threshold, chosen before fitting (from earlier work or the business)units of x
- The slope below the thresholdunits of y per unit of x
- How fast the slope grows above the thresholdunits of y per (unit of x)²
- The predictorunits of x
- Fitted valueunits of y
The slope at x is b₁ + 2b₂ max(0, x − c). In Excel: =MAX(0,B2-7)^2 as a new column.
Leverage with one predictor
- Row i’s leverage, between 1/n and 1
- Row i’s predictor valueunits of x
- Mean of the predictorunits of x
- Number of rows
Leverages average p/n; rows above 2p/n are often flagged. With several predictors, hᵢ is the i-th diagonal of the hat matrix X(X⊤X)⁻¹X⊤.
Studentized residuals
- Row i’s residualunits of y
- Residual standard error of the fit with every rowunits of y
- Row i’s leverage
- Number of coefficients, counting the intercept
- Internally studentized residual
- Externally studentized residual, which uses s from the fit without row i
- Number of rows
tᵢ follows a t distribution on n − p − 1 df if the model is right.
Cook’s distance
- Internally studentized residual
- Leverage
- Number of coefficients, counting the intercept
- How far all the fitted values move when row i is left out, in units of the model’s precision
Look closer above 0.5; above 1 the row is quite likely influential. Some analysts flag values above 4/n.
Worked examples
Fictional figures
Harbourside’s model for ln(footfall) has slopes of 0.0109 for the day’s high (°F), −0.784 for rain (inches) and 0.469 for the weekend dummy. Turn each into a percentage change in footfall, holding the other predictors fixed: for one °F, for 0.1 inch of rain and for a weekend day.
- High: , about 1.1% more people per °F. The shortcut 100b gives 1.09%, near enough because b is small.
- Rain, per 0.1 inch: multiply first, , then , about 7.5% fewer people. (Per whole inch, , 54% fewer, where the shortcut would say 78%.)
- Weekend: , about 60% more people than on a weekday with the same weather, not the 47% the shortcut gives.
Answer. About +1.1% per °F, −7.5% per 0.1 inch of rain, +60% on a weekend day, each holding the other predictors fixed.
What it means. Because the effects are percentages, a 10°F warmer day adds about 12% (e^0.109 − 1) whether it is a Tuesday or a Saturday, though 12% of a Saturday is more people. That is the multiplicative pattern the funnel in the levels model was hinting at.
Fictional figures
In the raw log, Millbrook’s 14 September footfall reads 2,566 (true value 256) with 130 transactions. Across the 612 café-days, raw footfall has mean 186.8 and Σ(x − x̄)² = 10,305,017. The raw regression of transactions on footfall has slope 0.260, intercept 46.4 and s = 36.5. Calculate the slip’s leverage, residual, internally studentized residual and Cook’s distance, and compare the slope after correction (0.560).
- Leverage: . The flag is 2p/n = 4/612 = 0.0065.
- Residual: the raw line predicts transactions, so .
- Internally studentized: .
- Cook’s distance: (349 unrounded), against a threshold of 1.
- Correct the value to 256 and refit: the slope becomes 0.560 and R² rises from 0.461 to 0.963. The corrected day has leverage 0.003 and a studentized residual of −0.61: an ordinary day.
Answer. Leverage 0.551, residual −583.5, studentized residual −23.8, Cook’s distance about 349. Corrected, the slope is 0.560 transactions per visitor instead of 0.260.
What it means. The slip was both far out in x and far from the line, so it dragged the line towards itself: the raw slope implies that only one visitor in four buys, when the café converts about half. One check of the source changes the answer; a regression run on the raw log would have been confidently wrong.
Fictional figures
Ana rosters Harbourside’s peak so that waits stay short on all but the busiest days. She wants an upper figure for footfall that only about one dry day in forty should exceed (the top of a 95% prediction interval), for a 75°F Saturday and a 45°F Tuesday after the build-up. Compare the log model with the levels model with the same predictors.
- Log model: predicted ln(footfall) is 5.427 for the Saturday and 4.631 for the Tuesday. Exponentiated, the medians are 227 and 103 people.
- Prediction intervals in logs, exponentiated: Saturday 187 to 276, Tuesday 85 to 124. The upper limits are 21% above the median on both days: the width is a percentage.
- Levels model: the Saturday’s interval is 180 to 246 and the Tuesday’s 68 to 134, ±33 people on both.
- Which to trust: the levels model’s residuals fan out (Breusch–Pagan LM = 67.8 with these predictors); the log model’s don’t (5.4). Over the year, its constant width is too narrow on busy days and too wide on quiet ones.
- Upper figures for the roster: about 276 people for the Saturday, not 246; about 124 for the Tuesday, not 134.
Answer. Roster for about 276 people on a warm Saturday and 124 on a cold Tuesday (log model), not the levels model’s 246 and 134.
What it means. The log model shifts staff from quiet days to busy ones: about 30 more people to plan for on the Saturday and 10 fewer on the Tuesday. The medians, 227 and 103, are what a typical such day brings; for the stock order, which follows the average, multiply by the smearing factor (1.005), a difference of about one person.
Explore
Common mistakes
“The log coefficient is 0.47, so weekends have 47% more people.”
The shortcut 100b works only for small b. The exact change is 100(e^b − 1): e^0.469 − 1 = 60%. For negative coefficients the shortcut overstates the fall: rain’s −0.784 per inch is a 54% fall, not 78%.
Exponentiating a log prediction and calling it the mean
e to the predicted log is an estimate of the median. Totals and averages built from it run low; multiply by a smearing factor (the mean of e to the residuals). For Harbourside it is 1.005; for noisier data it can be much larger.
Choosing between y and ln y by R²
The two R²s measure the share of variation in different quantities. Decide by the residual plots and by errors on a common scale, and by whether the effects work as amounts or as percentages.
Reaching for a high-degree polynomial
A polynomial bends everywhere. The quadratic for the wait has R² = 0.943 and still predicts that waits rise as the load falls below 6. A hinge at a threshold you can justify follows the data with fewer surprises; extrapolate neither.
“It has a huge residual, so it’s influential”, or “It has high leverage, so delete it”
Influence needs both: in the log model without a festival term, the festival days’ studentized residuals of 5 to 7 come with Cook’s distances of 0.2 or less, and a high-leverage point that sits on the line changes nothing. Measure with Cook’s distance, then go to the source.
Deleting the points that spoil the result
Points removed because the result looks better without them make every later p-value and interval misleading. Correct errors from the source, model real differences (a festival dummy), and report with and without when a real point decides a conclusion.
Guided practice
Work through these with hints when you need them. Every hint and every worked solution is there to be used.
Loading four questions…
On your own
No hints this time. Answer, check, and read the feedback; the solution appears once you have answered or if you ask for it. Your first answers count toward mastery.
Loading six questions…
In practice
Fictional case
Ana’s change record for the 2027 log: “Millbrook, 14 Sep, footfall typed 2,566, door counter says 256: corrected (Cook’s D for the raw value 349). Festival days (5, Harbourside): real, kept, modelled with a dummy; without it the weekend effect reads 68% instead of 60%. Harbourside footfall now modelled in logs with a build-up term (to 29 June): residuals pass the spread and time-order checks; coefficients reported as percentages; roster limits from the log model’s prediction intervals.”
Records like this are routine wherever regression output drives money. A marketing analyst at an online retailer, say, fits sales in logs so that advertising effects come out as elasticities, checks the influence of the week a competitor’s site was down, and files a note saying which weeks were corrected, which were modelled and which were left as they are. The note matters as much as the model: anyone who reruns the analysis, or audits it, can see that no row was dropped to get a better answer.
Two habits separate a careful analyst from a hasty one. The first is choosing transformations for reasons that would hold before seeing the fit: effects that are percentages, a threshold known from the operation, a build-up known to have happened. Transformations chosen by trying many and keeping the best fit look better than they are on the next year’s data. The second is reporting results in the units a decision-maker uses. A log coefficient of 0.0109 means nothing to a café manager; “about 1% more people for every degree warmer” does, and so does “plan for 276 people on a warm Saturday”.
Finally, the influence checks belong at the start of an analysis as well as the end. Plotting the data and screening for leverage before fitting would have caught the slip in minutes. Done last, after a model has been presented, the same check becomes an awkward correction.
Knowledge check
Five questions, one attempt each, no hints. Score 80% or more to complete the lesson; you can take it again at any time.
Loading five questions…
Summary
- When effects are percentages, model ln y: products become sums, a funnel often disappears (Harbourside: Breusch–Pagan 67.8 in levels, 5.4 in logs), and coefficients read as percentage changes, exactly 100(eᵇ − 1), about 100b when b is small.
- Back-transform a log prediction with eˣ to get a median; multiply by a smearing factor for a mean; exponentiate the interval’s limits, which then widen with the level.
- Polynomial terms bend the whole curve; hinge terms, max(0, x − c) or its square, change it only beyond a threshold. The wait’s squared hinge at 7 has R² = 0.961 against 0.689 for a line, and a coefficient close to the planted 0.22.
- Leverage measures how unusual a row’s x values are (average p/n); studentized residuals measure how far it is from the line on a common scale; Cook’s distance combines them into influence.
- The raw slip had leverage 0.551, studentized residual −23.8 and Cook’s distance 349, and halved the slope; in a log model without a festival term the festival days are outliers with little leverage and Cook’s distances of 0.2 or less, but they still shift the weekend coefficient.
- Check unusual points against the source; correct errors; model real differences; report with and without; never delete a point to improve a result.
Key formulas
- Percentage change from a coefficient in a model for ln y
- Back-transforming a log prediction
- A squared hinge term
- Leverage with one predictor
- Studentized residuals
- Cook’s distance
Terms introduced
- Polynomial regression
- A regression that includes powers of a predictor (x², x³, …) as extra predictors so the fitted relationship can curve; the curve’s shape is set over the whole range of x at once.
- Leverage (in regression)
- How far a row’s predictor values are from the centre of all the rows’, measured by its hat value hᵢ between 1/n and 1. High-leverage rows pull the fitted line towards themselves.
- Studentized residual
- A residual divided by the standard deviation it should have given its leverage, so that residuals can be compared on one scale; the externally studentized version estimates that standard deviation without the row itself.
- Cook’s distance
- A measure of a row’s influence on a regression: how far all the fitted values move when the row is left out, scaled by the model’s precision. It is large only when a row has both a large residual and high leverage.
- Smearing estimate
- A correction for back-transforming predictions from a model of ln y: multiply e to the predicted log by the average of e to the residuals, to estimate the mean of y rather than its median.
Sources
- Penn State Eberly College of Science. STAT 462: Applied Regression Analysis, 7.2, “Log-transforming Only the Response for SLR”. online.stat.psu.edu/stat462/node/153
- Penn State Eberly College of Science. STAT 462: Applied Regression Analysis, 9.3, “Identifying Outliers (Unusual Y Values)”. online.stat.psu.edu/stat462/node/172
- Penn State Eberly College of Science. STAT 462: Applied Regression Analysis, 9.4, “Studentized Residuals”. online.stat.psu.edu/stat462/node/247
- Penn State Eberly College of Science. STAT 462: Applied Regression Analysis, 9.5, “Identifying Influential Data Points”. online.stat.psu.edu/stat462/node/173
- Microsoft Support. “LN function”. support.microsoft.com/en-us/Excel/functions/ln-function
