teg_scree

The function returns the element in a vector of (low-to-high sorted) eigenvalues where the derivative of the normalized values passes through 1 (i.e. the scree criterion or knik criterium), using a polynomial estimation of the curve.

The code:

function knik = teg_scree(vec)

% function knik = teg_scree(EV)
%
% EV is a vector of eigenvalues, e.g. diag(EV) where EV is from eig.

plotit = 0;

x = ((1:length(vec)) - 1) / (length(vec) - 1);
x = x(:);
vec = vec ./ max(vec);

if plotit,
    figure;
    plot(x, vec, 'k-');
    hold on;
end;

err0 = var(vec);
Pred = ones(size(x));
p = 0;
while 1,
    b = inv(Pred'*Pred)*Pred'*vec;
    mod0 = Pred*b;
    if plotit,
        plot(x, mod0);
    end;
    err1 = var(mod0 - vec);
    disp(err1 / err0);
    if err1 / err0 < 0.05,
        break;
    end;
    p = p + 1;
    Pred0 = x .^ p;
    Pred = [Pred Pred0];
end;

deriv = zeros(size(x));
for m = 2:size(Pred, 2),
    n = m - 1;
    deriv = deriv + b(m) * n * x .^ (n - 1);
end;
kniks = find(deriv >= 1);
knik = kniks(1);