# Error in 1-1-1 indirect effect using mediation package and nlme

I regularly run multilevel models for repeated measures (over 10 or 15 work days) to analyse within-person links between job demands (say social stressors) at the day-level and well-being (say positive affect) the next day. I am also interested in indirect effects via a mediator variable, e.g. psychological detachment/switching off in the evening. I'd like to examine the indirect effect at Level-1. social stressors day n--> detachment evening day n --> affect morning day n+1

The mediation package for R by Tingley and colleagues used to be very helpful to estimate the indirect effect when using lme4. The package provides direct, indirect and total effects for the focal link and includes confidence intervals. However, upon analyzing indirect effects based on nlme (instead of lme4) model, I receive an error-message:

Error in if (n.m != n.y) { : argument is of length zero


I replicated the error using these sample data:

d <- read.csv("http://www.ats.ucla.edu/stat/data/ml_sim.csv")


I estimated the a- and b-paths of the indirect effect respectively using nlme

library(nlme)
summary(path.a1<- lme(m ~ 1 + x,
random = ~ 1 + x| id,
data = d,
#method = "ML",
#correlation=corAR1(),
na.action = na.omit,
control = list(opt = "optim")))

summary(path.b1<- lme(y ~ 1 + x + m,
random = ~ 1 + x + m| id,
data = d,
#method = "ML",
#correlation=corAR1(),
na.action = na.omit,
control = list(opt = "optim")))

library(mediation)
indirectmodel <- mediate(path.a1, path.b1, treat = "x", mediator = "m",na.action="na.omit")


In my focal data.frame I made sure there are no missing cases. However the error message indicates that there seems to be a problem with the sample sizes for estimation of the a-path and the b-path. I suspect this error message has a rather trivial cause which I am not able to identify, given that I have rather rudimentary R using skills.

Is anyone aware of potential causes and solutions of this problem?

• Did you figure this issue out ? – Mehdi Boukhechba Jun 23 '18 at 20:07