File: pca.r

package info (click to toggle)
sm 2.2-5.4-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 1,496 kB
  • ctags: 7
  • sloc: f90: 259; ansic: 6; makefile: 1
file content (230 lines) | stat: -rwxr-xr-x 8,622 bytes parent folder | download | duplicates (2)
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
sm.pca <- function(x, Y, h, cor = TRUE, nperm = 100, pc = 1, ...) {

   opt <- sm.options(list(...))
   replace.na(opt, test,    TRUE)
   replace.na(opt, band,    TRUE)
   replace.na(opt, display, c("eigenvalues", "eigenvectors"))
   replace.na(opt, col,     "red")
   
   # If x is a previously computed sm.pca object then plot as required
   
   if (is.list(x) & ("evals" %in% names(x))) {
   	
   	  evalmat <- t(x$evals)
   	  evec    <- x$evecs
      zgrid   <- x$eval.points
   	  ngrid   <- length(zgrid)
    	  if ("evals.perm" %in% names(x)) neweval <- x$evals.perm
   	  if ("evecs.perm" %in% names(x)) newevec <- x$evecs.perm
      ylim.missing <- any(is.na(opt$ylim))

      if ("eigenvalues" %in% opt$display) {
         e   <- evalmat[pc, ]
         rng <- range(e)
         if (opt$band & ("evals.perm" %in% names(x))) {
            r           <- neweval[pc, , ]
            neweig1     <- apply(r, 1, function(x) quantile(x, prob = 0.025))
            neweig2     <- apply(r, 1, function(x) quantile(x, prob = 0.975))
            rng         <- range(rng, neweig1, neweig2)
            mtch        <- match("band", names(x))
            x$band      <- cbind(q1 = neweig2, q2 = neweig1)
         }
         if (ylim.missing) opt$ylim <- rng
         plot(zgrid, e, type = "n", xlab = opt$xlab, ylab = "Variance", ylim = opt$ylim)
         if (opt$band & ("evals.perm" %in% names(x)))
            polygon(c(zgrid, rev(zgrid)), c(neweig1, rev(neweig2)), col = opt$col.band, border = NA)
         lines(zgrid, e, lwd = opt$lwd, col = opt$col)
      }

      if ("eigenvectors" %in% opt$display) {

         e <- evec[ , pc, ]
         p <- dim(e)[1]

         if (opt$band & ("evecs.perm" %in% names(x))) {
            r <- newevec[ , pc, , ]
            for(k in 1:p) {
            for(i in 1:nperm) {
            for(j in 1:ngrid) {
               if((sign(sum(e[k, ])) == sign(r[k, j, i])) == FALSE)
                  r[k, j, i] <- -1 * r[k, j, i]
            }}}

            xmat   <- matrix(nrow = 2 * ngrid - 1, ncol = p)
            clrmat <- matrix(nrow = 2 * ngrid - 2, ncol = p)
            for (i in 1:p) {
               q    <- apply(r[i, , ], 1, function(x) quantile(x, prob = c(0.025, 0.975)))
               ind  <- as.numeric(sign((q[1, ] - e[i, ]) * (q[2, ] - e[i, ])) < 1)
               ind  <- rep(ind, each = 2)
               ind  <- ind[-c(1, 2 * ngrid)]
               clrb <- rep(255, 3)
               clri <- col2rgb(i)
               clri <- rgb(clri[1] + ind * (clrb[1] - clri[1]) * 0.9,
                           clri[2] + ind * (clrb[2] - clri[2]) * 0.9,
                           clri[3] + ind * (clrb[3] - clri[3]) * 0.9,
                           maxColorValue = 255)
               xg   <- c(rbind(e[i, ], e[i, ] + c(0.5 * diff(e[i, ]), 0)))
               xmat[ , i]   <- xg[-2 * ngrid]
               clrmat[ , i] <- clri
            }
      
            zg   <- c(rbind(zgrid, zgrid + c(0.5 * diff(zgrid), 0)))
            zg   <- zg[-2 * ngrid]
            if (ylim.missing) opt$ylim <- range(xmat)
            plot(range(zg), range(xmat), type = "n", xlab = opt$xlab, ylab = "PC loadings", ylim = opt$ylim)
            for (i in 1:p)
               segments(zg[-length(zg)], xmat[-nrow(xmat), i], zg[-1], xmat[-1, i], col = clrmat[ , i], lty = i)
            x$xgrid.plot <- zg
            x$evecs.plot <- xmat
            x$col.plot   <- clrmat
         }
         else {
            replace.na(opt, ylim, range(e))
            plot(range(zgrid), range(e), type = "n", xlab = opt$xlab, ylab = "PC loadings", ylim = opt$ylim)
            for (i in 1:p) lines(zgrid, e[i, ], col = i, lty = i)
         }
 
      }
   
      if (opt$test & ("evals.perm" %in% names(x))) {
         n    <- length(x$x)
         p    <- ncol(x$Y)
         S    <- sm.weight(x$x, x$x, h = x$h, options = list(poly.index = 1))
         mht  <- S %*% x$Y
         S    <- x$Y - mht
         # S    <- sweep(x$Y, 2, apply(x$Y, 2, mean))
         sdv  <- if (cor) apply(x$Y, 2, sd) else rep(1, p)
         S    <- S / matrix(rep(sdv, each = n), ncol = p)
         S    <- t(S) %*% S / n
         pc0  <- eigen(S, symmetric = TRUE)
         
         e    <- evalmat[pc, ]
         r    <- neweval[pc, , ]         
         tst1 <- sum(abs(e - pc0$values[pc]))
         ref1 <- apply(abs(r - pc0$values[pc]), 2, sum)
         pv1  <- length(which(ref1 > tst1)) / length(ref1)
         
         e0   <- pc0$vectors[ , pc]
         e    <- evec[ , pc, ]
         r    <- newevec[ , pc, , ]
         tst2 <- sum(1 - apply(sweep(e, 1, e0, "*"), 2, sum)^2)
         ref2 <- apply(1 - apply(sweep(r, 1, e0, "*"), 2:3, sum)^2, 2, sum)
         pv2  <- length(which(ref2 > tst2)) / length(ref2)

         x$p.values  <- pv1
         x$p.vectors <- pv2
         if (opt$verbose > 0) {
            cat("Eigenvalues:  p =", round(pv1, 3), "\n")
            cat("Eigenvectors: p =", round(pv2, 3), "\n")
         }
      }
   
      return(invisible(x))
   }
   
   #  Compute the information needed for plotting
 
   if (!is.vector(x)) stop("x should be a vector.")
   if (!is.matrix(Y)) stop("Y should be a matrix.")
   if (!(length(x) == nrow(Y))) stop("the length of x should match the number of rows of Y.")
   
   x.name  <- deparse(substitute(x))
   y.name  <- deparse(substitute(Y))
   z       <- x

   ind     <- apply(cbind(x, Y), 1, function(x) any(is.na(x)))
   z       <- z[!ind]
   Y       <- Y[!ind, ]
   
   if(missing(h)) h <- h.select(x, Y[ , 1], ...)

   replace.na(opt, ngrid, 25)
   replace.na(opt, eval.points, seq(min(z), max(z), length = opt$ngrid))
   replace.na(opt, xlab,  x.name)
   zgrid <- opt$eval.points
   ngrid <- opt$ngrid

   n          <- nrow(Y)
   p          <- ncol(Y)
   evec       <- array(0, c(p, p, ngrid))
   evalmat    <- matrix(0, nrow = p, ncol = ngrid)
   varmat     <- matrix(0, nrow = p, ncol = ngrid)
   mhat       <- matrix(0, nrow = p, ncol = ngrid)
   indmat     <- matrix(0, nrow = p, ncol = ngrid)
   indmat[,1] <- 1:p
   max.ind    <- function(x, p)  which(abs(x) == max(abs(x)))

   # Vtot <- matrix(0, p, p)

   for (i in 1:ngrid) {

      S         <- sm.weight(z, zgrid[i], h = h, options = list(poly.index = 1))
      mhat[, i] <- as.vector(S %*% Y)

      D         <- Y - matrix(rep(mhat[, i], n), ncol = p, byrow = TRUE)
      sdvec     <- if (cor) apply(Y, 2, sd) else rep(1, p)
      D         <- D / matrix(rep(sdvec, each = n), ncol = p)
      S         <- sm.weight(z, zgrid[i], h = h, options = list(poly.index = 1))
      V         <- t(D) %*% diag(as.vector(S)) %*% D
	
      # Vtot      <- Vtot + length(z) * V

      pca       <- eigen(V, symmetric = TRUE)

      if (i > 1) {
        d          <- crossprod(pca$vectors, old.vectors)
        indmat[,i] <- apply(d, 1, max.ind, p = p)
        indmat[,i] <- indmat[ , i - 1][indmat[ , i]]
        dmax       <- d[cbind(1:p, indmat[ , i])]
        for (j in (1:p)[dmax < 0]) 
           pca$vectors[ , j] <- -pca$vectors[ , j]
      }
      
      pca$values[pca$values < 0] <- 0
      pcvar        <- pca$values / sum(pca$values)
      ord          <- order(indmat[ , i])
      varmat[, i]  <- pcvar[ord]
      evalmat[, i] <- pca$values[ord]
      evec[ , , i] <- pca$vectors[, ord]

      old.vectors  <- pca$vectors
      ind.old      <- indmat[,i]
   }
     
   result <- list(xgrid = zgrid, evecs = evec, evals = t(evalmat), mhat = t(mhat),
                  var.explained = t(varmat), eval.points = opt$eval.points, xlab = opt$xlab,
                  h = h, x = x, Y = Y, nperm = nperm, cor = cor)

   if (opt$test | opt$band) {
      newevec <- array(NA, c(ncol(Y), ncol(Y), ngrid, nperm))
      neweval <- array(NA, c(ncol(Y), ngrid, nperm))
      # Vtot    <- array(NA, c(dim(Y)[2], dim(Y)[2], nperm))
      znew    <- replicate(nperm, sample(z, length(z)), simplify = TRUE)

      spc <- function(x){
         spc2    <- sm.pca(x, Y, h, cor = cor, eval.points = opt$eval.points,
                                 test = FALSE, band = FALSE, display = "none")	
         # Vtot    <- spc2$Vtot
         newevec <- spc2$evecs
         neweval <- t(spc2$evals)
         list(newevec = newevec, neweval = neweval)
      }

      res <- apply(znew, 2, spc)

      for(i in 1:nperm){
         # Vtot[ , , i]    <- res[[i]]$Vtot
         newevec[ , , , i] <- res[[i]]$newevec
         neweval[ , , i]   <- res[[i]]$neweval
      }
      
   result$evecs.perm <- newevec
   result$evals.perm <- neweval
   }
   
   if (!("none" %in% opt$display)) result <- sm.pca(result, display = opt$display, pc = pc)


   invisible(result)
}