Skip to content

Instantly share code, notes, and snippets.

View gavinsimpson's full-sized avatar

Gavin Simpson gavinsimpson

View GitHub Profile
@gavinsimpson
gavinsimpson / predict-bam-bug-with-terms-argument.R
Created May 9, 2022 08:23
Predicting from a model fitted with `bam()` when `discrete = TRUE` was used to both fit and predict from the model with `terms` in the `predict` call doesn't include the model intercept
# Weird behaviour of predict.bam()
# packages
library("mgcv")
# simulate some data
set.seed(42)
df <- gamSim(1, n = 10000)
# fit a bam with and without discrete = TRUE and a gam
@gavinsimpson
gavinsimpson / smooth_re2.R
Created August 26, 2020 21:07 — forked from noamross/smooth_re2.R
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)
@gavinsimpson
gavinsimpson / b-spline-extrapolation.R
Last active October 16, 2023 21:21
Extrapolating with penalised b-splines and mgcv
## Can we do better extrapolation than the TPRS example used by Gabriel
## Riutort-Mayol et al in their recent paper on low rank approximations to
## Gaussian Processes? https://arxiv.org/abs/2004.11408
## Packages
library('ggplot2')
library('tibble')
library('tidyr')
library('dplyr')
library('mgcv')
@gavinsimpson
gavinsimpson / animated-basis-functions.R
Created May 7, 2020 19:18
Animated spline basis functions
## plots and animations of how basis functions are used to make
## splines and how these are fitted to data
library('ggplot2')
library('tibble')
library('tidyr')
library('dplyr')
library('mgcv')
library('mvnfast')
library('purrr')
@gavinsimpson
gavinsimpson / animated-basis-functions.R
Created November 8, 2019 01:36
Animation showing how weighted basis functions combine to yield a cubic regression spline
## Procuces an animation showing how weighted basis functions combine to produce a sline
## - this version is for a cubic regression spline basis
## packages required
library('ggplot2')
library('tibble')
library('tidyr')
library('dplyr')
library('mgcv')
library('mvnfast')
@gavinsimpson
gavinsimpson / maddy-peat.csv
Created June 16, 2018 09:30
peat age data from maddy and drew
Sample upperDepth lowerDepth ageBP ageError calUpper calLower
SRR-4556 20 22 355 35 509 307
SRR-4557 26 28 465 35 542 480
SRR-4558 32 34 635 35 671 545
SRR-4559 38 40 740 35 732 666
SRR-4560 44 46 865 35 916 691
SRR-4561 50 52.5 870 35 918 692
SRR-4562 56 58 985 35 967 795
SRR-4563 100 108 1270 35 1284 1097
SRR-4564 200 207 2575 35 2761 2558
@gavinsimpson
gavinsimpson / darlingtonia.csv
Created June 16, 2018 08:07
Darlingotnia data
We can make this file beautiful and searchable if this error is corrected: Illegal quoting in line 1.
## leafHeight is in cm.; visited, 1 == TRUE, 0 == FALSE"
"leafHeight","visited"
67,1
84,1
49,1
70,1
77,1
77,1
66,1
65,1
@gavinsimpson
gavinsimpson / data-plus-effects-original-scale.png
Last active February 3, 2021 18:57
Analyse transect data from Shultz et al (2016) PeerJ
data-plus-effects-original-scale.png
@gavinsimpson
gavinsimpson / geochimica-metals.csv
Created October 10, 2017 22:11
Subset of data used in Rose et al (2012) Geochimica et Cosmochimica Acta 82, 113–135 doi: http://doi.org/10.1016/j.gca.2010.12.026
We can make this file beautiful and searchable if this error is corrected: It looks like row 2 should actually have 2 columns, instead of 5 in line 1.
## Subset of data used in Rose et al (2012) Geochimica et Cosmochimica Acta 82, 113–135 doi: http://doi.org/10.1016/j.gca.2010.12.026
SiteCode,Date,SoilType,Region,Hg
CHNA,2000,thin,NW,3.84339855275794
CHNA,1990,thin,NW,5.42461797827638
CHNA,1980,thin,NW,8.81972984714937
CHNA,1970,thin,NW,11.4174572948292
CHNA,1960,thin,NW,16.513540420367
CHNA,1950,thin,NW,16.5120469510654
CHNA,1940,thin,NW,11.1888397621818
CHNA,1930,thin,NW,11.6222216338607
@gavinsimpson
gavinsimpson / compare-glmmTMB-with-mgcv.R
Last active April 30, 2024 20:09
A quick R script I knocked up to compare the glmmTMB and mgcv packages for fitting zero-inflated GLMMs to the Salamander and Owls data sets from Brooks et al (2017)
## Compare Brooks et al glmmTMB paper with mgcv
## Packages
library("glmmTMB")
library("mgcv")
library("ggplot2")
theme_set(theme_bw())
library("ggstance")
## Salamander