Lecture 7
Duke University
STA 199 - Fall 2024
September 19, 2024
Prepare for today’s application exercise: ae-07-taxes-join
Go to your ae
project in RStudio.
Make sure all of your changes up to this point are committed and pushed, i.e., there’s nothing left in your Git pane.
Click Pull to get today’s application exercise file: ae-07-taxes-join.qmd.
Wait till the you’re prompted to work on the application exercise during class before editing the file.
Use of AI tools:
The bare minimum citation must include the AI tool you’re using (e.g., ChatGPT) and your prompt. The prompt you use cannot be copied and pasted directly from the assignment; you must create a prompt yourself.
If we suspect code was generated with by / with the help of an AI tool but not cited, it’s an automatic 0 on that question + potentially further penalties as outlined in https://sta199-f24.github.io/course-syllabus.html#academic-honesty.
Plot labeling: Keep it concise and non-redundant (same info doesn’t need to be repeated, in detail, in axis labels and title).
Code style and readability:
Highlight code you want to style
Click on Addins (top of editor), scroll down to STYLER, select Style selection
Narrative: Avoid lengthy or conflicting prose
Update x-axis scale: 2012 to 2024, increments of 2 years.
Use custom colors.
Add custom labels and change theme.
ggplot(
statsci_longer,
aes(
x = year, y = n, color = degree_type
)
) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = seq(2012, 2024, 2)) +
scale_color_manual(
values = c(
"BS" = "cadetblue4",
"BS2" = "cadetblue3",
"AB" = "lightgoldenrod4",
"AB2" = "lightgoldenrod3"
)
) +
labs(
x = "Graduation year",
y = "Number of majors graduating",
color = "Degree type",
title = "Statistical Science majors over the years",
subtitle = "Academic years 2011 - 2024",
caption = "Source: Office of the University Registrar\nhttps://registrar.duke.edu/registration/enrollment-statistics"
) +
theme_minimal()
ggplot(
statsci_longer,
aes(
x = year, y = n, color = degree_type
)
) +
geom_point() +
geom_line() +
scale_x_continuous(breaks = seq(2012, 2024, 2)) +
scale_color_manual(
values = c(
"BS" = "cadetblue4",
"BS2" = "cadetblue3",
"AB" = "lightgoldenrod4",
"AB2" = "lightgoldenrod3"
)
) +
labs(
x = "Graduation year",
y = "Number of majors graduating",
color = "Degree type",
title = "Statistical Science majors over the years",
subtitle = "Academic years 2011 - 2024",
caption = "Source: Office of the University Registrar\nhttps://registrar.duke.edu/registration/enrollment-statistics"
) +
theme_minimal() +
theme(
legend.position = "inside",
legend.position.inside = c(0.1, 0.7),
legend.background = element_rect(fill = "white", color = "gray")
)
Data sets can’t be labeled as wide or long but they can be made wider or longer for a certain analysis that requires a certain format
When pivoting longer, variable names that turn into values are characters by default. If you need them to be in another format, you need to explicitly make that transformation, which you can do so within the pivot_longer()
function.
You can tweak a plot forever, but at some point the tweaks are likely not very productive. However, you should always be critical of defaults (however pretty they might be) and see if you can improve the plot to better portray your data / results / what you want to communicate.
Can you guess the variable plotted here?
# A tibble: 51 × 5
state state_tax_rate avg_local_tax_rate combined_rate
<chr> <dbl> <dbl> <dbl>
1 Alabama 0.04 0.0529 0.0929
2 Alaska 0 0.0182 0.0182
3 Arizona 0.056 0.0278 0.0838
4 Arkansas 0.065 0.0295 0.0945
5 California 0.0725 0.016 0.0885
6 Colorado 0.029 0.0491 0.0781
7 Connecticut 0.0635 0 0.0635
8 Delaware 0 0 0
9 Florida 0.06 0.01 0.07
10 Georgia 0.04 0.0338 0.0738
# ℹ 41 more rows
# ℹ 1 more variable: max_local_tax_rate <dbl>
Suppose you’re tasked with the following:
Compare the average state sales tax rates of swing states (Arizona, Georgia, Michigan, Nevada, North Carolina, Pennsylvania, and Wisconsin) vs. non-swing states.
How would you approach this task?
swing_state
with levels "Swing"
and "Non-swing"
swing_state
mutate()
with if_else()
Create a new variable called swing_state
with levels "Swing"
and "Non-swing"
.
if_else()
TRUE
FALSE
Compare the average state sales tax rates of swing states vs. non-swing states.
Suppose you’re tasked with the following:
Compare the average state sales tax rates of states on the Pacific Coast, states on the Atlantic Coast, and the rest of the states.
How would you approach this task?
coast
with levels "Pacific"
, "Atlantic"
, and "Neither"
coast
mutate()
with case_when()
Create a new variable called coast
with levels "Pacific"
, "Atlantic"
, and "Neither"
.
case_when()
case_when(
1 x > y ~ "x is greater than y",
2 x < y ~ "x is less than y",
3 .default = "x is equal to y"
)
TRUE
TRUE
TRUE
, i.e., default value
Compare the average state sales tax rates of states on the Pacific Coast, states on the Atlantic Coast, and the rest of the states.
Suppose you’re tasked with the following:
Compare the average state sales tax rates of states in various regions (Midwest - 12 states, Northeast - 9 states, South - 16 states, West - 13 states).
How would you approach this task?
region
with levels "Midwest"
, "Northeast"
, "South"
, and "West"
.region
mutate()
with case_when()
Who feels like filling in the blanks lists of states in each region? Who feels like it’s simply too tedious to write out names of all states?
Suppose we want to answer questions like:
Is there a relationship between
- number of QS courses taken
- having scored a 4 or 5 on the AP stats exam
- motivation for taking course
- …
and performance in this course?”
Each of these would require joining class performance data with an outside data source so we can have all relevant information (columns) in a single data frame.
Suppose we want to answer questions like:
Compare the average state sales tax rates of states in various regions (Midwest - 12 states, Northeast - 9 states, South - 16 states, West - 13 states).
This can also be solved with joining region information with the state-level sales tax data.
For the next few slides…
left_join()
right_join()
full_join()
inner_join()
semi_join()
anti_join()
Compare the average state sales tax rates of states in various regions (Midwest, Northeast, South, West), where the input data are:
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-07-taxes-join.qmd.
Work through the application exercise in class, and render, commit, and push your edits by the end of class.