I am new to R programming and wanted to know how to run in parallel plot
on 12 trellis objects made with lattice
package.
Basically, after a lot of pre-processing steps, I have the following commands:
plot(adhd_plot, split = c(1,1,4,3)) #plot adhd trellis object at 1,1 in a grid of 4 by 3 i.e 4 COLUMNS x 3 ROWS
plot(bpd_plot, split = c(2,1,4,3), newpage = F) #plot bpd trellis object in 2nd Column in a grid of 4colx3row
plot(bmi_plot, split = c(3,1,4,3), newpage = F)
plot(dbp_plot, split = c(4,1,4,3), newpage = F)
plot(height_plot, split = c(1,2,4,3), newpage = F)
plot(hdl_plot, split = c(2,2,4,3), newpage = F)
plot(ldl_plot, split = c(3,2,4,3), newpage = F)
plot(ra_plot, split = c(4,2,4,3), newpage = F)
plot(sbp_plot, split = c(1,3,4,3), newpage = F)
plot(scz_plot, split = c(2,3,4,3), newpage = F)
plot(tc_plot, split = c(3,3,4,3), newpage = F)
plot(tg_plot, split = c(4,3,4,3), newpage = F)
The issue is that while the above commands work, they take really long (>4hrs) on Mac OSX to produce a figure like the following :
Since my Mac has 8 cores, I thought I should try to split up the plot command across the different cores so as to speed up the plotting.
After searching across other parallelization questions, I found the doParallel
package and thought I could potentially implement the parLapply
function in it like as follows:
library(doParallel)
detectCores()
cl <- makeCluster(6) #6 out of 8 cores
registerdoParallel(cl)
parLapply(cl, list_of_all_trellis_objects, plot)
However, I am not sure how to use the split
parameter in the above parLapply
command to place the plots in different locations on the grid.
I necessarily need the 12 plots placed separately and not superimposed, so how to do that?
Thank you for going through my query and I look forward to your hints and solutions .
As suggested in comments, there is no way to write to plotting device in parallel.
Some workarounds to speed up drawing of individual plots:
Reduce the number of points in the QQ plot, see:
https://stats.stackexchange.com/questions/35220/removing-extraneous-points-near-the-centre-of-a-qq-plot
Load data faster by applying these tips:
http://cbio.ensmp.fr/~thocking/reading-large-text-files-into-R.html
You could try to draw/save multiple plots in parallel (where each plot uses methods from point 1 and 2), but writing to disk may cause significant bottleneck.
Edit:
Here is rough code to draw fast qq-plot:
https://github.com/vforget/fastqq
Code below:
find_conf_intervals = function(row){
i = row[1]
len = row[2]
if (i < 10000 | i %% 100 == 0){
return(c(-log10(qbeta(0.95,i,len-i+1)), -log10(qbeta(0.05,i,len-i+1))))
} else { # Speed up
return(c(NA,NA))
}
}
confidence.intervals <- function(e){
xspace = 0.078
print("1")
ci = apply(cbind( 1:length(e), rep(length(e),length(e))), MARGIN=1, FUN=find_conf_intervals)
print("2")
bks = append(seq(10000,length(e),100),length(e)+1)
print("3")
for (i in 1:(length(bks)-1)){
ci[1, bks[i]:(bks[i+1]-1)] = ci[1, bks[i]]
ci[2, bks[i]:(bks[i+1]-1)] = ci[2, bks[i]]
}
colnames(ci) = names(e)
## Extrapolate to make plotting prettier (doesn't affect intepretation at data points)
slopes = c((ci[1,1] - ci[1,2]) / (e[1] - e[2]), (ci[2,1] - ci[2,2]) / (e[1] - e[2]))
print("4")
extrap_x = append(e[1]+xspace,e) ## extrapolate slightly for plotting purposes only
extrap_y = cbind( c(ci[1,1] + slopes[1]*xspace, ci[2,1] + slopes[2]*xspace), ci)
print("5")
polygon(c(extrap_x, rev(extrap_x)), c(extrap_y[1,], rev(extrap_y[2,])),
col = "grey81", border = "grey81")
}
quant.subsample <- function(y, m=100, e=1) {
## m: size of a systematic sample
## e: number of extreme values at either end to use
x <- sort(y)
n <- length(x)
quants <- (1 + sin(1:m / (m+1) * pi - pi/2))/2
sort(c(x[1:e], quantile(x, probs=quants), x[(n+1-e):n]))
## Returns m + 2*e sorted values from the EDF of y
}
get.points <- function(pv) {
suppressWarnings(as.numeric(pv))
names(d) = names(pv)
d = d[!is.na(d)]
d = d[d>0 & d<1]
d = d[order(d,decreasing=F)]
y = -log10(d)
x = -log10( ppoints(length(d) ))
m <- 0.001 * length(x)
e <- floor(0.0005 * length(x))
return(list(x=quant.subsample(x, m, e), y=quant.subsample(y, m, e)))
}
fqq <- function(x, y, ...) {
plot(0,
col=FALSE,
xlim=range(x),
ylim=range(y),
xlab=expression(Expected~~-log[10](italic(p))),
ylab=expression(Observed~~-log[10](italic(p))),
...)
abline(0,1,col=2)
points(x,y, ...)
}
args <- commandArgs(trailingOnly = TRUE)
pv.f = args[1]
qq.f = args[2]
nrows = as.numeric(args[3])
message(Sys.time())
message("READING")
d <- read.table(pv.f, header=TRUE, sep=" ", nrows=nrows, colClasses=c("numeric"))
message(Sys.time())
message("LAMBDA")
chisq <- qchisq(1-d$P_VAL,1)
lambda = median(chisq)/qchisq(0.5,1)
message(Sys.time())
message("PLOTING")
p <- get.points(d$P_VAL)
png(file=qq.f)
fqq(p$x, p$y, main=paste(pv.f, lambda, sep="\n"), cex.axis=1.5, cex.lab=1.5)
dev.off()
message(Sys.time())