[R] How to convert List object to function arguments?
Shiazy
shiazy at gmail.com
Sat Mar 3 15:32:37 CET 2007
Dear R gurus,
I have a function "goftests" that receives the following arguments:
* a vector "x" of data values;
* a distribution name "dist";
* the dots list ("...") containing a list a parameters to pass to CDF
function;
and calls several goodness-of-fit tests on the given data values against
the given distribution.
That is:
##### BEGIN CODE SNIP #####
# Covert a distribution name to the related CDF function name
distname2cdfname <- function( dist )
{
if ( dist == "beta" ) { return( "pbeta" ); }
if ( dist == "cauchy" ) { return( "pcauchy" ); }
# many other and personalized (e.g. pareto, ...) distributions ...
return( "" ); # fall-back case
}
# Performs some GoF tests (KS, Chi-Square, Anderson-Darling,...)
# (the "..." list contains parameters for CDF function)
goftests <- function( x, dist, ... )
{
cdfname <- distname2cdfname( dist );
res$ks <- ks.test( x, cdfname, ... );
# res$chisq <- ...
# res$anddarl <- ...
return( res )
};
##### END CODE SNIP #####
So the problem is passing "..." to CDF function (for instance, see
ks.test above) in the "right form". The "..." is passed (to "goftests")
as a list object and it should be converted to an "argument list".
For instance:
##### BEGIN CODE SNIP #####
parms <- list( shape1 = 2, shape2 = 5 );
goftests( x, "beta", parms[["shape1"]], parms[["shape2"]] ); # Works!
goftests( x, "beta", parms ) # Don't Works!
parms <- list( aNum = 5, aVector = c(1,2,3), aMatrix =
matrix(c(4,5,6,7,8,9),nrow=2,byrow=T) );
goftests( x, "my-special-distr", parms[["aVector"]], parms[["aMatrix"]],
parms[["aNumber"]] ); # Works!
goftests( x, "my-special-distr", parms ) # Don't Works!
##### END CODE SNIP #####
Obviously I have to use the "don't work" form.
How can I do this in R (if possible)?
Thank you very much in advance!
Best regards,
-- Marco
More information about the R-help
mailing list