-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathblank_notes.Rmd
185 lines (114 loc) · 3.45 KB
/
blank_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
---
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)")
```
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}
```
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}
```
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}
```
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}
```
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}
```
If-statements and Loops
-----------------------
Two kinds of if-statements:
* `if` is the thing to use in most cases
* `ifelse()` is vectorized
Examples:
```{r}
```
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>