File size: 5,060 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
function Classify_Scalars_LASSO_MatchSubjs(InData,TITLE,MATCHTOEEG,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
% Align them so each CTL matches each PD
Big_B=Big_B(MATCHTOEEG',:);



for rep = 1:28



    AllData = cat(1,Big_A,Big_B);

    GROUPS = [ones(1,size(Big_A,1)),zeros(1,size(Big_B,1))];

    Size_Per_Set = size(AllData,1) .* .5;

    

    % Cross Validate

    testsize = 1;

    trainsize = Size_Per_Set-testsize;

    % get shuffled trials for train set

    TrainBool = ones(1,Size_Per_Set); TrainBool(rep)=0;

    % get shuffled trials for validate set

    Test1Bool = zeros(1,Size_Per_Set); Test1Bool(rep)=1;



    trainsize = Size_Per_Set-2*testsize;

    % get shuffled trials for train set

    TrainBool = ones(1,Size_Per_Set); TrainBool(rep)=0;

    % get shuffled trials for validate set

    Test1Bool = zeros(1,Size_Per_Set); Test1Bool(rep)=1;

    % get shuffled trials for test set

    Test2Bool = zeros(1,Size_Per_Set); if rep==28, Test2Bool(1)=1; else Test2Bool(rep+1)=1; end

    % same for standards and targets

    TrainBool = [TrainBool TrainBool];

    Test1Bool = [Test1Bool Test1Bool];

    Test2Bool = [Test2Bool Test2Bool];

    

    

    % 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,'_iter28.mat'],'LASSO_Probability_Train','LASSO_Probability_Tst1','LASSO_Probability_Tst2','LASSO_Betas');