The torch
package for R (found here) is CRAN-installable and provides automatic differentiation in R, as long as you’re willing to rewrite your code using Torch functions.
The current docs for the torch
package are great, but assume you’re interested in machine learning. But gradients are useful for ordinary statistics, too! In the notebook below I fit a simple Poisson regression model using optim
by implementing the log likelihod and derivatives in torch. Though not really competitive with (the highly optimized) lme4::glm
on this toy example, the my point is more how easily you can roll your own MLE in R using torch
.
The notebook itself can be downloaded here, and an markdown version follows.
{% include_relative html/2022-04-01_poisson_regression_torch_for_r.md %}