---
title: "ldamatch demos"
author: "Kyle Gorman & Géza Kiss"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{ldamatch demos}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Univariate case...

```{r}
library(ldamatch)
set.seed(257)

default_method = "heuristic4"

SIZE <- 15
condition <- as.factor(c(rep("control", 2 * SIZE), rep("treatment", SIZE)))
covariate1 <- c(rnorm(2 * SIZE), rnorm(SIZE, 1, 2))
```

# Univariate case (with random search)...

```{r}
is.in <- match_groups(condition, covariate1, t_halt, method = "random")
print(table(condition, is.in))
```

# Univariate case (with default search method)...

```{r}
is.in <- match_groups(condition, covariate1, t_halt, method = default_method)
print(table(condition, is.in))
```

# Multivariate case...

```{r}
covariate2 <- c(rnorm(2 * SIZE), rnorm(SIZE, 1, 2))
covariates <- cbind(covariate1, covariate2)
```

# Multivariate case (with default search method)...

```{r}
is.in <- match_groups(condition, covariates, t_halt, method = default_method)
print(table(condition, is.in))
```

# Multivariate case (with random search)...

```{r}
is.in <- match_groups(condition, covariates, t_halt, method = "random")
print(table(condition, is.in))
```

# Multivariate case (with special proportions and Wilcox test)...

```{r}
my.props <- prop.table(c(control = 4, treatment = 3))
is.in <- match_groups(condition, covariates, U_halt, props = my.props)
print(table(condition, is.in))
```

# Multivariate case (with Wilks test)...

```{r}
is.in <- match_groups(condition, covariates, wilks_halt)
print(table(condition, is.in))
```

# Multivariate case (with Wilks test and random search)...

```{r}
is.in <- match_groups(condition, covariates, wilks_halt, method = "random")
print(table(condition, is.in))
```

# Multivariate case (with Anderson-Darling test and default search method)...

```{r}
is.in <- match_groups(condition, covariates, ad_halt, method = default_method, prefer_test = TRUE)
print(table(condition, is.in))
```

# Multivariate case (with t-test and Anderson-Darling test simultaneously)...

```{r}
t_ad_halt <- create_halting_test(c(t_halt, ad_halt))
threshes <- c(.2, .02)
is.in <- match_groups(condition, covariates, t_ad_halt, threshes)
print(table(condition, is.in))
```

# Univariate case (with exhaustive search)...

```{r}
estimate_exhaustive(min_preserved = 42, condition, cases_per_second = 100)
foreach::registerDoSEQ()
is.ins <- match_groups(condition, covariate1, t_halt, method = "exhaustive", all_results = TRUE)
print(table(condition, is.ins[[1]]))
print(length(is.ins))

# (Confirm exhaustive search by applying default search method to it.)
is.in <- match_groups(condition[is.ins[[1]]], covariate1[is.ins[[1]]], t_halt,
                      method = default_method)
print(table(condition[is.ins[[1]]], is.in))
```


# Univariate case for more than two groups...

```{r}
set.seed(257)

SIZE <- 15
condition <- as.factor(c(rep("group1", SIZE), rep("group2", SIZE), rep("group3", SIZE)))
covariate1 <- c(rnorm(SIZE, 0, 1), rnorm(SIZE, 0, 2), rnorm(SIZE, 1, 2))
covariate2 <- c(rnorm(SIZE, 0, 1), rnorm(SIZE, 0, 2), rnorm(SIZE, 1, 2))
covariates <- cbind(covariate1, covariate2)
```

# Univariate case for more than two groups (with default search method)...

```{r}
is.in <- match_groups(condition, covariates, t_ad_halt, method = default_method)
print(table(condition, is.in))
```

