-
Notifications
You must be signed in to change notification settings - Fork 1
/
.Rhistory
322 lines (322 loc) · 10.9 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
create("FastPCA")
x = matrix(runif(900,0,20), nrow = 30, ncol = 30)
x
y = princomp(x)
y
y$sdev
?princomp
?(coop::pcor())
?coop::pcor()
orig_dat = x
cent_mat = orig_dat - rep(1, nrow(orig_dat)) %*% t(colMeans(orig_dat))
relat = coop::pcor(cent_mat)
relat = coop::covar(cent_mat)
?princomp
cent_mat = orig_dat - rep(1, nrow(orig_dat)) %*% t(colMeans(orig_dat))
if (precomp == TRUE) {
relat = cent_mat
} else if ( (cor == TRUE) & (precomp == FALSE) {
relat = coop::pcor(cent_mat)
} else {
relat = coop::covar(cent_mat)
}
eig_vals = PRIMME::eigs_sym(relat, PCs)
loadings = eig_vals$vectors
colnames(loadings) = paste0("Comp ", 1:ncol(loadings))
row.names(loadings) = colnames(orig_dat)
scores = t(t(eig_vals$vectors) %*% t(cent_mat))
return(list(scores = scores, loadings = loadings))
}
cent_mat = orig_dat - rep(1, nrow(orig_dat)) %*% t(colMeans(orig_dat))
cent_mat = orig_dat - rep(1, nrow(orig_dat)) %*% t(colMeans(orig_dat))
cor = FALSE
precomp = FALSE
PCs
FastPCA = function(orig_dat, PCs = 2, cor = FALSE, precomp = FALSE) {
cent_mat = orig_dat - rep(1, nrow(orig_dat)) %*% t(colMeans(orig_dat))
if (precomp == TRUE) {
relat = cent_mat
} else if ((cor == TRUE) & (precomp == FALSE)) {
relat = coop::pcor(cent_mat)
} else {
relat = coop::covar(cent_mat)
}
eig_vals = PRIMME::eigs_sym(relat, PCs)
loadings = eig_vals$vectors
colnames(loadings) = paste0("Comp ", 1:ncol(loadings))
row.names(loadings) = colnames(orig_dat)
scores = t(t(eig_vals$vectors) %*% t(cent_mat))
return(list(scores = scores, loadings = loadings))
}
FastPCA(x)
z = princomp(x)
plot(z)
remove(z)
?princomp
z = princomp(x)
z$sdev
y = FastPCA(x)
y$scores
sd(y$scores)
sd(y$scores[,1])
y = FastPCA(x)
y
z$sdev
sd(y$scores[,2])
sd(y$scores[,1])
y = FastPCA(x, PCs = 30)
y
sd(y$scores[,1])
z$sdev
z$loadings
z$loadings
y$loadings
plot(PCA)
autoplot(y)
ggplot2::autoplot(y)
PCA_ob = orig_dat
PCA_ob = y
PCA_ob
PCA_ob$scores
PCA_ob$loadings
ggplot2::ggplot(PCA_ob$loadings, aes(x = PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2]))
ggplot2::ggplot(PCA_ob$loadings, ggplot2::aes(x = PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2]))
ggplot2::ggplot(,ggplot2::aes(x = PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2]))
PC_Frame = cbind.data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2])
PC1 = 1
PC2 = 2
PC_Frame = cbind.data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2])
PC_Frame
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2])
PC_Frame
head(PC_Frame)
colnames(PC_Frame) = c(paste0("PC ", PC1), paste0("PC ", PC2))
ggplot2::ggplot(PC_Frame, aes(x = PC_Frame$`PC 1`, y = PC_Frame$`PC 2`))
ggplot2::ggplot(PC_Frame, ggplot::aes(x = PC_Frame$`PC 1`, y = PC_Frame$`PC 2`))
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame$`PC 1`, y = PC_Frame$`PC 2`))
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2]))
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) + geom_point()
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) + ggplot2::geom_point()
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1), y = paste0("PC ", PC2))
PC_Frame
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2], label = row.names(PCA_ob$loadings))
PCA_ob$loadings
orig_dat
colnames(orig_dat) = colnames(paste0("Var"), 1:30)
colnames(orig_dat) = colnames(paste0("Var", 1:30))
FastPCA = function(orig_dat, PCs = 2, cor = FALSE, precomp = FALSE) {
cent_mat = orig_dat - rep(1, nrow(orig_dat)) %*% t(colMeans(orig_dat))
if (precomp == TRUE) {
relat = cent_mat
} else if ((cor == TRUE) & (precomp == FALSE)) {
relat = coop::pcor(cent_mat)
} else {
relat = coop::covar(cent_mat)
}
eig_vals = PRIMME::eigs_sym(relat, PCs)
loadings = eig_vals$vectors
colnames(loadings) = paste0("Comp ", 1:ncol(loadings))
row.names(loadings) = colnames(orig_dat)
scores = t(t(eig_vals$vectors) %*% t(cent_mat))
return(list(scores = scores, loadings = loadings))
}
y = FastPCA(orig_dat)
y$scores
y$loadings
colnames(orig_dat)
colnames(orig_dat) = paste0("Var", 1:30)
y = FastPCA(orig_dat)
y$loadings
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2], label = row.names(PCA_ob))
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2], label = row.names(PCA_ob$loadings))
PCA_ob$loadings
PCA_ob = y
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2],
label = row.names(PCA_ob$loadings))
PC_Frame
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) + geom_text()
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) + ggplot2::geom_text()
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) + ggplot2::geom_text(label = PC_Frame$label)
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(label = PC_Frame$label, vjust = .5)
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(aes(label = PC_Frame$label), vjust = .5)
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = .5)
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = .9)
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = .1.5)
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = .1.5)
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = 1.5)
is.integer(1.5)
is.integer(1)
is.wholenumber(1.5)
check.integer(1.5)
names(PCA_ob)
names(PCA_ob) %in% c("scores", "loadings")
plotPCs = function(PCA_ob, PC1 = 1, PC2 = 2, label = FALSE) {
if (all(names(PCA_ob) %in% c("scores", "loadings"))) {
stop("Please use object from FastPCA")
}
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2],
label = row.names(PCA_ob$loadings))
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = 1.5)
}
y = FastPCA(orig_dat)
y
plotPCs(y)
y
names(y)
plotPCs = function(PCA_ob, PC1 = 1, PC2 = 2, label = FALSE) {
if (!all(names(PCA_ob) %in% c("scores", "loadings"))) {
stop("Please use object from FastPCA")
}
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2],
label = row.names(PCA_ob$loadings))
ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2)) +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = 1.5)
}
plotPCs(y)
plotPCs(y, PC2 = 1)
plotPCs(y, PC2 = 3)
plotPCs = function(PCA_ob, PC1 = 1, PC2 = 2, label = TRUE) {
plotPCs = function(PCA_ob, PC1 = 1, PC2 = 2, label = TRUE) {
if (!all(names(PCA_ob) %in% c("scores", "loadings"))) {
stop("Please use object from FastPCA")
}
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2],
label = row.names(PCA_ob$loadings))
PC_plot = ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2))
if (label) {
PC_plot +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = 1.5
} else {
PC_plot
}
}
plotPCs = function(PCA_ob, PC1 = 1, PC2 = 2, label = TRUE) {
if (!all(names(PCA_ob) %in% c("scores", "loadings"))) {
stop("Please use object from FastPCA")
}
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2],
label = row.names(PCA_ob$loadings))
PC_plot = ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2))
if (label) {
PC_plot +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = 1.5)
} else {
PC_plot
}
}
plotPCs(y)
plotPCs(y, PC2 = 4)
plotPCs(y, PC2 = 3)
names(PCA_ob)
y = FastPCA(orig_dat, PCs =10)
plotPCs = function(PCA_ob, PC1 = 1, PC2 = 2, label = TRUE) {
if (!all(names(PCA_ob) %in% c("scores", "loadings"))) {
stop("Please use object from FastPCA")
}
PC_Frame = data.frame(PCA_ob$loadings[,PC1], PCA_ob$loadings[,PC2],
label = row.names(PCA_ob$loadings))
PC_plot = ggplot2::ggplot(PC_Frame, ggplot2::aes(x = PC_Frame[,1], y = PC_Frame[,2])) +
ggplot2::geom_point() + ggplot2::labs(x = paste0("PC ", PC1),
y = paste0("PC ", PC2))
if (label) {
PC_plot +
ggplot2::geom_text(ggplot2::aes(label = PC_Frame$label), vjust = 1.5)
} else {
PC_plot
}
}
plotPCs
plotPCs(y)
plotPCs(y, PC2 = 10)
plotPCs(y, PC2 = 10, labels = FALSE)
plotPCs(y, PC2 = 10, label = FALSE)
document()
devtools::document()
?FastPCA
?princomp
princomp(orig_dat)
z = princomp(orig_dat)
z$loadings
head(z$loadings)
head(y$loadings)
z$loadings
pca
z = prcomp(orig_dat)
z
z$rotation[,2]
head(y$loadings[,])
head(y$loadings[,2])
z$rotation[,2]
?prcomp
?eigen
?coop::covar()
z$x
head(z$x)
z$x[,1]
y$scores[,1]
tcrossprod
?Matrix::tcrossprod()
FastPCA = function(orig_dat, PCs = 2, cor = FALSE, precomp = FALSE) {
#Center and scale
cent_mat = orig_dat - rep(1, nrow(orig_dat)) %*% t(colMeans(orig_dat))
#Calculate covariance, correlation or neither depending on inputs
if (precomp == TRUE) {
relat = cent_mat
} else if ((cor == TRUE) & (precomp == FALSE)) {
relat = coop::pcor(cent_mat)
} else {
relat = coop::covar(cent_mat)
}
#Get appropriate number of eigenvectors
eig_vals = PRIMME::eigs_sym(relat, PCs)
#Pull out eigenvectors
loadings = eig_vals$vectors
#Name column based on PC
colnames(loadings) = paste0("Comp ", 1:ncol(loadings))
#Check for column names and label rows when necessary
if (!is.null(colnames(orig_dat)))
row.names(loadings) = colnames(orig_dat)
#Get scores
scores = t(crossprod(t(eig_vals$vectors),t(cent_mat)))
return(list(scores = scores, loadings = loadings))
}
devtools::use_vignette("FastPCA")
sim_mat = matrix(runif(30000^2, 0, 200), 30000, 30000)
sim_mat = matrix(runif(30000^2, 0, 200), 30000, 30000)