Skip to content

Instantly share code, notes, and snippets.

View florianhartig's full-sized avatar

Florian Hartig florianhartig

View GitHub Profile
# The purpose of this script is to show that RF variable importance
# will split importance values for collinear variables evenly,
# even if collinearity is low enough so that variables are sepearable
# and would be correctly separameted by an lm / ANOVA
set.seed(123)
# simulation parameters
n = 3000
col = 0.7
library(gdata)
Data = read.xls("http://www.pnas.org/content/suppl/2014/05/30/1402786111.DCSupplemental/pnas.1402786111.sd01.xlsx",
nrows = 92, as.is = TRUE)
library(glmmTMB)
originalModelGAM = glmmTMB(alldeaths ~ scale(MasFem) *
(scale(Minpressure_Updated.2014) + scale(NDAM)),
data = Data, family = nbinom2)
# Residual checks with DHARMa
@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)
n = 50
x = seq(-1,1, len = n)
f = function(x) 0.3 * x^2
y = f(x) + rnorm(n, sd = 0.2)
par(mfrow = c(1,2))
lmFit <- lm(y ~ x + I(x^2))
@florianhartig
florianhartig / metropolis.R
Created March 23, 2021 20:18
Metropols MCMC
trueA = 5
trueB = 0
trueSd = 10
sampleSize = 31
# create independent x-values
x =(-(sampleSize-1)/2):((sampleSize-1)/2)
# create dependent values according to ax + b + N(0,sd)
y = trueA * x + trueB + rnorm(n=sampleSize,mean=0,sd=trueSd)
@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