Home > Enterprise >  R: Reiterate command metafor:rma using a loop
R: Reiterate command metafor:rma using a loop

Time:09-17

I am doing a metanalysis of proportions using the metafor package and the rma.glmm function. Since I the dataset is large, with many variables, I would like to reiterate the rma.glmm function through some columns of the dataframe.

This is the dataframe:

    dput(data)
structure(list(event = c(5, 55, 4, 43, 2, 45, 34, 0, 34, 2, 23, 
54, 45, 45, 67, 67, 78, 34, 45, 0, 34, 2, 23, 54, 45, 45, 67, 
67, 78, 34, 45, 0, 34, 2, 23, 54, 45, 45), tot = c(45, 67, 89, 
111, 133, 155, 177, 199, 221, 243, 265, 287, 309, 331, 353, 375, 
397, 419, 441, 463, 485, 507, 529, 551, 573, 595, 617, 639, 661, 
683, 705, 727, 749, 771, 793, 815, 837, 859), moderator = c("a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", 
"a", "a", "a", "a", "a", "a", "a", "a", "a", "b", "b", "b", "b", 
"b", "b", "b", "b", "b", "b", "b", "b", "b", "b", "b"), x1 = c(5, 
55, 4, 43, 2, 45, 34, 0, 34, 2, 23, 54, 45, 45, 67, 67, 78, 34, 
45, 0, 34, 2, 23, 54, 45, 45, 67, 67, 78, 34, 45, 0, 34, 2, 23, 
54, 45, 45), x2 = c(6, 56, 5, 44, 3, 46, 35, 1, 35, 3, 24, 55, 
46, 46, 68, 68, 79, 35, 46, 1, 35, 3, 24, 55, 46, 46, 68, 68, 
79, 35, 46, 1, 35, 3, 24, 55, 46, 46), x3 = c(7, 57, 6, 45, 4, 
47, 36, 2, 36, 4, 25, 56, 47, 47, 55, 4, 43, 2, 45, 34, 0, 34, 
2, 23, 54, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45
)), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-38L))

And I wrote the following code:

  columns <- c(4,5,6) # I decide which columns of the dataframe I want analyze
  n_columns <- as.numeric(length(columns))

 for (var in 1:n_columns){
    colname <- colnames(data[,columns[var]])
    glmm<-rma.glmm(xi=colname, ni=tot, measure="PLO", data=data)
    pes=predict(glmm, transf=transf.ilogit, targ=list(ni=data$tot))
          }

I get the following error: Error in ni - xi : non-numeric argument to binary operator

Basically, the code does not read the variable name which is assigned to colname.

Can anyone help? Thank you

CodePudding user response:

colname is a string, which doesn't work. It's a bit like you are trying to do something like lm("x1" ~ x2, data=data).

You can simply use

glmm <- rma.glmm(xi=data[[var]], ni=tot, measure="PLO", data=data)

And you can leave out the targ=list(ni=data$tot) from predict(). This has no effect.

  • Related