---
output: pdf_document
---

### In technical appendix 1, we use data wrangling techniques to clean and tranform the data. In technical appendix 2, we built a function to obtain a GGM plot. Just  as a sanity check, in this R file, we build GGM plot where initial opportunities is 2 using other forms of code. 

```{r setup}
knitr::opts_chunk$set(echo = TRUE)
model_2_factor <- read.csv("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/Modeling_Two_Step_Expressions_2019-2020/worksheet_grapher_a1_patterns_2step_expr_1920_datashop.txt", header = TRUE, sep = "\t")
scale_factor <- read.csv("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/ Using_Scale_Factor_2019-2020/scale_drawings_3_1920_datashop.txt", header = TRUE, sep = "\t")
analyze_2_factor <- read.csv("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/ Analyzing_Models_of_Two-Step_Linear_Relationships_2019-2020/analyzing_models_2step_integers_datashop.txt", header = TRUE, sep = "\t")
library(tidyverse)
```

```{r}
scale_factor_2 <- scale_factor %>%
  select(Time, Anon.Student.Id, Level..Workspace.Id., Problem.Name, Step.Name,
         Outcome, Attempt.At.Step, KC.Model.MATHia.) %>%
  filter(KC.Model.MATHia. != "") %>%
  mutate(Workspace = "C", Outcome_01 = ifelse(Outcome == "OK", 1, 0))
model_2_factor_2 <- model_2_factor %>%
  select(Time, Anon.Student.Id, Level..Workspace.Id., Problem.Name, Step.Name,
         Outcome, Attempt.At.Step, KC.Model.MATHia.) %>%
  filter(KC.Model.MATHia. != "") %>%
  mutate(Workspace = "B", Outcome_01 = ifelse(Outcome == "OK", 1, 0))
analyze_2_factor_2 <- analyze_2_factor %>%
  select(Time, Anon.Student.Id, Level..Workspace.Id., Problem.Name, Step.Name,
         Outcome, Attempt.At.Step, KC.Model.MATHia.) %>%
  filter(KC.Model.MATHia. != "") %>%
  mutate(Workspace = "A", Outcome_01 = ifelse(Outcome == "OK", 1, 0))
```

```{r}
abc <- rbind(scale_factor_2, model_2_factor_2, analyze_2_factor_2)
```

```{r}
abc_2 <- abc %>%
  arrange(Anon.Student.Id, Time, Workspace, Step.Name) 
```

```{r}
abc_3 <- abc_2 %>%
  group_by(Anon.Student.Id, Workspace, Step.Name, KC.Model.MATHia.) %>%
  summarise(Success.Rate = mean(Outcome_01))
```

```{r}
abc_3.2 <- abc_3 %>%
  ungroup() %>%
  group_by(Anon.Student.Id, KC.Model.MATHia.) %>%
  slice(1:2)
```

```{r}
abc_3.2_2 <- abc_3.2 %>%
  summarise(Success.Rate.2 = mean(Success.Rate)) %>%
  select(Anon.Student.Id, KC.Model.MATHia., Success.Rate.2)
```

```{r}
abc_3.2_3 <- abc_3.2_2 %>%
  pivot_wider(names_from = KC.Model.MATHia., values_from = Success.Rate.2)
```

```{r}
abc_3.2_4 <- abc_3.2_3 %>%
  drop_na()
```

```{r}
abc_3.2_5 <- abc_3.2_4 %>%
  ungroup() %>%
  select(-Anon.Student.Id)
```

```{r}
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)))
}
abc_3.2_6 <- abc_3.2_5
for (i in colnames(abc_3.2_6)){
      abc_3.2_6[[i]] <- lapply(abc_3.2_6[[i]], logit)
}
for (i in colnames(abc_3.2_6)){
      abc_3.2_6[[i]] <- as.numeric(abc_3.2_6[[i]])
  }
```

```{r}
abc_pd <- psych::corFiml(abc_3.2_6)
```

```{r}
group_items <- list(
    A = c(10:16),
    B = c(5:9,17:20),
    C = c(1:4)
    )
qgraph::qgraph(
      abc_pd,
      layout = "spring",
      graph = "glasso",
      labels = TRUE,
      legend.cex = 0.40,
      tuning = 0.1,
      color = c("light blue", "light yellow", "light green"),
      groups = group_items,
      labels = TRUE,
      sampleSize = 10761,
      minimum = 0.05)
```



