knitr::opts_chunk$set(comment='.',fig.path="inst/img/README-")

Installation

devtools:::install_github("kylebaron/klava")

Example

requireNamespace("nlme")
library(dplyr)
library(mrgsolve)
library(nloptr)
library(ggplot2)
library(rlang)
library(klava)

Load an mrgsolve model

mod <- modlib("pk2")

Grab some data

data <- readRDS("inst/dat/2cmtA.RDS")

ggplot(data, aes(time,DV)) + geom_point() + theme_bw()

The data is more or less in NONMEM-type format

data

Define a parameter list

theta <- all_log(CL = 0.5, V2 = 50, Q = 1.1, V3 = 30, KA = 1.1, sigma=1.1)

This is a self-transforming vector

theta
trans(theta)

That also supports fixed values

foo <- quick_par(CL = log(1), KA = fixed(1.1), F1 = logit(0.8))

foo

trans(foo)

untrans(trans(foo))

Fit the model

fit <- fit_nl(theta, data, mod = mod, pred_name= "CP", cov_step=TRUE,
              pred_initial=TRUE)

Result

fit$tab

Diagnostics

plot(fit)
ggplot(fit$data, aes(time,RES)) + geom_point() + 
  geom_hline(yintercept=0) + theme_bw()
ggplot(fit$data, aes(PRED,DV)) + geom_point() + 
  geom_abline(intercept = 0, slope = 1) + theme_bw()

Objective functions

Extended Least Squares - ELS

fit <- fit_nl(theta, data, mod, pred_name= "CP", ofv=els)

Normal likelihood

fit <- fit_nl(theta, data, mod, pred_name= "CP", ofv=ml)

Ordinary Least Squares - OLS

fit <- fit_nl(theta, data, mod, pred_name= "CP", ofv=ols)

Weighted Least Squares - WLS

fit <- fit_nl(theta, data, mod, pred_name= "CP", ofv=wls)


kylebaron/optimhelp documentation built on Sept. 13, 2021, 7:40 a.m.