Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
Merge in more PopED upstream bug fixes/improvements
(e.g. diff equation solver defaults)
  • Loading branch information
yngman committed Jul 1, 2016
2 parents 21585dd + 2a9ea01 commit 4e0de3c
Show file tree
Hide file tree
Showing 36 changed files with 222 additions and 50 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export(diag_matlab)
export(downsizing_general_design)
export(ed_laplace_ofv)
export(ed_mftot)
export(efficiency)
export(evaluate.e.ofv.fim)
export(evaluate.fim)
export(feps.add)
Expand Down
8 changes: 5 additions & 3 deletions R/blockfinal.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,11 @@ blockfinal <- function(fn,fmf,dmf,groupsize,ni,xt,x,a,model_switch,bpop,d,docc,s
# ofv_criterion(dmf,npar,poped.db)/ofv_criterion(dmf_init,npar,poped.db))
# }

fprintf(fn,'\nEfficiency (Final/Initial): %0.5g\n',
ofv_criterion(dmf,npar,poped.db)/ofv_criterion(dmf_init,npar,poped.db),both=TRUE)

eff <- efficiency(dmf_init, dmf, poped.db)
fprintf(fn,"\nEfficiency: \n (%s) = %.5g\n",attr(eff,"description"),eff,both=TRUE)
# fprintf(fn,'\nEfficiency (Final/Initial): %0.5g\n',
# ofv_criterion(dmf,npar,poped.db)/ofv_criterion(dmf_init,npar,poped.db),both=TRUE)
#
#fprintf(fn,'\nEfficiency criterion: det(FIM)^(1/npar) = %g\n',dmf^(1/length(params)))
#fprintf(fn,'\nEfficiency (final_design/initial_design): %g\n',(dmf^(1/length(params)))/(dmf_init^(1/length(params))))
#if(fn!="") fprintf('\nEfficiency (final_design/initial_design): %g\n',(dmf^(1/length(params)))/(dmf_init^(1/length(params))))
Expand Down
8 changes: 4 additions & 4 deletions R/create.poped.database.R
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
#' @param hle Step length of derivative of model w.r.t. sigma
#' @param AbsTol The absolute tolerance for the diff equation solver
#' @param RelTol The relative tolerance for the diff equation solver
#' @param iDiffSolverMethod The diff equation solver method, 0, no other option
#' @param iDiffSolverMethod The diff equation solver method, NULL as default.
#' @param bUseMemorySolver If the differential equation results should be stored in memory (1) or not (0)
#' @param rsit Number of Random search iterations
#' @param sgit Number of stochastic gradient iterations
Expand Down Expand Up @@ -576,11 +576,11 @@ create.poped.database <-
## -- Step length of derivative of model w.r.t. sigma --
hle=poped.choose(popedInput$hle,0.00001),
## -- The absolute tolerance for the diff equation solver --
AbsTol=poped.choose(popedInput$AbsTol,0.00001),
AbsTol=poped.choose(popedInput$AbsTol,0.000001),
## -- The relative tolerance for the diff equation solver --
RelTol=poped.choose(popedInput$RelTol,0.00001),
RelTol=poped.choose(popedInput$RelTol,0.000001),
## -- The diff equation solver method, 0, no other option --
iDiffSolverMethod=poped.choose(popedInput$iDiffSolverMethod,0),
iDiffSolverMethod=poped.choose(popedInput$iDiffSolverMethod,NULL),
## -- If the differential equation results should be stored in memory (1) or not (0) --
bUseMemorySolver=poped.choose(popedInput$bUseMemorySolver,FALSE),
## -- Number of Random search iterations --
Expand Down
14 changes: 12 additions & 2 deletions R/create_design.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ create_design <- function(
#all_cov_names <- unique(unlist(sapply(a,names)))

#a <- as.matrix(plyr::rbind.fill(lapply(a,function(x){data.frame(rbind(unlist(x)))})))
a <- as.matrix(dplyr::rbind_all(lapply(a,function(x){data.frame(rbind(unlist(x)))})))
if(packageVersion("dplyr") >= "0.5.0"){
a <- as.matrix(dplyr::bind_rows(lapply(a,function(x){data.frame(rbind(unlist(x)))})))
} else {
a <- as.matrix(dplyr::rbind_all(lapply(a,function(x){data.frame(rbind(unlist(x)))})))
}
}
colnam <- names(a)
if(is.null(colnam)) colnam <- colnames(a)
Expand All @@ -93,7 +97,13 @@ create_design <- function(

## for x ----------
if(!is.null(x)){
if(is.list(x)) x <- as.matrix(dplyr::rbind_all(lapply(x,function(x){data.frame(rbind(unlist(x)))})))
if(is.list(x)){
if(packageVersion("dplyr") >= "0.5.0"){
x <- as.matrix(dplyr::bind_rows(lapply(x,function(x){data.frame(rbind(unlist(x)))})))
} else {
x <- as.matrix(dplyr::rbind_all(lapply(x,function(x){data.frame(rbind(unlist(x)))})))
}
}
colnam <- names(x)
if(is.null(colnam)) colnam <- colnames(x)
if(size(x,1)==1 && m!=1) x <- matrix(rep(x,m),ncol=length(x),nrow=m,byrow=T,dimnames=list(paste("grp_",1:m,sep=""),colnam))
Expand Down
12 changes: 10 additions & 2 deletions R/create_design_space.R
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,11 @@ create_design_space <- function(
## for a ---------
if(!is.null(maxa)){
if(is.list(maxa)){
maxa <- as.matrix(dplyr::rbind_all(lapply(maxa,function(x){data.frame(rbind(unlist(x)))})))
if(packageVersion("dplyr") >= "0.5.0"){
maxa <- as.matrix(dplyr::bind_rows(lapply(maxa,function(x){data.frame(rbind(unlist(x)))})))
} else {
maxa <- as.matrix(dplyr::rbind_all(lapply(maxa,function(x){data.frame(rbind(unlist(x)))})))
}
}
if(size(maxa,1)==1 && m!=1) maxa <- matrix(rep(maxa,m),ncol=length(maxa),nrow=m,byrow=T)
if(!is.matrix(maxa)) maxa <- rbind(maxa)
Expand All @@ -360,7 +364,11 @@ create_design_space <- function(

if(!is.null(mina)){
if(is.list(mina)){
mina <- as.matrix(dplyr::rbind_all(lapply(mina,function(x){data.frame(rbind(unlist(x)))})))
if(packageVersion("dplyr") >= "0.5.0"){
mina <- as.matrix(dplyr::bind_rows(lapply(mina,function(x){data.frame(rbind(unlist(x)))})))
} else {
mina <- as.matrix(dplyr::rbind_all(lapply(mina,function(x){data.frame(rbind(unlist(x)))})))
}
}
if(size(mina,1)==1 && m!=1) mina <- matrix(rep(mina,m),ncol=length(mina),nrow=m,byrow=T)
if(!is.matrix(mina)) mina <- rbind(mina)
Expand Down
6 changes: 5 additions & 1 deletion R/model_prediction.R
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,11 @@ model_prediction <- function(poped.db=NULL,
if(length(unique(tmp.df[nam]))==1) dose.df[nam] <- tmp.df[1,nam]
}
dose.df$dose_record_tmp <- 1
tmp.df <- dplyr::rbind_list(dose.df,tmp.df)
if(packageVersion("dplyr") >= "0.5.0"){
tmp.df <- dplyr::bind_rows(dose.df,tmp.df)
} else {
tmp.df <- dplyr::rbind_list(dose.df,tmp.df)
}
tmp.df <- tmp.df[order(tmp.df$Time,tmp.df$dose_record_tmp),]
tmp.df$dose_record_tmp <- NULL
}
Expand Down
42 changes: 21 additions & 21 deletions R/plot_efficiency_of_windows.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ plot_efficiency_of_windows <- function(poped.db,

if(y_eff){
eff = zeros(1,iNumSimulations)
if(ofv_calc_type==4) {
d_eff = zeros(1,iNumSimulations)
}
# if(ofv_calc_type==4) {
# d_eff = zeros(1,iNumSimulations)
# }
}
if(y_rse) rse <- zeros(iNumSimulations,p)

Expand Down Expand Up @@ -206,13 +206,13 @@ plot_efficiency_of_windows <- function(poped.db,
tmp2 <- ofv_criterion(ofv_fim(ref_fmf,poped.db,ofv_calc_type=ofv_calc_type),
p,poped.db,ofv_calc_type=ofv_calc_type)
eff[1,i] = tmp1/tmp2
if(ofv_calc_type==4) {
tmp1 <- ofv_criterion(ofv_fim(fmf,poped.db,ofv_calc_type=1),
p,poped.db,ofv_calc_type=1)
tmp2 <- ofv_criterion(ofv_fim(ref_fmf,poped.db,ofv_calc_type=1),
p,poped.db,ofv_calc_type=1)
d_eff[1,i] = tmp1/tmp2
}
# if(ofv_calc_type==4) {
# tmp1 <- ofv_criterion(ofv_fim(fmf,poped.db,ofv_calc_type=1),
# p,poped.db,ofv_calc_type=1)
# tmp2 <- ofv_criterion(ofv_fim(ref_fmf,poped.db,ofv_calc_type=1),
# p,poped.db,ofv_calc_type=1)
# d_eff[1,i] = tmp1/tmp2
# }
}
if(y_rse){
rse_tmp <- get_rse(fmf,poped.db)
Expand Down Expand Up @@ -324,16 +324,16 @@ plot_efficiency_of_windows <- function(poped.db,
ind <- NULL
if(y_eff){
efficiency <- eff[1,]*100
if(ofv_calc_type==4) {
d_efficiency <- d_eff[1,]*100
}
# if(ofv_calc_type==4) {
# d_efficiency <- d_eff[1,]*100
# }
}
df <- data.frame(sample=c(1:iNumSimulations))
if(y_eff){
df$Efficiency <- efficiency
if(ofv_calc_type==4) {
df["D-Efficiency"] <- d_efficiency
}
# if(ofv_calc_type==4) {
# df["D-Efficiency"] <- d_efficiency
# }
}
if(y_rse){
rse_df <- data.frame(rse)
Expand All @@ -345,11 +345,11 @@ plot_efficiency_of_windows <- function(poped.db,
names(df_stack) <- c("sample","values","ind")
if(y_eff){
levs <- levels(df_stack$ind)
if(ofv_calc_type==4) {
df_stack$ind <- factor(df_stack$ind,levels=c("D-Efficiency","Efficiency",levs[-c(grep("Efficiency",levs))]))
} else {
df_stack$ind <- factor(df_stack$ind,levels=c("Efficiency",levs[-c(grep("Efficiency",levs))]))
}
# if(ofv_calc_type==4) {
# df_stack$ind <- factor(df_stack$ind,levels=c("D-Efficiency","Efficiency",levs[-c(grep("Efficiency",levs))]))
# } else {
df_stack$ind <- factor(df_stack$ind,levels=c("Efficiency",levs[-c(grep("Efficiency",levs))]))
# }
#levels(df_stack$ind) <- c("Efficiency",levs[-c(grep("Efficiency",levs))])
}

Expand Down
68 changes: 65 additions & 3 deletions R/poped_optim.R
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,11 @@ poped_optim <- function(poped.db,
fprintf("Relative difference in OFV: %.3g%%\n",rel_diff*100)

# efficiency
p = get_fim_size(poped.db)
eff = ofv_criterion(output$ofv,p,poped.db)/ofv_criterion(ofv_init,p,poped.db)
cat("Efficiency: ",sprintf("%.5g",eff), "\n")


eff <- efficiency(ofv_init, output$ofv, poped.db)
fprintf("Efficiency: \n (%s) = %.5g\n",attr(eff,"description"),eff)
#cat("Efficiency: \n ", attr(eff,"description"), sprintf("%.5g",eff), "\n")
#if(eff<=stop_crit_eff) stop_crit <- TRUE

#cat("eff: ",sprintf("%.3g",(output$ofv - ofv_init)/p), "\n")
Expand Down Expand Up @@ -708,3 +710,63 @@ poped_optim <- function(poped.db,
return(invisible(list( ofv= output$ofv, FIM=FIM, poped.db = poped.db )))
}

#' Compute efficiency.
#'
#' Efficiency calculation between two designs.
#'
#'
#' @param ofv_init An initial objective function
#' @param ofv_final A final objective function.
#' @param npar The number of parameters to use for normalization.
#' @param poped_db a poped database
#' @inheritParams ofv_fim
#' @inheritParams poped_optim
#' @inheritParams create.poped.database
#'
#' @return The specified efficiency value depending on the ofv_calc_type.
#' The attribute "description" tells you how the calculation was made
#' \code{attr(return_vale,"description")}
#'
#' @family FIM
#'
#'
## @example tests/testthat/examples_fcn_doc/warfarin_optimize.R
## @example tests/testthat/examples_fcn_doc/examples_ofv_criterion.R
#'
#' @export

efficiency <- function(ofv_init, ofv_final, poped_db,
npar = get_fim_size(poped_db),
ofv_calc_type=poped_db$settings$ofv_calc_type,
ds_index = poped_db$parameters$ds_index) {


eff = ofv_final/ofv_init
attr(eff,"description") <- "ofv_final / ofv_init"

if((ofv_calc_type==1) ){#D-Optimal Design
eff = eff^(1/npar)
attr(eff,"description") <- "(ofv_final / ofv_init)^(1/n_parameters)"
}
if((ofv_calc_type==4) ){#lnD-Optimal Design
eff = (exp(ofv_final)/exp(ofv_init))^(1/npar)
attr(eff,"description") <- "(exp(ofv_final) / exp(ofv_init))^(1/n_parameters)"

}
# if((ofv_calc_type==2) ){#A-Optimal Design
# eff=ofv_f/npar
# }
#
# if((ofv_calc_type==3) ){#S-Optimal Design
# stop(sprintf('Criterion for S-optimal design not implemented yet'))
# }
#
if((ofv_calc_type==6) ){#Ds-Optimal design
eff = eff^(1/sum(ds_index))
attr(eff,"description") <- "(ofv_final / ofv_init)^(1/sum(interesting_parameters))"

}

return( eff )
}

1 change: 1 addition & 0 deletions man/LinMatrixH.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/LinMatrixLH.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/LinMatrixL_occ.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/calc_ofv_and_fim.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions man/create.poped.database.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions man/ed_laplace_ofv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/ed_mftot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions man/efficiency.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4e0de3c

Please sign in to comment.