/
mult-MSHAPESs.R
executable file
·224 lines (198 loc) · 6.34 KB
/
mult-MSHAPESs.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
##### mean shapes on coefficients todo: better handling of $slots
##### (eg r2 for Opn, etc.)
#' Mean shape calculation for Coo, Coe, etc.
#'
#' Quite a versatile function that calculates mean (or median, or whatever function)
#' on list or an array of shapes, an Ldk object. It can also be used on Coe objects.
#' In that case, the reverse transformation (from coefficients to shapes) is calculated, (within
#' groups defined with the fac argument if provided) and the Coe object is _also_ returned
#' (in `$Coe`) along with a list of shapes (in `$shp`) and can then be passed to `plot_MSHAPES`.
#'
#' @param x a list, array, Ldk, LdkCoe, OutCoe or OpnCoe or PCA object
#' @param fac factor specification for [fac_dispatcher]
#' @param FUN a function to compute the mean shape (\link{mean} by default, by \link{median} can be considered)
#' @param nb.pts numeric the number of points for calculated shapes (only Coe objects)
#' @param ... useless here.
#' @return the averaged shape; on Coe objects, a list with two components: \code{$Coe} object of the same class, and
#' \code{$shp} a list of matrices of (x, y) coordinates. On [PCA] and [LDA] objects, the FUN (typically mean or median)
#' of scores on `PCs` or `LDs`. This method used on the latter objects may be moved to another function at some point.
#' @rdname MSHAPES
#' @family multivariate
#' @examples
#' #### on shapes
#' MSHAPES(wings)
#' MSHAPES(wings$coo)
#' MSHAPES(coo_sample(bot, 24)$coo)
#' stack(wings)
#' coo_draw(MSHAPES(wings))
#'
#' bot.f <- efourier(bot, 12)
#' MSHAPES(bot.f) # the mean (global) shape
#' ms <- MSHAPES(bot.f, 'type')
#' ms$Coe
#' class(ms$Coe)
#' ms <- ms$shp
#' coo_plot(ms$beer)
#' coo_draw(ms$whisky, border='forestgreen')
#' @export
MSHAPES <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...) {
UseMethod("MSHAPES")
}
#' @export
MSHAPES.list <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...) {
A <- ldk_check(x)
return(apply(A, 1:2, FUN, na.rm = TRUE))
}
#' @export
MSHAPES.array <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...) {
if (length(dim(x)) == 3) {
A <- ldk_check(x)
return(apply(A, 1:2, FUN, na.rm = TRUE))
}
}
#' @export
MSHAPES.Ldk <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...) {
Ldk <- x
A <- ldk_check(Ldk$coo)
return(apply(A, 1:2, mean, na.rm = TRUE))
}
#' @export
MSHAPES.OutCoe <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...) {
OutCoe <- x
nb.h <- ncol(OutCoe$coe)/4 #todo
if (is.null(fac)) {
message("no 'fac' provided, returns meanshape")
coe.mshape <- apply(OutCoe$coe, 2, FUN)
xf <- coeff_split(coe.mshape, nb.h, 4)
return(efourier_i(xf, nb.pts = nb.pts))
}
f <- fac_dispatcher(x, fac) %>% factor
fl <- levels(f)
shp <- list()
rows <- numeric()
coe <- matrix(NA, nrow = nlevels(f), ncol = ncol(OutCoe$coe),
dimnames = list(fl, colnames(OutCoe$coe)))
for (i in seq(along = fl)) {
coe.i <- OutCoe$coe[f == fl[i], ]
rows[i] <- which(f == fl[i])[1]
if (is.matrix(coe.i) | is.data.frame(coe.i)) {
coe.i <- apply(coe.i, 2, FUN)
}
coe[i, ] <- coe.i
xf <- coeff_split(cs = coe.i, nb.h = nb.h, cph = 4)
shp[[i]] <- efourier_i(xf, nb.h = nb.h, nb.pts = nb.pts)
}
names(shp) <- fl
Coe2 <- OutCoe
Coe2$coe <- coe
Coe2$fac <- slice(Coe2$fac, rows)
res <- list(Coe = Coe2, shp = shp) %>%
.prepend_class("MSHAPES")
return(res)
}
#' @export
MSHAPES.OpnCoe <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...) {
OpnCoe <- x
#todo: check if method is all identical
p <- pmatch(tolower(OpnCoe$method[1]), c("opoly", "npoly", "dfourier"))
if (is.na(p)) {
stop("unvalid method\n")
} else {
method_i <- switch(p, opoly_i, npoly_i, dfourier_i) # dfourier_i
}
n <- length(OpnCoe$mshape) #todo
if (is.null(fac)) {
message("no 'fac' provided, returns meanshape")
coe.mshape <- apply(OpnCoe$coe, 2, FUN)
mod.mshape <- OpnCoe$mod
mod.mshape$coefficients <- coe.mshape
return(method_i(mod.mshape))
}
f <- fac_dispatcher(x, fac) %>% factor
fl <- levels(f)
shp <- list()
rows <- numeric()
coe <- matrix(NA, nrow = nlevels(f), ncol = ncol(OpnCoe$coe),
dimnames = list(fl, colnames(OpnCoe$coe)))
mod.mshape <- OpnCoe$mod
for (i in seq(along = fl)) {
coe.i <- OpnCoe$coe[f == fl[i], ]
rows[i] <- which(f == fl[i])[1]
if (is.matrix(coe.i)) {
coe.i <- apply(coe.i, 2, FUN)
}
mod.mshape$coeff <- coe.i
coe[i, ] <- coe.i
shp[[i]] <- method_i(mod.mshape)
}
names(shp) <- fl
Coe2 <- OpnCoe
Coe2$coe <- coe
Coe2$fac <- slice(Coe2$fac, rows)
res <- list(Coe = Coe2, shp = shp) %>%
.prepend_class("MSHAPES")
return(res)
}
#' @export
MSHAPES.LdkCoe <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...) {
LdkCoe <- x
if (is.null(fac)) {
message("no 'fac' provided. Returns meanshape")
return(MSHAPES(LdkCoe$coo))
}
f <- fac_dispatcher(x, fac) %>% factor
fl <- levels(f)
shp <- list()
rows <- numeric()
for (i in seq(along = fl)) {
shp[[i]] <- MSHAPES(LdkCoe$coo[f == fl[i]], FUN=FUN)
rows[i] <- which(f == fl[i])[1]
}
names(shp) <- fl
Coe2 <- Ldk(shp, fac=slice(LdkCoe$fac, rows))
res <- list(Coe = Coe2, shp = shp) %>%
.prepend_class("MSHAPES")
return(res)
}
#' @export
MSHAPES.PCA <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...){
# check for single individuals within a group..
x0 <- x
# f data_frame
# first dispatch
f <- fac_dispatcher(x, fac)
fdf <- dplyr::tibble(fac=f)
# res data_frame
res <- x$x %>%
tibble::as_tibble() %>%
dplyr::bind_cols(fdf, .) %>%
dplyr::group_by(fac) %>%
dplyr::summarise_all(FUN)
# just change the fac for the real name and return
colnames(res)[1] <- fac %>% as.character() %>% `[`(-1)
res
}
#' @export
MSHAPES.LDA <- function(x, fac=NULL, FUN=mean, nb.pts = 120, ...){
# check for single individuals within a group..
x0 <- x
# # if fac provided, dispatch it - not sure this has an utility
# if (!missing(fac))
f <- fac_dispatcher(x, fac)
#otherwise use the one from the LDA
# else
# f <- x$f
# f data_frame
# first dispatch
fdf <- dplyr::tibble(fac=f)
# res data_frame
res <- x$mod.pred$x %>%
tibble::as_tibble() %>%
dplyr::bind_cols(fdf, .) %>%
dplyr::group_by(fac) %>%
dplyr::summarise_all(FUN)
# just change the fac for the real name and return
colnames(res)[1] <- fac %>% as.character() %>% `[`(-1)
res
}
##### end MSHAPES