---
title: "Results classroom PGG"
author: "Jantsje Mol"
date: "23/9/2020"
output: html_document
---

```{r setup, include=FALSE}
# !diagnostics off
here <- here::here # because lubridate also has a here function

library(tidyverse, stringr)
library(numbers)
library(dplyr)
library(anytime) # for converting Epoch times from oTree Page Time 

select <- dplyr::select
filter <- dplyr::filter

timespent1 <- read.csv(here("_DATA", "PageTimes-2020-11-24.csv"), sep = ',', header=TRUE)
timespent2 <- read.csv(here("_DATA", "PageTimes-2020-11-26.csv"), sep = ',', header=TRUE)
timespent3 <- read.csv(here("_DATA", "PageTimes-2020-11-30.csv"), sep = ',', header=TRUE)

timespent <- rbind(timespent1, timespent2, timespent3) %>% 
  rename(pp_code = participant_code) %>% 
  mutate(timestamp = anytime(epoch_time)) %>% 
  add_count(pp_code, sort=TRUE) %>% 
  filter(app_name == "classroom_pgg") %>% 
  filter(page_index == 1 | page_index == 70) %>% 
  select(c(pp_code, page_index, timestamp))

rm(timespent1, timespent2, timespent3)

timespent <- pivot_wider(timespent, names_from = page_index, names_prefix = "page", values_from = timestamp) %>% 
  filter(!is.na(page70))
timespent <- timespent %>% 
  mutate(seconds_total = difftime(page70, page1, units = "secs"))%>% 
  mutate(pp_minutes= as.integer(as.numeric(seconds_total)/60),
         pp_seconds= mod(as.numeric(seconds_total),60)) %>% 
  select(-starts_with("page"))

df1 <- read.csv(here("_DATA", "all_apps_wide_2020-11-24.csv"), sep = ',', header=TRUE) %>% 
  filter(participant._current_app_name == "classroom_pgg")

df2 <- read.csv(here("_DATA", "all_apps_wide_2020-11-26.csv"), sep = ',', header=TRUE) %>% 
  filter(participant._current_app_name == "classroom_pgg")

df3 <- read.csv(here("_DATA", "all_apps_wide_2020-11-30.csv"), sep = ',', header=TRUE) %>% 
  filter(participant._current_app_name == "classroom_pgg")

df <- rbind(df1, df2, df3) %>% 
  rename(pp_code = participant.code,
         pp_label = participant.label,
         pp_payoff = participant.payoff,
         pp_start_time = participant.time_started,
         pp_id = participant.id_in_session,
         session_label = session.label,
         grup_id = classroom_pgg.1.group.id_in_subsession) # typo is intentional to be able to deselect all other group_ variables
rm(df1, df2, df3)

df <- merge(df, timespent, by="pp_code", all.x = TRUE) 
rm(timespent)

df <- df %>% 
  rename(session_num_rounds = session.config.num_rounds,
         session_chat = session.config.cheap_talk,
         session_threshold = session.config.threshold,
         session_punishment = session.config.punishment) %>% 
  select(-contains("config")) %>%
  select(-contains("floodgame")) %>% 
  select(-contains("punishment_endowment")) %>%
  select(-contains("subsession")) %>%
  select(-contains("id_in_group")) %>%
  select(-contains("_share")) %>%
  select(-contains("average_contribution")) %>%
  select(-contains("pd_payoff")) %>%
  select(-c(participant._is_bot, participant._max_page_index,
            participant._current_page_name, participant._current_app_name, participant.visited, 
            participant.mturk_worker_id, participant.mturk_assignment_id, session.mturk_HITId, 
            session.comment, session.mturk_HITGroupId, session.is_demo, participant._index_in_pages,
            participant.payoff_plus_participation_fee)) 

thenames <- names(df)
function.clean_varnames <-  function(text){
  if(grepl("player", text, perl=TRUE) | grepl("group", text, perl=TRUE)){
    if(grepl("classroom_pgg.", text, perl=TRUE)){text <- sub(pattern = "classroom_pgg.", replacement = "", x = text)}
    if(grepl(".player", text, perl=TRUE)){text <- sub(pattern = ".player", replacement = "", x = text)} 
    if(grepl(".group", text, perl=TRUE)){text <- sub(pattern = ".group", replacement = "", x = text)} 
    text <- strsplit(text, '[.]')
    text <- text[[1]][c(2,1)]
    text <- paste(text, collapse='_')
  }
  else {text <- text}} 
thenames <- lapply(thenames, function.clean_varnames)
names(df) <- thenames 
rm(thenames, function.clean_varnames)


df <- df %>% 
  filter(session_label != "test-jantsje") %>% 
  filter(pp_label != "") %>% # used to force to next page
  select(starts_with("pp_"), starts_with("session"), grup_id, ends_with("_1"), ends_with("_2"), ends_with("_3"), ends_with("_4"),
         ends_with("_5"), ends_with("_6"), ends_with("_7"), ends_with("_8"), ends_with("_9"), ends_with("_10")) %>% 
  gather(variable,value,-c("pp_label", "pp_start_time", "pp_payoff", "pp_code", "pp_id", "session.code", "session_label",
                           "pp_minutes", "pp_seconds", 
                           "session_chat", "session_threshold", "session_punishment", "session_num_rounds",
                           "grup_id")) %>%
  mutate(round = parse_number(variable)) %>%
  mutate(variable =  gsub("\\d","",x = variable)) %>% 
  spread(variable,value) %>% 
  rename(contribution = contribution_,
         payoff = payoff_,
         group_contribution = total_contribution_,
         reached_threshold = reached_threshold_,
         punishment_received = punishment_received_,
         punishment_sent = punishment_sent_,
         session_code = session.code,
         group_id = grup_id) %>% 
  mutate(contribution = case_when(round > session_num_rounds ~ NA_integer_,
                                  TRUE ~ contribution),
         payoff = case_when(round > session_num_rounds ~ NA_integer_,
                                  TRUE ~ payoff),
         punishment_received = case_when(round > session_num_rounds ~ NA_integer_,
                                  TRUE ~ punishment_received),
         punishment_sent = case_when(round > session_num_rounds ~ NA_integer_,
                                  TRUE ~ punishment_sent),
         reached_threshold = case_when(round > session_num_rounds ~ NA_integer_,
                                     TRUE ~ reached_threshold),
         group_contribution = case_when(round > session_num_rounds ~ NA_integer_,
                                     TRUE ~ group_contribution),
         punishment_received = case_when(session_punishment == 0 ~ NA_integer_,
                                         TRUE ~ punishment_received),
         punishment_sent = case_when(session_punishment == 0 ~ NA_integer_,
                                         TRUE ~ punishment_sent),
         reached_threshold = case_when(session_threshold == 0 ~ NA_integer_,
                                     TRUE ~ reached_threshold)) 

parameters.session1 <- df %>% 
  filter(session_label == "session 1") %>% 
  filter(pp_id == max(pp_id), round == 1) %>% 
  select(starts_with("session"), pp_id, group_id)
```

```{r correct winners session 3, include = FALSE}
average_payoff_session3 <- df %>% 
  filter(session_label == "session 3") %>% 
  filter(pp_payoff > 0) %>% 
  filter(round == 10)

hist(average_payoff_session3$pp_payoff)
table(average_payoff_session3$pp_payoff)

average_payoff_session3 <- average_payoff_session3 %>% 
  select(pp_code, pp_payoff) %>% 
  summarize(mean = mean(pp_payoff))

gedupeerden <- df %>% 
  filter(session_label == "session 3" & 
           (pp_payoff == 0 | pp_code == "o7fcnxq7" | pp_code == "hcaerzpj" | pp_code == "k8uumn7t" | pp_code == "bd50sv8t")) %>% 
  mutate(pp_code = as.character(pp_code))

table(gedupeerden$pp_code)

df <- df %>% 
  mutate(pp_payoff = case_when(session_label == "session 3" & 
                                 (pp_payoff == 0 | pp_code == "o7fcnxq7" | pp_code == "hcaerzpj" | 
                                    pp_code == "k8uumn7t" | pp_code == "bd50sv8t") ~ as.numeric(average_payoff_session3),
                               TRUE ~ as.numeric(pp_payoff)))

rm(gedupeerden, average_payoff_session3)
  
```


## Session 1: one shot PGG
*Monday November 2nd, 2020*

In this game, `r parameters.session1$pp_id` students participated.

* Number of rounds: `r parameters.session1$session_num_rounds`
* Group size: 3
* Multiplication factor: 1.6
* Punishment: `r parameters.session1$session_punishment`
* Threshold: `r parameters.session1$session_threshold`
* Chat: `r parameters.session1$session_chat`

```{r histograms contributions, echo = FALSE, warning = FALSE, message=FALSE}
df.session1 <- df %>% 
  filter(round == 1,
         session_label == "session 1") 

fig.contribution <- ggplot(df.session1, aes(x=contribution), subset = round == 1) + geom_histogram(binwidth = 5, color = "white") +
  labs(x="Individual contribution", y = "Frequency") 

fig.group.contribution <- ggplot(df.session1, aes(x=group_contribution), subset = round == 1) + geom_histogram(binwidth = 10, color = "white") +
  labs(x="Group contribution", y = "Frequency") + scale_x_continuous(limits = c(0,300))

library(gridExtra)
grid.arrange(fig.contribution, fig.group.contribution,
             nrow = 1)

ggplot(df.session1, aes(x=payoff)) + geom_histogram(binwidth = 5, color = "white") +
  labs(x="Individual payoff in points", y = "Frequency") + scale_x_continuous(limits = c(40,220)) +
  geom_vline(xintercept=160/3, linetype = "dashed") + annotate(geom = "label", x = 160/3, y = 13, label = "minimum") +
  geom_vline(xintercept=206, linetype = "dashed") + annotate(geom = "label", x = 205, y = 13, label = "maximum") +
  geom_vline(xintercept=(300*1.6)/3, linetype = "dashed") + annotate(geom = "label", x = (300*1.6)/3, y = 13, label = "full contribution") 

rm(list = ls(pattern = "fig"))
rm(parameters.session1)

```

```{r, include = FALSE}
parameters.session2 <- df %>% 
  filter(session_label == "session 2") %>% 
  filter(pp_id == max(pp_id), round == 1) %>% 
  select(starts_with("session"), pp_id, group_id)

```
## Session 2: ten rounds PGG
*Wednesday November 11th, 2020*

In this game, `r parameters.session2$pp_id` students participated.

* Number of rounds: `r parameters.session2$session_num_rounds`
* Group size: 3
* Multiplication factor: 1.6
* Punishment: `r parameters.session2$session_punishment`
* Threshold: `r parameters.session2$session_threshold`
* Chat: `r parameters.session2$session_chat`

```{r, include = FALSE}
parameters.session3 <- df %>% 
  filter(session_label == "session 3") %>% 
  filter(pp_id == max(pp_id), round == 1) %>% 
  select(starts_with("session"), pp_id, group_id)

```
## Session 3: ten rounds PGG + threshold
*Thursday November 19th, 2020*

In this game, `r parameters.session3$pp_id` students participated.

* Number of rounds: `r parameters.session3$session_num_rounds`
* Group size: 3
* Multiplication factor: 1.6
* Punishment: `r parameters.session3$session_punishment`
* Threshold: `r parameters.session3$session_threshold`
* Chat: `r parameters.session3$session_chat`

```{r, include = FALSE}
parameters.session4 <- df %>% 
  filter(session_label == "session 4") %>% 
  filter(pp_id == max(pp_id), round == 1) %>% 
  select(starts_with("session"), pp_id, group_id)

parameters.session5 <- df %>% 
  filter(session_label == "session 5") %>% 
  filter(pp_id == max(pp_id), round == 1) %>% 
  select(starts_with("session"), pp_id, group_id)


```

## Session 4: ten rounds PGG + threshold + chat
*Thursday November 26th, 2020*

In this game, `r parameters.session4$pp_id` students participated.

* Number of rounds: `r parameters.session4$session_num_rounds`
* Group size: 3
* Multiplication factor: 1.6
* Punishment: `r parameters.session4$session_punishment`
* Threshold: `r parameters.session4$session_threshold`
* Chat: `r parameters.session4$session_chat`

## Session 5: ten rounds PGG + threshold + chat + punishment

*Monday November 30th, 2020*

In this game, `r parameters.session5$pp_id` students participated.

* Number of rounds: `r parameters.session4$session_num_rounds`
* Group size: 3
* Multiplication factor: 1.6
* Punishment: `r parameters.session5$session_punishment`
* Threshold: `r parameters.session5$session_threshold`
* Chat: `r parameters.session5$session_chat`

## Average individual contributions over rounds

```{r, echo = FALSE, warning= FALSE, message=FALSE}
df <- df %>% 
  mutate(session_label_inf = case_when(session_label == "session 1" ~ "Basic one round",
                                       session_label == "session 2" ~ "Basic ten rounds",
                                       session_label == "session 3" ~ "Threshold",
                                       session_label == "session 4" ~ "Threshold + chat",
                                       session_label == "session 5" ~ "Threshold + chat \n + punishment"))

df.gather <- df %>% 
  group_by(round, session_label_inf) %>% 
  summarise(mean.contribution = mean(contribution, na.rm=TRUE),
            sd.contribution = sd(contribution, na.rm=TRUE),
            n.contribution = n()) %>% 
  mutate(se.contribution = sd.contribution / sqrt(n.contribution),
         lower.ci.contribution = mean.contribution - qt(1 - (0.05 / 2), n.contribution - 1) * se.contribution,
         upper.ci.contribution = mean.contribution + qt(1 - (0.05 / 2), n.contribution - 1) * se.contribution)


ggplot(data=subset(df.gather, session_label_inf == "Basic one round"), aes(x=round, y=mean.contribution, color = session_label_inf)) + geom_point() + geom_line() +
  geom_errorbar(aes(ymin=lower.ci.contribution, ymax=upper.ci.contribution), alpha=0.9, width=.1) +
  scale_x_continuous(breaks = c(1:10)) + labs(y="Average individual contribution", x= "Round", color = "Session") +
  scale_y_continuous(limit = c(0,100)) + scale_color_brewer(palette="Dark2")

ggsave(here("_FIGURES", "contributions1.jpg"), width = 20, height = 15, units = "cm")

ggplot(data = subset(df.gather, session_label_inf == "Basic one round" | session_label_inf ==  "Basic ten rounds"), aes(x=round, y=mean.contribution, color = session_label_inf)) + geom_point() + geom_line() +
  geom_errorbar(aes(ymin=lower.ci.contribution, ymax=upper.ci.contribution), alpha=0.9, width=.1) +
  scale_x_continuous(breaks = c(1:10)) + labs(y="Average individual contribution", x= "Round", color = "Session") +
  scale_y_continuous(limit = c(0,100)) + scale_color_brewer(palette="Dark2")

ggsave(here("_FIGURES", "contributions2.jpg"), width = 20, height = 15, units = "cm")

ggplot(data = subset(df.gather, 
                     session_label_inf == "Basic one round" | 
                       session_label_inf ==  "Basic ten rounds" | 
                       session_label_inf == "Threshold"), aes(x=round, y=mean.contribution, color = session_label_inf)) + geom_point() + geom_line() +
  geom_errorbar(aes(ymin=lower.ci.contribution, ymax=upper.ci.contribution), alpha=0.9, width=.1) +
  scale_x_continuous(breaks = c(1:10)) + labs(y="Average individual contribution", x= "Round", color = "Session") +
  scale_y_continuous(limit = c(0,100)) + scale_color_brewer(palette="Dark2")

ggsave(here("_FIGURES", "contributions3.jpg"), width = 20, height = 15, units = "cm")

ggplot(data = subset(df.gather, 
                     session_label_inf == "Basic one round" | 
                       session_label_inf ==  "Basic ten rounds" | 
                       session_label_inf == "Threshold" | 
                       session_label_inf == "Threshold + chat"), 
       aes(x=round, y=mean.contribution, color = session_label_inf)) + 
  geom_point() + geom_line() +
  geom_errorbar(aes(ymin=lower.ci.contribution, ymax=upper.ci.contribution), alpha=0.9, width=.1) +
  scale_x_continuous(breaks = c(1:10)) + labs(y="Average individual contribution", x= "Round", color = "Session") +
  scale_y_continuous(limit = c(0,100)) + scale_color_brewer(palette="Dark2")

ggsave(here("_FIGURES", "contributions4.jpg"), width = 20, height = 15, units = "cm")

ggplot(df.gather, aes(x=round, y=mean.contribution, color = session_label_inf)) + geom_point() + geom_line() +
  geom_errorbar(aes(ymin=lower.ci.contribution, ymax=upper.ci.contribution), alpha=0.9, width=.1) +
  scale_x_continuous(breaks = c(1:10)) + labs(y="Average individual contribution", x= "Round", color = "Session") +
  scale_y_continuous(limit = c(0,100)) + scale_color_brewer(palette="Dark2", direction = -1)

ggsave(here("_FIGURES", "contributions5.jpg"), width = 20, height = 15, units = "cm")

```







## Groups reaching threshold over rounds
Note that for the first two sessions, no threshold was imposed. 
```{r threshold reached over rounds, echo = FALSE}

threshold <- df %>% 
  filter(session_label == "session 5" |
           session_label == "session 3" |
           session_label == "session 4") %>% 
  select(round, group_contribution, group_id, reached_threshold, session_label) %>% 
  filter(!is.na(reached_threshold)) %>% 
  distinct() %>% 
  group_by(round, session_label, reached_threshold) %>% 
  summarise(n = n()) %>%
  mutate(freq = n / sum(n)) %>% 
  filter(reached_threshold == 1)

threshold <- df %>% 
  mutate(reached_threshold_ = case_when(reached_threshold == 0 ~ "No",
                                       reached_threshold == 1 ~ "Yes",
                                       session_label == "session 2" & group_contribution >= 150 ~ "Yes",
                                       session_label == "session 2" & group_contribution < 150 ~ "No",
                                       session_label == "session 1" & group_contribution >= 150 ~ "Yes",
                                       session_label == "session 1" & group_contribution < 150 ~ "No")) %>% 
  select(round, group_contribution, group_id, reached_threshold_, session_label) %>% 
  filter(!is.na(reached_threshold_)) %>% 
  distinct() %>% 
  group_by(round, session_label, reached_threshold_) %>% 
  summarise(n = n()) %>%
  mutate(freq = n / sum(n)) %>% 
  filter(reached_threshold_ == "Yes")
  
ggplot(threshold, aes(x=round, y=freq, color = session_label)) + 
  geom_point() + geom_line() +
  scale_x_continuous(breaks = c(1:10)) + 
  labs(y="Percentage of groups reaching threshold", x= "Round", color = "Session") +
  scale_y_continuous(limit = c(0,1), labels = c("0%", "25%", "50%", "75%", "100%")) +  
  scale_color_brewer(palette="Dark2", direction = -1,  
                     labels = c("Basic one round", "Basic ten rounds", 
                                "Threshold", "Threshold + chat", "Threshold + chat \n + punishment"))

ggsave(here("_FIGURES", "threshold.jpg"), width = 20, height = 15, units = "cm")

```


## Number of chat messages

```{r clean chat data, echo = FALSE}
library(stringr)

chats1 <- read.csv(here("_DATA", "ChatMessages-2020-11-26.csv"), sep = ',', header=TRUE) %>% 
  select(c("participant__code", "nickname", "channel", "body", "timestamp")) %>% 
  mutate(session = "session 4")
chats2 <- read.csv(here("_DATA", "ChatMessages-2020-11-30.csv"), sep = ',', header=TRUE) %>% 
  select(c("participant__code", "nickname", "channel", "body", "timestamp")) %>% 
  mutate(session = "session 5")

chats1 <- chats1 %>% 
  mutate(timestamp = anytime(timestamp)) %>% 
  rename(participant = participant__code) %>% 
  mutate(channel = as.character(channel)) %>% 
  mutate(channel = as.integer(str_remove(channel, "3-classroom_pgg-"))) %>% 
  mutate(group = channel%%70-30) %>% 
  mutate(group = case_when(group < 0 ~ group + 70, TRUE ~ group),
         round = as.integer(channel/70))

chats2 <- chats2 %>% 
  mutate(timestamp = anytime(timestamp)) %>% 
  rename(participant = participant__code) %>% 
  mutate(channel = as.character(channel)) %>% 
  mutate(channel = as.integer(str_remove(channel, "5-classroom_pgg-"))) %>% 
  mutate(group = channel%%70-30) %>% 
  mutate(group = case_when(group < 0 ~ group + 70, TRUE ~ group),
         round = as.integer(channel/70))

chats <- rbind(chats1, chats2)
rm(chats1, chats2)

chat_sum <- chats %>% 
  group_by(group, round, session) %>% 
  tally() %>% 
  group_by(round, session) %>% 
  mutate(average = mean(n))


ggplot(chat_sum, aes(x=round, color = session)) + 
  geom_line(aes(y=average), size = 3, alpha = 0.3) +
  geom_point(aes(y=average), size = 4, alpha = 0.3) +
  geom_jitter(aes(y=n), width = 0.2) +
  scale_color_brewer(palette="Dark2", direction = -1,  
                     labels = c("Threshold + chat", "Threshold + chat \n + punishment"))+
  scale_x_continuous(breaks = c(1:10)) + 
  labs(y="Number of chat messages", x= "Round", color = "Session") 

# ggplot(chat_sum) + 
#   geom_boxplot(aes(y = n, group = factor(round))) +
#   scale_x_continuous(breaks = c(1:10), labels = c(1:10)) +
#   labs(y="Number of chat messages", x= "Round") 

ggsave(here("_FIGURES", "num_chat.jpg"), width = 20, height = 15, units = "cm")
```

```{r print chats, echo = FALSE}
library(kableExtra)
  chats$nickname <- cell_spec(chats$nickname, bold = T,
                              color = ifelse(chats$nickname == "Participant 1", "red", 
                                             ifelse(chats$nickname == "Participant 2", "blue", "green")))
  
chats %>% 
  filter(group == 6 & session == "session 5") %>% 
  select(nickname, body) %>% 
  kbl(escape = F, format = "html", booktabs = T) %>%
  kable_paper("striped", full_width = F) %>% 
  pack_rows(index=c("Round 1" = 3, "Round 2" = 7, "Round 3" = 14, "Round 4" = 4))

```


## Punishments over rounds, split by threshold
```{r punishments over rounds, echo = FALSE}

session5 <- df %>% 
  filter(session_label == "session 5") %>% 
  select(punishment_sent, round, group_contribution, group_id, reached_threshold) %>% 
  group_by(round, group_id) %>% 
  mutate(avg_punishment_sent= mean(punishment_sent)) %>% 
  select(-punishment_sent) %>% 
  filter(!is.na(reached_threshold)) %>% 
  distinct() %>% 
  mutate(reached_threshold = case_when(reached_threshold == 1 ~ "Group reached threshold",
                                       reached_threshold == 0 ~ "Group did not reach threshold"))
  
ggplot(session5, aes(x=round)) + 
  geom_boxplot(aes(y=avg_punishment_sent, group = round)) +
  geom_jitter(aes(y=avg_punishment_sent), alpha =0.1) +
  scale_color_brewer(palette="Dark2", direction = -1) +
  scale_x_continuous(breaks = c(1:10)) + 
  scale_y_continuous(breaks = c(0,2,4,6,8,10)) +
  labs(y="Average punishment sent", x= "Round") +
  facet_wrap(~ reached_threshold, ncol = 2)

ggsave(here("_FIGURES", "punishments.jpg"), width = 20, height = 15, units = "cm")

```

## Latest leaderboard
```{r, echo = FALSE, warning= FALSE, message=FALSE, results = 'asis'}
 payoffs <- df %>%
  filter(round == 10) %>% 
  select(pp_label, pp_payoff, session_label) %>% 
  pivot_longer(cols = pp_payoff) %>% 
   filter(pp_label != "mke240") %>% # teachers cannot win
   filter(pp_label != "aaa111") %>%
   filter(pp_label != "jml740") %>% 
  group_by(pp_label) %>% 
  mutate(pp_payoff_total = sum(value))

df.leaderboard <- payoffs %>% 
  ungroup() %>% 
  filter(session_label == "session 1") %>% 
   arrange(-pp_payoff_total) %>%
   filter(row_number() < 4) %>%
   mutate(ranking = row_number()) %>%
   select(ranking, pp_label, pp_payoff_total) # this also adds the order

 library(kableExtra)
 knitr::kable(df.leaderboard) %>%
   kableExtra::kable_styling(full_width = F,
                             position = "left")
```



