How can I model multilevel diary data? I am currently trying to model diary data in a multilevel regression format and I do not really understand how i can include the diary data lagged structure into my r script when I am trying to predict a certain variable from a variable that occured on the previous day.
So far I've been using the lmer and glmer command, but just as "normally" modelled with   
model1<-lmer(pred.previousday~outcomepresentday+1(1|level2variable),data=data)

But i think this command does not really capture the diary aspect. Also, I don't know whether or how I could control for the occurence of the predictor variable not only on the previous but also on the present day.
How can I model multilevel diary data?
 A: You need to restructure your dataset accordingly, i.e. you need to create a variable that includes the previous-day measurements. For example, if you had measured the data on 5 days, you would "shift" the data by -1 day to create a data set which includes the measurements from day 1 as a predictor of day 2, the measurements from day 2 as predictors of day 3 and so on, leading to NA for day 1 (since there is no day 0 to predict the day 1 values). 
Furthermore, I think you might to restructure your model. In the current form, you are predicting the former day measurements by the current day measurements.
This could look something like:
mod1 <- nlme::lme(current_day_measurement ~ previous_day_measurement, random = ~1|level2, data = dat, na.action = na.omit)
A: Lagged versions of the dependent variable are a no-no in traditional random effects models. The problem is that they are correlated with the random intercept and produce inconsistent estimates of the regression coefficients. This is known as the initial conditions problem. Fortunately there are ways of getting around it, but different procedures are required for estimating such models, for example using structural equation models (see also here), or the Generalized Methods of Moments approach of Arellano-Bond. Depending on how many occasions there are in your diary study, the SEM approach might be burdensome. But if you have less than 10 or so, you're probably ok. Williams, Allison, & Moral-Benito show how to implement the SEM approach in Stata, Mplus, and perhaps lavaan in R. 
A: This is an important topic. The correlation among stable factors and the lagged predictors must be accounted for somehow, and the way this is done can have profound implications for coefficient estimates. This can be visualized perhaps more easily for the smaller T panel data case by considering a cross-lagged panel model (or panel VAR in econometrics), where you can then trace the path of earlier shocks on future occasions (as is done with impulse response analysis in VAR literature). Some of the implications and meaning of fixed effects and impulse responses in data like yours are discussed extensively in Zyphur et al. (2020) parts one and two ([here][1] and [here][2]). But Mplus is currently the only program I know of that properly handles this case in a multilevel data setup with its new DSEM approach.
