detrend_ts: Remove linear trend from time series

View source: R/detrend_ts.R

detrend_tsR Documentation

Remove linear trend from time series

Description

Fits a linear trend (intercept + slope) to each selected numeric column (or vector) and returns the residuals. This is useful for removing long-term trends before correlation analysis.

Usage

detrend_ts(data, cols = NULL, date_col = NULL, verbose = FALSE)

Arguments

data

A data frame, matrix, or numeric vector.

cols

Column indices or names to detrend. If NULL, all numeric columns are used.

date_col

Optional time column (not strictly required for linear detrending).

verbose

Logical; if TRUE, prints progress message.

Value

A data frame or vector with the linear trend removed.

Examples

detrend_ts(data[1:100, c(1, 4, 17:19)], cols = 3:5)

dataprep documentation built on Oct. 1, 2026, 5:07 p.m.