Write an R script to simulate tossing a coin. Plot the evolution of the relative frequency of heads in a sequence of 100 coin tosses?
please use R script
Solution: The R script to simulate tossing a coin is given
here:
We can write a function to flip a coin n times as:
> FlipCoin = function(n) sample(0:1,n,rep=T)
> e1=FlipCoin(100)
> e1
[1] 1 1 1 1 1 0 1 0 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 0 1 0 0 1
0 0 0 1 1
[38] 1 1 1 0 0 1 1 1 1 0 1 0 0 1 0 1 1 1 0 0 0 0 1 1 1 0 1 1 0 0 1
1 0 1 1 1 0
[75] 0 0 1 1 0 1 1 0 0 1 1 1 1 1 0 0 1 0 1 1 1 0 1 1 1 0
> hist(e1,breaks=c(-0.5,0.5,1.5), prob=T)
The evolution of the relative frequency of heads in a sequence
of 100 coin tosses is given here
Get Answers For Free
Most questions answered within 1 hours.