Skip to content

Instantly share code, notes, and snippets.

@jepusto
jepusto / Simulate Welch t-test
Last active December 29, 2015 14:49
A simulation evaluating the confidence interval coverage for a difference in means, allowing for unequal variances, based on Welch's approximation for the degrees of freedom. The code demonstrates the basic structure of a simulation study in R.
#----------------------------------------------
# data-generating model
#----------------------------------------------
two_group_data <- function(iterations, n, p, var_ratio, delta) {
Group <- c(rep("C", n * p), rep("T", n * (1 - p)))
Y_C <- matrix(rnorm(iterations * n * p, mean = 0, sd = 1), n * p, iterations)
Y_T <- matrix(rnorm(iterations * n * (1 - p), mean = delta, sd = sqrt(var_ratio)), n * (1 - p), iterations)
dat <- data.frame(Group, rbind(Y_C, Y_T))
return(dat)