Access over 40 Millions of academic & study documents

Lab 2 Probability

Content type
User Generated
Subject
Statistics
Type
Homework
Showing Page:
1/6
Lab 2: Basic Probability
Fall 2021
In this lab we will work through two basic probability problems, and in the process
practice more with RMarkdown.
Packages needed:
knitr, xtable, pander
Task 1: Coin flipping
This task illustrates the interpretation of a probability as the long run relative frequency of
an event after a large number of trials.
Dobrow presents R code on pages 24 and 453 for simulating coin tosses. We perform the
experiment of observing the number of heads after tossing a fair coin 100 times
(probability of a heads on any one toss is 50%). Just like rolling a die, we can use the R
function sample to flip a coin. Though recognize there are only two outcomes: heads (1)
and tails (0). We will report the number of heads after 50 tosses and intermediary output.
We will also graphically display the cumulative proportion of heads again the coin toss (1
to 100). The type="l" parameter in the R function plot will draw a solid line. Always label
your axes! In RMarkdown, a graph title is useful too.
Code set-up
simnum = 100 # number of coin flips
coinflips = sample(0:1, simnum, replace = TRUE) # flip the coin: heads = 1, t
ails = 0
heads = cumsum(coinflips) # cumulative sum of number of heads after each coin
toss
prop = heads/(1:simnum) # running proportion of heads after each coin toss
head(heads) # report cumulative number of heads after each of the first 6 fli
ps
## [1] 1 1 1 1 1 2
heads[50]
## [1] 25
# running mean plot for proportion of heads.
plot(1:simnum, prop, type="l", xlab="Number of coins", ylab="Running average"
, main="Proportion of heads in 100 coin flips")
abline(h=0.5) # add a line at 50%

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/6
The problem
Let us now flip a “biased” coin. Perform the experiment of observing the number of heads
after tossing a coin 1000 times, with the probability of getting a heads on any one toss
being 40%. To change the probability in the R function sample use the parameter
prob=c(0.6,0.4); note that we need to specify the probability of a tails (0) and a heads (1)
in this parameter. Note that if you do not want the code presented in your html report, use
the parameter echo=FALSE in the code chunk.
Report the following:
Proportion of heads after 10, 50, 100, 200, and 500 tosses (see table code chunk below
under “RMarkdown presenting output”!)
Plot of the cumulative proportion of heads vs. coin toss number (1 to 1000); label the
axes and title the graphic appropriately!
On the plot, draw a horizontal line at y=0.40, the probability of tossing a head for this
coin
simnum1 = 1000
coinflips1 = sample(0:1,simnum1, replace = TRUE, prob = c(0.6,0.4))
heads1 = cumsum(coinflips1)
prop1 = heads1/(1:simnum1)
prop1[10]; prop1[50]; prop1[100]; prop1[200]; prop1[500];

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/6

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 6 pages?
Access Now
Unformatted Attachment Preview
Lab 2: Basic Probability Fall 2021 In this lab we will work through two basic probability problems, and in the process practice more with RMarkdown. Packages needed: knitr, xtable, pander Task 1: Coin flipping This task illustrates the interpretation of a probability as the long run relative frequency of an event after a large number of trials. Dobrow presents R code on pages 24 and 453 for simulating coin tosses. We perform the experiment of observing the number of heads after tossing a fair coin 100 times (probability of a heads on any one toss is 50%). Just like rolling a die, we can use the R function sample to flip a coin. Though recognize there are only two outcomes: heads (1) and tails (0). We will report the number of heads after 50 tosses and intermediary output. We will also graphically display the cumulative proportion of heads again the coin toss (1 to 100). The type="l" parameter in the R function plot will draw a solid line. Always label your axes! In RMarkdown, a graph title is useful too. Code set-up simnum = 100 # number of coin flips coinflips = sample(0:1, simnum, replace = TRUE) # flip the coin: heads = 1, t ails = 0 heads = cumsum(coinflips) # cumulative sum of number of heads after each coin toss prop = heads/(1:simnum) # running proportion of heads after each coin toss head(heads) # report cumulative number of heads after each of the first 6 fli ps ## [1] 1 1 1 1 1 2 heads[50] ## [1] 25 # running mean plot for proportion of heads. plot(1:simnum, prop, ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Indeed
4.5
Sitejabber
4.4