
Fit the non-proportional slowing model.
Source:R/pmrm_model_slowing_nonproportional.R
pmrm_model_slowing_nonproportional.RdFit the non-proportional slowing model to a clinical dataset on a progressive disease.
Usage
pmrm_model_slowing_nonproportional(
data,
outcome,
time,
patient,
visit,
arm,
covariates = ~0,
visit_times = NULL,
spline_knots = visit_times,
spline_method = c("natural", "fmm"),
reml = FALSE,
hessian = c("divergence", "never", "always"),
saddle = FALSE,
control = list(eval.max = 4000L, iter.max = 4000L),
initial_method = c("regression", "regression_control", "zero"),
initial = NULL,
silent = TRUE
)Arguments
- data
A data frame or
tibbleof clinical data.- outcome
Character string, name of the column in the data with the numeric outcome variable on the continuous scale. Could be a clinical measure of healthy or of disease severity. Baseline is part of the model, so the
outcomeshould not already be a change from baseline. The vector of outcomes may have missing values, either with explicitNAs or with rows in the data missing for one or more visits.- time
Character string, name of the column in the data with the numeric time variable on the continuous scale. This time is the time since enrollment/randomization of each patient. A time value of 0 should indicate baseline.
- patient
Character string, name of the column in the data with the patient ID. This vector could be a numeric, integer, factor, or character vector.
pmrmautomatically converts it into an unordered factor.- visit
Character string, name of the column in the data which indicates the study visit of each row. This column could be a numeric, integer, factor, or character vector. An ordered factor is highly recommended because
pmrmwith levels assumed to be in chronological order. The minimum visit must be baseline.- arm
Character string, name of the column in the data which indicates the study arm of each row. This column could be a numeric, integer, factor, or character vector. An ordered factor is highly recommended because
pmrmautomatically convertsdata[[arm]]into an ordered factor anyway. The minimum level is assumed to be the control arm.- covariates
Partial right-sided formula of concomitant terms in the model for covariate adjustment (e.g. by age, gender, biomarker status, etc.). Should not include main variables such as the values of
outcome,time,patient,visit, orarm. The columns in the data referenced in the formula must not have any missing values.Set
covariatesto~ 0(default) to opt out of covariate adjustment. The intercept term is removed from the model matrixWwhether or not the formula begins with `~ 0.- visit_times
Numeric vector, the continuous scheduled time of each study visit (since randomization). If
NULL, each visit time is automatically set set to the median of the observed times at categorical visit in the data.- spline_knots
Numeric vector of spline knots on the continuous scale, including boundary knots.
- spline_method
Character string, spline method to use for the base model. Must be
"natural"or"fmm". Seestats::splinefun()for details.- reml
TRUEto fit the model with restricted maximum likelihood (REML), which involves integrating out fixed effects.FALSEto use unrestricted maximum likelihood. IfremlisTRUE, thenhessianis automatically set to"never".- hessian
Character string controlling when to supply the Hessian matrix of the objective function to the optimizer
stats::nlminb(). Supplying the Hessian usually slows down optimization but may improve convergence in some cases, particularly saddle points in the objective function.The
hessianargument is automatically set to"never"wheneverremlisTRUE.The
hessianargument must be one of the following values:"divergence": first try the model without supplying the Hessian. Then if the model does not converge, retry while supplying the Hessian."never": fit the model only once and do not supply the Hessian tostats::nlminb()."always": fit the model once and supply the Hessian tostats::nlminb().
- saddle
TRUEto check if the optimization hit a saddle point, and if it did, treat the model fit as if it diverged.FALSEto skip this check for the sake of speed.- control
A named list of control parameters passed directly to the
controlargument ofstats::nlminb().- initial_method
Character string, name of the method for computing initial values. Ignored unless
initialisNULL. Must have one of the following values:"regression": sets the spline vertical distancesalphato the fitted values at the knots of a simple linear regression of the responses versus continuous time. Sets all the other true model parameters to 0."regression_control": like"regression"except we only use the data from the control group. Sets all the other true model parameters to 0."zero": sets all true model parameters to 0, includingalpha.
- initial
If
initialis a named list, thenpmrmuses this list as the initial parameter values for the optimization. Otherwise,pmrmautomatically computes the starting values using the method given in theinitial_methodargument (see below).If
initialis a list, then it must have the following named finite numeric elements conforming to all the true parameters defined invignette("models", package = "pmrm"):alpha: a vector with the same length asspline_knots.theta: a matrix withK - 1rows andJ - 1columns, whereKis the number of study arms andJis the number of study visits.gamma: a vector withVelements, whereVis the number of columns in the covariate adjustment model matrixW. If you are unsure ofV, simply fit a test model (e.g.fit <- pmrm_model_slowing_nonproportional(...)) and then checkncol(fit$constants$W).phi: a vector with the same length asvisit_times(which may be different from the length ofspline_knots).rho: a vector withJ * (J - 1) / 2elements, whereJis the length ofvisit_times.
You can generate an example of the format of this list by fitting a test model (e.g.
fit <- pmrm_model_slowing_nonproportional(...)) and then extractingfit$initialorfit$final.- silent
As MakeADFun.
Value
A pmrm fit object of class c("pmrm_fit_slowing", "pmrm_fit").
For details, see the "pmrm fit objects" section of this help file.
Details
See vignette("models", package = "pmrm") for details.
pmrm fit objects
A "pmrm_fit" object is a classed list returned by modeling functions.
It has the following named elements:
data: atibble, the input data with the missing outcomes removed and the remaining rows sorted by patient and visit within patient. The data has a special"pmrm_data"class and should not be modified by the user.constants: a list of fixed quantities from the data that the objective function uses in the optimization. Most of these quantities are defined in the modeling and simulation vignettes in thepmrmpackage.n_visitsis a positive integer vector with the number of non-missing outcomes for each patient.options: a list of low-level model-fitting options forRTMB.objective: the objective function for the optimization. Returns the minus log likelihood of the model. The arguments are (1) a list of constants, and (2) a list of model parameters. Both arguments have strict formatting requirements. For (1), see theconstantselement of the fitted model object. For (2), seeinitialorfinal.model$fn(from themodelelement of the fitted model object) contains a copy of the objective function that only takes a parameter list. (The constants are in the closure ofmodel$fn.)model: model object returned byRTMB::MakeADFun()with the compiled objective function and gradient. The elements can be supplied to an optimization routine in R such asstats::nlminb().optimization: the object returned bystats::nlminb()to perform the optimization that estimates the parameters.optimization$convergenceequals 0 if an only if the model converges.report: object returned byRTMB::sdreport()which has information on the standard deviations of model parameters.initial: a list of model parameters initial values. Includes true parameters likethetaandalphabut does not include derived parameters likebetaorsigma. You can supply your own list of similarly formatted initial values to theinitialargument of the modeling function you choose.final: a list of model parameter estimates after optimization, but not including derived parameters likebetaorsigma. The format is exactly the same asinitial(see above) to help deal with divergent model fits. If your model fit diverged and you want to try resume the optimization with slightly better values, you can modify values infinaland supply the result to theinitialargument of the modeling function.estimates: a full list of parameter estimates, including derived parametersstandard_errors: a list of parameter standard errors.metrics: a list of high-level model metrics, including:n_observations: positive integer scalar, number of non-missing observations in the data.n_parameters: positive integer scalar, number of model parameters in the data. Includes true parameters likethetabut excludes downstream functions of parameters such asbeta.log_likelihood: numeric scalar, the maximized log likelihood of the fitted model.deviance: deviance of the fitted model, defined here as-2 * log_likelihood.aic: numeric scalar, the Akaike information criterion of the fitted model.bic: numeric scalar, the Bayesian information criterion of the fitted model.
spline: a vectorized function that accepts continuous timexand returns the value of the fitted splinef(x | spline_knots, alpha)at timexgiven the user-specified knotsspline_knotsand the maximum likelihood estimates ofalpha. Useful for diagnosing strange behavior in the fitted spline. If the spline behaves oddly, especially extrapolating beyond the range of the time points, please consider adjusting the knotsspline_knotsor the initial values ofalphawhen refitting the model.
Examples
set.seed(0L)
simulation <- pmrm_simulate_slowing_nonproportional(
visit_times = seq_len(5L) - 1,
gamma = c(1, 2)
)
fit <- pmrm_model_slowing_nonproportional(
data = simulation,
outcome = "y",
time = "t",
patient = "patient",
visit = "visit",
arm = "arm",
covariates = ~ w_1 + w_2
)
str(fit$estimates)
#> List of 9
#> $ alpha : Named num [1:5] 0.00274 0.80652 0.8958 1.38832 1.65612
#> ..- attr(*, "names")= chr [1:5] "1" "2" "3" "4" ...
#> $ theta : num [1:2, 1:4] 0.507 0.578 -0.109 0.168 0.151 ...
#> $ gamma : num [1:2] 1.01 1.96
#> $ phi : num [1:5] 0.00709 -0.0325 0.01659 -0.02732 0.00773
#> $ rho : num [1:10] -0.00339 -0.06394 0.02979 -0.10189 -0.07404 ...
#> $ beta : num [1:3, 1:5] 0 0 0 0 0.507 ...
#> $ sigma : num [1:5] 1.007 0.968 1.017 0.973 1.008
#> $ Lambda: num [1:5, 1:5] 1 -0.00339 -0.06364 0.02965 -0.10012 ...
#> $ Sigma : num [1:5, 1:5] 1.01429 -0.00331 -0.06516 0.02905 -0.10162 ...
names(fit)
#> [1] "data" "constants" "options" "objective"
#> [5] "model" "optimization" "report" "initial"
#> [9] "final" "estimates" "standard_errors" "metrics"
#> [13] "spline"