---
title: "simmr: quick start guide"
author: "Andrew Parnell"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{quick_start}
  %\VignetteEncoding{UTF-8}
  %\VignetteEngine{knitr::rmarkdown}
editor_options: 
  chunk_output_type: console
---

## Step 1: install `simmr`

Use:
```{r, eval = FALSE}
install.packages("simmr")
```

then
```{r, message=FALSE}
library(simmr)
```

## Step 2: load in the data

Some geese isotope data is included with this package. Find where it is with:
```{r, eval = FALSE}
system.file("extdata", "geese_data.xls", package = "simmr")
```

Load into R with:
```{r, echo = FALSE}
if (!requireNamespace("readxl", quietly = TRUE)) {
  stop("readxl needed for this vignette to work. Please install it.",
    call. = FALSE
  )
}
```
```{r}
library(readxl)
path <- system.file("extdata", "geese_data.xls", package = "simmr")
geese_data <- lapply(excel_sheets(path), read_excel, path = path)
```

If you want to see what the original Excel sheet looks like you can run `system(paste('open',path))`.

We can now separate out the data into parts 
```{r}
targets <- geese_data[[1]]
sources <- geese_data[[2]]
TEFs <- geese_data[[3]]
concdep <- geese_data[[4]]
```

Note that if you don't have TEFs or concentration dependence you can set these all to the value 0 or just leave them blank in the step below. 

## Step 3: load the data into `simmr`

```{r}
geese_simmr <- simmr_load(
  mixtures = targets[, 1:2],
  source_names = sources$Sources,
  source_means = sources[, 2:3],
  source_sds = sources[, 4:5],
  correction_means = TEFs[, 2:3],
  correction_sds = TEFs[, 4:5],
  concentration_means = concdep[, 2:3],
  group = as.factor(paste("Day", targets$Time))
)
```

## Step 4: plot the data 

```{r,fig.align = 'center',fig.width = 7,fig.height = 5}
plot(geese_simmr, group = 1:8)
```

## Step 5: run through `simmr` and check convergence

```{r, results = 'hide', message = FALSE}
geese_simmr_out <- simmr_mcmc(geese_simmr)
summary(geese_simmr_out,
  type = "diagnostics",
  group = 1
)
```

Check that the model fitted well:
```{r,fig.align = 'center',fig.width = 7,fig.height = 5}
posterior_predictive(geese_simmr_out, group = 5)
```

## Step 6: look at the output

Look at the influence of the prior:
```{r,fig.align = 'center',fig.width = 7,fig.height = 5}
prior_viz(geese_simmr_out)
```

Look at the histogram of the dietary proportions:
```{r,fig.align = 'center',fig.width = 7,fig.height = 5}
plot(geese_simmr_out, type = "histogram")
```

```{r,fig.align = 'center',fig.width = 7,fig.height = 5}
compare_groups(geese_simmr_out,
  groups = 1:4,
  source_name = "Enteromorpha"
)
```

For the many more options available to run and analyse output, see the main vignette via `vignette('simmr')`
