Skip to content

Instantly share code, notes, and snippets.

View hypercompetent's full-sized avatar

Lucas Graybuck hypercompetent

View GitHub Profile
@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(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 / 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)
}