This function takes multiple imputed datasets (as generated by
the impute() function) and runs an analysis function on
each of them.
Usage
analyse(
imputations,
fun = ancova,
delta = NULL,
transform = attr(fun, "transform"),
...,
ncores = 1,
.validate = TRUE
)Arguments
- imputations
An
imputationsobject as created byimpute().- fun
An analysis function to be applied to each imputed dataset. See details.
- delta
A
data.framecontaining the delta transformation to be applied to the imputed datasets prior to runningfun. See details.- transform
Either
NULLor a transformation specification created byuse_transform(). Defaults toattr(fun, "transform"), if present. The transformation is common to every pooled parameter. See details.- ...
Additional arguments passed onto
fun.- ncores
The number of parallel processes to use when running this function. Can also be a cluster object created by
make_rbmi_cluster(). See the parallelisation section below.- .validate
Should
inputationsbe checked to ensure it conforms to the required format (default =TRUE) ? Can gain a small performance increase if this is set toFALSEwhen analysing a large number of samples.
Value
An analysis object; a list of class "analysis" containing the analysis
results (one entry per imputed dataset, each a named list of parameter
estimates), the name of the analysis function (fun_name), the applied
delta data.frame, the analysis function (fun) and the imputation method.
If supplied, the reporting-scale transform specification is also retained.
This object is normally passed on to pool().
Details
This function works by performing the following steps:
Extract a dataset from the
imputationsobject.Apply any delta adjustments as specified by the
deltaargument.Run the analysis function
funon the dataset.Repeat steps 1-3 across all of the datasets inside the
imputationsobject.Collect and return all of the analysis results.
The analysis function fun must take a data.frame as its first
argument. All other options to analyse() are passed onto fun
via ....
fun must return a named list with each element itself being a
list containing a single
numeric element called est (or additionally se and df if
you had originally specified method_bayes() or method_approxbayes())
i.e.:
myfun <- function(dat, ...) {
mod_1 <- lm(data = dat, outcome ~ group)
mod_2 <- lm(data = dat, outcome ~ group + covar)
x <- list(
trt_1 = list(
est = coef(mod_1)[[group]],
se = sqrt(vcov(mod_1)[group, group]),
df = df.residual(mod_1)
),
trt_2 = list(
est = coef(mod_2)[[group]],
se = sqrt(vcov(mod_2)[group, group]),
df = df.residual(mod_2)
)
)
return(x)
}
Please note that the vars$subjid column (as defined in the original call to
draws()) will be scrambled in the data.frames that are provided to fun.
This is to say they will not contain the original subject values and as such
any hard coding of subject ids is strictly to be avoided.
Warnings raised by fun are collected for each imputed dataset and stored
as unique messages in the rbmi_warnings attribute of its result (accessible
via analysis$results). After analysis, one summary warning lists each
distinct message and the number of datasets affected. This also applies
when running in parallel. Errors from fun still stop the analysis.
By default fun is the ancova() function.
Please note that this function
requires that a vars object, as created by set_vars(), is provided via
the vars argument e.g. analyse(imputeObj, vars = set_vars(...)). Please
see the documentation for ancova() for full details.
Please also note that the theoretical justification for the conditional mean imputation
method (method = method_condmean() in draws()) relies on the fact that ANCOVA is
a linear transformation of the outcomes.
Thus care is required when applying alternative analysis functions in this setting.
Note that ancova() supports the analysis of two or more treatment arms; for more than
two arms it extends the ref / alt naming scheme with alt2, alt3, etc. The set of
treatment-group contrasts to estimate can be customised via the group_contrasts
argument of set_vars(). See ancova() for full details.
The delta argument can be used to specify offsets to be applied
to the outcome variable in the imputed datasets prior to the analysis.
This is typically used for sensitivity or tipping point analyses. The
delta dataset must contain columns vars$subjid, vars$visit (as specified
in the original call to draws()) and delta. Essentially this data.frame
is merged onto the imputed dataset by vars$subjid and vars$visit and then
the outcome variable is modified by:
Please note that in order to provide maximum flexibility, the delta argument
can be used to modify any/all outcome values including those that were not
imputed. Care must be taken when defining offsets. It is recommend that you
use the helper function delta_template() to define the delta datasets as
this provides utility variables such as is_missing which can be used to identify
exactly which visits have been imputed.
Additive delta adjustments are supported only for continuous outcomes.
For count outcomes, delta must be NULL in both this function and
extract_imputed_dfs(). Use rate multipliers in count_strategy() for
count sensitivity analyses.
The optional transform argument specifies a transformation to apply to all
pooled parameters for reporting. It defaults to the "transform" attribute
of fun, so an analysis function can declare its usual reporting scale.
Use use_transform() to create the specification from an expression in x.
A specification is a list with transform and derivative functions and
a scale string naming the reporting scale in printed results.
The analysis estimates are always retained and pooled on their original
scale. After pooling, the transformation is applied to the pooled estimate
and confidence limits; its derivative is used to obtain the transformed
standard error by the delta method. Transformations must be monotone over
each pooled confidence interval; this is checked from the supplied derivative
before transformed results are reported. The original p-values are retained.
See pool() for extracting transformed results.
Parallelisation
To speed up the evaluation of analyse() you can use the ncores argument to enable parallelisation.
Simply providing an integer will get rbmi to automatically spawn that many background processes
to parallelise across. If you are using a custom analysis function then you need to ensure
that any libraries or global objects required by your function are available in the
sub-processes. To do this you need to use the make_rbmi_cluster() function for example:
my_custom_fun <- function(...) <some analysis code>
cl <- make_rbmi_cluster(
4,
objects = list("my_custom_fun" = my_custom_fun),
packages = c("dplyr", "nlme")
)
analyse(
imputations = imputeObj,
fun = my_custom_fun,
ncores = cl
)
parallel::stopCluster(cl)
Note that there is significant overhead both with setting up the sub-processes and with
transferring data back-and-forth between the main process and the sub-processes. As such
parallelisation of the analyse() function tends to only be worth it when you have
> 2000 samples generated by draws(). Conversely using parallelisation if your samples
are smaller than this may lead to longer run times than just running it sequentially.
It is important to note that the implementation of parallel processing within analyse() has
been optimised around the assumption that the parallel processes will be spawned on the same
machine and not a remote cluster. One such optimisation is that the required data is saved to
a temporary file on the local disk from which it is then read into each sub-process. This is
done to avoid the overhead of transferring the data over the network. Our assumption is that
if you are at the stage where you need to be parallelising your analysis over a remote cluster
then you would likely be better off parallelising across multiple rbmi runs rather than within
a single rbmi run.
Finally, if you are doing a tipping point analysis you can get a reasonable performance
improvement by re-using the cluster between each call to analyse() e.g.
See also
extract_imputed_dfs() for manually extracting imputed
datasets.
delta_template() for creating delta data.frames.
ancova() for the default analysis function.
Examples
if (FALSE) { # \dontrun{
vars <- set_vars(
subjid = "subjid",
visit = "visit",
outcome = "outcome",
group = "group",
covariates = c("sex", "age", "sex*age")
)
analyse(
imputations = imputeObj,
vars = vars
)
deltadf <- data.frame(
subjid = c("Pt1", "Pt1", "Pt2"),
visit = c("Visit_1", "Visit_2", "Visit_2"),
delta = c( 5, 9, -10)
)
analyse(
imputations = imputeObj,
delta = deltadf,
vars = vars
)
} # }
