https://github.com/cran/fields
Tip revision: 759a1e4ef5534b5c79692d4007086edf880a95ac authored by Doug Nychka on 18 April 2013, 22:51:12 UTC
version 6.7.5
version 6.7.5
Tip revision: 759a1e4
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
}