This is to help 
1. Reenforce the coding that we have gone over and 
2. Help make a boring lecture slightly less boring

I go into more detail on some of the functions like subset() and aggregate() towards the end of the file


```{r}
colleges <- read.csv("https://remiller1450.github.io/data/Colleges2019_Complete.csv")

head(colleges) #look at the first few rows of data

colnames(colleges) #and look at the column names
```


```{r}
library(ggplot2) #load ggplot2

ggplot(data = colleges, #data is colleges
       aes(x = PercentFemale)) + #and our x-axis is % female
  geom_histogram() + #and I want a histogram
  theme_bw() #with a black and white theme


#summary summarizes information  about the thing fed to it
summary(colleges$PercentFemale)
#in particular the five number summary (plus the mean) 
#for a numeric vector
```



```{r}
#same battleplan as the above ones but now we switch
#out PercentFemale with Enrollment
ggplot(data = colleges,
       aes(x = Enrollment)) +
  geom_histogram() +
  theme_bw()

#and this time I swithced out geom_histogram for geom_boxplot
ggplot(data = colleges,
       aes(x = Enrollment)) +
  geom_boxplot() +
  theme_bw()

#summary summarizes information  about the thing fed to it
summary(colleges$Enrollment)
```



# Enrollment on Log base 10 Scale
```{r}
#Very little has to change for the graph!
ggplot(data = colleges,
       aes(x = Enrollment)) +
  geom_histogram() +
  theme_bw() +
  scale_x_log10() #this is all we add, it says the x-axis is on
                  #a log base 10 scale


#################
#Calculating the five number summary is a bit harder
#We have to make a new variable via a transformation 


#new variable time
#We are creating a new column on our data set
#called enrollment_log10. We are then assigning
#the right hand side to that column.

#The right hand side is the function log() applied
#to our enrollment buuut we have changed the base 
#of the log to base = 10 (instead of e)
colleges$enrollment_log10 <- log(colleges$Enrollment,
                                 base = 10)

colnames(colleges) #glance at the column names to see
#if our new variable is there

#and now we look at the summary
summary(colleges$enrollment_log10)
```


# Correcting the Wrong Axis
Shout out to your mentor for figuring out which variable was used to make the graph. It's FourYearComp_Females it turns out. Hard to find the right variable but the code to make the graph isn't hard at all
```{r}
ggplot(data = colleges,
       aes(x = FourYearComp_Females)) +
  geom_histogram() +
  theme_classic() 

summary(colleges$FourYearComp_Females)
```


# % White Enrollment

```{r}
ggplot(data = colleges,
       aes(x = PercentWhite)) +
  geom_histogram() +
  theme_classic() 

summary(colleges$PercentWhite)
```


# Median Debt
```{r}
ggplot(data = colleges,
       aes(x = Debt_median)) +
  geom_histogram(bins = 30) +
  theme_classic() +
  facet_grid(Private~.)

summary(colleges$Debt_median)

#aggregate your data. Much more info below
aggregate(Debt_median ~ Private,
          data = colleges,
          FUN = summary)
```


#Finding Grinnell College
```{r}
subset(colleges,
       Name == "Grinnell College") #Q1 and Q3 are the same....

head(colleges)

#several way to calculate if they are the same but
#since they are numeric lets be direct and subtract 
colleges$ACT_Q1 - colleges$ACT_Q3 
```



# Five number summary for Enrollment by Private
We will use the aggregate() function for this. There is (almost) always three parts that need to be specfied. 

1. The easiest is the data which behaes the same like ggplot() where you just need to say data = MY_DATA_HERE.
2.  The second is the FUNction you want to be applied. In the examples below I use the summary() and mean() functions 
3. Is the formula where the left hand side of the tilda (squiggly line) is the response variable and the right hand side is the explanatory variables. 

The reason we would do this at all is that it allows us to find statisics (5 number summary, mean, etc...) for each category of a nominal variable (eg Public vs Private colleges) OR the combinations of multiple nominal variables (eg Public/Private for each region of the US, see below)

```{r}
#aggregate your data
aggregate(Enrollment ~ Private, 
          #the left hand side is the response var
          #the right hand side is the explanatory
          #variableSSS (multiple is allowed, see below)
          
          data = colleges, #say what your data is
          
          FUN = summary) 
              #the FUNction we want is summary()


aggregate(Enrollment ~ Region, #same as above
          data = colleges,
          FUN = mean) #but now our FUNcation is mean()

#The below aggregate() finds the means of each
#combination of Region and Private
aggregate(Enrollment ~ Region + Private,
          data = colleges,
          FUN = mean)


#the below function is subset which lets take a 
#subset (or smaller grouping) of our data. The first
#argument is the data's name and the second is 
# PARAMETER_OF_INTERST == "CATEGORY_IM_INTERESTED_IN"
#Note the double equal sign which in R is a comparison
#operator/functon asking if the two sides are equal (TRUE)
#or if they are different (FALSE)
subset(colleges,
       Region == 'Rocky Mountains')
#It's Brigham Young Uni. that keeps making
#the data for Rocky Mountains region act
#so strangely
```



# Rebuild the boxplots below

```{r}
#this doesn't match the notes. Upon closer 
#inspection you realize the notes are on log base
#10 scale....
ggplot(data = colleges,
       aes(x = Private,
           y = Enrollment)) +
  
  #Please note that we use fill and not 
  #color to inject color into the inside of
  #the boxplot (color = Private will color
  #the outlines of the symbols. Try it!
  geom_boxplot(aes(fill = Private))

#and now we include the log base ten code and
#we include a useful y label so it's not as
#easy to misinterpret the graph
ggplot(data = colleges,
       aes(x = Private,
           y = Enrollment)) +
  geom_boxplot(aes(
                   fill = Private)) +
  scale_y_log10()  

ggplot(data = colleges,
       aes(x = Private,
           y = Enrollment)) +
  geom_boxplot(aes(
                   fill = Private)) +
  scale_y_log10() +
  
  #The below line is CRITICAL! as most people
  #would miss the face it's on the log scale
  ylab("Enrollment on Log base 10 scale")
```



# Boxplot vs Histogram Outlier Detection
First we generate 10,000 observations from a normal distribution (bell shaped curve)
```{r}
x <- rnorm(n = 10000) #rnorm stands for Random NORMal
                      #and n is the number of obs. we
                      #want

#For speed and efficiency I'm using base plot() functions
#instead of ggplot2() which is overkill for the simple
#graphs I want to make
hist(x) #hist() for a histogram
boxplot(x) #boxplot() for a boxplot
```