Repository navigation
Expand file tree
/
Copy pathTutorial_1-R.Rmd
More file actions
169 lines (141 loc) · 5.11 KB
/
Copy pathTutorial_1-R.Rmd
File metadata and controls
169 lines (141 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
---
title: "Applied Statistics with R (STAT 215)"
subtitle: "Tutorial 1: The R Language"
author:
- "Ziling Ma"
- "ziling.ma@kaust.edu.sa"
date: "September 29, 2024"
output:
prettydoc::html_pretty:
theme: leonids
highlight: vignette
toc: true
number_sections: true
---
<!-- <style type="text/css"> -->
<!-- body{ -->
<!-- font-size: 10pt; -->
<!-- } -->
<!-- </style> -->
# Assigning Objects {#Ex1}
a. Create an object that stores the value $32 × \frac{41}{8}$.
b. Overwrite your object in (a) by itself divided by $2.33$. Print the result to the console.
c. Create a new object with the value $−8.2 × 10^{−3}$.
d. Print directly to the console the result of multiplying (b) by (c).
<a href="#Ex1">Back to</a> <a href="#top">top</a>
# Vectors {#Ex2}
a. Create and store a sequence of values from 5 to −11 that progresses in steps of 0.3.
b. Overwrite the object from (a) using the same sequence with the order reversed.
c. Repeat the vector c(-1,3,-5,7,-9) twice, with each element repeated 10 times, and store the result. Display the result sorted from largest to smallest.
d. Extract the first and last elements of your vector from (a), storing them as a new object.
<a href="#Ex1">Back to</a> <a href="#top">top</a>
# Matrix operations {#Ex3}
In `R`, use just one line of code to create the following mtrices and assign them to the variables `W`, and `X`, respectively.
\[
W=\begin{pmatrix}
1 & 4 & 7 \\
2 & 5 & 8 \\
3 & 6 & 9
\end{pmatrix};
X = \begin{pmatrix}
1 & 2 & 3 \\
4 & 5 & 6 \\
7 & 8 & 9
\end{pmatrix};
\]
Also, compute $W\odot X$ and $W\cdot X$, such that $\odot$ represents the element-wise multiplication and $\cdot$ represents matrix multiplication.
Given a matrix Y, where
```{r}
Y <- matrix(c(2,3,1,4),nrow = 2)
Y
```
How can you compute the inverse matrix of Y?
<a href="#Ex1">Back to</a> <a href="#top">top</a>
# Matrix and functions {#Ex4}
a. Create a matrix $A\in\mathbb R^{3\times3}$, such that
\[
a_{ij} = i + j,\quad\forall i,j.
\]
b. Now, we consider a NON-NUMERIC case. Create $B\in \mathbb R^{3\times3}$, where the element is obtained
using the function below and the values in matrix A, such that
\[
b_{ij}=\begin{cases}
\text{TRUE}, & a_{ij}\text{ is even}, \\
\text{FALSE}, &\text{otherwise}.
\end{cases}
\]
c. Finally, create $C\in \mathbb R^{3\times3}$, such that
\[
c_{ij}=\begin{cases}
1, & a_{ij} \in \{1,2,3\}, \\
0, &\text{otherwise}.
\end{cases}
\]
d. Confirm the dimensions of the matrix from (a) are $2 × 3$ if you remove any one row.
<a href="#Ex1">Back to</a> <a href="#top">top</a>
# Recursive functions 1 {#Ex5}
Write a function for computing $f(n)=n!,\ (n\in \mathbb N)$. Print the 10 first numbers from the sequence $f$.
Also, for $n=10$, compare your result with the `gamma()` function from `R` (recall that, for any positive integer $n$, $\Gamma(n) = (n-1)!$).
<a href="#Ex1">Back to</a> <a href="#top">top</a>
# Graph function {#Ex6}
Use the functions `outer` y `persp` to graph the following functions\n\n")
$$f(x, y) = 1 - exp(-1 / (x^2 + y^2)), \text{for}\: x, y ∈ [-5, 5]$$
$$f(x, y) = sin(x)cos(y), \text{for}\: x, y ∈ [-\pi/2, \pi/2]$$
<a href="#Ex1">Back to</a> <a href="#top">top</a>
# Linear regression {#Ex7}
Suppose that we model the height of male teenagers as
\[
\mathrm{height}_i = \beta_0 + \beta_1 \cdot \mathrm{age}_i + \epsilon_i,\ \text{s.t. } \epsilon_i \sim \mathcal N\left(0, \sigma^2\right).
\]
Now, for a random sample of size $n=20$, we want to estimate $\boldsymbol \beta = (\beta_0,\beta_1)^\intercal$ as
\[
\boldsymbol \beta = (X^\intercal X)^{-1} X^{\intercal} \boldsymbol y,
\]
where
\[
X = \begin{pmatrix}
1 & \textrm{age}_1 \\
\vdots & \vdots \\
1 & \textrm{age}_{20}
\end{pmatrix},\quad
\boldsymbol y = \begin{pmatrix}
\textrm{height}_1 \\
\vdots\\
\textrm{height}_{20}
\end{pmatrix}.
\]
Compute $\hat {\boldsymbol \beta}$ and plot $\hat {\boldsymbol y} =X\hat {\boldsymbol \beta}$. For this exercise, generate the data set using the following code:
```{r}
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)
```
<a href="#Ex1">Back to</a> <a href="#top">top</a>
# `R Markdown` and `R Sweave`
* Very elegent and powerful tools to use with `Rstudio`.
* Work very well for homework, reports, presentations, etc.
* `R Markdown` is mainly for generating `.html` files though it can also generate `.pdf` files and many more (this file is generated by `R Markdown`).
* `R Sweave` is like $\LaTeX$ but with `R` chunks within. It generates standard `.pdf` files.
# A fun exercise if you would like to try (Recursive functions)
Create 3 `N`-element row vectors `p`, `q`, and `r`:
\[
\begin{cases}
\mathtt{p}: &\text{prime number sequence},\\
\mathtt{q}: &\text{Fibonacci sequence},\\
\mathtt{r}: &r_n=\lfloor\sqrt{n\pi}\rfloor.
\end{cases}
\]
Do NOT compute the terms manually (write functions!).
Also, create matrices:
\[
A=\begin{pmatrix}
\mathtt p \\ \mathtt q \\ \mathtt r
\end{pmatrix},
B=\begin{pmatrix}
\mathtt p^\intercal & \mathtt q^\intercal & \mathtt r^\intercal
\end{pmatrix}
\]