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 op inbo.be
www.inbo.be
-----Oorspronkelijk bericht-----
Van: r-help-bounces op stat.math.ethz.ch
[mailto:r-help-bounces op stat.math.ethz.ch] Namens Zodet, Marc W. (AHRQ)
Verzonden: woensdag 20 september 2006 15:29
Aan: r-help op 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 op 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.