Annotation of imach/src/imach.c, revision 1.359
1.359 ! brouard 1: /* $Id: imach.c,v 1.353 2023/05/08 18:48:22 brouard Exp $
1.126 brouard 2: $State: Exp $
1.359 ! brouard 3: $Log: imachprax.c,v $
! 4: Revision 1.6 2024/04/24 21:10:29 brouard
! 5: Summary: First IMaCh version using Brent Praxis software based on Buckhardt and Gegenfürtner C codes
1.358 brouard 6:
1.359 ! brouard 7: Revision 1.5 2023/10/09 09:10:01 brouard
! 8: Summary: trying to reconsider
1.357 brouard 9:
1.359 ! brouard 10: Revision 1.4 2023/06/22 12:50:51 brouard
! 11: Summary: stil on going
1.357 brouard 12:
1.359 ! brouard 13: Revision 1.3 2023/06/22 11:28:07 brouard
! 14: *** empty log message ***
1.356 brouard 15:
1.359 ! brouard 16: Revision 1.2 2023/06/22 11:22:40 brouard
! 17: Summary: with svd but not working yet
1.355 brouard 18:
1.354 brouard 19: Revision 1.353 2023/05/08 18:48:22 brouard
20: *** empty log message ***
21:
1.353 brouard 22: Revision 1.352 2023/04/29 10:46:21 brouard
23: *** empty log message ***
24:
1.352 brouard 25: Revision 1.351 2023/04/29 10:43:47 brouard
26: Summary: 099r45
27:
1.351 brouard 28: Revision 1.350 2023/04/24 11:38:06 brouard
29: *** empty log message ***
30:
1.350 brouard 31: Revision 1.349 2023/01/31 09:19:37 brouard
32: Summary: Improvements in models with age*Vn*Vm
33:
1.348 brouard 34: Revision 1.347 2022/09/18 14:36:44 brouard
35: Summary: version 0.99r42
36:
1.347 brouard 37: Revision 1.346 2022/09/16 13:52:36 brouard
38: * src/imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
39:
1.346 brouard 40: Revision 1.345 2022/09/16 13:40:11 brouard
41: Summary: Version 0.99r41
42:
43: * imach.c (Module): 0.99r41 Was an error when product of timevarying and fixed. Using FixedV[of name] now. Thank you Feinuo
44:
1.345 brouard 45: Revision 1.344 2022/09/14 19:33:30 brouard
46: Summary: version 0.99r40
47:
48: * imach.c (Module): Fixing names of variables in T_ (thanks to Feinuo)
49:
1.344 brouard 50: Revision 1.343 2022/09/14 14:22:16 brouard
51: Summary: version 0.99r39
52:
53: * imach.c (Module): Version 0.99r39 with colored dummy covariates
54: (fixed or time varying), using new last columns of
55: ILK_parameter.txt file.
56:
1.343 brouard 57: Revision 1.342 2022/09/11 19:54:09 brouard
58: Summary: 0.99r38
59:
60: * imach.c (Module): Adding timevarying products of any kinds,
61: should work before shifting cotvar from ncovcol+nqv columns in
62: order to have a correspondance between the column of cotvar and
63: the id of column.
64: (Module): Some cleaning and adding covariates in ILK.txt
65:
1.342 brouard 66: Revision 1.341 2022/09/11 07:58:42 brouard
67: Summary: Version 0.99r38
68:
69: After adding change in cotvar.
70:
1.341 brouard 71: Revision 1.340 2022/09/11 07:53:11 brouard
72: Summary: Version imach 0.99r37
73:
74: * imach.c (Module): Adding timevarying products of any kinds,
75: should work before shifting cotvar from ncovcol+nqv columns in
76: order to have a correspondance between the column of cotvar and
77: the id of column.
78:
1.340 brouard 79: Revision 1.339 2022/09/09 17:55:22 brouard
80: Summary: version 0.99r37
81:
82: * imach.c (Module): Many improvements for fixing products of fixed
83: timevarying as well as fixed * fixed, and test with quantitative
84: covariate.
85:
1.339 brouard 86: Revision 1.338 2022/09/04 17:40:33 brouard
87: Summary: 0.99r36
88:
89: * imach.c (Module): Now the easy runs i.e. without result or
90: model=1+age only did not work. The defautl combination should be 1
91: and not 0 because everything hasn't been tranformed yet.
92:
1.338 brouard 93: Revision 1.337 2022/09/02 14:26:02 brouard
94: Summary: version 0.99r35
95:
96: * src/imach.c: Version 0.99r35 because it outputs same results with
97: 1+age+V1+V1*age for females and 1+age for females only
98: (education=1 noweight)
99:
1.337 brouard 100: Revision 1.336 2022/08/31 09:52:36 brouard
101: *** empty log message ***
102:
1.336 brouard 103: Revision 1.335 2022/08/31 08:23:16 brouard
104: Summary: improvements...
105:
1.335 brouard 106: Revision 1.334 2022/08/25 09:08:41 brouard
107: Summary: In progress for quantitative
108:
1.334 brouard 109: Revision 1.333 2022/08/21 09:10:30 brouard
110: * src/imach.c (Module): Version 0.99r33 A lot of changes in
111: reassigning covariates: my first idea was that people will always
112: use the first covariate V1 into the model but in fact they are
113: producing data with many covariates and can use an equation model
114: with some of the covariate; it means that in a model V2+V3 instead
115: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
116: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
117: the equation model is restricted to two variables only (V2, V3)
118: and the combination for V2 should be codtabm(k,1) instead of
119: (codtabm(k,2), and the code should be
120: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
121: made. All of these should be simplified once a day like we did in
122: hpxij() for example by using precov[nres] which is computed in
123: decoderesult for each nres of each resultline. Loop should be done
124: on the equation model globally by distinguishing only product with
125: age (which are changing with age) and no more on type of
126: covariates, single dummies, single covariates.
127:
1.333 brouard 128: Revision 1.332 2022/08/21 09:06:25 brouard
129: Summary: Version 0.99r33
130:
131: * src/imach.c (Module): Version 0.99r33 A lot of changes in
132: reassigning covariates: my first idea was that people will always
133: use the first covariate V1 into the model but in fact they are
134: producing data with many covariates and can use an equation model
135: with some of the covariate; it means that in a model V2+V3 instead
136: of codtabm(k,Tvaraff[j]) which calculates for combination k, for
137: three covariates (V1, V2, V3) the value of Tvaraff[j], but in fact
138: the equation model is restricted to two variables only (V2, V3)
139: and the combination for V2 should be codtabm(k,1) instead of
140: (codtabm(k,2), and the code should be
141: codtabm(k,TnsdVar[Tvaraff[j]]. Many many changes have been
142: made. All of these should be simplified once a day like we did in
143: hpxij() for example by using precov[nres] which is computed in
144: decoderesult for each nres of each resultline. Loop should be done
145: on the equation model globally by distinguishing only product with
146: age (which are changing with age) and no more on type of
147: covariates, single dummies, single covariates.
148:
1.332 brouard 149: Revision 1.331 2022/08/07 05:40:09 brouard
150: *** empty log message ***
151:
1.331 brouard 152: Revision 1.330 2022/08/06 07:18:25 brouard
153: Summary: last 0.99r31
154:
155: * imach.c (Module): Version of imach using partly decoderesult to rebuild xpxij function
156:
1.330 brouard 157: Revision 1.329 2022/08/03 17:29:54 brouard
158: * imach.c (Module): Many errors in graphs fixed with Vn*age covariates.
159:
1.329 brouard 160: Revision 1.328 2022/07/27 17:40:48 brouard
161: Summary: valgrind bug fixed by initializing to zero DummyV as well as Tage
162:
1.328 brouard 163: Revision 1.327 2022/07/27 14:47:35 brouard
164: Summary: Still a problem for one-step probabilities in case of quantitative variables
165:
1.327 brouard 166: Revision 1.326 2022/07/26 17:33:55 brouard
167: Summary: some test with nres=1
168:
1.326 brouard 169: Revision 1.325 2022/07/25 14:27:23 brouard
170: Summary: r30
171:
172: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
173: coredumped, revealed by Feiuno, thank you.
174:
1.325 brouard 175: Revision 1.324 2022/07/23 17:44:26 brouard
176: *** empty log message ***
177:
1.324 brouard 178: Revision 1.323 2022/07/22 12:30:08 brouard
179: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
180:
1.323 brouard 181: Revision 1.322 2022/07/22 12:27:48 brouard
182: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
183:
1.322 brouard 184: Revision 1.321 2022/07/22 12:04:24 brouard
185: Summary: r28
186:
187: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
188:
1.321 brouard 189: Revision 1.320 2022/06/02 05:10:11 brouard
190: *** empty log message ***
191:
1.320 brouard 192: Revision 1.319 2022/06/02 04:45:11 brouard
193: * imach.c (Module): Adding the Wald tests from the log to the main
194: htm for better display of the maximum likelihood estimators.
195:
1.319 brouard 196: Revision 1.318 2022/05/24 08:10:59 brouard
197: * imach.c (Module): Some attempts to find a bug of wrong estimates
198: of confidencce intervals with product in the equation modelC
199:
1.318 brouard 200: Revision 1.317 2022/05/15 15:06:23 brouard
201: * imach.c (Module): Some minor improvements
202:
1.317 brouard 203: Revision 1.316 2022/05/11 15:11:31 brouard
204: Summary: r27
205:
1.316 brouard 206: Revision 1.315 2022/05/11 15:06:32 brouard
207: *** empty log message ***
208:
1.315 brouard 209: Revision 1.314 2022/04/13 17:43:09 brouard
210: * imach.c (Module): Adding link to text data files
211:
1.314 brouard 212: Revision 1.313 2022/04/11 15:57:42 brouard
213: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
214:
1.313 brouard 215: Revision 1.312 2022/04/05 21:24:39 brouard
216: *** empty log message ***
217:
1.312 brouard 218: Revision 1.311 2022/04/05 21:03:51 brouard
219: Summary: Fixed quantitative covariates
220:
221: Fixed covariates (dummy or quantitative)
222: with missing values have never been allowed but are ERRORS and
223: program quits. Standard deviations of fixed covariates were
224: wrongly computed. Mean and standard deviations of time varying
225: covariates are still not computed.
226:
1.311 brouard 227: Revision 1.310 2022/03/17 08:45:53 brouard
228: Summary: 99r25
229:
230: Improving detection of errors: result lines should be compatible with
231: the model.
232:
1.310 brouard 233: Revision 1.309 2021/05/20 12:39:14 brouard
234: Summary: Version 0.99r24
235:
1.309 brouard 236: Revision 1.308 2021/03/31 13:11:57 brouard
237: Summary: Version 0.99r23
238:
239:
240: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
241:
1.308 brouard 242: Revision 1.307 2021/03/08 18:11:32 brouard
243: Summary: 0.99r22 fixed bug on result:
244:
1.307 brouard 245: Revision 1.306 2021/02/20 15:44:02 brouard
246: Summary: Version 0.99r21
247:
248: * imach.c (Module): Fix bug on quitting after result lines!
249: (Module): Version 0.99r21
250:
1.306 brouard 251: Revision 1.305 2021/02/20 15:28:30 brouard
252: * imach.c (Module): Fix bug on quitting after result lines!
253:
1.305 brouard 254: Revision 1.304 2021/02/12 11:34:20 brouard
255: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
256:
1.304 brouard 257: Revision 1.303 2021/02/11 19:50:15 brouard
258: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
259:
1.303 brouard 260: Revision 1.302 2020/02/22 21:00:05 brouard
261: * (Module): imach.c Update mle=-3 (for computing Life expectancy
262: and life table from the data without any state)
263:
1.302 brouard 264: Revision 1.301 2019/06/04 13:51:20 brouard
265: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
266:
1.301 brouard 267: Revision 1.300 2019/05/22 19:09:45 brouard
268: Summary: version 0.99r19 of May 2019
269:
1.300 brouard 270: Revision 1.299 2019/05/22 18:37:08 brouard
271: Summary: Cleaned 0.99r19
272:
1.299 brouard 273: Revision 1.298 2019/05/22 18:19:56 brouard
274: *** empty log message ***
275:
1.298 brouard 276: Revision 1.297 2019/05/22 17:56:10 brouard
277: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
278:
1.297 brouard 279: Revision 1.296 2019/05/20 13:03:18 brouard
280: Summary: Projection syntax simplified
281:
282:
283: We can now start projections, forward or backward, from the mean date
284: of inteviews up to or down to a number of years of projection:
285: prevforecast=1 yearsfproj=15.3 mobil_average=0
286: or
287: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
288: or
289: prevbackcast=1 yearsbproj=12.3 mobil_average=1
290: or
291: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
292:
1.296 brouard 293: Revision 1.295 2019/05/18 09:52:50 brouard
294: Summary: doxygen tex bug
295:
1.295 brouard 296: Revision 1.294 2019/05/16 14:54:33 brouard
297: Summary: There was some wrong lines added
298:
1.294 brouard 299: Revision 1.293 2019/05/09 15:17:34 brouard
300: *** empty log message ***
301:
1.293 brouard 302: Revision 1.292 2019/05/09 14:17:20 brouard
303: Summary: Some updates
304:
1.292 brouard 305: Revision 1.291 2019/05/09 13:44:18 brouard
306: Summary: Before ncovmax
307:
1.291 brouard 308: Revision 1.290 2019/05/09 13:39:37 brouard
309: Summary: 0.99r18 unlimited number of individuals
310:
311: 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.
312:
1.290 brouard 313: Revision 1.289 2018/12/13 09:16:26 brouard
314: Summary: Bug for young ages (<-30) will be in r17
315:
1.289 brouard 316: Revision 1.288 2018/05/02 20:58:27 brouard
317: Summary: Some bugs fixed
318:
1.288 brouard 319: Revision 1.287 2018/05/01 17:57:25 brouard
320: Summary: Bug fixed by providing frequencies only for non missing covariates
321:
1.287 brouard 322: Revision 1.286 2018/04/27 14:27:04 brouard
323: Summary: some minor bugs
324:
1.286 brouard 325: Revision 1.285 2018/04/21 21:02:16 brouard
326: Summary: Some bugs fixed, valgrind tested
327:
1.285 brouard 328: Revision 1.284 2018/04/20 05:22:13 brouard
329: Summary: Computing mean and stdeviation of fixed quantitative variables
330:
1.284 brouard 331: Revision 1.283 2018/04/19 14:49:16 brouard
332: Summary: Some minor bugs fixed
333:
1.283 brouard 334: Revision 1.282 2018/02/27 22:50:02 brouard
335: *** empty log message ***
336:
1.282 brouard 337: Revision 1.281 2018/02/27 19:25:23 brouard
338: Summary: Adding second argument for quitting
339:
1.281 brouard 340: Revision 1.280 2018/02/21 07:58:13 brouard
341: Summary: 0.99r15
342:
343: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
344:
1.280 brouard 345: Revision 1.279 2017/07/20 13:35:01 brouard
346: Summary: temporary working
347:
1.279 brouard 348: Revision 1.278 2017/07/19 14:09:02 brouard
349: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
350:
1.278 brouard 351: Revision 1.277 2017/07/17 08:53:49 brouard
352: Summary: BOM files can be read now
353:
1.277 brouard 354: Revision 1.276 2017/06/30 15:48:31 brouard
355: Summary: Graphs improvements
356:
1.276 brouard 357: Revision 1.275 2017/06/30 13:39:33 brouard
358: Summary: Saito's color
359:
1.275 brouard 360: Revision 1.274 2017/06/29 09:47:08 brouard
361: Summary: Version 0.99r14
362:
1.274 brouard 363: Revision 1.273 2017/06/27 11:06:02 brouard
364: Summary: More documentation on projections
365:
1.273 brouard 366: Revision 1.272 2017/06/27 10:22:40 brouard
367: Summary: Color of backprojection changed from 6 to 5(yellow)
368:
1.272 brouard 369: Revision 1.271 2017/06/27 10:17:50 brouard
370: Summary: Some bug with rint
371:
1.271 brouard 372: Revision 1.270 2017/05/24 05:45:29 brouard
373: *** empty log message ***
374:
1.270 brouard 375: Revision 1.269 2017/05/23 08:39:25 brouard
376: Summary: Code into subroutine, cleanings
377:
1.269 brouard 378: Revision 1.268 2017/05/18 20:09:32 brouard
379: Summary: backprojection and confidence intervals of backprevalence
380:
1.268 brouard 381: Revision 1.267 2017/05/13 10:25:05 brouard
382: Summary: temporary save for backprojection
383:
1.267 brouard 384: Revision 1.266 2017/05/13 07:26:12 brouard
385: Summary: Version 0.99r13 (improvements and bugs fixed)
386:
1.266 brouard 387: Revision 1.265 2017/04/26 16:22:11 brouard
388: Summary: imach 0.99r13 Some bugs fixed
389:
1.265 brouard 390: Revision 1.264 2017/04/26 06:01:29 brouard
391: Summary: Labels in graphs
392:
1.264 brouard 393: Revision 1.263 2017/04/24 15:23:15 brouard
394: Summary: to save
395:
1.263 brouard 396: Revision 1.262 2017/04/18 16:48:12 brouard
397: *** empty log message ***
398:
1.262 brouard 399: Revision 1.261 2017/04/05 10:14:09 brouard
400: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
401:
1.261 brouard 402: Revision 1.260 2017/04/04 17:46:59 brouard
403: Summary: Gnuplot indexations fixed (humm)
404:
1.260 brouard 405: Revision 1.259 2017/04/04 13:01:16 brouard
406: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
407:
1.259 brouard 408: Revision 1.258 2017/04/03 10:17:47 brouard
409: Summary: Version 0.99r12
410:
411: Some cleanings, conformed with updated documentation.
412:
1.258 brouard 413: Revision 1.257 2017/03/29 16:53:30 brouard
414: Summary: Temp
415:
1.257 brouard 416: Revision 1.256 2017/03/27 05:50:23 brouard
417: Summary: Temporary
418:
1.256 brouard 419: Revision 1.255 2017/03/08 16:02:28 brouard
420: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
421:
1.255 brouard 422: Revision 1.254 2017/03/08 07:13:00 brouard
423: Summary: Fixing data parameter line
424:
1.254 brouard 425: Revision 1.253 2016/12/15 11:59:41 brouard
426: Summary: 0.99 in progress
427:
1.253 brouard 428: Revision 1.252 2016/09/15 21:15:37 brouard
429: *** empty log message ***
430:
1.252 brouard 431: Revision 1.251 2016/09/15 15:01:13 brouard
432: Summary: not working
433:
1.251 brouard 434: Revision 1.250 2016/09/08 16:07:27 brouard
435: Summary: continue
436:
1.250 brouard 437: Revision 1.249 2016/09/07 17:14:18 brouard
438: Summary: Starting values from frequencies
439:
1.249 brouard 440: Revision 1.248 2016/09/07 14:10:18 brouard
441: *** empty log message ***
442:
1.248 brouard 443: Revision 1.247 2016/09/02 11:11:21 brouard
444: *** empty log message ***
445:
1.247 brouard 446: Revision 1.246 2016/09/02 08:49:22 brouard
447: *** empty log message ***
448:
1.246 brouard 449: Revision 1.245 2016/09/02 07:25:01 brouard
450: *** empty log message ***
451:
1.245 brouard 452: Revision 1.244 2016/09/02 07:17:34 brouard
453: *** empty log message ***
454:
1.244 brouard 455: Revision 1.243 2016/09/02 06:45:35 brouard
456: *** empty log message ***
457:
1.243 brouard 458: Revision 1.242 2016/08/30 15:01:20 brouard
459: Summary: Fixing a lots
460:
1.242 brouard 461: Revision 1.241 2016/08/29 17:17:25 brouard
462: Summary: gnuplot problem in Back projection to fix
463:
1.241 brouard 464: Revision 1.240 2016/08/29 07:53:18 brouard
465: Summary: Better
466:
1.240 brouard 467: Revision 1.239 2016/08/26 15:51:03 brouard
468: Summary: Improvement in Powell output in order to copy and paste
469:
470: Author:
471:
1.239 brouard 472: Revision 1.238 2016/08/26 14:23:35 brouard
473: Summary: Starting tests of 0.99
474:
1.238 brouard 475: Revision 1.237 2016/08/26 09:20:19 brouard
476: Summary: to valgrind
477:
1.237 brouard 478: Revision 1.236 2016/08/25 10:50:18 brouard
479: *** empty log message ***
480:
1.236 brouard 481: Revision 1.235 2016/08/25 06:59:23 brouard
482: *** empty log message ***
483:
1.235 brouard 484: Revision 1.234 2016/08/23 16:51:20 brouard
485: *** empty log message ***
486:
1.234 brouard 487: Revision 1.233 2016/08/23 07:40:50 brouard
488: Summary: not working
489:
1.233 brouard 490: Revision 1.232 2016/08/22 14:20:21 brouard
491: Summary: not working
492:
1.232 brouard 493: Revision 1.231 2016/08/22 07:17:15 brouard
494: Summary: not working
495:
1.231 brouard 496: Revision 1.230 2016/08/22 06:55:53 brouard
497: Summary: Not working
498:
1.230 brouard 499: Revision 1.229 2016/07/23 09:45:53 brouard
500: Summary: Completing for func too
501:
1.229 brouard 502: Revision 1.228 2016/07/22 17:45:30 brouard
503: Summary: Fixing some arrays, still debugging
504:
1.227 brouard 505: Revision 1.226 2016/07/12 18:42:34 brouard
506: Summary: temp
507:
1.226 brouard 508: Revision 1.225 2016/07/12 08:40:03 brouard
509: Summary: saving but not running
510:
1.225 brouard 511: Revision 1.224 2016/07/01 13:16:01 brouard
512: Summary: Fixes
513:
1.224 brouard 514: Revision 1.223 2016/02/19 09:23:35 brouard
515: Summary: temporary
516:
1.223 brouard 517: Revision 1.222 2016/02/17 08:14:50 brouard
518: Summary: Probably last 0.98 stable version 0.98r6
519:
1.222 brouard 520: Revision 1.221 2016/02/15 23:35:36 brouard
521: Summary: minor bug
522:
1.220 brouard 523: Revision 1.219 2016/02/15 00:48:12 brouard
524: *** empty log message ***
525:
1.219 brouard 526: Revision 1.218 2016/02/12 11:29:23 brouard
527: Summary: 0.99 Back projections
528:
1.218 brouard 529: Revision 1.217 2015/12/23 17:18:31 brouard
530: Summary: Experimental backcast
531:
1.217 brouard 532: Revision 1.216 2015/12/18 17:32:11 brouard
533: Summary: 0.98r4 Warning and status=-2
534:
535: Version 0.98r4 is now:
536: - displaying an error when status is -1, date of interview unknown and date of death known;
537: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
538: Older changes concerning s=-2, dating from 2005 have been supersed.
539:
1.216 brouard 540: Revision 1.215 2015/12/16 08:52:24 brouard
541: Summary: 0.98r4 working
542:
1.215 brouard 543: Revision 1.214 2015/12/16 06:57:54 brouard
544: Summary: temporary not working
545:
1.214 brouard 546: Revision 1.213 2015/12/11 18:22:17 brouard
547: Summary: 0.98r4
548:
1.213 brouard 549: Revision 1.212 2015/11/21 12:47:24 brouard
550: Summary: minor typo
551:
1.212 brouard 552: Revision 1.211 2015/11/21 12:41:11 brouard
553: Summary: 0.98r3 with some graph of projected cross-sectional
554:
555: Author: Nicolas Brouard
556:
1.211 brouard 557: Revision 1.210 2015/11/18 17:41:20 brouard
1.252 brouard 558: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
1.210 brouard 559: Summary: Adding ftolpl parameter
560: Author: N Brouard
561:
562: We had difficulties to get smoothed confidence intervals. It was due
563: to the period prevalence which wasn't computed accurately. The inner
564: parameter ftolpl is now an outer parameter of the .imach parameter
565: file after estepm. If ftolpl is small 1.e-4 and estepm too,
566: computation are long.
567:
1.209 brouard 568: Revision 1.208 2015/11/17 14:31:57 brouard
569: Summary: temporary
570:
1.208 brouard 571: Revision 1.207 2015/10/27 17:36:57 brouard
572: *** empty log message ***
573:
1.207 brouard 574: Revision 1.206 2015/10/24 07:14:11 brouard
575: *** empty log message ***
576:
1.206 brouard 577: Revision 1.205 2015/10/23 15:50:53 brouard
578: Summary: 0.98r3 some clarification for graphs on likelihood contributions
579:
1.205 brouard 580: Revision 1.204 2015/10/01 16:20:26 brouard
581: Summary: Some new graphs of contribution to likelihood
582:
1.204 brouard 583: Revision 1.203 2015/09/30 17:45:14 brouard
584: Summary: looking at better estimation of the hessian
585:
586: Also a better criteria for convergence to the period prevalence And
587: therefore adding the number of years needed to converge. (The
588: prevalence in any alive state shold sum to one
589:
1.203 brouard 590: Revision 1.202 2015/09/22 19:45:16 brouard
591: Summary: Adding some overall graph on contribution to likelihood. Might change
592:
1.202 brouard 593: Revision 1.201 2015/09/15 17:34:58 brouard
594: Summary: 0.98r0
595:
596: - Some new graphs like suvival functions
597: - Some bugs fixed like model=1+age+V2.
598:
1.201 brouard 599: Revision 1.200 2015/09/09 16:53:55 brouard
600: Summary: Big bug thanks to Flavia
601:
602: Even model=1+age+V2. did not work anymore
603:
1.200 brouard 604: Revision 1.199 2015/09/07 14:09:23 brouard
605: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
606:
1.199 brouard 607: Revision 1.198 2015/09/03 07:14:39 brouard
608: Summary: 0.98q5 Flavia
609:
1.198 brouard 610: Revision 1.197 2015/09/01 18:24:39 brouard
611: *** empty log message ***
612:
1.197 brouard 613: Revision 1.196 2015/08/18 23:17:52 brouard
614: Summary: 0.98q5
615:
1.196 brouard 616: Revision 1.195 2015/08/18 16:28:39 brouard
617: Summary: Adding a hack for testing purpose
618:
619: After reading the title, ftol and model lines, if the comment line has
620: a q, starting with #q, the answer at the end of the run is quit. It
621: permits to run test files in batch with ctest. The former workaround was
622: $ echo q | imach foo.imach
623:
1.195 brouard 624: Revision 1.194 2015/08/18 13:32:00 brouard
625: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
626:
1.194 brouard 627: Revision 1.193 2015/08/04 07:17:42 brouard
628: Summary: 0.98q4
629:
1.193 brouard 630: Revision 1.192 2015/07/16 16:49:02 brouard
631: Summary: Fixing some outputs
632:
1.192 brouard 633: Revision 1.191 2015/07/14 10:00:33 brouard
634: Summary: Some fixes
635:
1.191 brouard 636: Revision 1.190 2015/05/05 08:51:13 brouard
637: Summary: Adding digits in output parameters (7 digits instead of 6)
638:
639: Fix 1+age+.
640:
1.190 brouard 641: Revision 1.189 2015/04/30 14:45:16 brouard
642: Summary: 0.98q2
643:
1.189 brouard 644: Revision 1.188 2015/04/30 08:27:53 brouard
645: *** empty log message ***
646:
1.188 brouard 647: Revision 1.187 2015/04/29 09:11:15 brouard
648: *** empty log message ***
649:
1.187 brouard 650: Revision 1.186 2015/04/23 12:01:52 brouard
651: Summary: V1*age is working now, version 0.98q1
652:
653: Some codes had been disabled in order to simplify and Vn*age was
654: working in the optimization phase, ie, giving correct MLE parameters,
655: but, as usual, outputs were not correct and program core dumped.
656:
1.186 brouard 657: Revision 1.185 2015/03/11 13:26:42 brouard
658: Summary: Inclusion of compile and links command line for Intel Compiler
659:
1.185 brouard 660: Revision 1.184 2015/03/11 11:52:39 brouard
661: Summary: Back from Windows 8. Intel Compiler
662:
1.184 brouard 663: Revision 1.183 2015/03/10 20:34:32 brouard
664: Summary: 0.98q0, trying with directest, mnbrak fixed
665:
666: We use directest instead of original Powell test; probably no
667: incidence on the results, but better justifications;
668: We fixed Numerical Recipes mnbrak routine which was wrong and gave
669: wrong results.
670:
1.183 brouard 671: Revision 1.182 2015/02/12 08:19:57 brouard
672: Summary: Trying to keep directest which seems simpler and more general
673: Author: Nicolas Brouard
674:
1.182 brouard 675: Revision 1.181 2015/02/11 23:22:24 brouard
676: Summary: Comments on Powell added
677:
678: Author:
679:
1.181 brouard 680: Revision 1.180 2015/02/11 17:33:45 brouard
681: Summary: Finishing move from main to function (hpijx and prevalence_limit)
682:
1.180 brouard 683: Revision 1.179 2015/01/04 09:57:06 brouard
684: Summary: back to OS/X
685:
1.179 brouard 686: Revision 1.178 2015/01/04 09:35:48 brouard
687: *** empty log message ***
688:
1.178 brouard 689: Revision 1.177 2015/01/03 18:40:56 brouard
690: Summary: Still testing ilc32 on OSX
691:
1.177 brouard 692: Revision 1.176 2015/01/03 16:45:04 brouard
693: *** empty log message ***
694:
1.176 brouard 695: Revision 1.175 2015/01/03 16:33:42 brouard
696: *** empty log message ***
697:
1.175 brouard 698: Revision 1.174 2015/01/03 16:15:49 brouard
699: Summary: Still in cross-compilation
700:
1.174 brouard 701: Revision 1.173 2015/01/03 12:06:26 brouard
702: Summary: trying to detect cross-compilation
703:
1.173 brouard 704: Revision 1.172 2014/12/27 12:07:47 brouard
705: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
706:
1.172 brouard 707: Revision 1.171 2014/12/23 13:26:59 brouard
708: Summary: Back from Visual C
709:
710: Still problem with utsname.h on Windows
711:
1.171 brouard 712: Revision 1.170 2014/12/23 11:17:12 brouard
713: Summary: Cleaning some \%% back to %%
714:
715: The escape was mandatory for a specific compiler (which one?), but too many warnings.
716:
1.170 brouard 717: Revision 1.169 2014/12/22 23:08:31 brouard
718: Summary: 0.98p
719:
720: Outputs some informations on compiler used, OS etc. Testing on different platforms.
721:
1.169 brouard 722: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 723: Summary: update
1.169 brouard 724:
1.168 brouard 725: Revision 1.167 2014/12/22 13:50:56 brouard
726: Summary: Testing uname and compiler version and if compiled 32 or 64
727:
728: Testing on Linux 64
729:
1.167 brouard 730: Revision 1.166 2014/12/22 11:40:47 brouard
731: *** empty log message ***
732:
1.166 brouard 733: Revision 1.165 2014/12/16 11:20:36 brouard
734: Summary: After compiling on Visual C
735:
736: * imach.c (Module): Merging 1.61 to 1.162
737:
1.165 brouard 738: Revision 1.164 2014/12/16 10:52:11 brouard
739: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
740:
741: * imach.c (Module): Merging 1.61 to 1.162
742:
1.164 brouard 743: Revision 1.163 2014/12/16 10:30:11 brouard
744: * imach.c (Module): Merging 1.61 to 1.162
745:
1.163 brouard 746: Revision 1.162 2014/09/25 11:43:39 brouard
747: Summary: temporary backup 0.99!
748:
1.162 brouard 749: Revision 1.1 2014/09/16 11:06:58 brouard
750: Summary: With some code (wrong) for nlopt
751:
752: Author:
753:
754: Revision 1.161 2014/09/15 20:41:41 brouard
755: Summary: Problem with macro SQR on Intel compiler
756:
1.161 brouard 757: Revision 1.160 2014/09/02 09:24:05 brouard
758: *** empty log message ***
759:
1.160 brouard 760: Revision 1.159 2014/09/01 10:34:10 brouard
761: Summary: WIN32
762: Author: Brouard
763:
1.159 brouard 764: Revision 1.158 2014/08/27 17:11:51 brouard
765: *** empty log message ***
766:
1.158 brouard 767: Revision 1.157 2014/08/27 16:26:55 brouard
768: Summary: Preparing windows Visual studio version
769: Author: Brouard
770:
771: In order to compile on Visual studio, time.h is now correct and time_t
772: and tm struct should be used. difftime should be used but sometimes I
773: just make the differences in raw time format (time(&now).
774: Trying to suppress #ifdef LINUX
775: Add xdg-open for __linux in order to open default browser.
776:
1.157 brouard 777: Revision 1.156 2014/08/25 20:10:10 brouard
778: *** empty log message ***
779:
1.156 brouard 780: Revision 1.155 2014/08/25 18:32:34 brouard
781: Summary: New compile, minor changes
782: Author: Brouard
783:
1.155 brouard 784: Revision 1.154 2014/06/20 17:32:08 brouard
785: Summary: Outputs now all graphs of convergence to period prevalence
786:
1.154 brouard 787: Revision 1.153 2014/06/20 16:45:46 brouard
788: Summary: If 3 live state, convergence to period prevalence on same graph
789: Author: Brouard
790:
1.153 brouard 791: Revision 1.152 2014/06/18 17:54:09 brouard
792: Summary: open browser, use gnuplot on same dir than imach if not found in the path
793:
1.152 brouard 794: Revision 1.151 2014/06/18 16:43:30 brouard
795: *** empty log message ***
796:
1.151 brouard 797: Revision 1.150 2014/06/18 16:42:35 brouard
798: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
799: Author: brouard
800:
1.150 brouard 801: Revision 1.149 2014/06/18 15:51:14 brouard
802: Summary: Some fixes in parameter files errors
803: Author: Nicolas Brouard
804:
1.149 brouard 805: Revision 1.148 2014/06/17 17:38:48 brouard
806: Summary: Nothing new
807: Author: Brouard
808:
809: Just a new packaging for OS/X version 0.98nS
810:
1.148 brouard 811: Revision 1.147 2014/06/16 10:33:11 brouard
812: *** empty log message ***
813:
1.147 brouard 814: Revision 1.146 2014/06/16 10:20:28 brouard
815: Summary: Merge
816: Author: Brouard
817:
818: Merge, before building revised version.
819:
1.146 brouard 820: Revision 1.145 2014/06/10 21:23:15 brouard
821: Summary: Debugging with valgrind
822: Author: Nicolas Brouard
823:
824: Lot of changes in order to output the results with some covariates
825: After the Edimburgh REVES conference 2014, it seems mandatory to
826: improve the code.
827: No more memory valgrind error but a lot has to be done in order to
828: continue the work of splitting the code into subroutines.
829: Also, decodemodel has been improved. Tricode is still not
830: optimal. nbcode should be improved. Documentation has been added in
831: the source code.
832:
1.144 brouard 833: Revision 1.143 2014/01/26 09:45:38 brouard
834: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
835:
836: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
837: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
838:
1.143 brouard 839: Revision 1.142 2014/01/26 03:57:36 brouard
840: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
841:
842: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
843:
1.142 brouard 844: Revision 1.141 2014/01/26 02:42:01 brouard
845: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
846:
1.141 brouard 847: Revision 1.140 2011/09/02 10:37:54 brouard
848: Summary: times.h is ok with mingw32 now.
849:
1.140 brouard 850: Revision 1.139 2010/06/14 07:50:17 brouard
851: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
852: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
853:
1.139 brouard 854: Revision 1.138 2010/04/30 18:19:40 brouard
855: *** empty log message ***
856:
1.138 brouard 857: Revision 1.137 2010/04/29 18:11:38 brouard
858: (Module): Checking covariates for more complex models
859: than V1+V2. A lot of change to be done. Unstable.
860:
1.137 brouard 861: Revision 1.136 2010/04/26 20:30:53 brouard
862: (Module): merging some libgsl code. Fixing computation
863: of likelione (using inter/intrapolation if mle = 0) in order to
864: get same likelihood as if mle=1.
865: Some cleaning of code and comments added.
866:
1.136 brouard 867: Revision 1.135 2009/10/29 15:33:14 brouard
868: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
869:
1.135 brouard 870: Revision 1.134 2009/10/29 13:18:53 brouard
871: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
872:
1.134 brouard 873: Revision 1.133 2009/07/06 10:21:25 brouard
874: just nforces
875:
1.133 brouard 876: Revision 1.132 2009/07/06 08:22:05 brouard
877: Many tings
878:
1.132 brouard 879: Revision 1.131 2009/06/20 16:22:47 brouard
880: Some dimensions resccaled
881:
1.131 brouard 882: Revision 1.130 2009/05/26 06:44:34 brouard
883: (Module): Max Covariate is now set to 20 instead of 8. A
884: lot of cleaning with variables initialized to 0. Trying to make
885: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
886:
1.130 brouard 887: Revision 1.129 2007/08/31 13:49:27 lievre
888: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
889:
1.129 lievre 890: Revision 1.128 2006/06/30 13:02:05 brouard
891: (Module): Clarifications on computing e.j
892:
1.128 brouard 893: Revision 1.127 2006/04/28 18:11:50 brouard
894: (Module): Yes the sum of survivors was wrong since
895: imach-114 because nhstepm was no more computed in the age
896: loop. Now we define nhstepma in the age loop.
897: (Module): In order to speed up (in case of numerous covariates) we
898: compute health expectancies (without variances) in a first step
899: and then all the health expectancies with variances or standard
900: deviation (needs data from the Hessian matrices) which slows the
901: computation.
902: In the future we should be able to stop the program is only health
903: expectancies and graph are needed without standard deviations.
904:
1.127 brouard 905: Revision 1.126 2006/04/28 17:23:28 brouard
906: (Module): Yes the sum of survivors was wrong since
907: imach-114 because nhstepm was no more computed in the age
908: loop. Now we define nhstepma in the age loop.
909: Version 0.98h
910:
1.126 brouard 911: Revision 1.125 2006/04/04 15:20:31 lievre
912: Errors in calculation of health expectancies. Age was not initialized.
913: Forecasting file added.
914:
915: Revision 1.124 2006/03/22 17:13:53 lievre
916: Parameters are printed with %lf instead of %f (more numbers after the comma).
917: The log-likelihood is printed in the log file
918:
919: Revision 1.123 2006/03/20 10:52:43 brouard
920: * imach.c (Module): <title> changed, corresponds to .htm file
921: name. <head> headers where missing.
922:
923: * imach.c (Module): Weights can have a decimal point as for
924: English (a comma might work with a correct LC_NUMERIC environment,
925: otherwise the weight is truncated).
926: Modification of warning when the covariates values are not 0 or
927: 1.
928: Version 0.98g
929:
930: Revision 1.122 2006/03/20 09:45:41 brouard
931: (Module): Weights can have a decimal point as for
932: English (a comma might work with a correct LC_NUMERIC environment,
933: otherwise the weight is truncated).
934: Modification of warning when the covariates values are not 0 or
935: 1.
936: Version 0.98g
937:
938: Revision 1.121 2006/03/16 17:45:01 lievre
939: * imach.c (Module): Comments concerning covariates added
940:
941: * imach.c (Module): refinements in the computation of lli if
942: status=-2 in order to have more reliable computation if stepm is
943: not 1 month. Version 0.98f
944:
945: Revision 1.120 2006/03/16 15:10:38 lievre
946: (Module): refinements in the computation of lli if
947: status=-2 in order to have more reliable computation if stepm is
948: not 1 month. Version 0.98f
949:
950: Revision 1.119 2006/03/15 17:42:26 brouard
951: (Module): Bug if status = -2, the loglikelihood was
952: computed as likelihood omitting the logarithm. Version O.98e
953:
954: Revision 1.118 2006/03/14 18:20:07 brouard
955: (Module): varevsij Comments added explaining the second
956: table of variances if popbased=1 .
957: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
958: (Module): Function pstamp added
959: (Module): Version 0.98d
960:
961: Revision 1.117 2006/03/14 17:16:22 brouard
962: (Module): varevsij Comments added explaining the second
963: table of variances if popbased=1 .
964: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
965: (Module): Function pstamp added
966: (Module): Version 0.98d
967:
968: Revision 1.116 2006/03/06 10:29:27 brouard
969: (Module): Variance-covariance wrong links and
970: varian-covariance of ej. is needed (Saito).
971:
972: Revision 1.115 2006/02/27 12:17:45 brouard
973: (Module): One freematrix added in mlikeli! 0.98c
974:
975: Revision 1.114 2006/02/26 12:57:58 brouard
976: (Module): Some improvements in processing parameter
977: filename with strsep.
978:
979: Revision 1.113 2006/02/24 14:20:24 brouard
980: (Module): Memory leaks checks with valgrind and:
981: datafile was not closed, some imatrix were not freed and on matrix
982: allocation too.
983:
984: Revision 1.112 2006/01/30 09:55:26 brouard
985: (Module): Back to gnuplot.exe instead of wgnuplot.exe
986:
987: Revision 1.111 2006/01/25 20:38:18 brouard
988: (Module): Lots of cleaning and bugs added (Gompertz)
989: (Module): Comments can be added in data file. Missing date values
990: can be a simple dot '.'.
991:
992: Revision 1.110 2006/01/25 00:51:50 brouard
993: (Module): Lots of cleaning and bugs added (Gompertz)
994:
995: Revision 1.109 2006/01/24 19:37:15 brouard
996: (Module): Comments (lines starting with a #) are allowed in data.
997:
998: Revision 1.108 2006/01/19 18:05:42 lievre
999: Gnuplot problem appeared...
1000: To be fixed
1001:
1002: Revision 1.107 2006/01/19 16:20:37 brouard
1003: Test existence of gnuplot in imach path
1004:
1005: Revision 1.106 2006/01/19 13:24:36 brouard
1006: Some cleaning and links added in html output
1007:
1008: Revision 1.105 2006/01/05 20:23:19 lievre
1009: *** empty log message ***
1010:
1011: Revision 1.104 2005/09/30 16:11:43 lievre
1012: (Module): sump fixed, loop imx fixed, and simplifications.
1013: (Module): If the status is missing at the last wave but we know
1014: that the person is alive, then we can code his/her status as -2
1015: (instead of missing=-1 in earlier versions) and his/her
1016: contributions to the likelihood is 1 - Prob of dying from last
1017: health status (= 1-p13= p11+p12 in the easiest case of somebody in
1018: the healthy state at last known wave). Version is 0.98
1019:
1020: Revision 1.103 2005/09/30 15:54:49 lievre
1021: (Module): sump fixed, loop imx fixed, and simplifications.
1022:
1023: Revision 1.102 2004/09/15 17:31:30 brouard
1024: Add the possibility to read data file including tab characters.
1025:
1026: Revision 1.101 2004/09/15 10:38:38 brouard
1027: Fix on curr_time
1028:
1029: Revision 1.100 2004/07/12 18:29:06 brouard
1030: Add version for Mac OS X. Just define UNIX in Makefile
1031:
1032: Revision 1.99 2004/06/05 08:57:40 brouard
1033: *** empty log message ***
1034:
1035: Revision 1.98 2004/05/16 15:05:56 brouard
1036: New version 0.97 . First attempt to estimate force of mortality
1037: directly from the data i.e. without the need of knowing the health
1038: state at each age, but using a Gompertz model: log u =a + b*age .
1039: This is the basic analysis of mortality and should be done before any
1040: other analysis, in order to test if the mortality estimated from the
1041: cross-longitudinal survey is different from the mortality estimated
1042: from other sources like vital statistic data.
1043:
1044: The same imach parameter file can be used but the option for mle should be -3.
1045:
1.324 brouard 1046: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 1047: former routines in order to include the new code within the former code.
1048:
1049: The output is very simple: only an estimate of the intercept and of
1050: the slope with 95% confident intervals.
1051:
1052: Current limitations:
1053: A) Even if you enter covariates, i.e. with the
1054: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
1055: B) There is no computation of Life Expectancy nor Life Table.
1056:
1057: Revision 1.97 2004/02/20 13:25:42 lievre
1058: Version 0.96d. Population forecasting command line is (temporarily)
1059: suppressed.
1060:
1061: Revision 1.96 2003/07/15 15:38:55 brouard
1062: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
1063: rewritten within the same printf. Workaround: many printfs.
1064:
1065: Revision 1.95 2003/07/08 07:54:34 brouard
1066: * imach.c (Repository):
1067: (Repository): Using imachwizard code to output a more meaningful covariance
1068: matrix (cov(a12,c31) instead of numbers.
1069:
1070: Revision 1.94 2003/06/27 13:00:02 brouard
1071: Just cleaning
1072:
1073: Revision 1.93 2003/06/25 16:33:55 brouard
1074: (Module): On windows (cygwin) function asctime_r doesn't
1075: exist so I changed back to asctime which exists.
1076: (Module): Version 0.96b
1077:
1078: Revision 1.92 2003/06/25 16:30:45 brouard
1079: (Module): On windows (cygwin) function asctime_r doesn't
1080: exist so I changed back to asctime which exists.
1081:
1082: Revision 1.91 2003/06/25 15:30:29 brouard
1083: * imach.c (Repository): Duplicated warning errors corrected.
1084: (Repository): Elapsed time after each iteration is now output. It
1085: helps to forecast when convergence will be reached. Elapsed time
1086: is stamped in powell. We created a new html file for the graphs
1087: concerning matrix of covariance. It has extension -cov.htm.
1088:
1089: Revision 1.90 2003/06/24 12:34:15 brouard
1090: (Module): Some bugs corrected for windows. Also, when
1091: mle=-1 a template is output in file "or"mypar.txt with the design
1092: of the covariance matrix to be input.
1093:
1094: Revision 1.89 2003/06/24 12:30:52 brouard
1095: (Module): Some bugs corrected for windows. Also, when
1096: mle=-1 a template is output in file "or"mypar.txt with the design
1097: of the covariance matrix to be input.
1098:
1099: Revision 1.88 2003/06/23 17:54:56 brouard
1100: * 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.
1101:
1102: Revision 1.87 2003/06/18 12:26:01 brouard
1103: Version 0.96
1104:
1105: Revision 1.86 2003/06/17 20:04:08 brouard
1106: (Module): Change position of html and gnuplot routines and added
1107: routine fileappend.
1108:
1109: Revision 1.85 2003/06/17 13:12:43 brouard
1110: * imach.c (Repository): Check when date of death was earlier that
1111: current date of interview. It may happen when the death was just
1112: prior to the death. In this case, dh was negative and likelihood
1113: was wrong (infinity). We still send an "Error" but patch by
1114: assuming that the date of death was just one stepm after the
1115: interview.
1116: (Repository): Because some people have very long ID (first column)
1117: we changed int to long in num[] and we added a new lvector for
1118: memory allocation. But we also truncated to 8 characters (left
1119: truncation)
1120: (Repository): No more line truncation errors.
1121:
1122: Revision 1.84 2003/06/13 21:44:43 brouard
1123: * imach.c (Repository): Replace "freqsummary" at a correct
1124: place. It differs from routine "prevalence" which may be called
1125: many times. Probs is memory consuming and must be used with
1126: parcimony.
1127: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
1128:
1129: Revision 1.83 2003/06/10 13:39:11 lievre
1130: *** empty log message ***
1131:
1132: Revision 1.82 2003/06/05 15:57:20 brouard
1133: Add log in imach.c and fullversion number is now printed.
1134:
1135: */
1136: /*
1137: Interpolated Markov Chain
1138:
1139: Short summary of the programme:
1140:
1.227 brouard 1141: This program computes Healthy Life Expectancies or State-specific
1142: (if states aren't health statuses) Expectancies from
1143: cross-longitudinal data. Cross-longitudinal data consist in:
1144:
1145: -1- a first survey ("cross") where individuals from different ages
1146: are interviewed on their health status or degree of disability (in
1147: the case of a health survey which is our main interest)
1148:
1149: -2- at least a second wave of interviews ("longitudinal") which
1150: measure each change (if any) in individual health status. Health
1151: expectancies are computed from the time spent in each health state
1152: according to a model. More health states you consider, more time is
1153: necessary to reach the Maximum Likelihood of the parameters involved
1154: in the model. The simplest model is the multinomial logistic model
1155: where pij is the probability to be observed in state j at the second
1156: wave conditional to be observed in state i at the first
1157: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
1158: etc , where 'age' is age and 'sex' is a covariate. If you want to
1159: have a more complex model than "constant and age", you should modify
1160: the program where the markup *Covariates have to be included here
1161: again* invites you to do it. More covariates you add, slower the
1.126 brouard 1162: convergence.
1163:
1164: The advantage of this computer programme, compared to a simple
1165: multinomial logistic model, is clear when the delay between waves is not
1166: identical for each individual. Also, if a individual missed an
1167: intermediate interview, the information is lost, but taken into
1168: account using an interpolation or extrapolation.
1169:
1170: hPijx is the probability to be observed in state i at age x+h
1171: conditional to the observed state i at age x. The delay 'h' can be
1172: split into an exact number (nh*stepm) of unobserved intermediate
1173: states. This elementary transition (by month, quarter,
1174: semester or year) is modelled as a multinomial logistic. The hPx
1175: matrix is simply the matrix product of nh*stepm elementary matrices
1176: and the contribution of each individual to the likelihood is simply
1177: hPijx.
1178:
1179: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 1180: of the life expectancies. It also computes the period (stable) prevalence.
1181:
1182: Back prevalence and projections:
1.227 brouard 1183:
1184: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1185: double agemaxpar, double ftolpl, int *ncvyearp, double
1186: dateprev1,double dateprev2, int firstpass, int lastpass, int
1187: mobilavproj)
1188:
1189: Computes the back prevalence limit for any combination of
1190: covariate values k at any age between ageminpar and agemaxpar and
1191: returns it in **bprlim. In the loops,
1192:
1193: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1194: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1195:
1196: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 1197: Computes for any combination of covariates k and any age between bage and fage
1198: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1199: oldm=oldms;savm=savms;
1.227 brouard 1200:
1.267 brouard 1201: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.218 brouard 1202: Computes the transition matrix starting at age 'age' over
1203: 'nhstepm*hstepm*stepm' months (i.e. until
1204: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 1205: nhstepm*hstepm matrices.
1206:
1207: Returns p3mat[i][j][h] after calling
1208: p3mat[i][j][h]=matprod2(newm,
1209: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1210: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1211: oldm);
1.226 brouard 1212:
1213: Important routines
1214:
1215: - func (or funcone), computes logit (pij) distinguishing
1216: o fixed variables (single or product dummies or quantitative);
1217: o varying variables by:
1218: (1) wave (single, product dummies, quantitative),
1219: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1220: % fixed dummy (treated) or quantitative (not done because time-consuming);
1221: % varying dummy (not done) or quantitative (not done);
1222: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1223: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1224: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1.325 brouard 1225: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1.226 brouard 1226: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 1227:
1.226 brouard 1228:
1229:
1.324 brouard 1230: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1231: Institut national d'études démographiques, Paris.
1.126 brouard 1232: This software have been partly granted by Euro-REVES, a concerted action
1233: from the European Union.
1234: It is copyrighted identically to a GNU software product, ie programme and
1235: software can be distributed freely for non commercial use. Latest version
1236: can be accessed at http://euroreves.ined.fr/imach .
1237:
1238: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1239: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1240:
1241: **********************************************************************/
1242: /*
1243: main
1244: read parameterfile
1245: read datafile
1246: concatwav
1247: freqsummary
1248: if (mle >= 1)
1249: mlikeli
1250: print results files
1251: if mle==1
1252: computes hessian
1253: read end of parameter file: agemin, agemax, bage, fage, estepm
1254: begin-prev-date,...
1255: open gnuplot file
1256: open html file
1.145 brouard 1257: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1258: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1259: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1260: freexexit2 possible for memory heap.
1261:
1262: h Pij x | pij_nom ficrestpij
1263: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1264: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1265: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1266:
1267: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1268: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1269: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1270: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1271: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1272:
1.126 brouard 1273: forecasting if prevfcast==1 prevforecast call prevalence()
1274: health expectancies
1275: Variance-covariance of DFLE
1276: prevalence()
1277: movingaverage()
1278: varevsij()
1279: if popbased==1 varevsij(,popbased)
1280: total life expectancies
1281: Variance of period (stable) prevalence
1282: end
1283: */
1284:
1.187 brouard 1285: /* #define DEBUG */
1286: /* #define DEBUGBRENT */
1.203 brouard 1287: /* #define DEBUGLINMIN */
1288: /* #define DEBUGHESS */
1289: #define DEBUGHESSIJ
1.224 brouard 1290: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 1291: #define POWELL /* Instead of NLOPT */
1.224 brouard 1292: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 1293: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1294: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.319 brouard 1295: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1.359 ! brouard 1296: /* #define POWELLORIGINCONJUGATE /\* Don't use conjugate but biggest decrease if valuable *\/ */
! 1297: /* #define NOTMINFIT */
1.126 brouard 1298:
1299: #include <math.h>
1300: #include <stdio.h>
1301: #include <stdlib.h>
1302: #include <string.h>
1.226 brouard 1303: #include <ctype.h>
1.159 brouard 1304:
1305: #ifdef _WIN32
1306: #include <io.h>
1.172 brouard 1307: #include <windows.h>
1308: #include <tchar.h>
1.159 brouard 1309: #else
1.126 brouard 1310: #include <unistd.h>
1.159 brouard 1311: #endif
1.126 brouard 1312:
1313: #include <limits.h>
1314: #include <sys/types.h>
1.171 brouard 1315:
1316: #if defined(__GNUC__)
1317: #include <sys/utsname.h> /* Doesn't work on Windows */
1318: #endif
1319:
1.126 brouard 1320: #include <sys/stat.h>
1321: #include <errno.h>
1.159 brouard 1322: /* extern int errno; */
1.126 brouard 1323:
1.157 brouard 1324: /* #ifdef LINUX */
1325: /* #include <time.h> */
1326: /* #include "timeval.h" */
1327: /* #else */
1328: /* #include <sys/time.h> */
1329: /* #endif */
1330:
1.126 brouard 1331: #include <time.h>
1332:
1.136 brouard 1333: #ifdef GSL
1334: #include <gsl/gsl_errno.h>
1335: #include <gsl/gsl_multimin.h>
1336: #endif
1337:
1.167 brouard 1338:
1.162 brouard 1339: #ifdef NLOPT
1340: #include <nlopt.h>
1341: typedef struct {
1342: double (* function)(double [] );
1343: } myfunc_data ;
1344: #endif
1345:
1.126 brouard 1346: /* #include <libintl.h> */
1347: /* #define _(String) gettext (String) */
1348:
1.349 brouard 1349: #define MAXLINE 16384 /* Was 256 and 1024 and 2048. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 1350:
1351: #define GNUPLOTPROGRAM "gnuplot"
1.343 brouard 1352: #define GNUPLOTVERSION 5.1
1353: double gnuplotversion=GNUPLOTVERSION;
1.126 brouard 1354: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1.329 brouard 1355: #define FILENAMELENGTH 256
1.126 brouard 1356:
1357: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1358: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1359:
1.349 brouard 1360: #define MAXPARM 216 /**< Maximum number of parameters for the optimization was 128 */
1.144 brouard 1361: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 1362:
1363: #define NINTERVMAX 8
1.144 brouard 1364: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1365: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1.325 brouard 1366: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1.197 brouard 1367: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 1368: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1369: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.290 brouard 1370: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1.144 brouard 1371: #define YEARM 12. /**< Number of months per year */
1.218 brouard 1372: /* #define AGESUP 130 */
1.288 brouard 1373: /* #define AGESUP 150 */
1374: #define AGESUP 200
1.268 brouard 1375: #define AGEINF 0
1.218 brouard 1376: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 1377: #define AGEBASE 40
1.194 brouard 1378: #define AGEOVERFLOW 1.e20
1.164 brouard 1379: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 1380: #ifdef _WIN32
1381: #define DIRSEPARATOR '\\'
1382: #define CHARSEPARATOR "\\"
1383: #define ODIRSEPARATOR '/'
1384: #else
1.126 brouard 1385: #define DIRSEPARATOR '/'
1386: #define CHARSEPARATOR "/"
1387: #define ODIRSEPARATOR '\\'
1388: #endif
1389:
1.359 ! brouard 1390: /* $Id: imachprax.c,v 1.6 2024/04/24 21:10:29 brouard Exp $ */
1.126 brouard 1391: /* $State: Exp $ */
1.196 brouard 1392: #include "version.h"
1393: char version[]=__IMACH_VERSION__;
1.359 ! brouard 1394: char copyright[]="April 2023,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-2022";
! 1395: char fullversion[]="$Revision: 1.6 $ $Date: 2024/04/24 21:10:29 $";
1.126 brouard 1396: char strstart[80];
1397: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 1398: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.342 brouard 1399: int debugILK=0; /* debugILK is set by a #d in a comment line */
1.187 brouard 1400: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.330 brouard 1401: /* Number of covariates model (1)=V2+V1+ V3*age+V2*V4 */
1402: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
1.335 brouard 1403: 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 1404: 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 1405: int cptcovs=0; /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
1406: int cptcovsnq=0; /**< cptcovsnq number of SIMPLE covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 1407: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1.349 brouard 1408: int cptcovprodage=0; /**< Number of fixed covariates with age: V3*age or V2*V3*age 1 */
1409: int cptcovprodvage=0; /**< Number of varying covariates with age: V7*age or V7*V6*age */
1410: int cptcovdageprod=0; /**< Number of doubleproducts with age, since 0.99r44 only: age*Vn*Vm for gnuplot printing*/
1.145 brouard 1411: int cptcovprodnoage=0; /**< Number of covariate products without age */
1.335 brouard 1412: 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 1413: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1414: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.339 brouard 1415: int ncovvt=0; /* Total number of effective (wave) varying covariates (dummy or quantitative or products [without age]) in the model */
1.349 brouard 1416: 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 */
1417: 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 */
1418: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (single or product, dummy or quantitative) in the model */
1419: 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 1420: int nsd=0; /**< Total number of single dummy variables (output) */
1421: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 1422: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 1423: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 1424: int ntveff=0; /**< ntveff number of effective time varying variables */
1425: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 1426: int cptcov=0; /* Working variable */
1.334 brouard 1427: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs+1 declared globally ;*/
1.290 brouard 1428: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1.218 brouard 1429: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.302 brouard 1430: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1.126 brouard 1431: int nlstate=2; /* Number of live states */
1432: int ndeath=1; /* Number of dead states */
1.130 brouard 1433: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.339 brouard 1434: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable*/
1435: int ncovcolt=0; /* ncovcolt=ncovcol+nqv+ntv+nqtv; total of covariates in the data, not in the model equation*/
1.126 brouard 1436: int popbased=0;
1437:
1438: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 1439: int maxwav=0; /* Maxim number of waves */
1440: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1441: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1.359 ! brouard 1442: int gipmx = 0;
! 1443: double gsw = 0; /* Global variables on the number of contributions
1.126 brouard 1444: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 1445: int mle=1, weightopt=0;
1.126 brouard 1446: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1447: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1448: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1449: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 1450: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 1451: int selected(int kvar); /* Is covariate kvar selected for printing results */
1452:
1.130 brouard 1453: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 1454: double **matprod2(); /* test */
1.126 brouard 1455: double **oldm, **newm, **savm; /* Working pointers to matrices */
1456: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 1457: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1458:
1.136 brouard 1459: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 1460: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 1461: FILE *ficlog, *ficrespow;
1.130 brouard 1462: int globpr=0; /* Global variable for printing or not */
1.126 brouard 1463: double fretone; /* Only one call to likelihood */
1.130 brouard 1464: long ipmx=0; /* Number of contributions */
1.126 brouard 1465: double sw; /* Sum of weights */
1466: char filerespow[FILENAMELENGTH];
1467: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1468: FILE *ficresilk;
1469: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1470: FILE *ficresprobmorprev;
1471: FILE *fichtm, *fichtmcov; /* Html File */
1472: FILE *ficreseij;
1473: char filerese[FILENAMELENGTH];
1474: FILE *ficresstdeij;
1475: char fileresstde[FILENAMELENGTH];
1476: FILE *ficrescveij;
1477: char filerescve[FILENAMELENGTH];
1478: FILE *ficresvij;
1479: char fileresv[FILENAMELENGTH];
1.269 brouard 1480:
1.126 brouard 1481: char title[MAXLINE];
1.234 brouard 1482: char model[MAXLINE]; /**< The model line */
1.217 brouard 1483: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1484: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1485: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1486: char command[FILENAMELENGTH];
1487: int outcmd=0;
1488:
1.217 brouard 1489: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1490: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1491: char filelog[FILENAMELENGTH]; /* Log file */
1492: char filerest[FILENAMELENGTH];
1493: char fileregp[FILENAMELENGTH];
1494: char popfile[FILENAMELENGTH];
1495:
1496: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1497:
1.157 brouard 1498: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1499: /* struct timezone tzp; */
1500: /* extern int gettimeofday(); */
1501: struct tm tml, *gmtime(), *localtime();
1502:
1503: extern time_t time();
1504:
1505: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1506: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1.349 brouard 1507: time_t rlast_btime; /* raw time */
1.157 brouard 1508: struct tm tm;
1509:
1.126 brouard 1510: char strcurr[80], strfor[80];
1511:
1512: char *endptr;
1513: long lval;
1514: double dval;
1515:
1516: #define NR_END 1
1517: #define FREE_ARG char*
1518: #define FTOL 1.0e-10
1519:
1520: #define NRANSI
1.240 brouard 1521: #define ITMAX 200
1522: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1523:
1524: #define TOL 2.0e-4
1525:
1526: #define CGOLD 0.3819660
1527: #define ZEPS 1.0e-10
1528: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1529:
1530: #define GOLD 1.618034
1531: #define GLIMIT 100.0
1532: #define TINY 1.0e-20
1533:
1534: static double maxarg1,maxarg2;
1535: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1536: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1537:
1538: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1539: #define rint(a) floor(a+0.5)
1.166 brouard 1540: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1541: #define mytinydouble 1.0e-16
1.166 brouard 1542: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1543: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1544: /* static double dsqrarg; */
1545: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1546: static double sqrarg;
1547: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1548: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1549: int agegomp= AGEGOMP;
1550:
1551: int imx;
1552: int stepm=1;
1553: /* Stepm, step in month: minimum step interpolation*/
1554:
1555: int estepm;
1556: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1557:
1558: int m,nb;
1559: long *num;
1.197 brouard 1560: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1561: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1562: covariate for which somebody answered excluding
1563: undefined. Usually 2: 0 and 1. */
1564: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1565: covariate for which somebody answered including
1566: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1567: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1568: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1569: double ***mobaverage, ***mobaverages; /* New global variable */
1.332 brouard 1570: 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 1571: double *ageexmed,*agecens;
1572: double dateintmean=0;
1.296 brouard 1573: double anprojd, mprojd, jprojd; /* For eventual projections */
1574: double anprojf, mprojf, jprojf;
1.126 brouard 1575:
1.296 brouard 1576: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1577: double anbackf, mbackf, jbackf;
1578: double jintmean,mintmean,aintmean;
1.126 brouard 1579: double *weight;
1580: int **s; /* Status */
1.141 brouard 1581: double *agedc;
1.145 brouard 1582: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1583: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1584: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.268 brouard 1585: double **coqvar; /* Fixed quantitative covariate nqv */
1.341 brouard 1586: double ***cotvar; /* Time varying covariate start at ncovcol + nqv + (1 to ntv) */
1.225 brouard 1587: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1588: double idx;
1589: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.319 brouard 1590: /* Some documentation */
1591: /* Design original data
1592: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1593: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1594: * ntv=3 nqtv=1
1.330 brouard 1595: * cptcovn number of covariates (not including constant and age or age*age) = number of plus sign + 1 = 10+1=11
1.319 brouard 1596: * For time varying covariate, quanti or dummies
1597: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1.341 brouard 1598: * cotvar[wav][ncovcol+nqv+ iv(1 to nqtv)][i]= [(1 to nqtv)][i]=(V12) quanti
1.319 brouard 1599: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1600: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1.332 brouard 1601: * covar[Vk,i], value of the Vkth fixed covariate dummy or quanti for individual i:
1.319 brouard 1602: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1603: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1604: * k= 1 2 3 4 5 6 7 8 9 10 11
1605: */
1606: /* According to the model, more columns can be added to covar by the product of covariates */
1.318 brouard 1607: /* 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
1608: # States 1=Coresidence, 2 Living alone, 3 Institution
1609: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1610: */
1.349 brouard 1611: /* V5+V4+ V3+V4*V3 +V5*age+V2 +V1*V2+V1*age+V1+V4*V3*age */
1612: /* kmodel 1 2 3 4 5 6 7 8 9 10 */
1613: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 3 *//*0 for simple covariate (dummy, quantitative,*/
1614: /* fixed or varying), 1 for age product, 2 for*/
1615: /* product without age, 3 for age and double product */
1616: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 3 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1617: /*(single or product without age), 2 dummy*/
1618: /* with age product, 3 quant with age product*/
1619: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 6 */
1620: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1621: /*TnsdVar[Tvar] 1 2 3 */
1622: /*Tvaraff[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1623: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1624: /*TvarsDind[nsd] 2 3 9 */ /* position K of single dummy cova */
1625: /* nsq 1 2 */ /* Counting single quantit tv */
1626: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1627: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1628: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1629: /* cptcovage 1 2 3 */ /* Counting cov*age in the model equation */
1630: /* Tage[cptcovage]=k 5 8 10 */ /* Position in the model of ith cov*age */
1.350 brouard 1631: /* 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"*/
1632: /* 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 1633: /* p Tvard[2][1]@21 = {7, 2, 6, 3, 7, 3, 6, 4, 7, 4, 0 <repeats 11 times>} */
1.350 brouard 1634: /* 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}*/
1635: /* 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 1636: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1.330 brouard 1637: /* 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 1638: /* 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 1639: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.234 brouard 1640: /* Type */
1641: /* V 1 2 3 4 5 */
1642: /* F F V V V */
1643: /* D Q D D Q */
1644: /* */
1645: int *TvarsD;
1.330 brouard 1646: int *TnsdVar;
1.234 brouard 1647: int *TvarsDind;
1648: int *TvarsQ;
1649: int *TvarsQind;
1650:
1.318 brouard 1651: #define MAXRESULTLINESPONE 10+1
1.235 brouard 1652: int nresult=0;
1.258 brouard 1653: int parameterline=0; /* # of the parameter (type) line */
1.334 brouard 1654: int TKresult[MAXRESULTLINESPONE]; /* TKresult[nres]=k for each resultline nres give the corresponding combination of dummies */
1655: int resultmodel[MAXRESULTLINESPONE][NCOVMAX];/* resultmodel[k1]=k3: k1th position in the model corresponds to the k3 position in the resultline */
1656: int modelresult[MAXRESULTLINESPONE][NCOVMAX];/* modelresult[k3]=k1: k1th position in the model corresponds to the k3 position in the resultline */
1657: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1.332 brouard 1658: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1659: double TinvDoQresult[MAXRESULTLINESPONE][NCOVMAX];/* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1.334 brouard 1660: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1.332 brouard 1661: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1.318 brouard 1662: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1.332 brouard 1663: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1.318 brouard 1664:
1665: /* 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
1666: # States 1=Coresidence, 2 Living alone, 3 Institution
1667: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1668: */
1.234 brouard 1669: /* 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 1670: 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 */
1671: 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 */
1672: 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 */
1673: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1674: 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 */
1675: 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 1676: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1677: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1678: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1679: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1680: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1681: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1682: 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 */
1683: 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 1684: int *TvarVV; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1685: int *TvarVVind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1.349 brouard 1686: int *TvarVVA; /* We count ncovvt time varying covariates (single or products with age) and put their name into TvarVVA */
1687: int *TvarVVAind; /* We count ncovvt time varying covariates (single or products without age) and put their name into TvarVV */
1688: int *TvarAVVA; /* We count ALL ncovta time varying covariates (single or products with age) and put their name into TvarVVA */
1689: int *TvarAVVAind; /* We count ALL ncovta time varying covariates (single or products without age) and put their name into TvarVV */
1.339 brouard 1690: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1.349 brouard 1691: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age */
1692: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
1693: /* TvarVV={3,1,3,1,3}, for V3 and then the product V1*V3 is decomposed into V1 and V3 */
1694: /* 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 1695: int *Tvarsel; /**< Selected covariates for output */
1696: double *Tvalsel; /**< Selected modality value of covariate for output */
1.349 brouard 1697: 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 1698: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1699: 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 1700: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1701: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1702: int *Tage;
1.227 brouard 1703: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1704: 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 1705: 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*/
1706: 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 1707: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1708: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1709: int **Tvard;
1.330 brouard 1710: int **Tvardk;
1.227 brouard 1711: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1712: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1713: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1714: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1715: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1716: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1717: double *lsurv, *lpop, *tpop;
1718:
1.231 brouard 1719: #define FD 1; /* Fixed dummy covariate */
1720: #define FQ 2; /* Fixed quantitative covariate */
1721: #define FP 3; /* Fixed product covariate */
1722: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1723: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1724: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1725: #define VD 10; /* Varying dummy covariate */
1726: #define VQ 11; /* Varying quantitative covariate */
1727: #define VP 12; /* Varying product covariate */
1728: #define VPDD 13; /* Varying product dummy*dummy covariate */
1729: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1730: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1731: #define APFD 16; /* Age product * fixed dummy covariate */
1732: #define APFQ 17; /* Age product * fixed quantitative covariate */
1733: #define APVD 18; /* Age product * varying dummy covariate */
1734: #define APVQ 19; /* Age product * varying quantitative covariate */
1735:
1736: #define FTYPE 1; /* Fixed covariate */
1737: #define VTYPE 2; /* Varying covariate (loop in wave) */
1738: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1739:
1740: struct kmodel{
1741: int maintype; /* main type */
1742: int subtype; /* subtype */
1743: };
1744: struct kmodel modell[NCOVMAX];
1745:
1.143 brouard 1746: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1747: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1748:
1749: /**************** split *************************/
1750: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1751: {
1752: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1753: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1754: */
1755: char *ss; /* pointer */
1.186 brouard 1756: int l1=0, l2=0; /* length counters */
1.126 brouard 1757:
1758: l1 = strlen(path ); /* length of path */
1759: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1760: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1761: if ( ss == NULL ) { /* no directory, so determine current directory */
1762: strcpy( name, path ); /* we got the fullname name because no directory */
1763: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1764: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1765: /* get current working directory */
1766: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1767: #ifdef WIN32
1768: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1769: #else
1770: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1771: #endif
1.126 brouard 1772: return( GLOCK_ERROR_GETCWD );
1773: }
1774: /* got dirc from getcwd*/
1775: printf(" DIRC = %s \n",dirc);
1.205 brouard 1776: } else { /* strip directory from path */
1.126 brouard 1777: ss++; /* after this, the filename */
1778: l2 = strlen( ss ); /* length of filename */
1779: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1780: strcpy( name, ss ); /* save file name */
1781: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1782: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1783: printf(" DIRC2 = %s \n",dirc);
1784: }
1785: /* We add a separator at the end of dirc if not exists */
1786: l1 = strlen( dirc ); /* length of directory */
1787: if( dirc[l1-1] != DIRSEPARATOR ){
1788: dirc[l1] = DIRSEPARATOR;
1789: dirc[l1+1] = 0;
1790: printf(" DIRC3 = %s \n",dirc);
1791: }
1792: ss = strrchr( name, '.' ); /* find last / */
1793: if (ss >0){
1794: ss++;
1795: strcpy(ext,ss); /* save extension */
1796: l1= strlen( name);
1797: l2= strlen(ss)+1;
1798: strncpy( finame, name, l1-l2);
1799: finame[l1-l2]= 0;
1800: }
1801:
1802: return( 0 ); /* we're done */
1803: }
1804:
1805:
1806: /******************************************/
1807:
1808: void replace_back_to_slash(char *s, char*t)
1809: {
1810: int i;
1811: int lg=0;
1812: i=0;
1813: lg=strlen(t);
1814: for(i=0; i<= lg; i++) {
1815: (s[i] = t[i]);
1816: if (t[i]== '\\') s[i]='/';
1817: }
1818: }
1819:
1.132 brouard 1820: char *trimbb(char *out, char *in)
1.137 brouard 1821: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1822: char *s;
1823: s=out;
1824: while (*in != '\0'){
1.137 brouard 1825: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1826: in++;
1827: }
1828: *out++ = *in++;
1829: }
1830: *out='\0';
1831: return s;
1832: }
1833:
1.351 brouard 1834: char *trimbtab(char *out, char *in)
1835: { /* Trim blanks or tabs in line but keeps first blanks if line starts with blanks */
1836: char *s;
1837: s=out;
1838: while (*in != '\0'){
1839: while( (*in == ' ' || *in == '\t')){ /* && *(in+1) != '\0'){*/
1840: in++;
1841: }
1842: *out++ = *in++;
1843: }
1844: *out='\0';
1845: return s;
1846: }
1847:
1.187 brouard 1848: /* char *substrchaine(char *out, char *in, char *chain) */
1849: /* { */
1850: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1851: /* char *s, *t; */
1852: /* t=in;s=out; */
1853: /* while ((*in != *chain) && (*in != '\0')){ */
1854: /* *out++ = *in++; */
1855: /* } */
1856:
1857: /* /\* *in matches *chain *\/ */
1858: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1859: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1860: /* } */
1861: /* in--; chain--; */
1862: /* while ( (*in != '\0')){ */
1863: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1864: /* *out++ = *in++; */
1865: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1866: /* } */
1867: /* *out='\0'; */
1868: /* out=s; */
1869: /* return out; */
1870: /* } */
1871: char *substrchaine(char *out, char *in, char *chain)
1872: {
1873: /* Substract chain 'chain' from 'in', return and output 'out' */
1.349 brouard 1874: /* in="V1+V1*age+age*age+V2", chain="+age*age" out="V1+V1*age+V2" */
1.187 brouard 1875:
1876: char *strloc;
1877:
1.349 brouard 1878: strcpy (out, in); /* out="V1+V1*age+age*age+V2" */
1879: strloc = strstr(out, chain); /* strloc points to out at "+age*age+V2" */
1880: 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 1881: if(strloc != NULL){
1.349 brouard 1882: /* will affect out */ /* strloc+strlen(chain)=|+V2 = "V1+V1*age+age*age|+V2" */ /* Will also work in Unicodek */
1883: 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)*/
1884: /* equivalent to strcpy (strloc, strloc +strlen(chain)) if no overlap; Copies from "+V2" to V1+V1*age+ */
1.187 brouard 1885: }
1.349 brouard 1886: 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 1887: return out;
1888: }
1889:
1890:
1.145 brouard 1891: char *cutl(char *blocc, char *alocc, char *in, char occ)
1892: {
1.187 brouard 1893: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.349 brouard 1894: and alocc starts after first occurence of char 'occ' : ex cutl(blocc,alocc,"abcdef2ghi2j",'2')
1.310 brouard 1895: gives alocc="abcdef" and blocc="ghi2j".
1.145 brouard 1896: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1897: */
1.160 brouard 1898: char *s, *t;
1.145 brouard 1899: t=in;s=in;
1900: while ((*in != occ) && (*in != '\0')){
1901: *alocc++ = *in++;
1902: }
1903: if( *in == occ){
1904: *(alocc)='\0';
1905: s=++in;
1906: }
1907:
1908: if (s == t) {/* occ not found */
1909: *(alocc-(in-s))='\0';
1910: in=s;
1911: }
1912: while ( *in != '\0'){
1913: *blocc++ = *in++;
1914: }
1915:
1916: *blocc='\0';
1917: return t;
1918: }
1.137 brouard 1919: char *cutv(char *blocc, char *alocc, char *in, char occ)
1920: {
1.187 brouard 1921: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1922: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1923: gives blocc="abcdef2ghi" and alocc="j".
1924: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1925: */
1926: char *s, *t;
1927: t=in;s=in;
1928: while (*in != '\0'){
1929: while( *in == occ){
1930: *blocc++ = *in++;
1931: s=in;
1932: }
1933: *blocc++ = *in++;
1934: }
1935: if (s == t) /* occ not found */
1936: *(blocc-(in-s))='\0';
1937: else
1938: *(blocc-(in-s)-1)='\0';
1939: in=s;
1940: while ( *in != '\0'){
1941: *alocc++ = *in++;
1942: }
1943:
1944: *alocc='\0';
1945: return s;
1946: }
1947:
1.126 brouard 1948: int nbocc(char *s, char occ)
1949: {
1950: int i,j=0;
1951: int lg=20;
1952: i=0;
1953: lg=strlen(s);
1954: for(i=0; i<= lg; i++) {
1.234 brouard 1955: if (s[i] == occ ) j++;
1.126 brouard 1956: }
1957: return j;
1958: }
1959:
1.349 brouard 1960: int nboccstr(char *textin, char *chain)
1961: {
1962: /* Counts the number of occurence of "chain" in string textin */
1963: /* in="+V7*V4+age*V2+age*V3+age*V4" chain="age" */
1964: char *strloc;
1965:
1966: int i,j=0;
1967:
1968: i=0;
1969:
1970: strloc=textin; /* strloc points to "^+V7*V4+age+..." in textin */
1971: for(;;) {
1972: strloc= strstr(strloc,chain); /* strloc points to first character of chain in textin if found. Example strloc points^ to "+V7*V4+^age" in textin */
1973: if(strloc != NULL){
1974: strloc = strloc+strlen(chain); /* strloc points to "+V7*V4+age^" in textin */
1975: j++;
1976: }else
1977: break;
1978: }
1979: return j;
1980:
1981: }
1.137 brouard 1982: /* void cutv(char *u,char *v, char*t, char occ) */
1983: /* { */
1984: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1985: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1986: /* gives u="abcdef2ghi" and v="j" *\/ */
1987: /* int i,lg,j,p=0; */
1988: /* i=0; */
1989: /* lg=strlen(t); */
1990: /* for(j=0; j<=lg-1; j++) { */
1991: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1992: /* } */
1.126 brouard 1993:
1.137 brouard 1994: /* for(j=0; j<p; j++) { */
1995: /* (u[j] = t[j]); */
1996: /* } */
1997: /* u[p]='\0'; */
1.126 brouard 1998:
1.137 brouard 1999: /* for(j=0; j<= lg; j++) { */
2000: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
2001: /* } */
2002: /* } */
1.126 brouard 2003:
1.160 brouard 2004: #ifdef _WIN32
2005: char * strsep(char **pp, const char *delim)
2006: {
2007: char *p, *q;
2008:
2009: if ((p = *pp) == NULL)
2010: return 0;
2011: if ((q = strpbrk (p, delim)) != NULL)
2012: {
2013: *pp = q + 1;
2014: *q = '\0';
2015: }
2016: else
2017: *pp = 0;
2018: return p;
2019: }
2020: #endif
2021:
1.126 brouard 2022: /********************** nrerror ********************/
2023:
2024: void nrerror(char error_text[])
2025: {
2026: fprintf(stderr,"ERREUR ...\n");
2027: fprintf(stderr,"%s\n",error_text);
2028: exit(EXIT_FAILURE);
2029: }
2030: /*********************** vector *******************/
2031: double *vector(int nl, int nh)
2032: {
2033: double *v;
2034: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
2035: if (!v) nrerror("allocation failure in vector");
2036: return v-nl+NR_END;
2037: }
2038:
2039: /************************ free vector ******************/
2040: void free_vector(double*v, int nl, int nh)
2041: {
2042: free((FREE_ARG)(v+nl-NR_END));
2043: }
2044:
2045: /************************ivector *******************************/
2046: int *ivector(long nl,long nh)
2047: {
2048: int *v;
2049: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
2050: if (!v) nrerror("allocation failure in ivector");
2051: return v-nl+NR_END;
2052: }
2053:
2054: /******************free ivector **************************/
2055: void free_ivector(int *v, long nl, long nh)
2056: {
2057: free((FREE_ARG)(v+nl-NR_END));
2058: }
2059:
2060: /************************lvector *******************************/
2061: long *lvector(long nl,long nh)
2062: {
2063: long *v;
2064: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
2065: if (!v) nrerror("allocation failure in ivector");
2066: return v-nl+NR_END;
2067: }
2068:
2069: /******************free lvector **************************/
2070: void free_lvector(long *v, long nl, long nh)
2071: {
2072: free((FREE_ARG)(v+nl-NR_END));
2073: }
2074:
2075: /******************* imatrix *******************************/
2076: int **imatrix(long nrl, long nrh, long ncl, long nch)
2077: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
2078: {
2079: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
2080: int **m;
2081:
2082: /* allocate pointers to rows */
2083: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
2084: if (!m) nrerror("allocation failure 1 in matrix()");
2085: m += NR_END;
2086: m -= nrl;
2087:
2088:
2089: /* allocate rows and set pointers to them */
2090: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
2091: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2092: m[nrl] += NR_END;
2093: m[nrl] -= ncl;
2094:
2095: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
2096:
2097: /* return pointer to array of pointers to rows */
2098: return m;
2099: }
2100:
2101: /****************** free_imatrix *************************/
2102: void free_imatrix(m,nrl,nrh,ncl,nch)
2103: int **m;
2104: long nch,ncl,nrh,nrl;
2105: /* free an int matrix allocated by imatrix() */
2106: {
2107: free((FREE_ARG) (m[nrl]+ncl-NR_END));
2108: free((FREE_ARG) (m+nrl-NR_END));
2109: }
2110:
2111: /******************* matrix *******************************/
2112: double **matrix(long nrl, long nrh, long ncl, long nch)
2113: {
2114: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
2115: double **m;
2116:
2117: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2118: if (!m) nrerror("allocation failure 1 in matrix()");
2119: m += NR_END;
2120: m -= nrl;
2121:
2122: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2123: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2124: m[nrl] += NR_END;
2125: m[nrl] -= ncl;
2126:
2127: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2128: return m;
1.145 brouard 2129: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
2130: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
2131: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 2132: */
2133: }
2134:
2135: /*************************free matrix ************************/
2136: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
2137: {
2138: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2139: free((FREE_ARG)(m+nrl-NR_END));
2140: }
2141:
2142: /******************* ma3x *******************************/
2143: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
2144: {
2145: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
2146: double ***m;
2147:
2148: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
2149: if (!m) nrerror("allocation failure 1 in matrix()");
2150: m += NR_END;
2151: m -= nrl;
2152:
2153: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
2154: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
2155: m[nrl] += NR_END;
2156: m[nrl] -= ncl;
2157:
2158: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
2159:
2160: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
2161: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
2162: m[nrl][ncl] += NR_END;
2163: m[nrl][ncl] -= nll;
2164: for (j=ncl+1; j<=nch; j++)
2165: m[nrl][j]=m[nrl][j-1]+nlay;
2166:
2167: for (i=nrl+1; i<=nrh; i++) {
2168: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
2169: for (j=ncl+1; j<=nch; j++)
2170: m[i][j]=m[i][j-1]+nlay;
2171: }
2172: return m;
2173: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
2174: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
2175: */
2176: }
2177:
2178: /*************************free ma3x ************************/
2179: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
2180: {
2181: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
2182: free((FREE_ARG)(m[nrl]+ncl-NR_END));
2183: free((FREE_ARG)(m+nrl-NR_END));
2184: }
2185:
2186: /*************** function subdirf ***********/
2187: char *subdirf(char fileres[])
2188: {
2189: /* Caution optionfilefiname is hidden */
2190: strcpy(tmpout,optionfilefiname);
2191: strcat(tmpout,"/"); /* Add to the right */
2192: strcat(tmpout,fileres);
2193: return tmpout;
2194: }
2195:
2196: /*************** function subdirf2 ***********/
2197: char *subdirf2(char fileres[], char *preop)
2198: {
1.314 brouard 2199: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
2200: Errors in subdirf, 2, 3 while printing tmpout is
1.315 brouard 2201: rewritten within the same printf. Workaround: many printfs */
1.126 brouard 2202: /* Caution optionfilefiname is hidden */
2203: strcpy(tmpout,optionfilefiname);
2204: strcat(tmpout,"/");
2205: strcat(tmpout,preop);
2206: strcat(tmpout,fileres);
2207: return tmpout;
2208: }
2209:
2210: /*************** function subdirf3 ***********/
2211: char *subdirf3(char fileres[], char *preop, char *preop2)
2212: {
2213:
2214: /* Caution optionfilefiname is hidden */
2215: strcpy(tmpout,optionfilefiname);
2216: strcat(tmpout,"/");
2217: strcat(tmpout,preop);
2218: strcat(tmpout,preop2);
2219: strcat(tmpout,fileres);
2220: return tmpout;
2221: }
1.213 brouard 2222:
2223: /*************** function subdirfext ***********/
2224: char *subdirfext(char fileres[], char *preop, char *postop)
2225: {
2226:
2227: strcpy(tmpout,preop);
2228: strcat(tmpout,fileres);
2229: strcat(tmpout,postop);
2230: return tmpout;
2231: }
1.126 brouard 2232:
1.213 brouard 2233: /*************** function subdirfext3 ***********/
2234: char *subdirfext3(char fileres[], char *preop, char *postop)
2235: {
2236:
2237: /* Caution optionfilefiname is hidden */
2238: strcpy(tmpout,optionfilefiname);
2239: strcat(tmpout,"/");
2240: strcat(tmpout,preop);
2241: strcat(tmpout,fileres);
2242: strcat(tmpout,postop);
2243: return tmpout;
2244: }
2245:
1.162 brouard 2246: char *asc_diff_time(long time_sec, char ascdiff[])
2247: {
2248: long sec_left, days, hours, minutes;
2249: days = (time_sec) / (60*60*24);
2250: sec_left = (time_sec) % (60*60*24);
2251: hours = (sec_left) / (60*60) ;
2252: sec_left = (sec_left) %(60*60);
2253: minutes = (sec_left) /60;
2254: sec_left = (sec_left) % (60);
2255: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2256: return ascdiff;
2257: }
2258:
1.126 brouard 2259: /***************** f1dim *************************/
2260: extern int ncom;
2261: extern double *pcom,*xicom;
2262: extern double (*nrfunc)(double []);
2263:
2264: double f1dim(double x)
2265: {
2266: int j;
2267: double f;
2268: double *xt;
2269:
2270: xt=vector(1,ncom);
2271: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2272: f=(*nrfunc)(xt);
2273: free_vector(xt,1,ncom);
2274: return f;
2275: }
2276:
2277: /*****************brent *************************/
2278: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 2279: {
2280: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2281: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2282: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2283: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2284: * returned function value.
2285: */
1.126 brouard 2286: int iter;
2287: double a,b,d,etemp;
1.159 brouard 2288: double fu=0,fv,fw,fx;
1.164 brouard 2289: double ftemp=0.;
1.126 brouard 2290: double p,q,r,tol1,tol2,u,v,w,x,xm;
2291: double e=0.0;
2292:
2293: a=(ax < cx ? ax : cx);
2294: b=(ax > cx ? ax : cx);
2295: x=w=v=bx;
2296: fw=fv=fx=(*f)(x);
2297: for (iter=1;iter<=ITMAX;iter++) {
2298: xm=0.5*(a+b);
2299: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2300: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2301: printf(".");fflush(stdout);
2302: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 2303: #ifdef DEBUGBRENT
1.126 brouard 2304: 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);
2305: 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);
2306: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2307: #endif
2308: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2309: *xmin=x;
2310: return fx;
2311: }
2312: ftemp=fu;
2313: if (fabs(e) > tol1) {
2314: r=(x-w)*(fx-fv);
2315: q=(x-v)*(fx-fw);
2316: p=(x-v)*q-(x-w)*r;
2317: q=2.0*(q-r);
2318: if (q > 0.0) p = -p;
2319: q=fabs(q);
2320: etemp=e;
2321: e=d;
2322: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 2323: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 2324: else {
1.224 brouard 2325: d=p/q;
2326: u=x+d;
2327: if (u-a < tol2 || b-u < tol2)
2328: d=SIGN(tol1,xm-x);
1.126 brouard 2329: }
2330: } else {
2331: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2332: }
2333: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2334: fu=(*f)(u);
2335: if (fu <= fx) {
2336: if (u >= x) a=x; else b=x;
2337: SHFT(v,w,x,u)
1.183 brouard 2338: SHFT(fv,fw,fx,fu)
2339: } else {
2340: if (u < x) a=u; else b=u;
2341: if (fu <= fw || w == x) {
1.224 brouard 2342: v=w;
2343: w=u;
2344: fv=fw;
2345: fw=fu;
1.183 brouard 2346: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 2347: v=u;
2348: fv=fu;
1.183 brouard 2349: }
2350: }
1.126 brouard 2351: }
2352: nrerror("Too many iterations in brent");
2353: *xmin=x;
2354: return fx;
2355: }
2356:
2357: /****************** mnbrak ***********************/
2358:
2359: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2360: double (*func)(double))
1.183 brouard 2361: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2362: the downhill direction (defined by the function as evaluated at the initial points) and returns
2363: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2364: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2365: */
1.126 brouard 2366: double ulim,u,r,q, dum;
2367: double fu;
1.187 brouard 2368:
2369: double scale=10.;
2370: int iterscale=0;
2371:
2372: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2373: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2374:
2375:
2376: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2377: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2378: /* *bx = *ax - (*ax - *bx)/scale; */
2379: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2380: /* } */
2381:
1.126 brouard 2382: if (*fb > *fa) {
2383: SHFT(dum,*ax,*bx,dum)
1.183 brouard 2384: SHFT(dum,*fb,*fa,dum)
2385: }
1.126 brouard 2386: *cx=(*bx)+GOLD*(*bx-*ax);
2387: *fc=(*func)(*cx);
1.183 brouard 2388: #ifdef DEBUG
1.224 brouard 2389: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2390: 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 2391: #endif
1.224 brouard 2392: 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 2393: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 2394: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 2395: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 2396: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2397: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2398: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 2399: fu=(*func)(u);
1.163 brouard 2400: #ifdef DEBUG
2401: /* f(x)=A(x-u)**2+f(u) */
2402: double A, fparabu;
2403: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2404: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 2405: 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);
2406: 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 2407: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2408: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2409: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2410: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 2411: #endif
1.184 brouard 2412: #ifdef MNBRAKORIGINAL
1.183 brouard 2413: #else
1.191 brouard 2414: /* if (fu > *fc) { */
2415: /* #ifdef DEBUG */
2416: /* printf("mnbrak4 fu > fc \n"); */
2417: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2418: /* #endif */
2419: /* /\* 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 *\\/ *\/ */
2420: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2421: /* dum=u; /\* Shifting c and u *\/ */
2422: /* u = *cx; */
2423: /* *cx = dum; */
2424: /* dum = fu; */
2425: /* fu = *fc; */
2426: /* *fc =dum; */
2427: /* } else { /\* end *\/ */
2428: /* #ifdef DEBUG */
2429: /* printf("mnbrak3 fu < fc \n"); */
2430: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2431: /* #endif */
2432: /* dum=u; /\* Shifting c and u *\/ */
2433: /* u = *cx; */
2434: /* *cx = dum; */
2435: /* dum = fu; */
2436: /* fu = *fc; */
2437: /* *fc =dum; */
2438: /* } */
1.224 brouard 2439: #ifdef DEBUGMNBRAK
2440: double A, fparabu;
2441: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2442: fparabu= *fa - A*(*ax-u)*(*ax-u);
2443: 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);
2444: 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 2445: #endif
1.191 brouard 2446: dum=u; /* Shifting c and u */
2447: u = *cx;
2448: *cx = dum;
2449: dum = fu;
2450: fu = *fc;
2451: *fc =dum;
1.183 brouard 2452: #endif
1.162 brouard 2453: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 2454: #ifdef DEBUG
1.224 brouard 2455: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2456: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 2457: #endif
1.126 brouard 2458: fu=(*func)(u);
2459: if (fu < *fc) {
1.183 brouard 2460: #ifdef DEBUG
1.224 brouard 2461: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2462: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2463: #endif
2464: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2465: SHFT(*fb,*fc,fu,(*func)(u))
2466: #ifdef DEBUG
2467: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 2468: #endif
2469: }
1.162 brouard 2470: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 2471: #ifdef DEBUG
1.224 brouard 2472: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2473: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 2474: #endif
1.126 brouard 2475: u=ulim;
2476: fu=(*func)(u);
1.183 brouard 2477: } else { /* u could be left to b (if r > q parabola has a maximum) */
2478: #ifdef DEBUG
1.224 brouard 2479: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2480: 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 2481: #endif
1.126 brouard 2482: u=(*cx)+GOLD*(*cx-*bx);
2483: fu=(*func)(u);
1.224 brouard 2484: #ifdef DEBUG
2485: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2486: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2487: #endif
1.183 brouard 2488: } /* end tests */
1.126 brouard 2489: SHFT(*ax,*bx,*cx,u)
1.183 brouard 2490: SHFT(*fa,*fb,*fc,fu)
2491: #ifdef DEBUG
1.224 brouard 2492: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2493: 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 2494: #endif
2495: } /* 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 2496: }
2497:
2498: /*************** linmin ************************/
1.162 brouard 2499: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2500: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2501: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2502: the value of func at the returned location p . This is actually all accomplished by calling the
2503: routines mnbrak and brent .*/
1.126 brouard 2504: int ncom;
2505: double *pcom,*xicom;
2506: double (*nrfunc)(double []);
2507:
1.224 brouard 2508: #ifdef LINMINORIGINAL
1.126 brouard 2509: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 2510: #else
2511: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2512: #endif
1.126 brouard 2513: {
2514: double brent(double ax, double bx, double cx,
2515: double (*f)(double), double tol, double *xmin);
2516: double f1dim(double x);
2517: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2518: double *fc, double (*func)(double));
2519: int j;
2520: double xx,xmin,bx,ax;
2521: double fx,fb,fa;
1.187 brouard 2522:
1.203 brouard 2523: #ifdef LINMINORIGINAL
2524: #else
2525: double scale=10., axs, xxs; /* Scale added for infinity */
2526: #endif
2527:
1.126 brouard 2528: ncom=n;
2529: pcom=vector(1,n);
2530: xicom=vector(1,n);
2531: nrfunc=func;
2532: for (j=1;j<=n;j++) {
2533: pcom[j]=p[j];
1.202 brouard 2534: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 2535: }
1.187 brouard 2536:
1.203 brouard 2537: #ifdef LINMINORIGINAL
2538: xx=1.;
2539: #else
2540: axs=0.0;
2541: xxs=1.;
2542: do{
2543: xx= xxs;
2544: #endif
1.187 brouard 2545: ax=0.;
2546: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2547: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2548: /* 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)) */
2549: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2550: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2551: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2552: /* 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 2553: #ifdef LINMINORIGINAL
2554: #else
2555: if (fx != fx){
1.224 brouard 2556: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2557: printf("|");
2558: fprintf(ficlog,"|");
1.203 brouard 2559: #ifdef DEBUGLINMIN
1.224 brouard 2560: 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 2561: #endif
2562: }
1.224 brouard 2563: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 2564: #endif
2565:
1.191 brouard 2566: #ifdef DEBUGLINMIN
2567: 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 2568: 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 2569: #endif
1.224 brouard 2570: #ifdef LINMINORIGINAL
2571: #else
1.317 brouard 2572: if(fb == fx){ /* Flat function in the direction */
2573: xmin=xx;
1.224 brouard 2574: *flat=1;
1.317 brouard 2575: }else{
1.224 brouard 2576: *flat=0;
2577: #endif
2578: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2579: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2580: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2581: /* fmin = f(p[j] + xmin * xi[j]) */
2582: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2583: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2584: #ifdef DEBUG
1.224 brouard 2585: 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);
2586: 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);
2587: #endif
2588: #ifdef LINMINORIGINAL
2589: #else
2590: }
1.126 brouard 2591: #endif
1.191 brouard 2592: #ifdef DEBUGLINMIN
2593: printf("linmin end ");
1.202 brouard 2594: fprintf(ficlog,"linmin end ");
1.191 brouard 2595: #endif
1.126 brouard 2596: for (j=1;j<=n;j++) {
1.203 brouard 2597: #ifdef LINMINORIGINAL
2598: xi[j] *= xmin;
2599: #else
2600: #ifdef DEBUGLINMIN
2601: if(xxs <1.0)
2602: printf(" before xi[%d]=%12.8f", j,xi[j]);
2603: #endif
2604: 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) */
2605: #ifdef DEBUGLINMIN
2606: if(xxs <1.0)
2607: 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 );
2608: #endif
2609: #endif
1.187 brouard 2610: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2611: }
1.191 brouard 2612: #ifdef DEBUGLINMIN
1.203 brouard 2613: printf("\n");
1.191 brouard 2614: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2615: 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 2616: for (j=1;j<=n;j++) {
1.202 brouard 2617: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2618: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2619: if(j % ncovmodel == 0){
1.191 brouard 2620: printf("\n");
1.202 brouard 2621: fprintf(ficlog,"\n");
2622: }
1.191 brouard 2623: }
1.203 brouard 2624: #else
1.191 brouard 2625: #endif
1.126 brouard 2626: free_vector(xicom,1,n);
2627: free_vector(pcom,1,n);
2628: }
2629:
1.359 ! brouard 2630: /**** praxis gegen ****/
! 2631:
! 2632: /* This has been tested by Visual C from Microsoft and works */
! 2633: /* meaning tha valgrind could be wrong */
! 2634: /*********************************************************************/
! 2635: /* f u n c t i o n p r a x i s */
! 2636: /* */
! 2637: /* praxis is a general purpose routine for the minimization of a */
! 2638: /* function in several variables. the algorithm used is a modifi- */
! 2639: /* cation of conjugate gradient search method by powell. the changes */
! 2640: /* are due to r.p. brent, who gives an algol-w program, which served */
! 2641: /* as a basis for this function. */
! 2642: /* */
! 2643: /* references: */
! 2644: /* - powell, m.j.d., 1964. an efficient method for finding */
! 2645: /* the minimum of a function in several variables without */
! 2646: /* calculating derivatives, computer journal, 7, 155-162 */
! 2647: /* - brent, r.p., 1973. algorithms for minimization without */
! 2648: /* derivatives, prentice hall, englewood cliffs. */
! 2649: /* */
! 2650: /* problems, suggestions or improvements are always wellcome */
! 2651: /* karl gegenfurtner 07/08/87 */
! 2652: /* c - version */
! 2653: /*********************************************************************/
! 2654: /* */
! 2655: /* usage: min = praxis(tol, macheps, h, n, prin, x, func) */
! 2656: /* macheps has been suppressed because it is replaced by DBL_EPSILON */
! 2657: /* and if it was an argument of praxis (as it is in original brent) */
! 2658: /* it should be declared external */
! 2659: /* usage: min = praxis(tol, h, n, prin, x, func) */
! 2660: /* was min = praxis(fun, x, n); */
! 2661: /* */
! 2662: /* fun the function to be minimized. fun is called from */
! 2663: /* praxis with x and n as arguments */
! 2664: /* x a double array containing the initial guesses for */
! 2665: /* the minimum, which will contain the solution on */
! 2666: /* return */
! 2667: /* n an integer specifying the number of unknown */
! 2668: /* parameters */
! 2669: /* min praxis returns the least calculated value of fun */
! 2670: /* */
! 2671: /* some additional global variables control some more aspects of */
! 2672: /* the inner workings of praxis. setting them is optional, they */
! 2673: /* are all set to some reasonable default values given below. */
! 2674: /* */
! 2675: /* prin controls the printed output from the routine. */
! 2676: /* 0 -> no output */
! 2677: /* 1 -> print only starting and final values */
! 2678: /* 2 -> detailed map of the minimization process */
! 2679: /* 3 -> print also eigenvalues and vectors of the */
! 2680: /* search directions */
! 2681: /* the default value is 1 */
! 2682: /* tol is the tolerance allowed for the precision of the */
! 2683: /* solution. praxis returns if the criterion */
! 2684: /* 2 * ||x[k]-x[k-1]|| <= sqrt(macheps) * ||x[k]|| + tol */
! 2685: /* is fulfilled more than ktm times. */
! 2686: /* the default value depends on the machine precision */
! 2687: /* ktm see just above. default is 1, and a value of 4 leads */
! 2688: /* to a very(!) cautious stopping criterion. */
! 2689: /* h0 or step is a steplength parameter and should be set equal */
! 2690: /* to the expected distance from the solution. */
! 2691: /* exceptionally small or large values of step lead to */
! 2692: /* slower convergence on the first few iterations */
! 2693: /* the default value for step is 1.0 */
! 2694: /* scbd is a scaling parameter. 1.0 is the default and */
! 2695: /* indicates no scaling. if the scales for the different */
! 2696: /* parameters are very different, scbd should be set to */
! 2697: /* a value of about 10.0. */
! 2698: /* illc should be set to true (1) if the problem is known to */
! 2699: /* be ill-conditioned. the default is false (0). this */
! 2700: /* variable is automatically set, when praxis finds */
! 2701: /* the problem to be ill-conditioned during iterations. */
! 2702: /* maxfun is the maximum number of calls to fun allowed. praxis */
! 2703: /* will return after maxfun calls to fun even when the */
! 2704: /* minimum is not yet found. the default value of 0 */
! 2705: /* indicates no limit on the number of calls. */
! 2706: /* this return condition is only checked every n */
! 2707: /* iterations. */
! 2708: /* */
! 2709: /*********************************************************************/
! 2710:
! 2711: #include <math.h>
! 2712: #include <stdio.h>
! 2713: #include <stdlib.h>
! 2714: #include <float.h> /* for DBL_EPSILON */
! 2715: /* #include "machine.h" */
! 2716:
! 2717:
! 2718: /* extern void minfit(int n, double eps, double tol, double **ab, double q[]); */
! 2719: /* extern void minfit(int n, double eps, double tol, double ab[N][N], double q[]); */
! 2720: /* control parameters */
! 2721: /* control parameters */
! 2722: #define SQREPSILON 1.0e-19
! 2723: /* #define EPSILON 1.0e-8 */ /* in main */
! 2724:
! 2725: double tol = SQREPSILON,
! 2726: scbd = 1.0,
! 2727: step = 1.0;
! 2728: int ktm = 1,
! 2729: /* prin = 2, */
! 2730: maxfun = 0,
! 2731: illc = 0;
! 2732:
! 2733: /* some global variables */
! 2734: static int i, j, k, k2, nl, nf, kl, kt;
! 2735: /* static double s; */
! 2736: double sl, dn, dmin,
! 2737: fx, f1, lds, ldt, sf, df,
! 2738: qf1, qd0, qd1, qa, qb, qc,
! 2739: m2, m4, small_windows, vsmall, large,
! 2740: vlarge, ldfac, t2;
! 2741: /* static double d[N], y[N], z[N], */
! 2742: /* q0[N], q1[N], v[N][N]; */
! 2743:
! 2744: static double *d, *y, *z;
! 2745: static double *q0, *q1, **v;
! 2746: double *tflin; /* used in flin: return (*fun)(tflin, n); */
! 2747: double *e; /* used in minfit, don't konw how to free memory and thus made global */
! 2748: /* static double s, sl, dn, dmin, */
! 2749: /* fx, f1, lds, ldt, sf, df, */
! 2750: /* qf1, qd0, qd1, qa, qb, qc, */
! 2751: /* m2, m4, small, vsmall, large, */
! 2752: /* vlarge, ldfac, t2; */
! 2753: /* static double d[N], y[N], z[N], */
! 2754: /* q0[N], q1[N], v[N][N]; */
! 2755:
! 2756: /* these will be set by praxis to point to it's arguments */
! 2757: static int prin; /* added */
! 2758: static int n;
! 2759: static double *x;
! 2760: static double (*fun)();
! 2761: /* static double (*fun)(double *x, int n); */
! 2762:
! 2763: /* these will be set by praxis to the global control parameters */
! 2764: /* static double h, macheps, t; */
! 2765: extern double macheps;
! 2766: static double h;
! 2767: static double t;
! 2768:
! 2769: static double
! 2770: drandom() /* return random no between 0 and 1 */
! 2771: {
! 2772: return (double)(rand()%(8192*2))/(double)(8192*2);
! 2773: }
! 2774:
! 2775: static void sort() /* d and v in descending order */
! 2776: {
! 2777: int k, i, j;
! 2778: double s;
! 2779:
! 2780: for (i=1; i<=n-1; i++) {
! 2781: k = i; s = d[i];
! 2782: for (j=i+1; j<=n; j++) {
! 2783: if (d[j] > s) {
! 2784: k = j;
! 2785: s = d[j];
! 2786: }
! 2787: }
! 2788: if (k > i) {
! 2789: d[k] = d[i];
! 2790: d[i] = s;
! 2791: for (j=1; j<=n; j++) {
! 2792: s = v[j][i];
! 2793: v[j][i] = v[j][k];
! 2794: v[j][k] = s;
! 2795: }
! 2796: }
! 2797: }
! 2798: }
! 2799:
! 2800: double randbrent ( int *naught )
! 2801: {
! 2802: double ran1, ran3[127], half;
! 2803: int ran2, q, r, i, j;
! 2804: int init=0; /* false */
! 2805: double rr;
! 2806: /* REAL*8 RAN1,RAN3(127),HALF */
! 2807:
! 2808: /* INTEGER RAN2,Q,R */
! 2809: /* LOGICAL INIT */
! 2810: /* DATA INIT/.FALSE./ */
! 2811: /* IF (INIT) GO TO 3 */
! 2812: if(!init){
! 2813: /* R = MOD(NAUGHT,8190) + 1 *//* 1804289383 rand () */
! 2814: r = *naught % 8190 + 1;/* printf(" naught r %d %d",*naught,r); */
! 2815: ran2=127;
! 2816: for(i=ran2; i>0; i--){
! 2817: /* RAN2 = 128 */
! 2818: /* DO 2 I=1,127 */
! 2819: ran2 = ran2-1;
! 2820: /* RAN2 = RAN2 - 1 */
! 2821: ran1 = -pow(2.0,55);
! 2822: /* RAN1 = -2.D0**55 */
! 2823: /* DO 1 J=1,7 */
! 2824: for(j=1; j<=7;j++){
! 2825: /* R = MOD(1756*R,8191) */
! 2826: r = (1756*r) % 8191;/* printf(" i=%d (1756*r)%8191=%d",j,r); */
! 2827: q=r/32;
! 2828: /* Q = R/32 */
! 2829: /* 1 RAN1 = (RAN1 + Q)*(1.0D0/256) */
! 2830: ran1 =(ran1+q)*(1.0/256);
! 2831: }
! 2832: /* 2 RAN3(RAN2) = RAN1 */
! 2833: ran3[ran2] = ran1; /* printf(" ran2=%d ran1=%.7g \n",ran2,ran1); */
! 2834: }
! 2835: /* INIT = .TRUE. */
! 2836: init=1;
! 2837: /* 3 IF (RAN2.EQ.1) RAN2 = 128 */
! 2838: }
! 2839: if(ran2 == 0) ran2 = 126;
! 2840: else ran2 = ran2 -1;
! 2841: /* RAN2 = RAN2 - 1 */
! 2842: /* RAN1 = RAN1 + RAN3(RAN2) */
! 2843: ran1 = ran1 + ran3[ran2];/* printf("BIS ran2=%d ran1=%.7g \n",ran2,ran1); */
! 2844: half= 0.5;
! 2845: /* HALF = .5D0 */
! 2846: /* IF (RAN1.GE.0.D0) HALF = -HALF */
! 2847: if(ran1 >= 0.) half =-half;
! 2848: ran1 = ran1 +half;
! 2849: ran3[ran2] = ran1;
! 2850: rr= ran1+0.5;
! 2851: /* RAN1 = RAN1 + HALF */
! 2852: /* RAN3(RAN2) = RAN1 */
! 2853: /* RANDOM = RAN1 + .5D0 */
! 2854: /* r = ( ( double ) ( *seed ) ) * 4.656612875E-10; */
! 2855: return rr;
! 2856: }
! 2857: static void matprint(char *s, double **v, int m, int n)
! 2858: /* char *s; */
! 2859: /* double v[N][N]; */
! 2860: {
! 2861: #define INCX 8
! 2862: int i;
! 2863:
! 2864: int i2hi;
! 2865: int ihi;
! 2866: int ilo;
! 2867: int i2lo;
! 2868: int jlo=1;
! 2869: int j;
! 2870: int j2hi;
! 2871: int jhi;
! 2872: int j2lo;
! 2873: ilo=1;
! 2874: ihi=n;
! 2875: jlo=1;
! 2876: jhi=n;
! 2877:
! 2878: printf ("\n" );
! 2879: printf ("%s\n", s );
! 2880: for ( j2lo = jlo; j2lo <= jhi; j2lo = j2lo + INCX )
! 2881: {
! 2882: j2hi = j2lo + INCX - 1;
! 2883: if ( n < j2hi )
! 2884: {
! 2885: j2hi = n;
! 2886: }
! 2887: if ( jhi < j2hi )
! 2888: {
! 2889: j2hi = jhi;
! 2890: }
! 2891:
! 2892: /* fprintf ( ficlog, "\n" ); */
! 2893: printf ("\n" );
! 2894: /*
! 2895: For each column J in the current range...
! 2896:
! 2897: Write the header.
! 2898: */
! 2899: /* fprintf ( ficlog, " Col: "); */
! 2900: printf ("Col:");
! 2901: for ( j = j2lo; j <= j2hi; j++ )
! 2902: {
! 2903: /* fprintf ( ficlog, " %7d ", j - 1 ); */
! 2904: /* printf (" %9d ", j - 1 ); */
! 2905: printf (" %9d ", j );
! 2906: }
! 2907: /* fprintf ( ficlog, "\n" ); */
! 2908: /* fprintf ( ficlog, " Row\n" ); */
! 2909: /* fprintf ( ficlog, "\n" ); */
! 2910: printf ("\n" );
! 2911: printf (" Row\n" );
! 2912: printf ("\n" );
! 2913: /*
! 2914: Determine the range of the rows in this strip.
! 2915: */
! 2916: if ( 1 < ilo ){
! 2917: i2lo = ilo;
! 2918: }else{
! 2919: i2lo = 1;
! 2920: }
! 2921: if ( m < ihi ){
! 2922: i2hi = m;
! 2923: }else{
! 2924: i2hi = ihi;
! 2925: }
! 2926:
! 2927: for ( i = i2lo; i <= i2hi; i++ ){
! 2928: /*
! 2929: Print out (up to) 5 entries in row I, that lie in the current strip.
! 2930: */
! 2931: /* fprintf ( ficlog, "%5d:", i - 1 ); */
! 2932: /* printf ("%5d:", i - 1 ); */
! 2933: printf ("%5d:", i );
! 2934: for ( j = j2lo; j <= j2hi; j++ )
! 2935: {
! 2936: /* fprintf ( ficlog, " %14g", a[i-1+(j-1)*m] ); */
! 2937: /* printf ("%14.7g ", a[i-1+(j-1)*m] ); */
! 2938: /* printf("%14.7f ", v[i-1][j-1]); */
! 2939: printf("%14.7f ", v[i][j]);
! 2940: /* fprintf ( stdout, " %14g", a[i-1+(j-1)*m] ); */
! 2941: }
! 2942: /* fprintf ( ficlog, "\n" ); */
! 2943: printf ("\n" );
! 2944: }
! 2945: }
! 2946:
! 2947: /* printf("%s\n", s); */
! 2948: /* for (k=0; k<n; k++) { */
! 2949: /* for (i=0; i<n; i++) { */
! 2950: /* /\* printf("%20.10e ", v[k][i]); *\/ */
! 2951: /* } */
! 2952: /* printf("\n"); */
! 2953: /* } */
! 2954: #undef INCX
! 2955: }
! 2956:
! 2957: void vecprint(char *s, double *x, int n)
! 2958: /* char *s; */
! 2959: /* double x[N]; */
! 2960: {
! 2961: int i=0;
! 2962:
! 2963: printf(" %s", s);
! 2964: /* for (i=0; i<n; i++) */
! 2965: for (i=1; i<=n; i++)
! 2966: printf (" %14.7g", x[i] );
! 2967: /* printf(" %8d: %14g\n", i, x[i]); */
! 2968: printf ("\n" );
! 2969: }
! 2970:
! 2971: static void print() /* print a line of traces */
! 2972: {
! 2973:
! 2974:
! 2975: printf("\n");
! 2976: /* printf("... chi square reduced to ... %20.10e\n", fx); */
! 2977: /* printf("... after %u function calls ...\n", nf); */
! 2978: /* printf("... including %u linear searches ...\n", nl); */
! 2979: printf("%10d %10d%14.7g",nl, nf, fx);
! 2980: vecprint("... current values of x ...", x, n);
! 2981: }
! 2982: /* static void print2(int n, double *x, int prin, double fx, int nf, int nl) */ /* print a line of traces */
! 2983: static void print2() /* print a line of traces */
! 2984: {
! 2985: int i; double fmin=0.;
! 2986:
! 2987: /* printf("\n"); */
! 2988: /* printf("... chi square reduced to ... %20.10e\n", fx); */
! 2989: /* printf("... after %u function calls ...\n", nf); */
! 2990: /* printf("... including %u linear searches ...\n", nl); */
! 2991: /* printf("%10d %10d%14.7g",nl, nf, fx); */
! 2992: printf ( "\n" );
! 2993: printf ( " Linear searches %d", nl );
! 2994: /* printf ( " Linear searches %d\n", nl ); */
! 2995: /* printf ( " Function evaluations %d\n", nf ); */
! 2996: /* printf ( " Function value FX = %g\n", fx ); */
! 2997: printf ( " Function evaluations %d", nf );
! 2998: printf ( " Function value FX = %.12lf\n", fx );
! 2999: #ifdef DEBUGPRAX
! 3000: printf("n=%d prin=%d\n",n,prin);
! 3001: #endif
! 3002: if(fx <= fmin) printf(" UNDEFINED "); else printf("%14.7g",log(fx-fmin));
! 3003: if ( n <= 4 || 2 < prin )
! 3004: {
! 3005: /* for(i=1;i<=n;i++)printf("%14.7g",x[i-1]); */
! 3006: for(i=1;i<=n;i++)printf("%14.7g",x[i]);
! 3007: /* r8vec_print ( n, x, " X:" ); */
! 3008: }
! 3009: printf("\n");
! 3010: }
! 3011:
! 3012:
! 3013: /* #ifdef MSDOS */
! 3014: /* static double tflin[N]; */
! 3015: /* #endif */
! 3016:
! 3017: static double flin(double l, int j)
! 3018: /* double l; */
! 3019: {
! 3020: int i;
! 3021: /* #ifndef MSDOS */
! 3022: /* double tflin[N]; */
! 3023: /* #endif */
! 3024: /* double *tflin; */ /* Be careful to put tflin on a vector n */
! 3025:
! 3026: /* j is used from 0 to n-1 and can be -1 for parabolic search */
! 3027:
! 3028: /* if (j != -1) { /\* linear search *\/ */
! 3029: if (j > 0) { /* linear search */
! 3030: /* for (i=0; i<n; i++){ */
! 3031: for (i=1; i<=n; i++){
! 3032: tflin[i] = x[i] + l *v[i][j];
! 3033: #ifdef DEBUGPRAX
! 3034: /* 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); */
! 3035: 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);
! 3036: #endif
! 3037: }
! 3038: }
! 3039: else { /* search along parabolic space curve */
! 3040: qa = l*(l-qd1)/(qd0*(qd0+qd1));
! 3041: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
! 3042: qc = l*(l+qd0)/(qd1*(qd0+qd1));
! 3043: #ifdef DEBUGPRAX
! 3044: 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);
! 3045: #endif
! 3046: /* for (i=0; i<n; i++){ */
! 3047: for (i=1; i<=n; i++){
! 3048: tflin[i] = qa*q0[i]+qb*x[i]+qc*q1[i];
! 3049: #ifdef DEBUGPRAX
! 3050: /* 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]); */
! 3051: 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]);
! 3052: #endif
! 3053: }
! 3054: }
! 3055: nf++;
! 3056:
! 3057: #ifdef NR_SHIFT
! 3058: return (*fun)((tflin-1), n);
! 3059: #else
! 3060: /* return (*fun)(tflin, n);*/
! 3061: return (*fun)(tflin);
! 3062: #endif
! 3063: }
! 3064:
! 3065: void minny(int j, int nits, double *d2, double *x1, double f1, int fk)
! 3066: /* double *d2, *x1, f1; */
! 3067: {
! 3068: /* here j is from 0 to n-1 and can be -1 for parabolic search */
! 3069: /* MINIMIZES F FROM X IN THE DIRECTION V(*,J) */
! 3070: /* UNLESS J<1, WHEN A QUADRATIC SEARCH IS DONE */
! 3071: /* IN THE PLANE DEFINED BY Q0, Q1 AND X. */
! 3072: /* D2 AN APPROXIMATION TO HALF F'' (OR ZERO), */
! 3073: /* X1 AN ESTIMATE OF DISTANCE TO MINIMUM, */
! 3074: /* RETURNED AS THE DISTANCE FOUND. */
! 3075: /* IF FK = TRUE THEN F1 IS FLIN(X1), OTHERWISE */
! 3076: /* X1 AND F1 ARE IGNORED ON ENTRY UNLESS FINAL */
! 3077: /* FX > F1. NITS CONTROLS THE NUMBER OF TIMES */
! 3078: /* AN ATTEMPT IS MADE TO HALVE THE INTERVAL. */
! 3079: /* SIDE EFFECTS: USES AND ALTERS X, FX, NF, NL. */
! 3080: /* IF J < 1 USES VARIABLES Q... . */
! 3081: /* USES H, N, T, M2, M4, LDT, DMIN, MACHEPS; */
! 3082: int k, i, dz;
! 3083: double x2, xm, f0, f2, fm, d1, t2, sf1, sx1;
! 3084: double s;
! 3085: double macheps;
! 3086: macheps=pow(16.0,-13.0);
! 3087: sf1 = f1; sx1 = *x1;
! 3088: k = 0; xm = 0.0; fm = f0 = fx; dz = *d2 < macheps;
! 3089: /* h=1.0;*/ /* To be revised */
! 3090: #ifdef DEBUGPRAX
! 3091: /* printf("min macheps=%14g h=%14g step=%14g t=%14g fx=%14g\n",macheps,h, step,t, fx); */
! 3092: /* Where is fx coming from */
! 3093: printf(" min macheps=%14g h=%14g t=%14g fx=%.9lf dirj=%d\n",macheps, h, t, fx, j);
! 3094: matprint(" min vectors:",v,n,n);
! 3095: #endif
! 3096: /* find step size */
! 3097: s = 0.;
! 3098: /* for (i=0; i<n; i++) s += x[i]*x[i]; */
! 3099: for (i=1; i<=n; i++) s += x[i]*x[i];
! 3100: s = sqrt(s);
! 3101: if (dz)
! 3102: t2 = m4*sqrt(fabs(fx)/dmin + s*ldt) + m2*ldt;
! 3103: else
! 3104: t2 = m4*sqrt(fabs(fx)/(*d2) + s*ldt) + m2*ldt;
! 3105: s = s*m4 + t;
! 3106: if (dz && t2 > s) t2 = s;
! 3107: if (t2 < small_windows) t2 = small_windows;
! 3108: if (t2 > 0.01*h) t2 = 0.01 * h;
! 3109: if (fk && f1 <= fm) {
! 3110: xm = *x1;
! 3111: fm = f1;
! 3112: }
! 3113: #ifdef DEBUGPRAX
! 3114: printf(" additional flin X1=%14.7f t2=%14.7f *f1=%14.7f fm=%14.7f fk=%d\n",*x1,t2,f1,fm,fk);
! 3115: #endif
! 3116: if (!fk || fabs(*x1) < t2) {
! 3117: *x1 = (*x1 >= 0 ? t2 : -t2);
! 3118: /* *x1 = (*x1 > 0 ? t2 : -t2); */ /* kind of error */
! 3119: #ifdef DEBUGPRAX
! 3120: printf(" additional flin X1=%16.10e dirj=%d fk=%d\n",*x1, j, fk);
! 3121: #endif
! 3122: f1 = flin(*x1, j);
! 3123: #ifdef DEBUGPRAX
! 3124: printf(" after flin f1=%18.12e dirj=%d fk=%d\n",f1, j,fk);
! 3125: #endif
! 3126: }
! 3127: if (f1 <= fm) {
! 3128: xm = *x1;
! 3129: fm = f1;
! 3130: }
! 3131: L0: /*L0 loop or next */
! 3132: /*
! 3133: Evaluate FLIN at another point and estimate the second derivative.
! 3134: */
! 3135: if (dz) {
! 3136: x2 = (f0 < f1 ? -(*x1) : 2*(*x1));
! 3137: #ifdef DEBUGPRAX
! 3138: printf(" additional second flin x2=%14.8e x1=%14.8e f0=%14.8e f1=%18.12e dirj=%d\n",x2,*x1,f0,f1,j);
! 3139: #endif
! 3140: f2 = flin(x2, j);
! 3141: #ifdef DEBUGPRAX
! 3142: 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);
! 3143: #endif
! 3144: if (f2 <= fm) {
! 3145: xm = x2;
! 3146: fm = f2;
! 3147: }
! 3148: /* d2 is the curvature or double difference f1 doesn't seem to be accurately computed */
! 3149: *d2 = (x2*(f1-f0) - (*x1)*(f2-f0))/((*x1)*x2*((*x1)-x2));
! 3150: #ifdef DEBUGPRAX
! 3151: double d11,d12;
! 3152: d11=(f1-f0)/(*x1);d12=(f2-f0)/x2;
! 3153: 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)));
! 3154: 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);
! 3155: double ff1=7.783920622852e+04;
! 3156: double f1mf0=9.0344736236e-05;
! 3157: *d2 = (f1mf0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2);
! 3158: /* *d2 = (ff1-f0)/ (*x1)/((*x1)-x2) - (f2-f0)/x2/((*x1)-x2); */
! 3159: printf(" simpliff computing *d2=%16.10e f1mf0=%18.12e,f1=f0+f1mf0=%18.12e\n",*d2,f1mf0,f0+f1mf0);
! 3160: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
! 3161: printf(" overlifi computing *d2=%16.10e\n",*d2);
! 3162: #endif
! 3163: *d2 = ((f1-f0)/ (*x1) - (f2-f0)/x2)/((*x1)-x2);
! 3164: }
! 3165: #ifdef DEBUGPRAX
! 3166: printf(" additional second flin xm=%14.8e fm=%14.8e *d2=%14.8e\n",xm, fm,*d2);
! 3167: #endif
! 3168: /*
! 3169: Estimate the first derivative at 0.
! 3170: */
! 3171: d1 = (f1-f0)/(*x1) - *x1**d2; dz = 1;
! 3172: /*
! 3173: Predict the minimum.
! 3174: */
! 3175: if (*d2 <= small_windows) {
! 3176: x2 = (d1 < 0 ? h : -h);
! 3177: }
! 3178: else {
! 3179: x2 = - 0.5*d1/(*d2);
! 3180: }
! 3181: #ifdef DEBUGPRAX
! 3182: 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);
! 3183: #endif
! 3184: if (fabs(x2) > h)
! 3185: x2 = (x2 > 0 ? h : -h);
! 3186: L1: /* L1 or try loop */
! 3187: #ifdef DEBUGPRAX
! 3188: printf(" AT predicted minimum flin x2=%14.8e x1=%14.8e K=%14d NITS=%14d dirj=%d\n",x2,*x1,k,nits,j);
! 3189: #endif
! 3190: f2 = flin(x2, j); /* x[i]+x2*v[i][j] */
! 3191: #ifdef DEBUGPRAX
! 3192: printf(" after flin f0=%14.8e f1=%14.8e f2=%14.8e fm=%14.8e\n",f0,f1,f2, fm);
! 3193: #endif
! 3194: if ((k < nits) && (f2 > f0)) {
! 3195: #ifdef DEBUGPRAX
! 3196: printf(" NO SUCCESS SO TRY AGAIN;\n");
! 3197: #endif
! 3198: k++;
! 3199: if ((f0 < f1) && (*x1*x2 > 0.0))
! 3200: goto L0; /* or next */
! 3201: x2 *= 0.5;
! 3202: goto L1;
! 3203: }
! 3204: nl++;
! 3205: #ifdef DEBUGPRAX
! 3206: 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);
! 3207: #endif
! 3208: if (f2 > fm) x2 = xm; else fm = f2;
! 3209: if (fabs(x2*(x2-*x1)) > small_windows) {
! 3210: *d2 = (x2*(f1-f0) - *x1*(fm-f0))/(*x1*x2*(*x1-x2));
! 3211: }
! 3212: else {
! 3213: if (k > 0) *d2 = 0;
! 3214: }
! 3215: #ifdef DEBUGPRAX
! 3216: printf(" bebe end of min x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
! 3217: #endif
! 3218: if (*d2 <= small_windows) *d2 = small_windows;
! 3219: *x1 = x2; fx = fm;
! 3220: if (sf1 < fx) {
! 3221: fx = sf1;
! 3222: *x1 = sx1;
! 3223: }
! 3224: /*
! 3225: Update X for linear search.
! 3226: */
! 3227: #ifdef DEBUGPRAX
! 3228: printf(" end of min x1=%14.8e fx=%14.8e d2=%14.8e\n",*x1, fx, *d2);
! 3229: #endif
! 3230:
! 3231: /* if (j != -1) */
! 3232: /* for (i=0; i<n; i++) */
! 3233: /* x[i] += (*x1)*v[i][j]; */
! 3234: if (j > 0)
! 3235: for (i=1; i<=n; i++)
! 3236: x[i] += (*x1)*v[i][j];
! 3237: }
! 3238:
! 3239: void quad() /* look for a minimum along the curve q0, q1, q2 */
! 3240: {
! 3241: int i;
! 3242: double l, s;
! 3243:
! 3244: s = fx; fx = qf1; qf1 = s; qd1 = 0.0;
! 3245: /* for (i=0; i<n; i++) { */
! 3246: for (i=1; i<=n; i++) {
! 3247: s = x[i]; l = q1[i]; x[i] = l; q1[i] = s;
! 3248: qd1 = qd1 + (s-l)*(s-l);
! 3249: }
! 3250: s = 0.0; qd1 = sqrt(qd1); l = qd1;
! 3251: #ifdef DEBUGPRAX
! 3252: printf(" QUAD after sqrt qd1=%14.8e \n",qd1);
! 3253: #endif
! 3254:
! 3255: if (qd0>0.0 && qd1>0.0 &&nl>=3*n*n) {
! 3256: #ifdef DEBUGPRAX
! 3257: printf(" QUAD before min value=%14.8e \n",qf1);
! 3258: #endif
! 3259: /* min(-1, 2, &s, &l, qf1, 1); */
! 3260: minny(0, 2, &s, &l, qf1, 1);
! 3261: qa = l*(l-qd1)/(qd0*(qd0+qd1));
! 3262: qb = (l+qd0)*(qd1-l)/(qd0*qd1);
! 3263: qc = l*(l+qd0)/(qd1*(qd0+qd1));
! 3264: }
! 3265: else {
! 3266: fx = qf1; qa = qb = 0.0; qc = 1.0;
! 3267: }
! 3268: #ifdef DEBUGPRAX
! 3269: printf("after eventual min qd0=%14.8e qd1=%14.8e nl=%d\n",qd0, qd1,nl);
! 3270: #endif
! 3271: qd0 = qd1;
! 3272: /* for (i=0; i<n; i++) { */
! 3273: for (i=1; i<=n; i++) {
! 3274: s = q0[i]; q0[i] = x[i];
! 3275: x[i] = qa*s + qb*x[i] + qc*q1[i];
! 3276: }
! 3277: #ifdef DEBUGQUAD
! 3278: vecprint ( " X after QUAD:" , x, n );
! 3279: #endif
! 3280: }
! 3281:
! 3282: /* void minfit(int n, double eps, double tol, double ab[N][N], double q[]) */
! 3283: void minfit(int n, double eps, double tol, double **ab, double q[])
! 3284: /* int n; */
! 3285: /* double eps, tol, ab[N][N], q[N]; */
! 3286: {
! 3287: int l, kt, l2, i, j, k;
! 3288: double c, f, g, h, s, x, y, z;
! 3289: /* double eps; */
! 3290: /* #ifndef MSDOS */
! 3291: /* double e[N]; /\* plenty of stack on a vax *\/ */
! 3292: /* #endif */
! 3293: /* double *e; */
! 3294: /* e=vector(0,n-1); /\* should be freed somewhere but gotos *\/ */
! 3295:
! 3296: /* householder's reduction to bidiagonal form */
! 3297:
! 3298: if(n==1){
! 3299: /* q[1-1]=ab[1-1][1-1]; */
! 3300: /* ab[1-1][1-1]=1.0; */
! 3301: q[1]=ab[1][1];
! 3302: ab[1][1]=1.0;
! 3303: return; /* added from hardt */
! 3304: }
! 3305: /* eps=macheps; */ /* added */
! 3306: x = g = 0.0;
! 3307: #ifdef DEBUGPRAX
! 3308: matprint (" HOUSE holder:", ab, n, n);
! 3309: #endif
! 3310:
! 3311: /* for (i=0; i<n; i++) { /\* FOR I := 1 UNTIL N DO *\/ */
! 3312: for (i=1; i<=n; i++) { /* FOR I := 1 UNTIL N DO */
! 3313: e[i] = g; s = 0.0; l = i+1;
! 3314: /* for (j=i; j<n; j++) /\* FOR J := I UNTIL N DO S := S*AB(J,I)**2; *\/ /\* not correct *\/ */
! 3315: for (j=i; j<=n; j++) /* FOR J := I UNTIL N DO S := S*AB(J,I)**2; */ /* not correct */
! 3316: s += ab[j][i] * ab[j][i];
! 3317: #ifdef DEBUGPRAXFIN
! 3318: printf("i=%d s=%d %.7g tol=%.7g",i,s,tol);
! 3319: #endif
! 3320: if (s < tol) {
! 3321: g = 0.0;
! 3322: }
! 3323: else {
! 3324: /* f = ab[i][i]; */
! 3325: f = ab[i][i];
! 3326: if (f < 0.0)
! 3327: g = sqrt(s);
! 3328: else
! 3329: g = -sqrt(s);
! 3330: /* h = f*g - s; ab[i][i] = f - g; */
! 3331: h = f*g - s; ab[i][i] = f - g;
! 3332: /* for (j=l; j<n; j++) { */ /* FOR J := L UNTIL N DO */ /* wrong */
! 3333: for (j=l; j<=n; j++) {
! 3334: f = 0.0;
! 3335: /* for (k=i; k<n; k++) /\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
! 3336: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
! 3337: /* f += ab[k][i] * ab[k][j]; */
! 3338: f += ab[k][i] * ab[k][j];
! 3339: f /= h;
! 3340: for (k=i; k<=n; k++) /* FOR K := I UNTIL N DO */
! 3341: /* for (k=i; k<n; k++)/\* FOR K := I UNTIL N DO *\/ /\* wrong *\/ */
! 3342: ab[k][j] += f * ab[k][i];
! 3343: /* ab[k][j] += f * ab[k][i]; */
! 3344: #ifdef DEBUGPRAX
! 3345: printf("Holder J=%d F=%.7g",j,f);
! 3346: #endif
! 3347: }
! 3348: } /* end s */
! 3349: /* q[i] = g; s = 0.0; */
! 3350: q[i] = g; s = 0.0;
! 3351: #ifdef DEBUGPRAX
! 3352: printf(" I Q=%d %.7g",i,q[i]);
! 3353: #endif
! 3354:
! 3355: /* if (i < n) */
! 3356: /* if (i <= n) /\* I is always lower or equal to n wasn't in golub reinsch*\/ */
! 3357: /* for (j=l; j<n; j++) */
! 3358: for (j=l; j<=n; j++)
! 3359: s += ab[i][j] * ab[i][j];
! 3360: /* s += ab[i][j] * ab[i][j]; */
! 3361: if (s < tol) {
! 3362: g = 0.0;
! 3363: }
! 3364: else {
! 3365: if(i<n)
! 3366: /* f = ab[i][i+1]; */ /* Brent golub overflow */
! 3367: f = ab[i][i+1];
! 3368: if (f < 0.0)
! 3369: g = sqrt(s);
! 3370: else
! 3371: g = - sqrt(s);
! 3372: h = f*g - s;
! 3373: /* h = f*g - s; ab[i][i+1] = f - g; */ /* Overflow for i=n Error in Golub too but not Burkardt*/
! 3374: /* for (j=l; j<n; j++) */
! 3375: /* e[j] = ab[i][j]/h; */
! 3376: if(i<n){
! 3377: ab[i][i+1] = f - g;
! 3378: for (j=l; j<=n; j++)
! 3379: e[j] = ab[i][j]/h;
! 3380: /* for (j=l; j<n; j++) { */
! 3381: for (j=l; j<=n; j++) {
! 3382: s = 0.0;
! 3383: /* for (k=l; k<n; k++) s += ab[j][k]*ab[i][k]; */
! 3384: for (k=l; k<=n; k++) s += ab[j][k]*ab[i][k];
! 3385: /* for (k=l; k<n; k++) ab[j][k] += s * e[k]; */
! 3386: for (k=l; k<=n; k++) ab[j][k] += s * e[k];
! 3387: } /* END J */
! 3388: } /* END i <n */
! 3389: } /* end s */
! 3390: /* y = fabs(q[i]) + fabs(e[i]); */
! 3391: y = fabs(q[i]) + fabs(e[i]);
! 3392: if (y > x) x = y;
! 3393: #ifdef DEBUGPRAX
! 3394: printf(" I Y=%d %.7g",i,y);
! 3395: #endif
! 3396: #ifdef DEBUGPRAX
! 3397: printf(" i=%d e(i) %.7g",i,e[i]);
! 3398: #endif
! 3399: } /* end i */
! 3400: /*
! 3401: Accumulation of right hand transformations */
! 3402: /* for (i=n-1; i >= 0; i--) { */ /* FOR I := N STEP -1 UNTIL 1 DO */
! 3403: /* We should avoid the overflow in Golub */
! 3404: /* ab[n-1][n-1] = 1.0; */
! 3405: /* g = e[n-1]; */
! 3406: ab[n][n] = 1.0;
! 3407: g = e[n];
! 3408: l = n;
! 3409:
! 3410: /* for (i=n; i >= 1; i--) { */
! 3411: for (i=n-1; i >= 1; i--) { /* n-1 loops, different from brent and golub*/
! 3412: if (g != 0.0) {
! 3413: /* h = ab[i-1][i]*g; */
! 3414: h = ab[i][i+1]*g;
! 3415: for (j=l; j<=n; j++) ab[j][i] = ab[i][j] / h;
! 3416: for (j=l; j<=n; j++) {
! 3417: /* h = ab[i][i+1]*g; */
! 3418: /* for (j=l; j<n; j++) ab[j][i] = ab[i][j] / h; */
! 3419: /* for (j=l; j<n; j++) { */
! 3420: s = 0.0;
! 3421: /* for (k=l; k<n; k++) s += ab[i][k] * ab[k][j]; */
! 3422: /* for (k=l; k<n; k++) ab[k][j] += s * ab[k][i]; */
! 3423: for (k=l; k<=n; k++) s += ab[i][k] * ab[k][j];
! 3424: for (k=l; k<=n; k++) ab[k][j] += s * ab[k][i];
! 3425: }/* END J */
! 3426: }/* END G */
! 3427: /* for (j=l; j<n; j++) */
! 3428: /* ab[i][j] = ab[j][i] = 0.0; */
! 3429: /* ab[i][i] = 1.0; g = e[i]; l = i; */
! 3430: for (j=l; j<=n; j++)
! 3431: ab[i][j] = ab[j][i] = 0.0;
! 3432: ab[i][i] = 1.0; g = e[i]; l = i;
! 3433: }/* END I */
! 3434: #ifdef DEBUGPRAX
! 3435: matprint (" HOUSE accumulation:",ab,n, n );
! 3436: #endif
! 3437:
! 3438: /* diagonalization to bidiagonal form */
! 3439: eps *= x;
! 3440: /* for (k=n-1; k>= 0; k--) { */
! 3441: for (k=n; k>= 1; k--) {
! 3442: kt = 0;
! 3443: TestFsplitting:
! 3444: #ifdef DEBUGPRAX
! 3445: printf(" TestFsplitting: k=%d kt=%d\n",k,kt);
! 3446: /* for(i=1;i<=n;i++)printf(" e(%d)=%.14f",i,e[i]);printf("\n"); */
! 3447: #endif
! 3448: kt = kt+1;
! 3449: /* TestFsplitting: */
! 3450: /* if (++kt > 30) { */
! 3451: if (kt > 30) {
! 3452: e[k] = 0.0;
! 3453: fprintf(stderr, "\n+++ MINFIT - Fatal error\n");
! 3454: fprintf ( stderr, " The QR algorithm failed to converge.\n" );
! 3455: }
! 3456: /* for (l2=k; l2>=0; l2--) { */
! 3457: for (l2=k; l2>=1; l2--) {
! 3458: l = l2;
! 3459: #ifdef DEBUGPRAX
! 3460: printf(" l e(l)< eps %d %.7g %.7g ",l,e[l], eps);
! 3461: #endif
! 3462: /* if (fabs(e[l]) <= eps) */
! 3463: if (fabs(e[l]) <= eps)
! 3464: goto TestFconvergence;
! 3465: /* if (fabs(q[l-1]) <= eps)*/ /* missing if ( 1 < l ){ *//* printf(" q(l-1)< eps %d %.7g %.7g ",l-1,q[l-2], eps); */
! 3466: if (fabs(q[l-1]) <= eps)
! 3467: break; /* goto Cancellation; */
! 3468: }
! 3469: Cancellation:
! 3470: #ifdef DEBUGPRAX
! 3471: printf(" Cancellation:\n");
! 3472: #endif
! 3473: c = 0.0; s = 1.0;
! 3474: for (i=l; i<=k; i++) {
! 3475: f = s * e[i]; e[i] *= c;
! 3476: /* f = s * e[i]; e[i] *= c; */
! 3477: if (fabs(f) <= eps)
! 3478: goto TestFconvergence;
! 3479: /* g = q[i]; */
! 3480: g = q[i];
! 3481: if (fabs(f) < fabs(g)) {
! 3482: double fg = f/g;
! 3483: h = fabs(g)*sqrt(1.0+fg*fg);
! 3484: }
! 3485: else {
! 3486: double gf = g/f;
! 3487: h = (f!=0.0 ? fabs(f)*sqrt(1.0+gf*gf) : 0.0);
! 3488: }
! 3489: /* COMMENT: THE ABOVE REPLACES Q(I):=H:=LONGSQRT(G*G+F*F) */
! 3490: /* WHICH MAY GIVE INCORRECT RESULTS IF THE */
! 3491: /* SQUARES UNDERFLOW OR IF F = G = 0; */
! 3492:
! 3493: /* q[i] = h; */
! 3494: q[i] = h;
! 3495: if (h == 0.0) { h = 1.0; g = 1.0; }
! 3496: c = g/h; s = -f/h;
! 3497: }
! 3498: TestFconvergence:
! 3499: #ifdef DEBUGPRAX
! 3500: printf(" TestFconvergence: l=%d k=%d\n",l,k);
! 3501: #endif
! 3502: /* z = q[k]; */
! 3503: z = q[k];
! 3504: if (l == k)
! 3505: goto Convergence;
! 3506: /* shift from bottom 2x2 minor */
! 3507: /* x = q[l]; y = q[k-l]; g = e[k-1]; h = e[k]; */ /* Error */
! 3508: x = q[l]; y = q[k-1]; g = e[k-1]; h = e[k];
! 3509: f = ((y-z)*(y+z) + (g-h)*(g+h)) / (2.0*h*y);
! 3510: g = sqrt(f*f+1.0);
! 3511: if (f <= 0.0)
! 3512: f = ((x-z)*(x+z) + h*(y/(f-g)-h))/x;
! 3513: else
! 3514: f = ((x-z)*(x+z) + h*(y/(f+g)-h))/x;
! 3515: /* next qr transformation */
! 3516: s = c = 1.0;
! 3517: for (i=l+1; i<=k; i++) {
! 3518: #ifdef DEBUGPRAXQR
! 3519: 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]);
! 3520: #endif
! 3521: /* g = e[i]; y = q[i]; h = s*g; g *= c; */
! 3522: g = e[i]; y = q[i]; h = s*g; g *= c;
! 3523: if (fabs(f) < fabs(h)) {
! 3524: double fh = f/h;
! 3525: z = fabs(h) * sqrt(1.0 + fh*fh);
! 3526: }
! 3527: else {
! 3528: double hf = h/f;
! 3529: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
! 3530: }
! 3531: /* e[i-1] = z; */
! 3532: e[i-1] = z;
! 3533: #ifdef DEBUGPRAXQR
! 3534: 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]);
! 3535: #endif
! 3536: if (z == 0.0)
! 3537: f = z = 1.0;
! 3538: c = f/z; s = h/z;
! 3539: f = x*c + g*s; g = - x*s + g*c; h = y*s;
! 3540: y *= c;
! 3541: /* for (j=0; j<n; j++) { */
! 3542: /* x = ab[j][i-1]; z = ab[j][i]; */
! 3543: /* ab[j][i-1] = x*c + z*s; */
! 3544: /* ab[j][i] = - x*s + z*c; */
! 3545: /* } */
! 3546: for (j=1; j<=n; j++) {
! 3547: x = ab[j][i-1]; z = ab[j][i];
! 3548: ab[j][i-1] = x*c + z*s;
! 3549: ab[j][i] = - x*s + z*c;
! 3550: }
! 3551: if (fabs(f) < fabs(h)) {
! 3552: double fh = f/h;
! 3553: z = fabs(h) * sqrt(1.0 + fh*fh);
! 3554: }
! 3555: else {
! 3556: double hf = h/f;
! 3557: z = (f!=0.0 ? fabs(f)*sqrt(1.0+hf*hf) : 0.0);
! 3558: }
! 3559: #ifdef DEBUGPRAXQR
! 3560: printf(" qr transformation z f h=%.7g %.7g %.7g i=%d k=%d\n",z,f,h, i, k);
! 3561: #endif
! 3562: q[i-1] = z;
! 3563: if (z == 0.0)
! 3564: z = f = 1.0;
! 3565: c = f/z; s = h/z;
! 3566: f = c*g + s*y; /* f can be very small */
! 3567: x = - s*g + c*y;
! 3568: }
! 3569: /* e[l] = 0.0; e[k] = f; q[k] = x; */
! 3570: e[l] = 0.0; e[k] = f; q[k] = x;
! 3571: #ifdef DEBUGPRAXQR
! 3572: 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);
! 3573: #endif
! 3574: goto TestFsplitting;
! 3575: Convergence:
! 3576: #ifdef DEBUGPRAX
! 3577: printf(" Convergence:\n");
! 3578: #endif
! 3579: if (z < 0.0) {
! 3580: /* q[k] = - z; */
! 3581: /* for (j=0; j<n; j++) ab[j][k] = - ab[j][k]; */
! 3582: q[k] = - z;
! 3583: for (j=1; j<=n; j++) ab[j][k] = - ab[j][k];
! 3584: }/* END Z */
! 3585: }/* END K */
! 3586: } /* END MINFIT */
! 3587:
! 3588:
! 3589: double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x))
! 3590: /* double praxis(double tol, double macheps, double h0, int _n, int _prin, double *_x, double (*_fun)(double *_x, int _n)) */
! 3591: /* double praxis(double (*_fun)(), double _x[], int _n) */
! 3592: /* double (*_fun)(); */
! 3593: /* double _x[N]; */
! 3594: /* double (*_fun)(); */
! 3595: /* double _x[N]; */
! 3596: {
! 3597: /* init global extern variables and parameters */
! 3598: /* double *d, *y, *z, */
! 3599: /* *q0, *q1, **v; */
! 3600: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
! 3601: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
! 3602:
! 3603:
! 3604: int seed; /* added */
! 3605: int biter=0;
! 3606: double r;
! 3607: double randbrent( int (*));
! 3608: double s, sf;
! 3609:
! 3610: h = h0; /* step; */
! 3611: t = tol;
! 3612: scbd = 1.0;
! 3613: illc = 0;
! 3614: ktm = 1;
! 3615:
! 3616: macheps = DBL_EPSILON;
! 3617: /* prin=4; */
! 3618: #ifdef DEBUGPRAX
! 3619: printf("Praxis macheps=%14g h=%14g step=%14g tol=%14g\n",macheps,h, h0,tol);
! 3620: #endif
! 3621: n = _n;
! 3622: x = _x;
! 3623: prin = _prin;
! 3624: fun = _fun;
! 3625: d=vector(1, n);
! 3626: y=vector(1, n);
! 3627: z=vector(1, n);
! 3628: q0=vector(1, n);
! 3629: q1=vector(1, n);
! 3630: e=vector(1, n);
! 3631: tflin=vector(1, n);
! 3632: v=matrix(1, n, 1, n);
! 3633: for(i=1;i<=n;i++){d[i]=y[i]=z[i]=q0[0]=e[i]=tflin[i]=0.;}
! 3634: small_windows = (macheps) * (macheps); vsmall = small_windows*small_windows;
! 3635: large = 1.0/small_windows; vlarge = 1.0/vsmall;
! 3636: m2 = sqrt(macheps); m4 = sqrt(m2);
! 3637: seed = 123456789; /* added */
! 3638: ldfac = (illc ? 0.1 : 0.01);
! 3639: for(i=1;i<=n;i++) z[i]=0.; /* Was missing in Gegenfurtner as well as Brent's algol or fortran */
! 3640: nl = kt = 0; nf = 1;
! 3641: #ifdef NR_SHIFT
! 3642: fx = (*fun)((x-1), n);
! 3643: #else
! 3644: fx = (*fun)(x);
! 3645: #endif
! 3646: qf1 = fx;
! 3647: t2 = small_windows + fabs(t); t = t2; dmin = small_windows;
! 3648: #ifdef DEBUGPRAX
! 3649: printf("praxis2 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
! 3650: #endif
! 3651: if (h < 100.0*t) h = 100.0*t;
! 3652: #ifdef DEBUGPRAX
! 3653: printf("praxis3 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
! 3654: #endif
! 3655: ldt = h;
! 3656: /* for (i=0; i<n; i++) for (j=0; j<n; j++) */
! 3657: for (i=1; i<=n; i++) for (j=1; j<=n; j++)
! 3658: v[i][j] = (i == j ? 1.0 : 0.0);
! 3659: d[1] = 0.0; qd0 = 0.0;
! 3660: /* for (i=0; i<n; i++) q1[i] = x[i]; */
! 3661: for (i=1; i<=n; i++) q1[i] = x[i];
! 3662: if (prin > 1) {
! 3663: printf("\n------------- enter function praxis -----------\n");
! 3664: printf("... current parameter settings ...\n");
! 3665: printf("... scaling ... %20.10e\n", scbd);
! 3666: printf("... tol ... %20.10e\n", t);
! 3667: printf("... maxstep ... %20.10e\n", h);
! 3668: printf("... illc ... %20u\n", illc);
! 3669: printf("... ktm ... %20u\n", ktm);
! 3670: printf("... maxfun ... %20u\n", maxfun);
! 3671: }
! 3672: if (prin) print2();
! 3673:
! 3674: mloop:
! 3675: biter++; /* Added to count the loops */
! 3676: /* sf = d[0]; */
! 3677: /* s = d[0] = 0.0; */
! 3678: printf("\n Big iteration %d \n",biter);
! 3679: fprintf(ficlog,"\n Big iteration %d \n",biter);
! 3680: sf = d[1];
! 3681: s = d[1] = 0.0;
! 3682:
! 3683: /* minimize along first direction V(*,1) */
! 3684: #ifdef DEBUGPRAX
! 3685: printf(" Minimize along the first direction V(*,1). illc=%d\n",illc);
! 3686: /* fprintf(ficlog," Minimize along the first direction V(*,1).\n"); */
! 3687: #endif
! 3688: #ifdef DEBUGPRAX2
! 3689: printf("praxis4 macheps=%14g h=%14g step=%14g small=%14g t=%14g\n",macheps,h, h0,small_windows, t);
! 3690: #endif
! 3691: /* min(0, 2, &d[0], &s, fx, 0); /\* mac heps not global *\/ */
! 3692: minny(1, 2, &d[1], &s, fx, 0); /* mac heps not global */
! 3693: #ifdef DEBUGPRAX
! 3694: printf("praxis5 macheps=%14g h=%14g looks at sign of s=%14g fx=%14g\n",macheps,h, s,fx);
! 3695: #endif
! 3696: if (s <= 0.0)
! 3697: /* for (i=0; i < n; i++) */
! 3698: for (i=1; i <= n; i++)
! 3699: v[i][1] = -v[i][1];
! 3700: /* if ((sf <= (0.9 * d[0])) || ((0.9 * sf) >= d[0])) */
! 3701: if ((sf <= (0.9 * d[1])) || ((0.9 * sf) >= d[1]))
! 3702: /* for (i=1; i<n; i++) */
! 3703: for (i=2; i<=n; i++)
! 3704: d[i] = 0.0;
! 3705: /* for (k=1; k<n; k++) { */
! 3706: for (k=2; k<=n; k++) {
! 3707: /*
! 3708: The inner loop starts here.
! 3709: */
! 3710: #ifdef DEBUGPRAX
! 3711: printf(" The inner loop here from k=%d to n=%d.\n",k,n);
! 3712: /* fprintf(ficlog," The inner loop here from k=%d to n=%d.\n",k,n); */
! 3713: #endif
! 3714: /* for (i=0; i<n; i++) */
! 3715: for (i=1; i<=n; i++)
! 3716: y[i] = x[i];
! 3717: sf = fx;
! 3718: #ifdef DEBUGPRAX
! 3719: printf(" illc=%d and kt=%d and ktm=%d\n", illc, kt, ktm);
! 3720: #endif
! 3721: illc = illc || (kt > 0);
! 3722: next:
! 3723: kl = k;
! 3724: df = 0.0;
! 3725: if (illc) { /* random step to get off resolution valley */
! 3726: #ifdef DEBUGPRAX
! 3727: printf(" A random step follows, to avoid resolution valleys.\n");
! 3728: matprint(" before rand, vectors:",v,n,n);
! 3729: #endif
! 3730: for (i=1; i<=n; i++) {
! 3731: #ifdef NOBRENTRAND
! 3732: r = drandom();
! 3733: #else
! 3734: seed=i;
! 3735: /* seed=i+1; */
! 3736: #ifdef DEBUGRAND
! 3737: printf(" Random seed=%d, brent i=%d",seed,i); /* YYYY i=5 j=1 vji= -0.0001170073 */
! 3738: #endif
! 3739: r = randbrent ( &seed );
! 3740: #endif
! 3741: #ifdef DEBUGRAND
! 3742: printf(" Random r=%.7g \n",r);
! 3743: #endif
! 3744: z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (r - 0.5);
! 3745: /* z[i] = (0.1 * ldt + t2 * pow(10.0,(double)kt)) * (drandom() - 0.5); */
! 3746:
! 3747: s = z[i];
! 3748: for (j=1; j <= n; j++)
! 3749: x[j] += s * v[j][i];
! 3750: }
! 3751: #ifdef DEBUGRAND
! 3752: matprint(" after rand, vectors:",v,n,n);
! 3753: #endif
! 3754: #ifdef NR_SHIFT
! 3755: fx = (*fun)((x-1), n);
! 3756: #else
! 3757: fx = (*fun)(x, n);
! 3758: #endif
! 3759: /* fx = (*func) ( (x-1) ); *//* This for func which is computed from x[1] and not from x[0] xm1=(x-1)*/
! 3760: nf++;
! 3761: }
! 3762: /* minimize along non-conjugate directions */
! 3763: #ifdef DEBUGPRAX
! 3764: printf(" Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n);
! 3765: /* fprintf(ficlog," Minimize along the 'non-conjugate' directions (dots printed) V(*,%d),...,V(*,%d).\n",k,n); */
! 3766: #endif
! 3767: /* for (k2=k; k2<n; k2++) { /\* Be careful here k2 <=n ? *\/ */
! 3768: for (k2=k; k2<=n; k2++) { /* Be careful here k2 <=n ? */
! 3769: sl = fx;
! 3770: s = 0.0;
! 3771: #ifdef DEBUGPRAX
! 3772: printf(" Minimize along the 'NON-CONJUGATE' true direction k2=%14d fx=%14.7f\n",k2, fx);
! 3773: matprint(" before min vectors:",v,n,n);
! 3774: #endif
! 3775: /* min(k2, 2, &d[k2], &s, fx, 0); */
! 3776: /* jsearch=k2-1; */
! 3777: /* min(jsearch, 2, &d[jsearch], &s, fx, 0); */
! 3778: minny(k2, 2, &d[k2], &s, fx, 0);
! 3779: #ifdef DEBUGPRAX
! 3780: 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);
! 3781: #endif
! 3782: if (illc) {
! 3783: /* double szk = s + z[k2]; */
! 3784: /* s = d[k2] * szk*szk; */
! 3785: double szk = s + z[k2];
! 3786: s = d[k2] * szk*szk;
! 3787: }
! 3788: else
! 3789: s = sl - fx;
! 3790: /* if (df < s) { */
! 3791: if (df <= s) {
! 3792: df = s;
! 3793: kl = k2;
! 3794: #ifdef DEBUGPRAX
! 3795: printf(" df=%.7g and choose kl=%d \n",df,kl); /* UUUU */
! 3796: #endif
! 3797: }
! 3798: } /* end loop k2 */
! 3799: /*
! 3800: If there was not much improvement on the first try, set
! 3801: ILLC = true and start the inner loop again.
! 3802: */
! 3803: #ifdef DEBUGPRAX
! 3804: printf(" If there was not much improvement on the first try, set ILLC = true and start the inner loop again. illc=%d\n",illc);
! 3805: /* fprintf(ficlog," If there was not much improvement on the first try, set ILLC = true and start the inner loop again.\n"); */
! 3806: #endif
! 3807: if (!illc && (df < fabs(100.0 * (macheps) * fx))) {
! 3808: #ifdef DEBUGPRAX
! 3809: 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);
! 3810: #endif
! 3811: illc = 1;
! 3812: goto next;
! 3813: }
! 3814: #ifdef DEBUGPRAX
! 3815: 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);
! 3816: #endif
! 3817:
! 3818: /* if ((k == 1) && (prin > 1)){ /\* be careful k=2 *\/ */
! 3819: if ((k == 2) && (prin > 1)){ /* be careful k=2 */
! 3820: #ifdef DEBUGPRAX
! 3821: printf(" NEW D The second difference array d:\n" );
! 3822: /* fprintf(ficlog, " NEW D The second difference array d:\n" ); */
! 3823: #endif
! 3824: vecprint(" NEW D The second difference array d:",d,n);
! 3825: }
! 3826: /* minimize along conjugate directions */
! 3827: /*
! 3828: Minimize along the "conjugate" directions V(*,1),...,V(*,K-1).
! 3829: */
! 3830: #ifdef DEBUGPRAX
! 3831: printf("Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1);
! 3832: /* fprintf(ficlog,"Minimize along the 'conjugate' directions V(*,1),...,V(*,K-1=%d).\n",k-1); */
! 3833: #endif
! 3834: /* for (k2=0; k2<=k-1; k2++) { */
! 3835: for (k2=1; k2<=k-1; k2++) {
! 3836: s = 0.0;
! 3837: /* min(k2-1, 2, &d[k2-1], &s, fx, 0); */
! 3838: minny(k2, 2, &d[k2], &s, fx, 0);
! 3839: }
! 3840: f1 = fx;
! 3841: fx = sf;
! 3842: lds = 0.0;
! 3843: /* for (i=0; i<n; i++) { */
! 3844: for (i=1; i<=n; i++) {
! 3845: sl = x[i];
! 3846: x[i] = y[i];
! 3847: y[i] = sl - y[i];
! 3848: sl = y[i];
! 3849: lds = lds + sl*sl;
! 3850: }
! 3851: lds = sqrt(lds);
! 3852: #ifdef DEBUGPRAX
! 3853: printf("Minimization done 'conjugate', shifted all points, computed lds=%.8f\n",lds);
! 3854: #endif
! 3855: /*
! 3856: Discard direction V(*,kl).
! 3857:
! 3858: If no random step was taken, V(*,KL) is the "non-conjugate"
! 3859: direction along which the greatest improvement was made.
! 3860: */
! 3861: if (lds > small_windows) {
! 3862: #ifdef DEBUGPRAX
! 3863: 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);
! 3864: matprint(" before shift new conjugate vectors:",v,n,n);
! 3865: #endif
! 3866: for (i=kl-1; i>=k; i--) {
! 3867: /* for (j=0; j < n; j++) */
! 3868: for (j=1; j <= n; j++)
! 3869: /* v[j][i+1] = v[j][i]; */ /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
! 3870: v[j][i+1] = v[j][i]; /* This is v[j][i+1]=v[j][i] i=kl-1 to k */
! 3871: /* v[j][i+1] = v[j][i]; */
! 3872: /* d[i+1] = d[i];*/ /* last is d[k+1]= d[k] */
! 3873: d[i+1] = d[i]; /* last is d[k]= d[k-1] */
! 3874: }
! 3875: #ifdef DEBUGPRAX
! 3876: matprint(" after shift new conjugate vectors:",v,n,n);
! 3877: #endif /* d[k] = 0.0; */
! 3878: d[k] = 0.0;
! 3879: for (i=1; i <= n; i++)
! 3880: v[i][k] = y[i] / lds;
! 3881: /* v[i][k] = y[i] / lds; */
! 3882: #ifdef DEBUGPRAX
! 3883: 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);
! 3884: /* fprintf(ficlog,"Minimize along the new 'conjugate' direction V(*,k=%d), which is the normalized vector: (new x) - (old x).\n",k); */
! 3885: matprint(" before min new conjugate vectors:",v,n,n);
! 3886: #endif
! 3887: /* min(k-1, 4, &d[k-1], &lds, f1, 1); */
! 3888: minny(k, 4, &d[k], &lds, f1, 1);
! 3889: #ifdef DEBUGPRAX
! 3890: printf(" after min d(k)=%d %.7g lds=%14f\n",k,d[k],lds);
! 3891: matprint(" after min vectors:",v,n,n);
! 3892: #endif
! 3893: if (lds <= 0.0) {
! 3894: lds = -lds;
! 3895: #ifdef DEBUGPRAX
! 3896: printf(" lds changed sign lds=%.14f k=%d\n",lds,k);
! 3897: #endif
! 3898: /* for (i=0; i<n; i++) */
! 3899: /* v[i][k] = -v[i][k]; */
! 3900: for (i=1; i<=n; i++)
! 3901: v[i][k] = -v[i][k];
! 3902: }
! 3903: }
! 3904: ldt = ldfac * ldt;
! 3905: if (ldt < lds)
! 3906: ldt = lds;
! 3907: if (prin > 0){
! 3908: #ifdef DEBUGPRAX
! 3909: printf(" k=%d",k);
! 3910: /* fprintf(ficlog," k=%d",k); */
! 3911: #endif
! 3912: print2();/* n, x, prin, fx, nf, nl ); */
! 3913: }
! 3914: t2 = 0.0;
! 3915: /* for (i=0; i<n; i++) */
! 3916: for (i=1; i<=n; i++)
! 3917: t2 += x[i]*x[i];
! 3918: t2 = m2 * sqrt(t2) + t;
! 3919: /*
! 3920: See whether the length of the step taken since starting the
! 3921: inner loop exceeds half the tolerance.
! 3922: */
! 3923: #ifdef DEBUGPRAX
! 3924: printf("See if step length exceeds half the tolerance.\n"); /* ZZZZZ */
! 3925: /* fprintf(ficlog,"See if step length exceeds half the tolerance.\n"); */
! 3926: #endif
! 3927: if (ldt > (0.5 * t2))
! 3928: kt = 0;
! 3929: else
! 3930: kt++;
! 3931: #ifdef DEBUGPRAX
! 3932: printf("if kt=%d >? ktm=%d gotoL2 loop\n",kt,ktm);
! 3933: #endif
! 3934: if (kt > ktm){
! 3935: if ( 0 < prin ){
! 3936: /* printf("\nr8vec_print\n X:\n"); */
! 3937: /* fprintf(ficlog,"\nr8vec_print\n X:\n"); */
! 3938: vecprint ("END X:", x, n );
! 3939: }
! 3940: goto fret;
! 3941: }
! 3942: #ifdef DEBUGPRAX
! 3943: matprint(" end of L2 loop vectors:",v,n,n);
! 3944: #endif
! 3945:
! 3946: }
! 3947: /* printf("The inner loop ends here.\n"); */
! 3948: /* fprintf(ficlog,"The inner loop ends here.\n"); */
! 3949: /*
! 3950: The inner loop ends here.
! 3951:
! 3952: Try quadratic extrapolation in case we are in a curved valley.
! 3953: */
! 3954: #ifdef DEBUGPRAX
! 3955: printf("Try QUAD ratic extrapolation in case we are in a curved valley.\n");
! 3956: #endif
! 3957: /* try quadratic extrapolation in case */
! 3958: /* we are stuck in a curved valley */
! 3959: quad();
! 3960: dn = 0.0;
! 3961: /* for (i=0; i<n; i++) { */
! 3962: for (i=1; i<=n; i++) {
! 3963: d[i] = 1.0 / sqrt(d[i]);
! 3964: if (dn < d[i])
! 3965: dn = d[i];
! 3966: }
! 3967: if (prin > 2)
! 3968: matprint(" NEW DIRECTIONS vectors:",v,n,n);
! 3969: /* for (j=0; j<n; j++) { */
! 3970: for (j=1; j<=n; j++) {
! 3971: s = d[j] / dn;
! 3972: /* for (i=0; i < n; i++) */
! 3973: for (i=1; i <= n; i++)
! 3974: v[i][j] *= s;
! 3975: }
! 3976:
! 3977: if (scbd > 1.0) { /* scale axis to reduce condition number */
! 3978: #ifdef DEBUGPRAX
! 3979: printf("Scale the axes to try to reduce the condition number.\n");
! 3980: #endif
! 3981: /* fprintf(ficlog,"Scale the axes to try to reduce the condition number.\n"); */
! 3982: s = vlarge;
! 3983: /* for (i=0; i<n; i++) { */
! 3984: for (i=1; i<=n; i++) {
! 3985: sl = 0.0;
! 3986: /* for (j=0; j < n; j++) */
! 3987: for (j=1; j <= n; j++)
! 3988: sl += v[i][j]*v[i][j];
! 3989: z[i] = sqrt(sl);
! 3990: if (z[i] < m4)
! 3991: z[i] = m4;
! 3992: if (s > z[i])
! 3993: s = z[i];
! 3994: }
! 3995: /* for (i=0; i<n; i++) { */
! 3996: for (i=1; i<=n; i++) {
! 3997: sl = s / z[i];
! 3998: z[i] = 1.0 / sl;
! 3999: if (z[i] > scbd) {
! 4000: sl = 1.0 / scbd;
! 4001: z[i] = scbd;
! 4002: }
! 4003: }
! 4004: }
! 4005: for (i=1; i<=n; i++)
! 4006: /* for (j=0; j<=i-1; j++) { */
! 4007: /* for (j=1; j<=i; j++) { */
! 4008: for (j=1; j<=i-1; j++) {
! 4009: s = v[i][j];
! 4010: v[i][j] = v[j][i];
! 4011: v[j][i] = s;
! 4012: }
! 4013: #ifdef DEBUGPRAX
! 4014: printf(" Calculate a new set of orthogonal directions before repeating the main loop.\n Transpose V for MINFIT:...\n");
! 4015: #endif
! 4016: /*
! 4017: MINFIT finds the singular value decomposition of V.
! 4018:
! 4019: This gives the principal values and principal directions of the
! 4020: approximating quadratic form without squaring the condition number.
! 4021: */
! 4022: #ifdef DEBUGPRAX
! 4023: 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");
! 4024: #endif
! 4025:
! 4026: minfit(n, macheps, vsmall, v, d);
! 4027: /* for(i=0; i<n;i++)printf(" %14.7g",d[i]); */
! 4028: /* v is overwritten with R. */
! 4029: /*
! 4030: Unscale the axes.
! 4031: */
! 4032: if (scbd > 1.0) {
! 4033: #ifdef DEBUGPRAX
! 4034: printf(" Unscale the axes.\n");
! 4035: #endif
! 4036: /* for (i=0; i<n; i++) { */
! 4037: for (i=1; i<=n; i++) {
! 4038: s = z[i];
! 4039: /* for (j=0; j<n; j++) */
! 4040: for (j=1; j<=n; j++)
! 4041: v[i][j] *= s;
! 4042: }
! 4043: /* for (i=0; i<n; i++) { */
! 4044: for (i=1; i<=n; i++) {
! 4045: s = 0.0;
! 4046: /* for (j=0; j<n; j++) */
! 4047: for (j=1; j<=n; j++)
! 4048: s += v[j][i]*v[j][i];
! 4049: s = sqrt(s);
! 4050: d[i] *= s;
! 4051: s = 1.0 / s;
! 4052: /* for (j=0; j<n; j++) */
! 4053: for (j=1; j<=n; j++)
! 4054: v[j][i] *= s;
! 4055: }
! 4056: }
! 4057: /* for (i=0; i<n; i++) { */
! 4058: double dni; /* added for compatibility with buckhardt but not brent */
! 4059: for (i=1; i<=n; i++) {
! 4060: dni=dn*d[i]; /* added for compatibility with buckhardt but not brent */
! 4061: if ((dn * d[i]) > large)
! 4062: d[i] = vsmall;
! 4063: else if ((dn * d[i]) < small_windows)
! 4064: d[i] = vlarge;
! 4065: else
! 4066: d[i] = 1.0 / dni / dni; /* added for compatibility with buckhardt but not brent */
! 4067: /* d[i] = pow(dn * d[i],-2.0); */
! 4068: }
! 4069: #ifdef DEBUGPRAX
! 4070: vecprint ("\n Before sort Eigenvalues of a:",d,n );
! 4071: #endif
! 4072:
! 4073: sort(); /* the new eigenvalues and eigenvectors */
! 4074: #ifdef DEBUGPRAX
! 4075: vecprint( " After sort the eigenvalues ....\n", d, n);
! 4076: matprint( " After sort the eigenvectors....\n", v, n,n);
! 4077: #endif
! 4078: #ifdef DEBUGPRAX
! 4079: printf(" Determine the smallest eigenvalue.\n");
! 4080: #endif
! 4081: /* dmin = d[n-1]; */
! 4082: dmin = d[n];
! 4083: if (dmin < small_windows)
! 4084: dmin = small_windows;
! 4085: /*
! 4086: The ratio of the smallest to largest eigenvalue determines whether
! 4087: the system is ill conditioned.
! 4088: */
! 4089:
! 4090: /* illc = (m2 * d[0]) > dmin; */
! 4091: illc = (m2 * d[1]) > dmin;
! 4092: #ifdef DEBUGPRAX
! 4093: 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]);
! 4094: #endif
! 4095:
! 4096: if ((prin > 2) && (scbd > 1.0))
! 4097: vecprint("\n The scale factors:",z,n);
! 4098: if (prin > 2)
! 4099: vecprint(" Principal values (EIGEN VALUES OF A) of the quadratic form:",d,n);
! 4100: if (prin > 2)
! 4101: matprint(" The principal axes (EIGEN VECTORS OF A:",v,n, n);
! 4102:
! 4103: if ((maxfun > 0) && (nf > maxfun)) {
! 4104: if (prin)
! 4105: printf("\n... maximum number of function calls reached ...\n");
! 4106: goto fret;
! 4107: }
! 4108: #ifdef DEBUGPRAX
! 4109: printf("Goto main loop\n");
! 4110: #endif
! 4111: goto mloop; /* back to main loop */
! 4112:
! 4113: fret:
! 4114: if (prin > 0) {
! 4115: vecprint("\n X:", x, n);
! 4116: /* printf("\n... ChiSq reduced to %20.10e ...\n", fx); */
! 4117: /* printf("... after %20u function calls.\n", nf); */
! 4118: }
! 4119: free_vector(d, 1, n);
! 4120: free_vector(y, 1, n);
! 4121: free_vector(z, 1, n);
! 4122: free_vector(q0, 1, n);
! 4123: free_vector(q1, 1, n);
! 4124: free_matrix(v, 1, n, 1, n);
! 4125: /* double *d, *y, *z, */
! 4126: /* *q0, *q1, **v; */
! 4127: free_vector(tflin, 1, n);
! 4128: /* double *tflin; /\* used in flin: return (*fun)(tflin, n); *\/ */
! 4129: free_vector(e, 1, n);
! 4130: /* double *e; /\* used in minfit, don't konw how to free memory and thus made global *\/ */
! 4131:
! 4132: return(fx);
! 4133: }
! 4134:
! 4135: /* end praxis gegen */
1.126 brouard 4136:
4137: /*************** powell ************************/
1.162 brouard 4138: /*
1.317 brouard 4139: Minimization of a function func of n variables. Input consists in an initial starting point
4140: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
4141: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
4142: such that failure to decrease by more than this amount in one iteration signals doneness. On
1.162 brouard 4143: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
4144: function value at p , and iter is the number of iterations taken. The routine linmin is used.
4145: */
1.224 brouard 4146: #ifdef LINMINORIGINAL
4147: #else
4148: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 4149: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 4150: #endif
1.126 brouard 4151: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
4152: double (*func)(double []))
4153: {
1.224 brouard 4154: #ifdef LINMINORIGINAL
4155: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 4156: double (*func)(double []));
1.224 brouard 4157: #else
1.241 brouard 4158: void linmin(double p[], double xi[], int n, double *fret,
4159: double (*func)(double []),int *flat);
1.224 brouard 4160: #endif
1.239 brouard 4161: int i,ibig,j,jk,k;
1.126 brouard 4162: double del,t,*pt,*ptt,*xit;
1.181 brouard 4163: double directest;
1.126 brouard 4164: double fp,fptt;
4165: double *xits;
4166: int niterf, itmp;
1.349 brouard 4167: int Bigter=0, nBigterf=1;
4168:
1.126 brouard 4169: pt=vector(1,n);
4170: ptt=vector(1,n);
4171: xit=vector(1,n);
4172: xits=vector(1,n);
4173: *fret=(*func)(p);
4174: for (j=1;j<=n;j++) pt[j]=p[j];
1.338 brouard 4175: rcurr_time = time(NULL);
4176: fp=(*fret); /* Initialisation */
1.126 brouard 4177: for (*iter=1;;++(*iter)) {
4178: ibig=0;
4179: del=0.0;
1.157 brouard 4180: rlast_time=rcurr_time;
1.349 brouard 4181: rlast_btime=rcurr_time;
1.157 brouard 4182: /* (void) gettimeofday(&curr_time,&tzp); */
4183: rcurr_time = time(NULL);
4184: curr_time = *localtime(&rcurr_time);
1.337 brouard 4185: /* 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); */
4186: /* 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 4187: /* Bigter=(*iter - *iter % ncovmodel)/ncovmodel +1; /\* Big iteration, i.e on ncovmodel cycle *\/ */
! 4188: Bigter=(*iter - (*iter-1) % n)/n +1; /* Big iteration, i.e on ncovmodel cycle */
1.349 brouard 4189: 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);
4190: 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);
4191: fprintf(ficrespow,"%d %d %.12f %d",*iter,Bigter, *fret,curr_time.tm_sec-start_time.tm_sec);
1.324 brouard 4192: fp=(*fret); /* From former iteration or initial value */
1.192 brouard 4193: for (i=1;i<=n;i++) {
1.126 brouard 4194: fprintf(ficrespow," %.12lf", p[i]);
4195: }
1.239 brouard 4196: fprintf(ficrespow,"\n");fflush(ficrespow);
4197: printf("\n#model= 1 + age ");
4198: fprintf(ficlog,"\n#model= 1 + age ");
4199: if(nagesqr==1){
1.241 brouard 4200: printf(" + age*age ");
4201: fprintf(ficlog," + age*age ");
1.239 brouard 4202: }
4203: for(j=1;j <=ncovmodel-2;j++){
4204: if(Typevar[j]==0) {
4205: printf(" + V%d ",Tvar[j]);
4206: fprintf(ficlog," + V%d ",Tvar[j]);
4207: }else if(Typevar[j]==1) {
4208: printf(" + V%d*age ",Tvar[j]);
4209: fprintf(ficlog," + V%d*age ",Tvar[j]);
4210: }else if(Typevar[j]==2) {
4211: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4212: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 4213: }else if(Typevar[j]==3) {
4214: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
4215: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.239 brouard 4216: }
4217: }
1.126 brouard 4218: printf("\n");
1.239 brouard 4219: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
4220: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 4221: fprintf(ficlog,"\n");
1.239 brouard 4222: for(i=1,jk=1; i <=nlstate; i++){
4223: for(k=1; k <=(nlstate+ndeath); k++){
4224: if (k != i) {
4225: printf("%d%d ",i,k);
4226: fprintf(ficlog,"%d%d ",i,k);
4227: for(j=1; j <=ncovmodel; j++){
4228: printf("%12.7f ",p[jk]);
4229: fprintf(ficlog,"%12.7f ",p[jk]);
4230: jk++;
4231: }
4232: printf("\n");
4233: fprintf(ficlog,"\n");
4234: }
4235: }
4236: }
1.241 brouard 4237: if(*iter <=3 && *iter >1){
1.157 brouard 4238: tml = *localtime(&rcurr_time);
4239: strcpy(strcurr,asctime(&tml));
4240: rforecast_time=rcurr_time;
1.126 brouard 4241: itmp = strlen(strcurr);
4242: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 4243: strcurr[itmp-1]='\0';
1.162 brouard 4244: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 4245: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.349 brouard 4246: for(nBigterf=1;nBigterf<=31;nBigterf+=10){
4247: niterf=nBigterf*ncovmodel;
4248: /* rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time); */
1.241 brouard 4249: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
4250: forecast_time = *localtime(&rforecast_time);
4251: strcpy(strfor,asctime(&forecast_time));
4252: itmp = strlen(strfor);
4253: if(strfor[itmp-1]=='\n')
4254: strfor[itmp-1]='\0';
1.349 brouard 4255: 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);
4256: 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 4257: }
4258: }
1.359 ! brouard 4259: for (i=1;i<=n;i++) { /* For each direction i, maximisation after loading directions */
! 4260: 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 */
! 4261:
! 4262: fptt=(*fret); /* Computes likelihood for parameters xit */
1.126 brouard 4263: #ifdef DEBUG
1.203 brouard 4264: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
4265: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 4266: #endif
1.203 brouard 4267: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 4268: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 4269: #ifdef LINMINORIGINAL
1.359 ! brouard 4270: linmin(p,xit,n,fret,func); /* New point i minimizing in direction xit, i has coordinates p[j].*/
1.357 brouard 4271: /* xit[j] gives the n coordinates of direction i as input.*/
4272: /* *fret gives the maximum value on direction xit */
1.224 brouard 4273: #else
4274: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.359 ! brouard 4275: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.224 brouard 4276: #endif
1.359 ! brouard 4277: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 4278: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.359 ! brouard 4279: /* because that direction will be replaced unless the gain del is small */
! 4280: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
! 4281: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
! 4282: /* with the new direction. */
! 4283: del=fabs(fptt-(*fret));
! 4284: ibig=i;
1.126 brouard 4285: }
4286: #ifdef DEBUG
4287: printf("%d %.12e",i,(*fret));
4288: fprintf(ficlog,"%d %.12e",i,(*fret));
4289: for (j=1;j<=n;j++) {
1.359 ! brouard 4290: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
! 4291: printf(" x(%d)=%.12e",j,xit[j]);
! 4292: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 4293: }
4294: for(j=1;j<=n;j++) {
1.359 ! brouard 4295: printf(" p(%d)=%.12e",j,p[j]);
! 4296: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 4297: }
4298: printf("\n");
4299: fprintf(ficlog,"\n");
4300: #endif
1.187 brouard 4301: } /* end loop on each direction i */
1.357 brouard 4302: /* Convergence test will use last linmin estimation (fret) and compare to former iteration (fp) */
1.188 brouard 4303: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.359 ! brouard 4304: /* New value of last point Pn is not computed, P(n-1) */
1.319 brouard 4305: for(j=1;j<=n;j++) {
4306: if(flatdir[j] >0){
4307: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
4308: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
1.302 brouard 4309: }
1.319 brouard 4310: /* printf("\n"); */
4311: /* fprintf(ficlog,"\n"); */
4312: }
1.243 brouard 4313: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
4314: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 4315: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
4316: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
4317: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
4318: /* decreased of more than 3.84 */
4319: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
4320: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
4321: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 4322:
1.188 brouard 4323: /* Starting the program with initial values given by a former maximization will simply change */
4324: /* the scales of the directions and the directions, because the are reset to canonical directions */
4325: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
4326: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 4327: #ifdef DEBUG
4328: int k[2],l;
4329: k[0]=1;
4330: k[1]=-1;
4331: printf("Max: %.12e",(*func)(p));
4332: fprintf(ficlog,"Max: %.12e",(*func)(p));
4333: for (j=1;j<=n;j++) {
4334: printf(" %.12e",p[j]);
4335: fprintf(ficlog," %.12e",p[j]);
4336: }
4337: printf("\n");
4338: fprintf(ficlog,"\n");
4339: for(l=0;l<=1;l++) {
4340: for (j=1;j<=n;j++) {
4341: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
4342: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4343: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
4344: }
4345: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4346: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
4347: }
4348: #endif
4349:
4350: free_vector(xit,1,n);
4351: free_vector(xits,1,n);
4352: free_vector(ptt,1,n);
4353: free_vector(pt,1,n);
4354: return;
1.192 brouard 4355: } /* enough precision */
1.240 brouard 4356: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.359 ! brouard 4357: 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 4358: ptt[j]=2.0*p[j]-pt[j];
1.359 ! brouard 4359: xit[j]=p[j]-pt[j]; /* Coordinate j of last direction xi_n=P_n-P_0 */
! 4360: #ifdef DEBUG
! 4361: printf("\n %d xit=%12.7g p=%12.7g pt=%12.7g ",j,xit[j],p[j],pt[j]);
! 4362: #endif
! 4363: pt[j]=p[j]; /* New P0 is Pn */
! 4364: }
! 4365: #ifdef DEBUG
! 4366: printf("\n");
! 4367: #endif
1.181 brouard 4368: fptt=(*func)(ptt); /* f_3 */
1.359 ! brouard 4369: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in directions until some iterations are done */
1.224 brouard 4370: if (*iter <=4) {
1.225 brouard 4371: #else
4372: #endif
1.224 brouard 4373: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 4374: #else
1.161 brouard 4375: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 4376: #endif
1.162 brouard 4377: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 4378: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 4379: /* Let f"(x2) be the 2nd derivative equal everywhere. */
4380: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
4381: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 4382: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
4383: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
4384: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 4385: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 4386: /* Even if f3 <f1, directest can be negative and t >0 */
4387: /* mu² and del² are equal when f3=f1 */
1.359 ! brouard 4388: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
! 4389: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
! 4390: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
! 4391: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 4392: #ifdef NRCORIGINAL
4393: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
4394: #else
4395: 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 4396: t= t- del*SQR(fp-fptt);
1.183 brouard 4397: #endif
1.202 brouard 4398: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 4399: #ifdef DEBUG
1.181 brouard 4400: 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);
4401: 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 4402: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4403: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4404: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
4405: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
4406: 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);
4407: 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);
4408: #endif
1.183 brouard 4409: #ifdef POWELLORIGINAL
4410: if (t < 0.0) { /* Then we use it for new direction */
4411: #else
1.182 brouard 4412: if (directest*t < 0.0) { /* Contradiction between both tests */
1.359 ! brouard 4413: 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 4414: 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 4415: 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 4416: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
4417: }
1.181 brouard 4418: if (directest < 0.0) { /* Then we use it for new direction */
4419: #endif
1.191 brouard 4420: #ifdef DEBUGLINMIN
1.234 brouard 4421: printf("Before linmin in direction P%d-P0\n",n);
4422: for (j=1;j<=n;j++) {
4423: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4424: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4425: if(j % ncovmodel == 0){
4426: printf("\n");
4427: fprintf(ficlog,"\n");
4428: }
4429: }
1.224 brouard 4430: #endif
4431: #ifdef LINMINORIGINAL
1.234 brouard 4432: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 4433: #else
1.234 brouard 4434: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
4435: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 4436: #endif
1.234 brouard 4437:
1.191 brouard 4438: #ifdef DEBUGLINMIN
1.234 brouard 4439: for (j=1;j<=n;j++) {
4440: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4441: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
4442: if(j % ncovmodel == 0){
4443: printf("\n");
4444: fprintf(ficlog,"\n");
4445: }
4446: }
1.224 brouard 4447: #endif
1.234 brouard 4448: for (j=1;j<=n;j++) {
4449: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
4450: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
4451: }
1.224 brouard 4452: #ifdef LINMINORIGINAL
4453: #else
1.234 brouard 4454: for (j=1, flatd=0;j<=n;j++) {
4455: if(flatdir[j]>0)
4456: flatd++;
4457: }
4458: if(flatd >0){
1.255 brouard 4459: printf("%d flat directions: ",flatd);
4460: fprintf(ficlog,"%d flat directions :",flatd);
1.234 brouard 4461: for (j=1;j<=n;j++) {
4462: if(flatdir[j]>0){
4463: printf("%d ",j);
4464: fprintf(ficlog,"%d ",j);
4465: }
4466: }
4467: printf("\n");
4468: fprintf(ficlog,"\n");
1.319 brouard 4469: #ifdef FLATSUP
4470: free_vector(xit,1,n);
4471: free_vector(xits,1,n);
4472: free_vector(ptt,1,n);
4473: free_vector(pt,1,n);
4474: return;
4475: #endif
1.234 brouard 4476: }
1.191 brouard 4477: #endif
1.234 brouard 4478: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4479: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
4480:
1.126 brouard 4481: #ifdef DEBUG
1.234 brouard 4482: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4483: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
4484: for(j=1;j<=n;j++){
4485: printf(" %lf",xit[j]);
4486: fprintf(ficlog," %lf",xit[j]);
4487: }
4488: printf("\n");
4489: fprintf(ficlog,"\n");
1.126 brouard 4490: #endif
1.192 brouard 4491: } /* end of t or directest negative */
1.359 ! brouard 4492: printf(" Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
! 4493: fprintf(ficlog," Directest is positive, P_n-P_0 does not increase the conjugacy. n=%d\n",n);
1.224 brouard 4494: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 4495: #else
1.234 brouard 4496: } /* end if (fptt < fp) */
1.192 brouard 4497: #endif
1.225 brouard 4498: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 4499: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 4500: #else
1.224 brouard 4501: #endif
1.234 brouard 4502: } /* loop iteration */
1.126 brouard 4503: }
1.234 brouard 4504:
1.126 brouard 4505: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 4506:
1.235 brouard 4507: 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 4508: {
1.338 brouard 4509: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij . Nicely done
1.279 brouard 4510: * (and selected quantitative values in nres)
4511: * by left multiplying the unit
4512: * matrix by transitions matrix until convergence is reached with precision ftolpl
4513: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
4514: * Wx is row vector: population in state 1, population in state 2, population dead
4515: * or prevalence in state 1, prevalence in state 2, 0
4516: * newm is the matrix after multiplications, its rows are identical at a factor.
4517: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
4518: * Output is prlim.
4519: * Initial matrix pimij
4520: */
1.206 brouard 4521: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4522: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4523: /* 0, 0 , 1} */
4524: /*
4525: * and after some iteration: */
4526: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4527: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4528: /* 0, 0 , 1} */
4529: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4530: /* {0.51571254859325999, 0.4842874514067399, */
4531: /* 0.51326036147820708, 0.48673963852179264} */
4532: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 4533:
1.332 brouard 4534: int i, ii,j,k, k1;
1.209 brouard 4535: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 4536: /* double **matprod2(); */ /* test */
1.218 brouard 4537: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 4538: double **newm;
1.209 brouard 4539: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 4540: int ncvloop=0;
1.288 brouard 4541: int first=0;
1.169 brouard 4542:
1.209 brouard 4543: min=vector(1,nlstate);
4544: max=vector(1,nlstate);
4545: meandiff=vector(1,nlstate);
4546:
1.218 brouard 4547: /* Starting with matrix unity */
1.126 brouard 4548: for (ii=1;ii<=nlstate+ndeath;ii++)
4549: for (j=1;j<=nlstate+ndeath;j++){
4550: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4551: }
1.169 brouard 4552:
4553: cov[1]=1.;
4554:
4555: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 4556: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 4557: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 4558: ncvloop++;
1.126 brouard 4559: newm=savm;
4560: /* Covariates have to be included here again */
1.138 brouard 4561: cov[2]=agefin;
1.319 brouard 4562: if(nagesqr==1){
4563: cov[3]= agefin*agefin;
4564: }
1.332 brouard 4565: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
4566: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
4567: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 4568: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 4569: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
4570: }else{
4571: cov[2+nagesqr+k1]=precov[nres][k1];
4572: }
4573: }/* End of loop on model equation */
4574:
4575: /* Start of old code (replaced by a loop on position in the model equation */
4576: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only of the model *\/ */
4577: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4578: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; *\/ */
4579: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TnsdVar[TvarsD[k]])]; */
4580: /* /\* model = 1 +age + V1*V3 + age*V1 + V2 + V1 + age*V2 + V3 + V3*age + V1*V2 */
4581: /* * k 1 2 3 4 5 6 7 8 */
4582: /* *cov[] 1 2 3 4 5 6 7 8 9 10 */
4583: /* *TypeVar[k] 2 1 0 0 1 0 1 2 */
4584: /* *Dummy[k] 0 2 0 0 2 0 2 0 */
4585: /* *Tvar[k] 4 1 2 1 2 3 3 5 */
4586: /* *nsd=3 (1) (2) (3) */
4587: /* *TvarsD[nsd] [1]=2 1 3 */
4588: /* *TnsdVar [2]=2 [1]=1 [3]=3 */
4589: /* *TvarsDind[nsd](=k) [1]=3 [2]=4 [3]=6 */
4590: /* *Tage[] [1]=1 [2]=2 [3]=3 */
4591: /* *Tvard[] [1][1]=1 [2][1]=1 */
4592: /* * [1][2]=3 [2][2]=2 */
4593: /* *Tprod[](=k) [1]=1 [2]=8 */
4594: /* *TvarsDp(=Tvar) [1]=1 [2]=2 [3]=3 [4]=5 */
4595: /* *TvarD (=k) [1]=1 [2]=3 [3]=4 [3]=6 [4]=6 */
4596: /* *TvarsDpType */
4597: /* *si model= 1 + age + V3 + V2*age + V2 + V3*age */
4598: /* * nsd=1 (1) (2) */
4599: /* *TvarsD[nsd] 3 2 */
4600: /* *TnsdVar (3)=1 (2)=2 */
4601: /* *TvarsDind[nsd](=k) [1]=1 [2]=3 */
4602: /* *Tage[] [1]=2 [2]= 3 */
4603: /* *\/ */
4604: /* /\* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; *\/ */
4605: /* /\* 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)); *\/ */
4606: /* } */
4607: /* for (k=1; k<=nsq;k++) { /\* For single quantitative varying covariates only of the model *\/ */
4608: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4609: /* /\* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline *\/ */
4610: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
4611: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][resultmodel[nres][k1]] */
4612: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4613: /* /\* 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]); *\/ */
4614: /* } */
4615: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4616: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
4617: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4618: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
4619: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
4620: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4621: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
4622: /* } */
4623: /* /\* 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]); *\/ */
4624: /* } */
4625: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4626: /* /\* 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]); *\/ */
4627: /* if(Dummy[Tvard[k][1]]==0){ */
4628: /* if(Dummy[Tvard[k][2]]==0){ */
4629: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4630: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4631: /* }else{ */
4632: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4633: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
4634: /* } */
4635: /* }else{ */
4636: /* if(Dummy[Tvard[k][2]]==0){ */
4637: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4638: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
4639: /* }else{ */
4640: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4641: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
4642: /* } */
4643: /* } */
4644: /* } /\* End product without age *\/ */
4645: /* ENd of old code */
1.138 brouard 4646: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4647: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4648: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 4649: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4650: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.319 brouard 4651: /* age and covariate values of ij are in 'cov' */
1.142 brouard 4652: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 4653:
1.126 brouard 4654: savm=oldm;
4655: oldm=newm;
1.209 brouard 4656:
4657: for(j=1; j<=nlstate; j++){
4658: max[j]=0.;
4659: min[j]=1.;
4660: }
4661: for(i=1;i<=nlstate;i++){
4662: sumnew=0;
4663: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
4664: for(j=1; j<=nlstate; j++){
4665: prlim[i][j]= newm[i][j]/(1-sumnew);
4666: max[j]=FMAX(max[j],prlim[i][j]);
4667: min[j]=FMIN(min[j],prlim[i][j]);
4668: }
4669: }
4670:
1.126 brouard 4671: maxmax=0.;
1.209 brouard 4672: for(j=1; j<=nlstate; j++){
4673: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
4674: maxmax=FMAX(maxmax,meandiff[j]);
4675: /* 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 4676: } /* j loop */
1.203 brouard 4677: *ncvyear= (int)age- (int)agefin;
1.208 brouard 4678: /* 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 4679: if(maxmax < ftolpl){
1.209 brouard 4680: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
4681: free_vector(min,1,nlstate);
4682: free_vector(max,1,nlstate);
4683: free_vector(meandiff,1,nlstate);
1.126 brouard 4684: return prlim;
4685: }
1.288 brouard 4686: } /* agefin loop */
1.208 brouard 4687: /* After some age loop it doesn't converge */
1.288 brouard 4688: if(!first){
4689: first=1;
4690: 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 4691: 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);
4692: }else if (first >=1 && first <10){
4693: 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);
4694: first++;
4695: }else if (first ==10){
4696: 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);
4697: 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");
4698: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
4699: first++;
1.288 brouard 4700: }
4701:
1.359 ! brouard 4702: /* Try to lower 'ftol', for example from 1.e-8 to 6.e-9.\n", ftolpl,
! 4703: * (int)age, (int)delaymax, (int)agefin, ncvloop,
! 4704: * (int)age-(int)agefin); */
1.209 brouard 4705: free_vector(min,1,nlstate);
4706: free_vector(max,1,nlstate);
4707: free_vector(meandiff,1,nlstate);
1.208 brouard 4708:
1.169 brouard 4709: return prlim; /* should not reach here */
1.126 brouard 4710: }
4711:
1.217 brouard 4712:
4713: /**** Back Prevalence limit (stable or period prevalence) ****************/
4714:
1.218 brouard 4715: /* 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) */
4716: /* 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 4717: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 4718: {
1.264 brouard 4719: /* 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 4720: matrix by transitions matrix until convergence is reached with precision ftolpl */
4721: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
4722: /* Wx is row vector: population in state 1, population in state 2, population dead */
4723: /* or prevalence in state 1, prevalence in state 2, 0 */
4724: /* newm is the matrix after multiplications, its rows are identical at a factor */
4725: /* Initial matrix pimij */
4726: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
4727: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
4728: /* 0, 0 , 1} */
4729: /*
4730: * and after some iteration: */
4731: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
4732: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
4733: /* 0, 0 , 1} */
4734: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
4735: /* {0.51571254859325999, 0.4842874514067399, */
4736: /* 0.51326036147820708, 0.48673963852179264} */
4737: /* If we start from prlim again, prlim tends to a constant matrix */
4738:
1.359 ! brouard 4739: int i, ii,j, k1;
1.247 brouard 4740: int first=0;
1.217 brouard 4741: double *min, *max, *meandiff, maxmax,sumnew=0.;
4742: /* double **matprod2(); */ /* test */
4743: double **out, cov[NCOVMAX+1], **bmij();
4744: double **newm;
1.218 brouard 4745: double **dnewm, **doldm, **dsavm; /* for use */
4746: double **oldm, **savm; /* for use */
4747:
1.217 brouard 4748: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
4749: int ncvloop=0;
4750:
4751: min=vector(1,nlstate);
4752: max=vector(1,nlstate);
4753: meandiff=vector(1,nlstate);
4754:
1.266 brouard 4755: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
4756: oldm=oldms; savm=savms;
4757:
4758: /* Starting with matrix unity */
4759: for (ii=1;ii<=nlstate+ndeath;ii++)
4760: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 4761: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4762: }
4763:
4764: cov[1]=1.;
4765:
4766: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
4767: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 4768: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
1.288 brouard 4769: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
4770: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 4771: ncvloop++;
1.218 brouard 4772: newm=savm; /* oldm should be kept from previous iteration or unity at start */
4773: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 4774: /* Covariates have to be included here again */
4775: cov[2]=agefin;
1.319 brouard 4776: if(nagesqr==1){
1.217 brouard 4777: cov[3]= agefin*agefin;;
1.319 brouard 4778: }
1.332 brouard 4779: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 4780: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 4781: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.242 brouard 4782: }else{
1.332 brouard 4783: cov[2+nagesqr+k1]=precov[nres][k1];
1.242 brouard 4784: }
1.332 brouard 4785: }/* End of loop on model equation */
4786:
4787: /* Old code */
4788:
4789: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
4790: /* /\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\/ */
4791: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])]; */
4792: /* /\* 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)); *\/ */
4793: /* } */
4794: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
4795: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
4796: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
4797: /* /\* /\\* 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])]); *\\/ *\/ */
4798: /* /\* } *\/ */
4799: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
4800: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
4801: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
4802: /* /\* 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]); *\/ */
4803: /* } */
4804: /* /\* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; *\/ */
4805: /* /\* for (k=1; k<=cptcovprod;k++) /\\* Useless *\\/ *\/ */
4806: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\\/ *\/ */
4807: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
4808: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
4809: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age *\\/ ERROR ???*\/ */
4810: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
4811: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
4812: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
4813: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
4814: /* } */
4815: /* /\* 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]); *\/ */
4816: /* } */
4817: /* for (k=1; k<=cptcovprod;k++){ /\* For product without age *\/ */
4818: /* /\* 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]); *\/ */
4819: /* if(Dummy[Tvard[k][1]]==0){ */
4820: /* if(Dummy[Tvard[k][2]]==0){ */
4821: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
4822: /* }else{ */
4823: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
4824: /* } */
4825: /* }else{ */
4826: /* if(Dummy[Tvard[k][2]]==0){ */
4827: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
4828: /* }else{ */
4829: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
4830: /* } */
4831: /* } */
4832: /* } */
1.217 brouard 4833:
4834: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
4835: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
4836: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
4837: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4838: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 4839: /* ij should be linked to the correct index of cov */
4840: /* age and covariate values ij are in 'cov', but we need to pass
4841: * ij for the observed prevalence at age and status and covariate
4842: * number: prevacurrent[(int)agefin][ii][ij]
4843: */
4844: /* 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 *\/ */
4845: /* 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 *\/ */
4846: 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 4847: /* if((int)age == 86 || (int)age == 87){ */
1.266 brouard 4848: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
4849: /* for(i=1; i<=nlstate+ndeath; i++) { */
4850: /* printf("%d newm= ",i); */
4851: /* for(j=1;j<=nlstate+ndeath;j++) { */
4852: /* printf("%f ",newm[i][j]); */
4853: /* } */
4854: /* printf("oldm * "); */
4855: /* for(j=1;j<=nlstate+ndeath;j++) { */
4856: /* printf("%f ",oldm[i][j]); */
4857: /* } */
1.268 brouard 4858: /* printf(" bmmij "); */
1.266 brouard 4859: /* for(j=1;j<=nlstate+ndeath;j++) { */
4860: /* printf("%f ",pmmij[i][j]); */
4861: /* } */
4862: /* printf("\n"); */
4863: /* } */
4864: /* } */
1.217 brouard 4865: savm=oldm;
4866: oldm=newm;
1.266 brouard 4867:
1.217 brouard 4868: for(j=1; j<=nlstate; j++){
4869: max[j]=0.;
4870: min[j]=1.;
4871: }
4872: for(j=1; j<=nlstate; j++){
4873: for(i=1;i<=nlstate;i++){
1.234 brouard 4874: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
4875: bprlim[i][j]= newm[i][j];
4876: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
4877: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 4878: }
4879: }
1.218 brouard 4880:
1.217 brouard 4881: maxmax=0.;
4882: for(i=1; i<=nlstate; i++){
1.318 brouard 4883: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
1.217 brouard 4884: maxmax=FMAX(maxmax,meandiff[i]);
4885: /* 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 4886: } /* i loop */
1.217 brouard 4887: *ncvyear= -( (int)age- (int)agefin);
1.268 brouard 4888: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 4889: if(maxmax < ftolpl){
1.220 brouard 4890: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 4891: free_vector(min,1,nlstate);
4892: free_vector(max,1,nlstate);
4893: free_vector(meandiff,1,nlstate);
4894: return bprlim;
4895: }
1.288 brouard 4896: } /* agefin loop */
1.217 brouard 4897: /* After some age loop it doesn't converge */
1.288 brouard 4898: if(!first){
1.247 brouard 4899: first=1;
4900: 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\
4901: 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);
4902: }
4903: 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 4904: 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);
4905: /* 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); */
4906: free_vector(min,1,nlstate);
4907: free_vector(max,1,nlstate);
4908: free_vector(meandiff,1,nlstate);
4909:
4910: return bprlim; /* should not reach here */
4911: }
4912:
1.126 brouard 4913: /*************** transition probabilities ***************/
4914:
4915: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
4916: {
1.138 brouard 4917: /* According to parameters values stored in x and the covariate's values stored in cov,
1.266 brouard 4918: computes the probability to be observed in state j (after stepm years) being in state i by appying the
1.138 brouard 4919: model to the ncovmodel covariates (including constant and age).
4920: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
4921: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
4922: ncth covariate in the global vector x is given by the formula:
4923: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
4924: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
4925: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
4926: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1.266 brouard 4927: Outputs ps[i][j] or probability to be observed in j being in i according to
1.138 brouard 4928: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1.266 brouard 4929: Sum on j ps[i][j] should equal to 1.
1.138 brouard 4930: */
4931: double s1, lnpijopii;
1.126 brouard 4932: /*double t34;*/
1.164 brouard 4933: int i,j, nc, ii, jj;
1.126 brouard 4934:
1.223 brouard 4935: for(i=1; i<= nlstate; i++){
4936: for(j=1; j<i;j++){
4937: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
4938: /*lnpijopii += param[i][j][nc]*cov[nc];*/
4939: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
4940: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
4941: }
4942: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1.330 brouard 4943: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
1.223 brouard 4944: }
4945: for(j=i+1; j<=nlstate+ndeath;j++){
4946: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
4947: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
4948: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
4949: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
4950: }
4951: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
1.330 brouard 4952: /* printf("Debug pmij() i=%d j=%d nc=%d s1=%.17f, lnpijopii=%.17f\n",i,j,nc, s1,lnpijopii); */
1.223 brouard 4953: }
4954: }
1.218 brouard 4955:
1.223 brouard 4956: for(i=1; i<= nlstate; i++){
4957: s1=0;
4958: for(j=1; j<i; j++){
1.339 brouard 4959: /* printf("debug1 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
1.223 brouard 4960: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
4961: }
4962: for(j=i+1; j<=nlstate+ndeath; j++){
1.339 brouard 4963: /* printf("debug2 %d %d ps=%lf exp(ps)=%lf \n",i,j,ps[i][j],exp(ps[i][j])); */
1.223 brouard 4964: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
4965: }
4966: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
4967: ps[i][i]=1./(s1+1.);
4968: /* Computing other pijs */
4969: for(j=1; j<i; j++)
1.325 brouard 4970: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
1.223 brouard 4971: for(j=i+1; j<=nlstate+ndeath; j++)
4972: ps[i][j]= exp(ps[i][j])*ps[i][i];
4973: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
4974: } /* end i */
1.218 brouard 4975:
1.223 brouard 4976: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
4977: for(jj=1; jj<= nlstate+ndeath; jj++){
4978: ps[ii][jj]=0;
4979: ps[ii][ii]=1;
4980: }
4981: }
1.294 brouard 4982:
4983:
1.223 brouard 4984: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
4985: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
4986: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
4987: /* } */
4988: /* printf("\n "); */
4989: /* } */
4990: /* printf("\n ");printf("%lf ",cov[2]);*/
4991: /*
4992: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 4993: goto end;*/
1.266 brouard 4994: return ps; /* Pointer is unchanged since its call */
1.126 brouard 4995: }
4996:
1.218 brouard 4997: /*************** backward transition probabilities ***************/
4998:
4999: /* 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 ) */
5000: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
5001: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
5002: {
1.302 brouard 5003: /* 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 5004: * 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 5005: */
1.359 ! brouard 5006: int ii, j;
1.222 brouard 5007:
1.359 ! brouard 5008: double **pmij();
1.222 brouard 5009: double sumnew=0.;
1.218 brouard 5010: double agefin;
1.292 brouard 5011: 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 5012: double **dnewm, **dsavm, **doldm;
5013: double **bbmij;
5014:
1.218 brouard 5015: doldm=ddoldms; /* global pointers */
1.222 brouard 5016: dnewm=ddnewms;
5017: dsavm=ddsavms;
1.318 brouard 5018:
5019: /* Debug */
5020: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
1.222 brouard 5021: agefin=cov[2];
1.268 brouard 5022: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
1.222 brouard 5023: /* bmij *//* age is cov[2], ij is included in cov, but we need for
1.266 brouard 5024: the observed prevalence (with this covariate ij) at beginning of transition */
5025: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.268 brouard 5026:
5027: /* P_x */
1.325 brouard 5028: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
1.268 brouard 5029: /* outputs pmmij which is a stochastic matrix in row */
5030:
5031: /* Diag(w_x) */
1.292 brouard 5032: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
1.268 brouard 5033: sumnew=0.;
1.269 brouard 5034: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
1.268 brouard 5035: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
1.297 brouard 5036: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
1.268 brouard 5037: sumnew+=prevacurrent[(int)agefin][ii][ij];
5038: }
5039: if(sumnew >0.01){ /* At least some value in the prevalence */
5040: for (ii=1;ii<=nlstate+ndeath;ii++){
5041: for (j=1;j<=nlstate+ndeath;j++)
1.269 brouard 5042: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
1.268 brouard 5043: }
5044: }else{
5045: for (ii=1;ii<=nlstate+ndeath;ii++){
5046: for (j=1;j<=nlstate+ndeath;j++)
5047: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
5048: }
5049: /* if(sumnew <0.9){ */
5050: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
5051: /* } */
5052: }
5053: k3=0.0; /* We put the last diagonal to 0 */
5054: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
5055: doldm[ii][ii]= k3;
5056: }
5057: /* End doldm, At the end doldm is diag[(w_i)] */
5058:
1.292 brouard 5059: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
5060: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
1.268 brouard 5061:
1.292 brouard 5062: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
1.268 brouard 5063: /* 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 5064: for (j=1;j<=nlstate+ndeath;j++){
1.268 brouard 5065: sumnew=0.;
1.222 brouard 5066: for (ii=1;ii<=nlstate;ii++){
1.266 brouard 5067: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
1.268 brouard 5068: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
1.222 brouard 5069: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
1.268 brouard 5070: for (ii=1;ii<=nlstate+ndeath;ii++){
1.222 brouard 5071: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
1.268 brouard 5072: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 5073: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
1.268 brouard 5074: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 5075: /* }else */
1.268 brouard 5076: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
5077: } /*End ii */
5078: } /* 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 */
5079:
1.292 brouard 5080: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
1.268 brouard 5081: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
1.222 brouard 5082: /* end bmij */
1.266 brouard 5083: return ps; /*pointer is unchanged */
1.218 brouard 5084: }
1.217 brouard 5085: /*************** transition probabilities ***************/
5086:
1.218 brouard 5087: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 5088: {
5089: /* According to parameters values stored in x and the covariate's values stored in cov,
5090: computes the probability to be observed in state j being in state i by appying the
5091: model to the ncovmodel covariates (including constant and age).
5092: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
5093: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
5094: ncth covariate in the global vector x is given by the formula:
5095: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
5096: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
5097: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
5098: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
5099: Outputs ps[i][j] the probability to be observed in j being in j according to
5100: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
5101: */
5102: double s1, lnpijopii;
5103: /*double t34;*/
5104: int i,j, nc, ii, jj;
5105:
1.234 brouard 5106: for(i=1; i<= nlstate; i++){
5107: for(j=1; j<i;j++){
5108: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5109: /*lnpijopii += param[i][j][nc]*cov[nc];*/
5110: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
5111: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5112: }
5113: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5114: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
5115: }
5116: for(j=i+1; j<=nlstate+ndeath;j++){
5117: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
5118: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
5119: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
5120: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
5121: }
5122: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
5123: }
5124: }
5125:
5126: for(i=1; i<= nlstate; i++){
5127: s1=0;
5128: for(j=1; j<i; j++){
5129: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5130: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5131: }
5132: for(j=i+1; j<=nlstate+ndeath; j++){
5133: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
5134: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
5135: }
5136: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
5137: ps[i][i]=1./(s1+1.);
5138: /* Computing other pijs */
5139: for(j=1; j<i; j++)
5140: ps[i][j]= exp(ps[i][j])*ps[i][i];
5141: for(j=i+1; j<=nlstate+ndeath; j++)
5142: ps[i][j]= exp(ps[i][j])*ps[i][i];
5143: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
5144: } /* end i */
5145:
5146: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
5147: for(jj=1; jj<= nlstate+ndeath; jj++){
5148: ps[ii][jj]=0;
5149: ps[ii][ii]=1;
5150: }
5151: }
1.296 brouard 5152: /* Added for prevbcast */ /* Transposed matrix too */
1.234 brouard 5153: for(jj=1; jj<= nlstate+ndeath; jj++){
5154: s1=0.;
5155: for(ii=1; ii<= nlstate+ndeath; ii++){
5156: s1+=ps[ii][jj];
5157: }
5158: for(ii=1; ii<= nlstate; ii++){
5159: ps[ii][jj]=ps[ii][jj]/s1;
5160: }
5161: }
5162: /* Transposition */
5163: for(jj=1; jj<= nlstate+ndeath; jj++){
5164: for(ii=jj; ii<= nlstate+ndeath; ii++){
5165: s1=ps[ii][jj];
5166: ps[ii][jj]=ps[jj][ii];
5167: ps[jj][ii]=s1;
5168: }
5169: }
5170: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
5171: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
5172: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
5173: /* } */
5174: /* printf("\n "); */
5175: /* } */
5176: /* printf("\n ");printf("%lf ",cov[2]);*/
5177: /*
5178: for(i=1; i<= npar; i++) printf("%f ",x[i]);
5179: goto end;*/
5180: return ps;
1.217 brouard 5181: }
5182:
5183:
1.126 brouard 5184: /**************** Product of 2 matrices ******************/
5185:
1.145 brouard 5186: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 5187: {
5188: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
5189: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
5190: /* in, b, out are matrice of pointers which should have been initialized
5191: before: only the contents of out is modified. The function returns
5192: a pointer to pointers identical to out */
1.145 brouard 5193: int i, j, k;
1.126 brouard 5194: for(i=nrl; i<= nrh; i++)
1.145 brouard 5195: for(k=ncolol; k<=ncoloh; k++){
5196: out[i][k]=0.;
5197: for(j=ncl; j<=nch; j++)
5198: out[i][k] +=in[i][j]*b[j][k];
5199: }
1.126 brouard 5200: return out;
5201: }
5202:
5203:
5204: /************* Higher Matrix Product ***************/
5205:
1.235 brouard 5206: 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 5207: {
1.336 brouard 5208: /* Already optimized with precov.
5209: 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 5210: 'nhstepm*hstepm*stepm' months (i.e. until
5211: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5212: nhstepm*hstepm matrices.
5213: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5214: (typically every 2 years instead of every month which is too big
5215: for the memory).
5216: Model is determined by parameters x and covariates have to be
5217: included manually here.
5218:
5219: */
5220:
1.359 ! brouard 5221: int i, j, d, h, k1;
1.131 brouard 5222: double **out, cov[NCOVMAX+1];
1.126 brouard 5223: double **newm;
1.187 brouard 5224: double agexact;
1.359 ! brouard 5225: /*double agebegin, ageend;*/
1.126 brouard 5226:
5227: /* Hstepm could be zero and should return the unit matrix */
5228: for (i=1;i<=nlstate+ndeath;i++)
5229: for (j=1;j<=nlstate+ndeath;j++){
5230: oldm[i][j]=(i==j ? 1.0 : 0.0);
5231: po[i][j][0]=(i==j ? 1.0 : 0.0);
5232: }
5233: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5234: for(h=1; h <=nhstepm; h++){
5235: for(d=1; d <=hstepm; d++){
5236: newm=savm;
5237: /* Covariates have to be included here again */
5238: cov[1]=1.;
1.214 brouard 5239: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 5240: cov[2]=agexact;
1.319 brouard 5241: if(nagesqr==1){
1.227 brouard 5242: cov[3]= agexact*agexact;
1.319 brouard 5243: }
1.330 brouard 5244: /* Model(2) V1 + V2 + V3 + V8 + V7*V8 + V5*V6 + V8*age + V3*age + age*age */
5245: /* total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age */
5246: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 5247: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 5248: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
5249: }else{
5250: cov[2+nagesqr+k1]=precov[nres][k1];
5251: }
5252: }/* End of loop on model equation */
5253: /* Old code */
5254: /* if( Dummy[k1]==0 && Typevar[k1]==0 ){ /\* Single dummy *\/ */
5255: /* /\* V(Tvarsel)=Tvalsel=Tresult[nres][pos](value); V(Tvresult[nres][pos] (variable): V(variable)=value) *\/ */
5256: /* /\* for (k=1; k<=nsd;k++) { /\\* For single dummy covariates only *\\/ *\/ */
5257: /* /\* /\\* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates *\\/ *\/ */
5258: /* /\* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 *\/ */
5259: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5260: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5261: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
5262: /* /\* nsd 1 2 3 *\/ /\* Counting single dummies covar fixed or tv *\/ */
5263: /* /\*TvarsD[nsd] 4 3 1 *\/ /\* ID of single dummy cova fixed or timevary*\/ */
5264: /* /\*TvarsDind[k] 2 3 9 *\/ /\* position K of single dummy cova *\/ */
5265: /* /\* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];or [codtabm(ij,TnsdVar[TvarsD[k]] *\/ */
5266: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5267: /* /\* 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]])); *\/ */
5268: /* 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); */
5269: /* printf("hpxij new Dummy precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5270: /* }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /\* Single quantitative variables *\/ */
5271: /* /\* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline *\/ */
5272: /* cov[2+nagesqr+k1]=Tqresult[nres][resultmodel[nres][k1]]; */
5273: /* /\* for (k=1; k<=nsq;k++) { /\\* For single varying covariates only *\\/ *\/ */
5274: /* /\* /\\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\\/ *\/ */
5275: /* /\* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; *\/ */
5276: /* 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]]); */
5277: /* printf("hpxij new Quanti precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5278: /* }else if( Dummy[k1]==2 ){ /\* For dummy with age product *\/ */
5279: /* /\* Tvar[k1] Variable in the age product age*V1 is 1 *\/ */
5280: /* /\* [Tinvresult[nres][V1] is its value in the resultline nres *\/ */
5281: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvar[k1]]*cov[2]; */
5282: /* 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]); */
5283: /* printf("hpxij new Dummy with age product precov[nres=%d][k1=%d]=%.4f * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5284:
5285: /* /\* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; *\/ */
5286: /* /\* for (k=1; k<=cptcovage;k++){ /\\* For product with age V1+V1*age +V4 +age*V3 *\\/ *\/ */
5287: /* /\* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*\/ */
5288: /* /\* *\/ */
1.330 brouard 5289: /* /\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\/ */
5290: /* /\* k 1 2 3 4 5 6 7 8 9 *\/ */
5291: /* /\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\/ */
1.332 brouard 5292: /* /\*cptcovage=2 1 2 *\/ */
5293: /* /\*Tage[k]= 5 8 *\/ */
5294: /* }else if( Dummy[k1]==3 ){ /\* For quant with age product *\/ */
5295: /* cov[2+nagesqr+k1]=Tresult[nres][resultmodel[nres][k1]]; */
5296: /* 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]]); */
5297: /* printf("hpxij new Quanti with age product precov[nres=%d][k1=%d] * age=%.2f\n", nres, k1, precov[nres][k1], cov[2]); */
5298: /* /\* if(Dummy[Tage[k]]== 2){ /\\* dummy with age *\\/ *\/ */
5299: /* /\* /\\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\\* dummy with age *\\\/ *\\/ *\/ */
5300: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\\/ *\/ */
5301: /* /\* /\\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\\/ *\/ */
5302: /* /\* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,TnsdVar[TvarsD[Tvar[Tage[k]]]])]*cov[2]; *\/ */
5303: /* /\* 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); *\/ */
5304: /* /\* } else if(Dummy[Tage[k]]== 3){ /\\* quantitative with age *\\/ *\/ */
5305: /* /\* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; *\/ */
5306: /* /\* } *\/ */
5307: /* /\* 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]); *\/ */
5308: /* }else if(Typevar[k1]==2 ){ /\* For product (not with age) *\/ */
5309: /* /\* for (k=1; k<=cptcovprod;k++){ /\\* For product without age *\\/ *\/ */
5310: /* /\* /\\* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 *\\/ *\/ */
5311: /* /\* /\\* k 1 2 3 4 5 6 7 8 9 *\\/ *\/ */
5312: /* /\* /\\*Tvar[k]= 5 4 3 6 5 2 7 1 1 *\\/ *\/ */
5313: /* /\* /\\*cptcovprod=1 1 2 *\\/ *\/ */
5314: /* /\* /\\*Tprod[]= 4 7 *\\/ *\/ */
5315: /* /\* /\\*Tvard[][1] 4 1 *\\/ *\/ */
5316: /* /\* /\\*Tvard[][2] 3 2 *\\/ *\/ */
1.330 brouard 5317:
1.332 brouard 5318: /* /\* 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])]); *\/ */
5319: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5320: /* cov[2+nagesqr+k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]]; */
5321: /* 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]]); */
5322: /* printf("hpxij new Product no age product precov[nres=%d][k1=%d]=%.4f\n", nres, k1, precov[nres][k1]); */
5323:
5324: /* /\* if(Dummy[Tvardk[k1][1]]==0){ *\/ */
5325: /* /\* if(Dummy[Tvardk[k1][2]]==0){ /\\* Product of dummies *\\/ *\/ */
5326: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
5327: /* /\* cov[2+nagesqr+k1]=Tinvresult[nres][Tvardk[k1][1]] * Tinvresult[nres][Tvardk[k1][2]]; *\/ */
5328: /* /\* 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]])]; *\/ */
5329: /* /\* }else{ /\\* Product of dummy by quantitative *\\/ *\/ */
5330: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,TnsdVar[Tvard[k][1]])] * Tqresult[nres][k]; *\/ */
5331: /* /\* cov[2+nagesqr+k1]=Tresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]]; *\/ */
5332: /* /\* } *\/ */
5333: /* /\* }else{ /\\* Product of quantitative by...*\\/ *\/ */
5334: /* /\* if(Dummy[Tvard[k][2]]==0){ /\\* quant by dummy *\\/ *\/ */
5335: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][Tvard[k][1]]; *\\/ *\/ */
5336: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5337: /* /\* }else{ /\\* Product of two quant *\\/ *\/ */
5338: /* /\* /\\* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\\/ *\/ */
5339: /* /\* cov[2+nagesqr+k1]=Tqresult[nres][Tinvresult[nres][Tvardk[k1][1]]] * Tqresult[nres][Tinvresult[nres][Tvardk[k1][2]]] ; *\/ */
5340: /* /\* } *\/ */
5341: /* /\* }/\\*end of products quantitative *\\/ *\/ */
5342: /* }/\*end of products *\/ */
5343: /* } /\* End of loop on model equation *\/ */
1.235 brouard 5344: /* for (k=1; k<=cptcovn;k++) */
5345: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
5346: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
5347: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
5348: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
5349: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 5350:
5351:
1.126 brouard 5352: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
5353: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.319 brouard 5354: /* right multiplication of oldm by the current matrix */
1.126 brouard 5355: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
5356: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 5357: /* if((int)age == 70){ */
5358: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5359: /* for(i=1; i<=nlstate+ndeath; i++) { */
5360: /* printf("%d pmmij ",i); */
5361: /* for(j=1;j<=nlstate+ndeath;j++) { */
5362: /* printf("%f ",pmmij[i][j]); */
5363: /* } */
5364: /* printf(" oldm "); */
5365: /* for(j=1;j<=nlstate+ndeath;j++) { */
5366: /* printf("%f ",oldm[i][j]); */
5367: /* } */
5368: /* printf("\n"); */
5369: /* } */
5370: /* } */
1.126 brouard 5371: savm=oldm;
5372: oldm=newm;
5373: }
5374: for(i=1; i<=nlstate+ndeath; i++)
5375: for(j=1;j<=nlstate+ndeath;j++) {
1.267 brouard 5376: po[i][j][h]=newm[i][j];
5377: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 5378: }
1.128 brouard 5379: /*printf("h=%d ",h);*/
1.126 brouard 5380: } /* end h */
1.267 brouard 5381: /* printf("\n H=%d \n",h); */
1.126 brouard 5382: return po;
5383: }
5384:
1.217 brouard 5385: /************* Higher Back Matrix Product ***************/
1.218 brouard 5386: /* 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 5387: 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 5388: {
1.332 brouard 5389: /* For dummy covariates given in each resultline (for historical, computes the corresponding combination ij),
5390: computes the transition matrix starting at age 'age' over
1.217 brouard 5391: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 5392: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
5393: nhstepm*hstepm matrices.
5394: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
5395: (typically every 2 years instead of every month which is too big
1.217 brouard 5396: for the memory).
1.218 brouard 5397: Model is determined by parameters x and covariates have to be
1.266 brouard 5398: included manually here. Then we use a call to bmij(x and cov)
5399: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
1.222 brouard 5400: */
1.217 brouard 5401:
1.359 ! brouard 5402: int i, j, d, h, k1;
1.266 brouard 5403: double **out, cov[NCOVMAX+1], **bmij();
5404: double **newm, ***newmm;
1.217 brouard 5405: double agexact;
1.359 ! brouard 5406: /*double agebegin, ageend;*/
1.222 brouard 5407: double **oldm, **savm;
1.217 brouard 5408:
1.266 brouard 5409: newmm=po; /* To be saved */
5410: oldm=oldms;savm=savms; /* Global pointers */
1.217 brouard 5411: /* Hstepm could be zero and should return the unit matrix */
5412: for (i=1;i<=nlstate+ndeath;i++)
5413: for (j=1;j<=nlstate+ndeath;j++){
5414: oldm[i][j]=(i==j ? 1.0 : 0.0);
5415: po[i][j][0]=(i==j ? 1.0 : 0.0);
5416: }
5417: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
5418: for(h=1; h <=nhstepm; h++){
5419: for(d=1; d <=hstepm; d++){
5420: newm=savm;
5421: /* Covariates have to be included here again */
5422: cov[1]=1.;
1.271 brouard 5423: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
1.217 brouard 5424: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
1.318 brouard 5425: /* Debug */
5426: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
1.217 brouard 5427: cov[2]=agexact;
1.332 brouard 5428: if(nagesqr==1){
1.222 brouard 5429: cov[3]= agexact*agexact;
1.332 brouard 5430: }
5431: /** New code */
5432: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 5433: if(Typevar[k1]==1 || Typevar[k1]==3){ /* A product with age */
1.332 brouard 5434: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.325 brouard 5435: }else{
1.332 brouard 5436: cov[2+nagesqr+k1]=precov[nres][k1];
1.325 brouard 5437: }
1.332 brouard 5438: }/* End of loop on model equation */
5439: /** End of new code */
5440: /** This was old code */
5441: /* for (k=1; k<=nsd;k++){ /\* For single dummy covariates only *\//\* cptcovn error *\/ */
5442: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; *\/ */
5443: /* /\* /\\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\\/ *\/ */
5444: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,TvarsD[k])];/\* Bug valgrind *\/ */
5445: /* /\* 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)); *\/ */
5446: /* } */
5447: /* for (k=1; k<=nsq;k++) { /\* For single varying covariates only *\/ */
5448: /* /\* Here comes the value of quantitative after renumbering k with single quantitative covariates *\/ */
5449: /* cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; */
5450: /* /\* 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]); *\/ */
5451: /* } */
5452: /* for (k=1; k<=cptcovage;k++){ /\* Should start at cptcovn+1 *\//\* For product with age *\/ */
5453: /* /\* if(Dummy[Tvar[Tage[k]]]== 2){ /\\* dummy with age error!!!*\\/ *\/ */
5454: /* if(Dummy[Tage[k]]== 2){ /\* dummy with age *\/ */
5455: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
5456: /* } else if(Dummy[Tage[k]]== 3){ /\* quantitative with age *\/ */
5457: /* cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; */
5458: /* } */
5459: /* /\* 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]); *\/ */
5460: /* } */
5461: /* for (k=1; k<=cptcovprod;k++){ /\* Useless because included in cptcovn *\/ */
5462: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
5463: /* if(Dummy[Tvard[k][1]]==0){ */
5464: /* if(Dummy[Tvard[k][2]]==0){ */
5465: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][1])]; */
5466: /* }else{ */
5467: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * Tqresult[nres][k]; */
5468: /* } */
5469: /* }else{ */
5470: /* if(Dummy[Tvard[k][2]]==0){ */
5471: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])] * Tqinvresult[nres][Tvard[k][1]]; */
5472: /* }else{ */
5473: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
5474: /* } */
5475: /* } */
5476: /* } */
5477: /* /\*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*\/ */
5478: /* /\*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*\/ */
5479: /** End of old code */
5480:
1.218 brouard 5481: /* Careful transposed matrix */
1.266 brouard 5482: /* age is in cov[2], prevacurrent at beginning of transition. */
1.218 brouard 5483: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 5484: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 5485: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.325 brouard 5486: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
1.217 brouard 5487: /* if((int)age == 70){ */
5488: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
5489: /* for(i=1; i<=nlstate+ndeath; i++) { */
5490: /* printf("%d pmmij ",i); */
5491: /* for(j=1;j<=nlstate+ndeath;j++) { */
5492: /* printf("%f ",pmmij[i][j]); */
5493: /* } */
5494: /* printf(" oldm "); */
5495: /* for(j=1;j<=nlstate+ndeath;j++) { */
5496: /* printf("%f ",oldm[i][j]); */
5497: /* } */
5498: /* printf("\n"); */
5499: /* } */
5500: /* } */
5501: savm=oldm;
5502: oldm=newm;
5503: }
5504: for(i=1; i<=nlstate+ndeath; i++)
5505: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 5506: po[i][j][h]=newm[i][j];
1.268 brouard 5507: /* if(h==nhstepm) */
5508: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
1.217 brouard 5509: }
1.268 brouard 5510: /* printf("h=%d %.1f ",h, agexact); */
1.217 brouard 5511: } /* end h */
1.268 brouard 5512: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
1.217 brouard 5513: return po;
5514: }
5515:
5516:
1.162 brouard 5517: #ifdef NLOPT
5518: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
5519: double fret;
5520: double *xt;
5521: int j;
5522: myfunc_data *d2 = (myfunc_data *) pd;
5523: /* xt = (p1-1); */
5524: xt=vector(1,n);
5525: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
5526:
5527: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
5528: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
5529: printf("Function = %.12lf ",fret);
5530: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
5531: printf("\n");
5532: free_vector(xt,1,n);
5533: return fret;
5534: }
5535: #endif
1.126 brouard 5536:
5537: /*************** log-likelihood *************/
5538: double func( double *x)
5539: {
1.336 brouard 5540: int i, ii, j, k, mi, d, kk, kf=0;
1.226 brouard 5541: int ioffset=0;
1.339 brouard 5542: int ipos=0,iposold=0,ncovv=0;
5543:
1.340 brouard 5544: double cotvarv, cotvarvold;
1.226 brouard 5545: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
5546: double **out;
5547: double lli; /* Individual log likelihood */
5548: int s1, s2;
1.228 brouard 5549: 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 5550:
1.226 brouard 5551: double bbh, survp;
5552: double agexact;
1.336 brouard 5553: double agebegin, ageend;
1.226 brouard 5554: /*extern weight */
5555: /* We are differentiating ll according to initial status */
5556: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
5557: /*for(i=1;i<imx;i++)
5558: printf(" %d\n",s[4][i]);
5559: */
1.162 brouard 5560:
1.226 brouard 5561: ++countcallfunc;
1.162 brouard 5562:
1.226 brouard 5563: cov[1]=1.;
1.126 brouard 5564:
1.226 brouard 5565: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 5566: ioffset=0;
1.226 brouard 5567: if(mle==1){
5568: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5569: /* Computes the values of the ncovmodel covariates of the model
5570: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
5571: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
5572: to be observed in j being in i according to the model.
5573: */
1.243 brouard 5574: ioffset=2+nagesqr ;
1.233 brouard 5575: /* Fixed */
1.345 brouard 5576: for (kf=1; kf<=ncovf;kf++){ /* For each fixed covariate dummy or quant or prod */
1.319 brouard 5577: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
5578: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
5579: /* 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 5580: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.336 brouard 5581: 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 5582: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
1.234 brouard 5583: }
1.226 brouard 5584: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
1.319 brouard 5585: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
1.226 brouard 5586: has been calculated etc */
5587: /* For an individual i, wav[i] gives the number of effective waves */
5588: /* We compute the contribution to Likelihood of each effective transition
5589: mw[mi][i] is real wave of the mi th effectve wave */
5590: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
5591: s2=s[mw[mi+1][i]][i];
1.341 brouard 5592: 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 5593: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
5594: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
5595: */
1.336 brouard 5596: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
5597: /* Wave varying (but not age varying) */
1.339 brouard 5598: /* 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*\/ */
5599: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? *\/ */
5600: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
5601: /* } */
1.340 brouard 5602: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age )*/
5603: itv=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
5604: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
1.345 brouard 5605: if(FixedV[itv]!=0){ /* Not a fixed covariate */
1.341 brouard 5606: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* cotvar[wav][ncovcol+nqv+iv][i] */
1.340 brouard 5607: }else{ /* fixed covariate */
1.345 brouard 5608: 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 5609: }
1.339 brouard 5610: if(ipos!=iposold){ /* Not a product or first of a product */
1.340 brouard 5611: cotvarvold=cotvarv;
5612: }else{ /* A second product */
5613: cotvarv=cotvarv*cotvarvold;
1.339 brouard 5614: }
5615: iposold=ipos;
1.340 brouard 5616: cov[ioffset+ipos]=cotvarv;
1.234 brouard 5617: }
1.339 brouard 5618: /* for products of time varying to be done */
1.234 brouard 5619: for (ii=1;ii<=nlstate+ndeath;ii++)
5620: for (j=1;j<=nlstate+ndeath;j++){
5621: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5622: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5623: }
1.336 brouard 5624:
5625: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
5626: 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 5627: for(d=0; d<dh[mi][i]; d++){
5628: newm=savm;
5629: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5630: cov[2]=agexact;
5631: if(nagesqr==1)
5632: cov[3]= agexact*agexact; /* Should be changed here */
1.349 brouard 5633: /* for (kk=1; kk<=cptcovage;kk++) { */
5634: /* if(!FixedV[Tvar[Tage[kk]]]) */
5635: /* cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /\* Tage[kk] gives the data-covariate associated with age *\/ */
5636: /* else */
5637: /* 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) *\/ */
5638: /* } */
5639: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
5640: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
5641: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
5642: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
5643: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
5644: }else{ /* fixed covariate */
5645: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
5646: }
5647: if(ipos!=iposold){ /* Not a product or first of a product */
5648: cotvarvold=cotvarv;
5649: }else{ /* A second product */
5650: cotvarv=cotvarv*cotvarvold;
5651: }
5652: iposold=ipos;
5653: cov[ioffset+ipos]=cotvarv*agexact;
5654: /* For products */
1.234 brouard 5655: }
1.349 brouard 5656:
1.234 brouard 5657: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5658: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5659: savm=oldm;
5660: oldm=newm;
5661: } /* end mult */
5662:
5663: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
5664: /* But now since version 0.9 we anticipate for bias at large stepm.
5665: * If stepm is larger than one month (smallest stepm) and if the exact delay
5666: * (in months) between two waves is not a multiple of stepm, we rounded to
5667: * the nearest (and in case of equal distance, to the lowest) interval but now
5668: * we keep into memory the bias bh[mi][i] and also the previous matrix product
5669: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
5670: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 5671: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
5672: * -stepm/2 to stepm/2 .
5673: * For stepm=1 the results are the same as for previous versions of Imach.
5674: * For stepm > 1 the results are less biased than in previous versions.
5675: */
1.234 brouard 5676: s1=s[mw[mi][i]][i];
5677: s2=s[mw[mi+1][i]][i];
5678: bbh=(double)bh[mi][i]/(double)stepm;
5679: /* bias bh is positive if real duration
5680: * is higher than the multiple of stepm and negative otherwise.
5681: */
5682: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
5683: if( s2 > nlstate){
5684: /* i.e. if s2 is a death state and if the date of death is known
5685: then the contribution to the likelihood is the probability to
5686: die between last step unit time and current step unit time,
5687: which is also equal to probability to die before dh
5688: minus probability to die before dh-stepm .
5689: In version up to 0.92 likelihood was computed
5690: as if date of death was unknown. Death was treated as any other
5691: health state: the date of the interview describes the actual state
5692: and not the date of a change in health state. The former idea was
5693: to consider that at each interview the state was recorded
5694: (healthy, disable or death) and IMaCh was corrected; but when we
5695: introduced the exact date of death then we should have modified
5696: the contribution of an exact death to the likelihood. This new
5697: contribution is smaller and very dependent of the step unit
5698: stepm. It is no more the probability to die between last interview
5699: and month of death but the probability to survive from last
5700: interview up to one month before death multiplied by the
5701: probability to die within a month. Thanks to Chris
5702: Jackson for correcting this bug. Former versions increased
5703: mortality artificially. The bad side is that we add another loop
5704: which slows down the processing. The difference can be up to 10%
5705: lower mortality.
5706: */
5707: /* If, at the beginning of the maximization mostly, the
5708: cumulative probability or probability to be dead is
5709: constant (ie = 1) over time d, the difference is equal to
5710: 0. out[s1][3] = savm[s1][3]: probability, being at state
5711: s1 at precedent wave, to be dead a month before current
5712: wave is equal to probability, being at state s1 at
5713: precedent wave, to be dead at mont of the current
5714: wave. Then the observed probability (that this person died)
5715: is null according to current estimated parameter. In fact,
5716: it should be very low but not zero otherwise the log go to
5717: infinity.
5718: */
1.183 brouard 5719: /* #ifdef INFINITYORIGINAL */
5720: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5721: /* #else */
5722: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
5723: /* lli=log(mytinydouble); */
5724: /* else */
5725: /* lli=log(out[s1][s2] - savm[s1][s2]); */
5726: /* #endif */
1.226 brouard 5727: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 5728:
1.226 brouard 5729: } else if ( s2==-1 ) { /* alive */
5730: for (j=1,survp=0. ; j<=nlstate; j++)
5731: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
5732: /*survp += out[s1][j]; */
5733: lli= log(survp);
5734: }
1.336 brouard 5735: /* else if (s2==-4) { */
5736: /* for (j=3,survp=0. ; j<=nlstate; j++) */
5737: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5738: /* lli= log(survp); */
5739: /* } */
5740: /* else if (s2==-5) { */
5741: /* for (j=1,survp=0. ; j<=2; j++) */
5742: /* survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; */
5743: /* lli= log(survp); */
5744: /* } */
1.226 brouard 5745: else{
5746: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
5747: /* 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 */
5748: }
5749: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
5750: /*if(lli ==000.0)*/
1.340 brouard 5751: /* 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 5752: ipmx +=1;
5753: sw += weight[i];
5754: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5755: /* if (lli < log(mytinydouble)){ */
5756: /* 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); */
5757: /* 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]); */
5758: /* } */
5759: } /* end of wave */
5760: } /* end of individual */
5761: } else if(mle==2){
5762: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.319 brouard 5763: ioffset=2+nagesqr ;
5764: for (k=1; k<=ncovf;k++)
5765: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
1.226 brouard 5766: for(mi=1; mi<= wav[i]-1; mi++){
1.319 brouard 5767: for(k=1; k <= ncovv ; k++){
1.341 brouard 5768: 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 5769: }
1.226 brouard 5770: for (ii=1;ii<=nlstate+ndeath;ii++)
5771: for (j=1;j<=nlstate+ndeath;j++){
5772: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5773: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5774: }
5775: for(d=0; d<=dh[mi][i]; d++){
5776: newm=savm;
5777: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5778: cov[2]=agexact;
5779: if(nagesqr==1)
5780: cov[3]= agexact*agexact;
5781: for (kk=1; kk<=cptcovage;kk++) {
5782: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5783: }
5784: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5785: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5786: savm=oldm;
5787: oldm=newm;
5788: } /* end mult */
5789:
5790: s1=s[mw[mi][i]][i];
5791: s2=s[mw[mi+1][i]][i];
5792: bbh=(double)bh[mi][i]/(double)stepm;
5793: 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 */
5794: ipmx +=1;
5795: sw += weight[i];
5796: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5797: } /* end of wave */
5798: } /* end of individual */
5799: } else if(mle==3){ /* exponential inter-extrapolation */
5800: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5801: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5802: for(mi=1; mi<= wav[i]-1; mi++){
5803: for (ii=1;ii<=nlstate+ndeath;ii++)
5804: for (j=1;j<=nlstate+ndeath;j++){
5805: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5806: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5807: }
5808: for(d=0; d<dh[mi][i]; d++){
5809: newm=savm;
5810: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5811: cov[2]=agexact;
5812: if(nagesqr==1)
5813: cov[3]= agexact*agexact;
5814: for (kk=1; kk<=cptcovage;kk++) {
1.340 brouard 5815: if(!FixedV[Tvar[Tage[kk]]])
5816: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5817: else
1.341 brouard 5818: 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 5819: }
5820: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5821: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5822: savm=oldm;
5823: oldm=newm;
5824: } /* end mult */
5825:
5826: s1=s[mw[mi][i]][i];
5827: s2=s[mw[mi+1][i]][i];
5828: bbh=(double)bh[mi][i]/(double)stepm;
5829: 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 */
5830: ipmx +=1;
5831: sw += weight[i];
5832: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5833: } /* end of wave */
5834: } /* end of individual */
5835: }else if (mle==4){ /* ml=4 no inter-extrapolation */
5836: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5837: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5838: for(mi=1; mi<= wav[i]-1; mi++){
5839: for (ii=1;ii<=nlstate+ndeath;ii++)
5840: for (j=1;j<=nlstate+ndeath;j++){
5841: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5842: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5843: }
5844: for(d=0; d<dh[mi][i]; d++){
5845: newm=savm;
5846: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5847: cov[2]=agexact;
5848: if(nagesqr==1)
5849: cov[3]= agexact*agexact;
5850: for (kk=1; kk<=cptcovage;kk++) {
5851: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
5852: }
1.126 brouard 5853:
1.226 brouard 5854: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5855: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5856: savm=oldm;
5857: oldm=newm;
5858: } /* end mult */
5859:
5860: s1=s[mw[mi][i]][i];
5861: s2=s[mw[mi+1][i]][i];
5862: if( s2 > nlstate){
5863: lli=log(out[s1][s2] - savm[s1][s2]);
5864: } else if ( s2==-1 ) { /* alive */
5865: for (j=1,survp=0. ; j<=nlstate; j++)
5866: survp += out[s1][j];
5867: lli= log(survp);
5868: }else{
5869: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5870: }
5871: ipmx +=1;
5872: sw += weight[i];
5873: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.343 brouard 5874: /* 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 5875: } /* end of wave */
5876: } /* end of individual */
5877: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
5878: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
5879: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
5880: for(mi=1; mi<= wav[i]-1; mi++){
5881: for (ii=1;ii<=nlstate+ndeath;ii++)
5882: for (j=1;j<=nlstate+ndeath;j++){
5883: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
5884: savm[ii][j]=(ii==j ? 1.0 : 0.0);
5885: }
5886: for(d=0; d<dh[mi][i]; d++){
5887: newm=savm;
5888: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
5889: cov[2]=agexact;
5890: if(nagesqr==1)
5891: cov[3]= agexact*agexact;
5892: for (kk=1; kk<=cptcovage;kk++) {
1.340 brouard 5893: if(!FixedV[Tvar[Tage[kk]]])
5894: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
5895: else
1.341 brouard 5896: 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 5897: }
1.126 brouard 5898:
1.226 brouard 5899: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
5900: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
5901: savm=oldm;
5902: oldm=newm;
5903: } /* end mult */
5904:
5905: s1=s[mw[mi][i]][i];
5906: s2=s[mw[mi+1][i]][i];
5907: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
5908: ipmx +=1;
5909: sw += weight[i];
5910: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
5911: /*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]);*/
5912: } /* end of wave */
5913: } /* end of individual */
5914: } /* End of if */
5915: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
5916: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
5917: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
5918: return -l;
1.126 brouard 5919: }
5920:
5921: /*************** log-likelihood *************/
5922: double funcone( double *x)
5923: {
1.228 brouard 5924: /* Same as func but slower because of a lot of printf and if */
1.359 ! brouard 5925: int i, ii, j, k, mi, d, kv=0, kf=0;
1.228 brouard 5926: int ioffset=0;
1.339 brouard 5927: int ipos=0,iposold=0,ncovv=0;
5928:
1.340 brouard 5929: double cotvarv, cotvarvold;
1.131 brouard 5930: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 5931: double **out;
5932: double lli; /* Individual log likelihood */
5933: double llt;
5934: int s1, s2;
1.228 brouard 5935: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
5936:
1.126 brouard 5937: double bbh, survp;
1.187 brouard 5938: double agexact;
1.214 brouard 5939: double agebegin, ageend;
1.126 brouard 5940: /*extern weight */
5941: /* We are differentiating ll according to initial status */
5942: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
5943: /*for(i=1;i<imx;i++)
5944: printf(" %d\n",s[4][i]);
5945: */
5946: cov[1]=1.;
5947:
5948: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 5949: ioffset=0;
5950: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.336 brouard 5951: /* Computes the values of the ncovmodel covariates of the model
5952: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
5953: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
5954: to be observed in j being in i according to the model.
5955: */
1.243 brouard 5956: /* ioffset=2+nagesqr+cptcovage; */
5957: ioffset=2+nagesqr;
1.232 brouard 5958: /* Fixed */
1.224 brouard 5959: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 5960: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
1.349 brouard 5961: 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 5962: /* printf("Debug3 TvarFind[%d]=%d",kf, TvarFind[kf]); */
5963: /* printf(" Tvar[TvarFind[kf]]=%d", Tvar[TvarFind[kf]]); */
5964: /* printf(" i=%d covar[Tvar[TvarFind[kf]]][i]=%f\n",i,covar[Tvar[TvarFind[kf]]][i]); */
1.335 brouard 5965: 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 5966: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
5967: /* cov[2+6]=covar[Tvar[6]][i]; */
5968: /* cov[2+6]=covar[2][i]; V2 */
5969: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
5970: /* cov[2+7]=covar[Tvar[7]][i]; */
5971: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
5972: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
5973: /* cov[2+9]=covar[Tvar[9]][i]; */
5974: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 5975: }
1.336 brouard 5976: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
5977: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
5978: has been calculated etc */
5979: /* For an individual i, wav[i] gives the number of effective waves */
5980: /* We compute the contribution to Likelihood of each effective transition
5981: mw[mi][i] is real wave of the mi th effectve wave */
5982: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
5983: s2=s[mw[mi+1][i]][i];
1.341 brouard 5984: And the iv th varying covariate in the DATA is the cotvar[mw[mi+1][i]][ncovcol+nqv+iv][i]
1.336 brouard 5985: */
5986: /* This part may be useless now because everythin should be in covar */
1.232 brouard 5987: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
5988: /* 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?)*\/ */
5989: /* } */
1.231 brouard 5990: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
5991: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
5992: /* } */
1.225 brouard 5993:
1.233 brouard 5994:
5995: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.339 brouard 5996: /* 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 */
5997: /* for(k=1; k <= ncovv ; k++){ /\* Varying covariates (single and product but no age )*\/ */
5998: /* /\* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; *\/ */
5999: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; */
6000: /* } */
6001:
6002: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
6003: /* model V1+V3+age*V1+age*V3+V1*V3 */
6004: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
6005: /* TvarVV[1]=V3 (first time varying in the model equation, TvarVV[2]=V1 (in V1*V3) TvarVV[3]=3(V3) */
6006: /* We need the position of the time varying or product in the model */
6007: /* 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 */
6008: /* TvarVV gives the variable name */
1.340 brouard 6009: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
6010: * k= 1 2 3 4 5 6 7 8 9
6011: * varying 1 2 3 4 5
6012: * ncovv 1 2 3 4 5 6 7 8
1.343 brouard 6013: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
1.340 brouard 6014: * TvarVVind 2 3 7 7 8 8 9 9
6015: * TvarFind[k] 1 0 0 0 0 0 0 0 0
6016: */
1.345 brouard 6017: /* Other model ncovcol=5 nqv=0 ntv=3 nqtv=0 nlstate=3
1.349 brouard 6018: * 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 6019: * FixedV[ncovcol+qv+ntv+nqtv] V5
1.349 brouard 6020: * 3 V1 V2 V3 V4 V5 V6 V7 V8 V3*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6021: * 0 0 0 0 0 1 1 1 0, 0, 1,1, 1, 0, 1, 0, 1, 0, 1, 0}
6022: * 3 0 0 0 0 0 1 1 1 0, 1 1 1 1 1}
6023: * model= V2 + V3 + V4 + V6 + V7 + V6*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6024: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6025: * +age*V6*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6026: * model2= V2 + V3 + V4 + V6 + V7 + V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6027: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6028: * +age*V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6029: * model3= V2 + V3 + V4 + V6 + V7 + age*V3*V2 + V7*V2 + V6*V3 + V7*V3 + V6*V4 + V7*V4
6030: * +age*V2 +age*V3 +age*V4 +age*V6 + age*V7
6031: * +V3*V2 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6032: * kmodel 1 2 3 4 5 6 7 8 9 10 11
6033: * 12 13 14 15 16
6034: * 17 18 19 20 21
6035: * Tvar[kmodel] 2 3 4 6 7 9 10 11 12 13 14
6036: * 2 3 4 6 7
6037: * 9 11 12 13 14
6038: * cptcovage=5+5 total of covariates with age
6039: * Tage[cptcovage] age*V2=12 13 14 15 16
6040: *1 17 18 19 20 21 gives the position in model of covariates associated with age
6041: *3 Tage[cptcovage] age*V3*V2=6
6042: *3 age*V2=12 13 14 15 16
6043: *3 age*V6*V3=18 19 20 21
6044: * Tvar[Tage[cptcovage]] Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6045: * 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
6046: * 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
6047: * 3 Tvar[Tage[cptcovage]] Tvar[6]=9 Tvar[12]=2 3 4 6 Tvar[16]=7(age*V7)
6048: * 3 Tvar[18]age*V6*V3=11 age*V7*V3=12 age*V6*V4=13 Tvar[21]age*V7*V4=14
6049: * 3 Tage[cptcovage] age*V3*V2=6 age*V2=12 age*V3 13 14 15 16
6050: * age*V6*V3=18 19 20 21 gives the position in model of covariates associated with age
6051: * 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
6052: * Tvar= {2, 3, 4, 6, 7,
6053: * 9, 10, 11, 12, 13, 14,
6054: * Tvar[12]=2, 3, 4, 6, 7,
6055: * Tvar[17]=9, 11, 12, 13, 14}
6056: * Typevar[1]@21 = {0, 0, 0, 0, 0,
6057: * 2, 2, 2, 2, 2, 2,
6058: * 3 3, 2, 2, 2, 2, 2,
6059: * 1, 1, 1, 1, 1,
6060: * 3, 3, 3, 3, 3}
6061: * 3 2, 3, 3, 3, 3}
6062: * 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
6063: * p Tprod[1]@21 {6, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6064: * 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}
6065: * 3 Tprod[1]@21 {17, 7, 8, 9, 10, 11, 0 <repeats 15 times>}
6066: * cptcovprod=11 (6+5)
6067: * FixedV[Tvar[Tage[cptcovage]]]] FixedV[2]=0 FixedV[3]=0 0 1 (age*V7)Tvar[16]=1 FixedV[absolute] not [kmodel]
6068: * FixedV[Tvar[17]=FixedV[age*V6*V2]=FixedV[9]=1 1 1 1 1
6069: * 3 FixedV[Tvar[17]=FixedV[age*V3*V2]=FixedV[9]=0 [11]=1 1 1 1
6070: * FixedV[] V1=0 V2=0 V3=0 v4=0 V5=0 V6=1 V7=1 v8=1 OK then model dependent
6071: * 9=1 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6072: * 3 9=0 [V7*V2]=[10]=1 11=1 12=1 13=1 14=1
6073: * cptcovdageprod=5 for gnuplot printing
6074: * cptcovprodvage=6
6075: * ncova=15 1 2 3 4 5
6076: * 6 7 8 9 10 11 12 13 14 15
6077: * TvarA 2 3 4 6 7
6078: * 6 2 6 7 7 3 6 4 7 4
6079: * TvaAind 12 12 13 13 14 14 15 15 16 16
1.345 brouard 6080: * ncovf 1 2 3
1.349 brouard 6081: * V6 V7 V6*V2 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6082: * ncovvt=14 1 2 3 4 5 6 7 8 9 10 11 12 13 14
6083: * TvarVV[1]@14 = itv {V6=6, 7, V6*V2=6, 2, 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6084: * TvarVVind[1]@14= {4, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11}
6085: * 3 ncovvt=12 V6 V7 V7*V2 V6*V3 V7*V3 V6*V4 V7*V4
6086: * 3 TvarVV[1]@12 = itv {6, 7, V7*V2=7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
6087: * 3 1 2 3 4 5 6 7 8 9 10 11 12
6088: * TvarVVind[1]@12= {V6 is in k=4, 5, 7,(4isV2)=7, 8, 8, 9, 9, 10,10, 11,11}TvarVVind[12]=k=11
6089: * TvarV 6, 7, 9, 10, 11, 12, 13, 14
6090: * 3 cptcovprodvage=6
6091: * 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
6092: * 3 TvarAVVA[1]@15= itva 3 2 2 3 4 6 7 6 3 7 3 6 4 7 4
6093: * 3 ncovta 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
1.354 brouard 6094: *?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 6095: * TvarAVVAind[1]@15= V3 is in k=6 6 12 13 14 15 16 18 18 19,19, 20,20 21,21}TvarVVAind[]
6096: * 3 ncovvta=10 +age*V6 + age*V7 + age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4
6097: * 3 we want to compute =cotvar[mw[mi][i]][TvarVVA[ncovva]][i] at position TvarVVAind[ncovva]
6098: * 3 TvarVVA[1]@10= itva 6 7 6 3 7 3 6 4 7 4
6099: * 3 ncovva 1 2 3 4 5 6 7 8 9 10
6100: * TvarVVAind[1]@10= V6 is in k=4 5 8,8 9, 9, 10,10 11 11}TvarVVAind[]
6101: * TvarVVAind[1]@10= 15 16 18,18 19,19, 20,20 21 21}TvarVVAind[]
6102: * TvarVA V3*V2=6 6 , 1, 2, 11, 12, 13, 14
1.345 brouard 6103: * TvarFind[1]@14= {1, 2, 3, 0 <repeats 12 times>}
1.349 brouard 6104: * Tvar[1]@21= {2, 3, 4, 6, 7, 9, 10, 11, 12, 13, 14,
6105: * 2, 3, 4, 6, 7,
6106: * 6, 8, 9, 10, 11}
1.345 brouard 6107: * TvarFind[itv] 0 0 0
6108: * FixedV[itv] 1 1 1 0 1 0 1 0 1 0 0
1.354 brouard 6109: *? FixedV[itv] 1 1 1 0 1 0 1 0 1 0 1 0 1 0
1.345 brouard 6110: * Tvar[TvarFind[ncovf]]=[1]=2 [2]=3 [4]=4
6111: * Tvar[TvarFind[itv]] [0]=? ?ncovv 1 à ncovvt]
6112: * Not a fixed cotvar[mw][itv][i] 6 7 6 2 7, 2, 6, 3, 7, 3, 6, 4, 7, 4}
1.349 brouard 6113: * fixed covar[itv] [6] [7] [6][2]
1.345 brouard 6114: */
6115:
1.349 brouard 6116: 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 */
6117: 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 6118: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
1.345 brouard 6119: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6120: 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 6121: /* printf("DEBUG ncovv=%d, Varying TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
1.345 brouard 6122: cotvarv=cotvar[mw[mi][i]][TvarVV[ncovv]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
1.354 brouard 6123: /* printf("DEBUG Varying cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
1.340 brouard 6124: }else{ /* fixed covariate */
1.345 brouard 6125: /* 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 6126: /* printf("DEBUG ncovv=%d, Fixed TvarVV[ncovv]=%d\n",ncovv, TvarVV[ncovv]); */
1.349 brouard 6127: cotvarv=covar[itv][i]; /* Good: In V6*V3, 3 is fixed at position of the data */
1.354 brouard 6128: /* printf("DEBUG Fixed cov[ioffset+ipos=%d]=%g \n",ioffset+ipos,cotvarv); */
1.340 brouard 6129: }
1.339 brouard 6130: if(ipos!=iposold){ /* Not a product or first of a product */
1.340 brouard 6131: cotvarvold=cotvarv;
6132: }else{ /* A second product */
6133: cotvarv=cotvarv*cotvarvold;
1.339 brouard 6134: }
6135: iposold=ipos;
1.340 brouard 6136: cov[ioffset+ipos]=cotvarv;
1.354 brouard 6137: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
1.339 brouard 6138: /* For products */
6139: }
6140: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates single *\/ */
6141: /* iv=TvarVDind[itv]; /\* iv, position in the model equation of time varying covariate itv *\/ */
6142: /* /\* "V1+V3+age*V1+age*V3+V1*V3" with V3 time varying *\/ */
6143: /* /\* 1 2 3 4 5 *\/ */
6144: /* /\*itv 1 *\/ */
6145: /* /\* TvarVInd[1]= 2 *\/ */
6146: /* /\* iv= Tvar[Tmodelind[itv]]-ncovcol-nqv; /\\* Counting the # varying covariate from 1 to ntveff *\\/ *\/ */
6147: /* /\* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; *\/ */
6148: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; *\/ */
6149: /* /\* k=ioffset-2-nagesqr-cptcovage+itv; /\\* position in simple model *\\/ *\/ */
6150: /* /\* cov[ioffset+iv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; *\/ */
6151: /* cov[ioffset+iv]=cotvar[mw[mi][i]][itv][i]; */
6152: /* /\* 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]); *\/ */
6153: /* } */
1.232 brouard 6154: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 6155: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
6156: /* /\* 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]); *\/ */
6157: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 6158: /* } */
1.126 brouard 6159: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 6160: for (j=1;j<=nlstate+ndeath;j++){
6161: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
6162: savm[ii][j]=(ii==j ? 1.0 : 0.0);
6163: }
1.214 brouard 6164:
6165: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
6166: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
6167: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.247 brouard 6168: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
1.242 brouard 6169: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
6170: and mw[mi+1][i]. dh depends on stepm.*/
6171: newm=savm;
1.247 brouard 6172: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
1.242 brouard 6173: cov[2]=agexact;
6174: if(nagesqr==1)
6175: cov[3]= agexact*agexact;
1.349 brouard 6176: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6177: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6178: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6179: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6180: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6181: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6182: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6183: }else{ /* fixed covariate */
6184: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6185: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6186: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6187: }
6188: if(ipos!=iposold){ /* Not a product or first of a product */
6189: cotvarvold=cotvarv;
6190: }else{ /* A second product */
6191: /* printf("DEBUG * \n"); */
6192: cotvarv=cotvarv*cotvarvold;
6193: }
6194: iposold=ipos;
6195: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6196: cov[ioffset+ipos]=cotvarv*agexact;
6197: /* For products */
1.242 brouard 6198: }
1.349 brouard 6199:
1.242 brouard 6200: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
6201: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
6202: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
6203: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
6204: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
6205: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
6206: savm=oldm;
6207: oldm=newm;
1.126 brouard 6208: } /* end mult */
1.336 brouard 6209: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
6210: /* But now since version 0.9 we anticipate for bias at large stepm.
6211: * If stepm is larger than one month (smallest stepm) and if the exact delay
6212: * (in months) between two waves is not a multiple of stepm, we rounded to
6213: * the nearest (and in case of equal distance, to the lowest) interval but now
6214: * we keep into memory the bias bh[mi][i] and also the previous matrix product
6215: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
6216: * probability in order to take into account the bias as a fraction of the way
6217: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
6218: * -stepm/2 to stepm/2 .
6219: * For stepm=1 the results are the same as for previous versions of Imach.
6220: * For stepm > 1 the results are less biased than in previous versions.
6221: */
1.126 brouard 6222: s1=s[mw[mi][i]][i];
6223: s2=s[mw[mi+1][i]][i];
1.217 brouard 6224: /* if(s2==-1){ */
1.268 brouard 6225: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
1.217 brouard 6226: /* /\* exit(1); *\/ */
6227: /* } */
1.126 brouard 6228: bbh=(double)bh[mi][i]/(double)stepm;
6229: /* bias is positive if real duration
6230: * is higher than the multiple of stepm and negative otherwise.
6231: */
6232: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 6233: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 6234: } else if ( s2==-1 ) { /* alive */
1.242 brouard 6235: for (j=1,survp=0. ; j<=nlstate; j++)
6236: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
6237: lli= log(survp);
1.126 brouard 6238: }else if (mle==1){
1.242 brouard 6239: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 6240: } else if(mle==2){
1.242 brouard 6241: 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 6242: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 6243: 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 6244: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 6245: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 6246: } else{ /* mle=0 back to 1 */
1.242 brouard 6247: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
6248: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 6249: } /* End of if */
6250: ipmx +=1;
6251: sw += weight[i];
6252: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.342 brouard 6253: /* Printing covariates values for each contribution for checking */
1.343 brouard 6254: /* 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 6255: if(globpr){
1.246 brouard 6256: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 6257: %11.6f %11.6f %11.6f ", \
1.242 brouard 6258: 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 6259: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
1.343 brouard 6260: /* printf("%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ */
6261: /* %11.6f %11.6f %11.6f ", \ */
6262: /* num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, */
6263: /* 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); */
1.242 brouard 6264: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
6265: llt +=ll[k]*gipmx/gsw;
6266: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
1.335 brouard 6267: /* printf(" %10.6f",-ll[k]*gipmx/gsw); */
1.242 brouard 6268: }
1.343 brouard 6269: fprintf(ficresilk," %10.6f ", -llt);
1.335 brouard 6270: /* printf(" %10.6f\n", -llt); */
1.342 brouard 6271: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
1.343 brouard 6272: /* fprintf(ficresilk,"%09ld ", num[i]); */ /* not necessary */
6273: for (kf=1; kf<=ncovf;kf++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
6274: fprintf(ficresilk," %g",covar[Tvar[TvarFind[kf]]][i]);
6275: }
6276: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Varying covariates (single and product but no age) including individual from products */
6277: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6278: if(ipos!=iposold){ /* Not a product or first of a product */
6279: fprintf(ficresilk," %g",cov[ioffset+ipos]);
6280: /* printf(" %g",cov[ioffset+ipos]); */
6281: }else{
6282: fprintf(ficresilk,"*");
6283: /* printf("*"); */
1.342 brouard 6284: }
1.343 brouard 6285: iposold=ipos;
6286: }
1.349 brouard 6287: /* for (kk=1; kk<=cptcovage;kk++) { */
6288: /* if(!FixedV[Tvar[Tage[kk]]]){ */
6289: /* fprintf(ficresilk," %g*age",covar[Tvar[Tage[kk]]][i]); */
6290: /* /\* printf(" %g*age",covar[Tvar[Tage[kk]]][i]); *\/ */
6291: /* }else{ */
6292: /* fprintf(ficresilk," %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6293: /* /\* printf(" %g*age",cotvar[mw[mi][i]][Tvar[Tage[kk]]][i]);/\\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\\/ *\/ */
6294: /* } */
6295: /* } */
6296: for(ncovva=1, iposold=0; ncovva <= ncovta ; ncovva++){ /* Time varying covariates with age including individual from products, product is computed dynamically */
6297: itv=TvarAVVA[ncovva]; /* TvarVV={3, 1, 3} gives the name of each varying covariate, exploding product Vn*Vm into Vn and then Vm */
6298: ipos=TvarAVVAind[ncovva]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate*/
6299: /* if(TvarFind[itv]==0){ /\* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv *\/ */
6300: if(FixedV[itv]!=0){ /* Not a fixed covariate? Could be a fixed covariate of a product with a higher than ncovcol+nqv, itv */
6301: /* printf("DEBUG ncovva=%d, Varying TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6302: cotvarv=cotvar[mw[mi][i]][TvarAVVA[ncovva]][i]; /* because cotvar starts now at first ncovcol+nqv+ntv+nqtv (1 to nqtv) */
6303: }else{ /* fixed covariate */
6304: /* cotvarv=covar[Tvar[TvarFind[itv]]][i]; /\* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model *\/ */
6305: /* printf("DEBUG ncovva=%d, Fixed TvarAVVA[ncovva]=%d\n", ncovva, TvarAVVA[ncovva]); */
6306: cotvarv=covar[itv][i]; /* Error: TvarFind gives the name, that is the true column of fixed covariates, but Tvar of the model */
6307: }
6308: if(ipos!=iposold){ /* Not a product or first of a product */
6309: cotvarvold=cotvarv;
6310: }else{ /* A second product */
6311: /* printf("DEBUG * \n"); */
6312: cotvarv=cotvarv*cotvarvold;
1.342 brouard 6313: }
1.349 brouard 6314: cotvarv=cotvarv*agexact;
6315: fprintf(ficresilk," %g*age",cotvarv);
6316: iposold=ipos;
6317: /* printf("DEBUG Product cov[ioffset+ipos=%d] \n",ioffset+ipos); */
6318: cov[ioffset+ipos]=cotvarv;
6319: /* For products */
1.343 brouard 6320: }
6321: /* printf("\n"); */
1.342 brouard 6322: /* } /\* End debugILK *\/ */
6323: fprintf(ficresilk,"\n");
6324: } /* End if globpr */
1.335 brouard 6325: } /* end of wave */
6326: } /* end of individual */
6327: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
1.232 brouard 6328: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
1.335 brouard 6329: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
6330: if(globpr==0){ /* First time we count the contributions and weights */
6331: gipmx=ipmx;
6332: gsw=sw;
6333: }
1.343 brouard 6334: return -l;
1.126 brouard 6335: }
6336:
6337:
6338: /*************** function likelione ***********/
1.292 brouard 6339: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
1.126 brouard 6340: {
6341: /* This routine should help understanding what is done with
6342: the selection of individuals/waves and
6343: to check the exact contribution to the likelihood.
6344: Plotting could be done.
1.342 brouard 6345: */
6346: void pstamp(FILE *ficres);
1.343 brouard 6347: int k, kf, kk, kvar, ncovv, iposold, ipos;
1.126 brouard 6348:
6349: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 6350: strcpy(fileresilk,"ILK_");
1.202 brouard 6351: strcat(fileresilk,fileresu);
1.126 brouard 6352: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
6353: printf("Problem with resultfile: %s\n", fileresilk);
6354: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
6355: }
1.342 brouard 6356: pstamp(ficresilk);fprintf(ficresilk,"# model=1+age+%s\n",model);
1.214 brouard 6357: 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");
6358: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 6359: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
6360: for(k=1; k<=nlstate; k++)
6361: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
1.342 brouard 6362: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total) ");
6363:
6364: /* if(debugILK){ /\* debugILK is set by a #d in a comment line *\/ */
6365: for(kf=1;kf <= ncovf; kf++){
6366: fprintf(ficresilk,"V%d",Tvar[TvarFind[kf]]);
6367: /* printf("V%d",Tvar[TvarFind[kf]]); */
6368: }
6369: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){
1.343 brouard 6370: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
1.342 brouard 6371: if(ipos!=iposold){ /* Not a product or first of a product */
6372: /* printf(" %d",ipos); */
6373: fprintf(ficresilk," V%d",TvarVV[ncovv]);
6374: }else{
6375: /* printf("*"); */
6376: fprintf(ficresilk,"*");
1.343 brouard 6377: }
1.342 brouard 6378: iposold=ipos;
6379: }
6380: for (kk=1; kk<=cptcovage;kk++) {
6381: if(!FixedV[Tvar[Tage[kk]]]){
6382: /* printf(" %d*age(Fixed)",Tvar[Tage[kk]]); */
6383: fprintf(ficresilk," %d*age(Fixed)",Tvar[Tage[kk]]);
6384: }else{
6385: fprintf(ficresilk," %d*age(Varying)",Tvar[Tage[kk]]);/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
6386: /* printf(" %d*age(Varying)",Tvar[Tage[kk]]);/\* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) *\/ */
6387: }
6388: }
6389: /* } /\* End if debugILK *\/ */
6390: /* printf("\n"); */
6391: fprintf(ficresilk,"\n");
6392: } /* End glogpri */
1.126 brouard 6393:
1.292 brouard 6394: *fretone=(*func)(p);
1.126 brouard 6395: if(*globpri !=0){
6396: fclose(ficresilk);
1.205 brouard 6397: if (mle ==0)
6398: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
6399: else if(mle >=1)
6400: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
6401: 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 6402: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
1.208 brouard 6403:
1.207 brouard 6404: 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 6405: <img src=\"%s-ori.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 6406: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.343 brouard 6407: <img src=\"%s-dest.png\">\n",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
6408:
6409: for (k=1; k<= nlstate ; k++) {
6410: 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 \
6411: <img src=\"%s-p%dj.png\">\n",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
6412: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
1.350 brouard 6413: kvar=Tvar[TvarFind[kf]]; /* variable */
6414: 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]]);
6415: 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);
6416: fprintf(fichtm,"<img src=\"%s-p%dj-%d.png\">",subdirf2(optionfilefiname,"ILK_"),k,Tvar[TvarFind[kf]]);
1.343 brouard 6417: }
6418: for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /* Loop on the time varying extended covariates (with extension of Vn*Vm */
6419: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
6420: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
6421: /* 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]); */
6422: if(ipos!=iposold){ /* Not a product or first of a product */
6423: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
6424: /* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); */
6425: 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) */
6426: 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> \
6427: <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);
6428: } /* End only for dummies time varying (single?) */
6429: }else{ /* Useless product */
6430: /* printf("*"); */
6431: /* fprintf(ficresilk,"*"); */
6432: }
6433: iposold=ipos;
6434: } /* For each time varying covariate */
6435: } /* End loop on states */
6436:
6437: /* if(debugILK){ */
6438: /* for(kf=1; kf <= ncovf; kf++){ /\* For each simple dummy covariate of the model *\/ */
6439: /* /\* kvar=Tvar[TvarFind[kf]]; *\/ /\* variable *\/ */
6440: /* for (k=1; k<= nlstate ; k++) { */
6441: /* 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> \ */
6442: /* <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]]); */
6443: /* } */
6444: /* } */
6445: /* for(ncovv=1, iposold=0; ncovv <= ncovvt ; ncovv++){ /\* Loop on the time varying extended covariates (with extension of Vn*Vm *\/ */
6446: /* ipos=TvarVVind[ncovv]; /\* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate *\/ */
6447: /* kvar=TvarVV[ncovv]; /\* TvarVV={3, 1, 3} gives the name of each varying covariate *\/ */
6448: /* /\* 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]); *\/ */
6449: /* if(ipos!=iposold){ /\* Not a product or first of a product *\/ */
6450: /* /\* fprintf(ficresilk," V%d",TvarVV[ncovv]); *\/ */
6451: /* /\* printf(" DebugILK fichtm ipos=%d != iposold=%d\n", ipos, iposold); *\/ */
6452: /* 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) *\/ */
6453: /* for (k=1; k<= nlstate ; k++) { */
6454: /* 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> \ */
6455: /* <img src=\"%s-p%dj-%d.png\">",k,k,kvar,kvar,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,kvar); */
6456: /* } /\* End state *\/ */
6457: /* } /\* End only for dummies time varying (single?) *\/ */
6458: /* }else{ /\* Useless product *\/ */
6459: /* /\* printf("*"); *\/ */
6460: /* /\* fprintf(ficresilk,"*"); *\/ */
6461: /* } */
6462: /* iposold=ipos; */
6463: /* } /\* For each time varying covariate *\/ */
6464: /* }/\* End debugILK *\/ */
1.207 brouard 6465: fflush(fichtm);
1.343 brouard 6466: }/* End globpri */
1.126 brouard 6467: return;
6468: }
6469:
6470:
6471: /*********** Maximum Likelihood Estimation ***************/
6472:
6473: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
6474: {
1.359 ! brouard 6475: int i,j, jkk=0, iter=0;
1.126 brouard 6476: double **xi;
1.359 ! brouard 6477: /*double fret;*/
! 6478: /*double fretone;*/ /* Only one call to likelihood */
1.126 brouard 6479: /* char filerespow[FILENAMELENGTH];*/
1.354 brouard 6480:
1.359 ! brouard 6481: /*double * p1;*/ /* Shifted parameters from 0 instead of 1 */
1.162 brouard 6482: #ifdef NLOPT
6483: int creturn;
6484: nlopt_opt opt;
6485: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
6486: double *lb;
6487: double minf; /* the minimum objective value, upon return */
1.354 brouard 6488:
1.162 brouard 6489: myfunc_data dinst, *d = &dinst;
6490: #endif
6491:
6492:
1.126 brouard 6493: xi=matrix(1,npar,1,npar);
1.357 brouard 6494: for (i=1;i<=npar;i++) /* Starting with canonical directions j=1,n xi[i=1,n][j] */
1.126 brouard 6495: for (j=1;j<=npar;j++)
6496: xi[i][j]=(i==j ? 1.0 : 0.0);
1.359 ! brouard 6497: printf("Powell-prax\n"); fprintf(ficlog,"Powell-prax\n");
1.201 brouard 6498: strcpy(filerespow,"POW_");
1.126 brouard 6499: strcat(filerespow,fileres);
6500: if((ficrespow=fopen(filerespow,"w"))==NULL) {
6501: printf("Problem with resultfile: %s\n", filerespow);
6502: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
6503: }
6504: fprintf(ficrespow,"# Powell\n# iter -2*LL");
6505: for (i=1;i<=nlstate;i++)
6506: for(j=1;j<=nlstate+ndeath;j++)
6507: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
6508: fprintf(ficrespow,"\n");
1.162 brouard 6509: #ifdef POWELL
1.319 brouard 6510: #ifdef LINMINORIGINAL
6511: #else /* LINMINORIGINAL */
6512:
6513: flatdir=ivector(1,npar);
6514: for (j=1;j<=npar;j++) flatdir[j]=0;
6515: #endif /*LINMINORIGINAL */
6516:
6517: #ifdef FLATSUP
6518: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6519: /* reorganizing p by suppressing flat directions */
6520: for(i=1, jk=1; i <=nlstate; i++){
6521: for(k=1; k <=(nlstate+ndeath); k++){
6522: if (k != i) {
6523: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6524: if(flatdir[jk]==1){
6525: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
6526: }
6527: for(j=1; j <=ncovmodel; j++){
6528: printf("%12.7f ",p[jk]);
6529: jk++;
6530: }
6531: printf("\n");
6532: }
6533: }
6534: }
6535: /* skipping */
6536: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
6537: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
6538: for(k=1; k <=(nlstate+ndeath); k++){
6539: if (k != i) {
6540: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
6541: if(flatdir[jk]==1){
6542: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
6543: for(j=1; j <=ncovmodel; jk++,j++){
6544: printf(" p[%d]=%12.7f",jk, p[jk]);
6545: /*q[jjk]=p[jk];*/
6546: }
6547: }else{
6548: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
6549: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
6550: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
6551: /*q[jjk]=p[jk];*/
6552: }
6553: }
6554: printf("\n");
6555: }
6556: fflush(stdout);
6557: }
6558: }
6559: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
6560: #else /* FLATSUP */
1.359 ! brouard 6561: /* powell(p,xi,npar,ftol,&iter,&fret,func);*/
! 6562: /* praxis ( t0, h0, n, prin, x, beale_f ); */
! 6563: int prin=1;
! 6564: double h0=0.25;
! 6565: double macheps;
! 6566: double fmin;
! 6567: macheps=pow(16.0,-13.0);
! 6568: /* #include "praxis.h" */
! 6569: /* Be careful that praxis start at x[0] and powell start at p[1] */
! 6570: /* praxis ( ftol, h0, npar, prin, p, func ); */
! 6571: /* p1= (p+1); */ /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
! 6572: printf("Praxis Gegenfurtner \n");
! 6573: fprintf(ficlog, "Praxis Gegenfurtner\n");fflush(ficlog);
! 6574: /* praxis ( ftol, h0, npar, prin, p1, func ); */
! 6575: /* fmin = praxis(1.e-5,macheps, h, n, prin, x, func); */
! 6576: fmin = praxis(ftol,macheps, h0, npar, prin, p, func);
! 6577: printf("End Praxis\n");
1.319 brouard 6578: #endif /* FLATSUP */
6579:
6580: #ifdef LINMINORIGINAL
6581: #else
6582: free_ivector(flatdir,1,npar);
6583: #endif /* LINMINORIGINAL*/
6584: #endif /* POWELL */
1.126 brouard 6585:
1.162 brouard 6586: #ifdef NLOPT
6587: #ifdef NEWUOA
6588: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
6589: #else
6590: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
6591: #endif
6592: lb=vector(0,npar-1);
6593: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
6594: nlopt_set_lower_bounds(opt, lb);
6595: nlopt_set_initial_step1(opt, 0.1);
6596:
6597: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
6598: d->function = func;
6599: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
6600: nlopt_set_min_objective(opt, myfunc, d);
6601: nlopt_set_xtol_rel(opt, ftol);
6602: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
6603: printf("nlopt failed! %d\n",creturn);
6604: }
6605: else {
6606: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
6607: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
6608: iter=1; /* not equal */
6609: }
6610: nlopt_destroy(opt);
6611: #endif
1.319 brouard 6612: #ifdef FLATSUP
6613: /* npared = npar -flatd/ncovmodel; */
6614: /* xired= matrix(1,npared,1,npared); */
6615: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
6616: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
6617: /* free_matrix(xire,1,npared,1,npared); */
6618: #else /* FLATSUP */
6619: #endif /* FLATSUP */
1.126 brouard 6620: free_matrix(xi,1,npar,1,npar);
6621: fclose(ficrespow);
1.203 brouard 6622: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
6623: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 6624: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 6625:
6626: }
6627:
6628: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 6629: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 6630: {
6631: double **a,**y,*x,pd;
1.203 brouard 6632: /* double **hess; */
1.164 brouard 6633: int i, j;
1.126 brouard 6634: int *indx;
6635:
6636: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 6637: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 6638: void lubksb(double **a, int npar, int *indx, double b[]) ;
6639: void ludcmp(double **a, int npar, int *indx, double *d) ;
6640: double gompertz(double p[]);
1.203 brouard 6641: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 6642:
6643: printf("\nCalculation of the hessian matrix. Wait...\n");
6644: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
6645: for (i=1;i<=npar;i++){
1.203 brouard 6646: printf("%d-",i);fflush(stdout);
6647: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 6648:
6649: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
6650:
6651: /* printf(" %f ",p[i]);
6652: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
6653: }
6654:
6655: for (i=1;i<=npar;i++) {
6656: for (j=1;j<=npar;j++) {
6657: if (j>i) {
1.203 brouard 6658: printf(".%d-%d",i,j);fflush(stdout);
6659: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
6660: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 6661:
6662: hess[j][i]=hess[i][j];
6663: /*printf(" %lf ",hess[i][j]);*/
6664: }
6665: }
6666: }
6667: printf("\n");
6668: fprintf(ficlog,"\n");
6669:
6670: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
6671: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
6672:
6673: a=matrix(1,npar,1,npar);
6674: y=matrix(1,npar,1,npar);
6675: x=vector(1,npar);
6676: indx=ivector(1,npar);
6677: for (i=1;i<=npar;i++)
6678: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
6679: ludcmp(a,npar,indx,&pd);
6680:
6681: for (j=1;j<=npar;j++) {
6682: for (i=1;i<=npar;i++) x[i]=0;
6683: x[j]=1;
6684: lubksb(a,npar,indx,x);
6685: for (i=1;i<=npar;i++){
6686: matcov[i][j]=x[i];
6687: }
6688: }
6689:
6690: printf("\n#Hessian matrix#\n");
6691: fprintf(ficlog,"\n#Hessian matrix#\n");
6692: for (i=1;i<=npar;i++) {
6693: for (j=1;j<=npar;j++) {
1.203 brouard 6694: printf("%.6e ",hess[i][j]);
6695: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 6696: }
6697: printf("\n");
6698: fprintf(ficlog,"\n");
6699: }
6700:
1.203 brouard 6701: /* printf("\n#Covariance matrix#\n"); */
6702: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
6703: /* for (i=1;i<=npar;i++) { */
6704: /* for (j=1;j<=npar;j++) { */
6705: /* printf("%.6e ",matcov[i][j]); */
6706: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
6707: /* } */
6708: /* printf("\n"); */
6709: /* fprintf(ficlog,"\n"); */
6710: /* } */
6711:
1.126 brouard 6712: /* Recompute Inverse */
1.203 brouard 6713: /* for (i=1;i<=npar;i++) */
6714: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
6715: /* ludcmp(a,npar,indx,&pd); */
6716:
6717: /* printf("\n#Hessian matrix recomputed#\n"); */
6718:
6719: /* for (j=1;j<=npar;j++) { */
6720: /* for (i=1;i<=npar;i++) x[i]=0; */
6721: /* x[j]=1; */
6722: /* lubksb(a,npar,indx,x); */
6723: /* for (i=1;i<=npar;i++){ */
6724: /* y[i][j]=x[i]; */
6725: /* printf("%.3e ",y[i][j]); */
6726: /* fprintf(ficlog,"%.3e ",y[i][j]); */
6727: /* } */
6728: /* printf("\n"); */
6729: /* fprintf(ficlog,"\n"); */
6730: /* } */
6731:
6732: /* Verifying the inverse matrix */
6733: #ifdef DEBUGHESS
6734: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 6735:
1.203 brouard 6736: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
6737: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 6738:
6739: for (j=1;j<=npar;j++) {
6740: for (i=1;i<=npar;i++){
1.203 brouard 6741: printf("%.2f ",y[i][j]);
6742: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 6743: }
6744: printf("\n");
6745: fprintf(ficlog,"\n");
6746: }
1.203 brouard 6747: #endif
1.126 brouard 6748:
6749: free_matrix(a,1,npar,1,npar);
6750: free_matrix(y,1,npar,1,npar);
6751: free_vector(x,1,npar);
6752: free_ivector(indx,1,npar);
1.203 brouard 6753: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 6754:
6755:
6756: }
6757:
6758: /*************** hessian matrix ****************/
6759: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 6760: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 6761: int i;
6762: int l=1, lmax=20;
1.203 brouard 6763: double k1,k2, res, fx;
1.132 brouard 6764: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 6765: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
6766: int k=0,kmax=10;
6767: double l1;
6768:
6769: fx=func(x);
6770: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 6771: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 6772: l1=pow(10,l);
6773: delts=delt;
6774: for(k=1 ; k <kmax; k=k+1){
6775: delt = delta*(l1*k);
6776: p2[theta]=x[theta] +delt;
1.145 brouard 6777: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 6778: p2[theta]=x[theta]-delt;
6779: k2=func(p2)-fx;
6780: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 6781: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 6782:
1.203 brouard 6783: #ifdef DEBUGHESSII
1.126 brouard 6784: 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);
6785: 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);
6786: #endif
6787: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
6788: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
6789: k=kmax;
6790: }
6791: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 6792: k=kmax; l=lmax*10;
1.126 brouard 6793: }
6794: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
6795: delts=delt;
6796: }
1.203 brouard 6797: } /* End loop k */
1.126 brouard 6798: }
6799: delti[theta]=delts;
6800: return res;
6801:
6802: }
6803:
1.203 brouard 6804: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 6805: {
6806: int i;
1.164 brouard 6807: int l=1, lmax=20;
1.126 brouard 6808: double k1,k2,k3,k4,res,fx;
1.132 brouard 6809: double p2[MAXPARM+1];
1.203 brouard 6810: int k, kmax=1;
6811: double v1, v2, cv12, lc1, lc2;
1.208 brouard 6812:
6813: int firstime=0;
1.203 brouard 6814:
1.126 brouard 6815: fx=func(x);
1.203 brouard 6816: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 6817: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 6818: p2[thetai]=x[thetai]+delti[thetai]*k;
6819: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 6820: k1=func(p2)-fx;
6821:
1.203 brouard 6822: p2[thetai]=x[thetai]+delti[thetai]*k;
6823: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 6824: k2=func(p2)-fx;
6825:
1.203 brouard 6826: p2[thetai]=x[thetai]-delti[thetai]*k;
6827: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 6828: k3=func(p2)-fx;
6829:
1.203 brouard 6830: p2[thetai]=x[thetai]-delti[thetai]*k;
6831: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 6832: k4=func(p2)-fx;
1.203 brouard 6833: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
6834: if(k1*k2*k3*k4 <0.){
1.208 brouard 6835: firstime=1;
1.203 brouard 6836: kmax=kmax+10;
1.208 brouard 6837: }
6838: if(kmax >=10 || firstime ==1){
1.354 brouard 6839: /* What are the thetai and thetaj? thetai/ncovmodel thetai=(thetai-thetai%ncovmodel)/ncovmodel +thetai%ncovmodel=(line,pos) */
1.246 brouard 6840: 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);
6841: 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 6842: 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);
6843: 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);
6844: }
6845: #ifdef DEBUGHESSIJ
6846: v1=hess[thetai][thetai];
6847: v2=hess[thetaj][thetaj];
6848: cv12=res;
6849: /* Computing eigen value of Hessian matrix */
6850: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6851: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6852: if ((lc2 <0) || (lc1 <0) ){
6853: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6854: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
6855: 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);
6856: 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);
6857: }
1.126 brouard 6858: #endif
6859: }
6860: return res;
6861: }
6862:
1.203 brouard 6863: /* Not done yet: Was supposed to fix if not exactly at the maximum */
6864: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
6865: /* { */
6866: /* int i; */
6867: /* int l=1, lmax=20; */
6868: /* double k1,k2,k3,k4,res,fx; */
6869: /* double p2[MAXPARM+1]; */
6870: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
6871: /* int k=0,kmax=10; */
6872: /* double l1; */
6873:
6874: /* fx=func(x); */
6875: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
6876: /* l1=pow(10,l); */
6877: /* delts=delt; */
6878: /* for(k=1 ; k <kmax; k=k+1){ */
6879: /* delt = delti*(l1*k); */
6880: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
6881: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6882: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6883: /* k1=func(p2)-fx; */
6884:
6885: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
6886: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6887: /* k2=func(p2)-fx; */
6888:
6889: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6890: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
6891: /* k3=func(p2)-fx; */
6892:
6893: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
6894: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
6895: /* k4=func(p2)-fx; */
6896: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
6897: /* #ifdef DEBUGHESSIJ */
6898: /* 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); */
6899: /* 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); */
6900: /* #endif */
6901: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
6902: /* k=kmax; */
6903: /* } */
6904: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
6905: /* k=kmax; l=lmax*10; */
6906: /* } */
6907: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
6908: /* delts=delt; */
6909: /* } */
6910: /* } /\* End loop k *\/ */
6911: /* } */
6912: /* delti[theta]=delts; */
6913: /* return res; */
6914: /* } */
6915:
6916:
1.126 brouard 6917: /************** Inverse of matrix **************/
6918: void ludcmp(double **a, int n, int *indx, double *d)
6919: {
6920: int i,imax,j,k;
6921: double big,dum,sum,temp;
6922: double *vv;
6923:
6924: vv=vector(1,n);
6925: *d=1.0;
6926: for (i=1;i<=n;i++) {
6927: big=0.0;
6928: for (j=1;j<=n;j++)
6929: if ((temp=fabs(a[i][j])) > big) big=temp;
1.256 brouard 6930: if (big == 0.0){
6931: printf(" Singular Hessian matrix at row %d:\n",i);
6932: for (j=1;j<=n;j++) {
6933: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
6934: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
6935: }
6936: fflush(ficlog);
6937: fclose(ficlog);
6938: nrerror("Singular matrix in routine ludcmp");
6939: }
1.126 brouard 6940: vv[i]=1.0/big;
6941: }
6942: for (j=1;j<=n;j++) {
6943: for (i=1;i<j;i++) {
6944: sum=a[i][j];
6945: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
6946: a[i][j]=sum;
6947: }
6948: big=0.0;
6949: for (i=j;i<=n;i++) {
6950: sum=a[i][j];
6951: for (k=1;k<j;k++)
6952: sum -= a[i][k]*a[k][j];
6953: a[i][j]=sum;
6954: if ( (dum=vv[i]*fabs(sum)) >= big) {
6955: big=dum;
6956: imax=i;
6957: }
6958: }
6959: if (j != imax) {
6960: for (k=1;k<=n;k++) {
6961: dum=a[imax][k];
6962: a[imax][k]=a[j][k];
6963: a[j][k]=dum;
6964: }
6965: *d = -(*d);
6966: vv[imax]=vv[j];
6967: }
6968: indx[j]=imax;
6969: if (a[j][j] == 0.0) a[j][j]=TINY;
6970: if (j != n) {
6971: dum=1.0/(a[j][j]);
6972: for (i=j+1;i<=n;i++) a[i][j] *= dum;
6973: }
6974: }
6975: free_vector(vv,1,n); /* Doesn't work */
6976: ;
6977: }
6978:
6979: void lubksb(double **a, int n, int *indx, double b[])
6980: {
6981: int i,ii=0,ip,j;
6982: double sum;
6983:
6984: for (i=1;i<=n;i++) {
6985: ip=indx[i];
6986: sum=b[ip];
6987: b[ip]=b[i];
6988: if (ii)
6989: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
6990: else if (sum) ii=i;
6991: b[i]=sum;
6992: }
6993: for (i=n;i>=1;i--) {
6994: sum=b[i];
6995: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
6996: b[i]=sum/a[i][i];
6997: }
6998: }
6999:
7000: void pstamp(FILE *fichier)
7001: {
1.196 brouard 7002: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 7003: }
7004:
1.297 brouard 7005: void date2dmy(double date,double *day, double *month, double *year){
7006: double yp=0., yp1=0., yp2=0.;
7007:
7008: yp1=modf(date,&yp);/* extracts integral of date in yp and
7009: fractional in yp1 */
7010: *year=yp;
7011: yp2=modf((yp1*12),&yp);
7012: *month=yp;
7013: yp1=modf((yp2*30.5),&yp);
7014: *day=yp;
7015: if(*day==0) *day=1;
7016: if(*month==0) *month=1;
7017: }
7018:
1.253 brouard 7019:
7020:
1.126 brouard 7021: /************ Frequencies ********************/
1.251 brouard 7022: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
1.226 brouard 7023: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
7024: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.250 brouard 7025: { /* Some frequencies as well as proposing some starting values */
1.332 brouard 7026: /* Frequencies of any combination of dummy covariate used in the model equation */
1.265 brouard 7027: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
1.226 brouard 7028: int iind=0, iage=0;
7029: int mi; /* Effective wave */
7030: int first;
7031: double ***freq; /* Frequencies */
1.268 brouard 7032: 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 */
7033: 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 7034: double *meanq, *stdq, *idq;
1.226 brouard 7035: double **meanqt;
7036: double *pp, **prop, *posprop, *pospropt;
7037: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
7038: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
7039: double agebegin, ageend;
7040:
7041: pp=vector(1,nlstate);
1.251 brouard 7042: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 7043: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
7044: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
7045: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
7046: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.284 brouard 7047: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.283 brouard 7048: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.226 brouard 7049: meanqt=matrix(1,lastpass,1,nqtveff);
7050: strcpy(fileresp,"P_");
7051: strcat(fileresp,fileresu);
7052: /*strcat(fileresphtm,fileresu);*/
7053: if((ficresp=fopen(fileresp,"w"))==NULL) {
7054: printf("Problem with prevalence resultfile: %s\n", fileresp);
7055: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
7056: exit(0);
7057: }
1.240 brouard 7058:
1.226 brouard 7059: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
7060: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
7061: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7062: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
7063: fflush(ficlog);
7064: exit(70);
7065: }
7066: else{
7067: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 7068: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 7069: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 7070: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7071: }
1.319 brouard 7072: 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 7073:
1.226 brouard 7074: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
7075: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
7076: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7077: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
7078: fflush(ficlog);
7079: exit(70);
1.240 brouard 7080: } else{
1.226 brouard 7081: 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 7082: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 7083: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 7084: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
7085: }
1.319 brouard 7086: 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 7087:
1.253 brouard 7088: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
7089: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.251 brouard 7090: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 7091: j1=0;
1.126 brouard 7092:
1.227 brouard 7093: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
1.335 brouard 7094: j=cptcoveff; /* Only simple dummy covariates used in the model */
1.330 brouard 7095: /* j=cptcovn; /\* Only dummy covariates of the model *\/ */
1.226 brouard 7096: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 7097:
7098:
1.226 brouard 7099: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
7100: reference=low_education V1=0,V2=0
7101: med_educ V1=1 V2=0,
7102: high_educ V1=0 V2=1
1.330 brouard 7103: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcovn
1.226 brouard 7104: */
1.249 brouard 7105: dateintsum=0;
7106: k2cpt=0;
7107:
1.253 brouard 7108: if(cptcoveff == 0 )
1.265 brouard 7109: nl=1; /* Constant and age model only */
1.253 brouard 7110: else
7111: nl=2;
1.265 brouard 7112:
7113: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
7114: /* Loop on nj=1 or 2 if dummy covariates j!=0
1.335 brouard 7115: * Loop on j1(1 to 2**cptcoveff) covariate combination
1.265 brouard 7116: * freq[s1][s2][iage] =0.
7117: * Loop on iind
7118: * ++freq[s1][s2][iage] weighted
7119: * end iind
7120: * if covariate and j!0
7121: * headers Variable on one line
7122: * endif cov j!=0
7123: * header of frequency table by age
7124: * Loop on age
7125: * pp[s1]+=freq[s1][s2][iage] weighted
7126: * pos+=freq[s1][s2][iage] weighted
7127: * Loop on s1 initial state
7128: * fprintf(ficresp
7129: * end s1
7130: * end age
7131: * if j!=0 computes starting values
7132: * end compute starting values
7133: * end j1
7134: * end nl
7135: */
1.253 brouard 7136: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
7137: if(nj==1)
7138: j=0; /* First pass for the constant */
1.265 brouard 7139: else{
1.335 brouard 7140: 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 7141: }
1.251 brouard 7142: first=1;
1.332 brouard 7143: 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 7144: posproptt=0.;
1.330 brouard 7145: /*printf("cptcovn=%d Tvaraff=%d", cptcovn,Tvaraff[1]);
1.251 brouard 7146: scanf("%d", i);*/
7147: for (i=-5; i<=nlstate+ndeath; i++)
1.265 brouard 7148: for (s2=-5; s2<=nlstate+ndeath; s2++)
1.251 brouard 7149: for(m=iagemin; m <= iagemax+3; m++)
1.265 brouard 7150: freq[i][s2][m]=0;
1.251 brouard 7151:
7152: for (i=1; i<=nlstate; i++) {
1.240 brouard 7153: for(m=iagemin; m <= iagemax+3; m++)
1.251 brouard 7154: prop[i][m]=0;
7155: posprop[i]=0;
7156: pospropt[i]=0;
7157: }
1.283 brouard 7158: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
1.284 brouard 7159: idq[z1]=0.;
7160: meanq[z1]=0.;
7161: stdq[z1]=0.;
1.283 brouard 7162: }
7163: /* for (z1=1; z1<= nqtveff; z1++) { */
1.251 brouard 7164: /* for(m=1;m<=lastpass;m++){ */
1.283 brouard 7165: /* meanqt[m][z1]=0.; */
7166: /* } */
7167: /* } */
1.251 brouard 7168: /* dateintsum=0; */
7169: /* k2cpt=0; */
7170:
1.265 brouard 7171: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
1.251 brouard 7172: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
7173: bool=1;
7174: if(j !=0){
7175: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
1.335 brouard 7176: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
7177: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
1.251 brouard 7178: /* if(Tvaraff[z1] ==-20){ */
7179: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
7180: /* }else if(Tvaraff[z1] ==-10){ */
7181: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
1.330 brouard 7182: /* }else */ /* TODO TODO codtabm(j1,z1) or codtabm(j1,Tvaraff[z1]]z1)*/
1.335 brouard 7183: /* 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); */
7184: if(Tvaraff[z1]<1 || Tvaraff[z1]>=NCOVMAX)
1.338 brouard 7185: printf("Error Tvaraff[z1]=%d<1 or >=%d, cptcoveff=%d model=1+age+%s\n",Tvaraff[z1],NCOVMAX, cptcoveff, model);
1.332 brouard 7186: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]){ /* for combination j1 of covariates */
1.265 brouard 7187: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
1.251 brouard 7188: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
1.332 brouard 7189: /* 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", */
7190: /* bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),*/
7191: /* j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
1.251 brouard 7192: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
7193: } /* Onlyf fixed */
7194: } /* end z1 */
1.335 brouard 7195: } /* cptcoveff > 0 */
1.251 brouard 7196: } /* end any */
7197: }/* end j==0 */
1.265 brouard 7198: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
1.251 brouard 7199: /* for(m=firstpass; m<=lastpass; m++){ */
1.284 brouard 7200: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
1.251 brouard 7201: m=mw[mi][iind];
7202: if(j!=0){
7203: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
1.335 brouard 7204: for (z1=1; z1<=cptcoveff; z1++) {
1.251 brouard 7205: if( Fixed[Tmodelind[z1]]==1){
1.341 brouard 7206: /* iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; /\* Good *\/ */
7207: iv= Tvar[Tmodelind[z1]]; /* Good *//* because cotvar starts now at first at ncovcol+nqv+ntv */
1.332 brouard 7208: 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 7209: value is -1, we don't select. It differs from the
7210: constant and age model which counts them. */
7211: bool=0; /* not selected */
7212: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
1.334 brouard 7213: /* i1=Tvaraff[z1]; */
7214: /* i2=TnsdVar[i1]; */
7215: /* i3=nbcode[i1][i2]; */
7216: /* i4=covar[i1][iind]; */
7217: /* if(i4 != i3){ */
7218: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) { /* Bug valgrind */
1.251 brouard 7219: bool=0;
7220: }
7221: }
7222: }
7223: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
7224: } /* end j==0 */
7225: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
1.284 brouard 7226: if(bool==1){ /*Selected */
1.251 brouard 7227: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
7228: and mw[mi+1][iind]. dh depends on stepm. */
7229: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
7230: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
7231: if(m >=firstpass && m <=lastpass){
7232: k2=anint[m][iind]+(mint[m][iind]/12.);
7233: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
7234: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
7235: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
7236: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
7237: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
7238: if (m<lastpass) {
7239: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
7240: /* 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]); */
7241: if(s[m][iind]==-1)
7242: 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.));
7243: 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 7244: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
7245: if(!isnan(covar[ncovcol+z1][iind])){
1.332 brouard 7246: idq[z1]=idq[z1]+weight[iind];
7247: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
7248: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
7249: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
1.311 brouard 7250: }
1.284 brouard 7251: }
1.251 brouard 7252: /* if((int)agev[m][iind] == 55) */
7253: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
7254: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
7255: 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 7256: }
1.251 brouard 7257: } /* end if between passes */
7258: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
7259: dateintsum=dateintsum+k2; /* on all covariates ?*/
7260: k2cpt++;
7261: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
1.234 brouard 7262: }
1.251 brouard 7263: }else{
7264: bool=1;
7265: }/* end bool 2 */
7266: } /* end m */
1.284 brouard 7267: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
7268: /* idq[z1]=idq[z1]+weight[iind]; */
7269: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
7270: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
7271: /* } */
1.251 brouard 7272: } /* end bool */
7273: } /* end iind = 1 to imx */
1.319 brouard 7274: /* prop[s][age] is fed for any initial and valid live state as well as
1.251 brouard 7275: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
7276:
7277:
7278: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.335 brouard 7279: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
1.265 brouard 7280: pstamp(ficresp);
1.335 brouard 7281: if (cptcoveff>0 && j!=0){
1.265 brouard 7282: pstamp(ficresp);
1.251 brouard 7283: printf( "\n#********** Variable ");
7284: fprintf(ficresp, "\n#********** Variable ");
7285: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
7286: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
7287: fprintf(ficlog, "\n#********** Variable ");
1.340 brouard 7288: for (z1=1; z1<=cptcoveff; z1++){
1.251 brouard 7289: if(!FixedV[Tvaraff[z1]]){
1.330 brouard 7290: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7291: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7292: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7293: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7294: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.250 brouard 7295: }else{
1.330 brouard 7296: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7297: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7298: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7299: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7300: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.251 brouard 7301: }
7302: }
7303: printf( "**********\n#");
7304: fprintf(ficresp, "**********\n#");
7305: fprintf(ficresphtm, "**********</h3>\n");
7306: fprintf(ficresphtmfr, "**********</h3>\n");
7307: fprintf(ficlog, "**********\n");
7308: }
1.284 brouard 7309: /*
7310: Printing means of quantitative variables if any
7311: */
7312: for (z1=1; z1<= nqfveff; z1++) {
1.311 brouard 7313: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
1.312 brouard 7314: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
1.284 brouard 7315: if(weightopt==1){
7316: printf(" Weighted mean and standard deviation of");
7317: fprintf(ficlog," Weighted mean and standard deviation of");
7318: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
7319: }
1.311 brouard 7320: /* mu = \frac{w x}{\sum w}
7321: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
7322: */
7323: 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]));
7324: 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]));
7325: 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 7326: }
7327: /* for (z1=1; z1<= nqtveff; z1++) { */
7328: /* for(m=1;m<=lastpass;m++){ */
7329: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
7330: /* } */
7331: /* } */
1.283 brouard 7332:
1.251 brouard 7333: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.335 brouard 7334: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
1.265 brouard 7335: fprintf(ficresp, " Age");
1.335 brouard 7336: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) {
7337: 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]]);
7338: fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
7339: }
1.251 brouard 7340: for(i=1; i<=nlstate;i++) {
1.335 brouard 7341: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
1.251 brouard 7342: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
7343: }
1.335 brouard 7344: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
1.251 brouard 7345: fprintf(ficresphtm, "\n");
7346:
7347: /* Header of frequency table by age */
7348: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
7349: fprintf(ficresphtmfr,"<th>Age</th> ");
1.265 brouard 7350: for(s2=-1; s2 <=nlstate+ndeath; s2++){
1.251 brouard 7351: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 7352: if(s2!=0 && m!=0)
7353: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
1.240 brouard 7354: }
1.226 brouard 7355: }
1.251 brouard 7356: fprintf(ficresphtmfr, "\n");
7357:
7358: /* For each age */
7359: for(iage=iagemin; iage <= iagemax+3; iage++){
7360: fprintf(ficresphtm,"<tr>");
7361: if(iage==iagemax+1){
7362: fprintf(ficlog,"1");
7363: fprintf(ficresphtmfr,"<tr><th>0</th> ");
7364: }else if(iage==iagemax+2){
7365: fprintf(ficlog,"0");
7366: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
7367: }else if(iage==iagemax+3){
7368: fprintf(ficlog,"Total");
7369: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
7370: }else{
1.240 brouard 7371: if(first==1){
1.251 brouard 7372: first=0;
7373: printf("See log file for details...\n");
7374: }
7375: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
7376: fprintf(ficlog,"Age %d", iage);
7377: }
1.265 brouard 7378: for(s1=1; s1 <=nlstate ; s1++){
7379: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
7380: pp[s1] += freq[s1][m][iage];
1.251 brouard 7381: }
1.265 brouard 7382: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 7383: for(m=-1, pos=0; m <=0 ; m++)
1.265 brouard 7384: pos += freq[s1][m][iage];
7385: if(pp[s1]>=1.e-10){
1.251 brouard 7386: if(first==1){
1.265 brouard 7387: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 7388: }
1.265 brouard 7389: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 7390: }else{
7391: if(first==1)
1.265 brouard 7392: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
7393: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
1.240 brouard 7394: }
7395: }
7396:
1.265 brouard 7397: for(s1=1; s1 <=nlstate ; s1++){
7398: /* posprop[s1]=0; */
7399: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
7400: pp[s1] += freq[s1][m][iage];
7401: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
7402:
7403: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
7404: pos += pp[s1]; /* pos is the total number of transitions until this age */
7405: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
7406: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7407: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
7408: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
7409: }
7410:
7411: /* Writing ficresp */
1.335 brouard 7412: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
1.265 brouard 7413: if( iage <= iagemax){
7414: fprintf(ficresp," %d",iage);
7415: }
7416: }else if( nj==2){
7417: if( iage <= iagemax){
7418: fprintf(ficresp," %d",iage);
1.335 brouard 7419: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]);
1.265 brouard 7420: }
1.240 brouard 7421: }
1.265 brouard 7422: for(s1=1; s1 <=nlstate ; s1++){
1.240 brouard 7423: if(pos>=1.e-5){
1.251 brouard 7424: if(first==1)
1.265 brouard 7425: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
7426: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
1.251 brouard 7427: }else{
7428: if(first==1)
1.265 brouard 7429: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
7430: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
1.251 brouard 7431: }
7432: if( iage <= iagemax){
7433: if(pos>=1.e-5){
1.335 brouard 7434: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
1.265 brouard 7435: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7436: }else if( nj==2){
7437: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7438: }
7439: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
7440: /*probs[iage][s1][j1]= pp[s1]/pos;*/
7441: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
7442: } else{
1.335 brouard 7443: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
1.265 brouard 7444: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
1.251 brouard 7445: }
1.240 brouard 7446: }
1.265 brouard 7447: pospropt[s1] +=posprop[s1];
7448: } /* end loop s1 */
1.251 brouard 7449: /* pospropt=0.; */
1.265 brouard 7450: for(s1=-1; s1 <=nlstate+ndeath; s1++){
1.251 brouard 7451: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 7452: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
1.251 brouard 7453: if(first==1){
1.265 brouard 7454: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 7455: }
1.265 brouard 7456: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
7457: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 7458: }
1.265 brouard 7459: if(s1!=0 && m!=0)
7460: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
1.240 brouard 7461: }
1.265 brouard 7462: } /* end loop s1 */
1.251 brouard 7463: posproptt=0.;
1.265 brouard 7464: for(s1=1; s1 <=nlstate; s1++){
7465: posproptt += pospropt[s1];
1.251 brouard 7466: }
7467: fprintf(ficresphtmfr,"</tr>\n ");
1.265 brouard 7468: fprintf(ficresphtm,"</tr>\n");
1.335 brouard 7469: if((cptcoveff==0 && nj==1)|| nj==2 ) {
1.265 brouard 7470: if(iage <= iagemax)
7471: fprintf(ficresp,"\n");
1.240 brouard 7472: }
1.251 brouard 7473: if(first==1)
7474: printf("Others in log...\n");
7475: fprintf(ficlog,"\n");
7476: } /* end loop age iage */
1.265 brouard 7477:
1.251 brouard 7478: fprintf(ficresphtm,"<tr><th>Tot</th>");
1.265 brouard 7479: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 7480: if(posproptt < 1.e-5){
1.265 brouard 7481: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
1.251 brouard 7482: }else{
1.265 brouard 7483: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
1.240 brouard 7484: }
1.226 brouard 7485: }
1.251 brouard 7486: fprintf(ficresphtm,"</tr>\n");
7487: fprintf(ficresphtm,"</table>\n");
7488: fprintf(ficresphtmfr,"</table>\n");
1.226 brouard 7489: if(posproptt < 1.e-5){
1.251 brouard 7490: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
7491: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
1.260 brouard 7492: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
7493: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
1.251 brouard 7494: invalidvarcomb[j1]=1;
1.226 brouard 7495: }else{
1.338 brouard 7496: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced (or no resultline).</p>",j1);
1.251 brouard 7497: invalidvarcomb[j1]=0;
1.226 brouard 7498: }
1.251 brouard 7499: fprintf(ficresphtmfr,"</table>\n");
7500: fprintf(ficlog,"\n");
7501: if(j!=0){
7502: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
1.265 brouard 7503: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 7504: for(k=1; k <=(nlstate+ndeath); k++){
7505: if (k != i) {
1.265 brouard 7506: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
1.253 brouard 7507: if(jj==1){ /* Constant case (in fact cste + age) */
1.251 brouard 7508: if(j1==1){ /* All dummy covariates to zero */
7509: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
7510: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
1.252 brouard 7511: printf("%d%d ",i,k);
7512: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 7513: 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]));
7514: 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]));
7515: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
1.251 brouard 7516: }
1.253 brouard 7517: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
7518: for(iage=iagemin; iage <= iagemax+3; iage++){
7519: x[iage]= (double)iage;
7520: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
1.265 brouard 7521: /* 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 7522: }
1.268 brouard 7523: /* Some are not finite, but linreg will ignore these ages */
7524: no=0;
1.253 brouard 7525: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
1.265 brouard 7526: pstart[s1]=b;
7527: pstart[s1-1]=a;
1.252 brouard 7528: }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 */
7529: 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]);
7530: 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 7531: 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 7532: printf("%d%d ",i,k);
7533: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 7534: 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 7535: }else{ /* Other cases, like quantitative fixed or varying covariates */
7536: ;
7537: }
7538: /* printf("%12.7f )", param[i][jj][k]); */
7539: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 7540: s1++;
1.251 brouard 7541: } /* end jj */
7542: } /* end k!= i */
7543: } /* end k */
1.265 brouard 7544: } /* end i, s1 */
1.251 brouard 7545: } /* end j !=0 */
7546: } /* end selected combination of covariate j1 */
7547: if(j==0){ /* We can estimate starting values from the occurences in each case */
7548: printf("#Freqsummary: Starting values for the constants:\n");
7549: fprintf(ficlog,"\n");
1.265 brouard 7550: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 7551: for(k=1; k <=(nlstate+ndeath); k++){
7552: if (k != i) {
7553: printf("%d%d ",i,k);
7554: fprintf(ficlog,"%d%d ",i,k);
7555: for(jj=1; jj <=ncovmodel; jj++){
1.265 brouard 7556: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
1.253 brouard 7557: if(jj==1){ /* Age has to be done */
1.265 brouard 7558: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
7559: 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]));
7560: 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 7561: }
7562: /* printf("%12.7f )", param[i][jj][k]); */
7563: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 7564: s1++;
1.250 brouard 7565: }
1.251 brouard 7566: printf("\n");
7567: fprintf(ficlog,"\n");
1.250 brouard 7568: }
7569: }
1.284 brouard 7570: } /* end of state i */
1.251 brouard 7571: printf("#Freqsummary\n");
7572: fprintf(ficlog,"\n");
1.265 brouard 7573: for(s1=-1; s1 <=nlstate+ndeath; s1++){
7574: for(s2=-1; s2 <=nlstate+ndeath; s2++){
7575: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
7576: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7577: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
7578: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
7579: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
7580: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
1.251 brouard 7581: /* } */
7582: }
1.265 brouard 7583: } /* end loop s1 */
1.251 brouard 7584:
7585: printf("\n");
7586: fprintf(ficlog,"\n");
7587: } /* end j=0 */
1.249 brouard 7588: } /* end j */
1.252 brouard 7589:
1.253 brouard 7590: if(mle == -2){ /* We want to use these values as starting values */
1.252 brouard 7591: for(i=1, jk=1; i <=nlstate; i++){
7592: for(j=1; j <=nlstate+ndeath; j++){
7593: if(j!=i){
7594: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7595: printf("%1d%1d",i,j);
7596: fprintf(ficparo,"%1d%1d",i,j);
7597: for(k=1; k<=ncovmodel;k++){
7598: /* printf(" %lf",param[i][j][k]); */
7599: /* fprintf(ficparo," %lf",param[i][j][k]); */
7600: p[jk]=pstart[jk];
7601: printf(" %f ",pstart[jk]);
7602: fprintf(ficparo," %f ",pstart[jk]);
7603: jk++;
7604: }
7605: printf("\n");
7606: fprintf(ficparo,"\n");
7607: }
7608: }
7609: }
7610: } /* end mle=-2 */
1.226 brouard 7611: dateintmean=dateintsum/k2cpt;
1.296 brouard 7612: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
1.240 brouard 7613:
1.226 brouard 7614: fclose(ficresp);
7615: fclose(ficresphtm);
7616: fclose(ficresphtmfr);
1.283 brouard 7617: free_vector(idq,1,nqfveff);
1.226 brouard 7618: free_vector(meanq,1,nqfveff);
1.284 brouard 7619: free_vector(stdq,1,nqfveff);
1.226 brouard 7620: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.253 brouard 7621: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
7622: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.251 brouard 7623: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 7624: free_vector(pospropt,1,nlstate);
7625: free_vector(posprop,1,nlstate);
1.251 brouard 7626: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 7627: free_vector(pp,1,nlstate);
7628: /* End of freqsummary */
7629: }
1.126 brouard 7630:
1.268 brouard 7631: /* Simple linear regression */
7632: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
7633:
7634: /* y=a+bx regression */
7635: double sumx = 0.0; /* sum of x */
7636: double sumx2 = 0.0; /* sum of x**2 */
7637: double sumxy = 0.0; /* sum of x * y */
7638: double sumy = 0.0; /* sum of y */
7639: double sumy2 = 0.0; /* sum of y**2 */
7640: double sume2 = 0.0; /* sum of square or residuals */
7641: double yhat;
7642:
7643: double denom=0;
7644: int i;
7645: int ne=*no;
7646:
7647: for ( i=ifi, ne=0;i<=ila;i++) {
7648: if(!isfinite(x[i]) || !isfinite(y[i])){
7649: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7650: continue;
7651: }
7652: ne=ne+1;
7653: sumx += x[i];
7654: sumx2 += x[i]*x[i];
7655: sumxy += x[i] * y[i];
7656: sumy += y[i];
7657: sumy2 += y[i]*y[i];
7658: denom = (ne * sumx2 - sumx*sumx);
7659: /* 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); */
7660: }
7661:
7662: denom = (ne * sumx2 - sumx*sumx);
7663: if (denom == 0) {
7664: // vertical, slope m is infinity
7665: *b = INFINITY;
7666: *a = 0;
7667: if (r) *r = 0;
7668: return 1;
7669: }
7670:
7671: *b = (ne * sumxy - sumx * sumy) / denom;
7672: *a = (sumy * sumx2 - sumx * sumxy) / denom;
7673: if (r!=NULL) {
7674: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
7675: sqrt((sumx2 - sumx*sumx/ne) *
7676: (sumy2 - sumy*sumy/ne));
7677: }
7678: *no=ne;
7679: for ( i=ifi, ne=0;i<=ila;i++) {
7680: if(!isfinite(x[i]) || !isfinite(y[i])){
7681: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
7682: continue;
7683: }
7684: ne=ne+1;
7685: yhat = y[i] - *a -*b* x[i];
7686: sume2 += yhat * yhat ;
7687:
7688: denom = (ne * sumx2 - sumx*sumx);
7689: /* 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); */
7690: }
7691: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
7692: *sa= *sb * sqrt(sumx2/ne);
7693:
7694: return 0;
7695: }
7696:
1.126 brouard 7697: /************ Prevalence ********************/
1.227 brouard 7698: 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)
7699: {
7700: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
7701: in each health status at the date of interview (if between dateprev1 and dateprev2).
7702: We still use firstpass and lastpass as another selection.
7703: */
1.126 brouard 7704:
1.227 brouard 7705: int i, m, jk, j1, bool, z1,j, iv;
7706: int mi; /* Effective wave */
7707: int iage;
1.359 ! brouard 7708: double agebegin; /*, ageend;*/
1.227 brouard 7709:
7710: double **prop;
7711: double posprop;
7712: double y2; /* in fractional years */
7713: int iagemin, iagemax;
7714: int first; /** to stop verbosity which is redirected to log file */
7715:
7716: iagemin= (int) agemin;
7717: iagemax= (int) agemax;
7718: /*pp=vector(1,nlstate);*/
1.251 brouard 7719: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 7720: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
7721: j1=0;
1.222 brouard 7722:
1.227 brouard 7723: /*j=cptcoveff;*/
7724: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 7725:
1.288 brouard 7726: first=0;
1.335 brouard 7727: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of simple dummy covariates */
1.227 brouard 7728: for (i=1; i<=nlstate; i++)
1.251 brouard 7729: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
1.227 brouard 7730: prop[i][iage]=0.0;
7731: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
7732: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
7733: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
7734:
7735: for (i=1; i<=imx; i++) { /* Each individual */
7736: bool=1;
7737: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
7738: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
7739: m=mw[mi][i];
7740: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
7741: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
7742: for (z1=1; z1<=cptcoveff; z1++){
7743: if( Fixed[Tmodelind[z1]]==1){
1.341 brouard 7744: iv= Tvar[Tmodelind[z1]];/* because cotvar starts now at first ncovcol+nqv+ (1 to nqtv) */
1.332 brouard 7745: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) /* iv=1 to ntv, right modality */
1.227 brouard 7746: bool=0;
7747: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
1.332 brouard 7748: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]) {
1.227 brouard 7749: bool=0;
7750: }
7751: }
7752: if(bool==1){ /* Otherwise we skip that wave/person */
7753: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
7754: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
7755: if(m >=firstpass && m <=lastpass){
7756: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
7757: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
7758: if(agev[m][i]==0) agev[m][i]=iagemax+1;
7759: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.251 brouard 7760: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
1.227 brouard 7761: 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);
7762: exit(1);
7763: }
7764: if (s[m][i]>0 && s[m][i]<=nlstate) {
7765: /*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]]);*/
7766: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
7767: prop[s[m][i]][iagemax+3] += weight[i];
7768: } /* end valid statuses */
7769: } /* end selection of dates */
7770: } /* end selection of waves */
7771: } /* end bool */
7772: } /* end wave */
7773: } /* end individual */
7774: for(i=iagemin; i <= iagemax+3; i++){
7775: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
7776: posprop += prop[jk][i];
7777: }
7778:
7779: for(jk=1; jk <=nlstate ; jk++){
7780: if( i <= iagemax){
7781: if(posprop>=1.e-5){
7782: probs[i][jk][j1]= prop[jk][i]/posprop;
7783: } else{
1.288 brouard 7784: if(!first){
7785: first=1;
1.266 brouard 7786: 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]);
7787: }else{
1.288 brouard 7788: 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 7789: }
7790: }
7791: }
7792: }/* end jk */
7793: }/* end i */
1.222 brouard 7794: /*} *//* end i1 */
1.227 brouard 7795: } /* end j1 */
1.222 brouard 7796:
1.227 brouard 7797: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
7798: /*free_vector(pp,1,nlstate);*/
1.251 brouard 7799: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 7800: } /* End of prevalence */
1.126 brouard 7801:
7802: /************* Waves Concatenation ***************/
7803:
7804: 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)
7805: {
1.298 brouard 7806: /* 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 7807: Death is a valid wave (if date is known).
7808: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
7809: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.298 brouard 7810: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
1.227 brouard 7811: */
1.126 brouard 7812:
1.224 brouard 7813: int i=0, mi=0, m=0, mli=0;
1.126 brouard 7814: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
7815: double sum=0., jmean=0.;*/
1.224 brouard 7816: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 7817: int j, k=0,jk, ju, jl;
7818: double sum=0.;
7819: first=0;
1.214 brouard 7820: firstwo=0;
1.217 brouard 7821: firsthree=0;
1.218 brouard 7822: firstfour=0;
1.164 brouard 7823: jmin=100000;
1.126 brouard 7824: jmax=-1;
7825: jmean=0.;
1.224 brouard 7826:
7827: /* Treating live states */
1.214 brouard 7828: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 7829: mi=0; /* First valid wave */
1.227 brouard 7830: mli=0; /* Last valid wave */
1.309 brouard 7831: m=firstpass; /* Loop on waves */
7832: while(s[m][i] <= nlstate){ /* a live state or unknown state */
1.227 brouard 7833: 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 */
7834: mli=m-1;/* mw[++mi][i]=m-1; */
7835: }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 7836: 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 7837: mli=m;
1.224 brouard 7838: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
7839: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 7840: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 7841: }
1.309 brouard 7842: else{ /* m = lastpass, eventual special issue with warning */
1.224 brouard 7843: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 7844: break;
1.224 brouard 7845: #else
1.317 brouard 7846: 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 7847: if(firsthree == 0){
1.302 brouard 7848: 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 7849: firsthree=1;
1.317 brouard 7850: }else if(firsthree >=1 && firsthree < 10){
7851: 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);
7852: firsthree++;
7853: }else if(firsthree == 10){
7854: printf("Information, too many Information flags: no more reported to log either\n");
7855: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
7856: firsthree++;
7857: }else{
7858: firsthree++;
1.227 brouard 7859: }
1.309 brouard 7860: mw[++mi][i]=m; /* Valid transition with unknown status */
1.227 brouard 7861: mli=m;
7862: }
7863: if(s[m][i]==-2){ /* Vital status is really unknown */
7864: nbwarn++;
1.309 brouard 7865: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
1.227 brouard 7866: 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);
7867: 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);
7868: }
7869: break;
7870: }
7871: break;
1.224 brouard 7872: #endif
1.227 brouard 7873: }/* End m >= lastpass */
1.126 brouard 7874: }/* end while */
1.224 brouard 7875:
1.227 brouard 7876: /* 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 7877: /* After last pass */
1.224 brouard 7878: /* Treating death states */
1.214 brouard 7879: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 7880: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
7881: /* } */
1.126 brouard 7882: mi++; /* Death is another wave */
7883: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 7884: /* Only death is a correct wave */
1.126 brouard 7885: mw[mi][i]=m;
1.257 brouard 7886: } /* else not in a death state */
1.224 brouard 7887: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.257 brouard 7888: else if ((int) andc[i] != 9999) { /* Date of death is known */
1.218 brouard 7889: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.309 brouard 7890: 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 7891: nbwarn++;
7892: if(firstfiv==0){
1.309 brouard 7893: 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 7894: firstfiv=1;
7895: }else{
1.309 brouard 7896: 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 7897: }
1.309 brouard 7898: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
7899: }else{ /* Month of Death occured afer last wave month, potential bias */
1.227 brouard 7900: nberr++;
7901: if(firstwo==0){
1.309 brouard 7902: 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 7903: firstwo=1;
7904: }
1.309 brouard 7905: 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 7906: }
1.257 brouard 7907: }else{ /* if date of interview is unknown */
1.227 brouard 7908: /* death is known but not confirmed by death status at any wave */
7909: if(firstfour==0){
1.309 brouard 7910: 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 7911: firstfour=1;
7912: }
1.309 brouard 7913: 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 7914: }
1.224 brouard 7915: } /* end if date of death is known */
7916: #endif
1.309 brouard 7917: wav[i]=mi; /* mi should be the last effective wave (or mli), */
7918: /* wav[i]=mw[mi][i]; */
1.126 brouard 7919: if(mi==0){
7920: nbwarn++;
7921: if(first==0){
1.227 brouard 7922: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
7923: first=1;
1.126 brouard 7924: }
7925: if(first==1){
1.227 brouard 7926: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 7927: }
7928: } /* end mi==0 */
7929: } /* End individuals */
1.214 brouard 7930: /* wav and mw are no more changed */
1.223 brouard 7931:
1.317 brouard 7932: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
7933: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
7934:
7935:
1.126 brouard 7936: for(i=1; i<=imx; i++){
7937: for(mi=1; mi<wav[i];mi++){
7938: if (stepm <=0)
1.227 brouard 7939: dh[mi][i]=1;
1.126 brouard 7940: else{
1.260 brouard 7941: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
1.227 brouard 7942: if (agedc[i] < 2*AGESUP) {
7943: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
7944: if(j==0) j=1; /* Survives at least one month after exam */
7945: else if(j<0){
7946: nberr++;
1.359 ! brouard 7947: 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 7948: j=1; /* Temporary Dangerous patch */
7949: 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 7950: 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 7951: 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);
7952: }
7953: k=k+1;
7954: if (j >= jmax){
7955: jmax=j;
7956: ijmax=i;
7957: }
7958: if (j <= jmin){
7959: jmin=j;
7960: ijmin=i;
7961: }
7962: sum=sum+j;
7963: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
7964: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
7965: }
7966: }
7967: else{
7968: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 7969: /* 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 7970:
1.227 brouard 7971: k=k+1;
7972: if (j >= jmax) {
7973: jmax=j;
7974: ijmax=i;
7975: }
7976: else if (j <= jmin){
7977: jmin=j;
7978: ijmin=i;
7979: }
7980: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
7981: /*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]);*/
7982: if(j<0){
7983: nberr++;
1.359 ! brouard 7984: 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]);
! 7985: 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 7986: }
7987: sum=sum+j;
7988: }
7989: jk= j/stepm;
7990: jl= j -jk*stepm;
7991: ju= j -(jk+1)*stepm;
7992: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
7993: if(jl==0){
7994: dh[mi][i]=jk;
7995: bh[mi][i]=0;
7996: }else{ /* We want a negative bias in order to only have interpolation ie
7997: * to avoid the price of an extra matrix product in likelihood */
7998: dh[mi][i]=jk+1;
7999: bh[mi][i]=ju;
8000: }
8001: }else{
8002: if(jl <= -ju){
8003: dh[mi][i]=jk;
8004: bh[mi][i]=jl; /* bias is positive if real duration
8005: * is higher than the multiple of stepm and negative otherwise.
8006: */
8007: }
8008: else{
8009: dh[mi][i]=jk+1;
8010: bh[mi][i]=ju;
8011: }
8012: if(dh[mi][i]==0){
8013: dh[mi][i]=1; /* At least one step */
8014: bh[mi][i]=ju; /* At least one step */
8015: /* 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);*/
8016: }
8017: } /* end if mle */
1.126 brouard 8018: }
8019: } /* end wave */
8020: }
8021: jmean=sum/k;
8022: 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 8023: 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 8024: }
1.126 brouard 8025:
8026: /*********** Tricode ****************************/
1.220 brouard 8027: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 8028: {
8029: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
8030: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
8031: * Boring subroutine which should only output nbcode[Tvar[j]][k]
8032: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
8033: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
8034: */
1.130 brouard 8035:
1.242 brouard 8036: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
8037: int modmaxcovj=0; /* Modality max of covariates j */
8038: int cptcode=0; /* Modality max of covariates j */
8039: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 8040:
8041:
1.242 brouard 8042: /* cptcoveff=0; */
8043: /* *cptcov=0; */
1.126 brouard 8044:
1.242 brouard 8045: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.285 brouard 8046: for (k=1; k <= maxncov; k++)
8047: for(j=1; j<=2; j++)
8048: nbcode[k][j]=0; /* Valgrind */
1.126 brouard 8049:
1.242 brouard 8050: /* Loop on covariates without age and products and no quantitative variable */
1.335 brouard 8051: 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 8052: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
1.343 brouard 8053: /* printf("Testing k=%d, cptcovt=%d\n",k, cptcovt); */
1.349 brouard 8054: 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 8055: switch(Fixed[k]) {
8056: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.311 brouard 8057: modmaxcovj=0;
8058: modmincovj=0;
1.242 brouard 8059: 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 8060: /* 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 8061: ij=(int)(covar[Tvar[k]][i]);
8062: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
8063: * If product of Vn*Vm, still boolean *:
8064: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
8065: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
8066: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
8067: modality of the nth covariate of individual i. */
8068: if (ij > modmaxcovj)
8069: modmaxcovj=ij;
8070: else if (ij < modmincovj)
8071: modmincovj=ij;
1.287 brouard 8072: if (ij <0 || ij >1 ){
1.311 brouard 8073: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8074: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
8075: fflush(ficlog);
8076: exit(1);
1.287 brouard 8077: }
8078: if ((ij < -1) || (ij > NCOVMAX)){
1.242 brouard 8079: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
8080: exit(1);
8081: }else
8082: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
8083: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
8084: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
8085: /* getting the maximum value of the modality of the covariate
8086: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
8087: female ies 1, then modmaxcovj=1.
8088: */
8089: } /* end for loop on individuals i */
8090: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8091: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
8092: cptcode=modmaxcovj;
8093: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
8094: /*for (i=0; i<=cptcode; i++) {*/
8095: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
8096: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8097: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
8098: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
8099: if( j != -1){
8100: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
8101: covariate for which somebody answered excluding
8102: undefined. Usually 2: 0 and 1. */
8103: }
8104: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
8105: covariate for which somebody answered including
8106: undefined. Usually 3: -1, 0 and 1. */
8107: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
8108: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
8109: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 8110:
1.242 brouard 8111: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
8112: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
8113: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
8114: /* modmincovj=3; modmaxcovj = 7; */
8115: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
8116: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
8117: /* defining two dummy variables: variables V1_1 and V1_2.*/
8118: /* nbcode[Tvar[j]][ij]=k; */
8119: /* nbcode[Tvar[j]][1]=0; */
8120: /* nbcode[Tvar[j]][2]=1; */
8121: /* nbcode[Tvar[j]][3]=2; */
8122: /* To be continued (not working yet). */
8123: ij=0; /* ij is similar to i but can jump over null modalities */
1.287 brouard 8124:
8125: /* 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*/
8126: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
8127: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
8128: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
8129: /*, could be restored in the future */
8130: 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 8131: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
8132: break;
8133: }
8134: ij++;
1.287 brouard 8135: 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 8136: cptcode = ij; /* New max modality for covar j */
8137: } /* end of loop on modality i=-1 to 1 or more */
8138: break;
8139: case 1: /* Testing on varying covariate, could be simple and
8140: * should look at waves or product of fixed *
8141: * varying. No time to test -1, assuming 0 and 1 only */
8142: ij=0;
8143: for(i=0; i<=1;i++){
8144: nbcode[Tvar[k]][++ij]=i;
8145: }
8146: break;
8147: default:
8148: break;
8149: } /* end switch */
8150: } /* end dummy test */
1.349 brouard 8151: if(Dummy[k]==1 && Typevar[k] !=1 && Typevar[k] !=3 && Fixed ==0){ /* Fixed Quantitative covariate and not age product */
1.311 brouard 8152: 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 8153: if(Tvar[k]<=0 || Tvar[k]>=NCOVMAX){
8154: printf("Error k=%d \n",k);
8155: exit(1);
8156: }
1.311 brouard 8157: if(isnan(covar[Tvar[k]][i])){
8158: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8159: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
8160: fflush(ficlog);
8161: exit(1);
8162: }
8163: }
1.335 brouard 8164: } /* end Quanti */
1.287 brouard 8165: } /* 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 8166:
8167: for (k=-1; k< maxncov; k++) Ndum[k]=0;
8168: /* Look at fixed dummy (single or product) covariates to check empty modalities */
8169: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
8170: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
8171: 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 */
8172: 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 */
8173: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
8174: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
8175:
8176: ij=0;
8177: /* 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 8178: 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 */
8179: /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
1.242 brouard 8180: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
8181: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
1.335 brouard 8182: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy simple and non empty in the model */
8183: /* Typevar[k] =0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
8184: /* 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 8185: /* If product not in single variable we don't print results */
8186: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
1.335 brouard 8187: ++ij;/* V5 + V4 + V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V1, *//* V5 quanti, V2 quanti, V4, V3, V1 dummies */
8188: /* k= 1 2 3 4 5 6 7 8 9 */
8189: /* Tvar[k]= 5 4 3 6 5 2 7 1 1 */
8190: /* ij 1 2 3 */
8191: /* Tvaraff[ij]= 4 3 1 */
8192: /* Tmodelind[ij]=2 3 9 */
8193: /* TmodelInvind[ij]=2 1 1 */
1.242 brouard 8194: 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*/
8195: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
8196: 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 */
8197: if(Fixed[k]!=0)
8198: anyvaryingduminmodel=1;
8199: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
8200: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
8201: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
8202: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
8203: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
8204: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
8205: }
8206: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
8207: /* ij--; */
8208: /* cptcoveff=ij; /\*Number of total covariates*\/ */
1.335 brouard 8209: *cptcov=ij; /* cptcov= Number of total real effective simple dummies (fixed or time arying) effective (used as cptcoveff in other functions)
1.242 brouard 8210: * because they can be excluded from the model and real
8211: * if in the model but excluded because missing values, but how to get k from ij?*/
8212: for(j=ij+1; j<= cptcovt; j++){
8213: Tvaraff[j]=0;
8214: Tmodelind[j]=0;
8215: }
8216: for(j=ntveff+1; j<= cptcovt; j++){
8217: TmodelInvind[j]=0;
8218: }
8219: /* To be sorted */
8220: ;
8221: }
1.126 brouard 8222:
1.145 brouard 8223:
1.126 brouard 8224: /*********** Health Expectancies ****************/
8225:
1.235 brouard 8226: 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 8227:
8228: {
8229: /* Health expectancies, no variances */
1.329 brouard 8230: /* cij is the combination in the list of combination of dummy covariates */
8231: /* strstart is a string of time at start of computing */
1.164 brouard 8232: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 8233: int nhstepma, nstepma; /* Decreasing with age */
8234: double age, agelim, hf;
8235: double ***p3mat;
8236: double eip;
8237:
1.238 brouard 8238: /* pstamp(ficreseij); */
1.126 brouard 8239: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
8240: fprintf(ficreseij,"# Age");
8241: for(i=1; i<=nlstate;i++){
8242: for(j=1; j<=nlstate;j++){
8243: fprintf(ficreseij," e%1d%1d ",i,j);
8244: }
8245: fprintf(ficreseij," e%1d. ",i);
8246: }
8247: fprintf(ficreseij,"\n");
8248:
8249:
8250: if(estepm < stepm){
8251: printf ("Problem %d lower than %d\n",estepm, stepm);
8252: }
8253: else hstepm=estepm;
8254: /* We compute the life expectancy from trapezoids spaced every estepm months
8255: * This is mainly to measure the difference between two models: for example
8256: * if stepm=24 months pijx are given only every 2 years and by summing them
8257: * we are calculating an estimate of the Life Expectancy assuming a linear
8258: * progression in between and thus overestimating or underestimating according
8259: * to the curvature of the survival function. If, for the same date, we
8260: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8261: * to compare the new estimate of Life expectancy with the same linear
8262: * hypothesis. A more precise result, taking into account a more precise
8263: * curvature will be obtained if estepm is as small as stepm. */
8264:
8265: /* For example we decided to compute the life expectancy with the smallest unit */
8266: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8267: nhstepm is the number of hstepm from age to agelim
8268: nstepm is the number of stepm from age to agelin.
1.270 brouard 8269: Look at hpijx to understand the reason which relies in memory size consideration
1.126 brouard 8270: and note for a fixed period like estepm months */
8271: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8272: survival function given by stepm (the optimization length). Unfortunately it
8273: means that if the survival funtion is printed only each two years of age and if
8274: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8275: results. So we changed our mind and took the option of the best precision.
8276: */
8277: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8278:
8279: agelim=AGESUP;
8280: /* If stepm=6 months */
8281: /* Computed by stepm unit matrices, product of hstepm matrices, stored
8282: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
8283:
8284: /* nhstepm age range expressed in number of stepm */
8285: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8286: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8287: /* if (stepm >= YEARM) hstepm=1;*/
8288: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8289: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8290:
8291: for (age=bage; age<=fage; age ++){
8292: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8293: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8294: /* if (stepm >= YEARM) hstepm=1;*/
8295: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
8296:
8297: /* If stepm=6 months */
8298: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8299: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
1.330 brouard 8300: /* 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 8301: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 8302:
8303: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8304:
8305: printf("%d|",(int)age);fflush(stdout);
8306: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8307:
8308: /* Computing expectancies */
8309: for(i=1; i<=nlstate;i++)
8310: for(j=1; j<=nlstate;j++)
8311: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8312: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
8313:
8314: /* 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]);*/
8315:
8316: }
8317:
8318: fprintf(ficreseij,"%3.0f",age );
8319: for(i=1; i<=nlstate;i++){
8320: eip=0;
8321: for(j=1; j<=nlstate;j++){
8322: eip +=eij[i][j][(int)age];
8323: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
8324: }
8325: fprintf(ficreseij,"%9.4f", eip );
8326: }
8327: fprintf(ficreseij,"\n");
8328:
8329: }
8330: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8331: printf("\n");
8332: fprintf(ficlog,"\n");
8333:
8334: }
8335:
1.235 brouard 8336: 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 8337:
8338: {
8339: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 8340: to initial status i, ei. .
1.126 brouard 8341: */
1.336 brouard 8342: /* Very time consuming function, but already optimized with precov */
1.126 brouard 8343: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
8344: int nhstepma, nstepma; /* Decreasing with age */
8345: double age, agelim, hf;
8346: double ***p3matp, ***p3matm, ***varhe;
8347: double **dnewm,**doldm;
8348: double *xp, *xm;
8349: double **gp, **gm;
8350: double ***gradg, ***trgradg;
8351: int theta;
8352:
8353: double eip, vip;
8354:
8355: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
8356: xp=vector(1,npar);
8357: xm=vector(1,npar);
8358: dnewm=matrix(1,nlstate*nlstate,1,npar);
8359: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
8360:
8361: pstamp(ficresstdeij);
8362: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
8363: fprintf(ficresstdeij,"# Age");
8364: for(i=1; i<=nlstate;i++){
8365: for(j=1; j<=nlstate;j++)
8366: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
8367: fprintf(ficresstdeij," e%1d. ",i);
8368: }
8369: fprintf(ficresstdeij,"\n");
8370:
8371: pstamp(ficrescveij);
8372: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
8373: fprintf(ficrescveij,"# Age");
8374: for(i=1; i<=nlstate;i++)
8375: for(j=1; j<=nlstate;j++){
8376: cptj= (j-1)*nlstate+i;
8377: for(i2=1; i2<=nlstate;i2++)
8378: for(j2=1; j2<=nlstate;j2++){
8379: cptj2= (j2-1)*nlstate+i2;
8380: if(cptj2 <= cptj)
8381: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
8382: }
8383: }
8384: fprintf(ficrescveij,"\n");
8385:
8386: if(estepm < stepm){
8387: printf ("Problem %d lower than %d\n",estepm, stepm);
8388: }
8389: else hstepm=estepm;
8390: /* We compute the life expectancy from trapezoids spaced every estepm months
8391: * This is mainly to measure the difference between two models: for example
8392: * if stepm=24 months pijx are given only every 2 years and by summing them
8393: * we are calculating an estimate of the Life Expectancy assuming a linear
8394: * progression in between and thus overestimating or underestimating according
8395: * to the curvature of the survival function. If, for the same date, we
8396: * estimate the model with stepm=1 month, we can keep estepm to 24 months
8397: * to compare the new estimate of Life expectancy with the same linear
8398: * hypothesis. A more precise result, taking into account a more precise
8399: * curvature will be obtained if estepm is as small as stepm. */
8400:
8401: /* For example we decided to compute the life expectancy with the smallest unit */
8402: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8403: nhstepm is the number of hstepm from age to agelim
8404: nstepm is the number of stepm from age to agelin.
8405: Look at hpijx to understand the reason of that which relies in memory size
8406: and note for a fixed period like estepm months */
8407: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
8408: survival function given by stepm (the optimization length). Unfortunately it
8409: means that if the survival funtion is printed only each two years of age and if
8410: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8411: results. So we changed our mind and took the option of the best precision.
8412: */
8413: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8414:
8415: /* If stepm=6 months */
8416: /* nhstepm age range expressed in number of stepm */
8417: agelim=AGESUP;
8418: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
8419: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8420: /* if (stepm >= YEARM) hstepm=1;*/
8421: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8422:
8423: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8424: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8425: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
8426: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
8427: gp=matrix(0,nhstepm,1,nlstate*nlstate);
8428: gm=matrix(0,nhstepm,1,nlstate*nlstate);
8429:
8430: for (age=bage; age<=fage; age ++){
8431: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
8432: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
8433: /* if (stepm >= YEARM) hstepm=1;*/
8434: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 8435:
1.126 brouard 8436: /* If stepm=6 months */
8437: /* Computed by stepm unit matrices, product of hstepma matrices, stored
8438: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
8439:
8440: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 8441:
1.126 brouard 8442: /* Computing Variances of health expectancies */
8443: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
8444: decrease memory allocation */
8445: for(theta=1; theta <=npar; theta++){
8446: for(i=1; i<=npar; i++){
1.222 brouard 8447: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8448: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 8449: }
1.235 brouard 8450: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
8451: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 8452:
1.126 brouard 8453: for(j=1; j<= nlstate; j++){
1.222 brouard 8454: for(i=1; i<=nlstate; i++){
8455: for(h=0; h<=nhstepm-1; h++){
8456: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
8457: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
8458: }
8459: }
1.126 brouard 8460: }
1.218 brouard 8461:
1.126 brouard 8462: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 8463: for(h=0; h<=nhstepm-1; h++){
8464: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
8465: }
1.126 brouard 8466: }/* End theta */
8467:
8468:
8469: for(h=0; h<=nhstepm-1; h++)
8470: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 8471: for(theta=1; theta <=npar; theta++)
8472: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 8473:
1.218 brouard 8474:
1.222 brouard 8475: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 8476: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 8477: varhe[ij][ji][(int)age] =0.;
1.218 brouard 8478:
1.222 brouard 8479: printf("%d|",(int)age);fflush(stdout);
8480: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
8481: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 8482: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 8483: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
8484: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
8485: for(ij=1;ij<=nlstate*nlstate;ij++)
8486: for(ji=1;ji<=nlstate*nlstate;ji++)
8487: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 8488: }
8489: }
1.320 brouard 8490: /* if((int)age ==50){ */
8491: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
8492: /* } */
1.126 brouard 8493: /* Computing expectancies */
1.235 brouard 8494: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 8495: for(i=1; i<=nlstate;i++)
8496: for(j=1; j<=nlstate;j++)
1.222 brouard 8497: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
8498: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 8499:
1.222 brouard 8500: /* 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 8501:
1.222 brouard 8502: }
1.269 brouard 8503:
8504: /* Standard deviation of expectancies ij */
1.126 brouard 8505: fprintf(ficresstdeij,"%3.0f",age );
8506: for(i=1; i<=nlstate;i++){
8507: eip=0.;
8508: vip=0.;
8509: for(j=1; j<=nlstate;j++){
1.222 brouard 8510: eip += eij[i][j][(int)age];
8511: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
8512: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
8513: 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 8514: }
8515: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
8516: }
8517: fprintf(ficresstdeij,"\n");
1.218 brouard 8518:
1.269 brouard 8519: /* Variance of expectancies ij */
1.126 brouard 8520: fprintf(ficrescveij,"%3.0f",age );
8521: for(i=1; i<=nlstate;i++)
8522: for(j=1; j<=nlstate;j++){
1.222 brouard 8523: cptj= (j-1)*nlstate+i;
8524: for(i2=1; i2<=nlstate;i2++)
8525: for(j2=1; j2<=nlstate;j2++){
8526: cptj2= (j2-1)*nlstate+i2;
8527: if(cptj2 <= cptj)
8528: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
8529: }
1.126 brouard 8530: }
8531: fprintf(ficrescveij,"\n");
1.218 brouard 8532:
1.126 brouard 8533: }
8534: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
8535: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
8536: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
8537: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
8538: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8539: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8540: printf("\n");
8541: fprintf(ficlog,"\n");
1.218 brouard 8542:
1.126 brouard 8543: free_vector(xm,1,npar);
8544: free_vector(xp,1,npar);
8545: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
8546: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
8547: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
8548: }
1.218 brouard 8549:
1.126 brouard 8550: /************ Variance ******************/
1.235 brouard 8551: 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 8552: {
1.279 brouard 8553: /** Variance of health expectancies
8554: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
8555: * double **newm;
8556: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
8557: */
1.218 brouard 8558:
8559: /* int movingaverage(); */
8560: double **dnewm,**doldm;
8561: double **dnewmp,**doldmp;
8562: int i, j, nhstepm, hstepm, h, nstepm ;
1.288 brouard 8563: int first=0;
1.218 brouard 8564: int k;
8565: double *xp;
1.279 brouard 8566: double **gp, **gm; /**< for var eij */
8567: double ***gradg, ***trgradg; /**< for var eij */
8568: double **gradgp, **trgradgp; /**< for var p point j */
8569: double *gpp, *gmp; /**< for var p point j */
8570: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
1.218 brouard 8571: double ***p3mat;
8572: double age,agelim, hf;
8573: /* double ***mobaverage; */
8574: int theta;
8575: char digit[4];
8576: char digitp[25];
8577:
8578: char fileresprobmorprev[FILENAMELENGTH];
8579:
8580: if(popbased==1){
8581: if(mobilav!=0)
8582: strcpy(digitp,"-POPULBASED-MOBILAV_");
8583: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
8584: }
8585: else
8586: strcpy(digitp,"-STABLBASED_");
1.126 brouard 8587:
1.218 brouard 8588: /* if (mobilav!=0) { */
8589: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8590: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
8591: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
8592: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
8593: /* } */
8594: /* } */
8595:
8596: strcpy(fileresprobmorprev,"PRMORPREV-");
8597: sprintf(digit,"%-d",ij);
8598: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
8599: strcat(fileresprobmorprev,digit); /* Tvar to be done */
8600: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
8601: strcat(fileresprobmorprev,fileresu);
8602: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
8603: printf("Problem with resultfile: %s\n", fileresprobmorprev);
8604: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
8605: }
8606: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8607: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
8608: pstamp(ficresprobmorprev);
8609: 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 8610: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
1.337 brouard 8611:
8612: /* We use TinvDoQresult[nres][resultmodel[nres][j] we sort according to the equation model and the resultline: it is a choice */
8613: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ /\* To be done*\/ */
8614: /* fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
8615: /* } */
8616: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */ /* To be done*/
1.344 brouard 8617: /* fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]); */
1.337 brouard 8618: fprintf(ficresprobmorprev," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 8619: }
1.337 brouard 8620: /* for(j=1;j<=cptcoveff;j++) */
8621: /* fprintf(ficresprobmorprev," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,TnsdVar[Tvaraff[j]])]); */
1.238 brouard 8622: fprintf(ficresprobmorprev,"\n");
8623:
1.218 brouard 8624: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
8625: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
8626: fprintf(ficresprobmorprev," p.%-d SE",j);
8627: for(i=1; i<=nlstate;i++)
8628: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
8629: }
8630: fprintf(ficresprobmorprev,"\n");
8631:
8632: fprintf(ficgp,"\n# Routine varevsij");
8633: fprintf(ficgp,"\nunset title \n");
8634: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
8635: 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");
8636: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
1.279 brouard 8637:
1.218 brouard 8638: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8639: pstamp(ficresvij);
8640: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
8641: if(popbased==1)
8642: 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);
8643: else
8644: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
8645: fprintf(ficresvij,"# Age");
8646: for(i=1; i<=nlstate;i++)
8647: for(j=1; j<=nlstate;j++)
8648: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
8649: fprintf(ficresvij,"\n");
8650:
8651: xp=vector(1,npar);
8652: dnewm=matrix(1,nlstate,1,npar);
8653: doldm=matrix(1,nlstate,1,nlstate);
8654: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
8655: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8656:
8657: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
8658: gpp=vector(nlstate+1,nlstate+ndeath);
8659: gmp=vector(nlstate+1,nlstate+ndeath);
8660: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 8661:
1.218 brouard 8662: if(estepm < stepm){
8663: printf ("Problem %d lower than %d\n",estepm, stepm);
8664: }
8665: else hstepm=estepm;
8666: /* For example we decided to compute the life expectancy with the smallest unit */
8667: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
8668: nhstepm is the number of hstepm from age to agelim
8669: nstepm is the number of stepm from age to agelim.
8670: Look at function hpijx to understand why because of memory size limitations,
8671: we decided (b) to get a life expectancy respecting the most precise curvature of the
8672: survival function given by stepm (the optimization length). Unfortunately it
8673: means that if the survival funtion is printed every two years of age and if
8674: you sum them up and add 1 year (area under the trapezoids) you won't get the same
8675: results. So we changed our mind and took the option of the best precision.
8676: */
8677: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
8678: agelim = AGESUP;
8679: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
8680: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8681: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
8682: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8683: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
8684: gp=matrix(0,nhstepm,1,nlstate);
8685: gm=matrix(0,nhstepm,1,nlstate);
8686:
8687:
8688: for(theta=1; theta <=npar; theta++){
8689: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
8690: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8691: }
1.279 brouard 8692: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
8693: * returns into prlim .
1.288 brouard 8694: */
1.242 brouard 8695: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.279 brouard 8696:
8697: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
1.218 brouard 8698: if (popbased==1) {
8699: if(mobilav ==0){
8700: for(i=1; i<=nlstate;i++)
8701: prlim[i][i]=probs[(int)age][i][ij];
8702: }else{ /* mobilav */
8703: for(i=1; i<=nlstate;i++)
8704: prlim[i][i]=mobaverage[(int)age][i][ij];
8705: }
8706: }
1.295 brouard 8707: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
1.279 brouard 8708: */
8709: 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 8710: /**< 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 8711: * at horizon h in state j including mortality.
8712: */
1.218 brouard 8713: for(j=1; j<= nlstate; j++){
8714: for(h=0; h<=nhstepm; h++){
8715: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
8716: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
8717: }
8718: }
1.279 brouard 8719: /* Next for computing shifted+ probability of death (h=1 means
1.218 brouard 8720: computed over hstepm matrices product = hstepm*stepm months)
1.279 brouard 8721: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
1.218 brouard 8722: */
8723: for(j=nlstate+1;j<=nlstate+ndeath;j++){
8724: for(i=1,gpp[j]=0.; i<= nlstate; i++)
8725: gpp[j] += prlim[i][i]*p3mat[i][j][1];
1.279 brouard 8726: }
8727:
8728: /* Again with minus shift */
1.218 brouard 8729:
8730: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
8731: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 8732:
1.242 brouard 8733: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 8734:
8735: if (popbased==1) {
8736: if(mobilav ==0){
8737: for(i=1; i<=nlstate;i++)
8738: prlim[i][i]=probs[(int)age][i][ij];
8739: }else{ /* mobilav */
8740: for(i=1; i<=nlstate;i++)
8741: prlim[i][i]=mobaverage[(int)age][i][ij];
8742: }
8743: }
8744:
1.235 brouard 8745: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 8746:
8747: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
8748: for(h=0; h<=nhstepm; h++){
8749: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
8750: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
8751: }
8752: }
8753: /* This for computing probability of death (h=1 means
8754: computed over hstepm matrices product = hstepm*stepm months)
8755: as a weighted average of prlim.
8756: */
8757: for(j=nlstate+1;j<=nlstate+ndeath;j++){
8758: for(i=1,gmp[j]=0.; i<= nlstate; i++)
8759: gmp[j] += prlim[i][i]*p3mat[i][j][1];
8760: }
1.279 brouard 8761: /* end shifting computations */
8762:
8763: /**< Computing gradient matrix at horizon h
8764: */
1.218 brouard 8765: for(j=1; j<= nlstate; j++) /* vareij */
8766: for(h=0; h<=nhstepm; h++){
8767: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
8768: }
1.279 brouard 8769: /**< Gradient of overall mortality p.3 (or p.j)
8770: */
8771: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
1.218 brouard 8772: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
8773: }
8774:
8775: } /* End theta */
1.279 brouard 8776:
8777: /* We got the gradient matrix for each theta and state j */
1.218 brouard 8778: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
8779:
8780: for(h=0; h<=nhstepm; h++) /* veij */
8781: for(j=1; j<=nlstate;j++)
8782: for(theta=1; theta <=npar; theta++)
8783: trgradg[h][j][theta]=gradg[h][theta][j];
8784:
8785: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
8786: for(theta=1; theta <=npar; theta++)
8787: trgradgp[j][theta]=gradgp[theta][j];
1.279 brouard 8788: /**< as well as its transposed matrix
8789: */
1.218 brouard 8790:
8791: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
8792: for(i=1;i<=nlstate;i++)
8793: for(j=1;j<=nlstate;j++)
8794: vareij[i][j][(int)age] =0.;
1.279 brouard 8795:
8796: /* Computing trgradg by matcov by gradg at age and summing over h
8797: * and k (nhstepm) formula 15 of article
8798: * Lievre-Brouard-Heathcote
8799: */
8800:
1.218 brouard 8801: for(h=0;h<=nhstepm;h++){
8802: for(k=0;k<=nhstepm;k++){
8803: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
8804: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
8805: for(i=1;i<=nlstate;i++)
8806: for(j=1;j<=nlstate;j++)
8807: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
8808: }
8809: }
8810:
1.279 brouard 8811: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
8812: * p.j overall mortality formula 49 but computed directly because
8813: * we compute the grad (wix pijx) instead of grad (pijx),even if
8814: * wix is independent of theta.
8815: */
1.218 brouard 8816: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
8817: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
8818: for(j=nlstate+1;j<=nlstate+ndeath;j++)
8819: for(i=nlstate+1;i<=nlstate+ndeath;i++)
8820: varppt[j][i]=doldmp[j][i];
8821: /* end ppptj */
8822: /* x centered again */
8823:
1.242 brouard 8824: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 8825:
8826: if (popbased==1) {
8827: if(mobilav ==0){
8828: for(i=1; i<=nlstate;i++)
8829: prlim[i][i]=probs[(int)age][i][ij];
8830: }else{ /* mobilav */
8831: for(i=1; i<=nlstate;i++)
8832: prlim[i][i]=mobaverage[(int)age][i][ij];
8833: }
8834: }
8835:
8836: /* This for computing probability of death (h=1 means
8837: computed over hstepm (estepm) matrices product = hstepm*stepm months)
8838: as a weighted average of prlim.
8839: */
1.235 brouard 8840: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 8841: for(j=nlstate+1;j<=nlstate+ndeath;j++){
8842: for(i=1,gmp[j]=0.;i<= nlstate; i++)
8843: gmp[j] += prlim[i][i]*p3mat[i][j][1];
8844: }
8845: /* end probability of death */
8846:
8847: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
8848: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
8849: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
8850: for(i=1; i<=nlstate;i++){
8851: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
8852: }
8853: }
8854: fprintf(ficresprobmorprev,"\n");
8855:
8856: fprintf(ficresvij,"%.0f ",age );
8857: for(i=1; i<=nlstate;i++)
8858: for(j=1; j<=nlstate;j++){
8859: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
8860: }
8861: fprintf(ficresvij,"\n");
8862: free_matrix(gp,0,nhstepm,1,nlstate);
8863: free_matrix(gm,0,nhstepm,1,nlstate);
8864: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
8865: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
8866: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8867: } /* End age */
8868: free_vector(gpp,nlstate+1,nlstate+ndeath);
8869: free_vector(gmp,nlstate+1,nlstate+ndeath);
8870: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
8871: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
8872: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
8873: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
8874: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
8875: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
8876: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
8877: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
8878: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
8879: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
8880: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
8881: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
8882: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
8883: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
8884: 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);
8885: /* 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 8886: */
1.218 brouard 8887: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
8888: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 8889:
1.218 brouard 8890: free_vector(xp,1,npar);
8891: free_matrix(doldm,1,nlstate,1,nlstate);
8892: free_matrix(dnewm,1,nlstate,1,npar);
8893: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8894: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
8895: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
8896: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8897: fclose(ficresprobmorprev);
8898: fflush(ficgp);
8899: fflush(fichtm);
8900: } /* end varevsij */
1.126 brouard 8901:
8902: /************ Variance of prevlim ******************/
1.269 brouard 8903: 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 8904: {
1.205 brouard 8905: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 8906: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 8907:
1.268 brouard 8908: double **dnewmpar,**doldm;
1.126 brouard 8909: int i, j, nhstepm, hstepm;
8910: double *xp;
8911: double *gp, *gm;
8912: double **gradg, **trgradg;
1.208 brouard 8913: double **mgm, **mgp;
1.126 brouard 8914: double age,agelim;
8915: int theta;
8916:
8917: pstamp(ficresvpl);
1.288 brouard 8918: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
1.241 brouard 8919: fprintf(ficresvpl,"# Age ");
8920: if(nresult >=1)
8921: fprintf(ficresvpl," Result# ");
1.126 brouard 8922: for(i=1; i<=nlstate;i++)
8923: fprintf(ficresvpl," %1d-%1d",i,i);
8924: fprintf(ficresvpl,"\n");
8925:
8926: xp=vector(1,npar);
1.268 brouard 8927: dnewmpar=matrix(1,nlstate,1,npar);
1.126 brouard 8928: doldm=matrix(1,nlstate,1,nlstate);
8929:
8930: hstepm=1*YEARM; /* Every year of age */
8931: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
8932: agelim = AGESUP;
8933: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
8934: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8935: if (stepm >= YEARM) hstepm=1;
8936: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
8937: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 8938: mgp=matrix(1,npar,1,nlstate);
8939: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 8940: gp=vector(1,nlstate);
8941: gm=vector(1,nlstate);
8942:
8943: for(theta=1; theta <=npar; theta++){
8944: for(i=1; i<=npar; i++){ /* Computes gradient */
8945: xp[i] = x[i] + (i==theta ?delti[theta]:0);
8946: }
1.288 brouard 8947: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
8948: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
8949: /* else */
8950: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 8951: for(i=1;i<=nlstate;i++){
1.126 brouard 8952: gp[i] = prlim[i][i];
1.208 brouard 8953: mgp[theta][i] = prlim[i][i];
8954: }
1.126 brouard 8955: for(i=1; i<=npar; i++) /* Computes gradient */
8956: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 8957: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
8958: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
8959: /* else */
8960: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 8961: for(i=1;i<=nlstate;i++){
1.126 brouard 8962: gm[i] = prlim[i][i];
1.208 brouard 8963: mgm[theta][i] = prlim[i][i];
8964: }
1.126 brouard 8965: for(i=1;i<=nlstate;i++)
8966: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 8967: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 8968: } /* End theta */
8969:
8970: trgradg =matrix(1,nlstate,1,npar);
8971:
8972: for(j=1; j<=nlstate;j++)
8973: for(theta=1; theta <=npar; theta++)
8974: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 8975: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
8976: /* printf("\nmgm mgp %d ",(int)age); */
8977: /* for(j=1; j<=nlstate;j++){ */
8978: /* printf(" %d ",j); */
8979: /* for(theta=1; theta <=npar; theta++) */
8980: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
8981: /* printf("\n "); */
8982: /* } */
8983: /* } */
8984: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
8985: /* printf("\n gradg %d ",(int)age); */
8986: /* for(j=1; j<=nlstate;j++){ */
8987: /* printf("%d ",j); */
8988: /* for(theta=1; theta <=npar; theta++) */
8989: /* printf("%d %lf ",theta,gradg[theta][j]); */
8990: /* printf("\n "); */
8991: /* } */
8992: /* } */
1.126 brouard 8993:
8994: for(i=1;i<=nlstate;i++)
8995: varpl[i][(int)age] =0.;
1.209 brouard 8996: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.268 brouard 8997: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
8998: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 8999: }else{
1.268 brouard 9000: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9001: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 9002: }
1.126 brouard 9003: for(i=1;i<=nlstate;i++)
9004: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9005:
9006: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 9007: if(nresult >=1)
9008: fprintf(ficresvpl,"%d ",nres );
1.288 brouard 9009: for(i=1; i<=nlstate;i++){
1.126 brouard 9010: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1.288 brouard 9011: /* for(j=1;j<=nlstate;j++) */
9012: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
9013: }
1.126 brouard 9014: fprintf(ficresvpl,"\n");
9015: free_vector(gp,1,nlstate);
9016: free_vector(gm,1,nlstate);
1.208 brouard 9017: free_matrix(mgm,1,npar,1,nlstate);
9018: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 9019: free_matrix(gradg,1,npar,1,nlstate);
9020: free_matrix(trgradg,1,nlstate,1,npar);
9021: } /* End age */
9022:
9023: free_vector(xp,1,npar);
9024: free_matrix(doldm,1,nlstate,1,npar);
1.268 brouard 9025: free_matrix(dnewmpar,1,nlstate,1,nlstate);
9026:
9027: }
9028:
9029:
9030: /************ Variance of backprevalence limit ******************/
1.269 brouard 9031: 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 9032: {
9033: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
9034: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
9035:
9036: double **dnewmpar,**doldm;
9037: int i, j, nhstepm, hstepm;
9038: double *xp;
9039: double *gp, *gm;
9040: double **gradg, **trgradg;
9041: double **mgm, **mgp;
9042: double age,agelim;
9043: int theta;
9044:
9045: pstamp(ficresvbl);
9046: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
9047: fprintf(ficresvbl,"# Age ");
9048: if(nresult >=1)
9049: fprintf(ficresvbl," Result# ");
9050: for(i=1; i<=nlstate;i++)
9051: fprintf(ficresvbl," %1d-%1d",i,i);
9052: fprintf(ficresvbl,"\n");
9053:
9054: xp=vector(1,npar);
9055: dnewmpar=matrix(1,nlstate,1,npar);
9056: doldm=matrix(1,nlstate,1,nlstate);
9057:
9058: hstepm=1*YEARM; /* Every year of age */
9059: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
9060: agelim = AGEINF;
9061: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
9062: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
9063: if (stepm >= YEARM) hstepm=1;
9064: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
9065: gradg=matrix(1,npar,1,nlstate);
9066: mgp=matrix(1,npar,1,nlstate);
9067: mgm=matrix(1,npar,1,nlstate);
9068: gp=vector(1,nlstate);
9069: gm=vector(1,nlstate);
9070:
9071: for(theta=1; theta <=npar; theta++){
9072: for(i=1; i<=npar; i++){ /* Computes gradient */
9073: xp[i] = x[i] + (i==theta ?delti[theta]:0);
9074: }
9075: if(mobilavproj > 0 )
9076: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9077: else
9078: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9079: for(i=1;i<=nlstate;i++){
9080: gp[i] = bprlim[i][i];
9081: mgp[theta][i] = bprlim[i][i];
9082: }
9083: for(i=1; i<=npar; i++) /* Computes gradient */
9084: xp[i] = x[i] - (i==theta ?delti[theta]:0);
9085: if(mobilavproj > 0 )
9086: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9087: else
9088: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
9089: for(i=1;i<=nlstate;i++){
9090: gm[i] = bprlim[i][i];
9091: mgm[theta][i] = bprlim[i][i];
9092: }
9093: for(i=1;i<=nlstate;i++)
9094: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
9095: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
9096: } /* End theta */
9097:
9098: trgradg =matrix(1,nlstate,1,npar);
9099:
9100: for(j=1; j<=nlstate;j++)
9101: for(theta=1; theta <=npar; theta++)
9102: trgradg[j][theta]=gradg[theta][j];
9103: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9104: /* printf("\nmgm mgp %d ",(int)age); */
9105: /* for(j=1; j<=nlstate;j++){ */
9106: /* printf(" %d ",j); */
9107: /* for(theta=1; theta <=npar; theta++) */
9108: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
9109: /* printf("\n "); */
9110: /* } */
9111: /* } */
9112: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
9113: /* printf("\n gradg %d ",(int)age); */
9114: /* for(j=1; j<=nlstate;j++){ */
9115: /* printf("%d ",j); */
9116: /* for(theta=1; theta <=npar; theta++) */
9117: /* printf("%d %lf ",theta,gradg[theta][j]); */
9118: /* printf("\n "); */
9119: /* } */
9120: /* } */
9121:
9122: for(i=1;i<=nlstate;i++)
9123: varbpl[i][(int)age] =0.;
9124: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
9125: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9126: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9127: }else{
9128: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
9129: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
9130: }
9131: for(i=1;i<=nlstate;i++)
9132: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
9133:
9134: fprintf(ficresvbl,"%.0f ",age );
9135: if(nresult >=1)
9136: fprintf(ficresvbl,"%d ",nres );
9137: for(i=1; i<=nlstate;i++)
9138: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
9139: fprintf(ficresvbl,"\n");
9140: free_vector(gp,1,nlstate);
9141: free_vector(gm,1,nlstate);
9142: free_matrix(mgm,1,npar,1,nlstate);
9143: free_matrix(mgp,1,npar,1,nlstate);
9144: free_matrix(gradg,1,npar,1,nlstate);
9145: free_matrix(trgradg,1,nlstate,1,npar);
9146: } /* End age */
9147:
9148: free_vector(xp,1,npar);
9149: free_matrix(doldm,1,nlstate,1,npar);
9150: free_matrix(dnewmpar,1,nlstate,1,nlstate);
1.126 brouard 9151:
9152: }
9153:
9154: /************ Variance of one-step probabilities ******************/
9155: 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 9156: {
9157: int i, j=0, k1, l1, tj;
9158: int k2, l2, j1, z1;
9159: int k=0, l;
9160: int first=1, first1, first2;
1.326 brouard 9161: int nres=0; /* New */
1.222 brouard 9162: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
9163: double **dnewm,**doldm;
9164: double *xp;
9165: double *gp, *gm;
9166: double **gradg, **trgradg;
9167: double **mu;
9168: double age, cov[NCOVMAX+1];
9169: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
9170: int theta;
9171: char fileresprob[FILENAMELENGTH];
9172: char fileresprobcov[FILENAMELENGTH];
9173: char fileresprobcor[FILENAMELENGTH];
9174: double ***varpij;
9175:
9176: strcpy(fileresprob,"PROB_");
1.356 brouard 9177: strcat(fileresprob,fileresu);
1.222 brouard 9178: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
9179: printf("Problem with resultfile: %s\n", fileresprob);
9180: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
9181: }
9182: strcpy(fileresprobcov,"PROBCOV_");
9183: strcat(fileresprobcov,fileresu);
9184: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
9185: printf("Problem with resultfile: %s\n", fileresprobcov);
9186: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
9187: }
9188: strcpy(fileresprobcor,"PROBCOR_");
9189: strcat(fileresprobcor,fileresu);
9190: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
9191: printf("Problem with resultfile: %s\n", fileresprobcor);
9192: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
9193: }
9194: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9195: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
9196: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9197: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
9198: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9199: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
9200: pstamp(ficresprob);
9201: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
9202: fprintf(ficresprob,"# Age");
9203: pstamp(ficresprobcov);
9204: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
9205: fprintf(ficresprobcov,"# Age");
9206: pstamp(ficresprobcor);
9207: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
9208: fprintf(ficresprobcor,"# Age");
1.126 brouard 9209:
9210:
1.222 brouard 9211: for(i=1; i<=nlstate;i++)
9212: for(j=1; j<=(nlstate+ndeath);j++){
9213: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
9214: fprintf(ficresprobcov," p%1d-%1d ",i,j);
9215: fprintf(ficresprobcor," p%1d-%1d ",i,j);
9216: }
9217: /* fprintf(ficresprob,"\n");
9218: fprintf(ficresprobcov,"\n");
9219: fprintf(ficresprobcor,"\n");
9220: */
9221: xp=vector(1,npar);
9222: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9223: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9224: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
9225: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
9226: first=1;
9227: fprintf(ficgp,"\n# Routine varprob");
9228: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
9229: fprintf(fichtm,"\n");
9230:
1.288 brouard 9231: 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 9232: 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);
9233: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 9234: and drawn. It helps understanding how is the covariance between two incidences.\
9235: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 9236: 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 9237: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
9238: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
9239: standard deviations wide on each axis. <br>\
9240: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
9241: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
9242: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
9243:
1.222 brouard 9244: cov[1]=1;
9245: /* tj=cptcoveff; */
1.225 brouard 9246: tj = (int) pow(2,cptcoveff);
1.222 brouard 9247: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
9248: j1=0;
1.332 brouard 9249:
9250: for(nres=1;nres <=nresult; nres++){ /* For each resultline */
9251: for(j1=1; j1<=tj;j1++){ /* For any combination of dummy covariates, fixed and varying */
1.342 brouard 9252: /* 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 9253: if(tj != 1 && TKresult[nres]!= j1)
9254: continue;
9255:
9256: /* for(j1=1; j1<=tj;j1++){ /\* For each valid combination of covariates or only once*\/ */
9257: /* for(nres=1;nres <=1; nres++){ /\* For each resultline *\/ */
9258: /* /\* for(nres=1;nres <=nresult; nres++){ /\\* For each resultline *\\/ *\/ */
1.222 brouard 9259: if (cptcovn>0) {
1.334 brouard 9260: fprintf(ficresprob, "\n#********** Variable ");
9261: fprintf(ficresprobcov, "\n#********** Variable ");
9262: fprintf(ficgp, "\n#********** Variable ");
9263: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
9264: fprintf(ficresprobcor, "\n#********** Variable ");
9265:
9266: /* Including quantitative variables of the resultline to be done */
9267: for (z1=1; z1<=cptcovs; z1++){ /* Loop on each variable of this resultline */
1.343 brouard 9268: /* printf("Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model); */
1.338 brouard 9269: fprintf(ficlog,"Varprob modelresult[%d][%d]=%d model=1+age+%s \n",nres, z1, modelresult[nres][z1], model);
9270: /* 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 9271: if(Dummy[modelresult[nres][z1]]==0){/* Dummy variable of the variable in position modelresult in the model corresponding to z1 in resultline */
9272: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed referenced to model equation */
9273: 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 */
9274: 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 */
9275: 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 */
9276: 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 */
9277: 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 */
9278: fprintf(ficresprob,"fixed ");
9279: fprintf(ficresprobcov,"fixed ");
9280: fprintf(ficgp,"fixed ");
9281: fprintf(fichtmcov,"fixed ");
9282: fprintf(ficresprobcor,"fixed ");
9283: }else{
9284: fprintf(ficresprob,"varyi ");
9285: fprintf(ficresprobcov,"varyi ");
9286: fprintf(ficgp,"varyi ");
9287: fprintf(fichtmcov,"varyi ");
9288: fprintf(ficresprobcor,"varyi ");
9289: }
9290: }else if(Dummy[modelresult[nres][z1]]==1){ /* Quanti variable */
9291: /* For each selected (single) quantitative value */
1.337 brouard 9292: fprintf(ficresprob," V%d=%lg ",Tvqresult[nres][z1],Tqresult[nres][z1]);
1.334 brouard 9293: if(Fixed[modelresult[nres][z1]]==0){ /* Fixed */
9294: fprintf(ficresprob,"fixed ");
9295: fprintf(ficresprobcov,"fixed ");
9296: fprintf(ficgp,"fixed ");
9297: fprintf(fichtmcov,"fixed ");
9298: fprintf(ficresprobcor,"fixed ");
9299: }else{
9300: fprintf(ficresprob,"varyi ");
9301: fprintf(ficresprobcov,"varyi ");
9302: fprintf(ficgp,"varyi ");
9303: fprintf(fichtmcov,"varyi ");
9304: fprintf(ficresprobcor,"varyi ");
9305: }
9306: }else{
9307: 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 */
9308: 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 */
9309: exit(1);
9310: }
9311: } /* End loop on variable of this resultline */
9312: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,TnsdVar[Tvaraff[z1]])]); */
1.222 brouard 9313: fprintf(ficresprob, "**********\n#\n");
9314: fprintf(ficresprobcov, "**********\n#\n");
9315: fprintf(ficgp, "**********\n#\n");
9316: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
9317: fprintf(ficresprobcor, "**********\n#");
9318: if(invalidvarcomb[j1]){
9319: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
9320: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
9321: continue;
9322: }
9323: }
9324: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
9325: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
9326: gp=vector(1,(nlstate)*(nlstate+ndeath));
9327: gm=vector(1,(nlstate)*(nlstate+ndeath));
1.334 brouard 9328: for (age=bage; age<=fage; age ++){ /* Fo each age we feed the model equation with covariates, using precov as in hpxij() ? */
1.222 brouard 9329: cov[2]=age;
9330: if(nagesqr==1)
9331: cov[3]= age*age;
1.334 brouard 9332: /* New code end of combination but for each resultline */
9333: for(k1=1;k1<=cptcovt;k1++){ /* loop on model equation (including products) */
1.349 brouard 9334: if(Typevar[k1]==1 || Typevar[k1] ==3){ /* A product with age */
1.334 brouard 9335: cov[2+nagesqr+k1]=precov[nres][k1]*cov[2];
1.326 brouard 9336: }else{
1.334 brouard 9337: cov[2+nagesqr+k1]=precov[nres][k1];
1.326 brouard 9338: }
1.334 brouard 9339: }/* End of loop on model equation */
9340: /* Old code */
9341: /* /\* for (k=1; k<=cptcovn;k++) { *\/ */
9342: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; *\/ */
9343: /* for (k=1; k<=nsd;k++) { /\* For single dummy covariates only *\/ */
9344: /* /\* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates *\/ */
9345: /* cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,TnsdVar[TvarsD[k]])]; */
9346: /* /\*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*\//\* j1 1 2 3 4 */
9347: /* * 1 1 1 1 1 */
9348: /* * 2 2 1 1 1 */
9349: /* * 3 1 2 1 1 */
9350: /* *\/ */
9351: /* /\* nbcode[1][1]=0 nbcode[1][2]=1;*\/ */
9352: /* } */
9353: /* /\* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
9354: /* /\* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] *\/ */
9355: /* /\*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; *\/ */
9356: /* for (k=1; k<=cptcovage;k++){ /\* For product with age *\/ */
9357: /* if(Dummy[Tage[k]]==2){ /\* dummy with age *\/ */
9358: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,TnsdVar[Tvar[Tage[k]]])]*cov[2]; */
9359: /* /\* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9360: /* } else if(Dummy[Tage[k]]==3){ /\* quantitative with age *\/ */
9361: /* 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]); */
9362: /* /\* cov[2+nagesqr+Tage[k]]=meanq[k]/idq[k]*cov[2];/\\* Using the mean of quantitative variable Tvar[Tage[k]] /\\* Tqresult[nres][k]; *\\/ *\/ */
9363: /* /\* exit(1); *\/ */
9364: /* /\* cov[++k1]=Tqresult[nres][k]; *\/ */
9365: /* } */
9366: /* /\* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; *\/ */
9367: /* } */
9368: /* for (k=1; k<=cptcovprod;k++){/\* For product without age *\/ */
9369: /* if(Dummy[Tvard[k][1]]==0){ */
9370: /* if(Dummy[Tvard[k][2]]==0){ */
9371: /* 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]])]; */
9372: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9373: /* }else{ /\* Should we use the mean of the quantitative variables? *\/ */
9374: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,TnsdVar[Tvard[k][1]])] * Tqresult[nres][resultmodel[nres][k]]; */
9375: /* /\* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; *\/ */
9376: /* } */
9377: /* }else{ */
9378: /* if(Dummy[Tvard[k][2]]==0){ */
9379: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,TnsdVar[Tvard[k][2]])] * Tqinvresult[nres][TnsdVar[Tvard[k][1]]]; */
9380: /* /\* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; *\/ */
9381: /* }else{ */
9382: /* cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][TnsdVar[Tvard[k][1]]]* Tqinvresult[nres][TnsdVar[Tvard[k][2]]]; */
9383: /* /\* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; *\/ */
9384: /* } */
9385: /* } */
9386: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; *\/ */
9387: /* } */
1.326 brouard 9388: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
1.222 brouard 9389: for(theta=1; theta <=npar; theta++){
9390: for(i=1; i<=npar; i++)
9391: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 9392:
1.222 brouard 9393: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 9394:
1.222 brouard 9395: k=0;
9396: for(i=1; i<= (nlstate); i++){
9397: for(j=1; j<=(nlstate+ndeath);j++){
9398: k=k+1;
9399: gp[k]=pmmij[i][j];
9400: }
9401: }
1.220 brouard 9402:
1.222 brouard 9403: for(i=1; i<=npar; i++)
9404: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 9405:
1.222 brouard 9406: pmij(pmmij,cov,ncovmodel,xp,nlstate);
9407: k=0;
9408: for(i=1; i<=(nlstate); i++){
9409: for(j=1; j<=(nlstate+ndeath);j++){
9410: k=k+1;
9411: gm[k]=pmmij[i][j];
9412: }
9413: }
1.220 brouard 9414:
1.222 brouard 9415: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
9416: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
9417: }
1.126 brouard 9418:
1.222 brouard 9419: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
9420: for(theta=1; theta <=npar; theta++)
9421: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 9422:
1.222 brouard 9423: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
9424: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 9425:
1.222 brouard 9426: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 9427:
1.222 brouard 9428: k=0;
9429: for(i=1; i<=(nlstate); i++){
9430: for(j=1; j<=(nlstate+ndeath);j++){
9431: k=k+1;
9432: mu[k][(int) age]=pmmij[i][j];
9433: }
9434: }
9435: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
9436: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
9437: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 9438:
1.222 brouard 9439: /*printf("\n%d ",(int)age);
9440: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9441: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9442: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
9443: }*/
1.220 brouard 9444:
1.222 brouard 9445: fprintf(ficresprob,"\n%d ",(int)age);
9446: fprintf(ficresprobcov,"\n%d ",(int)age);
9447: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 9448:
1.222 brouard 9449: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
9450: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
9451: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
9452: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
9453: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
9454: }
9455: i=0;
9456: for (k=1; k<=(nlstate);k++){
9457: for (l=1; l<=(nlstate+ndeath);l++){
9458: i++;
9459: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
9460: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
9461: for (j=1; j<=i;j++){
9462: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
9463: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
9464: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
9465: }
9466: }
9467: }/* end of loop for state */
9468: } /* end of loop for age */
9469: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
9470: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
9471: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9472: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
9473:
9474: /* Confidence intervalle of pij */
9475: /*
9476: fprintf(ficgp,"\nunset parametric;unset label");
9477: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
9478: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
9479: 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);
9480: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
9481: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
9482: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
9483: */
9484:
9485: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
9486: first1=1;first2=2;
9487: for (k2=1; k2<=(nlstate);k2++){
9488: for (l2=1; l2<=(nlstate+ndeath);l2++){
9489: if(l2==k2) continue;
9490: j=(k2-1)*(nlstate+ndeath)+l2;
9491: for (k1=1; k1<=(nlstate);k1++){
9492: for (l1=1; l1<=(nlstate+ndeath);l1++){
9493: if(l1==k1) continue;
9494: i=(k1-1)*(nlstate+ndeath)+l1;
9495: if(i<=j) continue;
9496: for (age=bage; age<=fage; age ++){
9497: if ((int)age %5==0){
9498: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
9499: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
9500: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
9501: mu1=mu[i][(int) age]/stepm*YEARM ;
9502: mu2=mu[j][(int) age]/stepm*YEARM;
9503: c12=cv12/sqrt(v1*v2);
9504: /* Computing eigen value of matrix of covariance */
9505: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9506: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
9507: if ((lc2 <0) || (lc1 <0) ){
9508: if(first2==1){
9509: first1=0;
9510: 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);
9511: }
9512: 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);
9513: /* lc1=fabs(lc1); */ /* If we want to have them positive */
9514: /* lc2=fabs(lc2); */
9515: }
1.220 brouard 9516:
1.222 brouard 9517: /* Eigen vectors */
1.280 brouard 9518: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
9519: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9520: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
9521: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
9522: }else
9523: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
1.222 brouard 9524: /*v21=sqrt(1.-v11*v11); *//* error */
9525: v21=(lc1-v1)/cv12*v11;
9526: v12=-v21;
9527: v22=v11;
9528: tnalp=v21/v11;
9529: if(first1==1){
9530: first1=0;
9531: 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);
9532: }
9533: 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);
9534: /*printf(fignu*/
9535: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
9536: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
9537: if(first==1){
9538: first=0;
9539: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
9540: fprintf(ficgp,"\nset parametric;unset label");
9541: 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);
9542: fprintf(ficgp,"\nset ter svg size 640, 480");
1.266 brouard 9543: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 9544: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 9545: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 9546: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
9547: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9548: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9549: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
9550: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9551: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9552: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9553: 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 9554: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
9555: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
1.222 brouard 9556: }else{
9557: first=0;
9558: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
9559: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
9560: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
9561: 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 9562: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
9563: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
1.222 brouard 9564: }/* if first */
9565: } /* age mod 5 */
9566: } /* end loop age */
9567: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
9568: first=1;
9569: } /*l12 */
9570: } /* k12 */
9571: } /*l1 */
9572: }/* k1 */
1.332 brouard 9573: } /* loop on combination of covariates j1 */
1.326 brouard 9574: } /* loop on nres */
1.222 brouard 9575: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
9576: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
9577: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
9578: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
9579: free_vector(xp,1,npar);
9580: fclose(ficresprob);
9581: fclose(ficresprobcov);
9582: fclose(ficresprobcor);
9583: fflush(ficgp);
9584: fflush(fichtmcov);
9585: }
1.126 brouard 9586:
9587:
9588: /******************* Printing html file ***********/
1.201 brouard 9589: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 9590: int lastpass, int stepm, int weightopt, char model[],\
9591: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.296 brouard 9592: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
9593: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
9594: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
1.359 ! brouard 9595: int jj1, k1, cpt, nres;
1.319 brouard 9596: /* In fact some results are already printed in fichtm which is open */
1.126 brouard 9597: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
9598: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
9599: </ul>");
1.319 brouard 9600: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
9601: /* </ul>", model); */
1.214 brouard 9602: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
9603: 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",
9604: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
1.332 brouard 9605: 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 9606: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
9607: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 9608: fprintf(fichtm,"\
9609: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 9610: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 9611: fprintf(fichtm,"\
1.217 brouard 9612: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
9613: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
9614: fprintf(fichtm,"\
1.288 brouard 9615: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 9616: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 9617: fprintf(fichtm,"\
1.288 brouard 9618: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.217 brouard 9619: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
9620: fprintf(fichtm,"\
1.211 brouard 9621: - (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 9622: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 9623: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 9624: if(prevfcast==1){
9625: fprintf(fichtm,"\
9626: - Prevalence projections by age and states: \
1.201 brouard 9627: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 9628: }
1.126 brouard 9629:
9630:
1.225 brouard 9631: m=pow(2,cptcoveff);
1.222 brouard 9632: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 9633:
1.317 brouard 9634: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
1.264 brouard 9635:
9636: jj1=0;
9637:
9638: fprintf(fichtm," \n<ul>");
1.337 brouard 9639: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9640: /* k1=nres; */
1.338 brouard 9641: k1=TKresult[nres];
9642: if(TKresult[nres]==0)k1=1; /* To be checked for no result */
1.337 brouard 9643: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9644: /* if(m != 1 && TKresult[nres]!= k1) */
9645: /* continue; */
1.264 brouard 9646: jj1++;
9647: if (cptcovn > 0) {
9648: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
1.337 brouard 9649: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
9650: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9651: }
1.337 brouard 9652: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9653: /* fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9654: /* } */
9655: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9656: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9657: /* } */
1.264 brouard 9658: fprintf(fichtm,"\">");
9659:
9660: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9661: fprintf(fichtm,"************ Results for covariates");
1.337 brouard 9662: for (cpt=1; cpt<=cptcovs;cpt++){
9663: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9664: }
1.337 brouard 9665: /* fprintf(fichtm,"************ Results for covariates"); */
9666: /* for (cpt=1; cpt<=cptcoveff;cpt++){ */
9667: /* fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); */
9668: /* } */
9669: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9670: /* fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9671: /* } */
1.264 brouard 9672: if(invalidvarcomb[k1]){
9673: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9674: continue;
9675: }
9676: fprintf(fichtm,"</a></li>");
9677: } /* cptcovn >0 */
9678: }
1.317 brouard 9679: fprintf(fichtm," \n</ul>");
1.264 brouard 9680:
1.222 brouard 9681: jj1=0;
1.237 brouard 9682:
1.337 brouard 9683: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9684: /* k1=nres; */
1.338 brouard 9685: k1=TKresult[nres];
9686: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 9687: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9688: /* if(m != 1 && TKresult[nres]!= k1) */
9689: /* continue; */
1.220 brouard 9690:
1.222 brouard 9691: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9692: jj1++;
9693: if (cptcovn > 0) {
1.264 brouard 9694: fprintf(fichtm,"\n<p><a name=\"rescov");
1.337 brouard 9695: for (cpt=1; cpt<=cptcovs;cpt++){
9696: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.264 brouard 9697: }
1.337 brouard 9698: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
9699: /* fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); */
9700: /* } */
1.264 brouard 9701: fprintf(fichtm,"\"</a>");
9702:
1.222 brouard 9703: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.337 brouard 9704: for (cpt=1; cpt<=cptcovs;cpt++){
9705: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9706: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.237 brouard 9707: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
9708: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 9709: }
1.230 brouard 9710: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.338 brouard 9711: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.222 brouard 9712: if(invalidvarcomb[k1]){
9713: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
9714: printf("\nCombination (%d) ignored because no cases \n",k1);
9715: continue;
9716: }
9717: }
9718: /* aij, bij */
1.259 brouard 9719: 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 9720: <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 9721: /* Pij */
1.241 brouard 9722: 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> \
9723: <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 9724: /* Quasi-incidences */
9725: 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 9726: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 9727: 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 9728: 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> \
9729: <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 9730: /* Survival functions (period) in state j */
9731: for(cpt=1; cpt<=nlstate;cpt++){
1.359 ! brouard 9732: 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 9733: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9734: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.222 brouard 9735: }
9736: /* State specific survival functions (period) */
9737: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 9738: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
1.359 ! brouard 9739: 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 9740: <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);
9741: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
9742: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.222 brouard 9743: }
1.288 brouard 9744: /* Period (forward stable) prevalence in each health state */
1.222 brouard 9745: for(cpt=1; cpt<=nlstate;cpt++){
1.359 ! brouard 9746: 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 9747: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJ_"),subdirf2(optionfilefiname,"PIJ_"));
1.329 brouard 9748: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.222 brouard 9749: }
1.296 brouard 9750: if(prevbcast==1){
1.288 brouard 9751: /* Backward prevalence in each health state */
1.222 brouard 9752: for(cpt=1; cpt<=nlstate;cpt++){
1.338 brouard 9753: 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);
9754: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"PIJB_"),subdirf2(optionfilefiname,"PIJB_"));
9755: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">" ,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.222 brouard 9756: }
1.217 brouard 9757: }
1.222 brouard 9758: if(prevfcast==1){
1.288 brouard 9759: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
1.222 brouard 9760: for(cpt=1; cpt<=nlstate;cpt++){
1.314 brouard 9761: 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);
9762: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
9763: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
9764: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.222 brouard 9765: }
9766: }
1.296 brouard 9767: if(prevbcast==1){
1.268 brouard 9768: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
9769: for(cpt=1; cpt<=nlstate;cpt++){
1.273 brouard 9770: 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 9771: 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 \
! 9772: 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 9773: 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);
9774: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
9775: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
1.268 brouard 9776: }
9777: }
1.220 brouard 9778:
1.222 brouard 9779: for(cpt=1; cpt<=nlstate;cpt++) {
1.314 brouard 9780: 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);
9781: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
9782: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
1.222 brouard 9783: }
9784: /* } /\* end i1 *\/ */
1.337 brouard 9785: }/* End k1=nres */
1.222 brouard 9786: fprintf(fichtm,"</ul>");
1.126 brouard 9787:
1.222 brouard 9788: fprintf(fichtm,"\
1.126 brouard 9789: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 9790: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 9791: - 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 9792: But because parameters are usually highly correlated (a higher incidence of disability \
9793: and a higher incidence of recovery can give very close observed transition) it might \
9794: be very useful to look not only at linear confidence intervals estimated from the \
9795: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
9796: (parameters) of the logistic regression, it might be more meaningful to visualize the \
9797: covariance matrix of the one-step probabilities. \
9798: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 9799:
1.222 brouard 9800: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
9801: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
9802: fprintf(fichtm,"\
1.126 brouard 9803: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9804: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 9805:
1.222 brouard 9806: fprintf(fichtm,"\
1.126 brouard 9807: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 9808: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
9809: fprintf(fichtm,"\
1.126 brouard 9810: - 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): \
9811: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 9812: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 9813: fprintf(fichtm,"\
1.126 brouard 9814: - (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): \
9815: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 9816: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 9817: fprintf(fichtm,"\
1.288 brouard 9818: - 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 9819: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
9820: fprintf(fichtm,"\
1.128 brouard 9821: - 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 9822: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
9823: fprintf(fichtm,"\
1.288 brouard 9824: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 9825: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 9826:
9827: /* if(popforecast==1) fprintf(fichtm,"\n */
9828: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
9829: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
9830: /* <br>",fileres,fileres,fileres,fileres); */
9831: /* else */
1.338 brouard 9832: /* 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 9833: fflush(fichtm);
1.126 brouard 9834:
1.225 brouard 9835: m=pow(2,cptcoveff);
1.222 brouard 9836: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 9837:
1.317 brouard 9838: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
9839:
9840: jj1=0;
9841:
9842: fprintf(fichtm," \n<ul>");
1.337 brouard 9843: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
9844: /* k1=nres; */
1.338 brouard 9845: k1=TKresult[nres];
1.337 brouard 9846: /* for(k1=1; k1<=m;k1++){ /\* For each combination of covariate *\/ */
9847: /* if(m != 1 && TKresult[nres]!= k1) */
9848: /* continue; */
1.317 brouard 9849: jj1++;
9850: if (cptcovn > 0) {
9851: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
1.337 brouard 9852: for (cpt=1; cpt<=cptcovs;cpt++){
9853: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9854: }
9855: fprintf(fichtm,"\">");
9856:
9857: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
9858: fprintf(fichtm,"************ Results for covariates");
1.337 brouard 9859: for (cpt=1; cpt<=cptcovs;cpt++){
9860: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9861: }
9862: if(invalidvarcomb[k1]){
9863: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
9864: continue;
9865: }
9866: fprintf(fichtm,"</a></li>");
9867: } /* cptcovn >0 */
1.337 brouard 9868: } /* End nres */
1.317 brouard 9869: fprintf(fichtm," \n</ul>");
9870:
1.222 brouard 9871: jj1=0;
1.237 brouard 9872:
1.241 brouard 9873: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 9874: /* k1=nres; */
1.338 brouard 9875: k1=TKresult[nres];
9876: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 9877: /* for(k1=1; k1<=m;k1++){ */
9878: /* if(m != 1 && TKresult[nres]!= k1) */
9879: /* continue; */
1.222 brouard 9880: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
9881: jj1++;
1.126 brouard 9882: if (cptcovn > 0) {
1.317 brouard 9883: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
1.337 brouard 9884: for (cpt=1; cpt<=cptcovs;cpt++){
9885: fprintf(fichtm,"_V%d=%lg_",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.317 brouard 9886: }
9887: fprintf(fichtm,"\"</a>");
9888:
1.126 brouard 9889: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.337 brouard 9890: for (cpt=1; cpt<=cptcovs;cpt++){ /**< cptcoveff number of variables */
9891: fprintf(fichtm," V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
9892: printf(" V%d=%lg ",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);
1.237 brouard 9893: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
1.317 brouard 9894: }
1.237 brouard 9895:
1.338 brouard 9896: fprintf(fichtm," (model=1+age+%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.220 brouard 9897:
1.222 brouard 9898: if(invalidvarcomb[k1]){
9899: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
9900: continue;
9901: }
1.337 brouard 9902: } /* If cptcovn >0 */
1.126 brouard 9903: for(cpt=1; cpt<=nlstate;cpt++) {
1.258 brouard 9904: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
1.314 brouard 9905: 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);
9906: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
9907: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
1.126 brouard 9908: }
9909: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.314 brouard 9910: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \
1.128 brouard 9911: true period expectancies (those weighted with period prevalences are also\
9912: drawn in addition to the population based expectancies computed using\
1.314 brouard 9913: 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);
9914: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
9915: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 9916: /* } /\* end i1 *\/ */
1.241 brouard 9917: }/* End nres */
1.222 brouard 9918: fprintf(fichtm,"</ul>");
9919: fflush(fichtm);
1.126 brouard 9920: }
9921:
9922: /******************* Gnuplot file **************/
1.296 brouard 9923: 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 9924:
1.354 brouard 9925: char dirfileres[256],optfileres[256];
9926: char gplotcondition[256], gplotlabel[256];
1.343 brouard 9927: 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.211 brouard 9928: int lv=0, vlv=0, kl=0;
1.130 brouard 9929: int ng=0;
1.201 brouard 9930: int vpopbased;
1.223 brouard 9931: int ioffset; /* variable offset for columns */
1.270 brouard 9932: int iyearc=1; /* variable column for year of projection */
9933: int iagec=1; /* variable column for age of projection */
1.235 brouard 9934: int nres=0; /* Index of resultline */
1.266 brouard 9935: int istart=1; /* For starting graphs in projections */
1.219 brouard 9936:
1.126 brouard 9937: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
9938: /* printf("Problem with file %s",optionfilegnuplot); */
9939: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
9940: /* } */
9941:
9942: /*#ifdef windows */
9943: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 9944: /*#endif */
1.225 brouard 9945: m=pow(2,cptcoveff);
1.126 brouard 9946:
1.274 brouard 9947: /* diagram of the model */
9948: fprintf(ficgp,"\n#Diagram of the model \n");
9949: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
9950: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
9951: 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);
9952:
1.343 brouard 9953: 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 9954: fprintf(ficgp,"\n#show arrow\nunset label\n");
9955: 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);
9956: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
9957: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
9958: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
9959: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
9960:
1.202 brouard 9961: /* Contribution to likelihood */
9962: /* Plot the probability implied in the likelihood */
1.223 brouard 9963: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
9964: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
9965: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
9966: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 9967: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 9968: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
9969: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 9970: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
9971: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
9972: 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));
9973: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
9974: 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));
9975: for (i=1; i<= nlstate ; i ++) {
9976: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
9977: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
9978: 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);
9979: for (j=2; j<= nlstate+ndeath ; j ++) {
9980: 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);
9981: }
9982: fprintf(ficgp,";\nset out; unset ylabel;\n");
9983: }
9984: /* 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 */
9985: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
9986: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
9987: fprintf(ficgp,"\nset out;unset log\n");
9988: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 9989:
1.343 brouard 9990: /* Plot the probability implied in the likelihood by covariate value */
9991: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
9992: /* if(debugILK==1){ */
9993: for(kf=1; kf <= ncovf; kf++){ /* For each simple dummy covariate of the model */
1.347 brouard 9994: kvar=Tvar[TvarFind[kf]]; /* variable name */
9995: /* k=18+Tvar[TvarFind[kf]];/\*offset because there are 18 columns in the ILK_ file but could be placed else where *\/ */
1.350 brouard 9996: /* k=18+kf;/\*offset because there are 18 columns in the ILK_ file *\/ */
1.356 brouard 9997: /* k=19+kf;/\*offset because there are 19 columns in the ILK_ file *\/ */
1.355 brouard 9998: 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 9999: for (i=1; i<= nlstate ; i ++) {
10000: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10001: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
1.348 brouard 10002: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10003: 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);
10004: for (j=2; j<= nlstate+ndeath ; j ++) {
10005: 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);
10006: }
10007: }else{
10008: 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);
10009: for (j=2; j<= nlstate+ndeath ; j ++) {
10010: 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);
10011: }
1.343 brouard 10012: }
10013: fprintf(ficgp,";\nset out; unset ylabel;\n");
10014: }
10015: } /* End of each covariate dummy */
10016: for(ncovv=1, iposold=0, kk=0; ncovv <= ncovvt ; ncovv++){
10017: /* Other example V1 + V3 + V5 + age*V1 + age*V3 + age*V5 + V1*V3 + V3*V5 + V1*V5
10018: * kmodel = 1 2 3 4 5 6 7 8 9
10019: * varying 1 2 3 4 5
10020: * ncovv 1 2 3 4 5 6 7 8
10021: * TvarVV[ncovv] V3 5 1 3 3 5 1 5
10022: * TvarVVind[ncovv]=kmodel 2 3 7 7 8 8 9 9
10023: * TvarFind[kmodel] 1 0 0 0 0 0 0 0 0
10024: * kdata ncovcol=[V1 V2] nqv=0 ntv=[V3 V4] nqtv=V5
10025: * Dummy[kmodel] 0 0 1 2 2 3 1 1 1
10026: */
10027: ipos=TvarVVind[ncovv]; /* TvarVVind={2, 5, 5] gives the position in the model of the ncovv th varying covariate */
10028: kvar=TvarVV[ncovv]; /* TvarVV={3, 1, 3} gives the name of each varying covariate */
10029: /* 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]); */
10030: if(ipos!=iposold){ /* Not a product or first of a product */
10031: /* printf(" %d",ipos); */
10032: /* fprintf(ficresilk," V%d",TvarVV[ncovv]); */
10033: /* printf(" DebugILK ficgp suite ipos=%d != iposold=%d\n", ipos, iposold); */
10034: kk++; /* Position of the ncovv column in ILK_ */
10035: k=18+ncovf+kk; /*offset because there are 18 columns in the ILK_ file plus ncovf fixed covariate */
10036: 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) */
10037: for (i=1; i<= nlstate ; i ++) {
10038: fprintf(ficgp,"\nset out \"%s-p%dj-%d.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i,kvar);
10039: fprintf(ficgp,"unset log;\n# For each simple dummy covariate of the model \n plot \"%s\"",subdirf(fileresilk));
10040:
1.348 brouard 10041: /* printf("Before DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
1.343 brouard 10042: if(gnuplotversion >=5.2){ /* Former gnuplot versions do not have variable pointsize!! */
10043: /* printf("DebugILK gnuplotversion=%g >=5.2\n",gnuplotversion); */
10044: 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);
10045: for (j=2; j<= nlstate+ndeath ; j ++) {
10046: 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);
10047: }
10048: }else{
10049: /* printf("DebugILK gnuplotversion=%g <5.2\n",gnuplotversion); */
10050: 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);
10051: for (j=2; j<= nlstate+ndeath ; j ++) {
10052: 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);
10053: }
10054: }
10055: fprintf(ficgp,";\nset out; unset ylabel;\n");
10056: }
10057: }/* End if dummy varying */
10058: }else{ /*Product */
10059: /* printf("*"); */
10060: /* fprintf(ficresilk,"*"); */
10061: }
10062: iposold=ipos;
10063: } /* For each time varying covariate */
10064: /* } /\* debugILK==1 *\/ */
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_")); */
10070:
10071:
10072:
1.126 brouard 10073: strcpy(dirfileres,optionfilefiname);
10074: strcpy(optfileres,"vpl");
1.223 brouard 10075: /* 1eme*/
1.238 brouard 10076: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
1.337 brouard 10077: /* for (k1=1; k1<= m ; k1 ++){ /\* For each valid combination of covariate *\/ */
1.236 brouard 10078: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10079: k1=TKresult[nres];
1.338 brouard 10080: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.238 brouard 10081: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
1.337 brouard 10082: /* if(m != 1 && TKresult[nres]!= k1) */
10083: /* continue; */
1.238 brouard 10084: /* We are interested in selected combination by the resultline */
1.246 brouard 10085: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
1.288 brouard 10086: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.264 brouard 10087: strcpy(gplotlabel,"(");
1.337 brouard 10088: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10089: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10090: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10091:
10092: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate k get corresponding value lv for combination k1 *\/ */
10093: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the value of the covariate corresponding to k1 combination *\\/ *\/ */
10094: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10095: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10096: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10097: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10098: /* vlv= nbcode[Tvaraff[k]][lv]; /\* vlv is the value of the covariate lv, 0 or 1 *\/ */
10099: /* /\* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv *\/ */
10100: /* /\* printf(" V%d=%d ",Tvaraff[k],vlv); *\/ */
10101: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10102: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10103: /* } */
10104: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10105: /* /\* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); *\/ */
10106: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10107: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.264 brouard 10108: }
10109: strcpy(gplotlabel+strlen(gplotlabel),")");
1.246 brouard 10110: /* printf("\n#\n"); */
1.238 brouard 10111: fprintf(ficgp,"\n#\n");
10112: if(invalidvarcomb[k1]){
1.260 brouard 10113: /*k1=k1-1;*/ /* To be checked */
1.238 brouard 10114: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10115: continue;
10116: }
1.235 brouard 10117:
1.241 brouard 10118: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
10119: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
1.276 brouard 10120: /* 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 10121: fprintf(ficgp,"set title \"Alive state %d %s model=1+age+%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
1.260 brouard 10122: 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);
10123: /* 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); */
10124: /* k1-1 error should be nres-1*/
1.238 brouard 10125: for (i=1; i<= nlstate ; i ++) {
10126: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10127: else fprintf(ficgp," %%*lf (%%*lf)");
10128: }
1.288 brouard 10129: 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 10130: for (i=1; i<= nlstate ; i ++) {
10131: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10132: else fprintf(ficgp," %%*lf (%%*lf)");
10133: }
1.260 brouard 10134: 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 10135: for (i=1; i<= nlstate ; i ++) {
10136: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10137: else fprintf(ficgp," %%*lf (%%*lf)");
10138: }
1.265 brouard 10139: /* 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)); */
10140:
10141: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
10142: if(cptcoveff ==0){
1.271 brouard 10143: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
1.265 brouard 10144: }else{
10145: kl=0;
10146: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
1.332 brouard 10147: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10148: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
1.265 brouard 10149: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10150: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10151: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10152: vlv= nbcode[Tvaraff[k]][lv];
10153: kl++;
10154: /* 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 *\/ */
10155: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10156: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10157: /* '' 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*/
10158: if(k==cptcoveff){
10159: 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], \
10160: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
10161: }else{
10162: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
10163: kl++;
10164: }
10165: } /* end covariate */
10166: } /* end if no covariate */
10167:
1.296 brouard 10168: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
1.238 brouard 10169: /* 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 10170: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 10171: if(cptcoveff ==0){
1.245 brouard 10172: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
1.238 brouard 10173: }else{
10174: kl=0;
10175: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
1.332 brouard 10176: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to k1 combination and kth covariate *\/ */
10177: lv=codtabm(k1,TnsdVar[Tvaraff[k]]);
1.238 brouard 10178: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10179: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10180: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.332 brouard 10181: /* vlv= nbcode[Tvaraff[k]][lv]; */
10182: vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])];
1.223 brouard 10183: kl++;
1.238 brouard 10184: /* 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 *\/ */
10185: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10186: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10187: /* '' 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*/
10188: if(k==cptcoveff){
1.245 brouard 10189: 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 10190: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 10191: }else{
1.332 brouard 10192: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]);
1.238 brouard 10193: kl++;
10194: }
10195: } /* end covariate */
10196: } /* end if no covariate */
1.296 brouard 10197: if(prevbcast == 1){
1.268 brouard 10198: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
10199: /* k1-1 error should be nres-1*/
10200: for (i=1; i<= nlstate ; i ++) {
10201: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10202: else fprintf(ficgp," %%*lf (%%*lf)");
10203: }
1.271 brouard 10204: 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 10205: for (i=1; i<= nlstate ; i ++) {
10206: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10207: else fprintf(ficgp," %%*lf (%%*lf)");
10208: }
1.276 brouard 10209: 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 10210: for (i=1; i<= nlstate ; i ++) {
10211: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
10212: else fprintf(ficgp," %%*lf (%%*lf)");
10213: }
1.274 brouard 10214: fprintf(ficgp,"\" t\"\" w l lt 4");
1.268 brouard 10215: } /* end if backprojcast */
1.296 brouard 10216: } /* end if prevbcast */
1.276 brouard 10217: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
10218: fprintf(ficgp,"\nset out ;unset title;\n");
1.238 brouard 10219: } /* nres */
1.337 brouard 10220: /* } /\* k1 *\/ */
1.201 brouard 10221: } /* cpt */
1.235 brouard 10222:
10223:
1.126 brouard 10224: /*2 eme*/
1.337 brouard 10225: /* for (k1=1; k1<= m ; k1 ++){ */
1.238 brouard 10226: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10227: k1=TKresult[nres];
1.338 brouard 10228: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10229: /* if(m != 1 && TKresult[nres]!= k1) */
10230: /* continue; */
1.238 brouard 10231: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.264 brouard 10232: strcpy(gplotlabel,"(");
1.337 brouard 10233: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10234: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10235: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10236: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10237: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10238: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10239: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10240: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10241: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10242: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10243: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10244: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10245: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10246: /* } */
10247: /* /\* for(k=1; k <= ncovds; k++){ *\/ */
10248: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10249: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10250: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
10251: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.238 brouard 10252: }
1.264 brouard 10253: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 10254: fprintf(ficgp,"\n#\n");
1.223 brouard 10255: if(invalidvarcomb[k1]){
10256: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10257: continue;
10258: }
1.219 brouard 10259:
1.241 brouard 10260: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 10261: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.264 brouard 10262: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
10263: if(vpopbased==0){
1.238 brouard 10264: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
1.264 brouard 10265: }else
1.238 brouard 10266: fprintf(ficgp,"\nreplot ");
10267: for (i=1; i<= nlstate+1 ; i ++) {
10268: k=2*i;
1.261 brouard 10269: 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);
1.238 brouard 10270: for (j=1; j<= nlstate+1 ; j ++) {
10271: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10272: else fprintf(ficgp," %%*lf (%%*lf)");
10273: }
10274: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
10275: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
1.261 brouard 10276: 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 10277: for (j=1; j<= nlstate+1 ; j ++) {
10278: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10279: else fprintf(ficgp," %%*lf (%%*lf)");
10280: }
10281: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.261 brouard 10282: 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 10283: for (j=1; j<= nlstate+1 ; j ++) {
10284: if (j==i) fprintf(ficgp," %%lf (%%lf)");
10285: else fprintf(ficgp," %%*lf (%%*lf)");
10286: }
10287: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
10288: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
10289: } /* state */
10290: } /* vpopbased */
1.264 brouard 10291: 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 10292: } /* end nres */
1.337 brouard 10293: /* } /\* k1 end 2 eme*\/ */
1.238 brouard 10294:
10295:
10296: /*3eme*/
1.337 brouard 10297: /* for (k1=1; k1<= m ; k1 ++){ */
1.238 brouard 10298: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10299: k1=TKresult[nres];
1.338 brouard 10300: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10301: /* if(m != 1 && TKresult[nres]!= k1) */
10302: /* continue; */
1.238 brouard 10303:
1.332 brouard 10304: for (cpt=1; cpt<= nlstate ; cpt ++) { /* Fragile no verification of covariate values */
1.261 brouard 10305: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.264 brouard 10306: strcpy(gplotlabel,"(");
1.337 brouard 10307: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10308: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10309: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10310: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10311: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10312: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10313: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10314: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10315: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10316: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10317: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10318: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10319: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10320: /* } */
10321: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10322: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10323: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][resultmodel[nres][k4]]); */
10324: }
1.264 brouard 10325: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10326: fprintf(ficgp,"\n#\n");
10327: if(invalidvarcomb[k1]){
10328: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10329: continue;
10330: }
10331:
10332: /* k=2+nlstate*(2*cpt-2); */
10333: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 10334: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.264 brouard 10335: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
1.238 brouard 10336: fprintf(ficgp,"set ter svg size 640, 480\n\
1.261 brouard 10337: 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 10338: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10339: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10340: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
10341: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
10342: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
10343: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 10344:
1.238 brouard 10345: */
10346: for (i=1; i< nlstate ; i ++) {
1.261 brouard 10347: 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 10348: /* 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 10349:
1.238 brouard 10350: }
1.261 brouard 10351: 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 10352: }
1.264 brouard 10353: fprintf(ficgp,"\nunset label;\n");
1.238 brouard 10354: } /* end nres */
1.337 brouard 10355: /* } /\* end kl 3eme *\/ */
1.126 brouard 10356:
1.223 brouard 10357: /* 4eme */
1.201 brouard 10358: /* Survival functions (period) from state i in state j by initial state i */
1.337 brouard 10359: /* for (k1=1; k1<=m; k1++){ /\* For each covariate and each value *\/ */
1.238 brouard 10360: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10361: k1=TKresult[nres];
1.338 brouard 10362: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10363: /* if(m != 1 && TKresult[nres]!= k1) */
10364: /* continue; */
1.238 brouard 10365: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
1.264 brouard 10366: strcpy(gplotlabel,"(");
1.337 brouard 10367: fprintf(ficgp,"\n#\n#\n# Survival functions in state %d : 'LIJ_' files, cov=%d state=%d", cpt, k1, cpt);
10368: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10369: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10370: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10371: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10372: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10373: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10374: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10375: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10376: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10377: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10378: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10379: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10380: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10381: /* } */
10382: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10383: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10384: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.238 brouard 10385: }
1.264 brouard 10386: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10387: fprintf(ficgp,"\n#\n");
10388: if(invalidvarcomb[k1]){
10389: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10390: continue;
1.223 brouard 10391: }
1.238 brouard 10392:
1.241 brouard 10393: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.264 brouard 10394: 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 10395: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10396: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10397: k=3;
10398: for (i=1; i<= nlstate ; i ++){
10399: if(i==1){
10400: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10401: }else{
10402: fprintf(ficgp,", '' ");
10403: }
10404: l=(nlstate+ndeath)*(i-1)+1;
10405: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10406: for (j=2; j<= nlstate+ndeath ; j ++)
10407: fprintf(ficgp,"+$%d",k+l+j-1);
10408: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
10409: } /* nlstate */
1.264 brouard 10410: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 10411: } /* end cpt state*/
10412: } /* end nres */
1.337 brouard 10413: /* } /\* end covariate k1 *\/ */
1.238 brouard 10414:
1.220 brouard 10415: /* 5eme */
1.201 brouard 10416: /* Survival functions (period) from state i in state j by final state j */
1.337 brouard 10417: /* for (k1=1; k1<= m ; k1++){ /\* For each covariate combination if any *\/ */
1.238 brouard 10418: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10419: k1=TKresult[nres];
1.338 brouard 10420: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10421: /* if(m != 1 && TKresult[nres]!= k1) */
10422: /* continue; */
1.238 brouard 10423: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.264 brouard 10424: strcpy(gplotlabel,"(");
1.238 brouard 10425: 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 10426: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10427: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10428: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10429: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10430: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10431: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10432: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10433: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10434: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10435: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10436: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10437: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10438: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10439: /* } */
10440: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10441: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10442: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.238 brouard 10443: }
1.264 brouard 10444: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 10445: fprintf(ficgp,"\n#\n");
10446: if(invalidvarcomb[k1]){
10447: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10448: continue;
10449: }
1.227 brouard 10450:
1.241 brouard 10451: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.264 brouard 10452: 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 10453: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
10454: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10455: k=3;
10456: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10457: if(j==1)
10458: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10459: else
10460: fprintf(ficgp,", '' ");
10461: l=(nlstate+ndeath)*(cpt-1) +j;
10462: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
10463: /* for (i=2; i<= nlstate+ndeath ; i ++) */
10464: /* fprintf(ficgp,"+$%d",k+l+i-1); */
10465: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
10466: } /* nlstate */
10467: fprintf(ficgp,", '' ");
10468: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
10469: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
10470: l=(nlstate+ndeath)*(cpt-1) +j;
10471: if(j < nlstate)
10472: fprintf(ficgp,"$%d +",k+l);
10473: else
10474: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
10475: }
1.264 brouard 10476: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 10477: } /* end cpt state*/
1.337 brouard 10478: /* } /\* end covariate *\/ */
1.238 brouard 10479: } /* end nres */
1.227 brouard 10480:
1.220 brouard 10481: /* 6eme */
1.202 brouard 10482: /* CV preval stable (period) for each covariate */
1.337 brouard 10483: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10484: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10485: k1=TKresult[nres];
1.338 brouard 10486: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10487: /* if(m != 1 && TKresult[nres]!= k1) */
10488: /* continue; */
1.255 brouard 10489: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
1.264 brouard 10490: strcpy(gplotlabel,"(");
1.288 brouard 10491: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10492: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10493: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10494: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10495: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10496: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10497: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10498: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10499: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10500: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10501: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10502: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10503: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10504: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10505: /* } */
10506: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10507: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10508: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10509: }
1.264 brouard 10510: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 10511: fprintf(ficgp,"\n#\n");
1.223 brouard 10512: if(invalidvarcomb[k1]){
1.227 brouard 10513: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10514: continue;
1.223 brouard 10515: }
1.227 brouard 10516:
1.241 brouard 10517: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.264 brouard 10518: 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 10519: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 10520: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 10521: k=3; /* Offset */
1.255 brouard 10522: for (i=1; i<= nlstate ; i ++){ /* State of origin */
1.227 brouard 10523: if(i==1)
10524: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
10525: else
10526: fprintf(ficgp,", '' ");
1.255 brouard 10527: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 10528: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
10529: for (j=2; j<= nlstate ; j ++)
10530: fprintf(ficgp,"+$%d",k+l+j-1);
10531: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 10532: } /* nlstate */
1.264 brouard 10533: fprintf(ficgp,"\nset out; unset label;\n");
1.153 brouard 10534: } /* end cpt state*/
10535: } /* end covariate */
1.227 brouard 10536:
10537:
1.220 brouard 10538: /* 7eme */
1.296 brouard 10539: if(prevbcast == 1){
1.288 brouard 10540: /* CV backward prevalence for each covariate */
1.337 brouard 10541: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10542: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10543: k1=TKresult[nres];
1.338 brouard 10544: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10545: /* if(m != 1 && TKresult[nres]!= k1) */
10546: /* continue; */
1.268 brouard 10547: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
1.264 brouard 10548: strcpy(gplotlabel,"(");
1.288 brouard 10549: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10550: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10551: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10552: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10553: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate and each value *\/ */
10554: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate number corresponding to k1 combination *\\/ *\/ */
10555: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10556: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10557: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10558: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10559: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10560: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10561: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10562: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10563: /* } */
10564: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10565: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10566: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10567: }
1.264 brouard 10568: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 10569: fprintf(ficgp,"\n#\n");
10570: if(invalidvarcomb[k1]){
10571: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10572: continue;
10573: }
10574:
1.241 brouard 10575: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.268 brouard 10576: 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 10577: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 10578: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 10579: k=3; /* Offset */
1.268 brouard 10580: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
1.227 brouard 10581: if(i==1)
10582: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
10583: else
10584: fprintf(ficgp,", '' ");
10585: /* l=(nlstate+ndeath)*(i-1)+1; */
1.255 brouard 10586: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.324 brouard 10587: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
10588: /* 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 10589: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
1.227 brouard 10590: /* for (j=2; j<= nlstate ; j ++) */
10591: /* fprintf(ficgp,"+$%d",k+l+j-1); */
10592: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
1.268 brouard 10593: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
1.227 brouard 10594: } /* nlstate */
1.264 brouard 10595: fprintf(ficgp,"\nset out; unset label;\n");
1.218 brouard 10596: } /* end cpt state*/
10597: } /* end covariate */
1.296 brouard 10598: } /* End if prevbcast */
1.218 brouard 10599:
1.223 brouard 10600: /* 8eme */
1.218 brouard 10601: if(prevfcast==1){
1.288 brouard 10602: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
1.218 brouard 10603:
1.337 brouard 10604: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.237 brouard 10605: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10606: k1=TKresult[nres];
1.338 brouard 10607: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10608: /* if(m != 1 && TKresult[nres]!= k1) */
10609: /* continue; */
1.211 brouard 10610: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.264 brouard 10611: strcpy(gplotlabel,"(");
1.288 brouard 10612: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.337 brouard 10613: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10614: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10615: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10616: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10617: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10618: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10619: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10620: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10621: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10622: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10623: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10624: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10625: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10626: /* } */
10627: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10628: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10629: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.237 brouard 10630: }
1.264 brouard 10631: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 10632: fprintf(ficgp,"\n#\n");
10633: if(invalidvarcomb[k1]){
10634: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10635: continue;
10636: }
10637:
10638: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 10639: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.264 brouard 10640: 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 10641: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 10642: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.266 brouard 10643:
10644: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10645: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10646: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10647: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
1.227 brouard 10648: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10649: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10650: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10651: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.266 brouard 10652: if(i==istart){
1.227 brouard 10653: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
10654: }else{
10655: fprintf(ficgp,",\\\n '' ");
10656: }
10657: if(cptcoveff ==0){ /* No covariate */
10658: ioffset=2; /* Age is in 2 */
10659: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10660: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10661: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10662: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10663: fprintf(ficgp," u %d:(", ioffset);
1.266 brouard 10664: if(i==nlstate+1){
1.270 brouard 10665: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
1.266 brouard 10666: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10667: fprintf(ficgp,",\\\n '' ");
10668: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 10669: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
1.266 brouard 10670: offyear, \
1.268 brouard 10671: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
1.266 brouard 10672: }else
1.227 brouard 10673: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
10674: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10675: }else{ /* more than 2 covariates */
1.270 brouard 10676: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10677: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10678: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10679: iyearc=ioffset-1;
10680: iagec=ioffset;
1.227 brouard 10681: fprintf(ficgp," u %d:(",ioffset);
10682: kl=0;
10683: strcpy(gplotcondition,"(");
1.351 brouard 10684: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
1.332 brouard 10685: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
1.351 brouard 10686: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10687: /* lv=codtabm(k1,TnsdVar[Tvaraff[k]]); */
10688: lv=Tvresult[nres][k];
10689: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
1.227 brouard 10690: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10691: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10692: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.332 brouard 10693: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
1.351 brouard 10694: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
1.227 brouard 10695: kl++;
1.351 brouard 10696: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
10697: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,lv, kl+1, vlv );
1.227 brouard 10698: kl++;
1.351 brouard 10699: if(k <cptcovs && cptcovs>1)
1.227 brouard 10700: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10701: }
10702: strcpy(gplotcondition+strlen(gplotcondition),")");
10703: /* 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 *\/ */
10704: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10705: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10706: /* '' 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*/
10707: if(i==nlstate+1){
1.270 brouard 10708: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
10709: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
1.266 brouard 10710: fprintf(ficgp,",\\\n '' ");
1.270 brouard 10711: fprintf(ficgp," u %d:(",iagec);
10712: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
10713: iyearc, iagec, offyear, \
10714: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
1.266 brouard 10715: /* '' 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 10716: }else{
10717: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
10718: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
10719: }
10720: } /* end if covariate */
10721: } /* nlstate */
1.264 brouard 10722: fprintf(ficgp,"\nset out; unset label;\n");
1.223 brouard 10723: } /* end cpt state*/
10724: } /* end covariate */
10725: } /* End if prevfcast */
1.227 brouard 10726:
1.296 brouard 10727: if(prevbcast==1){
1.268 brouard 10728: /* Back projection from cross-sectional to stable (mixed) for each covariate */
10729:
1.337 brouard 10730: /* for (k1=1; k1<= m ; k1 ++) /\* For each covariate combination if any *\/ */
1.268 brouard 10731: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10732: k1=TKresult[nres];
1.338 brouard 10733: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10734: /* if(m != 1 && TKresult[nres]!= k1) */
10735: /* continue; */
1.268 brouard 10736: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
10737: strcpy(gplotlabel,"(");
10738: 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 10739: for (k=1; k<=cptcovs; k++){ /* For each covariate k get corresponding value lv for combination k1 */
10740: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10741: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10742: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10743: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10744: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10745: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10746: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10747: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10748: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10749: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10750: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10751: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10752: /* } */
10753: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10754: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10755: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
1.268 brouard 10756: }
10757: strcpy(gplotlabel+strlen(gplotlabel),")");
10758: fprintf(ficgp,"\n#\n");
10759: if(invalidvarcomb[k1]){
10760: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
10761: continue;
10762: }
10763:
10764: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
10765: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
10766: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
10767: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
10768: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
10769:
10770: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
10771: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
10772: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
10773: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
10774: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10775: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10776: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10777: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10778: if(i==istart){
10779: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
10780: }else{
10781: fprintf(ficgp,",\\\n '' ");
10782: }
1.351 brouard 10783: /* if(cptcoveff ==0){ /\* No covariate *\/ */
10784: if(cptcovs ==0){ /* No covariate */
1.268 brouard 10785: ioffset=2; /* Age is in 2 */
10786: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10787: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10788: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
10789: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
10790: fprintf(ficgp," u %d:(", ioffset);
10791: if(i==nlstate+1){
1.270 brouard 10792: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
1.268 brouard 10793: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
10794: fprintf(ficgp,",\\\n '' ");
10795: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 10796: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
1.268 brouard 10797: offbyear, \
10798: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
10799: }else
10800: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
10801: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
10802: }else{ /* more than 2 covariates */
1.270 brouard 10803: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
10804: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
10805: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
10806: iyearc=ioffset-1;
10807: iagec=ioffset;
1.268 brouard 10808: fprintf(ficgp," u %d:(",ioffset);
10809: kl=0;
10810: strcpy(gplotcondition,"(");
1.337 brouard 10811: for (k=1; k<=cptcovs; k++){ /* For each covariate k of the resultline, get corresponding value lv for combination k1 */
1.338 brouard 10812: if(Dummy[modelresult[nres][k]]==0){ /* To be verified */
1.337 brouard 10813: /* for (k=1; k<=cptcoveff; k++){ /\* For each covariate writing the chain of conditions *\/ */
10814: /* lv= decodtabm(k1,k,cptcoveff); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10815: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10816: lv=Tvresult[nres][k];
10817: vlv=TinvDoQresult[nres][Tvresult[nres][k]];
10818: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
10819: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
10820: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
10821: /* vlv= nbcode[Tvaraff[k]][lv]; /\* Value of the modality of Tvaraff[k] *\/ */
10822: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10823: kl++;
10824: /* sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); */
10825: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%lg " ,kl,Tvresult[nres][k], kl+1,TinvDoQresult[nres][Tvresult[nres][k]]);
10826: kl++;
1.338 brouard 10827: if(k <cptcovs && cptcovs>1)
1.337 brouard 10828: sprintf(gplotcondition+strlen(gplotcondition)," && ");
10829: }
1.268 brouard 10830: }
10831: strcpy(gplotcondition+strlen(gplotcondition),")");
10832: /* 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 *\/ */
10833: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
10834: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
10835: /* '' 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*/
10836: if(i==nlstate+1){
1.270 brouard 10837: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
10838: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
1.268 brouard 10839: fprintf(ficgp,",\\\n '' ");
1.270 brouard 10840: fprintf(ficgp," u %d:(",iagec);
1.268 brouard 10841: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
1.270 brouard 10842: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
10843: iyearc,iagec,offbyear, \
10844: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
1.268 brouard 10845: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
10846: }else{
10847: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
10848: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
10849: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
10850: }
10851: } /* end if covariate */
10852: } /* nlstate */
10853: fprintf(ficgp,"\nset out; unset label;\n");
10854: } /* end cpt state*/
10855: } /* end covariate */
1.296 brouard 10856: } /* End if prevbcast */
1.268 brouard 10857:
1.227 brouard 10858:
1.238 brouard 10859: /* 9eme writing MLE parameters */
10860: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 10861: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 10862: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 10863: for(k=1; k <=(nlstate+ndeath); k++){
10864: if (k != i) {
1.227 brouard 10865: fprintf(ficgp,"# current state %d\n",k);
10866: for(j=1; j <=ncovmodel; j++){
10867: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
10868: jk++;
10869: }
10870: fprintf(ficgp,"\n");
1.126 brouard 10871: }
10872: }
1.223 brouard 10873: }
1.187 brouard 10874: fprintf(ficgp,"##############\n#\n");
1.227 brouard 10875:
1.145 brouard 10876: /*goto avoid;*/
1.238 brouard 10877: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
10878: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 10879: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
10880: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
10881: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
10882: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
10883: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10884: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
10885: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10886: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
10887: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
10888: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
10889: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
10890: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
10891: fprintf(ficgp,"#\n");
1.223 brouard 10892: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 10893: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.338 brouard 10894: fprintf(ficgp,"#model=1+age+%s \n",model);
1.238 brouard 10895: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.351 brouard 10896: /* fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/\* to be checked *\/ */
10897: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcovs,m);/* to be checked */
1.337 brouard 10898: /* for(k1=1; k1 <=m; k1++) /\* For each combination of covariate *\/ */
1.237 brouard 10899: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 10900: /* k1=nres; */
1.338 brouard 10901: k1=TKresult[nres];
10902: if(TKresult[nres]==0) k1=1; /* To be checked for noresult */
1.337 brouard 10903: fprintf(ficgp,"\n\n# Resultline k1=%d ",k1);
1.264 brouard 10904: strcpy(gplotlabel,"(");
1.276 brouard 10905: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
1.337 brouard 10906: for (k=1; k<=cptcovs; k++){ /**< cptcovs number of SIMPLE covariates in the model V2+V1 =2 (dummy or quantit or time varying) */
10907: /* for each resultline nres, and position k, Tvresult[nres][k] gives the name of the variable and
10908: TinvDoQresult[nres][Tvresult[nres][k]] gives its value double or integer) */
10909: fprintf(ficgp," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10910: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%lg ",Tvresult[nres][k],TinvDoQresult[nres][Tvresult[nres][k]]);
10911: }
10912: /* if(m != 1 && TKresult[nres]!= k1) */
10913: /* continue; */
10914: /* fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); */
10915: /* strcpy(gplotlabel,"("); */
10916: /* /\*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*\/ */
10917: /* for (k=1; k<=cptcoveff; k++){ /\* For each correspondig covariate value *\/ */
10918: /* /\* lv= decodtabm(k1,k,cptcoveff); /\\* Should be the covariate value corresponding to k1 combination and kth covariate *\\/ *\/ */
10919: /* lv= codtabm(k1,TnsdVar[Tvaraff[k]]); /\* Should be the covariate value corresponding to combination k1 and covariate k *\/ */
10920: /* /\* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 *\/ */
10921: /* /\* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 *\/ */
10922: /* /\* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 *\/ */
10923: /* /\* vlv= nbcode[Tvaraff[k]][lv]; *\/ */
10924: /* vlv= nbcode[Tvaraff[k]][codtabm(k1,TnsdVar[Tvaraff[k]])]; */
10925: /* fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); */
10926: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); */
10927: /* } */
10928: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
10929: /* fprintf(ficgp," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10930: /* sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][resultmodel[nres][k4]],Tqresult[nres][resultmodel[nres][k4]]); */
10931: /* } */
1.264 brouard 10932: strcpy(gplotlabel+strlen(gplotlabel),")");
1.237 brouard 10933: fprintf(ficgp,"\n#\n");
1.264 brouard 10934: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
1.276 brouard 10935: fprintf(ficgp,"\nset key outside ");
10936: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
10937: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
1.223 brouard 10938: fprintf(ficgp,"\nset ter svg size 640, 480 ");
10939: if (ng==1){
10940: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
10941: fprintf(ficgp,"\nunset log y");
10942: }else if (ng==2){
10943: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
10944: fprintf(ficgp,"\nset log y");
10945: }else if (ng==3){
10946: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
10947: fprintf(ficgp,"\nset log y");
10948: }else
10949: fprintf(ficgp,"\nunset title ");
10950: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
10951: i=1;
10952: for(k2=1; k2<=nlstate; k2++) {
10953: k3=i;
10954: for(k=1; k<=(nlstate+ndeath); k++) {
10955: if (k != k2){
10956: switch( ng) {
10957: case 1:
10958: if(nagesqr==0)
10959: fprintf(ficgp," p%d+p%d*x",i,i+1);
10960: else /* nagesqr =1 */
10961: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
10962: break;
10963: case 2: /* ng=2 */
10964: if(nagesqr==0)
10965: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
10966: else /* nagesqr =1 */
10967: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
10968: break;
10969: case 3:
10970: if(nagesqr==0)
10971: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
10972: else /* nagesqr =1 */
10973: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
10974: break;
10975: }
10976: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 10977: ijp=1; /* product no age */
10978: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
10979: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 10980: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.329 brouard 10981: switch(Typevar[j]){
10982: case 1:
10983: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
10984: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
10985: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
10986: if(DummyV[j]==0){/* Bug valgrind */
10987: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
10988: }else{ /* quantitative */
10989: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
10990: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
10991: }
10992: ij++;
1.268 brouard 10993: }
1.237 brouard 10994: }
1.329 brouard 10995: }
10996: break;
10997: case 2:
10998: if(cptcovprod >0){
10999: if(j==Tprod[ijp]) { /* */
11000: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11001: if(ijp <=cptcovprod) { /* Product */
11002: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11003: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11004: /* 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)]); */
11005: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11006: }else{ /* Vn is dummy and Vm is quanti */
11007: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11008: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11009: }
11010: }else{ /* Vn*Vm Vn is quanti */
11011: if(DummyV[Tvard[ijp][2]]==0){
11012: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11013: }else{ /* Both quanti */
11014: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11015: }
1.268 brouard 11016: }
1.329 brouard 11017: ijp++;
1.237 brouard 11018: }
1.329 brouard 11019: } /* end Tprod */
11020: }
11021: break;
1.349 brouard 11022: case 3:
11023: if(cptcovdageprod >0){
11024: /* if(j==Tprod[ijp]) { */ /* not necessary */
11025: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
1.350 brouard 11026: if(ijp <=cptcovprod) { /* Product Vn*Vm and age*VN*Vm*/
11027: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11028: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
1.349 brouard 11029: /* 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)]); */
11030: fprintf(ficgp,"+p%d*%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11031: }else{ /* Vn is dummy and Vm is quanti */
11032: /* 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 11033: 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 11034: }
1.350 brouard 11035: }else{ /* age* Vn*Vm Vn is quanti HERE */
1.349 brouard 11036: if(DummyV[Tvard[ijp][2]]==0){
1.350 brouard 11037: 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 11038: }else{ /* Both quanti */
1.350 brouard 11039: 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 11040: }
11041: }
11042: ijp++;
11043: }
11044: /* } */ /* end Tprod */
11045: }
11046: break;
1.329 brouard 11047: case 0:
11048: /* simple covariate */
1.264 brouard 11049: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
1.237 brouard 11050: if(Dummy[j]==0){
11051: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
11052: }else{ /* quantitative */
11053: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.264 brouard 11054: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
1.223 brouard 11055: }
1.329 brouard 11056: /* end simple */
11057: break;
11058: default:
11059: break;
11060: } /* end switch */
1.237 brouard 11061: } /* end j */
1.329 brouard 11062: }else{ /* k=k2 */
11063: if(ng !=1 ){ /* For logit formula of log p11 is more difficult to get */
11064: fprintf(ficgp," (1.");i=i-ncovmodel;
11065: }else
11066: i=i-ncovmodel;
1.223 brouard 11067: }
1.227 brouard 11068:
1.223 brouard 11069: if(ng != 1){
11070: fprintf(ficgp,")/(1");
1.227 brouard 11071:
1.264 brouard 11072: for(cpt=1; cpt <=nlstate; cpt++){
1.223 brouard 11073: if(nagesqr==0)
1.264 brouard 11074: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
1.223 brouard 11075: else /* nagesqr =1 */
1.264 brouard 11076: 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 11077:
1.223 brouard 11078: ij=1;
1.329 brouard 11079: ijp=1;
11080: /* for(j=3; j <=ncovmodel-nagesqr; j++){ */
11081: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
11082: switch(Typevar[j]){
11083: case 1:
11084: if(cptcovage >0){
11085: if(j==Tage[ij]) { /* Bug valgrind */
11086: if(ij <=cptcovage) { /* Bug valgrind */
11087: if(DummyV[j]==0){/* Bug valgrind */
11088: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); */
11089: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,nbcode[Tvar[j]][codtabm(k1,j)]); */
11090: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]);
11091: /* fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; */
11092: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11093: }else{ /* quantitative */
11094: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11095: fprintf(ficgp,"+p%d*%f*x",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
11096: /* fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* Tqinvresult in decoderesult *\/ */
11097: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11098: }
11099: ij++;
11100: }
11101: }
11102: }
11103: break;
11104: case 2:
11105: if(cptcovprod >0){
11106: if(j==Tprod[ijp]) { /* */
11107: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11108: if(ijp <=cptcovprod) { /* Product */
11109: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
11110: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
11111: /* 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)]); */
11112: fprintf(ficgp,"+p%d*%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
11113: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11114: }else{ /* Vn is dummy and Vm is quanti */
11115: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
11116: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11117: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11118: }
11119: }else{ /* Vn*Vm Vn is quanti */
11120: if(DummyV[Tvard[ijp][2]]==0){
11121: fprintf(ficgp,"+p%d*%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
11122: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11123: }else{ /* Both quanti */
11124: fprintf(ficgp,"+p%d*%f*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
11125: /* fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11126: }
11127: }
11128: ijp++;
11129: }
11130: } /* end Tprod */
11131: } /* end if */
11132: break;
1.349 brouard 11133: case 3:
11134: if(cptcovdageprod >0){
11135: /* if(j==Tprod[ijp]) { /\* *\/ */
11136: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
11137: if(ijp <=cptcovprod) { /* Product */
1.350 brouard 11138: if(DummyV[Tvardk[ijp][1]]==0){/* Vn is dummy */
11139: if(DummyV[Tvardk[ijp][2]]==0){/* Vn and Vm are dummy */
1.349 brouard 11140: /* 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 11141: 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 11142: /* fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); */
11143: }else{ /* Vn is dummy and Vm is quanti */
11144: /* 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 11145: 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 11146: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11147: }
11148: }else{ /* Vn*Vm Vn is quanti */
1.350 brouard 11149: if(DummyV[Tvardk[ijp][2]]==0){
11150: 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 11151: /* fprintf(ficgp,"+p%d*%d*%f*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); */
11152: }else{ /* Both quanti */
1.350 brouard 11153: 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 11154: /* fprintf(ficgp,"+p%d*%f*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); */
11155: }
11156: }
11157: ijp++;
11158: }
11159: /* } /\* end Tprod *\/ */
11160: } /* end if */
11161: break;
1.329 brouard 11162: case 0:
11163: /* simple covariate */
11164: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
11165: if(Dummy[j]==0){
11166: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11167: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tinvresult[nres][Tvar[j]]); /* */
11168: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /\* *\/ */
11169: }else{ /* quantitative */
11170: fprintf(ficgp,"+p%d*%f",k3+(cpt-1)*ncovmodel+1+j+nagesqr,Tqinvresult[nres][Tvar[j]]); /* */
11171: /* fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /\* *\/ */
11172: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
11173: }
11174: /* end simple */
11175: /* fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/\* Valgrind bug nbcode *\/ */
11176: break;
11177: default:
11178: break;
11179: } /* end switch */
1.223 brouard 11180: }
11181: fprintf(ficgp,")");
11182: }
11183: fprintf(ficgp,")");
11184: if(ng ==2)
1.276 brouard 11185: 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 11186: else /* ng= 3 */
1.276 brouard 11187: 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 11188: }else{ /* end ng <> 1 */
1.223 brouard 11189: if( k !=k2) /* logit p11 is hard to draw */
1.276 brouard 11190: 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 11191: }
11192: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
11193: fprintf(ficgp,",");
11194: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
11195: fprintf(ficgp,",");
11196: i=i+ncovmodel;
11197: } /* end k */
11198: } /* end k2 */
1.276 brouard 11199: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
11200: fprintf(ficgp,"\n set out; unset title;set key default;\n");
1.337 brouard 11201: } /* end resultline */
1.223 brouard 11202: } /* end ng */
11203: /* avoid: */
11204: fflush(ficgp);
1.126 brouard 11205: } /* end gnuplot */
11206:
11207:
11208: /*************** Moving average **************/
1.219 brouard 11209: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 11210: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 11211:
1.222 brouard 11212: int i, cpt, cptcod;
11213: int modcovmax =1;
11214: int mobilavrange, mob;
11215: int iage=0;
1.288 brouard 11216: int firstA1=0, firstA2=0;
1.222 brouard 11217:
1.266 brouard 11218: double sum=0., sumr=0.;
1.222 brouard 11219: double age;
1.266 brouard 11220: double *sumnewp, *sumnewm, *sumnewmr;
11221: double *agemingood, *agemaxgood;
11222: double *agemingoodr, *agemaxgoodr;
1.222 brouard 11223:
11224:
1.278 brouard 11225: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
11226: /* a covariate has 2 modalities, should be equal to ncovcombmax */
1.222 brouard 11227:
11228: sumnewp = vector(1,ncovcombmax);
11229: sumnewm = vector(1,ncovcombmax);
1.266 brouard 11230: sumnewmr = vector(1,ncovcombmax);
1.222 brouard 11231: agemingood = vector(1,ncovcombmax);
1.266 brouard 11232: agemingoodr = vector(1,ncovcombmax);
1.222 brouard 11233: agemaxgood = vector(1,ncovcombmax);
1.266 brouard 11234: agemaxgoodr = vector(1,ncovcombmax);
1.222 brouard 11235:
11236: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
1.266 brouard 11237: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
1.222 brouard 11238: sumnewp[cptcod]=0.;
1.266 brouard 11239: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
11240: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
1.222 brouard 11241: }
11242: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
11243:
1.266 brouard 11244: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
11245: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
1.222 brouard 11246: else mobilavrange=mobilav;
11247: for (age=bage; age<=fage; age++)
11248: for (i=1; i<=nlstate;i++)
11249: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
11250: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11251: /* We keep the original values on the extreme ages bage, fage and for
11252: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
11253: we use a 5 terms etc. until the borders are no more concerned.
11254: */
11255: for (mob=3;mob <=mobilavrange;mob=mob+2){
11256: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
1.266 brouard 11257: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
11258: sumnewm[cptcod]=0.;
11259: for (i=1; i<=nlstate;i++){
1.222 brouard 11260: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
11261: for (cpt=1;cpt<=(mob-1)/2;cpt++){
11262: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
11263: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
11264: }
11265: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
1.266 brouard 11266: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11267: } /* end i */
11268: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
11269: } /* end cptcod */
1.222 brouard 11270: }/* end age */
11271: }/* end mob */
1.266 brouard 11272: }else{
11273: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
1.222 brouard 11274: return -1;
1.266 brouard 11275: }
11276:
11277: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
1.222 brouard 11278: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
11279: if(invalidvarcomb[cptcod]){
11280: printf("\nCombination (%d) ignored because no cases \n",cptcod);
11281: continue;
11282: }
1.219 brouard 11283:
1.266 brouard 11284: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
11285: sumnewm[cptcod]=0.;
11286: sumnewmr[cptcod]=0.;
11287: for (i=1; i<=nlstate;i++){
11288: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11289: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11290: }
11291: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11292: agemingoodr[cptcod]=age;
11293: }
11294: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11295: agemingood[cptcod]=age;
11296: }
11297: } /* age */
11298: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
1.222 brouard 11299: sumnewm[cptcod]=0.;
1.266 brouard 11300: sumnewmr[cptcod]=0.;
1.222 brouard 11301: for (i=1; i<=nlstate;i++){
11302: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 11303: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11304: }
11305: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11306: agemaxgoodr[cptcod]=age;
1.222 brouard 11307: }
11308: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
1.266 brouard 11309: agemaxgood[cptcod]=age;
11310: }
11311: } /* age */
11312: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
11313: /* but they will change */
1.288 brouard 11314: firstA1=0;firstA2=0;
1.266 brouard 11315: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
11316: sumnewm[cptcod]=0.;
11317: sumnewmr[cptcod]=0.;
11318: for (i=1; i<=nlstate;i++){
11319: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11320: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11321: }
11322: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11323: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
11324: agemaxgoodr[cptcod]=age; /* age min */
11325: for (i=1; i<=nlstate;i++)
11326: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11327: }else{ /* bad we change the value with the values of good ages */
11328: for (i=1; i<=nlstate;i++){
11329: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
11330: } /* i */
11331: } /* end bad */
11332: }else{
11333: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11334: agemaxgood[cptcod]=age;
11335: }else{ /* bad we change the value with the values of good ages */
11336: for (i=1; i<=nlstate;i++){
11337: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
11338: } /* i */
11339: } /* end bad */
11340: }/* end else */
11341: sum=0.;sumr=0.;
11342: for (i=1; i<=nlstate;i++){
11343: sum+=mobaverage[(int)age][i][cptcod];
11344: sumr+=probs[(int)age][i][cptcod];
11345: }
11346: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.288 brouard 11347: if(!firstA1){
11348: firstA1=1;
11349: 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);
11350: }
11351: 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 11352: } /* end bad */
11353: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11354: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.288 brouard 11355: if(!firstA2){
11356: firstA2=1;
11357: 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);
11358: }
11359: 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 11360: } /* end bad */
11361: }/* age */
1.266 brouard 11362:
11363: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
1.222 brouard 11364: sumnewm[cptcod]=0.;
1.266 brouard 11365: sumnewmr[cptcod]=0.;
1.222 brouard 11366: for (i=1; i<=nlstate;i++){
11367: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 11368: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
11369: }
11370: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
11371: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
11372: agemingoodr[cptcod]=age;
11373: for (i=1; i<=nlstate;i++)
11374: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
11375: }else{ /* bad we change the value with the values of good ages */
11376: for (i=1; i<=nlstate;i++){
11377: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
11378: } /* i */
11379: } /* end bad */
11380: }else{
11381: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
11382: agemingood[cptcod]=age;
11383: }else{ /* bad */
11384: for (i=1; i<=nlstate;i++){
11385: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
11386: } /* i */
11387: } /* end bad */
11388: }/* end else */
11389: sum=0.;sumr=0.;
11390: for (i=1; i<=nlstate;i++){
11391: sum+=mobaverage[(int)age][i][cptcod];
11392: sumr+=mobaverage[(int)age][i][cptcod];
1.222 brouard 11393: }
1.266 brouard 11394: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.268 brouard 11395: 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 11396: } /* end bad */
11397: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
11398: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.268 brouard 11399: 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 11400: } /* end bad */
11401: }/* age */
1.266 brouard 11402:
1.222 brouard 11403:
11404: for (age=bage; age<=fage; age++){
1.235 brouard 11405: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 11406: sumnewp[cptcod]=0.;
11407: sumnewm[cptcod]=0.;
11408: for (i=1; i<=nlstate;i++){
11409: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
11410: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
11411: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
11412: }
11413: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
11414: }
11415: /* printf("\n"); */
11416: /* } */
1.266 brouard 11417:
1.222 brouard 11418: /* brutal averaging */
1.266 brouard 11419: /* for (i=1; i<=nlstate;i++){ */
11420: /* for (age=1; age<=bage; age++){ */
11421: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
11422: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11423: /* } */
11424: /* for (age=fage; age<=AGESUP; age++){ */
11425: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
11426: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
11427: /* } */
11428: /* } /\* end i status *\/ */
11429: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
11430: /* for (age=1; age<=AGESUP; age++){ */
11431: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
11432: /* mobaverage[(int)age][i][cptcod]=0.; */
11433: /* } */
11434: /* } */
1.222 brouard 11435: }/* end cptcod */
1.266 brouard 11436: free_vector(agemaxgoodr,1, ncovcombmax);
11437: free_vector(agemaxgood,1, ncovcombmax);
11438: free_vector(agemingood,1, ncovcombmax);
11439: free_vector(agemingoodr,1, ncovcombmax);
11440: free_vector(sumnewmr,1, ncovcombmax);
1.222 brouard 11441: free_vector(sumnewm,1, ncovcombmax);
11442: free_vector(sumnewp,1, ncovcombmax);
11443: return 0;
11444: }/* End movingaverage */
1.218 brouard 11445:
1.126 brouard 11446:
1.296 brouard 11447:
1.126 brouard 11448: /************** Forecasting ******************/
1.296 brouard 11449: /* 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)*/
11450: 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){
11451: /* dateintemean, mean date of interviews
11452: dateprojd, year, month, day of starting projection
11453: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
1.126 brouard 11454: agemin, agemax range of age
11455: dateprev1 dateprev2 range of dates during which prevalence is computed
11456: */
1.296 brouard 11457: /* double anprojd, mprojd, jprojd; */
11458: /* double anprojf, mprojf, jprojf; */
1.359 ! brouard 11459: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
1.126 brouard 11460: double agec; /* generic age */
1.359 ! brouard 11461: double agelim, ppij;
! 11462: /*double *popcount;*/
1.126 brouard 11463: double ***p3mat;
1.218 brouard 11464: /* double ***mobaverage; */
1.126 brouard 11465: char fileresf[FILENAMELENGTH];
11466:
11467: agelim=AGESUP;
1.211 brouard 11468: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11469: in each health status at the date of interview (if between dateprev1 and dateprev2).
11470: We still use firstpass and lastpass as another selection.
11471: */
1.214 brouard 11472: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11473: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 11474:
1.201 brouard 11475: strcpy(fileresf,"F_");
11476: strcat(fileresf,fileresu);
1.126 brouard 11477: if((ficresf=fopen(fileresf,"w"))==NULL) {
11478: printf("Problem with forecast resultfile: %s\n", fileresf);
11479: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
11480: }
1.235 brouard 11481: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
11482: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 11483:
1.225 brouard 11484: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 11485:
11486:
11487: stepsize=(int) (stepm+YEARM-1)/YEARM;
11488: if (stepm<=12) stepsize=1;
11489: if(estepm < stepm){
11490: printf ("Problem %d lower than %d\n",estepm, stepm);
11491: }
1.270 brouard 11492: else{
11493: hstepm=estepm;
11494: }
11495: if(estepm > stepm){ /* Yes every two year */
11496: stepsize=2;
11497: }
1.296 brouard 11498: hstepm=hstepm/stepm;
1.126 brouard 11499:
1.296 brouard 11500:
11501: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11502: /* fractional in yp1 *\/ */
11503: /* aintmean=yp; */
11504: /* yp2=modf((yp1*12),&yp); */
11505: /* mintmean=yp; */
11506: /* yp1=modf((yp2*30.5),&yp); */
11507: /* jintmean=yp; */
11508: /* if(jintmean==0) jintmean=1; */
11509: /* if(mintmean==0) mintmean=1; */
1.126 brouard 11510:
1.296 brouard 11511:
11512: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
11513: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
11514: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
1.351 brouard 11515: /* i1=pow(2,cptcoveff); */
11516: /* if (cptcovn < 1){i1=1;} */
1.126 brouard 11517:
1.296 brouard 11518: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.126 brouard 11519:
11520: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 11521:
1.126 brouard 11522: /* if (h==(int)(YEARM*yearp)){ */
1.351 brouard 11523: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11524: k=TKresult[nres];
11525: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11526: /* 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) *\/ */
11527: /* if(i1 != 1 && TKresult[nres]!= k) */
11528: /* continue; */
11529: /* if(invalidvarcomb[k]){ */
11530: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11531: /* continue; */
11532: /* } */
1.227 brouard 11533: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
1.351 brouard 11534: for(j=1;j<=cptcovs;j++){
11535: /* for(j=1;j<=cptcoveff;j++) { */
11536: /* /\* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); *\/ */
11537: /* fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11538: /* } */
11539: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11540: /* fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11541: /* } */
11542: fprintf(ficresf," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.235 brouard 11543: }
1.351 brouard 11544:
1.227 brouard 11545: fprintf(ficresf," yearproj age");
11546: for(j=1; j<=nlstate+ndeath;j++){
11547: for(i=1; i<=nlstate;i++)
11548: fprintf(ficresf," p%d%d",i,j);
11549: fprintf(ficresf," wp.%d",j);
11550: }
1.296 brouard 11551: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
1.227 brouard 11552: fprintf(ficresf,"\n");
1.296 brouard 11553: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
1.270 brouard 11554: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
11555: for (agec=fage; agec>=(bage); agec--){
1.227 brouard 11556: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
11557: nhstepm = nhstepm/hstepm;
11558: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11559: oldm=oldms;savm=savms;
1.268 brouard 11560: /* We compute pii at age agec over nhstepm);*/
1.235 brouard 11561: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.268 brouard 11562: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
1.227 brouard 11563: for (h=0; h<=nhstepm; h++){
11564: if (h*hstepm/YEARM*stepm ==yearp) {
1.268 brouard 11565: break;
11566: }
11567: }
11568: fprintf(ficresf,"\n");
1.351 brouard 11569: /* for(j=1;j<=cptcoveff;j++) */
11570: for(j=1;j<=cptcovs;j++)
11571: fprintf(ficresf,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.332 brouard 11572: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Tvaraff not correct *\/ */
1.351 brouard 11573: /* fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* TnsdVar[Tvaraff] correct *\/ */
1.296 brouard 11574: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
1.268 brouard 11575:
11576: for(j=1; j<=nlstate+ndeath;j++) {
11577: ppij=0.;
11578: for(i=1; i<=nlstate;i++) {
1.278 brouard 11579: if (mobilav>=1)
11580: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
11581: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
11582: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
11583: }
1.268 brouard 11584: fprintf(ficresf," %.3f", p3mat[i][j][h]);
11585: } /* end i */
11586: fprintf(ficresf," %.3f", ppij);
11587: }/* end j */
1.227 brouard 11588: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11589: } /* end agec */
1.266 brouard 11590: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
11591: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
1.227 brouard 11592: } /* end yearp */
11593: } /* end k */
1.219 brouard 11594:
1.126 brouard 11595: fclose(ficresf);
1.215 brouard 11596: printf("End of Computing forecasting \n");
11597: fprintf(ficlog,"End of Computing forecasting\n");
11598:
1.126 brouard 11599: }
11600:
1.269 brouard 11601: /************** Back Forecasting ******************/
1.296 brouard 11602: /* 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){ */
11603: 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){
11604: /* back1, year, month, day of starting backprojection
1.267 brouard 11605: agemin, agemax range of age
11606: dateprev1 dateprev2 range of dates during which prevalence is computed
1.269 brouard 11607: anback2 year of end of backprojection (same day and month as back1).
11608: prevacurrent and prev are prevalences.
1.267 brouard 11609: */
1.359 ! brouard 11610: int yearp, stepsize, hstepm, nhstepm, j, k, i, h, nres=0;
1.267 brouard 11611: double agec; /* generic age */
1.359 ! brouard 11612: double agelim, ppij, ppi; /* ,jintmean,mintmean,aintmean;*/
! 11613: /*double *popcount;*/
1.267 brouard 11614: double ***p3mat;
11615: /* double ***mobaverage; */
11616: char fileresfb[FILENAMELENGTH];
11617:
1.268 brouard 11618: agelim=AGEINF;
1.267 brouard 11619: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
11620: in each health status at the date of interview (if between dateprev1 and dateprev2).
11621: We still use firstpass and lastpass as another selection.
11622: */
11623: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
11624: /* firstpass, lastpass, stepm, weightopt, model); */
11625:
11626: /*Do we need to compute prevalence again?*/
11627:
11628: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
11629:
11630: strcpy(fileresfb,"FB_");
11631: strcat(fileresfb,fileresu);
11632: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
11633: printf("Problem with back forecast resultfile: %s\n", fileresfb);
11634: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
11635: }
11636: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11637: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
11638:
11639: if (cptcoveff==0) ncodemax[cptcoveff]=1;
11640:
11641:
11642: stepsize=(int) (stepm+YEARM-1)/YEARM;
11643: if (stepm<=12) stepsize=1;
11644: if(estepm < stepm){
11645: printf ("Problem %d lower than %d\n",estepm, stepm);
11646: }
1.270 brouard 11647: else{
11648: hstepm=estepm;
11649: }
11650: if(estepm >= stepm){ /* Yes every two year */
11651: stepsize=2;
11652: }
1.267 brouard 11653:
11654: hstepm=hstepm/stepm;
1.296 brouard 11655: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
11656: /* fractional in yp1 *\/ */
11657: /* aintmean=yp; */
11658: /* yp2=modf((yp1*12),&yp); */
11659: /* mintmean=yp; */
11660: /* yp1=modf((yp2*30.5),&yp); */
11661: /* jintmean=yp; */
11662: /* if(jintmean==0) jintmean=1; */
11663: /* if(mintmean==0) jintmean=1; */
1.267 brouard 11664:
1.351 brouard 11665: /* i1=pow(2,cptcoveff); */
11666: /* if (cptcovn < 1){i1=1;} */
1.267 brouard 11667:
1.296 brouard 11668: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
11669: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.267 brouard 11670:
11671: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
11672:
1.351 brouard 11673: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11674: k=TKresult[nres];
11675: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
11676: /* for(k=1; k<=i1;k++){ */
11677: /* if(i1 != 1 && TKresult[nres]!= k) */
11678: /* continue; */
11679: /* if(invalidvarcomb[k]){ */
11680: /* printf("\nCombination (%d) projection ignored because no cases \n",k); */
11681: /* continue; */
11682: /* } */
1.268 brouard 11683: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
1.351 brouard 11684: for(j=1;j<=cptcovs;j++){
11685: /* for(j=1;j<=cptcoveff;j++) { */
11686: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11687: /* } */
11688: fprintf(ficresfb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.267 brouard 11689: }
1.351 brouard 11690: /* fprintf(ficrespij,"******\n"); */
11691: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
11692: /* fprintf(ficresfb," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
11693: /* } */
1.267 brouard 11694: fprintf(ficresfb," yearbproj age");
11695: for(j=1; j<=nlstate+ndeath;j++){
11696: for(i=1; i<=nlstate;i++)
1.268 brouard 11697: fprintf(ficresfb," b%d%d",i,j);
11698: fprintf(ficresfb," b.%d",j);
1.267 brouard 11699: }
1.296 brouard 11700: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
1.267 brouard 11701: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
11702: fprintf(ficresfb,"\n");
1.296 brouard 11703: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
1.273 brouard 11704: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
1.270 brouard 11705: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
11706: for (agec=bage; agec<=fage; agec++){ /* testing */
1.268 brouard 11707: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
1.271 brouard 11708: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
1.267 brouard 11709: nhstepm = nhstepm/hstepm;
11710: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11711: oldm=oldms;savm=savms;
1.268 brouard 11712: /* computes hbxij at age agec over 1 to nhstepm */
1.271 brouard 11713: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
1.267 brouard 11714: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.268 brouard 11715: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
11716: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
11717: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
1.267 brouard 11718: for (h=0; h<=nhstepm; h++){
1.268 brouard 11719: if (h*hstepm/YEARM*stepm ==-yearp) {
11720: break;
11721: }
11722: }
11723: fprintf(ficresfb,"\n");
1.351 brouard 11724: /* for(j=1;j<=cptcoveff;j++) */
11725: for(j=1;j<=cptcovs;j++)
11726: fprintf(ficresfb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11727: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.296 brouard 11728: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
1.268 brouard 11729: for(i=1; i<=nlstate+ndeath;i++) {
11730: ppij=0.;ppi=0.;
11731: for(j=1; j<=nlstate;j++) {
11732: /* if (mobilav==1) */
1.269 brouard 11733: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
11734: ppi=ppi+prevacurrent[(int)agec][j][k];
11735: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
11736: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
1.267 brouard 11737: /* else { */
11738: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
11739: /* } */
1.268 brouard 11740: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
11741: } /* end j */
11742: if(ppi <0.99){
11743: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11744: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
11745: }
11746: fprintf(ficresfb," %.3f", ppij);
11747: }/* end j */
1.267 brouard 11748: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11749: } /* end agec */
11750: } /* end yearp */
11751: } /* end k */
1.217 brouard 11752:
1.267 brouard 11753: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
1.217 brouard 11754:
1.267 brouard 11755: fclose(ficresfb);
11756: printf("End of Computing Back forecasting \n");
11757: fprintf(ficlog,"End of Computing Back forecasting\n");
1.218 brouard 11758:
1.267 brouard 11759: }
1.217 brouard 11760:
1.269 brouard 11761: /* Variance of prevalence limit: varprlim */
11762: 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 11763: /*------- Variance of forward period (stable) prevalence------*/
1.269 brouard 11764:
11765: char fileresvpl[FILENAMELENGTH];
11766: FILE *ficresvpl;
11767: double **oldm, **savm;
11768: double **varpl; /* Variances of prevalence limits by age */
11769: int i1, k, nres, j ;
11770:
11771: strcpy(fileresvpl,"VPL_");
11772: strcat(fileresvpl,fileresu);
11773: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.288 brouard 11774: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
1.269 brouard 11775: exit(0);
11776: }
1.288 brouard 11777: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
11778: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.269 brouard 11779:
11780: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
11781: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
11782:
11783: i1=pow(2,cptcoveff);
11784: if (cptcovn < 1){i1=1;}
11785:
1.337 brouard 11786: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11787: k=TKresult[nres];
1.338 brouard 11788: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 11789: /* for(k=1; k<=i1;k++){ /\* We find the combination equivalent to result line values of dummies *\/ */
1.269 brouard 11790: if(i1 != 1 && TKresult[nres]!= k)
11791: continue;
11792: fprintf(ficresvpl,"\n#****** ");
11793: printf("\n#****** ");
11794: fprintf(ficlog,"\n#****** ");
1.337 brouard 11795: for(j=1;j<=cptcovs;j++) {
11796: fprintf(ficresvpl,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11797: fprintf(ficlog,"V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11798: printf("V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
11799: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11800: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.269 brouard 11801: }
1.337 brouard 11802: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11803: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11804: /* fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11805: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11806: /* } */
1.269 brouard 11807: fprintf(ficresvpl,"******\n");
11808: printf("******\n");
11809: fprintf(ficlog,"******\n");
11810:
11811: varpl=matrix(1,nlstate,(int) bage, (int) fage);
11812: oldm=oldms;savm=savms;
11813: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
11814: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
11815: /*}*/
11816: }
11817:
11818: fclose(ficresvpl);
1.288 brouard 11819: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
11820: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
1.269 brouard 11821:
11822: }
11823: /* Variance of back prevalence: varbprlim */
11824: 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){
11825: /*------- Variance of back (stable) prevalence------*/
11826:
11827: char fileresvbl[FILENAMELENGTH];
11828: FILE *ficresvbl;
11829:
11830: double **oldm, **savm;
11831: double **varbpl; /* Variances of back prevalence limits by age */
11832: int i1, k, nres, j ;
11833:
11834: strcpy(fileresvbl,"VBL_");
11835: strcat(fileresvbl,fileresu);
11836: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
11837: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
11838: exit(0);
11839: }
11840: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
11841: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
11842:
11843:
11844: i1=pow(2,cptcoveff);
11845: if (cptcovn < 1){i1=1;}
11846:
1.337 brouard 11847: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11848: k=TKresult[nres];
1.338 brouard 11849: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 11850: /* for(k=1; k<=i1;k++){ */
11851: /* if(i1 != 1 && TKresult[nres]!= k) */
11852: /* continue; */
1.269 brouard 11853: fprintf(ficresvbl,"\n#****** ");
11854: printf("\n#****** ");
11855: fprintf(ficlog,"\n#****** ");
1.337 brouard 11856: for (j=1; j<= cptcovs; j++){ /* For each selected (single) quantitative value */
1.338 brouard 11857: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11858: fprintf(ficresvbl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
11859: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][resultmodel[nres][j]]);
1.337 brouard 11860: /* for(j=1;j<=cptcoveff;j++) { */
11861: /* fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11862: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11863: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
11864: /* } */
11865: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
11866: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11867: /* fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
11868: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
1.269 brouard 11869: }
11870: fprintf(ficresvbl,"******\n");
11871: printf("******\n");
11872: fprintf(ficlog,"******\n");
11873:
11874: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
11875: oldm=oldms;savm=savms;
11876:
11877: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
11878: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
11879: /*}*/
11880: }
11881:
11882: fclose(ficresvbl);
11883: printf("done variance-covariance of back prevalence\n");fflush(stdout);
11884: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
11885:
11886: } /* End of varbprlim */
11887:
1.126 brouard 11888: /************** Forecasting *****not tested NB*************/
1.227 brouard 11889: /* 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 11890:
1.227 brouard 11891: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
11892: /* int *popage; */
11893: /* double calagedatem, agelim, kk1, kk2; */
11894: /* double *popeffectif,*popcount; */
11895: /* double ***p3mat,***tabpop,***tabpopprev; */
11896: /* /\* double ***mobaverage; *\/ */
11897: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 11898:
1.227 brouard 11899: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
11900: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
11901: /* agelim=AGESUP; */
11902: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 11903:
1.227 brouard 11904: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 11905:
11906:
1.227 brouard 11907: /* strcpy(filerespop,"POP_"); */
11908: /* strcat(filerespop,fileresu); */
11909: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
11910: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
11911: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
11912: /* } */
11913: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
11914: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 11915:
1.227 brouard 11916: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 11917:
1.227 brouard 11918: /* /\* if (mobilav!=0) { *\/ */
11919: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
11920: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
11921: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
11922: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
11923: /* /\* } *\/ */
11924: /* /\* } *\/ */
1.126 brouard 11925:
1.227 brouard 11926: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
11927: /* if (stepm<=12) stepsize=1; */
1.126 brouard 11928:
1.227 brouard 11929: /* agelim=AGESUP; */
1.126 brouard 11930:
1.227 brouard 11931: /* hstepm=1; */
11932: /* hstepm=hstepm/stepm; */
1.218 brouard 11933:
1.227 brouard 11934: /* if (popforecast==1) { */
11935: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
11936: /* printf("Problem with population file : %s\n",popfile);exit(0); */
11937: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
11938: /* } */
11939: /* popage=ivector(0,AGESUP); */
11940: /* popeffectif=vector(0,AGESUP); */
11941: /* popcount=vector(0,AGESUP); */
1.126 brouard 11942:
1.227 brouard 11943: /* i=1; */
11944: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 11945:
1.227 brouard 11946: /* imx=i; */
11947: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
11948: /* } */
1.218 brouard 11949:
1.227 brouard 11950: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
11951: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
11952: /* k=k+1; */
11953: /* fprintf(ficrespop,"\n#******"); */
11954: /* for(j=1;j<=cptcoveff;j++) { */
11955: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
11956: /* } */
11957: /* fprintf(ficrespop,"******\n"); */
11958: /* fprintf(ficrespop,"# Age"); */
11959: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
11960: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 11961:
1.227 brouard 11962: /* for (cpt=0; cpt<=0;cpt++) { */
11963: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 11964:
1.227 brouard 11965: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
11966: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
11967: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 11968:
1.227 brouard 11969: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
11970: /* oldm=oldms;savm=savms; */
11971: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 11972:
1.227 brouard 11973: /* for (h=0; h<=nhstepm; h++){ */
11974: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
11975: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
11976: /* } */
11977: /* for(j=1; j<=nlstate+ndeath;j++) { */
11978: /* kk1=0.;kk2=0; */
11979: /* for(i=1; i<=nlstate;i++) { */
11980: /* if (mobilav==1) */
11981: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
11982: /* else { */
11983: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
11984: /* } */
11985: /* } */
11986: /* if (h==(int)(calagedatem+12*cpt)){ */
11987: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
11988: /* /\*fprintf(ficrespop," %.3f", kk1); */
11989: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
11990: /* } */
11991: /* } */
11992: /* for(i=1; i<=nlstate;i++){ */
11993: /* kk1=0.; */
11994: /* for(j=1; j<=nlstate;j++){ */
11995: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
11996: /* } */
11997: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
11998: /* } */
1.218 brouard 11999:
1.227 brouard 12000: /* if (h==(int)(calagedatem+12*cpt)) */
12001: /* for(j=1; j<=nlstate;j++) */
12002: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
12003: /* } */
12004: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12005: /* } */
12006: /* } */
1.218 brouard 12007:
1.227 brouard 12008: /* /\******\/ */
1.218 brouard 12009:
1.227 brouard 12010: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
12011: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
12012: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
12013: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
12014: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 12015:
1.227 brouard 12016: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12017: /* oldm=oldms;savm=savms; */
12018: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
12019: /* for (h=0; h<=nhstepm; h++){ */
12020: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
12021: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
12022: /* } */
12023: /* for(j=1; j<=nlstate+ndeath;j++) { */
12024: /* kk1=0.;kk2=0; */
12025: /* for(i=1; i<=nlstate;i++) { */
12026: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
12027: /* } */
12028: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
12029: /* } */
12030: /* } */
12031: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
12032: /* } */
12033: /* } */
12034: /* } */
12035: /* } */
1.218 brouard 12036:
1.227 brouard 12037: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 12038:
1.227 brouard 12039: /* if (popforecast==1) { */
12040: /* free_ivector(popage,0,AGESUP); */
12041: /* free_vector(popeffectif,0,AGESUP); */
12042: /* free_vector(popcount,0,AGESUP); */
12043: /* } */
12044: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12045: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
12046: /* fclose(ficrespop); */
12047: /* } /\* End of popforecast *\/ */
1.218 brouard 12048:
1.126 brouard 12049: int fileappend(FILE *fichier, char *optionfich)
12050: {
12051: if((fichier=fopen(optionfich,"a"))==NULL) {
12052: printf("Problem with file: %s\n", optionfich);
12053: fprintf(ficlog,"Problem with file: %s\n", optionfich);
12054: return (0);
12055: }
12056: fflush(fichier);
12057: return (1);
12058: }
12059:
12060:
12061: /**************** function prwizard **********************/
12062: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
12063: {
12064:
12065: /* Wizard to print covariance matrix template */
12066:
1.164 brouard 12067: char ca[32], cb[32];
12068: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 12069: int numlinepar;
12070:
12071: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12072: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12073: for(i=1; i <=nlstate; i++){
12074: jj=0;
12075: for(j=1; j <=nlstate+ndeath; j++){
12076: if(j==i) continue;
12077: jj++;
12078: /*ca[0]= k+'a'-1;ca[1]='\0';*/
12079: printf("%1d%1d",i,j);
12080: fprintf(ficparo,"%1d%1d",i,j);
12081: for(k=1; k<=ncovmodel;k++){
12082: /* printf(" %lf",param[i][j][k]); */
12083: /* fprintf(ficparo," %lf",param[i][j][k]); */
12084: printf(" 0.");
12085: fprintf(ficparo," 0.");
12086: }
12087: printf("\n");
12088: fprintf(ficparo,"\n");
12089: }
12090: }
12091: printf("# Scales (for hessian or gradient estimation)\n");
12092: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
12093: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
12094: for(i=1; i <=nlstate; i++){
12095: jj=0;
12096: for(j=1; j <=nlstate+ndeath; j++){
12097: if(j==i) continue;
12098: jj++;
12099: fprintf(ficparo,"%1d%1d",i,j);
12100: printf("%1d%1d",i,j);
12101: fflush(stdout);
12102: for(k=1; k<=ncovmodel;k++){
12103: /* printf(" %le",delti3[i][j][k]); */
12104: /* fprintf(ficparo," %le",delti3[i][j][k]); */
12105: printf(" 0.");
12106: fprintf(ficparo," 0.");
12107: }
12108: numlinepar++;
12109: printf("\n");
12110: fprintf(ficparo,"\n");
12111: }
12112: }
12113: printf("# Covariance matrix\n");
12114: /* # 121 Var(a12)\n\ */
12115: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12116: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12117: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12118: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12119: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12120: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12121: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12122: fflush(stdout);
12123: fprintf(ficparo,"# Covariance matrix\n");
12124: /* # 121 Var(a12)\n\ */
12125: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12126: /* # ...\n\ */
12127: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12128:
12129: for(itimes=1;itimes<=2;itimes++){
12130: jj=0;
12131: for(i=1; i <=nlstate; i++){
12132: for(j=1; j <=nlstate+ndeath; j++){
12133: if(j==i) continue;
12134: for(k=1; k<=ncovmodel;k++){
12135: jj++;
12136: ca[0]= k+'a'-1;ca[1]='\0';
12137: if(itimes==1){
12138: printf("#%1d%1d%d",i,j,k);
12139: fprintf(ficparo,"#%1d%1d%d",i,j,k);
12140: }else{
12141: printf("%1d%1d%d",i,j,k);
12142: fprintf(ficparo,"%1d%1d%d",i,j,k);
12143: /* printf(" %.5le",matcov[i][j]); */
12144: }
12145: ll=0;
12146: for(li=1;li <=nlstate; li++){
12147: for(lj=1;lj <=nlstate+ndeath; lj++){
12148: if(lj==li) continue;
12149: for(lk=1;lk<=ncovmodel;lk++){
12150: ll++;
12151: if(ll<=jj){
12152: cb[0]= lk +'a'-1;cb[1]='\0';
12153: if(ll<jj){
12154: if(itimes==1){
12155: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12156: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12157: }else{
12158: printf(" 0.");
12159: fprintf(ficparo," 0.");
12160: }
12161: }else{
12162: if(itimes==1){
12163: printf(" Var(%s%1d%1d)",ca,i,j);
12164: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
12165: }else{
12166: printf(" 0.");
12167: fprintf(ficparo," 0.");
12168: }
12169: }
12170: }
12171: } /* end lk */
12172: } /* end lj */
12173: } /* end li */
12174: printf("\n");
12175: fprintf(ficparo,"\n");
12176: numlinepar++;
12177: } /* end k*/
12178: } /*end j */
12179: } /* end i */
12180: } /* end itimes */
12181:
12182: } /* end of prwizard */
12183: /******************* Gompertz Likelihood ******************************/
12184: double gompertz(double x[])
12185: {
1.302 brouard 12186: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
1.126 brouard 12187: int i,n=0; /* n is the size of the sample */
12188:
1.220 brouard 12189: for (i=1;i<=imx ; i++) {
1.126 brouard 12190: sump=sump+weight[i];
12191: /* sump=sump+1;*/
12192: num=num+1;
12193: }
1.302 brouard 12194: L=0.0;
12195: /* agegomp=AGEGOMP; */
1.126 brouard 12196: /* for (i=0; i<=imx; i++)
12197: 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]);*/
12198:
1.302 brouard 12199: for (i=1;i<=imx ; i++) {
12200: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
12201: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
12202: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
12203: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
12204: * +
12205: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
12206: */
12207: if (wav[i] > 1 || agedc[i] < AGESUP) {
12208: if (cens[i] == 1){
12209: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
12210: } else if (cens[i] == 0){
1.126 brouard 12211: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
1.302 brouard 12212: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
12213: } else
12214: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
1.126 brouard 12215: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
1.302 brouard 12216: L=L+A*weight[i];
1.126 brouard 12217: /* 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 12218: }
12219: }
1.126 brouard 12220:
1.302 brouard 12221: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
1.126 brouard 12222:
12223: return -2*L*num/sump;
12224: }
12225:
1.136 brouard 12226: #ifdef GSL
12227: /******************* Gompertz_f Likelihood ******************************/
12228: double gompertz_f(const gsl_vector *v, void *params)
12229: {
1.302 brouard 12230: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
1.136 brouard 12231: double *x= (double *) v->data;
12232: int i,n=0; /* n is the size of the sample */
12233:
12234: for (i=0;i<=imx-1 ; i++) {
12235: sump=sump+weight[i];
12236: /* sump=sump+1;*/
12237: num=num+1;
12238: }
12239:
12240:
12241: /* for (i=0; i<=imx; i++)
12242: 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]);*/
12243: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
12244: for (i=1;i<=imx ; i++)
12245: {
12246: if (cens[i] == 1 && wav[i]>1)
12247: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
12248:
12249: if (cens[i] == 0 && wav[i]>1)
12250: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
12251: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
12252:
12253: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
12254: if (wav[i] > 1 ) { /* ??? */
12255: LL=LL+A*weight[i];
12256: /* 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]);*/
12257: }
12258: }
12259:
12260: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
12261: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
12262:
12263: return -2*LL*num/sump;
12264: }
12265: #endif
12266:
1.126 brouard 12267: /******************* Printing html file ***********/
1.201 brouard 12268: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 12269: int lastpass, int stepm, int weightopt, char model[],\
12270: int imx, double p[],double **matcov,double agemortsup){
12271: int i,k;
12272:
12273: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
12274: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
12275: for (i=1;i<=2;i++)
12276: 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 12277: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 12278: fprintf(fichtm,"</ul>");
12279:
12280: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
12281:
12282: 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>");
12283:
12284: for (k=agegomp;k<(agemortsup-2);k++)
12285: 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]);
12286:
12287:
12288: fflush(fichtm);
12289: }
12290:
12291: /******************* Gnuplot file **************/
1.201 brouard 12292: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 12293:
12294: char dirfileres[132],optfileres[132];
1.164 brouard 12295:
1.359 ! brouard 12296: /*int ng;*/
1.126 brouard 12297:
12298:
12299: /*#ifdef windows */
12300: fprintf(ficgp,"cd \"%s\" \n",pathc);
12301: /*#endif */
12302:
12303:
12304: strcpy(dirfileres,optionfilefiname);
12305: strcpy(optfileres,"vpl");
1.199 brouard 12306: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 12307: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 12308: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 12309: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 12310: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
12311:
12312: }
12313:
1.136 brouard 12314: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
12315: {
1.126 brouard 12316:
1.136 brouard 12317: /*-------- data file ----------*/
12318: FILE *fic;
12319: char dummy[]=" ";
1.359 ! brouard 12320: int i = 0, j = 0, n = 0, iv = 0;/* , v;*/
1.223 brouard 12321: int lstra;
1.136 brouard 12322: int linei, month, year,iout;
1.302 brouard 12323: int noffset=0; /* This is the offset if BOM data file */
1.136 brouard 12324: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 12325: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 12326: char *stratrunc;
1.223 brouard 12327:
1.349 brouard 12328: /* DummyV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
12329: /* FixedV=ivector(-1,NCOVMAX); /\* 1 to 3 *\/ */
1.339 brouard 12330:
12331: ncovcolt=ncovcol+nqv+ntv+nqtv; /* total of covariates in the data, not in the model equation */
12332:
1.136 brouard 12333: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 12334: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12335: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 12336: }
1.126 brouard 12337:
1.302 brouard 12338: /* Is it a BOM UTF-8 Windows file? */
12339: /* First data line */
12340: linei=0;
12341: while(fgets(line, MAXLINE, fic)) {
12342: noffset=0;
12343: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
12344: {
12345: noffset=noffset+3;
12346: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
12347: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
12348: fflush(ficlog); return 1;
12349: }
12350: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
12351: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
12352: {
12353: noffset=noffset+2;
1.304 brouard 12354: 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);
12355: 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 12356: fflush(ficlog); return 1;
12357: }
12358: else if( line[0] == 0 && line[1] == 0)
12359: {
12360: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
12361: noffset=noffset+4;
1.304 brouard 12362: 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);
12363: 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 12364: fflush(ficlog); return 1;
12365: }
12366: } else{
12367: ;/*printf(" Not a BOM file\n");*/
12368: }
12369: /* If line starts with a # it is a comment */
12370: if (line[noffset] == '#') {
12371: linei=linei+1;
12372: break;
12373: }else{
12374: break;
12375: }
12376: }
12377: fclose(fic);
12378: if((fic=fopen(datafile,"r"))==NULL) {
12379: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
12380: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
12381: }
12382: /* Not a Bom file */
12383:
1.136 brouard 12384: i=1;
12385: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
12386: linei=linei+1;
12387: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
12388: if(line[j] == '\t')
12389: line[j] = ' ';
12390: }
12391: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
12392: ;
12393: };
12394: line[j+1]=0; /* Trims blanks at end of line */
12395: if(line[0]=='#'){
12396: fprintf(ficlog,"Comment line\n%s\n",line);
12397: printf("Comment line\n%s\n",line);
12398: continue;
12399: }
12400: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 12401: strcpy(line, linetmp);
1.223 brouard 12402:
12403: /* Loops on waves */
12404: for (j=maxwav;j>=1;j--){
12405: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 12406: cutv(stra, strb, line, ' ');
12407: if(strb[0]=='.') { /* Missing value */
12408: lval=-1;
12409: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
1.341 brouard 12410: cotvar[j][ncovcol+nqv+ntv+iv][i]=-1; /* For performance reasons */
1.238 brouard 12411: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
12412: 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);
12413: 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);
12414: return 1;
12415: }
12416: }else{
12417: errno=0;
12418: /* what_kind_of_number(strb); */
12419: dval=strtod(strb,&endptr);
12420: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
12421: /* if(strb != endptr && *endptr == '\0') */
12422: /* dval=dlval; */
12423: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12424: if( strb[0]=='\0' || (*endptr != '\0')){
12425: 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);
12426: 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);
12427: return 1;
12428: }
12429: cotqvar[j][iv][i]=dval;
1.341 brouard 12430: cotvar[j][ncovcol+nqv+ntv+iv][i]=dval; /* because cotvar starts now at first ntv */
1.238 brouard 12431: }
12432: strcpy(line,stra);
1.223 brouard 12433: }/* end loop ntqv */
1.225 brouard 12434:
1.223 brouard 12435: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 12436: cutv(stra, strb, line, ' ');
12437: if(strb[0]=='.') { /* Missing value */
12438: lval=-1;
12439: }else{
12440: errno=0;
12441: lval=strtol(strb,&endptr,10);
12442: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
12443: if( strb[0]=='\0' || (*endptr != '\0')){
12444: 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);
12445: 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);
12446: return 1;
12447: }
12448: }
12449: if(lval <-1 || lval >1){
12450: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 12451: 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 12452: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 12453: For example, for multinomial values like 1, 2 and 3,\n \
12454: build V1=0 V2=0 for the reference value (1),\n \
12455: V1=1 V2=0 for (2) \n \
1.223 brouard 12456: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 12457: output of IMaCh is often meaningless.\n \
1.319 brouard 12458: Exiting.\n",lval,linei, i,line,iv,j);
1.238 brouard 12459: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 12460: 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 12461: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 12462: For example, for multinomial values like 1, 2 and 3,\n \
12463: build V1=0 V2=0 for the reference value (1),\n \
12464: V1=1 V2=0 for (2) \n \
1.223 brouard 12465: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 12466: output of IMaCh is often meaningless.\n \
1.319 brouard 12467: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
1.238 brouard 12468: return 1;
12469: }
1.341 brouard 12470: cotvar[j][ncovcol+nqv+iv][i]=(double)(lval);
1.238 brouard 12471: strcpy(line,stra);
1.223 brouard 12472: }/* end loop ntv */
1.225 brouard 12473:
1.223 brouard 12474: /* Statuses at wave */
1.137 brouard 12475: cutv(stra, strb, line, ' ');
1.223 brouard 12476: if(strb[0]=='.') { /* Missing value */
1.238 brouard 12477: lval=-1;
1.136 brouard 12478: }else{
1.238 brouard 12479: errno=0;
12480: lval=strtol(strb,&endptr,10);
12481: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
1.347 brouard 12482: if( strb[0]=='\0' || (*endptr != '\0' )){
12483: 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);
12484: 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);
12485: return 1;
12486: }else if( lval==0 || lval > nlstate+ndeath){
1.348 brouard 12487: 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);
12488: 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 12489: return 1;
12490: }
1.136 brouard 12491: }
1.225 brouard 12492:
1.136 brouard 12493: s[j][i]=lval;
1.225 brouard 12494:
1.223 brouard 12495: /* Date of Interview */
1.136 brouard 12496: strcpy(line,stra);
12497: cutv(stra, strb,line,' ');
1.169 brouard 12498: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12499: }
1.169 brouard 12500: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 12501: month=99;
12502: year=9999;
1.136 brouard 12503: }else{
1.225 brouard 12504: 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);
12505: 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);
12506: return 1;
1.136 brouard 12507: }
12508: anint[j][i]= (double) year;
1.302 brouard 12509: mint[j][i]= (double)month;
12510: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
12511: /* 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]); */
12512: /* 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]); */
12513: /* } */
1.136 brouard 12514: strcpy(line,stra);
1.223 brouard 12515: } /* End loop on waves */
1.225 brouard 12516:
1.223 brouard 12517: /* Date of death */
1.136 brouard 12518: cutv(stra, strb,line,' ');
1.169 brouard 12519: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12520: }
1.169 brouard 12521: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 12522: month=99;
12523: year=9999;
12524: }else{
1.141 brouard 12525: 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 12526: 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);
12527: return 1;
1.136 brouard 12528: }
12529: andc[i]=(double) year;
12530: moisdc[i]=(double) month;
12531: strcpy(line,stra);
12532:
1.223 brouard 12533: /* Date of birth */
1.136 brouard 12534: cutv(stra, strb,line,' ');
1.169 brouard 12535: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 12536: }
1.169 brouard 12537: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 12538: month=99;
12539: year=9999;
12540: }else{
1.141 brouard 12541: 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);
12542: 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 12543: return 1;
1.136 brouard 12544: }
12545: if (year==9999) {
1.141 brouard 12546: 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);
12547: 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 12548: return 1;
12549:
1.136 brouard 12550: }
12551: annais[i]=(double)(year);
1.302 brouard 12552: moisnais[i]=(double)(month);
12553: for (j=1;j<=maxwav;j++){
12554: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
12555: 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]);
12556: 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]);
12557: }
12558: }
12559:
1.136 brouard 12560: strcpy(line,stra);
1.225 brouard 12561:
1.223 brouard 12562: /* Sample weight */
1.136 brouard 12563: cutv(stra, strb,line,' ');
12564: errno=0;
12565: dval=strtod(strb,&endptr);
12566: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 12567: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
12568: 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 12569: fflush(ficlog);
12570: return 1;
12571: }
12572: weight[i]=dval;
12573: strcpy(line,stra);
1.225 brouard 12574:
1.223 brouard 12575: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
12576: cutv(stra, strb, line, ' ');
12577: if(strb[0]=='.') { /* Missing value */
1.225 brouard 12578: lval=-1;
1.311 brouard 12579: coqvar[iv][i]=NAN;
12580: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
1.223 brouard 12581: }else{
1.225 brouard 12582: errno=0;
12583: /* what_kind_of_number(strb); */
12584: dval=strtod(strb,&endptr);
12585: /* if(strb != endptr && *endptr == '\0') */
12586: /* dval=dlval; */
12587: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
12588: if( strb[0]=='\0' || (*endptr != '\0')){
12589: 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);
12590: 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);
12591: return 1;
12592: }
12593: coqvar[iv][i]=dval;
1.226 brouard 12594: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 12595: }
12596: strcpy(line,stra);
12597: }/* end loop nqv */
1.136 brouard 12598:
1.223 brouard 12599: /* Covariate values */
1.136 brouard 12600: for (j=ncovcol;j>=1;j--){
12601: cutv(stra, strb,line,' ');
1.223 brouard 12602: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 12603: lval=-1;
1.136 brouard 12604: }else{
1.225 brouard 12605: errno=0;
12606: lval=strtol(strb,&endptr,10);
12607: if( strb[0]=='\0' || (*endptr != '\0')){
12608: 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);
12609: 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);
12610: return 1;
12611: }
1.136 brouard 12612: }
12613: if(lval <-1 || lval >1){
1.225 brouard 12614: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 12615: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12616: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 12617: For example, for multinomial values like 1, 2 and 3,\n \
12618: build V1=0 V2=0 for the reference value (1),\n \
12619: V1=1 V2=0 for (2) \n \
1.136 brouard 12620: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 12621: output of IMaCh is often meaningless.\n \
1.136 brouard 12622: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 12623: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 12624: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
12625: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 12626: For example, for multinomial values like 1, 2 and 3,\n \
12627: build V1=0 V2=0 for the reference value (1),\n \
12628: V1=1 V2=0 for (2) \n \
1.136 brouard 12629: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 12630: output of IMaCh is often meaningless.\n \
1.136 brouard 12631: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 12632: return 1;
1.136 brouard 12633: }
12634: covar[j][i]=(double)(lval);
12635: strcpy(line,stra);
12636: }
12637: lstra=strlen(stra);
1.225 brouard 12638:
1.136 brouard 12639: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
12640: stratrunc = &(stra[lstra-9]);
12641: num[i]=atol(stratrunc);
12642: }
12643: else
12644: num[i]=atol(stra);
12645: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
12646: 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;}*/
12647:
12648: i=i+1;
12649: } /* End loop reading data */
1.225 brouard 12650:
1.136 brouard 12651: *imax=i-1; /* Number of individuals */
12652: fclose(fic);
1.225 brouard 12653:
1.136 brouard 12654: return (0);
1.164 brouard 12655: /* endread: */
1.225 brouard 12656: printf("Exiting readdata: ");
12657: fclose(fic);
12658: return (1);
1.223 brouard 12659: }
1.126 brouard 12660:
1.234 brouard 12661: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 12662: char *p1 = *stri, *p2 = *stri;
1.235 brouard 12663: while (*p2 == ' ')
1.234 brouard 12664: p2++;
12665: /* while ((*p1++ = *p2++) !=0) */
12666: /* ; */
12667: /* do */
12668: /* while (*p2 == ' ') */
12669: /* p2++; */
12670: /* while (*p1++ == *p2++); */
12671: *stri=p2;
1.145 brouard 12672: }
12673:
1.330 brouard 12674: int decoderesult( char resultline[], int nres)
1.230 brouard 12675: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
12676: {
1.235 brouard 12677: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 12678: char resultsav[MAXLINE];
1.330 brouard 12679: /* int resultmodel[MAXLINE]; */
1.334 brouard 12680: /* int modelresult[MAXLINE]; */
1.230 brouard 12681: char stra[80], strb[80], strc[80], strd[80],stre[80];
12682:
1.234 brouard 12683: removefirstspace(&resultline);
1.332 brouard 12684: printf("decoderesult:%s\n",resultline);
1.230 brouard 12685:
1.332 brouard 12686: strcpy(resultsav,resultline);
1.342 brouard 12687: /* printf("Decoderesult resultsav=\"%s\" resultline=\"%s\"\n", resultsav, resultline); */
1.230 brouard 12688: if (strlen(resultsav) >1){
1.334 brouard 12689: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' in this resultline */
1.230 brouard 12690: }
1.353 brouard 12691: if(j == 0 && cptcovs== 0){ /* Resultline but no = and no covariate in the model */
1.253 brouard 12692: TKresult[nres]=0; /* Combination for the nresult and the model */
12693: return (0);
12694: }
1.234 brouard 12695: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
1.353 brouard 12696: 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);
12697: 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);
12698: if(j==0)
12699: return 1;
1.234 brouard 12700: }
1.334 brouard 12701: for(k=1; k<=j;k++){ /* Loop on any covariate of the RESULT LINE */
1.234 brouard 12702: if(nbocc(resultsav,'=') >1){
1.318 brouard 12703: 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 12704: /* 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 12705: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
1.332 brouard 12706: /* If a blank, then strc="V4=" and strd='\0' */
12707: if(strc[0]=='\0'){
12708: printf("Error in resultline, probably a blank after the \"%s\", \"result:%s\", stra=\"%s\" resultsav=\"%s\"\n",strb,resultline, stra, resultsav);
12709: fprintf(ficlog,"Error in resultline, probably a blank after the \"V%s=\", resultline=%s\n",strb,resultline);
12710: return 1;
12711: }
1.234 brouard 12712: }else
12713: cutl(strc,strd,resultsav,'=');
1.318 brouard 12714: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
1.234 brouard 12715:
1.230 brouard 12716: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
1.318 brouard 12717: 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 12718: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
12719: /* cptcovsel++; */
12720: if (nbocc(stra,'=') >0)
12721: strcpy(resultsav,stra); /* and analyzes it */
12722: }
1.235 brouard 12723: /* Checking for missing or useless values in comparison of current model needs */
1.332 brouard 12724: /* 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 12725: 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 12726: if(Typevar[k1]==0){ /* Single covariate in model */
12727: /* 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.234 brouard 12728: match=0;
1.318 brouard 12729: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12730: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
1.334 brouard 12731: modelresult[nres][k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.318 brouard 12732: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
1.234 brouard 12733: break;
12734: }
12735: }
12736: if(match == 0){
1.338 brouard 12737: 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]);
12738: 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 12739: return 1;
1.234 brouard 12740: }
1.332 brouard 12741: }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*/
12742: /* We feed resultmodel[k1]=k2; */
12743: match=0;
12744: 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 */
12745: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
1.334 brouard 12746: 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 12747: resultmodel[nres][k1]=k2; /* Added here */
1.342 brouard 12748: /* printf("Decoderesult first modelresult[k2=%d]=%d (k1) V%d*AGE\n",k2,k1,Tvar[k1]); */
1.332 brouard 12749: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12750: break;
12751: }
12752: }
12753: if(match == 0){
1.338 brouard 12754: 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]);
12755: 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 12756: return 1;
12757: }
1.349 brouard 12758: }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 12759: /* resultmodel[nres][of such a Vn * Vm product k1] is not unique, so can't exist, we feed Tvard[k1][1] and [2] */
12760: match=0;
1.342 brouard 12761: /* 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 12762: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12763: if(Tvardk[k1][1]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12764: /* modelresult[k2]=k1; */
1.342 brouard 12765: /* printf("Decoderesult first Product modelresult[k2=%d]=%d (k1) V%d * \n",k2,k1,Tvarsel[k2]); */
1.332 brouard 12766: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12767: }
12768: }
12769: if(match == 0){
1.349 brouard 12770: 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);
12771: 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 12772: return 1;
12773: }
12774: match=0;
12775: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
12776: if(Tvardk[k1][2]==Tvarsel[k2]) {/* Tvardk is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
12777: /* modelresult[k2]=k1;*/
1.342 brouard 12778: /* printf("Decoderesult second Product modelresult[k2=%d]=%d (k1) * V%d \n ",k2,k1,Tvarsel[k2]); */
1.332 brouard 12779: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
12780: break;
12781: }
12782: }
12783: if(match == 0){
1.349 brouard 12784: 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);
12785: 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 12786: return 1;
12787: }
12788: }/* End of testing */
1.333 brouard 12789: }/* End loop cptcovt */
1.235 brouard 12790: /* Checking for missing or useless values in comparison of current model needs */
1.332 brouard 12791: /* Feeds resultmodel[nres][k1]=k2 for single covariate (k1) in the model equation */
1.334 brouard 12792: 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)
12793: * Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 12794: match=0;
1.318 brouard 12795: 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 12796: if(Typevar[k1]==0){ /* Single only */
1.349 brouard 12797: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 What if a product? */
1.330 brouard 12798: 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 12799: 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 12800: ++match;
12801: }
12802: }
12803: }
12804: if(match == 0){
1.338 brouard 12805: printf("Error in result line: variable V%d is missing in model; result: %s, model=1+age+%s\n",Tvarsel[k2], resultline, model);
12806: 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 12807: return 1;
1.234 brouard 12808: }else if(match > 1){
1.338 brouard 12809: printf("Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
12810: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=1+age+%s\n",k2, resultline, model);
1.310 brouard 12811: return 1;
1.234 brouard 12812: }
12813: }
1.334 brouard 12814: /* cptcovres=j /\* Number of variables in the resultline is equal to cptcovs and thus useless *\/ */
1.234 brouard 12815: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 12816: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.330 brouard 12817: /* nres=1st result line: V4=1 V5=25.1 V3=0 V2=8 V1=1 */
12818: /* 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*/
12819: /* nres=2nd result line: V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.235 brouard 12820: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
12821: /* 1 0 0 0 */
12822: /* 2 1 0 0 */
12823: /* 3 0 1 0 */
1.330 brouard 12824: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 (nres=2)*/
1.235 brouard 12825: /* 5 0 0 1 */
1.330 brouard 12826: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 (nres=1)*/
1.235 brouard 12827: /* 7 0 1 1 */
12828: /* 8 1 1 1 */
1.237 brouard 12829: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
12830: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
12831: /* V5*age V5 known which value for nres? */
12832: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.334 brouard 12833: 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.
12834: * loop on position k1 in the MODEL LINE */
1.331 brouard 12835: /* k counting number of combination of single dummies in the equation model */
12836: /* k4 counting single dummies in the equation model */
12837: /* k4q counting single quantitatives in the equation model */
1.344 brouard 12838: 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 12839: /* 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 12840: /* 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 12841: /* Value in the (current nres) resultline of the variable at the k1th position in the model equation resultmodel[nres][k1]= k3 */
1.332 brouard 12842: /* resultmodel[nres][k1]=k3: k1th position in the model correspond to the k3 position in the resultline */
12843: /* k3 is the position in the nres result line of the k1th variable of the model equation */
12844: /* Tvarsel[k3]: Name of the variable at the k3th position in the result line. */
12845: /* Tvalsel[k3]: Value of the variable at the k3th position in the result line. */
12846: /* Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline */
1.334 brouard 12847: /* Tvresult[nres][result_position]= name of the dummy variable at the result_position in the nres resultline */
1.332 brouard 12848: /* Tinvresult[nres][Name of a dummy variable]= value of the variable in the result line */
1.330 brouard 12849: /* TinvDoQresult[nres][Name of a Dummy or Q variable]= value of the variable in the result line */
1.332 brouard 12850: k3= resultmodel[nres][k1]; /* From position k1 in model get position k3 in result line */
12851: /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
12852: 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 12853: 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 12854: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][Name]=Value; stores the value into the name of the variable. */
1.332 brouard 12855: /* Tinvresult[nres][4]=1 */
1.334 brouard 12856: /* Tresult[nres][k4+1]=Tvalsel[k3];/\* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) *\/ */
12857: Tresult[nres][k3]=Tvalsel[k3];/* Tresult[nres=2][1]=1(V4=1) Tresult[nres=2][2]=0(V3=0) */
12858: /* Tvresult[nres][k4+1]=(int)Tvarsel[k3];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12859: Tvresult[nres][k3]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
1.237 brouard 12860: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.334 brouard 12861: precov[nres][k1]=Tvalsel[k3]; /* Value from resultline of the variable at the k1 position in the model */
1.342 brouard 12862: /* 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 12863: k4++;;
1.331 brouard 12864: }else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Quantitative and single */
1.330 brouard 12865: /* Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
1.332 brouard 12866: /* Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline */
1.330 brouard 12867: /* Tqinvresult[nres][Name of a quantitative variable]= value of the variable in the result line */
1.332 brouard 12868: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 5 =k3q */
12869: k2q=(int)Tvarsel[k3q]; /* Name of variable at k3q th position in the resultline */
12870: /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
1.334 brouard 12871: /* Tqresult[nres][k4q+1]=Tvalsel[k3q]; /\* Tqresult[nres][1]=25.1 *\/ */
12872: /* Tvresult[nres][k4q+1]=(int)Tvarsel[k3q];/\* Tvresult[nres][1]=4 Tvresult[nres][3]=1 *\/ */
12873: /* Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /\* Tvqresult[nres][1]=5 *\/ */
12874: Tqresult[nres][k3q]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
12875: Tvresult[nres][k3q]=(int)Tvarsel[k3q];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
12876: Tvqresult[nres][k3q]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
1.237 brouard 12877: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.330 brouard 12878: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.332 brouard 12879: precov[nres][k1]=Tvalsel[k3q];
1.342 brouard 12880: /* 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 12881: k4q++;;
1.350 brouard 12882: }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"*/
12883: /* Tvar[k1]; */ /* Age variable */ /* 17 age*V6*V2 ?*/
1.332 brouard 12884: /* Wrong we want the value of variable name Tvar[k1] */
1.350 brouard 12885: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
12886: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
12887: /* 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]]); */
12888: }else{
12889: k3= resultmodel[nres][k1]; /* nres=1 k1=2 resultmodel[2(V4)] = 1=k3 ; k1=3 resultmodel[3(V3)] = 2=k3*/
12890: 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)*/
12891: TinvDoQresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* TinvDoQresult[nres][4]=1 */
12892: precov[nres][k1]=Tvalsel[k3];
12893: }
1.342 brouard 12894: /* 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 12895: }else if( Dummy[k1]==3 ){ /* For quant with age product */
1.350 brouard 12896: if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
12897: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
12898: /* 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]]); */
12899: }else{
12900: k3q= resultmodel[nres][k1]; /* resultmodel[1(V5)] = 25.1=k3q */
12901: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
12902: TinvDoQresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* TinvDoQresult[nres][5]=25.1 */
12903: precov[nres][k1]=Tvalsel[k3q];
12904: }
1.342 brouard 12905: /* 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 12906: }else if(Typevar[k1]==2 || Typevar[k1]==3 ){ /* For product quant or dummy (with or without age) */
1.332 brouard 12907: precov[nres][k1]=TinvDoQresult[nres][Tvardk[k1][1]] * TinvDoQresult[nres][Tvardk[k1][2]];
1.342 brouard 12908: /* 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 12909: }else{
1.332 brouard 12910: printf("Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
12911: fprintf(ficlog,"Error Decoderesult probably a product Dummy[%d]==%d && Typevar[%d]==%d\n", k1, Dummy[k1], k1, Typevar[k1]);
1.235 brouard 12912: }
12913: }
1.234 brouard 12914:
1.334 brouard 12915: TKresult[nres]=++k; /* Number of combinations of dummies for the nresult and the model =Tvalsel[k3]*pow(2,k4) + 1*/
1.230 brouard 12916: return (0);
12917: }
1.235 brouard 12918:
1.230 brouard 12919: int decodemodel( char model[], int lastobs)
12920: /**< This routine decodes the model and returns:
1.224 brouard 12921: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
12922: * - nagesqr = 1 if age*age in the model, otherwise 0.
12923: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
12924: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
12925: * - cptcovage number of covariates with age*products =2
12926: * - cptcovs number of simple covariates
1.339 brouard 12927: * ncovcolt=ncovcol+nqv+ntv+nqtv total of covariates in the data, not in the model equation
1.224 brouard 12928: * - 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 12929: * which is a new column after the 9 (ncovcol+nqv+ntv+nqtv) variables.
1.319 brouard 12930: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
1.224 brouard 12931: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
12932: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
12933: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
12934: */
1.319 brouard 12935: /* 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 12936: {
1.359 ! brouard 12937: int i, j, k, ks;/* , v;*/
1.349 brouard 12938: int n,m;
12939: int j1, k1, k11, k12, k2, k3, k4;
12940: char modelsav[300];
12941: char stra[300], strb[300], strc[300], strd[300],stre[300],strf[300];
1.187 brouard 12942: char *strpt;
1.349 brouard 12943: int **existcomb;
12944:
12945: existcomb=imatrix(1,NCOVMAX,1,NCOVMAX);
12946: for(i=1;i<=NCOVMAX;i++)
12947: for(j=1;j<=NCOVMAX;j++)
12948: existcomb[i][j]=0;
12949:
1.145 brouard 12950: /*removespace(model);*/
1.136 brouard 12951: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.349 brouard 12952: j=0, j1=0, k1=0, k12=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 12953: if (strstr(model,"AGE") !=0){
1.192 brouard 12954: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
12955: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 12956: return 1;
12957: }
1.141 brouard 12958: if (strstr(model,"v") !=0){
1.338 brouard 12959: printf("Error. 'v' must be in upper case 'V' model=1+age+%s ",model);
12960: fprintf(ficlog,"Error. 'v' must be in upper case model=1+age+%s ",model);fflush(ficlog);
1.141 brouard 12961: return 1;
12962: }
1.187 brouard 12963: strcpy(modelsav,model);
12964: if ((strpt=strstr(model,"age*age")) !=0){
1.338 brouard 12965: printf(" strpt=%s, model=1+age+%s\n",strpt, model);
1.187 brouard 12966: if(strpt != model){
1.338 brouard 12967: printf("Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 12968: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 12969: corresponding column of parameters.\n",model);
1.338 brouard 12970: fprintf(ficlog,"Error in model: 'model=1+age+%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 12971: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 12972: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 12973: return 1;
1.225 brouard 12974: }
1.187 brouard 12975: nagesqr=1;
12976: if (strstr(model,"+age*age") !=0)
1.234 brouard 12977: substrchaine(modelsav, model, "+age*age");
1.187 brouard 12978: else if (strstr(model,"age*age+") !=0)
1.234 brouard 12979: substrchaine(modelsav, model, "age*age+");
1.187 brouard 12980: else
1.234 brouard 12981: substrchaine(modelsav, model, "age*age");
1.187 brouard 12982: }else
12983: nagesqr=0;
1.349 brouard 12984: 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 12985: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
12986: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.351 brouard 12987: cptcovs=0; /**< Number of simple covariates V1 +V1*age +V3 +V3*V4 +age*age => V1 + V3 =4+1-3=2 Wrong */
1.187 brouard 12988: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 12989: * cst, age and age*age
12990: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
12991: /* including age products which are counted in cptcovage.
12992: * but the covariates which are products must be treated
12993: * separately: ncovn=4- 2=2 (V1+V3). */
1.349 brouard 12994: cptcovprod=0; /**< Number of products V1*V2 +v3*age = 2 */
12995: cptcovdageprod=0; /* Number of doouble products with age age*Vn*VM or Vn*age*Vm or Vn*Vm*age */
1.187 brouard 12996: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.349 brouard 12997: cptcovprodage=0;
12998: /* cptcovprodage=nboccstr(modelsav,"age");*/
1.225 brouard 12999:
1.187 brouard 13000: /* Design
13001: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
13002: * < ncovcol=8 >
13003: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
13004: * k= 1 2 3 4 5 6 7 8
13005: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
1.345 brouard 13006: * covar[k,i], are for fixed covariates, value of kth covariate if not including age for individual i:
1.224 brouard 13007: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
13008: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 13009: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
13010: * Tage[++cptcovage]=k
1.345 brouard 13011: * if products, new covar are created after ncovcol + nqv (quanti fixed) with k1
1.187 brouard 13012: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
13013: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
13014: * 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
13015: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
13016: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
13017: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
1.345 brouard 13018: * < ncovcol=8 8 fixed covariate. Additional starts at 9 (V5*V6) and 10(V7*V8) >
1.187 brouard 13019: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
13020: * k= 1 2 3 4 5 6 7 8 9 10 11 12
1.345 brouard 13021: * Tvard[k]= 2 1 3 3 10 11 8 8 5 6 7 8
13022: * p Tvar[1]@12={2, 1, 3, 3, 9, 10, 8, 8}
1.187 brouard 13023: * p Tprod[1]@2={ 6, 5}
13024: *p Tvard[1][1]@4= {7, 8, 5, 6}
13025: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
13026: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1.319 brouard 13027: *How to reorganize? Tvars(orted)
1.187 brouard 13028: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
13029: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
13030: * {2, 1, 4, 8, 5, 6, 3, 7}
13031: * Struct []
13032: */
1.225 brouard 13033:
1.187 brouard 13034: /* This loop fills the array Tvar from the string 'model'.*/
13035: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
13036: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
13037: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
13038: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
13039: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
13040: /* k=1 Tvar[1]=2 (from V2) */
13041: /* k=5 Tvar[5] */
13042: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 13043: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 13044: /* } */
1.198 brouard 13045: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 13046: /*
13047: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 13048: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
13049: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
13050: }
1.187 brouard 13051: cptcovage=0;
1.351 brouard 13052:
13053: /* First loop in order to calculate */
13054: /* for age*VN*Vm
13055: * Provides, Typevar[k], Tage[cptcovage], existcomb[n][m], FixedV[ncovcolt+k12]
13056: * Tprod[k1]=k Tposprod[k]=k1; Tvard[k1][1] =m;
13057: */
13058: /* Needs FixedV[Tvardk[k][1]] */
13059: /* For others:
13060: * Sets Typevar[k];
13061: * Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13062: * Tposprod[k]=k11;
13063: * Tprod[k11]=k;
13064: * Tvardk[k][1] =m;
13065: * Needs FixedV[Tvardk[k][1]] == 0
13066: */
13067:
1.319 brouard 13068: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
13069: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
13070: 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" */
13071: if (nbocc(modelsav,'+')==0)
13072: strcpy(strb,modelsav); /* and analyzes it */
1.234 brouard 13073: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
13074: /*scanf("%d",i);*/
1.349 brouard 13075: 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 */
13076: 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 */
13077: 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 */
13078: Typevar[k]=3; /* 3 for age and double product age*Vn*Vm varying of fixed */
13079: if(strstr(strc,"age")!=0) { /* It means that strc=V2*age or age*V2 and thus that strd=Vn */
13080: cutl(stre,strf,strc,'*') ; /* strf=age or Vm, stre=Vm or age. If strc=V6*V2 then strf=V6 and stre=V2 */
13081: strcpy(strc,strb); /* save strb(=age*Vn*Vm) into strc */
13082: /* We want strb=Vn*Vm */
13083: if(strcmp(strf,"age")==0){ /* strf is "age" so that stre=Vm =V2 . */
13084: strcpy(strb,strd);
13085: strcat(strb,"*");
13086: strcat(strb,stre);
13087: }else{ /* strf=Vm If strf=V6 then stre=V2 */
13088: strcpy(strb,strf);
13089: strcat(strb,"*");
13090: strcat(strb,stre);
13091: strcpy(strd,strb); /* in order for strd to not be "age" for next test (will be Vn*Vm */
13092: }
1.351 brouard 13093: /* 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]]]); */
13094: /* FixedV[Tvar[Tage[k]]]=0; /\* HERY not sure if V7*V4*age Fixed might not exist yet*\/ */
1.349 brouard 13095: }else{ /* strc=Vn*Vm (and strd=age) and should be strb=Vn*Vm but want to keep original strb double product */
13096: strcpy(stre,strb); /* save full b in stre */
13097: strcpy(strb,strc); /* save short c in new short b for next block strb=Vn*Vm*/
13098: strcpy(strf,strc); /* save short c in new short f */
13099: cutl(strc,strd,strf,'*'); /* We get strd=Vn and strc=Vm for next block (strb=Vn*Vm)*/
13100: /* strcpy(strc,stre);*/ /* save full e in c for future */
13101: }
13102: cptcovdageprod++; /* double product with age Which product is it? */
13103: /* 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 *\/ */
13104: /* cutl(strc,strd,strb,'*'); /\* strd= V6 or V2 or age and strc= V2 or age or V2 *\/ */
1.234 brouard 13105: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
1.349 brouard 13106: n=atoi(stre);
1.234 brouard 13107: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
1.349 brouard 13108: m=atoi(strc);
13109: cptcovage++; /* Counts the number of covariates which include age as a product */
13110: Tage[cptcovage]=k; /* For age*V3*V2 gives the position in model of covariates associated with age Tage[1]=6 HERY too*/
13111: if(existcomb[n][m] == 0){
13112: /* r /home/brouard/Documents/Recherches/REVES/Zachary/Zach-2022/Feinuo_Sun/Feinuo-threeway/femV12V15_3wayintNBe.imach */
13113: 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);
13114: 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);
13115: fflush(ficlog);
13116: k1++; /* The combination Vn*Vm will be in the model so we create it at k1 */
13117: k12++;
13118: existcomb[n][m]=k1;
13119: existcomb[m][n]=k1;
13120: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1;
13121: 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*/
13122: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 Gives the k1 double product Vn*Vm or age*Vn*Vm at the k position */
13123: Tvard[k1][1] =m; /* m 1 for V1*/
13124: Tvardk[k][1] =m; /* m 1 for V1*/
13125: Tvard[k1][2] =n; /* n 4 for V4*/
13126: Tvardk[k][2] =n; /* n 4 for V4*/
1.351 brouard 13127: /* Tvar[Tage[cptcovage]]=k1;*/ /* Tvar[6=age*V3*V2]=9 (new fixed covariate) */ /* We don't know about Fixed yet HERE */
1.349 brouard 13128: 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 */
13129: for (i=1; i<=lastobs;i++){/* For fixed product */
13130: /* Computes the new covariate which is a product of
13131: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13132: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13133: }
13134: cptcovprodage++; /* Counting the number of fixed covariate with age */
13135: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13136: k12++;
13137: FixedV[ncovcolt+k12]=0;
13138: }else{ /*End of FixedV */
13139: cptcovprodvage++; /* Counting the number of varying covariate with age */
13140: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13141: k12++;
13142: FixedV[ncovcolt+k12]=1;
13143: }
13144: }else{ /* k1 Vn*Vm already exists */
13145: k11=existcomb[n][m];
13146: Tposprod[k]=k11; /* OK */
13147: Tvar[k]=Tvar[Tprod[k11]]; /* HERY */
13148: Tvardk[k][1]=m;
13149: Tvardk[k][2]=n;
13150: 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 */
13151: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13152: cptcovprodage++; /* Counting the number of fixed covariate with age */
13153: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13154: Tvar[Tage[cptcovage]]=k1;
13155: FixedV[ncovcolt+k12]=0; /* We expand Vn*Vm */
13156: k12++;
13157: FixedV[ncovcolt+k12]=0;
13158: }else{ /* Already exists but time varying (and age) */
13159: /*cptcovage++;*/ /* Counts the number of covariates which include age as a product */
13160: /*Tage[cptcovage]=k;*/ /* For age*V3*V2 Tage[1]=V3*V3=9 HERY too*/
13161: /* Tvar[Tage[cptcovage]]=k1; */
13162: cptcovprodvage++;
13163: FixedV[ncovcolt+k12]=1; /* We expand Vn*Vm */
13164: k12++;
13165: FixedV[ncovcolt+k12]=1;
13166: }
13167: }
13168: /* Tage[cptcovage]=k; /\* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 *\/ */
13169: /* Tvar[k]=k11; /\* HERY *\/ */
13170: } 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 */
13171: cptcovprod++;
13172: if (strcmp(strc,"age")==0) { /**< Model includes age: strb= Vn*age c=age d=Vn*/
13173: /* covar is not filled and then is empty */
13174: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
13175: 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 */
13176: Typevar[k]=1; /* 1 for age product */
13177: cptcovage++; /* Counts the number of covariates which include age as a product */
13178: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
13179: if( FixedV[Tvar[k]] == 0){
13180: cptcovprodage++; /* Counting the number of fixed covariate with age */
13181: }else{
13182: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
13183: }
13184: /*printf("stre=%s ", stre);*/
13185: } else if (strcmp(strd,"age")==0) { /* strb= age*Vn c=Vn */
13186: cutl(stre,strb,strc,'V');
13187: Tvar[k]=atoi(stre);
13188: Typevar[k]=1; /* 1 for age product */
13189: cptcovage++;
13190: Tage[cptcovage]=k;
13191: if( FixedV[Tvar[k]] == 0){
13192: cptcovprodage++; /* Counting the number of fixed covariate with age */
13193: }else{
13194: cptcovprodvage++; /* Counting the number of fixedvarying covariate with age */
1.339 brouard 13195: }
1.349 brouard 13196: }else{ /* for product Vn*Vm */
13197: Typevar[k]=2; /* 2 for product Vn*Vm */
13198: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
13199: n=atoi(stre);
13200: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
13201: m=atoi(strc);
13202: k1++;
13203: cptcovprodnoage++;
13204: if(existcomb[n][m] != 0 || existcomb[m][n] != 0){
13205: printf("Warning in model combination V%d*V%d already exists in the model in position k1=%d!\n",n,m,existcomb[n][m]);
13206: 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]);
13207: fflush(ficlog);
13208: k11=existcomb[n][m];
13209: Tvar[k]=ncovcol+nqv+ntv+nqtv+k11;
13210: Tposprod[k]=k11;
13211: Tprod[k11]=k;
13212: Tvardk[k][1] =m; /* m 1 for V1*/
13213: /* Tvard[k11][1] =m; /\* n 4 for V4*\/ */
13214: Tvardk[k][2] =n; /* n 4 for V4*/
13215: /* Tvard[k11][2] =n; /\* n 4 for V4*\/ */
13216: }else{ /* combination Vn*Vm doesn't exist we create it (no age)*/
13217: existcomb[n][m]=k1;
13218: existcomb[m][n]=k1;
13219: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* ncovcolt+k1; For model-covariate k tells which data-covariate to use but
13220: because this model-covariate is a construction we invent a new column
13221: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
13222: If already ncovcol=4 and model= V2 + V1 + V1*V4 + age*V3 + V3*V2
13223: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
13224: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=3 etc */
13225: /* Please remark that the new variables are model dependent */
13226: /* If we have 4 variable but the model uses only 3, like in
13227: * model= V1 + age*V1 + V2 + V3 + age*V2 + age*V3 + V1*V2 + V1*V3
13228: * k= 1 2 3 4 5 6 7 8
13229: * Tvar[k]=1 1 2 3 2 3 (5 6) (and not 4 5 because of V4 missing)
13230: * Tage[kk] [1]= 2 [2]=5 [3]=6 kk=1 to cptcovage=3
13231: * Tvar[Tage[kk]][1]=2 [2]=2 [3]=3
13232: */
13233: /* We need to feed some variables like TvarVV, but later on next loop because of ncovv (k2) is not correct */
13234: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 +V6*V2*age */
13235: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
13236: Tvard[k1][1] =m; /* m 1 for V1*/
13237: Tvardk[k][1] =m; /* m 1 for V1*/
13238: Tvard[k1][2] =n; /* n 4 for V4*/
13239: Tvardk[k][2] =n; /* n 4 for V4*/
13240: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
13241: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
13242: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
13243: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
13244: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
13245: 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 */
13246: for (i=1; i<=lastobs;i++){/* For fixed product */
13247: /* Computes the new covariate which is a product of
13248: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
13249: covar[ncovcolt+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
13250: }
13251: /* TvarVV[k2]=n; */
13252: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13253: /* TvarVV[k2+1]=m; */
13254: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13255: }else{ /* not FixedV */
13256: /* TvarVV[k2]=n; */
13257: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13258: /* TvarVV[k2+1]=m; */
13259: /* FixedV[ncovcolt+k2]=0; /\* or FixedV[Tvar[k]]=0; FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13260: }
13261: } /* End of creation of Vn*Vm if not created by age*Vn*Vm earlier */
13262: } /* End of product Vn*Vm */
13263: } /* End of age*double product or simple product */
13264: }else { /* not a product */
1.234 brouard 13265: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
13266: /* scanf("%d",i);*/
13267: cutl(strd,strc,strb,'V');
13268: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
13269: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
13270: Tvar[k]=atoi(strd);
13271: Typevar[k]=0; /* 0 for simple covariates */
13272: }
13273: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 13274: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 13275: scanf("%d",i);*/
1.187 brouard 13276: } /* end of loop + on total covariates */
1.351 brouard 13277:
13278:
1.187 brouard 13279: } /* end if strlen(modelsave == 0) age*age might exist */
13280: } /* end if strlen(model == 0) */
1.349 brouard 13281: 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 */
13282:
1.136 brouard 13283: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
13284: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 13285:
1.136 brouard 13286: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 13287: printf("cptcovprod=%d ", cptcovprod);
13288: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
13289: scanf("%d ",i);*/
13290:
13291:
1.230 brouard 13292: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
13293: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 13294: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
13295: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
13296: k = 1 2 3 4 5 6 7 8 9
13297: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
1.319 brouard 13298: Typevar[k]= 0 0 0 2 1 0 2 1 0
1.227 brouard 13299: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
13300: Dummy[k] 1 0 0 0 3 1 1 2 3
13301: Tmodelind[combination of covar]=k;
1.225 brouard 13302: */
13303: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 13304: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 13305: /* 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 13306: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.318 brouard 13307: printf("Model=1+age+%s\n\
1.349 brouard 13308: 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 13309: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13310: 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 13311: fprintf(ficlog,"Model=1+age+%s\n\
1.349 brouard 13312: 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 13313: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
13314: 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 13315: for(k=-1;k<=NCOVMAX; k++){ Fixed[k]=0; Dummy[k]=0;}
13316: for(k=1;k<=NCOVMAX; k++){TvarFind[k]=0; TvarVind[k]=0;}
1.351 brouard 13317:
13318:
13319: /* Second loop for calculating Fixed[k], Dummy[k]*/
13320:
13321:
1.349 brouard 13322: 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 13323: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 13324: Fixed[k]= 0;
13325: Dummy[k]= 0;
1.225 brouard 13326: ncoveff++;
1.232 brouard 13327: ncovf++;
1.234 brouard 13328: nsd++;
13329: modell[k].maintype= FTYPE;
13330: TvarsD[nsd]=Tvar[k];
13331: TvarsDind[nsd]=k;
1.330 brouard 13332: TnsdVar[Tvar[k]]=nsd;
1.234 brouard 13333: TvarF[ncovf]=Tvar[k];
13334: TvarFind[ncovf]=k;
13335: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13336: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.339 brouard 13337: /* }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 13338: }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 13339: Fixed[k]= 0;
13340: Dummy[k]= 1;
1.230 brouard 13341: nqfveff++;
1.234 brouard 13342: modell[k].maintype= FTYPE;
13343: modell[k].subtype= FQ;
13344: nsq++;
1.334 brouard 13345: TvarsQ[nsq]=Tvar[k]; /* Gives the variable name (extended to products) of first nsq simple quantitative covariates (fixed or time vary see below */
13346: TvarsQind[nsq]=k; /* Gives the position in the model equation of the first nsq simple quantitative covariates (fixed or time vary) */
1.232 brouard 13347: ncovf++;
1.234 brouard 13348: TvarF[ncovf]=Tvar[k];
13349: TvarFind[ncovf]=k;
1.231 brouard 13350: 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 13351: 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 13352: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.339 brouard 13353: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13354: /* model V1+V3+age*V1+age*V3+V1*V3 */
13355: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13356: ncovvt++;
13357: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13358: TvarVVind[ncovvt]=k; /* TvarVVind[1]=2 (second position in the model equation */
13359:
1.227 brouard 13360: Fixed[k]= 1;
13361: Dummy[k]= 0;
1.225 brouard 13362: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 13363: modell[k].maintype= VTYPE;
13364: modell[k].subtype= VD;
13365: nsd++;
13366: TvarsD[nsd]=Tvar[k];
13367: TvarsDind[nsd]=k;
1.330 brouard 13368: TnsdVar[Tvar[k]]=nsd; /* To be verified */
1.234 brouard 13369: ncovv++; /* Only simple time varying variables */
13370: TvarV[ncovv]=Tvar[k];
1.242 brouard 13371: 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 13372: 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 */
13373: 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 13374: 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);
13375: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 13376: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.339 brouard 13377: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13378: /* model V1+V3+age*V1+age*V3+V1*V3 */
13379: /* Tvar={1, 3, 1, 3, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13380: ncovvt++;
13381: TvarVV[ncovvt]=Tvar[k]; /* TvarVV[1]=V3 (first time varying in the model equation */
13382: TvarVVind[ncovvt]=k; /* TvarVV[1]=V3 (first time varying in the model equation */
13383:
1.234 brouard 13384: Fixed[k]= 1;
13385: Dummy[k]= 1;
13386: nqtveff++;
13387: modell[k].maintype= VTYPE;
13388: modell[k].subtype= VQ;
13389: ncovv++; /* Only simple time varying variables */
13390: nsq++;
1.334 brouard 13391: 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) */
13392: 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 13393: TvarV[ncovv]=Tvar[k];
1.242 brouard 13394: 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 13395: 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 */
13396: 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 13397: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
13398: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
1.349 brouard 13399: /* 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 13400: /* printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); */
1.227 brouard 13401: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 13402: ncova++;
13403: TvarA[ncova]=Tvar[k];
13404: TvarAind[ncova]=k;
1.349 brouard 13405: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13406: /** 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 13407: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 13408: Fixed[k]= 2;
13409: Dummy[k]= 2;
13410: modell[k].maintype= ATYPE;
13411: modell[k].subtype= APFD;
1.349 brouard 13412: ncovta++;
13413: TvarAVVA[ncovta]=Tvar[k]; /* (2)age*V3 */
13414: TvarAVVAind[ncovta]=k;
1.240 brouard 13415: /* ncoveff++; */
1.227 brouard 13416: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 13417: Fixed[k]= 2;
13418: Dummy[k]= 3;
13419: modell[k].maintype= ATYPE;
13420: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
1.349 brouard 13421: ncovta++;
13422: TvarAVVA[ncovta]=Tvar[k]; /* */
13423: TvarAVVAind[ncovta]=k;
1.240 brouard 13424: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 13425: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 13426: Fixed[k]= 3;
13427: Dummy[k]= 2;
13428: modell[k].maintype= ATYPE;
13429: modell[k].subtype= APVD; /* Product age * varying dummy */
1.349 brouard 13430: ncovva++;
13431: TvarVVA[ncovva]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13432: TvarVVAind[ncovva]=k;
13433: ncovta++;
13434: TvarAVVA[ncovta]=Tvar[k]; /* */
13435: TvarAVVAind[ncovta]=k;
1.240 brouard 13436: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 13437: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 13438: Fixed[k]= 3;
13439: Dummy[k]= 3;
13440: modell[k].maintype= ATYPE;
13441: modell[k].subtype= APVQ; /* Product age * varying quantitative */
1.349 brouard 13442: ncovva++;
13443: TvarVVA[ncovva]=Tvar[k]; /* */
13444: TvarVVAind[ncovva]=k;
13445: ncovta++;
13446: TvarAVVA[ncovta]=Tvar[k]; /* (1)+age*V6 + (2)age*V7 */
13447: TvarAVVAind[ncovta]=k;
1.240 brouard 13448: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 13449: }
1.349 brouard 13450: }else if( Tposprod[k]>0 && Typevar[k]==2){ /* Detects if fixed product no age Vm*Vn */
13451: printf("MEMORY ERRORR k=%d Tposprod[k]=%d, Typevar[k]=%d\n ",k, Tposprod[k], Typevar[k]);
13452: 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 */
13453: 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]]);
13454: Fixed[k]= 0;
13455: Dummy[k]= 0;
13456: ncoveff++;
13457: ncovf++;
13458: /* ncovv++; */
13459: /* TvarVV[ncovv]=Tvardk[k][1]; */
13460: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13461: /* ncovv++; */
13462: /* TvarVV[ncovv]=Tvardk[k][2]; */
13463: /* FixedV[ncovcolt+ncovv]=0; /\* or FixedV[TvarVV[ncovv]]=0 HERE *\/ */
13464: modell[k].maintype= FTYPE;
13465: TvarF[ncovf]=Tvar[k];
13466: /* TnsdVar[Tvar[k]]=nsd; */ /* To be done */
13467: TvarFind[ncovf]=k;
13468: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13469: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
13470: }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 */
13471: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
13472: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13473: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13474: 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 */
13475: ncovvt++;
13476: TvarVV[ncovvt]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13477: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13478: ncovvt++;
13479: TvarVV[ncovvt]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13480: TvarVVind[ncovvt]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13481:
13482: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13483: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13484:
13485: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13486: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
13487: Fixed[k]= 1;
13488: Dummy[k]= 0;
13489: modell[k].maintype= FTYPE;
13490: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
13491: ncovf++; /* Fixed variables without age */
13492: TvarF[ncovf]=Tvar[k];
13493: TvarFind[ncovf]=k;
13494: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
13495: Fixed[k]= 0; /* Fixed product */
13496: Dummy[k]= 1;
13497: modell[k].maintype= FTYPE;
13498: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
13499: ncovf++; /* Varying variables without age */
13500: TvarF[ncovf]=Tvar[k];
13501: TvarFind[ncovf]=k;
13502: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
13503: Fixed[k]= 1;
13504: Dummy[k]= 0;
13505: modell[k].maintype= VTYPE;
13506: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
13507: ncovv++; /* Varying variables without age */
13508: TvarV[ncovv]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13509: TvarVind[ncovv]=k;/* TvarVind[1]=5 */
13510: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
13511: Fixed[k]= 1;
13512: Dummy[k]= 1;
13513: modell[k].maintype= VTYPE;
13514: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
13515: ncovv++; /* Varying variables without age */
13516: TvarV[ncovv]=Tvar[k];
13517: TvarVind[ncovv]=k;
13518: }
13519: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13520: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
13521: Fixed[k]= 0; /* Fixed product */
13522: Dummy[k]= 1;
13523: modell[k].maintype= FTYPE;
13524: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
13525: ncovf++; /* Fixed variables without age */
13526: TvarF[ncovf]=Tvar[k];
13527: TvarFind[ncovf]=k;
13528: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
13529: Fixed[k]= 1;
13530: Dummy[k]= 1;
13531: modell[k].maintype= VTYPE;
13532: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
13533: ncovv++; /* Varying variables without age */
13534: TvarV[ncovv]=Tvar[k];
13535: TvarVind[ncovv]=k;
13536: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
13537: Fixed[k]= 1;
13538: Dummy[k]= 1;
13539: modell[k].maintype= VTYPE;
13540: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
13541: ncovv++; /* Varying variables without age */
13542: TvarV[ncovv]=Tvar[k];
13543: TvarVind[ncovv]=k;
13544: ncovv++; /* Varying variables without age */
13545: TvarV[ncovv]=Tvar[k];
13546: TvarVind[ncovv]=k;
13547: }
13548: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
13549: if(Tvard[k1][2] <=ncovcol){
13550: Fixed[k]= 1;
13551: Dummy[k]= 1;
13552: modell[k].maintype= VTYPE;
13553: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
13554: ncovv++; /* Varying variables without age */
13555: TvarV[ncovv]=Tvar[k];
13556: TvarVind[ncovv]=k;
13557: }else if(Tvard[k1][2] <=ncovcol+nqv){
13558: Fixed[k]= 1;
13559: Dummy[k]= 1;
13560: modell[k].maintype= VTYPE;
13561: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
13562: ncovv++; /* Varying variables without age */
13563: TvarV[ncovv]=Tvar[k];
13564: TvarVind[ncovv]=k;
13565: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13566: Fixed[k]= 1;
13567: Dummy[k]= 0;
13568: modell[k].maintype= VTYPE;
13569: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
13570: ncovv++; /* Varying variables without age */
13571: TvarV[ncovv]=Tvar[k];
13572: TvarVind[ncovv]=k;
13573: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13574: Fixed[k]= 1;
13575: Dummy[k]= 1;
13576: modell[k].maintype= VTYPE;
13577: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
13578: ncovv++; /* Varying variables without age */
13579: TvarV[ncovv]=Tvar[k];
13580: TvarVind[ncovv]=k;
13581: }
13582: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
13583: if(Tvard[k1][2] <=ncovcol){
13584: Fixed[k]= 1;
13585: Dummy[k]= 1;
13586: modell[k].maintype= VTYPE;
13587: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
13588: ncovv++; /* Varying variables without age */
13589: TvarV[ncovv]=Tvar[k];
13590: TvarVind[ncovv]=k;
13591: }else if(Tvard[k1][2] <=ncovcol+nqv){
13592: Fixed[k]= 1;
13593: Dummy[k]= 1;
13594: modell[k].maintype= VTYPE;
13595: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
13596: ncovv++; /* Varying variables without age */
13597: TvarV[ncovv]=Tvar[k];
13598: TvarVind[ncovv]=k;
13599: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
13600: Fixed[k]= 1;
13601: Dummy[k]= 1;
13602: modell[k].maintype= VTYPE;
13603: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
13604: ncovv++; /* Varying variables without age */
13605: TvarV[ncovv]=Tvar[k];
13606: TvarVind[ncovv]=k;
13607: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
13608: Fixed[k]= 1;
13609: Dummy[k]= 1;
13610: modell[k].maintype= VTYPE;
13611: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
13612: ncovv++; /* Varying variables without age */
13613: TvarV[ncovv]=Tvar[k];
13614: TvarVind[ncovv]=k;
13615: }
13616: }else{
13617: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13618: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13619: } /*end k1*/
13620: }
13621: }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 13622: /*# ID V1 V2 weight birth death 1st s1 V3 V4 V5 2nd s2 */
1.349 brouard 13623: /* model V1+V3+age*V1+age*V3+V1*V3 + V1*V3*age*/
13624: /* Tvar={1, 3, 1, 3, 6, 6}, the 6 comes from the fact that there are already V1, V2, V3, V4, V5 native covariates */
13625: 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 */
13626: ncova++;
13627: TvarA[ncova]=Tvard[k1][1]; /* TvarVV[2]=V1 (because TvarVV[1] was V3, first time varying covariates */
13628: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
13629: ncova++;
13630: TvarA[ncova]=Tvard[k1][2]; /* TvarVV[3]=V3 */
13631: TvarAind[ncova]=k; /* TvarVVind[2]=5 (because TvarVVind[2] was V1*V3 at position 5 */
1.339 brouard 13632:
1.349 brouard 13633: /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
13634: /** Dummy[k] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
13635: if( FixedV[Tvardk[k][1]] == 0 && FixedV[Tvardk[k][2]] == 0){
13636: ncovta++;
13637: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13638: TvarAVVAind[ncovta]=k;
13639: ncovta++;
13640: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13641: TvarAVVAind[ncovta]=k;
13642: }else{
13643: ncovva++; /* HERY reached */
13644: TvarVVA[ncovva]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13645: TvarVVAind[ncovva]=k;
13646: ncovva++;
13647: TvarVVA[ncovva]=Tvard[k1][2]; /* */
13648: TvarVVAind[ncovva]=k;
13649: ncovta++;
13650: TvarAVVA[ncovta]=Tvard[k1][1]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13651: TvarAVVAind[ncovta]=k;
13652: ncovta++;
13653: TvarAVVA[ncovta]=Tvard[k1][2]; /* age*V6*V3 +age*V7*V3 + age*V6*V4 +age*V7*V4 */
13654: TvarAVVAind[ncovta]=k;
13655: }
1.339 brouard 13656: if(Tvard[k1][1] <=ncovcol){ /* Vn is dummy fixed, (Tvard[1][1]=V1), (Tvard[1][1]=V3 time varying) */
13657: if(Tvard[k1][2] <=ncovcol){ /* Vm is dummy fixed */
1.349 brouard 13658: Fixed[k]= 2;
13659: Dummy[k]= 2;
1.240 brouard 13660: modell[k].maintype= FTYPE;
13661: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
1.349 brouard 13662: /* TvarF[ncova]=Tvar[k]; /\* Problem to solve *\/ */
13663: /* TvarFind[ncova]=k; */
1.339 brouard 13664: }else if(Tvard[k1][2] <=ncovcol+nqv){ /* Vm is quanti fixed */
1.349 brouard 13665: Fixed[k]= 2; /* Fixed product */
13666: Dummy[k]= 3;
1.240 brouard 13667: modell[k].maintype= FTYPE;
13668: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
1.349 brouard 13669: /* TvarF[ncova]=Tvar[k]; */
13670: /* TvarFind[ncova]=k; */
1.339 brouard 13671: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is a time varying dummy covariate */
1.349 brouard 13672: Fixed[k]= 3;
13673: Dummy[k]= 2;
1.240 brouard 13674: modell[k].maintype= VTYPE;
13675: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
1.349 brouard 13676: TvarV[ncova]=Tvar[k]; /* TvarV[1]=Tvar[5]=5 because there is a V4 */
13677: TvarVind[ncova]=k;/* TvarVind[1]=5 */
1.339 brouard 13678: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is a time varying quantitative covariate */
1.349 brouard 13679: Fixed[k]= 3;
13680: Dummy[k]= 3;
1.240 brouard 13681: modell[k].maintype= VTYPE;
13682: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
1.349 brouard 13683: /* ncovv++; /\* Varying variables without age *\/ */
13684: /* TvarV[ncovv]=Tvar[k]; */
13685: /* TvarVind[ncovv]=k; */
1.240 brouard 13686: }
1.339 brouard 13687: }else if(Tvard[k1][1] <=ncovcol+nqv){ /* Vn is fixed quanti */
13688: if(Tvard[k1][2] <=ncovcol){ /* Vm is fixed dummy */
1.349 brouard 13689: Fixed[k]= 2; /* Fixed product */
13690: Dummy[k]= 2;
1.240 brouard 13691: modell[k].maintype= FTYPE;
13692: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
1.349 brouard 13693: /* ncova++; /\* Fixed variables with age *\/ */
13694: /* TvarF[ncovf]=Tvar[k]; */
13695: /* TvarFind[ncovf]=k; */
1.339 brouard 13696: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ /* Vm is time varying */
1.349 brouard 13697: Fixed[k]= 2;
13698: Dummy[k]= 3;
1.240 brouard 13699: modell[k].maintype= VTYPE;
13700: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
1.349 brouard 13701: /* ncova++; /\* Varying variables with age *\/ */
13702: /* TvarV[ncova]=Tvar[k]; */
13703: /* TvarVind[ncova]=k; */
1.339 brouard 13704: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ /* Vm is time varying quanti */
1.349 brouard 13705: Fixed[k]= 3;
13706: Dummy[k]= 2;
1.240 brouard 13707: modell[k].maintype= VTYPE;
13708: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
1.349 brouard 13709: ncova++; /* Varying variables without age */
13710: TvarV[ncova]=Tvar[k];
13711: TvarVind[ncova]=k;
13712: /* ncova++; /\* Varying variables without age *\/ */
13713: /* TvarV[ncova]=Tvar[k]; */
13714: /* TvarVind[ncova]=k; */
1.240 brouard 13715: }
1.339 brouard 13716: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ /* Vn is time varying dummy */
1.240 brouard 13717: if(Tvard[k1][2] <=ncovcol){
1.349 brouard 13718: Fixed[k]= 2;
13719: Dummy[k]= 2;
1.240 brouard 13720: modell[k].maintype= VTYPE;
13721: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
1.349 brouard 13722: /* ncova++; /\* Varying variables with age *\/ */
13723: /* TvarV[ncova]=Tvar[k]; */
13724: /* TvarVind[ncova]=k; */
1.240 brouard 13725: }else if(Tvard[k1][2] <=ncovcol+nqv){
1.349 brouard 13726: Fixed[k]= 2;
13727: Dummy[k]= 3;
1.240 brouard 13728: modell[k].maintype= VTYPE;
13729: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
1.349 brouard 13730: /* ncova++; /\* Varying variables with age *\/ */
13731: /* TvarV[ncova]=Tvar[k]; */
13732: /* TvarVind[ncova]=k; */
1.240 brouard 13733: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
1.349 brouard 13734: Fixed[k]= 3;
13735: Dummy[k]= 2;
1.240 brouard 13736: modell[k].maintype= VTYPE;
13737: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
1.349 brouard 13738: /* ncova++; /\* Varying variables with age *\/ */
13739: /* TvarV[ncova]=Tvar[k]; */
13740: /* TvarVind[ncova]=k; */
1.240 brouard 13741: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
1.349 brouard 13742: Fixed[k]= 3;
13743: Dummy[k]= 3;
1.240 brouard 13744: modell[k].maintype= VTYPE;
13745: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
1.349 brouard 13746: /* ncova++; /\* Varying variables with age *\/ */
13747: /* TvarV[ncova]=Tvar[k]; */
13748: /* TvarVind[ncova]=k; */
1.240 brouard 13749: }
1.339 brouard 13750: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ /* Vn is time varying quanti */
1.240 brouard 13751: if(Tvard[k1][2] <=ncovcol){
1.349 brouard 13752: Fixed[k]= 2;
13753: Dummy[k]= 2;
1.240 brouard 13754: modell[k].maintype= VTYPE;
13755: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
1.349 brouard 13756: /* ncova++; /\* Varying variables with age *\/ */
13757: /* TvarV[ncova]=Tvar[k]; */
13758: /* TvarVind[ncova]=k; */
1.240 brouard 13759: }else if(Tvard[k1][2] <=ncovcol+nqv){
1.349 brouard 13760: Fixed[k]= 2;
13761: Dummy[k]= 3;
1.240 brouard 13762: modell[k].maintype= VTYPE;
13763: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
1.349 brouard 13764: /* ncova++; /\* Varying variables with age *\/ */
13765: /* TvarV[ncova]=Tvar[k]; */
13766: /* TvarVind[ncova]=k; */
1.240 brouard 13767: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
1.349 brouard 13768: Fixed[k]= 3;
13769: Dummy[k]= 2;
1.240 brouard 13770: modell[k].maintype= VTYPE;
13771: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
1.349 brouard 13772: /* ncova++; /\* Varying variables with age *\/ */
13773: /* TvarV[ncova]=Tvar[k]; */
13774: /* TvarVind[ncova]=k; */
1.240 brouard 13775: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
1.349 brouard 13776: Fixed[k]= 3;
13777: Dummy[k]= 3;
1.240 brouard 13778: modell[k].maintype= VTYPE;
13779: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
1.349 brouard 13780: /* ncova++; /\* Varying variables with age *\/ */
13781: /* TvarV[ncova]=Tvar[k]; */
13782: /* TvarVind[ncova]=k; */
1.240 brouard 13783: }
1.227 brouard 13784: }else{
1.240 brouard 13785: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13786: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
13787: } /*end k1*/
1.349 brouard 13788: } else{
1.226 brouard 13789: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
13790: 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 13791: }
1.342 brouard 13792: /* 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]); */
13793: /* printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); */
1.227 brouard 13794: 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]);
13795: }
1.349 brouard 13796: ncovvta=ncovva;
1.227 brouard 13797: /* Searching for doublons in the model */
13798: for(k1=1; k1<= cptcovt;k1++){
13799: for(k2=1; k2 <k1;k2++){
1.285 brouard 13800: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
13801: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
1.234 brouard 13802: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
13803: if(Tvar[k1]==Tvar[k2]){
1.338 brouard 13804: 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]);
13805: 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 13806: return(1);
13807: }
13808: }else if (Typevar[k1] ==2){
13809: k3=Tposprod[k1];
13810: k4=Tposprod[k2];
13811: 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 13812: 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]]);
13813: 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 13814: return(1);
13815: }
13816: }
1.227 brouard 13817: }
13818: }
1.225 brouard 13819: }
13820: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
13821: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 13822: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
13823: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.349 brouard 13824:
13825: free_imatrix(existcomb,1,NCOVMAX,1,NCOVMAX);
1.137 brouard 13826: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 13827: /*endread:*/
1.225 brouard 13828: printf("Exiting decodemodel: ");
13829: return (1);
1.136 brouard 13830: }
13831:
1.169 brouard 13832: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.248 brouard 13833: {/* Check ages at death */
1.136 brouard 13834: int i, m;
1.218 brouard 13835: int firstone=0;
13836:
1.136 brouard 13837: for (i=1; i<=imx; i++) {
13838: for(m=2; (m<= maxwav); m++) {
13839: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
13840: anint[m][i]=9999;
1.216 brouard 13841: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
13842: s[m][i]=-1;
1.136 brouard 13843: }
13844: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.260 brouard 13845: *nberr = *nberr + 1;
1.218 brouard 13846: if(firstone == 0){
13847: firstone=1;
1.260 brouard 13848: 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 13849: }
1.262 brouard 13850: 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 13851: s[m][i]=-1; /* Droping the death status */
1.136 brouard 13852: }
13853: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 13854: (*nberr)++;
1.259 brouard 13855: 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 13856: 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 13857: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.136 brouard 13858: }
13859: }
13860: }
13861:
13862: for (i=1; i<=imx; i++) {
13863: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
13864: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 13865: 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 13866: if (s[m][i] >= nlstate+1) {
1.169 brouard 13867: if(agedc[i]>0){
13868: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 13869: agev[m][i]=agedc[i];
1.214 brouard 13870: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 13871: }else {
1.136 brouard 13872: if ((int)andc[i]!=9999){
13873: nbwarn++;
13874: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
13875: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
13876: agev[m][i]=-1;
13877: }
13878: }
1.169 brouard 13879: } /* agedc > 0 */
1.214 brouard 13880: } /* end if */
1.136 brouard 13881: else if(s[m][i] !=9){ /* Standard case, age in fractional
13882: years but with the precision of a month */
13883: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
13884: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
13885: agev[m][i]=1;
13886: else if(agev[m][i] < *agemin){
13887: *agemin=agev[m][i];
13888: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
13889: }
13890: else if(agev[m][i] >*agemax){
13891: *agemax=agev[m][i];
1.156 brouard 13892: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 13893: }
13894: /*agev[m][i]=anint[m][i]-annais[i];*/
13895: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 13896: } /* en if 9*/
1.136 brouard 13897: else { /* =9 */
1.214 brouard 13898: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 13899: agev[m][i]=1;
13900: s[m][i]=-1;
13901: }
13902: }
1.214 brouard 13903: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 13904: agev[m][i]=1;
1.214 brouard 13905: else{
13906: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
13907: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
13908: agev[m][i]=0;
13909: }
13910: } /* End for lastpass */
13911: }
1.136 brouard 13912:
13913: for (i=1; i<=imx; i++) {
13914: for(m=firstpass; (m<=lastpass); m++){
13915: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 13916: (*nberr)++;
1.136 brouard 13917: 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);
13918: 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);
13919: return 1;
13920: }
13921: }
13922: }
13923:
13924: /*for (i=1; i<=imx; i++){
13925: for (m=firstpass; (m<lastpass); m++){
13926: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
13927: }
13928:
13929: }*/
13930:
13931:
1.139 brouard 13932: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
13933: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 13934:
13935: return (0);
1.164 brouard 13936: /* endread:*/
1.136 brouard 13937: printf("Exiting calandcheckages: ");
13938: return (1);
13939: }
13940:
1.172 brouard 13941: #if defined(_MSC_VER)
13942: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
13943: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
13944: //#include "stdafx.h"
13945: //#include <stdio.h>
13946: //#include <tchar.h>
13947: //#include <windows.h>
13948: //#include <iostream>
13949: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
13950:
13951: LPFN_ISWOW64PROCESS fnIsWow64Process;
13952:
13953: BOOL IsWow64()
13954: {
13955: BOOL bIsWow64 = FALSE;
13956:
13957: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
13958: // (HANDLE, PBOOL);
13959:
13960: //LPFN_ISWOW64PROCESS fnIsWow64Process;
13961:
13962: HMODULE module = GetModuleHandle(_T("kernel32"));
13963: const char funcName[] = "IsWow64Process";
13964: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
13965: GetProcAddress(module, funcName);
13966:
13967: if (NULL != fnIsWow64Process)
13968: {
13969: if (!fnIsWow64Process(GetCurrentProcess(),
13970: &bIsWow64))
13971: //throw std::exception("Unknown error");
13972: printf("Unknown error\n");
13973: }
13974: return bIsWow64 != FALSE;
13975: }
13976: #endif
1.177 brouard 13977:
1.191 brouard 13978: void syscompilerinfo(int logged)
1.292 brouard 13979: {
13980: #include <stdint.h>
13981:
13982: /* #include "syscompilerinfo.h"*/
1.185 brouard 13983: /* command line Intel compiler 32bit windows, XP compatible:*/
13984: /* /GS /W3 /Gy
13985: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
13986: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
13987: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 13988: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
13989: */
13990: /* 64 bits */
1.185 brouard 13991: /*
13992: /GS /W3 /Gy
13993: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
13994: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
13995: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
13996: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
13997: /* Optimization are useless and O3 is slower than O2 */
13998: /*
13999: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
14000: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
14001: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
14002: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
14003: */
1.186 brouard 14004: /* Link is */ /* /OUT:"visual studio
1.185 brouard 14005: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
14006: /PDB:"visual studio
14007: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
14008: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
14009: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
14010: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
14011: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
14012: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
14013: uiAccess='false'"
14014: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
14015: /NOLOGO /TLBID:1
14016: */
1.292 brouard 14017:
14018:
1.177 brouard 14019: #if defined __INTEL_COMPILER
1.178 brouard 14020: #if defined(__GNUC__)
14021: struct utsname sysInfo; /* For Intel on Linux and OS/X */
14022: #endif
1.177 brouard 14023: #elif defined(__GNUC__)
1.179 brouard 14024: #ifndef __APPLE__
1.174 brouard 14025: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 14026: #endif
1.177 brouard 14027: struct utsname sysInfo;
1.178 brouard 14028: int cross = CROSS;
14029: if (cross){
14030: printf("Cross-");
1.191 brouard 14031: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 14032: }
1.174 brouard 14033: #endif
14034:
1.191 brouard 14035: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 14036: #if defined(__clang__)
1.191 brouard 14037: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 14038: #endif
14039: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 14040: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 14041: #endif
14042: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 14043: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 14044: #endif
14045: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 14046: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 14047: #endif
14048: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 14049: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 14050: #endif
14051: #if defined(_MSC_VER)
1.191 brouard 14052: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 14053: #endif
14054: #if defined(__PGI)
1.191 brouard 14055: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 14056: #endif
14057: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 14058: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 14059: #endif
1.191 brouard 14060: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 14061:
1.167 brouard 14062: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
14063: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
14064: // Windows (x64 and x86)
1.191 brouard 14065: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 14066: #elif __unix__ // all unices, not all compilers
14067: // Unix
1.191 brouard 14068: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 14069: #elif __linux__
14070: // linux
1.191 brouard 14071: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 14072: #elif __APPLE__
1.174 brouard 14073: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 14074: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 14075: #endif
14076:
14077: /* __MINGW32__ */
14078: /* __CYGWIN__ */
14079: /* __MINGW64__ */
14080: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
14081: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
14082: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
14083: /* _WIN64 // Defined for applications for Win64. */
14084: /* _M_X64 // Defined for compilations that target x64 processors. */
14085: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 14086:
1.167 brouard 14087: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 14088: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 14089: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 14090: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 14091: #else
1.191 brouard 14092: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 14093: #endif
14094:
1.169 brouard 14095: #if defined(__GNUC__)
14096: # if defined(__GNUC_PATCHLEVEL__)
14097: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14098: + __GNUC_MINOR__ * 100 \
14099: + __GNUC_PATCHLEVEL__)
14100: # else
14101: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
14102: + __GNUC_MINOR__ * 100)
14103: # endif
1.174 brouard 14104: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 14105: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 14106:
14107: if (uname(&sysInfo) != -1) {
14108: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 14109: 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 14110: }
14111: else
14112: perror("uname() error");
1.179 brouard 14113: //#ifndef __INTEL_COMPILER
14114: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 14115: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 14116: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 14117: #endif
1.169 brouard 14118: #endif
1.172 brouard 14119:
1.286 brouard 14120: // void main ()
1.172 brouard 14121: // {
1.169 brouard 14122: #if defined(_MSC_VER)
1.174 brouard 14123: if (IsWow64()){
1.191 brouard 14124: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
14125: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 14126: }
14127: else{
1.191 brouard 14128: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
14129: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 14130: }
1.172 brouard 14131: // printf("\nPress Enter to continue...");
14132: // getchar();
14133: // }
14134:
1.169 brouard 14135: #endif
14136:
1.167 brouard 14137:
1.219 brouard 14138: }
1.136 brouard 14139:
1.219 brouard 14140: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.288 brouard 14141: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
1.332 brouard 14142: /* Computes the prevalence limit for each combination of the dummy covariates */
1.235 brouard 14143: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 14144: /* double ftolpl = 1.e-10; */
1.180 brouard 14145: double age, agebase, agelim;
1.203 brouard 14146: double tot;
1.180 brouard 14147:
1.202 brouard 14148: strcpy(filerespl,"PL_");
14149: strcat(filerespl,fileresu);
14150: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.288 brouard 14151: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
14152: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
1.202 brouard 14153: }
1.288 brouard 14154: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
14155: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 14156: pstamp(ficrespl);
1.288 brouard 14157: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 14158: fprintf(ficrespl,"#Age ");
14159: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
14160: fprintf(ficrespl,"\n");
1.180 brouard 14161:
1.219 brouard 14162: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 14163:
1.219 brouard 14164: agebase=ageminpar;
14165: agelim=agemaxpar;
1.180 brouard 14166:
1.227 brouard 14167: /* i1=pow(2,ncoveff); */
1.234 brouard 14168: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 14169: if (cptcovn < 1){i1=1;}
1.180 brouard 14170:
1.337 brouard 14171: /* for(k=1; k<=i1;k++){ /\* For each combination k of dummy covariates in the model *\/ */
1.238 brouard 14172: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 14173: k=TKresult[nres];
1.338 brouard 14174: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14175: /* if(i1 != 1 && TKresult[nres]!= k) /\* We found the combination k corresponding to the resultline value of dummies *\/ */
14176: /* continue; */
1.235 brouard 14177:
1.238 brouard 14178: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14179: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
14180: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
14181: /* k=k+1; */
14182: /* to clean */
1.332 brouard 14183: /*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*/
1.238 brouard 14184: fprintf(ficrespl,"#******");
14185: printf("#******");
14186: fprintf(ficlog,"#******");
1.337 brouard 14187: 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 14188: /* fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,Tvaraff[j])]); /\* Here problem for varying dummy*\/ */
1.337 brouard 14189: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14190: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14191: fprintf(ficrespl," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14192: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14193: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14194: }
14195: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14196: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14197: /* fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14198: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14199: /* } */
1.238 brouard 14200: fprintf(ficrespl,"******\n");
14201: printf("******\n");
14202: fprintf(ficlog,"******\n");
14203: if(invalidvarcomb[k]){
14204: printf("\nCombination (%d) ignored because no case \n",k);
14205: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
14206: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
14207: continue;
14208: }
1.219 brouard 14209:
1.238 brouard 14210: fprintf(ficrespl,"#Age ");
1.337 brouard 14211: /* for(j=1;j<=cptcoveff;j++) { */
14212: /* fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14213: /* } */
14214: for(j=1;j<=cptcovs;j++) { /* New the quanti variable is added */
14215: fprintf(ficrespl,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14216: }
14217: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
14218: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 14219:
1.238 brouard 14220: for (age=agebase; age<=agelim; age++){
14221: /* for (age=agebase; age<=agebase; age++){ */
1.337 brouard 14222: /**< Computes the prevalence limit in each live state at age x and for covariate combination (k and) nres */
14223: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); /* Nicely done */
1.238 brouard 14224: fprintf(ficrespl,"%.0f ",age );
1.337 brouard 14225: /* for(j=1;j<=cptcoveff;j++) */
14226: /* fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14227: for(j=1;j<=cptcovs;j++)
14228: fprintf(ficrespl,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14229: tot=0.;
14230: for(i=1; i<=nlstate;i++){
14231: tot += prlim[i][i];
14232: fprintf(ficrespl," %.5f", prlim[i][i]);
14233: }
14234: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
14235: } /* Age */
14236: /* was end of cptcod */
1.337 brouard 14237: } /* nres */
14238: /* } /\* for each combination *\/ */
1.219 brouard 14239: return 0;
1.180 brouard 14240: }
14241:
1.218 brouard 14242: 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 14243: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
1.218 brouard 14244:
14245: /* Computes the back prevalence limit for any combination of covariate values
14246: * at any age between ageminpar and agemaxpar
14247: */
1.235 brouard 14248: int i, j, k, i1, nres=0 ;
1.217 brouard 14249: /* double ftolpl = 1.e-10; */
14250: double age, agebase, agelim;
14251: double tot;
1.218 brouard 14252: /* double ***mobaverage; */
14253: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 14254:
14255: strcpy(fileresplb,"PLB_");
14256: strcat(fileresplb,fileresu);
14257: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
1.288 brouard 14258: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
14259: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
1.217 brouard 14260: }
1.288 brouard 14261: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
14262: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
1.217 brouard 14263: pstamp(ficresplb);
1.288 brouard 14264: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.217 brouard 14265: fprintf(ficresplb,"#Age ");
14266: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
14267: fprintf(ficresplb,"\n");
14268:
1.218 brouard 14269:
14270: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
14271:
14272: agebase=ageminpar;
14273: agelim=agemaxpar;
14274:
14275:
1.227 brouard 14276: i1=pow(2,cptcoveff);
1.218 brouard 14277: if (cptcovn < 1){i1=1;}
1.227 brouard 14278:
1.238 brouard 14279: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.338 brouard 14280: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14281: k=TKresult[nres];
14282: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
14283: /* if(i1 != 1 && TKresult[nres]!= k) */
14284: /* continue; */
14285: /* /\*printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));*\/ */
1.238 brouard 14286: fprintf(ficresplb,"#******");
14287: printf("#******");
14288: fprintf(ficlog,"#******");
1.338 brouard 14289: 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) */
14290: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14291: fprintf(ficresplb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14292: fprintf(ficlog," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14293: }
1.338 brouard 14294: /* for(j=1;j<=cptcoveff ;j++) {/\* all covariates *\/ */
14295: /* fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14296: /* printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14297: /* fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14298: /* } */
14299: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14300: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14301: /* fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14302: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14303: /* } */
1.238 brouard 14304: fprintf(ficresplb,"******\n");
14305: printf("******\n");
14306: fprintf(ficlog,"******\n");
14307: if(invalidvarcomb[k]){
14308: printf("\nCombination (%d) ignored because no cases \n",k);
14309: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
14310: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
14311: continue;
14312: }
1.218 brouard 14313:
1.238 brouard 14314: fprintf(ficresplb,"#Age ");
1.338 brouard 14315: for(j=1;j<=cptcovs;j++) {
14316: fprintf(ficresplb,"V%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14317: }
14318: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
14319: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 14320:
14321:
1.238 brouard 14322: for (age=agebase; age<=agelim; age++){
14323: /* for (age=agebase; age<=agebase; age++){ */
14324: if(mobilavproj > 0){
14325: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
14326: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 14327: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 14328: }else if (mobilavproj == 0){
14329: 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);
14330: 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);
14331: exit(1);
14332: }else{
14333: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 14334: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.266 brouard 14335: /* printf("TOTOT\n"); */
14336: /* exit(1); */
1.238 brouard 14337: }
14338: fprintf(ficresplb,"%.0f ",age );
1.338 brouard 14339: for(j=1;j<=cptcovs;j++)
14340: fprintf(ficresplb,"%d %lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.238 brouard 14341: tot=0.;
14342: for(i=1; i<=nlstate;i++){
14343: tot += bprlim[i][i];
14344: fprintf(ficresplb," %.5f", bprlim[i][i]);
14345: }
14346: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
14347: } /* Age */
14348: /* was end of cptcod */
1.255 brouard 14349: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
1.338 brouard 14350: /* } /\* end of any combination *\/ */
1.238 brouard 14351: } /* end of nres */
1.218 brouard 14352: /* hBijx(p, bage, fage); */
14353: /* fclose(ficrespijb); */
14354:
14355: return 0;
1.217 brouard 14356: }
1.218 brouard 14357:
1.180 brouard 14358: int hPijx(double *p, int bage, int fage){
14359: /*------------- h Pij x at various ages ------------*/
1.336 brouard 14360: /* to be optimized with precov */
1.180 brouard 14361: int stepsize;
14362: int agelim;
14363: int hstepm;
14364: int nhstepm;
1.359 ! brouard 14365: int h, i, i1, j, k, nres=0;
1.180 brouard 14366:
14367: double agedeb;
14368: double ***p3mat;
14369:
1.337 brouard 14370: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
14371: if((ficrespij=fopen(filerespij,"w"))==NULL) {
14372: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
14373: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
14374: }
14375: printf("Computing pij: result on file '%s' \n", filerespij);
14376: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
14377:
14378: stepsize=(int) (stepm+YEARM-1)/YEARM;
14379: /*if (stepm<=24) stepsize=2;*/
14380:
14381: agelim=AGESUP;
14382: hstepm=stepsize*YEARM; /* Every year of age */
14383: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14384:
14385: /* hstepm=1; aff par mois*/
14386: pstamp(ficrespij);
14387: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
14388: i1= pow(2,cptcoveff);
14389: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14390: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14391: /* k=k+1; */
14392: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
14393: k=TKresult[nres];
1.338 brouard 14394: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14395: /* for(k=1; k<=i1;k++){ */
14396: /* if(i1 != 1 && TKresult[nres]!= k) */
14397: /* continue; */
14398: fprintf(ficrespij,"\n#****** ");
14399: for(j=1;j<=cptcovs;j++){
14400: fprintf(ficrespij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
14401: /* fprintf(ficrespij,"@wV%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14402: /* for (k4=1; k4<= nsq; k4++){ /\* For each selected (single) quantitative value *\/ */
14403: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14404: /* fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
14405: }
14406: fprintf(ficrespij,"******\n");
14407:
14408: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
14409: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
14410: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
14411:
14412: /* nhstepm=nhstepm*YEARM; aff par mois*/
14413:
14414: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14415: oldm=oldms;savm=savms;
14416: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
14417: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
14418: for(i=1; i<=nlstate;i++)
14419: for(j=1; j<=nlstate+ndeath;j++)
14420: fprintf(ficrespij," %1d-%1d",i,j);
14421: fprintf(ficrespij,"\n");
14422: for (h=0; h<=nhstepm; h++){
14423: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14424: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.183 brouard 14425: for(i=1; i<=nlstate;i++)
14426: for(j=1; j<=nlstate+ndeath;j++)
1.337 brouard 14427: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.183 brouard 14428: fprintf(ficrespij,"\n");
14429: }
1.337 brouard 14430: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14431: fprintf(ficrespij,"\n");
1.180 brouard 14432: }
1.337 brouard 14433: }
14434: /*}*/
14435: return 0;
1.180 brouard 14436: }
1.218 brouard 14437:
14438: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 14439: /*------------- h Bij x at various ages ------------*/
1.336 brouard 14440: /* To be optimized with precov */
1.217 brouard 14441: int stepsize;
1.218 brouard 14442: /* int agelim; */
14443: int ageminl;
1.217 brouard 14444: int hstepm;
14445: int nhstepm;
1.238 brouard 14446: int h, i, i1, j, k, nres;
1.218 brouard 14447:
1.217 brouard 14448: double agedeb;
14449: double ***p3mat;
1.218 brouard 14450:
14451: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
14452: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
14453: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14454: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
14455: }
14456: printf("Computing pij back: result on file '%s' \n", filerespijb);
14457: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
14458:
14459: stepsize=(int) (stepm+YEARM-1)/YEARM;
14460: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 14461:
1.218 brouard 14462: /* agelim=AGESUP; */
1.289 brouard 14463: ageminl=AGEINF; /* was 30 */
1.218 brouard 14464: hstepm=stepsize*YEARM; /* Every year of age */
14465: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
14466:
14467: /* hstepm=1; aff par mois*/
14468: pstamp(ficrespijb);
1.255 brouard 14469: 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 14470: i1= pow(2,cptcoveff);
1.218 brouard 14471: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
14472: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
14473: /* k=k+1; */
1.238 brouard 14474: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.337 brouard 14475: k=TKresult[nres];
1.338 brouard 14476: if(TKresult[nres]==0) k=1; /* To be checked for noresult */
1.337 brouard 14477: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
14478: /* if(i1 != 1 && TKresult[nres]!= k) */
14479: /* continue; */
14480: fprintf(ficrespijb,"\n#****** ");
14481: for(j=1;j<=cptcovs;j++){
1.338 brouard 14482: fprintf(ficrespijb," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
1.337 brouard 14483: /* for(j=1;j<=cptcoveff;j++) */
14484: /* fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
14485: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
14486: /* fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
14487: }
14488: fprintf(ficrespijb,"******\n");
14489: if(invalidvarcomb[k]){ /* Is it necessary here? */
14490: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
14491: continue;
14492: }
14493:
14494: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
14495: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
14496: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
14497: 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 */
14498: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
14499:
14500: /* nhstepm=nhstepm*YEARM; aff par mois*/
14501:
14502: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
14503: /* and memory limitations if stepm is small */
14504:
14505: /* oldm=oldms;savm=savms; */
14506: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
14507: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
14508: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
14509: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
14510: for(i=1; i<=nlstate;i++)
14511: for(j=1; j<=nlstate+ndeath;j++)
14512: fprintf(ficrespijb," %1d-%1d",i,j);
14513: fprintf(ficrespijb,"\n");
14514: for (h=0; h<=nhstepm; h++){
14515: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
14516: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
14517: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 14518: for(i=1; i<=nlstate;i++)
14519: for(j=1; j<=nlstate+ndeath;j++)
1.337 brouard 14520: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
1.217 brouard 14521: fprintf(ficrespijb,"\n");
1.337 brouard 14522: }
14523: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
14524: fprintf(ficrespijb,"\n");
14525: } /* end age deb */
14526: /* } /\* end combination *\/ */
1.238 brouard 14527: } /* end nres */
1.218 brouard 14528: return 0;
14529: } /* hBijx */
1.217 brouard 14530:
1.180 brouard 14531:
1.136 brouard 14532: /***********************************************/
14533: /**************** Main Program *****************/
14534: /***********************************************/
14535:
14536: int main(int argc, char *argv[])
14537: {
14538: #ifdef GSL
14539: const gsl_multimin_fminimizer_type *T;
14540: size_t iteri = 0, it;
14541: int rval = GSL_CONTINUE;
14542: int status = GSL_SUCCESS;
14543: double ssval;
14544: #endif
14545: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.290 brouard 14546: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
14547: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
1.209 brouard 14548: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 14549: int jj, ll, li, lj, lk;
1.136 brouard 14550: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 14551: int num_filled;
1.136 brouard 14552: int itimes;
14553: int NDIM=2;
14554: int vpopbased=0;
1.235 brouard 14555: int nres=0;
1.258 brouard 14556: int endishere=0;
1.277 brouard 14557: int noffset=0;
1.274 brouard 14558: int ncurrv=0; /* Temporary variable */
14559:
1.164 brouard 14560: char ca[32], cb[32];
1.136 brouard 14561: /* FILE *fichtm; *//* Html File */
14562: /* FILE *ficgp;*/ /*Gnuplot File */
14563: struct stat info;
1.191 brouard 14564: double agedeb=0.;
1.194 brouard 14565:
14566: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 14567: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 14568:
1.165 brouard 14569: double fret;
1.191 brouard 14570: double dum=0.; /* Dummy variable */
1.359 ! brouard 14571: /* double*** p3mat;*/
1.218 brouard 14572: /* double ***mobaverage; */
1.319 brouard 14573: double wald;
1.164 brouard 14574:
1.351 brouard 14575: char line[MAXLINE], linetmp[MAXLINE];
1.197 brouard 14576: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
14577:
1.234 brouard 14578: char modeltemp[MAXLINE];
1.332 brouard 14579: char resultline[MAXLINE], resultlineori[MAXLINE];
1.230 brouard 14580:
1.136 brouard 14581: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 14582: char *tok, *val; /* pathtot */
1.334 brouard 14583: /* int firstobs=1, lastobs=10; /\* nobs = lastobs-firstobs declared globally ;*\/ */
1.359 ! brouard 14584: int c, h; /* c2; */
1.191 brouard 14585: int jl=0;
14586: int i1, j1, jk, stepsize=0;
1.194 brouard 14587: int count=0;
14588:
1.164 brouard 14589: int *tab;
1.136 brouard 14590: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.296 brouard 14591: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
14592: /* double anprojf, mprojf, jprojf; */
14593: /* double jintmean,mintmean,aintmean; */
14594: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14595: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
14596: double yrfproj= 10.0; /* Number of years of forward projections */
14597: double yrbproj= 10.0; /* Number of years of backward projections */
14598: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
1.136 brouard 14599: int mobilav=0,popforecast=0;
1.191 brouard 14600: int hstepm=0, nhstepm=0;
1.136 brouard 14601: int agemortsup;
14602: float sumlpop=0.;
14603: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
14604: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
14605:
1.191 brouard 14606: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 14607: double ftolpl=FTOL;
14608: double **prlim;
1.217 brouard 14609: double **bprlim;
1.317 brouard 14610: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
14611: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
1.251 brouard 14612: double ***paramstart; /* Matrix of starting parameter values */
14613: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
1.136 brouard 14614: double **matcov; /* Matrix of covariance */
1.203 brouard 14615: double **hess; /* Hessian matrix */
1.136 brouard 14616: double ***delti3; /* Scale */
14617: double *delti; /* Scale */
14618: double ***eij, ***vareij;
1.359 ! brouard 14619: //double **varpl; /* Variances of prevalence limits by age */
1.269 brouard 14620:
1.136 brouard 14621: double *epj, vepp;
1.164 brouard 14622:
1.273 brouard 14623: double dateprev1, dateprev2;
1.296 brouard 14624: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
14625: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
14626:
1.217 brouard 14627:
1.136 brouard 14628: double **ximort;
1.145 brouard 14629: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 14630: int *dcwave;
14631:
1.164 brouard 14632: char z[1]="c";
1.136 brouard 14633:
14634: /*char *strt;*/
14635: char strtend[80];
1.126 brouard 14636:
1.164 brouard 14637:
1.126 brouard 14638: /* setlocale (LC_ALL, ""); */
14639: /* bindtextdomain (PACKAGE, LOCALEDIR); */
14640: /* textdomain (PACKAGE); */
14641: /* setlocale (LC_CTYPE, ""); */
14642: /* setlocale (LC_MESSAGES, ""); */
14643:
14644: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 14645: rstart_time = time(NULL);
14646: /* (void) gettimeofday(&start_time,&tzp);*/
14647: start_time = *localtime(&rstart_time);
1.126 brouard 14648: curr_time=start_time;
1.157 brouard 14649: /*tml = *localtime(&start_time.tm_sec);*/
14650: /* strcpy(strstart,asctime(&tml)); */
14651: strcpy(strstart,asctime(&start_time));
1.126 brouard 14652:
14653: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 14654: /* tp.tm_sec = tp.tm_sec +86400; */
14655: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 14656: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
14657: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
14658: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 14659: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 14660: /* strt=asctime(&tmg); */
14661: /* printf("Time(after) =%s",strstart); */
14662: /* (void) time (&time_value);
14663: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
14664: * tm = *localtime(&time_value);
14665: * strstart=asctime(&tm);
14666: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
14667: */
14668:
14669: nberr=0; /* Number of errors and warnings */
14670: nbwarn=0;
1.184 brouard 14671: #ifdef WIN32
14672: _getcwd(pathcd, size);
14673: #else
1.126 brouard 14674: getcwd(pathcd, size);
1.184 brouard 14675: #endif
1.191 brouard 14676: syscompilerinfo(0);
1.359 ! brouard 14677: printf("\nIMaCh prax version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 14678: if(argc <=1){
14679: printf("\nEnter the parameter file name: ");
1.205 brouard 14680: if(!fgets(pathr,FILENAMELENGTH,stdin)){
14681: printf("ERROR Empty parameter file name\n");
14682: goto end;
14683: }
1.126 brouard 14684: i=strlen(pathr);
14685: if(pathr[i-1]=='\n')
14686: pathr[i-1]='\0';
1.156 brouard 14687: i=strlen(pathr);
1.205 brouard 14688: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 14689: pathr[i-1]='\0';
1.205 brouard 14690: }
14691: i=strlen(pathr);
14692: if( i==0 ){
14693: printf("ERROR Empty parameter file name\n");
14694: goto end;
14695: }
14696: for (tok = pathr; tok != NULL; ){
1.126 brouard 14697: printf("Pathr |%s|\n",pathr);
14698: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
14699: printf("val= |%s| pathr=%s\n",val,pathr);
14700: strcpy (pathtot, val);
14701: if(pathr[0] == '\0') break; /* Dirty */
14702: }
14703: }
1.281 brouard 14704: else if (argc<=2){
14705: strcpy(pathtot,argv[1]);
14706: }
1.126 brouard 14707: else{
14708: strcpy(pathtot,argv[1]);
1.281 brouard 14709: strcpy(z,argv[2]);
14710: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
1.126 brouard 14711: }
14712: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
14713: /*cygwin_split_path(pathtot,path,optionfile);
14714: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
14715: /* cutv(path,optionfile,pathtot,'\\');*/
14716:
14717: /* Split argv[0], imach program to get pathimach */
14718: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
14719: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14720: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
14721: /* strcpy(pathimach,argv[0]); */
14722: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
14723: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
14724: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 14725: #ifdef WIN32
14726: _chdir(path); /* Can be a relative path */
14727: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
14728: #else
1.126 brouard 14729: chdir(path); /* Can be a relative path */
1.184 brouard 14730: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
14731: #endif
14732: printf("Current directory %s!\n",pathcd);
1.126 brouard 14733: strcpy(command,"mkdir ");
14734: strcat(command,optionfilefiname);
14735: if((outcmd=system(command)) != 0){
1.169 brouard 14736: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 14737: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
14738: /* fclose(ficlog); */
14739: /* exit(1); */
14740: }
14741: /* if((imk=mkdir(optionfilefiname))<0){ */
14742: /* perror("mkdir"); */
14743: /* } */
14744:
14745: /*-------- arguments in the command line --------*/
14746:
1.186 brouard 14747: /* Main Log file */
1.126 brouard 14748: strcat(filelog, optionfilefiname);
14749: strcat(filelog,".log"); /* */
14750: if((ficlog=fopen(filelog,"w"))==NULL) {
14751: printf("Problem with logfile %s\n",filelog);
14752: goto end;
14753: }
14754: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 14755: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 14756: fprintf(ficlog,"\nEnter the parameter file name: \n");
14757: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
14758: path=%s \n\
14759: optionfile=%s\n\
14760: optionfilext=%s\n\
1.156 brouard 14761: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 14762:
1.197 brouard 14763: syscompilerinfo(1);
1.167 brouard 14764:
1.126 brouard 14765: printf("Local time (at start):%s",strstart);
14766: fprintf(ficlog,"Local time (at start): %s",strstart);
14767: fflush(ficlog);
14768: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 14769: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 14770:
14771: /* */
14772: strcpy(fileres,"r");
14773: strcat(fileres, optionfilefiname);
1.201 brouard 14774: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 14775: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 14776: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 14777:
1.186 brouard 14778: /* Main ---------arguments file --------*/
1.126 brouard 14779:
14780: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 14781: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
14782: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 14783: fflush(ficlog);
1.149 brouard 14784: /* goto end; */
14785: exit(70);
1.126 brouard 14786: }
14787:
14788: strcpy(filereso,"o");
1.201 brouard 14789: strcat(filereso,fileresu);
1.126 brouard 14790: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
14791: printf("Problem with Output resultfile: %s\n", filereso);
14792: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
14793: fflush(ficlog);
14794: goto end;
14795: }
1.278 brouard 14796: /*-------- Rewriting parameter file ----------*/
14797: strcpy(rfileres,"r"); /* "Rparameterfile */
14798: strcat(rfileres,optionfilefiname); /* Parameter file first name */
14799: strcat(rfileres,"."); /* */
14800: strcat(rfileres,optionfilext); /* Other files have txt extension */
14801: if((ficres =fopen(rfileres,"w"))==NULL) {
14802: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
14803: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
14804: fflush(ficlog);
14805: goto end;
14806: }
14807: fprintf(ficres,"#IMaCh %s\n",version);
1.126 brouard 14808:
1.278 brouard 14809:
1.126 brouard 14810: /* Reads comments: lines beginning with '#' */
14811: numlinepar=0;
1.277 brouard 14812: /* Is it a BOM UTF-8 Windows file? */
14813: /* First parameter line */
1.197 brouard 14814: while(fgets(line, MAXLINE, ficpar)) {
1.277 brouard 14815: noffset=0;
14816: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
14817: {
14818: noffset=noffset+3;
14819: printf("# File is an UTF8 Bom.\n"); // 0xBF
14820: }
1.302 brouard 14821: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
14822: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
1.277 brouard 14823: {
14824: noffset=noffset+2;
14825: printf("# File is an UTF16BE BOM file\n");
14826: }
14827: else if( line[0] == 0 && line[1] == 0)
14828: {
14829: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
14830: noffset=noffset+4;
14831: printf("# File is an UTF16BE BOM file\n");
14832: }
14833: } else{
14834: ;/*printf(" Not a BOM file\n");*/
14835: }
14836:
1.197 brouard 14837: /* If line starts with a # it is a comment */
1.277 brouard 14838: if (line[noffset] == '#') {
1.197 brouard 14839: numlinepar++;
14840: fputs(line,stdout);
14841: fputs(line,ficparo);
1.278 brouard 14842: fputs(line,ficres);
1.197 brouard 14843: fputs(line,ficlog);
14844: continue;
14845: }else
14846: break;
14847: }
14848: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
14849: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
14850: if (num_filled != 5) {
14851: printf("Should be 5 parameters\n");
1.283 brouard 14852: fprintf(ficlog,"Should be 5 parameters\n");
1.197 brouard 14853: }
1.126 brouard 14854: numlinepar++;
1.197 brouard 14855: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.283 brouard 14856: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14857: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
14858: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.197 brouard 14859: }
14860: /* Second parameter line */
14861: while(fgets(line, MAXLINE, ficpar)) {
1.283 brouard 14862: /* while(fscanf(ficpar,"%[^\n]", line)) { */
14863: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
1.197 brouard 14864: if (line[0] == '#') {
14865: numlinepar++;
1.283 brouard 14866: printf("%s",line);
14867: fprintf(ficres,"%s",line);
14868: fprintf(ficparo,"%s",line);
14869: fprintf(ficlog,"%s",line);
1.197 brouard 14870: continue;
14871: }else
14872: break;
14873: }
1.223 brouard 14874: 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", \
14875: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
14876: if (num_filled != 11) {
14877: 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 14878: printf("but line=%s\n",line);
1.283 brouard 14879: 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");
14880: fprintf(ficlog,"but line=%s\n",line);
1.197 brouard 14881: }
1.286 brouard 14882: if( lastpass > maxwav){
14883: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
14884: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
14885: fflush(ficlog);
14886: goto end;
14887: }
14888: 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 14889: 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 14890: 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 14891: 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 14892: }
1.203 brouard 14893: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 14894: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 14895: /* Third parameter line */
14896: while(fgets(line, MAXLINE, ficpar)) {
14897: /* If line starts with a # it is a comment */
14898: if (line[0] == '#') {
14899: numlinepar++;
1.283 brouard 14900: printf("%s",line);
14901: fprintf(ficres,"%s",line);
14902: fprintf(ficparo,"%s",line);
14903: fprintf(ficlog,"%s",line);
1.197 brouard 14904: continue;
14905: }else
14906: break;
14907: }
1.351 brouard 14908: if((num_filled=sscanf(line,"model=%[^.\n]", model)) !=EOF){ /* Every character after model but dot and return */
14909: if (num_filled != 1){
14910: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
14911: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
14912: model[0]='\0';
14913: goto end;
14914: }else{
14915: trimbtab(linetmp,line); /* Trims multiple blanks in line */
14916: strcpy(line, linetmp);
14917: }
14918: }
14919: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){ /* Every character after 1+age but dot and return */
1.279 brouard 14920: if (num_filled != 1){
1.302 brouard 14921: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
14922: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
1.197 brouard 14923: model[0]='\0';
14924: goto end;
14925: }
14926: else{
14927: if (model[0]=='+'){
14928: for(i=1; i<=strlen(model);i++)
14929: modeltemp[i-1]=model[i];
1.201 brouard 14930: strcpy(model,modeltemp);
1.197 brouard 14931: }
14932: }
1.338 brouard 14933: /* printf(" model=1+age%s modeltemp= %s, model=1+age+%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 14934: printf("model=1+age+%s\n",model);fflush(stdout);
1.283 brouard 14935: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
14936: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
14937: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 14938: }
14939: /* 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); */
14940: /* numlinepar=numlinepar+3; /\* In general *\/ */
14941: /* 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 14942: /* 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); */
14943: /* 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 14944: fflush(ficlog);
1.190 brouard 14945: /* if(model[0]=='#'|| model[0]== '\0'){ */
14946: if(model[0]=='#'){
1.279 brouard 14947: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
14948: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
14949: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
1.187 brouard 14950: if(mle != -1){
1.279 brouard 14951: 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 14952: exit(1);
14953: }
14954: }
1.126 brouard 14955: while((c=getc(ficpar))=='#' && c!= EOF){
14956: ungetc(c,ficpar);
14957: fgets(line, MAXLINE, ficpar);
14958: numlinepar++;
1.195 brouard 14959: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
14960: z[0]=line[1];
1.342 brouard 14961: }else if(line[1]=='d'){ /* For debugging individual values of covariates in ficresilk */
1.343 brouard 14962: debugILK=1;printf("DebugILK\n");
1.195 brouard 14963: }
14964: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 14965: fputs(line, stdout);
14966: //puts(line);
1.126 brouard 14967: fputs(line,ficparo);
14968: fputs(line,ficlog);
14969: }
14970: ungetc(c,ficpar);
14971:
14972:
1.290 brouard 14973: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
14974: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
14975: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
1.341 brouard 14976: /* if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /\**< Time varying covariate (dummy and quantitative)*\/ */
14977: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs); /**< Might be better */
1.136 brouard 14978: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
14979: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
14980: v1+v2*age+v2*v3 makes cptcovn = 3
14981: */
14982: if (strlen(model)>1)
1.187 brouard 14983: 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 14984: else
1.187 brouard 14985: ncovmodel=2; /* Constant and age */
1.133 brouard 14986: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
14987: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 14988: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
14989: 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);
14990: 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);
14991: fflush(stdout);
14992: fclose (ficlog);
14993: goto end;
14994: }
1.126 brouard 14995: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
14996: delti=delti3[1][1];
14997: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
14998: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
1.247 brouard 14999: /* We could also provide initial parameters values giving by simple logistic regression
15000: * only one way, that is without matrix product. We will have nlstate maximizations */
15001: /* for(i=1;i<nlstate;i++){ */
15002: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15003: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15004: /* } */
1.126 brouard 15005: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 15006: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
15007: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 15008: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
15009: fclose (ficparo);
15010: fclose (ficlog);
15011: goto end;
15012: exit(0);
1.220 brouard 15013: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 15014: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 15015: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
15016: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 15017: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
15018: matcov=matrix(1,npar,1,npar);
1.203 brouard 15019: hess=matrix(1,npar,1,npar);
1.220 brouard 15020: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 15021: /* Read guessed parameters */
1.126 brouard 15022: /* Reads comments: lines beginning with '#' */
15023: while((c=getc(ficpar))=='#' && c!= EOF){
15024: ungetc(c,ficpar);
15025: fgets(line, MAXLINE, ficpar);
15026: numlinepar++;
1.141 brouard 15027: fputs(line,stdout);
1.126 brouard 15028: fputs(line,ficparo);
15029: fputs(line,ficlog);
15030: }
15031: ungetc(c,ficpar);
15032:
15033: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.251 brouard 15034: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.126 brouard 15035: for(i=1; i <=nlstate; i++){
1.234 brouard 15036: j=0;
1.126 brouard 15037: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 15038: if(jj==i) continue;
15039: j++;
1.292 brouard 15040: while((c=getc(ficpar))=='#' && c!= EOF){
15041: ungetc(c,ficpar);
15042: fgets(line, MAXLINE, ficpar);
15043: numlinepar++;
15044: fputs(line,stdout);
15045: fputs(line,ficparo);
15046: fputs(line,ficlog);
15047: }
15048: ungetc(c,ficpar);
1.234 brouard 15049: fscanf(ficpar,"%1d%1d",&i1,&j1);
15050: if ((i1 != i) || (j1 != jj)){
15051: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 15052: It might be a problem of design; if ncovcol and the model are correct\n \
15053: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 15054: exit(1);
15055: }
15056: fprintf(ficparo,"%1d%1d",i1,j1);
15057: if(mle==1)
15058: printf("%1d%1d",i,jj);
15059: fprintf(ficlog,"%1d%1d",i,jj);
15060: for(k=1; k<=ncovmodel;k++){
15061: fscanf(ficpar," %lf",¶m[i][j][k]);
15062: if(mle==1){
15063: printf(" %lf",param[i][j][k]);
15064: fprintf(ficlog," %lf",param[i][j][k]);
15065: }
15066: else
15067: fprintf(ficlog," %lf",param[i][j][k]);
15068: fprintf(ficparo," %lf",param[i][j][k]);
15069: }
15070: fscanf(ficpar,"\n");
15071: numlinepar++;
15072: if(mle==1)
15073: printf("\n");
15074: fprintf(ficlog,"\n");
15075: fprintf(ficparo,"\n");
1.126 brouard 15076: }
15077: }
15078: fflush(ficlog);
1.234 brouard 15079:
1.251 brouard 15080: /* Reads parameters values */
1.126 brouard 15081: p=param[1][1];
1.251 brouard 15082: pstart=paramstart[1][1];
1.126 brouard 15083:
15084: /* Reads comments: lines beginning with '#' */
15085: while((c=getc(ficpar))=='#' && c!= EOF){
15086: ungetc(c,ficpar);
15087: fgets(line, MAXLINE, ficpar);
15088: numlinepar++;
1.141 brouard 15089: fputs(line,stdout);
1.126 brouard 15090: fputs(line,ficparo);
15091: fputs(line,ficlog);
15092: }
15093: ungetc(c,ficpar);
15094:
15095: for(i=1; i <=nlstate; i++){
15096: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 15097: fscanf(ficpar,"%1d%1d",&i1,&j1);
15098: if ( (i1-i) * (j1-j) != 0){
15099: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
15100: exit(1);
15101: }
15102: printf("%1d%1d",i,j);
15103: fprintf(ficparo,"%1d%1d",i1,j1);
15104: fprintf(ficlog,"%1d%1d",i1,j1);
15105: for(k=1; k<=ncovmodel;k++){
15106: fscanf(ficpar,"%le",&delti3[i][j][k]);
15107: printf(" %le",delti3[i][j][k]);
15108: fprintf(ficparo," %le",delti3[i][j][k]);
15109: fprintf(ficlog," %le",delti3[i][j][k]);
15110: }
15111: fscanf(ficpar,"\n");
15112: numlinepar++;
15113: printf("\n");
15114: fprintf(ficparo,"\n");
15115: fprintf(ficlog,"\n");
1.126 brouard 15116: }
15117: }
15118: fflush(ficlog);
1.234 brouard 15119:
1.145 brouard 15120: /* Reads covariance matrix */
1.126 brouard 15121: delti=delti3[1][1];
1.220 brouard 15122:
15123:
1.126 brouard 15124: /* 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 15125:
1.126 brouard 15126: /* Reads comments: lines beginning with '#' */
15127: while((c=getc(ficpar))=='#' && c!= EOF){
15128: ungetc(c,ficpar);
15129: fgets(line, MAXLINE, ficpar);
15130: numlinepar++;
1.141 brouard 15131: fputs(line,stdout);
1.126 brouard 15132: fputs(line,ficparo);
15133: fputs(line,ficlog);
15134: }
15135: ungetc(c,ficpar);
1.220 brouard 15136:
1.126 brouard 15137: matcov=matrix(1,npar,1,npar);
1.203 brouard 15138: hess=matrix(1,npar,1,npar);
1.131 brouard 15139: for(i=1; i <=npar; i++)
15140: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 15141:
1.194 brouard 15142: /* Scans npar lines */
1.126 brouard 15143: for(i=1; i <=npar; i++){
1.226 brouard 15144: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 15145: if(count != 3){
1.226 brouard 15146: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 15147: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15148: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 15149: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 15150: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
15151: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 15152: exit(1);
1.220 brouard 15153: }else{
1.226 brouard 15154: if(mle==1)
15155: printf("%1d%1d%d",i1,j1,jk);
15156: }
15157: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
15158: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 15159: for(j=1; j <=i; j++){
1.226 brouard 15160: fscanf(ficpar," %le",&matcov[i][j]);
15161: if(mle==1){
15162: printf(" %.5le",matcov[i][j]);
15163: }
15164: fprintf(ficlog," %.5le",matcov[i][j]);
15165: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 15166: }
15167: fscanf(ficpar,"\n");
15168: numlinepar++;
15169: if(mle==1)
1.220 brouard 15170: printf("\n");
1.126 brouard 15171: fprintf(ficlog,"\n");
15172: fprintf(ficparo,"\n");
15173: }
1.194 brouard 15174: /* End of read covariance matrix npar lines */
1.126 brouard 15175: for(i=1; i <=npar; i++)
15176: for(j=i+1;j<=npar;j++)
1.226 brouard 15177: matcov[i][j]=matcov[j][i];
1.126 brouard 15178:
15179: if(mle==1)
15180: printf("\n");
15181: fprintf(ficlog,"\n");
15182:
15183: fflush(ficlog);
15184:
15185: } /* End of mle != -3 */
1.218 brouard 15186:
1.186 brouard 15187: /* Main data
15188: */
1.290 brouard 15189: nobs=lastobs-firstobs+1; /* was = lastobs;*/
15190: /* num=lvector(1,n); */
15191: /* moisnais=vector(1,n); */
15192: /* annais=vector(1,n); */
15193: /* moisdc=vector(1,n); */
15194: /* andc=vector(1,n); */
15195: /* weight=vector(1,n); */
15196: /* agedc=vector(1,n); */
15197: /* cod=ivector(1,n); */
15198: /* for(i=1;i<=n;i++){ */
15199: num=lvector(firstobs,lastobs);
15200: moisnais=vector(firstobs,lastobs);
15201: annais=vector(firstobs,lastobs);
15202: moisdc=vector(firstobs,lastobs);
15203: andc=vector(firstobs,lastobs);
15204: weight=vector(firstobs,lastobs);
15205: agedc=vector(firstobs,lastobs);
15206: cod=ivector(firstobs,lastobs);
15207: for(i=firstobs;i<=lastobs;i++){
1.234 brouard 15208: num[i]=0;
15209: moisnais[i]=0;
15210: annais[i]=0;
15211: moisdc[i]=0;
15212: andc[i]=0;
15213: agedc[i]=0;
15214: cod[i]=0;
15215: weight[i]=1.0; /* Equal weights, 1 by default */
15216: }
1.290 brouard 15217: mint=matrix(1,maxwav,firstobs,lastobs);
15218: anint=matrix(1,maxwav,firstobs,lastobs);
1.325 brouard 15219: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
1.336 brouard 15220: /* printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel)); */
1.126 brouard 15221: tab=ivector(1,NCOVMAX);
1.144 brouard 15222: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 15223: 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 15224:
1.136 brouard 15225: /* Reads data from file datafile */
15226: if (readdata(datafile, firstobs, lastobs, &imx)==1)
15227: goto end;
15228:
15229: /* Calculation of the number of parameters from char model */
1.234 brouard 15230: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 15231: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
15232: k=3 V4 Tvar[k=3]= 4 (from V4)
15233: k=2 V1 Tvar[k=2]= 1 (from V1)
15234: k=1 Tvar[1]=2 (from V2)
1.234 brouard 15235: */
15236:
15237: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
15238: TvarsDind=ivector(1,NCOVMAX); /* */
1.330 brouard 15239: TnsdVar=ivector(1,NCOVMAX); /* */
1.335 brouard 15240: /* for(i=1; i<=NCOVMAX;i++) TnsdVar[i]=3; */
1.234 brouard 15241: TvarsD=ivector(1,NCOVMAX); /* */
15242: TvarsQind=ivector(1,NCOVMAX); /* */
15243: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 15244: TvarF=ivector(1,NCOVMAX); /* */
15245: TvarFind=ivector(1,NCOVMAX); /* */
15246: TvarV=ivector(1,NCOVMAX); /* */
15247: TvarVind=ivector(1,NCOVMAX); /* */
15248: TvarA=ivector(1,NCOVMAX); /* */
15249: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 15250: TvarFD=ivector(1,NCOVMAX); /* */
15251: TvarFDind=ivector(1,NCOVMAX); /* */
15252: TvarFQ=ivector(1,NCOVMAX); /* */
15253: TvarFQind=ivector(1,NCOVMAX); /* */
15254: TvarVD=ivector(1,NCOVMAX); /* */
15255: TvarVDind=ivector(1,NCOVMAX); /* */
15256: TvarVQ=ivector(1,NCOVMAX); /* */
15257: TvarVQind=ivector(1,NCOVMAX); /* */
1.339 brouard 15258: TvarVV=ivector(1,NCOVMAX); /* */
15259: TvarVVind=ivector(1,NCOVMAX); /* */
1.349 brouard 15260: TvarVVA=ivector(1,NCOVMAX); /* */
15261: TvarVVAind=ivector(1,NCOVMAX); /* */
15262: TvarAVVA=ivector(1,NCOVMAX); /* */
15263: TvarAVVAind=ivector(1,NCOVMAX); /* */
1.231 brouard 15264:
1.230 brouard 15265: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 15266: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 15267: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
15268: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
15269: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.349 brouard 15270: DummyV=ivector(-1,NCOVMAX); /* 1 to 3 */
15271: FixedV=ivector(-1,NCOVMAX); /* 1 to 3 */
15272:
1.137 brouard 15273: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
15274: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
15275: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
15276: */
15277: /* For model-covariate k tells which data-covariate to use but
15278: because this model-covariate is a construction we invent a new column
15279: ncovcol + k1
15280: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
15281: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 15282: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
15283: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 15284: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
15285: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 15286: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 15287: */
1.145 brouard 15288: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
15289: 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 15290: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
15291: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.351 brouard 15292: Tvardk=imatrix(0,NCOVMAX,1,2);
1.145 brouard 15293: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 15294: 4 covariates (3 plus signs)
15295: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
1.328 brouard 15296: */
15297: for(i=1;i<NCOVMAX;i++)
15298: Tage[i]=0;
1.230 brouard 15299: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 15300: * individual dummy, fixed or varying:
15301: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
15302: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 15303: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
15304: * V1 df, V2 qf, V3 & V4 dv, V5 qv
15305: * Tmodelind[1]@9={9,0,3,2,}*/
15306: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
15307: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 15308: * individual quantitative, fixed or varying:
15309: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
15310: * 3, 1, 0, 0, 0, 0, 0, 0},
15311: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.349 brouard 15312:
15313: /* Probably useless zeroes */
15314: for(i=1;i<NCOVMAX;i++){
15315: DummyV[i]=0;
15316: FixedV[i]=0;
15317: }
15318:
15319: for(i=1; i <=ncovcol;i++){
15320: DummyV[i]=0;
15321: FixedV[i]=0;
15322: }
15323: for(i=ncovcol+1; i <=ncovcol+nqv;i++){
15324: DummyV[i]=1;
15325: FixedV[i]=0;
15326: }
15327: for(i=ncovcol+nqv+1; i <=ncovcol+nqv+ntv;i++){
15328: DummyV[i]=0;
15329: FixedV[i]=1;
15330: }
15331: for(i=ncovcol+nqv+ntv+1; i <=ncovcol+nqv+ntv+nqtv;i++){
15332: DummyV[i]=1;
15333: FixedV[i]=1;
15334: }
15335: for(i=1; i <=ncovcol+nqv+ntv+nqtv;i++){
15336: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",i,i,DummyV[i],i,FixedV[i]);
15337: 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]);
15338: }
15339:
15340:
15341:
1.186 brouard 15342: /* Main decodemodel */
15343:
1.187 brouard 15344:
1.223 brouard 15345: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 15346: goto end;
15347:
1.137 brouard 15348: if((double)(lastobs-imx)/(double)imx > 1.10){
15349: nbwarn++;
15350: 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);
15351: 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);
15352: }
1.136 brouard 15353: /* if(mle==1){*/
1.137 brouard 15354: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
15355: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 15356: }
15357:
15358: /*-calculation of age at interview from date of interview and age at death -*/
15359: agev=matrix(1,maxwav,1,imx);
15360:
15361: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
15362: goto end;
15363:
1.126 brouard 15364:
1.136 brouard 15365: agegomp=(int)agemin;
1.290 brouard 15366: free_vector(moisnais,firstobs,lastobs);
15367: free_vector(annais,firstobs,lastobs);
1.126 brouard 15368: /* free_matrix(mint,1,maxwav,1,n);
15369: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 15370: /* free_vector(moisdc,1,n); */
15371: /* free_vector(andc,1,n); */
1.145 brouard 15372: /* */
15373:
1.126 brouard 15374: wav=ivector(1,imx);
1.214 brouard 15375: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
15376: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
15377: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
15378: 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.*/
15379: bh=imatrix(1,lastpass-firstpass+2,1,imx);
15380: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 15381:
15382: /* Concatenates waves */
1.214 brouard 15383: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
15384: Death is a valid wave (if date is known).
15385: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
15386: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
15387: and mw[mi+1][i]. dh depends on stepm.
15388: */
15389:
1.126 brouard 15390: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.248 brouard 15391: /* Concatenates waves */
1.145 brouard 15392:
1.290 brouard 15393: free_vector(moisdc,firstobs,lastobs);
15394: free_vector(andc,firstobs,lastobs);
1.215 brouard 15395:
1.126 brouard 15396: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
15397: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
15398: ncodemax[1]=1;
1.145 brouard 15399: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 15400: cptcoveff=0;
1.220 brouard 15401: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
1.335 brouard 15402: 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 15403: }
15404:
15405: ncovcombmax=pow(2,cptcoveff);
1.338 brouard 15406: invalidvarcomb=ivector(0, ncovcombmax);
15407: for(i=0;i<ncovcombmax;i++)
1.227 brouard 15408: invalidvarcomb[i]=0;
15409:
1.211 brouard 15410: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 15411: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 15412: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 15413:
1.200 brouard 15414: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 15415: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 15416: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 15417: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
15418: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
15419: * (currently 0 or 1) in the data.
15420: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
15421: * corresponding modality (h,j).
15422: */
15423:
1.145 brouard 15424: h=0;
15425: /*if (cptcovn > 0) */
1.126 brouard 15426: m=pow(2,cptcoveff);
15427:
1.144 brouard 15428: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 15429: * For k=4 covariates, h goes from 1 to m=2**k
15430: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
15431: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.329 brouard 15432: * h\k 1 2 3 4 * h-1\k-1 4 3 2 1
15433: *______________________________ *______________________
15434: * 1 i=1 1 i=1 1 i=1 1 i=1 1 * 0 0 0 0 0
15435: * 2 2 1 1 1 * 1 0 0 0 1
15436: * 3 i=2 1 2 1 1 * 2 0 0 1 0
15437: * 4 2 2 1 1 * 3 0 0 1 1
15438: * 5 i=3 1 i=2 1 2 1 * 4 0 1 0 0
15439: * 6 2 1 2 1 * 5 0 1 0 1
15440: * 7 i=4 1 2 2 1 * 6 0 1 1 0
15441: * 8 2 2 2 1 * 7 0 1 1 1
15442: * 9 i=5 1 i=3 1 i=2 1 2 * 8 1 0 0 0
15443: * 10 2 1 1 2 * 9 1 0 0 1
15444: * 11 i=6 1 2 1 2 * 10 1 0 1 0
15445: * 12 2 2 1 2 * 11 1 0 1 1
15446: * 13 i=7 1 i=4 1 2 2 * 12 1 1 0 0
15447: * 14 2 1 2 2 * 13 1 1 0 1
15448: * 15 i=8 1 2 2 2 * 14 1 1 1 0
15449: * 16 2 2 2 2 * 15 1 1 1 1
15450: */
1.212 brouard 15451: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 15452: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
15453: * and the value of each covariate?
15454: * V1=1, V2=1, V3=2, V4=1 ?
15455: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
15456: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
15457: * In order to get the real value in the data, we use nbcode
15458: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
15459: * We are keeping this crazy system in order to be able (in the future?)
15460: * to have more than 2 values (0 or 1) for a covariate.
15461: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
15462: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
15463: * bbbbbbbb
15464: * 76543210
15465: * h-1 00000101 (6-1=5)
1.219 brouard 15466: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 15467: * &
15468: * 1 00000001 (1)
1.219 brouard 15469: * 00000000 = 1 & ((h-1) >> (k-1))
15470: * +1= 00000001 =1
1.211 brouard 15471: *
15472: * h=14, k=3 => h'=h-1=13, k'=k-1=2
15473: * h' 1101 =2^3+2^2+0x2^1+2^0
15474: * >>k' 11
15475: * & 00000001
15476: * = 00000001
15477: * +1 = 00000010=2 = codtabm(14,3)
15478: * Reverse h=6 and m=16?
15479: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
15480: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
15481: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
15482: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
15483: * V3=decodtabm(14,3,2**4)=2
15484: * h'=13 1101 =2^3+2^2+0x2^1+2^0
15485: *(h-1) >> (j-1) 0011 =13 >> 2
15486: * &1 000000001
15487: * = 000000001
15488: * +1= 000000010 =2
15489: * 2211
15490: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
15491: * V3=2
1.220 brouard 15492: * codtabm and decodtabm are identical
1.211 brouard 15493: */
15494:
1.145 brouard 15495:
15496: free_ivector(Ndum,-1,NCOVMAX);
15497:
15498:
1.126 brouard 15499:
1.186 brouard 15500: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 15501: strcpy(optionfilegnuplot,optionfilefiname);
15502: if(mle==-3)
1.201 brouard 15503: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 15504: strcat(optionfilegnuplot,".gp");
15505:
15506: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
15507: printf("Problem with file %s",optionfilegnuplot);
15508: }
15509: else{
1.204 brouard 15510: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 15511: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 15512: //fprintf(ficgp,"set missing 'NaNq'\n");
15513: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 15514: }
15515: /* fclose(ficgp);*/
1.186 brouard 15516:
15517:
15518: /* Initialisation of --------- index.htm --------*/
1.126 brouard 15519:
15520: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
15521: if(mle==-3)
1.201 brouard 15522: strcat(optionfilehtm,"-MORT_");
1.126 brouard 15523: strcat(optionfilehtm,".htm");
15524: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 15525: printf("Problem with %s \n",optionfilehtm);
15526: exit(0);
1.126 brouard 15527: }
15528:
15529: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
15530: strcat(optionfilehtmcov,"-cov.htm");
15531: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
15532: printf("Problem with %s \n",optionfilehtmcov), exit(0);
15533: }
15534: else{
15535: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
15536: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 15537: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 15538: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
15539: }
15540:
1.335 brouard 15541: fprintf(fichtm,"<html><head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n\
15542: <title>IMaCh %s</title></head>\n\
15543: <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n\
15544: <font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>\
15545: -EUROREVES-Institut de longévité-2013-2022-Japan Society for the Promotion of Sciences 日本学術振興会 \
15546: (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - \
15547: <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \n", optionfilehtm);
15548:
15549: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 15550: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 15551: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.337 brouard 15552: 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 15553: \n\
15554: <hr size=\"2\" color=\"#EC5E5E\">\
15555: <ul><li><h4>Parameter files</h4>\n\
15556: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
15557: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
15558: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
15559: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
15560: - Date and time at start: %s</ul>\n",\
1.335 brouard 15561: version,fullversion,optionfilehtm,optionfilehtm,title,datafile,datafile,firstpass,lastpass,stepm, weightopt, model, \
1.126 brouard 15562: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
15563: fileres,fileres,\
15564: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
15565: fflush(fichtm);
15566:
15567: strcpy(pathr,path);
15568: strcat(pathr,optionfilefiname);
1.184 brouard 15569: #ifdef WIN32
15570: _chdir(optionfilefiname); /* Move to directory named optionfile */
15571: #else
1.126 brouard 15572: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 15573: #endif
15574:
1.126 brouard 15575:
1.220 brouard 15576: /* Calculates basic frequencies. Computes observed prevalence at single age
15577: and for any valid combination of covariates
1.126 brouard 15578: and prints on file fileres'p'. */
1.359 ! brouard 15579: freqsummary(fileres, p, pstart, (double)agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.227 brouard 15580: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 15581:
15582: fprintf(fichtm,"\n");
1.286 brouard 15583: 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 15584: ftol, stepm);
15585: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
15586: ncurrv=1;
15587: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
15588: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
15589: ncurrv=i;
15590: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 15591: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
1.274 brouard 15592: ncurrv=i;
15593: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 15594: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
1.274 brouard 15595: ncurrv=i;
15596: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
15597: 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", \
15598: nlstate, ndeath, maxwav, mle, weightopt);
15599:
15600: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
15601: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
15602:
15603:
1.317 brouard 15604: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
1.126 brouard 15605: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
15606: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.274 brouard 15607: imx,agemin,agemax,jmin,jmax,jmean);
1.126 brouard 15608: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.268 brouard 15609: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15610: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15611: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
15612: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 15613:
1.126 brouard 15614: /* For Powell, parameters are in a vector p[] starting at p[1]
15615: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
15616: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
15617:
15618: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 15619: /* For mortality only */
1.126 brouard 15620: if (mle==-3){
1.136 brouard 15621: ximort=matrix(1,NDIM,1,NDIM);
1.248 brouard 15622: for(i=1;i<=NDIM;i++)
15623: for(j=1;j<=NDIM;j++)
15624: ximort[i][j]=0.;
1.186 brouard 15625: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.290 brouard 15626: cens=ivector(firstobs,lastobs);
15627: ageexmed=vector(firstobs,lastobs);
15628: agecens=vector(firstobs,lastobs);
15629: dcwave=ivector(firstobs,lastobs);
1.223 brouard 15630:
1.126 brouard 15631: for (i=1; i<=imx; i++){
15632: dcwave[i]=-1;
15633: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 15634: if (s[m][i]>nlstate) {
15635: dcwave[i]=m;
15636: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
15637: break;
15638: }
1.126 brouard 15639: }
1.226 brouard 15640:
1.126 brouard 15641: for (i=1; i<=imx; i++) {
15642: if (wav[i]>0){
1.226 brouard 15643: ageexmed[i]=agev[mw[1][i]][i];
15644: j=wav[i];
15645: agecens[i]=1.;
15646:
15647: if (ageexmed[i]> 1 && wav[i] > 0){
15648: agecens[i]=agev[mw[j][i]][i];
15649: cens[i]= 1;
15650: }else if (ageexmed[i]< 1)
15651: cens[i]= -1;
15652: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
15653: cens[i]=0 ;
1.126 brouard 15654: }
15655: else cens[i]=-1;
15656: }
15657:
15658: for (i=1;i<=NDIM;i++) {
15659: for (j=1;j<=NDIM;j++)
1.226 brouard 15660: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 15661: }
15662:
1.302 brouard 15663: p[1]=0.0268; p[NDIM]=0.083;
15664: /* printf("%lf %lf", p[1], p[2]); */
1.126 brouard 15665:
15666:
1.136 brouard 15667: #ifdef GSL
15668: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 15669: #else
1.359 ! brouard 15670: printf("Powell-mort\n"); fprintf(ficlog,"Powell-mort\n");
1.136 brouard 15671: #endif
1.201 brouard 15672: strcpy(filerespow,"POW-MORT_");
15673: strcat(filerespow,fileresu);
1.126 brouard 15674: if((ficrespow=fopen(filerespow,"w"))==NULL) {
15675: printf("Problem with resultfile: %s\n", filerespow);
15676: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
15677: }
1.136 brouard 15678: #ifdef GSL
15679: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 15680: #else
1.126 brouard 15681: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 15682: #endif
1.126 brouard 15683: /* for (i=1;i<=nlstate;i++)
15684: for(j=1;j<=nlstate+ndeath;j++)
15685: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
15686: */
15687: fprintf(ficrespow,"\n");
1.136 brouard 15688: #ifdef GSL
15689: /* gsl starts here */
15690: T = gsl_multimin_fminimizer_nmsimplex;
15691: gsl_multimin_fminimizer *sfm = NULL;
15692: gsl_vector *ss, *x;
15693: gsl_multimin_function minex_func;
15694:
15695: /* Initial vertex size vector */
15696: ss = gsl_vector_alloc (NDIM);
15697:
15698: if (ss == NULL){
15699: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
15700: }
15701: /* Set all step sizes to 1 */
15702: gsl_vector_set_all (ss, 0.001);
15703:
15704: /* Starting point */
1.126 brouard 15705:
1.136 brouard 15706: x = gsl_vector_alloc (NDIM);
15707:
15708: if (x == NULL){
15709: gsl_vector_free(ss);
15710: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
15711: }
15712:
15713: /* Initialize method and iterate */
15714: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 15715: /* gsl_vector_set(x, 0, 0.0268); */
15716: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 15717: gsl_vector_set(x, 0, p[1]);
15718: gsl_vector_set(x, 1, p[2]);
15719:
15720: minex_func.f = &gompertz_f;
15721: minex_func.n = NDIM;
15722: minex_func.params = (void *)&p; /* ??? */
15723:
15724: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
15725: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
15726:
15727: printf("Iterations beginning .....\n\n");
15728: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
15729:
15730: iteri=0;
15731: while (rval == GSL_CONTINUE){
15732: iteri++;
15733: status = gsl_multimin_fminimizer_iterate(sfm);
15734:
15735: if (status) printf("error: %s\n", gsl_strerror (status));
15736: fflush(0);
15737:
15738: if (status)
15739: break;
15740:
15741: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
15742: ssval = gsl_multimin_fminimizer_size (sfm);
15743:
15744: if (rval == GSL_SUCCESS)
15745: printf ("converged to a local maximum at\n");
15746:
15747: printf("%5d ", iteri);
15748: for (it = 0; it < NDIM; it++){
15749: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
15750: }
15751: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
15752: }
15753:
15754: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
15755:
15756: gsl_vector_free(x); /* initial values */
15757: gsl_vector_free(ss); /* inital step size */
15758: for (it=0; it<NDIM; it++){
15759: p[it+1]=gsl_vector_get(sfm->x,it);
15760: fprintf(ficrespow," %.12lf", p[it]);
15761: }
15762: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
15763: #endif
15764: #ifdef POWELL
15765: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
15766: #endif
1.126 brouard 15767: fclose(ficrespow);
15768:
1.203 brouard 15769: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 15770:
15771: for(i=1; i <=NDIM; i++)
15772: for(j=i+1;j<=NDIM;j++)
1.359 ! brouard 15773: matcov[i][j]=matcov[j][i];
1.126 brouard 15774:
15775: printf("\nCovariance matrix\n ");
1.203 brouard 15776: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 15777: for(i=1; i <=NDIM; i++) {
15778: for(j=1;j<=NDIM;j++){
1.220 brouard 15779: printf("%f ",matcov[i][j]);
15780: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 15781: }
1.203 brouard 15782: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 15783: }
15784:
15785: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 15786: for (i=1;i<=NDIM;i++) {
1.126 brouard 15787: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 15788: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
15789: }
1.302 brouard 15790: lsurv=vector(agegomp,AGESUP);
15791: lpop=vector(agegomp,AGESUP);
15792: tpop=vector(agegomp,AGESUP);
1.126 brouard 15793: lsurv[agegomp]=100000;
15794:
15795: for (k=agegomp;k<=AGESUP;k++) {
15796: agemortsup=k;
15797: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
15798: }
15799:
15800: for (k=agegomp;k<agemortsup;k++)
15801: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
15802:
15803: for (k=agegomp;k<agemortsup;k++){
15804: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
15805: sumlpop=sumlpop+lpop[k];
15806: }
15807:
15808: tpop[agegomp]=sumlpop;
15809: for (k=agegomp;k<(agemortsup-3);k++){
15810: /* tpop[k+1]=2;*/
15811: tpop[k+1]=tpop[k]-lpop[k];
15812: }
15813:
15814:
15815: printf("\nAge lx qx dx Lx Tx e(x)\n");
15816: for (k=agegomp;k<(agemortsup-2);k++)
15817: 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]);
15818:
15819:
15820: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 15821: ageminpar=50;
15822: agemaxpar=100;
1.194 brouard 15823: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
15824: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15825: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15826: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
15827: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
15828: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
15829: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 15830: }else{
15831: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
15832: 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 15833: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 15834: }
1.201 brouard 15835: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 15836: stepm, weightopt,\
15837: model,imx,p,matcov,agemortsup);
15838:
1.302 brouard 15839: free_vector(lsurv,agegomp,AGESUP);
15840: free_vector(lpop,agegomp,AGESUP);
15841: free_vector(tpop,agegomp,AGESUP);
1.220 brouard 15842: free_matrix(ximort,1,NDIM,1,NDIM);
1.290 brouard 15843: free_ivector(dcwave,firstobs,lastobs);
15844: free_vector(agecens,firstobs,lastobs);
15845: free_vector(ageexmed,firstobs,lastobs);
15846: free_ivector(cens,firstobs,lastobs);
1.220 brouard 15847: #ifdef GSL
1.136 brouard 15848: #endif
1.186 brouard 15849: } /* Endof if mle==-3 mortality only */
1.205 brouard 15850: /* Standard */
15851: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
15852: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
15853: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 15854: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 15855: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
15856: for (k=1; k<=npar;k++)
15857: printf(" %d %8.5f",k,p[k]);
15858: printf("\n");
1.205 brouard 15859: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
15860: /* mlikeli uses func not funcone */
1.247 brouard 15861: /* for(i=1;i<nlstate;i++){ */
15862: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
15863: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
15864: /* } */
1.205 brouard 15865: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
15866: }
15867: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
15868: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
15869: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
15870: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
15871: }
15872: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 15873: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
15874: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
1.335 brouard 15875: /* exit(0); */
1.126 brouard 15876: for (k=1; k<=npar;k++)
15877: printf(" %d %8.5f",k,p[k]);
15878: printf("\n");
15879:
15880: /*--------- results files --------------*/
1.283 brouard 15881: /* 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 15882:
15883:
15884: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 15885: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
1.126 brouard 15886: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 15887:
15888: printf("#model= 1 + age ");
15889: fprintf(ficres,"#model= 1 + age ");
15890: fprintf(ficlog,"#model= 1 + age ");
15891: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
15892: </ul>", model);
15893:
15894: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
15895: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
15896: if(nagesqr==1){
15897: printf(" + age*age ");
15898: fprintf(ficres," + age*age ");
15899: fprintf(ficlog," + age*age ");
15900: fprintf(fichtm, "<th>+ age*age</th>");
15901: }
15902: for(j=1;j <=ncovmodel-2;j++){
15903: if(Typevar[j]==0) {
15904: printf(" + V%d ",Tvar[j]);
15905: fprintf(ficres," + V%d ",Tvar[j]);
15906: fprintf(ficlog," + V%d ",Tvar[j]);
15907: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
15908: }else if(Typevar[j]==1) {
15909: printf(" + V%d*age ",Tvar[j]);
15910: fprintf(ficres," + V%d*age ",Tvar[j]);
15911: fprintf(ficlog," + V%d*age ",Tvar[j]);
15912: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
15913: }else if(Typevar[j]==2) {
15914: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
15915: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
15916: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
15917: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 15918: }else if(Typevar[j]==3) { /* TO VERIFY */
15919: printf(" + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
15920: fprintf(ficres," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
15921: fprintf(ficlog," + V%d*V%d*age ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
15922: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.319 brouard 15923: }
15924: }
15925: printf("\n");
15926: fprintf(ficres,"\n");
15927: fprintf(ficlog,"\n");
15928: fprintf(fichtm, "</tr>");
15929: fprintf(fichtm, "\n");
15930:
15931:
1.126 brouard 15932: for(i=1,jk=1; i <=nlstate; i++){
15933: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 15934: if (k != i) {
1.319 brouard 15935: fprintf(fichtm, "<tr>");
1.225 brouard 15936: printf("%d%d ",i,k);
15937: fprintf(ficlog,"%d%d ",i,k);
15938: fprintf(ficres,"%1d%1d ",i,k);
1.319 brouard 15939: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 15940: for(j=1; j <=ncovmodel; j++){
15941: printf("%12.7f ",p[jk]);
15942: fprintf(ficlog,"%12.7f ",p[jk]);
15943: fprintf(ficres,"%12.7f ",p[jk]);
1.319 brouard 15944: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
1.225 brouard 15945: jk++;
15946: }
15947: printf("\n");
15948: fprintf(ficlog,"\n");
15949: fprintf(ficres,"\n");
1.319 brouard 15950: fprintf(fichtm, "</tr>\n");
1.225 brouard 15951: }
1.126 brouard 15952: }
15953: }
1.319 brouard 15954: /* fprintf(fichtm,"</tr>\n"); */
15955: fprintf(fichtm,"</table>\n");
15956: fprintf(fichtm, "\n");
15957:
1.203 brouard 15958: if(mle != 0){
15959: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 15960: ftolhess=ftol; /* Usually correct */
1.203 brouard 15961: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
15962: 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");
15963: 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 15964: 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 15965: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
15966: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
15967: if(nagesqr==1){
15968: printf(" + age*age ");
15969: fprintf(ficres," + age*age ");
15970: fprintf(ficlog," + age*age ");
15971: fprintf(fichtm, "<th>+ age*age</th>");
15972: }
15973: for(j=1;j <=ncovmodel-2;j++){
15974: if(Typevar[j]==0) {
15975: printf(" + V%d ",Tvar[j]);
15976: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
15977: }else if(Typevar[j]==1) {
15978: printf(" + V%d*age ",Tvar[j]);
15979: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
15980: }else if(Typevar[j]==2) {
15981: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.349 brouard 15982: }else if(Typevar[j]==3) { /* TO VERIFY */
15983: fprintf(fichtm, "<th>+ V%d*V%d*age</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
1.319 brouard 15984: }
15985: }
15986: fprintf(fichtm, "</tr>\n");
15987:
1.203 brouard 15988: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 15989: for(k=1; k <=(nlstate+ndeath); k++){
15990: if (k != i) {
1.319 brouard 15991: fprintf(fichtm, "<tr valign=top>");
1.225 brouard 15992: printf("%d%d ",i,k);
15993: fprintf(ficlog,"%d%d ",i,k);
1.319 brouard 15994: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 15995: for(j=1; j <=ncovmodel; j++){
1.319 brouard 15996: wald=p[jk]/sqrt(matcov[jk][jk]);
1.324 brouard 15997: 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]));
15998: 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 15999: if(fabs(wald) > 1.96){
1.321 brouard 16000: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
1.319 brouard 16001: }else{
16002: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
16003: }
1.324 brouard 16004: fprintf(fichtm,"W=%8.3f</br>",wald);
1.319 brouard 16005: 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 16006: jk++;
16007: }
16008: printf("\n");
16009: fprintf(ficlog,"\n");
1.319 brouard 16010: fprintf(fichtm, "</tr>\n");
1.225 brouard 16011: }
16012: }
1.193 brouard 16013: }
1.203 brouard 16014: } /* end of hesscov and Wald tests */
1.319 brouard 16015: fprintf(fichtm,"</table>\n");
1.225 brouard 16016:
1.203 brouard 16017: /* */
1.126 brouard 16018: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
16019: printf("# Scales (for hessian or gradient estimation)\n");
16020: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
16021: for(i=1,jk=1; i <=nlstate; i++){
16022: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 16023: if (j!=i) {
16024: fprintf(ficres,"%1d%1d",i,j);
16025: printf("%1d%1d",i,j);
16026: fprintf(ficlog,"%1d%1d",i,j);
16027: for(k=1; k<=ncovmodel;k++){
16028: printf(" %.5e",delti[jk]);
16029: fprintf(ficlog," %.5e",delti[jk]);
16030: fprintf(ficres," %.5e",delti[jk]);
16031: jk++;
16032: }
16033: printf("\n");
16034: fprintf(ficlog,"\n");
16035: fprintf(ficres,"\n");
16036: }
1.126 brouard 16037: }
16038: }
16039:
16040: 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 16041: if(mle >= 1) /* Too big for the screen */
1.126 brouard 16042: 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");
16043: 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");
16044: /* # 121 Var(a12)\n\ */
16045: /* # 122 Cov(b12,a12) Var(b12)\n\ */
16046: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
16047: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
16048: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
16049: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
16050: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
16051: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
16052:
16053:
16054: /* Just to have a covariance matrix which will be more understandable
16055: even is we still don't want to manage dictionary of variables
16056: */
16057: for(itimes=1;itimes<=2;itimes++){
16058: jj=0;
16059: for(i=1; i <=nlstate; i++){
1.225 brouard 16060: for(j=1; j <=nlstate+ndeath; j++){
16061: if(j==i) continue;
16062: for(k=1; k<=ncovmodel;k++){
16063: jj++;
16064: ca[0]= k+'a'-1;ca[1]='\0';
16065: if(itimes==1){
16066: if(mle>=1)
16067: printf("#%1d%1d%d",i,j,k);
16068: fprintf(ficlog,"#%1d%1d%d",i,j,k);
16069: fprintf(ficres,"#%1d%1d%d",i,j,k);
16070: }else{
16071: if(mle>=1)
16072: printf("%1d%1d%d",i,j,k);
16073: fprintf(ficlog,"%1d%1d%d",i,j,k);
16074: fprintf(ficres,"%1d%1d%d",i,j,k);
16075: }
16076: ll=0;
16077: for(li=1;li <=nlstate; li++){
16078: for(lj=1;lj <=nlstate+ndeath; lj++){
16079: if(lj==li) continue;
16080: for(lk=1;lk<=ncovmodel;lk++){
16081: ll++;
16082: if(ll<=jj){
16083: cb[0]= lk +'a'-1;cb[1]='\0';
16084: if(ll<jj){
16085: if(itimes==1){
16086: if(mle>=1)
16087: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16088: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16089: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
16090: }else{
16091: if(mle>=1)
16092: printf(" %.5e",matcov[jj][ll]);
16093: fprintf(ficlog," %.5e",matcov[jj][ll]);
16094: fprintf(ficres," %.5e",matcov[jj][ll]);
16095: }
16096: }else{
16097: if(itimes==1){
16098: if(mle>=1)
16099: printf(" Var(%s%1d%1d)",ca,i,j);
16100: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
16101: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
16102: }else{
16103: if(mle>=1)
16104: printf(" %.7e",matcov[jj][ll]);
16105: fprintf(ficlog," %.7e",matcov[jj][ll]);
16106: fprintf(ficres," %.7e",matcov[jj][ll]);
16107: }
16108: }
16109: }
16110: } /* end lk */
16111: } /* end lj */
16112: } /* end li */
16113: if(mle>=1)
16114: printf("\n");
16115: fprintf(ficlog,"\n");
16116: fprintf(ficres,"\n");
16117: numlinepar++;
16118: } /* end k*/
16119: } /*end j */
1.126 brouard 16120: } /* end i */
16121: } /* end itimes */
16122:
16123: fflush(ficlog);
16124: fflush(ficres);
1.225 brouard 16125: while(fgets(line, MAXLINE, ficpar)) {
16126: /* If line starts with a # it is a comment */
16127: if (line[0] == '#') {
16128: numlinepar++;
16129: fputs(line,stdout);
16130: fputs(line,ficparo);
16131: fputs(line,ficlog);
1.299 brouard 16132: fputs(line,ficres);
1.225 brouard 16133: continue;
16134: }else
16135: break;
16136: }
16137:
1.209 brouard 16138: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
16139: /* ungetc(c,ficpar); */
16140: /* fgets(line, MAXLINE, ficpar); */
16141: /* fputs(line,stdout); */
16142: /* fputs(line,ficparo); */
16143: /* } */
16144: /* ungetc(c,ficpar); */
1.126 brouard 16145:
16146: estepm=0;
1.209 brouard 16147: 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 16148:
16149: if (num_filled != 6) {
16150: 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);
16151: 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);
16152: goto end;
16153: }
16154: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
16155: }
16156: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
16157: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
16158:
1.209 brouard 16159: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 16160: if (estepm==0 || estepm < stepm) estepm=stepm;
16161: if (fage <= 2) {
16162: bage = ageminpar;
16163: fage = agemaxpar;
16164: }
16165:
16166: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 16167: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
16168: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 16169:
1.186 brouard 16170: /* Other stuffs, more or less useful */
1.254 brouard 16171: while(fgets(line, MAXLINE, ficpar)) {
16172: /* If line starts with a # it is a comment */
16173: if (line[0] == '#') {
16174: numlinepar++;
16175: fputs(line,stdout);
16176: fputs(line,ficparo);
16177: fputs(line,ficlog);
1.299 brouard 16178: fputs(line,ficres);
1.254 brouard 16179: continue;
16180: }else
16181: break;
16182: }
16183:
16184: 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){
16185:
16186: if (num_filled != 7) {
16187: 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);
16188: 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);
16189: goto end;
16190: }
16191: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
16192: 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);
16193: 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);
16194: 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 16195: }
1.254 brouard 16196:
16197: while(fgets(line, MAXLINE, ficpar)) {
16198: /* If line starts with a # it is a comment */
16199: if (line[0] == '#') {
16200: numlinepar++;
16201: fputs(line,stdout);
16202: fputs(line,ficparo);
16203: fputs(line,ficlog);
1.299 brouard 16204: fputs(line,ficres);
1.254 brouard 16205: continue;
16206: }else
16207: break;
1.126 brouard 16208: }
16209:
16210:
16211: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
16212: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
16213:
1.254 brouard 16214: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
16215: if (num_filled != 1) {
16216: 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);
16217: 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);
16218: goto end;
16219: }
16220: printf("pop_based=%d\n",popbased);
16221: fprintf(ficlog,"pop_based=%d\n",popbased);
16222: fprintf(ficparo,"pop_based=%d\n",popbased);
16223: fprintf(ficres,"pop_based=%d\n",popbased);
16224: }
16225:
1.258 brouard 16226: /* Results */
1.359 ! brouard 16227: /* Value of covariate in each resultine will be computed (if product) and sorted according to model rank */
1.332 brouard 16228: /* It is precov[] because we need the varying age in order to compute the real cov[] of the model equation */
16229: precov=matrix(1,MAXRESULTLINESPONE,1,NCOVMAX+1);
1.307 brouard 16230: endishere=0;
1.258 brouard 16231: nresult=0;
1.308 brouard 16232: parameterline=0;
1.258 brouard 16233: do{
16234: if(!fgets(line, MAXLINE, ficpar)){
16235: endishere=1;
1.308 brouard 16236: parameterline=15;
1.258 brouard 16237: }else if (line[0] == '#') {
16238: /* If line starts with a # it is a comment */
1.254 brouard 16239: numlinepar++;
16240: fputs(line,stdout);
16241: fputs(line,ficparo);
16242: fputs(line,ficlog);
1.299 brouard 16243: fputs(line,ficres);
1.254 brouard 16244: continue;
1.258 brouard 16245: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
16246: parameterline=11;
1.296 brouard 16247: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
1.258 brouard 16248: parameterline=12;
1.307 brouard 16249: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
1.258 brouard 16250: parameterline=13;
1.307 brouard 16251: }
1.258 brouard 16252: else{
16253: parameterline=14;
1.254 brouard 16254: }
1.308 brouard 16255: switch (parameterline){ /* =0 only if only comments */
1.258 brouard 16256: case 11:
1.296 brouard 16257: 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)){
16258: 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 16259: 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);
16260: 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);
16261: 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);
16262: /* day and month of proj2 are not used but only year anproj2.*/
1.273 brouard 16263: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
16264: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
1.296 brouard 16265: prvforecast = 1;
16266: }
16267: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
1.313 brouard 16268: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16269: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
16270: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
1.296 brouard 16271: prvforecast = 2;
16272: }
16273: else {
16274: 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);
16275: 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);
16276: goto end;
1.258 brouard 16277: }
1.254 brouard 16278: break;
1.258 brouard 16279: case 12:
1.296 brouard 16280: 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)){
16281: 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);
16282: 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);
16283: 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);
16284: 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);
16285: /* day and month of back2 are not used but only year anback2.*/
1.273 brouard 16286: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
16287: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
1.296 brouard 16288: prvbackcast = 1;
16289: }
16290: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
1.313 brouard 16291: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16292: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
16293: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
1.296 brouard 16294: prvbackcast = 2;
16295: }
16296: else {
16297: 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);
16298: 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);
16299: goto end;
1.258 brouard 16300: }
1.230 brouard 16301: break;
1.258 brouard 16302: case 13:
1.332 brouard 16303: num_filled=sscanf(line,"result:%[^\n]\n",resultlineori);
1.307 brouard 16304: nresult++; /* Sum of resultlines */
1.342 brouard 16305: /* printf("Result %d: result:%s\n",nresult, resultlineori); */
1.332 brouard 16306: /* removefirstspace(&resultlineori); */
16307:
16308: if(strstr(resultlineori,"v") !=0){
16309: printf("Error. 'v' must be in upper case 'V' result: %s ",resultlineori);
16310: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultlineori);fflush(ficlog);
16311: return 1;
16312: }
16313: trimbb(resultline, resultlineori); /* Suppressing double blank in the resultline */
1.342 brouard 16314: /* printf("Decoderesult resultline=\"%s\" resultlineori=\"%s\"\n", resultline, resultlineori); */
1.318 brouard 16315: if(nresult > MAXRESULTLINESPONE-1){
16316: 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);
16317: 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 16318: goto end;
16319: }
1.332 brouard 16320:
1.310 brouard 16321: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.314 brouard 16322: fprintf(ficparo,"result: %s\n",resultline);
16323: fprintf(ficres,"result: %s\n",resultline);
16324: fprintf(ficlog,"result: %s\n",resultline);
1.310 brouard 16325: } else
16326: goto end;
1.307 brouard 16327: break;
16328: case 14:
16329: printf("Error: Unknown command '%s'\n",line);
16330: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
1.314 brouard 16331: if(line[0] == ' ' || line[0] == '\n'){
16332: printf("It should not be an empty line '%s'\n",line);
16333: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
16334: }
1.307 brouard 16335: if(ncovmodel >=2 && nresult==0 ){
16336: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
16337: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
1.258 brouard 16338: }
1.307 brouard 16339: /* goto end; */
16340: break;
1.308 brouard 16341: case 15:
16342: printf("End of resultlines.\n");
16343: fprintf(ficlog,"End of resultlines.\n");
16344: break;
16345: default: /* parameterline =0 */
1.307 brouard 16346: nresult=1;
16347: decoderesult(".",nresult ); /* No covariate */
1.258 brouard 16348: } /* End switch parameterline */
16349: }while(endishere==0); /* End do */
1.126 brouard 16350:
1.230 brouard 16351: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 16352: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 16353:
16354: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 16355: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 16356: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 16357: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16358: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 16359: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 16360: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
16361: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 16362: }else{
1.270 brouard 16363: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
1.296 brouard 16364: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
16365: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
16366: if(prvforecast==1){
16367: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
16368: jprojd=jproj1;
16369: mprojd=mproj1;
16370: anprojd=anproj1;
16371: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
16372: jprojf=jproj2;
16373: mprojf=mproj2;
16374: anprojf=anproj2;
16375: } else if(prvforecast == 2){
16376: dateprojd=dateintmean;
16377: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
16378: dateprojf=dateintmean+yrfproj;
16379: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
16380: }
16381: if(prvbackcast==1){
16382: datebackd=(jback1+12*mback1+365*anback1)/365;
16383: jbackd=jback1;
16384: mbackd=mback1;
16385: anbackd=anback1;
16386: datebackf=(jback2+12*mback2+365*anback2)/365;
16387: jbackf=jback2;
16388: mbackf=mback2;
16389: anbackf=anback2;
16390: } else if(prvbackcast == 2){
16391: datebackd=dateintmean;
16392: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
16393: datebackf=dateintmean-yrbproj;
16394: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
16395: }
16396:
1.350 brouard 16397: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);/* HERE valgrind Tvard*/
1.220 brouard 16398: }
16399: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.296 brouard 16400: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
16401: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
1.220 brouard 16402:
1.225 brouard 16403: /*------------ free_vector -------------*/
16404: /* chdir(path); */
1.220 brouard 16405:
1.215 brouard 16406: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
16407: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
16408: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
16409: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.290 brouard 16410: free_lvector(num,firstobs,lastobs);
16411: free_vector(agedc,firstobs,lastobs);
1.126 brouard 16412: /*free_matrix(covar,0,NCOVMAX,1,n);*/
16413: /*free_matrix(covar,1,NCOVMAX,1,n);*/
16414: fclose(ficparo);
16415: fclose(ficres);
1.220 brouard 16416:
16417:
1.186 brouard 16418: /* Other results (useful)*/
1.220 brouard 16419:
16420:
1.126 brouard 16421: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 16422: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
16423: prlim=matrix(1,nlstate,1,nlstate);
1.332 brouard 16424: /* Computes the prevalence limit for each combination k of the dummy covariates by calling prevalim(k) */
1.209 brouard 16425: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 16426: fclose(ficrespl);
16427:
16428: /*------------- h Pij x at various ages ------------*/
1.180 brouard 16429: /*#include "hpijx.h"*/
1.332 brouard 16430: /** 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?*/
16431: /* calls hpxij with combination k */
1.180 brouard 16432: hPijx(p, bage, fage);
1.145 brouard 16433: fclose(ficrespij);
1.227 brouard 16434:
1.220 brouard 16435: /* ncovcombmax= pow(2,cptcoveff); */
1.332 brouard 16436: /*-------------- Variance of one-step probabilities for a combination ij or for nres ?---*/
1.145 brouard 16437: k=1;
1.126 brouard 16438: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 16439:
1.269 brouard 16440: /* Prevalence for each covariate combination in probs[age][status][cov] */
16441: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16442: for(i=AGEINF;i<=AGESUP;i++)
1.219 brouard 16443: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 16444: for(k=1;k<=ncovcombmax;k++)
16445: probs[i][j][k]=0.;
1.269 brouard 16446: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
16447: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.219 brouard 16448: if (mobilav!=0 ||mobilavproj !=0 ) {
1.269 brouard 16449: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
16450: for(i=AGEINF;i<=AGESUP;i++)
1.268 brouard 16451: for(j=1;j<=nlstate+ndeath;j++)
1.227 brouard 16452: for(k=1;k<=ncovcombmax;k++)
16453: mobaverages[i][j][k]=0.;
1.219 brouard 16454: mobaverage=mobaverages;
16455: if (mobilav!=0) {
1.235 brouard 16456: printf("Movingaveraging observed prevalence\n");
1.258 brouard 16457: fprintf(ficlog,"Movingaveraging observed prevalence\n");
1.227 brouard 16458: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
16459: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
16460: printf(" Error in movingaverage mobilav=%d\n",mobilav);
16461: }
1.269 brouard 16462: } else if (mobilavproj !=0) {
1.235 brouard 16463: printf("Movingaveraging projected observed prevalence\n");
1.258 brouard 16464: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
1.227 brouard 16465: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
16466: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
16467: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
16468: }
1.269 brouard 16469: }else{
16470: printf("Internal error moving average\n");
16471: fflush(stdout);
16472: exit(1);
1.219 brouard 16473: }
16474: }/* end if moving average */
1.227 brouard 16475:
1.126 brouard 16476: /*---------- Forecasting ------------------*/
1.296 brouard 16477: if(prevfcast==1){
16478: /* /\* if(stepm ==1){*\/ */
16479: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16480: /*This done previously after freqsummary.*/
16481: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
16482: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
16483:
16484: /* } else if (prvforecast==2){ */
16485: /* /\* if(stepm ==1){*\/ */
16486: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
16487: /* } */
16488: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
16489: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
1.126 brouard 16490: }
1.269 brouard 16491:
1.296 brouard 16492: /* Prevbcasting */
16493: if(prevbcast==1){
1.219 brouard 16494: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16495: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16496: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
16497:
16498: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
16499:
16500: bprlim=matrix(1,nlstate,1,nlstate);
1.269 brouard 16501:
1.219 brouard 16502: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
16503: fclose(ficresplb);
16504:
1.222 brouard 16505: hBijx(p, bage, fage, mobaverage);
16506: fclose(ficrespijb);
1.219 brouard 16507:
1.296 brouard 16508: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
16509: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
16510: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
16511: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
16512: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
16513: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
16514:
16515:
1.269 brouard 16516: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 16517:
16518:
1.269 brouard 16519: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.219 brouard 16520: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16521: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
16522: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
1.296 brouard 16523: } /* end Prevbcasting */
1.268 brouard 16524:
1.186 brouard 16525:
16526: /* ------ Other prevalence ratios------------ */
1.126 brouard 16527:
1.215 brouard 16528: free_ivector(wav,1,imx);
16529: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
16530: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
16531: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 16532:
16533:
1.127 brouard 16534: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 16535:
1.201 brouard 16536: strcpy(filerese,"E_");
16537: strcat(filerese,fileresu);
1.126 brouard 16538: if((ficreseij=fopen(filerese,"w"))==NULL) {
16539: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16540: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
16541: }
1.208 brouard 16542: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
16543: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 16544:
16545: pstamp(ficreseij);
1.219 brouard 16546:
1.351 brouard 16547: /* i1=pow(2,cptcoveff); /\* Number of combination of dummy covariates *\/ */
16548: /* if (cptcovn < 1){i1=1;} */
1.235 brouard 16549:
1.351 brouard 16550: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
16551: /* for(k=1; k<=i1;k++){ /\* For any combination of dummy covariates, fixed and varying *\/ */
16552: /* if(i1 != 1 && TKresult[nres]!= k) */
16553: /* continue; */
1.219 brouard 16554: fprintf(ficreseij,"\n#****** ");
1.235 brouard 16555: printf("\n#****** ");
1.351 brouard 16556: for(j=1;j<=cptcovs;j++){
16557: /* for(j=1;j<=cptcoveff;j++) { */
16558: /* fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16559: fprintf(ficreseij," V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16560: printf(" V%d=%lg ",Tvresult[nres][j],TinvDoQresult[nres][Tvresult[nres][j]]);
16561: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
1.235 brouard 16562: }
16563: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
1.337 brouard 16564: printf(" V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]); /* TvarsQ[j] gives the name of the jth quantitative (fixed or time v) */
16565: fprintf(ficreseij,"V%d=%lg ",TvarsQ[j], TinvDoQresult[nres][TvarsQ[j]]);
1.219 brouard 16566: }
16567: fprintf(ficreseij,"******\n");
1.235 brouard 16568: printf("******\n");
1.219 brouard 16569:
16570: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16571: oldm=oldms;savm=savms;
1.330 brouard 16572: /* 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 16573: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 16574:
1.219 brouard 16575: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 16576: }
16577: fclose(ficreseij);
1.208 brouard 16578: printf("done evsij\n");fflush(stdout);
16579: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.269 brouard 16580:
1.218 brouard 16581:
1.227 brouard 16582: /*---------- State-specific expectancies and variances ------------*/
1.336 brouard 16583: /* Should be moved in a function */
1.201 brouard 16584: strcpy(filerest,"T_");
16585: strcat(filerest,fileresu);
1.127 brouard 16586: if((ficrest=fopen(filerest,"w"))==NULL) {
16587: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
16588: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
16589: }
1.208 brouard 16590: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
16591: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.201 brouard 16592: strcpy(fileresstde,"STDE_");
16593: strcat(fileresstde,fileresu);
1.126 brouard 16594: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 16595: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
16596: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 16597: }
1.227 brouard 16598: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
16599: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 16600:
1.201 brouard 16601: strcpy(filerescve,"CVE_");
16602: strcat(filerescve,fileresu);
1.126 brouard 16603: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 16604: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
16605: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 16606: }
1.227 brouard 16607: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
16608: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 16609:
1.201 brouard 16610: strcpy(fileresv,"V_");
16611: strcat(fileresv,fileresu);
1.126 brouard 16612: if((ficresvij=fopen(fileresv,"w"))==NULL) {
16613: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
16614: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
16615: }
1.227 brouard 16616: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
16617: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 16618:
1.235 brouard 16619: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
16620: if (cptcovn < 1){i1=1;}
16621:
1.334 brouard 16622: for(nres=1; nres <= nresult; nres++) /* For each resultline, find the combination and output results according to the values of dummies and then quanti. */
16623: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying. For each nres and each value at position k
16624: * we know Tresult[nres][result_position]= value of the dummy variable at the result_position in the nres resultline
16625: * Tvqresult[nres][result_position]= id of the variable at the result_position in the nres resultline
16626: * and Tqresult[nres][result_position]= value of the variable at the result_position in the nres resultline */
16627: /* */
16628: 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 16629: continue;
1.359 ! brouard 16630: printf("\n# model=1+age+%s \n#****** Result for:", model); /* HERE model is empty */
! 16631: fprintf(ficrest,"\n# model=1+age+%s \n#****** Result for:", model);
! 16632: fprintf(ficlog,"\n# model=1+age+%s \n#****** Result for:", model);
1.334 brouard 16633: /* It might not be a good idea to mix dummies and quantitative */
16634: /* 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 *\/ */
16635: 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 */
16636: /* printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); /\* Output by variables in the resultline *\/ */
16637: /* Tvaraff[j] is the name of the dummy variable in position j in the equation model:
16638: * Tvaraff[1]@9={4, 3, 0, 0, 0, 0, 0, 0, 0}, in model=V5+V4+V3+V4*V3+V5*age
16639: * (V5 is quanti) V4 and V3 are dummies
16640: * TnsdVar[4] is the position 1 and TnsdVar[3]=2 in codtabm(k,l)(V4 V3)=V4 V3
16641: * l=1 l=2
16642: * k=1 1 1 0 0
16643: * k=2 2 1 1 0
16644: * k=3 [1] [2] 0 1
16645: * k=4 2 2 1 1
16646: * If nres=1 result: V3=1 V4=0 then k=3 and outputs
16647: * If nres=2 result: V4=1 V3=0 then k=2 and outputs
16648: * nres=1 =>k=3 j=1 V4= nbcode[4][codtabm(3,1)=1)=0; j=2 V3= nbcode[3][codtabm(3,2)=2]=1
16649: * nres=2 =>k=2 j=1 V4= nbcode[4][codtabm(2,1)=2)=1; j=2 V3= nbcode[3][codtabm(2,2)=1]=0
16650: */
16651: /* Tvresult[nres][j] Name of the variable at position j in this resultline */
16652: /* Tresult[nres][j] Value of this variable at position j could be a float if quantitative */
16653: /* We give up with the combinations!! */
1.342 brouard 16654: /* if(debugILK) */
16655: /* 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 16656:
16657: 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 16658: /* 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] */
16659: 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 */
16660: 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 */
16661: 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 16662: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16663: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16664: }else{
16665: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16666: }
16667: /* fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16668: /* fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16669: }else if(Dummy[modelresult[nres][j]]==1){ /* Quanti variable */
16670: /* For each selected (single) quantitative value */
1.337 brouard 16671: printf(" V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16672: fprintf(ficlog," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
16673: fprintf(ficrest," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][j]);
1.334 brouard 16674: if(Fixed[modelresult[nres][j]]==0){ /* Fixed */
16675: printf("fixed ");fprintf(ficlog,"fixed ");fprintf(ficrest,"fixed ");
16676: }else{
16677: printf("varyi ");fprintf(ficlog,"varyi ");fprintf(ficrest,"varyi ");
16678: }
16679: }else{
16680: 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 */
16681: 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 */
16682: exit(1);
16683: }
1.335 brouard 16684: } /* End loop for each variable in the resultline */
1.334 brouard 16685: /* for (j=1; j<= nsq; j++){ /\* For each selected (single) quantitative value *\/ */
16686: /* printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /\* Wrong j is not in the equation model *\/ */
16687: /* fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16688: /* fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); */
16689: /* } */
1.208 brouard 16690: fprintf(ficrest,"******\n");
1.227 brouard 16691: fprintf(ficlog,"******\n");
16692: printf("******\n");
1.208 brouard 16693:
16694: fprintf(ficresstdeij,"\n#****** ");
16695: fprintf(ficrescveij,"\n#****** ");
1.337 brouard 16696: /* It could have been: for(j=1;j<=cptcoveff;j++) {printf("V=%d=%lg",Tvresult[nres][cpt],TinvDoQresult[nres][Tvresult[nres][cpt]]);} */
16697: /* But it won't be sorted and depends on how the resultline is ordered */
1.225 brouard 16698: for(j=1;j<=cptcoveff;j++) {
1.334 brouard 16699: fprintf(ficresstdeij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16700: /* fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16701: /* fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[Tvaraff[j]])]); */
16702: }
16703: 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 16704: fprintf(ficresstdeij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
16705: fprintf(ficrescveij," V%d=%lg ",Tvar[TvarsQind[j]],Tqresult[nres][resultmodel[nres][TvarsQind[j]]]);
1.235 brouard 16706: }
1.208 brouard 16707: fprintf(ficresstdeij,"******\n");
16708: fprintf(ficrescveij,"******\n");
16709:
16710: fprintf(ficresvij,"\n#****** ");
1.238 brouard 16711: /* pstamp(ficresvij); */
1.225 brouard 16712: for(j=1;j<=cptcoveff;j++)
1.335 brouard 16713: fprintf(ficresvij,"V%d=%d ",Tvresult[nres][j],Tresult[nres][j]);
16714: /* fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,TnsdVar[TnsdVar[Tvaraff[j]]])]); */
1.235 brouard 16715: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
1.332 brouard 16716: /* fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); /\* To solve *\/ */
1.337 brouard 16717: fprintf(ficresvij," V%d=%lg ",Tvqresult[nres][j],Tqresult[nres][resultmodel[nres][j]]); /* Solved */
1.235 brouard 16718: }
1.208 brouard 16719: fprintf(ficresvij,"******\n");
16720:
16721: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16722: oldm=oldms;savm=savms;
1.235 brouard 16723: printf(" cvevsij ");
16724: fprintf(ficlog, " cvevsij ");
16725: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 16726: printf(" end cvevsij \n ");
16727: fprintf(ficlog, " end cvevsij \n ");
16728:
16729: /*
16730: */
16731: /* goto endfree; */
16732:
16733: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
16734: pstamp(ficrest);
16735:
1.269 brouard 16736: epj=vector(1,nlstate+1);
1.208 brouard 16737: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 16738: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
16739: cptcod= 0; /* To be deleted */
16740: printf("varevsij vpopbased=%d \n",vpopbased);
16741: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 16742: 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.227 brouard 16743: fprintf(ficrest,"# Total life expectancy with std error and decomposition into time to be expected in each health state\n# (weighted average of eij where weights are ");
16744: if(vpopbased==1)
16745: 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);
16746: else
1.288 brouard 16747: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
1.335 brouard 16748: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); /* Adding covariate values? */
1.227 brouard 16749: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
16750: fprintf(ficrest,"\n");
16751: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
1.288 brouard 16752: printf("Computing age specific forward period (stable) prevalences in each health state \n");
16753: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 16754: for(age=bage; age <=fage ;age++){
1.235 brouard 16755: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 16756: if (vpopbased==1) {
16757: if(mobilav ==0){
16758: for(i=1; i<=nlstate;i++)
16759: prlim[i][i]=probs[(int)age][i][k];
16760: }else{ /* mobilav */
16761: for(i=1; i<=nlstate;i++)
16762: prlim[i][i]=mobaverage[(int)age][i][k];
16763: }
16764: }
1.219 brouard 16765:
1.227 brouard 16766: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
16767: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
16768: /* printf(" age %4.0f ",age); */
16769: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
16770: for(i=1, epj[j]=0.;i <=nlstate;i++) {
16771: epj[j] += prlim[i][i]*eij[i][j][(int)age];
16772: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
16773: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
16774: }
16775: epj[nlstate+1] +=epj[j];
16776: }
16777: /* printf(" age %4.0f \n",age); */
1.219 brouard 16778:
1.227 brouard 16779: for(i=1, vepp=0.;i <=nlstate;i++)
16780: for(j=1;j <=nlstate;j++)
16781: vepp += vareij[i][j][(int)age];
16782: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
16783: for(j=1;j <=nlstate;j++){
16784: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
16785: }
16786: fprintf(ficrest,"\n");
16787: }
1.208 brouard 16788: } /* End vpopbased */
1.269 brouard 16789: free_vector(epj,1,nlstate+1);
1.208 brouard 16790: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
16791: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.235 brouard 16792: printf("done selection\n");fflush(stdout);
16793: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 16794:
1.335 brouard 16795: } /* End k selection or end covariate selection for nres */
1.227 brouard 16796:
16797: printf("done State-specific expectancies\n");fflush(stdout);
16798: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
16799:
1.335 brouard 16800: /* variance-covariance of forward period prevalence */
1.269 brouard 16801: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 16802:
1.227 brouard 16803:
1.290 brouard 16804: free_vector(weight,firstobs,lastobs);
1.351 brouard 16805: free_imatrix(Tvardk,0,NCOVMAX,1,2);
1.227 brouard 16806: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.290 brouard 16807: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
16808: free_matrix(anint,1,maxwav,firstobs,lastobs);
16809: free_matrix(mint,1,maxwav,firstobs,lastobs);
16810: free_ivector(cod,firstobs,lastobs);
1.227 brouard 16811: free_ivector(tab,1,NCOVMAX);
16812: fclose(ficresstdeij);
16813: fclose(ficrescveij);
16814: fclose(ficresvij);
16815: fclose(ficrest);
16816: fclose(ficpar);
16817:
16818:
1.126 brouard 16819: /*---------- End : free ----------------*/
1.219 brouard 16820: if (mobilav!=0 ||mobilavproj !=0)
1.269 brouard 16821: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
16822: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 16823: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
16824: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 16825: } /* mle==-3 arrives here for freeing */
1.227 brouard 16826: /* endfree:*/
1.359 ! brouard 16827: if(mle!=-3) free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /* Could be elsewhere ?*/
1.227 brouard 16828: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
16829: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
16830: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.341 brouard 16831: /* if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); */
16832: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,ncovcol+nqv+1,ncovcol+nqv+ntv+nqtv,firstobs,lastobs);
1.290 brouard 16833: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
16834: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
16835: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
1.227 brouard 16836: free_matrix(matcov,1,npar,1,npar);
16837: free_matrix(hess,1,npar,1,npar);
16838: /*free_vector(delti,1,npar);*/
16839: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16840: free_matrix(agev,1,maxwav,1,imx);
1.269 brouard 16841: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.227 brouard 16842: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
16843:
16844: free_ivector(ncodemax,1,NCOVMAX);
16845: free_ivector(ncodemaxwundef,1,NCOVMAX);
16846: free_ivector(Dummy,-1,NCOVMAX);
16847: free_ivector(Fixed,-1,NCOVMAX);
1.349 brouard 16848: free_ivector(DummyV,-1,NCOVMAX);
16849: free_ivector(FixedV,-1,NCOVMAX);
1.227 brouard 16850: free_ivector(Typevar,-1,NCOVMAX);
16851: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 16852: free_ivector(TvarsQ,1,NCOVMAX);
16853: free_ivector(TvarsQind,1,NCOVMAX);
16854: free_ivector(TvarsD,1,NCOVMAX);
1.330 brouard 16855: free_ivector(TnsdVar,1,NCOVMAX);
1.234 brouard 16856: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 16857: free_ivector(TvarFD,1,NCOVMAX);
16858: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 16859: free_ivector(TvarF,1,NCOVMAX);
16860: free_ivector(TvarFind,1,NCOVMAX);
16861: free_ivector(TvarV,1,NCOVMAX);
16862: free_ivector(TvarVind,1,NCOVMAX);
16863: free_ivector(TvarA,1,NCOVMAX);
16864: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 16865: free_ivector(TvarFQ,1,NCOVMAX);
16866: free_ivector(TvarFQind,1,NCOVMAX);
16867: free_ivector(TvarVD,1,NCOVMAX);
16868: free_ivector(TvarVDind,1,NCOVMAX);
16869: free_ivector(TvarVQ,1,NCOVMAX);
16870: free_ivector(TvarVQind,1,NCOVMAX);
1.349 brouard 16871: free_ivector(TvarAVVA,1,NCOVMAX);
16872: free_ivector(TvarAVVAind,1,NCOVMAX);
16873: free_ivector(TvarVVA,1,NCOVMAX);
16874: free_ivector(TvarVVAind,1,NCOVMAX);
1.339 brouard 16875: free_ivector(TvarVV,1,NCOVMAX);
16876: free_ivector(TvarVVind,1,NCOVMAX);
16877:
1.230 brouard 16878: free_ivector(Tvarsel,1,NCOVMAX);
16879: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 16880: free_ivector(Tposprod,1,NCOVMAX);
16881: free_ivector(Tprod,1,NCOVMAX);
16882: free_ivector(Tvaraff,1,NCOVMAX);
1.338 brouard 16883: free_ivector(invalidvarcomb,0,ncovcombmax);
1.227 brouard 16884: free_ivector(Tage,1,NCOVMAX);
16885: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 16886: free_ivector(TmodelInvind,1,NCOVMAX);
16887: free_ivector(TmodelInvQind,1,NCOVMAX);
1.332 brouard 16888:
1.359 ! brouard 16889: /* free_matrix(precov, 1,MAXRESULTLINESPONE,1,NCOVMAX+1); /\* Could be elsewhere ?*\/ */
1.332 brouard 16890:
1.227 brouard 16891: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
16892: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 16893: fflush(fichtm);
16894: fflush(ficgp);
16895:
1.227 brouard 16896:
1.126 brouard 16897: if((nberr >0) || (nbwarn>0)){
1.216 brouard 16898: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
16899: 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 16900: }else{
16901: printf("End of Imach\n");
16902: fprintf(ficlog,"End of Imach\n");
16903: }
16904: printf("See log file on %s\n",filelog);
16905: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 16906: /*(void) gettimeofday(&end_time,&tzp);*/
16907: rend_time = time(NULL);
16908: end_time = *localtime(&rend_time);
16909: /* tml = *localtime(&end_time.tm_sec); */
16910: strcpy(strtend,asctime(&end_time));
1.126 brouard 16911: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
16912: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 16913: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 16914:
1.157 brouard 16915: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
16916: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
16917: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 16918: /* printf("Total time was %d uSec.\n", total_usecs);*/
16919: /* if(fileappend(fichtm,optionfilehtm)){ */
16920: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
16921: fclose(fichtm);
16922: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
16923: fclose(fichtmcov);
16924: fclose(ficgp);
16925: fclose(ficlog);
16926: /*------ End -----------*/
1.227 brouard 16927:
1.281 brouard 16928:
16929: /* Executes gnuplot */
1.227 brouard 16930:
16931: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 16932: #ifdef WIN32
1.227 brouard 16933: if (_chdir(pathcd) != 0)
16934: printf("Can't move to directory %s!\n",path);
16935: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 16936: #else
1.227 brouard 16937: if(chdir(pathcd) != 0)
16938: printf("Can't move to directory %s!\n", path);
16939: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 16940: #endif
1.126 brouard 16941: printf("Current directory %s!\n",pathcd);
16942: /*strcat(plotcmd,CHARSEPARATOR);*/
16943: sprintf(plotcmd,"gnuplot");
1.157 brouard 16944: #ifdef _WIN32
1.126 brouard 16945: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
16946: #endif
16947: if(!stat(plotcmd,&info)){
1.158 brouard 16948: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 16949: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 16950: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 16951: }else
16952: strcpy(pplotcmd,plotcmd);
1.157 brouard 16953: #ifdef __unix
1.126 brouard 16954: strcpy(plotcmd,GNUPLOTPROGRAM);
16955: if(!stat(plotcmd,&info)){
1.158 brouard 16956: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 16957: }else
16958: strcpy(pplotcmd,plotcmd);
16959: #endif
16960: }else
16961: strcpy(pplotcmd,plotcmd);
16962:
16963: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 16964: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.292 brouard 16965: strcpy(pplotcmd,plotcmd);
1.227 brouard 16966:
1.126 brouard 16967: if((outcmd=system(plotcmd)) != 0){
1.292 brouard 16968: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 16969: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 16970: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.292 brouard 16971: if((outcmd=system(plotcmd)) != 0){
1.153 brouard 16972: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.292 brouard 16973: strcpy(plotcmd,pplotcmd);
16974: }
1.126 brouard 16975: }
1.158 brouard 16976: printf(" Successful, please wait...");
1.126 brouard 16977: while (z[0] != 'q') {
16978: /* chdir(path); */
1.154 brouard 16979: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 16980: scanf("%s",z);
16981: /* if (z[0] == 'c') system("./imach"); */
16982: if (z[0] == 'e') {
1.158 brouard 16983: #ifdef __APPLE__
1.152 brouard 16984: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 16985: #elif __linux
16986: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 16987: #else
1.152 brouard 16988: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 16989: #endif
16990: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
16991: system(pplotcmd);
1.126 brouard 16992: }
16993: else if (z[0] == 'g') system(plotcmd);
16994: else if (z[0] == 'q') exit(0);
16995: }
1.227 brouard 16996: end:
1.126 brouard 16997: while (z[0] != 'q') {
1.195 brouard 16998: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 16999: scanf("%s",z);
17000: }
1.283 brouard 17001: printf("End\n");
1.282 brouard 17002: exit(0);
1.126 brouard 17003: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>