Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the suggested practices for function polymorphism in R?

Tags:

r

polymorphism

Suppose I want to write a function in R which is a function of a couple of sufficient statistics on some data. For example, suppose the function, call it foo.func depends only on the sample mean of a sample of data. For convenience, I think users might like to pass to foo.func the sample of random variables (in which case foo.func computes the sample mean), or the sample mean itself, which is all that foo.func needs. For reasons of efficiency, the latter is preferred if there are multiple functions like foo.func being called which can take the sample mean. In that case the mean need only be computed once (in the real problem I have, the sample statistics in question might be computationally intensive).

In summary, I would like to write foo.func to be accessible to the beginner (pass in the data, let the function compute the sufficient statistics) as well as the expert (precompute the sufficient statistics for efficiency and pass them in). What are the recommended practices for this? Do I have a logical flag passed in? Multiple arguments? Some ways to do it might be:

#optional arguments
foo.func <- function(xdata, suff.stats=NULL) {
  if (is.null(suff.stats)) {
    suff.stats <- compute.suff.stats(x)
  }
  #now operate on suff.stats
}

or

#flag input
foo.func <- function(data.or.stat, gave.data=TRUE) {
  if (gave.data) {
    data.or.stat <- compute.suff.stats(data.or.stat)
  }
  #now operate on data.or.stat
}

I am leaning towards the former, I think

like image 807
shabbychef Avatar asked Oct 28 '11 17:10

shabbychef


People also ask

What is required for polymorphism?

Static polymorphism They need to have a different number of parameters, one method accepting 2 and another one accepting 3 parameters. The types of the parameters need to be different, one method accepting a String and another one accepting a Long. They need to expect the parameters in a different order.

What is the limit for polymorphism?

A more stringent definition of genetic polymorphism sets a lower limit to the frequency of the most common allele (95% or less).

In which way polymorphism can be implemented in Python?

As is the case with other programming languages like as Java and C++, polymorphism is implemented in Python for a variety of purposes, most notably Duck Typing, Operator and Method overloading, and Method overriding. This polymorphism may be accomplished in two distinct ways: overloading and overriding.

What is polymorphism and example in Python?

Polymorphism is a programming term that refers to the use of the same function name, but with different signatures, for multiple types. Example of in-built polymorphic functions: # Python program for demonstrating the in-built poly-morphic functions. # len() function is used for a string. print (len("Javatpoint"))


2 Answers

The R way of implementing polymorphism is through a CLOS (Common Lisp's OO) model where methods are associated with generic functions (verbs) rather than classes (nouns). For instance,

# suprising that there is not an equivalent function in R
# to propagate inheritance...
addclass <- function(x,classname) structure(x,class=append(class(x),classname))

# this should be your main function that does stuff
# here, the identity function is assigned for example
dostuff <- identity

# define generic function and methods
foo <- function(x,...) UseMethod("foo")
foo.raw <- function(x,...) dostuff(mean(x))
foo.stats <- function(x,...) dostuff(x)

# define two types of inputs
x <- 1:10
x <- addclass(x,"raw")

y <- 5
y <- addclass(y,"stats")

# apply
foo(x)
# [1] 5.5
foo(y)
# [1] 5
# attr(,"class")
# [1] "numeric" "stats"  

The example was using R's S3 OOP model, which I think are quite sufficient; S4 is more modern and safe but adds a lot of boilerplate.

like image 119
hatmatrix Avatar answered Oct 18 '22 19:10

hatmatrix


You can also embed functions into the arguments, as:

foo.func <- function(x, suff.stats = foo.func.suff.stat(x)){
  # your code here
}

As an example:

foo.func <- function(x, avg = mean(x)){
  return(avg)
}

foo.func(1:20)
foo.func(avg = 42)

Alternatively, you can either use a default setting of NULL for various arguments, and test for is.null(argument), or simply check the value of missing(argument) for each for each argument you might calculate.


Update 1: I erred in suggesting use of a default value of NA: it is far more appropriate to use NULL. Using NA and is.na() will behave oddly for vector inputs, whereas NULL is just a single object - one cannot create a vector of NULL values, so is.null(argument) behaves as expected. Apologies for the forgetfulness.

like image 8
Iterator Avatar answered Oct 18 '22 18:10

Iterator