Skip to content

Instantly share code, notes, and snippets.

@padpadpadpad
Last active February 4, 2017 16:19
Show Gist options
  • Save padpadpadpad/3a05dbdb066f1da0ffb09f10b30b79d9 to your computer and use it in GitHub Desktop.
Save padpadpadpad/3a05dbdb066f1da0ffb09f10b30b79d9 to your computer and use it in GitHub Desktop.
Some prawns and some colours and some stats
# good coding practice ####
# 1. #hashtag your code so you know what it does
# 2. clear workspace and load packages at the top to keep track of what you have loaded
# 3. make sure your working directory is in the right place
# 4. space things out in a way that makes your code readable to you
# 5. google things you do not understand. The answers are out there, go find them
# 6. do not get scared/angry when you get errors. It does get easier.... eventually
# clear workspace #### Good code practice to do first
rm(list = ls())
# set working directory - do not need to do here ####
#setwd("~/where/your/stuff/is")
# load packages ####
# if you do not have these packages - install.packages('package name')
library(dplyr)
library(tidyr)
library(ggplot2)
# create some dummy data ####
# set possible prawn colours
col_prawn <- c('duck_egg_blue', 'orange')
# set possible seaweed colours
col_seaweed <- c('duck_egg_blue', 'orange')
# set seaweed types
# whether you have artificial or natural seaweed
seaweed_type <- c('natural', 'artificial')
# create dataframe with equal numbers of prawns in
# randomly choose values between 0 (no choice), 1 (duck egg blue), 2 (orange)
d <- data.frame(expand.grid(col_prawn = rep(col_prawn, each = 30), seaweed_type = seaweed_type), stringsAsFactors = FALSE) %>%
mutate(., choice = rbinom(n(), size = 2, prob = 0.5))
# create a new variable using mutate for
d <- mutate(d, col_choice = plyr::mapvalues(choice, from = c(0,1,2), to = c('no_choice', 'duck_egg_blue', 'orange')))
# create new variable that denotes whether each individual has made the correct choice or not
# for orange prawns, correct choice is orange seaweed, wrong choice is duck egg blue seaweed or no choice
d <- mutate(d, is_correct = ifelse(col_choice == col_prawn, 1, 0))
# quickplot the data ####
# ggplot adds layers on top of each other using a +
ggplot(d, aes(col_prawn, fill = col_choice)) +
geom_bar(position = 'dodge', col = 'black') +
facet_wrap(~ seaweed_type) +
theme_bw() +
scale_fill_manual(values = c('turquoise2', 'white', 'orange'))
# quickplot of right choice or not
# quick plot of data
ggplot(d, aes(x = col_prawn, y = is_correct)) +
geom_jitter(alpha = 0.5, width = 0.1, height = 0.1) +
theme_bw() +
facet_wrap(~ seaweed_type)
# make factors in our data characters instead ####
str(d)
# col_prawn and seaweed_type are factors
d <- mutate_at(d, c('col_prawn', 'seaweed_type'), as.character)
# model choice, correct or not, against prawn colour and natural vs artifical seaweed
fit <- glm(is_correct ~ col_prawn*seaweed_type,
data = d,
family = 'binomial')
# diagnostic plots
plot(fit)
# model summary:
summary(fit)
# DO ANY MODEL SELECTION AS NECESSARY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment