Stephane DRAY
2004-Oct-22 22:10 UTC
[R] Evaluate a function for various value of parameters
Hello list,
I have a problem ... and do not know how to solve it.
I would like create a function that estimate the quality of the fit of
different functions with different values of parameters.
This problem is related to the following one:
I would like to create a function "evaluatemyfunction" which evaluate
a
function f for different values of parameters. For instance, if
f=function(a,b) a*b
evaluatemyfunction(f,1:10,5) would return:
5
10
15...
I would like that f could have a variable number of arguments. I have begin
to write a function:
evaluatemyfunction=function(f,...){
b=match.call(expand.dot=F)$...
myg=expand.grid(lapply(b,eval)) #contains all combinations of parameters
for (i in 1:nrow(myg)){
argsasname=paste(paste(names(myg),myg[1,],sep="="),collapse=",")
# ??????
}
}
I have try to construct arguments of the function as character, then parse
and pass it to f but parse doesn't work:
>
parse(text=paste(paste(names(myg),myg[i,],sep="="),collapse=","))
Error in parse(file, n, text, prompt) : parse error
Any ideas ??
Thanks in advance,
sincerely
St??phane DRAY
--------------------------------------------------------------------------------------------------
D??partement des Sciences Biologiques
Universit?? de Montr??al, C.P. 6128, succursale centre-ville
Montr??al, Qu??bec H3C 3J7, Canada
Tel : (514) 343-6111 poste 1233 Fax : (514) 343-2293
E-mail : stephane.dray at umontreal.ca
--------------------------------------------------------------------------------------------------
Web http://www.steph280.freesurf.fr/
Olaf Mersmann
2004-Oct-22 22:29 UTC
[R] Evaluate a function for various value of parameters
Hi Stephane, * Stephane DRAY <dray at biomserv.univ-lyon1.fr> [041023 00:20]:> Hello list, > > I have a problem ... and do not know how to solve it. > I would like create a function that estimate the quality of the fit of > different functions with different values of parameters. > This problem is related to the following one: > I would like to create a function "evaluatemyfunction" which evaluate a > function f for different values of parameters. For instance, if > f=function(a,b) a*b > evaluatemyfunction(f,1:10,5) would return: > 5 > 10 > 15... > > I would like that f could have a variable number of arguments. I have begin > to write a function:That function has already been written for you. Lookup 'mapply' in the online help. HTH Olaf
Gabor Grothendieck
2004-Oct-23 01:24 UTC
[R] Evaluate a function for various value of parameters
Stephane DRAY <dray <at> biomserv.univ-lyon1.fr> writes: : I would like to create a function "evaluatemyfunction" which evaluate a : function f for different values of parameters. For instance, if : f=function(a,b) a*b : evaluatemyfunction(f,1:10,5) would return: : 5 : 10 : 15... : This is not a general solution (Olaf has already provided that) but in some cases you may simply be able to write your function f so that it takes vector arguments. For example, with the f you have above one could write f(1:10,5) .
ÔÚ 2004Äê10ÔÂ23ÈÕ ÐÇÆÚÁù 06:10£¬Stephane DRAY дµÀ£º> Hello list, > > I have a problem ... and do not know how to solve it. > I would like create a function that estimate the quality of the fit of > different functions with different values of parameters. > This problem is related to the following one: > I would like to create a function "evaluatemyfunction" which evaluate a > function f for different values of parameters. For instance, if > f=function(a,b) a*b > evaluatemyfunction(f,1:10,5) would return: > 5 > 10 > 15... > mapply("*",1:10,5)[1] 5 10 15 20 25 30 35 40 45 50> outer(1:10,5,"*")[,1] [1,] 5 [2,] 10 [3,] 15 [4,] 20 [5,] 25 [6,] 30 [7,] 35 [8,] 40 [9,] 45 [10,] 50 is these meet your need? try>?outer >?mapplyto see more details.
Gabor Grothendieck
2004-Oct-23 02:40 UTC
[R] Evaluate a function for various value of parameters
Gabor Grothendieck <ggrothendieck <at> myway.com> writes:
:
: Stephane DRAY <dray <at> biomserv.univ-lyon1.fr> writes:
: : I would like to create a function "evaluatemyfunction" which
evaluate a
: : function f for different values of parameters. For instance, if
: : f=function(a,b) a*b
: : evaluatemyfunction(f,1:10,5) would return:
: : 5
: : 10
: : 15...
: :
:
: This is not a general solution (Olaf has already provided that) but
: in some cases you may simply be able to write your function f so that
: it takes vector arguments. For example, with the f you have above
: one could write f(1:10,5) .
Sorry, I missed the last part of your question where you
provided some code. That code shows that you want all
combos, not just parallel evaluation of the args so here
it is again.
This answer creates a grid and evaluates. It makes use of
do.call and mapply.
The first two lines of the body collect the args into a list and
create the grid. The third line reestablishes the names of the
columns since expand.grid may not give the ones we want.
Lastly we preface the grid with the function name and mapply
the result.
evaluatemyfunction <- function(f, ...) {
args <- list(...)
grid <- do.call("expand.grid", args)
names(grid) <- names(args)
do.call("mapply", append("f", grid))
}