Page 40 - Applied Statistics with R
P. 40
40 CHAPTER 3. DATA AND PROGRAMMING
These functions could be very useful later. When used with matrices and
as arguments, it calculates
⊤
.
When dealing with linear models, the calculation
⊤
is used repeatedly.
C_mat = matrix(c(1, 2, 3, 4, 5, 6), 2, 3)
D_mat = matrix(c(2, 2, 2, 2, 2, 2), 2, 3)
This is useful both as a shortcut for a frequent calculation and as a more efficient
implementation than using t() and %*%.
crossprod(C_mat, D_mat)
## [,1] [,2] [,3]
## [1,] 6 6 6
## [2,] 14 14 14
## [3,] 22 22 22
t(C_mat) %*% D_mat
## [,1] [,2] [,3]
## [1,] 6 6 6
## [2,] 14 14 14
## [3,] 22 22 22
all.equal(crossprod(C_mat, D_mat), t(C_mat) %*% D_mat)
## [1] TRUE
crossprod(C_mat, C_mat)
## [,1] [,2] [,3]
## [1,] 5 11 17
## [2,] 11 25 39
## [3,] 17 39 61

