Skip to content

Instantly share code, notes, and snippets.

@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)
@dill
dill / andy.R
Last active May 20, 2020 10:32
Projection matrices with fit=FALSE
library(mgcv)
set.seed(2) ## simulate some data...
dat <- gamSim(1,n=400,dist="normal",scale=2)
b <- gam(y~s(x1), data=dat, fit=FALSE)
newpred <- data.frame(x1=seq(0, 10, length.out=100))
# Lp for the data
lp_data <- PredictMat(b$smooth[[1]], dat)
@dill
dill / tax.R
Last active October 8, 2019 10:42
NYT tax rate data
# Tax data from NYT article
# data scraped from https://www.nytimes.com/interactive/2019/10/06/opinion/income-tax-rate-wealthy.html
# load data scraped from SVG
tax <- readLines("taxdat.dat")
taxy <- c()
for(i in 1:length(tax)){
@dill
dill / cloudy_day.R
Last active February 21, 2019 19:55
☁️🎲💻
# generate some relaxing cloudy sky squares
#
# David Lawrence Miller (2019)
library(INLA)
library(ggplot2)
## function to simulate spatial fields
simdat <- function(n, seed, prior.range = c(5, .05),
prior.sigma = c(.05, .05)){
@dill
dill / good_modelz.R
Last active August 27, 2020 00:42
NBER isn't good at models
# data digitized from https://twitter.com/nberpubs/status/1062080096549261312
# thanks @tpoi and co! https://cran.r-project.org/web/packages/digitize/index.html
# load the above data
dat <- read.csv("nber.csv")
# it's ya boi, mgcv
library(mgcv)
# fit the next stupidest model
@dill
dill / dat.csv
Last active November 5, 2019 16:15
deep dive into a terrible graphic (laffer curve)
tax_rate tax_rev
0.07357329 -0.0567062
8.81285407 2.4505785
12.84385295 3.5807126
16.23680790 2.1860302
19.18260073 2.4643621
19.29050823 1.9514672
18.45218145 1.2480201
26.73285538 1.5886201
22.37159417 3.3657369
Hat Season Episode
Thick 6 9
Wet To Activate 4 3
Ho Ho Horny 2 9
Horny 3 1
Horny 3 1
Rods 1 9
Feet 6 6
Feet 6 7
Squeeze It 1 15
@dill
dill / preds.R
Created September 8, 2018 07:40
Calculate the average abundance estimates and uncertainty over multiple time periods
# average predictions
library(Distance)
library(dsm)
# download data from http://workshops.distancesampling.org/stand-intermed-2018/practicals/spermwhale.RData
load("spermwhale.RData")
# make sure we get the same results every time
set.seed(123)
@dill
dill / thingo.R
Created September 3, 2018 06:22
Uncertainty map animation
# little animation to illustrate uncertainty in a density map
library(dsm)
library(mvtnorm)
library(ggplot2)
library(animation)
library(viridis)
library(cowplot)
set.seed(1997)