Skip to content

Instantly share code, notes, and snippets.

View jebyrnes's full-sized avatar

Jarrett Byrnes jebyrnes

View GitHub Profile
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)
@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,
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 / 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))
@jebyrnes
jebyrnes / rethinking_measurement_errors.R
Created November 27, 2016 15:43
Rethinking Measurement Error - models using the rethinking package that incorporate measurement in both the response and predictor variables.
library(rethinking)
library(dplyr)
data(WaffleDivorce)
mod <- alist(
#likelihood
div_est ~ dnorm(mu, sigma),
#model
@jebyrnes
jebyrnes / casey_mixed_mod_power.R
Created October 1, 2016 22:20
An a priori power analysis of a mixed model design from the terHorst lab. Just for fun.
#Some libraries that will help
library(nlme)
library(dplyr)
library(tidyr)
library(mvtnorm)
######This controls it all!
n_sims <- 5
#A function to make a sigma matrix
@jebyrnes
jebyrnes / testFrame.R
Last active September 26, 2016 13:34
testFrame <- structure(list(Year = c(1948, 1949, 1950, 1951, 1952), `X_-177_52` = c(18.9220237749285,
21.8483886536779, 18.620676780609, 27.0751608868709, 32.4921595618974
), `X_-172.5_52` = c(31.9994862200926, 38.5639899672935, 31.836112737235,
42.7914952437946, 53.1652576093285), `X_-168_53` = c(26.226590342774,
35.5284697356814, 26.2798166889216, 37.5791359982477, 45.5915562248318
), `X_-136.5_58` = c(24.7796363128934, 24.5255804518598, 16.5003662047736,
23.5168277681755, 32.9788961278221), `X_-132_53` = c(19.5107068615539,
19.6662279616348, 16.0907033411067, 19.1839879174579, 24.0906788816321
), `X_-130.5_52` = c(25.4554902780508, 27.105312123341, 25.5231126523454,
25.9769619101539, 33.7769913419713), `X_-127.5_50` = c(31.9535070030102,
@jebyrnes
jebyrnes / had_crut_spider_plot.R
Created May 13, 2016 18:40
R code to reproduce the awesome visualization of global temperature change from Ed Hawkins at http://www.climate-lab-book.ac.uk/2016/spiralling-global-temperatures/ using R and ggplot2 (with the animations package)
library(dplyr)
library(tidyr)
library(ggplot2)
library(animation)
#Data from https://crudata.uea.ac.uk/cru/data/temperature/
#As well as data read in script
source("read_cru_hemi.R")
temp_dat <- read_cru_hemi("./HadCRUT4-gl.dat")
#remove cover
library(dplyr)
library(ggplot2)
library(meowR); data(regions)
library(sp)
kelp <- read.csv("../01_clean_raw_data/temporal_data_REBENT_Brittany_NW_France.csv")
#Create a spatial Points Data Frame
@jebyrnes
jebyrnes / conditional_independence_dcov.R
Created March 28, 2016 17:00
A simulation to look at how distance based correlation can work with D-sep tests
#############
#' @title Fisher's C and Distance Correlation
#'
#' @author Jarrett Byrnes
#'
#' @description A simulation to look at
#' how distance based correlation can work with
#' D-sep tests
#'
#############