Correlation of two continuous variables but with clustered data I want to correlate two changes in time, a change in microbiota with a change in some marker. There are several time points, so I have several differences in each participant. My idea is to see if there's a correlation between those changes, and if so, I would use a simple correlation per difference (Pair, see below) as the post hoc test. The data structure is following:
 
Subject   Pair   DeltaMicrobiome   DeltaMarker 
1          T1T2   value                 value 
1          T1T3   value                 value 
1          T1T4   value                 value 
.. 
2          T1T2   value                 value 
2          T1T3   value                 value 
2          T1T4   value                 value 
I was thinking of using the mixed effect model.
m0 <- lmer (DeltaMicrobiome ~ (1|Pair) + (1|Subject), data)
m1 <- lmer (DeltaMicrobiome ~ (1|Pair) + (1|Subject) + DeltaMarker, data)
lrtest(m0, m1)

And if significant, then I would run the correlation for each time period (Pair) as the post hoc test
cor.test(~ DeltaMicrobiome + DeltaCalprotectin, data = subset(data, Pair == "T1T2")
cor.test(~ DeltaMicrobiome + DeltaCalprotectin, data = subset(data, Pair == "T1T3")
.. and so on

My primary interest is the time period in which the microbiome change correlates with marker change. Does it make any sense?
Thanks in advance,
Mariusz
 A: A couple of points:

*

*It seems like you have longitudinal data, i.e., microbiome measured over time, and the marker measured over time. In this case, it is better to work with the original outcome data and calculate differences/deltas. This is especially in case you have any missing data. Also, you would more appropriately account for the correlations in your data, and multiple testing.


*Given the point above it would make more sense to include a random slopes term. I.e., you want to say that microbiome measurements within the subjects are correlated, but measurements taken further apart in time are less correlated than measurements taken closer to each other. The corresponding syntax would be along these lines:
lmer(Microbiome ~ Marker * timeF + (time_meas | Subject), data = orig_data)

where time_meas is the numeric variable denoting the time points the measurements were taken, and time_F is the factor version of time denoting the time points, T1, T2, T3, $\ldots$

*

*If the interaction turns out practically and statistically significant, you could proceed to post-hoc testing to see at which time points there is a difference in the size association between the marker and the microbiome. You could do this using the emmeans package.

A: Dimitris,
thanks for your valuable comments. I followed your suggestions and have a couple of further questions.
First of all, what worries me a bit is that for almost every model a warning pops up with a singular fit. I'm not sure if this is a matter of random slope term as without it the same warning appears from time to time. 
Is it OK to use a lrtest to test for the Marker x TimeF interaction?
m0 <- lmer(Microbiome ~ Marker + timeF + (time_meas | Subject), data = orig_data)
m1 <- lmer(Microbiome ~ Marker * timeF + (time_meas | Subject), data = orig_data)
lrtest(m0, m1)

When it come to the emmeans package and post-hoc test I used (in case of the significant interaction term)
emtrends(m1, pairwise ~ timeF, var = "Marker")
emmip(m1, timeF ~ Marker, cov.reduce = range)

As I understand it allows me to find out between which two time points there is a difference in the association between the marker and the microbiome (a significant difference in the slope of the lines representing an association between the microbiome and the marker?). However, suppose there's a difference in the slopes between T1 and T2, does it really address my primary question which is the association between the microbiome change and the marker change (statistically speaking) between those two time points (T1 and T2)?
