library(tidyverse)
library(ggplot2)BIOS 507 – Homework 1
Background
Problem 1
How many rows does the matrix have?
Answer: 2 rowsHow many columns does the matrix have?
Answer: 3 columnsIf it exists, what is element (2,1)?
Answer: 0.23If it exists, what is element (1,3)?
Answer: 0.03If it exists, what is element (3,2)?
Answer: Element (3,2) does not existInput the matrix in R (in your attached code)
matrix_q6 <- matrix(c(1.9, 0.23, -0.07,
2.04, 0.03, 2.22), nrow = 2, ncol = 3)- Print the matrix in R (in your attached code)
print(matrix_q6) [,1] [,2] [,3]
[1,] 1.90 -0.07 0.03
[2,] 0.23 2.04 2.22
- Print the transpose of the matrix in R (in your attached code)
transposed_matrix_q6 <- t(matrix_q6)
print(transposed_matrix_q6) [,1] [,2]
[1,] 1.90 0.23
[2,] -0.07 2.04
[3,] 0.03 2.22
- What are the dimensions of the transposed matrix?
Answer: (3 x 2)
(3 rows x 2 columns)
Problem 2
#Load matrices
x <- matrix(c(1, -1,
2, 3,
3, 0), nrow = 3, ncol = 2)
y <- matrix(c(-4, 1, 9,
-2, 3, 0), nrow = 2, ncol = 3)
transposed_x <- t(x)
#Check matrices were entered correctly
print(x) [,1] [,2]
[1,] 1 3
[2,] -1 3
[3,] 2 0
print(y) [,1] [,2] [,3]
[1,] -4 9 3
[2,] 1 -2 0
print(transposed_x) [,1] [,2] [,3]
[1,] 1 -1 2
[2,] 3 3 0
What is the dimension of X?
Answer: (3 x 2)
(3 rows x 2 columns)What is the dimension of Y?
Answer: (2 x 3)
(2 rows x 3 columns)Is it possible to calculate X′X? If so, what is the resulting matrix? Do the calculation by hand and separately verify your work using R (in attached code)
Answer: Yes, it is possible to calculate X′X. Matrices X′ and X are compatible.
X′ = (2 x 3)
X = (3 x 2)
X′X = (2 x 2)
Calculate by hand:
Element (1,1) = 1*1 + -1*(-1) + 2*2 = 1 + 1 + 4 = 6
Element (1,2) = 1*3 + -1*3 + 2*0 = 3 - 3 + 0
Element (2,1) = 3*1 + 3*(-1) + 0*0 = 3 - 3 + 0 = 0
Element (2,2) = 3*3 + 3*3 + 0*0 = 9 + 9 + 0 = 18
matrix_xtx_manual = matrix(c(6, 0,
0, 18), nrow = 2, ncol = 2)
print(matrix_xtx_manual) [,1] [,2]
[1,] 6 0
[2,] 0 18
Verify with R:
#Multiply matrices
matrix_xtx_r = transposed_x %*% x
#Print product matrix
print(matrix_xtx_r) [,1] [,2]
[1,] 6 0
[2,] 0 18
- Is it possible to calculate X′Y? If so, what is the resulting matrix? Do the calculation by hand and separately verify your work using R (in attached code).
Answer: No, it is not possible to calculate X′Y. Matrices X′ and Y are both (2 x 3) matrices and are not compatible (inner dimensions do not match).
- Is it possible to calculate XY? If so, what is the resulting matrix? Do the calculation by hand and separately verify your work using R (in attached code).
Answer: Yes, it is possible to calculate XY. Matrices X and Y are compatible.
X = (3 x 2)
Y = (2 x 3)
XY = (3 x 3)
Calculate by hand:
Element (1,1) = 1*(-4) + 3*1 = -4 + 3 = -1
Element (1,2) = 1*9 + 3*(-2) = 9-6 = 3
Element (1,3) = 1*3 + 3*0 = 3 + 0 = 3
Element (2,1) = -1*(-4) + 3*1 = 4+3 = 7
Element (2,2) = -1*(9) + 3*(-2) = -9 - 6 = -15
Element (2,3) = -1*3 + 3*0 = -3 + 0 = -3
Element (3,1) = 2*(-4) + 0*1 = -8 + 0 = -8
Element (3,2) = 2*9 + 0*(-2) = 18 + 0 = 18
Element (3,3) = 2*3 + 0*0 = 6 + 0 = 6
matrix_xy_manual = matrix(c(-1, 7, -8,
3, -15, 18,
3, -3, 6), nrow = 3, ncol = 3)
print(matrix_xy_manual) [,1] [,2] [,3]
[1,] -1 3 3
[2,] 7 -15 -3
[3,] -8 18 6
Verify with R:
#Multiply matrices
matrix_xy_r = x %*% y
#Print product matrix
print(matrix_xy_r) [,1] [,2] [,3]
[1,] -1 3 3
[2,] 7 -15 -3
[3,] -8 18 6