Skip to content

Commit

Permalink
Show exercises solutions only in HTML version
Browse files Browse the repository at this point in the history
  • Loading branch information
ivelasq authored and szimmer committed Mar 21, 2024
1 parent c914d13 commit 796deb7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 32 deletions.
18 changes: 9 additions & 9 deletions 05-descriptive-analysis.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -1244,7 +1244,7 @@ The exercises use the design objects `anes_des` and `recs_des` as provided in th

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

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution1
# Option 1:
femgd_option1 <- anes_des %>%
Expand All @@ -1263,7 +1263,7 @@ femgd_option2

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

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution2
psd <- anes_des %>%
group_by(PartyID) %>%
Expand All @@ -1275,7 +1275,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution3
psr <- anes_des %>%
filter(VotedPres2020 == "Yes") %>%
Expand All @@ -1288,7 +1288,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution4
#| message: false
pvb <- anes_des %>%
Expand All @@ -1302,7 +1302,7 @@ pvb

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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution5
pdeff <- anes_des %>%
filter(!is.na(EarlyVote2020)) %>%
Expand All @@ -1315,7 +1315,7 @@ 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}
```{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,
Expand All @@ -1326,7 +1326,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution7
# Option 1
med_wintertempday <- recs_des %>%
Expand Down Expand Up @@ -1390,7 +1390,7 @@ med_alltemp <- recs_des %>%

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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution8
#| warning: false
corr_summer_temp <- recs_des %>%
Expand All @@ -1402,7 +1402,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: desc-solution9
quant_baenergyexp <- recs_des %>%
group_by(ClimateRegion_BA) %>%
Expand Down
12 changes: 6 additions & 6 deletions 06-statistical-testing.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-ttest-solution1
ttest_solution1 <- recs_des %>%
svyttest(design = .,
Expand All @@ -729,7 +729,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-ttest-solution2
ttest_solution2 <- recs_des %>%
svyttest(
Expand All @@ -743,7 +743,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-ttest-solution3
ttest_solution3 <- anes_des %>%
svyttest(
Expand All @@ -760,15 +760,15 @@ ttest_solution3
b. Test of independence (`svychisq()`)
c. Test of homogeneity (`svychisq()`)

```{r}
```{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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-chisq-solution2
chisq_solution2 <- recs_des %>%
svychisq(
Expand All @@ -783,7 +783,7 @@ 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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: stattest-chisq-solution3
chisq_solution3 <- anes_des %>%
svychisq(
Expand Down
14 changes: 7 additions & 7 deletions 07-modeling.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ Interactions in models can be difficult to understand from the coefficients alon

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.

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-1
recs_des %>%
group_by(HousingUnitType) %>%
Expand All @@ -779,7 +779,7 @@ tidy(exp_unit_out)

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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-2
temps_sqft_exp <- recs_des %>%
svyglm(
Expand All @@ -793,7 +793,7 @@ tidy(temps_sqft_exp)

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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-lin-sol-3
temps_sqft_exp_fit <- temps_sqft_exp %>%
augment() %>%
Expand All @@ -802,7 +802,7 @@ temps_sqft_exp_fit <- temps_sqft_exp %>%
.fitted = as.numeric(.fitted))
```

```{r}
```{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 %>%
Expand All @@ -816,7 +816,7 @@ temps_sqft_exp_fit %>%
theme_minimal()
```

```{r}
```{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 %>%
Expand All @@ -830,7 +830,7 @@ temps_sqft_exp_fit %>%

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}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: model-ex-logistic-1
earlyvote_mod <- anes_des %>%
filter(!is.na(EarlyVote2020)) %>%
Expand All @@ -845,7 +845,7 @@ 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}
```{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) %>%
Expand Down
4 changes: 2 additions & 2 deletions 10-specifying-sample-designs.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ As with other replicate design objects, when printing the object or looking at t

1. The National Health Interview Survey (NHIS) is an annual household survey conducted by the National Center for Health Statistics (NCHS). The NHIS includes a wide variety of health topics for adults including health status and conditions, functioning and disability, health care access and health service utilization, health-related behaviors, health promotion, mental health, barriers to care, and community engagement. Like many national in-person surveys, the sampling design is a stratified clustered design with details included in the Survey Description^[2022 National Health Interview Survey (NHIS) Survey Description: https://www.cdc.gov/nchs/nhis/2022nhis.htm]. The Survey Description provides information on setting up syntax in SUDAAN, Stata, SPSS, SAS, and R ({survey} package implementation). How would you specify the design using {srvyr} using either `as_survey_design` or `as_survey_rep()`?

```r
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
nhis_adult_des <- nhis_adult_data %>%
as_survey_design(ids=PPSU,
strata=PSTRAT,
Expand All @@ -832,7 +832,7 @@ nhis_adult_des <- nhis_adult_data %>%

2. The General Social Survey is a survey that has been administered since 1972 on social, behavioral, and attitudinal topics. The 2016-2020 GSS Panel codebook^[2016-2020 GSS Panel Codebook Release 1a: https://gss.norc.org/Documents/codebook/2016-2020%20GSS%20Panel%20Codebook%20-%20R1a.pdf] provides examples of setting up syntax in SAS and Stata but not R. How would you specify the design in R?

```r
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
gss_des <- gss_data %>%
as_survey_design(ids = VPSU_2,
strata = VSTRAT_2,
Expand Down
11 changes: 5 additions & 6 deletions 13-ncvs-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,7 @@ The output of the statistical test shows the same difference of `r prop_tenure_t

1. What proportion of completed motor vehicle thefts are not reported to the police? Hint: Use the codebook to look at the definition of Type of Crime (V4529).

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: ncvs-vign-des-sol1
ans1 <- inc_des %>%
filter(str_detect(V4529, "40|41")) %>%
Expand All @@ -892,7 +892,7 @@ ans1

2. How many violent crimes occur in each region?

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: ncvs-vign-des-sol2
inc_des %>%
filter(Violent) %>%
Expand All @@ -901,7 +901,7 @@ inc_des %>%

3. What is the property victimization rate among each income level?

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: ncvs-vign-des-sol3
hh_des %>%
group_by(Income) %>%
Expand All @@ -911,7 +911,7 @@ hh_des %>%

4. What is the difference between the violent victimization rate between males and females? Is it statistically different?

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
pers_des %>%
group_by(Sex) %>%
summarize(
Expand All @@ -928,5 +928,4 @@ pers_des %>%
na.rm = TRUE
) %>%
broom::tidy()
```

```
4 changes: 2 additions & 2 deletions 14-ambarom-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ In Figure \@ref(fig:ambarom-make-maps-covid-ed-c-s), we can see that most countr

1. Calculate the percentage of households with broadband internet and those with any internet at home, including from a phone or tablet. Hint: if you come across countries with 0% internet usage, you may want to filter by something first.

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: ambarom-int-prev
int_ests <-
ambarom_des %>%
Expand All @@ -575,7 +575,7 @@ int_ests %>%

2. Create a faceted map showing both broadband internet and any internet usage.

```{r}
```{r, echo=knitr::is_html_output(), eval=knitr::is_html_output()}
#| label: ambarom-facet-map
#| error: true
#| fig.cap: "Percent of broadband internet and any internet usage, Central and South America"
Expand Down

0 comments on commit 796deb7

Please sign in to comment.