slideimp is a lightweight R package for fast K-NN and PCA imputation of missing values in high-dimensional numeric matrices (samples in rows, variables in columns). Its main functions:
group_imp(): group-wise K-NN or PCA imputation, wrappingknn_imp()andpca_imp()(e.g., by chromosome for Illumina DNAm microarrays such as 450K, EPIC, EPICv2, MSA).slide_imp(): sliding window K-NN or PCA imputation over columns ordered by position, wrappingknn_imp()andpca_imp(). Note: not intended for Illumina microarrays, usegroup_imp()instead.pca_imp(): optimized reimplementation ofmissMDA::imputePCA()for high-dimensional matrices. Supports a LOBPCG eigensolver that warm-starts each EM iteration from the previous eigenblock and search direction.knn_imp(): full K-NN imputation with RcppThread multi-threading. Supportssubsetimputation to restrict work to specific columns (e.g., clock CpGs) for very fast targeted imputation.mipca(): Bayesian PCA sampler that draws multiple imputations of the missing values, with the method of Audigier, Husson and Josse (2016). Recommended for whole-genome methylation data (WGBS, EM-seq), which are sparsely observed. See the multiple imputation article.mipca_flank(): runsmipca()on target CpGs.
Installation
The stable version of slideimp can be installed from CRAN using:
install.packages("slideimp")You can install the development version of slideimp with:
pak::pkg_install("hhp94/slideimp")You can install the optional slideimp.extra package (which provides lightweight Illumina manifests) with:
pak::pkg_install("hhp94/slideimp.extra")Citation
If you find slideimp helpful in your work, please cite our Bioinformatics paper:
@article{10.1093/bioinformatics/btag318,
author = {Pham, Hung and Lombroso, Adam P and Cevik, Esma Cansu and Taylor, Hugh S and O'Donnell, Kieran J},
title = {slideimp: efficient imputation of {DNA} methylation data},
journal = {Bioinformatics},
volume = {42},
number = {6},
pages = {btag318},
year = {2026},
doi = {10.1093/bioinformatics/btag318},
month = jun,
}PCA or K-NN imputation of Illumina DNAm microarrays
- This is a simulated beta matrix from the MSA microarray. CpGs are in columns.
dim(MSA_beta_matrix)
#> [1] 20 281797
MSA_beta_matrix[1:4, 1:4]
#> cg06185909_TC11 cg18975462_BC11 cg20516119_TC11 cg10149399_BC11
#> sample1 NA 0.5023435 0.3835431 NA
#> sample2 0.4907466 0.5095459 0.9025816 0.4313347
#> sample3 0.6885036 NA 0.7646753 0.4498772
#> sample4 0.0000000 0.0000000 NA 0.0000000Chromosome-wise imputation of Illumina microarrays can be performed with a single function call (requires the Illumina manifests, which the
slideimp.extrapackage provides).This example demonstrates PCA imputation. To use K-NN imputation instead, supply the
kargument.
library(slideimp.extra)
library(slideimp)
imputed <- group_imp(
obj = MSA_beta_matrix,
group = "MSA", # <- this feature requires the `slideimp.extra` package
ncp = 10, # <- change to `k` for K-NN imputation. Optimal value requires `tune_imp()`.
clamp = c(0, 1), # <- PCA imputation can generate values outside of `c(0, 1)`
.progress = FALSE # <- turn on to monitor progress of longer running jobs
)
#> Found cleaned manifest for 'MSA'
#> ! 1 group dropped: no feature matches a column name.
#> ℹ Dropped group: M
#> Imputing 25 groups using PCA.
#> Running mode: sequential
print(imputed, n = 4, p = 4)
#> Method: group_imp (PCA imputation)
#> Dimensions: 20 x 281797
#>
#> cg06185909_TC11 cg18975462_BC11 cg20516119_TC11 cg10149399_BC11
#> sample1 0.1517542 0.5023435 0.38354308 0.2067731
#> sample2 0.4907466 0.5095459 0.90258164 0.4313347
#> sample3 0.6885036 0.7339375 0.76467530 0.4498772
#> sample4 0.0000000 0.0000000 0.05230101 0.0000000
#> # Showing 4 of 20 rows and 4 of 281797 columnsTroubleshooting tips
-
group_imp()fails with unmapped probes: your matrix likely containssesame-generated control probes (prefixedctl_) or you picked the wrong manifest ("EPICv2"vs."EPICv2_deduped"). Check the manifest, remove"ctl"probes withobj <- obj[, !grepl("^ctl", colnames(obj))], or passallow_unmapped = TRUEto bypass. - Impute the MSA or EPICv2 data with the duplicated probes, then dedup with
slideimp.extra::dedup_matrix().
Performance tips
- For tips on the
solverchoice and LOBPCG warm-start, plusthreshold,scale, and mirai/BLAS threading, see Speeding up PCA imputation. - For PCA imputation of bounded data, i.e., DNA methylation beta values, use
clamp = c(0, 1)to restrict PCA-imputed values to the valid range. - For parallel PCA imputation,
group_imp()andtune_imp()pin BLAS to one thread on each mirai worker, so BLAS threads and workers do not compete for cores. Setpin_blas = FALSEto turn this off. On Windows with OpenBLAS, the pin does not reach every call. See Speeding up PCA imputation for the fix. - For faster PCA imputation on Windows machines, advanced users can replace R’s default reference BLAS with OpenBLAS. See this community guide.
- For parallel K-NN imputation, use the
coresargument, which uses RcppThread’sparallelFor, instead of mirai. If you only need clock CpGs, passsubsetto skip all other probes.
Extended Workflow
- Simulate DNA methylation (DNAm) microarray data from 2 chromosomes. All slideimp functions expect the input to be a double matrix (convert an integer matrix with
storage.mode(obj) <- "double") where variables are stored in the columns.
library(slideimp)
set.seed(1234)
sim_obj <- sim_mat(n = 20, p = 100, n_col_groups = 2)
# `input` holds the simulated matrix with missing data
obj <- sim_obj$input
# `col_group` holds the column-wise grouping
group_df <- sim_obj$col_group-
Tune the hyperparameters with
tune_imp(). This grid search evaluates these K-NN imputation options:- Number of nearest neighbors (
k): 5 or 20 - Inverse distance power (
dist_pow) for weighted averaging: 1 or 2 (higher values assign lower weights to more distant neighbors)
- Number of nearest neighbors (
The tuning runs on a subset of the data. It uses 10 repeats of cross-validation (
n_reps = 10). Each repeat masks 50 observed values (num_na = 50) and computes the RMSE and MAE. Use larger values for bothn_repsandnum_nain real analyses for more reliable error estimates.
knn_params <- expand.grid(k = c(5, 20), dist_pow = c(1, 2))
group2_columns <- subset(group_df, group == "group2")
group2_only <- obj[, group2_columns$feature]
tune_knn <- tune_imp(
group2_only,
parameters = knn_params,
.f = "knn_imp",
cores = 8, # <- only available for K-NN imputation
n_reps = 10,
num_na = 50
)
#> Tuning `knn_imp()`
#> Step 1/2: Resolving NA locations
#> Running mode: threaded (8 cores)
#> Step 2/2: Tuning- Calculate errors using
compute_metrics()or yardstick functions.
metrics <- compute_metrics(tune_knn)
aggregate(.estimate ~ k + dist_pow + .metric, data = metrics, FUN = mean)
#> k dist_pow .metric .estimate
#> 1 5 1 mae 0.1636596
#> 2 20 1 mae 0.1473074
#> 3 5 2 mae 0.1654782
#> 4 20 2 mae 0.1487407
#> 5 5 1 rmse 0.2098225
#> 6 20 1 rmse 0.1898989
#> 7 5 2 rmse 0.2120673
#> 8 20 2 rmse 0.1926341- For PCA imputation and custom functions, set up parallelization with
mirai::daemons(). See Speeding up PCA imputation for more guidance on PCA performance.
mirai::daemons(2) # 2 Cores
# PCA imputation.
pca_params <- data.frame(ncp = c(1, 5))
tune_pca <- tune_imp(obj, parameters = pca_params, .f = "pca_imp", n_reps = 10, num_na = 50)
mirai::daemons(0) # Close daemons- Then perform imputation with
group_imp()using the best parameters.
knn_group_results <- group_imp(obj, group = group_df, k = 20, dist_pow = 1, cores = 2)
#> Imputing 2 groups using K-NN.
#> Running mode: threaded (2 cores)
knn_group_results
#> Method: group_imp (K-NN imputation)
#> Dimensions: 20 x 100
#>
#> feature1 feature2 feature3 feature4 feature5 feature6
#> sample1 0.3486482 0.7385414 0.4077444 0.1607935 0.3924661 0.2434143
#> sample2 0.5338935 0.4724364 0.9663621 0.4788070 0.5061132 0.3923603
#> sample3 0.7185848 0.7351035 0.6724479 0.3162537 0.7634236 1.0000000
#> sample4 0.1734418 0.0000000 0.0000000 0.0000000 0.0000000 0.2106358
#> sample5 0.5388440 0.5306182 0.5685354 0.5383513 0.4680080 0.8518388
#> sample6 0.3768380 0.5570723 0.8764909 0.5276245 0.6722794 0.5740639
#> # Showing 6 of 20 rows and 6 of 100 columnsgroup_imp()runs PCA imputation in parallel on mirai daemons in the same way astune_imp().Alternatively, full matrix imputation can be performed using
knn_imp()orpca_imp().
Multiple imputation of WGBS/EM-seq data
- For WGBS/EM-seq data,
mipca_flank()draws multiple imputations of target CpGs, each fitted on the CpGs withinwindow_sizebp of it. Split the data by chromosome first. - This simulated beta matrix stands in for one chromosome, with 50 to 500 bp between neighboring CpGs.
set.seed(1234)
chr1_beta <- sim_mat(n = 30, p = 500, beta = TRUE)$input
location <- cumsum(sample(50:500, ncol(chr1_beta), replace = TRUE))- First, find the window of each target with
mipca_flank_plan().
targets <- colnames(chr1_beta)[c(100, 250, 400)]
plan <- mipca_flank_plan(
chr1_beta,
location,
window_size = 2000, # bp on each side of the target
subset = targets,
min_window_n = 10
)
plan
#> si_flank_plan: 3 targets on a 30 x 500 matrix, nothing fitted
#> status: impute 2, clean 1
#> target name n_miss start end window_n status
#> 100 feature100 6 94 105 12 impute
#> 250 feature250 0 244 256 13 clean
#> 400 feature400 6 393 406 14 impute
#> next: mipca_flank(x, rank, seed, transform) imputes.
#> mipca_flank_window(x, subset) gives one target's window to tune on.- Then fit each window and keep the targets’ draws. Choose
rankwithmipca_tune()on one target’s window.
fit <- mipca_flank(plan, rank = 2, seed = 1, transform = "logit_biwhiten", .progress = FALSE)
fit
#> si_mi fit: 30 x 3, 12 imputed cells, 400 draws
#> next: as.matrix() with 'draw' for a completed matrix, summary() for Rhat and ESS- See the multiple imputation article for tuning, convergence checks, and pooling the draws.
Other functions
-
col_vars(): Column-wise variance, skipping missing values. -
mat_miss(): Efficient column or row missing statistics.