next up previous
Next: About this document ...

R Lab Session : Part 2

To see a review of how to start R, look at the beginning of Lab1

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
(ii)
is greater than 29
(iii)
is less than 17
(iv)
is less than 15 or greater than 25
2.
Probability that in 60 tosses of a fair coin the head comes up
(i)
20,25 or 30 times
(ii)
less than 20 times
(iii)
between 20 and 30 times
3.
A random variable X has Poisson distribution with mean 7. Find the probability that
(i)
X is less than 5
(ii)
X is greater than 10
(iii)
X is between 4 and 16

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 .

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. 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 ?




next up previous
Next: About this document ...
Elizabeth Purdom 2003-10-13