backtransform `scale()` for plotting

前端 未结 8 2254
别那么骄傲
别那么骄傲 2020-11-28 07:05

I have a explanatory variable that is centered using scale() that is used to predict a response variable:

d <- data.frame(
  x=runif(100),
           


        
8条回答
  •  执念已碎
    2020-11-28 07:20

    I felt like this should be a proper function, here was my attempt at it:

    #' Reverse a scale
    #'
    #' Computes x = sz+c, which is the inverse of z = (x - c)/s 
    #' provided by the \code{scale} function.
    #' 
    #' @param z a numeric matrix(like) object
    #' @param center either NULL or a numeric vector of length equal to the number of columns of z  
    #' @param scale  either NULL or a a numeric vector of length equal to the number of columns of z
    #'
    #' @seealso \code{\link{scale}}
    #'  mtcs <- scale(mtcars)
    #'  
    #'  all.equal(
    #'    unscale(mtcs), 
    #'    as.matrix(mtcars), 
    #'    check.attributes=FALSE
    #'  )
    #'  
    #' @export
    unscale <- function(z, center = attr(z, "scaled:center"), scale = attr(z, "scaled:scale")) {
      if(!is.null(scale))  z <- sweep(z, 2, scale, `*`)
      if(!is.null(center)) z <- sweep(z, 2, center, `+`)
      structure(z,
        "scaled:center"   = NULL,
        "scaled:scale"    = NULL,
        "unscaled:center" = center,
        "unscaled:scale"  = scale
      )
    }
    

提交回复
热议问题