title: How to unscale parameters from lm() when predictors are scale(x)^2
body: <p>I have a model where I scaled a variable, x, then used the scaled x and the square of scaled x as predictors. I.e., <code>lm(y ~ I(scale(x)) + I(scale(x)^2)</code>. I want to get the coefficients to be applicable to the original x units, but I'm having a hard time figuring out how to do this. I think the trouble is because of squaring after scaling, but I don't know.</p>
  
  <p><a href="https://stackoverflow.com/a/23643740/2343633">This answer</a> got me quite close, I think. Again, the difference might be the squaring after scaling.</p>
  
  <p>Below is an R script that shows my process in creating fake data and trying to unscale. Sorry, I guess it's a little long.</p>
  
  <p>What's peculiar is that at the end, I get the rescaling right for the squared terms, but not the linear terms or the intercept. Given that I used the method from the aforementioned answer, I'd have guessed I would get the squared coefficients wrong!</p>
  
  <p>How do I 'unscale' the parameters?</p>
  
  <pre><code>set.seed(1)
  # ---- Create Covariates ----
  # Define dimensions for simulation
  n &lt;- 100
  # Covariate Simulation Parameters
  mean.temp &lt;- 10
  sd.temp &lt;- 5
  mean.depth &lt;- 200
  sd.depth &lt;- 20
  # Simulate Covariates
  temperature &lt;- rnorm(n=n, mean=mean.temp, sd=sd.temp)
  depth &lt;- rnorm(n=n, mean=mean.depth, sd=sd.depth)
  # Create Unscaled Data Matrix
  dmat &lt;- data.frame( 
      temp=temperature, temp2=temperature^2, 
      depth=depth, depth2=depth^2
  )
  # Scale Covariates
  temp.scale &lt;- scale(temperature)
  depth.scale &lt;- scale(depth)
  # Create Scaled Data Matrix
  dmat.scale &lt;- data.frame(
      temp=temp.scale, temp2=temp.scale^2, 
      depth=depth.scale, depth2=depth.scale^2
  )
  # Record Scaling Factors
  mu.vec &lt;- c(
      "temp.mu"=attr(temp.scale,'scaled:center'),
      "temp2.mu"= attr(temp.scale,'scaled:center')^2, # is this right?
      "depth.mu"=attr(depth.scale,'scaled:center'),
      "depth2.mu"= attr(depth.scale,'scaled:center')^2 # is this right?
  )
  sd.vec &lt;- c(
      "temp.sd"=attr(temp.scale,'scaled:scale'),
      "temp2.sd"= attr(temp.scale,'scaled:scale')^2,
      "depth.sd"=attr(depth.scale,'scaled:scale'),
      "depth2.sd"= attr(depth.scale,'scaled:scale')^2
  )
  # ---- Create Parameters ----
  beta &lt;- matrix(c(0.5, 0.1, -0.8, 1.2, -0.1),ncol=1)
  # ---- Simulate ----
  eps &lt;- rnorm(n=n, mean=0, sd=0.001)
  y &lt;- (cbind(1,as.matrix(dmat))%*%beta + eps)[,1]
  # ---- Fit Models ----
  mod &lt;- lm(y~as.matrix(dmat))
  mod.scale &lt;- lm(y~as.matrix(dmat.scale))
  
  beta.orig &lt;- coef(mod)
  beta.scale &lt;- coef(mod.scale)
  # ---- Rescale Function ----
  # From: https://stackoverflow.com/a/23643740/2343633
  rescale.coefs &lt;- function(beta,mu,sigma) {
      beta2 &lt;- beta ## inherit names etc.
      beta2[-1] &lt;- sigma[1]*beta[-1]/sigma[-1]
      beta2[1]  &lt;- sigma[1]*beta[1]+mu[1]-sum(beta2[-1]*mu[-1])
      beta2
  }
  beta.rescale &lt;- rescale.coefs(beta.scale, mu=c(0,mu.vec), sigma=c(1,sd.vec))
  
  # ---- Compare ----
  beta.orig
  beta.rescale
  </code></pre>