This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
par (mar=c(3,3,2,1), mgp=c(2,.7,0), tck=-.012, las=1) | |
with(iris, plot(Sepal.Length, Sepal.Width, | |
col=as.numeric(Species)+1, pch=20)) | |
lbs <- levels(iris$Species) | |
legend('topright', legend=lbs, | |
col=2:4, cex=0.7, pch=20, box.lwd=0.5, pt.cex=0.6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
order <- 10 | |
x <- seq(0, 2*pi, 0.01) | |
y <- cos(x) | |
y_poly <- poly(y, order, raw = TRUE) | |
plot(y_poly[, 1], col = 1, type = "l", xlab = "", ylab = "", axes = FALSE) | |
for (i in 2:order) { | |
lines(y_poly[, i], col = i) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('manipulate') | |
manipulate( | |
{ | |
x <- seq(-10, 10, length = 1000) | |
d <- dcauchy(x, scale = scale, location = location, log = Log) | |
plot(d ~ x, type = "l") | |
}, | |
scale = slider(1, 10, step = 1), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var_imp <- function(rpart_fit) { | |
var_imp <- sort(rpart_fit$variable.importance) | |
in_model <- levels(rpart_fit$frame$var)[-1] | |
return(list(var_imp = var_imp, in_model = in_model)) | |
} | |
var_imp_plot <- function(rpart_fit) { | |
vars <- var_imp(rpart_fit) | |
groups <- factor(x = rep("less so", length(vars$var_imp)), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Verifying that +ericnovik is my openname (Bitcoin username). https://onename.io/ericnovik |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Example 2.2.7 (A girl born in winter). Page 46. | |
# A family has two children. Find the probability that both children are girls, | |
# given that at least one of the two is a girl who was born in winter. Assume | |
# that the four seasons are equally likely and that gender is independent of season. | |
# Blitzstein, Joseph K.; Hwang, Jessica (2014-07-24). | |
# Introduction to Probability (Chapman & Hall/CRC Texts in Statistical Science) | |
library(stringr) | |
n <- 1000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
read_cmd_stan_output <- function (dir, file_prefix, n_chains = 4) { | |
csvfiles <- character(n_chains) | |
for (i in 1:n_chains) | |
csvfiles[i] <- paste0(dir, file_prefix, i, ".csv") | |
stan_fit <- rstan::read_stan_csv(csvfiles) | |
return(stan_fit) | |
} | |
# Assume /my/base/dir/ contains stan_output_model_1.csv, | |
# stan_output_model__2.csv, stan_output_model_3.csv, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data <- list(N = 5, y = c(0, 1, 1, 0, 1)) | |
# log probability function | |
# Note: the below operations are not arithmetically | |
# stable. Use for demo purposed only. | |
lp <- function(theta, d) { | |
lp <- 0 | |
for (i in 1:d$N) { | |
lp <- lp + log(theta) * d$y[i] + | |
log(1 - theta) * (1 - d$y[i]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
entropy <- function(p) -sum(p * log(p)) | |
n <- 1e3 | |
p <- runif(n) | |
q <- 1 - p | |
z <- matrix(c(p, q), ncol = 2) | |
ent <- apply(z, 1, entropy) | |
qplot(q, ent, geom = "line") + | |
ylab("Entropy") + xlab("Probability") + | |
ggtitle("Entropy For Two Events") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(ggplot2) | |
theme_set(theme_minimal()) | |
# Visualizing entropy | |
entropy <- function(p) -sum(p * log(p)) | |
n <- 1e3 | |
p <- runif(n) | |
q <- 1 - p | |
z <- matrix(c(p, q), ncol = 2) | |
ent <- apply(z, 1, entropy) |
OlderNewer