Skip to content

Instantly share code, notes, and snippets.

View daviddalpiaz's full-sized avatar
💀
zzz

David Dalpiaz daviddalpiaz

💀
zzz
View GitHub Profile
@daviddalpiaz
daviddalpiaz / general-gainz.R
Last active October 12, 2021 13:39
general gainz workout generator
# https://i.imgur.com/tqoTyK3.jpg
# TODO: add rest timings
# TODO: add weight based on previous?
# TODO: push / hold / find / extend
# TODO: 4.5 -> 4/3
gen_workout = function(type = "full") {
tier_1_lift = sample(c("ossumo", "bp", "ohp"), size = 1, prob = c(2, 1, 1))
tier_2_lift = sample(c("hbbs", "bp", "ohp"), size = 1, prob = c(2, 1, 1))
tier_3_lift = sample(c("dbr", "sr", "hc", "bc"), size = 1, prob = c(4, 1, 1, 1))
@daviddalpiaz
daviddalpiaz / coan-phillipi.R
Created April 14, 2021 20:47
Coan-Phillipi Deadlift Routine
max = 210
data.frame(
work_wt = c(
max * 0.750,
max * 0.800,
max * 0.850,
max * 0.900,
max * 0.800,
max * 0.850,
@daviddalpiaz
daviddalpiaz / diff-os-diff-results.R
Created October 20, 2020 02:23
An example where the results differ by OS
y = c(2637,2495,2977,3062,3203,3860,3856,2920,1970,2920,3090,3941,3042,2836,
1928,2977,4990,3983,3175,2240,3651,3544,2750,3416,3572,2353,2100,3132,
1729,3643,2782,3005,2424,3799,3175,2778,3941,2211,2084,2353,3062,3770,
3997,3374,4167,2414,2495,2920,2126,3487,2977,3572,1588,3614,3473,2523,
3997,2557,2821,3637,3232,3969,3756,3460,3790,3651,2055,2877,2466,3100,
2835,2906,3651,4238,3912,2084,2055,2410,2495,3225,2125,2733,3827,2948,
2466,3770,2922,2920,1021,2296,4593,2438,3090,3203,3402,2187,3651,1818,
1928,2600,1893,3317,1330,3062,3884,3080,3232,2442,2495,3884,2663,1899,
3331,1790,2977,2225,3062,3860,3104,3629,3203,2769,2410,2450,2665,2466,
1135,3225,3274,3317,4111,3234,3544,3444,3033,2187,2807,2296,3090,1928,
@daviddalpiaz
daviddalpiaz / mnist-keras.R
Created October 3, 2019 14:33
train a neural network on MNIST in R using Keras
# see here: https://keras.rstudio.com/
# code reproduced here for easy copy-pasting
# install TF if necessary
devtools::install_github("rstudio/tensorflow")
tensorflow::install_tensorflow()
# load keras
library(keras)
@daviddalpiaz
daviddalpiaz / check_mem.R
Created September 21, 2019 13:57
function for checking memory using on RStudio Cloud
check_mem = function() {
mem_lim = as.numeric(system("cat /sys/fs/cgroup/memory/memory.limit_in_bytes", intern = TRUE)) * 0.000001
mem_use = as.numeric(system("cat /sys/fs/cgroup/memory/memory.usage_in_bytes", intern = TRUE)) * 0.000001
prc_free = mem_use / mem_lim * 100, 1
prc_used = 100 - prc_free
memory_info = c("Used (MB)" = mem_use,
"Free (MB)" = mem_lim - mem_use,
"Used (%)" = prc_used,
"Free (%)" = prc_free)
round(memory_info, 1)
@daviddalpiaz
daviddalpiaz / beta-binom.R
Last active April 9, 2019 10:39
plots for discussing the beta-binomial model
plot_beta = function(a, b) {
par(mfrow = c(1, 1))
curve(dbeta(x, shape1 = a, shape2 = b), col = "darkorange",
main = "beta distribution", xlab = "x", ylab = "density", lwd = 2)
grid()
}
plot_prior_like_post = function(a, b, x, y, plot_est = FALSE) {
@daviddalpiaz
daviddalpiaz / load_MNIST.R
Last active July 16, 2022 11:58
Load the MNIST handwritten digits dataset into R as a tidy data frame
# modification of https://gist.github.com/brendano/39760
# automatically obtains data from the web
# creates two data frames, test and train
# labels are stored in the y variables of each data frame
# can easily train many models using formula `y ~ .` syntax
# download data from http://yann.lecun.com/exdb/mnist/
download.file("http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz",
"train-images-idx3-ubyte.gz")
download.file("http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz",
obs = replicate(10000, sum(runif(n = 48, min = -0.5, max = 0.5)))
hist(obs, breaks = 25, col = "darkorange", border = "dodgerblue")
mean(abs(obs) < 1 * 2)
mean(abs(obs) < 2 * 2)
mean(abs(obs) < 3 * 2)
@daviddalpiaz
daviddalpiaz / pois-approx-binom.R
Last active October 21, 2016 14:01
Plots to show Poisson approximation to binomial.
plot_pois = function(lambda = 1.4) {
stop_graph = max(15, ceiling(lambda + 3 * sqrt(lambda)))
x = 0:stop_graph
fx = dpois(x, lambda = lambda)
barplot(fx, names.arg = x, space = 0,
xlab = "values", ylab = "probabilites",
col = "darkorange", border = "dodgerblue",
main = "Poisson Probabilities")
}
@daviddalpiaz
daviddalpiaz / aic-difference.R
Last active February 27, 2017 18:34
extractAIC() vs AIC()
fit_test = lm(dist ~ ., data = cars)
n = length(resid(fit_test))
p = length(coef(fit_test))
-2 * logLik(fit_test) + 2 * p
AIC(fit_test)
extractAIC(fit_test) + n + n * log(2 * pi)