Load libraries

source("~/Projects/Other/Ranalysis/useful_dplyr.R")
library(tidyr)
library(magrittr)
library(jsonlite)

Load data

all.result.files <- paste("brief_data/",
                          list.files(path = "brief_data/", pattern = '*.json', 
                                     all.files = FALSE), sep="");
jsons <- lapply(all.result.files,fromJSON)
workers <- sapply(jsons,function(x) x$WorkerId)
data <- bind_rows(sapply(jsons,function(x) x$answers$data,simplify=FALSE))

data$worker <- unlist(lapply(workers, function (x) rep(x,5)))

Exclusions

#data <- filter(data,worker != "A30D0ZW3Y1YMEW") #drop Mike
total.subjs <- nrow(data)/5

practice.data <- data %>%
  filter(practiceOrExp == "practice") %>%
  group_by(worker,trialType) %>%
  mutate(correct = if(trialType == "FirstLabel" & response == "cup") TRUE
         else if(trialType == "SecondLabel" & response == "truck") TRUE
         else FALSE) %>%
  group_by(worker) %>%
  summarise(correct = sum(correct)) %>%
  filter(correct == 2)

passedPractice = 1- (total.subjs-nrow(practice.data))/total.subjs
print(passedPractice)
## [1] 0.95

Does it blend?

test.data <- data %>%
  filter(practiceOrExp == "exp", worker %in% practice.data$worker) %>%
  rowwise() %>%
  mutate(response = if(response == FirstObj) "FirstObj"
         else if(response == SecondObj) "SecondObj"
         else if(response == BothObj)"BothObj"
         else NA) %>%
  mutate(response = factor(response,levels=c("FirstObj","SecondObj","BothObj")),
         trialType = factor(trialType,levels=c("FirstLabel","SecondLabel","New Label")))

# Compute proportions
ms <- test.data %>%
  group_by(trialType,response) %>%
  summarise(n = n()) %>%
  mutate(prop = n/sum(n))

# Does it blend?
ggplot(ms,aes(x=trialType, y=prop,fill=response))+
  geom_bar(stat="identity",position="dodge") +
  scale_x_discrete(name = "Trial Type") +
  scale_y_continuous(name = "Proportion Adults Choosing",limits=c(0,1))+
  theme_bw(base_size=14) +
  theme(legend.position=c(.8,.8), panel.grid=element_blank()) +
  scale_fill_brewer(palette="Set1")

Compute proportions by order?

all.resps <- expand.grid(order = unique(test.data$order), 
            trialType = unique(test.data$trialType),
            response = unique(test.data$response)) %>%
  mutate(n = 0)

ms.order <- test.data %>%
  group_by(order,trialType,response) %>%
  summarise(n = n()) %>%
  full_join(all.resps) %>%
  mutate(prop = n/sum(n))


ggplot(ms.order,aes(x=trialType, y=prop,fill=response))+
  geom_bar(stat="identity",position="dodge") +
  facet_wrap(~ order)+
  scale_x_discrete(name = "Trial Type") +
  scale_y_continuous(name = "Proportion Adults Choosing",limits=c(0,1))+
  theme_bw(base_size=14) +
  theme(legend.position=c(.9,.85), panel.grid=element_blank()) +
  scale_fill_brewer(palette="Set1")