[R] Is it possible to recursively update a function?
cgw at witthoft.com
cgw at witthoft.com
Sun Mar 7 15:07:13 CET 2010
Well, it takes a bit of hacking with concatenation functions followed by get, eval, and parse,
but I've written functions/scripts which build functions like the f1,f2,... below out of various strings.
Heck, I even (don't ask :-) ) wrote a tool to invert an array along a specified dimension. That involved , for the j-th axis,
building a string like paste("[",rep(",",j-1),"1:n",rep(",",k-j),"]") (where the array was of rank k and was of length n along the j-th axis)
That's not the exact code, but you get the idea-- or at least an idea of how goofy I am.
Carl
Mar 6, 2010 12:45:04 PM, ligges at statistik.tu-dortmund.de wrote:
===========================================
It is not obvious to me if you can calculate f1, ..., f4, ...
automatically or have to set them manually. Looks like you need to do it
manually. In that case I'd suggest to write something along the lines:
dose <- c(-1.47, -1.1, -0.69, -0.42, 0.0, 0.42)
f1 <- function(x) exp(-x)
f2 <- function(x) f1(x) * (1 - 0.2^x)
f3 <- function(x) f2(x) * (1 - 0.3^x)
f4 <- function(x) f3(x) * 0.3^x
calcid <- function(f, dose){
denom <- integrate(f, 0, 100)$value
alpha I was trying to replicate one CRM simulation. The following code works but
> seems redundant so I want to create a loop.
>
> ####O'Quigley CRM example 1#####
> f1 dose p y
> f2 denom2 alpha2 id locate the next prob.
>
> f3 denom3 alpha3 id
> f4 denom4 alpha4 id
> 2010/3/5 Uwe Ligges
>
>>
>>
>> On 05.03.2010 01:40, Carl Witthoft wrote:
>>
>>> My foolish move for this week: I'm going to go way out on a limb and
>>> guess what the OP wanted was something like this.
>>>
>>> i=1, foo = x*exp(-x)
>>>
>>> i=2, foo= x^2*exp(-x)
>>> i=3, foo = x^3*exp(-x)
>>> .
>>> .
>>> .
>>>
>>>
>>> In which case he really should create a vector bar>> and then inside the loop,
>>>
>>> bar[i]>>
>>
>> Since in this case foo(x) is independent of i, you are wasting resources.
>> Moreover you could calculate it for a whole matrix at once. Say you want to
>> calculate this for i=1, ..., n with n=5 for some (here pseudo random x),
>> then you could do it simpler after defining some data as in:
>>
>> set.seed(123)
>> x> n>
>>
>> using the single and probably most efficient line:
>>
>> outer(x, 1:n, "^") * exp(-x)
>>
>> or if x is a length 1 vector then even simpler:
>>
>> set.seed(123)
>> x> n>
>> x^(1:5) * exp(-x)
>>
>> But we still do not know if this is really the question ...
>>
>> Uwe Ligges
>>
>>
>>
>>
>>> Carl
>>>
>>>
>>>
>>> quoted material:
>>> Date: Thu, 04 Mar 2010 11:37:23 -0800 (PST)
>>>
>>>
>>> I need to update posterior dist function upon the coming results and
>>> find the posterior mean each time.
>>>
>>>
>>> On Mar 4, 1:31 pm, jim holtman wrote:
>>> > What exactly are you trying to do? 'foo' calls 'foo' calls 'foo' ....
>>> > How did you expect it to stop the recursive calls?
>>> >
>>> >
>>> >
>>> >
>>> >
>>> > On Thu, Mar 4, 2010 at 2:08 PM, Seeker wrote:
>>> > > Here is the test code.
>>> >
>>> > > foo>> > > for (i in 1:5)
>>> > > {
>>> > > foo>> > > foo(2)
>>> > > }
>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>
More information about the R-help
mailing list