Skip to content

Commit

Permalink
Week 4 materials ready to go
Browse files Browse the repository at this point in the history
  • Loading branch information
robjhyndman committed Mar 15, 2024
1 parent 5ed7554 commit 89a9545
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 103 deletions.
49 changes: 0 additions & 49 deletions week4/examples.R

This file was deleted.

2 changes: 1 addition & 1 deletion week4/index.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ schedule |>
* Debugging
* Measuring performance
* Efficient R pgoramming
* R environments
* Caching
* R environments

## Online resources

Expand Down
14 changes: 0 additions & 14 deletions week4/profiling-example.R

This file was deleted.

75 changes: 36 additions & 39 deletions week4/slides.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ The **reprex** package helps create *minimal reproducible examples*.


## Debugging tools in R
\vspace*{-0.2cm}
\fontsize{14}{15}\sf

* `traceback`: prints out the function call stack after an error occurs; does nothing if there's no error.
Expand Down Expand Up @@ -98,21 +99,21 @@ traceback()
```

## Interactive debugging
\fontsize{13}{14}\sf

* Using `browser()`

```{r, eval = FALSE}
g <- function(b) {
```r
i <- function(d) {
browser()
h(b)
if (!is.numeric(d)) stop("`d` must be numeric", call. = FALSE)
d + 10
}
f(10)
```

* Setting breakpoints
* Similar to `browser()` but no change to source code.
* Can be set in RStudio by clicking to the left of the line number, or pressing `Shift + F9`.

* Set in RStudio by clicking to left of line number, or pressing `Shift+F9`.
* `options(error = browser)`

## Interactive debugging
Expand All @@ -121,6 +122,25 @@ traceback()
* `undebug()` : removes `browser()` statement.
* `debugonce()` : same as `debug()`, but removes `browser()` after first run.

## Exercises

1. What's wrong with this code?\fontsize{10}{10}\sf

```{r, error = TRUE}
# Multivariate scaling function
mvscale <- function(object) {
# Remove centers
mat <- sweep(object, 2L, colMeans(object))
# Scale and rotate
S <- var(mat)
U <- chol(solve(S))
z <- mat %*% t(U)
# Return orthogonalized data
return(z)
}
mvscale(mtcars)
```
## Example
\vspace*{-0.15cm}
\centerline{\href{https://posit.co/resources/videos/debugging-techniques-in-rstudio-2/}{\includegraphics[width=16cm, height=20cm]{../screenshots/Amanda_Gadrow.png}}}
Expand Down Expand Up @@ -152,27 +172,6 @@ traceback()
* longer object length is not a multiple of shorter object length
* package is not available for R version `xx`
## Exercises

1. What's wrong with this code?

\fontsize{10}{10}\sf

```{r, error = TRUE}
# Multivariate scaling function
mvscale <- function(object) {
# Remove centers
mat <- sweep(object, 2L, colMeans(object))
# Scale and rotate
S <- var(mat)
U <- chol(solve(S))
z <- mat %*% t(U)
# Return orthogonalized data
return(z)
}
mvscale(mtcars)
```

## Non-interactive debugging
* Necessary for debugging code that runs in a non-interactive environment.
Expand Down Expand Up @@ -309,11 +308,6 @@ bench::mark(

Repeat using `bench::mark()`. Why are they different?

## Exercises

3. Write a function to find the second largest element of a numeric vector. Test several alternatives, and choose the fastest one.


# Improving performance

## Vectorization
Expand Down Expand Up @@ -345,11 +339,7 @@ bench::mark(

## Exercises

4. How can you use `crossprod()` to compute a weighted sum? How much faster is it than the naive `sum(x * w)`? Why is it faster?

## Exercises

5. Write the following algorithm to estimate $\displaystyle\int_0^1 x^2 dx$ using vectorized code
3. Write the following algorithm to estimate $\displaystyle\int_0^1 x^2 dx$ using vectorized code

### Monte Carlo Integration
a. Initialise: `hits = 0`
Expand Down Expand Up @@ -399,6 +389,13 @@ compute()
compute()
```

```{r}
#| include: false
#| cache: false
# Need to explicitly remove results.rds for some reason when doing this in quarto
file.remove(here::here("cache/results.rds"))
```

```{r}
#| label: cache2
#| cache: false
Expand Down Expand Up @@ -463,7 +460,7 @@ memo_sq(2)

## Exercises

6. Use `bench::mark()` to compare the speed of `sq()` and `memo_sq()`.
4. Use `bench::mark()` to compare the speed of `sq()` and `memo_sq()`.

# R environments

Expand Down Expand Up @@ -494,4 +491,4 @@ memo_sq(2)

## Exercises

7. Add renv to your Assignment 1 project.
5. Add renv to your Assignment 1 project. Make sure the packages included are the latest CRAN versions of all packages.
136 changes: 136 additions & 0 deletions week4/workshop_code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
## Using traceback and debug (Slide 8)

f <- function(a) g(a)
g <- function(b) h(b)
h <- function(c) i(c)
i <- function(d) {
if (!is.numeric(d)) stop("`d` must be numeric", call. = FALSE)
d + 10
}
f("a")

traceback()

options(error = recover)
f("a")



# Exercise 1 (slide 11): Multivariate scaling function
mvscale <- function(object) {
# Remove centers
mat <- sweep(object, 2L, colMeans(object))
# Scale and rotate
S <- var(mat)
U <- chol(solve(S))
z <- mat %*% t(U)
# Return orthogonalized data
return(z)
}
mvscale(mtcars)


## Slide 20

library(profvis)
library(bench)
f <- function() {
pause(0.1)
g()
h()
}
g <- function() {
pause(0.1)
h()
}
h <- function() {
pause(0.1)
}


# Slide 21

tmp <- tempfile()
Rprof(tmp, interval = 0.1)
f()
Rprof(NULL)
writeLines(readLines(tmp))

profvis(f())

## Additional profiling example

Rprof()
x <- NULL
for(i in seq(1e5)) {
x <- c(x, i)
}
Rprof(NULL)
summaryRprof()

Rprof()
x <- numeric(1e5)
for(i in seq(1e5)) {
x[i] <- i
}
Rprof(NULL)
summaryRprof()

f <- function(n = 1e5) {
x <- NULL
for(i in seq(n)) {
x <- c(x, i)
}
return(x)
}

profvis::profvis(f(1e4))

## Slide 23

x <- rnorm(1e6)
system.time(min(x))
system.time(sort(x)[1])
system.time(x[order(x)[1]])

## Slide 24

bench::mark(
min(x),
sort(x)[1],
x[order(x)[1]]
)

## Exercise 2: Slide 26

x <- exp(rnorm(1e7))
system.time(sqrt(x))
system.time(x^0.5)
system.time(exp(log(x) / 2))

bench::mark(sqrt(x), x^0.5, exp(log(x) / 2))

## Exercise 3: Slide 27

sort(x, partial = 2)


# Caching

compute <- function(...) {
xfun::cache_rds(rnorm(6), file = "results.rds", ...)
}
compute()
compute()
compute(rerun = TRUE)
compute()


library(memoise)
sq <- function(x) {
print("Computing square of 'x'")
x**2
}
memo_sq <- memoise(sq)
memo_sq(2)
memo_sq(2)

0 comments on commit 89a9545

Please sign in to comment.