Skip to content

Instantly share code, notes, and snippets.

@lukereding
Last active May 11, 2018 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lukereding/bbd102a1435cb777d3a9caea74d50637 to your computer and use it in GitHub Desktop.
Save lukereding/bbd102a1435cb777d3a9caea74d50637 to your computer and use it in GitHub Desktop.
library(tidyverse)
## testing overall incidence of intransitivity
# number of intransitive fish in each experiment per the results
intransitive <- c(9, 6, 6)
# total number of fish in each experiment
total <- c(10, 11, 23) + intransitive
# test whether, across all experiments, there are more intransitive fish than expected by chance
binom.test(x = sum(intransitive), n = sum(total), p = 0.25)
# result:
# Exact binomial test
#
# data: sum(intransitive) and sum(total)
# number of successes = 21, number of trials = 65, p-value = 0.1964
# alternative hypothesis: true probability of success is not equal to 0.25
# 95 percent confidence interval:
# 0.2123276 0.4505496
# sample estimates:
# probability of success
# 0.3230769
# we can do a proportion test to see whether the proportion of intransitive fish
# is different across the three experiments
prop.test(intransitive, total) # p = 0.147, not significant
# if we were to do post-hoc test with Bonferonni-Holm correction, we would do it like this:
pairwise.prop.test(intransitive, total, p.adjust.method = "holm")
## resampling individual females across experiments
# define a vector with 21 intransitive females, 44 transitive
females <- c(rep("intransitive", 21), rep("transitive", 44))
resample_intransitive <- function(x, reps = 1000, seed = 1234){
# set the seed for reproducability
set.seed(seed)
# define a tibble to store the results
df <- tibble::tibble(
experiment_1 = vector(mode = "integer", length = reps),
experiment_2 = vector(mode = "integer", length = reps),
experiment_3 = vector(mode = "integer", length = reps),
replicate = vector(mode = "integer", length = reps)
)
for(i in 1:reps){
df$replicate[i] <- i
# resample
# this randomly reshuffles the placement of the intransitive females
y <- sample(x, replace = FALSE)
# experiment 1, n = 19
# get the number of intransitive females in the first experiment
num_1 = sum(y[1:19] == "intransitive")
# experiment 2, n = 17
num_2 = sum(y[20:36] == "intransitive")
# experiment 3, n = 29
num_3 = sum(y[37:length(y)] == "intransitive")
# run a binomial test and extract the p-value for the proportion of intransitive females
df$experiment_1[i] <- broom::tidy(binom.test(x = num_1, n = 19, p = 0.25)) %>% dplyr::pull("p.value")
df$experiment_2[i] <- broom::tidy(binom.test(x = num_2, n = 17, p = 0.25))%>% dplyr::pull("p.value")
df$experiment_3[i] <- broom::tidy(binom.test(x = num_3, n = 29, p = 0.25))%>% dplyr::pull("p.value")
}
df
}
resample_intransitive(females) %>%
rowwise() %>%
mutate(at_least_one_test_sig = if_else(any(experiment_1 <= 0.05,
experiment_2 <= 0.05,
experiment_3 <= 0.05), TRUE, FALSE)) %>%
group_by(at_least_one_test_sig) %>%
count
# # A tibble: 2 x 2
# # Groups: at_least_one_test_sig [2]
# at_least_one_test_sig n
# <lgl> <int>
# 1 FALSE 785
# 2 TRUE 215
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment