Skip to content

Instantly share code, notes, and snippets.

View mbjoseph's full-sized avatar

Max Joseph mbjoseph

View GitHub Profile
@mbjoseph
mbjoseph / get_lsat.rasters.R
Last active June 7, 2017 19:51
Fetch rasters from USGS server
library(RCurl)
# Fetching burn rasters ---------------------------------------------------
# get and read the file listing from the usgs server
prefix <- 'https://rmgsc.cr.usgs.gov/outgoing/baecv/BAECV_CONUS_v1_2017/'
download.file(prefix, destfile = 'listing.txt')
out <- readLines('listing.txt')
# now create paths to each tar.gz file by processing the html
@mbjoseph
mbjoseph / HDF_to_TIF.R
Created March 10, 2017 20:36
Setting raster projections, extent, and computing annual summaries
library(rgdal)
library(rgeos)
library(raster)
library(gdalUtils)
# gdalinfo("GFED40_MQ_199506_BA.hdf")
# sds <- get_subdatasets("GFED40_MQ_199506_BA.hdf")
# sds
@mbjoseph
mbjoseph / main.ipynb
Created January 16, 2017 21:41
sad error
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbjoseph
mbjoseph / parallel-loop-unroll.R
Last active December 9, 2016 17:38
How to unroll a nested for-loop in R so that it can be run in parallel
library(parallel)
# Create matrix to store result -------------------------------------------
M <- 4
N <- 6
A <- matrix(0, nrow = M, ncol = N)
list <- list(3, 4, 6)
# Serial version ----------------------------------------------------------
@mbjoseph
mbjoseph / small-sample-weibull.R
Created November 22, 2016 20:47
Exploring how sample size affects the estimates of Weibull shape and scale parameters
# Script to evaluate small sample behavior of Weibull parameter MLEs --------
library(fitdistrplus)
library(dplyr)
library(tidyr)
library(gridExtra)
library(parallel)
library(ggplot2)
library(viridis)
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@mbjoseph
mbjoseph / leung_drton_factor_analysis.R
Last active January 16, 2020 08:56
Factor analysis sandbox
# Simulation script for factor analysis ala Leung & Drton (2016) ----------
library(rstan)
library(bayesplot)
m <- 5 # dimension of observed data (e.g., # traits)
k <- 2 # number of latent factors
n <- 100 # number of sample units (e.g., # species)
# residual variance matrix (is diagonal)
Omega <- diag(.3 + abs(rnorm(m, sd = .3)))
@mbjoseph
mbjoseph / lm_segs.stan
Last active September 12, 2016 04:38
Piecewise regression w/ unknown breakpoint in Stan
data {
int<lower=1> n;
vector[n] x;
vector[n] y;
}
parameters {
real alpha;
vector[2] beta;
real<lower=0> sigma;
# Importing data from USDA into large data file ---------------------------
library(stringr)
library(dplyr)
path_to_data <- "~/Desktop/ams_cattle_data/"
data_files <- list.files(path = path_to_data, pattern = "cattle",
full.names = TRUE)
# Define helper functions -------------------------------------------------