---
title: "Get started with CAR-LASSO models"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Get started with CAR-LASSO models}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r setup}
library(CARlasso)
```

`CARlasso` is the main interface to work with the model, for details see the [reference](https://yunyishen.ml/CAR-LASSO/dev/reference/CARlasso.html). 

## CAR-LASSO Network for Gaussian data

This is the case when data is Gaussian or can be transformed to Gaussian, for example, (log) biomass of trees.
We will provide an example with simulated data. The use of the `Carlasso` is similar to `lm`, we could supply a formula and a dataframe with both responses and predictors.

First, we simulate data under a 5-node AR1 model:

```{r ar1data}
set.seed(42)
dt <- simu_AR1(n=100,k=5, rho=0.7)
head(dt)

```

To use the Normal version, we should set `link="identity"` which is the default. In this case, we are setting `adaptive=TRUE` to use the adaptive version of CAR-LASSO (for more details, see [the paper](https://arxiv.org/abs/2012.08397)):

```{r ar1example_first, eval = FALSE}
car_res <- CARlasso(y1+y2+y3+y4+y5~x1+x2+x3+x4+x5, data = dt, adaptive = TRUE)
plot(car_res,tol = 0.05)
```

The color of the edge represents the type of correlation (negative=blue, positive=red) and the width of the edge corresponds to the effect size. Response nodes are represented by circles and predictor nodes are represented by triangles.

We can have a more formal horseshoe inference on the structure of the network which will update the `car_res` object:

```{r horseshoe_1,eval = FALSE}
# with horseshoe inference
car_res <- horseshoe(car_res)
plot(car_res)

```

## CAR-LASSO Network for compositional data

This is common in the case of microbe-related studies and some ecological applications with relative abundances. For instance, microbe relative abundance come from sequencing and in this case, the sum of "abundance" is determined by the sequence depth rather than the real total abundance. The data is usually described as "compositional". In `CARlasso`, this type of data are modeled as Logit-Normal-multinomial. In this case, we need to have a "reference level" taxa and all others are "relative" to this taxa.

First, we take a look at the data which is still a dataframe with all predictors and responses

```{r comp_data}
mgp154[1:5,1:7]
```

To run the composition model, we need to set `link="logit"`

```{r compositional1, eval = FALSE}
gut_res <- CARlasso(Alistipes+Bacteroides+
                        Eubacterium+Parabacteroides+all_others~
                        BMI+Age+Gender+Stratum,
                    data = mgp154,link = "logit", 
                    adaptive = TRUE, n_iter = 2000, 
                    n_burn_in = 1000, thin_by = 2)
```

Note that in this case `all_others` (an existing column in our data), i.e. the last one in the left hand side of the formula will be the reference level.

We can update the network inference by a horseshoe method to determine when edges will be considered non-existent. More details can be found in [the paper](https://arxiv.org/abs/2012.08397).

```{r horseshoe_comp, eval = FALSE}
# horseshoe will take a while, as it needs to sample the latent normal too
gut_res <- horseshoe(gut_res)
plot(gut_res)
```

## CAR-LASSO Network for counting data

This is common in a lot of ecological applications. For instance, number of seedlings within a site. The responses are counts rather than continuous. In `CARlasso`, it is modeled as Poisson with log-Normal rate. 

We will use the same compositional data as before to illustrate the counts model. However, it is important to note that relative abundances should not be considered as counts. To distinguish between compositional and count data, one can ask the question: **is the sum decided by us?** If yes, we want to use compositional models. 

To run the count model, we need to set `link="log"`:

```{r counting, eval = FALSE}
gut_res <- CARlasso(Alistipes+Bacteroides+
                        Eubacterium+Parabacteroides+all_others~
                        BMI+Age+Gender+Stratum,
                    data = mgp154,link = "log", 
                    adaptive = TRUE, 
                    r_beta = 0.1, # default sometimes cause singularity in Poisson model due to exponential transformation, slightly change can fix it.
                    n_iter = 2000, 
                    n_burn_in = 1000, thin_by = 2)
# horseshoe will take a while, as it's currently implemented in R rather than C++
gut_res <- horseshoe(gut_res)
plot(gut_res)
```

