Skip to content

S3 vs S4 classes

3 messages · Rossi, Peter E., Brian Ripley, John Chambers

#
I have developed a package, bayesm, which uses existing classes of
objects.  I would like
to add a new class corresponding to objects from this package.

I have been reading about classes and all sources tell me that I should
use
so-called "new" or S4 classes.

However, a major purpose of defining a class for my package would be to
add methods to the existing generic functions:  print, plot, and
summary.  My understanding
is that these functions work with the "old" or S3 classes.  

If I want to use S4 classes, do I need to write new generic functions or
is there
as way to "extend" the existing generics to work with S4 classes and
objects.  I may
be using the wrong word here "extend" but I hope that everyone
understands the point.
If I have to write new generic functions, it is a simply matter of
registering the pre-existing
methods based on S3 objects?  Or do you basicaly have to start from
scratch -- i.e.
the two types of classes, methods and generic functions are distinct?

thanks 

peter r
 
................................
 Peter E. Rossi
 Joseph T. and Bernice S. Lewis Professor of Marketing and Statistics
 Editor, Quantitative Marketing and Economics
 Rm 353, Graduate School of Business, U of Chicago
 5807 S. Woodlawn Ave, Chicago IL 60637
 Tel: (773) 702-7513   |   Fax: (773) 834-2081
#
On Sat, 17 Feb 2007, Rossi, Peter E. wrote:

            
For new projects, perhaps: the issue is far less clear cut for existing 
ones.  In particular, the structure of model-fitting classes inherited 
from the White Book (lm, glm and so on) is S3 classes, and so related 
model-fitting functions (e.g. my polr) fit most naturally into the S3 
paradigm.
S4 classes should use show() rather than print(), according to the 
established practice (and I think the Green Book, but I do not have it to 
hand).  Certainly auto-printing uses show() for S4 objects in preference 
to print().
If you define an S4 method for any function, e.g. plot, it becomes S4 
generic and after looking for S4 methods will use the previous function 
(and if that is S3 generic, it will still look for S3 methods).

To my mind the advantage of S4 classes is that they are what JMC sometimes 
calls 'formal' classes: they are rigidly defined.  That suits them to new 
projects where you can think about the classes you want and define their 
structure and relationships in advance.  On the other hand, for 
development and experimenting that rigid definition can be a 
straightjacket.  I have moved various model-fitting functions to S4, and 
moved them back to S3 in frustration at the lack of fit to existing tools.
#
Rossi, Peter E. wrote:
If you define a method for any function that is currently not a generic, 
the function is automatically turned into a generic with the existing 
function becoming the default method.  So you can define methods for any 
function.

In particular, defining a method for an S3 generic makes that generic 
the default method for the S4 generic function.

Should you do this?  The main advantages are that you can then know 
something about the methods (i.e., there is some metadata that allows 
one to list the methods, ask what method would be selected for 
particular arguments, etc.),  and also,  methods can be defined for more 
than one argument.  The advantages increase if you want to define new 
classes of objects, since these also have a formal definition and some 
guarantee that the objects really do behave as the class says they do.

Basically, the S3 methods were a "quick and dirty" implementation that 
came out with the "Statistical Methods in S" project and book.  They 
have been intensively worked up for those applications, and the easy way 
out is still to use them for that purpose.

Over the (15)  years since that project got going, a more serious effort 
produced a more extensive view of classes and methods.

You should not feel pressed to choose one or another.  Some of the older 
generation are still using Fortran after all.

But the newer version was developed with the goal of having better 
software.  Some people don't like them, which is their privilege.  
People have their own goals, and their own prior investments.