Skip to content

Instantly share code, notes, and snippets.

irb(main):012:0> account.logo.url
=> "/uploads/account/logo/1/me_climbing_mountains.jpg"
- method: energy_cost_savings
display: Energy Cost Savings
tip: This is the tooltip
tooltips.keys.each do |key|
define_method(key) { tooltip tooltips[key][:display], t[key][:tip] }
end
@tvladeck
tvladeck / spectral.R
Last active December 19, 2015 16:26
spectral clustering for random forest proximity / similarity matrix
spectral <- function(proximity, k = 2)
{
D <- diag(apply(proximity, 1, sum))
U <- D - proximity
# L <- diag(nrow(my.data)) - solve(D) %*% A # simple Laplacian
# round(L[1:12,1:12],1)
# matrix power operator: computes M^power (M must be diagonalizable)
"%^%" <- function(M, power)
@tvladeck
tvladeck / kmm_scree.R
Created December 23, 2015 18:46
determining optimal # of clusters in k means
mydata <- d
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(mydata,
centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
@tvladeck
tvladeck / beta_nbd.R
Created February 17, 2016 20:23
NBD with t distributed beta
# NBD with t distributed beta
beta.nbd.density <- function(x, r, alpha, a, b)
{
# params:
# x: count we are evaluating density at
# r: nbd r parameter
# alpha: nbd alpha parameter
# a: beta alpha parameter
# b: beta beta parameter
@tvladeck
tvladeck / beta_nbd_ll.R
Created February 17, 2016 23:48
log likelihood of beta nbd model
beta.nbd.ll <- function(x, n_x, r, alpha, a, b)
{
# params:
# x: vector of counts
# n_x: number of observations for each count
# r, alpha: nbd params
# a, b: beta params
# output:
# the log-likelihood of the model
probs <- sapply(x, function(y) beta.nbd.density(y, r, alpha, a, b))
@tvladeck
tvladeck / repel_plot.R
Last active February 19, 2019 21:00
repel plot from contingency table
library(ca)
library(ggplot2)
library(ggrepel)
library(stringr)
library(scales)
library(magrittr)
library(factoextra)
repel_ca_from_table <-
function(
@tvladeck
tvladeck / gradient_heatmap.R
Last active February 19, 2019 21:00
heatmap of table
library(RColorBrewer)
library(fields) #to use designer.colors
library(reshape2)
library(dplyr)
gradient_heatmap <-
function(
df,
num_percentiles = 10,
@tvladeck
tvladeck / bugs_code.bug
Created December 22, 2016 16:12
i don't quite get why you run _coda.samples_ twice
# Betas #
samples <- coda.samples(jags, c('beta_age',
'beta_percent_white',
'beta_uninsured',
'beta_unemployed',
'beta_percent_degree',
'beta_income',
'beta_health'), 10000)