| calc_lrvar_bartlett | R Documentation |
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.
calc_lrvar_bartlett(x, maxlag, nodemean = FALSE)
x |
A numeric vector. Missing values are removed prior to computation. |
maxlag |
Non-negative integer. Maximum lag order |
nodemean |
Logical. If |
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.
A single numeric value:
The Bartlett-kernel long-run variance estimate \widehat{\Omega} (scalar),
or NA if x contains no non-missing values.
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.
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.
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.
maxlag sets the truncation point m. Larger m captures more
serial correlation but increases estimation noise.
westerlund_test
## 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.