You are on page 1of 1

Some demonstrations from class on Monday, June 27.

The following code takes 100 samples of size 100 from the gamma distribution with
parameters 5 and 1, and then estimates the parameters from those samples and plots
the sampling distribution.

alphas = rep(0,100)
lambdas = rep(0,100)
for (i in c(1:100)) {s <- rgamma(100,5,1); mu1 <- sum(s)/100; mu2 <-
sum(s^2)/100; lambdas[i] = mu1/(mu2-mu1^2); alphas[i] =
plot(alphas, lambdas)

This is similar code for the Rayleigh distribution. (To generate a random Rayleigh-
distributed random variable I generate two independent normals and then take the
square root of the sum of their squares; this is easier to write than loading in
the package which contains the Rayleigh distribution.) For each sample two
estimators are generated: meanest[i] is the estimator based on the mean, varest[i]
is the estimator based on the second moment. You can experiment with this data to
see that the estimator "varest" has slightly lower standard error, as discussed in

n = 1000; #sample size

r = 1000; #number of samples
meanest = rep(0,r); varest = rep(0,r);
for (i in (1:r)) {S = sqrt(rnorm(n)^2+rnorm(n)^2); meanest[i] =
mean(S)*sqrt(2/pi); varest[i] = sqrt(mean(S^2)/2)}

This is something I didn't really get to talk about in class.

What does 576 draws from a Poisson with parameter 0.9323 look like?

S = rpois(576,.9323)
hist(S, breaks=seq(-0.5,6.5,1)) #might have to change upper limit of 6.5
lambda = mean(S)

Now once we have a sample like this, we estimate its lambda, and then we can take
many samples of the same size from Poissons with that new lambda, which will be
near 0.9323 but probably not exactly that. We make a histogram of the sampling

lambdas = rep(0,1000)
for (i in (1:1000)) {T = rpois(576,lambda); lambdas[i] = mean(T)}

Finally we can find the mean and standard deviation, and a 95 percent confidence
interval for lambda based on this particular sample.

mean(lambdas); sd(lambdas);
mean(lambdas)+qnorm(.025)*sd(lambdas); mean(lambdas)+qnorm(.975)*sd(lambdas)