R/data.aug.edit.r

Defines functions data.aug.edit data.edit

Documented in data.aug.edit

#' Data editing function for network meta-analysis
#'
#' Data editing function for network meta-analysis involving data augmentation of White et al. (2012).
#'
#' @param dt1 Dataset to be edited for analyzing by multivariate meta-analysis functions.
#' To be formatted like the style of the example dataset "smoking".
#' @return
#' \item{y}{Odds-ratio estimates for all of the pairwise comparisons in which the reference treatment
#'  is set to "trt" 1. Formated to a N x p matrix.}
#' \item{S}{Within-studies covariance matrix estimates. A matrix with N rows and p(p+1)/2 columns.}
#' @references
#' White, I. R., Barrett, J. K., Jackson, D., Higgins, J. P. (2012).
#' Consistency and inconsistency in network meta-analysis: model estimation using multivariate meta-regression.
#' \emph{Research Synthesis Methods} \strong{3}: 111-125.
#'
#' Noma, H., Nagashima, K., Maruo, K., Gosho, M., Furukawa, T. A. (2017).
#' Bartlett-type corrections and bootstrap adjustments of likelihood-based inference methods for network meta-analysis.
#' \emph{ISM Research Memorandum} 1205.
#' @examples
#' data.aug.edit(smoking)
#' @export
data.aug.edit <- function(dt1){

  study <- dt1[, 1]
  treatment <- dt1[, 2]
  trt <- dt1[, 3]
  d <- dt1[, 4]
  n <- dt1[, 5]

  L <- dim(dt1)[1]

  N <- max(study)
  p <- max(trt)

  X1 <- X2 <- matrix(rep(NA, times = N*p), N)

  for(i in 1:L){

    k <- study[i]
    l <- trt[i]

    X1[k,l] <- d[i]
    X2[k,l] <- n[i]

  }

  p <- p - 1

  for(i in 1:N){

    if(length(which(X1[i,] == 0)) > 0){
      X1[i, ] <- X1[i, ] + 0.5
      X2[i, ] <- X2[i, ] + 1
    }

    if(length(which(X1[i, ] == X2[i, ])) > 0){
      X1[i, ] <- X1[i, ] + 0.5
      X2[i, ] <- X2[i, ] + 1
    }

  }

  X1[is.na(X1[, 1]), 1] <- 0.001
  X2[is.na(X2[, 1]), 1] <- 0.01

  de1 <- data.edit(X1, X2)
  y <- de1$y
  S <- de1$S

  return(list(y = y, S = S))

}


data.edit <- function(X1, X2){

  r <- is.na(X1[, 1])
  X1[r, 1] <- 0.001
  X2[r, 1] <- 0.01

  N <- dim(X1)[1]
  p <- dim(X1)[2]

  y <- matrix(numeric(N*(p-1)), N)

  for(i in 2:p){

    z1 <- X1[, 1]
    z2 <- X2[, 1]

    x1 <- X1[, i]
    x2 <- X2[, i]

    y[, (i - 1)] <- log(x1/(x2-x1)) - log(z1/(z2-z1))

  }

  S <- NULL

  for(i in 2:(p - 1)){

    z1 <- X1[, 1]
    z2 <- X2[, 1]

    x1 <- X1[, i]
    x2 <- X2[, i]

    s11 <- 1/z1 + 1/(z2 - z1) + 1/x1 + 1/(x2 - x1)

    S <- cbind(S, s11)

    for(j in (i + 1):p){

      w1 <- X1[, j]
      w2 <- X2[, j]

      s11 <- 1/z1 + 1/(z2 - z1) + (x1 - x1) + (w1 - w1)
      S <- cbind(S, s11)

    }

  }

  x1 <- X1[, p]
  x2 <- X2[, p]

  s11 <- 1/z1 + 1/(z2 - z1) + 1/x1 + 1/(x2 - x1)

  S <- cbind(S, s11); colnames(S) <- NULL

  mng2 <- list(y = y, S = S)

  return(mng2)

}
nshi-stat/netiim3 documentation built on May 6, 2019, 10:51 p.m.