-
Notifications
You must be signed in to change notification settings - Fork 3
/
Outline.html
459 lines (367 loc) · 10.4 KB
/
Outline.html
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<!DOCTYPE html>
<html>
<head>
<title>R Programming - Data Types, Preallocation, Concatenation, Vectorization</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body { font-family: 'Droid Serif'; }
h1, h2, h3 {
font-family: 'Yanone Kaffeesatz';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
# R Programming - Data Types, Preallocation, Concatenation, Vectorization
### Duncan Temple Lang
<div style="clear: both"/>
<!-- <hr width="50%"/> -->
---
# Questions on Assignment 5
+ Your questions
---
# R Fundamentals
## Vector, List, data.frame
+ vector/list - ordered collection of elements
<pre>
________________________________
| x1 | x2 | x3 | x4 | x5 | ... |
________________________________
</pre>
+ indexed by position
+ can have names on the elements
<pre>
__________________________________
| a | b | cd | ef | xyz | ....
__________________________________
__________________________________
| x1 | x2 | x3 | x4 | x5 | ... |
__________________________________
</pre>
---
# Vectors
+ homogeneous types for each element
+ when we combine vectors of different types, R coerces to "lowest" common type
+ logical `<` integer `<` numeric `<` character `<` complex
```r
c(TRUE, FALSE, TRUE) # logical
c(TRUE, 1L) # integer
c(TRUE, 1L, 3.1415) # numeric
c(TRUE, 1L, 3.1415, "text") # character
```
---
# List
+ list - vector - ordered collection
+ but allows each element to have a different type
```
c(1, "text") # a character vector with two elements, "1" and "text"
list(1, "text") # list with two elements of class numeric and character
```
```
c(1:2, 3:10) # an integer vector
list(1:2, 3:10) # a list with 2 elements each of length 2 and 8
```
---
# `[` and `[[`
---
# Subsetting
1. index/position
1. negative index/position - omit
1. names of elements
1. logical
1.
---
# *apply()
+ lapply() - always returns a list.
+ sapply() - simplify after lapply().
+ vapply() - vector lapply() - specify expected type of each element.
+ tapply() - table lapply() - group by. Simplfies.
+ mapply() - SIMPLIFY = TRUE/FALSE
---
# data.frame
+ list of columns
+ each column has to have the same number of elements
+ 2-D structure - rows (observations) and columns (variables)
+ each column can have a different type
+ logical, integer, numeric, character,
+ factor ?
+ list
+ Column - list with each element a matrix of different dimension
```r
d = data.frame(a = 2*1:10, y = rnorm(10))
d$matrix = lapply(d$a, function(n) matrix(rnorm(n*n), n, n))
sapply(d, class)
sapply(d$matrix, class)
sapply(d$matrix, nrow)
```
---
# Associative Arrays
+ named elements - ignore index/position
```
x = list()
x$a = 1
x$xyz = "text"
```
+ hash tables - fast named lookups
+ environments
+ not important for almost all work.
---
# Making Code Better/Efficient
#### [riverdist package](https://github.com/mbtyers/riverdist)
+ making faster in different ways
+ debugging to access values
+ whoconnected() function
+ removemicrosegs()
---
# `removemicrosegs()` Function
### Preallocation
+ Consider the first 5 lines of the removemicrosegs() function
```r
displacement <- NA
for(i in 1:length(rivers$lines)) {
dim.i <- dim(rivers$lines[[i]])[1]
displacement[i] <- pdist(rivers$lines[[i]][1,], rivers$lines[[i]][dim.i,])
}
```
+ Can think about the code "statically"
+ Or debug and watch computations
```
debug(removemicrosegs)
rivers = readRDS("rivers.rds")
z = removemicrosegs(rivers)
```
---
# Don't Repeat Yourself - DRY
+ 4 references to `rivers$lines[[i]]`
+ repetition of same computation
+ Simplify
```r
displacement <- numeric() # or 0 to make numeric()
for(i in 1:length(rivers$lines)) {
r = rivers$lines[[i]]
displacement[i] <- pdist(r[1,], r[ nrow(r), ])
}
```
---
# Preallocation - `removemicrosegs()`
+ Appending to displacement vector one element at a time.
+ displacement is a logical vector of length 1
+ `displacement[i] <- ...`
+ inserting into position 1, 2, 3, ....
+ each time, R has to grow displacement by one element.
+ potentially has to
+ create new vector with n + 1 elements
+ copy all existing n elements to new vector
+ insert new element into n + 1
+ clean up old vector
---
# Simpler & much more efficient code
```r
displacement <- sapply(rivers$lines,
function(r)
pdist(r[1,], r[nrow(r),]))
```
---
# Redundant/Unnecessary Computations
### `whoconnected()`
```r
whoconnected <- function(seg,rivers) {
connections1 <- !is.na(rivers$connections)
connected <- which(connections1[seg,])
return(connected)
}
```
+ rivers$connections is a matrix
+ is.na(rivers$connections) returns a matrix of TRUE/FALSE values.
+ same dimensions as rivers$connections.
+ vectorized function
+ `connections1[seg, ]` - subset a row of result
+ which() gives integer vector of indices
---
# Simplify
+ No need to assign last computation to variable and then explicitly `return()` it
```r
whoconnected <- function(seg, rivers) {
connections1 <- !is.na(rivers$connections)
which(connections1[seg,])
}
```
---
# Significant Improvement
+ Real issue is
+ why compute the NA values of all elements in matrix AND then subset row.
+ Instead, change the order
+ subset the row
+ compute the non-NA indices
```
tmp = rivers$connections[seg, ]
which(!is.na(tmp))
```
+ Avoids very large number of redundant computations - each call!
+ Huge speed-up.
---
# For loops versus **Vectorization**
+ `removemicrosegs()`
```r
for(j in problems) {
connectedto <- whoconnected(seg = j, rivers = rivers)
for(jj in connectedto) {
for(jjj in connectedto) {
if(jj != jjj && !any(whoconnected( jj, rivers) == jjj)) {
rivers <- connectsegs(connect = jj, connectto = jjj, rivers = rivers,
calcconnections = FALSE)
}
}
}
}
```
+ rivers$connnections - 7784 x 7784 matrix
+ number of iterations 471,637,666,304
+ 4 x 10<sup>11</sup> `>` ~ 400 billion
---
# Improvements
1. `!any(whoconnected(jj, rivers) == jjj)`
+ really is `is.na(rivers$connections[jj, jjj])`
+ many redundant computations
---
# Vectorize & Remove nested loops - jj and jjj
1. For each pair of indices in `connectedto` (jj and jjj)
+ see if `rivers$connections[jj, jjj]` is `NA`
+ one pair at a time
1. Vectorize - compute `rivers$connections[ IndexMatrix2D ]`
+ all pairs in one call
+ IndexMatrix2D needs to be 2 column matrix `[ jj, jjj]`
1. Create IndexMatrix2D
```r
n = length(connectedto)
gr = cbind(rep(connectedto, n), rep(connectedto, each = n))
# drop the rows where the two values are the same.
gr = gr[ - (((1:n) -1 )*n + 1:n), ]
```
1. Compute rows of pairs of indices not connected
```r
w = is.na(rivers$connections[ gr ])
gr[w,]
```
---
# Alternative Way to Create 2-D Matrix
```r
gr = as.matrix(expand.grid(connectedto, connectedto))
```
---
# Assignment 5
+ Potentially long run times.
+ Start early.
+ Don't recompute timings each time you knit.
+ Compute the timings in a script
+ make reproducible.
+ saveRDS() timings
+ In .Rmd file, readRDS() and use from there.
+ Make certain timings are correct and up-to-date.
+ i.e., synchronized with actual code.
---
# Preallocation
Look at the code for URLdecode
+ Where is it concatenating to a vector?
+ How can we avoid doing this?
```r
function (URL)
{
x <- charToRaw(URL)
pc <- charToRaw("%")
out <- raw(0L)
i <- 1L
while (i <= length(x)) {
if (x[i] != pc) {
out <- c(out, x[i])
i <- i + 1L
} else {
y <- as.integer(x[i + 1L:2L])
y[y > 96L] <- y[y > 96L] - 32L
y[y > 57L] <- y[y > 57L] - 7L
y <- sum((y - 48L) * c(16L, 1L))
out <- c(out, as.raw(as.character(y)))
i <- i + 3L
}
}
rawToChar(out)
}
```
---
# Vectorizing
+ Two possible approaches
1. Totally different approach
+ regular expressions?
2. Or can we vectorize the existing code in URLdecode()
+ Avoid while() loop altogether.
---
# Run Times
+ Generate sample URL-encoded strings
+ lengths ranging from 1 to 200K
<ol>
<li>
<a href="utilsURLDecodeCompTimeFit.png" target="_blank">Run times for utils::URLdecode() 1 - 200K</a>
</li>
<li>
<a href="utilsURLDecodeCompTimeFitExtrapolation.png" target="_blank">Run times utils::URLdecode() extrapolated to 600K</a>
</li>
<li>
<a href="Runtimes2.png" target="_blank">Run times for 3 Implementations - 1 to 200K</a>
</li>
<li>
<a href="Runtimes.png" target="_blank">Run times for 3 Implementations - 1 to 600K</a>
</li>
<li>
<a href="Runtime3.png" target="_blank">Run times for Preallocated & Vector Implementations - 1 to 200K</a>
</li>
</ol>
---
# `utils::URLdecode()`
## Computational Time
+ Quadratic Fit
<div style="xxfloat:right">
<a href="utilsURLDecodeCompTimeFit.png" target="_blank">
<img src="utilsURLDecodeCompTimeFit2.png" width=400 height=400></img>
</a>
</div>
---
# Run-time `utils::URLdecode()`
## Extrapolation to 600K Input Size
<a href="utilsURLDecodeCompTimeFitExtrapolation.png" target="_blank">
<img src="utilsURLDecodeCompTimeFitExtrapolation.png" width=450 height=450></img>
</a>
---
## Run-times for 3 Implementations
+ Input size 1 - 200K
<a href="Runtimes2.png" target="_blank">
<img src=Runtimes2.png width=450 height=450></img>
</a>
---
## Run-times for 3 Implementations
+ Full range of inputs 1 - 600K
<a href="Runtimes.png" target="_blank">
<img src=Runtimes.png width=450 height=450></img>
</a>
---
## Run times for Preallocated & Vectorized Implementatins
+ Range of inuts 1 - 200K
<a href="Runtime3.png" target="_blank">
<img src=Runtime3.png width=450 height=450></img>
</a>
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js">
</script>
<script src="remark-toc/index.js"> </script>
<script>
var slideshow = remark.create();
// var toc = require('remark-toc');
// slideshow.use(toc);
</script>
</body>
</html>