Skip to content

Instantly share code, notes, and snippets.

@dill
dill / phylo.R
Created February 22, 2024 08:56
random phylogenetic trees + multidimensional scaling
# generate a random phylogenetic tree, then apply multidimensional
# scaling to the distances and see what the 2D plot looks like
library(ape)
layout(matrix(1:12, 3, 4, byrow=TRUE))
for (i in 1:3){
# generate a tree
rr <- rtree(20)
@dill
dill / is_this_a_good_idea.R
Created April 13, 2023 15:08
📈 fitting categorical covariates as 1D Markov random fields
# example using an MRF for ordered categorical predictors
library(mgcv)
# simulate some data...
set.seed(2)
dat <- gamSim(1,n=400,dist="normal",scale=2)
# fit a simple model
@dill
dill / aes_of_spades.R
Created January 19, 2023 23:59
🤖🍄🎨 get eqipped with aes()
library(ggplot2)
library(emoGG)
library(readr)
library(tidyr)
library(dplyr)
# let's visualise the content of season 1 of Bob Ross' The Joy of Painting
dat <- read_csv("https://raw.githubusercontent.com/fivethirtyeight/data/master/bob-ross/elements-by-episode.csv")
# just get what we need and select season 1
@dill
dill / Pig_jawbone_Whitey_only.csv
Last active May 6, 2021 19:40
🐷🦷 cursed data from Hodges chapter 4
Transect CorMdA distmm ElMod Hard
1 C 0.015 5.3 0.13
1 C 0.045 6.5 0.21
1 C 0.06 6.7 0.19
1 C 0.075 5.2 0.14
1 C 0.195 7.1 0.14
1 C 0.21 6.1 0.11
1 C 0.225 5.6 0.1
1 C 0.24 8 0.15
1 C 0.255 7.5 0.15
@dill
dill / badnames.R
Created March 19, 2021 10:40
Assess consistency of argument and function word separators in a package
# find bad variable/function names in a package
library(stringr)
# retrieve args and functions exported by a package
get_pkgdat <- function(pkgname){
contents <- objects(paste0("package:", pkgname))
fns <- Filter(function(x) is.function(eval(parse(text=x))), contents)
@dill
dill / dhnpt.R
Created October 5, 2020 09:44
point transect distance sampling in Nimble (half-normal with 2 detection function covariates)
# need to define half-normal point transect detection function pdf
dhnpt <<- nimbleFunction(
run = function(x = double(0), b0 = double(0), b1 = double(0),
covar = double(0), width = double(0),
log = integer(0, default = 0)) {
returnType(double(0))
# calculate scale parameter
sigma <- exp(b0 + b1*covar)
@dill
dill / usfws_palette.R
Created September 18, 2020 10:20
🐟🎨📈USFWS colours from https://www.instagram.com/p/CFP2H7JBSxR/
#' USFWS palette generator
#'
#' Palettes taken from https://www.instagram.com/p/CFP2H7JBSxR/
#'
#' @param n Number of colours desired (max 5!). If omitted, uses all colours.
#' @param name one of "sockeye", "coaster", "dolly" or "florida"
#' @param type Either "continuous" or "discrete". Use continuous if you want
#' to automatically interpolate between colours.
#' @return A vector of colours.
#' @export
@dill
dill / bad_code.R
Created August 18, 2020 15:51
gratia overplotting
# put all yr models in a list
model_list <- list(b_norm, b_term, b_term_sel)
# name them for nice labels
names(model_list) <- c("No selection", "Shrinkage smoother", "Extra penalty")
# all the terms (there's probably a gratia built-in for this)
term_list <- c("s(Depth)", "s(Bottom)", "s(Surface)")
library(gratia)
# pre-storage
@dill
dill / mrf_ti.R
Last active July 30, 2020 20:55
t(mrf, time) but trying to not have a ti(time) term
# Markov Random Fields with temporal interactions
# based on https://gist.github.com/dill/ecfe7d2f0e542bb274ff
# David L Miller 2020
# Released under MIT license, YMMV
# example from ?mgcv::smooth.construct.mrf.smooth.spec
library(mgcv)
## Load Columbus Ohio crime data (see ?columbus for details and credits)
@dill
dill / extr.R
Created May 29, 2020 06:44
Forecasting with B-splines
# extrapolating into the future with B-splines
# based on code in ?smooth.construct.bs.smooth.spec
library(mgcv)
# annual diameter of women’s skirts at the hem 1866 to 1911
# Hipel and McLeod, 1994
skirts <- scan("http://robjhyndman.com/tsdldata/roberts/skirts.dat",skip=5)
skirtseries <- data.frame(year=1866:1911, diam=skirts)