function [] = plotcomponents(inRes,inLambda)
%function [] = plotcomponents(inRes,inLambda)
%
% Plot all components in the same figure for a specific value of lambda.
%
% Input:
%   inRes: output from POCREPath();
%   inLambda: prespecifed lambda.
%
% Dabao Zhang    June 18, 2014
%
% Revised by:
%   Dabao Zhang, 12/15/2017 -- Indices of screened variables from
%                              POCREScreen();
%
% Copyright (c) 2014-2017 by Dabao Zhang.

error(nargchk(1, 2, nargin));
if( nargin<2 ) 
    idxC= 1;
else
    lambdaList = [inRes(1:end).lambda];
    [minDiff,idxC] = min(abs(lambdaList-inLambda));
end

[p,ny] = size(inRes(idxC).beta);
if( ~isfield(inRes,'XIdx') )
    inRes.XIdx = 1:p;
else
    p = max(inRes.XIdx);
end

tmpstr = '';
for k=1:ny
    if( ny>1 )
        tmpstr = ['Y_' num2str(k) ': '];
    end
    
    figure;
    for j=1:inRes(idxC).nCmp
        subplot(inRes(idxC).nCmp,1,j);
        %h = plot(1:p,inRes(idxC).varpi(:,j),'x');
        varpi = zeros(p,1); varpi(inRes.XIdx) = inRes(idxC).varpi(:,j);
        h = plot(1:p,varpi,'x');
        ylabel(['\varpi_' num2str(j)]);
        xlabel('index of predictor');
        if( isfield(inRes(idxC),'pvalue') )
            title([tmpstr 'p-value of X\varpi_' num2str(j) '=' ...
                   num2str(inRes(idxC).pvalue(j))]);
        else
            title(tmpstr);
        end
    end
end
