library("nycflights13")
library("mlbench")
library("magrittr")
library("dplyr")
## 
## 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")
library("reshape2")

flights<- as.data.frame(flights)
data(BostonHousing2)
boston<-BostonHousing2
rm(BostonHousing2)

Question 1

Use dplyr and the pipe operator(s) on the flights dataset to determine the following

Part A

Create a table with all the flights for 3 carriers (UA, DL, AA) to each of the following airports Chicago ORD or Atlanta (ATL) airports in July and Aug.

table1A <- flights %>% select(carrier, dest, month) %>% filter((carrier == "UA" | carrier == "DL" | carrier == "AA") & (dest == "ORD" | dest == "ATL") & (month == 7 | month == 8))
summary(table1A)
##    carrier              dest               month      
##  Length:4230        Length:4230        Min.   :7.000  
##  Class :character   Class :character   1st Qu.:7.000  
##  Mode  :character   Mode  :character   Median :8.000  
##                                        Mean   :7.505  
##                                        3rd Qu.:8.000  
##                                        Max.   :8.000
head(table1A)

Part B

Using the output of 1.a. above, determine the counts for the total number of flights for the 3 carriers (UA, DL, AA) to each of the following airports Chicago ORD or Atlanta (ATL) airport in July and Aug. Want table with counts by airline by airport by month.

table1B <- table1A %>% group_by(carrier,dest,month) %>% summarise(count=n())
table1B

Part C

Using the output of 1.b. above, use the min_rank() function and add a new column with the ranking by carrier by airport by month with the highest rank associated with the largest count.

table1C <- table1B %>% mutate(countrank = min_rank(desc(count))) %>% arrange(countrank) 
table1C

Part D

Using the output of 1.a. above (NOT 1.b.), create a new column with the total number of flights by carrier by airport. Then arrange this new column largest to smallest.

 table1D <- table1A %>% select(carrier,dest) %>% group_by(carrier, dest) %>% summarise(count=n()) %>% arrange(desc(count))
table1D

Question 2

Use dplyr and the pipe operator(s) on the flights dataset to determine the following

Part A

Create a table with the departure delay times of all UA, DL and AA flights by month, then remove all delay values less than 0, greater than 30, and remove all NAs

table2A <-flights %>% select(carrier, dep_delay, month) %>% group_by(carrier,month) %>% filter(dep_delay>0 & dep_delay <30 & !is.na(dep_delay) & (carrier=="UA" | carrier=="DL" | carrier =="AA"))
summary(table2A)
##    carrier            dep_delay          month       
##  Length:36090       Min.   : 1.000   Min.   : 1.000  
##  Class :character   1st Qu.: 3.000   1st Qu.: 4.000  
##  Mode  :character   Median : 7.000   Median : 7.000  
##                     Mean   : 9.676   Mean   : 6.664  
##                     3rd Qu.:15.000   3rd Qu.:10.000  
##                     Max.   :29.000   Max.   :12.000
head(table2A)

Part B

Then pipe this information to ggplot2 and plot violin plots of the departure delay time by month and facet by carrier

graph2B <-table2A %>% ggplot(aes(x=as.factor(month), y = dep_delay)) + geom_violin(aes(fill=as.factor(month)), draw_quantiles = c(0.25, 0.5, 0.75), trim=FALSE, adjust = 0.3) + xlab(label="Month") + ylab(label="Departure Delay in Minutes") + stat_summary(fun.y="mean", geom="point", shape=8, size=3, color="red") + facet_grid(carrier~ .) + scale_x_discrete(labels=c("Jan", "Feb", "Mar","Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) + guides(fill=FALSE)
graph2B

Part C

Start again with the output from 2.a. then pipe this information to ggplot2 and plot a stacked density plot of departure delay on the X axis, count from 0 to 1 on the Y axis and month as the fill (all carriers lumped together). Which 4 months dominate at delay = 30?

graph2C <- table2A %>% ggplot(aes(x = dep_delay,..count.. ,fill = as.factor(month))) + geom_density(position="fill", adjust=3/5) + xlab(label="Departure Delay in Minutes")+ ylab(label="Count") + labs(fill="Month") + scale_fill_hue(labels=c("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")) + xlim(0,30) + ylim(0,1.00)
graph2C

The four months that comprise the greatest amount of deta at dep_delay=30 are December, August, July, and June (about 40%-50% of the sample).

Question 3

Use dplyr and the pipe operator(s) on the BostonSousing2 dataset to determine the following

Part A

Create a table for all towns that have houses on the Charles River and include the corresponding cmedv, rm, and age information. Eliminate all other columns

table3A <- boston %>% select(town,chas, cmedv, rm, age) %>% filter(chas==1)
table3A

Part B

Create a new column by rounding rm and converting it to an integer

table3B <- table3A %>% mutate(rm_integer=as.integer(round(rm)))
table3B

Part C

Determine the median values and counts of cmedv and age by town then by the new rm column from 3.b. above and name these new columns appropriately

table3C <- table3B %>% group_by(town,rm_integer) %>% select(town,cmedv, age, rm_integer) %>% summarise(med_cmedv = median(cmedv), count = n(), med_age = median(age))
table3C

Part D

Pipe the results of 3.c. to ggplot and plot a scatter plot (HINT: use geom_point) with

Part i. X = median cmedv vs Y = median age

graph1Di <- table3C %>% ggplot(aes(med_cmedv, med_age)) +geom_point() + xlab(label="Median Cmedv Value")+ ylab(label="Median Age")
graph1Di

Part ii. add a loess fitting curve

graph1Dii <- table3C %>% ggplot(aes(med_cmedv, med_age)) +geom_point() + xlab(label="Median Cmedv Value")+ ylab(label="Median Age") + geom_smooth() + ylim(0,130)
graph1Dii

Part iii. make the size of the plotted points dependent on the rounded integer value of rm from 3.b

graph1Diii <- table3C %>% ggplot(aes(x = med_cmedv, y = med_age, size=rm_integer)) +geom_point() + xlab(label="Median Cmedv Value")+ ylab(label="Median Age") + geom_smooth() + labs(size="RM") + ylim(0,130)
graph1Diii

I spent a lot of time trying to figure out how to remove the blue line from the legend and couldn’t figure it out! do you know how?

Part iv. make the color of the plotted points dependent on the count you calculated from 3.c

graph1Div <- table3C %>% ggplot(aes(x = med_cmedv, y = med_age, size=rm_integer, color=count)) +geom_point() + xlab(label="Median Cmedv Value")+ ylab(label="Median Age") + geom_smooth(method="loess", se=T) + labs(size="RM", color="Count")+ ylim(0,130)
graph1Div

Part v. Briefly describe any trend you see between median age and cmedv

There seems to be a positive quadratic trend between age and median house value, whre very low and very high median house value are associated with higher age, but values in the middle of median house value are associated with lower age. Room number alone seems to have little impact on the relation between median house value and age; however, when count and room number are evaluated together, it seems that rooms with a higher count value have higher age and lower median income values. Therefore, it seems houses that are more rare combinations of town and room number have higher median values (perhaps unique houses are valueable?).