Skip to contents

Calculates AR model residuals with zero-phase correction using FFT-based phase adjustment. This function applies an AR filter causally, then corrects for phase distortion in the frequency domain to produce zero-phase residuals.

Usage

residual(x, ar)

Arguments

x

A numeric vector or `ts` object. The input time series.

ar

A numeric vector. AR coefficients from an AR model fit.

Value

A numeric vector of zero-phase corrected residuals with the same length as `x`. The first `order` and last `order` values are set to NA where `order = length(ar)`.

Details

The function performs the following steps: 1. Applies causal AR filter: constructs polynomial (1, -ar) and convolves 2. Fills NA values with 0 for FFT computation 3. Computes phase response of AR coefficients in frequency domain 4. Corrects phase distortion by multiplying FFT of residuals with exp(-i * phase) 5. Transforms back to time domain and restores edge NAs