R/gen_stan_data_backup.R

#' Generate surv data for stan
#'
#' This function generates a long format dataframe for
#' repeated tte, time dependent covariates.
#'
#' @param d a dataset \cr
#' @return d a long dataset
#' @export gen_long_data_backup
#' @importFrom rlang !!
#' @importFrom magrittr %>%
gen_long_data_backup <- function(x, timepoints = NULL, status = "status", time = "time", patient_id = "patient_id") {
  # prepare for longdat formating
  x$sample_id <- 1:nrow(x)  #create sample id
  # get unique times: only event times equivalent to Cox model
  
  if(is.null(timepoints)){
    timepoints <- get_obs.time(x)
  }

  dtimepoints <- diff(c(0, timepoints))
  x$t_observed <- x$time
  x$time <- NULL
  
  tt_dat <- plyr::ddply(x, patient_id,
                        function(x) data.frame(tend = timepoints, t_id = seq_along(timepoints), dtime = dtimepoints, t_observed = x$t_observed )  )#create dataset with real observation
  
  tt_dat <- merge(tt_dat, x, by = patient_id)
  
  tt_dat$status_observed <- tt_dat$status
  tt_dat$status <- 0
  tt_dat$status[tt_dat$time == tt_dat$t_observed] <- as.numeric( tt_dat$status_observed[tt_dat$time == tt_dat$t_observed] )
  tt_dat$log_dtime <- as.double( unlist( log(tt_dat$dtime) ) )
  tt_dat
}
csetraynor/rstanhaz documentation built on May 9, 2019, 8:14 a.m.