Skip to content

Latest commit

 

History

History
1192 lines (1017 loc) · 50.1 KB

FoodIE_norming.md

File metadata and controls

1192 lines (1017 loc) · 50.1 KB

load packages

library(tidyverse)
library(psych)

load data

data = read.csv("~/Documents/code/dsnlab/FoodIE/FoodIE_Norming.csv", header=TRUE, stringsAsFactors = FALSE)

rename columns

colnames(data) = gsub("_1", "", colnames(data), fixed = TRUE)

data = data %>%
  select(-mTurkCode, -starts_with("V"), -starts_with("consent"), -starts_with("craved")) %>%
  rename("attn_2_burgers_report" = attn_2_burgers_repor,
         "attn_chocolate_report" = attn_chocolate_repor,
         "attn_2_chocolate_report" = attn_2_chocolate_rep,
         "attn_2_cookies_report" = attn_2_cookies_repor,
         "attn_fruitDesserts" = attn_fruitD,
         "attn_fruitDesserts_report" = attn_fruitD_report,
         "attn_2_fruitDesserts" = attn_2_fruitD,
         "attn_2_fruitDesserts_report" = attn_2_fruitD_report,
         "attn_2_iceCream_report" = attn_2_iceCream_repo,
         "attn_2_veggies_report" = attn_2_veggies_repor,
         "desire_fruitDesserts" = desire_fruitD,
         "exemplar_fruitDesserts" = exemplar_fruitD,
         "feedback_fruitDesserts" = feedback_fruitD)

colnames(data) = gsub("Q4.", "bbq", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q5.", "burgers", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q6.", "candy", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q7.", "cheese", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q8.", "chips", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q9.", "chocolate", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q10.", "cookies", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q11.", "donuts", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q12.", "fries", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q13.", "fruit", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q14.", "fruitDesserts", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q15.", "iceCream", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q16.", "pasta", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q17.", "pizza", colnames(data), fixed = TRUE)
colnames(data) = gsub("Q18.", "veggies", colnames(data), fixed = TRUE)

tidy and recode

data1 = data %>% 
  # select only completed responses
  filter(Completed == 1 & grepl("^A", workerID)) %>%
  # convert text to lower case
  mutate_all(tolower) %>%
  # rename IDs
  mutate(workerID = paste0("A",row.names(.))) %>%
  # change to numeric values
  mutate_at(vars(contains("desire")), as.numeric) %>%
  mutate_at(vars(contains("rate")), as.numeric) %>%
  mutate_at(vars(contains("exemplar")), as.numeric)

bbq

# select participants that completed this questionnaire
bbq = data1 %>%
  select(workerID, rate_bbq, desire_bbq, exemplar_bbq, feedback_bbq, attn_bbq_report, attn_2_bbq_report, starts_with("bbq")) %>%
  filter(bbq1 %in% c(1,2,3,4,5))

# rename columns
names(bbq)[8:length(bbq)] = sprintf("bbq%02d",1:(length(bbq)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = bbq %>%
  mutate(fail = ifelse(!grepl("jelly|candy", attn_bbq_report) & !grepl("cheese", attn_2_bbq_report), 1, 0), #attention check
         fail = ifelse(rate_bbq <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_bbq == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_bbq == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_bbq, desire_bbq, exemplar_bbq, feedback_bbq, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
bbq.ex = bbq %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_bbq, -exemplar_bbq, -feedback_bbq) %>%
  mutate_at(vars(contains("bbq")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("bbq")), na.rm=TRUE)) %>%
  select(workerID, rate_bbq, mean, everything())

# summarize 
bbq.long = bbq.ex %>%
  gather(image, rating, starts_with("bbq")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

bbq.summary = bbq.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(bbq.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(bbq.ex, "~/Documents/code/dsnlab/FoodIE/ratings/bbq.csv", row.names = FALSE)
write.csv(bbq.summary, "~/Documents/code/dsnlab/FoodIE/summaries/bbq.csv", row.names = FALSE)

burgers

# select participants that completed this questionnaire
burgers = data1 %>%
  select(workerID, rate_burgers, desire_burgers, exemplar_burgers, feedback_burgers, attn_burgers_report, attn_2_burgers_report, starts_with("burgers")) %>%
  filter(burgers1 %in% c(1,2,3,4,5))

# rename columns
names(burgers)[8:length(burgers)] = sprintf("burgers%02d",1:(length(burgers)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = burgers %>%
  mutate(fail = ifelse(!grepl("chips", attn_burgers_report) & !grepl("chocolate", attn_2_burgers_report), 1, 0), #attention check
         fail = ifelse(rate_burgers <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_burgers == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_burgers == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_burgers, desire_burgers, exemplar_burgers, feedback_burgers, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
burgers.ex = burgers %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_burgers, -exemplar_burgers, -feedback_burgers) %>%
  mutate_at(vars(contains("burgers")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("burgers")), na.rm=TRUE)) %>%
  select(workerID, rate_burgers, mean, everything())

# summarize 
burgers.long = burgers.ex %>%
  gather(image, rating, starts_with("burgers")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

burgers.summary = burgers.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(burgers.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(burgers.ex, "~/Documents/code/dsnlab/FoodIE/ratings/burgers.csv", row.names = FALSE)
write.csv(burgers.summary, "~/Documents/code/dsnlab/FoodIE/summaries/burgers.csv", row.names = FALSE)

candy

# select participants that completed this questionnaire
candy = data1 %>%
  select(workerID, rate_candy, desire_candy, exemplar_candy, feedback_candy, attn_candy_report, attn_2_candy_report, starts_with("candy")) %>%
  filter(candy1 %in% c(1,2,3,4,5))

# rename columns
names(candy)[8:length(candy)] = sprintf("candy%02d",1:(length(candy)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = candy %>%
  mutate(fail = ifelse(!grepl("dough|donut|fries", attn_candy_report) & !grepl("dough|donut|fries", attn_2_candy_report), 1, 0), #attention check
         fail = ifelse(rate_candy <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_candy == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_candy == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_candy, desire_candy, exemplar_candy, feedback_candy, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
candy.ex = candy %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_candy, -exemplar_candy, -feedback_candy) %>%
  mutate_at(vars(contains("candy")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("candy")), na.rm=TRUE)) %>%
  select(workerID, rate_candy, mean, everything())

# summarize 
candy.long = candy.ex %>%
  gather(image, rating, starts_with("candy")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

candy.summary = candy.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(candy.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(candy.ex, "~/Documents/code/dsnlab/FoodIE/ratings/candy.csv", row.names = FALSE)
write.csv(candy.summary, "~/Documents/code/dsnlab/FoodIE/summaries/candy.csv", row.names = FALSE)

cheese

# select participants that completed this questionnaire
cheese = data1 %>%
  select(workerID, rate_cheese, desire_cheese, exemplar_cheese, feedback_cheese, attn_cheese_report, attn_2_cheese_report, starts_with("cheese")) %>%
  filter(cheese1 %in% c(1,2,3,4,5))

# rename columns
names(cheese)[8:length(cheese)] = sprintf("cheese%02d",1:(length(cheese)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = cheese %>%
  mutate(fail = ifelse(!grepl("pear", attn_cheese_report) & !grepl("cream", attn_2_cheese_report), 1, 0), #attention check
         fail = ifelse(rate_cheese <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_cheese == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_cheese == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_cheese, desire_cheese, exemplar_cheese, feedback_cheese, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
cheese.ex = cheese %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_cheese, -exemplar_cheese, -feedback_cheese) %>%
  mutate_at(vars(contains("cheese")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("cheese")), na.rm=TRUE)) %>%
  select(workerID, rate_cheese, mean, everything())

# summarize 
cheese.long = cheese.ex %>%
  gather(image, rating, starts_with("cheese")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

cheese.summary = cheese.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(cheese.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(cheese.ex, "~/Documents/code/dsnlab/FoodIE/ratings/cheese.csv", row.names = FALSE)
write.csv(cheese.summary, "~/Documents/code/dsnlab/FoodIE/summaries/cheese.csv", row.names = FALSE)

chips

# select participants that completed this questionnaire
chips = data1 %>%
  select(workerID, rate_chips, desire_chips, exemplar_chips, feedback_chips, attn_chips_report, attn_2_chips_report, starts_with("chips")) %>%
  filter(chips1 %in% c(1,2,3,4,5))

# rename columns
names(chips)[8:length(chips)] = sprintf("chips%02d",1:(length(chips)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = chips %>%
  mutate(fail = ifelse(!grepl("pasta|fettucini", attn_chips_report) & !grepl("pizza", attn_2_chips_report), 1, 0), #attention check
         fail = ifelse(rate_chips <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_chips == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_chips == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_chips, desire_chips, exemplar_chips, feedback_chips, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
chips.ex = chips %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_chips, -exemplar_chips, -feedback_chips) %>%
  mutate_at(vars(contains("chips")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("chips")), na.rm=TRUE)) %>%
  select(workerID, rate_chips, mean, everything())

# summarize 
chips.long = chips.ex %>%
  gather(image, rating, starts_with("chips")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

chips.summary = chips.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(chips.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(chips.ex, "~/Documents/code/dsnlab/FoodIE/ratings/chips.csv", row.names = FALSE)
write.csv(chips.summary, "~/Documents/code/dsnlab/FoodIE/summaries/chips.csv", row.names = FALSE)

chocolate

# select participants that completed this questionnaire
chocolate = data1 %>%
  select(workerID, rate_chocolate, desire_chocolate, exemplar_chocolate, feedback_chocolate, attn_chocolate_report, attn_2_chocolate_report, starts_with("chocolate")) %>%
  filter(chocolate1 %in% c(1,2,3,4,5))

# rename columns
names(chocolate)[8:length(chocolate)] = sprintf("chocolate%02d",1:(length(chocolate)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = chocolate %>%
  mutate(fail = ifelse(!grepl("corn", attn_chocolate_report) & !grepl("ribs|meat", attn_2_chocolate_report), 1, 0), #attention check
         fail = ifelse(rate_chocolate <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_chocolate == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_chocolate == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_chocolate, desire_chocolate, exemplar_chocolate, feedback_chocolate, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
chocolate.ex = chocolate %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_chocolate, -exemplar_chocolate, -feedback_chocolate) %>%
  mutate_at(vars(contains("chocolate")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("chocolate")), na.rm=TRUE)) %>%
  select(workerID, rate_chocolate, mean, everything())

# summarize 
chocolate.long = chocolate.ex %>%
  gather(image, rating, starts_with("chocolate")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

chocolate.summary = chocolate.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(chocolate.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(chocolate.ex, "~/Documents/code/dsnlab/FoodIE/ratings/chocolate.csv", row.names = FALSE)
write.csv(chocolate.summary, "~/Documents/code/dsnlab/FoodIE/summaries/chocolate.csv", row.names = FALSE)

cookies

# select participants that completed this questionnaire
cookies = data1 %>%
  select(workerID, rate_cookies, desire_cookies, exemplar_cookies, feedback_cookies, attn_cookies_report, attn_2_cookies_report, starts_with("cookies")) %>%
  filter(cookies1 %in% c(1,2,3,4,5))

# rename columns
names(cookies)[8:length(cookies)] = sprintf("cookies%02d",1:(length(cookies)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = cookies %>%
  mutate(fail = ifelse(!grepl("burger", attn_cookies_report) & !grepl("jelly|candy", attn_2_cookies_report), 1, 0), #attention check
         fail = ifelse(rate_cookies <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_cookies == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_cookies == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_cookies, desire_cookies, exemplar_cookies, feedback_cookies, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
cookies.ex = cookies %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_cookies, -exemplar_cookies, -feedback_cookies) %>%
  mutate_at(vars(contains("cookies")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("cookies")), na.rm=TRUE)) %>%
  select(workerID, rate_cookies, mean, everything())

# summarize 
cookies.long = cookies.ex %>%
  gather(image, rating, starts_with("cookies")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

cookies.summary = cookies.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(cookies.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(cookies.ex, "~/Documents/code/dsnlab/FoodIE/ratings/cookies.csv", row.names = FALSE)
write.csv(cookies.summary, "~/Documents/code/dsnlab/FoodIE/summaries/cookies.csv", row.names = FALSE)

donuts

# select participants that completed this questionnaire
donuts = data1 %>%
  select(workerID, rate_donuts, desire_donuts, exemplar_donuts, feedback_donuts, attn_donuts_report, attn_2_donuts_report, starts_with("donuts")) %>%
  filter(donuts1 %in% c(1,2,3,4,5))

# rename columns
names(donuts)[8:length(donuts)] = sprintf("donuts%02d",1:(length(donuts)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = donuts %>%
  mutate(fail = ifelse(!grepl("cheese", attn_donuts_report) & !grepl("chips|cheese", attn_2_donuts_report), 1, 0), #attention check
         fail = ifelse(rate_donuts <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_donuts == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_donuts == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_donuts, desire_donuts, exemplar_donuts, feedback_donuts, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
donuts.ex = donuts %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_donuts, -exemplar_donuts, -feedback_donuts) %>%
  mutate_at(vars(contains("donuts")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("donuts")), na.rm=TRUE)) %>%
  select(workerID, rate_donuts, mean, everything())

# summarize 
donuts.long = donuts.ex %>%
  gather(image, rating, starts_with("donuts")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

donuts.summary = donuts.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(donuts.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(donuts.ex, "~/Documents/code/dsnlab/FoodIE/ratings/donuts.csv", row.names = FALSE)
write.csv(donuts.summary, "~/Documents/code/dsnlab/FoodIE/summaries/donuts.csv", row.names = FALSE)

fries

# select participants that completed this questionnaire
fries = data1 %>%
  select(workerID, rate_fries, desire_fries, exemplar_fries, feedback_fries, attn_fries_report, attn_2_fries_report, starts_with("fries")) %>%
  filter(fries1 %in% c(1,2,3,4,5))

# rename columns
names(fries)[8:length(fries)] = sprintf("fries%02d",1:(length(fries)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = fries %>%
  mutate(fail = ifelse(!grepl("chocolate|choclate|candy", attn_fries_report) & !grepl("cookie", attn_2_fries_report), 1, 0), #attention check
         fail = ifelse(rate_fries <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_fries == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_fries == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_fries, desire_fries, exemplar_fries, feedback_fries, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
fries.ex = fries %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_fries, -exemplar_fries, -feedback_fries) %>%
  mutate_at(vars(contains("fries")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("fries")), na.rm=TRUE)) %>%
  select(workerID, rate_fries, mean, everything())

# summarize 
fries.long = fries.ex %>%
  gather(image, rating, starts_with("fries")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

fries.summary = fries.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(fries.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(fries.ex, "~/Documents/code/dsnlab/FoodIE/ratings/fries.csv", row.names = FALSE)
write.csv(fries.summary, "~/Documents/code/dsnlab/FoodIE/summaries/fries.csv", row.names = FALSE)

fruit

# select participants that completed this questionnaire
fruit = data1 %>%
  select(workerID, rate_fruit, desire_fruit, exemplar_fruit, feedback_fruit, attn_fruit_report, attn_2_fruit_report, matches("fruit[0-9]{1,2}")) %>%
  filter(fruit1 %in% c(1,2,3,4,5))

# rename columns
names(fruit)[8:length(fruit)] = sprintf("fruit%02d",1:(length(fruit)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = fruit %>%
  mutate(fail = ifelse(!grepl("dough|donut", attn_fruit_report) & !grepl("fries", attn_2_fruit_report), 1, 0), #attention check
         fail = ifelse(rate_fruit <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_fruit == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_fruit == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_fruit, desire_fruit, exemplar_fruit, feedback_fruit, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
fruit.ex = fruit %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_fruit, -exemplar_fruit, -feedback_fruit) %>%
  mutate_at(vars(contains("fruit")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("fruit")), na.rm=TRUE)) %>%
  select(workerID, rate_fruit, mean, everything())

# summarize 
fruit.long = fruit.ex %>%
  gather(image, rating, starts_with("fruit")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

fruit.summary = fruit.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(fruit.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(fruit.ex, "~/Documents/code/dsnlab/FoodIE/ratings/fruit.csv", row.names = FALSE)
write.csv(fruit.summary, "~/Documents/code/dsnlab/FoodIE/summaries/fruit.csv", row.names = FALSE)

fruitDesserts

# select participants that completed this questionnaire
fruitDesserts = data1 %>%
  select(workerID, rate_fruitDesserts, desire_fruitDesserts, exemplar_fruitDesserts, feedback_fruitDesserts, attn_fruitDesserts_report, attn_2_fruitDesserts_report, starts_with("fruitDesserts")) %>%
  filter(fruitDesserts1 %in% c(1,2,3,4,5))

# rename columns
names(fruitDesserts)[8:length(fruitDesserts)] = sprintf("fruitDesserts%02d",1:(length(fruitDesserts)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = fruitDesserts %>%
  mutate(fail = ifelse(!grepl("dough|donut", attn_fruitDesserts_report) & !grepl("cream", attn_2_fruitDesserts_report), 1, 0), #attention check
         fail = ifelse(rate_fruitDesserts <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_fruitDesserts == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_fruitDesserts == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_fruitDesserts, desire_fruitDesserts, exemplar_fruitDesserts, feedback_fruitDesserts, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
fruitDesserts.ex = fruitDesserts %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_fruitDesserts, -exemplar_fruitDesserts, -feedback_fruitDesserts) %>%
  mutate_at(vars(contains("fruitDesserts")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("fruitDesserts")), na.rm=TRUE)) %>%
  select(workerID, rate_fruitDesserts, mean, everything())

# summarize 
fruitDesserts.long = fruitDesserts.ex %>%
  gather(image, rating, starts_with("fruitDesserts")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

fruitDesserts.summary = fruitDesserts.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(fruitDesserts.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(fruitDesserts.ex, "~/Documents/code/dsnlab/FoodIE/ratings/fruitDesserts.csv", row.names = FALSE)
write.csv(fruitDesserts.summary, "~/Documents/code/dsnlab/FoodIE/summaries/fruitDesserts.csv", row.names = FALSE)

iceCream

# select participants that completed this questionnaire
iceCream = data1 %>%
  select(workerID, rate_iceCream, desire_iceCream, exemplar_iceCream, feedback_iceCream, attn_iceCream_report, attn_2_iceCream_report, starts_with("iceCream")) %>%
  filter(iceCream1 %in% c(1,2,3,4,5))

# rename columns
names(iceCream)[8:length(iceCream)] = sprintf("iceCream%02d",1:(length(iceCream)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = iceCream %>%
  mutate(fail = ifelse(!grepl("pasta", attn_iceCream_report) & !grepl("corn", attn_2_iceCream_report), 1, 0), #attention check
         fail = ifelse(rate_iceCream <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_iceCream == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_iceCream == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_iceCream, desire_iceCream, exemplar_iceCream, feedback_iceCream, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
iceCream.ex = iceCream %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_iceCream, -exemplar_iceCream, -feedback_iceCream) %>%
  mutate_at(vars(contains("iceCream")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("iceCream")), na.rm=TRUE)) %>%
  select(workerID, rate_iceCream, mean, everything())

# summarize 
iceCream.long = iceCream.ex %>%
  gather(image, rating, starts_with("iceCream")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

iceCream.summary = iceCream.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(iceCream.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(iceCream.ex, "~/Documents/code/dsnlab/FoodIE/ratings/iceCream.csv", row.names = FALSE)
write.csv(iceCream.summary, "~/Documents/code/dsnlab/FoodIE/summaries/iceCream.csv", row.names = FALSE)

pasta

# select participants that completed this questionnaire
pasta = data1 %>%
  select(workerID, rate_pasta, desire_pasta, exemplar_pasta, feedback_pasta, attn_pasta_report, attn_2_pasta_report, starts_with("pasta")) %>%
  filter(pasta1 %in% c(1,2,3,4,5))

# rename columns
names(pasta)[8:length(pasta)] = sprintf("pasta%02d",1:(length(pasta)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = pasta %>%
  mutate(fail = ifelse(!grepl("pizza", attn_pasta_report) & !grepl("ribs|meat|pizza", attn_2_pasta_report), 1, 0), #attention check
         fail = ifelse(rate_pasta <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_pasta == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_pasta == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_pasta, desire_pasta, exemplar_pasta, feedback_pasta, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
pasta.ex = pasta %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_pasta, -exemplar_pasta, -feedback_pasta) %>%
  mutate_at(vars(contains("pasta")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("pasta")), na.rm=TRUE)) %>%
  select(workerID, rate_pasta, mean, everything())

# summarize 
pasta.long = pasta.ex %>%
  gather(image, rating, starts_with("pasta")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

pasta.summary = pasta.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(pasta.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(pasta.ex, "~/Documents/code/dsnlab/FoodIE/ratings/pasta.csv", row.names = FALSE)
write.csv(pasta.summary, "~/Documents/code/dsnlab/FoodIE/summaries/pasta.csv", row.names = FALSE)

pizza

# select participants that completed this questionnaire
pizza = data1 %>%
  select(workerID, rate_pizza, desire_pizza, exemplar_pizza, feedback_pizza, attn_pizza_report, attn_2_pizza_report, starts_with("pizza")) %>%
  filter(pizza1 %in% c(1,2,3,4,5))

# rename columns
names(pizza)[8:length(pizza)] = sprintf("pizza%02d",1:(length(pizza)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = pizza %>%
  mutate(fail = ifelse(!grepl("burger", attn_pizza_report) & !grepl("cheese", attn_2_pizza_report), 1, 0), #attention check
         fail = ifelse(rate_pizza <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_pizza == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_pizza == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_pizza, desire_pizza, exemplar_pizza, feedback_pizza, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
pizza.ex = pizza %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_pizza, -exemplar_pizza, -feedback_pizza) %>%
  mutate_at(vars(contains("pizza")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("pizza")), na.rm=TRUE)) %>%
  select(workerID, rate_pizza, mean, everything())

# summarize 
pizza.long = pizza.ex %>%
  gather(image, rating, starts_with("pizza")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

pizza.summary = pizza.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(pizza.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(pizza.ex, "~/Documents/code/dsnlab/FoodIE/ratings/pizza.csv", row.names = FALSE)
write.csv(pizza.summary, "~/Documents/code/dsnlab/FoodIE/summaries/pizza.csv", row.names = FALSE)

veggies

# select participants that completed this questionnaire
veggies = data1 %>%
  select(workerID, rate_veggies, desire_veggies, exemplar_veggies, feedback_veggies, attn_veggies_report, attn_2_veggies_report, starts_with("veggies")) %>%
  filter(veggies1 %in% c(1,2,3,4,5))

# rename columns
names(veggies)[8:length(veggies)] = sprintf("veggies%02d",1:(length(veggies)-7))

# exclude participants that:
# * failed attention checks
# * didn't rate their craving for the foods above 3
# * didn't think the images were desirable
# * didn't think the images were representative of the category
failed = veggies %>%
  mutate(fail = ifelse(!grepl("ribs|meat", attn_veggies_report) & !grepl("pasta|noodles|meat", attn_2_veggies_report), 1, 0), #attention check
         fail = ifelse(rate_veggies <= 3 , 1, fail), #craving of food category
         fail = ifelse(desire_veggies == 3 , 1, fail), #desirability
         fail = ifelse(exemplar_veggies == 3 , 1, fail)) %>% #representative
  arrange(desc(fail)) %>%
  select(workerID, fail, rate_veggies, desire_veggies, exemplar_veggies, feedback_veggies, starts_with("attn"))

exclusions = failed %>%
  filter(fail == 1) %>%
  .$workerID

# exclude participants and calculate participant mean
veggies.ex = veggies %>%
  filter(!workerID %in% exclusions) %>%
  select(-starts_with("attn"), -desire_veggies, -exemplar_veggies, -feedback_veggies) %>%
  mutate_at(vars(contains("veggies")), as.numeric) %>%
  mutate(mean = rowMeans(select(., starts_with("veggies")), na.rm=TRUE)) %>%
  select(workerID, rate_veggies, mean, everything())

# summarize 
veggies.long = veggies.ex %>%
  gather(image, rating, starts_with("veggies")) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  mutate(grand_mean = mean(rating, na.rm = TRUE)) %>%
  group_by(image) %>%
  mutate(mean_rating = mean(rating, na.rm = TRUE),
         diff = mean_rating - grand_mean)

veggies.summary = veggies.long %>%
  group_by(image) %>%
  summarize(mean = mean(rating, na.rm = TRUE),
            sd = sd(rating, na.rm = TRUE),
            n = n()) %>%
  extract(image,"sort","[a-z]([0-9]{1,2})", remove = FALSE) %>%
  arrange(as.numeric(sort)) %>%
  select(-sort)

# plot
palette = c("#3B9AB2", "#EBCC2A", "#F21A00")
ggplot(veggies.long, aes(reorder(sort, as.numeric(sort)), rating, color = diff)) +
  stat_summary() + 
  labs(x = "image number",
      y = "mean rating",
      color = "difference from grand mean") + 
  scale_color_gradient2(low = palette[1], mid = palette[2], high = palette[3]) + 
  coord_cartesian(ylim = c(2,5)) +
  theme_minimal() + 
  theme(legend.direction = "horizontal", 
        legend.justification=c(1,1),
        legend.position=c(1,1))

# write .csv files
write.csv(veggies.ex, "~/Documents/code/dsnlab/FoodIE/ratings/veggies.csv", row.names = FALSE)
write.csv(veggies.summary, "~/Documents/code/dsnlab/FoodIE/summaries/veggies.csv", row.names = FALSE)