Skip to content
Prev 105893 / 398506 Next

Simulation with R

Ethan Johnsons <ethan.johnsons <at> gmail.com> writes:
After poking around a bit, I see from your Google groups profile
that you are (or at least claim to be) self-teaching yourself
biostats -- which explains your really heavy posting record
here and in other stats forums and means this isn't 
necessarily homework.
(I know, I could just keep my mouth shut).  It would
probably be helpful if it were possible for you to find someone
local to help with some of these questions, as many of them are 
basic stats questions ...

  As for this question -- this process generates a binomial distribution.
Some ways of simulating the frequency distribution are
(1) use rbinom() to sample a number of left- moves for
each of 100 balls -- then the number of right-moves is known,
and you can calculate the ending position
(2) construct a string of 1 and -1 values
for each ball
(either use sample(c(-1,1),size=100,replace=TRUE),
or something involving sign(runif(100),-1,1)) and
use cumsum() to calculate the final position
(3) use a for() loop to sample values and calculate
the position one step at a time [move <- sample(c(-1,1));
cpos <- cpos+move; etc.]

#1 is by far the most efficient, #3 the least, but
might be the most transparent.

  Would you consider "giving back to the community" by writing
up some of the most useful advice you've gotten
for the R wiki?

  Ben Bolker