Predicting Mortality of ICU Patients: The PhysioNet/Computing in Cardiology Challenge 2012 1.0.0

File: <base>/sources/reko.kemppainen_at_gmail.com/entry7/physionet2012.m (7,917 bytes)
function [risk,prediction]=physionet2012(time,param,value)

% PCA based


% [risk,prediction]=physionet2012(time,param,value)
%
% Sample Submission for the PhysioNet 2012 Challenge. Variables are:
%
% time       - (Nx1 Cell Array) Cell array containing time of measurement
% param      - (Nx1 Cell Array) Cell array containing type (param) of
%               measurement
% value      - (Nx1 Double Array) Double array containing value of measurement
%
%
% risk       - (Scalar) estimate of the risk of the patient dying in hospital
% prediction - (Logical)Binary classification if the patient is going to die
%               in the hospital (1 - Died, 0 - Survived)
%
% Example:
% [risk,prediction]=physionet2012(time,param,value)
%
% Written by Ikaro Silva, 2012
%
% Version 1.1
%
%The sample submission calculates the in hospital death
%probability based on Bayesian Rule conditioned on SAPS_SCORE
%and PDF fitting based on training Set A.
%
%    P(dying | SAPS_SCORE) = P(dying)*P(SAPS_SCORE|Dying)/
%    (P(dying)*P(SAPS_SCORE|dying) + P(living)*P(SAPS_SCORE|living))
%
%Calculate likelihood, P(SAPS_SCORE|Dying), based polynomial fitting of the CDF
%of the training data A conditioned on those that died (values of the fit
%are hardcoded below)
%
% saps_died=SAPS_SCORE(ihd==1); %ihd is logical vector where 1 = in hospital death
% MX_SAPS=4*14;
% [Ndied,xx]=hist(saps_died,[0:MX_SAPS]);
% md=nanmean(saps_died);
% st_d=nanstd(saps_died)
% pdf_died=normpdf(xx,md,st_d);
% plot(xx,Ndied./sum(Ndied));hold on;grid on;plot(xx,pdf_died,'r') %Check fit
%
% Repeat to get conditional probability on those that lived using Extreme
% Value Distribution
% saps_alive=SAPS_SCORE(ihd==0); %ihd is logical vector where 1 = in hospital death
% [Nalive,xx]=hist(saps_alive,[0:MX_SAPS]);
% parmhat = evfit(saps_alive(~isnan(saps_alive)));
% pdf_alive=evpdf(xx,parmhat(1),parmhat(2));
% figure
% %This fitting is not very good, but for the purpose of an example will do
% plot(xx,Nalive./sum(Nalive));hold on;grid on;plot(xx,pdf_alive,'r')
% 
% TH=0.2700; %Threshold for classifying the patient as non-survivor
% %            %Determined through maximization of the min(PPV,Sensitivity) on
% %            %training Set A
% %
% TH_SAPS=.3300;
%
%
%
%
% B=[ 1.4037238316e+00;
%     -8.1833389095e-02;
%     -2.4939330155e-02;
%     -1.5119424507e-02;
%     2.7833454606e-02;
%     -1.4908319274e-02;
%     1.2317990562e-02;
%     1.0654354558e-02;
%     5.6335820205e-02;
%     2.0570191510e-01;
%     -2.7707893188e-01;
%     4.0909316076e-04;
%     -1.7088353456e-04;
%     -5.4632240876e-03;
%      5.3569006748e-03;
%     1.2035782695e-02;
%     -4.1221677189e-02];
%
%
% B_saps=[
%     4.8341800475e+00;
%     2.1514309096e-01;
%    -2.3846361213e-01;
% ];

I=1;
i=1;

[ALL_CATEGORIES,time_series_names,descriptors]=get_param_names();
num_params=length(ALL_CATEGORIES);
num_ts_params=length(time_series_names);
num_descriptors=length(descriptors);

MEAN_DATA_24=zeros(I,num_ts_params) + NaN;
MEAN_DATA_48=zeros(I,num_ts_params) + NaN;

DESCRIPTORS=zeros(I,num_descriptors) + NaN;




[times,values,names]=extract_param_series(time,param,value);

[ts_times,ts_values,ts_names]=get_param_subset(time_series_names,times,values,names);

[des_times,des_values,des_names]=get_param_subset(descriptors,times,values,names);

DESCRIPTORS(i,:)=cell2mat(des_values(:))';

means24=calculate_mean(ts_times,ts_values,ts_names,time_series_names,[0 24*60]);
means48=calculate_mean(ts_times,ts_values,ts_names,time_series_names,[24*60 48*60]);
MEAN_DATA_24(i,:)=means24;
MEAN_DATA_48(i,:)=means48;

%SAPS_SCORES(i)=saps_score(time,param,value,1,[0 24]);
%SAPS_SCORES_48(i)=saps_score(time,param,value,1,[24 48]);


[PHAT,TH]=PCA_classify(MEAN_DATA_24,MEAN_DATA_48,DESCRIPTORS,time_series_names,des_names);

if isnan(PHAT(:,2))
    X_trunc_saps=[SAPS_SCORES-SAPS_SCORES_48 SAPS_SCORES ];
    PHAT_saps = mnrval(B_saps,X_trunc_saps);
end

if isnan(PHAT(2))
    % [risk,prediction]=physionet2012_SAPS(time,param,value);
         risk=0.5;
         prediction=risk>TH;
elseif PHAT(2)<0.01
    risk=0.01;
    prediction=risk>TH;
elseif PHAT(2)>0.99
    risk=0.99;
    prediction=risk>TH;
else
    risk=PHAT(2);
    prediction=risk>TH;
end


    function [PHAT,best_th]=PCA_classify(X1,X2,DESCRIPTORS,names1,names2)
        % X_orig=X;
        % x1=X(~isnan(X(:,1)) & ~isnan(X(:,2)),1);
        % x2=X(~isnan(X(:,1)) & ~isnan(X(:,2)),2);
        % X=[x1 x2];
        
%         %n2=length(x1);
%         DIF_A=X1-X2;
%         DIF_A(:,strcmp(names1,'PH'))=abs(DIF_A(:,strcmp(names1,'PH')));

        
        DIF_A=X2;
        
        X=[X1 DIF_A ];
        X=[DESCRIPTORS(:,strcmp(names2,'Age')) X];
        ICUTYPE_a=[DESCRIPTORS(:,strcmp(names2,'ICUType'))];
        
        
        
        
        if ICUTYPE_a==1      
            
            %savefile='icu1.mat';
             savefile='icu_1.mat'; % for all
                  %        savefile='icu_1_pca.mat';

             
        %     savefile='icu_all.mat';
            load(savefile,'N2','PHAT_all','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
            S_new=calc_S_new_data(X',A,V,Mu,N,CV);
            X_new_rec =( repmat(Mu,1,1) + A*S_new)';
            sigma0 = sigma;
            sigma0(sigma0==0) = 1;
            z = bsxfun(@minus,X_new_rec', mu');
            z = bsxfun(@rdivide, z, sigma0');
            x_in=pinv(pc1)*z;
               x_in=x_in(1:N2);
            PHAT = mnrval(B,x_in');
            
        elseif ICUTYPE_a==2
            
        %    savefile='icu2.mat';
                        savefile='icu_2.mat';
            % savefile='icu_2_pca.mat';

                      %   savefile='icu_all.mat';

            load(savefile,'N2','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
            S_new=calc_S_new_data(X',A,V,Mu,N,CV);
            X_new_rec =( repmat(Mu,1,1) + A*S_new)';
            sigma0 = sigma;
            sigma0(sigma0==0) = 1;
            z = bsxfun(@minus,X_new_rec', mu');
            z = bsxfun(@rdivide, z, sigma0');
            x_in=pinv(pc1)*z;
               x_in=x_in(1:N2);
            PHAT = mnrval(B,x_in');            
            
        elseif ICUTYPE_a==3
            
         %   savefile='icu3.mat';
                        savefile='icu_3.mat';
          %   savefile='icu_3_pca.mat';

                       %  savefile='icu_all.mat';

            load(savefile,'N2','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
            S_new=calc_S_new_data(X',A,V,Mu,N,CV);
            X_new_rec =( repmat(Mu,1,1) + A*S_new)';
            sigma0 = sigma;
            sigma0(sigma0==0) = 1;
            z = bsxfun(@minus,X_new_rec', mu');
            z = bsxfun(@rdivide, z, sigma0');
            x_in=pinv(pc1)*z;
               x_in=x_in(1:N2);
            PHAT = mnrval(B,x_in');            
            
        elseif ICUTYPE_a==4
            
          %  savefile='icu4.mat';
            savefile='icu_4.mat';
         %    savefile='icu_4_pca.mat';
                       %  savefile='icu_all.mat';

            load(savefile,'N2','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
            S_new=calc_S_new_data(X',A,V,Mu,N,CV);
            X_new_rec =( repmat(Mu,1,1) + A*S_new)';
            sigma0 = sigma;
            sigma0(sigma0==0) = 1;
            z = bsxfun(@minus,X_new_rec', mu');
            z = bsxfun(@rdivide, z, sigma0');
            x_in=pinv(pc1)*z;
            x_in=x_in(1:N2);
            PHAT = mnrval(B,x_in');
            
        else
            error('no icu type')
        end
        
        
        
        
    end

end