---
title: "Test of the functions gridpts, h1, hupdate"
output:
  rmarkdown::html_document:
    toc: true
    toc_float: true
    toc_depth: 2
    highlight: "textmate"
    css: "custom.css"
# bibliography: "example.bib"
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteIndexEntry{Test of the functions gridpts, h1, hupdate}
---

```{r, message=FALSE}
library(tibble)
library(dplyr)
library(testthat)
library(gt)
devtools::load_all()
```

# Tests of `gridpts`

## Default (N(0,1))

approximate variance of standard normal (i.e., 1)

```{r}
x1 <- gridpts_(mu = 0, a = -20, b = 20, r = 18)
x2 <- gridpts(mu = 0, a = -20, b = 20, r = 18)
x3a <- gsDesign::normalGrid(bounds = c(-10, 10), mu = 0, sigma = 1, r = 18)
x3b <- gsDesign::normalGrid(bounds = c(-20, 20), mu = 0, sigma = 1, r = 18)
x3c <- gsDesign::normalGrid(bounds = c(-80, 80), mu = 0, sigma = 1, r = 18)
```

```{r, echo=FALSE}
tibble(
  Rcpp = length(x1$z),
  R = length(x2$z),
  `C with (a, b) = (-10, 10)` = length(x3a$z),
  `C with (a, b) = (-20, 20)` = length(x3b$z),
  `C with (a, b) = (-80, 80)` = length(x3c$z)
) %>%
  gt() %>%
  tab_header("Number of grid points")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x2$z)),
  diff_w = sum(abs(x1$w - x2$w))
) %>%
  gt() %>%
  tab_header(title = "Difference between Rcpp and R", subtitle = "in gsdmvn")
```

```{r, echo=FALSE, warning=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x3a$z)),
  diff_w = sum(abs(x1$w - x3a$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign (C) and gsdmvn (Rcpp)", subtitle = "under (a, b) = (-10, 10)")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x3b$z)),
  diff_w = sum(abs(x1$w - x3b$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign and gsdmvn", subtitle = "under (a, b) = (-20, 20)")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x3b$z)),
  diff_w = sum(abs(x1$w - x3b$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign and gsdmvn", subtitle = "under (a, b) = (-80, 80)")
```

## Approximate probability of N(0,1) above .95 quantile (i.e., .05)

```{r}
x1 <- gridpts_(mu = 0, a = qnorm(0.95), b = Inf, r = 18)
x2 <- gridpts(mu = 0, a = qnorm(0.95), b = Inf, r = 18)
x3a <- gsDesign::normalGrid(bounds = c(qnorm(0.95), 10), mu = 0, sigma = 1, r = 18)
x3b <- gsDesign::normalGrid(bounds = c(qnorm(0.95), 20), mu = 0, sigma = 1, r = 18)
x3c <- gsDesign::normalGrid(bounds = c(qnorm(0.95), 80), mu = 0, sigma = 1, r = 18)
```

```{r, echo=FALSE}
tibble(
  Rcpp = length(x1$z),
  R = length(x2$z),
  `C with (a, b) = (-10, 10)` = length(x3a$z),
  `C with (a, b) = (-20, 20)` = length(x3b$z),
  `C with (a, b) = (-80, 80)` = length(x3c$z)
) %>%
  gt() %>%
  tab_header("Number of grid points")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x2$z)),
  diff_w = sum(abs(x1$w - x2$w))
) %>%
  gt() %>%
  tab_header(title = "Difference between Rcpp and R", subtitle = "in gsdmvn")
```

```{r, echo=FALSE, warning=FALSE}
tibble(
  diff_z = sum(abs(x2$z - x3a$z)),
  diff_w = sum(abs(x2$w - x3a$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign (C) and gsdmvn (Rcpp)", subtitle = "under (a, b) = (-10, 10)")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x2$z - x3b$z)),
  diff_w = sum(abs(x2$w - x3b$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign and gsdmvn", subtitle = "under (a, b) = (-20, 20)")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x2$z - x3b$z)),
  diff_w = sum(abs(x2$w - x3b$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign and gsdmvn", subtitle = "under (a, b) = (-80, 80)")
```

## Approximate probability of N(0.5, 1) above .95 quantile (i.e., .05)

```{r}
x1 <- gridpts_(mu = 0.5, a = qnorm(0.95), b = Inf, r = 18)
x2 <- gridpts(mu = 0.5, a = qnorm(0.95), b = Inf, r = 18)
x3a <- gsDesign::normalGrid(bounds = c(qnorm(0.95), 10), mu = 0.5, sigma = 1, r = 18)
x3b <- gsDesign::normalGrid(bounds = c(qnorm(0.95), 20), mu = 0.5, sigma = 1, r = 18)
x3c <- gsDesign::normalGrid(bounds = c(qnorm(0.95), 80), mu = 0.5, sigma = 1, r = 18)
```

```{r, echo=FALSE}
tibble(
  Rcpp = length(x1$z),
  R = length(x2$z),
  `C with (a, b) = (-10, 10)` = length(x3a$z),
  `C with (a, b) = (-20, 20)` = length(x3b$z),
  `C with (a, b) = (-80, 80)` = length(x3c$z)
) %>%
  gt() %>%
  tab_header("Number of grid points")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x2$z)),
  diff_w = sum(abs(x1$w - x2$w))
) %>%
  gt() %>%
  tab_header(title = "Difference between Rcpp and R", subtitle = "in gsdmvn")
```

```{r, echo=FALSE, warning=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x3a$z)),
  diff_w = sum(abs(x1$w - x3a$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign (C) and gsdmvn (Rcpp)", subtitle = "under (a, b) = (-10, 10)")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x3b$z)),
  diff_w = sum(abs(x1$w - x3b$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign and gsdmvn", subtitle = "under (a, b) = (-20, 20)")
```

```{r, echo=FALSE}
tibble(
  diff_z = sum(abs(x1$z - x3b$z)),
  diff_w = sum(abs(x1$w - x3b$gridwgts))
) %>%
  gt() %>%
  tab_header(title = "Difference between gsDesign and gsdmvn", subtitle = "under (a, b) = (-80, 80)")
```
