2

I am trying to use the tidymodels package for a GLM and want to use the Gamma or Poisson distribution.

Using glm I would use something like the following

# using glm
mdl <- glm(data = data, y ~ x, family = Gamma(link = "inverse"))
mdl <- glm(data = data, y ~ x, family = poisson(link = "log"))

# using glmnet
library(glmnet)
mdl <- glmnet(data$x, data$y, family = Gamma(link = "inverse"))
mdl <- glmnet(data$x, data$y, family = poisson(link = "log"))

How can I achieve the same using tidymodels? Note that I am trying to do a regression and not a classification (logistic regression) for which I could use parsnip::logistic_reg().

I found one article on Generalized Linear Models on tidymodels, which belongs to the embed package but does not show how to specify the family.

I would expect something similar to this (which does not work as neither linear_reg has the parameters family or link, nor does set_engine support glm in linear regression mode)

mdl <- linear_reg(mode = "regression", family = "gamma", link = "inverse") %>% set_engine("glm") # or glmnet
David
  • 9,216
  • 4
  • 45
  • 78

1 Answers1

4

That was easier than expected:

mdl <- linear_reg(mode = "regression") %>%
  set_engine("glmnet", family = "gamma")

# or 
mdl <- linear_reg(mode = "regression") %>%
  set_engine("glmnet", family = Gamma(link = "inverse"))
David
  • 9,216
  • 4
  • 45
  • 78
  • 1
    When using "glmnet" you should set `lambda = 0` so it does not add a penalty and thus becomes a "normal" GLM without regularization. – drT Nov 10 '21 at 10:10