STSA 3732: Tutorial 2 (MEMO) : Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C
STSA 3732: Tutorial 2 (MEMO) : Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C Matrix C
Question 1
1.1 Create the appropriate data objects, either a vector or matrix, for the following in R:
1.2 Calculate C + D.
C+D
## [,1] [,2]
## [1,] 3 5
## [2,] 2 3
## [3,] 9 8
t(C)
t(d)
1.5 Calculate b ′D. (Hint: use * for element-wise multiplication and %*% for matrix multiplication.)
1
t(b)%*%D
## [,1] [,2]
## [1,] 13 29
t(C)%*%D
## [,1] [,2]
## [1,] 15 39
## [2,] 9 24
4*t(d)%*%t(Y)
## [,1] [,2]
## [1,] 280 232
t(Y)%*%Z
solve(A)%*%X
## [,1] [,2]
## [1,] -0.2972973 1.2432432
## [2,] -0.9945946 0.8864865
Question 2
2.1 Create the following matrices in R:
1 6 6 1
1 7 7 1
A = 1 8 , B = 8 1 .
1 9 9 1
1 10 10 1
2
A <- matrix(c(1,1,1,1,1,6,7,8,9,10), ncol=2)
B <- matrix(c(6,7,8,9,10,1,1,1,1,1), ncol=2)
Aletrnatively:
2.2 Calculate A + B.
A+B
## [,1] [,2]
## [1,] 7 7
## [2,] 8 8
## [3,] 9 9
## [4,] 10 10
## [5,] 11 11
t(A)
solve(t(B)%*%B)
## [,1] [,2]
## [1,] 0.1 -0.8
## [2,] -0.8 6.6
Question 3
3.1 Create appropriate data objects for the following in R. Do not type the values out or copy them in any
way. Use functions to generate these values.
3
600
580
560
1 1 6 1 1 1
540
1 2 5 32 4 8
520
1 3 4 243 9 27
w = 500 , Y = , Z = .
1 4 3 1024 16 64
480
1 5 2 3125 25 125
460
1 6 1 7776 36 216
440
420
400
2*w+30
## [1] 1230 1190 1150 1110 1070 1030 990 950 910 870 830
3.3 Determine the transpose of the matrix Z and store the second row of the transposed matrix in a vector
named x .
x <- t(Z)[2,]
x
## [1] 1 4 9 16 25 36
solve(t(Z)%*%Z)
Question 4
4.1 Create a matrix with the values 1, 5, 2 and −3 in the first column, 1, 2, 3 and 7 in the second column, 3,
6, 7 and 5 in the third column and 6, 8, 11 and −7 in the fourth column.
4
Q6.1 <- matrix(c(1,5,2,-3,1,2,3,7,3,6,7,5,6,8,11,-7), ncol=4)
4.2 Replace the fourth column of the matrix above in (6.1) by the sum of the first and third columns.
4.4 Determine the mean of the third column of the matrix created in (6.3).
mean(Q6.3[,3])
## [1] 5.25
Question 5
Consider the following data set containing 5 observations for each of the variables named X1, X2 and X3:
X1 X2 X3
10 21 15
9 27 12
7 22 12
11 24 10
15 13 11
5.1 Create a data frame named my.data containing the data shown in the table above.
5
## X1 X2 X3
## 1 10 21 15
## 2 9 27 12
## 3 7 22 12
## 4 11 24 10
## 5 15 13 11
attach(my.data)
sum(X1)
## [1] 52
sd(X2)
## [1] 5.22494
sum(X1,X3)
## [1] 112
Alternative solution:
sum(X1+X3)
## [1] 112