Does the MDE–SUD association differ by sex?
Self-paced bonus activity · compare P(SUD | MDE) across females and males
Welcome to a self-paced bonus activity. In the M06 lab you built the MDE × SUD cross-tab for females and read P(SUD | MDE) off the positive-MDE row. Here you’ll build the same table for males, compare the two, and turn the comparison into a small grouped bar chart. It’s an extension of the Part 1 core — a good one to revisit when you’re reviewing conditional probability for exams.
Everything happens in the sandbox chunks on this page — nothing here goes into your lab notebook. The 2019 NSDUH subset (nsduh_2019) is already loaded for you, and tidyverse and gtsummary are attached.
The male cross-tab
Build the MDE × SUD cross-tab for males the same way you built the female one in the lab, and request percent = "row" — that gives you P(SUD | MDE) for males.
Same pipe as the female cross-tab, but filter() to sex == "Male" and set percent = "row" inside tbl_cross() so the positive-MDE row shows P(SUD | MDE).
Look at the row-% version (P(SUD | MDE) for each sex) alongside the female table from the lab. Compare — jot it on paper or just reason it through:
- P(SUD | MDE) for females vs. males
- P(SUD | no MDE) for females vs. males
Is SUD more common among adolescents with MDE than among those without — and is that difference similar for females and males, or does the MDE–SUD association look stronger for one sex?
A quick picture of the comparison
Numbers are great, but a small plot makes the pattern snap into focus. We’ll use the common two-step pattern: first summarize the data into a small table, then plot that summary.
The tidy summary
Build one row for each of the four sex × MDE combinations, carrying two computed columns:
- p_sud — the proportion of that group with a substance use disorder: exactly the conditional P(SUD | MDE status) from the cross-tabs, and
- n — how many adolescents that proportion is based on.
To get there, group_by() sex and mde_pastyear, then summarize() those two columns. The result is a compact four-row table — exactly the shape ggplot() wants.
Group by both sex and mde_pastyear so you get one row per combination. For p_sud, mean(substance_disorder == "Positive") computes the proportion — the mean of a TRUE/FALSE vector is the fraction that are TRUE.
You should see four rows: Female × Positive, Female × Negative, Male × Positive, Male × Negative. The p_sud column is exactly the conditional P(SUD | MDE status, sex) you read off the cross-tabs.
The plot
Now turn that four-row table into a grouped bar chart. Put past-year MDE status on the x-axis, the conditional probability P(SUD | MDE status) on the y-axis, and color the bars by sex — position_dodge() sets the female and male bars side by side within each MDE category. The finished chart lets you see two things at once: that SUD is more common among adolescents with MDE (bars rise from the “No” group to the “Yes” group), and whether that increase looks similar for females and males (the paired colored bars within each category).
Map three aesthetics to columns of comorbid_by_sex: the MDE category to x, the proportion p_sud to y, and sex to fill (which splits each MDE category into two dodged bars).
Once the chart renders, make it your own — small tweaks are the fastest way to build ggplot2 fluency. A few to try:
- swap the scale_fill_manual() colors for a palette you prefer;
- label each bar with its probability using geom_text();
- show the y-axis as percentages with
scale_y_continuous(labels = scales::percent); - widen or narrow the gap between the paired bars via the position_dodge()
width; - rewrite the title and subtitle in your own words.
As a wrap-up, put the takeaway in one sentence: does the MDE–SUD association look similar for females and males, or notably different?
Back to the M06 lab → return to the lab