---
title: "The Bootstrap and Consonance Functions"
output: rmarkdown::html_vignette
bibliography: references.bib
link-citations: yes
csl: american-medical-association.csl
vignette: >
  %\VignetteIndexEntry{The Bootstrap and Consonance Functions}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r setup, include = FALSE}
knitr::opts_chunk$set(
  message = TRUE,
  warning = TRUE,
  collapse = TRUE,
  comment = "#>"
)
library(concurve)
```

Some authors have shown that the bootstrap distribution is equal to the confidence distribution because it meets the definition of a consonance distribution.[@efron1994; @efron2018; @xie2013isr] The bootstrap distribution and the asymptotic consonance distribution would be defined as: 

$$H_{n}(\theta)=1-P\left(\hat{\theta}-\hat{\theta}^{*} \leq \hat{\theta}-\theta | \mathbf{x}\right)=P\left(\hat{\theta}^{*} \leq \theta | \mathbf{x}\right)$$

Certain bootstrap methods such as the `BCa` method and `t`-bootstrap method also yield second order accuracy of consonance distributions. 

$$H_{n}(\theta)=1-P\left(\frac{\hat{\theta}^{*}-\hat{\theta}}{\widehat{S E}^{*}\left(\hat{\theta}^{*}\right)} \leq \frac{\hat{\theta}-\theta}{\widehat{S E}(\hat{\theta})} | \mathbf{x}\right)$$

Here, I demonstrate how to use these particular bootstrap methods to arrive at consonance curves and densities. 

We'll use the `Iris` dataset and construct a function that'll yield a parameter of interest.

## The Nonparametric Bootstrap 

```{r echo=TRUE, fig.height=4.5, fig.width=6}
iris <- datasets::iris
foo <- function(data, indices) {
  dt <- data[indices, ]
  c(
    cor(dt[, 1], dt[, 2], method = "p")
  )
}
```

We can now use the `curve_boot()` method to construct a function. The default method used for this function is the "`Bca`" method provided by the [`bcaboot`](https://cran.r-project.org/package=bcaboot) package.[@efron2018] 

```{r include=FALSE}
y <- curve_boot(data = iris, func = foo, method = "bca", replicates = 2000, steps = 1000)
```

I will suppress the output of the function because it is unnecessarily long. But we've placed all the estimates into a list object called y. 

The first item in the list will be the consonance distribution constructed by typical means, while the third item will be the bootstrap approximation to the consonance distribution. 

```{r echo=TRUE, fig.height=4.5, fig.width=6}
ggcurve(data = y[[1]], nullvalue = TRUE)
ggcurve(data = y[[3]], nullvalue = TRUE)
```

We can also print out a table for TeX documents

```{r echo=TRUE, fig.height=2, fig.width=4}
(gg <- curve_table(data = y[[1]], format = "image"))
```

More bootstrap replications will lead to a smoother function. But for now, we can compare these two functions to see how similar they are. 

```{r echo=TRUE, fig.height=4.5, fig.width=6}
plot_compare(y[[1]], y[[3]])
```

If we wanted to look at the bootstrap standard errors, we could do so by loading the fifth item in the list 
```{r echo=TRUE, fig.height=4.5, fig.width=6}
knitr::kable(y[[5]])
```

where in the top row, `theta` is the point estimate, and `sdboot` is the bootstrap estimate of the standard error, `sdjack` is the jacknife estimate of the standard error. `z0` is the bias correction value and `a` is the acceleration constant. 

The values in the second row are essentially the internal standard errors of the estimates in the top row. 

* * * 

The densities can also be calculated accurately using the `t`-bootstrap method. Here we use a different dataset to show this

```{r echo=TRUE, fig.height=4.5, fig.width=6}
library(Lock5Data)
dataz <- data(CommuteAtlanta)
func <- function(data, index) {
  x <- as.numeric(unlist(data[1]))
  y <- as.numeric(unlist(data[2]))
  return(mean(x[index]) - mean(y[index]))
}
```

Our function is a simple mean difference. This time, we'll set the method to "`t`" for the `t`-bootstrap method

```{r echo=TRUE, fig.height=4.5, fig.width=6}
z <- curve_boot(data = CommuteAtlanta, func = func, method = "t", replicates = 2000, steps = 1000)
ggcurve(data = z[[1]], nullvalue = FALSE)
ggcurve(data = z[[2]], type = "cd", nullvalue = FALSE)
```

The consonance curve and density are nearly identical. With more bootstrap replications, they are very likely to converge. 

```{r echo=TRUE, fig.height=2, fig.width=4}
(zz <- curve_table(data = z[[1]], format = "image"))
```

## The Parametric Bootstrap

For the examples above, we mainly used nonparametric bootstrap methods. Here I show an example using the parametric `Bca` bootstrap and the results it yields.

First, we'll load our data again and set our function.

```{r echo=TRUE, fig.height=4.5, fig.width=6}
data(diabetes, package = "bcaboot")
X <- diabetes$x
y <- scale(diabetes$y, center = TRUE, scale = FALSE)
lm.model <- lm(y ~ X - 1)
mu.hat <- lm.model$fitted.values
sigma.hat <- stats::sd(lm.model$residuals)
t0 <- summary(lm.model)$adj.r.squared
y.star <- sapply(mu.hat, rnorm, n = 1000, sd = sigma.hat)
tt <- apply(y.star, 1, function(y) summary(lm(y ~ X - 1))$adj.r.squared)
b.star <- y.star %*% X
```

Now, we'll use the same function, but set the method to "`bcapar`" for the parametric method. 

```{r echo=TRUE, fig.height=4.5, fig.width=6}
df <- curve_boot(method = "bcapar", t0 = t0, tt = tt, bb = b.star)
```

Now we can look at our outputs. 

```{r echo=TRUE, fig.height=4.5, fig.width=6}
ggcurve(df[[1]], nullvalue = FALSE)
ggcurve(df[[3]], nullvalue = FALSE)
```

We can compare the functions to see how well the bootstrap approximations match up 
```{r echo=TRUE, fig.height=4.5, fig.width=6}
plot_compare(df[[1]], df[[3]])
```

We can also look at the density function 

```{r echo=TRUE, fig.height=4.5, fig.width=6}
ggcurve(df[[5]], type = "cd", nullvalue = FALSE)
```

That concludes our demonstration of the bootstrap method to approximate consonance functions. 

* * * 

# References

* * * 
