---
title: "Appendix 2"
output: pdf_document
---

```{r setup, include=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(echo = TRUE)
load("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/GGM-opp_data/ggm_function2.RData")
```

### In this technical appendix, we import the dataframe created in technical appendix 1 and build a function to build a Gaussian Graphical Model. The inputs to the function are `data_frame` (in the format shown in examples), `initial_opportunities` (example: 1,2,3,..), and `minimum_correlateion` (GGM would show lines having absolute value of partial correlation above this threshold).

### Loading packages.

```{r message=FALSE, warning=FALSE}
require(tidyverse)
require(qgraph)
require(xtable)
require(dplyr)
require(bootnet)
require(rstudioapi)
```

### Creating logit function.

```{r message=FALSE, warning=FALSE}
logit <- function(v){
  if (is.na(v)==TRUE){
    return(NA)
  }
  if (is.nan(v) == TRUE){
    return(NA)
  }
  if (v==1){
    v = 0.9999
  }
  if (v==0){
    v = 0.0001
  }
  return(log(v/(1-v)))
}
```

### Removing `Anon.Student.Id` from the dataset.

```{r message=FALSE, warning=FALSE}
ggm_opp <- tp_opp[-1]
```

### Function for creating GGM for KCs.

```{r message=FALSE, warning=FALSE}
ggm <- function(dat, num, min_cor = 0.03){
  success_initial_opp <- function(v){
      if(length(v) >= num){
        v1 <- as.numeric(v[1:num])
        success <- sum(v1)/num
        return(success)
      }
      else {v1 <- as.numeric(v)}
      success <- sum(v1)/length(v1)
      return(success)
    }
  opp_data_success_initial <- dat
  for (i in colnames(opp_data_success_initial)){
    opp_data_success_initial[[i]] <- 
      lapply(opp_data_success_initial[[i]], success_initial_opp)
  }
  new_opp_data_success_initial <- opp_data_success_initial
  for (i in colnames(new_opp_data_success_initial)){
      new_opp_data_success_initial[[i]] <- 
        lapply(new_opp_data_success_initial[[i]], logit)
  }
  for (i in 1:ncol(new_opp_data_success_initial)){
      new_opp_data_success_initial[[i]] <- 
        as.numeric(new_opp_data_success_initial[[i]])
  }
  new_opp_data_success_initial <- na.omit(new_opp_data_success_initial)
  data_success_initial_corr <- psych::corFiml(new_opp_data_success_initial)
  group_items <- list(
    A = c(6:12),
    B = c(1:5,17:20),
    C = c(13:16)
    )
  return(qgraph::qgraph(
      data_success_initial_corr,
      layout = "spring",
      graph = "glasso",
      labels = TRUE,
      legend.cex = 0.7,
      tuning = 0.1,
      color = c("light blue", "light yellow", "light green"),
      groups = group_items,
      labels = TRUE,
      sampleSize = nrow(new_opp_data_success_initial),
      minimum = min_cor
    ))
}
```

```{r include = FALSE,warning=FALSE, message=FALSE}
x2 <- ggm(dat = ggm_opp,num = 2, min_cor = 0.05)
x3 <- ggm(dat = ggm_opp,num = 3, min_cor = 0.05)
x4 <- ggm(dat = ggm_opp,num = 4, min_cor = 0.05)
x5 <- ggm(dat = ggm_opp,num = 5, min_cor = 0.05)
x6 <- ggm(dat = ggm_opp,num = 6, min_cor = 0.05)
```

### In the following plots, for the sake of easier visual comparison, we only show partial correlations that are larger than 0.05. In this way, only lines representing stronger partial correlations would be shown. 

### When the number of initial opportunities is 1, the GGM plot is as following: 

```{r,warning=FALSE,warning=FALSE}
x2 <- ggm(dat = ggm_opp,num = 1,min_cor = 0.05)
```

### When the number of initial opportunities is 2, the GGM plot is as following: 

```{r,warning=FALSE,warning=FALSE}
ggm(dat = ggm_opp,num = 2,min_cor = 0.05)
```

### When the number of initial opportunities is 3, the GGM plot is as following: 

```{r,warning=FALSE,warning=FALSE}
ggm(dat = ggm_opp,num = 3,min_cor = 0.05)
```

### When the number of initial opportunities is 4, the GGM plot is as following:  

```{r,warning=FALSE,warning=FALSE}
ggm(dat = ggm_opp,num = 4,min_cor = 0.05)
```

### When the number of initial opportunities is 5, the GGM plot is as following:  

```{r,warning=FALSE,warning=FALSE}
ggm(dat = ggm_opp,num = 5,min_cor = 0.05)
```

### When the number of initial opportunities is 6, the GGM plot is as following:  

```{r,warning=FALSE,warning=FALSE}
ggm(dat = ggm_opp,num = 6,min_cor = 0.05)
```

### When the number of initial opportunities is 7, the GGM plot is as following: 

```{r,warning=FALSE,warning=FALSE}
ggm(dat = ggm_opp,num = 7,min_cor = 0.05)
```

### As we can see, when the number of initial opportunities is 2, the partial correlations between workspace A and workspace B are the strongest. There are more lines between those two workspaces. Therefore, we chose 2 as our final cutoff point for initial opportunities.

```{r warning=FALSE, message=FALSE}
df2 <- data.frame(x2$Edgelist$from, x2$Edgelist$to, x2$Edgelist$weight)
df2 <- df2 %>%
  arrange(desc(x2$Edgelist$weight))
head(df2, n = 10)
```

