-
Notifications
You must be signed in to change notification settings - Fork 20
/
blockexp.R
225 lines (189 loc) · 8.29 KB
/
blockexp.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#' Summarize your experiment for optimization routines
#'
#' Create some output to the screen and a text file that summarizes the initial design and the design space
#' you will use to optimize.
#'
#' @inheritParams RS_opt
#' @inheritParams evaluate.fim
#' @inheritParams Doptim
#' @inheritParams create.poped.database
#' @inheritParams Dtrace
#' @param fn The file handle to write to.
#' @param e_flag Should output be with uncertainty around parameters?
#'
#'
#' @family Helper
#' @example tests/testthat/examples_fcn_doc/warfarin_optimize.R
#' @example tests/testthat/examples_fcn_doc/examples_blockexp.R
#' @export
#' @keywords internal
# @importFrom MASS write.matrix
## Function translated automatically using 'matlab.to.r()'
## Author: Andrew Hooker
blockexp <- function(fn,poped.db,e_flag=FALSE,
opt_xt=poped.db$settings$optsw[2],opt_a=poped.db$settings$optsw[4],opt_x=poped.db$settings$optsw[4],
opt_samps=poped.db$settings$optsw[1],opt_inds=poped.db$settings$optsw[5]){
fprintf(fn,'==============================================================================\n')
fprintf(fn,'Model description : %s \n',poped.db$settings$modtit)
fprintf(fn,'\n')
fprintf(fn,'Model Sizes : \n')
fprintf(fn,'Number of individual model parameters g[j] : Ng = %g\n',poped.db$parameters$ng)
fprintf(fn,'Number of population model fixed parameters bpop[j] : Nbpop = %g\n',poped.db$parameters$nbpop)
fprintf(fn,'Number of population model random effects parameters b[j] : Nb = %g\n',poped.db$parameters$NumRanEff)
fprintf(fn,'\n')
print_params(poped.db$parameters$bpop,"bpop",fn=fn,poped.db=poped.db,
head_txt="Typical Population Parameters",e_flag=e_flag)
fprintf(fn,'\n')
if((poped.db$parameters$NumRanEff!=0)){
fprintf(fn,"Between Subject Variability matrix D (variance units) \n")
d=getfulld(poped.db$parameters$d[,2,drop=F],poped.db$parameters$covd)
MASS::write.matrix(d,file=fn)
fprintf(fn,'\n')
print_params(poped.db$parameters$d,"D",fn=fn,poped.db=poped.db,param_sqrt=TRUE, matrix_elements=T,
head_txt="Diagonal Elements of D",e_flag=e_flag)
fprintf(fn,'\n')
}
docc_full = getfulld(poped.db$parameters$docc[,2,drop=F],poped.db$parameters$covdocc)
fprintf(fn,'Residual Unexplained Variability matrix SIGMA (variance units) : \n')
sigma = poped.db$parameters$sigma
MASS::write.matrix(sigma,file=fn)
fprintf(fn,'\n')
#sigma_d = diag_matlab(poped.db$parameters$sigma)
sigma_d <- cbind(c(0,0),diag_matlab(poped.db$parameters$sigma),c(1,1))
print_params(sigma_d,"SIGMA",fn=fn,poped.db=poped.db,param_sqrt=TRUE, matrix_elements=T,
head_txt="Diagonal Elements of SIGMA",e_flag=e_flag)
fprintf(fn,'\n')
fprintf(fn,'==============================================================================\n')
fprintf(fn,'Experiment description (design and design space)\n')
fprintf(fn,'\n')
tmp_txt <- "Number of individuals"
if(opt_inds) tmp_txt <- paste(tmp_txt,'(min, max)',sep=" ")
tmp_txt <- paste(tmp_txt,': %g',sep="")
if(opt_inds) tmp_txt <- paste(tmp_txt,'(%g, %g)',sep=" ")
tmp_txt <- paste(tmp_txt,'\n',sep="")
fprintf(fn,tmp_txt,sum(poped.db$design$groupsize),poped.db$design_space$mintotgroupsize,poped.db$design_space$maxtotgroupsize)
fprintf(fn,'Number of groups (individuals with same design): %g\n',poped.db$design$m)
tmp_txt <- "Number of individuals per group"
if(opt_inds) tmp_txt <- paste(tmp_txt,'(min, max)',sep=" ")
tmp_txt <- paste(tmp_txt,':\n',sep="")
fprintf(fn,tmp_txt)
fprintf(fn," ")
tmp_txt <- ' Group %g: %g'
if(opt_inds) tmp_txt <- paste(tmp_txt,'(%g, %g)',sep=" ")
tmp_txt <- paste(tmp_txt,'\n',sep="")
fprintf(fn,tmp_txt,1:poped.db$design$m,poped.db$design$groupsize,poped.db$design_space$maxgroupsize, poped.db$design_space$maxgroupsize)
tmp_txt <- "Number of samples per group"
if(opt_samps) tmp_txt <- paste(tmp_txt,'(min, max)',sep=" ")
tmp_txt <- paste(tmp_txt,':\n',sep="")
fprintf(fn,tmp_txt)
fprintf(fn," ")
tmp_txt <- ' Group %g: %g'
if(opt_samps) tmp_txt <- paste(tmp_txt,'(%g, %g)',sep=" ")
tmp_txt <- paste(tmp_txt,'\n',sep="")
fprintf(fn,tmp_txt,1:poped.db$design$m,poped.db$design$ni,poped.db$design$minni, poped.db$design$maxni)
fprintf(fn,'Number of discrete experimental variables: %g\n',size(poped.db$design$x,2))
fprintf(fn,'Number of model covariates: %g\n',size(poped.db$design$a,2))
fprintf(fn,'\n')
print_xt(poped.db$design$xt,poped.db$design$ni,poped.db$design$model_switch,fn,
head_txt="Initial Sampling Schedule\n")
fprintf(fn,'\n')
if(opt_xt){
print_xt(poped.db$design$xt,poped.db$design$ni,poped.db$design$model_switch,fn,
head_txt="Minimum allowed sampling values\n",xt_other=poped.db$design_space$minxt)
fprintf(fn,'\n')
print_xt(poped.db$design$xt,poped.db$design$ni,poped.db$design$model_switch,fn,
head_txt="Maximum allowed sampling values\n",xt_other=poped.db$design_space$maxxt)
fprintf(fn,'\n')
}
if((size(poped.db$design$x,2)!=0)){
tmp_txt <- "Discrete Variables"
if(opt_x) tmp_txt <- paste(tmp_txt,' (possible vales)',sep=" ")
tmp_txt <- paste(tmp_txt,':\n',sep="")
fprintf(fn,tmp_txt)
for(ct1 in 1:poped.db$design$m){
fprintf(fn,'Group %g: ', ct1)
for(ct2 in 1:size(poped.db$design$x,2)){
tmp_txt <- '%g'
if(opt_x) tmp_txt <- paste(tmp_txt,'(%s)',sep=" ")
if(ct2<size(poped.db$design$x,2)) tmp_txt <- paste(tmp_txt,' : ',sep="")
discrete_val = poped.db$design_space$discrete_x[[ct1,ct2]]
fprintf(fn,tmp_txt,poped.db$design$x[ct1,ct2],paste(discrete_val, collapse = ' '))
}
fprintf(fn,'\n')
}
fprintf(fn,'\n')
}
if((size(poped.db$design$a,2)!=0)){
tmp_txt <- "Covariates"
if(opt_a) tmp_txt <- paste(tmp_txt,' (min, max)',sep=" ")
tmp_txt <- paste(tmp_txt,':\n',sep="")
fprintf(fn,tmp_txt)
for(ct1 in 1:poped.db$design$m){
fprintf(fn,'Group %g: ', ct1)
for(ct2 in 1:size(poped.db$design$a,2)){
tmp_txt <- '%g'
if(opt_a) tmp_txt <- paste(tmp_txt,'(%g, %g)',sep=" ")
if(ct2<size(poped.db$design$a,2)) tmp_txt <- paste(tmp_txt,' : ',sep="")
fprintf(fn,tmp_txt,poped.db$design$a[ct1,ct2],poped.db$design_space$mina[ct1,ct2],poped.db$design_space$maxa[ct1,ct2])
}
fprintf(fn,'\n')
}
fprintf(fn,'\n')
}
return( )
}
print_params <- function (params,name_str, fn, poped.db, param_sqrt=FALSE,head_txt=NULL,matrix_elements=F,e_flag=FALSE) {
if(is.null(head_txt)) head_txt <- "Parameter Values"
uncer_txt <- ""
if(e_flag) uncer_txt <- " (Uncertainty Distribution)"
sqrt_txt <- ""
if(param_sqrt) sqrt_txt <- " [sqrt(param)]"
fprintf(fn,paste(head_txt,sqrt_txt,uncer_txt,":\n",sep=""))
for(ct in 1:size(params,1)){
par_val <- params[ct,2]
uncer_val <- params[ct,3]
cv_uncer <- sqrt(uncer_val)/par_val*100
cv_str <- ", %CV="
uncer_str <- ", Var="
par_val_sqrt <- ""
if(param_sqrt) par_val_sqrt =sqrt(par_val)
#if(param_sqrt) cv_uncer =cv_uncer/2
if((params[ct,1]==0)){
dist_str <- "Point value"
uncer_val <- ""
cv_uncer <- ""
cv_str <- ""
uncer_str <- ""
}
if((params[ct,1]==2)){
dist_str <- "Uniform"
cv_uncer <- ""
cv_str <- ""
uncer_str <- ", Max-Min"
}
if((params[ct,1]==4)){
dist_str <- "Log-Normal"
}
if(params[ct,1]==1){
dist_str <- "Normal"
}
if((params[ct,1]==3)){
dist_str <- "User Defined"
cv_uncer <- ""
cv_str <- ""
}
if((params[ct,1]==5)){
dist_str <- "Zero-Truncated Normal"
}
if(!is.character(uncer_val)) uncer_val <- sprintf("%5.4g",uncer_val)
if(!is.character(cv_uncer)) cv_uncer <- sprintf("%5.4g",cv_uncer)
if(!is.character(par_val_sqrt)) par_val_sqrt <- sprintf("[%5.4g] ",par_val_sqrt)
mat_str <- ""
if(matrix_elements) mat_str <- sprintf(",%g",ct)
if(e_flag){
fprintf(fn,'%s[%g%s]: %5.4g %s(%s%s%s%s%s)\n', name_str,ct,mat_str,par_val, par_val_sqrt,dist_str, uncer_str, uncer_val, cv_str, cv_uncer)
} else {
fprintf(fn,'%s[%g%s]: %5.4g %s\n', name_str,ct,mat_str,par_val, par_val_sqrt,dist_str, uncer_str, uncer_val, cv_str, cv_uncer)
}
}
}