Learn R Programming

timesboot (version 1.0)

boot_autocov: A function that computes the bootstrapped autocovariances for a time series object. The computations are done via phase scrambling bootstrap

Description

The function resamples the time series object and returns the average , upper, and lower bounds for the autocovariances for each lag.

Usage

boot_autocov(series, replic = 5000, plot = TRUE, alpha = 0.05)

Arguments

series
A time series object
replic
The amount of boostrap replicates
plot
TRUE,FALSE indicating whether the plot is desired
alpha
the alpha needed for the intervals

Value

average
The average ACF for each lag
lower
The ACF lower quantile for each lag
upper
The ACF upper quantile for each lag

Examples

Run this code

boot_autocov(AirPassengers,replic=1000,alpha=0.05)


function (series, replic = 5000, plot = TRUE, alpha = 0.05) 
{
    if (is.ts(series) == TRUE) {
        library(boot)
        kas = tsboot(series, statistic, R = replic, sim = "scramble")
        quantiles = matrix(0, length(kas$t[1, ]), 3)
        for (i in 2:length(kas$t[1, ])) {
            cp = kas$t[, i]
            quantiles[i, 1] = quantile(cp, alpha)
            quantiles[i, 2] = quantile(cp, 1 - alpha/2)
            quantiles[i, 3] = mean(cp)
        }
        quantiles = quantiles[-1, ]
        if (plot == TRUE) {
            par(mfrow = c(1, 2))
            x = seq(1, length(quantiles[, 1]), 1)/frequency(series)
            plot(x, quantiles[, 1], type = "l", col = "blue", 
                main = "Bootstraped Correlogram", ylab = "value", 
                lwd = 1, xlab = "lag")
            polygon(c(x, rev(x)), c(quantiles[, 1], rev(quantiles[, 
                2])), col = "skyblue")
            lines(x, quantiles[, 3], type = "o", col = "black", 
                pch = 20)
            abline(a = 0, b = 0)
            plot(acf(series, plot = FALSE), main = "Asymptotic Correlogram", 
                ylim = c(-1, 1))
        }
        lista = list(average = quantiles[, 1], upper = quantiles[, 
            2], lower = quantiles[, 3])
        return(lista)
    }
    else {
        return("Object is not a time-series")
    }
  }

Run the code above in your browser using DataLab