Pārlūkot izejas kodu

Adding basic routines

Andrej 2 dienas atpakaļ
vecāks
revīzija
06229ffc91
14 mainītis faili ar 407 papildinājumiem un 2 dzēšanām
  1. 11 0
      NAMESPACE
  2. 173 0
      R/stats.R
  3. 23 0
      man/get.chisq.Rd
  4. 23 0
      man/get.contingency.Rd
  5. 19 0
      man/get.events.Rd
  6. 23 0
      man/get.fisher.Rd
  7. 17 0
      man/get.mean.Rd
  8. 17 0
      man/get.median.Rd
  9. 17 0
      man/get.medianQ.Rd
  10. 19 0
      man/get.portion.Rd
  11. 21 0
      man/get.series.Rd
  12. 21 0
      man/get.t.Rd
  13. 21 0
      man/get.u.Rd
  14. 2 2
      man/simple.plotROCgg.Rd

+ 11 - 0
NAMESPACE

@@ -8,8 +8,19 @@ export(convert.to.date)
 export(convert.to.sul)
 export(cox.hazard)
 export(cox.univariate)
+export(get.chisq)
+export(get.contingency)
+export(get.events)
+export(get.fisher)
 export(get.ggplot)
 export(get.ggtext)
+export(get.mean)
+export(get.median)
+export(get.medianQ)
+export(get.portion)
+export(get.series)
+export(get.t)
+export(get.u)
 export(kaplan.meier)
 export(kaplan.meier.plot.gg)
 export(kaplan.meier.stats)

+ 173 - 0
R/stats.R

@@ -0,0 +1,173 @@
+#' Construct a string to report mean and SD
+
+#' @param v a vector of values
+#'
+#' @return a combined string
+#'
+#' @export
+
+get.mean<-function(v){
+   base::sprintf('%.0f (%.0f)',base::mean(v,na.rm=TRUE),stats::sd(v,na.rm=TRUE))
+}
+
+#' Construct a string to report median and range
+#'
+#' @param v a vector of values
+#'
+#' @return a combined string
+#'
+#' @export
+
+get.median<-function(v){
+   base::sprintf('%.0f (%.0f-%.0f)',stats::median(v,na.rm=TRUE),base::min(v,na.rm=TRUE),base::max(v,na.rm=TRUE))
+}
+
+#' Construct a string to report median and quartiles
+#'
+#' @param v a vector of values
+#'
+#' @return a combined string
+#'
+#' @export
+
+
+get.medianQ<-function(v){
+   q=base::c(0.25,0.75)
+   qv=stats::quantile(v,probs=q,na.rm=TRUE)
+   base::print(qv[1])
+   base::sprintf('%.0f (%.0f-%.0f)',stats::median(v,na.rm=TRUE),qv[1],qv[2])
+}
+
+#' Construct a string to report count and portion that match comma separated string list
+#'
+#' @param v a vector of values
+#' @param val comma separated list of values 
+#'
+#' @return a combined string
+#'
+#' @export
+
+get.portion<-function(v,val){
+   vals<-base::as.integer(base::strsplit(base::as.character(val),',')[[1]])
+   n1=base::length(v[base::is.element(v,vals)])
+   base::sprintf('%.0f (%.0f %%)',n1,100*n1/base::length(v))
+}
+
+#' Report count of elements in v that match comma separated string list
+#'
+#' @param v a vector of values
+#' @param val comma separated list of values 
+#'
+#' @return a combined string
+#'
+#' @export
+
+get.events<-function(v,val){
+   vals<-base::as.integer(base::strsplit(base::as.character(val),',')[[1]])
+   base::length(v[base::is.element(v,vals)])
+}
+
+#' Repeat function and merge output in a vector
+#'
+#' @param func function to perform, with elements v and a key k
+#' @param v parameter of the func
+#' @param keys a set of k values for the func
+#'
+#' @return a combined output
+#'
+#' @export
+
+get.series<-function(func,v,keys=c()){
+   out=base::c()
+   for (k in keys){
+      out=base::c(out,func(v,k))
+   }
+   out
+}
+
+#' Construct a contingency table
+#'
+#' @param df data frame with entries selected by an outcome variable
+#' @param df1 complementary data frame to df
+#' @param var variable to test dependency for
+#' @param keys possible values of var
+#'
+#' @return contingency table as a data frame, rows are keys, and columns are v for df and v1 for df1
+#'
+#' @export
+
+
+get.contingency<-function(df,df1,var,keys){
+   v<-base::c()
+   v1<-base::c()
+   #for (k in names(z)){
+       #keys=z[[k]][,'Key']
+   v<-base::c(v,get.series(get.events,df[,var],keys))
+   v1<-base::c(v1,get.series(get.events,df1[,var],keys))
+   if (base::length(keys)==1){
+       v<-base::c(v,base::nrow(df)-v[1])
+       v1<-base::c(v1,base::nrow(df1)-v1[1])
+   }   
+   base::data.frame(v=v,v1=v1)
+}
+
+#' Determine statistical significance of df/df1 splitting for variable using chi-square test
+#'
+#' @param df data frame with entries selected by an outcome variable
+#' @param df1 complementary data frame to df
+#' @param var variable to test dependency for
+#' @param keys possible values of var
+#'
+#' @return chisq.test output
+#'
+#' @export
+
+get.chisq<-function(df,df1,var,keys){
+   cf<-get.contingency(df,df1,var,keys)
+   stats::chisq.test(cf,simulate.p.value=TRUE)$p.value
+}
+
+#' Determine statistical significance of df/df1 splitting for variable using FIsher's exact test
+#'
+#' @param df data frame with entries selected by an outcome variable
+#' @param df1 complementary data frame to df
+#' @param var variable to test dependency for
+#' @param keys possible values of var
+#'
+#' @return fisher.test output
+#'
+#' @export
+
+get.fisher<-function(df,df1,var,keys){
+   cf<-get.contingency(df,df1,var,keys)
+   stats::fisher.test(cf,simulate.p.value=TRUE)$p.value
+}
+
+#' Determine statistical significance of df/df1 splitting for a continous variable (MWU/Wilcox)
+#'
+#' @param df data frame with entries selected by an outcome variable
+#' @param df1 complementary data frame to df
+#' @param var variable to test dependency for
+#'
+#' @return wilcox.test output
+#'
+#' @export
+
+get.u<-function(df,df1,var){
+   stats::wilcox.test(df[,var],df1[,var])$p.value
+}
+
+#' Determine statistical significance of df/df1 splitting for a continous variable (T-test) 
+#'
+#' @param df data frame with entries selected by an outcome variable
+#' @param df1 complementary data frame to df
+#' @param var variable to test dependency for
+#'
+#' @return wilcox.test output
+#'
+#' @export
+
+get.t<-function(df,df1,var){
+   stats::t.test(df[,var],df1[,var])$p.value
+}
+

+ 23 - 0
man/get.chisq.Rd

@@ -0,0 +1,23 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.chisq}
+\alias{get.chisq}
+\title{Determine statistical significance of df/df1 splitting for variable using chi-square test}
+\usage{
+get.chisq(df, df1, var, keys)
+}
+\arguments{
+\item{df}{data frame with entries selected by an outcome variable}
+
+\item{df1}{complementary data frame to df}
+
+\item{var}{variable to test dependency for}
+
+\item{keys}{possible values of var}
+}
+\value{
+chisq.test output
+}
+\description{
+Determine statistical significance of df/df1 splitting for variable using chi-square test
+}

+ 23 - 0
man/get.contingency.Rd

@@ -0,0 +1,23 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.contingency}
+\alias{get.contingency}
+\title{Construct a contingency table}
+\usage{
+get.contingency(df, df1, var, keys)
+}
+\arguments{
+\item{df}{data frame with entries selected by an outcome variable}
+
+\item{df1}{complementary data frame to df}
+
+\item{var}{variable to test dependency for}
+
+\item{keys}{possible values of var}
+}
+\value{
+contingency table as a data frame, rows are keys, and columns are v for df and v1 for df1
+}
+\description{
+Construct a contingency table
+}

+ 19 - 0
man/get.events.Rd

@@ -0,0 +1,19 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.events}
+\alias{get.events}
+\title{Report count of elements in v that match comma separated string list}
+\usage{
+get.events(v, val)
+}
+\arguments{
+\item{v}{a vector of values}
+
+\item{val}{comma separated list of values}
+}
+\value{
+a combined string
+}
+\description{
+Report count of elements in v that match comma separated string list
+}

+ 23 - 0
man/get.fisher.Rd

@@ -0,0 +1,23 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.fisher}
+\alias{get.fisher}
+\title{Determine statistical significance of df/df1 splitting for variable using FIsher's exact test}
+\usage{
+get.fisher(df, df1, var, keys)
+}
+\arguments{
+\item{df}{data frame with entries selected by an outcome variable}
+
+\item{df1}{complementary data frame to df}
+
+\item{var}{variable to test dependency for}
+
+\item{keys}{possible values of var}
+}
+\value{
+fisher.test output
+}
+\description{
+Determine statistical significance of df/df1 splitting for variable using FIsher's exact test
+}

+ 17 - 0
man/get.mean.Rd

@@ -0,0 +1,17 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.mean}
+\alias{get.mean}
+\title{Construct a string to report mean and SD}
+\usage{
+get.mean(v)
+}
+\arguments{
+\item{v}{a vector of values}
+}
+\value{
+a combined string
+}
+\description{
+Construct a string to report mean and SD
+}

+ 17 - 0
man/get.median.Rd

@@ -0,0 +1,17 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.median}
+\alias{get.median}
+\title{Construct a string to report median and range}
+\usage{
+get.median(v)
+}
+\arguments{
+\item{v}{a vector of values}
+}
+\value{
+a combined string
+}
+\description{
+Construct a string to report median and range
+}

+ 17 - 0
man/get.medianQ.Rd

@@ -0,0 +1,17 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.medianQ}
+\alias{get.medianQ}
+\title{Construct a string to report median and quartiles}
+\usage{
+get.medianQ(v)
+}
+\arguments{
+\item{v}{a vector of values}
+}
+\value{
+a combined string
+}
+\description{
+Construct a string to report median and quartiles
+}

+ 19 - 0
man/get.portion.Rd

@@ -0,0 +1,19 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.portion}
+\alias{get.portion}
+\title{Construct a string to report count and portion that match comma separated string list}
+\usage{
+get.portion(v, val)
+}
+\arguments{
+\item{v}{a vector of values}
+
+\item{val}{comma separated list of values}
+}
+\value{
+a combined string
+}
+\description{
+Construct a string to report count and portion that match comma separated string list
+}

+ 21 - 0
man/get.series.Rd

@@ -0,0 +1,21 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.series}
+\alias{get.series}
+\title{Repeat function and merge output in a vector}
+\usage{
+get.series(func, v, keys = c())
+}
+\arguments{
+\item{func}{function to perform, with elements v and a key k}
+
+\item{v}{parameter of the func}
+
+\item{keys}{a set of k values for the func}
+}
+\value{
+a combined output
+}
+\description{
+Repeat function and merge output in a vector
+}

+ 21 - 0
man/get.t.Rd

@@ -0,0 +1,21 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.t}
+\alias{get.t}
+\title{Determine statistical significance of df/df1 splitting for a continous variable (T-test)}
+\usage{
+get.t(df, df1, var)
+}
+\arguments{
+\item{df}{data frame with entries selected by an outcome variable}
+
+\item{df1}{complementary data frame to df}
+
+\item{var}{variable to test dependency for}
+}
+\value{
+wilcox.test output
+}
+\description{
+Determine statistical significance of df/df1 splitting for a continous variable (T-test)
+}

+ 21 - 0
man/get.u.Rd

@@ -0,0 +1,21 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/stats.R
+\name{get.u}
+\alias{get.u}
+\title{Determine statistical significance of df/df1 splitting for a continous variable (MWU/Wilcox)}
+\usage{
+get.u(df, df1, var)
+}
+\arguments{
+\item{df}{data frame with entries selected by an outcome variable}
+
+\item{df1}{complementary data frame to df}
+
+\item{var}{variable to test dependency for}
+}
+\value{
+wilcox.test output
+}
+\description{
+Determine statistical significance of df/df1 splitting for a continous variable (MWU/Wilcox)
+}

+ 2 - 2
man/simple.plotROCgg.Rd

@@ -10,7 +10,7 @@ simple.plotROCgg(
   cols,
   x = 0.7,
   y = 0.3,
-  unit = "ml",
+  units = c(),
   precise = "FALSE",
   target = "alive"
 )
@@ -26,7 +26,7 @@ simple.plotROCgg(
 
 \item{y}{coordinate for legend}
 
-\item{unit}{unit for threshold in labels}
+\item{units}{units for threshold in labels}
 
 \item{precise}{number of decimal places to use when reporting opt threshold, TRUE:2, FALSE:0}