forked from gbm-developers/gbm3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasehaz.gbm.R
78 lines (73 loc) · 2.66 KB
/
basehaz.gbm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# compute Breslow estimator of the baseline hazard function
#' Baseline hazard function
#'
#' Computes the Breslow estimator of the baseline hazard function for a
#' proportional hazard regression model
#'
#' The proportional hazard model assumes h(t|x)=lambda(t)*exp(f(x)).
#' \code{\link{gbm}} can estimate the f(x) component via partial likelihood.
#' After estimating f(x), \code{basehaz.gbm} can compute a nonparametric
#' estimate of lambda(t).
#'
#' @param t the survival times
#' @param delta the censoring indicator
#' @param f.x the predicted values of the regression model on the log hazard
#' scale
#' @param t.eval values at which the baseline hazard will be evaluated
#' @param smooth if \code{TRUE} \code{basehaz.gbm} will smooth the estimated
#' baseline hazard using Friedman's super smoother \code{\link{supsmu}}
#' @param cumulative if \code{TRUE} the cumulative survival function will be
#' computed
#' @return a vector of length equal to the length of t (or of length
#' \code{t.eval} if \code{t.eval} is not \code{NULL}) containing the baseline
#' hazard evaluated at t (or at \code{t.eval} if \code{t.eval} is not
#' \code{NULL}). If \code{cumulative} is set to \code{TRUE} then the returned
#' vector evaluates the cumulative hazard function at those values.
#' @author Greg Ridgeway \email{gregridgeway@@gmail.com}
#' @seealso \code{\link[survival]{survfit}}, \code{\link{gbm}}
#' @references N. Breslow (1972). "Discussion of `Regression Models and
#' Life-Tables' by D.R. Cox," Journal of the Royal Statistical Society, Series
#' B, 34(2):216-217.
#'
#' N. Breslow (1974). "Covariance analysis of censored survival data,"
#' Biometrics 30:89-99.
#' @keywords methods survival
basehaz.gbm <- function(t,delta,f.x,
t.eval=NULL,
smooth=FALSE,
cumulative=TRUE)
{
t.unique <- sort(unique(t[delta==1]))
alpha <- length(t.unique)
for(i in 1:length(t.unique))
{
alpha[i] <- sum(t[delta==1]==t.unique[i])/
sum(exp(f.x[t>=t.unique[i]]))
}
if(!smooth && !cumulative)
{
if(!is.null(t.eval))
{
stop("Cannot evaluate unsmoothed baseline hazard at t.eval.")
}
} else
if(smooth && !cumulative)
{
lambda.smooth <- supsmu(t.unique,alpha)
} else
if(smooth && cumulative)
{
lambda.smooth <- supsmu(t.unique,cumsum(alpha))
} else # (!smooth && cumulative) - THE DEFAULT
{
lambda.smooth <- list(x=t.unique,y=cumsum(alpha))
}
if(!is.null(t.eval))
{
obj <- approx(lambda.smooth$x,lambda.smooth$y,xout=t.eval)$y
} else
{
obj <- approx(lambda.smooth$x,lambda.smooth$y,xout=t)$y
}
return(obj)
}