Thank you all.

What Chuck has suggested might not be applicable since the number of different 
times is around 40,000.
The object of optimization in my function is the varying "value", which is 
basically data * parameter, of which "parameter" is the object of optimization..
 
And from the r profiling with a subset of data,
I got this report..any idea what "<Anonymous>" is?


$by.total
                        total.time total.pct self.time self.pct
"<Anonymous>"               571.56     100.0      0.02      0.0
"optim"                     571.56     100.0      0.00      0.0
"fn"                        571.54     100.0      0.98      0.2
"eval"                      423.74      74.1      0.00      0.0
"with.default"              423.74      74.1      0.00      0.0
"with"                      423.74      74.1      0.00      0.0
"tapply"                    414.28      72.5     13.84      2.4
"lapply"                    255.48      44.7     76.94     13.5
"factor"                    127.68      22.3     11.08      1.9
"unlist"                    120.54      21.1     80.46     14.1
"FUN"                        94.16      16.5     94.16     16.5
.
.
.
.
.


> Date: Sun, 1 Nov 2009 15:35:41 -0400
> Subject: Re: [R] avoiding loop
> From: jholt...@gmail.com
> To: bbom...@hotmail.com
> CC: dwinsem...@comcast.net; d.rizopou...@erasmusmc.nl; r-help@r-project.org
> 
> What you need to do is to understand how to use Rprof so that you can
> determine where the time is being spent.  It probably indicates that
> this is not the source of slowness in your optimization function.  How
> much time are we talking about?  You may spent more time trying to
> optimize the function than just running the current version even if it
> is "slow" (slow is a relative term and does not hold much meaning
> without some context round it).
> 
> On Sat, Oct 31, 2009 at 11:36 PM, parkbomee <bbom...@hotmail.com> wrote:
> >
> > Thank you both.
> >
> > However, using tapply() instead of a loop does not seem to improve my code 
> > much.
> > I am using this inside of an optimization function,
> > and it still takes more than it needs...
> >
> >
> >
> >> CC: bbom...@hotmail.com; r-help@r-project.org
> >> From: dwinsem...@comcast.net
> >> To: d.rizopou...@erasmusmc.nl
> >> Subject: Re: [R] avoiding loop
> >> Date: Sat, 31 Oct 2009 22:26:17 -0400
> >>
> >> This is pretty much equivalent:
> >>
> >> tapply(DF$value[DF$choice==1], DF$time[DF$choice==1], sum) /
> >>          tapply(DF$value, DF$time, sum)
> >>
> >> And both will probably fail if the number of groups with choice==1 is
> >> different than the number overall.
> >>
> >> --
> >> David.
> >>
> >> On Oct 31, 2009, at 5:14 PM, Dimitris Rizopoulos wrote:
> >>
> >> > one approach is the following:
> >> >
> >> > # say 'DF' is your data frame, then
> >> > with(DF, {
> >> >    ind <- choice == 1
> >> >    n <- tapply(value[ind], time[ind], sum)
> >> >    d <- tapply(value, time, sum)
> >> >    n / d
> >> > })
> >> >
> >> >
> >> > I hope it helps.
> >> >
> >> > Best,
> >> > Dimitris
> >> >
> >> >
> >> > parkbomee wrote:
> >> >> Hi all,
> >> >> I am trying to figure out a way to improve my code's efficiency by
> >> >> avoiding the use of loop.
> >> >> I want to calculate a conditional mean(?) given time.
> >> >> For example, from the data below, I want to calculate sum((value|
> >> >> choice==1)/sum(value)) across time.
> >> >> Is there a way to do it without using a loop?
> >> >> time  cum_time  choice    value
> >> >> 1         4             1           3
> >> >> 1         4              0           2
> >> >> 1          4             0           3
> >> >> 1          4             0           3
> >> >> 2         6             1           4
> >> >> 2         6             0           4
> >> >> 2         6             0           2
> >> >> 2         6             0           4
> >> >> 2         6             0           2
> >> >> 2         6             0           2 3         4
> >> >> 1           2 3         4             0           3 3
> >> >> 4             0           5 3         4             0           2
> >> >> My code looks like
> >> >> objective[1] = value[1] / sum(value[1:cum_time[1])
> >> >> for (i in 2:max(time)){
> >> >>     objective[i] = value[cum_time[i-1]+1] /
> >> >> sum(value[(cum_time[i-1]+1) : cum_time[i])])
> >> >> }
> >> >> sum(objective)
> >> >> Anyone have an idea that I can do this without using a loop??
> >> >> Thanks.
> >> >>
> >> >> _________________________________________________________________
> >> >> [[elided Hotmail spam]]
> >> >>    [[alternative HTML version deleted]]
> >> >> ______________________________________________
> >> >> R-help@r-project.org mailing list
> >> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> >> PLEASE do read the posting guide 
> >> >> http://www.R-project.org/posting-guide.html
> >> >> and provide commented, minimal, self-contained, reproducible code.
> >> >
> >> > --
> >> > Dimitris Rizopoulos
> >> > Assistant Professor
> >> > Department of Biostatistics
> >> > Erasmus University Medical Center
> >> >
> >> > Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
> >> > Tel: +31/(0)10/7043478
> >> > Fax: +31/(0)10/7043014
> >> >
> >> > ______________________________________________
> >> > R-help@r-project.org mailing list
> >> > https://stat.ethz.ch/mailman/listinfo/r-help
> >> > PLEASE do read the posting guide 
> >> > http://www.R-project.org/posting-guide.html
> >> > and provide commented, minimal, self-contained, reproducible code.
> >>
> >> David Winsemius, MD
> >> Heritage Laboratories
> >> West Hartford, CT
> >>
> >
> > _________________________________________________________________
> > [[elided Hotmail spam]]
> >        [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-help@r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
> 
> 
> 
> -- 
> Jim Holtman
> Cincinnati, OH
> +1 513 646 9390
> 
> What is the problem that you are trying to solve?
                                          
_________________________________________________________________
[[elided Hotmail spam]]

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to