Skip to content

Instantly share code, notes, and snippets.

View ericnovik's full-sized avatar
🎯
Focusing

Eric Novik ericnovik

🎯
Focusing
View GitHub Profile
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)
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)
}
@ericnovik
ericnovik / manip.R
Last active August 29, 2015 14:04
Example using RStudio's manipulate function
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),
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)),
Verifying that +ericnovik is my openname (Bitcoin username). https://onename.io/ericnovik
# 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
@ericnovik
ericnovik / read_cmd_stan_output.R
Created October 24, 2016 20:58
Helper R function to read output csv files generated by CmdStan
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,
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])
@ericnovik
ericnovik / entropy.R
Last active September 28, 2018 01:00
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")
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)