Skip to content

Instantly share code, notes, and snippets.

View rBatt's full-sized avatar

Ryan Batt rBatt

  • Newell
  • Philadelphia, PA
View GitHub Profile
@rBatt
rBatt / LM_mbkkp_test.R
Last active March 27, 2018 17:25 — forked from cbuelo/LM_mbkkp_test.R
LakeMetabolizer meta.bookkeep() example
#Test case for calculating metabolism
library(LakeMetabolizer)
#set up test example where DO starts and ends at same level, is at saturation (= no flux)
DOobs = c(3, 2, 3, 4, 3, 2)
DOsat = c(3, 2, 3, 4, 3, 2)
kGas = c(.2, .2, .2, .2, .2, .2)
zMix = c(1, 1, 1, 1, 1, 1)
irr = c(0, 1, 1, 0, 0, 0)
x = arima.sim(model=list(ar=0.9), n=200)
acf(x)
pacf(x
@rBatt
rBatt / scatterLine.R
Last active April 7, 2017 01:12
Scatter Plot with per-subset regression lines
#' Scatter plot with certain groups of points getting own regression and fitted line
#'
#' @param Data a data.table
#' @param x,y,lineBy character, name of column in \code{Data}
#' @param ptCol,lineCol character or quoted expression (call) for point or line color, repsectively
#' @param ... arguments to be passed to \code{\link{plot}} and \code{link{lines}}
#'
#' @examples
#' dt <- data.table(
#' x=x<-rnorm(9, sd=2), # why <- is better than =
@rBatt
rBatt / beta_range.R
Last active April 26, 2017 18:16
What the relationship between species' range size (cross-species average) and spatial beta diversity?
# ==============================
# = Simulating Spreading Range =
# ==============================
# ---- setup ----
nspp <- 25
nsite <- 25
nr <- nsite
nc <- nspp
n_iter <- 25
@rBatt
rBatt / all1_1all_betaDiv.R
Last active April 1, 2017 01:50
If all species occupy only 1 site, does beta diversity depend on whether they occupy different sites or overlap?
nspp <- 10
nsites <- 10
Y_1sppPer <- diag(x=1, nrow=nspp, ncol=nsites)
Y_allSpp1Site <- rbind(rep(1,nspp), matrix(rep(0,nspp*nsites-nspp),nrow=nsites-1))
# euclidean distance
euc <- function(x){
sum(scale(x,scale=FALSE)^2)/(nrow(x)-1)
}
@rBatt
rBatt / reformatCoverage.R
Last active March 29, 2017 13:42
How much space does one save by reformatting the coverage .txt files? For SGW
library(reshape2)
hasDT <- library(data.table, logical.return=TRUE) # load package, if not installed return FALSE
# ==================
# = Make Fake Data =
# ==================
# ---- Options affecting size of 'big' ----
nScaff <- 50 # number of scaffolds (or 'pages')
nIndex <- 100 # number of loci or indices (or 'words')
la <- c(Okanagan=348, Seneca=173, Cayuga=172, Ballston=1.10884, Reindeer=5650, Somyn=NA, Thetis=8.336524, Manitoba=4624, Cold=373, Winnipegosis=5370, Crescent=18.401866, Simcoe=722, Muskrat=12.02, Superior=127700, Ontario=7340, Timiskaming=295, Memphremagog=110, Tahoe=490, Tarpon=10, Fulks=NA, Flathead=510, Hopatcong=10, Champlain=1331, Willoughby=1687, Erie=25667, Bear=280, Raystown=33.6, GreatSalt=4400, Arenal=85, NahuelHuapi=530, Dilolo=12.35, MamfePool=NA, DiloloSwamps=NA, Edward=2325, MaikaMarshes=NA, Victoria=68800, Makele=NA, Tangankyika=32900, Bangweulu=15100, KafueFlats=NA, Mweru=5120, IshibaNgandu=NA, Kariba=5580, Bassenthwaite=5.1, Ness=56, Morar=26.7, Maree=28.6, Lagarfljot=53, LoughLeane=19, Muckross=2.7, UpperLake=1.7, Como=146, Seljordsvatnet=16.7, Storsjon=464, Gryttjen=NA, Brosno=7.55, Labynkyr=61, LakeVan=3755, Kanas=45.73, TianChiHeaven=9.82, Ikeda=11, Kussharo=79.3, Pinatubo=1.83, Modewarre=NA, Utopia=14) # area in km^2; Dilolo I measured on google maps and I excluded the NE corner that lo
# dev.new(width=3.5, height=3.5)
png("~/Desktop/test2.png", width=3.5, height=3.5, res=150, units='in')
par(mfrow=c(2,1), mar=c(2.5,2.5,0.25,2.5), ps=8, cex=1, mgp=c(1,0.25,0), tcl=-0.15)
plot(1:10, xlab="x", ylab="y", type='o')
par(new=TRUE)
plot(x=1:10, y=cumsum(rnorm(10)), xlab="", ylab="", type='o', col='red', xaxt="n", yaxt="n")
### Get a list of all your installed packages (so that you can reinstall them after updating R)
a <- installed.packages()
my_pkgs <- dimnames(a)[[1]]
save(my_pkgs, file="my_pkgs.RData")
# exit R, reopen R, then continue to following
load("my_pkgs.RData")
@rBatt
rBatt / bootstrap_overlap.R
Created July 12, 2016 17:46
how to bootstrap the overlap data, and also checking out why you can't just use the probabilities
set.seed(1337)
mean_true_sppA <- 0.25
mean_true_sppB <- 0.75
grid_w <- 5
grid_h <- 5
environment <- seq(0.8, 1.2, length.out=grid_w*grid_h) # makes it so the 2 distributions are not independent
n_reps <- 20
probs_true_sppA <- plogis(rnorm(grid_w*grid_h, qlogis(mean_true_sppA*environment))) # the GAM model estimates these values