To illustrate the use of CGNM here we illustrate how CGNM can be used to estimate two sets of the best fit parameters of the pharmacokinetics model when the drug is administered orally (known as flip-flop kinetics).
Here we have specified the upper and lower range of the initial guess.
CGNM_result=Cluster_Gauss_Newton_method(nonlinearFunction=model_function,targetVector = observation,
initial_lowerRange =rep(0.01,3),initial_upperRange = rep(100,3),lowerBound = rep(0,3), saveLog=TRUE, num_minimizersToFind = 500, ParameterNames = c("Ka","V1","CL"))
#> Ka, V1, CL will be transformed internally to impose boundaries. See CGNM_result$runSetting$ReparameterizationDef for exact transformation. Transformed parameters are denoted as x and untransformed parameters are denoted as theta.
#> checking if the nonlinearFunction can be evaluated at the initial_lowerRange
#> Evaluation Successful
#> checking if the nonlinearFunction can be evaluated at the initial_upperRange
#> Evaluation Successful
#> checking if the nonlinearFunction can be evaluated at the (initial_upperRange+initial_lowerRange)/2
#> Evaluation Successful
#> CGNM iteration should finish before: 2024-11-10 06:21:38.824653
#> Generating initial cluster. 496 out of 500 done
#> Generating initial cluster. 500 out of 500 done
#> Iteration:1 Median sum of squares residual=6.8163936223372
#> Rough estimation of remaining computation time: 0.9 min
#> CGNM iteration estimated to finish at: 2024-11-10 06:19:59.362087
#> Iteration:2 Median sum of squares residual=2.57549068418749
#> Iteration:3 Median sum of squares residual=0.927415820013554
#> Iteration:4 Median sum of squares residual=0.926981156722836
#> Iteration:5 Median sum of squares residual=0.884024173270177
#> Iteration:6 Median sum of squares residual=0.680403504134484
#> Iteration:7 Median sum of squares residual=0.195429904208436
#> Iteration:8 Median sum of squares residual=0.0104779640078736
#> Iteration:9 Median sum of squares residual=0.00735037852257622
#> Iteration:10 Median sum of squares residual=0.00734924487292652
#> Iteration:11 Median sum of squares residual=0.00734923500160858
#> CGNM iteration estimated to finish at: 2024-11-10 06:19:44.651595
#> Iteration:12 Median sum of squares residual=0.0073492341195234
#> Iteration:13 Median sum of squares residual=0.00734923409177934
#> Iteration:14 Median sum of squares residual=0.00734923408253059
#> Iteration:15 Median sum of squares residual=0.00734923408245078
#> Iteration:16 Median sum of squares residual=0.00734923408240189
#> Iteration:17 Median sum of squares residual=0.0073492340823864
#> Iteration:18 Median sum of squares residual=0.00734923408238403
#> Iteration:19 Median sum of squares residual=0.00734923408238379
#> Iteration:20 Median sum of squares residual=0.00734923408238202
#> Iteration:21 Median sum of squares residual=0.00734923408238102
#> CGNM iteration estimated to finish at: 2024-11-10 06:19:47.135682
#> Iteration:22 Median sum of squares residual=0.00734923408238099
#> Iteration:23 Median sum of squares residual=0.00734923408238093
#> Iteration:24 Median sum of squares residual=0.00734923408238091
#> Iteration:25 Median sum of squares residual=0.00734923408238087
#> CGNM computation time: 0.7 min
Ka | V1 | CL |
---|---|---|
0.9265057 | 19.07204 | 9.877326 |
0.9265056 | 19.07204 | 9.877326 |
0.5178958 | 10.66085 | 9.877323 |
0.9265055 | 19.07204 | 9.877325 |
0.9265057 | 19.07204 | 9.877326 |
0.5178956 | 10.66084 | 9.877326 |
CGNM: Minimum | 25 percentile | Median | 75 percentile | Maximum | |
---|---|---|---|---|---|
Ka | 0.5178362 | 0.5178956 | 0.5178957 | 0.9265056 | 0.9265657 |
V1 | 10.6584911 | 10.6608371 | 10.6608426 | 19.0720415 | 19.0730830 |
CL | 9.8765446 | 9.8773257 | 9.8773258 | 9.8773258 | 9.8779785 |
CGNM_bootstrap=Cluster_Gauss_Newton_Bootstrap_method(CGNM_result, nonlinearFunction=model_function)
#> checking if the nonlinearFunction can be evaluated at the initial_lowerRange
#> Evaluation Successful
#> checking if the nonlinearFunction can be evaluated at the initial_upperRange
#> Evaluation Successful
#> checking if the nonlinearFunction can be evaluated at the (initial_upperRange+initial_lowerRange)/2
#> Evaluation Successful
#> CGNM iteration should finish before: 2024-11-10 06:19:50.197896
#> Generating initial cluster. 200 out of 200 done
#> Iteration:1 Median sum of squares residual=0.0119214682054041
#> Rough estimation of remaining computation time: 0.1 min
#> CGNM iteration estimated to finish at: 2024-11-10 06:19:51.959614
#> Iteration:2 Median sum of squares residual=0.0109178900957768
#> Iteration:3 Median sum of squares residual=0.010839310418537
#> Iteration:4 Median sum of squares residual=0.0108379427073545
#> Iteration:5 Median sum of squares residual=0.0108379417996094
#> Iteration:6 Median sum of squares residual=0.0108379417996094
#> Iteration:7 Median sum of squares residual=0.0108379417996094
#> Iteration:8 Median sum of squares residual=0.0108379417996094
#> Iteration:9 Median sum of squares residual=0.0108379417996094
#> Iteration:10 Median sum of squares residual=0.0108379416862367
#> Iteration:11 Median sum of squares residual=0.0108379416802038
#> CGNM iteration estimated to finish at: 2024-11-10 06:19:51.700308
#> Iteration:12 Median sum of squares residual=0.0108379388600639
#> Iteration:13 Median sum of squares residual=0.0108379384003275
#> Iteration:14 Median sum of squares residual=0.0108379384003275
#> Iteration:15 Median sum of squares residual=0.0108379383976801
#> Iteration:16 Median sum of squares residual=0.0108379383976801
#> Iteration:17 Median sum of squares residual=0.0108379383976801
#> Iteration:18 Median sum of squares residual=0.0108379383974245
#> Iteration:19 Median sum of squares residual=0.0108379383974245
#> Iteration:20 Median sum of squares residual=0.0108379383971709
#> Iteration:21 Median sum of squares residual=0.0108379383971709
#> CGNM iteration estimated to finish at: 2024-11-10 06:19:51.682825
#> Iteration:22 Median sum of squares residual=0.0108379383971709
#> Iteration:23 Median sum of squares residual=0.0108379383971709
#> Iteration:24 Median sum of squares residual=0.0108379383971709
#> Iteration:25 Median sum of squares residual=0.0108379383971706
#> CGNM computation time: 0.1 min
CGNM Bootstrap: Minimum | 25 percentile | Median | 75 percentile | Maximum | RSE (%) | |
---|---|---|---|---|---|---|
Ka | 0.4825952 | 0.5185654 | 0.5797809 | 0.921994 | 1.165495 | 29.088484 |
V1 | 9.4367182 | 10.6050940 | 12.5429304 | 18.864891 | 23.125694 | 28.825515 |
CL | 9.4066568 | 9.6463657 | 9.7847685 | 10.032685 | 11.036551 | 2.859271 |
To use the plot functions the user needs to manually load ggplot2.
Despite the robustness of the algorithm not all approximate minimizers converge so here we visually inspect to see how many of the approximate minimizers we consider to have the similar SSR to the minimum SSR. Currently the algorithm automatically choose “acceptable” approximate minimizer based on Grubbs’ Test for Outliers. If for whatever the reason this criterion is not satisfactly the users can manually set the indicies of the acceptable approximat minimizers.
plot_profileLikelihood(c("CGNM_log","CGNM_log_bootstrap"))+scale_x_continuous(trans="log10")
#> [1] "log saved in /tmp/Rtmp0QD21t/Rbuildb3b185ee13f/CGNM/vignettes/CGNM_log is used to draw SSR/likelihood surface"
#> [1] "log saved in /tmp/Rtmp0QD21t/Rbuildb3b185ee13f/CGNM/vignettes/CGNM_log_bootstrap is used to draw SSR/likelihood surface"
kable(table_profileLikelihoodConfidenceInterval(c("CGNM_log","CGNM_log_bootstrap"), alpha = 0.25))
#> [1] "WARNING: ALWAYS first inspect the profile likelihood plot (using plot_profileLikelihood()) and then use this table, DO NOT USE this table by itself."
#> [1] "log saved in /tmp/Rtmp0QD21t/Rbuildb3b185ee13f/CGNM/vignettes/CGNM_log is used to draw SSR/likelihood surface"
#> [1] "log saved in /tmp/Rtmp0QD21t/Rbuildb3b185ee13f/CGNM/vignettes/CGNM_log_bootstrap is used to draw SSR/likelihood surface"
25 percentile | best-fit | 75 percentile | identifiability | |
---|---|---|---|---|
Ka | 0.4856014 | NA | 1.077699 | Not identifiable |
V1 | 9.4408910 | NA | 21.459620 | Not identifiable |
CL | 9.3136345 | NA | 10.494766 | Not identifiable |
plot_2DprofileLikelihood(CGNM_result, showInitialRange=FALSE, alpha = 0.05)+scale_x_continuous(trans="log10")+scale_y_continuous(trans="log10")
#> [1] "log saved in /tmp/Rtmp0QD21t/Rbuildb3b185ee13f/CGNM/vignettes/CGNM_log is used to draw SSR/likelihood surface"
#> [1] "log saved in /tmp/Rtmp0QD21t/Rbuildb3b185ee13f/CGNM/vignettes/CGNM_log_bootstrap is used to draw SSR/likelihood surface"
Cluster Gauss Newton method implementation in CGNM package (above version 0.6) can use nonlinear function that takes multiple input vectors stored in matrix (each column as the input vector) and output matrix (each column as the output vector). This implementation was to be used to parallelize the computation. See below for the examples of parallelized implementation in various hardware. Cluster Gauss Newton method is embarrassingly parallelizable so the computation speed is almost proportional to the number of computation cores used especially for the nonlinear functions that takes time to compute (e.g. models with numerical method to solve a large system of ODEs).
model_matrix_function=function(x){
Y_list=lapply(split(x, rep(seq(1:nrow(x)),ncol(x))), model_function)
Y=t(matrix(unlist(Y_list),ncol=length(Y_list)))
}
testX=t(matrix(c(rep(0.01,3),rep(10,3),rep(100,3)), nrow = 3))
print("testX")
#> [1] "testX"
print(testX)
#> [,1] [,2] [,3]
#> [1,] 1e-02 1e-02 1e-02
#> [2,] 1e+01 1e+01 1e+01
#> [3,] 1e+02 1e+02 1e+02
print("model_matrix_function(testX)")
#> [1] "model_matrix_function(testX)"
print(model_matrix_function(testX))
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 1.9782455 2.2578754 2.517166 2.6529261 2.7982741 2.9311513 2.9684634
#> [2,] 1.7756978 1.8804296 1.860008 1.7832148 1.6114094 1.1771685 0.7428740
#> [3,] 0.9609136 0.9175059 0.830647 0.7437881 0.5700703 0.1357758 -0.2985186
#> [,8] [,9]
#> [1,] 2.9771626 2.952246
#> [2,] -0.5600094 -3.165776
#> [3,] -1.6014021 -4.207169
print("model_matrix_function(testX)-rbind(model_function(testX[1,]),model_function(testX[2,]),model_function(testX[3,]))")
#> [1] "model_matrix_function(testX)-rbind(model_function(testX[1,]),model_function(testX[2,]),model_function(testX[3,]))"
print(model_matrix_function(testX)-rbind(model_function(testX[1,]),model_function(testX[2,]),model_function(testX[3,])))
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
#> [1,] 0 0 0 0 0 0 0 0 0
#> [2,] 0 0 0 0 0 0 0 0 0
#> [3,] 0 0 0 0 0 0 0 0 0
# library(parallel)
#
# obsLength=length(observation)
#
## Given CGNM searches through wide range of parameter combination, it can encounter
## parameter combinations that is not feasible to evaluate. This try catch function
## is implemented within CGNM for regular functions but for the matrix functions
## user needs to implement outside of CGNM
#
# modelFunction_tryCatch=function(x_in){
# out=tryCatch({model_function(x_in)},
# error=function(cond) {rep(NA, obsLength)}
# )
# return(out)
# }
#
# model_matrix_function=function(X){
# Y_list=mclapply(split(X, rep(seq(1:nrow(X)),ncol(X))), modelFunction_tryCatch,mc.cores = (parallel::detectCores()-1), mc.preschedule = FALSE)
#
# Y=t(matrix(unlist(Y_list),ncol=length(Y_list)))
#
# return(Y)
# }
#library(foreach)
#library(doParallel)
#numCore=8
#registerDoParallel(numCore-1)
#cluster=makeCluster(numCore-1, type = "PSOCK")
#registerDoParallel(cl=cluster)
# obsLength=length(observation)
## Given CGNM searches through wide range of parameter combination, it can encounter
## parameter combinations that is not feasible to evaluate. This try catch function
## is implemented within CGNM for regular functions but for the matrix functions
## user needs to implement outside of CGNM
# modelFunction_tryCatch=function(x_in){
# out=tryCatch({model_function(x_in)},
# error=function(cond) {rep(NA, obsLength)}
# )
# return(out)
# }
#model_matrix_function=function(X){
# Y_list=foreach(i=1:dim(X)[1], .export = c("model_function", "modelFunction_tryCatch"))%dopar%{ #make sure to include all related functions in .export and all used packages in .packages for more information read documentation of dopar
# modelFunction_tryCatch((X[i,]))
# }
# Y=t(matrix(unlist(Y_list),ncol=length(Y_list)))
#}
CGNM_result=Cluster_Gauss_Newton_method(nonlinearFunction=model_matrix_function,
targetVector = observation,
initial_lowerRange =rep(0.01,3),initial_upperRange = rep(100,3),lowerBound = rep(0,3), saveLog=TRUE, num_minimizersToFind = 500, ParameterNames = c("Ka","V1","CL"))
#> Warning in dir.create(saveFolderName): 'CGNM_log' already exists
#> Ka, V1, CL will be transformed internally to impose boundaries. See CGNM_result$runSetting$ReparameterizationDef for exact transformation. Transformed parameters are denoted as x and untransformed parameters are denoted as theta.
#> nonlinearFunction is given as matrix to matrix function
#> NonlinearFunction evaluation at initial_lowerRange Successful.
#> NonlinearFunction evaluation at (initial_upperRange+initial_lowerRange)/2 Successful.
#> NonlinearFunction evaluation at initial_upperRange Successful.
#> CGNM iteration should finish before: 2024-11-10 06:20:01.148404
#> Warning in dir.create(saveFolderName): 'CGNM_log' already exists
#> Generating initial cluster. 498 out of 500 done
#> Generating initial cluster. 500 out of 500 done
#> Iteration:1 Median sum of squares residual=5.56328202167458
#> Rough estimation of remaining computation time: 1 min
#> CGNM iteration estimated to finish at: 2024-11-10 06:20:55.859258
#> Iteration:2 Median sum of squares residual=1.56601899046656
#> Iteration:3 Median sum of squares residual=0.926987980167291
#> Iteration:4 Median sum of squares residual=0.769633862288475
#> Iteration:5 Median sum of squares residual=0.379485456145462
#> Iteration:6 Median sum of squares residual=0.010626376327517
#> Iteration:7 Median sum of squares residual=0.00734959211684528
#> Iteration:8 Median sum of squares residual=0.00734923429121967
#> Iteration:9 Median sum of squares residual=0.00734923408506695
#> Iteration:10 Median sum of squares residual=0.00734923408335566
#> Iteration:11 Median sum of squares residual=0.00734923408269689
#> CGNM iteration estimated to finish at: 2024-11-10 06:20:44.220766
#> Iteration:12 Median sum of squares residual=0.00734923408252463
#> Iteration:13 Median sum of squares residual=0.00734923408241583
#> Iteration:14 Median sum of squares residual=0.00734923408239155
#> Iteration:15 Median sum of squares residual=0.00734923408238872
#> Iteration:16 Median sum of squares residual=0.00734923408238479
#> Iteration:17 Median sum of squares residual=0.00734923408238448
#> Iteration:18 Median sum of squares residual=0.00734923408238376
#> Iteration:19 Median sum of squares residual=0.00734923408238277
#> Iteration:20 Median sum of squares residual=0.00734923408238225
#> Iteration:21 Median sum of squares residual=0.00734923408238176
#> CGNM iteration estimated to finish at: 2024-11-10 06:20:41.3132
#> Iteration:22 Median sum of squares residual=0.00734923408238158
#> Iteration:23 Median sum of squares residual=0.00734923408238126
#> Iteration:24 Median sum of squares residual=0.00734923408238123
#> Iteration:25 Median sum of squares residual=0.00734923408238123
#> CGNM computation time: 0.8 min
#stopCluster(cluster) #make sure to close the created cluster if needed
For the complete description and comparison with the conventional algorithm please see (https: //doi.org/10.1007/s11081-020-09571-2):
Aoki, Y., Hayami, K., Toshimoto, K., & Sugiyama, Y. (2020). Cluster Gauss–Newton method. Optimization and Engineering, 1-31.
Cluster Gauss-Newton method is an algorithm for obtaining multiple minimisers of nonlinear least squares problems minx||f(x) − y*||2 2 which do not have a unique solution (global minimiser), that is to say, there exist x(1) ≠ x(2) such that minx||f(x) − y*||2 2 = ||f(x(1)) − y*||2 2 = ||f(x(2)) − y*||2 2 . Parameter estimation problems of mathematical models can often be formulated as nonlinear least squares problems. Typically these problems are solved numerically using iterative methods. The local minimiser obtained using these iterative methods usually depends on the choice of the initial iterate. Thus, the estimated parameter and subsequent analyses using it depend on the choice of the initial iterate. One way to reduce the analysis bias due to the choice of the initial iterate is to repeat the algorithm from multiple initial iterates (i.e. use a multi-start method). However, the procedure can be computationally intensive and is not always used in practice. To overcome this problem, we propose the Cluster Gauss-Newton method (CGNM), an efficient algorithm for finding multiple approximate minimisers of nonlinear-least squares problems. CGN simultaneously solves the nonlinear least squares problem from multiple initial iterates. Then, CGNM iteratively improves the approximations from these initial iterates similarly to the Gauss-Newton method. However, it uses a global linear approximation instead of the Jacobian. The global linear approximations are computed collectively among all the iterates to minimise the computational cost associated with the evaluation of the mathematical model.