David
David

Reputation: 10232

GLM Family using tidymodels

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

Upvotes: 2

Views: 1387

Answers (1)

David
David

Reputation: 10232

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"))

Upvotes: 4

Related Questions