library(readr)
library(tidyr)
library(readxl)
library(tidyverse)
library(ggplot2)

Question 1

##read in 
edu_long <- read_csv("education_long.csv")

## transform from long to wide
edu_wide <- edu_long |>
  pivot_wider(names_from  = Year,
              values_from = College_Completion_Rate)

##read in proverty report
povertyreport <- read_excel("PovertyReport.xlsx", sheet = 2)

##select years
edu_wide <- edu_wide |>
  select(Name, starts_with("2013-2017"))


#nrow(edu_wide)
#nrow(povertyreport)
anti_join(edu_wide, povertyreport, by = "Name")
## # A tibble: 2 × 4
##   Name          `2013-2017_Total` `2013-2017_Urban` `2013-2017_Rural`
##   <chr>                     <dbl>             <dbl>             <dbl>
## 1 United States             0.309             0.329             0.193
## 2 Puerto Rico               0.249             0.252             0.189
anti_join(povertyreport, edu_wide, by = "Name")
## # A tibble: 1 × 3
##   Name     Percent Percent_0to17
##   <chr>      <dbl>         <dbl>
## 1 National    13.4          18.4
#If you were to merge them without the removal of peurto rico data than it wouldnt
#merge seamlessly because their isnt data for every row

##join data
edu_proverty <- inner_join(edu_wide, povertyreport, by = "Name")

#grahp
ed_graph <- edu_proverty |>
  pivot_longer(starts_with("2013-2017"),
               names_to = "Group", values_to = "Completion",
               names_prefix = "2013-2017_") |>
  mutate(Completion = Completion * 100) |>
  drop_na(Completion)

ggplot(ed_graph, aes(x = Percent, y = Completion, color = Group)) +
  geom_point(alpha = 0.7, size = 2) +
  geom_smooth(se = FALSE) +
  scale_color_manual(values = c(Total = "darkgreen", Urban = "lightblue", Rural = "darkorange")) +
  labs(title = "State Poverty Rate vs. College Completion (2013-2017)",
       x = "Poverty Rate (%)",
       y = "College Completion rate (%)",
       color = "Demographic") +
  theme_bw()

#I notice that there is a higher rate of decline in college completetion when 
#the factor of proverty rises, yet the rate of decline is higher in students
#from rural backgrounds

Question 2

#read in SAT data
sat10 <- read_csv("SAT__College_Board__2010_School_Level_Results.csv")
sat12 <- read_csv("2012_SAT_Results_20250306.csv")

#convert to numeric
str(sat12)
## spc_tbl_ [478 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ DBN                            : chr [1:478] "01M292" "01M448" "01M450" "01M458" ...
##  $ SCHOOL NAME                    : chr [1:478] "HENRY STREET SCHOOL FOR INTERNATIONAL STUDIES" "UNIVERSITY NEIGHBORHOOD HIGH SCHOOL" "EAST SIDE COMMUNITY SCHOOL" "FORSYTH SATELLITE ACADEMY" ...
##  $ Num of SAT Test Takers         : chr [1:478] "29" "91" "70" "7" ...
##  $ SAT Critical Reading Avg. Score: chr [1:478] "355" "383" "377" "414" ...
##  $ SAT Math Avg. Score            : chr [1:478] "404" "423" "402" "401" ...
##  $ SAT Writing Avg. Score         : chr [1:478] "363" "366" "370" "359" ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   DBN = col_character(),
##   ..   `SCHOOL NAME` = col_character(),
##   ..   `Num of SAT Test Takers` = col_character(),
##   ..   `SAT Critical Reading Avg. Score` = col_character(),
##   ..   `SAT Math Avg. Score` = col_character(),
##   ..   `SAT Writing Avg. Score` = col_character()
##   .. )
##  - attr(*, "problems")=<pointer: 0x00000249f1ef6c90>
for (j in 3:6) {
  sat12[[j]][sat12[[j]] == "s"] <- NA
  sat12[[j]] <- as.numeric(sat12[[j]])}
str(sat12)
## spc_tbl_ [478 × 6] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
##  $ DBN                            : chr [1:478] "01M292" "01M448" "01M450" "01M458" ...
##  $ SCHOOL NAME                    : chr [1:478] "HENRY STREET SCHOOL FOR INTERNATIONAL STUDIES" "UNIVERSITY NEIGHBORHOOD HIGH SCHOOL" "EAST SIDE COMMUNITY SCHOOL" "FORSYTH SATELLITE ACADEMY" ...
##  $ Num of SAT Test Takers         : num [1:478] 29 91 70 7 44 112 159 18 130 16 ...
##  $ SAT Critical Reading Avg. Score: num [1:478] 355 383 377 414 390 332 522 417 624 395 ...
##  $ SAT Math Avg. Score            : num [1:478] 404 423 402 401 433 557 574 418 604 400 ...
##  $ SAT Writing Avg. Score         : num [1:478] 363 366 370 359 384 316 525 411 628 387 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   DBN = col_character(),
##   ..   `SCHOOL NAME` = col_character(),
##   ..   `Num of SAT Test Takers` = col_character(),
##   ..   `SAT Critical Reading Avg. Score` = col_character(),
##   ..   `SAT Math Avg. Score` = col_character(),
##   ..   `SAT Writing Avg. Score` = col_character()
##   .. )
##  - attr(*, "problems")=<pointer: 0x00000249f1ef6c90>
colSums(is.na(sat12[, 3:6]))
##          Num of SAT Test Takers SAT Critical Reading Avg. Score 
##                              57                              57 
##             SAT Math Avg. Score          SAT Writing Avg. Score 
##                              57                              57
#joint apperence
length(intersect(sat10$DBN, sat12$DBN))
## [1] 431
#431 intersections between both datasets 

sat10 <- sat10[!duplicated(sat10$DBN), ]

#print names
names(sat10)
## [1] "DBN"                   "School Name"           "Number of Test Takers"
## [4] "Critical Reading Mean" "Mathematics Mean"      "Writing Mean"
names(sat12)
## [1] "DBN"                             "SCHOOL NAME"                    
## [3] "Num of SAT Test Takers"          "SAT Critical Reading Avg. Score"
## [5] "SAT Math Avg. Score"             "SAT Writing Avg. Score"
names(sat10) <- names(sat12)
names(sat10)[3:6] <- paste0(names(sat10)[3:6], " 2010")
names(sat12)[3:6] <- paste0(names(sat12)[3:6], " 2012")

#merge
sat10_12 <- merge(sat10, sat12[, -2], by = "DBN")
dim(sat10_12) 
## [1] 431  10
#graph
ggplot(sat10_12, aes(x = `SAT Math Avg. Score 2010`, y = `SAT Math Avg. Score 2012`)) +
  geom_point(alpha = 0.6, color = "lightblue") +#
  geom_smooth(method = "lm", se = FALSE, color = "darkgreen") +
  labs(title = "SAT Math Scoring: 2010 vs 2012",
       x = "Mean SAT Math, 2010", y = "Mean SAT Math, 2012") +
  theme_classic()

#The progession in change in math scoring hardly changed from the 2 year gap

Question 2B

#read in
demo <- read_csv("2006_-_2012_School_Demographics_and_Accountability_Snapshot.csv")
dim(demo) 
## [1] 10075    38
length(unique(demo$DBN))
## [1] 1594
table(demo$schoolyear)   
## 
## 20052006 20062007 20072008 20082009 20092010 20102011 20112012 
##     1356     1386     1410     1441     1475     1498     1509
demo <- demo[demo$schoolyear %in% c(20092010, 20112012), ]


demo <- demo[, c("DBN", "schoolyear", "frl_percent")]
demo$year <- ifelse(demo$schoolyear == 20092010, 2010, 2012)
demo <- demo[, c("DBN", "year", "frl_percent")]

demo <- pivot_wider(demo, names_from = year, values_from = frl_percent,
                         names_prefix = "frl_percent ")

names(demo)
## [1] "DBN"              "frl_percent 2010" "frl_percent 2012"
sat_demo <- merge(sat10_12, demo, by = "DBN")
dim(sat_demo)
## [1] 410  12
cuts <- quantile(sat_demo$`frl_percent 2012`, probs = seq(0, 1, 0.2))
cuts
##    0%   20%   40%   60%   80%  100% 
## 15.80 55.08 66.32 72.54 78.30 95.10
sat_demo$poverty <- cut(sat_demo$`frl_percent 2012`, breaks = cuts, include.lowest = TRUE,
                        labels = c("low", "medium-low", "medium", "medium-high", "high"))
table(sat_demo$poverty)
## 
##         low  medium-low      medium medium-high        high 
##          82          82          82          85          79
sat_sub <- sat_demo[, c("DBN", "SCHOOL NAME", "poverty",
                        grep("Avg. Score", names(sat_demo), value = TRUE))]
names(sat_sub)
## [1] "DBN"                                 
## [2] "SCHOOL NAME"                         
## [3] "poverty"                             
## [4] "SAT Critical Reading Avg. Score 2010"
## [5] "SAT Math Avg. Score 2010"            
## [6] "SAT Writing Avg. Score 2010"         
## [7] "SAT Critical Reading Avg. Score 2012"
## [8] "SAT Math Avg. Score 2012"            
## [9] "SAT Writing Avg. Score 2012"
sat_long <- pivot_longer(sat_sub, cols = grep("Avg. Score", names(sat_sub)),
                         names_to = c("subject", "year"),
                         names_pattern = "SAT (.*) Avg. Score (\\d{4})",
                         values_to = "score")
head(sat_long)
## # A tibble: 6 × 6
##   DBN    `SCHOOL NAME`                               poverty subject year  score
##   <chr>  <chr>                                       <fct>   <chr>   <chr> <dbl>
## 1 01M292 Henry Street School for International Stud… high    Critic… 2010    391
## 2 01M292 Henry Street School for International Stud… high    Math    2010    425
## 3 01M292 Henry Street School for International Stud… high    Writing 2010    385
## 4 01M292 Henry Street School for International Stud… high    Critic… 2012    355
## 5 01M292 Henry Street School for International Stud… high    Math    2012    404
## 6 01M292 Henry Street School for International Stud… high    Writing 2012    363
dim(sat_long)     
## [1] 2460    6
sat_avg <- aggregate(score ~ poverty + subject + year, data = sat_long, FUN = mean)

sat_avg$subject_year <- paste(sat_avg$subject, sat_avg$year)

#graph
ggplot(sat_avg, aes(x = poverty, y = score, color = subject_year, group = subject_year)) +
  geom_point(size = 2) +
  geom_line() +
  scale_color_manual(values = c("lightblue", "blue",
                                "lightgreen", "darkgreen",
                                "orange", "red")) +
  scale_y_continuous(breaks = seq(360, 500, by = 10)) +
  labs(title = "Average SAT Score by Poverty Level (2010 vs 2012)",
       x = "Poverty level", y = "Average score", color = "Subject/Year") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 30, hjust = 1),
        panel.grid.major = element_line(size= .5, color = "grey"))

#I used ai to give me the line of code, to add graph lines that aligned with
#the text. came to the conclusion that it would work since it was a fairly
#simple request