---
title: "Pilot"
output: pdf_document
date: "2023-07-20"
---

```{r setup, include=FALSE}

library(tidyverse)

theme_set(theme_minimal())

# load in both raw datasets
df1_raw <- read.csv("pilot1.csv")
df2_raw <- read.csv("pilot2.csv")

# load in respective set data
sets1 <- read.csv("pilot 1 sets.csv")
sets2 <- read.csv("pilot 2 sets.csv")


# function to clean them
clean_pilot <- function(df_raw, sets, comp_check) {
  
  # filter by example responses, save that variable into df_all
  df_all <- df_raw  %>% filter(
    example_1 == 1 & example_2 == 2 & example_3 == 3 & example_4 == 4 & example_5 == 5
  ) 
  if(comp_check) {
    df_all <- df_all %>% filter(comp.check1 == "correct" | comp.check2 == "correct")
  }
  
  # then, grab the select variables and make them long
  df <- df_all %>% filter(
    example_1 == 1 & example_2 == 2 & example_3 == 3 & example_4 == 4 & example_5 == 5
  ) %>% 
    select(!matches("example") & !matches("elab")  & !matches("comp.check") & matches("\\d")) %>% 
    pivot_longer(
      cols = everything(),  # Apply to all columns
      names_to = "variant",  # Name of new column to store the old column names
      values_to = "rank"  # Name of new column to store value inside cell
    ) %>% 
    mutate(
      rank = as.integer(rank)
    ) %>% 
    separate(
      col = variant,  # Column to separate
      into = c("question", "mode", "set"),  # New column names
      sep = "\\.",  # Column separator - escaped since "." is special character
      convert = TRUE  # Convert number to numeric type
    ) %>% 
    separate(
      col = set, # Column to separate
      into = c("set", "number"), # New column anmes
      sep = "_", # Column separator
      convert = TRUE # Convert number to numeric type
    ) 
  
  df <- left_join(df, sets, by = c("set", "number"))
  
  return(list(df_all = df_all, df = df))
  
}

result1 <- clean_pilot(df1_raw, sets1, FALSE)
df1_all <- result1$df_all
df1 <- result1$df

result2 <- clean_pilot(df2_raw, sets2, TRUE)
df2_all <- result2$df_all
df2 <- result2$df

```

```{r pilot demographics, echo = FALSE}

# participant age
ggplot(df1_all, aes(x = age))+
  geom_bar(fill = "#6F9795")+
  scale_x_discrete(limits = c("18 - 24", "25 - 34", "35 - 44", "45 - 54", "55 - 64", "65 - 74", "75 - 84", "85 or older", "Prefer not to answer"), labels = c("18-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", ">85", "No answer\n"))+
  labs(
    x = "Age Range",
    y = "Count",
    title = "Distribution of Participant Ages",
    subtitle = "Pilot 1"
  )
# participant education
ggplot(df1_all, aes(x = education))+
  geom_bar(fill = "#6F9795")+
  scale_x_discrete(limits =  c("Less than high school", "High school graduate", "Some college", "2 year degree", "4 year degree", "Professional degree", "Doctorate", "Prefer not to answer"), labels = c("Less than\nhigh school\n", "High school\ngraduate", "Some\ncollege", "2 year\ndegree", "4 year\ndegree", "Professional\ndegree", "Doctorate", "No answer"))+
  labs(
    x = "Education Level",
    y = "Count",
    title = "Distribution of Participant Education",
    subtitle = "Pilot 1"
  )

# participant age
ggplot(df2_all, aes(x = age))+
  geom_bar(fill = "#ABC475")+
  scale_x_discrete(limits = c("18 - 24", "25 - 34", "35 - 44", "45 - 54", "55 - 64", "65 - 74", "75 - 84", "85 or older", "Prefer not to answer"), labels = c("18-24", "25-34", "35-44", "45-54", "55-64", "65-74", "75-84", ">85", "No answer\n"))+
  labs(
    x = "Age Range",
    y = "Count",
    title = "Distribution of Participant Ages",
    subtitle = "Pilot 2"
  )
# participant education
ggplot(df2_all, aes(x = education))+
  geom_bar(fill = "#ABC475")+
  scale_x_discrete(limits =  c("Less than high school", "High school graduate", "Some college", "2 year degree", "4 year degree", "Professional degree", "Doctorate", "Prefer not to answer"), labels = c("Less than\nhigh school\n", "High school\ngraduate", "Some\ncollege", "2 year\ndegree", "4 year\ndegree", "Professional\ndegree", "Doctorate", "No answer"))+
  labs(
    x = "Education Level",
    y = "Count",
    title = "Distribution of Participant Education",
    subtitle = "Pilot 2"
  )


```

1 participant failed the data quality check for Pilot 1. n = 20
9 participants failed the data quality check for Pilot 2. n = 15

```{r visualize, echo = FALSE}

visualize_pilot <- function(df, pilot_number) {
  
  df_summary <- df %>% group_by(mode, set, variant) %>% 
    summarise(rank_mean = mean(rank), 
              sd = sd(rank),
              n = n(),
              se = sd / sqrt(n),
              rank_min = rank_mean - se, # change this if you'd like to use SD or SE 
              rank_max = rank_mean + se, # change this if you'd like to use SD or SE 
    )
  
  pilot_subtitle = paste0("Pilot ", pilot_number)
  
  for (set_number in 1:length(unique(df_summary$set))) {
    set_string <- unique(df_summary$set)[set_number]
    df_summary_subset <- subset(df_summary, set == set_string)
    visual <- ggplot(df_summary_subset, aes(x = variant, y = rank_mean))+
      geom_errorbar(aes(ymin = rank_min, ymax = rank_max),
                    color = ifelse(pilot_number == 1, "#306A68", "#86A34A"))+
      scale_y_reverse()+
      labs(
        title = paste("Rankings for", df_summary_subset$set[1], "set,", pilot_subtitle, " (1 = most fuzzy, 5 = most concrete)"),
        x = "\nForecast Variant",
        y = "Average Rank",
        subtitle = ifelse(df_summary_subset$mode[1] == "speech", "Mode = Speech", 
                          ifelse(df_summary_subset$mode[1] == "text", "Mode = Text", "Mode = Visualization")),
      )
    print(visual)
  }
}

visualize_pilot(df1, 1)
visualize_pilot(df2, 2)

```


