# Replacevars

## Contents

### Purpose

Replace variables based on principal component analysis (PCA) or partial least squares (PLS) regression models.

### Synopsis

- rm = replace(model,vars)
- [rm,repdata] = replace(model,vars,data)
- repdata = replace(model,data)

### Description

REPLACE replaces variables from data matrices with values most consistent with the given PCA or PLS model. Input model can be any of the following:

1) a standard model structure generated by the PCA or PLS functions or the Anlysis GUI

2) a set of loading column vectors (*e.g.*, loads returned by the pca routine, or model.loads{2} if the output is a model structure)

3) the PCA residual generating matrix (I-loads\*loads�), or

4) the PLS residuals generating matrix coeff returned by the plsrsgn routine.

Optional input vars is a row vector containing the indices of the variables (columns) to be replaced. If omitted, the input data is searched for non-finite values (NaN, Inf) and these values are replaced.

When vars in input, the outputs are the replacement matrix rm and the replaced data (if data was provided), repdata. Multiplication of a data matrix xnew by rm will replace variables with values most consistent with the given PCA or PLS model. If vars was not supplied, only repdata is output.

### Examples

A PCA model was created on a data matrix xold giving a model structure model. The loadings, a set of loadings column vectors, were extracted to a variable loads using loads = model.loads{2};. It was found that the sensor measuring variable 9 has gone "bad" and we would like to replace it in the new data matrix xnew. A replacement matrix rm is first created using replace.

rm = replace(loads,9);

The new data matrix with variable 9 replaced rxnew is then calculated by multiplying xnew by rm.

rxnew = xnew\*rm;