Annotation of imach/src/imach.c, revision 1.366
1.366 ! brouard 1: /* $Id: imach.c,v 1.365 2024/06/28 13:53:38 brouard Exp $
1.126 brouard 2: $State: Exp $
1.360 brouard 3: $Log: imach.c,v $
1.366 ! brouard 4: Revision 1.365 2024/06/28 13:53:38 brouard
! 5: * imach.c (Module): fixing some bugs in gnuplot and quantitative variables, but not completely solved
! 6:
1.365 brouard 7: Revision 1.364 2024/06/28 12:27:05 brouard
8: * imach.c (Module): fixing some bugs in gnuplot and quantitative variables, but not completely solved
9:
1.364 brouard 10: Revision 1.363 2024/06/28 09:31:55 brouard
11: Summary: Adding log lines too
12:
1.363 brouard 13: Revision 1.362 2024/06/28 08:00:31 brouard
14: Summary: 0.99s6
15:
16: * imach.c (Module): s6 errors with age*age (harmless).
17:
1.362 brouard 18: Revision 1.361 2024/05/12 20:29:32 brouard
19: Summary: Version 0.99s5
20:
21: * src/imach.c Version 0.99s5 In fact, the covariance of total life
22: expectancy e.. with a partial life expectancy e.j is high,
23: therefore the complete matrix of variance covariance has to be
24: included in the formula of the standard error of the proportion of
25: total life expectancy spent in a specific state:
26: var(X/Y)=mu_x^2/mu_y^2*(sigma_x^2/mu_x^2 -2
27: sigma_xy/mu_x/mu_y+sigma^2/mu_y^2). Also an error with mle=-3
28: made the program core dump. It is fixed in this version.
29:
1.361 brouard 30: Revision 1.360 2024/04/30 10:59:22 brouard
31: Summary: Version 0.99s4 and estimation of std of e.j/e..
32:
1.360 brouard 33: Revision 1.359 2024/04/24 21:21:17 brouard
34: Summary: First IMaCh version using Brent Praxis software based on Buckhardt and Gegenfürtner C codes
35:
1.359 brouard 36: Revision 1.6 2024/04/24 21:10:29 brouard
37: Summary: First IMaCh version using Brent Praxis software based on Buckhardt and Gegenfürtner C codes
1.358 brouard 38:
1.359 brouard 39: Revision 1.5 2023/10/09 09:10:01 brouard
40: Summary: trying to reconsider
1.357 brouard 41:
1.359 brouard 42: Revision 1.4 2023/06/22 12:50:51 brouard
43: Summary: stil on going
1.357 brouard 44:
1.359 brouard 45: Revision 1.3 2023/06/22 11:28:07 brouard
46: *** empty log message ***
1.356 brouard 47:
1.359 brouard 48: Revision 1.2 2023/06/22 11:22:40 brouard
49: Summary: with svd but not working yet
1.355 brouard 50:
1.354 brouard 51: Revision 1.353 2023/05/08 18:48:22 brouard
52: *** empty log message ***
53:
1.353 brouard 54: Revision 1.352 2023/04/29 10:46:21 brouard
55: *** empty log message ***
56:
1.352 brouard 57: Revision 1.351 2023/04/29 10:43:47 brouard
58: Summary: 099r45
59:
1.351 brouard 60: Revision 1.350 2023/04/24 11:38:06 brouard
61: *** empty log message ***
62:
1.350 brouard 63: Revision 1.349 2023/01/31 09:19:37 brouard
64: Summary: Improvements in models with age*Vn*Vm
65:
1.348 brouard 66: Revision 1.347 2022/09/18 14:36:44 brouard
67: Summary: version 0.99r42
68:
1.347 brouard 69: Revision 1.346 2022/09/16 13:52:36 brouard
70: * src/imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
71:
1.346 brouard 72: Revision 1.345 2022/09/16 13:40:11 brouard
73: Summary: Version 0.99r41
74:
75: * imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
76:
1.345 brouard 77: Revision 1.344 2022/09/14 19:33:30 brouard
78: Summary: version 0.99r40
79:
80: * imach.c (Module): Fixing names of variables in T_ (thanks to Feinuo)
81:
1.344 brouard 82: Revision 1.343 2022/09/14 14:22:16 brouard
83: Summary: version 0.99r39
84:
85: * imach.c (Module): Version 0.99r39 with colored dummy covariates
86: (fixed or time varying), using new last columns of
87: ILK_parameter.txt file.
88:
1.343 brouard 89: Revision 1.342 2022/09/11 19:54:09 brouard
90: Summary: 0.99r38
91:
92: * imach.c (Module): Adding timevarying products of any kinds,
93: should work before shifting cotvar from ncovcol+nqv columns in
94: order to have a correspondance between the column of cotvar and
95: the id of column.
96: (Module): Some cleaning and adding covariates in ILK.txt
97:
1.342 brouard 98: Revision 1.341 2022/09/11 07:58:42 brouard
99: Summary: Version 0.99r38
100:
101: After adding change in cotvar.
102:
1.341 brouard 103: Revision 1.340 2022/09/11 07:53:11 brouard
104: Summary: Version imach 0.99r37
105:
106: * imach.c (Module): Adding timevarying products of any kinds,
107: should work before shifting cotvar from ncovcol+nqv columns in
108: order to have a correspondance between the column of cotvar and
109: the id of column.
110:
1.340 brouard 111: Revision 1.339 2022/09/09 17:55:22 brouard
112: Summary: version 0.99r37
113:
114: * imach.c (Module): Many improvements for fixing products of fixed
115: timevarying as well as fixed * fixed, and test with quantitative
116: covariate.
117:
1.339 brouard 118: Revision 1.338 2022/09/04 17:40:33 brouard
119: Summary: 0.99r36
120:
121: * imach.c (Module): Now the easy runs i.e. without result or
122: model=1+age only did not work. The defautl combination should be 1
123: and not 0 because everything hasn't been tranformed yet.
124:
1.338 brouard 125: Revision 1.337 2022/09/02 14:26:02 brouard
126: Summary: version 0.99r35
127:
128: * src/imach.c: Version 0.99r35 because it outputs same results with
129: 1+age+V1+V1*age for females and 1+age for females only
130: (education=1 noweight)
131:
1.337 brouard 132: Revision 1.336 2022/08/31 09:52:36 brouard
133: *** empty log message ***
134:
1.336 brouard 135: Revision 1.335 2022/08/31 08:23:16 brouard
136: Summary: improvements...
137:
1.335 brouard 138: Revision 1.334 2022/08/25 09:08:41 brouard
139: Summary: In progress for quantitative
140:
1.334 brouard 141: Revision 1.333 2022/08/21 09:10:30 brouard
142: * src/imach.c (Module): Version 0.99r33 A lot of changes in
143: reassigning covariates: my first idea was that people will always
144: use the first covariate V1 into the model but in fact they are
145: producing data with many covariates and can use an equation model
146: with some of the covariate; it means that in a model V2+V3 instead
147: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
148: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
149: the equation model is restricted to two variables only (V2, V3)
150: and the combination for V2 should be codtabm(k,1) instead of
151: (codtabm(k,2), and the code should be
152: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
153: made. All of these should be simplified once a day like we did in
154: hpxij() for example by using precov[nres] which is computed in
155: decoderesult for each nres of each resultline. Loop should be done
156: on the equation model globally by distinguishing only product with
157: age (which are changing with age) and no more on type of
158: covariates, single dummies, single covariates.
159:
1.333 brouard 160: Revision 1.332 2022/08/21 09:06:25 brouard
161: Summary: Version 0.99r33
162:
163: * src/imach.c (Module): Version 0.99r33 A lot of changes in
164: reassigning covariates: my first idea was that people will always
165: use the first covariate V1 into the model but in fact they are
166: producing data with many covariates and can use an equation model
167: with some of the covariate; it means that in a model V2+V3 instead
168: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
169: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
170: the equation model is restricted to two variables only (V2, V3)
171: and the combination for V2 should be codtabm(k,1) instead of
172: (codtabm(k,2), and the code should be
173: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
174: made. All of these should be simplified once a day like we did in
175: hpxij() for example by using precov[nres] which is computed in
176: decoderesult for each nres of each resultline. Loop should be done
177: on the equation model globally by distinguishing only product with
178: age (which are changing with age) and no more on type of
179: covariates, single dummies, single covariates.
180:
1.332 brouard 181: Revision 1.331 2022/08/07 05:40:09 brouard
182: *** empty log message ***
183:
1.331 brouard 184: Revision 1.330 2022/08/06 07:18:25 brouard
185: Summary: last 0.99r31
186:
187: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
188:
1.330 brouard 189: Revision 1.329 2022/08/03 17:29:54 brouard
190: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
191:
1.329 brouard 192: Revision 1.328 2022/07/27 17:40:48 brouard
193: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
194:
1.328 brouard 195: Revision 1.327 2022/07/27 14:47:35 brouard
196: Summary: Still a problem for one-step probabilities in case of quantitative variables
197:
1.327 brouard 198: Revision 1.326 2022/07/26 17:33:55 brouard
199: Summary: some test with nres=1
200:
1.326 brouard 201: Revision 1.325 2022/07/25 14:27:23 brouard
202: Summary: r30
203:
204: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
205: coredumped, revealed by Feiuno, thank you.
206:
1.325 brouard 207: Revision 1.324 2022/07/23 17:44:26 brouard
208: *** empty log message ***
209:
1.324 brouard 210: Revision 1.323 2022/07/22 12:30:08 brouard
211: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
212:
1.323 brouard 213: Revision 1.322 2022/07/22 12:27:48 brouard
214: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
215:
1.322 brouard 216: Revision 1.321 2022/07/22 12:04:24 brouard
217: Summary: r28
218:
219: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
220:
1.321 brouard 221: Revision 1.320 2022/06/02 05:10:11 brouard
222: *** empty log message ***
223:
1.320 brouard 224: Revision 1.319 2022/06/02 04:45:11 brouard
225: * imach.c (Module): Adding the Wald tests from the log to the main
226: htm for better display of the maximum likelihood estimators.
227:
1.319 brouard 228: Revision 1.318 2022/05/24 08:10:59 brouard
229: * imach.c (Module): Some attempts to find a bug of wrong estimates
230: of confidencce intervals with product in the equation modelC
231:
1.318 brouard 232: Revision 1.317 2022/05/15 15:06:23 brouard
233: * imach.c (Module): Some minor improvements
234:
1.317 brouard 235: Revision 1.316 2022/05/11 15:11:31 brouard
236: Summary: r27
237:
1.316 brouard 238: Revision 1.315 2022/05/11 15:06:32 brouard
239: *** empty log message ***
240:
1.315 brouard 241: Revision 1.314 2022/04/13 17:43:09 brouard
242: * imach.c (Module): Adding link to text data files
243:
1.314 brouard 244: Revision 1.313 2022/04/11 15:57:42 brouard
245: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
246:
1.313 brouard 247: Revision 1.312 2022/04/05 21:24:39 brouard
248: *** empty log message ***
249:
1.312 brouard 250: Revision 1.311 2022/04/05 21:03:51 brouard
251: Summary: Fixed quantitative covariates
252:
253: Fixed covariates (dummy or quantitative)
254: with missing values have never been allowed but are ERRORS and
255: program quits. Standard deviations of fixed covariates were
256: wrongly computed. Mean and standard deviations of time varying
257: covariates are still not computed.
258:
1.311 brouard 259: Revision 1.310 2022/03/17 08:45:53 brouard
260: Summary: 99r25
261:
262: Improving detection of errors: result lines should be compatible with
263: the model.
264:
1.310 brouard 265: Revision 1.309 2021/05/20 12:39:14 brouard
266: Summary: Version 0.99r24
267:
1.309 brouard 268: Revision 1.308 2021/03/31 13:11:57 brouard
269: Summary: Version 0.99r23
270:
271:
272: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
273:
1.308 brouard 274: Revision 1.307 2021/03/08 18:11:32 brouard
275: Summary: 0.99r22 fixed bug on result:
276:
1.307 brouard 277: Revision 1.306 2021/02/20 15:44:02 brouard
278: Summary: Version 0.99r21
279:
280: * imach.c (Module): Fix bug on quitting after result lines!
281: (Module): Version 0.99r21
282:
1.306 brouard 283: Revision 1.305 2021/02/20 15:28:30 brouard
284: * imach.c (Module): Fix bug on quitting after result lines!
285:
1.305 brouard 286: Revision 1.304 2021/02/12 11:34:20 brouard
287: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
288:
1.304 brouard 289: Revision 1.303 2021/02/11 19:50:15 brouard
290: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
291:
1.303 brouard 292: Revision 1.302 2020/02/22 21:00:05 brouard
293: * (Module): imach.c Update mle=-3 (for computing Life expectancy
294: and life table from the data without any state)
295:
1.302 brouard 296: Revision 1.301 2019/06/04 13:51:20 brouard
297: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
298:
1.301 brouard 299: Revision 1.300 2019/05/22 19:09:45 brouard
300: Summary: version 0.99r19 of May 2019
301:
1.300 brouard 302: Revision 1.299 2019/05/22 18:37:08 brouard
303: Summary: Cleaned 0.99r19
304:
1.299 brouard 305: Revision 1.298 2019/05/22 18:19:56 brouard
306: *** empty log message ***
307:
1.298 brouard 308: Revision 1.297 2019/05/22 17:56:10 brouard
309: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
310:
1.297 brouard 311: Revision 1.296 2019/05/20 13:03:18 brouard
312: Summary: Projection syntax simplified
313:
314:
315: We can now start projections, forward or backward, from the mean date
316: of inteviews up to or down to a number of years of projection:
317: prevforecast=1 yearsfproj=15.3 mobil_average=0
318: or
319: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
320: or
321: prevbackcast=1 yearsbproj=12.3 mobil_average=1
322: or
323: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
324:
1.296 brouard 325: Revision 1.295 2019/05/18 09:52:50 brouard
326: Summary: doxygen tex bug
327:
1.295 brouard 328: Revision 1.294 2019/05/16 14:54:33 brouard
329: Summary: There was some wrong lines added
330:
1.294 brouard 331: Revision 1.293 2019/05/09 15:17:34 brouard
332: *** empty log message ***
333:
1.293 brouard 334: Revision 1.292 2019/05/09 14:17:20 brouard
335: Summary: Some updates
336:
1.292 brouard 337: Revision 1.291 2019/05/09 13:44:18 brouard
338: Summary: Before ncovmax
339:
1.291 brouard 340: Revision 1.290 2019/05/09 13:39:37 brouard
341: Summary: 0.99r18 unlimited number of individuals
342:
343: The number n which was limited to 20,000 cases is now unlimited, from firstobs to lastobs. If the number is too for the virtual memory, probably an error will occur.
344:
1.290 brouard 345: Revision 1.289 2018/12/13 09:16:26 brouard
346: Summary: Bug for young ages (<-30) will be in r17
347:
1.289 brouard 348: Revision 1.288 2018/05/02 20:58:27 brouard
349: Summary: Some bugs fixed
350:
1.288 brouard 351: Revision 1.287 2018/05/01 17:57:25 brouard
352: Summary: Bug fixed by providing frequencies only for non missing covariates
353:
1.287 brouard 354: Revision 1.286 2018/04/27 14:27:04 brouard
355: Summary: some minor bugs
356:
1.286 brouard 357: Revision 1.285 2018/04/21 21:02:16 brouard
358: Summary: Some bugs fixed, valgrind tested
359:
1.285 brouard 360: Revision 1.284 2018/04/20 05:22:13 brouard
361: Summary: Computing mean and stdeviation of fixed quantitative variables
362:
1.284 brouard 363: Revision 1.283 2018/04/19 14:49:16 brouard
364: Summary: Some minor bugs fixed
365:
1.283 brouard 366: Revision 1.282 2018/02/27 22:50:02 brouard
367: *** empty log message ***
368:
1.282 brouard 369: Revision 1.281 2018/02/27 19:25:23 brouard
370: Summary: Adding second argument for quitting
371:
1.281 brouard 372: Revision 1.280 2018/02/21 07:58:13 brouard
373: Summary: 0.99r15
374:
375: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
376:
1.280 brouard 377: Revision 1.279 2017/07/20 13:35:01 brouard
378: Summary: temporary working
379:
1.279 brouard 380: Revision 1.278 2017/07/19 14:09:02 brouard
381: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
382:
1.278 brouard 383: Revision 1.277 2017/07/17 08:53:49 brouard
384: Summary: BOM files can be read now
385:
1.277 brouard 386: Revision 1.276 2017/06/30 15:48:31 brouard
387: Summary: Graphs improvements
388:
1.276 brouard 389: Revision 1.275 2017/06/30 13:39:33 brouard
390: Summary: Saito's color
391:
1.275 brouard 392: Revision 1.274 2017/06/29 09:47:08 brouard
393: Summary: Version 0.99r14
394:
1.274 brouard 395: Revision 1.273 2017/06/27 11:06:02 brouard
396: Summary: More documentation on projections
397:
1.273 brouard 398: Revision 1.272 2017/06/27 10:22:40 brouard
399: Summary: Color of backprojection changed from 6 to 5(yellow)
400:
1.272 brouard 401: Revision 1.271 2017/06/27 10:17:50 brouard
402: Summary: Some bug with rint
403:
1.271 brouard 404: Revision 1.270 2017/05/24 05:45:29 brouard
405: *** empty log message ***
406:
1.270 brouard 407: Revision 1.269 2017/05/23 08:39:25 brouard
408: Summary: Code into subroutine, cleanings
409:
1.269 brouard 410: Revision 1.268 2017/05/18 20:09:32 brouard
411: Summary: backprojection and confidence intervals of backprevalence
412:
1.268 brouard 413: Revision 1.267 2017/05/13 10:25:05 brouard
414: Summary: temporary save for backprojection
415:
1.267 brouard 416: Revision 1.266 2017/05/13 07:26:12 brouard
417: Summary: Version 0.99r13 (improvements and bugs fixed)
418:
1.266 brouard 419: Revision 1.265 2017/04/26 16:22:11 brouard
420: Summary: imach 0.99r13 Some bugs fixed
421:
1.265 brouard 422: Revision 1.264 2017/04/26 06:01:29 brouard
423: Summary: Labels in graphs
424:
1.264 brouard 425: Revision 1.263 2017/04/24 15:23:15 brouard
426: Summary: to save
427:
1.263 brouard 428: Revision 1.262 2017/04/18 16:48:12 brouard
429: *** empty log message ***
430:
1.262 brouard 431: Revision 1.261 2017/04/05 10:14:09 brouard
432: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
433:
1.261 brouard 434: Revision 1.260 2017/04/04 17:46:59 brouard
435: Summary: Gnuplot indexations fixed (humm)
436:
1.260 brouard 437: Revision 1.259 2017/04/04 13:01:16 brouard
438: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
439:
1.259 brouard 440: Revision 1.258 2017/04/03 10:17:47 brouard
441: Summary: Version 0.99r12
442:
443: Some cleanings, conformed with updated documentation.
444:
1.258 brouard 445: Revision 1.257 2017/03/29 16:53:30 brouard
446: Summary: Temp
447:
1.257 brouard 448: Revision 1.256 2017/03/27 05:50:23 brouard
449: Summary: Temporary
450:
1.256 brouard 451: Revision 1.255 2017/03/08 16:02:28 brouard
452: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
453:
1.255 brouard 454: Revision 1.254 2017/03/08 07:13:00 brouard
455: Summary: Fixing data parameter line
456:
1.254 brouard 457: Revision 1.253 2016/12/15 11:59:41 brouard
458: Summary: 0.99 in progress
459:
1.253 brouard 460: Revision 1.252 2016/09/15 21:15:37 brouard
461: *** empty log message ***
462:
1.252 brouard 463: Revision 1.251 2016/09/15 15:01:13 brouard
464: Summary: not working
465:
1.251 brouard 466: Revision 1.250 2016/09/08 16:07:27 brouard
467: Summary: continue
468:
1.250 brouard 469: Revision 1.249 2016/09/07 17:14:18 brouard
470: Summary: Starting values from frequencies
471:
1.249 brouard 472: Revision 1.248 2016/09/07 14:10:18 brouard
473: *** empty log message ***
474:
1.248 brouard 475: Revision 1.247 2016/09/02 11:11:21 brouard
476: *** empty log message ***
477:
1.247 brouard 478: Revision 1.246 2016/09/02 08:49:22 brouard
479: *** empty log message ***
480:
1.246 brouard 481: Revision 1.245 2016/09/02 07:25:01 brouard
482: *** empty log message ***
483:
1.245 brouard 484: Revision 1.244 2016/09/02 07:17:34 brouard
485: *** empty log message ***
486:
1.244 brouard 487: Revision 1.243 2016/09/02 06:45:35 brouard
488: *** empty log message ***
489:
1.243 brouard 490: Revision 1.242 2016/08/30 15:01:20 brouard
491: Summary: Fixing a lots
492:
1.242 brouard 493: Revision 1.241 2016/08/29 17:17:25 brouard
494: Summary: gnuplot problem in Back projection to fix
495:
1.241 brouard 496: Revision 1.240 2016/08/29 07:53:18 brouard
497: Summary: Better
498:
1.240 brouard 499: Revision 1.239 2016/08/26 15:51:03 brouard
500: Summary: Improvement in Powell output in order to copy and paste
501:
502: Author:
503:
1.239 brouard 504: Revision 1.238 2016/08/26 14:23:35 brouard
505: Summary: Starting tests of 0.99
506:
1.238 brouard 507: Revision 1.237 2016/08/26 09:20:19 brouard
508: Summary: to valgrind
509:
1.237 brouard 510: Revision 1.236 2016/08/25 10:50:18 brouard
511: *** empty log message ***
512:
1.236 brouard 513: Revision 1.235 2016/08/25 06:59:23 brouard
514: *** empty log message ***
515:
1.235 brouard 516: Revision 1.234 2016/08/23 16:51:20 brouard
517: *** empty log message ***
518:
1.234 brouard 519: Revision 1.233 2016/08/23 07:40:50 brouard
520: Summary: not working
521:
1.233 brouard 522: Revision 1.232 2016/08/22 14:20:21 brouard
523: Summary: not working
524:
1.232 brouard 525: Revision 1.231 2016/08/22 07:17:15 brouard
526: Summary: not working
527:
1.231 brouard 528: Revision 1.230 2016/08/22 06:55:53 brouard
529: Summary: Not working
530:
1.230 brouard 531: Revision 1.229 2016/07/23 09:45:53 brouard
532: Summary: Completing for func too
533:
1.229 brouard 534: Revision 1.228 2016/07/22 17:45:30 brouard
535: Summary: Fixing some arrays, still debugging
536:
1.227 brouard 537: Revision 1.226 2016/07/12 18:42:34 brouard
538: Summary: temp
539:
1.226 brouard 540: Revision 1.225 2016/07/12 08:40:03 brouard
541: Summary: saving but not running
542:
1.225 brouard 543: Revision 1.224 2016/07/01 13:16:01 brouard
544: Summary: Fixes
545:
1.224 brouard 546: Revision 1.223 2016/02/19 09:23:35 brouard
547: Summary: temporary
548:
1.223 brouard 549: Revision 1.222 2016/02/17 08:14:50 brouard
550: Summary: Probably last 0.98 stable version 0.98r6
551:
1.222 brouard 552: Revision 1.221 2016/02/15 23:35:36 brouard
553: Summary: minor bug
554:
1.220 brouard 555: Revision 1.219 2016/02/15 00:48:12 brouard
556: *** empty log message ***
557:
1.219 brouard 558: Revision 1.218 2016/02/12 11:29:23 brouard
559: Summary: 0.99 Back projections
560:
1.218 brouard 561: Revision 1.217 2015/12/23 17:18:31 brouard
562: Summary: Experimental backcast
563:
1.217 brouard 564: Revision 1.216 2015/12/18 17:32:11 brouard
565: Summary: 0.98r4 Warning and status=-2
566:
567: Version 0.98r4 is now:
568: - displaying an error when status is -1, date of interview unknown and date of death known;
569: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
570: Older changes concerning s=-2, dating from 2005 have been supersed.
571:
1.216 brouard 572: Revision 1.215 2015/12/16 08:52:24 brouard
573: Summary: 0.98r4 working
574:
1.215 brouard 575: Revision 1.214 2015/12/16 06:57:54 brouard
576: Summary: temporary not working
577:
1.214 brouard 578: Revision 1.213 2015/12/11 18:22:17 brouard
579: Summary: 0.98r4
580:
1.213 brouard 581: Revision 1.212 2015/11/21 12:47:24 brouard
582: Summary: minor typo
583:
1.212 brouard 584: Revision 1.211 2015/11/21 12:41:11 brouard
585: Summary: 0.98r3 with some graph of projected cross-sectional
586:
587: Author: Nicolas Brouard
588:
1.211 brouard 589: Revision 1.210 2015/11/18 17:41:20 brouard
1.252 brouard 590: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
1.210 brouard 591: Summary: Adding ftolpl parameter
592: Author: N Brouard
593:
594: We had difficulties to get smoothed confidence intervals. It was due
595: to the period prevalence which wasn't computed accurately. The inner
596: parameter ftolpl is now an outer parameter of the .imach parameter
597: file after estepm. If ftolpl is small 1.e-4 and estepm too,
598: computation are long.
599:
1.209 brouard 600: Revision 1.208 2015/11/17 14:31:57 brouard
601: Summary: temporary
602:
1.208 brouard 603: Revision 1.207 2015/10/27 17:36:57 brouard
604: *** empty log message ***
605:
1.207 brouard 606: Revision 1.206 2015/10/24 07:14:11 brouard
607: *** empty log message ***
608:
1.206 brouard 609: Revision 1.205 2015/10/23 15:50:53 brouard
610: Summary: 0.98r3 some clarification for graphs on likelihood contributions
611:
1.205 brouard 612: Revision 1.204 2015/10/01 16:20:26 brouard
613: Summary: Some new graphs of contribution to likelihood
614:
1.204 brouard 615: Revision 1.203 2015/09/30 17:45:14 brouard
616: Summary: looking at better estimation of the hessian
617:
618: Also a better criteria for convergence to the period prevalence And
619: therefore adding the number of years needed to converge. (The
620: prevalence in any alive state shold sum to one
621:
1.203 brouard 622: Revision 1.202 2015/09/22 19:45:16 brouard
623: Summary: Adding some overall graph on contribution to likelihood. Might change
624:
1.202 brouard 625: Revision 1.201 2015/09/15 17:34:58 brouard
626: Summary: 0.98r0
627:
628: - Some new graphs like suvival functions
629: - Some bugs fixed like model=1+age+V2.
630:
1.201 brouard 631: Revision 1.200 2015/09/09 16:53:55 brouard
632: Summary: Big bug thanks to Flavia
633:
634: Even model=1+age+V2. did not work anymore
635:
1.200 brouard 636: Revision 1.199 2015/09/07 14:09:23 brouard
637: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
638:
1.199 brouard 639: Revision 1.198 2015/09/03 07:14:39 brouard
640: Summary: 0.98q5 Flavia
641:
1.198 brouard 642: Revision 1.197 2015/09/01 18:24:39 brouard
643: *** empty log message ***
644:
1.197 brouard 645: Revision 1.196 2015/08/18 23:17:52 brouard
646: Summary: 0.98q5
647:
1.196 brouard 648: Revision 1.195 2015/08/18 16:28:39 brouard
649: Summary: Adding a hack for testing purpose
650:
651: After reading the title, ftol and model lines, if the comment line has
652: a q, starting with #q, the answer at the end of the run is quit. It
653: permits to run test files in batch with ctest. The former workaround was
654: $ echo q | imach foo.imach
655:
1.195 brouard 656: Revision 1.194 2015/08/18 13:32:00 brouard
657: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
658:
1.194 brouard 659: Revision 1.193 2015/08/04 07:17:42 brouard
660: Summary: 0.98q4
661:
1.193 brouard 662: Revision 1.192 2015/07/16 16:49:02 brouard
663: Summary: Fixing some outputs
664:
1.192 brouard 665: Revision 1.191 2015/07/14 10:00:33 brouard
666: Summary: Some fixes
667:
1.191 brouard 668: Revision 1.190 2015/05/05 08:51:13 brouard
669: Summary: Adding digits in output parameters (7 digits instead of 6)
670:
671: Fix 1+age+.
672:
1.190 brouard 673: Revision 1.189 2015/04/30 14:45:16 brouard
674: Summary: 0.98q2
675:
1.189 brouard 676: Revision 1.188 2015/04/30 08:27:53 brouard
677: *** empty log message ***
678:
1.188 brouard 679: Revision 1.187 2015/04/29 09:11:15 brouard
680: *** empty log message ***
681:
1.187 brouard 682: Revision 1.186 2015/04/23 12:01:52 brouard
683: Summary: V1*age is working now, version 0.98q1
684:
685: Some codes had been disabled in order to simplify and Vn*age was
686: working in the optimization phase, ie, giving correct MLE parameters,
687: but, as usual, outputs were not correct and program core dumped.
688:
1.186 brouard 689: Revision 1.185 2015/03/11 13:26:42 brouard
690: Summary: Inclusion of compile and links command line for Intel Compiler
691:
1.185 brouard 692: Revision 1.184 2015/03/11 11:52:39 brouard
693: Summary: Back from Windows 8. Intel Compiler
694:
1.184 brouard 695: Revision 1.183 2015/03/10 20:34:32 brouard
696: Summary: 0.98q0, trying with directest, mnbrak fixed
697:
698: We use directest instead of original Powell test; probably no
699: incidence on the results, but better justifications;
700: We fixed Numerical Recipes mnbrak routine which was wrong and gave
701: wrong results.
702:
1.183 brouard 703: Revision 1.182 2015/02/12 08:19:57 brouard
704: Summary: Trying to keep directest which seems simpler and more general
705: Author: Nicolas Brouard
706:
1.182 brouard 707: Revision 1.181 2015/02/11 23:22:24 brouard
708: Summary: Comments on Powell added
709:
710: Author:
711:
1.181 brouard 712: Revision 1.180 2015/02/11 17:33:45 brouard
713: Summary: Finishing move from main to function (hpijx and prevalence_limit)
714:
1.180 brouard 715: Revision 1.179 2015/01/04 09:57:06 brouard
716: Summary: back to OS/X
717:
1.179 brouard 718: Revision 1.178 2015/01/04 09:35:48 brouard
719: *** empty log message ***
720:
1.178 brouard 721: Revision 1.177 2015/01/03 18:40:56 brouard
722: Summary: Still testing ilc32 on OSX
723:
1.177 brouard 724: Revision 1.176 2015/01/03 16:45:04 brouard
725: *** empty log message ***
726:
1.176 brouard 727: Revision 1.175 2015/01/03 16:33:42 brouard
728: *** empty log message ***
729:
1.175 brouard 730: Revision 1.174 2015/01/03 16:15:49 brouard
731: Summary: Still in cross-compilation
732:
1.174 brouard 733: Revision 1.173 2015/01/03 12:06:26 brouard
734: Summary: trying to detect cross-compilation
735:
1.173 brouard 736: Revision 1.172 2014/12/27 12:07:47 brouard
737: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
738:
1.172 brouard 739: Revision 1.171 2014/12/23 13:26:59 brouard
740: Summary: Back from Visual C
741:
742: Still problem with utsname.h on Windows
743:
1.171 brouard 744: Revision 1.170 2014/12/23 11:17:12 brouard
745: Summary: Cleaning some \%% back to %%
746:
747: The escape was mandatory for a specific compiler (which one?), but too many warnings.
748:
1.170 brouard 749: Revision 1.169 2014/12/22 23:08:31 brouard
750: Summary: 0.98p
751:
752: Outputs some informations on compiler used, OS etc. Testing on different platforms.
753:
1.169 brouard 754: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 755: Summary: update
1.169 brouard 756:
1.168 brouard 757: Revision 1.167 2014/12/22 13:50:56 brouard
758: Summary: Testing uname and compiler version and if compiled 32 or 64
759:
760: Testing on Linux 64
761:
1.167 brouard 762: Revision 1.166 2014/12/22 11:40:47 brouard
763: *** empty log message ***
764:
1.166 brouard 765: Revision 1.165 2014/12/16 11:20:36 brouard
766: Summary: After compiling on Visual C
767:
768: * imach.c (Module): Merging 1.61 to 1.162
769:
1.165 brouard 770: Revision 1.164 2014/12/16 10:52:11 brouard
771: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
772:
773: * imach.c (Module): Merging 1.61 to 1.162
774:
1.164 brouard 775: Revision 1.163 2014/12/16 10:30:11 brouard
776: * imach.c (Module): Merging 1.61 to 1.162
777:
1.163 brouard 778: Revision 1.162 2014/09/25 11:43:39 brouard
779: Summary: temporary backup 0.99!
780:
1.162 brouard 781: Revision 1.1 2014/09/16 11:06:58 brouard
782: Summary: With some code (wrong) for nlopt
783:
784: Author:
785:
786: Revision 1.161 2014/09/15 20:41:41 brouard
787: Summary: Problem with macro SQR on Intel compiler
788:
1.161 brouard 789: Revision 1.160 2014/09/02 09:24:05 brouard
790: *** empty log message ***
791:
1.160 brouard 792: Revision 1.159 2014/09/01 10:34:10 brouard
793: Summary: WIN32
794: Author: Brouard
795:
1.159 brouard 796: Revision 1.158 2014/08/27 17:11:51 brouard
797: *** empty log message ***
798:
1.158 brouard 799: Revision 1.157 2014/08/27 16:26:55 brouard
800: Summary: Preparing windows Visual studio version
801: Author: Brouard
802:
803: In order to compile on Visual studio, time.h is now correct and time_t
804: and tm struct should be used. difftime should be used but sometimes I
805: just make the differences in raw time format (time(&now).
806: Trying to suppress #ifdef LINUX
807: Add xdg-open for __linux in order to open default browser.
808:
1.157 brouard 809: Revision 1.156 2014/08/25 20:10:10 brouard
810: *** empty log message ***
811:
1.156 brouard 812: Revision 1.155 2014/08/25 18:32:34 brouard
813: Summary: New compile, minor changes
814: Author: Brouard
815:
1.155 brouard 816: Revision 1.154 2014/06/20 17:32:08 brouard
817: Summary: Outputs now all graphs of convergence to period prevalence
818:
1.154 brouard 819: Revision 1.153 2014/06/20 16:45:46 brouard
820: Summary: If 3 live state, convergence to period prevalence on same graph
821: Author: Brouard
822:
1.153 brouard 823: Revision 1.152 2014/06/18 17:54:09 brouard
824: Summary: open browser, use gnuplot on same dir than imach if not found in the path
825:
1.152 brouard 826: Revision 1.151 2014/06/18 16:43:30 brouard
827: *** empty log message ***
828:
1.151 brouard 829: Revision 1.150 2014/06/18 16:42:35 brouard
830: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
831: Author: brouard
832:
1.150 brouard 833: Revision 1.149 2014/06/18 15:51:14 brouard
834: Summary: Some fixes in parameter files errors
835: Author: Nicolas Brouard
836:
1.149 brouard 837: Revision 1.148 2014/06/17 17:38:48 brouard
838: Summary: Nothing new
839: Author: Brouard
840:
841: Just a new packaging for OS/X version 0.98nS
842:
1.148 brouard 843: Revision 1.147 2014/06/16 10:33:11 brouard
844: *** empty log message ***
845:
1.147 brouard 846: Revision 1.146 2014/06/16 10:20:28 brouard
847: Summary: Merge
848: Author: Brouard
849:
850: Merge, before building revised version.
851:
1.146 brouard 852: Revision 1.145 2014/06/10 21:23:15 brouard
853: Summary: Debugging with valgrind
854: Author: Nicolas Brouard
855:
856: Lot of changes in order to output the results with some covariates
857: After the Edimburgh REVES conference 2014, it seems mandatory to
858: improve the code.
859: No more memory valgrind error but a lot has to be done in order to
860: continue the work of splitting the code into subroutines.
861: Also, decodemodel has been improved. Tricode is still not
862: optimal. nbcode should be improved. Documentation has been added in
863: the source code.
864:
1.144 brouard 865: Revision 1.143 2014/01/26 09:45:38 brouard
866: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
867:
868: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
869: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
870:
1.143 brouard 871: Revision 1.142 2014/01/26 03:57:36 brouard
872: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
873:
874: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
875:
1.142 brouard 876: Revision 1.141 2014/01/26 02:42:01 brouard
877: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
878:
1.141 brouard 879: Revision 1.140 2011/09/02 10:37:54 brouard
880: Summary: times.h is ok with mingw32 now.
881:
1.140 brouard 882: Revision 1.139 2010/06/14 07:50:17 brouard
883: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
884: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
885:
1.139 brouard 886: Revision 1.138 2010/04/30 18:19:40 brouard
887: *** empty log message ***
888:
1.138 brouard 889: Revision 1.137 2010/04/29 18:11:38 brouard
890: (Module): Checking covariates for more complex models
891: than V1+V2. A lot of change to be done. Unstable.
892:
1.137 brouard 893: Revision 1.136 2010/04/26 20:30:53 brouard
894: (Module): merging some libgsl code. Fixing computation
895: of likelione (using inter/intrapolation if mle = 0) in order to
896: get same likelihood as if mle=1.
897: Some cleaning of code and comments added.
898:
1.136 brouard 899: Revision 1.135 2009/10/29 15:33:14 brouard
900: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
901:
1.135 brouard 902: Revision 1.134 2009/10/29 13:18:53 brouard
903: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
904:
1.134 brouard 905: Revision 1.133 2009/07/06 10:21:25 brouard
906: just nforces
907:
1.133 brouard 908: Revision 1.132 2009/07/06 08:22:05 brouard
909: Many tings
910:
1.132 brouard 911: Revision 1.131 2009/06/20 16:22:47 brouard
912: Some dimensions resccaled
913:
1.131 brouard 914: Revision 1.130 2009/05/26 06:44:34 brouard
915: (Module): Max Covariate is now set to 20 instead of 8. A
916: lot of cleaning with variables initialized to 0. Trying to make
917: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
918:
1.130 brouard 919: Revision 1.129 2007/08/31 13:49:27 lievre
920: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
921:
1.129 lievre 922: Revision 1.128 2006/06/30 13:02:05 brouard
923: (Module): Clarifications on computing e.j
924:
1.128 brouard 925: Revision 1.127 2006/04/28 18:11:50 brouard
926: (Module): Yes the sum of survivors was wrong since
927: imach-114 because nhstepm was no more computed in the age
928: loop. Now we define nhstepma in the age loop.
929: (Module): In order to speed up (in case of numerous covariates) we
930: compute health expectancies (without variances) in a first step
931: and then all the health expectancies with variances or standard
932: deviation (needs data from the Hessian matrices) which slows the
933: computation.
934: In the future we should be able to stop the program is only health
935: expectancies and graph are needed without standard deviations.
936:
1.127 brouard 937: Revision 1.126 2006/04/28 17:23:28 brouard
938: (Module): Yes the sum of survivors was wrong since
939: imach-114 because nhstepm was no more computed in the age
940: loop. Now we define nhstepma in the age loop.
941: Version 0.98h
942:
1.126 brouard 943: Revision 1.125 2006/04/04 15:20:31 lievre
944: Errors in calculation of health expectancies. Age was not initialized.
945: Forecasting file added.
946:
947: Revision 1.124 2006/03/22 17:13:53 lievre
948: Parameters are printed with %lf instead of %f (more numbers after the comma).
949: The log-likelihood is printed in the log file
950:
951: Revision 1.123 2006/03/20 10:52:43 brouard
952: * imach.c (Module): <title> changed, corresponds to .htm file
953: name. <head> headers where missing.
954:
955: * imach.c (Module): Weights can have a decimal point as for
956: English (a comma might work with a correct LC_NUMERIC environment,
957: otherwise the weight is truncated).
958: Modification of warning when the covariates values are not 0 or
959: 1.
960: Version 0.98g
961:
962: Revision 1.122 2006/03/20 09:45:41 brouard
963: (Module): Weights can have a decimal point as for
964: English (a comma might work with a correct LC_NUMERIC environment,
965: otherwise the weight is truncated).
966: Modification of warning when the covariates values are not 0 or
967: 1.
968: Version 0.98g
969:
970: Revision 1.121 2006/03/16 17:45:01 lievre
971: * imach.c (Module): Comments concerning covariates added
972:
973: * imach.c (Module): refinements in the computation of lli if
974: status=-2 in order to have more reliable computation if stepm is
975: not 1 month. Version 0.98f
976:
977: Revision 1.120 2006/03/16 15:10:38 lievre
978: (Module): refinements in the computation of lli if
979: status=-2 in order to have more reliable computation if stepm is
980: not 1 month. Version 0.98f
981:
982: Revision 1.119 2006/03/15 17:42:26 brouard
983: (Module): Bug if status = -2, the loglikelihood was
984: computed as likelihood omitting the logarithm. Version O.98e
985:
986: Revision 1.118 2006/03/14 18:20:07 brouard
987: (Module): varevsij Comments added explaining the second
988: table of variances if popbased=1 .
989: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
990: (Module): Function pstamp added
991: (Module): Version 0.98d
992:
993: Revision 1.117 2006/03/14 17:16:22 brouard
994: (Module): varevsij Comments added explaining the second
995: table of variances if popbased=1 .
996: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
997: (Module): Function pstamp added
998: (Module): Version 0.98d
999:
1000: Revision 1.116 2006/03/06 10:29:27 brouard
1001: (Module): Variance-covariance wrong links and
1002: varian-covariance of ej. is needed (Saito).
1003:
1004: Revision 1.115 2006/02/27 12:17:45 brouard
1005: (Module): One freematrix added in mlikeli! 0.98c
1006:
1007: Revision 1.114 2006/02/26 12:57:58 brouard
1008: (Module): Some improvements in processing parameter
1009: filename with strsep.
1010:
1011: Revision 1.113 2006/02/24 14:20:24 brouard
1012: (Module): Memory leaks checks with valgrind and:
1013: datafile was not closed, some imatrix were not freed and on matrix
1014: allocation too.
1015:
1016: Revision 1.112 2006/01/30 09:55:26 brouard
1017: (Module): Back to gnuplot.exe instead of wgnuplot.exe
1018:
1019: Revision 1.111 2006/01/25 20:38:18 brouard
1020: (Module): Lots of cleaning and bugs added (Gompertz)
1021: (Module): Comments can be added in data file. Missing date values
1022: can be a simple dot '.'.
1023:
1024: Revision 1.110 2006/01/25 00:51:50 brouard
1025: (Module): Lots of cleaning and bugs added (Gompertz)
1026:
1027: Revision 1.109 2006/01/24 19:37:15 brouard
1028: (Module): Comments (lines starting with a #) are allowed in data.
1029:
1030: Revision 1.108 2006/01/19 18:05:42 lievre
1031: Gnuplot problem appeared...
1032: To be fixed
1033:
1034: Revision 1.107 2006/01/19 16:20:37 brouard
1035: Test existence of gnuplot in imach path
1036:
1037: Revision 1.106 2006/01/19 13:24:36 brouard
1038: Some cleaning and links added in html output
1039:
1040: Revision 1.105 2006/01/05 20:23:19 lievre
1041: *** empty log message ***
1042:
1043: Revision 1.104 2005/09/30 16:11:43 lievre
1044: (Module): sump fixed, loop imx fixed, and simplifications.
1045: (Module): If the status is missing at the last wave but we know
1046: that the person is alive, then we can code his/her status as -2
1047: (instead of missing=-1 in earlier versions) and his/her
1048: contributions to the likelihood is 1 - Prob of dying from last
1049: health status (= 1-p13= p11+p12 in the easiest case of somebody in
1050: the healthy state at last known wave). Version is 0.98
1051:
1052: Revision 1.103 2005/09/30 15:54:49 lievre
1053: (Module): sump fixed, loop imx fixed, and simplifications.
1054:
1055: Revision 1.102 2004/09/15 17:31:30 brouard
1056: Add the possibility to read data file including tab characters.
1057:
1058: Revision 1.101 2004/09/15 10:38:38 brouard
1059: Fix on curr_time
1060:
1061: Revision 1.100 2004/07/12 18:29:06 brouard
1062: Add version for Mac OS X. Just define UNIX in Makefile
1063:
1064: Revision 1.99 2004/06/05 08:57:40 brouard
1065: *** empty log message ***
1066:
1067: Revision 1.98 2004/05/16 15:05:56 brouard
1068: New version 0.97 . First attempt to estimate force of mortality
1069: directly from the data i.e. without the need of knowing the health
1070: state at each age, but using a Gompertz model: log u =a + b*age .
1071: This is the basic analysis of mortality and should be done before any
1072: other analysis, in order to test if the mortality estimated from the
1073: cross-longitudinal survey is different from the mortality estimated
1074: from other sources like vital statistic data.
1075:
1076: The same imach parameter file can be used but the option for mle should be -3.
1077:
1.324 brouard 1078: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 1079: former routines in order to include the new code within the former code.
1080:
1081: The output is very simple: only an estimate of the intercept and of
1082: the slope with 95% confident intervals.
1083:
1084: Current limitations:
1085: A) Even if you enter covariates, i.e. with the
1086: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
1087: B) There is no computation of Life Expectancy nor Life Table.
1088:
1089: Revision 1.97 2004/02/20 13:25:42 lievre
1090: Version 0.96d. Population forecasting command line is (temporarily)
1091: suppressed.
1092:
1093: Revision 1.96 2003/07/15 15:38:55 brouard
1094: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
1095: rewritten within the same printf. Workaround: many printfs.
1096:
1097: Revision 1.95 2003/07/08 07:54:34 brouard
1098: * imach.c (Repository):
1099: (Repository): Using imachwizard code to output a more meaningful covariance
1100: matrix (cov(a12,c31) instead of numbers.
1101:
1102: Revision 1.94 2003/06/27 13:00:02 brouard
1103: Just cleaning
1104:
1105: Revision 1.93 2003/06/25 16:33:55 brouard
1106: (Module): On windows (cygwin) function asctime_r doesn't
1107: exist so I changed back to asctime which exists.
1108: (Module): Version 0.96b
1109:
1110: Revision 1.92 2003/06/25 16:30:45 brouard
1111: (Module): On windows (cygwin) function asctime_r doesn't
1112: exist so I changed back to asctime which exists.
1113:
1114: Revision 1.91 2003/06/25 15:30:29 brouard
1115: * imach.c (Repository): Duplicated warning errors corrected.
1116: (Repository): Elapsed time after each iteration is now output. It
1117: helps to forecast when convergence will be reached. Elapsed time
1118: is stamped in powell. We created a new html file for the graphs
1119: concerning matrix of covariance. It has extension -cov.htm.
1120:
1121: Revision 1.90 2003/06/24 12:34:15 brouard
1122: (Module): Some bugs corrected for windows. Also, when
1123: mle=-1 a template is output in file "or"mypar.txt with the design
1124: of the covariance matrix to be input.
1125:
1126: Revision 1.89 2003/06/24 12:30:52 brouard
1127: (Module): Some bugs corrected for windows. Also, when
1128: mle=-1 a template is output in file "or"mypar.txt with the design
1129: of the covariance matrix to be input.
1130:
1131: Revision 1.88 2003/06/23 17:54:56 brouard
1132: * imach.c (Repository): Create a sub-directory where all the secondary files are. Only imach, htm, gp and r(imach) are on the main directory. Correct time and other things.
1133:
1134: Revision 1.87 2003/06/18 12:26:01 brouard
1135: Version 0.96
1136:
1137: Revision 1.86 2003/06/17 20:04:08 brouard
1138: (Module): Change position of html and gnuplot routines and added
1139: routine fileappend.
1140:
1141: Revision 1.85 2003/06/17 13:12:43 brouard
1142: * imach.c (Repository): Check when date of death was earlier that
1143: current date of interview. It may happen when the death was just
1144: prior to the death. In this case, dh was negative and likelihood
1145: was wrong (infinity). We still send an "Error" but patch by
1146: assuming that the date of death was just one stepm after the
1147: interview.
1148: (Repository): Because some people have very long ID (first column)
1149: we changed int to long in num[] and we added a new lvector for
1150: memory allocation. But we also truncated to 8 characters (left
1151: truncation)
1152: (Repository): No more line truncation errors.
1153:
1154: Revision 1.84 2003/06/13 21:44:43 brouard
1155: * imach.c (Repository): Replace "freqsummary" at a correct
1156: place. It differs from routine "prevalence" which may be called
1157: many times. Probs is memory consuming and must be used with
1158: parcimony.
1159: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1160:
1161: Revision 1.83 2003/06/10 13:39:11 lievre
1162: *** empty log message ***
1163:
1164: Revision 1.82 2003/06/05 15:57:20 brouard
1165: Add log in imach.c and fullversion number is now printed.
1166:
1167: */
1168: /*
1169: Interpolated Markov Chain
1170:
1171: Short summary of the programme:
1172:
1.227 brouard 1173: This program computes Healthy Life Expectancies or State-specific
1174: (if states aren't health statuses) Expectancies from
1175: cross-longitudinal data. Cross-longitudinal data consist in:
1176:
1177: -1- a first survey ("cross") where individuals from different ages
1178: are interviewed on their health status or degree of disability (in
1179: the case of a health survey which is our main interest)
1180:
1181: -2- at least a second wave of interviews ("longitudinal") which
1182: measure each change (if any) in individual health status. Health
1183: expectancies are computed from the time spent in each health state
1184: according to a model. More health states you consider, more time is
1185: necessary to reach the Maximum Likelihood of the parameters involved
1186: in the model. The simplest model is the multinomial logistic model
1187: where pij is the probability to be observed in state j at the second
1188: wave conditional to be observed in state i at the first
1189: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1190: etc , where 'age' is age and 'sex' is a covariate. If you want to
1191: have a more complex model than "constant and age", you should modify
1192: the program where the markup *Covariates have to be included here
1193: again* invites you to do it. More covariates you add, slower the
1.126 brouard 1194: convergence.
1195:
1196: The advantage of this computer programme, compared to a simple
1197: multinomial logistic model, is clear when the delay between waves is not
1198: identical for each individual. Also, if a individual missed an
1199: intermediate interview, the information is lost, but taken into
1200: account using an interpolation or extrapolation.
1201:
1202: hPijx is the probability to be observed in state i at age x+h
1203: conditional to the observed state i at age x. The delay 'h' can be
1204: split into an exact number (nh*stepm) of unobserved intermediate
1205: states. This elementary transition (by month, quarter,
1206: semester or year) is modelled as a multinomial logistic. The hPx
1207: matrix is simply the matrix product of nh*stepm elementary matrices
1208: and the contribution of each individual to the likelihood is simply
1209: hPijx.
1210:
1211: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 1212: of the life expectancies. It also computes the period (stable) prevalence.
1213:
1214: Back prevalence and projections:
1.227 brouard 1215:
1216: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1217: double agemaxpar, double ftolpl, int *ncvyearp, double
1218: dateprev1,double dateprev2, int firstpass, int lastpass, int
1219: mobilavproj)
1220:
1221: Computes the back prevalence limit for any combination of
1222: covariate values k at any age between ageminpar and agemaxpar and
1223: returns it in **bprlim. In the loops,
1224:
1225: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1226: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1227:
1228: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 1229: Computes for any combination of covariates k and any age between bage and fage
1230: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1231: oldm=oldms;savm=savms;
1.227 brouard 1232:
1.267 brouard 1233: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.218 brouard 1234: Computes the transition matrix starting at age 'age' over
1235: 'nhstepm*hstepm*stepm' months (i.e. until
1236: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 1237: nhstepm*hstepm matrices.
1238:
1239: Returns p3mat[i][j][h] after calling
1240: p3mat[i][j][h]=matprod2(newm,
1241: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1242: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1243: oldm);
1.226 brouard 1244:
1245: Important routines
1246:
1247: - func (or funcone), computes logit (pij) distinguishing
1248: o fixed variables (single or product dummies or quantitative);
1249: o varying variables by:
1250: (1) wave (single, product dummies, quantitative),
1251: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1252: % fixed dummy (treated) or quantitative (not done because time-consuming);
1253: % varying dummy (not done) or quantitative (not done);
1254: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1255: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1256: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1.364 brouard 1257: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, eliminating 1 1 if
1.226 brouard 1258: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 1259:
1.226 brouard 1260:
1261:
1.324 brouard 1262: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1263: Institut national d'études démographiques, Paris.
1.126 brouard 1264: This software have been partly granted by Euro-REVES, a concerted action
1265: from the European Union.
1266: It is copyrighted identically to a GNU software product, ie programme and
1267: software can be distributed freely for non commercial use. Latest version
1268: can be accessed at http://euroreves.ined.fr/imach .
1269:
1270: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1271: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1272:
1273: **********************************************************************/
1274: /*
1275: main
1276: read parameterfile
1277: read datafile
1278: concatwav
1279: freqsummary
1280: if (mle >= 1)
1281: mlikeli
1282: print results files
1283: if mle==1
1284: computes hessian
1285: read end of parameter file: agemin, agemax, bage, fage, estepm
1286: begin-prev-date,...
1287: open gnuplot file
1288: open html file
1.145 brouard 1289: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1290: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1291: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1292: freexexit2 possible for memory heap.
1293:
1294: h Pij x | pij_nom ficrestpij
1295: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1296: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1297: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1298:
1299: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1300: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1301: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1302: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1303: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1304:
1.126 brouard 1305: forecasting if prevfcast==1 prevforecast call prevalence()
1306: health expectancies
1307: Variance-covariance of DFLE
1308: prevalence()
1309: movingaverage()
1310: varevsij()
1311: if popbased==1 varevsij(,popbased)
1312: total life expectancies
1313: Variance of period (stable) prevalence
1314: end
1315: */
1316:
1.187 brouard 1317: /* #define DEBUG */
1318: /* #define DEBUGBRENT */
1.203 brouard 1319: /* #define DEBUGLINMIN */
1320: /* #define DEBUGHESS */
1321: #define DEBUGHESSIJ
1.224 brouard 1322: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 1323: #define POWELL /* Instead of NLOPT */
1.224 brouard 1324: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 1325: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1326: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.319 brouard 1327: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1.359 brouard 1328: /* #define POWELLORIGINCONJUGATE /\* Don't use conjugate but biggest decrease if valuable *\/ */
1329: /* #define NOTMINFIT */
1.126 brouard 1330:
1331: #include <math.h>
1332: #include <stdio.h>
1333: #include <stdlib.h>
1334: #include <string.h>
1.226 brouard 1335: #include <ctype.h>
1.159 brouard 1336:
1337: #ifdef _WIN32
1338: #include <io.h>
1.172 brouard 1339: #include <windows.h>
1340: #include <tchar.h>
1.159 brouard 1341: #else
1.126 brouard 1342: #include <unistd.h>
1.159 brouard 1343: #endif
1.126 brouard 1344:
1345: #include <limits.h>
1346: #include <sys/types.h>
1.171 brouard 1347:
1348: #if defined(__GNUC__)
1349: #include <sys/utsname.h> /* Doesn't work on Windows */
1350: #endif
1351:
1.126 brouard 1352: #include <sys/stat.h>
1353: #include <errno.h>
1.159 brouard 1354: /* extern int errno; */
1.126 brouard 1355:
1.157 brouard 1356: /* #ifdef LINUX */
1357: /* #include <time.h> */
1358: /* #include "timeval.h" */
1359: /* #else */
1360: /* #include <sys/time.h> */
1361: /* #endif */
1362:
1.126 brouard 1363: #include <time.h>
1364:
1.136 brouard 1365: #ifdef GSL
1366: #include <gsl/gsl_errno.h>
1367: #include <gsl/gsl_multimin.h>
1368: #endif
1369:
1.167 brouard 1370:
1.162 brouard 1371: #ifdef NLOPT
1372: #include <nlopt.h>
1373: typedef struct {
1374: double (* function)(double [] );
1375: } myfunc_data ;
1376: #endif
1377:
1.126 brouard 1378: /* #include <libintl.h> */
1379: /* #define _(String) gettext (String) */
1380:
1.349 brouard 1381: #define MAXLINE 16384 /* Was 256 and 1024 and 2048. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 1382:
1383: #define GNUPLOTPROGRAM "gnuplot"
1.343 brouard 1384: #define GNUPLOTVERSION 5.1
1385: double gnuplotversion=GNUPLOTVERSION;
1.126 brouard 1386: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1.329 brouard 1387: #define FILENAMELENGTH 256
1.126 brouard 1388:
1389: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1390: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1391:
1.349 brouard 1392: #define MAXPARM 216 /**< Maximum number of parameters for the optimization was 128 */
1.144 brouard 1393: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 1394:
1395: #define NINTERVMAX 8
1.144 brouard 1396: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1397: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1.325 brouard 1398: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1.197 brouard 1399: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 1400: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1401: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.290 brouard 1402: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1.144 brouard 1403: #define YEARM 12. /**< Number of months per year */
1.218 brouard 1404: /* #define AGESUP 130 */
1.288 brouard 1405: /* #define AGESUP 150 */
1406: #define AGESUP 200
1.268 brouard 1407: #define AGEINF 0
1.218 brouard 1408: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 1409: #define AGEBASE 40
1.194 brouard 1410: #define AGEOVERFLOW 1.e20
1.164 brouard 1411: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 1412: #ifdef _WIN32
1413: #define DIRSEPARATOR '\\'
1414: #define CHARSEPARATOR "\\"
1415: #define ODIRSEPARATOR '/'
1416: #else
1.126 brouard 1417: #define DIRSEPARATOR '/'
1418: #define CHARSEPARATOR "/"
1419: #define ODIRSEPARATOR '\\'
1420: #endif
1421:
1.366 ! brouard 1422: /* $Id: imach.c,v 1.365 2024/06/28 13:53:38 brouard Exp $ */
1.126 brouard 1423: /* $State: Exp $ */
1.196 brouard 1424: #include "version.h"
1425: char version[]=__IMACH_VERSION__;
1.360 brouard 1426: char copyright[]="April 2024,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2020, Nihon University 2021-202, INED 2000-2024";
1.366 ! brouard 1427: char fullversion[]="$Revision: 1.365 $ $Date: 2024/06/28 13:53:38 $";
1.126 brouard 1428: char strstart[80];
1429: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 1430: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.342 brouard 1431: int debugILK=0; /* debugILK is set by a #d in a comment line */
1.187 brouard 1432: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.330 brouard 1433: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1434: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1.335 brouard 1435: int cptcovn=0; /**< cptcovn decodemodel: number of covariates k of the models excluding age*products =6 and age*age but including products */
1.330 brouard 1436: int cptcovt=0; /**< cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1.335 brouard 1437: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1438: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 1439: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1.349 brouard 1440: int cptcovprodage=0; /**< Number of fixed covariates with age: V3*age or V2*V3*age 1 */
1441: int cptcovprodvage=0; /**< Number of varying covariates with age: V7*age or V7*V6*age */
1442: int cptcovdageprod=0; /**< Number of doubleproducts with age, since 0.99r44 only: age*Vn*Vm for gnuplot printing*/
1.145 brouard 1443: int cptcovprodnoage=0; /**< Number of covariate products without age */
1.335 brouard 1444: int cptcoveff=0; /* Total number of single dummy covariates (fixed or time varying) to vary for printing results (2**cptcoveff combinations of dummies)(computed in tricode as cptcov) */
1.233 brouard 1445: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1446: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.339 brouard 1447: int ncovvt=0; /* Total number of effective (wave) varying covariates (dummy or quantitative or products [without age]) in the model */
1.349 brouard 1448: int ncovvta=0; /* +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1449: int ncovta=0; /*age*V3*V2 +age*V2+agev3+ageV4 +age*V6 + age*V7+ age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of expandend products [with age]) in the model */
1450: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1451: int ncovva=0; /* +age*V6 + age*V7+ge*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1.234 brouard 1452: int nsd=0; /**< Total number of single dummy variables (output) */
1453: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 1454: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 1455: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 1456: int ntveff=0; /**< ntveff number of effective time varying variables */
1457: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 1458: int cptcov=0; /* Working variable */
1.334 brouard 1459: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1.290 brouard 1460: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1.218 brouard 1461: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.302 brouard 1462: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1.126 brouard 1463: int nlstate=2; /* Number of live states */
1464: int ndeath=1; /* Number of dead states */
1.130 brouard 1465: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.339 brouard 1466: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable*/
1467: int ncovcolt=0; /* ncovcolt=ncovcol+nqv+ntv+nqtv; total of covariates in the data, not in the model equation*/
1.126 brouard 1468: int popbased=0;
1469:
1470: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 1471: int maxwav=0; /* Maxim number of waves */
1472: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1473: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1.359 brouard 1474: int gipmx = 0;
1475: double gsw = 0; /* Global variables on the number of contributions
1.126 brouard 1476: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 1477: int mle=1, weightopt=0;
1.126 brouard 1478: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1479: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1480: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1481: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 1482: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 1483: int selected(int kvar); /* Is covariate kvar selected for printing results */
1484:
1.130 brouard 1485: double jmean=1; /* Mean space between 2 waves */
1.366 ! brouard 1486: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b); /* test */
! 1487: /* double **matprod2(); *//* test */
1.126 brouard 1488: double **oldm, **newm, **savm; /* Working pointers to matrices */
1489: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 1490: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1491:
1.136 brouard 1492: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 1493: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 1494: FILE *ficlog, *ficrespow;
1.130 brouard 1495: int globpr=0; /* Global variable for printing or not */
1.126 brouard 1496: double fretone; /* Only one call to likelihood */
1.130 brouard 1497: long ipmx=0; /* Number of contributions */
1.126 brouard 1498: double sw; /* Sum of weights */
1499: char filerespow[FILENAMELENGTH];
1500: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1501: FILE *ficresilk;
1502: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1503: FILE *ficresprobmorprev;
1504: FILE *fichtm, *fichtmcov; /* Html File */
1505: FILE *ficreseij;
1506: char filerese[FILENAMELENGTH];
1507: FILE *ficresstdeij;
1508: char fileresstde[FILENAMELENGTH];
1509: FILE *ficrescveij;
1510: char filerescve[FILENAMELENGTH];
1511: FILE *ficresvij;
1512: char fileresv[FILENAMELENGTH];
1.269 brouard 1513:
1.126 brouard 1514: char title[MAXLINE];
1.234 brouard 1515: char model[MAXLINE]; /**< The model line */
1.217 brouard 1516: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1517: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1518: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1519: char command[FILENAMELENGTH];
1520: int outcmd=0;
1521:
1.217 brouard 1522: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1523: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1524: char filelog[FILENAMELENGTH]; /* Log file */
1525: char filerest[FILENAMELENGTH];
1526: char fileregp[FILENAMELENGTH];
1527: char popfile[FILENAMELENGTH];
1528:
1529: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1530:
1.157 brouard 1531: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1532: /* struct timezone tzp; */
1533: /* extern int gettimeofday(); */
1534:
1.366 ! brouard 1535: /* extern time_t time(); */ /* Commented out for clang */
! 1536: /* struct tm tml, *gmtime(), *localtime(); */
! 1537:
1.157 brouard 1538:
1539: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1540: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1.349 brouard 1541: time_t rlast_btime; /* raw time */
1.366 ! brouard 1542: /* struct tm tm; */
! 1543: struct tm tm, tml;
1.157 brouard 1544:
1.126 brouard 1545: char strcurr[80], strfor[80];
1546:
1547: char *endptr;
1548: long lval;
1549: double dval;
1550:
1.362 brouard 1551: /* This for praxis gegen */
1552: /* int prin=1; */
1553: double h0=0.25;
1554: double macheps;
1555: double ffmin;
1556:
1.126 brouard 1557: #define NR_END 1
1558: #define FREE_ARG char*
1559: #define FTOL 1.0e-10
1560:
1561: #define NRANSI
1.240 brouard 1562: #define ITMAX 200
1563: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1564:
1565: #define TOL 2.0e-4
1566:
1567: #define CGOLD 0.3819660
1568: #define ZEPS 1.0e-10
1569: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1570:
1571: #define GOLD 1.618034
1572: #define GLIMIT 100.0
1573: #define TINY 1.0e-20
1574:
1575: static double maxarg1,maxarg2;
1576: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1577: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1578:
1579: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1580: #define rint(a) floor(a+0.5)
1.166 brouard 1581: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1582: #define mytinydouble 1.0e-16
1.166 brouard 1583: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1584: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1585: /* static double dsqrarg; */
1586: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1587: static double sqrarg;
1588: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1589: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1590: int agegomp= AGEGOMP;
1591:
1592: int imx;
1593: int stepm=1;
1594: /* Stepm, step in month: minimum step interpolation*/
1595:
1596: int estepm;
1597: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1598:
1599: int m,nb;
1600: long *num;
1.197 brouard 1601: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1602: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1603: covariate for which somebody answered excluding
1604: undefined. Usually 2: 0 and 1. */
1605: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1606: covariate for which somebody answered including
1607: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1608: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1609: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1610: double ***mobaverage, ***mobaverages; /* New global variable */
1.332 brouard 1611: double **precov; /* New global variable to store for each resultline, values of model covariates given by the resultlines (in order to speed up) */
1.126 brouard 1612: double *ageexmed,*agecens;
1613: double dateintmean=0;
1.296 brouard 1614: double anprojd, mprojd, jprojd; /* For eventual projections */
1615: double anprojf, mprojf, jprojf;
1.126 brouard 1616:
1.296 brouard 1617: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1618: double anbackf, mbackf, jbackf;
1619: double jintmean,mintmean,aintmean;
1.126 brouard 1620: double *weight;
1621: int **s; /* Status */
1.141 brouard 1622: double *agedc;
1.145 brouard 1623: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1624: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1625: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.268 brouard 1626: double **coqvar; /* Fixed quantitative covariate nqv */
1.341 brouard 1627: double ***cotvar; /* Time varying covariate start at ncovcol + nqv + (1 to ntv) */
1.225 brouard 1628: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1629: double idx;
1630: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.319 brouard 1631: /* Some documentation */
1632: /* Design original data
1633: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1634: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1635: * ntv=3 nqtv=1
1.330 brouard 1636: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1.319 brouard 1637: * For time varying covariate, quanti or dummies
1638: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1.341 brouard 1639: * cotvar[wav][ncovcol+nqv+ iv(1 to nqtv)][i]= [(1 to nqtv)][i]=(V12) quanti
1.319 brouard 1640: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1641: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1.332 brouard 1642: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1.319 brouard 1643: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1644: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1645: * k= 1 2 3 4 5 6 7 8 9 10 11
1646: */
1647: /* According to the model, more columns can be added to covar by the product of covariates */
1.318 brouard 1648: /* ncovcol=1(Males=0 Females=1) nqv=1(raedyrs) ntv=2(withoutiadl=0 withiadl=1, witoutadl=0 withoutadl=1) nqtv=1(bmi) nlstate=3 ndeath=1
1649: # States 1=Coresidence, 2 Living alone, 3 Institution
1650: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1651: */
1.349 brouard 1652: /* V5+V4+ V3+V4*V3 +V5*age+V2 +V1*V2+V1*age+V1+V4*V3*age */
1653: /* kmodel 1 2 3 4 5 6 7 8 9 10 */
1654: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 3 *//*0 for simple covariate (dummy, quantitative,*/
1655: /* fixed or varying), 1 for age product, 2 for*/
1656: /* product without age, 3 for age and double product */
1657: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 3 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1658: /*(single or product without age), 2 dummy*/
1659: /* with age product, 3 quant with age product*/
1660: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 6 */
1661: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1662: /*TnsdVar[Tvar] 1 2 3 */
1663: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1664: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1665: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1666: /* nsq 1 2 */ /* Counting single quantit tv */
1667: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1668: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1669: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1670: /* cptcovage 1 2 3 */ /* Counting cov*age in the model equation */
1671: /* Tage[cptcovage]=k 5 8 10 */ /* Position in the model of ith cov*age */
1.350 brouard 1672: /* model="V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
1673: /* p Tvard[1][1]@21 = {6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0}*/
1.354 brouard 1674: /* p Tvard[2][1]@21 = {7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0 <repeats 11 times>} */
1.350 brouard 1675: /* p Tvardk[1][1]@24 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0}*/
1676: /* p Tvardk[1][1]@22 = {0, 0, 0, 0, 0, 0, 0, 0, 6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0, 0} */
1.349 brouard 1677: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1.330 brouard 1678: /* Tvardk[4][1]=4;Tvardk[4][2]=3;Tvardk[7][1]=1;Tvardk[7][2]=2 */ /* Variables of a prod at position in the model equation*/
1.319 brouard 1679: /* TvarF TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 ID of fixed covariates or product V2, V1*V2, V1 */
1.320 brouard 1680: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.234 brouard 1681: /* Type */
1682: /* V 1 2 3 4 5 */
1683: /* F F V V V */
1684: /* D Q D D Q */
1685: /* */
1686: int *TvarsD;
1.330 brouard 1687: int *TnsdVar;
1.234 brouard 1688: int *TvarsDind;
1689: int *TvarsQ;
1690: int *TvarsQind;
1691:
1.318 brouard 1692: #define MAXRESULTLINESPONE 10+1
1.235 brouard 1693: int nresult=0;
1.258 brouard 1694: int parameterline=0; /* # of the parameter (type) line */
1.334 brouard 1695: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1696: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1697: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1698: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1.332 brouard 1699: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1700: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1.334 brouard 1701: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1.332 brouard 1702: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1.318 brouard 1703: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1.332 brouard 1704: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1.318 brouard 1705:
1706: /* ncovcol=1(Males=0 Females=1) nqv=1(raedyrs) ntv=2(withoutiadl=0 withiadl=1, witoutadl=0 withoutadl=1) nqtv=1(bmi) nlstate=3 ndeath=1
1707: # States 1=Coresidence, 2 Living alone, 3 Institution
1708: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1709: */
1.234 brouard 1710: /* int *TDvar; /\**< TDvar[1]=4, TDvarF[2]=3, TDvar[3]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
1.232 brouard 1711: int *TvarF; /**< TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1712: int *TvarFind; /**< TvarFind[1]=6, TvarFind[2]=7, Tvarind[3]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1713: int *TvarV; /**< TvarV[1]=Tvar[1]=5, TvarV[2]=Tvar[2]=4 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1714: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1715: int *TvarA; /**< TvarA[1]=Tvar[5]=5, TvarA[2]=Tvar[8]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1716: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 1717: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1718: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1719: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1720: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1721: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1722: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1723: int *TvarVQ; /* TvarVQ[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1724: int *TvarVQind; /* TvarVQind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1.339 brouard 1725: int *TvarVV; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1726: int *TvarVVind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1.349 brouard 1727: int *TvarVVA; /* We count ncovvt time varying covariates (single or products with age) and put their name into TvarVVA */
1728: int *TvarVVAind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1729: int *TvarAVVA; /* We count ALL ncovta time varying covariates (single or products with age) and put their name into TvarVVA */
1730: int *TvarAVVAind; /* We count ALL ncovta time varying covariates (single or products without age) and put their name into TvarVV */
1.339 brouard 1731: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1.349 brouard 1732: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age */
1733: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
1734: /* TvarVV={3,1,3,1,3}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1735: /* TvarVVind={2,5,5,6,6}, for V3 and then the product V1*V3 is decomposed into V1 and V3 and V1*V3*age into 6,6 */
1.230 brouard 1736: int *Tvarsel; /**< Selected covariates for output */
1737: double *Tvalsel; /**< Selected modality value of covariate for output */
1.349 brouard 1738: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 age*Vn*Vm */
1.227 brouard 1739: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1740: int *Dummy; /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.238 brouard 1741: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1742: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1743: int *Tage;
1.227 brouard 1744: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1745: int *Tmodelind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.230 brouard 1746: int *TmodelInvind; /** Tmodelind[Tvaraff[3]]=9 for V1 position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1747: int *TmodelInvQind; /** Tmodelqind[1]=1 for V5(quantitative varying) position,Tvaraff[1]@9={4, 3, 1, 0, 0, 0, 0, 0, 0}, model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.145 brouard 1748: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1749: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1750: int **Tvard;
1.330 brouard 1751: int **Tvardk;
1.227 brouard 1752: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1753: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1754: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1755: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1756: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1757: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1758: double *lsurv, *lpop, *tpop;
1759:
1.231 brouard 1760: #define FD 1; /* Fixed dummy covariate */
1761: #define FQ 2; /* Fixed quantitative covariate */
1762: #define FP 3; /* Fixed product covariate */
1763: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1764: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1765: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1766: #define VD 10; /* Varying dummy covariate */
1767: #define VQ 11; /* Varying quantitative covariate */
1768: #define VP 12; /* Varying product covariate */
1769: #define VPDD 13; /* Varying product dummy*dummy covariate */
1770: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1771: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1772: #define APFD 16; /* Age product * fixed dummy covariate */
1773: #define APFQ 17; /* Age product * fixed quantitative covariate */
1774: #define APVD 18; /* Age product * varying dummy covariate */
1775: #define APVQ 19; /* Age product * varying quantitative covariate */
1776:
1777: #define FTYPE 1; /* Fixed covariate */
1778: #define VTYPE 2; /* Varying covariate (loop in wave) */
1779: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1780:
1781: struct kmodel{
1782: int maintype; /* main type */
1783: int subtype; /* subtype */
1784: };
1785: struct kmodel modell[NCOVMAX];
1786:
1.143 brouard 1787: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1788: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1789:
1790: /**************** split *************************/
1791: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1792: {
1793: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1794: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1795: */
1796: char *ss; /* pointer */
1.186 brouard 1797: int l1=0, l2=0; /* length counters */
1.126 brouard 1798:
1799: l1 = strlen(path ); /* length of path */
1800: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1801: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1802: if ( ss == NULL ) { /* no directory, so determine current directory */
1803: strcpy( name, path ); /* we got the fullname name because no directory */
1804: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1805: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1806: /* get current working directory */
1807: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1808: #ifdef WIN32
1809: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1810: #else
1811: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1812: #endif
1.126 brouard 1813: return( GLOCK_ERROR_GETCWD );
1814: }
1815: /* got dirc from getcwd*/
1816: printf(" DIRC = %s \n",dirc);
1.205 brouard 1817: } else { /* strip directory from path */
1.126 brouard 1818: ss++; /* after this, the filename */
1819: l2 = strlen( ss ); /* length of filename */
1820: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1821: strcpy( name, ss ); /* save file name */
1822: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1823: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1824: printf(" DIRC2 = %s \n",dirc);
1825: }
1826: /* We add a separator at the end of dirc if not exists */
1827: l1 = strlen( dirc ); /* length of directory */
1828: if( dirc[l1-1] != DIRSEPARATOR ){
1829: dirc[l1] = DIRSEPARATOR;
1830: dirc[l1+1] = 0;
1831: printf(" DIRC3 = %s \n",dirc);
1832: }
1833: ss = strrchr( name, '.' ); /* find last / */
1834: if (ss >0){
1835: ss++;
1836: strcpy(ext,ss); /* save extension */
1837: l1= strlen( name);
1838: l2= strlen(ss)+1;
1839: strncpy( finame, name, l1-l2);
1840: finame[l1-l2]= 0;
1841: }
1842:
1843: return( 0 ); /* we're done */
1844: }
1845:
1846:
1847: /******************************************/
1848:
1849: void replace_back_to_slash(char *s, char*t)
1850: {
1851: int i;
1852: int lg=0;
1853: i=0;
1854: lg=strlen(t);
1855: for(i=0; i<= lg; i++) {
1856: (s[i] = t[i]);
1857: if (t[i]== '\\') s[i]='/';
1858: }
1859: }
1860:
1.132 brouard 1861: char *trimbb(char *out, char *in)
1.137 brouard 1862: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1863: char *s;
1864: s=out;
1865: while (*in != '\0'){
1.137 brouard 1866: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1867: in++;
1868: }
1869: *out++ = *in++;
1870: }
1871: *out='\0';
1872: return s;
1873: }
1874:
1.351 brouard 1875: char *trimbtab(char *out, char *in)
1876: { /* Trim blanks or tabs in line but keeps first blanks if line starts with blanks */
1877: char *s;
1878: s=out;
1879: while (*in != '\0'){
1880: while( (*in == ' ' || *in == '\t')){ /* && *(in+1) != '\0'){*/
1881: in++;
1882: }
1883: *out++ = *in++;
1884: }
1885: *out='\0';
1886: return s;
1887: }
1888:
1.187 brouard 1889: /* char *substrchaine(char *out, char *in, char *chain) */
1890: /* { */
1891: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1892: /* char *s, *t; */
1893: /* t=in;s=out; */
1894: /* while ((*in != *chain) && (*in != '\0')){ */
1895: /* *out++ = *in++; */
1896: /* } */
1897:
1898: /* /\* *in matches *chain *\/ */
1899: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1900: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1901: /* } */
1902: /* in--; chain--; */
1903: /* while ( (*in != '\0')){ */
1904: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1905: /* *out++ = *in++; */
1906: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1907: /* } */
1908: /* *out='\0'; */
1909: /* out=s; */
1910: /* return out; */
1911: /* } */
1912: char *substrchaine(char *out, char *in, char *chain)
1913: {
1914: /* Substract chain 'chain' from 'in', return and output 'out' */
1.349 brouard 1915: /* in="V1+V1*age+age*age+V2", chain="+age*age" out="V1+V1*age+V2" */
1.187 brouard 1916:
1917: char *strloc;
1918:
1.349 brouard 1919: strcpy (out, in); /* out="V1+V1*age+age*age+V2" */
1920: strloc = strstr(out, chain); /* strloc points to out at "+age*age+V2" */
1921: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out); /* strloc=+age*age+V2 chain="+age*age", out="V1+V1*age+age*age+V2" */
1.187 brouard 1922: if(strloc != NULL){
1.349 brouard 1923: /* will affect out */ /* strloc+strlen(chain)=|+V2 = "V1+V1*age+age*age|+V2" */ /* Will also work in Unicodek */
1924: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1); /* move number of bytes corresponding to the length of "+V2" which is 3, plus one is 4 (including the null)*/
1925: /* equivalent to strcpy (strloc, strloc +strlen(chain)) if no overlap; Copies from "+V2" to V1+V1*age+ */
1.187 brouard 1926: }
1.349 brouard 1927: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out); /* strloc=+V2 chain="+age*age", in="V1+V1*age+age*age+V2", out="V1+V1*age+V2" */
1.187 brouard 1928: return out;
1929: }
1930:
1931:
1.145 brouard 1932: char *cutl(char *blocc, char *alocc, char *in, char occ)
1933: {
1.187 brouard 1934: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.349 brouard 1935: and alocc starts after first occurence of char 'occ' : ex cutl(blocc,alocc,"abcdef2ghi2j",'2')
1.310 brouard 1936: gives alocc="abcdef" and blocc="ghi2j".
1.145 brouard 1937: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1938: */
1.160 brouard 1939: char *s, *t;
1.145 brouard 1940: t=in;s=in;
1941: while ((*in != occ) && (*in != '\0')){
1942: *alocc++ = *in++;
1943: }
1944: if( *in == occ){
1945: *(alocc)='\0';
1946: s=++in;
1947: }
1948:
1949: if (s == t) {/* occ not found */
1950: *(alocc-(in-s))='\0';
1951: in=s;
1952: }
1953: while ( *in != '\0'){
1954: *blocc++ = *in++;
1955: }
1956:
1957: *blocc='\0';
1958: return t;
1959: }
1.137 brouard 1960: char *cutv(char *blocc, char *alocc, char *in, char occ)
1961: {
1.187 brouard 1962: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1963: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1964: gives blocc="abcdef2ghi" and alocc="j".
1965: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1966: */
1967: char *s, *t;
1968: t=in;s=in;
1969: while (*in != '\0'){
1970: while( *in == occ){
1971: *blocc++ = *in++;
1972: s=in;
1973: }
1974: *blocc++ = *in++;
1975: }
1976: if (s == t) /* occ not found */
1977: *(blocc-(in-s))='\0';
1978: else
1979: *(blocc-(in-s)-1)='\0';
1980: in=s;
1981: while ( *in != '\0'){
1982: *alocc++ = *in++;
1983: }
1984:
1985: *alocc='\0';
1986: return s;
1987: }
1988:
1.126 brouard 1989: int nbocc(char *s, char occ)
1990: {
1991: int i,j=0;
1992: int lg=20;
1993: i=0;
1994: lg=strlen(s);
1995: for(i=0; i<= lg; i++) {
1.234 brouard 1996: if (s[i] == occ ) j++;
1.126 brouard 1997: }
1998: return j;
1999: }
2000:
1.349 brouard 2001: int nboccstr(char *textin, char *chain)
2002: {
2003: /* Counts the number of occurence of "chain" in string textin */
2004: /* in="+V7*V4+age*V2+age*V3+age*V4" chain="age" */
2005: char *strloc;
2006:
1.366 ! brouard 2007: int j=0;
1.349 brouard 2008:
2009: strloc=textin; /* strloc points to "^+V7*V4+age+..." in textin */
2010: for(;;) {
2011: strloc= strstr(strloc,chain); /* strloc points to first character of chain in textin if found. Example strloc points^ to "+V7*V4+^age" in textin */
2012: if(strloc != NULL){
2013: strloc = strloc+strlen(chain); /* strloc points to "+V7*V4+age^" in textin */
2014: j++;
2015: }else
2016: break;
2017: }
2018: return j;
2019:
2020: }
1.137 brouard 2021: /* void cutv(char *u,char *v, char*t, char occ) */
2022: /* { */
2023: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
2024: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
2025: /* gives u="abcdef2ghi" and v="j" *\/ */
2026: /* int i,lg,j,p=0; */
2027: /* i=0; */
2028: /* lg=strlen(t); */
2029: /* for(j=0; j<=lg-1; j++) { */
2030: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
2031: /* } */
1.126 brouard 2032:
1.137 brouard 2033: /* for(j=0; j<p; j++) { */
2034: /* (u[j] = t[j]); */
2035: /* } */
2036: /* u[p]='\0'; */
1.126 brouard 2037:
1.137 brouard 2038: /* for(j=0; j<= lg; j++) { */
2039: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
2040: /* } */
2041: /* } */
1.126 brouard 2042:
1.160 brouard 2043: #ifdef _WIN32
2044: char * strsep(char **pp, const char *delim)
2045: {
2046: char *p, *q;
2047:
2048: if ((p = *pp) == NULL)
2049: return 0;
2050: if ((q = strpbrk (p, delim)) != NULL)
2051: {
2052: *pp = q + 1;
2053: *q = '\0';
2054: }
2055: else
2056: *pp = 0;
2057: return p;
2058: }
2059: #endif
2060:
1.126 brouard 2061: /********************** nrerror ********************/
2062:
2063: void nrerror(char error_text[])
2064: {
2065: fprintf(stderr,"ERREUR ...\n");
2066: fprintf(stderr,"%s\n",error_text);
2067: exit(EXIT_FAILURE);
2068: }
2069: /*********************** vector *******************/
2070: double *vector(int nl, int nh)
2071: {
2072: double *v;
2073: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
2074: if (!v) nrerror("allocation failure in vector");
2075: return v-nl+NR_END;
2076: }
2077:
2078: /************************ free vector ******************/
2079: void free_vector(double*v, int nl, int nh)
2080: {
2081: free((FREE_ARG)(v+nl-NR_END));
2082: }
2083:
2084: /************************ivector *******************************/
2085: int *ivector(long nl,long nh)
2086: {
2087: int *v;
2088: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
2089: if (!v) nrerror("allocation failure in ivector");
2090: return v-nl+NR_END;
2091: }
2092:
2093: /******************free ivector **************************/
2094: void free_ivector(int *v, long nl, long nh)
2095: {
2096: free((FREE_ARG)(v+nl-NR_END));
2097: }
2098:
2099: /************************lvector *******************************/
2100: long *lvector(long nl,long nh)
2101: {
2102: long *v;
2103: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
2104: if (!v) nrerror("allocation failure in ivector");
2105: return v-nl+NR_END;
2106: }
2107:
2108: /******************free lvector **************************/
2109: void free_lvector(long *v, long nl, long nh)
2110: {
2111: free((FREE_ARG)(v+nl-NR_END));
2112: }
2113:
2114: /******************* imatrix *******************************/
2115: int **imatrix(long nrl, long nrh, long ncl, long nch)
2116: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
2117: {
2118: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
2119: int **m;
2120:
2121: /* allocate pointers to rows */
2122: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
2123: if (!m) nrerror("allocation failure 1 in matrix()");
2124: m += NR_END;
2125: m -= nrl;
2126:
2127:
2128: /* allocate rows and set pointers to them */
2129: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
2130: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2131: m[nrl] += NR_END;
2132: m[nrl] -= ncl;
2133:
2134: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
2135:
2136: /* return pointer to array of pointers to rows */
2137: return m;
2138: }
2139:
2140: /****************** free_imatrix *************************/
1.366 ! brouard 2141: /* void free_imatrix(m,nrl,nrh,ncl,nch); */
! 2142: /* int **m; */
! 2143: /* long nch,ncl,nrh,nrl; */
! 2144: void free_imatrix(int **m,long nrl, long nrh, long ncl, long nch)
! 2145: /* free an int matrix allocated by imatrix() */
! 2146: {
! 2147: free((FREE_ARG) (m[nrl]+ncl-NR_END));
! 2148: free((FREE_ARG) (m+nrl-NR_END));
! 2149: }
1.126 brouard 2150:
2151: /******************* matrix *******************************/
2152: double **matrix(long nrl, long nrh, long ncl, long nch)
2153: {
2154: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
2155: double **m;
2156:
2157: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2158: if (!m) nrerror("allocation failure 1 in matrix()");
2159: m += NR_END;
2160: m -= nrl;
2161:
2162: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2163: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2164: m[nrl] += NR_END;
2165: m[nrl] -= ncl;
2166:
2167: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2168: return m;
1.145 brouard 2169: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
2170: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
2171: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 2172: */
2173: }
2174:
2175: /*************************free matrix ************************/
2176: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
2177: {
2178: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2179: free((FREE_ARG)(m+nrl-NR_END));
2180: }
2181:
2182: /******************* ma3x *******************************/
2183: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
2184: {
2185: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
2186: double ***m;
2187:
2188: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2189: if (!m) nrerror("allocation failure 1 in matrix()");
2190: m += NR_END;
2191: m -= nrl;
2192:
2193: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2194: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2195: m[nrl] += NR_END;
2196: m[nrl] -= ncl;
2197:
2198: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2199:
2200: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2201: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2202: m[nrl][ncl] += NR_END;
2203: m[nrl][ncl] -= nll;
2204: for (j=ncl+1; j<=nch; j++)
2205: m[nrl][j]=m[nrl][j-1]+nlay;
2206:
2207: for (i=nrl+1; i<=nrh; i++) {
2208: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2209: for (j=ncl+1; j<=nch; j++)
2210: m[i][j]=m[i][j-1]+nlay;
2211: }
2212: return m;
2213: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2214: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2215: */
2216: }
2217:
2218: /*************************free ma3x ************************/
2219: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2220: {
2221: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2222: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2223: free((FREE_ARG)(m+nrl-NR_END));
2224: }
2225:
2226: /*************** function subdirf ***********/
2227: char *subdirf(char fileres[])
2228: {
2229: /* Caution optionfilefiname is hidden */
2230: strcpy(tmpout,optionfilefiname);
2231: strcat(tmpout,"/"); /* Add to the right */
2232: strcat(tmpout,fileres);
2233: return tmpout;
2234: }
2235:
2236: /*************** function subdirf2 ***********/
2237: char *subdirf2(char fileres[], char *preop)
2238: {
1.314 brouard 2239: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2240: Errors in subdirf, 2, 3 while printing tmpout is
1.315 brouard 2241: rewritten within the same printf. Workaround: many printfs */
1.126 brouard 2242: /* Caution optionfilefiname is hidden */
2243: strcpy(tmpout,optionfilefiname);
2244: strcat(tmpout,"/");
2245: strcat(tmpout,preop);
2246: strcat(tmpout,fileres);
2247: return tmpout;
2248: }
2249:
2250: /*************** function subdirf3 ***********/
2251: char *subdirf3(char fileres[], char *preop, char *preop2)
2252: {
2253:
2254: /* Caution optionfilefiname is hidden */
2255: strcpy(tmpout,optionfilefiname);
2256: strcat(tmpout,"/");
2257: strcat(tmpout,preop);
2258: strcat(tmpout,preop2);
2259: strcat(tmpout,fileres);
2260: return tmpout;
2261: }
1.213 brouard 2262:
2263: /*************** function subdirfext ***********/
2264: char *subdirfext(char fileres[], char *preop, char *postop)
2265: {
2266:
2267: strcpy(tmpout,preop);
2268: strcat(tmpout,fileres);
2269: strcat(tmpout,postop);
2270: return tmpout;
2271: }
1.126 brouard 2272:
1.213 brouard 2273: /*************** function subdirfext3 ***********/
2274: char *subdirfext3(char fileres[], char *preop, char *postop)
2275: {
2276:
2277: /* Caution optionfilefiname is hidden */
2278: strcpy(tmpout,optionfilefiname);
2279: strcat(tmpout,"/");
2280: strcat(tmpout,preop);
2281: strcat(tmpout,fileres);
2282: strcat(tmpout,postop);
2283: return tmpout;
2284: }
2285:
1.162 brouard 2286: char *asc_diff_time(long time_sec, char ascdiff[])
2287: {
2288: long sec_left, days, hours, minutes;
2289: days = (time_sec) / (60*60*24);
2290: sec_left = (time_sec) % (60*60*24);
2291: hours = (sec_left) / (60*60) ;
2292: sec_left = (sec_left) %(60*60);
2293: minutes = (sec_left) /60;
2294: sec_left = (sec_left) % (60);
2295: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2296: return ascdiff;
2297: }
2298:
1.126 brouard 2299: /***************** f1dim *************************/
2300: extern int ncom;
2301: extern double *pcom,*xicom;
2302: extern double (*nrfunc)(double []);
2303:
2304: double f1dim(double x)
2305: {
2306: int j;
2307: double f;
2308: double *xt;
2309:
2310: xt=vector(1,ncom);
2311: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2312: f=(*nrfunc)(xt);
2313: free_vector(xt,1,ncom);
2314: return f;
2315: }
2316:
2317: /*****************brent *************************/
2318: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 2319: {
2320: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2321: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2322: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2323: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2324: * returned function value.
2325: */
1.126 brouard 2326: int iter;
2327: double a,b,d,etemp;
1.159 brouard 2328: double fu=0,fv,fw,fx;
1.164 brouard 2329: double ftemp=0.;
1.126 brouard 2330: double p,q,r,tol1,tol2,u,v,w,x,xm;
2331: double e=0.0;
2332:
2333: a=(ax < cx ? ax : cx);
2334: b=(ax > cx ? ax : cx);
2335: x=w=v=bx;
2336: fw=fv=fx=(*f)(x);
2337: for (iter=1;iter<=ITMAX;iter++) {
2338: xm=0.5*(a+b);
2339: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2340: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2341: printf(".");fflush(stdout);
2342: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 2343: #ifdef DEBUGBRENT
1.126 brouard 2344: printf("br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
2345: fprintf(ficlog,"br %d,x=%.10e xm=%.10e b=%.10e a=%.10e tol=%.10e tol1=%.10e tol2=%.10e x-xm=%.10e fx=%.12e fu=%.12e,fw=%.12e,ftemp=%.12e,ftol=%.12e\n",iter,x,xm,b,a,tol,tol1,tol2,(x-xm),fx,fu,fw,ftemp,ftol);
2346: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2347: #endif
2348: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2349: *xmin=x;
2350: return fx;
2351: }
2352: ftemp=fu;
2353: if (fabs(e) > tol1) {
2354: r=(x-w)*(fx-fv);
2355: q=(x-v)*(fx-fw);
2356: p=(x-v)*q-(x-w)*r;
2357: q=2.0*(q-r);
2358: if (q > 0.0) p = -p;
2359: q=fabs(q);
2360: etemp=e;
2361: e=d;
2362: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 2363: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 2364: else {
1.224 brouard 2365: d=p/q;
2366: u=x+d;
2367: if (u-a < tol2 || b-u < tol2)
2368: d=SIGN(tol1,xm-x);
1.126 brouard 2369: }
2370: } else {
2371: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2372: }
2373: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2374: fu=(*f)(u);
2375: if (fu <= fx) {
2376: if (u >= x) a=x; else b=x;
2377: SHFT(v,w,x,u)
1.183 brouard 2378: SHFT(fv,fw,fx,fu)
2379: } else {
2380: if (u < x) a=u; else b=u;
2381: if (fu <= fw || w == x) {
1.224 brouard 2382: v=w;
2383: w=u;
2384: fv=fw;
2385: fw=fu;
1.183 brouard 2386: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 2387: v=u;
2388: fv=fu;
1.183 brouard 2389: }
2390: }
1.126 brouard 2391: }
2392: nrerror("Too many iterations in brent");
2393: *xmin=x;
2394: return fx;
2395: }
2396:
2397: /****************** mnbrak ***********************/
2398:
2399: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2400: double (*func)(double))
1.183 brouard 2401: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2402: the downhill direction (defined by the function as evaluated at the initial points) and returns
2403: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2404: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2405: */
1.126 brouard 2406: double ulim,u,r,q, dum;
2407: double fu;
1.187 brouard 2408:
1.366 ! brouard 2409: /* double scale=10.; */
! 2410: /* int iterscale=0; */
1.187 brouard 2411:
2412: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2413: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2414:
2415:
2416: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2417: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2418: /* *bx = *ax - (*ax - *bx)/scale; */
2419: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2420: /* } */
2421:
1.126 brouard 2422: if (*fb > *fa) {
2423: SHFT(dum,*ax,*bx,dum)
1.183 brouard 2424: SHFT(dum,*fb,*fa,dum)
2425: }
1.126 brouard 2426: *cx=(*bx)+GOLD*(*bx-*ax);
2427: *fc=(*func)(*cx);
1.183 brouard 2428: #ifdef DEBUG
1.224 brouard 2429: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2430: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1.183 brouard 2431: #endif
1.224 brouard 2432: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
1.126 brouard 2433: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 2434: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 2435: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 2436: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2437: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2438: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 2439: fu=(*func)(u);
1.163 brouard 2440: #ifdef DEBUG
2441: /* f(x)=A(x-u)**2+f(u) */
2442: double A, fparabu;
2443: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2444: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 2445: printf("\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
2446: fprintf(ficlog,"\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1.183 brouard 2447: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2448: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2449: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2450: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 2451: #endif
1.184 brouard 2452: #ifdef MNBRAKORIGINAL
1.183 brouard 2453: #else
1.191 brouard 2454: /* if (fu > *fc) { */
2455: /* #ifdef DEBUG */
2456: /* printf("mnbrak4 fu > fc \n"); */
2457: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2458: /* #endif */
2459: /* /\* SHFT(u,*cx,*cx,u) /\\* ie a=c, c=u and u=c; in that case, next SHFT(a,b,c,u) will give a=b=b, b=c=u, c=u=c and *\\/ *\/ */
2460: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2461: /* dum=u; /\* Shifting c and u *\/ */
2462: /* u = *cx; */
2463: /* *cx = dum; */
2464: /* dum = fu; */
2465: /* fu = *fc; */
2466: /* *fc =dum; */
2467: /* } else { /\* end *\/ */
2468: /* #ifdef DEBUG */
2469: /* printf("mnbrak3 fu < fc \n"); */
2470: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2471: /* #endif */
2472: /* dum=u; /\* Shifting c and u *\/ */
2473: /* u = *cx; */
2474: /* *cx = dum; */
2475: /* dum = fu; */
2476: /* fu = *fc; */
2477: /* *fc =dum; */
2478: /* } */
1.224 brouard 2479: #ifdef DEBUGMNBRAK
2480: double A, fparabu;
2481: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2482: fparabu= *fa - A*(*ax-u)*(*ax-u);
2483: printf("\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
2484: fprintf(ficlog,"\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1.183 brouard 2485: #endif
1.191 brouard 2486: dum=u; /* Shifting c and u */
2487: u = *cx;
2488: *cx = dum;
2489: dum = fu;
2490: fu = *fc;
2491: *fc =dum;
1.183 brouard 2492: #endif
1.162 brouard 2493: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 2494: #ifdef DEBUG
1.224 brouard 2495: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2496: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 2497: #endif
1.126 brouard 2498: fu=(*func)(u);
2499: if (fu < *fc) {
1.183 brouard 2500: #ifdef DEBUG
1.224 brouard 2501: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2502: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2503: #endif
2504: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2505: SHFT(*fb,*fc,fu,(*func)(u))
2506: #ifdef DEBUG
2507: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 2508: #endif
2509: }
1.162 brouard 2510: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 2511: #ifdef DEBUG
1.224 brouard 2512: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2513: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 2514: #endif
1.126 brouard 2515: u=ulim;
2516: fu=(*func)(u);
1.183 brouard 2517: } else { /* u could be left to b (if r > q parabola has a maximum) */
2518: #ifdef DEBUG
1.224 brouard 2519: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2520: fprintf(ficlog,"\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1.183 brouard 2521: #endif
1.126 brouard 2522: u=(*cx)+GOLD*(*cx-*bx);
2523: fu=(*func)(u);
1.224 brouard 2524: #ifdef DEBUG
2525: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2526: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2527: #endif
1.183 brouard 2528: } /* end tests */
1.126 brouard 2529: SHFT(*ax,*bx,*cx,u)
1.183 brouard 2530: SHFT(*fa,*fb,*fc,fu)
2531: #ifdef DEBUG
1.224 brouard 2532: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2533: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1.183 brouard 2534: #endif
2535: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
1.126 brouard 2536: }
2537:
2538: /*************** linmin ************************/
1.162 brouard 2539: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2540: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2541: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2542: the value of func at the returned location p . This is actually all accomplished by calling the
2543: routines mnbrak and brent .*/
1.126 brouard 2544: int ncom;
2545: double *pcom,*xicom;
2546: double (*nrfunc)(double []);
2547:
1.224 brouard 2548: #ifdef LINMINORIGINAL
1.126 brouard 2549: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 2550: #else
2551: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2552: #endif
1.126 brouard 2553: {
2554: double brent(double ax, double bx, double cx,
2555: double (*f)(double), double tol, double *xmin);
2556: double f1dim(double x);
2557: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2558: double *fc, double (*func)(double));
2559: int j;
2560: double xx,xmin,bx,ax;
2561: double fx,fb,fa;
1.187 brouard 2562:
1.203 brouard 2563: #ifdef LINMINORIGINAL
2564: #else
2565: double scale=10., axs, xxs; /* Scale added for infinity */
2566: #endif
2567:
1.126 brouard 2568: ncom=n;
2569: pcom=vector(1,n);
2570: xicom=vector(1,n);
2571: nrfunc=func;
2572: for (j=1;j<=n;j++) {
2573: pcom[j]=p[j];
1.202 brouard 2574: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 2575: }
1.187 brouard 2576:
1.203 brouard 2577: #ifdef LINMINORIGINAL
2578: xx=1.;
2579: #else
2580: axs=0.0;
2581: xxs=1.;
2582: do{
2583: xx= xxs;
2584: #endif
1.187 brouard 2585: ax=0.;
2586: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2587: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2588: /* xt[x,j]=pcom[j]+x*xicom[j] f(ax) = f(xt(a,j=1,n)) = f(p(j) + 0 * xi(j)) and f(xx) = f(xt(x, j=1,n)) = f(p(j) + 1 * xi(j)) */
2589: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2590: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2591: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2592: /* Find a bracket a,x,b in direction n=xi ie xicom, order may change. Scale is [0:xxs*xi[j]] et non plus [0:xi[j]]*/
1.203 brouard 2593: #ifdef LINMINORIGINAL
2594: #else
2595: if (fx != fx){
1.224 brouard 2596: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2597: printf("|");
2598: fprintf(ficlog,"|");
1.203 brouard 2599: #ifdef DEBUGLINMIN
1.224 brouard 2600: printf("\nLinmin NAN : input [axs=%lf:xxs=%lf], mnbrak outputs fx=%lf <(fb=%lf and fa=%lf) with xx=%lf in [ax=%lf:bx=%lf] \n", axs, xxs, fx,fb, fa, xx, ax, bx);
1.203 brouard 2601: #endif
2602: }
1.224 brouard 2603: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 2604: #endif
2605:
1.191 brouard 2606: #ifdef DEBUGLINMIN
2607: printf("\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.202 brouard 2608: fprintf(ficlog,"\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.191 brouard 2609: #endif
1.224 brouard 2610: #ifdef LINMINORIGINAL
2611: #else
1.317 brouard 2612: if(fb == fx){ /* Flat function in the direction */
2613: xmin=xx;
1.224 brouard 2614: *flat=1;
1.317 brouard 2615: }else{
1.224 brouard 2616: *flat=0;
2617: #endif
2618: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2619: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2620: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2621: /* fmin = f(p[j] + xmin * xi[j]) */
2622: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2623: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2624: #ifdef DEBUG
1.224 brouard 2625: printf("retour brent from bracket (a=%lf fa=%lf, xx=%lf fx=%lf, b=%lf fb=%lf): fret=%lf xmin=%lf\n",ax,fa,xx,fx,bx,fb,*fret,xmin);
2626: fprintf(ficlog,"retour brent from bracket (a=%lf fa=%lf, xx=%lf fx=%lf, b=%lf fb=%lf): fret=%lf xmin=%lf\n",ax,fa,xx,fx,bx,fb,*fret,xmin);
2627: #endif
2628: #ifdef LINMINORIGINAL
2629: #else
2630: }
1.126 brouard 2631: #endif
1.191 brouard 2632: #ifdef DEBUGLINMIN
2633: printf("linmin end ");
1.202 brouard 2634: fprintf(ficlog,"linmin end ");
1.191 brouard 2635: #endif
1.126 brouard 2636: for (j=1;j<=n;j++) {
1.203 brouard 2637: #ifdef LINMINORIGINAL
2638: xi[j] *= xmin;
2639: #else
2640: #ifdef DEBUGLINMIN
2641: if(xxs <1.0)
2642: printf(" before xi[%d]=%12.8f", j,xi[j]);
2643: #endif
2644: xi[j] *= xmin*xxs; /* xi rescaled by xmin and number of loops: if xmin=-1.237 and xi=(1,0,...,0) xi=(-1.237,0,...,0) */
2645: #ifdef DEBUGLINMIN
2646: if(xxs <1.0)
2647: printf(" after xi[%d]=%12.8f, xmin=%12.8f, ax=%12.8f, xx=%12.8f, bx=%12.8f, xxs=%12.8f", j,xi[j], xmin, ax, xx, bx,xxs );
2648: #endif
2649: #endif
1.187 brouard 2650: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2651: }
1.191 brouard 2652: #ifdef DEBUGLINMIN
1.203 brouard 2653: printf("\n");
1.191 brouard 2654: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2655: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.191 brouard 2656: for (j=1;j<=n;j++) {
1.202 brouard 2657: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2658: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2659: if(j % ncovmodel == 0){
1.191 brouard 2660: printf("\n");
1.202 brouard 2661: fprintf(ficlog,"\n");
2662: }
1.191 brouard 2663: }
1.203 brouard 2664: #else
1.191 brouard 2665: #endif
1.126 brouard 2666: free_vector(xicom,1,n);
2667: free_vector(pcom,1,n);
2668: }
2669:
1.359 brouard 2670: /**** praxis gegen ****/
2671:
2672: /* This has been tested by Visual C from Microsoft and works */
2673: /* meaning tha valgrind could be wrong */
2674: /*********************************************************************/
2675: /* f u n c t i o n p r a x i s */
2676: /* */
2677: /* praxis is a general purpose routine for the minimization of a */
2678: /* function in several variables. the algorithm used is a modifi- */
2679: /* cation of conjugate gradient search method by powell. the changes */
2680: /* are due to r.p. brent, who gives an algol-w program, which served */
2681: /* as a basis for this function. */
2682: /* */
2683: /* references: */
2684: /* - powell, m.j.d., 1964. an efficient method for finding */
2685: /* the minimum of a function in several variables without */
2686: /* calculating derivatives, computer journal, 7, 155-162 */
2687: /* - brent, r.p., 1973. algorithms for minimization without */
2688: /* derivatives, prentice hall, englewood cliffs. */
2689: /* */
2690: /* problems, suggestions or improvements are always wellcome */
2691: /* karl gegenfurtner 07/08/87 */
2692: /* c - version */
2693: /*********************************************************************/
2694: /* */
2695: /* usage: min = praxis(tol, macheps, h, n, prin, x, func) */
2696: /* macheps has been suppressed because it is replaced by DBL_EPSILON */
2697: /* and if it was an argument of praxis (as it is in original brent) */
2698: /* it should be declared external */
2699: /* usage: min = praxis(tol, h, n, prin, x, func) */
2700: /* was min = praxis(fun, x, n); */
2701: /* */
2702: /* fun the function to be minimized. fun is called from */
2703: /* praxis with x and n as arguments */
2704: /* x a double array containing the initial guesses for */
2705: /* the minimum, which will contain the solution on */
2706: /* return */
2707: /* n an integer specifying the number of unknown */
2708: /* parameters */
2709: /* min praxis returns the least calculated value of fun */
2710: /* */
2711: /* some additional global variables control some more aspects of */
2712: /* the inner workings of praxis. setting them is optional, they */
2713: /* are all set to some reasonable default values given below. */
2714: /* */
2715: /* prin controls the printed output from the routine. */
2716: /* 0 -> no output */
2717: /* 1 -> print only starting and final values */
2718: /* 2 -> detailed map of the minimization process */
2719: /* 3 -> print also eigenvalues and vectors of the */
2720: /* search directions */
2721: /* the default value is 1 */
2722: /* tol is the tolerance allowed for the precision of the */
2723: /* solution. praxis returns if the criterion */
2724: /* 2 * ||x[k]-x[k-1]|| <= sqrt(macheps) * ||x[k]|| + tol */
2725: /* is fulfilled more than ktm times. */
2726: /* the default value depends on the machine precision */
2727: /* ktm see just above. default is 1, and a value of 4 leads */
2728: /* to a very(!) cautious stopping criterion. */
2729: /* h0 or step is a steplength parameter and should be set equal */
2730: /* to the expected distance from the solution. */
2731: /* exceptionally small or large values of step lead to */
2732: /* slower convergence on the first few iterations */
2733: /* the default value for step is 1.0 */
2734: /* scbd is a scaling parameter. 1.0 is the default and */
2735: /* indicates no scaling. if the scales for the different */
2736: /* parameters are very different, scbd should be set to */
2737: /* a value of about 10.0. */
2738: /* illc should be set to true (1) if the problem is known to */
2739: /* be ill-conditioned. the default is false (0). this */
2740: /* variable is automatically set, when praxis finds */
2741: /* the problem to be ill-conditioned during iterations. */
2742: /* maxfun is the maximum number of calls to fun allowed. praxis */
2743: /* will return after maxfun calls to fun even when the */
2744: /* minimum is not yet found. the default value of 0 */
2745: /* indicates no limit on the number of calls. */
2746: /* this return condition is only checked every n */
2747: /* iterations. */
2748: /* */
2749: /*********************************************************************/
2750:
2751: #include <math.h>
2752: #include <stdio.h>
2753: #include <stdlib.h>
2754: #include <float.h> /* for DBL_EPSILON */
2755: /* #include "machine.h" */
2756:
2757:
2758: /* extern void minfit(int n, double eps, double tol, double **ab, double q[]); */
2759: /* extern void minfit(int n, double eps, double tol, double ab[N][N], double q[]); */
2760: /* control parameters */
2761: /* control parameters */
2762: #define SQREPSILON 1.0e-19
2763: /* #define EPSILON 1.0e-8 */ /* in main */
2764:
2765: double tol = SQREPSILON,
2766: scbd = 1.0,
2767: step = 1.0;
2768: int ktm = 1,
2769: /* prin = 2, */
2770: maxfun = 0,
2771: illc = 0;
2772:
2773: /* some global variables */
2774: static int i, j, k, k2, nl, nf, kl, kt;
2775: /* static double s; */
2776: double sl, dn, dmin,
2777: fx, f1, lds, ldt, sf, df,
2778: qf1, qd0, qd1, qa, qb, qc,
2779: m2, m4, small_windows, vsmall, large,
2780: vlarge, ldfac, t2;
2781: /* static double d[N], y[N], z[N], */
2782: /* q0[N], q1[N], v[N][N]; */
2783:
2784: static double *d, *y, *z;
2785: static double *q0, *q1, **v;
2786: double *tflin; /* used in flin: return (*fun)(tflin, n); */
2787: double *e; /* used in minfit, don't konw how to free memory and thus made global */
2788: /* static double s, sl, dn, dmin, */
2789: /* fx, f1, lds, ldt, sf, df, */
2790: /* qf1, qd0, qd1, qa, qb, qc, */
2791: /* m2, m4, small, vsmall, large, */
2792: /* vlarge, ldfac, t2; */
2793: /* static double d[N], y[N], z[N], */
2794: /* q0[N], q1[N], v[N][N]; */
2795:
2796: /* these will be set by praxis to point to it's arguments */
2797: static int prin; /* added */
2798: static int n;
2799: static double *x;
1.366 ! brouard 2800: static double (*fun)(double *x); /* New for clang */
! 2801: /* static double (*fun)(); */
1.359 brouard 2802: /* static double (*fun)(double *x, int n); */
2803:
2804: /* these will be set by praxis to the global control parameters */
2805: /* static double h, macheps, t; */
2806: extern double macheps;
2807: static double h;
2808: static double t;
2809:
2810: static double
2811: drandom() /* return random no between 0 and 1 */
2812: {
2813: return (double)(rand()%(8192*2))/(double)(8192*2);
2814: }
2815:
2816: static void sort() /* d and v in descending order */
2817: {
2818: int k, i, j;
2819: double s;
2820:
2821: for (i=1; i<=n-1; i++) {
2822: k = i; s = d[i];
2823: for (j=i+1; j<=n; j++) {
2824: if (d[j] > s) {
2825: k = j;
2826: s = d[j];
2827: }
2828: }
2829: if (k > i) {
2830: d[k] = d[i];
2831: d[i] = s;
2832: for (j=1; j<=n; j++) {
2833: s = v[j][i];
2834: v[j][i] = v[j][k];
2835: v[j][k] = s;
2836: }
2837: }
2838: }
2839: }
2840:
2841: double randbrent ( int *naught )
2842: {
2843: double ran1, ran3[127], half;
2844: int ran2, q, r, i, j;
2845: int init=0; /* false */
2846: double rr;
2847: /* REAL*8 RAN1,RAN3(127),HALF */
2848:
2849: /* INTEGER RAN2,Q,R */
2850: /* LOGICAL INIT */
2851: /* DATA INIT/.FALSE./ */
2852: /* IF (INIT) GO TO 3 */
2853: if(!init){
2854: /* R = MOD(NAUGHT,8190) + 1 *//* 1804289383 rand () */
2855: r = *naught % 8190 + 1;/* printf(" naught r %d %d",*naught,r); */
2856: ran2=127;
2857: for(i=ran2; i>0; i--){
2858: /* RAN2 = 128 */
2859: /* DO 2 I=1,127 */
2860: ran2 = ran2-1;
2861: /* RAN2 = RAN2 - 1 */
2862: ran1 = -pow(2.0,55);
2863: /* RAN1 = -2.D0**55 */
2864: /* DO 1 J=1,7 */
2865: for(j=1; j<=7;j++){
2866: /* R = MOD(1756*R,8191) */
2867: r = (1756*r) % 8191;/* printf(" i=%d (1756*r)%8191=%d",j,r); */
2868: q=r/32;
2869: /* Q = R/32 */
2870: /* 1 RAN1 = (RAN1 + Q)*(1.0D0/256) */
2871: ran1 =(ran1+q)*(1.0/256);
2872: }
2873: /* 2 RAN3(RAN2) = RAN1 */
2874: ran3[ran2] = ran1; /* printf(" ran2=%d ran1=%.7g \n",ran2,ran1); */
2875: }
2876: /* INIT = .TRUE. */
2877: init=1;
2878: /* 3 IF (RAN2.EQ.1) RAN2 = 128 */
2879: }
2880: if(ran2 == 0) ran2 = 126;
2881: else ran2 = ran2 -1;
2882: /* RAN2 = RAN2 - 1 */
2883: /* RAN1 = RAN1 + RAN3(RAN2) */
2884: ran1 = ran1 + ran3[ran2];/* printf("BIS ran2=%d ran1=%.7g \n",ran2,ran1); */
2885: half= 0.5;
2886: /* HALF = .5D0 */
2887: /* IF (RAN1.GE.0.D0) HALF = -HALF */
2888: if(ran1 >= 0.) half =-half;
2889: ran1 = ran1 +half;
2890: ran3[ran2] = ran1;
2891: rr= ran1+0.5;
2892: /* RAN1 = RAN1 + HALF */
2893: /* RAN3(RAN2) = RAN1 */
2894: /* RANDOM = RAN1 + .5D0 */
2895: /* r = ( ( double ) ( *seed ) ) * 4.656612875E-10; */
2896: return rr;
2897: }
2898: static void matprint(char *s, double **v, int m, int n)
2899: /* char *s; */
2900: /* double v[N][N]; */
2901: {
2902: #define INCX 8
2903: int i;
2904:
2905: int i2hi;
2906: int ihi;
2907: int ilo;
2908: int i2lo;
2909: int jlo=1;
2910: int j;
2911: int j2hi;
2912: int jhi;
2913: int j2lo;
2914: ilo=1;
2915: ihi=n;
2916: jlo=1;
2917: jhi=n;
2918:
2919: printf ("\n" );
2920: printf ("%s\n", s );
2921: for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
2922: {
2923: j2hi = j2lo + INCX - 1;
2924: if ( n < j2hi )
2925: {
2926: j2hi = n;
2927: }
2928: if ( jhi < j2hi )
2929: {
2930: j2hi = jhi;
2931: }
2932:
2933: /* fprintf ( ficlog, "\n" ); */
2934: printf ("\n" );
2935: /*
2936: For each column J in the current range...
2937:
2938: Write the header.
2939: */
2940: /* fprintf ( ficlog, " Col: "); */
2941: printf ("Col:");
2942: for ( j = j2lo; j <= j2hi; j++ )
2943: {
2944: /* fprintf ( ficlog, " %7d ", j - 1 ); */
2945: /* printf (" %9d ", j - 1 ); */
2946: printf (" %9d ", j );
2947: }
2948: /* fprintf ( ficlog, "\n" ); */
2949: /* fprintf ( ficlog, " Row\n" ); */
2950: /* fprintf ( ficlog, "\n" ); */
2951: printf ("\n" );
2952: printf (" Row\n" );
2953: printf ("\n" );
2954: /*
2955: Determine the range of the rows in this strip.
2956: */
2957: if ( 1 < ilo ){
2958: i2lo = ilo;
2959: }else{
2960: i2lo = 1;
2961: }
2962: if ( m < ihi ){
2963: i2hi = m;
2964: }else{
2965: i2hi = ihi;
2966: }
2967:
2968: for ( i = i2lo; i <= i2hi; i++ ){
2969: /*
2970: Print out (up to) 5 entries in row I, that lie in the current strip.
2971: */
2972: /* fprintf ( ficlog, "%5d:", i - 1 ); */
2973: /* printf ("%5d:", i - 1 ); */
2974: printf ("%5d:", i );
2975: for ( j = j2lo; j <= j2hi; j++ )
2976: {
2977: /* fprintf ( ficlog, " %14g", a[i-1+(j-1)*m] ); */
2978: /* printf ("%14.7g ", a[i-1+(j-1)*m] ); */
2979: /* printf("%14.7f ", v[i-1][j-1]); */
2980: printf("%14.7f ", v[i][j]);
2981: /* fprintf ( stdout, " %14g", a[i-1+(j-1)*m] ); */
2982: }
2983: /* fprintf ( ficlog, "\n" ); */
2984: printf ("\n" );
2985: }
2986: }
2987:
2988: /* printf("%s\n", s); */
2989: /* for (k=0; k<n; k++) { */
2990: /* for (i=0; i<n; i++) { */
2991: /* /\* printf("%20.10e ", v[k][i]); *\/ */
2992: /* } */
2993: /* printf("\n"); */
2994: /* } */
2995: #undef INCX
2996: }
2997:
2998: void vecprint(char *s, double *x, int n)
2999: /* char *s; */
3000: /* double x[N]; */
3001: {
3002: int i=0;
3003:
3004: printf(" %s", s);
3005: /* for (i=0; i<n; i++) */
3006: for (i=1; i<=n; i++)
3007: printf (" %14.7g", x[i] );
3008: /* printf(" %8d: %14g\n", i, x[i]); */
3009: printf ("\n" );
3010: }
3011:
3012: static void print() /* print a line of traces */
3013: {
3014:
3015:
3016: printf("\n");
3017: /* printf("... chi square reduced to ... %20.10e\n", fx); */
3018: /* printf("... after %u function calls ...\n", nf); */
3019: /* printf("... including %u linear searches ...\n", nl); */
3020: printf("%10d %10d%14.7g",nl, nf, fx);
3021: vecprint("... current values of x ...", x, n);
3022: }
3023: /* static void print2(int n, double *x, int prin, double fx, int nf, int nl) */ /* print a line of traces */
3024: static void print2() /* print a line of traces */
3025: {
1.366 ! brouard 3026: int i; /* double fmin=0.; */
1.359 brouard 3027:
3028: /* printf("\n"); */
3029: /* printf("... chi square reduced to ... %20.10e\n", fx); */
3030: /* printf("... after %u function calls ...\n", nf); */
3031: /* printf("... including %u linear searches ...\n", nl); */
3032: /* printf("%10d %10d%14.7g",nl, nf, fx); */
1.363 brouard 3033: /* printf ( "\n" ); */
1.359 brouard 3034: printf ( " Linear searches %d", nl );
1.364 brouard 3035: fprintf (ficlog, " Linear searches %d", nl );
1.359 brouard 3036: /* printf ( " Linear searches %d\n", nl ); */
3037: /* printf ( " Function evaluations %d\n", nf ); */
3038: /* printf ( " Function value FX = %g\n", fx ); */
3039: printf ( " Function evaluations %d", nf );
3040: printf ( " Function value FX = %.12lf\n", fx );
1.363 brouard 3041: fprintf (ficlog, " Function evaluations %d", nf );
3042: fprintf (ficlog, " Function value FX = %.12lf\n", fx );
1.359 brouard 3043: #ifdef DEBUGPRAX
3044: printf("n=%d prin=%d\n",n,prin);
3045: #endif
1.363 brouard 3046: /* if(fx <= fmin) printf(" UNDEFINED "); else printf("%14.7g",log(fx-fmin)); */
1.359 brouard 3047: if ( n <= 4 || 2 < prin )
3048: {
3049: /* for(i=1;i<=n;i++)printf("%14.7g",x[i-1]); */
1.363 brouard 3050: for(i=1;i<=n;i++){
1.364 brouard 3051: printf(" %14.7g",x[i]);
3052: fprintf(ficlog," %14.7g",x[i]);
1.363 brouard 3053: }
1.359 brouard 3054: /* r8vec_print ( n, x, " X:" ); */
3055: }
3056: printf("\n");
1.363 brouard 3057: fprintf(ficlog,"\n");
1.359 brouard 3058: }
3059:
3060:
3061: /* #ifdef MSDOS */
3062: /* static double tflin[N]; */
3063: /* #endif */
3064:
3065: static double flin(double l, int j)
3066: /* double l; */
3067: {
3068: int i;
3069: /* #ifndef MSDOS */
3070: /* double tflin[N]; */
3071: /* #endif */
3072: /* double *tflin; */ /* Be careful to put tflin on a vector n */
3073:
3074: /* j is used from 0 to n-1 and can be -1 for parabolic search */
3075:
3076: /* if (j != -1) { /\* linear search *\/ */
3077: if (j > 0) { /* linear search */
3078: /* for (i=0; i<n; i++){ */
3079: for (i=1; i<=n; i++){
3080: tflin[i] = x[i] + l *v[i][j];
3081: #ifdef DEBUGPRAX
3082: /* printf(" flin i=%14d t=%14.7f x=%14.7f l=%14.7f v[%d,%d]=%14.7f nf=%14d\n",i+1, tflin[i],x[i],l,i,j,v[i][j],nf); */
3083: printf(" flin i=%14d t=%14.7f x=%14.7f l=%14.7f v[%d,%d]=%14.7f nf=%14d\n",i, tflin[i],x[i],l,i,j,v[i][j],nf);
3084: #endif
3085: }
3086: }
3087: else { /* search along parabolic space curve */
3088: qa = l*(l-qd1)/(qd0*(qd0+qd1));
3089: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
3090: qc = l*(l+qd0)/(qd1*(qd0+qd1));
3091: #ifdef DEBUGPRAX
3092: printf(" search along a parabolic space curve. j=%14d nf=%14d l=%14.7f qd0=%14.7f qd1=%14.7f\n",j,nf,l,qd0,qd1);
3093: #endif
3094: /* for (i=0; i<n; i++){ */
3095: for (i=1; i<=n; i++){
3096: tflin[i] = qa*q0[i]+qb*x[i]+qc*q1[i];
3097: #ifdef DEBUGPRAX
3098: /* printf(" parabole i=%14d t(i)=%14.7f q0=%14.7f x=%14.7f q1=%14.7f\n",i+1,tflin[i],q0[i],x[i],q1[i]); */
3099: printf(" parabole i=%14d t(i)=%14.7e q0=%14.7e x=%14.7e q1=%14.7e\n",i,tflin[i],q0[i],x[i],q1[i]);
3100: #endif
3101: }
3102: }
3103: nf++;
3104:
3105: #ifdef NR_SHIFT
3106: return (*fun)((tflin-1), n);
3107: #else
3108: /* return (*fun)(tflin, n);*/
3109: return (*fun)(tflin);
3110: #endif
3111: }
3112:
3113: void minny(int j, int nits, double *d2, double *x1, double f1, int fk)
3114: /* double *d2, *x1, f1; */
3115: {
3116: /* here j is from 0 to n-1 and can be -1 for parabolic search */
3117: /* MINIMIZES F FROM X IN THE DIRECTION V(*,J) */
3118: /* UNLESS J<1, WHEN A QUADRATIC SEARCH IS DONE */
3119: /* IN THE PLANE DEFINED BY Q0, Q1 AND X. */
3120: /* D2 AN APPROXIMATION TO HALF F'' (OR ZERO), */
3121: /* X1 AN ESTIMATE OF DISTANCE TO MINIMUM, */
3122: /* RETURNED AS THE DISTANCE FOUND. */
3123: /* IF FK = TRUE THEN F1 IS FLIN(X1), OTHERWISE */
3124: /* X1 AND F1 ARE IGNORED ON ENTRY UNLESS FINAL */
3125: /* FX > F1. NITS CONTROLS THE NUMBER OF TIMES */
3126: /* AN ATTEMPT IS MADE TO HALVE THE INTERVAL. */
3127: /* SIDE EFFECTS: USES AND ALTERS X, FX, NF, NL. */
3128: /* IF J < 1 USES VARIABLES Q... . */
3129: /* USES H, N, T, M2, M4, LDT, DMIN, MACHEPS; */
3130: int k, i, dz;
3131: double x2, xm, f0, f2, fm, d1, t2, sf1, sx1;
3132: double s;
3133: double macheps;
3134: macheps=pow(16.0,-13.0);
3135: sf1 = f1; sx1 = *x1;
3136: k = 0; xm = 0.0; fm = f0 = fx; dz = *d2 < macheps;
3137: /* h=1.0;*/ /* To be revised */
3138: #ifdef DEBUGPRAX
3139: /* printf("min macheps=%14g h=%14g step=%14g t=%14g fx=%14g\n",macheps,h, step,t, fx); */
3140: /* Where is fx coming from */
3141: printf(" min macheps=%14g h=%14g t=%14g fx=%.9lf dirj=%d\n",macheps, h, t, fx, j);
3142: matprint(" min vectors:",v,n,n);
3143: #endif
3144: /* find step size */
3145: s = 0.;
3146: /* for (i=0; i<n; i++) s += x[i]*x[i]; */
3147: for (i=1; i<=n; i++) s += x[i]*x[i];
3148: s = sqrt(s);
3149: if (dz)
3150: t2 = m4*sqrt(fabs(fx)/dmin + s*ldt) + m2*ldt;
3151: else
3152: t2 = m4*sqrt(fabs(fx)/(*d2) + s*ldt) + m2*ldt;
3153: s = s*m4 + t;
3154: if (dz && t2 > s) t2 = s;
3155: if (t2 < small_windows) t2 = small_windows;
3156: if (t2 > 0.01*h) t2 = 0.01 * h;
3157: if (fk && f1 <= fm) {
3158: xm = *x1;
3159: fm = f1;
3160: }
3161: #ifdef DEBUGPRAX
3162: printf(" additional flin X1=%14.7f t2=%14.7f *f1=%14.7f fm=%14.7f fk=%d\n",*x1,t2,f1,fm,fk);
3163: #endif
3164: if (!fk || fabs(*x1) < t2) {
3165: *x1 = (*x1 >= 0 ? t2 : -t2);
3166: /* *x1 = (*x1 > 0 ? t2 : -t2); */ /* kind of error */
3167: #ifdef DEBUGPRAX
3168: printf(" additional flin X1=%16.10e dirj=%d fk=%d\n",*x1, j, fk);
3169: #endif
3170: f1 = flin(*x1, j);
3171: #ifdef DEBUGPRAX
3172: printf(" after flin f1=%18.12e dirj=%d fk=%d\n",f1, j,fk);
3173: #endif
3174: }
3175: if (f1 <= fm) {
3176: xm = *x1;
3177: fm = f1;
3178: }
3179: L0: /*L0 loop or next */
3180: /*
3181: Evaluate FLIN at another point and estimate the second derivative.
3182: */
3183: if (dz) {
3184: x2 = (f0 < f1 ? -(*x1) : 2*(*x1));
3185: #ifdef DEBUGPRAX
3186: printf(" additional second flin x2=%14.8e x1=%14.8e f0=%14.8e f1=%18.12e dirj=%d\n",x2,*x1,f0,f1,j);
3187: #endif
3188: f2 = flin(x2, j);
3189: #ifdef DEBUGPRAX
3190: printf(" additional second flin x2=%16.10e x1=%16.10e f1=%18.12e f0=%18.10e f2=%18.10e fm=%18.10e\n",x2, *x1, f1,f0,f2,fm);
3191: #endif
3192: if (f2 <= fm) {
3193: xm = x2;
3194: fm = f2;
3195: }
3196: /* d2 is the curvature or double difference f1 doesn't seem to be accurately computed */
3197: *d2 = (x2*(f1-f0) - (*x1)*(f2-f0))/((*x1)*x2*((*x1)-x2));
3198: #ifdef DEBUGPRAX
3199: double d11,d12;
3200: d11=(f1-f0)/(*x1);d12=(f2-f0)/x2;
3201: printf(" d11=%18.12e d12=%18.12e d11-d12=%18.12e x1-x2=%18.12e (d11-d12)/(x2-(*x1))=%18.12e\n", d11 ,d12, d11-d12, x2-(*x1), (d11-d12)/(x2-(*x1)));
3202: printf(" original computing f1=%18.12e *d2=%16.10e f0=%18.12e f1-f0=%16.10e f2-f0=%16.10e\n",f1,*d2,f0,f1-f0, f2-f0);
3203: double ff1=7.783920622852e+04;
3204: double f1mf0=9.0344736236e-05;
3205: *d2 = (f1mf0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2);
3206: /* *d2 = (ff1-f0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2); */
3207: printf(" simpliff computing *d2=%16.10e f1mf0=%18.12e,f1=f0+f1mf0=%18.12e\n",*d2,f1mf0,f0+f1mf0);
3208: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
3209: printf(" overlifi computing *d2=%16.10e\n",*d2);
3210: #endif
3211: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
3212: }
3213: #ifdef DEBUGPRAX
3214: printf(" additional second flin xm=%14.8e fm=%14.8e *d2=%14.8e\n",xm, fm,*d2);
3215: #endif
3216: /*
3217: Estimate the first derivative at 0.
3218: */
3219: d1 = (f1-f0)/(*x1) - *x1**d2; dz = 1;
3220: /*
3221: Predict the minimum.
3222: */
3223: if (*d2 <= small_windows) {
3224: x2 = (d1 < 0 ? h : -h);
3225: }
3226: else {
3227: x2 = - 0.5*d1/(*d2);
3228: }
3229: #ifdef DEBUGPRAX
3230: printf(" AT d1=%14.8e d2=%14.8e small=%14.8e dz=%d x1=%14.8e x2=%14.8e\n",d1,*d2,small_windows,dz,*x1,x2);
3231: #endif
3232: if (fabs(x2) > h)
3233: x2 = (x2 > 0 ? h : -h);
3234: L1: /* L1 or try loop */
3235: #ifdef DEBUGPRAX
3236: printf(" AT predicted minimum flin x2=%14.8e x1=%14.8e K=%14d NITS=%14d dirj=%d\n",x2,*x1,k,nits,j);
3237: #endif
3238: f2 = flin(x2, j); /* x[i]+x2*v[i][j] */
3239: #ifdef DEBUGPRAX
3240: printf(" after flin f0=%14.8e f1=%14.8e f2=%14.8e fm=%14.8e\n",f0,f1,f2, fm);
3241: #endif
3242: if ((k < nits) && (f2 > f0)) {
3243: #ifdef DEBUGPRAX
3244: printf(" NO SUCCESS SO TRY AGAIN;\n");
3245: #endif
3246: k++;
3247: if ((f0 < f1) && (*x1*x2 > 0.0))
3248: goto L0; /* or next */
3249: x2 *= 0.5;
3250: goto L1;
3251: }
3252: nl++;
3253: #ifdef DEBUGPRAX
3254: printf(" bebeBE end of min x1=%14.8e x2=%14.8e f1=%14.8e f2=%14.8e f0=%14.8e fm=%14.8e d2=%14.8e\n",*x1, x2, f1, f2, f0, fm, *d2);
3255: #endif
3256: if (f2 > fm) x2 = xm; else fm = f2;
3257: if (fabs(x2*(x2-*x1)) > small_windows) {
3258: *d2 = (x2*(f1-f0) - *x1*(fm-f0))/(*x1*x2*(*x1-x2));
3259: }
3260: else {
3261: if (k > 0) *d2 = 0;
3262: }
3263: #ifdef DEBUGPRAX
1.362 brouard 3264: printf(" bebe end of min x1 might be very wrong x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
1.359 brouard 3265: #endif
3266: if (*d2 <= small_windows) *d2 = small_windows;
3267: *x1 = x2; fx = fm;
3268: if (sf1 < fx) {
3269: fx = sf1;
3270: *x1 = sx1;
3271: }
3272: /*
3273: Update X for linear search.
3274: */
3275: #ifdef DEBUGPRAX
3276: printf(" end of min x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
3277: #endif
3278:
3279: /* if (j != -1) */
3280: /* for (i=0; i<n; i++) */
3281: /* x[i] += (*x1)*v[i][j]; */
3282: if (j > 0)
3283: for (i=1; i<=n; i++)
3284: x[i] += (*x1)*v[i][j];
3285: }
3286:
3287: void quad() /* look for a minimum along the curve q0, q1, q2 */
3288: {
3289: int i;
3290: double l, s;
3291:
3292: s = fx; fx = qf1; qf1 = s; qd1 = 0.0;
3293: /* for (i=0; i<n; i++) { */
3294: for (i=1; i<=n; i++) {
3295: s = x[i]; l = q1[i]; x[i] = l; q1[i] = s;
3296: qd1 = qd1 + (s-l)*(s-l);
3297: }
3298: s = 0.0; qd1 = sqrt(qd1); l = qd1;
3299: #ifdef DEBUGPRAX
3300: printf(" QUAD after sqrt qd1=%14.8e \n",qd1);
3301: #endif
3302:
3303: if (qd0>0.0 && qd1>0.0 &&nl>=3*n*n) {
3304: #ifdef DEBUGPRAX
3305: printf(" QUAD before min value=%14.8e \n",qf1);
3306: #endif
3307: /* min(-1, 2, &s, &l, qf1, 1); */
3308: minny(0, 2, &s, &l, qf1, 1);
3309: qa = l*(l-qd1)/(qd0*(qd0+qd1));
3310: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
3311: qc = l*(l+qd0)/(qd1*(qd0+qd1));
3312: }
3313: else {
3314: fx = qf1; qa = qb = 0.0; qc = 1.0;
3315: }
3316: #ifdef DEBUGPRAX
3317: printf("after eventual min qd0=%14.8e qd1=%14.8e nl=%d\n",qd0, qd1,nl);
3318: #endif
3319: qd0 = qd1;
3320: /* for (i=0; i<n; i++) { */
3321: for (i=1; i<=n; i++) {
3322: s = q0[i]; q0[i] = x[i];
3323: x[i] = qa*s + qb*x[i] + qc*q1[i];
3324: }
3325: #ifdef DEBUGQUAD
3326: vecprint ( " X after QUAD:" , x, n );
3327: #endif
3328: }
3329:
3330: /* void minfit(int n, double eps, double tol, double ab[N][N], double q[]) */
3331: void minfit(int n, double eps, double tol, double **ab, double q[])
3332: /* int n; */
3333: /* double eps, tol, ab[N][N], q[N]; */
3334: {
3335: int l, kt, l2, i, j, k;
3336: double c, f, g, h, s, x, y, z;
3337: /* double eps; */
3338: /* #ifndef MSDOS */
3339: /* double e[N]; /\* plenty of stack on a vax *\/ */
3340: /* #endif */
3341: /* double *e; */
3342: /* e=vector(0,n-1); /\* should be freed somewhere but gotos *\/ */
3343:
3344: /* householder's reduction to bidiagonal form */
3345:
3346: if(n==1){
3347: /* q[1-1]=ab[1-1][1-1]; */
3348: /* ab[1-1][1-1]=1.0; */
3349: q[1]=ab[1][1];
3350: ab[1][1]=1.0;
3351: return; /* added from hardt */
3352: }
3353: /* eps=macheps; */ /* added */
3354: x = g = 0.0;
3355: #ifdef DEBUGPRAX
3356: matprint (" HOUSE holder:", ab, n, n);
3357: #endif
3358:
3359: /* for (i=0; i<n; i++) { /\* FOR I := 1 UNTIL N DO *\/ */
3360: for (i=1; i<=n; i++) { /* FOR I := 1 UNTIL N DO */
3361: e[i] = g; s = 0.0; l = i+1;
3362: /* for (j=i; j<n; j++) /\* FOR J := I UNTIL N DO S := S*AB(J,I)**2; *\/ /\* not correct *\/ */
3363: for (j=i; j<=n; j++) /* FOR J := I UNTIL N DO S := S*AB(J,I)**2; */ /* not correct */
3364: s += ab[j][i] * ab[j][i];
3365: #ifdef DEBUGPRAXFIN
3366: printf("i=%d s=%d %.7g tol=%.7g",i,s,tol);
3367: #endif
3368: if (s < tol) {
3369: g = 0.0;
3370: }
3371: else {
3372: /* f = ab[i][i]; */
3373: f = ab[i][i];
3374: if (f < 0.0)
3375: g = sqrt(s);
3376: else
3377: g = -sqrt(s);
3378: /* h = f*g - s; ab[i][i] = f - g; */
3379: h = f*g - s; ab[i][i] = f - g;
3380: /* for (j=l; j<n; j++) { */ /* FOR J := L UNTIL N DO */ /* wrong */
3381: for (j=l; j<=n; j++) {
3382: f = 0.0;
3383: /* for (k=i; k<n; k++) /\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
3384: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
3385: /* f += ab[k][i] * ab[k][j]; */
3386: f += ab[k][i] * ab[k][j];
3387: f /= h;
3388: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
3389: /* for (k=i; k<n; k++)/\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
3390: ab[k][j] += f * ab[k][i];
3391: /* ab[k][j] += f * ab[k][i]; */
3392: #ifdef DEBUGPRAX
3393: printf("Holder J=%d F=%.7g",j,f);
3394: #endif
3395: }
3396: } /* end s */
3397: /* q[i] = g; s = 0.0; */
3398: q[i] = g; s = 0.0;
3399: #ifdef DEBUGPRAX
3400: printf(" I Q=%d %.7g",i,q[i]);
3401: #endif
3402:
3403: /* if (i < n) */
3404: /* if (i <= n) /\* I is always lower or equal to n wasn't in golub reinsch*\/ */
3405: /* for (j=l; j<n; j++) */
3406: for (j=l; j<=n; j++)
3407: s += ab[i][j] * ab[i][j];
3408: /* s += ab[i][j] * ab[i][j]; */
3409: if (s < tol) {
3410: g = 0.0;
3411: }
3412: else {
3413: if(i<n)
3414: /* f = ab[i][i+1]; */ /* Brent golub overflow */
3415: f = ab[i][i+1];
3416: if (f < 0.0)
3417: g = sqrt(s);
3418: else
3419: g = - sqrt(s);
3420: h = f*g - s;
3421: /* h = f*g - s; ab[i][i+1] = f - g; */ /* Overflow for i=n Error in Golub too but not Burkardt*/
3422: /* for (j=l; j<n; j++) */
3423: /* e[j] = ab[i][j]/h; */
3424: if(i<n){
3425: ab[i][i+1] = f - g;
3426: for (j=l; j<=n; j++)
3427: e[j] = ab[i][j]/h;
3428: /* for (j=l; j<n; j++) { */
3429: for (j=l; j<=n; j++) {
3430: s = 0.0;
3431: /* for (k=l; k<n; k++) s += ab[j][k]*ab[i][k]; */
3432: for (k=l; k<=n; k++) s += ab[j][k]*ab[i][k];
3433: /* for (k=l; k<n; k++) ab[j][k] += s * e[k]; */
3434: for (k=l; k<=n; k++) ab[j][k] += s * e[k];
3435: } /* END J */
3436: } /* END i <n */
3437: } /* end s */
3438: /* y = fabs(q[i]) + fabs(e[i]); */
3439: y = fabs(q[i]) + fabs(e[i]);
3440: if (y > x) x = y;
3441: #ifdef DEBUGPRAX
3442: printf(" I Y=%d %.7g",i,y);
3443: #endif
3444: #ifdef DEBUGPRAX
3445: printf(" i=%d e(i) %.7g",i,e[i]);
3446: #endif
3447: } /* end i */
3448: /*
3449: Accumulation of right hand transformations */
3450: /* for (i=n-1; i >= 0; i--) { */ /* FOR I := N STEP -1 UNTIL 1 DO */
3451: /* We should avoid the overflow in Golub */
3452: /* ab[n-1][n-1] = 1.0; */
3453: /* g = e[n-1]; */
3454: ab[n][n] = 1.0;
3455: g = e[n];
3456: l = n;
3457:
3458: /* for (i=n; i >= 1; i--) { */
3459: for (i=n-1; i >= 1; i--) { /* n-1 loops, different from brent and golub*/
3460: if (g != 0.0) {
3461: /* h = ab[i-1][i]*g; */
3462: h = ab[i][i+1]*g;
3463: for (j=l; j<=n; j++) ab[j][i] = ab[i][j] / h;
3464: for (j=l; j<=n; j++) {
3465: /* h = ab[i][i+1]*g; */
3466: /* for (j=l; j<n; j++) ab[j][i] = ab[i][j] / h; */
3467: /* for (j=l; j<n; j++) { */
3468: s = 0.0;
3469: /* for (k=l; k<n; k++) s += ab[i][k] * ab[k][j]; */
3470: /* for (k=l; k<n; k++) ab[k][j] += s * ab[k][i]; */
3471: for (k=l; k<=n; k++) s += ab[i][k] * ab[k][j];
3472: for (k=l; k<=n; k++) ab[k][j] += s * ab[k][i];
3473: }/* END J */
3474: }/* END G */
3475: /* for (j=l; j<n; j++) */
3476: /* ab[i][j] = ab[j][i] = 0.0; */
3477: /* ab[i][i] = 1.0; g = e[i]; l = i; */
3478: for (j=l; j<=n; j++)
3479: ab[i][j] = ab[j][i] = 0.0;
3480: ab[i][i] = 1.0; g = e[i]; l = i;
3481: }/* END I */
3482: #ifdef DEBUGPRAX
3483: matprint (" HOUSE accumulation:",ab,n, n );
3484: #endif
3485:
3486: /* diagonalization to bidiagonal form */
3487: eps *= x;
3488: /* for (k=n-1; k>= 0; k--) { */
3489: for (k=n; k>= 1; k--) {
3490: kt = 0;
3491: TestFsplitting:
3492: #ifdef DEBUGPRAX
3493: printf(" TestFsplitting: k=%d kt=%d\n",k,kt);
3494: /* for(i=1;i<=n;i++)printf(" e(%d)=%.14f",i,e[i]);printf("\n"); */
3495: #endif
3496: kt = kt+1;
3497: /* TestFsplitting: */
3498: /* if (++kt > 30) { */
3499: if (kt > 30) {
3500: e[k] = 0.0;
3501: fprintf(stderr, "\n+++ MINFIT - Fatal error\n");
3502: fprintf ( stderr, " The QR algorithm failed to converge.\n" );
3503: }
3504: /* for (l2=k; l2>=0; l2--) { */
3505: for (l2=k; l2>=1; l2--) {
3506: l = l2;
3507: #ifdef DEBUGPRAX
3508: printf(" l e(l)< eps %d %.7g %.7g ",l,e[l], eps);
3509: #endif
3510: /* if (fabs(e[l]) <= eps) */
3511: if (fabs(e[l]) <= eps)
3512: goto TestFconvergence;
3513: /* if (fabs(q[l-1]) <= eps)*/ /* missing if ( 1 < l ){ *//* printf(" q(l-1)< eps %d %.7g %.7g ",l-1,q[l-2], eps); */
3514: if (fabs(q[l-1]) <= eps)
3515: break; /* goto Cancellation; */
3516: }
3517: Cancellation:
3518: #ifdef DEBUGPRAX
3519: printf(" Cancellation:\n");
3520: #endif
3521: c = 0.0; s = 1.0;
3522: for (i=l; i<=k; i++) {
3523: f = s * e[i]; e[i] *= c;
3524: /* f = s * e[i]; e[i] *= c; */
3525: if (fabs(f) <= eps)
3526: goto TestFconvergence;
3527: /* g = q[i]; */
3528: g = q[i];
3529: if (fabs(f) < fabs(g)) {
3530: double fg = f/g;
3531: h = fabs(g)*sqrt(1.0+fg*fg);
3532: }
3533: else {
3534: double gf = g/f;
3535: h = (f!=0.0 ? fabs(f)*sqrt(1.0+gf*gf) : 0.0);
3536: }
3537: /* COMMENT: THE ABOVE REPLACES Q(I):=H:=LONGSQRT(G*G+F*F) */
3538: /* WHICH MAY GIVE INCORRECT RESULTS IF THE */
3539: /* SQUARES UNDERFLOW OR IF F = G = 0; */
3540:
3541: /* q[i] = h; */
3542: q[i] = h;
3543: if (h == 0.0) { h = 1.0; g = 1.0; }
3544: c = g/h; s = -f/h;
3545: }
3546: TestFconvergence:
3547: #ifdef DEBUGPRAX
3548: printf(" TestFconvergence: l=%d k=%d\n",l,k);
3549: #endif
3550: /* z = q[k]; */
3551: z = q[k];
3552: if (l == k)
3553: goto Convergence;
3554: /* shift from bottom 2x2 minor */
3555: /* x = q[l]; y = q[k-l]; g = e[k-1]; h = e[k]; */ /* Error */
3556: x = q[l]; y = q[k-1]; g = e[k-1]; h = e[k];
3557: f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2.0*h*y);
3558: g = sqrt(f*f+1.0);
3559: if (f <= 0.0)
3560: f = ((x-z)*(x+z) + h*(y/(f-g)-h))/x;
3561: else
3562: f = ((x-z)*(x+z) + h*(y/(f+g)-h))/x;
3563: /* next qr transformation */
3564: s = c = 1.0;
3565: for (i=l+1; i<=k; i++) {
3566: #ifdef DEBUGPRAXQR
3567: printf(" Before Mid TestFconvergence: l+1=%d i=%d k=%d h=%.6e e(i)=%14.8f e(i-1)=%14.8f\n",l+1,i,k, h, e[i],e[i-1]);
3568: #endif
3569: /* g = e[i]; y = q[i]; h = s*g; g *= c; */
3570: g = e[i]; y = q[i]; h = s*g; g *= c;
3571: if (fabs(f) < fabs(h)) {
3572: double fh = f/h;
3573: z = fabs(h) * sqrt(1.0 + fh*fh);
3574: }
3575: else {
3576: double hf = h/f;
3577: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
3578: }
3579: /* e[i-1] = z; */
3580: e[i-1] = z;
3581: #ifdef DEBUGPRAXQR
3582: printf(" Mid TestFconvergence: l+1=%d i=%d k=%d h=%.6e e(i)=%14.8f e(i-1)=%14.8f\n",l+1,i,k, h, e[i],e[i-1]);
3583: #endif
3584: if (z == 0.0)
3585: f = z = 1.0;
3586: c = f/z; s = h/z;
3587: f = x*c + g*s; g = - x*s + g*c; h = y*s;
3588: y *= c;
3589: /* for (j=0; j<n; j++) { */
3590: /* x = ab[j][i-1]; z = ab[j][i]; */
3591: /* ab[j][i-1] = x*c + z*s; */
3592: /* ab[j][i] = - x*s + z*c; */
3593: /* } */
3594: for (j=1; j<=n; j++) {
3595: x = ab[j][i-1]; z = ab[j][i];
3596: ab[j][i-1] = x*c + z*s;
3597: ab[j][i] = - x*s + z*c;
3598: }
3599: if (fabs(f) < fabs(h)) {
3600: double fh = f/h;
3601: z = fabs(h) * sqrt(1.0 + fh*fh);
3602: }
3603: else {
3604: double hf = h/f;
3605: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
3606: }
3607: #ifdef DEBUGPRAXQR
3608: printf(" qr transformation z f h=%.7g %.7g %.7g i=%d k=%d\n",z,f,h, i, k);
3609: #endif
3610: q[i-1] = z;
3611: if (z == 0.0)
3612: z = f = 1.0;
3613: c = f/z; s = h/z;
3614: f = c*g + s*y; /* f can be very small */
3615: x = - s*g + c*y;
3616: }
3617: /* e[l] = 0.0; e[k] = f; q[k] = x; */
3618: e[l] = 0.0; e[k] = f; q[k] = x;
3619: #ifdef DEBUGPRAXQR
3620: printf(" aftermid loop l=%d k=%d e(l)=%7g e(k)=%.7g q(k)=%.7g x=%.7g\n",l,k,e[l],e[k],q[k],x);
3621: #endif
3622: goto TestFsplitting;
3623: Convergence:
3624: #ifdef DEBUGPRAX
3625: printf(" Convergence:\n");
3626: #endif
3627: if (z < 0.0) {
3628: /* q[k] = - z; */
3629: /* for (j=0; j<n; j++) ab[j][k] = - ab[j][k]; */
3630: q[k] = - z;
3631: for (j=1; j<=n; j++) ab[j][k] = - ab[j][k];
3632: }/* END Z */
3633: }/* END K */
3634: } /* END MINFIT */
3635:
3636:
3637: double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x))
3638: /* double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x, int _n)) */
3639: /* double praxis(double (*_fun)(), double _x[], int _n) */
3640: /* double (*_fun)(); */
3641: /* double _x[N]; */
3642: /* double (*_fun)(); */
3643: /* double _x[N]; */
3644: {
3645: /* init global extern variables and parameters */
3646: /* double *d, *y, *z, */
3647: /* *q0, *q1, **v; */
3648: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
3649: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
3650:
3651:
3652: int seed; /* added */
3653: int biter=0;
3654: double r;
3655: double randbrent( int (*));
3656: double s, sf;
3657:
3658: h = h0; /* step; */
3659: t = tol;
3660: scbd = 1.0;
3661: illc = 0;
3662: ktm = 1;
3663:
3664: macheps = DBL_EPSILON;
3665: /* prin=4; */
3666: #ifdef DEBUGPRAX
3667: printf("Praxis macheps=%14g h=%14g step=%14g tol=%14g\n",macheps,h, h0,tol);
3668: #endif
3669: n = _n;
3670: x = _x;
3671: prin = _prin;
3672: fun = _fun;
3673: d=vector(1, n);
3674: y=vector(1, n);
3675: z=vector(1, n);
3676: q0=vector(1, n);
3677: q1=vector(1, n);
3678: e=vector(1, n);
3679: tflin=vector(1, n);
3680: v=matrix(1, n, 1, n);
3681: for(i=1;i<=n;i++){d[i]=y[i]=z[i]=q0[0]=e[i]=tflin[i]=0.;}
3682: small_windows = (macheps) * (macheps); vsmall = small_windows*small_windows;
3683: large = 1.0/small_windows; vlarge = 1.0/vsmall;
3684: m2 = sqrt(macheps); m4 = sqrt(m2);
3685: seed = 123456789; /* added */
3686: ldfac = (illc ? 0.1 : 0.01);
3687: for(i=1;i<=n;i++) z[i]=0.; /* Was missing in Gegenfurtner as well as Brent's algol or fortran */
3688: nl = kt = 0; nf = 1;
3689: #ifdef NR_SHIFT
3690: fx = (*fun)((x-1), n);
3691: #else
3692: fx = (*fun)(x);
3693: #endif
3694: qf1 = fx;
3695: t2 = small_windows + fabs(t); t = t2; dmin = small_windows;
3696: #ifdef DEBUGPRAX
3697: printf("praxis2 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3698: #endif
3699: if (h < 100.0*t) h = 100.0*t;
3700: #ifdef DEBUGPRAX
3701: printf("praxis3 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3702: #endif
3703: ldt = h;
3704: /* for (i=0; i<n; i++) for (j=0; j<n; j++) */
3705: for (i=1; i<=n; i++) for (j=1; j<=n; j++)
3706: v[i][j] = (i == j ? 1.0 : 0.0);
3707: d[1] = 0.0; qd0 = 0.0;
3708: /* for (i=0; i<n; i++) q1[i] = x[i]; */
3709: for (i=1; i<=n; i++) q1[i] = x[i];
3710: if (prin > 1) {
3711: printf("\n------------- enter function praxis -----------\n");
3712: printf("... current parameter settings ...\n");
3713: printf("... scaling ... %20.10e\n", scbd);
3714: printf("... tol ... %20.10e\n", t);
3715: printf("... maxstep ... %20.10e\n", h);
3716: printf("... illc ... %20u\n", illc);
3717: printf("... ktm ... %20u\n", ktm);
3718: printf("... maxfun ... %20u\n", maxfun);
3719: }
3720: if (prin) print2();
3721:
3722: mloop:
3723: biter++; /* Added to count the loops */
3724: /* sf = d[0]; */
3725: /* s = d[0] = 0.0; */
3726: printf("\n Big iteration %d \n",biter);
3727: fprintf(ficlog,"\n Big iteration %d \n",biter);
3728: sf = d[1];
3729: s = d[1] = 0.0;
3730:
3731: /* minimize along first direction V(*,1) */
3732: #ifdef DEBUGPRAX
3733: printf(" Minimize along the first direction V(*,1). illc=%d\n",illc);
3734: /* fprintf(ficlog," Minimize along the first direction V(*,1).\n"); */
3735: #endif
3736: #ifdef DEBUGPRAX2
3737: printf("praxis4 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
3738: #endif
3739: /* min(0, 2, &d[0], &s, fx, 0); /\* mac heps not global *\/ */
1.362 brouard 3740: minny(1, 2, &d[1], &s, fx, 0); /* mac heps not global it seems that fx doesn't correspond to f(s=*x1) */
1.359 brouard 3741: #ifdef DEBUGPRAX
3742: printf("praxis5 macheps=%14g h=%14g looks at sign of s=%14g fx=%14g\n",macheps,h, s,fx);
3743: #endif
3744: if (s <= 0.0)
3745: /* for (i=0; i < n; i++) */
3746: for (i=1; i <= n; i++)
3747: v[i][1] = -v[i][1];
3748: /* if ((sf <= (0.9 * d[0])) || ((0.9 * sf) >= d[0])) */
3749: if ((sf <= (0.9 * d[1])) || ((0.9 * sf) >= d[1]))
3750: /* for (i=1; i<n; i++) */
3751: for (i=2; i<=n; i++)
3752: d[i] = 0.0;
3753: /* for (k=1; k<n; k++) { */
3754: for (k=2; k<=n; k++) {
3755: /*
3756: The inner loop starts here.
3757: */
3758: #ifdef DEBUGPRAX
3759: printf(" The inner loop here from k=%d to n=%d.\n",k,n);
3760: /* fprintf(ficlog," The inner loop here from k=%d to n=%d.\n",k,n); */
3761: #endif
3762: /* for (i=0; i<n; i++) */
3763: for (i=1; i<=n; i++)
3764: y[i] = x[i];
3765: sf = fx;
3766: #ifdef DEBUGPRAX
3767: printf(" illc=%d and kt=%d and ktm=%d\n", illc, kt, ktm);
3768: #endif
3769: illc = illc || (kt > 0);
3770: next:
3771: kl = k;
3772: df = 0.0;
3773: if (illc) { /* random step to get off resolution valley */
3774: #ifdef DEBUGPRAX
3775: printf(" A random step follows, to avoid resolution valleys.\n");
3776: matprint(" before rand, vectors:",v,n,n);
3777: #endif
3778: for (i=1; i<=n; i++) {
3779: #ifdef NOBRENTRAND
3780: r = drandom();
3781: #else
3782: seed=i;
3783: /* seed=i+1; */
3784: #ifdef DEBUGRAND
3785: printf(" Random seed=%d, brent i=%d",seed,i); /* YYYY i=5 j=1 vji= -0.0001170073 */
3786: #endif
3787: r = randbrent ( &seed );
3788: #endif
3789: #ifdef DEBUGRAND
3790: printf(" Random r=%.7g \n",r);
3791: #endif
3792: z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (r - 0.5);
3793: /* z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (drandom() - 0.5); */
3794:
3795: s = z[i];
3796: for (j=1; j <= n; j++)
3797: x[j] += s * v[j][i];
3798: }
3799: #ifdef DEBUGRAND
3800: matprint(" after rand, vectors:",v,n,n);
3801: #endif
3802: #ifdef NR_SHIFT
3803: fx = (*fun)((x-1), n);
3804: #else
1.366 ! brouard 3805: fx = (*fun)(x);
1.359 brouard 3806: #endif
3807: /* fx = (*func) ( (x-1) ); *//* This for func which is computed from x[1] and not from x[0] xm1=(x-1)*/
3808: nf++;
3809: }
3810: /* minimize along non-conjugate directions */
3811: #ifdef DEBUGPRAX
3812: printf(" Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n);
3813: /* fprintf(ficlog," Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n); */
3814: #endif
3815: /* for (k2=k; k2<n; k2++) { /\* Be careful here k2 <=n ? *\/ */
3816: for (k2=k; k2<=n; k2++) { /* Be careful here k2 <=n ? */
3817: sl = fx;
3818: s = 0.0;
3819: #ifdef DEBUGPRAX
3820: printf(" Minimize along the 'NON-CONJUGATE' true direction k2=%14d fx=%14.7f\n",k2, fx);
3821: matprint(" before min vectors:",v,n,n);
3822: #endif
3823: /* min(k2, 2, &d[k2], &s, fx, 0); */
3824: /* jsearch=k2-1; */
3825: /* min(jsearch, 2, &d[jsearch], &s, fx, 0); */
3826: minny(k2, 2, &d[k2], &s, fx, 0);
3827: #ifdef DEBUGPRAX
3828: printf(" . D(%d)=%14.7f d[k2]=%14.7f z[k2]=%14.7f illc=%14d fx=%14.7f\n",k2,d[k2],d[k2],z[k2],illc,fx);
3829: #endif
3830: if (illc) {
3831: /* double szk = s + z[k2]; */
3832: /* s = d[k2] * szk*szk; */
3833: double szk = s + z[k2];
3834: s = d[k2] * szk*szk;
3835: }
3836: else
3837: s = sl - fx;
3838: /* if (df < s) { */
3839: if (df <= s) {
3840: df = s;
3841: kl = k2;
3842: #ifdef DEBUGPRAX
3843: printf(" df=%.7g and choose kl=%d \n",df,kl); /* UUUU */
3844: #endif
3845: }
3846: } /* end loop k2 */
3847: /*
3848: If there was not much improvement on the first try, set
3849: ILLC = true and start the inner loop again.
3850: */
3851: #ifdef DEBUGPRAX
3852: printf(" If there was not much improvement on the first try, set ILLC = true and start the inner loop again. illc=%d\n",illc);
3853: /* fprintf(ficlog," If there was not much improvement on the first try, set ILLC = true and start the inner loop again.\n"); */
3854: #endif
3855: if (!illc && (df < fabs(100.0 * (macheps) * fx))) {
3856: #ifdef DEBUGPRAX
3857: printf("\n NO SUCCESS because DF is small, starts inner loop with same K(=%d), fabs( 100.0 * machep(=%.10e) * fx(=%.9e) )=%.9e > df(=%.9e) break illc=%d\n", k, macheps, fx, fabs ( 100.0 * macheps * fx ), df, illc);
3858: #endif
3859: illc = 1;
3860: goto next;
3861: }
3862: #ifdef DEBUGPRAX
3863: printf("\n SUCCESS, BREAKS inner loop K(=%d) because DF is big, fabs( 100.0 * machep(=%.10e) * fx(=%.9e) )=%.9e <= df(=%.9e) break illc=%d\n", k, macheps, fx, fabs ( 100.0 * macheps * fx ), df, illc);
3864: #endif
3865:
3866: /* if ((k == 1) && (prin > 1)){ /\* be careful k=2 *\/ */
3867: if ((k == 2) && (prin > 1)){ /* be careful k=2 */
3868: #ifdef DEBUGPRAX
3869: printf(" NEW D The second difference array d:\n" );
3870: /* fprintf(ficlog, " NEW D The second difference array d:\n" ); */
3871: #endif
3872: vecprint(" NEW D The second difference array d:",d,n);
3873: }
3874: /* minimize along conjugate directions */
3875: /*
3876: Minimize along the "conjugate" directions V(*,1),...,V(*,K-1).
3877: */
3878: #ifdef DEBUGPRAX
3879: printf("Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1);
3880: /* fprintf(ficlog,"Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1); */
3881: #endif
3882: /* for (k2=0; k2<=k-1; k2++) { */
3883: for (k2=1; k2<=k-1; k2++) {
3884: s = 0.0;
3885: /* min(k2-1, 2, &d[k2-1], &s, fx, 0); */
3886: minny(k2, 2, &d[k2], &s, fx, 0);
3887: }
3888: f1 = fx;
3889: fx = sf;
3890: lds = 0.0;
3891: /* for (i=0; i<n; i++) { */
3892: for (i=1; i<=n; i++) {
3893: sl = x[i];
3894: x[i] = y[i];
3895: y[i] = sl - y[i];
3896: sl = y[i];
3897: lds = lds + sl*sl;
3898: }
3899: lds = sqrt(lds);
3900: #ifdef DEBUGPRAX
3901: printf("Minimization done 'conjugate', shifted all points, computed lds=%.8f\n",lds);
3902: #endif
3903: /*
3904: Discard direction V(*,kl).
3905:
3906: If no random step was taken, V(*,KL) is the "non-conjugate"
3907: direction along which the greatest improvement was made.
3908: */
3909: if (lds > small_windows) {
3910: #ifdef DEBUGPRAX
3911: printf("lds big enough to throw direction V(*,kl=%d). If no random step was taken, V(*,KL) is the 'non-conjugate' direction along which the greatest improvement was made.\n",kl);
3912: matprint(" before shift new conjugate vectors:",v,n,n);
3913: #endif
3914: for (i=kl-1; i>=k; i--) {
3915: /* for (j=0; j < n; j++) */
3916: for (j=1; j <= n; j++)
3917: /* v[j][i+1] = v[j][i]; */ /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
3918: v[j][i+1] = v[j][i]; /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
3919: /* v[j][i+1] = v[j][i]; */
3920: /* d[i+1] = d[i];*/ /* last is d[k+1]= d[k] */
3921: d[i+1] = d[i]; /* last is d[k]= d[k-1] */
3922: }
3923: #ifdef DEBUGPRAX
3924: matprint(" after shift new conjugate vectors:",v,n,n);
3925: #endif /* d[k] = 0.0; */
3926: d[k] = 0.0;
3927: for (i=1; i <= n; i++)
3928: v[i][k] = y[i] / lds;
3929: /* v[i][k] = y[i] / lds; */
3930: #ifdef DEBUGPRAX
3931: printf("Minimize along the new 'conjugate' direction V(*,k=%d), which is the normalized vector: (new x) - (old x). d2=%14.7g lds=%.10f\n",k,d[k],lds);
3932: /* fprintf(ficlog,"Minimize along the new 'conjugate' direction V(*,k=%d), which is the normalized vector: (new x) - (old x).\n",k); */
3933: matprint(" before min new conjugate vectors:",v,n,n);
3934: #endif
3935: /* min(k-1, 4, &d[k-1], &lds, f1, 1); */
3936: minny(k, 4, &d[k], &lds, f1, 1);
3937: #ifdef DEBUGPRAX
3938: printf(" after min d(k)=%d %.7g lds=%14f\n",k,d[k],lds);
3939: matprint(" after min vectors:",v,n,n);
3940: #endif
3941: if (lds <= 0.0) {
3942: lds = -lds;
3943: #ifdef DEBUGPRAX
3944: printf(" lds changed sign lds=%.14f k=%d\n",lds,k);
3945: #endif
3946: /* for (i=0; i<n; i++) */
3947: /* v[i][k] = -v[i][k]; */
3948: for (i=1; i<=n; i++)
3949: v[i][k] = -v[i][k];
3950: }
3951: }
3952: ldt = ldfac * ldt;
3953: if (ldt < lds)
3954: ldt = lds;
3955: if (prin > 0){
3956: #ifdef DEBUGPRAX
3957: printf(" k=%d",k);
3958: /* fprintf(ficlog," k=%d",k); */
3959: #endif
3960: print2();/* n, x, prin, fx, nf, nl ); */
3961: }
3962: t2 = 0.0;
3963: /* for (i=0; i<n; i++) */
3964: for (i=1; i<=n; i++)
3965: t2 += x[i]*x[i];
3966: t2 = m2 * sqrt(t2) + t;
3967: /*
3968: See whether the length of the step taken since starting the
3969: inner loop exceeds half the tolerance.
3970: */
3971: #ifdef DEBUGPRAX
3972: printf("See if step length exceeds half the tolerance.\n"); /* ZZZZZ */
3973: /* fprintf(ficlog,"See if step length exceeds half the tolerance.\n"); */
3974: #endif
3975: if (ldt > (0.5 * t2))
3976: kt = 0;
3977: else
3978: kt++;
3979: #ifdef DEBUGPRAX
3980: printf("if kt=%d >? ktm=%d gotoL2 loop\n",kt,ktm);
3981: #endif
3982: if (kt > ktm){
3983: if ( 0 < prin ){
3984: /* printf("\nr8vec_print\n X:\n"); */
3985: /* fprintf(ficlog,"\nr8vec_print\n X:\n"); */
3986: vecprint ("END X:", x, n );
3987: }
3988: goto fret;
3989: }
3990: #ifdef DEBUGPRAX
3991: matprint(" end of L2 loop vectors:",v,n,n);
3992: #endif
3993:
3994: }
3995: /* printf("The inner loop ends here.\n"); */
3996: /* fprintf(ficlog,"The inner loop ends here.\n"); */
3997: /*
3998: The inner loop ends here.
3999:
4000: Try quadratic extrapolation in case we are in a curved valley.
4001: */
4002: #ifdef DEBUGPRAX
4003: printf("Try QUAD ratic extrapolation in case we are in a curved valley.\n");
4004: #endif
4005: /* try quadratic extrapolation in case */
4006: /* we are stuck in a curved valley */
4007: quad();
4008: dn = 0.0;
4009: /* for (i=0; i<n; i++) { */
4010: for (i=1; i<=n; i++) {
4011: d[i] = 1.0 / sqrt(d[i]);
4012: if (dn < d[i])
4013: dn = d[i];
4014: }
4015: if (prin > 2)
4016: matprint(" NEW DIRECTIONS vectors:",v,n,n);
4017: /* for (j=0; j<n; j++) { */
4018: for (j=1; j<=n; j++) {
4019: s = d[j] / dn;
4020: /* for (i=0; i < n; i++) */
4021: for (i=1; i <= n; i++)
4022: v[i][j] *= s;
4023: }
4024:
4025: if (scbd > 1.0) { /* scale axis to reduce condition number */
4026: #ifdef DEBUGPRAX
4027: printf("Scale the axes to try to reduce the condition number.\n");
4028: #endif
4029: /* fprintf(ficlog,"Scale the axes to try to reduce the condition number.\n"); */
4030: s = vlarge;
4031: /* for (i=0; i<n; i++) { */
4032: for (i=1; i<=n; i++) {
4033: sl = 0.0;
4034: /* for (j=0; j < n; j++) */
4035: for (j=1; j <= n; j++)
4036: sl += v[i][j]*v[i][j];
4037: z[i] = sqrt(sl);
4038: if (z[i] < m4)
4039: z[i] = m4;
4040: if (s > z[i])
4041: s = z[i];
4042: }
4043: /* for (i=0; i<n; i++) { */
4044: for (i=1; i<=n; i++) {
4045: sl = s / z[i];
4046: z[i] = 1.0 / sl;
4047: if (z[i] > scbd) {
4048: sl = 1.0 / scbd;
4049: z[i] = scbd;
4050: }
4051: }
4052: }
4053: for (i=1; i<=n; i++)
4054: /* for (j=0; j<=i-1; j++) { */
4055: /* for (j=1; j<=i; j++) { */
4056: for (j=1; j<=i-1; j++) {
4057: s = v[i][j];
4058: v[i][j] = v[j][i];
4059: v[j][i] = s;
4060: }
4061: #ifdef DEBUGPRAX
4062: printf(" Calculate a new set of orthogonal directions before repeating the main loop.\n Transpose V for MINFIT:...\n");
4063: #endif
4064: /*
4065: MINFIT finds the singular value decomposition of V.
4066:
4067: This gives the principal values and principal directions of the
4068: approximating quadratic form without squaring the condition number.
4069: */
4070: #ifdef DEBUGPRAX
4071: printf(" MINFIT finds the singular value decomposition of V. \n This gives the principal values and principal directions of the\n approximating quadratic form without squaring the condition number...\n");
4072: #endif
4073:
4074: minfit(n, macheps, vsmall, v, d);
4075: /* for(i=0; i<n;i++)printf(" %14.7g",d[i]); */
4076: /* v is overwritten with R. */
4077: /*
4078: Unscale the axes.
4079: */
4080: if (scbd > 1.0) {
4081: #ifdef DEBUGPRAX
4082: printf(" Unscale the axes.\n");
4083: #endif
4084: /* for (i=0; i<n; i++) { */
4085: for (i=1; i<=n; i++) {
4086: s = z[i];
4087: /* for (j=0; j<n; j++) */
4088: for (j=1; j<=n; j++)
4089: v[i][j] *= s;
4090: }
4091: /* for (i=0; i<n; i++) { */
4092: for (i=1; i<=n; i++) {
4093: s = 0.0;
4094: /* for (j=0; j<n; j++) */
4095: for (j=1; j<=n; j++)
4096: s += v[j][i]*v[j][i];
4097: s = sqrt(s);
4098: d[i] *= s;
4099: s = 1.0 / s;
4100: /* for (j=0; j<n; j++) */
4101: for (j=1; j<=n; j++)
4102: v[j][i] *= s;
4103: }
4104: }
4105: /* for (i=0; i<n; i++) { */
4106: double dni; /* added for compatibility with buckhardt but not brent */
4107: for (i=1; i<=n; i++) {
4108: dni=dn*d[i]; /* added for compatibility with buckhardt but not brent */
4109: if ((dn * d[i]) > large)
4110: d[i] = vsmall;
4111: else if ((dn * d[i]) < small_windows)
4112: d[i] = vlarge;
4113: else
4114: d[i] = 1.0 / dni / dni; /* added for compatibility with buckhardt but not brent */
4115: /* d[i] = pow(dn * d[i],-2.0); */
4116: }
4117: #ifdef DEBUGPRAX
4118: vecprint ("\n Before sort Eigenvalues of a:",d,n );
4119: #endif
4120:
4121: sort(); /* the new eigenvalues and eigenvectors */
4122: #ifdef DEBUGPRAX
4123: vecprint( " After sort the eigenvalues ....\n", d, n);
4124: matprint( " After sort the eigenvectors....\n", v, n,n);
4125: #endif
4126: #ifdef DEBUGPRAX
4127: printf(" Determine the smallest eigenvalue.\n");
4128: #endif
4129: /* dmin = d[n-1]; */
4130: dmin = d[n];
4131: if (dmin < small_windows)
4132: dmin = small_windows;
4133: /*
4134: The ratio of the smallest to largest eigenvalue determines whether
4135: the system is ill conditioned.
4136: */
4137:
4138: /* illc = (m2 * d[0]) > dmin; */
4139: illc = (m2 * d[1]) > dmin;
4140: #ifdef DEBUGPRAX
4141: printf(" The ratio of the smallest to largest eigenvalue determines whether\n the system is ill conditioned=%d . dmin=%.10lf < m2=%.10lf * d[1]=%.10lf \n",illc, dmin,m2, d[1]);
4142: #endif
4143:
4144: if ((prin > 2) && (scbd > 1.0))
4145: vecprint("\n The scale factors:",z,n);
4146: if (prin > 2)
4147: vecprint(" Principal values (EIGEN VALUES OF A) of the quadratic form:",d,n);
4148: if (prin > 2)
4149: matprint(" The principal axes (EIGEN VECTORS OF A:",v,n, n);
4150:
4151: if ((maxfun > 0) && (nf > maxfun)) {
4152: if (prin)
4153: printf("\n... maximum number of function calls reached ...\n");
4154: goto fret;
4155: }
4156: #ifdef DEBUGPRAX
4157: printf("Goto main loop\n");
4158: #endif
4159: goto mloop; /* back to main loop */
4160:
4161: fret:
4162: if (prin > 0) {
4163: vecprint("\n X:", x, n);
4164: /* printf("\n... ChiSq reduced to %20.10e ...\n", fx); */
4165: /* printf("... after %20u function calls.\n", nf); */
4166: }
4167: free_vector(d, 1, n);
4168: free_vector(y, 1, n);
4169: free_vector(z, 1, n);
4170: free_vector(q0, 1, n);
4171: free_vector(q1, 1, n);
4172: free_matrix(v, 1, n, 1, n);
4173: /* double *d, *y, *z, */
4174: /* *q0, *q1, **v; */
4175: free_vector(tflin, 1, n);
4176: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
4177: free_vector(e, 1, n);
4178: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
4179:
4180: return(fx);
4181: }
4182:
4183: /* end praxis gegen */
1.126 brouard 4184:
4185: /*************** powell ************************/
1.162 brouard 4186: /*
1.317 brouard 4187: Minimization of a function func of n variables. Input consists in an initial starting point
4188: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
4189: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
4190: such that failure to decrease by more than this amount in one iteration signals doneness. On
1.162 brouard 4191: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
4192: function value at p , and iter is the number of iterations taken. The routine linmin is used.
4193: */
1.224 brouard 4194: #ifdef LINMINORIGINAL
4195: #else
4196: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 4197: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 4198: #endif
1.126 brouard 4199: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
4200: double (*func)(double []))
4201: {
1.224 brouard 4202: #ifdef LINMINORIGINAL
4203: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 4204: double (*func)(double []));
1.224 brouard 4205: #else
1.241 brouard 4206: void linmin(double p[], double xi[], int n, double *fret,
4207: double (*func)(double []),int *flat);
1.224 brouard 4208: #endif
1.239 brouard 4209: int i,ibig,j,jk,k;
1.126 brouard 4210: double del,t,*pt,*ptt,*xit;
1.181 brouard 4211: double directest;
1.126 brouard 4212: double fp,fptt;
4213: double *xits;
4214: int niterf, itmp;
1.349 brouard 4215: int Bigter=0, nBigterf=1;
4216:
1.126 brouard 4217: pt=vector(1,n);
4218: ptt=vector(1,n);
4219: xit=vector(1,n);
4220: xits=vector(1,n);
4221: *fret=(*func)(p);
4222: for (j=1;j<=n;j++) pt[j]=p[j];
1.338 brouard 4223: rcurr_time = time(NULL);
4224: fp=(*fret); /* Initialisation */
1.126 brouard 4225: for (*iter=1;;++(*iter)) {
4226: ibig=0;
4227: del=0.0;
1.157 brouard 4228: rlast_time=rcurr_time;
1.349 brouard 4229: rlast_btime=rcurr_time;
1.157 brouard 4230: /* (void) gettimeofday(&curr_time,&tzp); */
4231: rcurr_time = time(NULL);
4232: curr_time = *localtime(&rcurr_time);
1.337 brouard 4233: /* printf("\nPowell iter=%d -2*LL=%.12f gain=%.12f=%.3g %ld sec. %ld sec.",*iter,*fret, fp-*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout); */
4234: /* fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f gain=%.12f=%.3g %ld sec. %ld sec.",*iter,*fret, fp-*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog); */
1.359 brouard 4235: /* Bigter=(*iter - *iter % ncovmodel)/ncovmodel +1; /\* Big iteration, i.e on ncovmodel cycle *\/ */
4236: Bigter=(*iter - (*iter-1) % n)/n +1; /* Big iteration, i.e on ncovmodel cycle */
1.349 brouard 4237: printf("\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
4238: fprintf(ficlog,"\nPowell iter=%d Big Iter=%d -2*LL=%.12f gain=%.3lg %ld sec. %ld sec.",*iter,Bigter,*fret,fp-*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
4239: fprintf(ficrespow,"%d %d %.12f %d",*iter,Bigter, *fret,curr_time.tm_sec-start_time.tm_sec);
1.324 brouard 4240: fp=(*fret); /* From former iteration or initial value */
1.192 brouard 4241: for (i=1;i<=n;i++) {
1.126 brouard 4242: fprintf(ficrespow," %.12lf", p[i]);
4243: }
1.239 brouard 4244: fprintf(ficrespow,"\n");fflush(ficrespow);
4245: printf("\n#model= 1 + age ");
4246: fprintf(ficlog,"\n#model= 1 + age ");
4247: if(nagesqr==1){
1.241 brouard 4248: printf(" + age*age ");
4249: fprintf(ficlog," + age*age ");
1.239 brouard 4250: }
1.362 brouard 4251: for(j=1;j <=ncovmodel-2-nagesqr;j++){
1.239 brouard 4252: if(Typevar[j]==0) {
4253: printf(" + V%d ",Tvar[j]);
4254: fprintf(ficlog," + V%d ",Tvar[j]);
4255: }else if(Typevar[j]==1) {
4256: printf(" + V%d*age ",Tvar[j]);
4257: fprintf(ficlog," + V%d*age ",Tvar[j]);
4258: }else if(Typevar[j]==2) {
4259: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4260: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 4261: }else if(Typevar[j]==3) {
4262: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4263: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.239 brouard 4264: }
4265: }
1.126 brouard 4266: printf("\n");
1.239 brouard 4267: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
4268: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 4269: fprintf(ficlog,"\n");
1.239 brouard 4270: for(i=1,jk=1; i <=nlstate; i++){
4271: for(k=1; k <=(nlstate+ndeath); k++){
4272: if (k != i) {
4273: printf("%d%d ",i,k);
4274: fprintf(ficlog,"%d%d ",i,k);
4275: for(j=1; j <=ncovmodel; j++){
4276: printf("%12.7f ",p[jk]);
4277: fprintf(ficlog,"%12.7f ",p[jk]);
4278: jk++;
4279: }
4280: printf("\n");
4281: fprintf(ficlog,"\n");
4282: }
4283: }
4284: }
1.241 brouard 4285: if(*iter <=3 && *iter >1){
1.157 brouard 4286: tml = *localtime(&rcurr_time);
4287: strcpy(strcurr,asctime(&tml));
4288: rforecast_time=rcurr_time;
1.126 brouard 4289: itmp = strlen(strcurr);
4290: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 4291: strcurr[itmp-1]='\0';
1.162 brouard 4292: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 4293: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.349 brouard 4294: for(nBigterf=1;nBigterf<=31;nBigterf+=10){
4295: niterf=nBigterf*ncovmodel;
4296: /* rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time); */
1.241 brouard 4297: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
4298: forecast_time = *localtime(&rforecast_time);
4299: strcpy(strfor,asctime(&forecast_time));
4300: itmp = strlen(strfor);
4301: if(strfor[itmp-1]=='\n')
4302: strfor[itmp-1]='\0';
1.349 brouard 4303: printf(" - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
4304: fprintf(ficlog," - if your program needs %d BIG iterations (%d iterations) to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",nBigterf, niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
1.126 brouard 4305: }
4306: }
1.359 brouard 4307: for (i=1;i<=n;i++) { /* For each direction i, maximisation after loading directions */
4308: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales. xi is not changed but one dim xit */
4309:
4310: fptt=(*fret); /* Computes likelihood for parameters xit */
1.126 brouard 4311: #ifdef DEBUG
1.203 brouard 4312: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
4313: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 4314: #endif
1.203 brouard 4315: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 4316: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 4317: #ifdef LINMINORIGINAL
1.359 brouard 4318: linmin(p,xit,n,fret,func); /* New point i minimizing in direction xit, i has coordinates p[j].*/
1.357 brouard 4319: /* xit[j] gives the n coordinates of direction i as input.*/
4320: /* *fret gives the maximum value on direction xit */
1.224 brouard 4321: #else
4322: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.359 brouard 4323: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.224 brouard 4324: #endif
1.359 brouard 4325: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 4326: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.359 brouard 4327: /* because that direction will be replaced unless the gain del is small */
4328: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
4329: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
4330: /* with the new direction. */
4331: del=fabs(fptt-(*fret));
4332: ibig=i;
1.126 brouard 4333: }
4334: #ifdef DEBUG
4335: printf("%d %.12e",i,(*fret));
4336: fprintf(ficlog,"%d %.12e",i,(*fret));
4337: for (j=1;j<=n;j++) {
1.359 brouard 4338: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
4339: printf(" x(%d)=%.12e",j,xit[j]);
4340: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 4341: }
4342: for(j=1;j<=n;j++) {
1.359 brouard 4343: printf(" p(%d)=%.12e",j,p[j]);
4344: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 4345: }
4346: printf("\n");
4347: fprintf(ficlog,"\n");
4348: #endif
1.187 brouard 4349: } /* end loop on each direction i */
1.357 brouard 4350: /* Convergence test will use last linmin estimation (fret) and compare to former iteration (fp) */
1.188 brouard 4351: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.359 brouard 4352: /* New value of last point Pn is not computed, P(n-1) */
1.319 brouard 4353: for(j=1;j<=n;j++) {
4354: if(flatdir[j] >0){
4355: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
4356: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
1.302 brouard 4357: }
1.319 brouard 4358: /* printf("\n"); */
4359: /* fprintf(ficlog,"\n"); */
4360: }
1.243 brouard 4361: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
4362: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 4363: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
4364: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
4365: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
4366: /* decreased of more than 3.84 */
4367: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
4368: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
4369: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 4370:
1.188 brouard 4371: /* Starting the program with initial values given by a former maximization will simply change */
4372: /* the scales of the directions and the directions, because the are reset to canonical directions */
4373: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
4374: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 4375: #ifdef DEBUG
4376: int k[2],l;
4377: k[0]=1;
4378: k[1]=-1;
4379: printf("Max: %.12e",(*func)(p));
4380: fprintf(ficlog,"Max: %.12e",(*func)(p));
4381: for (j=1;j<=n;j++) {
4382: printf(" %.12e",p[j]);
4383: fprintf(ficlog," %.12e",p[j]);
4384: }
4385: printf("\n");
4386: fprintf(ficlog,"\n");
4387: for(l=0;l<=1;l++) {
4388: for (j=1;j<=n;j++) {
4389: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
4390: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4391: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4392: }
4393: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4394: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4395: }
4396: #endif
4397:
4398: free_vector(xit,1,n);
4399: free_vector(xits,1,n);
4400: free_vector(ptt,1,n);
4401: free_vector(pt,1,n);
4402: return;
1.192 brouard 4403: } /* enough precision */
1.240 brouard 4404: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.359 brouard 4405: for (j=1;j<=n;j++) { /* Computes the extrapolated point and value f3, P_0 + 2 (P_n-P_0)=2Pn-P0 and xit is direction Pn-P0 */
1.126 brouard 4406: ptt[j]=2.0*p[j]-pt[j];
1.359 brouard 4407: xit[j]=p[j]-pt[j]; /* Coordinate j of last direction xi_n=P_n-P_0 */
4408: #ifdef DEBUG
4409: printf("\n %d xit=%12.7g p=%12.7g pt=%12.7g ",j,xit[j],p[j],pt[j]);
4410: #endif
4411: pt[j]=p[j]; /* New P0 is Pn */
4412: }
4413: #ifdef DEBUG
4414: printf("\n");
4415: #endif
1.181 brouard 4416: fptt=(*func)(ptt); /* f_3 */
1.359 brouard 4417: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in directions until some iterations are done */
1.224 brouard 4418: if (*iter <=4) {
1.225 brouard 4419: #else
4420: #endif
1.224 brouard 4421: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 4422: #else
1.161 brouard 4423: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 4424: #endif
1.162 brouard 4425: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 4426: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 4427: /* Let f"(x2) be the 2nd derivative equal everywhere. */
4428: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
4429: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 4430: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
4431: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
4432: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 4433: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 4434: /* Even if f3 <f1, directest can be negative and t >0 */
4435: /* mu² and del² are equal when f3=f1 */
1.359 brouard 4436: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
4437: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
4438: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
4439: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 4440: #ifdef NRCORIGINAL
4441: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
4442: #else
4443: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
1.161 brouard 4444: t= t- del*SQR(fp-fptt);
1.183 brouard 4445: #endif
1.202 brouard 4446: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 4447: #ifdef DEBUG
1.181 brouard 4448: printf("t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
4449: fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
1.161 brouard 4450: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4451: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4452: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4453: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4454: printf("tt= %.12lf, t=%.12lf\n",2.0*(fp-2.0*(*fret)+fptt)*(fp-(*fret)-del)*(fp-(*fret)-del)-del*(fp-fptt)*(fp-fptt),t);
4455: fprintf(ficlog, "tt= %.12lf, t=%.12lf\n",2.0*(fp-2.0*(*fret)+fptt)*(fp-(*fret)-del)*(fp-(*fret)-del)-del*(fp-fptt)*(fp-fptt),t);
4456: #endif
1.183 brouard 4457: #ifdef POWELLORIGINAL
4458: if (t < 0.0) { /* Then we use it for new direction */
1.361 brouard 4459: #else /* Not POWELLOriginal but Brouard's */
1.182 brouard 4460: if (directest*t < 0.0) { /* Contradiction between both tests */
1.359 brouard 4461: printf("directest= %.12lf (if <0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt,del);
1.192 brouard 4462: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
1.224 brouard 4463: fprintf(ficlog,"directest= %.12lf (if directest<0 or t<0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
1.192 brouard 4464: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
4465: }
1.361 brouard 4466: if (directest < 0.0) { /* Then we use (P0, Pn) for new direction Xi_n or Xi_iBig */
1.181 brouard 4467: #endif
1.191 brouard 4468: #ifdef DEBUGLINMIN
1.234 brouard 4469: printf("Before linmin in direction P%d-P0\n",n);
4470: for (j=1;j<=n;j++) {
4471: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4472: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4473: if(j % ncovmodel == 0){
4474: printf("\n");
4475: fprintf(ficlog,"\n");
4476: }
4477: }
1.224 brouard 4478: #endif
4479: #ifdef LINMINORIGINAL
1.234 brouard 4480: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 4481: #else
1.234 brouard 4482: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
4483: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 4484: #endif
1.234 brouard 4485:
1.191 brouard 4486: #ifdef DEBUGLINMIN
1.234 brouard 4487: for (j=1;j<=n;j++) {
4488: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4489: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4490: if(j % ncovmodel == 0){
4491: printf("\n");
4492: fprintf(ficlog,"\n");
4493: }
4494: }
1.224 brouard 4495: #endif
1.234 brouard 4496: for (j=1;j<=n;j++) {
4497: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
4498: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
4499: }
1.361 brouard 4500:
4501: /* #else */
4502: /* for (i=1;i<=n-1;i++) { */
4503: /* for (j=1;j<=n;j++) { */
4504: /* xi[j][i]=xi[j][i+1]; /\* Standard method of conjugate directions, not Powell who changes the nth direction by p0 pn . *\/ */
4505: /* } */
4506: /* } */
4507: /* for (j=1;j<=n;j++) { */
4508: /* xi[j][n]=xit[j]; /\* and this nth direction by the by the average p_0 p_n *\/ */
4509: /* } */
4510: /* /\* for (j=1;j<=n-1;j++) { *\/ */
4511: /* /\* xi[j][1]=xi[j][j+1]; /\\* Standard method of conjugate directions *\\/ *\/ */
4512: /* /\* xi[j][n]=xit[j]; /\\* and this nth direction by the by the average p_0 p_n *\\/ *\/ */
4513: /* /\* } *\/ */
4514: /* #endif */
1.224 brouard 4515: #ifdef LINMINORIGINAL
4516: #else
1.234 brouard 4517: for (j=1, flatd=0;j<=n;j++) {
4518: if(flatdir[j]>0)
4519: flatd++;
4520: }
4521: if(flatd >0){
1.255 brouard 4522: printf("%d flat directions: ",flatd);
4523: fprintf(ficlog,"%d flat directions :",flatd);
1.234 brouard 4524: for (j=1;j<=n;j++) {
4525: if(flatdir[j]>0){
4526: printf("%d ",j);
4527: fprintf(ficlog,"%d ",j);
4528: }
4529: }
4530: printf("\n");
4531: fprintf(ficlog,"\n");
1.319 brouard 4532: #ifdef FLATSUP
4533: free_vector(xit,1,n);
4534: free_vector(xits,1,n);
4535: free_vector(ptt,1,n);
4536: free_vector(pt,1,n);
4537: return;
4538: #endif
1.361 brouard 4539: } /* endif(flatd >0) */
4540: #endif /* LINMINORIGINAL */
1.234 brouard 4541: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4542: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4543:
1.126 brouard 4544: #ifdef DEBUG
1.234 brouard 4545: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4546: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4547: for(j=1;j<=n;j++){
4548: printf(" %lf",xit[j]);
4549: fprintf(ficlog," %lf",xit[j]);
4550: }
4551: printf("\n");
4552: fprintf(ficlog,"\n");
1.126 brouard 4553: #endif
1.192 brouard 4554: } /* end of t or directest negative */
1.359 brouard 4555: printf(" Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
4556: fprintf(ficlog," Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
1.224 brouard 4557: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 4558: #else
1.234 brouard 4559: } /* end if (fptt < fp) */
1.192 brouard 4560: #endif
1.225 brouard 4561: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 4562: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 4563: #else
1.224 brouard 4564: #endif
1.234 brouard 4565: } /* loop iteration */
1.126 brouard 4566: }
1.234 brouard 4567:
1.126 brouard 4568: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 4569:
1.235 brouard 4570: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres)
1.234 brouard 4571: {
1.338 brouard 4572: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
1.279 brouard 4573: * (and selected quantitative values in nres)
4574: * by left multiplying the unit
4575: * matrix by transitions matrix until convergence is reached with precision ftolpl
4576: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
4577: * Wx is row vector: population in state 1, population in state 2, population dead
4578: * or prevalence in state 1, prevalence in state 2, 0
4579: * newm is the matrix after multiplications, its rows are identical at a factor.
4580: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
4581: * Output is prlim.
4582: * Initial matrix pimij
4583: */
1.206 brouard 4584: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4585: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4586: /* 0, 0 , 1} */
4587: /*
4588: * and after some iteration: */
4589: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4590: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4591: /* 0, 0 , 1} */
4592: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4593: /* {0.51571254859325999, 0.4842874514067399, */
4594: /* 0.51326036147820708, 0.48673963852179264} */
4595: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 4596:
1.332 brouard 4597: int i, ii,j,k, k1;
1.209 brouard 4598: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.366 ! brouard 4599: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b); /* test */ /* for clang */
! 4600: /* double **matprod2(); */ /* test */
! 4601: /* double **out, cov[NCOVMAX+1], **pmij(); */ /* **pmmij is a global variable feeded with oldms etc */
! 4602: double **out, cov[NCOVMAX+1], **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 4603: double **newm;
1.209 brouard 4604: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 4605: int ncvloop=0;
1.288 brouard 4606: int first=0;
1.169 brouard 4607:
1.209 brouard 4608: min=vector(1,nlstate);
4609: max=vector(1,nlstate);
4610: meandiff=vector(1,nlstate);
4611:
1.218 brouard 4612: /* Starting with matrix unity */
1.126 brouard 4613: for (ii=1;ii<=nlstate+ndeath;ii++)
4614: for (j=1;j<=nlstate+ndeath;j++){
4615: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4616: }
1.169 brouard 4617:
4618: cov[1]=1.;
4619:
4620: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 4621: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 4622: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 4623: ncvloop++;
1.126 brouard 4624: newm=savm;
4625: /* Covariates have to be included here again */
1.138 brouard 4626: cov[2]=agefin;
1.319 brouard 4627: if(nagesqr==1){
4628: cov[3]= agefin*agefin;
4629: }
1.332 brouard 4630: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
4631: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
4632: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 4633: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 4634: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
4635: }else{
4636: cov[2+nagesqr+k1]=precov[nres][k1];
4637: }
4638: }/* End of loop on model equation */
4639:
4640: /* Start of old code (replaced by a loop on position in the model equation */
4641: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
4642: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4643: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
4644: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
4645: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
4646: /* * k 1 2 3 4 5 6 7 8 */
4647: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
4648: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
4649: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
4650: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
4651: /* *nsd=3 (1) (2) (3) */
4652: /* *TvarsD[nsd] [1]=2 1 3 */
4653: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
4654: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
4655: /* *Tage[] [1]=1 [2]=2 [3]=3 */
4656: /* *Tvard[] [1][1]=1 [2][1]=1 */
4657: /* * [1][2]=3 [2][2]=2 */
4658: /* *Tprod[](=k) [1]=1 [2]=8 */
4659: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
4660: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
4661: /* *TvarsDpType */
4662: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
4663: /* * nsd=1 (1) (2) */
4664: /* *TvarsD[nsd] 3 2 */
4665: /* *TnsdVar (3)=1 (2)=2 */
4666: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
4667: /* *Tage[] [1]=2 [2]= 3 */
4668: /* *\/ */
4669: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
4670: /* /\* printf("prevalim Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
4671: /* } */
4672: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
4673: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4674: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
4675: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
4676: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
4677: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4678: /* /\* printf("prevalim Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); *\/ */
4679: /* } */
4680: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4681: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
4682: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4683: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
4684: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
4685: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4686: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4687: /* } */
4688: /* /\* printf("prevalim Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
4689: /* } */
4690: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4691: /* /\* printf("prevalim Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); *\/ */
4692: /* if(Dummy[Tvard[k][1]]==0){ */
4693: /* if(Dummy[Tvard[k][2]]==0){ */
4694: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4695: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4696: /* }else{ */
4697: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4698: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
4699: /* } */
4700: /* }else{ */
4701: /* if(Dummy[Tvard[k][2]]==0){ */
4702: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4703: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
4704: /* }else{ */
4705: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4706: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
4707: /* } */
4708: /* } */
4709: /* } /\* End product without age *\/ */
4710: /* ENd of old code */
1.138 brouard 4711: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4712: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4713: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 4714: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4715: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.319 brouard 4716: /* age and covariate values of ij are in 'cov' */
1.142 brouard 4717: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 4718:
1.126 brouard 4719: savm=oldm;
4720: oldm=newm;
1.209 brouard 4721:
4722: for(j=1; j<=nlstate; j++){
4723: max[j]=0.;
4724: min[j]=1.;
4725: }
4726: for(i=1;i<=nlstate;i++){
4727: sumnew=0;
4728: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
4729: for(j=1; j<=nlstate; j++){
4730: prlim[i][j]= newm[i][j]/(1-sumnew);
4731: max[j]=FMAX(max[j],prlim[i][j]);
4732: min[j]=FMIN(min[j],prlim[i][j]);
4733: }
4734: }
4735:
1.126 brouard 4736: maxmax=0.;
1.209 brouard 4737: for(j=1; j<=nlstate; j++){
4738: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
4739: maxmax=FMAX(maxmax,meandiff[j]);
4740: /* printf(" age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, j, meandiff[j],(int)agefin, j, max[j], j, min[j],maxmax); */
1.169 brouard 4741: } /* j loop */
1.203 brouard 4742: *ncvyear= (int)age- (int)agefin;
1.208 brouard 4743: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.126 brouard 4744: if(maxmax < ftolpl){
1.209 brouard 4745: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
4746: free_vector(min,1,nlstate);
4747: free_vector(max,1,nlstate);
4748: free_vector(meandiff,1,nlstate);
1.126 brouard 4749: return prlim;
4750: }
1.288 brouard 4751: } /* agefin loop */
1.208 brouard 4752: /* After some age loop it doesn't converge */
1.288 brouard 4753: if(!first){
4754: first=1;
4755: printf("Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d). Others in log file only...\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
1.317 brouard 4756: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
4757: }else if (first >=1 && first <10){
4758: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
4759: first++;
4760: }else if (first ==10){
4761: fprintf(ficlog, "Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.d years and %d loops. Try to lower 'ftolpl'. Youngest age to start was %d=(%d-%d).\n", (int)age, maxmax, ftolpl, *ncvyear, ncvloop, (int)(agefin+stepm/YEARM), (int)(age-stepm/YEARM), (int)delaymax);
4762: printf("Warning: the stable prevalence dit not converge. This warning came too often, IMaCh will stop notifying, even in its log file. Look at the graphs to appreciate the non convergence.\n");
4763: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
4764: first++;
1.288 brouard 4765: }
4766:
1.359 brouard 4767: /* Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl,
4768: * (int)age, (int)delaymax, (int)agefin, ncvloop,
4769: * (int)age-(int)agefin); */
1.209 brouard 4770: free_vector(min,1,nlstate);
4771: free_vector(max,1,nlstate);
4772: free_vector(meandiff,1,nlstate);
1.208 brouard 4773:
1.169 brouard 4774: return prlim; /* should not reach here */
1.126 brouard 4775: }
4776:
1.217 brouard 4777:
4778: /**** Back Prevalence limit (stable or period prevalence) ****************/
4779:
1.218 brouard 4780: /* double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ageminpar, double agemaxpar, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, double ftolpl, int *ncvyear, int ij) */
4781: /* double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, double ftolpl, int *ncvyear, int ij) */
1.242 brouard 4782: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 4783: {
1.264 brouard 4784: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit
1.217 brouard 4785: matrix by transitions matrix until convergence is reached with precision ftolpl */
4786: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
4787: /* Wx is row vector: population in state 1, population in state 2, population dead */
4788: /* or prevalence in state 1, prevalence in state 2, 0 */
4789: /* newm is the matrix after multiplications, its rows are identical at a factor */
4790: /* Initial matrix pimij */
4791: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4792: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4793: /* 0, 0 , 1} */
4794: /*
4795: * and after some iteration: */
4796: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4797: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4798: /* 0, 0 , 1} */
4799: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4800: /* {0.51571254859325999, 0.4842874514067399, */
4801: /* 0.51326036147820708, 0.48673963852179264} */
4802: /* If we start from prlim again, prlim tends to a constant matrix */
4803:
1.359 brouard 4804: int i, ii,j, k1;
1.247 brouard 4805: int first=0;
1.217 brouard 4806: double *min, *max, *meandiff, maxmax,sumnew=0.;
4807: /* double **matprod2(); */ /* test */
1.366 ! brouard 4808: double **out, cov[NCOVMAX+1], **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij);
! 4809: /* double **out, cov[NCOVMAX+1], **bmij(); */ /* Deprecated in clang */
1.217 brouard 4810: double **newm;
1.218 brouard 4811: double **dnewm, **doldm, **dsavm; /* for use */
4812: double **oldm, **savm; /* for use */
4813:
1.217 brouard 4814: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
4815: int ncvloop=0;
4816:
4817: min=vector(1,nlstate);
4818: max=vector(1,nlstate);
4819: meandiff=vector(1,nlstate);
4820:
1.266 brouard 4821: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
4822: oldm=oldms; savm=savms;
4823:
4824: /* Starting with matrix unity */
4825: for (ii=1;ii<=nlstate+ndeath;ii++)
4826: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 4827: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4828: }
4829:
4830: cov[1]=1.;
4831:
4832: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
4833: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 4834: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
1.288 brouard 4835: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
4836: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 4837: ncvloop++;
1.218 brouard 4838: newm=savm; /* oldm should be kept from previous iteration or unity at start */
4839: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 4840: /* Covariates have to be included here again */
4841: cov[2]=agefin;
1.319 brouard 4842: if(nagesqr==1){
1.217 brouard 4843: cov[3]= agefin*agefin;;
1.319 brouard 4844: }
1.332 brouard 4845: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 4846: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 4847: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.242 brouard 4848: }else{
1.332 brouard 4849: cov[2+nagesqr+k1]=precov[nres][k1];
1.242 brouard 4850: }
1.332 brouard 4851: }/* End of loop on model equation */
4852:
4853: /* Old code */
4854:
4855: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
4856: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4857: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
4858: /* /\* printf("bprevalim Dummy agefin=%.0f combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov[%d]=%lf codtabm(%d,Tvar[%d])=%d \n",agefin,ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],2+nagesqr+TvarsDind[k],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
4859: /* } */
4860: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
4861: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
4862: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
4863: /* /\* /\\* printf("prevalim ij=%d k=%d Tvar[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, Tvar[k],nbcode[Tvar[k]][codtabm(ij,Tvar[k])],cov[2+k], ij, k, codtabm(ij,Tvar[k])]); *\\/ *\/ */
4864: /* /\* } *\/ */
4865: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
4866: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4867: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
4868: /* /\* printf("prevalim Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); *\/ */
4869: /* } */
4870: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
4871: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
4872: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
4873: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4874: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4875: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
4876: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
4877: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4878: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
4879: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4880: /* } */
4881: /* /\* printf("prevalim Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
4882: /* } */
4883: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4884: /* /\* printf("prevalim Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2]); *\/ */
4885: /* if(Dummy[Tvard[k][1]]==0){ */
4886: /* if(Dummy[Tvard[k][2]]==0){ */
4887: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4888: /* }else{ */
4889: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4890: /* } */
4891: /* }else{ */
4892: /* if(Dummy[Tvard[k][2]]==0){ */
4893: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4894: /* }else{ */
4895: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4896: /* } */
4897: /* } */
4898: /* } */
1.217 brouard 4899:
4900: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4901: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4902: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
4903: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4904: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 4905: /* ij should be linked to the correct index of cov */
4906: /* age and covariate values ij are in 'cov', but we need to pass
4907: * ij for the observed prevalence at age and status and covariate
4908: * number: prevacurrent[(int)agefin][ii][ij]
4909: */
4910: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, ageminpar, agemaxpar, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */
4911: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij)); /\* Bug Valgrind *\/ */
4912: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
1.268 brouard 4913: /* if((int)age == 86 || (int)age == 87){ */
1.266 brouard 4914: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
4915: /* for(i=1; i<=nlstate+ndeath; i++) { */
4916: /* printf("%d newm= ",i); */
4917: /* for(j=1;j<=nlstate+ndeath;j++) { */
4918: /* printf("%f ",newm[i][j]); */
4919: /* } */
4920: /* printf("oldm * "); */
4921: /* for(j=1;j<=nlstate+ndeath;j++) { */
4922: /* printf("%f ",oldm[i][j]); */
4923: /* } */
1.268 brouard 4924: /* printf(" bmmij "); */
1.266 brouard 4925: /* for(j=1;j<=nlstate+ndeath;j++) { */
4926: /* printf("%f ",pmmij[i][j]); */
4927: /* } */
4928: /* printf("\n"); */
4929: /* } */
4930: /* } */
1.217 brouard 4931: savm=oldm;
4932: oldm=newm;
1.266 brouard 4933:
1.217 brouard 4934: for(j=1; j<=nlstate; j++){
4935: max[j]=0.;
4936: min[j]=1.;
4937: }
4938: for(j=1; j<=nlstate; j++){
4939: for(i=1;i<=nlstate;i++){
1.234 brouard 4940: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
4941: bprlim[i][j]= newm[i][j];
4942: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
4943: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 4944: }
4945: }
1.218 brouard 4946:
1.217 brouard 4947: maxmax=0.;
4948: for(i=1; i<=nlstate; i++){
1.318 brouard 4949: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
1.217 brouard 4950: maxmax=FMAX(maxmax,meandiff[i]);
4951: /* printf("Back age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, i, meandiff[i],(int)agefin, i, max[i], i, min[i],maxmax); */
1.268 brouard 4952: } /* i loop */
1.217 brouard 4953: *ncvyear= -( (int)age- (int)agefin);
1.268 brouard 4954: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 4955: if(maxmax < ftolpl){
1.220 brouard 4956: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 4957: free_vector(min,1,nlstate);
4958: free_vector(max,1,nlstate);
4959: free_vector(meandiff,1,nlstate);
4960: return bprlim;
4961: }
1.288 brouard 4962: } /* agefin loop */
1.217 brouard 4963: /* After some age loop it doesn't converge */
1.288 brouard 4964: if(!first){
1.247 brouard 4965: first=1;
4966: printf("Warning: the back stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.0f years. Try to lower 'ftolpl'. Others in log file only...\n\
4967: Oldest age to start was %d-%d=%d, ncvloop=%d, ncvyear=%d\n", (int)age, maxmax, ftolpl, delaymax, (int)age, (int)delaymax, (int)agefin, ncvloop, *ncvyear);
4968: }
4969: fprintf(ficlog,"Warning: the back stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.0f years. Try to lower 'ftolpl'. \n\
1.217 brouard 4970: Oldest age to start was %d-%d=%d, ncvloop=%d, ncvyear=%d\n", (int)age, maxmax, ftolpl, delaymax, (int)age, (int)delaymax, (int)agefin, ncvloop, *ncvyear);
4971: /* Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl, (int)age, (int)delaymax, (int)agefin, ncvloop, (int)age-(int)agefin); */
4972: free_vector(min,1,nlstate);
4973: free_vector(max,1,nlstate);
4974: free_vector(meandiff,1,nlstate);
4975:
4976: return bprlim; /* should not reach here */
4977: }
4978:
1.126 brouard 4979: /*************** transition probabilities ***************/
4980:
4981: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
4982: {
1.138 brouard 4983: /* According to parameters values stored in x and the covariate's values stored in cov,
1.266 brouard 4984: computes the probability to be observed in state j (after stepm years) being in state i by appying the
1.138 brouard 4985: model to the ncovmodel covariates (including constant and age).
4986: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
4987: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
4988: ncth covariate in the global vector x is given by the formula:
4989: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
4990: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
4991: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
4992: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1.266 brouard 4993: Outputs ps[i][j] or probability to be observed in j being in i according to
1.138 brouard 4994: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1.266 brouard 4995: Sum on j ps[i][j] should equal to 1.
1.138 brouard 4996: */
4997: double s1, lnpijopii;
1.126 brouard 4998: /*double t34;*/
1.164 brouard 4999: int i,j, nc, ii, jj;
1.126 brouard 5000:
1.223 brouard 5001: for(i=1; i<= nlstate; i++){
5002: for(j=1; j<i;j++){
5003: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5004: /*lnpijopii += param[i][j][nc]*cov[nc];*/
5005: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
5006: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5007: }
5008: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1.330 brouard 5009: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
1.223 brouard 5010: }
5011: for(j=i+1; j<=nlstate+ndeath;j++){
5012: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5013: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
5014: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
5015: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
5016: }
5017: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1.330 brouard 5018: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
1.223 brouard 5019: }
5020: }
1.218 brouard 5021:
1.223 brouard 5022: for(i=1; i<= nlstate; i++){
5023: s1=0;
5024: for(j=1; j<i; j++){
1.339 brouard 5025: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
1.223 brouard 5026: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5027: }
5028: for(j=i+1; j<=nlstate+ndeath; j++){
1.339 brouard 5029: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
1.223 brouard 5030: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5031: }
5032: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5033: ps[i][i]=1./(s1+1.);
5034: /* Computing other pijs */
5035: for(j=1; j<i; j++)
1.325 brouard 5036: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
1.223 brouard 5037: for(j=i+1; j<=nlstate+ndeath; j++)
5038: ps[i][j]= exp(ps[i][j])*ps[i][i];
5039: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5040: } /* end i */
1.218 brouard 5041:
1.223 brouard 5042: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5043: for(jj=1; jj<= nlstate+ndeath; jj++){
5044: ps[ii][jj]=0;
5045: ps[ii][ii]=1;
5046: }
5047: }
1.294 brouard 5048:
5049:
1.223 brouard 5050: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5051: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5052: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5053: /* } */
5054: /* printf("\n "); */
5055: /* } */
5056: /* printf("\n ");printf("%lf ",cov[2]);*/
5057: /*
5058: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 5059: goto end;*/
1.266 brouard 5060: return ps; /* Pointer is unchanged since its call */
1.126 brouard 5061: }
5062:
1.218 brouard 5063: /*************** backward transition probabilities ***************/
5064:
5065: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ageminpar, double agemaxpar, double ***dnewm, double **doldm, double **dsavm, int ij ) */
5066: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
5067: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
5068: {
1.302 brouard 5069: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too.
1.266 brouard 5070: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij.
1.222 brouard 5071: */
1.359 brouard 5072: int ii, j;
1.222 brouard 5073:
1.366 ! brouard 5074: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate);
! 5075: /* double **pmij(); */ /* No more for clang */
1.222 brouard 5076: double sumnew=0.;
1.218 brouard 5077: double agefin;
1.292 brouard 5078: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */
1.222 brouard 5079: double **dnewm, **dsavm, **doldm;
5080: double **bbmij;
5081:
1.218 brouard 5082: doldm=ddoldms; /* global pointers */
1.222 brouard 5083: dnewm=ddnewms;
5084: dsavm=ddsavms;
1.318 brouard 5085:
5086: /* Debug */
5087: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
1.222 brouard 5088: agefin=cov[2];
1.268 brouard 5089: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
1.222 brouard 5090: /* bmij *//* age is cov[2], ij is included in cov, but we need for
1.266 brouard 5091: the observed prevalence (with this covariate ij) at beginning of transition */
5092: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.268 brouard 5093:
5094: /* P_x */
1.325 brouard 5095: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
1.268 brouard 5096: /* outputs pmmij which is a stochastic matrix in row */
5097:
5098: /* Diag(w_x) */
1.292 brouard 5099: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
1.268 brouard 5100: sumnew=0.;
1.269 brouard 5101: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
1.268 brouard 5102: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
1.297 brouard 5103: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
1.268 brouard 5104: sumnew+=prevacurrent[(int)agefin][ii][ij];
5105: }
5106: if(sumnew >0.01){ /* At least some value in the prevalence */
5107: for (ii=1;ii<=nlstate+ndeath;ii++){
5108: for (j=1;j<=nlstate+ndeath;j++)
1.269 brouard 5109: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
1.268 brouard 5110: }
5111: }else{
5112: for (ii=1;ii<=nlstate+ndeath;ii++){
5113: for (j=1;j<=nlstate+ndeath;j++)
5114: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
5115: }
5116: /* if(sumnew <0.9){ */
5117: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
5118: /* } */
5119: }
5120: k3=0.0; /* We put the last diagonal to 0 */
5121: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
5122: doldm[ii][ii]= k3;
5123: }
5124: /* End doldm, At the end doldm is diag[(w_i)] */
5125:
1.292 brouard 5126: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
5127: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
1.268 brouard 5128:
1.292 brouard 5129: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
1.268 brouard 5130: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */
1.222 brouard 5131: for (j=1;j<=nlstate+ndeath;j++){
1.268 brouard 5132: sumnew=0.;
1.222 brouard 5133: for (ii=1;ii<=nlstate;ii++){
1.266 brouard 5134: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
1.268 brouard 5135: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
1.222 brouard 5136: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
1.268 brouard 5137: for (ii=1;ii<=nlstate+ndeath;ii++){
1.222 brouard 5138: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
1.268 brouard 5139: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 5140: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
1.268 brouard 5141: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 5142: /* }else */
1.268 brouard 5143: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
5144: } /*End ii */
5145: } /* End j, At the end dsavm is diag[1/(w_1p1i+w_2 p2i)] for ALL states even if the sum is only for live states */
5146:
1.292 brouard 5147: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
1.268 brouard 5148: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
1.222 brouard 5149: /* end bmij */
1.266 brouard 5150: return ps; /*pointer is unchanged */
1.218 brouard 5151: }
1.217 brouard 5152: /*************** transition probabilities ***************/
5153:
1.218 brouard 5154: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 5155: {
5156: /* According to parameters values stored in x and the covariate's values stored in cov,
5157: computes the probability to be observed in state j being in state i by appying the
5158: model to the ncovmodel covariates (including constant and age).
5159: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
5160: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
5161: ncth covariate in the global vector x is given by the formula:
5162: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
5163: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
5164: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
5165: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
5166: Outputs ps[i][j] the probability to be observed in j being in j according to
5167: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
5168: */
5169: double s1, lnpijopii;
5170: /*double t34;*/
5171: int i,j, nc, ii, jj;
5172:
1.234 brouard 5173: for(i=1; i<= nlstate; i++){
5174: for(j=1; j<i;j++){
5175: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5176: /*lnpijopii += param[i][j][nc]*cov[nc];*/
5177: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
5178: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5179: }
5180: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5181: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5182: }
5183: for(j=i+1; j<=nlstate+ndeath;j++){
5184: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5185: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
5186: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
5187: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
5188: }
5189: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5190: }
5191: }
5192:
5193: for(i=1; i<= nlstate; i++){
5194: s1=0;
5195: for(j=1; j<i; j++){
5196: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5197: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5198: }
5199: for(j=i+1; j<=nlstate+ndeath; j++){
5200: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5201: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5202: }
5203: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5204: ps[i][i]=1./(s1+1.);
5205: /* Computing other pijs */
5206: for(j=1; j<i; j++)
5207: ps[i][j]= exp(ps[i][j])*ps[i][i];
5208: for(j=i+1; j<=nlstate+ndeath; j++)
5209: ps[i][j]= exp(ps[i][j])*ps[i][i];
5210: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5211: } /* end i */
5212:
5213: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5214: for(jj=1; jj<= nlstate+ndeath; jj++){
5215: ps[ii][jj]=0;
5216: ps[ii][ii]=1;
5217: }
5218: }
1.296 brouard 5219: /* Added for prevbcast */ /* Transposed matrix too */
1.234 brouard 5220: for(jj=1; jj<= nlstate+ndeath; jj++){
5221: s1=0.;
5222: for(ii=1; ii<= nlstate+ndeath; ii++){
5223: s1+=ps[ii][jj];
5224: }
5225: for(ii=1; ii<= nlstate; ii++){
5226: ps[ii][jj]=ps[ii][jj]/s1;
5227: }
5228: }
5229: /* Transposition */
5230: for(jj=1; jj<= nlstate+ndeath; jj++){
5231: for(ii=jj; ii<= nlstate+ndeath; ii++){
5232: s1=ps[ii][jj];
5233: ps[ii][jj]=ps[jj][ii];
5234: ps[jj][ii]=s1;
5235: }
5236: }
5237: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5238: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5239: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5240: /* } */
5241: /* printf("\n "); */
5242: /* } */
5243: /* printf("\n ");printf("%lf ",cov[2]);*/
5244: /*
5245: for(i=1; i<= npar; i++) printf("%f ",x[i]);
5246: goto end;*/
5247: return ps;
1.217 brouard 5248: }
5249:
5250:
1.126 brouard 5251: /**************** Product of 2 matrices ******************/
5252:
1.145 brouard 5253: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 5254: {
5255: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
5256: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
5257: /* in, b, out are matrice of pointers which should have been initialized
5258: before: only the contents of out is modified. The function returns
5259: a pointer to pointers identical to out */
1.145 brouard 5260: int i, j, k;
1.126 brouard 5261: for(i=nrl; i<= nrh; i++)
1.145 brouard 5262: for(k=ncolol; k<=ncoloh; k++){
5263: out[i][k]=0.;
5264: for(j=ncl; j<=nch; j++)
5265: out[i][k] +=in[i][j]*b[j][k];
5266: }
1.126 brouard 5267: return out;
5268: }
5269:
5270:
5271: /************* Higher Matrix Product ***************/
5272:
1.235 brouard 5273: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres )
1.126 brouard 5274: {
1.336 brouard 5275: /* Already optimized with precov.
5276: Computes the transition matrix starting at age 'age' and dummies values in each resultline (loop on ij to find the corresponding combination) to over
1.126 brouard 5277: 'nhstepm*hstepm*stepm' months (i.e. until
5278: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5279: nhstepm*hstepm matrices.
5280: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5281: (typically every 2 years instead of every month which is too big
5282: for the memory).
5283: Model is determined by parameters x and covariates have to be
5284: included manually here.
5285:
5286: */
5287:
1.359 brouard 5288: int i, j, d, h, k1;
1.131 brouard 5289: double **out, cov[NCOVMAX+1];
1.126 brouard 5290: double **newm;
1.187 brouard 5291: double agexact;
1.359 brouard 5292: /*double agebegin, ageend;*/
1.126 brouard 5293:
5294: /* Hstepm could be zero and should return the unit matrix */
5295: for (i=1;i<=nlstate+ndeath;i++)
5296: for (j=1;j<=nlstate+ndeath;j++){
5297: oldm[i][j]=(i==j ? 1.0 : 0.0);
5298: po[i][j][0]=(i==j ? 1.0 : 0.0);
5299: }
5300: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5301: for(h=1; h <=nhstepm; h++){
5302: for(d=1; d <=hstepm; d++){
5303: newm=savm;
5304: /* Covariates have to be included here again */
5305: cov[1]=1.;
1.214 brouard 5306: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 5307: cov[2]=agexact;
1.319 brouard 5308: if(nagesqr==1){
1.227 brouard 5309: cov[3]= agexact*agexact;
1.319 brouard 5310: }
1.330 brouard 5311: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
5312: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
5313: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 5314: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 5315: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
5316: }else{
5317: cov[2+nagesqr+k1]=precov[nres][k1];
5318: }
5319: }/* End of loop on model equation */
5320: /* Old code */
5321: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
5322: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
5323: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
5324: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
5325: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
5326: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5327: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5328: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
5329: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
5330: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
5331: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
5332: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
5333: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5334: /* /\* printf("hpxij Dummy combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov=%lf codtabm(%d,TnsdVar[TvarsD[%d])=%d \n",ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,TnsdVar[TvarsD[k]])); *\/ */
5335: /* printf("hpxij Dummy combi=%d k1=%d Tvar[%d]=V%d cov[2+%d+%d]=%lf resultmodel[nres][%d]=%d nres/nresult=%d/%d \n",ij,k1,k1, Tvar[k1],nagesqr,k1,cov[2+nagesqr+k1],k1,resultmodel[nres][k1],nres,nresult); */
5336: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5337: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
5338: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
5339: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
5340: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
5341: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
5342: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
5343: /* printf("hPxij Quantitative k1=%d resultmodel[nres][%d]=%d,Tqresult[%d][%d]=%f\n",k1,k1,resultmodel[nres][k1],nres,resultmodel[nres][k1],Tqresult[nres][resultmodel[nres][k1]]); */
5344: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5345: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
5346: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
5347: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
5348: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
5349: /* printf("DhPxij Dummy with age k1=%d Tvar[%d]=%d TinvDoQresult[nres=%d][%d]=%.f age=%.2f,cov[2+%d+%d]=%.3f\n",k1,k1,Tvar[k1],nres,TinvDoQresult[nres][Tvar[k1]],cov[2],nagesqr,k1,cov[2+nagesqr+k1]); */
5350: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5351:
5352: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
5353: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
5354: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
5355: /* /\* *\/ */
1.330 brouard 5356: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5357: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5358: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
1.332 brouard 5359: /* /\*cptcovage=2 1 2 *\/ */
5360: /* /\*Tage[k]= 5 8 *\/ */
5361: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
5362: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5363: /* printf("QhPxij Quant with age k1=%d resultmodel[nres][%d]=%d,Tqresult[%d][%d]=%f\n",k1,k1,resultmodel[nres][k1],nres,resultmodel[nres][k1],Tqresult[nres][resultmodel[nres][k1]]); */
5364: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5365: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
5366: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
5367: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
5368: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
5369: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
5370: /* /\* printf("hPxij Age combi=%d k=%d cptcovage=%d Tage[%d]=%d Tvar[Tage[%d]]=V%d nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[Tvar[Tage[k]]]])]=%d nres=%d\n",ij,k,cptcovage,k,Tage[k],k,Tvar[Tage[k]], nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[Tvar[Tage[k]]])],nres); *\/ */
5371: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
5372: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
5373: /* /\* } *\/ */
5374: /* /\* printf("hPxij Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
5375: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
5376: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
5377: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
5378: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
5379: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
5380: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
5381: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
5382: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
5383: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
1.330 brouard 5384:
1.332 brouard 5385: /* /\* printf("hPxij Prod ij=%d k=%d Tprod[%d]=%d Tvard[%d][1]=V%d, Tvard[%d][2]=V%d nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]=%d nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]=%d\n",ij,k,k,Tprod[k], k,Tvard[k][1], k,Tvard[k][2],nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])],nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]); *\/ */
5386: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5387: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
5388: /* printf("hPxij Prod ij=%d k1=%d cov[2+%d+%d]=%.5f Tvard[%d][1]=V%d * Tvard[%d][2]=V%d ; TinvDoQresult[nres][Tvardk[k1][1]]=%.4f * TinvDoQresult[nres][Tvardk[k1][1]]=%.4f\n",ij,k1,nagesqr,k1,cov[2+nagesqr+k1],k1,Tvardk[k1][1], k1,Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][1]], TinvDoQresult[nres][Tvardk[k1][2]]); */
5389: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5390:
5391: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
5392: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
5393: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5394: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
5395: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])]; *\/ */
5396: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
5397: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
5398: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
5399: /* /\* } *\/ */
5400: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
5401: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
5402: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
5403: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5404: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
5405: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
5406: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5407: /* /\* } *\/ */
5408: /* /\* }/\\*end of products quantitative *\\/ *\/ */
5409: /* }/\*end of products *\/ */
5410: /* } /\* End of loop on model equation *\/ */
1.235 brouard 5411: /* for (k=1; k<=cptcovn;k++) */
5412: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
5413: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
5414: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
5415: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
5416: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 5417:
5418:
1.126 brouard 5419: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
5420: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.319 brouard 5421: /* right multiplication of oldm by the current matrix */
1.126 brouard 5422: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
5423: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 5424: /* if((int)age == 70){ */
5425: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5426: /* for(i=1; i<=nlstate+ndeath; i++) { */
5427: /* printf("%d pmmij ",i); */
5428: /* for(j=1;j<=nlstate+ndeath;j++) { */
5429: /* printf("%f ",pmmij[i][j]); */
5430: /* } */
5431: /* printf(" oldm "); */
5432: /* for(j=1;j<=nlstate+ndeath;j++) { */
5433: /* printf("%f ",oldm[i][j]); */
5434: /* } */
5435: /* printf("\n"); */
5436: /* } */
5437: /* } */
1.126 brouard 5438: savm=oldm;
5439: oldm=newm;
5440: }
5441: for(i=1; i<=nlstate+ndeath; i++)
5442: for(j=1;j<=nlstate+ndeath;j++) {
1.267 brouard 5443: po[i][j][h]=newm[i][j];
5444: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 5445: }
1.128 brouard 5446: /*printf("h=%d ",h);*/
1.126 brouard 5447: } /* end h */
1.267 brouard 5448: /* printf("\n H=%d \n",h); */
1.126 brouard 5449: return po;
5450: }
5451:
1.217 brouard 5452: /************* Higher Back Matrix Product ***************/
1.218 brouard 5453: /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */
1.267 brouard 5454: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres )
1.217 brouard 5455: {
1.332 brouard 5456: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
5457: computes the transition matrix starting at age 'age' over
1.217 brouard 5458: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 5459: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5460: nhstepm*hstepm matrices.
5461: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5462: (typically every 2 years instead of every month which is too big
1.217 brouard 5463: for the memory).
1.218 brouard 5464: Model is determined by parameters x and covariates have to be
1.266 brouard 5465: included manually here. Then we use a call to bmij(x and cov)
5466: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
1.222 brouard 5467: */
1.217 brouard 5468:
1.359 brouard 5469: int i, j, d, h, k1;
1.366 ! brouard 5470: double **out, cov[NCOVMAX+1], **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij);
! 5471: /* double **out, cov[NCOVMAX+1], **bmij(); */ /* No more for clang */
1.266 brouard 5472: double **newm, ***newmm;
1.217 brouard 5473: double agexact;
1.359 brouard 5474: /*double agebegin, ageend;*/
1.222 brouard 5475: double **oldm, **savm;
1.217 brouard 5476:
1.266 brouard 5477: newmm=po; /* To be saved */
5478: oldm=oldms;savm=savms; /* Global pointers */
1.217 brouard 5479: /* Hstepm could be zero and should return the unit matrix */
5480: for (i=1;i<=nlstate+ndeath;i++)
5481: for (j=1;j<=nlstate+ndeath;j++){
5482: oldm[i][j]=(i==j ? 1.0 : 0.0);
5483: po[i][j][0]=(i==j ? 1.0 : 0.0);
5484: }
5485: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5486: for(h=1; h <=nhstepm; h++){
5487: for(d=1; d <=hstepm; d++){
5488: newm=savm;
5489: /* Covariates have to be included here again */
5490: cov[1]=1.;
1.271 brouard 5491: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
1.217 brouard 5492: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
1.318 brouard 5493: /* Debug */
5494: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
1.217 brouard 5495: cov[2]=agexact;
1.332 brouard 5496: if(nagesqr==1){
1.222 brouard 5497: cov[3]= agexact*agexact;
1.332 brouard 5498: }
5499: /** New code */
5500: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 5501: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 5502: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.325 brouard 5503: }else{
1.332 brouard 5504: cov[2+nagesqr+k1]=precov[nres][k1];
1.325 brouard 5505: }
1.332 brouard 5506: }/* End of loop on model equation */
5507: /** End of new code */
5508: /** This was old code */
5509: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
5510: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
5511: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
5512: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
5513: /* /\* printf("hbxij Dummy agexact=%.0f combi=%d k=%d TvarsD[%d]=V%d TvarsDind[%d]=%d nbcode=%d cov[%d]=%lf codtabm(%d,Tvar[%d])=%d \n",agexact,ij,k, k, TvarsD[k],k,TvarsDind[k],nbcode[TvarsD[k]][codtabm(ij,k)],2+nagesqr+TvarsDind[k],cov[2+nagesqr+TvarsDind[k]], ij, k, codtabm(ij,k)); *\/ */
5514: /* } */
5515: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
5516: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
5517: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
5518: /* /\* printf("hPxij Quantitative k=%d TvarsQind[%d]=%d, TvarsQ[%d]=V%d,Tqresult[%d][%d]=%f\n",k,k,TvarsQind[k],k,TvarsQ[k],nres,k,Tqresult[nres][k]); *\/ */
5519: /* } */
5520: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
5521: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
5522: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
5523: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
5524: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
5525: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
5526: /* } */
5527: /* /\* printf("hBxij Age combi=%d k=%d Tage[%d]=V%d Tqresult[%d][%d]=%f\n",ij,k,k,Tage[k],nres,k,Tqresult[nres][k]); *\/ */
5528: /* } */
5529: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
5530: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
5531: /* if(Dummy[Tvard[k][1]]==0){ */
5532: /* if(Dummy[Tvard[k][2]]==0){ */
5533: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
5534: /* }else{ */
5535: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
5536: /* } */
5537: /* }else{ */
5538: /* if(Dummy[Tvard[k][2]]==0){ */
5539: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
5540: /* }else{ */
5541: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
5542: /* } */
5543: /* } */
5544: /* } */
5545: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
5546: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
5547: /** End of old code */
5548:
1.218 brouard 5549: /* Careful transposed matrix */
1.266 brouard 5550: /* age is in cov[2], prevacurrent at beginning of transition. */
1.218 brouard 5551: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 5552: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 5553: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.325 brouard 5554: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
1.217 brouard 5555: /* if((int)age == 70){ */
5556: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5557: /* for(i=1; i<=nlstate+ndeath; i++) { */
5558: /* printf("%d pmmij ",i); */
5559: /* for(j=1;j<=nlstate+ndeath;j++) { */
5560: /* printf("%f ",pmmij[i][j]); */
5561: /* } */
5562: /* printf(" oldm "); */
5563: /* for(j=1;j<=nlstate+ndeath;j++) { */
5564: /* printf("%f ",oldm[i][j]); */
5565: /* } */
5566: /* printf("\n"); */
5567: /* } */
5568: /* } */
5569: savm=oldm;
5570: oldm=newm;
5571: }
5572: for(i=1; i<=nlstate+ndeath; i++)
5573: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 5574: po[i][j][h]=newm[i][j];
1.268 brouard 5575: /* if(h==nhstepm) */
5576: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
1.217 brouard 5577: }
1.268 brouard 5578: /* printf("h=%d %.1f ",h, agexact); */
1.217 brouard 5579: } /* end h */
1.268 brouard 5580: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
1.217 brouard 5581: return po;
5582: }
5583:
5584:
1.162 brouard 5585: #ifdef NLOPT
5586: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
5587: double fret;
5588: double *xt;
5589: int j;
5590: myfunc_data *d2 = (myfunc_data *) pd;
5591: /* xt = (p1-1); */
5592: xt=vector(1,n);
5593: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
5594:
5595: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
5596: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
5597: printf("Function = %.12lf ",fret);
5598: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
5599: printf("\n");
5600: free_vector(xt,1,n);
5601: return fret;
5602: }
5603: #endif
1.126 brouard 5604:
5605: /*************** log-likelihood *************/
5606: double func( double *x)
5607: {
1.336 brouard 5608: int i, ii, j, k, mi, d, kk, kf=0;
1.226 brouard 5609: int ioffset=0;
1.339 brouard 5610: int ipos=0,iposold=0,ncovv=0;
5611:
1.340 brouard 5612: double cotvarv, cotvarvold;
1.226 brouard 5613: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
5614: double **out;
5615: double lli; /* Individual log likelihood */
5616: int s1, s2;
1.228 brouard 5617: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
1.336 brouard 5618:
1.226 brouard 5619: double bbh, survp;
5620: double agexact;
1.336 brouard 5621: double agebegin, ageend;
1.226 brouard 5622: /*extern weight */
5623: /* We are differentiating ll according to initial status */
5624: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
5625: /*for(i=1;i<imx;i++)
5626: printf(" %d\n",s[4][i]);
5627: */
1.162 brouard 5628:
1.226 brouard 5629: ++countcallfunc;
1.162 brouard 5630:
1.226 brouard 5631: cov[1]=1.;
1.126 brouard 5632:
1.226 brouard 5633: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 5634: ioffset=0;
1.226 brouard 5635: if(mle==1){
5636: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5637: /* Computes the values of the ncovmodel covariates of the model
5638: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
5639: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
5640: to be observed in j being in i according to the model.
5641: */
1.243 brouard 5642: ioffset=2+nagesqr ;
1.233 brouard 5643: /* Fixed */
1.345 brouard 5644: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummy or quant or prod */
1.319 brouard 5645: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
5646: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
5647: /* TvarF[1]=Tvar[6]=2, TvarF[2]=Tvar[7]=7, TvarF[3]=Tvar[9]=1 ID of fixed covariates or product V2, V1*V2, V1 */
1.320 brouard 5648: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.336 brouard 5649: cov[ioffset+TvarFind[kf]]=covar[Tvar[TvarFind[kf]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (TvarFind[1]=6)*/
1.319 brouard 5650: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
1.234 brouard 5651: }
1.226 brouard 5652: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
1.319 brouard 5653: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
1.226 brouard 5654: has been calculated etc */
5655: /* For an individual i, wav[i] gives the number of effective waves */
5656: /* We compute the contribution to Likelihood of each effective transition
5657: mw[mi][i] is real wave of the mi th effectve wave */
5658: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
5659: s2=s[mw[mi+1][i]][i];
1.341 brouard 5660: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i] because now is moved after nvocol+nqv
1.226 brouard 5661: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
5662: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
5663: */
1.336 brouard 5664: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
5665: /* Wave varying (but not age varying) */
1.339 brouard 5666: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates in the model (single and product but no age )"V5+V4+V3+V4*V3+V5*age+V1*age+V1" +TvarVind 1,2,3,4(V4*V3) Tvar[1]@7{5, 4, 3, 6, 5, 1, 1 ; 6 because the created covar is after V5 and is 6, minus 1+1, 3,2,1,4 positions in cotvar*\/ */
5667: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? *\/ */
5668: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
5669: /* } */
1.340 brouard 5670: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age )*/
5671: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
5672: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
1.345 brouard 5673: if(FixedV[itv]!=0){ /* Not a fixed covariate */
1.341 brouard 5674: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* cotvar[wav][ncovcol+nqv+iv][i] */
1.340 brouard 5675: }else{ /* fixed covariate */
1.345 brouard 5676: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
1.340 brouard 5677: }
1.339 brouard 5678: if(ipos!=iposold){ /* Not a product or first of a product */
1.340 brouard 5679: cotvarvold=cotvarv;
5680: }else{ /* A second product */
5681: cotvarv=cotvarv*cotvarvold;
1.339 brouard 5682: }
5683: iposold=ipos;
1.340 brouard 5684: cov[ioffset+ipos]=cotvarv;
1.234 brouard 5685: }
1.339 brouard 5686: /* for products of time varying to be done */
1.234 brouard 5687: for (ii=1;ii<=nlstate+ndeath;ii++)
5688: for (j=1;j<=nlstate+ndeath;j++){
5689: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5690: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5691: }
1.336 brouard 5692:
5693: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
5694: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
1.234 brouard 5695: for(d=0; d<dh[mi][i]; d++){
5696: newm=savm;
5697: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5698: cov[2]=agexact;
5699: if(nagesqr==1)
5700: cov[3]= agexact*agexact; /* Should be changed here */
1.349 brouard 5701: /* for (kk=1; kk<=cptcovage;kk++) { */
5702: /* if(!FixedV[Tvar[Tage[kk]]]) */
5703: /* cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /\* Tage[kk] gives the data-covariate associated with age *\/ */
5704: /* else */
5705: /* cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
5706: /* } */
5707: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
5708: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
5709: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5710: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
5711: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
5712: }else{ /* fixed covariate */
5713: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
5714: }
5715: if(ipos!=iposold){ /* Not a product or first of a product */
5716: cotvarvold=cotvarv;
5717: }else{ /* A second product */
5718: cotvarv=cotvarv*cotvarvold;
5719: }
5720: iposold=ipos;
5721: cov[ioffset+ipos]=cotvarv*agexact;
5722: /* For products */
1.234 brouard 5723: }
1.349 brouard 5724:
1.234 brouard 5725: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5726: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5727: savm=oldm;
5728: oldm=newm;
5729: } /* end mult */
5730:
5731: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
5732: /* But now since version 0.9 we anticipate for bias at large stepm.
5733: * If stepm is larger than one month (smallest stepm) and if the exact delay
5734: * (in months) between two waves is not a multiple of stepm, we rounded to
5735: * the nearest (and in case of equal distance, to the lowest) interval but now
5736: * we keep into memory the bias bh[mi][i] and also the previous matrix product
5737: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
5738: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 5739: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
5740: * -stepm/2 to stepm/2 .
5741: * For stepm=1 the results are the same as for previous versions of Imach.
5742: * For stepm > 1 the results are less biased than in previous versions.
5743: */
1.234 brouard 5744: s1=s[mw[mi][i]][i];
5745: s2=s[mw[mi+1][i]][i];
5746: bbh=(double)bh[mi][i]/(double)stepm;
5747: /* bias bh is positive if real duration
5748: * is higher than the multiple of stepm and negative otherwise.
5749: */
5750: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
5751: if( s2 > nlstate){
5752: /* i.e. if s2 is a death state and if the date of death is known
5753: then the contribution to the likelihood is the probability to
5754: die between last step unit time and current step unit time,
5755: which is also equal to probability to die before dh
5756: minus probability to die before dh-stepm .
5757: In version up to 0.92 likelihood was computed
5758: as if date of death was unknown. Death was treated as any other
5759: health state: the date of the interview describes the actual state
5760: and not the date of a change in health state. The former idea was
5761: to consider that at each interview the state was recorded
5762: (healthy, disable or death) and IMaCh was corrected; but when we
5763: introduced the exact date of death then we should have modified
5764: the contribution of an exact death to the likelihood. This new
5765: contribution is smaller and very dependent of the step unit
5766: stepm. It is no more the probability to die between last interview
5767: and month of death but the probability to survive from last
5768: interview up to one month before death multiplied by the
5769: probability to die within a month. Thanks to Chris
5770: Jackson for correcting this bug. Former versions increased
5771: mortality artificially. The bad side is that we add another loop
5772: which slows down the processing. The difference can be up to 10%
5773: lower mortality.
5774: */
5775: /* If, at the beginning of the maximization mostly, the
5776: cumulative probability or probability to be dead is
5777: constant (ie = 1) over time d, the difference is equal to
5778: 0. out[s1][3] = savm[s1][3]: probability, being at state
5779: s1 at precedent wave, to be dead a month before current
5780: wave is equal to probability, being at state s1 at
5781: precedent wave, to be dead at mont of the current
5782: wave. Then the observed probability (that this person died)
5783: is null according to current estimated parameter. In fact,
5784: it should be very low but not zero otherwise the log go to
5785: infinity.
5786: */
1.183 brouard 5787: /* #ifdef INFINITYORIGINAL */
5788: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5789: /* #else */
5790: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
5791: /* lli=log(mytinydouble); */
5792: /* else */
5793: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5794: /* #endif */
1.226 brouard 5795: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 5796:
1.226 brouard 5797: } else if ( s2==-1 ) { /* alive */
5798: for (j=1,survp=0. ; j<=nlstate; j++)
5799: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
5800: /*survp += out[s1][j]; */
5801: lli= log(survp);
5802: }
1.336 brouard 5803: /* else if (s2==-4) { */
5804: /* for (j=3,survp=0. ; j<=nlstate; j++) */
5805: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5806: /* lli= log(survp); */
5807: /* } */
5808: /* else if (s2==-5) { */
5809: /* for (j=1,survp=0. ; j<=2; j++) */
5810: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5811: /* lli= log(survp); */
5812: /* } */
1.226 brouard 5813: else{
5814: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
5815: /* lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2]));*/ /* linear interpolation */
5816: }
5817: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
5818: /*if(lli ==000.0)*/
1.340 brouard 5819: /* printf("num[i], i=%d, bbh= %f lli=%f savm=%f out=%f %d\n",bbh,lli,savm[s1][s2], out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]],i); */
1.226 brouard 5820: ipmx +=1;
5821: sw += weight[i];
5822: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5823: /* if (lli < log(mytinydouble)){ */
5824: /* printf("Close to inf lli = %.10lf < %.10lf i= %d mi= %d, s[%d][i]=%d s1=%d s2=%d\n", lli,log(mytinydouble), i, mi,mw[mi][i], s[mw[mi][i]][i], s1,s2); */
5825: /* fprintf(ficlog,"Close to inf lli = %.10lf i= %d mi= %d, s[mw[mi][i]][i]=%d\n", lli, i, mi,s[mw[mi][i]][i]); */
5826: /* } */
5827: } /* end of wave */
5828: } /* end of individual */
5829: } else if(mle==2){
5830: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.319 brouard 5831: ioffset=2+nagesqr ;
5832: for (k=1; k<=ncovf;k++)
5833: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
1.226 brouard 5834: for(mi=1; mi<= wav[i]-1; mi++){
1.319 brouard 5835: for(k=1; k <= ncovv ; k++){
1.341 brouard 5836: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
1.319 brouard 5837: }
1.226 brouard 5838: for (ii=1;ii<=nlstate+ndeath;ii++)
5839: for (j=1;j<=nlstate+ndeath;j++){
5840: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5841: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5842: }
5843: for(d=0; d<=dh[mi][i]; d++){
5844: newm=savm;
5845: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5846: cov[2]=agexact;
5847: if(nagesqr==1)
5848: cov[3]= agexact*agexact;
5849: for (kk=1; kk<=cptcovage;kk++) {
5850: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5851: }
5852: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5853: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5854: savm=oldm;
5855: oldm=newm;
5856: } /* end mult */
5857:
5858: s1=s[mw[mi][i]][i];
5859: s2=s[mw[mi+1][i]][i];
5860: bbh=(double)bh[mi][i]/(double)stepm;
5861: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*(savm[s1][s2])):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
5862: ipmx +=1;
5863: sw += weight[i];
5864: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5865: } /* end of wave */
5866: } /* end of individual */
5867: } else if(mle==3){ /* exponential inter-extrapolation */
5868: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5869: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5870: for(mi=1; mi<= wav[i]-1; mi++){
5871: for (ii=1;ii<=nlstate+ndeath;ii++)
5872: for (j=1;j<=nlstate+ndeath;j++){
5873: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5874: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5875: }
5876: for(d=0; d<dh[mi][i]; d++){
5877: newm=savm;
5878: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5879: cov[2]=agexact;
5880: if(nagesqr==1)
5881: cov[3]= agexact*agexact;
5882: for (kk=1; kk<=cptcovage;kk++) {
1.340 brouard 5883: if(!FixedV[Tvar[Tage[kk]]])
5884: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5885: else
1.341 brouard 5886: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
1.226 brouard 5887: }
5888: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5889: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5890: savm=oldm;
5891: oldm=newm;
5892: } /* end mult */
5893:
5894: s1=s[mw[mi][i]][i];
5895: s2=s[mw[mi+1][i]][i];
5896: bbh=(double)bh[mi][i]/(double)stepm;
5897: lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
5898: ipmx +=1;
5899: sw += weight[i];
5900: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5901: } /* end of wave */
5902: } /* end of individual */
5903: }else if (mle==4){ /* ml=4 no inter-extrapolation */
5904: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5905: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5906: for(mi=1; mi<= wav[i]-1; mi++){
5907: for (ii=1;ii<=nlstate+ndeath;ii++)
5908: for (j=1;j<=nlstate+ndeath;j++){
5909: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5910: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5911: }
5912: for(d=0; d<dh[mi][i]; d++){
5913: newm=savm;
5914: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5915: cov[2]=agexact;
5916: if(nagesqr==1)
5917: cov[3]= agexact*agexact;
5918: for (kk=1; kk<=cptcovage;kk++) {
5919: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5920: }
1.126 brouard 5921:
1.226 brouard 5922: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5923: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5924: savm=oldm;
5925: oldm=newm;
5926: } /* end mult */
5927:
5928: s1=s[mw[mi][i]][i];
5929: s2=s[mw[mi+1][i]][i];
5930: if( s2 > nlstate){
5931: lli=log(out[s1][s2] - savm[s1][s2]);
5932: } else if ( s2==-1 ) { /* alive */
5933: for (j=1,survp=0. ; j<=nlstate; j++)
5934: survp += out[s1][j];
5935: lli= log(survp);
5936: }else{
5937: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5938: }
5939: ipmx +=1;
5940: sw += weight[i];
5941: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.343 brouard 5942: /* printf("num[i]=%09ld, i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
1.226 brouard 5943: } /* end of wave */
5944: } /* end of individual */
5945: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
5946: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5947: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5948: for(mi=1; mi<= wav[i]-1; mi++){
5949: for (ii=1;ii<=nlstate+ndeath;ii++)
5950: for (j=1;j<=nlstate+ndeath;j++){
5951: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5952: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5953: }
5954: for(d=0; d<dh[mi][i]; d++){
5955: newm=savm;
5956: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5957: cov[2]=agexact;
5958: if(nagesqr==1)
5959: cov[3]= agexact*agexact;
5960: for (kk=1; kk<=cptcovage;kk++) {
1.340 brouard 5961: if(!FixedV[Tvar[Tage[kk]]])
5962: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5963: else
1.341 brouard 5964: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]*agexact; /* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
1.226 brouard 5965: }
1.126 brouard 5966:
1.226 brouard 5967: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5968: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5969: savm=oldm;
5970: oldm=newm;
5971: } /* end mult */
5972:
5973: s1=s[mw[mi][i]][i];
5974: s2=s[mw[mi+1][i]][i];
5975: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5976: ipmx +=1;
5977: sw += weight[i];
5978: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5979: /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]);*/
5980: } /* end of wave */
5981: } /* end of individual */
5982: } /* End of if */
5983: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
5984: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
5985: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
5986: return -l;
1.126 brouard 5987: }
5988:
5989: /*************** log-likelihood *************/
5990: double funcone( double *x)
5991: {
1.228 brouard 5992: /* Same as func but slower because of a lot of printf and if */
1.359 brouard 5993: int i, ii, j, k, mi, d, kv=0, kf=0;
1.228 brouard 5994: int ioffset=0;
1.339 brouard 5995: int ipos=0,iposold=0,ncovv=0;
5996:
1.340 brouard 5997: double cotvarv, cotvarvold;
1.131 brouard 5998: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 5999: double **out;
6000: double lli; /* Individual log likelihood */
6001: double llt;
6002: int s1, s2;
1.228 brouard 6003: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
6004:
1.126 brouard 6005: double bbh, survp;
1.187 brouard 6006: double agexact;
1.214 brouard 6007: double agebegin, ageend;
1.126 brouard 6008: /*extern weight */
6009: /* We are differentiating ll according to initial status */
6010: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
6011: /*for(i=1;i<imx;i++)
6012: printf(" %d\n",s[4][i]);
6013: */
6014: cov[1]=1.;
6015:
6016: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 6017: ioffset=0;
6018: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.336 brouard 6019: /* Computes the values of the ncovmodel covariates of the model
6020: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
6021: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
6022: to be observed in j being in i according to the model.
6023: */
1.243 brouard 6024: /* ioffset=2+nagesqr+cptcovage; */
6025: ioffset=2+nagesqr;
1.232 brouard 6026: /* Fixed */
1.224 brouard 6027: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 6028: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
1.349 brouard 6029: for (kf=1; kf<=ncovf;kf++){ /* V2 + V3 + V4 Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
1.339 brouard 6030: /* printf("Debug3 TvarFind[%d]=%d",kf, TvarFind[kf]); */
6031: /* printf(" Tvar[TvarFind[kf]]=%d", Tvar[TvarFind[kf]]); */
6032: /* printf(" i=%d covar[Tvar[TvarFind[kf]]][i]=%f\n",i,covar[Tvar[TvarFind[kf]]][i]); */
1.335 brouard 6033: cov[ioffset+TvarFind[kf]]=covar[Tvar[TvarFind[kf]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
1.232 brouard 6034: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
6035: /* cov[2+6]=covar[Tvar[6]][i]; */
6036: /* cov[2+6]=covar[2][i]; V2 */
6037: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
6038: /* cov[2+7]=covar[Tvar[7]][i]; */
6039: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
6040: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
6041: /* cov[2+9]=covar[Tvar[9]][i]; */
6042: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 6043: }
1.336 brouard 6044: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
6045: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
6046: has been calculated etc */
6047: /* For an individual i, wav[i] gives the number of effective waves */
6048: /* We compute the contribution to Likelihood of each effective transition
6049: mw[mi][i] is real wave of the mi th effectve wave */
6050: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
6051: s2=s[mw[mi+1][i]][i];
1.341 brouard 6052: And the iv th varying covariate in the DATA is the cotvar[mw[mi+1][i]][ncovcol+nqv+iv][i]
1.336 brouard 6053: */
6054: /* This part may be useless now because everythin should be in covar */
1.232 brouard 6055: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
6056: /* cov[++ioffset]=coqvar[TvarFQ[k]][i];/\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V2 and V1*V2 is fixed (k=6 and 7?)*\/ */
6057: /* } */
1.231 brouard 6058: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
6059: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
6060: /* } */
1.225 brouard 6061:
1.233 brouard 6062:
6063: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.339 brouard 6064: /* Wave varying (but not age varying) *//* V1+V3+age*V1+age*V3+V1*V3 with V4 tv and V5 tvq k= 1 to 5 and extra at V(5+1)=6 for V1*V3 */
6065: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates (single and product but no age )*\/ */
6066: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; *\/ */
6067: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
6068: /* } */
6069:
6070: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
6071: /* model V1+V3+age*V1+age*V3+V1*V3 */
6072: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
6073: /* TvarVV[1]=V3 (first time varying in the model equation, TvarVV[2]=V1 (in V1*V3) TvarVV[3]=3(V3) */
6074: /* We need the position of the time varying or product in the model */
6075: /* TvarVVind={2,5,5}, for V3 at position 2 and then the product V1*V3 is decomposed into V1 and V3 but at same position 5 */
6076: /* TvarVV gives the variable name */
1.340 brouard 6077: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
6078: * k= 1 2 3 4 5 6 7 8 9
6079: * varying 1 2 3 4 5
6080: * ncovv 1 2 3 4 5 6 7 8
1.343 brouard 6081: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
1.340 brouard 6082: * TvarVVind 2 3 7 7 8 8 9 9
6083: * TvarFind[k] 1 0 0 0 0 0 0 0 0
6084: */
1.345 brouard 6085: /* Other model ncovcol=5 nqv=0 ntv=3 nqtv=0 nlstate=3
1.349 brouard 6086: * V2 V3 V4 are fixed V6 V7 are timevarying so V8 and V5 are not in the model and product column will start at 9 Tvar[(v6*V2)6]=9
1.345 brouard 6087: * FixedV[ncovcol+qv+ntv+nqtv] V5
1.349 brouard 6088: * 3 V1 V2 V3 V4 V5 V6 V7 V8 V3*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6089: * 0 0 0 0 0 1 1 1 0, 0, 1,1, 1, 0, 1, 0, 1, 0, 1, 0}
6090: * 3 0 0 0 0 0 1 1 1 0, 1 1 1 1 1}
6091: * model= V2 + V3 + V4 + V6 + V7 + V6*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6092: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6093: * +age*V6*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6094: * model2= V2 + V3 + V4 + V6 + V7 + V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6095: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6096: * +age*V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6097: * model3= V2 + V3 + V4 + V6 + V7 + age*V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6098: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6099: * +V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6100: * kmodel 1 2 3 4 5 6 7 8 9 10 11
6101: * 12 13 14 15 16
6102: * 17 18 19 20 21
6103: * Tvar[kmodel] 2 3 4 6 7 9 10 11 12 13 14
6104: * 2 3 4 6 7
6105: * 9 11 12 13 14
6106: * cptcovage=5+5 total of covariates with age
6107: * Tage[cptcovage] age*V2=12 13 14 15 16
6108: *1 17 18 19 20 21 gives the position in model of covariates associated with age
6109: *3 Tage[cptcovage] age*V3*V2=6
6110: *3 age*V2=12 13 14 15 16
6111: *3 age*V6*V3=18 19 20 21
6112: * Tvar[Tage[cptcovage]] Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6113: * Tvar[17]age*V6*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6114: * 2 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6115: * 3 Tvar[Tage[cptcovage]] Tvar[6]=9 Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6116: * 3 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6117: * 3 Tage[cptcovage] age*V3*V2=6 age*V2=12 age*V3 13 14 15 16
6118: * age*V6*V3=18 19 20 21 gives the position in model of covariates associated with age
6119: * 3 Tvar[17]age*V3*V2=9 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6120: * Tvar= {2, 3, 4, 6, 7,
6121: * 9, 10, 11, 12, 13, 14,
6122: * Tvar[12]=2, 3, 4, 6, 7,
6123: * Tvar[17]=9, 11, 12, 13, 14}
6124: * Typevar[1]@21 = {0, 0, 0, 0, 0,
6125: * 2, 2, 2, 2, 2, 2,
6126: * 3 3, 2, 2, 2, 2, 2,
6127: * 1, 1, 1, 1, 1,
6128: * 3, 3, 3, 3, 3}
6129: * 3 2, 3, 3, 3, 3}
6130: * p Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6} Id of the prod at position k in the model
6131: * p Tprod[1]@21 {6, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6132: * 3 Tposprod[1]@21 {0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 0, 0, 0, 0, 0, 1, 3, 4, 5, 6}
6133: * 3 Tprod[1]@21 {17, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6134: * cptcovprod=11 (6+5)
6135: * FixedV[Tvar[Tage[cptcovage]]]] FixedV[2]=0 FixedV[3]=0 0 1 (age*V7)Tvar[16]=1 FixedV[absolute] not [kmodel]
6136: * FixedV[Tvar[17]=FixedV[age*V6*V2]=FixedV[9]=1 1 1 1 1
6137: * 3 FixedV[Tvar[17]=FixedV[age*V3*V2]=FixedV[9]=0 [11]=1 1 1 1
6138: * FixedV[] V1=0 V2=0 V3=0 v4=0 V5=0 V6=1 V7=1 v8=1 OK then model dependent
6139: * 9=1 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6140: * 3 9=0 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6141: * cptcovdageprod=5 for gnuplot printing
6142: * cptcovprodvage=6
6143: * ncova=15 1 2 3 4 5
6144: * 6 7 8 9 10 11 12 13 14 15
6145: * TvarA 2 3 4 6 7
6146: * 6 2 6 7 7 3 6 4 7 4
6147: * TvaAind 12 12 13 13 14 14 15 15 16 16
1.345 brouard 6148: * ncovf 1 2 3
1.349 brouard 6149: * V6 V7 V6*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6150: * ncovvt=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6151: * TvarVV[1]@14 = itv {V6=6, 7, V6*V2=6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6152: * TvarVVind[1]@14= {4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11}
6153: * 3 ncovvt=12 V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6154: * 3 TvarVV[1]@12 = itv {6, 7, V7*V2=7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6155: * 3 1 2 3 4 5 6 7 8 9 10 11 12
6156: * TvarVVind[1]@12= {V6 is in k=4, 5, 7,(4isV2)=7, 8, 8, 9, 9, 10,10, 11,11}TvarVVind[12]=k=11
6157: * TvarV 6, 7, 9, 10, 11, 12, 13, 14
6158: * 3 cptcovprodvage=6
6159: * 3 ncovta=15 +age*V3*V2+age*V2+agev3+ageV4 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6160: * 3 TvarAVVA[1]@15= itva 3 2 2 3 4 6 7 6 3 7 3 6 4 7 4
6161: * 3 ncovta 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1.354 brouard 6162: *?TvarAVVAind[1]@15= V3 is in k=2 1 1 2 3 4 5 4,2 5,2, 4,3 5 3}TvarVVAind[]
1.349 brouard 6163: * TvarAVVAind[1]@15= V3 is in k=6 6 12 13 14 15 16 18 18 19,19, 20,20 21,21}TvarVVAind[]
6164: * 3 ncovvta=10 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6165: * 3 we want to compute =cotvar[mw[mi][i]][TvarVVA[ncovva]][i] at position TvarVVAind[ncovva]
6166: * 3 TvarVVA[1]@10= itva 6 7 6 3 7 3 6 4 7 4
6167: * 3 ncovva 1 2 3 4 5 6 7 8 9 10
6168: * TvarVVAind[1]@10= V6 is in k=4 5 8,8 9, 9, 10,10 11 11}TvarVVAind[]
6169: * TvarVVAind[1]@10= 15 16 18,18 19,19, 20,20 21 21}TvarVVAind[]
6170: * TvarVA V3*V2=6 6 , 1, 2, 11, 12, 13, 14
1.345 brouard 6171: * TvarFind[1]@14= {1, 2, 3, 0 <repeats 12 times>}
1.349 brouard 6172: * Tvar[1]@21= {2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14,
6173: * 2, 3, 4, 6, 7,
6174: * 6, 8, 9, 10, 11}
1.345 brouard 6175: * TvarFind[itv] 0 0 0
6176: * FixedV[itv] 1 1 1 0 1 0 1 0 1 0 0
1.354 brouard 6177: *? FixedV[itv] 1 1 1 0 1 0 1 0 1 0 1 0 1 0
1.345 brouard 6178: * Tvar[TvarFind[ncovf]]=[1]=2 [2]=3 [4]=4
6179: * Tvar[TvarFind[itv]] [0]=? ?ncovv 1 à ncovvt]
6180: * Not a fixed cotvar[mw][itv][i] 6 7 6 2 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
1.349 brouard 6181: * fixed covar[itv] [6] [7] [6][2]
1.345 brouard 6182: */
6183:
1.349 brouard 6184: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4 Time varying covariates (single and extended product but no age) including individual from products, product is computed dynamically */
6185: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, or fixed covariate of a varying product after exploding product Vn*Vm into Vn and then Vm */
1.340 brouard 6186: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
1.345 brouard 6187: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6188: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
1.354 brouard 6189: /* printf("DEBUG ncovv=%d, Varying TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
1.345 brouard 6190: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
1.354 brouard 6191: /* printf("DEBUG Varying cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
1.340 brouard 6192: }else{ /* fixed covariate */
1.345 brouard 6193: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
1.354 brouard 6194: /* printf("DEBUG ncovv=%d, Fixed TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
1.349 brouard 6195: cotvarv=covar[itv][i]; /* Good: In V6*V3, 3 is fixed at position of the data */
1.354 brouard 6196: /* printf("DEBUG Fixed cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
1.340 brouard 6197: }
1.339 brouard 6198: if(ipos!=iposold){ /* Not a product or first of a product */
1.340 brouard 6199: cotvarvold=cotvarv;
6200: }else{ /* A second product */
6201: cotvarv=cotvarv*cotvarvold;
1.339 brouard 6202: }
6203: iposold=ipos;
1.340 brouard 6204: cov[ioffset+ipos]=cotvarv;
1.354 brouard 6205: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
1.339 brouard 6206: /* For products */
6207: }
6208: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates single *\/ */
6209: /* iv=TvarVDind[itv]; /\* iv, position in the model equation of time varying covariate itv *\/ */
6210: /* /\* "V1+V3+age*V1+age*V3+V1*V3" with V3 time varying *\/ */
6211: /* /\* 1 2 3 4 5 *\/ */
6212: /* /\*itv 1 *\/ */
6213: /* /\* TvarVInd[1]= 2 *\/ */
6214: /* /\* iv= Tvar[Tmodelind[itv]]-ncovcol-nqv; /\\* Counting the # varying covariate from 1 to ntveff *\\/ *\/ */
6215: /* /\* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; *\/ */
6216: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; *\/ */
6217: /* /\* k=ioffset-2-nagesqr-cptcovage+itv; /\\* position in simple model *\\/ *\/ */
6218: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; *\/ */
6219: /* cov[ioffset+iv]=cotvar[mw[mi][i]][itv][i]; */
6220: /* /\* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][itv][i]=%f\n", i, mi, itv, TvarVDind[itv],cotvar[mw[mi][i]][itv][i]); *\/ */
6221: /* } */
1.232 brouard 6222: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 6223: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
6224: /* /\* printf(" i=%d,mi=%d,iqtv=%d,TmodelInvQind[iqtv]=%d,cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]=%f\n", i, mi, iqtv, TmodelInvQind[iqtv],cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]); *\/ */
6225: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 6226: /* } */
1.126 brouard 6227: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 6228: for (j=1;j<=nlstate+ndeath;j++){
6229: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
6230: savm[ii][j]=(ii==j ? 1.0 : 0.0);
6231: }
1.214 brouard 6232:
6233: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
6234: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
6235: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.247 brouard 6236: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
1.242 brouard 6237: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
6238: and mw[mi+1][i]. dh depends on stepm.*/
6239: newm=savm;
1.247 brouard 6240: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
1.242 brouard 6241: cov[2]=agexact;
6242: if(nagesqr==1)
6243: cov[3]= agexact*agexact;
1.349 brouard 6244: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6245: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6246: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6247: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6248: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6249: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6250: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6251: }else{ /* fixed covariate */
6252: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6253: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6254: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6255: }
6256: if(ipos!=iposold){ /* Not a product or first of a product */
6257: cotvarvold=cotvarv;
6258: }else{ /* A second product */
6259: /* printf("DEBUG * \n"); */
6260: cotvarv=cotvarv*cotvarvold;
6261: }
6262: iposold=ipos;
6263: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6264: cov[ioffset+ipos]=cotvarv*agexact;
6265: /* For products */
1.242 brouard 6266: }
1.349 brouard 6267:
1.242 brouard 6268: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
6269: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
6270: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
6271: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
6272: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
6273: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
6274: savm=oldm;
6275: oldm=newm;
1.126 brouard 6276: } /* end mult */
1.336 brouard 6277: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
6278: /* But now since version 0.9 we anticipate for bias at large stepm.
6279: * If stepm is larger than one month (smallest stepm) and if the exact delay
6280: * (in months) between two waves is not a multiple of stepm, we rounded to
6281: * the nearest (and in case of equal distance, to the lowest) interval but now
6282: * we keep into memory the bias bh[mi][i] and also the previous matrix product
6283: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
6284: * probability in order to take into account the bias as a fraction of the way
6285: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
6286: * -stepm/2 to stepm/2 .
6287: * For stepm=1 the results are the same as for previous versions of Imach.
6288: * For stepm > 1 the results are less biased than in previous versions.
6289: */
1.126 brouard 6290: s1=s[mw[mi][i]][i];
6291: s2=s[mw[mi+1][i]][i];
1.217 brouard 6292: /* if(s2==-1){ */
1.268 brouard 6293: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
1.217 brouard 6294: /* /\* exit(1); *\/ */
6295: /* } */
1.126 brouard 6296: bbh=(double)bh[mi][i]/(double)stepm;
6297: /* bias is positive if real duration
6298: * is higher than the multiple of stepm and negative otherwise.
6299: */
6300: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 6301: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 6302: } else if ( s2==-1 ) { /* alive */
1.242 brouard 6303: for (j=1,survp=0. ; j<=nlstate; j++)
6304: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
6305: lli= log(survp);
1.126 brouard 6306: }else if (mle==1){
1.242 brouard 6307: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 6308: } else if(mle==2){
1.242 brouard 6309: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
1.126 brouard 6310: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 6311: lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
1.126 brouard 6312: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 6313: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 6314: } else{ /* mle=0 back to 1 */
1.242 brouard 6315: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
6316: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 6317: } /* End of if */
6318: ipmx +=1;
6319: sw += weight[i];
6320: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.342 brouard 6321: /* Printing covariates values for each contribution for checking */
1.343 brouard 6322: /* printf("num[i]=%09ld, i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",num[i],i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
1.126 brouard 6323: if(globpr){
1.246 brouard 6324: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 6325: %11.6f %11.6f %11.6f ", \
1.242 brouard 6326: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
1.268 brouard 6327: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
1.343 brouard 6328: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
6329: /* %11.6f %11.6f %11.6f ", \ */
6330: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
6331: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
1.242 brouard 6332: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
6333: llt +=ll[k]*gipmx/gsw;
6334: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
1.335 brouard 6335: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
1.242 brouard 6336: }
1.343 brouard 6337: fprintf(ficresilk," %10.6f ", -llt);
1.335 brouard 6338: /* printf(" %10.6f\n", -llt); */
1.342 brouard 6339: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
1.343 brouard 6340: /* fprintf(ficresilk,"%09ld ", num[i]); */ /* not necessary */
6341: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
6342: fprintf(ficresilk," %g",covar[Tvar[TvarFind[kf]]][i]);
6343: }
6344: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
6345: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6346: if(ipos!=iposold){ /* Not a product or first of a product */
6347: fprintf(ficresilk," %g",cov[ioffset+ipos]);
6348: /* printf(" %g",cov[ioffset+ipos]); */
6349: }else{
6350: fprintf(ficresilk,"*");
6351: /* printf("*"); */
1.342 brouard 6352: }
1.343 brouard 6353: iposold=ipos;
6354: }
1.349 brouard 6355: /* for (kk=1; kk<=cptcovage;kk++) { */
6356: /* if(!FixedV[Tvar[Tage[kk]]]){ */
6357: /* fprintf(ficresilk," %g*age",covar[Tvar[Tage[kk]]][i]); */
6358: /* /\* printf(" %g*age",covar[Tvar[Tage[kk]]][i]); *\/ */
6359: /* }else{ */
6360: /* fprintf(ficresilk," %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6361: /* /\* printf(" %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\\/ *\/ */
6362: /* } */
6363: /* } */
6364: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6365: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6366: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6367: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6368: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6369: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6370: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6371: }else{ /* fixed covariate */
6372: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6373: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6374: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6375: }
6376: if(ipos!=iposold){ /* Not a product or first of a product */
6377: cotvarvold=cotvarv;
6378: }else{ /* A second product */
6379: /* printf("DEBUG * \n"); */
6380: cotvarv=cotvarv*cotvarvold;
1.342 brouard 6381: }
1.349 brouard 6382: cotvarv=cotvarv*agexact;
6383: fprintf(ficresilk," %g*age",cotvarv);
6384: iposold=ipos;
6385: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6386: cov[ioffset+ipos]=cotvarv;
6387: /* For products */
1.343 brouard 6388: }
6389: /* printf("\n"); */
1.342 brouard 6390: /* } /\* End debugILK *\/ */
6391: fprintf(ficresilk,"\n");
6392: } /* End if globpr */
1.335 brouard 6393: } /* end of wave */
6394: } /* end of individual */
6395: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
1.232 brouard 6396: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
1.335 brouard 6397: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
6398: if(globpr==0){ /* First time we count the contributions and weights */
6399: gipmx=ipmx;
6400: gsw=sw;
6401: }
1.343 brouard 6402: return -l;
1.126 brouard 6403: }
6404:
6405:
6406: /*************** function likelione ***********/
1.292 brouard 6407: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
1.126 brouard 6408: {
6409: /* This routine should help understanding what is done with
6410: the selection of individuals/waves and
6411: to check the exact contribution to the likelihood.
6412: Plotting could be done.
1.342 brouard 6413: */
6414: void pstamp(FILE *ficres);
1.343 brouard 6415: int k, kf, kk, kvar, ncovv, iposold, ipos;
1.126 brouard 6416:
6417: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 6418: strcpy(fileresilk,"ILK_");
1.202 brouard 6419: strcat(fileresilk,fileresu);
1.126 brouard 6420: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
6421: printf("Problem with resultfile: %s\n", fileresilk);
6422: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
6423: }
1.342 brouard 6424: pstamp(ficresilk);fprintf(ficresilk,"# model=1+age+%s\n",model);
1.214 brouard 6425: fprintf(ficresilk, "#individual(line's_record) count ageb ageend s1 s2 wave# effective_wave# number_of_matrices_product pij weight weight/gpw -2ln(pij)*weight 0pij_x 0pij_(x-stepm) cumulating_loglikeli_by_health_state(reweighted=-2ll*weightXnumber_of_contribs/sum_of_weights) and_total\n");
6426: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 6427: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
6428: for(k=1; k<=nlstate; k++)
6429: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
1.342 brouard 6430: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total) ");
6431:
6432: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
6433: for(kf=1;kf <= ncovf; kf++){
6434: fprintf(ficresilk,"V%d",Tvar[TvarFind[kf]]);
6435: /* printf("V%d",Tvar[TvarFind[kf]]); */
6436: }
6437: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){
1.343 brouard 6438: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
1.342 brouard 6439: if(ipos!=iposold){ /* Not a product or first of a product */
6440: /* printf(" %d",ipos); */
6441: fprintf(ficresilk," V%d",TvarVV[ncovv]);
6442: }else{
6443: /* printf("*"); */
6444: fprintf(ficresilk,"*");
1.343 brouard 6445: }
1.342 brouard 6446: iposold=ipos;
6447: }
6448: for (kk=1; kk<=cptcovage;kk++) {
6449: if(!FixedV[Tvar[Tage[kk]]]){
6450: /* printf(" %d*age(Fixed)",Tvar[Tage[kk]]); */
6451: fprintf(ficresilk," %d*age(Fixed)",Tvar[Tage[kk]]);
6452: }else{
6453: fprintf(ficresilk," %d*age(Varying)",Tvar[Tage[kk]]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
6454: /* printf(" %d*age(Varying)",Tvar[Tage[kk]]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6455: }
6456: }
6457: /* } /\* End if debugILK *\/ */
6458: /* printf("\n"); */
6459: fprintf(ficresilk,"\n");
6460: } /* End glogpri */
1.126 brouard 6461:
1.292 brouard 6462: *fretone=(*func)(p);
1.126 brouard 6463: if(*globpri !=0){
6464: fclose(ficresilk);
1.205 brouard 6465: if (mle ==0)
6466: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
6467: else if(mle >=1)
6468: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
6469: fprintf(fichtm," You should at least run with mle >= 1 to get starting values corresponding to the optimized parameters in order to visualize the real contribution of each individual/wave: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
1.274 brouard 6470: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
1.208 brouard 6471:
1.207 brouard 6472: fprintf(fichtm,"<br>- The function drawn is -2Log(L) in Log scale: by state of origin <a href=\"%s-ori.png\">%s-ori.png</a><br> \
1.343 brouard 6473: <img src=\"%s-ori.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 6474: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.343 brouard 6475: <img src=\"%s-dest.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
6476:
6477: for (k=1; k<= nlstate ; k++) {
6478: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j. Dot's sizes are related to corresponding weight: <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br>\n \
6479: <img src=\"%s-p%dj.png\">\n",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
6480: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
1.350 brouard 6481: kvar=Tvar[TvarFind[kf]]; /* variable */
6482: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored covariate V%d. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): ",k,k,Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],Tvar[TvarFind[kf]]);
6483: fprintf(fichtm,"<a href=\"%s-p%dj-%d.png\">%s-p%dj-%d.png</a><br>",subdirf2(optionfilefiname,"ILK_"),k,kvar,subdirf2(optionfilefiname,"ILK_"),k,kvar);
6484: fprintf(fichtm,"<img src=\"%s-p%dj-%d.png\">",subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]);
1.343 brouard 6485: }
6486: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Loop on the time varying extended covariates (with extension of Vn*Vm */
6487: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
6488: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
6489: /* printf("DebugILK fichtm ncovv=%d, kvar=TvarVV[ncovv]=V%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); */
6490: if(ipos!=iposold){ /* Not a product or first of a product */
6491: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
6492: /* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); */
6493: if(Dummy[ipos]==0 && Typevar[ipos]==0){ /* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) */
6494: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored time varying dummy covariate V%d. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \
6495: <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar);
6496: } /* End only for dummies time varying (single?) */
6497: }else{ /* Useless product */
6498: /* printf("*"); */
6499: /* fprintf(ficresilk,"*"); */
6500: }
6501: iposold=ipos;
6502: } /* For each time varying covariate */
6503: } /* End loop on states */
6504:
6505: /* if(debugILK){ */
6506: /* for(kf=1; kf <= ncovf; kf++){ /\* For each simple dummy covariate of the model *\/ */
6507: /* /\* kvar=Tvar[TvarFind[kf]]; *\/ /\* variable *\/ */
6508: /* for (k=1; k<= nlstate ; k++) { */
6509: /* fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j with colored covariate V%. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \ */
6510: /* <img src=\"%s-p%dj-%d.png\">",k,k,Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],Tvar[TvarFind[kf]],subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]); */
6511: /* } */
6512: /* } */
6513: /* for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /\* Loop on the time varying extended covariates (with extension of Vn*Vm *\/ */
6514: /* ipos=TvarVVind[ncovv]; /\* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate *\/ */
6515: /* kvar=TvarVV[ncovv]; /\* TvarVV={3, 1, 3} gives the name of each varying covariate *\/ */
6516: /* /\* printf("DebugILK fichtm ncovv=%d, kvar=TvarVV[ncovv]=V%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); *\/ */
6517: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
6518: /* /\* fprintf(ficresilk," V%d",TvarVV[ncovv]); *\/ */
6519: /* /\* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); *\/ */
6520: /* if(Dummy[ipos]==0 && Typevar[ipos]==0){ /\* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) *\/ */
6521: /* for (k=1; k<= nlstate ; k++) { */
6522: /* fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j. Same dot size of all points but with a different color for transitions with dummy variable V%d=1 at beginning of transition (keeping former color for V%d=0): <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \ */
6523: /* <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar); */
6524: /* } /\* End state *\/ */
6525: /* } /\* End only for dummies time varying (single?) *\/ */
6526: /* }else{ /\* Useless product *\/ */
6527: /* /\* printf("*"); *\/ */
6528: /* /\* fprintf(ficresilk,"*"); *\/ */
6529: /* } */
6530: /* iposold=ipos; */
6531: /* } /\* For each time varying covariate *\/ */
6532: /* }/\* End debugILK *\/ */
1.207 brouard 6533: fflush(fichtm);
1.343 brouard 6534: }/* End globpri */
1.126 brouard 6535: return;
6536: }
6537:
6538:
6539: /*********** Maximum Likelihood Estimation ***************/
6540:
6541: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
6542: {
1.359 brouard 6543: int i,j, jkk=0, iter=0;
1.126 brouard 6544: double **xi;
1.359 brouard 6545: /*double fret;*/
6546: /*double fretone;*/ /* Only one call to likelihood */
1.126 brouard 6547: /* char filerespow[FILENAMELENGTH];*/
1.354 brouard 6548:
1.359 brouard 6549: /*double * p1;*/ /* Shifted parameters from 0 instead of 1 */
1.162 brouard 6550: #ifdef NLOPT
6551: int creturn;
6552: nlopt_opt opt;
6553: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
6554: double *lb;
6555: double minf; /* the minimum objective value, upon return */
1.354 brouard 6556:
1.162 brouard 6557: myfunc_data dinst, *d = &dinst;
6558: #endif
6559:
6560:
1.126 brouard 6561: xi=matrix(1,npar,1,npar);
1.357 brouard 6562: for (i=1;i<=npar;i++) /* Starting with canonical directions j=1,n xi[i=1,n][j] */
1.126 brouard 6563: for (j=1;j<=npar;j++)
6564: xi[i][j]=(i==j ? 1.0 : 0.0);
1.359 brouard 6565: printf("Powell-prax\n"); fprintf(ficlog,"Powell-prax\n");
1.201 brouard 6566: strcpy(filerespow,"POW_");
1.126 brouard 6567: strcat(filerespow,fileres);
6568: if((ficrespow=fopen(filerespow,"w"))==NULL) {
6569: printf("Problem with resultfile: %s\n", filerespow);
6570: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
6571: }
6572: fprintf(ficrespow,"# Powell\n# iter -2*LL");
6573: for (i=1;i<=nlstate;i++)
6574: for(j=1;j<=nlstate+ndeath;j++)
6575: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
6576: fprintf(ficrespow,"\n");
1.162 brouard 6577: #ifdef POWELL
1.319 brouard 6578: #ifdef LINMINORIGINAL
6579: #else /* LINMINORIGINAL */
6580:
6581: flatdir=ivector(1,npar);
6582: for (j=1;j<=npar;j++) flatdir[j]=0;
6583: #endif /*LINMINORIGINAL */
6584:
6585: #ifdef FLATSUP
6586: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6587: /* reorganizing p by suppressing flat directions */
6588: for(i=1, jk=1; i <=nlstate; i++){
6589: for(k=1; k <=(nlstate+ndeath); k++){
6590: if (k != i) {
6591: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6592: if(flatdir[jk]==1){
6593: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
6594: }
6595: for(j=1; j <=ncovmodel; j++){
6596: printf("%12.7f ",p[jk]);
6597: jk++;
6598: }
6599: printf("\n");
6600: }
6601: }
6602: }
6603: /* skipping */
6604: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
6605: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
6606: for(k=1; k <=(nlstate+ndeath); k++){
6607: if (k != i) {
6608: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6609: if(flatdir[jk]==1){
6610: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
6611: for(j=1; j <=ncovmodel; jk++,j++){
6612: printf(" p[%d]=%12.7f",jk, p[jk]);
6613: /*q[jjk]=p[jk];*/
6614: }
6615: }else{
6616: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
6617: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
6618: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
6619: /*q[jjk]=p[jk];*/
6620: }
6621: }
6622: printf("\n");
6623: }
6624: fflush(stdout);
6625: }
6626: }
6627: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6628: #else /* FLATSUP */
1.359 brouard 6629: /* powell(p,xi,npar,ftol,&iter,&fret,func);*/
6630: /* praxis ( t0, h0, n, prin, x, beale_f ); */
1.364 brouard 6631: int prin=4;
1.362 brouard 6632: /* double h0=0.25; */
6633: /* double macheps; */
6634: /* double fmin; */
1.359 brouard 6635: macheps=pow(16.0,-13.0);
6636: /* #include "praxis.h" */
6637: /* Be careful that praxis start at x[0] and powell start at p[1] */
6638: /* praxis ( ftol, h0, npar, prin, p, func ); */
6639: /* p1= (p+1); */ /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
6640: printf("Praxis Gegenfurtner \n");
6641: fprintf(ficlog, "Praxis Gegenfurtner\n");fflush(ficlog);
6642: /* praxis ( ftol, h0, npar, prin, p1, func ); */
6643: /* fmin = praxis(1.e-5,macheps, h, n, prin, x, func); */
1.362 brouard 6644: ffmin = praxis(ftol,macheps, h0, npar, prin, p, func);
1.359 brouard 6645: printf("End Praxis\n");
1.319 brouard 6646: #endif /* FLATSUP */
6647:
6648: #ifdef LINMINORIGINAL
6649: #else
6650: free_ivector(flatdir,1,npar);
6651: #endif /* LINMINORIGINAL*/
6652: #endif /* POWELL */
1.126 brouard 6653:
1.162 brouard 6654: #ifdef NLOPT
6655: #ifdef NEWUOA
6656: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
6657: #else
6658: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
6659: #endif
6660: lb=vector(0,npar-1);
6661: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
6662: nlopt_set_lower_bounds(opt, lb);
6663: nlopt_set_initial_step1(opt, 0.1);
6664:
6665: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
6666: d->function = func;
6667: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
6668: nlopt_set_min_objective(opt, myfunc, d);
6669: nlopt_set_xtol_rel(opt, ftol);
6670: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
6671: printf("nlopt failed! %d\n",creturn);
6672: }
6673: else {
6674: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
6675: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
6676: iter=1; /* not equal */
6677: }
6678: nlopt_destroy(opt);
6679: #endif
1.319 brouard 6680: #ifdef FLATSUP
6681: /* npared = npar -flatd/ncovmodel; */
6682: /* xired= matrix(1,npared,1,npared); */
6683: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
6684: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
6685: /* free_matrix(xire,1,npared,1,npared); */
6686: #else /* FLATSUP */
6687: #endif /* FLATSUP */
1.126 brouard 6688: free_matrix(xi,1,npar,1,npar);
6689: fclose(ficrespow);
1.203 brouard 6690: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
6691: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 6692: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 6693:
6694: }
6695:
6696: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 6697: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 6698: {
6699: double **a,**y,*x,pd;
1.203 brouard 6700: /* double **hess; */
1.164 brouard 6701: int i, j;
1.126 brouard 6702: int *indx;
6703:
6704: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 6705: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 6706: void lubksb(double **a, int npar, int *indx, double b[]) ;
6707: void ludcmp(double **a, int npar, int *indx, double *d) ;
6708: double gompertz(double p[]);
1.203 brouard 6709: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 6710:
6711: printf("\nCalculation of the hessian matrix. Wait...\n");
6712: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
6713: for (i=1;i<=npar;i++){
1.203 brouard 6714: printf("%d-",i);fflush(stdout);
6715: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 6716:
6717: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
6718:
6719: /* printf(" %f ",p[i]);
6720: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
6721: }
6722:
6723: for (i=1;i<=npar;i++) {
6724: for (j=1;j<=npar;j++) {
6725: if (j>i) {
1.203 brouard 6726: printf(".%d-%d",i,j);fflush(stdout);
6727: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
6728: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 6729:
6730: hess[j][i]=hess[i][j];
6731: /*printf(" %lf ",hess[i][j]);*/
6732: }
6733: }
6734: }
6735: printf("\n");
6736: fprintf(ficlog,"\n");
6737:
6738: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
6739: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
6740:
6741: a=matrix(1,npar,1,npar);
6742: y=matrix(1,npar,1,npar);
6743: x=vector(1,npar);
6744: indx=ivector(1,npar);
6745: for (i=1;i<=npar;i++)
6746: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
6747: ludcmp(a,npar,indx,&pd);
6748:
6749: for (j=1;j<=npar;j++) {
6750: for (i=1;i<=npar;i++) x[i]=0;
6751: x[j]=1;
6752: lubksb(a,npar,indx,x);
6753: for (i=1;i<=npar;i++){
6754: matcov[i][j]=x[i];
6755: }
6756: }
6757:
6758: printf("\n#Hessian matrix#\n");
6759: fprintf(ficlog,"\n#Hessian matrix#\n");
6760: for (i=1;i<=npar;i++) {
6761: for (j=1;j<=npar;j++) {
1.203 brouard 6762: printf("%.6e ",hess[i][j]);
6763: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 6764: }
6765: printf("\n");
6766: fprintf(ficlog,"\n");
6767: }
6768:
1.203 brouard 6769: /* printf("\n#Covariance matrix#\n"); */
6770: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
6771: /* for (i=1;i<=npar;i++) { */
6772: /* for (j=1;j<=npar;j++) { */
6773: /* printf("%.6e ",matcov[i][j]); */
6774: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
6775: /* } */
6776: /* printf("\n"); */
6777: /* fprintf(ficlog,"\n"); */
6778: /* } */
6779:
1.126 brouard 6780: /* Recompute Inverse */
1.203 brouard 6781: /* for (i=1;i<=npar;i++) */
6782: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
6783: /* ludcmp(a,npar,indx,&pd); */
6784:
6785: /* printf("\n#Hessian matrix recomputed#\n"); */
6786:
6787: /* for (j=1;j<=npar;j++) { */
6788: /* for (i=1;i<=npar;i++) x[i]=0; */
6789: /* x[j]=1; */
6790: /* lubksb(a,npar,indx,x); */
6791: /* for (i=1;i<=npar;i++){ */
6792: /* y[i][j]=x[i]; */
6793: /* printf("%.3e ",y[i][j]); */
6794: /* fprintf(ficlog,"%.3e ",y[i][j]); */
6795: /* } */
6796: /* printf("\n"); */
6797: /* fprintf(ficlog,"\n"); */
6798: /* } */
6799:
6800: /* Verifying the inverse matrix */
6801: #ifdef DEBUGHESS
6802: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 6803:
1.203 brouard 6804: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
6805: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 6806:
6807: for (j=1;j<=npar;j++) {
6808: for (i=1;i<=npar;i++){
1.203 brouard 6809: printf("%.2f ",y[i][j]);
6810: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 6811: }
6812: printf("\n");
6813: fprintf(ficlog,"\n");
6814: }
1.203 brouard 6815: #endif
1.126 brouard 6816:
6817: free_matrix(a,1,npar,1,npar);
6818: free_matrix(y,1,npar,1,npar);
6819: free_vector(x,1,npar);
6820: free_ivector(indx,1,npar);
1.203 brouard 6821: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 6822:
6823:
6824: }
6825:
6826: /*************** hessian matrix ****************/
6827: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 6828: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 6829: int i;
6830: int l=1, lmax=20;
1.203 brouard 6831: double k1,k2, res, fx;
1.132 brouard 6832: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 6833: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
6834: int k=0,kmax=10;
6835: double l1;
6836:
6837: fx=func(x);
6838: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 6839: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 6840: l1=pow(10,l);
6841: delts=delt;
6842: for(k=1 ; k <kmax; k=k+1){
6843: delt = delta*(l1*k);
6844: p2[theta]=x[theta] +delt;
1.145 brouard 6845: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 6846: p2[theta]=x[theta]-delt;
6847: k2=func(p2)-fx;
6848: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 6849: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 6850:
1.203 brouard 6851: #ifdef DEBUGHESSII
1.126 brouard 6852: printf("%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
6853: fprintf(ficlog,"%d %d k1=%.12e k2=%.12e xk1=%.12e xk2=%.12e delt=%.12e res=%.12e l=%d k=%d,fx=%.12e\n",theta,theta,k1,k2,x[theta]+delt,x[theta]-delt,delt,res, l, k,fx);
6854: #endif
6855: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
6856: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
6857: k=kmax;
6858: }
6859: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 6860: k=kmax; l=lmax*10;
1.126 brouard 6861: }
6862: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
6863: delts=delt;
6864: }
1.203 brouard 6865: } /* End loop k */
1.126 brouard 6866: }
6867: delti[theta]=delts;
6868: return res;
6869:
6870: }
6871:
1.203 brouard 6872: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 6873: {
6874: int i;
1.164 brouard 6875: int l=1, lmax=20;
1.126 brouard 6876: double k1,k2,k3,k4,res,fx;
1.132 brouard 6877: double p2[MAXPARM+1];
1.203 brouard 6878: int k, kmax=1;
6879: double v1, v2, cv12, lc1, lc2;
1.208 brouard 6880:
6881: int firstime=0;
1.203 brouard 6882:
1.126 brouard 6883: fx=func(x);
1.203 brouard 6884: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 6885: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 6886: p2[thetai]=x[thetai]+delti[thetai]*k;
6887: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 6888: k1=func(p2)-fx;
6889:
1.203 brouard 6890: p2[thetai]=x[thetai]+delti[thetai]*k;
6891: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 6892: k2=func(p2)-fx;
6893:
1.203 brouard 6894: p2[thetai]=x[thetai]-delti[thetai]*k;
6895: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 6896: k3=func(p2)-fx;
6897:
1.203 brouard 6898: p2[thetai]=x[thetai]-delti[thetai]*k;
6899: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 6900: k4=func(p2)-fx;
1.203 brouard 6901: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
6902: if(k1*k2*k3*k4 <0.){
1.208 brouard 6903: firstime=1;
1.203 brouard 6904: kmax=kmax+10;
1.208 brouard 6905: }
6906: if(kmax >=10 || firstime ==1){
1.354 brouard 6907: /* What are the thetai and thetaj? thetai/ncovmodel thetai=(thetai-thetai%ncovmodel)/ncovmodel +thetai%ncovmodel=(line,pos) */
1.246 brouard 6908: printf("Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you could increase ftol=%.2e\n",thetai,thetaj, ftol);
6909: fprintf(ficlog,"Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you could increase ftol=%.2e\n",thetai,thetaj, ftol);
1.203 brouard 6910: printf("%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti*k=%.12e deltj*k=%.12e, xi-de*k=%.12e xj-de*k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
6911: fprintf(ficlog,"%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti*k=%.12e deltj*k=%.12e, xi-de*k=%.12e xj-de*k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
6912: }
6913: #ifdef DEBUGHESSIJ
6914: v1=hess[thetai][thetai];
6915: v2=hess[thetaj][thetaj];
6916: cv12=res;
6917: /* Computing eigen value of Hessian matrix */
6918: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6919: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6920: if ((lc2 <0) || (lc1 <0) ){
6921: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6922: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6923: printf("%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
6924: fprintf(ficlog,"%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4);
6925: }
1.126 brouard 6926: #endif
6927: }
6928: return res;
6929: }
6930:
1.203 brouard 6931: /* Not done yet: Was supposed to fix if not exactly at the maximum */
6932: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
6933: /* { */
6934: /* int i; */
6935: /* int l=1, lmax=20; */
6936: /* double k1,k2,k3,k4,res,fx; */
6937: /* double p2[MAXPARM+1]; */
6938: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
6939: /* int k=0,kmax=10; */
6940: /* double l1; */
6941:
6942: /* fx=func(x); */
6943: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
6944: /* l1=pow(10,l); */
6945: /* delts=delt; */
6946: /* for(k=1 ; k <kmax; k=k+1){ */
6947: /* delt = delti*(l1*k); */
6948: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
6949: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6950: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6951: /* k1=func(p2)-fx; */
6952:
6953: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6954: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6955: /* k2=func(p2)-fx; */
6956:
6957: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6958: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6959: /* k3=func(p2)-fx; */
6960:
6961: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6962: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6963: /* k4=func(p2)-fx; */
6964: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
6965: /* #ifdef DEBUGHESSIJ */
6966: /* printf("%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4); */
6967: /* fprintf(ficlog,"%d %d k=%d, k1=%.12e k2=%.12e k3=%.12e k4=%.12e delti/k=%.12e deltj/k=%.12e, xi-de/k=%.12e xj-de/k=%.12e res=%.12e k1234=%.12e,k1-2=%.12e,k3-4=%.12e\n",thetai,thetaj,k,k1,k2,k3,k4,delti[thetai]/k,delti[thetaj]/k,x[thetai]-delti[thetai]/k,x[thetaj]-delti[thetaj]/k, res,k1-k2-k3+k4,k1-k2,k3-k4); */
6968: /* #endif */
6969: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
6970: /* k=kmax; */
6971: /* } */
6972: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
6973: /* k=kmax; l=lmax*10; */
6974: /* } */
6975: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
6976: /* delts=delt; */
6977: /* } */
6978: /* } /\* End loop k *\/ */
6979: /* } */
6980: /* delti[theta]=delts; */
6981: /* return res; */
6982: /* } */
6983:
6984:
1.126 brouard 6985: /************** Inverse of matrix **************/
6986: void ludcmp(double **a, int n, int *indx, double *d)
6987: {
6988: int i,imax,j,k;
6989: double big,dum,sum,temp;
6990: double *vv;
6991:
6992: vv=vector(1,n);
6993: *d=1.0;
6994: for (i=1;i<=n;i++) {
6995: big=0.0;
6996: for (j=1;j<=n;j++)
6997: if ((temp=fabs(a[i][j])) > big) big=temp;
1.256 brouard 6998: if (big == 0.0){
6999: printf(" Singular Hessian matrix at row %d:\n",i);
7000: for (j=1;j<=n;j++) {
7001: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
7002: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
7003: }
7004: fflush(ficlog);
7005: fclose(ficlog);
7006: nrerror("Singular matrix in routine ludcmp");
7007: }
1.126 brouard 7008: vv[i]=1.0/big;
7009: }
7010: for (j=1;j<=n;j++) {
7011: for (i=1;i<j;i++) {
7012: sum=a[i][j];
7013: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
7014: a[i][j]=sum;
7015: }
7016: big=0.0;
7017: for (i=j;i<=n;i++) {
7018: sum=a[i][j];
7019: for (k=1;k<j;k++)
7020: sum -= a[i][k]*a[k][j];
7021: a[i][j]=sum;
7022: if ( (dum=vv[i]*fabs(sum)) >= big) {
7023: big=dum;
7024: imax=i;
7025: }
7026: }
7027: if (j != imax) {
7028: for (k=1;k<=n;k++) {
7029: dum=a[imax][k];
7030: a[imax][k]=a[j][k];
7031: a[j][k]=dum;
7032: }
7033: *d = -(*d);
7034: vv[imax]=vv[j];
7035: }
7036: indx[j]=imax;
7037: if (a[j][j] == 0.0) a[j][j]=TINY;
7038: if (j != n) {
7039: dum=1.0/(a[j][j]);
7040: for (i=j+1;i<=n;i++) a[i][j] *= dum;
7041: }
7042: }
7043: free_vector(vv,1,n); /* Doesn't work */
7044: ;
7045: }
7046:
7047: void lubksb(double **a, int n, int *indx, double b[])
7048: {
7049: int i,ii=0,ip,j;
7050: double sum;
7051:
7052: for (i=1;i<=n;i++) {
7053: ip=indx[i];
7054: sum=b[ip];
7055: b[ip]=b[i];
7056: if (ii)
7057: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
7058: else if (sum) ii=i;
7059: b[i]=sum;
7060: }
7061: for (i=n;i>=1;i--) {
7062: sum=b[i];
7063: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
7064: b[i]=sum/a[i][i];
7065: }
7066: }
7067:
7068: void pstamp(FILE *fichier)
7069: {
1.196 brouard 7070: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 7071: }
7072:
1.297 brouard 7073: void date2dmy(double date,double *day, double *month, double *year){
7074: double yp=0., yp1=0., yp2=0.;
7075:
7076: yp1=modf(date,&yp);/* extracts integral of date in yp and
7077: fractional in yp1 */
7078: *year=yp;
7079: yp2=modf((yp1*12),&yp);
7080: *month=yp;
7081: yp1=modf((yp2*30.5),&yp);
7082: *day=yp;
7083: if(*day==0) *day=1;
7084: if(*month==0) *month=1;
7085: }
7086:
1.253 brouard 7087:
7088:
1.126 brouard 7089: /************ Frequencies ********************/
1.251 brouard 7090: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
1.226 brouard 7091: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
7092: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.250 brouard 7093: { /* Some frequencies as well as proposing some starting values */
1.332 brouard 7094: /* Frequencies of any combination of dummy covariate used in the model equation */
1.265 brouard 7095: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
1.226 brouard 7096: int iind=0, iage=0;
7097: int mi; /* Effective wave */
7098: int first;
7099: double ***freq; /* Frequencies */
1.268 brouard 7100: double *x, *y, a=0.,b=0.,r=1., sa=0., sb=0.; /* for regression, y=b+m*x and r is the correlation coefficient */
7101: int no=0, linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb);
1.284 brouard 7102: double *meanq, *stdq, *idq;
1.226 brouard 7103: double **meanqt;
7104: double *pp, **prop, *posprop, *pospropt;
7105: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
7106: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
7107: double agebegin, ageend;
7108:
7109: pp=vector(1,nlstate);
1.251 brouard 7110: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 7111: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
7112: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
7113: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
7114: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.284 brouard 7115: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.283 brouard 7116: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.226 brouard 7117: meanqt=matrix(1,lastpass,1,nqtveff);
7118: strcpy(fileresp,"P_");
7119: strcat(fileresp,fileresu);
7120: /*strcat(fileresphtm,fileresu);*/
7121: if((ficresp=fopen(fileresp,"w"))==NULL) {
7122: printf("Problem with prevalence resultfile: %s\n", fileresp);
7123: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
7124: exit(0);
7125: }
1.240 brouard 7126:
1.226 brouard 7127: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
7128: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
7129: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7130: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7131: fflush(ficlog);
7132: exit(70);
7133: }
7134: else{
7135: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 7136: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 7137: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 7138: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7139: }
1.319 brouard 7140: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies (weight=%d) and prevalence by age at begin of transition and dummy covariate value at beginning of transition</h4>\n",fileresphtm, fileresphtm, weightopt);
1.240 brouard 7141:
1.226 brouard 7142: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
7143: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
7144: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7145: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7146: fflush(ficlog);
7147: exit(70);
1.240 brouard 7148: } else{
1.226 brouard 7149: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.319 brouard 7150: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 7151: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 7152: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7153: }
1.319 brouard 7154: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>(weight=%d) frequencies of all effective transitions of the model, by age at begin of transition, and covariate value at the begin of transition (if the covariate is a varying covariate) </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr,weightopt);
1.240 brouard 7155:
1.253 brouard 7156: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7157: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.251 brouard 7158: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 7159: j1=0;
1.126 brouard 7160:
1.227 brouard 7161: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
1.335 brouard 7162: j=cptcoveff; /* Only simple dummy covariates used in the model */
1.330 brouard 7163: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
1.226 brouard 7164: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 7165:
7166:
1.226 brouard 7167: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
7168: reference=low_education V1=0,V2=0
7169: med_educ V1=1 V2=0,
7170: high_educ V1=0 V2=1
1.330 brouard 7171: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
1.226 brouard 7172: */
1.249 brouard 7173: dateintsum=0;
7174: k2cpt=0;
7175:
1.253 brouard 7176: if(cptcoveff == 0 )
1.265 brouard 7177: nl=1; /* Constant and age model only */
1.253 brouard 7178: else
7179: nl=2;
1.265 brouard 7180:
7181: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
7182: /* Loop on nj=1 or 2 if dummy covariates j!=0
1.335 brouard 7183: * Loop on j1(1 to 2**cptcoveff) covariate combination
1.265 brouard 7184: * freq[s1][s2][iage] =0.
7185: * Loop on iind
7186: * ++freq[s1][s2][iage] weighted
7187: * end iind
7188: * if covariate and j!0
7189: * headers Variable on one line
7190: * endif cov j!=0
7191: * header of frequency table by age
7192: * Loop on age
7193: * pp[s1]+=freq[s1][s2][iage] weighted
7194: * pos+=freq[s1][s2][iage] weighted
7195: * Loop on s1 initial state
7196: * fprintf(ficresp
7197: * end s1
7198: * end age
7199: * if j!=0 computes starting values
7200: * end compute starting values
7201: * end j1
7202: * end nl
7203: */
1.253 brouard 7204: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
7205: if(nj==1)
7206: j=0; /* First pass for the constant */
1.265 brouard 7207: else{
1.335 brouard 7208: j=cptcoveff; /* Other passes for the covariate values number of simple covariates in the model V2+V1 =2 (simple dummy fixed or time varying) */
1.265 brouard 7209: }
1.251 brouard 7210: first=1;
1.332 brouard 7211: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on all dummy covariates combination of the model, ie excluding quantitatives, V4=0, V3=0 for example, fixed or varying covariates */
1.251 brouard 7212: posproptt=0.;
1.330 brouard 7213: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
1.251 brouard 7214: scanf("%d", i);*/
7215: for (i=-5; i<=nlstate+ndeath; i++)
1.265 brouard 7216: for (s2=-5; s2<=nlstate+ndeath; s2++)
1.251 brouard 7217: for(m=iagemin; m <= iagemax+3; m++)
1.265 brouard 7218: freq[i][s2][m]=0;
1.251 brouard 7219:
7220: for (i=1; i<=nlstate; i++) {
1.240 brouard 7221: for(m=iagemin; m <= iagemax+3; m++)
1.251 brouard 7222: prop[i][m]=0;
7223: posprop[i]=0;
7224: pospropt[i]=0;
7225: }
1.283 brouard 7226: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
1.284 brouard 7227: idq[z1]=0.;
7228: meanq[z1]=0.;
7229: stdq[z1]=0.;
1.283 brouard 7230: }
7231: /* for (z1=1; z1<= nqtveff; z1++) { */
1.251 brouard 7232: /* for(m=1;m<=lastpass;m++){ */
1.283 brouard 7233: /* meanqt[m][z1]=0.; */
7234: /* } */
7235: /* } */
1.251 brouard 7236: /* dateintsum=0; */
7237: /* k2cpt=0; */
7238:
1.265 brouard 7239: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
1.251 brouard 7240: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
7241: bool=1;
7242: if(j !=0){
7243: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.335 brouard 7244: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
7245: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
1.251 brouard 7246: /* if(Tvaraff[z1] ==-20){ */
7247: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
7248: /* }else if(Tvaraff[z1] ==-10){ */
7249: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
1.330 brouard 7250: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
1.335 brouard 7251: /* if( iind >=imx-3) printf("Searching error iind=%d Tvaraff[z1]=%d covar[Tvaraff[z1]][iind]=%.f TnsdVar[Tvaraff[z1]]=%d, cptcoveff=%d, cptcovs=%d \n",iind, Tvaraff[z1], covar[Tvaraff[z1]][iind],TnsdVar[Tvaraff[z1]],cptcoveff, cptcovs); */
7252: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
1.338 brouard 7253: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
1.332 brouard 7254: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
1.265 brouard 7255: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
1.251 brouard 7256: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
1.332 brouard 7257: /* printf("bool=%d i=%d, z1=%d, Tvaraff[%d]=%d, covar[Tvarff][%d]=%2f, codtabm(%d,%d)=%d, nbcode[Tvaraff][codtabm(%d,%d)=%d, j1=%d\n", */
7258: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
7259: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
1.251 brouard 7260: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
7261: } /* Onlyf fixed */
7262: } /* end z1 */
1.335 brouard 7263: } /* cptcoveff > 0 */
1.251 brouard 7264: } /* end any */
7265: }/* end j==0 */
1.265 brouard 7266: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
1.251 brouard 7267: /* for(m=firstpass; m<=lastpass; m++){ */
1.284 brouard 7268: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
1.251 brouard 7269: m=mw[mi][iind];
7270: if(j!=0){
7271: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
1.335 brouard 7272: for (z1=1; z1<=cptcoveff; z1++) {
1.251 brouard 7273: if( Fixed[Tmodelind[z1]]==1){
1.341 brouard 7274: /* iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; /\* Good *\/ */
7275: iv= Tvar[Tmodelind[z1]]; /* Good *//* because cotvar starts now at first at ncovcol+nqv+ntv */
1.332 brouard 7276: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality. If covariate's
1.251 brouard 7277: value is -1, we don't select. It differs from the
7278: constant and age model which counts them. */
7279: bool=0; /* not selected */
7280: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
1.334 brouard 7281: /* i1=Tvaraff[z1]; */
7282: /* i2=TnsdVar[i1]; */
7283: /* i3=nbcode[i1][i2]; */
7284: /* i4=covar[i1][iind]; */
7285: /* if(i4 != i3){ */
7286: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
1.251 brouard 7287: bool=0;
7288: }
7289: }
7290: }
7291: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
7292: } /* end j==0 */
7293: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
1.284 brouard 7294: if(bool==1){ /*Selected */
1.251 brouard 7295: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
7296: and mw[mi+1][iind]. dh depends on stepm. */
7297: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
7298: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
7299: if(m >=firstpass && m <=lastpass){
7300: k2=anint[m][iind]+(mint[m][iind]/12.);
7301: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
7302: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
7303: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
7304: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
7305: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
7306: if (m<lastpass) {
7307: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
7308: /* printf(" num=%ld m=%d, iind=%d s1=%d s2=%d agev at m=%d\n", num[iind], m, iind,s[m][iind],s[m+1][iind], (int)agev[m][iind]); */
7309: if(s[m][iind]==-1)
7310: printf(" num=%ld m=%d, iind=%d s1=%d s2=%d agev at m=%d agebegin=%.2f ageend=%.2f, agemed=%d\n", num[iind], m, iind,s[m][iind],s[m+1][iind], (int)agev[m][iind],agebegin, ageend, (int)((agebegin+ageend)/2.));
7311: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
1.311 brouard 7312: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
7313: if(!isnan(covar[ncovcol+z1][iind])){
1.332 brouard 7314: idq[z1]=idq[z1]+weight[iind];
7315: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
7316: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
7317: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
1.311 brouard 7318: }
1.284 brouard 7319: }
1.251 brouard 7320: /* if((int)agev[m][iind] == 55) */
7321: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
7322: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
7323: freq[s[m][iind]][s[m+1][iind]][iagemax+3] += weight[iind]; /* Total is in iagemax+3 *//* At age of beginning of transition, where status is known */
1.234 brouard 7324: }
1.251 brouard 7325: } /* end if between passes */
7326: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
7327: dateintsum=dateintsum+k2; /* on all covariates ?*/
7328: k2cpt++;
7329: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
1.234 brouard 7330: }
1.251 brouard 7331: }else{
7332: bool=1;
7333: }/* end bool 2 */
7334: } /* end m */
1.284 brouard 7335: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
7336: /* idq[z1]=idq[z1]+weight[iind]; */
7337: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
7338: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
7339: /* } */
1.251 brouard 7340: } /* end bool */
7341: } /* end iind = 1 to imx */
1.319 brouard 7342: /* prop[s][age] is fed for any initial and valid live state as well as
1.251 brouard 7343: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
7344:
7345:
7346: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.335 brouard 7347: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
1.265 brouard 7348: pstamp(ficresp);
1.335 brouard 7349: if (cptcoveff>0 && j!=0){
1.265 brouard 7350: pstamp(ficresp);
1.251 brouard 7351: printf( "\n#********** Variable ");
7352: fprintf(ficresp, "\n#********** Variable ");
7353: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
7354: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
7355: fprintf(ficlog, "\n#********** Variable ");
1.340 brouard 7356: for (z1=1; z1<=cptcoveff; z1++){
1.251 brouard 7357: if(!FixedV[Tvaraff[z1]]){
1.330 brouard 7358: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7359: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7360: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7361: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7362: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.250 brouard 7363: }else{
1.330 brouard 7364: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7365: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7366: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7367: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7368: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.251 brouard 7369: }
7370: }
7371: printf( "**********\n#");
7372: fprintf(ficresp, "**********\n#");
7373: fprintf(ficresphtm, "**********</h3>\n");
7374: fprintf(ficresphtmfr, "**********</h3>\n");
7375: fprintf(ficlog, "**********\n");
7376: }
1.284 brouard 7377: /*
7378: Printing means of quantitative variables if any
7379: */
7380: for (z1=1; z1<= nqfveff; z1++) {
1.311 brouard 7381: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
1.312 brouard 7382: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
1.284 brouard 7383: if(weightopt==1){
7384: printf(" Weighted mean and standard deviation of");
7385: fprintf(ficlog," Weighted mean and standard deviation of");
7386: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
7387: }
1.311 brouard 7388: /* mu = \frac{w x}{\sum w}
7389: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
7390: */
7391: printf(" fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
7392: fprintf(ficlog," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
7393: fprintf(ficresphtmfr," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)<p>\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
1.284 brouard 7394: }
7395: /* for (z1=1; z1<= nqtveff; z1++) { */
7396: /* for(m=1;m<=lastpass;m++){ */
7397: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
7398: /* } */
7399: /* } */
1.283 brouard 7400:
1.251 brouard 7401: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.335 brouard 7402: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
1.265 brouard 7403: fprintf(ficresp, " Age");
1.335 brouard 7404: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
7405: printf(" V%d=%d, z1=%d, Tvaraff[z1]=%d, j1=%d, TnsdVar[Tvaraff[%d]]=%d |",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])], z1, Tvaraff[z1], j1,z1,TnsdVar[Tvaraff[z1]]);
7406: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7407: }
1.251 brouard 7408: for(i=1; i<=nlstate;i++) {
1.335 brouard 7409: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
1.251 brouard 7410: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
7411: }
1.335 brouard 7412: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
1.251 brouard 7413: fprintf(ficresphtm, "\n");
7414:
7415: /* Header of frequency table by age */
7416: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
7417: fprintf(ficresphtmfr,"<th>Age</th> ");
1.265 brouard 7418: for(s2=-1; s2 <=nlstate+ndeath; s2++){
1.251 brouard 7419: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 7420: if(s2!=0 && m!=0)
7421: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
1.240 brouard 7422: }
1.226 brouard 7423: }
1.251 brouard 7424: fprintf(ficresphtmfr, "\n");
7425:
7426: /* For each age */
7427: for(iage=iagemin; iage <= iagemax+3; iage++){
7428: fprintf(ficresphtm,"<tr>");
7429: if(iage==iagemax+1){
7430: fprintf(ficlog,"1");
7431: fprintf(ficresphtmfr,"<tr><th>0</th> ");
7432: }else if(iage==iagemax+2){
7433: fprintf(ficlog,"0");
7434: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
7435: }else if(iage==iagemax+3){
7436: fprintf(ficlog,"Total");
7437: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
7438: }else{
1.240 brouard 7439: if(first==1){
1.251 brouard 7440: first=0;
7441: printf("See log file for details...\n");
7442: }
7443: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
7444: fprintf(ficlog,"Age %d", iage);
7445: }
1.265 brouard 7446: for(s1=1; s1 <=nlstate ; s1++){
7447: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
7448: pp[s1] += freq[s1][m][iage];
1.251 brouard 7449: }
1.265 brouard 7450: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 7451: for(m=-1, pos=0; m <=0 ; m++)
1.265 brouard 7452: pos += freq[s1][m][iage];
7453: if(pp[s1]>=1.e-10){
1.251 brouard 7454: if(first==1){
1.265 brouard 7455: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 7456: }
1.265 brouard 7457: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 7458: }else{
7459: if(first==1)
1.265 brouard 7460: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
7461: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
1.240 brouard 7462: }
7463: }
7464:
1.265 brouard 7465: for(s1=1; s1 <=nlstate ; s1++){
7466: /* posprop[s1]=0; */
7467: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
7468: pp[s1] += freq[s1][m][iage];
7469: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
7470:
7471: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
7472: pos += pp[s1]; /* pos is the total number of transitions until this age */
7473: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
7474: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7475: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
7476: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7477: }
7478:
7479: /* Writing ficresp */
1.335 brouard 7480: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
1.265 brouard 7481: if( iage <= iagemax){
7482: fprintf(ficresp," %d",iage);
7483: }
7484: }else if( nj==2){
7485: if( iage <= iagemax){
7486: fprintf(ficresp," %d",iage);
1.335 brouard 7487: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.265 brouard 7488: }
1.240 brouard 7489: }
1.265 brouard 7490: for(s1=1; s1 <=nlstate ; s1++){
1.240 brouard 7491: if(pos>=1.e-5){
1.251 brouard 7492: if(first==1)
1.265 brouard 7493: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
7494: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
1.251 brouard 7495: }else{
7496: if(first==1)
1.265 brouard 7497: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
7498: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
1.251 brouard 7499: }
7500: if( iage <= iagemax){
7501: if(pos>=1.e-5){
1.335 brouard 7502: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
1.265 brouard 7503: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7504: }else if( nj==2){
7505: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7506: }
7507: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7508: /*probs[iage][s1][j1]= pp[s1]/pos;*/
7509: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
7510: } else{
1.335 brouard 7511: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
1.265 brouard 7512: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
1.251 brouard 7513: }
1.240 brouard 7514: }
1.265 brouard 7515: pospropt[s1] +=posprop[s1];
7516: } /* end loop s1 */
1.251 brouard 7517: /* pospropt=0.; */
1.265 brouard 7518: for(s1=-1; s1 <=nlstate+ndeath; s1++){
1.251 brouard 7519: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 7520: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
1.251 brouard 7521: if(first==1){
1.265 brouard 7522: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 7523: }
1.265 brouard 7524: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
7525: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 7526: }
1.265 brouard 7527: if(s1!=0 && m!=0)
7528: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
1.240 brouard 7529: }
1.265 brouard 7530: } /* end loop s1 */
1.251 brouard 7531: posproptt=0.;
1.265 brouard 7532: for(s1=1; s1 <=nlstate; s1++){
7533: posproptt += pospropt[s1];
1.251 brouard 7534: }
7535: fprintf(ficresphtmfr,"</tr>\n ");
1.265 brouard 7536: fprintf(ficresphtm,"</tr>\n");
1.335 brouard 7537: if((cptcoveff==0 && nj==1)|| nj==2 ) {
1.265 brouard 7538: if(iage <= iagemax)
7539: fprintf(ficresp,"\n");
1.240 brouard 7540: }
1.251 brouard 7541: if(first==1)
7542: printf("Others in log...\n");
7543: fprintf(ficlog,"\n");
7544: } /* end loop age iage */
1.265 brouard 7545:
1.251 brouard 7546: fprintf(ficresphtm,"<tr><th>Tot</th>");
1.265 brouard 7547: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 7548: if(posproptt < 1.e-5){
1.265 brouard 7549: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
1.251 brouard 7550: }else{
1.265 brouard 7551: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
1.240 brouard 7552: }
1.226 brouard 7553: }
1.251 brouard 7554: fprintf(ficresphtm,"</tr>\n");
7555: fprintf(ficresphtm,"</table>\n");
7556: fprintf(ficresphtmfr,"</table>\n");
1.226 brouard 7557: if(posproptt < 1.e-5){
1.251 brouard 7558: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
7559: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
1.260 brouard 7560: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
7561: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
1.251 brouard 7562: invalidvarcomb[j1]=1;
1.226 brouard 7563: }else{
1.338 brouard 7564: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
1.251 brouard 7565: invalidvarcomb[j1]=0;
1.226 brouard 7566: }
1.251 brouard 7567: fprintf(ficresphtmfr,"</table>\n");
7568: fprintf(ficlog,"\n");
7569: if(j!=0){
7570: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
1.265 brouard 7571: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 7572: for(k=1; k <=(nlstate+ndeath); k++){
7573: if (k != i) {
1.265 brouard 7574: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
1.253 brouard 7575: if(jj==1){ /* Constant case (in fact cste + age) */
1.251 brouard 7576: if(j1==1){ /* All dummy covariates to zero */
7577: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
7578: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
1.252 brouard 7579: printf("%d%d ",i,k);
7580: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 7581: printf("%12.7f ln(%.0f/%.0f)= %f, OR=%f sd=%f \n",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]),freq[i][k][iagemax+3]/freq[i][i][iagemax+3], sqrt(1/freq[i][k][iagemax+3]+1/freq[i][i][iagemax+3]));
7582: fprintf(ficlog,"%12.7f ln(%.0f/%.0f)= %12.7f \n",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
7583: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
1.251 brouard 7584: }
1.253 brouard 7585: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
7586: for(iage=iagemin; iage <= iagemax+3; iage++){
7587: x[iage]= (double)iage;
7588: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
1.265 brouard 7589: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */
1.253 brouard 7590: }
1.268 brouard 7591: /* Some are not finite, but linreg will ignore these ages */
7592: no=0;
1.253 brouard 7593: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
1.265 brouard 7594: pstart[s1]=b;
7595: pstart[s1-1]=a;
1.252 brouard 7596: }else if( j1!=1 && (j1==2 || (log(j1-1.)/log(2.)-(int)(log(j1-1.)/log(2.))) <0.010) && ( TvarsDind[(int)(log(j1-1.)/log(2.))+1]+2+nagesqr == jj) && Dummy[jj-2-nagesqr]==0){ /* We want only if the position, jj, in model corresponds to unique covariate equal to 1 in j1 combination */
7597: printf("j1=%d, jj=%d, (int)(log(j1-1.)/log(2.))+1=%d, TvarsDind[(int)(log(j1-1.)/log(2.))+1]=%d\n",j1, jj,(int)(log(j1-1.)/log(2.))+1,TvarsDind[(int)(log(j1-1.)/log(2.))+1]);
7598: printf("j1=%d, jj=%d, (log(j1-1.)/log(2.))+1=%f, TvarsDind[(int)(log(j1-1.)/log(2.))+1]=%d\n",j1, jj,(log(j1-1.)/log(2.))+1,TvarsDind[(int)(log(j1-1.)/log(2.))+1]);
1.265 brouard 7599: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]));
1.252 brouard 7600: printf("%d%d ",i,k);
7601: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 7602: printf("s1=%d,i=%d,k=%d,p[%d]=%12.7f ln((%.0f/%.0f)/(%.0f/%.0f))= %f, OR=%f sd=%f \n",s1,i,k,s1,p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3],freq[i][k][iagemax+4],freq[i][i][iagemax+4], log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4])),(freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4]), sqrt(1/freq[i][k][iagemax+3]+1/freq[i][i][iagemax+3]+1/freq[i][k][iagemax+4]+1/freq[i][i][iagemax+4]));
1.251 brouard 7603: }else{ /* Other cases, like quantitative fixed or varying covariates */
7604: ;
7605: }
7606: /* printf("%12.7f )", param[i][jj][k]); */
7607: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 7608: s1++;
1.251 brouard 7609: } /* end jj */
7610: } /* end k!= i */
7611: } /* end k */
1.265 brouard 7612: } /* end i, s1 */
1.251 brouard 7613: } /* end j !=0 */
7614: } /* end selected combination of covariate j1 */
7615: if(j==0){ /* We can estimate starting values from the occurences in each case */
7616: printf("#Freqsummary: Starting values for the constants:\n");
7617: fprintf(ficlog,"\n");
1.265 brouard 7618: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 7619: for(k=1; k <=(nlstate+ndeath); k++){
7620: if (k != i) {
7621: printf("%d%d ",i,k);
7622: fprintf(ficlog,"%d%d ",i,k);
7623: for(jj=1; jj <=ncovmodel; jj++){
1.265 brouard 7624: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
1.253 brouard 7625: if(jj==1){ /* Age has to be done */
1.265 brouard 7626: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
7627: printf("%12.7f ln(%.0f/%.0f)= %12.7f ",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
7628: fprintf(ficlog,"%12.7f ln(%.0f/%.0f)= %12.7f ",p[s1],freq[i][k][iagemax+3],freq[i][i][iagemax+3], log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]));
1.251 brouard 7629: }
7630: /* printf("%12.7f )", param[i][jj][k]); */
7631: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 7632: s1++;
1.250 brouard 7633: }
1.251 brouard 7634: printf("\n");
7635: fprintf(ficlog,"\n");
1.250 brouard 7636: }
7637: }
1.284 brouard 7638: } /* end of state i */
1.251 brouard 7639: printf("#Freqsummary\n");
7640: fprintf(ficlog,"\n");
1.265 brouard 7641: for(s1=-1; s1 <=nlstate+ndeath; s1++){
7642: for(s2=-1; s2 <=nlstate+ndeath; s2++){
7643: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
7644: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7645: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7646: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
7647: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
7648: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
1.251 brouard 7649: /* } */
7650: }
1.265 brouard 7651: } /* end loop s1 */
1.251 brouard 7652:
7653: printf("\n");
7654: fprintf(ficlog,"\n");
7655: } /* end j=0 */
1.249 brouard 7656: } /* end j */
1.252 brouard 7657:
1.253 brouard 7658: if(mle == -2){ /* We want to use these values as starting values */
1.252 brouard 7659: for(i=1, jk=1; i <=nlstate; i++){
7660: for(j=1; j <=nlstate+ndeath; j++){
7661: if(j!=i){
7662: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7663: printf("%1d%1d",i,j);
7664: fprintf(ficparo,"%1d%1d",i,j);
7665: for(k=1; k<=ncovmodel;k++){
7666: /* printf(" %lf",param[i][j][k]); */
7667: /* fprintf(ficparo," %lf",param[i][j][k]); */
7668: p[jk]=pstart[jk];
7669: printf(" %f ",pstart[jk]);
7670: fprintf(ficparo," %f ",pstart[jk]);
7671: jk++;
7672: }
7673: printf("\n");
7674: fprintf(ficparo,"\n");
7675: }
7676: }
7677: }
7678: } /* end mle=-2 */
1.226 brouard 7679: dateintmean=dateintsum/k2cpt;
1.296 brouard 7680: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
1.240 brouard 7681:
1.226 brouard 7682: fclose(ficresp);
7683: fclose(ficresphtm);
7684: fclose(ficresphtmfr);
1.283 brouard 7685: free_vector(idq,1,nqfveff);
1.226 brouard 7686: free_vector(meanq,1,nqfveff);
1.284 brouard 7687: free_vector(stdq,1,nqfveff);
1.226 brouard 7688: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.253 brouard 7689: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
7690: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.251 brouard 7691: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 7692: free_vector(pospropt,1,nlstate);
7693: free_vector(posprop,1,nlstate);
1.251 brouard 7694: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 7695: free_vector(pp,1,nlstate);
7696: /* End of freqsummary */
7697: }
1.126 brouard 7698:
1.268 brouard 7699: /* Simple linear regression */
7700: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
7701:
7702: /* y=a+bx regression */
7703: double sumx = 0.0; /* sum of x */
7704: double sumx2 = 0.0; /* sum of x**2 */
7705: double sumxy = 0.0; /* sum of x * y */
7706: double sumy = 0.0; /* sum of y */
7707: double sumy2 = 0.0; /* sum of y**2 */
7708: double sume2 = 0.0; /* sum of square or residuals */
7709: double yhat;
7710:
7711: double denom=0;
7712: int i;
7713: int ne=*no;
7714:
7715: for ( i=ifi, ne=0;i<=ila;i++) {
7716: if(!isfinite(x[i]) || !isfinite(y[i])){
7717: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7718: continue;
7719: }
7720: ne=ne+1;
7721: sumx += x[i];
7722: sumx2 += x[i]*x[i];
7723: sumxy += x[i] * y[i];
7724: sumy += y[i];
7725: sumy2 += y[i]*y[i];
7726: denom = (ne * sumx2 - sumx*sumx);
7727: /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */
7728: }
7729:
7730: denom = (ne * sumx2 - sumx*sumx);
7731: if (denom == 0) {
7732: // vertical, slope m is infinity
7733: *b = INFINITY;
7734: *a = 0;
7735: if (r) *r = 0;
7736: return 1;
7737: }
7738:
7739: *b = (ne * sumxy - sumx * sumy) / denom;
7740: *a = (sumy * sumx2 - sumx * sumxy) / denom;
7741: if (r!=NULL) {
7742: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
7743: sqrt((sumx2 - sumx*sumx/ne) *
7744: (sumy2 - sumy*sumy/ne));
7745: }
7746: *no=ne;
7747: for ( i=ifi, ne=0;i<=ila;i++) {
7748: if(!isfinite(x[i]) || !isfinite(y[i])){
7749: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7750: continue;
7751: }
7752: ne=ne+1;
7753: yhat = y[i] - *a -*b* x[i];
7754: sume2 += yhat * yhat ;
7755:
7756: denom = (ne * sumx2 - sumx*sumx);
7757: /* printf("ne=%d, i=%d,x[%d]=%f, y[%d]=%f sumx=%f, sumx2=%f, sumxy=%f, sumy=%f, sumy2=%f, denom=%f\n",ne,i,i,x[i],i,y[i], sumx, sumx2,sumxy, sumy, sumy2,denom); */
7758: }
7759: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
7760: *sa= *sb * sqrt(sumx2/ne);
7761:
7762: return 0;
7763: }
7764:
1.126 brouard 7765: /************ Prevalence ********************/
1.227 brouard 7766: void prevalence(double ***probs, double agemin, double agemax, int **s, double **agev, int nlstate, int imx, int *Tvar, int **nbcode, int *ncodemax,double **mint,double **anint, double dateprev1,double dateprev2, int firstpass, int lastpass)
7767: {
7768: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
7769: in each health status at the date of interview (if between dateprev1 and dateprev2).
7770: We still use firstpass and lastpass as another selection.
7771: */
1.126 brouard 7772:
1.227 brouard 7773: int i, m, jk, j1, bool, z1,j, iv;
7774: int mi; /* Effective wave */
7775: int iage;
1.359 brouard 7776: double agebegin; /*, ageend;*/
1.227 brouard 7777:
7778: double **prop;
7779: double posprop;
7780: double y2; /* in fractional years */
7781: int iagemin, iagemax;
7782: int first; /** to stop verbosity which is redirected to log file */
7783:
7784: iagemin= (int) agemin;
7785: iagemax= (int) agemax;
7786: /*pp=vector(1,nlstate);*/
1.251 brouard 7787: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 7788: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
7789: j1=0;
1.222 brouard 7790:
1.227 brouard 7791: /*j=cptcoveff;*/
7792: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 7793:
1.288 brouard 7794: first=0;
1.335 brouard 7795: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
1.227 brouard 7796: for (i=1; i<=nlstate; i++)
1.251 brouard 7797: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
1.227 brouard 7798: prop[i][iage]=0.0;
7799: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
7800: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
7801: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
7802:
7803: for (i=1; i<=imx; i++) { /* Each individual */
7804: bool=1;
7805: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
7806: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
7807: m=mw[mi][i];
7808: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
7809: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
7810: for (z1=1; z1<=cptcoveff; z1++){
7811: if( Fixed[Tmodelind[z1]]==1){
1.341 brouard 7812: iv= Tvar[Tmodelind[z1]];/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
1.332 brouard 7813: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
1.227 brouard 7814: bool=0;
7815: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
1.332 brouard 7816: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
1.227 brouard 7817: bool=0;
7818: }
7819: }
7820: if(bool==1){ /* Otherwise we skip that wave/person */
7821: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
7822: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
7823: if(m >=firstpass && m <=lastpass){
7824: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
7825: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
7826: if(agev[m][i]==0) agev[m][i]=iagemax+1;
7827: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.251 brouard 7828: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
1.227 brouard 7829: printf("Error on individual # %d agev[m][i]=%f <%d-%d or > %d+3+%d m=%d; either change agemin or agemax or fix data\n",i, agev[m][i],iagemin,AGEMARGE, iagemax,AGEMARGE,m);
7830: exit(1);
7831: }
7832: if (s[m][i]>0 && s[m][i]<=nlstate) {
7833: /*if(i>4620) printf(" i=%d m=%d s[m][i]=%d (int)agev[m][i]=%d weight[i]=%f prop=%f\n",i,m,s[m][i],(int)agev[m][m],weight[i],prop[s[m][i]][(int)agev[m][i]]);*/
7834: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
7835: prop[s[m][i]][iagemax+3] += weight[i];
7836: } /* end valid statuses */
7837: } /* end selection of dates */
7838: } /* end selection of waves */
7839: } /* end bool */
7840: } /* end wave */
7841: } /* end individual */
7842: for(i=iagemin; i <= iagemax+3; i++){
7843: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
7844: posprop += prop[jk][i];
7845: }
7846:
7847: for(jk=1; jk <=nlstate ; jk++){
7848: if( i <= iagemax){
7849: if(posprop>=1.e-5){
7850: probs[i][jk][j1]= prop[jk][i]/posprop;
7851: } else{
1.288 brouard 7852: if(!first){
7853: first=1;
1.266 brouard 7854: printf("Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases\nSee others in log file...\n",jk,i,jk, j1,probs[i][jk][j1]);
7855: }else{
1.288 brouard 7856: fprintf(ficlog,"Warning Observed prevalence doesn't sum to 1 for state %d: probs[%d][%d][%d]=%lf because of lack of cases.\n",jk,i,jk, j1,probs[i][jk][j1]);
1.227 brouard 7857: }
7858: }
7859: }
7860: }/* end jk */
7861: }/* end i */
1.222 brouard 7862: /*} *//* end i1 */
1.227 brouard 7863: } /* end j1 */
1.222 brouard 7864:
1.227 brouard 7865: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
7866: /*free_vector(pp,1,nlstate);*/
1.251 brouard 7867: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 7868: } /* End of prevalence */
1.126 brouard 7869:
7870: /************* Waves Concatenation ***************/
7871:
7872: void concatwav(int wav[], int **dh, int **bh, int **mw, int **s, double *agedc, double **agev, int firstpass, int lastpass, int imx, int nlstate, int stepm)
7873: {
1.298 brouard 7874: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i.
1.126 brouard 7875: Death is a valid wave (if date is known).
7876: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
7877: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.298 brouard 7878: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
1.227 brouard 7879: */
1.126 brouard 7880:
1.224 brouard 7881: int i=0, mi=0, m=0, mli=0;
1.126 brouard 7882: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
7883: double sum=0., jmean=0.;*/
1.224 brouard 7884: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 7885: int j, k=0,jk, ju, jl;
7886: double sum=0.;
7887: first=0;
1.214 brouard 7888: firstwo=0;
1.217 brouard 7889: firsthree=0;
1.218 brouard 7890: firstfour=0;
1.164 brouard 7891: jmin=100000;
1.126 brouard 7892: jmax=-1;
7893: jmean=0.;
1.224 brouard 7894:
7895: /* Treating live states */
1.214 brouard 7896: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 7897: mi=0; /* First valid wave */
1.227 brouard 7898: mli=0; /* Last valid wave */
1.309 brouard 7899: m=firstpass; /* Loop on waves */
7900: while(s[m][i] <= nlstate){ /* a live state or unknown state */
1.227 brouard 7901: if(m >firstpass && s[m][i]==s[m-1][i] && mint[m][i]==mint[m-1][i] && anint[m][i]==anint[m-1][i]){/* Two succesive identical information on wave m */
7902: mli=m-1;/* mw[++mi][i]=m-1; */
7903: }else if(s[m][i]>=1 || s[m][i]==-4 || s[m][i]==-5){ /* Since 0.98r4 if status=-2 vital status is really unknown, wave should be skipped */
1.309 brouard 7904: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */
1.227 brouard 7905: mli=m;
1.224 brouard 7906: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
7907: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 7908: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 7909: }
1.309 brouard 7910: else{ /* m = lastpass, eventual special issue with warning */
1.224 brouard 7911: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 7912: break;
1.224 brouard 7913: #else
1.317 brouard 7914: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){ /* no death date and known date of interview, case -2 (vital status unknown is warned later */
1.227 brouard 7915: if(firsthree == 0){
1.302 brouard 7916: printf("Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as 1-p_{%d%d} .\nOthers in log file only\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m, s[m][i], nlstate+ndeath);
1.227 brouard 7917: firsthree=1;
1.317 brouard 7918: }else if(firsthree >=1 && firsthree < 10){
7919: fprintf(ficlog,"Information! Unknown status for individual %ld line=%d occurred at last wave %d at known date %d/%d. Please, check if your unknown date of death %d/%d means a live state %d at wave %d. This case(%d)/wave(%d) contributes to the likelihood as 1-p_{%d%d} .\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], (int) moisdc[i], (int) andc[i], s[m][i], m, i, m, s[m][i], nlstate+ndeath);
7920: firsthree++;
7921: }else if(firsthree == 10){
7922: printf("Information, too many Information flags: no more reported to log either\n");
7923: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
7924: firsthree++;
7925: }else{
7926: firsthree++;
1.227 brouard 7927: }
1.309 brouard 7928: mw[++mi][i]=m; /* Valid transition with unknown status */
1.227 brouard 7929: mli=m;
7930: }
7931: if(s[m][i]==-2){ /* Vital status is really unknown */
7932: nbwarn++;
1.309 brouard 7933: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
1.227 brouard 7934: printf("Warning! Vital status for individual %ld (line=%d) at last wave %d interviewed at date %d/%d is unknown %d. Please, check if the vital status and the date of death %d/%d are really unknown. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], (int) moisdc[i], (int) andc[i], i, m);
7935: fprintf(ficlog,"Warning! Vital status for individual %ld (line=%d) at last wave %d interviewed at date %d/%d is unknown %d. Please, check if the vital status and the date of death %d/%d are really unknown. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], (int) moisdc[i], (int) andc[i], i, m);
7936: }
7937: break;
7938: }
7939: break;
1.224 brouard 7940: #endif
1.227 brouard 7941: }/* End m >= lastpass */
1.126 brouard 7942: }/* end while */
1.224 brouard 7943:
1.227 brouard 7944: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
1.216 brouard 7945: /* After last pass */
1.224 brouard 7946: /* Treating death states */
1.214 brouard 7947: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 7948: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
7949: /* } */
1.126 brouard 7950: mi++; /* Death is another wave */
7951: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 7952: /* Only death is a correct wave */
1.126 brouard 7953: mw[mi][i]=m;
1.257 brouard 7954: } /* else not in a death state */
1.224 brouard 7955: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.257 brouard 7956: else if ((int) andc[i] != 9999) { /* Date of death is known */
1.218 brouard 7957: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.309 brouard 7958: if((andc[i]+moisdc[i]/12.) <=(anint[m][i]+mint[m][i]/12.)){ /* month of death occured before last wave month and status should have been death instead of -1 */
1.227 brouard 7959: nbwarn++;
7960: if(firstfiv==0){
1.309 brouard 7961: printf("Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d, interviewed on %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
1.227 brouard 7962: firstfiv=1;
7963: }else{
1.309 brouard 7964: fprintf(ficlog,"Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d, interviewed on %d/%d and should have been coded as death instead of '%d'. This case (%d)/wave (%d) is contributing to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
1.227 brouard 7965: }
1.309 brouard 7966: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
7967: }else{ /* Month of Death occured afer last wave month, potential bias */
1.227 brouard 7968: nberr++;
7969: if(firstwo==0){
1.309 brouard 7970: printf("Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d with status %d. Potential bias if other individuals are still alive on this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood. Please add a new fictitious wave at the date of last vital status scan, with a dead status. See documentation\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
1.227 brouard 7971: firstwo=1;
7972: }
1.309 brouard 7973: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%d with status %d. Potential bias if other individuals are still alive on this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood. Please add a new fictitious wave at the date of last vital status scan, with a dead status. See documentation\n\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
1.227 brouard 7974: }
1.257 brouard 7975: }else{ /* if date of interview is unknown */
1.227 brouard 7976: /* death is known but not confirmed by death status at any wave */
7977: if(firstfour==0){
1.309 brouard 7978: printf("Error! Death for individual %ld line=%d occurred %d/%d but not confirmed by any death status for any wave, including last wave %d at unknown date %d/%d with status %d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\nOthers in log file only\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
1.227 brouard 7979: firstfour=1;
7980: }
1.309 brouard 7981: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred %d/%d but not confirmed by any death status for any wave, including last wave %d at unknown date %d/%d with status %d. Potential bias if other individuals are still alive at this date but ignored. This case (%d)/wave (%d) is skipped, no contribution to likelihood.\n",num[i],i,(int) moisdc[i], (int) andc[i], lastpass,(int)mint[m][i],(int)anint[m][i], s[m][i], i,m );
1.214 brouard 7982: }
1.224 brouard 7983: } /* end if date of death is known */
7984: #endif
1.309 brouard 7985: wav[i]=mi; /* mi should be the last effective wave (or mli), */
7986: /* wav[i]=mw[mi][i]; */
1.126 brouard 7987: if(mi==0){
7988: nbwarn++;
7989: if(first==0){
1.227 brouard 7990: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
7991: first=1;
1.126 brouard 7992: }
7993: if(first==1){
1.227 brouard 7994: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 7995: }
7996: } /* end mi==0 */
7997: } /* End individuals */
1.214 brouard 7998: /* wav and mw are no more changed */
1.223 brouard 7999:
1.317 brouard 8000: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
8001: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
8002:
8003:
1.126 brouard 8004: for(i=1; i<=imx; i++){
8005: for(mi=1; mi<wav[i];mi++){
8006: if (stepm <=0)
1.227 brouard 8007: dh[mi][i]=1;
1.126 brouard 8008: else{
1.260 brouard 8009: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
1.227 brouard 8010: if (agedc[i] < 2*AGESUP) {
8011: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
8012: if(j==0) j=1; /* Survives at least one month after exam */
8013: else if(j<0){
8014: nberr++;
1.359 brouard 8015: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
1.227 brouard 8016: j=1; /* Temporary Dangerous patch */
8017: printf(" We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
1.359 brouard 8018: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
1.227 brouard 8019: fprintf(ficlog," We assumed that the date of interview was correct (and not the date of death) and postponed the death %d month(s) (one stepm) after the interview. You MUST fix the contradiction between dates.\n",stepm);
8020: }
8021: k=k+1;
8022: if (j >= jmax){
8023: jmax=j;
8024: ijmax=i;
8025: }
8026: if (j <= jmin){
8027: jmin=j;
8028: ijmin=i;
8029: }
8030: sum=sum+j;
8031: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
8032: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
8033: }
8034: }
8035: else{
8036: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 8037: /* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
1.223 brouard 8038:
1.227 brouard 8039: k=k+1;
8040: if (j >= jmax) {
8041: jmax=j;
8042: ijmax=i;
8043: }
8044: else if (j <= jmin){
8045: jmin=j;
8046: ijmin=i;
8047: }
8048: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
8049: /*printf("%d %lf %d %d %d\n", i,agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);*/
8050: if(j<0){
8051: nberr++;
1.359 brouard 8052: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
8053: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld (around line %d) who is aged %.1f with statuses from %d to %d\n ",j,mw[mi][i],mw[mi+1][i],num[i], i,agev[mw[mi][i]][i],s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]);
1.227 brouard 8054: }
8055: sum=sum+j;
8056: }
8057: jk= j/stepm;
8058: jl= j -jk*stepm;
8059: ju= j -(jk+1)*stepm;
8060: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
8061: if(jl==0){
8062: dh[mi][i]=jk;
8063: bh[mi][i]=0;
8064: }else{ /* We want a negative bias in order to only have interpolation ie
8065: * to avoid the price of an extra matrix product in likelihood */
8066: dh[mi][i]=jk+1;
8067: bh[mi][i]=ju;
8068: }
8069: }else{
8070: if(jl <= -ju){
8071: dh[mi][i]=jk;
8072: bh[mi][i]=jl; /* bias is positive if real duration
8073: * is higher than the multiple of stepm and negative otherwise.
8074: */
8075: }
8076: else{
8077: dh[mi][i]=jk+1;
8078: bh[mi][i]=ju;
8079: }
8080: if(dh[mi][i]==0){
8081: dh[mi][i]=1; /* At least one step */
8082: bh[mi][i]=ju; /* At least one step */
8083: /* printf(" bh=%d ju=%d jl=%d dh=%d jk=%d stepm=%d %d\n",bh[mi][i],ju,jl,dh[mi][i],jk,stepm,i);*/
8084: }
8085: } /* end if mle */
1.126 brouard 8086: }
8087: } /* end wave */
8088: }
8089: jmean=sum/k;
8090: printf("Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, num[ijmin], jmax, num[ijmax], jmean);
1.141 brouard 8091: fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %d) Max=%d (%d) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
1.227 brouard 8092: }
1.126 brouard 8093:
8094: /*********** Tricode ****************************/
1.220 brouard 8095: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 8096: {
8097: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
8098: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
8099: * Boring subroutine which should only output nbcode[Tvar[j]][k]
8100: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
8101: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
8102: */
1.130 brouard 8103:
1.242 brouard 8104: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
8105: int modmaxcovj=0; /* Modality max of covariates j */
8106: int cptcode=0; /* Modality max of covariates j */
8107: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 8108:
8109:
1.242 brouard 8110: /* cptcoveff=0; */
8111: /* *cptcov=0; */
1.126 brouard 8112:
1.242 brouard 8113: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.285 brouard 8114: for (k=1; k <= maxncov; k++)
8115: for(j=1; j<=2; j++)
8116: nbcode[k][j]=0; /* Valgrind */
1.126 brouard 8117:
1.242 brouard 8118: /* Loop on covariates without age and products and no quantitative variable */
1.335 brouard 8119: for (k=1; k<=cptcovt; k++) { /* cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
1.242 brouard 8120: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
1.343 brouard 8121: /* printf("Testing k=%d, cptcovt=%d\n",k, cptcovt); */
1.349 brouard 8122: if(Dummy[k]==0 && Typevar[k] !=1 && Typevar[k] != 3 && Typevar[k] != 2){ /* Dummy covariate and not age product nor fixed product */
1.242 brouard 8123: switch(Fixed[k]) {
8124: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.311 brouard 8125: modmaxcovj=0;
8126: modmincovj=0;
1.242 brouard 8127: for (i=1; i<=imx; i++) { /* Loop on individuals: reads the data file to get the maximum value of the modality of this covariate Vj*/
1.339 brouard 8128: /* printf("Waiting for error tricode Tvar[%d]=%d i=%d (int)(covar[Tvar[k]][i]=%d\n",k,Tvar[k], i, (int)(covar[Tvar[k]][i])); */
1.242 brouard 8129: ij=(int)(covar[Tvar[k]][i]);
8130: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
8131: * If product of Vn*Vm, still boolean *:
8132: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
8133: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
8134: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
8135: modality of the nth covariate of individual i. */
8136: if (ij > modmaxcovj)
8137: modmaxcovj=ij;
8138: else if (ij < modmincovj)
8139: modmincovj=ij;
1.287 brouard 8140: if (ij <0 || ij >1 ){
1.311 brouard 8141: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8142: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8143: fflush(ficlog);
8144: exit(1);
1.287 brouard 8145: }
8146: if ((ij < -1) || (ij > NCOVMAX)){
1.242 brouard 8147: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
8148: exit(1);
8149: }else
8150: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
8151: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
8152: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
8153: /* getting the maximum value of the modality of the covariate
8154: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
8155: female ies 1, then modmaxcovj=1.
8156: */
8157: } /* end for loop on individuals i */
8158: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8159: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8160: cptcode=modmaxcovj;
8161: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
8162: /*for (i=0; i<=cptcode; i++) {*/
8163: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
8164: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8165: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8166: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
8167: if( j != -1){
8168: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
8169: covariate for which somebody answered excluding
8170: undefined. Usually 2: 0 and 1. */
8171: }
8172: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
8173: covariate for which somebody answered including
8174: undefined. Usually 3: -1, 0 and 1. */
8175: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
8176: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
8177: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 8178:
1.242 brouard 8179: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
8180: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
8181: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
8182: /* modmincovj=3; modmaxcovj = 7; */
8183: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
8184: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
8185: /* defining two dummy variables: variables V1_1 and V1_2.*/
8186: /* nbcode[Tvar[j]][ij]=k; */
8187: /* nbcode[Tvar[j]][1]=0; */
8188: /* nbcode[Tvar[j]][2]=1; */
8189: /* nbcode[Tvar[j]][3]=2; */
8190: /* To be continued (not working yet). */
8191: ij=0; /* ij is similar to i but can jump over null modalities */
1.287 brouard 8192:
8193: /* for (i=modmincovj; i<=modmaxcovj; i++) { */ /* i= 1 to 2 for dichotomous, or from 1 to 3 or from -1 or 0 to 1 currently*/
8194: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
8195: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
8196: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
8197: /*, could be restored in the future */
8198: for (i=0; i<=1; i++) { /* i= 1 to 2 for dichotomous, or from 1 to 3 or from -1 or 0 to 1 currently*/
1.242 brouard 8199: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
8200: break;
8201: }
8202: ij++;
1.287 brouard 8203: nbcode[Tvar[k]][ij]=i; /* stores the original value of modality i in an array nbcode, ij modality from 1 to last non-nul modality. nbcode[1][1]=0 nbcode[1][2]=1 . Could be -1*/
1.242 brouard 8204: cptcode = ij; /* New max modality for covar j */
8205: } /* end of loop on modality i=-1 to 1 or more */
8206: break;
8207: case 1: /* Testing on varying covariate, could be simple and
8208: * should look at waves or product of fixed *
8209: * varying. No time to test -1, assuming 0 and 1 only */
8210: ij=0;
8211: for(i=0; i<=1;i++){
8212: nbcode[Tvar[k]][++ij]=i;
8213: }
8214: break;
8215: default:
8216: break;
8217: } /* end switch */
8218: } /* end dummy test */
1.349 brouard 8219: if(Dummy[k]==1 && Typevar[k] !=1 && Typevar[k] !=3 && Fixed ==0){ /* Fixed Quantitative covariate and not age product */
1.311 brouard 8220: for (i=1; i<=imx; i++) { /* Loop on individuals: reads the data file to get the maximum value of the modality of this covariate Vj*/
1.335 brouard 8221: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
8222: printf("Error k=%d \n",k);
8223: exit(1);
8224: }
1.311 brouard 8225: if(isnan(covar[Tvar[k]][i])){
8226: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8227: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8228: fflush(ficlog);
8229: exit(1);
8230: }
8231: }
1.335 brouard 8232: } /* end Quanti */
1.287 brouard 8233: } /* end of loop on model-covariate k. nbcode[Tvark][1]=-1, nbcode[Tvark][1]=0 and nbcode[Tvark][2]=1 sets the value of covariate k*/
1.242 brouard 8234:
8235: for (k=-1; k< maxncov; k++) Ndum[k]=0;
8236: /* Look at fixed dummy (single or product) covariates to check empty modalities */
8237: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
8238: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
8239: ij=Tvar[i]; /* Tvar 5,4,3,6,5,7,1,4 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V4*age */
8240: Ndum[ij]++; /* Count the # of 1, 2 etc: {1,1,1,2,2,1,1} because V1 once, V2 once, two V4 and V5 in above */
8241: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
8242: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
8243:
8244: ij=0;
8245: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
1.335 brouard 8246: for (k=1; k<= cptcovt; k++) { /* cptcovt: total number of covariates of the model (2) nbocc(+)+1 = 8 excepting constant and age and age*age */
8247: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
1.242 brouard 8248: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
8249: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
1.335 brouard 8250: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
8251: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
8252: /* Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product*/
1.242 brouard 8253: /* If product not in single variable we don't print results */
8254: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
1.335 brouard 8255: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
8256: /* k= 1 2 3 4 5 6 7 8 9 */
8257: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
8258: /* ij 1 2 3 */
8259: /* Tvaraff[ij]= 4 3 1 */
8260: /* Tmodelind[ij]=2 3 9 */
8261: /* TmodelInvind[ij]=2 1 1 */
1.242 brouard 8262: Tvaraff[ij]=Tvar[k]; /* For printing combination *//* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, Tvar {5, 4, 3, 6, 5, 2, 7, 1, 1} Tvaraff={4, 3, 1} V4, V3, V1*/
8263: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
8264: TmodelInvind[ij]=Tvar[k]- ncovcol-nqv; /* Inverse TmodelInvind[2=V4]=2 second dummy varying cov (V4)4-1-1 {0, 2, 1, } TmodelInvind[3]=1 */
8265: if(Fixed[k]!=0)
8266: anyvaryingduminmodel=1;
8267: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
8268: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
8269: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
8270: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
8271: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
8272: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
8273: }
8274: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
8275: /* ij--; */
8276: /* cptcoveff=ij; /\*Number of total covariates*\/ */
1.335 brouard 8277: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
1.242 brouard 8278: * because they can be excluded from the model and real
8279: * if in the model but excluded because missing values, but how to get k from ij?*/
8280: for(j=ij+1; j<= cptcovt; j++){
8281: Tvaraff[j]=0;
8282: Tmodelind[j]=0;
8283: }
8284: for(j=ntveff+1; j<= cptcovt; j++){
8285: TmodelInvind[j]=0;
8286: }
8287: /* To be sorted */
8288: ;
8289: }
1.126 brouard 8290:
1.145 brouard 8291:
1.126 brouard 8292: /*********** Health Expectancies ****************/
8293:
1.235 brouard 8294: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[], int nres )
1.126 brouard 8295:
8296: {
8297: /* Health expectancies, no variances */
1.329 brouard 8298: /* cij is the combination in the list of combination of dummy covariates */
8299: /* strstart is a string of time at start of computing */
1.164 brouard 8300: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 8301: int nhstepma, nstepma; /* Decreasing with age */
8302: double age, agelim, hf;
8303: double ***p3mat;
8304: double eip;
8305:
1.238 brouard 8306: /* pstamp(ficreseij); */
1.126 brouard 8307: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
8308: fprintf(ficreseij,"# Age");
8309: for(i=1; i<=nlstate;i++){
8310: for(j=1; j<=nlstate;j++){
8311: fprintf(ficreseij," e%1d%1d ",i,j);
8312: }
8313: fprintf(ficreseij," e%1d. ",i);
8314: }
8315: fprintf(ficreseij,"\n");
8316:
8317:
8318: if(estepm < stepm){
8319: printf ("Problem %d lower than %d\n",estepm, stepm);
8320: }
8321: else hstepm=estepm;
8322: /* We compute the life expectancy from trapezoids spaced every estepm months
8323: * This is mainly to measure the difference between two models: for example
8324: * if stepm=24 months pijx are given only every 2 years and by summing them
8325: * we are calculating an estimate of the Life Expectancy assuming a linear
8326: * progression in between and thus overestimating or underestimating according
8327: * to the curvature of the survival function. If, for the same date, we
8328: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8329: * to compare the new estimate of Life expectancy with the same linear
8330: * hypothesis. A more precise result, taking into account a more precise
8331: * curvature will be obtained if estepm is as small as stepm. */
8332:
8333: /* For example we decided to compute the life expectancy with the smallest unit */
8334: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8335: nhstepm is the number of hstepm from age to agelim
8336: nstepm is the number of stepm from age to agelin.
1.270 brouard 8337: Look at hpijx to understand the reason which relies in memory size consideration
1.126 brouard 8338: and note for a fixed period like estepm months */
8339: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8340: survival function given by stepm (the optimization length). Unfortunately it
8341: means that if the survival funtion is printed only each two years of age and if
8342: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8343: results. So we changed our mind and took the option of the best precision.
8344: */
8345: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8346:
8347: agelim=AGESUP;
8348: /* If stepm=6 months */
8349: /* Computed by stepm unit matrices, product of hstepm matrices, stored
8350: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
8351:
8352: /* nhstepm age range expressed in number of stepm */
8353: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8354: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8355: /* if (stepm >= YEARM) hstepm=1;*/
8356: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8357: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8358:
8359: for (age=bage; age<=fage; age ++){
8360: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8361: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8362: /* if (stepm >= YEARM) hstepm=1;*/
8363: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
8364:
8365: /* If stepm=6 months */
8366: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8367: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
1.330 brouard 8368: /* printf("HELLO evsij Entering hpxij age=%d cij=%d hstepm=%d x[1]=%f nres=%d\n",(int) age, cij, hstepm, x[1], nres); */
1.235 brouard 8369: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 8370:
8371: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8372:
8373: printf("%d|",(int)age);fflush(stdout);
8374: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8375:
8376: /* Computing expectancies */
8377: for(i=1; i<=nlstate;i++)
8378: for(j=1; j<=nlstate;j++)
8379: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8380: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
8381:
8382: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
8383:
8384: }
8385:
8386: fprintf(ficreseij,"%3.0f",age );
8387: for(i=1; i<=nlstate;i++){
8388: eip=0;
8389: for(j=1; j<=nlstate;j++){
8390: eip +=eij[i][j][(int)age];
8391: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
8392: }
8393: fprintf(ficreseij,"%9.4f", eip );
8394: }
8395: fprintf(ficreseij,"\n");
8396:
8397: }
8398: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8399: printf("\n");
8400: fprintf(ficlog,"\n");
8401:
8402: }
8403:
1.235 brouard 8404: void cvevsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,double delti[],double **matcov,char strstart[], int nres )
1.126 brouard 8405:
8406: {
8407: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 8408: to initial status i, ei. .
1.126 brouard 8409: */
1.336 brouard 8410: /* Very time consuming function, but already optimized with precov */
1.126 brouard 8411: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
8412: int nhstepma, nstepma; /* Decreasing with age */
8413: double age, agelim, hf;
8414: double ***p3matp, ***p3matm, ***varhe;
8415: double **dnewm,**doldm;
8416: double *xp, *xm;
8417: double **gp, **gm;
8418: double ***gradg, ***trgradg;
8419: int theta;
8420:
8421: double eip, vip;
8422:
8423: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
8424: xp=vector(1,npar);
8425: xm=vector(1,npar);
8426: dnewm=matrix(1,nlstate*nlstate,1,npar);
8427: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
8428:
8429: pstamp(ficresstdeij);
8430: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
8431: fprintf(ficresstdeij,"# Age");
8432: for(i=1; i<=nlstate;i++){
8433: for(j=1; j<=nlstate;j++)
8434: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
8435: fprintf(ficresstdeij," e%1d. ",i);
8436: }
8437: fprintf(ficresstdeij,"\n");
8438:
8439: pstamp(ficrescveij);
8440: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
8441: fprintf(ficrescveij,"# Age");
8442: for(i=1; i<=nlstate;i++)
8443: for(j=1; j<=nlstate;j++){
8444: cptj= (j-1)*nlstate+i;
8445: for(i2=1; i2<=nlstate;i2++)
8446: for(j2=1; j2<=nlstate;j2++){
8447: cptj2= (j2-1)*nlstate+i2;
8448: if(cptj2 <= cptj)
8449: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
8450: }
8451: }
8452: fprintf(ficrescveij,"\n");
8453:
8454: if(estepm < stepm){
8455: printf ("Problem %d lower than %d\n",estepm, stepm);
8456: }
8457: else hstepm=estepm;
8458: /* We compute the life expectancy from trapezoids spaced every estepm months
8459: * This is mainly to measure the difference between two models: for example
8460: * if stepm=24 months pijx are given only every 2 years and by summing them
8461: * we are calculating an estimate of the Life Expectancy assuming a linear
8462: * progression in between and thus overestimating or underestimating according
8463: * to the curvature of the survival function. If, for the same date, we
8464: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8465: * to compare the new estimate of Life expectancy with the same linear
8466: * hypothesis. A more precise result, taking into account a more precise
8467: * curvature will be obtained if estepm is as small as stepm. */
8468:
8469: /* For example we decided to compute the life expectancy with the smallest unit */
8470: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8471: nhstepm is the number of hstepm from age to agelim
8472: nstepm is the number of stepm from age to agelin.
8473: Look at hpijx to understand the reason of that which relies in memory size
8474: and note for a fixed period like estepm months */
8475: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8476: survival function given by stepm (the optimization length). Unfortunately it
8477: means that if the survival funtion is printed only each two years of age and if
8478: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8479: results. So we changed our mind and took the option of the best precision.
8480: */
8481: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8482:
8483: /* If stepm=6 months */
8484: /* nhstepm age range expressed in number of stepm */
8485: agelim=AGESUP;
8486: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
8487: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8488: /* if (stepm >= YEARM) hstepm=1;*/
8489: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8490:
8491: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8492: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8493: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
8494: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
8495: gp=matrix(0,nhstepm,1,nlstate*nlstate);
8496: gm=matrix(0,nhstepm,1,nlstate*nlstate);
8497:
8498: for (age=bage; age<=fage; age ++){
8499: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8500: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8501: /* if (stepm >= YEARM) hstepm=1;*/
8502: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 8503:
1.126 brouard 8504: /* If stepm=6 months */
8505: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8506: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
8507:
8508: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 8509:
1.126 brouard 8510: /* Computing Variances of health expectancies */
8511: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
8512: decrease memory allocation */
8513: for(theta=1; theta <=npar; theta++){
8514: for(i=1; i<=npar; i++){
1.222 brouard 8515: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8516: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 8517: }
1.235 brouard 8518: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
8519: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 8520:
1.126 brouard 8521: for(j=1; j<= nlstate; j++){
1.222 brouard 8522: for(i=1; i<=nlstate; i++){
8523: for(h=0; h<=nhstepm-1; h++){
8524: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
8525: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
8526: }
8527: }
1.126 brouard 8528: }
1.218 brouard 8529:
1.126 brouard 8530: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 8531: for(h=0; h<=nhstepm-1; h++){
8532: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
8533: }
1.126 brouard 8534: }/* End theta */
8535:
8536:
8537: for(h=0; h<=nhstepm-1; h++)
8538: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 8539: for(theta=1; theta <=npar; theta++)
8540: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 8541:
1.218 brouard 8542:
1.222 brouard 8543: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 8544: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 8545: varhe[ij][ji][(int)age] =0.;
1.218 brouard 8546:
1.222 brouard 8547: printf("%d|",(int)age);fflush(stdout);
8548: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8549: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 8550: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 8551: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
8552: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
8553: for(ij=1;ij<=nlstate*nlstate;ij++)
8554: for(ji=1;ji<=nlstate*nlstate;ji++)
8555: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 8556: }
8557: }
1.320 brouard 8558: /* if((int)age ==50){ */
8559: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
8560: /* } */
1.126 brouard 8561: /* Computing expectancies */
1.235 brouard 8562: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 8563: for(i=1; i<=nlstate;i++)
8564: for(j=1; j<=nlstate;j++)
1.222 brouard 8565: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8566: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 8567:
1.222 brouard 8568: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
1.218 brouard 8569:
1.222 brouard 8570: }
1.269 brouard 8571:
8572: /* Standard deviation of expectancies ij */
1.126 brouard 8573: fprintf(ficresstdeij,"%3.0f",age );
8574: for(i=1; i<=nlstate;i++){
8575: eip=0.;
8576: vip=0.;
8577: for(j=1; j<=nlstate;j++){
1.222 brouard 8578: eip += eij[i][j][(int)age];
8579: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
8580: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
8581: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
1.126 brouard 8582: }
8583: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
8584: }
8585: fprintf(ficresstdeij,"\n");
1.218 brouard 8586:
1.269 brouard 8587: /* Variance of expectancies ij */
1.126 brouard 8588: fprintf(ficrescveij,"%3.0f",age );
8589: for(i=1; i<=nlstate;i++)
8590: for(j=1; j<=nlstate;j++){
1.222 brouard 8591: cptj= (j-1)*nlstate+i;
8592: for(i2=1; i2<=nlstate;i2++)
8593: for(j2=1; j2<=nlstate;j2++){
8594: cptj2= (j2-1)*nlstate+i2;
8595: if(cptj2 <= cptj)
8596: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
8597: }
1.126 brouard 8598: }
8599: fprintf(ficrescveij,"\n");
1.218 brouard 8600:
1.126 brouard 8601: }
8602: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
8603: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
8604: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
8605: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
8606: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8607: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8608: printf("\n");
8609: fprintf(ficlog,"\n");
1.218 brouard 8610:
1.126 brouard 8611: free_vector(xm,1,npar);
8612: free_vector(xp,1,npar);
8613: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
8614: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
8615: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
8616: }
1.218 brouard 8617:
1.126 brouard 8618: /************ Variance ******************/
1.235 brouard 8619: void varevsij(char optionfilefiname[], double ***vareij, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, int estepm, int cptcov, int cptcod, int popbased, int mobilav, char strstart[], int nres)
1.218 brouard 8620: {
1.361 brouard 8621: /** Computes the matrix of variance covariance of health expectancies e.j= sum_i w_i e_ij where w_i depends of popbased,
8622: * either cross-sectional or implied.
8623: * return vareij[i][j][(int)age]=cov(e.i,e.j)=sum_h sum_k trgrad(h_p.i) V(theta) grad(k_p.k) Equation 20
1.279 brouard 8624: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
8625: * double **newm;
8626: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
8627: */
1.218 brouard 8628:
8629: /* int movingaverage(); */
8630: double **dnewm,**doldm;
8631: double **dnewmp,**doldmp;
8632: int i, j, nhstepm, hstepm, h, nstepm ;
1.288 brouard 8633: int first=0;
1.218 brouard 8634: int k;
8635: double *xp;
1.279 brouard 8636: double **gp, **gm; /**< for var eij */
8637: double ***gradg, ***trgradg; /**< for var eij */
8638: double **gradgp, **trgradgp; /**< for var p point j */
8639: double *gpp, *gmp; /**< for var p point j */
1.362 brouard 8640: double **varppt; /**< for var p.3 p.death nlstate+1 to nlstate+ndeath */
1.218 brouard 8641: double ***p3mat;
8642: double age,agelim, hf;
8643: /* double ***mobaverage; */
8644: int theta;
8645: char digit[4];
8646: char digitp[25];
8647:
8648: char fileresprobmorprev[FILENAMELENGTH];
8649:
8650: if(popbased==1){
8651: if(mobilav!=0)
8652: strcpy(digitp,"-POPULBASED-MOBILAV_");
8653: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
8654: }
8655: else
8656: strcpy(digitp,"-STABLBASED_");
1.126 brouard 8657:
1.218 brouard 8658: /* if (mobilav!=0) { */
8659: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8660: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
8661: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
8662: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
8663: /* } */
8664: /* } */
8665:
8666: strcpy(fileresprobmorprev,"PRMORPREV-");
8667: sprintf(digit,"%-d",ij);
8668: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
8669: strcat(fileresprobmorprev,digit); /* Tvar to be done */
8670: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
8671: strcat(fileresprobmorprev,fileresu);
8672: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
8673: printf("Problem with resultfile: %s\n", fileresprobmorprev);
8674: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
8675: }
8676: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8677: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8678: pstamp(ficresprobmorprev);
8679: fprintf(ficresprobmorprev,"# probabilities of dying before estepm=%d months for people of exact age and weighted probabilities w1*p1j+w2*p2j+... stand dev in()\n",estepm);
1.238 brouard 8680: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
1.337 brouard 8681:
8682: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
8683: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
8684: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
8685: /* } */
8686: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
1.344 brouard 8687: /* fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); */
1.337 brouard 8688: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 8689: }
1.337 brouard 8690: /* for(j=1;j<=cptcoveff;j++) */
8691: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
1.238 brouard 8692: fprintf(ficresprobmorprev,"\n");
8693:
1.218 brouard 8694: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
8695: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
8696: fprintf(ficresprobmorprev," p.%-d SE",j);
8697: for(i=1; i<=nlstate;i++)
8698: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
8699: }
8700: fprintf(ficresprobmorprev,"\n");
8701:
8702: fprintf(ficgp,"\n# Routine varevsij");
8703: fprintf(ficgp,"\nunset title \n");
8704: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
8705: fprintf(fichtm,"\n<li><h4> Computing probabilities of dying over estepm months as a weighted average (i.e global mortality independent of initial healh state)</h4></li>\n");
8706: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
1.279 brouard 8707:
1.361 brouard 8708: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); /* In fact, currently a double */
1.218 brouard 8709: pstamp(ficresvij);
8710: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
8711: if(popbased==1)
8712: fprintf(ficresvij,"the age specific prevalence observed (cross-sectionally) in the population i.e cross-sectionally\n in each health state (popbased=1) (mobilav=%d\n",mobilav);
8713: else
8714: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
8715: fprintf(ficresvij,"# Age");
8716: for(i=1; i<=nlstate;i++)
8717: for(j=1; j<=nlstate;j++)
8718: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
8719: fprintf(ficresvij,"\n");
8720:
8721: xp=vector(1,npar);
8722: dnewm=matrix(1,nlstate,1,npar);
8723: doldm=matrix(1,nlstate,1,nlstate);
8724: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
8725: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8726:
8727: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
8728: gpp=vector(nlstate+1,nlstate+ndeath);
8729: gmp=vector(nlstate+1,nlstate+ndeath);
8730: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 8731:
1.218 brouard 8732: if(estepm < stepm){
8733: printf ("Problem %d lower than %d\n",estepm, stepm);
8734: }
8735: else hstepm=estepm;
8736: /* For example we decided to compute the life expectancy with the smallest unit */
8737: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8738: nhstepm is the number of hstepm from age to agelim
8739: nstepm is the number of stepm from age to agelim.
8740: Look at function hpijx to understand why because of memory size limitations,
8741: we decided (b) to get a life expectancy respecting the most precise curvature of the
8742: survival function given by stepm (the optimization length). Unfortunately it
8743: means that if the survival funtion is printed every two years of age and if
8744: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8745: results. So we changed our mind and took the option of the best precision.
8746: */
8747: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8748: agelim = AGESUP;
8749: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
8750: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8751: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8752: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8753: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
8754: gp=matrix(0,nhstepm,1,nlstate);
8755: gm=matrix(0,nhstepm,1,nlstate);
8756:
8757:
8758: for(theta=1; theta <=npar; theta++){
8759: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
8760: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8761: }
1.279 brouard 8762: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
8763: * returns into prlim .
1.288 brouard 8764: */
1.242 brouard 8765: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.279 brouard 8766:
8767: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
1.218 brouard 8768: if (popbased==1) {
8769: if(mobilav ==0){
8770: for(i=1; i<=nlstate;i++)
8771: prlim[i][i]=probs[(int)age][i][ij];
8772: }else{ /* mobilav */
8773: for(i=1; i<=nlstate;i++)
8774: prlim[i][i]=mobaverage[(int)age][i][ij];
8775: }
8776: }
1.361 brouard 8777: /**< Computes the shifted plus (gp) transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
1.279 brouard 8778: */
8779: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */
1.292 brouard 8780: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability
1.279 brouard 8781: * at horizon h in state j including mortality.
8782: */
1.218 brouard 8783: for(j=1; j<= nlstate; j++){
8784: for(h=0; h<=nhstepm; h++){
8785: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
1.361 brouard 8786: gp[h][j] += prlim[i][i]*p3mat[i][j][h]; /* gp[h][j]= w_i h_pij */
1.218 brouard 8787: }
8788: }
1.279 brouard 8789: /* Next for computing shifted+ probability of death (h=1 means
1.218 brouard 8790: computed over hstepm matrices product = hstepm*stepm months)
1.279 brouard 8791: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
1.218 brouard 8792: */
1.361 brouard 8793: for(j=nlstate+1;j<=nlstate+ndeath;j++){ /* Currently only once for theta plus p.3(age) Sum_i wi pi3*/
1.218 brouard 8794: for(i=1,gpp[j]=0.; i<= nlstate; i++)
8795: gpp[j] += prlim[i][i]*p3mat[i][j][1];
1.279 brouard 8796: }
8797:
8798: /* Again with minus shift */
1.218 brouard 8799:
8800: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
8801: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 8802:
1.242 brouard 8803: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 8804:
8805: if (popbased==1) {
8806: if(mobilav ==0){
8807: for(i=1; i<=nlstate;i++)
8808: prlim[i][i]=probs[(int)age][i][ij];
8809: }else{ /* mobilav */
8810: for(i=1; i<=nlstate;i++)
8811: prlim[i][i]=mobaverage[(int)age][i][ij];
8812: }
8813: }
8814:
1.361 brouard 8815: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Still minus */
1.218 brouard 8816:
1.361 brouard 8817: for(j=1; j<= nlstate; j++){ /* gm[h][j]= Sum_i of wi * pij = h_p.j */
1.218 brouard 8818: for(h=0; h<=nhstepm; h++){
8819: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
8820: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
8821: }
8822: }
8823: /* This for computing probability of death (h=1 means
8824: computed over hstepm matrices product = hstepm*stepm months)
1.361 brouard 8825: as a weighted average of prlim. j is death. gmp[3]=sum_i w_i*p_i3=p.3 minus theta
1.218 brouard 8826: */
1.361 brouard 8827: for(j=nlstate+1;j<=nlstate+ndeath;j++){ /* Currently only once theta_minus p.3=Sum_i wi pi3*/
1.218 brouard 8828: for(i=1,gmp[j]=0.; i<= nlstate; i++)
8829: gmp[j] += prlim[i][i]*p3mat[i][j][1];
8830: }
1.279 brouard 8831: /* end shifting computations */
8832:
1.361 brouard 8833: /**< Computing gradient of p.j matrix at horizon h and still for one parameter of vector theta
8834: * equation 31 and 32
1.279 brouard 8835: */
1.361 brouard 8836: for(j=1; j<= nlstate; j++) /* computes grad p.j(x, over each h) where p.j is Sum_i w_i*pij(x over h)
8837: * equation 24 */
1.218 brouard 8838: for(h=0; h<=nhstepm; h++){
8839: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
8840: }
1.361 brouard 8841: /**< Gradient of overall mortality p.3 (or p.death)
1.279 brouard 8842: */
1.361 brouard 8843: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* computes grad of p.3 from wi+pi3 grad p.3 (theta) */
1.218 brouard 8844: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
8845: }
8846:
8847: } /* End theta */
1.279 brouard 8848:
1.361 brouard 8849: /* We got the gradient matrix for each theta and each state j of gradg(h]theta][j)=grad(_hp.j(theta) */
8850: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar);
1.218 brouard 8851:
1.361 brouard 8852: for(h=0; h<=nhstepm; h++) /* veij */ /* computes the transposed of grad (_hp.j(theta)*/
1.218 brouard 8853: for(j=1; j<=nlstate;j++)
8854: for(theta=1; theta <=npar; theta++)
8855: trgradg[h][j][theta]=gradg[h][theta][j];
8856:
1.361 brouard 8857: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* computes transposed of grad p.3 (theta)*/
1.218 brouard 8858: for(theta=1; theta <=npar; theta++)
8859: trgradgp[j][theta]=gradgp[theta][j];
1.279 brouard 8860: /**< as well as its transposed matrix
8861: */
1.218 brouard 8862:
8863: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8864: for(i=1;i<=nlstate;i++)
8865: for(j=1;j<=nlstate;j++)
8866: vareij[i][j][(int)age] =0.;
1.279 brouard 8867:
8868: /* Computing trgradg by matcov by gradg at age and summing over h
1.361 brouard 8869: * and k (nhstepm) formula 32 of article
8870: * Lievre-Brouard-Heathcote so that for each j, computes the cov(e.j,e.k) (formula 31).
8871: * for given h and k computes trgradg[h](i,j) matcov (theta) gradg(k)(i,j) into vareij[i][j] which is
8872: cov(e.i,e.j) and sums on h and k
8873: * including the covariances.
1.279 brouard 8874: */
8875:
1.218 brouard 8876: for(h=0;h<=nhstepm;h++){
8877: for(k=0;k<=nhstepm;k++){
8878: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
8879: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
8880: for(i=1;i<=nlstate;i++)
8881: for(j=1;j<=nlstate;j++)
1.361 brouard 8882: vareij[i][j][(int)age] += doldm[i][j]*hf*hf; /* This is vareij=sum_h sum_k trgrad(h_pij) V(theta) grad(k_pij)
8883: including the covariances of e.j */
1.218 brouard 8884: }
8885: }
8886:
1.361 brouard 8887: /* Mortality: pptj is p.3 or p.death = trgradgp by cov by gradgp, variance of
8888: * p.3=1-p..=1-sum i p.i overall mortality computed directly because
1.279 brouard 8889: * we compute the grad (wix pijx) instead of grad (pijx),even if
1.361 brouard 8890: * wix is independent of theta.
1.279 brouard 8891: */
1.218 brouard 8892: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
8893: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
8894: for(j=nlstate+1;j<=nlstate+ndeath;j++)
8895: for(i=nlstate+1;i<=nlstate+ndeath;i++)
1.361 brouard 8896: varppt[j][i]=doldmp[j][i]; /* This is the variance of p.3 */
1.218 brouard 8897: /* end ppptj */
8898: /* x centered again */
8899:
1.242 brouard 8900: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 8901:
8902: if (popbased==1) {
8903: if(mobilav ==0){
8904: for(i=1; i<=nlstate;i++)
8905: prlim[i][i]=probs[(int)age][i][ij];
8906: }else{ /* mobilav */
8907: for(i=1; i<=nlstate;i++)
8908: prlim[i][i]=mobaverage[(int)age][i][ij];
8909: }
8910: }
8911:
8912: /* This for computing probability of death (h=1 means
8913: computed over hstepm (estepm) matrices product = hstepm*stepm months)
8914: as a weighted average of prlim.
8915: */
1.235 brouard 8916: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 8917: for(j=nlstate+1;j<=nlstate+ndeath;j++){
8918: for(i=1,gmp[j]=0.;i<= nlstate; i++)
1.361 brouard 8919: gmp[j] += prlim[i][i]*p3mat[i][j][1]; /* gmp[j] is p.3 */
1.218 brouard 8920: }
8921: /* end probability of death */
8922:
8923: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
8924: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
1.361 brouard 8925: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));/* p.3 (STD p.3) */
1.218 brouard 8926: for(i=1; i<=nlstate;i++){
1.361 brouard 8927: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]); /* wi, pi3 */
1.218 brouard 8928: }
8929: }
8930: fprintf(ficresprobmorprev,"\n");
8931:
8932: fprintf(ficresvij,"%.0f ",age );
8933: for(i=1; i<=nlstate;i++)
8934: for(j=1; j<=nlstate;j++){
8935: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
8936: }
8937: fprintf(ficresvij,"\n");
8938: free_matrix(gp,0,nhstepm,1,nlstate);
8939: free_matrix(gm,0,nhstepm,1,nlstate);
8940: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
8941: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
8942: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8943: } /* End age */
8944: free_vector(gpp,nlstate+1,nlstate+ndeath);
8945: free_vector(gmp,nlstate+1,nlstate+ndeath);
8946: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
8947: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
8948: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
8949: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
8950: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
8951: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
8952: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
8953: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
8954: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
8955: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
8956: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
8957: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
8958: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
8959: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
8960: fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months. <br> <img src=\"%s%s.svg\"> <br>\n", estepm,subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
8961: /* fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.svg\"> <br>\n", stepm,YEARM,digitp,digit);
1.126 brouard 8962: */
1.218 brouard 8963: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
8964: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 8965:
1.218 brouard 8966: free_vector(xp,1,npar);
8967: free_matrix(doldm,1,nlstate,1,nlstate);
8968: free_matrix(dnewm,1,nlstate,1,npar);
8969: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8970: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
8971: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8972: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8973: fclose(ficresprobmorprev);
8974: fflush(ficgp);
8975: fflush(fichtm);
8976: } /* end varevsij */
1.126 brouard 8977:
8978: /************ Variance of prevlim ******************/
1.269 brouard 8979: void varprevlim(char fileresvpl[], FILE *ficresvpl, double **varpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **prlim, double ftolpl, int *ncvyearp, int ij, char strstart[], int nres)
1.126 brouard 8980: {
1.205 brouard 8981: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 8982: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 8983:
1.268 brouard 8984: double **dnewmpar,**doldm;
1.126 brouard 8985: int i, j, nhstepm, hstepm;
8986: double *xp;
8987: double *gp, *gm;
8988: double **gradg, **trgradg;
1.208 brouard 8989: double **mgm, **mgp;
1.126 brouard 8990: double age,agelim;
8991: int theta;
8992:
8993: pstamp(ficresvpl);
1.288 brouard 8994: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
1.241 brouard 8995: fprintf(ficresvpl,"# Age ");
8996: if(nresult >=1)
8997: fprintf(ficresvpl," Result# ");
1.126 brouard 8998: for(i=1; i<=nlstate;i++)
8999: fprintf(ficresvpl," %1d-%1d",i,i);
9000: fprintf(ficresvpl,"\n");
9001:
9002: xp=vector(1,npar);
1.268 brouard 9003: dnewmpar=matrix(1,nlstate,1,npar);
1.126 brouard 9004: doldm=matrix(1,nlstate,1,nlstate);
9005:
9006: hstepm=1*YEARM; /* Every year of age */
9007: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
9008: agelim = AGESUP;
9009: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
9010: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9011: if (stepm >= YEARM) hstepm=1;
9012: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9013: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 9014: mgp=matrix(1,npar,1,nlstate);
9015: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 9016: gp=vector(1,nlstate);
9017: gm=vector(1,nlstate);
9018:
9019: for(theta=1; theta <=npar; theta++){
9020: for(i=1; i<=npar; i++){ /* Computes gradient */
9021: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9022: }
1.288 brouard 9023: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
9024: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
9025: /* else */
9026: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 9027: for(i=1;i<=nlstate;i++){
1.126 brouard 9028: gp[i] = prlim[i][i];
1.208 brouard 9029: mgp[theta][i] = prlim[i][i];
9030: }
1.126 brouard 9031: for(i=1; i<=npar; i++) /* Computes gradient */
9032: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 9033: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
9034: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
9035: /* else */
9036: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 9037: for(i=1;i<=nlstate;i++){
1.126 brouard 9038: gm[i] = prlim[i][i];
1.208 brouard 9039: mgm[theta][i] = prlim[i][i];
9040: }
1.126 brouard 9041: for(i=1;i<=nlstate;i++)
9042: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 9043: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 9044: } /* End theta */
9045:
9046: trgradg =matrix(1,nlstate,1,npar);
9047:
9048: for(j=1; j<=nlstate;j++)
9049: for(theta=1; theta <=npar; theta++)
9050: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 9051: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9052: /* printf("\nmgm mgp %d ",(int)age); */
9053: /* for(j=1; j<=nlstate;j++){ */
9054: /* printf(" %d ",j); */
9055: /* for(theta=1; theta <=npar; theta++) */
9056: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9057: /* printf("\n "); */
9058: /* } */
9059: /* } */
9060: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9061: /* printf("\n gradg %d ",(int)age); */
9062: /* for(j=1; j<=nlstate;j++){ */
9063: /* printf("%d ",j); */
9064: /* for(theta=1; theta <=npar; theta++) */
9065: /* printf("%d %lf ",theta,gradg[theta][j]); */
9066: /* printf("\n "); */
9067: /* } */
9068: /* } */
1.126 brouard 9069:
9070: for(i=1;i<=nlstate;i++)
9071: varpl[i][(int)age] =0.;
1.209 brouard 9072: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.268 brouard 9073: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9074: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 9075: }else{
1.268 brouard 9076: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9077: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 9078: }
1.126 brouard 9079: for(i=1;i<=nlstate;i++)
9080: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9081:
9082: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 9083: if(nresult >=1)
9084: fprintf(ficresvpl,"%d ",nres );
1.288 brouard 9085: for(i=1; i<=nlstate;i++){
1.126 brouard 9086: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1.288 brouard 9087: /* for(j=1;j<=nlstate;j++) */
9088: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
9089: }
1.126 brouard 9090: fprintf(ficresvpl,"\n");
9091: free_vector(gp,1,nlstate);
9092: free_vector(gm,1,nlstate);
1.208 brouard 9093: free_matrix(mgm,1,npar,1,nlstate);
9094: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 9095: free_matrix(gradg,1,npar,1,nlstate);
9096: free_matrix(trgradg,1,nlstate,1,npar);
9097: } /* End age */
9098:
9099: free_vector(xp,1,npar);
9100: free_matrix(doldm,1,nlstate,1,npar);
1.268 brouard 9101: free_matrix(dnewmpar,1,nlstate,1,nlstate);
9102:
9103: }
9104:
9105:
9106: /************ Variance of backprevalence limit ******************/
1.269 brouard 9107: void varbrevlim(char fileresvbl[], FILE *ficresvbl, double **varbpl, double **matcov, double x[], double delti[], int nlstate, int stepm, double bage, double fage, double **oldm, double **savm, double **bprlim, double ftolpl, int mobilavproj, int *ncvyearp, int ij, char strstart[], int nres)
1.268 brouard 9108: {
9109: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
9110: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
9111:
9112: double **dnewmpar,**doldm;
9113: int i, j, nhstepm, hstepm;
9114: double *xp;
9115: double *gp, *gm;
9116: double **gradg, **trgradg;
9117: double **mgm, **mgp;
9118: double age,agelim;
9119: int theta;
9120:
9121: pstamp(ficresvbl);
9122: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
9123: fprintf(ficresvbl,"# Age ");
9124: if(nresult >=1)
9125: fprintf(ficresvbl," Result# ");
9126: for(i=1; i<=nlstate;i++)
9127: fprintf(ficresvbl," %1d-%1d",i,i);
9128: fprintf(ficresvbl,"\n");
9129:
9130: xp=vector(1,npar);
9131: dnewmpar=matrix(1,nlstate,1,npar);
9132: doldm=matrix(1,nlstate,1,nlstate);
9133:
9134: hstepm=1*YEARM; /* Every year of age */
9135: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
9136: agelim = AGEINF;
9137: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
9138: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9139: if (stepm >= YEARM) hstepm=1;
9140: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9141: gradg=matrix(1,npar,1,nlstate);
9142: mgp=matrix(1,npar,1,nlstate);
9143: mgm=matrix(1,npar,1,nlstate);
9144: gp=vector(1,nlstate);
9145: gm=vector(1,nlstate);
9146:
9147: for(theta=1; theta <=npar; theta++){
9148: for(i=1; i<=npar; i++){ /* Computes gradient */
9149: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9150: }
9151: if(mobilavproj > 0 )
9152: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9153: else
9154: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9155: for(i=1;i<=nlstate;i++){
9156: gp[i] = bprlim[i][i];
9157: mgp[theta][i] = bprlim[i][i];
9158: }
9159: for(i=1; i<=npar; i++) /* Computes gradient */
9160: xp[i] = x[i] - (i==theta ?delti[theta]:0);
9161: if(mobilavproj > 0 )
9162: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9163: else
9164: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9165: for(i=1;i<=nlstate;i++){
9166: gm[i] = bprlim[i][i];
9167: mgm[theta][i] = bprlim[i][i];
9168: }
9169: for(i=1;i<=nlstate;i++)
9170: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
9171: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
9172: } /* End theta */
9173:
9174: trgradg =matrix(1,nlstate,1,npar);
9175:
9176: for(j=1; j<=nlstate;j++)
9177: for(theta=1; theta <=npar; theta++)
9178: trgradg[j][theta]=gradg[theta][j];
9179: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9180: /* printf("\nmgm mgp %d ",(int)age); */
9181: /* for(j=1; j<=nlstate;j++){ */
9182: /* printf(" %d ",j); */
9183: /* for(theta=1; theta <=npar; theta++) */
9184: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9185: /* printf("\n "); */
9186: /* } */
9187: /* } */
9188: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9189: /* printf("\n gradg %d ",(int)age); */
9190: /* for(j=1; j<=nlstate;j++){ */
9191: /* printf("%d ",j); */
9192: /* for(theta=1; theta <=npar; theta++) */
9193: /* printf("%d %lf ",theta,gradg[theta][j]); */
9194: /* printf("\n "); */
9195: /* } */
9196: /* } */
9197:
9198: for(i=1;i<=nlstate;i++)
9199: varbpl[i][(int)age] =0.;
9200: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
9201: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9202: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9203: }else{
9204: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9205: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9206: }
9207: for(i=1;i<=nlstate;i++)
9208: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9209:
9210: fprintf(ficresvbl,"%.0f ",age );
9211: if(nresult >=1)
9212: fprintf(ficresvbl,"%d ",nres );
9213: for(i=1; i<=nlstate;i++)
9214: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
9215: fprintf(ficresvbl,"\n");
9216: free_vector(gp,1,nlstate);
9217: free_vector(gm,1,nlstate);
9218: free_matrix(mgm,1,npar,1,nlstate);
9219: free_matrix(mgp,1,npar,1,nlstate);
9220: free_matrix(gradg,1,npar,1,nlstate);
9221: free_matrix(trgradg,1,nlstate,1,npar);
9222: } /* End age */
9223:
9224: free_vector(xp,1,npar);
9225: free_matrix(doldm,1,nlstate,1,npar);
9226: free_matrix(dnewmpar,1,nlstate,1,nlstate);
1.126 brouard 9227:
9228: }
9229:
9230: /************ Variance of one-step probabilities ******************/
9231: void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
1.222 brouard 9232: {
9233: int i, j=0, k1, l1, tj;
9234: int k2, l2, j1, z1;
9235: int k=0, l;
9236: int first=1, first1, first2;
1.326 brouard 9237: int nres=0; /* New */
1.222 brouard 9238: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
9239: double **dnewm,**doldm;
9240: double *xp;
9241: double *gp, *gm;
9242: double **gradg, **trgradg;
9243: double **mu;
9244: double age, cov[NCOVMAX+1];
9245: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
9246: int theta;
9247: char fileresprob[FILENAMELENGTH];
9248: char fileresprobcov[FILENAMELENGTH];
9249: char fileresprobcor[FILENAMELENGTH];
9250: double ***varpij;
9251:
9252: strcpy(fileresprob,"PROB_");
1.356 brouard 9253: strcat(fileresprob,fileresu);
1.222 brouard 9254: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
9255: printf("Problem with resultfile: %s\n", fileresprob);
9256: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
9257: }
9258: strcpy(fileresprobcov,"PROBCOV_");
9259: strcat(fileresprobcov,fileresu);
9260: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
9261: printf("Problem with resultfile: %s\n", fileresprobcov);
9262: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
9263: }
9264: strcpy(fileresprobcor,"PROBCOR_");
9265: strcat(fileresprobcor,fileresu);
9266: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
9267: printf("Problem with resultfile: %s\n", fileresprobcor);
9268: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
9269: }
9270: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9271: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9272: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9273: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9274: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9275: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9276: pstamp(ficresprob);
9277: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
9278: fprintf(ficresprob,"# Age");
9279: pstamp(ficresprobcov);
9280: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
9281: fprintf(ficresprobcov,"# Age");
9282: pstamp(ficresprobcor);
9283: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
9284: fprintf(ficresprobcor,"# Age");
1.126 brouard 9285:
9286:
1.222 brouard 9287: for(i=1; i<=nlstate;i++)
9288: for(j=1; j<=(nlstate+ndeath);j++){
9289: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
9290: fprintf(ficresprobcov," p%1d-%1d ",i,j);
9291: fprintf(ficresprobcor," p%1d-%1d ",i,j);
9292: }
9293: /* fprintf(ficresprob,"\n");
9294: fprintf(ficresprobcov,"\n");
9295: fprintf(ficresprobcor,"\n");
9296: */
9297: xp=vector(1,npar);
9298: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9299: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9300: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
9301: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
9302: first=1;
9303: fprintf(ficgp,"\n# Routine varprob");
9304: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
9305: fprintf(fichtm,"\n");
9306:
1.288 brouard 9307: fprintf(fichtm,"\n<li><h4> <a href=\"%s\">Matrix of variance-covariance of one-step probabilities (drawings)</a></h4> this page is important in order to visualize confidence intervals and especially correlation between disability and recovery, or more generally, way in and way back. File %s</li>\n",optionfilehtmcov,optionfilehtmcov);
1.222 brouard 9308: fprintf(fichtmcov,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Matrix of variance-covariance of pairs of step probabilities</h4>\n",optionfilehtmcov, optionfilehtmcov);
9309: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 9310: and drawn. It helps understanding how is the covariance between two incidences.\
9311: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 9312: fprintf(fichtmcov,"\n<br> Contour plot corresponding to x'cov<sup>-1</sup>x = 4 (where x is the column vector (pij,pkl)) are drawn. \
1.126 brouard 9313: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
9314: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
9315: standard deviations wide on each axis. <br>\
9316: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
9317: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
9318: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
9319:
1.222 brouard 9320: cov[1]=1;
9321: /* tj=cptcoveff; */
1.225 brouard 9322: tj = (int) pow(2,cptcoveff);
1.222 brouard 9323: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
9324: j1=0;
1.332 brouard 9325:
9326: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
9327: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
1.342 brouard 9328: /* printf("Varprob TKresult[nres]=%d j1=%d, nres=%d, cptcovn=%d, cptcoveff=%d tj=%d cptcovs=%d\n", TKresult[nres], j1, nres, cptcovn, cptcoveff, tj, cptcovs); */
1.332 brouard 9329: if(tj != 1 && TKresult[nres]!= j1)
9330: continue;
9331:
9332: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
9333: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
9334: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
1.222 brouard 9335: if (cptcovn>0) {
1.334 brouard 9336: fprintf(ficresprob, "\n#********** Variable ");
9337: fprintf(ficresprobcov, "\n#********** Variable ");
9338: fprintf(ficgp, "\n#********** Variable ");
9339: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
9340: fprintf(ficresprobcor, "\n#********** Variable ");
9341:
9342: /* Including quantitative variables of the resultline to be done */
9343: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
1.343 brouard 9344: /* printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model); */
1.338 brouard 9345: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
9346: /* fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s resultline[%d]=%s \n",nres, z1, modelresult[nres][z1], model, nres, resultline[nres]); */
1.334 brouard 9347: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
9348: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
9349: fprintf(ficresprob,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
9350: fprintf(ficresprobcov,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
9351: fprintf(ficgp,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
9352: fprintf(fichtmcov,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
9353: fprintf(ficresprobcor,"V%d=%d ",Tvresult[nres][z1],Tresult[nres][z1]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
9354: fprintf(ficresprob,"fixed ");
9355: fprintf(ficresprobcov,"fixed ");
9356: fprintf(ficgp,"fixed ");
9357: fprintf(fichtmcov,"fixed ");
9358: fprintf(ficresprobcor,"fixed ");
9359: }else{
9360: fprintf(ficresprob,"varyi ");
9361: fprintf(ficresprobcov,"varyi ");
9362: fprintf(ficgp,"varyi ");
9363: fprintf(fichtmcov,"varyi ");
9364: fprintf(ficresprobcor,"varyi ");
9365: }
9366: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
9367: /* For each selected (single) quantitative value */
1.337 brouard 9368: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
1.334 brouard 9369: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
9370: fprintf(ficresprob,"fixed ");
9371: fprintf(ficresprobcov,"fixed ");
9372: fprintf(ficgp,"fixed ");
9373: fprintf(fichtmcov,"fixed ");
9374: fprintf(ficresprobcor,"fixed ");
9375: }else{
9376: fprintf(ficresprob,"varyi ");
9377: fprintf(ficresprobcov,"varyi ");
9378: fprintf(ficgp,"varyi ");
9379: fprintf(fichtmcov,"varyi ");
9380: fprintf(ficresprobcor,"varyi ");
9381: }
9382: }else{
9383: printf("Error in varprob() Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=V%d cptcovs=%d, cptcoveff=%d \n", nres, z1, Dummy[modelresult[nres][z1]],nres,z1,modelresult[nres][z1],cptcovs, cptcoveff); /* end if dummy or quanti */
9384: fprintf(ficlog,"Error in varprob() Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=V%d cptcovs=%d, cptcoveff=%d \n", nres, z1, Dummy[modelresult[nres][z1]],nres,z1,modelresult[nres][z1],cptcovs, cptcoveff); /* end if dummy or quanti */
9385: exit(1);
9386: }
9387: } /* End loop on variable of this resultline */
9388: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
1.222 brouard 9389: fprintf(ficresprob, "**********\n#\n");
9390: fprintf(ficresprobcov, "**********\n#\n");
9391: fprintf(ficgp, "**********\n#\n");
9392: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
9393: fprintf(ficresprobcor, "**********\n#");
9394: if(invalidvarcomb[j1]){
9395: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
9396: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
9397: continue;
9398: }
9399: }
9400: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
9401: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9402: gp=vector(1,(nlstate)*(nlstate+ndeath));
9403: gm=vector(1,(nlstate)*(nlstate+ndeath));
1.334 brouard 9404: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
1.222 brouard 9405: cov[2]=age;
9406: if(nagesqr==1)
9407: cov[3]= age*age;
1.334 brouard 9408: /* New code end of combination but for each resultline */
9409: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 9410: if(Typevar[k1]==1 || Typevar[k1] ==3){ /* A product with age */
1.334 brouard 9411: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.326 brouard 9412: }else{
1.334 brouard 9413: cov[2+nagesqr+k1]=precov[nres][k1];
1.326 brouard 9414: }
1.334 brouard 9415: }/* End of loop on model equation */
9416: /* Old code */
9417: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
9418: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
9419: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
9420: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
9421: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
9422: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
9423: /* * 1 1 1 1 1 */
9424: /* * 2 2 1 1 1 */
9425: /* * 3 1 2 1 1 */
9426: /* *\/ */
9427: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
9428: /* } */
9429: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
9430: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
9431: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
9432: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
9433: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
9434: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
9435: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9436: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
9437: /* printf("Internal IMaCh error, don't know which value for quantitative covariate with age, Tage[k]%d, k=%d, Tvar[Tage[k]]=V%d, age=%d\n",Tage[k],k ,Tvar[Tage[k]], (int)cov[2]); */
9438: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
9439: /* /\* exit(1); *\/ */
9440: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
9441: /* } */
9442: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9443: /* } */
9444: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
9445: /* if(Dummy[Tvard[k][1]]==0){ */
9446: /* if(Dummy[Tvard[k][2]]==0){ */
9447: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])]; */
9448: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9449: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
9450: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
9451: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
9452: /* } */
9453: /* }else{ */
9454: /* if(Dummy[Tvard[k][2]]==0){ */
9455: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
9456: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
9457: /* }else{ */
9458: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
9459: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
9460: /* } */
9461: /* } */
9462: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9463: /* } */
1.326 brouard 9464: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
1.222 brouard 9465: for(theta=1; theta <=npar; theta++){
9466: for(i=1; i<=npar; i++)
9467: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 9468:
1.222 brouard 9469: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 9470:
1.222 brouard 9471: k=0;
9472: for(i=1; i<= (nlstate); i++){
9473: for(j=1; j<=(nlstate+ndeath);j++){
9474: k=k+1;
9475: gp[k]=pmmij[i][j];
9476: }
9477: }
1.220 brouard 9478:
1.222 brouard 9479: for(i=1; i<=npar; i++)
9480: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 9481:
1.222 brouard 9482: pmij(pmmij,cov,ncovmodel,xp,nlstate);
9483: k=0;
9484: for(i=1; i<=(nlstate); i++){
9485: for(j=1; j<=(nlstate+ndeath);j++){
9486: k=k+1;
9487: gm[k]=pmmij[i][j];
9488: }
9489: }
1.220 brouard 9490:
1.222 brouard 9491: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
9492: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
9493: }
1.126 brouard 9494:
1.222 brouard 9495: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
9496: for(theta=1; theta <=npar; theta++)
9497: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 9498:
1.222 brouard 9499: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
9500: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 9501:
1.222 brouard 9502: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 9503:
1.222 brouard 9504: k=0;
9505: for(i=1; i<=(nlstate); i++){
9506: for(j=1; j<=(nlstate+ndeath);j++){
9507: k=k+1;
9508: mu[k][(int) age]=pmmij[i][j];
9509: }
9510: }
9511: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
9512: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
9513: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 9514:
1.222 brouard 9515: /*printf("\n%d ",(int)age);
9516: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9517: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9518: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9519: }*/
1.220 brouard 9520:
1.222 brouard 9521: fprintf(ficresprob,"\n%d ",(int)age);
9522: fprintf(ficresprobcov,"\n%d ",(int)age);
9523: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 9524:
1.222 brouard 9525: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
9526: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
9527: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9528: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
9529: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
9530: }
9531: i=0;
9532: for (k=1; k<=(nlstate);k++){
9533: for (l=1; l<=(nlstate+ndeath);l++){
9534: i++;
9535: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
9536: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
9537: for (j=1; j<=i;j++){
9538: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
9539: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
9540: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
9541: }
9542: }
9543: }/* end of loop for state */
9544: } /* end of loop for age */
9545: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
9546: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
9547: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9548: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9549:
9550: /* Confidence intervalle of pij */
9551: /*
9552: fprintf(ficgp,"\nunset parametric;unset label");
9553: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
9554: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
9555: fprintf(fichtm,"\n<br>Probability with confidence intervals expressed in year<sup>-1</sup> :<a href=\"pijgr%s.png\">pijgr%s.png</A>, ",optionfilefiname,optionfilefiname);
9556: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
9557: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
9558: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
9559: */
9560:
9561: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
9562: first1=1;first2=2;
9563: for (k2=1; k2<=(nlstate);k2++){
9564: for (l2=1; l2<=(nlstate+ndeath);l2++){
9565: if(l2==k2) continue;
9566: j=(k2-1)*(nlstate+ndeath)+l2;
9567: for (k1=1; k1<=(nlstate);k1++){
9568: for (l1=1; l1<=(nlstate+ndeath);l1++){
9569: if(l1==k1) continue;
9570: i=(k1-1)*(nlstate+ndeath)+l1;
9571: if(i<=j) continue;
9572: for (age=bage; age<=fage; age ++){
9573: if ((int)age %5==0){
9574: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
9575: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
9576: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
9577: mu1=mu[i][(int) age]/stepm*YEARM ;
9578: mu2=mu[j][(int) age]/stepm*YEARM;
9579: c12=cv12/sqrt(v1*v2);
9580: /* Computing eigen value of matrix of covariance */
9581: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9582: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9583: if ((lc2 <0) || (lc1 <0) ){
9584: if(first2==1){
9585: first1=0;
9586: printf("Strange: j1=%d One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e.\n It means that the matrix was not well estimated (varpij), for i=%2d, j=%2d, age=%4d .\n See files %s and %s. Probably WRONG RESULTS. See log file for details...\n", j1, lc1, lc2, v1, v2, cv12, i, j, (int)age,fileresprobcov, fileresprobcor);
9587: }
9588: fprintf(ficlog,"Strange: j1=%d One eigen value of 2x2 matrix of covariance is negative, lc1=%11.3e, lc2=%11.3e, v1=%11.3e, v2=%11.3e, cv12=%11.3e.\n It means that the matrix was not well estimated (varpij), for i=%2d, j=%2d, age=%4d .\n See files %s and %s. Probably WRONG RESULTS.\n", j1, lc1, lc2, v1, v2, cv12, i, j, (int)age,fileresprobcov, fileresprobcor);fflush(ficlog);
9589: /* lc1=fabs(lc1); */ /* If we want to have them positive */
9590: /* lc2=fabs(lc2); */
9591: }
1.220 brouard 9592:
1.222 brouard 9593: /* Eigen vectors */
1.280 brouard 9594: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
9595: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9596: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9597: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
9598: }else
9599: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
1.222 brouard 9600: /*v21=sqrt(1.-v11*v11); *//* error */
9601: v21=(lc1-v1)/cv12*v11;
9602: v12=-v21;
9603: v22=v11;
9604: tnalp=v21/v11;
9605: if(first1==1){
9606: first1=0;
9607: printf("%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tang %.3f\nOthers in log...\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
9608: }
9609: fprintf(ficlog,"%d %d%d-%d%d mu %.4e %.4e Var %.4e %.4e cor %.3f cov %.4e Eig %.3e %.3e 1stv %.3f %.3f tan %.3f\n",(int) age,k1,l1,k2,l2,mu1,mu2,v1,v2,c12,cv12,lc1,lc2,v11,v21,tnalp);
9610: /*printf(fignu*/
9611: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
9612: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
9613: if(first==1){
9614: first=0;
9615: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
9616: fprintf(ficgp,"\nset parametric;unset label");
9617: fprintf(ficgp,"\nset log y;set log x; set xlabel \"p%1d%1d (year-1)\";set ylabel \"p%1d%1d (year-1)\"",k1,l1,k2,l2);
9618: fprintf(ficgp,"\nset ter svg size 640, 480");
1.266 brouard 9619: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 9620: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 9621: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 9622: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
9623: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9624: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9625: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
9626: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9627: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9628: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9629: fprintf(ficgp,"\nplot [-pi:pi] %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not", \
1.280 brouard 9630: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
9631: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
1.222 brouard 9632: }else{
9633: first=0;
9634: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
9635: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9636: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9637: fprintf(ficgp,"\nreplot %11.3e+ %.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)), %11.3e +%.3f*(%11.3e*%11.3e*cos(t)+%11.3e*%11.3e*sin(t)) not", \
1.266 brouard 9638: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
9639: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
1.222 brouard 9640: }/* if first */
9641: } /* age mod 5 */
9642: } /* end loop age */
9643: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9644: first=1;
9645: } /*l12 */
9646: } /* k12 */
9647: } /*l1 */
9648: }/* k1 */
1.332 brouard 9649: } /* loop on combination of covariates j1 */
1.326 brouard 9650: } /* loop on nres */
1.222 brouard 9651: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
9652: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
9653: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9654: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
9655: free_vector(xp,1,npar);
9656: fclose(ficresprob);
9657: fclose(ficresprobcov);
9658: fclose(ficresprobcor);
9659: fflush(ficgp);
9660: fflush(fichtmcov);
9661: }
1.126 brouard 9662:
9663:
9664: /******************* Printing html file ***********/
1.201 brouard 9665: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 9666: int lastpass, int stepm, int weightopt, char model[],\
9667: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.296 brouard 9668: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
9669: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
9670: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
1.359 brouard 9671: int jj1, k1, cpt, nres;
1.319 brouard 9672: /* In fact some results are already printed in fichtm which is open */
1.126 brouard 9673: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
9674: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
9675: </ul>");
1.319 brouard 9676: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
9677: /* </ul>", model); */
1.214 brouard 9678: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
9679: fprintf(fichtm,"<li>- Observed frequency between two states (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file)<br/>\n",
9680: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
1.332 brouard 9681: fprintf(fichtm,"<li> - Observed prevalence (cross-sectional prevalence) in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file) ",
1.213 brouard 9682: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
9683: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 9684: fprintf(fichtm,"\
9685: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 9686: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 9687: fprintf(fichtm,"\
1.217 brouard 9688: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
9689: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
9690: fprintf(fichtm,"\
1.288 brouard 9691: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 9692: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 9693: fprintf(fichtm,"\
1.288 brouard 9694: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.217 brouard 9695: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
9696: fprintf(fichtm,"\
1.211 brouard 9697: - (a) Life expectancies by health status at initial age, e<sub>i.</sub> (b) health expectancies by health status at initial age, e<sub>ij</sub> . If one or more covariates are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \
1.126 brouard 9698: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 9699: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 9700: if(prevfcast==1){
9701: fprintf(fichtm,"\
9702: - Prevalence projections by age and states: \
1.201 brouard 9703: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 9704: }
1.126 brouard 9705:
9706:
1.225 brouard 9707: m=pow(2,cptcoveff);
1.222 brouard 9708: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 9709:
1.317 brouard 9710: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
1.264 brouard 9711:
9712: jj1=0;
9713:
9714: fprintf(fichtm," \n<ul>");
1.337 brouard 9715: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9716: /* k1=nres; */
1.338 brouard 9717: k1=TKresult[nres];
9718: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
1.337 brouard 9719: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9720: /* if(m != 1 && TKresult[nres]!= k1) */
9721: /* continue; */
1.264 brouard 9722: jj1++;
9723: if (cptcovn > 0) {
9724: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
1.337 brouard 9725: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
9726: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9727: }
1.337 brouard 9728: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9729: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9730: /* } */
9731: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9732: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9733: /* } */
1.264 brouard 9734: fprintf(fichtm,"\">");
9735:
9736: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9737: fprintf(fichtm,"************ Results for covariates");
1.337 brouard 9738: for (cpt=1; cpt<=cptcovs;cpt++){
9739: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9740: }
1.337 brouard 9741: /* fprintf(fichtm,"************ Results for covariates"); */
9742: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9743: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9744: /* } */
9745: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9746: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9747: /* } */
1.264 brouard 9748: if(invalidvarcomb[k1]){
9749: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9750: continue;
9751: }
9752: fprintf(fichtm,"</a></li>");
9753: } /* cptcovn >0 */
9754: }
1.317 brouard 9755: fprintf(fichtm," \n</ul>");
1.264 brouard 9756:
1.222 brouard 9757: jj1=0;
1.237 brouard 9758:
1.337 brouard 9759: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9760: /* k1=nres; */
1.338 brouard 9761: k1=TKresult[nres];
9762: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 9763: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9764: /* if(m != 1 && TKresult[nres]!= k1) */
9765: /* continue; */
1.220 brouard 9766:
1.222 brouard 9767: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9768: jj1++;
9769: if (cptcovn > 0) {
1.264 brouard 9770: fprintf(fichtm,"\n<p><a name=\"rescov");
1.337 brouard 9771: for (cpt=1; cpt<=cptcovs;cpt++){
9772: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9773: }
1.337 brouard 9774: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9775: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9776: /* } */
1.264 brouard 9777: fprintf(fichtm,"\"</a>");
9778:
1.222 brouard 9779: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.337 brouard 9780: for (cpt=1; cpt<=cptcovs;cpt++){
9781: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9782: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.237 brouard 9783: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
9784: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 9785: }
1.230 brouard 9786: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.338 brouard 9787: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.222 brouard 9788: if(invalidvarcomb[k1]){
9789: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
9790: printf("\nCombination (%d) ignored because no cases \n",k1);
9791: continue;
9792: }
9793: }
9794: /* aij, bij */
1.259 brouard 9795: fprintf(fichtm,"<br>- Logit model (yours is: logit(pij)=log(pij/pii)= aij+ bij age+%s) as a function of age: <a href=\"%s_%d-1-%d.svg\">%s_%d-1-%d.svg</a><br> \
1.241 brouard 9796: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 9797: /* Pij */
1.241 brouard 9798: fprintf(fichtm,"<br>\n- P<sub>ij</sub> or conditional probabilities to be observed in state j being in state i, %d (stepm) months before: <a href=\"%s_%d-2-%d.svg\">%s_%d-2-%d.svg</a><br> \
9799: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 9800: /* Quasi-incidences */
9801: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.220 brouard 9802: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 9803: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
1.241 brouard 9804: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3-%d.svg\">%s_%d-3-%d.svg</a><br> \
9805: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres);
1.222 brouard 9806: /* Survival functions (period) in state j */
9807: for(cpt=1; cpt<=nlstate;cpt++){
1.359 brouard 9808: fprintf(fichtm,"<br>\n- Survival functions in state %d. And probability to be observed in state %d being in state (1 to %d) at different ages. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.329 brouard 9809: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9810: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.222 brouard 9811: }
9812: /* State specific survival functions (period) */
9813: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 9814: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
1.359 brouard 9815: And probability to be observed in various states (up to %d) being in state %d at different ages. Mean times spent in state (or Life Expectancy or Health Expectancy etc.) are the areas under each curve. \
1.329 brouard 9816: <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> ", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
9817: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9818: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.222 brouard 9819: }
1.288 brouard 9820: /* Period (forward stable) prevalence in each health state */
1.222 brouard 9821: for(cpt=1; cpt<=nlstate;cpt++){
1.359 brouard 9822: fprintf(fichtm,"<br>\n- Convergence to period (stable) prevalence in state %d. Or probability for a person being in state (1 to %d) at different ages, to be alive in state %d some years after. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, nlstate, cpt, subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.338 brouard 9823: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
1.329 brouard 9824: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.222 brouard 9825: }
1.296 brouard 9826: if(prevbcast==1){
1.288 brouard 9827: /* Backward prevalence in each health state */
1.222 brouard 9828: for(cpt=1; cpt<=nlstate;cpt++){
1.338 brouard 9829: fprintf(fichtm,"<br>\n- Convergence to mixed (stable) back prevalence in state %d. Or probability for a person to be in state %d at a younger age, knowing that she/he was in state (1 to %d) at different older ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br>", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
9830: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
9831: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.222 brouard 9832: }
1.217 brouard 9833: }
1.222 brouard 9834: if(prevfcast==1){
1.288 brouard 9835: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
1.222 brouard 9836: for(cpt=1; cpt<=nlstate;cpt++){
1.314 brouard 9837: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), from year %.1f up to year %.1f tending to period (stable) forward prevalence in state %d. Or probability to be in state %d being in an observed weighted state (from 1 to %d). <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateprojd, dateprojf, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
9838: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
9839: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
9840: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.222 brouard 9841: }
9842: }
1.296 brouard 9843: if(prevbcast==1){
1.268 brouard 9844: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
9845: for(cpt=1; cpt<=nlstate;cpt++){
1.273 brouard 9846: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
1.359 brouard 9847: from year %.1f up to year %.1f (probably close to stable [mixed] back prevalence in state %d). Randomness in cross-sectional prevalence is not taken into \
9848: account but can visually be appreciated. Or probability to have been in an state %d, knowing that the person was in either state (1 or %d) \
1.314 brouard 9849: with weights corresponding to observed prevalence at different ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateback1, dateback2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
9850: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
9851: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
1.268 brouard 9852: }
9853: }
1.220 brouard 9854:
1.222 brouard 9855: for(cpt=1; cpt<=nlstate;cpt++) {
1.314 brouard 9856: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies in each alive state (1 to %d) (or area under each survival functions): <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
9857: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
9858: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
1.222 brouard 9859: }
9860: /* } /\* end i1 *\/ */
1.337 brouard 9861: }/* End k1=nres */
1.222 brouard 9862: fprintf(fichtm,"</ul>");
1.126 brouard 9863:
1.222 brouard 9864: fprintf(fichtm,"\
1.126 brouard 9865: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 9866: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 9867: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
1.197 brouard 9868: But because parameters are usually highly correlated (a higher incidence of disability \
9869: and a higher incidence of recovery can give very close observed transition) it might \
9870: be very useful to look not only at linear confidence intervals estimated from the \
9871: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
9872: (parameters) of the logistic regression, it might be more meaningful to visualize the \
9873: covariance matrix of the one-step probabilities. \
9874: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 9875:
1.222 brouard 9876: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
9877: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
9878: fprintf(fichtm,"\
1.126 brouard 9879: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9880: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 9881:
1.222 brouard 9882: fprintf(fichtm,"\
1.126 brouard 9883: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9884: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
9885: fprintf(fichtm,"\
1.126 brouard 9886: - Variances and covariances of health expectancies by age and <b>initial health status</b> (cov(e<sup>ij</sup>,e<sup>kl</sup>)(estepm=%2d months): \
9887: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 9888: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 9889: fprintf(fichtm,"\
1.126 brouard 9890: - (a) Health expectancies by health status at initial age (e<sup>ij</sup>) and standard errors (in parentheses) (b) life expectancies and standard errors (e<sup>i.</sup>=e<sup>i1</sup>+e<sup>i2</sup>+...)(estepm=%2d months): \
9891: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 9892: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 9893: fprintf(fichtm,"\
1.288 brouard 9894: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), e<sup>ij</sup> are weighted by the forward (period) prevalences in each state i (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a><br>\n",
1.222 brouard 9895: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
9896: fprintf(fichtm,"\
1.128 brouard 9897: - Total life expectancy and total health expectancies to be spent in each health state e<sup>.j</sup> with their standard errors (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9898: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
9899: fprintf(fichtm,"\
1.288 brouard 9900: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 9901: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 9902:
9903: /* if(popforecast==1) fprintf(fichtm,"\n */
9904: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
9905: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
9906: /* <br>",fileres,fileres,fileres,fileres); */
9907: /* else */
1.338 brouard 9908: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=1+age+%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
1.222 brouard 9909: fflush(fichtm);
1.126 brouard 9910:
1.225 brouard 9911: m=pow(2,cptcoveff);
1.222 brouard 9912: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 9913:
1.317 brouard 9914: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
9915:
9916: jj1=0;
9917:
9918: fprintf(fichtm," \n<ul>");
1.337 brouard 9919: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9920: /* k1=nres; */
1.338 brouard 9921: k1=TKresult[nres];
1.337 brouard 9922: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9923: /* if(m != 1 && TKresult[nres]!= k1) */
9924: /* continue; */
1.317 brouard 9925: jj1++;
9926: if (cptcovn > 0) {
9927: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
1.337 brouard 9928: for (cpt=1; cpt<=cptcovs;cpt++){
9929: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9930: }
9931: fprintf(fichtm,"\">");
9932:
9933: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9934: fprintf(fichtm,"************ Results for covariates");
1.337 brouard 9935: for (cpt=1; cpt<=cptcovs;cpt++){
9936: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9937: }
9938: if(invalidvarcomb[k1]){
9939: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9940: continue;
9941: }
9942: fprintf(fichtm,"</a></li>");
9943: } /* cptcovn >0 */
1.337 brouard 9944: } /* End nres */
1.317 brouard 9945: fprintf(fichtm," \n</ul>");
9946:
1.222 brouard 9947: jj1=0;
1.237 brouard 9948:
1.241 brouard 9949: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 9950: /* k1=nres; */
1.338 brouard 9951: k1=TKresult[nres];
9952: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 9953: /* for(k1=1; k1<=m;k1++){ */
9954: /* if(m != 1 && TKresult[nres]!= k1) */
9955: /* continue; */
1.222 brouard 9956: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9957: jj1++;
1.126 brouard 9958: if (cptcovn > 0) {
1.317 brouard 9959: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
1.337 brouard 9960: for (cpt=1; cpt<=cptcovs;cpt++){
9961: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9962: }
9963: fprintf(fichtm,"\"</a>");
9964:
1.126 brouard 9965: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.337 brouard 9966: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
9967: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9968: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.237 brouard 9969: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
1.317 brouard 9970: }
1.237 brouard 9971:
1.338 brouard 9972: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.220 brouard 9973:
1.222 brouard 9974: if(invalidvarcomb[k1]){
9975: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
9976: continue;
9977: }
1.337 brouard 9978: } /* If cptcovn >0 */
1.126 brouard 9979: for(cpt=1; cpt<=nlstate;cpt++) {
1.258 brouard 9980: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
1.314 brouard 9981: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d-%d.svg\"> %s_%d-%d-%d.svg</a>",mobilav,cpt,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
9982: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
9983: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
1.126 brouard 9984: }
9985: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.360 brouard 9986: health expectancies in each live state (1 to %d) with confidence intervals \
9987: on left y-scale as well as proportions of time spent in each live state \
9988: (with confidence intervals) on right y-scale 0 to 100%%.\
9989: If popbased=1 the smooth (due to the model) \
1.128 brouard 9990: true period expectancies (those weighted with period prevalences are also\
9991: drawn in addition to the population based expectancies computed using\
1.314 brouard 9992: observed and cahotic prevalences: <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a>",nlstate, subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres);
9993: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
9994: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 9995: /* } /\* end i1 *\/ */
1.241 brouard 9996: }/* End nres */
1.222 brouard 9997: fprintf(fichtm,"</ul>");
9998: fflush(fichtm);
1.126 brouard 9999: }
10000:
10001: /******************* Gnuplot file **************/
1.296 brouard 10002: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double bage, double fage , int prevfcast, int prevbcast, char pathc[], double p[], int offyear, int offbyear){
1.126 brouard 10003:
1.354 brouard 10004: char dirfileres[256],optfileres[256];
10005: char gplotcondition[256], gplotlabel[256];
1.343 brouard 10006: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,k4=0,kf=0,kvar=0,kk=0,ipos=0,iposold=0,ij=0, ijp=0, l=0;
1.365 brouard 10007: /* int lv=0, vlv=0, kl=0; */
10008: int lv=0, kl=0;
10009: double vlv=0;
1.130 brouard 10010: int ng=0;
1.201 brouard 10011: int vpopbased;
1.223 brouard 10012: int ioffset; /* variable offset for columns */
1.270 brouard 10013: int iyearc=1; /* variable column for year of projection */
10014: int iagec=1; /* variable column for age of projection */
1.235 brouard 10015: int nres=0; /* Index of resultline */
1.266 brouard 10016: int istart=1; /* For starting graphs in projections */
1.219 brouard 10017:
1.126 brouard 10018: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
10019: /* printf("Problem with file %s",optionfilegnuplot); */
10020: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
10021: /* } */
10022:
10023: /*#ifdef windows */
10024: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 10025: /*#endif */
1.225 brouard 10026: m=pow(2,cptcoveff);
1.126 brouard 10027:
1.274 brouard 10028: /* diagram of the model */
10029: fprintf(ficgp,"\n#Diagram of the model \n");
10030: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
10031: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
10032: fprintf(ficgp,"\n#Peripheral arrows\nset for [i=1:%d] for [j=1:%d] arrow i*10+j from cos(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d))-(i!=j?(i-j)/abs(i-j)*delta:0), yoff +sin(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) rto -0.95*(cos(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d))+(i!=j?(i-j)/abs(i-j)*delta:0) - cos(pi*((1-(%d/2)*2./%d)/2+(j-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta2:0)), -0.95*(sin(pi*((1-(%d/2)*2./%d)/2+(i-1)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) - sin(pi*((1-(%d/2)*2./%d)/2+(j-1)*2./%d))+( i!=j?(i-j)/abs(i-j)*delta2:0)) ls (i < j? 1:2)\n",nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
10033:
1.343 brouard 10034: fprintf(ficgp,"\n#Centripete arrows (turning in other direction (1-i) instead of (i-1)) \nset for [i=1:%d] for [j=1:%d] arrow (%d+1)*10+i from cos(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d))-(i!=j?(i-j)/abs(i-j)*delta:0), yoff +sin(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) rto -0.80*(cos(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d))+(i!=j?(i-j)/abs(i-j)*delta:0) ), -0.80*(sin(pi*((1-(%d/2)*2./%d)/2+(1-i)*2./%d)) + (i!=j?(i-j)/abs(i-j)*delta:0) + yoff ) ls 4\n",nlstate, nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
1.274 brouard 10035: fprintf(ficgp,"\n#show arrow\nunset label\n");
10036: fprintf(ficgp,"\n#States labels, starting from 2 (2-i) instead of (1-i), was (i-1)\nset for [i=1:%d] label i sprintf(\"State %%d\",i) center at cos(pi*((1-(%d/2)*2./%d)/2+(2-i)*2./%d)), yoff+sin(pi*((1-(%d/2)*2./%d)/2+(2-i)*2./%d)) font \"helvetica, 16\" tc rgbcolor \"blue\"\n",nlstate,nlstate,nlstate,nlstate,nlstate,nlstate,nlstate);
10037: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
10038: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
10039: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
10040: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
10041:
1.202 brouard 10042: /* Contribution to likelihood */
10043: /* Plot the probability implied in the likelihood */
1.223 brouard 10044: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
10045: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
10046: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
10047: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 10048: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 10049: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
10050: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 10051: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
10052: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
10053: fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$13):6 t \"All sample, transitions colored by destination\" with dots lc variable; set out;\n",subdirf(fileresilk));
10054: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
10055: fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$13):5 t \"All sample, transitions colored by origin\" with dots lc variable; set out;\n\n",subdirf(fileresilk));
10056: for (i=1; i<= nlstate ; i ++) {
10057: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
10058: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
10059: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($12/4.):6 t \"p%d%d\" with points pointtype 7 ps variable lc variable \\\n",i,1,i,1);
10060: for (j=2; j<= nlstate+ndeath ; j ++) {
10061: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($12/4.):6 t \"p%d%d\" with points pointtype 7 ps variable lc variable ",i,j,i,j);
10062: }
10063: fprintf(ficgp,";\nset out; unset ylabel;\n");
10064: }
10065: /* unset log; plot "rrtest1_sorted_4/ILK_rrtest1_sorted_4.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with points lc variable */
10066: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10067: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10068: fprintf(ficgp,"\nset out;unset log\n");
10069: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 10070:
1.343 brouard 10071: /* Plot the probability implied in the likelihood by covariate value */
10072: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
10073: /* if(debugILK==1){ */
10074: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
1.347 brouard 10075: kvar=Tvar[TvarFind[kf]]; /* variable name */
10076: /* k=18+Tvar[TvarFind[kf]];/\*offset because there are 18 columns in the ILK_ file but could be placed else where *\/ */
1.350 brouard 10077: /* k=18+kf;/\*offset because there are 18 columns in the ILK_ file *\/ */
1.356 brouard 10078: /* k=19+kf;/\*offset because there are 19 columns in the ILK_ file *\/ */
1.355 brouard 10079: k=16+nlstate+kf;/*offset because there are 19 columns in the ILK_ file, first cov Vn on col 21 with 4 living states */
1.343 brouard 10080: for (i=1; i<= nlstate ; i ++) {
10081: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10082: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
1.348 brouard 10083: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10084: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable \\\n",i,1,k,k,i,1,kvar);
10085: for (j=2; j<= nlstate+ndeath ; j ++) {
10086: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable ",i,j,k,k,i,j,kvar);
10087: }
10088: }else{
10089: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable \\\n",i,1,k,i,1,kvar);
10090: for (j=2; j<= nlstate+ndeath ; j ++) {
10091: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable ",i,j,k,i,j,kvar);
10092: }
1.343 brouard 10093: }
10094: fprintf(ficgp,";\nset out; unset ylabel;\n");
10095: }
10096: } /* End of each covariate dummy */
10097: for(ncovv=1, iposold=0, kk=0; ncovv <= ncovvt ; ncovv++){
10098: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
10099: * kmodel = 1 2 3 4 5 6 7 8 9
10100: * varying 1 2 3 4 5
10101: * ncovv 1 2 3 4 5 6 7 8
10102: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
10103: * TvarVVind[ncovv]=kmodel 2 3 7 7 8 8 9 9
10104: * TvarFind[kmodel] 1 0 0 0 0 0 0 0 0
10105: * kdata ncovcol=[V1 V2] nqv=0 ntv=[V3 V4] nqtv=V5
10106: * Dummy[kmodel] 0 0 1 2 2 3 1 1 1
10107: */
10108: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
10109: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
10110: /* printf("DebugILK ficgp ncovv=%d, kvar=TvarVV[ncovv]=%d, ipos=TvarVVind[ncovv]=%d, Dummy[ipos]=%d, Typevar[ipos]=%d\n", ncovv,kvar,ipos,Dummy[ipos],Typevar[ipos]); */
10111: if(ipos!=iposold){ /* Not a product or first of a product */
10112: /* printf(" %d",ipos); */
10113: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
10114: /* printf(" DebugILK ficgp suite ipos=%d != iposold=%d\n", ipos, iposold); */
10115: kk++; /* Position of the ncovv column in ILK_ */
10116: k=18+ncovf+kk; /*offset because there are 18 columns in the ILK_ file plus ncovf fixed covariate */
10117: if(Dummy[ipos]==0 && Typevar[ipos]==0){ /* Only if dummy time varying: Dummy(0, 1=quant singor prod without age,2 dummy*age, 3quant*age) Typevar (0 single, 1=*age,2=Vn*vm) */
10118: for (i=1; i<= nlstate ; i ++) {
10119: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10120: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
10121:
1.348 brouard 10122: /* printf("Before DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
1.343 brouard 10123: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10124: /* printf("DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
10125: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable \\\n",i,1,k,k,i,1,kvar);
10126: for (j=2; j<= nlstate+ndeath ; j ++) {
10127: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? 7 : 9):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt variable ps 0.4 lc variable ",i,j,k,k,i,j,kvar);
10128: }
10129: }else{
10130: /* printf("DebugILK gnuplotversion=%g <5.2\n",gnuplotversion); */
10131: fprintf(ficgp," u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable \\\n",i,1,k,i,1,kvar);
10132: for (j=2; j<= nlstate+ndeath ; j ++) {
10133: fprintf(ficgp,",\\\n \"\" u 2:($5 == %d && $6==%d ? $10 : 1/0):($%d==0 ? $6 : $6+4) t \"p%d%d V%d\" with points pt 7 ps 0.4 lc variable ",i,j,k,i,j,kvar);
10134: }
10135: }
10136: fprintf(ficgp,";\nset out; unset ylabel;\n");
10137: }
10138: }/* End if dummy varying */
10139: }else{ /*Product */
10140: /* printf("*"); */
10141: /* fprintf(ficresilk,"*"); */
10142: }
10143: iposold=ipos;
10144: } /* For each time varying covariate */
10145: /* } /\* debugILK==1 *\/ */
10146: /* unset log; plot "rrtest1_sorted_4/ILK_rrtest1_sorted_4.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with points lc variable */
10147: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
10148: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
10149: fprintf(ficgp,"\nset out;unset log\n");
10150: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
10151:
10152:
10153:
1.126 brouard 10154: strcpy(dirfileres,optionfilefiname);
10155: strcpy(optfileres,"vpl");
1.223 brouard 10156: /* 1eme*/
1.238 brouard 10157: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
1.337 brouard 10158: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
1.236 brouard 10159: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10160: k1=TKresult[nres];
1.338 brouard 10161: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.238 brouard 10162: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
1.337 brouard 10163: /* if(m != 1 && TKresult[nres]!= k1) */
10164: /* continue; */
1.238 brouard 10165: /* We are interested in selected combination by the resultline */
1.246 brouard 10166: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
1.288 brouard 10167: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.264 brouard 10168: strcpy(gplotlabel,"(");
1.337 brouard 10169: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10170: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10171: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10172:
10173: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
10174: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
10175: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10176: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10177: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10178: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10179: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
10180: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
10181: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
10182: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10183: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10184: /* } */
10185: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10186: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
10187: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10188: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.264 brouard 10189: }
10190: strcpy(gplotlabel+strlen(gplotlabel),")");
1.246 brouard 10191: /* printf("\n#\n"); */
1.238 brouard 10192: fprintf(ficgp,"\n#\n");
10193: if(invalidvarcomb[k1]){
1.260 brouard 10194: /*k1=k1-1;*/ /* To be checked */
1.238 brouard 10195: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10196: continue;
10197: }
1.235 brouard 10198:
1.241 brouard 10199: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
10200: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
1.276 brouard 10201: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
1.338 brouard 10202: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
1.260 brouard 10203: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter svg size 640, 480\nplot [%.f:%.f] \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
10204: /* fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \nset ter svg size 640, 480\nplot [%.f:%.f] \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1,nres); */
10205: /* k1-1 error should be nres-1*/
1.238 brouard 10206: for (i=1; i<= nlstate ; i ++) {
10207: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10208: else fprintf(ficgp," %%*lf (%%*lf)");
10209: }
1.288 brouard 10210: fprintf(ficgp,"\" t\"Forward prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
1.238 brouard 10211: for (i=1; i<= nlstate ; i ++) {
10212: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10213: else fprintf(ficgp," %%*lf (%%*lf)");
10214: }
1.260 brouard 10215: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VPL_"),nres-1,nres-1,nres);
1.238 brouard 10216: for (i=1; i<= nlstate ; i ++) {
10217: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10218: else fprintf(ficgp," %%*lf (%%*lf)");
10219: }
1.265 brouard 10220: /* fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" every :::%d::%d u 1:($%d) t\"Observed prevalence\" w l lt 2",subdirf2(fileresu,"P_"),k1-1,k1-1,2+4*(cpt-1)); */
10221:
10222: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
10223: if(cptcoveff ==0){
1.271 brouard 10224: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
1.265 brouard 10225: }else{
10226: kl=0;
10227: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
1.332 brouard 10228: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10229: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
1.265 brouard 10230: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10231: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10232: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10233: vlv= nbcode[Tvaraff[k]][lv];
10234: kl++;
10235: /* kl=6+(cpt-1)*(nlstate+1)+1+(i-1); /\* 6+(1-1)*(2+1)+1+(1-1)=7, 6+(2-1)(2+1)+1+(1-1)=10 *\/ */
10236: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10237: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10238: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
10239: if(k==cptcoveff){
10240: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Observed prevalence in state %d' w l lt 2",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
10241: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
10242: }else{
10243: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
10244: kl++;
10245: }
10246: } /* end covariate */
10247: } /* end if no covariate */
10248:
1.296 brouard 10249: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
1.238 brouard 10250: /* fprintf(ficgp,",\"%s\" every :::%d::%d u 1:($%d) t\"Backward stable prevalence\" w l lt 3",subdirf2(fileresu,"PLB_"),k1-1,k1-1,1+cpt); */
1.242 brouard 10251: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 10252: if(cptcoveff ==0){
1.245 brouard 10253: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
1.238 brouard 10254: }else{
10255: kl=0;
10256: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
1.332 brouard 10257: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10258: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
1.238 brouard 10259: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10260: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10261: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.332 brouard 10262: /* vlv= nbcode[Tvaraff[k]][lv]; */
10263: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
1.223 brouard 10264: kl++;
1.238 brouard 10265: /* kl=6+(cpt-1)*(nlstate+1)+1+(i-1); /\* 6+(1-1)*(2+1)+1+(1-1)=7, 6+(2-1)(2+1)+1+(1-1)=10 *\/ */
10266: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10267: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10268: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
10269: if(k==cptcoveff){
1.245 brouard 10270: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Backward prevalence in state %d' w l lt 3",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
1.242 brouard 10271: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 10272: }else{
1.332 brouard 10273: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
1.238 brouard 10274: kl++;
10275: }
10276: } /* end covariate */
10277: } /* end if no covariate */
1.296 brouard 10278: if(prevbcast == 1){
1.268 brouard 10279: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
10280: /* k1-1 error should be nres-1*/
10281: for (i=1; i<= nlstate ; i ++) {
10282: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10283: else fprintf(ficgp," %%*lf (%%*lf)");
10284: }
1.271 brouard 10285: fprintf(ficgp,"\" t\"Backward (stable) prevalence\" w l lt 6 dt 3,\"%s\" every :::%d::%d u 1:($2==%d ? $3+1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
1.268 brouard 10286: for (i=1; i<= nlstate ; i ++) {
10287: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10288: else fprintf(ficgp," %%*lf (%%*lf)");
10289: }
1.276 brouard 10290: fprintf(ficgp,"\" t\"95%% CI\" w l lt 4,\"%s\" every :::%d::%d u 1:($2==%d ? $3-1.96*$4 : 1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
1.268 brouard 10291: for (i=1; i<= nlstate ; i ++) {
10292: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10293: else fprintf(ficgp," %%*lf (%%*lf)");
10294: }
1.274 brouard 10295: fprintf(ficgp,"\" t\"\" w l lt 4");
1.268 brouard 10296: } /* end if backprojcast */
1.296 brouard 10297: } /* end if prevbcast */
1.276 brouard 10298: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
10299: fprintf(ficgp,"\nset out ;unset title;\n");
1.238 brouard 10300: } /* nres */
1.337 brouard 10301: /* } /\* k1 *\/ */
1.201 brouard 10302: } /* cpt */
1.235 brouard 10303:
10304:
1.126 brouard 10305: /*2 eme*/
1.337 brouard 10306: /* for (k1=1; k1<= m ; k1 ++){ */
1.238 brouard 10307: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10308: k1=TKresult[nres];
1.338 brouard 10309: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10310: /* if(m != 1 && TKresult[nres]!= k1) */
10311: /* continue; */
1.238 brouard 10312: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.264 brouard 10313: strcpy(gplotlabel,"(");
1.337 brouard 10314: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10315: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10316: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10317: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10318: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10319: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10320: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10321: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10322: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10323: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10324: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10325: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10326: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10327: /* } */
10328: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
10329: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10330: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10331: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10332: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.238 brouard 10333: }
1.264 brouard 10334: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 10335: fprintf(ficgp,"\n#\n");
1.223 brouard 10336: if(invalidvarcomb[k1]){
10337: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10338: continue;
10339: }
1.219 brouard 10340:
1.241 brouard 10341: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 10342: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.264 brouard 10343: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
10344: if(vpopbased==0){
1.360 brouard 10345: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nunset ytics; unset y2tics; set ytics nomirror; set y2tics 0,10,100;set y2range [0:100];\nplot [%.f:%.f] ",ageminpar,fage);
1.264 brouard 10346: }else
1.238 brouard 10347: fprintf(ficgp,"\nreplot ");
1.360 brouard 10348: for (i=1; i<= nlstate+1 ; i ++) { /* For state i-1=0 is LE, while i-1=1 to nlstate are origin state */
1.238 brouard 10349: k=2*i;
1.360 brouard 10350: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ?$4 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1, vpopbased); /* for fixed variables age, popbased, mobilav */
10351: for (j=1; j<= nlstate+1 ; j ++) { /* e.. e.1 e.2 again j-1 is the state of end, wlim_i eij*/
10352: if (j==i) fprintf(ficgp," %%lf (%%lf)"); /* We want to read e.. i=1,j=1, e.1 i=2,j=2, e.2 i=3,j=3 */
10353: else fprintf(ficgp," %%*lf (%%*lf)"); /* skipping that field with a star */
1.238 brouard 10354: }
10355: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
1.360 brouard 10356: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1); /* state=i-1=1 to nlstate */
1.261 brouard 10357: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4-$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
1.238 brouard 10358: for (j=1; j<= nlstate+1 ; j ++) {
10359: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10360: else fprintf(ficgp," %%*lf (%%*lf)");
10361: }
10362: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.261 brouard 10363: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4+$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
1.238 brouard 10364: for (j=1; j<= nlstate+1 ; j ++) {
10365: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10366: else fprintf(ficgp," %%*lf (%%*lf)");
10367: }
1.360 brouard 10368: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0,\\\n"); /* ,\\\n added for th percentage graphs */
1.238 brouard 10369: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
10370: } /* state */
1.360 brouard 10371: /* again for the percentag spent in state i-1=1 to i-1=nlstate */
10372: for (i=2; i<= nlstate+1 ; i ++) { /* For state i-1=0 is LE, while i-1=1 to nlstate are origin state */
10373: k=2*i;
10374: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4)<=1 && ($4)>=0 ?($4)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1, vpopbased); /* for fixed variables age, popbased, mobilav */
10375: for (j=1; j<= nlstate ; j ++)
10376: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10377: for (j=1; j<= nlstate+1 ; j ++) { /* e.. e.1 e.2 again j-1 is the state of end, wlim_i eij*/
10378: if (j==i) fprintf(ficgp," %%lf (%%lf)"); /* We want to read e.. i=1,j=1, e.1 i=2,j=2, e.2 i=3,j=3 */
10379: else fprintf(ficgp," %%*lf (%%*lf)"); /* skipping that field with a star */
10380: }
10381: if (i== 1) fprintf(ficgp,"\" t\"%%TLE\" w l lt %d axis x1y2, \\\n",i); /* Not used */
10382: else fprintf(ficgp,"\" t\"%%LE in state (%d)\" w l lw 2 lt %d axis x1y2, \\\n",i-1,i+1); /* state=i-1=1 to nlstate */
10383: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4-$5*2)<=1 && ($4-$5*2)>=0? ($4-$5*2)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
10384: for (j=1; j<= nlstate ; j ++)
10385: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10386: for (j=1; j<= nlstate+1 ; j ++) {
10387: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10388: else fprintf(ficgp," %%*lf (%%*lf)");
10389: }
10390: fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2,");
10391: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && ($4+$5*2)<=1 && ($4+$5*2)>=0 ? ($4+$5*2)*100. : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),nres-1,nres-1,vpopbased);
10392: for (j=1; j<= nlstate ; j ++)
10393: fprintf(ficgp," %%*lf (%%*lf)"); /* Skipping TLE and LE to read %LE only */
10394: for (j=1; j<= nlstate+1 ; j ++) {
10395: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10396: else fprintf(ficgp," %%*lf (%%*lf)");
10397: }
10398: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2");
10399: else fprintf(ficgp,"\" t\"\" w l lt 0 axis x1y2,\\\n");
10400: } /* state for percent */
1.238 brouard 10401: } /* vpopbased */
1.264 brouard 10402: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */
1.238 brouard 10403: } /* end nres */
1.337 brouard 10404: /* } /\* k1 end 2 eme*\/ */
1.238 brouard 10405:
10406:
10407: /*3eme*/
1.337 brouard 10408: /* for (k1=1; k1<= m ; k1 ++){ */
1.238 brouard 10409: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10410: k1=TKresult[nres];
1.338 brouard 10411: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10412: /* if(m != 1 && TKresult[nres]!= k1) */
10413: /* continue; */
1.238 brouard 10414:
1.332 brouard 10415: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
1.261 brouard 10416: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.264 brouard 10417: strcpy(gplotlabel,"(");
1.337 brouard 10418: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10419: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10420: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10421: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10422: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10423: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10424: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10425: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10426: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10427: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10428: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10429: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10430: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10431: /* } */
10432: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10433: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10434: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10435: }
1.264 brouard 10436: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10437: fprintf(ficgp,"\n#\n");
10438: if(invalidvarcomb[k1]){
10439: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10440: continue;
10441: }
10442:
10443: /* k=2+nlstate*(2*cpt-2); */
10444: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 10445: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.264 brouard 10446: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
1.238 brouard 10447: fprintf(ficgp,"set ter svg size 640, 480\n\
1.261 brouard 10448: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileresu,"E_"),nres-1,nres-1,k,cpt);
1.238 brouard 10449: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10450: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10451: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
10452: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10453: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10454: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 10455:
1.238 brouard 10456: */
10457: for (i=1; i< nlstate ; i ++) {
1.261 brouard 10458: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+i,cpt,i+1);
1.238 brouard 10459: /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
1.219 brouard 10460:
1.238 brouard 10461: }
1.261 brouard 10462: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt);
1.238 brouard 10463: }
1.264 brouard 10464: fprintf(ficgp,"\nunset label;\n");
1.238 brouard 10465: } /* end nres */
1.337 brouard 10466: /* } /\* end kl 3eme *\/ */
1.126 brouard 10467:
1.223 brouard 10468: /* 4eme */
1.201 brouard 10469: /* Survival functions (period) from state i in state j by initial state i */
1.337 brouard 10470: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
1.238 brouard 10471: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10472: k1=TKresult[nres];
1.338 brouard 10473: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10474: /* if(m != 1 && TKresult[nres]!= k1) */
10475: /* continue; */
1.238 brouard 10476: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
1.264 brouard 10477: strcpy(gplotlabel,"(");
1.337 brouard 10478: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
10479: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10480: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10481: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10482: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10483: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10484: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10485: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10486: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10487: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10488: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10489: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10490: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10491: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10492: /* } */
10493: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10494: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10495: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.238 brouard 10496: }
1.264 brouard 10497: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10498: fprintf(ficgp,"\n#\n");
10499: if(invalidvarcomb[k1]){
10500: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10501: continue;
1.223 brouard 10502: }
1.238 brouard 10503:
1.241 brouard 10504: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.264 brouard 10505: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.238 brouard 10506: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10507: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10508: k=3;
10509: for (i=1; i<= nlstate ; i ++){
10510: if(i==1){
10511: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10512: }else{
10513: fprintf(ficgp,", '' ");
10514: }
10515: l=(nlstate+ndeath)*(i-1)+1;
10516: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10517: for (j=2; j<= nlstate+ndeath ; j ++)
10518: fprintf(ficgp,"+$%d",k+l+j-1);
10519: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
10520: } /* nlstate */
1.264 brouard 10521: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 10522: } /* end cpt state*/
10523: } /* end nres */
1.337 brouard 10524: /* } /\* end covariate k1 *\/ */
1.238 brouard 10525:
1.220 brouard 10526: /* 5eme */
1.201 brouard 10527: /* Survival functions (period) from state i in state j by final state j */
1.337 brouard 10528: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
1.238 brouard 10529: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10530: k1=TKresult[nres];
1.338 brouard 10531: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10532: /* if(m != 1 && TKresult[nres]!= k1) */
10533: /* continue; */
1.238 brouard 10534: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.264 brouard 10535: strcpy(gplotlabel,"(");
1.238 brouard 10536: fprintf(ficgp,"\n#\n#\n# Survival functions in state j and all livestates from state i by final state j: 'lij' files, cov=%d state=%d",k1, cpt);
1.337 brouard 10537: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10538: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10539: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10540: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10541: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10542: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10543: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10544: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10545: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10546: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10547: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10548: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10549: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10550: /* } */
10551: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10552: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10553: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.238 brouard 10554: }
1.264 brouard 10555: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10556: fprintf(ficgp,"\n#\n");
10557: if(invalidvarcomb[k1]){
10558: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10559: continue;
10560: }
1.227 brouard 10561:
1.241 brouard 10562: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.264 brouard 10563: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.238 brouard 10564: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10565: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10566: k=3;
10567: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10568: if(j==1)
10569: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10570: else
10571: fprintf(ficgp,", '' ");
10572: l=(nlstate+ndeath)*(cpt-1) +j;
10573: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
10574: /* for (i=2; i<= nlstate+ndeath ; i ++) */
10575: /* fprintf(ficgp,"+$%d",k+l+i-1); */
10576: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
10577: } /* nlstate */
10578: fprintf(ficgp,", '' ");
10579: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
10580: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10581: l=(nlstate+ndeath)*(cpt-1) +j;
10582: if(j < nlstate)
10583: fprintf(ficgp,"$%d +",k+l);
10584: else
10585: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
10586: }
1.264 brouard 10587: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 10588: } /* end cpt state*/
1.337 brouard 10589: /* } /\* end covariate *\/ */
1.238 brouard 10590: } /* end nres */
1.227 brouard 10591:
1.220 brouard 10592: /* 6eme */
1.202 brouard 10593: /* CV preval stable (period) for each covariate */
1.337 brouard 10594: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10595: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10596: k1=TKresult[nres];
1.338 brouard 10597: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10598: /* if(m != 1 && TKresult[nres]!= k1) */
10599: /* continue; */
1.255 brouard 10600: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
1.264 brouard 10601: strcpy(gplotlabel,"(");
1.288 brouard 10602: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10603: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10604: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10605: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10606: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10607: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10608: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10609: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10610: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10611: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10612: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10613: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10614: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10615: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10616: /* } */
10617: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10618: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10619: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10620: }
1.264 brouard 10621: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 10622: fprintf(ficgp,"\n#\n");
1.223 brouard 10623: if(invalidvarcomb[k1]){
1.227 brouard 10624: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10625: continue;
1.223 brouard 10626: }
1.227 brouard 10627:
1.241 brouard 10628: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.264 brouard 10629: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.126 brouard 10630: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 10631: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 10632: k=3; /* Offset */
1.255 brouard 10633: for (i=1; i<= nlstate ; i ++){ /* State of origin */
1.227 brouard 10634: if(i==1)
10635: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10636: else
10637: fprintf(ficgp,", '' ");
1.255 brouard 10638: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 10639: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10640: for (j=2; j<= nlstate ; j ++)
10641: fprintf(ficgp,"+$%d",k+l+j-1);
10642: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 10643: } /* nlstate */
1.264 brouard 10644: fprintf(ficgp,"\nset out; unset label;\n");
1.153 brouard 10645: } /* end cpt state*/
10646: } /* end covariate */
1.227 brouard 10647:
10648:
1.220 brouard 10649: /* 7eme */
1.296 brouard 10650: if(prevbcast == 1){
1.288 brouard 10651: /* CV backward prevalence for each covariate */
1.337 brouard 10652: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10653: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10654: k1=TKresult[nres];
1.338 brouard 10655: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10656: /* if(m != 1 && TKresult[nres]!= k1) */
10657: /* continue; */
1.268 brouard 10658: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
1.264 brouard 10659: strcpy(gplotlabel,"(");
1.288 brouard 10660: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10661: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10662: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10663: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10664: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10665: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10666: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10667: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10668: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10669: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10670: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10671: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10672: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10673: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10674: /* } */
10675: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10676: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10677: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10678: }
1.264 brouard 10679: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 10680: fprintf(ficgp,"\n#\n");
10681: if(invalidvarcomb[k1]){
10682: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10683: continue;
10684: }
10685:
1.241 brouard 10686: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.268 brouard 10687: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.227 brouard 10688: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 10689: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 10690: k=3; /* Offset */
1.268 brouard 10691: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
1.227 brouard 10692: if(i==1)
10693: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
10694: else
10695: fprintf(ficgp,", '' ");
10696: /* l=(nlstate+ndeath)*(i-1)+1; */
1.255 brouard 10697: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.324 brouard 10698: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
10699: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
1.255 brouard 10700: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
1.227 brouard 10701: /* for (j=2; j<= nlstate ; j ++) */
10702: /* fprintf(ficgp,"+$%d",k+l+j-1); */
10703: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
1.268 brouard 10704: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
1.227 brouard 10705: } /* nlstate */
1.264 brouard 10706: fprintf(ficgp,"\nset out; unset label;\n");
1.218 brouard 10707: } /* end cpt state*/
10708: } /* end covariate */
1.296 brouard 10709: } /* End if prevbcast */
1.218 brouard 10710:
1.223 brouard 10711: /* 8eme */
1.218 brouard 10712: if(prevfcast==1){
1.288 brouard 10713: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
1.218 brouard 10714:
1.337 brouard 10715: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10716: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10717: k1=TKresult[nres];
1.338 brouard 10718: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10719: /* if(m != 1 && TKresult[nres]!= k1) */
10720: /* continue; */
1.211 brouard 10721: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.264 brouard 10722: strcpy(gplotlabel,"(");
1.288 brouard 10723: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10724: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10725: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10726: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10727: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10728: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10729: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10730: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10731: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10732: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10733: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10734: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10735: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10736: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10737: /* } */
10738: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10739: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10740: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10741: }
1.264 brouard 10742: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 10743: fprintf(ficgp,"\n#\n");
10744: if(invalidvarcomb[k1]){
10745: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10746: continue;
10747: }
10748:
10749: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 10750: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.264 brouard 10751: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
1.227 brouard 10752: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 10753: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.266 brouard 10754:
10755: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10756: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10757: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10758: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
1.227 brouard 10759: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10760: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10761: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10762: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.266 brouard 10763: if(i==istart){
1.227 brouard 10764: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
10765: }else{
10766: fprintf(ficgp,",\\\n '' ");
10767: }
10768: if(cptcoveff ==0){ /* No covariate */
10769: ioffset=2; /* Age is in 2 */
10770: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10771: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10772: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10773: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10774: fprintf(ficgp," u %d:(", ioffset);
1.266 brouard 10775: if(i==nlstate+1){
1.270 brouard 10776: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
1.266 brouard 10777: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10778: fprintf(ficgp,",\\\n '' ");
10779: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 10780: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
1.266 brouard 10781: offyear, \
1.268 brouard 10782: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
1.266 brouard 10783: }else
1.227 brouard 10784: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
10785: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10786: }else{ /* more than 2 covariates */
1.270 brouard 10787: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10788: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10789: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10790: iyearc=ioffset-1;
10791: iagec=ioffset;
1.227 brouard 10792: fprintf(ficgp," u %d:(",ioffset);
10793: kl=0;
10794: strcpy(gplotcondition,"(");
1.351 brouard 10795: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
1.332 brouard 10796: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
1.351 brouard 10797: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10798: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10799: lv=Tvresult[nres][k];
10800: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
1.227 brouard 10801: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10802: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10803: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.332 brouard 10804: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
1.351 brouard 10805: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
1.227 brouard 10806: kl++;
1.364 brouard 10807: /* Problem with quantitative variables TinvDoQresult[nres] */
1.351 brouard 10808: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
1.364 brouard 10809: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,lv, kl+1, vlv );/* Solved but quantitative must be shifted */
1.227 brouard 10810: kl++;
1.351 brouard 10811: if(k <cptcovs && cptcovs>1)
1.227 brouard 10812: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10813: }
10814: strcpy(gplotcondition+strlen(gplotcondition),")");
10815: /* kl=6+(cpt-1)*(nlstate+1)+1+(i-1); /\* 6+(1-1)*(2+1)+1+(1-1)=7, 6+(2-1)(2+1)+1+(1-1)=10 *\/ */
10816: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10817: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10818: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
10819: if(i==nlstate+1){
1.270 brouard 10820: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
10821: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
1.266 brouard 10822: fprintf(ficgp,",\\\n '' ");
1.364 brouard 10823: fprintf(ficgp," u %d:(",iagec); /* Below iyearc should be increades if quantitative variable in the reult line */
10824: /* $7==6 && $8==2.47 ) && (($9-$10) == 1953 ) ? $12/(1.-$24) : 1/0):7 with labels center not */
10825: /* but was && $7==6 && $8==2 ) && (($7-$8) == 1953 ) ? $12/(1.-$24) : 1/0):7 with labels center not */
1.270 brouard 10826: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
10827: iyearc, iagec, offyear, \
10828: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
1.266 brouard 10829: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
1.227 brouard 10830: }else{
10831: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
10832: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10833: }
10834: } /* end if covariate */
10835: } /* nlstate */
1.264 brouard 10836: fprintf(ficgp,"\nset out; unset label;\n");
1.223 brouard 10837: } /* end cpt state*/
10838: } /* end covariate */
10839: } /* End if prevfcast */
1.227 brouard 10840:
1.296 brouard 10841: if(prevbcast==1){
1.268 brouard 10842: /* Back projection from cross-sectional to stable (mixed) for each covariate */
10843:
1.337 brouard 10844: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.268 brouard 10845: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10846: k1=TKresult[nres];
1.338 brouard 10847: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10848: /* if(m != 1 && TKresult[nres]!= k1) */
10849: /* continue; */
1.268 brouard 10850: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
10851: strcpy(gplotlabel,"(");
10852: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
1.337 brouard 10853: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10854: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10855: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10856: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10857: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10858: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10859: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10860: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10861: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10862: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10863: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10864: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10865: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10866: /* } */
10867: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10868: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10869: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.268 brouard 10870: }
10871: strcpy(gplotlabel+strlen(gplotlabel),")");
10872: fprintf(ficgp,"\n#\n");
10873: if(invalidvarcomb[k1]){
10874: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10875: continue;
10876: }
10877:
10878: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
10879: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
10880: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10881: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
10882: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10883:
10884: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10885: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10886: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10887: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
10888: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10889: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10890: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10891: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10892: if(i==istart){
10893: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
10894: }else{
10895: fprintf(ficgp,",\\\n '' ");
10896: }
1.351 brouard 10897: /* if(cptcoveff ==0){ /\* No covariate *\/ */
10898: if(cptcovs ==0){ /* No covariate */
1.268 brouard 10899: ioffset=2; /* Age is in 2 */
10900: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10901: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10902: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10903: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10904: fprintf(ficgp," u %d:(", ioffset);
10905: if(i==nlstate+1){
1.270 brouard 10906: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
1.268 brouard 10907: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10908: fprintf(ficgp,",\\\n '' ");
10909: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 10910: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
1.268 brouard 10911: offbyear, \
10912: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
10913: }else
10914: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
10915: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
10916: }else{ /* more than 2 covariates */
1.270 brouard 10917: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10918: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10919: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10920: iyearc=ioffset-1;
10921: iagec=ioffset;
1.268 brouard 10922: fprintf(ficgp," u %d:(",ioffset);
10923: kl=0;
10924: strcpy(gplotcondition,"(");
1.337 brouard 10925: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
1.338 brouard 10926: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
1.337 brouard 10927: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
10928: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10929: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10930: lv=Tvresult[nres][k];
10931: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
10932: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10933: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10934: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10935: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
10936: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10937: kl++;
10938: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
10939: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
10940: kl++;
1.338 brouard 10941: if(k <cptcovs && cptcovs>1)
1.337 brouard 10942: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10943: }
1.268 brouard 10944: }
10945: strcpy(gplotcondition+strlen(gplotcondition),")");
10946: /* kl=6+(cpt-1)*(nlstate+1)+1+(i-1); /\* 6+(1-1)*(2+1)+1+(1-1)=7, 6+(2-1)(2+1)+1+(1-1)=10 *\/ */
10947: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10948: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10949: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0)? $9/(1.-$15) : 1/0):($5==2000? 3:2) t 'p.1' with line lc variable*/
10950: if(i==nlstate+1){
1.270 brouard 10951: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
10952: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
1.268 brouard 10953: fprintf(ficgp,",\\\n '' ");
1.270 brouard 10954: fprintf(ficgp," u %d:(",iagec);
1.268 brouard 10955: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
1.270 brouard 10956: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
10957: iyearc,iagec,offbyear, \
10958: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
1.268 brouard 10959: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
10960: }else{
10961: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
10962: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
10963: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
10964: }
10965: } /* end if covariate */
10966: } /* nlstate */
10967: fprintf(ficgp,"\nset out; unset label;\n");
10968: } /* end cpt state*/
10969: } /* end covariate */
1.296 brouard 10970: } /* End if prevbcast */
1.268 brouard 10971:
1.227 brouard 10972:
1.238 brouard 10973: /* 9eme writing MLE parameters */
10974: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 10975: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 10976: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 10977: for(k=1; k <=(nlstate+ndeath); k++){
10978: if (k != i) {
1.227 brouard 10979: fprintf(ficgp,"# current state %d\n",k);
10980: for(j=1; j <=ncovmodel; j++){
10981: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
10982: jk++;
10983: }
10984: fprintf(ficgp,"\n");
1.126 brouard 10985: }
10986: }
1.223 brouard 10987: }
1.187 brouard 10988: fprintf(ficgp,"##############\n#\n");
1.227 brouard 10989:
1.145 brouard 10990: /*goto avoid;*/
1.238 brouard 10991: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
10992: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 10993: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
10994: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
10995: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
10996: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
10997: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10998: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
10999: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
11000: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
11001: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
11002: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
11003: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
11004: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
11005: fprintf(ficgp,"#\n");
1.223 brouard 11006: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 11007: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.338 brouard 11008: fprintf(ficgp,"#model=1+age+%s \n",model);
1.238 brouard 11009: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.351 brouard 11010: /* fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/\* to be checked *\/ */
11011: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcovs,m);/* to be checked */
1.337 brouard 11012: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
1.237 brouard 11013: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 11014: /* k1=nres; */
1.338 brouard 11015: k1=TKresult[nres];
11016: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 11017: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
1.264 brouard 11018: strcpy(gplotlabel,"(");
1.276 brouard 11019: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
1.337 brouard 11020: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
11021: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
11022: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
11023: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11024: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
11025: }
11026: /* if(m != 1 && TKresult[nres]!= k1) */
11027: /* continue; */
11028: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
11029: /* strcpy(gplotlabel,"("); */
11030: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
11031: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
11032: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
11033: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
11034: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
11035: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
11036: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
11037: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
11038: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
11039: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
11040: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
11041: /* } */
11042: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11043: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11044: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
11045: /* } */
1.264 brouard 11046: strcpy(gplotlabel+strlen(gplotlabel),")");
1.237 brouard 11047: fprintf(ficgp,"\n#\n");
1.264 brouard 11048: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
1.276 brouard 11049: fprintf(ficgp,"\nset key outside ");
11050: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
11051: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
1.223 brouard 11052: fprintf(ficgp,"\nset ter svg size 640, 480 ");
11053: if (ng==1){
11054: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
11055: fprintf(ficgp,"\nunset log y");
11056: }else if (ng==2){
11057: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
11058: fprintf(ficgp,"\nset log y");
11059: }else if (ng==3){
11060: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
11061: fprintf(ficgp,"\nset log y");
11062: }else
11063: fprintf(ficgp,"\nunset title ");
11064: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
11065: i=1;
11066: for(k2=1; k2<=nlstate; k2++) {
11067: k3=i;
11068: for(k=1; k<=(nlstate+ndeath); k++) {
11069: if (k != k2){
11070: switch( ng) {
11071: case 1:
11072: if(nagesqr==0)
11073: fprintf(ficgp," p%d+p%d*x",i,i+1);
11074: else /* nagesqr =1 */
11075: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11076: break;
11077: case 2: /* ng=2 */
11078: if(nagesqr==0)
11079: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
11080: else /* nagesqr =1 */
11081: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
11082: break;
11083: case 3:
11084: if(nagesqr==0)
11085: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
11086: else /* nagesqr =1 */
11087: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
11088: break;
11089: }
11090: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 11091: ijp=1; /* product no age */
11092: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
11093: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 11094: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.329 brouard 11095: switch(Typevar[j]){
11096: case 1:
11097: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11098: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
11099: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
11100: if(DummyV[j]==0){/* Bug valgrind */
11101: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
11102: }else{ /* quantitative */
11103: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11104: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11105: }
11106: ij++;
1.268 brouard 11107: }
1.237 brouard 11108: }
1.329 brouard 11109: }
11110: break;
11111: case 2:
11112: if(cptcovprod >0){
11113: if(j==Tprod[ijp]) { /* */
11114: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11115: if(ijp <=cptcovprod) { /* Product */
11116: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11117: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11118: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
11119: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11120: }else{ /* Vn is dummy and Vm is quanti */
11121: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11122: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11123: }
11124: }else{ /* Vn*Vm Vn is quanti */
11125: if(DummyV[Tvard[ijp][2]]==0){
11126: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11127: }else{ /* Both quanti */
11128: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11129: }
1.268 brouard 11130: }
1.329 brouard 11131: ijp++;
1.237 brouard 11132: }
1.329 brouard 11133: } /* end Tprod */
11134: }
11135: break;
1.349 brouard 11136: case 3:
11137: if(cptcovdageprod >0){
11138: /* if(j==Tprod[ijp]) { */ /* not necessary */
11139: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
1.350 brouard 11140: if(ijp <=cptcovprod) { /* Product Vn*Vm and age*VN*Vm*/
11141: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11142: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
1.349 brouard 11143: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
11144: fprintf(ficgp,"+p%d*%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11145: }else{ /* Vn is dummy and Vm is quanti */
11146: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
1.350 brouard 11147: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11148: }
1.350 brouard 11149: }else{ /* age* Vn*Vm Vn is quanti HERE */
1.349 brouard 11150: if(DummyV[Tvard[ijp][2]]==0){
1.350 brouard 11151: fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
1.349 brouard 11152: }else{ /* Both quanti */
1.350 brouard 11153: fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11154: }
11155: }
11156: ijp++;
11157: }
11158: /* } */ /* end Tprod */
11159: }
11160: break;
1.329 brouard 11161: case 0:
11162: /* simple covariate */
1.264 brouard 11163: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
1.237 brouard 11164: if(Dummy[j]==0){
11165: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
11166: }else{ /* quantitative */
11167: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.264 brouard 11168: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
1.223 brouard 11169: }
1.329 brouard 11170: /* end simple */
11171: break;
11172: default:
11173: break;
11174: } /* end switch */
1.237 brouard 11175: } /* end j */
1.329 brouard 11176: }else{ /* k=k2 */
11177: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
11178: fprintf(ficgp," (1.");i=i-ncovmodel;
11179: }else
11180: i=i-ncovmodel;
1.223 brouard 11181: }
1.227 brouard 11182:
1.223 brouard 11183: if(ng != 1){
11184: fprintf(ficgp,")/(1");
1.227 brouard 11185:
1.264 brouard 11186: for(cpt=1; cpt <=nlstate; cpt++){
1.223 brouard 11187: if(nagesqr==0)
1.264 brouard 11188: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
1.223 brouard 11189: else /* nagesqr =1 */
1.264 brouard 11190: fprintf(ficgp,"+exp(p%d+p%d*x+p%d*x*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1,k3+(cpt-1)*ncovmodel+1+nagesqr);
1.217 brouard 11191:
1.223 brouard 11192: ij=1;
1.329 brouard 11193: ijp=1;
11194: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
11195: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
11196: switch(Typevar[j]){
11197: case 1:
11198: if(cptcovage >0){
11199: if(j==Tage[ij]) { /* Bug valgrind */
11200: if(ij <=cptcovage) { /* Bug valgrind */
11201: if(DummyV[j]==0){/* Bug valgrind */
11202: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
11203: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
11204: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
11205: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
11206: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11207: }else{ /* quantitative */
11208: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11209: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11210: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11211: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11212: }
11213: ij++;
11214: }
11215: }
11216: }
11217: break;
11218: case 2:
11219: if(cptcovprod >0){
11220: if(j==Tprod[ijp]) { /* */
11221: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11222: if(ijp <=cptcovprod) { /* Product */
11223: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11224: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11225: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
11226: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11227: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11228: }else{ /* Vn is dummy and Vm is quanti */
11229: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11230: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11231: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11232: }
11233: }else{ /* Vn*Vm Vn is quanti */
11234: if(DummyV[Tvard[ijp][2]]==0){
11235: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11236: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11237: }else{ /* Both quanti */
11238: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11239: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11240: }
11241: }
11242: ijp++;
11243: }
11244: } /* end Tprod */
11245: } /* end if */
11246: break;
1.349 brouard 11247: case 3:
11248: if(cptcovdageprod >0){
11249: /* if(j==Tprod[ijp]) { /\* *\/ */
11250: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11251: if(ijp <=cptcovprod) { /* Product */
1.350 brouard 11252: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11253: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
1.349 brouard 11254: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],nbcode[Tvard[ijp][2]][codtabm(k1,j)]); */
1.350 brouard 11255: fprintf(ficgp,"+p%d*%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11256: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11257: }else{ /* Vn is dummy and Vm is quanti */
11258: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
1.350 brouard 11259: fprintf(ficgp,"+p%d*%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11260: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11261: }
11262: }else{ /* Vn*Vm Vn is quanti */
1.350 brouard 11263: if(DummyV[Tvardk[ijp][2]]==0){
11264: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvardk[ijp][2]],Tqinvresult[nres][Tvardk[ijp][1]]);
1.349 brouard 11265: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11266: }else{ /* Both quanti */
1.350 brouard 11267: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvardk[ijp][1]],Tqinvresult[nres][Tvardk[ijp][2]]);
1.349 brouard 11268: /* fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11269: }
11270: }
11271: ijp++;
11272: }
11273: /* } /\* end Tprod *\/ */
11274: } /* end if */
11275: break;
1.329 brouard 11276: case 0:
11277: /* simple covariate */
11278: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
11279: if(Dummy[j]==0){
11280: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11281: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
11282: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11283: }else{ /* quantitative */
11284: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
11285: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
11286: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11287: }
11288: /* end simple */
11289: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
11290: break;
11291: default:
11292: break;
11293: } /* end switch */
1.223 brouard 11294: }
11295: fprintf(ficgp,")");
11296: }
11297: fprintf(ficgp,")");
11298: if(ng ==2)
1.276 brouard 11299: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"p%d%d\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
1.223 brouard 11300: else /* ng= 3 */
1.276 brouard 11301: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"i%d%d\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
1.329 brouard 11302: }else{ /* end ng <> 1 */
1.223 brouard 11303: if( k !=k2) /* logit p11 is hard to draw */
1.276 brouard 11304: fprintf(ficgp," w l lw 2 lt (%d*%d+%d)%%%d+1 dt %d t \"logit(p%d%d)\" ", nlstate+ndeath, k2, k, nlstate+ndeath, k2, k2,k);
1.223 brouard 11305: }
11306: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
11307: fprintf(ficgp,",");
11308: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
11309: fprintf(ficgp,",");
11310: i=i+ncovmodel;
11311: } /* end k */
11312: } /* end k2 */
1.276 brouard 11313: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
11314: fprintf(ficgp,"\n set out; unset title;set key default;\n");
1.337 brouard 11315: } /* end resultline */
1.223 brouard 11316: } /* end ng */
11317: /* avoid: */
11318: fflush(ficgp);
1.126 brouard 11319: } /* end gnuplot */
11320:
11321:
11322: /*************** Moving average **************/
1.219 brouard 11323: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 11324: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 11325:
1.222 brouard 11326: int i, cpt, cptcod;
11327: int modcovmax =1;
11328: int mobilavrange, mob;
11329: int iage=0;
1.288 brouard 11330: int firstA1=0, firstA2=0;
1.222 brouard 11331:
1.266 brouard 11332: double sum=0., sumr=0.;
1.222 brouard 11333: double age;
1.266 brouard 11334: double *sumnewp, *sumnewm, *sumnewmr;
11335: double *agemingood, *agemaxgood;
11336: double *agemingoodr, *agemaxgoodr;
1.222 brouard 11337:
11338:
1.278 brouard 11339: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
11340: /* a covariate has 2 modalities, should be equal to ncovcombmax */
1.222 brouard 11341:
11342: sumnewp = vector(1,ncovcombmax);
11343: sumnewm = vector(1,ncovcombmax);
1.266 brouard 11344: sumnewmr = vector(1,ncovcombmax);
1.222 brouard 11345: agemingood = vector(1,ncovcombmax);
1.266 brouard 11346: agemingoodr = vector(1,ncovcombmax);
1.222 brouard 11347: agemaxgood = vector(1,ncovcombmax);
1.266 brouard 11348: agemaxgoodr = vector(1,ncovcombmax);
1.222 brouard 11349:
11350: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
1.266 brouard 11351: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
1.222 brouard 11352: sumnewp[cptcod]=0.;
1.266 brouard 11353: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
11354: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
1.222 brouard 11355: }
11356: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
11357:
1.266 brouard 11358: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
11359: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
1.222 brouard 11360: else mobilavrange=mobilav;
11361: for (age=bage; age<=fage; age++)
11362: for (i=1; i<=nlstate;i++)
11363: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
11364: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11365: /* We keep the original values on the extreme ages bage, fage and for
11366: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
11367: we use a 5 terms etc. until the borders are no more concerned.
11368: */
11369: for (mob=3;mob <=mobilavrange;mob=mob+2){
11370: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
1.266 brouard 11371: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
11372: sumnewm[cptcod]=0.;
11373: for (i=1; i<=nlstate;i++){
1.222 brouard 11374: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
11375: for (cpt=1;cpt<=(mob-1)/2;cpt++){
11376: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
11377: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
11378: }
11379: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
1.266 brouard 11380: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11381: } /* end i */
11382: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
11383: } /* end cptcod */
1.222 brouard 11384: }/* end age */
11385: }/* end mob */
1.266 brouard 11386: }else{
11387: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
1.222 brouard 11388: return -1;
1.266 brouard 11389: }
11390:
11391: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
1.222 brouard 11392: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
11393: if(invalidvarcomb[cptcod]){
11394: printf("\nCombination (%d) ignored because no cases \n",cptcod);
11395: continue;
11396: }
1.219 brouard 11397:
1.266 brouard 11398: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
11399: sumnewm[cptcod]=0.;
11400: sumnewmr[cptcod]=0.;
11401: for (i=1; i<=nlstate;i++){
11402: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11403: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11404: }
11405: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11406: agemingoodr[cptcod]=age;
11407: }
11408: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11409: agemingood[cptcod]=age;
11410: }
11411: } /* age */
11412: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
1.222 brouard 11413: sumnewm[cptcod]=0.;
1.266 brouard 11414: sumnewmr[cptcod]=0.;
1.222 brouard 11415: for (i=1; i<=nlstate;i++){
11416: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 11417: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11418: }
11419: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11420: agemaxgoodr[cptcod]=age;
1.222 brouard 11421: }
11422: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
1.266 brouard 11423: agemaxgood[cptcod]=age;
11424: }
11425: } /* age */
11426: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
11427: /* but they will change */
1.288 brouard 11428: firstA1=0;firstA2=0;
1.266 brouard 11429: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
11430: sumnewm[cptcod]=0.;
11431: sumnewmr[cptcod]=0.;
11432: for (i=1; i<=nlstate;i++){
11433: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11434: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11435: }
11436: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11437: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11438: agemaxgoodr[cptcod]=age; /* age min */
11439: for (i=1; i<=nlstate;i++)
11440: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11441: }else{ /* bad we change the value with the values of good ages */
11442: for (i=1; i<=nlstate;i++){
11443: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
11444: } /* i */
11445: } /* end bad */
11446: }else{
11447: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11448: agemaxgood[cptcod]=age;
11449: }else{ /* bad we change the value with the values of good ages */
11450: for (i=1; i<=nlstate;i++){
11451: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
11452: } /* i */
11453: } /* end bad */
11454: }/* end else */
11455: sum=0.;sumr=0.;
11456: for (i=1; i<=nlstate;i++){
11457: sum+=mobaverage[(int)age][i][cptcod];
11458: sumr+=probs[(int)age][i][cptcod];
11459: }
11460: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.288 brouard 11461: if(!firstA1){
11462: firstA1=1;
11463: printf("Moving average A1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you increase bage=%d. Others in log file...\n",cptcod,sumr, (int)age, (int)bage);
11464: }
11465: fprintf(ficlog,"Moving average A1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, (int)bage);
1.266 brouard 11466: } /* end bad */
11467: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11468: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.288 brouard 11469: if(!firstA2){
11470: firstA2=1;
11471: printf("Moving average A2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase bage=%d. Others in log file...\n",cptcod,sumr, (int)age, (int)bage);
11472: }
11473: fprintf(ficlog,"Moving average A2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase bage=%d\n",cptcod,sumr, (int)age, (int)bage);
1.222 brouard 11474: } /* end bad */
11475: }/* age */
1.266 brouard 11476:
11477: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
1.222 brouard 11478: sumnewm[cptcod]=0.;
1.266 brouard 11479: sumnewmr[cptcod]=0.;
1.222 brouard 11480: for (i=1; i<=nlstate;i++){
11481: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 11482: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11483: }
11484: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11485: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
11486: agemingoodr[cptcod]=age;
11487: for (i=1; i<=nlstate;i++)
11488: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11489: }else{ /* bad we change the value with the values of good ages */
11490: for (i=1; i<=nlstate;i++){
11491: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
11492: } /* i */
11493: } /* end bad */
11494: }else{
11495: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11496: agemingood[cptcod]=age;
11497: }else{ /* bad */
11498: for (i=1; i<=nlstate;i++){
11499: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
11500: } /* i */
11501: } /* end bad */
11502: }/* end else */
11503: sum=0.;sumr=0.;
11504: for (i=1; i<=nlstate;i++){
11505: sum+=mobaverage[(int)age][i][cptcod];
11506: sumr+=mobaverage[(int)age][i][cptcod];
1.222 brouard 11507: }
1.266 brouard 11508: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.268 brouard 11509: printf("Moving average B1: For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one (%f) at any descending age! age=%d, could you decrease fage=%d?\n",cptcod, sum, (int) age, (int)fage);
1.266 brouard 11510: } /* end bad */
11511: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11512: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.268 brouard 11513: printf("Moving average B2: For this combination of covariate cptcod=%d, the raw prevalence doesn't sums to one (%f) even with smoothed values at young ages! age=%d, could you increase fage=%d\n",cptcod,sumr, (int)age, (int)fage);
1.222 brouard 11514: } /* end bad */
11515: }/* age */
1.266 brouard 11516:
1.222 brouard 11517:
11518: for (age=bage; age<=fage; age++){
1.235 brouard 11519: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 11520: sumnewp[cptcod]=0.;
11521: sumnewm[cptcod]=0.;
11522: for (i=1; i<=nlstate;i++){
11523: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
11524: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11525: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
11526: }
11527: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
11528: }
11529: /* printf("\n"); */
11530: /* } */
1.266 brouard 11531:
1.222 brouard 11532: /* brutal averaging */
1.266 brouard 11533: /* for (i=1; i<=nlstate;i++){ */
11534: /* for (age=1; age<=bage; age++){ */
11535: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
11536: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11537: /* } */
11538: /* for (age=fage; age<=AGESUP; age++){ */
11539: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
11540: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11541: /* } */
11542: /* } /\* end i status *\/ */
11543: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
11544: /* for (age=1; age<=AGESUP; age++){ */
11545: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
11546: /* mobaverage[(int)age][i][cptcod]=0.; */
11547: /* } */
11548: /* } */
1.222 brouard 11549: }/* end cptcod */
1.266 brouard 11550: free_vector(agemaxgoodr,1, ncovcombmax);
11551: free_vector(agemaxgood,1, ncovcombmax);
11552: free_vector(agemingood,1, ncovcombmax);
11553: free_vector(agemingoodr,1, ncovcombmax);
11554: free_vector(sumnewmr,1, ncovcombmax);
1.222 brouard 11555: free_vector(sumnewm,1, ncovcombmax);
11556: free_vector(sumnewp,1, ncovcombmax);
11557: return 0;
11558: }/* End movingaverage */
1.218 brouard 11559:
1.126 brouard 11560:
1.296 brouard 11561:
1.126 brouard 11562: /************** Forecasting ******************/
1.296 brouard 11563: /* void prevforecast(char fileres[], double dateintmean, double anprojd, double mprojd, double jprojd, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double ***prev, double bage, double fage, int firstpass, int lastpass, double anprojf, double p[], int cptcoveff)*/
11564: void prevforecast(char fileres[], double dateintmean, double dateprojd, double dateprojf, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double ***prev, double bage, double fage, int firstpass, int lastpass, double p[], int cptcoveff){
11565: /* dateintemean, mean date of interviews
11566: dateprojd, year, month, day of starting projection
11567: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
1.126 brouard 11568: agemin, agemax range of age
11569: dateprev1 dateprev2 range of dates during which prevalence is computed
11570: */
1.296 brouard 11571: /* double anprojd, mprojd, jprojd; */
11572: /* double anprojf, mprojf, jprojf; */
1.359 brouard 11573: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
1.126 brouard 11574: double agec; /* generic age */
1.359 brouard 11575: double agelim, ppij;
11576: /*double *popcount;*/
1.126 brouard 11577: double ***p3mat;
1.218 brouard 11578: /* double ***mobaverage; */
1.126 brouard 11579: char fileresf[FILENAMELENGTH];
11580:
11581: agelim=AGESUP;
1.211 brouard 11582: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11583: in each health status at the date of interview (if between dateprev1 and dateprev2).
11584: We still use firstpass and lastpass as another selection.
11585: */
1.214 brouard 11586: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11587: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 11588:
1.201 brouard 11589: strcpy(fileresf,"F_");
11590: strcat(fileresf,fileresu);
1.126 brouard 11591: if((ficresf=fopen(fileresf,"w"))==NULL) {
11592: printf("Problem with forecast resultfile: %s\n", fileresf);
11593: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
11594: }
1.235 brouard 11595: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
11596: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 11597:
1.225 brouard 11598: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 11599:
11600:
11601: stepsize=(int) (stepm+YEARM-1)/YEARM;
11602: if (stepm<=12) stepsize=1;
11603: if(estepm < stepm){
11604: printf ("Problem %d lower than %d\n",estepm, stepm);
11605: }
1.270 brouard 11606: else{
11607: hstepm=estepm;
11608: }
11609: if(estepm > stepm){ /* Yes every two year */
11610: stepsize=2;
11611: }
1.296 brouard 11612: hstepm=hstepm/stepm;
1.126 brouard 11613:
1.296 brouard 11614:
11615: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11616: /* fractional in yp1 *\/ */
11617: /* aintmean=yp; */
11618: /* yp2=modf((yp1*12),&yp); */
11619: /* mintmean=yp; */
11620: /* yp1=modf((yp2*30.5),&yp); */
11621: /* jintmean=yp; */
11622: /* if(jintmean==0) jintmean=1; */
11623: /* if(mintmean==0) mintmean=1; */
1.126 brouard 11624:
1.296 brouard 11625:
11626: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
11627: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
11628: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
1.351 brouard 11629: /* i1=pow(2,cptcoveff); */
11630: /* if (cptcovn < 1){i1=1;} */
1.126 brouard 11631:
1.296 brouard 11632: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.126 brouard 11633:
11634: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 11635:
1.126 brouard 11636: /* if (h==(int)(YEARM*yearp)){ */
1.351 brouard 11637: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11638: k=TKresult[nres];
11639: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11640: /* for(k=1; k<=i1;k++){ /\* We want to find the combination k corresponding to the values of the dummies given in this resut line (to be cleaned one day) *\/ */
11641: /* if(i1 != 1 && TKresult[nres]!= k) */
11642: /* continue; */
11643: /* if(invalidvarcomb[k]){ */
11644: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11645: /* continue; */
11646: /* } */
1.227 brouard 11647: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
1.351 brouard 11648: for(j=1;j<=cptcovs;j++){
11649: /* for(j=1;j<=cptcoveff;j++) { */
11650: /* /\* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); *\/ */
11651: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11652: /* } */
11653: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11654: /* fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11655: /* } */
11656: fprintf(ficresf," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.235 brouard 11657: }
1.351 brouard 11658:
1.227 brouard 11659: fprintf(ficresf," yearproj age");
11660: for(j=1; j<=nlstate+ndeath;j++){
11661: for(i=1; i<=nlstate;i++)
11662: fprintf(ficresf," p%d%d",i,j);
11663: fprintf(ficresf," wp.%d",j);
11664: }
1.296 brouard 11665: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
1.227 brouard 11666: fprintf(ficresf,"\n");
1.296 brouard 11667: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
1.270 brouard 11668: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
11669: for (agec=fage; agec>=(bage); agec--){
1.227 brouard 11670: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
11671: nhstepm = nhstepm/hstepm;
11672: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11673: oldm=oldms;savm=savms;
1.268 brouard 11674: /* We compute pii at age agec over nhstepm);*/
1.235 brouard 11675: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.268 brouard 11676: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
1.227 brouard 11677: for (h=0; h<=nhstepm; h++){
11678: if (h*hstepm/YEARM*stepm ==yearp) {
1.268 brouard 11679: break;
11680: }
11681: }
11682: fprintf(ficresf,"\n");
1.351 brouard 11683: /* for(j=1;j<=cptcoveff;j++) */
11684: for(j=1;j<=cptcovs;j++)
11685: fprintf(ficresf,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.332 brouard 11686: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
1.351 brouard 11687: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* TnsdVar[Tvaraff] correct *\/ */
1.296 brouard 11688: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
1.268 brouard 11689:
11690: for(j=1; j<=nlstate+ndeath;j++) {
11691: ppij=0.;
11692: for(i=1; i<=nlstate;i++) {
1.278 brouard 11693: if (mobilav>=1)
11694: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
11695: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
11696: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
11697: }
1.268 brouard 11698: fprintf(ficresf," %.3f", p3mat[i][j][h]);
11699: } /* end i */
11700: fprintf(ficresf," %.3f", ppij);
11701: }/* end j */
1.227 brouard 11702: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11703: } /* end agec */
1.266 brouard 11704: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
11705: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
1.227 brouard 11706: } /* end yearp */
11707: } /* end k */
1.219 brouard 11708:
1.126 brouard 11709: fclose(ficresf);
1.215 brouard 11710: printf("End of Computing forecasting \n");
11711: fprintf(ficlog,"End of Computing forecasting\n");
11712:
1.126 brouard 11713: }
11714:
1.269 brouard 11715: /************** Back Forecasting ******************/
1.296 brouard 11716: /* void prevbackforecast(char fileres[], double ***prevacurrent, double anback1, double mback1, double jback1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anback2, double p[], int cptcoveff){ */
11717: void prevbackforecast(char fileres[], double ***prevacurrent, double dateintmean, double dateprojd, double dateprojf, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double p[], int cptcoveff){
11718: /* back1, year, month, day of starting backprojection
1.267 brouard 11719: agemin, agemax range of age
11720: dateprev1 dateprev2 range of dates during which prevalence is computed
1.269 brouard 11721: anback2 year of end of backprojection (same day and month as back1).
11722: prevacurrent and prev are prevalences.
1.267 brouard 11723: */
1.359 brouard 11724: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
1.267 brouard 11725: double agec; /* generic age */
1.359 brouard 11726: double agelim, ppij, ppi; /* ,jintmean,mintmean,aintmean;*/
11727: /*double *popcount;*/
1.267 brouard 11728: double ***p3mat;
11729: /* double ***mobaverage; */
11730: char fileresfb[FILENAMELENGTH];
11731:
1.268 brouard 11732: agelim=AGEINF;
1.267 brouard 11733: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11734: in each health status at the date of interview (if between dateprev1 and dateprev2).
11735: We still use firstpass and lastpass as another selection.
11736: */
11737: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11738: /* firstpass, lastpass, stepm, weightopt, model); */
11739:
11740: /*Do we need to compute prevalence again?*/
11741:
11742: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
11743:
11744: strcpy(fileresfb,"FB_");
11745: strcat(fileresfb,fileresu);
11746: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
11747: printf("Problem with back forecast resultfile: %s\n", fileresfb);
11748: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
11749: }
11750: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11751: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11752:
11753: if (cptcoveff==0) ncodemax[cptcoveff]=1;
11754:
11755:
11756: stepsize=(int) (stepm+YEARM-1)/YEARM;
11757: if (stepm<=12) stepsize=1;
11758: if(estepm < stepm){
11759: printf ("Problem %d lower than %d\n",estepm, stepm);
11760: }
1.270 brouard 11761: else{
11762: hstepm=estepm;
11763: }
11764: if(estepm >= stepm){ /* Yes every two year */
11765: stepsize=2;
11766: }
1.267 brouard 11767:
11768: hstepm=hstepm/stepm;
1.296 brouard 11769: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11770: /* fractional in yp1 *\/ */
11771: /* aintmean=yp; */
11772: /* yp2=modf((yp1*12),&yp); */
11773: /* mintmean=yp; */
11774: /* yp1=modf((yp2*30.5),&yp); */
11775: /* jintmean=yp; */
11776: /* if(jintmean==0) jintmean=1; */
11777: /* if(mintmean==0) jintmean=1; */
1.267 brouard 11778:
1.351 brouard 11779: /* i1=pow(2,cptcoveff); */
11780: /* if (cptcovn < 1){i1=1;} */
1.267 brouard 11781:
1.296 brouard 11782: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
11783: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.267 brouard 11784:
11785: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
11786:
1.351 brouard 11787: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11788: k=TKresult[nres];
11789: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11790: /* for(k=1; k<=i1;k++){ */
11791: /* if(i1 != 1 && TKresult[nres]!= k) */
11792: /* continue; */
11793: /* if(invalidvarcomb[k]){ */
11794: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11795: /* continue; */
11796: /* } */
1.268 brouard 11797: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
1.351 brouard 11798: for(j=1;j<=cptcovs;j++){
11799: /* for(j=1;j<=cptcoveff;j++) { */
11800: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11801: /* } */
11802: fprintf(ficresfb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.267 brouard 11803: }
1.351 brouard 11804: /* fprintf(ficrespij,"******\n"); */
11805: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11806: /* fprintf(ficresfb," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11807: /* } */
1.267 brouard 11808: fprintf(ficresfb," yearbproj age");
11809: for(j=1; j<=nlstate+ndeath;j++){
11810: for(i=1; i<=nlstate;i++)
1.268 brouard 11811: fprintf(ficresfb," b%d%d",i,j);
11812: fprintf(ficresfb," b.%d",j);
1.267 brouard 11813: }
1.296 brouard 11814: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
1.267 brouard 11815: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
11816: fprintf(ficresfb,"\n");
1.296 brouard 11817: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
1.273 brouard 11818: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
1.270 brouard 11819: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
11820: for (agec=bage; agec<=fage; agec++){ /* testing */
1.268 brouard 11821: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
1.271 brouard 11822: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
1.267 brouard 11823: nhstepm = nhstepm/hstepm;
11824: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11825: oldm=oldms;savm=savms;
1.268 brouard 11826: /* computes hbxij at age agec over 1 to nhstepm */
1.271 brouard 11827: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
1.267 brouard 11828: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.268 brouard 11829: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
11830: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
11831: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
1.267 brouard 11832: for (h=0; h<=nhstepm; h++){
1.268 brouard 11833: if (h*hstepm/YEARM*stepm ==-yearp) {
11834: break;
11835: }
11836: }
11837: fprintf(ficresfb,"\n");
1.351 brouard 11838: /* for(j=1;j<=cptcoveff;j++) */
11839: for(j=1;j<=cptcovs;j++)
11840: fprintf(ficresfb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11841: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.296 brouard 11842: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
1.268 brouard 11843: for(i=1; i<=nlstate+ndeath;i++) {
11844: ppij=0.;ppi=0.;
11845: for(j=1; j<=nlstate;j++) {
11846: /* if (mobilav==1) */
1.269 brouard 11847: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
11848: ppi=ppi+prevacurrent[(int)agec][j][k];
11849: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
11850: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
1.267 brouard 11851: /* else { */
11852: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
11853: /* } */
1.268 brouard 11854: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
11855: } /* end j */
11856: if(ppi <0.99){
11857: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11858: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11859: }
11860: fprintf(ficresfb," %.3f", ppij);
11861: }/* end j */
1.267 brouard 11862: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11863: } /* end agec */
11864: } /* end yearp */
11865: } /* end k */
1.217 brouard 11866:
1.267 brouard 11867: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
1.217 brouard 11868:
1.267 brouard 11869: fclose(ficresfb);
11870: printf("End of Computing Back forecasting \n");
11871: fprintf(ficlog,"End of Computing Back forecasting\n");
1.218 brouard 11872:
1.267 brouard 11873: }
1.217 brouard 11874:
1.269 brouard 11875: /* Variance of prevalence limit: varprlim */
11876: void varprlim(char fileresu[], int nresult, double ***prevacurrent, int mobilavproj, double bage, double fage, double **prlim, int *ncvyearp, double ftolpl, double p[], double **matcov, double *delti, int stepm, int cptcoveff){
1.288 brouard 11877: /*------- Variance of forward period (stable) prevalence------*/
1.269 brouard 11878:
11879: char fileresvpl[FILENAMELENGTH];
11880: FILE *ficresvpl;
11881: double **oldm, **savm;
11882: double **varpl; /* Variances of prevalence limits by age */
11883: int i1, k, nres, j ;
11884:
11885: strcpy(fileresvpl,"VPL_");
11886: strcat(fileresvpl,fileresu);
11887: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.288 brouard 11888: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
1.269 brouard 11889: exit(0);
11890: }
1.288 brouard 11891: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
11892: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.269 brouard 11893:
11894: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11895: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
11896:
11897: i1=pow(2,cptcoveff);
11898: if (cptcovn < 1){i1=1;}
11899:
1.337 brouard 11900: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11901: k=TKresult[nres];
1.338 brouard 11902: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 11903: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
1.269 brouard 11904: if(i1 != 1 && TKresult[nres]!= k)
11905: continue;
11906: fprintf(ficresvpl,"\n#****** ");
11907: printf("\n#****** ");
11908: fprintf(ficlog,"\n#****** ");
1.337 brouard 11909: for(j=1;j<=cptcovs;j++) {
11910: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11911: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11912: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11913: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11914: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.269 brouard 11915: }
1.337 brouard 11916: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11917: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11918: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11919: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11920: /* } */
1.269 brouard 11921: fprintf(ficresvpl,"******\n");
11922: printf("******\n");
11923: fprintf(ficlog,"******\n");
11924:
11925: varpl=matrix(1,nlstate,(int) bage, (int) fage);
11926: oldm=oldms;savm=savms;
11927: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
11928: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
11929: /*}*/
11930: }
11931:
11932: fclose(ficresvpl);
1.288 brouard 11933: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
11934: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
1.269 brouard 11935:
11936: }
11937: /* Variance of back prevalence: varbprlim */
11938: void varbprlim(char fileresu[], int nresult, double ***prevacurrent, int mobilavproj, double bage, double fage, double **bprlim, int *ncvyearp, double ftolpl, double p[], double **matcov, double *delti, int stepm, int cptcoveff){
11939: /*------- Variance of back (stable) prevalence------*/
11940:
11941: char fileresvbl[FILENAMELENGTH];
11942: FILE *ficresvbl;
11943:
11944: double **oldm, **savm;
11945: double **varbpl; /* Variances of back prevalence limits by age */
11946: int i1, k, nres, j ;
11947:
11948: strcpy(fileresvbl,"VBL_");
11949: strcat(fileresvbl,fileresu);
11950: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
11951: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
11952: exit(0);
11953: }
11954: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
11955: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
11956:
11957:
11958: i1=pow(2,cptcoveff);
11959: if (cptcovn < 1){i1=1;}
11960:
1.337 brouard 11961: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11962: k=TKresult[nres];
1.338 brouard 11963: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 11964: /* for(k=1; k<=i1;k++){ */
11965: /* if(i1 != 1 && TKresult[nres]!= k) */
11966: /* continue; */
1.269 brouard 11967: fprintf(ficresvbl,"\n#****** ");
11968: printf("\n#****** ");
11969: fprintf(ficlog,"\n#****** ");
1.337 brouard 11970: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
1.338 brouard 11971: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11972: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11973: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
1.337 brouard 11974: /* for(j=1;j<=cptcoveff;j++) { */
11975: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11976: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11977: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11978: /* } */
11979: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11980: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11981: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11982: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
1.269 brouard 11983: }
11984: fprintf(ficresvbl,"******\n");
11985: printf("******\n");
11986: fprintf(ficlog,"******\n");
11987:
11988: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
11989: oldm=oldms;savm=savms;
11990:
11991: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
11992: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
11993: /*}*/
11994: }
11995:
11996: fclose(ficresvbl);
11997: printf("done variance-covariance of back prevalence\n");fflush(stdout);
11998: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
11999:
12000: } /* End of varbprlim */
12001:
1.126 brouard 12002: /************** Forecasting *****not tested NB*************/
1.227 brouard 12003: /* void populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2s, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){ */
1.126 brouard 12004:
1.227 brouard 12005: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
12006: /* int *popage; */
12007: /* double calagedatem, agelim, kk1, kk2; */
12008: /* double *popeffectif,*popcount; */
12009: /* double ***p3mat,***tabpop,***tabpopprev; */
12010: /* /\* double ***mobaverage; *\/ */
12011: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 12012:
1.227 brouard 12013: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12014: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12015: /* agelim=AGESUP; */
12016: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 12017:
1.227 brouard 12018: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 12019:
12020:
1.227 brouard 12021: /* strcpy(filerespop,"POP_"); */
12022: /* strcat(filerespop,fileresu); */
12023: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
12024: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
12025: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
12026: /* } */
12027: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
12028: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 12029:
1.227 brouard 12030: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 12031:
1.227 brouard 12032: /* /\* if (mobilav!=0) { *\/ */
12033: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
12034: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
12035: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
12036: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
12037: /* /\* } *\/ */
12038: /* /\* } *\/ */
1.126 brouard 12039:
1.227 brouard 12040: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
12041: /* if (stepm<=12) stepsize=1; */
1.126 brouard 12042:
1.227 brouard 12043: /* agelim=AGESUP; */
1.126 brouard 12044:
1.227 brouard 12045: /* hstepm=1; */
12046: /* hstepm=hstepm/stepm; */
1.218 brouard 12047:
1.227 brouard 12048: /* if (popforecast==1) { */
12049: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
12050: /* printf("Problem with population file : %s\n",popfile);exit(0); */
12051: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
12052: /* } */
12053: /* popage=ivector(0,AGESUP); */
12054: /* popeffectif=vector(0,AGESUP); */
12055: /* popcount=vector(0,AGESUP); */
1.126 brouard 12056:
1.227 brouard 12057: /* i=1; */
12058: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 12059:
1.227 brouard 12060: /* imx=i; */
12061: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
12062: /* } */
1.218 brouard 12063:
1.227 brouard 12064: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
12065: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
12066: /* k=k+1; */
12067: /* fprintf(ficrespop,"\n#******"); */
12068: /* for(j=1;j<=cptcoveff;j++) { */
12069: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
12070: /* } */
12071: /* fprintf(ficrespop,"******\n"); */
12072: /* fprintf(ficrespop,"# Age"); */
12073: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
12074: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 12075:
1.227 brouard 12076: /* for (cpt=0; cpt<=0;cpt++) { */
12077: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 12078:
1.227 brouard 12079: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12080: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12081: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 12082:
1.227 brouard 12083: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12084: /* oldm=oldms;savm=savms; */
12085: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 12086:
1.227 brouard 12087: /* for (h=0; h<=nhstepm; h++){ */
12088: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12089: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12090: /* } */
12091: /* for(j=1; j<=nlstate+ndeath;j++) { */
12092: /* kk1=0.;kk2=0; */
12093: /* for(i=1; i<=nlstate;i++) { */
12094: /* if (mobilav==1) */
12095: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
12096: /* else { */
12097: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
12098: /* } */
12099: /* } */
12100: /* if (h==(int)(calagedatem+12*cpt)){ */
12101: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
12102: /* /\*fprintf(ficrespop," %.3f", kk1); */
12103: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
12104: /* } */
12105: /* } */
12106: /* for(i=1; i<=nlstate;i++){ */
12107: /* kk1=0.; */
12108: /* for(j=1; j<=nlstate;j++){ */
12109: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
12110: /* } */
12111: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
12112: /* } */
1.218 brouard 12113:
1.227 brouard 12114: /* if (h==(int)(calagedatem+12*cpt)) */
12115: /* for(j=1; j<=nlstate;j++) */
12116: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
12117: /* } */
12118: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12119: /* } */
12120: /* } */
1.218 brouard 12121:
1.227 brouard 12122: /* /\******\/ */
1.218 brouard 12123:
1.227 brouard 12124: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
12125: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
12126: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12127: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12128: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 12129:
1.227 brouard 12130: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12131: /* oldm=oldms;savm=savms; */
12132: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12133: /* for (h=0; h<=nhstepm; h++){ */
12134: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12135: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12136: /* } */
12137: /* for(j=1; j<=nlstate+ndeath;j++) { */
12138: /* kk1=0.;kk2=0; */
12139: /* for(i=1; i<=nlstate;i++) { */
12140: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
12141: /* } */
12142: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
12143: /* } */
12144: /* } */
12145: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12146: /* } */
12147: /* } */
12148: /* } */
12149: /* } */
1.218 brouard 12150:
1.227 brouard 12151: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 12152:
1.227 brouard 12153: /* if (popforecast==1) { */
12154: /* free_ivector(popage,0,AGESUP); */
12155: /* free_vector(popeffectif,0,AGESUP); */
12156: /* free_vector(popcount,0,AGESUP); */
12157: /* } */
12158: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12159: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12160: /* fclose(ficrespop); */
12161: /* } /\* End of popforecast *\/ */
1.218 brouard 12162:
1.126 brouard 12163: int fileappend(FILE *fichier, char *optionfich)
12164: {
12165: if((fichier=fopen(optionfich,"a"))==NULL) {
12166: printf("Problem with file: %s\n", optionfich);
12167: fprintf(ficlog,"Problem with file: %s\n", optionfich);
12168: return (0);
12169: }
12170: fflush(fichier);
12171: return (1);
12172: }
12173:
12174:
12175: /**************** function prwizard **********************/
12176: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
12177: {
12178:
12179: /* Wizard to print covariance matrix template */
12180:
1.164 brouard 12181: char ca[32], cb[32];
12182: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 12183: int numlinepar;
12184:
12185: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12186: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12187: for(i=1; i <=nlstate; i++){
12188: jj=0;
12189: for(j=1; j <=nlstate+ndeath; j++){
12190: if(j==i) continue;
12191: jj++;
12192: /*ca[0]= k+'a'-1;ca[1]='\0';*/
12193: printf("%1d%1d",i,j);
12194: fprintf(ficparo,"%1d%1d",i,j);
12195: for(k=1; k<=ncovmodel;k++){
12196: /* printf(" %lf",param[i][j][k]); */
12197: /* fprintf(ficparo," %lf",param[i][j][k]); */
12198: printf(" 0.");
12199: fprintf(ficparo," 0.");
12200: }
12201: printf("\n");
12202: fprintf(ficparo,"\n");
12203: }
12204: }
12205: printf("# Scales (for hessian or gradient estimation)\n");
12206: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
12207: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
12208: for(i=1; i <=nlstate; i++){
12209: jj=0;
12210: for(j=1; j <=nlstate+ndeath; j++){
12211: if(j==i) continue;
12212: jj++;
12213: fprintf(ficparo,"%1d%1d",i,j);
12214: printf("%1d%1d",i,j);
12215: fflush(stdout);
12216: for(k=1; k<=ncovmodel;k++){
12217: /* printf(" %le",delti3[i][j][k]); */
12218: /* fprintf(ficparo," %le",delti3[i][j][k]); */
12219: printf(" 0.");
12220: fprintf(ficparo," 0.");
12221: }
12222: numlinepar++;
12223: printf("\n");
12224: fprintf(ficparo,"\n");
12225: }
12226: }
12227: printf("# Covariance matrix\n");
12228: /* # 121 Var(a12)\n\ */
12229: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12230: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12231: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12232: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12233: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12234: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12235: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12236: fflush(stdout);
12237: fprintf(ficparo,"# Covariance matrix\n");
12238: /* # 121 Var(a12)\n\ */
12239: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12240: /* # ...\n\ */
12241: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12242:
12243: for(itimes=1;itimes<=2;itimes++){
12244: jj=0;
12245: for(i=1; i <=nlstate; i++){
12246: for(j=1; j <=nlstate+ndeath; j++){
12247: if(j==i) continue;
12248: for(k=1; k<=ncovmodel;k++){
12249: jj++;
12250: ca[0]= k+'a'-1;ca[1]='\0';
12251: if(itimes==1){
12252: printf("#%1d%1d%d",i,j,k);
12253: fprintf(ficparo,"#%1d%1d%d",i,j,k);
12254: }else{
12255: printf("%1d%1d%d",i,j,k);
12256: fprintf(ficparo,"%1d%1d%d",i,j,k);
12257: /* printf(" %.5le",matcov[i][j]); */
12258: }
12259: ll=0;
12260: for(li=1;li <=nlstate; li++){
12261: for(lj=1;lj <=nlstate+ndeath; lj++){
12262: if(lj==li) continue;
12263: for(lk=1;lk<=ncovmodel;lk++){
12264: ll++;
12265: if(ll<=jj){
12266: cb[0]= lk +'a'-1;cb[1]='\0';
12267: if(ll<jj){
12268: if(itimes==1){
12269: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12270: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12271: }else{
12272: printf(" 0.");
12273: fprintf(ficparo," 0.");
12274: }
12275: }else{
12276: if(itimes==1){
12277: printf(" Var(%s%1d%1d)",ca,i,j);
12278: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
12279: }else{
12280: printf(" 0.");
12281: fprintf(ficparo," 0.");
12282: }
12283: }
12284: }
12285: } /* end lk */
12286: } /* end lj */
12287: } /* end li */
12288: printf("\n");
12289: fprintf(ficparo,"\n");
12290: numlinepar++;
12291: } /* end k*/
12292: } /*end j */
12293: } /* end i */
12294: } /* end itimes */
12295:
12296: } /* end of prwizard */
12297: /******************* Gompertz Likelihood ******************************/
12298: double gompertz(double x[])
12299: {
1.302 brouard 12300: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
1.126 brouard 12301: int i,n=0; /* n is the size of the sample */
12302:
1.220 brouard 12303: for (i=1;i<=imx ; i++) {
1.126 brouard 12304: sump=sump+weight[i];
12305: /* sump=sump+1;*/
12306: num=num+1;
12307: }
1.302 brouard 12308: L=0.0;
12309: /* agegomp=AGEGOMP; */
1.126 brouard 12310: /* for (i=0; i<=imx; i++)
12311: if (wav[i]>0) printf("i=%d ageex=%lf agecens=%lf agedc=%lf cens=%d %d\n" ,i,ageexmed[i],agecens[i],agedc[i],cens[i],wav[i]);*/
12312:
1.302 brouard 12313: for (i=1;i<=imx ; i++) {
12314: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
12315: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
12316: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
12317: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
12318: * +
12319: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
12320: */
12321: if (wav[i] > 1 || agedc[i] < AGESUP) {
12322: if (cens[i] == 1){
12323: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
12324: } else if (cens[i] == 0){
1.126 brouard 12325: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
1.362 brouard 12326: +log(fabs(x[1])/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
12327: /* +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM); */ /* To be seen */
1.302 brouard 12328: } else
12329: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
1.126 brouard 12330: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
1.302 brouard 12331: L=L+A*weight[i];
1.126 brouard 12332: /* printf("\ni=%d A=%f L=%lf x[1]=%lf x[2]=%lf ageex=%lf agecens=%lf cens=%d agedc=%lf weight=%lf\n",i,A,L,x[1],x[2],ageexmed[i]*12,agecens[i]*12,cens[i],agedc[i]*12,weight[i]);*/
1.302 brouard 12333: }
12334: }
1.126 brouard 12335:
1.302 brouard 12336: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
1.126 brouard 12337:
12338: return -2*L*num/sump;
12339: }
12340:
1.136 brouard 12341: #ifdef GSL
12342: /******************* Gompertz_f Likelihood ******************************/
12343: double gompertz_f(const gsl_vector *v, void *params)
12344: {
1.302 brouard 12345: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
1.136 brouard 12346: double *x= (double *) v->data;
12347: int i,n=0; /* n is the size of the sample */
12348:
12349: for (i=0;i<=imx-1 ; i++) {
12350: sump=sump+weight[i];
12351: /* sump=sump+1;*/
12352: num=num+1;
12353: }
12354:
12355:
12356: /* for (i=0; i<=imx; i++)
12357: if (wav[i]>0) printf("i=%d ageex=%lf agecens=%lf agedc=%lf cens=%d %d\n" ,i,ageexmed[i],agecens[i],agedc[i],cens[i],wav[i]);*/
12358: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
12359: for (i=1;i<=imx ; i++)
12360: {
12361: if (cens[i] == 1 && wav[i]>1)
12362: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
12363:
12364: if (cens[i] == 0 && wav[i]>1)
12365: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
12366: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
12367:
12368: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
12369: if (wav[i] > 1 ) { /* ??? */
12370: LL=LL+A*weight[i];
12371: /* printf("\ni=%d A=%f L=%lf x[1]=%lf x[2]=%lf ageex=%lf agecens=%lf cens=%d agedc=%lf weight=%lf\n",i,A,L,x[1],x[2],ageexmed[i]*12,agecens[i]*12,cens[i],agedc[i]*12,weight[i]);*/
12372: }
12373: }
12374:
12375: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
12376: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
12377:
12378: return -2*LL*num/sump;
12379: }
12380: #endif
12381:
1.126 brouard 12382: /******************* Printing html file ***********/
1.201 brouard 12383: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 12384: int lastpass, int stepm, int weightopt, char model[],\
12385: int imx, double p[],double **matcov,double agemortsup){
12386: int i,k;
12387:
12388: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
12389: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
12390: for (i=1;i<=2;i++)
12391: fprintf(fichtm," p[%d] = %lf [%f ; %f]<br>\n",i,p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.199 brouard 12392: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 12393: fprintf(fichtm,"</ul>");
12394:
12395: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
12396:
12397: fprintf(fichtm,"\nAge l<inf>x</inf> q<inf>x</inf> d(x,x+1) L<inf>x</inf> T<inf>x</inf> e<infx</inf><br>");
12398:
12399: for (k=agegomp;k<(agemortsup-2);k++)
12400: fprintf(fichtm,"%d %.0lf %lf %.0lf %.0lf %.0lf %lf<br>\n",k,lsurv[k],p[1]*exp(p[2]*(k-agegomp)),(p[1]*exp(p[2]*(k-agegomp)))*lsurv[k],lpop[k],tpop[k],tpop[k]/lsurv[k]);
12401:
12402:
12403: fflush(fichtm);
12404: }
12405:
12406: /******************* Gnuplot file **************/
1.201 brouard 12407: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 12408:
12409: char dirfileres[132],optfileres[132];
1.164 brouard 12410:
1.359 brouard 12411: /*int ng;*/
1.126 brouard 12412:
12413:
12414: /*#ifdef windows */
12415: fprintf(ficgp,"cd \"%s\" \n",pathc);
12416: /*#endif */
12417:
12418:
12419: strcpy(dirfileres,optionfilefiname);
12420: strcpy(optfileres,"vpl");
1.199 brouard 12421: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 12422: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 12423: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 12424: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 12425: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
12426:
12427: }
12428:
1.136 brouard 12429: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
12430: {
1.126 brouard 12431:
1.136 brouard 12432: /*-------- data file ----------*/
12433: FILE *fic;
12434: char dummy[]=" ";
1.359 brouard 12435: int i = 0, j = 0, n = 0, iv = 0;/* , v;*/
1.223 brouard 12436: int lstra;
1.136 brouard 12437: int linei, month, year,iout;
1.302 brouard 12438: int noffset=0; /* This is the offset if BOM data file */
1.136 brouard 12439: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 12440: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 12441: char *stratrunc;
1.223 brouard 12442:
1.349 brouard 12443: /* DummyV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
12444: /* FixedV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
1.339 brouard 12445:
12446: ncovcolt=ncovcol+nqv+ntv+nqtv; /* total of covariates in the data, not in the model equation */
12447:
1.136 brouard 12448: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 12449: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12450: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 12451: }
1.126 brouard 12452:
1.302 brouard 12453: /* Is it a BOM UTF-8 Windows file? */
12454: /* First data line */
12455: linei=0;
12456: while(fgets(line, MAXLINE, fic)) {
12457: noffset=0;
12458: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
12459: {
12460: noffset=noffset+3;
12461: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
12462: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
12463: fflush(ficlog); return 1;
12464: }
12465: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
12466: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
12467: {
12468: noffset=noffset+2;
1.304 brouard 12469: printf("# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);fflush(stdout);
12470: fprintf(ficlog,"# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);
1.302 brouard 12471: fflush(ficlog); return 1;
12472: }
12473: else if( line[0] == 0 && line[1] == 0)
12474: {
12475: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
12476: noffset=noffset+4;
1.304 brouard 12477: printf("# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);fflush(stdout);
12478: fprintf(ficlog,"# Error Data file '%s' is a huge UTF16BE BOM file, please convert to UTF8 or ascii file (for example with dos2unix) and rerun.\n",datafile);
1.302 brouard 12479: fflush(ficlog); return 1;
12480: }
12481: } else{
12482: ;/*printf(" Not a BOM file\n");*/
12483: }
12484: /* If line starts with a # it is a comment */
12485: if (line[noffset] == '#') {
12486: linei=linei+1;
12487: break;
12488: }else{
12489: break;
12490: }
12491: }
12492: fclose(fic);
12493: if((fic=fopen(datafile,"r"))==NULL) {
12494: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12495: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
12496: }
12497: /* Not a Bom file */
12498:
1.136 brouard 12499: i=1;
12500: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
12501: linei=linei+1;
12502: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
12503: if(line[j] == '\t')
12504: line[j] = ' ';
12505: }
12506: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
12507: ;
12508: };
12509: line[j+1]=0; /* Trims blanks at end of line */
12510: if(line[0]=='#'){
12511: fprintf(ficlog,"Comment line\n%s\n",line);
12512: printf("Comment line\n%s\n",line);
12513: continue;
12514: }
12515: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 12516: strcpy(line, linetmp);
1.223 brouard 12517:
12518: /* Loops on waves */
12519: for (j=maxwav;j>=1;j--){
12520: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 12521: cutv(stra, strb, line, ' ');
12522: if(strb[0]=='.') { /* Missing value */
12523: lval=-1;
12524: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
1.341 brouard 12525: cotvar[j][ncovcol+nqv+ntv+iv][i]=-1; /* For performance reasons */
1.238 brouard 12526: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
12527: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. If missing, you should remove this individual or impute a value. Exiting.\n", strb, linei,i,line,iv, nqtv, j);
12528: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. If missing, you should remove this individual or impute a value. Exiting.\n", strb, linei,i,line,iv, nqtv, j);fflush(ficlog);
12529: return 1;
12530: }
12531: }else{
12532: errno=0;
12533: /* what_kind_of_number(strb); */
12534: dval=strtod(strb,&endptr);
12535: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
12536: /* if(strb != endptr && *endptr == '\0') */
12537: /* dval=dlval; */
12538: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12539: if( strb[0]=='\0' || (*endptr != '\0')){
12540: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, nqtv, j,maxwav);
12541: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqtv, j,maxwav);fflush(ficlog);
12542: return 1;
12543: }
12544: cotqvar[j][iv][i]=dval;
1.341 brouard 12545: cotvar[j][ncovcol+nqv+ntv+iv][i]=dval; /* because cotvar starts now at first ntv */
1.238 brouard 12546: }
12547: strcpy(line,stra);
1.223 brouard 12548: }/* end loop ntqv */
1.225 brouard 12549:
1.223 brouard 12550: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 12551: cutv(stra, strb, line, ' ');
12552: if(strb[0]=='.') { /* Missing value */
12553: lval=-1;
12554: }else{
12555: errno=0;
12556: lval=strtol(strb,&endptr,10);
12557: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
12558: if( strb[0]=='\0' || (*endptr != '\0')){
12559: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th dummy covariate out of %d measured at wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, ntv, j,maxwav);
12560: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d dummy covariate out of %d measured wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,iv, ntv,j,maxwav);fflush(ficlog);
12561: return 1;
12562: }
12563: }
12564: if(lval <-1 || lval >1){
12565: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 12566: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
1.223 brouard 12567: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 12568: For example, for multinomial values like 1, 2 and 3,\n \
12569: build V1=0 V2=0 for the reference value (1),\n \
12570: V1=1 V2=0 for (2) \n \
1.223 brouard 12571: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 12572: output of IMaCh is often meaningless.\n \
1.319 brouard 12573: Exiting.\n",lval,linei, i,line,iv,j);
1.238 brouard 12574: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 12575: Should be a value of %d(nth) covariate of wave %d (0 should be the value for the reference and 1\n \
1.223 brouard 12576: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 12577: For example, for multinomial values like 1, 2 and 3,\n \
12578: build V1=0 V2=0 for the reference value (1),\n \
12579: V1=1 V2=0 for (2) \n \
1.223 brouard 12580: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 12581: output of IMaCh is often meaningless.\n \
1.319 brouard 12582: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
1.238 brouard 12583: return 1;
12584: }
1.341 brouard 12585: cotvar[j][ncovcol+nqv+iv][i]=(double)(lval);
1.238 brouard 12586: strcpy(line,stra);
1.223 brouard 12587: }/* end loop ntv */
1.225 brouard 12588:
1.223 brouard 12589: /* Statuses at wave */
1.137 brouard 12590: cutv(stra, strb, line, ' ');
1.223 brouard 12591: if(strb[0]=='.') { /* Missing value */
1.238 brouard 12592: lval=-1;
1.136 brouard 12593: }else{
1.238 brouard 12594: errno=0;
12595: lval=strtol(strb,&endptr,10);
12596: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
1.347 brouard 12597: if( strb[0]=='\0' || (*endptr != '\0' )){
12598: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);
12599: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a status of wave %d. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line,j,maxwav);fflush(ficlog);
12600: return 1;
12601: }else if( lval==0 || lval > nlstate+ndeath){
1.348 brouard 12602: printf("Error in data around '%s' at line number %d for individual %d, '%s'\n Should be a state at wave %d. A state should be 1 to %d and not %ld.\n Fix your data file '%s'! Exiting.\n", strb, linei,i,line,j,nlstate+ndeath, lval, datafile);fflush(stdout);
12603: fprintf(ficlog,"Error in data around '%s' at line number %d for individual %d, '%s'\n Should be a state at wave %d. A state should be 1 to %d and not %ld.\n Fix your data file '%s'! Exiting.\n", strb, linei,i,line,j,nlstate+ndeath, lval, datafile); fflush(ficlog);
1.238 brouard 12604: return 1;
12605: }
1.136 brouard 12606: }
1.225 brouard 12607:
1.136 brouard 12608: s[j][i]=lval;
1.225 brouard 12609:
1.223 brouard 12610: /* Date of Interview */
1.136 brouard 12611: strcpy(line,stra);
12612: cutv(stra, strb,line,' ');
1.169 brouard 12613: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12614: }
1.169 brouard 12615: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 12616: month=99;
12617: year=9999;
1.136 brouard 12618: }else{
1.225 brouard 12619: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of interview (mm/yyyy or .) at wave %d. Exiting.\n",strb, linei,i, line,j);
12620: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of interview (mm/yyyy or .) at wave %d. Exiting.\n",strb, linei,i, line,j);fflush(ficlog);
12621: return 1;
1.136 brouard 12622: }
12623: anint[j][i]= (double) year;
1.302 brouard 12624: mint[j][i]= (double)month;
12625: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
12626: /* printf("Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, mint[j][i],anint[j][i], moisnais[i],annais[i]); */
12627: /* fprintf(ficlog,"Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, mint[j][i],anint[j][i], moisnais[i],annais[i]); */
12628: /* } */
1.136 brouard 12629: strcpy(line,stra);
1.223 brouard 12630: } /* End loop on waves */
1.225 brouard 12631:
1.223 brouard 12632: /* Date of death */
1.136 brouard 12633: cutv(stra, strb,line,' ');
1.169 brouard 12634: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12635: }
1.169 brouard 12636: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 12637: month=99;
12638: year=9999;
12639: }else{
1.141 brouard 12640: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
1.225 brouard 12641: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
12642: return 1;
1.136 brouard 12643: }
12644: andc[i]=(double) year;
12645: moisdc[i]=(double) month;
12646: strcpy(line,stra);
12647:
1.223 brouard 12648: /* Date of birth */
1.136 brouard 12649: cutv(stra, strb,line,' ');
1.169 brouard 12650: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12651: }
1.169 brouard 12652: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 12653: month=99;
12654: year=9999;
12655: }else{
1.141 brouard 12656: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
12657: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 12658: return 1;
1.136 brouard 12659: }
12660: if (year==9999) {
1.141 brouard 12661: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);
12662: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 brouard 12663: return 1;
12664:
1.136 brouard 12665: }
12666: annais[i]=(double)(year);
1.302 brouard 12667: moisnais[i]=(double)(month);
12668: for (j=1;j<=maxwav;j++){
12669: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
12670: printf("Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, (int)mint[j][i],(int)anint[j][i], j,(int)moisnais[i],(int)annais[i]);
12671: fprintf(ficlog,"Warning reading data around '%s' at line number %d for individual %d, '%s'\nThe date of interview (%2d/%4d) at wave %d occurred before the date of birth (%2d/%4d).\n",strb, linei,i, line, (int)mint[j][i],(int)anint[j][i], j, (int)moisnais[i],(int)annais[i]);
12672: }
12673: }
12674:
1.136 brouard 12675: strcpy(line,stra);
1.225 brouard 12676:
1.223 brouard 12677: /* Sample weight */
1.136 brouard 12678: cutv(stra, strb,line,' ');
12679: errno=0;
12680: dval=strtod(strb,&endptr);
12681: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 12682: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
12683: fprintf(ficlog,"Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
1.136 brouard 12684: fflush(ficlog);
12685: return 1;
12686: }
12687: weight[i]=dval;
12688: strcpy(line,stra);
1.225 brouard 12689:
1.223 brouard 12690: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
12691: cutv(stra, strb, line, ' ');
12692: if(strb[0]=='.') { /* Missing value */
1.225 brouard 12693: lval=-1;
1.311 brouard 12694: coqvar[iv][i]=NAN;
12695: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
1.223 brouard 12696: }else{
1.225 brouard 12697: errno=0;
12698: /* what_kind_of_number(strb); */
12699: dval=strtod(strb,&endptr);
12700: /* if(strb != endptr && *endptr == '\0') */
12701: /* dval=dlval; */
12702: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12703: if( strb[0]=='\0' || (*endptr != '\0')){
12704: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value (out of %d) constant for all waves. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqv, maxwav);
12705: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be the %d th quantitative value (out of %d) constant for all waves. Setting maxwav=%d might be wrong. Exiting.\n", strb, linei,i,line, iv, nqv, maxwav);fflush(ficlog);
12706: return 1;
12707: }
12708: coqvar[iv][i]=dval;
1.226 brouard 12709: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 12710: }
12711: strcpy(line,stra);
12712: }/* end loop nqv */
1.136 brouard 12713:
1.223 brouard 12714: /* Covariate values */
1.136 brouard 12715: for (j=ncovcol;j>=1;j--){
12716: cutv(stra, strb,line,' ');
1.223 brouard 12717: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 12718: lval=-1;
1.136 brouard 12719: }else{
1.225 brouard 12720: errno=0;
12721: lval=strtol(strb,&endptr,10);
12722: if( strb[0]=='\0' || (*endptr != '\0')){
12723: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\nShould be a covariate value (=0 for the reference or 1 for alternative). Exiting.\n",lval, linei,i, line);
12724: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\nShould be a covariate value (=0 for the reference or 1 for alternative). Exiting.\n",lval, linei,i, line);fflush(ficlog);
12725: return 1;
12726: }
1.136 brouard 12727: }
12728: if(lval <-1 || lval >1){
1.225 brouard 12729: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 12730: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12731: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 12732: For example, for multinomial values like 1, 2 and 3,\n \
12733: build V1=0 V2=0 for the reference value (1),\n \
12734: V1=1 V2=0 for (2) \n \
1.136 brouard 12735: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 12736: output of IMaCh is often meaningless.\n \
1.136 brouard 12737: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 12738: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 12739: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12740: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 12741: For example, for multinomial values like 1, 2 and 3,\n \
12742: build V1=0 V2=0 for the reference value (1),\n \
12743: V1=1 V2=0 for (2) \n \
1.136 brouard 12744: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 12745: output of IMaCh is often meaningless.\n \
1.136 brouard 12746: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 12747: return 1;
1.136 brouard 12748: }
12749: covar[j][i]=(double)(lval);
12750: strcpy(line,stra);
12751: }
12752: lstra=strlen(stra);
1.225 brouard 12753:
1.136 brouard 12754: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
12755: stratrunc = &(stra[lstra-9]);
12756: num[i]=atol(stratrunc);
12757: }
12758: else
12759: num[i]=atol(stra);
12760: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
12761: printf("%ld %.lf %.lf %.lf %.lf/%.lf %.lf/%.lf %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d %.lf/%.lf %d\n",num[i],(covar[1][i]), (covar[2][i]),weight[i], (moisnais[i]), (annais[i]), (moisdc[i]), (andc[i]), (mint[1][i]), (anint[1][i]), (s[1][i]), (mint[2][i]), (anint[2][i]), (s[2][i]), (mint[3][i]), (anint[3][i]), (s[3][i]), (mint[4][i]), (anint[4][i]), (s[4][i])); ij=ij+1;}*/
12762:
12763: i=i+1;
12764: } /* End loop reading data */
1.225 brouard 12765:
1.136 brouard 12766: *imax=i-1; /* Number of individuals */
12767: fclose(fic);
1.225 brouard 12768:
1.136 brouard 12769: return (0);
1.164 brouard 12770: /* endread: */
1.225 brouard 12771: printf("Exiting readdata: ");
12772: fclose(fic);
12773: return (1);
1.223 brouard 12774: }
1.126 brouard 12775:
1.234 brouard 12776: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 12777: char *p1 = *stri, *p2 = *stri;
1.235 brouard 12778: while (*p2 == ' ')
1.234 brouard 12779: p2++;
12780: /* while ((*p1++ = *p2++) !=0) */
12781: /* ; */
12782: /* do */
12783: /* while (*p2 == ' ') */
12784: /* p2++; */
12785: /* while (*p1++ == *p2++); */
12786: *stri=p2;
1.145 brouard 12787: }
12788:
1.330 brouard 12789: int decoderesult( char resultline[], int nres)
1.230 brouard 12790: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
12791: {
1.235 brouard 12792: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 12793: char resultsav[MAXLINE];
1.330 brouard 12794: /* int resultmodel[MAXLINE]; */
1.334 brouard 12795: /* int modelresult[MAXLINE]; */
1.230 brouard 12796: char stra[80], strb[80], strc[80], strd[80],stre[80];
12797:
1.234 brouard 12798: removefirstspace(&resultline);
1.332 brouard 12799: printf("decoderesult:%s\n",resultline);
1.230 brouard 12800:
1.332 brouard 12801: strcpy(resultsav,resultline);
1.342 brouard 12802: /* printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline); */
1.230 brouard 12803: if (strlen(resultsav) >1){
1.334 brouard 12804: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
1.230 brouard 12805: }
1.353 brouard 12806: if(j == 0 && cptcovs== 0){ /* Resultline but no = and no covariate in the model */
1.253 brouard 12807: TKresult[nres]=0; /* Combination for the nresult and the model */
12808: return (0);
12809: }
1.234 brouard 12810: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
1.353 brouard 12811: fprintf(ficlog,"ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, 1+age+%s.\n",j, cptcovs, model);fflush(ficlog);
12812: printf("ERROR: the number of variables in the resultline which is %d, differs from the number %d of single variables used in the model line, 1+age+%s.\n",j, cptcovs, model);fflush(stdout);
12813: if(j==0)
12814: return 1;
1.234 brouard 12815: }
1.334 brouard 12816: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
1.234 brouard 12817: if(nbocc(resultsav,'=') >1){
1.318 brouard 12818: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' ' (stra is the rest of the resultline to be analyzed in the next loop *//* resultsav= "V4=1 V5=25.1 V3=0" stra= "V5=25.1 V3=0" strb= "V4=1" */
1.332 brouard 12819: /* If resultsav= "V4= 1 V5=25.1 V3=0" with a blank then strb="V4=" and stra="1 V5=25.1 V3=0" */
1.318 brouard 12820: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
1.332 brouard 12821: /* If a blank, then strc="V4=" and strd='\0' */
12822: if(strc[0]=='\0'){
12823: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
12824: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
12825: return 1;
12826: }
1.234 brouard 12827: }else
12828: cutl(strc,strd,resultsav,'=');
1.318 brouard 12829: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
1.234 brouard 12830:
1.230 brouard 12831: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
1.318 brouard 12832: Tvarsel[k]=atoi(strc); /* 4 */ /* Tvarsel is the id of the kth covariate in the result line Tvarsel[1] in "V4=1.." is 4.*/
1.230 brouard 12833: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
12834: /* cptcovsel++; */
12835: if (nbocc(stra,'=') >0)
12836: strcpy(resultsav,stra); /* and analyzes it */
12837: }
1.235 brouard 12838: /* Checking for missing or useless values in comparison of current model needs */
1.332 brouard 12839: /* Feeds resultmodel[nres][k1]=k2 for k1th product covariate with age in the model equation fed by the index k2 of the resutline*/
1.334 brouard 12840: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on MODEL LINE V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.332 brouard 12841: if(Typevar[k1]==0){ /* Single covariate in model */
12842: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.234 brouard 12843: match=0;
1.318 brouard 12844: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12845: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
1.334 brouard 12846: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.318 brouard 12847: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
1.234 brouard 12848: break;
12849: }
12850: }
12851: if(match == 0){
1.338 brouard 12852: printf("Error in result line (Dummy single): V%d is missing in result: %s according to model=1+age+%s. Tvar[k1=%d]=%d is different from Tvarsel[k2=%d]=%d.\n",Tvar[k1], resultline, model,k1, Tvar[k1], k2, Tvarsel[k2]);
12853: fprintf(ficlog,"Error in result line (Dummy single): V%d is missing in result: %s according to model=1+age+%s\n",Tvar[k1], resultline, model);
1.310 brouard 12854: return 1;
1.234 brouard 12855: }
1.332 brouard 12856: }else if(Typevar[k1]==1){ /* Product with age We want to get the position k2 in the resultline of the product k1 in the model line*/
12857: /* We feed resultmodel[k1]=k2; */
12858: match=0;
12859: for(k2=1; k2 <=j;k2++){/* Loop on resultline. jth occurence of = signs in the result line. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12860: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
1.334 brouard 12861: modelresult[nres][k2]=k1;/* we found a Vn=1 corrresponding to Vn*age in the model modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.332 brouard 12862: resultmodel[nres][k1]=k2; /* Added here */
1.342 brouard 12863: /* printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]); */
1.332 brouard 12864: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12865: break;
12866: }
12867: }
12868: if(match == 0){
1.338 brouard 12869: printf("Error in result line (Product with age): V%d is missing in result: %s according to model=1+age+%s (Tvarsel[k2=%d]=%d)\n",Tvar[k1], resultline, model, k2, Tvarsel[k2]);
12870: fprintf(ficlog,"Error in result line (Product with age): V%d is missing in result: %s according to model=1+age+%s (Tvarsel[k2=%d]=%d)\n",Tvar[k1], resultline, model, k2, Tvarsel[k2]);
1.332 brouard 12871: return 1;
12872: }
1.349 brouard 12873: }else if(Typevar[k1]==2 || Typevar[k1]==3){ /* Product with or without age. We want to get the position in the resultline of the product in the model line*/
1.332 brouard 12874: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
12875: match=0;
1.342 brouard 12876: /* printf("Decoderesult very first Product Tvardk[k1=%d][1]=%d Tvardk[k1=%d][2]=%d V%d * V%d\n",k1,Tvardk[k1][1],k1,Tvardk[k1][2],Tvardk[k1][1],Tvardk[k1][2]); */
1.332 brouard 12877: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12878: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12879: /* modelresult[k2]=k1; */
1.342 brouard 12880: /* printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]); */
1.332 brouard 12881: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12882: }
12883: }
12884: if(match == 0){
1.349 brouard 12885: printf("Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
12886: fprintf(ficlog,"Error in result line (Product without age first variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][1], resultline, model);
1.332 brouard 12887: return 1;
12888: }
12889: match=0;
12890: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12891: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12892: /* modelresult[k2]=k1;*/
1.342 brouard 12893: /* printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]); */
1.332 brouard 12894: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12895: break;
12896: }
12897: }
12898: if(match == 0){
1.349 brouard 12899: printf("Error in result line (Product without age second variable or double product with age): V%d is missing in result: %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
12900: fprintf(ficlog,"Error in result line (Product without age second variable or double product with age): V%d is missing in result : %s according to model=1+age+%s\n",Tvardk[k1][2], resultline, model);
1.332 brouard 12901: return 1;
12902: }
12903: }/* End of testing */
1.333 brouard 12904: }/* End loop cptcovt */
1.235 brouard 12905: /* Checking for missing or useless values in comparison of current model needs */
1.332 brouard 12906: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
1.334 brouard 12907: for(k2=1; k2 <=j;k2++){ /* j or cptcovs is the number of single covariates used either in the model line as well as in the result line (dummy or quantitative)
12908: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 12909: match=0;
1.318 brouard 12910: for(k1=1; k1<= cptcovt ;k1++){ /* loop on model: model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.332 brouard 12911: if(Typevar[k1]==0){ /* Single only */
1.349 brouard 12912: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 What if a product? */
1.330 brouard 12913: resultmodel[nres][k1]=k2; /* k1th position in the model equation corresponds to k2th position in the result line. resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.334 brouard 12914: modelresult[nres][k2]=k1; /* k1th position in the model equation corresponds to k2th position in the result line. modelresult[1]=2 modelresult[2]=1 modelresult[3]=3 remodelresult[4]=6 modelresult[5]=9 */
1.234 brouard 12915: ++match;
12916: }
12917: }
12918: }
12919: if(match == 0){
1.338 brouard 12920: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
12921: fprintf(ficlog,"Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
1.310 brouard 12922: return 1;
1.234 brouard 12923: }else if(match > 1){
1.338 brouard 12924: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
12925: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
1.310 brouard 12926: return 1;
1.234 brouard 12927: }
12928: }
1.334 brouard 12929: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
1.234 brouard 12930: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 12931: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.330 brouard 12932: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
12933: /* should correspond to the combination 6 of dummy: V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 1*1 + 0*2 + 1*4 = 5 + (1offset) = 6*/
12934: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.235 brouard 12935: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
12936: /* 1 0 0 0 */
12937: /* 2 1 0 0 */
12938: /* 3 0 1 0 */
1.330 brouard 12939: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
1.235 brouard 12940: /* 5 0 0 1 */
1.330 brouard 12941: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
1.235 brouard 12942: /* 7 0 1 1 */
12943: /* 8 1 1 1 */
1.237 brouard 12944: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
12945: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
12946: /* V5*age V5 known which value for nres? */
12947: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.334 brouard 12948: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* cptcovt number of covariates (excluding 1 and age or age*age) in the MODEL equation.
12949: * loop on position k1 in the MODEL LINE */
1.331 brouard 12950: /* k counting number of combination of single dummies in the equation model */
12951: /* k4 counting single dummies in the equation model */
12952: /* k4q counting single quantitatives in the equation model */
1.344 brouard 12953: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Dummy and Single, fixed or timevarying, k1 is sorting according to MODEL, but k3 to resultline */
1.334 brouard 12954: /* k4+1= (not always if quant in model) position in the resultline V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) */
1.332 brouard 12955: /* modelresult[k3]=k1: k3th position in the result line corresponds to the k1 position in the model line (doesn't work with products)*/
1.330 brouard 12956: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
1.332 brouard 12957: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
12958: /* k3 is the position in the nres result line of the k1th variable of the model equation */
12959: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
12960: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
12961: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1.334 brouard 12962: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1.332 brouard 12963: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1.330 brouard 12964: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1.332 brouard 12965: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
12966: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
12967: k2=(int)Tvarsel[k3]; /* from position k3 in resultline get name k2: nres=1 k1=2=>k3=1 Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 (V4); k1=3=>k3=2 Tvarsel[2]=3 (V3)*/
1.330 brouard 12968: k+=Tvalsel[k3]*pow(2,k4); /* nres=1 k1=2 Tvalsel[1]=1 (V4=1); k1=3 k3=2 Tvalsel[2]=0 (V3=0) */
1.334 brouard 12969: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
1.332 brouard 12970: /* Tinvresult[nres][4]=1 */
1.334 brouard 12971: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
12972: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
12973: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12974: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
1.237 brouard 12975: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.334 brouard 12976: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
1.342 brouard 12977: /* printf("Decoderesult Dummy k=%d, k1=%d precov[nres=%d][k1=%d]=%.f V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k1, nres, k1,precov[nres][k1], k2, k3, (int)Tvalsel[k3], k4); */
1.235 brouard 12978: k4++;;
1.331 brouard 12979: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
1.330 brouard 12980: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1.332 brouard 12981: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1.330 brouard 12982: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
1.332 brouard 12983: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
12984: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
12985: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
1.334 brouard 12986: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
12987: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12988: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
12989: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
12990: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
12991: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
1.237 brouard 12992: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.330 brouard 12993: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.332 brouard 12994: precov[nres][k1]=Tvalsel[k3q];
1.342 brouard 12995: /* printf("Decoderesult Quantitative nres=%d,precov[nres=%d][k1=%d]=%.f V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, nres, k1,precov[nres][k1], k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]); */
1.235 brouard 12996: k4q++;;
1.350 brouard 12997: }else if( Dummy[k1]==2 ){ /* For dummy with age product "V2+V3+V4+V6+V7+V6*V2+V7*V2+V6*V3+V7*V3+V6*V4+V7*V4+age*V2+age*V3+age*V4+age*V6+age*V7+age*V6*V2+age*V6*V3+age*V7*V3+age*V6*V4+age*V7*V4\r"*/
12998: /* Tvar[k1]; */ /* Age variable */ /* 17 age*V6*V2 ?*/
1.332 brouard 12999: /* Wrong we want the value of variable name Tvar[k1] */
1.350 brouard 13000: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
13001: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
13002: /* printf("Decoderesult Quantitative or Dummy (not with age) nres=%d k1=%d precov[nres=%d][k1=%d]=%.f V%d(=%.f) * V%d(=%.f) \n",nres, k1, nres, k1,precov[nres][k1], Tvardk[k1][1], TinvDoQresult[nres][Tvardk[k1][1]], Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][2]]); */
13003: }else{
13004: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
13005: k2=(int)Tvarsel[k3]; /* nres=1 k1=2=>k3=1 Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 (V4); k1=3=>k3=2 Tvarsel[2]=3 (V3)*/
13006: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
13007: precov[nres][k1]=Tvalsel[k3];
13008: }
1.342 brouard 13009: /* printf("Decoderesult Dummy with age k=%d, k1=%d precov[nres=%d][k1=%d]=%.f Tvar[%d]=V%d k2=Tvarsel[%d]=%d Tvalsel[%d]=%d\n",k, k1, nres, k1,precov[nres][k1], k1, Tvar[k1], k3,(int)Tvarsel[k3], k3, (int)Tvalsel[k3]); */
1.331 brouard 13010: }else if( Dummy[k1]==3 ){ /* For quant with age product */
1.350 brouard 13011: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
13012: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
13013: /* printf("Decoderesult Quantitative or Dummy (not with age) nres=%d k1=%d precov[nres=%d][k1=%d]=%.f V%d(=%.f) * V%d(=%.f) \n",nres, k1, nres, k1,precov[nres][k1], Tvardk[k1][1], TinvDoQresult[nres][Tvardk[k1][1]], Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][2]]); */
13014: }else{
13015: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
13016: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
13017: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
13018: precov[nres][k1]=Tvalsel[k3q];
13019: }
1.342 brouard 13020: /* printf("Decoderesult Quantitative with age nres=%d, k1=%d, precov[nres=%d][k1=%d]=%f Tvar[%d]=V%d V(k2q=%d)= Tvarsel[%d]=%d, Tvalsel[%d]=%f\n",nres, k1, nres, k1,precov[nres][k1], k1, Tvar[k1], k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]); */
1.349 brouard 13021: }else if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
1.332 brouard 13022: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
1.342 brouard 13023: /* printf("Decoderesult Quantitative or Dummy (not with age) nres=%d k1=%d precov[nres=%d][k1=%d]=%.f V%d(=%.f) * V%d(=%.f) \n",nres, k1, nres, k1,precov[nres][k1], Tvardk[k1][1], TinvDoQresult[nres][Tvardk[k1][1]], Tvardk[k1][2], TinvDoQresult[nres][Tvardk[k1][2]]); */
1.330 brouard 13024: }else{
1.332 brouard 13025: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
13026: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
1.235 brouard 13027: }
13028: }
1.234 brouard 13029:
1.334 brouard 13030: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
1.230 brouard 13031: return (0);
13032: }
1.235 brouard 13033:
1.230 brouard 13034: int decodemodel( char model[], int lastobs)
13035: /**< This routine decodes the model and returns:
1.224 brouard 13036: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
13037: * - nagesqr = 1 if age*age in the model, otherwise 0.
13038: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
13039: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
13040: * - cptcovage number of covariates with age*products =2
13041: * - cptcovs number of simple covariates
1.339 brouard 13042: * ncovcolt=ncovcol+nqv+ntv+nqtv total of covariates in the data, not in the model equation
1.224 brouard 13043: * - Tvar[k] is the id of the kth covariate Tvar[1]@12 {1, 2, 3, 8, 10, 11, 8, 3, 7, 8, 5, 6}, thus Tvar[5=V7*V8]=10
1.339 brouard 13044: * which is a new column after the 9 (ncovcol+nqv+ntv+nqtv) variables.
1.319 brouard 13045: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
1.224 brouard 13046: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
13047: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
13048: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
13049: */
1.319 brouard 13050: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
1.136 brouard 13051: {
1.359 brouard 13052: int i, j, k, ks;/* , v;*/
1.349 brouard 13053: int n,m;
13054: int j1, k1, k11, k12, k2, k3, k4;
13055: char modelsav[300];
13056: char stra[300], strb[300], strc[300], strd[300],stre[300],strf[300];
1.187 brouard 13057: char *strpt;
1.349 brouard 13058: int **existcomb;
13059:
13060: existcomb=imatrix(1,NCOVMAX,1,NCOVMAX);
13061: for(i=1;i<=NCOVMAX;i++)
13062: for(j=1;j<=NCOVMAX;j++)
13063: existcomb[i][j]=0;
13064:
1.145 brouard 13065: /*removespace(model);*/
1.136 brouard 13066: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.349 brouard 13067: j=0, j1=0, k1=0, k12=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 13068: if (strstr(model,"AGE") !=0){
1.192 brouard 13069: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
13070: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 13071: return 1;
13072: }
1.141 brouard 13073: if (strstr(model,"v") !=0){
1.338 brouard 13074: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
13075: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
1.141 brouard 13076: return 1;
13077: }
1.187 brouard 13078: strcpy(modelsav,model);
13079: if ((strpt=strstr(model,"age*age")) !=0){
1.338 brouard 13080: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
1.187 brouard 13081: if(strpt != model){
1.338 brouard 13082: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 13083: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 13084: corresponding column of parameters.\n",model);
1.338 brouard 13085: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 13086: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 13087: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 13088: return 1;
1.225 brouard 13089: }
1.187 brouard 13090: nagesqr=1;
13091: if (strstr(model,"+age*age") !=0)
1.234 brouard 13092: substrchaine(modelsav, model, "+age*age");
1.187 brouard 13093: else if (strstr(model,"age*age+") !=0)
1.234 brouard 13094: substrchaine(modelsav, model, "age*age+");
1.187 brouard 13095: else
1.234 brouard 13096: substrchaine(modelsav, model, "age*age");
1.187 brouard 13097: }else
13098: nagesqr=0;
1.349 brouard 13099: if (strlen(modelsav) >1){ /* V2 +V3 +V4 +V6 +V7 +V6*V2 +V7*V2 +V6*V3 +V7*V3 +V6*V4 +V7*V4 +age*V2 +age*V3 +age*V4 +age*V6 +age*V7 +age*V6*V2 +V7*V2 +age*V6*V3 +age*V7*V3 +age*V6*V4 +age*V7*V4 */
1.187 brouard 13100: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
13101: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.351 brouard 13102: cptcovs=0; /**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age => V1 + V3 =4+1-3=2 Wrong */
1.187 brouard 13103: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 13104: * cst, age and age*age
13105: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
13106: /* including age products which are counted in cptcovage.
13107: * but the covariates which are products must be treated
13108: * separately: ncovn=4- 2=2 (V1+V3). */
1.349 brouard 13109: cptcovprod=0; /**< Number of products V1*V2 +v3*age = 2 */
13110: cptcovdageprod=0; /* Number of doouble products with age age*Vn*VM or Vn*age*Vm or Vn*Vm*age */
1.187 brouard 13111: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.349 brouard 13112: cptcovprodage=0;
13113: /* cptcovprodage=nboccstr(modelsav,"age");*/
1.225 brouard 13114:
1.187 brouard 13115: /* Design
13116: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
13117: * < ncovcol=8 >
13118: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
13119: * k= 1 2 3 4 5 6 7 8
13120: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
1.345 brouard 13121: * covar[k,i], are for fixed covariates, value of kth covariate if not including age for individual i:
1.224 brouard 13122: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
13123: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 13124: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
13125: * Tage[++cptcovage]=k
1.345 brouard 13126: * if products, new covar are created after ncovcol + nqv (quanti fixed) with k1
1.187 brouard 13127: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
13128: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
13129: * Tvard[k1][1]=m Tvard[k1][2]=m; Tvard[1][1]=5 (V5) Tvard[1][2]=6 Tvard[2][1]=7 (V7) Tvard[2][2]=8
13130: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
13131: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
13132: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
1.345 brouard 13133: * < ncovcol=8 8 fixed covariate. Additional starts at 9 (V5*V6) and 10(V7*V8) >
1.187 brouard 13134: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
13135: * k= 1 2 3 4 5 6 7 8 9 10 11 12
1.345 brouard 13136: * Tvard[k]= 2 1 3 3 10 11 8 8 5 6 7 8
13137: * p Tvar[1]@12={2, 1, 3, 3, 9, 10, 8, 8}
1.187 brouard 13138: * p Tprod[1]@2={ 6, 5}
13139: *p Tvard[1][1]@4= {7, 8, 5, 6}
13140: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
13141: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1.319 brouard 13142: *How to reorganize? Tvars(orted)
1.187 brouard 13143: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
13144: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
13145: * {2, 1, 4, 8, 5, 6, 3, 7}
13146: * Struct []
13147: */
1.225 brouard 13148:
1.187 brouard 13149: /* This loop fills the array Tvar from the string 'model'.*/
13150: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
13151: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
13152: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
13153: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
13154: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
13155: /* k=1 Tvar[1]=2 (from V2) */
13156: /* k=5 Tvar[5] */
13157: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 13158: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 13159: /* } */
1.198 brouard 13160: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 13161: /*
13162: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 13163: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
13164: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
13165: }
1.187 brouard 13166: cptcovage=0;
1.351 brouard 13167:
13168: /* First loop in order to calculate */
13169: /* for age*VN*Vm
13170: * Provides, Typevar[k], Tage[cptcovage], existcomb[n][m], FixedV[ncovcolt+k12]
13171: * Tprod[k1]=k Tposprod[k]=k1; Tvard[k1][1] =m;
13172: */
13173: /* Needs FixedV[Tvardk[k][1]] */
13174: /* For others:
13175: * Sets Typevar[k];
13176: * Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13177: * Tposprod[k]=k11;
13178: * Tprod[k11]=k;
13179: * Tvardk[k][1] =m;
13180: * Needs FixedV[Tvardk[k][1]] == 0
13181: */
13182:
1.319 brouard 13183: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
13184: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
13185: modelsav==V2+V1+V5*age+V4+V3*age strb=V3*age stra=V2+V1V5*age+V4 */ /* <model> "V5+V4+V3+V4*V3+V5*age+V1*age+V1" strb="V5" stra="V4+V3+V4*V3+V5*age+V1*age+V1" */
13186: if (nbocc(modelsav,'+')==0)
13187: strcpy(strb,modelsav); /* and analyzes it */
1.234 brouard 13188: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
13189: /*scanf("%d",i);*/
1.349 brouard 13190: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V5*age+ V4+V3*age strb=V3*age OR double product with age strb=age*V6*V2 or V6*V2*age or V6*age*V2 */
13191: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 OR strb=age*V6*V2 strc=V6*V2 strd=age OR c=V2*age OR c=age*V2 */
13192: if(strchr(strc,'*')) { /**< Model with age and DOUBLE product: allowed since 0.99r44, strc=V6*V2 or V2*age or age*V2, strd=age or V6 or V6 */
13193: Typevar[k]=3; /* 3 for age and double product age*Vn*Vm varying of fixed */
13194: if(strstr(strc,"age")!=0) { /* It means that strc=V2*age or age*V2 and thus that strd=Vn */
13195: cutl(stre,strf,strc,'*') ; /* strf=age or Vm, stre=Vm or age. If strc=V6*V2 then strf=V6 and stre=V2 */
13196: strcpy(strc,strb); /* save strb(=age*Vn*Vm) into strc */
13197: /* We want strb=Vn*Vm */
13198: if(strcmp(strf,"age")==0){ /* strf is "age" so that stre=Vm =V2 . */
13199: strcpy(strb,strd);
13200: strcat(strb,"*");
13201: strcat(strb,stre);
13202: }else{ /* strf=Vm If strf=V6 then stre=V2 */
13203: strcpy(strb,strf);
13204: strcat(strb,"*");
13205: strcat(strb,stre);
13206: strcpy(strd,strb); /* in order for strd to not be "age" for next test (will be Vn*Vm */
13207: }
1.351 brouard 13208: /* printf("DEBUG FIXED k=%d, Tage[k]=%d, Tvar[Tage[k]=%d,FixedV[Tvar[Tage[k]]]=%d\n",k,Tage[k],Tvar[Tage[k]],FixedV[Tvar[Tage[k]]]); */
13209: /* FixedV[Tvar[Tage[k]]]=0; /\* HERY not sure if V7*V4*age Fixed might not exist yet*\/ */
1.349 brouard 13210: }else{ /* strc=Vn*Vm (and strd=age) and should be strb=Vn*Vm but want to keep original strb double product */
13211: strcpy(stre,strb); /* save full b in stre */
13212: strcpy(strb,strc); /* save short c in new short b for next block strb=Vn*Vm*/
13213: strcpy(strf,strc); /* save short c in new short f */
13214: cutl(strc,strd,strf,'*'); /* We get strd=Vn and strc=Vm for next block (strb=Vn*Vm)*/
13215: /* strcpy(strc,stre);*/ /* save full e in c for future */
13216: }
13217: cptcovdageprod++; /* double product with age Which product is it? */
13218: /* strcpy(strb,strc); /\* strb was age*V6*V2 or V6*V2*age or V6*age*V2 IS now V6*V2 or V2*age or age*V2 *\/ */
13219: /* cutl(strc,strd,strb,'*'); /\* strd= V6 or V2 or age and strc= V2 or age or V2 *\/ */
1.234 brouard 13220: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
1.349 brouard 13221: n=atoi(stre);
1.234 brouard 13222: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
1.349 brouard 13223: m=atoi(strc);
13224: cptcovage++; /* Counts the number of covariates which include age as a product */
13225: Tage[cptcovage]=k; /* For age*V3*V2 gives the position in model of covariates associated with age Tage[1]=6 HERY too*/
13226: if(existcomb[n][m] == 0){
13227: /* r /home/brouard/Documents/Recherches/REVES/Zachary/Zach-2022/Feinuo_Sun/Feinuo-threeway/femV12V15_3wayintNBe.imach */
13228: printf("Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
13229: fprintf(ficlog,"Warning in model combination V%d*V%d should exist in the model before adding V%d*V%d*age !\n",n,m,n,m);
13230: fflush(ficlog);
13231: k1++; /* The combination Vn*Vm will be in the model so we create it at k1 */
13232: k12++;
13233: existcomb[n][m]=k1;
13234: existcomb[m][n]=k1;
13235: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1;
13236: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2+ age*V6*V3 Gives the k position of the k1 double product Vn*Vm or age*Vn*Vm*/
13237: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 Gives the k1 double product Vn*Vm or age*Vn*Vm at the k position */
13238: Tvard[k1][1] =m; /* m 1 for V1*/
13239: Tvardk[k][1] =m; /* m 1 for V1*/
13240: Tvard[k1][2] =n; /* n 4 for V4*/
13241: Tvardk[k][2] =n; /* n 4 for V4*/
1.351 brouard 13242: /* Tvar[Tage[cptcovage]]=k1;*/ /* Tvar[6=age*V3*V2]=9 (new fixed covariate) */ /* We don't know about Fixed yet HERE */
1.349 brouard 13243: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
13244: for (i=1; i<=lastobs;i++){/* For fixed product */
13245: /* Computes the new covariate which is a product of
13246: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13247: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13248: }
13249: cptcovprodage++; /* Counting the number of fixed covariate with age */
13250: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13251: k12++;
13252: FixedV[ncovcolt+k12]=0;
13253: }else{ /*End of FixedV */
13254: cptcovprodvage++; /* Counting the number of varying covariate with age */
13255: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13256: k12++;
13257: FixedV[ncovcolt+k12]=1;
13258: }
13259: }else{ /* k1 Vn*Vm already exists */
13260: k11=existcomb[n][m];
13261: Tposprod[k]=k11; /* OK */
13262: Tvar[k]=Tvar[Tprod[k11]]; /* HERY */
13263: Tvardk[k][1]=m;
13264: Tvardk[k][2]=n;
13265: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
13266: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13267: cptcovprodage++; /* Counting the number of fixed covariate with age */
13268: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13269: Tvar[Tage[cptcovage]]=k1;
13270: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13271: k12++;
13272: FixedV[ncovcolt+k12]=0;
13273: }else{ /* Already exists but time varying (and age) */
13274: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13275: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13276: /* Tvar[Tage[cptcovage]]=k1; */
13277: cptcovprodvage++;
13278: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13279: k12++;
13280: FixedV[ncovcolt+k12]=1;
13281: }
13282: }
13283: /* Tage[cptcovage]=k; /\* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
13284: /* Tvar[k]=k11; /\* HERY *\/ */
13285: } else {/* simple product strb=age*Vn so that c=Vn and d=age, or strb=Vn*age so that c=age and d=Vn, or b=Vn*Vm so that c=Vm and d=Vn */
13286: cptcovprod++;
13287: if (strcmp(strc,"age")==0) { /**< Model includes age: strb= Vn*age c=age d=Vn*/
13288: /* covar is not filled and then is empty */
13289: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
13290: Tvar[k]=atoi(stre); /* V2+V1+V5*age+V4+V3*age Tvar[5]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
13291: Typevar[k]=1; /* 1 for age product */
13292: cptcovage++; /* Counts the number of covariates which include age as a product */
13293: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
13294: if( FixedV[Tvar[k]] == 0){
13295: cptcovprodage++; /* Counting the number of fixed covariate with age */
13296: }else{
13297: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
13298: }
13299: /*printf("stre=%s ", stre);*/
13300: } else if (strcmp(strd,"age")==0) { /* strb= age*Vn c=Vn */
13301: cutl(stre,strb,strc,'V');
13302: Tvar[k]=atoi(stre);
13303: Typevar[k]=1; /* 1 for age product */
13304: cptcovage++;
13305: Tage[cptcovage]=k;
13306: if( FixedV[Tvar[k]] == 0){
13307: cptcovprodage++; /* Counting the number of fixed covariate with age */
13308: }else{
13309: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
1.339 brouard 13310: }
1.349 brouard 13311: }else{ /* for product Vn*Vm */
13312: Typevar[k]=2; /* 2 for product Vn*Vm */
13313: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
13314: n=atoi(stre);
13315: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
13316: m=atoi(strc);
13317: k1++;
13318: cptcovprodnoage++;
13319: if(existcomb[n][m] != 0 || existcomb[m][n] != 0){
13320: printf("Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
13321: fprintf(ficlog,"Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
13322: fflush(ficlog);
13323: k11=existcomb[n][m];
13324: Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13325: Tposprod[k]=k11;
13326: Tprod[k11]=k;
13327: Tvardk[k][1] =m; /* m 1 for V1*/
13328: /* Tvard[k11][1] =m; /\* n 4 for V4*\/ */
13329: Tvardk[k][2] =n; /* n 4 for V4*/
13330: /* Tvard[k11][2] =n; /\* n 4 for V4*\/ */
13331: }else{ /* combination Vn*Vm doesn't exist we create it (no age)*/
13332: existcomb[n][m]=k1;
13333: existcomb[m][n]=k1;
13334: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* ncovcolt+k1; For model-covariate k tells which data-covariate to use but
13335: because this model-covariate is a construction we invent a new column
13336: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
13337: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
13338: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
13339: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=3 etc */
13340: /* Please remark that the new variables are model dependent */
13341: /* If we have 4 variable but the model uses only 3, like in
13342: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
13343: * k= 1 2 3 4 5 6 7 8
13344: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
13345: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
13346: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
13347: */
13348: /* We need to feed some variables like TvarVV, but later on next loop because of ncovv (k2) is not correct */
13349: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 +V6*V2*age */
13350: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
13351: Tvard[k1][1] =m; /* m 1 for V1*/
13352: Tvardk[k][1] =m; /* m 1 for V1*/
13353: Tvard[k1][2] =n; /* n 4 for V4*/
13354: Tvardk[k][2] =n; /* n 4 for V4*/
13355: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
13356: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
13357: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
13358: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
13359: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
13360: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* If the product is a fixed covariate then we feed the new column with Vn*Vm */
13361: for (i=1; i<=lastobs;i++){/* For fixed product */
13362: /* Computes the new covariate which is a product of
13363: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13364: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13365: }
13366: /* TvarVV[k2]=n; */
13367: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13368: /* TvarVV[k2+1]=m; */
13369: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13370: }else{ /* not FixedV */
13371: /* TvarVV[k2]=n; */
13372: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13373: /* TvarVV[k2+1]=m; */
13374: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13375: }
13376: } /* End of creation of Vn*Vm if not created by age*Vn*Vm earlier */
13377: } /* End of product Vn*Vm */
13378: } /* End of age*double product or simple product */
13379: }else { /* not a product */
1.234 brouard 13380: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
13381: /* scanf("%d",i);*/
13382: cutl(strd,strc,strb,'V');
13383: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
13384: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
13385: Tvar[k]=atoi(strd);
13386: Typevar[k]=0; /* 0 for simple covariates */
13387: }
13388: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 13389: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 13390: scanf("%d",i);*/
1.187 brouard 13391: } /* end of loop + on total covariates */
1.351 brouard 13392:
13393:
1.187 brouard 13394: } /* end if strlen(modelsave == 0) age*age might exist */
13395: } /* end if strlen(model == 0) */
1.349 brouard 13396: cptcovs=cptcovt - cptcovdageprod - cptcovprod;/**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age + age*v4*V3=> V1 + V3 =4+1-3=2 */
13397:
1.136 brouard 13398: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
13399: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 13400:
1.136 brouard 13401: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 13402: printf("cptcovprod=%d ", cptcovprod);
13403: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
13404: scanf("%d ",i);*/
13405:
13406:
1.230 brouard 13407: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
13408: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 13409: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
13410: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
13411: k = 1 2 3 4 5 6 7 8 9
13412: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
1.319 brouard 13413: Typevar[k]= 0 0 0 2 1 0 2 1 0
1.227 brouard 13414: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
13415: Dummy[k] 1 0 0 0 3 1 1 2 3
13416: Tmodelind[combination of covar]=k;
1.225 brouard 13417: */
13418: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 13419: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 13420: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
1.226 brouard 13421: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.318 brouard 13422: printf("Model=1+age+%s\n\
1.349 brouard 13423: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
1.227 brouard 13424: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13425: Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product\n",model);
1.318 brouard 13426: fprintf(ficlog,"Model=1+age+%s\n\
1.349 brouard 13427: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product, 3 for double product with age \n\
1.227 brouard 13428: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13429: Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product\n",model);
1.342 brouard 13430: for(k=-1;k<=NCOVMAX; k++){ Fixed[k]=0; Dummy[k]=0;}
13431: for(k=1;k<=NCOVMAX; k++){TvarFind[k]=0; TvarVind[k]=0;}
1.351 brouard 13432:
13433:
13434: /* Second loop for calculating Fixed[k], Dummy[k]*/
13435:
13436:
1.349 brouard 13437: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0,ncovva=0,ncovvta=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0, ncovvt=0;k<=cptcovt; k++){ /* or cptocvt loop on k from model */
1.234 brouard 13438: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 13439: Fixed[k]= 0;
13440: Dummy[k]= 0;
1.225 brouard 13441: ncoveff++;
1.232 brouard 13442: ncovf++;
1.234 brouard 13443: nsd++;
13444: modell[k].maintype= FTYPE;
13445: TvarsD[nsd]=Tvar[k];
13446: TvarsDind[nsd]=k;
1.330 brouard 13447: TnsdVar[Tvar[k]]=nsd;
1.234 brouard 13448: TvarF[ncovf]=Tvar[k];
13449: TvarFind[ncovf]=k;
13450: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13451: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.339 brouard 13452: /* }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /\* Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol *\/ */
1.240 brouard 13453: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */
1.227 brouard 13454: Fixed[k]= 0;
13455: Dummy[k]= 1;
1.230 brouard 13456: nqfveff++;
1.234 brouard 13457: modell[k].maintype= FTYPE;
13458: modell[k].subtype= FQ;
13459: nsq++;
1.334 brouard 13460: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
13461: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
1.232 brouard 13462: ncovf++;
1.234 brouard 13463: TvarF[ncovf]=Tvar[k];
13464: TvarFind[ncovf]=k;
1.231 brouard 13465: TvarFQ[nqfveff]=Tvar[k]-ncovcol; /* TvarFQ[1]=V2-1=1st in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.230 brouard 13466: TvarFQind[nqfveff]=k; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1.242 brouard 13467: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.339 brouard 13468: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13469: /* model V1+V3+age*V1+age*V3+V1*V3 */
13470: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13471: ncovvt++;
13472: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13473: TvarVVind[ncovvt]=k; /* TvarVVind[1]=2 (second position in the model equation */
13474:
1.227 brouard 13475: Fixed[k]= 1;
13476: Dummy[k]= 0;
1.225 brouard 13477: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 13478: modell[k].maintype= VTYPE;
13479: modell[k].subtype= VD;
13480: nsd++;
13481: TvarsD[nsd]=Tvar[k];
13482: TvarsDind[nsd]=k;
1.330 brouard 13483: TnsdVar[Tvar[k]]=nsd; /* To be verified */
1.234 brouard 13484: ncovv++; /* Only simple time varying variables */
13485: TvarV[ncovv]=Tvar[k];
1.242 brouard 13486: TvarVind[ncovv]=k; /* TvarVind[2]=2 TvarVind[3]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
1.231 brouard 13487: TvarVD[ntveff]=Tvar[k]; /* TvarVD[1]=V4 TvarVD[2]=V3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
13488: TvarVDind[ntveff]=k; /* TvarVDind[1]=2 TvarVDind[2]=3 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying dummy variable */
1.228 brouard 13489: printf("Quasi Tmodelind[%d]=%d,Tvar[Tmodelind[%d]]=V%d, ncovcol=%d, nqv=%d,Tvar[k]- ncovcol-nqv=%d\n",ntveff,k,ntveff,Tvar[k], ncovcol, nqv,Tvar[k]- ncovcol-nqv);
13490: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 13491: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.339 brouard 13492: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13493: /* model V1+V3+age*V1+age*V3+V1*V3 */
13494: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13495: ncovvt++;
13496: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13497: TvarVVind[ncovvt]=k; /* TvarVV[1]=V3 (first time varying in the model equation */
13498:
1.234 brouard 13499: Fixed[k]= 1;
13500: Dummy[k]= 1;
13501: nqtveff++;
13502: modell[k].maintype= VTYPE;
13503: modell[k].subtype= VQ;
13504: ncovv++; /* Only simple time varying variables */
13505: nsq++;
1.334 brouard 13506: TvarsQ[nsq]=Tvar[k]; /* k=1 Tvar=5 nsq=1 TvarsQ[1]=5 */ /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary here) */
13507: TvarsQind[nsq]=k; /* For single quantitative covariate gives the model position of each single quantitative covariate *//* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
1.234 brouard 13508: TvarV[ncovv]=Tvar[k];
1.242 brouard 13509: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */
1.231 brouard 13510: TvarVQ[nqtveff]=Tvar[k]; /* TvarVQ[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
13511: TvarVQind[nqtveff]=k; /* TvarVQind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple time varying quantitative variable */
1.234 brouard 13512: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
13513: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
1.349 brouard 13514: /* printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%Ad,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv); */
1.342 brouard 13515: /* printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); */
1.227 brouard 13516: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 13517: ncova++;
13518: TvarA[ncova]=Tvar[k];
13519: TvarAind[ncova]=k;
1.349 brouard 13520: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13521: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.231 brouard 13522: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 13523: Fixed[k]= 2;
13524: Dummy[k]= 2;
13525: modell[k].maintype= ATYPE;
13526: modell[k].subtype= APFD;
1.349 brouard 13527: ncovta++;
13528: TvarAVVA[ncovta]=Tvar[k]; /* (2)age*V3 */
13529: TvarAVVAind[ncovta]=k;
1.240 brouard 13530: /* ncoveff++; */
1.227 brouard 13531: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 13532: Fixed[k]= 2;
13533: Dummy[k]= 3;
13534: modell[k].maintype= ATYPE;
13535: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
1.349 brouard 13536: ncovta++;
13537: TvarAVVA[ncovta]=Tvar[k]; /* */
13538: TvarAVVAind[ncovta]=k;
1.240 brouard 13539: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 13540: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 13541: Fixed[k]= 3;
13542: Dummy[k]= 2;
13543: modell[k].maintype= ATYPE;
13544: modell[k].subtype= APVD; /* Product age * varying dummy */
1.349 brouard 13545: ncovva++;
13546: TvarVVA[ncovva]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13547: TvarVVAind[ncovva]=k;
13548: ncovta++;
13549: TvarAVVA[ncovta]=Tvar[k]; /* */
13550: TvarAVVAind[ncovta]=k;
1.240 brouard 13551: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 13552: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 13553: Fixed[k]= 3;
13554: Dummy[k]= 3;
13555: modell[k].maintype= ATYPE;
13556: modell[k].subtype= APVQ; /* Product age * varying quantitative */
1.349 brouard 13557: ncovva++;
13558: TvarVVA[ncovva]=Tvar[k]; /* */
13559: TvarVVAind[ncovva]=k;
13560: ncovta++;
13561: TvarAVVA[ncovta]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13562: TvarAVVAind[ncovta]=k;
1.240 brouard 13563: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 13564: }
1.349 brouard 13565: }else if( Tposprod[k]>0 && Typevar[k]==2){ /* Detects if fixed product no age Vm*Vn */
13566: printf("MEMORY ERRORR k=%d Tposprod[k]=%d, Typevar[k]=%d\n ",k, Tposprod[k], Typevar[k]);
13567: if(FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){ /* Needs a fixed product Product of fixed dummy (<=ncovcol) covariates For a fixed product k is higher than ncovcol V3*V2 */
13568: printf("MEMORY ERRORR k=%d Tvardk[k][1]=%d, Tvardk[k][2]=%d, FixedV[Tvardk[k][1]]=%d,FixedV[Tvardk[k][2]]=%d\n ",k,Tvardk[k][1],Tvardk[k][2],FixedV[Tvardk[k][1]],FixedV[Tvardk[k][2]]);
13569: Fixed[k]= 0;
13570: Dummy[k]= 0;
13571: ncoveff++;
13572: ncovf++;
13573: /* ncovv++; */
13574: /* TvarVV[ncovv]=Tvardk[k][1]; */
13575: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13576: /* ncovv++; */
13577: /* TvarVV[ncovv]=Tvardk[k][2]; */
13578: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13579: modell[k].maintype= FTYPE;
13580: TvarF[ncovf]=Tvar[k];
13581: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
13582: TvarFind[ncovf]=k;
13583: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13584: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13585: }else{/* product varying Vn * Vm without age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
13586: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13587: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13588: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13589: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
13590: ncovvt++;
13591: TvarVV[ncovvt]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13592: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13593: ncovvt++;
13594: TvarVV[ncovvt]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13595: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13596:
13597: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13598: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13599:
13600: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13601: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
13602: Fixed[k]= 1;
13603: Dummy[k]= 0;
13604: modell[k].maintype= FTYPE;
13605: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
13606: ncovf++; /* Fixed variables without age */
13607: TvarF[ncovf]=Tvar[k];
13608: TvarFind[ncovf]=k;
13609: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
13610: Fixed[k]= 0; /* Fixed product */
13611: Dummy[k]= 1;
13612: modell[k].maintype= FTYPE;
13613: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
13614: ncovf++; /* Varying variables without age */
13615: TvarF[ncovf]=Tvar[k];
13616: TvarFind[ncovf]=k;
13617: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
13618: Fixed[k]= 1;
13619: Dummy[k]= 0;
13620: modell[k].maintype= VTYPE;
13621: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
13622: ncovv++; /* Varying variables without age */
13623: TvarV[ncovv]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13624: TvarVind[ncovv]=k;/* TvarVind[1]=5 */
13625: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
13626: Fixed[k]= 1;
13627: Dummy[k]= 1;
13628: modell[k].maintype= VTYPE;
13629: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
13630: ncovv++; /* Varying variables without age */
13631: TvarV[ncovv]=Tvar[k];
13632: TvarVind[ncovv]=k;
13633: }
13634: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13635: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
13636: Fixed[k]= 0; /* Fixed product */
13637: Dummy[k]= 1;
13638: modell[k].maintype= FTYPE;
13639: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
13640: ncovf++; /* Fixed variables without age */
13641: TvarF[ncovf]=Tvar[k];
13642: TvarFind[ncovf]=k;
13643: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
13644: Fixed[k]= 1;
13645: Dummy[k]= 1;
13646: modell[k].maintype= VTYPE;
13647: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
13648: ncovv++; /* Varying variables without age */
13649: TvarV[ncovv]=Tvar[k];
13650: TvarVind[ncovv]=k;
13651: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
13652: Fixed[k]= 1;
13653: Dummy[k]= 1;
13654: modell[k].maintype= VTYPE;
13655: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
13656: ncovv++; /* Varying variables without age */
13657: TvarV[ncovv]=Tvar[k];
13658: TvarVind[ncovv]=k;
13659: ncovv++; /* Varying variables without age */
13660: TvarV[ncovv]=Tvar[k];
13661: TvarVind[ncovv]=k;
13662: }
13663: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
13664: if(Tvard[k1][2] <=ncovcol){
13665: Fixed[k]= 1;
13666: Dummy[k]= 1;
13667: modell[k].maintype= VTYPE;
13668: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
13669: ncovv++; /* Varying variables without age */
13670: TvarV[ncovv]=Tvar[k];
13671: TvarVind[ncovv]=k;
13672: }else if(Tvard[k1][2] <=ncovcol+nqv){
13673: Fixed[k]= 1;
13674: Dummy[k]= 1;
13675: modell[k].maintype= VTYPE;
13676: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
13677: ncovv++; /* Varying variables without age */
13678: TvarV[ncovv]=Tvar[k];
13679: TvarVind[ncovv]=k;
13680: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13681: Fixed[k]= 1;
13682: Dummy[k]= 0;
13683: modell[k].maintype= VTYPE;
13684: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
13685: ncovv++; /* Varying variables without age */
13686: TvarV[ncovv]=Tvar[k];
13687: TvarVind[ncovv]=k;
13688: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13689: Fixed[k]= 1;
13690: Dummy[k]= 1;
13691: modell[k].maintype= VTYPE;
13692: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
13693: ncovv++; /* Varying variables without age */
13694: TvarV[ncovv]=Tvar[k];
13695: TvarVind[ncovv]=k;
13696: }
13697: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
13698: if(Tvard[k1][2] <=ncovcol){
13699: Fixed[k]= 1;
13700: Dummy[k]= 1;
13701: modell[k].maintype= VTYPE;
13702: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
13703: ncovv++; /* Varying variables without age */
13704: TvarV[ncovv]=Tvar[k];
13705: TvarVind[ncovv]=k;
13706: }else if(Tvard[k1][2] <=ncovcol+nqv){
13707: Fixed[k]= 1;
13708: Dummy[k]= 1;
13709: modell[k].maintype= VTYPE;
13710: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
13711: ncovv++; /* Varying variables without age */
13712: TvarV[ncovv]=Tvar[k];
13713: TvarVind[ncovv]=k;
13714: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13715: Fixed[k]= 1;
13716: Dummy[k]= 1;
13717: modell[k].maintype= VTYPE;
13718: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
13719: ncovv++; /* Varying variables without age */
13720: TvarV[ncovv]=Tvar[k];
13721: TvarVind[ncovv]=k;
13722: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13723: Fixed[k]= 1;
13724: Dummy[k]= 1;
13725: modell[k].maintype= VTYPE;
13726: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
13727: ncovv++; /* Varying variables without age */
13728: TvarV[ncovv]=Tvar[k];
13729: TvarVind[ncovv]=k;
13730: }
13731: }else{
13732: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13733: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13734: } /*end k1*/
13735: }
13736: }else if(Typevar[k] == 3){ /* product Vn * Vm with age, V1+V3+age*V1+age*V3+V1*V3 looking at V1*V3, Typevar={0, 0, 1, 1, 2}, k=5, V1 is fixed, V3 is timevary, V5 is a product */
1.339 brouard 13737: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1.349 brouard 13738: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13739: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13740: k1=Tposprod[k]; /* Position in the products of product k, Tposprod={0, 0, 0, 0, 1, 1} k1=1 first product but second time varying because of V3 */
13741: ncova++;
13742: TvarA[ncova]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13743: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13744: ncova++;
13745: TvarA[ncova]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13746: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
1.339 brouard 13747:
1.349 brouard 13748: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13749: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13750: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){
13751: ncovta++;
13752: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13753: TvarAVVAind[ncovta]=k;
13754: ncovta++;
13755: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13756: TvarAVVAind[ncovta]=k;
13757: }else{
13758: ncovva++; /* HERY reached */
13759: TvarVVA[ncovva]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13760: TvarVVAind[ncovva]=k;
13761: ncovva++;
13762: TvarVVA[ncovva]=Tvard[k1][2]; /* */
13763: TvarVVAind[ncovva]=k;
13764: ncovta++;
13765: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13766: TvarAVVAind[ncovta]=k;
13767: ncovta++;
13768: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13769: TvarAVVAind[ncovta]=k;
13770: }
1.339 brouard 13771: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13772: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
1.349 brouard 13773: Fixed[k]= 2;
13774: Dummy[k]= 2;
1.240 brouard 13775: modell[k].maintype= FTYPE;
13776: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
1.349 brouard 13777: /* TvarF[ncova]=Tvar[k]; /\* Problem to solve *\/ */
13778: /* TvarFind[ncova]=k; */
1.339 brouard 13779: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
1.349 brouard 13780: Fixed[k]= 2; /* Fixed product */
13781: Dummy[k]= 3;
1.240 brouard 13782: modell[k].maintype= FTYPE;
13783: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
1.349 brouard 13784: /* TvarF[ncova]=Tvar[k]; */
13785: /* TvarFind[ncova]=k; */
1.339 brouard 13786: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
1.349 brouard 13787: Fixed[k]= 3;
13788: Dummy[k]= 2;
1.240 brouard 13789: modell[k].maintype= VTYPE;
13790: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
1.349 brouard 13791: TvarV[ncova]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13792: TvarVind[ncova]=k;/* TvarVind[1]=5 */
1.339 brouard 13793: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
1.349 brouard 13794: Fixed[k]= 3;
13795: Dummy[k]= 3;
1.240 brouard 13796: modell[k].maintype= VTYPE;
13797: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
1.349 brouard 13798: /* ncovv++; /\* Varying variables without age *\/ */
13799: /* TvarV[ncovv]=Tvar[k]; */
13800: /* TvarVind[ncovv]=k; */
1.240 brouard 13801: }
1.339 brouard 13802: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13803: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
1.349 brouard 13804: Fixed[k]= 2; /* Fixed product */
13805: Dummy[k]= 2;
1.240 brouard 13806: modell[k].maintype= FTYPE;
13807: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
1.349 brouard 13808: /* ncova++; /\* Fixed variables with age *\/ */
13809: /* TvarF[ncovf]=Tvar[k]; */
13810: /* TvarFind[ncovf]=k; */
1.339 brouard 13811: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
1.349 brouard 13812: Fixed[k]= 2;
13813: Dummy[k]= 3;
1.240 brouard 13814: modell[k].maintype= VTYPE;
13815: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
1.349 brouard 13816: /* ncova++; /\* Varying variables with age *\/ */
13817: /* TvarV[ncova]=Tvar[k]; */
13818: /* TvarVind[ncova]=k; */
1.339 brouard 13819: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
1.349 brouard 13820: Fixed[k]= 3;
13821: Dummy[k]= 2;
1.240 brouard 13822: modell[k].maintype= VTYPE;
13823: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
1.349 brouard 13824: ncova++; /* Varying variables without age */
13825: TvarV[ncova]=Tvar[k];
13826: TvarVind[ncova]=k;
13827: /* ncova++; /\* Varying variables without age *\/ */
13828: /* TvarV[ncova]=Tvar[k]; */
13829: /* TvarVind[ncova]=k; */
1.240 brouard 13830: }
1.339 brouard 13831: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
1.240 brouard 13832: if(Tvard[k1][2] <=ncovcol){
1.349 brouard 13833: Fixed[k]= 2;
13834: Dummy[k]= 2;
1.240 brouard 13835: modell[k].maintype= VTYPE;
13836: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
1.349 brouard 13837: /* ncova++; /\* Varying variables with age *\/ */
13838: /* TvarV[ncova]=Tvar[k]; */
13839: /* TvarVind[ncova]=k; */
1.240 brouard 13840: }else if(Tvard[k1][2] <=ncovcol+nqv){
1.349 brouard 13841: Fixed[k]= 2;
13842: Dummy[k]= 3;
1.240 brouard 13843: modell[k].maintype= VTYPE;
13844: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
1.349 brouard 13845: /* ncova++; /\* Varying variables with age *\/ */
13846: /* TvarV[ncova]=Tvar[k]; */
13847: /* TvarVind[ncova]=k; */
1.240 brouard 13848: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
1.349 brouard 13849: Fixed[k]= 3;
13850: Dummy[k]= 2;
1.240 brouard 13851: modell[k].maintype= VTYPE;
13852: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
1.349 brouard 13853: /* ncova++; /\* Varying variables with age *\/ */
13854: /* TvarV[ncova]=Tvar[k]; */
13855: /* TvarVind[ncova]=k; */
1.240 brouard 13856: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
1.349 brouard 13857: Fixed[k]= 3;
13858: Dummy[k]= 3;
1.240 brouard 13859: modell[k].maintype= VTYPE;
13860: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
1.349 brouard 13861: /* ncova++; /\* Varying variables with age *\/ */
13862: /* TvarV[ncova]=Tvar[k]; */
13863: /* TvarVind[ncova]=k; */
1.240 brouard 13864: }
1.339 brouard 13865: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
1.240 brouard 13866: if(Tvard[k1][2] <=ncovcol){
1.349 brouard 13867: Fixed[k]= 2;
13868: Dummy[k]= 2;
1.240 brouard 13869: modell[k].maintype= VTYPE;
13870: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
1.349 brouard 13871: /* ncova++; /\* Varying variables with age *\/ */
13872: /* TvarV[ncova]=Tvar[k]; */
13873: /* TvarVind[ncova]=k; */
1.240 brouard 13874: }else if(Tvard[k1][2] <=ncovcol+nqv){
1.349 brouard 13875: Fixed[k]= 2;
13876: Dummy[k]= 3;
1.240 brouard 13877: modell[k].maintype= VTYPE;
13878: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
1.349 brouard 13879: /* ncova++; /\* Varying variables with age *\/ */
13880: /* TvarV[ncova]=Tvar[k]; */
13881: /* TvarVind[ncova]=k; */
1.240 brouard 13882: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
1.349 brouard 13883: Fixed[k]= 3;
13884: Dummy[k]= 2;
1.240 brouard 13885: modell[k].maintype= VTYPE;
13886: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
1.349 brouard 13887: /* ncova++; /\* Varying variables with age *\/ */
13888: /* TvarV[ncova]=Tvar[k]; */
13889: /* TvarVind[ncova]=k; */
1.240 brouard 13890: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
1.349 brouard 13891: Fixed[k]= 3;
13892: Dummy[k]= 3;
1.240 brouard 13893: modell[k].maintype= VTYPE;
13894: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
1.349 brouard 13895: /* ncova++; /\* Varying variables with age *\/ */
13896: /* TvarV[ncova]=Tvar[k]; */
13897: /* TvarVind[ncova]=k; */
1.240 brouard 13898: }
1.227 brouard 13899: }else{
1.240 brouard 13900: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13901: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13902: } /*end k1*/
1.349 brouard 13903: } else{
1.226 brouard 13904: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
13905: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
1.225 brouard 13906: }
1.342 brouard 13907: /* printf("Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[k],Dummy[k]); */
13908: /* printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); */
1.227 brouard 13909: fprintf(ficlog,"Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[k],Dummy[k]);
13910: }
1.349 brouard 13911: ncovvta=ncovva;
1.227 brouard 13912: /* Searching for doublons in the model */
13913: for(k1=1; k1<= cptcovt;k1++){
13914: for(k2=1; k2 <k1;k2++){
1.285 brouard 13915: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
13916: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
1.234 brouard 13917: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
13918: if(Tvar[k1]==Tvar[k2]){
1.338 brouard 13919: printf("Error duplication in the model=1+age+%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[k1],Dummy[k1]);
13920: fprintf(ficlog,"Error duplication in the model=1+age+%s at positions (+) %d and %d, Tvar[%d]=V%d, Tvar[%d]=V%d, Typevar=%d, Fixed=%d, Dummy=%d\n", model, k1,k2, k1, Tvar[k1], k2, Tvar[k2],Typevar[k1],Fixed[k1],Dummy[k1]); fflush(ficlog);
1.234 brouard 13921: return(1);
13922: }
13923: }else if (Typevar[k1] ==2){
13924: k3=Tposprod[k1];
13925: k4=Tposprod[k2];
13926: if( ((Tvard[k3][1]== Tvard[k4][1])&&(Tvard[k3][2]== Tvard[k4][2])) || ((Tvard[k3][1]== Tvard[k4][2])&&(Tvard[k3][2]== Tvard[k4][1])) ){
1.338 brouard 13927: printf("Error duplication in the model=1+age+%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]);
13928: fprintf(ficlog,"Error duplication in the model=1+age+%s at positions (+) %d and %d, V%d*V%d, Typevar=%d, Fixed=%d, Dummy=%d\n",model, k1,k2, Tvard[k3][1], Tvard[k3][2],Typevar[k1],Fixed[Tvar[k1]],Dummy[Tvar[k1]]); fflush(ficlog);
1.234 brouard 13929: return(1);
13930: }
13931: }
1.227 brouard 13932: }
13933: }
1.225 brouard 13934: }
13935: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
13936: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 13937: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
13938: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.349 brouard 13939:
13940: free_imatrix(existcomb,1,NCOVMAX,1,NCOVMAX);
1.137 brouard 13941: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 13942: /*endread:*/
1.225 brouard 13943: printf("Exiting decodemodel: ");
13944: return (1);
1.136 brouard 13945: }
13946:
1.169 brouard 13947: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.248 brouard 13948: {/* Check ages at death */
1.136 brouard 13949: int i, m;
1.218 brouard 13950: int firstone=0;
13951:
1.136 brouard 13952: for (i=1; i<=imx; i++) {
13953: for(m=2; (m<= maxwav); m++) {
13954: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
13955: anint[m][i]=9999;
1.216 brouard 13956: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
13957: s[m][i]=-1;
1.136 brouard 13958: }
13959: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.260 brouard 13960: *nberr = *nberr + 1;
1.218 brouard 13961: if(firstone == 0){
13962: firstone=1;
1.260 brouard 13963: printf("Warning (#%d)! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown but status is a death state %d at wave %d. If you don't know the vital status, please enter -2. If he/she is still alive but don't know the state, please code with '-1 or '.'. Here, we do not believe in a death, skipped.\nOther similar cases in log file\n", *nberr,(int)moisdc[i],(int)andc[i],num[i],i,s[m][i],m);
1.218 brouard 13964: }
1.262 brouard 13965: fprintf(ficlog,"Warning (#%d)! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown but status is a death state %d at wave %d. If you don't know the vital status, please enter -2. If he/she is still alive but don't know the state, please code with '-1 or '.'. Here, we do not believe in a death, skipped.\n", *nberr,(int)moisdc[i],(int)andc[i],num[i],i,s[m][i],m);
1.260 brouard 13966: s[m][i]=-1; /* Droping the death status */
1.136 brouard 13967: }
13968: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 13969: (*nberr)++;
1.259 brouard 13970: printf("Error (#%d)! Month of death of individual %ld on line %d was unknown (%2d) (year of death is %4d) and status is a death state %d at wave %d. Please impute an arbitrary (or not) month and rerun. Currently this transition to death will be skipped (status is set to -2).\nOther similar cases in log file\n", *nberr, num[i],i,(int)moisdc[i],(int)andc[i],s[m][i],m);
1.262 brouard 13971: fprintf(ficlog,"Error (#%d)! Month of death of individual %ld on line %d was unknown (%2d) (year of death is %4d) and status is a death state %d at wave %d. Please impute an arbitrary (or not) month and rerun. Currently this transition to death will be skipped (status is set to -2).\n", *nberr, num[i],i,(int)moisdc[i],(int)andc[i],s[m][i],m);
1.259 brouard 13972: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.136 brouard 13973: }
13974: }
13975: }
13976:
13977: for (i=1; i<=imx; i++) {
13978: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
13979: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 13980: if(s[m][i] >0 || s[m][i]==-1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){ /* What if s[m][i]=-1 */
1.136 brouard 13981: if (s[m][i] >= nlstate+1) {
1.169 brouard 13982: if(agedc[i]>0){
13983: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 13984: agev[m][i]=agedc[i];
1.214 brouard 13985: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 13986: }else {
1.136 brouard 13987: if ((int)andc[i]!=9999){
13988: nbwarn++;
13989: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
13990: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
13991: agev[m][i]=-1;
13992: }
13993: }
1.169 brouard 13994: } /* agedc > 0 */
1.214 brouard 13995: } /* end if */
1.136 brouard 13996: else if(s[m][i] !=9){ /* Standard case, age in fractional
13997: years but with the precision of a month */
13998: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
13999: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
14000: agev[m][i]=1;
14001: else if(agev[m][i] < *agemin){
14002: *agemin=agev[m][i];
14003: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
14004: }
14005: else if(agev[m][i] >*agemax){
14006: *agemax=agev[m][i];
1.156 brouard 14007: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 14008: }
14009: /*agev[m][i]=anint[m][i]-annais[i];*/
14010: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 14011: } /* en if 9*/
1.136 brouard 14012: else { /* =9 */
1.214 brouard 14013: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 14014: agev[m][i]=1;
14015: s[m][i]=-1;
14016: }
14017: }
1.214 brouard 14018: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 14019: agev[m][i]=1;
1.214 brouard 14020: else{
14021: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
14022: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
14023: agev[m][i]=0;
14024: }
14025: } /* End for lastpass */
14026: }
1.136 brouard 14027:
14028: for (i=1; i<=imx; i++) {
14029: for(m=firstpass; (m<=lastpass); m++){
14030: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 14031: (*nberr)++;
1.136 brouard 14032: printf("Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);
14033: fprintf(ficlog,"Error: on wave %d of individual %d status %d > (nlstate+ndeath)=(%d+%d)=%d\n",m,i,s[m][i],nlstate, ndeath, nlstate+ndeath);
14034: return 1;
14035: }
14036: }
14037: }
14038:
14039: /*for (i=1; i<=imx; i++){
14040: for (m=firstpass; (m<lastpass); m++){
14041: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
14042: }
14043:
14044: }*/
14045:
14046:
1.139 brouard 14047: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
14048: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 14049:
14050: return (0);
1.164 brouard 14051: /* endread:*/
1.136 brouard 14052: printf("Exiting calandcheckages: ");
14053: return (1);
14054: }
14055:
1.172 brouard 14056: #if defined(_MSC_VER)
14057: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14058: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
14059: //#include "stdafx.h"
14060: //#include <stdio.h>
14061: //#include <tchar.h>
14062: //#include <windows.h>
14063: //#include <iostream>
14064: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
14065:
14066: LPFN_ISWOW64PROCESS fnIsWow64Process;
14067:
14068: BOOL IsWow64()
14069: {
14070: BOOL bIsWow64 = FALSE;
14071:
14072: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
14073: // (HANDLE, PBOOL);
14074:
14075: //LPFN_ISWOW64PROCESS fnIsWow64Process;
14076:
14077: HMODULE module = GetModuleHandle(_T("kernel32"));
14078: const char funcName[] = "IsWow64Process";
14079: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
14080: GetProcAddress(module, funcName);
14081:
14082: if (NULL != fnIsWow64Process)
14083: {
14084: if (!fnIsWow64Process(GetCurrentProcess(),
14085: &bIsWow64))
14086: //throw std::exception("Unknown error");
14087: printf("Unknown error\n");
14088: }
14089: return bIsWow64 != FALSE;
14090: }
14091: #endif
1.177 brouard 14092:
1.191 brouard 14093: void syscompilerinfo(int logged)
1.292 brouard 14094: {
14095: #include <stdint.h>
14096:
14097: /* #include "syscompilerinfo.h"*/
1.185 brouard 14098: /* command line Intel compiler 32bit windows, XP compatible:*/
14099: /* /GS /W3 /Gy
14100: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
14101: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
14102: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 14103: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
14104: */
14105: /* 64 bits */
1.185 brouard 14106: /*
14107: /GS /W3 /Gy
14108: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
14109: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
14110: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
14111: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
14112: /* Optimization are useless and O3 is slower than O2 */
14113: /*
14114: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
14115: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
14116: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
14117: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
14118: */
1.186 brouard 14119: /* Link is */ /* /OUT:"visual studio
1.185 brouard 14120: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
14121: /PDB:"visual studio
14122: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
14123: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
14124: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
14125: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
14126: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
14127: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
14128: uiAccess='false'"
14129: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
14130: /NOLOGO /TLBID:1
14131: */
1.292 brouard 14132:
14133:
1.177 brouard 14134: #if defined __INTEL_COMPILER
1.178 brouard 14135: #if defined(__GNUC__)
14136: struct utsname sysInfo; /* For Intel on Linux and OS/X */
14137: #endif
1.177 brouard 14138: #elif defined(__GNUC__)
1.179 brouard 14139: #ifndef __APPLE__
1.174 brouard 14140: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 14141: #endif
1.177 brouard 14142: struct utsname sysInfo;
1.178 brouard 14143: int cross = CROSS;
14144: if (cross){
14145: printf("Cross-");
1.191 brouard 14146: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 14147: }
1.174 brouard 14148: #endif
14149:
1.191 brouard 14150: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 14151: #if defined(__clang__)
1.191 brouard 14152: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 14153: #endif
14154: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 14155: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 14156: #endif
14157: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 14158: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 14159: #endif
14160: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 14161: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 14162: #endif
14163: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 14164: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 14165: #endif
14166: #if defined(_MSC_VER)
1.191 brouard 14167: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 14168: #endif
14169: #if defined(__PGI)
1.191 brouard 14170: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 14171: #endif
14172: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 14173: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 14174: #endif
1.191 brouard 14175: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 14176:
1.167 brouard 14177: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
14178: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
14179: // Windows (x64 and x86)
1.191 brouard 14180: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 14181: #elif __unix__ // all unices, not all compilers
14182: // Unix
1.191 brouard 14183: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 14184: #elif __linux__
14185: // linux
1.191 brouard 14186: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 14187: #elif __APPLE__
1.174 brouard 14188: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 14189: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 14190: #endif
14191:
14192: /* __MINGW32__ */
14193: /* __CYGWIN__ */
14194: /* __MINGW64__ */
14195: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
14196: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
14197: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
14198: /* _WIN64 // Defined for applications for Win64. */
14199: /* _M_X64 // Defined for compilations that target x64 processors. */
14200: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 14201:
1.167 brouard 14202: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 14203: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 14204: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 14205: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 14206: #else
1.191 brouard 14207: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 14208: #endif
14209:
1.169 brouard 14210: #if defined(__GNUC__)
14211: # if defined(__GNUC_PATCHLEVEL__)
14212: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14213: + __GNUC_MINOR__ * 100 \
14214: + __GNUC_PATCHLEVEL__)
14215: # else
14216: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14217: + __GNUC_MINOR__ * 100)
14218: # endif
1.174 brouard 14219: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 14220: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 14221:
14222: if (uname(&sysInfo) != -1) {
14223: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 14224: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.176 brouard 14225: }
14226: else
14227: perror("uname() error");
1.179 brouard 14228: //#ifndef __INTEL_COMPILER
14229: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 14230: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 14231: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 14232: #endif
1.169 brouard 14233: #endif
1.172 brouard 14234:
1.286 brouard 14235: // void main ()
1.172 brouard 14236: // {
1.169 brouard 14237: #if defined(_MSC_VER)
1.174 brouard 14238: if (IsWow64()){
1.191 brouard 14239: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
14240: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 14241: }
14242: else{
1.191 brouard 14243: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
14244: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 14245: }
1.172 brouard 14246: // printf("\nPress Enter to continue...");
14247: // getchar();
14248: // }
14249:
1.169 brouard 14250: #endif
14251:
1.167 brouard 14252:
1.219 brouard 14253: }
1.136 brouard 14254:
1.219 brouard 14255: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.288 brouard 14256: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
1.332 brouard 14257: /* Computes the prevalence limit for each combination of the dummy covariates */
1.235 brouard 14258: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 14259: /* double ftolpl = 1.e-10; */
1.180 brouard 14260: double age, agebase, agelim;
1.203 brouard 14261: double tot;
1.180 brouard 14262:
1.202 brouard 14263: strcpy(filerespl,"PL_");
14264: strcat(filerespl,fileresu);
14265: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.288 brouard 14266: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
14267: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
1.202 brouard 14268: }
1.288 brouard 14269: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
14270: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 14271: pstamp(ficrespl);
1.288 brouard 14272: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 14273: fprintf(ficrespl,"#Age ");
14274: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
14275: fprintf(ficrespl,"\n");
1.180 brouard 14276:
1.219 brouard 14277: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 14278:
1.219 brouard 14279: agebase=ageminpar;
14280: agelim=agemaxpar;
1.180 brouard 14281:
1.227 brouard 14282: /* i1=pow(2,ncoveff); */
1.234 brouard 14283: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 14284: if (cptcovn < 1){i1=1;}
1.180 brouard 14285:
1.337 brouard 14286: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
1.238 brouard 14287: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 14288: k=TKresult[nres];
1.338 brouard 14289: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14290: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
14291: /* continue; */
1.235 brouard 14292:
1.238 brouard 14293: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14294: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
14295: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
14296: /* k=k+1; */
14297: /* to clean */
1.332 brouard 14298: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
1.238 brouard 14299: fprintf(ficrespl,"#******");
14300: printf("#******");
14301: fprintf(ficlog,"#******");
1.337 brouard 14302: for(j=1;j<=cptcovs ;j++) {/**< cptcovs number of SIMPLE covariates in the model or resultline V2+V1 =2 (dummy or quantit or time varying) */
1.332 brouard 14303: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
1.337 brouard 14304: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14305: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14306: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14307: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14308: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14309: }
14310: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14311: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14312: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14313: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14314: /* } */
1.238 brouard 14315: fprintf(ficrespl,"******\n");
14316: printf("******\n");
14317: fprintf(ficlog,"******\n");
14318: if(invalidvarcomb[k]){
14319: printf("\nCombination (%d) ignored because no case \n",k);
14320: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
14321: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
14322: continue;
14323: }
1.219 brouard 14324:
1.238 brouard 14325: fprintf(ficrespl,"#Age ");
1.337 brouard 14326: /* for(j=1;j<=cptcoveff;j++) { */
14327: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14328: /* } */
14329: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
14330: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14331: }
14332: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
14333: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 14334:
1.238 brouard 14335: for (age=agebase; age<=agelim; age++){
14336: /* for (age=agebase; age<=agebase; age++){ */
1.337 brouard 14337: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
14338: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
1.238 brouard 14339: fprintf(ficrespl,"%.0f ",age );
1.337 brouard 14340: /* for(j=1;j<=cptcoveff;j++) */
14341: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14342: for(j=1;j<=cptcovs;j++)
14343: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14344: tot=0.;
14345: for(i=1; i<=nlstate;i++){
14346: tot += prlim[i][i];
14347: fprintf(ficrespl," %.5f", prlim[i][i]);
14348: }
14349: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
14350: } /* Age */
14351: /* was end of cptcod */
1.337 brouard 14352: } /* nres */
14353: /* } /\* for each combination *\/ */
1.219 brouard 14354: return 0;
1.180 brouard 14355: }
14356:
1.218 brouard 14357: int back_prevalence_limit(double *p, double **bprlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp, double dateprev1,double dateprev2, int firstpass, int lastpass, int mobilavproj){
1.288 brouard 14358: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
1.218 brouard 14359:
14360: /* Computes the back prevalence limit for any combination of covariate values
14361: * at any age between ageminpar and agemaxpar
14362: */
1.235 brouard 14363: int i, j, k, i1, nres=0 ;
1.217 brouard 14364: /* double ftolpl = 1.e-10; */
14365: double age, agebase, agelim;
14366: double tot;
1.218 brouard 14367: /* double ***mobaverage; */
14368: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 14369:
14370: strcpy(fileresplb,"PLB_");
14371: strcat(fileresplb,fileresu);
14372: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
1.288 brouard 14373: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
14374: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
1.217 brouard 14375: }
1.288 brouard 14376: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
14377: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
1.217 brouard 14378: pstamp(ficresplb);
1.288 brouard 14379: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.217 brouard 14380: fprintf(ficresplb,"#Age ");
14381: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
14382: fprintf(ficresplb,"\n");
14383:
1.218 brouard 14384:
14385: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
14386:
14387: agebase=ageminpar;
14388: agelim=agemaxpar;
14389:
14390:
1.227 brouard 14391: i1=pow(2,cptcoveff);
1.218 brouard 14392: if (cptcovn < 1){i1=1;}
1.227 brouard 14393:
1.238 brouard 14394: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.338 brouard 14395: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14396: k=TKresult[nres];
14397: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
14398: /* if(i1 != 1 && TKresult[nres]!= k) */
14399: /* continue; */
14400: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
1.238 brouard 14401: fprintf(ficresplb,"#******");
14402: printf("#******");
14403: fprintf(ficlog,"#******");
1.338 brouard 14404: for(j=1;j<=cptcovs ;j++) {/**< cptcovs number of SIMPLE covariates in the model or resultline V2+V1 =2 (dummy or quantit or time varying) */
14405: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14406: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14407: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14408: }
1.338 brouard 14409: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
14410: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14411: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14412: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14413: /* } */
14414: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14415: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14416: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14417: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14418: /* } */
1.238 brouard 14419: fprintf(ficresplb,"******\n");
14420: printf("******\n");
14421: fprintf(ficlog,"******\n");
14422: if(invalidvarcomb[k]){
14423: printf("\nCombination (%d) ignored because no cases \n",k);
14424: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
14425: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
14426: continue;
14427: }
1.218 brouard 14428:
1.238 brouard 14429: fprintf(ficresplb,"#Age ");
1.338 brouard 14430: for(j=1;j<=cptcovs;j++) {
14431: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14432: }
14433: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
14434: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 14435:
14436:
1.238 brouard 14437: for (age=agebase; age<=agelim; age++){
14438: /* for (age=agebase; age<=agebase; age++){ */
14439: if(mobilavproj > 0){
14440: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
14441: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 14442: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 14443: }else if (mobilavproj == 0){
14444: printf("There is no chance to get back prevalence limit if data aren't non zero and summing to 1, please try a non null mobil_average(=%d) parameter or mobil_average=-1 if you want to try at your own risk.\n",mobilavproj);
14445: fprintf(ficlog,"There is no chance to get back prevalence limit if data aren't non zero and summing to 1, please try a non null mobil_average(=%d) parameter or mobil_average=-1 if you want to try at your own risk.\n",mobilavproj);
14446: exit(1);
14447: }else{
14448: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 14449: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.266 brouard 14450: /* printf("TOTOT\n"); */
14451: /* exit(1); */
1.238 brouard 14452: }
14453: fprintf(ficresplb,"%.0f ",age );
1.338 brouard 14454: for(j=1;j<=cptcovs;j++)
14455: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14456: tot=0.;
14457: for(i=1; i<=nlstate;i++){
14458: tot += bprlim[i][i];
14459: fprintf(ficresplb," %.5f", bprlim[i][i]);
14460: }
14461: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
14462: } /* Age */
14463: /* was end of cptcod */
1.255 brouard 14464: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
1.338 brouard 14465: /* } /\* end of any combination *\/ */
1.238 brouard 14466: } /* end of nres */
1.218 brouard 14467: /* hBijx(p, bage, fage); */
14468: /* fclose(ficrespijb); */
14469:
14470: return 0;
1.217 brouard 14471: }
1.218 brouard 14472:
1.180 brouard 14473: int hPijx(double *p, int bage, int fage){
14474: /*------------- h Pij x at various ages ------------*/
1.336 brouard 14475: /* to be optimized with precov */
1.180 brouard 14476: int stepsize;
14477: int agelim;
14478: int hstepm;
14479: int nhstepm;
1.359 brouard 14480: int h, i, i1, j, k, nres=0;
1.180 brouard 14481:
14482: double agedeb;
14483: double ***p3mat;
14484:
1.337 brouard 14485: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
14486: if((ficrespij=fopen(filerespij,"w"))==NULL) {
14487: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
14488: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
14489: }
14490: printf("Computing pij: result on file '%s' \n", filerespij);
14491: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
14492:
14493: stepsize=(int) (stepm+YEARM-1)/YEARM;
14494: /*if (stepm<=24) stepsize=2;*/
14495:
14496: agelim=AGESUP;
14497: hstepm=stepsize*YEARM; /* Every year of age */
14498: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14499:
14500: /* hstepm=1; aff par mois*/
14501: pstamp(ficrespij);
14502: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
14503: i1= pow(2,cptcoveff);
14504: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14505: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14506: /* k=k+1; */
14507: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
14508: k=TKresult[nres];
1.338 brouard 14509: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14510: /* for(k=1; k<=i1;k++){ */
14511: /* if(i1 != 1 && TKresult[nres]!= k) */
14512: /* continue; */
14513: fprintf(ficrespij,"\n#****** ");
14514: for(j=1;j<=cptcovs;j++){
14515: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14516: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14517: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14518: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14519: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14520: }
14521: fprintf(ficrespij,"******\n");
14522:
14523: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
14524: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
14525: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
14526:
14527: /* nhstepm=nhstepm*YEARM; aff par mois*/
14528:
14529: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14530: oldm=oldms;savm=savms;
14531: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
14532: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
14533: for(i=1; i<=nlstate;i++)
14534: for(j=1; j<=nlstate+ndeath;j++)
14535: fprintf(ficrespij," %1d-%1d",i,j);
14536: fprintf(ficrespij,"\n");
14537: for (h=0; h<=nhstepm; h++){
14538: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14539: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.183 brouard 14540: for(i=1; i<=nlstate;i++)
14541: for(j=1; j<=nlstate+ndeath;j++)
1.337 brouard 14542: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.183 brouard 14543: fprintf(ficrespij,"\n");
14544: }
1.337 brouard 14545: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14546: fprintf(ficrespij,"\n");
1.180 brouard 14547: }
1.337 brouard 14548: }
14549: /*}*/
14550: return 0;
1.180 brouard 14551: }
1.218 brouard 14552:
14553: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 14554: /*------------- h Bij x at various ages ------------*/
1.336 brouard 14555: /* To be optimized with precov */
1.217 brouard 14556: int stepsize;
1.218 brouard 14557: /* int agelim; */
14558: int ageminl;
1.217 brouard 14559: int hstepm;
14560: int nhstepm;
1.238 brouard 14561: int h, i, i1, j, k, nres;
1.218 brouard 14562:
1.217 brouard 14563: double agedeb;
14564: double ***p3mat;
1.218 brouard 14565:
14566: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
14567: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
14568: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14569: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14570: }
14571: printf("Computing pij back: result on file '%s' \n", filerespijb);
14572: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
14573:
14574: stepsize=(int) (stepm+YEARM-1)/YEARM;
14575: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 14576:
1.218 brouard 14577: /* agelim=AGESUP; */
1.289 brouard 14578: ageminl=AGEINF; /* was 30 */
1.218 brouard 14579: hstepm=stepsize*YEARM; /* Every year of age */
14580: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14581:
14582: /* hstepm=1; aff par mois*/
14583: pstamp(ficrespijb);
1.255 brouard 14584: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 ");
1.227 brouard 14585: i1= pow(2,cptcoveff);
1.218 brouard 14586: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14587: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14588: /* k=k+1; */
1.238 brouard 14589: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 14590: k=TKresult[nres];
1.338 brouard 14591: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14592: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14593: /* if(i1 != 1 && TKresult[nres]!= k) */
14594: /* continue; */
14595: fprintf(ficrespijb,"\n#****** ");
14596: for(j=1;j<=cptcovs;j++){
1.338 brouard 14597: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.337 brouard 14598: /* for(j=1;j<=cptcoveff;j++) */
14599: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14600: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14601: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14602: }
14603: fprintf(ficrespijb,"******\n");
14604: if(invalidvarcomb[k]){ /* Is it necessary here? */
14605: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
14606: continue;
14607: }
14608:
14609: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
14610: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
14611: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
14612: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm+0.1)-1; /* Typically 20 years = 20*12/6=40 or 55*12/24=27.5-1.1=>27 */
14613: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
14614:
14615: /* nhstepm=nhstepm*YEARM; aff par mois*/
14616:
14617: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
14618: /* and memory limitations if stepm is small */
14619:
14620: /* oldm=oldms;savm=savms; */
14621: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
14622: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
14623: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
14624: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
14625: for(i=1; i<=nlstate;i++)
14626: for(j=1; j<=nlstate+ndeath;j++)
14627: fprintf(ficrespijb," %1d-%1d",i,j);
14628: fprintf(ficrespijb,"\n");
14629: for (h=0; h<=nhstepm; h++){
14630: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14631: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
14632: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 14633: for(i=1; i<=nlstate;i++)
14634: for(j=1; j<=nlstate+ndeath;j++)
1.337 brouard 14635: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
1.217 brouard 14636: fprintf(ficrespijb,"\n");
1.337 brouard 14637: }
14638: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14639: fprintf(ficrespijb,"\n");
14640: } /* end age deb */
14641: /* } /\* end combination *\/ */
1.238 brouard 14642: } /* end nres */
1.218 brouard 14643: return 0;
14644: } /* hBijx */
1.217 brouard 14645:
1.180 brouard 14646:
1.136 brouard 14647: /***********************************************/
14648: /**************** Main Program *****************/
14649: /***********************************************/
14650:
14651: int main(int argc, char *argv[])
14652: {
14653: #ifdef GSL
14654: const gsl_multimin_fminimizer_type *T;
14655: size_t iteri = 0, it;
14656: int rval = GSL_CONTINUE;
14657: int status = GSL_SUCCESS;
14658: double ssval;
14659: #endif
14660: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.290 brouard 14661: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
14662: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
1.209 brouard 14663: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 14664: int jj, ll, li, lj, lk;
1.136 brouard 14665: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 14666: int num_filled;
1.136 brouard 14667: int itimes;
14668: int NDIM=2;
14669: int vpopbased=0;
1.235 brouard 14670: int nres=0;
1.258 brouard 14671: int endishere=0;
1.277 brouard 14672: int noffset=0;
1.274 brouard 14673: int ncurrv=0; /* Temporary variable */
14674:
1.164 brouard 14675: char ca[32], cb[32];
1.136 brouard 14676: /* FILE *fichtm; *//* Html File */
14677: /* FILE *ficgp;*/ /*Gnuplot File */
14678: struct stat info;
1.191 brouard 14679: double agedeb=0.;
1.194 brouard 14680:
14681: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 14682: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 14683:
1.361 brouard 14684: double stdpercent; /* for computing the std error of percent e.i: e.i/e.. */
1.165 brouard 14685: double fret;
1.191 brouard 14686: double dum=0.; /* Dummy variable */
1.359 brouard 14687: /* double*** p3mat;*/
1.218 brouard 14688: /* double ***mobaverage; */
1.319 brouard 14689: double wald;
1.164 brouard 14690:
1.351 brouard 14691: char line[MAXLINE], linetmp[MAXLINE];
1.197 brouard 14692: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
14693:
1.234 brouard 14694: char modeltemp[MAXLINE];
1.332 brouard 14695: char resultline[MAXLINE], resultlineori[MAXLINE];
1.230 brouard 14696:
1.136 brouard 14697: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 14698: char *tok, *val; /* pathtot */
1.334 brouard 14699: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
1.359 brouard 14700: int c, h; /* c2; */
1.191 brouard 14701: int jl=0;
14702: int i1, j1, jk, stepsize=0;
1.194 brouard 14703: int count=0;
14704:
1.164 brouard 14705: int *tab;
1.136 brouard 14706: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.296 brouard 14707: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
14708: /* double anprojf, mprojf, jprojf; */
14709: /* double jintmean,mintmean,aintmean; */
14710: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14711: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14712: double yrfproj= 10.0; /* Number of years of forward projections */
14713: double yrbproj= 10.0; /* Number of years of backward projections */
14714: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
1.136 brouard 14715: int mobilav=0,popforecast=0;
1.191 brouard 14716: int hstepm=0, nhstepm=0;
1.136 brouard 14717: int agemortsup;
14718: float sumlpop=0.;
14719: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
14720: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
14721:
1.191 brouard 14722: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 14723: double ftolpl=FTOL;
14724: double **prlim;
1.217 brouard 14725: double **bprlim;
1.317 brouard 14726: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
14727: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
1.251 brouard 14728: double ***paramstart; /* Matrix of starting parameter values */
14729: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
1.136 brouard 14730: double **matcov; /* Matrix of covariance */
1.203 brouard 14731: double **hess; /* Hessian matrix */
1.136 brouard 14732: double ***delti3; /* Scale */
14733: double *delti; /* Scale */
14734: double ***eij, ***vareij;
1.359 brouard 14735: //double **varpl; /* Variances of prevalence limits by age */
1.269 brouard 14736:
1.136 brouard 14737: double *epj, vepp;
1.164 brouard 14738:
1.273 brouard 14739: double dateprev1, dateprev2;
1.296 brouard 14740: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
14741: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
14742:
1.217 brouard 14743:
1.136 brouard 14744: double **ximort;
1.145 brouard 14745: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 14746: int *dcwave;
14747:
1.164 brouard 14748: char z[1]="c";
1.136 brouard 14749:
14750: /*char *strt;*/
14751: char strtend[80];
1.126 brouard 14752:
1.164 brouard 14753:
1.126 brouard 14754: /* setlocale (LC_ALL, ""); */
14755: /* bindtextdomain (PACKAGE, LOCALEDIR); */
14756: /* textdomain (PACKAGE); */
14757: /* setlocale (LC_CTYPE, ""); */
14758: /* setlocale (LC_MESSAGES, ""); */
14759:
14760: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 14761: rstart_time = time(NULL);
14762: /* (void) gettimeofday(&start_time,&tzp);*/
14763: start_time = *localtime(&rstart_time);
1.126 brouard 14764: curr_time=start_time;
1.157 brouard 14765: /*tml = *localtime(&start_time.tm_sec);*/
14766: /* strcpy(strstart,asctime(&tml)); */
14767: strcpy(strstart,asctime(&start_time));
1.126 brouard 14768:
14769: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 14770: /* tp.tm_sec = tp.tm_sec +86400; */
14771: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 14772: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
14773: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
14774: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 14775: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 14776: /* strt=asctime(&tmg); */
14777: /* printf("Time(after) =%s",strstart); */
14778: /* (void) time (&time_value);
14779: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
14780: * tm = *localtime(&time_value);
14781: * strstart=asctime(&tm);
14782: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
14783: */
14784:
14785: nberr=0; /* Number of errors and warnings */
14786: nbwarn=0;
1.184 brouard 14787: #ifdef WIN32
14788: _getcwd(pathcd, size);
14789: #else
1.126 brouard 14790: getcwd(pathcd, size);
1.184 brouard 14791: #endif
1.191 brouard 14792: syscompilerinfo(0);
1.359 brouard 14793: printf("\nIMaCh prax version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 14794: if(argc <=1){
14795: printf("\nEnter the parameter file name: ");
1.205 brouard 14796: if(!fgets(pathr,FILENAMELENGTH,stdin)){
14797: printf("ERROR Empty parameter file name\n");
14798: goto end;
14799: }
1.126 brouard 14800: i=strlen(pathr);
14801: if(pathr[i-1]=='\n')
14802: pathr[i-1]='\0';
1.156 brouard 14803: i=strlen(pathr);
1.205 brouard 14804: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 14805: pathr[i-1]='\0';
1.205 brouard 14806: }
14807: i=strlen(pathr);
14808: if( i==0 ){
14809: printf("ERROR Empty parameter file name\n");
14810: goto end;
14811: }
14812: for (tok = pathr; tok != NULL; ){
1.126 brouard 14813: printf("Pathr |%s|\n",pathr);
14814: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
14815: printf("val= |%s| pathr=%s\n",val,pathr);
14816: strcpy (pathtot, val);
14817: if(pathr[0] == '\0') break; /* Dirty */
14818: }
14819: }
1.281 brouard 14820: else if (argc<=2){
14821: strcpy(pathtot,argv[1]);
14822: }
1.126 brouard 14823: else{
14824: strcpy(pathtot,argv[1]);
1.281 brouard 14825: strcpy(z,argv[2]);
14826: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
1.126 brouard 14827: }
14828: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
14829: /*cygwin_split_path(pathtot,path,optionfile);
14830: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
14831: /* cutv(path,optionfile,pathtot,'\\');*/
14832:
14833: /* Split argv[0], imach program to get pathimach */
14834: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
14835: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14836: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14837: /* strcpy(pathimach,argv[0]); */
14838: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
14839: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
14840: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 14841: #ifdef WIN32
14842: _chdir(path); /* Can be a relative path */
14843: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
14844: #else
1.126 brouard 14845: chdir(path); /* Can be a relative path */
1.184 brouard 14846: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
14847: #endif
14848: printf("Current directory %s!\n",pathcd);
1.126 brouard 14849: strcpy(command,"mkdir ");
14850: strcat(command,optionfilefiname);
14851: if((outcmd=system(command)) != 0){
1.169 brouard 14852: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 14853: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
14854: /* fclose(ficlog); */
14855: /* exit(1); */
14856: }
14857: /* if((imk=mkdir(optionfilefiname))<0){ */
14858: /* perror("mkdir"); */
14859: /* } */
14860:
14861: /*-------- arguments in the command line --------*/
14862:
1.186 brouard 14863: /* Main Log file */
1.126 brouard 14864: strcat(filelog, optionfilefiname);
14865: strcat(filelog,".log"); /* */
14866: if((ficlog=fopen(filelog,"w"))==NULL) {
14867: printf("Problem with logfile %s\n",filelog);
14868: goto end;
14869: }
14870: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 14871: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 14872: fprintf(ficlog,"\nEnter the parameter file name: \n");
14873: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
14874: path=%s \n\
14875: optionfile=%s\n\
14876: optionfilext=%s\n\
1.156 brouard 14877: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 14878:
1.197 brouard 14879: syscompilerinfo(1);
1.167 brouard 14880:
1.126 brouard 14881: printf("Local time (at start):%s",strstart);
14882: fprintf(ficlog,"Local time (at start): %s",strstart);
14883: fflush(ficlog);
14884: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 14885: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 14886:
14887: /* */
14888: strcpy(fileres,"r");
14889: strcat(fileres, optionfilefiname);
1.201 brouard 14890: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 14891: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 14892: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 14893:
1.186 brouard 14894: /* Main ---------arguments file --------*/
1.126 brouard 14895:
14896: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 14897: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
14898: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 14899: fflush(ficlog);
1.149 brouard 14900: /* goto end; */
14901: exit(70);
1.126 brouard 14902: }
14903:
14904: strcpy(filereso,"o");
1.201 brouard 14905: strcat(filereso,fileresu);
1.126 brouard 14906: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
14907: printf("Problem with Output resultfile: %s\n", filereso);
14908: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
14909: fflush(ficlog);
14910: goto end;
14911: }
1.278 brouard 14912: /*-------- Rewriting parameter file ----------*/
14913: strcpy(rfileres,"r"); /* "Rparameterfile */
14914: strcat(rfileres,optionfilefiname); /* Parameter file first name */
14915: strcat(rfileres,"."); /* */
14916: strcat(rfileres,optionfilext); /* Other files have txt extension */
14917: if((ficres =fopen(rfileres,"w"))==NULL) {
14918: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
14919: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
14920: fflush(ficlog);
14921: goto end;
14922: }
14923: fprintf(ficres,"#IMaCh %s\n",version);
1.126 brouard 14924:
1.278 brouard 14925:
1.126 brouard 14926: /* Reads comments: lines beginning with '#' */
14927: numlinepar=0;
1.277 brouard 14928: /* Is it a BOM UTF-8 Windows file? */
14929: /* First parameter line */
1.197 brouard 14930: while(fgets(line, MAXLINE, ficpar)) {
1.277 brouard 14931: noffset=0;
14932: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
14933: {
14934: noffset=noffset+3;
14935: printf("# File is an UTF8 Bom.\n"); // 0xBF
14936: }
1.302 brouard 14937: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
14938: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
1.277 brouard 14939: {
14940: noffset=noffset+2;
14941: printf("# File is an UTF16BE BOM file\n");
14942: }
14943: else if( line[0] == 0 && line[1] == 0)
14944: {
14945: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
14946: noffset=noffset+4;
14947: printf("# File is an UTF16BE BOM file\n");
14948: }
14949: } else{
14950: ;/*printf(" Not a BOM file\n");*/
14951: }
14952:
1.197 brouard 14953: /* If line starts with a # it is a comment */
1.277 brouard 14954: if (line[noffset] == '#') {
1.197 brouard 14955: numlinepar++;
14956: fputs(line,stdout);
14957: fputs(line,ficparo);
1.278 brouard 14958: fputs(line,ficres);
1.197 brouard 14959: fputs(line,ficlog);
14960: continue;
14961: }else
14962: break;
14963: }
14964: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
14965: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
14966: if (num_filled != 5) {
14967: printf("Should be 5 parameters\n");
1.283 brouard 14968: fprintf(ficlog,"Should be 5 parameters\n");
1.197 brouard 14969: }
1.126 brouard 14970: numlinepar++;
1.197 brouard 14971: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.283 brouard 14972: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14973: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14974: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.197 brouard 14975: }
14976: /* Second parameter line */
14977: while(fgets(line, MAXLINE, ficpar)) {
1.283 brouard 14978: /* while(fscanf(ficpar,"%[^\n]", line)) { */
14979: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
1.197 brouard 14980: if (line[0] == '#') {
14981: numlinepar++;
1.283 brouard 14982: printf("%s",line);
14983: fprintf(ficres,"%s",line);
14984: fprintf(ficparo,"%s",line);
14985: fprintf(ficlog,"%s",line);
1.197 brouard 14986: continue;
14987: }else
14988: break;
14989: }
1.223 brouard 14990: if((num_filled=sscanf(line,"ftol=%lf stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n", \
14991: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
14992: if (num_filled != 11) {
14993: printf("Not 11 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nqv=1 ntv=2 nqtv=1 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
1.209 brouard 14994: printf("but line=%s\n",line);
1.283 brouard 14995: fprintf(ficlog,"Not 11 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nqv=1 ntv=2 nqtv=1 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
14996: fprintf(ficlog,"but line=%s\n",line);
1.197 brouard 14997: }
1.286 brouard 14998: if( lastpass > maxwav){
14999: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
15000: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
15001: fflush(ficlog);
15002: goto end;
15003: }
15004: printf("ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.283 brouard 15005: fprintf(ficparo,"ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.286 brouard 15006: fprintf(ficres,"ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, 0, weightopt);
1.283 brouard 15007: fprintf(ficlog,"ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.126 brouard 15008: }
1.203 brouard 15009: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 15010: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 15011: /* Third parameter line */
15012: while(fgets(line, MAXLINE, ficpar)) {
15013: /* If line starts with a # it is a comment */
15014: if (line[0] == '#') {
15015: numlinepar++;
1.283 brouard 15016: printf("%s",line);
15017: fprintf(ficres,"%s",line);
15018: fprintf(ficparo,"%s",line);
15019: fprintf(ficlog,"%s",line);
1.197 brouard 15020: continue;
15021: }else
15022: break;
15023: }
1.351 brouard 15024: if((num_filled=sscanf(line,"model=%[^.\n]", model)) !=EOF){ /* Every character after model but dot and return */
15025: if (num_filled != 1){
15026: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15027: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15028: model[0]='\0';
15029: goto end;
15030: }else{
15031: trimbtab(linetmp,line); /* Trims multiple blanks in line */
15032: strcpy(line, linetmp);
15033: }
15034: }
15035: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){ /* Every character after 1+age but dot and return */
1.279 brouard 15036: if (num_filled != 1){
1.302 brouard 15037: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
15038: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
1.197 brouard 15039: model[0]='\0';
15040: goto end;
15041: }
15042: else{
15043: if (model[0]=='+'){
15044: for(i=1; i<=strlen(model);i++)
15045: modeltemp[i-1]=model[i];
1.201 brouard 15046: strcpy(model,modeltemp);
1.197 brouard 15047: }
15048: }
1.338 brouard 15049: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 15050: printf("model=1+age+%s\n",model);fflush(stdout);
1.283 brouard 15051: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
15052: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
15053: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 15054: }
15055: /* fscanf(ficpar,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d model=1+age+%s\n",title, datafile, &lastobs, &firstpass,&lastpass,&ftol, &stepm, &ncovcol, &nlstate,&ndeath, &maxwav, &mle, &weightopt,model); */
15056: /* numlinepar=numlinepar+3; /\* In general *\/ */
15057: /* printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model); */
1.283 brouard 15058: /* fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model); */
15059: /* fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model); */
1.126 brouard 15060: fflush(ficlog);
1.190 brouard 15061: /* if(model[0]=='#'|| model[0]== '\0'){ */
15062: if(model[0]=='#'){
1.279 brouard 15063: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
15064: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
15065: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
1.187 brouard 15066: if(mle != -1){
1.279 brouard 15067: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter vectors and subdiagonal covariance matrix.\n");
1.187 brouard 15068: exit(1);
15069: }
15070: }
1.126 brouard 15071: while((c=getc(ficpar))=='#' && c!= EOF){
15072: ungetc(c,ficpar);
15073: fgets(line, MAXLINE, ficpar);
15074: numlinepar++;
1.195 brouard 15075: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
15076: z[0]=line[1];
1.342 brouard 15077: }else if(line[1]=='d'){ /* For debugging individual values of covariates in ficresilk */
1.343 brouard 15078: debugILK=1;printf("DebugILK\n");
1.195 brouard 15079: }
15080: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 15081: fputs(line, stdout);
15082: //puts(line);
1.126 brouard 15083: fputs(line,ficparo);
15084: fputs(line,ficlog);
15085: }
15086: ungetc(c,ficpar);
15087:
15088:
1.290 brouard 15089: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
15090: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
15091: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
1.341 brouard 15092: /* if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /\**< Time varying covariate (dummy and quantitative)*\/ */
15093: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs); /**< Might be better */
1.136 brouard 15094: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
15095: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
15096: v1+v2*age+v2*v3 makes cptcovn = 3
15097: */
15098: if (strlen(model)>1)
1.187 brouard 15099: ncovmodel=2+nbocc(model,'+')+1; /*Number of variables including intercept and age = cptcovn + intercept + age : v1+v2+v3+v2*v4+v5*age makes 5+2=7,age*age makes 3*/
1.145 brouard 15100: else
1.187 brouard 15101: ncovmodel=2; /* Constant and age */
1.133 brouard 15102: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
15103: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 15104: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
15105: printf("Too complex model for current IMaCh: npar=(nlstate+ndeath-1)*nlstate*ncovmodel=%d >= %d(MAXPARM) or nlstate=%d >= %d(NLSTATEMAX) or ndeath=%d >= %d(NDEATHMAX) or ncovmodel=(k+age+#of+signs)=%d(NCOVMAX) >= %d\n",npar, MAXPARM, nlstate, NLSTATEMAX, ndeath, NDEATHMAX, ncovmodel, NCOVMAX);
15106: fprintf(ficlog,"Too complex model for current IMaCh: %d >=%d(MAXPARM) or %d >=%d(NLSTATEMAX) or %d >=%d(NDEATHMAX) or %d(NCOVMAX) >=%d\n",npar, MAXPARM, nlstate, NLSTATEMAX, ndeath, NDEATHMAX, ncovmodel, NCOVMAX);
15107: fflush(stdout);
15108: fclose (ficlog);
15109: goto end;
15110: }
1.126 brouard 15111: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15112: delti=delti3[1][1];
15113: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
15114: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
1.247 brouard 15115: /* We could also provide initial parameters values giving by simple logistic regression
15116: * only one way, that is without matrix product. We will have nlstate maximizations */
15117: /* for(i=1;i<nlstate;i++){ */
15118: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15119: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15120: /* } */
1.126 brouard 15121: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 15122: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
15123: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 15124: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15125: fclose (ficparo);
15126: fclose (ficlog);
15127: goto end;
15128: exit(0);
1.220 brouard 15129: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 15130: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 15131: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
15132: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 15133: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15134: matcov=matrix(1,npar,1,npar);
1.203 brouard 15135: hess=matrix(1,npar,1,npar);
1.220 brouard 15136: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 15137: /* Read guessed parameters */
1.126 brouard 15138: /* Reads comments: lines beginning with '#' */
15139: while((c=getc(ficpar))=='#' && c!= EOF){
15140: ungetc(c,ficpar);
15141: fgets(line, MAXLINE, ficpar);
15142: numlinepar++;
1.141 brouard 15143: fputs(line,stdout);
1.126 brouard 15144: fputs(line,ficparo);
15145: fputs(line,ficlog);
15146: }
15147: ungetc(c,ficpar);
15148:
15149: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.251 brouard 15150: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.126 brouard 15151: for(i=1; i <=nlstate; i++){
1.234 brouard 15152: j=0;
1.126 brouard 15153: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 15154: if(jj==i) continue;
15155: j++;
1.292 brouard 15156: while((c=getc(ficpar))=='#' && c!= EOF){
15157: ungetc(c,ficpar);
15158: fgets(line, MAXLINE, ficpar);
15159: numlinepar++;
15160: fputs(line,stdout);
15161: fputs(line,ficparo);
15162: fputs(line,ficlog);
15163: }
15164: ungetc(c,ficpar);
1.234 brouard 15165: fscanf(ficpar,"%1d%1d",&i1,&j1);
15166: if ((i1 != i) || (j1 != jj)){
15167: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 15168: It might be a problem of design; if ncovcol and the model are correct\n \
15169: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 15170: exit(1);
15171: }
15172: fprintf(ficparo,"%1d%1d",i1,j1);
15173: if(mle==1)
15174: printf("%1d%1d",i,jj);
15175: fprintf(ficlog,"%1d%1d",i,jj);
15176: for(k=1; k<=ncovmodel;k++){
15177: fscanf(ficpar," %lf",¶m[i][j][k]);
15178: if(mle==1){
15179: printf(" %lf",param[i][j][k]);
15180: fprintf(ficlog," %lf",param[i][j][k]);
15181: }
15182: else
15183: fprintf(ficlog," %lf",param[i][j][k]);
15184: fprintf(ficparo," %lf",param[i][j][k]);
15185: }
15186: fscanf(ficpar,"\n");
15187: numlinepar++;
15188: if(mle==1)
15189: printf("\n");
15190: fprintf(ficlog,"\n");
15191: fprintf(ficparo,"\n");
1.126 brouard 15192: }
15193: }
15194: fflush(ficlog);
1.234 brouard 15195:
1.251 brouard 15196: /* Reads parameters values */
1.126 brouard 15197: p=param[1][1];
1.251 brouard 15198: pstart=paramstart[1][1];
1.126 brouard 15199:
15200: /* Reads comments: lines beginning with '#' */
15201: while((c=getc(ficpar))=='#' && c!= EOF){
15202: ungetc(c,ficpar);
15203: fgets(line, MAXLINE, ficpar);
15204: numlinepar++;
1.141 brouard 15205: fputs(line,stdout);
1.126 brouard 15206: fputs(line,ficparo);
15207: fputs(line,ficlog);
15208: }
15209: ungetc(c,ficpar);
15210:
15211: for(i=1; i <=nlstate; i++){
15212: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 15213: fscanf(ficpar,"%1d%1d",&i1,&j1);
15214: if ( (i1-i) * (j1-j) != 0){
15215: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
15216: exit(1);
15217: }
15218: printf("%1d%1d",i,j);
15219: fprintf(ficparo,"%1d%1d",i1,j1);
15220: fprintf(ficlog,"%1d%1d",i1,j1);
15221: for(k=1; k<=ncovmodel;k++){
15222: fscanf(ficpar,"%le",&delti3[i][j][k]);
15223: printf(" %le",delti3[i][j][k]);
15224: fprintf(ficparo," %le",delti3[i][j][k]);
15225: fprintf(ficlog," %le",delti3[i][j][k]);
15226: }
15227: fscanf(ficpar,"\n");
15228: numlinepar++;
15229: printf("\n");
15230: fprintf(ficparo,"\n");
15231: fprintf(ficlog,"\n");
1.126 brouard 15232: }
15233: }
15234: fflush(ficlog);
1.234 brouard 15235:
1.145 brouard 15236: /* Reads covariance matrix */
1.126 brouard 15237: delti=delti3[1][1];
1.220 brouard 15238:
15239:
1.126 brouard 15240: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
1.220 brouard 15241:
1.126 brouard 15242: /* Reads comments: lines beginning with '#' */
15243: while((c=getc(ficpar))=='#' && c!= EOF){
15244: ungetc(c,ficpar);
15245: fgets(line, MAXLINE, ficpar);
15246: numlinepar++;
1.141 brouard 15247: fputs(line,stdout);
1.126 brouard 15248: fputs(line,ficparo);
15249: fputs(line,ficlog);
15250: }
15251: ungetc(c,ficpar);
1.220 brouard 15252:
1.126 brouard 15253: matcov=matrix(1,npar,1,npar);
1.203 brouard 15254: hess=matrix(1,npar,1,npar);
1.131 brouard 15255: for(i=1; i <=npar; i++)
15256: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 15257:
1.194 brouard 15258: /* Scans npar lines */
1.126 brouard 15259: for(i=1; i <=npar; i++){
1.226 brouard 15260: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 15261: if(count != 3){
1.226 brouard 15262: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 15263: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15264: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 15265: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 15266: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15267: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 15268: exit(1);
1.220 brouard 15269: }else{
1.226 brouard 15270: if(mle==1)
15271: printf("%1d%1d%d",i1,j1,jk);
15272: }
15273: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
15274: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 15275: for(j=1; j <=i; j++){
1.226 brouard 15276: fscanf(ficpar," %le",&matcov[i][j]);
15277: if(mle==1){
15278: printf(" %.5le",matcov[i][j]);
15279: }
15280: fprintf(ficlog," %.5le",matcov[i][j]);
15281: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 15282: }
15283: fscanf(ficpar,"\n");
15284: numlinepar++;
15285: if(mle==1)
1.220 brouard 15286: printf("\n");
1.126 brouard 15287: fprintf(ficlog,"\n");
15288: fprintf(ficparo,"\n");
15289: }
1.194 brouard 15290: /* End of read covariance matrix npar lines */
1.126 brouard 15291: for(i=1; i <=npar; i++)
15292: for(j=i+1;j<=npar;j++)
1.226 brouard 15293: matcov[i][j]=matcov[j][i];
1.126 brouard 15294:
15295: if(mle==1)
15296: printf("\n");
15297: fprintf(ficlog,"\n");
15298:
15299: fflush(ficlog);
15300:
15301: } /* End of mle != -3 */
1.218 brouard 15302:
1.186 brouard 15303: /* Main data
15304: */
1.290 brouard 15305: nobs=lastobs-firstobs+1; /* was = lastobs;*/
15306: /* num=lvector(1,n); */
15307: /* moisnais=vector(1,n); */
15308: /* annais=vector(1,n); */
15309: /* moisdc=vector(1,n); */
15310: /* andc=vector(1,n); */
15311: /* weight=vector(1,n); */
15312: /* agedc=vector(1,n); */
15313: /* cod=ivector(1,n); */
15314: /* for(i=1;i<=n;i++){ */
15315: num=lvector(firstobs,lastobs);
15316: moisnais=vector(firstobs,lastobs);
15317: annais=vector(firstobs,lastobs);
15318: moisdc=vector(firstobs,lastobs);
15319: andc=vector(firstobs,lastobs);
15320: weight=vector(firstobs,lastobs);
15321: agedc=vector(firstobs,lastobs);
15322: cod=ivector(firstobs,lastobs);
15323: for(i=firstobs;i<=lastobs;i++){
1.234 brouard 15324: num[i]=0;
15325: moisnais[i]=0;
15326: annais[i]=0;
15327: moisdc[i]=0;
15328: andc[i]=0;
15329: agedc[i]=0;
15330: cod[i]=0;
15331: weight[i]=1.0; /* Equal weights, 1 by default */
15332: }
1.290 brouard 15333: mint=matrix(1,maxwav,firstobs,lastobs);
15334: anint=matrix(1,maxwav,firstobs,lastobs);
1.325 brouard 15335: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
1.336 brouard 15336: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
1.126 brouard 15337: tab=ivector(1,NCOVMAX);
1.144 brouard 15338: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 15339: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.126 brouard 15340:
1.136 brouard 15341: /* Reads data from file datafile */
15342: if (readdata(datafile, firstobs, lastobs, &imx)==1)
15343: goto end;
15344:
15345: /* Calculation of the number of parameters from char model */
1.234 brouard 15346: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 15347: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
15348: k=3 V4 Tvar[k=3]= 4 (from V4)
15349: k=2 V1 Tvar[k=2]= 1 (from V1)
15350: k=1 Tvar[1]=2 (from V2)
1.234 brouard 15351: */
15352:
15353: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
15354: TvarsDind=ivector(1,NCOVMAX); /* */
1.330 brouard 15355: TnsdVar=ivector(1,NCOVMAX); /* */
1.335 brouard 15356: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
1.234 brouard 15357: TvarsD=ivector(1,NCOVMAX); /* */
15358: TvarsQind=ivector(1,NCOVMAX); /* */
15359: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 15360: TvarF=ivector(1,NCOVMAX); /* */
15361: TvarFind=ivector(1,NCOVMAX); /* */
15362: TvarV=ivector(1,NCOVMAX); /* */
15363: TvarVind=ivector(1,NCOVMAX); /* */
15364: TvarA=ivector(1,NCOVMAX); /* */
15365: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 15366: TvarFD=ivector(1,NCOVMAX); /* */
15367: TvarFDind=ivector(1,NCOVMAX); /* */
15368: TvarFQ=ivector(1,NCOVMAX); /* */
15369: TvarFQind=ivector(1,NCOVMAX); /* */
15370: TvarVD=ivector(1,NCOVMAX); /* */
15371: TvarVDind=ivector(1,NCOVMAX); /* */
15372: TvarVQ=ivector(1,NCOVMAX); /* */
15373: TvarVQind=ivector(1,NCOVMAX); /* */
1.339 brouard 15374: TvarVV=ivector(1,NCOVMAX); /* */
15375: TvarVVind=ivector(1,NCOVMAX); /* */
1.349 brouard 15376: TvarVVA=ivector(1,NCOVMAX); /* */
15377: TvarVVAind=ivector(1,NCOVMAX); /* */
15378: TvarAVVA=ivector(1,NCOVMAX); /* */
15379: TvarAVVAind=ivector(1,NCOVMAX); /* */
1.231 brouard 15380:
1.230 brouard 15381: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 15382: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 15383: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
15384: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
15385: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.349 brouard 15386: DummyV=ivector(-1,NCOVMAX); /* 1 to 3 */
15387: FixedV=ivector(-1,NCOVMAX); /* 1 to 3 */
15388:
1.137 brouard 15389: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
15390: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
15391: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
15392: */
15393: /* For model-covariate k tells which data-covariate to use but
15394: because this model-covariate is a construction we invent a new column
15395: ncovcol + k1
15396: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
15397: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 15398: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
15399: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 15400: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
15401: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 15402: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 15403: */
1.145 brouard 15404: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
15405: Tvard=imatrix(1,NCOVMAX,1,2); /* n=Tvard[k1][1] and m=Tvard[k1][2] gives the couple n,m of the k1 th product Vn*Vm
1.141 brouard 15406: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
15407: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.351 brouard 15408: Tvardk=imatrix(0,NCOVMAX,1,2);
1.145 brouard 15409: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 15410: 4 covariates (3 plus signs)
15411: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
1.328 brouard 15412: */
15413: for(i=1;i<NCOVMAX;i++)
15414: Tage[i]=0;
1.230 brouard 15415: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 15416: * individual dummy, fixed or varying:
15417: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
15418: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 15419: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
15420: * V1 df, V2 qf, V3 & V4 dv, V5 qv
15421: * Tmodelind[1]@9={9,0,3,2,}*/
15422: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
15423: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 15424: * individual quantitative, fixed or varying:
15425: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
15426: * 3, 1, 0, 0, 0, 0, 0, 0},
15427: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.349 brouard 15428:
15429: /* Probably useless zeroes */
15430: for(i=1;i<NCOVMAX;i++){
15431: DummyV[i]=0;
15432: FixedV[i]=0;
15433: }
15434:
15435: for(i=1; i <=ncovcol;i++){
15436: DummyV[i]=0;
15437: FixedV[i]=0;
15438: }
15439: for(i=ncovcol+1; i <=ncovcol+nqv;i++){
15440: DummyV[i]=1;
15441: FixedV[i]=0;
15442: }
15443: for(i=ncovcol+nqv+1; i <=ncovcol+nqv+ntv;i++){
15444: DummyV[i]=0;
15445: FixedV[i]=1;
15446: }
15447: for(i=ncovcol+nqv+ntv+1; i <=ncovcol+nqv+ntv+nqtv;i++){
15448: DummyV[i]=1;
15449: FixedV[i]=1;
15450: }
15451: for(i=1; i <=ncovcol+nqv+ntv+nqtv;i++){
15452: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
15453: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
15454: }
15455:
15456:
15457:
1.186 brouard 15458: /* Main decodemodel */
15459:
1.187 brouard 15460:
1.223 brouard 15461: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 15462: goto end;
15463:
1.137 brouard 15464: if((double)(lastobs-imx)/(double)imx > 1.10){
15465: nbwarn++;
15466: printf("Warning: The value of parameter lastobs=%d is big compared to the \n effective number of cases imx=%d, please adjust, \n otherwise you are allocating more memory than necessary.\n",lastobs, imx);
15467: fprintf(ficlog,"Warning: The value of parameter lastobs=%d is big compared to the \n effective number of cases imx=%d, please adjust, \n otherwise you are allocating more memory than necessary.\n",lastobs, imx);
15468: }
1.136 brouard 15469: /* if(mle==1){*/
1.137 brouard 15470: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
15471: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 15472: }
15473:
15474: /*-calculation of age at interview from date of interview and age at death -*/
15475: agev=matrix(1,maxwav,1,imx);
15476:
15477: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
15478: goto end;
15479:
1.126 brouard 15480:
1.136 brouard 15481: agegomp=(int)agemin;
1.290 brouard 15482: free_vector(moisnais,firstobs,lastobs);
15483: free_vector(annais,firstobs,lastobs);
1.126 brouard 15484: /* free_matrix(mint,1,maxwav,1,n);
15485: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 15486: /* free_vector(moisdc,1,n); */
15487: /* free_vector(andc,1,n); */
1.145 brouard 15488: /* */
15489:
1.126 brouard 15490: wav=ivector(1,imx);
1.214 brouard 15491: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
15492: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
15493: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
15494: dh=imatrix(1,lastpass-firstpass+2,1,imx); /* We are adding a wave if status is unknown at last wave but death occurs after last wave.*/
15495: bh=imatrix(1,lastpass-firstpass+2,1,imx);
15496: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 15497:
15498: /* Concatenates waves */
1.214 brouard 15499: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
15500: Death is a valid wave (if date is known).
15501: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
15502: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
15503: and mw[mi+1][i]. dh depends on stepm.
15504: */
15505:
1.126 brouard 15506: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.248 brouard 15507: /* Concatenates waves */
1.145 brouard 15508:
1.290 brouard 15509: free_vector(moisdc,firstobs,lastobs);
15510: free_vector(andc,firstobs,lastobs);
1.215 brouard 15511:
1.126 brouard 15512: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
15513: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
15514: ncodemax[1]=1;
1.145 brouard 15515: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 15516: cptcoveff=0;
1.220 brouard 15517: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
1.335 brouard 15518: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; as well as calculate cptcoveff or number of total effective dummy covariates*/
1.227 brouard 15519: }
15520:
15521: ncovcombmax=pow(2,cptcoveff);
1.338 brouard 15522: invalidvarcomb=ivector(0, ncovcombmax);
15523: for(i=0;i<ncovcombmax;i++)
1.227 brouard 15524: invalidvarcomb[i]=0;
15525:
1.211 brouard 15526: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 15527: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 15528: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 15529:
1.200 brouard 15530: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 15531: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 15532: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 15533: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
15534: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
15535: * (currently 0 or 1) in the data.
15536: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
15537: * corresponding modality (h,j).
15538: */
15539:
1.145 brouard 15540: h=0;
15541: /*if (cptcovn > 0) */
1.126 brouard 15542: m=pow(2,cptcoveff);
15543:
1.144 brouard 15544: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 15545: * For k=4 covariates, h goes from 1 to m=2**k
15546: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
15547: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.329 brouard 15548: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
15549: *______________________________ *______________________
15550: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
15551: * 2 2 1 1 1 * 1 0 0 0 1
15552: * 3 i=2 1 2 1 1 * 2 0 0 1 0
15553: * 4 2 2 1 1 * 3 0 0 1 1
15554: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
15555: * 6 2 1 2 1 * 5 0 1 0 1
15556: * 7 i=4 1 2 2 1 * 6 0 1 1 0
15557: * 8 2 2 2 1 * 7 0 1 1 1
15558: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
15559: * 10 2 1 1 2 * 9 1 0 0 1
15560: * 11 i=6 1 2 1 2 * 10 1 0 1 0
15561: * 12 2 2 1 2 * 11 1 0 1 1
15562: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
15563: * 14 2 1 2 2 * 13 1 1 0 1
15564: * 15 i=8 1 2 2 2 * 14 1 1 1 0
15565: * 16 2 2 2 2 * 15 1 1 1 1
15566: */
1.212 brouard 15567: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 15568: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
15569: * and the value of each covariate?
15570: * V1=1, V2=1, V3=2, V4=1 ?
15571: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
15572: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
15573: * In order to get the real value in the data, we use nbcode
15574: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
15575: * We are keeping this crazy system in order to be able (in the future?)
15576: * to have more than 2 values (0 or 1) for a covariate.
15577: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
15578: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
15579: * bbbbbbbb
15580: * 76543210
15581: * h-1 00000101 (6-1=5)
1.219 brouard 15582: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 15583: * &
15584: * 1 00000001 (1)
1.219 brouard 15585: * 00000000 = 1 & ((h-1) >> (k-1))
15586: * +1= 00000001 =1
1.211 brouard 15587: *
15588: * h=14, k=3 => h'=h-1=13, k'=k-1=2
15589: * h' 1101 =2^3+2^2+0x2^1+2^0
15590: * >>k' 11
15591: * & 00000001
15592: * = 00000001
15593: * +1 = 00000010=2 = codtabm(14,3)
15594: * Reverse h=6 and m=16?
15595: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
15596: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
15597: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
15598: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
15599: * V3=decodtabm(14,3,2**4)=2
15600: * h'=13 1101 =2^3+2^2+0x2^1+2^0
15601: *(h-1) >> (j-1) 0011 =13 >> 2
15602: * &1 000000001
15603: * = 000000001
15604: * +1= 000000010 =2
15605: * 2211
15606: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
15607: * V3=2
1.220 brouard 15608: * codtabm and decodtabm are identical
1.211 brouard 15609: */
15610:
1.145 brouard 15611:
15612: free_ivector(Ndum,-1,NCOVMAX);
15613:
15614:
1.126 brouard 15615:
1.186 brouard 15616: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 15617: strcpy(optionfilegnuplot,optionfilefiname);
15618: if(mle==-3)
1.201 brouard 15619: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 15620: strcat(optionfilegnuplot,".gp");
15621:
15622: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
15623: printf("Problem with file %s",optionfilegnuplot);
15624: }
15625: else{
1.204 brouard 15626: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 15627: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 15628: //fprintf(ficgp,"set missing 'NaNq'\n");
15629: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 15630: }
15631: /* fclose(ficgp);*/
1.186 brouard 15632:
15633:
15634: /* Initialisation of --------- index.htm --------*/
1.126 brouard 15635:
15636: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
15637: if(mle==-3)
1.201 brouard 15638: strcat(optionfilehtm,"-MORT_");
1.126 brouard 15639: strcat(optionfilehtm,".htm");
15640: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 15641: printf("Problem with %s \n",optionfilehtm);
15642: exit(0);
1.126 brouard 15643: }
15644:
15645: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
15646: strcat(optionfilehtmcov,"-cov.htm");
15647: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
15648: printf("Problem with %s \n",optionfilehtmcov), exit(0);
15649: }
15650: else{
15651: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
15652: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 15653: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 15654: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
15655: }
15656:
1.335 brouard 15657: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
15658: <title>IMaCh %s</title></head>\n\
15659: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
15660: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
15661: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
15662: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
15663: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
15664:
15665: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 15666: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 15667: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.337 brouard 15668: This file: <a href=\"%s\">%s</a></br>Title=%s <br>Datafile=<a href=\"%s\">%s</a> Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 15669: \n\
15670: <hr size=\"2\" color=\"#EC5E5E\">\
15671: <ul><li><h4>Parameter files</h4>\n\
15672: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
15673: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
15674: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
15675: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
15676: - Date and time at start: %s</ul>\n",\
1.335 brouard 15677: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
1.126 brouard 15678: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
15679: fileres,fileres,\
15680: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
15681: fflush(fichtm);
15682:
15683: strcpy(pathr,path);
15684: strcat(pathr,optionfilefiname);
1.184 brouard 15685: #ifdef WIN32
15686: _chdir(optionfilefiname); /* Move to directory named optionfile */
15687: #else
1.126 brouard 15688: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 15689: #endif
15690:
1.126 brouard 15691:
1.220 brouard 15692: /* Calculates basic frequencies. Computes observed prevalence at single age
15693: and for any valid combination of covariates
1.126 brouard 15694: and prints on file fileres'p'. */
1.359 brouard 15695: freqsummary(fileres, p, pstart, (double)agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.227 brouard 15696: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 15697:
15698: fprintf(fichtm,"\n");
1.286 brouard 15699: fprintf(fichtm,"<h4>Parameter line 2</h4><ul><li>Tolerance for the convergence of the likelihood: ftol=%g \n<li>Interval for the elementary matrix (in month): stepm=%d",\
1.274 brouard 15700: ftol, stepm);
15701: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
15702: ncurrv=1;
15703: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
15704: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
15705: ncurrv=i;
15706: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 15707: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
1.274 brouard 15708: ncurrv=i;
15709: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 15710: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
1.274 brouard 15711: ncurrv=i;
15712: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
15713: fprintf(fichtm,"\n<li>Weights column \n<br>Number of alive states: nlstate=%d <br>Number of death states (not really implemented): ndeath=%d \n<li>Number of waves: maxwav=%d \n<li>Parameter for maximization (1), using parameter values (0), for design of parameters and variance-covariance matrix: mle=%d \n<li>Does the weight column be taken into account (1), or not (0): weight=%d</ul>\n", \
15714: nlstate, ndeath, maxwav, mle, weightopt);
15715:
15716: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
15717: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
15718:
15719:
1.317 brouard 15720: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
1.126 brouard 15721: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
15722: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.274 brouard 15723: imx,agemin,agemax,jmin,jmax,jmean);
1.126 brouard 15724: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.268 brouard 15725: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15726: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15727: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15728: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 15729:
1.126 brouard 15730: /* For Powell, parameters are in a vector p[] starting at p[1]
15731: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
15732: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
15733:
15734: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 15735: /* For mortality only */
1.126 brouard 15736: if (mle==-3){
1.136 brouard 15737: ximort=matrix(1,NDIM,1,NDIM);
1.248 brouard 15738: for(i=1;i<=NDIM;i++)
15739: for(j=1;j<=NDIM;j++)
15740: ximort[i][j]=0.;
1.186 brouard 15741: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.290 brouard 15742: cens=ivector(firstobs,lastobs);
15743: ageexmed=vector(firstobs,lastobs);
15744: agecens=vector(firstobs,lastobs);
15745: dcwave=ivector(firstobs,lastobs);
1.223 brouard 15746:
1.126 brouard 15747: for (i=1; i<=imx; i++){
15748: dcwave[i]=-1;
15749: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 15750: if (s[m][i]>nlstate) {
15751: dcwave[i]=m;
15752: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
15753: break;
15754: }
1.126 brouard 15755: }
1.226 brouard 15756:
1.126 brouard 15757: for (i=1; i<=imx; i++) {
15758: if (wav[i]>0){
1.226 brouard 15759: ageexmed[i]=agev[mw[1][i]][i];
15760: j=wav[i];
15761: agecens[i]=1.;
15762:
15763: if (ageexmed[i]> 1 && wav[i] > 0){
15764: agecens[i]=agev[mw[j][i]][i];
15765: cens[i]= 1;
15766: }else if (ageexmed[i]< 1)
15767: cens[i]= -1;
15768: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
15769: cens[i]=0 ;
1.126 brouard 15770: }
15771: else cens[i]=-1;
15772: }
15773:
15774: for (i=1;i<=NDIM;i++) {
15775: for (j=1;j<=NDIM;j++)
1.226 brouard 15776: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 15777: }
15778:
1.302 brouard 15779: p[1]=0.0268; p[NDIM]=0.083;
15780: /* printf("%lf %lf", p[1], p[2]); */
1.126 brouard 15781:
15782:
1.136 brouard 15783: #ifdef GSL
15784: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 15785: #else
1.359 brouard 15786: printf("Powell-mort\n"); fprintf(ficlog,"Powell-mort\n");
1.136 brouard 15787: #endif
1.201 brouard 15788: strcpy(filerespow,"POW-MORT_");
15789: strcat(filerespow,fileresu);
1.126 brouard 15790: if((ficrespow=fopen(filerespow,"w"))==NULL) {
15791: printf("Problem with resultfile: %s\n", filerespow);
15792: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
15793: }
1.136 brouard 15794: #ifdef GSL
15795: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 15796: #else
1.126 brouard 15797: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 15798: #endif
1.126 brouard 15799: /* for (i=1;i<=nlstate;i++)
15800: for(j=1;j<=nlstate+ndeath;j++)
15801: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
15802: */
15803: fprintf(ficrespow,"\n");
1.136 brouard 15804: #ifdef GSL
15805: /* gsl starts here */
15806: T = gsl_multimin_fminimizer_nmsimplex;
15807: gsl_multimin_fminimizer *sfm = NULL;
15808: gsl_vector *ss, *x;
15809: gsl_multimin_function minex_func;
15810:
15811: /* Initial vertex size vector */
15812: ss = gsl_vector_alloc (NDIM);
15813:
15814: if (ss == NULL){
15815: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
15816: }
15817: /* Set all step sizes to 1 */
15818: gsl_vector_set_all (ss, 0.001);
15819:
15820: /* Starting point */
1.126 brouard 15821:
1.136 brouard 15822: x = gsl_vector_alloc (NDIM);
15823:
15824: if (x == NULL){
15825: gsl_vector_free(ss);
15826: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
15827: }
15828:
15829: /* Initialize method and iterate */
15830: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 15831: /* gsl_vector_set(x, 0, 0.0268); */
15832: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 15833: gsl_vector_set(x, 0, p[1]);
15834: gsl_vector_set(x, 1, p[2]);
15835:
15836: minex_func.f = &gompertz_f;
15837: minex_func.n = NDIM;
15838: minex_func.params = (void *)&p; /* ??? */
15839:
15840: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
15841: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
15842:
15843: printf("Iterations beginning .....\n\n");
15844: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
15845:
15846: iteri=0;
15847: while (rval == GSL_CONTINUE){
15848: iteri++;
15849: status = gsl_multimin_fminimizer_iterate(sfm);
15850:
15851: if (status) printf("error: %s\n", gsl_strerror (status));
15852: fflush(0);
15853:
15854: if (status)
15855: break;
15856:
15857: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
15858: ssval = gsl_multimin_fminimizer_size (sfm);
15859:
15860: if (rval == GSL_SUCCESS)
15861: printf ("converged to a local maximum at\n");
15862:
15863: printf("%5d ", iteri);
15864: for (it = 0; it < NDIM; it++){
15865: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
15866: }
15867: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
15868: }
15869:
15870: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
15871:
15872: gsl_vector_free(x); /* initial values */
15873: gsl_vector_free(ss); /* inital step size */
15874: for (it=0; it<NDIM; it++){
15875: p[it+1]=gsl_vector_get(sfm->x,it);
15876: fprintf(ficrespow," %.12lf", p[it]);
15877: }
15878: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
15879: #endif
15880: #ifdef POWELL
1.361 brouard 15881: #ifdef LINMINORIGINAL
15882: #else /* LINMINORIGINAL */
15883:
15884: flatdir=ivector(1,npar);
15885: for (j=1;j<=npar;j++) flatdir[j]=0;
15886: #endif /*LINMINORIGINAL */
1.362 brouard 15887: /* powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz); */
15888: /* double h0=0.25; */
15889: macheps=pow(16.0,-13.0);
15890: printf("Praxis Gegenfurtner mle=%d\n",mle);
15891: fprintf(ficlog, "Praxis Gegenfurtner mle=%d\n", mle);fflush(ficlog);
15892: /* ffmin = praxis(ftol,macheps, h0, npar, prin, p, gompertz); */
15893: /* For the Gompertz we use only two parameters */
15894: int _npar=2;
15895: ffmin = praxis(ftol,macheps, h0, _npar, 4, p, gompertz);
15896: printf("End Praxis\n");
1.126 brouard 15897: fclose(ficrespow);
1.361 brouard 15898: #ifdef LINMINORIGINAL
15899: #else
15900: free_ivector(flatdir,1,npar);
15901: #endif /* LINMINORIGINAL*/
1.364 brouard 15902: #endif /* POWELL */
1.203 brouard 15903: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 15904:
15905: for(i=1; i <=NDIM; i++)
15906: for(j=i+1;j<=NDIM;j++)
1.359 brouard 15907: matcov[i][j]=matcov[j][i];
1.126 brouard 15908:
15909: printf("\nCovariance matrix\n ");
1.203 brouard 15910: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 15911: for(i=1; i <=NDIM; i++) {
15912: for(j=1;j<=NDIM;j++){
1.220 brouard 15913: printf("%f ",matcov[i][j]);
15914: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 15915: }
1.203 brouard 15916: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 15917: }
15918:
15919: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 15920: for (i=1;i<=NDIM;i++) {
1.126 brouard 15921: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 15922: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
15923: }
1.302 brouard 15924: lsurv=vector(agegomp,AGESUP);
15925: lpop=vector(agegomp,AGESUP);
15926: tpop=vector(agegomp,AGESUP);
1.126 brouard 15927: lsurv[agegomp]=100000;
15928:
15929: for (k=agegomp;k<=AGESUP;k++) {
15930: agemortsup=k;
15931: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
15932: }
15933:
15934: for (k=agegomp;k<agemortsup;k++)
15935: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
15936:
15937: for (k=agegomp;k<agemortsup;k++){
15938: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
15939: sumlpop=sumlpop+lpop[k];
15940: }
15941:
15942: tpop[agegomp]=sumlpop;
15943: for (k=agegomp;k<(agemortsup-3);k++){
15944: /* tpop[k+1]=2;*/
15945: tpop[k+1]=tpop[k]-lpop[k];
15946: }
15947:
15948:
15949: printf("\nAge lx qx dx Lx Tx e(x)\n");
15950: for (k=agegomp;k<(agemortsup-2);k++)
15951: printf("%d %.0lf %lf %.0lf %.0lf %.0lf %lf\n",k,lsurv[k],p[1]*exp(p[2]*(k-agegomp)),(p[1]*exp(p[2]*(k-agegomp)))*lsurv[k],lpop[k],tpop[k],tpop[k]/lsurv[k]);
15952:
15953:
15954: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 15955: ageminpar=50;
15956: agemaxpar=100;
1.194 brouard 15957: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
15958: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15959: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15960: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
15961: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15962: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15963: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 15964: }else{
15965: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
15966: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
1.201 brouard 15967: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 15968: }
1.201 brouard 15969: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 15970: stepm, weightopt,\
15971: model,imx,p,matcov,agemortsup);
15972:
1.302 brouard 15973: free_vector(lsurv,agegomp,AGESUP);
15974: free_vector(lpop,agegomp,AGESUP);
15975: free_vector(tpop,agegomp,AGESUP);
1.220 brouard 15976: free_matrix(ximort,1,NDIM,1,NDIM);
1.290 brouard 15977: free_ivector(dcwave,firstobs,lastobs);
15978: free_vector(agecens,firstobs,lastobs);
15979: free_vector(ageexmed,firstobs,lastobs);
15980: free_ivector(cens,firstobs,lastobs);
1.220 brouard 15981: #ifdef GSL
1.136 brouard 15982: #endif
1.186 brouard 15983: } /* Endof if mle==-3 mortality only */
1.205 brouard 15984: /* Standard */
15985: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
15986: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
15987: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 15988: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 15989: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
15990: for (k=1; k<=npar;k++)
15991: printf(" %d %8.5f",k,p[k]);
15992: printf("\n");
1.205 brouard 15993: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
15994: /* mlikeli uses func not funcone */
1.247 brouard 15995: /* for(i=1;i<nlstate;i++){ */
15996: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15997: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15998: /* } */
1.205 brouard 15999: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
16000: }
16001: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
16002: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
16003: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
16004: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
16005: }
16006: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 16007: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
16008: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
1.335 brouard 16009: /* exit(0); */
1.126 brouard 16010: for (k=1; k<=npar;k++)
16011: printf(" %d %8.5f",k,p[k]);
16012: printf("\n");
16013:
16014: /*--------- results files --------------*/
1.283 brouard 16015: /* fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, weightopt,model); */
1.126 brouard 16016:
16017:
16018: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 16019: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
1.126 brouard 16020: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 16021:
16022: printf("#model= 1 + age ");
16023: fprintf(ficres,"#model= 1 + age ");
16024: fprintf(ficlog,"#model= 1 + age ");
16025: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
16026: </ul>", model);
16027:
16028: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
16029: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
16030: if(nagesqr==1){
16031: printf(" + age*age ");
16032: fprintf(ficres," + age*age ");
16033: fprintf(ficlog," + age*age ");
16034: fprintf(fichtm, "<th>+ age*age</th>");
16035: }
1.362 brouard 16036: for(j=1;j <=ncovmodel-2-nagesqr;j++){
1.319 brouard 16037: if(Typevar[j]==0) {
16038: printf(" + V%d ",Tvar[j]);
16039: fprintf(ficres," + V%d ",Tvar[j]);
16040: fprintf(ficlog," + V%d ",Tvar[j]);
16041: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
16042: }else if(Typevar[j]==1) {
16043: printf(" + V%d*age ",Tvar[j]);
16044: fprintf(ficres," + V%d*age ",Tvar[j]);
16045: fprintf(ficlog," + V%d*age ",Tvar[j]);
16046: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
16047: }else if(Typevar[j]==2) {
16048: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16049: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16050: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16051: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 16052: }else if(Typevar[j]==3) { /* TO VERIFY */
16053: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16054: fprintf(ficres," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16055: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
16056: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.319 brouard 16057: }
16058: }
16059: printf("\n");
16060: fprintf(ficres,"\n");
16061: fprintf(ficlog,"\n");
16062: fprintf(fichtm, "</tr>");
16063: fprintf(fichtm, "\n");
16064:
16065:
1.126 brouard 16066: for(i=1,jk=1; i <=nlstate; i++){
16067: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 16068: if (k != i) {
1.319 brouard 16069: fprintf(fichtm, "<tr>");
1.225 brouard 16070: printf("%d%d ",i,k);
16071: fprintf(ficlog,"%d%d ",i,k);
16072: fprintf(ficres,"%1d%1d ",i,k);
1.319 brouard 16073: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 16074: for(j=1; j <=ncovmodel; j++){
16075: printf("%12.7f ",p[jk]);
16076: fprintf(ficlog,"%12.7f ",p[jk]);
16077: fprintf(ficres,"%12.7f ",p[jk]);
1.319 brouard 16078: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
1.225 brouard 16079: jk++;
16080: }
16081: printf("\n");
16082: fprintf(ficlog,"\n");
16083: fprintf(ficres,"\n");
1.319 brouard 16084: fprintf(fichtm, "</tr>\n");
1.225 brouard 16085: }
1.126 brouard 16086: }
16087: }
1.319 brouard 16088: /* fprintf(fichtm,"</tr>\n"); */
16089: fprintf(fichtm,"</table>\n");
16090: fprintf(fichtm, "\n");
16091:
1.203 brouard 16092: if(mle != 0){
16093: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 16094: ftolhess=ftol; /* Usually correct */
1.203 brouard 16095: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
16096: printf("Parameters and 95%% confidence intervals\n W is simply the result of the division of the parameter by the square root of covariance of the parameter.\n And Wald-based confidence intervals plus and minus 1.96 * W .\n But be careful that parameters are highly correlated because incidence of disability is highly correlated to incidence of recovery.\n It might be better to visualize the covariance matrix. See the page 'Matrix of variance-covariance of one-step probabilities' and its graphs.\n");
16097: fprintf(ficlog, "Parameters, Wald tests and Wald-based confidence intervals\n W is simply the result of the division of the parameter by the square root of covariance of the parameter.\n And Wald-based confidence intervals plus and minus 1.96 * W \n It might be better to visualize the covariance matrix. See the page 'Matrix of variance-covariance of one-step probabilities' and its graphs.\n");
1.322 brouard 16098: fprintf(fichtm, "\n<p>The Wald test results are output only if the maximimzation of the Likelihood is performed (mle=1)\n</br>Parameters, Wald tests and Wald-based confidence intervals\n</br> W is simply the result of the division of the parameter by the square root of covariance of the parameter.\n</br> And Wald-based confidence intervals plus and minus 1.96 * W \n </br> It might be better to visualize the covariance matrix. See the page '<a href=\"%s\">Matrix of variance-covariance of one-step probabilities and its graphs</a>'.\n</br>",optionfilehtmcov);
1.319 brouard 16099: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
16100: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
16101: if(nagesqr==1){
16102: printf(" + age*age ");
16103: fprintf(ficres," + age*age ");
16104: fprintf(ficlog," + age*age ");
16105: fprintf(fichtm, "<th>+ age*age</th>");
16106: }
1.362 brouard 16107: for(j=1;j <=ncovmodel-2-nagesqr;j++){
1.319 brouard 16108: if(Typevar[j]==0) {
16109: printf(" + V%d ",Tvar[j]);
16110: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
16111: }else if(Typevar[j]==1) {
16112: printf(" + V%d*age ",Tvar[j]);
16113: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
16114: }else if(Typevar[j]==2) {
16115: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 16116: }else if(Typevar[j]==3) { /* TO VERIFY */
16117: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.319 brouard 16118: }
16119: }
16120: fprintf(fichtm, "</tr>\n");
16121:
1.203 brouard 16122: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 16123: for(k=1; k <=(nlstate+ndeath); k++){
16124: if (k != i) {
1.319 brouard 16125: fprintf(fichtm, "<tr valign=top>");
1.225 brouard 16126: printf("%d%d ",i,k);
16127: fprintf(ficlog,"%d%d ",i,k);
1.319 brouard 16128: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 16129: for(j=1; j <=ncovmodel; j++){
1.319 brouard 16130: wald=p[jk]/sqrt(matcov[jk][jk]);
1.324 brouard 16131: printf("%12.7f(%12.7f) W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk],sqrt(matcov[jk][jk]), p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
16132: fprintf(ficlog,"%12.7f(%12.7f) W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk],sqrt(matcov[jk][jk]), p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
1.319 brouard 16133: if(fabs(wald) > 1.96){
1.321 brouard 16134: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
1.319 brouard 16135: }else{
16136: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
16137: }
1.324 brouard 16138: fprintf(fichtm,"W=%8.3f</br>",wald);
1.319 brouard 16139: fprintf(fichtm,"[%12.7f;%12.7f]</br></td>", p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
1.225 brouard 16140: jk++;
16141: }
16142: printf("\n");
16143: fprintf(ficlog,"\n");
1.319 brouard 16144: fprintf(fichtm, "</tr>\n");
1.225 brouard 16145: }
16146: }
1.193 brouard 16147: }
1.203 brouard 16148: } /* end of hesscov and Wald tests */
1.319 brouard 16149: fprintf(fichtm,"</table>\n");
1.225 brouard 16150:
1.203 brouard 16151: /* */
1.126 brouard 16152: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
16153: printf("# Scales (for hessian or gradient estimation)\n");
16154: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
16155: for(i=1,jk=1; i <=nlstate; i++){
16156: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 16157: if (j!=i) {
16158: fprintf(ficres,"%1d%1d",i,j);
16159: printf("%1d%1d",i,j);
16160: fprintf(ficlog,"%1d%1d",i,j);
16161: for(k=1; k<=ncovmodel;k++){
16162: printf(" %.5e",delti[jk]);
16163: fprintf(ficlog," %.5e",delti[jk]);
16164: fprintf(ficres," %.5e",delti[jk]);
16165: jk++;
16166: }
16167: printf("\n");
16168: fprintf(ficlog,"\n");
16169: fprintf(ficres,"\n");
16170: }
1.126 brouard 16171: }
16172: }
16173:
16174: fprintf(ficres,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
1.349 brouard 16175: if(mle >= 1) /* Too big for the screen */
1.126 brouard 16176: printf("# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
16177: fprintf(ficlog,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
16178: /* # 121 Var(a12)\n\ */
16179: /* # 122 Cov(b12,a12) Var(b12)\n\ */
16180: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
16181: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
16182: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
16183: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
16184: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
16185: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
16186:
16187:
16188: /* Just to have a covariance matrix which will be more understandable
16189: even is we still don't want to manage dictionary of variables
16190: */
16191: for(itimes=1;itimes<=2;itimes++){
16192: jj=0;
16193: for(i=1; i <=nlstate; i++){
1.225 brouard 16194: for(j=1; j <=nlstate+ndeath; j++){
16195: if(j==i) continue;
16196: for(k=1; k<=ncovmodel;k++){
16197: jj++;
16198: ca[0]= k+'a'-1;ca[1]='\0';
16199: if(itimes==1){
16200: if(mle>=1)
16201: printf("#%1d%1d%d",i,j,k);
16202: fprintf(ficlog,"#%1d%1d%d",i,j,k);
16203: fprintf(ficres,"#%1d%1d%d",i,j,k);
16204: }else{
16205: if(mle>=1)
16206: printf("%1d%1d%d",i,j,k);
16207: fprintf(ficlog,"%1d%1d%d",i,j,k);
16208: fprintf(ficres,"%1d%1d%d",i,j,k);
16209: }
16210: ll=0;
16211: for(li=1;li <=nlstate; li++){
16212: for(lj=1;lj <=nlstate+ndeath; lj++){
16213: if(lj==li) continue;
16214: for(lk=1;lk<=ncovmodel;lk++){
16215: ll++;
16216: if(ll<=jj){
16217: cb[0]= lk +'a'-1;cb[1]='\0';
16218: if(ll<jj){
16219: if(itimes==1){
16220: if(mle>=1)
16221: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16222: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16223: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16224: }else{
16225: if(mle>=1)
16226: printf(" %.5e",matcov[jj][ll]);
16227: fprintf(ficlog," %.5e",matcov[jj][ll]);
16228: fprintf(ficres," %.5e",matcov[jj][ll]);
16229: }
16230: }else{
16231: if(itimes==1){
16232: if(mle>=1)
16233: printf(" Var(%s%1d%1d)",ca,i,j);
16234: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
16235: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
16236: }else{
16237: if(mle>=1)
16238: printf(" %.7e",matcov[jj][ll]);
16239: fprintf(ficlog," %.7e",matcov[jj][ll]);
16240: fprintf(ficres," %.7e",matcov[jj][ll]);
16241: }
16242: }
16243: }
16244: } /* end lk */
16245: } /* end lj */
16246: } /* end li */
16247: if(mle>=1)
16248: printf("\n");
16249: fprintf(ficlog,"\n");
16250: fprintf(ficres,"\n");
16251: numlinepar++;
16252: } /* end k*/
16253: } /*end j */
1.126 brouard 16254: } /* end i */
16255: } /* end itimes */
16256:
16257: fflush(ficlog);
16258: fflush(ficres);
1.225 brouard 16259: while(fgets(line, MAXLINE, ficpar)) {
16260: /* If line starts with a # it is a comment */
16261: if (line[0] == '#') {
16262: numlinepar++;
16263: fputs(line,stdout);
16264: fputs(line,ficparo);
16265: fputs(line,ficlog);
1.299 brouard 16266: fputs(line,ficres);
1.225 brouard 16267: continue;
16268: }else
16269: break;
16270: }
16271:
1.209 brouard 16272: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
16273: /* ungetc(c,ficpar); */
16274: /* fgets(line, MAXLINE, ficpar); */
16275: /* fputs(line,stdout); */
16276: /* fputs(line,ficparo); */
16277: /* } */
16278: /* ungetc(c,ficpar); */
1.126 brouard 16279:
16280: estepm=0;
1.209 brouard 16281: if((num_filled=sscanf(line,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm, &ftolpl)) !=EOF){
1.225 brouard 16282:
16283: if (num_filled != 6) {
16284: printf("Error: Not 6 parameters in line, for example:agemin=60 agemax=95 bage=55 fage=95 estepm=24 ftolpl=6e-4\n, your line=%s . Probably you are running an older format.\n",line);
16285: fprintf(ficlog,"Error: Not 6 parameters in line, for example:agemin=60 agemax=95 bage=55 fage=95 estepm=24 ftolpl=6e-4\n, your line=%s . Probably you are running an older format.\n",line);
16286: goto end;
16287: }
16288: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
16289: }
16290: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
16291: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
16292:
1.209 brouard 16293: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 16294: if (estepm==0 || estepm < stepm) estepm=stepm;
16295: if (fage <= 2) {
16296: bage = ageminpar;
16297: fage = agemaxpar;
16298: }
16299:
16300: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 16301: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
16302: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 16303:
1.186 brouard 16304: /* Other stuffs, more or less useful */
1.254 brouard 16305: while(fgets(line, MAXLINE, ficpar)) {
16306: /* If line starts with a # it is a comment */
16307: if (line[0] == '#') {
16308: numlinepar++;
16309: fputs(line,stdout);
16310: fputs(line,ficparo);
16311: fputs(line,ficlog);
1.299 brouard 16312: fputs(line,ficres);
1.254 brouard 16313: continue;
16314: }else
16315: break;
16316: }
16317:
16318: if((num_filled=sscanf(line,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mov_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav)) !=EOF){
16319:
16320: if (num_filled != 7) {
16321: printf("Error: Not 7 (data)parameters in line but %d, for example:begin-prev-date=1/1/1990 end-prev-date=1/6/2004 mov_average=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
16322: fprintf(ficlog,"Error: Not 7 (data)parameters in line but %d, for example:begin-prev-date=1/1/1990 end-prev-date=1/6/2004 mov_average=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
16323: goto end;
16324: }
16325: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
16326: fprintf(ficparo,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
16327: fprintf(ficres,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
16328: fprintf(ficlog,"begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
1.126 brouard 16329: }
1.254 brouard 16330:
16331: while(fgets(line, MAXLINE, ficpar)) {
16332: /* If line starts with a # it is a comment */
16333: if (line[0] == '#') {
16334: numlinepar++;
16335: fputs(line,stdout);
16336: fputs(line,ficparo);
16337: fputs(line,ficlog);
1.299 brouard 16338: fputs(line,ficres);
1.254 brouard 16339: continue;
16340: }else
16341: break;
1.126 brouard 16342: }
16343:
16344:
16345: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
16346: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
16347:
1.254 brouard 16348: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
16349: if (num_filled != 1) {
16350: printf("Error: Not 1 (data)parameters in line but %d, for example:pop_based=0\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
16351: fprintf(ficlog,"Error: Not 1 (data)parameters in line but %d, for example: pop_based=1\n, your line=%s . Probably you are running an older format.\n",num_filled,line);
16352: goto end;
16353: }
16354: printf("pop_based=%d\n",popbased);
16355: fprintf(ficlog,"pop_based=%d\n",popbased);
16356: fprintf(ficparo,"pop_based=%d\n",popbased);
16357: fprintf(ficres,"pop_based=%d\n",popbased);
16358: }
16359:
1.258 brouard 16360: /* Results */
1.359 brouard 16361: /* Value of covariate in each resultine will be computed (if product) and sorted according to model rank */
1.332 brouard 16362: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
16363: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
1.307 brouard 16364: endishere=0;
1.258 brouard 16365: nresult=0;
1.308 brouard 16366: parameterline=0;
1.258 brouard 16367: do{
16368: if(!fgets(line, MAXLINE, ficpar)){
16369: endishere=1;
1.308 brouard 16370: parameterline=15;
1.258 brouard 16371: }else if (line[0] == '#') {
16372: /* If line starts with a # it is a comment */
1.254 brouard 16373: numlinepar++;
16374: fputs(line,stdout);
16375: fputs(line,ficparo);
16376: fputs(line,ficlog);
1.299 brouard 16377: fputs(line,ficres);
1.254 brouard 16378: continue;
1.258 brouard 16379: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
16380: parameterline=11;
1.296 brouard 16381: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
1.258 brouard 16382: parameterline=12;
1.307 brouard 16383: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
1.258 brouard 16384: parameterline=13;
1.307 brouard 16385: }
1.258 brouard 16386: else{
16387: parameterline=14;
1.254 brouard 16388: }
1.308 brouard 16389: switch (parameterline){ /* =0 only if only comments */
1.258 brouard 16390: case 11:
1.296 brouard 16391: if((num_filled=sscanf(line,"prevforecast=%d starting-proj-date=%lf/%lf/%lf final-proj-date=%lf/%lf/%lf mobil_average=%d\n",&prevfcast,&jproj1,&mproj1,&anproj1,&jproj2,&mproj2,&anproj2,&mobilavproj)) !=EOF && (num_filled == 8)){
16392: fprintf(ficparo,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
1.258 brouard 16393: printf("prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
16394: fprintf(ficlog,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
16395: fprintf(ficres,"prevforecast=%d starting-proj-date=%.lf/%.lf/%.lf final-proj-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevfcast,jproj1,mproj1,anproj1,jproj2,mproj2,anproj2,mobilavproj);
16396: /* day and month of proj2 are not used but only year anproj2.*/
1.273 brouard 16397: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
16398: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
1.296 brouard 16399: prvforecast = 1;
16400: }
16401: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
1.313 brouard 16402: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16403: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16404: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
1.296 brouard 16405: prvforecast = 2;
16406: }
16407: else {
16408: printf("Error: Not 8 (data)parameters in line but %d, for example:prevforecast=1 starting-proj-date=1/1/1990 final-proj-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevforecast=1 yearsfproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
16409: fprintf(ficlog,"Error: Not 8 (data)parameters in line but %d, for example:prevforecast=1 starting-proj-date=1/1/1990 final-proj-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevforecast=1 yearproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
16410: goto end;
1.258 brouard 16411: }
1.254 brouard 16412: break;
1.258 brouard 16413: case 12:
1.296 brouard 16414: if((num_filled=sscanf(line,"prevbackcast=%d starting-back-date=%lf/%lf/%lf final-back-date=%lf/%lf/%lf mobil_average=%d\n",&prevbcast,&jback1,&mback1,&anback1,&jback2,&mback2,&anback2,&mobilavproj)) !=EOF && (num_filled == 8)){
16415: fprintf(ficparo,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
16416: printf("prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
16417: fprintf(ficlog,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
16418: fprintf(ficres,"prevbackcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",prevbcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
16419: /* day and month of back2 are not used but only year anback2.*/
1.273 brouard 16420: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
16421: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
1.296 brouard 16422: prvbackcast = 1;
16423: }
16424: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
1.313 brouard 16425: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16426: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16427: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
1.296 brouard 16428: prvbackcast = 2;
16429: }
16430: else {
16431: printf("Error: Not 8 (data)parameters in line but %d, for example:prevbackcast=1 starting-back-date=1/1/1990 final-back-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevbackcast=1 yearsbproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
16432: fprintf(ficlog,"Error: Not 8 (data)parameters in line but %d, for example:prevbackcast=1 starting-back-date=1/1/1990 final-back-date=1/1/2000 mobil_average=0\nnor 3 (data)parameters, for example:prevbackcast=1 yearbproj=10 mobil_average=0. Your line=%s . You are running probably an older format.\n, ",num_filled,line);
16433: goto end;
1.258 brouard 16434: }
1.230 brouard 16435: break;
1.258 brouard 16436: case 13:
1.332 brouard 16437: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
1.307 brouard 16438: nresult++; /* Sum of resultlines */
1.342 brouard 16439: /* printf("Result %d: result:%s\n",nresult, resultlineori); */
1.332 brouard 16440: /* removefirstspace(&resultlineori); */
16441:
16442: if(strstr(resultlineori,"v") !=0){
16443: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
16444: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
16445: return 1;
16446: }
16447: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
1.342 brouard 16448: /* printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori); */
1.318 brouard 16449: if(nresult > MAXRESULTLINESPONE-1){
16450: printf("ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\nYou can use the 'r' parameter file '%s' which uses option mle=0 to get other results. ",MAXRESULTLINESPONE-1,nresult,rfileres);
16451: fprintf(ficlog,"ERROR: Current version of IMaCh limits the number of resultlines to %d, you used %d\nYou can use the 'r' parameter file '%s' which uses option mle=0 to get other results. ",MAXRESULTLINESPONE-1,nresult,rfileres);
1.307 brouard 16452: goto end;
16453: }
1.332 brouard 16454:
1.310 brouard 16455: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.314 brouard 16456: fprintf(ficparo,"result: %s\n",resultline);
16457: fprintf(ficres,"result: %s\n",resultline);
16458: fprintf(ficlog,"result: %s\n",resultline);
1.310 brouard 16459: } else
16460: goto end;
1.307 brouard 16461: break;
16462: case 14:
16463: printf("Error: Unknown command '%s'\n",line);
16464: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
1.314 brouard 16465: if(line[0] == ' ' || line[0] == '\n'){
16466: printf("It should not be an empty line '%s'\n",line);
16467: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
16468: }
1.307 brouard 16469: if(ncovmodel >=2 && nresult==0 ){
16470: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
16471: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
1.258 brouard 16472: }
1.307 brouard 16473: /* goto end; */
16474: break;
1.308 brouard 16475: case 15:
16476: printf("End of resultlines.\n");
16477: fprintf(ficlog,"End of resultlines.\n");
16478: break;
16479: default: /* parameterline =0 */
1.307 brouard 16480: nresult=1;
16481: decoderesult(".",nresult ); /* No covariate */
1.258 brouard 16482: } /* End switch parameterline */
16483: }while(endishere==0); /* End do */
1.126 brouard 16484:
1.230 brouard 16485: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 16486: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 16487:
16488: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 16489: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 16490: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 16491: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16492: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 16493: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 16494: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16495: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 16496: }else{
1.270 brouard 16497: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
1.296 brouard 16498: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
16499: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
16500: if(prvforecast==1){
16501: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
16502: jprojd=jproj1;
16503: mprojd=mproj1;
16504: anprojd=anproj1;
16505: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
16506: jprojf=jproj2;
16507: mprojf=mproj2;
16508: anprojf=anproj2;
16509: } else if(prvforecast == 2){
16510: dateprojd=dateintmean;
16511: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
16512: dateprojf=dateintmean+yrfproj;
16513: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
16514: }
16515: if(prvbackcast==1){
16516: datebackd=(jback1+12*mback1+365*anback1)/365;
16517: jbackd=jback1;
16518: mbackd=mback1;
16519: anbackd=anback1;
16520: datebackf=(jback2+12*mback2+365*anback2)/365;
16521: jbackf=jback2;
16522: mbackf=mback2;
16523: anbackf=anback2;
16524: } else if(prvbackcast == 2){
16525: datebackd=dateintmean;
16526: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
16527: datebackf=dateintmean-yrbproj;
16528: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
16529: }
16530:
1.350 brouard 16531: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);/* HERE valgrind Tvard*/
1.220 brouard 16532: }
16533: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.296 brouard 16534: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
16535: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
1.220 brouard 16536:
1.225 brouard 16537: /*------------ free_vector -------------*/
16538: /* chdir(path); */
1.220 brouard 16539:
1.215 brouard 16540: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
16541: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
16542: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
16543: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.290 brouard 16544: free_lvector(num,firstobs,lastobs);
16545: free_vector(agedc,firstobs,lastobs);
1.126 brouard 16546: /*free_matrix(covar,0,NCOVMAX,1,n);*/
16547: /*free_matrix(covar,1,NCOVMAX,1,n);*/
16548: fclose(ficparo);
16549: fclose(ficres);
1.220 brouard 16550:
16551:
1.186 brouard 16552: /* Other results (useful)*/
1.220 brouard 16553:
16554:
1.126 brouard 16555: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 16556: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
16557: prlim=matrix(1,nlstate,1,nlstate);
1.332 brouard 16558: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
1.209 brouard 16559: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 16560: fclose(ficrespl);
16561:
16562: /*------------- h Pij x at various ages ------------*/
1.180 brouard 16563: /*#include "hpijx.h"*/
1.332 brouard 16564: /** h Pij x Probability to be in state j at age x+h being in i at x, for each combination k of dummies in the model line or to nres?*/
16565: /* calls hpxij with combination k */
1.180 brouard 16566: hPijx(p, bage, fage);
1.145 brouard 16567: fclose(ficrespij);
1.227 brouard 16568:
1.220 brouard 16569: /* ncovcombmax= pow(2,cptcoveff); */
1.332 brouard 16570: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
1.145 brouard 16571: k=1;
1.126 brouard 16572: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 16573:
1.269 brouard 16574: /* Prevalence for each covariate combination in probs[age][status][cov] */
16575: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16576: for(i=AGEINF;i<=AGESUP;i++)
1.219 brouard 16577: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 16578: for(k=1;k<=ncovcombmax;k++)
16579: probs[i][j][k]=0.;
1.269 brouard 16580: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
16581: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.219 brouard 16582: if (mobilav!=0 ||mobilavproj !=0 ) {
1.269 brouard 16583: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16584: for(i=AGEINF;i<=AGESUP;i++)
1.268 brouard 16585: for(j=1;j<=nlstate+ndeath;j++)
1.227 brouard 16586: for(k=1;k<=ncovcombmax;k++)
16587: mobaverages[i][j][k]=0.;
1.219 brouard 16588: mobaverage=mobaverages;
16589: if (mobilav!=0) {
1.235 brouard 16590: printf("Movingaveraging observed prevalence\n");
1.258 brouard 16591: fprintf(ficlog,"Movingaveraging observed prevalence\n");
1.227 brouard 16592: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
16593: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
16594: printf(" Error in movingaverage mobilav=%d\n",mobilav);
16595: }
1.269 brouard 16596: } else if (mobilavproj !=0) {
1.235 brouard 16597: printf("Movingaveraging projected observed prevalence\n");
1.258 brouard 16598: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
1.227 brouard 16599: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
16600: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
16601: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
16602: }
1.269 brouard 16603: }else{
16604: printf("Internal error moving average\n");
16605: fflush(stdout);
16606: exit(1);
1.219 brouard 16607: }
16608: }/* end if moving average */
1.227 brouard 16609:
1.126 brouard 16610: /*---------- Forecasting ------------------*/
1.296 brouard 16611: if(prevfcast==1){
16612: /* /\* if(stepm ==1){*\/ */
16613: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16614: /*This done previously after freqsummary.*/
16615: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
16616: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
16617:
16618: /* } else if (prvforecast==2){ */
16619: /* /\* if(stepm ==1){*\/ */
16620: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16621: /* } */
16622: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
16623: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
1.126 brouard 16624: }
1.269 brouard 16625:
1.296 brouard 16626: /* Prevbcasting */
16627: if(prevbcast==1){
1.219 brouard 16628: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16629: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16630: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16631:
16632: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
16633:
16634: bprlim=matrix(1,nlstate,1,nlstate);
1.269 brouard 16635:
1.219 brouard 16636: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
16637: fclose(ficresplb);
16638:
1.222 brouard 16639: hBijx(p, bage, fage, mobaverage);
16640: fclose(ficrespijb);
1.219 brouard 16641:
1.296 brouard 16642: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
16643: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
16644: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
16645: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
16646: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
16647: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
16648:
16649:
1.269 brouard 16650: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 16651:
16652:
1.269 brouard 16653: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.219 brouard 16654: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16655: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16656: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
1.296 brouard 16657: } /* end Prevbcasting */
1.268 brouard 16658:
1.186 brouard 16659:
16660: /* ------ Other prevalence ratios------------ */
1.126 brouard 16661:
1.215 brouard 16662: free_ivector(wav,1,imx);
16663: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
16664: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
16665: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 16666:
16667:
1.127 brouard 16668: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 16669:
1.201 brouard 16670: strcpy(filerese,"E_");
16671: strcat(filerese,fileresu);
1.126 brouard 16672: if((ficreseij=fopen(filerese,"w"))==NULL) {
16673: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16674: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16675: }
1.208 brouard 16676: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
16677: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 16678:
16679: pstamp(ficreseij);
1.219 brouard 16680:
1.351 brouard 16681: /* i1=pow(2,cptcoveff); /\* Number of combination of dummy covariates *\/ */
16682: /* if (cptcovn < 1){i1=1;} */
1.235 brouard 16683:
1.351 brouard 16684: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
16685: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
16686: /* if(i1 != 1 && TKresult[nres]!= k) */
16687: /* continue; */
1.219 brouard 16688: fprintf(ficreseij,"\n#****** ");
1.235 brouard 16689: printf("\n#****** ");
1.351 brouard 16690: for(j=1;j<=cptcovs;j++){
16691: /* for(j=1;j<=cptcoveff;j++) { */
16692: /* fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16693: fprintf(ficreseij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16694: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16695: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.235 brouard 16696: }
16697: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
1.337 brouard 16698: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
16699: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
1.219 brouard 16700: }
16701: fprintf(ficreseij,"******\n");
1.235 brouard 16702: printf("******\n");
1.219 brouard 16703:
16704: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16705: oldm=oldms;savm=savms;
1.330 brouard 16706: /* printf("HELLO Entering evsij bage=%d fage=%d k=%d estepm=%d nres=%d\n",(int) bage, (int)fage, k, estepm, nres); */
1.235 brouard 16707: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 16708:
1.219 brouard 16709: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 16710: }
16711: fclose(ficreseij);
1.208 brouard 16712: printf("done evsij\n");fflush(stdout);
16713: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.269 brouard 16714:
1.218 brouard 16715:
1.227 brouard 16716: /*---------- State-specific expectancies and variances ------------*/
1.336 brouard 16717: /* Should be moved in a function */
1.201 brouard 16718: strcpy(filerest,"T_");
16719: strcat(filerest,fileresu);
1.127 brouard 16720: if((ficrest=fopen(filerest,"w"))==NULL) {
16721: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
16722: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
16723: }
1.208 brouard 16724: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
16725: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.201 brouard 16726: strcpy(fileresstde,"STDE_");
16727: strcat(fileresstde,fileresu);
1.126 brouard 16728: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 16729: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
16730: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 16731: }
1.227 brouard 16732: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
16733: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 16734:
1.201 brouard 16735: strcpy(filerescve,"CVE_");
16736: strcat(filerescve,fileresu);
1.126 brouard 16737: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 16738: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
16739: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 16740: }
1.227 brouard 16741: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
16742: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 16743:
1.201 brouard 16744: strcpy(fileresv,"V_");
16745: strcat(fileresv,fileresu);
1.126 brouard 16746: if((ficresvij=fopen(fileresv,"w"))==NULL) {
16747: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
16748: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
16749: }
1.227 brouard 16750: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
16751: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 16752:
1.235 brouard 16753: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
16754: if (cptcovn < 1){i1=1;}
16755:
1.334 brouard 16756: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
16757: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
16758: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
16759: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
16760: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
16761: /* */
16762: if(i1 != 1 && TKresult[nres]!= k) /* TKresult[nres] is the combination of this nres resultline. All the i1 combinations are not output */
1.235 brouard 16763: continue;
1.359 brouard 16764: printf("\n# model=1+age+%s \n#****** Result for:", model); /* HERE model is empty */
16765: fprintf(ficrest,"\n# model=1+age+%s \n#****** Result for:", model);
16766: fprintf(ficlog,"\n# model=1+age+%s \n#****** Result for:", model);
1.334 brouard 16767: /* It might not be a good idea to mix dummies and quantitative */
16768: /* for(j=1;j<=cptcoveff;j++){ /\* j=resultpos. Could be a loop on cptcovs: number of single dummy covariate in the result line as well as in the model *\/ */
16769: for(j=1;j<=cptcovs;j++){ /* j=resultpos. Could be a loop on cptcovs: number of single covariate (dummy or quantitative) in the result line as well as in the model */
16770: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
16771: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
16772: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
16773: * (V5 is quanti) V4 and V3 are dummies
16774: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
16775: * l=1 l=2
16776: * k=1 1 1 0 0
16777: * k=2 2 1 1 0
16778: * k=3 [1] [2] 0 1
16779: * k=4 2 2 1 1
16780: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
16781: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
16782: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
16783: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
16784: */
16785: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
16786: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
16787: /* We give up with the combinations!! */
1.342 brouard 16788: /* if(debugILK) */
16789: /* printf("\n j=%d In computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d Fixed[modelresult[nres][j]]=%d\n", j, nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff,Fixed[modelresult[nres][j]]); /\* end if dummy or quanti *\/ */
1.334 brouard 16790:
16791: if(Dummy[modelresult[nres][j]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to j in resultline */
1.344 brouard 16792: /* printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][j]); /\* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline *\/ */ /* TinvDoQresult[nres][Name of the variable] */
16793: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordered by the covariate values in the resultline */
16794: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
16795: fprintf(ficrest,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); /* Output of each value for the combination TKresult[nres], ordere by the covariate values in the resultline */
1.334 brouard 16796: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16797: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16798: }else{
16799: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16800: }
16801: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16802: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16803: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
16804: /* For each selected (single) quantitative value */
1.337 brouard 16805: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16806: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16807: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
1.334 brouard 16808: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16809: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16810: }else{
16811: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16812: }
16813: }else{
16814: printf("Error in computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d \n", nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff); /* end if dummy or quanti */
16815: fprintf(ficlog,"Error in computing T_ Dummy[modelresult[%d][%d]]=%d, modelresult[%d][%d]=%d cptcovs=%d, cptcoveff=%d \n", nres, j, Dummy[modelresult[nres][j]],nres,j,modelresult[nres][j],cptcovs, cptcoveff); /* end if dummy or quanti */
16816: exit(1);
16817: }
1.335 brouard 16818: } /* End loop for each variable in the resultline */
1.334 brouard 16819: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
16820: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
16821: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16822: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16823: /* } */
1.208 brouard 16824: fprintf(ficrest,"******\n");
1.227 brouard 16825: fprintf(ficlog,"******\n");
16826: printf("******\n");
1.208 brouard 16827:
16828: fprintf(ficresstdeij,"\n#****** ");
16829: fprintf(ficrescveij,"\n#****** ");
1.337 brouard 16830: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
16831: /* But it won't be sorted and depends on how the resultline is ordered */
1.225 brouard 16832: for(j=1;j<=cptcoveff;j++) {
1.334 brouard 16833: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16834: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16835: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16836: }
16837: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value, TvarsQind gives the position of a quantitative in model equation */
1.337 brouard 16838: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
16839: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
1.235 brouard 16840: }
1.208 brouard 16841: fprintf(ficresstdeij,"******\n");
16842: fprintf(ficrescveij,"******\n");
16843:
16844: fprintf(ficresvij,"\n#****** ");
1.238 brouard 16845: /* pstamp(ficresvij); */
1.225 brouard 16846: for(j=1;j<=cptcoveff;j++)
1.335 brouard 16847: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16848: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
1.235 brouard 16849: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
1.332 brouard 16850: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
1.337 brouard 16851: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
1.235 brouard 16852: }
1.208 brouard 16853: fprintf(ficresvij,"******\n");
16854:
16855: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16856: oldm=oldms;savm=savms;
1.235 brouard 16857: printf(" cvevsij ");
16858: fprintf(ficlog, " cvevsij ");
16859: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 16860: printf(" end cvevsij \n ");
16861: fprintf(ficlog, " end cvevsij \n ");
16862:
16863: /*
16864: */
16865: /* goto endfree; */
16866:
16867: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16868: pstamp(ficrest);
16869:
1.269 brouard 16870: epj=vector(1,nlstate+1);
1.208 brouard 16871: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 16872: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
16873: cptcod= 0; /* To be deleted */
1.360 brouard 16874: printf("varevsij vpopbased=%d popbased=%d \n",vpopbased,popbased);
16875: fprintf(ficlog, "varevsij vpopbased=%d popbased=%d \n",vpopbased,popbased);
1.361 brouard 16876: /* Call to varevsij to get cov(e.i, e.j)= vareij[i][j][(int)age]=sum_h sum_k trgrad(h_p.i) V(theta) grad(k_p.k) Equation 20 */
16877: /* Depending of popbased which changes the prevalences, either cross-sectional or period */
1.235 brouard 16878: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart, nres); /* cptcod not initialized Intel */
1.360 brouard 16879: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each state\n\
16880: # (these are weighted average of eij where weights are ");
1.227 brouard 16881: if(vpopbased==1)
1.360 brouard 16882: fprintf(ficrest,"the age specific prevalence observed (cross-sectionally) in the population i.e cross-sectionally)\n in each health state (popbased=1) (mobilav=%d)\n",mobilav);
1.227 brouard 16883: else
1.360 brouard 16884: fprintf(ficrest,"the age specific forward period (stable) prevalences in each state) \n");
16885: fprintf(ficrest,"# with proportions of time spent in each state with standard error (on the right of the table.\n ");
1.335 brouard 16886: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
1.227 brouard 16887: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
1.360 brouard 16888: for (i=1;i<=nlstate;i++) fprintf(ficrest," %% e.%d/e.. (std) ",i);
1.227 brouard 16889: fprintf(ficrest,"\n");
16890: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
1.288 brouard 16891: printf("Computing age specific forward period (stable) prevalences in each health state \n");
16892: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 16893: for(age=bage; age <=fage ;age++){
1.235 brouard 16894: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 16895: if (vpopbased==1) {
16896: if(mobilav ==0){
16897: for(i=1; i<=nlstate;i++)
16898: prlim[i][i]=probs[(int)age][i][k];
16899: }else{ /* mobilav */
16900: for(i=1; i<=nlstate;i++)
16901: prlim[i][i]=mobaverage[(int)age][i][k];
16902: }
16903: }
1.219 brouard 16904:
1.227 brouard 16905: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
16906: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
16907: /* printf(" age %4.0f ",age); */
16908: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
16909: for(i=1, epj[j]=0.;i <=nlstate;i++) {
16910: epj[j] += prlim[i][i]*eij[i][j][(int)age];
16911: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
16912: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
16913: }
1.361 brouard 16914: epj[nlstate+1] +=epj[j]; /* epp=sum_j epj = sum_j sum_i w_i e_ij */
1.227 brouard 16915: }
16916: /* printf(" age %4.0f \n",age); */
1.219 brouard 16917:
1.361 brouard 16918: for(i=1, vepp=0.;i <=nlstate;i++) /* Variance of total life expectancy e.. */
1.227 brouard 16919: for(j=1;j <=nlstate;j++)
1.361 brouard 16920: vepp += vareij[i][j][(int)age]; /* sum_i sum_j cov(e.i, e.j) = var(e..) */
1.227 brouard 16921: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
1.361 brouard 16922: /* vareij[i][j] is the covariance cov(e.i, e.j) and vareij[j][j] is the variance of e.j */
1.227 brouard 16923: for(j=1;j <=nlstate;j++){
16924: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
16925: }
1.360 brouard 16926: /* And proportion of time spent in state j */
16927: /* $$ E[r(X,Y)-E(r(X,Y))]^2=[\frac{1}{\mu_y} -\frac{\mu_x}{{\mu_y}^2}]' Var(X,Y)[\frac{1}{\mu_y} -\frac{\mu_x}{{\mu_y}^2}]$$ */
1.361 brouard 16928: /* \frac{\mu_x^2}{\mu_y^2} ( \frac{\sigma^2_x}{\mu_x^2}-2\frac{\sigma_{xy}}{\mu_x\mu_y} +\frac{\sigma^2_y}{\mu_y^2}) */
16929: /* \frac{e_{.i}^2}{e_{..}^2} ( \frac{\Var e_{.i}}{e_{.i}^2}-2\frac{\Var e_{.i} + \sum_{j\ne i} \Cov e_{.j},e_{.i}}{e_{.i}e_{..}} +\frac{\Var e_{..}}{e_{..}^2})*/
16930: /*\mu_x = epj[j], \sigma^2_x = vareij[j][j][(int)age] and \mu_y=epj[nlstate+1], \sigma^2_y=vepp \sigmaxy= */
16931: /* vareij[j][j][(int)age]/epj[nlstate+1]^2 + vepp/epj[nlstate+1]^4 */
1.360 brouard 16932: for(j=1;j <=nlstate;j++){
16933: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[j]/epj[j]/epj[j]/epj[j] )); */
1.361 brouard 16934: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[j]/epj[j]/epj[j]/epj[j] )); */
16935:
16936: for(i=1,stdpercent=0.;i<=nlstate;i++){ /* Computing cov(e..,e.j)=cov(sum_i e.i,e.j)=sum_i cov(e.i, e.j) */
16937: stdpercent += vareij[i][j][(int)age];
16938: }
16939: stdpercent= epj[j]*epj[j]/epj[nlstate+1]/epj[nlstate+1]* (vareij[j][j][(int)age]/epj[j]/epj[j]-2.*stdpercent/epj[j]/epj[nlstate+1]+ vepp/epj[nlstate+1]/epj[nlstate+1]);
16940: /* stdpercent= epj[j]*epj[j]/epj[nlstate+1]/epj[nlstate+1]*(vareij[j][j][(int)age]/epj[j]/epj[j] + vepp/epj[nlstate+1]/epj[nlstate+1]); */ /* Without covariance */
16941: /* fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt( vareij[j][j][(int)age]/epj[nlstate+1]/epj[nlstate+1] + epj[j]*epj[j]*vepp/epj[nlstate+1]/epj[nlstate+1]/epj[nlstate+1]/epj[nlstate+1] )); */
16942: fprintf(ficrest," %7.3f (%7.3f)", epj[j]/epj[nlstate+1], sqrt(stdpercent));
1.360 brouard 16943: }
1.227 brouard 16944: fprintf(ficrest,"\n");
16945: }
1.208 brouard 16946: } /* End vpopbased */
1.269 brouard 16947: free_vector(epj,1,nlstate+1);
1.208 brouard 16948: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
16949: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.235 brouard 16950: printf("done selection\n");fflush(stdout);
16951: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 16952:
1.335 brouard 16953: } /* End k selection or end covariate selection for nres */
1.227 brouard 16954:
16955: printf("done State-specific expectancies\n");fflush(stdout);
16956: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
16957:
1.335 brouard 16958: /* variance-covariance of forward period prevalence */
1.269 brouard 16959: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 16960:
1.227 brouard 16961:
1.290 brouard 16962: free_vector(weight,firstobs,lastobs);
1.351 brouard 16963: free_imatrix(Tvardk,0,NCOVMAX,1,2);
1.227 brouard 16964: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.290 brouard 16965: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
16966: free_matrix(anint,1,maxwav,firstobs,lastobs);
16967: free_matrix(mint,1,maxwav,firstobs,lastobs);
16968: free_ivector(cod,firstobs,lastobs);
1.227 brouard 16969: free_ivector(tab,1,NCOVMAX);
16970: fclose(ficresstdeij);
16971: fclose(ficrescveij);
16972: fclose(ficresvij);
16973: fclose(ficrest);
16974: fclose(ficpar);
16975:
16976:
1.126 brouard 16977: /*---------- End : free ----------------*/
1.219 brouard 16978: if (mobilav!=0 ||mobilavproj !=0)
1.269 brouard 16979: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
16980: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 16981: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
16982: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 16983: } /* mle==-3 arrives here for freeing */
1.227 brouard 16984: /* endfree:*/
1.359 brouard 16985: if(mle!=-3) free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
1.227 brouard 16986: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
16987: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
16988: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.341 brouard 16989: /* if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); */
16990: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs);
1.290 brouard 16991: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
16992: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
16993: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
1.227 brouard 16994: free_matrix(matcov,1,npar,1,npar);
16995: free_matrix(hess,1,npar,1,npar);
16996: /*free_vector(delti,1,npar);*/
16997: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16998: free_matrix(agev,1,maxwav,1,imx);
1.269 brouard 16999: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.227 brouard 17000: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
17001:
17002: free_ivector(ncodemax,1,NCOVMAX);
17003: free_ivector(ncodemaxwundef,1,NCOVMAX);
17004: free_ivector(Dummy,-1,NCOVMAX);
17005: free_ivector(Fixed,-1,NCOVMAX);
1.349 brouard 17006: free_ivector(DummyV,-1,NCOVMAX);
17007: free_ivector(FixedV,-1,NCOVMAX);
1.227 brouard 17008: free_ivector(Typevar,-1,NCOVMAX);
17009: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 17010: free_ivector(TvarsQ,1,NCOVMAX);
17011: free_ivector(TvarsQind,1,NCOVMAX);
17012: free_ivector(TvarsD,1,NCOVMAX);
1.330 brouard 17013: free_ivector(TnsdVar,1,NCOVMAX);
1.234 brouard 17014: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 17015: free_ivector(TvarFD,1,NCOVMAX);
17016: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 17017: free_ivector(TvarF,1,NCOVMAX);
17018: free_ivector(TvarFind,1,NCOVMAX);
17019: free_ivector(TvarV,1,NCOVMAX);
17020: free_ivector(TvarVind,1,NCOVMAX);
17021: free_ivector(TvarA,1,NCOVMAX);
17022: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 17023: free_ivector(TvarFQ,1,NCOVMAX);
17024: free_ivector(TvarFQind,1,NCOVMAX);
17025: free_ivector(TvarVD,1,NCOVMAX);
17026: free_ivector(TvarVDind,1,NCOVMAX);
17027: free_ivector(TvarVQ,1,NCOVMAX);
17028: free_ivector(TvarVQind,1,NCOVMAX);
1.349 brouard 17029: free_ivector(TvarAVVA,1,NCOVMAX);
17030: free_ivector(TvarAVVAind,1,NCOVMAX);
17031: free_ivector(TvarVVA,1,NCOVMAX);
17032: free_ivector(TvarVVAind,1,NCOVMAX);
1.339 brouard 17033: free_ivector(TvarVV,1,NCOVMAX);
17034: free_ivector(TvarVVind,1,NCOVMAX);
17035:
1.230 brouard 17036: free_ivector(Tvarsel,1,NCOVMAX);
17037: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 17038: free_ivector(Tposprod,1,NCOVMAX);
17039: free_ivector(Tprod,1,NCOVMAX);
17040: free_ivector(Tvaraff,1,NCOVMAX);
1.338 brouard 17041: free_ivector(invalidvarcomb,0,ncovcombmax);
1.227 brouard 17042: free_ivector(Tage,1,NCOVMAX);
17043: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 17044: free_ivector(TmodelInvind,1,NCOVMAX);
17045: free_ivector(TmodelInvQind,1,NCOVMAX);
1.332 brouard 17046:
1.359 brouard 17047: /* free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /\* Could be elsewhere ?*\/ */
1.332 brouard 17048:
1.227 brouard 17049: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
17050: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 17051: fflush(fichtm);
17052: fflush(ficgp);
17053:
1.227 brouard 17054:
1.126 brouard 17055: if((nberr >0) || (nbwarn>0)){
1.216 brouard 17056: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
17057: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
1.126 brouard 17058: }else{
17059: printf("End of Imach\n");
17060: fprintf(ficlog,"End of Imach\n");
17061: }
17062: printf("See log file on %s\n",filelog);
17063: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 17064: /*(void) gettimeofday(&end_time,&tzp);*/
17065: rend_time = time(NULL);
17066: end_time = *localtime(&rend_time);
17067: /* tml = *localtime(&end_time.tm_sec); */
17068: strcpy(strtend,asctime(&end_time));
1.126 brouard 17069: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
17070: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 17071: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 17072:
1.157 brouard 17073: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
17074: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
17075: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 17076: /* printf("Total time was %d uSec.\n", total_usecs);*/
17077: /* if(fileappend(fichtm,optionfilehtm)){ */
17078: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17079: fclose(fichtm);
17080: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
17081: fclose(fichtmcov);
17082: fclose(ficgp);
17083: fclose(ficlog);
17084: /*------ End -----------*/
1.227 brouard 17085:
1.281 brouard 17086:
17087: /* Executes gnuplot */
1.227 brouard 17088:
17089: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 17090: #ifdef WIN32
1.227 brouard 17091: if (_chdir(pathcd) != 0)
17092: printf("Can't move to directory %s!\n",path);
17093: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 17094: #else
1.227 brouard 17095: if(chdir(pathcd) != 0)
17096: printf("Can't move to directory %s!\n", path);
17097: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 17098: #endif
1.126 brouard 17099: printf("Current directory %s!\n",pathcd);
17100: /*strcat(plotcmd,CHARSEPARATOR);*/
17101: sprintf(plotcmd,"gnuplot");
1.157 brouard 17102: #ifdef _WIN32
1.126 brouard 17103: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
17104: #endif
17105: if(!stat(plotcmd,&info)){
1.158 brouard 17106: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 17107: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 17108: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 17109: }else
17110: strcpy(pplotcmd,plotcmd);
1.157 brouard 17111: #ifdef __unix
1.126 brouard 17112: strcpy(plotcmd,GNUPLOTPROGRAM);
17113: if(!stat(plotcmd,&info)){
1.158 brouard 17114: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 17115: }else
17116: strcpy(pplotcmd,plotcmd);
17117: #endif
17118: }else
17119: strcpy(pplotcmd,plotcmd);
17120:
17121: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 17122: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.292 brouard 17123: strcpy(pplotcmd,plotcmd);
1.227 brouard 17124:
1.126 brouard 17125: if((outcmd=system(plotcmd)) != 0){
1.292 brouard 17126: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 17127: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 17128: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.292 brouard 17129: if((outcmd=system(plotcmd)) != 0){
1.153 brouard 17130: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.292 brouard 17131: strcpy(plotcmd,pplotcmd);
17132: }
1.126 brouard 17133: }
1.158 brouard 17134: printf(" Successful, please wait...");
1.126 brouard 17135: while (z[0] != 'q') {
17136: /* chdir(path); */
1.154 brouard 17137: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 17138: scanf("%s",z);
17139: /* if (z[0] == 'c') system("./imach"); */
17140: if (z[0] == 'e') {
1.158 brouard 17141: #ifdef __APPLE__
1.152 brouard 17142: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 17143: #elif __linux
17144: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 17145: #else
1.152 brouard 17146: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 17147: #endif
17148: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
17149: system(pplotcmd);
1.126 brouard 17150: }
17151: else if (z[0] == 'g') system(plotcmd);
17152: else if (z[0] == 'q') exit(0);
17153: }
1.227 brouard 17154: end:
1.126 brouard 17155: while (z[0] != 'q') {
1.195 brouard 17156: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 17157: scanf("%s",z);
17158: }
1.283 brouard 17159: printf("End\n");
1.282 brouard 17160: exit(0);
1.126 brouard 17161: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>