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)
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)
table1B <- table1A %>% group_by(carrier,dest,month) %>% summarise(count=n())
table1B
table1C <- table1B %>% mutate(countrank = min_rank(desc(count))) %>% arrange(countrank)
table1C
table1D <- table1A %>% select(carrier,dest) %>% group_by(carrier, dest) %>% summarise(count=n()) %>% arrange(desc(count))
table1D
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)
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
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).
table3A <- boston %>% select(town,chas, cmedv, rm, age) %>% filter(chas==1)
table3A
table3B <- table3A %>% mutate(rm_integer=as.integer(round(rm)))
table3B
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
graph1Di <- table3C %>% ggplot(aes(med_cmedv, med_age)) +geom_point() + xlab(label="Median Cmedv Value")+ ylab(label="Median Age")
graph1Di
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
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?
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
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?).