FitModel
Location: ..\cubatch\Common\FitModel.m

function Model = FitModel(FitFun,PredFun,X,Y,Factors,PreProcess,varargin);
Description:
Fits a model (either in regression or simply to decompose an array in factors) and saves the results in ModelOut.model.
Inputs:
FitFun and PredFun have specific requirements with respect to input and output.

- FitFun(X,[Y,]F,varargin)
  It is a function fitting the desired model to the data. It can be a string (for MatLab© 5.3 or superior) or a function handle (for MatLab© 6.x or superior)
  The first input parameter must be X, followed (only in regression) by Y and then by the dimensionality of the model F.
  The additional inputs come next. The output of this function should be only one: either a cell vector with the loading matrices in each mode as elements or a structure. In the second case the fields that the returned structure has in common with ModelOut.model are filled with the results.

> > E.g. function Model = Tucker3Fit(X,F,varargin)
> > [Model.xfactors,Model.core] = tucker(X,F,varargin{:});

> > > can be used to compute a simple Tucker3 model on a generic n-way array and the resulting loading matrices and scores will be stored in ModelOut.model.xfactors and ModelOut.model.core.
> > > varargin{1} can be the Options, varargin{2} the constraints on the Factors and so forth

- PredFun(X,Model,varargin)
  Function computing the predictions for the desired model. It can be a string (for MatLab© 5.3 or superior) or a function handle (for MatLab© 6.x or superior).
  The first input parameter must be X, followed by the model parameters (i.e. by ModelOut.model or a reduced version, so long as the function is capable of fitting the model on the data).
  If a Y is necessary it shall be passed within the varargin parameter.

> > E.g. function Model = Tucker3Pred(X,Model,varargin)

> > > W = Model.xfactors{ndims(X)};
> > > for m = ndims(X)-1:-1:2
> > > W = kr(W,Model.xfactors{m});
> > > end
> > > Z = nshape(Model.core,1)*W';
> > > Model.xfactors{1} = nshape(X,1)*pinv(Z);
> > > Model.xpred = reshape(Model.xfactors{1}*Z,size(X));

X: double array relative to the calibration set
Y: double array relative to the calibration set (it must be empty if it is a decomposition method)
Factors: number of factors to extract (it can be a single number or a vector depending on the desired model)
PreProcess: structure with two fields: 'modx' and 'mody'; each of them is also a structure with two fields: 'cen' and 'scal'.
'modx' defines the preprocessing relative to X, while 'mody' refers to Y.
The two fields 'cen' and 'scal' are vectors with the same number of elements as the modes of the relative array (e.g. if X is 3-way PreProcess.modx.cen and PreProcess.modx.scal must be 3 x 1 (or 1 x 3) vectors. For more information on the values that these two vectors can be given check the help of nprocess (n-way toolbox).
varargin: additional inputs (e.g. Options, Constraints, etc) handled directly by FitFun and PredFun

Outputs:
The results of the computations as well as the new model's parameters are stored in ModelOut.model

Called by:
Model_nPLS1\Calculate, Model_PARAFAC\Calculate
Subroutines:
Internal: Fit_Decomposition, Fit_Regression, CleanX
External: definemodelout, nprocess (N-Way toolbox), nshape (N-Way toolbox),

Author:
Giorgio Tomasi
Royal Agricultural and Veterinary University
MLI, LMT, Chemometrics group
Rolighedsvej 30
DK-1958 Frederiksberg C
Danmark
Last modified: 21-Oct-2002 10:32:04
Contact: Giorgio Tomasi, gt@kvl.dk
References

Fit_Decomposition
function Model = Fit_Decomposition(FitFun,PredFun,X,Factors,PreProcess,varargin)
Description:
Fits a model which decomposes the X in absence of a Y of predicted variables.
Inputs:
The same as FitModel
Outputs:
The same as FitModel. Only the fields referring to the X array are filled in.
Subroutines:
Internal: CleanX
External: DefineModelOut, NProcess (n-way toolbox)
Author:
Giorgio Tomasi
Royal Agricultural and Veterinary University
MLI, LMT, Chemometrics group
Rolighedsvej 30
DK-1958 Frederiksberg C
Danmark
Last modified: 05-gen-03
Contact: Giorgio Tomasi, gt@kvl.dk
References

Fit_Regression
function Model = Fit_Regression(FitFun,PredFun,X,Y,Factors,PreProcess,varargin)
Description:
Fits a regression model having X as the predictor matrix and Y as predicted variables.
Inputs:
The same as FitModel
Outputs:
The same as FitModel.
Subroutines:
Internal: CleanX
External: DefineModelOut, NProcess (n-way toolbox)
Author:
Giorgio Tomasi
Royal Agricultural and Veterinary University
MLI, LMT, Chemometrics group
Rolighedsvej 30
DK-1958 Frederiksberg C
Danmark
Last modified: 05-gen-03
Contact: Giorgio Tomasi, gt@kvl.dk
References

CleanX

function [Flag,IndX1,IndX2,...IndXn,...,IndY1,IndY2,...IndYm] = CleanX(X,Y);
Description:
Extracts the indexes on X corresponding to slabs that are not constituted only by missing values.
In case a Y is provided the horizontal slabs (i.e. the samples/batches) that have only missing values either in X or in Y are absent from these indexes. Flag is 1 in case any of the slabs had to be removed. Otherwise it is 0.
Inputs:
X: n-way array (where n >= 2)
Y: m-way array (where m >= 2)
The first mode of X and Y must have the same dimensions in the first mode.
Outputs:
Flag: is 1 in presence of at least one slab in either X or Y containing only missing values
IndX1,IndX2,...IndXn: indexes in the n-dimensions of X.
IndY1,IndY2,...IndYm: indexes in the m-dimensions of Y.
Subroutines:
Internal: none
External: nshape (n-way toolbox)
Author:
Giorgio Tomasi
Royal Agricultural and Veterinary University
MLI, LMT, Chemometrics group
Rolighedsvej 30
DK-1958 Frederiksberg C
Danmark
Last modified: 05-gen-03
Contact: Giorgio Tomasi, gt@kvl.dk
References