library(meta)
settings.meta(digits.Q = 4)

library(metafor)


data <-
  read.table(text="study,agegrp,Mean.FU,popXFU,event
DeBonis.2014,Adult,11.5,1552.5,13
Fucci.2007,Adult,6.5,416,4
Ram.2020,Pediatrics,5.583333,290.33333,4
Kadirogullari.2019,Pediatrics,1.616667,90.53333,2
Kashiyama.2014,Pediatrics,3.275,180.125,1
Tomita.2005,Pediatrics,6.708333,134.16667,1
Lawrie.2006,Pediatrics,3.1,291.4,16
Maeda.2019,Pediatrics,5.5,203.5,1
Zussa.1997,Pediatrics,3.05,350.75,2
Murashita.2013,Pediatrics,7.5,562.5,6
David.2013,Pediatrics,10.1,1070.6,30
Bourguignon.2016,Pediatrics,3.4,98.6,3
Phillips.2000,Pediatrics,0.9,66.6,5
Pfannmuller.2013,Pediatrics,4.3,498.8,5
Totaro.1999,Adult,3.833333,199.33333,3
", sep=",", header=T)


mr <-
  metarate(event, popXFU, data = data, studlab = study,
           irscale = 1000, method = "GLMM",
           common = FALSE)
##
mr.ped <- update(mr, subset = agegrp == "Pediatrics")
mr.adu <- update(mr, subset = agegrp == "Adult")
##
mr2 <- update(mr, subgroup = agegrp)


## Exact same results
##
print(mr2,
      overall = FALSE, overall.hetstat = FALSE)
##
print(mr.ped, details = FALSE, print.H = FALSE)
print(mr.adu, details = FALSE, print.H = FALSE)


## Compare results from meta and metafor
##
mr4.ped <-
  rma.glmm(xi = event, ti = popXFU, data = data,
           subset = agegrp == "Pediatrics",
           measure = "IRLN")
##
mr4.adu <-
  rma.glmm(xi = event, ti = popXFU, data = data,
           subset = agegrp == "Adult",
           measure = "IRLN")
##
res  <-
  data.frame(lnIR.ped = mr.ped$TE.random, tau2.ped = mr.ped$tau2,
             lnIR.adu = mr.adu$TE.random, tau2.adu = mr.adu$tau2,
             row.names = "subgrps")
##
res4  <-
  data.frame(lnIR.ped = mr4.ped$b, tau2.ped = mr4.ped$tau2,
             lnIR.adu = mr4.adu$b, tau2.adu = mr4.adu$tau2,
             row.names = "subgrps")
##
all.equal(res, res4)


## Test for subgroup differences
##
metagen(mr2$TE.random.w, mr2$seTE.random.w)$Q
metagen(mr2$TE.random.w, mr2$seTE.random.w)$pval.Q
