library(MASS) set.seed(123123) train <- c(rep(0, 40), rep(1, 40)) knowledge <- round(30 + 20*train + rnorm(80, 0, 10)) motivation <- round(30 + 20*train + rnorm(80, 0, 10)) performance <- round(30 + 0.5*(motivation - mean(motivation)) + 0.5*(knowledge - mean(knowledge)) + rnorm(80, 0, 10)) performance <- performance - min(performance) dat <- data.frame(train, knowledge, motivation, performance) summary(lm(performance ~ train)) summary(lm(knowledge ~ train)) summary(lm(performance ~ knowledge + train)) # c = 24.625 # a = 20.950 # b = 0.4166 # c' = 15.8983 write.table(dat, file="example.csv", sep=",", row.names=FALSE, col.names=TRUE) set.seed(123123) conflict <- round(rnorm(262, 15, 2)) selfstress <- round(0.799 + 0.173*conflict + rnorm(262, 0, 0.94)) pcouplestress <- round(rnorm(262, 3, 1)) lifesat <- round(-1.447 + (0.077 * conflict) + (-0.769 * selfstress) + rnorm(262, 0, 1.34)) + 9 dat2 <- data.frame(conflict, selfstress, pcouplestress, lifesat) summary(lm(lifesat ~ conflict)) summary(lm(selfstress ~ conflict)) summary(lm(lifesat ~ selfstress + conflict)) # c = 24.625 # a = 20.950 # b = 0.4166 # c' = 15.8983 write.table(dat2, file="conflict.csv", sep=",", row.names=FALSE, col.names=TRUE) library(boot) ab.fun <- function(d, i) { newdat <- d[i,] a <- coef(lm(performance ~ train, data = newdat))[2] b <- coef(lm(performance ~ knowledge + train, data = newdat))[2] a * b } ab.boot <- boot(dat, ab.fun, R = 5000) boot.ci(ab.boot, type = c("norm", "basic", "perc", "stud"))