library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.3.3
college_data <- read.csv('/Users/gavin/Downloads/college.csv')
## Question 1
num_variables <- ncol(college_data)
num_observations <- nrow(college_data)
missing_values <- sum(is.na(college_data))

cat("Number of variables:", num_variables, "\n")
## Number of variables: 17
cat("Number of observations:", num_observations, "\n")
## Number of observations: 1269
cat("Missing values:", missing_values, "\n")
## Missing values: 2
## Question 2
average_admission_rate <- college_data %>%
  group_by(state) %>%
  summarise(avg_admission_rate = mean(admission_rate, na.rm = TRUE))

barplot <- ggplot(average_admission_rate, aes(x = state, y = avg_admission_rate)) +
  geom_bar(stat = "identity") +
  labs(x = "State", y = "Average Admission Rate") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

print(barplot)

## Question 3
scatter_plot <- ggplot(college_data, aes(x = median_debt, y = loan_default_rate)) +
  geom_point() +
  geom_smooth(method = "gam") +
  labs(x = "Median Debt", y = "Loan Default Rate")

print(scatter_plot)
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

## Question 4
bubble_plot <- ggplot(college_data, aes(x = median_debt, y = loan_default_rate, size = undergrads)) +
  geom_point() +
  geom_smooth(method = "gam", aes(weight = undergrads)) +
  labs(x = "Median Debt", y = "Loan Default Rate", size = "Number of Undergrads")

print(bubble_plot)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 2 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Warning: The following aesthetics were dropped during statistical transformation: size.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?
## Warning: Removed 2 rows containing missing values or values outside the scale range
## (`geom_point()`).

## Question 5
print("The weighted trend in question 4 might provide a better understanding of the relationship considering the influence of the number of undergrads.")
## [1] "The weighted trend in question 4 might provide a better understanding of the relationship considering the influence of the number of undergrads."
## Question 6
density_plot <- ggplot(college_data, aes(x = log10(undergrads), fill = highest_degree)) +
  geom_density(alpha = 0.5) +
  labs(x = "Log10(Undergrads)", y = "Density", fill = "Highest Degree")

print(density_plot)

## Question 7
stacked_density <- ggplot(college_data, aes(x = log10(undergrads), fill = highest_degree)) +
  geom_density(alpha = 0.5, position = "stack") +
  labs(x = "Log10(Undergrads)", y = "Density", fill = "Highest Degree")

print(stacked_density)

## Question 8
degree_counts <- college_data %>%
  group_by(highest_degree) %>%
  summarise(count = n())

barplot_degree_counts <- ggplot(degree_counts, aes(x = highest_degree, y = count)) +
  geom_bar(stat = "identity") +
  labs(x = "Highest Degree", y = "Number of Schools")

print(barplot_degree_counts)

## Question 9
improved_stacked_density <- ggplot(college_data, aes(x = log10(undergrads), fill = highest_degree)) +
  geom_density(aes(weight = after_stat(count)), alpha = 0.5, position = "stack") +
  labs(x = "Log10(Undergrads)", y = "Density", fill = "Highest Degree")

print(improved_stacked_density)

## Question 10
association_plot <- ggplot(college_data, aes(x = median_debt, y = admission_rate, color = control)) +
  geom_point(size = 3, alpha = 0.7) +
  facet_wrap(~control) +
  labs(x = "Median Debt", y = "Admission Rate", color = "Control") +
  ggtitle("Association between Median Debt and Admission Rate by Control Type")

print(association_plot)

"In the plot: Each point represents a college, the x-axis represents the median debt, the y-axis represents the admission rate."
## [1] "In the plot: Each point represents a college, the x-axis represents the median debt, the y-axis represents the admission rate."
"Observations: In both public and private colleges, there seems to be a trend of higher median debt being associated with lower admission rates. Public colleges generally appear to have lower median debt compared to private colleges. There seems to be more variability in admission rates among private colleges compared to public colleges."
## [1] "Observations: In both public and private colleges, there seems to be a trend of higher median debt being associated with lower admission rates. Public colleges generally appear to have lower median debt compared to private colleges. There seems to be more variability in admission rates among private colleges compared to public colleges."
####Question 11

#What are the top 5 least affordable colleges in the US?
top_5_expensive <- college_data %>%
  arrange(desc(tuition)) %>%
  head(5)

tuition_barplot <- ggplot(top_5_expensive, aes(x = reorder(name, -tuition), y = tuition)) +
  geom_bar(stat = "identity", fill = "blue") +
  labs(x = "College Name", y = "Tuition", title = "Top 5 Most Expensive Colleges") +
  theme(axis.text.x = element_text(angle = 90))

print(tuition_barplot)

#What are the top 5 colleges that pay their faculty the most on average?
top_5_salary <- college_data %>%
  arrange(desc(faculty_salary_avg)) %>%
  head(5)

salary_barplot <- ggplot(top_5_salary, aes(x = reorder(name, -faculty_salary_avg), y = faculty_salary_avg)) +
  geom_bar(stat = "identity", fill = "salmon") +
  labs(x = "College Name", y = "Average Faculty Salary", title = "Top 5 Colleges with Highest Faculty Salaries") +
  theme(axis.text.x = element_text(angle = 90))

print(salary_barplot)