Skip to content

Instantly share code, notes, and snippets.

View florianhartig's full-sized avatar

Florian Hartig florianhartig

View GitHub Profile
@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")
@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 / 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 / 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)
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
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
@florianhartig
florianhartig / copyInstalledPackages.r
Last active June 15, 2022 08:29
Script to copy the packages installed on one computer or R version to another. Originally from http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows
# modified from http://stackoverflow.com/questions/1401904/painless-way-to-install-a-new-version-of-r-on-windows
# run on old computer / r version
setwd("/Users/Florian/Dropbox/temp") # or any other existing temp directory
packages <- installed.packages()[,"Package"]
save(packages, file="Rpackages")
# run on new computer / r version
setwd("/Users/Florian/Dropbox/temp") # or any other existing temp directory
@florianhartig
florianhartig / source_https
Created February 10, 2015 16:10
To source an R script over https, e.g. from a github repository
# from https://tonybreyal.wordpress.com/2011/11/24/source_https-sourcing-an-r-script-from-github/
# and http://stackoverflow.com/questions/7715723/sourcing-r-script-over-https
source_https <- function(url, ...) {
# load package
require(RCurl)
# parse and evaluate each .R script
sapply(c(url, ...), function(u) {
eval(parse(text = getURL(u, followlocation = TRUE, cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl"))), envir = .GlobalEnv)
@florianhartig
florianhartig / cl-jags.R
Created January 15, 2016 11:27 — forked from chrishanretty/cl-jags.R
Conditional logit in R + JAGS
## Load libraries
library(mclogit)
library(reshape)
library(rjags)
library(R2WinBUGS) ## for write.model
## Load the data file from mclogit
data(Transport)
## Do the mclogit model
############ CREATE ZERO-INFLATED GLMM DATA #################
# This first part creates a dataset with beetles counts across an altitudinal gradient (several plots each observed several years), with a random intercept on year and zero-inflation.
altitude = rep(seq(0,1,len = 50), each = 20)
dataID = 1:1000
spatialCoordinate = rep(seq(0,30, len = 50), each = 20)
# random effects + zeroinflation
plot = rep(1:50, each = 20)