Note # 3
Objectives:
1. To introduce a number of probability distributions in R
2. To calculate probability in various probability distribution using R
Task # 1
In class we learnt about PDF and CDF
> help (Distributions) # list all available distributions in R
We will discuss: normal(norm), Student-t (t), Khi-Kuasa dua(chisq), F(f)
Four useful commands in R
Normal Distribution
> dnorm() # gives PDF
> pnorm() # gives CDF
> qnorm() # gives standard normal variate Z or ordinary normal X given a
probability
> rnorm() # general normally distributed random numbers
Example:
> dnorm(0)
[1] 0.3989423
The height of pdf at z=0 is 0.3989423.
> z=seq(-3,3,by=0.05) # gives z values from -3.0 to 3.0 with 0.05 increment
> y=dnorm(z) # gives heights of z values from -3.0 to 3.
> plot(z,y) # plotting z vs. y.
>pnorm(Z) # gives CDF
> args(pnorm) # gives you syntax on how to use this function
> args(pnorm)
function (q, mean = 0, sd = 1, lower.tail = TRUE, log.p = FALSE)
lower.tail = TRUE mean the probability is summed from left of Z whereas
lower.tail = FALSE means probability is summed from right of Z.
Try to get these values:
i) p(-∞ < z <=0)
ii) p(+∞ > z >=0)
iii) p(z < = 0.5)
iv) p( z > = 0.5)
v) p ( X > 3.5) if X ~ N(3, 1.5) (use two methods 1) directly using
pnorm() of non-standard normal and pnorm() of standard normal)
vi) p( >= 4.5 ) if a sample of size n=15 is taken from a population
of N(5, 1.5) (use two methods 1) directly using pnorm() of non-
standard normal and pnorm() of standard normal)
> qnorm(0.5) # gives Zo so that p(-∞ < z < Zo) =0.5. In this case Zo=0.
[1] 0
> qnorm(0.75) # 3 rd quantile
> qnorm(0.25) # 1st quantile
Try to get values of Zo or Xo from:
i) p( - ∞ < z < Zo)=0.7
ii) p( Zo < z < +∞ ) = 0.42
iii) p (- ∞ < X < Xo)=0.7 jika X ~ N(3, 1.5)
vii) p( >= Xo)= 0.75 if a sample of size n=15 is taken from N(5, 1.5)
(use two methods 1) directly using pnorm() of non-standard
normal and pnorm() of standard normal)
> zsample=rnorm(100) # generate random sample of saiz 100 from standard
normal
> hist(zsample) # plot the histogram
> xsample=rnorm(100, mean=5, sd=1.5) # generate random sample of size n=
100 from normal distribution of mean=5 and std=1.5)
> hist(xsample) # historgram a sample from normal distribution of mean=5 dan
sd=1.5
Student t Distribution
> dt(0,df=3) # PDF of t-dist with df=3
Compe t-distribution with different values of df e.g. df=10, 20, 50, 100 and
standard normal
> x=seq(-5,5,by=0.1) # generate values [-5:-1:5]
> y=dt(x,df=3) # PDF for t with df=3
> y1= dt(x,df=10)
> y2= dt(x,df=20)
> y3= dt(x,df=50)
> y4= dt(x,df=100)
> y5= dnorm(x)
> plot(x,y);lines(x,y) # plot x vs y and plot a line.
> lines(x,y1,col=’red’) # plot values of x vs y1 in same plot with red line
> lines(x,y2,col='green')
> lines(x,y3,col='blue')
> lines(x,y4,col='yellow')
> lines(x,y5,col='magenta')
Compares those different t-distribution and also with standard normal.
> pt(0,df=3) # CDF
> pt(0, df=3, lower.tail=TRUE)
> pt(0.5, df=3, lower.tail=TRUE)
Find these values:
i) p(-∞ < t <=0) for t(df=10)
ii) p(+∞ > t >=0) for t(df=10)
iii) p(t < = 0.5), for t(df=10)
iv) p( t > = 0.5), for t(df=10)
v) p ( X > 3.5) if X ~ N(3, σ 2), a sample of n= 20 taken with sample
variance of s2=1.2325
> qt(0.5, df=3) # What is the value of this?
> qt(0.25, df=3)
> qt (0.75, df=3)
> r=rt(100,df=20) # what is the value of this?
> hist(r)
Students can try chisq and f distribution.