rmcelreath / stat_rethinking_2023

Statistical Rethinking Course for Jan-Mar 2023
Creative Commons Zero v1.0 Universal
2.2k stars 248 forks source link

`sapply()` is an avoidable complication in code block 2.1 #6

Open dave-lovell opened 1 year ago

dave-lovell commented 1 year ago

I've just been watching lecture 2, and when R example 2.1 comes up (35:00), you explain that

For those of you who don't use R, sapply is just a loop, it's just a function that loops over a list

Because R vectorises functions by default, you don't need to use sapply here. This might make the code flow clearer, since you wouldn't need to explain the purpose of sapply.

I've written a small counter-example that uses vectorisation rather than sapply:

sample <- c("W", "L", "W", "W", "W", "L", "W", "L", "W")
W <- sum(sample == "W")
L <- sum(sample == "L")
p <- c(0, 0.25, 0.5, 0.75, 1)

get_ways <- function(q) (q*4)^W * ((1-q)*4)^L
ways <- get_ways(p)

prob <- ways/sum(ways)

cbind(p, ways, prob)
#>         p ways       prob
#> [1,] 0.00    0 0.00000000
#> [2,] 0.25   27 0.02129338
#> [3,] 0.50  512 0.40378549
#> [4,] 0.75  729 0.57492114
#> [5,] 1.00    0 0.00000000

Created on 2023-03-20 with reprex v2.0.2