###### examples in Vignette ######
library(owea)


## For CrossOver Dropout ##
# example 1 #
# p = 4, t = 4, n = 16, drop mechanism = (0,0,0.5,0.5)
# opt = 0, D-optimal; opt = 1, A-optimal.
set.seed(232)

# D-optimal Design
example1 <- design('dropout', n = 16, opt = 0, t = 4, p = 4,
                   drop = c(0, 0, 0.5, 0.5), max_iter = 40)

summary(example1) # printing output
## D-optimal designs for dropout model with 4 treatments 4 periods dropout mechanism 0, 0, 0.5, 0.5 :
## $exact_design
##               Repetitions
##  [1,] 1 2 3 4           1
##  [2,] 3 2 4 4           1
##  [3,] 2 4 1 3           2
##  [4,] 3 1 4 2           2
##  [5,] 4 3 2 1           1
##  [6,] 3 4 2 1           1
##  [7,] 2 1 3 3           1
##  [8,] 4 2 1 1           1
##  [9,] 4 1 2 2           1
## [10,] 1 3 2 2           1
## [11,] 1 2 3 4           1
## [12,] 1 4 3 3           1
## [13,] 2 3 4 4           1
## [14,] 4 3 1 1           1
## 
## $approximate_design
##                   Weights
##  [1,] 1 2 3 4 0.136574074
##  [2,] 2 3 4 4 0.040151065
##  [3,] 3 2 4 4 0.047214527
##  [4,] 2 4 1 3 0.117909521
##  [5,] 3 1 4 2 0.112831469
##  [6,] 4 3 2 1 0.067786510
##  [7,] 1 3 4 2 0.023742605
##  [8,] 3 4 2 1 0.068787564
##  [9,] 2 1 3 3 0.049790692
## [10,] 4 2 1 1 0.043682796
## [11,] 4 1 2 2 0.053654742
## [12,] 1 4 3 3 0.026048086
## [13,] 4 3 1 1 0.058548636
## [14,] 1 3 2 2 0.059771184
## [15,] 4 1 3 3 0.007662763
## [16,] 4 2 1 3 0.018664553
## [17,] 3 1 4 4 0.009971946
## [18,] 2 4 3 3 0.026060335
## [19,] 3 4 1 1 0.011194494
## [20,] 2 1 4 4 0.016088388
## [21,] 1 2 3 3 0.003864050
## 
## $computing_time
##    user  system elapsed 
##   2.592   0.071   2.731
eff(example1) # for efficiency
## $Optimal_Criterion
## [1] "D-optimal"
## 
## $efficiency
## [1] 0.9926396
design_compare <- cbind(t(matrix(c(2,4,3,3,1,4,2,2,2,3,1,1,3,4,1,1,3,1,
                                   2,2,4,1,3,3,3,2,4,4,2,1,4,4,1,2,3,4,
                                   1,2,3,4,1,3,4,2,2,4,1,3,4,3,2,1,4,3,
                                   2,1,4,2,1,3,3,1,4,2), ncol=16)),1)


eff(example1, ex = design_compare) # relative efficiency
## $Optimal_Criterion
## [1] "D-optimal"
## 
## $efficiency
## [1] 0.9944724
effLB(example1) # for lower bound efficiency
## $efficiency.self
## [1] 0.82123
## 
## $optimal.value
## [1] 0.002452183
# A-optimal Design
example1 <- design('dropout', n = 16, opt = 1, t = 4, p = 4,
                   drop = c(0, 0, 0.5, 0.5), max_iter = 40)

summary(example1) # printing output
## A-optimal designs for dropout model with 4 treatments 4 periods dropout mechanism 0, 0, 0.5, 0.5 :
## $exact_design
##               Repetitions
##  [1,] 3 2 4 1           2
##  [2,] 2 1 3 3           1
##  [3,] 4 3 2 1           1
##  [4,] 4 3 1 2           1
##  [5,] 4 2 1 1           1
##  [6,] 2 4 1 3           1
##  [7,] 1 3 4 4           1
##  [8,] 1 4 2 3           1
##  [9,] 1 2 3 4           1
## [10,] 1 4 3 3           1
## [11,] 2 3 1 4           1
## [12,] 2 3 4 4           1
## [13,] 3 1 2 2           1
## [14,] 3 4 2 2           1
## [15,] 4 1 3 2           1
## 
## $approximate_design
##                   Weights
##  [1,] 2 3 4 4 0.070351470
##  [2,] 1 4 2 2 0.060094357
##  [3,] 3 2 4 1 0.101942688
##  [4,] 1 2 3 4 0.083298709
##  [5,] 2 1 3 3 0.068869430
##  [6,] 3 1 2 2 0.039701394
##  [7,] 4 3 2 1 0.034631365
##  [8,] 3 4 1 1 0.058518624
##  [9,] 4 3 1 2 0.083298681
## [10,] 4 1 3 3 0.011832861
## [11,] 4 2 1 1 0.053331549
## [12,] 2 4 1 3 0.035570525
## [13,] 3 1 4 4 0.006119151
## [14,] 1 3 4 4 0.036955317
## [15,] 1 4 2 3 0.066372204
## [16,] 2 1 4 3 0.034631342
## [17,] 4 1 3 2 0.053275355
## [18,] 3 1 2 4 0.035570495
## [19,] 2 4 3 3 0.029444268
## [20,] 3 2 1 4 0.008147648
## [21,] 2 3 1 1 0.001575751
## [22,] 4 3 2 2 0.013630187
## [23,] 2 3 1 4 0.009557239
## [24,] 1 2 3 3 0.003279389
## 
## $computing_time
##    user  system elapsed 
##   2.651   0.061   2.787
eff(example1) # for efficiency
## $Optimal_Criterion
## [1] "A-optima"
## 
## $efficiency
## [1] 0.9934793
eff(example1, ex = design_compare) # relative efficiency
## $Optimal_Criterion
## [1] "A-optima"
## 
## $efficiency
## [1] 0.9942615
effLB(example1) # for lower bound efficiency
## $optimal
## [1] "A-optima"
## 
## $lower.bound
## [1] 0.9427708
## 
## $optimal.value
## [1] 0.2548506
# Example 2 #
# t = 4, p = 4, n = 19, dropout mechanism = (0,0,0.5,0.5)
# opt = 0, D-optimal; opt = 1, A-optimal.

set.seed(232)

# D-optimal Design
example2 <- design('dropout', n = 16, opt = 0, t = 4, p = 4,
                   drop = c(0, 0, 0.5, 0.5), max_iter = 40)

summary(example2) # printing output
## D-optimal designs for dropout model with 4 treatments 4 periods dropout mechanism 0, 0, 0.5, 0.5 :
## $exact_design
##               Repetitions
##  [1,] 1 2 3 4           1
##  [2,] 3 2 4 4           1
##  [3,] 2 4 1 3           2
##  [4,] 3 1 4 2           2
##  [5,] 4 3 2 1           1
##  [6,] 3 4 2 1           1
##  [7,] 2 1 3 3           1
##  [8,] 4 2 1 1           1
##  [9,] 4 1 2 2           1
## [10,] 1 3 2 2           1
## [11,] 1 2 3 4           1
## [12,] 1 4 3 3           1
## [13,] 2 3 4 4           1
## [14,] 4 3 1 1           1
## 
## $approximate_design
##                   Weights
##  [1,] 1 2 3 4 0.136574074
##  [2,] 2 3 4 4 0.040151065
##  [3,] 3 2 4 4 0.047214527
##  [4,] 2 4 1 3 0.117909521
##  [5,] 3 1 4 2 0.112831469
##  [6,] 4 3 2 1 0.067786510
##  [7,] 1 3 4 2 0.023742605
##  [8,] 3 4 2 1 0.068787564
##  [9,] 2 1 3 3 0.049790692
## [10,] 4 2 1 1 0.043682796
## [11,] 4 1 2 2 0.053654742
## [12,] 1 4 3 3 0.026048086
## [13,] 4 3 1 1 0.058548636
## [14,] 1 3 2 2 0.059771184
## [15,] 4 1 3 3 0.007662763
## [16,] 4 2 1 3 0.018664553
## [17,] 3 1 4 4 0.009971946
## [18,] 2 4 3 3 0.026060335
## [19,] 3 4 1 1 0.011194494
## [20,] 2 1 4 4 0.016088388
## [21,] 1 2 3 3 0.003864050
## 
## $computing_time
##    user  system elapsed 
##   2.358   0.039   2.427
eff(example2) # for efficiency
## $Optimal_Criterion
## [1] "D-optimal"
## 
## $efficiency
## [1] 0.9926396
effLB(example2) # for lower bound efficiency
## $efficiency.self
## [1] 0.82123
## 
## $optimal.value
## [1] 0.002452183
# A-optimal Design
example2 <- design('dropout', n = 16, opt = 1, t = 4, p = 4,
                   drop = c(0, 0, 0.5, 0.5), max_iter = 40)

summary(example2) # printing output
## A-optimal designs for dropout model with 4 treatments 4 periods dropout mechanism 0, 0, 0.5, 0.5 :
## $exact_design
##               Repetitions
##  [1,] 3 2 4 1           2
##  [2,] 2 1 3 3           1
##  [3,] 4 3 2 1           1
##  [4,] 4 3 1 2           1
##  [5,] 4 2 1 1           1
##  [6,] 2 4 1 3           1
##  [7,] 1 3 4 4           1
##  [8,] 1 4 2 3           1
##  [9,] 1 2 3 4           1
## [10,] 1 4 3 3           1
## [11,] 2 3 1 4           1
## [12,] 2 3 4 4           1
## [13,] 3 1 2 2           1
## [14,] 3 4 2 2           1
## [15,] 4 1 3 2           1
## 
## $approximate_design
##                   Weights
##  [1,] 2 3 4 4 0.070351470
##  [2,] 1 4 2 2 0.060094357
##  [3,] 3 2 4 1 0.101942688
##  [4,] 1 2 3 4 0.083298709
##  [5,] 2 1 3 3 0.068869430
##  [6,] 3 1 2 2 0.039701394
##  [7,] 4 3 2 1 0.034631365
##  [8,] 3 4 1 1 0.058518624
##  [9,] 4 3 1 2 0.083298681
## [10,] 4 1 3 3 0.011832861
## [11,] 4 2 1 1 0.053331549
## [12,] 2 4 1 3 0.035570525
## [13,] 3 1 4 4 0.006119151
## [14,] 1 3 4 4 0.036955317
## [15,] 1 4 2 3 0.066372204
## [16,] 2 1 4 3 0.034631342
## [17,] 4 1 3 2 0.053275355
## [18,] 3 1 2 4 0.035570495
## [19,] 2 4 3 3 0.029444268
## [20,] 3 2 1 4 0.008147648
## [21,] 2 3 1 1 0.001575751
## [22,] 4 3 2 2 0.013630187
## [23,] 2 3 1 4 0.009557239
## [24,] 1 2 3 3 0.003279389
## 
## $computing_time
##    user  system elapsed 
##   2.642   0.057   2.771
eff(example2) # for efficiency
## $Optimal_Criterion
## [1] "A-optima"
## 
## $efficiency
## [1] 0.9934793
effLB(example2) # for lower bound efficiency
## $optimal
## [1] "A-optima"
## 
## $lower.bound
## [1] 0.9427708
## 
## $optimal.value
## [1] 0.2548506