How to exclude interaction term from glmulti?

169 Views Asked by At

I am fitting the following:

offspring.survival_STAB <-glmulti(ST~ STAB + Age + BS+ BSP + Sex,
                                     data=ST, 
                                     exclude="BS:BSP",
                                     level=2,
                                     fitfunc=glm,
                                     crit="aicc",
                                     family = Gamma(link = "inverse"),
                                     method = "h")

The ain of this code is to get all the possible interactions excluding the interaction "BS:BSP".

However I get this error when I add exclude=c() in the formula:

Error in glmulti(ST ~ STAB + Age + BS + BSP + Sex, data = ST, exclude = "BS:BSP",  : 
  Improper call of glmulti.

Am I missing something about exclude=c()? Is another way to specify interactions terms in 'glmulti'?

1

There are 1 best solutions below

3
On

glmulti does not take a formula as input. You specify the regression model by text arguments:

glmulti("ST", 
        c("STAB", "Age", "BS", "BSP", "Sex"),
        data=ST, 
        exclude="BS:BSP", etc.)

From the docs at https://www.rdocumentation.org/packages/glmulti/versions/0.5-1/topics/glmulti

From the comments it seems glmulti does not work one way or the other. Then there is doing it yourself!

This function takes a data frame and constructs an equation with all main effects and all pair-wise interactions, except those given as an argument. It assumes the left-hand side is in column 1 and that the remaining columns constitute the right-hand predictors. Accomodate the code as you wish:

make_eq = function(df, exclude) {
  Y = colnames(df)[1]
  X = colnames(df)[-1]
  terms = c(X, apply(combn(X, 2), 2, paste, collapse=":"))
  terms = setdiff(terms, exclude)
  terms = paste(terms, collapse=" + ")
  eq = paste0(Y, " ~ ", terms)
  eval(parse(text=eq))
}

obs = data.frame(
  Y = rnorm(10),
  A = rnorm(10),
  B = rnorm(10),
  C = rnorm(10),
  D = rnorm(10)
)

lm(make_eq(obs, c("A:C", "C:D")), data = obs)