R/gen_designmat.R

Defines functions gen_designmat

#' Generates a design matrix to be used to generate zero-inflated
#' count data.
#'
#' @param n  size of data set to be generated
#' @param p  dimension of design matrix for count model
#' @param q  dimension of design matrix for zero inflation model
#' @param type  type of design matrix, options include "uniform", "stdnormal"
#' @param intercept  should output design matrices contain an intercept
#' @param designmat_basis  a list of two data.frame from which
#' simulated design matrices can be generated.  If non-null,
#' the first data.frame will be the basis of the count design matrix; the
#' second will be for the zero-inflation model design matrix.
#' Design matrices are generated by taking a random sample of rows with
#' replacement.
#' @param add_colnames  a list of length 2 to be used if design matrices are
#'                      supplied.  the first will contain names for X, and the
#'                      second will contain names for Z.
#' @return A list
#' @export
gen_designmat <- function(n, p=NULL, q=NULL, type="uniform", intercept=F,
                          designmat_basis=NULL, add_colnames=NULL) {
  if(is.null(p)){
    dim(designmat_basis[[1]])[2]
  }
  if(is.null(q)){
    dim(designmat_basis[[2]])[2]
  }
  if(type == "uniform"){
    x <- matrix(stats::runif(n*p, 0, 1), n, p)
    z <- matrix(stats::runif(n*q, 0, 1), n, q)
  }
  if(type == "stdnorm"){
    x <- matrix(stats::rnorm(n*p, 0, 1), n, p)
    z <- matrix(stats::rnorm(n*q, 0, 1), n, q)
  }
  if(!is.null(designmat_basis)){
    x_basis <- designmat_basis[[1]]
    z_basis <- designmat_basis[[2]]
    n_basis <- dim(z_basis)[1]
    p <- dim(x_basis)[2]
    q <- dim(z_basis)[2]
    sample_ind <- sample(1:n_basis, n, replace = T)
    x <- x_basis[sample_ind, ]
    z <- z_basis[sample_ind, ]
  }
  x <- cbind(rep(1, n), x)
  z <- cbind(rep(1, n), z)
  if(is.null(add_colnames)){
    colnames(x) <- paste("V", 0:p, sep="")
    colnames(z) <- paste("U", 0:q, sep="")
  }
  if(!is.null(add_colnames)){
    colnames(x) <- add_colnames[[1]]
    colnames(z) <- add_colnames[[2]]
  }
  out <- list(x=x, z=z)
}
daniel-conn17/scRNAzirf documentation built on Dec. 19, 2021, 8:05 p.m.