I am taking a basic statistics course this summer, and while the majority of the class is using a statistical package that came with the book, I am doing everything in R for practical reasons. Forgive me if there is documentation/instruction easily available on this topic, but Google and forums searches left me with nothing. We are learning about randomness at the moment and are required to create simulations to use as an example. For the problem in the text, there are 57 numbers with 1 through 20 belonging to the same ?group?, meaning the probability that a number in that group is picked is 20/57, and 21 through 57 are individuals, belonging to no larger group. The point of the exercise is to simulate the probability of 1 through 20 being chosen at random (with no replacement) three times in a row through hundreds of trials. This is the basic syntax I have been using? sample(c(rep(0, 20), seq(1:37)), 3, replace = F) That works just fine, but I am wondering if there is a more efficient way of doing this. Right now, I am hitting the up arrow and hitting ?enter? hundreds of times and making note of each time a trial results in 0,0,0. If I place this syntax in a rep function, it just repeats the same output of a single sample x times instead of giving me new data. -- View this message in context: http://r.789695.n4.nabble.com/Simple-simulations-tp3628863p3628863.html Sent from the R help mailing list archive at Nabble.com.
#sampling elements
x<-c(rep(0, 20), seq(1:37))
#number of simulations to perform
sims<-100
#vector to store results
results<-c()
#using for loop to perform simulations
for(i in 1:sims){
#take your sample
y<-sample(x,3,replace=FALSE)
#check if all elements are zero
if(y[1]==0 && y[2]==0 && y[3]==0){
#return 1 if they are
results[i]<-1
} else {
#otherwise return 0
results[i]<-0
}
}
#proportion of simulations where all are zero
sum(results)/sims
On Tue, Jun 28, 2011 at 7:08 AM, robcinm <robcinm at gmail.com>
wrote:> I am taking a basic statistics course this summer, and while the majority
of
> the class is using a statistical package that came with the book, I am
doing
> everything in R for practical reasons. Forgive me if there is
> documentation/instruction easily available on this topic, but Google and
> forums searches left me with nothing.
>
> We are learning about randomness at the moment and are required to create
> simulations to use as an example. For the problem in the text, there are 57
> numbers with 1 through 20 belonging to the same ?group?, meaning the
> probability that a number in that group is picked is 20/57, and 21 through
> 57 are individuals, belonging to no larger group. The point of the exercise
> is to simulate the probability of 1 through 20 being chosen at random (with
> no replacement) three times in a row through hundreds of trials. This is
the
> basic syntax I have been using?
>
> sample(c(rep(0, 20), seq(1:37)), 3, replace = F)
>
> That works just fine, but I am wondering if there is a more efficient way
of
> doing this. Right now, I am hitting the up arrow and hitting ?enter?
> hundreds of times and making note of each time a trial results in 0,0,0. If
> I place this syntax in a rep function, it just repeats the same output of a
> single sample x times instead of giving me new data.
>
>
> --
> View this message in context:
http://r.789695.n4.nabble.com/Simple-simulations-tp3628863p3628863.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
Hi robcinm,
You might also consider:
# data
x <- c(rep(0, 20), 1:37)
# number of simulations
B <- 1000
# result: TRUE/FALSE
out <- replicate(B, {
y <- sample(x, 3, replace = FALSE)
all(y == 0)
})
mean(out)
HTH,
Jorge
On Mon, Jun 27, 2011 at 5:08 PM, robcinm <> wrote:
> I am taking a basic statistics course this summer, and while the majority
> of
> the class is using a statistical package that came with the book, I am
> doing
> everything in R for practical reasons. Forgive me if there is
> documentation/instruction easily available on this topic, but Google and
> forums searches left me with nothing.
>
> We are learning about randomness at the moment and are required to create
> simulations to use as an example. For the problem in the text, there are 57
> numbers with 1 through 20 belonging to the same “group”, meaning the
> probability that a number in that group is picked is 20/57, and 21 through
> 57 are individuals, belonging to no larger group. The point of the exercise
> is to simulate the probability of 1 through 20 being chosen at random (with
> no replacement) three times in a row through hundreds of trials. This is
> the
> basic syntax I have been using…
>
> sample(c(rep(0, 20), seq(1:37)), 3, replace = F)
>
> That works just fine, but I am wondering if there is a more efficient way
> of
> doing this. Right now, I am hitting the up arrow and hitting “enter”
> hundreds of times and making note of each time a trial results in 0,0,0. If
> I place this syntax in a rep function, it just repeats the same output of a
> single sample x times instead of giving me new data.
>
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Simple-simulations-tp3628863p3628863.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
>
[[alternative HTML version deleted]]
Here's a one-liner. Let's see their software do that! sum(replicate(100, sum(sample(c(rep(0, 20), seq(1:37)), 3, replace = FALSE)) == 0)) Am 27.06.2011 23:08, schrieb robcinm:> I am taking a basic statistics course this summer, and while the majority of > the class is using a statistical package that came with the book, I am doing > everything in R for practical reasons. Forgive me if there is > documentation/instruction easily available on this topic, but Google and > forums searches left me with nothing. > > We are learning about randomness at the moment and are required to create > simulations to use as an example. For the problem in the text, there are 57 > numbers with 1 through 20 belonging to the same ?group?, meaning the > probability that a number in that group is picked is 20/57, and 21 through > 57 are individuals, belonging to no larger group. The point of the exercise > is to simulate the probability of 1 through 20 being chosen at random (with > no replacement) three times in a row through hundreds of trials. This is the > basic syntax I have been using? > > sample(c(rep(0, 20), seq(1:37)), 3, replace = F) > > That works just fine, but I am wondering if there is a more efficient way of > doing this. Right now, I am hitting the up arrow and hitting ?enter? > hundreds of times and making note of each time a trial results in 0,0,0. If > I place this syntax in a rep function, it just repeats the same output of a > single sample x times instead of giving me new data. > > > -- > View this message in context: http://r.789695.n4.nabble.com/Simple-simulations-tp3628863p3628863.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.