# SQUIC for R
SQUIC is a second-order, L1-regularized maximum likelihood method for performant large-scale sparse precision matrix estimation. This repository contains the source code for the R(v4.2) interface of SQUIC. 

**For an interactive session using SQUIC for R see Google Colab:**

[![SQUIC for R](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1WDH4juHjXGAYPJHbyWVeD--izlEWXmlR?usp=sharing)

## Installation

#### Step 1:
Download the library $\mathtt{libSQUIC}$ from [here](www.gitlab.ci.inf.usi.ch/SQUIC/libSQUIC), and follow its README instructions. The default and recommended location for $\mathtt{libSQUIC}$ is the home directory, i.e., `~/`. Note that precompiled versions are available.

#### Step 2:
Run the following command to install the library:
```R
library(devtools) 
install_git("https://www.gitlab.ci.inf.usi.ch/SQUIC/SQUIC_R.git")
```

The environment variable `SQUIC_LIB_PATH` defines the location of $\mathtt{libSQUIC}$. If this variable is not set, the default location of $\mathtt{libSQUIC}$ is `~/`. To change the default location, in R use the command `Sys.setenv(SQUIC_LIB_PATH="/path/to/squic/")`, replacing `/path/to/squic` with the path of where $\mathtt{libSQUIC}$ can be found.

## Example
In this example, we will use SQUIC to estimate the precision matrix of a synthetically generated dataset with correlated random variables, where the true precision matrix is tridiagonal.

```R
library(SQUIC)

p <- 1024
n <- 100
lambda <- .4

# generate a tridiagonal matrix
iC_star <- Matrix::bandSparse(p, p, (-1):1, list(rep(-.5, p-1), rep(1.25,   p), rep(-.5, p-1)))

# generate the data
set.seed(1)
z    <- replicate(n,rnorm(p))
iC_L <- chol(iC_star)
Y    <- matrix(solve(iC_L,z),p,n)

# run SQUIC
out <- SQUIC(Y,lambda)
```
For further details type ``help(SQUIC)`` or check the [User Manual](https://www.gitlab.ci.inf.usi.ch/SQUIC/gitlab-profile/-/raw/main/SQUIC_User_Manual.pdf).
