[381c22]: / R / calc_pcareduceres.R

Download this file

23 lines (22 with data), 546 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
calc_pcareduceres <- function(obj, reduce) {
if (!reduce) {
return(t(obj))
}
df <- data.frame(x = 1:20)
df$sdev <- prcomp(t(obj), scale = TRUE)$sdev[1:20]
optpoint <- which.min(
vapply(
2:10,
function(i) {
data$x2 <- pmax(0, data$x - i)
sum(lm(sdev ~ x + x2, df)$residuals^2)
},
0
)
)
pcadim <- optpoint + 1
tmpdata <- t(apply(obj, 1, scale))
colnames(tmpdata) <- colnames(obj)
tmppc <- prcomp(t(tmpdata), scale = TRUE)
return(t(tmpdata) %*% tmppc$rotation[, 1:pcadim])
}