# 1. 2 x 2 randomized design, single covariate.
set.seed(113)
design_2x2 <- expand.grid(A = factor(1:2), B = factor(1:2))
design_2x2$mu_y <- c(50, 60, 55, 65) # cell means in expand.grid order
d1 <- simulate_ancova_factorial_data(
a = 2, b = 2,
mu_y = design_2x2$mu_y,
mu_x = matrix(10, nrow = 4, ncol = 1), # constant covariate mean
sigma_y = 8,
sigma_x = 3,
rho_y_x = 0.40,
n = 30
)
aggregate(y ~ A + B, data = d1, FUN = mean)
# 2. 3 x 2 nonrandomized design with two covariates and unequal n.
set.seed(113)
a <- 3; b <- 2; q <- 2
n_cells <- a * b
d2 <- simulate_ancova_factorial_data(
a = a, b = b,
n_covariates = q,
mu_y = c(50, 55, 60, 52, 58, 64),
mu_x = matrix(c(10, 11, 12, 9, 10, 11,
5, 6, 7, 4, 5, 6),
nrow = n_cells, ncol = q),
sigma_y = 8,
sigma_x = c(3, 2),
rho_y_x = c(0.40, 0.25),
rho_x_x = matrix(c(1, 0.3,
0.3, 1),
nrow = 2),
n = c(30, 25, 20, 35, 30, 25),
randomized = FALSE
)
head(d2)
# 3. 2 x 2 x 2 randomized design with one covariate.
set.seed(113)
d3 <- simulate_ancova_factorial_data(
a = 2, b = 2, c = 2,
mu_y = c(50, 55, 52, 57, 53, 58, 55, 60), # 8 cells in A-fastest order
mu_x = matrix(10, nrow = 8, ncol = 1),
sigma_y = 8,
sigma_x = 3,
rho_y_x = 0.40,
n = 25
)
table(d3$A, d3$B, d3$C) # 25 per cell, 200 total
Run the code above in your browser using DataLab