Answer

Log-transforming data: when it helps, and what it changes

Inspired by a question on Cross Validated ·

distributionst testdescriptive statistics

The short answer

Use a log when your numbers are positive, right-skewed and vary by factors rather than fixed amounts, as prices, incomes, concentrations and reaction times often do. On the log scale, equal ratios become equal distances, skew shrinks, and spread stops growing with the level. Nothing is lost, because exp() undoes the log, but results now describe geometric means and ratios rather than arithmetic means and differences.

Why a log helps: many quantities grow by factors

A logarithm turns multiplication into addition: log(a × b) = log(a) + log(b). That matters because many real quantities change in proportion to their size. A stock that moves 2% a day moves more dollars at $500 than at $5; a salary raise, a bacterial count or a drug concentration also tends to change by a percentage. When data are built from many such proportional nudges, they pile up near zero with a long right tail, and the log of the values tends to look roughly normal. That shape is called log-normal.

On the log scale, a doubling is the same distance whether it goes from 10 to 20 or from 1,000 to 2,000. That single change brings several practical benefits:

When to use a log, and when not to

Good signs: every value is strictly positive, the data are right-skewed, the largest values are many times the smallest, the spread rises with the mean, and it makes sense to talk about changes in percent. Subject-matter knowledge counts more than any test. If the process is multiplicative, a log scale is the natural one even when a histogram is ambiguous.

Is the transformation lossless?

For positive numbers, yes: each value has exactly one log, and exp() returns the original. Orderings, medians and percentiles carry straight back, so the median of the logged data, exponentiated, is the median of the original data.

Means do not carry back the same way. The average of the logs, exponentiated, is the geometric mean, which for right-skewed data sits below the ordinary mean and close to the median. A difference in mean logs becomes a ratio of geometric means, and a confidence interval on the log scale becomes an interval for that ratio. Standard deviations and regression slopes on the log scale are likewise about multiplicative spread and percent change. So the conclusions are valid, but they answer a slightly different question, and your write-up should say which one.

See it in R and Python

The example simulates weekly spending for two groups of 150 people from log-normal distributions. It compares skewness before and after logging, shows the three candidate centres of one group, and runs a Welch t test on the log scale, back-transformed into a ratio of geometric means. The R version uses base R; the Python tab uses NumPy and SciPy.

R

set.seed(18)
n <- 150
# Right-skewed, positive data: e.g. weekly spending in dollars for two groups
a <- rlnorm(n, meanlog = log(40), sdlog = 0.8)
b <- rlnorm(n, meanlog = log(55), sdlog = 0.8)

skew <- function(x) mean((x - mean(x))^3) / sd(x)^3
round(c(raw = skew(a), logged = skew(log(a))), 2)

# Three "centres" of group A: the log scale targets the geometric mean
round(c(mean = mean(a), median = median(a), geo_mean = exp(mean(log(a)))), 2)

# Welch t test on the log scale, back-transformed to a ratio of geometric means
tt <- t.test(log(b), log(a))
ratio <- exp(unname(tt$estimate[1] - tt$estimate[2]))
round(c(ratio = ratio, lower = exp(tt$conf.int[1]), upper = exp(tt$conf.int[2])), 2)
round(c(t = unname(tt$statistic), df = unname(tt$parameter)), 2)
signif(tt$p.value, 3)

# The same comparison on the raw scale, for contrast
signif(t.test(b, a)$p.value, 3)

# The transform loses nothing for positive numbers: exp() undoes it
all.equal(exp(log(a)), a)

Python

import numpy as np
from scipy import stats

rng = np.random.default_rng(18)
n = 150
# Right-skewed, positive data: e.g. weekly spending in dollars for two groups
a = rng.lognormal(mean=np.log(40), sigma=0.8, size=n)
b = rng.lognormal(mean=np.log(55), sigma=0.8, size=n)

def skew(x):
    return np.mean((x - x.mean()) ** 3) / x.std(ddof=1) ** 3

print("skewness raw", round(skew(a), 2), "logged", round(skew(np.log(a)), 2))

# Three "centres" of group A: the log scale targets the geometric mean
print("mean", round(a.mean(), 2), "median", round(np.median(a), 2),
      "geo_mean", round(np.exp(np.log(a).mean()), 2))

# Welch t test on the log scale, back-transformed to a ratio of geometric means
la, lb = np.log(a), np.log(b)
diff = lb.mean() - la.mean()
va, vb = la.var(ddof=1) / n, lb.var(ddof=1) / n
se = np.sqrt(va + vb)
df = (va + vb) ** 2 / (va ** 2 / (n - 1) + vb ** 2 / (n - 1))
tcrit = stats.t.ppf(0.975, df)
res = stats.ttest_ind(lb, la, equal_var=False)
print("ratio", round(np.exp(diff), 2), "lower", round(np.exp(diff - tcrit * se), 2),
      "upper", round(np.exp(diff + tcrit * se), 2))
print("t", round(res.statistic, 2), "df", round(df, 2), "p", float(f"{res.pvalue:.3g}"))

# The same comparison on the raw scale, for contrast
print("raw-scale p", float(f"{stats.ttest_ind(b, a, equal_var=False).pvalue:.3g}"))

# The transform loses nothing for positive numbers: exp() undoes it
print(np.allclose(np.exp(np.log(a)), a))

The figures below come from one seeded run of the R code. Python uses a different random number generator, so its figures differ slightly, but they show the same pattern.

How to report a ratio of geometric means in APA style (7th edition)

State in the method section that the variable was log-transformed and why, then report results back-transformed to the original units so readers never have to think in logs. Using the example above:

"Because weekly spending was strongly right-skewed, it was natural-log transformed before analysis, and results are back-transformed to ratios of geometric means. Group B's geometric mean spending was 1.36 times that of Group A, 95% CI [1.13, 1.64], Welch's t(297.39) = 3.26, p = .001."

In descriptive tables, give the geometric mean with its back-transformed confidence interval (or the median with the interquartile range) rather than the mean and SD of the logs, and add a table note saying which transformation was used.

Related tools and guides

More answered questions

Working with your own data?

General answers only go so far. Send us your situation and we'll reply by email within two business days.

Ask your question

Written by AskStats with AI assistance. This is general information, not advice for your specific data or study. When the results matter, check your approach with a qualified statistician.