|
a |
|
b/partyMod/R/Utils.R |
|
|
1 |
|
|
|
2 |
# $Id$ |
|
|
3 |
|
|
|
4 |
### Wrapper for functions defined in ./src/Utilsc |
|
|
5 |
|
|
|
6 |
qsvd <- function(x) { |
|
|
7 |
if (!is.matrix(x) || ncol(x) != nrow(x)) |
|
|
8 |
stop(sQuote("x"), " is not a quadratic matrix") |
|
|
9 |
|
|
|
10 |
svdmem <- new("svd_mem", ncol(x)) |
|
|
11 |
dummy <- .Call("R_svd", x, svdmem, PACKAGE = "atlantisPartyMod") |
|
|
12 |
rm(dummy) |
|
|
13 |
return(list(u = svdmem@u, vt = svdmem@v, d = svdmem@s)) |
|
|
14 |
} |
|
|
15 |
|
|
|
16 |
MPinv <- function(x, tol = sqrt(.Machine$double.eps)) { |
|
|
17 |
if (!is.matrix(x) || ncol(x) != nrow(x)) |
|
|
18 |
stop(sQuote("x"), " is not a quadratic matrix") |
|
|
19 |
|
|
|
20 |
svdmem <- new("svd_mem", ncol(x)) |
|
|
21 |
RET <- .Call("R_MPinv", x, tol, svdmem, PACKAGE = "atlantisPartyMod") |
|
|
22 |
return(RET@MPinv) |
|
|
23 |
} |
|
|
24 |
|
|
|
25 |
### wrapper for functions defined in ./src/Splits.c |
|
|
26 |
|
|
|
27 |
Split <- function(x, y, weights, splitctrl) { |
|
|
28 |
|
|
|
29 |
if (is.factor(y)) |
|
|
30 |
ym <- sapply(levels(y), function(l) as.numeric(y == l)) |
|
|
31 |
else |
|
|
32 |
ym <- matrix(y, ncol = 1) |
|
|
33 |
storage.mode(ym) <- "double" |
|
|
34 |
|
|
|
35 |
if (is.factor(x)) { |
|
|
36 |
xm <- sapply(levels(x), function(l) as.numeric(x == l)) |
|
|
37 |
storage.mode(xm) <- "double" |
|
|
38 |
xc <- as.numeric(x) |
|
|
39 |
storage.mode(xc) <- "integer" |
|
|
40 |
lecxy <- new("LinStatExpectCovar", ncol(xm), ncol(ym)) |
|
|
41 |
lec <- new("LinStatExpectCovar", as.integer(1), ncol(ym)) |
|
|
42 |
eci <- ExpectCovarInfluence(ym, weights) |
|
|
43 |
split <- .Call("R_splitcategorical", xm, xc, ym, weights, lec, lecxy, |
|
|
44 |
eci, splitctrl, PACKAGE = "atlantisPartyMod") |
|
|
45 |
} else { |
|
|
46 |
ox <- order(x) |
|
|
47 |
storage.mode(ox) <- "integer" |
|
|
48 |
xm <- matrix(x, ncol = 1) |
|
|
49 |
storage.mode(xm) <- "double" |
|
|
50 |
lec <- new("LinStatExpectCovar", as.integer(1), ncol(ym)) |
|
|
51 |
eci <- ExpectCovarInfluence(ym, weights) |
|
|
52 |
split <- .Call("R_split", xm, ym, weights, ox, lec, |
|
|
53 |
eci, splitctrl, PACKAGE = "atlantisPartyMod") |
|
|
54 |
} |
|
|
55 |
split |
|
|
56 |
} |
|
|
57 |
|
|
|
58 |
### Wrapper for functions defined in ./src/TestStatistic.c |
|
|
59 |
|
|
|
60 |
maxabsTestStatistic <- function(t, mu, Sigma, tol = sqrt(.Machine$double.eps)) { |
|
|
61 |
storage.mode(t) <- "double" |
|
|
62 |
storage.mode(mu) <- "double" |
|
|
63 |
storage.mode(Sigma) <- "double" |
|
|
64 |
storage.mode(tol) <- "double" |
|
|
65 |
|
|
|
66 |
if (length(t) != length(mu) || length(t) != nrow(Sigma)) |
|
|
67 |
stop("dimensions don't match") |
|
|
68 |
|
|
|
69 |
.Call("R_maxabsTestStatistic", t, mu, Sigma, tol, PACKAGE = "atlantisPartyMod") |
|
|
70 |
} |
|
|
71 |
|
|
|
72 |
quadformTestStatistic <- function(t, mu, Sigma, |
|
|
73 |
tol = sqrt(.Machine$double.eps)) { |
|
|
74 |
|
|
|
75 |
storage.mode(t) <- "double" |
|
|
76 |
storage.mode(mu) <- "double" |
|
|
77 |
storage.mode(Sigma) <- "double" |
|
|
78 |
storage.mode(tol) <- "double" |
|
|
79 |
|
|
|
80 |
if (length(t) != length(mu) || length(t) != nrow(Sigma)) |
|
|
81 |
stop("dimensions don't match") |
|
|
82 |
|
|
|
83 |
SigmaPlus <- MPinv(Sigma, tol = tol) |
|
|
84 |
.Call("R_quadformTestStatistic", t, mu, SigmaPlus, PACKAGE = "atlantisPartyMod") |
|
|
85 |
} |
|
|
86 |
|
|
|
87 |
|
|
|
88 |
### Wrapper for functions defined in ./src/LinearStatistic.c |
|
|
89 |
|
|
|
90 |
LinearStatistic <- function(x, y, weights) { |
|
|
91 |
storage.mode(x) <- "double" |
|
|
92 |
storage.mode(y) <- "double" |
|
|
93 |
storage.mode(weights) <- "double" |
|
|
94 |
.Call("R_LinearStatistic", x, y, weights, PACKAGE = "atlantisPartyMod") |
|
|
95 |
} |
|
|
96 |
|
|
|
97 |
ExpectCovarInfluence <- function(y, weights) { |
|
|
98 |
storage.mode(y) <- "double" |
|
|
99 |
storage.mode(weights) <- "double" |
|
|
100 |
.Call("R_ExpectCovarInfluence", y, weights, PACKAGE = "atlantisPartyMod") |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
ExpectCovarLinearStatistic <- function(x, y, weights) { |
|
|
104 |
storage.mode(x) <- "double" |
|
|
105 |
storage.mode(y) <- "double" |
|
|
106 |
storage.mode(weights) <- "double" |
|
|
107 |
expcovinf <- ExpectCovarInfluence(y, weights) |
|
|
108 |
.Call("R_ExpectCovarLinearStatistic", x, y, weights, expcovinf, |
|
|
109 |
PACKAGE = "atlantisPartyMod") |
|
|
110 |
} |
|
|
111 |
|
|
|
112 |
PermutedLinearStatistic <- function(x, y, indx, perm) { |
|
|
113 |
storage.mode(x) <- "double" |
|
|
114 |
storage.mode(y) <- "double" |
|
|
115 |
|
|
|
116 |
if (any(indx < 1 || indx > nrow(y))) |
|
|
117 |
stop("wrong indices") |
|
|
118 |
|
|
|
119 |
if (any(perm < 1 || perm > nrow(y))) |
|
|
120 |
stop("wrong indices") |
|
|
121 |
|
|
|
122 |
# C indexing |
|
|
123 |
indx <- indx - 1 |
|
|
124 |
perm <- perm - 1 |
|
|
125 |
storage.mode(indx) <- "integer" |
|
|
126 |
storage.mode(perm) <- "integer" |
|
|
127 |
.Call("R_PermutedLinearStatistic", x, y, indx, perm, |
|
|
128 |
PACKAGE = "atlantisPartyMod") |
|
|
129 |
} |
|
|
130 |
|
|
|
131 |
### Median Survival Time, see survival:::print.survfit |
|
|
132 |
|
|
|
133 |
mst <- function(x) { |
|
|
134 |
minmin <- function(y, xx) { |
|
|
135 |
|
|
|
136 |
### don't complain about Inf |
|
|
137 |
ww <- getOption("warn") |
|
|
138 |
on.exit(options(warn = ww)) |
|
|
139 |
options(warn = -1) |
|
|
140 |
|
|
|
141 |
if (any(!is.na(y) & y==.5)) { |
|
|
142 |
if (any(!is.na(y) & y <.5)) |
|
|
143 |
.5*(min(xx[!is.na(y) & y==.5]) + min(xx[!is.na(y) & y<.5])) |
|
|
144 |
else |
|
|
145 |
.5*(min(xx[!is.na(y) & y==.5]) + max(xx[!is.na(y) & y==.5])) |
|
|
146 |
} else min(xx[!is.na(y) & y<=.5]) |
|
|
147 |
} |
|
|
148 |
med <- minmin(x$surv, x$time) |
|
|
149 |
return(med) |
|
|
150 |
} |
|
|
151 |
|
|
|
152 |
dostep <- function(x, y) { |
|
|
153 |
|
|
|
154 |
### create a step function based on x, y coordinates |
|
|
155 |
### modified from `survival:print.survfit' |
|
|
156 |
if (is.na(x[1] + y[1])) { |
|
|
157 |
x <- x[-1] |
|
|
158 |
y <- y[-1] |
|
|
159 |
} |
|
|
160 |
n <- length(x) |
|
|
161 |
if (n > 2) { |
|
|
162 |
# replace verbose horizonal sequences like |
|
|
163 |
# (1, .2), (1.4, .2), (1.8, .2), (2.3, .2), (2.9, .2), (3, .1) |
|
|
164 |
# with (1, .2), (3, .1). They are slow, and can smear the looks |
|
|
165 |
# of the line type. |
|
|
166 |
dupy <- c(TRUE, diff(y[-n]) !=0, TRUE) |
|
|
167 |
n2 <- sum(dupy) |
|
|
168 |
|
|
|
169 |
#create a step function |
|
|
170 |
xrep <- rep(x[dupy], c(1, rep(2, n2-1))) |
|
|
171 |
yrep <- rep(y[dupy], c(rep(2, n2-1), 1)) |
|
|
172 |
RET <- list(x = xrep, y = yrep) |
|
|
173 |
} else { |
|
|
174 |
if (n == 1) { |
|
|
175 |
RET <- list(x = x, y = y) |
|
|
176 |
} else { |
|
|
177 |
RET <- list(x = x[c(1,2,2)], y = y[c(1,1,2)]) |
|
|
178 |
} |
|
|
179 |
} |
|
|
180 |
return(RET) |
|
|
181 |
} |
|
|
182 |
|
|
|
183 |
### Normalized mutual information, Stehl & Gosh (2002) JMLR |
|
|
184 |
nmi <- function(x, y) |
|
|
185 |
{ |
|
|
186 |
x <- table(x, y) |
|
|
187 |
x <- x / sum(x) # ??? |
|
|
188 |
|
|
|
189 |
m_x <- rowSums(x) |
|
|
190 |
m_y <- colSums(x) |
|
|
191 |
y <- outer(m_x, m_y) |
|
|
192 |
|
|
|
193 |
i <- which((x > 0) & (y > 0)) |
|
|
194 |
out <- sum(x[i] * log(x[i] / y[i])) |
|
|
195 |
e_x <- sum(m_x * log(ifelse(m_x > 0, m_x, 1))) |
|
|
196 |
e_y <- sum(m_y * log(ifelse(m_y > 0, m_y, 1))) |
|
|
197 |
|
|
|
198 |
out / sqrt(e_x * e_y) |
|
|
199 |
} |
|
|
200 |
|
|
|
201 |
### check if two objects are identical and print differences else |
|
|
202 |
isequal <- function(a, b) { |
|
|
203 |
attributes(a) <- NULL |
|
|
204 |
attributes(b) <- NULL |
|
|
205 |
if (!isTRUE(all.equal(a, b))) { |
|
|
206 |
print(a, digits = 10) |
|
|
207 |
print(b, digits = 10) |
|
|
208 |
return(FALSE) |
|
|
209 |
} else { |
|
|
210 |
return(TRUE) |
|
|
211 |
} |
|
|
212 |
} |
|
|
213 |
|
|
|
214 |
mysurvfit <- function(y, weights, ...) { |
|
|
215 |
|
|
|
216 |
stopifnot(extends(class(y), "Surv")) |
|
|
217 |
### see comment on weights and subset in ?survfit |
|
|
218 |
y <- y[weights > 0,] |
|
|
219 |
weights <- weights[weights > 0] |
|
|
220 |
return(survfit(y ~ 1, weights = weights, ...)) |
|
|
221 |
} |
|
|
222 |
|
|
|
223 |
R_get_nodeID <- function(tree, inputs, mincriterion) |
|
|
224 |
.Call("R_get_nodeID", tree, inputs, 0.0, -1L, PACKAGE = "atlantisPartyMod") |
|
|
225 |
|
|
|
226 |
R_getpredictions <- function(tree, where) |
|
|
227 |
.Call("R_getpredictions", tree, where, PACKAGE = "atlantisPartyMod") |
|
|
228 |
|
|
|
229 |
R_remove_weights <- function(tree, remove) |
|
|
230 |
.Call("R_remove_weights", tree, remove, package = "atlantisPartyMod") |
|
|
231 |
|
|
|
232 |
R_modify_response <- function(y, responses) |
|
|
233 |
.Call("R_modify_response", as.double(y), responses, |
|
|
234 |
PACKAGE = "atlantisPartyMod") |
|
|
235 |
|
|
|
236 |
R_TreeGrow <- function(object, weights, fitmem, ctrl, where) |
|
|
237 |
.Call("R_TreeGrow", object, weights, fitmem, ctrl, |
|
|
238 |
where, PACKAGE = "atlantisPartyMod") |
|
|
239 |
|
|
|
240 |
copyslots <- function(source, target) { |
|
|
241 |
slots <- names(getSlots(class(source))) |
|
|
242 |
slots <- slots[(slots %in% names(getSlots(class(target))))] |
|
|
243 |
if (length(slots) == 0) |
|
|
244 |
stop("no common slots to copy to") |
|
|
245 |
for (s in slots) |
|
|
246 |
eval(parse(text = paste("target@", s, " <- source@", s))) |
|
|
247 |
return(target) |
|
|
248 |
} |