Skip to content

Instantly share code, notes, and snippets.

@mkuhn
mkuhn / uncompressed.js
Last active February 6, 2023 17:10
ShortDOI Bookmarklet
javascript:var%20metas=document.getElementsByTagName('meta');var%20a='';for(i=0;i<metas.length;i++){if(metas[i].getAttribute('name')=='citation_doi'){a=metas[i].getAttribute('content')}}if(a){location.href='http://shortdoi.org/'+a}else{a=document.documentElement.innerHTML.match(/doi%20*[:=]%20*10[-/0-9a-z.]\/[-/0-9a-z.]+/mig)||document.documentElement.innerHTML.match(/dx.doi.org\/10[-/0-9a-z.]+/mig);if(a==null){alert('Could%20not%20find%20DOI!')}else{a=a.map(function(x){return%20x.match('10.*')[0]});var%20d=a[0];if(a.length>1){var%20c=new%20Array();for(i%20in%20a){if(c[a[i]]){c[a[i]]++}else{c[a[i]]=1}}var%20m=0;d=null;for(i%20in%20c){if(c[i]>m){m=c[i];d=i}else%20if(c[i]==m){d=null}}}if(d==null){alert('More%20than%20one%20DOI%20found!')}else{location.href='http://shortdoi.org/'+d}}}
@mkuhn
mkuhn / guides.md
Created June 30, 2021 08:24
Hollow circles for size legend
library(tidyverse)

set.seed(23)

d <- tibble(x = rnorm(10), y = rnorm(10), a = rep(c("x", "y"),5), b = rep(1:5, 2))

d %>% 
  ggplot(aes(x, y, color = a, size = b)) + geom_point() +
 guides(colour = guide_legend(order = 1), 
---
title: "Fitting with uncertainty"
author: "Michael Kuhn"
date: "24 Aug 2015"
output: html_document
---
In this toy example, we assume that we've independently measured values $x$ and $y$ and want to find a linear relationship between them, accounting for measurement uncertainty. Each $x$ and $y$ value is assigned a different uncertainty, and the challenge is to take this information into account. A standard linear model will treat all points equally.
When we treat each measurement as a multivariate normal distribution, we can find a point along the proposed fitted line that is maximizing the probability density function (i.e. that has a maximum likelihood). Thus, given a slope and intercept, we virtually move all measurements to their most likely point along the line, and use the likelihood at this point.
@mkuhn
mkuhn / pdz
Created September 28, 2017 08:41
Scribble:
B7Z2Y1_HUMAN
ARHG7_HUMAN
ARHG8_HUMAN
KCNA5_HUMAN
VWCE_HUMAN
DNML1_HUMAN
PKP4_HUMAN
NXPE2_HUMAN
@mkuhn
mkuhn / inplace_comparison.R
Created March 8, 2016 09:07
Count comparison results without additional memory allocation
library(Rcpp)
`%count<%` <- cppFunction('
size_t count_less(NumericVector x, NumericVector y) {
const size_t nx = x.size();
const size_t ny = y.size();
if (nx > 1 & ny > 1) stop("Only one parameter can be a vector!");
@mkuhn
mkuhn / InChi.js
Last active December 21, 2015 17:19 — forked from lsauer/InChi.js
// International Chemical Identifier Regex, by lo sauer - lsauer.com
// Morphine InchI:
var x="InChI=1S/C17H19NO3/c1-18-7-6-17-10-3-5-13(20)16(17)21-15-12(19)4-2-9(14(15)17)8-11(10)18/h2-5,10-11,13,16,19-20H,6-8H2,1H3/t10-,11+,13-,16-,17-/m0/s1"
// applying an organic character-subset
// we could check for the length property, but in case of 0 matches 'null' is returned -> hence !!.. \ generally equal to Boolean(..)
!!x.trim().match(/^((InChI=)?[^J][0-9BCOHNSOPrIFla+\-\(\)\\\/,pqbtmsih]{6,})$/ig)
>true
//generic:
x.trim().match(/^((InChI=)?[^J][0-9a-z+\-\(\)\\\/,]+)$/ig)
library(ggplot2)
library(gridExtra)
mtcars$cyl <- ordered(mtcars$cyl)
p <- ggplot(mtcars, aes(mpg, hp, colour = cyl)) + geom_point()
p1 <- p + theme(legend.position = "none")
p2 <- ggplot(mtcars, aes(x=mpg, group=cyl, colour=cyl))
p2 <- p2 + stat_density(fill = NA, position="dodge")
> library(purrr)
> ll <- list(list(a=1:3, b=4), list(a=5:7, b=8))
> ll %>% map(lift_dl(c)) %>% map_call(rbind)
a1 a2 a3 b
[1,] 1 2 3 4
[2,] 5 6 7 8
> Reduce(rbind, ll)
a b
init Integer,3 4
Integer,3 8
@mkuhn
mkuhn / initial_param_vs_runtime.R
Created September 30, 2015 09:52
Detecting correlation between initial parameters and run time in RStan
library(purrr)
runtimes <- get_elapsed_time(fit)[,2]
inits <- get_inits(fit)
## traditional conversion to a matrix
# m.init <- do.call(rbind, lapply(inits, function(l) do.call(c, l)))
## using purrr
@mkuhn
mkuhn / ggplot2_order.R
Created August 11, 2011 08:08
ggplot: Determining the order in which lines are drawn
library(ggplot2)
df <- data.frame( n=c("a","a","b","b","c","c"), x = rep(c(1,2), 3), y = rep(c(1), 6), l = as.factor(c(1,1,0,0,0,0)))
# Contents of df:
# n x y l
# 1 a 1 1 1
# 2 a 2 1 1
# 3 b 1 1 0
# 4 b 2 1 0