Question:
Is there a clean and fast-running way to compute geometric mean using data.table?
Background:
So I am using this:
my_col_list <- names(mydata)[ my_start:ncol(mydata)]
my_name_list<- paste0(my_col_list, "_", "arithmean")
mydata[, (my_name_list) := unlist(lapply(.SD,
function(x) rollapply(x,
5,
mean,
na.pad = TRUE)),
recursive = F),
.SDcols = my_col_list]
But I want to compute geometric mean. I am working on ~20 million rows and ~1500 columns, so a fast-runing built-in is preferred.
I saw these (link) ways of computing geometric mean, but they are hand-coded so they are going to be slower. This (link) is about an overall geometric mean, not a windowed/rolling geometric mean.
Packages that have hand-coded (slower-running) geometric means include:
- psych, geometric.mean, hand coded
- EnvStats, geoMean, hand coded with more error checking
- compositions, geometricmean, hand coded
- caroline, geomean, hand coded
- PerformanceAnalytics, mean.geometric, hand coded
- pracma, geomean, hand coded
- FinCal, geometric.mean, a novel hand-coded version (prod vs. mean)
- fdaMixed, dataTrans, hand-coded
- evolqg, MeanMatrix, hand coded, operates on covariance matrices, uses chol
not mean, but it is fast:
- Gmedian, Gmedian, median instead of mean, but built to be faster. Uses Rcpp for compute.
maybe, but not sure:
- rotations, mean.SO3