![](https://csdnimg.cn/release/download_crawler_static/16820968/bg4.jpg)
The numerical estimates are exactly the same, but our code lacks a convenient user interface:
1. Prettier formatting of results.
2. Add utilities for fitted model like a summary() function to test for significance of parameters.
3. Handle categorical predictors.
4. Use formulas for model specification.
Object oriented programming helps us with issues 1 and 2, formulas with 3 and 4.
3 Object oriented programming in R
3.1 S3 and S4
Our function linmodEst returns a list with four named elements, the parameter estimates and
their covariance matrix, and the standard deviation and degrees of freedom of the residuals. From
the context it is clear to us that this is a linear model fit, however, nobody told the computer so
far. For the computer this is simply a list containing a vector, a matrix and two scalar values.
Many programming languages, including S, use so-called
1. classes to define how objects of a certain type look like, and
2. methods to define special functions operating on objects of a certain class
A class defines how an object is represented in the program, while an object is an instance of the
class that exists at run time. In our case we will shortly define a class for linear model fits. The
class is the abstract definition, while every time we actually use it to store the results for a given
data set, we create an object of the class.
Once the classes are defined we probably want to perform some computations on objects. In
most cases we do not care how the object is stored internally, the computer should decide how to
perform the tasks. The S way of reaching this goal is to use generic functions and method dispatch:
the same function performs different computations depending on the classes of its arguments.
S is rare because it is both interactive and has a system for object-orientation. Designing
classes clearly is programming, yet to make S useful as an interactive data analysis environment,
it makes sense that it is a functional language. In “real” object-oriented programming (OOP)
languages like C++ or Java class and method definitions are tightly bound together, methods
are part of classes (and hence objects). We want incremental and interactive additions like user-
defined methods for pre-defined classes. These additions can be made at any point in time, even
on the fly at the command line prompt while we analyze a data set. S tries to make a compromise
between object orientation and interactive use, and although compromises are never optimal with
respect to all goals they try to reach, they often work surprisingly well in practice.
The S language has two object systems, known informally as S3 and S4.
S3 objects, classes and methods have been available in R from the beginning, they are informal,
yet “very interactive”. S3 was first described in the “White Book” (Chambers & Hastie
1992).
S4 objects, classes and methods are much more formal and rigorous, hence “less interactive”. S4
was first described in the “Green Book” (Chambers 1998). In R it is available through the
methods package, attached by default since version 1.7.0.
S4 provides formal object oriented programming within an interactive environment. It can help
a lot to write clean and consistent code, checks automatically if objects conform to class definitions,
and has much more features than S3, which in turn is more a set of naming conventions than a
true OOP system, but it is sufficient for most purposes (take almost all of R as proof). Because
S3 is much easier to learn than S4 and sufficient for our purposes, we will use it throughout this
tutorial.
4