I'm performing a mediation analysis in R, block-bootstrapping by participant instead of by row since I have multiple observations for each participant. My code looks like this:
# Create function to block bootstrap by participant
indirectsaved = function(formula2, formula3, dataset, ids) {
Get unique ids
unique_ids <- unique(ids)
Shuffle ids with replacement
shuffled_ids <- sample(unique_ids, replace=TRUE)
Create a new dataframe by appending rows for each shuffled id
d <- do.call(rbind, lapply(shuffled_ids, function(id) {
dataset[dataset$id == id, ]
}))
model2 = lm(formula2, data = d)
model3 = lm(formula3, data = d)
a = coef(model2)[2]
b = coef(model3)[3]
c = coef(model3)[2] #direct effect
indirect = a*b
total = c + indirect
return indirect, total, and direct effects
c(indirect, total, c)
}
Calculate bootstrapped results
bootresults = boot(data = df,
statistic = indirectsaved,
formula2 = mediator ~ type,
formula3 = DV ~ type + mediator,
R = 10000)
Results
bootresults
boot.ci(bootresults,
conf = .95,
type = "norm")
Calculate prop mediated
prop_mediated <- bootresults$t[,1] / bootresults$t[,2]
mean(prop_mediated)
Calculate p-value
p_value = sum(bootresults$t[,1] <= 0) / length(bootresults$t[,1])
p_value
As far as I know, this method of performing a mediation analysis and obtaining a p-value from it are both correct. However, I recently obtained a result where the indirect effect's CI was (1.05, -0.02) yet the p-value was still 0 (indicating <0.001% of indirect effects should be less than 0). What could possibly produce this result?