forked from metrumresearchgroup/metrumrg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas.digest.R
310 lines (307 loc) · 8.91 KB
/
as.digest.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
as.best.digest <- function(x,...){
x[] <- lapply(x,as.best)
x
}
digest <- function(x,...)UseMethod('as.digest')
as.digest <- function(x,...)UseMethod('as.digest')
as.digest.keyed <- function(
x,
key=match.fun('key')(x),
strict=TRUE,
...
)as.digest.data.frame(x,key=key,strict=strict,...)
as.digest.nm <- function(x,key=match.fun('key')(x),...)as.digest(as.keyed(x,key=key,...),...)
`[.digest`<-function (x, ...)
{
y <- NextMethod()
class(y) <- class(x)
y
}
as.digest.digest <- function(x,...)x
as.keyed.nm <- function (x, key=match.fun('key')(x), ...){
class(x) <- setdiff(class(x),'nm')
x <- as.keyed(x,key)
x
}
motif <- function(x,...)UseMethod('as.motif')
as.motif <- function(x,...)UseMethod('as.motif')
as.motif.keyed <- function(x,...){
motif <- list(y='.')
if(length(key(x))) motif$x <- key(x)[[1]]
if(length(key(x))>1)motif$z <- key(x)[[2]]
class(motif) <- 'motif'
motif
}
as.motif.digest <- function(x,...){
keys <- lapply(x,key)
keys <- unlist(keys)
keys <- unique(keys)
keys <- c('.',keys)
motif <- list(
y=keys,
x=keys,
z=keys
)
class(motif) <- 'motif'
motif
}
as.motif.motif <- function(x,...)x
as.motif.nm <- function(x,...){
use <- c('TIME','ID','SEQ',key(x))
use <- unique(use)
use <- use[use %in% names(x)]
motif <- list(
y='.',
x=use,
z=use
)
class(motif) <- 'motif'
motif
}
roles <- function(x,...)UseMethod('as.roles')
as.roles <- function(x,...)UseMethod('as.roles')
as.roles.digest <- function(x,...)lapply(x,as.roles,...)
as.roles.keyed <- function(x,motif=as.motif(x),...){
available <- function(col,x,result){
available <- FALSE
if(col %in% names(x) | col=='.')
if(!col %in% unlist(result))
available <- TRUE
available
}
res <- list()
for(role in names(motif))
for(opt in motif[[role]])
if(is.null(res[[role]]))
if(available(opt,x,res))
res[[role]] <- opt
res <- unlist(res)
res
}
plot.digest <- function(
x,
motif=as.motif(x),
roles=as.roles(x,motif=motif),
...
){
stopifnot(length(roles)==length(x))
plottable <- function(x)!any(is.na(key(x))) & length(key(x)) > 0
keep <- sapply(x,plottable)
indices <- seq_along(x)[keep]
res <- lapply(indices,function(i,...)plot(x[[i]],roles=roles[[i]],...),...)
res <- unlist(res,recursive=FALSE)
res
}
index.digest <- function(
x,
motif=as.motif(x),
roles=as.roles(x,motif=motif),
...
){
stopifnot(length(roles)==length(x))
plottable <- function(x)!any(is.na(key(x))) & length(key(x)) > 0
keep <- sapply(x,plottable)
indices <- seq_along(x)[keep]
res <- lapply(indices,function(i,...)index(x[[i]],roles=roles[[i]],...),...)
res <- unlist(res,recursive=FALSE)
res
}
legacy <- function(x,...)attr(x,'legacy')
`legacy<-` <- function(x,value){
if(is.null(names(value)))stop('value must have a name')
attr(x,'legacy') <- c(attr(x,'legacy'),value)
x
}
format.legacy <- function(x,...){
f <- legacy(x)
if (is.null(f))
return("")
vec <- sapply(names(f), function(nm) paste(nm, "=", f[[nm]]))
vec <- rev(vec)
vec <- paste(vec, collapse = "; ")
vec
}
as.conditioned <- function(x,...)UseMethod('as.conditioned')
as.conditioned.isolated <- function(x,...){
warning('isolated objects have no conditioning variables by definition')
x
}
as.conditioned.keyed <- function(x,roles=as.roles(x),...){
#every key that is not a role must be a classifier: recurse
if(!all(key(x) %in% roles)){
#identify col as the last (in canonical order) of the classifiers
classifiers <- setdiff(key(x),roles)
col <- classifiers[[length(classifiers)]]
dex <- x[[col]] # save for local use
x[[col]] <- NULL # remove from forwarded context
key(x) <- setdiff(key(x),col) # remove from key if present
dat <- split(x,dex,drop=TRUE) # split into sub-frames for sub-plots
for(nm in names(dat))legacy(dat[[nm]]) <- structure(nm,names=col) #internal accumulator
for(nm in names(dat))if(!inherits(dat[[nm]],'conditioned'))class(dat[[nm]]) <- c('conditioned',class(dat[[nm]]))
names(dat) <- paste(col,names(dat),sep=':') # external hierarchical indicator
res <- lapply(dat,as.conditioned,roles=roles,...)
res <- unlist(res,recursive=FALSE)
return(res)
}
if(!inherits(x,'conditioned'))class(x) <- c('conditioned',class(x))
list(x)
}
`[.conditioned`<-function (x, ...)
{
y <- NextMethod()
if(!is.null(legacy(x)) & is.null(legacy(y))) legacy(y) <- legacy(x)
y
}
as.conditioned.digest <- function(x,...){
res <- lapply(x,as.conditioned,...)
res <- unlist(res,recursive=FALSE)
res
}
as.isolated.digest <- function(x,...){
res <- lapply(x,as.isolated,...)
res <- unlist(res,recursive=FALSE)
res
}
#as.conditioned.conditioned <- function(x,...)x
as.isolated <- function(x,...)UseMethod('as.isolated')
as.isolated.isolated <- function(x,...)x
as.isolated.keyed <- function(x,...){
targets <- setdiff(names(x),key(x))
result <- lapply(
targets,
function(column){
res <- x[,names(x) %in% c(key(x),column),drop=FALSE]
class(res) <- c('isolated',class(res))
res
}
)
names(result) <- targets
return(result)
}
plot.keyed <- function(x,roles=as.roles(x),...){
tiles <- as.conditioned(x,roles=roles,...)
lapply(tiles,plot,roles=roles,...)
}
splom.keyed <- function(x,data=NULL,roles=as.roles(x),...){
tiles <- as.conditioned(x,roles=roles,...)
lapply(tiles,splom,roles=roles,...)
}
plot.conditioned <- function(x,roles=as.roles(x),...){
splom <- splom(x,roles=roles,...)
index <- index(x,roles=roles,...)
result <- list(splom,index)
names(result) <- c('splom','index')
return(result)
}
plot.isolated <- function(x,...)index(x,...)
splom.conditioned <- function(
x,
data=NULL,
roles=as.roles(x),
main='',
xlab='',
pscales=0,
...
){
main <- format.legacy(x)
names(x)[names(x)==roles['y']] <- 'y'
names(x)[names(x)==roles['x']] <- 'x'
names(x)[names(x)==roles['z']] <- 'z'
x <- as.data.frame(x)
x$x <- NULL
x <- x[,sapply(x,function(col)!all(is.na(col))),drop=FALSE]
args <- list()
args$x <- x
args$data=NULL
args$main <- main
args$xlab <- paste('for each',roles['x'])
args$pscales <- pscales
if('z' %in% names(x)){ # have grouping argument
args$groups <- x$z
args$x$z <- NULL
args$sub <- paste('grouped by',roles['z'])
}
args <- c(args,list(...))
if(ncol(args$x)<2) return(NULL)
do.call(splom,args)
}
splom.digest <- function(
x,
data=NULL,
motif=as.motif(x),
roles=as.roles(x,motif=motif),
main='',
xlab='',
pscales=0,
...
){
stopifnot(length(roles)==length(x))
plottable <- function(x)!any(is.na(key(x))) & length(key(x)) > 0
keep <- sapply(x,plottable)
indices <- seq_along(x)[keep]
res <- lapply(
indices,
function(i,...)splom(
x[[i]],
roles=roles[[i]]
,...
),
main=main,
xlab=xlab,
pscales=pscales,
...
)
res <- unlist(res,recursive=FALSE)
res
}
index <- function(x,...)UseMethod('index')
index.keyed <- function(x,roles=as.roles(x),...){
tiles <- as.conditioned(x,roles=roles,...)
lapply(tiles,index,roles=roles,...)
}
index.nm <- function(x,density=20,...){
if('set' %in% names(x))stop("'set' is a reserved label in index.nm")
x$ID <- factor(as.integer(as.character(x$ID)))
x$set <- map(x$ID,from=unique(x$ID),to=rep(seq_along(unique(x$ID)),each=density,length.out=length(unique(x$ID))))
index(as.keyed(x,c('ID','set')),roles=c(y='.',x='ID'),...)
}
index.conditioned <- function(x,roles=as.roles(x),...){
isolates <- as.isolated(x) # named list of isolates
result <- lapply(isolates,index,roles=roles)
return(result)
}
index.isolated <- function(x,roles=as.roles(x),...){
if(!nrow(x))return(NULL)
#canonical order is the isolated column, followed by the key
#in an isolate, we know we have only key colums and one variable.
#at this point, we also have some assigned roles, or maybe none.
#anything unassigned is used as a manual conditioning variable, a.o.t. lattice conditioning.
#we can fully inform the roles at this point, because we know what '.' means
#then we can recurse until we have nothing but roles.
canon <-c(setdiff(names(x),key(x)),key(x))
roles[roles=='.'] <- canon[[1]]
names(x)[names(x)==roles['y']] <- 'y'
names(x)[names(x)==roles['x']] <- 'x'
names(x)[names(x)==roles['z']] <- 'z'
if(is.character(x$x))x$x <- factor(x$x)
if(is.character(x$y))x$y <- factor(x$y)
# now everything is presumably factor or numeric (i.e. plottable)
args <- list()
args$x <- y~x # first arg is formula, for dispatch on formula method
if('z' %in% names(roles)){ # have grouping argument
args$groups <- expression(z)
args$sub <- paste('grouped by',roles['z'])
}
args$data <- x
args$aspect <- 1
args$xlab <- roles['x']
args$ylab <- roles['y']
args$main <- format.legacy(x)
args$scales <- list()
args$scales$tck <- c(1,0)
args$scales$x <- list()
if(!is.numeric(x$x))args$scales$x$rot <- 90
#rotate factor labels on x axis to prevent overlap
args <- c(args,list(...))
do.call(xyplot,args)
}