Skip to content

Instantly share code, notes, and snippets.

View pboesu's full-sized avatar

Philipp Boersch-Supan pboesu

View GitHub Profile
@noamross
noamross / coupling.R
Last active January 18, 2022 16:29
Calculating spatial correlations in a MRF
library(mgcv)
library(spdep)
library(sf)
library(ggplot2)
# Columbus crime data from mgcv/spdep
example(columbus)
# Make a neighborhood object compatible with mgcv's MRF
nb <- poly2nb(columbus)
library(mgcv)
set.seed(3);n <- 400
############################################
## First example: simulated Tweedie model...
############################################
dat <- gamSim(1,n=n,dist="poisson",scale=.2)
dat$y <- rTweedie(exp(dat$f),p=1.3,phi=.5) ## Tweedie response
b <- gam(y~s(x0)+s(x1)+s(x2)+s(x3),family=tw(),
@mbjoseph
mbjoseph / dist.R
Last active June 24, 2020 07:03
Distance sampling in Stan
# Distance sampling model with data augmentation.
# Based on section 8.3.1 of the Applied Hierarchical Modeling book by Royle and Kery
library(rstan)
B <- 50
# note that I'm dividing by 10 and adding 1e-6 to put this on a manageable
# scale and prevent values == 0
d_obs <- c(71.93, 26.05, 58.47, 92.35, 163.83, 84.52, 163.83, 157.33,
@noamross
noamross / partition.R
Last active October 27, 2020 13:13
A quick greedy algorithm to partition unequal-sized groups into near-equal shards
#' Function to partition unequal sized groups into shards of similar size.
#'
#' Based on the greedy algorithm described in [The Wikipedia article on the
#' the partitioning problem](https://en.wikipedia.org/wiki/Partition_problem#The_greedy_algorithm)
#'
#' @param groups_vector An integer vector of group ids, such as a group ID
#' column in a data frame
#' @param n_shards The number of shards to split groups up into
#' @examples
#' n_groups <- 200
@noamross
noamross / mgcv-posterior-animate.R
Created September 2, 2018 00:29
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(
@dill
dill / ebird_sp_disc.R
Created January 9, 2018 09:37
make an eBird species discovery curve
### species discovery curve from eBird
library(lubridate)
# life list
life <- read.csv("ebird_world_life_list.csv")
life$date <- lubridate::dmy(as.character(life$Date))
#sort
life <- life[order(life$date),]
@dill
dill / better_check.R
Last active September 14, 2021 17:09
Combined deviance and quantile residuals check plots for GAMs
library(gridBase)
library(grid)
library(mgcv)
library(statmod)
## nicer version of gam.check/rqgam.chack
# - deviance residuals for the Q-Q and histogram
# - RQR for resids vs linear pred
# - response vs fitted
# hist.p gives the quantiles of the residuals to show
better_check <- function(b, k.sample = 5000, k.rep = 200, rep = 0, level = 0.9,
@dill
dill / uncertainty-anim.R
Last active February 23, 2017 16:55
Animations of uncertainty in density maps
# little animation to illustrate uncertainty in a density map
library(dsm)
library(mvtnorm)
library(ggplot2)
library(animation)
library(viridis)
# load the models and prediction data
# using data from the Duke spatial distance sampling course
@psychemedia
psychemedia / numbers2words.R
Created July 12, 2015 00:12
R function to convert numbers to words
#https://github.com/ateucher/useful_code/blob/master/R/numbers2words.r
numbers2words <- function(x){
## Function by John Fox found here:
## http://tolstoy.newcastle.edu.au/R/help/05/04/2715.html
## Tweaks by AJH to add commas and "and"
helper <- function(x){
digits <- rev(strsplit(as.character(x), "")[[1]])
nDigits <- length(digits)
if (nDigits == 1) as.vector(ones[digits])

Guidelines for contribution

This document described guidelines for contributions for projects led by members of the [Poisot lab of computational ecology][pl], Université de Montréal. It should be followed by lab members (at all times), as well as people with suggested added features, or willing to contribute enhancement or bug fixes.

Authorship

Any contribution to the code of a software grants authorship on the next paper