# R scripts for means, one and two sample. # One mean, small sample t test. # Example is mercury concentrations in a lake (mg/m^3). mu0=1.2 # Baseline before mercury spill. alpha=.05 # Test significance level. x=c(1.60,1.77,1.61,1.08,1.07,1.79,1.34,1.07,1.45,1.59,1.43, 2.07,1.16,.85,2.11) # Measurements after spill. t.test(x,mu=mu0,conf.level=(1-alpha)) boxplot(x) abline(h=mu0,lty=2) # Paired data example, textbook prices at UCLA vs Amazon texts=read.table("textbooks.txt",header=TRUE) attach(texts) diff=uclaNew-amazNew diff mu0=0 alpha=.05 t.test(diff,mu=mu0,conf.level=(1-alpha)) # Alternative. # t.test(uclaNew,amazNew,paired=TRUE,mu=mu0,conf.level=(1-alpha)) boxplot(diff) abline(h=mu0,lty=2) detach(texts) # Two sample t test. Example is androsterone levels in # heterosexual (s) and homosexual (g) males, from # Margolese, MS 1970. Homosexuality: a new endocrine # correlate. Hormones and Behavior 1:151-155. By default # the variances are assumed unequal and the # Welch-Satterthwaite approximation is used. andro=read.table("androsterone.txt",header=TRUE) attach(andro) andro t.test(androsterone~orientation,mu=0,conf.level=.95) boxplot(androsterone~orientation,range=0, names=c("gay","straight"),ylab="androsterone",boxwex=.5) # Alternative. # andro.g=androsterone[,orientation=="g"] # andro.s=androsterone[,orientation=="s"] # t.test(andro.g,andro.s,mu=0,conf.level=.95) detach(andro)