Description
This lab will investigate some of the continuous distributions. You are expected to learn the
skills needed to make plots and areas under density curves.
The Lab progresses from the Normal to the Beta. Once you understand how the Normal
works it is an easy matter to extend the techniques you have learnt to other distributions.
It is expected in this lab that you be able to use available information in R to work out
how the Beta distribution works by using ?dbeta in R.
Make sure that all plots are legible – this will mean adjusting ylim and xlim options in the
curve() function.
Objectives
In this lab you will learn how to:
1. Plot the densities.
2. Make areas under the curve.
3. Calculate probabilities.
4. Create a function that calculates and displays areas
5. Add the above function to a package
Tasks
Use RMD to knit your final HTML document. Upload both files to canvas at the end.
Note: All plots you are asked to make should be recorded through
the correct use of RMD.
• Task 1
o Make a folder LAB6
o Download the file “lab6.r”
o Place this file with the others in LAB6.
o Start Rstudio
o Open “lab6.r” from within Rstudio.
o Go to the “session” menu within Rstudio and “set working directory” to where the source
files are located.
o Issue the function getwd().
• Task 2
o Open Lab 6.R and make space at the top for the task code needed for the current lab.
o Use the hash # symbol and write your own comments in the code file explaining what the
code does.
o Use layout() to construct a plotting region that will take 4 plots.
o Use it to plot 4 normal curves with the following parameters. Hint: You will need to
adjust the xlim option to show all of what is needed.
▪ 𝜇 = 10, 𝜎 = 4
▪ 𝜇 = 10, 𝜎 = 2
▪ 𝜇 = 5, 𝜎 = 10
▪ 𝜇 = 5, 𝜎 =
1
o Plot the following regions and probabilities using one graphical interface per plot (use 4
dec. places)
▪ 𝑌 ∼ 𝑁(0, 1), 𝑃(𝑌 ≥ 2)
▪ 𝑌 ∼ 𝑁(𝜇 = 4, 𝜎 = 2), 𝑃(1 ≤ 𝑌 < 5)
▪ 𝑌 ∼ 𝑁(𝜇 = 10, 𝜎 = 4), 𝑃(𝑌 < 10)
▪ 𝑌 ∼ 𝑁 (𝜇 = −2, 𝜎 =
1
2
) , 𝑃(−3 < 𝑌 ≤ −2)
• Task 3
o We will now study the gamma distribution. This has two parameters, shape (>0) and
scale (>0). You will use dgamma() and pgamma()
o Notice that the random variable Y takes only positive values.
o On the one plotting surface overlay the following three plots (NB – Don’t use layout)
▪ 𝑌 ∼ 𝐺𝑎𝑚𝑚𝑎(𝑠ℎ𝑎𝑝𝑒 = 1, 𝑠𝑐𝑎𝑙𝑒 = 1)
▪ 𝑌 ∼ 𝐺𝑎𝑚𝑚𝑎(𝑠ℎ𝑎𝑝𝑒 = 3, 𝑠𝑐𝑎𝑙𝑒 = 1)
▪ 𝑌 ∼ 𝐺𝑎𝑚𝑚𝑎(𝑠ℎ𝑎𝑝𝑒 = 5, 𝑠𝑐𝑎𝑙𝑒 = 1)
o Plot the following regions and probabilities in R
▪ 𝑌~𝐺𝑎𝑚𝑚𝑎(𝑠ℎ𝑎𝑝𝑒 = 3, 𝑠𝑐𝑎𝑙𝑒 = 2), 𝑃(2 < 𝑌 < 5)
▪ 𝑌 ∼ 𝐺𝑎𝑚𝑚𝑎(𝑠ℎ𝑎𝑝𝑒 = 6, 𝑠𝑐𝑎𝑙𝑒 = 3), 𝑃(1 ≤ 𝑌 ≤ 4)
▪ 𝑌 ∼ 𝐺𝑎𝑚𝑚𝑎(𝑠ℎ𝑎𝑝𝑒 = 2, 𝑠𝑐𝑎𝑙𝑒 = 4), 𝑃(3 ≤ 𝑌 < 6)
• Task 4
o The chi-square distribution is a special case of the gamma with shape = 𝜈
2
and scale =2,
where 𝜈 is called the number of degrees of freedom.
o Use layout() and break up the graphical interface so that there are four squares to plot the
following on:
▪ 𝑌 ∼ 𝑐ℎ𝑖𝑠𝑞(𝑑𝑓 = 1)
▪ 𝑌 ∼ 𝑐ℎ𝑖𝑠𝑞(𝑑𝑓 = 2)
▪ 𝑌 ∼ 𝑐ℎ𝑖𝑠𝑞(𝑑𝑓 = 4)
▪ 𝑌 ∼ 𝑐ℎ𝑖𝑠𝑞(𝑑𝑓 = 20)
o Plot the following regions and probabilities in R on separate plotting surfaces.
▪ 𝑌 ∼ 𝑐ℎ𝑖𝑠𝑞(𝑑𝑓 = 2), 𝑃(2 ≤ 𝑌 ≤ 4)
▪ 𝑌 ∼ 𝑐ℎ𝑖𝑠𝑞(𝑑𝑓 = 3), 𝑃(3 ≤ 𝑌 ≤ 5)
▪ 𝑌 ∼ 𝑐ℎ𝑖𝑠𝑞(𝑑𝑓 = 20), 𝑃(10 < 𝑌 ≤ 21)
• Task 5
o The Weibull measures length of life like a gamma. 0 ≤ 𝑌 < ∞ and it has two parameters,
shape and scale both > 0.
o Use dweibull() and pweibull().
o Make four plots as above with any shape and scale combinations you wish (Using
layout()).
o Calculate any three areas and probabilities you choose and show them on the graphs as
above (separate plots).
• Task 6
o Investigate the beta distribution 0 ≤ 𝑌 ≤ 1, it has two parameters.
o Make sure you understand the parameters by using ?dbeta in R
o Repeat task 5 for the beta distribution.
• Task 7
o Look at the function myncurve()below and edit it so that it will display the curve,
shaded area between the curve and x axis from −∞ to x=a, and calculate the area
(probability, P(X<=a)) which is released to the command-line in a list.
myncurve = function(mu, sigma){
curve(dnorm(x,mean=mu,sd=sigma), xlim = c(mu-3*sigma, mu +
3*sigma))
}
o Add the completed function to your package – you can consult http://r-pkgs.had.co.nz/
for more information about building R packages.
o Build and install the package
o And run the function in an R chunk (part of Lab6.Rmd) to prove the package is working
by calculating
ILAS2019::myncurve(mu=10,sigma=5, a=6)
################### LAB FINISHES HERE ###############################
• Task 8 – Extra for experts
o Write some code that will use the gamma density to produce exponential densities. Check
the results by using the built in dexp() function.