################################## #The t distribution: x <- (-100):100/10 plot(x, dt(x, 1), type="l") lines(x, dt(x, 3), col="red") lines(x, dt(x, 10), col="yellow") lines(x, dt(x, 100), col="cyan") lines(x, dt(x, 1000), col="blue") lines(x, dnorm(x)) ################################### #Example showing the connection of the t distr. #with the normal and chi-square: sample5 <- rnorm(10000)/sqrt(rchisq(10000, 5)/5) x <- (-20):20 hist(sample5, breaks=x-0.5, prob=TRUE) points(x, dt(x, 5)) ################################### #Examples of F-distributions: x <- 1:100/20 plot(x, df(x, 1, 1), type="l") lines(x, df(x, 1, 3)) lines(x, df(x, 1, 100)) lines(x, df(x, 3, 1)) lines(x, df(x, 3, 10)) lines(x, df(x, 3, 100)) lines(x, df(x, 10, 1)) lines(x, df(x, 10, 10)) lines(x, df(x, 10, 100)) ################################### #Examples of differences between t tests: data1 <- c(24, 34, 25, 43, 12, 54, 32, 35) data2 <- c(42, 12, 29, 13, 31, 27, 19) t.test(data1, data2) help(t.test) t.test(data1, data2, var.equal=T) t.test(data1, data2, var.equal=T, conf.level=0.99) t.test(data1, data2, var.equal=T, alternative="greater") ################################## #A randomization test replacing a t-test: t.test(data1, data2) A <- matrix(c(data1, data2), 10000, 15, byrow=T) B <- apply(A, 1, function(x) {sum(sample(x, 8))}) C <- sum(data1)+sum(data2) - B D <- B/8 - C/7 hist(D) diff <- mean(data2)-mean(data1) abline(v=diff) sum(diff>D)/length(D) ################################## #Another randomization test, for paired data: dat1 <- rpois(15, 4) dat2 <- dat1 + rt(15, 3) +1 t.test(dat1, dat2, paired=T) diffs <- dat1 - dat2 A <- matrix(diffs, 10000, 15, byrow=T) A <- A*matrix((runif(10000*15)<0.5)*2-1, 10000, 15) B <- apply(A, 1, mean) hist(B) mean(diff)