---
title: "Supplementary Material 2: Correlation Matrices"
subtitle: "Measuring and assessing indeterminacy and variation in the morphology-syntax distinction"
author: 
  - Sandra Auderset
  - Adam J.R. Tallman
# date: "6/27/2021"
output: 
  pdf_document: 
    fig_caption: yes
    latex_engine: xelatex
    keep_tex: yes
---

```{r setup, include=FALSE}
library(knitr)
opts_chunk$set(tidy.opts=list(width.cutoff=80),tidy=TRUE, echo=TRUE)
```


In this file we provide the code as well as some additional explanation and plots of the correlation matrices (cf. Section 5 of the paper). 

## Data preparation

First, we load the necessary packages and set the seed to make the analysis fully reproducible (cf. the Rmd file for details).
```{r preamble, include=FALSE}
# load packages
library(groundhog)
packages <- c('here', 'tidyverse', 'lattice', 'GGally', 'ggpubr', 'MESS')
groundhog.library(packages, "2022-02-04")
```

```{r setseed}
# set seed
set.seed(200)
```

Next we load data (available at DOI), excluding columns we do not need for the analysis, i.e. we exclude the variables that are summarized in Exponence Complexity and certain meta information about the languages such as affiliation and glottocode.
```{r data}
db <- read_csv(here("SM1_Database.csv")) %>% select(Language, PMfixed:EXPcomplex)
glimpse(db)
```

For computing correlation matrices we have to convert all variables to a numeric version. We do this by coding more morphology-like values as 1 and more syntax-like values as 0. An overview by variable is provided in table XX in the paper.
We also rename the variables for better plotting and exclude columns not relevant for this analysis. 

```{r recoding}
db_corr <- db %>% 
  rename(EC = EXPcomplex) %>%
  mutate(Contiguity = as.integer(ifelse(INTERone=="y", 0, ifelse(INTERone=="n", 1, NA)))) %>%
  mutate(Simplex = as.integer(ifelse(CODelab=="y", 0, ifelse(CODelab=="n", 1, NA)))) %>%
  mutate(Fixed = as.integer(ifelse(PMfixed=="y", 1, ifelse(PMfixed=="n", 0, NA)))) %>%
  mutate(Bound = as.integer(ifelse(FREE=="y", 0, ifelse(FREE=="n", 1, NA)))) %>%
  mutate(NoStress = as.integer(ifelse(PRM=="y", 0, ifelse(PRM=="both", 1, ifelse(PRM=="n", 2, NA))))) %>%
  select(Language, EC:NoStress)
glimpse(db_corr)
```


## Correlation matrix across all languages

We compute a correlation matrix of all variables across all languages. We do this using Kendall's Tau and pairwise comparison and plot the result. 

```{r corrall}
cm_all <- db_corr %>% select(-Language) %>% 
  ggcorr(., method=c("pairwise", "kendall"), label=TRUE, hjust=0.5)
cm_all
```
```{r export, include=FALSE}
ggsave("corralllang.png", plot = cm_all, device = "png", width = 25, height = 20, units = "cm", dpi = 600)
```

## Correlation matrices per language

Next we compute one correlation matrix per language to inspect cross-linguistic differences. 
To do this efficiently, we use a custom function that takes one variable, namely the language, and generates the correlation matrix as well as the overall correlation coefficient. We apply it to all the languages of the sample.

```{r corrfun}
# function for correlation matrix and coefficient per language
corr.lang <- function(x){
  lang_corr <- db_corr %>% filter(Language==x) %>% select(-Language) %>%
    ggcorr(., method=c("pairwise", "kendall"), label="TRUE", hjust=0.7) + ggplot2::labs(title = x)
  mean_corr <- round(mean(lang_corr$data$coefficient), 2)
  returnlist <- list("corr_matrix" = lang_corr, "corr_coeff" = mean_corr)
}
# apply to each language and display plots
mov_corr <- corr.lang("Movima")
mov_corr
pui_corr <- corr.lang("Puinave")
pui_corr
tar_corr <- corr.lang("Tariana")
tar_corr
ash_corr <- corr.lang("AsheninkaPerene")
ash_corr
cha_corr <- corr.lang("Chacobo")
cha_corr
cav_corr <- corr.lang("Cavineña")
cav_corr
hup_corr <- corr.lang("Hup")
hup_corr
cay_corr <- corr.lang("CentralAlaskanYupik")
cay_corr
```

```{r export langplots, include=FALSE}
# arrange plots
corr_grid <- ggarrange(mov_corr$corr_matrix, pui_corr$corr_matrix, tar_corr$corr_matrix, ash_corr$corr_matrix, cha_corr$corr_matrix, cav_corr$corr_matrix, hup_corr$corr_matrix, cay_corr$corr_matrix,  ncol = 3, nrow = 3)
# export
ggsave("corrplots.png", plot = corr_grid, device = "png", width = 25, height = 20, units = "cm", dpi = 600)
```

