Consider the vectors
HDDA Tutorial: MatrixBasics : Solutions
Department of Econometrics and Business Statistics, Monash University Tutorial 7
1. X+Y 2. XY
Copyright By PowCoder代写 加微信 powcoder
Solutions provided in section using R
Vectors and Matrices in R
2 1 −2 a=4b=0c=1
Work out the following (without using R). All multiplication is matrix multiplication.
1. a+b 2. a′a 3. a′b 4. a′c
Solutions provided in section using R
Consider the matrices
Work out the following (without using R). All multiplication is matrix multiplication.
12 2−1 X=1 4 Y=3 0
Repeat all above questions using R. Useful functions are c for setting a vector, matrix for setting a matrix and t for the transpose. Also note that * does NOT do matrix multiplication. Instead use %*%.
a<-c(2,4) b<-c(1,0) c<-c(-2,1)
## [1] 3 4
## [1,] ## [2,] ##[3,]
## ##[1,] ## [2,] ##[3,]
## [1,] ## [2,] ##[3,]
#Make sure you set byrow=T
X<-matrix(c(1,2, 1,4,
0,-1),3,2,byrow = T)
Y<-matrix(c(2,-1, 3,0,
3,-1),3,2,byrow = T)
# X%*%Y This is non-conformable
##[1,] 5 -1 ## [2,] 13 -1
Data matrix
Consider the data matrix Y
where yij is the value of variable j for observation i 1. How many rows are there in Y?
There are n rows, i.e. each observation is a row
2. How many columns are there in Y?
There are p columns, i.e. each variable is a column
3. What are the dimensions of Y? The matrix is an n × p matrix
4. Find an expression for the first row and first column of S =
The element on the first row and first column is found by multiplying y11
yn1 by its transpose.
y11 y12 ... y1p y21 y22 ... y2p
Y= . . ......
. yn1 yn2 ... ynp
This is the same as y2 +y2 +...+y2 . Putting everything together it is 1 y2 . If the data have mean
1121 n1 n−1i1 i=1
zero, then this is the sample variance of the first variable.