falkcarl / multilevelmediation

5 stars 1 forks source link

Configure random effects structure in modmed.mlm #33

Open brizzen opened 8 months ago

brizzen commented 8 months ago

Hey! First of all, I really appreciate this package and also the publication which really helped me to understand multilevel mediation analysis.

I was wondering whether it is possible to customize the random effects structure to, for example, include only a random intercept per subject. From how I understand the code, the maximal random effects structure is always inputted to "random" in nlme::lme, right?

Might be related to #4. However, I was not able (yet) to customize the code in a way to (a) input only a random intercept as random effect to the combined model and (b) bootstrap SEs for the indirect effect from that combined model.

Thank you! Brais

falkcarl commented 8 months ago

Hi Brais,

Thank you. By default, no random effects aside from the random intercept are specified.

So, after loading some data. e.g., data(BPG06dat)

Something like this should give you what you want (provided I understand the request correctly):

fit<-modmed.mlm(BPG06dat,"id", "x", "y", "m")

If one wants to be explicit about it:

fit<-modmed.mlm(BPG06dat,"id", "x", "y", "m", random.a=FALSE, random.b=FALSE, random.cprime=FALSE)

brizzen commented 8 months ago

Hi, I just compared the random intercepts by calculating two separate linear-mixed models vs. the approach above with modmed.mlm and a stacked dataset and the random intercepts are indeed identical (besides differences at the the 3rd decimal place, could you know why?) - thanks for your help.

falkcarl commented 8 months ago

If you post some code and/or output we can take a quick look.

brizzen commented 8 months ago

Sure. I am not fully sure whether I am missing some subtraction/addition of fixed/random effects when comparing those between the individual models and the LMM on stacked data.

* I am somehow not getting similar ranef/fixef anymore. Maybe I am missing an obvious error while simulating or model-construction

set.seed(4)
# Number of subjects
n_subjects <- 30

# Number of conditions
n_conditions <- 4

# Levels of conditions
conditions <- factor(rep(1:n_conditions, times = n_subjects))

# Generate random data for predictor variable X with subject and condition effects
X <- rnorm(n_subjects * n_conditions, mean = rep(1:n_conditions, each = n_subjects), sd = 0.5)

# Generate random data for mediator variable M with subject and condition effects
M <- rnorm(n_subjects * n_conditions, mean = rep(1:n_conditions, each = n_subjects), sd = 0.5)

# Generate random data for outcome variable Y with subject and condition effects
Y <- rnorm(n_subjects * n_conditions, mean = rep(1:n_conditions, each = n_subjects), sd = 1)

# Create a data frame
DF <- data.frame(ID = rep(1:n_subjects, each = n_conditions),
                 X = X,
                 M = M,
                 Y = Y)

#define formula
fixed.form.m = "M ~ X"
fixed.form.y =  "Y ~ X + M"
random.form.m.y = "~ 1 | ID"

#calculate separate lmm
model.m = nlme::lme(fixed = as.formula(fixed.form.m),
                    random = as.formula(random.form.m.y), data = DF)
model.y = nlme::lme(fixed = as.formula(fixed.form.y),
                    random = as.formula(random.form.m.y), data = DF)

#calculate lmm on stacked data with modmed.mlm
fit<-modmed.mlm(DF,"ID", "X", "Y", "M")

#comparison may be possible with:
fixef(model.m)
fixef(fit$model) 
#and so on for random effects
brizzen commented 8 months ago

If I may ask an additional question for understanding the role of random effects in calculating indirect effects: Is it correct that for a model with only a random intercept (~ 0 + Sm + Sy | ID), no covariance is added to the indirect effect a*b?

falkcarl commented 8 months ago

I have not completely digested/figured out the simulated example issues. Something seems to be a bit strange about the simulated data in the sense that what the true model parameters are is not obvious based on the code, yet clearly some dependencies are introduced among the variables.

lme may not always be alerting the user to problems here; for example, if one uses lmer from lme4 to estimate the same two separate models, it's obvious that the random effect variance for the intercept in the model for y is zero and a warning about singular fit is generated.

We are working separately to offer support for glmmTMB, which I hope may do a better job than lme for the combined model; e.g., in playing around with this code (same sample and a much larger sample), glmmTMB for a combined model generates warnings about convergence problems. Something could also be worked into the docs or some function that will do some such sanity checks.

falkcarl commented 8 months ago

If I may ask an additional question for understanding the role of random effects in calculating indirect effects: Is it correct that for a model with only a random intercept (~ 0 + Sm + Sy | ID), no covariance is added to the indirect effect a*b?

Yes, that is correct.