---
title: "S4 - Discrete RSSI Correlations with self-reported friendship and advice-seeking"
output:
  html_document:
    theme: cerulean
    number_sections: true
---

```{r setup, include=FALSE, echo=FALSE, message=FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  echo = FALSE, 
  message = FALSE, 
  warning = FALSE,
  comment = "#>"
)

rm(list=ls())
gc()


# CRAN libraries 
library(cowplot)
library(cocor)
library(network)
library(tidyverse)
library(magrittr)
library(RColorBrewer)

# Download and install from Github https://github.com/jmueller17/sociometrics
library(sociometrics)

# Download and install from Zenodo https://doi.org/10.5281/zenodo.3446071
library(gediismtrx)
data("gedii_sociodemo_fix")
data("gedii_rr")
data("gedii_interaction")

source("S4_utility.fun.R")


rssi_range <- c(-53:-91) #indicating levels included 

teams <- c(1:6, 8)
str_teams <- c(paste0("Team_", teams), "Team_7A", "Team_7B")

# use only bluetooth from indicated teams
bt <- gedii_interaction %>%
  dplyr::filter(Team %in% str_teams & !is.na(RSSI) & RSSI %in% rssi_range ) %>%
  mreverse(cols=c("Badge.ID", "Other.ID"), into="edge")

```



# Correlations for discrete RSSI levels


## All teams 
All teams are used. Interpolation of round-robin is "mean" for friends, "sum" for advice and "sum" for psychological safety. 

```{r}

disc.all <- corrtable("disc", bt, teams=c(1:6, 8, 71, 72), to.undir=c("mean", "sum"), impute.na="recip")

disc.all %<>% 
  mutate(org = "All teams",
         rho = round(rho,3),
         p = round(p,3))

disc.all
#print(flextable(disc.all), preview="docx")
```


```{r fig.width=6, fig.height=5}
g1.soc <- disc.all %>% 
  filter(type == "soc") %>%
  ggplot(aes(x=rssi, y=rho)) + 
  geom_line(alpha=.4) + 
  geom_point() + 
  scale_y_continuous(limits=c(0,0.55), position="right") +
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", y="Spearman Rho") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"))

```


```{r fig.width=6, fig.height=5}
g1.adv <- disc.all %>% 
  filter(type == "adv") %>% 
  ggplot(aes(x=rssi, y=rho)) + 
  geom_line(alpha=.4) + 
  geom_point() + 
  scale_y_continuous(limits=c(-0.01,0.55), position="right") +
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", y="Spearman Rho") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"))

```

Correlation coefficients for (A) friendship and (B) advice-seeking across all teams at discrete RSSI levels
```{r fig.width=12, fig.height=4.5}
cowplot::plot_grid(g1.soc, g1.adv, labels=c("A", "B"))
```

<p>&nbsp;</p>
<p>&nbsp;</p>



## Organizational embedding 

### Univeristy (T1, T3, T6)
```{r}
teams <- c(1,3,6)

str_teams <- paste0("Team_", teams)

bt.uni <- bt %>%  
  filter(Team %in% str_teams) 

disc.uni <- corrtable("disc", bt.uni, teams, to.undir=c("mean", "sum"), impute.na="recip")

disc.uni %<>% 
  mutate(org = "University",
         rho = round(rho,3),
         p = round(p,3))

disc.uni
```

<p>&nbsp;</p>

### Research center (T2, T4, T5)
```{r}

teams <- c(2,4,5)

str_teams <- paste0("Team_", teams)

bt.labs <- bt %>%  
  filter(Team %in% str_teams) 

disc.labs <- corrtable("disc", bt.labs, teams, to.undir=c("mean", "sum"), impute.na="recip")

disc.labs %<>% 
  mutate(org = "Research labs",
         rho = round(rho,3),
         p = round(p,3))

disc.labs
```

<p>&nbsp;</p>


### Private company (T7.1, T7.2, T8)
```{r}

bt.pri <- bt %>%  
  filter(Team %in% c("Team_7A", "Team_7B", "Team_8")) 

disc.priv <- corrtable("disc", bt.pri, c(71,72,8), to.undir=c("mean", "sum"), impute.na="recip")

disc.priv %<>% 
  mutate(org = "Private Company",
         rho = round(rho,3),
         p = round(p,3))

disc.priv
```

<p>&nbsp;</p>


Correlation coefficients of (A) friendship and (B) advice-seeking scores with BT detections at discrete RSSI levels by organizational context

```{r fig.width=12, fig.height=4.5}

disc.org <- bind_rows(disc.uni, disc.labs, disc.priv, disc.all)

g1 <- disc.org %>% 
  filter(type == "soc") %>% 
  ggplot(aes(x=rssi, y=rho, colour=org)) +
    geom_line(alpha=.4) +
    geom_point() + 
  scale_y_continuous(limits=c(-.25,0.65), position="right") +
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  scale_colour_manual(values=c("#d3d3d3", "#1f9bde", "#de1f9b", "#9bde1f"))  +
  labs(colour="Type of organization", x="RSSI", y="Spearman Rho") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        legend.position="bottom", legend.title = element_blank())


g2<- disc.org %>% 
  filter(type == "adv") %>% 
  ggplot(aes(x=rssi, y=rho, colour=org)) +
    geom_line(alpha=.4) +
    geom_point() + 
  scale_y_continuous(limits=c(-.25,0.65), position="right") +
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  scale_colour_manual(values=c("#d3d3d3", "#1f9bde", "#de1f9b", "#9bde1f"))  +
  labs(colour="Type of organization", x="RSSI", y="Spearman Rho") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        legend.position="bottom", legend.title = element_blank())

cowplot::plot_grid(g1, g2, ncol = 2, labels=c("A", "B"))

```




<p>&nbsp;</p>
<p>&nbsp;</p>


## Cocor tests - Friendship 

 
### Friends. University - Research Labs
```{r}
tmp1.1 <- lapply(rssi_range, compare.cor, bt, c(1,3,6), c(2,4,5), type="m_soc", to.undir="mean", impute.na="recip", ctype="disc")

rs_comp1.1 <- bind_rows(tmp1.1)

rs_comp1.1 %<>%
  mutate(corgrp1 = round(corgrp1,4),
         corgrp2 = round(corgrp2,4),
         confintL = round(confintL, 4),
         confintR = round(confintR, 4),
         confint = paste0("[",confintL, ", ", confintR, "]"),
         pval = round(pval,4),
         diff = round(diff,4)) %>% 
  select(rssi, corgrp1, corgrp2, diff, confint, pval)

rs_comp1.1
#print(flextable(rs_comp1.1), preview="docx")
```

```{r fig.width=6, fig.height=4}

g.rs1.1 <- rs_comp1.1 %>% 
  mutate(diff2 = diff, 
         corgrp11 = corgrp1,
         corgrp22 = corgrp2,
         Significant=if_else(pval<=.05, T,F)) %>% 
  gather(key="type", value="coef", corgrp1, corgrp2) %>% 
  ggplot(aes(x=rssi, y=coef, color=type)) + 
  geom_segment(aes(x=rssi, xend=rssi, y=corgrp11, yend=corgrp22, color=Significant), size=1.1) +
  geom_point() +
  scale_color_manual(values=c( "#9bde1f", "#de1f9b", "#f0f0f0", "#0d0d0d"),
    labels=c("University", "Research Labs", "Difference p > .05", "Difference p < .05")) + 
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", y="Correlation", colour="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        legend.position="bottom") 
```


<p>&nbsp;</p>


### Friends. University - Private Comapny 
```{r}
tmp1.2 <- lapply(rssi_range, compare.cor, bt, c(1,3,6), c(71,72,8), type="m_soc", to.undir="mean", impute.na="recip", ctype="disc")

rs_comp1.2 <- bind_rows(tmp1.2)

rs_comp1.2 %<>%
  mutate(corgrp1 = round(corgrp1,4),
         corgrp2 = round(corgrp2,4),
         confintL = round(confintL, 4),
         confintR = round(confintR, 4),
         confint = paste0("[",confintL, ", ", confintR, "]"),
         pval = round(pval,4),
         diff = round(diff,4)) %>% 
  select(rssi, corgrp1, corgrp2, diff, confint, pval)

rs_comp1.2
```

```{r fig.width=6, fig.height=4}

g.rs1.2 <- rs_comp1.2 %>% 
  mutate(diff2 = diff, 
         corgrp11 = corgrp1,
         corgrp22 = corgrp2,
         Significant=if_else(pval<=.05, T,F)) %>% 
  gather(key="type", value="coef", corgrp1, corgrp2) %>% 
  ggplot(aes(x=rssi, y=coef, color=type)) + 
  geom_segment(aes(x=rssi, xend=rssi, y=corgrp11, yend=corgrp22, color=Significant), size=1.1) +
  geom_point() +
  scale_color_manual(values=c("#9bde1f", "#1f9bde", "#e0e0e0", "#0d0d0d"), 
    labels=c("University", "Private Company", "Difference p > .05", "Difference p < .05")) + 
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", 
       y="Correlation", 
       colour="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        legend.position="bottom") 
```


<p>&nbsp;</p>


### Friends. Research Labs - Private Company 
```{r}
tmp1.3 <- lapply(rssi_range, compare.cor, bt, c(2,4,5), c(71,72,8), type="m_soc", to.undir="mean", impute.na="recip", ctype="disc")

rs_comp1.3 <- bind_rows(tmp1.3)


rs_comp1.3 %<>%
    mutate(corgrp1 = round(corgrp1,4),
         corgrp2 = round(corgrp2,4),
         confintL = round(confintL, 4),
         confintR = round(confintR, 4),
         confint = paste0("[",confintL, ", ", confintR, "]"),
         pval = round(pval,4),
         diff = round(diff,4)) %>% 
  select(rssi, corgrp1, corgrp2, diff, confint, pval)

rs_comp1.3
```

```{r fig.width=6, fig.height=4}

g.rs1.3 <- rs_comp1.3 %>% 
  mutate(diff2 = diff, 
         corgrp11 = corgrp1,
         corgrp22 = corgrp2,
         Significant=if_else(pval<=.05, T,F)) %>% 
  gather(key="type", value="coef", corgrp1, corgrp2) %>% 
  ggplot(aes(x=rssi, y=coef, color=type)) + 
  geom_segment(aes(x=rssi, xend=rssi, y=corgrp11, yend=corgrp22, color=Significant), size=1.1) +
  geom_point() +
  scale_color_manual(values=c( "#de1f9b", "#1f9bde", "#e0e0e0", "#0d0d0d"), 
      labels=c("Research Labs", "Private Company", "Difference p > .05", "Difference p < .05")) + 
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", 
       y="Correlation", colour="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        legend.position="bottom") 

```

<p>&nbsp;</p>

Discriminant validity. Significant differences between organizational contexts for discrete BT detections and friendship scores. 
```{r fig.width=20, fig.height=4}
cowplot::plot_grid(g.rs1.1, g.rs1.2, g.rs1.3, ncol = 3)
```

<p>&nbsp;</p>
<p>&nbsp;</p>


## Cocor tests - Advice seeking  


### Advice. University - Research labs 
```{r}

tmp2.1 <- lapply(rssi_range, compare.cor, bt, c(1,3,6), c(2,4,5), type="m_adv", to.undir="sum", impute.na="recip", ctype="disc")

rs_comp2.1 <- bind_rows(tmp2.1)

rs_comp2.1 %<>%
    mutate(corgrp1 = round(corgrp1,4),
         corgrp2 = round(corgrp2,4),
         confintL = round(confintL, 4),
         confintR = round(confintR, 4),
        confint = paste0("[",confintL, ", ", confintR, "]"),
         pval = round(pval,4),
         diff = round(diff,4)) %>% 
  select(rssi, corgrp1, corgrp2, diff, confint, pval)

rs_comp2.1
```

```{r fig.width=6, fig.height=4}

g.rs2.1 <- rs_comp2.1 %>% 
  mutate(diff2 = diff, 
         corgrp11 = corgrp1,
         corgrp22 = corgrp2,
         Significant=if_else(pval<=.05, T,F)) %>% 
  gather(key="type", value="coef", corgrp1, corgrp2) %>% 
  ggplot(aes(x=rssi, y=coef, color=type)) + 
  geom_segment(aes(x=rssi, xend=rssi, y=corgrp11, yend=corgrp22, color=Significant), size=1.1) +
  geom_point() +
  scale_color_manual(values=c( "#9bde1f", "#de1f9b", "#f0f0f0", "#0d0d0d"),
    labels=c("University", "Research Labs", "Difference p > .05", "Difference p < .05")) + 
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", y="Correlation", colour="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        legend.position="bottom") 
```

<p>&nbsp;</p>


### Advice. University - Private Company 
```{r}
tmp2.2 <- lapply(rssi_range, compare.cor, bt, c(1,3,6), c(71,72,8), type="m_adv", to.undir="sum", impute.na="recip", ctype="disc")

rs_comp2.2 <- bind_rows(tmp2.2)


rs_comp2.2 %<>%
    mutate(corgrp1 = round(corgrp1,4),
         corgrp2 = round(corgrp2,4),
         confintL = round(confintL, 4),
         confintR = round(confintR, 4),
         confint = paste0("[",confintL, ", ", confintR, "]"),
         pval = round(pval,4),
         diff = round(diff,4)) %>% 
  select(rssi, corgrp1, corgrp2, diff, confint, pval)

rs_comp2.2

```

```{r fig.width=6, fig.height=4}

g.rs2.2 <- rs_comp2.2 %>% 
  mutate(diff2 = diff, 
         corgrp11 = corgrp1,
         corgrp22 = corgrp2,
         Significant=if_else(pval<=.05, T,F)) %>% 
  gather(key="type", value="coef", corgrp1, corgrp2) %>% 
  ggplot(aes(x=rssi, y=coef, color=type)) + 
  geom_segment(aes(x=rssi, xend=rssi, y=corgrp11, yend=corgrp22, color=Significant), size=1.1) +
  geom_point() +
  scale_color_manual(values=c("#9bde1f", "#1f9bde", "#e0e0e0", "#0d0d0d"), 
    labels=c("University", "Private Company", "Difference p > .05", "Difference p < .05")) + 
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", y="Correlation", colour="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        legend.position="bottom") 

```

<p>&nbsp;</p>


### Advice. Research Labs - Private Company 
```{r}
tmp2.3 <- lapply(rssi_range, compare.cor, bt, c(2,4,5), c(71,72,8), type="m_adv", to.undir="sum", impute.na="recip")

rs_comp2.3 <- bind_rows(tmp2.3)

rs_comp2.3 %<>%
    mutate(corgrp1 = round(corgrp1,4),
         corgrp2 = round(corgrp2,4),
         confintL = round(confintL, 4),
         confintR = round(confintR, 4),
         confint = paste0("[",confintL, ", ", confintR, "]"),
         pval = round(pval,4),
         diff = round(diff,4)) %>% 
  select(rssi, corgrp1, corgrp2, diff, confint, pval)


rs_comp2.3
```

```{r fig.width=6, fig.height=4}

g.rs2.3 <- rs_comp2.3 %>% 
  mutate(diff2 = diff, 
         corgrp11 = corgrp1,
         corgrp22 = corgrp2,
         Significant=if_else(pval<=.05, T,F)) %>% 
  gather(key="type", value="coef", corgrp1, corgrp2) %>% 
  ggplot(aes(x=rssi, y=coef, color=type)) + 
  geom_segment(aes(x=rssi, xend=rssi, y=corgrp11, yend=corgrp22, color=Significant), size=1.1) +
  geom_point() +
  scale_color_manual(values=c( "#de1f9b", "#1f9bde", "#e0e0e0", "#0d0d0d"), 
      labels=c("Research Labs", "Private Company", "Difference p > .05", "Difference p < .05")) + 
  scale_x_continuous(breaks=c(-53, -60, -70,-80, -91)) +
  labs(x="RSSI", y="Correlation", colour="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(), 
        legend.position="bottom") 
```


<p>&nbsp;</p>

Discriminant validity. Significant differences between organizational contexts for discrete BT detections and advice seeking scores. 
```{r fig.width=20, fig.height=4}
cowplot::plot_grid(g.rs2.1, g.rs2.2, g.rs2.3, ncol=3, labels=c("A", "B", "C"))
```


<p>&nbsp;</p>
<p>&nbsp;</p>


