---
title: "El Farol Bar Problem Results"
author: "Sergio"
date: "10/5/2020"
output:
  pdf_document: default
  html_document: default
---

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

library(tidyverse)
library(data.table)
```

## El Farol Bar Problem

This is the file with the results ofthe El Farol Bar Problem Simulation (Arthur, 1994)

```{r getSimulationFile}
last_simulation_file <- system("ls -t runs/ |head -n1", intern = TRUE)
runs_dir = "runs/"
sim_results <- read.csv(file.path(runs_dir,last_simulation_file))
```

## Including Plots

### Attendance

```{r attendance}

attendance <- sim_results %>% group_by(run, step, my_play) %>%
  count(my_play)

ggplot(data=subset(attendance, my_play == "GOING"), aes(x = step, y = n)) + geom_line()

ggplot(sim_results, aes(predictor_prediction, predictor_fitness, color = my_play)) + geom_point()

ggplot(sim_results, aes(predictor_prediction, predictor_fitness, color = selected_predictor)) + geom_point()

ggplot(sim_results, aes(predictor_prediction, predictor_fitness, color = selected_predictor)) + geom_point() + facet_grid(cols = vars(my_play))

ggplot(data=sim_results, aes(x = predictor_prediction, color = selected_predictor)) + geom_density()

ggplot(data=sim_results, aes(x = predictor_prediction, y = predictor_fitness, color = my_play)) + geom_point()

ggplot(data=sim_results, aes(x = log(predictor_fitness), color = selected_predictor)) + geom_density()

ggplot(data=attendance, aes(x = n, color = my_play)) + geom_density()

ggplot(data=attendance, aes(x = my_play, y = n)) + geom_violin()

```

### Predictors Fitness

```{r predictors}

ggplot(sim_results, aes(color = selected_predictor, x=log(predictor_fitness))) + geom_density()

predictors <- sim_results %>% group_by(run, step, selected_predictor) %>%
  summarise(fitness = mean(predictor_fitness, na.rm = TRUE),
            mean_prediction = mean(predictor_prediction, na.rm = TRUE),
            median_prediction = median(predictor_prediction, na.rm = TRUE),
            n = n()
            )
  

ggplot(predictors, aes(x = step, y = log(fitness), color = selected_predictor)) + geom_line()

ggplot(predictors, aes(x = step, y = mean_prediction, color = selected_predictor)) + geom_line() 

ggplot(predictors, aes(x = step, y = mean_prediction, color = selected_predictor)) + geom_line()  + facet_grid(rows = vars(selected_predictor))

ggplot(predictors, aes(x = step, y = mean_prediction, color = factor(selected_predictor))) + facet_grid(rows = vars(selected_predictor)) + 
geom_ribbon(aes(ymin = mean_prediction - 10, ymax = mean_prediction + 10, group = selected_predictor), fill = "grey70")  + geom_line()

ggplot(predictors, aes(x = step, y = median_prediction, color = selected_predictor)) + geom_line()

ggplot(predictors, aes(x = step, y = n, color = selected_predictor)) + geom_line()

```

### Changing

```{r changing}
frequencies <- pivot_wider(attendance, names_from = my_play, values_from = n)

freq_names = c("run", "step", "going","not_going")
colnames(frequencies)  <-  freq_names
frequencies_df <- as.data.frame(frequencies)
frequencies_df$last_going = shift(frequencies_df$going, fill = NA, type = "lag")
frequencies_df$last_not_going = shift(frequencies_df$not_going, fill = NA, type = "lag")
ggplot(frequencies_df, aes(going, last_going)) + geom_tile(aes(fill = going))
ggplot(frequencies_df, aes(not_going, last_not_going)) + geom_tile(aes(fill = not_going))
```



