13 Samples and Surveys

The Analytics examples in this chapter are conceptual, so there’s no need for R. That gives me the chance to illustrate how to draw samples in R. We covered simulating samples from distributions associated with random variables in prior chapters, particularly Chapter 12 for the normal distribution. There’s a different function, sample, that is used to obtain random samples from the collection of values in a vector (or a data frame).

Suppose you would like to draw a simple random sample (SRS) from a list of friends or classmates. I will give your friends very short names – letters from the alphabet – to make this easier to type.

names <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
           'i','j','k', 'l', 'm','n','o','p','q',
           'r','s','t','u','v','w','x','y','z')

Rather that writing these names on pieces of paper then drawing them at random to get an SRS, we can use the function sample instead.

sample(names, 10)    # draw an SRS of 10 from this collection
##  [1] "k" "x" "l" "c" "z" "u" "s" "i" "p" "f"

As discussed in the textbook, it’s easy to get an SRS once you have the appropriate sampling frame.