-
Notifications
You must be signed in to change notification settings - Fork 1
/
logistic.cpp
409 lines (320 loc) · 7.77 KB
/
logistic.cpp
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
//////////////////////////////////////////////////////////////////
// //
// PLINK (c) 2005-2009 Shaun Purcell //
// //
// This file is distributed under the GNU General Public //
// License, Version 2. Please see the file COPYING for more //
// details //
// //
//////////////////////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <cmath>
#include "logistic.h"
#include "plink.h"
#include "helper.h"
#include "options.h"
#include "stats.h"
LogisticModel::LogisticModel(Plink * p_)
{
P = p_;
nc = 0;
cluster = false;
}
void LogisticModel::setDependent()
{
// Set phenotype to 'aff' variable
Y.clear();
for (int i=0; i<P->n; i++)
{
if ( !miss[i] )
{
if ( P->sample[i]->pperson->aff )
Y.push_back( 1 ) ;
else
Y.push_back( 0 ) ;
}
}
nind = Y.size();
p.resize(nind);
V.resize(nind);
}
void LogisticModel::pruneY()
{
//////////////////////////////////
// Prune out rows that are missing
if ( miss.size() != Y.size() )
error("Internal error: bad call to Model::pruneY()");
vector<int> Y2;
for (int i=0; i<Y.size(); i++)
if ( ! miss[i] )
Y2.push_back(Y[i]);
Y = Y2;
}
void LogisticModel::fitLM()
{
coef.resize(np);
sizeMatrix(S,np,np);
if (np==0 || nind==0 || ! all_valid )
return;
if (par::verbose)
{
for (int i=0; i<nind; i++)
{
cout << i << "\t"
<< Y[i] << "\t";
for (int j=0; j<np; j++)
cout << X[i][j] << "\t";
cout << "\n";
}
}
///////////////////////////////////////
// Newton-Raphson to fit logistic model
bool converge = false;
int it = 0;
while ( ! converge && it < 20 )
{
// Determine p and V
for (int i=0; i<nind; i++)
{
double t = 0;
for (int j=0; j<np; j++)
t += coef[j] * X[i][j];
p[i] = 1/(1+exp(-t));
V[i] = p[i] * (1-p[i]);
}
// Update coefficients
// b <- b + solve( t(X) %*% V %*% X ) %*% t(X) %*% ( y - p )
matrix_t T;
sizeMatrix(T,np,np);
for (int j=0; j<np; j++)
for (int k=j; k<np; k++)
{
double sum = 0;
for (int i=0; i<nind; i++)
sum += X[i][j] * V[i] * X[i][k] ;
T[j][k] = T[k][j] = sum;
}
bool flag = true;
T = svd_inverse(T,flag);
if ( ! flag )
{
all_valid = false;
return;
}
matrix_t T2;
// Resize and set elements to 0
sizeMatrix(T2,np,nind);
// note implicit transpose of X
for (int i=0; i<np; i++)
for (int j=0; j<nind; j++)
for (int k=0; k<np; k++)
T2[i][j] += T[i][k] * X[j][k];
vector_t t3(nind);
for (int i=0; i<nind; i++)
t3[i] = Y[i] - p[i];
vector_t ncoef(np);
for (int j=0; j<np; j++)
for (int i=0; i<nind; i++)
ncoef[j] += T2[j][i] * t3[i];
// Update coefficients, and check for
// convergence
double delta = 0;
for (int j=0; j<np; j++)
{
delta += abs(ncoef[j]);
coef[j] += ncoef[j];
}
if ( delta < 1e-6 )
converge = true;
// Next iteration
it++;
}
/////////////////////////////////////////
// Obtain covariance matrix of estimates
// S <- solve( t(X) %*% V %*% X )
// Transpose X and multiple by diagonal V
matrix_t Xt;
sizeMatrix(Xt, np, nind);
for (int i=0; i<nind; i++)
for (int j=0; j<np; j++)
Xt[j][i] = X[i][j] * V[i];
multMatrix(Xt,X,S);
bool flag = true;
S = svd_inverse(S,flag);
if ( ! flag )
{
all_valid = false;
return;
}
if ( cluster )
HuberWhite();
if (par::verbose)
{
cout << "beta\n";
display(coef);
cout << "Sigma\n";
display(S);
cout << "\n";
}
}
vector_t LogisticModel::getCoefs()
{
return coef;
}
vector_t LogisticModel::getVar()
{
vector_t var(np);
for (int i=0; i<np; i++)
var[i] = S[i][i];
return var;
}
vector_t LogisticModel::getSE()
{
vector_t var(np);
for (int i=0; i<np; i++)
var[i] = sqrt(S[i][i]);
return var;
}
void LogisticModel::reset()
{
np=0;
nind=0;
coef.clear();
S.clear();
Y.clear();
X.clear();
miss.clear();
}
void LogisticModel::displayResults(ofstream & OUT, Locus * loc)
{
vector_t var;
if ( all_valid )
var = getVar();
else
{
var.clear();
var.resize(np,0);
}
for (int p=1; p<np; p++) // Skip intercept
{
bool okay = var[p] < 1e-20 || !realnum(var[p]) ? false : all_valid;
double se = 0;
double Z = 0;
double pvalue = 1;
if (okay)
{
se = sqrt(var[p]);
Z = coef[p] / se;
// pvalue = pT(Z,Y.size()-np);
pvalue = chiprobP(Z*Z,1);
}
// If filtering p-values
if ( (!par::pfilter) || pvalue <= par::pfvalue )
{
// Skip covariates?
if ( par::no_show_covar && p != testParameter )
continue;
OUT << setw(4) << loc->chr << " "
<< setw(par::pp_maxsnp) << loc->name << " "
<< setw(10) << loc->bp << " "
<< setw(4) << loc->allele1 << " "
<< setw(10) << label[p] << " "
<< setw(8) << Y.size() << " ";
if (okay)
{
if ( par::return_beta )
OUT << setw(10) << coef[p] << " ";
else
OUT << setw(10) << exp(coef[p]) << " ";
if (par::display_ci)
{
OUT << setw(8) << se << " ";
if ( par::return_beta )
OUT << setw(8) << coef[p] - par::ci_zt * se << " "
<< setw(8) << coef[p] + par::ci_zt * se << " ";
else
OUT << setw(8) << exp(coef[p] - par::ci_zt * se) << " "
<< setw(8) << exp(coef[p] + par::ci_zt * se) << " ";
}
OUT << setw(12) << Z << " "
<< setw(12) << pvalue;
}
else
{
OUT << setw(10) << "NA" << " ";
if (par::display_ci)
OUT << setw(8) << "NA" << " "
<< setw(8) << "NA" << " "
<< setw(8) << "NA" << " ";
OUT << setw(12) << "NA" << " "
<< setw(12) << "NA";
}
OUT << "\n";
}
}
}
double LogisticModel::getPValue()
{
vector_t var = getVar();
bool okay = var[testParameter] < 1e-20 || !realnum(var[testParameter]) ? false : all_valid;
if (all_valid)
{
double se = sqrt(var[testParameter]);
double Z = coef[testParameter] / se;
return chiprobP(Z*Z,1);
}
else return 1;
}
vector_t LogisticModel::getPVals()
{
int tmp = testParameter;
vector_t res;
for ( testParameter = 1; testParameter < np; testParameter++)
res.push_back( getPValue() );
testParameter = tmp;
return res;
}
double LogisticModel::getLnLk()
{
// Return -2 * sample log-likelihood
// We assume the model is fit, and all Y's are either 0 or 1
double lnlk = 0;
for (int i=0; i<nind; i++)
{
double t = 0;
for (int j=0; j<np; j++)
t += coef[j] * X[i][j];
lnlk += Y[i] == 1 ? log( 1/(1+exp(-t))) : log(1 - (1/(1+exp(-t))) );
}
return -2 * lnlk;
}
void LogisticModel::HuberWhite()
{
// Calculate sandwich variance estimators, potentially allowing for
// clustered data
// Works to update the S matrix, variance/covariance matrix
// Originally, S will contain this, uncorrected
// Calcuate S = (XtX)^-1
matrix_t S0 = S;
vector<vector_t> sc(nc);
for (int i=0; i<nc; i++)
sc[i].resize(np,0);
for (int i=0; i<nind; i++)
{
double err = Y[i] - p[i];
for (int j=0; j<np; j++)
sc[clst[i]][j] += err * X[i][j];
}
matrix_t meat;
sizeMatrix(meat, np, np);
for (int k=0; k<nc; k++)
{
for (int i=0; i<np; i++)
for (int j=0; j<np; j++)
meat[i][j] += sc[k][i] * sc[k][j];
}
matrix_t tmp1;
multMatrix( S0 , meat, tmp1);
multMatrix( tmp1 , S0, S);
}