GreenBlueBar.gif GreenBlueBar.gif

R Code for Chapter 8

Sampling Distributions and Hypothesis Testing


The first block of code will generate Figure 8.1 by drawing many many samples of a fixed size, calculating the mean and the st. dev. of means for each sample, and plotting the results. Figures 8.2 and 8.3 can be plotted with the same general code with obvious modifications.

When it comes to Figure 8.4, the code goes a ways beyond the text. It plots a histogram of the obtained data whose mean is 59, and then overplots with a normal distribution with a mean of 50. This gives you a sense of how different we would expect means from a population with μ = 59 to be from a distribution with μ = 50. I have included a lot of stuff that will annotate and do pretty things with the text. Note that I used polygon() as I did earlier.

### Figure 8.1
### Sampling distribution of the mean   
 
par(mfrow = (c(1,1)))
nreps <- 10000                 # Number of replications
n <- 50                               # Size of each sample
xbar = numeric(nreps)    # Set aside space to store the means
for (i in 1:nreps) {
   x <- rnorm(n = n,mean = .667, sd = 0.88) 
   xbar[i] <- mean(x)
}
   ### This has drawn 10,000 samples of size 50 from a normal distribution with 
   ### mean = 0.667 and st. dev = 0.88
meanxbar <- round(mean(xbar), digits = 3)  # Make the numbers neater  
semean <-  round(sd(xbar), digits = 3) 
cat("The mean of the sampling distribution is ",meanxbar, "\n")
                # the "\n" tells it to goto a new line.
cat("The standard error of the mean is ", semean, "\n")
hist(xbar, col = "#22748A", xlab = "Sample Mean",main = "Histogram of Sample Means" )
   ### col = #22748A is a dark green color -- you can try other values
legend(0.85, 800, paste("Mean = ", meanxbar, "\nSt. Dev = ", semean,"\nn = ",n), bty = "n")
   ### "paste" allows me to combine quoted text and numbers that have been 
   ### calculated in the code.

### Finger tapping   Figure 8.4
### Shows distribution of raw data and normal distribution when mu = 50
x <- rnorm(5000, 59, 7)
x <- sort(x)
y <- dnorm(x, 50, 7)
plot(x,y, type = "l", xlab = "Tapping Speed" , ylab = "" , xlim = c(30, 90), 
  ylim = c(0,.055))
polygon(c(30,x[x<=45], 45), c(0,dnorm( x[x <= 45], 50, 7),0), col = "gray")
par(new = TRUE)
hist(x,xlim = c(30, 90), ylim = c(0, .055), freq = FALSE,
 xlab = "", main = "Histogram of Tapping Rate", density = 5)
arrows(33,.02,45,-.002)
text(33, .02, "cutoff at 45")
text(35,.04, "p = .0228" )
arrows(35, .038, 43, .025)
text(33, .05, "Normal\nmu = 50")
arrows(33, .048, 42, .048)
text(73, .05, "Raw Data\nxbar = 59")
arrows(70, .048, 65, .048)


Notice the last line in the next bit of code. It uses the "expression" command. When you get into this area of R things can get very messy with commands like "expression", "paste", "bquote", "substitute", and others. I have no intention of trying to explain "expression," but it is worth noticing how I used it to write a Greek alpha (α). "Paste" pastes that alpha and some text (= .05) together. You will see "expression" used again, but my advice is to make yourself a list of notes and examples. For example, "To put a Greek character on a plot use the following ..." For now it is better to steal my code and modify it to your needs than it is to work your way through all of that stuff. You can also go to R and type ?plotmath and find more information.


###  Figure 8.3

x <- seq(20,89,by =.1)  
z <- (x-59)/7      
ht<- dnorm(z)
plot(x, ht, type="l",  ylab="density",  xlab="Tapping Rate",xlim = c(20, 90), 
ylim = c(0,.4))
   # Now define the shaded area
polygon(c(x[x <= 45],45), c( ht[x <= 45],0), col = "red")
arrows(51.083, 0.078,45, 0.00)
text(51.083, 0.09, "45")
arrows(40, 0.078, 43, .005)
text(39,.1, "area = \n.023")
text(30, 0.150, expression(paste(alpha, " = .05")))

I wrote a whole bunch of code that I am not showing you. It plotted various figures in the chapter and did some other stuff. But I think that you can get along happily without it. It is included in the Chapter8.R file if you would like to play with it.

GreenBlueBar.gif GreenBlueBar.gif dch: