setwd("M:\\General Research Directory\\171-161\\Data")
  # similar to cd in unix
tips <- read.delim("tips.txt")

attach(tips)

## Summary Statistics
mean(Tip)
mean(TotBill)
median(TotBill)
quantile(Tip, c(0, .25, .5, .75, 1))

## Group Specific Statistics
mean(TotBill[Time == "Night"])
sd(TotBill[Time == "Night"])
mean(TotBill[Time == "Day"])
sd(TotBill[Time == "Day"])

## Histogram
## gives counts
hist(TotBill)
## gives percentage
hist(TotBill, freq = F)

require(lattice)
## gives percentage
histogram(TotBill)

## Histogram by Time Shift
histogram(~ TotBill | Time)

## Stacked Histogram
histogram(~ TotBill | Time, layout = c(1,2))
  # layout
    # 1st option = # columns
    # 2nd option = # rows

## Boxplots
boxplot(TotBill ~ Time, ylab = "Total Bill")
points(1, mean(TotBill[Time == "Day"]), pch = 5)
points(2, mean(TotBill[Time == "Night"]), pch = 5)

## Scatter Plots
plot(TotBill, Tip) 
  # sometimes we'd like to add titles and change the 
  # default x and y labels, which are the variable
  # names; we can also add subtitles or any other text
  # we would like using the mtext() function; it is
  # also easy to add legends, change colors, and change
  # the default plotting value
#plot(TotBill, Tip, pch = 16, col = rainbow(n = 50),
#     main = "Relationship between Tip and Total Bill",
#     xlab = "Total Bill", ylab = "Tip",
#     sub = "Sub-titles appear down here") 
#mtext("I like my Sub-titles up here \n so I use mtext", cex = 1.15, line = -1)
#legend("topleft", "My Legend", text.col = "magenta", bty = 'n')
xyplot(Tip ~ TotBill)

## Scatter Plot with Trend Line
xyplot(Tip ~ TotBill, type = c("p", "r"))
xyplot(TotBill ~ Tip, type = c("p", "r"))

## Scatter Paneled by Smoking Status
xyplot(Tip ~ TotBill | Smoker)

## correlation
cor(Tip, TotBill)

## linear regression
lm(Tip ~ TotBill)
summary(lm(Tip ~ TotBill))

detach(tips)

setwd("M:\\General Research Directory\\171-161\\Labs2014\\Lab 4")