r - missing output from foreach -



r - missing output from foreach -

if create simple model, foreach returns every result way think should:

m=function(i,j){data.frame(i=i,j=j)} > foreach(i=1:2, .combine='rbind') %:% foreach(j=1:2, .combine='rbind') %dopar%{ + m(i,j) + } j 1 1 1 2 1 2 3 2 1 4 2 2

but using more complicated function misses first loop:

# loop through prediction model (in parallel) different parameters results = foreach(i=1:2, .combine='rbind') %:% foreach(j=1:2, .combine='rbind') %dopar%{ model(i,j) } > results j tpr fpr rj day 1 1 2 0 0.2127812 1.022387 wed oct 29 11:53:45 2014 2 2 1 0 0.2161888 1.023102 wed oct 29 11:54:41 2014 3 2 2 0 0.2127812 1.022387 wed oct 29 11:53:45 2014

you might assume function generating error when i=1,j=1, running function outside foreach loop gives result:

> model(1,1) j tpr fpr rj day 1 1 1 0 0.2161888 1.023102 wed oct 29 12:30:31 2014

so assume i've set foreach iterators wrong. problem more obvious me.

edit: works if %dopar% replaced %do%. of course, solution defeats purpose of using foreach.

i assume "model" returning different result when executed worker, perchance because worker wasn't initialized correctly.

to test theory, add together additional test code:

results <- foreach(i=1:2, .combine='rbind') %:% foreach(j=1:2, .combine='rbind') %dopar% { x <- model(i,j) stopifnot(! is.null(x)) stopifnot(nrow(x) == 1) x }

if "model" returns value, theory may correct, although it's hard imagine how "model" cause problem iteration variables.

r foreach parallel-processing

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -