Thursday, April 7, 2016

Lab III

We will do two experiments today. The first one is to explore the distribution of one-sample T-statistics. The second one is to compare the power of two-sample independent T-test and Paired T-test.


1. A Numerical experiment to find out the distribution of one-sample T-statistics


First, let us write a function to compute one sample T-statistics

t.stat <- function(x){ # x is the input
  n <- length(x)
  tstat <-  mean(x)/sqrt(var(x)/n)
  return(tstat) # t is the result you get after applying this function
}

Now, you have created a function that computes one sample t-statistic.


Next we will simulate 10000 set of random sample of size 10, and compute t-statistics

tt <- rep(0,10000)
for(i in 1:10000){
  x <- rnorm(10)
  tt[i] <- t.stat(x)
}

hist(tt) # histogram is not the best tool to tell the difference between t-distribution and normal distribution

qqnorm(tt) # a better tool is quantile-quantile plot
abline(0,1) # if two distribution are similar, all points should roughly lineup on the line x=y

qqplot(qt((1:10000)/10001,df=9),tt) # compare quantile of t-distribution with df=9 with the t-statistics
abline(0,1)


2. A numerical experiment to compare the power of paired t-test and two sample independent T-test. 

t.paired <- rep(0,10000)
t.ind <- rep(0,10000)


for(i in 1:10000){
   
   
   y1 <- rnorm(10) #  simulate the first one observation of a pair
   d <- rnorm(10,1,1) # the difference between two observations in a pair follows a normal distribution with mean 1. 
   y2 <- y1+d # simulate the second observation in the pair

   t.paired[i] <- t.stat(d) # paired t-test
   t.ind[i] <- t.test(y1,y2,var.equal=T)$stat # treat it as two independent sample
}

sum(abs(t.paired) > qt(1-0.025,9)) # the number of times the observed t-statistics exceed the threshold of P-value less that 0.05

sum(abs(t.ind) > qt(1-0.025,18)) # check the power of two-sample independent test



If the data follows a paired design, which of the two statistical tests are better, two-sample independent t-test, or paired t-test?


Now, try to do the following experiment on your own, if two samples are independent, and we applied paired t-test to it, what is the consequence?

t.paired <- rep(0,10000)
t.ind <- rep(0,10000)


for(i in 1:10000){
   
   y1 <- rnorm(10) # the first sample of 10 from standard normal
   y2 <- rnorm(10,1,1) # the second sample of 10 at mean 0 and std 1

   d <-       # fill the blank here

   t.paired[i] <- t.stat(d) # paired t-test
   t.ind[i] <- t.test(y1,y2,var.equal=T)$stat # treat it as two independent sample
}

sum(abs(t.paired) > qt(1-0.025,9)) # the number of times the observed t-statistics exceed the threshold of P-value less that 0.05

sum(abs(t.ind) > qt(1-0.025,18)) # check the power of two-sample independent test


For the two-independent T-test above, we used the version with  the assumption that the variances of two groups are equal, and we simulated the data following exactly this assumption. Below we would like to experiment what might happen if we use the same method when the assumption is not true. We will also do the t-test without the equal variance assumption to see if it works well.

p.eq <- rep(0,10000)
p.neq <- rep(0,10000)
for(i in 1:10000){
   
## we generate the data under the null hypothesis that mu1 = mu2

   y1 <- rnorm(10) # the first sample of 10 from standard normal

   y2 <- rnorm(10,0,10) # the second sample of 10 at mean 0 and std 10



   p.eq[i] <- t.test(y1,y2,var.equal=T)$p.value # get p-value from the t-test that assumes the equal variance
   p.neq[i] <- t.test(y1,y2,var.equal=T)$p.value # P-value of the t-test that does not assumes the equal variance
} ## end of the loop


sum(p.eq < 0.05) # see how many times we get P-value less than 0.05 

If we get P-values less than 0.05 more often than the specified 0.05 significance level, then we have a problem of inflated Type-I error. We want to control Type-I error at 5% but using this method we will get more. 

sum(p.neq < 0.05) # see how many times we get P-value less than 0.05




No comments:

Post a Comment