Ron Flatau
Ron Flatau

Reputation: 21

How to exclude interaction term from glmulti?

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'?

Upvotes: 2

Views: 245

Answers (2)

swihart
swihart

Reputation: 2738

I read the JSS glmulti paper, and in it the package authors state many ways to exclude interactions.

I have ran into this error too. I think the simpler the variable names the better is my working hypothesis. I'll keep updating this post.

library(glmulti)
set.seed(1)
myDataFrame <- data.frame( c=rnorm(100),
                           a=as.factor(rbinom(100,5,0.9)),
                           z=rbinom(100,1,0.05))
summary(myDataFrame)
myDataFrame$y <- 3*myDataFrame$c + 
                 2*(myDataFrame$a==4)*myDataFrame$z +
                 2*(myDataFrame$a==5)*myDataFrame$z +
                10* myDataFrame$c    *myDataFrame$z

summary(myDataFrame)
head(myDataFrame)
output <- glmulti(y ~ -1 + c*a*z - c:z - a:c, 
                  data = myDataFrame, 
                  maxit = 30)
summary(output)

## check all the formulas to make sure they
## excluded `c:z` and `a:c`
output@formulas

## and the best formula:
summary(output)$bestmodel


## If we had not excluded `c:z` and `a:c`:
output2 <- glmulti(y ~ -1 + c*a*z, 
                  data = myDataFrame, 
                  maxit = 30)
summary(output2)

## check all the formulas to make sure they
## include `c:z` and `a:c`
output2@formulas

## ...and the best formula:
summary(output2)$bestmodel

Update: I made longer names and it seemed to work the same as the single letters!

library(glmulti)
set.seed(1)
myDataFrame <- data.frame( crabby=rnorm(100),
                           angler=as.factor(rbinom(100,5,0.9)),
                           zootic=rbinom(100,1,0.05))
summary(myDataFrame)
myDataFrame$y <- 3*myDataFrame$crabby + 
                 2*(myDataFrame$angler==4)*myDataFrame$zootic +
                 2*(myDataFrame$angler==5)*myDataFrame$zootic +
                10* myDataFrame$crabby    *myDataFrame$zootic

summary(myDataFrame)
head(myDataFrame)
output <- glmulti(y ~ -1 + crabby*angler*zootic - crabby:zootic - angler:crabby, 
                  data = myDataFrame, 
                  maxit = 30)
summary(output)

## check all the formulas to make sure they
## excluded `c:z` and `a:c`
output@formulas
## best formula:
summary(output)$bestmodel


## If we had not excluded `c:z` and `a:c`:
output2 <- glmulti(y ~ -1 + crabby*angler*zootic, 
                  data = myDataFrame, 
                  maxit = 30)
summary(output2)

## check all the formulas to make sure they
## include `c:z` and `a:c`
output2@formulas

## best formula:
summary(output2)$bestmodel

Ah, but look at this. If we use the exclude argument, we reproduce OP's error:

output <- glmulti(y ~ -1 + crabby*angler*zootic ,
                  exclude = c("crabby:zootic", "angler:crabby"), 
                  data = myDataFrame, 
                  maxit = 30)
Error in glmulti(y ~ -1 + crabby * angler * zootic, exclude = c("crabby:zootic",  : 
  Improper call of glmulti.

Ah, paper says to use the 3 argument approach (can't use formula) when you use exclude=, like this:

output <- glmulti("y",
                  c("crabby","angler","zootic"),
                  exclude = c("crabby:zootic", "angler:crabby"), 
                  intercept = FALSE,
                  data = myDataFrame, 
                  maxit = 30)
summary(output)

From the paper: enter image description here

Upvotes: 0

Niels Holst
Niels Holst

Reputation: 626

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)

Upvotes: 0

Related Questions