forked from califano-lab/PISCES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
512 lines (512 loc) · 13 KB
/
.Rhistory
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
sum(x**2)
}))
test.err <- mean(apply(test.x - test.xp, 1, function(x) {
sum(x**2)
}))
# add to vector
train.rerr <- c(train.rerr, train.err)
test.rerr <- c(test.rerr, test.err)
}
plot(2:num.feats, train.rerr)
plot(2:num.feats, test.rerr)
num.samps <- 10000
num.feats <- 500
sim.dat <- rmvnorm(n = num.samps, mean = rep(0, num.feats),
sigma = diag(x = 1, nrow = num.feats, ncol = num.feats))
rownames(sim.dat) <- paste('sample', 1:nrow(sim.dat), sep = '.')
train.samps <- sample(rownames(sim.dat), size = num.samps * 0.63)
test.samps <- setdiff(rownames(sim.dat), train.samps)
train.x <- sim.dat[train.samps,]
test.x <- sim.dat[test.samps,]
train.pca <- prcomp(train.x)
train.z <- predict(train.pca, newdata = train.x)
test.z <- predict(train.pca, newdata = test.x)
train.rerr <- c()
test.rerr <- c()
for (l in 2:num.feats) {
# make x
train.xp <- train.z[,1:l] %*% t(train.pca$rotation[,1:l])
test.xp <- test.z[,1:l] %*% t(train.pca$rotation[,1:l])
# get err
train.err <- mean(apply(train.x - train.xp, 1, function(x) {
sum(x**2)
}))
test.err <- mean(apply(test.x - test.xp, 1, function(x) {
sum(x**2)
}))
# add to vector
train.rerr <- c(train.rerr, train.err)
test.rerr <- c(test.rerr, test.err)
}
plot(2:num.feats, train.rerr)
library(mvtnorm)
## general params
pure.samps <- 20
mix.samps <- 20
## set distribution parameters
a.mean <- c(0, 0)
b.mean <- c(10, 0)
sigma.mat <- matrix(c(1, 0, 0, 1), nrow = 2)
## sample pure populatios
a.pure <- rmvnorm(pure.samps, mean = a.mean, sigma = sigma.mat)
b.pure <- rmvnorm(pure.samps, mean = b.mean, sigma = sigma.mat)
## sample mixed populations
a.mix <- rmvnorm(mix.samps, mean = a.mean, sigma = sigma.mat)
b.mix <- rmvnorm(mix.samps, mean = b.mean, sigma = sigma.mat)
mix.per <- runif(mix.samps)
mix.x <- a.mix[,1] * mix.per + b.mix[,1] * (1 - mix.per)
mix.y <- a.mix[,2] * mix.per + b.mix[,2] * (1 - mix.per)
mix.dat <- cbind(mix.x, mix.y)
## combine
sim.mat <- Reduce(rbind, list(a.pure, b.pure, mix.dat))
true.phi <- c(rep(0, pure.samps), rep(1, pure.samps), mix.per)
plot(sim.mat[,1], sim.mat[,2])
sample(1:5, 2)
X <- sim.mat
k <- 2
N <- nrow(X)
# create initial centers by random sampling
c_mat <- t(X[sample(1:N, 2),])
c_mat
# create initial centers by random sampling, empty phi.mat
c_mat <- t(X[sample(1:N, 2),])
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
#' Finds the minimum u_ijk - assignment of x_i to pair of clusters c_j and c_k.
#'
#' @param x_i Data for x_i.
#' @param c_j Cluster representative for cluster j.
#' @param c_k Cluster representative for cluster k.
#' @return u_ijk; value betwen 0 and 1.
MinU <- function(x_i, c_j, c_k) {
# compute
u_ijk <- sum(t((c_k - c_j)) * (c_k - x_i)) / sum((c_k - c_j)**2)
# bound appropriately
u_ijk <- max(u_ijk, 0)
u_ijk <- min(u_ijk, 1)
# return
return(u_ijk)
}
#' Finds the optimal phi vector for data x_i given matrix of cluster representatives c_mat.
#'
#' @param x_i Data x_i.
#' @param c_mat Matrix of cluster representatives.
#' @return 1xC Phi vector.
MinPhi <- function(x_i, c_mat) {
# set tracker variables
min.err <- Inf
opt.u <- Inf
best.inds <- c(-1, -1)
# loop through all pairs
for (j in 1:(nrow(c_mat) - 1)) {
for (k in (j + 1):nrow(c_mat)) {
# minimize u
min.u <- MinU(x_i, c_mat[j,], c_mat[k,])
# calculate error
x.err <- sum((x_i - (min.u*c_mat[j,] + (1 - min.u)*c_mat[k,]))**2)
# update if necessary
if (x.err < min.err) {
min.err <- x.err
opt.u <- min.u
best.inds <- c(j, k)
}
}
}
# return phi vec
print(best.inds)
phi.vec <- rep(0, nrow(c_mat))
phi.vec[best.inds] <- c(opt.u, 1 - opt.u)
return(phi.vec)
}
phi_mat[,a] <- MinPhi(X[a,], c_mat)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
phi_mat
phi.prod <- inv(phi_mat %*% t(phi_mat)) %*% phi_mat
phi.prod <- solve(phi_mat %*% t(phi_mat)) %*% phi_mat
phi.prod
new.c <- phi.prod %*% X
new.c
return(t(new.c))
X <- sim.mat
k <- 3
N <- nrow(X)
# create initial centers by random sampling, empty phi.mat
c_mat <- t(X[sample(1:N, k),])
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
new.c
# create initial centers by random sampling, empty phi.mat
c_mat <- t(X[sample(1:N, k),])
c_mat
#' Finds the optimal phi vector for data x_i given matrix of cluster representatives c_mat.
#'
#' @param x_i Data x_i.
#' @param c_mat Matrix of cluster representatives.
#' @return 1xC Phi vector.
MinPhi <- function(x_i, c_mat) {
# set tracker variables
min.err <- Inf
opt.u <- Inf
best.inds <- c(-1, -1)
# loop through all pairs
for (j in 1:(nrow(c_mat) - 1)) {
for (k in (j + 1):nrow(c_mat)) {
# minimize u
min.u <- MinU(x_i, c_mat[j,], c_mat[k,])
# calculate error
x.err <- sum((x_i - (min.u*c_mat[,j] + (1 - min.u)*c_mat[,k]))**2)
# update if necessary
if (x.err < min.err) {
min.err <- x.err
opt.u <- min.u
best.inds <- c(j, k)
}
}
}
# return phi vec
phi.vec <- rep(0, nrow(c_mat))
phi.vec[best.inds] <- c(opt.u, 1 - opt.u)
return(phi.vec)
}
#' Finds optimal cluster centers given X and Phi matrices.
#'
#' @param x_mat Design matrix X (features X samples).
#' @param phi_mat Matrix of pairwise cluster assignments ().
#' @return (Clusters X Features) matrix of cluster representatives.
MinC <- function(x_mat, phi_mat) {
phi.prod <- solve(phi_mat %*% t(phi_mat)) %*% phi_mat
new.c <- phi.prod %*% X
return(t(new.c))
}
X <- sim.mat
k <- 3
N <- nrow(X)
# create initial centers by random sampling, empty phi.mat
c_mat <- t(X[sample(1:N, k),])
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
MinPhi(X[a,], c_mat)
#' Finds the optimal phi vector for data x_i given matrix of cluster representatives c_mat.
#'
#' @param x_i Data x_i.
#' @param c_mat Matrix of cluster representatives.
#' @return 1xC Phi vector.
MinPhi <- function(x_i, c_mat) {
# set tracker variables
min.err <- Inf
opt.u <- Inf
best.inds <- c(-1, -1)
# loop through all pairs
for (j in 1:(nrow(c_mat) - 1)) {
for (k in (j + 1):nrow(c_mat)) {
# minimize u
min.u <- MinU(x_i, c_mat[j,], c_mat[k,])
# calculate error
x.err <- sum((x_i - (min.u*c_mat[,j] + (1 - min.u)*c_mat[,k]))**2)
# update if necessary
if (x.err < min.err) {
min.err <- x.err
opt.u <- min.u
best.inds <- c(j, k)
}
}
}
# return phi vec
phi.vec <- rep(0, ncol(c_mat))
phi.vec[best.inds] <- c(opt.u, 1 - opt.u)
return(phi.vec)
}
#' Finds optimal cluster centers given X and Phi matrices.
#'
#' @param x_mat Design matrix X (features X samples).
#' @param phi_mat Matrix of pairwise cluster assignments ().
#' @return (Clusters X Features) matrix of cluster representatives.
MinC <- function(x_mat, phi_mat) {
phi.prod <- solve(phi_mat %*% t(phi_mat)) %*% phi_mat
new.c <- phi.prod %*% X
return(t(new.c))
}
X <- sim.mat
k <- 3
N <- nrow(X)
# create initial centers by random sampling, empty phi.mat
c_mat <- t(X[sample(1:N, k),])
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
phi_mat
# assign C
new.c_mat <- MinC(X, phi_mat)
phi_mat %*% t(phi_mat)
X <- sim.mat
k <- 2
N <- nrow(X)
# create initial centers by random sampling, empty phi.mat
c_mat <- t(X[sample(1:N, k),])
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
new.c_mat
c_mat
X <- sim.mat
k <- 2
converged <- FALSE
N <- nrow(X)
# create initial centers by random sampling, empty phi.mat
c_mat <- t(X[sample(1:N, k),])
# create initial centers by random sampling, empty phi.mat
new.c_mat <- t(X[sample(1:N, k),])
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
c_mat
new.c_mat
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
N <- nrow(X)
X <- scale(X, scale = FALSE)
# create initial centers by random sampling, empty phi.mat
new.c_mat <- t(X[sample(1:N, k),])
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
plot(X[,1], X[,2])
#' Finds optimal cluster centers given X and Phi matrices.
#'
#' @param x_mat Design matrix X (features X samples).
#' @param phi_mat Matrix of pairwise cluster assignments ().
#' @return (Clusters X Features) matrix of cluster representatives.
MinC <- function(x_mat, phi_mat) {
phi.prod <- solve(phi_mat %*% t(phi_mat)) %*% phi_mat
new.c <- phi.prod %*% X
return(new.c)
}
X <- sim.mat
k <- 2
converged <- FALSE
N <- nrow(X)
X <- scale(X, scale = FALSE)
# create initial centers by random sampling, empty phi.mat
new.c_mat <- t(X[sample(1:N, k),])
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
if (!converged) {c_mat <- new.c_mat}
phi_mat <- matrix(0L, nrow = k, ncol = N)
# assign phi
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
X <- sim.mat
k <- 2
converged <- FALSE
N <- nrow(X)
X <- scale(X, scale = FALSE)
# create initial centers by random sampling, empty phi.mat
new.c_mat <- t(X[sample(1:N, k),])
iter.count <- 0
while (!converged) {
iter.count <- iter.count + 1
print(iter.count)
# assign phi
phi_mat <- matrix(0L, nrow = k, ncol = N)
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
# check convergence
if (mean(abs(new.c_mat - c_mat)) < 0.01) { converged <- TRUE }
}
print(new.c_mat)
new.c_mat
c_mat
N <- nrow(X)
X <- scale(X, scale = FALSE)
# create initial centers by random sampling, empty phi.mat
new.c_mat <- t(X[sample(1:N, k),])
iter.count <- 0
while (!converged) {
iter.count <- iter.count + 1
print(iter.count)
# reset C
c_mat <- new.c_mat
# assign phi
phi_mat <- matrix(0L, nrow = k, ncol = N)
for (a in 1:N) {
phi_mat[,a] <- MinPhi(X[a,], c_mat)
}
# assign C
new.c_mat <- MinC(X, phi_mat)
print(new.c_mat)
# check convergence
if (mean(abs(new.c_mat - c_mat)) < 0.01) { converged <- TRUE }
}
print(new.c_mat)
phi_mat
plot(phi_mat[1,], true.phi)
plot(phi_mat[2,], true.phi)
plot(sim.mat[,1], sim.mat[,2])
plot(phi_mat[2,], true.phi)
plot(phi_mat[1,], true.phi)
devtools::install_github(repo = "califano-lab/PISCES", force = TRUE, build_vignettes = TRUE)
setwd('C://Users/lvlah/linux/ac_lab/PISCES-dev/')
dectools::document()
library(devtools)
document()
git config --global credential.helper store
devtools::install_github(repo = "califano-lab/PISCES", force = TRUE, build_vignettes = TRUE)