Load required packages: (tidyverse; lme4; lmSupport; cowplot; performance; see)

Load dataframe: timescale 350; offset 5-30

setwd("C:/Users/pkmon/Desktop/TLCC_baseline_check")

d = read.csv('timescale_350_offset_5-30_bin_1.csv')

d = d[which(d$Date!='2'),] # remove 6 hr data

File formatting and DF cleaning

d$treatment = varRecode(d$Treatment, c('No Oxytocin','Oxytocin'), c('saline','oxytocin'))
d$treatment_num = varRecode(d$treatment, c('saline','oxytocin'), c(0,1))
d$treatment_C = varRecode(d$treatment_num, c(0,1), c(-.5,.5))

d$hr = varRecode(d$Date, c(1,3,4,5), c(0,24,48,168))
d$hr_C = d$hr - mean(d$hr)

d$correlation = d$Correlation

d = select(d, -MousePair_ZhiminLabel_Original, -Treatment, -Date, -Bin, - Correlation) # remove old columns

Curiosity - general summary statistics

grand_mean = mean(d$correlation)
print(grand_mean) # 'Baseline' synchrony is about 0.14
## [1] 0.1417341
hourly_mean = aggregate(correlation ~ hr, data=d, FUN=mean)
print(hourly_mean)
##    hr correlation
## 1   0   0.1376716
## 2  24   0.1511825
## 3  48   0.1417210
## 4 168   0.1363612
treatment_mean = aggregate(correlation ~ treatment, data=d, FUN=mean)
print(treatment_mean)
##   treatment correlation
## 1  oxytocin   0.1348256
## 2    saline   0.1486425
hourly_mean_by_treatment = aggregate(correlation ~ hr * treatment, data=d, FUN=mean)
print(hourly_mean_by_treatment)
##    hr treatment correlation
## 1   0  oxytocin   0.1253660
## 2  24  oxytocin   0.1468504
## 3  48  oxytocin   0.1343879
## 4 168  oxytocin   0.1326982
## 5   0    saline   0.1499772
## 6  24    saline   0.1555146
## 7  48    saline   0.1490541
## 8 168    saline   0.1400242

Method 1: full repeated measures (though the random effects are a little confusing in this case, because it’s round-robin so not independent)

m1 = lmer(correlation ~ treatment_C * hr_C + (1|subject) + (1|shuffle) + (1|hr_C), data = d)
modelSummary(m1)
## lmer(formula = correlation ~ treatment_C * hr_C + (1 | subject) + 
##     (1 | shuffle) + (1 | hr_C), data = d)
## Observations: 576; Groups: subject, 18
## 
##  Observations: 576; Groups: shuffle, 18
## 
##  Observations: 576; Groups: hr_C, 4
## 
## Linear mixed model fit by REML
## 
## Fixed Effects:
##                    Estimate         SE       F error df   Pr(>F)    
## (Intercept)       1.417e-01  7.113e-03 396.994    18.14 8.81e-14 ***
## treatment_C      -1.382e-02  1.246e-02   1.230    29.36   0.2765    
## hr_C             -4.050e-05  5.666e-05   0.511     2.00   0.5489    
## treatment_C:hr_C  7.024e-05  3.932e-05   3.192   538.00   0.0746 .  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## NOTE: F, error df, and p-values from Kenward-Roger approximation
## 
## Random Effects:
##  Groups   Name        Std.Dev. 
##  subject  (Intercept) 0.0178245
##  shuffle  (Intercept) 0.0187567
##  hr_C     (Intercept) 0.0068678
##  Residual             0.0304878
## Warning in deviance.merMod(Model): deviance() is deprecated for REML fits; use
## REMLcrit for the REML criterion or deviance(.,REML=FALSE) for deviance
## calculated at the REML fit
## 
## AIC: -2229.6; BIC: -2194.7; logLik: 1122.8; Deviance: -2245.6
# A repeated measures framework for the random shuffle doesn't show a significant difference between groups, time points, or their interactions

Version 2: aggregated baseline per pair (average all the measurements per pair per timepoint)

d_aggregate = d %>%
  group_by(subject, hr) %>%
  summarise(
    average_correlation = mean(correlation, na.rm = TRUE),
    hr = first(hr),
    hr_C = first(hr_C),
    treatment = first(treatment),
    treatment_C = first(treatment_C),
    .groups = 'drop'
  )

m2 = lmer(average_correlation ~ treatment_C * hr_C + (1|subject) + (1|hr_C), data = d_aggregate)
modelSummary(m2)
## lmer(formula = average_correlation ~ treatment_C * hr_C + (1 | 
##     subject) + (1 | hr_C), data = d_aggregate)
## Observations: 72; Groups: subject, 18
## 
##  Observations: 72; Groups: hr_C, 4
## 
## Linear mixed model fit by REML
## 
## Fixed Effects:
##                    Estimate         SE       F error df   Pr(>F)    
## (Intercept)       1.417e-01  5.027e-03 794.849    6.044 1.21e-07 ***
## treatment_C      -1.382e-02  7.941e-03   3.027   16.000    0.101    
## hr_C             -4.050e-05  5.666e-05   0.511    2.000    0.549    
## treatment_C:hr_C  7.024e-05  6.110e-05   1.322   50.000    0.256    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## NOTE: F, error df, and p-values from Kenward-Roger approximation
## 
## Random Effects:
##  Groups   Name        Std.Dev. 
##  subject  (Intercept) 0.0146161
##  hr_C     (Intercept) 0.0061669
##  Residual             0.0167518
## Warning in deviance.merMod(Model): deviance() is deprecated for REML fits; use
## REMLcrit for the REML criterion or deviance(.,REML=FALSE) for deviance
## calculated at the REML fit
## 
## AIC: -293.3; BIC: -277.4; logLik: 153.7; Deviance: -307.3
# A repeated measures framework for the aggregated random shuffle doesn't show a significant difference between groups, time points, or their interactions

Model assumptions/performance for m2

model_performance(m2)
check_model(m2)

Plotting the aggregate (m2)

plot =
  ggplot(aes(x = hr, y = average_correlation, group = as.factor(treatment), fill = as.factor(treatment)), data = d_aggregate) +
  geom_point(aes(colour = as.factor(treatment)), data = d_aggregate)+
  stat_smooth(method = "lm", formula = y ~ x, geom = "smooth", aes(colour = as.factor(treatment)), linewidth = .5, linetype = 1, fullrange = TRUE)+
  scale_y_continuous(expand = c(0,0), limits = c(0.05,.25), breaks = seq(from = 0.05, to = .25, by = .05))+
  scale_x_continuous(expand = c(0,0), limits = c(-6,174), breaks = seq(from = 0, to = 168, by = 24))+
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  ylab("Correlation (Pearson's r)")+
  xlab("Hours following pairing")+
  theme_cowplot()+
  theme(legend.title = element_blank())+
  ggtitle("TLCC BASELINE RANDOM | TS=350s; OS=5-30s")
print(plot)

plot_MeanLine = plot +
  geom_hline(yintercept = grand_mean, linetype = 2, linewidth = .75)
print(plot_MeanLine)

plot_Time_no_treatment =
  ggplot(aes(x = hr, y = average_correlation), data = d_aggregate) +
  geom_point()+
  stat_smooth(method = "lm", formula = y ~ x, geom = "smooth", linewidth = 1, linetype = 1, fullrange = TRUE)+
  scale_y_continuous(expand = c(0,0), limits = c(0.05,.25), breaks = seq(from = 0.05, to = .25, by = .05))+
  scale_x_continuous(expand = c(0,0), limits = c(-6,174), breaks = seq(from = 0, to = 168, by = 24))+
  geom_hline(yintercept = grand_mean, linetype = 2, linewidth = .75)+
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  ylab("Correlation (Pearson's r)")+
  xlab("Hours following pairing")+
  theme_cowplot()+
  theme(legend.title = element_blank())+
  ggtitle("TLCC BASELINE RANDOM | TS=350s; OS=5-30s")
print(plot_Time_no_treatment)

plot_Time_no_treatment_v2 =
  ggplot(aes(x = hr, y = average_correlation), data = d_aggregate) +
  geom_point(aes(colour = as.factor(treatment)), data = d_aggregate)+
  stat_smooth(method = "lm", formula = y ~ x, geom = "smooth", linewidth = .5, linetype = 2, colour = "black", fullrange = TRUE)+
  geom_line(aes(x = hr, y = average_correlation, colour = as.factor(treatment), group = subject), data = d_aggregate,linetype = 1, linewidth=.3, alpha = .2) +
  scale_y_continuous(expand = c(0,0), limits = c(0.05,.25), breaks = seq(from = 0.05, to = .25, by = .05))+
  scale_x_continuous(expand = c(0,0), limits = c(-6,174), breaks = seq(from = 0, to = 168, by = 24))+
  geom_hline(yintercept = grand_mean, linetype = 5, linewidth = .5, colour = "red")+
  scale_colour_brewer(palette = "Dark2")+
  scale_fill_brewer(palette = "Dark2") +
  ylab("Correlation (Pearson's r)")+
  xlab("Hours following pairing")+
  theme_cowplot()+
  theme(legend.title = element_blank())+
  ggtitle("TLCC BASELINE RANDOM | TS=350s; OS=5-30s")
print(plot_Time_no_treatment_v2)