Skip to content

Instantly share code, notes, and snippets.

Avatar

Gavin Simpson gavinsimpson

View GitHub Profile
@gavinsimpson
gavinsimpson / smooth_re2.R
Created Aug 26, 2020 — forked from noamross/smooth_re2.R
Smooth construct, predict, and plot for random effects with numeric inputs
View smooth_re2.R
#' 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 May 31, 2020
Extrapolating with penalised b-splines and mgcv
View b-spline-extrapolation.R
## 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
Animated spline basis functions
View animated-basis-functions.R
## 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 Nov 8, 2019
Animation showing how weighted basis functions combine to yield a cubic regression spline
View animated-basis-functions.R
## 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 Jun 16, 2018
peat age data from maddy and drew
View maddy-peat.csv
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
View darlingtonia.csv
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 / geochimica-metals.csv
Created Oct 10, 2017
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
View geochimica-metals.csv
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 Jul 21, 2017
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)
View compare-glmmTMB-with-mgcv.R
## Compare Brooks et al glmmTMB paper with mgcv
## Packages
library("glmmTMB")
library("mgcv")
library("ggplot2")
theme_set(theme_bw())
library("ggstance")
## Salamander
@gavinsimpson
gavinsimpson / modelled-nuuk-rainfall.png
Last active Apr 28, 2021
R code to download, extract, and fit a Tweedie GAM to monthly rainfall total time series from Nuuk, Greenland, using mgcv