1
1
# ' Calculate prediction score (quality of prediction)
2
2
# '
3
- # ' @param x An object of class [aphylo_estimates] or a numeric matrix.
4
- # ' @param expected Integer vector of length \eqn{n}. Expected values (either 0 or 1).
3
+ # ' @param x An object of class [aphylo_estimates] or other numeric vector-like
4
+ # ' object (see details).
5
+ # ' @param expected Numeric vector-like object length \eqn{n} (see details).
6
+ # ' Expected values (either 0 or 1).
5
7
# ' @param alpha0,alpha1 Probability of observing a zero an a one, respectively.
6
8
# ' @param W A square matrix. Must have as many rows as genes in `expected`.
7
9
# ' @param ... Further arguments passed to [predict.aphylo_estimates]
8
10
# ' @export
9
11
# ' @details In the case of `prediction_score`, `...` are passed to
10
12
# ' `predict.aphylo_estimates`.
13
+ # ' The function will accept `x` as a numeric vector, list of vectors, or matrix.
14
+ # ' Otherwise, it will try to coerce it to a matrix. If it fails, it will throw
15
+ # ' an error.
11
16
# ' @returns
12
17
# ' A list of class `aphylo_prediction_score`:
13
18
# ' - obs : Observed 1 - MAE.
@@ -53,7 +58,66 @@ prediction_score <- function(
53
58
) UseMethod(" prediction_score" )
54
59
55
60
# ' @export
56
- # ' @rdname prediction_score
61
+ prediction_score.matrix <- function (
62
+ x ,
63
+ expected ,
64
+ alpha0 = NULL ,
65
+ alpha1 = NULL ,
66
+ W = NULL ,
67
+ ...
68
+ ) {
69
+
70
+ # Both x and expected should be numeric
71
+ if (! is.numeric(x ) || ! is.numeric(expected ))
72
+ stop(" `x` and `expected` must be numeric." , call. = FALSE )
73
+
74
+ # Checking that it has a single column
75
+ if (ncol(x ) != 1L || ncol(expected ) != 1L )
76
+ stop(" `x` and `y` must have a single column." , call. = FALSE )
77
+
78
+ # Checking dimensions
79
+ if (any(dim(x ) != dim(expected )))
80
+ stop(" `x` and `expected` differ in length. These must match." , call. = FALSE )
81
+
82
+ # Vectorizing
83
+ x <- as.vector(x )
84
+ expected <- as.vector(expected )
85
+
86
+ # Passing to default method
87
+ prediction_score_backend(
88
+ x = matrix (x , ncol = 1 ),
89
+ expected = matrix (expected , ncol = 1 ),
90
+ alpha0 = alpha0 ,
91
+ alpha1 = alpha1 ,
92
+ W = W ,
93
+ ...
94
+ )
95
+
96
+ }
97
+
98
+ # ' @export
99
+ prediction_score.list <- function (
100
+ x ,
101
+ expected ,
102
+ alpha0 = NULL ,
103
+ alpha1 = NULL ,
104
+ W = NULL ,
105
+ ...
106
+ ) {
107
+
108
+ # Passing to default method
109
+ prediction_score.matrix(
110
+ x = do.call(rbind , x ),
111
+ expected = do.call(rbind , expected ),
112
+ alpha0 = alpha0 ,
113
+ alpha1 = alpha1 ,
114
+ W = W ,
115
+ ...
116
+ )
117
+
118
+ }
119
+
120
+ # ' @export
57
121
prediction_score.default <- function (
58
122
x ,
59
123
expected ,
@@ -62,7 +126,33 @@ prediction_score.default <- function(
62
126
W = NULL ,
63
127
...
64
128
) {
65
-
129
+
130
+ x <- tryCatch(as.matrix(x ), error = function (e ) e )
131
+ expected <- tryCatch(as.matrix(expected ), error = function (e ) e )
132
+
133
+ if (inherits(x , " error" ) || inherits(expected , " error" ))
134
+ stop(" If not list or matrix, `x` and `expected` must be numeric." , call. = FALSE )
135
+
136
+ prediction_score.matrix(
137
+ x = x ,
138
+ expected = expected ,
139
+ alpha0 = alpha0 ,
140
+ alpha1 = alpha1 ,
141
+ W = W ,
142
+ ...
143
+ )
144
+
145
+ }
146
+
147
+ prediction_score_backend <- function (
148
+ x ,
149
+ expected ,
150
+ alpha0 = NULL ,
151
+ alpha1 = NULL ,
152
+ W = NULL ,
153
+ ...
154
+ ) {
155
+
66
156
# Checking dimensions
67
157
if (length(x ) != length(expected ))
68
158
stop(" `x` and `expected` differ in length. These must match." , call. = FALSE )
@@ -81,7 +171,7 @@ prediction_score.default <- function(
81
171
# score.
82
172
if (! length(alpha0 ))
83
173
alpha0 <- 1 - mean(expected )
84
- if (is.null (alpha1 ))
174
+ if (! length (alpha1 ))
85
175
alpha1 <- 1 - alpha0
86
176
87
177
if (is.null(W ))
@@ -227,20 +317,20 @@ prediction_score.aphylo_estimates <- function(
227
317
# Adjusting alphas according to loo logic. To make the benchmark fair, we need
228
318
# to exclude one annotation from each type for the loo
229
319
n <- length(ids )
230
- if (is.null (alpha0 ) && loo ) {
320
+ if (! length (alpha0 ) && loo ) {
231
321
232
322
alpha0 <- max(sum(expected [ids ,] == 0 ) - ncol(expected ), 0 )
233
323
alpha0 <- alpha0 / ((nrow(expected [ids ,]) - 1 ) * ncol(expected ))
234
324
235
325
}
236
- if (is.null (alpha1 ) && loo ) {
326
+ if (! length (alpha1 ) && loo ) {
237
327
238
328
alpha1 <- max(sum(expected [ids ,]) - ncol(expected ), 0 )
239
329
alpha1 <- alpha1 / ((nrow(expected [ids ,]) - 1 ) * ncol(expected ))
240
330
241
331
}
242
332
243
- ans <- prediction_score (
333
+ ans <- prediction_score_backend (
244
334
x = pred [ids ,,drop = FALSE ],
245
335
expected = expected [ids ,,drop = FALSE ],
246
336
alpha0 = alpha0 ,
0 commit comments