forked from restrepd-zz/CaImAnDR
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrgCaImAn_LDA_for_glm.m
59 lines (45 loc) · 1.85 KB
/
drgCaImAn_LDA_for_glm.m
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
function [correct_predict,correct_predict_shuffled] = drgCaImAn_LDA_for_glm(Nall,measurements,events,no_comps)
%UNTITLED3 Summary of this function goes here
% Detailed explanation goes here
for ii=1:Nall
%Partition the data into training and test sets.
%Create input and target vectors leaving one trial out
%For per_input each column has the dF/F for one trial
%each row is a single time point for dF/F for one of the cells
%For per_target the top row is 1 if the odor is S+ and 0 if it is
%S-, and row 2 has 1 for S-
idxTrn=ones(Nall,1);
idxTrn(ii)=0;
idxTest=zeros(Nall,1);
idxTest(ii)=1;
%Store the training data in a table.
tblTrn=[];
tblTrn = array2table(measurements(logical(idxTrn),1:no_comps(ii)));
these_events=[];
noEvs=0;
all_these_events=[];
noallEvs=0;
for jj=1:Nall
if (jj~=ii)
noEvs=noEvs+1;
these_events{noEvs}=events{jj};
end
noallEvs=noallEvs+1;
all_these_events{noallEvs}=events{jj};
if jj==ii
ii_event=noallEvs;
end
end
tblTrn.Y = these_events';
%Train a discriminant analysis model using the training set and default options.
%By default this is a regularized linear discriminant analysis (LDA)
Mdl = fitcdiscr(tblTrn,'Y');
%Predict labels for the test set. You trained Mdl using a table of data, but you can predict labels using a matrix.
[label,score] = predict(Mdl,measurements(logical(idxTest),1:no_comps(ii)));
%label is the predicted label, and score is the predicted class
%posterior probability
scores(ii)=score(1);
correct_predict(ii)=strcmp(events{ii},label);
ii_shuffled=randperm(Nall);
correct_predict_shuffled(ii)=strcmp(all_these_events{ii_shuffled(ii_event)},label);
end