https://github.com/cran/fields
Tip revision: 04279c16ce718b82025aae97f6fcd30ba3a8b1c5 authored by Doug Nychka on 05 September 2011, 20:18:33 UTC
version 6.6
version 6.6
Tip revision: 04279c1
transformx.R
# fields, Tools for spatial data
# Copyright 2004-2011, Institute for Mathematics Applied Geosciences
# University Corporation for Atmospheric Research
# Licensed under the GPL -- www.gpl.org/licenses/gpl.html
"transformx" <- function(x, scale.type = "unit.sd",
x.center, x.scale) {
if (scale.type == "unscaled") {
x.center <- rep(0, ncol(x))
x.scale <- rep(1, ncol(x))
}
else if (scale.type == "unit.sd") {
x.center <- apply(x, 2, mean)
x.scale <- sqrt(apply(x, 2, var))
x <- scale(x)
}
else if (scale.type == "range") {
x.center <- apply(x, 2, min)
x.scale <- apply(x, 2, max) - apply(x, 2, min)
x <- scale(x, center = x.center, scale = x.scale)
}
else if (scale.type == "user") {
if (missing(x.center))
x.center <- apply(x, 2, mean)
if (missing(x.scale) || length(x.scale) != ncol(x))
stop("Error: x.scale must be a vector of length d")
x <- scale(x, center = x.center, scale = x.scale)
}
else stop(paste("Error: scale.type must be one of", "unit.sd, range, user, unscaled"))
attr(x, "x.center") <- x.center
attr(x, "x.scale") <- x.scale
attr(x, "x.scale.type") <- scale.type
x
}