[0aeb11]: / predict.voomNSC.R

Download this file

89 lines (82 with data), 2.4 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
predict.voomNSC =
function (fit, newdata, threshold = NULL, prior = NULL)
{
if (is.null(prior)) prior = fit$prior
if (is.null(threshold)) threshold = fit$opt.threshold
vm = voomGSD(data.train = fit$counts, data.test = newdata, group = fit$conditions, norm.method = fit$normalization)
x = vm$TestExp
# soft.shrink(...)
soft.shrink =
function (delta, threshold)
{
dif = abs(delta) - threshold
delta = sign(delta) * dif * (dif > 0)
nonzero = sum(drop((dif > 0) %*% rep(1, ncol(delta))) > 0)
attr(delta, "nonzero") = nonzero
delta
}
# diag.disc(...)
diag.disc =
function (x, centroids, prior, weight)
{
if (!missing(weight)) {
posid = (weight > 0)
if (any(posid)) {
weight = sqrt(weight[posid])
centroids = centroids[posid, , drop = FALSE] * weight
x = x[posid, , drop = FALSE] * weight
}
else {
mat = outer(rep(1, ncol(x)), log(prior), "*")
dimnames(mat) = list(NULL, dimnames(centroids)[[2]])
return(mat)
}
}
dd = t(x) %*% centroids
dd0 = drop(rep(1, nrow(centroids)) %*% (centroids^2))/2 -
log(prior)
names(dd0) = NULL
scale(dd, dd0, FALSE)
}
# safe.exp(...)
safe.exp = function (x)
{
xx = sign(x) * pmin(abs(x), 500)
return(exp(xx))
}
#soft.max(...)
softmax =
function (x, gap = FALSE)
{
d = dim(x)
maxdist = x[, 1]
pclass = rep(1, d[1])
for (i in seq(2, d[2])) {
l = x[, i] > maxdist
pclass[l] = i
maxdist[l] = x[l, i]
}
dd = dimnames(x)[[2]]
if (gap) {
x = abs(maxdist - x)
x[cbind(seq(d[1]), pclass)] = drop(x %*% rep(1, d[2]))
gaps = do.call("pmin", data.frame(x))
}
pclass = if (is.null(dd) || !length(dd))
pclass
else factor(pclass, levels = seq(d[2]), labels = dd)
if (gap)
list(class = pclass, gaps = gaps)
else pclass
}
sd = fit$weightedSD.pooled
centroid.overall = fit$weightedMean
centroids = fit$weightedMean.C
se.scale = fit$se.scale
delta = fit$delta
delta.shrunk = soft.shrink(delta, threshold)
delta.shrunk = t(t(delta.shrunk) * as.numeric(se.scale))
posid = drop(abs(delta.shrunk) %*% rep(1, length(prior))) > 0
dd = diag.disc((x - fit$weightedMean)/(fit$weightedSD.pooled), delta.shrunk, prior, posid)
softmax(dd)
}