Professional Documents
Culture Documents
R Companion - Student's T-Test For Two Samples
R Companion - Student's T-Test For Two Samples
Sta s cs
Salvatore S. Mangiafico
Example
Two-sample t-test, independent (unpaired) observations
### --------------------------------------------------------------
### Two-sample t-test, biological data analysis class, pp. 128–129
### --------------------------------------------------------------
Input =("
Group Value
2pm 69
2pm 70
2pm 66
2pm 63
2pm 68
2pm 70
2pm 69
2pm 67
2pm 62
2pm 63
2pm 76
2pm 59
2pm 62
2pm 62
2pm 75
2pm 62
2pm 72
2pm 63
5pm 68
5pm 62
5pm 67
5pm 68
5pm 69
5pm 67
5pm 61
5pm 59
5pm 62
5pm 61
5pm 69
5pm 66
5pm 62
5pm 62
5pm 61
5pm 70
")
Data = read.table(textConnection(Input),header=TRUE)
bartlett.test(Value ~ Group, data=Data)
### If p-value >= 0.05, use var.equal=TRUE below
Bartlett's K-squared = 1.2465, df = 1, p-value = 0.2642
t.test(Value ~ Group, data=Data,
var.equal=TRUE,
conf.level=0.95)
Two Sample t-test
t = 1.2888, df = 32, p-value = 0.2067
t.test(Value ~ Group, data=Data,
var.equal=FALSE,
conf.level=0.95)
Welch Two Sample t-test
t = 1.3109, df = 31.175, p-value = 0.1995
Plot of histograms
library(lattice)
histogram(~ Value | Group,
data=Data,
layout=c(1,2) # columns and rows of individual plots
)
Histograms for each popula on in a two-sample t-test. For the t-test to be valid, the
data in each popula on should be approximately normal. If the distribu ons are
different, minimally Welch’s t-test should be used. If the data are not normal or the
distribu ons are different, a non-parametric test like Mann-Whitney U-test or
permuta on test may be appropriate.
Box plots
boxplot(Value ~ Group,
data = Data,
names=c("2 pm","5 pm"),
ylab="Value")
Box plots of two popula ons from a two-sample t-test.
# # #
Similar tests
Welch’s t-test is discussed below. The paired t-test and signed-rank test are discussed in this book in their
own chapters. Analysis of variance (anova) is discussed in several subsequent chapters.
As non-parametric alternatives, the Mann–Whitney U-test and the permutation test for two independent
samples are discussed in the chapter Mann–Whitney and Two-sample Permutation Test.
Welch’s t-test
Welch’s t-test is shown above in the “Example” section (“Two sample unpaired t-test”). It is invoked with
the var.equal=FALSE option in the t.test function.
Power analysis
Power analysis for t-test
### --------------------------------------------------------------
### Power analysis, t-test, wide feet, p. 131
### --------------------------------------------------------------
M1 = 100.6 # Mean for sample 1
M2 = 103.6 # Mean for sample 2
S1 = 5.26 # Std dev for sample 1
S2 = 5.26 # Std dev for sample 2
Cohen.d = (M1 - M2)/sqrt(((S1^2) + (S2^2))/2)
library(pwr)
pwr.t.test(
n = NULL, # Observations in _each_ group
d = Cohen.d,
sig.level = 0.05, # Type I probability
power = 0.90, # 1 minus Type II probability
type = "two.sample", # Change for one- or two-sample
alternative = "two.sided")
Two-sample t test power calculation
n = 65.57875 # Number for each group
# # #
This site uses advertising from Media.net. For more information, visit our privacy policy page. Proceeds from these ads
go to support education and research activities, including the improvement of this site.
Citation:
Mangia ico, S.S. 2015. An R Companion for the Handbook of Biological Statistics, version 1.3.2.
rcompanion.org/rcompanion/. (Pdf version: rcompanion.org/documents/RCompanionBioStatistics.pdf.)