product can be expressed as a linear combination of the columns of the first
factor. It will help to see a specific example of this property, shown in Equa-
tion 9.2.
⎛
⎝
123
012
005
⎞
⎠
⎛
⎝
432
012
001
⎞
⎠=
⎛
⎝
459
014
005
⎞
⎠ (9.2)
The comments say that, for instance, column 3 of the product is equal to
the following:
2
⎛
⎝
1
0
0
⎞
⎠+2
⎛
⎝
2
1
0
⎞
⎠+1
⎛
⎝
3
2
5
⎞
⎠
Inspection of Equation 9.2 confirms the relation.
Couching the multiplication problem in terms of columns of the
two input matrices enables us to compact the code and to likely increase
the speed. The latter again stems from vectorization, a benefit discussed
in detail in Chapter 14. This approach is used in the loop beginning at
line 53. (Arguably, in this case, the increase in speed comes at the expense
of readability of the code.)
9.1.7 Extended Example: A Procedure for Polynomial Regression.........
As another example, consider a statistical regression setting with one pre-
dictor variable. Since any statistical model is merely an approximation, in
principle, you can get better and better models by fitting polynomials of
higher and higher degrees. However, at some point, this becomes over-
fitting, so that the prediction of new, future data actually deteriorates for
degrees higher than some value.
The class"polyreg"aims to deal with this issue. It fits polynomials of var-
ious degrees but assesses fits via cross-validation to reduce the risk of over-
fitting. In this form of cross-validation, known as theleaving-one-out method,
for each point we fit the regression to all the dataexceptthis observation, and
then we predict that observation from the fit. An object of this class consists
of outputs from the various regression models, plus the original data.
The following is the code for the"polyreg"class.
1 # "polyreg," S3 class for polynomial regression in one predictor variable
2
3 # polyfit(y,x,maxdeg) fits all polynomials up to degree maxdeg; y is
4 # vector for response variable, x for predictor; creates an object of
5 # class "polyreg"
6 polyfit <- function(y,x,maxdeg) {
7 # form powers of predictor variable, ith power in ith column
8 pwrs <- powers(x,maxdeg) # could use orthog polys for greater accuracy
9 lmout <- list() # start to build class
10 class(lmout) <- "polyreg" # create a new class
Object-Oriented Programming 219