The solution can be found here.
In R
, and using just one line of code, create the following matrices and assign them to the variables W
, X
, Y
, and Z
, respectively. \[\begin{align}
\texttt{W} =
\begin{pmatrix}
1 & 4 & 7 \\
2 & 5 & 8 \\
3 & 6 & 9
\end{pmatrix},~
\texttt{X} =
\begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix},~
\texttt{Y} =
\begin{pmatrix}
1 & 0 & 0 \\
0 & 1 & 0 \\
0 & 0 & 1
\end{pmatrix}, \text{ and }
\texttt{Z} =
\begin{pmatrix}
0 & 0 & 1 \\
0 & 1 & 0 \\
1 & 0 & 0
\end{pmatrix}.
\end{align}\] Also, compute \(\texttt{W}\odot\texttt{X}\) and \(\texttt{Y} \cdot \texttt{Z}\), such that \(\odot\) represents the element-wise multiplication and \(\cdot\) matrix multiplication.
Create a matrix \([\texttt{A}]_{3 \times 3}\), such that \(\texttt{a}_{ij} = i + j\), \(\forall i, j\). Also, create \([\texttt{B}]_{3 \times 3}\), such that \(\texttt{b}_{ij} = \texttt{TRUE}\), if \(i + j\) is even, and \(\texttt{b}_{ij} = \texttt{FALSE}\), otherwise. Finally, create \([\texttt{C}]_{3 \times 3}\), such that \(\texttt{c}_{ij} = 1\), if \(i + j \in \{1, 2, 3\}\), and \(\texttt{c}_{ij} = 0\), otherwise.
Write a function for computing \(n!\), for any \(n\) non-negative integer. Print the 10 first numbers from the sequence \((\texttt{f}_n)_n\), such that \(\texttt{f}_n = n!\), for all \(n \in \mathbb{Z}_{+}\). Also, for \(n = 10\), compare your result with the function from (recall that, for any positive integer \(n\), \(\Gamma(n) = (n - 1)!\)).
Create 3 five-element (row) vectors \(\texttt{p}\), \(\texttt{q}\), and \(\texttt{r}\), such that \(p\) contains the first 5 prime numbers, \(q\) the first 5 numbers from the Fibonacci sequence, and \(r\) the 5 first elements of the sequence \((\texttt{r}_n)_n\), such that \(\texttt{r}_n = \lfloor\sqrt{(n \cdot \pi)}\rfloor\), \(\forall n\), and \(\lfloor \cdot \rfloor\) is the floor function. Do NOT compute the terms manually (write functions!), so that you can easily extend it to larger vectors. Also, create the matrices \[\begin{align} \texttt{A} = \begin{pmatrix} \texttt{p} \\ \texttt{q} \\ \texttt{r} \end{pmatrix}, \text{ and } \texttt{B} = \begin{pmatrix} \texttt{p}^{\text{T}} & \texttt{q}^{\text{T}} & \texttt{r}^{\text{T}} \end{pmatrix}. \end{align}\]
Suppose that, for the population of male teenagers, we can model their height as \[\begin{align} \texttt{height}_i = \beta_0 + \beta_1 \cdot \texttt{age}_i + \epsilon_i, \text{ such that } \epsilon_i \sim \text{Normal}(0, \sigma^2_{\epsilon}). \end{align}\] Now, for a random sample of size \(n = 20\), we want to estimate \(\boldsymbol{\beta} = (\beta_0, \beta_1)^{\text{T}}\) as \(\hat{\boldsymbol{\beta}} = (\text{X}^{\text{T}}\text{X})^{-1}\text{X}^{\text{T}}\boldsymbol{y}\), such that \[\begin{align} \text{X} = \begin{pmatrix} 1 & \texttt{age}_1 \\ \vdots & \vdots \\ 1 & \texttt{age}_{20} \end{pmatrix}, \text{ and } \boldsymbol{y} = \begin{pmatrix} \texttt{height}_1 \\ \vdots \\ \texttt{height}_{20} \end{pmatrix}. \end{align}\] Compute \(\hat{\boldsymbol{\beta}}\) and plot \(\hat{\boldsymbol{y}} = \text{X}\hat{\boldsymbol{\beta}}\). For this exercise, generate the data set using the following code
set.seed(999)
beta_0 <- 140
beta_1 <- 2
ages <- sample(x = 12:18, size = 20, replace = TRUE)
heights <- round(x = beta_0 + beta_1 * ages + rnorm(n = 10, mean = 0, sd = 2),
digits = 2)
data <- data.frame(heights = heights, ages = ages)