Suppose I want to write a function in R , which is a function of several sufficient statistics for some data. For example, suppose a function called foo.func depends only on the average sample data sample. For convenience, I think users may want to switch to a foo.func sample of random variables (in this case, foo.func calculates the average value of the sample), or the sample itself means that all that foo.func requires. For efficiency reasons, the latter is preferable if several functions are called, such as foo.func , which can take the average value of the selection. In this case, the average value needs to be calculated only once (in the real problem that I have, the sample statistics in question can be computationally intensive).
In conclusion, I would like to write foo.func in order to be accessible for a beginner (to transmit data, to let the function calculate sufficient statistics), as well as an expert (to compare previously sufficient performance statistics and pass them to). What are the recommended methods for doing this? Do I have a boolean flag? A few arguments? Some ways to do this may be:
#optional arguments foo.func <- function(xdata, suff.stats=NULL) { if (is.null(suff.stats)) { suff.stats <- compute.suff.stats(x) }
or
#flag input foo.func <- function(data.or.stat, gave.data=TRUE) { if (gave.data) { data.or.stat <- compute.suff.stats(data.or.stat) }
I am inclined to the first, I think
polymorphism r
shabbychef
source share