####################################################### ### Nutrition example: Welch two-sample t-test in R ### ####################################################### # Read the two samples into R xA = c(7.53, 7.48, 8.08, 8.09, 10.15, 8.4, 10.88, 6.13, 7.9) xB = c(9.21, 11.51, 12.79, 11.85, 9.97, 8.79, 9.69, 9.68, 9.19) # Perform Welch two-sample t-test t.test(xB, xA) ################################################## ### Sleep medicine example: Paired t-test in R ### ################################################## # Read the paired samples into R x1 = c(.7,-1.6,-.2,-1.2,-1,3.4,3.7,.8,0,2) x2 = c(1.9,.8,1.1,.1,-.1,4.4,5.5,1.6,4.6,3.4) # Compute differences for the paired t-test dif = x2 - x1 # Perform paired t-test t.test(dif) # Another way to perform the paired t-test t.test(x2, x1, paired = TRUE) # Normal Q-Q plots separately for each sample qqnorm(xA, main = "Hospital A") qqline(xA) qqnorm(xB, main = "Hospital B") qqline(xB) # Multiple (simulated) Q-Q plots and sample A require(MESS) fitA <- lm(xA ~ 1) qqnorm.wally <- function(x, y, ...) { qqnorm(y, ...); qqline(y, ...)} wallyplot(fitA, FUN = qqnorm.wally, main = "") # Multiple (simulated) Q-Q plots and sample B fitB <- lm(xB ~ 1) qqnorm.wally <- function(x, y, ...) { qqnorm(y, ...); qqline(y, ...)} wallyplot(fitB, FUN = qqnorm.wally, main = "") ####################################################### ### Example: Power-related calculations for t-tests ### ####################################################### # Power calculation (one-sample) power.t.test(n = 40, delta = 4, sd = 12.21, type = "one.sample") # Sample size calculation (one-sample) power.t.test(power = .80, delta = 4, sd = 12.21, type = "one.sample") # Power calculation (two-sample) power.t.test(n = 10, delta = 2, sd = 1, sig.level = 0.05) # Sample size calculation (two-sample) power.t.test(power = 0.90, delta = 2, sd = 1, sig.level = 0.05) # Detectable effect size (two-sample) power.t.test(power = 0.90, n = 10, sd = 1, sig.level = 0.05)