## R code Lab 5
## 2/16/15

tips <- read.table("http://myweb.uiowa.edu/pbreheny/data/tips.txt", header = TRUE)
attach(tips)

lm(Tip ~ TotBill)
summary(lm(Tip ~ TotBill))

tip_rate <- 100*Tip/TotBill
cor(TotBill, Tip)

tips2 <- data.frame(tips, tip_rate)
require(lattice)
histogram(tip_rate)

BigTip <- tip_rate > 20

barchart(Sex, freq = F)
histogram( ~ Sex)
histogram( ~ Sex | BigTip)
histogram(Sex)
histogram(Time)



## how does tip rate change with total bill?
lm.y <- lm(tip_rate ~ TotBill)
summary(lm.y)
plot(TotBill, tip_rate)
abline(lm.y)

## bullet one, many answers
small <- tips2[TotBill < 30,8]
large <- tips2[TotBill >= 30,8]
var(small)
var(large)

mean(small)
mean(large)
hist(small, breaks = 5)
hist(large, breaks = 5)

## bullet two
smoker <- tips2[Smoker == "Yes",]
nonsmoker <- tips2[Smoker == "No",]

lm.smoker <- lm(tip_rate ~ TotBill, data = smoker)
lm.nonsmoker <- lm(tip_rate ~ TotBill, data = nonsmoker)
summary(lm.smoker)
summary(lm.nonsmoker)

plot(smoker$TotBill, smoker$tip_rate, main = "Smoker")
abline(lm.smoker)

plot(nonsmoker$TotBill, nonsmoker$tip_rate, main = "Non Smoker")
abline(lm.nonsmoker)


## Play around with numbers to see how the predictions change
predict(lm.smoker)[40]
predict(lm.nonsmoker)[40]

MD <- tips2[Sex == "M" & Time == "Day",]
MN <- tips2[Sex == "M" & Time == "Night",]
FD <- tips2[Sex == "F" & Time == "Day",]
FN <- tips2[Sex == "F" & Time == "Night",]

par(mfrow = c(2,2))
hist(MD$tip_rate, xlab = "Tip Rate", main = "Sex = Male, Time = Day")
hist(MN$tip_rate, xlab = "Tip Rate", main = "Sex = Male, Time = Night")
hist(FD$tip_rate, xlab = "Tip Rate", main = "Sex = Female, Time = Day")
hist(FN$tip_rate, xlab = "Tip Rate", main = "Sex = Female, Time = Night")

## Does tipping behavior change lunch and dinner? find rho.
lunch <- tips[Time == "Day",]
dinner <- tips[Time == "Night",]

cor(lunch$TotBill, lunch$Tip)
cor(dinner$TotBill, dinner$Tip)

detach(tips)
