# 
titanic <- read.delim("https://s3.amazonaws.com/pbreheny-data-sets/titanic.txt")


# 
barplot(height = table(titanic$Survived, titanic$Class),
        beside=FALSE, col = c("blue", "red"), main = "Survival Rate Between Classes")
legend(x = "topleft", legend = c("Died", "Survived"), title = "Survival Status", fill = c("blue", "red"))


# 
par(mfrow=c(1,2)) # To turn it back to normal, run par(mfrow = c(1,1))


# 
{
par(mfrow=c(1,2))

# Bar plot for the females
barplot(height = table(titanic$Survived, titanic$Class, titanic$Sex)[,,1],
        beside=FALSE, 
        col = c("blue", "red"), 
        main = "Female Survival Rate vs Classes")
legend(x = "topleft", 
       legend = c("Died", "Survived"), 
       title = "Survival Status", 
       fill = c("blue", "red"))

# Bar plot for the males
barplot(height = table(titanic$Survived, titanic$Class, titanic$Sex)[,,2],
        beside=FALSE, 
        col = c("blue", "red"), 
        main = "Male Survival Rate vs Classes")
legend(x = "topleft", 
       legend = c("Died", "Survived"), 
       title = "Survival Status", 
       fill = c("blue", "red"))
}


# 
tailgating <- read.delim("http://myweb.uiowa.edu/pbreheny/data/tailgating.txt")


# 
summary(tailgating$Distance)


# 
sd(tailgating$Distance)


# 
by(tailgating$Distance, tailgating$Group, summary)


# out.width='.6\\linewidth'
hist(tailgating$Distance)


# out.width='.6\\linewidth'
hist(tailgating$Distance, main = "Histogram of Tailgating Distance", 
     xlab = "Following Distance")
abline(v=mean(tailgating$Distance), 
       col="steelblue4", lwd=2) #lwd makes the line thicker (line width)
abline(v=median(tailgating$Distance), col="orange", 
       lwd=2, lty = 2)#lty makes the line dashed (line type) 
legend(x = "topright", 
       legend = c("Median", "Mean"), 
       col = c("orange", "steelblue4"),
       lwd = 2, lty = c(2,1))

# the mean is the solid line and the median is the dashed line 


# out.width='.6\\linewidth'
hist(tailgating$Distance, breaks = seq(0, 400, 2)) # bin size of 2

hist(tailgating$Distance, breaks = seq(0, 400, 100)) # bin size of 100

hist(tailgating$Distance, breaks = seq(0, 400, 10)) # bin size of 10



# out.width='.6\\linewidth'

#customized labels, solid color & white border   
hist(tailgating$Distance, col= "pink", border="white", breaks = seq(1, 400, 10),
     xlab="Distance",
     ylab="Frequency",
     main = "")



# 
par(mfrow=c(2,2)) # view all four histograms in a 2 by 2 window

hist(tailgating$Distance[tailgating$Group=="ALC"], col= "yellow", breaks = seq(1, 400, 10), 
     main = "", xlab = "ALC")
hist(tailgating$Distance[tailgating$Group=="MDMA"], col= "red", breaks = seq(1, 400, 10), 
     main = "", xlab = "MDMA")
hist(tailgating$Distance[tailgating$Group=="NODRUG"], col= "blue", breaks = seq(1, 400, 10), 
     main = "", xlab = "NoDrug")
hist(tailgating$Distance[tailgating$Group=="THC"], col= "green", breaks = seq(1, 400, 10), 
     main = "", xlab = "THC")


# out.width='.6\\linewidth'
boxplot(tailgating$Distance ~ tailgating$Group, col=rainbow(4))


# out.width='.6\\linewidth'
boxplot(tailgating$Distance ~ tailgating$Group, col= rainbow(4), outline=FALSE)


# 
quantile(tailgating$Distance, 0.30)


# echo=FALSE
library(kableExtra)

tab2 <- data.frame(. = c("Don't Reject", "Reject", "Total"),
                   NullT = character(3),
                   NullF = character(3),
                   Total = character(3))
colnames(tab2) <- c("", "True Null", "False Null", "Total")

kable(tab2) %>%
  kable_styling(bootstrap_options = c("striped", "hover"))



# echo=FALSE
sitstand <- data.frame("Group" = c("Sitting", "Standing"),
                       Perfect.Responses = c(71, 94),
                       Total.Responses = c(134, 142),
                       Perfect_Responses = c(88, 57),
                       Total_Responses = c(104, 90))
kable(sitstand)%>%
  kable_styling()%>%
  add_header_above(c(" " = 1, "Drinking" = 2, "Eating" = 2))


