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 version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.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.0.0      limma_3.58.0     tidyr_1.3.0     
## [5] stringr_1.5.0    dplyr_1.1.3      BiocStyle_2.30.0
## 
## loaded via a namespace (and not attached):
##  [1] sass_0.4.7          utf8_1.2.4          generics_0.1.3     
##  [4] shape_1.4.6         stringi_1.7.12      lattice_0.22-5     
##  [7] lme4_1.1-34         mitml_0.4-5         digest_0.6.33      
## [10] magrittr_2.0.3      evaluate_0.22       grid_4.3.1         
## [13] bookdown_0.36       iterators_1.0.14    fastmap_1.1.1      
## [16] jomo_2.7-6          foreach_1.5.2       jsonlite_1.8.7     
## [19] glmnet_4.1-8        Matrix_1.6-1.1      nnet_7.3-19        
## [22] backports_1.4.1     survival_3.5-7      BiocManager_1.30.22
## [25] purrr_1.0.2         fansi_1.0.5         codetools_0.2-19   
## [28] jquerylib_0.1.4     cli_3.6.1           rlang_1.1.1        
## [31] splines_4.3.1       withr_2.5.1         cachem_1.0.8       
## [34] yaml_2.3.7          pan_1.9             tools_4.3.1        
## [37] nloptr_2.0.3        minqa_1.2.6         locfit_1.5-9.8     
## [40] boot_1.3-28.1       broom_1.0.5         rpart_4.1.21       
## [43] vctrs_0.6.4         R6_2.5.1            lifecycle_1.0.3    
## [46] MASS_7.3-60         pkgconfig_2.0.3     pillar_1.9.0       
## [49] bslib_0.5.1         glue_1.6.2          Rcpp_1.0.11        
## [52] statmod_1.5.0       xfun_0.40           tibble_3.2.1       
## [55] tidyselect_1.2.0    knitr_1.44          nlme_3.1-163       
## [58] htmltools_0.5.6.1   rmarkdown_2.25      compiler_4.3.1