---
title: "Section 1: Installing the R package bigDM"
author: "Aritz Adin"
date: "2024-08-13"
date-format: medium
format:
  html:
    toc: true
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)  
```

## Manual for installing the `bigDM` package

The R package [`bigDM`](https://cran.r-project.org/web/packages/bigDM/index.html) implements several spatial and spatio-temporal scalable disease mapping models for high-dimensional count data using the integrated nested Laplace approximation (INLA) technique for approximate Bayesian inference in latent Gaussian models ([Orozco-Acosta et al., 2021](https://doi.org/10.1016/j.spasta.2021.100496); [Orozco-Acosta et al., 2023](https://doi.org/10.1016/j.cmpb.2023.107403); and [Vicente et al., 2023](https://doi.org/10.1007/s11222-023-10263-x)).

### Prerequisites

Before installing `bigDM`, ensure that you have the following prerequisites:

-   **R**: Make sure you have `R` installed on your system. You can download and install it from [CRAN](https://cran.r-project.org/).

-   **RStudio (optional)**: For a better development experience, you might want to use RStudio, which can be downloaded from [RStudio's official website](https://rstudio.com/).

-   **Rtools:** R version 4.0 and newer for Windows requires Rtools to build R packages containing C, C++, or Fortran code from source. [Installing Rtools for Windows](https://cran.r-project.org/bin/windows/Rtools/)

-   **R-INLA package**: Model fitting and inference is fully Bayesian using the well-known INLA technique through the `R-INLA` package.

    To install the latest stable version:

    ```{r, eval=FALSE}
    install.packages("INLA",repos=c(getOption("repos"),INLA="https://inla.r-inla-download.org/R/stable"), dep=TRUE) 
    ```

    ***IMPORTANT NOTES**:*

    -   **At least the stable version of INLA 22.11.22 (or newer) must be installed for the correct use of the `bigDM` package.**

    -   Visit the [R-INLA website](https://www.r-inla.org/download-install) for further details about how to install the package on MacOS and for information on version compatibilities.

### Installation

1.  Install from CRAN

    ```{r, eval=FALSE}
    install.packages("bigDM", dependencies=TRUE) 
    ```

2.  Install from GitHub (development version)

    ```{r, eval=FALSE}
    # Install devtools package from CRAN repository
    install.packages("devtools")

    # Load devtools library
    library(devtools)

    # In some Linux OS, it might be necessary to first install the following packages
    install.packages(c("cpp11","proxy","progress","tzdb","vroom"))

    # Install bigDM from GitHub repository
    install_github("spatialstatisticsupna/bigDM")
    ```

### Verifying the installation

Load the package and check its documentation:

```{r, message=FALSE, warning=FALSE}
library(bigDM)
packageVersion("bigDM")

help("bigDM-package")
```

Run a test function:

```{r, message=FALSE, warning=FALSE}
library(sf)
library(tmap)
library(RColorBrewer)

tmap4 <- packageVersion("tmap") >= "3.99"


## 1) Load simulated colorectal cancer mortality data 
data("Carto_SpainMUN")
str(Carto_SpainMUN)

## 2) Divide the spatial domain into provinces
carto.list <- divide_carto(Carto_SpainMUN, ID.group="region")
carto.NAV <- carto.list$Navarra
head(carto.NAV)

## 3) Plot a map with the SMRs
paleta <- brewer.pal(8,"RdYlGn")[8:1]
values <- c(-Inf,0.77,0.83,0.91,1,1.10,1.20,1.30,Inf)

tmap_mode("view")

if(tmap4){
  tm_shape(carto.NAV) + 
    tm_polygons(fill="SMR", id="name", popup.vars=c("ID","obs","exp","SMR"),
                fill.scale=tm_scale(values=paleta, breaks=values, interval.closure="left"),
                fill.legend=tm_legend("SMR", show=TRUE, reverse=TRUE,
                                      position=tm_pos_in("right","top"))) + 
    tm_title("Colorectal cancer")
}else{
  tm_shape(carto.NAV) + 
    tm_polygons(col="SMR", id="name", palette=paleta,
                popup.vars=c("ID","obs","exp","SMR"),
                legend.show=T, legend.reverse=T,
                style="fixed", breaks=values, interval.closure="left") + 
    tm_layout(main.title="Colorectal cancer", main.title.position="center",
              legend.outside=T, legend.outside.position="right", legend.frame=F)
}
```
