-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfixed-terms.R
284 lines (223 loc) · 7.1 KB
/
fixed-terms.R
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#' @include KINOMO-class.R
#' @include KINOMOstd-class.R
NULL
setMethod('c', 'KINOMO',
function(x, ..., margin=3L, recursive=FALSE){
y <- ..1
if( is.matrix(y) ){
if( missing(margin) ){
if( nrow(y) == nrow(x) ){
if( ncol(y) == ncol(x) ){
warning("KINOMO::`c` - Right argument match both target dimensions: concatenating basis columns."
, " Use `margin=4L` to concatenate coefficient rows.")
}
margin <- 3L
}else if( ncol(y) == ncol(x) ){
margin <- 4L
}else{
stop("KINOMO::`c` - Incompatible argument dimensions: could not infer concatenation margin.")
}
}
if( margin == 1L ){ # extend basis vectors
if( nbterms(x) ){ # cannot extend models with fixed basis terms
stop("KINOMO::`c` - Could not extend basis vectors:"
, " KINOMO model has fixed basis terms [", nbterms(x), "]")
}
if( ncol(y) != nbasis(x) ){
stop("KINOMO::`c` - Could not extend basis vectors:"
, " incompatible number of columns [", nbasis(x), '!=', ncol(y), "].")
}
# extend basis vectors
basis(x) <- rbind(basis(x), y)
} else if( margin == 2L ){ # extend basis profiles
if( ncterms(x) ){ # cannot extend models with fixed coef terms
stop("KINOMO::`c` - Could not extend basis profiles:"
, " KINOMO model has fixed coefficient terms [", ncterms(x), "]")
}
if( nrow(y) != nbasis(x) ){
stop("KINOMO::`c` - Could not extend basis profiles:"
, " incompatible number of rows [", nbasis(x), '!=', nrow(y), "].")
}
# extend basis profiles
coef(x) <- cbind(coef(x), y)
} else if( margin == 3L ){ # add basis vectors
if( nrow(y) != nrow(x) ){
stop("KINOMO::`c` - Could not concatenate basis vectors:"
, " incompatible number of rows [", nrow(x), '!=', nrow(y), "].")
}
# bind basis terms
.basis(x) <- cbind(basis(x), y)
dn <- colnames(.basis(x))
# bind dummy coef
.coef(x) <- rbind(coef(x), matrix(NA, ncol(y), ncol(x)))
basisnames(x) <- dn
} else if( margin == 4L ){ # add basis profiles
if( ncol(y) != ncol(x) ){
stop("KINOMO::`c` - Could not concatenate basis profiles:"
, " incompatible number of columns [", ncol(x), '!=', ncol(y), "].")
}
# bind coef terms
.coef(x) <- rbind(coef(x), y)
dn <- rownames(.coef(x))
# bind dummy basis
.basis(x) <- cbind(basis(x), matrix(NA, nrow(x), nrow(y)))
basisnames(x) <- dn
}else{
stop("KINOMO::`c` - Invalid concatenation margin: should be either"
, " 1L (basis rows), 2L (coef columns), 3L (basis vectors/columns) or 4L (basis profiles/coef rows).")
}
}else if( is.KINOMO(y) ){
# check dimensions
if( nrow(x) != nrow(y) )
stop("KINOMO::`c` - Could not concatenate KINOMO objects:"
, " incompatible number of rows [", nrow(x), '!=', nrow(y), "]")
if( ncol(x) != ncol(y) )
stop("KINOMO::`c` - Could not concatenate KINOMO objects:"
, " incompatible number of columns [", ncol(x), '!=', ncol(y), "]")
.basis(x) <- cbind(basis(x), basis(y))
.coef(x) <- rbind(coef(x), coef(y))
}else{
stop("KINOMO::`c` - Concatenation of an KINOMO object with objects of class '", class(y), "' is not supported.")
}
# return augmented object
x
}
)
fterms <- function(value){
res <- list(n=0L, terms=NULL, df=NULL, i=integer())
if( !length(value) ) return(res)
# convert into a data.frame
if( is.factor(value) ) value <- data.frame(Group=value)
else if( is.numeric(value) ) value <- data.frame(Var=value)
else if( !is.data.frame(value) ) value <- as.data.frame(value)
res$n <- length(value)
res$df <- value
# generate fixed term matrix
terms <- model.matrix(~ -1 + ., data=value)
res$terms <- terms
# build indexes
res$i <- 1:ncol(terms)
res
}
setGeneric('bterms<-', function(object, value) standardGeneric('bterms<-'))
setReplaceMethod('bterms', signature('KINOMOstd', 'ANY'),
function(object, value){
if( nterms(object) ){
stop("Cannot set fixed basis terms on an object that already has fixed terms:",
" these can be set only once and before setting any fixed coefficient term",
" [coef=", ncterms(object), ", basis=", nbterms(object), "].")
}
# build terms
t <- fterms(value)
if( !t$n ) return(object)
# check dimension
if( nrow(t$terms) != nrow(object) ){
stop("Invalid fixed basis terms: all terms should have length the number of target rows"
, "[terms=", nrow(t$terms), " != ", nrow(object), "=target]")
}
# set data
object@bterms <- t$df
# set indexes
i <- t$i
nv <- nbasis(object)
object@ibterms <- nv + i
# set terms
object <- c(object, t$terms, margin=3L)
object
}
)
setGeneric('cterms<-', function(object, value) standardGeneric('cterms<-'))
setReplaceMethod('cterms', signature('KINOMOstd', 'ANY'),
function(object, value){
if( ncterms(object) ){
stop("Cannot set fixed coef terms on an object that already has fixed coef terms:",
" these can be set only once",
" [coef=", ncterms(object), ", basis=", nbterms(object), "].")
}
# build terms
t <- fterms(value)
if( !t$n ) return(object)
# check dimension
if( nrow(t$terms) != ncol(object) ){
stop("Invalid fixed coefficient terms: all terms should have length the number of target columns"
, "[terms=", nrow(t$terms), " != ", ncol(object), "=target]")
}
# transpose term matrix
t$terms <- t(t$terms)
# set data
object@cterms <- t$df
# set indexes
i <- t$i
nv <- nbasis(object)
object@icterms <- nv + i
# set terms
object <- c(object, t$terms, margin=4L)
object
}
)
setGeneric('ibterms', function(object, ...) standardGeneric('ibterms') )
setMethod('ibterms', 'KINOMO',
function(object, ...){
stop("KINOMO::ibterms is a pure virtual method of interface 'KINOMO'."
," It should be overloaded in class '", class(object),"'.")
}
)
setMethod('ibterms', 'KINOMOstd',
function(object){
object@ibterms
}
)
setGeneric('icterms', function(object, ...) standardGeneric('icterms') )
setMethod('icterms', 'KINOMO',
function(object, ...){
stop("KINOMO::icterms is a pure virtual method of interface 'KINOMO'."
," It should be overloaded in class '", class(object),"'.")
}
)
setMethod('icterms', 'KINOMOstd',
function(object){
object@icterms
}
)
iterms <- function(object, ...){
c(ibterms(object), icterms(object))
}
nterms <- function(object){
length(ibterms(object)) + length(icterms(object))
}
nbterms <- function(object){
length(ibterms(object))
}
ncterms <- function(object){
length(icterms(object))
}
bterms <- function(object){
object@bterms
}
cterms <- function(object){
object@cterms
}
ibasis <- function(object, ...){
i <- 1:nbasis(object)
if( length(idx <- ibterms(object, ...)) ) i[-idx]
else i
}
icoef <- function(object, ...){
i <- 1:nbasis(object)
if( length(idx <- icterms(object, ...)) ) i[-idx]
else i
}
#' @export
t.KINOMOstd <- function(x){
# transpose and swap factors
x <- t.KINOMO(x)
# swap fixed terms
bt <- bterms(x)
ibt <- ibterms(x)
x@bterms <- cterms(x)
x@ibterms <- icterms(x)
x@cterms <- bt
x@icterms <- ibt
# returns
x
}