log_returns: Logarithmic returns for financial time series
Description
Computes the logarithmic return \(\log(x_t / x_{t-1})\) for selected numeric columns. Used extensively in financial time series analysis. Group-wise computation is supported.
Usage
log_returns(data, cols = NULL, group = NULL, verbose = FALSE)
Value
A vector or data frame with logarithmic returns. The first observation of each group (or the vector) is NA.
Arguments
data
A data frame, matrix, or numeric vector.
cols
Column indices or names of numeric variables. If NULL, all numeric columns are used.
group
Optional grouping column for group-specific returns.
verbose
Logical; if TRUE, prints progress message.
Details
The function only computes a return when both the current and previous values are positive. Otherwise, the result is NA.