function retRes = Ex4CVGPOCRE(inPType)
%function retRes = Ex4CVGPOCRE(inPType)
%
% An example to show how to use gPOCRE to analyze simglmdata.txt with the
% tuning parameter chosen by 10-fold cross-validation.
%
% Input:
%   inPType -- type of penalization:
%            'ebtz' ~ EBT w/ z-transformation (by default),
%            'ebt' ~ EBT w/o z-transformation,
%            'l1' ~ L1,
%            'scad' ~ SCAD (with gamma=3.7),
%            'mcp' ~ MCP (with gamma=3.7).
%
% Dabao Zhang     August 1, 2014
%
% Revised by:
%       Dabao Zhang, August 8, 2014 -- Need to standardize X & optimize W.
%
% Copyright (c) 2013-2014 by Dabao Zhang.

if( nargin<1 ) inPType='ebtz'; end

YX = load('simglmdata.txt');

% Standardization of X is always recommended.
% Output X from Standardize(...) exclude constant covariates.
[X,idxX] = Standardize(YX(:,2:end));    % Standardizing all covariates

% Settings:
option.delta = 0.05;        % 0.1,0.05,0.02, or 0.01
option.covidx = [15 16];    % covariates of no interest
option.ptype = inPType;
nCVFolds = 10;

%-------------------------------------------------------------
% Use 10-Fold Cross-Validation to Choose Tuning Parameter
%-------------------------------------------------------------
tp = CVGPOCRE(YX(:,1),X,'binomial','logit',nCVFolds,option);
disp(['  gPOCRE: Optimal Tuning Parameter = ' num2str(tp)]);
tmpRes = gPOCRE(YX(:,1),X,'binomial','logit',tp,option);
option.W = CalcOptW(tmpRes.mu+X*tmpRes.beta,'binomial','logit');
[retRes,tmpStats] = gPOCRE(YX(:,1),X,'binomial','logit',tp,option);

retRes.pvalue = tmpStats.pvalue;

% Plot beta(j) vs. j:
figure;
plot(1:1000,retRes.beta,'.');
ylabel('beta'); xlabel('j');

% Plot all components:
plotcomponents(retRes);
