Skip to content

Commit

Permalink
Select vpc vpc_vpc based on vpc version installed.
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfidler committed May 21, 2018
1 parent 3592d97 commit e8b9314
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 23 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -215,5 +215,4 @@ importFrom(utils,sessionInfo)
importFrom(utils,stack)
importFrom(utils,str)
importFrom(utils,tail)
importFrom(vpc,vpc)
useDynLib(nlmixr, .registration=TRUE)
10 changes: 7 additions & 3 deletions R/vpc.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,13 @@ vpc <- function (sim, ...)
vpc.default <- function(sim, ...){
ns <- loadNamespace("vpc");
if (exists("vpc_vpc",ns)){
vpc::vpc_vpc(sim, ...)
vpcn <- "vpc_vpc"
} else {
vpc::vpc(sim, ...)
vpcn <- "vpc"
}
call <- as.list(match.call(expand.dots=TRUE))[-1];
call <- call[names(call) %in% methods::formalArgs(getFromNamespace(vpcn,"vpc"))]
p = do.call(getFromNamespace(vpcn,"vpc"), call, envir = parent.frame(1))
}

#' Visual predictive check (VPC) for nlmixr nlme objects
Expand All @@ -69,14 +72,15 @@ vpc.default <- function(sim, ...){
#' @param fit nlme fit object
#' @param nsim number of simulations
#' @param condition conditional variable
#' @param ... Additional arguments
#' @inheritParams vpc::vpc
#' @return NULL
#' @examples
#' specs <- list(fixed=lKA+lCL+lV~1, random = pdDiag(lKA+lCL~1), start=c(lKA=0.5, lCL=-3.2, lV=-1))
#' fit <- nlme_lin_cmpt(theo_md, par_model=specs, ncmt=1, verbose=TRUE)
#' vpc(fit, nsim = 100, condition = NULL)
#' @export
vpc_nlmixr_nlme = function(fit, nsim=100, condition=NULL)
vpc_nlmixr_nlme = function(fit, nsim=100, condition=NULL, ...)
{
nlmeModList(fit$env);
on.exit({nlmeModList(new.env(parent=emptyenv()))})
Expand Down
16 changes: 12 additions & 4 deletions R/vpc.nlme.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
vpc.nlme = function (fit, nsim = 100, by = NULL, ...)
vpc.nlme = function (fit, nsim = 100, by = NULL, ...)
{
if (class(fit) == "nlmixr.ui.nlme") fit = as.nlme(fit)
dat = getData(fit)
Expand Down Expand Up @@ -30,11 +30,19 @@ vpc.nlme = function (fit, nsim = 100, by = NULL, ...)
xs = do.call("cbind", s)

df = cbind(xd[ord, c("ID", "TIME", "grp")], DV = as.vector(xs), SIM = sim)
if (!is.null(by)) {
p = vpc::vpc(sim = df, obs = dat, strat = c("grp"), facet = "wrap", ...)
ns <- loadNamespace("vpc");
if (exists("vpc_vpc",ns)){
vpcn <- "vpc_vpc"
} else {
p = vpc::vpc(sim = df, obs = dat, ...)
vpcn <- "vpc"
}
call <- as.list(match.call(expand.dots=TRUE))[-1];
if (!is.null(by)) {
call$strat <- c("grp")
call$facet <- "wrap";
}
call <- call[names(call) %in% methods::formalArgs(getFromNamespace(vpcn,"vpc"))]
p = do.call(getFromNamespace(vpcn,"vpc"), c(list(sim=df, obs=dat), call), envir = parent.frame(1))
print(p)
invisible(df)
}
Expand Down
14 changes: 11 additions & 3 deletions R/vpc.saemFit.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,19 @@ vpc_saemFit = function(fit, dat, nsim = 100, by=NULL, ...) {
xs = do.call("cbind",s)
df = cbind(xd[ord, c("ID", "TIME", "grp")], DV=as.vector(xs), SIM=sim)

if (!is.null(by)) {
p = vpc::vpc(sim = df, obs = dat, strat=c("grp"), facet="wrap", ...)
ns <- loadNamespace("vpc");
if (exists("vpc_vpc",ns)){
vpcn <- "vpc_vpc"
} else {
p = vpc::vpc(sim = df, obs = dat, ...)
vpcn <- "vpc"
}
call <- as.list(match.call(expand.dots=TRUE))[-1];
if (!is.null(by)) {
call$strat <- c("grp")
call$facet <- "wrap";
}
call <- call[names(call) %in% methods::formalArgs(getFromNamespace(vpcn,"vpc"))]
p = do.call(getFromNamespace(vpcn,"vpc"), c(list(sim=df, obs=dat), call), envir = parent.frame(1))
print(p)

invisible(df)
Expand Down
10 changes: 4 additions & 6 deletions R/vpc.ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
##' @inheritParams vpc::vpc
##' @inheritParams RxODE::rxSolve
##' @param ... Args sent to \code{\link[RxODE]{rxSolve}}
##' @inheritParams vpc::vpc
##' @return Simulated dataset (invisibly)
##' @author Matthew L. Fidler
##' @export
vpc_ui <- function(fit, data=NULL, n=100, bins = "jenks",
Expand Down Expand Up @@ -100,7 +100,9 @@ vpc_ui <- function(fit, data=NULL, n=100, bins = "jenks",
call$obs_cols = list(id="id", dv="dv", idv="time")
call$sim_cols = list(id="id", dv="dv", idv="time")
call$stratify = stratify
do.call(getFromNamespace(vpcn,"vpc"), c(list(sim=sim, obs=dat), call), envir = parent.frame(1))
p = do.call(getFromNamespace(vpcn,"vpc"), c(list(sim=sim, obs=dat), call), envir = parent.frame(1))
print(p);
return(invisible(sim));
}


Expand Down Expand Up @@ -128,7 +130,3 @@ vpc.nlmixr.ui.nlme <- function(sim, ...){
vpc.ui <- function(sim, ...){
vpc_ui(fit=sim, ...);
}

##' @importFrom vpc vpc
##' @export
vpc::vpc
6 changes: 1 addition & 5 deletions man/reexports.Rd

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

19 changes: 19 additions & 0 deletions man/vpc.Rd

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

4 changes: 3 additions & 1 deletion man/vpc_nlmixr_nlme.Rd

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

3 changes: 3 additions & 0 deletions man/vpc_ui.Rd

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

0 comments on commit e8b9314

Please sign in to comment.