-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdeprecated.R
168 lines (153 loc) · 4.64 KB
/
deprecated.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
# identity_rank <- function(rank){
# diag(1, rank)
# }
# lambda_rank <- function(x, y, rank, k = 0){
# cov_x <- cov(x) + k * diag(dim(x)[2])
# cov_y <- cov(y) + k * diag(dim(y)[2])
# cov_xy <- cov(x, y)
# cov_yx <- t(cov_xy)
# r_star <- solve(sqrt_matrix(cov_x)) %*%
# cov_xy %*%
# solve(cov_y) %*%
# cov_yx %*%
# solve(sqrt_matrix(cov_x))
# eigens <- Re(eigen(r_star)[["values"]])[1:rank]
# diag(eigens)
# }
#' Canonical Covariance Matrix
#'
#' \code{canonical_cov}
#' @inheritParams cva
#'
#' @examples
#' data(COMBO17)
#'
#'
#'
#canonical_cov <- function(x, y, rank = "full", k = 0){
# full_rank <- min(dim(x)[2], dim(y)[2])
# if(rank == "full"){
# reduce_rank <- full_rank
# } else if(rank <= full_rank){
# reduce_rank <- rank
# } else {
# stop("rank out of bounds")
# }
# lambda <- lambda_rank(x, y, reduce_rank, k)
# identity <- identity_rank(reduce_rank)
# cov_mat <- rbind(cbind(lambda, lambda), cbind(lambda, identity))
# mat_names <-c(paste("xi", 1:reduce_rank, sep = ""),
# paste("omega", 1:reduce_rank, sep = ""))
# rownames(cov_mat) <- mat_names
# colnames(cov_mat) <- mat_names
# cov_mat
#}
#' Canonical Correlation Matrix
#'
#' \code{canonical_corr}
#'
#' @inheritParams cva
#'
#'
#canonical_corr <- function(x, y, rank = "full", type = "cov", k = 0){
# full_rank <- min(dim(x)[2], dim(y)[2])
# if(rank == "full"){
# reduce_rank <- full_rank
# } else if(rank <= full_rank){
# reduce_rank <- rank
# } else {
# stop("rank out of bounds")
# }
# identity <- identity_rank(reduce_rank)
# lambda <- sqrt(lambda_rank(x, y, reduce_rank, k))
# cov_mat <- rbind(cbind(identity, lambda), cbind(lambda, identity))
# mat_names <-c(paste("xi", 1:reduce_rank, sep = ""),
# paste("omega", 1:reduce_rank, sep = ""))
# rownames(cov_mat) <- mat_names
# colnames(cov_mat) <- mat_names
# cov_mat
#}
#cva_allpairs_plot <- function(x, y, rank, type = "cov", k = 0){
# scores_object <- cva_scores(x, y, rank, type, k)
# all_pairs <- dplyr::bind_cols(scores_object[["xi"]], scores_object[["omega"]])
# GGally::ggpairs(all_pairs)
#}
#' Predict via Reduced-Rank Principal Component Analysis
#'
#' \code{pca_predict}
#'
#' @param pca_object list object obtained from \code{pca()}
#' @param x_new data frame or matrix of new observations to predict.
#'
#'
#pca_predict <- function(pca_object, x_new){
# pca_object[["C"]] %*% organize(x_new)
#}
#' Reduced-Rank PCA Error
#'
#' \code{pca_error}
#'
#' @inheritParams pca_predict
#'
#'
#pca_error <- function(pca_object, x_new){
# x_new - pca_predict(pca_object, x_new)
#}
#' Reduced-Rank PCA Residuals
#'
#' \code{pca_residuals}
#'
#' @inheritParams pca
#'
#'
#pca_residuals <- function(x, rank = "full", type = "cov", k = 0){
# object <- pca(x, rank, type, k)
# pca_error(object, x)
#}
lda <- function(x, class, rank = "full", type = "cov", k = 0, quadratic = FALSE){
class_names <- class %>%
as_data_frame() %>%
select(class = 1) %>%
mutate(class = as.character(class)) %>%
distinct() %>%
arrange(class) %>%
as.matrix() %>%
as.vector()
if(quadratic == TRUE){
x <- expand_feature_space(x)
}
ordered <- lda_organize(x, class)
x_ordered <- ordered$features_ordered
y_ordered <- ordered$classes_ordered
full_rank <- min(dim(x_ordered)[2], dim(y_ordered)[2])
if(rank == "full"){
reduce_rank <- full_rank
} else if(rank <= full_rank){
reduce_rank <- rank
} else {
stop("rank out of bounds")
}
n <- colSums(y_ordered)
num_classes <- dim(y_ordered)[1]
mean_y <- n / num_classes
prior <- c(mean_y, 1 - sum(mean_y))
names(prior) <- class_names
n_last <- n[length(n)]
#x_organize <- organize(x_ordered)
#y_organize <- organize(y_ordered)
cov_y_inv <- solve(cov(y_ordered) + k * diag(1, dim(y_ordered)[2]))#diag(1 / n) + 1 / n_last * matrix(1, length(n), length(n))
cov_x_inv_sqrt <- sqrt_matrix(solve(cov(x_ordered) + k * diag(1, dim(x_ordered)[2])))
cov_xy <- cov(x_ordered, y_ordered)
cov_yx <- t(cov_xy)
r_star <- cov_x_inv_sqrt %*% cov_xy %*% cov_y_inv %*% cov_yx %*% cov_x_inv_sqrt
eigens <- eigen(r_star)
vecs <- Re(eigens$vectors)[,1:reduce_rank]
gam <- Re(cov_x_inv_sqrt %*% vecs)
h <- t(gam) %*% cov_xy %*% cov_y_inv
G <- as_data_frame(gam)
names(G) <- paste("LD", 1:reduce_rank, sep = "")
H <- as_data_frame(t(h))
names(H) <- names(G)
eigen_portion <- Re(eigens$values[1:reduce_rank] / sum(eigens$values[1:reduce_rank]))
list(prior = prior, G = G, H = H, eigen_portion = eigen_portion)
}