R Lab Session : Part 2
(Solutions)

To see a review of how to start R, look at the beginning of Lab1
Lab1 http://www-stat.stanford.edu/ epurdom/RLab.htm

Probability Calculations

The following examples demonstrate how to calculate the value of the cumulative distribution function at (or the probability to the left of) a given number.

Exercise : Calculate the following probabilities :

1.
Probability that a normal random variable with mean 22 and variance 25
(i)
lies between 16.2 and 27.5
pnorm(27.5,22,sd=5)-pnorm(16.2,22,sd=5)
[1] 0.7413095
(ii)
is greater than 29 1-pnorm(29,22,sd=5)
[1] 0.08075666
(iii)
is less than 17 pnorm(17,22,sd=5)
[1] 0.1586553
(iv)
is less than 15 or greater than 25 pnorm(15,22,sd=5)+1-pnorm(25,22,sd=5)
[1] 0.3550098
2.
Probability that in 60 tosses of a fair coin the head comes up
(i)
20,25 or 30 times
sum(dbinom(c(20,25,30),60,prob=0.5))
[1] 0.1512435
(ii)
less than 20 times
pbinom(19,60,prob=0.5)
[1] 0.0031088
(iii)
between 20 and 30 times pbinom(30,60,prob=0.5)-pbinom(20,60,prob=0.5)
[1] 0.5445444
3.
A random variable X has Poisson distribution with mean 7. Find the probability that
(i)
X is less than 5 less or equal is:
> ppois(5,7)
[1]0.3007083
less than is
> ppois(4,7)
[1]0.1729916
(ii)
X is greater than 10 (strictly)
> 1-ppois(10,7) [1] 0.0985208
(iii)
X is between 4 and 16 > ppois(16,7)-ppois(3,7) [1] 0.9172764

Quantiles

The following examples show how to common the quantiles of some common distributions for a given probability (or a number between 0 and 1).

Random Variable generation

The following examples illustrate how to generate random samples from some of the well-known probability distributions.

Exercise (Advanced) : Generate 500 samples from Student's $t$ distribution with 5 degrees of freedom and plot the historgam. (Note: $t$ distribution is going to be covered in class). The corresponding function is rt . hist(rt(500,5),40)

Density Plots

** Note the distinction between the continuous (Normal) and the discrete (Binomial) distrubtions.

Exercise : Plot the probability mass functions for the Poisson distribution with mean 4.5 and 12 respectively. Do you see any similarity of these plots to any of the plots above? If so, can you guess why ?

Exercise : Recreate the probabilities that Professor Holmes did in class (Bin(5,.4)) [You can do it in 1 command!] How would you get the expected counts?

Q-Q plot

R has two different functions that can be used for generating a Q-Q plot. Use the function qqnorm for plotting sample quantiles against theoretical (population) quantiles of standard normal random variable.

Example :

    > stdnormsamp <-rnorm(100,mean=0,sd=1)
    > normsamp <- rnorm(100,mean=5,sd=1)
    > binomsamp <-rbinom(100,size=20,prob=.25)
    > poissamp <- rpois(100,5)

    > par(mfrow=c(2,2))

    > qqnorm(stdnormsamp,main="Normal Q-Q plot : N(0,1) samples")
    > qqline(stdnormsamp,col=2)
    > qqnorm(normsamp,main="Normal Q-Q plot : N(5,1) samples")
    > qqline(normsamp,col=2)
    > qqnorm(binomsamp,main="Normal Q-Q plot : Bin(20,.25) samples")
    > qqline(binomsamp,col=2)
    > qqnorm(poissamp,main="Normal Q-Q plot : Poisson(5) samples")
    > qqline(poissamp,col=2)

Note: Systematic departure of points from the Q-Q line (the red straight line in the plots) would indicate some type of departure from normality for the sample points.

Use of function qqplot for plotting sample quantiles for one sample against the sample quantiles of another sample

Example :

    > par(mfrow=c(2,1))

    > qqplot(stdnormsamp,normsamp,xlab = "Sample quantiles : N(0,1) samples",
    + ylab = "Sample quantiles : N(5,1) samples")
    > qqplot(stdnormsamp,binomsamp,xlab = "Sample quantiles : N(0,1) samples",
    + ylab = "Sample quantiles : Bin(20,.25) samples")

Exercise : Generate 100 samples from Student's $t$ distribution with 4 degrees of freedom and generate the qqplot for this sample.
qqnorm(rt(100,df=4))Generate another sample of same size, but now from a $t$ distribution with 30 degrees of freedom and generate the q-q plot. Do you see any difference ?
qqnorm(rt(100,df=30))

It should be evident to you that the t distribution is very far from normal, and the 30 degrees of freedom t is indistinguishable from Normal.


Susan Holmes 2004-10-31