---
title: "Testing the CRE Package"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Testing the CRE package}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

We encourage all developers to test the package in different conditions. 
Testing the package is the easiest way to get familiar with the package and its 
functionalities.  

# Getting the code


To test the package, please install the package on your
system (R (>= 3.5.0)). You can install the package by following one of these 
approaches:

- Directly from GitHub
- CRAN (not recommended)
- Source 
- Forked Repository (recommended)

## Installing the package directly from Github

In this project, we follow [A successful Git Branching Model](https://nvie.com/posts/a-successful-git-branching-model/).
As a result, the `develop` branch is the most updated branch for developers. Use
`devtools::install_github` to install the package. If you do not specify the `ref`,
it will install the master (or main) branch.  

```R
library(devtools)
try(detach("package:CRE", unload = TRUE), silent = TRUE) # if already you have the package, detach and unload it, to have a new install. 
install_github("NSAPH-Software/CRE", ref="develop")
library(CRE)
```

Try `?CRE`. It should open the package description page under the help tab 
(assuming you are using RStudio).

## Installing the package from CRAN

Installing the package from CRAN for developing purposes is not recommended. 
Because most probably, the version on CRAN is not the latest version. 

[Complete this section after submitting the package to CRAN]

## Installing the package from the source

In order to install the package from the source, you need to download the source 
code into your computer and install it from the source. Here are the steps:

- Go to package [Github repository](https://github.com/NSAPH-Software/CRE) and from the 
drop-down menu change the branch to `develop`. Then click on the `Code` tab and 
then click on the `Download Zip` tab.

- Open one of the files using RStudio, then change the project directory to the 
project directory (`Session > Set Working Directory > To Project Directory`). 

- Load `devtools` library and then load CRE.

```R
library(devtools)
load_all()
?CRE
```

## Forking the package

Forking the package under your Github account is the best option if you are 
planning on installing, testing, modifying, and contributing to the 
project. Go to package [Github repository](https://github.com/NSAPH-Software/CRE) and 
click on the `Fork` button at the top right corner. After forking the package, 
Open your terminal (or Gitbash for Windows, Anaconda prompt, ...) and run the 
following command  (brackets are not included):

```S
git clone git@github.com:[your user name]/CRE.git
```

Now, you can modify the codebase and track your modification. Navigate to the 
package folder and Install the package following the 
**Installing the package from source** steps. It is a good idea 
to create a new branch to work on the codebase. Read 
[A successful Git Branching Model](https://nvie.com/posts/a-successful-git-branching-model/) for 
branching convention.

# Testing the Package

Run the following command to test the package. 

```{r, warning=FALSE, eval=FALSE}
library(CRE)

# Generate sample data
set.seed(1358)
dataset <- generate_cre_dataset(n = 1000,
                                rho = 0,
                                n_rules = 2,
                                p = 10,
                                effect_size = 2,
                                binary_covariates = TRUE,
                                binary_outcome = FALSE,
                                confounding = "no")
y <- dataset[["y"]]
z <- dataset[["z"]]
X <- dataset[["X"]]

method_params <- list(ratio_dis = 0.5,
                      ite_method = "aipw",
                      learner_ps = "SL.xgboost",
                      learner_y = "SL.xgboost",
                      offset = NULL)

hyper_params <- list(intervention_vars = NULL,
                     ntrees = 20,
                     node_size = 20,
                     max_rules = 50,
                     max_depth = 3,
                     t_decay = 0.025,
                     t_ext = 0.01,
                     t_corr = 1,
                     t_pvalue = 0.05,
                     stability_selection = "vanilla",
                     cutoff = 0.6,
                     pfer = 1,
                     B = 10,
                     subsample = 0.5)

# linreg CATE estimation with aipw ITE estimation
cre_results <- cre(y, z, X, method_params, hyper_params)
summary(cre_results)
plot(cre_results)
ite_pred <- predict(cre_results, X)
```