-
Notifications
You must be signed in to change notification settings - Fork 0
/
filter_arrows.R
50 lines (35 loc) · 1.58 KB
/
filter_arrows.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
#' Filter covariance arrows for biplots
#'
#' Filter the variables most represented in the firsts dimensions of an ordination object.
#'
#' @param loadings Data frame, loadings or the covariances
#' between variables and the coordinates calculated.
#' @param min_dist Numeric, ratio representing the minimun distante
#' of arrow's points to their origin. 1 return no variables.
#' @param dimensions Numeric, the number of dimensions where arrows will be projected.
#'
#' @export filter_arrows
filter_arrows <- function(loadings, min_dist = 0.5, dimensions = 2){
filtered_arrows <- loadings[, 1:dimensions]
if (min_dist > 0) {
if (min_dist == 1) warning("min_dist = 1 is too restricte.")
if (dimensions == 2) {
max_x <- max(abs(filtered_arrows[, 1])) * min_dist
max_y <- max(abs(filtered_arrows[, 2])) * min_dist
threshold <- sqrt((max_x ^ 2) + (max_y ^ 2))
arrows_lengths <- sqrt((filtered_arrows[, 1] ^ 2) + (filtered_arrows[, 2] ^ 2))
filtered_arrows <-
subset(filtered_arrows, arrows_lengths > threshold)
} else if (dimensions == 3) {
max_x <- max(abs(filtered_arrows[, 1])) * min_dist
max_y <- max(abs(filtered_arrows[, 2])) * min_dist
max_z <- max(abs(filtered_arrows[, 3])) * min_dist
threshold <- sqrt((max_x ^ 2) + (max_y ^ 2) + (max_z ^ 2))
arrows_lengths <-
sqrt((filtered_arrows[, 1] ^ 2) + (filtered_arrows[, 2] ^ 2) + (filtered_arrows[, 3] ^ 2))
filtered_arrows <-
subset(filtered_arrows, arrows_lengths > threshold)
}
}
return(filtered_arrows)
}