```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
source("R/models.R") # (includes loading of packages, function and objects)
```

First thing first: check out the model results and look for interesting species pairs

# Relative importance of covariates
```{r fig.height=10, fig.width=10, dev='png'}
p_relimp_pois
```


## Groupers

```{r}
# Relative importance
grps_pois_relimp

# And coefficients
grps_coefs

```

### Species pairs for visualisations
Find out which species pairs are effected by covarites interaction

**Temperature**
```{r}
lapply(grps_pois$key_coefs, function(x) x %>% 
         filter(str_detect(string = Variable, pattern = "temp_")))

```

*Epinephelus.costae* ~ *Serranus.cabrilla* x Temperature (RI = 0.06)

**MPA**
```{r}
lapply(grps_pois$key_coefs, function(x) x %>% 
         filter(str_detect(string = Variable, pattern = "mpa_")))

```

*Epinephelus.costae* ~ *Serranus.cabrilla* x MPA (RI = 0.07)
*Epinephelus.costae* ~ *Epinephelus.marginatus* x MPA (RI = 0.06)
*Epinephelus.marginatus* ~ *Epinephelus.costae* x MPA (RI = 0.04)

### Visualise abundance of species *i*
(A) Using `MRF_predict` to predict the abundance of species *i* along temperature gradient / MPA status in two scenarios:
1. When species *j* is absent
2. When species *j* is present (at its 90th percentile of abundance)

(B) Using raw data (transformed abundance of species *i*) and smoothed GAM function along temperature gradient / MPA status in two scenarios:
1. When species *j* is absent
2. When species *j* is present (at any abundance)

**Epinephelus.costae ~ Serranus.cabrilla x Temperature**
RI = 0.06

```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_temp_pred_pair("Epinephelus.costae", "Serranus.cabrilla", grps_mat, grps_pois, groupers)
vis_raw_temp(species_i = "Epinephelus.costae", species_j = "Serranus.cabrilla", spp_mat = grps_mat)
```


**Epinephelus.costae ~ Serranus.cabrilla x MPA**
RI = 0.07

```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Epinephelus.costae", "Serranus.cabrilla", grps_mat, grps_pois, groupers)
vis_raw_mpa(spp_mat = grps_mat, "Epinephelus.costae", "Serranus.cabrilla")

```

**Epinephelus.costae ~ Epinephelus.marginatus x MPA**
RI = 0.06

```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Epinephelus.costae", "Epinephelus.marginatus", grps_mat, grps_pois, groupers)
vis_raw_mpa(spp_mat = grps_mat, "Epinephelus.costae", "Epinephelus.marginatus")
```

**Epinephelus.marginatus ~ Epinephelus.costae x MPA**
RI = 0.04

```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Epinephelus.marginatus", "Epinephelus.costae", grps_mat, grps_pois, groupers)
vis_raw_mpa(spp_mat = grps_mat, "Epinephelus.marginatus", "Epinephelus.costae")

```

## Seabreams

```{r}
# Relative importance
dip_pois_relimp

# And coefficients
dip_coefs
```

### Species pairs for visualisations
Find out which species pairs are effected by covarites interaction

**Temperature**
```{r}
lapply(dip_pois$key_coefs, function(x) x %>% 
         filter(str_detect(string = Variable, pattern = "temp_")))

```

*Diplodus.annularis* ~ *Diplodus.vulgaris* x Temperature (RI = 0.07)
*Diplodus.puntazzo* ~ *Diplodus.vulgaris* x Temperature (RI = 0.01)
*Diplodus.sargus* ~ *Diplodus.vulgaris* x Temperature (RI = 0.04)
*Diplodus.vulgaris* ~ *Diplodus.sargus* x Temperature (RI = 0.04)
*Diplodus.vulgaris* ~ *Diplodus.annularis* x Temperature (RI = 0.01)

**MPA**
```{r}
lapply(dip_pois$key_coefs, function(x) x %>% 
         filter(str_detect(string = Variable, pattern = "mpa_")))

```

*Diplodus.annularis* ~ *Diplodus.vulgaris* x MPA (RI = 0.04)
*Diplodus.puntazzo* ~ *Diplodus.vulgaris* x MPA (RI = 0.33)
*Diplodus.sargus* ~ *Diplodus.vulgaris* x MPA (RI = 0.19)
*Diplodus.vulgaris* ~ *Diplodus.sargus* x MPA (RI = 0.22)
*Diplodus.vulgaris* ~ *Diplodus.puntazzo* x MPA (RI = 0.07)


### Visualise abundance of species *i*
(A) Using `MRF_predict` to predict the abundance of species *i* along temperature gradient / MPA status in two scenarios:
1. When species *j* is absent
2. When species *j* is present (at its 90th percentile of abundance)

(B) Using raw data (transformed abundance of species *i*) and smoothed GAM function along temperature gradient / MPA status in two scenarios:
1. When species *j* is absent
2. When species *j* is present (at any abundance)

**Diplodus.annularis ~ Diplodus.vulgaris x Temperature**
RI = 0.07
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_temp_pred_pair("Diplodus.annularis", "Diplodus.vulgaris", dip_mat, dip_pois, diplodus)
vis_raw_temp(species_i = "Diplodus.annularis", species_j = "Diplodus.vulgaris", spp_mat = dip_mat)
```
**Diplodus.vulgaris ~ Diplodus.annularis x Temperature**
RI = 0.01
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_temp_pred_pair("Diplodus.vulgaris", "Diplodus.annularis", dip_mat, dip_pois, diplodus)
vis_raw_temp(species_i = "Diplodus.vulgaris", species_j = "Diplodus.annularis", spp_mat = dip_mat)

```

**Diplodus.puntazzo ~ Diplodus.vulgaris x Temperature**
RI = 0.01
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_temp_pred_pair("Diplodus.puntazzo", "Diplodus.vulgaris", dip_mat, dip_pois, diplodus)
vis_raw_temp(species_i = "Diplodus.puntazzo", species_j = "Diplodus.vulgaris", spp_mat = dip_mat)

```


**Diplodus.sargus ~ Diplodus.vulgaris x Temperature**
RI = 0.05
Coef = 0.13
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_temp_pred_pair("Diplodus.sargus", "Diplodus.vulgaris", dip_mat, dip_pois, diplodus)
vis_raw_temp(species_i = "Diplodus.sargus", species_j = "Diplodus.vulgaris", spp_mat = dip_mat)

```

**Diplodus.vulgaris ~ Diplodus.sargus x Temperature**
RI = 0.05
Coef = 0.13
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_temp_pred_pair("Diplodus.vulgaris", "Diplodus.sargus", dip_mat, dip_pois, diplodus)
vis_raw_temp(species_i = "Diplodus.vulgaris", species_j = "Diplodus.sargus", spp_mat = dip_mat)

```


**Diplodus.annularis ~ Diplodus.vulgaris x MPA**
RI = 0.04
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Diplodus.annularis", "Diplodus.vulgaris", dip_mat, dip_pois, diplodus)
vis_raw_mpa(species_i = "Diplodus.annularis", species_j = "Diplodus.vulgaris", spp_mat = dip_mat)

```

**Diplodus.puntazzo ~ Diplodus.vulgaris x MPA**
RI = 0.33

```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Diplodus.puntazzo", "Diplodus.vulgaris", dip_mat, dip_pois, diplodus)
vis_raw_mpa(species_i = "Diplodus.puntazzo", species_j = "Diplodus.vulgaris", spp_mat = dip_mat)

```

**Diplodus.vulgaris ~ Diplodus.puntazzo x MPA**
RI = 0.07
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Diplodus.vulgaris", "Diplodus.puntazzo", dip_mat, dip_pois, diplodus)
vis_raw_mpa(species_i = "Diplodus.vulgaris", species_j = "Diplodus.puntazzo", spp_mat = dip_mat)

```


**Diplodus.sargus ~ Diplodus.vulgaris x MPA** 
RI = 0.19
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Diplodus.sargus", "Diplodus.vulgaris", dip_mat, dip_pois, diplodus)
vis_raw_mpa(species_i = "Diplodus.sargus", species_j = "Diplodus.vulgaris", spp_mat = dip_mat)

```

**Diplodus.vulgaris ~ Diplodus.sargus x MPA**
RI = 0.22
```{r echo=FALSE, message=FALSE, warning=FALSE, dev='png', fig.show='hold', fig.ncol=2}
vis_mpa_pred_pair("Diplodus.vulgaris", "Diplodus.sargus", dip_mat, dip_pois, diplodus)
vis_raw_mpa(species_i = "Diplodus.vulgaris", species_j = "Diplodus.sargus", spp_mat = dip_mat)

```






