---
title: "PlotSimulation"
author: "Canaan Breiss"
date: "8/4/2021"
output: html_document
---

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

```{r}
library(tidyverse)
library(RColorBrewer)
library(viridis)
setwd("C:/Users/canaa/Google Drive/UCLA/Research and thoughts/VariationAndAdditivity/SimulatingToUnderstand")
```

In dat

```{r}
dat <- read_csv("outfile_probspace.csv") %>% 
  mutate_all(as.numeric) %>% 
mutate(Cumulativity = case_when(Difference < -0.01 ~ "Sublinear cumulativity",
                                     (Difference > -0.01) & (Difference < 0.01) ~ "Approximately linear cumulativity",
                                     Difference > 0.01 ~ "Superlinear cumulativity"))
```
  
  

Plug through appendix formula
  
```{r}
dat <- dat %>% 
  mutate(Numerator = exp(-WeightOfNullParse),
         Denominator = (exp(-WeightOfNullParse-WeightOfSingleViolation)+exp(-WeightOfNullParse-WeightOfSingleViolation)+exp(-2*WeightOfNullParse)),
         Ratio = Numerator/Denominator,
       Ratio_binned = case_when(Ratio < 0.99 ~ "Sublinear cumulativity",
                                     (Ratio > 0.99) & (Ratio < 1.01) ~ "Approximately linear cumulativity",
                                     Ratio > 1.01 ~ "Superlinear cumulativity")) %>% 
  mutate(prob_joint = (exp(-WeightOfSingleViolation-WeightOfSingleViolation))/(exp(-WeightOfNullParse)+exp(-WeightOfSingleViolation-WeightOfSingleViolation)),
         prob_doubly_violating = 
           (exp(-WeightOfSingleViolation-WeightOfSingleViolation))/(exp(-WeightOfSingleViolation-WeightOfSingleViolation)+exp(-WeightOfNullParse-WeightOfSingleViolation)+exp(-WeightOfNullParse-WeightOfSingleViolation)+exp(-2*WeightOfNullParse)),
         diffpred = (exp(-WeightOfNullParse-2*WeightOfSingleViolation-WeightOfSingleViolation)+exp(-WeightOfNullParse-WeightOfSingleViolation-2*WeightOfSingleViolation)+exp(-2*WeightOfNullParse-WeightOfSingleViolation-WeightOfSingleViolation)+exp(-WeightOfNullParse-WeightOfSingleViolation-WeightOfSingleViolation))/((exp(-WeightOfSingleViolation-WeightOfSingleViolation)+exp(-WeightOfNullParse))*(exp(-WeightOfNullParse-WeightOfSingleViolation)+exp(-WeightOfNullParse-WeightOfSingleViolation)+exp(-WeightOfSingleViolation-WeightOfSingleViolation)+exp(-2*WeightOfNullParse)))) %>% 
  select(PredictedProbDoublyViolating,JointProbDoublyViolating,prob_joint,prob_doubly_violating,diffpred, everything())

```


Plot

```{r}

rf <- colorRampPalette(rev(brewer.pal(11,'Spectral')))
r <- rf(32)
dat %>% 
  ggplot(aes(x=WeightOfNullParse,y=WeightOfSingleViolation,color = Ratio_binned))+
  geom_tile()#+scale_color_gradient(low = "blue",  high = "red")

ggsave("ratio_smaller.png")

dat %>% 
  ggplot(aes(x=WeightOfNullParse,y=WeightOfSingleViolation, z = Difference))+
  stat_contour_filled()+
  ylab("Weight of Markedness")+
  xlab("Weight of MParse")+
  theme_bw()+
  scale_fill_viridis(discrete = T, name = "Difference (predicted minus \njoint probability)")+
  theme(legend.position = "right")

ggsave("Weight_domain.jpeg",height = 5, width = 8)


```

```{r}
dat %>% 
  ggplot(aes(x=Difference,y=Ratio))+
  geom_point()

```





```{r}
dat %>% 
  select(WeightOfNullParse,PredictedProbSingleViolating,Difference,Cumulativity) %>%
  mutate(Cumulativity = factor(Cumulativity, levels = c("Sublinear cumulativity","Approximately linear cumulativity", "Superlinear cumulativity"),ordered = T)) %>% 
  #distinct() %>% 
  ggplot(aes(x=WeightOfNullParse,y=PredictedProbSingleViolating,  fill = Cumulativity, color = Cumulativity))+
  geom_point(size=.1, shape=15)+
  guides(color = guide_legend(override.aes = list(size = 10)))+
  ylab("Probability of singly-violating candidate")+
        xlab("Weight of MParse")+
  #coord_fixed()+
  scale_color_viridis(discrete = T)+
  theme_bw()+
  theme(legend.position = "bottom",
        legend.title = element_blank(),
        legend.direction = "horizontal")
      #geom_vline(xintercept=0.7)+
  #geom_hline(yintercept=0.25)
  #
ggsave("SimulationProbDomain.jpeg",height = 6.5, width = 6.5)
```


```{r}
dat %>% 
  ggplot(aes(x=PredictedProbSingleViolating,y=PredictedProbDoublyViolating,color = WeightOfNullParse))+
  geom_point()+scale_color_continuous(low="red", high = "blue")
```

```{r}
dat %>% 
  ggplot(aes(x=((2*WeightOfSingleViolation)-WeightOfNullParse),y=Difference,color = WeightOfNullParse))+
  geom_point()+scale_color_continuous(low="red", high = "blue")
```

```{r}


t <- dat %>% 
  filter(WeightOfNullParse == 2.5)
```

