0

I am conducting a meta-analysis and need to analyse multiple moderators. I am using the rma.uni function and have code which will stay the same for all moderators I'm testing:

res <- rma.uni(method = "HS", measure = "SMD", m1i = EXPM, m2i = CONM, sd1i = EXPSD, sd2i = CONSD, n1i = EXPN, n2i = CONN, slab=paste(AUTHOR, YEAR, sep=", "), data = outcomeData)

I want to add the argument "mods = ~ x", where x is the moderator I am testing (e.g. YEAR or AUTHOR) and then do a for loop of a combined list of moderators.

I have tried a couple of different ways but I'm having trouble interprettign the error messages.

mod.test <- c("AUTHOR", "YEAR")

for (i in mod.test){
  res <- rma.uni(method = "HS", measure = "SMD", m1i = EXPM, m2i = CONM, sd1i = EXPSD, sd2i = CONSD, n1i = EXPN, n2i = CONN, slab=paste(AUTHOR, YEAR, sep=", "), mods = ~ i, data = outcomeData)
  print(res)
}

and

mod.test <- c("AUTHOR", "YEAR")

for (x in mod.test){
  i <- paste("mods = ~ ", x)
  res <- rma.uni(method = "HS", measure = "SMD", m1i = EXPM, m2i = CONM, sd1i = EXPSD, sd2i = CONSD, n1i = EXPN, n2i = CONN, slab=paste(AUTHOR, YEAR, sep=", "), mods = ~ i, data = outcomeData)
  print(res)
}

both give the error:

Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

Thanks for any help.

  • Did you try `mods = paste("~",i,sep=" ")` inside the rm.uni function as for your first example ? – maydin Jul 22 '18 at 20:46
  • Just tried this and got the error code: `Error in rma.uni(method = "HS", measure = "SMD", m1i = EXPM, m2i = CONM, : Model matrix contains character variables.` – Robin Cafferata Jul 23 '18 at 08:01

2 Answers2

0

Since I don't know the data set you use, I used a base data instead.

You need to use direct indexing instead of giving names. In your case, try outcomeData[[i]] inside the loop. I changed your model a little bit since the column names in my data does not match with yours.

mod.test <- c("author", "year")

for (i in mod.test){
print(i)
res <- rma.uni(method = "HS", measure = "SMD",
 m1i = ablat, m2i = ablat, sd1i = ablat, sd2i = ablat,
 n1i = ablat, n2i = ablat, 
slab=paste(author, year, sep=", "),mods= ~dat.bcg[[i]],data = dat.bcg)

  print(res)
}

So in your case, try this.

mod.test <- c("AUTHOR", "YEAR")

for (i in mod.test){
  res <- rma.uni(method = "HS", measure = "SMD", m1i = EXPM, 
            m2i = CONM, sd1i = EXPSD, sd2i = CONSD, 
            n1i = EXPN, n2i = CONN, slab=paste(AUTHOR, YEAR, sep=", "), 
            mods = ~outcomeData[[i]], data = outcomeData)
  print(res)
}

You can find more info about that in this question.

maydin
  • 3,715
  • 3
  • 10
  • 27
0

I did what Maydin adviced, here is my fixed code.

mod.test <- c("YEAR", "TASK")

for (i in mod.test){
  res <- rma.uni(method = "HS", measure = "SMD", 
                 m1i = EXPM, m2i = CONM, sd1i = EXPSD, 
                 sd2i = CONSD, n1i = EXPN, n2i = CONN, 
                 slab=paste(AUTHOR, YEAR, sep=", "), 
                 mods = ~outcomeData[[i]], 
                 data = outcomeData)
  print(i)
  print(res)

One difficulty was I was using "AUTHOR" as a test moderator but this causes an error:

Error in rma.uni(method = "HS", measure = "SMD", m1i = EXPM, m2i = CONM,  : 
  Number of parameters to be estimated is larger than the number of observations.
In addition: Warning message:
In rma.uni(method = "HS", measure = "SMD", m1i = EXPM, m2i = CONM,  :
  Redundant predictors dropped from the model.

I think because each study has a unique author in my example dataset.