[R] Loop avoidance in simulating a vector

Ray Brownrigg Ray.Brownrigg at mcs.vuw.ac.nz
Thu Oct 16 22:36:09 CEST 2008


If you want to avoid using a loop, rather than coding a loop, then someting like:

DV <- rnorm(sum(n))
DV <- DV * rep(sigma, times=n) + rep(mu, times=n)

will achieve the same as the loop you specified, but generalised to non-constant n.

HTH
Ray Brownrigg

On Fri, 17 Oct 2008, Bert Gunter wrote:
> mapply is still a (disguised) loop (at the interpreted level). So other
> than improving code readability (always a good thing!), it shouldn't make
> much of an efficiency difference.
>
> A longer answer is: if all you're doing is a location-scale family of
> distributions, then creating a matrix of standard normal (or whatever)
> distributed data for all 1:N at once and then using matrix operations to
> multiply and add, say, so each column becomes your different distribution
> might be faster. This gets the loops down to C code.
>
> A shorter answer is: it's unlikely that any of this makes enough of a
> difference to be worth the effort. Random number generation is so efficient
> in R that "avoiding loops" rarely matters.
>
> Also see ?replicate for a way to perhaps write cleaner code (but still
> using hidden interpreted loops).
>
> -- Bert Gunter
>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Christos Hatzis
> Sent: Thursday, October 16, 2008 1:06 PM
> To: 'David Afshartous'; r-help at r-project.org
> Subject: Re: [R] Loop avoidance in simulating a vector
>
> Have a look at mapply.
>
> -Christos
>
> > -----Original Message-----
> > From: r-help-bounces at r-project.org
> > [mailto:r-help-bounces at r-project.org] On Behalf Of David Afshartous
> > Sent: Thursday, October 16, 2008 3:47 PM
> > To: r-help at r-project.org
> > Subject: [R] Loop avoidance in simulating a vector
> >
> >
> >
> > All,
> >
> > I'd like to simulate a vector that is formed from many
> > distinct distributions and avoid a loop if possible.  E.g, consider:
> >
> > mu = c(1, 2, 3)
> > sigma = c(1, 2, 3)
> > n = c(10, 10, 10)
> >
> > And we simulate a vector of length 30 that consists of
> > N(mu[i], sigma[i])
> > distributed data, each of length n[i].   Of course for just
> > three groups we
> > can simply write it out as:
> >
> > DV = c(rnorm(n[1], mu[1], sigma[1]), rnorm(n[2], mu[2],
> > sigma[2]), rnorm(n[3], mu[3], sigma[3]) )
> >
> > For many groups we can use a loop (assuming equal numbers per group):
> >
> > n = n[1]
> > DV = numeric(N*n)
> > for (i in 1:N) {
> >     DV[(n*i - (n-1)): (n*i)] = rnorm(n, mu[i], sigma[i])
> >     }
> >
> > Is there any way to do the general cas without using a loop?
> >
> > Cheers,
> > David
> >
> > ______________________________________________
> > R-help at 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.
>
> ______________________________________________
> R-help at 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.
>
> ______________________________________________
> R-help at 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.



More information about the R-help mailing list