简体   繁体   中英

Make a loop on different family for glm in R

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM