Skip to content

Instantly share code, notes, and snippets.

View mbjoseph's full-sized avatar

Max Joseph mbjoseph

View GitHub Profile
@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")
library(rstan)
options(mc.cores = parallel::detectCores())
rstan_options(auto_write = TRUE)
psi <- .2
theta <- .3
p <- .5
nsite <- 100
ntime <- 4
@mbjoseph
mbjoseph / zoib-recovery.R
Created November 27, 2017 07:33
Simulation for zero-one inflated beta regression in Stan
## DATA GENERATION
# reproducibility
library(bayesplot)
set.seed(1839)
# matches the stan function
inv_logit <- function(x){
exp(x) / (1 + exp(x))
}
@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 / 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 / reticulate-demo.Rmd
Created December 19, 2018 02:38
Using earthpy in R Markdown
---
title: "Using earthpy via R Markdown"
author: "Max Joseph"
date: "December 18, 2018"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(reticulate)
@mbjoseph
mbjoseph / vioplot2.R
Last active December 9, 2020 17:06
Slight change to the vioplot function from Daniel Adler that plots median lines instead of points, and gives the option to plot either side of a violin rather than always both.
vioplot2 <- function (x, ..., range = 1.5, h = NULL, ylim = NULL, names = NULL,
horizontal = FALSE, col = "magenta", border = "black", lty = 1,
lwd = 1, rectCol = "black", colMed = "white", pchMed = 19,
at, add = FALSE, wex = 1, drawRect = TRUE, side="both")
{
datas <- list(x, ...)
n <- length(datas)
if (missing(at))
at <- 1:n
upper <- vector(mode = "numeric", length = n)
@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.