2929 # mean by groups (Individual)
3030 groupMeans <- with(data , ave(get(covariate ),get(uidCol ), FUN = function (x ) mean(x , na.rm = TRUE )))
3131 # pop mean
32- popMean <- mean(groupMeans )
32+ popMean <- mean(groupMeans , na.rm = TRUE )
3333 # pop std
3434 popStd <- .sd.p (groupMeans )
3535
@@ -171,7 +171,8 @@ foldgen <- function(data,nfold=5,stratVar=NULL){
171171 unique(
172172 quantile(y ,
173173 probs =
174- seq(0 , 1 , length = cuts ))),
174+ seq(0 , 1 , length = cuts ),
175+ na.rm = TRUE )),
175176 include.lowest = TRUE )
176177 }
177178
@@ -235,8 +236,8 @@ optimUnisampling <- function(xvec,N=1000,medValue,floorT=TRUE) {
235236 if (floorT ){
236237 x <- floor(stats :: runif(N , xmin , xmax ))}
237238 else {
238- x <- stats :: runif(N , xmin , xmax )
239- }
239+ x <- stats :: runif(N , xmin , xmax )
240+ }
240241 xdist <- (median(x )- medValue )^ 2
241242 xdist
242243 }
@@ -518,7 +519,7 @@ bootstrapFit <- function(fit,
518519 xPosthoc <- nlmixr2(x ,
519520 data = origData , est = " posthoc" ,
520521 control = list (calcTables = FALSE , print = 1 , compress = FALSE )
521- )
522+ )
522523 saveRDS(xPosthoc , .path )
523524 }
524525 xPosthoc $ objf - fit $ objf
@@ -617,7 +618,7 @@ sampling <- function(data,
617618 len = 1 ,
618619 any.missing = FALSE ,
619620 lower = 2
620- )
621+ )
621622 }
622623
623624 if (performStrat && missing(stratVar )) {
@@ -629,7 +630,7 @@ sampling <- function(data,
629630 lower = 2 ,
630631 len = 1 ,
631632 any.missing = FALSE
632- )
633+ )
633634
634635 if (missing(uid_colname )) {
635636 # search the dataframe for a column name of 'ID'
@@ -761,7 +762,7 @@ modelBootstrap <- function(fit,
761762 len = 1 ,
762763 any.missing = FALSE ,
763764 lower = 1
764- )
765+ )
765766
766767 if (missing(nSampIndiv )) {
767768 nSampIndiv <- length(unique(data [, uidCol ]))
@@ -806,7 +807,7 @@ modelBootstrap <- function(fit,
806807 fnameBootDataPattern <-
807808 paste0(" boot_data_" , " [0-9]+" , " .rds" ,
808809 sep = " "
809- )
810+ )
810811 fileExists <-
811812 list.files(paste0(" ./" , output_dir ), pattern = fnameBootDataPattern )
812813
@@ -886,7 +887,7 @@ modelBootstrap <- function(fit,
886887
887888 if (! restart ) {
888889 if (length(modFileExists ) > 0 &&
889- (length(fileExists ) > 0 )) {
890+ (length(fileExists ) > 0 )) {
890891
891892 # read bootData and modelsEnsemble files from disk
892893 cli :: cli_alert_success(
@@ -960,28 +961,28 @@ modelBootstrap <- function(fit,
960961 modIdx ))
961962
962963 fit <- tryCatch(
963- {
964- fit <- suppressWarnings(nlmixr2(ui ,
965- boot_data ,
966- est = fitMeth ,
967- control = .ctl ))
968-
969- .env $ multipleFits <- list (
970- # objf = fit$OBJF,
971- # aic = fit$AIC,
972- omega = fit $ omega ,
973- parFixedDf = fit $ parFixedDf [, c(" Estimate" , " Back-transformed" )],
974- message = fit $ message ,
975- warnings = fit $ warnings )
976-
977- fit # to return 'fit'
978- },
979- error = function (error_message ) {
980- message(" error fitting the model" )
981- message(error_message )
982- message(" storing the models as NA ..." )
983- return (NA ) # return NA otherwise (instead of NULL)
984- })
964+ {
965+ fit <- suppressWarnings(nlmixr2(ui ,
966+ boot_data ,
967+ est = fitMeth ,
968+ control = .ctl ))
969+
970+ .env $ multipleFits <- list (
971+ # objf = fit$OBJF,
972+ # aic = fit$AIC,
973+ omega = fit $ omega ,
974+ parFixedDf = fit $ parFixedDf [, c(" Estimate" , " Back-transformed" )],
975+ message = fit $ message ,
976+ warnings = fit $ warnings )
977+
978+ fit # to return 'fit'
979+ },
980+ error = function (error_message ) {
981+ message(" error fitting the model" )
982+ message(error_message )
983+ message(" storing the models as NA ..." )
984+ return (NA ) # return NA otherwise (instead of NULL)
985+ })
985986
986987 saveRDS(
987988 .env $ multipleFits ,
@@ -1070,7 +1071,7 @@ extractVars <- function(fitlist, id = "method") {
10701071
10711072
10721073 if (! (id == " omega" ||
1073- id == " parFixedDf" )) {
1074+ id == " parFixedDf" )) {
10741075 # check if all message strings are empty
10751076 if (id == " message" ) {
10761077 prev <- TRUE
@@ -1136,11 +1137,11 @@ getBootstrapSummary <- function(fitList,
11361137 # omega estimates
11371138 omegaMatlist <- extractVars(fitList , id )
11381139 varVec <- simplify2array(omegaMatlist )
1139- mn <- apply(varVec , 1 : 2 , mean )
1140- sd <- apply(varVec , 1 : 2 , sd )
1140+ mn <- apply(varVec , 1 : 2 , mean , na.rm = TRUE )
1141+ sd <- apply(varVec , 1 : 2 , sd , na.rm = TRUE )
11411142
11421143 quants <- apply(varVec , 1 : 2 , function (x ) {
1143- unname(quantile(x , quantLevels ))
1144+ unname(quantile(x , quantLevels , na.rm = TRUE ))
11441145 })
11451146 median <- quants [1 , , ]
11461147 confLower <- quants [2 , , ]
@@ -1162,7 +1163,7 @@ getBootstrapSummary <- function(fitList,
11621163 parFixedlistVec <- do.call(" rbind" , parFixedlistVec )
11631164
11641165 omgVecBoot <- list ()
1165- omegaIdx <- seq(length( omegaMatlist ) )
1166+ omegaIdx <- seq_along( omegaMatlist )
11661167
11671168 omgVecBoot <- lapply(omegaIdx , function (idx ) {
11681169 omgMat <- omegaMatlist [[idx ]]
@@ -1201,8 +1202,16 @@ getBootstrapSummary <- function(fitList,
12011202 parFixedOmegaCombined <- cbind(parFixedlistVec , omgVecBoot )
12021203
12031204 covMatrix <- cov(parFixedOmegaCombined )
1204- corMatrix <- cov2cor(covMatrix )
1205+ w <- which(diag(covMatrix ) == 0 )
1206+ if (length(w ) > 0 ) {
1207+ d <- dim(covMatrix )[1 ]
1208+ corMatrix <- matrix (rep(0 ,d * d ), d , d )
1209+ corMatrix [- w , - w ] <- cov2cor(covMatrix [- w , - w ])
1210+ } else {
1211+ corMatrix <- cov2cor(covMatrix )
1212+ }
12051213 diag(corMatrix ) <- sqrt(diag(covMatrix ))
1214+ dimnames(corMatrix ) <- dimnames(covMatrix )
12061215 lst <- list (
12071216 mean = mn ,
12081217 median = median ,
@@ -1412,11 +1421,11 @@ bootplot.nlmixr2FitCore <- function(x, ...) {
14121421 } else {
14131422 stop(" this nlmixr2 object does not include boostrap distribution statics for comparison" ,
14141423 call. = FALSE
1415- )
1424+ )
14161425 }
14171426 } else {
14181427 stop(" this is not a nlmixr2 object" ,
14191428 call. = FALSE
1420- )
1429+ )
14211430 }
14221431}
0 commit comments