# sgapca: Stochastic Gradient Ascent PCA In onlinePCA: Online Principal Component Analysis

## Description

Online PCA with the SGA algorithm of Oja (1992).

## Usage

 ```1 2``` ```sgapca(lambda, U, x, gamma, q = length(lambda), center, type = c("exact", "nn"), sort = TRUE) ```

## Arguments

 `lambda` optional vector of eigenvalues. `U` matrix of eigenvectors (PC) stored in columns. `x` new data vector. `gamma` vector of gain parameters. `q` number of eigenvectors to compute. `center` optional centering vector for `x`. `type` algorithm implementation: "exact" or "nn" (neural network). `sort` Should the new eigenpairs be sorted?

## Details

The gain vector `gamma` determines the weight placed on the new data in updating each principal component. The first coefficient of `gamma` corresponds to the first principal component, etc.. It can be specified as a single positive number (which is recycled by the function) or as a vector of length `ncol(U)`. For larger values of `gamma`, more weight is placed on `x` and less on `U`. A common choice for (the components of) `gamma` is of the form `c/n`, with `n` the sample size and `c` a suitable positive constant.
The Stochastic Gradient Ascent PCA can be implemented exactly or through a neural network. The latter is less accurate but faster.
If `sort` is TRUE and `lambda` is not missing, the updated eigenpairs are sorted by decreasing eigenvalue. Otherwise, they are not sorted.

## Value

A list with components

 `values` updated eigenvalues or NULL. `vectors` updated principal components.

## References

Oja (1992). Principal components, Minor components, and linear neural networks. Neural Networks.

## See Also

`ghapca`, `snlpca`

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19``` ```## Initialization n <- 1e4 # sample size n0 <- 5e3 # initial sample size d <- 10 # number of variables q <- d # number of PC to compute x <- matrix(runif(n*d), n, d) x <- x %*% diag(sqrt(12*(1:d))) # The eigenvalues of x are close to 1, 2, ..., d # and the corresponding eigenvectors are close to # the canonical basis of R^d ## SGA PCA xbar <- colMeans(x[1:n0,]) pca <- batchpca(x[1:n0,], q, center=xbar, byrow=TRUE) for (i in (n0+1):n) { xbar <- updateMean(xbar, x[i,], i-1) pca <- sgapca(pca\$values, pca\$vectors, x[i,], 2/i, q, xbar) } pca ```

### Example output

```Loading required package: RSpectra
Warning message:
In fun(A, k, nu, nv, opts, mattype = "matrix") :
all singular values are requested, svd() is used instead
\$values
 9.860851 8.913251 8.052058 7.021341 5.930567 4.945176 4.012094 2.970270
 2.016671 1.005965

\$vectors
[,1]         [,2]         [,3]        [,4]         [,5]
[1,] -0.007626752  0.013113615  0.004835017 -0.01161393  0.007276235
[2,] -0.027448722 -0.013814205  0.044760669  0.02727489  0.019141467
[3,]  0.001135454 -0.001968897 -0.029604318  0.03437294  0.022114553
[4,]  0.003506517  0.009522230 -0.053169585 -0.01161837  0.030606735
[5,]  0.038267367  0.025733931  0.008493241  0.04262844 -0.038180103
[6,] -0.002883987  0.015155339  0.010367141  0.02743960  0.997840305
[7,] -0.033943422  0.046844421  0.053525755 -0.99458303  0.025301191
[8,] -0.055879892  0.076177267  0.990813795  0.05745584 -0.011524585
[9,]  0.090341424 -0.990790190  0.082981252 -0.04443703  0.015012774
[10,]  0.992605569  0.094805886  0.051256219 -0.02762907  0.003672981
[,6]          [,7]          [,8]          [,9]        [,10]
[1,] -0.009585772  0.0075302832  0.0104379632 -0.0082421165  0.999616519
[2,] -0.009235326  0.0121497952  0.0008180214  0.9978200136  0.007971530
[3,] -0.016488486  0.0556835981 -0.9969805906 -0.0001269832  0.010247872
[4,] -0.021589777  0.9960045049  0.0577414170 -0.0099623544 -0.008593948
[5,] -0.996843042 -0.0209289696  0.0155897543 -0.0083175980 -0.008946403
[6,] -0.035427467 -0.0322034850  0.0214492702 -0.0201183042 -0.007702457
[7,] -0.043595716 -0.0086170397 -0.0353430578  0.0238462900 -0.012659500
[8,]  0.010067653  0.0545389175 -0.0250851270 -0.0467815471 -0.005904793
[9,] -0.024126239  0.0120652918 -0.0004031680 -0.0144949764  0.012222615
[10,]  0.039365684 -0.0007975389 -0.0020835053  0.0273282225  0.006364463
```

onlinePCA documentation built on May 2, 2019, 3:28 a.m.