set.seed(2025)
### YOUR ANSWER HERE
Problem set 8
You are not allowed to load any package or use for-loop. For exercises 1 and 3-6 you only get to write one line of code for the solution.
For better preparation for midterm, we recommend not using chatGPT for this homework.
- Create a 100 by 10 matrix of randomly generated standard normal numbers. Put the result in
x
. Show the subset ofx
defined by the first 5 rows and the first 4 columns.
- Apply the three R functions that give you the dimension of
x
, the number of rows ofx
, and the number of columns ofx
, respectively. Print the responses.
### YOUR ANSWER HERE
- Generate matrix
y
obtained from adding the scalar 1 to row 1, the scalar 2 to row 2, and so on, to the matrixx
. Show the subset ofy
defined by the first 5 rows and the first 4 columns.
### YOUR ANSWER HERE
- Generate matrix
z
obtained from adding the scalar 2 to column 1, the scalar 4 to column 2, and so on, to the matrixy
. Hint: Usesweep
withFUN = "+"
. Show the subset ofz
defined by the first 5 rows and the first 4 columns.
### YOUR ANSWER HERE
- Compute the average of each row of
z
. Show the first 10 elements
### YOUR ANSWER HERE
- Use matrix multiplication to compute the average of each column of
z
and store in a single row matrix. Hint define a \(1\times n\) matrix \((1/n, \dots, 1/n)\) with \(n\) thenrow(z)
. Show the first 10 elements
### YOUR ANSWER HERE
- Use matrix multiplication and other matrix / vector operations to compute the standard deviation of each column of z. Do not use
sweep
orapply
. Print the results. For this exercise, you must only use the following operations:t, -, %*%, *, /,
andas.vector
### YOUR ANSWER HERE
- For each digit in the MNIST training data, compute and print the overall proportion of pixels that are in a grey area, defined as values between 50 and 205, inclusive. Hint: use the
read_mnist
function from thedslabs
package.
### YOUR ANSWER HERE
- Compute and print the average grey proportion by digit class. Hint: Use logical operators and
sapply
.
### YOUR ANSWER HERE
- Make a box plot of grey proportion by digit class. Each point on the boxplot should represent one training image. Hint: Use logical operators and
rowMeans
.
### YOUR ANSWER HERE
- Use the function
solve
to solve the following system of equations. Hint: use the functionsolve
. Show the solution.
\[ \begin{align} x+2y−2z &=−15\\ 2x+y−5z&=−21\\ x−4y+z&=18 \end{align} \]
### YOUR ANSWER HERE