Example R programs and commands Student-t test from reduced data # All lines preceded by the "#" character are my comments. # All other left-justified lines are my input. # All other indented lines are the R program output. ### ONE-SAMPLE: # # Given: # Xbar = sample mean, approximates mu # n = number of samples # SE = sample standard error = (sample s.d.)/sqrt(n) # mu0 = hypothesized mean # # Test: H0:mu= mu0 [one-tailed, one-sample test] t <- (Xbar - mu0)/SE; # statistic -- no abs() -- positive if HA is true nu <- n-1; # degrees of freedom p <- pt(t,df=nu,lower.tail=FALSE); # p-value # Test: H0:mu>=mu0 versus HA: mu < mu0 [one-tailed, one-sample test] t <- (Xbar - mu0)/SE; # statistic -- no abs() -- negative if HA is true nu <- n-1; # degrees of freedom p <- pt(t,df=nu,lower.tail=TRUE); # p-value # Test: H0:mu=mu0 versus HA: mu != mu0 [two-tailed, one-sample test] t <- abs(Xbar - mu0)/SE; # statistic, always nonegative nu <- n-1; # degrees of freedom p <- pt(t,df=nu,lower.tail=FALSE) + pt(-t,df=nu,lower.tail=TRUE); # p-value # NOTE: it is equivalent to use p<-2*pt(t,df=nu,lower.tail=FALSE) ### using the data faker: source("faker.R") x<- faker(n, Xbar, SE*sqrt(n)); t.test(x, mu=mu0, alternative='greater') t.test(x, mu=mu0, alternative='less') t.test(x, mu=mu0, alternative='two.tailed') ### TWO-SAMPLE: # # Given: # X1 = sample 1 mean, approximates mu1 # n1 = number of samples in X1 # SE1 = sample 1 standard error = (sample 1 s.d.)/sqrt(n1) # X2 = sample 2 mean, approximates mu2 # n2 = number of samples in X1 # SE2 = sample 2 standard error = (sample 2 s.d.)/sqrt(n2) # delta = hypothesized difference mu1-mu2 between the means # # Whether the sample sizes are equal (n1=n2) or unequal (n1 != n2), the # following formulas will hold. It is useful to precompute: nu1 <- n1-1; nu2 <- n2-1; # degrees of freedom s1 <- SE1*sqrt(n1); s2 <- SE2*sqrt(n2); # sample s.d. estimates # ASSUMPTIONS: # # Equal variances assumption (hence equal s.d.) sigma1=sigma2 # sp <- sqrt( (nu1*s1^2 + nu2*s2^2)/(nu1+nu2) ); # pooled s.d. estimate SE <- sp*sqrt(1/n1 + 1/n2); # standard error in X1-X2 t <- (X1-X2 - mu0)/SE; # statistic usable for one-or two-tailed tests nu <- nu1 + nu2 # degrees of freedom to use in pt() # # OR # # Possibly unequal variances (hence unequal s.d.) sigma1 != sigma2 # SE <- sqrt(s1^2/n1 + s2^2/n2); # Welch's standard error in X1-X2 t <- (X1-X2 - mu0)/SE; # statistic usable for one-or two-tailed tests nu <- SE^4/((s1^2/n1)^2/nu1 +(s2^2/n2)^2/nu2) # Welch's df to use in pt() # Test: H0:mu1-m2 =< mu0 versus HA: mu1-mu2 > mu0 [one-tailed, two-sample] p <- pt(t,df=nu,lower.tail=FALSE); # p-value # Test: H0:mu1-m2 >= mu0 versus HA: mu1-mu2 < mu0 [one-tailed, two-sample] p <- pt(t,df=nu,lower.tail=TRUE); # p-value # Test: H0:mu1-m2 = mu0 versus HA: mu1-mu2 != mu0 [two-tailed, two-sample] p <- pt(abs(t),df=nu,lower.tail=FALSE) + pt(-abs(t),df=nu,lower.tail=TRUE) # p-value # NOTE: it is equivalent to use p<-2*pt(-abs(t),df=nu) ### using the data faker: source("faker.R") x1<- faker(n1, X1, SE1*sqrt(n1)); x2<- faker(n2, X2, SE2*sqrt(n2)); # Equal variances assumption (hence equal s.d.) sigma1=sigma2 t.test(x1,x2, mu=mu0, alternative='greater', var.equal=TRUE) t.test(x1,x2, mu=mu0, alternative='less', var.equal=TRUE) t.test(x1,x2, mu=mu0, alternative='two.tailed', var.equal=TRUE) # Possibly unequal variances (hence unequal s.d.) sigma1 != sigma2 [R default] t.test(x1,x2, mu=mu0, alternative='greater', var.equal=FALSE) t.test(x1,x2, mu=mu0, alternative='less', var.equal=FALSE) t.test(x1,x2, mu=mu0, alternative='two.tailed', var.equal=FALSE) ### PAIRED # # Given: # XY = mean of x-y over all pairs (x,y) # n = number of sample pairs (x,y) # SE = standard error in XY # mu0 = hypothesized mean of XY # # Then the formulas for ONE-SAMPLE TESTS apply with Xbar <-- XY ### using the data faker: source("faker.R") x<- faker(n, XY, SE*sqrt(n)); t.test(x, mu=mu0, alternative='greater') t.test(x, mu=mu0, alternative='less') t.test(x, mu=mu0, alternative='two.tailed')