Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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)