---
title: "Credible Intervals"
author: "Donald R. Williams"
output: 
  rmarkdown::html_vignette:
vignette: >
  %\VignetteIndexEntry{Credible Intervals}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
  \usepackage{amsmath}
params:
  EVAL: !r identical(Sys.getenv("NOT_CRAN"), "true")
---
  
```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 7, fig.align = "center")
library(BGGM)
```


# A measure of uncertainty
It is common place in psychological networks to compute "confidence" intervals. This can also be done in **BGGM**. In a Bayesian context, they are called credible intervals. Note the intepretation also differs between confidence and credible intervals, but that is not 
something I discuss here (see google search).

## Fit Model
First fit the model.
```{r}
# data
Y <- BGGM::bfi[,1:5]

# sample posterior
fit <- estimate(Y, analytic = FALSE, 
                iter = 5000)
```

## Summarize the Posterior Distributions
Next summarize the partial correlations.
```{r}
summary_fit <- summary(fit, cred = 0.90)

# print
summary_fit
```


## Plot the Posterior Distributions
```{r}
summary_plot <- plot(summary_fit)
```

### Plot Customization
The object `summary_plot` can then be further customized. The following is a simple example.

```{r}
library(ggplot2)

# plot 
plot(summary_fit, width = 0) +
  # flip
  coord_flip() +
  # change the theme
  theme_bw() +
  # remove legend
  theme(legend.position = "none", 
        panel.grid  = element_blank()) +
  # add line at zero
  geom_hline(yintercept = 0, 
             linetype = "dotted", 
             alpha = 0.30) +
  # change color to black border
  geom_point(color = "black", size = 2) +
  # make inner point white
  geom_point(color = "white", size = 1.75) +
  # color blind
  scale_color_manual(values = c("#999999", "#CC79A7")) 
```

When there are many partial correlations, it might be useful to add `scale_x_discrete(labels = NULL)` which will remove the names.
