Closed KyleBarrett closed 6 years ago
I used the command "run_nmx(mod_name,proj=proj_obj,ext = FALSE)" as a workaround. It now appears in the shiny app
Thanks for your message and sorry for my late response... The bootstrap function looks great!
I think the workaround you implemented is OK if this works for you. The reason that the new models are not picked up in the interface might have something to do with naming. Currently it is quite picky about that and only models named 'run[digit(s)].r' are picked up. Although it seems you used this in naming the new models I am not entirely sure (it looks like it works only for run numbers < 10?).
I am not sure if you are able to share the entire project directory, if so I could look into it in more detail.
Hi Richard, I actually have abandoned this script, it was during my early days of messing around with shinyMixR. I have since become very fluid with shinyMixR and have written a couple shiny apps that interface well with the shinyMixR app itself. I declare the server, ui, and RunApp() all within the lapply wrapping that you would see in a traditional shinyMixR script. This allows me to launch a shiny app for dosing event simulation, bootstrapping, etc.. for any model I select...in essence it's a shiny app launched from a shiny app. I often stratify by covariates or pull other useful information from shinyMixR objects, thanks to the various information found in the get_proj() object. Both apps I mentioned use a lot of the nlmixr functions such as nlmixrSim or vpc, and thus interact with nlmixr objects quite well. I will post them to this page once they get a little more stable and have a bit more functionality.
Hi Kyle,
Very nice to see that you are using the package to the fullest :) I am curious to see the shiny apps you created on top of of the package. If you are willing to share them once they are stable and they can be generally used, I could add them to the package if you like...
Hi Richard, That would be great! I have a lot on my plate at the moment but I'll get it to you soon. It's pretty stable now but there are a couple necessary features I still have to add. Do you have a preferred way for me to send it to you or just attach it below?
You could also give code by a pull request.
On Sat, Sep 29, 2018, 12:38 PM Kyle notifications@github.com wrote:
Hi Richard, That would be great! I have a lot on my plate at the moment but I'll get it to you soon. It's pretty stable now but there are a couple necessary features I still have to add. Do you have a preferred way for me to send it to you or just attach it below?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/RichardHooijmaijers/shinyMixR/issues/4#issuecomment-425662628, or mute the thread https://github.com/notifications/unsubscribe-auth/AAfa2mZQAC9st2NCOofoQcBsaT0RoASaks5uf7AVgaJpZM4U95FN .
Hi Richard,
I just want to start by saying this is one of my favorite R packages. Job well done. I wrote a script using the bootstrap package that will bootstrap nlmixr's parameter estimates and save them as the initial estimates in a new model. I used the dump() function to create the new model.r file in the correct shinyMixR folder. I can run the script in the shiny app and it runs as expected by adding a new model to the overview section once I refresh the page. However, I cannot run or edit the new model, and thus cannot run my other scripts either. Im assuming I have to edit a file elsewhere but I haven't yet found a place to do this. Ill post my script below incase it helps:
library(dplyr) library(bootstrap) library(ggplot2) library(formatR) library(tictoc) library(shinyMixR)
tic("Total") lapply(models,function(x){ res <- readRDS(paste0("./shinyMixR/",x,".res.rds"))
Bootstrap Function(
ini <- res$par.fixed ini_names <- rownames(ini) %>% as.character() ini_val <- ini$Estimate %>% as.numeric() %>% round(4) ini_se <- ini$SE %>% as.numeric() %>% round(4) boot <- ini_names %>%as.data.frame(); boot$values <- ini_val; boot$SE <- ini_se; boot <- boot %>% dplyr::rename(Param=".") for(i in 1:length(boot$SE)){ if(is.na(boot$SE[i])==TRUE){boot$SE[i] <- 0}else{boot$SE[i] <- boot$SE[i]}} boot$SampleSize <- 1 boot$rnorm <- rnorm(mean=boot$values,sd=boot$SE,n=boot$SampleSize) %>% round(4)
func <- function(x,i){rnorm(mean=boot$values[i],sd=boot$SE[i],n=boot$SampleSize[i])} for(i in 1:length(boot$SE)){ boot$bootstrap[i] <- mean(bootstrap(x=boot,i,nboot = 100,theta=func)$thetastar) %>% round(4)}
Retrieve Model Estimates
param initial conditions
ini <- res$par.fixed ini_names <- rownames(ini) ini_val <- boot$bootstrap
etas
eta_val <-diag(res$omega) eta_names <- names(diag(res$omega))
New Model Creation
parse model structure
mod <- source(paste0("./models/",x,".r"))$value depar <- deparse(mod) mod_func <- depar[1:(grep("ini",depar)-1)] mod_ini <- depar[grep("ini",depar):(grep("model",depar)-1)] mod_model <- depar[grep("model",depar):length(depar)]
reference old model
ref_pos <- grep("ref",mod_func) ref <- mod_func[grep("ref",mod_func)] mod_func[ref_pos] <- paste(" ref = ",deparse(x))
positions of initial conditions
ini_pos <- vector(); eta_pos <- ini_pos for(i in 1:length(ini_names)){ ini_pos[i] <- grep(ini_names[i],mod_ini)}
positions of eta values
for(i in 1:length(eta_names)){ eta_pos[i] <- grep(eta_names[i],mod_ini)}
repopulate initial conditions
for(i in 1:length(ini_pos)){ mod_ini[ini_pos[i]]<- paste(ini_names[i]," <- ",round(ini_val[i],4))}
repopulate eta values
for(i in 1:length(eta_pos)){ mod_ini[eta_pos[i]] <- paste(eta_names[i]," ~ ",round(eta_val[i],4))}
create name
parse model components
new_mod <-eval(parse(text=c(mod_func,mod_ini,mod_model)))
assign(mod_name,new_mod)
write new model file
dump(mod_name,file=paste0("./models/",mod_name,".r")) toc() })