forked from 3poundhealth/qrisk2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q77_qrisk2_2013_0.c
321 lines (292 loc) · 10.9 KB
/
Q77_qrisk2_2013_0.c
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
/*
* Copyright 2013 ClinRisk Ltd.
*
* This file is part of QRISK2-2013 (http://qrisk.org, http://svn.clinrisk.co.uk/opensource/qrisk2).
*
* QRISK2-2013 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QRISK2-2013 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with QRISK2-2013. If not, see <http://www.gnu.org/licenses/>.
*
* Additional terms
*
* The following disclaimer must be displayed alongside any risk score generated by this code.
* The initial version of this file, to be found at http://svn.clinrisk.co.uk/opensource/qrisk2, faithfully implements QRISK2-2013.
* We have released this code under the GNU Lesser General Public License to enable others to implement the algorithm faithfully.
* However, the nature of the GNU Lesser General Public License is such that we cannot prevent, for example, someone accidentally
* altering the coefficients, getting the inputs wrong, or just poor programming.
* We stress, therefore, that it is the responsibility of the end user to check that the source that they receive produces the same results as the original code posted at http://svn.clinrisk.co.uk/opensource/qrisk2.
* Inaccurate implementations of risk scores can lead to wrong patients being given the wrong treatment.
*
* This file has been auto-generated.
* XML source: Q77_qrisk2_2013_0.xml
* STATA dta time stamp: 16 Nov 2012 22:10
* This file was created on: Wed 8 May 2013 07:41:31 BST
*/
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <Q77_qrisk2_2013_0.h>
#include <utils.h>
static double cvd_female_raw(
int age,int b_AF,int b_ra,int b_renal,int b_treatedhyp,int b_type1,int b_type2,double bmi,int ethrisk,int fh_cvd,double rati,double sbp,int smoke_cat,int surv,double town,int debug
)
{
double survivor[16] = {
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0.988779306411743,
0,
0,
0,
0,
0
};
/* The conditional arrays */
double Iethrisk[10] = {
0,
0,
0.3247649848349589700000000,
0.6490919016600146300000000,
0.3225960666395069100000000,
-0.0698843828816220060000000,
-0.0526174237973003890000000,
-0.3583620628676293400000000,
-0.4631969844038496000000000,
-0.0934620942542618430000000
};
double Ismoke[5] = {
0,
0.2325777856801529700000000,
0.5540189636820945800000000,
0.6903408593375299800000000,
0.8853278749891940700000000
};
/* Applying the fractional polynomial transforms */
/* (which includes scaling) */
double dage = age;
dage=dage/10;
double age_1 = pow(dage,.5);
double age_2 = dage;
double dbmi = bmi;
dbmi=dbmi/10;
double bmi_1 = pow(dbmi,-2);
double bmi_2 = pow(dbmi,-2)*log(dbmi);
/* Centring the continuous variables */
age_1 = age_1 - 2.111304044723511;
age_2 = age_2 - 4.457604408264160;
bmi_1 = bmi_1 - 0.153318107128143;
bmi_2 = bmi_2 - 0.143754154443741;
rati = rati - 3.597785472869873;
sbp = sbp - 126.525978088378910;
//town = town - -0.099030286073685;
if(debug) printf("a1: %6.2f, a2: %6.2f\n", age_1, age_2);
if(debug) printf("b1: %6.2f, b2: %6.2f\n", bmi_1, bmi_2);
/* Start of Sum */
double a=0;
/* The conditional sums */
a += Iethrisk[ethrisk];
a += Ismoke[smoke_cat];
/* Sum from continuous values */
a += age_1 * 4.1924277678057837000000000;
a += age_2 * 0.0727365264473135150000000;
a += bmi_1 * -0.4914322358663353900000000;
a += bmi_2 * -2.9736893891126503000000000;
a += rati * 0.1456741893144524700000000;
a += sbp * 0.0124301977948376370000000;
//a += town * 0.0638839291971372570000000;
if(debug)
printf("cr: %6.2f, bp: %6.2f\n", rati * 0.1456741893144524700000000, sbp * 0.0124301977948376370000000);
/* Sum from boolean values */
a += b_AF * 1.2548823570386274000000000;
a += b_ra * 0.3660166445401525400000000;
a += b_renal * 0.8000369779764396900000000;
a += b_treatedhyp * 0.6183822524814552900000000;
a += b_type1 * 1.7623106103850039000000000;
a += b_type2 * 1.0714795465634313000000000;
a += fh_cvd * 0.6138914873273221300000000;
if(debug)
printf("af:%5.2f, ra:%5.2f, rn:%5.2f, tr:%5.2f, rl:%5.2f, db:%5.2f\n",
b_AF * 1.2548823570386274000000000,
b_ra * 0.3660166445401525400000000,
b_renal * 0.8000369779764396900000000,
b_treatedhyp * 0.6183822524814552900000000,
fh_cvd * 0.6138914873273221300000000,
b_type1 * 1.7623106103850039000000000 +
b_type2 * 1.0714795465634313000000000
);
/* Sum from interaction terms */
a += age_1 * (smoke_cat==1) * 1.5288149154981097000000000;
a += age_1 * (smoke_cat==2) * 1.8932677924373249000000000;
a += age_1 * (smoke_cat==3) * -0.2120377720167573300000000;
a += age_1 * (smoke_cat==4) * -1.8022626575074350000000000;
a += age_1 * b_AF * -2.0489636167088636000000000;
a += age_1 * b_renal * 2.1378812069259072000000000;
a += age_1 * b_treatedhyp * -3.9467205554873872000000000;
a += age_1 * b_type1 * 5.0295952006040174000000000;
a += age_1 * b_type2 * -4.1006039491910871000000000;
a += age_1 * bmi_1 * 15.6869018580842020000000000;
a += age_1 * bmi_2 * 10.5172051502483440000000000;
a += age_1 * fh_cvd * 0.1788021490217178200000000;
a += age_1 * sbp * 0.0035841283715529319000000;
//a += age_1 * town * 0.2643549142624118100000000;
a += age_2 * (smoke_cat==1) * -0.3515526357562532800000000;
a += age_2 * (smoke_cat==2) * -0.4673806143887411200000000;
a += age_2 * (smoke_cat==3) * -0.0520627514443069360000000;
a += age_2 * (smoke_cat==4) * 0.2408148624618006200000000;
a += age_2 * b_AF * 0.1989800041198926100000000;
a += age_2 * b_renal * -0.5666471358116902400000000;
a += age_2 * b_treatedhyp * 0.6659359807539360100000000;
a += age_2 * b_type1 * -1.3790306447153591000000000;
a += age_2 * b_type2 * 0.6477002677213917800000000;
a += age_2 * bmi_1 * -3.0654336861574962000000000;
a += age_2 * bmi_2 * -1.0265341871793443000000000;
a += age_2 * fh_cvd * -0.1598294041246048900000000;
a += age_2 * sbp * -0.0037836254753488216000000;
//a += age_2 * town * -0.0731237907079357600000000;
if(debug){
printf("i:%6.2f u:%6.2f l:%6.2f er:%6.2f\n",
Iethrisk[ethrisk]+
Ismoke[smoke_cat]+
bmi_1 * -0.4914322358663353900000000+
bmi_2 * -2.9736893891126503000000000+
b_AF * 1.2548823570386274000000000+
b_ra * 0.3660166445401525400000000+
b_renal * 0.8000369779764396900000000+
b_treatedhyp * 0.6183822524814552900000000+
b_type1 * 1.7623106103850039000000000+
b_type2 * 1.0714795465634313000000000+
fh_cvd * 0.6138914873273221300000000,
age_1 * (smoke_cat==1) * 1.5288149154981097000000000+
age_1 * (smoke_cat==2) * 1.8932677924373249000000000+
age_1 * (smoke_cat==3) * -0.2120377720167573300000000+
age_1 * (smoke_cat==4) * -1.8022626575074350000000000+
age_1 * b_AF * -2.0489636167088636000000000+
age_1 * b_renal * 2.1378812069259072000000000+
age_1 * b_treatedhyp * -3.9467205554873872000000000+
age_1 * b_type1 * 5.0295952006040174000000000+
age_1 * b_type2 * -4.1006039491910871000000000+
age_1 * bmi_1 * 15.6869018580842020000000000+
age_1 * bmi_2 * 10.5172051502483440000000000+
age_1 * fh_cvd * 0.1788021490217178200000000+
age_1 * sbp * 0.0035841283715529319000000,
age_2 * (smoke_cat==1) * -0.3515526357562532800000000+
age_2 * (smoke_cat==2) * -0.4673806143887411200000000+
age_2 * (smoke_cat==3) * -0.0520627514443069360000000+
age_2 * (smoke_cat==4) * 0.2408148624618006200000000+
age_2 * b_AF * 0.1989800041198926100000000+
age_2 * b_renal * -0.5666471358116902400000000+
age_2 * b_treatedhyp * 0.6659359807539360100000000+
age_2 * b_type1 * -1.3790306447153591000000000+
age_2 * b_type2 * 0.6477002677213917800000000+
age_2 * bmi_1 * -3.0654336861574962000000000+
age_2 * bmi_2 * -1.0265341871793443000000000+
age_2 * fh_cvd * -0.1598294041246048900000000+
age_2 * sbp * -0.0037836254753488216000000,
Iethrisk[ethrisk]
);
printf("bp:%6.2f bu:%6.2f bl:%6.2f\n",
sbp,
age_1 * sbp * 0.0035841283715529319000000,
age_2 * sbp * -0.0037836254753488216000000
);
}
/* Calculate the score itself */
double score = 100.0 * (1 - pow(survivor[surv], exp(a)) );
return score;
}
static int cvd_female_validation(
int age,int b_AF,int b_ra,int b_renal,int b_treatedhyp,int b_type1,int b_type2,double bmi,int ethrisk,int fh_cvd,double rati,double sbp,int smoke_cat,int surv,double town,char *errorBuf,int errorBufSize
)
{
int ok=1;
*errorBuf=0;
if (!i_in_range(age,25,84)) {
ok=0;
strlcat(errorBuf,"error: age must be in range (25,84)\n",errorBufSize);
}
if (!is_boolean(b_AF)) {
ok=0;
strlcat(errorBuf,"error: b_AF must be in range (0,1)\n",errorBufSize);
}
if (!is_boolean(b_ra)) {
ok=0;
strlcat(errorBuf,"error: b_ra must be in range (0,1)\n",errorBufSize);
}
if (!is_boolean(b_renal)) {
ok=0;
strlcat(errorBuf,"error: b_renal must be in range (0,1)\n",errorBufSize);
}
if (!is_boolean(b_treatedhyp)) {
ok=0;
strlcat(errorBuf,"error: b_treatedhyp must be in range (0,1)\n",errorBufSize);
}
if (!is_boolean(b_type1)) {
ok=0;
strlcat(errorBuf,"error: b_type1 must be in range (0,1)\n",errorBufSize);
}
if (!is_boolean(b_type2)) {
ok=0;
strlcat(errorBuf,"error: b_type2 must be in range (0,1)\n",errorBufSize);
}
if (!d_in_range(bmi,20,40)) {
ok=0;
strlcat(errorBuf,"error: bmi must be in range (20,40)\n",errorBufSize);
}
if (!i_in_range(ethrisk,1,9)) {
ok=0;
strlcat(errorBuf,"error: ethrisk must be in range (1,9)\n",errorBufSize);
}
if (!is_boolean(fh_cvd)) {
ok=0;
strlcat(errorBuf,"error: fh_cvd must be in range (0,1)\n",errorBufSize);
}
if (!d_in_range(rati,1,12)) {
ok=0;
strlcat(errorBuf,"error: rati must be in range (1,12)\n",errorBufSize);
}
if (!d_in_range(sbp,70,210)) {
ok=0;
strlcat(errorBuf,"error: sbp must be in range (70,210)\n",errorBufSize);
}
if (!i_in_range(smoke_cat,0,4)) {
ok=0;
strlcat(errorBuf,"error: smoke_cat must be in range (0,4)\n",errorBufSize);
}
if (surv!=10) {
ok=0;
strlcat(errorBuf,"error: surv must be 10\n",errorBufSize);
}
if (!d_in_range(town,-7,11)) {
ok=0;
strlcat(errorBuf,"error: town must be in range (-7,11)\n",errorBufSize);
}
return ok;
}
double cvd_female(
int age,int b_AF,int b_ra,int b_renal,int b_treatedhyp,int b_type1,int b_type2,double bmi,int ethrisk,int fh_cvd,double rati,double sbp,int smoke_cat,int surv,double town,int *error,char *errorBuf,int errorBufSize, int debug
)
{ /*
*error = 0; int ok = cvd_female_validation(age,b_AF,b_ra,b_renal,b_treatedhyp,b_type1,b_type2,bmi,ethrisk,fh_cvd,rati,sbp,smoke_cat,surv,town,errorBuf,errorBufSize);
if(!ok) {
*error = 1;
return 0.0;
}*/
return cvd_female_raw(age,b_AF,b_ra,b_renal,b_treatedhyp,b_type1,b_type2,bmi,ethrisk,fh_cvd,rati,sbp,smoke_cat,surv,town,debug);
}