Re: [R] function to transform response of a formula

From: Paul Emberson <email_at_calidasoft.co.uk>
Date: Wed, 30 Jul 2008 13:50:32 +0100

Hi,

I am now trying to use update.formula from within a function but I am not able to get the result I want. Here is an example

f <- function(fm, n) {

     update(fm, n*. ~ .);
}

If I run

 > fm <- formula( y ~ a + b )
 > f(fm,3)
n * y ~ a + b

Now when I try to use the formula, n does not exist. Is there a way I can change my function so that n is replaced by its value within the formula created by f?

Thanks,

Paul

email_at_calidasoft.co.uk wrote:

> Its ok.
> 
> I've just read about update.formula in another message.
> 
> Paul
> 
> Paul Emberson wrote:

>> Hi,
>>
>> I am trying to write a function which takes a formula as input and
>> outputs a new formula with a different response while keeping the rest
>> of the formula the same.
>>
>> I.e.
>>
>> respapply : ( y ~ a+b ) -> ( f(y) ~ a + b )
>>
>> I have tried the following but it doesn't work. The terms become
>> invalid as shown below.
>>
>> respapply <- function(fm, f) {
>>
>> fm[[2]] <- f(eval(fm[[2]]))
>> fm;
>>
>> }
>>
>> > fm <- formula(y ~ a + b)
>> > y <- runif(5)
>> > newfm <- respapply(fm,identity)
>> > newfm
>>
>> c(0.552921097259969, 0.939932722365484, 0.62522904924117,
>> 0.899310540175065,
>> 0.877736972644925) ~ a + b
>>
>> > terms(newfm)
>> Error in terms.formula(fmapply(fm, identity)) :
>> invalid term in model formula
>>
>> Could someone put me in the right direction of how to correctly write
>> a respapply function as described.
>>
>> Regards,
>>
>> Paul Emberson
>>
>> ______________________________________________
>> 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. Received on Wed 30 Jul 2008 - 13:06:56 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 Wed 30 Jul 2008 - 13:32:59 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