doseresNMA antidep/net.functions.R

#!!! think about it and ask Georgia: the quantile should be computed from min OR 0 to max dose
# functions to be used in net.structure

# 1. take the drug-dose and find its RCS transformation at
dose_to_rcs <- function(dose.per.drug,knot_probs=c(0.25,0.50,0.75)){
  require('rms')
  max.dose <- max(dose.per.drug)
  min.dose <- min(dose.per.drug)
  knots <- quantile(min.dose:max.dose,probs = knot_probs)
  rcs.dose.per.drug <-  rcspline.eval(dose.per.drug,knots = knots,inclx = TRUE)
  return(rcs.dose.per.drug)
}

# 2. convert the data column to matrix ( row is study and column is arm (dose-level) )

col_to_mat <- function(data,var){
  ns <-length(unique(data$studyid))
  na <- as.numeric(table(data$studyid)) # number of arms per study
  max.na <- max(na)
  data$studyID <- as.numeric(as.factor(data$studyid))         # transform studyid to ordered numeric values
  study_id <- unique(data$studyID)
  varmat <- matrix(NA,ns,max.na)
  for (i in 1:ns) {
    varmat[i,1:as.numeric(table(data$studyID)[i])] <- var[data$studyID == study_id[i]]
  }
  return(varmat)
}

# 3. find the indices of the direct head-to-head comparisons
direct.comp.index <- function(data)
{
  require(dplyr)
  data <- dplyr::arrange(data, data$studyid, data$dose)
  t1 <- vector()
  t2 <- vector()
  for (i in seq_along(unique(data[["studyid"]]))) {
    subset <- subset(data, studyid==unique(data[["studyid"]])[i])
    for (k in 2:nrow(subset)) {
      t1 <- append(t1, subset[["drug"]][1])
      t2 <- append(t2, subset[["drug"]][k])
      if (is.na(subset[["drug"]][k])) {
        stop()
      }
    }
  }

  comparisons <- data.frame(t1 = t1, t2 = t2)
  comparisons <- comparisons %>% dplyr::group_by(t1, t2) %>%
    dplyr::mutate(nr = dplyr::n())
  comparisons <- unique(comparisons)
  comparisons <- dplyr::arrange(comparisons, t1, t2)
  row_name = comparisons$row_name
  comparisons %<>% select(-row_name) %>% as.matrix
  rownames(comparisons) = row_name
  return(comparisons)
}
htx-r/doseresNMA documentation built on Jan. 28, 2021, 5:32 a.m.