0

I'm trying my best but I can't find out how to loop on different laws, to find the best glm model!

famille = paste0(fam,'(link=',sprintf(" '%s'",lien),')')
famille
[1] "poisson(link= 'log')"

I got two variables fam and lien I want to loop on two fam (poisson and binomial) and two links (logit and log).

And I don't know how to put my variable famille in the argument family of glm function.

I'm doing this

glm_model = glm(as.formula(paste(gar,"~."),family=famille,data=train)

and I got this error:

Error in get(family, mode = "function", envir = parent.frame()) :
objet 'poisson(link= 'log')' de mode 'function' introuvable

I wish you could help me please?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    First, can you explain why you want to use both a poisson model and a logit model for the same data? Those are two very different classes of models that should be used for different purposes – Matt Kaye Mar 29 '21 at 16:13
  • Ok, I will need to run a lots of GLM to check the pricing of an insurer on new data. Therefore I need to loop on every configuration of GLM I will be provided with! – Victor Dequatre Mar 30 '21 at 11:40
  • I'm still not understanding. If the variable your'e trying to predict is a price, why are you using a poisson and binomial GLM? Binomial GLMs (logistic regression, probit, tobit, etc.) are used for predicting classes + probabilities of an event, and poisson regression is used for predicting counts (i.e. number of events that will happen, number of typos in a book, etc.) – Matt Kaye Mar 30 '21 at 15:23
  • I got two models for price (I will use gamma or gaussian(log) depending on the results) and frequency ( poisson and binomial negative (sorry )). I need too check which law fits the best on these two models – Victor Dequatre Mar 31 '21 at 15:19

1 Answers1

0

Alright, I think I get what you're after from the comments. Here's how I'd do it:

df <- data.frame(
    y = 1:10,
    x = rnorm(10)
)

fam <- poisson
link <- 'log'

glm(y ~ x, data = df, family = fam(link = link))
#> 
#> Call:  glm(formula = y ~ x, family = fam(link = link), data = df)
#> 
#> Coefficients:
#> (Intercept)            x  
#>      1.6723      -0.1973  
#> 
#> Degrees of Freedom: 9 Total (i.e. Null);  8 Residual
#> Null Deviance:       16.64 
#> Residual Deviance: 13.98     AIC: 51.94

Created on 2021-04-01 by the reprex package (v1.0.0)

No need for anything fancy. Then you can just swap the functions (families) and the link functions ('log', 'logit', etc.) as you see fit

Matt Kaye
  • 521
  • 4
  • 5