-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.Rmd
More file actions
244 lines (196 loc) · 8.88 KB
/
README.Rmd
File metadata and controls
244 lines (196 loc) · 8.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
---
output: github_document
editor_options:
markdown:
wrap: 72
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
fig.align = "center",
fig.ext = "svg",
dev = "svg"
)
set.seed(32)
```
# blocklength <img src="man/figures/logo.svg" style="padding-left: 20px" align="right"/>
<!-- badges: start -->
[](https://github.com/Alec-Stashevsky/blocklength/actions/workflows/R-CMD-check.yaml)
[](https://CRAN.R-project.org/package=blocklength)
[](https://CRAN.R-project.org/package=blocklength)
[](https://CRAN.R-project.org/package=blocklength)
[](https://app.codecov.io/gh/Alec-Stashevsky/blocklength)
<!-- badges: end -->
`blocklength` is an R package used to automatically select the
block-length parameter for a block-bootstrap. It is meant for use with
dependent data such as stationary time series.
## The Story
Regular bootstrap methods rely on assumptions that observations are
independent and identically distributed (*i.i.d.*), but this assumption
fails for many types of time series because we would expect the
observation in the previous period to have some explanatory power over
the current observation. This could occur in any time series from
unemployment rates, stock prices, biological data, etc. A time series
that is *i.i.d.* would look like white noise, since the following
observation would be totally independent of the previous one (random).
To get around this problem, we can retain some of this *time-dependence*
by breaking-up a time series into a number of blocks with length *l*.
Instead of sampling each observation randomly (with replacement) like a
regular bootstrap, we can resample these *blocks* at random. This way
within each block the time-dependence is preserved.
The problem with the block bootstrap is the high sensitivity to the
choice of block-length, or the number of blocks to break the time series
into.
The goal of `blocklength` is to simplify and automate the process of
selecting a block-length to perform a bootstrap on dependent data.
`blocklength` has several functions that take their name from the
authors who have proposed them. Currently, there are three methods
available:
1. `hhj()` takes its name from the [Hall, Horowitz, and Jing
(1995)](https://doi.org/10.1093/biomet/82.3.561)
"HHJ" method to select the optimal block-length using a
cross-validation algorithm which minimizes the mean squared error
*(MSE)* incurred by the bootstrap at various block-lengths.
2. `pwsd()` takes its name from the [Politis and White
(2004)](https://doi.org/10.1081/ETC-120028836) Spectral Density
"PWSD" Plug-in method to automatically select the optimal
block-length using spectral density estimation via "flat-top" lag
windows of [Politis and Romano
(1995).](https://doi.org/10.1111/j.1467-9892.1995.tb00223.x)
3. `nppi()` takes its name from the [Lahiri, Furukawa, and Lee
(2007)](https://doi.org/10.1016/j.stamet.2006.08.002) Nonparametric
Plug-In "NPPI" method to select the optimal block-length for block
bootstrap procedures. The NPPI method estimates the leading term in
the first-order expansion of the theoretically optimal block length
by using resampling methods to construct consistent bias and
variance estimators for the block-bootstrap. Specifically, this
package implements the Moving Block Bootstrap (MBB) method of
[Künsch (1989)](https://doi.org/10.1214/aos/1176347265) and
the Moving Blocks Jackknife (MBJ) of [Liu and Singh
(1992)](https://doi.org/10.1214/aos/1176348653) as the bias and
variance estimators, respectively.
Under the hood, `hhj()` uses the moving block bootstrap (MBB) procedure
according to [Künsch
(1989)](https://projecteuclid.org/euclid.aos/1176347265) which resamples
blocks from a set of overlapping sub-samples with a fixed block-length.
However, the results of `hhj()` may be generalized to other block
bootstrap procedures such as the *stationary bootstrap* of [Politis and
Romano
(1994).](https://doi.org/10.1080/01621459.1994.10476870)
Compared to `pwsd()`, `hhj()` is more computationally intensive as it
relies on iterative sub-sampling processes that optimize the MSE
function over each possible block-length (or a select grid of
block-lengths), while `pwsd()` is a simpler "plug-in" rule that uses
auto-correlations, auto-covariance, and the spectral density of the
series to optimize the choice of block-length. Similarly, `nppi()` is
another "plug-in" rule, however, due to its heavy reliance on
resampling, it can also be computationally intensive compared to `pwsd()`.
For a detailed comparison, see the table below:
```{r table, echo=FALSE}
# Comparison table
table <- data.frame(
rows = c(
"**Method Type**",
"**Computational Cost**",
"**Primary Goal**",
"**Variance Estimation**",
"**Bias Estimation**",
"**Best for**",
"**Estimation Capacity**",
"**Dependency\\***"
),
NPPI = c(
"Nonparametric resampling",
"Medium (bootstrap resampling & jackknife)",
"Minimize MSE of bootstrap estimator",
"Moving Blocks Jackknife-After-Bootstrap (JAB)",
"Directly estimates bias from bootstrap",
"General-purpose estimators, small sample sizes, and quantile estimation",
"Bootstrap bias, variance, distribution function, and quantile estimation",
"User-defined parameters for initial block-length `l` and number of deletion blocks `m`"
),
PWSD = c(
"Spectral density estimation",
"Low (direct ACF computation)",
"Estimate block length using spectral density",
"Implicitly estimated via spectral density",
"Indirectly accounts for bias via ACF decay",
"Block-length selection for circular and stationary bootstrap, time series with strong autocorrelation",
"Bootstrap sample mean only",
"User-defined parameters for autocorrelation lag and implied hypothesis tests (4 total)"
),
HHJ = c(
"Subsampling-based cross-validation",
"High (subsampling & cross-validation)",
"Minimize MSE via cross-validation",
"Uses subsample-based variance estimation",
"Uses subsample-based bias estimation",
"Estimating functionals with strong dependencies",
"Bootstrap variance and distribution function estimation",
"Requires user-defined parameters for `pilot_block_length` (*l\\**) and `sub_sample` size (*m*)"
)
)
# Render table
knitr::kable(
table,
format = "markdown",
col.names = c(
"",
"NPPI (Lahiri et al., 2007)",
"PWSD (Politis & White, 2004)",
"HHJ (Hall, Horowitz & Jing, 1995)"
),
caption = "* All algorithms have default user-defined parameters recomended by the respective authors."
)
```
## Installation
You can install the released version from
[CRAN](https://cran.r-project.org/package=blocklength) with:
``` r
install.packages("blocklength")
```
You can install the development version from
[GitHub](https://github.com/Alec-Stashevsky/blocklength) with:
``` r
# install.packages("devtools")
devtools::install_github("Alec-Stashevsky/blocklength")
```
## Use Case
We want to select the optimal block-length to perform a block bootstrap
on a simulated autoregressive *AR(1)* time series.
First we will generate the time series:
```{r series}
library(blocklength)
# Simulate AR(1) time series
series <- stats::arima.sim(model = list(order = c(1, 0, 0), ar = 0.5),
n = 500, rand.gen = rnorm)
# Coerce time series to data.frame (not necessary)
data <- data.frame("AR1" = series)
```
Now, we can find the optimal block-length to perform a block-bootstrap.
We do this using the three available methods.
### 1. The Hall, Horowitz, and Jing (1995) "HHJ" Method
```{r hhj}
## Using the HHJ Algorithm with overlapping subsamples of width 10
hhj(series, sub_sample = 10, k = "bias/variance")
```
### 2. The Politis and White (2004) Spectral Density Estimation "PWSD" Method
```{r pwsd}
# Using Politis and White (2004) Spectral Density Estimation
pwsd(data)
```
We can see that both methods produce similar results for a block-length
of 9 or 11 depending on the type of bootstrap method used.
### 3. The Lahiri, Furukawa, and Lee (2007) Nonparametric Plug-In "NPPI" Method
```{r nppi}
# Using Lahiri, Furukawa, and Lee (2007) Nonparametric Plug-In
nppi(data, m = 8)
```
## Acknowledgements
A big shoutout to Malina Cheeneebash for designing the `blocklength` hex
sticker! Also to Sergio Armella and [Simon P.
Couch](https://www.simonpcouch.com) for their help and feedback!