---
title: "S4 - Descriptive Statistics - Self-reported measures"
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")
data("gedii_bm_activity")

source("S4_utility.fun.R")


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

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

# get all teams, using all RSSI levels. 
bt <- gedii_interaction %>%
  filter(Team %in% str_teams & !is.na(RSSI) & RSSI %in% rssi_range) %>% 
  mreverse(cols=c("Badge.ID", "Other.ID"), into="edge")

# assign organizational type to each team 
bt$org <- sapply(bt$Team, team2org)

# get ratings matrix 
teams <- c(1:6,71,72,8)

friends <- bind_rows(lapply(teams, comb.rr, "m_soc", directed=F, to.undir="mean", impute.na="recip"))
advice <-  bind_rows(lapply(teams, comb.rr, "m_adv", directed=F, to.undir="sum", impute.na="recip"))

friends$type <- "Friendship" 
advice$type <- "Advice"

rating <- bind_rows(friends, advice)
rating$weight <- as.numeric(rating$weight)

# assign organizational type to each team 
rating$org <- sapply(rating$Team, team2org)


# split groups according to rating high versus low 
bt.hilo <- hilow.bt(bt, rating)

# total hours visible approx. 8h x 5 working days. 
gmin <- 5 * 8

```


# BT stats per organizational context

```{r}
bt.stats <- bt.hilo %>% 
  group_by(org) %>% 
  summarize(bt.count = n(),
            edges = length(unique(edge)),
            bt.avgXdyad = round((bt.count/edges),0),
            bt.dyadXhour = round((bt.avgXdyad/gmin), 0),
            mean.rssi = round(mean(RSSI, na.rm = T), 2),
            median.rssi = median(RSSI, na.rm = T)) %>% 
  arrange(desc(org))

bt.stats
```




# High versus low self-reported groups


## BT statistics across all teams

```{r}

bt.hilo.stats <- bt.hilo %>%
  group_by(rating) %>% 
  summarize(bt.count = n(),
            edges = length(unique(edge)),
            bt.avgXdyad = round((bt.count/edges),0),
            bt.dyadXhour = round((bt.avgXdyad/gmin), 0),
            mean.rssi = round(mean(RSSI, na.rm = T), 2),
            median.rssi = median(RSSI, na.rm = T)) %>% 
  arrange(rating)

bt.hilo.stats

```



## BT statistics by organizational context

```{r}

bt.hilo.stats.org <- bt.hilo %>%
  group_by(org, rating) %>% 
  summarize(bt.count = n(),
            edges = length(unique(edge)),
            bt.avgXdyad = round((bt.count/edges),0),
            bt.dyadXhour = round((bt.avgXdyad/gmin), 0),
            mean.rssi = round(mean(RSSI, na.rm = T), 2),
            median.rssi = median(RSSI, na.rm = T)) %>% 
  arrange(desc(org), rating)

bt.hilo.stats.org

```




```{r eval=F}
library(flextable)
library(officer)

ftb1 <- flextable(bt.hilo.stats)
ftb2 <- flextable(bt.stats)

print(ftb2, preview = "docx") # opens microsoft word 
```



## Boxplot High versus low rating groups

```{r}
bt.all <- bt.hilo %>% 
  mutate(org = "All teams")

bt.all <- bind_rows(bt.hilo, bt.all)
```


```{r fig.width=9, fig.height=4.5}
bt.all %>% 
  ggplot(aes(x=org, y=RSSI, fill=rating)) + 
  geom_boxplot() +
  scale_fill_manual(values=c("#CB181D", "#FB6A4A","#2171B5","#6BAED6" )) +
  labs(x="", y="RSSI") +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"), legend.position = "right", legend.title=element_blank())
  
  
```



# Descriptive statistics self-reported measures


## Boxplots by organizational context

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

g4 <- rating %>% 
  filter(type == "Friendship") %>% 
  ggplot(aes(x=org, y=weight, fill=org)) + 
  geom_boxplot(width=.5) +
  stat_summary(fun.y="mean", geom="point", shape=23, fill="black") +
  scale_y_continuous(limits=c(0, 5), breaks=c(0:5)) +
  scale_fill_manual(values=c("#1f9bde","#de1f9b", "#9bde1f"))  +
  labs(y="Friendship rating", x="") +
  guides(fill=F) +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"))

g5 <- rating %>% 
  filter(type == "Advice") %>% 
  ggplot(aes(x=org, y=weight, fill=org)) + 
  geom_boxplot(width=.5) +
  stat_summary(fun.y="mean", geom="point", shape=23, fill="black") +
  scale_y_continuous(limits=c(0, 10), breaks=c(0:10)) +
  scale_fill_manual(values=c("#1f9bde","#de1f9b", "#9bde1f"))  +
  labs(y="Advice rating", x="") +
  guides(fill=F) +
  theme_bw() + 
  theme(plot.margin=unit(c(1.2,1,0,1),"cm"))


cowplot::plot_grid(g4,g5, ncol=2, labels=c("A", "B"))
```


## Frequency plots of self-reported friendship/advice seeking scores 

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


g.soc.frq <- rating %>% 
  filter(type == "Friendship") %>% 
  ggplot(aes(x=weight, fill=org)) + 
  geom_bar(stat="count", position="stack", alpha=1) +
  scale_fill_manual(values=c("#1f9bde","#de1f9b", "#9bde1f"))  +
    labs(guide=F, x="Friendship score", y="Count") +
    theme_bw() + 
    theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
          legend.position="bottom", 
          legend.title=element_blank())

g.soc.dens <- rating %>% 
  filter(type == "Friendship") %>% 
  ggplot(aes(x=weight, color=org)) + 
  geom_line(stat="density", alpha=1) + 
  scale_color_manual(values=c("#1f9bde","#de1f9b", "#9bde1f"))  +
  scale_x_continuous(limits=c(1, 5), breaks=c(1:5)) +
    labs(guide=F, x="Friendship score", y="Density") +
    theme_bw() + 
    theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
          legend.position="bottom", 
          legend.title=element_blank())


g.adv.frq <- rating %>% 
  filter(type == "Advice") %>% 
  ggplot(aes(x=weight, fill=org)) + 
  geom_bar(stat="count", position="stack", alpha=1) +
  scale_fill_manual(values=c("#1f9bde","#de1f9b", "#9bde1f"))  +
    labs(guide=F, x="Advice score", y="Count") +
    theme_bw() + 
    theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
          legend.position="bottom", 
          legend.title=element_blank())



g.adv.dens <- rating %>% 
  filter(type == "Advice") %>% 
  ggplot(aes(x=weight, color=org)) + 
  geom_line(stat="density", alpha=1) + 
  scale_color_manual(values=c("#1f9bde","#de1f9b", "#9bde1f"))  +
    labs(guide=F, x="Advice score", y="Density") +
    theme_bw() + 
    theme(plot.margin=unit(c(1.2,1,0,1),"cm"), 
          legend.position="bottom", 
          legend.title=element_blank())




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



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


