function [] = plotbetarsq(inRes)
%function [] = plotbetarsq(inRes)
%
% Provide the plot of lambda vs. (beta, R^2) 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)';

for k=1:ny
    beta = zeros(p,nlambda);
    for j=1:nlambda
        beta(:,j) = inRes(j).beta(:,k);
    end
    beta = beta(sum(abs(beta),2)>eps,:);

    figure; hold on;
    for j=1:(size(beta,1)-1)
        plot(lambda,beta(j,:),':x');
    end
    [ha,hl1,hl2] = plotyy(lambda,beta(end,:),lambda,rsq(:,k));
    set(hl1,'Marker','x');
    set(hl1,'LineStyle',':');
    set(hl2,'Marker','*');
    set(hl2,'LineStyle','-');
    set(get(ha(1),'Ylabel'),'String','\beta');
    set(get(ha(2),'Ylabel'),'String','R^2');
    xlabel('\lambda');
    title(['Component of Y: ' num2str(k)]);
    hold off;
end
