Skip to content

Instantly share code, notes, and snippets.

Avatar

Max Joseph mbjoseph

View GitHub Profile
@mbjoseph
mbjoseph / distance-sampling.stan
Created Oct 7, 2021
Poisson-binomial multinomial distance sampling model in Stan, with half-normal detection function
View distance-sampling.stan
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 Aug 26, 2021
Visualizing execution time for tests within a file using testthat's ListReporter
View plot-test-times.R
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 Feb 25, 2021
Minimal example of a Bayesian finite sample approach for wildfires using brms
View minimal-wildfire-brms.R
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 Jun 24, 2020
Distance sampling in Stan
View dist.R
# 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 Jun 4, 2020
Zero one inflated beta distribution in PyTorch
View zoib.ipynb
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
Example of using grouped linear interpolation
View linear-interp-spectra.R
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
Predicting for new factor levels in a mixed effects model - lme4
View lme4-predict-new-ranef-levels.R
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),
View multiscale-occupancy.R
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 Mar 6, 2020
Introduction to gradient descent with pytorch
View sgd-intro.ipynb
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 Mar 27, 2019
Timing experiments: GeoPandas' overlay() and earthpy's clip_shp
View timing-overlay-vs-clip_shp.ipynb
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.