-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotes.Rmd
272 lines (184 loc) · 4.88 KB
/
notes.Rmd
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
---
title: "Stat 33B - Lecture 10"
date: April 1, 2020
output: pdf_document
---
Review: Homework 4
==================
Suppose we want to sample from the distribution on -1 to 1 shown in the plot
produced by this code:
```{r, dslide}
dslide = function(x) {
ifelse(x > 1, 0,
ifelse(x > 0, dnorm(x) / dnorm(0), dunif(x, -1, 0))
)
}
curve(dslide, -2, 2, xlab = "Value", ylab = "Density (unscaled)")
curve(dunif(x, -1, 1), add = TRUE)
```
The exact steps in rejection sampling are:
1. Sample an x coordinate.
2. Sample a y coordinate.
3. Test whether the y value falls below the target distribution's density
curve. If it does, then x is a new sample value. If it does not, then x is
rejected.
4. Repeat steps 1-3 until reaching the desired number of sample values.
```{r}
rslide = function(n = 100) {
accepted = 0
iterations = 0
results = numeric(n)
while (accepted < n) {
iterations = iterations + 1
x = runif(1, -1, 1)
y = runif(1, 0, 1.2)
if (y < dslide(x)) {
# Accepted
accepted = accepted + 1
results[accepted] = x
}
}
list(results, acceptance = accepted / iterations)
}
set.seed(53)
samp = rslide(200000)
samp
plot(density(samp))
repeat {
x = runif(1, -1, 1)
y = runif(1, 0, 1.2)
if (y < dslide(x)) {
# Accepted
accepted = accepted + 1
results[accepted] = x
if (accepeted == n)
break
}
}
```
Review: STAT 33B So Far
=======================
Topics:
* Vectors, data frames, and lists
* Types and classes
* Taking subsets with `[`, `[[`, `$`, and `subset()`
* ggplot2
* Tidy data and tidyr
* Relational data and `merge()`
* If-statements and loops
* Writing functions
* Scoping and environments
See the video lectures for a review of the last two.
Types and Classes
-----------------
Types describe how an object is stored in memory.
Classes describe how an object behaves. Objects may have more than one.
Common types and classes:
```{r}
typeof(4)
typeof("hi")
typeof(3+4i)
typeof(sin)
typeof(iris)
class(iris)
class(5)
class(TRUE)
typeof(TRUE)
```
Taking Subsets
--------------
Use `[` to get one or more elements. Keeps the container.
Use `[[` to get exactly one element. Drops the container.
Use `$` to get columns or list elements by name. Drops the container.
Examples:
```{r}
x = c(5, 3, 1.2)
x
x[c(1, 1, 2)]
x[[1]]
x[1]
mylist = list(a = 1:3, b = letters)
class(mylist[1])
class(mylist[[1]])
mylist["a"]
mylist[c(TRUE, FALSE)]
```
ggplot2
-------
Build up the plot in layers. Create layers with functions, add layers with `+`.
Fundamental layers are:
1. Data with `ggplot()`.
2. Geometry with `geom_` functions.
3. Aesthetics with `aes()`. Goes inside data or geometry layer.
Other layers described in the docs allow further customization.
Examples:
```{r}
library(ggplot2)
ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point()
```
Tidy Data
---------
Tidy data are tabular data that satisfy 3 properties:
1. Each row is corresponds to one observation.
2. Each column corresponds to one covariate.
3. Each cell contains only one value.
Most common problems with data:
* Observations split across multiple rows. Fix with `pivot_wider()`.
* Multiple observations combined into a single row. Fix with `pivot_longer()`.
Relational Data
---------------
Relational data are data split across multiple related tables. Tables are
linked by "key" columns.
Often we need to "join" tables by matching rows using the key columns. The
`merge()` function joins tables.
Several kinds of joins:
* Inner join (default) keeps only matching rows.
* Left join (`all.x = TRUE`) keeps all rows in left table, matching rows in
right table.
* Right join (`all.y = TRUE`) keeps all rows in right table, matching rows in
left table.
* Full join (`all = TRUE`) keeps all rows in both tables.
Example:
```{r}
titles = readRDS("data/imdb/titles10s.rds")
cast = readRDS("data/imdb/cast10s.rds")
people = readRDS("data/imdb/people10s.rds")
lb = titles[titles$primaryTitle == "Lady Bird", ]
lb_cast = merge(lb, cast, all.x = TRUE)
merge(lb_cast, people, all.x = TRUE)
```
If-statements and Loops
-----------------------
Two kinds of if-statements:
* `if` is the thing to use in most cases
* `ifelse()` is vectorized
Examples:
```{r}
x = 10
if (x < 20) {
message("Hello")
} else {
message("Bye")
}
x = c(1, 2, 3, 4)
ifelse(x < 3, c(-1, -2, -3, -4), 10)
# Alternative:
x[x < 3] = c(-1, -2, -3, -4)[x < 3]
x[x >= 3] = 10
```
Four kinds of loops:
* Vectorization
* Apply functions
+ Use apply functions if you know the number of iterations and each
iteration is independent
* `for`, `while`, `repeat`
+ Use `for` if you know the number of **iterations**
* Use `while` or `repeat` if you don't know the number of iterations
* Recursion
Examples:
```{r}
```
R Gotchas
=========
Many of R's gotchas are listed in The R Inferno:
<https://www.burns-stat.com/pages/Tutor/R_inferno.pdf>