# pcares

Residuals from principal component analysis

## Syntax

```residuals = pcares(X,ndim) [residuals,reconstructed] = pcares(X,ndim) ```

## Description

`residuals = pcares(X,ndim)` returns the `residuals` obtained by retaining `ndim` principal components of the n-by-p matrix `X`. Rows of `X` correspond to observations, columns to variables. `ndim` is a scalar and must be less than or equal to p. `residuals` is a matrix of the same size as `X`. Use the data matrix, not the covariance matrix, with this function.

`pcares` does not normalize the columns of X. To perform the principal components analysis based on standardized variables, that is, based on correlations, use ```pcares(zscore(X), ndim)```. You can perform principal components analysis directly on a covariance or correlation matrix, but without constructing residuals, by using `pcacov`.

`[residuals,reconstructed] = pcares(X,ndim)` returns the reconstructed observations; that is, the approximation to `X` obtained by retaining its first `ndim` principal components.

## Examples

This example shows the drop in the residuals from the first row of the Hald data as the number of component dimensions increases from one to three.

```load hald r1 = pcares(ingredients,1); r2 = pcares(ingredients,2); r3 = pcares(ingredients,3); r11 = r1(1,:) r11 = 2.0350 2.8304 -6.8378 3.0879 r21 = r2(1,:) r21 = -2.4037 2.6930 -1.6482 2.3425 r31 = r3(1,:) r31 = 0.2008 0.1957 0.2045 0.1921```

## References

[1] Jackson, J. E., A User's Guide to Principal Components, John Wiley and Sons, 1991.

[2] Jolliffe, I. T., Principal Component Analysis, 2nd Edition, Springer, 2002.

[3] Krzanowski, W. J. Principles of Multivariate Analysis: A User's Perspective. New York: Oxford University Press, 1988.

[4] Seber, G. A. F. Multivariate Observations. Hoboken, NJ: John Wiley & Sons, Inc., 1984.