T test

T-test is used to check whether two groups have the same mean (measurement), it should satisify the following conditions, 1. sample size <30 2. samples should are indepedently normally distributed 3. unknown mean and 2 groups have unknown variances 4. limiting comparing with 2 groups

x<-c(91,87,99,77,88,91) 
y<- c(101,110,103,93,99,102)
t.test(x, y, alternative="less", var.equal=TRUE)
## 
##  Two Sample t-test
## 
## data:  x and y
## t = -3.3806, df = 10, p-value = 0.003498
## alternative hypothesis: true difference in means is less than 0
## 95 percent confidence interval:
##      -Inf -5.79825
## sample estimates:
## mean of x mean of y 
##  88.83333 101.33333

p<0.005, we reject null hypthoese, decided the mean of x and y are not equal

F test

F test compare the variances of two normally distributed groups, determine whether the variances are equal. there is no limitation on sample size.

x<- rnorm(100, mean=0)
y<- rnorm(100, mean=1)
var.test(x,y)
## 
##  F test to compare two variances
## 
## data:  x and y
## F = 1.1441, num df = 99, denom df = 99, p-value = 0.5043
## alternative hypothesis: true ratio of variances is not equal to 1
## 95 percent confidence interval:
##  0.7697904 1.7003828
## sample estimates:
## ratio of variances 
##           1.144088

p=0.5254, we accept the variances of x and y are equal

Chi-square test

Chi square test can test, it can use for continuous variables 1. a goodness of fit, comparing frequencies of one attribute variable to theoretical expectations. 2. a test of indepence, comparing if a variable is associate with another variable.(the larger the sample size, the better the fit)

data(mtcars)
chisq.test(mtcars$carb,mtcars$cyl)
## Warning in chisq.test(mtcars$carb, mtcars$cyl): Chi-squared approximation
## may be incorrect
## 
##  Pearson's Chi-squared test
## 
## data:  mtcars$carb and mtcars$cyl
## X-squared = 24.389, df = 10, p-value = 0.006632

since p<0.05, we conclude carb and cyl are dependent. however, due to the small sample size, this conclusion might be wrong.

p value interpretation

  1. t-test, if p<0.05, we reject the null hypthoese(m1=m2), which means mean(x1) not equal to mean(x2)

  2. f-test, if p<0.05, we reject the null hypthoese(var1=var2), which means variance(x1) not equal to variance(x2)

  3. chi-square test, if p<0.05, we reject the null hpythoese(x1 idependent from x2), which means two variables are dependent