-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglm_results_cell_function_shuffling.m
464 lines (430 loc) · 23.6 KB
/
glm_results_cell_function_shuffling.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
function glm = glm_results_cell_function_shuffling(mouse, session, baseDir)
%% For figure 3. GLM result plots fro function assignment in each cell
% Notes:
% Assume u is fixed. No change of u.cellNums (very important in indexing)
% After running 10 repeats of glmnet_cell_tupe.m
% Based on d190322_glm_repetition.m (and former checking codes)
%
% Input:
% mouse
% session
% baseDir
%
% Files:
% 10 repeated ridge results
% Uber file
% ca anova results
% spk anova results
%
% Output:
% Proportion of touch cells, whisking cells, touch & whisking cells
% Proportion of angle-tuned touch cells.
% Comparison with anova test results
% These in C2, non-C2, L2/3, L4, L2/3 C2, L2/3 non-C2, (To compare with Peron et al), L4 C2, and L4 non-C2
%
% Proportion of sound cells, reward cells, and licking cells
%
% Example map of cells in different function assignment (information for these)
%
% glm.cellFitID
% glm.cellFitIndC2
% glm.cellFitIndL23
% glm.cellFitDepths
% glm.cellFitxpoint
% glm.cellFitypoint
% glm.cellNums = u.cellNums
% glm.cellDepths = u.cellDepths
% glm.isC2 = u.isC2
% glm.cellFunction : 1 - touch, 2 - sound, 3 - reward, 4 - whisking, 5 - licking
% glm.tunedID : cell ID from u.cellNum, that has tuning (either excited or inhibited, whichever is larger in absolute value)
% glm.tunedAngle : best tuned angle (45-135). Same length as tuned. Calculated by area under curve of each angle (same as summation of the coefficients)
% glm.tuneDirection : 1 - excited, 2 - inhibited
% glm.touchID : cell ID of touch response cells, EXCLUDING tuned cells
% ca.tunedID
% ca.tunedAngle
% ca.tuneDireaction
% ca.touchID
% spk.tunedID
% spk.tunedAngle
% spk.tuneDirection
% spk.touchID
%
% 2019/04/02 JK
% Updates:
% Forget about tuning here. It can't be well inferred from glm. 2019/04/08 JK
%
% Instead of excluding one by one, shuffle the predictors.
% This is because when excluding one group of coefficients, the intercept does not explain the partial model well
% (veryfied by the fact that inclusion method gives negative deviance explained)
% When shuffling, maintain trial sequences. Shuffle only within trials.
%
% 2109/04/11 JK
%% basic settings
% chi2pvalThreshold = 0.001; % less than 0.001 for fitting
deThreshold = 0.1; % include 0.1 as fit
coeffThreshold = 0; % include 0.01 as a coefficient
repeat = 10;
glm = struct;
% ca = struct;
% spk = struct;
L4depth = 350; % include 350 um as L4 (350 is the starting point)
% angles = 45:15:135;
%% dependent settings
ufn = sprintf('UberJK%03dS%02d',mouse, session);
% glmfnBase = sprintf('glmResponseType_JK%03dS%02d_m45_R', mouse, session);
glmfnBase = sprintf('glmWithWhiskerTouchVariables_JK%03dS%02d_R', mouse, session);
% cafn = sprintf('JK%03dS%02dsingleCell_anova_calcium_final', mouse, session);
% spkfn = sprintf('JK%03dS%02dsingleCell_anova_spk_final', mouse, session);
%% load uber
cd(sprintf('%s%03d',baseDir, mouse))
load(ufn, 'u') % loading u
u = u;
% %% select cells with average DE > deThreshold (0.1) and average coefficients
% averageDE = zeros(length(u.cellNums),1);
% allCoeff = cell(length(u.cellNums),repeat);
% for ri = 1 : repeat
% load(sprintf('%s%02d',glmfnBase, ri), 'fitCoeffs', 'fitDevExplained')
% averageDE = averageDE + fitDevExplained/repeat;
% allCoeff(:,ri) = fitCoeffs;
% end
% load(sprintf('%s%02d',glmfnBase, repeat), 'allPredictors', 'indPartial', 'posShift')
% allPredictors = allPredictors;
% indPartial = indPartial;
% posShift = posShift;
% averageCoeff = cell(length(u.cellNums),1);
% for ci = 1 : length(u.cellNums)
% averageCoeff{ci} = mean(cell2mat(allCoeff(ci,:)),2);
% end
% deFitInd = find(averageDE >= deThreshold);
load(sprintf('%s%02d',glmfnBase, repeat), 'cIDAll', 'allPredictors', 'indPartial', 'posShift')
cIDAll = cIDAll;
allPredictors = allPredictors;
indPartial = indPartial;
posShift = posShift;
numCells = length(cIDAll);
averageDE = zeros(numCells,1);
allCoeff = cell(numCells,repeat);
for ri = 1 : repeat
load(sprintf('%s%02d',glmfnBase, ri), 'fitCoeffs', 'fitDevExplained')
averageDE = averageDE + fitDevExplained/repeat;
allCoeff(:,ri) = fitCoeffs;
end
averageCoeff = cell(numCells,1);
for ci = 1 : numCells
averageCoeff{ci} = mean(cell2mat(allCoeff(ci,:)),2);
end
%% assigning functions to each cell
% cellFunction = cell(length(deFitInd), 1);
% numCells = length(u.cellNums);
% cellFunction = cell(numCells,1);
% deviance = cell(length(deFitInd),1);
deviance = zeros(numCells,1);
errorRatio = cell(numCells,1);
devExp = zeros(numCells,1);
DEdiff = cell(numCells,1);
exclusionER = cell(numCells,1);
whiskerVariableER = cell(numCells,1);
whiskerVariableExclusionER = zeros(numCells,13);
whiskerVariableDEdiff = zeros(numCells,13);
parfor ci = 1 : numCells
if ~isempty(averageCoeff{ci})
fprintf('Processing %d/%d of JK%03d S%02d \n', ci, numCells, mouse, session)
cID = cIDAll(ci);
tindCell = find(cellfun(@(x) ismember(cID, x.neuindSession), u.trials));
cindSpk = find(u.trials{tindCell(1)}.neuindSession == cID);
planeInd = floor(cID/1000);
testInput = allPredictors{planeInd};
spkTest = cell2mat(cellfun(@(x) [nan(1,posShift), x.spk(cindSpk,:), nan(1,posShift)], u.trials(tindCell)','uniformoutput',false));
if length(testInput) ~= length(spkTest)
error('input matrix and spike length mismatch')
end
finiteIndTest = intersect(find(isfinite(spkTest)), find(isfinite(sum(testInput,2))));
spkTest = spkTest(finiteIndTest);
coeff = averageCoeff{ci};
model = exp([ones(length(finiteIndTest),1),testInput(finiteIndTest,:)]*coeff);
mu = mean(spkTest); % null poisson parameter
nullLogLikelihood = sum(log(poisspdf(spkTest,mu)));
saturatedLogLikelihood = sum(log(poisspdf(spkTest,spkTest)));
fullLogLikelihood = sum(log(poisspdf(spkTest',model)));
tempDeviance = 2 * (fullLogLikelihood - nullLogLikelihood);
devExplained = (fullLogLikelihood - nullLogLikelihood)/(saturatedLogLikelihood - nullLogLikelihood);
% tempFit = zeros(1,length(indPartial) + 1);
% if devExplained >= deThreshold % for ridge
% tempFit(1) = 1;
numPermute = 100;
tempPartialDEsub = zeros(1,length(indPartial));
tempExclusionER = zeros(1,length(indPartial));
% permER = zeros(length(indPartial),numPermute);
permER = zeros(2,numPermute);
permWTV = zeros(13,numPermute);
tempWVDEdiff = zeros(1,13);
tempWTVexclusionER = zeros(1,13);
for pi = 1 : length(indPartial)
%% exclusion method
partialInds = setdiff(1:length(coeff), indPartial{pi}+1); % including intercept
partialCoeffs = coeff(partialInds);
partialModel = exp([ones(length(finiteIndTest),1),testInput(finiteIndTest,partialInds(2:end)-1)]*partialCoeffs);
partialLogLikelihood = sum(log(poisspdf(spkTest',partialModel)));
partialDevExp = (partialLogLikelihood - nullLogLikelihood)/(saturatedLogLikelihood - nullLogLikelihood);
tempPartialDEsub(pi) = devExplained - partialDevExp;
tempExclusionER(pi) = (saturatedLogLikelihood - partialLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
%% permutation method
switch pi
case 1 % in case of touch angles, I put every angle + all touches and then circshift 8 angles together.
% # of delays are different too.
aptest = testInput(:,indPartial{pi}(1));
nonanind = find(~isnan(aptest));
numGroups = length(find(diff(nonanind)>1));
indIntervals = [0;find(diff(nonanind)>1)]; % (i)+1:(i+1)
indGroups = cell(numGroups,1);
randGroups = cell(numGroups,numPermute);
for gi = 1 : numGroups
indGroups{gi} = nonanind(indIntervals(gi)+1:indIntervals(gi+1));
for ri = 1 : numPermute
randGroups{gi,ri} = indGroups{gi}(randperm(length(indGroups{gi})));
end
end
randGroups = cell2mat(randGroups);
indGroups = cell2mat(indGroups);
for ri = 1 : numPermute
tempPartialInputNodelay = testInput(:,indPartial{pi}(1:8));
tempPartialInputNodelay(indGroups,:) = tempPartialInputNodelay(randGroups(:,ri),:);
tempPartialInputAll = zeros(size(tempPartialInputNodelay,1), size(tempPartialInputNodelay,2)*3);
for di = 1 : 3
tempPartialInputAll(:,(di-1)*size(tempPartialInputNodelay,2)+1 : di*size(tempPartialInputNodelay,2)) = ...
circshift(tempPartialInputNodelay, [0 di-1]);
end
tempInput = testInput;
tempInput(:,indPartial{pi}) = tempPartialInputAll;
permModel = exp([ones(length(finiteIndTest),1),tempInput(finiteIndTest,:)]*coeff);
permLogLikelihood = sum(log(poisspdf(spkTest',permModel)));
permER(pi,ri) = (saturatedLogLikelihood - permLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
end
% case 2 % in other cases, it repeats in every segment
% % sound. shifts 3
% aptest = testInput(:,indPartial{pi}(1));
%
% nonanind = find(~isnan(aptest));
% numGroups = length(find(diff(nonanind)>1));
% indIntervals = [0;find(diff(nonanind)>1)]; % (i)+1:(i+1)
%
% indGroups = cell(numGroups,1);
%
% randGroups = cell(numGroups,numPermute);
% for gi = 1 : numGroups
% indGroups{gi} = nonanind(indIntervals(gi)+1:indIntervals(gi+1));
% for ri = 1 : numPermute
% randGroups{gi,ri} = indGroups{gi}(randperm(length(indGroups{gi})));
% end
% end
% randGroups = cell2mat(randGroups);
% indGroups = cell2mat(indGroups);
%
% for ri = 1 : numPermute
% tempPartialInputNodelay = testInput(:,indPartial{pi}(1));
% tempPartialInputNodelay(indGroups,:) = tempPartialInputNodelay(randGroups(:,ri),:);
% tempPartialInputAll = zeros(size(tempPartialInputNodelay,1), size(tempPartialInputNodelay,2)*4);
% for di = 1 : 4
% tempPartialInputAll(:,(di-1)*size(tempPartialInputNodelay,2)+1 : di*size(tempPartialInputNodelay,2)) = ...
% circshift(tempPartialInputNodelay, [0 di-1]);
% end
% tempInput = testInput;
% tempInput(:,indPartial{pi}) = tempPartialInputAll;
% permModel = exp([ones(length(finiteIndTest),1),tempInput(finiteIndTest,:)]*coeff);
% permLogLikelihood = sum(log(poisspdf(spkTest',permModel)));
% permER(pi,ri) = (saturatedLogLikelihood - permLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
% end
% case 3
% % reward. shifts 3. grouped in angles, so same as in touch
% aptest = testInput(:,indPartial{pi}(1));
%
% nonanind = find(~isnan(aptest));
% numGroups = length(find(diff(nonanind)>1));
% indIntervals = [0;find(diff(nonanind)>1)]; % (i)+1:(i+1)
%
% indGroups = cell(numGroups,1);
%
% randGroups = cell(numGroups,numPermute);
% for gi = 1 : numGroups
% indGroups{gi} = nonanind(indIntervals(gi)+1:indIntervals(gi+1));
% for ri = 1 : numPermute
% randGroups{gi,ri} = indGroups{gi}(randperm(length(indGroups{gi})));
% end
% end
% randGroups = cell2mat(randGroups);
% indGroups = cell2mat(indGroups);
%
% for ri = 1 : numPermute
% tempPartialInputNodelay = testInput(:,indPartial{pi}(1:8));
% tempPartialInputNodelay(indGroups,:) = tempPartialInputNodelay(randGroups(:,ri),:);
% tempPartialInputAll = zeros(size(tempPartialInputNodelay,1), size(tempPartialInputNodelay,2)*4);
% for di = 1 : 4
% tempPartialInputAll(:,(di-1)*size(tempPartialInputNodelay,2)+1 : di*size(tempPartialInputNodelay,2)) = ...
% circshift(tempPartialInputNodelay, [0 di-1]);
% end
% tempInput = testInput;
% tempInput(:,indPartial{pi}) = tempPartialInputAll;
% permModel = exp([ones(length(finiteIndTest),1),tempInput(finiteIndTest,:)]*coeff);
% permLogLikelihood = sum(log(poisspdf(spkTest',permModel)));
% permER(pi,ri) = (saturatedLogLikelihood - permLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
% end
% case 4
% % whisking. shifts 7. 3 groups in sequential shift
% aptest = testInput(:,indPartial{pi}(1));
%
% nonanind = find(~isnan(aptest));
% numGroups = length(find(diff(nonanind)>1));
% indIntervals = [0;find(diff(nonanind)>1)]; % (i)+1:(i+1)
%
% indGroups = cell(numGroups,1);
%
% randGroups = cell(numGroups,numPermute);
% for gi = 1 : numGroups
% indGroups{gi} = nonanind(indIntervals(gi)+1:indIntervals(gi+1));
% for ri = 1 : numPermute
% randGroups{gi,ri} = indGroups{gi}(randperm(length(indGroups{gi})));
% end
% end
% randGroups = cell2mat(randGroups);
% indGroups = cell2mat(indGroups);
%
% for ri = 1 : numPermute
% tempPartialInputNodelay = testInput(:,indPartial{pi}(1:7:21));
% tempPartialInputNodelay(indGroups,:) = tempPartialInputNodelay(randGroups(:,ri),:);
% tempPartialInputAll = zeros(size(tempPartialInputNodelay,1), size(tempPartialInputNodelay,2)*7);
% for di = 1 : 7
% tempPartialInputAll(:, di : 7 : di+14 ) = ...
% circshift(tempPartialInputNodelay, [0 di-1]);
% end
% tempInput = testInput;
% tempInput(:,indPartial{pi}) = tempPartialInputAll;
% permModel = exp([ones(length(finiteIndTest),1),tempInput(finiteIndTest,:)]*coeff);
% permLogLikelihood = sum(log(poisspdf(spkTest',permModel)));
% permER(pi,ri) = (saturatedLogLikelihood - permLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
% end
% case 5
% % licking. shifts 4. 2 groups in sequential shift
% aptest = testInput(:,indPartial{pi}(1));
%
% nonanind = find(~isnan(aptest));
% numGroups = length(find(diff(nonanind)>1));
% indIntervals = [0;find(diff(nonanind)>1)]; % (i)+1:(i+1)
%
% indGroups = cell(numGroups,1);
%
% randGroups = cell(numGroups,numPermute);
% for gi = 1 : numGroups
% indGroups{gi} = nonanind(indIntervals(gi)+1:indIntervals(gi+1));
% for ri = 1 : numPermute
% randGroups{gi,ri} = indGroups{gi}(randperm(length(indGroups{gi})));
% end
% end
% randGroups = cell2mat(randGroups);
% indGroups = cell2mat(indGroups);
%
% for ri = 1 : numPermute
% tempPartialInputNodelay = testInput(:,indPartial{pi}(1:4:5));
% tempPartialInputNodelay(indGroups,:) = tempPartialInputNodelay(randGroups(:,ri),:);
% tempPartialInputAll = zeros(size(tempPartialInputNodelay,1), size(tempPartialInputNodelay,2)*4);
% for di = 1 : 4
% tempPartialInputAll(:, di : 4 : di+4 ) = ...
% circshift(tempPartialInputNodelay, [0 di-1]);
% end
% tempInput = testInput;
% tempInput(:,indPartial{pi}) = tempPartialInputAll;
% permModel = exp([ones(length(finiteIndTest),1),tempInput(finiteIndTest,:)]*coeff);
% permLogLikelihood = sum(log(poisspdf(spkTest',permModel)));
% permER(pi,ri) = (saturatedLogLikelihood - permLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
% end
case 6
% whisker touch variables. shifts 3. 13 groups in sequential shift
aptest = testInput(:,indPartial{pi}(1));
nonanind = find(~isnan(aptest));
numGroups = length(find(diff(nonanind)>1));
indIntervals = [0;find(diff(nonanind)>1)]; % (i)+1:(i+1)
indGroups = cell(numGroups,1);
randGroups = cell(numGroups,numPermute);
for gi = 1 : numGroups
indGroups{gi} = nonanind(indIntervals(gi)+1:indIntervals(gi+1));
for ri = 1 : numPermute
randGroups{gi,ri} = indGroups{gi}(randperm(length(indGroups{gi})));
end
end
randGroups = cell2mat(randGroups);
indGroups = cell2mat(indGroups);
for ri = 1 : numPermute
tempPartialInputNodelay = testInput(:,indPartial{pi}(1:3:37));
tempPartialInputNodelay(indGroups,:) = tempPartialInputNodelay(randGroups(:,ri),:);
tempPartialInputAll = zeros(size(tempPartialInputNodelay,1), size(tempPartialInputNodelay,2)*3);
for di = 1 : 3
tempPartialInputAll(:, di : 3 : di+36 ) = ...
circshift(tempPartialInputNodelay, [0 di-1]);
end
tempInput = testInput;
tempInput(:,indPartial{pi}) = tempPartialInputAll;
permModel = exp([ones(length(finiteIndTest),1),tempInput(finiteIndTest,:)]*coeff);
permLogLikelihood = nansum(log(poisspdf(spkTest',permModel)));
permER(pi-4,ri) = (saturatedLogLikelihood - permLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
if isnan(permER(pi-4,ri))
error('nan value')
end
end
% comparing between whisker touch variables. There are
% 13 of them currently 2019/04/19 JK
for ri = 1 : numPermute
for j = 1 : 13
tempPartialInputNodelay = testInput(:,indPartial{pi}((j-1)*3+1));
tempPartialInputNodelay(indGroups,:) = tempPartialInputNodelay(randGroups(:,ri),:);
tempPartialInputAll = zeros(size(tempPartialInputNodelay,1), 3);
for di = 1 : 3
tempPartialInputAll(:, di ) = ...
circshift(tempPartialInputNodelay, [0 di-1]);
end
tempInput = testInput;
tempInput(:,indPartial{pi}((j-1)*3+1:j*3)) = tempPartialInputAll;
permModel = exp([ones(length(finiteIndTest),1),tempInput(finiteIndTest,:)]*coeff);
permLogLikelihood = nansum(log(poisspdf(spkTest',permModel)));
permWTV(j,ri) = (saturatedLogLikelihood - permLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
if isnan(permWTV(j,ri))
error('nan value')
end
end
end
for j = 1 : 13
partialInds = setdiff(1:length(coeff), indPartial{pi}((j-1)*3+1:j*3) + 1); % including intercept
partialCoeffs = coeff(partialInds);
partialModel = exp([ones(length(finiteIndTest),1),testInput(finiteIndTest,partialInds(2:end)-1)]*partialCoeffs);
partialLogLikelihood = nansum(log(poisspdf(spkTest',partialModel)));
partialDevExp = (partialLogLikelihood - nullLogLikelihood)/(saturatedLogLikelihood - nullLogLikelihood);
tempWVDEdiff(j) = devExplained - partialDevExp;
tempWTVexclusionER(j) = (saturatedLogLikelihood - partialLogLikelihood)/(saturatedLogLikelihood - fullLogLikelihood);
if isnan(tempWTVexclusionER(j))
error('nan value')
end
end
end
end
% if sum(tempFit(2:end)) == 0 % when there is no function fit, the cell is regarded not fit
% tempFit(1) = 0;
% end
% end
% cellFunction{ci} = tempFit;
whiskerVariableDEdiff(ci,:) = tempWVDEdiff;
whiskerVariableExclusionER(ci,:) = tempWTVexclusionER;
deviance(ci) = tempDeviance;
errorRatio{ci} = permER;
devExp(ci) = devExplained;
DEdiff{ci} = tempPartialDEsub;
exclusionER{ci} = tempExclusionER;
whiskerVariableER{ci} = permWTV;
end
end
glm.cID = u.cellNums;
glm.deviance = deviance;
glm.errorRatio = errorRatio;
glm.devExp = devExp;
glm.DEdiff = DEdiff;
glm.exclusionER = exclusionER;
glm.whiskerVariableER = whiskerVariableER;
glm.whiskerVariableExclusionER = whiskerVariableExclusionER;
glm.whiskerVariableDEdiff = whiskerVariableDEdiff;