问题
The experiment involves rolling a fair die and getting x say, then tossing a fair coin x number of times and recording the number of tails. I need to do this experiment 50 times and record the outcomes in a vector, (which I'll then use to plot a histogram.)
This is my code so far:
for (i in 1:100)
{X <- sample(6,1,replace=TRUE,c(1,1,1,1,1,1)/6)
Y <- sample(2,1,replace=TRUE,c(1,1)/2)}
Youtcomes <- c(sum(Y))
Youtcomes
But instead of giving me a vector with 100 elements, I keep getting just a single number. Where am I going wrong?
Note: I have to use a for loop.
回答1:
Discalimer: (very inefficient solution see mnel/Gavin's solution)
As you can read the many, many, .. MANY comments underneath each of the answers, while this answer attempts to answer OP's specific question(however inefficient his requirements maybe), in the spirit of maintaining decorum of the forum, some have (rightly) pointed out that the question is in bad taste and my answer doesn't do justice to the forum requirements. I accept all criticism and leave the answer here only for obvious reasons (marked as answer, continuity). I suggest you look at mnel/Gavin's answer for a vectorised solution to this specific problem. If you're interested in looking at an implementation of for-loop, then refer to the bottom of this post, but I suggest you look at it to know the structure of for-loop, but not implement a for-loop to this specific problem. Thank you.
Your code is riddled with quite a few problems, apart from the main problem @Joshua already mentioned:
First, you rewrite every time the values of X and Y inside the loop so, at the end of the loop, there is only the last value of Y that is being summed up.
Second, your code for Y
is not correct. You say, you have to get x
amount of coin tosses, Yet, you use sample(2, 1, ...)
. The 1
must be replaced with X
which equals the number from the die roll.
Try out this code instead:
Youtcomes <- sapply(1:100, function(x) {
X <- sample(1:6, 1, replace=TRUE, rep(1,6)/6)
Y <- sample(c("H", "T"), X, replace=TRUE, rep(1,2)/2)
sum(Y == "T")
})
Here, we loop over 100 times, and each time, sample values between 1 and 6 and store in X. Then, we sample either head
(H) or tail
(T) X
number of times and store in Y.
Now, sum(Y == "T")
gives the sum for current value of x (1 <= x <= 100). So, at the end, Youtcomes
will be your set of simulated Y == Tail
values.
Then, you can do a hist(Youtcomes)
.
Edit: If its a for-loop solution that's desired then,
# always assign the variable you'll index inside for-loop
# else the object will keep growing every time and a copy of
# entire object is made for every i, which makes it extremely
# slow/inefficient.
Youtcomes <- rep(0, 100)
for (i in 1:100) {
X <- sample(1:6, 1, replace=TRUE, rep(1,6)/6)
Y <- sample(c("H", "T"), X, replace=TRUE, rep(1,2)/2)
# assign output inside the loop with [i] indexing
Youtcomes[i] <- sum(Y == "T")
# since Youtcomes is assigned a 100 values of 0's before
# the values will replace 0' at each i. Thus the object
# is not copied every time. This is faster/efficient.
}
回答2:
Use the fact that R
is vectorized. You can then use a binomial distribution to replicate the coin toss.
heads <- rbinom(size = sample(6,100, replace = TRUE), n=100, prob = 0.5)
sum(heads)
回答3:
Perhaps I have missed something, but what is wrong with one call to sample()
to do the 100 rolls of the dice, and then plug that into rbinom()
to do the coin tosses? We pass the output from sample()
to the size
argument
> set.seed(1)
> rbinom(100, size = sample(6, 100, replace = TRUE), prob = 0.5)
[1] 1 1 1 6 1 2 2 2 3 1 2 1 2 1 1 0 3 1 1 3 6 1 2 0 2 1 1 1 2 2 2 1 0 1 4 3 3
[38] 1 5 2 3 2 2 1 3 2 0 2 1 4 2 3 1 1 1 0 1 1 1 1 2 2 1 2 3 1 0 2 1 2 2 4 2 1
[75] 1 5 3 2 3 5 1 2 3 1 4 0 3 1 2 1 1 0 1 5 2 3 0 2 2 3
回答4:
Arun beat me to it. But another of the many many ways could be (if I understand your desired outcome correctly..
X <- sample(6,100,replace=TRUE,c(1,1,1,1,1,1)/6)
Y <- lapply(X , function(x){ res <- sample( c( "H" , "T" ) , x , replace=TRUE , c(1,1)/2 ) ; table( res ) } )
You want to histogram the results....
res <- unlist(Y)
hist( res[names( res )=="T"] )
来源:https://stackoverflow.com/questions/15259182/outcome-of-a-simulated-dice-and-coin-toss-in-r