function [] = plotrsqnzbeta(inRes)
%function [] = plotrsqnzbeta(inRes)
%
% Provide the plot of lambda vs. (R^2, #[beta~=0]) for the results from
% POCREPath(...).
%
% Input:
%   inRes: output from POCREPath();
%
% Dabao Zhang    June 18, 2014
%
% Revised by:
%
% Copyright (c) 2014 by Dabao Zhang.

error(nargchk(1, 1, nargin));

nlambda = length(inRes);
lambda = [inRes(1:end).lambda];
if( ~issorted(lambda) )
    [lambda, idxL] = sort(lambda);
    inRes = inRes(idxL);
end

[p,ny] = size(inRes(1).beta);
rsq = reshape([inRes(1:end).rsq],ny,nlambda)';

nzbeta = zeros(ny,1);
for k=1:ny
    beta = zeros(p,nlambda);
    for j=1:nlambda
        beta(:,j) = inRes(j).beta(:,k);
        nzbeta(j) = sum(abs(inRes(j).beta(:,k))>eps);
    end
    
    [ha,hl1,hl2] = plotyy(lambda,rsq(:,k),lambda,nzbeta);
    set(hl1,'Marker','o');
    set(hl1,'LineStyle','-');
    set(hl2,'Marker','*');
    set(hl2,'LineStyle',':');
    set(get(ha(1),'Ylabel'),'String','R^2');
    set(get(ha(2),'Ylabel'),'String','number of non-zero  \beta');
    xlabel('\lambda');
    title(['Component of Y: ' num2str(k)]);
end
