Re: [R] replacing characters in formulae / models

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu, 06 Nov 2008 12:23:05 -0500

Try this:

library(alr3)
library(gsubfn)
example(delta.method) # defines m1

num <- 2
fn$delta.method(m1, "-b1/($num*b2)")

See gsubfn home page at:
http://gsubfn.googlecode.com

On Thu, Nov 6, 2008 at 12:13 PM, Christoph Scherber <Christoph.Scherber_at_agr.uni-goettingen.de> wrote:
> Dear all, Dear Keith,
>
> Well, of course in fact the problem is more complicated than that. The
> example was just for illustration.
>
> I have several statistical models for which I want to retrieve predictions
> using delta.method (from library(alr3)).
>
> Now for this I need a character string such as e.g.
> delta.method(model, "a + exp(c * x)") # model could for example be an
> exponential nls fit
>
> where the "x" shall be replaced by a numeric value at which the predictions
> plus s.e. shall be returned:
>
> delta.method(model, "a + exp(c * 10)")
>
> ##
>
> Because there are many models for which this shall be done, I would like to
> have a function that does something like the following:
>
> extract.estimates=function(model.list,xval)
> for (i in 1:length(model.list)){
> mod=model.list[i]
>
> - extract the model formula using formula(mod)
> - every time the variable "x" is present, it shall be replaced by xval
> - create a pasted character string "pastestring" to be used by delta.method
>
> result=list(delta.method(mod,pastestring))
> return(result)
> }
>
> I hope this has helped illustrating my point.
>
> All the best
> Christoph
>
>
>
>
> Keith Jewell schrieb:
>>
>> Hi,
>>
>> Firstly, I'd recommend using '<-' for assignment, rather than '='; it
>> saves confusion
>> Secondly, I don't think you want 'a*x+b' as a formula, I think you want an
>> expression.
>> Thirdly, your 'y' has only one term, a 9 character constant = "a * x + b"
>>
>> Consider instead,
>> y <- expression(a*x+b)
>> a <- 2
>> b <- 3
>> x <- 1:10
>> y
>> eval(y)
>>
>> Now, how to replace 'x' by 'w'?
>> I'm not an expert, but this is the kind of thing I need to do, so I'd
>> welcome criticism of my approach.
>> I would view the expression as a list:
>> as.list(y)
>> as.list(y[[1]])
>>
>> So y is an expression containing a sub-expression; that is y is '(a*x) +
>> b'
>> You want to access the sub-expression 'a*x'
>> y[[1]][[2]]
>> as.list(y[[1]][[2]])
>>
>> Now you want to replace the third item in that sub-expression with the
>> name (not the character) w
>> y[[1]][[2]][[3]] <- as.name("w")
>> w <- 11:20
>> y
>> eval(y)
>>
>> hth
>>
>> Keith J
>>
>> P.S. Perhaps you really do want a formula; y ~ a*x+b ??
>> In that case I'd still probably manipulate it as a list.
>> -------------------------------------------
>> "Christoph Scherber" <Christoph.Scherber_at_agr.uni-goettingen.de> wrote in
>> message news:4913125C.2060500_at_agr.uni-goettingen.de...
>>>
>>> Dear all,
>>>
>>> How can I replace text in objects that are of class "formula"?
>>>
>>> y="a * x + b"
>>> class(y)="formula"
>>> grep("x",y)
>>> y[1]
>>>
>>> Suppose I would like to replace the "x" by "w" in the formula object "y".
>>>
>>> How can this be done? Somehow, the methods that can be used in character
>>> objects do not work 1:1 in formula objects...
>>>
>>> Many thanks and best wishes
>>> Christoph
>>>
>>>
>>>
>>> --
>>> Dr. rer.nat. Christoph Scherber
>>> University of Goettingen
>>> DNPW, Agroecology
>>> Waldweg 26
>>> D-37073 Goettingen
>>> Germany
>>>
>>> phone +49 (0)551 39 8807
>>> fax +49 (0)551 39 8806
>>>
>>> Homepage http://www.gwdg.de/~cscherb1
>>
>> ______________________________________________
>> 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.
>>
>> .
>>
>
> --
> Dr. rer.nat. Christoph Scherber
> University of Goettingen
> DNPW, Agroecology
> Waldweg 26
> D-37073 Goettingen
> Germany
>
> phone +49 (0)551 39 8807
> fax +49 (0)551 39 8806
>
> Homepage http://www.gwdg.de/~cscherb1
>
> ______________________________________________
> 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. Received on Thu 06 Nov 2008 - 17:31:27 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Thu 06 Nov 2008 - 18:30:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive