---
title: "Appendix 1"
output: pdf_document
---

```{r setup, include=FALSE, warning=FALSE, message=FALSE}
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)
```

### In this technical appendix, we use data wrangling techniques to merge the data on the three workspaces and create a dataframe which has success rate for opportunities for each student per KC.

```{r message=FALSE, warning=FALSE}
head(model_2_factor)
head(analyze_2_factor)
head(scale_factor)
```

### Removing observations which have blank KCs.

```{r message=FALSE, warning=FALSE}
new_scale_factor <- subset(scale_factor, KC.Model.MATHia. != "")
revised_new_scale_factor <- new_scale_factor %>%
  select(Anon.Student.Id, KC.Model.MATHia., Step.Name) %>%
  count(Anon.Student.Id, KC.Model.MATHia., Step.Name) 
```

### Transforming data by using `pivot_wider` to be able to compute correlations

```{r message=FALSE, warning=FALSE}
opp_scale_factor <- revised_new_scale_factor %>%
  pivot_wider(names_from = KC.Model.MATHia., values_from = n)
```

### We want each observation to have number of opportunities across KC.

```{r message=FALSE, warning=FALSE}
new_opp_scale_factor <- opp_scale_factor %>%
  select(-Step.Name) %>%
  group_by(Anon.Student.Id) %>%
  mutate("scale-drawings-3-enter scale factor value." = 
           list(`scale-drawings-3-enter scale factor value.`),
         "scale-drawings-3-enter scale factor units." = 
           list(`scale-drawings-3-enter scale factor units.`),
         "scale-drawings-3-determine unknown measure, simple scale factor." = 
           list(`scale-drawings-3-determine unknown measure, simple scale factor.`),
         "scale-drawings-3-determine unknown measure, complex scale factor." = 
           list(`scale-drawings-3-determine unknown measure, complex scale factor.`)) %>%
  distinct()
```

```{r message=FALSE, warning=FALSE}
head(new_opp_scale_factor)
```

### Reating the above procedure for another workspace. We want each observation to have number of opportunities across KC.

```{r message=FALSE, warning=FALSE}
new_model_2_factor <- subset(model_2_factor, KC.Model.MATHia. != "")
revised_new_model_2_factor <- new_model_2_factor %>%
  select(Anon.Student.Id, KC.Model.MATHia., Step.Name) %>%
  count(Anon.Student.Id, KC.Model.MATHia., Step.Name) 
```

```{r message=FALSE, warning=FALSE}
opp_model_2_factor <- revised_new_model_2_factor %>%
  pivot_wider(names_from = KC.Model.MATHia., values_from = n) 
```

```{r message=FALSE, warning=FALSE}
new_opp_model_2_factor <- opp_model_2_factor %>%
  select(-Step.Name) %>%
  group_by(Anon.Student.Id) %>%
  mutate("identifying units-1" = list(`identifying units-1`),
         "enter given, reading words-1" = list(`enter given, reading words-1`),
         "find y, any form-1" = list(`find y, any form-1`),
         "enter given, reading numerals-1" = list(`enter given, reading numerals-1`),
         "define variable-1" = list(`define variable-1`),
         "write expression, positive intercept-1" = 
           list(`write expression, positive intercept-1`),
         "write expression, negative intercept-1" = 
           list(`write expression, negative intercept-1`),
         "write expression, negative slope-1" =
           list(`write expression, negative slope-1`),
         "write expression, positive slope-1" = 
           list(`write expression, positive slope-1`)) %>%
  distinct()
```

```{r message=FALSE, warning=FALSE}
head(new_opp_model_2_factor)
```

### Reating the above procedure for another workspace. We want each observation to have number of opportunities across KC.

```{r message=FALSE, warning=FALSE}
new_analyze_2_factor <- subset(analyze_2_factor, KC.Model.MATHia. != "")
revised_new_analyze_2_factor <- new_analyze_2_factor %>%
  select(Anon.Student.Id, KC.Model.MATHia., Step.Name) %>%
  count(Anon.Student.Id, KC.Model.MATHia., Step.Name)
```

```{r message=FALSE, warning=FALSE}
opp_analyze_2_factor <- revised_new_analyze_2_factor %>%
  pivot_wider(names_from = KC.Model.MATHia., values_from = n) 
```

```{r message=FALSE, warning=FALSE}
new_opp_analyze_2_factor <- opp_analyze_2_factor %>%
  select(-Step.Name) %>% 
  group_by(Anon.Student.Id) %>%
  mutate("match _dep expression with description." = 
           list(`match _dep expression with description.`),
         "match _linear-term expression with description." = 
           list(`match _linear-term expression with description.`),
         "match _intercept expression with description." = 
           list(`match _intercept expression with description.`),
         "interpret scenario with numbers" =
           list(`interpret scenario with numbers`),
         "match _indep expression with description." = 
           list(`match _indep expression with description.`),
         "interpret scenario with words" = 
           list(`interpret scenario with words`),
         "match _slope expression with description." = 
           list(`match _slope expression with description.`)) %>%
  distinct()
```

```{r message=FALSE, warning=FALSE}
head(new_opp_analyze_2_factor)
```

Joining these dataframes by `Anon.Student.Id`

```{r message=FALSE, warning=FALSE}
opp_all_workspaces <- new_opp_analyze_2_factor %>%
  dplyr::inner_join(new_opp_model_2_factor, by = "Anon.Student.Id")
opp_all_workspaces <- opp_all_workspaces %>%
  dplyr::inner_join(new_opp_scale_factor, by = "Anon.Student.Id")
```

### Remove NAs for each cell.

```{r message=FALSE, warning=FALSE}
new_opp_all_workspaces <- opp_all_workspaces
remove_na <- function(v){
  v1 <- v[!is.na(v)]
  if (is_empty(v1) == TRUE){
    return(0)
  }
  return(v1)
}
for (i in colnames(new_opp_all_workspaces)[-1]){
  new_opp_all_workspaces[[i]] <- lapply(new_opp_all_workspaces[[i]], remove_na)
}
new_opp_all_workspaces <- 
  new_opp_all_workspaces[,order(colnames(new_opp_all_workspaces))]
```

```{r message=FALSE, warning=FALSE}
# Sanity check
check_1 <- new_opp_all_workspaces
for (i in colnames(check_1)[-1]){
  check_1[[i]] <- lapply(check_1[[i]], sum)
}
check_cols <- colnames(check_1)[1]
for (i in colnames(check_1)[-1]){
  check_cols <- c(check_cols, substr(i,7,100))
}
colnames(check_1) <- check_cols
check_1 <- check_1[,order(colnames(check_1))]
```

```{r message=FALSE, warning=FALSE}
# Sanity check
check_2_scale_factor <- new_scale_factor %>%
  select(Anon.Student.Id, KC.Model.MATHia., Outcome) %>%
  group_by(Anon.Student.Id)
check_2_scale_factor <- check_2_scale_factor %>%
  pivot_wider(names_from = KC.Model.MATHia., values_from = Outcome)
check_2_model_2_factor <- new_model_2_factor %>%
  select(Anon.Student.Id, KC.Model.MATHia., Outcome) %>%
  group_by(Anon.Student.Id)
check_2_model_2_factor <- check_2_model_2_factor %>%
  pivot_wider(names_from = KC.Model.MATHia., values_from = Outcome)
check_2_analyze_2_factor <- new_analyze_2_factor %>%
  select(Anon.Student.Id, KC.Model.MATHia., Outcome) %>%
  group_by(Anon.Student.Id)
check_2_analyze_2_factor <- check_2_analyze_2_factor %>%
  pivot_wider(names_from = KC.Model.MATHia., values_from = Outcome)
check_2 <- check_2_analyze_2_factor %>%
  dplyr::inner_join(check_2_model_2_factor, by = "Anon.Student.Id")
check_2 <- check_2 %>%
  dplyr::inner_join(check_2_scale_factor, by = "Anon.Student.Id")
check_2 <- check_2[,order(colnames(check_2))]
opp_data <- check_2
for (i in colnames(check_2)[-1]){
  check_2[[i]] <- lapply(check_2[[i]], length)
}
```

Outcome data and opportunity count per KC

```{r message=FALSE, warning=FALSE}
full_opp <- cbind(new_opp_all_workspaces, opp_data)
```

### Function for obtaining success rate for each opportunities.

```{r message=FALSE, warning=FALSE}
get_opp_success_rate <- function(v1, v2){
  if (v1 == 0 | is.null(v2)==TRUE){
    return(0)
  }
  success <- function(vec){
    ret_vec <- length(which(vec %in% "OK"))/length(vec)
    return(ret_vec)
  }
  ret_fin_vec <- c()
  v <- v2
  for (i in 1:length(v1)){
    ret_fin_vec <- c(ret_fin_vec, success(v[1:v1[i]]))
    v <- v[-c(1:v1[i])]
  }
  return(ret_fin_vec)
}
```

Testing the function.

```{r message=FALSE, warning=FALSE}
get_opp_success_rate(0, c("OK","ERROR","OK","OK", "BUG"))
get_opp_success_rate(c(3,2), c("OK","ERROR","OK","OK", "BUG"))
get_opp_success_rate(c(2,3), c("OK","ERROR","OK","OK", "BUG"))
get_opp_success_rate(c(1,1,3), c("OK","ERROR","OK","OK", "BUG"))
```

### Creating list having success rate across opportunities for KC for each student.

```{r message=FALSE, warning=FALSE}
opp_opp <- list()
for (j in 2:21){
  temp <- full_opp[,c(j,j+21)]
  x <- c(temp[[1]], temp[[2]])
  temp_res <- list()
  for (i in 1:13114){
    temp_res[[i]] <- get_opp_success_rate(x[[i]],x[[i+13114]])
  }
  opp_opp[[j-1]] <- temp_res
}

```

### Creating a dataframe.

```{r message=FALSE, warning=FALSE}
tp_opp <- new_opp_all_workspaces
for (i in 1:20){
  tp_opp[[i+1]] <- as.vector(opp_opp[[i]])
}
```




