Lab 8

Due: end-of-lab Friday November 13

The General Social Survey (GSS) gathers data on contemporary American society in order to monitor and explain trends and constants in attitudes, behaviours, and attributes. Hundreds of trends have been tracked since 1972. In addition, since the GSS adopted questions from earlier surveys, trends can be followed for up to 70 years. The GSS contains a standard core of demographic, behavioural, and attitudinal questions, plus topics of special interest. Among the topics covered are civil liberties, crime and violence, intergroup tolerance, morality, national spending priorities, psychological well-being, social mobility, and stress and traumatic events.

You will work with the 2022 General Social Survey:

gss22 <- read_csv("data/gss22.csv")

We will focus on the variable advfront, which records whether the respondent agrees or disagrees with the statement: “Even if it brings no immediate benefits, scientific research that advances the frontiers of knowledge is necessary and should be supported by the federal government.”

Our dataset contains a sample of only 1207 respondents, which is far less than the US population. Based on these data, our estimate of public opinion on this issue is

gss22 |>
  count(advfront) |>
  mutate(prop = n / sum(n))
# A tibble: 2 × 3
  advfront      n  prop
  <chr>     <int> <dbl>
1 Agree      1051 0.871
2 Not agree   156 0.129

How reliable is that estimate?

Setup

  1. Log-in to your container;
  2. Double-check that you have the sta101-f26-files project loaded in the upper-right corner of RStudio (this should always be true);
  3. Go to the “Git” tab in the upper-right panel of RStudio and click the Pull button (arrow pointing down). Now the new assignment template and dataset should appear in the lab folder of your Files;
  4. At the top of the new .qmd, in between ---, you have the settings for the document (the so-called YAML, but don’t worry about that). Modify the authors so it lists yourself and your teammates.

Part 1 - point estimation

As with linear regression, we are required to compute the point estimate in a particular way so that it plays nice with the tools we use for inference. Here is the point estimate, which matches what we got earlier:

p_hat <- gss22 |>
  specify(response = advfront, success = "Agree") |>
  calculate(stat = "prop")

p_hat
Response: advfront (factor)
# A tibble: 1 × 1
   stat
  <dbl>
1 0.871

Part 2 - interval estimation (two ways!)

Task 1

Use the bootstrap to simulate the sampling distribution of the proportion and use the simulations to visualize, calculate, and interpret a 99% confidence interval.

Hint

In order to simulate the bootstrap, you should take the data pipeline above that computed the point estimate and add the generate command. At what step should generate be inserted? What inputs should it take? I leave it to you.

Task 2

If instead of simulation we wanted to approximate the sampling distribution with the central limit theorem, then we don’t need to generate anymore, we need to assume:

gss22 |>
   specify(response = advfront, success = "Agree") |>
   assume(distribution = "z")

That code produces a smooth bell curve that approximates the sampling distribution. After that, you can use exactly the same commands (get_ci, visualize, and shade_confidence_interval) to do inference for the unknown proportion. Give it a try! Your interval and picture from this task should be fairly close to your interval from the previous task.

Part 3 - hypothesis testing (two ways!)

Let \(p\) be the true proportion of American adults that agree with the statement. We don’t know what \(p\) is. We can only guess at its value by collecting data and calculating an estimate, which may or may not be accurate.

Given our data, let’s say we wish to test the following point null hypothesis against a two-sided alternative:

\[ \begin{aligned} H_0&:p=0.5\\ H_A&:p\neq 0.5. \end{aligned} \]

So, we want to know if the evidence is strong enough to distinguish between these two competing claims.

Task 3

Use random sampling to simulate the null distribution of the proportion and use the simulations to visualize, calculate, and interpret a \(p\)-value for our test.

Hint

In order to simulate the bootstrap, you should take the data pipeline above that computed the point estimate and add the hypothesize and generate commands. At what step should you insert these commands? What inputs should they take? I leave it to you.

Task 4

If instead of simulation you want to run the test using a normal approximation, then the infer package we’ve been using actually has a single shortcut command. Read the documentation for this command by tying this in the console:

?prop_test

Then use the command to conduct the same test that you performed in the previous part. Your \(p\)-value should be similar.

Part 4 - why stop there?

Congratulations! You have just completed all of the work you have to submit for today’s lab. If any time remains in the session, I encourage you to stick around and work with your teammates on either…

Submission

You collaborated with your team, but now everyone submits individually:

  1. Hit the blue Render button to generate your final PDF;
  2. Give your work a final look over to double-check a few things:
    • that your code is stylish;
    • that none of your code or pictures runs off the page. We cannot grade what we cannot read;
    • that all of your plots are well-labeled and human-readable. In other words “Flipper length (mm)” instead of flipper_length_mm;
    • If your work is lacking on any of these items, fix them and re-render as needed;
  3. Download the PDF from your container;
  4. Upload it to Gradescope;
  5. Don’t forget to mark your pages.