| Type: | Package |
| Title: | Imputation with Deep Learning Methods |
| Version: | 1.2.0 |
| Description: | Imputation of mixed-type and compositional data with neural networks. Each network head is a generalised linear model plus a neural-network correction. Missing values are imputed by point predictions or, for multiple imputation, by predictive draws on bootstrap fits, with pooling through 'mice'. The architecture (number and size of hidden layers, dropout, activation, optimiser) and the training settings are user-configurable. See Templ (2021) <doi:10.1007/978-3-030-71175-7>. |
| License: | GPL-2 |
| URL: | https://github.com/matthias-da/deepImp |
| BugReports: | https://github.com/matthias-da/deepImp/issues |
| Encoding: | UTF-8 |
| LazyData: | TRUE |
| ByteCompile: | TRUE |
| Depends: | R (≥ 4.1) |
| Imports: | torch, nnet, VIM, stats, utils, graphics |
| Suggests: | future, future.apply, keras3, knitr, mice, rmarkdown, robCompositions, testthat (≥ 3.0.0) |
| RoxygenNote: | 7.3.3 |
| Config/testthat/edition: | 3 |
| VignetteBuilder: | knitr |
| NeedsCompilation: | no |
| Packaged: | 2026-11-14 11:40:00 UTC; matthias |
| Author: | Matthias Templ |
| Maintainer: | Matthias Templ <matthias.templ@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-11-14 13:50:02 UTC |
deepImp: Imputation with Deep Learning Methods
Description
Imputes missing values with configurable neural networks, for both mixed-type
data (impNNet()) and compositional data with rounded zeros
(impNNetCoDa()). The network architecture (depth, width, dropout, activation,
optimiser) is described by a deepimp_arch() object. Networks run on a native
torch backend by default (no Python required) or, optionally, on
keras3. Results are returned as a "deepimp" object; read the completed
data with getImputed().
Details
See vignette("deepImp") for a walkthrough.
Author(s)
Maintainer: Matthias Templ matthias.templ@gmail.com (ORCID)
References
Templ, M. (2021). Imputation of rounded zeros for compositional data using neural networks. In: Advances in Compositional Data Analysis (Festschrift). Springer. doi:10.1007/978-3-030-71175-7
See Also
Useful links:
Convert a deepimp object to a mice mids object
Description
Hands the completed data sets to the mice analysis and pooling
machinery, e.g. with(as_mids(imp), lm(y ~ x)) followed by mice::pool().
Rubin's rules are only meaningful for multiple imputations, i.e. for objects
from impNNet() with draw = "pmm" or "normal" and m > 1. Point-mode
replicates (draw = "none") differ only by retraining; pooling them
understates the between-imputation variance, so the conversion warns.
Usage
as_mids(object)
Arguments
object |
a |
Value
a mids object (requires the mice package).
See Also
Examples
if (requireNamespace("torch", quietly = TRUE) && torch::torch_is_installed() &&
requireNamespace("mice", quietly = TRUE)) {
data(sleep, package = "VIM")
mi <- impNNet(sleep, arch = deepimp_arch_small(dropout = 0), m = 3,
draw = "pmm", iterations = 2, epochs = 5, seed = 1)
fit <- with(as_mids(mi), lm(Sleep ~ log(BodyWgt) + Danger))
summary(mice::pool(fit))
}
keras3 backend for deepImp
Description
Internal backend implementing the deepImp model seam (build/fit/predict) with keras3 and its Keras/TensorFlow backend. Optional alternative to the default torch backend; not called directly by users.
torch backend for deepImp
Description
Internal backend implementing the deepImp model seam (build/fit/predict) with torch and a plain training loop on preloaded tensors. Not called directly by users.
Beer ageing volatile-compound data
Description
Concentrations of 16 volatile compounds measured in beer samples, together with an indicator distinguishing fresh from aged beer. The volatile compounds are recognised markers of beer flavour and ageing (e.g. furfural, 5-hydroxymethylfurfural, hexanal, methional-related aldehydes). The data are used to illustrate imputation of mixed continuous measurements.
Usage
data(beer)
Format
A data frame with 86 rows and 17 variables:
- v3MeBual
3-methylbutanal concentration.
- v3MeBuon
3-methylbutanone concentration.
- v2MeBual
2-methylbutanal concentration.
- vHexanal
hexanal concentration.
- v2FurMeol
2-furanmethanol (furfuryl alcohol) concentration.
- vHeptanal
heptanal concentration.
- v2AcFur
2-acetylfuran concentration.
- v5Me2Fur
5-methyl-2-furaldehyde concentration.
- vEssFuEst
furanoic acid ethyl ester concentration.
- v2Ac5MeFu
2-acetyl-5-methylfuran concentration.
- v2PhEtal
2-phenylethanal (phenylacetaldehyde) concentration.
- vNicEtEst
nicotinic acid ethyl ester concentration.
- v2PhEssEt
2-phenylethyl acetate concentration.
- vgNonalac
gamma-nonalactone concentration.
- vFurfural
furfural concentration.
- vHMF
5-hydroxymethylfurfural (HMF) concentration.
- vnewold2
beer condition: 1 = fresh, 2 = aged.
Source
Derived from a raw beer-ageing table without the brewery identifiers;
the derivation is documented in data-raw/beer.R in the package sources.
The deepimp object
Description
impNNet() and impNNetCoDa() return an S3 object of class "deepimp", a
list with the elements
datathe incomplete input data.
imputeda list of
mcompleted data frames.archthe
deepimp_arch()object used.infoa list of fitting metadata:
backend,vartypes,draw,bootstrap,pmm_k,iterations,eps,initialize,control(thedeepimp_control()object),convergence(per replicate, the pooled standardised change per sweep),converged(per replicate;NAin draw mode, which runs a fixed number of sweeps),trace(arraysmeanandsdof the imputed cells, variable x sweep x replicate),history(one row per fit: replicate, sweep, variable, epochs used, best epoch, best validation loss, safeguard status),skipped(variables caught by a safeguard),seeds,n_missing, andmodels(withkeep_models = TRUE: per replicate and variable the exported model used bypredict.deepimp()).impNNetCoDa()addscoda,dlandcorrection.
Details
Methods: getImputed() and as.data.frame() (completed data), print(),
summary.deepimp(), plot.deepimp() (convergence and chain trace),
as_mids() (pooling with mice), predict.deepimp() (imputing new
data). Objects created by deepImp 1.1.0 lack some info fields; all methods
still work on them.
See Also
impNNet(), impNNetCoDa(), new_deepimp()
Architecture configuration for neural-network imputation
Description
A backend-neutral description of the multilayer perceptron used by impNNet().
The same object is translated into a torch (and, later, keras) model.
Usage
deepimp_arch(
hidden = c(256, 128, 64),
dropout = 0.1,
activation = "relu",
batchnorm = TRUE,
optimizer = "adam",
learning_rate = 0.001,
glm_offset = TRUE,
glm_ridge = 0.001,
weight_decay = 0
)
deepimp_arch_small(
dropout = 0.1,
activation = "relu",
batchnorm = TRUE,
optimizer = "adam",
learning_rate = 0.001,
glm_offset = TRUE,
glm_ridge = 0.001,
weight_decay = 0
)
Arguments
|
integer vector of hidden-layer widths; its length is the depth. | |
dropout |
dropout rate applied after every hidden layer; a scalar in |
activation |
hidden-layer activation: one of |
batchnorm |
logical; apply batch normalisation after each hidden linear layer. |
optimizer |
one of |
learning_rate |
positive learning rate for the optimiser. |
glm_offset |
logical; if |
glm_ridge |
non-negative ridge penalty of the GLM offset (ridge on the
least-squares fit, intercept unpenalised; |
weight_decay |
non-negative weight decay of the MLP weights (the GLM
offset has its own |
Value
an object of class "deepimp_arch".
See Also
Examples
deepimp_arch(hidden = c(128, 64), dropout = 0.2)
Training controls for neural-network imputation
Description
A validated set of training settings for impNNet() and impNNetCoDa(),
the counterpart of deepimp_arch() for the fitting procedure. The
top-level arguments epochs, patience, batch_size and
validation_split of impNNet() remain as shortcuts; supply either those
or control, not both.
Usage
deepimp_control(
epochs = 400L,
patience = 10L,
batch_size = "auto",
validation_split = 0.2,
restore_best = TRUE,
lr_schedule = c("none", "plateau"),
device = c("cpu", "cuda", "auto"),
num_threads = NULL,
warm_start = NULL,
parallel = FALSE,
min_obs = 20L
)
Arguments
epochs |
maximum number of training epochs per model ( |
patience |
early stopping: number of epochs without improvement of the
validation loss before training stops. The defaults ( |
batch_size |
minibatch size, or |
validation_split |
share of the observed rows held out for early stopping (not used with bootstrap fits, which validate on the out-of-bag rows). |
restore_best |
logical; restore the weights of the epoch with the
lowest validation loss after training (default). With |
lr_schedule |
|
device |
torch backend only: |
num_threads |
torch backend only: number of CPU threads for libtorch,
or |
warm_start |
logical; from the second sweep on, continue training each
column model from its weights of the previous sweep (with patience
|
parallel |
logical; run the |
min_obs |
minimum number of observed values a variable needs for a network fit; variables with fewer keep their starting values, with a warning. |
Value
an object of class "deepimp_control".
See Also
impNNet(), impNNetCoDa(), deepimp_arch()
Examples
deepimp_control(epochs = 100, patience = 10, batch_size = "auto")
Extract imputed data from a deepimp object
Description
Extract imputed data from a deepimp object
Usage
getImputed(object, m = 1L, ...)
Arguments
object |
a |
m |
which completed dataset to return: an integer index, |
... |
unused. |
Value
a data.frame, or a list of data.frames when m = "all".
See Also
Guess the measurement type of each variable
Description
Classifies each column as "numeric", "mixed" (semi-continuous, i.e. a spike
of repeated values such as zeros plus a continuous part), "binary",
"nominal", or "count". Used by impNNet() to choose the model head, and by
summary() of a "deepimp" object.
Usage
guessType(x)
Arguments
x |
a data.frame, data.table, or tibble. |
Value
a list with indices (a type-by-variable logical matrix) and type
(a character vector, one entry per column).
Author(s)
Matthias Templ
Examples
data(sleep, package = "VIM")
guessType(sleep)
Neural-network imputation for mixed-type data
Description
Iterative, chained imputation of numeric, count, semi-continuous, binary and nominal variables with a configurable multilayer perceptron (torch backend).
Usage
impNNet(
data,
arch = NULL,
m = 1L,
backend = c("torch", "keras"),
vartypes = "guess",
initialize = "knn",
iterations = NULL,
eps = 0.01,
normalize = TRUE,
epochs = 400L,
patience = 10L,
validation_split = 0.2,
batch_size = "auto",
seed = NULL,
verbose = FALSE,
draw = c("none", "pmm", "normal"),
bootstrap = NULL,
pmm_k = 5L,
control = NULL,
keep_models = FALSE,
...
)
Arguments
data |
a data.frame (tibbles/data.tables are coerced). |
arch |
a |
m |
number of completed data sets (replicates). See "Point mode and draw mode": only draw-mode replicates are multiple imputations. |
backend |
|
vartypes |
|
initialize |
starting values for NAs: |
iterations |
number of chained sweeps: the maximum in point mode
(default 3), the fixed number in draw mode (default 5). |
eps |
convergence tolerance on the standardised mean change (point mode only). |
normalize |
standardise numeric predictors. |
epochs, patience, validation_split, batch_size |
shortcuts for the
corresponding |
seed |
optional integer seed (sets the R and backend RNGs). Exact
reproducibility is guaranteed when |
verbose |
|
draw |
|
bootstrap |
logical; fit every column model on a bootstrap resample of
the observed rows, with the out-of-bag rows as the validation set. Defaults
to |
pmm_k |
number of donors for |
control |
a |
keep_models |
logical; keep the fitted models of the last sweep (as
plain R arrays, so the object can be saved with |
... |
architecture scalar overrides forwarded to |
Value
a "deepimp" object (see deepimp); read the data with
getImputed().
Point mode and draw mode
With draw = "none" (point mode) every missing cell gets the network's
conditional mean (regression heads) or its most probable class
(classification heads), and the chain stops once the pooled change falls
below eps. Point imputations suit prediction-type use, but replicates
(m > 1) then differ only by retraining, so they understate the imputation
uncertainty and must not be pooled with Rubin's rules.
With draw = "pmm" or "normal" (draw mode) every missing cell gets a draw
from the predictive distribution, and by default (bootstrap = TRUE) every
fit uses a bootstrap resample of the observed rows. The classification heads
then sample from their softmax probabilities. Each replicate runs all
iterations sweeps. The m completed data sets are multiple imputations
that can be analysed and pooled with Rubin's rules, e.g. with
mice::pool() via as_mids().
PMM copies observed values, so it keeps count and semi-continuous variables
on their observed support; "normal" adds Gaussian noise with the residual
SD of the held-out (out-of-bag) rows and suits continuous variables. For
exactly reproducible draws use dropout = 0.
Author(s)
Matthias Templ
See Also
deepimp_arch(), deepimp_control(), getImputed(), as_mids(),
predict.deepimp(), guessType()
Examples
if (requireNamespace("torch", quietly = TRUE) && torch::torch_is_installed()) {
data(sleep, package = "VIM")
imp <- impNNet(sleep, arch = deepimp_arch_small(), epochs = 5, seed = 1)
head(getImputed(imp))
# multiple imputation: predictive draws on bootstrap fits
mi <- impNNet(sleep, arch = deepimp_arch_small(dropout = 0), m = 3,
draw = "pmm", iterations = 2, epochs = 5, seed = 1)
head(getImputed(mi, m = "long"))
}
Neural-network imputation of rounded zeros in compositional data
Description
Imputes rounded zeros (values below a detection limit) in compositional data with a configurable neural network, following Templ (2021). Each part with rounded zeros is pivoted to the front, transformed to pivot log-ratio coordinates, regressed on the remaining coordinates, censored at the detection limit, and back-transformed with preservation of the observed absolute values.
Usage
impNNetCoDa(
x,
dl = NULL,
label = 0,
coda = TRUE,
correction = c("truncate", "expectation", "none"),
initialize = "kNNa",
arch = NULL,
m = 1L,
backend = c("torch", "keras"),
iterations = 2L,
eps = 0.01,
normalize = TRUE,
epochs = 400L,
patience = 10L,
validation_split = 0.2,
batch_size = "auto",
seed = NULL,
verbose = FALSE,
control = NULL,
...
)
Arguments
x |
a data.frame of compositional parts (positive values; rounded zeros
marked by |
dl |
detection limits, a numeric vector of length |
label |
the value marking a rounded zero in |
coda |
if |
correction |
censoring of imputed values: |
initialize |
starting values for the rounded zeros: |
arch |
a |
m |
number of stochastic replicate completions (not valid multiple
imputation; see |
backend |
|
iterations |
maximum number of chained sweeps. |
eps |
convergence tolerance on the standardised mean change. |
normalize |
standardise numeric predictors. |
epochs, patience, validation_split, batch_size |
shortcuts for the
corresponding |
seed |
optional integer seed (sets the R and backend RNGs). Exact
reproducibility is guaranteed when |
verbose |
|
control |
a |
... |
architecture scalar overrides forwarded to |
Details
Needs the robCompositions package (pivot coordinates and the compositional kNN start), which deepImp suggests but does not import.
Value
a "deepimp" object; read the data with getImputed().
Author(s)
Matthias Templ
References
Templ, M. (2021) Imputation of rounded zeros for high-dimensional compositional data.
See Also
impNNet(), deepimp_arch(), getImputed()
Examples
if (requireNamespace("torch", quietly = TRUE) && torch::torch_is_installed() &&
requireNamespace("robCompositions", quietly = TRUE)) {
set.seed(1)
x <- data.frame(a = runif(50, 5, 10), b = runif(50, 5, 10), c = runif(50, 5, 10))
x$a[1:5] <- 0
imp <- impNNetCoDa(x, dl = c(1, 1, 1), label = 0,
arch = deepimp_arch_small(), epochs = 5, seed = 1)
head(getImputed(imp))
}
Construct a deepimp object
Description
Low-level constructor for the object returned by impNNet(). Most users do
not call this directly; use getImputed() to read the completed data.
Usage
new_deepimp(data, imputed, arch, info = list())
Arguments
data |
data.frame with missing values (original input). |
imputed |
list of completed data.frames (one per imputation). |
arch |
a |
info |
list with training metadata (backend, vartypes, convergence, ...). |
Value
an object of class "deepimp".
See Also
Plot diagnostics of a deepimp object
Description
type = "convergence" plots the pooled standardised change of the imputed
cells per sweep (first replicate). type = "trace" plots, per incomplete
variable, the mean and SD of the imputed cells by sweep with one line per
replicate (as plot() of a mice::mids object); factors enter by their
integer codes. Well-mixed draw-mode chains show intertwined lines without a
trend.
Usage
## S3 method for class 'deepimp'
plot(x, type = c("convergence", "trace"), vars = NULL, ...)
Arguments
x |
a |
type |
|
vars |
for |
... |
further arguments passed to |
Value
x, invisibly.
Impute new data with the models of a deepimp object
Description
Imputes the missing values of new observations with the models fitted by
impNNet() with keep_models = TRUE, without retraining. Starting values
for the new rows come from the method of the fit (initialize) with the
first completed training data set as donors; then the chained sweeps run
with the stored models of each replicate, in the mode of the fit (point
imputation or draws). Each replicate of the fit gives one completed version
of newdata.
Usage
## S3 method for class 'deepimp'
predict(object, newdata, iterations = NULL, seed = NULL, ...)
Arguments
object |
a |
newdata |
a data.frame with the columns of the training data (by name).
Factor levels unknown to the fit are set to |
iterations |
number of sweeps over the new rows; default: the number used in the fit. |
seed |
optional integer seed for the draws. |
... |
unused. |
Value
a "deepimp" object for newdata, with info$predicted = TRUE.
See Also
Examples
if (requireNamespace("torch", quietly = TRUE) && torch::torch_is_installed()) {
data(sleep, package = "VIM")
fit <- impNNet(sleep[1:50, ], arch = deepimp_arch_small(), epochs = 5,
keep_models = TRUE, seed = 1)
new <- predict(fit, sleep[51:62, ])
getImputed(new)
}
Summarise a deepimp object
Description
Summarise a deepimp object
Usage
## S3 method for class 'deepimp'
summary(object, ...)
## S3 method for class 'summary.deepimp'
print(x, ...)
Arguments
object |
a |
... |
unused. |
x |
a |
Value
summary() returns an object of class "summary.deepimp": a list
with the dimensions, the mode ("point", "pmm", "normal" or
"coda"), the missing values per variable, the sweeps run and the
convergence flag per replicate, the epochs used per fit (summary
statistics) and the variables caught by a safeguard. print() shows it.