Independent t test Example Manual vs t.test()
# T test manually
# Generating the data
x_raw <- rnorm(100, 170, 5)
y_raw <- rnorm(100, 166, 5)
# rnorm() doesn't give us a sample with the mean and sd exactly of what we specified so,
# Manually rescaling the data so that the exact mean and sd are what we want
# NOTE: YOU ONLY NEED TO DO THIS SORT OF THING FOR THIS EXAMPLE BECAUSE OF RNORM()
x <- (x_raw - mean(x_raw)) / sd(x_raw) * 5 + 170 # mean = 170, sd = 5
y <- (y_raw - mean(y_raw)) / sd(y_raw) * 5 + 166 # mean = 166, sd = 5
# Checking
#mean(x)
#mean(y)
#sd(x)
#sd(y)
# Changing variable names to be consistent with lecture slides
x_obs <- x
y_obs <- y
# Pooled standard dev
vp = (99*25 + 99*25) / 198
sp <- sqrt(vp)
# Pooled standard error - the denominator of t value formula
se <- sp * sqrt(1/100 + 1/100)
# T value calculation
t_manual <- (170 - 166) / se
# P value calculation
p_value_manual <- pt(t_manual, 198, lower.tail = FALSE)
# Using t test function
res <- t.test(x= x_obs,
y= y_obs,
alternative= "greater",
mu=0,
paired=FALSE,
var.equal=TRUE,
conf.level=0.95)
res
##
## Two Sample t-test
##
## data: x_obs and y_obs
## t = 5.6569, df = 198, p-value = 2.67e-08
## alternative hypothesis: true difference in means is greater than 0
## 95 percent confidence interval:
## 2.831445 Inf
## sample estimates:
## mean of x mean of y
## 170 166
t_manual
## [1] 5.656854
p_value_manual
## [1] 2.670421e-08