##
## Based on examples from help page of netposet()
##

library(netmeta)

data(Linde2015)

## (1) Early response
##
p1 <- pairwise(treat = list(treatment1, treatment2, treatment3),
               event = list(resp1, resp2, resp3),
               n = list(n1, n2, n3),
               studlab = id, data = Linde2015, sm = "OR")
##
net1 <- netmeta(p1, comb.fixed = FALSE, ref = "Placebo")

## (2) Early remission
##
p2 <- pairwise(treat = list(treatment1, treatment2, treatment3),
               event = list(remi1, remi2, remi3),
               n = list(n1, n2, n3),
               studlab = id, data = Linde2015, sm = "OR")
##
net2 <- netmeta(p2, comb.fixed = FALSE, ref = "Placebo")

## (5) Adverse events
##
p5 <- pairwise(treat = list(treatment1, treatment2, treatment3),
               event = list(ae1, ae2, ae3),
               n = list(n1, n2, n3),
               studlab = id, data = Linde2015, sm = "OR")
##
net5 <- netmeta(p5, comb.fixed = FALSE, ref = "Placebo")


## Print results for outcome 1
##
summary(net1)

## Log odds ratios for comparisons with reference group, i.e. placebo
##
round(net1$TE.random[, net1$reference.group], 4)


## Odds ratios for comparisons with placebo
##
round(exp(net1$TE.random[, net1$reference.group]), 4)
round(exp(net2$TE.random[, net2$reference.group]), 4)
round(exp(net5$TE.random[, net5$reference.group]), 4)


##
## Create datasets of comparisons with placebo
##
dat1 <- data.frame(trts = net1$trts,
                   OR1 = exp(net1$TE.random[, net1$reference.group]),
                   lower1 = exp(net1$lower.random[, net1$reference.group]),
                   upper1 = exp(net1$upper.random[, net1$reference.group]))
##
dat2 <- data.frame(trts = net2$trts,
                   OR2 = exp(net2$TE.random[, net2$reference.group]),
                   lower2 = exp(net2$lower.random[, net2$reference.group]),
                   upper2 = exp(net2$upper.random[, net2$reference.group]))
##
dat5 <- data.frame(trts = net5$trts,
                   OR5 = exp(net5$TE.random[, net5$reference.group]),
                   lower5 = exp(net5$lower.random[, net5$reference.group]),
                   upper5 = exp(net5$upper.random[, net5$reference.group]))

##
## Merge datasets
##
dat <- merge(dat1, dat2, by = "trts", all = TRUE)
dat <- merge(dat, dat5, by = "trts", all = TRUE)
##
dat


##
## Drop line with reference treatment
##
dat <- dat[dat$trts != net1$reference.group, ]


##
## Scatter plot of odds ratios (outcomes 1 and 2)
##
lim <- range(c(dat$OR1, dat$OR2), na.rm = TRUE)
##
plot(dat$OR1, dat$OR2, log = "xy",
     xlab = "Odds ratio (outcome 1)", ylab = "Odds ratio (outcome 2)",
     main = "Comparisons with 'Placebo'",
     xlim = lim, ylim = lim, pty = "s")
##
abline(h = 1, v = 1)


##
## Scatter plot of odds ratios (outcomes 1 and 2)
## with treatment labels
##
plot(dat$OR1, dat$OR2, log = "xy",
     xlab = "Odds ratio (outcome 1)", ylab = "Odds ratio (outcome 2)",
     main = "Comparisons with 'Placebo'",
     xlim = c(0.95, 2.22), ylim = c(0.95, 2.22), pty = "s")
##
abline(h = 1, v = 1)
##
text(dat$OR1, dat$OR2, labels = dat$trts, pos = c(4, 4, 1, 4, 3, 4, 4, 2))


##
## Scatter plot of odds ratios and 95% confidence intervals
## (outcomes 1 and 5)
##
lim <- range(c(dat$lower1, dat$lower5, dat$upper1, dat$upper5),
             na.rm = TRUE)
##
plot(dat$OR1, dat$OR5, log = "xy",
     xlab = "Odds ratio (outcome 1)", ylab = "Odds ratio (outcome 5)",
     main = "Comparisons with 'Placebo'",
     xlim = lim, ylim = lim, pty = "s")
##
abline(h = 1, v = 1)
##
for (i in seq(along = dat$trts)) {
  ## Confidence intervals for outcome 1
  segments(dat$lower1[i], dat$OR5[i],
           dat$upper1[i], dat$OR5[i],
           col = "green")
  ## Confidence intervals for outcome 5
  segments(dat$OR1[i], dat$lower5[i],
           dat$OR1[i], dat$upper5[i],
           col = "blue")
}
##
points(dat$OR1, dat$OR5)
