Unit 9, lesson 67

Transformations and Curved Relationships

How polynomial terms and log transformations let linear regression fit curved relationships, and why back-transformation of a log model gives a conditional median unless the lognormal error is taken into account.

  • Video 67
  • Multiple Regression and Model Building

Suppose the typical time to finish a task grows by roughly the same percentage, not the same number of minutes, with each extra level of difficulty, and the times spread out more as they grow. A straight line in minutes misses both the curve and the growing spread. Regression can still describe such a pattern, either by adding powers of the predictor or by modeling the logarithm of the response. This lesson explains both approaches and settles which original-scale quantity a model for log responses actually predicts.

Key terms

Polynomial regression

Definition A polynomial regression of degree kk models the conditional mean m(x)=E(Y∣x)m(x) = E(Y \mid x) of a response YY given a numerical predictor xx as a polynomial in xx,

m(x)=∑j=0kβjxj,m(x) = \sum_{j=0}^{k} \beta_j x^j ,

that is, β0+β1x+⋯+βkxk\beta_0 + \beta_1 x + \cdots + \beta_k x^k. It estimates the coefficients by least squares with the columns 1,x,x2,…,xk1, x, x^2, \dots, x^k as predictors. The estimates are unique when the data contain at least k+1k + 1 distinct values of xx. When k≥2k \ge 2 and βk≠0\beta_k \ne 0 the mean function is curved, so the change in the mean for a one-unit step in xx depends on where the step starts. For a quadratic

m(x+1)−m(x)=β1+β2(2x+1).\begin{aligned} &m(x + 1) - m(x) \\[0.4em] &\quad = \beta_1 + \beta_2 (2x + 1) . \end{aligned}

In plain words A quadratic curve is built by adding a constant, a straight-line part and a squared part. Equal steps in xx give unequal changes in the mean, so no single number is the slope. Once x2x^2 is in the model, the coefficient β1\beta_1 is not the change per unit of xx, because changing xx also changes x2x^2. To compare two predictor values, evaluate the whole fitted function at both. Outside the range of the observed xx values a polynomial can bend sharply, and the data give no check on its shape there.

Example Take the mean function m(x)=2+3x+x2m(x) = 2 + 3x + x^2. Its values at x=0,1,2,3x = 0, 1, 2, 3 are 22, 66, 1212 and 2020, so successive one-unit steps raise the mean by 44, 66 and 88. In general m(x+1)−m(x)=4+2xm(x + 1) - m(x) = 4 + 2x. The coefficient 33 on xx matches none of these changes.

Linear in the coefficients

Definition A regression mean function is linear in the coefficients when it has the form

m(x)=β0f0(x)+β1f1(x)+⋯+βkfk(x),\begin{aligned} m(x) &= \beta_0 f_0(x) + \beta_1 f_1(x) \\[0.4em] &\quad + \cdots + \beta_k f_k(x) , \end{aligned}

where f0,…,fkf_0, \dots, f_k are known functions of the predictors that contain no unknown parameters. Each fj(x)f_j(x) can be computed from the data and used as a column of the design matrix, so least squares and the usual linear-model inference apply, under the same assumptions as for any other linear regression. Polynomials, the function β0+β1log⁡x\beta_0 + \beta_1 \log x for positive xx, and models with interaction terms are all linear in the coefficients. The mean function β0eβ1x\beta_0 e^{\beta_1 x} is not, because β1\beta_1 sits inside the exponential.

In plain words The word linear in linear regression describes how the unknown coefficients enter, each one multiplying a known column, and not the shape of the graph against xx. A curve in xx can be a linear model.

Example For the quadratic above, the design columns at x=0,1,2,3x = 0, 1, 2, 3 are the constant column, the values of xx and the values of x2x^2, and the mean vector is a weighted sum of them,

2[1111]+3[0123]+[0149]=[261220].\begin{aligned} &2\begin{bmatrix} 1 \\ 1 \\ 1 \\ 1 \end{bmatrix} + 3\begin{bmatrix} 0 \\ 1 \\ 2 \\ 3 \end{bmatrix} + \begin{bmatrix} 0 \\ 1 \\ 4 \\ 9 \end{bmatrix} \\[0.4em] &\quad = \begin{bmatrix} 2 \\ 6 \\ 12 \\ 20 \end{bmatrix} . \end{aligned}

A multiplicative model Y/y0=θ0eθ1xeεY/y_0 = \theta_0 e^{\theta_1 x} e^{\varepsilon} with θ0>0\theta_0 > 0 becomes linear in the coefficients after taking logs, log⁡(Y/y0)=log⁡θ0+θ1x+ε\log(Y/y_0) = \log \theta_0 + \theta_1 x + \varepsilon. A model with an added error, Y/y0=θ0eθ1x+εY/y_0 = \theta_0 e^{\theta_1 x} + \varepsilon, does not, because the logarithm of a sum does not split into a sum.

Log transformation

Definition Here log⁡\log is the natural logarithm, the inverse of exp⁡(u)=eu\exp(u) = e^u with e≈2.71828e \approx 2.71828. It is defined only for positive arguments, and log⁡(ab)=log⁡a+log⁡b\log(ab) = \log a + \log b for a,b>0a, b > 0. A log transformation replaces a positive variable by its logarithm. For a response Y>0Y > 0 measured in units, write log⁡(Y/y0)\log(Y/y_0) with a fixed positive reference amount y0y_0 in the same units, such as one minute, so that the logarithm is taken of a pure number. A response transformation models log⁡(Y/y0)\log(Y/y_0) in place of YY, which changes the scale on which the mean, the errors and their assumptions are stated. A predictor transformation uses log⁡(x/x0)\log(x/x_0) as a predictor, which changes the shape of the mean function in xx and leaves the response scale alone.

In plain words On a log scale equal ratios become equal differences. Doubling is always the same step, log⁡2≈0.693\log 2 \approx 0.693, whether it goes from 1 to 2 minutes or from 4 to 8 minutes. That suits outcomes whose typical level changes by percentages rather than by fixed amounts. Zero and negative values have no logarithm, and replacing YY by Y+cY + c to avoid zeros gives a different model whose conclusions depend on the chosen cc. A transformation does not repair dependent observations, biased sampling or predictions far outside the observed range.

Example Times of 11, 22, 44 and 88 minutes with y0=1y_0 = 1 minute have logs 00, 0.69310.6931, 1.38631.3863 and 2.07942.0794, which are equally spaced. If each extra unit of a predictor doubles the time, the log of the time rises by log⁡2\log 2 at every step, and a straight line in xx describes it exactly. Changing the reference to y0=60y_0 = 60 minutes subtracts log⁡60\log 60 from every value and changes only the intercept.

Lognormal error

Definition A positive random variable WW is lognormal with parameters μ\mu and σ2\sigma^2 when log⁡W∼N(μ,σ2)\log W \sim N(\mu, \sigma^2). The parameters are the mean and variance of log⁡W\log W, not of WW. A regression with lognormal error states that

log⁡(Y/y0)=β0+β1x+ε,\log(Y/y_0) = \beta_0 + \beta_1 x + \varepsilon ,

where, given xx, the error is ε∼N(0,σ2)\varepsilon \sim N(0, \sigma^2) with the same σ2\sigma^2 for every xx, and errors of different observations are independent. On the original scale this reads

Y=y0 eβ0+β1x eε,Y = y_0 \, e^{\beta_0 + \beta_1 x} \, e^{\varepsilon} ,

so the error multiplies the typical value instead of adding to it, and given xx the ratio Y/y0Y/y_0 is lognormal with parameters β0+β1x\beta_0 + \beta_1 x and σ2\sigma^2.

In plain words A symmetric bell curve of errors on the log scale becomes a right-skewed distribution on the original scale. An error of +0.4+0.4 multiplies the typical value by e0.4≈1.49e^{0.4} \approx 1.49, while an error of −0.4-0.4 multiplies it by e−0.4≈0.67e^{-0.4} \approx 0.67, so deviations above the typical value stretch farther in original units than deviations below it. The spread in original units grows in proportion to the typical level, while the spread on the log scale stays fixed. Normality and constant variance are assumptions about the log scale, so residual plots from the log-scale fit are where they are checked. Predictions still need checking on the scale where they will be used.

Example A teaching model declares

log⁡(Y/y0)=log⁡2+0.5x+ε\begin{aligned} &\log(Y/y_0) \\[0.4em] &\quad = \log 2 + 0.5x + \varepsilon \end{aligned}

with ε∼N(0,0.16)\varepsilon \sim N(0, 0.16), so the log error has standard deviation σ=0.4\sigma = 0.4. These coefficients are chosen for illustration, not estimated from data. At x=2x = 2 the quantity log⁡(Y/y0)\log(Y/y_0) is normal with mean log⁡2+1\log 2 + 1 and variance 0.160.16.

Conditional median

Definition A conditional median of YY given xx is a number MM with P(Y≤M∣x)≥1/2P(Y \le M \mid x) \ge 1/2 and P(Y≥M∣x)≥1/2P(Y \ge M \mid x) \ge 1/2. For a continuous conditional distribution whose density is positive on an interval and zero outside it, such as a lognormal one, it is the unique value with P(Y≤M∣x)=1/2P(Y \le M \mid x) = 1/2. A strictly increasing function carries the median of a variable to the median of the new variable, and in the lognormal error model the log error has median 00. Hence

median⁡(Y∣x)=y0exp⁡(β0+β1x).\begin{aligned} &\operatorname{median}(Y \mid x) \\[0.4em] &\quad = y_0 \exp(\beta_0 + \beta_1 x) . \end{aligned}

The same holds for any continuous log error with unique median 00, normal or not.

In plain words Exponentiating moves the middle of the log-scale distribution to the middle of the original-scale distribution, because an increasing function keeps values in the same order. Half of the modeled outcomes lie below the exponentiated value and half lie above it. Averages are not carried over in the same way.

Example In the teaching model at x=2x = 2 the mean and median of log⁡(Y/y0)\log(Y/y_0) are both log⁡2+1\log 2 + 1, so

median⁡(Y∣x=2)=2e y0≈5.437 y0.\begin{aligned} &\operatorname{median}(Y \mid x = 2) \\[0.4em] &\quad = 2e \, y_0 \approx 5.437 \, y_0 . \end{aligned}

One more unit of xx multiplies the median by e0.5≈1.6487e^{0.5} \approx 1.6487, an increase of about 64.964.9 percent, not an additive increase of 0.50.5 units of YY.

Back-transformation

Definition Back-transformation converts a value on the log scale to the original scale by exponentiating it and multiplying by y0y_0. Which original-scale quantity the result describes depends on the error model. Suppose the conditional mean of log⁡(Y/y0)\log(Y/y_0) is μ(x)\mu(x), so that log⁡(Y/y0)=μ(x)+ε\log(Y/y_0) = \mu(x) + \varepsilon with an error of mean 00. Unless the error equals 00 with probability 11, Jensen’s inequality gives E(eε∣x)>1E(e^{\varepsilon} \mid x) > 1, so y0eμ(x)y_0 e^{\mu(x)} is smaller than E(Y∣x)E(Y \mid x). Under the lognormal error model with constant variance σ2\sigma^2,

E(eε∣x)=eσ2/2,E(e^{\varepsilon} \mid x) = e^{\sigma^2/2} ,

and therefore

E(Y∣x)median⁡(Y∣x)=eσ2/2,\frac{E(Y \mid x)}{\operatorname{median}(Y \mid x)} = e^{\sigma^2/2} ,

that is, E(Y∣x)=y0exp⁡(β0+β1x+σ2/2)E(Y \mid x) = y_0 \exp(\beta_0 + \beta_1 x + \sigma^2/2). If the log-error variance changes with xx, the factor changes with xx too, and for a log error that is not normal the factor that turns y0eμ(x)y_0 e^{\mu(x)} into the mean is E(eε∣x)E(e^{\varepsilon} \mid x) under that distribution, while y0eμ(x)y_0 e^{\mu(x)} is the median only if the error has median 00. When the coefficients and σ2\sigma^2 are estimated, the values obtained by back-transformation are estimates and carry that uncertainty.

In plain words When the log error is symmetric about 00, as in the normal model, exponentiating a fitted mean log value estimates a typical value, the median, not an average. The long right tail on the original scale pulls the mean above the median, and more spread on the log scale pulls it further. Reporting the uncorrected value as a mean understates the mean, and any correction factor is only as trustworthy as the error model behind it.

Example In the teaching model σ2/2=0.08\sigma^2/2 = 0.08, so at x=2x = 2

E(Y∣x=2)=2e⋅e0.08 y0=2e1.08 y0≈5.889 y0,\begin{aligned} &E(Y \mid x = 2) \\[0.4em] &\quad = 2e \cdot e^{0.08} \, y_0 \\[0.4em] &\quad = 2e^{1.08} \, y_0 \approx 5.889 \, y_0 , \end{aligned}

about 8.38.3 percent above the median 2e y0≈5.437 y02e \, y_0 \approx 5.437 \, y_0. About 57.957.9 percent of the modeled outcomes at this xx fall below the mean. With a log-error variance of 0.640.64 instead, the median would stay the same and the mean-to-median factor would become e0.32≈1.377e^{0.32} \approx 1.377. Because σ2\sigma^2 does not depend on xx here, each unit of xx multiplies the mean by e0.5e^{0.5}, the same factor as for the median.

Check yourself

  1. A fitted quadratic is y^=1+4x−x2\hat{y} = 1 + 4x - x^2. Find the fitted change in the response from x=1x = 1 to x=2x = 2 and from x=2x = 2 to x=3x = 3. Is the coefficient 44 the change per unit of xx, and is this a linear model?
Show the answer

The fitted values at x=1,2,3x = 1, 2, 3 are 44, 55 and 44, so the changes are +1+1 and −1-1. In general the change from xx to x+1x + 1 is 4−(2x+1)=3−2x4 - (2x + 1) = 3 - 2x, which depends on xx, so the coefficient 44 is not a per-unit change. The model is linear in the coefficients, since the coefficients 11, 44 and −1-1 multiply the known columns 11, xx and x2x^2, even though the fitted curve rises and then falls.

  1. A model for repair times YY in hours states log⁡(Y/y0)=log⁡4+0.3x+ε\log(Y/y_0) = \log 4 + 0.3x + \varepsilon with y0=1y_0 = 1 hour, where xx is the number of faulty parts and ε∼N(0,0.25)\varepsilon \sim N(0, 0.25) for every xx. A report exponentiates the mean log value at x=0x = 0 and calls the result, 44 hours, the average repair time. What does 44 hours describe under this model, and what is the conditional mean?
Show the answer

The log error is normal with mean 00, so its median is also 00, and exponentiating the mean log value gives the conditional median. Under the model half of the repairs with no faulty parts take less than 44 hours and half take longer. The conditional mean is

E(Y∣x=0)=4e0.25/2 y0,E(Y \mid x = 0) = 4e^{0.25/2} \, y_0 ,

which is 4e0.125≈4.5334e^{0.125} \approx 4.533 hours, about 13.313.3 percent higher. Calling 44 hours the average understates the mean by about half an hour, and it presents a statement about a typical repair as a statement about the long-run average. With estimated coefficients and an estimated variance both numbers would be estimates.

  1. In the same model, someone says that each extra faulty part adds 0.30.3 hours to the mean repair time. Correct the claim. What changes if the log-error variance is 0.250.25 at x=0x = 0 but 0.450.45 at x=1x = 1?
Show the answer

The slope 0.30.3 is a change on the log scale. One more faulty part multiplies the median by e0.3≈1.350e^{0.3} \approx 1.350, an increase of about 3535 percent, and with a constant log-error variance it multiplies the mean by the same factor. In hours the change depends on the starting point. From x=0x = 0 to x=1x = 1 the median rises from 44 to about 5.3995.399 hours, an increase of about 1.401.40 hours, and the mean rises from about 4.5334.533 to about 6.1186.118 hours. If the variance grows from 0.250.25 to 0.450.45, the median still grows by the factor e0.3e^{0.3}, but the ratio of the means becomes

e0.3+(0.45−0.25)/2=e0.4≈1.492,\begin{aligned} &e^{0.3 + (0.45 - 0.25)/2} \\[0.4em] &\quad = e^{0.4} \approx 1.492 , \end{aligned}

so the factor for the median no longer describes the mean.

How this lesson connects