尝试使用大数据集的应用函数替换R中的循环

时间:2014-04-09 18:30:02

标签: r loops apply lapply replicate

我正在尝试对大量参数组合执行简单计算。我有15,625个排列,并希望为每个组合运行蒙特卡罗实验(~5000)。我的问题是正确存储数据并避免永远占用的循环。我想使用应用功能,但无法弄清楚。我有以下代码运行,但效率非常低!我有兴趣保存" res [i,j]"值。我已经看到一个简单的方法来做蒙特卡洛正在使用复制命令...但很明显我还没有.... 任何建议都会非常感激!!

#run the beta function
beta <- function(M) {
  b_slope <- log(M) / 10
  return (b_slope)
}
#set the experiment conditions for looping through different M, Cv, and q parameter vals
cvVals <- seq(0.1,3.09,0.12)
mVals <- seq(1,2.98,0.08)
qVals <- seq(0.9,0.999,0.004)
mNum <- length(mVals);cvNum <- length(cvVals);qNum<-length(qVals);
total<-mNum*cvNum*qNum

#iterate through time (up to 5000 yrs)
imax<-5000

#Number of experiments
expts<-5

#fill a matrix with each combination of cv, m, q values
df <- data.frame(expand.grid(cv=cvVals, m=mVals, q=qVals))

#set a column in the df to have X_Crit values
df$i<-seq(1:nrow(df))
df$X_crit <- qlnorm(df$q)

#store the results in a df with the dimensions of df by # of experiments
res <- data.frame(nrow=nrow(df), ncol=expts)

for (i in 1:nrow(df)) {

  for (j in 1:ncol(res)) {
    #fill in all the x_critical values for each q
    X_crit <- df$X_crit[i]

    #compute the mean and std dev and flow for all values up to imax
    tempmean <- beta(df$m[i])*seq(0, imax-1)
    tempstd <- df$cv[i]*tempmean
    #generate imax random lognorm variables as error terms 
    err <- rlnorm(imax, 0, 1)
    #compute flow from lognormal quantile function
    flow <- tempmean + tempstd*err

    #store the result which looks for the first exceedance of flow 
    if (sum(flow>X_crit)>0) {
      res[i, j] <-min(which(flow > X_crit))
    } else {
      res[i,j] <- imax
    }

  }

}

1 个答案:

答案 0 :(得分:0)

删除for j循环。它似乎没有任何用处