Skip to content

Instantly share code, notes, and snippets.

@aammd
Created June 28, 2018 11:34
Show Gist options
  • Save aammd/64a91d2ec41e39e3eef0eb8e485c8e6f to your computer and use it in GitHub Desktop.
Save aammd/64a91d2ec41e39e3eef0eb8e485c8e6f to your computer and use it in GitHub Desktop.
simulate relationships from a logistic curve, about Dragons because why not
library(tidyverse)
set.seed(4812)
islands <- data_frame(area =
# runif(35, min = 0, max = 29)
rlnorm(35, meanlog = log(5), sdlog = log(3))
)
dragon_response <- function(b0, b1){
force(b0)
force(b1)
function(v) {
rethinking::logistic(b0 + b1 * v)
}
}
library(gganimate)
purrr::rerun(20,
islands %>%
mutate(area_centered = log(area) - mean(log(area)),
p_dragon = map_dbl(area_centered, dragon_response(1.1, 0.5)),
is_dragon = rbinom(length(p_dragon), 1, p_dragon))) %>%
bind_rows(.id = "frame") %>%
ggplot(aes(x = log(area), y = is_dragon, frame = frame)) +
geom_point() +
stat_smooth(method = "glm", method.args = list(family = "binomial")) +
geom_line(aes(x = area_centered + mean(log(area)), y = p_dragon)) +
theme_minimal()+
theme(text = element_text(size = 16)) +
NULL
gganimate(interval = .2, title_frame = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment