Home > wafo > wstats > fitmodel.m

# fitmodel

## PURPOSE

Fits response by polynomial

## SYNOPSIS

[fit,res,sd,dof] = fitmodel(y,model)

## DESCRIPTION

FITMODEL  Fits response by polynomial

CALL:  [fit,res,sd,dof ] = fitmodel(y,model)

fit   = fitted response
res   = residual, i.e., y-fit
sd    = standard deviation of residual
dof   = degrees of freedom
y     = Response in standard order
model = character array of model parameters

Example
y = [60 72 54 68 52 83 45 80]; % Responses to design D.
[ef, id] = yates(y);           % Calculate effects
nplot(ef,id)                   % Identify model
model = strvcat('A','B','AC'); % model parameters
[fit,res,sd,dof] = fitmodel(y,model);
wnormplot(res)                 % Diagnostic check on fitted model.

## CROSS-REFERENCE INFORMATION

This function calls:
 cnr2cl Column Number to Column Label. ryates Reverse Yates' algorithm to give estimated responses yates Calculates main and interaction effects using Yates' algorithm. error Display message and abort function. isnumeric True for numeric arrays. std Standard deviation. strmatch Find possible matches for string. warning Display warning message; disable or enable warning messages.
This function is called by:

## SOURCE CODE

001 function [fit,res,sd,dof] = fitmodel(y,model)
002 %FITMODEL  Fits response by polynomial
003 %
004 % CALL:  [fit,res,sd,dof ] = fitmodel(y,model)
005 %
006 %  fit   = fitted response
007 %  res   = residual, i.e., y-fit
008 %  sd    = standard deviation of residual
009 %  dof   = degrees of freedom
010 %  y     = Response in standard order
011 %  model = character array of model parameters
012 %
013 % Example
014 %   D = ffd(3);                    % complete 2^3 design in standard order.
015 %   y = [60 72 54 68 52 83 45 80]; % Responses to design D.
016 %   [ef, id] = yates(y);           % Calculate effects
017 %   nplot(ef,id)                   % Identify model
018 %   model = strvcat('A','B','AC'); % model parameters
019 %   [fit,res,sd,dof] = fitmodel(y,model);
020 %   wnormplot(res)                 % Diagnostic check on fitted model.
021 %
023
024 error(nargchk(2,2,nargin))
025 sz = size(y);
026 n  = length(y);
027 if prod(sz) == n,
028   y = y(:);       % Make sure it is a column vector
029 else
030   n = sz(1);      % Number of runs
031 end
032 if isnumeric(model)
033   model = cnr2cl(model); % Transform into columnlabels
034 end
035 model = fliplr(sort(model,2));
036 p = size(model,1);
037
038 [ef,id] = yates(y); % Calculate the effects
039 id  = fliplr(sort(id,2));
040 ind = ones(n,1);
041 for ix=1:p
042   k = strmatch(model(ix,:),id,'exact');
043   if any(k),
044     ind(k+1)=0;
045   else
046     warning('Something wrong!')
047   end
048 end
049
050 ef2      = ef;
051 ind(1)   = 0;
052 ind      = find(ind);
053 ef2(ind,:) = 0;       % Neglect effects from variables not in the model
054
055 fit = ryates(ef2);  % Calculate the fit
056 res = y-fit;        % Residual
057
058 sd  = std(res);
059 dof = n-p-1;
060
061
062
063
064

Mathematical Statistics
Centre for Mathematical Sciences
Lund University with Lund Institute of Technology

Comments or corrections to the WAFO group

Generated on Thu 06-Oct-2005 02:21:16 for WAFO by m2html © 2003