Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Lab Project 2: Using R to simulate experiments, Study notes of Probability and Statistics

Use R to simulate an experiment of tossing a coin 100 times. Print the relative histogram as above with your your name on it. 2. Find the relative frequency ...

Typology: Study notes

2021/2022

Uploaded on 09/12/2022

mathieu
mathieu 🇮🇹

4.2

(11)

235 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lab Project 2: Using R to simulate experiments
Course : Introduction to Probability and Statistics, Math 113 Section 3234
Instructor: Abhijit Champanerkar
Date: Oct 17th 2012
Tossing a coin
The probability of getting a Heads or a Tails on a coin toss is both 0.5. We can use Rto
simulate an experiment of flipping a coin a number of times and compare our results with
the theoretical probability. First let fix the convention:
0 = Tails and 1 = Heads
We can use the following command to tell Rto flip a coin 15 times:
> sample(0:1,15,rep=T)
[1]110101110001110
This gives 6 Tails and 9 heads. In fact we can write a function to flip a coin ntimes:
> FlipCoin = function(n) sample(0:1,n,rep=T)
> e1=FlipCoin(30)
> e1
[1]011011010111001010111101000001
Now we can use the sum command to compare the results from this experiment to the
theoretical probabilities. For example in the above experiment of flipping a coin 30 times,
we can count the heads and tails as:
> sum(e1==0)
[1] 14
> sum(e1==0)/30
[1] 0.4666667
> sum(e1==1)
[1] 16
> sum(e1==1)/30
[1] 0.5333333
This gives us 14 Tails and 16 Heads. The “probability” or relative frequency of a Tail in this
experiment is 0.467 and a Head is 0.533. Note that you may get different answers.
We can plot a relative histogram using the command:
> hist(e1,breaks=c(-0.5,0.5,1.5), prob=T)
1
pf3
pf4

Partial preview of the text

Download Lab Project 2: Using R to simulate experiments and more Study notes Probability and Statistics in PDF only on Docsity!

Lab Project 2: Using R to simulate experiments

Course : Introduction to Probability and Statistics, Math 113 Section 3234 Instructor: Abhijit Champanerkar Date: Oct 17th 2012

Tossing a coin

The probability of getting a Heads or a Tails on a coin toss is both 0.5. We can use R to simulate an experiment of flipping a coin a number of times and compare our results with the theoretical probability. First let fix the convention:

0 = Tails and 1 = Heads

We can use the following command to tell R to flip a coin 15 times:

sample(0:1,15,rep=T) [1] 1 1 0 1 0 1 1 1 0 0 0 1 1 1 0

This gives 6 Tails and 9 heads. In fact we can write a function to flip a coin n times:

FlipCoin = function(n) sample(0:1,n,rep=T) e1=FlipCoin(30) e [1] 0 1 1 0 1 1 0 1 0 1 1 1 0 0 1 0 1 0 1 1 1 1 0 1 0 0 0 0 0 1

Now we can use the sum command to compare the results from this experiment to the theoretical probabilities. For example in the above experiment of flipping a coin 30 times, we can count the heads and tails as:

sum(e1==0) [1] 14 sum(e1==0)/ [1] 0. sum(e1==1) [1] 16 sum(e1==1)/ [1] 0.

This gives us 14 Tails and 16 Heads. The “probability” or relative frequency of a Tail in this experiment is 0.467 and a Head is 0.533. Note that you may get different answers. We can plot a relative histogram using the command:

hist(e1,breaks=c(-0.5,0.5,1.5), prob=T)

Questions

  1. Use R to simulate an experiment of tossing a coin 100 times. Print the relative histogram as above with your your name on it.
  2. Find the relative frequency of a Tail and Head in your experiment and fill in the table on the next page.
  3. Repeat 2 for tossing a coin 500 times (do not print histogram).

Rolling dice

The probability of getting a number between 1 to 6 on a roll of a die is 1/6 = 0.1666667. As above we can use R to simulate an experiment of rolling a die a number of times and compare our results with the theoretical probability. We can use the following command to tell R to roll a die 20 times:

sample(1:6,20,rep=T) [1] 3 3 4 1 1 2 2 5 1 2 4 4 3 2 1 5 2 6 5 2

As before we can write a function to roll a die n times:

RollDie = function(n) sample(1:6,n,rep=T) d1=RollDie(50) d [1] 3 4 5 5 6 5 1 6 3 3 1 3 5 4 4 3 2 1 5 2 1 1 2 2 3 1 6 2 6 1 5 1 4 1 4 4 4 6 [39] 2 1 5 5 2 6 1 3 6 3 1 6

Now we can use the sum command to compare the results from this experiment to the theoretical probabilities. For example in the above experiment the number of 3’s and its relative frequency is:

sum(d1==3) [1] 8 sum(d1==3)/ [1] 0.

The number 3 occurs 8 times and its relative frequency is 0.16 which is quite close to 1/6. Note that you may get different answers. We can plot a relative histogram using the command:

hist(d1,breaks=c(0.5,1.5,2.5,3.5,4.5,5.5,6.5), prob=T)

Lab Project 2

Please write your name, fill in the values, tear off and hand to instructor.

Name:

Coin Toss

100 tosses 500 tosses

Relative Frequency of Heads

Relative Frequency of Tails

Rolling Dice

200 rolls 1000 rolls

Relative Frequency of 1

Relative Frequency of 2

Relative Frequency of 3

Relative Frequency of 4

Relative Frequency of 5

Relative Frequency of 6