https://github.com/cran/fields
Raw File
Tip revision: 6c8b30169bba182a68765ee3cb9b4e2ef7d38332 authored by Doug Nychka on 16 November 2011, 00:00:00 UTC
version 6.6.3
Tip revision: 6c8b301
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
}
back to top