Contents

1 Generate random data

Normally you would have your own covariate and RNA-sequencing data. We generate random data below for the purpose of demonstrating how the functions in the RNAseqCovarImpute package work.

library(dplyr)
library(stringr)
library(tidyr)
library(edgeR)
library(mice)
# Generate random covariate data
set.seed(2023)
x1 <- rnorm(n = 500)
y1 <- rnorm(n = 500)
z1 <- rnorm(n = 500)
a1 <- rbinom(n = 500, prob = .25, size = 1)
b1 <- rbinom(n = 500, prob = .5, size = 1)
data <- data.frame(x = x1, y = y1, z = z1, a = a1, b = b1)

# Generate random count data from Poisson distribution
nsamp <- nrow(data)
ngene <- 500
mat <- matrix(stats::rpois(n = nsamp * ngene, lambda = sample(1:500, ngene, replace = TRUE)),
    nrow = ngene,
    ncol = nsamp
)

# Make fake ENSEMBL gene numbers
annot <- tibble(number = seq_len(ngene), name1 = "ENS") %>%
    mutate(ENSEMBL = str_c(name1, number)) %>%
    dplyr::select(ENSEMBL)
rownames(mat) <- annot$ENSEMBL

# Make DGE list and set rownames to ENSEMBL gene numbers above
example_DGE <- DGEList(mat, genes = annot)
rownames(example_DGE) <- annot$ENSEMBL

2 Simulate missingness in the random data

Now we simulate missingness in the random data generated above using the mice ampute function. The below code simulates missingness in the data for all but the first variable (x), which we consider the main predictor of interest in this example. See the ampute documentation for more details.

# First get all combos of 0 or 1 for the 4 other variables (y, z, a, and b)
pattern_vars <- expand.grid(c(0, 1), c(0, 1), c(0, 1), c(0, 1))
# Then add back a column for the predictor of interest, which is never amputed, so the first col =1 the whole way down
pattern2 <- matrix(1, nrow = nrow(pattern_vars), ncol = 1)
pattern1 <- cbind(pattern2, pattern_vars)
# Remove last row which is all 1s (all 1s means no missingness induced)
pattern <- pattern1[seq_len(15), ]

# Specify proportion of missing data and missingness mechanism for the ampute function
prop_miss <- 25
miss_mech <- "MAR"
result <- ampute(data = data, prop = prop_miss, mech = miss_mech, patterns = pattern)

# Extract the missing data
example_data <- result$amp
# Ampute function turns factors to numeric, so convert back to factors
example_data <- example_data %>% mutate(
    a = as.factor(a),
    b = as.factor(b)
)

# Calculate the new sample size if NAs are dropped as in a complete case analysis
sample_size <- example_data %>%
    drop_na() %>%
    nrow()

# As shown below, we have 24.2% missingness.
100 * (nsamp - sample_size) / nsamp
## [1] 24.2

Table 1: The first 10 rows of simulated covariate data with missingness
x y z a b
-0.084 1.179 -0.269 0 0
-0.983 0.795 -0.119 0 0
-1.875 -0.820 0.065 0 1
-0.186 2.238 1.291 0 0
-0.633 1.552 -0.451 0 1
1.091 NA NA NA 1
-0.914 0.951 0.853 0 1
1.002 -2.007 1.766 1 NA
-0.399 0.900 0.640 NA 1
-0.468 0.716 0.705 0 1

Session info

## R Under development (unstable) (2024-10-21 r87258)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 24.04.1 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.21-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.12.0
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_GB              LC_COLLATE=C              
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] mice_3.16.0      edgeR_4.5.0      limma_3.63.0     tidyr_1.3.1     
## [5] stringr_1.5.1    dplyr_1.1.4      BiocStyle_2.35.0
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.9          utf8_1.2.4          generics_0.1.3     
##  [4] shape_1.4.6.1       stringi_1.8.4       lattice_0.22-6     
##  [7] lme4_1.1-35.5       mitml_0.4-5         digest_0.6.37      
## [10] magrittr_2.0.3      evaluate_1.0.1      grid_4.5.0         
## [13] bookdown_0.41       iterators_1.0.14    fastmap_1.2.0      
## [16] jomo_2.7-6          foreach_1.5.2       jsonlite_1.8.9     
## [19] glmnet_4.1-8        Matrix_1.7-1        nnet_7.3-19        
## [22] backports_1.5.0     survival_3.7-0      BiocManager_1.30.25
## [25] purrr_1.0.2         fansi_1.0.6         codetools_0.2-20   
## [28] jquerylib_0.1.4     cli_3.6.3           rlang_1.1.4        
## [31] splines_4.5.0       withr_3.0.2         cachem_1.1.0       
## [34] yaml_2.3.10         pan_1.9             tools_4.5.0        
## [37] nloptr_2.1.1        minqa_1.2.8         locfit_1.5-9.10    
## [40] boot_1.3-31         broom_1.0.7         rpart_4.1.23       
## [43] vctrs_0.6.5         R6_2.5.1            lifecycle_1.0.4    
## [46] MASS_7.3-61         pkgconfig_2.0.3     pillar_1.9.0       
## [49] bslib_0.8.0         glue_1.8.0          Rcpp_1.0.13        
## [52] statmod_1.5.0       xfun_0.48           tibble_3.2.1       
## [55] tidyselect_1.2.1    knitr_1.48          nlme_3.1-166       
## [58] htmltools_0.5.8.1   rmarkdown_2.28      compiler_4.5.0