Skip to content

Commit

Permalink
Move exercise solutions to Appendix D
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelasq authored and szimmer committed Mar 21, 2024
1 parent 796deb7 commit ecdc6db
Show file tree
Hide file tree
Showing 7 changed files with 580 additions and 444 deletions.
162 changes: 3 additions & 159 deletions 05-descriptive-analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1240,178 +1240,22 @@ In addition to our results above, we can also see the output for `TrustPeople`.

## Exercises

The exercises use the design objects `anes_des` and `recs_des` as provided in the Prerequisites box in the beginning of the chapter.
The exercises use the design objects `anes_des` and `recs_des` provided in the Prerequisites box in the beginning of the chapter.

1. How many females have a graduate degree? Hint: the variables `Gender` and `Education` will be useful.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution1
# Option 1:
femgd_option1 <- anes_des %>%
filter(Gender == "Female", Education == "Graduate") %>%
survey_count(name = "n")
femgd_option1
# Option 2:
femgd_option2 <- anes_des %>%
filter(Gender == "Female", Education == "Graduate") %>%
summarize(N = survey_total(), .groups = "drop")
femgd_option2
```

2. What percentage of people identify as "Strong Democrat"? Hint: The variable `PartyID` indicates someone's party affiliation.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution2
psd <- anes_des %>%
group_by(PartyID) %>%
summarize(p = survey_mean()) %>%
filter(PartyID == "Strong democrat")
psd
```

3. What percentage of people who voted in the 2020 election identify as "Strong Republican"? Hint: The variable `VotedPres2020` indicates whether someone voted in 2020.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution3
psr <- anes_des %>%
filter(VotedPres2020 == "Yes") %>%
group_by(PartyID) %>%
summarize(p = survey_mean()) %>%
filter(PartyID == "Strong republican")
psr
```

4. What percentage of people voted in both the 2016 election and the 2020 election? Include the logit confidence interval. Hint: The variable `VotedPres2016` indicates whether someone voted in 2016.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution4
#| message: false
pvb <- anes_des %>%
filter(!is.na(VotedPres2016),!is.na(VotedPres2020)) %>%
group_by(interact(VotedPres2016, VotedPres2020)) %>%
summarize(p = survey_prop(var = "ci", method = "logit"),) %>%
filter(VotedPres2016 == "Yes", VotedPres2020 == "Yes")
pvb
```
4. What percentage of people voted in both the 2016 election and the 2020 election? Include the logit confidence interval. Hint: The variable `VotedPres2016` indicates whether someone voted in 2016.

5. What is the design effect for the proportion of people who voted early? Hint: The variable `EarlyVote2020` indicates whether someone voted early in 2020.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution5
pdeff <- anes_des %>%
filter(!is.na(EarlyVote2020)) %>%
group_by(EarlyVote2020) %>%
summarize(p = survey_mean(deff = TRUE)) %>%
filter(EarlyVote2020 == "Yes")
pdeff
```

6. What is the median temperature people set their thermostats to at night during the winter? Hint: The variable `WinterTempNight` indicates the temperature that people set their temperature in the winter at night.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution6
mean_wintertempnight <- recs_des %>%
summarize(wtn_mean = survey_mean(x = WinterTempNight,
na.rm = TRUE))
mean_wintertempnight
```

7. People sometimes set their temperature differently over different seasons and during the day. What median temperatures do people set their thermostat to in the summer and winter, both during the day and at night? Include confidence intervals. Hint: Use the variables `WinterTempDay`, `WinterTempNight`, `SummerTempDay`, and `SummerTempNight`.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution7
# Option 1
med_wintertempday <- recs_des %>%
summarize(wtd_mean = survey_median(WinterTempDay,
vartype = "se",
na.rm = TRUE))
med_wintertempday
med_wintertempnight <- recs_des %>%
summarize(wtn_mean = survey_median(WinterTempNight,
vartype = "se",
na.rm = TRUE))
med_wintertempnight
med_summertempday <- recs_des %>%
summarize(std_mean = survey_median(SummerTempDay,
vartype = "se",
na.rm = TRUE))
med_summertempday
med_summertempnight <- recs_des %>%
summarize(stn_mean = survey_median(SummerTempNight,
vartype = "se",
na.rm = TRUE))
med_summertempnight
# Alternatively, could use `survey_quantile()` as shown below for WinterTempNight:
quant_wintertemp <- recs_des %>%
summarize(wnt_quant = survey_quantile(
WinterTempNight,
quantiles = 0.5,
vartype = "se",
na.rm = TRUE
))
quant_wintertemp
# Can also calculate all of these medians in a single summarize() function:
med_alltemp <- recs_des %>%
summarize(
wtd_mean = survey_median(WinterTempDay,
vartype = "se",
na.rm = TRUE),
wtn_mean = survey_median(WinterTempNight,
vartype = "se",
na.rm = TRUE),
std_mean = survey_median(SummerTempDay,
vartype = "se",
na.rm = TRUE),
stn_mean = survey_median(SummerTempNight,
vartype = "se",
na.rm = TRUE)
)
```

8. What is the correlation between the temperature that people set their temperature at during the night and during the day in the summer?

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution8
#| warning: false
corr_summer_temp <- recs_des %>%
summarize(summer_corr = survey_corr(SummerTempNight, SummerTempDay,
na.rm = TRUE))
corr_summer_temp
```

9. What is the 1st, 2nd, and 3rd quartile of the amount of money spent on energy by Building America (BA) climate zone? Hint: `TOTALDOL` indicates the total amount spent on electricity, and `ClimateRegion_BA` indicates the BA climate zones.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution9
quant_baenergyexp <- recs_des %>%
group_by(ClimateRegion_BA) %>%
summarize(dol_quant = survey_quantile(
TOTALDOL,
quantiles = c(0.25, 0.5, 0.75),
vartype = "se",
na.rm = TRUE
))
quant_baenergyexp
```
9. What is the 1st, 2nd, and 3rd quartile of the amount of money spent on energy by Building America (BA) climate zone? Hint: `TOTALDOL` indicates the total amount spent on electricity, and `ClimateRegion_BA` indicates the BA climate zones.
69 changes: 2 additions & 67 deletions 06-statistical-testing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -717,81 +717,16 @@ The exercises use the design objects `anes_des` and `recs_des` as provided in th

1. Using the RECS data, do more than 50% of U.S. households use AC (`ACUsed`)?

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-ttest-solution1
ttest_solution1 <- recs_des %>%
svyttest(design = .,
formula = ((ACUsed == TRUE) - 0.5) ~ 0,
na.rm = TRUE)
ttest_solution1
```

2. Using the RECS data, does the average temperature that U.S. households set their thermostats to differ between the day and night in the winter (`WinterTempDay` and `WinterTempNight`)?

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-ttest-solution2
ttest_solution2 <- recs_des %>%
svyttest(
design = .,
formula = WinterTempDay - WinterTempNight ~ 0,
na.rm = TRUE
)
ttest_solution2
```

3. Using the ANES data, does the average age (`Age`) of those who voted for Joseph Biden in 2020 (`VotedPres2020_selection`) differ from those who voted for another candidate?

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-ttest-solution3
ttest_solution3 <- anes_des %>%
svyttest(
design = .,
formula = Age ~ VotedPres2020_selection == "Biden",
na.rm = TRUE
)
ttest_solution3
```

4. If you wanted to determine if the political party affiliation differed for males and females, what test would you use?

a. Goodness of fit test (`svygofchisq()`)
b. Test of independence (`svychisq()`)
c. Test of homogeneity (`svychisq()`)

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-chisq-solution1
chisq_solution1 <- "c. Test of homogeneity (`svychisq()`)"
chisq_solution1
```

5. In the RECS data, is there a relationship between the type of housing unit (`HousingUnitType`) and the year the house was built (`YearMade`)?

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-chisq-solution2
chisq_solution2 <- recs_des %>%
svychisq(
formula = ~ HousingUnitType + YearMade,
design = .,
statistic = "Wald",
na.rm = TRUE
)
chisq_solution2
```

6. In the ANES data, is there a difference in the distribution of gender (`Gender`) across early voting status in 2020 (`EarlyVote2020`)?

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-chisq-solution3
chisq_solution3 <- anes_des %>%
svychisq(
formula = ~ Gender + EarlyVote2020,
design = .,
statistic = "F",
na.rm = TRUE
)
chisq_solution3
```
6. In the ANES data, is there a difference in the distribution of gender (`Gender`) across early voting status in 2020 (`EarlyVote2020`)?
109 changes: 4 additions & 105 deletions 07-modeling.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -753,113 +753,12 @@ Interactions in models can be difficult to understand from the coefficients alon

## Exercises

1. The type of housing unit may have an impact on energy expenses. Using the RECS data, is there any relationship between housing unit type (`HousingUnitType`) and total energy expenditure (`TOTALDOL`)? First, find the average energy expenditure by housing unit type as a descriptive analysis and then do the test. The reference level in the comparison should be the housing unit type that is most common.
1. The type of housing unit may have an impact on energy expenses. Is there any relationship between housing unit type (`HousingUnitType`) and total energy expenditure (`TOTALDOL`)? First, find the average energy expenditure by housing unit type as a descriptive analysis and then do the test. The reference level in the comparison should be the housing unit type that is most common.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-1
recs_des %>%
group_by(HousingUnitType) %>%
summarize(Expense = survey_mean(TOTALDOL, na.rm = TRUE),
HUs = survey_total()) %>%
arrange(desc(HUs))
exp_unit_out <- recs_des %>%
mutate(HousingUnitType = fct_infreq(HousingUnitType, NWEIGHT)) %>%
svyglm(
design = .,
formula = TOTALDOL ~ HousingUnitType,
na.action = na.omit
)
tidy(exp_unit_out)
# Single-family detached units are most common
# There is a significant relationship between energy expenditure and housing unit type
```

2. Using the RECS data, does temperature play a role in energy expenditure? Cooling degree days are a measure of how hot a place is. Variable `CDD65` for a given day indicates the number of degrees Fahrenheit warmer than 65°F (18.3°C) it is in a location. On a day that averages 65°F and below, `CDD65=0`. While a day that averages 85°F would have `CDD65=20` because it is 20 degrees warmer. For each day in the year, this is summed to give an indicator of how hot the place is throughout the year. Similarly, `HDD65` indicates the days colder than 65°F (18.3°C)^[<https://www.eia.gov/energyexplained/units-and-calculators/degree-days.php>]. Can energy expenditure be predicted using these temperature indicators along with square footage? Is there a significant relationship? Include main effects and two-way interactions.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-2
temps_sqft_exp <- recs_des %>%
svyglm(
design = .,
formula = DOLLAREL ~ (TOTSQFT_EN + CDD65 + HDD65) ^ 2,
na.action = na.omit
)
tidy(temps_sqft_exp)
```
2. Does temperature play a role in energy expenditure? Cooling degree days are a measure of how hot a place is. CDD65 for a given day indicates the number of degrees Fahrenheit warmer than 65°F (18.3°C) it is in a location. On a day that averages 65°F and below, CDD65=0. While a day that averages 85°F would have CDD80=20 because it is 20 degrees warmer. For each day in the year, this is summed to give an indicator of how hot the place is throughout the year. Similarly, HDD65 indicates the days colder than 65°F (18.3°C)^[<https://www.eia.gov/energyexplained/units-and-calculators/degree-days.php>]. Can energy expenditure be predicted using these temperature indicators along with square footage? Is there a significant relationship? Include main effects and two-way interactions.

3. Continuing with our results from question 2, create a plot between the actual and predicted expenditures and a residual plot for the predicted expenditures.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-3
temps_sqft_exp_fit <- temps_sqft_exp %>%
augment() %>%
mutate(.se.fit = sqrt(attr(.fitted, "var")),
# extract the variance of the fitted value
.fitted = as.numeric(.fitted))
```
4. Early voting expanded in 2020^[<https://www.npr.org/2020/10/26/927803214/62-million-and-counting-americans-are-breaking-early-voting-records>]. Build a logistic model predicting early voting in 2020 (`EarlyVote2020`) using age (`Age`), education (`Education`), and party identification (`PartyID`). Include two-way interactions.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-3-p1
#| fig.cap: "Actual and predicted electricity expenditures"
temps_sqft_exp_fit %>%
ggplot(aes(x = DOLLAREL, y = .fitted)) +
geom_point() +
geom_abline(intercept = 0,
slope = 1,
color = "red") +
xlab("Actual expenditures") +
ylab("Predicted expenditures") +
theme_minimal()
```

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-3-p2
#| fig.cap: "Residual plot of electric cost model with covariates TOTSQFT_EN, CDD65, and HDD65"
temps_sqft_exp_fit %>%
ggplot(aes(x = .fitted, y = .resid)) +
geom_point() +
geom_hline(yintercept = 0, color = "red") +
xlab("Predicted expenditure") +
ylab("Residual value of expenditure") +
theme_minimal()
```

4. Early voting expanded in 2020^[<https://www.npr.org/2020/10/26/927803214/62-million-and-counting-americans-are-breaking-early-voting-records>]. Using the ANES data, build a logistic model predicting early voting in 2020 (`EarlyVote2020`) using age (`Age`), education (`Education`), and party identification (`PartyID`). Include two-way interactions.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-ex-logistic-1
earlyvote_mod <- anes_des %>%
filter(!is.na(EarlyVote2020)) %>%
svyglm(
design = .,
formula = EarlyVote2020 ~ (Age + Education + PartyID) ^ 2 ,
family = quasibinomial
)
tidy(earlyvote_mod) %>% arrange(p.value)
```

5. Continuing from Exercise 4, predict the probability of early voting for two people. Both are 28 years old and have a graduate degree, but one person is a strong Democrat, and the other is a strong Republican.

```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-ex-logistic-2
add_vote_dat <- anes_2020 %>%
select(EarlyVote2020, Age, Education, PartyID) %>%
rbind(tibble(
EarlyVote2020 = NA,
Age = 28,
Education = "Graduate",
PartyID = c("Strong democrat", "Strong republican")
)) %>%
tail(2)
log_ex_2_out <- earlyvote_mod %>%
augment(newdata = add_vote_dat, type.predict = "response") %>%
mutate(.se.fit = sqrt(attr(.fitted, "var")),
# extract the variance of the fitted value
.fitted = as.numeric(.fitted))
```
5. Continuing from Exercise 1, predict the probability of early voting for two people. Both are 28 years old and have a graduate degree, but one person is a strong Democrat, and the other is a strong Republican.
Loading

0 comments on commit ecdc6db

Please sign in to comment.