Skip to content

Instantly share code, notes, and snippets.

View jebyrnes's full-sized avatar

Jarrett Byrnes jebyrnes

View GitHub Profile
@jebyrnes
jebyrnes / nesting_models.R
Created May 17, 2018 19:23
Using gapminder to show how tidyr and broom and dplyr can fit a lot of models all at once.
library(tidyverse)
library(gapminder)
library(broom)
library(ggplot2)
life_exp_mods <- gapminder %>%
#do this for each country
group_by(country) %>%
@jebyrnes
jebyrnes / plot_poly_fits.R
Created May 16, 2018 00:38
plot predictions of a model with an interaction and a poly term
library(tidyverse)
library(ggplot2)
library(visreg)
#some fake data
set.seed(35)
my_data <- data.frame(x1 = runif(100,-50,50), x2 = runif(100, -50, 50)) %>%
mutate(y = rnorm(100, 0.001*x2*(x1 - x1^2), 100))
#let's see that data
@jebyrnes
jebyrnes / error.R
Created February 4, 2018 23:46
odd gdal error running polygonizer
Traceback (most recent call last):
File "/usr/local/bin/gdal_polygonize.py", line 36, in <module>
import gdal, ogr, osr
File "/Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages/gdal.py", line 2, in <module>
from osgeo.gdal import deprecation_warn
File "/Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages/osgeo/__init__.py", line 21, in <module>
_gdal = swig_import_helper()
File "/Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages/osgeo/__init__.py", line 17, in swig_import_helper
_mod = imp.load_module('_gdal', fp, pathname, description)
ImportError: dlopen(/Library/Frameworks/GDAL.framework/Versions/1.11/Python/2.7/site-packages/osgeo/_gdal.so, 2): Symbol not found: _GTIFAllocDefn
@jebyrnes
jebyrnes / coastline_crop.R
Last active January 12, 2018 20:16
crop out coasts with R
###########
# Libraries
###########
library(sp)
library(rgdal)
library(rgeos)
library(raster)
library(tidyverse)
library(spdplyr)
@jebyrnes
jebyrnes / sp_v_ggplot_different.R
Created January 3, 2018 18:53
A contrast between sp plot and ggplot with thresholds producing different results
library(tidyverse)
library(broom)
library(sp)
library(spdplyr)
library(ggplot2)
library(rgdal)
library(raster)
library(rgeos)
library(lubridate)
@jebyrnes
jebyrnes / ff_quick_viz.R
Created January 2, 2018 18:53
A brief visualization
library(tidyverse)
library(broom)
library(sp)
library(spdplyr)
library(ggplot2)
library(ggmap)
library(rgdal)
library(raster)
#read in spatialPolygonsDataFrame (shapefile)
@jebyrnes
jebyrnes / brms_sem.Rmd
Created December 22, 2017 19:34
.Rmd file for blog post on brms and bayesian sem
---
title: "Bayesian SEM with BRMS"
author: "Jarrett Byrnes"
date: "12/20/2017"
output: html_document
---
```{r setup, include=FALSE}
#http://sites.tufts.edu/emotiononthebrain/2017/08/12/blog-posting-from-r-markdown-to-wordpress/
knitr::opts_chunk$set(echo = TRUE)
@jebyrnes
jebyrnes / waic_lm.R
Created May 11, 2017 16:00
Functions for calculating WAIC from a linear model
#from rethinking library for numerically stable log sums
log_sum_exp <- function (x) {
xmax <- max(x)
xsum <- sum(exp(x - xmax))
xmax + log(xsum)
}
#function for WAIC from an LM
waic.lm <- function(mod, n.sims=1e3){
library(rethinking)
#make a multivariate vector with known correlation
sigma_mat <- matrix(c(1,.3, 0.2,
0.3, 1, 0.4,
0.2, 0.4, 1), nrow=3)
z <- rmvnorm(1, c(0,0, 0), sigma_mat)
z_2 <- z[2]
z[2] <- NA
kelp_mod_noyear <- alist(
kelp_s ~ dnorm(mu, sigma),
mu <- a[time_idx],
a[time_idx] ~ GPL2( Dmat , etasq , rhosq , delta_sq),
sigma ~ dcauchy(0,5),
etasq ~ dcauchy(0,2),
delta_sq ~ dcauchy(0,2),