---
title: "Hypothesis-Testing"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Hypothesis-Testing}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

has_cmdstan <- instantiate::stan_cmdstan_exists()
```

## Setup

First, lets load the package:
```{r setup}
library(bvarnet)
library(qgraph)
```

## Data

Now, we can load the example data:
```{r}
data(studentlife)
```

There is some missing data in the dataset. The models default options handle this by themselves. For a further elaboration on this, you can read `vignette("Missing-Data")`.

## Model Estimation

This time, well simply use the default priors, for the ordinal model they are

```{r, eval = has_cmdstan, results='hide', message=FALSE}
fit <- bvar(
  id_col = "id",
  time_col = "day",
  y_cols = c("anxious", "calm", "conventional", "critical", "dependable"),
  x_cols = c("sleep_hour"),
  re_cols = NULL,
  re_temporal = FALSE,
  K = 1,
  data = studentlife,
  family = c("ordinal"),
  priors = set_priors(),
  iter = 4000,
  warmup = 1000,
  chains = 4,
  cores = 4,
  seed = 1337
)
```


## Model Output

Again, lets get the model overview and summary:
```{r, eval = has_cmdstan}
print(fit)
summary(fit)
```

## Hypothesis Testing with Bayes Factors

To estimate Bayes factors for a single parameter, or a set of parameters, we can use the `bf_table()` function. Bayes factors are computed via the **Savage–Dickey density ratio** (SDDR), which, for a point null $H_0 : \theta = \theta_0$ nested in $H_1 : \theta \neq \theta_0$ with shared nuisance parameters, gives

$$
\mathrm{BF}_{01} \;=\; \frac{p(\theta = \theta_0 \mid y)}{p(\theta = \theta_0)},
$$

i.e. the ratio of the posterior to the prior density evaluated at the null value. `bf_table()` reports $\mathrm{BF}_{10} = 1 / \mathrm{BF}_{01}$ by approximating the posterior density at $\theta_0 = 0$ from the MCMC draws.

To perform a hypothesis test on effects involving a specific variable, we can call the `bf_table()` function, specifying which variable we want to look at using the `variable` argument. Lets investigate if the hours of sleep the individual got in the previous night (`sleep_hour`) has an influence on the network structure:

```{r}
bf_tab <- bf_table(fit, variable = "sleep_hour")
bf_tab
```

The output now shows 6 rows. The five "Fixed Effect" rows show if the the covariate `sleep_hour` has an influence on each node separately. The "Fixed Effect (join)" row displays a single Bayes factor that in this case tests the hypothesis

H: Is there a difference in the baseline of the network structure that is explained by the variable `sleep_hour`

As we can see, the `BF_{10} = bf_tab[]`. Therefore we can conlude that the variable `sleep_hour` does not influence the baseline level of the network.


## Joint Bayes Factors

For more elaborate analyses, or to report more results at once, we can get a table with all single and joint Bayes factors. For this we can call the `bf_table(fit)` function

```{r}
bf_tab <- bf_table(fit)
bf_tab
```

The table can be used to easily get a set of Bayes factors. If we have hypotheses concerning the temporal structure, we can call the `bf_table(fit)` function using the argument `type = "temporal"`. 

```{r}
bf_temp <- bf_table(fit, type = "temporal")
bf_temp
```

The following arguments are available as filters for the `type` argument: "ar", "cl", "intercepts", "fe", "lag_fe", and "temporal".


