Lab 3 Instructions
PUBH 523/623
This lab is ready! (Nicky 8/10/26)
Directions
Please turn in your .qmd and .html file on Brightspace. Please use the same naming convention as practice assignments: lastname_firstinit_lab_03.qmd and lastname_firstinit_lab_03.html.
You can download the .qmd file for this lab here.
The above link will take you to your editing file. Please do not remove anything from this editing file!! You will only add your code and work to this file.
Purpose
The purpose of this lab is to summarize your data and make a few more targeted data transformations that your summaries and future visualizations will need. In Lab 2, you filtered to your population, selected and renamed your variables, and built one or more composite scores. Now we’ll actually look at what those variables tell us: quick numeric summaries, polished summary tables, and summaries grouped by a variable of interest. Along the way, you’ll likely find that a variable isn’t quite in the shape you need it in yet, that’s the “further, needed data transformations” part of this lab. This is meant to build your comfort translating a numeric result back into a plain-language sentence someone outside this class could understand.
1 Lab activities
1.1 Restate your selected project
Recall the following table from Lab 2 with each project’s population, main variables, and composite score(s). You’ll be summarizing the main variables and composite score(s) for your project throughout this lab.
| # | Project Question | Population | Population variables | Main variables | Suggested additional variables | Composite score |
|---|---|---|---|---|---|---|
| 1 | For individuals under the federal poverty line, how do food support systems help individuals’ food security? | People under 100% federal poverty line (FPL) and received free groceries or meals in the past 12 months | FPL_LT50, FPL50_100 |
FOOD_INSEC, Q26D_1, Q26D_2, Q26D_3, Q26D_4 |
Q23, PPETHM, Q37, NUM_CHILD_HH, Q25, Q25A, Q26, Q26A |
Composite score of ease of access to free groceries or meals (combined score from Q26D_) |
| 2 | For individuals who identify as LGBTQ, how does trust and/or fair treatment in healthcare effect self-reported health? | People who identify as LGBTQ and have a healthcare provider. (You can change this population if you want. For example, you may want to focus on how trust and fair treatment may improve health for people with larger bodies. Check out Q70F for options!) |
LGBTQ, Q108 |
Q108B_2, Q108B_1, Q27 |
Q108, Q70F_4, Q70F_5, PPETHM, Q28O |
Level of trust, courtesy, and respect around treatment (combined score from Q108B_1 and Q108B_2) |
| 3 | For individuals with school-aged children and below the federal poverty line, how do benefits received effect their mental health? | People who have at least one child (aged 5-18 years old) in the household and are under the 100% FPL | ANY_CHILDHH_5_18, FPL_LT50, FPL50_100 |
Q30_1, Q30_2, Q30_3, Q30_4, Q30_5, Q30_6, Q53_A, Q53_B, Q53_C, Q53_D, Q53_E, Q53_G, Q53_H, Q53_I, Q53_J, Q11, Q31_4 |
NUM_CHILD_HH, Q1, Q37, PPETHM, Q27 |
Total mental health (combined scores from Q30_), Total benefits (combined scores from Q53_, Q11, and Q31_4) |
Copy and paste the row from the above table that corresponds to your project into your .qmd file. This will help you (and me hehe) keep track of your project question, population, main variables, and composite score(s) as you work through this lab.
1.2 Import your saved data from Lab 2
From the .rda file you exported at the end of Lab 2, import your data into R using import() from the rio package. Save this as a new object called wbns_05_composite. Print out the number of rows and columns to verify the import.
1.3 Quick summaries of your variables
Before building any polished tables, get a feel for your variables using skimr.
Take your wbns_05_composite dataset and use skim() to generate a quick summary of your main variables and composite score(s). Save the result as a new object called quick_summaries, and print it out. Use this to check for any unexpected values, missing data, or other issues that may need to be addressed before moving forward.
1.4 Creating categorical versions of your composite score(s)
A continuous composite score is precise, but it’s hard to summarize or visualize in a way that’s easy to interpret at a glance (e.g., “the average score was 14.3” means less to a general audience than “most people reported easy access”). For this lab, create a categorical version of each composite score you built in Lab 2, so you have both forms available going forward.
There’s no single correct set of categories — use your quick summaries above (the range, and where most of your data falls) to choose meaningful, roughly interpretable cut points. case_when() or cut() both work well for this.
Below are project-specific starting points. These are suggestions, not requirements — adjust the cut points and labels based on what your own data actually look like.
1.4.1 Project 1: ease_free_food
This composite ranges from 4 (very difficult across the board) to 20 (very easy across the board). A reasonable starting point is something like:
- 4-9: “Difficult access”
- 10-15: “Moderate access”
- 16-20: “Easy access”
1.4.2 Project 2: trust_courtesy_respect
This composite ranges from 2 (never true on both items) to 10 (always true on both). A reasonable starting point is something like:
- 2-5: “Low trust”
- 6-8: “Moderate trust”
- 9-10: “High trust”
1.4.3 Project 3: total_benefits
For total_benefits, you likely already have a natural, interpretable scale (a count of benefit types). A reasonable starting point is something like:
- 0-1: “0-1”
- 2-3: “2-3”
- 4-5: “4-5”
- 6-8: “6-8”
Use mutate() with case_when() or cut() to create a new categorical variable for each composite score, with meaningful category labels. Save the result as a new object called wbns_06_cat. Use tabyl() or count() to display how many respondents fall into each category, and confirm no one was left out (e.g., turned into an unexpected NA).
1.5 Building a summary table
Now put your quick summaries into a table that would make sense to someone outside this class (this is the kind of table you might include in your final information sheet). Use tbl_summary() from gtsummary to build one overall summary table of your composite score(s) and the other main variable.
Here are the specific variables to include by project:
- Project 1:
FOOD_INSECand the composite score of ease of access to free groceries or meals - Project 2:
Q27and the composite score of trust, courtesy, and respect around treatment - Project 3: the composite score of total mental health and the composite score of total benefits
Use tbl_summary() to build a clean overall summary table of your composite score(s) and other main variable. Save this as a new object called summary_table_overall, and display it.
Customize at least one thing about the table’s appearance or labels (e.g., with modify_header() or by relabeling variables) so it’s easier for someone unfamiliar with your project to read.
1.6 Grouped summaries
Your project question is ultimately about a relationship (e.g., how ease of food access relates to food insecurity, or how trust relates to self-reported health). A single overall summary can’t show that; you need to compare your composite score across groups.
For each project, we have a suggested grouping variable that you can use to stratify your summaries:
- Project 1: Use
FOOD_INSECto group your summaries of the composite score of ease of access to free groceries or meals. - Project 2: Use
Q27to group your summaries of the composite score of trust, courtesy, and respect around treatment. - Project 3: Use your categorical total benefits to group your summaries of the composite score of total mental health.
Use group_by() and get_summary_stats() to calculate your composite score’s common summary statistics separately for each level of your chosen grouping variable. Write 2-3 sentences interpreting what you see. Does the composite score noticeably differ across groups? Does this align with what you’d expect given your project question?
Optional/Additional: You can also pass your grouping variable into tbl_summary(by = ...) to get a nicely formatted side-by-side table instead of (or in addition to) the group_by()/get_summary_stats() output above.
1.7 Export tables
As always, save your work! Use export() in rio to save wbns_06_cat and your table summary_table_overall as a .rds file in your data_project folder. This is the dataset you’ll build on for visualizations in Lab 4.
Save wbns_06_cat and summary_table_overall in a .rda file in your data_project folder. Name the file something that will help you identify it later, like wbns_data_lab_03_work.rda or wbns_data_03_summ.rda.