Simulate a raw count matrix with no cluster structure.
Split the counts evenly (epsilon = 0.5) by drawing from
a Binomial distribution where the size parameter is the
original raw count.
epsilon <- 0.5
train_counts <- matrix(
rbinom(n = length(raw_counts), size = raw_counts, prob = epsilon),
nrow = nrow(raw_counts),
ncol = ncol(raw_counts)
)
# Assign gene and sample names to the training set
rownames(train_counts) <- rownames(raw_counts)
colnames(train_counts) <- colnames(raw_counts)
# Define the test matrix as the remainder of the counts
test_counts <- raw_counts - train_countsUse the train_counts to perform unsupervised learning
without “using up” our data for downstream inference. Here, we transpose
train_counts to cluster the samples (columns) into 2 groups
using K-means.
Perform differential abundance analysis on test_counts
using clusters discovered from training_counts without
“double dipping.”
p_values <- apply(test_counts, 1, function(x) {
t.test(x ~ clusters)$p.value
})
# Visualize the distribution of p-values
hist(p_values, main = "Histogram of p-values", xlab = "p-value", col = "lightblue")