-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdual-conversation-constraints.Rmd
2042 lines (1553 loc) · 79.1 KB
/
dual-conversation-constraints.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "High- and Low-Level Constraints on Coordination during Conversation: Code for Paxton & Dale (accepted, Fontiers in Psychology)"
output:
html_document:
number_sections: true
keep_md: true
---
This R markdown provides the basis for our manuscript, "Interpersonal movement synchrony responds to high- and low-level conversational constraints" (Paxton & Dale, *accepted*, *Frontiers in Psychology*). The study explores how high-level (i.e., conversational context) and low-level (i.e., visual stimuli) constraints affect interpersonal synchrony during conversation. We quantify coordination using amplitude of movement from head-mounted accelerometers (using Google Glass; see Paxton, Rodriguez, & Dale, 2015, *Behavior Research Methods*).
To run these analyses from scratch, you will need the following files:
* `./data/prepped_data-DCC.csv`: Contains experimental data. All data for included dyads are freely available in the OSF repository for the project: `https://osf.io/x9ay6/`.
* `./supplementary-code/required_packages-DCC.r`: Installs required libraries, if they are not already installed. **NOTE**: This should be run *before* running this script.
* `./supplementary-code/libraries_and_functions-DCC.r`: Loads in necessary libraries and creates new functions for our analyses.
* `./supplementary-code/continuous_rqa_parameters-DCC.r`: Identifies the appropriate parameters for continuous cross-recurrence quantification analysis (CRQA).
Additional files will be created during the initial run that will help reduce processing time. Several of these files--including the chosen CRQA parameters, the final plotting dataframe, and the final analysis dataframe---are available as CSVs from the OSF repository listed above.
**Written by**: A. Paxton (University of California, Berkeley) and R. Dale (University of California, Merced)
<br>**Date last modified**: 1 July 2017
***
# Data trimming
**NOTE**: The chunks of code in this section do not have to be run each time, since the resulting datasets will be saved to CSV files. As a result, these chunks are currently set to `eval=FALSE`. Bear this in mind if these data need to be re-calculated.
***
## Preliminaries
This section reads in participant data, saved in long format (i.e., one line per sample). The columns include:
* `dyad`: identifier for each dyad
* `partic`: identifier for each participant within each dyad
* `conv.type`: high-level constraint (within subjects)
+ `0` = affiliative conversation
+ `1` = argumentative conversation
* `cond`: low-level constraint (between subjects)
+ `0` = noise
+ `1` = dual-task
* `conv.num`: conversation number (2 total for each dyad)
* `t`: time of sample in seconds
* `x`, `y`, `z`: accelerometer readings (from head-mounted sensor in Google Glass) at each sample in 3-dimensional plane, relative to head location at initialization
```{r trimming-prelims, warning = FALSE, error = FALSE, message = FALSE, eval = FALSE}
# clear our workspace
rm(list=ls())
# read in libraries and create functions
source('./supplementary-code/libraries_and_functions-DCC.r')
# read in unified dataframe
coords = read.table('./data/prepped_data-DCC.csv',sep=',',header = TRUE)
```
***
## Pre-process movement data
Because participants' movement series were sampled separately, we here time-align time series for each dyad.
These data were collected using an accelerometer. Because this measures the change in velocity on each plane, we might call this Euclidean *acceleration* (rather than Euclidean distance).
```{r align-time-series, eval = FALSE}
# specify Butterworth filters
anti_aliasing_butter = butter(4,.4)
post_downsample_butter = butter(2,.02)
# get Euclidean acceleration, filter, downsample, and take mean of each axis at new time scale
coords = coords %>% ungroup() %>%
group_by(dyad,partic,conv.num,conv.type,cond) %>%
mutate(euclid_accel = c(NA,euclidean(x,y,z))) %>% # get Euclidean acceleration
mutate(euclid_accel = signal::filtfilt(anti_aliasing_butter, euclid_accel)) %>% # filter
select(-x,-y,-z) %>% # drox unneeded variables
mutate(t = floor(t * sampling_rate) / sampling_rate) %>% # downsample
ungroup() %>%
group_by(dyad, partic, conv.num, conv.type, cond, t) %>%
summarize_each(funs(mean(.))) %>% # take means
mutate(euclid_accel = signal::filtfilt(post_downsample_butter, euclid_accel)) # filter
# isolate participants' time series within each dyad
p0 = coords %>% ungroup() %>%
dplyr::filter(partic == 0) %>%
select(-partic) %>%
dplyr::rename(euclid0 = euclid_accel)
p1 = coords %>% ungroup() %>%
dplyr::filter(partic == 1) %>%
select(-partic) %>%
dplyr::rename(euclid1 = euclid_accel)
# join together the participants' time series, leaving only any overlapping slices
coords = plyr::join(p0, p1,
by=c("dyad", "conv.num", "conv.type", "cond", "t"),
type="inner")
```
***
## Trim instructions from time series
After instructions were given, both participants were instructed to shake their heads repeatedly to indicate the start of the experiment. Here, we identify that head shake by looking for the first bouts of high-velocity movement and trim everything before it.
***
### Calculate Euclidean acceleration first and derivatives
Because participants were instructed to shake their heads to signal the start of the trial, we can identify that by looking at the derivatives of acceleration: jerk (first derivative of acceleration) and jounce (second derivative of acceleration). We calculate each here.
```{r trim-instructions, eval = FALSE}
# get acceleration derivatives
coords.deriv = coords %>% ungroup() %>%
group_by(dyad, conv.num, conv.type, cond) %>%
mutate(jerk0 = c(0,diff(euclid0) / diff(t))) %>% # jerk for p0
mutate(jerk1 = c(0,diff(euclid1) / diff(t))) %>% # jerk for p1
mutate(jounce0 = c(0,diff(jerk0) / diff(t))) %>% # jounce for p0
mutate(jounce1 = c(0,diff(jerk1) / diff(t))) # jounce for p1
```
***
### Identify cutoff points for movement
Allowing time for participant questions and any setup issues, the total instruction time preceding each conversation should have been approximately 60 to 120 seconds. We therefore check between 60 and 120 seconds of the data to identify likely beginning times using peak jerk and jounce.
```{r identify-cutoffs, eval = FALSE}
# identify our minimum and maximum possible instruction end times
min.check = 60
max.check = 120
# identify possible cutoff times using jerk
cutoff.points.jerk = coords.deriv %>% ungroup() %>%
group_by(dyad,conv.num, conv.type, cond) %>%
dplyr::filter((t > min.check) & (t < max.check)) %>%
select(dyad, conv.num, conv.type, cond, t, jerk0, jerk1) %>%
dplyr::summarize(cutoff = max(c(jerk0,jerk1))) %>%
merge(.,coords.deriv) %>%
dplyr::filter((jerk0 == cutoff) | (jerk1 == cutoff)) %>%
select(-ends_with("0"),-ends_with("1"))
# identify possible cutoff times using jounce
cutoff.points.jounce = coords.deriv %>% ungroup() %>%
group_by(dyad,conv.num, conv.type, cond) %>%
dplyr::filter((t > min.check) & (t < max.check)) %>%
select(dyad, conv.num, conv.type, cond, t, jounce0, jounce1) %>%
dplyr::summarize(cutoff = max(c(jounce0,jounce1))) %>%
merge(.,coords.deriv) %>%
dplyr::filter((jounce0 == cutoff) | (jounce1 == cutoff)) %>%
select(-ends_with("0"),-ends_with("1"))
```
```{r invisible-store-jounce-and-jerk-values-for-table, echo=FALSE, eval = FALSE}
# invisible chunk: save file so that we can get the output without having to run everything
write.table(cutoff.points.jounce,'./data/DCC-cutoff_jounce.csv', sep=',',
row.names=FALSE,col.names=TRUE)
write.table(cutoff.points.jerk,'./data/DCC-cutoff_jerk.csv', sep=',',
row.names=FALSE,col.names=TRUE)
```
```{r invisible-load-jounce-and-jerk-values-for-table, eval=TRUE, echo=FALSE, warning = FALSE, error = FALSE, message = FALSE}
# invisible chunk: load file to correctly display correlation if everything else is `eval=FALSE`
source('./supplementary-code/libraries_and_functions-DCC.r')
cutoff.points.jounce = read.table('./data/DCC-cutoff_jounce.csv', sep=',', header = TRUE)
cutoff.points.jerk = read.table('./data/DCC-cutoff_jerk.csv', sep=',', header = TRUE)
```
```{r check-correlation-between-measures, eval = TRUE}
# are they correlated?
pander::pander(cor.test(cutoff.points.jounce$t,cutoff.points.jerk$t),
style = "rmarkdown")
```
```{r plot-jounce-and-jerk, echo=FALSE, fig.width=3, fig.height=3, fig.align='center', eval = FALSE}
# create the plot
jounce.v.jerk = qplot(x = cutoff.points.jounce$t,
y = cutoff.points.jerk$t) +
xlab('Jounce') + ylab('Jerk') +
ggtitle('Relation of Jerk and Jounce') +
geom_abline()
# save a high-resolution version of the plot
ggsave(plot = jounce.v.jerk,
height = 3,
width = 3,
filename = './figures/DCC-jounce_v_jerk.jpg')
# save a smaller version of the plot for knitr
ggsave(plot = jounce.v.jerk,
height = 3,
width = 3,
dpi=100,
filename = './figures/DCC-jounce_v_jerk-knitr.jpg')
```
![**Figure**. Correlation of jounce and jerk for each conversation of each dyad, including best-fit line.](./figures/DCC-jounce_v_jerk-knitr.jpg)
Jerk and jounce are significantly correlated, so we'll use the more conservative measure (i.e., tending to identify later points). We then remove everything before that cutoff point from the analyzed dataset.
```{r trim-data, eval = FALSE}
# identify which tends to be more conservative
cutoff_test = cutoff.points.jerk$t - cutoff.points.jounce$t
conservative_jerk = length(cutoff_test[cutoff_test>=0])
conservative_jounce = length(cutoff_test[cutoff_test<0])
if( conservative_jerk >= conservative_jounce ){
cutoff.points = plyr::rename(cutoff.points.jerk,c('t'='cutoff.t'))
}else{
cutoff.points = plyr::rename(cutoff.points.jounce,c('t'='cutoff.t'))
}
# merge with the rest of the dataframe, trim instruction period, drop unneded variables, and filter
coords.trimmed = coords.deriv %>% ungroup() %>%
merge(., cutoff.points,
by = c('dyad','conv.num','conv.type','cond')) %>%
group_by(dyad, conv.num, conv.type, cond) %>%
dplyr::filter(t > unique(cutoff.t)) %>%
select(-one_of('cutoff.t','cutoff'),
-matches('jerk'),
-matches('jounce'))
```
***
## Save trimmed data to file
```{r save-trimmed-data, eval = FALSE}
write.table(coords.trimmed,'./data/DCC-trimmed-data.csv', sep=',',
row.names=FALSE,col.names=TRUE)
```
***
## Summary statistics on conversation lengths
```{r invisible-load-trimmed-for-stats, echo = FALSE}
# invisible chunk: load in the conversation data to correctly display table if everything else is `eval=FALSE`
coords.trimmed = read.table('./data/DCC-trimmed-data.csv', sep=',',header=TRUE)
```
```{r summary-conv-stats, eval = TRUE}
# identify the maximum time for each dyad
interaction.time = coords.trimmed %>%
dplyr::group_by(dyad, conv.type) %>%
dplyr::summarize(duration = max(t) - min(t))
# what's the mean length of conversation data (in seconds)?
mean(interaction.time$duration)
# what's the range of conversation data (in seconds)?
range(interaction.time$duration)
```
```{r plot-recorded-lengths, echo=FALSE, fig.width=3, fig.height=3, fig.align='center', eval = FALSE}
# create the plot
bin_width = 20
recorded.lengths = qplot(round(interaction.time$duration),
geom='histogram',binwidth=bin_width) +
geom_histogram(aes(fill = ..count..),binwidth=bin_width) +
scale_fill_viridis(discrete=FALSE) +
xlab('Recorded Length (sec)') + ylab('Frequency') +
ggtitle('Histogram of Recording Lengths\nfor All Included Conversations') +
labs(fill="Freq.")
# save a high-resolution version of the plot
ggsave(plot = recorded.lengths,
height = 3,
width = 3,
filename = './figures/DCC-recorded_lengths.jpg')
# save a smaller version of the plot for knitr
ggsave(plot = recorded.lengths,
height = 3,
width = 3,
dpi=100,
filename = './figures/DCC-recorded_lengths-knitr.jpg')
```
![**Figure**. Histogram of all recorded conversation lengths within the dataset.](./figures/DCC-recorded_lengths-knitr.jpg)
***
# Recurrence analyses
**NOTE**: The chunks of code in this section do not have to be run each time, since the resulting datasets will be saved to CSV files. As a result, these chunks are currently set to `eval=FALSE`. Bear this in mind if these data need to be re-calculated.
***
## Preliminaries
This section clears the workspace and reads in the prepared data files.
```{r recurrence-prelims, warning = FALSE, error = FALSE, message = FALSE, eval = FALSE}
# clear our workspace
rm(list=ls())
# read in libraries and create functions
source('./supplementary-code/libraries_and_functions-DCC.r')
# load data
coords = read.table('./data/DCC-trimmed-data.csv', sep=',', header = TRUE)
```
***
## Identify CRQA parameters
Before we can analyze the data, we need to identify the appropriate parameters for continuous CRQA for the dataset. We identify parameters that provide a steady *rate of recurrence* or *RR* of 5% for each conversation of each dyad and save these parameters to a CSV file.
The source file produces outputs that are useful for tracking progress, but we suppress them here for brevity.
```{r crqa-parameters, echo = FALSE, warning = FALSE, error = FALSE, message = FALSE, eval = FALSE}
# run CRQA parameters
source('./supplementary-code/continuous_rqa_parameters-DCC.r')
```
***
## Prepare for CRQA and DRPs
```{r ready-for-crqa-drps, eval = FALSE}
# read in our chosen parameters
crqa_params = read.table('./data/crqa_data_and_parameters-DCC.csv',
sep=',',header=TRUE)
# grab only the parameters we need
crqa_params = crqa_params %>%
select(-matches('euclid')) %>%
distinct()
# rescale the data (by mean)
coords_crqa = coords %>% ungroup() %>%
group_by(dyad,conv.num) %>%
mutate(rescale.euclid0 = euclid0/mean(euclid0)) %>%
mutate(rescale.euclid1 = euclid1/mean(euclid1)) %>%
select(-matches('jounce'))
# fold in our CRQA parameter information
coords_crqa = plyr::join(x=crqa_params,y=coords_crqa,
by=c('dyad'='dyad',
'conv.num'='conv.num'))
# slice up the data so that we have one dataset per conversation
split_convs = split(coords_crqa,
list(coords_crqa$dyad, coords_crqa$conv.num))
```
***
## Run CRQA and DRPs
Now that we have our parameters, we run continuous CRQA over each conversation for each dyad using the `crqa` function from the `crqa` package (Coco & Dale, 2014, *Frontiers in Psychology*).
```{r run-crqa-using-parameters, eval = FALSE}
# identify window size
target_seconds = 5
win_size = target_seconds * sampling_rate
# cycle through each conversation using the sliced subsets
drp_results = data.frame()
crqa_results = data.frame()
for (next_conv in split_convs){
# isolate parameters for this next dyad
chosen.delay = unique(next_conv$chosen.delay)
chosen.embed = unique(next_conv$chosen.embed)
chosen.radius = unique(next_conv$chosen.radius)
# # print update
# print(paste("CRQA: Dyad ", unique(next_conv$dyad),
# ", conversation ",unique(next_conv$conv.num),
# sep=""))
# run cross-recurrence
rec_analysis = crqa(ts1=next_conv$rescale.euclid0,
ts2=next_conv$rescale.euclid1,
delay=chosen.delay,
embed=chosen.embed,
r=chosen.radius,
normalize=0,
rescale=0,
mindiagline=2,
minvertline=2,
tw=0,
whiteline=FALSE,
recpt=FALSE)
# save plot-level information to dataframe
dyad_num = unique(next_conv$dyad)
next_data_line = data.frame(c(dyad_num,
unique(next_conv$conv.type),
rec_analysis[1:9]))
names(next_data_line) = c("dyad",'conv.type',names(rec_analysis[1:9]))
crqa_results = rbind.data.frame(crqa_results,next_data_line)
# recreate DRP from diagonal lines within our target window
diag_lines = spdiags(rec_analysis$RP)
subset_plot = data.frame(diag_lines$B[,diag_lines$d >= -win_size & diag_lines$d <= win_size])
rr = colSums(subset_plot)/dim(subset_plot)[1]
# convert to dataframe, padding (with 0) where no RR was observed
next_drp = dplyr::full_join(data.frame(lag = as.integer(stringr::str_replace(names(rr),'X',''))-(win_size+1),
rr = rr),
data.frame(lag = -win_size:win_size),
by='lag')
next_drp[is.na(next_drp)] = 0
# save it to dataframe
next_drp$dyad = dyad_num
next_drp$conv.type = unique(next_conv$conv.type)
drp_results = rbind.data.frame(drp_results,next_drp)
}
# save results to file
write.table(crqa_results,'./data/crqa_results-DCC.csv',sep=",")
write.table(drp_results,'./data/drp_results-DCC.csv',sep=',')
```
***
## Export merged recurrence dataset
```{r merge-recurrence-datasets, eval = FALSE}
# merge CRQA and DRP analysis results
recurrence_results = plyr::join(drp_results, crqa_results,
by=c('dyad','conv.type'))
# grab information about experiment condition
additional_dyad_info = coords %>% ungroup() %>%
select(dyad,conv.num,conv.type,cond) %>% distinct()
# merge recurrence analyses and condition information
recurrence_df = plyr::join(recurrence_results, additional_dyad_info,
by=c('dyad','conv.type'))
# save to file
write.table(recurrence_df,'./data/recurrence_df-DCC.csv',sep=',')
```
***
# Data preparation
Now that we've calculated our CRQA and DRP measures, we're ready to prepare our data for analysis.
**NOTE**: The chunks of code in this section do not have to be run each time, since the resulting datasets will be saved to CSV files. As a result, these chunks are currently set to `eval=FALSE`. Bear this in mind if these data need to be re-calculated.
***
## Preliminaries
This section clears the workspace and reads in the prepared data files.
```{r prep-prelim, warning=FALSE, error=FALSE, message=FALSE, eval = FALSE}
# clear our workspace
rm(list=ls())
# read in libraries and create functions
source('./supplementary-code/libraries_and_functions-DCC.r')
# read in the recurrence dataframe
recurrence_df = read.table('./data/recurrence_df-DCC.csv',sep=',',header=TRUE)
```
***
## Create first- and second-order polynomials
In order to examine the linear and curvilinear patterns in the DRPs (cf. Main, Paxton, & Dale, 2016, *Emotion*), we create orthogonal polynomials for the lag term. This section creates the first- and second-order othogonal polynomials that are essential to allowing us to interpret the linear (i.e., first-order polynomial) and quadratic (i.e., second-order polynomial) patterns in the DRP independently from one another.
```{r create-polynomials, eval = FALSE}
# create first- and second-order orthogonal polynomials for lag
raw_lag = min(recurrence_df$lag):max(recurrence_df$lag)
lag_vals = data.frame(raw_lag)
lag_offset = (0-min(raw_lag)) + 1
t = stats::poly((raw_lag + lag_offset), 2)
lag_vals[, paste("ot", 1:2, sep="")] = t[lag_vals$raw_lag + lag_offset, 1:2]
# join it to the original data table
recurrence_df = left_join(recurrence_df,lag_vals, by = c("lag" = "raw_lag"))
```
***
## Create interaction terms
Because we will be providing both standardized and raw models, we create all interaction terms here. For simplicity, we will now change the `conv.type` variable to `convers` and `cond` to `condition`. Additionally, because we will be manually creating all interaction terms, we code `condition` and `convers` with levels `-0.5` and `0.5`; this ensures that we have nonzero values for interaction terms in the affiliative (`convers = 0`) and dual-task (`condition = 0`) cases.
```{r create-interactions, eval = FALSE}
# rename variables and center the binary variables
recurrence_df = recurrence_df %>% ungroup() %>%
plyr::rename(.,
c("conv.type"="convers",
"cond"="condition")) %>%
mutate(condition = condition-.5) %>%
mutate(convers = convers-.5) %>%
mutate(condition.convers = condition * convers) %>%
# first-order polynomials
mutate(condition.ot1 = condition * ot1) %>%
mutate(convers.ot1 = convers * ot1) %>%
mutate(condition.convers.ot1 = condition * convers * ot1) %>%
# second-order polynomials
mutate(condition.ot2 = condition * ot2) %>%
mutate(convers.ot2 = convers * ot2) %>%
mutate(condition.convers.ot2 = condition * convers * ot2) %>%
# polynomial interactions
mutate(ot1.ot2 = ot1 * ot2) %>%
mutate(condition.ot1.ot2 = condition * ot1 * ot2) %>%
mutate(convers.ot1.ot2 = convers * ot1 * ot2) %>%
mutate(condition.convers.ot1.ot2 = condition * convers * ot1 * ot2)
```
***
## Create standardized dataframe
Let's create a new dataframe with all standardized variables. This allows us to interpret the resulting values as effect sizes (see Keith, 2005, *Multiple regression and beyond*).
```{r standardized-dataframe, eval = FALSE}
# standardize all variables
rec_st = mutate_each(recurrence_df,funs(as.numeric(scale(.))))
```
***
## Export analysis and plotting dataframes
```{r export-analysis-and-plotting, eval = FALSE}
# export plotting dataframe
write.table(recurrence_df,'./data/plotting_df-DCC.csv',row.names=FALSE,sep=',')
# export standardized analysis dataframe
write.table(rec_st,'./data/analysis_df-DCC.csv',row.names=FALSE,sep=',')
```
***
# Data analysis
All data have been cleaned, all parameters have been identified, and all final data preparation has been finished. Using the analysis-ready dataframe (`rec_st`) and plotting dataframe (`rec_plot`), we now analyze our data and generate visualizations.
***
## Preliminaries
This section clears the workspace and reads in the prepared data files.
```{r analysis-prelim, warning=FALSE, error=FALSE, message=FALSE}
# clear our workspace
rm(list=ls())
# read in libraries and create functions
source('./supplementary-code/libraries_and_functions-DCC.r')
# read in the plotting and analysis recurrence dataframes
rec_st = read.table('./data/analysis_df-DCC.csv',sep=',',header=TRUE)
rec_plot = read.table('./data/plotting_df-DCC.csv',sep=',',header=TRUE)
```
***
## Recurrence by lag, conversation type, and condition
We now create a linear mixed-effects model to gauge how linear lag (`ot1`) and quadratic lag (`ot2`) interact with conversation type (`convers`) and task (`condition`) to influence head movement coordination (`rr`). We present both standardized and raw models below.
```{r central-model, warning=FALSE, error=FALSE, message=FALSE}
# standardized maximal random-effects model
rec_convers_condition_gca_st = lmer(rr ~ convers + condition + ot1 + ot2 +
condition.convers + ot1.ot2 +
convers.ot1 + condition.ot1 + condition.convers.ot1 +
convers.ot2 + condition.ot2 + condition.convers.ot2 +
convers.ot1.ot2 + condition.ot1.ot2 + condition.convers.ot1.ot2 +
(1 + ot1 + ot2 + convers + condition.convers.ot1 | conv.num) +
(1 + ot1 + ot2 + convers + condition.convers.ot1 | dyad),
data=rec_st, REML=FALSE)
invisible(pander_lme_to_latex(rec_convers_condition_gca_st,'standardized_model_latex-DCC.tex'))
pander_lme(rec_convers_condition_gca_st,stats.caption = TRUE)
# raw maximal random-effects model
rec_convers_condition_gca_raw = lmer(rr ~ convers + condition + ot1 + ot2 +
condition.convers + ot1.ot2 +
convers.ot1 + condition.ot1 + condition.convers.ot1 +
convers.ot2 + condition.ot2 + condition.convers.ot2 +
convers.ot1.ot2 + condition.ot1.ot2 + condition.convers.ot1.ot2 +
(1 + ot1 + ot2 + convers + condition.convers.ot1 | conv.num) +
(1 + ot1 + ot2 + convers + condition.convers.ot1 | dyad),
data=rec_plot,REML=FALSE)
pander_lme(rec_convers_condition_gca_raw,stats.caption = TRUE)
```
***
### Comparing maximal random-effects model to random-intercepts-only model
We next check whether our maximal random-effects model (above) provides a better fit to the data than a model with only random intercepts for `dyad` and `conv.num` that is otherwise identical. We present analyses of both standardized and raw datasets.
The maximal random-effects model (i.e., that with maximally permissible random slopes for random intercepts of `dyad` and `conv.num` using backwards selection; cf. Barr et al., 2013, *Journal of Memory and Language*) accounts for significantly more of the data than the random-intercept-only model. Plots of the residuals of both maximal and random-intercepts-only models demonstrate that the maximal model better meets the assumptions of homogeneity and normality of residuals than the random-intercepts-only model.
```{r checking-maximal-standardized, warning=FALSE, error=FALSE, message=FALSE}
# standardized random-intercepts-only model
rec_convers_condition_gca_st_rio = lmer(rr ~ convers + condition + ot1 + ot2 +
condition.convers + ot1.ot2 +
convers.ot1 + condition.ot1 + condition.convers.ot1 +
convers.ot2 + condition.ot2 + condition.convers.ot2 +
convers.ot1.ot2 + condition.ot1.ot2 + condition.convers.ot1.ot2 +
(1 | conv.num) + (1 | dyad), data=rec_st, REML=FALSE)
# is the maximal random-effects model a better fit than the random-intercepts-only model? (standardized)
pander_anova(anova(rec_convers_condition_gca_st_rio,rec_convers_condition_gca_st,test="Chisq"))
```
```{r checking-maximal-raw, warning=FALSE, error=FALSE, message=FALSE}
# raw random-intercepts-only model
rec_convers_condition_gca_raw_rio = lmer(rr ~ convers + condition + ot1 + ot2 +
condition.convers + ot1.ot2 +
convers.ot1 + condition.ot1 + condition.convers.ot1 +
convers.ot2 + condition.ot2 + condition.convers.ot2 +
convers.ot1.ot2 + condition.ot1.ot2 + condition.convers.ot1.ot2 +
(1 | conv.num) + (1 | dyad), data=rec_plot,REML=FALSE)
# is the maximal random-effects model a better fit than the random-intercepts-only model? (raw)
pander_anova(anova(rec_convers_condition_gca_raw_rio,rec_convers_condition_gca_raw,test='Chisq'))
```
```{r plot-standardized-residuals, echo = FALSE, eval = FALSE}
# plot residuals for random-intercepts-only model
rio_residuals = qplot(x = fitted(rec_convers_condition_gca_st_rio),
y = resid(rec_convers_condition_gca_st_rio)) +
geom_hline(aes(yintercept=0)) + geom_smooth() +
scale_color_viridis() +
coord_cartesian(xlim = c(-4,4), ylim = c(-2,3)) +
ylab('Residuals') + xlab('Fitted') + ggtitle('Random-Intercepts-Only')
# plot residuals for maximal model
max_residuals = qplot(x = fitted(rec_convers_condition_gca_st),
y = resid(rec_convers_condition_gca_st)) +
geom_hline(aes(yintercept=0)) + geom_smooth() +
coord_cartesian(xlim = c(-4,4), ylim = c(-2,3)) +
ylab('Residuals') + xlab('Fitted') + ggtitle('Maximal')
# plot and save
ggsave('./figures/DCC-standardized_residuals_plot.png',
units = "in", width = 5, height = 3, dpi=100,
grid.arrange(
top=textGrob("Homogeneity of Standardized Models",
gp=gpar(fontsize=14)),
rio_residuals,
max_residuals,
nrow = 1
))
```
![**Figure**. Residuals plots using standardized dataset. Residuals are more evenly distributed around 0 for the maximal model (right) than the random-intercepts-only model (left), meeting the assumption of homogeneity of residuals.](./figures/DCC-standardized_residuals_plot.png)
```{r hist-standardized-residuals, echo = FALSE, eval = FALSE}
# set bin width
bin_width = .1
y_max = 900
# plot histogram of residuals for random-intercepts-only model
rio_residuals = qplot(resid(rec_convers_condition_gca_st_rio),
geom='histogram',binwidth=bin_width) +
geom_histogram(aes(fill = ..density..),binwidth=bin_width) +
ylab('Frequency') + xlab('Residuals') +
scale_fill_viridis(discrete=FALSE,name='Density') +
coord_cartesian(xlim = c(-3,3), ylim = c(0,y_max)) +
theme(legend.position='bottom') +
ggtitle('Random-Intercepts-Only')
# plot histogram of residuals maximal model
max_residuals = qplot(resid(rec_convers_condition_gca_st),
geom='histogram',binwidth=bin_width) +
geom_histogram(aes(fill = ..density..),binwidth=bin_width) +
ylab('Frequency') + xlab('Residuals') +
scale_fill_viridis(discrete=FALSE,name='Density') +
coord_cartesian(xlim = c(-3,3), ylim = c(0,y_max)) +
theme(legend.position='bottom') +
ggtitle('Maximal')
# plot and save
ggsave('./figures/DCC-standardized_residuals_hist.png',
units = "in", width = 6, height = 3, dpi=100,
grid.arrange(
top=textGrob("Normality of Standardized Models",
gp=gpar(fontsize=14)),
rio_residuals,
max_residuals,
nrow = 1
))
```
![**Figure**. Frequency density plots of residuals using standardized dataset. Residuals for the maximal model (right) are more normally distributed than the random-intercepts-only model (left), meeting the assumption of normality of residuals.](./figures/DCC-standardized_residuals_hist.png)
***
## Exploring interaction terms
Let's do some investigations into the significant four-way interaction. After inspecting the interaction plot (see Discussion section), we choose to divide the data by conversation type -- exploring whether we still see significant differences in synchrony by task condition when we examine the conversations separately.
***
### Prepare separate datasets for each conversation
We create raw and standardized datasets for each conversation type here.
```{r prep-post-hoc-data}
# select only the friendly conversations (convers = -.5)
aff_only_raw = rec_plot %>%
dplyr::filter(convers < 0)
# restandardize friendly conversation data
aff_only_st = aff_only_raw %>%
mutate_each(.,funs(as.numeric(scale(.)))) %>%
mutate(convers = -.5)
# select only the arguments (convers = .5)
arg_only_raw = rec_plot %>%
dplyr::filter(convers > 0)
# restandardize argument data
arg_only_st = arg_only_raw %>%
mutate_each(.,funs(as.numeric(scale(.)))) %>%
mutate(convers = .5)
```
***
### Post-hoc interaction tests
Do we still see significant differences by condition in each conversation?
```{r aff-post-hoc-test}
# check for differences in the friendly conversation (raw)
cond_aff_gca_raw = lmer(rr ~ condition +
ot1 + ot2 + ot1.ot2 +
condition.ot1 + condition.ot2 + condition.ot1.ot2 +
(1 + ot1 + ot2 | conv.num) +
(1 + ot1 + ot2 | dyad),
data=aff_only_raw,REML=FALSE)
pander_lme(cond_aff_gca_raw, stats.caption=TRUE)
# check for differences in the friendly conversation (standardized)
cond_aff_gca_st = lmer(rr ~ condition +
ot1 + ot2 + ot1.ot2 +
condition.ot1 + condition.ot2 + condition.ot1.ot2 +
(1 + ot1 + ot2 | conv.num) +
(1 + ot1 + ot2 | dyad),
data=aff_only_st,REML=FALSE)
invisible(pander_lme_to_latex(cond_aff_gca_st,'aff_posthoc_latex-DCC.tex'))
pander_lme(cond_aff_gca_st, stats.caption=TRUE)
```
```{r arg-post-hoc-test}
# check for differences in the argumentative conversation (raw)
cond_arg_gca_raw = lmer(rr ~ condition +
ot1 + ot2 + ot1.ot2 +
condition.ot1 + condition.ot2 + condition.ot1.ot2 +
(1 + ot1 + ot2 | conv.num) +
(1 + ot1 + ot2 | dyad),
data=arg_only_raw,REML=FALSE)
pander_lme(cond_arg_gca_raw, stats.caption=TRUE)
# check for differences in the argumentative conversation (standardized)
cond_arg_gca_st = lmer(rr ~ condition +
ot1 + ot2 + ot1.ot2 +
condition.ot1 + condition.ot2 + condition.ot1.ot2 +
(1 + ot1 + ot2 | conv.num) +
(1 + ot1 + ot2 | dyad),
data=arg_only_st,REML=FALSE)
invisible(pander_lme_to_latex(cond_arg_gca_st,'arg_posthoc_latex-DCC.tex'))
pander_lme(cond_arg_gca_st, stats.caption=TRUE)
```
***
# Surrogate analysis: Phase-radomized baseline
To explore how these patterns differ from the amount of coordination that might be expected by chance, we here re-create the above models with a deconstructed time series.
***
## Surrogate data preparation
***
### Preliminaries
```{r surrogate-prep-prelim, warning=FALSE, error=FALSE, message=FALSE, eval = FALSE}
# clear our workspace
rm(list=ls())
# read in libraries and create functions
source('./supplementary-code/libraries_and_functions-DCC.r')
# load real data and CRQA parameters
coords = read.table('./data/DCC-trimmed-data.csv', sep=',', header = TRUE)
crqa_params = read.table('./data/crqa_data_and_parameters-DCC.csv',
sep=',',header=TRUE)
```
***
### Load CRQA parameters and specify chunks
Load in the CRQA parameters specified for the real data and prepare the dataset for analysis.
```{r surrogate-prep-for-crqa-drps, eval = FALSE}
# grab only the parameters we need
crqa_params = crqa_params %>%
select(-matches('euclid')) %>%
distinct()
# rescale the data (by mean)
coords_crqa = coords %>% ungroup() %>%
group_by(dyad,conv.num) %>%
mutate(rescale.euclid0 = euclid0/mean(euclid0)) %>%
mutate(rescale.euclid1 = euclid1/mean(euclid1)) %>%
select(-matches('jounce'))
# merge with parameters
coords_crqa = plyr::join(x = crqa_params,
y = coords_crqa,
by = c('dyad'='dyad',
'conv.num'='conv.num'))
# slice up the data so that we have one dataset per conversation
split_convs = split(coords_crqa,
list(coords_crqa$dyad, coords_crqa$conv.num))
```
***
### Calculate CRQA and DRPs for surrogate data
We create surrogate movement data for each participant through phase-randomization, implemented using the `FFTsurrogate` function in the `nonlinearTseries` package. We then calculate CRQA over those surrogate data in order to create a baseline value of synchrony between participants.
```{r surrogate-crqa-calculation, eval = FALSE}
# identify window size and set seed (for reproducibility)
target_seconds = 5
win_size = target_seconds * sampling_rate
set.seed(111)
# cycle through each conversation using the sliced subsets
drp_results = data.frame()
crqa_results = data.frame()
for (next_conv in split_convs){
# isolate parameters for next dyad
chosen.delay = unique(next_conv$chosen.delay)
chosen.embed = unique(next_conv$chosen.embed)
chosen.radius = unique(next_conv$chosen.radius)
dyad_num = unique(next_conv$dyad)
# # print update
# print(paste("Surrogate CRQA: Dyad ", unique(next_conv$dyad),
# ", conversation ",unique(next_conv$conv.num),
# sep=""))
# re-create the surrogate dyad and run again
for (run in 1:10){
# create phase-randomized baseline for each participant
shuffle0 = nonlinearTseries::FFTsurrogate(next_conv$rescale.euclid0, n.samples = 1)
shuffle1 = nonlinearTseries::FFTsurrogate(next_conv$rescale.euclid1, n.samples = 1)
# run cross-recurrence
rec_analysis = crqa(ts1=shuffle0,
ts2=shuffle1,
delay=chosen.delay,
embed=chosen.embed,
r=chosen.radius,
normalize=0,
rescale=0,
mindiagline=2,
minvertline=2,
tw=0,
whiteline=FALSE,
recpt=FALSE)
# save plot-level information to dataframe
next_data_line = data.frame(c(run,
dyad_num,
unique(next_conv$conv.type),
rec_analysis[1:9]))
names(next_data_line) = c('run',"dyad",'conv.type',names(rec_analysis[1:9]))
crqa_results = rbind.data.frame(crqa_results,next_data_line)
# recreate DRP from diagonal lines within our target window
diag_lines = spdiags(rec_analysis$RP)
subset_plot = data.frame(diag_lines$B[,diag_lines$d >= -win_size & diag_lines$d <= win_size])
rr = colSums(subset_plot)/dim(subset_plot)[1]
# convert to dataframe, padding (with 0) where no RR was observed
next_drp = full_join(data.frame(lag = as.integer(stringr::str_replace(names(rr),'X',''))-(win_size+1),
rr = rr),
data.frame(lag = -win_size:win_size),
by='lag')
next_drp[is.na(next_drp)] = 0
# save it to dataframe
next_drp$dyad = dyad_num
next_drp$conv.type = unique(next_conv$conv.type)
next_drp$run = run
drp_results = rbind.data.frame(drp_results,next_drp)
}}
# merge CRQA and DRP analysis results
recurrence_results = plyr::join(drp_results, crqa_results,
by=c('dyad',
'conv.type',
'run'))
# grab information about experiment condition
additional_dyad_info = coords %>% ungroup() %>%
select(dyad,conv.num,conv.type,cond) %>% distinct()
# merge recurrence analyses and condition information
surrogate_recurrence_df = plyr::join(recurrence_results, additional_dyad_info,
by=c('dyad','conv.type'))
```
***
### Create polynomials and center binary variables for surrogate data
```{r surrogate-polynomials, eval = FALSE}