Question

R CODE PROGRAM 1. Suppose we want to simulate an experiment that can take outcomes 1;...

R CODE PROGRAM

1. Suppose we want to simulate an experiment that can take outcomes 1; : : : ; n with probabilies p1; : : : ; pn. To be specic, suppose the R-vector p=c(.1,.2,.3,.35, .02, .03) gives the desired probabilities. Write R code that produces a number from 1 to 6 with the given probabilities, without using if statements. I recommend using the R command cumsum to do this, though there many possible approaches.

2. Suppose we are interested in P(A) for some event A. While the n^(1/2) rule

is simple, a better 95% confidence interval is given by

p +- 1.96((p(1-p)/n)^(1/2))

where ^p is still the proportion of times A occurs in our sample. This

confidence is smaller, which is better, but still contains the true probability

about 95% of the time.

Suppose our event of interest has P(A) = 1/10.

(a) Simulate 300 trials of the experiment and print out the resulting

confidence interval using the formula above.

(b) In your simulation above, for each trial, 1,. . . , 300, plot the upper

and lower endponts of the confidence interval. That is, if n is the

number of trials done so far, and un and ln are the upper and lower

limits of the confidence interval based on n trials, plot the points

(x; y ) = (n; Un ) and (x; y ) = (n; Ln ) for n = 1; : : : ; 300.

(c) Repeat the experiment above 1000 times and compute the fraction of

the time the confidence interval contains the true probability, 1/10.

This should happen about 95% of the time.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Only Question 1 solved

Write R code that produces a number from 1 to 6 with the given probabilities, without using if statements. num <- seq (1:6) pく-c (.1, .2, .3, .35, .02, .03) output <-sample (x-num, size = 10000, replace = T , prob = p ) prop.table (table (output)) tt output 1 3 # 0.1018 0.1978 0.2986 0.3493 0.0205 0.0320 2 4 The output shows when the random sampling is done 10000 times, it is close to the probability distribution specified.

Add a comment
Know the answer?
Add Answer to:
R CODE PROGRAM 1. Suppose we want to simulate an experiment that can take outcomes 1;...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT