kosukeimai / mediation

R package mediation
58 stars 29 forks source link

Resampling does not take place #44

Closed harrysouthworth closed 2 years ago

harrysouthworth commented 2 years ago

I reduced the code as far as I could and inserted a browser() statement into med.fun.

med.fun has 3 arguments: y.data, m.data and index.

boot requires the function to have an argument "data", but med.fun doesn't.

Therefore, y.data and m.data are not resampled.

Also, new.fit.M <- eval.parent(Call.M) is problematic, as is the equivalent for Y.

In particular, Call.M and Call.Y reference the original data in the stacked environments. So both models are refit to the original data, not to y.data and m.data.

One approach to fixing this is to add y.data <- y.data[index, ] m.data <- m.data[index, ] near the top of med.fun and to do

model.m <- update(model.m, data = m.data) model.y <- update(model.y, data = y.data)

instead of re-evaluating Call.M and Call.Y.

Finally, because we're dealing with a treatment and control, I think the observed group sizes should be conditioned on, so the function should default to passing strata = y.data[, treat] into the call to boot (though you can argue I'm wrong about that).

Harry

harrysouthworth commented 2 years ago

I was mistaken