Skip to content

Instantly share code, notes, and snippets.

@pteetor
Created November 3, 2021 12:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pteetor/431ece15cbf2ee6b91911698d7665043 to your computer and use it in GitHub Desktop.
Save pteetor/431ece15cbf2ee6b91911698d7665043 to your computer and use it in GitHub Desktop.
Demonstration of bootstrapping the residuals of an AR(1) time series model
#
# Demonstration of bootstrapping AR(1) residuals,
# using the AirPassergers data.
#
# This code performs a simple bootstrap, not a
# block bootstrap, because it assumes the model's residuals
# are independent. If your residuals exhibit autocorrelation,
# then a block bootstrap would be more appropriate.
# (However, if your residuals *do* show autocorrelation,
# then your time series model needs improvement.)
#
# y is the time series, T is the series' length
y <- AirPassengers
T <- length(y)
# Fit the time series to an AR(1) model
model <- arima(y, order = c(1,0,0))
# This function computes one bootstrap replication.
# Using the resampled residuals, it simulates one
# AR(1) time series and returns the statistic for
# that series.
statistic <- function(data, ind) {
resids <- data[ind]
ts <- arima.sim(model = model$model, n = T,
innov = resids)
# For this demonstration, we compute the
# Augmented Dickey-Fuller statistic.
# You can change this to compute your statistic of interest.
htest <- tseries::adf.test(ts)
# Return just the statistic
htest$statistic
}
# Perform the bootstrap
b <- boot::boot(data = resid(model),
statistic = statistic,
R = 999 )
# Plot the bootstrap replications
plot(b)
# Show the bootstrap confidence intervals
boot::boot.ci(b, type = c("norm", "basic"))
# Note: The AR(1) model of AirPassengers is a very poor model,
# so the bootstrap confidence intervals will be wide.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment