Hello,
please have a look to an attempt to approach the problem in form of a R package testable for feedback