Problem 1: Practice with the sample function
Recall that the sample function in R takes a sample of specified size from the elements given in its first argument.
a. Look up the help file for the sample function. Explain what the argument “replace” does in the sample function.
b. Use the sample function to simulate five rolls of a six-sided (fair) die.
c. Suppose you have a population of 1000 people, where 500 are Democrat, 300 are Republican, and 200 are Independent.
i. How many Democrats would you expect to get in a simple random sample of 10 people from this population?
ii. Use the sample function to simulate taking a simple random sample of 10 people from this population, where you only record the person’s political preference (“D”, “R”, or “I”). How many Democrats did you get in your sample? How does this compare to your answer in part i.?
Problem 2: The classic hat problem
Suppose n people go to a fancy restaurant. Each person is wearing a hat and checks his/her hat at the door as he/she arrives. The hat-check attendant gets tipsy throughout the evening, forgetting which hat belongs to whom, and returns a random hat to each person as they leave. The patrons leave in a random order.
Conduct a simulation in R to answer the following questions:
a. Assume n = 5. Calculate an empirical probability that no one gets his or her own hat back. Use this to also estimate the probability that at least one patron gets his or her own hat back.
b. Still assuming n = 5, calculate the empirical mean (average) number of patrons who get their own hats back.
c. Repeat parts a. and b. using n = 20.
d. How do you think the probability of at least one match will change if there are 50 people? How do you think the average number of matches will change with 50 people? Test your guesses by repeating the simulation with 50 hats and people.
e. Try the simulation for larger and larger values of n to get an estimate of the limits for the probability of no one getting his/her hat back and for the average number of patrons who get their hats back. (We will derive the exact probability of no one getting his/her hat back in class, exp(-1). You can check your simulation by checking if your simulated answer is close to the exact answer.)
Hints for Problem 2
You will need to simulate returning the hats at random many many times (1000 times or more), keeping track of how many patrons got their hat back on each trial. Try to write R code to perform the following steps:
• Set up an empty vector to store the result of each trial (in the end, this will be a vector of non-negative integers representing the number of patrons who got their hat back on each trial).
• Within a for loop, for each trial generate two random permutations of size n (the sample function will come in handy here). One represents the random order in which the patrons leave, the other the random order in which the hats are returned. (Teaser: if the hats are returned in random order, does it matter in which order the patrons leave?).
• Compute the number of matches and save it in the vector created in step 1.
Once the for loop runs, you can calculate the proportion of times that the number of matches was greater than zero, and the mean number of matches over all trials. Run your simulation several times to get an idea of how variable your results are.
Click the button below to order a similar custom written paper.
PLACE THIS ORDER OR A SIMILAR ORDER WITH US TODAY AND GET AN AMAZING DISCOUNT