log(A + x), not log(1 + x)

This recent discussion in comments reminds me of something that comes up in regression modeling from time to time. You have a predictor on an outcome which you’d like to model on the log scale—the usual reason, and it’s a good one, is that you’d like your model to have multiplicative effects, which are linear on the logarithmic scale—but some of the observations are 0, and you can’t take the log of 0. Often what’s done is to take log(1 + x).

At best this is a shortcut to whatever model you’d really like to be fitting, but that’s fine—we take lots of shortcuts in statistics, no apology for that.

The point of this post is that if you’re gonna work with log(1 + x), you should work with log(A + x), and set A to some reasonable value based on the context of your problem. If you want to set A = 1, do that only in the context of scaling x appropriately.

From a mathematical standpoint this is obvious: log(1 + x) is a special case of log(A + x), and the appropriate value of A has to depend on the scaling of x. But so so so so often I see people unreflectively doing log(1 + x) without seeming to recognize the issue. Hence this post.

P.S. Also relevant is this post from forever ago on the 1/4-power transformation.

24 thoughts on “log(A + x), not log(1 + x)”

        • Not a problem. It just reminded me of that notation thing. One of my favorite examples of terminological confusion is the term “marginal.” In statistics, marginal is the integral. In economics, marginal is the derivative. Either way, it’s something you could put in the rightmost margin of a table: you either sum the entries or you anticipate the change going forward.

    • @Michael Weissman: Well, from my experience it’s fairly common, though you’re right in that we take x=some dimensionless ratio. A lot of useful cases arise in the limit that the dimensionless ratio x <> 1, we can define y=1/x and expand in y instead.

      There’s also the nice property of ln(1 + x) being the integral of 1/(1+x), whose expansion is a geometric series (or one with alternating sign, if you prefer), which can be exploited at times.

      • Oops, the angle brackets got eaten up by the HTML formatting. The first paragraph should read:

        Well, from my experience it’s fairly common, though you’re right in that we take x=some dimensionless ratio. A lot of useful cases arise in the limit that the dimensionless ratio x < < 1, and use the fact that the Taylor series of ln(1+x) around x=0 has x as its leading term. And if x > > 1, we can define y=1/x and expand in y instead.

  1. I’ve struggled with this from time to time, and in at least one paper I ended up just replacing A with whatever made the reviewers happy. It’d be nice if you could provide some real world examples of how to choose A.

    I also can’t help but wonder if there’s some sort mechanical bias-minimizing way of choosing A. Some kind of Monte Carlo exercise, perhaps.

    • First, as discussed in the comments Andrew referred to, whatever’s inside the log() should be a dimensionless quantity. To make 1+x dimensionless you must have x dimensionless. If it’s dimensionless then there’s no problem that Michael Weissman mentioned above about dimension because 1 + dimensionless is also dimensionless.

      Usually a good way to choose your denominator is to make it so that the dimensionless ratio takes on values that are “about 1” also sometimes written in this context O(1). In that context, you definitely shouldn’t do log(1+x) you should do something like log(x+epsilon) where epsilon is “small relative to what x usually is”. So if you’ve chosen x to be a/b where a is a measurement and b is a scale that is important in your problem such that a/b is usually O(1) then you’d typically do something like say log(x + .001) or maybe calculate a quantile for x on the left tail of the distribution
      x0 = quantile(x,0.001);
      formula = y ~ log(x + x0)

      that kind of thing.

        • Sam, O(1) in computer science means constant yes, in other areas of applied math it’s used differently sometimes. basically more like “order of magnitude”, so you might say something like O(1000) for something that is sometimes 800 sometimes 3250 sometimes 2601 etc. To confirm that I’m not making up notation I pulled down my copy of A.C. Fowler “Mathematical Models in the Applied Sciences” and sure enough it’s used there, along with notation like a >> b to mean a is much greater than b etc. These concepts are “fuzzy” and are used by applied mathematicians to justify approximations and so are not meant to be entirely crisply defined. If you say 1+1/x for x >> 1 is ≈ 1 this is the kind of thing being discussed.

          Dimensionless means if your measurements have units and the units are used consistently (examples: meters, seconds, kg, pounds, pounds/inch^2, people, dollars, years) then if you work out the units of a dimensionless thing it has no units. A “dimension” is basically the “type” of a measurement (ie. it specifies the kinds of units that are relevant, a “length” dimension could be in units of meters, feet, inches, furlongs, football fields whatever but never kg or Newtons or years).

          Examples are the waist to hips ratios w/h. Since w is in some length units and h is in the same length units then the units of w/h are … 1. A more complicated case is the Reynolds number: rho * v * l / mu

          rho is mass/length^3, v is length / time, l is in length, and mu is a viscosity which has dimensions of pressure per velocity gradient which works out to mass / length / second.

          putting it all together you get:

          [(mass / length^3) * (length / time) * length] / (mass / length / time)

          if you work out the dimensions in the numerator it’s mass/length/time and so is the denominator and therefore the ratio is dimensionless.

  2. I have no objection to short cuts when the short cuts don’t impact the practical conclusion reached, but if the issue is you want multiplicative and you have some zeros, would not a Poisson model remove the need for the log(A+x) adjustment altogether, and be cleaner? If there are some zeros on the RHS I can see that this option might not work, but the below paper seems to suggest (i think) a pretty straightforward solution to that problem also. So I think switching to GLM rather than LM works for this.

    Happy to be corrected if i have misunderstood the issue :)

    G.E. BATTESE (1997) A note on the estimation of Cobb‐Douglas production functions when some explanatory variables have zero values
    Journal of Agricultural Economics, 48, pp. 250-252.

      • Yeah, I could more correctly have said count data model, which is the basic idea I was thinking of… I tend to start with Poisson and then look at the over/under dispersion issue.

        Note: For anyone going down this path R throws a warning: I guess to make sure you are aware it was expecting integers only

        my.glm <- glm(y ~ x + factor…., family = poisson())
        Warning messages:
        1: In dpois(y, mu, log = TRUE) : non-integer x = …

        P.S. I have not dropped by the blog for ages. I had forgotten how much fun it is. thanks for your efforts to keep it running.

      • Any reason not to treat A as a parameter to be estimated? Suppose you are predicting death counts by county, with a lagged death rate. Some counties might have zero as their lagged rate, but that is probably a poor estimate of the true county death rate, so instead we sometimes estimate A in the lagged ln(A + death.rate). Seems to work fine in our Stan models.

        • Yeah, in general this is a missing data thing. The main reason for doing stuff like log(x+epsilon) is for plots so that the zero count data doesn’t just disappear, you want to put it somewhere. When building models it’s better to impute the right rate.

        • > treat A as a parameter to be estimated

          This was my thought as well, although I also like James’ suggestion to revisit how the likelihood/outcome is specified.

          I view those two approaches (change the likelihood function, introduce a free parameter) as manifestations of what I think is Andrew’s broader point, namely, that adjustments to models should be chosen thoughtfully so as to have meaning in context, as opposed to just being plugged in to deal with some numerical issue.

        • So if you are doing Poisson type models that are something like:

          log(E[Count_t]) = B0 + B1*log(Count_{t-1})

          IMO you should do:

          log(E[Count_t]) = B0 + B1*log(Count_{t-1} for Count > 0) + B2*I(Count = 0)

          Where I is the dummy variable when the count is 0. It implies a discontinuous model for the auto-regressive term, but no imputation needed, https://andrewpwheeler.com/2014/06/17/poisson-regression-and-crazy-predictions/

          A bigger issue is IMO when people do the log(x + 1) trick on the left hand side. This IMO requires some rethinking about what the model is exactly.

  3. I personally prefer using the asinh transform. Asinh(0)=0 and if I remember correctly, lim x->inf: asinh(x) -> log(x) + c with something like c=1.

    Not only is it cleanly differerentiable, it can be applied to th whole real line if you need too as well.

    • That’s a great alternative when the logarithm is not applicable. The presence of zero or negative values doesn’t just pose a practical problem, it removes any theoretical reason to use logarithms anyway.

      Using log for natural logarithm, it is

      arsinh(x) = log(x+ sqrt(x^2 +1))

      arsinh(0)=0 and the function is linear around 0

      arsinh(x) + arsinh(-x)=0 resulting in a nice transformation with similar shape for positive and negative numbers

      For large x, it goes to log(2x) = log(x) + log(2) = log(x) + 0.69

      It’s a nice way to plot charts that contain both positive and negative numbers compressing the tails symmetrically. The only thing that may need to be adjusted is the scale of the data – to control the size of the linear region in the middle.

  4. The choice of A tunes the effective distance between 0 and low numbers (on the original scale, x) compared to the distance between larger numbers. The larger A, the closer x=0 becomes to small nonzero values in relation to the distances between other values. Of course it depends on the measurement unit “how small is small”. Also what the impact of “effective distance” is depends on what exactly we’re afterwards doing with the data. In distance-based clustering, the lower A, the more easily it happens that the clustering method isolates all observations with x=0 in a cluster on their own (at least if there are several of them). We discuss this for income and savings variables here, regarding clustering: https://rss.onlinelibrary.wiley.com/doi/abs/10.1111/j.1467-9876.2012.01066.x

    However, “effective distance” will also have impact on regression and the like. One approach to choose A is to ask what the “interpretative distance” is between zero and low values in the application in question, in relation to the distance between larger values. This of course isn’t an easy question, as people are not used to thinking in this way. Also it is hard to find out how to relate this to knowledge and observations so that we can say something qualified about it. An example for the use of external data (namely data from an earlier year clustering football players based on performance data from a certain year) is quickly mentioned here, regarding goals scored and other variables: https://www.degruyter.com/document/doi/10.1515/jqas-2022-0037/html?lang=en also on arxiv.

    • I really don’t recommend doing log(x + \epsilon) in modeling. If we’re talking modeling, we should be estimating a low rate in a Poisson or something similar:

      x ~ Poisson(rate(predictors))

      (or I guess negative binomial if that’s your thing, though I usually have a hard time interpreting negative binomial).

      But when doing diagnostic or exploratory plots, keeping the zeros on the graph and putting them somewhere is often important. estimating that for the zeros the correct rate is \epsilon on average lets you do stuff like:

      plot(x,log(y+epsilon))

      and the zero count y values still show up at log(epsilon) rather than just being dropped from the graph.

  5. One nice thing about log(1+x) is that zeros stay zeros. This can be important if you have large sparse data, as you can keep using a sparse matrix representation.

    You can keep this property by using log(1+x/A). This is commonly used with single-cell RNA-Seq data, for example.

Leave a Reply

Your email address will not be published. Required fields are marked *