```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE) 
# packages
library(ez)
library(reshape2)
library(reshape)
library(ggplot2)
library(plyr)
library(pastecs)
library(ez)
library(data.table)
library(tidyverse) 

library(showtext)
library(readr)
library(scales)

font_add_google("Poppins", "Poppins")
font_add_google("Roboto Mono", "Roboto Mono")
showtext_auto()
```


```{r}
explan_data <- read_delim("subj_explanations_main.csv", 
    delim = ";", escape_double = FALSE, trim_ws = TRUE)
```


# Data preparation

```{r, echo = FALSE}
# reorder factor 

# to create chart, the data must be in long format and only contain the relevant dependent variables

# 1. make a subset with only the relevant dvs

tdata_long <- explan_data


tdata_long$scale_orient <- factor(tdata_long$scale_orient, levels = c("narrow_left", "narrow_right"), 
                               labels = c("narrow_left", "narrow_right"))



# Recode ratings so that high values represent broad scope and low values represent narrow scope

dat_narrow_left <- subset(tdata_long, scale_orient == "narrow_left")
dat_narrow_left$rating_rec <- (dat_narrow_left$DV_rating) - 5

dat_narrow_right <- subset(tdata_long, scale_orient == "narrow_right")
dat_narrow_right$rating_rec <- (dat_narrow_right$DV_rating * -1) + 5

tdata_long <- rbind(dat_narrow_left, dat_narrow_right)



table(tdata_long$Features, tdata_long$Knowledge)

tdata_long$Features <- factor(tdata_long$Features, levels = c("spearNet", "featherTooth"), labels = c("Spear and net", "Feathers and tooth"))

tdata_long$Knowledge <- factor(tdata_long$Knowledge, levels = c("notKnow", "noInfo"), labels = c("'you don't know'", "no information"))

```


Note: Subjects' explanations were coded in the last columns (the column names describe the coded criterion). 1 means that the coding criterion is (clearly) met, 0 that it isn't. 

```{r}
# turn explanation coding values into factors 

tdata_long$`Inferred absence total` <- tdata_long$`Inferred absence of latent feature due to visibility` + tdata_long$`Inferred absence of latent feature for any reason`

explan_data <- tdata_long

explan_data$`Correct explanation` <- as.factor(explan_data$`Correct explanation`)

explan_data$`Unclear explanation` <- as.factor(explan_data$`Unclear explanation`)

explan_data$`Inferred absence of latent feature due to visibility` <- as.factor(explan_data$`Inferred absence of latent feature due to visibility`)

explan_data$`Inferred absence of latent feature for any reason` <- as.factor(explan_data$`Inferred absence of latent feature for any reason`)

explan_data$`Inferred absence total` <- as.factor(explan_data$`Inferred absence total`)

```


# Explanation Analysis

1. Proportions of subjects in each feature condition who gave (correct) explanations for why the answer is 50:50

```{r}
# create a summary dataset that also contains the percentages
plotdata_between <- explan_data %>%
  group_by(Features, `Correct explanation`) %>%
  summarize(n = n()) %>% 
  mutate(pct = n/sum(n),
         lbl = scales::percent(pct))


plotdata_between

plotdata_sub <- subset(plotdata_between, `Correct explanation` == 0)
```



```{r}
plotdata <- plotdata_between

g<- ggplot(plotdata, 
       aes(x = Features,
           y = pct,
           fill = `Correct explanation`)) +
  #facet_grid( ~ Features)+
  geom_bar(stat = "identity",
           position = "fill") +
  scale_y_continuous(limits = seq(0, 2),
                     breaks = seq(0, 1, .25),
                     expand = c(0,0),
                     label = percent) +
  #scale_x_discrete(labels = c("not \nmentioned", "'you don't \nknow'"))+
  coord_cartesian(xlim =c(1, 2), ylim = c(0, 1.1))+
  #coord_cartesian(clip = "off")+
  geom_text(aes(label = lbl), 
            size = 4.5,
            position = position_stack(vjust = 0.5)) +
  scale_fill_brewer(palette = "Pastel1") +
  labs(y = "Percentage", 
       fill = "Correct Explanation",
       x = "Features")+
  theme(legend.position = "top", axis.title = element_text(size = 15), axis.text = element_text(size = 13, color = "black"),
        legend.text = element_text(size = 13),legend.title = element_text(size = 13))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

g
```

```{r}
prop.test(x = c(plotdata$n[2], plotdata$n[4]), n = c(50, 50), alternative = "less", correct = F)
```

2. Proportions of subjects in the different feature conditions whose explanation indicated that the unobserved feature is more likely to be absent because of "feature visibility" (i.e., because it should have been observed if it was actually present)

```{r}
# create a summary dataset that also contains the percentages
plotdata_between <- explan_data %>%
  group_by(Features, `Inferred absence of latent feature due to visibility`) %>%
  summarize(n = n()) %>% 
  mutate(pct = n/sum(n),
         lbl = scales::percent(pct))


plotdata_between

plotdata_sub <- subset(plotdata_between, `Inferred absence of latent feature due to visibility` == 1)
```


```{r}
plotdata <- plotdata_between

g<- ggplot(plotdata, 
       aes(x = Features,
           y = pct,
           fill = `Inferred absence of latent feature due to visibility`)) +
  #facet_grid( ~ Features)+
  geom_bar(stat = "identity",
           position = "fill") +
  scale_y_continuous(limits = seq(0, 2),
                     breaks = seq(0, 1, .25),
                     expand = c(0,0),
                     label = percent) +
  #scale_x_discrete(labels = c("not \nmentioned", "'you don't \nknow'"))+
  coord_cartesian(xlim =c(1, 2), ylim = c(0, 1.1))+
  #coord_cartesian(clip = "off")+
  geom_text(aes(label = lbl), 
            size = 4.5,
            position = position_stack(vjust = 0.5)) +
  scale_fill_brewer(palette = "Pastel1") +
  labs(y = "Percentage", 
       fill = "Inferred absence of \nunobserved feature \ndue to visibility",
       x = "Features")+
  theme(legend.position = "top", axis.title = element_text(size = 15), axis.text = element_text(size = 13, color = "black"),
        legend.text = element_text(size = 13),legend.title = element_text(size = 13))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

g
```

```{r}
prop.test(x = c(plotdata$n[2], 0), n = c(50, 50), alternative = "greater", correct = F)
```
3. proportions of subjects who inferred the absence of the unobserved feature for whatever reason 

```{r}
# create a summary dataset that also contains the percentages
plotdata_between <- explan_data %>%
  group_by(Features, `Inferred absence total`) %>%
  summarize(n = n()) %>% 
  mutate(pct = n/sum(n),
         lbl = scales::percent(pct))


plotdata_between

plotdata_sub <- subset(plotdata_between, `Inferred absence total` == 1)
```



```{r}
plotdata <- plotdata_between

g<- ggplot(plotdata, 
       aes(x = Features,
           y = pct,
           fill = `Inferred absence total`)) +
  #facet_grid( ~ Features)+
  geom_bar(stat = "identity",
           position = "fill") +
  scale_y_continuous(limits = seq(0, 2),
                     breaks = seq(0, 1, .25),
                     expand = c(0,0),
                     label = percent) +
  #scale_x_discrete(labels = c("not \nmentioned", "'you don't \nknow'"))+
  coord_cartesian(xlim =c(1, 2), ylim = c(0, 1.1))+
  #coord_cartesian(clip = "off")+
  geom_text(aes(label = lbl), 
            size = 4.5,
            position = position_stack(vjust = 0.5)) +
  scale_fill_brewer(palette = "Pastel1") +
  labs(y = "Percentage", 
       fill = "Inferred absence of unobserved feature (total)",
       x = "Features")+
  theme(legend.position = "top", axis.title = element_text(size = 15), axis.text = element_text(size = 13, color = "black"),
        legend.text = element_text(size = 13),legend.title = element_text(size = 13))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())

g
```



```{r}
prop.test(x = c(plotdata$n[2], 0), n = c(50, 50), alternative = "greater", correct = F)
```
