Repeated measure correlation in Python I'm trying to see if there's correlation between two variables over days for different individuals.
I have 100 individuals' data over 60 days. For each day, I have the distance traveled on that day vs their stress trait for that day (as a self-report survey). I want to see if there's any correlation between stress and distance travelled.
I can calculate correlation for each individuals separately. But how do I calculate over all the individuals? I understand one way to do this is to just take mean of stress of an individual over 60 days, as well as their mean distance travelled over 60 days so that I have one row representing each individual. But I've read that this doesn't account properly for variability and a better way is to use mixture models. I'm clueless about implementing mixture model to get correlation between these two variables (over all the individuals). Has anybody got any reference or examples about how I could go about doing this on Python? Any help will be appreciated.
Thank you!
 A: What you're trying to do is a repeated measures correlation, as explained in this paper. You can find an implementation of the repeated measures correlation in my Pingouin package:
For example,
import pingouin as pg
pg.rm_corr(data=df, x='FirstVar', y='SecondVar', subject='Individuals')

This will give you the r-value, p-value, degrees of freedom, 95% confidence intervals and statistical power.
A: There are lots of possibilities here, but one approach is below.  This is a linear regression model fit using GEE.  It is regression, not correlation, but I think it fits the spirit of your question.
Most of the code below is for simulating a data set, which you wouldn't need to do.  You would need to get your data into the same long format as the DataFrame df has below.  After that, you would basically use the last four lines below. 
This example uses an autoregressive correlation, but there are other interesting choices.  Also, this is a linear model, but there are alternatives to that as well.
import statsmodels.api as sm                                                                                                                  
import pandas as pd                                                                                                                           
import numpy as np                                                                                                                            

n_person = 100                                                                                                                                
n_time = 60                                                                                                                                   

r = 0.5                                                                                                                                       

dist = np.random.normal(size=(n_person, n_time))                                                                                              
for i in range(1, n_time):                                                                                                                    
    dist[:, i] = r*dist[:, i-1] + np.sqrt(1-r**2)*dist[:, i]                                                                                  

err = np.random.normal(size=(n_person, n_time))                                                                                               
for i in range(1, n_time):                                                                                                                    
    err[:, i] = r*err[:, i-1] + np.sqrt(1-r**2)*err[:, i]                                                                                     

stress = dist + err                                                                                                                           

df = pd.DataFrame({"stress": stress.flat, "dist": dist.flat})                                                                                 
df["time"] = np.arange(df.shape[0]) % n_time                                                                                                  
df["person"] = np.floor(np.arange(df.shape[0]) / n_time).astype(np.int)                                                                       

model = sm.GEE.from_formula("stress ~ dist", cov_struct=sm.cov_struct.Autoregressive(), groups="person", data=df)                             
result = model.fit(maxiter=5)                                                                                                                 
print(result.summary())                                                                                                                       
print(result.cov_struct.summary())  
```

