Inference for a Single Mean with R

This example uses the speeds on Triphammer Road from De Veaux, Velleman and Bock, Stats.: Data and Models 2nd ed., 2008, Addison Wesley, Boston. (It's the first example in Chapter 23.) Police measured traffic speeds (in miles per hour) on a road where this was a concern. Here are the results.

      29   34   34   28   30   29   38   31   29   34   32   31   27   37   29
      26   24   34   36   31   34   36   21

The R scan function allows you to enter data without typing commas. In the case below, the values were not actually typed but inserted with cut and paste. The "24:" prompt means R has received 23 numbers and is waiting for the 24th. Hit RETURN to cease data entry.

> speeds = scan()
1:    29   34   34   28   30   29   38   31   29   34   32   31   27   37   29   26   24   34   36   31   34   36   21
24: 
Read 23 items 

> stem(speeds)

  The decimal point is 1 digit(s) to the right of the |

  2 | 14
  2 | 6789999
  3 | 0111244444
  3 | 6678

> stem(speeds, scale=2)

  The decimal point is at the |

  20 | 0
  22 | 
  24 | 0
  26 | 00
  28 | 00000
  30 | 0000
  32 | 0
  34 | 00000
  36 | 000
  38 | 0

> summary(speeds)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  21.00   29.00   31.00   31.04   34.00   38.00 

> t.test(speeds, mu=30, alternative="greater", conf.level=0.90)

        One Sample t-test

data:  speeds 
t = 1.1781, df = 22, p-value = 0.1257
alternative hypothesis: true mean is greater than 30 
90 percent confidence interval:
 29.87323      Inf 
sample estimates:
mean of x 
 31.04348 

Note that a single command returns both a hypothesis test and a confidence interval and that one-sided tests return one-sided confidence intervals (as they should). The confidence level must be specified as a number between 0 and 1. Another alternative for alternative is "less". Leaving it out gives a two-sided test/interval. The default mu is 0 and the default confidence level 95%=0.95. In the textbook example, the question was whether the average speed exceeded 30 miles per hour so that's what we tested. We could question the result on two grounds. First, the stem and leaf shows the data bimodal and skewed toward low values (or is that an outlier?), and checking the mean may not be the appropriate tool here. Second, even though the average speed was close to 30, we can note that a majority of the vehicles were exceeding the 30 MPH speed limit.


© 2008 statistics.com. Portions © 2006-2007 Robert W. Hayden and used by permission.