R Practical Exercise 3

advertisement
R Exercise Practical 3
Follow the instructions described in this sheet. Type or paste any answers you are asked for
into a Microsoft Word document.
Note: You should have this sheet on your computer for reference and to copy and paste some
text.
1.
A coin is given 10 independent tosses, each of which lands heads with probability a.
Let the random variable N denote the total number of heads obtained. Then
 10 
p( N  x )    a x (1  a )10 x
x
Given a and any value (or vector of values!) of x, we can calculate this in R as
> dbinom(x,10,a)
Set a = 0.3 (not a very fair coin!) and verify that the above equation and the R command
agree for x=2.
(i.e. type dbinom(2,10,0.3) )
Generate the entire probability function of the random variable N with
> pf = dbinom(0:10,10,0.3)
Use sum to verify that the sum of these probabilities is 1, and plot a bar chart to
show their distribution with
> barplot(pf,names=0:10)
What are the first, second, and third most likely numbers of heads? Calculate the
mean and standard deviation of this distribution with
> mn = sum(0:10*pf)
> stdev = sqrt(sum((0:10)^2*pf) - mn^2)
(to see them, type in mn and stdev after you have done that).
Verify that the values obtained agree with those obtained from your own knowledge of the
binomial distribution (i.e. with the standard formulae for mean and stdev of binomial).
Now, suppose that we are unable to do exact calculations with the Binomial distribution.
Simulate 1000 realisations of the random variable N with
> heads = rbinom(1000,10,0.3)
Draw a histogram of the distribution of heads with hist(heads), or better with
> hist(heads, breaks=seq(-1,10))
(which fixes a slight glitch with plotting histograms of counts).
Compare it with the bar chart of the probability function obtained earlier. Use the functions
mean and sd to calculate also the mean and standard deviation of the distribution of heads,
and compare these with the mean and standard deviation for the theoretical distribution
obtained earlier.
2.
This question confirms the Central Limit Theorem for the sum of Uniform Distributions.
Let X1, X2 ……X9 come from the Uniform distribution, U(5,10).
Recall that for any uniform distribution on [A,B], the mean µ is equal to (A+B)/ 2 and the
variance, σ2, is equal to (B-A)2/12.
Manually calculate the mean and variance of X1 (and hence of X2 ……X9 too).
Now Let S = X1+ X2 +…+ X9
From elementary probability theory, what will the mean and variance of S be?
Now show that S comes from a normal distribution with mean and variance just calculated.
Do this by simulating 10 000 results in each of 9 columns as follows:
> x1=runif(10000,5,10)
> x2=runif(10000,5,10)
…………………………
> x9=runif(10000,5,10)
> S=x1+x2+x3+x4+x5+x6+x7+x8+x9
Verify that the individual simulations look as you would expect for a uniform distribution (use
hist(x1) , etc.).
Obtain a histogram of S and thus give reasons whether or not the Central Limit Theorem can
be confirmed here (calculate the mean and standard deviation of S).
Download