s-news
[Top] [All Lists]

Re: [S] using strings for formula in ms()

To: "Kristian S. Gleditsch" <ksg@cfia.harvard.edu>
Subject: Re: [S] using strings for formula in ms()
From: Prof Brian D Ripley <ripley@stats.ox.ac.uk>
Date: Thu, 20 Aug 1998 07:26:02 +0100 (BST)
Cc: s-news@wubios.wustl.edu
In-reply-to: <3.0.2.32.19980820004705.006e0260@latte.harvard.edu>
Sender: owner-s-news@wubios.wustl.edu
On Thu, 20 Aug 1998, Kristian S. Gleditsch wrote:

> I have a fairly lengthy equation that I want to minimize with ms() inside a
> generic function I am writing

`generic' as in `general' not as in S `generic'?

> I would like to create the formula corresponding to a set of right hand
> side variables in a character vector
> 
> (e.g., xxx.formula <- c("log(1-lambda*wroot),
>       paste("+lambda*b",1:(length(betas)-1),"*",sep="",...")
> 
> with the different variables and parameters used all in a dataframe
> 
> (e.g. xxx.data). 
> 
> However, I am unable to use this character vector as the formula in ms, e.g.
> 
>  ms(~xxx.formula,data=xxx.data)
> 
> Must I convert the vector to some other mode or class, or is there any
> other way to get this to work?

There are several ways, but two of the most promising are:

(1) ms(formula(paste("~", xxx.formula)), ...)

(2) eval(parse(text=paste("ms(~", xxx.formula, ", data=xxx.data)",sep="")))


As in (from the ms help page)

   lprob <- function(lp)log(1+ exp(lp)) - lp  # log-likelihood
   xxx.formula <- "lprob(D * alpha)"
   ms(formula(paste("~", xxx.formula)), pingpong)
value: 1127.635 
parameters:
      alpha 
 0.01114251
formula:  ~  lprob(D * alpha) 
3017 observations
call: ms(formula = formula(paste("~", xxx.formula)), data = pingpong) 

If you want to see a sensible call in the result, you can use

  xxx.data <- pingpong
  eval(parse(text=paste("ms(~", xxx.formula, ", data=xxx.data)",sep="")))
value: 1127.635 
parameters:
      alpha 
 0.01114251
formula:  ~  lprob(D * alpha) 
3017 observations
call: ms(formula =  ~ lprob(D * alpha), data = xxx.data) 

There are slicker ways in S version 4, so a reminder than when S-PLUS 5
appears it will be vital to tell us which system you are using. 

-- 
Brian D. Ripley,                  ripley@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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>