Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangt369 committed Aug 26, 2021
1 parent ed64f53 commit ec6cea4
Show file tree
Hide file tree
Showing 38 changed files with 683 additions and 2,983 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: SurvivalPath
Type: Package
Title: Survival Path models for time-series survival data
Version: 1.1.0
Version: 1.1.1
Author: Shen Lujun and Zhang Tao
Maintainer: Zhang Tao <[email protected]>
Description: Build the survival path of patients with a single disease.
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ export(matchsubgroup)
export(plotKM)
export(survivalpath)
export(timedivision)
import(dplyr)
import(survival)
import(survivalROC)
import(survminer)
import(treeio)
Binary file removed R.rar
Binary file not shown.
38 changes: 19 additions & 19 deletions R/EvolutionAfterTreatment.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

#'@title Calculate the number of people (proportion) assigned to different branches after a certain
#'@title Calculate the number of participants (proportion) assigned to different sub-nodes after a certain
#'node is treated by a specified plan
#'@description Calculate the number of people (proportion) assigned to different branches after a
#'@description Calculate the number of people (proportion) assigned to different sub-nodes after a
#'certain node is treated by a specified plan
#'@usage EvolutionAfterTreatment(
#'df,
Expand All @@ -15,14 +15,14 @@
#'@param mytree "mytree" in the return result of the survivalpath function
#'@param source Raw data
#'@param treatment Choose treatment
#'@details By specifying the node, using mytree to calculate the relevant branch variables and subsequent nodes,
#'and using the original data to calculate, filter out the group of subjects who branch to the next node after
#'the node adopts different treatment plans. Calculate the number of people assigned to different nodes according
#'@details By specifying the node, using mytree to calculate the relevant bifurcation variables and subsequent nodes,
#'and using the original data to calculate the number or proportion of subjects who branch to each sub-nodes after
#'the participants in parent-node adopts different treatment plans. Calculate the number of people assigned to different sub-nodes according
#'to different treatment methods (proportion).
#'@return A dataframe whose rows and columns are the next node and treatment plan
#'@return A dataframe whose rows and columns are the next node and treatment plan, respectively
#'@export
#'@examples data("dataset")
#'dataset = timedivision(X2021data,"ID","Date",period = 90,left_interval = 0.5,right_interval=1.5)
#'dataset = timedivision(X2021data,"ID","Date",period = 90,left_interval = 0.5,right_interval=0.5)
#'
#'time <- list()
#'status <- list()
Expand All @@ -32,11 +32,11 @@
#'treatment <- list()
#'for (i in 1:10){
#'
#' data <- dataset[dataset['timenode']==i,]
#' data <- dataset[dataset['time_slice']==i,]
#'
#' time <- c(time,list(data['OStime_new']))
#'
#' status <- c(status,list(data['Status_new']))
#' status <- c(status,list(data['Status_of_death']))
#'
#' tsid <- c(tsid,list(data['ID']))
#'
Expand All @@ -46,14 +46,14 @@
#'
#' tsdata <- c(tsdata,list(c_data))
#'
#' c_treatment <- subset(data, select = c("Treatment2"))
#' c_treatment <- subset(data, select = c("Resection"))
#'
#' treatment <- c(treatment,list(c_treatment))
#'}
#'
#'tsdata <- classifydata(time,status,tsdata,tsid,cutoff=365*1)
#'tsdata <- classifydata(time,status,tsdata,tsid,predict.time=365*1)
#'
#'result <- survivalpath(time,status,tsdata[[1]],tsid,time_slices = 10,treatments = treatment,p.value=0.05,degreeofcorrelation=0.7)
#'result <- survivalpath(time,status,tsdata[[1]],tsid,time_slices = 8,treatments = treatment,p.value=0.05,degreeofcorrelation=0.7)
#'
#'mytree <- result$tree
#'ggtree(mytree, color="black",linetype=1,size=1.2,ladderize = T, )+
Expand All @@ -71,15 +71,15 @@
#' theme(legend.title=element_blank(),legend.position = c(0.1,0.9))
#'
#'#plot KM curve
#'treepoints = c(16,23)
#'treepoints = c(32,55)
#'plotKM(result$data, treepoints,mytree,risk.table=T)
#'#Comparing the efficacy of treatment methods by drawing survival curves
#'treepoints = c(17,22)
#'compareTreatmentPlans(result$data, treepoints,mytree,dataset,"Treatment")
#'treepoints = c(32,55)
#'compareTreatmentPlans(result$data, treepoints,mytree,dataset,"Resection")
#'
#'treepoint=16
#'A = EvolutionAfterTreatment(result$data, treepoint,mytree,dataset,"Treatment")
#'mytable <- xtabs(~ `Treatment2`+treepoint, data=A)
#'treepoint=32
#'A = EvolutionAfterTreatment(result$data, treepoint,mytree,dataset,"Resection")
#'mytable <- xtabs(~ `Resection`+treepoint, data=A)
#'prop.table(mytable,1)
#'

Expand Down Expand Up @@ -179,7 +179,7 @@ getnodePatients <- function(df,treepoint,mytree,source,treatment){

result <- newdf[,1:3]

sourcedata <- source[which(source$timenode==time_slice-1),]
sourcedata <- source[which(source$time_slice==time_slice-1),]
#print(dim(sourcedata))
sourcedata <- sourcedata[sourcedata$ID %in% result$ID,]

Expand Down
36 changes: 19 additions & 17 deletions R/classifydata.R
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
#'@title The same classification of different risk factors at each time node
#'@title Reclassify variables into binary variables based on survivalROC
#'@description Based on the survivalROC package, using the survival time
#'and survival state, calculate the optimal threshold for the binary classification
#'of each variable of the initial node. According to the threshold, the variables of
#'and survival state, calculate the optimal cutoff for the binary classification
#'of each variable using data at the first time slice. According to the cutoff, the variables of
#' different nodes are classified into two categories.
#'@usage classifydata(
#'time,
#'status,
#'timeslicedata,
#'tspatientid,
#'cutoff,
#'predict.time,
#'isfill=T
#')
#'@param time Event time or censoring time for subjects
#'@param status Indicator of status, 1 if death or event, 0 otherwise
#'@param timeslicedata list object; in turn include the risk factors of subjects at different time nodes
#'@param tspatientid list object; The unique identification number corresponding to the subject at each time node
#'@param predict.time Time point of the ROC curve
#'@param timeslicedata list object; each dataframe of the list represent data at individual time slices
#'@param tspatientid list object; The unique identification number corresponding to the subject at each time slice
#'@param predict.time Time point to predict during drawing the ROC curve
#'@param isfill Logical value, used to confirm whether to fill in missing data. If it is True, then fill
#'@details According to the first time node, the survival data is used to calculate the optimal classification threshold
#'of different risk factors. The risk factor threshold calculated at the first time node is used to calculate
Expand All @@ -26,6 +26,7 @@
#'@return cutoff Optimal classification threshold
#'@seealso survivalROC
#'@export
#'@import survivalROC
#'@examples
#'data("dataset")
#'
Expand All @@ -43,11 +44,11 @@
#'
#'for (i in 1:10){
#'
#' data <- dataset[dataset['timenode']==i,]
#' data <- dataset[dataset['time_slice']==i,]
#'
#' time <- c(time,list(data['OStime_new']))
#'
#' status <- c(status,list(data['Status_new']))
#' status <- c(status,list(data['Status_of_death']))
#'
#' tsid <- c(tsid,list(data['ID']))
#'
Expand All @@ -57,14 +58,14 @@
#'
#' tsdata <- c(tsdata,list(c_data))
#'
#' c_treatment <- subset(data, select = c("Treatment2"))
#' c_treatment <- subset(data, select = c("Resection"))
#'
#' treatment <- c(treatment,list(c_treatment))
#'}
#'
#'
#' # cutoff
#'tsdata <- classifydata(time,status,tsdata,tsid,cutoff=365*1)
#' # predict.time
#'tsdata <- classifydata(time,status,tsdata,tsid,predict.time=365*1)
#'


Expand All @@ -75,7 +76,7 @@ classifydata <- function(time,status,timeslicedata,tspatientid,predict.time,isfi
variables <- timeslicedata[[1]]

result <- list()
predict.time <- list()
cutoff <- list()
cut.variable <- list()

for (v in 1:length(varnames)){
Expand Down Expand Up @@ -113,16 +114,17 @@ classifydata <- function(time,status,timeslicedata,tspatientid,predict.time,isfi
timeslicedata[[i]][[v]] <- ifelse(timeslicedata[[i]][[v]] <= thresholds,0,1)
}

predict.time <- c(predict.time,thresholds)
cutoff <- c(cutoff,thresholds)
#print(cutoff)
cut.variable <- c(cut.variable,varn)
}

names(predict.time) <- cut.variable
predict.time <- data.frame(predict.time)
cutoff <- data.frame(cutoff)
names(cutoff) <- cut.variable

}

(result$timeslicedata <- timeslicedata)
(result$predict.time <- predict.time)
(result$cutoff <- cutoff)
result
}
40 changes: 22 additions & 18 deletions R/compareTreatmentPlans.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
# Test Package: 'Ctrl + Shift + T'


#'@title Draw the KM curve of different nodes after the specified treatment plan
#'@title Compare and Draw the KM curve of different nodes based on specified treatment plan
#'@description Based on the survival tree, specify nodes and specify treatment methods,
#'draw survival curves to compare prognostic effects.
#'draw survival curves to compare treatments
#'@usage compareTreatmentPlans(
#'df,
#'treepoints,
Expand All @@ -16,15 +16,15 @@
#'treatment
#')
#'@param df "data" in the return result of the survivalpath function
#'@param treepoints list object;Specify the node for drawing the KM curve, which is in the survival tree
#'@param treepoints list object;Specify the node for drawing the KM curve, which is displayed in the survival path graphs
#'@param mytree "mytree" in the return result of the survivalpath function
#'@param source Raw data
#'@param treatment Choose treatment
#'@param treatment Choose treatment variable
#'@details By specifying the node, using mytree to calculate the previous branch variables,
#'and calculating with the original data, the group of subjects with different treatment plans
#'at different nodes can be screened out. Draw the survival curve of patients based on the selected groups
#'@seealso survminer
#'
#'@import survival
#'@export
#'@examples
#'data("dataset")
Expand All @@ -38,11 +38,11 @@
#'treatment <- list()
#'for (i in 1:10){
#'
#' data <- dataset[dataset['timenode']==i,]
#' data <- dataset[dataset['time_slice']==i,]
#'
#' time <- c(time,list(data['OStime_new']))
#'
#' status <- c(status,list(data['Status_new']))
#' status <- c(status,list(data['Status_of_death']))
#'
#' tsid <- c(tsid,list(data['ID']))
#'
Expand All @@ -52,12 +52,12 @@
#'
#' tsdata <- c(tsdata,list(c_data))
#'
#' c_treatment <- subset(data, select = c("Treatment2"))
#' c_treatment <- subset(data, select = c("Resection"))
#'
#' treatment <- c(treatment,list(c_treatment))
#'}
#'
#'tsdata <- classifydata(time,status,tsdata,tsid,cutoff=365*1)
#'tsdata <- classifydata(time,status,tsdata,tsid,predict.time=365*1)
#'
#'result <- survivalpath(time,status,tsdata[[1]],tsid,time_slices = 10,treatments = treatment,p.value=0.05,degreeofcorrelation=0.7)
#'
Expand All @@ -81,7 +81,7 @@
#'plotKM(result$data, treepoints,mytree,risk.table=T)
#'#Comparing the efficacy of treatment methods by drawing survival curves
#'treepoints = c(17,22)
#'compareTreatmentPlans(result$data, treepoints,mytree,dataset,"Treatment")
#'compareTreatmentPlans(result$data, treepoints,mytree,dataset,"Resection")
#'

compareTreatmentPlans <- function(df,treepoints,mytree,source,treatment){
Expand All @@ -91,9 +91,13 @@ compareTreatmentPlans <- function(df,treepoints,mytree,source,treatment){
newdf$treepoint <- d
data.df <- rbind(data.df,newdf)
}
#print(table(data.df[,3:4]))
name <- names(data.df)
fit <- survfit(Surv( time,status)~Treatment2+treepoint, data = data.df)

formula <- paste("Surv(time,status)~",paste(names(data.df)[3:4],collapse ="+") )

formula <- as.formula(formula)

fit <- surv_fit(formula,data=data.df)

ggsurvplot(fit,data = data.df,
pval = TRUE,
#conf.int = TRUE,
Expand Down Expand Up @@ -136,7 +140,7 @@ getPatients <- function(df,treepoint,mytree,source,treatment){
}
#get treepoint survival periods
time_slice <- length(path)
print(path)
#print(path)

for (i in time_slice:1){

Expand Down Expand Up @@ -175,14 +179,14 @@ getPatients <- function(df,treepoint,mytree,source,treatment){
}

result <- newdf[,1:3]
print(dim(result))
#print(dim(result))

sourcedata <- source[which(source$timenode==time_slice-1),]
print(dim(sourcedata))
sourcedata <- source[which(source$time_slice==time_slice-1),]
#print(dim(sourcedata))

sourcedata <- sourcedata[sourcedata$ID %in% result$ID,]

result <- subset(sourcedata,select=c("OStime_new","Status_new"))
result <- subset(sourcedata,select=c("OStime_new","Status_of_death"))
names(result) <- c("time","status")
result <- cbind(result,sourcedata[,treatment])

Expand Down
Loading

0 comments on commit ec6cea4

Please sign in to comment.