ST430 Introduction to Regression Analysis
ST430: Introduction to Regression Analysis, Appendix B:
Matrix Algebra in R
Luo Xiao
October 7, 2015
1 / 12
ST430 Introduction to Regression Analysis
To create an identity matrix of dimensions k × k, use R code ’diag(k)’
R example:
I = diag(3) #create an identity matrix
I #display the matrix
##
[,1] [,2] [,3]
## [1,]
1
0
0
## [2,]
0
1
0
## [3,]
0
0
1
2 / 12
ST430 Introduction to Regression Analysis
To create a diagonal matrix of dimensions k × k, use R code ’ diag(c)’,
where ’c’ is the vector of elements on the diagonal.
R code for creating a 3 × 3 matrix
2 0
0
0 :
0 4
0 0 −3
d = c(2,4,-3) # a vector of diagonal elements
A = diag(d) # create a diagonal matrix
A #display the matrix
##
[,1] [,2] [,3]
## [1,]
2
0
0
## [2,]
0
4
0
## [3,]
0
0
-3
3 / 12
ST430 Introduction to Regression Analysis
To create a general matrix, e.g., a 3 × 3 matrix as below,
2 1 −1
2 ,
3 4
2 0 −3
the R code can be as follows:
d = c(2,3,2,1,4,0,-1,2,-3) #a vector of all elements
A = matrix(d,nrow=3,ncol=3) #create a matrix
A #display the matrix
##
[,1] [,2] [,3]
## [1,]
2
1
-1
## [2,]
3
4
2
## [3,]
2
0
-3
4 / 12
ST430 Introduction to Regression Analysis
Matrix operations in R (output on next slide)
d = c(2,3,2,1,4,0,-1,2,-3)
A = matrix(d,nrow=3,ncol=3) #create a 3 by 3 matrix A
f = c(1,2,3,4,5,6)
B = matrix(f,nrow=3,ncol=2,byrow=TRUE)
#create a 3 by 2 matrix B by rows
dim(A) #display dimensions of A
dim(B) #display dimensions of B
A[2,1] #the element at 2nd row and 1st column
A #display the matrix
5 / 12
ST430 Introduction to Regression Analysis
## [1] 3 3
## [1] 3 2
## [1] 3
##
[,1] [,2] [,3]
## [1,]
2
1
-1
## [2,]
3
4
2
## [3,]
2
0
-3
6 / 12
ST430 Introduction to Regression Analysis
Matrix operations in R (output on next slide)
d = c(2,3,2,1,4,0,-1,2,-3)
A = matrix(d,nrow=3,ncol=3) #create a 3 by 3 matrix A
f = c(1,2,3,4,5,6)
B = matrix(f,nrow=3,ncol=2, byrow=TRUE)
#create a 3 by 2 matrix B
A%*%B #matrix multiplication
t(A) #matrix transpose
solve(A) #matrix inverse
7 / 12
ST430 Introduction to Regression Analysis
##
[,1] [,2]
## [1,]
0
2
## [2,]
25
34
## [3,] -13 -14
##
[,1] [,2] [,3]
## [1,]
2
3
2
## [2,]
1
4
0
## [3,]
-1
2
-3
##
[,1]
[,2]
[,3]
## [1,] 4.000000 -1.0000000 -2.000000
## [2,] -4.333333 1.3333333 2.333333
## [3,] 2.666667 -0.6666667 -1.666667
8 / 12
ST430 Introduction to Regression Analysis
Simple linear regression using matrix
See the R output (output1.txt)
x = c(1,2,3,4,5)
y = c(1,1,2,2,4)
fit = lm(y~x)
summary(fit)
9 / 12
ST430 Introduction to Regression Analysis
Linear regression using matrix (output on next slide)
x = c(1,2,3,4,5)
y = c(1,1,2,2,4)
X = cbind(rep(1,5),x) #create the 5 by 2 matrix X
A = t(X)%*%X
B = solve(A)
C = t(X)%*%y
beta = B%*%C
beta #estimates of beta
SSE = t(y-X%*%beta)%*%(y-X%*%beta)
s = sqrt(SSE/(5-2))
s #root MSE
s*sqrt(B[1,1]) #standard error for intercept
s*sqrt(B[2,2]) #standard error for slope
10 / 12
ST430 Introduction to Regression Analysis
##
[,1]
##
-0.1
## x 0.7
##
[,1]
## [1,] 0.6055301
##
[,1]
## [1,] 0.6350853
##
[,1]
## [1,] 0.1914854
11 / 12
ST430 Introduction to Regression Analysis
Useful links for Matrix Algebra in R
\href{http:
//www.ats.ucla.edu/stat/r/library/matrix_alg.html}
\href{http://www.johnmyleswhite.com/notebook/2009/12/16/
quick-review-of-matrix-algebra-in-r}
\href{http://www.statpower.net/Content/MLRM/Lecture%
20Slides/RMatrixIntro.pdf}
12 / 12
© Copyright 2026 Paperzz