2

私は R プログラミングが初めてで、パッケージで作成された12 個のトレリス オブジェクトplotで並列に実行する方法を知りたいと思っていました。lattice

基本的に、多くの前処理手順の後、次のコマンドがあります。

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) 

問題は、上記のコマンドは機能しますが、Mac OSX では次のような図を生成するのに非常に長い(>4 時間)かかることです。

ここに画像の説明を入力 私の Mac には 8 つのコアがあるので、プロット コマンドをコアごとに分割して、プロットを高速化する必要があると考えました。

他の並列化に関する質問を検索した後、パッケージを見つけ、次のように関数をdoParallel実装できる可能性があると考え ました。parLapply

library(doParallel)
detectCores()
cl <- makeCluster(6) #6 out of 8 cores
registerdoParallel(cl)
parLapply(cl, list_of_all_trellis_objects, plot)

ただし、split上記のコマンドでパラメーターを使用しparLapplyて、プロットをグリッド上の別の場所に配置する方法がわかりません。

私は必然的に12個のプロットを別々に配置し、重ね合わせる必要はありません。

私の質問に答えていただきありがとうございます。あなたのヒントと解決策を楽しみにしています。

4

1 に答える 1

1

コメントで示唆されているように、プロッティングデバイスに並行して書き込む方法はありません。

個々のプロットの描画を高速化するためのいくつかの回避策:

  1. QQ プロットのポイント数を減らします。以下を参照してください。

    https://stats.stackexchange.com/questions/35220/removing-extraneous-points-near-the-centre-of-a-qq-plot

  2. 次のヒントを適用して、データをより速くロードします。

    http://cbio.ensmp.fr/~thocking/reading-large-text-files-into-R.html

  3. 複数のプロットを並行して描画/保存しようとすることはできますが (各プロットはポイント 1 と 2 の方法を使用します)、ディスクへの書き込みが重大なボトルネックを引き起こす可能性があります。

編集:

高速な qq-plot を描画する大まかなコードは次のとおりです。

https://github.com/vforget/fastqq

以下のコード:

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())
于 2015-09-15T15:46:12.410 に答える