Skip to content

Instantly share code, notes, and snippets.

@menugget
menugget / gist:7153698
Created October 25, 2013 12:12
a test bit of code
mu <- 5
sigma <- 2
vals <- seq(-2,12,,100)
ds <- dnorm(vals, mean=mu, sd=sigma)
plot(vals, ds, t="l")
qs <- qnorm(c(0.05, 0.95), mean=mu, sd=sigma)
abline(v=qs, col=2, lty=2)
@menugget
menugget / gist:7153738
Last active December 26, 2015 12:49
pca prediction
###pca - calculated for the first 4 columns of the data set that correspond to biometric measurements ("Sepal.Length" "Sepal.Width" "Petal.Length" "Petal.Width")
data(iris)
dat <- as.matrix(iris[,-5])
pca <- prcomp(dat, retx=TRUE, center=TRUE, scale=TRUE)
###Create new data sets for each of the three species.
#Biometric values are based on the distributions of the original data means
#and the covariances between these parameters.
setosa.mean <- apply(iris[iris$Species=="setosa",-5], 2, mean)
setosa.cov <- cov(iris[iris$Species=="setosa",-5])
@menugget
menugget / new.range.r
Last active July 10, 2016 18:03
[R function] "new.range" - Scale data to a new range. Arguments include the data, the new minimum, and the new maximum values.
new.range <- function(x, new.min=0, new.max=1){
ranx <- range(x, na.rm=TRUE)
px <- (x-ranx[1])/(ranx[2]-ranx[1])
res <- new.min+((new.max-new.min)*px)
res
}
@menugget
menugget / bio.env.r
Created November 28, 2013 08:38
BIOENV (Clarke and Ainsworth 1993)
bio.env <- function(fix.mat, var.mat,
fix.dist.method="bray", var.dist.method="euclidean",
scale.fix=FALSE, scale.var=TRUE,
output.best=10,
var.max=ncol(var.mat)
){
if(dim(fix.mat)[1] != dim(var.mat)[1]){stop("fixed and variable matrices must have the same number of rows")}
if(var.max > dim(var.mat)[2]){stop("var.max cannot be larger than the number of variables (columns) in var.mat")}
require(vegan)
@menugget
menugget / bv.step.r
Created November 28, 2013 08:46
BVSTEP (Clarke and Ainsworth 1992). Requires package "vegan".
bv.step <- function(fix.mat, var.mat,
fix.dist.method="bray", var.dist.method="euclidean",
scale.fix=FALSE, scale.var=TRUE,
max.rho=0.95,
min.delta.rho=0.001,
random.selection=TRUE,
prop.selected.var=0.2,
num.restarts=10,
var.always.include=NULL,
var.exclude=NULL,
@menugget
menugget / image.scale.2.r
Created November 28, 2013 09:09
Add color scale to corresponding image plot
#This function creates a color scale for use with the image()
#function. Input parameters should be consistent with those
#used in the corresponding image plot. The "axis.pos" argument
#defines the side of the axis. The "add.axis" argument defines
#whether the axis is added (default: TRUE)or not (FALSE).
image.scale <- function(z, zlim, col = heat.colors(12),
breaks, axis.pos=1, add.axis=TRUE, ...){
if(!missing(breaks)){
if(length(breaks) != (length(col)+1)){stop("must have one more break than colour")}
}
@menugget
menugget / val2col.3.0.r
Created November 28, 2013 09:12
Convert values to color levels
#this function converts a vector of values("z") to a vector of color
#levels. One must define the number of colors. The limits of the color
#scale("zlim") or the break points for the color changes("breaks") can
#also be defined. when breaks and zlim are defined, breaks overrides zlim.
val2col<-function(z, zlim, col = heat.colors(12), breaks){
if(!missing(breaks)){
if(length(breaks) != (length(col)+1)){stop("must have one more break than colour")}
}
if(missing(breaks) & !missing(zlim)){
zlim[2] <- zlim[2]+c(zlim[2]-zlim[1])*(1E-3)#adds a bit to the range in both directions
@menugget
menugget / prcomp.recon.r
Created November 28, 2013 09:15
Reconstruct a data set using PCA results from function "prcomp". A subset of PCs can be defined for truncated reconstruction.
#This function reconstructs a data set using a defined set of principal components.
#arguments "pca" is the pca object from prcomp, "pcs" is a vector of principal components
#to be used for reconstruction (default includes all pcs)
prcomp.recon <- function(pca, pcs=NULL){
if(is.null(pcs)) pcs <- seq(pca$sdev)
recon <- as.matrix(pca$x[,pcs]) %*% t(as.matrix(pca$rotation[,pcs]))
if(pca$scale[1] != FALSE){
recon <- scale(recon , center=FALSE, scale=1/pca$scale)
}
if(pca$center[1] != FALSE){
@menugget
menugget / image.scale_ex.R
Created December 5, 2013 11:26
An example of the use of the image.scale function
library(devtools)
source_url('https://gist.github.com/menugget/7689145/raw/dac746aa322ca4160a5fe66c70fec16ebe26faf9/image.scale.2.r')
png("volcano_w_scale.png", width=7, height=4, units="in", res=200)
layout(matrix(c(1,2,3,0,4,0), nrow=2, ncol=3), widths=c(4,4,1), heights=c(4,1))
layout.show(4)
#1st image
breaks <- seq(min(volcano), max(volcano),length.out=100)
par(mar=c(1,1,1,1))
@menugget
menugget / plot.stream.4.R
Last active February 25, 2019 18:46
Stream plot.
#plot.stream makes a "stream plot" where each y series is plotted
#as stacked filled polygons on alternating sides of a baseline.
#
#Arguments include:
#'x' - a vector of values
#'y' - a matrix of data series (columns) corresponding to x
#'order.method' = c("as.is", "max", "first")
# "as.is" - plot in order of y column
# "max" - plot in order of when each y series reaches maximum value
# "first" - plot in order of when each y series first value > 0