Lecture 5
Duke University
STA 199 - Fall 2025
September 9, 2025
Suppose you have a dataset df
with 100 rows and 5 columns: x1
, x2
, x3
, x4
, and x5
. x1
is a categorical variable with levels a
and b
. You run the following code:
The resulting data frame will have:
Scan the QR code or go to app.wooclap.com/sta199. Log in with your Duke NetID.
Labs:
Submit PDF on Gradescope by the end of lab session
Make regular commits and push .qmd
and PDF to GitHub
Graded primarily for attendance, participation, collaboration, and effort primarily
Feedback provided for correctness
Homework: HW 1 due Sunday 11:59pm
.qmd
and PDF to GitHub.qmd
and PDF to GitHubgerrymander
# A tibble: 435 × 12
district last_name first_name party16 clinton16 trump16 dem16 state
<chr> <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr>
1 AK-AL Young Don R 37.6 52.8 0 AK
2 AL-01 Byrne Bradley R 34.1 63.5 0 AL
3 AL-02 Roby Martha R 33 64.9 0 AL
4 AL-03 Rogers Mike D. R 32.3 65.3 0 AL
5 AL-04 Aderholt Rob R 17.4 80.4 0 AL
6 AL-05 Brooks Mo R 31.3 64.7 0 AL
7 AL-06 Palmer Gary R 26.1 70.8 0 AL
8 AL-07 Sewell Terri D 69.8 28.6 1 AL
9 AR-01 Crawford Rick R 30.2 65 0 AR
10 AR-02 Hill French R 41.7 52.4 0 AR
# ℹ 425 more rows
# ℹ 4 more variables: party18 <chr>, dem18 <dbl>, flip18 <dbl>,
# gerry <fct>
You are given a new dataset to analyze. What are some of the first things you would do to get to know the data?
Scan the QR code or go to app.wooclap.com/sta199. Log in with your Duke NetID.
gerrymander
Rows: 435
Columns: 12
$ district <chr> "AK-AL", "AL-01", "AL-02", "AL-03", "AL-04", "AL-…
$ last_name <chr> "Young", "Byrne", "Roby", "Rogers", "Aderholt", "…
$ first_name <chr> "Don", "Bradley", "Martha", "Mike D.", "Rob", "Mo…
$ party16 <chr> "R", "R", "R", "R", "R", "R", "R", "D", "R", "R",…
$ clinton16 <dbl> 37.6, 34.1, 33.0, 32.3, 17.4, 31.3, 26.1, 69.8, 3…
$ trump16 <dbl> 52.8, 63.5, 64.9, 65.3, 80.4, 64.7, 70.8, 28.6, 6…
$ dem16 <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0…
$ state <chr> "AK", "AL", "AL", "AL", "AL", "AL", "AL", "AL", "…
$ party18 <chr> "R", "R", "R", "R", "R", "R", "R", "D", "R", "R",…
$ dem18 <dbl> 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0…
$ flip18 <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0…
$ gerry <fct> mid, high, high, high, high, high, high, high, mi…
gerrymander
Rows: Congressional districts
Columns:
Congressional district and state
2016 election: winning party, % for Clinton, % for Trump, whether a Democrat won the House election, name of election winner
2018 election: winning party, whether a Democrat won the 2018 House election
Whether a Democrat flipped the seat in the 2018 election
Prevalence of gerrymandering: low, mid, and high
district
last_name
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
|
party16 |
|
clinton16 |
|
trump16 |
|
dem16 |
|
state |
|
party18 |
|
dem18 |
|
flip18 |
|
gerry |
first_name
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
|
clinton16 |
|
trump16 |
|
dem16 |
|
state |
|
party18 |
|
dem18 |
|
flip18 |
|
gerry |
party16
clinton16
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
|
dem16 |
|
state |
|
party18 |
|
dem18 |
|
flip18 |
|
gerry |
trump16
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
numerical, continuous |
dem16 |
|
state |
|
party18 |
|
dem18 |
|
flip18 |
|
gerry |
dem16
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
numerical, continuous |
dem16 |
categorical |
state |
|
party18 |
|
dem18 |
|
flip18 |
|
gerry |
state
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
numerical, continuous |
dem16 |
categorical |
state |
categorical |
party18 |
|
dem18 |
|
flip18 |
|
gerry |
party18
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
numerical, continuous |
dem16 |
categorical |
state |
categorical |
party18 |
categorical |
dem18 |
|
flip18 |
|
gerry |
dem18
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
numerical, continuous |
dem16 |
categorical |
state |
categorical |
party18 |
categorical |
dem18 |
categorical |
flip18 |
|
gerry |
flip18
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
numerical, continuous |
dem16 |
categorical |
state |
categorical |
party18 |
categorical |
dem18 |
categorical |
flip18 |
categorical |
gerry |
gerry
Variable | Type |
---|---|
district |
categorical, ID |
last_name |
categorical, ID |
first_name |
categorical, ID |
party16 |
categorical |
clinton16 |
numerical, continuous |
trump16 |
numerical, continuous |
dem16 |
categorical |
state |
categorical |
party18 |
categorical |
dem18 |
categorical |
flip18 |
categorical |
gerry |
categorical, ordinal |
Analyzing a single variable:
Numerical: histogram, box plot, density plot, etc.
Categorical: bar plot, pie chart, etc.
`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Which of the following histograms has the most appropriate binwidth for visualizing the distribution of trump16
?
Scan the QR code or go to app.wooclap.com/sta199. Log in with your Duke NetID.
Describe the distribution of percent of vote received by Trump in 2016 Presidential Election from Congressional Districts.
Shape: The distribution of votes for Trump in the 2016 election from Congressional Districts is unimodal and left-skewed.
Center: The percent of vote received by Trump in the 2016 Presidential Election from a typical Congressional Districts is 48.7%.
Spread: In the middle 50% of Congressional Districts, 34.8% to 58.1% of voters voted for Trump in the 2016 Presidential Election.
Unusual observations: -
Analyzing the relationship between two variables:
Numerical + numerical: scatterplot
Numerical + categorical: side-by-side box plots, violin plots, etc.
Categorical + categorical: stacked bar plots
Using an aesthetic (e.g., fill, color, shape, etc.) or facets to represent the second variable in any plot
What goes in the [blank]
in the code below to do the following step for each level of gerry
?
Scan the QR code or go to app.wooclap.com/sta199. Log in with your Duke NetID.
gerrymander |>
group_by(gerry) |>
summarize(
min = min(trump16),
q25 = quantile(trump16, 0.25),
median = median(trump16),
q75 = quantile(trump16, 0.75),
max = max(trump16),
)
# A tibble: 3 × 6
gerry min q25 median q75 max
<fct> <dbl> <dbl> <dbl> <dbl> <dbl>
1 low 4.9 36.3 48.4 54.7 74.9
2 mid 6.8 34.8 48.0 57.9 79.9
3 high 9.2 33.5 50.5 60.8 80.4
Analyzing the relationship between multiple variables:
In general, one variable is identified as the outcome of interest
The remaining variables are predictors or explanatory variables
Plots for exploring multivariate relationships are the same as those for bivariate relationships, but conditional on one or more variables
y
vs. x1
, colored by x2
, faceted by x3
)Summary statistics for exploring multivariate relationships are the same as those for bivariate relationships, but conditional on one or more variables
y
and x1
, grouped by levels of x2
and x3
)Go to your ae project in RStudio.
If you haven’t yet done so, make sure all of your changes up to this point are committed and pushed, i.e., there’s nothing left in your Git pane.
If you haven’t yet done so, click Pull to get today’s application exercise file: ae-03-gerrymander-explore-I.qmd.
Work through the application exercise in class, and render, commit, and push your edits by the end of class.