Tutorials › Exam PA - Predictive Analytics › Log One Plus for Predictors With Zeros

Exam PA · Transformations, Scaling, and Binning · Tutorial 199 of 966

Log One Plus for Predictors With Zeros

Use log1p to retain zero-valued observations while compressing large predictor values, and assess the consequences of alternative offsets.

Intermediate 10 min read

What You'll Learn

  • Define the domain and formula for the log1p transformation.
  • Calculate transformed values at zero, near zero, and larger predictor values.
  • Explain the transformation’s slope, curvature, and large-value behavior.
  • Compare log1p with adding a chosen constant before taking a logarithm.
  • Apply the transformed predictor in a specified logistic regression model.
  • Identify domain, unit, and calculation traps when using log1p.

Why Use Log1p When a Predictor Can Be Zero?

A log transformation can reduce the influence of very large predictor values and represent diminishing increases in the transformed scale. But the natural logarithm of zero is undefined. If a predictor is a nonnegative count, amount, or duration and some observations are zero, the transformation \( \log(x) \) cannot be applied to every observation as recorded.

The transformation \( \log(1+x) \), commonly called log1p, gives a finite transformed value at zero and increases more slowly as \(x\) grows. Its practical behavior depends on the units in which \(x\) is measured, so the transformation should be chosen and documented with the predictor’s scale in mind.

Definition: For a real-valued input \(x>-1\), the log1p transformation is \(g(x)=\log(1+x)\), where \(\log\) denotes the natural logarithm. When the predictor is intended to be nonnegative, its values satisfy \(x\geq 0\), and \(g(0)=0\). Although the mathematical function is also defined for \(-1<x<0\), those inputs may not be meaningful or appropriate for a predictor that is intended to represent a nonnegative quantity.

The domain matters in implementation. In R, log1p(x) computes \(\log(1+x)\) and is designed to calculate it accurately, especially when \(x\) is close to zero. It does not make arbitrary negative values safe: the real-valued function is undefined at \(x=-1\) and for \(x<-1\). For a nonnegative predictor, check that the observed values meet that intended condition before transforming them.

What the Transformation Does

At zero, log1p has no special exception: it maps zero to zero. For small positive values, it is close to the original value. For large values, it grows approximately like the logarithm of \(x\), so increases in the original predictor produce smaller increments in the transformed predictor.

$$ g(0)=0,\qquad g'(x)=\frac{1}{1+x},\qquad g''(x)=-\frac{1}{(1+x)^2} $$

For \(x\geq 0\), the derivative is positive, so the transformation is increasing. Its second derivative is negative, so it is concave: equal increases in \(x\) produce smaller increases in \(g(x)\) at larger starting values. The derivative at zero is 1, but the numerical interpretation of a “unit” of \(x\) depends on how the predictor is measured.

Justification: Differentiating \(g(x)=\log(1+x)\) gives \(g'(x)=1/(1+x)\); differentiating again gives \(g''(x)=-1/(1+x)^2\). Thus \(g'(x)>0\) and \(g''(x)<0\) throughout \(x\geq0\): log1p is increasing and concave there. For values near zero, the integral form \(g(x)=\int_0^x 1/(1+t)\,dt\) shows why \(g(x)\) is close to \(x\): the integrand is close to 1 when \(t\) is close to zero. For large \(x\), writing \(\log(1+x)=\log(x)+\log(1+1/x)\) shows that the second term tends to zero, so log1p behaves approximately like \(\log(x)\).

Worked Example: Comparing Transformed Increments

A hypothetical service team records a nonnegative predictor \(x\), the number of service events for each account during a specified period. Consider accounts with \(x=0,1,9,\) and \(99\). Calculate log1p for each value, then compare the transformed increase over the intervals from 0 to 1 and from 9 to 99. Use natural logarithms and round transformed values to four decimal places.

Method. Apply \(g(x)=\log(1+x)\) to each original value. The added 1 is part of the transformation; it is not an estimate fitted from these four observations.

At \(x=0\), \(g(0)=\log(1)=0\). At \(x=1\), \(g(1)=\log(2)=0.693147\ldots\), which rounds to 0.6931. At \(x=9\), \(g(9)=\log(10)=2.302585\ldots\), which rounds to 2.3026. At \(x=99\), \(g(99)=\log(100)=4.605170\ldots\), which rounds to 4.6052.

The transformed increase from \(x=0\) to \(x=1\) is \(0.693147\ldots-0=0.693147\ldots\), or 0.6931 to four decimals. The increase from \(x=9\) to \(x=99\) is \(\log(100)-\log(10)=\log(10)=2.302585\ldots\), or 2.3026. The second interval spans 90 original units, compared with 1 unit in the first, yet its transformed increase is only about 3.32 times as large, not 90 times as large.

Check. Both increments are positive, consistent with an increasing transformation. The large interval has a smaller transformed increase per original unit, consistent with concavity. A common mistake would be to compare only the original interval lengths and conclude that the transformed increase must be 90 times as large.

Log1p and an Arbitrary Offset Are Not Always Equivalent

A related way to deal with a zero is to add a positive constant \(c\) and use \(\log(x+c)\). Log1p is the particular choice \(c=1\) in the units in which \(x\) is recorded. If a different constant is chosen, the resulting transformation is generally not just log1p plus a fixed amount. Its slope is \(1/(x+c)\), which changes with both \(x\) and the selected constant.

$$ h_c(x)=\log(x+c),\qquad h_c'(x)=\frac{1}{x+c},\qquad x+c>0 $$

The constant must have the same units as \(x\). For example, adding 1 to an amount measured in dollars means adding one dollar; adding 1 to the same amount measured in thousands of dollars means adding one thousand dollars. These are different transformations. A constant chosen only to avoid taking the logarithm of zero can therefore make results sensitive to measurement units and to a choice that may lack a substantive justification.

Worked Example: Assessing a Chosen Offset

A hypothetical analyst records a nonnegative amount \(x\) in thousands of dollars and is considering \(\log(x+10)\) instead of log1p. The constant 10 therefore represents ten thousand dollars. For \(x=0,4,\) and \(24\), compare each transformation’s increase from its value at zero. Use natural logarithms and round to four decimal places.

Method. Calculate both transformations at every stated value, then subtract each transformation’s value at zero. Subtracting the baseline is useful here because it distinguishes a change in shape from a constant difference in level.

For log1p, the values are \(\log(1)=0\), \(\log(5)=1.6094379\ldots\), and \(\log(25)=3.2188758\ldots\). Thus the increases from zero are 0, 1.6094, and 3.2189.

For the offset transformation, the values are \(\log(10)=2.3025851\ldots\), \(\log(14)=2.6390573\ldots\), and \(\log(34)=3.5263605\ldots\). The increases from its value at zero are \(0\), \(\log(14)-\log(10)=\log(1.4)=0.3364722\ldots\), and \(\log(34)-\log(10)=\log(3.4)=1.2237754\ldots\). Rounded to four decimals, these are 0, 0.3365, and 1.2238.

The two transformations do not have the same increases: at \(x=4\), for example, the increases are 1.6094 and 0.3365. They are not made equivalent by subtracting a constant. As a further unit check, the same amount represented as 4,000 dollars is \(x=4\) in thousands of dollars. Log1p then gives \(\log(5)=1.6094\); applying log1p to the dollar value instead gives \(\log(4001)=8.2943\). The numeric transformation changes when the unit convention changes.

Check. At \(x=0\), the offset transformation is finite because its argument is 10, while log1p is zero. The offset is not “free”: its value changes the curve, and it must be justified and stated in the predictor’s units. The mistake to avoid is treating a convenient constant as if it had no effect beyond making the logarithm defined.

Using a Transformed Predictor in a Model

A transformed predictor can be entered into a model in place of the original predictor. The transformation changes the predictor values supplied to the model; it does not itself specify the response distribution, link function, or whether the resulting model is suitable. Those choices still need to be stated and assessed for the modeling problem.

For a binary response \(Y\), suppose the specified model is Bernoulli conditional on \(x\), with a logistic link and linear predictor \(\eta(x)=-2+0.6\log(1+x)\). Then \(P(Y=1\mid x)=1/(1+\exp(-\eta(x)))\). This formula uses the logit link: \(\log(p/(1-p))=\eta(x)\), where \(p=P(Y=1\mid x)\).

Worked Example: Calculating a Logistic-Model Prediction

A hypothetical screening model uses a nonnegative count \(x\). Assume the conditional response \(Y\mid x\) is Bernoulli, and the fitted logistic model is \(\eta(x)=-2+0.6\log(1+x)\). Calculate the predicted probability of \(Y=1\) for \(x=0\), \(x=4\), and \(x=24\). Keep full precision until the final step and round probabilities to four decimal places.

Method. Transform the count, calculate the linear predictor, and then apply the inverse-logit formula \(p=1/(1+\exp(-\eta))\). The Bernoulli assumption identifies the binary response model; the numerical prediction follows from the stated logistic form.

For \(x=0\), \(\log(1+x)=\log(1)=0\), so \(\eta(0)=-2+0.6(0)=-2\). Therefore \(p=1/(1+\exp(2))=0.1192029\ldots\), which rounds to 0.1192.

For \(x=4\), \(\log(1+x)=\log(5)=1.609437912\ldots\). Thus \(0.6\log(5)=0.965662747\ldots\), and \(\eta(4)=-2+0.965662747\ldots=-1.034337253\ldots\). The probability is \(1/(1+\exp(1.034337253\ldots))=0.26224\ldots\), which rounds to 0.2622.

For \(x=24\), \(\log(1+x)=\log(25)=3.218875825\ldots\). Thus \(0.6\log(25)=1.931325495\ldots\), and \(\eta(24)=-2+1.931325495\ldots=-0.068674505\ldots\). The probability is \(1/(1+\exp(0.068674505\ldots))=0.48283\ldots\), which rounds to 0.4828.

Check. The transformed predictor and the linear predictor both increase across these three \(x\) values, and so does the predicted probability under the logistic link. In particular, retaining full precision gives \(\eta(4)\approx-1.0343\) and probability 0.2622. Rounding intermediate quantities too early can change the final rounded answer.

Common Mistakes and Practical Checks

  • Taking \(\log(x)\) at zero. This is undefined. For a nonnegative predictor with zeros, log1p maps zero to \(\log(1)=0\).
  • Misstating the mathematical domain. Log1p is defined as a real-valued function for \(x>-1\), not just for \(x\geq0\). For a predictor intended to be nonnegative, however, the intended input range is \(x\geq0\). Inputs between \(-1\) and 0 are mathematically valid but may not make sense for that predictor.
  • Adding an offset without stating its units or rationale. A constant must be on the same measurement scale as \(x\), and changing it generally changes the transformation’s shape.
  • Assuming transformation guarantees a suitable model. Log1p changes the predictor scale; it does not guarantee that a chosen model captures the relationship adequately.
  • Rounding before the final calculation. Retain adequate precision in transformed values and linear predictors, especially when the requested answer is rounded to a fixed number of decimals.

In R, applying log1p(x) directly makes the intended transformation clear. As a basic check, verify that the predictor has the expected nonnegative values, inspect the transformed values at zero and at representative larger values, and use the same stated transformation when calculating predictions. If an offset other than 1 is proposed, document its value, units, and rationale rather than presenting it as interchangeable with log1p.

Key takeaway: Log1p is \(\log(1+x)\). For a nonnegative predictor it preserves zero, is increasing and concave, and behaves approximately like the original predictor near zero and like a logarithm for large values. Its implicit 1 is tied to the predictor’s measurement units; a different offset generally produces a different transformation.

Check Your Understanding

For each problem, use natural logarithms. Round numerical values to four decimal places unless the question specifies otherwise.

  1. A nonnegative count is \(x=0,3,\) or \(15\). Calculate log1p for all three values. (Answer: \(0.0000,\ 1.3863,\ 2.7726\), respectively.)
  2. A binary response has a Bernoulli conditional distribution and follows the specified logistic model \(\eta(x)=-1.2+0.4\log(1+x)\). Find the predicted probability at \(x=3\). (Answer: \(0.3440\).)
  3. At \(x=9\), compare log1p with \(h_4(x)=\log(x+4)\) by calculating each transformation’s increase from its own value at \(x=0\). (Answer: log1p increase \(=\log(10)=2.3026\); offset-transformation increase \(=\log(13)-\log(4)=\log(3.25)=1.1787\).)
  4. For \(g(x)=\log(1+x)\), calculate the first derivative at \(x=0\) and \(x=9\), then the second derivative at \(x=9\). State what the signs imply. (Answer: \(g'(0)=1\), \(g'(9)=0.1000\), and \(g''(9)=-0.0100\); the function is increasing and concave at these values.)
  5. A positive amount is recorded as either 500 dollars or 5 hundreds of dollars. Calculate log1p under each numeric representation and explain whether the results agree. (Answer: \(\log(501)=6.2166\) versus \(\log(6)=1.7918\); they do not agree because the numeric scale changed.)
Clinical Biostats