From 796deb74802c81ff7b54cf35bb888d70ecaea8b0 Mon Sep 17 00:00:00 2001 From: Isabella Velasquez Date: Sat, 16 Mar 2024 22:05:35 -0700 Subject: [PATCH] Show exercises solutions only in HTML version --- 05-descriptive-analysis.Rmd | 18 +++++++++--------- 06-statistical-testing.Rmd | 12 ++++++------ 07-modeling.Rmd | 14 +++++++------- 10-specifying-sample-designs.Rmd | 4 ++-- 13-ncvs-vignette.Rmd | 11 +++++------ 14-ambarom-vignette.Rmd | 4 ++-- 6 files changed, 31 insertions(+), 32 deletions(-) diff --git a/05-descriptive-analysis.Rmd b/05-descriptive-analysis.Rmd index 8f5fc183..5f945a4e 100644 --- a/05-descriptive-analysis.Rmd +++ b/05-descriptive-analysis.Rmd @@ -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 %>% @@ -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) %>% @@ -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") %>% @@ -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 %>% @@ -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)) %>% @@ -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, @@ -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 %>% @@ -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 %>% @@ -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) %>% diff --git a/06-statistical-testing.Rmd b/06-statistical-testing.Rmd index 964cbe2b..316c405c 100644 --- a/06-statistical-testing.Rmd +++ b/06-statistical-testing.Rmd @@ -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 = ., @@ -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( @@ -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( @@ -760,7 +760,7 @@ 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 @@ -768,7 +768,7 @@ 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( @@ -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( diff --git a/07-modeling.Rmd b/07-modeling.Rmd index df8c444f..8af7f20f 100644 --- a/07-modeling.Rmd +++ b/07-modeling.Rmd @@ -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) %>% @@ -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)^[]. 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( @@ -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() %>% @@ -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 %>% @@ -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 %>% @@ -830,7 +830,7 @@ temps_sqft_exp_fit %>% 4. Early voting expanded in 2020^[]. 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)) %>% @@ -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) %>% diff --git a/10-specifying-sample-designs.Rmd b/10-specifying-sample-designs.Rmd index 441bedf4..9c69eea9 100644 --- a/10-specifying-sample-designs.Rmd +++ b/10-specifying-sample-designs.Rmd @@ -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, @@ -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, diff --git a/13-ncvs-vignette.Rmd b/13-ncvs-vignette.Rmd index 51a4a70d..523e4f39 100644 --- a/13-ncvs-vignette.Rmd +++ b/13-ncvs-vignette.Rmd @@ -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")) %>% @@ -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) %>% @@ -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) %>% @@ -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( @@ -928,5 +928,4 @@ pers_des %>% na.rm = TRUE ) %>% broom::tidy() -``` - +``` \ No newline at end of file diff --git a/14-ambarom-vignette.Rmd b/14-ambarom-vignette.Rmd index e7b6fbaf..78e29c1d 100644 --- a/14-ambarom-vignette.Rmd +++ b/14-ambarom-vignette.Rmd @@ -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 %>% @@ -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"