Skip to content
svgboxcox-profile

Box–Cox profile

Which power transformation would make this outcome behave best?

Demo data · seed 20261101 · 81 rows · default settings · 640 × 360SVG, lightSVG, darkDemo CSV

How to read it

  1. Find the peak: the λ that makes the transformed outcome fit best.
  2. Read the shaded interval: any λ in it fits about as well.
  3. Prefer a round value inside the interval: 0 (log), 0.5 (square root) or 1 (no change).
  1. Callout 1: The best λ, with its interval shaded.
  2. Callout 2: The dashed line: the interval's cutoff.
  3. Callout 3: λ = 1 means no transformation.

Watch out

  • It targets normal residuals with equal spread, not interpretation. A transformed outcome changes what the coefficients mean.

Defaults

Interval level
0.95
How often intervals built this way contain the true value, over repeated samples.
Range 0.5 to 0.99, step 0.01

Demo data

Skew of the demo data
0.7
The log-normal σ behind the demo: more skew moves the best λ towards the log.
Range 0.1 to 1.5, step 0.1

Pinned in the code

  • As MASS::boxcox: y is divided by its geometric mean, then log-likelihood = −n/2 · log RSS of the transformed y.
  • The interval is where the curve stays within χ²₁(level)/2 of its maximum, by linear interpolation on the λ grid.
# Box–Cox profile — Codesplash Chart Lab (recipe "boxcox-profile")
# controls: level=0.95
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from scipy import stats

try:
    import codesplash as cs  # optional: the app's chart theme
except ImportError:
    cs = None
if cs:
    cs.use_codesplash(dark=False)
PALETTE = cs.palette(dark=False)["categorical"] if cs else plt.rcParams["axes.prop_cycle"].by_key()["color"]

df = pd.read_csv("boxcox-profile-demo.csv", keep_default_na=False, na_values=[""])  # demo data (only empty cells are missing): Chart Lab → Code → Download demo CSV

_tokens = cs.palette(dark=False) if cs else {}
SEQ = _tokens.get("sequential", ["#eff6fd", "#1a469d"])  # sequential ramp (low → high)
DIV = _tokens.get("diverging", ["#c13e2e", "#f3f5f8", "#0859ac"])  # negative, midpoint, positive
REF = _tokens.get("reference", "#4e5661")
GREY = _tokens.get("dim", "#ced1d6")
L = "λ"
LL = "log-likelihood"
d = df.dropna(subset=[L, LL]).sort_values(L)
lam, ll = d[L].to_numpy(float), d[LL].to_numpy(float)
best = int(np.argmax(ll))
cut = ll[best] - stats.chi2.ppf(0.95, 1) / 2  # interval: within χ²₁/2 of the maximum
inside = np.flatnonzero(ll >= cut)
lo = np.interp(cut, ll[: inside[0] + 1], lam[: inside[0] + 1]) if inside[0] > 0 else np.nan
hi = np.interp(cut, ll[inside[-1] :][::-1], lam[inside[-1] :][::-1]) if inside[-1] < len(ll) - 1 else np.nan
fig, ax = plt.subplots(figsize=(7, 4))
if np.isfinite(lo) and np.isfinite(hi):
    ax.axvspan(lo, hi, color=PALETTE[0], alpha=0.1, label=f"interval [{lo:.2f}, {hi:.2f}]")
ax.plot(lam, ll, color=PALETTE[0], linewidth=2)
ax.scatter([lam[best]], [ll[best]], s=40, color=PALETTE[0], zorder=3, label=f"best λ {lam[best]:.2f}")
ax.axhline(cut, color=REF, linestyle="--", linewidth=1)
for v, t in ((0, "log"), (1, "no change")):
    if lam[0] <= v <= lam[-1]:
        ax.axvline(v, color=REF, linestyle=":", linewidth=0.8)
        ax.annotate(t, (v, ll.max()), fontsize=8, xytext=(3, 0), textcoords="offset points")
ax.set_xlabel("λ")
ax.set_ylabel("log-likelihood")
ax.legend(fontsize=8)
plt.show()

Needs pandas, numpy, matplotlib, scipy · reads boxcox-profile-demo.csv · app theme from the optional codesplash helper