# Re: [R] Simulation help

From: ONKELINX, Thierry <Thierry.ONKELINX_at_inbo.be>
Date: Wed 20 Sep 2006 - 14:07:31 GMT

This should work. I try to avoid for loops because large (or nested) for loops tend to consume a lot of memory.

one.dataset <- function(dummy = 0, slope = 0.01){   return(c(yr1= mean(rbinom(50,1,.5)), yr2 =mean(rbinom(50,1,.5 + slope)), yr3 =mean(rbinom(50,1,.5 + 2 * slope)), yr4 =mean(rbinom(50,1,.5 + 3 * slope)), yr5 =mean(rbinom(50,1,.5 + 4 * slope))))
}

datasets.slope01 <- t(sapply(1:100, one.dataset, slope = 0.01)) datasets.slope05 <- t(sapply(1:100, one.dataset, slope = 0.05))

Cheers,

Thierry

ir. Thierry Onkelinx

Instituut voor natuur- en bosonderzoek / Reseach Institute for Nature and Forest

Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance

Gaverstraat 4

9500 Geraardsbergen

Belgium

tel. + 32 54/436 185

Thierry.Onkelinx@inbo.be

www.inbo.be

-----Oorspronkelijk bericht-----
Van: r-help-bounces@stat.math.ethz.ch
[mailto:r-help-bounces@stat.math.ethz.ch] Namens Zodet, Marc W. (AHRQ) Verzonden: woensdag 20 september 2006 15:29 Aan: r-help@stat.math.ethz.ch
Onderwerp: [R] Simulation help

I'm trying to simulate trend data over a five year period. I want different trend profiles...the simplest being a linear trend. I've been using the following code:

patBdta1 <- NULL

for(i in 1:100)

patBdta1 <- rbind(patBdta1,c(yr1= mean(rbinom(50,1,.50)),

```                             yr2 =mean(rbinom(50,1,.51)),

yr3 =mean(rbinom(50,1,.52)),

yr4 =mean(rbinom(50,1,.53)),

yr5 =mean(rbinom(50,1,.54))))

```

This code creates 100 data sets each with a 5 yr binomial trend profile with a slope of approximately .01.

Now, what I want to do is pass this code (or some code) in such a way that I can simulate various trend slopes (i.e., pass in a loop or vectorize vs. copying/repeating code for each slope).

Any guidance is much appreciated. Thanks!

Marc

Marc W. Zodet, MS

Senior Health Statistician

Agency for Healthcare Research and Quality

[[alternative HTML version deleted]]

R-help@stat.math.ethz.ch mailing list
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.

R-help@stat.math.ethz.ch 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. Received on Thu Sep 21 00:13:40 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 20 Sep 2006 - 14:30:05 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.