---
title: "Graphical LASSO support"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Graphical LASSO support}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

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

We also implemented the standard Graphical LASSO to infer a graph without predictors. The main interface is `bGlasso`. For more details, please see the [reference](https://yunyishen.ml/CAR-LASSO/dev/reference/bGlasso.html).

## GLASSO Network for Gaussian data

This is the case when data is Gaussian or can be transformed to Gaussian. We only need responses in this case. We use a 5-node AR1 model to simulate data:

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

```

Unlike `CARlasso`, we do not really need to have a formula (because we do not have predictors, only responses). To use the Normal version, we should set `link="identity"` which is the default. 


```{r ar1example_first, eval = FALSE}
glassores <- bGlasso(data = dt)
plot(glassores)
```

Some of the connections are actually due to predictors. 

## GLASSO 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". 

We first extract only the responses that we want to include in the model:

```{r comp_data}
dt <- mgp154[,c("Alistipes","Bacteroides",
                        "Eubacterium","Parabacteroides","all_others")]
```

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

```{r compositional1, eval = FALSE}
gut_res <- bGlasso( data = dt,link = "logit", 
                    n_iter = 2000, 
                    n_burn_in = 1000, thin_by = 2)
plot(gut_res)
```

Note that in this case the last one in the dataframe will be the reference level (`all_others` in this case).

## GLASSO 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. 
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"`. Note that we only include the first 4 responses:

```{r counting, eval = FALSE}
gut_res <- gut_res <- bGlasso( data = dt[,1:4],link = "log", 
                    n_iter = 2000, 
                    n_burn_in = 1000, thin_by = 2)
plot(gut_res)
```



