---
title: "S4 - Reference Distributions"
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(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")

# set to true will carry out the simulation, which takes a long time. If set to F
# loads simulated data. 
do_simulation = F

# number of random reshuffles of simulation
reps = 1000

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

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

# use only bluetooth from indicated teams and rssi values
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")

```



# Simulated reference distribution

Comparison of simulated reference distribution with observed distribution of correlation coefficients between BT detections and self-reported friendship and advice-seeking measures. Quantiles (vertical dotted lines) indicate .5% lower threshold and 99.5% upper threshold to observe rho values as extreme. 


```{r warning=F, message=F}

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

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

if (do_simulation){

  disc.rdis.1000 <- refdistribution(reps, "disc", bt, teams=c(1:6, 8, 71, 72), to.undir=c("mean", "sum"), impute.na="recip")
  disc.rdis.1000$datatype <- "Simulated"
  
  disc.uni.rdis.1000 <- refdistribution(reps, "disc", bt, teams=c(1,3,6), to.undir=c("mean", "sum"), impute.na="recip")
  disc.uni.rdis.1000$datatype <- "Simulated"
  
  disc.lab.rdis.1000 <- refdistribution(reps, "disc", bt, teams=c(2,4,5), to.undir=c("mean", "sum"), impute.na="recip")
  disc.lab.rdis.1000$datatype <- "Simulated"
  
  disc.priv.rdis.1000 <- refdistribution(reps, "disc", bt, teams=c(71,72,8), to.undir=c("mean", "sum"),impute.na="recip")
  disc.priv.rdis.1000$datatype <- "Simulated"
  
  save(disc.rdis.1000, disc.uni.rdis.1000, disc.lab.rdis.1000, disc.priv.rdis.1000, file="S4_disc_3JUN2020_rdis53_91.Rdata")

} else { 
    load("S4_disc_3JUN2020_rdis53_91.Rdata")
    
}


# bind df together for df of all teams. 
disc.comp <- bind_rows(disc.all, disc.rdis.1000)

# calculate quantiles of simulated distributions
quant.all <- disc.comp %>% 
  filter(datatype == "Simulated") %>% 
  group_by(type) %>% 
  summarize(q005 = quantile(rho, .005), 
            q025 = quantile(rho, .025), 
            q975 = quantile(rho, .975), 
            q995 = quantile(rho, .995), 
            org = "all")
```




## All teams (rho).


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

g.comp.soc <- disc.comp %>% 
  filter(type == "soc") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  scale_x_continuous(limits=c(-.4, .8), breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  geom_vline(aes(xintercept = quant.all[[2, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.all[[2, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_fill_manual(values=c("#2171B5","#DEEBF7")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")


g.comp.adv <- disc.comp %>% 
  filter(type == "adv") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  geom_vline(aes(xintercept = quant.all[[1, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.all[[1, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_x_continuous(limits=c(-.4, .8), breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  scale_fill_manual(values=c("#CB181D", "#FEE0D2")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")

cowplot::plot_grid(g.comp.soc, g.comp.adv, ncol=2, labels=c("A) Friendship", "B) Advice"))
```



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



## Universities (rho)

```{r warning=F, message=F}
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), 
         datatype = "Observed")

disc.comp.uni <- bind_rows(disc.uni, disc.uni.rdis.1000)

quant.uni <- disc.comp.uni %>% 
  filter(datatype == "Simulated") %>% 
  group_by(type) %>% 
  summarize(q005 = quantile(rho, .005), 
            q025 = quantile(rho, .025), 
            q975 = quantile(rho, .975), 
            q995 = quantile(rho, .995), 
            org = "uni")
```

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

g.comp.uni.soc <- disc.comp.uni %>% 
  filter(type == "soc") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  geom_vline(aes(xintercept = quant.uni[[2, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.uni[[2, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_x_continuous(limits=c(-.5, .8),  breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  scale_fill_manual(values=c("#2171B5","#DEEBF7")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")



g.comp.uni.adv <- disc.comp.uni %>% 
  filter(type == "adv") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  geom_vline(aes(xintercept = quant.uni[[1, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.uni[[1, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_x_continuous(limits=c(-.5, .8), breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  scale_fill_manual(values=c("#CB181D", "#FEE0D2")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")

cowplot::plot_grid(g.comp.uni.soc, g.comp.uni.adv, ncol=2, labels=c("A) Friendship", "B) Advice"))
  
```



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


## Research centers (rho)

```{r warning=F, message=F}

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), 
         datatype = "Observed")

disc.comp.labs <- bind_rows(disc.labs, disc.lab.rdis.1000)

quant.labs <- disc.comp.labs %>% 
  filter(datatype == "Simulated") %>% 
  group_by(type) %>% 
  summarize(q005 = quantile(rho, .005), 
            q025 = quantile(rho, .025), 
            q975 = quantile(rho, .975), 
            q995 = quantile(rho, .995), 
            org = "labs")
```


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

g.comp.labs.soc <- disc.comp.labs %>% 
  filter(type == "soc") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  geom_vline(aes(xintercept = quant.labs[[2, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.labs[[2, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_x_continuous(limits=c(-.5, .6),  breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  scale_fill_manual(values=c("#2171B5","#DEEBF7")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")



g.comp.labs.adv <- disc.comp.labs %>% 
  filter(type == "adv") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  geom_vline(aes(xintercept = quant.labs[[1, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.labs[[1, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_x_continuous(limits=c(-.5, .6),  breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  scale_fill_manual(values=c("#CB181D", "#FEE0D2")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")

cowplot::plot_grid(g.comp.labs.soc, g.comp.labs.adv, ncol=2, labels=c("A) Friendship", "B) Advice"))
  
```

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


## Private company (rho)

```{r warning=F, message=F}

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), 
         datatype = "Observed")

disc.comp.priv <- bind_rows(disc.priv, disc.priv.rdis.1000)

quant.priv <- disc.comp.priv %>% 
  filter(datatype == "Simulated") %>% 
  group_by(type) %>% 
  summarize(q005 = quantile(rho, .005), 
            q025 = quantile(rho, .025), 
            q975 = quantile(rho, .975), 
            q995 = quantile(rho, .995), 
            org = "priv")
```


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

g.comp.priv.soc <- disc.comp.priv %>% 
  filter(type == "soc") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  geom_vline(aes(xintercept = quant.priv[[2, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.priv[[2, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_x_continuous(limits=c(-0.5, .8), breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  scale_fill_manual(values=c("#2171B5","#DEEBF7")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")



g.comp.priv.adv <- disc.comp.priv %>% 
  filter(type == "adv") %>% 
  ggplot(aes(x=rho, fill=datatype)) + 
  geom_density(alpha=0.7) +
  geom_vline(aes(xintercept = quant.priv[[1, "q005"]]), color=c("#626262"), linetype="dotdash") +
  geom_vline(aes(xintercept = quant.priv[[1, "q995"]]), color=c("#626262"), linetype="dotdash") +
  scale_x_continuous(limits=c(-0.5, .8), breaks=c(-.5, -.25, 0, .25, .5, .75)) +
  scale_fill_manual(values=c("#CB181D", "#FEE0D2")) +
  labs(x="rho", y="density", fill="") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position="bottom")

cowplot::plot_grid(g.comp.priv.soc, g.comp.priv.adv, ncol=2, labels=c("A) Friendship", "B) Advice"))
  
```

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


### Quantiles of simulated reference distribution

```{r}
quant <- bind_rows(quant.all, quant.uni, quant.labs, quant.priv) %>% 
  select(org, type, q005, q025, q975, q995)

quant
#print(flextable(quant), preview="docx")
```



<p>&nbsp;</p>
<p>&nbsp;</p>
# Package- and Session Info 
```{r}
sessionInfo()
```





