Skip to content

Instantly share code, notes, and snippets.

View florianhartig's full-sized avatar

Florian Hartig florianhartig

View GitHub Profile
rm(list=ls(all=TRUE))
library(rjags)
# assuming the data is created from an ecological system that creates an
# exponential size distribution (e.g. you sample individuals from a population that can be
# expected to follow this distribution), but this measurments are done with
# a considerable lognormal observation error
# for a realistic application see http://www.plosone.org/article/info%3Adoi%2F10.1371%2Fjournal.pone.0058036
rm(list=ls(all=TRUE))
#Analysis of likelihood, p-value, and Bayes for binomial model,
#10 trials, 3 success, unknown coin, want to do inference
trials = 10
success = 3
# GETTING THE MLE ESTIMATE
@florianhartig
florianhartig / gaussianAutoregressiveExample.jags.
Last active November 9, 2021 11:07
A modification of code posted originally by Petr Keil http://www.petrkeil.com/?p=1910, to illustrate some comments I made in response to this blog post
library(mvtnorm) # to draw multivariate normal outcomes
library(R2jags) # JAGS-R interface
# function that makes distance matrix for a side*side 2D array
dist.matrix <- function(side)
{
row.coords <- rep(1:side, times=side)
col.coords <- rep(1:side, each=side)
row.col <- data.frame(row.coords, col.coords)
D <- dist(row.col, method="euclidean", diag=TRUE, upper=TRUE)
@florianhartig
florianhartig / samplingDesign3SpeciesWithNoEqualNeigbors.r
Last active January 3, 2016 08:09
Distributes n=3 plant species across k=12 x m=12 grid cells, in a way that no individual has another individual of it’s own species in its 4-cell (von Neumann: up, down, left, right) neighborhood. This script originated from a question posted on http://theoreticalecology.wordpress.com/2014/01/14/sampling-design-combinatorics/ , solution posted b…
# plot layout
x=12
y=12
# number of diff samples
n=3
# plot A, which holds
A=array(NA,c(x,y))
# repeat 1 through n as many times as subplots are in A
s=rep(1:n, x*y/n)
@florianhartig
florianhartig / CorrelationDensityPlotWithIpanel.r
Last active January 3, 2016 06:29
A code snippet creating a pair correlation plot that works well to visualize correlations in large datasets such as MCMC chains or similar. Marginal distributions on the diagonal, correlation density plots on the lower triangle, spearman correlation in scaled numbers on the upper triangle.
library(IDPmisc)
panel.hist <- function(x, ...)
{
usr <- par("usr"); on.exit(par(usr))
par(usr = c(usr[1:2], 0, 1.5) )
h <- hist(x, plot = FALSE)
breaks <- h$breaks; nB <- length(breaks)
y <- h$counts; y <- y/max(y)
rect(breaks[-nB], 0, breaks[-1], y, col="blue4", ...)
@florianhartig
florianhartig / Teaching-Stats-Likelihood-Intro-GER
Last active December 27, 2015 22:39
Code snippets from Introductory Stats Lecture, Introduction Likelihood, comments in German
# Praktische Vorlesung, Einführung Statistik, WS 13/14
# Florian Hartig, http://www.biom.uni-freiburg.de/mitarbeiter/hartig
?read.csv
# Wiederholung likelihood
# Als estes erstellen wir einen leeren Plot
plot(NULL, NULL, xlim=c(-4,6), ylim = c(0,0.5), ylab = "Wahrscheinlichkeit(sdichte)", xlab = "Observed value")