Created
January 24, 2018 01:13
-
-
Save abikoushi/b9c56028929dc0720e27e36d784a027e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
make_df4hist <-function(sf,bw){ | |
nb <- ceiling(max(sf$time)/bw) | |
if(is.null(sf$strata)){ | |
sfdf <- data.frame(time=sf$time,survival=sf$surv, | |
stringsAsFactors = FALSE) | |
df4hist <- sfdf %>% | |
mutate(time=cut(time,0:nb*bw)) %>% | |
group_by(time) %>% | |
summarise(TP=diff(range(survival))) %>% | |
mutate(density=TP/bw) %>% | |
ungroup() %>% | |
mutate(midtime=(as.integer(time)-1/2)*bw) | |
}else{ | |
sfdf <- data.frame(time=sf$time,survival=sf$surv, | |
strata=rep(names(sf$strata),sf$strata), | |
stringsAsFactors = FALSE) | |
df4hist <- sfdf %>% | |
mutate(time=cut(time,0:nb*bw)) %>% | |
group_by(strata,time) %>% | |
summarise(TP=diff(range(survival))) %>% | |
mutate(density=TP/bw) %>% | |
ungroup() %>% | |
mutate(midtime=(as.integer(time)-1/2)*bw) | |
tmp <- do.call("rbind",strsplit(df4hist$strata,", |=")) | |
n_strata <- ncol(tmp)/2 | |
values=tmp[,2*1:n_strata,drop=FALSE] | |
colnames(values)<-tmp[1,2*1:n_strata-1] | |
df4hist <- data.frame(df4hist,values,stringsAsFactors = FALSE) | |
} | |
return(df4hist) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment