Skip to content

Instantly share code, notes, and snippets.

View ateucher's full-sized avatar

Andy Teucher ateucher

View GitHub Profile
-- Before running, load ecoregions, ecosections, bec, nr regions
-- to the db with the same schema.table name as in bcgw
-- This assumes using ogr2ogr for the load, it creates the objectid columns
-- ----------------------------
-- first, overlay designatedlands with nr regions
-- ----------------------------
DROP TABLE IF EXISTS nr_sub;
CREATE TABLE nr_sub AS
@ateucher
ateucher / rasterizing
Created September 26, 2018 19:04 — forked from boshek/rasterizing
Comapring POSTGIS rasterization with fasterize
library(sf)
library(dplyr)
library(fasterize)
shape <- "MULTIPOLYGON (((1126095 1246073, 1126221 1246053, 1126358 1246059, 1126383 1245909, 1126457 1245800, 1126605 1245831, 1126756 1245812, 1126812 1245652, 1126805 1245526, 1126841 1245377, 1126859 1245227, 1126868 1245077, 1126881 1244927, 1126949 1244805, 1126954 1244667, 1126806 1244635, 1126658 1244602, 1126508 1244591, 1126358 1244591, 1126283 1244701, 1126248 1244825, 1126256 1244951, 1126219 1245100, 1126197 1245249, 1126175 1245374, 1126145 1245498, 1126114 1245622, 1126025 1245743, 1125919 1245839, 1125803 1245897, 1125638 1245928, 1125547 1246037, 1125693 1246143, 1125842 1246150, 1126095 1246073)))"
tictoc::tic()
## Actually connect to the database
@ateucher
ateucher / mgcv-posterior-animate.R
Created September 5, 2018 23:39 — forked from noamross/mgcv-posterior-animate.R
Animating smoothing uncertainty in a GAM
library(tidyverse)
library(gganimate)
library(mgcv)
library(mvtnorm)
# Fit a GAM to the data
mod <- gam(hp ~ s(mpg), data=mtcars, method="REML")
# Get the linear prediction matrix
newdat = data.frame(
@ateucher
ateucher / derivSimulCI.R
Created June 22, 2018 21:25 — forked from gavinsimpson/derivSimulCI.R
First derivatives and simultaneous confidence intervals for GAM spline terms
`derivSimulCI` <- function(mod, n = 200, eps = 1e-7, newdata, term,
samples = 10000) {
stopifnot(require("MASS"))
if(inherits(mod, "gamm"))
mod <- mod$gam
m.terms <- attr(terms(mod), "term.labels")
if(missing(newdata)) {
newD <- sapply(model.frame(mod)[, m.terms, drop = FALSE],
function(x) seq(min(x), max(x) - (2*eps), length = n))
names(newD) <- m.terms
@ateucher
ateucher / simulate.gamm.R
Created June 22, 2018 21:24 — forked from gavinsimpson/simulate.gamm.R
S3 method for simulate() for "gamm" objects from package mgcv
`simulate.gamm` <- function(object, nsim = 1, seed = NULL, newdata,
freq = FALSE, unconditional = FALSE, ...) {
if (!exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE))
runif(1)
if (is.null(seed))
RNGstate <- get(".Random.seed", envir = .GlobalEnv)
else {
R.seed <- get(".Random.seed", envir = .GlobalEnv)
set.seed(seed)
RNGstate <- structure(seed, kind = as.list(RNGkind()))
## GOAL:
## re-create a figure similar to Fig. 2 in Wilson et al. (2018),
## Nature 554: 183-188. Available from:
## https://www.nature.com/articles/nature25479#s1
##
## combines a boxplot (or violin) with the raw data, by splitting each
## category location in two (box on left, raw data on right)
# initial set-up ----------------------------------------------------------
@ateucher
ateucher / setup.sh
Created September 20, 2017 20:00 — forked from smnorris/setup.sh
# install gdal, python, postgresql/postgis on Ubuntu 16.04
# install dependencies
sudo apt install unzip
sudo apt-get update
sudo apt-get install python-pip
sudo add-apt-repository ppa:ubuntugis/ppa
sudo apt-get update
sudo apt-get install gdal-bin libgdal-dev
pip install --upgrade pip
@ateucher
ateucher / git-function.sh
Last active September 19, 2017 17:53 — forked from depressiveRobot/git-function.sh
bash function to override git init/clone commands
function git() {
for i do
lastArgument=$i # last argument can be the directory or the repository url
done
command git $@
if [[ $? -eq 0 ]] # only show prompt if git command was successful
then
@ateucher
ateucher / git-email-prompt.sh
Created September 19, 2017 17:48 — forked from depressiveRobot/git-email-prompt.sh
prompt for email address to use for current git repository
#!/bin/bash
# bash prompt which asks for email address
# to configure for current git repository
# set your available emails
MAILS=(private@example.com work@example.com phd@example.com)
# prompt for email
echo