R/makejagsIPDADmeta.R

Defines functions makejagsIPDADmeta

makejagsIPDADmeta <- function(y=y,treat=treat,x=NULL,studyid=studyid,reg=FALSE,
                              AD=FALSE,rt=NULL,nt=NULL,rc=NULL,nc=NULL,
                              xbar.a=NULL,studyid.a=NULL,data=data){
 # This function is to reformat the origional dataset as jags model fromat

 # Arguments
  #** IPD
  # y: the binary outcome for each patient; 0/1
  # treat: treatment identifier for each patient: 0/1
  # sudyid: the study id for IPD
  # x: covariate
  #** AD
  # rt: number of events in treatment arm
  # nt: sample size in the treatment arm
  # rc: number of events in control arm
  # nc: sample size in the control arm
  # studyid.a: the study id for AD
  # xbar.a: covariate in study-level; mean or propotion

  # evaluate the arguments in the data
  IPDdata <- data$IPDdata
  ADdata <- data$ADdata
  IPDdata$studyid <- eval(substitute(studyid), IPDdata)
  IPDdata$y <- eval(substitute(y), IPDdata)
  IPDdata$treat <- eval(substitute(treat), IPDdata)
  rt <- eval(substitute(rt), ADdata)
  nt <- eval(substitute(nt), ADdata)
  rc <- eval(substitute(rc), ADdata)
  nc <- eval(substitute(nc), ADdata)
  ADdata$studyid.a <- eval(substitute(studyid.a), ADdata)
  #** IPD
  npIPD <- as.vector(table(IPDdata$studyid)) # number of patients
  nIPD <- length(unique(IPDdata$studyid))    # number of IPDs

  # matrix of the binary outocme where studies in row and patients in columns, the extra replaced by NA
  y <- matrix(NA,nIPD,max(npIPD))
  for (i in 1:nIPD) {
    y[i,1:npIPD[i]] <- IPDdata$y[IPDdata$studyid == i]
  }

  # matrix of the treatment identifier where studies in rows and patients in columns, the extra replaced by NA
  treat <- matrix(NA,nIPD,max(npIPD))
  for (i in 1:nIPD) {
    treat[i,1:npIPD[i]] <- IPDdata$treat[IPDdata$studyid == i]
  }

  #** AD: with or without covariate
  nAD <- length(unique(ADdata$studyid.a))    # number of ADs

if(AD==TRUE){ # with AD
  if(reg==FALSE){ # without covariate
    jagsdataIPDADmeta<- list(y=y,treat=treat,npIPD=npIPD,nIPD=nIPD,
                           rc=rc,rt=rt,nc=nc,nt=nt,nAD=nAD)
  }else{# with covariate
    IPDdata$x <- eval(substitute(x), IPDdata)
    x <- matrix(NA,nIPD,max(npIPD))
    for (i in 1:nIPD) {
      x[i,1:npIPD[i]] <- IPDdata$x[IPDdata$studyid == i]
    }

    xbar <- colMeans(t(x),na.rm = TRUE)
    xbar.a  <- eval(substitute(xbar.a), ADdata)
    jagsdataIPDADmeta<- list(y=y,treat=treat,x=x,xbar=xbar,npIPD=npIPD,nIPD=nIPD,
                             rc=rc,rt=rt,nc=nc,nt=nt,nAD=nAD,xbar.a=xbar.a)
  }
}else{ # without AD
  if(reg==FALSE){ # without covariate
    jagsdataIPDADmeta<- list(y=y,treat=treat,npIPD=npIPD,nIPD=nIPD)
  }else{ # with covariate
    IPDdata$x <- eval(substitute(x), IPDdata)
    x <- matrix(NA,nIPD,max(npIPD))
    for (i in 1:nIPD) {
      x[i,1:npIPD[i]] <- IPDdata$x[IPDdata$studyid == i]
    }
    xbar <- colMeans(t(x),na.rm = TRUE)
    jagsdataIPDADmeta<- list(y=y,treat=treat,x=x,xbar=xbar,npIPD=npIPD,nIPD=nIPD)
  }
}

# return a list of new jags formatted IPD with or without AD dataset
  return(jagsdataIPDADmeta)
}
htx-r/GenericModelNMA documentation built on Nov. 10, 2020, 2:36 a.m.