Gorjanc Gregor
2005-Sep-11 20:25 UTC
[R] Backtransforming regression coefficient for scaled covariate
Hello! Scaling i.e. (x - mean(x)) / sd(x) of covariates in the model can improve the efficiency of estimation. That is nice, but sometimes one needs to report estimates for original scale. I was able to backtransform estimates of linear regression quite easily but I stumped on higher polynomials. Is there a general rule that I am not aware of or is my algebra so bad? I appologize for not pure R question but I hope others will also benefit. I attached the R code for example bellow. ## --- Generate data for linear regression --- e <- rnorm(n = 100, sd = 10) x <- rnorm(n = 100, mean = 100, sd = 10) b <- 3 mu <- 2 y <- mu + b * x + e plot(y = y, x = x) ## Fit linear regression (lm1 <- lm(y ~ x)) ## Fit linear regression with transformed i.e. standardized covariate (lm2 <- lm(y ~ scale(x))) ## Backtransform estimate of regression coefficient coef(lm2)[2] / sd(x) ## --- Generate data for quadratic regression --- e <- rnorm(n = 100, sd = 10) x <- runif(n = 100, min = 1, max = 100) b1 <- 2 b2 <- -0.01 mu <- 2 y <- mu + b1 * x + b2 * x^2 + e plot(y = y, x = x) ## Fit regression (lm1 <- lm(y ~ x + I(x^2))) ## Fit regression with transformed i.e. standardized covariate (lm2 <- lm(y ~ scale(x) + I(scale(x)^2))) ## Backtransform estimates of regression coefficients ## ?? Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana Biotechnical Faculty URI: http://www.bfro.uni-lj.si/MR/ggorjan Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si Groblje 3 tel: +386 (0)1 72 17 861 SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia, Europe ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C.
Andres Legarra
2005-Sep-12 06:53 UTC
[R] Backtransforming regression coefficient for scaled covariate
[R] Backtransforming regression coefficient for scaled covariate Your covariate in the second part of the polynomial is x^2 and not x. Therefore the transformation should be applied to x^2. Like this: (lm2 <- lm(y ~ scale(x) + I(scale(x^2)) ) then you would use coef(lm2)[3]/sd(x^2) Andres -- Andres Legarra NEIKER Apdo. 46 Vitoria-Gasteiz 01080 Spain -- ----- Original Message ----- From: Gorjanc Gregor To: r-help at stat.math.ethz.ch Sent: Sunday, September 11, 2005 10:25 PM Subject: [R] Backtransforming regression coefficient for scaled covariate Hello! Scaling i.e. (x - mean(x)) / sd(x) of covariates in the model can improve the efficiency of estimation. That is nice, but sometimes one needs to report estimates for original scale. I was able to backtransform estimates of linear regression quite easily but I stumped on higher polynomials. Is there a general rule that I am not aware of or is my algebra so bad? I appologize for not pure R question but I hope others will also benefit. I attached the R code for example bellow. ## --- Generate data for linear regression --- e <- rnorm(n = 100, sd = 10) x <- rnorm(n = 100, mean = 100, sd = 10) b <- 3 mu <- 2 y <- mu + b * x + e plot(y = y, x = x) ## Fit linear regression (lm1 <- lm(y ~ x)) ## Fit linear regression with transformed i.e. standardized covariate (lm2 <- lm(y ~ scale(x))) ## Backtransform estimate of regression coefficient coef(lm2)[2] / sd(x) ## --- Generate data for quadratic regression --- e <- rnorm(n = 100, sd = 10) x <- runif(n = 100, min = 1, max = 100) b1 <- 2 b2 <- -0.01 mu <- 2 y <- mu + b1 * x + b2 * x^2 + e plot(y = y, x = x) ## Fit regression (lm1 <- lm(y ~ x + I(x^2))) ## Fit regression with transformed i.e. standardized covariate (lm2 <- lm(y ~ scale(x) + I(scale(x)^2))) ## Backtransform estimates of regression coefficients ## ?? Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana Biotechnical Faculty URI: http://www.bfro.uni-lj.si/MR/ggorjan Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si Groblje 3 tel: +386 (0)1 72 17 861 SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia, Europe ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Gorjanc Gregor
2005-Sep-12 08:12 UTC
[R] Backtransforming regression coefficient for scaled covariate
Andres, this seems not to be the case. Look bellow the coefficients. They are not the same as in unscaled regression. R> (lm1 <- lm(y ~ x + I(x^2))) Call: lm(formula = y ~ x + I(x^2)) Coefficients: (Intercept) x I(x^2) 4.62069 1.78811 -0.00751 R> ## Fit regression with transformed i.e. standardized covariate R> (lm2 <- lm(y ~ scale(x) + I(scale(x)^2))) Call: lm(formula = y ~ scale(x) + I(scale(x)^2)) Coefficients: (Intercept) scale(x) I(scale(x)^2) 75.12 29.86 -6.21 R> coef(lm2)[3]/sd(x^2) I(scale(x)^2) -0.0020519 R> coef(lm2)[2]/sd(x) scale(x) 1.0384 -----Original Message----- From: Andres Legarra [mailto:alegarra at neiker.net] Sent: Mon 2005-09-12 08:53 To: Gorjanc Gregor; r-help at stat.math.ethz.ch Subject: Re: [R] Backtransforming regression coefficient for scaled covariate [R] Backtransforming regression coefficient for scaled covariate Your covariate in the second part of the polynomial is x^2 and not x. Therefore the transformation should be applied to x^2. Like this: (lm2 <- lm(y ~ scale(x) + I(scale(x^2)) ) then you would use coef(lm2)[3]/sd(x^2) Andres -- Andres Legarra NEIKER Apdo. 46 Vitoria-Gasteiz 01080 Spain -- ----- Original Message ----- From: Gorjanc Gregor To: r-help at stat.math.ethz.ch Sent: Sunday, September 11, 2005 10:25 PM Subject: [R] Backtransforming regression coefficient for scaled covariate Hello! Scaling i.e. (x - mean(x)) / sd(x) of covariates in the model can improve the efficiency of estimation. That is nice, but sometimes one needs to report estimates for original scale. I was able to backtransform estimates of linear regression quite easily but I stumped on higher polynomials. Is there a general rule that I am not aware of or is my algebra so bad? I appologize for not pure R question but I hope others will also benefit. I attached the R code for example bellow. ## --- Generate data for linear regression --- e <- rnorm(n = 100, sd = 10) x <- rnorm(n = 100, mean = 100, sd = 10) b <- 3 mu <- 2 y <- mu + b * x + e plot(y = y, x = x) ## Fit linear regression (lm1 <- lm(y ~ x)) ## Fit linear regression with transformed i.e. standardized covariate (lm2 <- lm(y ~ scale(x))) ## Backtransform estimate of regression coefficient coef(lm2)[2] / sd(x) ## --- Generate data for quadratic regression --- e <- rnorm(n = 100, sd = 10) x <- runif(n = 100, min = 1, max = 100) b1 <- 2 b2 <- -0.01 mu <- 2 y <- mu + b1 * x + b2 * x^2 + e plot(y = y, x = x) ## Fit regression (lm1 <- lm(y ~ x + I(x^2))) ## Fit regression with transformed i.e. standardized covariate (lm2 <- lm(y ~ scale(x) + I(scale(x)^2))) ## Backtransform estimates of regression coefficients ## ?? Lep pozdrav / With regards, Gregor Gorjanc ---------------------------------------------------------------------- University of Ljubljana Biotechnical Faculty URI: http://www.bfro.uni-lj.si/MR/ggorjan Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si Groblje 3 tel: +386 (0)1 72 17 861 SI-1230 Domzale fax: +386 (0)1 72 17 888 Slovenia, Europe ---------------------------------------------------------------------- "One must learn by doing the thing; for though you think you know it, you have no certainty until you try." Sophocles ~ 450 B.C. ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Christoph Buser
2005-Sep-12 08:43 UTC
[R] Backtransforming regression coefficient for scaled covariate
Dear Gregor The solution of Andres was correct, but by reproducing his example you did a copy paste error. In the model lm2 you should scale your variable after the polynomial transformation I(scale(x^2)) and not I(scale(x)^2) Then the backtransformation in your example should work. Regards, Christoph Buser -------------------------------------------------------------- Christoph Buser <buser at stat.math.ethz.ch> Seminar fuer Statistik, LEO C13 ETH (Federal Inst. Technology) 8092 Zurich SWITZERLAND phone: x-41-44-632-4673 fax: 632-1228 http://stat.ethz.ch/~buser/ -------------------------------------------------------------- Gorjanc Gregor writes: > Andres, this seems not to be the case. Look bellow > the coefficients. They are not the same as in unscaled > regression. > > R> (lm1 <- lm(y ~ x + I(x^2))) > > Call: > lm(formula = y ~ x + I(x^2)) > > Coefficients: > (Intercept) x I(x^2) > 4.62069 1.78811 -0.00751 > > R> ## Fit regression with transformed i.e. standardized covariate Wrong line!!!! > R> (lm2 <- lm(y ~ scale(x) + I(scale(x)^2))) Correct one > R> (lm2 <- lm(y ~ scale(x) + I(scale(x^2)))) > > Call: > lm(formula = y ~ scale(x) + I(scale(x)^2)) > > Coefficients: > (Intercept) scale(x) I(scale(x)^2) > 75.12 29.86 -6.21 > > R> coef(lm2)[3]/sd(x^2) > I(scale(x)^2) > -0.0020519 > > R> coef(lm2)[2]/sd(x) > scale(x) > 1.0384 > > -----Original Message----- > From: Andres Legarra [mailto:alegarra at neiker.net] > Sent: Mon 2005-09-12 08:53 > To: Gorjanc Gregor; r-help at stat.math.ethz.ch > Subject: Re: [R] Backtransforming regression coefficient for scaled covariate > > [R] Backtransforming regression coefficient for scaled covariate > > Your > covariate in the second part of the polynomial is x^2 and not x. Therefore > the transformation should be applied to x^2. > Like this: > (lm2 <- lm(y ~ scale(x) + I(scale(x^2)) ) > then you would use > coef(lm2)[3]/sd(x^2) > > Andres > -- > Andres Legarra > NEIKER > Apdo. 46 > Vitoria-Gasteiz 01080 Spain > -- > > > > ----- Original Message ----- > From: Gorjanc Gregor > To: r-help at stat.math.ethz.ch > Sent: Sunday, September 11, 2005 10:25 PM > Subject: [R] Backtransforming regression coefficient for scaled covariate > > > Hello! > Scaling i.e. (x - mean(x)) / sd(x) of covariates in the model > can improve the efficiency of estimation. That is nice, but > sometimes one needs to report estimates for original scale. I > was able to backtransform estimates of linear regression quite > easily but I stumped on higher polynomials. Is there a general > rule that I am not aware of or is my algebra so bad? > I appologize for not pure R question but I hope others will also > benefit. I attached the R code for example bellow. > ## --- Generate data for linear regression --- > e <- rnorm(n = 100, sd = 10) > x <- rnorm(n = 100, mean = 100, sd = 10) > b <- 3 > mu <- 2 > y <- mu + b * x + e > plot(y = y, x = x) > ## Fit linear regression > (lm1 <- lm(y ~ x)) > ## Fit linear regression with transformed i.e. standardized covariate > (lm2 <- lm(y ~ scale(x))) > ## Backtransform estimate of regression coefficient > coef(lm2)[2] / sd(x) > ## --- Generate data for quadratic regression --- > e <- rnorm(n = 100, sd = 10) > x <- runif(n = 100, min = 1, max = 100) > b1 <- 2 > b2 <- -0.01 > mu <- 2 > y <- mu + b1 * x + b2 * x^2 + e > plot(y = y, x = x) > ## Fit regression > (lm1 <- lm(y ~ x + I(x^2))) > ## Fit regression with transformed i.e. standardized covariate > (lm2 <- lm(y ~ scale(x) + I(scale(x)^2))) > ## Backtransform estimates of regression coefficients > ## ?? > Lep pozdrav / With regards, > Gregor Gorjanc > ---------------------------------------------------------------------- > University of Ljubljana > Biotechnical Faculty URI: http://www.bfro.uni-lj.si/MR/ggorjan > Zootechnical Department mail: gregor.gorjanc <at> bfro.uni-lj.si > Groblje 3 tel: +386 (0)1 72 17 861 > SI-1230 Domzale fax: +386 (0)1 72 17 888 > Slovenia, Europe > ---------------------------------------------------------------------- > "One must learn by doing the thing; for though you think you know it, > you have no certainty until you try." Sophocles ~ 450 B.C. > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html > > > !DSPAM:43253a39268411607118103!