Skip to content

Instantly share code, notes, and snippets.

View hypercompetent's full-sized avatar

Lucas Graybuck hypercompetent

View GitHub Profile
@hypercompetent
hypercompetent / circle_sphere_distributions.R
Created April 21, 2021 18:17
Circular and Spherical random uniform distribution functions
rcau <- function() {
angle <- 2 * pi * runif(1)
radius <- 1 * (runif(1) ^ (1/2))
x <- radius * cos(angle)
y <- radius * sin(angle)
c(x, y)
}
library(reshape2)
library(ggplot2)
df <- melt(volcano[20:44,10:34])
names(df) <- c("x","y","val")
rect <- data.frame(st = 5, en = 20)
ggplot() +
@hypercompetent
hypercompetent / pearson_residuals.R
Last active January 7, 2021 16:53
Pearson residuals function in R, from Lause, Berens, and Kobak
pearson_residuals <- function(counts, theta = 100) {
counts_sum0 <- as(matrix(Matrix::colSums(counts),nrow=1),"dgCMatrix")
counts_sum1 <- as(matrix(Matrix::rowSums(counts),ncol=1),"dgCMatrix")
counts_sum <- sum(counts@x)
#get residuals
mu <- (counts_sum1 %*% counts_sum0) / counts_sum
z <- (counts - mu) / sqrt(mu + mu^2/theta)
#clip to sqrt(n)
library(rgl)
sl <- shapelist3d(cube3d(alpha = 1,
color = 'grey90',
specular = 'black',
lit = TRUE),
plot = FALSE)
create_next_level <- function(sl, level) {
@hypercompetent
hypercompetent / office_writers.csv
Created December 24, 2019 20:43
Director, Writer, and Staff Writer credits for each episode of The Office (US).
season episode director writer staff_writer
01 01 Ken Kwapis Ricky Gervais;Stephen Merchant;Greg Daniels
01 02 Ken Kwapis B.J. Novak Mindy Kaling
01 03 Ken Whittingham Paul Lieberstein Mindy Kaling
01 04 Bryan Gordon Michael Schur Mindy Kaling
01 05 Greg Daniels Greg Daniels Mindy Kaling
01 06 Amy Heckerling Mindy Kaling Mindy Kaling
02 01 Greg Daniels Mindy Kaling
02 02 Ken Kwapis B.J. Novak
02 03 Paul Feig Michael Schur
@hypercompetent
hypercompetent / protein_migration_rates.csv
Created August 5, 2019 16:52
Approximate protein migration rates as a function of buffer and acrylamide concentration on SDS-PAGE
buffer percent_acrylamide kDa_size percent_migration
MES 8 260 12
MES 8 160 22
MES 8 110 30
MES 8 80 35
MES 8 60 40
MES 8 50 45
MES 8 40 55
MES 8 30 60
MES 8 20 70
@hypercompetent
hypercompetent / tidy_eval_notes.R
Last active July 14, 2019 05:18
Notes for tidy evaluation based on strings as sources of column names
# Tidyeval notes
library(dplyr)
library(rlang)
library(tasic2016data)
# one character element:
test <- "primary_type_id"
expr_test <- parse_expr(test)
@hypercompetent
hypercompetent / plot_aifi_logo.R
Created June 17, 2019 19:01
Generate a plot for the Allen Institute for Immunology logo
plot_logo <- function() {
library(ggplot2)
core_rect <- data.frame(xmin = 0,
xmax = 4,
ymin = 0,
ymax = 0.6,
color = "")
@hypercompetent
hypercompetent / cocoframer_black_bg.R
Created May 21, 2019 14:59
Use bg3d() to specify cocoframer background color
library(cocoframer)
library(purrr)
library(rgl)
structures <- c("root","CA")
mesh_list <- map(structures, ccf_2017_mesh)
names(mesh_list) <- structures
plot_ccf_meshes(mesh_list,
@hypercompetent
hypercompetent / purrr_happy.R
Last active May 5, 2019 19:11 — forked from schnee/purrr_happy.R
What considerations are important in choosing between the two styles below? Interpretability, maintainability, speed, other?
library(purrr)
library(magrittr)
library(microbenchmark)
y_test <- sample(0:9, 2000, replace = TRUE)
pred1 <- sample(0:9, 2000, replace= TRUE)
pred2 <- sample(0:9, 2000, replace= TRUE)
preds <- list(pred1, pred2)
# want to apply the following to preds