---
title: "MATHIA_PREREQUISITES"
author: "Smeet Poladia"
date: "5/5/2021"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
```

### To make an R Shiny app, we use a list to retrieve values for different pairs of workspaces, such as p-value, mean score, etc. In this R file, we create that list (success rate is the student performance metric).

```{r}
mathia2 <- read_csv("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/mathia_filtered.csv")
df_success_rate <- read_csv("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/df_success_rate.csv")
df_assistance_score <- read_csv("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/df_assistance_score.csv")
```

```{r}
df_success_rate_complete <- df_success_rate[complete.cases(df_success_rate),]
```

```{r message=FALSE, warning=FALSE}
ten_list <- list()
for (i in 1:nrow(df_success_rate_complete)){
ws.1 = as.character(df_success_rate_complete[i, 2])
ws.2 = as.character(df_success_rate_complete[i, 4])
temp.time <- mathia2 %>%
  filter(Level..Workspace.Id. %in% c(ws.1, ws.2)) %>%
  group_by(Anon.Student.Id, Level..Workspace.Id.) %>%
  summarise(Start.Time = min(Time))
result = NA
for (id in unique(temp.time$Anon.Student.Id)){
  t.1 = temp.time[which(temp.time$Anon.Student.Id == id & 
                          temp.time$Level..Workspace.Id. == ws.1), ]
  t.2 = temp.time[which(temp.time$Anon.Student.Id == id & 
                          temp.time$Level..Workspace.Id. == ws.2), ]
  if (nrow(t.1) == 0){
    label = paste("Attempted only '", ws.2, " '", sep = "")
  } else if (nrow(t.2) == 0){
    label = paste("Attempted only '", ws.1, " '", sep = "")
  } else if (t.1$Start.Time < t.2$Start.Time){
    label = paste("Attempted '", ws.1, "' first, and then '", ws.2, "'", sep ="")
  } else {
    label = paste("Attempted '", ws.2, "' first, and then '", ws.1, "'", sep ="")
  }
  if (is.na(result)){
    result = data.frame("id" = id, "label" = label)
  } else{
    result = rbind(result, c(id, label))
  }
}
id.v = result$id[which(result$label %in% 
                         c(paste("Attempted '", ws.1, "' first, and then '", ws.2, "'", sep =""),
                           paste("Attempted '", ws.2, "' first, and then '", ws.1, "'", sep =""),
                           paste("Attempted only '", ws.2, " '", sep = "")))]

test = mathia2[which(mathia2$Anon.Student.Id %in% id.v & mathia2$Attempt.At.Step == 1 
                & mathia2$Level..Workspace.Id. == ws.2), ]
success = test %>%
  group_by(Anon.Student.Id) %>%
  summarise(success.rate = sum(Outcome == "OK")/sum(Attempt.At.Step))
names(success) = c("id", "Success Rate")
bozo = inner_join(success, result, by = "id")
bozo$`indicator variable` = ifelse(bozo$label == paste("Attempted '", ws.1, "' first, and then '", ws.2, "'", sep =""), 1, 0)
graph2 <- ggplot(bozo, aes(x = `Success Rate`, fill = as.factor(`indicator variable`))) + 
  geom_density(alpha = 0.7) +
  labs(title = "Density plot",
       x = "Success rate on Workspace 2",
       y = "",
       fill = "") + 
  theme_bw() +
  scale_fill_discrete(
    limits = c(0, 1),
    labels = c("Didn't attempt Workspace 1 first", "Attempted Workspace 1 first")
  )
graph1 <- ggplot(bozo, aes(x = as.factor(`indicator variable`), y = `Success Rate`, fill = as.factor(`indicator variable`))) + 
  geom_boxplot() + 
  labs(title = "Boxplot",
       y = "Success rate on Workspace 2",
       x = "",
       fill = "") +
  theme_bw() +
  scale_fill_discrete(
    limits = c(0, 1),
    labels = c("Didn't attempt Workspace 1 first", "Attempted Workspace 1 first")
  )
df_1 <- bozo %>%
  group_by(label) %>%
  summarise(Count = length(id)) %>%
  rename(Label = label)
test_t <- tryCatch(
  {t.test(bozo[which(bozo$`indicator variable` == 1), 2], bozo[which(bozo$`indicator variable` == 0), 2],
          var.equal = FALSE, alternative = "greater")},
  error = function(msg){return(NA)})
if (is.na(test_t) == FALSE){
  #fit = lm(`Success Rate` ~ `indicator variable`, data = bozo)
  fit_display <- "not there"
  statement_1 <- paste("Mean success rate of Workspace 2 for students who attempted Workspace 1 first is", round(test_t$estimate[[1]], 4))
  statement_2 <- paste("Mean success rate of Workspace 2 for students who didn't attempt Workspace 1 first is", round(test_t$estimate[[2]], 4))
  statement_3 <- paste("The p-value is ", round(test_t$p.value, 4), ". The independent sample t test checks whether mean success rate on Workspace 2 for students who first attempted Workspace 1 is greater than that of students who didn't attempt Workspace 1 first", sep = "")
  statement_func <- function(pval){
   if(pval < 0.05){
      return("There is significant difference in mean success rate on Workspace 2 when students first attempt Workspace 1 (at 5% level of significance)")
   } else{
     return("There is no significant difference in mean success rate on Workspace 2 when students first attempt Workspace 1 (at 5% level of significance)")
   }
  }
  statement_4 <- statement_func(test_t$p.value)
  list.1 <- list(ws.1, ws.2, df_1,
               statement_1,
               statement_2,
               statement_3,
               statement_4,
               fit_display,
               graph1,
               graph2)
} else {
  list.1 <- list(ws.1, ws.2, df_1,
               graph1,
               graph2)
}
temp_ws <- ws.1
ws.1 <- ws.2
ws.2 <- temp_ws
temp.time <- mathia2 %>%
  filter(Level..Workspace.Id. %in% c(ws.1, ws.2)) %>%
  group_by(Anon.Student.Id, Level..Workspace.Id.) %>%
  summarise(Start.Time = min(Time))
result = NA
for (id in unique(temp.time$Anon.Student.Id)){
  t.1 = temp.time[which(temp.time$Anon.Student.Id == id & 
                          temp.time$Level..Workspace.Id. == ws.1), ]
  t.2 = temp.time[which(temp.time$Anon.Student.Id == id & 
                          temp.time$Level..Workspace.Id. == ws.2), ]
  if (nrow(t.1) == 0){
    label = paste("Attempted only '", ws.2, " '", sep = "")
  } else if (nrow(t.2) == 0){
    label = paste("Attempted only '", ws.1, " '", sep = "")
  } else if (t.1$Start.Time < t.2$Start.Time){
    label = paste("Attempted '", ws.1, "' first, and then '", ws.2, "'", sep ="")
  } else {
    label = paste("Attempted '", ws.2, "' first, and then '", ws.1, "'", sep ="")
  }
  if (is.na(result)){
    result = data.frame("id" = id, "label" = label)
  } else{
    result = rbind(result, c(id, label))
  }
}
id.v = result$id[which(result$label %in% 
                         c(paste("Attempted '", ws.1, "' first, and then '", ws.2, "'", sep =""),
                           paste("Attempted '", ws.2, "' first, and then '", ws.1, "'", sep =""),
                           paste("Attempted only '", ws.2, " '", sep = "")))]

test = mathia2[which(mathia2$Anon.Student.Id %in% id.v & mathia2$Attempt.At.Step == 1 
                & mathia2$Level..Workspace.Id. == ws.2), ]
success = test %>%
  group_by(Anon.Student.Id) %>%
  summarise(success.rate = sum(Outcome == "OK")/sum(Attempt.At.Step))
names(success) = c("id", "Success Rate")
bozo = inner_join(success, result, by = "id")
bozo$`indicator variable` = ifelse(bozo$label == paste("Attempted '", ws.1, "' first, and then '", ws.2, "'", sep =""), 1, 0)
graph2 <- ggplot(bozo, aes(x = `Success Rate`, fill = as.factor(`indicator variable`))) + 
  geom_density(alpha = 0.7) +
  labs(title = "Density plot",
       x = "Success rate on Workspace 2",
       y = "",
       fill = "") + 
  theme_bw() +
  scale_fill_discrete(
    limits = c(0, 1),
    labels = c("Didn't attempt Workspace 1 first", "Attempted Workspace 1 first")
  )
graph1 <- ggplot(bozo, aes(x = as.factor(`indicator variable`), y = `Success Rate`, fill = as.factor(`indicator variable`))) + 
  geom_boxplot() + 
  labs(title = "Boxplot",
       y = "Success rate on Workspace 2",
       x = "",
       fill = "") +
  theme_bw() +
  scale_fill_discrete(
    limits = c(0, 1),
    labels = c("Didn't attempt Workspace 1 first", "Attempted Workspace 1 first")
  )
df_1 <- bozo %>%
  group_by(label) %>%
  summarise(Count = length(id)) %>%
  rename(Label = label)
test_t <- tryCatch(
  {t.test(bozo[which(bozo$`indicator variable` == 1), 2], bozo[which(bozo$`indicator variable` == 0), 2],
          var.equal = FALSE, alternative = "greater")},
  error = function(msg){return(NA)})
if (is.na(test_t) == FALSE){
  #fit = lm(`Success Rate` ~ `indicator variable`, data = bozo)
  fit_display <- "not there"
  statement_1 <- paste("Mean success rate of Workspace 2 for students who attempted Workspace 1 first is", round(test_t$estimate[[1]], 4))
  statement_2 <- paste("Mean success rate of Workspace 2 for students who didn't attempt Workspace 1 first is", round(test_t$estimate[[2]], 4))
  statement_3 <- paste("The p-value is ", round(test_t$p.value, 4), ". The independent sample t test checks whether mean success rate on Workspace 2 for students who first attempted Workspace 1 is greater than that of students who didn't attempt Workspace 1 first", sep = "")
  statement_func <- function(pval){
   if(pval < 0.05){
      return("There is significant difference in mean success rate on Workspace 2 when students first attempt Workspace 1 (at 5% level of significance)")
   } else{
     return("There is no significant difference in mean success rate on Workspace 2 when students first attempt Workspace 1 (at 5% level of significance)")
   }
  }
  statement_4 <- statement_func(test_t$p.value)
  list.2 <- list(ws.1, ws.2, df_1,
               statement_1,
               statement_2,
               statement_3,
               statement_4,
               fit_display,
               graph1,
               graph2)
} else {
  list.2 <- list(ws.1, ws.2, df_1,
               graph1,
               graph2)
}
s <- list(list.1, list.2)
ten_list[[i]] <- s
}
```

```{r}
ten_list[[15]][[1]][[7]]
```

```{r}
save(ten_list, file = "/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/code/success_rate.RData")
```







