Learn R Programming

Westerlund (version 0.1.2)

calc_lrvar_bartlett: Long-Run Variance Estimation with Bartlett Kernel

Description

Computes a Bartlett-kernel (Newey--West style) long-run variance estimate for a univariate series using Stata-like conventions: missing values are removed (na.omit), autocovariances are scaled by \(1/n\) (not \(1/(n-j)\)), and optional centering is controlled by nodemean.

Usage

calc_lrvar_bartlett(x, maxlag, nodemean = FALSE)

Value

A single numeric value:

  • The Bartlett-kernel long-run variance estimate \(\widehat{\Omega}\) (scalar),

  • or NA if x contains no non-missing values.

Arguments

x

A numeric vector. Missing values are removed prior to computation.

maxlag

Non-negative integer. Maximum lag order \(m\) used in the Bartlett kernel. If maxlag=0, the function returns the sample variance estimate \(\gamma_0\).

nodemean

Logical. If FALSE (default), the series is centered by subtracting its mean before computing autocovariances. If TRUE, the function assumes x is already centered and does not de-mean it.

Technical Notes

This section illustrates how calc_lrvar_bartlett() computes a Bartlett-kernel long-run variance (LRV) estimate and how its options map to common time-series preprocessing choices.

Stata-like conventions

This helper function is designed to match Stata-style calculations:

  • Missing values are dropped: na.omit(x) is applied first.

  • Scaling by \(1/n\): autocovariances at all lags divide by \(n\), not by \(n-j\).

  • Optional de-meaning: controlled by nodemean.

Centering

By default (nodemean=FALSE), the series is centered: \(x_t \leftarrow x_t - \bar{x}\). Set nodemean=TRUE when you have already centered the series elsewhere.

Choosing maxlag

maxlag sets the truncation point \(m\). Larger \(m\) captures more serial correlation but increases estimation noise.

Details

Let \(x_t\) be the input series after removing missing values. If nodemean=FALSE, the function replaces \(x_t\) with \(x_t - \bar{x}\).

Define the lag-\(j\) autocovariance using the Stata-style scaling: $$ \gamma_j = \frac{1}{n}\sum_{t=j+1}^{n} x_t x_{t-j}, \qquad j=0,1,\dots,m, $$ where \(n\) is the length of the cleaned series and \(m=\code{maxlag}\). Note that the scaling uses \(1/n\) for all \(j\) (rather than \(1/(n-j)\)).

The Bartlett kernel weight at lag \(j\) is: $$ w_j = 1 - \frac{j}{m+1}. $$ The long-run variance estimate is then: $$ \widehat{\Omega} = \gamma_0 + 2\sum_{j=1}^{m} w_j \gamma_j. $$

If the cleaned series has zero length, the function returns NA.

See Also

westerlund_test

Examples

Run this code
## Example 1: Basic usage
x <- rnorm(200)
calc_lrvar_bartlett(x, maxlag = 4)

## Example 2: maxlag = 0 returns gamma_0
calc_lrvar_bartlett(x, maxlag = 0)

## Example 3: Handle missing values (they are removed)
x_na <- x
x_na[c(5, 10, 50)] <- NA
calc_lrvar_bartlett(x_na, maxlag = 4)

## Example 4: Compare centering choices
## Default: de-mean internally
lr1 <- calc_lrvar_bartlett(x, maxlag = 4, nodemean = FALSE)

## Pre-center and skip de-meaning
x_centered <- x - mean(x)
lr2 <- calc_lrvar_bartlett(x_centered, maxlag = 4, nodemean = TRUE)

Run the code above in your browser using DataLab