Assignment 08

Author

Anna StPierre

Open the assign08.qmd file and complete the exercises.

The Grades.sqlite file is preloaded into your working directory. In case there are any issues, you can also download it if you need to. It is up to you how much you want to do directly in SQL versus using R to complete the exercises below. Note: you will receive deductions for not using tidyverse syntax when applicable in this assignment. That includes the use of filter, mutate, and the up-to-date pipe operator |>.

The Grading Rubric is available at the end of this document.

Exercises

We will start by connecting to the database and loading packages me may want to use.

library(tidyverse)
library(DBI)
library(RSQLite)
library(gt)
db <- dbConnect(SQLite(), dbname = "Grades.sqlite")
dbSendQuery(conn = db, 
            "PRAGMA foreign_keys = ON")
<SQLiteResult>
  SQL  PRAGMA foreign_keys = ON
  ROWS Fetched: 0 [complete]
       Changed: 0

Exercise 1

Recreate the graph below showing the total students by course in Spring 2015.

library(tidyverse)
library(DBI)
library(RSQLite)
library(gt)

library(stringr)

spring_counts <- tbl(db, "sections") %>%
  inner_join(tbl(db, "grades"), by = "section_id") %>%
  filter(year == "2015", semester == "Spring") %>%
  collect() %>%                            # ← bring into R
  mutate(course = str_extract(name, "^[^-]+")) %>%
  group_by(course) %>%
  summarise(n_students = n_distinct(student_id), .groups = "drop")
Warning: Closing open result set, pending rows
ggplot(spring_counts, aes(x = course, y = n_students)) +
  geom_col(fill = "gray30") +
  labs(
    title = "Total students by course, Spring 2015",
    x     = "Section",
    y     = "Number of students"
  ) +
  theme_minimal()

Exercise 2

Show enrollments by section for the entire year 2015. Make sure you include year, semester, course name, section_id and the number of students in each section. Arrange the table by semester so that all of the Fall sections are listed first.

library(stringr)

enrollments_2015 <- tbl(db, "sections") %>%
  inner_join(tbl(db, "grades"), by = "section_id") %>%
  filter(year == "2015") %>%
  # aggregate counts in SQL
  group_by(year, semester, name, section_id) %>%
  summarise(n_students = n_distinct(student_id), .groups = "drop") %>%
  # now bring into R
  collect() %>%
  # local string/factor operations
  mutate(
    course   = str_extract(name, "^[^-]+"),
    semester = factor(semester, levels = c("Fall", "Spring"))
  ) %>%
  arrange(semester)

# render with gt
enrollments_2015 %>%
  select(year, semester, course, section_id, n_students) %>%
  gt() %>%
  tab_header(title = "Enrollments by Section, 2015")
Enrollments by Section, 2015
year semester course section_id n_students
2015 Fall BUS 377 68813 36
2015 Fall MBA 676 38737 33
2015 Fall MBA 676 86362 39
2015 Spring BUS 345 25822 31
2015 Spring MBA 674 29369 24
2015 Spring MBA 674 42666 40

Exercise 3

Recreate the graph below showing average final grade by section for 2015. The vertical red line showing the final average across all sections for the year is added using geom_vline().

section_avgs <- tbl(db, "sections") %>%
  inner_join(tbl(db, "grades"), by = "section_id") %>%
  filter(year == "2015") %>%
  mutate(section = name) %>%
  group_by(section) %>%
  summarise(avg_final = mean(final_avg), .groups = "drop") %>%
  collect()
Warning: Missing values are always removed in SQL aggregation functions.
Use `na.rm = TRUE` to silence this warning
This warning is displayed once every 8 hours.
overall_avg <- mean(section_avgs$avg_final)

ggplot(section_avgs, aes(x = avg_final, y = fct_reorder(section, avg_final))) +
  geom_col(fill = "blue") +
  geom_vline(xintercept = overall_avg, color = "red", size = 1) +
  labs(
    title   = "Average final grade by section, 2015",
    x       = "Average final grade",
    y       = "Section",
    caption = "Red line is the overall average for the year across all sections"
  ) +
  theme_minimal()
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.

Exercise 4

Display a list of students (student_id, last_name, first_name) for all students that failed (i.e., final_avg < 65) MBA 674 in the Spring of 2015.

failures <- tbl(db, "sections") %>%
  filter(
    year     == "2015",
    semester == "Spring",
    sql("name LIKE 'MBA 674%'")            # SQL-compatible prefix match
  ) %>%
  inner_join(tbl(db, "grades"),   by = "section_id") %>%
  filter(final_avg < 65) %>%
  inner_join(tbl(db, "students"), by = "student_id") %>%
  select(student_id, last_name, first_name) %>%
  distinct() %>%
  collect()                              # now pull into R

failures %>%
  gt() %>%
  tab_header(title = "Students who failed MBA 674, Spring 2015")
Students who failed MBA 674, Spring 2015
student_id last_name first_name
6106351 Middleton Sheridan
7197441 Brierley Sergio
7237806 Fletcher Vicky
7352157 Gonzales Kyrie
9553576 Garcia Daniel

Submission

To submit your assignment:

  • Change the author name to your name in the YAML portion at the top of this document
  • Render your document to html and publish it to RPubs.
  • Submit the link to your Rpubs document in the Brightspace comments section for this assignment.
  • Click on the “Add a File” button and upload your .qmd file for this assignment to Brightspace.

Grading Rubric

Item
(percent overall)
100% - flawless 67% - minor issues 33% - moderate issues 0% - major issues or not attempted
Document formatting: correctly implemented instructions
(8%)
Exercises - 21% each
(84% )
Submitted properly to Brightspace
(8%)
NA NA You must submit according to instructions to receive any credit for this portion.