Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Last active August 29, 2015 14:12
Show Gist options
  • Save abikoushi/0f860c69b1c9184acedd to your computer and use it in GitHub Desktop.
Save abikoushi/0f860c69b1c9184acedd to your computer and use it in GitHub Desktop.
plot a censored data histgram
cdh <- function(sf, bw=NULL, digits =0, strata=1, col="white",main=NULL){
if(class(sf)!="survfit"){
cat("survfitオブジェクトを入れてください。\n")
}
if(is.null(sf$strata)){
DT =sf$time
surv =sf$surv
}else{
if(strata==1){
DT =sf$time[1:sf$strata[1]]
surv =sf$surv[1:sf$strata[1]]
}else{
DT =sf$time[(sf$strata[strata-1]+1):(sf$strata[strata-1]+sf$strata[strata])]
surv =sf$surv[(sf$strata[strata-1]+1):(sf$strata[strata-1]+sf$strata[strata])]
}
}
if(is.null(bw)){
#Scott's choice
bw <- zapsmall((3.5*sd(DT))/length(DT)^(1/3), digits=digits)
if(bw==0){bw=0.5} #(>_<)
}
nb <- ceiling((max(DT)-min(DT))/bw)
bins <- (1:nb)*bw
tab1 <- data.frame(DT, surv)
TP <- rev(diff(c(sapply(length(bins):1, function(i) min(tab1[tab1$DT <= bins[i],2])),1)))
barplot(TP/bw, space=0, col=col, main=main)
axis(side=1,labels=c(0, bins),at=0:nb)
invisible(TP)
}
@abikoushi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment