R/sieveSurv.predict.R

Defines functions sieveSurv.predict

Documented in sieveSurv.predict

#' Predict survival based on semiparametric B-spline sieve estimate
#'
#' @param newdata Dataframe or matrix containing named columns \code{time} and \code{covar}.
#' @param time Column name for follow-up time.
#' @param event Column name for event indicators.
#' @param all_covar Dataframe or matrix containing all values of \code{covar} from \code{data} used in \code{sieveSurv()}.
#' @param all_event_times Dataframe or matrix containing all values of \code{time} for uncensored subjects from \code{data} used in \code{sieveSurv()}.
#' @param sum_pBspline Vector of sum over splines for all combinations of \code{all_event_times} and \code{all_covar}.
#'
#' @return Copy of \code{newdata} with added column \code{surv} containing the predicted survival probability.
#'
#'
#' @export
#'
sieveSurv.predict <- function(newdata, time, covar, all_covar, all_event_times, sum_pBspline) {
  z <- newdata[, covar]
  x <- newdata[, time]
  newdata$surv <- sapply(X = 1:nrow(newdata),
                         FUN = function(nd) {
                           numer <- sum(rep(x = (all_covar <= z[nd]), each = length(all_event_times)) *
                                          rep(x = (all_event_times <= x[nd]), times = length(all_covar)) * sum_pBspline)
                           denom <- sum(all_covar <= z[nd])
                           1 - numer / denom
                           })
  return(newdata)
}
sarahlotspeich/sieveSurv documentation built on Feb. 14, 2022, 5:10 a.m.