---
title: "Masking certain entries from imputation model"
date: "`r format(Sys.Date(), '%Y-%m-%d')`"
output: 
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 2
    fig_width: 7
    fig_height: 5
    dpi: 600
vignette: >
  %\VignetteIndexEntry{Masking certain entries from imputation model}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

There are some cases in which we want to tell the CISSVAE model not to impute certain missing data entries. For example, we may not want to impute patient biomarker values for collection dates past date of death.

We can use the `imputable_matrix` argument in the `run_cissvae()` function to mask these values. The binary `imputable_matrix` should have the same dimensions as the input dataframe, with each cell containing 1 (impute if missing) or 0 (do not impute, even if missing). 

Let's use a simple survival dataset as an example. The mock survival dataset and its imputable_matrix (dni) can be loaded using the data function. 


```{r setup, echo=FALSE, include=FALSE}
rm(list = ls())

library(rCISSVAE)
library(tidyverse)
library(kableExtra)
library(reticulate)
library(gtsummary)
set.seed(42)

```

```{r}
library(rCISSVAE)

data(dni, mock_surv)

mock_surv %>% 
   tbl_summary(include = -c("patient_id")) %>%
   as_kable()
```


### Understanding the 'Imputable' matrix

```{r}
mock_surv[7,] %>% kableExtra::kable()

dni[7,] %>%  kableExtra::kable()
```

If we look at the entry for P007, which has a death event at year 3, we can see that the biomarker entries after month 36 (aka year 3) are marked as non-imputable. 

When running the cissvae model with this dataset, it is important that the dataset and dni matrix have the same shape and column names. This allows us to take advantage of the `index_col` and `columns_ignore` arguments. We pass the dni matrix through the `imputable_matrix` argument. 

```{r eval=FALSE}
res <- run_cissvae(
  mock_surv,
  index_col = "patient_id",
  columns_ignore = c("death_event","death_year"),
  imputable_matrix = dni,   
  val_proportion = 0.3,
  return_clusters =FALSE,
  return_history = FALSE,
  epochs = 100,
  leiden_resolution = 0.01,
  k_neighbors = 5,
  return_silhouettes = FALSE
)
res$imputed_dataset[1:7,] %>% kableExtra::kable()

```

When we unpack the results and look at the imputed dataset, we can see that P007 still has NAs for biomarkers past 36 months. 

```{r include=FALSE, echo=FALSE}
res <- readRDS(system.file("extdata", "imputable.rds", package = "rCISSVAE"))
res[1:7,] %>% kableExtra::kable()
```
