slideimp

library(slideimp)
set.seed(1234)

Comparing Methods on One Cross-Validation Split with tune_imp()

# 20 rows, 1000 columns, all columns have at least some NA
sim_obj <- sim_mat(n = 20, p = 1000, perc_col_na = 1)
obj <- sim_obj$input
obj[1:4, 1:4]
#>          feature1  feature2  feature3  feature4
#> sample1 0.3486482 0.7385414 0.4077444 0.1607935
#> sample2 0.5338935 0.4724364 0.9663621 0.3722729
#> sample3 0.7185848 0.7351035 0.6724479 0.3162537
#> sample4 0.1734418        NA 0.0000000 0.0000000
na_loc <- sample_na_loc(obj, n_cols = 200, n_rows = 5, n_reps = 5)
length(na_loc)
#> [1] 5
na_loc[[1]][1:6, ]
#>      row col
#> [1,]   1 154
#> [2,]   5 154
#> [3,]  12 154
#> [4,]  11 154
#> [5,]   2 154
#> [6,]  17 704
# This custom function imputes missing values with random normal values and takes
# `mean` and `sd` as params
rnorm_imp <- function(obj, mean, sd) {
  na <- is.na(obj)
  obj[na] <- rnorm(sum(na), mean = mean, sd = sd) # <- impute values with rnorm
  return(obj) # <- return an imputed object with the same dim as obj
}

pca_tune <- tune_imp(
  obj,
  .f = "pca_imp",
  na_loc = na_loc,
  parameters = data.frame(ncp = 10)
)
#> Tuning `pca_imp()`
#> Step 1/2: Resolving NA locations
#> Running mode: sequential
#> Step 2/2: Tuning

knn_tune <- tune_imp(
  obj,
  .f = "knn_imp",
  na_loc = na_loc,
  parameters = data.frame(k = 10)
)
#> Tuning `knn_imp()`
#> Step 1/2: Resolving NA locations
#> Running mode: sequential
#> Step 2/2: Tuning

rnorm_tune <- tune_imp(
  obj,
  .f = rnorm_imp,
  na_loc = na_loc,
  parameters = data.frame(mean = 0, sd = 1) # must match with arguments of `rnorm_imp`
)
#> Tuning a custom function
#> Step 1/2: Resolving NA locations
#> Running mode: sequential
#> Step 2/2: Tuning
mean(compute_metrics(pca_tune, metrics = "rmse")$.estimate)
#> [1] 0.2004882
mean(compute_metrics(knn_tune, metrics = "rmse")$.estimate)
#> [1] 0.1935046
mean(compute_metrics(rnorm_tune, metrics = "rmse")$.estimate)
#> [1] 1.131702

Group-Wise Imputation with group_imp()

sim_obj <- sim_mat(n = 20, p = 50, n_col_groups = 2)

# Matrix to be imputed
obj <- sim_obj$input
obj[1:5, 1:4]
#>          feature1  feature2  feature3  feature4
#> sample1 0.0000000        NA 0.2437860 0.0000000
#> sample2 0.9022484 0.6724700 0.5377050 0.8242256
#> sample3 1.0000000 0.6652907 0.3189869 1.0000000
#> sample4 0.8367542 0.1726599 0.2097971 0.4225234
#> sample5 0.4962462 0.6250307 0.4247322 0.6390391

# Metadata, i.e., which features belong to which group
meta <- sim_obj$col_group
meta[1:5, ]
#>    feature  group
#> 1 feature1 group1
#> 2 feature2 group2
#> 3 feature3 group1
#> 4 feature4 group1
#> 5 feature5 group1

# Put feature 1 in `group3`
meta[1, 2] <- "group3"
meta[1:5, ]
#>    feature  group
#> 1 feature1 group3
#> 2 feature2 group2
#> 3 feature3 group1
#> 4 feature4 group1
#> 5 feature5 group1
set.seed(1234)
group_imp_df <- prep_groups(colnames(obj), group = meta, min_group_size = 10)
group_imp_df$parameters <- list(list(k = 3), list(k = 4), list(k = 5))
group_imp_df
#> # slideimp table: 3 x 4
#>   group          feature             aux parameters
#>  group1 <character [25]> <character [0]> <list [1]>
#>  group2 <character [24]> <character [0]> <list [1]>
#>  group3  <character [1]> <character [9]> <list [1]>
knn_results <- group_imp(obj, group = group_imp_df, cores = 2, k = 10)
#> Imputing 3 groups using K-NN.
#> Running mode: threaded (2 cores)
print(knn_results, p = 4)
#> Method: group_imp (K-NN imputation)
#> Dimensions: 20 x 50
#> 
#>          feature1  feature2  feature3  feature4
#> sample1 0.0000000 0.2177326 0.2437860 0.0000000
#> sample2 0.9022484 0.6724700 0.5377050 0.8242256
#> sample3 1.0000000 0.6652907 0.3189869 1.0000000
#> sample4 0.8367542 0.1726599 0.2097971 0.4225234
#> sample5 0.4962462 0.6250307 0.4247322 0.6390391
#> sample6 0.5124205 0.7016570 0.1918216 0.5038718
#> # Showing 6 of 20 rows and 4 of 50 columns

Multiple Imputation of WGBS/EM-seq Data with mipca_flank()

set.seed(1234)
chr1_beta <- sim_mat(n = 30, p = 500)$input
location <- cumsum(sample(50:500, ncol(chr1_beta), replace = TRUE))
chr1_beta[1:4, 1:4]
#>          feature1  feature2   feature3  feature4
#> sample1 0.5409157 0.4077444 0.09981068 0.2434143
#> sample2 0.5224927 0.9663621 0.49824360 0.3923603
#> sample3 0.6355535        NA 1.00000000 1.0000000
#> sample4 0.0000000 0.0000000 0.08115191 0.2106358
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.
win <- mipca_flank_window(plan, targets[1])
tuned <- mipca_tune(win, rank = 1:3, transform = "logit_biwhiten", n_cols = 5, seed = 1)
tuned
#> si_tune: 3 ranks on a 30 x 12 matrix, holdout 20 cells over 5 columns
#>  rank    crps ess_p10  ess_ok rhat_max implied_nu implied_lo implied_hi time
#>     1 0.08725  224.08 0.03571    1.050      2.231      2.143      3.049    0
#>     2 0.10157   97.22 0.00000    1.132      2.435      2.281      4.789    0
#>     3 0.09657   87.45 0.00000    1.160      3.477      2.698      5.068    0
#>   next: mipca_fit(x, obj) fits the rank with the lowest crps.
#>         mipca_fit(x, obj, rank) fits another rank.
fit <- mipca_flank(
  plan,
  rank = tuned$rank[which.min(tuned$crps)],
  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
dim(as.matrix(fit, draw = 1))
#> [1] 30  3

mirror server hosted at Truenetwork, Russian Federation.