Browse code

add feature for probability of a degree of response or greater given response.

gfinak authored on 08/05/2019 15:39:06
Showing 3 changed files

... ...
@@ -7,7 +7,6 @@ S3method(FunctionalityScore,COMPASSResult)
7 7
 S3method(FunctionalityScore,default)
8 8
 S3method(PolyfunctionalityScore,COMPASSResult)
9 9
 S3method(PolyfunctionalityScore,default)
10
-S3method(Response,COMPASSResult)
11 10
 S3method(UniqueCombinations,COMPASSContainer)
12 11
 S3method(UniqueCombinations,default)
13 12
 S3method(markers,COMPASSContainer)
... ...
@@ -23,6 +22,7 @@ S3method(print,COMPASSResult)
23 22
 S3method(subset,COMPASSContainer)
24 23
 S3method(summary,COMPASSContainer)
25 24
 S3method(summary,COMPASSResult)
25
+export(">=")
26 26
 export("COMPASSDescription<-")
27 27
 export("metadata<-")
28 28
 export(COMPASS)
... ...
@@ -45,9 +45,12 @@ export(SimpleCOMPASS)
45 45
 export(TotalCellCounts)
46 46
 export(UniqueCombinations)
47 47
 export(categories)
48
+export(condition)
49
+export(degree)
48 50
 export(getCounts)
49 51
 export(markers)
50 52
 export(metadata)
53
+export(on)
51 54
 export(pheatmap)
52 55
 export(plot2)
53 56
 export(plotCOMPASSResultStack)
... ...
@@ -57,6 +60,7 @@ export(shinyCOMPASS)
57 60
 export(shinyCOMPASSDeps)
58 61
 export(translate_marker_names)
59 62
 export(transpose_list)
63
+export(x)
60 64
 import(data.table)
61 65
 import(grid)
62 66
 import(magrittr)
... ...
@@ -4,6 +4,8 @@
4 4
 #' @param markers a \code{vector} of marker names.
5 5
 #' @param degree the \code{numeric} degree of functionality to test.
6 6
 #' @param max.prob \code{logical} Use the max probability rather than the average across subsets. Defaults to FALSE.
7
+#' @param cond_response \code{logical} Renormalize the probabilities to condition on response. i.e. given that there
8
+#' is a response, what's the probability of a response of degree X or greater?
7 9
 #' @description
8 10
 #' Compute a response probability based on the selected markers, evaluating the probability
9 11
 #' that a subject exhibits a response of size \code{degree} or greater.
... ...
@@ -16,13 +18,13 @@
16 18
 #'
17 19
 #' @examples
18 20
 #' Response(CR, markers = c("M1","M2","M3"), degree = 2)
19
-Response <- function(x, markers, degree, max.prob){
21
+Response <- function(x, markers, degree, max.prob, cond_response){
20 22
   UseMethod("Response")
21 23
 }
22 24
 
23 25
 ##' @rdname Response
24 26
 ##' @export
25
-Response.COMPASSResult <- function(x, markers = NULL, degree = 1, max.prob = FALSE) {
27
+Response.COMPASSResult <- function(x, markers = NULL, degree = 1, max.prob = FALSE, cond_response = FALSE) {
26 28
   ## we drop the last column as it is the 'NULL' category
27 29
   if (is.null(markers)) {
28 30
     markers <- markers(x)
... ...
@@ -44,25 +46,55 @@ Response.COMPASSResult <- function(x, markers = NULL, degree = 1, max.prob = FAL
44 46
     }
45 47
     new_mean_gamma = apply(cat_indices, 2, function(i)
46 48
       apply(Gamma(x)[, i, ], 1, mean))
49
+    # new_gamma <- Gamma(x)[,cat_indices,]
47 50
     new_categories = cbind(new_categories, Counts = rowSums(new_categories))
48 51
     reord = c(setdiff(1:nrow(new_categories), which(new_categories[, "Counts"] ==
49 52
                                                       0)), which(new_categories[, "Counts"] == 0))
50 53
     new_categories = new_categories[reord, ]
51 54
     new_mean_gamma = new_mean_gamma[, reord]
55
+    # new_gamma = new_gamma[,reord,]
52 56
     colnames(new_mean_gamma) = apply(new_categories[, -ncol(new_categories)], 1, function(x)
53 57
       paste0(x, collapse = ""))
58
+    # dimnames(new_gamma)[[2]] = apply(new_categories[, -ncol(new_categories)], 1, function(x)
59
+      # paste0(x, collapse = ""))
54 60
     include_cols <- new_categories[,"Counts"] >= degree
55 61
     if (sum(include_cols) == 0) {
56
-      response <- matrix(rep(0,nrow(new_mean_gamma)),nrow=nrow(new_mean_gamma),ncol=1)
62
+      response <- matrix(rep(0,nrow(new_mean_gamma)),nrow = nrow(new_mean_gamma),ncol=1)
57 63
       rownames(response) <- rownames(new_mean_gamma)
58 64
       colnames(response) <- paste0("Pr(response|degree >=",degree,")")
59 65
     }else{
66
+      #' condition on degree >= x
60 67
       response <- new_mean_gamma[,include_cols, drop = FALSE]
61
-      if(!max.prob){
62
-        #polyfunctional response is the average over subsets
68
+      if (!max.prob & !cond_response) {
69
+        # probability of response from total probability
70
+        # response <- structure(
71
+        #   rowMeans(new_gamma[, include_cols, , drop = FALSE]) / (
72
+        #   rowMeans(new_gamma[, !include_cols, , drop = FALSE]) +
73
+        #     rowMeans(new_gamma[, include_cols, , drop = FALSE]) +
74
+        #     rowMeans(1 - new_gamma[, !include_cols, , drop = FALSE]) +
75
+        #     rowMeans(1 - new_gamma[,include_cols, , drop = FALSE])),
76
+        #   dim = c(nrow(response),1),
77
+        #   names = NULL,
78
+        #   dimnames = list(rownames(response),paste0("Pr(response | degree >=",degree,")")))
79
+
63 80
         response <- structure(rowMeans(response), dim = c(nrow(response),1), names = NULL, dimnames = list(rownames(response),paste0("Pr(response | degree >=",degree,")")))
64
-      }else{
81
+      }else if (max.prob & !cond_response){
65 82
         response <- structure(apply(response,1,max),dim=c(nrow(response),1), names = NULL, dimnames = list(rownames(response), paste0("Pr(response | degree >=",degree,")")))
83
+      }else if (cond_response & ! max.prob){
84
+        response <-
85
+          structure(
86
+            rowSums(exp(log(
87
+              apply(Gamma(scr), 1:2, mean)
88
+            ) - log(
89
+              rowSums(apply(Gamma(scr), 1:2, mean)) + 1e-10
90
+            ))[, include_cols, drop = FALSE]),
91
+            dim = c(nrow(response), 1),
92
+            names = NULL,
93
+            dimnames = list(
94
+              rownames(response),
95
+              paste0("Pr(degree >= ",degree," | response )")
96
+            )
97
+          )
66 98
       }
67 99
       # response <- matrix(response, ncol = 1, nrow = length(response))
68 100
       # rownames(response) <- rownames(new_mean_gamma)
... ...
@@ -5,10 +5,10 @@
5 5
 \alias{Response.COMPASSResult}
6 6
 \title{Compute a response probability from COMPASS mcmc samples.}
7 7
 \usage{
8
-Response(x, markers, degree, max.prob)
8
+Response(x, markers, degree, max.prob, cond_response)
9 9
 
10 10
 \method{Response}{COMPASSResult}(x, markers = NULL, degree = 1,
11
-  max.prob = FALSE)
11
+  max.prob = FALSE, cond_response = FALSE)
12 12
 }
13 13
 \arguments{
14 14
 \item{x}{a \code{COMPASSResult} object.}
... ...
@@ -18,6 +18,9 @@ Response(x, markers, degree, max.prob)
18 18
 \item{degree}{the \code{numeric} degree of functionality to test.}
19 19
 
20 20
 \item{max.prob}{\code{logical} Use the max probability rather than the average across subsets. Defaults to FALSE.}
21
+
22
+\item{cond_response}{\code{logical} Renormalize the probabilities to condition on response. i.e. given that there
23
+is a response, what's the probability of a response of degree X or greater?}
21 24
 }
22 25
 \value{
23 26
 A \code{vector} of response probabilities for each subject.