For your homework report this week, you do not need to submit solutions to the swirl lessons and matrix exercises 1-6. For exercises 1-6, you should say which parts you were able to compute by hand and check successfully using R. You do need to submit a solution to exercise 7. As usual, you also need to make Sources and Please explain statements.
Continuing from Homework 1, complete the following swirl modules: Lesson 5: Missing Values
, Lesson 6: Subsetting Vectors
, Lesson 7: Matrices and Data Frames
, Lesson 9: Functions
. We will not need the material in Lesson 8: Logic
, but you can do this too if you like. You will have a chance to ask swirl questions in lab (on 1/11 or 1/12) if difficulties arise.
It is good to do at least one example of each of the fundamental matrix operations by hand, even though in practice we almost always use a computer. For the homework, you are required to carry out and check exactly one example of each operation, but the code provided can be used to make more examples until you are comfortable with carrying out all of them.
We will generates exercises using the following function:
randomMatrix <- function(p,q,values=-4:4){
matrix(sample(values,size=p*q,replace=TRUE),p,q)
}
This produces a \(p\times q\) matrix with random entries which are, by default, drawn with replacement from the sequence \(-4,-3,\dots,0,\dots,3,4\). Let’s use randomMatrix
to produce two \(2\times 2\) matrices \({\mathbb{A}}\) and \({\mathbb{B}}\), setting the seed of the random number generator so that the function returns the same matrix each time
set.seed(1)
A <- randomMatrix(2,2)
B <- randomMatrix(2,2)
A
## [,1] [,2]
## [1,] -2 1
## [2,] -1 4
B
## [,1] [,2]
## [1,] -3 4
## [2,] 4 1
Then check your calculation using R.
Then check your calculation using R.
Then check your calculation using R.
set.seed(2)
C <- randomMatrix(2,3)
D <- randomMatrix(3,2)
Then check your calculation using R.
Then check your calculation using R. Generate more examples using randomMatrix()
until you are confident about inverting \(2\times 2\) matrices by hand.
Then check your calculation using R.
License: This material is provided under an MIT license