#Foundation and Application of Econometrics, Spring 2015, Pepared by Dr. I-Ming Chiu
#Accompany 368_Handout05.pdf
#Two sample t-test
rm(list=ls())
g1 = c(7, 13, 15, 10, 8, 9, 21, 9, 14, 8) #new drug
g2 = c(15, 14, 12, 8, 14, 7, 16, 10, 15, 12) #pacebo
boxplot(g1, g2)
#variances are different (this default)
t.test(g1, g2)
#variances are equal
t.tesy(g1, g2, var.equal = T)
#One-Way ANOVA
library("faraway")
data(coagulation)
names(coagulation)
attach(coagulation)
boxplot(coag~diet)
#alternative command
oneway.test(coag~diet, var.equal = T)
#Let's do it manually (pp. 3 in Handout)
meanA = mean(coagulation[diet=="A",1])
meanB = mean(coagulation[diet=="B",1])
meanC = mean(coagulation[diet=="C",1])
meanD = mean(coagulation[diet=="D",1])
meanG = mean(coagulation[,1])
SST = sum((coagulation[,1]-meanG)^2)
SSTrt = 4*(meanA-meanG)^2 + 6*(meanB-meanG)^2 + 6*(meanC-meanG)^2 + 8*(meanD-meanG)^2
SSE = SST - SSTrt
F = (SSTrt/3)/(SSE/20) #df = 3 (g-1) and 20 (N-g) respectively
F