#' @title Estimation from scDesign2
#' @description Use scDesign2 as the generative model and generate data.
#' @param x_pilot input variables of pilot data
#' @param y_pilot labels of pilot data
#' @param n0_train the number of training data of class 0
#' @param n1_train the number of training data of class 1
#' @param n0_test the number of test data of class 0
#' @param n1_test the number of test data of class 1
#'
#' @return the generated data list including train_x, train_y and test_x
#' @export
#'
#' @examples
#' data_mat <- readRDS(system.file("extdata", "mouse_sie_10x.rds", package = "scDesign2"))
#' nonspikes <- which(!grepl("ercc", rownames(data_mat), ignore.case = TRUE))
#' data_mat <- data_mat[nonspikes, ,drop = FALSE]
#' dim(data_mat)
#' table(colnames(data_mat))
#' needed_cell_type <- c("Enterocyte.Progenitor.Early","Stem")
#'
#' set.seed(1)
#' all_idx <- unlist(sapply(needed_cell_type, function(x){
#' cell_type_idx <- which(colnames(data_mat) == x)
#' n_cell_total <- length(cell_type_idx)
#' sample(cell_type_idx, 800)
#' }))
#' allcount <- data_mat[, all_idx]
#' x_data = t(allcount)
#' y_data = c(rep(0,800),rep(1,800))
#' data = list(x_data=x_data, y_data=y_data)
#'
#' pilot_rest_data = split_data(data$x_data, data$y_data, n_train=15, seed=1)
#'
#' data_list = pilot_tfe_scDesign2(pilot_rest_data$x_train,pilot_rest_data$y_train,n0_train=60,n1_train=60,n0_test=300,n1_test=300)
#'
pilot_tfe_scDesign2 <- function(x_pilot,y_pilot,n0_train,n1_train,n0_test,n1_test, copula_model_0=NULL, copula_model_1=NULL)
{
library(scDesign2)
library(MASS)
library(rlist)
id0_p = which(y_pilot==0)
id1_p = which(y_pilot==1)
n0_p = length(id0_p)
n1_p = length(id1_p)
num_feature = length(x_pilot[id0_p[1],])
x0_pilot = x_pilot[id0_p,]
x1_pilot = x_pilot[id1_p,]
rownames(x0_pilot) = c(rep("0",n0_p))
rownames(x1_pilot) = c(rep("1",n1_p))
###generate the samples based on the estimated distributions
n_train=n0_train+n1_train
n_test=n0_test+n1_test
###y
train_y=c(rep(0,n0_train),rep(1,n1_train))
###train
if(is.null(copula_model_0) || is.null(copula_model_1)){
copula_model_0 <- fit_model_scDesign2(t(x0_pilot), '0', sim_method = 'copula')
copula_model_1 <- fit_model_scDesign2(t(x1_pilot), '1', sim_method = 'copula')
}
train_x0 <- simulate_count_scDesign2(copula_model_0, n0_train, sim_method = 'copula')
train_x1 <- simulate_count_scDesign2(copula_model_1, n1_train, sim_method = 'copula')
trx=matrix(rbind(t(train_x0),t(train_x1)),n_train,num_feature)
train_x=as.data.frame(trx)
###test
test_x0 <- simulate_count_scDesign2(copula_model_0, n0_test, sim_method = 'copula')
test_x1 <- simulate_count_scDesign2(copula_model_1, n1_test, sim_method = 'copula')
tex=matrix(rbind(t(test_x0),t(test_x1)),n_test,num_feature)
test_x=as.data.frame(tex)
data_list<-list(train_x=train_x,train_y=train_y,test_x=test_x)
return(data_list)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.