Here’s a quick comparison of performance between the compoisson and compoisson2 packages. In a first pass, I’ve improved speed by replacing two of the package functions, com.log.sum() and com.log.factorial() with functions in the base and matrixStats package that implement the same functions, but are written in C.
library(compoisson)
## Loading required package: MASS
data("insurance")
library(microbenchmark)
com_fit_compare = microbenchmark(compoisson::com.fit(Lemaire), compoisson2::com.fit(Lemaire), times=500L)
summary(com_fit_compare)
## expr min lq mean median
## 1 compoisson::com.fit(Lemaire) 18.554191 22.141790 24.889012 24.146013
## 2 compoisson2::com.fit(Lemaire) 3.092236 3.380301 4.369136 3.693177
## uq max neval cld
## 1 26.58203 65.15569 500 b
## 2 4.96483 48.80340 500 a
We can an approximately 6-fold improvement right off the bat.