dirkschumacher / rcppglm

GLM implementation in c++ with rcpp and armadillo
GNU Lesser General Public License v2.1
5 stars 2 forks source link
armadillo-library generalized-linear-models machine-learning-algorithms r

RcppGLM

Lifecycle:
experimental

RcppGLM implements the GLM fitting algorithm in c++ using Armadillo. It follows the code from [1]. Requires c++11 at the moment.

It is work in progress and mainly for learning purposes.

There are some numerical problems depending on the input. But a lot of stuff seems to work already.

Supported families and link functions

Example

Logistic regression:

library(RcppGLM)
X <- model.matrix(I(mpg < 20) ~ -1 + hp + drat + cyl, data = mtcars)
y <- mtcars$mpg < 20
family <- binomial_family(link = "logit")
glm_fit(X, y, family)
#>             [,1]
#> [1,]  0.12609238
#> [2,] -4.00405338
#> [3,] -0.03569851

Same as the R implementation:

coef(glm.fit(X, y, family = stats::binomial(link = "logit")))
#>         hp       drat        cyl 
#>  0.1260924 -4.0040534 -0.0356985

Poisson regression:

X <- model.matrix(hp ~ -1 + drat + cyl, data = mtcars)
y <- mtcars$hp
family <- poisson_family(link = "log")
glm_fit(X, y, family)
#>           [,1]
#> [1,] 0.6425100
#> [2,] 0.4078514

Same as the R implementation:

coef(glm.fit(X, y, family = stats::poisson(link = "log")))
#>      drat       cyl 
#> 0.6425100 0.4078514

References

1: Arnold, T., Kane, M., & Lewis, B. W. (2019). A Computational Approach to Statistical Learning. CRC Press.