# NOT RUN {
# Load example dataset:
data(autism)
# Subset this example dataset to reduce the
# computational burden of the toy examples:
# Random subset of 150 variables:
set.seed(1234)
Xsub <- X[,sample(1:ncol(X), size=150)]
# In cases of batches with more than 20 observations
# select 20 observations at random:
subinds <- unlist(sapply(1:length(levels(batch)), function(x) {
indbatch <- which(batch==x)
if(length(indbatch) > 20)
indbatch <- sort(sample(indbatch, size=20))
indbatch
}))
Xsub <- Xsub[subinds,]
batchsub <- batch[subinds]
ysub <- y[subinds]
# Split into training and test sets:
trainind <- which(batchsub %in% c(1,2))
Xsubtrain <- Xsub[trainind,]
ysubtrain <- ysub[trainind]
batchsubtrain <- factor(as.numeric(batchsub[trainind]), levels=c(1,2))
testind <- which(batchsub %in% c(3,4))
Xsubtest <- Xsub[testind,]
ysubtest <- ysub[testind]
batchsubtest <- as.numeric(batchsub[testind])
batchsubtest[batchsubtest==3] <- 1
batchsubtest[batchsubtest==4] <- 2
batchsubtest <- factor(batchsubtest, levels=c(1,2))
# Batch effect adjustment:
combatparams <- ba(x=Xsubtrain, y=ysubtrain, batch=batchsubtrain,
method = "combat")
Xsubtraincombat <- combatparams$xadj
meancenterparams <- ba(x=Xsubtrain, y=ysubtrain, batch=batchsubtrain,
method = "meancenter")
Xsubtrainmeancenter <- meancenterparams$xadj
# Addon batch effect adjustment:
Xsubtestcombat <- baaddon(params=combatparams, x=Xsubtest,
batch=batchsubtest)
Xsubtestmeancenter <- baaddon(params=meancenterparams, x=Xsubtest,
batch=batchsubtest)
# Metrics for evaluating the success of batch effect adjustment:
bametric(xba=Xsubtrain, batch=batchsubtrain, metric = "sep")
bametric(xba=Xsubtrainmeancenter, batch=batchsubtrain, metric = "sep")
bametric(x=Xsubtrain, batch=batchsubtrain, y=ysubtrain,
metric = "diffexpr", method = "meancenter")
bametric(xba=Xsubtrainmeancenter, x=Xsubtrain, metric = "cor")
# Principal component analysis plots for the visualization
# of batch effects:
par(mfrow=c(1,3))
pcplot(x=Xsub, batch=batchsub, y=ysub, alpha=0.25, main="alpha = 0.25")
pcplot(x=Xsub, batch=batchsub, y=ysub, alpha=0.75, main="alpha = 0.75")
pcplot(x=Xsub, batch=batchsub, y=ysub, col=1:length(unique(batchsub)),
main="col = 1:length(unique(batchsub))")
par(mfrow=c(1,1))
# (Addon) quantile normalization:
qunormparams <- qunormtrain(x=Xsubtrain)
Xtrainnorm <- qunormparams$xnorm
Xtestaddonnorm <- qunormaddon(qunormparams, x=Xsubtest)
# }
Run the code above in your browser using DataLab