Hi all, I have a problem with an object inside a formula in a function that I make. I create an object named qvartemp. This object is OK, look the levels:> print(levels(qvartemp))[1] "baixomedio" "alto" Now I create a new object with a new formula:> new.form <- as.formula(gsub(qvar,"qvartemp",as.expression(mma$formula)))Look this new object:> print(new.form)Riqueza ~ Biomassa * qvartemp Now here is the problem, I try to update an existing model (mma) changing the formula by the new.form mmaa <- update(mma,new.form) But my function is aborted with this message: Error in eval(expr, envir, enclos) : object "qvartemp" not found If I create this object qvartemp in R section manually it my function work, because it get the object from my workspace (but it is usefull just for testing), but my function dont find this object qvartemp created inside the function. If I run all my function line by line without use the function it work, I think that is because in this case the object is created in my workspace. But when I run a function the object is created only in memory (not in my workspece) and in this case update dont find this object. Anybody can suggest any Idea to fix it? I necessary I send my function and a example. Thanks Ronaldo -- Everywhere is within walking distance if you have the time. -- Steven Wright. --> Prof. Ronaldo Reis J?nior| .''`. UNIMONTES/Depto. Biologia Geral/Lab. de Biologia Computacional | : :' : Campus Universit?rio Prof. Darcy Ribeiro, Vila Mauric?ia | `. `'` CP: 126, CEP: 39401-089, Montes Claros - MG - Brasil | `- Fone: (38) 3229-8187 | ronaldo.reis em unimontes.br | chrysopa em gmail.com | http://www.ppgcb.unimontes.br/ | ICQ#: 5692561 | LinuxUser#: 205366
Em Dom 20 Abr 2008, Ronaldo Reis Junior escreveu:> Hi all, > > I have a problem with an object inside a formula in a function that I make. > > I create an object named qvartemp. This object is OK, look the levels: > > print(levels(qvartemp)) > > [1] "baixomedio" "alto" > > Now I create a new object with a new formula: > > new.form <- as.formula(gsub(qvar,"qvartemp",as.expression(mma$formula))) > > Look this new object: > > print(new.form) > > Riqueza ~ Biomassa * qvartemp > > Now here is the problem, I try to update an existing model (mma) changing > the formula by the new.form > > mmaa <- update(mma,new.form) > > But my function is aborted with this message: > > Error in eval(expr, envir, enclos) : object "qvartemp" not found > > If I create this object qvartemp in R section manually it my function work, > because it get the object from my workspace (but it is usefull just for > testing), but my function dont find this object qvartemp created inside the > function. > > If I run all my function line by line without use the function it work, I > think that is because in this case the object is created in my workspace. > But when I run a function the object is created only in memory (not in my > workspece) and in this case update dont find this object. > > Anybody can suggest any Idea to fix it? I necessary I send my function and > a example. > > Thanks > RonaldoCompleting the problem, if I use> mmaa <- glm(new.form)instead update(mma,new.form) the function work perfectly, but using update I can generalise my function and I need this. The problem is with update function, It maybe get the object differently, but I dont find the problem. Thanks Ronaldo -- Positive, adj.: Mistaken at the top of one's voice. -- Ambrose Bierce, "The Devil's Dictionary" --> Prof. Ronaldo Reis J?nior| .''`. UNIMONTES/Depto. Biologia Geral/Lab. de Biologia Computacional | : :' : Campus Universit?rio Prof. Darcy Ribeiro, Vila Mauric?ia | `. `'` CP: 126, CEP: 39401-089, Montes Claros - MG - Brasil | `- Fone: (38) 3229-8187 | ronaldo.reis em unimontes.br | chrysopa em gmail.com | http://www.ppgcb.unimontes.br/ | ICQ#: 5692561 | LinuxUser#: 205366
On 20/04/2008 9:40 AM, Ronaldo Reis Junior wrote:> Hi all, > > I have a problem with an object inside a formula in a function that I make. > > I create an object named qvartemp. This object is OK, look the levels: > >> print(levels(qvartemp)) > [1] "baixomedio" "alto" > > Now I create a new object with a new formula: > >> new.form <- as.formula(gsub(qvar,"qvartemp",as.expression(mma$formula))) > > Look this new object: > >> print(new.form) > Riqueza ~ Biomassa * qvartemp > > Now here is the problem, I try to update an existing model (mma) changing the > formula by the new.form > > mmaa <- update(mma,new.form) > > But my function is aborted with this message: > > Error in eval(expr, envir, enclos) : object "qvartemp" not found > > If I create this object qvartemp in R section manually it my function work, > because it get the object from my workspace (but it is usefull just for > testing), but my function dont find this object qvartemp created inside the > function. > > If I run all my function line by line without use the function it work, I > think that is because in this case the object is created in my workspace. But > when I run a function the object is created only in memory (not in my > workspece) and in this case update dont find this object. > > Anybody can suggest any Idea to fix it? I necessary I send my function and a > example.Formulas have attached environments; that's where they look for the variables they use. You created new.form, but didn't give it the environment of the old one. I think all you need is environment(new.form) <- environment(mma$formula) Duncan Murdoch
On 20/04/2008 3:04 PM, Ronaldo Reis Junior wrote:> Em Dom 20 Abr 2008, Duncan Murdoch escreveu: >> On 20/04/2008 12:32 PM, Ronaldo Reis Junior wrote: >>> Em Dom 20 Abr 2008, Duncan Murdoch escreveu: >>>> On 20/04/2008 9:40 AM, Ronaldo Reis Junior wrote: >>>>> Hi all, >>>>> >>>>> I have a problem with an object inside a formula in a function that I >>>>> make. >>>>> >>>>> I create an object named qvartemp. This object is OK, look the levels: >>>>>> print(levels(qvartemp)) >>>>> [1] "baixomedio" "alto" >>>>> >>>>> Now I create a new object with a new formula: >>>>>> new.form <- >>>>>> as.formula(gsub(qvar,"qvartemp",as.expression(mma$formula))) >>>>> Look this new object: >>>>>> print(new.form) >>>>> Riqueza ~ Biomassa * qvartemp >>>>> >>>>> Now here is the problem, I try to update an existing model (mma) >>>>> changing the formula by the new.form >>>>> >>>>> mmaa <- update(mma,new.form) >>>>> >>>>> But my function is aborted with this message: >>>>> >>>>> Error in eval(expr, envir, enclos) : object "qvartemp" not found >>>>> >>>>> If I create this object qvartemp in R section manually it my function >>>>> work, because it get the object from my workspace (but it is usefull >>>>> just for testing), but my function dont find this object qvartemp >>>>> created inside the function. >>>>> >>>>> If I run all my function line by line without use the function it work, >>>>> I think that is because in this case the object is created in my >>>>> workspace. But when I run a function the object is created only in >>>>> memory (not in my workspece) and in this case update dont find this >>>>> object. >>>>> >>>>> Anybody can suggest any Idea to fix it? I necessary I send my function >>>>> and a example. >>>> Formulas have attached environments; that's where they look for the >>>> variables they use. You created new.form, but didn't give it the >>>> environment of the old one. >>>> >>>> I think all you need is >>>> >>>> environment(new.form) <- environment(mma$formula) >>>> >>>> Duncan Murdoch >>> Hi Duncan, >>> >>> thanks for your quick reply. I think that is not the problem, because >>> with glm or lm the formula inside the new.form work, only on update it >>> dont work. >>> >>> But I try this solution, I put new.form enclosed in an environment: >>> >>> environment(new.form) <- >>> environment(as.formula(gsub(qvar,"qvartemp",as.expression(mma$formula)))) >>> >>> But the error is the same. >> Of course it is. That operation does nothing, since the value of >> new.form was already >> as.formula(gsub(qvar,"qvartemp",as.expression(mma$formula))). >> >> If you want any more help, you're going to have to provide a >> reproducible example. >> >> Duncan Murdoch > > Duncan, > > sorry, I dont understand how to make this. > > I send for you a full code with an example that work and other that dont work.This is a little more complicated than I thought. The problem is that update() uses the environment of the old formula, and ignores the new environment. So you need to sneak your qvartemp into visibility in the old formula: new.form <- as.formula(gsub(qvar,"qvartemp",as.expression(mma$formula))) environment(mma$formula) <- new.env(parent = environment(mma$formula)) environment(mma$formula)$qvartemp <- qvartemp print(new.form) mmaa <- update(mma,new.form) This appears to work. It looks ugly; I don't know if there's a cleaner way to achieve what you want, i.e. to update a formula to use a variable that's defined locally in a function. Duncan Murdoch