Ikerne del Valle
2009-May-19 15:17 UTC
[R] nlrwr package. Error when fitting the optimal Box-Cox transformation with two variables
Dear all: I'm trying to fit the optimal Box-Cox transformation related to nls (see the code below) for the demand of money data in Green (3th Edition) but in the last step R gives the next error message. Error en `[.data.frame`(eval(object$data), , as.character(formula(object)[[2]])[2]) : undefined columns selected. ?Any idea to solve the problem? Thanks in advance, 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.28,11.77,13.42,11.02,8.50,8.80,7.69) M<-c(480.00,524.30,566.30,589.50,628.20,712.80,805.20,861.00,908.40,1023.10,1163.60,1286.60,1388.90,1497.90,1631.40,1794.40,1954.90,2188.80,2371.70,2563.60) 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) money<-data.frame(r,M,Y) attach(money) ols1<-lm(log(M)~log(r)+log(Y)) output1<-summary(ols1) coef1<-ols1$coefficients a1<-coef1[[1]] b11<-coef1[[2]] b21<-coef1[[3]] money.m1<-nls(log(M)~a+b*r^g+c*Y^g,data=money,start=list(a=a1,b=b11,g=1,c=b21)) summary(money.m1) money.m2<-boxcox(money.m1) Prof. 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
F.Tusell
2009-May-20 08:33 UTC
[R] nlrwr package. Error when fitting the optimal Box-Cox transformation with two variables
El mar, 19-05-2009 a las 17:17 +0200, Ikerne del Valle escribi?:> > Dear all: > > I'm trying to fit the optimal Box-Cox > transformation related to nls (see the code > below) for the demand of money data in Green (3th > Edition) but in the last step R gives the next > error message. > Error en > `[.data.frame`(eval(object$data), , > as.character(formula(object)[[2]])[2]) : > undefined columns selected. > ?Any idea to solve the problem? > Thanks in advance, > > > 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.28,11.77,13.42,11.02,8.50,8.80,7.69) > M<-c(480.00,524.30,566.30,589.50,628.20,712.80,805.20,861.00,908.40,1023.10,1163.60,1286.60,1388.90,1497.90,1631.40,1794.40,1954.90,2188.80,2371.70,2563.60) > 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) > money<-data.frame(r,M,Y) > attach(money) > ols1<-lm(log(M)~log(r)+log(Y)) > output1<-summary(ols1) > coef1<-ols1$coefficients > a1<-coef1[[1]] > b11<-coef1[[2]] > b21<-coef1[[3]] > money.m1<-nls(log(M)~a+b*r^g+c*Y^g,data=money,start=list(a=a1,b=b11,g=1,c=b21)) > summary(money.m1) > money.m2<-boxcox(money.m1) > > > > Prof. 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 >Ikerne, Inside boxcox.nls, the variable "log(M)" is searched in dataframe money and not found. Indeed, it is a little redundant to take logs and then search a Box-Cox transformation --which gives the log when lambda is zero. If you really want to do that, you might define a new variable "logM" and include that in your money dataframe, then rewrite the nls() call as: money.m1 <- nls(logM ~ a + b ... etc. Best, ft. -- Fernando TUSELL e-mail: Departamento de Econometr?a y Estad?stica fernando.tusell at ehu.es Facultad de CC.EE. y Empresariales Tel: (+34)94.601.3733 Universidad del Pa?s Vasco Secr: (+34)94.601.3740 Avenida Lendakari Aguirre, 83 Fax: (+34)94.601.3754 E-48015 BILBAO (Spain) http://www.et.bs.ehu.es/~etptupaf
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.28,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.10,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[[1]] b11<-coef1[[2]] b21<-coef1[[3]] ols2<-lm(GM~Gr+GY) output2<-summary(ols2) coef2<-ols2$coefficients a2<-coef2[[1]] b12<-coef2[[2]] b22<-coef2[[3]] 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
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[[1]] > b11<-coef1[[2]] > b21<-coef1[[3]] > ols2<-lm(GM~Gr+GY) > output2<-summary(ols2) > coef2<-ols2$coefficients > a2<-coef2[[1]] > b12<-coef2[[2]] > b22<-coef2[[3]] > 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 > PLEASE do read the posting guide http://www.R-project.org/posting- > guide.html > and provide commented, minimal, self-contained, reproducible code.
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[[1]] >> b11<-coef1[[2]] >> b21<-coef1[[3]] >> ols2<-lm(GM~Gr+GY) >> output2<-summary(ols2) >> coef2<-ols2$coefficients >> a2<-coef2[[1]] >> b12<-coef2[[2]] >> b22<-coef2[[3]] >> 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 >> PLEASE do read the posting guide http://www.R-project.org/posting- >> guide.html >> and provide commented, minimal, self-contained, reproducible code.