-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_daisie_2type_ml.R
134 lines (124 loc) · 3.21 KB
/
run_daisie_2type_ml.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#' Run 2 type DAISIE analysis
#'
#' @inheritParams default_params_doc
#'
#' @return Nothing. Writes [`DAISIE`] analysis results to a `.rds` file. This
#' file is stored in `file_path`. The directory in of `file_path` is created
#' if it doesn't exist.
#' @export
#'
#' @examples
#' \dontrun{
#' data(Galapagos_datalist, package = "DAISIE")
#' run_daisie_2type_ml(
#' daisie_data = Galapagos_datalist,
#' data_name = "Galapagos_datalist",
#' model = "cr_dd",
#' array_index = 1,
#' cond = 1
#' )
#' }
#' @author Pedro Santos Neves, Joshua W. Lambert, Luis Valente
run_daisie_2type_ml <- function(daisie_data,
data_name,
model,
array_index,
cond,
methode = "lsodes",
optimmethod = "subplex",
results_dir = NULL,
low_rates = FALSE,
rep_index = "NULL",
res = 100,
prop_type2_pool,
test = FALSE) {
if (test) {
seed <- array_index
} else {
seed <- as.integer(Sys.time()) %% 10000L * array_index
}
set.seed(
seed,
kind = "Mersenne-Twister",
normal.kind = "Inversion",
sample.kind = "Rejection"
)
print_metadata(
data_name = data_name,
model = model,
array_index = array_index,
seed = seed,
methode = methode,
optimmethod = optimmethod
)
output_folder_path <- create_output_folder(
data_name = data_name,
results_dir = results_dir
)
testit::assert(is.numeric(array_index) && is.finite(array_index))
testit::assert(is.numeric(cond) && is.finite(cond))
model_arguments <- setup_2type_model(
model = model,
prop_type2_pool = prop_type2_pool,
low_rates = low_rates
)
initparsopt <- model_arguments$initparsopt
idparsnoshift <- model_arguments$idparsnoshift
idparsopt <- model_arguments$idparsopt
parsfix <- model_arguments$parsfix
idparsfix <- model_arguments$idparsfix
ddmodel <- model_arguments$ddmodel
cs_version <- model_arguments$cs_version
##### ML Optimization ####
lik_res <- DAISIE::DAISIE_ML(
datalist = daisie_data,
initparsopt = initparsopt,
idparsnoshift = idparsnoshift,
idparsopt = idparsopt,
parsfix = parsfix,
idparsfix = idparsfix,
res = res,
ddmodel = ddmodel,
cond = cond,
methode = methode,
optimmethod = optimmethod,
CS_version = cs_version
)
bic <- calc_bic(results = lik_res, daisie_data = daisie_data)
lik_res <- cbind(lik_res, bic)
if (identical(rep_index, "NULL")) {
output_path <- file.path(
output_folder_path,
paste0(
data_name,
"_",
model,
"_",
array_index,
".rds"
)
)
} else {
output_path <- file.path(
output_folder_path,
paste0(
data_name,
"_",
model,
"_",
array_index,
"_",
rep_index,
".rds"
)
)
}
if (is.na(output_path)) {
return(lik_res)
} else {
saveRDS(
lik_res,
file = output_path
)
}
}