File size: 5,958 Bytes
d7989aa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
function Classify_Scalars_LASSO(InData,TITLE,iterations,vars,Xval)
% Nomenclature is that 'A' TARGETS are classified (1's) from 'B' STANDARDS (0's)
Big_A=InData(InData(:,1)==1,vars) ; % PD
Big_B=InData(InData(:,1)~=1,vars) ; % CTL
for rep = 1:iterations
if rem(rep,100)==0
['LASSO ' Xval ' ' TITLE ' ' num2str(rep)]
end
% X-Validation: Equate epochs
size_targs=size(Big_A,1); % -------- "TARGETS" are Patient
size_stds=size(Big_B,1); % -------- "STANDARDS" are CTL
if size_targs<size_stds
temp=shuffle(1:size_stds);
TARGETS=Big_A;
STANDARDS=Big_B(temp(1:size_targs),:); clear temp size_stds size_targs;
elseif size_stds<size_targs
temp=shuffle(1:size_targs);
TARGETS=Big_A(:,:,temp(1:size_stds)); clear temp size_stds size_targs;
STANDARDS=Big_B;
else
TARGETS=Big_A;
STANDARDS=Big_B;
end
AllData = cat(1,TARGETS,STANDARDS);
GROUPS = [ones(1,size(TARGETS,1)),zeros(1,size(STANDARDS,1))];
Size_Per_Set = size(AllData,1) .* .5;
% Cross Validate
if strmatch(Xval,'5X'); testsize = floor(.2*Size_Per_Set);
elseif strmatch(Xval,'10X'); testsize = floor(.1*Size_Per_Set);
elseif strmatch(Xval,'LOO'); testsize = 1;
end
trainsize = Size_Per_Set-2*testsize;
% For TARGETS
ForRand = shuffle(1:Size_Per_Set);
TrainBool_T = zeros(1,Size_Per_Set); TrainBool_T(ForRand(1:trainsize))=1;
Test1Bool_T = zeros(1,Size_Per_Set); Test1Bool_T(ForRand(trainsize+1:trainsize+testsize))=1;
Test2Bool_T = zeros(1,Size_Per_Set); Test2Bool_T(ForRand(trainsize+testsize+1:end))=1;
% For STANDARDS
ForRand = shuffle(1:Size_Per_Set);
TrainBool_S = zeros(1,Size_Per_Set); TrainBool_S(ForRand(1:trainsize))=1;
Test1Bool_S = zeros(1,Size_Per_Set); Test1Bool_S(ForRand(trainsize+1:trainsize+testsize))=1;
Test2Bool_S = zeros(1,Size_Per_Set); Test2Bool_S(ForRand(trainsize+testsize+1:end))=1;
% same for standards and targets
TrainBool = [TrainBool_T TrainBool_S];
Test1Bool = [Test1Bool_T Test1Bool_S];
Test2Bool = [Test2Bool_T Test2Bool_S];
% Classify ****** Targets ******
% training set
x_train = AllData(TrainBool==1,:);
% normalize!
x_mean = mean(x_train); x_std = std(x_train);
x_train_norm = (x_train - repmat(x_mean,size(x_train,1),1)); % mean -normalize
x_train_norm = x_train_norm./repmat(x_std,size(x_train,1),1);
y_train = GROUPS(TrainBool==1);
% validate set
xtstset1 = AllData(Test1Bool == 1,:);
% normalize to train set
xtstset1_norm = (xtstset1-repmat(x_mean,size(xtstset1,1),1));
xtstset1_norm = xtstset1_norm./ repmat(x_std,size(xtstset1,1),1); % normalize to trn data
y_tstset1 = GROUPS(Test1Bool==1);
% test set
xtstset2 = AllData(Test2Bool == 1,:);
% normalize to train set
xtstset2_norm = (xtstset2-repmat(x_mean,size(xtstset2,1),1));
xtstset2_norm = xtstset2_norm./ repmat(x_std,size(xtstset2,1),1); % normalize to trn data
y_tstset2 = GROUPS(Test2Bool==1);
% store normalizing constants. Important to normalize to that when applying
% weights to a different data set later.
norm{1}(rep,:) = x_mean;
norm{2}(rep,:) = x_std;
x_train = x_train_norm;
x_test1 = xtstset1_norm;
x_test2 = xtstset2_norm;
% ############# LASSO #############
[B,stats] = lasso(x_train, y_train');
% predict train
predictor_train = logical((x_train * B >0) );
IsTarget_train = repmat(y_train',1,size(B,2));
% predict val
predictor_tstset1 = logical((x_test1 * B >0) );
IsTarget_tstset1 = repmat(y_tstset1',1,size(B,2));
% predict test
predictor_tstset2 = logical((x_test2 * B >0) );
IsTarget_tstset2 = repmat(y_tstset2',1,size(B,2));
% select regularization param that gets best generalization on
% validation set. If many, go for sparsity.
generalize = mean(predictor_tstset1==IsTarget_tstset1);
[Test1Acc,tochoose] = max(generalize);
tochoose = tochoose(1);
% Get Training accuracy for that param
TrainAcc = mean(IsTarget_train(:,tochoose)==predictor_train(:,tochoose));
% Get test set accuracy for that param, unbiased
Test2Acc = mean(IsTarget_tstset2(:,tochoose)==predictor_tstset2(:,tochoose));
% Get separately for the two conditions
targets = find(y_train == 1);
TrainAcc1 = mean(IsTarget_train(targets,tochoose)==predictor_train(targets,tochoose));
targets = find(y_tstset1 == 1);
Test1Acc1 = mean(IsTarget_tstset1(targets,tochoose)==predictor_tstset1(targets,tochoose));
targets = find(y_tstset2 == 1);
Test2Acc1 = mean(IsTarget_tstset2(targets,tochoose)==predictor_tstset2(targets,tochoose));
targets = find(y_train == 0);
TrainAcc0 = mean(IsTarget_train(targets,tochoose)==predictor_train(targets,tochoose));
targets = find(y_tstset1 == 0);
Test1Acc0 = mean(IsTarget_tstset1(targets,tochoose)==predictor_tstset1(targets,tochoose));
targets = find(y_tstset2 == 0);
Test2Acc0 = mean(IsTarget_tstset2(targets,tochoose)==predictor_tstset2(targets,tochoose));
% store
LASSO_Probability_Train(rep,:) = [TrainAcc TrainAcc1 TrainAcc0];
LASSO_Probability_Tst1(rep,:) = [Test1Acc Test1Acc1 Test1Acc0];
LASSO_Probability_Tst2(rep,:) = [Test2Acc Test2Acc1 Test2Acc0];
LASSO_Betas(rep,:)=B(:,tochoose)';
% ############# SVM #############
clearvars -except norm LASSO_Probability_Train LASSO_Probability_Tst1 LASSO_Probability_Tst2 LASSO_Betas rep Big_A Big_B InData Xval TITLE iterations
end
% Save
save(['LASSO_',TITLE,'_',Xval,'_iter',num2str(iterations),'.mat'],'LASSO_Probability_Train','LASSO_Probability_Tst1','LASSO_Probability_Tst2','LASSO_Betas');
|