浏览代码

Adding cox calcluation functions

Andrej 2 周之前
父节点
当前提交
a352418f7c
共有 6 个文件被更改,包括 113 次插入5 次删除
  1. 2 0
      NAMESPACE
  2. 67 0
      R/cox.R
  3. 3 2
      R/km.R
  4. 19 0
      man/cox.hazard.Rd
  5. 19 0
      man/cox.univariate.Rd
  6. 3 3
      man/set.from.arg.list.Rd

+ 2 - 0
NAMESPACE

@@ -2,6 +2,8 @@
 
 export(convert.to.date)
 export(convert.to.sul)
+export(cox.hazard)
+export(cox.univariate)
 export(kaplan.meier)
 export(kaplan.meier.plot.gg)
 export(kaplan.meier.stats)

+ 67 - 0
R/cox.R

@@ -0,0 +1,67 @@
+#' COX hazards
+#'
+#' @param x data frame
+#' @param vars a vector of variables used in Cox's survival analysis
+#'
+#' @return model as returned by coxph
+#'
+#' @export 
+
+cox.hazard<-function(x,vars){
+	vlist=base::paste(vars,collapse='+')
+	f<-stats::as.formula(base::paste('Surv(followup,censored)',vlist,sep='~'))
+   s=survival::coxph(f,data=x)
+   s
+}
+
+#' COX for univariate variables
+#'
+#' @param x data frame containing followup and censored columns
+#' @param var the variable used to explain survival in format mode:name where mode drives unitChange selection, SD for m, 1 for r or logic and X for uX
+#'
+#' @return data frame with a single row for tested variable, with fields variableName,unitChange,HR,l95,u95,pWald,pLikelihood
+#'
+#' @export 
+
+cox.univariate<-function(x,var){
+   data=base::c(var)
+   qv=base::strsplit(data,split=':')[[1]]
+   mode=qv[1]
+   v=qv[2]
+   #print(sprintf('Calculating for %s',var))
+   s=cox.hazard(x,v)
+   s1<-base::summary(s)  
+  
+   #calculate survival probability at mean or prescribed value, which is given as false for binary and as predifined value with categorical data
+   if (mode=='m'){
+      #use sd as a unit change
+      x.dx<-stats::sd(x[,v],na.rm=TRUE)
+   }
+   if (base::substr(mode,1,1)=='u'){
+       #use specified unit
+      x.dx=base::as.numeric(base::substring(mode,2))
+   }
+   
+   if (mode=='logic'){
+       #unit is 1 
+      x.dx=1
+       
+   }
+   if (substr(mode,1,1)=='r'){
+       #the same as for logic
+      x.dx=1
+   }
+   	#adjust output data
+   sd=base::sqrt(s$var)*x.dx
+   c=s1$coef[1]*x.dx
+   l95=base::round(base::exp(c-1.96*sd),2)
+   u95=base::round(base::exp(c+1.96*sd),2)
+   HR=base::round(base::exp(c),2)
+   pWald=base::round(s1$coefficients[1,5],2)
+   pLikelihood=base::round(s1$logtest['pvalue'],3)
+   unitChange=base::round(x.dx,2)
+#return as data frame with a single row
+   base::data.frame(varName=v,unitChange=unitChange,HR=HR,low95=l95,up95=u95,pWald=pWald,pLikelihood=pLikelihood)
+  
+}
+

+ 3 - 2
R/km.R

@@ -41,9 +41,10 @@ set.from.list<-function(var,default,...){
 
 #' Set argument value from list or to default value
 #'
-#' @param name of value in list
+#' @param var name of value in list
 #' @param default value
-#' @param list of values, such as labkey.url.params
+#' @param z of values, such as labkey.url.params
+#'
 #' @return value from list or default if missing
 #'
 #' @export 

+ 19 - 0
man/cox.hazard.Rd

@@ -0,0 +1,19 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/cox.R
+\name{cox.hazard}
+\alias{cox.hazard}
+\title{COX hazards}
+\usage{
+cox.hazard(x, vars)
+}
+\arguments{
+\item{x}{data frame}
+
+\item{vars}{a vector of variables used in Cox's survival analysis}
+}
+\value{
+model as returned by coxph
+}
+\description{
+COX hazards
+}

+ 19 - 0
man/cox.univariate.Rd

@@ -0,0 +1,19 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/cox.R
+\name{cox.univariate}
+\alias{cox.univariate}
+\title{COX for univariate variables}
+\usage{
+cox.univariate(x, var)
+}
+\arguments{
+\item{x}{data frame containing followup and censored columns}
+
+\item{var}{the variable used to explain survival in format mode:name where mode drives unitChange selection, SD for m, 1 for r or logic and X for uX}
+}
+\value{
+data frame with a single row for tested variable, with fields variableName,unitChange,HR,l95,u95,pWald,pLikelihood
+}
+\description{
+COX for univariate variables
+}

+ 3 - 3
man/set.from.arg.list.Rd

@@ -7,11 +7,11 @@
 set.from.arg.list(var, default, z)
 }
 \arguments{
-\item{default}{value}
+\item{var}{name of value in list}
 
-\item{name}{of value in list}
+\item{default}{value}
 
-\item{list}{of values, such as labkey.url.params}
+\item{z}{of values, such as labkey.url.params}
 }
 \value{
 value from list or default if missing