-
Notifications
You must be signed in to change notification settings - Fork 0
/
politeness_and_coordination.Rmd
428 lines (333 loc) · 15.3 KB
/
politeness_and_coordination.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
---
title: "Politeness and Coordination"
author: "A. Paxton, L. Brown, & B. Winter"
output:
html_document:
keep_md: yes
number_sections: yes
---
This R markdown provides the data preparation for our project analyzing how
power and politeness affect interpersonal movement synchrony during a variety of
interaction tasks (Paxton, Brown, & Winter, *in preparation*).
To run this from scratch, you will need the following files:
* `./data/movement_dataframes-aggregated/`: Directory of movement data derived
from videos (30 FPS) using a frame-differencing method (e.g., Paxton & Dale,
2013, *Behavior Research Methods*). De-identified movement data are freely
available in the OSF repository for the project: `https://osf.io/wrzf2/`.
* `./scripts/03-data_analysis/required_packages-pac.r`: Installs required
libraries, if they are not already installed. **NOTE**: This should be run
*before* running this script.
* `./scripts/03-data_analysis/libraries_and_functions-pac.r`: Loads in necessary
libraries and creates new functions for our analyses.
Additional files will be created during the initial run that will help reduce processing
time. Many of these are available as CSVs from the OSF repository listed above.
**Code written by**: A. Paxton (University of Connecticut)
**Date last modified**: 20 July 2019
***
# Data import
***
## Preliminaries
This section clears the workspace, loads required packages, and specifies
important global variables.
**Remember** to have run `./scripts/03-data_analysis/required_packages-pac.r`
locally *first*, to ensure that you have all necessary packages installed.
```{r prep-prelim, warning = FALSE, error = FALSE, message = FALSE}
# clear our workspace
rm(list=ls())
# turn on caching
knitr::opts_chunk$set(cache=TRUE, autodep=TRUE, cache.lazy=FALSE)
# set random seed, with options for parallelizations (later)
RNGkind("L'Ecuyer-CMRG")
set.seed(111)
# read in libraries and create functions
source('./scripts/03-data_analysis/libraries_and_functions-pac.r')
```
***
## Pre-process and concatenate movement data
After initial video processing, each video clip produced its own file of
movement data. Here, we first pre-process the data by downsampling to 10Hz
(consistent with previous research on movement coordination; cf. Paxton & Dale,
2013, *Behavior Research Methods*). We then remove the first and last few
seconds of the data from each conversation and concatenate all conversations'
movement data into a single file.
```{r preprocess-concatenate-data, warning = FALSE, error = FALSE, message = FALSE}
# get list of individual conversations included in the data
conversation_files = list.files('./data/movement_dataframes-aggregated',
full.names = TRUE)
# process each file
conversation_df = data.frame()
for (conversation_file in conversation_files){
# read in next file
next_conversation = read.table(conversation_file, sep=',',
header=TRUE, stringsAsFactors = FALSE)
# split info from dyad
if (!grepl("Map", conversation_file)){
next_conversation = next_conversation %>% ungroup() %>%
separate(dyad, into = c('participant_id',
'participant_gender',
'partner_type',
'task',
'condition'), sep='_') %>%
# remove .mov extension from condition
mutate(condition = sub(pattern = "(.*?)\\..*$",
replacement = "\\1",
condition)) %>%
# delineate movement between target participant (1 / L) and partner (0 / R)
mutate(participant = ifelse(participant=='left',
1,
0))
} else {
next_conversation = next_conversation %>% ungroup() %>%
separate(dyad, into = c('participant_id',
'participant_gender',
'partner_type',
'task',
'condition',
'map_cond'), sep='_') %>%
# fix the task information to include who led (participant vs. interlocutor)
unite(task, c('task','map_cond')) %>%
mutate(task = sub(pattern = "(.*?)\\..*$",
replacement = "\\1",
task)) %>%
# delineate movement between target participant (1 / L) and partner (0 / R)
mutate(participant = ifelse(participant=='left',
1,
0))
}
# decimate movement
conversations_split = split(next_conversation,
next_conversation$participant)
decimated_df = data.frame()
for (next_ts in conversations_split){
# decimate the signal
next_ts = dplyr::arrange(next_ts, difference_number)
movement = signal::decimate(x = next_ts$movement, q = 3)
# reconstitute the dataframe
next_ts = data.frame(next_ts) %>% ungroup() %>%
# grab only the top-line data we need and remove old movement variable
slice(1) %>%
dplyr::select(-difference_number,
-movement) %>%
# add the decimated movement and create time variable
cbind(., movement) %>%
rownames_to_column(var="frame") %>%
mutate(t = as.numeric(frame) / 10) %>%
# trim the beginning and end (instructions and wrap-up time)
dplyr::filter(t > trimmed_time,
t < (max(t) - trimmed_time)) %>%
# rename participant
dplyr::rename(interlocutor = participant)
# add to shell
decimated_df = rbind.data.frame(decimated_df,
next_ts)
}
# save plot of movement with beginning and end trimmed
trimmed_movement_plot = ggplot(decimated_df,
aes(x = t,
y = movement,
color = as.factor(interlocutor))) +
geom_line() +
geom_path() +
theme(legend.position="bottom")
ggsave(plot = trimmed_movement_plot,
height = 3,
width = 3,
filename = paste0('./figures/movement-trimmed/pac-trimmed_movement-',
basename(conversation_file),'.jpg'))
# append dataframe to group files
conversation_df = rbind.data.frame(conversation_df,
decimated_df)
}
# save aggregated file
write.table(conversation_df,
'./data/pac-filtered_movement_data.csv',
sep = ",", append = FALSE, row.names = FALSE, col.names = TRUE)
```
## Summarize conversation information
Now that we've prepared the data for recurrence analyses, let's find
out a bit more about the conversations.
```{r summarize-info}
summary_stats = conversation_df %>% ungroup() %>%
dplyr::group_by(participant_id, task) %>%
summarise(duration = max(t)) %>%
mutate(num_samples = duration * downsampled_sampling_rate) %>%
mutate(max_time = round(duration / 60, 2)) %>%
ungroup()
```
```{r summary-range-duration}
# what's the range of conversation data (in minutes) by conversation type?
summary_duration = summary_stats %>% ungroup() %>%
dplyr::group_by(task) %>%
summarise(min_duration = min(max_time),
max_duration = max(max_time),
mean_duration = mean(max_time))
print(summary_duration)
```
```{r plot-conversation-summary-stats, echo=FALSE, warning = FALSE, error = FALSE, message = FALSE}
# separate by different tasks
map_p_summary_df = filter(summary_stats, task=="Map_I")
map_i_summary_df = filter(summary_stats, task=="Map_I")
convo_summary_df = filter(summary_stats, task=="Convo")
tweety_summary_df = filter(summary_stats, task=="Tweety")
role_summary_df = filter(summary_stats, task=="Role")
# create histograms for each
map_p_summary_hist = qplot(map_p_summary_df$max_time,
geom = 'histogram', bins = 30) +
xlim(c(0,15)) + ylab('Count') + xlab('Duration (min)') + ggtitle('Map task: Participant leads')
map_i_summary_hist = qplot(map_i_summary_df$max_time,
geom = 'histogram', bins = 30) +
xlim(c(0,15)) + ylab('Count') + xlab('Duration (min)') + ggtitle('Map task: Partner leads')
convo_summary_hist = qplot(convo_summary_df$max_time,
geom = 'histogram', bins = 30) +
xlim(c(0,15)) + ylab('Count') + xlab('Duration (min)') + ggtitle('Conversation')
tweety_summary_hist = qplot(tweety_summary_df$max_time,
geom = 'histogram', bins = 30) +
xlim(c(0,15)) + ylab('Count') + xlab('Duration (min)') + ggtitle('Story retelling')
role_summary_hist = qplot(role_summary_df$max_time,
geom = 'histogram', bins = 30) +
xlim(c(0,15)) + ylab('Count') + xlab('Duration (min)') + ggtitle('Role-playing')
# save smaller version for knitr
ggsave('./figures/summary_info/pac-conversation_lengths.png',
units = "in", width = 4, height = 7, dpi=100,
grid.arrange(
top = textGrob("Duration histograms by conversation type",
gp=gpar(fontsize=14)),
map_p_summary_hist,
map_i_summary_hist,
convo_summary_hist,
tweety_summary_hist,
role_summary_hist,
ncol = 1
))
```
![**Figure**. Histograms for conversation duration by conversation type.](./figures/summary_info/pac-conversation_lengths.png)
***
# Cross-wavelet analyses
To measure interpersonal coordination, we'll be using *wavelet coherence* (e.g.,
Grinsted, Moore, & Jevrejeva, 2004, *Nonlinear Processes in Geophysics*). This
allows us to examine the phase relationship between two signals in time *and*
frequency.
***
## Preliminaries
Before we get going, let's re-read our data (so that we're sure it's clean) and
prepare our variables.
```{r wavelet-prelims, warning = FALSE, error = FALSE, message = FALSE}
# clear our workspace
rm(list=ls())
# read in libraries and functions again
source('./scripts/03-data_analysis/libraries_and_functions-pac.r')
```
```{r wavelet-data-prep}
# load in our data
conversation_df = read.table('./data/pac-filtered_movement_data.csv',
sep=',',header=TRUE)
# clean up our data
conversation_df = conversation_df %>%
# spread data to wide form (with fixed variable names)
spread(.,
interlocutor, movement) %>%
rename(movement_0 = '0',
movement_1 = '1') %>%
# convert to partner type to numeric
mutate(partner_type_str = partner_type) %>%
mutate(partner_type = ifelse(partner_type=='Friend',
0, # friend = 0
1)) %>% # prof = 1
# convert to gender to numeric
mutate(participant_gender_str = participant_gender) %>%
mutate(participant_gender = ifelse(participant_gender=='F',
0, # female = 0
1)) %>% # male = 1
# convert to condition to numeric
mutate(condition_str = condition) %>%
mutate(condition = ifelse(condition=='AB',
0, # AB = 0
1)) %>% # BA = 1
# convert to task to numeric
mutate(task_str = task) %>%
mutate(task = ifelse(task=='Convo',
1, # Convo = 1
ifelse(task=='Map_P',
2, # Map, participant-lead = 2
ifelse(task=='Map_I',
3, # Map, partner-led = 3
ifelse(task=='Role',
4, # Role = 4
5))))) # Tweety = 5
# slice up the data so that we have one dataset per conversation
split_convs = split(conversation_df,
list(conversation_df$participant_id,
conversation_df$partner_type,
conversation_df$task))
```
## Parallelized wavelet coherence analyses
We have a large number of individual interactions to analyze. As a result, we
will take advantage of R's ability to facilitate *parallelization*, or
splitting activity across multiple cores so that we can run the code faster.
Because each interaction can be analyzed independently, we have the opportunity
to run each interaction's analysis in parallel rather than in sequence.
**NOTE**: If you are unfamiliar with parallelization, *proceed with caution*.
For more reading, check out
[this guide to parallelism in R by Florian Prive](https://privefl.github.io/blog/a-guide-to-parallelism-in-r/)
and [this chapter in *R programming for data science*](https://bookdown.org/rdpeng/rprogdatascience/parallel-computation.html).
```{r wavelet-parallelization}
# create directories for our output, if we don't have them yet
raw_output_directory = "./data/wavelet_results_raw"
processed_output_directory = "./data/wavelet_results_processed"
figure_directory = "./figures/wavelets"
dir.create(raw_output_directory,
showWarnings = TRUE,
recursive = TRUE)
dir.create(processed_output_directory,
showWarnings = TRUE,
recursive = TRUE)
dir.create(figure_directory,
showWarnings = TRUE,
recursive = TRUE)
# identify number of cores available
available_cores = detectCores() - 1
# initialize a pseudo-cluster with available cores
pseudo_cluster = parallel::makeCluster(available_cores,
type="FORK",
setup_strategy="sequential",
outfile = 'wavelet_log.txt',
verbose = TRUE)
# set seed for everyone
parallel::clusterSetRNGStream(pseudo_cluster, iseed = 111)
# parallelize our wavelet analyses
doParallel::registerDoParallel(pseudo_cluster)
source('./scripts/03-data_analysis/parallel_wavelets.R')
parallel_wavelets(split_convs,
raw_output_directory,
processed_output_directory,
figure_directory)
# stop the pseudocluster
stopCluster(pseudo_cluster)
```
## Gather wavelet outputs
```{r gather-wavelet-output}
# concatenate significant phases from file into a new dataframe
mean_sig_phases = list.files(processed_output_directory,
pattern = "*mean_sig_phase.csv",
full.names = TRUE, recursive = FALSE) %>%
lapply(read.csv, header=TRUE, sep=",") %>%
lapply(rownames_to_column, "n") %>%
lapply(mutate, n = as.numeric(n)) %>%
bind_rows()
# concatenate significant power from file into a new dataframe
mean_sig_power = list.files(processed_output_directory,
pattern = "*mean_sig_power.csv",
full.names = TRUE, recursive = FALSE) %>%
lapply(read.csv, header=TRUE, sep=",") %>%
lapply(rownames_to_column, "n") %>%
lapply(mutate, n = as.numeric(n)) %>%
bind_rows()
# concatenate significant phases from file into a new dataframe
mean_sig_time_power = list.files(processed_output_directory,
pattern = "*mean_sig_time_power.csv",
full.names = TRUE, recursive = FALSE) %>%
lapply(read.csv, header=TRUE, sep=",") %>%
lapply(rownames_to_column, "n") %>%
lapply(mutate, n = as.numeric(n)) %>%
bind_rows()
```