# [R] How can I estimate a Box-Cox function with R?

Ikerne del Valle ikerne.delvalle at ehu.es
Fri May 22 20:09:33 CEST 2009

```
Thanks Gregory. I see that that with
boxcox.lm() the optimal lambda is obtained and
plotted against log-likelihood.

library(MASS)
boxcox(Volume ~ log(Height) + log(Girth), data = trees,
lambda = seq(-0.25, 0.25, length = 10))

But has how can I see the fit of the same linear
model with the optimal BoxCox transformation, the
standard error for lambda etc.?

Best. Ikerne.

>Have you looked at the boxcox function in the
>MASS package?  That may do what you want.
>
>--
>Gregory (Greg) L. Snow Ph.D.
>Statistical Data Center
>Intermountain Healthcare
>greg.snow at imail.org
>801.408.8111
>
>
>>  -----Original Message-----
>>  From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>>  project.org] On Behalf Of Ikerne del Valle
>>  Sent: Thursday, May 21, 2009 4:29 AM
>>  To: fernando.tusell at ehu.es
>  > Cc: r-help at r-project.org
>>  Subject: [R] How can I estimate a Box-Cox function with R?
>>
>>
>>	Dear Fernando and all:
>>
>>	Thanks for your help. Now works. This is
>>  a training example to learn how to estimate a
>>  Box-Cox (right and/or left side transformations)
>>  with R (as LIMDEP does) in order to compare these
>>  estimations with the ones derived by applying
>>  NLS, ones the dependent variable has been divided
>>  by its geometric mean (see below) as suggested by
>>  (Zarembka (1974) and Spitzer (1984). However the
>>  example of the demand of money seems not to work.
>>  Any idea to face the error messages or how to
>>  estimate a Box-Cox function with R?
>>
>>	Best regards,
>>	Ikerne
>>
>>  library(nlrwr)
>>  r<-
>>  c(4.50,4.19,5.16,5.87,5.95,4.88,4.50,6.44,7.83,6.25,5.50,5.46,7.46,10.2
>>  8,11.77,13.42,11.02,8.50,8.80,7.69)
>>  Lr<-log(r)
>>  M<-
>>  c(480.00,524.30,566.30,589.50,628.20,712.80,805.20,861.00,908.40,1023.1
>>  0,1163.60,1286.60,1388.90,1497.90,1631.40,1794.40,1954.90,2188.80,2371.
>>  70,2563.60)
>>  LM<-log(M)
>>  Y<-
>>  c(2208.30,2271.40,2365.60,2423.30,2416.20,2484.80,2608.50,2744.10,2729.
>>  30,2695.00,2826.70,2958.60,3115.20,3192.40,3187.10,3248.80,3166.00,3277
>>  .70,3492.00,3573.50)
>>  LY<-log(Y)
>>  gmM<-exp((1/20)*sum(LM))
>>  GM<-M/gmM
>>  Gr<-r/gmM
>>  GY<-Y/gmM
>>  money<-data.frame(r,M,Y,Lr,LM,LY,GM,Gr,GY)
>>  attach(money)
>>  ols1<-lm(GM~r+Y)
>>  output1<-summary(ols1)
>>  coef1<-ols1\$coefficients
>>  a1<-coef1[]
>>  b11<-coef1[]
>>  b21<-coef1[]
>>  ols2<-lm(GM~Gr+GY)
>>  output2<-summary(ols2)
>>  coef2<-ols2\$coefficients
>>  a2<-coef2[]
>>  b12<-coef2[]
>>  b22<-coef2[]
>>  money.m1<-
>>  nls(GM~a+b*r^g+c*Y^g,data=money,start=list(a=a1,b=b11,g=1,c=b21))
>>  money.m2<-
>>  nls(GM~a+b*Gr^g+c*GY^g,data=money,start=list(a=a2,b=b12,g=1,c=b22))
>>
>>
>>		Ikerne del Valle Erkiaga
>>		Department of Applied Economics V
>>		Faculty of Economic and Business Sciences
>>		University of the Basque Country
>>		Avda. Lehendakari Agirre, Nº 83
>>		48015 Bilbao (Bizkaia) Spain
>>
>>  ______________________________________________
>>  R-help at r-project.org mailing list
>>  https://stat.ethz.ch/mailman/listinfo/r-help