Introduction

This lab is intended to serve as an opportunity to practice some old skills and learn some new ones. We will return to our college dataset, this time to investigate the proportion of schools we would consider large. In particular, we will practice using:

  • ifelse() to turn a numerical variable into a binary variable
  • Tables and bar charts to describe conditional proportions
  • qnorm() and qt() to find critical values
  • prop.test() to construct confidence intervals and test hypotheses

The framework is the same one we used for means: identify a population quantity, state a claim, and use our observations to assess the evidence. Here, the quantity of interest is a population proportion, \(p\). Our sample estimate is \(\hat p=x/n\), where \(x\) is the number of successes and \(n\) is the number of observations. A “success” is simply the event we are counting; it does not have to be something good.

Use the Lab 7 template for your code, relevant output, plots, and brief answers. It loads the data and packages and includes a table to fill in for Problem 5. You do not need to copy the questions. Save new datasets and summaries with useful names. Bullet points are fine.

## Copy and paste this chunk into the top of your R Markdown document
library(ggplot2)
library(dplyr)

theme_set(theme_bw())

college <- read.csv("https://collinn.github.io/data/college2019.csv")
college <- select(college, Name, State, Enrollment, Type, Region)

Each observation is a college, not a student. These data describe primarily undergraduate institutions with at least 400 full-time students in 2019. For the inferential exercises, treat the colleges as independent random samples from populations of comparable institutions in their regions. All tests in this lab are two-sided; use \(\alpha=0.05\) unless told otherwise.

Some old friends:

A few new tools

Critical values without the sheet

Our critical value sheet gives us selected quantiles. We can ask R for these directly. qnorm() returns a quantile of the standard normal distribution; qt() does the same for a t-distribution with specified df. The argument is the area to the left of the value we want.

For a two-sided 95% interval, the middle area is 0.95 and each tail has area 0.025. The positive critical value therefore has 0.975 to its left.

## Positive critical values for 95% confidence
qnorm(0.975)
## [1] 1.96
qt(0.975, df = 19)
## [1] 2.093

More generally, the positive critical value for confidence \(1-\alpha\) has area \(1-\alpha/2\) to its left. We use qt() when working with a t reference distribution and qnorm() when working with a normal reference distribution.

Problem 1 – Getting our critical values

  • Part A Use qnorm() and qt() to reproduce these two entries from your critical value sheet: 1.645 for a two-sided 90% normal interval and 2.947 for a two-sided 99% t interval with \(df=15\). Show your code and round to three decimal places.

  • Part B Find the positive critical values for a two-sided 92% normal interval and a two-sided 97% t interval with \(df=37\). Show your code and confirm your answers with a partner.

Proportions are means, too

While most of our data so far in the second half of the semester has involved quantitative data, we are also able to perform inference on data that is categorical in nature. For binary data, with possible outcomes of success or failure, one relevant statistic is that of the proportion of successess, or the total number of observed successes divided by the total number of observations. As it turns out, the CLT also applies to using estimates of sample proportions, which are a type of mean. For example, consider ten coin flips in which four result in heads. We understand this proportion to be

\[ \hat p=\frac{4}{10}=0.4. \]

If we record heads as 1 and tails as 0 and take the mean, we find the same:

## Ten flips, four heads
flips <- c(0, 1, 1, 0, 0, 0, 1, 0, 0, 1)
mean(flips)
## [1] 0.4

For a population of 0s and 1s, the mean is \(p\) and the variance is \(p(1-p)\). For independent observations and a large enough sample, the CLT therefore tells us that \(\hat p\) has an approximately normal sampling distribution, centered at \(p\), with standard error \(\sqrt{p(1-p)/n}\). We estimate this using our sample proportion:

\[ \widehat{SE}=\sqrt{\frac{\hat p(1-\hat p)}{n}}. \]

We can now construct a t-statistic in the same way as for a sample mean: subtract the null value and divide by the estimated standard error. For our hand calculations, use the t approximation with \(df=n-1\):

\[ t=\frac{\hat p-p_0}{\widehat{SE}},\qquad \text{confidence interval: }\hat p\pm C\times\widehat{SE}. \]

Suppose we want to investigate whether a die is fair. We roll it 60 times and observe 12 sixes. For a fair die, the probability of a six is \(1/6\). Test \(H_0:p=1/6\) against \(H_A:p\ne1/6\) and construct a 95% confidence interval:

n <- 60
phat <- 12 / n
SE <- sqrt(phat * (1 - phat) / n)
t_stat <- (phat - 1/6) / SE
t_stat
## [1] 0.6455
2 * pt(-abs(t_stat), df = n - 1)
## [1] 0.52111
phat + qt(c(0.025, 0.975), df = n - 1) * SE
## [1] 0.096669 0.303331

Is \(1/6\) inside our confidence interval? What does this tell us about the claim that a six occurs with probability \(1/6\)? This checks one implication of fairness, rather than the probabilities of all six faces.

The structure is the same as before; the formula for SE reflects that our observations are 0s and 1s. Rather than use t.test(), however, the prop.test() function below provides another procedure for proportions, with its own interval and test calculation.

Using prop.test()

We can assess our claim by checking whether the null value falls inside a confidence interval, as we did above. Alternatively, prop.test() carries out a hypothesis test for us and reports a p-value, along with a confidence interval and an estimated proportion. We use it much as we used t.test(): state the null, supply the data, and interpret the output.

For one population proportion, we will use three main arguments:

  • x: the number of successes, not the sample proportion
  • n: the total number of observations, including successes
  • p: the hypothesized population proportion.

As with t.test(), we can use conf.level to choose the confidence level. For the same die example, we observed 12 sixes in 60 rolls and wish to test \(H_0:p=1/6\) against \(H_A:p\ne1/6\).

prop.test(x = 12, n = 60, p = 1/6, conf.level = 0.95)
## 
##  1-sample proportions test with continuity correction
## 
## data:  12 out of 60, null probability 1/6
## X-squared = 0.27, df = 1, p-value = 0.6
## alternative hypothesis: true p is not equal to 0.16667
## 95 percent confidence interval:
##  0.11190 0.32704
## sample estimates:
##   p 
## 0.2

In the output, sample estimates gives the observed proportion of sixes, 95 percent confidence interval gives the interval, and p-value tells us how much evidence we have against \(H_0:p=1/6\). As before, reject when the p-value is less than alpha. Compare this decision with your confidence interval above.

You will also see a statistic called X-squared. We will study that statistic soon; for now, use the p-value and confidence interval in the same way as with t.test().

The normal approximation needs enough successes and failures. For a one-proportion test, check that the expected counts under the null, \(np_0\) and \(n(1-p_0)\), are both at least 10. For two groups, we will check that the expected success and failure counts in each group, using the pooled proportion, are at least 10. Generally for this class, we can assume that these conditions are satisfied.

Keep the default settings of prop.test(), including its continuity correction. Its interval calculation differs from the simple \(\hat p\pm C\sqrt{\hat p(1-\hat p)/n}\) approximation, so do not expect identical endpoints from those two methods.

Comparing two proportions

A common test with proportions is to test equality of proportions of “success” between two groups, denoted \(p_1\) for the proportion in group 1 and \(p_2\) for the proportion in group 2.

\[ H_0: p_1 = p_2 \] Another way to express this is to test

\[ H_0: p_1 - p_2 = 0 \]

To illustrate, consider the tips dataset. Is the proportion of meals with a tip of at least 20% different at lunch and dinner? First, use ifelse() to turn tip percentage into a binary variable:

tips <- read.csv("https://collinn.github.io/data/tips.csv")
tips <- tips %>%
  mutate(percent_tip = 100 * tip / total_bill,
         TipGroup = ifelse(percent_tip >= 20, "at_least_20", "below_20"))

As in our tables lab, put the groups being compared in the rows and the two possible outcomes in the columns:

tip_table <- with(tips, table(time, TipGroup))
tip_table
##         TipGroup
## time     at_least_20 below_20
##   Dinner          27      149
##   Lunch           12       56

Each row proportion uses that row’s total number of meals as its denominator. So, for example the proportion of Dinner meals with a tip of at least 20% would be \(\hat p_{\mathrm{Dinner}} = \frac{27}{27 + 149} = 0.153\). Our null hypothesis, then, would be of the form:

\[ H_0:p_{\mathrm{Dinner}}=p_{\mathrm{Lunch}} \quad \text{vs} \quad H_A:p_{\mathrm{Dinner}}\ne p_{\mathrm{Lunch}} \]

Once we have created our table of counts, we can pass it directly into prop.test():

prop.test(tip_table, conf.level = 0.95)
## 
##  2-sample test for equality of proportions with continuity correction
## 
## data:  tip_table
## X-squared = 0.0605, df = 1, p-value = 0.81
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.138349  0.092226
## sample estimates:
##  prop 1  prop 2 
## 0.15341 0.17647

With a two-column table, prop.test() treats the first column as successes and the second as failures. Check the printed table: our first column is at_least_20, and each row total supplies that group’s sample size. Pass the counts, not the table of proportions. We leave out p because the null claim is that the two population proportions are equal.

The output follows the row order: Dinner first, then Lunch. The confidence interval estimates \(p_{\mathrm{Dinner}}-p_{\mathrm{Lunch}}\). A positive difference means the proportion is higher at dinner; zero means no difference. Use the p-value to decide whether to reject and the interval to describe plausible values for the difference.

Note: if you do not have a table to put into prop.test(), you can still enter two groups manually. To replicate the test above, you could also use

prop.test(x = c(27, 12), # Number of successes for each group
          n = c(27 + 149, 12 + 56), # Total observations from each group
          conf.level = 0.95)
## 
##  2-sample test for equality of proportions with continuity correction
## 
## data:  c(27, 12) out of c(27 + 149, 12 + 56)
## X-squared = 0.0605, df = 1, p-value = 0.81
## alternative hypothesis: two.sided
## 95 percent confidence interval:
##  -0.138349  0.092226
## sample estimates:
##  prop 1  prop 2 
## 0.15341 0.17647

This is the same sequence you can use in the exercises below: create a binary variable, make a two-way table, check its row and column order, and pass the count table to prop.test().

Problem 2

We used enrollment to create school-size categories in the dplyr lab. We will start with the same definition: a school is large if its Enrollment is at least 10,000, and small otherwise.

  • Part A Use mutate() and ifelse() to create a variable called Size with the values "large" and "small". Save the resulting dataset as college_size. Do not replace the numerical enrollment variable. How would a school with exactly 10,000 students be classified?

  • Part B Filter college_size to include only schools in the "Plains" and "Great Lakes" regions, saving the result as regional_colleges. Create a two-way table with Region in the rows and Size in the columns. Then find the row proportions. What is the denominator for each row? Which region has the higher proportion of large schools?

  • Part C Create two bar charts: one showing the counts of small and large schools within each region, and one showing the proportions within each region. Use the same x and fill variables for both plots. Which chart answers the question in Part B more directly? Explain why comparing counts alone could be misleading. What other information would you need to identify a statistical difference?

Problem 3

A researcher claims that 20% of comparable colleges in the Plains region are large. Use our original definition of large: enrollment of at least 10,000 students.

  • Part A Filter regional_colleges to create a dataset called plains_colleges. Identify the success we are counting and state the null and alternative hypotheses. Use table() to report the number of large schools, total number of schools, and sample proportion. Check the two expected counts under \(H_0\).

  • Part B Use prop.test() to test the researcher’s claim at \(\alpha=0.05\). Report the sample estimate, 95% confidence interval, p-value, and decision. Explain your conclusion in context, including whether the observed proportion is above or below the claimed value. Does the interval agree with your decision for these data?

  • Part C Using qnorm() and your sample proportion, calculate the approximate interval below. Compare it with the interval returned by prop.test(). Are they similar? Which population quantity do both intervals estimate?

\[ \hat p\ \pm\ C\sqrt{\frac{\hat p(1-\hat p)}{n}}. \]

Problem 4

We now ask whether the proportion of large schools differs between the Plains and Great Lakes regions. Continue using the 10,000 cutoff.

  • Part A Return to your conditional bar chart from Problem 2. Describe the observed difference. Based on the graph alone, can we decide whether the population proportions differ at \(\alpha=0.05\)? What information about uncertainty is missing from the bar heights?

  • Part B Use prop.test() to compare the proportion of large schools for both the Plains and the Great Lakes. Report both sample proportions, their difference, the 95% confidence interval for the difference, and the p-value. Give your decision and conclusion. Explain the sign of the difference and what zero would mean in the interval. Does the test support your initial impression from the plot?

  • Part C Consider two hypothetical studies of the same question. Study A finds 12 large schools out of 40 in one region and 20 out of 40 in another. Study B finds 120 out of 400 and 200 out of 400. Would conditional proportion bar charts have different bar heights for these studies? Run a two-proportion test for each. Do they give the same decision at \(\alpha=0.05\)? Explain why identical observed proportions need not provide identical strength of evidence.

Problem 5 (Optional, if time)

There is no universal rule that says a large school begins at exactly 10,000 students. Suppose we instead use 5,000.

  • Part A Starting from regional_colleges, use ifelse() to add a variable called Size5000 that labels schools with enrollment of at least 5,000 as "large" and the others as "small". Save this dataset as regional_colleges_5000. Keep the original Size variable so that you can compare the two definitions. How many schools change classification? Did either region’s total number of schools change?

  • Part B Construct a conditional bar chart using Size5000, then repeat the two-proportion test from Problem 4 using the new success counts. Keep the regions in the same order and use the same confidence level. Fill in the comparison table in the template with each region’s sample proportion, the difference, the p-value, and the decision at \(\alpha=0.05\) for both enrollment thresholds.

  • Part C Did your conclusion change? Explain how changing the definition changed the question and the data entering the test, even though no colleges were added or removed. Does failing to reject under one definition establish that the regions are the same?

  • Part D A classmate suggests trying several more cutoffs and reporting only the one with the smallest p-value. What would be misleading about that report? In 2–3 sentences, explain why a definition should have a reason beyond producing a desired result.