backtransform `scale()` for plotting

前端 未结 8 2290
别那么骄傲
别那么骄傲 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条回答
  •  猫巷女王i
    2020-11-28 07:23

    Just inspired by Fermando´s answer, but unscaling line with less code:

    set.seed(1)
    x = matrix(sample(1:12), ncol= 3)
    xs = scale(x, center = TRUE, scale = TRUE)
    center <- attr(xs,"scaled:center")
    scale <- attr(xs,"scaled:scale")
    x.orig <- t(t(xs) * scale + center) # code is less here
    
    print(x)
    [1,]    9    2    6
    [2,]    4    5   11
    [3,]    7    3   12
    [4,]    1    8   10
    
    print(x.orig)
    [1,]    9    2    6
    [2,]    4    5   11
    [3,]    7    3   12
    [4,]    1    8   10
    attr(,"scaled:center")
    [1] 5.25 4.50 9.75
    attr(,"scaled:scale")
    [1] 3.50 2.65 2.63
    

提交回复
热议问题