Skip to content

Instantly share code, notes, and snippets.

@tpapp
tpapp / map_subarrays.r
Created January 27, 2014 15:35
Mapping a list of subarrays in R.
array_or_vector_p <- function(object) {
## Test if object is what we consider an array for the purposes of
## the code below.
##
## Lists are not arrays, but vectors are, even if they don't have a
## dim attribute. The following give an error:
## - objects which are neither lists, (non-list) vectors, or arrays
## - vectors and arrays with zero length
## - arrays with rank lower than 1
if (is.list(object))
@tpapp
tpapp / truncation_misfit.r
Last active August 29, 2015 14:05
comparison of automatic and explicit modeling of censoring
## A simple study in implementing univariate censoring
library(rstan)
## simulated data
m <- 1 # boundary
z <- rlnorm(100) # latent variable
z <- z[z > m] # only kept when above m
N <- length(z)
z_hat <- rlnorm(N, log(z), 0.1) # observed with noise
@tpapp
tpapp / logit.stan
Created November 14, 2014 10:28
coding a multilevel logit in Stan, two categorical predictors, fast & efficient inference, proper (but vague) priors
data {
int N;
int a_N; // number of categories in a
int b_N; // number of categories in b
int c_N; // number of c coefficients (continuous)
int<lower=1, upper=a_N> a[N];
int<lower=1, upper=b_N> b[N];
matrix[N, c_N] c;
int<lower=0, upper=1> y[N];
}
@tpapp
tpapp / perm.jl
Created July 31, 2015 11:13
permutations
@doc "All length `n` permutations of `v` in a matrix, with repetitions."->
function perm_matrix{T}(v::Vector{T}, n)
l = length(v)
m = l^n
a = Array(T, m, n)
for i = 1:m
k = i-1
j = n
while (j > 0)
(d,r) = divrem(k,l)
@tpapp
tpapp / data.r
Last active August 29, 2015 14:27
example of truncated data inference, posterior mean vs posterior
N <- 100
w <- rlnorm(N,0,1)
a <- runif(N,0,1)
m <- 2
keep <- w < a*m
w_kept <- w[keep]
a_kept <- a[keep]
N_kept <- sum(keep)
> stanmodelcode <- "
data {
int<lower=0> N;
real y[N];
}
parameters {
real mu;
}
@tpapp
tpapp / tuple_dispatch.jl
Last active April 3, 2016 09:11
dispatch on type of ...
module Foos
type Foo{T <: Tuple}
end
m1{T}(f::Foo{Tuple{T}}, index::T...) = 42 # suggested by Bill Hart
m2{T}(f::Foo{T}, index::T...) = 42 # what I thought would work
_m3{T}(f::Foo{T}, index::T) = 42 # indirect solution
m3{T}(f::Foo{T}, index...) = _m3(f, index)
end
f1 = Foos.Foo{Tuple{Int}}()
@tpapp
tpapp / demo.jl
Created November 3, 2016 08:32
looking for simpler solution: how to report context for an error
type ParseErrorLoc
T
string
column
line
end
function Base.show(io::IO, pel::ParseErrorLoc)
print("could not parse \"$(pel.string)\" as $(pel.T)")
if pel.column > 0
@tpapp
tpapp / top1000.jl
Created December 5, 2016 14:53
eyeballing AMDB data with lots of spells
using AMDB
using DataStructures
using GZip
using UnicodePlots
# replace this line with the path on your own machine
records = GZip.open(deserialize,
expanduser("~/research/AMDB/data/AMDB_subsample.jls.gz"), "r")
long_samples = [data for (id,data) in records if length(data.AMP_spells) > 1000]
@tpapp
tpapp / vmz.jl
Created December 8, 2016 07:53
countingVMZ
using AMDB
using DataStructures
using GZip
using UnicodePlots
# if we want to pass around code, extend it with your own machine name
# (gethostname# ()) and path
AMDB_path = get(Dict("tamas" =>
"/home/tamas/research/AMDB/data/AMDB_subsample.jls.gz"),
gethostname(),