Dear all Nobody responded to my previous post so far so I try with more offending subject. I just encountered a strange problem with nls formula. I tried to use nls in cycle but I was not successful. I traced the problem to some parse command. Here is an example DF<-data.frame(x=1:10, y=3*(1:10)^.5+rnorm(10)) coef(lm(log(DF[,2])~log(DF[,1]))) (Intercept) log(DF[, 1]) 0.7437320 0.6831726 # this works coef(nls(y~a*x^b, data=DF, start=list(a=3, b=.7))) a b 2.6412881 0.5545907 # OK, this works too coef(nls(DF[,2]~a*DF[,1]^b, data=DF, start=list(a=3, b=.7))) Error in parse(text = x) : unexpected end of input in "~ " coef(nls(DF[,2]~a*DF[,1]^b, start=list(a=3, b=.7))) Error in parse(text = x) : unexpected end of input in "~ " # but this does not Browse[1]> debug: mf$formula <- as.formula(paste("~", paste(varNames[varIndex], collapse = "+")), env = environment(formula)) Browse[1]> Error in parse(text = x) : unexpected end of input in "~ ">Actually the problem is that with calling nls with DF[,n]~... varNames and varIndex is not correctly specified. I am not sure if this behaviour is a bug or feature. If it is a feature, please help me how to call variables from data frame when using nls inside cycle for (i in ....) result[i,] <- coef(nls( ...., )) Thank you Petr> sessionInfo()R version 2.8.0 Under development (unstable) (2008-05-18 r45723) i386-pc-mingw32 locale: LC_COLLATE=Czech_Czech Republic.1250;LC_CTYPE=Czech_Czech Republic.1250;LC_MONETARY=Czech_Czech Republic.1250;LC_NUMERIC=C;LC_TIME=Czech_Czech Republic.1250 attached base packages: [1] stats grDevices datasets utils graphics methods base other attached packages: [1] nlme_3.1-88 lattice_0.17-7 fun_0.1 loaded via a namespace (and not attached): [1] grid_2.8.0 tools_2.8.0 Petr Pikal petr.pikal at precheza.cz 724008364, 581252140, 581252257

Dear Petr, I think it''s a feature. the formula interface also won''t let you specify the slots of S4 objects in the model spec. How about coef(nls(y~a*x^b, data=list(x=DF[,1], y=DF[,2]), start=list(a=3, b=.7))) ? On Thu, 26 Jun 2008, Petr PIKAL wrote:> Dear all > > Nobody responded to my previous post so far so I try with more offending > subject. > > I just encountered a strange problem with nls formula. I tried to use nls > in cycle but I was not successful. I traced the problem to some parse > command. > > Here is an example > > DF<-data.frame(x=1:10, y=3*(1:10)^.5+rnorm(10)) > > coef(lm(log(DF[,2])~log(DF[,1]))) > (Intercept) log(DF[, 1]) > 0.7437320 0.6831726 > # this works > > coef(nls(y~a*x^b, data=DF, start=list(a=3, b=.7))) > a b > 2.6412881 0.5545907 > # OK, this works too > > coef(nls(DF[,2]~a*DF[,1]^b, data=DF, start=list(a=3, b=.7))) > Error in parse(text = x) : unexpected end of input in "~ " > coef(nls(DF[,2]~a*DF[,1]^b, start=list(a=3, b=.7))) > Error in parse(text = x) : unexpected end of input in "~ " > # but this does not > > > Browse[1]> > debug: mf$formula <- as.formula(paste("~", paste(varNames[varIndex], > collapse = "+")), env = environment(formula)) > Browse[1]> > Error in parse(text = x) : unexpected end of input in "~ " > > > > Actually the problem is that with calling nls with DF[,n]~... varNames and > varIndex is not correctly specified. > > I am not sure if this behaviour is a bug or feature. If it is a feature, > please help me how to call variables from data frame > when using nls inside cycle > > for (i in ....) result[i,] <- coef(nls( ...., )) > > Thank you > Petr > > > sessionInfo() > R version 2.8.0 Under development (unstable) (2008-05-18 r45723) > i386-pc-mingw32 > > locale: > LC_COLLATE=Czech_Czech Republic.1250;LC_CTYPE=Czech_Czech > Republic.1250;LC_MONETARY=Czech_Czech > Republic.1250;LC_NUMERIC=C;LC_TIME=Czech_Czech Republic.1250 > > attached base packages: > [1] stats grDevices datasets utils graphics methods base > > other attached packages: > [1] nlme_3.1-88 lattice_0.17-7 fun_0.1 > > loaded via a namespace (and not attached): > [1] grid_2.8.0 tools_2.8.0 > > > Petr Pikal > petr.pikal at precheza.cz > 724008364, 581252140, 581252257 > > ______________________________________________ > 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. >

Try this and note, in particular, that the "model:" line in the output has the correct variables substituted:> nm <- names(DF) > eqn <- sprintf("%s ~ a * %s", nm[2], nm[1]) > nls(eqn, DF, start = c(a = 1))Nonlinear regression model model: y ~ a * x data: DF a 1.133 residual sum-of-squares: 31.51 Number of iterations to convergence: 1 Achieved convergence tolerance: 1.55e-10 On Thu, Jun 26, 2008 at 7:57 AM, Petr PIKAL <petr.pikal at precheza.cz> wrote:> Dear all > > Nobody responded to my previous post so far so I try with more offending > subject. > > I just encountered a strange problem with nls formula. I tried to use nls > in cycle but I was not successful. I traced the problem to some parse > command. > > Here is an example > > DF<-data.frame(x=1:10, y=3*(1:10)^.5+rnorm(10)) > > coef(lm(log(DF[,2])~log(DF[,1]))) > (Intercept) log(DF[, 1]) > 0.7437320 0.6831726 > # this works > > coef(nls(y~a*x^b, data=DF, start=list(a=3, b=.7))) > a b > 2.6412881 0.5545907 > # OK, this works too > > coef(nls(DF[,2]~a*DF[,1]^b, data=DF, start=list(a=3, b=.7))) > Error in parse(text = x) : unexpected end of input in "~ " > coef(nls(DF[,2]~a*DF[,1]^b, start=list(a=3, b=.7))) > Error in parse(text = x) : unexpected end of input in "~ " > # but this does not > > > Browse[1]> > debug: mf$formula <- as.formula(paste("~", paste(varNames[varIndex], > collapse = "+")), env = environment(formula)) > Browse[1]> > Error in parse(text = x) : unexpected end of input in "~ " >> > > Actually the problem is that with calling nls with DF[,n]~... varNames and > varIndex is not correctly specified. > > I am not sure if this behaviour is a bug or feature. If it is a feature, > please help me how to call variables from data frame > when using nls inside cycle > > for (i in ....) result[i,] <- coef(nls( ...., )) > > Thank you > Petr > >> sessionInfo() > R version 2.8.0 Under development (unstable) (2008-05-18 r45723) > i386-pc-mingw32 > > locale: > LC_COLLATE=Czech_Czech Republic.1250;LC_CTYPE=Czech_Czech > Republic.1250;LC_MONETARY=Czech_Czech > Republic.1250;LC_NUMERIC=C;LC_TIME=Czech_Czech Republic.1250 > > attached base packages: > [1] stats grDevices datasets utils graphics methods base > > other attached packages: > [1] nlme_3.1-88 lattice_0.17-7 fun_0.1 > > loaded via a namespace (and not attached): > [1] grid_2.8.0 tools_2.8.0 > > > Petr Pikal > petr.pikal at precheza.cz > 724008364, 581252140, 581252257 > > ______________________________________________ > 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. >

G''day Petr, On Thu, 26 Jun 2008 13:57:39 +0200 Petr PIKAL <petr.pikal at precheza.cz> wrote:> I just encountered a strange problem with nls formula. I tried to use > nls in cycle but I was not successful. I traced the problem to some > parse command. > > [...] > > > I am not sure if this behaviour is a bug or feature. [...]It is definitely a feature. It is an error to believe that all modelling functions that use modelling formulae use the same syntax for their modelling formulae. This is, perhaps, easiest realised by observing how lm() and nls() interpret "*" and "/" in model formulae. In nls(), "[..]" can be used to index parameters, if the parameter is allowed to change between groups in the data. This seems to be a little known feature, though there is an example that uses that feature in MASS. The contributed documentation "An Introduction to R: Software for Statistical Modelling & Computing" by Petra Kuhnert and Bill Venables, available from CRAN, also has such an example on pages 134 and 230. The fact that nls() allows you to use "[..]" to index parameters in the model formulae seems to conflict with the way you wanted to specify the observed values in the formula. I guess Gabor''s solution is a fix for your problem. Cheers, Berwin =========================== Full address ============================Berwin A Turlach Tel.: +65 6515 4416 (secr) Dept of Statistics and Applied Probability +65 6515 6650 (self) Faculty of Science FAX : +65 6872 3919 National University of Singapore 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg Singapore 117546 http://www.stat.nus.edu.sg/~statba

Thank you both. Katharine approach seemed to me easier to implement so I used it successfully. However I still wonder why in linear model using lm I can use for (i in ....) lm( data[,i]~data[,1]) but in nls the same construction fails. I understand that it is sometimes difficult to set correct starting values especially if the values can by quite different and then the cycle fails. However it works in lm and probably in other regression models (I tested it in ltsreg from MASS) Petr petr.pikal at precheza.cz 724008364, 581252140, 581252257 r-help-bounces at r-project.org napsal dne 26.06.2008 14:16:35:> Dear Petr, > > I think it''s a feature. the formula interface also won''t let youspecify> the slots of S4 objects in the model spec. > > How about > > coef(nls(y~a*x^b, data=list(x=DF[,1], y=DF[,2]), start=list(a=3, b=.7))) > > ? > > On Thu, 26 Jun 2008, Petr PIKAL wrote: > > > Dear all > > > > Nobody responded to my previous post so far so I try with moreoffending> > subject. > > > > I just encountered a strange problem with nls formula. I tried to usenls> > in cycle but I was not successful. I traced the problem to some parse > > command. > > > > Here is an example > > > > DF<-data.frame(x=1:10, y=3*(1:10)^.5+rnorm(10)) > > > > coef(lm(log(DF[,2])~log(DF[,1]))) > > (Intercept) log(DF[, 1]) > > 0.7437320 0.6831726 > > # this works > > > > coef(nls(y~a*x^b, data=DF, start=list(a=3, b=.7))) > > a b > > 2.6412881 0.5545907 > > # OK, this works too > > > > coef(nls(DF[,2]~a*DF[,1]^b, data=DF, start=list(a=3, b=.7))) > > Error in parse(text = x) : unexpected end of input in "~ " > > coef(nls(DF[,2]~a*DF[,1]^b, start=list(a=3, b=.7))) > > Error in parse(text = x) : unexpected end of input in "~ " > > # but this does not > > > > > > Browse[1]> > > debug: mf$formula <- as.formula(paste("~", paste(varNames[varIndex], > > collapse = "+")), env = environment(formula)) > > Browse[1]> > > Error in parse(text = x) : unexpected end of input in "~ " > > > > > > > Actually the problem is that with calling nls with DF[,n]~... varNamesand> > varIndex is not correctly specified. > > > > I am not sure if this behaviour is a bug or feature. If it is afeature,> > please help me how to call variables from data frame > > when using nls inside cycle > > > > for (i in ....) result[i,] <- coef(nls( ...., )) > > > > Thank you > > Petr > > > > > sessionInfo() > > R version 2.8.0 Under development (unstable) (2008-05-18 r45723) > > i386-pc-mingw32 > > > > locale: > > LC_COLLATE=Czech_Czech Republic.1250;LC_CTYPE=Czech_Czech > > Republic.1250;LC_MONETARY=Czech_Czech > > Republic.1250;LC_NUMERIC=C;LC_TIME=Czech_Czech Republic.1250 > > > > attached base packages: > > [1] stats grDevices datasets utils graphics methods base > > > > other attached packages: > > [1] nlme_3.1-88 lattice_0.17-7 fun_0.1 > > > > loaded via a namespace (and not attached): > > [1] grid_2.8.0 tools_2.8.0 > > > > > > Petr Pikal > > petr.pikal at precheza.cz > > 724008364, 581252140, 581252257 > > > > ______________________________________________ > > R-help at r-project.org mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> > and provide commented, minimal, self-contained, reproducible code. > > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.

Thank you Berwin. Ok, I take your point. I normally do nls modelling interactively but this time I was given a set of data so I tried to use an approach which I use quite often in lm or in plotting to pdf file. I obviously was not successful and there is nothing about it in documentation or at least I did not find it :-). I used Katharine''s solution and will look on Gabor''s too later. Regards Petr Berwin A Turlach <berwin at maths.uwa.edu.au> napsal dne 26.06.2008 20:59:20:> G''day Petr, > > On Thu, 26 Jun 2008 13:57:39 +0200 > Petr PIKAL <petr.pikal at precheza.cz> wrote: > > > I just encountered a strange problem with nls formula. I tried to use > > nls in cycle but I was not successful. I traced the problem to some > > parse command. > > > > [...] > > > > > > I am not sure if this behaviour is a bug or feature. [...] > > It is definitely a feature. > > It is an error to believe that all modelling functions that use > modelling formulae use the same syntax for their modelling formulae. > This is, perhaps, easiest realised by observing how lm() and nls() > interpret "*" and "/" in model formulae. > > In nls(), "[..]" can be used to index parameters, if the parameter is > allowed to change between groups in the data. This seems to be a > little known feature, though there is an example that uses that feature > in MASS. The contributed documentation "An Introduction to R: Software > for Statistical Modelling & Computing" by Petra Kuhnert and Bill > Venables, available from CRAN, also has such an example on pages 134 > and 230. > > The fact that nls() allows you to use "[..]" to index parameters in the > model formulae seems to conflict with the way you wanted to specify > the observed values in the formula. I guess Gabor''s solution is a fix > for your problem. > > Cheers, > > Berwin > > =========================== Full address ============================> Berwin A Turlach Tel.: +65 6515 4416 (secr) > Dept of Statistics and Applied Probability +65 6515 6650 (self) > Faculty of Science FAX : +65 6872 3919 > National University of Singapore > 6 Science Drive 2, Blk S16, Level 7 e-mail: statba at nus.edu.sg > Singapore 117546 http://www.stat.nus.edu.sg/~statba

As has already been pointed out, the syntax of formulae in nls() is not the same as in linear models. So things which are valid for linear models are not necessarily valid for others. Since nls() is sparsely documented on the help page, you need to look at the references (and you can also learn a lot of tricks from Bill Venables'' account in MASS). On Fri, 27 Jun 2008, Petr PIKAL wrote:> Thank you both. > > Katharine approach seemed to me easier to implement so I used it > successfully. However I still wonder why in linear model using lm I can > use > > for (i in ....) lm( data[,i]~data[,1]) > > but in nls the same construction fails. I understand that it is sometimes > difficult to set correct starting values especially if the values can by > quite different and then the cycle fails. > > However it works in lm and probably in other regression models (I tested > it in ltsreg from MASS) > > Petr > petr.pikal at precheza.cz > 724008364, 581252140, 581252257 > > > r-help-bounces at r-project.org napsal dne 26.06.2008 14:16:35: > >> Dear Petr, >> >> I think it''s a feature. the formula interface also won''t let you > specify >> the slots of S4 objects in the model spec. >> >> How about >> >> coef(nls(y~a*x^b, data=list(x=DF[,1], y=DF[,2]), start=list(a=3, b=.7))) >> >> ? >> >> On Thu, 26 Jun 2008, Petr PIKAL wrote: >> >>> Dear all >>> >>> Nobody responded to my previous post so far so I try with more > offending >>> subject. >>> >>> I just encountered a strange problem with nls formula. I tried to use > nls >>> in cycle but I was not successful. I traced the problem to some parse >>> command. >>> >>> Here is an example >>> >>> DF<-data.frame(x=1:10, y=3*(1:10)^.5+rnorm(10)) >>> >>> coef(lm(log(DF[,2])~log(DF[,1]))) >>> (Intercept) log(DF[, 1]) >>> 0.7437320 0.6831726 >>> # this works >>> >>> coef(nls(y~a*x^b, data=DF, start=list(a=3, b=.7))) >>> a b >>> 2.6412881 0.5545907 >>> # OK, this works too >>> >>> coef(nls(DF[,2]~a*DF[,1]^b, data=DF, start=list(a=3, b=.7))) >>> Error in parse(text = x) : unexpected end of input in "~ " >>> coef(nls(DF[,2]~a*DF[,1]^b, start=list(a=3, b=.7))) >>> Error in parse(text = x) : unexpected end of input in "~ " >>> # but this does not >>> >>> >>> Browse[1]> >>> debug: mf$formula <- as.formula(paste("~", paste(varNames[varIndex], >>> collapse = "+")), env = environment(formula)) >>> Browse[1]> >>> Error in parse(text = x) : unexpected end of input in "~ " >>>> >>> >>> Actually the problem is that with calling nls with DF[,n]~... varNames > and >>> varIndex is not correctly specified. >>> >>> I am not sure if this behaviour is a bug or feature. If it is a > feature, >>> please help me how to call variables from data frame >>> when using nls inside cycle >>> >>> for (i in ....) result[i,] <- coef(nls( ...., )) >>> >>> Thank you >>> Petr >>> >>>> sessionInfo() >>> R version 2.8.0 Under development (unstable) (2008-05-18 r45723) >>> i386-pc-mingw32 >>> >>> locale: >>> LC_COLLATE=Czech_Czech Republic.1250;LC_CTYPE=Czech_Czech >>> Republic.1250;LC_MONETARY=Czech_Czech >>> Republic.1250;LC_NUMERIC=C;LC_TIME=Czech_Czech Republic.1250 >>> >>> attached base packages: >>> [1] stats grDevices datasets utils graphics methods base >>> >>> other attached packages: >>> [1] nlme_3.1-88 lattice_0.17-7 fun_0.1 >>> >>> loaded via a namespace (and not attached): >>> [1] grid_2.8.0 tools_2.8.0 >>> >>> >>> Petr Pikal >>> petr.pikal at precheza.cz >>> 724008364, 581252140, 581252257 >>> >>> ______________________________________________ >>> 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. >>> >> >> ______________________________________________ >> 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. > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595