Skip to content

Instantly share code, notes, and snippets.

@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)
@dill
dill / contour_experiment.R
Created July 16, 2018 20:57
silly experiment with contours
###
n <- 200
add_layer <- function(coords=data.frame(x=0, y=0), layer=0){
so_rad <- seq(0, 2*pi-0.1, len=n)
if(layer==0){
lens0 <- 0.1*runif(1, 0.1, 1)
@dill
dill / figure-captions.R
Created April 14, 2018 12:31
knitr captions in HTML
## this code makes figure captions work in HTML output
# adapted from http://gforge.se/2014/01/fast-track-publishing-using-knitr-part-iii/
library(knitr)
options(figure_counter = TRUE)
# Evaluate the figure caption after the chunk,
# sometimes you want to calculate stuff inside the
# chunk that you want to include in the caption and
# it is therefore useful to evaluate it afterwards.