Skip to content

Instantly share code, notes, and snippets.

View gavinsimpson's full-sized avatar

Gavin Simpson gavinsimpson

View GitHub Profile
@noamross
noamross / smooth_re2.R
Created June 12, 2020 22:28
Smooth construct, predict, and plot for random effects with numeric inputs
#' Create a random effect basis with integers rather than factors
#' @import mgcv
#' @export
smooth.construct.re2.smooth.spec <- function (object, data, knots) {
if (!is.null(object$id))
stop("random effects don't work with ids.")
if(any(sapply(data, is.numeric))) data <- lapply(data, as.factor) ## <-- All I did was this (and below)
form <- as.formula(paste("~", paste(object$term, collapse = ":"),
"-1"))
object$X <- model.matrix(form, data)
@noamross
noamross / mgcv-posterior-animate.R
Created September 2, 2018 00:29
Animating smoothing uncertainty in a GAM
library(tidyverse)
library(gganimate)
library(mgcv)
library(mvtnorm)
# Fit a GAM to the data
mod <- gam(hp ~ s(mpg), data=mtcars, method="REML")
# Get the linear prediction matrix
newdat = data.frame(
@smdabdoub
smdabdoub / jags_mkl_build_linux.txt
Last active March 20, 2018 21:08
Building JAGS and rjags with the Intel MKL LAPACK routines (LINUX)
# NOTE: This was done on RHEL 7.3 with JAGS v4.3.0 and rjags v4-6. It should be fairly portable, but YMMV.
# Step 1: create an MKL environment variable for use in the --with-blas option for the JAGS configure script
# the below was taken from Martyn Plummer's "Building a portable R binary with Intel tools"
# https://martynplummer.files.wordpress.com/2014/10/r-intel-fedora.pdf
# I set MKL_LIB_PATH by looking at MKL_LIBS
export MKL="-Wl,--start-group ${MKL_LIB_PATH}/libmkl_gf_lp64.a \
> ${MKL_LIB_PATH}/libmkl_gnu_thread.a \
> ${MKL_LIB_PATH}/libmkl_core.a \
@dill
dill / better_check.R
Last active September 14, 2021 17:09
Combined deviance and quantile residuals check plots for GAMs
library(gridBase)
library(grid)
library(mgcv)
library(statmod)
## nicer version of gam.check/rqgam.chack
# - deviance residuals for the Q-Q and histogram
# - RQR for resids vs linear pred
# - response vs fitted
# hist.p gives the quantiles of the residuals to show
better_check <- function(b, k.sample = 5000, k.rep = 200, rep = 0, level = 0.9,
@dill
dill / verhoef.R
Created August 28, 2015 03:18
ver Hoef and Boveng (2007) plots
# makes plots (somewhat) like those in ver Hoef and Boveng (2007)
# Jay M. Ver Hoef and Peter L. Boveng 2007. QUASI-POISSON VS. NEGATIVE BINOMIAL REGRESSION: HOW SHOULD WE MODEL OVERDISPERSED COUNT DATA? Ecology 88:2766–2772. http://dx.doi.org/10.1890/07-0043.1
# http://www.utstat.utoronto.ca/reid/sta2201s/QUASI-POISSON.pdf
# calling with something like:
# par(mfrow=c(1,2))
# # define the breaks
@Thell
Thell / Exmple.Rmd
Created June 20, 2013 23:59
R markdown to illustrate creation of in-document referencing for chunks, sections, pages, and such.
```{r setup, echo=FALSE, results='hide'}
chunkref <- local({
function(chunklabel) {
sprintf('[%s](#%s)', chunklabel, chunklabel )
}
})
secref <- local({
function(seclabel) {
sprintf('[%s](#%s)', seclabel, seclabel )
@aaronberdanier
aaronberdanier / Sankey.R
Created December 2, 2011 14:50
Program for creating sankey diagrams in R
SankeyR <- function(inputs, losses, unit, labels, format="plot"){
########################
# SankeyR version 1.01 (updated August 10, 2010)
# is a function for creating Sankey Diagrams in R.
# See http://www.sankey-diagrams.com for excellent examples of Sankey Diagrams.
#
# OPTIONS:
# 'inputs' is a vector of input values
# 'losses' is a vector of loss values
# 'unit' is a string of the unit