On this laid upwards of exercises, nosotros are going to explore some of the probability functions inwards R amongst practical applications. Basic probability noesis is required.
Note: We are going to utilization random number functions together with random procedure functions inwards R such as
runif
, a occupation amongst these functions is that every fourth dimension you lot run them you lot volition obtain a unlike value. To brand your results reproducible you lot tin specify the value of the seed using set.seed(‘any number’)
before calling a random function. (If you lot are non familiar amongst seeds, shout back of them every bit the tracking number of your random numbers). For this laid upwards of exercises nosotros volition use set.seed(1)
, don’t forget to specify it earlier every random exercise. Answers to the exercises are available here
If you lot obtained a unlike (correct) response than those listed on the solutions page, delight experience gratis to postal service your response every bit a comment on that page.
Exercise 1
Generating random numbers. Set your seed to 1 together with generate 10 random numbers using
runif
and salve it inwards an object called random_numbers
. Exercise 2
Using the function
ifelse
and the object random_numbers
simulate money tosses. Hint: If random_numbers
is bigger than .5 together with thence the number is head, otherwise is tail. Another agency of generating random money tosses is past times using the
rbinom
function. Set the seed in 1 lawsuit again to 1 together with copy amongst this business office 10 money tosses. Note: The value you lot volition obtain is the total number of heads of those 10 money tosses. Exercise 3
Using the function
rbinom
to generate 10 unfair money tosses amongst probability success of 0.3. Set the seed to 1. Exercise 4
We tin copy rolling a choke inwards R with
runif
. Save inwards an object called die_roll
1 random number with min = 0
and max = 6
. This hateful that nosotros volition generate a random number betwixt 1 together with 6. Apply the function
ceiling
to die_roll
. Don’t forget to laid upwards the seed to 1 earlier calling runif
. Exercise 5
Simulate normal distribution values. Imagine a population inwards which the average tiptop is 1.70 thousand amongst an touchstone departure of 0.1, using
rnorm
simulate the tiptop of 100 people together with salve it inwards an object called heights
. To acquire an catch of the values of heights applying the function
summary
to it. Exercise 6
a) What’s the probability that a mortal volition endure smaller or equal to 1.90 thousand ? Use
b) What’s the probability that a mortal volition endure taller or equal to 1.60 m? Use
pnorm
b) What’s the probability that a mortal volition endure taller or equal to 1.60 m? Use
pnorm
Exercise 7
The waiting fourth dimension (in minutes) at a doctor’s hospital follows an exponential distribution amongst a charge per unit of measurement parameter of 1/50. Use the function
rexp
to copy the waiting fourth dimension of xxx people at the doctor’s office. Exercise 8
What’s the probability that a mortal volition hold off less than 10 minutes? Use
pexp
Exercise 9
What’s the waiting fourth dimension average?
Exercise 10
Let’s assume that patients amongst a waiting fourth dimension bigger than threescore minutes leave. Out of 100 patients that acquire inwards to the hospital how many are expected to leave? Use
pexp
______________________________________________________
Below are the solutions to these exercises on probability functions.
#################### # # # Exercise 1 # # # #################### set.seed(1) random_numbers <- runif(10) #################### # # # Exercise two # # # #################### (coin_tosses_1 <- ifelse(random_numbers>.5, 'head', 'tail'))
## [1] "tail" "tail" "head" "head" "tail" "head" "head" "head" "head" "tail"
set.seed(1) (coin_tosses <- rbinom(n = 1, size = 10, prob = .5))
## [1] four
#################### # # # Exercise 3 # # # #################### set.seed(1) (coin_tosses_unfair <- rbinom(n = 1, size = 10, prob = .3))
## [1] two
#################### # # # Exercise four # # # #################### set.seed(1) (die_roll <- runif( n = 1, min = 0, max = 6))
## [1] 1.593052
(ceiling(die_roll))
## [1] two
#################### # # # Exercise v # # # #################### set.seed(1) heights <- rnorm(n = 100, mean = 1.70, sd = .1) summary(heights)
## Min. 1st Qu. Median Mean third Qu. Max. ## 1.479 1.651 1.711 1.711 1.769 1.940
#################### # # # Exercise vi # # # #################### pnorm(1.90, mean = 1.70, sd = .1)
## [1] 0.9772499
1 - pnorm(1.60, mean = 1.70, sd = .1)
## [1] 0.8413447
#################### # # # Exercise vii # # # #################### set.seed(1) (patients <- rexp(rate = 1/50, n =30))
## [1] 37.759092 59.082139 7.285336 6.989763 21.803431 144.748427 ## [7] 61.478103 26.984142 47.828375 7.352300 69.536756 38.101493 ## [13] 61.880178 221.196711 52.727158 51.762197 93.801759 32.737332 ## [19] 16.846674 29.423986 118.225763 32.094629 14.706019 28.293276 ## [25] 5.303631 2.971958 28.935623 197.946643 58.665605 49.840648
#################### # # # Exercise 8 # # # #################### pexp(q = 10,rate = 1/50)
## [1] 0.1812692
#################### # # # Exercise ix # # # #################### qexp(.5, rate = 1/50)
## [1] 34.65736
#################### # # # Exercise 10 # # # #################### (1 - pexp(q=60, rate =1/50)) *100
## [1] 30.11942
________________________________
Sources:
http://www.r-exercises.com/2017/08/17/probability-functions-beginner/
http://www.r-exercises.com/2017/08/17/probabilty-functions-beginner-solution/