Browse code

Added VIP scores for PLS-DA.

Jaehyun Joo authored on 14/11/2023 04:57:56
Showing 2 changed files

... ...
@@ -244,6 +244,8 @@ reduceTSNE <- function(x, ncomp = 2, normalize = TRUE, ...) {
244 244
 ##' * `projection`: The projection matrix.
245 245
 ##' * `fitted.values`: An array of fitted values.
246 246
 ##' * `residuals`: An array of regression residuals.
247
+##' * `vip`: An array of VIP (Variable Importance in the Projection)
248
+##' coefficients.
247 249
 ##' * `centered`: A logical indicating whether the data was mean-centered prior
248 250
 ##' to PLS-DA.
249 251
 ##' * `scaled`: A logical indicating whether the data was scaled prior to
... ...
@@ -316,7 +318,7 @@ reducePLSDA <- function(x, y, ncomp = 2, center = TRUE, scale = FALSE,
316 318
     attr(out, "predictors") <- pls::prednames(fit)
317 319
     attr(out, "coefficients") <- fit$coefficients
318 320
     attr(out, "loadings") <- fit$loadings
319
-    attr(out, "loadings.weights") <- fit$loadings.weights
321
+    attr(out, "loading.weights") <- fit$loading.weights
320 322
     attr(out, "Y.observed") <- y
321 323
     attr(out, "Y.predicted") <- y_predicted
322 324
     attr(out, "Y.scores") <- fit$Yscores
... ...
@@ -325,9 +327,24 @@ reducePLSDA <- function(x, y, ncomp = 2, center = TRUE, scale = FALSE,
325 327
     attr(out, "fitted.values") <- fitted(fit)
326 328
     attr(out, "residuals") <- residuals(fit)
327 329
     attr(out, "ncomp") <- fit$ncomp
330
+    attr(out, "vip") <- .vip_mat(fit)
328 331
     attr(out, "centered") <- center
329 332
     attr(out, "scaled") <- scale
330 333
     attr(out, "validation") <- fit$validation
331 334
     class(out) <- c("reduced.plsda", "matrix", class(out))
332 335
     out
333 336
 }
337
+
338
+.vip_mat <- function(fit) {
339
+  ncomp <- fit$ncomp
340
+  w <- fit$loading.weights
341
+  m <- cor(fit$model$y, fit$scores, use = "pairwise")**2
342
+  vip <- do.call(cbind, lapply(1:ncomp, function(x) .vip_vec(w, m, x)))
343
+  dimnames(vip) <- dimnames(w)
344
+  vip
345
+}
346
+
347
+.vip_vec <- function(w, m, comp) {
348
+  rd <- colSums(m[, 1:comp, drop = FALSE])
349
+  as.vector(sqrt(nrow(w) * rd %*% t(w[, 1:comp]^2) / sum(rd)))
350
+}
... ...
@@ -54,6 +54,8 @@ each component.
54 54
 \item \code{projection}: The projection matrix.
55 55
 \item \code{fitted.values}: An array of fitted values.
56 56
 \item \code{residuals}: An array of regression residuals.
57
+\item \code{vip}: An array of VIP (Variable Importance in the Projection)
58
+coefficients.
57 59
 \item \code{centered}: A logical indicating whether the data was mean-centered prior
58 60
 to PLS-DA.
59 61
 \item \code{scaled}: A logical indicating whether the data was scaled prior to