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.
Simple simulations
5 messages · robcinm, Steven Kennedy, Jorge Ivan Velez +2 more
#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.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20110627/1015a81e/attachment.pl>
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.
Here is another solution if you want to count a sequence like 00000 as three overlapping runs of 3 zeros.
x <- sample(c(rep(0,20), rep(1,37)), 1000000, TRUE) # vector of 1M z <- rle(x) # get the runs indx <- which(z$values == 0 & z$lengths > 2) # find the runs # now determine the number of 3 zeros # if you have 00000 then this is 3 runs of three sum(z$lengths[indx] - 2)
[1] 43353
On Mon, Jun 27, 2011 at 5:53 PM, Alexander Engelhardt
<alex at chaotic-neutral.de> wrote:
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.
______________________________________________ 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.
Jim Holtman Data Munger Guru What is the problem that you are trying to solve?