
R: Funciones genéricas
Analizando el ajuste de un modelo lineal simple utilizando la funcion plot()
Residuos vs Estimados:
model1 <- lm(log(population_c1) ~ log(diameter_c1), data=planets_df_c)
plot(model1, 1)
Me percato que la función plot para mi sorpresa podía recibir distintos parametros de entrada, dependiendo del primero de ellos.
Así es como llego a buscar la función plot de la libreria graphics.
Generic function for plotting of R objects. For simple scatter plots, plot.default will be used. However, there are plot methods for many R objects, including functions, data.frames, density objects, etc. Use methods(plot) and the documentation for these.
Es como llego a este blog Advanced R by Hadley Wickham Donde en la seccion de The S3 object system describe las funciones genéricas de R y la sobrecarga de funciones:
R has three object oriented systems (plus the base types) Central to any object-oriented system are the concepts of class and method. A class defines the behaviour of objects by describing their attributes and their relationship to other classes. The class is also used when selecting methods, functions that behave differently depending on the class of their input. Classes are usually organised in a hierarchy: if a method does not exist for a child, then the parent’s method is used instead; the child inherits behaviour from the parent.
R’s three OO systems differ in how classes and methods are defined:
S3 implements a style of OO programming called generic-function OO. This is different from most programming languages, like Java, C++, and C#, which implement message-passing OO. With message-passing, messages (methods) are sent to objects and the object determines which function to call. Typically, this object has a special appearance in the method call, usually appearing before the name of the method/message: e.g., canvas.drawRect("blue"). S3 is different. While computations are still carried out via methods, a special type of function called a generic function decides which method to call, e.g., drawRect(canvas, "blue"). S3 is a very casual system. It has no formal definition of classes.
S4 works similarly to S3, but is more formal. There are two major differences to S3. S4 has formal class definitions, which describe the representation and inheritance for each class, and has special helper functions for defining generics and methods. S4 also has multiple dispatch, which means that generic functions can pick methods based on the class of any number of arguments, not just one.
Reference classes, called RC for short, are quite different from S3 and S4. RC implements message-passing OO, so methods belong to classes, not functions. $ is used to separate objects and methods, so method calls look like canvas$drawRect("blue"). RC objects are also mutable: they don’t use R’s usual copy-on-modify semantics, but are modified in place. This makes them harder to reason about, but allows them to solve problems that are difficult to solve with S3 or S4.
There’s also one other system that’s not quite OO, but it’s important to mention here:
base types, the internal C-level types that underlie the other OO systems. Base types are mostly manipulated using C code, but they’re important to know about because they provide the building blocks for the other OO systems. Only R-core can add new classes to this system, but it’s important to know about because it underpins the three other systems.
Generic functions and method dispatch Method dispatch starts with a generic function that decides which specific method to dispatch to. Generic functions all have the same form: a call to UseMethod that specifies the generic name and the object to dispatch on. This means that generic functions are usually very simple, like mean:
mean <- function (x, ...) {
UseMethod("mean", x)
}
Methods are ordinary functions that use a special naming convention: generic.class:
mean.numeric <- function(x, ...) sum(x) / length(x)
mean.data.frame <- function(x, ...) sapply(x, mean, ...)
mean.matrix <- function(x, ...) apply(x, 2, mean)
type vs class
'mode' is a mutually exclusive classification of objects according to their basic structure. The 'atomic' modes are numeric, complex, character and logical. Recursive objects have modes such as 'list' or 'function' or a few others. An object has one and only one mode.
'class' is a property assigned to an object that determines how generic functions operate with it. It is not a mutually exclusive classification. If an object has no specific class assigned to it, such as a simple numeric vector, it's class is usually the same as its mode, by convention.
typeof reveals the most fundamental datatype for R objects, going all the way down to the underlying C language datatypes in which R is implemented. So, it indicates how the variable is stored in the computer memory in terms of the lowest-level datatypes available in R. An object can only have one typeof type. The different possibilities can be found from help(typeof)
typeof is read-only whereas class is read-write. That is, we cannot directly change an object's typeof type; we must convert the underlying object into an object of a different type; indeed, there is no assignment allowed for typeof. In contrast, class allows assignment, which, for basic datatypes, converts the variable from one datatype to another. So, a variable's typeof can be changed by indirectly converting it by changing its class.