Skip to content

Instantly share code, notes, and snippets.

View jebyrnes's full-sized avatar

Jarrett Byrnes jebyrnes

View GitHub Profile
@jebyrnes
jebyrnes / glm_profile.R
Created January 3, 2017 22:09
gets a profile of a GLM using offset
library(dplyr)
adf <- data.frame(x=1:100) %>%
mutate(y=rnorm(100, 5*x, 90))
#What do we have
plot(adf)
#examine the output
summary(glm(y ~ x, data=adf))
MULTIPOLYGON (((-173.3941876799999875 55.5980717300000009, -168.5559317000000021 54.0107796100000002, -165.6772025799999994 54.5778467100000029, -165.1042097499999954 54.5660556999999997, -164.9849046600000122 54.5636006399999971, -164.9369200099999944 54.5213987699999976, -164.9299005899999884 54.5152252800000028, -164.9050902399999927 54.4934049100000024, -164.8942196200000012 54.4838443399999974, -164.8036491500000125 54.4038111899999990, -164.8030237199999988 54.4032585200000014, -164.7004447299999867 54.3121036300000029, -164.6969431500000098 54.3029074499999993, -164.5301978699999950 53.8649844400000006, -164.5293457399999966 53.8617368299999981, -163.7620149299999923 50.9373064099999979, -164.0171571799999981 50.8472377000000009, -164.1599166299999979 50.8198464999999970, -164.3274076799999932 50.7914016200000020, -164.5069552200000089 50.7410382200000001, -164.7422776100000021 50.7218656800000005, -164.8138120300000082 50.6738661099999987, -164.9352386699999897 50.5929327200000003, -164.98966272999999
@jebyrnes
jebyrnes / gamma_sample.R
Created April 10, 2017 19:19
a data set for gamma use
gamma_sample <- structure(list(Duration = c(44, 112, 60, 90, 1, 335, 1370, 425,
2920, 3, 3, 420, 10, 60, 4, 365, 330, 90, 335, 60, 60, 60, 0.083,
0.083, 24, 15, 5, 12, 956, 120, 90, 15, 10, 10, 54, 120, 16,
24, 13, 1800, 308, 365, 35, 1800, 14, 335, 14, 203, 22, 21, 570,
153, 1, 730, 122, 155, 183, 90, 21, 488, 60, 155, 40, 28, 105,
3, 365, 730, 65, 20, 165, 56, 365, 90, 21, 16, 365, 18, 12, 2,
84, 120, 840, 1, 15, 30, 180, 84, 56, 120, 25, 150, 150, 460,
810, 4, 4, 5, 910, 550, 2184, 3, 520, 730, 14, 28, 1095, 70,
365, 365, 120, 245, 1095, 56, 121, 130, 414, 58, 990, 680, 750,
240, 540, 450, 255, 730, 32, 365, 120, 1200, 450, 730, 730, 150,
set.seed(1)
x <- 1:150
y <- rbinom(length(x), size = 1, prob = 0.7)
seas_m <- runif(length(x), 0.1, 5)
shape = 0.4
y_true = exp(2 - 0.5 * seas_m)
y <- y*rgamma(length(x), rate = shape / y_true, shape = shape)
non_zero <- ifelse(y > 0, 1, 0)
d <- data.frame(days_at_sea = y, seas_m = seas_m, non_zero = non_zero)
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),
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
@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){
@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 / 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 / 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)