Browse Source

Adding kaplan.meier.stats function for getting statistical significance without plotting

Andrej 1 month ago
parent
commit
2080d7f738
3 changed files with 53 additions and 11 deletions
  1. 1 0
      NAMESPACE
  2. 33 11
      R/km.R
  3. 19 0
      man/kaplan.meier.stats.Rd

+ 1 - 0
NAMESPACE

@@ -3,6 +3,7 @@
 export(convert.to.sul)
 export(kaplan.meier)
 export(kaplan.meier.plot.gg)
+export(kaplan.meier.stats)
 export(mapNA)
 export(remapVariable)
 export(selectValid)

+ 33 - 11
R/km.R

@@ -11,25 +11,25 @@
 kaplan.meier<-function(x,var,comment=''){
 	#x should have followup and censored columns	
 	surv.obj<-survival::Surv(x$followup,x$censored)
-    m=base::max(x$followup)
+   m=base::max(x$followup)
 
 	f<-stats::as.formula(paste('surv.obj',var,sep='~'))
 	s1<-survival::survfit(f,data=x)
 	#str(s1)
-    tit=base::sprintf('Kaplan-Meier plot by %s',var)
-    cols=base::c('red','blue')
-    labels=base::c(base::sprintf('%s=true',var),base::sprintf('%s=false',var))
+   tit=base::sprintf('Kaplan-Meier plot by %s',var)
+   cols=base::c('red','blue')
+   labels=base::c(base::sprintf('%s=true',var),base::sprintf('%s=false',var))
 	#plot(s1,mark.time=TRUE,col=c('red','blue'),pch=labels,main=tit)
 	graphics::plot(s1,mark.time=TRUE,col=c('red','blue'),main=tit)
 	s=survival::survdiff(f,data=x)
     #str(s)
-    p=stats::pchisq(s$chisq, length(s$n)-1, lower.tail = FALSE)
-	 sv=base::sprintf("p=%.3f",p)
-    nLab=base::sprintf('N=%d',nrow(x))
-    graphics::text(x=c(0.9*m,0.3*m,0.9*m),y=c(0.2,0.1,0.3),label=c(sv,comment,nLab),cex=1.2)
-    lLab <- base::gsub("x=","",base::names(s1$strata))  ## legend labels
-    graphics::legend("top",legend=lLab,col=cols,lty=c(1,1),horiz=FALSE, bty='n')
-    p
+   p=stats::pchisq(s$chisq, length(s$n)-1, lower.tail = FALSE)
+   sv=base::sprintf("p=%.3f",p)
+   nLab=base::sprintf('N=%d',nrow(x))
+   graphics::text(x=c(0.9*m,0.3*m,0.9*m),y=c(0.2,0.1,0.3),label=c(sv,comment,nLab),cex=1.2)
+   lLab <- base::gsub("x=","",base::names(s1$strata))  ## legend labels
+   graphics::legend("top",legend=lLab,col=cols,lty=c(1,1),horiz=FALSE, bty='n')
+   p
 }
 
 set.from.list<-function(var,default,...){
@@ -39,6 +39,28 @@ set.from.list<-function(var,default,...){
    result
 }
 
+#' Evaluate a KM plot
+#'
+#' @param x a data frame that contains followup and censored columns
+#' @param var a categorical variable to split the data to sub-curves
+#'
+#' @return p probability that curves split by var differ significantly
+#'
+#' @export
+
+kaplan.meier.stats<-function(x,var){
+	#x should have followup and censored columns	
+	surv.obj<-survival::Surv(x$followup,x$censored)
+   m=base::max(x$followup)
+
+	f<-stats::as.formula(paste('surv.obj',var,sep='~'))
+	s1<-survival::survfit(f,data=x)
+	s=survival::survdiff(f,data=x)
+   p=stats::pchisq(s$chisq, length(s$n)-1, lower.tail = FALSE)
+   p
+}
+
+
 #' Plot a Kaplan-Meier curve with ggsurvfit
 #'
 #'@param x data frame containing followup and censored column

+ 19 - 0
man/kaplan.meier.stats.Rd

@@ -0,0 +1,19 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/km.R
+\name{kaplan.meier.stats}
+\alias{kaplan.meier.stats}
+\title{Evaluate a KM plot}
+\usage{
+kaplan.meier.stats(x, var)
+}
+\arguments{
+\item{x}{a data frame that contains followup and censored columns}
+
+\item{var}{a categorical variable to split the data to sub-curves}
+}
+\value{
+p probability that curves split by var differ significantly
+}
+\description{
+Evaluate a KM plot
+}