Univariate case…

library(ldamatch)
set.seed(257)

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)…

is.in <- match_groups(condition, covariate1, t_halt, method = "random")
## Search method:  random 
## Initial group sizes:  control: 30    treatment: 15 
## Starting random search.
## Found matching: control: 30; treatment: 13 (total: 43)
## Found matching: control: 30; treatment: 13 (total: 43)
## Found matching: control: 30; treatment: 13 (total: 43)
## Found matching: control: 30; treatment: 13 (total: 43)
## Finished random search in 10.638 seconds (wall click time passed:10.935).
## Eventual group sizes: control: 30    treatment: 13 
## Removed subjects:     control: 0 treatment: 2 
## The p-value before matching: 0.06059131 
## The p-values after matching: 0.220897103271512
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     2   13

Univariate case (with heuristic2 search)…

is.in <- match_groups(condition, covariate1, t_halt, method = "heuristic2")
## Search method:  heuristic2 
## Initial group sizes:  control: 30    treatment: 15 
## Starting heuristic2 search.
## Number of subjects: 45
## Number of subjects: 44
## Number of subjects: 43
## Finished heuristic2 search in 0.134 seconds (wall click time passed:0.136).
## Eventual group sizes: control: 30    treatment: 13 
## Removed subjects:     control: 0 treatment: 2 
## The p-value before matching: 0.06059131 
## The p-values after matching: 0.22165738589329
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     2   13

Multivariate case…

covariate2 <- c(rnorm(2 * SIZE), rnorm(SIZE, 1, 2))
covariates <- cbind(covariate1, covariate2)

Multivariate case (with heuristic2 search)…

is.in <- match_groups(condition, covariates, t_halt, method = "heuristic2")
## Search method:  heuristic2 
## Initial group sizes:  control: 30    treatment: 15 
## Starting heuristic2 search.
## Number of subjects: 45
## Number of subjects: 44
## Number of subjects: 43
## Finished heuristic2 search in 0.229 seconds (wall click time passed:0.233).
## Eventual group sizes: control: 30    treatment: 13 
## Removed subjects:     control: 0 treatment: 2 
## The p-value before matching: 0.06059131 
## The p-values after matching: 0.22165738589329
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     2   13

Multivariate case (with random search)…

is.in <- match_groups(condition, covariates, t_halt, method = "random")
## Search method:  random 
## Initial group sizes:  control: 30    treatment: 15 
## Starting random search.
## Found matching: control: 27; treatment: 12 (total: 39)
## Found matching: control: 29; treatment: 13 (total: 42)
## Found matching: control: 29; treatment: 13 (total: 42)
## Found matching: control: 30; treatment: 12 (total: 42)
## Found matching: control: 30; treatment: 13 (total: 43)
## Found matching: control: 30; treatment: 13 (total: 43)
## Found matching: control: 30; treatment: 13 (total: 43)
## Finished random search in 10.269 seconds (wall click time passed:10.559).
## Eventual group sizes: control: 30    treatment: 13 
## Removed subjects:     control: 0 treatment: 2 
## The p-value before matching: 0.06059131 
## The p-values after matching: 0.22165738589329
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     2   13

Multivariate case (with special proportions and Wilcox test)…

my.props <- prop.table(c(control = 4, treatment = 3))
is.in <- match_groups(condition, covariates, U_halt, props = my.props)
## Search method:  heuristic2 
## Initial group sizes:  control: 30    treatment: 15 
## Starting heuristic2 search.
## Number of subjects: 45
## Number of subjects: 44
## Finished heuristic2 search in 0.105 seconds (wall click time passed:0.106).
## Eventual group sizes: control: 30    treatment: 14 
## Removed subjects:     control: 0 treatment: 1 
## The p-value before matching: 0.160699 
## The p-values after matching: 0.265692168887322
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     1   14

Multivariate case (with Wilks test)…

is.in <- match_groups(condition, covariates, wilks_halt)
## Search method:  heuristic2 
## Initial group sizes:  control: 30    treatment: 15 
## Starting heuristic2 search.
## Number of subjects: 45
## Number of subjects: 44
## Number of subjects: 43
## Number of subjects: 42
## Finished heuristic2 search in 0.25 seconds (wall click time passed:0.252).
## Eventual group sizes: control: 30    treatment: 12 
## Removed subjects:     control: 0 treatment: 3 
## The p-value before matching: 0.03195062 
## The p-values after matching: 0.382310742100903
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     3   12

Multivariate case (with Wilks test and random search)…

is.in <- match_groups(condition, covariates, wilks_halt, method = "random")
## Search method:  random 
## Initial group sizes:  control: 30    treatment: 15 
## Starting random search.
## Found matching: control: 30; treatment: 12 (total: 42)
## Found matching: control: 29; treatment: 13 (total: 42)
## Found matching: control: 30; treatment: 12 (total: 42)
## Found matching: control: 30; treatment: 12 (total: 42)
## Finished random search in 18.665 seconds (wall click time passed:19.027).
## Eventual group sizes: control: 30    treatment: 12 
## Removed subjects:     control: 0 treatment: 3 
## The p-value before matching: 0.03195062 
## The p-values after matching: 0.323684521786494
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     3   12

Multivariate case (with Anderson-Darling test and heuristic2 search)…

is.in <- match_groups(condition, covariates, ad_halt, method = "heuristic2", prefer_test = TRUE)
## Search method:  heuristic2 
## Initial group sizes:  control: 30    treatment: 15 
## Starting heuristic2 search.
## Number of subjects: 45
## Number of subjects: 44
## Number of subjects: 43
## Number of subjects: 42
## Finished heuristic2 search in 10.377 seconds (wall click time passed:10.732).
## Eventual group sizes: control: 30    treatment: 12 
## Removed subjects:     control: 0 treatment: 3 
## The p-value before matching: 0.057657 
## The p-values after matching: 0.33079
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     3   12

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

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)
## Search method:  heuristic2 
## Initial group sizes:  control: 30    treatment: 15 
## Starting heuristic2 search.
## Number of subjects: 45
## Number of subjects: 44
## Number of subjects: 43
## Finished heuristic2 search in 7.188 seconds (wall click time passed:7.472).
## Eventual group sizes: control: 30    treatment: 13 
## Removed subjects:     control: 0 treatment: 2 
## The p-value before matching: 0.057657 
## The p-values after matching: 0.21597
print(table(condition, is.in))
##            is.in
## condition   FALSE TRUE
##   control       0   30
##   treatment     2   13

Univariate case (with exhaustive search)…

estimate_exhaustive(min_preserved = 42, condition, cases_per_second = 100)
## If 44 of 45 kept: at most 45 cases. If 100 cases per second evaluated: 0.4 seconds.
## If 43 of 45 kept: at most 1035 cases. If 100 cases per second evaluated: 10.3 seconds.
## If 42 of 45 kept: at most 15225 cases. If 100 cases per second evaluated: 2.5 minutes.
## [1] 15225
foreach::registerDoSEQ()
is.ins <- match_groups(condition, covariate1, t_halt, method = "exhaustive", all_results = TRUE)
## Search method:  exhaustive 
## Initial group sizes:  control: 30    treatment: 15 
## Starting exhaustive search.
## Created 2 group size configurations each with a total size of 44 
## control: 29 treatment: 15 divergence: 0.000128651338541343 
## Size of Cartesian product: 30 
## 
1 to 30
Number of cases processed per second: 4.803074 (cpu time) or 4.656216 (wall clock time).
## control: 30 treatment: 14 divergence: 0.000520578560755861 
## Size of Cartesian product: 15 
## 
1 to 15
Number of cases processed per second: 8.017103 (cpu time) or 7.869885 (wall clock time).
## Created 4 group size configurations each with a total size of 43 
## control: 29 treatment: 14 divergence: 0.000135741532290407 
## Size of Cartesian product: 450 
## 
1 to 450
Number of cases processed per second: 229.709 (cpu time) or 219.2982 (wall clock time).
## control: 29 treatment: 14 divergence: 0.000135741532290407 
## Size of Cartesian product: 450 
## 
1 to 450
Number of cases processed per second: 223.2143 (cpu time) or 217.7068 (wall clock time).
## control: 28 treatment: 15 divergence: 0.000536783341068091 
## Size of Cartesian product: 435 
## 
1 to 435
Number of cases processed per second: 67.42095 (cpu time) or 64.20664 (wall clock time).
## control: 30 treatment: 13 divergence: 0.0021993283249259 
## Size of Cartesian product: 105 
## 
1 to 105
Number of cases processed per second: 68.53786 (cpu time) or 61.04651 (wall clock time).
## Finished exhaustive search in 20.114 seconds (wall click time passed:21.004).
## Eventual group sizes: control: 30    treatment: 13 
## Removed subjects:     control: 0 treatment: 2 
## The p-value before matching: 0.06059131 
## The p-values after matching: 0.22165738589329
print(table(condition, is.ins[[1]]))
##            
## condition   FALSE TRUE
##   control       0   30
##   treatment     2   13
print(length(is.ins))
## [1] 1
# (Confirm exhaustive search by applying heuristic2 search to it.)
is.in <- match_groups(condition[is.ins[[1]]], covariate1[is.ins[[1]]], t_halt,
                      method = "heuristic2")
## Search method:  heuristic2 
## Groups are already matched.
print(table(condition[is.ins[[1]]], is.in))
##            is.in
##             TRUE
##   control     30
##   treatment   13

Univariate case for more than two groups…

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 heuristic2 search)…

is.in <- match_groups(condition, covariates, t_ad_halt, method = "heuristic2")
## Search method:  heuristic2 
## Initial group sizes:  group1: 15 group2: 15  group3: 15 
## Starting heuristic2 search.
## Number of subjects: 45
## Number of subjects: 44
## Number of subjects: 43
## Number of subjects: 42
## Number of subjects: 41
## Number of subjects: 40
## Number of subjects: 39
## Number of subjects: 38
## Number of subjects: 37
## Number of subjects: 36
## Finished heuristic2 search in 32.78 seconds (wall click time passed:34.501).
## Eventual group sizes: group1: 15 group2: 12  group3: 9 
## Removed subjects:     group1: 0  group2: 3   group3: 6 
## The p-value before matching: 0.012664 
## The p-values after matching: 0.29084
print(table(condition, is.in))
##          is.in
## condition FALSE TRUE
##    group1     0   15
##    group2     3   12
##    group3     6    9