**R snippets**, and kindly contributed to R-bloggers)

After my last post I have recurringly received two questions: (a) is it worthwhile to analyze GNU R speed in simulations and (b) how would simulation speed compare between GNU R and Python. In this post I want to address the former question and next time I am going to tackle the latter.

An area in which I use simulation in GNU R on a daily basis is bootstrapping. Therefore I have decided to check out how much speedup one can expect with tuning of standard model estimation procedures.

I started with linear regression: a simple model with two explanatory variables and 100 observations. I wanted to compare speed of generation of 10000 bootstrap replications using standard lm function and bare matrix calculus formula for **b** solving the equation: X’X**b**=X’y. Here is the simulation:

**(**1

**)**

**<-**100

**<-**runif

**(**n

**)**

**<-**runif

**(**n

**)**

**<-**x1

**+**x2

**+**1

**+**rexp

**(**n

**)**# add non-normal error distribution

**<-**cbind

**(**“(Intercept)”

**=**1, x1, x2, y

**)**

**<-**data.frame

**(**x1, x2, y

**)**

**<-**

**function**

**()**

**{**

**<-**data.m

**[**sample.int

**(**n, replace

**=**T

**)**,

**]**

**<-**dm

**[**, 1

**:**3

**]**

**<-**t

**(**X

**)**

**<-**dm

**[**, 4

**]**

**(**tX %*% X, tX %*% y

**)**

**}**

**<-**

**function**

**()**

**{**

**(**y

**~**., data

**=**data.f

**[**sample.int

**(**n, replace

**=**T

**)**,

**])$**coef

**}**

**(**2

**)**

**(**rb

**<-**replicate

**(**10000, boot.bare

**())[**, 1,

**])**

**(**2

**)**

**(**rl

**<-**replicate

**(**10000, boot.lm

**()))**

**(**rb

**–**rl

**)**

# OK difference ranges from -5.373479e-14 to 4.751755e-14

Both formulas yield practically identical results but using lm produces over 20 times slower execution. Of course lm does much more work than simple calculation of parameter estimates – boot for bootstrapping I need only the parameters.

So next I thought to check out a more complex model. Therefore I switched to logistic regression.

I compared glm estimation with two alternatives: brute-force optimization of log-likelihood using nlm and direct computation of estimates using iteratively reweighted least squares (IRLS, the method actually used by glm internally). Here is the code:

**(**1

**)**

**<-**100

**<-**runif

**(**n

**)**

**<-**runif

**(**n

**)**

**<-**x1

**+**x2

**+**runif

**(**n

**)**

**<**1.5 # add non-logistic error distribution

**<-**cbind

**(**1, x1, x2, y

**)**

**<-**data.frame

**(**x1, x2, y

**)**

**<-**

**function**

**()**

**{**

**<-**

**function**

**(**a

**)**

**{**

**<-**1

**/**

**(**1

**+**exp

**(-**X %*% a

**))**

**[**y

**==**F

**]**

**<-**1

**–**prob

**[**y

**==**F

**]**

**–**sum

**(**log

**(**prob

**))**

**}**

**<-**data.m

**[**sample.int

**(**n, replace

**=**T

**)**,

**]**

**<-**dm

**[**, 1

**:**3

**]**

**<-**t

**(**X

**)**

**<-**dm

**[**, 4

**]**

**(**llik, c

**(**1, 2, 3

**))$**estimate

**}**

**<-**

**function**

**()**

**{**

**<-**data.m

**[**sample.int

**(**n, replace

**=**T

**)**,

**]**

**<-**dm

**[**, 1

**:**3

**]**

**<-**t

**(**X

**)**

**<-**dm

**[**, 4

**]**

**<-**rep

**(**0, 3

**)**

**while**

**(**

**TRUE**

**)**

**{**

**<-**1

**/**

**(**1

**+**exp

**(-**X %*% b

**))**

**<-**as.vector

**(**prob

*****

**(**1

**–**prob

**))**

**<-**rbind

**(**pp, pp, pp

**)**

**<-**tX %*%

**(**y

**–**prob

**)**

**<-**b

**+**solve

**((**tX

*****pp

**)**%*% X, grad

**)**

**if**

**(**sum

**(**abs

**(**grad

**))**

**<**1e

**–**6

**)**

**{**

**(**b

**)**

**}**

**}**

**}**

**<-**

**function**

**()**

**{**

**<-**data.f

**[**sample.int

**(**n, replace

**=**T

**)**,

**]**

**(**y

**~**., data

**=**x, family

**=**“binomial”

**)$**coef

**}**

**(**2

**)**

**(**rn

**<-**replicate

**(**10000, boot.nlm

**()))**

**(**2

**)**

**(**ri

**<-**replicate

**(**10000, boot.irls

**())[**, 1,

**])**

**(**2

**)**

**(**rg

**<-**replicate

**(**10000, boot.glm

**()))**

**(**rn

**–**ri

**)**# -0.0002572192 0.0002470589

**(**rn

**–**rg

**)**# -0.0002572191 0.0002470588

range**(**ri **–** rg**)**# -4.196647e-07 3.602730e-07

We can see that using nlm is the slowest and produces unstable results and that IRLS and glm give identical results but IRLS is 6 times faster.

What are the conclusions? If you really need it is is possible to substantially reduce computational time even for “core” functions.

However, simplest means are not always guaranteed to be efficient (like nlm in logistic regression) and there is a question if the speedup pays of, as there is a substantial additional development effort needed.

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...