Skip to content

Instantly share code, notes, and snippets.

@noamross
noamross / proftable.R
Created November 12, 2012 19:09
Another way to view R profiling Data
proftable <- function(file) {
require(plyr)
sample.interval <- as.numeric(strsplit(readLines(file, 1), "=")[[1L]][2L])/1e+06
profdata <- as.matrix(read.table(file, header=FALSE, sep=" ", colClasses="character", skip=1, fill=TRUE, na.strings=""))
total.time <- nrow(profdata)*sample.interval
stacktable <- data.frame(table(aaply(profdata, 1, function(x) paste(rev(na.omit(x)), collapse=" > "))))
names(stacktable) <- c("Stack","PctTime")
stacktable$PctTime <- 100*stacktable$PctTime/nrow(profdata)
stacktable <- stacktable[order(stacktable$PctTime, decreasing=TRUE), c("PctTime", "Stack")]
rownames(stacktable) <- NULL
@noamross
noamross / fix.csv.R
Created November 19, 2012 21:49
Use R's native data editor to edit a CSV
#' Use the data editor for a CSV file
#'
#' This function loads a CSV file, lets the user edit it in the native data
#' editor, then re-saves it, prompting the user for a new name if desired.
#'
fix.csv <- function(file, new.name=TRUE, sep=",", comment.char="") {
tmpframe <- read.csv(file, sep=sep,quote="", colClasses="character",
stringsAsFactors=FALSE, comment.char="",
blank.lines.skip=FALSE, na.strings="")
tmpframe <- edit(tmpframe)
@noamross
noamross / fix.csv.R
Created November 19, 2012 21:49
Use R's native data editor to edit a CSV
#' Use the data editor for a CSV file
#'
#' This function loads a CSV file, lets the user edit it in the native data
#' editor, then re-saves it, prompting the user for a new file name if desired.
#'
fix.csv <- function(file, new.name=TRUE, sep=",", comment.char="") {
tmpframe <- read.csv(file, sep=sep,quote="", colClasses="character",
stringsAsFactors=FALSE, comment.char="",
blank.lines.skip=FALSE, na.strings="")
tmpframe <- edit(tmpframe)
@noamross
noamross / DRUG_code_LY.Rmd
Created December 3, 2012 19:52
Matrix population models by Lauren Yamane
A discrete time, age-structured model of a salmon population (semelparous) that can live to age 5, with fishing and environmental stochasticity (by Lauren Yamane, presented to UC Davis R Users' Group)
========================================================
Parameter values
```{r parameters, comment="#"}
a=60 #alpha for Beverton-Holt stock-recruitment curve
b=0.00017 #beta for Beverton-Holt
tf=2000 #number of time steps
N0=c(100,0,0,0,0) #initial population vector for 5 age classes
s=0.28 #survival rate with fishing
@noamross
noamross / DRUG plyr code.R
Last active December 25, 2020 08:26
Some examples of using the plyr package for data manipulation
## Some examples using the package plyr
library(plyr)
## Example dataset from ggplot
library(ggplot2)
data(mpg)
str(mpg)
Noam Ross
---------
### 13-01-22 14:50:15
Abstract
========
Forest disease spreads through plant communities structured by species composition, age distribution, and spatial arrangement. I propose to examine the consequences of the interaction of these components of population structure on a model systems of *Phytophthora ramorum* invasion in California redwood forests. First, I will compare the dynamic behavior of a series of epidemiological models that include different configurations of population structures. Then I will fit these models to time-series data from a network of disease monitoring plots to determine what components of forest population structure are most important for prediction of disease spread. Using the most parsimoniuous models, I will determine optimal schedules of treatment to minimize the probability of disease outbreak.
time15 RUB_sol MFA_sol NFA_sol NFY_sol SFY_baro_air NFA_baro_air
4/30/2012 0:15 11.929 12.689 11.26 8.19 9.43 13.134
4/30/2012 0:30 11.879 12.627 11.28 8.18 9.25 12.925
4/30/2012 0:45 11.828 12.57 11.26 8.21 9.03 12.736
4/30/2012 1:00 11.779 12.511 11.23 8.22 8.82 12.605
4/30/2012 1:15 11.73 12.459 11.17 8.23 8.6 12.455
4/30/2012 1:30 11.682 12.397 11.15 8.24 8.42 12.335
4/30/2012 1:45 11.635 12.353 11.13 8.27 8.22 12.206
4/30/2012 2:00 11.589 12.296 11.08 8.28 8.06 12.083
4/30/2012 2:15 11.545 12.25 11.03 8.29 7.89 11.95
4/30/2012 0:15 11.929 12.689 11.26 8.19 9.43 13.134
4/30/2012 0:30 11.879 12.627 11.28 8.18 9.25 12.925
4/30/2012 0:45 11.828 12.57 11.26 8.21 9.03 12.736
4/30/2012 1:00 11.779 12.511 11.23 8.22 8.82 12.605
4/30/2012 1:15 11.73 12.459 11.17 8.23 8.6 12.455
4/30/2012 1:30 11.682 12.397 11.15 8.24 8.42 12.335
4/30/2012 1:45 11.635 12.353 11.13 8.27 8.22 12.206
4/30/2012 2:00 11.589 12.296 11.08 8.28 8.06 12.083
4/30/2012 2:15 11.545 12.25 11.03 8.29 7.89 11.95
@noamross
noamross / Air_Water_loggers_15min_example.csv
Created February 6, 2013 23:53
A tutorial on using xts and ggplot for time series data
4/30/2012 0:15 11.929 12.689 11.26 8.19 9.43 13.134
4/30/2012 0:30 11.879 12.627 11.28 8.18 9.25 12.925
4/30/2012 0:45 11.828 12.57 11.26 8.21 9.03 12.736
4/30/2012 1:00 11.779 12.511 11.23 8.22 8.82 12.605
4/30/2012 1:15 11.73 12.459 11.17 8.23 8.6 12.455
4/30/2012 1:30 11.682 12.397 11.15 8.24 8.42 12.335
4/30/2012 1:45 11.635 12.353 11.13 8.27 8.22 12.206
4/30/2012 2:00 11.589 12.296 11.08 8.28 8.06 12.083
4/30/2012 2:15 11.545 12.25 11.03 8.29 7.89 11.95
@noamross
noamross / fishlakes.csv
Created February 21, 2013 01:23
Model Selection and Multi-Model Inference
We can make this file beautiful and searchable if this error is corrected: It looks like row 3 should actually have 41 columns, instead of 39. in line 2.
"","X","Lake.ID","Name","treatment","shorline","sizehectars","elevation","UTM.zone","northing","easting","BUBO.breeding","BUBO.breeding2","PSRE.breeding.Y.N","RACA.y.n","veg.extent","X..of.transects.with.veg","bank.slope","silt.0.1.","silt.total","X10cm.area","vegetated.area","RACA.1K","lakes.1k","dist.ave","ls","raca.basin","fish.basin","lakes.basin","co.lakes.basin","ls2","woody","herbacious","unveg","total","woodyprop","herbprop","unvegprop","logveg","archerb","arcsilt"
"1",1,531,"Mill Creek",0,377.96,0.713,2008.9368,10,518882,4565085,"y",1,"n","y",0.691,0.682,0.308095216,0.6818,0.71212,111.5,261.17,3,4,0.5538,0.75,3,0,3,0,1,6082255.229,54367.17182,1399749.194,7536371.595,0.807053521,0.007213972,0.185732508,2.41692328905509,0.0850375574910141,1.00445938904933
"2",2,533,"East Boulder",1,1356.4,11.4,2034.8448,10,518069,4564441,"y",1,"y","y",2.28,0.292,0.092475736,0.376,0.3472,980.56,3092.59,5,10,0.8268,0.5,4,4,8,2,0.5,5699686.549,1302747.189,1470012.242,8472445.979,0.67273212,0.15376282,0.173505059,3.4903223