backtransform `scale()` for plotting

前端 未结 8 2284
别那么骄傲
别那么骄傲 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:22

    Old question, but why wouldn't you just do this:

    plot(d$x, predict(m1, d))
    

    As an easier way than manually using the attributes from the scaled object, DMwR has a function for this: unscale. It works like this:

    d <- data.frame(
      x=runif(100)
    )
    
    d$y <- 17 + d$x * 12
    
    s.x <- scale(d$x)
    
    m1 <- lm(d$y~s.x)
    
    library(DMwR)
    unsc.x <- unscale(d$x, s.x)
    plot(unsc.x, predict(m1, d))
    

    Importantly, the second argument of unscale needs to have something with the attributes of 'scaled:scale' and 'scaled:center'

提交回复
热议问题