Skip to content

Instantly share code, notes, and snippets.

View rBatt's full-sized avatar

Ryan Batt rBatt

  • TRM Labs
  • New York, New York
View GitHub Profile
@rBatt
rBatt / read_jho.R
Last active April 4, 2016 23:13
How to read a data file with 2 dimensional data interspersed with comments, in R
#' Read JHo
#'
#' Read in a file formatted like Jennifer's data sets
#'
#' @param file a character indicating the file name (and possibly path)
#' @param n_nrows integer indicating number of rows for each matrix
#' @param n_matrices integer indicating number of matrices in the file
#' @param n_comment integer indicating the number of comment lines preceding each matrix of data; should include blank lines
#' @param n_top_comment integer indicating the number of comment lines that are unique to the start of the file; default is 0 lines
#' @param ... arguments to be passed to \code{scan}, such as \code{what=numeric()} and \code{sep=","}
@rBatt
rBatt / urchin_meanShift.R
Created April 20, 2016 00:29
detect a breakpoint
# ============
# = Packages =
# ============
# to do everything in this script, make sure the following packages are installed
# install.packages(c("rgenoud", "strucchange"))
# =============
# = Functions =
@rBatt
rBatt / sub_clean.neus_jho.R
Created June 16, 2016 21:43
subset neus for jho
library("data.table")
load("clean.neus.RData")
s2c <- function (x, type = "list")
{
as.call(lapply(c(type, x), as.symbol))
}
colsuwant <- c("depth","btemp","stemp","bsalin","ssalin")
jho_cneus <- clean.neus[spp=="Paralichthys dentatus", structure(lapply(eval(s2c(colsuwant)),meanna, na.rm=TRUE),.Names=colsuwant),by=c("reg","datetime","year","stratum","lon","lat","haulid","season","station","spp","common")]
setkey(jho_cneus, datetime, year, stratum, station)
@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
### 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")
# 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")
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
@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')
@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 / 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