Skip to content

Instantly share code, notes, and snippets.

View mbjoseph's full-sized avatar

Max Joseph mbjoseph

View GitHub Profile
@mbjoseph
mbjoseph / distance-sampling.stan
Created October 7, 2021 02:39
Poisson-binomial multinomial distance sampling model in Stan, with half-normal detection function
data {
int<lower=1> n_site;
int<lower=1> n_distance_bins;
vector[n_distance_bins + 1] bin_breakpoints;
array[n_site, n_distance_bins] int y;
array[n_site] int n_obs;
}
transformed data {
real max_distance = max(bin_breakpoints);
@mbjoseph
mbjoseph / plot-test-times.R
Created August 26, 2021 17:58
Visualizing execution time for tests within a file using testthat's ListReporter
library(forcats)
library(ggplot2)
library(testthat)
path <- testthat_example("success")
l <- test_file(path, reporter = "list")
l %>%
as.data.frame() %>%
ggplot(aes(y = fct_reorder(test, real), x = real)) +
@mbjoseph
mbjoseph / minimal-wildfire-brms.R
Created February 25, 2021 23:40
Minimal example of a Bayesian finite sample approach for wildfires using brms
library(tidyverse)
library(sf)
library(here)
library(brms)
library(lubridate)
library(reshape2)
# Get ecoregion data ------------------------------------------------------
download.file("ftp://newftp.epa.gov/EPADataCommons/ORD/Ecoregions/us/us_eco_l4.zip",
destfile = "ecoregions.zip")
@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,
@mbjoseph
mbjoseph / zoib.ipynb
Created June 4, 2020 14:26
Zero one inflated beta distribution in PyTorch
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbjoseph
mbjoseph / linear-interp-spectra.R
Created May 21, 2020 17:33
Example of using grouped linear interpolation
library(tidyverse)
library(reshape2)
library(pbapply)
n_samples <- 100
n_wavelength <- 400
wavelengths <- matrix(nrow = n_samples, ncol = n_wavelength)
reflectances <- matrix(nrow = n_samples, ncol = n_wavelength)
for (i in 1:n_samples) {
@mbjoseph
mbjoseph / lme4-predict-new-ranef-levels.R
Created May 7, 2020 17:38
Predicting for new factor levels in a mixed effects model - lme4
library(lme4)
# adapted from ?predict.merMod
gm1 <- glmer(cbind(incidence, size - incidence) ~ period + (1 |herd),
cbpp,
binomial)
# add some new levels to herd, which is a random effect
newdata <- with(cbpp,
expand.grid(period=unique(period),
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
psi <- .2
theta <- .3
p <- .5
nsite <- 100
ntime <- 4
@mbjoseph
mbjoseph / sgd-intro.ipynb
Created March 6, 2020 16:33
Introduction to gradient descent with pytorch
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbjoseph
mbjoseph / timing-overlay-vs-clip_shp.ipynb
Last active March 27, 2019 14:04
Timing experiments: GeoPandas' overlay() and earthpy's clip_shp
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.