---
title: "Introduction to mixedLSR"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to mixedLSR}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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


## Setup

Mixed, low-rank, and sparse multivariate regression (mixedLSR) provides tools for performing mixture regression when the coefficient matrix is low-rank and sparse. mixedLSR allows subgroup identification by alternating optimization with simulated annealing to encourage global optimum convergence. This method is data-adaptive, automatically performing parameter selection to identify low-rank substructures in the coefficient matrix.

```{r setup}
library(mixedLSR)
set.seed(1)
```

## Simulate Data

To demonstrate mixedLSR, we simulate a heterogeneous population where the coefficient matrix is low-rank and sparse and the number of coefficients to estimate is much larger than the sample size.

```{r simulate}
sim <- simulate_lsr(N = 100, k = 2, p = 30, m = 35)
```

## Compute Model

Then, we compute the model. We limit the number of iterations the model can run.

```{r compute}
model <- mixed_lsr(sim$x, sim$y, k = 2, alt_iter = 1, anneal_iter = 10, em_iter = 10, verbose = TRUE)
```

## Clustering Performance

Next, we can evaluate the clustering performance of mixedLSR by viewing a cross-tabulation of the partition labels and by computing the adjusted Rand index (ARI). In this case, mixedLSR perfectly clustered the data.

```{r cluster_perf}
table(sim$true, model$assign)
ari <- mclust::adjustedRandIndex(sim$true, model$assign)
print(paste("ARI:",ari))
```

## Coefficient Heatmaps

Lastly, we can view a heatmap of the coefficient matrices and compare them to the true simulated matrices.

```{r visualize}
plot_lsr(model$a)
plot_lsr(sim$a)
```


Reproducibility

```{r Reproducibility, collapse=TRUE}
sessionInfo()
```
