# Clear the work space
rm(list = ls()) # Clear environment
gc() # Clear unused memory
## used (Mb) gc trigger (Mb) limit (Mb) max used (Mb)
## Ncells 526348 28.2 1169210 62.5 NA 669282 35.8
## Vcells 968254 7.4 8388608 64.0 32768 1840408 14.1
cat("\f") # Clear the console
graphics.off() # Clear plots. Can use par(mfrow=c(1,1))
Either look at the math camp notes, or on some simplified online blogs.
Thus, you should always check for variable values and duplicate rows in your data to be able to check if you can estimate your “beta” coefficients in your linear regression as you have to invert the your data matrix to find it. Most statistical software will automatically drop duplicate rows but will give you an error if the matrix is not invertible.
Will you be able to invert these 4 matrices ?
\[ \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ \end{bmatrix} \]
A <- matrix(data = c(1,2,
3,4),
nrow = 2,
ncol = 2,
byrow = TRUE
)
A
## [,1] [,2]
## [1,] 1 2
## [2,] 3 4
det(A)
## [1] -2
solve(A)
## [,1] [,2]
## [1,] -2.0 1.0
## [2,] 1.5 -0.5
Combine rows under each other.
a1 <- c(1,2)
a2 <- c(3,4)
?rbind
A <- rbind(a1,a2)
A
## [,1] [,2]
## a1 1 2
## a2 3 4
det(A)
## [1] -2
solve(A)
## a1 a2
## [1,] -2.0 1.0
## [2,] 1.5 -0.5
Combine columns after each other.
a1 <- c(1,3)
a2 <- c(2,4)
?cbind
A <- cbind(a1,a2)
det(A)
## [1] -2
solve(A)
## [,1] [,2]
## a1 -2.0 1.0
## a2 1.5 -0.5
# install.packages("RConics")
require(RConics)
## Loading required package: RConics
?RConics
R <- matrix(data = c(1,0,1,
2,2,1,
0,0,3),
nrow = 3,
ncol = 3,
byrow = TRUE
)
R
## [,1] [,2] [,3]
## [1,] 1 0 1
## [2,] 2 2 1
## [3,] 0 0 3
adj_R <- adjoint(R) # Compute the classical adjoint (also called adjugate) of a square matrix. The adjoint is the transpose of the cofactor matrix.
adj_R
## [,1] [,2] [,3]
## [1,] 6 0 -2
## [2,] -6 3 1
## [3,] 0 0 2
det(R)
## [1] 6
R_inverse <- adj_R/det(R)
R_inverse
## [,1] [,2] [,3]
## [1,] 1 0.0 -0.3333333
## [2,] -1 0.5 0.1666667
## [3,] 0 0.0 0.3333333
solve(R)
## [,1] [,2] [,3]
## [1,] 1 0.0 -0.3333333
## [2,] -1 0.5 0.1666667
## [3,] 0 0.0 0.3333333
\[ \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 1 & 2 & 3 \\ \end{bmatrix} \]
B <- matrix(data = c(1,2,3,
4,5,6,
1,2,3),
nrow = 3,
ncol = 3,
byrow = TRUE
)
B
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 1 2 3
typeof(B)
## [1] "double"
?det
det(B)
## [1] 0
??inverse
# Use the solve() function to calculate the inverse.
# solve(B)
# Using tryCatch to catch and print the error message
result <- tryCatch({
solve(B)
}, error = function(e) {
cat("Error: ", conditionMessage(e), "\n")
})
## Error: Lapack routine dgesv: system is exactly singular: U[3,3] = 0
\[ \begin{bmatrix} 1 & 2 & 2 \\ -2 & 0.5 & 0 \\ -2 & 5 & 4 \\ \end{bmatrix} \]
You may not be able to visually eyeball that the third row is a
linear combination of the first two rows, with an equal weight on row 1
and row 2. But calculating the determinant identifies this
(i.e. det(C) = 0) and thus you know you will not be able to
calculate the inverse of the matrix C as it should not exist.
solve(C) command gives us an error, as expected.
# Row 1
m11 <- 1
m12 <- 2
m13 <- 2
# Row 2
m21 <- -2
m22 <- .5
m23 <- 0
# Row 3 is a linear combination of Row 1 and Row 2
C <- matrix(data = c(m11,m12,m13,
m21,m22,m23,
2*m11+2*m21,2*m12+2*m22,2*m13+2*m23),
nrow = 3,
ncol = 3,
byrow = TRUE)
C
## [,1] [,2] [,3]
## [1,] 1 2.0 2
## [2,] -2 0.5 0
## [3,] -2 5.0 4
det(C)
## [1] 0
#solve(C)
# Using tryCatch to catch and print the error message
result <- tryCatch({
solve(C)
}, error = function(e) {
cat("Error: ", conditionMessage(e), "\n")
})
## Error: Lapack routine dgesv: system is exactly singular: U[3,3] = 0
You may not be able to visually eyeball that the third row is a
linear combination of the first two rows, with a weight of -2 on row 1
and a weight of 3 on the second row. Seeing the pattern is harder here
than matrix C above. But again calculating the determinant identifies
this (i.e. det(D) = 0) and thus you know you will not be
able to calculate the inverse of the matrix D as it should not exist.
solve(D) command gives us an error, as expected.
\[ \begin{bmatrix} 1 & -6 & -20 \\ 2 & -1 & -7 \\ 3 & -3 & 3 \\ \end{bmatrix} \]
m11 <- 1
m12 <- 2
m13 <- 3
m21 <- -6
m22 <- -1
m23 <- 3
w_row1 <- -2
w_row2 <- 3
D <- matrix(data = c(m11,m12,m13,
m21,m22,m23,
w_row1 * m11 + w_row2 * m21, w_row1 * m12 + w_row2 * m22,w_row1 * m13 + w_row2 * m23),
nrow = 3,
ncol = 3,
byrow = FALSE)
D
## [,1] [,2] [,3]
## [1,] 1 -6 -20
## [2,] 2 -1 -7
## [3,] 3 3 3
det(D)
## [1] 0
#solve(D)
# Using tryCatch to catch and print the error message
result <- tryCatch({
solve(D)
}, error = function(e) {
cat("Error: ", conditionMessage(e), "\n")
})
## Error: Lapack routine dgesv: system is exactly singular: U[3,3] = 0
Determinant of E is not defined if you have NA ,
non-numerical or missing values.
# define columns
e1 <- c(1,3)
e2 <- c(2,NA)
E <- cbind(e1,e2)
E
## e1 e2
## [1,] 1 2
## [2,] 3 NA
det(E)
## [1] NA
Do not replace NA values with 0. Think about how to
impute missing values.
E[is.na(E)] <- 0
det(E)
## [1] -6