(This article was originally published at Statistical Modeling, Causal Inference, and Social Science, and syndicated at StatsBlogs.)
A students wrote in one of his homework assignments:
Sidenote: I know some people say you’re not supposed to use loops in R, but I’ve never been totally sure why this is (a speed thing?). My first computer language was Java, so my inclination is to think in loops before using some of the other R functions that iterate for you. Maybe I should practice more with those.
There’s an answer to why loops are slow in R, I think it has something to do with memory allocation and the creation of new variables.
But the “why” is not really my point here.
Rather, my point is that you can directly check if it’s a speed thing. Just run a simulation.
For example:
N <- 1e5 date() a <- rep(1, N) date() for (n in 1:N) a[n] <- 1 date()
OK, let's try it out:
> N <- 1e5 > date() [1] "Fri Sep 15 09:15:18 2017" > a <- rep(1, N) > date() [1] "Fri Sep 15 09:15:18 2017" > for (n in 1:N) a[n] <- 1 > date() [1] "Fri Sep 15 09:15:18 2017"
Damn! It ran too fast. Let's up the N to 10 million:
> N <- 1e7 > date() [1] "Fri Sep 15 09:16:03 2017" > a <- rep(1, N) > date() [1] "Fri Sep 15 09:16:03 2017" > for (n in 1:N) a[n] <- 1 > date() [1] "Fri Sep 15 09:16:07 2017"
Yup. The loop runs slower than the unlooped version.
Is it just that rep(1, N) is just some super-fast function? We'll try another:
N <- 1e7 date() a <- rnorm(N) date() for (n in 1:N) a[n] <- rnorm(1) date()
Here's what happens:
> N <- 1e7 > date() [1] "Fri Sep 15 09:17:29 2017" > a <- rnorm(N) > date() [1] "Fri Sep 15 09:17:29 2017" > for (n in 1:N) a[n] <- rnorm(1) > date() [1] "Fri Sep 15 09:17:50 2017"
Again, the loop runs slower than the unlooped version.
The above is nothing definitive. The point is that, yes, a little experimentation can clarify our questions. The same principle applies in more complicated settings, for example this article which relates the results of experiments that had a big effect in the practice of computational statistics.
It's good to get in the habit of experimenting as a routine part of your workflow.
The post Learn by experimenting! appeared first on Statistical Modeling, Causal Inference, and Social Science.
Please comment on the article here: Statistical Modeling, Causal Inference, and Social Science
