Skip to content
Method note

detrending methodsDetrending: confirm it is safe before you flatten the trend

Detrending removes a long-term direction so the remaining structure can be studied. It is only meaningful when the direction is deterministic. If the series is a random walk, a straight line will manufacture waves that were never in the data. This page covers the three detrending methods in the workspace and the diagnostics that gate them.

Reproducible procedure

This is the procedure the workspace executes for this method, in order. Each step has an action, a rule that can stop the step, and a defined output.

  1. 01

    Run the ADF and KPSS unit-root tests on the cleaned series.

    Rule
    A blocked verdict stops deterministic detrending before any transform is applied.
    Output
    Stationarity verdict and the availability of each transform.
  2. 02

    Select the transform: STL, first or seasonal differencing, or regression residuals.

    Rule
    The transform is a function of the verdict and the detected cycle, never a fixed default.
    Output
    The applied transform and its parameters.
  3. 03

    Decompose the series into trend, seasonal and residual components.

    Rule
    A cycle length must be identified before STL; a wrong period contaminates the residual and the seasonal test at the same time.
    Output
    Component series with variance shares.
  4. 04

    Re-test the residuals for autocorrelation that the transform did not remove.

    Rule
    Residual autocorrelation above the 95% band means the transform was incomplete and the result is reported with a warning.
    Output
    Residual diagnostics and the attached warning.

Why unit-root diagnostics come first

ADF tests the null of a unit root; KPSS tests the null of stationarity. Rejecting ADF while failing to reject KPSS supports a deterministic trend. The reverse supports a stochastic trend, where differencing is the right operation and deterministic detrending is blocked.

When both tests agree in the “wrong” direction, or when the sample is too short, the verdict is ambiguous or insufficient. The workspace does not resolve that ambiguity silently: deterministic detrending is marked as a warning or blocked, and the result keeps the label.

  • Trend-stationary: STL or regression residuals are allowed.
  • Difference-stationary: differencing is the default; deterministic detrending requires explicit override and carries a permanent warning.
  • Ambiguous: both paths are shown; neither is presented as the only answer.
  • Insufficient evidence (n < 20 or > 30% missing): trend inference is blocked, descriptive statistics remain available.

STL decomposition

STL separates a series into trend, seasonal and residual components using LOESS smoothers. It is the right choice when the series has a stable direction plus a repeating cycle. Robust mode downweights outliers through bisquare weights, which is why this workspace forces robust = true by default.

STL decomposition
y_t = T_t + S_t + R_t
What this computes:
Split the series into long-term direction + repeating cycle + leftover noise.
Example:
Monthly sales = long-term growth (trend) + yearly peak (season) + random wiggle (residual).
Caution:
A wrong period contaminates the residual.
  • period must come from the data or from the user; a wrong period contaminates the residual.
  • seasonal must be an odd integer ≥ 7; trend is the smallest odd integer above 1.5·period/(1 − 1.5/seasonal).
  • Above 50,000 observations the jump parameters rise to 2. The actual parameters are written into the result metadata; degradation is never silent.

Differencing

Differencing replaces each value with the change from the previous step (or from the same season one cycle earlier). It removes stochastic trends and is the correct alternative when the series is difference-stationary. The cost is that the differenced series is noisier and has one fewer observation per lag.

Over-differencing is a real failure mode: it induces negative autocorrelation and can make an already stationary series look artificially volatile. The diagnostics, not habit, decide.

Regression residuals

Fitting a least-squares line and keeping the residuals is the simplest deterministic detrend. It is transparent, fast and works when the trend is close to linear. It is also the most dangerous method on a random walk, because the fitted slope will always be non-zero.

Use it when the stationarity verdict is trend-stationary and no reliable seasonal cycle is present. Inspect the residual for curvature; a leftover curve means the line was not enough.

Least-squares detrending
y_t = a + b·t + r_t
What this computes:
Fit the straight line that minimises squared error, then keep the residual r_t as the detrended series.
Example:
A slope of 2.4 per step means the fitted line rises 2.4 units each step; the residual is what the line does not explain.
Caution:
On a random walk the fitted slope is almost never zero, and the residual is not a meaningful fluctuation.

Detrending methods: failure modes worth naming

Spurious detrending: flattening a stochastic trend and then testing the residual for structure. Over-differencing: turning white noise into a negatively autocorrelated series. Wrong seasonal period: moving real variation into the residual and then calling it noise. Non-linear trends: a straight line leaves a systematic residual that later inflates the apparent autocorrelation.

Common questions

Can I detrend a time series online without uploading the file?

Yes. Detrend reads the workbook with the browser File API and runs the analysis in local Web Workers. There is no upload endpoint, so the file bytes never cross the network. Closing the tab discards the data.

Should I run a unit-root test before detrending?

Yes. ADF and KPSS decide whether the series has a stable trend baseline or behaves like a random walk. Detrending before that check is how a straight line ends up manufacturing structure that was never in the data. This workspace blocks or cautions the operation based on those tests.

Is STL detrending better than a straight line?

Only when a repeating cycle is present and the direction is deterministic. STL separates trend, season and residual; a least-squares line is simpler and appropriate when the trend is close to linear. For a random walk, differencing is safer than either.

What happens if I detrend a random walk anyway?

The fitted slope will be non-zero almost by construction, and the residual will look like a meaningful fluctuation when it is not. The workspace lets you override the blocked state only with a permanent warning, and keeps the differencing result available for comparison.

Related method notes

Cite this page: Detrend, “Detrending: confirm it is safe before you flatten the trend”, https://detrend.datacorp.top/methods/detrending. Last updated 2026-11-15.

Verification matrixFAQAll method notesRSSllms.txt

Run this procedure on your own workbook

The workspace executes exactly these steps locally, selects the method from the diagnostics and keeps every alternative visible.

Open the workspace