Skip to content

Instantly share code, notes, and snippets.

View jebyrnes's full-sized avatar

Jarrett Byrnes jebyrnes

View GitHub Profile
@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 / 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 / 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 / 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 / discrete_grrrowth.R
Created October 2, 2018 03:58
How to use purrr::iwalk to do discrete time logistic growth models.
#'----------------
#' Discrete time logistic growth model
#' Using purrr and iwalk
#'----------------
library(purrr)
r <- 3.95237783423
n0 <- 0.3
library(sf)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
oceans <- ne_download(type = "ocean", category = "physical", returnclass="sf")
ocean_antarctic <- st_transform(oceans, 2154)
ggplot(ocean_antarctic) +
geom_sf(fill = "lightblue", color=NA)
@jebyrnes
jebyrnes / glm_profile.R
Last active April 22, 2019 21:20
Take a glm and creates a deviance profile, as MASS::profile only returns a transformed version of the deviance.
library(ggplot2)
#A glm from the glm helpfile
counts <- c(18,17,15,20,10,20,25,13,12)
outcome <- gl(3,1,9)
treatment <- gl(3,3)
print(d.AD <- data.frame(treatment, outcome, counts))
glm.D93 <- glm(counts ~ outcome + treatment, family = poisson())
#The old way...
@jebyrnes
jebyrnes / sst_lambert.R
Created October 6, 2018 03:10
SST change in the lambert projection
library(raster)
#devtools::install_github("jebyrnes/hadsstr")
library(hadsstr) #from jebyrnes/hadsstr
library(ggplot2)
library(sf)
library(rnaturalearth)
#get the world in the lambert projection
countries_50_sf <- ne_download(scale = 50, category = 'cultural', type = 'countries', returnclass="sf")
countries_lambert <- st_transform(oceans, 2154)
@jebyrnes
jebyrnes / bayesian_posthoc.R
Last active February 10, 2023 20:29
Posthoc contrasts with emmeans, tidybayes, and brms
library(tidyverse)
library(emmeans)
library(brms)
library(tidybayes)
warp.brms <- brm(breaks ~ wool * tension, data = warpbreaks)
#get the adjusted means
warp_em <- emmeans (warp.brms, ~ tension | wool)
warp_em
brainGene <- structure(list(group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L), .Label = c("control", "schizo", "bipolar"
), class = "factor"), PLP1.expression = c(-0.02, -0.27, -0.11,
0.09, 0.25, -0.02, 0.48, -0.24, 0.06, 0.07, -0.3, -0.18, 0.04,
-0.16, 0.25, -0.1, -0.31, -0.05, 0.11, -0.38, 0.23, -0.23, -0.28,
-0.36, -0.22, -0.4, -0.19, -0.34, -0.29, -0.12, -0.34, -0.39,