---
title: "Plots and analyses repair project"
author: "Marlou"
date: "13-01-2021"
output: html_document
---


```{r}
library(tidyr)
library(dplyr)
library(magrittr)
```

## read in data
```{r}
df_repair <- read.csv2("processing_output/4_repair_speech_clean.csv", header=T, colClasses = c(pairnr = "character", target= "factor", repair_seq_id="factor", speech_original="character", speech_clean="character", onset="character", offset="character"), stringsAsFactors=T)

df_gest <- read.csv2("processing_output/5_gestures_kinematics.csv", header=T, colClasses = c(pairnr = "character", onset="character", offset="character"), stringsAsFactors=T)


```

## process repair df
```{r}
#convert cols Tmin1, T0 and Tplus1 into 1 column 'repair_part' with Tmin1,T0 and Tplus1 as levels
df_repair <- df_repair %>% unite('repair_part', Tmin1:Tplus1, remove=T, na.rm=T)
#change level names (no special characters)
df_repair$repair_part %<>% gsub("T[+]1", "Tplus1", .) %>% gsub("T-1", "Tmin1", .)


#make sure T0_type and T0_position info is included in rows of T-1 and T+1 too
df_info <- df_repair %>% subset(repair_part == "T0") %>% select(repair_seq_id, T0_type, T0_position)

for (x in 1:nrow(df_repair)){
  for (y in 1:nrow(df_info)){
    if (df_repair[x,]$repair_seq_id == df_info[y,]$repair_seq_id){
      df_repair[x,]$T0_type <- df_info[y,]$T0_type
      df_repair[x,]$T0_position <- df_info[y,]$T0_position}
    }
}

#reorder levels
df_repair$T0_type <- factor(df_repair$T0_type, levels = c("openrequest", "restrictedrequest", "restrictedoffer"))
df_repair$repair_part <- factor(df_repair$repair_part, levels = c("Tmin1", "T0", "Tplus1"))

```

## process df_gest
compute gesture counts (split by gesture type) and submovements for every repair_id

```{r}
#start with merging gesture columns: one value per row (we do not distinguish between 1 or 2 handed gestures)
df_gest <- df_gest %>% 
  mutate(gesture_type = coalesce(A_LH_gesture_type, A_RH_gesture_type, B_LH_gesture_type, B_RH_gesture_type))


#COUNT GESTURES

#count gesture per repair_id
#NOTE: this also includes repetitions of the same gesture (coded with -REP/-rep in gesture_referent)
gest_summed <- df_gest %>%  group_by(repair_id, .drop=F) %>%  count(gesture_type)

#long to wide
gest_summed_wide <- spread(gest_summed, gesture_type, n)

#rename cols
gest_summed_wide <- gest_summed_wide  %>% rename("deictic_count"="deictic", "iconic_count"="iconic", "other_count"="other")
#compute total
gest_summed_wide$total_count <- gest_summed_wide$deictic_count+gest_summed_wide$iconic_count+gest_summed_wide$other_count
#reorder: iconic first
gest_summed_wide <- gest_summed_wide[,c(1,3,2,4,5)] 


#SUM NR OF SUBMOVEMENTS
gest_summed_wide <- merge(gest_summed_wide, df_gest %>% group_by(repair_id, .drop=F) %>% summarise(sum_subm = sum(submovements)), by="repair_id")
```

#merge gesture dfs and df_repair > df
```{r}
df <- merge(df_repair, gest_summed_wide, by=as.factor("repair_id"), all=T)

#replace NA to 0 for gesture columns
df <- mutate_at(df, c("iconic_count", "deictic_count", "other_count", "total_count", "sum_subm"), ~replace(., is.na(.), 0))

df$target <- as.factor(df$target)
df$repair_seq_id <- as.factor(df$repair_seq_id)
```

#speech count
```{r}
#count nr. of orthographic characters
df$speech_count <- nchar(gsub(" ", "", df$speech_clean))
```

## DIVISION OF LABOUR
Compute the proportion of labour by B in repair initiation relative to the total labour by both A+B
```{r}
##speech (not standardized yet)

#long to wide: one row per repair_seq_id
df_speech_labour <- spread(df, repair_part, speech_count)
df_speech_labour <- df_speech_labour %>% select(pairnr, target, repair_seq_id, T0_type, Tmin1:Tplus1) %>%group_by(pairnr, target, repair_seq_id, T0_type) %>% summarise_if(is.numeric, list(~sum(., na.rm=T)))
#convert 0 to NA (in T-1 column; for non-first repairs)
df_speech_labour[df_speech_labour$Tmin1==0,]$Tmin1<-NA
#compute relative labour T0 vs Tplus1
df_speech_labour$speech_T0_prop <- df_speech_labour$T0 / (df_speech_labour$T0 + df_speech_labour$Tplus1)
df_speech_labour$speech_Tplus1_prop <- df_speech_labour$Tplus1 / (df_speech_labour$T0 + df_speech_labour$Tplus1)
#rename cols
df_speech_labour <- df_speech_labour %>%
  rename(speech_Tmin1 = Tmin1, speech_T0 = T0, speech_Tplus1 = Tplus1)

##gesture (not standardized yet)

#long to wide: one row per repair_seq_id
df_gesture_labour <- spread(df, repair_part, sum_subm)
df_gesture_labour <- df_gesture_labour %>% select(pairnr, target, repair_seq_id, T0_type, Tmin1:Tplus1) %>%group_by(pairnr, target, repair_seq_id, T0_type) %>% summarise_if(is.numeric, list(~sum(., na.rm=T)))
#convert 0 to NA (in T-1 column; for non-first repairs)
df_gesture_labour[df_gesture_labour$Tmin1==0,]$Tmin1<-NA
#compute relative labour T0 vs Tplus1
df_gesture_labour$gesture_subm_T0_prop <- df_gesture_labour$T0 / (df_gesture_labour$T0 + df_gesture_labour$Tplus1)
df_gesture_labour$gesture_subm_Tplus1_prop <- df_gesture_labour$Tplus1 / (df_gesture_labour$T0 + df_gesture_labour$Tplus1)
#rename cols
df_gesture_labour <- df_gesture_labour %>%
  rename(gesture_subm_Tmin1 = Tmin1, gesture_subm_T0 = T0, gesture_subm_Tplus1 = Tplus1)


##multimodal labour

#standardize speech and submovement counts, then sum them
df_multi_labour <- df
df_multi_labour$speech_stand <-scale(df$speech_count, center=F, scale=T)
df_multi_labour$subm_stand <-scale(df$sum_subm, center=F, scale=T)
df_multi_labour$multi <- df_multi_labour$speech_stand+df_multi_labour$subm_stand

#long to wide: one row per repair_seq_id
df_multi_labour <- spread(df_multi_labour, repair_part, multi) 
df_multi_labour <- df_multi_labour %>% select(pairnr, target, repair_seq_id, T0_type, Tmin1:Tplus1) %>%group_by(pairnr, target, repair_seq_id, T0_type) %>% summarise_if(is.numeric, list(~sum(., na.rm=T)))
#compute relative labour T0 vs Tplus1
df_multi_labour$multi_T0_prop <- df_multi_labour$T0 / (df_multi_labour$T0 + df_multi_labour$Tplus1)
df_multi_labour$multi_Tplus1_prop <- df_multi_labour$Tplus1 / (df_multi_labour$T0 + df_multi_labour$Tplus1)
#rename cols
df_multi_labour <- df_multi_labour %>%
  rename(multi_Tmin1 = Tmin1, multi_T0 = T0, multi_Tplus1 = Tplus1)

#merge
df_div_labour <- merge(df_speech_labour, df_gesture_labour, by=c("pairnr", "target", "repair_seq_id", "T0_type")) %>%
                 merge(df_multi_labour, by=c("pairnr", "target", "repair_seq_id", "T0_type"))%>%
                select(pairnr, target, repair_seq_id, T0_type, 
                       speech_Tmin1, speech_T0, speech_Tplus1, 
                       speech_T0_prop, speech_Tplus1_prop,
                       gesture_subm_Tmin1, gesture_subm_T0, gesture_subm_Tplus1, 
                       gesture_subm_T0_prop, gesture_subm_Tplus1_prop,
                       multi_Tmin1, multi_T0, multi_Tplus1, 
                       multi_T0_prop, multi_Tplus1_prop)

#convert multi_Tmin1 0 to NA if speech_Tmin1 is NA
for (x in 1:nrow(df_div_labour)){
  if (is.na(df_div_labour[x,]$speech_Tmin1)) {df_div_labour[x,]$multi_Tmin1 <- NA}
}


#add information on how much of the total effort per repair sequence is gestural
df_div_labour$percent_of_total_effort_is_gesture <- 
  (scale(df_div_labour$gesture_subm_T0, center=F, scale=T)+scale(df_div_labour$gesture_subm_Tplus1, center=F, scale=T)) / #gesture effort divided by
  (scale(df_div_labour$gesture_subm_T0, center=F, scale=T)+scale(df_div_labour$gesture_subm_Tplus1, center=F, scale=T)+ #gesture effort
  scale(df_div_labour$speech_T0, center=F, scale=T)+scale(df_div_labour$speech_Tplus1, center=F, scale=T)) # + speech effort
    

```

```{r}
write.csv2(df, "processing_output/6_final_df_main.csv", row.names=F)
write.csv2(df_div_labour, "processing_output/6_final_df_division_labour.csv", row.names=F)
```


```{r}
sessionInfo()
```

R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19044)

Matrix products: default

locale:
[1] LC_COLLATE=Dutch_Netherlands.1252  LC_CTYPE=Dutch_Netherlands.1252    LC_MONETARY=Dutch_Netherlands.1252 LC_NUMERIC=C                      
[5] LC_TIME=Dutch_Netherlands.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] magrittr_1.5    tidyr_1.1.0     cowplot_1.0.0   ggplot2_3.3.5   dplyr_1.0.0     rstudioapi_0.11 pracma_2.3.3   

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.7       pillar_1.6.4     compiler_4.0.2   tools_4.0.2      digest_0.6.25    evaluate_0.14    lifecycle_1.0.1  tibble_3.1.6    
 [9] gtable_0.3.0     pkgconfig_2.0.3  rlang_0.4.11     cli_2.0.2        yaml_2.2.1       xfun_0.14        withr_2.2.0      knitr_1.28      
[17] generics_0.1.1   vctrs_0.3.8      grid_4.0.2       tidyselect_1.1.0 glue_1.4.1       R6_2.4.1         fansi_0.4.1      rmarkdown_2.2   
[25] purrr_0.3.4      scales_1.1.1     ellipsis_0.3.2   htmltools_0.4.0  assertthat_0.2.1 colorspace_1.4-1 utf8_1.1.4       tinytex_0.23    
[33] munsell_0.5.0    crayon_1.3.4  