First we load the NLreg
package and and the DataFrames
package. The readtable
function in creates a DataFrame
from a comma-separated or tab-separated file's contents. The file can be compressed, as shown here.
using DataFrames,NLreg
const sd1 = readtable(Pkg.dir("NLreg","data","sd1.csv.gz"));
Nonlinear regression models are represented as types in the NLreg
package. The type can implement several constructors. Here we generate a BolusSD1
model from a formula expression and a data frame.
m = BolusSD1(:(CONC ~ TIME), sd1);
The formula indicates that the response is CONC
and the first (and only) covariate is TIME
.
A nonlinear least squares fit is represented by the type NonlinearLS
, which is constructed from a nonlinear regression model and, optionally, initial values for the parameters.
If initial values for the parameters are not given the model's initpars
method is used to generate initial values.
show(initpars(m))
The gnfit
function fits the model using the Gauss-Newton algorithm. An optional second argument determines if verbose output is generated.
nl = gnfit(NonlinearLS(m),true)
There are several extractor methods such as coef
, coeftable
, deviance
, pnames
, stderr
and vcov
for this type.
coeftable(nl)
deviance(nl) # residual sum-of-squares
A simple nonlinear mixed-effects model has a random effect for each parameter for each group (e.g. subject). Uncorrelated random effects are indicated by specifying a Diagonal
\(\Lambda\) matrix. Correlated random effects user a Triangular
\(\Lambda\) matrix. The model form and initial estimates of the fixed-effects parameters can be taken from a fitted NonlinearLS
object.
nm = NLreg.fit(SimpleNLMM(nl,vector(sd1[:ID]),Diagonal))
nm1 = NLreg.fit(SimpleNLMM(nl,vector(sd1[:ID]),Triangular))
Although not currently shown in the output, the within-subject correlation of the random effects for this model fit is -0.091
. A likelihood ratio test comparting nm
versus nm1
has a p-value of 39.6% from which we would conclude that the more complex model, nm1
, does not provide a sufficiently better fit to justify the additional parameter.