[R] Trying to build up functions with its names by means of lapply
Bert Gunter
gunter.berton at gene.com
Wed Jun 5 19:51:10 CEST 2013
Rui et al.
Certainly correct.
However, I think the use of force() and similar should be avoided if
possible, as computing on the language can be tricky. So here is an
alternative formulation that avoids it:
faux <- function(c){
f <- get(paste0(c,"gamma")) ## evaluation of c is forced here
function(x)f(x,k,scale=theta)
}
Now another way to do this is:
faux <- function(c, nm = paste0(c,"gamma")){
f <- get(nm)
function(x)nm(x,k,scale=theta)}
This, in turn, suggests a more flexible function that would work for
any distribution, using ... to pass in the arguments needed
faux <- function(c, nm = "gamma",...){
f <- get(paste0(c,nm))
function(x)f(x,...)
}
This could be called with:
> xgam <- lapply(c("p","d"), faux, shape=k, scale=theta)
> xgam[[1]](1000)
[1] 0.8710477
> xgam[[2]](1000)
[1] 0.001265311
But it would also work for, e.g. "norm"
> xnorm <- lapply(c("p","d"), faux, nm="norm", mean=5)
> xnorm[[1]](6)
[1] 0.8413447
> xnorm[[2]](6)
[1] 0.2419707
Cheers,
Bert
P.S. Bill Dunlap might have something to say about this. I think there
may be better ways to approach this whole business, but I prefer such
insight from real experts.
-- Bert
On Wed, Jun 5, 2013 at 2:58 AM, Rui Barradas <ruipbarradas at sapo.pt> wrote:
> Hello,
>
> My solution works but it is incorrect. We should force the argument 'c', not
> the return value. Like said in the help page for force. Which I've only read
> after my first post. The following way makes much more sense and is a bit
> shorter.
>
> faux <- function(c) {
> force(c)
> function (x) get(paste0(c,"gamma"))(x,k,scale=theta)
> }
>
> Rui Barradas
>
> Em 05-06-2013 10:49, Rui Barradas escreveu:
>>
>> Hello,
>>
>> If in faux we ?force the return value, the bug is gone.
>>
>>
>> faux <- function(c) {
>> f <- function (x) get(paste0(c,"gamma"))(x,k,scale=theta)
>> force(f)
>> f
>> }
>>
>> Hope this helps,
>>
>> Rui Barradas
>>
>> Em 05-06-2013 07:13, Michael Weylandt escreveu:
>>>
>>>
>>>
>>> On Jun 5, 2013, at 3:53, Julio Sergio <juliosergio at gmail.com> wrote:
>>>
>>>> I want to generate specific gamma distribution functions, given fixed
>>>> parameters.
>>>> This is I have k, and theta, say
>>>>
>>>> k <- 32.2549 # shape
>>>> theta <- 26.32809 # scale
>>>>
>>>>
>>>> # I have an auxiliary function that produces funcions according to
>>>> # a given character (this is to have either dgamma, pgamma or qgamma)
>>>> # for the specific parameters given above:
>>>>
>>>> faux <- function(c) {
>>>> function (x) get(paste0(c,"gamma"))(x,k,scale=theta)
>>>> }
>>>>
>>>> # So I can have, for instance, dgamma, and pgamma with
>>>>
>>>> dff <- faux("d")
>>>> pff <- faux("p")
>>>>
>>>> dff(1000)
>>>> ## [1] 0.001433138
>>>>
>>>> pff(1000)
>>>> ## [1] 0.844305
>>>>
>>>> Now, if I try to produce both functions in one shot with lapply, the
>>>> thing
>>>> doesn't work, see
>>>>
>>>> ffs <- lapply(c("d", "p"), faux)
>>>>
>>>> ffs[[1]](1000)
>>>> ## [1] 0.844305
>>>>
>>>> ffs[[2]](1000)
>>>> ## [1] 0.844305
>>>>
>>>> The two produced functions are the very same and correspond to pgamma!!
>>>> Maybe I'm missing something. Do you have any idea?
>>>
>>>
>>> I think you are hitting a bit of strangeness R generously calls 'lazy
>>> evaluation'. I'm afraid I don't have a reference at hand, but search
>>> the archives for mention of the promise mechanism.
>>>
>>> MW
>>>
>>>>
>>>> Thanks,
>>>>
>>>> -Sergio.
>>>>
>>>> ______________________________________________
>>>> 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.
>
>
> ______________________________________________
> 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.
--
Bert Gunter
Genentech Nonclinical Biostatistics
Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm
More information about the R-help
mailing list