Skip to content

Commit

Permalink
Changed t-test ex2 to use RECS instead of ANES. (#88)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephanie Zimmer <[email protected]>
  • Loading branch information
rpowell22 and szimmer authored Feb 7, 2024
1 parent 8b61cc1 commit 24e6b4f
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions 06-statistical-testing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -208,29 +208,29 @@ confint(ttest_ex1, level = 0.8)

#### Example 2: One-sample t-test for Proportion {.unnumbered #stattest-ttest-ex2}

ANES asked respondents if they voted in the presidential election in 2020.^[This question is not about the primary elections and caucuses that were held a few months ago. Instead, we'd like to ask you about the election for President to be held on November 3, in which (Donald Trump / Joe Biden) is running against (Joe Biden / Donald Trump). Have you already voted in that election, or have you not voted?] In our data, we call this variable `VotedPres2020`. Let's look at the proportion of the U.S. voting-eligible population that voted for the president in 2020 using the `survey_prop()` function we learned in Chapter \@ref(c05-descriptive-analysis).
RECS asked respondents if they use any air conditioning (AC) in their home.^[Is any air conditioning equipment used in your home?] In our data, we call this variable `ACUsed`. Let's look at the proportion of U.S. households that use AC in their homes using the `survey_prop()` function we learned in Chapter \@ref(c05-descriptive-analysis).

```{r}
#| label: stattest-ttest-voteprop
voteprop <- anes_des %>%
group_by(VotedPres2020) %>%
#| label: stattest-ttest-acused
acprop <- recs_des %>%
group_by(ACUsed) %>%
summarize(p = survey_prop())
voteprop
acprop
```

Based on this, `r signif((voteprop %>% filter(VotedPres2020=="Yes") %>% pull(p))*100,3)`% of the U.S. voting-eligible population voted for president in 2020. If we wanted to know how this compares to another country, we could use `svyttest()`. For example, if we know that the voter turnout in Germany in the 2017 general election was 76.2%, we could set up our hypothesis as follows:
Based on this, `r signif((acprop %>% filter(ACUsed==TRUE) %>% pull(p))*100,3)`% of U.S. households use AC in their homes. If we wanted to know if this differs from 90%, we could set up our hypothesis as follows:

- $H_0: p = 0.762$ where $p$ is the proportion of the U.S. voting-eligible population that voted for president in 2020
- $H_A: p \neq 0.762$
- $H_0: p = 0.90$ where $p$ is the proportion of the U.S. households that use AC in their homes
- $H_A: p \neq 0.90$

To conduct this in R, we use the `svyttest()` function as follows:

```{r}
#| label: stattest-ttest-ex2
ttest_ex2 <- anes_des %>%
ttest_ex2 <- recs_des %>%
svyttest(
formula = (VotedPres2020 == "Yes") - 0.762 ~ 0,
formula = (ACUsed == TRUE) - 0.90 ~ 0,
design = .,
na.rm = TRUE
)
Expand All @@ -245,7 +245,7 @@ The output from the `svyttest()` function can be a bit hard to read. Using the {
broom::tidy(ttest_ex2)
```

The estimate differs from example one in that the estimate is not displaying \(\mu - 0.762\) but rather \(\mu\), or the difference between the U.S. proportion and the German proportion we are comparing. We can see that there is a difference of `r signif(ttest_ex2$estimate*100,3)` percentage points. Additionally, the t-statistic value in the `statistic` column is `r signif(ttest_ex2$statistic,3)`, and the p-value is `r signif(ttest_ex2$p.value,3)`. These results indicate that the U.S. and Germany have similar voter turnout.
The estimate differs from Example 1 in that the estimate is not displaying \(\mu - 0.90\) but rather \(\mu\), or the difference between the U.S. households that use AC and the proportion we are comparing to. We can see that there is a difference of `r signif(ttest_ex2$estimate*100,3)` percentage points. Additionally, the t-statistic value in the `statistic` column is `r signif(ttest_ex2$statistic,3)`, and the p-value is `r signif(ttest_ex2$p.value,3)`. These results indicate that the fewer than 90% of U.S. households use AC in their homes.

<!--Add in callout box about how to use the $ notation to help call out the different values? Maybe indicate how this will be covered more in the reporting chapter? IV: I added a bit up top, not sure if it needs a whole call out box but happy to revisit.-->

Expand Down

0 comments on commit 24e6b4f

Please sign in to comment.