Predicting Mortality of ICU Patients: The PhysioNet/Computing in Cardiology Challenge 2012 1.0.0
(10,402 bytes)
function [risk,prediction]=physionet2012_4(time,param,value,N2,best_th,A,Mu,V, CV,N,mu,sigma)
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;
[cont_times,cont_values,cont_names,d,dd,md_max,cont_values2,vari]=resample_params(time_series_names,ts_times,ts_values,ts_names,des_values,des_names);
MD_DATA_D(i,1:length(d))=d';
MD_DATA_DD(i,1:length(d))=dd';
MD_VARI(i,:)=vari';
MD_DATA(i)=md_max;
DYN_DATA(:,:,i)=cont_values;
R=~isnan(MEAN_DATA_24);
%SAPS_SCORES(i)=saps_score(time,param,value,1,[0 24]);
%SAPS_SCORES_48(i)=saps_score(time,param,value,1,[24 48]);
[risk,prediction]=lin_reg_classify(time_series_names,MD_DATA_D,MD_DATA_DD,MD_DATA,DESCRIPTORS,des_names,R,MD_VARI,MEAN_DATA_24,MEAN_DATA_48,N2,best_th,A,Mu,V, CV,N,mu,sigma);
% [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 [risk,prediction]=lin_reg_classify(time_series_names,MD_DATA_D,MD_DATA_DD,MD_DATA,DESCRIPTORS,des_names,R,MD_VARI,MEAN_DATA_24,MEAN_DATA_48,N2,best_th,A,Mu,V, CV,N,mu,sigma)
X=[MEAN_DATA_24 MEAN_DATA_48 ];
%X=[X1];
%X=[X];
AGE_a=DESCRIPTORS(:,strcmp(des_names,'Age'));
Gender_a=DESCRIPTORS(:,strcmp(des_names,'Gender'));
ICUTYPE_a=[DESCRIPTORS(:,strcmp(des_names,'ICUType'))];
ICUTYPE_t=ICUTYPE_a;
ICUTYPE_a(ICUTYPE_t==1)=2;
ICUTYPE_a(ICUTYPE_t==2)=4;
ICUTYPE_a(ICUTYPE_t==3)=1;
ICUTYPE_a(ICUTYPE_t==4)=3;
X=[AGE_a ICUTYPE_a X];
if Gender_a==0
%if des{:,strcmp(des_names,'Gender')}==0
%X_new_rec=zeros(size(X));
% savefile='gender0.mat';
savefile='gender0_60.mat';
% savefile='gender0_60_ppca';
% load(savefile,'N2','PHAT_all','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
% load(savefile,'N2','best_th','A','Mu','V', 'CV','N','mu','sigma');
%save('gender0_60_karsittu.mat','N2','best_th','A','Mu','V', 'CV','N','mu','sigma')
% S_new=calc_S_new_data(X',A,V,Mu,N,CV);
% X_new_rec =( repmat(Mu,1,size(S,2)) + (A*S))';
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');
z(isnan(z))=0;
% x_in=(pinv(pc1)*z)';
% X(Gender_a==0,:)=x_in(Gender_a==0,:);
z=z';
X(Gender_a==0,:)=z(Gender_a==0,:);
elseif Gender_a==1 || Gender_a==-1
% savefile='gender1.mat';
savefile='gender1_60.mat';
% savefile='gender1_60_ppca';
%load(savefile,'N2','PHAT_all','best_th','A','S','Mu','V', 'CV', 'HP', 'LC','N','pc1','mu','sigma','B');
% load(savefile,'N2','best_th','A','Mu','V', 'CV','N','mu','sigma');
%save('gender1_60_karsittu.mat','N2','best_th','A','Mu','V', 'CV','N','mu','sigma')
% S_new=calc_S_new_data(X(Gender_a==1,:),A,V,Mu,N,CV);
% X_new_rec =( repmat(Mu,1,size(S,2)) + A*S)';
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(Gender_a==1,:)=x_in(Gender_a==1,:);
z=z';
z(isnan(z))=0;
X(Gender_a==1,:)=z(Gender_a==1,:);
X(Gender_a==-1,:)=z(Gender_a==-1,:);
end
%X=[ MD_DATA_D MD_DATA_DD MD_DATA double(R) zscore(MD_VARI) X];
X=[Gender_a double(R) MD_VARI X];
load('w1.mat','w1')
load('w2.mat','w2')
[ResTrain,labelsEstimTrain,probsTrain,P] = predict_LRoma(w1,X,1);
[ResTrain,labelsEstimTrain,probsTrain,P2] = predict_LRoma(w1,X,1);
PHAT = P';
PHAT2 = P2';
class_th=0.32;
% IHD_=IHD;
% max_score1=0;
% max_score2=100000;
% best_th=0;
% best_th2=0;
% for class_th=.1:.01:0.5
DATA(:,1)=str2double('0000');
DATA(:,2)=PHAT2(:,1);
DATA(:,3)=PHAT(:,1)> class_th;
DATA(DATA(:,2)<0.01,2)=0.01;
DATA(DATA(:,2)>0.99,2)=0.99;
risk=DATA(:,2);
prediction=DATA(:,3);
% if(~isempty(results))
% % Calculate sensitivity (Se) and positive predictivity (PPV)
% TP=sum(DATA(IHD_==1,3));
% FN=sum(~DATA(IHD_==1,3));
% FP=sum(DATA(IHD_==0,3));
% Se=TP/(TP+FN);
% PPV=TP/(TP+FP);
%
% show=0; % if show is 1, the decile graph will be displayed by lemeshow()
% H=lemeshow([IHD_ DATA(:,2)],show);
%
% Use the title of figure to display the results
% title(['H= ' num2str(H) ' Se= ' num2str(Se) ' PPV= ' num2str(PPV) '. ' num2str(class_th) ])
% The event 1 score is the smaller of Se and PPV.
% score1 = min(Se, PPV);
% if score1>max_score1
% max_score1=score1;
% best_th=class_th;
% % max_score2=H;
% BEST_DATA=DATA;
% % display(['Unofficial Event 1 score: ' num2str(score1)]);
% end
%
% if H<max_score2
%
% max_score2=H;
% best_th2=class_th;
% % BEST_DATA=DATA;
% % display(['Unofficial Event 1 score: ' num2str(score1)]);
% end
% end
% end
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