Since I am fairly new to R, I do not know what the S3 methods and objects are. I found that there are S3 and S4 object systems, and some recommend to use S3 over S4 if possible (http://google-styleguide.googlecode.com/svn/trunk/google-r-style.html). However, I do not know the exact definition of S3 methods/objects.
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
I came to this question mostly wondering where the names came from. It appears from this wikipedia article that the name refers to the version of the S Programming Language that R is based on. The method dispatching schemes described in the other answers come from S and are labelled appropriately according to version.
Try
which lists, among others, "residuals.lm" and "residuals.glm". This means when you have fitted a linear model, m, and type
residuals(m)
, residuals.lm will be called. When you have fitted a generalized linear model, residuals.glm will be called. It's kind of the C++ object model turned upside down. In C++, you define a base class having virtual functions, which are overrided by derived classed. In R you define a virtual (aka generic) function and then you decide which classes will override this function (aka define a method). Note that the classes doing this do not need to be derived from one common super class. I would not agree to generally prefer S3 over S4. S4 has more formalism (= more typing) and this may be too much for some applications. S4 classes, however, can be de defined like a class or struct in C++. You can specify that an object of a certain class is made up of a string and two numbers for example:Methods that are called with an object of that class can rely on the object having those members. That's very different from S3 classes, which are just a list of a bunch of elements.
With S3 and S4, you call a member function by
fun(object, args)
and not byobject$fun(args)
. If you are looking for something like the latter, have a look at the proto package.Most of the relevant information can be found by looking at
?S3
or?UseMethod
, but in a nutshell:S3 refers to a scheme of method dispatching. If you've used R for a while, you'll notice that there are
print
,predict
andsummary
methods for a lot of different kinds of objects.In S3, this works by:
glm
has classglm
)print
), then a dot, and then the classname (e.g.:print.glm
)print
) for this to work, but if you're simply looking to conform yourself to existing method names, you don't need this (see the help I refered to earlier if you do).To the eye of the beholder, and particularly, the user of your newly created funky model fitting package, it is much more convenient to be able to type
predict(myfit, type="class")
thanpredict.mykindoffit(myfit, type="class")
.There is quite a bit more to it, but this should get you started. There are quite a few disadvantages to this way of dispatching methods based upon an attribute (class) of objects (and C purists probably lie awake at night in horror of it), but for a lot of situations, it works decently. With the current version of R, newer ways have been implemented (S4 and reference classes), but most people still (only) use S3.
From http://adv-r.had.co.nz/OO-essentials.html:
To get you started with S3, look at the code for the
median
function. Typingmedian
at the command prompt reveals that it has one line in its body, namelyThat means that it is an S3 method. In other words, you can have a different
median
function for different S3 classes. To list all the possible median methods, typeIn this case, there's only one method, the default, which is called for anything. You can see the code for that by typing
A much more interesting example is the
print
function, which has many different methods.Notice that some of the methods have
*
s next to their name. That means that they are hidden inside some package's namespace. Usefind
to find out which package they are in. For example