Skip to content

Instantly share code, notes, and snippets.

@statwonk statwonk/cdfs.R
Created Apr 11, 2019

Embed
What would you like to do?
CDF gist
library(tidyverse)
# Cumulative density functions
?hist
# built in datasets
str(mtcars)
head(mtcars)
mtcars %>%
head() %>%
tail(3)
mtcars %>%
pull(mpg) %>%
hist()
?hist
hist(
mtcars$mpg,
breaks = 3,
col = "orange",
freq = FALSE)
mtcars %>%
ggplot(aes(x = mpg)) +
geom_histogram(binwidth = 10, color = "black")
mtcars %>%
ggplot(aes(x = mpg, fill = factor(cyl))) +
geom_histogram(color = "black",
position = "dodge",
binwidth = 5)
mtcars %>%
ggplot(aes(x = mpg, color = factor(cyl))) +
# geom_histogram() +
stat_ecdf()
mtcars %>%
ggplot(aes(x = mpg)) +
stat_ecdf()
# install.packages("ggplot2")
# from fitdistrplus import *
library(fitdistrplus)
library(help = "fitdistrplus")
mtcars %>%
pull(mpg) %>%
fitdist(distr = "norm") %>%
plot()
hist(exp(rnorm(1e4)))
hist(log(rlnorm(1e4)))
mtcars %>%
pull(mpg) %>%
fitdist(distr = "norm")
mtcars %>%
pull(mpg) %>%
fitdist(distr = "lnorm") %>%
plot()
########
conditions <- function() { sample(1:6, 1) }
replicate(1e4, conditions() * conditions()) %>%
hist(col = "orange")
replicate(1e4, conditions() * conditions() * conditions()) %>%
hist(col = "orange")
replicate(1e4, conditions() * conditions() * conditions() * conditions()) %>%
hist(col = "orange")
replicate(1e4, conditions() + conditions() + conditions() + conditions()) %>%
hist(col = "orange")
# Bootstrapping
mtcars %>%
ggplot(aes(x = mpg)) +
stat_ecdf()
seq_len(1e3) %>%
map(function(i) {
mtcars %>%
sample_frac(1, replace = TRUE) %>%
mutate(id = i)
}) %>%
bind_rows() %>%
ggplot(aes(x = mpg, group = factor(id))) +
stat_ecdf(alpha = 0.5)
mtcars %>%
pull(mpg) %>%
fitdist(distr = "norm") %>%
summary()
plot(fitdist(mtcars$mpg, "norm"))
sales <- 3
leads <- 10
dbinom(sales, leads, 0.1)
binomial_likelihood <- function(p) {
dbinom(sales, leads, p)
}
tibble(p = seq(0, 1, 0.001)) %>%
mutate(likelihood = binomial_likelihood(p))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.