[R] ...... looping
Martin Maechler
maechler at stat.math.ethz.ch
Wed Oct 25 16:08:53 CEST 2006
>>>>> "BaRow" == Barry Rowlingson <B.Rowlingson at lancaster.ac.uk>
>>>>> on Wed, 25 Oct 2006 11:06:06 +0100 writes:
BaRow> Xiaofan Cao wrote:
>> Hi there,
>>
>> I'm writing a program in R that has a few nested loops. I'd like to
>> monitor the progress when the program is running and be able to estimate
>> the remaining time.
BaRow> A long time ago I started writing some code to give R something like
BaRow> an 'iterator' object. You could do this:
>> ml=loop(5)
>> while(iterate(ml))
BaRow> + {cat("doing ",iteration(ml)," of ",N(ml),"\n","Ending at
BaRow> ",predictEnd(ml),"\n");sleep(5)}
BaRow> doing 1 of 5
BaRow> Ending at Wed 25 Oct 2006 11:00:05 BST
BaRow> doing 2 of 5
BaRow> Ending at Wed 25 Oct 2006 11:00:20 BST
BaRow> doing 3 of 5
BaRow> Ending at Wed 25 Oct 2006 11:00:20 BST
BaRow> doing 4 of 5
BaRow> Ending at Wed 25 Oct 2006 11:00:20 BST
BaRow> doing 5 of 5
BaRow> Ending at Wed 25 Oct 2006 11:00:20 BST
BaRow> you use loop(N) to construct a 1:N loop object, while(iterate(ml)) to
BaRow> loop round it, iteration(ml) to get the current iteration number, N(ml)
BaRow> to get the iteration limit, and predictEnd(ml) to guess when the whole
BaRow> thing will finish.
BaRow> All the information about the loop is encapsulated in the ml object.
BaRow> It needs a chunk of polishing up and nobody seemed that interested in
BaRow> it last time I mentioned it. My particular application was to MCMC,
BaRow> where you could have an MCMC iterator object that was a subclass of my
BaRow> simple loop class, and then you could have methods like if(isBurnIn(ml))
BaRow> to decide when to start taking samples, or if(!isThinned(ml)) to decide
BaRow> whether to store a sample from a thinned chain. Again, all the info
BaRow> encapsulated in the loop object.
BaRow> Another advantage is that unlike for(i in 1:10000000) it doesn't
BaRow> create a vector of 10000000 objects...
BaRow> If anyone thinks this is worth me working on then I'll try and find
BaRow> some spare time (hah!) to fix it up. Or if anyone wants to take over, I
BaRow> can throw my code at you at see if it sticks.
I think it's very well worth pursuing and "publishing" one way
or the other. IIRC, even with R-core (Luke?) people have had related ideas
(not mainly for MCMC, but for remedying the fact that you
don't want to construct 1:10000000 or its version with another '0'),
even thinking about using (simple versions of these) for
"standard R".
Probably the topic then really belongs to R-devel rather than
R-help.
Martin Maechler, ETH Zurich
More information about the R-help
mailing list