Getting Started

Load packages

In this lab, we will explore and visualize the data using the tidyverse suite of packages, and perform statistical inference using infer. The data can be found in the companion package for OpenIntro resources, openintro.

Let’s load the packages.

library(tidyverse)
library(openintro)
library(infer)

The data

You will be analyzing the same dataset as in the previous lab, where you delved into a sample from the Youth Risk Behavior Surveillance System (YRBSS) survey, which uses data from high schoolers to help discover health patterns. The dataset is called yrbss.

  1. What are the counts within each category for the amount of days these students have texted while driving within the past 30 days?

Insert your answer here

glimpse(yrbss)
## Rows: 13,583
## Columns: 13
## $ age                      <int> 14, 14, 15, 15, 15, 15, 15, 14, 15, 15, 15, 1…
## $ gender                   <chr> "female", "female", "female", "female", "fema…
## $ grade                    <chr> "9", "9", "9", "9", "9", "9", "9", "9", "9", …
## $ hispanic                 <chr> "not", "not", "hispanic", "not", "not", "not"…
## $ race                     <chr> "Black or African American", "Black or Africa…
## $ height                   <dbl> NA, NA, 1.73, 1.60, 1.50, 1.57, 1.65, 1.88, 1…
## $ weight                   <dbl> NA, NA, 84.37, 55.79, 46.72, 67.13, 131.54, 7…
## $ helmet_12m               <chr> "never", "never", "never", "never", "did not …
## $ text_while_driving_30d   <chr> "0", NA, "30", "0", "did not drive", "did not…
## $ physically_active_7d     <int> 4, 2, 7, 0, 2, 1, 4, 4, 5, 0, 0, 0, 4, 7, 7, …
## $ hours_tv_per_school_day  <chr> "5+", "5+", "5+", "2", "3", "5+", "5+", "5+",…
## $ strength_training_7d     <int> 0, 0, 0, 0, 1, 0, 2, 0, 3, 0, 3, 0, 0, 7, 7, …
## $ school_night_hours_sleep <chr> "8", "6", "<5", "6", "9", "8", "9", "6", "<5"…
# Amount of days the students texted while driving within past 30 days. Ignore the ones who didn't drive.
yrbss %>%
  count(text_while_driving_30d) %>%
  filter(text_while_driving_30d > 0 & text_while_driving_30d != "did not drive") %>%
  mutate(p_textdrive = n /sum(n)) %>%
  arrange(desc(p_textdrive))
## # A tibble: 6 × 3
##   text_while_driving_30d     n p_textdrive
##   <chr>                  <int>       <dbl>
## 1 1-2                      925      0.287 
## 2 30                       827      0.256 
## 3 3-5                      493      0.153 
## 4 10-19                    373      0.116 
## 5 6-9                      311      0.0964
## 6 20-29                    298      0.0923
  1. What is the proportion of people who have texted while driving every day in the past 30 days and never wear helmets?

Insert your answer here

yrbss %>%
  count(text_while_driving_30d, helmet_12m) %>%
  filter(text_while_driving_30d == 30) %>%
  mutate(p_textdrive = n /sum(n)) %>%
  arrange(desc(p_textdrive))
## # A tibble: 7 × 4
##   text_while_driving_30d helmet_12m       n p_textdrive
##   <chr>                  <chr>        <int>       <dbl>
## 1 30                     never          463     0.560  
## 2 30                     did not ride   295     0.357  
## 3 30                     rarely          29     0.0351 
## 4 30                     always          13     0.0157 
## 5 30                     <NA>            13     0.0157 
## 6 30                     sometimes       10     0.0121 
## 7 30                     most of time     4     0.00484
Of the people who’ve driven and texted every day in the past 30 days, 463/827 (56%) never wore a helmet. Very irresponsible.

Remember that you can use filter to limit the dataset to just non-helmet wearers. Here, we will name the dataset no_helmet.

data('yrbss', package='openintro')

no_helmet <- yrbss %>%
  filter(helmet_12m == "never")

Also, it may be easier to calculate the proportion if you create a new variable that specifies whether the individual has texted every day while driving over the past 30 days or not. We will call this variable text_ind.

no_helmet <- no_helmet %>%
  mutate(text_ind = ifelse(text_while_driving_30d == "30", "yes", "no"))

Inference on proportions

When summarizing the YRBSS, the Centers for Disease Control and Prevention seeks insight into the population parameters. To do this, you can answer the question, “What proportion of people in your sample reported that they have texted while driving each day for the past 30 days?” with a statistic; while the question “What proportion of people on earth have texted while driving each day for the past 30 days?” is answered with an estimate of the parameter.

The inferential tools for estimating population proportion are analogous to those used for means in the last chapter: the confidence interval and the hypothesis test.

no_helmet %>%
  drop_na(text_ind) %>%
  specify(response = text_ind, success = "yes") %>%
  generate(reps = 1000, type = "bootstrap") %>%
  calculate(stat = "prop") %>%
  get_ci(level = 0.95)
## # A tibble: 1 × 2
##   lower_ci upper_ci
##      <dbl>    <dbl>
## 1   0.0652   0.0777

Note that since the goal is to construct an interval estimate for a proportion, it’s necessary to both include the success argument within specify, which accounts for the proportion of non-helmet wearers than have consistently texted while driving the past 30 days, in this example, and that stat within calculate is here “prop”, signaling that you are trying to do some sort of inference on a proportion.

  1. What is the margin of error for the estimate of the proportion of non-helmet wearers that have texted while driving each day for the past 30 days based on this survey?

Insert your answer here

# Margin of error =  z * σ/sqrt(n) where z= z-score, σ = standard deviation of population, n = sample size.
# For a 95% confidence interval, the z-score is 1.96.
# For the non-helmet wearers, the sample is 6977 from the original 13583 yrbss dataset. 
# Of all those non-helmet wearers, 463 drove every day.

no_helmet_drive_everyday <- yrbss %>%
  filter(helmet_12m == "never" & text_while_driving_30d == "30")

ci_lower1 <- 0.065
ci_upper1 <- 0.077

error_margin1 <- (ci_upper1 - ci_lower1) / 2
error_margin1
## [1] 0.006
The error of margin here is 0.006
  1. Using the infer package, calculate confidence intervals for two other categorical variables (you’ll need to decide which level to call “success”, and report the associated margins of error. Interpret the interval in context of the data. It may be helpful to create new data sets for each of the two countries first, and then use these data sets to construct the confidence intervals.

Insert your answer here

# How many non-helmet wearers are active at least 4 days a week

no_helmet_active <- no_helmet %>%
  mutate(text_ind = ifelse(physically_active_7d >= 4, "yes", "no"))

no_helmet_active %>%
  drop_na(text_ind) %>%
  specify(response = text_ind, success = "yes") %>%
  generate(reps = 1000, type = "bootstrap") %>%
  calculate(stat = "prop") %>%
  get_ci(level = 0.95)
## # A tibble: 1 × 2
##   lower_ci upper_ci
##      <dbl>    <dbl>
## 1    0.583    0.607
ci_lower2 <- 0.583
ci_upper2 <- 0.606

error_margin2 <- (ci_upper2 - ci_lower2) / 2
error_margin2
## [1] 0.0115
# Margin of error is 0.0115


#-------------------------------------------------------------------------------

# How many non-helmet wearers get at least 7 hours of sleep a night 
no_helmet_sleep <- no_helmet %>%
  mutate(text_ind = ifelse(school_night_hours_sleep >= 7, "yes", "no"))

no_helmet_sleep %>%
  drop_na(text_ind) %>%
  specify(response = text_ind, success = "yes") %>%
  generate(reps = 1000, type = "bootstrap") %>%
  calculate(stat = "prop") %>%
  get_ci(level = 0.95)
## # A tibble: 1 × 2
##   lower_ci upper_ci
##      <dbl>    <dbl>
## 1    0.546    0.570
ci_lower3 <- 0.546
ci_upper3 <- 0.571

error_margin3 <- (ci_upper3 - ci_lower3) / 2
error_margin3
## [1] 0.0125
# Margin of error is 0.0125
Contextually, the data is 95% confident that the proportion of non-helmet wearers who are active at least 4 days a week, is between 0.583 and 0.606, with a margin of error of 0.0115.
We are also 95% confident that the proportion of non-helmet wearers who get at least 7 hours of sleep a night is between 0.546 and 0.571, with a margin of error of 0.0125.

How does the proportion affect the margin of error?

Imagine you’ve set out to survey 1000 people on two questions: are you at least 6-feet tall? and are you left-handed? Since both of these sample proportions were calculated from the same sample size, they should have the same margin of error, right? Wrong! While the margin of error does change with sample size, it is also affected by the proportion.

Think back to the formula for the standard error: \(SE = \sqrt{p(1-p)/n}\). This is then used in the formula for the margin of error for a 95% confidence interval:

\[ ME = 1.96\times SE = 1.96\times\sqrt{p(1-p)/n} \,. \] Since the population proportion \(p\) is in this \(ME\) formula, it should make sense that the margin of error is in some way dependent on the population proportion. We can visualize this relationship by creating a plot of \(ME\) vs. \(p\).

Since sample size is irrelevant to this discussion, let’s just set it to some value (\(n = 1000\)) and use this value in the following calculations:

n <- 1000

The first step is to make a variable p that is a sequence from 0 to 1 with each number incremented by 0.01. You can then create a variable of the margin of error (me) associated with each of these values of p using the familiar approximate formula (\(ME = 2 \times SE\)).

p <- seq(from = 0, to = 1, by = 0.01)
me <- 2 * sqrt(p * (1 - p)/n)

Lastly, you can plot the two variables against each other to reveal their relationship. To do so, we need to first put these variables in a data frame that you can call in the ggplot function.

dd <- data.frame(p = p, me = me)

ggplot(data = dd, aes(x = p, y = me)) + 
  geom_line() +
  labs(x = "Population Proportion", y = "Margin of Error")

  1. Describe the relationship between p and me. Include the margin of error vs. population proportion plot you constructed in your answer. For a given sample size, for which value of p is margin of error maximized?

Insert your answer here

As our proportion (p) increases, the margin of error (me) increases as well, until it reaches a maximum value when p = 0.5, then begins to decrease at the same rate of its prior increase. The margin of error is maximized when p = 0.5

Success-failure condition

We have emphasized that you must always check conditions before making inference. For inference on proportions, the sample proportion can be assumed to be nearly normal if it is based upon a random sample of independent observations and if both \(np \geq 10\) and \(n(1 - p) \geq 10\). This rule of thumb is easy enough to follow, but it makes you wonder: what’s so special about the number 10?

The short answer is: nothing. You could argue that you would be fine with 9 or that you really should be using 11. What is the “best” value for such a rule of thumb is, at least to some degree, arbitrary. However, when \(np\) and \(n(1-p)\) reaches 10 the sampling distribution is sufficiently normal to use confidence intervals and hypothesis tests that are based on that approximation.

You can investigate the interplay between \(n\) and \(p\) and the shape of the sampling distribution by using simulations. Play around with the following app to investigate how the shape, center, and spread of the distribution of \(\hat{p}\) changes as \(n\) and \(p\) changes.

  1. Describe the sampling distribution of sample proportions at \(n = 300\) and \(p = 0.1\). Be sure to note the center, spread, and shape.

Insert your answer here

The sampling distribution vaguely resembles a normal distribution, albeit with some odd peaks that aren’t close to the actual max value of the plot. The center is at 0.1, with very little spread due to having a good sample size.
  1. Keep \(n\) constant and change \(p\). How does the shape, center, and spread of the sampling distribution vary as \(p\) changes. You might want to adjust min and max for the \(x\)-axis for a better view of the distribution.

Insert your answer here

As p changes, the distribution retains its shape of being somewhat close to a normal distribution, with some breaks in its smoothness.The center is what we define our population proportion (p) to be. However, as we saw in the prior plot, we know that as p approaches 0.5, there is a larger margin of error, so we clearly see more spread in the distribution. When we go further from p = 0.5, then the sread starts to decrease and return to the same spread it had at lower values.
  1. Now also change \(n\). How does \(n\) appear to affect the distribution of \(\hat{p}\)?

Insert your answer here

When n is low, there is fewer data points to work with, but it sttill seems to resemble a normal distribution. As n increases, the distribution more accurately resembles a normal distribution, and the spread reduces as well as more data gets closer to the center. Since there is less spread, we know that the standard error and margin of error shrink as well.

More Practice

For some of the exercises below, you will conduct inference comparing two proportions. In such cases, you have a response variable that is categorical, and an explanatory variable that is also categorical, and you are comparing the proportions of success of the response variable across the levels of the explanatory variable. This means that when using infer, you need to include both variables within specify.

  1. Is there convincing evidence that those who sleep 10+ hours per day are more likely to strength train every day of the week? As always, write out the hypotheses for any tests you conduct and outline the status of the conditions for inference. If you find a significant difference, also quantify this difference with a confidence interval.

Insert your answer here

# All categories at first
yrbss %>%
  count(school_night_hours_sleep, strength_training_7d) %>%
  #filter(text_while_driving_30d > 0 & text_while_driving_30d != "did not drive") %>%
  mutate(p_strength_training_7d = n /sum(n)) %>%
  group_by(school_night_hours_sleep) %>%
  arrange(school_night_hours_sleep) %>%
  print(n = 75)
## # A tibble: 72 × 4
## # Groups:   school_night_hours_sleep [8]
##    school_night_hours_sleep strength_training_7d     n p_strength_training_7d
##    <chr>                                   <int> <int>                  <dbl>
##  1 10+                                         0   100               0.00736 
##  2 10+                                         1    17               0.00125 
##  3 10+                                         2    31               0.00228 
##  4 10+                                         3    31               0.00228 
##  5 10+                                         4    18               0.00133 
##  6 10+                                         5    23               0.00169 
##  7 10+                                         6     8               0.000589
##  8 10+                                         7    84               0.00618 
##  9 10+                                        NA     4               0.000294
## 10 5                                           0   500               0.0368  
## 11 5                                           1   138               0.0102  
## 12 5                                           2   163               0.0120  
## 13 5                                           3   175               0.0129  
## 14 5                                           4    97               0.00714 
## 15 5                                           5   135               0.00994 
## 16 5                                           6    44               0.00324 
## 17 5                                           7   211               0.0155  
## 18 5                                          NA    17               0.00125 
## 19 6                                           0   822               0.0605  
## 20 6                                           1   223               0.0164  
## 21 6                                           2   294               0.0216  
## 22 6                                           3   323               0.0238  
## 23 6                                           4   216               0.0159  
## 24 6                                           5   276               0.0203  
## 25 6                                           6    99               0.00729 
## 26 6                                           7   372               0.0274  
## 27 6                                          NA    33               0.00243 
## 28 7                                           0   946               0.0696  
## 29 7                                           1   303               0.0223  
## 30 7                                           2   367               0.0270  
## 31 7                                           3   428               0.0315  
## 32 7                                           4   325               0.0239  
## 33 7                                           5   390               0.0287  
## 34 7                                           6   153               0.0113  
## 35 7                                           7   521               0.0384  
## 36 7                                          NA    28               0.00206 
## 37 8                                           0   664               0.0489  
## 38 8                                           1   179               0.0132  
## 39 8                                           2   260               0.0191  
## 40 8                                           3   316               0.0233  
## 41 8                                           4   253               0.0186  
## 42 8                                           5   331               0.0244  
## 43 8                                           6   140               0.0103  
## 44 8                                           7   525               0.0387  
## 45 8                                          NA    24               0.00177 
## 46 9                                           0   172               0.0127  
## 47 9                                           1    59               0.00434 
## 48 9                                           2    77               0.00567 
## 49 9                                           3    88               0.00648 
## 50 9                                           4    71               0.00523 
## 51 9                                           5    85               0.00626 
## 52 9                                           6    33               0.00243 
## 53 9                                           7   175               0.0129  
## 54 9                                          NA     3               0.000221
## 55 <5                                          0   380               0.0280  
## 56 <5                                          1    77               0.00567 
## 57 <5                                          2    99               0.00729 
## 58 <5                                          3    88               0.00648 
## 59 <5                                          4    65               0.00479 
## 60 <5                                          5    72               0.00530 
## 61 <5                                          6    23               0.00169 
## 62 <5                                          7   154               0.0113  
## 63 <5                                         NA     7               0.000515
## 64 <NA>                                        0    48               0.00353 
## 65 <NA>                                        1    16               0.00118 
## 66 <NA>                                        2    14               0.00103 
## 67 <NA>                                        3    19               0.00140 
## 68 <NA>                                        4    14               0.00103 
## 69 <NA>                                        5    21               0.00155 
## 70 <NA>                                        6    13               0.000957
## 71 <NA>                                        7    43               0.00317 
## 72 <NA>                                       NA  1060               0.0780
# Only those who get at least 10 hours of sleep
yrbss %>%
  count(school_night_hours_sleep, strength_training_7d) %>%
  filter(school_night_hours_sleep  == "10+" & strength_training_7d >= 0) %>%
  mutate(p_strength_training_7d = n /sum(n)) %>%
  arrange(strength_training_7d) 
## # A tibble: 8 × 4
##   school_night_hours_sleep strength_training_7d     n p_strength_training_7d
##   <chr>                                   <int> <int>                  <dbl>
## 1 10+                                         0   100                 0.321 
## 2 10+                                         1    17                 0.0545
## 3 10+                                         2    31                 0.0994
## 4 10+                                         3    31                 0.0994
## 5 10+                                         4    18                 0.0577
## 6 10+                                         5    23                 0.0737
## 7 10+                                         6     8                 0.0256
## 8 10+                                         7    84                 0.269
# Only those who workout at least once a week and sleep 10+ hours
yrbss %>%
  count(school_night_hours_sleep, strength_training_7d) %>%
  filter(school_night_hours_sleep  == "10+" & strength_training_7d >= 1) %>%
  mutate(p_strength_training_7d = n /sum(n)) %>%
  arrange(strength_training_7d) 
## # A tibble: 7 × 4
##   school_night_hours_sleep strength_training_7d     n p_strength_training_7d
##   <chr>                                   <int> <int>                  <dbl>
## 1 10+                                         1    17                 0.0802
## 2 10+                                         2    31                 0.146 
## 3 10+                                         3    31                 0.146 
## 4 10+                                         4    18                 0.0849
## 5 10+                                         5    23                 0.108 
## 6 10+                                         6     8                 0.0377
## 7 10+                                         7    84                 0.396
# Only those who workout everyday, regardless of sleep.
yrbss %>%
  count(school_night_hours_sleep, strength_training_7d) %>%
  filter(strength_training_7d == 7) %>%
  mutate(p_strength_training_7d = n /sum(n)) %>%
  arrange(desc(n)) 
## # A tibble: 8 × 4
##   school_night_hours_sleep strength_training_7d     n p_strength_training_7d
##   <chr>                                   <int> <int>                  <dbl>
## 1 8                                           7   525                 0.252 
## 2 7                                           7   521                 0.250 
## 3 6                                           7   372                 0.178 
## 4 5                                           7   211                 0.101 
## 5 9                                           7   175                 0.0839
## 6 <5                                          7   154                 0.0739
## 7 10+                                         7    84                 0.0403
## 8 <NA>                                        7    43                 0.0206
#-------------------------------------------------------------------------------
# Let's look from confidence interval perspective. 
# Get those who sleep 10+ hours
sleep10 <- yrbss  %>%
  filter(school_night_hours_sleep == "10+")

# Those who workout everyday
sleep10 <- sleep10 %>%
  mutate(text_ind_str7 = ifelse(strength_training_7d == "7", "yes", "no"))

sleep10 %>%
  drop_na(text_ind_str7) %>%
  specify(response = text_ind_str7, success = "yes") %>%
  generate(reps = 1000, type = "bootstrap") %>%
  calculate(stat = "prop") %>%
  get_ci(level = 0.95)
## # A tibble: 1 × 2
##   lower_ci upper_ci
##      <dbl>    <dbl>
## 1    0.221    0.321
  # 0.218 to 0.321
From this, it seems that those who get at least 10 hrs of sleep fall into 2 categories; those who don’t work out at all (32.1%) or those who workout at least 1 day a week (67.9%). Of those who do actually workout, 39.6% do strength training every day of the week. However, when looking at those who workout everyday of the week, it seems that those who train everyday and sleep 10+ hours are the second smallest category, with only 4%. The category that is most likely to workout 7 days a week gets 8 hours of sleep, followed by those with 7 (25.2% and 25% respectively). Therefore there doesn’t appear to be a strong correlation between people who sleep 10+ hours each day and working out every day.
  1. Let’s say there has been no difference in likeliness to strength train every day of the week for those who sleep 10+ hours. What is the probability that you could detect a change (at a significance level of 0.05) simply by chance? Hint: Review the definition of the Type 1 error.

Insert your answer here

Per the class notes; “If the test results suggest that the data do not provide convincing evidence for the alternative hypothesis (H_a), we stick with the null hypothesis (H_o). If they do, then we reject the null hypothesis in favor of the alternative.”
Type I Error: Rejecting the null hypothesis when it is true.
Type II Error: Failing to reject the null hypothesis when it is false.
A type 1 error is when we reject the null hypothesis H_o despite it being true, and not needing to reject it. Simply put, it’s like a false positive. We wouldn’t want to reject H_o if the error was significant, which we would have a 5% chance of detecting such a change
  1. Suppose you’re hired by the local government to estimate the proportion of residents that attend a religious service on a weekly basis. According to the guidelines, the estimate must have a margin of error no greater than 1% with 95% confidence. You have no idea what to expect for \(p\). How many people would you have to sample to ensure that you are within the guidelines?
    Hint: Refer to your plot of the relationship between \(p\) and margin of error. This question does not require using a dataset.

Insert your answer here

p <- 0.5 # Use p = 0.5 as this corresponds to the greatest margin of error. 
me <- 0.01 
z <- 1.96 # The Z-Score for a 95% confidence interval

se <- sqrt((p*(1-p))/n)
n <- ((z^2)*(p*(1-p)))/(me^2)
n
## [1] 9604
I would sample 9604 people to ensure the margin of error was no greater than 1% with 95% confidence. I use p = 0.5 as it coincides with the highest margin of error, and I’d rather have a greater value to find a larger sample, instead of using a lower p and having a smaller sample which could potentially have a higher margin of error.