metafor coupled with glmulti: How to exclude undesirable interactions

799 views Asked by At

I am fitting the following:

rma.glmulti.ran <- function (formula, data, random, ...) {
  rma.mv(as.formula(paste(deparse(formula))), Variance, random=  ~1 | Experiment, data = msa, method="REML", ...)
}
msa_res <- glmulti(MSA ~ MAPl+MAT_e+Duration.yrl+Fert+Naddl+Ndepl,
                   data=msa,
                   level=2,
                   exclude=c("MAPl:MAT_e","MAPl:Duration.yrl","MAPl:Fert",
                                     "MAPl:Naddl","MAPl:Ndepl","MAT_e:Duration.yrl","MAT_e:Fert","MAT_e:depl",
                                     "Duration.yrl:Fert","Duration.yrl:Ndepl","Duration.yrl:Naddl","Fert:Ndepl","Naddl:Ndepl"),
                   fitfunction=rma.glmulti.ran, crit="aicc")

The aim of this code is to include only these two interactions: "Naddl:MAT_e" and "Naddl:Fert". Thus, I am using exclude=c() to filter out all the other undesirable pairwise interactions from the full model (level=2).

This, theoretically should be the same than:

MSA ~ MAPl + MAT_e + ... + Naddl:MAT_e + Naddl:Fert

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

Error in glmulti(MSA ~ MAPl + MAT_e + Duration.yrl + Fert + Naddl + Ndepl,  : 
  Improper call of glmulti.

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

1

There are 1 answers

0
Choc_waffles On

Couldn't find the data you were referring to, so i generated fictitious variables below;

The exclude didn't work for me too, so i hard coded the exclusion into the formula. The following code worked for me, and no undesired interactions went into the glmulti.

library('utils')
retain_var <- c('DOW_MON','DOW_TUE','DOW_WED','DOW_THU') #set variables interested for testing
excl_inter_effects <- c('DOW_FRI','DOW_SAT','DOW_SUN') #set variables to exclude
set_depth <- 2 #generate 2 way interaction to exclude explicitly
excl_inter_form <- t(combn(excl_inter_effects,set_depth)) 
Paste <- function(x) paste(x, collapse = ":") 
excl_inter_form <- apply(excl_inter_form, 1, Paste)

rm(list = c('Paste','set_depth','excl_inter_effects')) #clean up

# ----- Generate formula incorporating exclusion ----- 

glm_formula <- as.formula(paste("y ~",paste(retain_var, collapse= "+"),"-",paste(excl_inter_form, collapse= "-")))