BIOS 507 – Homework 1

Author

Elaina Sinclair

Published

January 21, 2026

Background

library(tidyverse)
library(ggplot2)

Problem 1

  1. How many rows does the matrix have?
    Answer: 2 rows

  2. How many columns does the matrix have?
    Answer: 3 columns

  3. If it exists, what is element (2,1)?
    Answer: 0.23

  4. If it exists, what is element (1,3)?
    Answer: 0.03

  5. If it exists, what is element (3,2)?
    Answer: Element (3,2) does not exist

  6. Input 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)
  1. 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
  1. 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
  1. 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
  1. What is the dimension of X?
    Answer: (3 x 2)
    (3 rows x 2 columns)

  2. What is the dimension of Y?
    Answer: (2 x 3)
    (2 rows x 3 columns)

  3. 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
  1. 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).

  1. 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