Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions R/predictRTConfModels.R
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ predictConfModels <- function(paramDf,
### Determine number of jobs, i.e. model-participant-combinations
nJobs <- nrow(paramDf)

call_predfct <- function(X) {
cur_model <- models[X[1]]
cur_sbj <- X[2]
call_predfct <- function(job) {
cur_model <- models[as.integer(job$model)]
cur_sbj <- job$sbj
sbj <- NULL # to omit a note because of an unbound variable
model <- NULL # to omit a note because of an unbound variable
params <- subset(paramDf, sbj==cur_sbj & model==cur_model)
Expand All @@ -184,13 +184,13 @@ predictConfModels <- function(paramDf,
return(res)
}

jobs <- expand.grid(model=1:length(models), sbj=subjects)
jobs <- expand.grid(model=seq_along(models), sbj=subjects, stringsAsFactors = FALSE)
if (nrow(jobs) < nJobs) stop("model and participant don't produce distinct rows!\nThere should be only one row per participant and model combination")

if (parallel) {
listjobs <- list()
for (i in 1:nrow(jobs)) {
listjobs[[i]] <- c(model = jobs[["model"]][i], sbj = jobs[["sbj"]][i])
for (i in seq_len(nrow(jobs))) {
listjobs[[i]] <- list(model = jobs[["model"]][i], sbj = jobs[["sbj"]][i])
}
if (is.null(n.cores)) {
n.cores <- detectCores()-1
Expand All @@ -205,12 +205,13 @@ predictConfModels <- function(paramDf,
res <- clusterApplyLB(cl, listjobs, fun=call_predfct)
stopCluster(cl)
} else {
res <- apply(jobs, 1, call_predfct)
res <- lapply(seq_len(nrow(jobs)), function(i) call_predfct(list(model = jobs$model[i], sbj = jobs$sbj[i])))
}
res <- do.call(rbind, res)

# Put sbj and model column to the front
res[,c(ncol(res),(ncol(res)-1), 1:(ncol(res)-2))]
leading_cols <- c(sbjcol, "model")
res <- res[, c(leading_cols, setdiff(names(res), leading_cols))]
return(res)
}

Expand Down Expand Up @@ -290,9 +291,9 @@ predictRTModels <- function(paramDf,
minrt <- min(minrt, pars_diffmodels$t0+pars_diffmodels$tau)
}
}
call_predfct <- function(X) {
cur_model <- models[X[1]]
cur_sbj <- X[2]
call_predfct <- function(job) {
cur_model <- models[as.integer(job$model)]
cur_sbj <- job$sbj
sbj <- NULL # to omit a note because of an unbound variable
model <- NULL # to omit a note because of an unbound variable
params <- subset(paramDf, sbj==cur_sbj & model==cur_model)
Expand All @@ -310,13 +311,13 @@ predictRTModels <- function(paramDf,
res[[sbjcol]] <- cur_sbj
return(res)
}
jobs <- expand.grid(model=1:length(models), sbj=subjects)
jobs <- expand.grid(model=seq_along(models), sbj=subjects, stringsAsFactors = FALSE)
if (nrow(jobs) < nJobs) stop("model and participant don't produce distinct rows!\nThere should be only one row per participant and model combination")

if (parallel) {
listjobs <- list()
for (i in 1:nrow(jobs)) {
listjobs[[i]] <- c(model = jobs[["model"]][i], sbj = jobs[["sbj"]][i])
for (i in seq_len(nrow(jobs))) {
listjobs[[i]] <- list(model = jobs[["model"]][i], sbj = jobs[["sbj"]][i])
}
cl <- makeCluster(type="SOCK", n.cores)
clusterExport(cl, c("paramDf", "sbjcol", "models",
Expand All @@ -327,11 +328,12 @@ predictRTModels <- function(paramDf,
res <- clusterApplyLB(cl, listjobs, fun=call_predfct)
stopCluster(cl)
} else {
res <- apply(jobs, 1, call_predfct)
res <- lapply(seq_len(nrow(jobs)), function(i) call_predfct(list(model = jobs$model[i], sbj = jobs$sbj[i])))
}
res <- do.call(rbind, res)

# Put sbj and model column to the front
res[,c(ncol(res),(ncol(res)-1), 1:(ncol(res)-2))]
leading_cols <- c(sbjcol, "model")
res <- res[, c(leading_cols, setdiff(names(res), leading_cols))]
return(res)
}