Annotation of imach/src/imach.c, revision 1.326
1.326 ! brouard 1: /* $Id: imach.c,v 1.325 2022/07/25 14:27:23 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.326 ! brouard 4: Revision 1.325 2022/07/25 14:27:23 brouard
! 5: Summary: r30
! 6:
! 7: * imach.c (Module): Error cptcovn instead of nsd in bmij (was
! 8: coredumped, revealed by Feiuno, thank you.
! 9:
1.325 brouard 10: Revision 1.324 2022/07/23 17:44:26 brouard
11: *** empty log message ***
12:
1.324 brouard 13: Revision 1.323 2022/07/22 12:30:08 brouard
14: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
15:
1.323 brouard 16: Revision 1.322 2022/07/22 12:27:48 brouard
17: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
18:
1.322 brouard 19: Revision 1.321 2022/07/22 12:04:24 brouard
20: Summary: r28
21:
22: * imach.c (Module): Output of Wald test in the htm file and not only in the log.
23:
1.321 brouard 24: Revision 1.320 2022/06/02 05:10:11 brouard
25: *** empty log message ***
26:
1.320 brouard 27: Revision 1.319 2022/06/02 04:45:11 brouard
28: * imach.c (Module): Adding the Wald tests from the log to the main
29: htm for better display of the maximum likelihood estimators.
30:
1.319 brouard 31: Revision 1.318 2022/05/24 08:10:59 brouard
32: * imach.c (Module): Some attempts to find a bug of wrong estimates
33: of confidencce intervals with product in the equation modelC
34:
1.318 brouard 35: Revision 1.317 2022/05/15 15:06:23 brouard
36: * imach.c (Module): Some minor improvements
37:
1.317 brouard 38: Revision 1.316 2022/05/11 15:11:31 brouard
39: Summary: r27
40:
1.316 brouard 41: Revision 1.315 2022/05/11 15:06:32 brouard
42: *** empty log message ***
43:
1.315 brouard 44: Revision 1.314 2022/04/13 17:43:09 brouard
45: * imach.c (Module): Adding link to text data files
46:
1.314 brouard 47: Revision 1.313 2022/04/11 15:57:42 brouard
48: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
49:
1.313 brouard 50: Revision 1.312 2022/04/05 21:24:39 brouard
51: *** empty log message ***
52:
1.312 brouard 53: Revision 1.311 2022/04/05 21:03:51 brouard
54: Summary: Fixed quantitative covariates
55:
56: Fixed covariates (dummy or quantitative)
57: with missing values have never been allowed but are ERRORS and
58: program quits. Standard deviations of fixed covariates were
59: wrongly computed. Mean and standard deviations of time varying
60: covariates are still not computed.
61:
1.311 brouard 62: Revision 1.310 2022/03/17 08:45:53 brouard
63: Summary: 99r25
64:
65: Improving detection of errors: result lines should be compatible with
66: the model.
67:
1.310 brouard 68: Revision 1.309 2021/05/20 12:39:14 brouard
69: Summary: Version 0.99r24
70:
1.309 brouard 71: Revision 1.308 2021/03/31 13:11:57 brouard
72: Summary: Version 0.99r23
73:
74:
75: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
76:
1.308 brouard 77: Revision 1.307 2021/03/08 18:11:32 brouard
78: Summary: 0.99r22 fixed bug on result:
79:
1.307 brouard 80: Revision 1.306 2021/02/20 15:44:02 brouard
81: Summary: Version 0.99r21
82:
83: * imach.c (Module): Fix bug on quitting after result lines!
84: (Module): Version 0.99r21
85:
1.306 brouard 86: Revision 1.305 2021/02/20 15:28:30 brouard
87: * imach.c (Module): Fix bug on quitting after result lines!
88:
1.305 brouard 89: Revision 1.304 2021/02/12 11:34:20 brouard
90: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
91:
1.304 brouard 92: Revision 1.303 2021/02/11 19:50:15 brouard
93: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
94:
1.303 brouard 95: Revision 1.302 2020/02/22 21:00:05 brouard
96: * (Module): imach.c Update mle=-3 (for computing Life expectancy
97: and life table from the data without any state)
98:
1.302 brouard 99: Revision 1.301 2019/06/04 13:51:20 brouard
100: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
101:
1.301 brouard 102: Revision 1.300 2019/05/22 19:09:45 brouard
103: Summary: version 0.99r19 of May 2019
104:
1.300 brouard 105: Revision 1.299 2019/05/22 18:37:08 brouard
106: Summary: Cleaned 0.99r19
107:
1.299 brouard 108: Revision 1.298 2019/05/22 18:19:56 brouard
109: *** empty log message ***
110:
1.298 brouard 111: Revision 1.297 2019/05/22 17:56:10 brouard
112: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
113:
1.297 brouard 114: Revision 1.296 2019/05/20 13:03:18 brouard
115: Summary: Projection syntax simplified
116:
117:
118: We can now start projections, forward or backward, from the mean date
119: of inteviews up to or down to a number of years of projection:
120: prevforecast=1 yearsfproj=15.3 mobil_average=0
121: or
122: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
123: or
124: prevbackcast=1 yearsbproj=12.3 mobil_average=1
125: or
126: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
127:
1.296 brouard 128: Revision 1.295 2019/05/18 09:52:50 brouard
129: Summary: doxygen tex bug
130:
1.295 brouard 131: Revision 1.294 2019/05/16 14:54:33 brouard
132: Summary: There was some wrong lines added
133:
1.294 brouard 134: Revision 1.293 2019/05/09 15:17:34 brouard
135: *** empty log message ***
136:
1.293 brouard 137: Revision 1.292 2019/05/09 14:17:20 brouard
138: Summary: Some updates
139:
1.292 brouard 140: Revision 1.291 2019/05/09 13:44:18 brouard
141: Summary: Before ncovmax
142:
1.291 brouard 143: Revision 1.290 2019/05/09 13:39:37 brouard
144: Summary: 0.99r18 unlimited number of individuals
145:
146: 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.
147:
1.290 brouard 148: Revision 1.289 2018/12/13 09:16:26 brouard
149: Summary: Bug for young ages (<-30) will be in r17
150:
1.289 brouard 151: Revision 1.288 2018/05/02 20:58:27 brouard
152: Summary: Some bugs fixed
153:
1.288 brouard 154: Revision 1.287 2018/05/01 17:57:25 brouard
155: Summary: Bug fixed by providing frequencies only for non missing covariates
156:
1.287 brouard 157: Revision 1.286 2018/04/27 14:27:04 brouard
158: Summary: some minor bugs
159:
1.286 brouard 160: Revision 1.285 2018/04/21 21:02:16 brouard
161: Summary: Some bugs fixed, valgrind tested
162:
1.285 brouard 163: Revision 1.284 2018/04/20 05:22:13 brouard
164: Summary: Computing mean and stdeviation of fixed quantitative variables
165:
1.284 brouard 166: Revision 1.283 2018/04/19 14:49:16 brouard
167: Summary: Some minor bugs fixed
168:
1.283 brouard 169: Revision 1.282 2018/02/27 22:50:02 brouard
170: *** empty log message ***
171:
1.282 brouard 172: Revision 1.281 2018/02/27 19:25:23 brouard
173: Summary: Adding second argument for quitting
174:
1.281 brouard 175: Revision 1.280 2018/02/21 07:58:13 brouard
176: Summary: 0.99r15
177:
178: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
179:
1.280 brouard 180: Revision 1.279 2017/07/20 13:35:01 brouard
181: Summary: temporary working
182:
1.279 brouard 183: Revision 1.278 2017/07/19 14:09:02 brouard
184: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
185:
1.278 brouard 186: Revision 1.277 2017/07/17 08:53:49 brouard
187: Summary: BOM files can be read now
188:
1.277 brouard 189: Revision 1.276 2017/06/30 15:48:31 brouard
190: Summary: Graphs improvements
191:
1.276 brouard 192: Revision 1.275 2017/06/30 13:39:33 brouard
193: Summary: Saito's color
194:
1.275 brouard 195: Revision 1.274 2017/06/29 09:47:08 brouard
196: Summary: Version 0.99r14
197:
1.274 brouard 198: Revision 1.273 2017/06/27 11:06:02 brouard
199: Summary: More documentation on projections
200:
1.273 brouard 201: Revision 1.272 2017/06/27 10:22:40 brouard
202: Summary: Color of backprojection changed from 6 to 5(yellow)
203:
1.272 brouard 204: Revision 1.271 2017/06/27 10:17:50 brouard
205: Summary: Some bug with rint
206:
1.271 brouard 207: Revision 1.270 2017/05/24 05:45:29 brouard
208: *** empty log message ***
209:
1.270 brouard 210: Revision 1.269 2017/05/23 08:39:25 brouard
211: Summary: Code into subroutine, cleanings
212:
1.269 brouard 213: Revision 1.268 2017/05/18 20:09:32 brouard
214: Summary: backprojection and confidence intervals of backprevalence
215:
1.268 brouard 216: Revision 1.267 2017/05/13 10:25:05 brouard
217: Summary: temporary save for backprojection
218:
1.267 brouard 219: Revision 1.266 2017/05/13 07:26:12 brouard
220: Summary: Version 0.99r13 (improvements and bugs fixed)
221:
1.266 brouard 222: Revision 1.265 2017/04/26 16:22:11 brouard
223: Summary: imach 0.99r13 Some bugs fixed
224:
1.265 brouard 225: Revision 1.264 2017/04/26 06:01:29 brouard
226: Summary: Labels in graphs
227:
1.264 brouard 228: Revision 1.263 2017/04/24 15:23:15 brouard
229: Summary: to save
230:
1.263 brouard 231: Revision 1.262 2017/04/18 16:48:12 brouard
232: *** empty log message ***
233:
1.262 brouard 234: Revision 1.261 2017/04/05 10:14:09 brouard
235: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
236:
1.261 brouard 237: Revision 1.260 2017/04/04 17:46:59 brouard
238: Summary: Gnuplot indexations fixed (humm)
239:
1.260 brouard 240: Revision 1.259 2017/04/04 13:01:16 brouard
241: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
242:
1.259 brouard 243: Revision 1.258 2017/04/03 10:17:47 brouard
244: Summary: Version 0.99r12
245:
246: Some cleanings, conformed with updated documentation.
247:
1.258 brouard 248: Revision 1.257 2017/03/29 16:53:30 brouard
249: Summary: Temp
250:
1.257 brouard 251: Revision 1.256 2017/03/27 05:50:23 brouard
252: Summary: Temporary
253:
1.256 brouard 254: Revision 1.255 2017/03/08 16:02:28 brouard
255: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
256:
1.255 brouard 257: Revision 1.254 2017/03/08 07:13:00 brouard
258: Summary: Fixing data parameter line
259:
1.254 brouard 260: Revision 1.253 2016/12/15 11:59:41 brouard
261: Summary: 0.99 in progress
262:
1.253 brouard 263: Revision 1.252 2016/09/15 21:15:37 brouard
264: *** empty log message ***
265:
1.252 brouard 266: Revision 1.251 2016/09/15 15:01:13 brouard
267: Summary: not working
268:
1.251 brouard 269: Revision 1.250 2016/09/08 16:07:27 brouard
270: Summary: continue
271:
1.250 brouard 272: Revision 1.249 2016/09/07 17:14:18 brouard
273: Summary: Starting values from frequencies
274:
1.249 brouard 275: Revision 1.248 2016/09/07 14:10:18 brouard
276: *** empty log message ***
277:
1.248 brouard 278: Revision 1.247 2016/09/02 11:11:21 brouard
279: *** empty log message ***
280:
1.247 brouard 281: Revision 1.246 2016/09/02 08:49:22 brouard
282: *** empty log message ***
283:
1.246 brouard 284: Revision 1.245 2016/09/02 07:25:01 brouard
285: *** empty log message ***
286:
1.245 brouard 287: Revision 1.244 2016/09/02 07:17:34 brouard
288: *** empty log message ***
289:
1.244 brouard 290: Revision 1.243 2016/09/02 06:45:35 brouard
291: *** empty log message ***
292:
1.243 brouard 293: Revision 1.242 2016/08/30 15:01:20 brouard
294: Summary: Fixing a lots
295:
1.242 brouard 296: Revision 1.241 2016/08/29 17:17:25 brouard
297: Summary: gnuplot problem in Back projection to fix
298:
1.241 brouard 299: Revision 1.240 2016/08/29 07:53:18 brouard
300: Summary: Better
301:
1.240 brouard 302: Revision 1.239 2016/08/26 15:51:03 brouard
303: Summary: Improvement in Powell output in order to copy and paste
304:
305: Author:
306:
1.239 brouard 307: Revision 1.238 2016/08/26 14:23:35 brouard
308: Summary: Starting tests of 0.99
309:
1.238 brouard 310: Revision 1.237 2016/08/26 09:20:19 brouard
311: Summary: to valgrind
312:
1.237 brouard 313: Revision 1.236 2016/08/25 10:50:18 brouard
314: *** empty log message ***
315:
1.236 brouard 316: Revision 1.235 2016/08/25 06:59:23 brouard
317: *** empty log message ***
318:
1.235 brouard 319: Revision 1.234 2016/08/23 16:51:20 brouard
320: *** empty log message ***
321:
1.234 brouard 322: Revision 1.233 2016/08/23 07:40:50 brouard
323: Summary: not working
324:
1.233 brouard 325: Revision 1.232 2016/08/22 14:20:21 brouard
326: Summary: not working
327:
1.232 brouard 328: Revision 1.231 2016/08/22 07:17:15 brouard
329: Summary: not working
330:
1.231 brouard 331: Revision 1.230 2016/08/22 06:55:53 brouard
332: Summary: Not working
333:
1.230 brouard 334: Revision 1.229 2016/07/23 09:45:53 brouard
335: Summary: Completing for func too
336:
1.229 brouard 337: Revision 1.228 2016/07/22 17:45:30 brouard
338: Summary: Fixing some arrays, still debugging
339:
1.227 brouard 340: Revision 1.226 2016/07/12 18:42:34 brouard
341: Summary: temp
342:
1.226 brouard 343: Revision 1.225 2016/07/12 08:40:03 brouard
344: Summary: saving but not running
345:
1.225 brouard 346: Revision 1.224 2016/07/01 13:16:01 brouard
347: Summary: Fixes
348:
1.224 brouard 349: Revision 1.223 2016/02/19 09:23:35 brouard
350: Summary: temporary
351:
1.223 brouard 352: Revision 1.222 2016/02/17 08:14:50 brouard
353: Summary: Probably last 0.98 stable version 0.98r6
354:
1.222 brouard 355: Revision 1.221 2016/02/15 23:35:36 brouard
356: Summary: minor bug
357:
1.220 brouard 358: Revision 1.219 2016/02/15 00:48:12 brouard
359: *** empty log message ***
360:
1.219 brouard 361: Revision 1.218 2016/02/12 11:29:23 brouard
362: Summary: 0.99 Back projections
363:
1.218 brouard 364: Revision 1.217 2015/12/23 17:18:31 brouard
365: Summary: Experimental backcast
366:
1.217 brouard 367: Revision 1.216 2015/12/18 17:32:11 brouard
368: Summary: 0.98r4 Warning and status=-2
369:
370: Version 0.98r4 is now:
371: - displaying an error when status is -1, date of interview unknown and date of death known;
372: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
373: Older changes concerning s=-2, dating from 2005 have been supersed.
374:
1.216 brouard 375: Revision 1.215 2015/12/16 08:52:24 brouard
376: Summary: 0.98r4 working
377:
1.215 brouard 378: Revision 1.214 2015/12/16 06:57:54 brouard
379: Summary: temporary not working
380:
1.214 brouard 381: Revision 1.213 2015/12/11 18:22:17 brouard
382: Summary: 0.98r4
383:
1.213 brouard 384: Revision 1.212 2015/11/21 12:47:24 brouard
385: Summary: minor typo
386:
1.212 brouard 387: Revision 1.211 2015/11/21 12:41:11 brouard
388: Summary: 0.98r3 with some graph of projected cross-sectional
389:
390: Author: Nicolas Brouard
391:
1.211 brouard 392: Revision 1.210 2015/11/18 17:41:20 brouard
1.252 brouard 393: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
1.210 brouard 394: Summary: Adding ftolpl parameter
395: Author: N Brouard
396:
397: We had difficulties to get smoothed confidence intervals. It was due
398: to the period prevalence which wasn't computed accurately. The inner
399: parameter ftolpl is now an outer parameter of the .imach parameter
400: file after estepm. If ftolpl is small 1.e-4 and estepm too,
401: computation are long.
402:
1.209 brouard 403: Revision 1.208 2015/11/17 14:31:57 brouard
404: Summary: temporary
405:
1.208 brouard 406: Revision 1.207 2015/10/27 17:36:57 brouard
407: *** empty log message ***
408:
1.207 brouard 409: Revision 1.206 2015/10/24 07:14:11 brouard
410: *** empty log message ***
411:
1.206 brouard 412: Revision 1.205 2015/10/23 15:50:53 brouard
413: Summary: 0.98r3 some clarification for graphs on likelihood contributions
414:
1.205 brouard 415: Revision 1.204 2015/10/01 16:20:26 brouard
416: Summary: Some new graphs of contribution to likelihood
417:
1.204 brouard 418: Revision 1.203 2015/09/30 17:45:14 brouard
419: Summary: looking at better estimation of the hessian
420:
421: Also a better criteria for convergence to the period prevalence And
422: therefore adding the number of years needed to converge. (The
423: prevalence in any alive state shold sum to one
424:
1.203 brouard 425: Revision 1.202 2015/09/22 19:45:16 brouard
426: Summary: Adding some overall graph on contribution to likelihood. Might change
427:
1.202 brouard 428: Revision 1.201 2015/09/15 17:34:58 brouard
429: Summary: 0.98r0
430:
431: - Some new graphs like suvival functions
432: - Some bugs fixed like model=1+age+V2.
433:
1.201 brouard 434: Revision 1.200 2015/09/09 16:53:55 brouard
435: Summary: Big bug thanks to Flavia
436:
437: Even model=1+age+V2. did not work anymore
438:
1.200 brouard 439: Revision 1.199 2015/09/07 14:09:23 brouard
440: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
441:
1.199 brouard 442: Revision 1.198 2015/09/03 07:14:39 brouard
443: Summary: 0.98q5 Flavia
444:
1.198 brouard 445: Revision 1.197 2015/09/01 18:24:39 brouard
446: *** empty log message ***
447:
1.197 brouard 448: Revision 1.196 2015/08/18 23:17:52 brouard
449: Summary: 0.98q5
450:
1.196 brouard 451: Revision 1.195 2015/08/18 16:28:39 brouard
452: Summary: Adding a hack for testing purpose
453:
454: After reading the title, ftol and model lines, if the comment line has
455: a q, starting with #q, the answer at the end of the run is quit. It
456: permits to run test files in batch with ctest. The former workaround was
457: $ echo q | imach foo.imach
458:
1.195 brouard 459: Revision 1.194 2015/08/18 13:32:00 brouard
460: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
461:
1.194 brouard 462: Revision 1.193 2015/08/04 07:17:42 brouard
463: Summary: 0.98q4
464:
1.193 brouard 465: Revision 1.192 2015/07/16 16:49:02 brouard
466: Summary: Fixing some outputs
467:
1.192 brouard 468: Revision 1.191 2015/07/14 10:00:33 brouard
469: Summary: Some fixes
470:
1.191 brouard 471: Revision 1.190 2015/05/05 08:51:13 brouard
472: Summary: Adding digits in output parameters (7 digits instead of 6)
473:
474: Fix 1+age+.
475:
1.190 brouard 476: Revision 1.189 2015/04/30 14:45:16 brouard
477: Summary: 0.98q2
478:
1.189 brouard 479: Revision 1.188 2015/04/30 08:27:53 brouard
480: *** empty log message ***
481:
1.188 brouard 482: Revision 1.187 2015/04/29 09:11:15 brouard
483: *** empty log message ***
484:
1.187 brouard 485: Revision 1.186 2015/04/23 12:01:52 brouard
486: Summary: V1*age is working now, version 0.98q1
487:
488: Some codes had been disabled in order to simplify and Vn*age was
489: working in the optimization phase, ie, giving correct MLE parameters,
490: but, as usual, outputs were not correct and program core dumped.
491:
1.186 brouard 492: Revision 1.185 2015/03/11 13:26:42 brouard
493: Summary: Inclusion of compile and links command line for Intel Compiler
494:
1.185 brouard 495: Revision 1.184 2015/03/11 11:52:39 brouard
496: Summary: Back from Windows 8. Intel Compiler
497:
1.184 brouard 498: Revision 1.183 2015/03/10 20:34:32 brouard
499: Summary: 0.98q0, trying with directest, mnbrak fixed
500:
501: We use directest instead of original Powell test; probably no
502: incidence on the results, but better justifications;
503: We fixed Numerical Recipes mnbrak routine which was wrong and gave
504: wrong results.
505:
1.183 brouard 506: Revision 1.182 2015/02/12 08:19:57 brouard
507: Summary: Trying to keep directest which seems simpler and more general
508: Author: Nicolas Brouard
509:
1.182 brouard 510: Revision 1.181 2015/02/11 23:22:24 brouard
511: Summary: Comments on Powell added
512:
513: Author:
514:
1.181 brouard 515: Revision 1.180 2015/02/11 17:33:45 brouard
516: Summary: Finishing move from main to function (hpijx and prevalence_limit)
517:
1.180 brouard 518: Revision 1.179 2015/01/04 09:57:06 brouard
519: Summary: back to OS/X
520:
1.179 brouard 521: Revision 1.178 2015/01/04 09:35:48 brouard
522: *** empty log message ***
523:
1.178 brouard 524: Revision 1.177 2015/01/03 18:40:56 brouard
525: Summary: Still testing ilc32 on OSX
526:
1.177 brouard 527: Revision 1.176 2015/01/03 16:45:04 brouard
528: *** empty log message ***
529:
1.176 brouard 530: Revision 1.175 2015/01/03 16:33:42 brouard
531: *** empty log message ***
532:
1.175 brouard 533: Revision 1.174 2015/01/03 16:15:49 brouard
534: Summary: Still in cross-compilation
535:
1.174 brouard 536: Revision 1.173 2015/01/03 12:06:26 brouard
537: Summary: trying to detect cross-compilation
538:
1.173 brouard 539: Revision 1.172 2014/12/27 12:07:47 brouard
540: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
541:
1.172 brouard 542: Revision 1.171 2014/12/23 13:26:59 brouard
543: Summary: Back from Visual C
544:
545: Still problem with utsname.h on Windows
546:
1.171 brouard 547: Revision 1.170 2014/12/23 11:17:12 brouard
548: Summary: Cleaning some \%% back to %%
549:
550: The escape was mandatory for a specific compiler (which one?), but too many warnings.
551:
1.170 brouard 552: Revision 1.169 2014/12/22 23:08:31 brouard
553: Summary: 0.98p
554:
555: Outputs some informations on compiler used, OS etc. Testing on different platforms.
556:
1.169 brouard 557: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 558: Summary: update
1.169 brouard 559:
1.168 brouard 560: Revision 1.167 2014/12/22 13:50:56 brouard
561: Summary: Testing uname and compiler version and if compiled 32 or 64
562:
563: Testing on Linux 64
564:
1.167 brouard 565: Revision 1.166 2014/12/22 11:40:47 brouard
566: *** empty log message ***
567:
1.166 brouard 568: Revision 1.165 2014/12/16 11:20:36 brouard
569: Summary: After compiling on Visual C
570:
571: * imach.c (Module): Merging 1.61 to 1.162
572:
1.165 brouard 573: Revision 1.164 2014/12/16 10:52:11 brouard
574: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
575:
576: * imach.c (Module): Merging 1.61 to 1.162
577:
1.164 brouard 578: Revision 1.163 2014/12/16 10:30:11 brouard
579: * imach.c (Module): Merging 1.61 to 1.162
580:
1.163 brouard 581: Revision 1.162 2014/09/25 11:43:39 brouard
582: Summary: temporary backup 0.99!
583:
1.162 brouard 584: Revision 1.1 2014/09/16 11:06:58 brouard
585: Summary: With some code (wrong) for nlopt
586:
587: Author:
588:
589: Revision 1.161 2014/09/15 20:41:41 brouard
590: Summary: Problem with macro SQR on Intel compiler
591:
1.161 brouard 592: Revision 1.160 2014/09/02 09:24:05 brouard
593: *** empty log message ***
594:
1.160 brouard 595: Revision 1.159 2014/09/01 10:34:10 brouard
596: Summary: WIN32
597: Author: Brouard
598:
1.159 brouard 599: Revision 1.158 2014/08/27 17:11:51 brouard
600: *** empty log message ***
601:
1.158 brouard 602: Revision 1.157 2014/08/27 16:26:55 brouard
603: Summary: Preparing windows Visual studio version
604: Author: Brouard
605:
606: In order to compile on Visual studio, time.h is now correct and time_t
607: and tm struct should be used. difftime should be used but sometimes I
608: just make the differences in raw time format (time(&now).
609: Trying to suppress #ifdef LINUX
610: Add xdg-open for __linux in order to open default browser.
611:
1.157 brouard 612: Revision 1.156 2014/08/25 20:10:10 brouard
613: *** empty log message ***
614:
1.156 brouard 615: Revision 1.155 2014/08/25 18:32:34 brouard
616: Summary: New compile, minor changes
617: Author: Brouard
618:
1.155 brouard 619: Revision 1.154 2014/06/20 17:32:08 brouard
620: Summary: Outputs now all graphs of convergence to period prevalence
621:
1.154 brouard 622: Revision 1.153 2014/06/20 16:45:46 brouard
623: Summary: If 3 live state, convergence to period prevalence on same graph
624: Author: Brouard
625:
1.153 brouard 626: Revision 1.152 2014/06/18 17:54:09 brouard
627: Summary: open browser, use gnuplot on same dir than imach if not found in the path
628:
1.152 brouard 629: Revision 1.151 2014/06/18 16:43:30 brouard
630: *** empty log message ***
631:
1.151 brouard 632: Revision 1.150 2014/06/18 16:42:35 brouard
633: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
634: Author: brouard
635:
1.150 brouard 636: Revision 1.149 2014/06/18 15:51:14 brouard
637: Summary: Some fixes in parameter files errors
638: Author: Nicolas Brouard
639:
1.149 brouard 640: Revision 1.148 2014/06/17 17:38:48 brouard
641: Summary: Nothing new
642: Author: Brouard
643:
644: Just a new packaging for OS/X version 0.98nS
645:
1.148 brouard 646: Revision 1.147 2014/06/16 10:33:11 brouard
647: *** empty log message ***
648:
1.147 brouard 649: Revision 1.146 2014/06/16 10:20:28 brouard
650: Summary: Merge
651: Author: Brouard
652:
653: Merge, before building revised version.
654:
1.146 brouard 655: Revision 1.145 2014/06/10 21:23:15 brouard
656: Summary: Debugging with valgrind
657: Author: Nicolas Brouard
658:
659: Lot of changes in order to output the results with some covariates
660: After the Edimburgh REVES conference 2014, it seems mandatory to
661: improve the code.
662: No more memory valgrind error but a lot has to be done in order to
663: continue the work of splitting the code into subroutines.
664: Also, decodemodel has been improved. Tricode is still not
665: optimal. nbcode should be improved. Documentation has been added in
666: the source code.
667:
1.144 brouard 668: Revision 1.143 2014/01/26 09:45:38 brouard
669: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
670:
671: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
672: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
673:
1.143 brouard 674: Revision 1.142 2014/01/26 03:57:36 brouard
675: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
676:
677: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
678:
1.142 brouard 679: Revision 1.141 2014/01/26 02:42:01 brouard
680: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
681:
1.141 brouard 682: Revision 1.140 2011/09/02 10:37:54 brouard
683: Summary: times.h is ok with mingw32 now.
684:
1.140 brouard 685: Revision 1.139 2010/06/14 07:50:17 brouard
686: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
687: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
688:
1.139 brouard 689: Revision 1.138 2010/04/30 18:19:40 brouard
690: *** empty log message ***
691:
1.138 brouard 692: Revision 1.137 2010/04/29 18:11:38 brouard
693: (Module): Checking covariates for more complex models
694: than V1+V2. A lot of change to be done. Unstable.
695:
1.137 brouard 696: Revision 1.136 2010/04/26 20:30:53 brouard
697: (Module): merging some libgsl code. Fixing computation
698: of likelione (using inter/intrapolation if mle = 0) in order to
699: get same likelihood as if mle=1.
700: Some cleaning of code and comments added.
701:
1.136 brouard 702: Revision 1.135 2009/10/29 15:33:14 brouard
703: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
704:
1.135 brouard 705: Revision 1.134 2009/10/29 13:18:53 brouard
706: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
707:
1.134 brouard 708: Revision 1.133 2009/07/06 10:21:25 brouard
709: just nforces
710:
1.133 brouard 711: Revision 1.132 2009/07/06 08:22:05 brouard
712: Many tings
713:
1.132 brouard 714: Revision 1.131 2009/06/20 16:22:47 brouard
715: Some dimensions resccaled
716:
1.131 brouard 717: Revision 1.130 2009/05/26 06:44:34 brouard
718: (Module): Max Covariate is now set to 20 instead of 8. A
719: lot of cleaning with variables initialized to 0. Trying to make
720: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
721:
1.130 brouard 722: Revision 1.129 2007/08/31 13:49:27 lievre
723: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
724:
1.129 lievre 725: Revision 1.128 2006/06/30 13:02:05 brouard
726: (Module): Clarifications on computing e.j
727:
1.128 brouard 728: Revision 1.127 2006/04/28 18:11:50 brouard
729: (Module): Yes the sum of survivors was wrong since
730: imach-114 because nhstepm was no more computed in the age
731: loop. Now we define nhstepma in the age loop.
732: (Module): In order to speed up (in case of numerous covariates) we
733: compute health expectancies (without variances) in a first step
734: and then all the health expectancies with variances or standard
735: deviation (needs data from the Hessian matrices) which slows the
736: computation.
737: In the future we should be able to stop the program is only health
738: expectancies and graph are needed without standard deviations.
739:
1.127 brouard 740: Revision 1.126 2006/04/28 17:23:28 brouard
741: (Module): Yes the sum of survivors was wrong since
742: imach-114 because nhstepm was no more computed in the age
743: loop. Now we define nhstepma in the age loop.
744: Version 0.98h
745:
1.126 brouard 746: Revision 1.125 2006/04/04 15:20:31 lievre
747: Errors in calculation of health expectancies. Age was not initialized.
748: Forecasting file added.
749:
750: Revision 1.124 2006/03/22 17:13:53 lievre
751: Parameters are printed with %lf instead of %f (more numbers after the comma).
752: The log-likelihood is printed in the log file
753:
754: Revision 1.123 2006/03/20 10:52:43 brouard
755: * imach.c (Module): <title> changed, corresponds to .htm file
756: name. <head> headers where missing.
757:
758: * imach.c (Module): Weights can have a decimal point as for
759: English (a comma might work with a correct LC_NUMERIC environment,
760: otherwise the weight is truncated).
761: Modification of warning when the covariates values are not 0 or
762: 1.
763: Version 0.98g
764:
765: Revision 1.122 2006/03/20 09:45:41 brouard
766: (Module): Weights can have a decimal point as for
767: English (a comma might work with a correct LC_NUMERIC environment,
768: otherwise the weight is truncated).
769: Modification of warning when the covariates values are not 0 or
770: 1.
771: Version 0.98g
772:
773: Revision 1.121 2006/03/16 17:45:01 lievre
774: * imach.c (Module): Comments concerning covariates added
775:
776: * imach.c (Module): refinements in the computation of lli if
777: status=-2 in order to have more reliable computation if stepm is
778: not 1 month. Version 0.98f
779:
780: Revision 1.120 2006/03/16 15:10:38 lievre
781: (Module): refinements in the computation of lli if
782: status=-2 in order to have more reliable computation if stepm is
783: not 1 month. Version 0.98f
784:
785: Revision 1.119 2006/03/15 17:42:26 brouard
786: (Module): Bug if status = -2, the loglikelihood was
787: computed as likelihood omitting the logarithm. Version O.98e
788:
789: Revision 1.118 2006/03/14 18:20:07 brouard
790: (Module): varevsij Comments added explaining the second
791: table of variances if popbased=1 .
792: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
793: (Module): Function pstamp added
794: (Module): Version 0.98d
795:
796: Revision 1.117 2006/03/14 17:16:22 brouard
797: (Module): varevsij Comments added explaining the second
798: table of variances if popbased=1 .
799: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
800: (Module): Function pstamp added
801: (Module): Version 0.98d
802:
803: Revision 1.116 2006/03/06 10:29:27 brouard
804: (Module): Variance-covariance wrong links and
805: varian-covariance of ej. is needed (Saito).
806:
807: Revision 1.115 2006/02/27 12:17:45 brouard
808: (Module): One freematrix added in mlikeli! 0.98c
809:
810: Revision 1.114 2006/02/26 12:57:58 brouard
811: (Module): Some improvements in processing parameter
812: filename with strsep.
813:
814: Revision 1.113 2006/02/24 14:20:24 brouard
815: (Module): Memory leaks checks with valgrind and:
816: datafile was not closed, some imatrix were not freed and on matrix
817: allocation too.
818:
819: Revision 1.112 2006/01/30 09:55:26 brouard
820: (Module): Back to gnuplot.exe instead of wgnuplot.exe
821:
822: Revision 1.111 2006/01/25 20:38:18 brouard
823: (Module): Lots of cleaning and bugs added (Gompertz)
824: (Module): Comments can be added in data file. Missing date values
825: can be a simple dot '.'.
826:
827: Revision 1.110 2006/01/25 00:51:50 brouard
828: (Module): Lots of cleaning and bugs added (Gompertz)
829:
830: Revision 1.109 2006/01/24 19:37:15 brouard
831: (Module): Comments (lines starting with a #) are allowed in data.
832:
833: Revision 1.108 2006/01/19 18:05:42 lievre
834: Gnuplot problem appeared...
835: To be fixed
836:
837: Revision 1.107 2006/01/19 16:20:37 brouard
838: Test existence of gnuplot in imach path
839:
840: Revision 1.106 2006/01/19 13:24:36 brouard
841: Some cleaning and links added in html output
842:
843: Revision 1.105 2006/01/05 20:23:19 lievre
844: *** empty log message ***
845:
846: Revision 1.104 2005/09/30 16:11:43 lievre
847: (Module): sump fixed, loop imx fixed, and simplifications.
848: (Module): If the status is missing at the last wave but we know
849: that the person is alive, then we can code his/her status as -2
850: (instead of missing=-1 in earlier versions) and his/her
851: contributions to the likelihood is 1 - Prob of dying from last
852: health status (= 1-p13= p11+p12 in the easiest case of somebody in
853: the healthy state at last known wave). Version is 0.98
854:
855: Revision 1.103 2005/09/30 15:54:49 lievre
856: (Module): sump fixed, loop imx fixed, and simplifications.
857:
858: Revision 1.102 2004/09/15 17:31:30 brouard
859: Add the possibility to read data file including tab characters.
860:
861: Revision 1.101 2004/09/15 10:38:38 brouard
862: Fix on curr_time
863:
864: Revision 1.100 2004/07/12 18:29:06 brouard
865: Add version for Mac OS X. Just define UNIX in Makefile
866:
867: Revision 1.99 2004/06/05 08:57:40 brouard
868: *** empty log message ***
869:
870: Revision 1.98 2004/05/16 15:05:56 brouard
871: New version 0.97 . First attempt to estimate force of mortality
872: directly from the data i.e. without the need of knowing the health
873: state at each age, but using a Gompertz model: log u =a + b*age .
874: This is the basic analysis of mortality and should be done before any
875: other analysis, in order to test if the mortality estimated from the
876: cross-longitudinal survey is different from the mortality estimated
877: from other sources like vital statistic data.
878:
879: The same imach parameter file can be used but the option for mle should be -3.
880:
1.324 brouard 881: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 882: former routines in order to include the new code within the former code.
883:
884: The output is very simple: only an estimate of the intercept and of
885: the slope with 95% confident intervals.
886:
887: Current limitations:
888: A) Even if you enter covariates, i.e. with the
889: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
890: B) There is no computation of Life Expectancy nor Life Table.
891:
892: Revision 1.97 2004/02/20 13:25:42 lievre
893: Version 0.96d. Population forecasting command line is (temporarily)
894: suppressed.
895:
896: Revision 1.96 2003/07/15 15:38:55 brouard
897: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
898: rewritten within the same printf. Workaround: many printfs.
899:
900: Revision 1.95 2003/07/08 07:54:34 brouard
901: * imach.c (Repository):
902: (Repository): Using imachwizard code to output a more meaningful covariance
903: matrix (cov(a12,c31) instead of numbers.
904:
905: Revision 1.94 2003/06/27 13:00:02 brouard
906: Just cleaning
907:
908: Revision 1.93 2003/06/25 16:33:55 brouard
909: (Module): On windows (cygwin) function asctime_r doesn't
910: exist so I changed back to asctime which exists.
911: (Module): Version 0.96b
912:
913: Revision 1.92 2003/06/25 16:30:45 brouard
914: (Module): On windows (cygwin) function asctime_r doesn't
915: exist so I changed back to asctime which exists.
916:
917: Revision 1.91 2003/06/25 15:30:29 brouard
918: * imach.c (Repository): Duplicated warning errors corrected.
919: (Repository): Elapsed time after each iteration is now output. It
920: helps to forecast when convergence will be reached. Elapsed time
921: is stamped in powell. We created a new html file for the graphs
922: concerning matrix of covariance. It has extension -cov.htm.
923:
924: Revision 1.90 2003/06/24 12:34:15 brouard
925: (Module): Some bugs corrected for windows. Also, when
926: mle=-1 a template is output in file "or"mypar.txt with the design
927: of the covariance matrix to be input.
928:
929: Revision 1.89 2003/06/24 12:30:52 brouard
930: (Module): Some bugs corrected for windows. Also, when
931: mle=-1 a template is output in file "or"mypar.txt with the design
932: of the covariance matrix to be input.
933:
934: Revision 1.88 2003/06/23 17:54:56 brouard
935: * 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.
936:
937: Revision 1.87 2003/06/18 12:26:01 brouard
938: Version 0.96
939:
940: Revision 1.86 2003/06/17 20:04:08 brouard
941: (Module): Change position of html and gnuplot routines and added
942: routine fileappend.
943:
944: Revision 1.85 2003/06/17 13:12:43 brouard
945: * imach.c (Repository): Check when date of death was earlier that
946: current date of interview. It may happen when the death was just
947: prior to the death. In this case, dh was negative and likelihood
948: was wrong (infinity). We still send an "Error" but patch by
949: assuming that the date of death was just one stepm after the
950: interview.
951: (Repository): Because some people have very long ID (first column)
952: we changed int to long in num[] and we added a new lvector for
953: memory allocation. But we also truncated to 8 characters (left
954: truncation)
955: (Repository): No more line truncation errors.
956:
957: Revision 1.84 2003/06/13 21:44:43 brouard
958: * imach.c (Repository): Replace "freqsummary" at a correct
959: place. It differs from routine "prevalence" which may be called
960: many times. Probs is memory consuming and must be used with
961: parcimony.
962: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
963:
964: Revision 1.83 2003/06/10 13:39:11 lievre
965: *** empty log message ***
966:
967: Revision 1.82 2003/06/05 15:57:20 brouard
968: Add log in imach.c and fullversion number is now printed.
969:
970: */
971: /*
972: Interpolated Markov Chain
973:
974: Short summary of the programme:
975:
1.227 brouard 976: This program computes Healthy Life Expectancies or State-specific
977: (if states aren't health statuses) Expectancies from
978: cross-longitudinal data. Cross-longitudinal data consist in:
979:
980: -1- a first survey ("cross") where individuals from different ages
981: are interviewed on their health status or degree of disability (in
982: the case of a health survey which is our main interest)
983:
984: -2- at least a second wave of interviews ("longitudinal") which
985: measure each change (if any) in individual health status. Health
986: expectancies are computed from the time spent in each health state
987: according to a model. More health states you consider, more time is
988: necessary to reach the Maximum Likelihood of the parameters involved
989: in the model. The simplest model is the multinomial logistic model
990: where pij is the probability to be observed in state j at the second
991: wave conditional to be observed in state i at the first
992: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
993: etc , where 'age' is age and 'sex' is a covariate. If you want to
994: have a more complex model than "constant and age", you should modify
995: the program where the markup *Covariates have to be included here
996: again* invites you to do it. More covariates you add, slower the
1.126 brouard 997: convergence.
998:
999: The advantage of this computer programme, compared to a simple
1000: multinomial logistic model, is clear when the delay between waves is not
1001: identical for each individual. Also, if a individual missed an
1002: intermediate interview, the information is lost, but taken into
1003: account using an interpolation or extrapolation.
1004:
1005: hPijx is the probability to be observed in state i at age x+h
1006: conditional to the observed state i at age x. The delay 'h' can be
1007: split into an exact number (nh*stepm) of unobserved intermediate
1008: states. This elementary transition (by month, quarter,
1009: semester or year) is modelled as a multinomial logistic. The hPx
1010: matrix is simply the matrix product of nh*stepm elementary matrices
1011: and the contribution of each individual to the likelihood is simply
1012: hPijx.
1013:
1014: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 1015: of the life expectancies. It also computes the period (stable) prevalence.
1016:
1017: Back prevalence and projections:
1.227 brouard 1018:
1019: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
1020: double agemaxpar, double ftolpl, int *ncvyearp, double
1021: dateprev1,double dateprev2, int firstpass, int lastpass, int
1022: mobilavproj)
1023:
1024: Computes the back prevalence limit for any combination of
1025: covariate values k at any age between ageminpar and agemaxpar and
1026: returns it in **bprlim. In the loops,
1027:
1028: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
1029: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
1030:
1031: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 1032: Computes for any combination of covariates k and any age between bage and fage
1033: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
1034: oldm=oldms;savm=savms;
1.227 brouard 1035:
1.267 brouard 1036: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.218 brouard 1037: Computes the transition matrix starting at age 'age' over
1038: 'nhstepm*hstepm*stepm' months (i.e. until
1039: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 1040: nhstepm*hstepm matrices.
1041:
1042: Returns p3mat[i][j][h] after calling
1043: p3mat[i][j][h]=matprod2(newm,
1044: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1045: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1046: oldm);
1.226 brouard 1047:
1048: Important routines
1049:
1050: - func (or funcone), computes logit (pij) distinguishing
1051: o fixed variables (single or product dummies or quantitative);
1052: o varying variables by:
1053: (1) wave (single, product dummies, quantitative),
1054: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1055: % fixed dummy (treated) or quantitative (not done because time-consuming);
1056: % varying dummy (not done) or quantitative (not done);
1057: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1058: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1059: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1.325 brouard 1060: o There are 2**cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1.226 brouard 1061: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 1062:
1.226 brouard 1063:
1064:
1.324 brouard 1065: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1066: Institut national d'études démographiques, Paris.
1.126 brouard 1067: This software have been partly granted by Euro-REVES, a concerted action
1068: from the European Union.
1069: It is copyrighted identically to a GNU software product, ie programme and
1070: software can be distributed freely for non commercial use. Latest version
1071: can be accessed at http://euroreves.ined.fr/imach .
1072:
1073: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1074: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1075:
1076: **********************************************************************/
1077: /*
1078: main
1079: read parameterfile
1080: read datafile
1081: concatwav
1082: freqsummary
1083: if (mle >= 1)
1084: mlikeli
1085: print results files
1086: if mle==1
1087: computes hessian
1088: read end of parameter file: agemin, agemax, bage, fage, estepm
1089: begin-prev-date,...
1090: open gnuplot file
1091: open html file
1.145 brouard 1092: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1093: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1094: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1095: freexexit2 possible for memory heap.
1096:
1097: h Pij x | pij_nom ficrestpij
1098: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1099: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1100: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1101:
1102: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1103: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1104: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1105: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1106: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1107:
1.126 brouard 1108: forecasting if prevfcast==1 prevforecast call prevalence()
1109: health expectancies
1110: Variance-covariance of DFLE
1111: prevalence()
1112: movingaverage()
1113: varevsij()
1114: if popbased==1 varevsij(,popbased)
1115: total life expectancies
1116: Variance of period (stable) prevalence
1117: end
1118: */
1119:
1.187 brouard 1120: /* #define DEBUG */
1121: /* #define DEBUGBRENT */
1.203 brouard 1122: /* #define DEBUGLINMIN */
1123: /* #define DEBUGHESS */
1124: #define DEBUGHESSIJ
1.224 brouard 1125: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 1126: #define POWELL /* Instead of NLOPT */
1.224 brouard 1127: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 1128: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1129: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.319 brouard 1130: /* #define FLATSUP *//* Suppresses directions where likelihood is flat */
1.126 brouard 1131:
1132: #include <math.h>
1133: #include <stdio.h>
1134: #include <stdlib.h>
1135: #include <string.h>
1.226 brouard 1136: #include <ctype.h>
1.159 brouard 1137:
1138: #ifdef _WIN32
1139: #include <io.h>
1.172 brouard 1140: #include <windows.h>
1141: #include <tchar.h>
1.159 brouard 1142: #else
1.126 brouard 1143: #include <unistd.h>
1.159 brouard 1144: #endif
1.126 brouard 1145:
1146: #include <limits.h>
1147: #include <sys/types.h>
1.171 brouard 1148:
1149: #if defined(__GNUC__)
1150: #include <sys/utsname.h> /* Doesn't work on Windows */
1151: #endif
1152:
1.126 brouard 1153: #include <sys/stat.h>
1154: #include <errno.h>
1.159 brouard 1155: /* extern int errno; */
1.126 brouard 1156:
1.157 brouard 1157: /* #ifdef LINUX */
1158: /* #include <time.h> */
1159: /* #include "timeval.h" */
1160: /* #else */
1161: /* #include <sys/time.h> */
1162: /* #endif */
1163:
1.126 brouard 1164: #include <time.h>
1165:
1.136 brouard 1166: #ifdef GSL
1167: #include <gsl/gsl_errno.h>
1168: #include <gsl/gsl_multimin.h>
1169: #endif
1170:
1.167 brouard 1171:
1.162 brouard 1172: #ifdef NLOPT
1173: #include <nlopt.h>
1174: typedef struct {
1175: double (* function)(double [] );
1176: } myfunc_data ;
1177: #endif
1178:
1.126 brouard 1179: /* #include <libintl.h> */
1180: /* #define _(String) gettext (String) */
1181:
1.251 brouard 1182: #define MAXLINE 2048 /* Was 256 and 1024. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 1183:
1184: #define GNUPLOTPROGRAM "gnuplot"
1185: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1186: #define FILENAMELENGTH 132
1187:
1188: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1189: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1190:
1.144 brouard 1191: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
1192: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 1193:
1194: #define NINTERVMAX 8
1.144 brouard 1195: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1196: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1.325 brouard 1197: #define NCOVMAX 30 /**< Maximum number of covariates used in the model, including generated covariates V1*V2 or V1*age */
1.197 brouard 1198: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 1199: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1200: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.290 brouard 1201: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1.144 brouard 1202: #define YEARM 12. /**< Number of months per year */
1.218 brouard 1203: /* #define AGESUP 130 */
1.288 brouard 1204: /* #define AGESUP 150 */
1205: #define AGESUP 200
1.268 brouard 1206: #define AGEINF 0
1.218 brouard 1207: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 1208: #define AGEBASE 40
1.194 brouard 1209: #define AGEOVERFLOW 1.e20
1.164 brouard 1210: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 1211: #ifdef _WIN32
1212: #define DIRSEPARATOR '\\'
1213: #define CHARSEPARATOR "\\"
1214: #define ODIRSEPARATOR '/'
1215: #else
1.126 brouard 1216: #define DIRSEPARATOR '/'
1217: #define CHARSEPARATOR "/"
1218: #define ODIRSEPARATOR '\\'
1219: #endif
1220:
1.326 ! brouard 1221: /* $Id: imach.c,v 1.325 2022/07/25 14:27:23 brouard Exp $ */
1.126 brouard 1222: /* $State: Exp $ */
1.196 brouard 1223: #include "version.h"
1224: char version[]=__IMACH_VERSION__;
1.323 brouard 1225: char copyright[]="July 2022,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";
1.326 ! brouard 1226: char fullversion[]="$Revision: 1.325 $ $Date: 2022/07/25 14:27:23 $";
1.126 brouard 1227: char strstart[80];
1228: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 1229: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 1230: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 1231: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
1232: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
1233: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 1234: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
1235: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 1236: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1237: int cptcovprodnoage=0; /**< Number of covariate products without age */
1238: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 1239: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1240: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 1241: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 brouard 1242: int nsd=0; /**< Total number of single dummy variables (output) */
1243: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 1244: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 1245: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 1246: int ntveff=0; /**< ntveff number of effective time varying variables */
1247: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 1248: int cptcov=0; /* Working variable */
1.290 brouard 1249: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1.218 brouard 1250: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.302 brouard 1251: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1.126 brouard 1252: int nlstate=2; /* Number of live states */
1253: int ndeath=1; /* Number of dead states */
1.130 brouard 1254: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 1255: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 1256: int popbased=0;
1257:
1258: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 1259: int maxwav=0; /* Maxim number of waves */
1260: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1261: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1262: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 1263: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 1264: int mle=1, weightopt=0;
1.126 brouard 1265: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1266: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1267: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1268: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 1269: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 1270: int selected(int kvar); /* Is covariate kvar selected for printing results */
1271:
1.130 brouard 1272: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 1273: double **matprod2(); /* test */
1.126 brouard 1274: double **oldm, **newm, **savm; /* Working pointers to matrices */
1275: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 1276: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1277:
1.136 brouard 1278: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 1279: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 1280: FILE *ficlog, *ficrespow;
1.130 brouard 1281: int globpr=0; /* Global variable for printing or not */
1.126 brouard 1282: double fretone; /* Only one call to likelihood */
1.130 brouard 1283: long ipmx=0; /* Number of contributions */
1.126 brouard 1284: double sw; /* Sum of weights */
1285: char filerespow[FILENAMELENGTH];
1286: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1287: FILE *ficresilk;
1288: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1289: FILE *ficresprobmorprev;
1290: FILE *fichtm, *fichtmcov; /* Html File */
1291: FILE *ficreseij;
1292: char filerese[FILENAMELENGTH];
1293: FILE *ficresstdeij;
1294: char fileresstde[FILENAMELENGTH];
1295: FILE *ficrescveij;
1296: char filerescve[FILENAMELENGTH];
1297: FILE *ficresvij;
1298: char fileresv[FILENAMELENGTH];
1.269 brouard 1299:
1.126 brouard 1300: char title[MAXLINE];
1.234 brouard 1301: char model[MAXLINE]; /**< The model line */
1.217 brouard 1302: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1303: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1304: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1305: char command[FILENAMELENGTH];
1306: int outcmd=0;
1307:
1.217 brouard 1308: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1309: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1310: char filelog[FILENAMELENGTH]; /* Log file */
1311: char filerest[FILENAMELENGTH];
1312: char fileregp[FILENAMELENGTH];
1313: char popfile[FILENAMELENGTH];
1314:
1315: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1316:
1.157 brouard 1317: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1318: /* struct timezone tzp; */
1319: /* extern int gettimeofday(); */
1320: struct tm tml, *gmtime(), *localtime();
1321:
1322: extern time_t time();
1323:
1324: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1325: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1326: struct tm tm;
1327:
1.126 brouard 1328: char strcurr[80], strfor[80];
1329:
1330: char *endptr;
1331: long lval;
1332: double dval;
1333:
1334: #define NR_END 1
1335: #define FREE_ARG char*
1336: #define FTOL 1.0e-10
1337:
1338: #define NRANSI
1.240 brouard 1339: #define ITMAX 200
1340: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1341:
1342: #define TOL 2.0e-4
1343:
1344: #define CGOLD 0.3819660
1345: #define ZEPS 1.0e-10
1346: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1347:
1348: #define GOLD 1.618034
1349: #define GLIMIT 100.0
1350: #define TINY 1.0e-20
1351:
1352: static double maxarg1,maxarg2;
1353: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1354: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1355:
1356: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1357: #define rint(a) floor(a+0.5)
1.166 brouard 1358: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1359: #define mytinydouble 1.0e-16
1.166 brouard 1360: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1361: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1362: /* static double dsqrarg; */
1363: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1364: static double sqrarg;
1365: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1366: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1367: int agegomp= AGEGOMP;
1368:
1369: int imx;
1370: int stepm=1;
1371: /* Stepm, step in month: minimum step interpolation*/
1372:
1373: int estepm;
1374: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1375:
1376: int m,nb;
1377: long *num;
1.197 brouard 1378: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1379: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1380: covariate for which somebody answered excluding
1381: undefined. Usually 2: 0 and 1. */
1382: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1383: covariate for which somebody answered including
1384: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1385: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1386: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1387: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1388: double *ageexmed,*agecens;
1389: double dateintmean=0;
1.296 brouard 1390: double anprojd, mprojd, jprojd; /* For eventual projections */
1391: double anprojf, mprojf, jprojf;
1.126 brouard 1392:
1.296 brouard 1393: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1394: double anbackf, mbackf, jbackf;
1395: double jintmean,mintmean,aintmean;
1.126 brouard 1396: double *weight;
1397: int **s; /* Status */
1.141 brouard 1398: double *agedc;
1.145 brouard 1399: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1400: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1401: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.268 brouard 1402: double **coqvar; /* Fixed quantitative covariate nqv */
1403: double ***cotvar; /* Time varying covariate ntv */
1.225 brouard 1404: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1405: double idx;
1406: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.319 brouard 1407: /* Some documentation */
1408: /* Design original data
1409: * V1 V2 V3 V4 V5 V6 V7 V8 Weight ddb ddth d1st s1 V9 V10 V11 V12 s2 V9 V10 V11 V12
1410: * < ncovcol=6 > nqv=2 (V7 V8) dv dv dv qtv dv dv dvv qtv
1411: * ntv=3 nqtv=1
1412: * cptcovn number of covariates (not including constant and age) = # of + plus 1 = 10+1=11
1413: * For time varying covariate, quanti or dummies
1414: * cotqvar[wav][iv(1 to nqtv)][i]= [1][12][i]=(V12) quanti
1415: * cotvar[wav][ntv+iv][i]= [3+(1 to nqtv)][i]=(V12) quanti
1416: * cotvar[wav][iv(1 to ntv)][i]= [1][1][i]=(V9) dummies at wav 1
1417: * cotvar[wav][iv(1 to ntv)][i]= [1][2][i]=(V10) dummies at wav 1
1418: * covar[k,i], value of kth fixed covariate dummy or quanti :
1419: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
1420: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 + V9 + V9*age + V10
1421: * k= 1 2 3 4 5 6 7 8 9 10 11
1422: */
1423: /* According to the model, more columns can be added to covar by the product of covariates */
1.318 brouard 1424: /* 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
1425: # States 1=Coresidence, 2 Living alone, 3 Institution
1426: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1427: */
1.319 brouard 1428: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1429: /* k 1 2 3 4 5 6 7 8 9 */
1430: /*Typevar[k]= 0 0 0 2 1 0 2 1 0 *//*0 for simple covariate (dummy, quantitative,*/
1431: /* fixed or varying), 1 for age product, 2 for*/
1432: /* product */
1433: /*Dummy[k]= 1 0 0 1 3 1 1 2 0 *//*Dummy[k] 0=dummy (0 1), 1 quantitative */
1434: /*(single or product without age), 2 dummy*/
1435: /* with age product, 3 quant with age product*/
1436: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1437: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
1438: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
1439: /*TvarsDind[k] 2 3 9 */ /* position K of single dummy cova */
1440: /* nsq 1 2 */ /* Counting single quantit tv */
1441: /* TvarsQ[k] 5 2 */ /* Number of single quantitative cova */
1442: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1443: /* Tprod[i]=k 1 2 */ /* Position in model of the ith prod without age */
1444: /* cptcovage 1 2 */ /* Counting cov*age in the model equation */
1445: /* Tage[cptcovage]=k 5 8 */ /* Position in the model of ith cov*age */
1446: /* Tvard[1][1]@4={4,3,1,2} V4*V3 V1*V2 */ /* Position in model of the ith prod without age */
1447: /* 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 1448: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.234 brouard 1449: /* Type */
1450: /* V 1 2 3 4 5 */
1451: /* F F V V V */
1452: /* D Q D D Q */
1453: /* */
1454: int *TvarsD;
1455: int *TvarsDind;
1456: int *TvarsQ;
1457: int *TvarsQind;
1458:
1.318 brouard 1459: #define MAXRESULTLINESPONE 10+1
1.235 brouard 1460: int nresult=0;
1.258 brouard 1461: int parameterline=0; /* # of the parameter (type) line */
1.318 brouard 1462: int TKresult[MAXRESULTLINESPONE];
1463: int Tresult[MAXRESULTLINESPONE][NCOVMAX];/* For dummy variable , value (output) */
1464: int Tinvresult[MAXRESULTLINESPONE][NCOVMAX];/* For dummy variable , value (output) */
1465: int Tvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For dummy variable , variable # (output) */
1466: double Tqresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1467: double Tqinvresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , value (output) */
1468: int Tvqresult[MAXRESULTLINESPONE][NCOVMAX]; /* For quantitative variable , variable # (output) */
1469:
1470: /* 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
1471: # States 1=Coresidence, 2 Living alone, 3 Institution
1472: # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi
1473: */
1.234 brouard 1474: /* 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 1475: 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 */
1476: 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 */
1477: 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 */
1478: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1479: 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 */
1480: 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 1481: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1482: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1483: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1484: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1485: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1486: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1487: 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 */
1488: 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 */
1489:
1.230 brouard 1490: int *Tvarsel; /**< Selected covariates for output */
1491: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1492: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1493: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1494: 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 1495: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1496: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1497: int *Tage;
1.227 brouard 1498: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1499: 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 1500: 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*/
1501: 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 1502: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1503: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1504: int **Tvard;
1505: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1506: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1507: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1508: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1509: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1510: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1511: double *lsurv, *lpop, *tpop;
1512:
1.231 brouard 1513: #define FD 1; /* Fixed dummy covariate */
1514: #define FQ 2; /* Fixed quantitative covariate */
1515: #define FP 3; /* Fixed product covariate */
1516: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1517: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1518: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1519: #define VD 10; /* Varying dummy covariate */
1520: #define VQ 11; /* Varying quantitative covariate */
1521: #define VP 12; /* Varying product covariate */
1522: #define VPDD 13; /* Varying product dummy*dummy covariate */
1523: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1524: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1525: #define APFD 16; /* Age product * fixed dummy covariate */
1526: #define APFQ 17; /* Age product * fixed quantitative covariate */
1527: #define APVD 18; /* Age product * varying dummy covariate */
1528: #define APVQ 19; /* Age product * varying quantitative covariate */
1529:
1530: #define FTYPE 1; /* Fixed covariate */
1531: #define VTYPE 2; /* Varying covariate (loop in wave) */
1532: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1533:
1534: struct kmodel{
1535: int maintype; /* main type */
1536: int subtype; /* subtype */
1537: };
1538: struct kmodel modell[NCOVMAX];
1539:
1.143 brouard 1540: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1541: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1542:
1543: /**************** split *************************/
1544: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1545: {
1546: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1547: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1548: */
1549: char *ss; /* pointer */
1.186 brouard 1550: int l1=0, l2=0; /* length counters */
1.126 brouard 1551:
1552: l1 = strlen(path ); /* length of path */
1553: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1554: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1555: if ( ss == NULL ) { /* no directory, so determine current directory */
1556: strcpy( name, path ); /* we got the fullname name because no directory */
1557: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1558: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1559: /* get current working directory */
1560: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1561: #ifdef WIN32
1562: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1563: #else
1564: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1565: #endif
1.126 brouard 1566: return( GLOCK_ERROR_GETCWD );
1567: }
1568: /* got dirc from getcwd*/
1569: printf(" DIRC = %s \n",dirc);
1.205 brouard 1570: } else { /* strip directory from path */
1.126 brouard 1571: ss++; /* after this, the filename */
1572: l2 = strlen( ss ); /* length of filename */
1573: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1574: strcpy( name, ss ); /* save file name */
1575: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1576: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1577: printf(" DIRC2 = %s \n",dirc);
1578: }
1579: /* We add a separator at the end of dirc if not exists */
1580: l1 = strlen( dirc ); /* length of directory */
1581: if( dirc[l1-1] != DIRSEPARATOR ){
1582: dirc[l1] = DIRSEPARATOR;
1583: dirc[l1+1] = 0;
1584: printf(" DIRC3 = %s \n",dirc);
1585: }
1586: ss = strrchr( name, '.' ); /* find last / */
1587: if (ss >0){
1588: ss++;
1589: strcpy(ext,ss); /* save extension */
1590: l1= strlen( name);
1591: l2= strlen(ss)+1;
1592: strncpy( finame, name, l1-l2);
1593: finame[l1-l2]= 0;
1594: }
1595:
1596: return( 0 ); /* we're done */
1597: }
1598:
1599:
1600: /******************************************/
1601:
1602: void replace_back_to_slash(char *s, char*t)
1603: {
1604: int i;
1605: int lg=0;
1606: i=0;
1607: lg=strlen(t);
1608: for(i=0; i<= lg; i++) {
1609: (s[i] = t[i]);
1610: if (t[i]== '\\') s[i]='/';
1611: }
1612: }
1613:
1.132 brouard 1614: char *trimbb(char *out, char *in)
1.137 brouard 1615: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1616: char *s;
1617: s=out;
1618: while (*in != '\0'){
1.137 brouard 1619: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1620: in++;
1621: }
1622: *out++ = *in++;
1623: }
1624: *out='\0';
1625: return s;
1626: }
1627:
1.187 brouard 1628: /* char *substrchaine(char *out, char *in, char *chain) */
1629: /* { */
1630: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1631: /* char *s, *t; */
1632: /* t=in;s=out; */
1633: /* while ((*in != *chain) && (*in != '\0')){ */
1634: /* *out++ = *in++; */
1635: /* } */
1636:
1637: /* /\* *in matches *chain *\/ */
1638: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1639: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1640: /* } */
1641: /* in--; chain--; */
1642: /* while ( (*in != '\0')){ */
1643: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1644: /* *out++ = *in++; */
1645: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1646: /* } */
1647: /* *out='\0'; */
1648: /* out=s; */
1649: /* return out; */
1650: /* } */
1651: char *substrchaine(char *out, char *in, char *chain)
1652: {
1653: /* Substract chain 'chain' from 'in', return and output 'out' */
1654: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1655:
1656: char *strloc;
1657:
1658: strcpy (out, in);
1659: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1660: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1661: if(strloc != NULL){
1662: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1663: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1664: /* strcpy (strloc, strloc +strlen(chain));*/
1665: }
1666: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1667: return out;
1668: }
1669:
1670:
1.145 brouard 1671: char *cutl(char *blocc, char *alocc, char *in, char occ)
1672: {
1.187 brouard 1673: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1674: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.310 brouard 1675: gives alocc="abcdef" and blocc="ghi2j".
1.145 brouard 1676: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1677: */
1.160 brouard 1678: char *s, *t;
1.145 brouard 1679: t=in;s=in;
1680: while ((*in != occ) && (*in != '\0')){
1681: *alocc++ = *in++;
1682: }
1683: if( *in == occ){
1684: *(alocc)='\0';
1685: s=++in;
1686: }
1687:
1688: if (s == t) {/* occ not found */
1689: *(alocc-(in-s))='\0';
1690: in=s;
1691: }
1692: while ( *in != '\0'){
1693: *blocc++ = *in++;
1694: }
1695:
1696: *blocc='\0';
1697: return t;
1698: }
1.137 brouard 1699: char *cutv(char *blocc, char *alocc, char *in, char occ)
1700: {
1.187 brouard 1701: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1702: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1703: gives blocc="abcdef2ghi" and alocc="j".
1704: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1705: */
1706: char *s, *t;
1707: t=in;s=in;
1708: while (*in != '\0'){
1709: while( *in == occ){
1710: *blocc++ = *in++;
1711: s=in;
1712: }
1713: *blocc++ = *in++;
1714: }
1715: if (s == t) /* occ not found */
1716: *(blocc-(in-s))='\0';
1717: else
1718: *(blocc-(in-s)-1)='\0';
1719: in=s;
1720: while ( *in != '\0'){
1721: *alocc++ = *in++;
1722: }
1723:
1724: *alocc='\0';
1725: return s;
1726: }
1727:
1.126 brouard 1728: int nbocc(char *s, char occ)
1729: {
1730: int i,j=0;
1731: int lg=20;
1732: i=0;
1733: lg=strlen(s);
1734: for(i=0; i<= lg; i++) {
1.234 brouard 1735: if (s[i] == occ ) j++;
1.126 brouard 1736: }
1737: return j;
1738: }
1739:
1.137 brouard 1740: /* void cutv(char *u,char *v, char*t, char occ) */
1741: /* { */
1742: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1743: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1744: /* gives u="abcdef2ghi" and v="j" *\/ */
1745: /* int i,lg,j,p=0; */
1746: /* i=0; */
1747: /* lg=strlen(t); */
1748: /* for(j=0; j<=lg-1; j++) { */
1749: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1750: /* } */
1.126 brouard 1751:
1.137 brouard 1752: /* for(j=0; j<p; j++) { */
1753: /* (u[j] = t[j]); */
1754: /* } */
1755: /* u[p]='\0'; */
1.126 brouard 1756:
1.137 brouard 1757: /* for(j=0; j<= lg; j++) { */
1758: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1759: /* } */
1760: /* } */
1.126 brouard 1761:
1.160 brouard 1762: #ifdef _WIN32
1763: char * strsep(char **pp, const char *delim)
1764: {
1765: char *p, *q;
1766:
1767: if ((p = *pp) == NULL)
1768: return 0;
1769: if ((q = strpbrk (p, delim)) != NULL)
1770: {
1771: *pp = q + 1;
1772: *q = '\0';
1773: }
1774: else
1775: *pp = 0;
1776: return p;
1777: }
1778: #endif
1779:
1.126 brouard 1780: /********************** nrerror ********************/
1781:
1782: void nrerror(char error_text[])
1783: {
1784: fprintf(stderr,"ERREUR ...\n");
1785: fprintf(stderr,"%s\n",error_text);
1786: exit(EXIT_FAILURE);
1787: }
1788: /*********************** vector *******************/
1789: double *vector(int nl, int nh)
1790: {
1791: double *v;
1792: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1793: if (!v) nrerror("allocation failure in vector");
1794: return v-nl+NR_END;
1795: }
1796:
1797: /************************ free vector ******************/
1798: void free_vector(double*v, int nl, int nh)
1799: {
1800: free((FREE_ARG)(v+nl-NR_END));
1801: }
1802:
1803: /************************ivector *******************************/
1804: int *ivector(long nl,long nh)
1805: {
1806: int *v;
1807: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1808: if (!v) nrerror("allocation failure in ivector");
1809: return v-nl+NR_END;
1810: }
1811:
1812: /******************free ivector **************************/
1813: void free_ivector(int *v, long nl, long nh)
1814: {
1815: free((FREE_ARG)(v+nl-NR_END));
1816: }
1817:
1818: /************************lvector *******************************/
1819: long *lvector(long nl,long nh)
1820: {
1821: long *v;
1822: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1823: if (!v) nrerror("allocation failure in ivector");
1824: return v-nl+NR_END;
1825: }
1826:
1827: /******************free lvector **************************/
1828: void free_lvector(long *v, long nl, long nh)
1829: {
1830: free((FREE_ARG)(v+nl-NR_END));
1831: }
1832:
1833: /******************* imatrix *******************************/
1834: int **imatrix(long nrl, long nrh, long ncl, long nch)
1835: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1836: {
1837: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1838: int **m;
1839:
1840: /* allocate pointers to rows */
1841: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1842: if (!m) nrerror("allocation failure 1 in matrix()");
1843: m += NR_END;
1844: m -= nrl;
1845:
1846:
1847: /* allocate rows and set pointers to them */
1848: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1849: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1850: m[nrl] += NR_END;
1851: m[nrl] -= ncl;
1852:
1853: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1854:
1855: /* return pointer to array of pointers to rows */
1856: return m;
1857: }
1858:
1859: /****************** free_imatrix *************************/
1860: void free_imatrix(m,nrl,nrh,ncl,nch)
1861: int **m;
1862: long nch,ncl,nrh,nrl;
1863: /* free an int matrix allocated by imatrix() */
1864: {
1865: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1866: free((FREE_ARG) (m+nrl-NR_END));
1867: }
1868:
1869: /******************* matrix *******************************/
1870: double **matrix(long nrl, long nrh, long ncl, long nch)
1871: {
1872: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1873: double **m;
1874:
1875: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1876: if (!m) nrerror("allocation failure 1 in matrix()");
1877: m += NR_END;
1878: m -= nrl;
1879:
1880: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1881: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1882: m[nrl] += NR_END;
1883: m[nrl] -= ncl;
1884:
1885: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1886: return m;
1.145 brouard 1887: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1888: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1889: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1890: */
1891: }
1892:
1893: /*************************free matrix ************************/
1894: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1895: {
1896: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1897: free((FREE_ARG)(m+nrl-NR_END));
1898: }
1899:
1900: /******************* ma3x *******************************/
1901: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1902: {
1903: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1904: double ***m;
1905:
1906: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1907: if (!m) nrerror("allocation failure 1 in matrix()");
1908: m += NR_END;
1909: m -= nrl;
1910:
1911: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1912: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1913: m[nrl] += NR_END;
1914: m[nrl] -= ncl;
1915:
1916: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1917:
1918: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1919: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1920: m[nrl][ncl] += NR_END;
1921: m[nrl][ncl] -= nll;
1922: for (j=ncl+1; j<=nch; j++)
1923: m[nrl][j]=m[nrl][j-1]+nlay;
1924:
1925: for (i=nrl+1; i<=nrh; i++) {
1926: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1927: for (j=ncl+1; j<=nch; j++)
1928: m[i][j]=m[i][j-1]+nlay;
1929: }
1930: return m;
1931: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1932: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1933: */
1934: }
1935:
1936: /*************************free ma3x ************************/
1937: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1938: {
1939: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1940: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1941: free((FREE_ARG)(m+nrl-NR_END));
1942: }
1943:
1944: /*************** function subdirf ***********/
1945: char *subdirf(char fileres[])
1946: {
1947: /* Caution optionfilefiname is hidden */
1948: strcpy(tmpout,optionfilefiname);
1949: strcat(tmpout,"/"); /* Add to the right */
1950: strcat(tmpout,fileres);
1951: return tmpout;
1952: }
1953:
1954: /*************** function subdirf2 ***********/
1955: char *subdirf2(char fileres[], char *preop)
1956: {
1.314 brouard 1957: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
1958: Errors in subdirf, 2, 3 while printing tmpout is
1.315 brouard 1959: rewritten within the same printf. Workaround: many printfs */
1.126 brouard 1960: /* Caution optionfilefiname is hidden */
1961: strcpy(tmpout,optionfilefiname);
1962: strcat(tmpout,"/");
1963: strcat(tmpout,preop);
1964: strcat(tmpout,fileres);
1965: return tmpout;
1966: }
1967:
1968: /*************** function subdirf3 ***********/
1969: char *subdirf3(char fileres[], char *preop, char *preop2)
1970: {
1971:
1972: /* Caution optionfilefiname is hidden */
1973: strcpy(tmpout,optionfilefiname);
1974: strcat(tmpout,"/");
1975: strcat(tmpout,preop);
1976: strcat(tmpout,preop2);
1977: strcat(tmpout,fileres);
1978: return tmpout;
1979: }
1.213 brouard 1980:
1981: /*************** function subdirfext ***********/
1982: char *subdirfext(char fileres[], char *preop, char *postop)
1983: {
1984:
1985: strcpy(tmpout,preop);
1986: strcat(tmpout,fileres);
1987: strcat(tmpout,postop);
1988: return tmpout;
1989: }
1.126 brouard 1990:
1.213 brouard 1991: /*************** function subdirfext3 ***********/
1992: char *subdirfext3(char fileres[], char *preop, char *postop)
1993: {
1994:
1995: /* Caution optionfilefiname is hidden */
1996: strcpy(tmpout,optionfilefiname);
1997: strcat(tmpout,"/");
1998: strcat(tmpout,preop);
1999: strcat(tmpout,fileres);
2000: strcat(tmpout,postop);
2001: return tmpout;
2002: }
2003:
1.162 brouard 2004: char *asc_diff_time(long time_sec, char ascdiff[])
2005: {
2006: long sec_left, days, hours, minutes;
2007: days = (time_sec) / (60*60*24);
2008: sec_left = (time_sec) % (60*60*24);
2009: hours = (sec_left) / (60*60) ;
2010: sec_left = (sec_left) %(60*60);
2011: minutes = (sec_left) /60;
2012: sec_left = (sec_left) % (60);
2013: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
2014: return ascdiff;
2015: }
2016:
1.126 brouard 2017: /***************** f1dim *************************/
2018: extern int ncom;
2019: extern double *pcom,*xicom;
2020: extern double (*nrfunc)(double []);
2021:
2022: double f1dim(double x)
2023: {
2024: int j;
2025: double f;
2026: double *xt;
2027:
2028: xt=vector(1,ncom);
2029: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
2030: f=(*nrfunc)(xt);
2031: free_vector(xt,1,ncom);
2032: return f;
2033: }
2034:
2035: /*****************brent *************************/
2036: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 2037: {
2038: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
2039: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
2040: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
2041: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
2042: * returned function value.
2043: */
1.126 brouard 2044: int iter;
2045: double a,b,d,etemp;
1.159 brouard 2046: double fu=0,fv,fw,fx;
1.164 brouard 2047: double ftemp=0.;
1.126 brouard 2048: double p,q,r,tol1,tol2,u,v,w,x,xm;
2049: double e=0.0;
2050:
2051: a=(ax < cx ? ax : cx);
2052: b=(ax > cx ? ax : cx);
2053: x=w=v=bx;
2054: fw=fv=fx=(*f)(x);
2055: for (iter=1;iter<=ITMAX;iter++) {
2056: xm=0.5*(a+b);
2057: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
2058: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
2059: printf(".");fflush(stdout);
2060: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 2061: #ifdef DEBUGBRENT
1.126 brouard 2062: 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);
2063: 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);
2064: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
2065: #endif
2066: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
2067: *xmin=x;
2068: return fx;
2069: }
2070: ftemp=fu;
2071: if (fabs(e) > tol1) {
2072: r=(x-w)*(fx-fv);
2073: q=(x-v)*(fx-fw);
2074: p=(x-v)*q-(x-w)*r;
2075: q=2.0*(q-r);
2076: if (q > 0.0) p = -p;
2077: q=fabs(q);
2078: etemp=e;
2079: e=d;
2080: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 2081: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 2082: else {
1.224 brouard 2083: d=p/q;
2084: u=x+d;
2085: if (u-a < tol2 || b-u < tol2)
2086: d=SIGN(tol1,xm-x);
1.126 brouard 2087: }
2088: } else {
2089: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2090: }
2091: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2092: fu=(*f)(u);
2093: if (fu <= fx) {
2094: if (u >= x) a=x; else b=x;
2095: SHFT(v,w,x,u)
1.183 brouard 2096: SHFT(fv,fw,fx,fu)
2097: } else {
2098: if (u < x) a=u; else b=u;
2099: if (fu <= fw || w == x) {
1.224 brouard 2100: v=w;
2101: w=u;
2102: fv=fw;
2103: fw=fu;
1.183 brouard 2104: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 2105: v=u;
2106: fv=fu;
1.183 brouard 2107: }
2108: }
1.126 brouard 2109: }
2110: nrerror("Too many iterations in brent");
2111: *xmin=x;
2112: return fx;
2113: }
2114:
2115: /****************** mnbrak ***********************/
2116:
2117: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2118: double (*func)(double))
1.183 brouard 2119: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2120: the downhill direction (defined by the function as evaluated at the initial points) and returns
2121: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2122: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2123: */
1.126 brouard 2124: double ulim,u,r,q, dum;
2125: double fu;
1.187 brouard 2126:
2127: double scale=10.;
2128: int iterscale=0;
2129:
2130: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2131: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2132:
2133:
2134: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2135: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2136: /* *bx = *ax - (*ax - *bx)/scale; */
2137: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2138: /* } */
2139:
1.126 brouard 2140: if (*fb > *fa) {
2141: SHFT(dum,*ax,*bx,dum)
1.183 brouard 2142: SHFT(dum,*fb,*fa,dum)
2143: }
1.126 brouard 2144: *cx=(*bx)+GOLD*(*bx-*ax);
2145: *fc=(*func)(*cx);
1.183 brouard 2146: #ifdef DEBUG
1.224 brouard 2147: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2148: 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 2149: #endif
1.224 brouard 2150: 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 2151: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 2152: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 2153: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 2154: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2155: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2156: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 2157: fu=(*func)(u);
1.163 brouard 2158: #ifdef DEBUG
2159: /* f(x)=A(x-u)**2+f(u) */
2160: double A, fparabu;
2161: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2162: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 2163: 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);
2164: 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 2165: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2166: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2167: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2168: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 2169: #endif
1.184 brouard 2170: #ifdef MNBRAKORIGINAL
1.183 brouard 2171: #else
1.191 brouard 2172: /* if (fu > *fc) { */
2173: /* #ifdef DEBUG */
2174: /* printf("mnbrak4 fu > fc \n"); */
2175: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2176: /* #endif */
2177: /* /\* 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 *\\/ *\/ */
2178: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2179: /* dum=u; /\* Shifting c and u *\/ */
2180: /* u = *cx; */
2181: /* *cx = dum; */
2182: /* dum = fu; */
2183: /* fu = *fc; */
2184: /* *fc =dum; */
2185: /* } else { /\* end *\/ */
2186: /* #ifdef DEBUG */
2187: /* printf("mnbrak3 fu < fc \n"); */
2188: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2189: /* #endif */
2190: /* dum=u; /\* Shifting c and u *\/ */
2191: /* u = *cx; */
2192: /* *cx = dum; */
2193: /* dum = fu; */
2194: /* fu = *fc; */
2195: /* *fc =dum; */
2196: /* } */
1.224 brouard 2197: #ifdef DEBUGMNBRAK
2198: double A, fparabu;
2199: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2200: fparabu= *fa - A*(*ax-u)*(*ax-u);
2201: 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);
2202: 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 2203: #endif
1.191 brouard 2204: dum=u; /* Shifting c and u */
2205: u = *cx;
2206: *cx = dum;
2207: dum = fu;
2208: fu = *fc;
2209: *fc =dum;
1.183 brouard 2210: #endif
1.162 brouard 2211: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 2212: #ifdef DEBUG
1.224 brouard 2213: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2214: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 2215: #endif
1.126 brouard 2216: fu=(*func)(u);
2217: if (fu < *fc) {
1.183 brouard 2218: #ifdef DEBUG
1.224 brouard 2219: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2220: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2221: #endif
2222: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2223: SHFT(*fb,*fc,fu,(*func)(u))
2224: #ifdef DEBUG
2225: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 2226: #endif
2227: }
1.162 brouard 2228: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 2229: #ifdef DEBUG
1.224 brouard 2230: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2231: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 2232: #endif
1.126 brouard 2233: u=ulim;
2234: fu=(*func)(u);
1.183 brouard 2235: } else { /* u could be left to b (if r > q parabola has a maximum) */
2236: #ifdef DEBUG
1.224 brouard 2237: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2238: 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 2239: #endif
1.126 brouard 2240: u=(*cx)+GOLD*(*cx-*bx);
2241: fu=(*func)(u);
1.224 brouard 2242: #ifdef DEBUG
2243: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2244: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2245: #endif
1.183 brouard 2246: } /* end tests */
1.126 brouard 2247: SHFT(*ax,*bx,*cx,u)
1.183 brouard 2248: SHFT(*fa,*fb,*fc,fu)
2249: #ifdef DEBUG
1.224 brouard 2250: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2251: 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 2252: #endif
2253: } /* 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 2254: }
2255:
2256: /*************** linmin ************************/
1.162 brouard 2257: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2258: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2259: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2260: the value of func at the returned location p . This is actually all accomplished by calling the
2261: routines mnbrak and brent .*/
1.126 brouard 2262: int ncom;
2263: double *pcom,*xicom;
2264: double (*nrfunc)(double []);
2265:
1.224 brouard 2266: #ifdef LINMINORIGINAL
1.126 brouard 2267: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 2268: #else
2269: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2270: #endif
1.126 brouard 2271: {
2272: double brent(double ax, double bx, double cx,
2273: double (*f)(double), double tol, double *xmin);
2274: double f1dim(double x);
2275: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2276: double *fc, double (*func)(double));
2277: int j;
2278: double xx,xmin,bx,ax;
2279: double fx,fb,fa;
1.187 brouard 2280:
1.203 brouard 2281: #ifdef LINMINORIGINAL
2282: #else
2283: double scale=10., axs, xxs; /* Scale added for infinity */
2284: #endif
2285:
1.126 brouard 2286: ncom=n;
2287: pcom=vector(1,n);
2288: xicom=vector(1,n);
2289: nrfunc=func;
2290: for (j=1;j<=n;j++) {
2291: pcom[j]=p[j];
1.202 brouard 2292: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 2293: }
1.187 brouard 2294:
1.203 brouard 2295: #ifdef LINMINORIGINAL
2296: xx=1.;
2297: #else
2298: axs=0.0;
2299: xxs=1.;
2300: do{
2301: xx= xxs;
2302: #endif
1.187 brouard 2303: ax=0.;
2304: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2305: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2306: /* 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)) */
2307: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2308: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2309: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2310: /* 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 2311: #ifdef LINMINORIGINAL
2312: #else
2313: if (fx != fx){
1.224 brouard 2314: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2315: printf("|");
2316: fprintf(ficlog,"|");
1.203 brouard 2317: #ifdef DEBUGLINMIN
1.224 brouard 2318: 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 2319: #endif
2320: }
1.224 brouard 2321: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 2322: #endif
2323:
1.191 brouard 2324: #ifdef DEBUGLINMIN
2325: 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 2326: 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 2327: #endif
1.224 brouard 2328: #ifdef LINMINORIGINAL
2329: #else
1.317 brouard 2330: if(fb == fx){ /* Flat function in the direction */
2331: xmin=xx;
1.224 brouard 2332: *flat=1;
1.317 brouard 2333: }else{
1.224 brouard 2334: *flat=0;
2335: #endif
2336: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2337: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2338: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2339: /* fmin = f(p[j] + xmin * xi[j]) */
2340: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2341: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2342: #ifdef DEBUG
1.224 brouard 2343: 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);
2344: 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);
2345: #endif
2346: #ifdef LINMINORIGINAL
2347: #else
2348: }
1.126 brouard 2349: #endif
1.191 brouard 2350: #ifdef DEBUGLINMIN
2351: printf("linmin end ");
1.202 brouard 2352: fprintf(ficlog,"linmin end ");
1.191 brouard 2353: #endif
1.126 brouard 2354: for (j=1;j<=n;j++) {
1.203 brouard 2355: #ifdef LINMINORIGINAL
2356: xi[j] *= xmin;
2357: #else
2358: #ifdef DEBUGLINMIN
2359: if(xxs <1.0)
2360: printf(" before xi[%d]=%12.8f", j,xi[j]);
2361: #endif
2362: 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) */
2363: #ifdef DEBUGLINMIN
2364: if(xxs <1.0)
2365: 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 );
2366: #endif
2367: #endif
1.187 brouard 2368: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2369: }
1.191 brouard 2370: #ifdef DEBUGLINMIN
1.203 brouard 2371: printf("\n");
1.191 brouard 2372: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2373: 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 2374: for (j=1;j<=n;j++) {
1.202 brouard 2375: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2376: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2377: if(j % ncovmodel == 0){
1.191 brouard 2378: printf("\n");
1.202 brouard 2379: fprintf(ficlog,"\n");
2380: }
1.191 brouard 2381: }
1.203 brouard 2382: #else
1.191 brouard 2383: #endif
1.126 brouard 2384: free_vector(xicom,1,n);
2385: free_vector(pcom,1,n);
2386: }
2387:
2388:
2389: /*************** powell ************************/
1.162 brouard 2390: /*
1.317 brouard 2391: Minimization of a function func of n variables. Input consists in an initial starting point
2392: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di-
2393: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value
2394: such that failure to decrease by more than this amount in one iteration signals doneness. On
1.162 brouard 2395: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2396: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2397: */
1.224 brouard 2398: #ifdef LINMINORIGINAL
2399: #else
2400: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2401: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2402: #endif
1.126 brouard 2403: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2404: double (*func)(double []))
2405: {
1.224 brouard 2406: #ifdef LINMINORIGINAL
2407: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2408: double (*func)(double []));
1.224 brouard 2409: #else
1.241 brouard 2410: void linmin(double p[], double xi[], int n, double *fret,
2411: double (*func)(double []),int *flat);
1.224 brouard 2412: #endif
1.239 brouard 2413: int i,ibig,j,jk,k;
1.126 brouard 2414: double del,t,*pt,*ptt,*xit;
1.181 brouard 2415: double directest;
1.126 brouard 2416: double fp,fptt;
2417: double *xits;
2418: int niterf, itmp;
2419:
2420: pt=vector(1,n);
2421: ptt=vector(1,n);
2422: xit=vector(1,n);
2423: xits=vector(1,n);
2424: *fret=(*func)(p);
2425: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2426: rcurr_time = time(NULL);
1.126 brouard 2427: for (*iter=1;;++(*iter)) {
2428: ibig=0;
2429: del=0.0;
1.157 brouard 2430: rlast_time=rcurr_time;
2431: /* (void) gettimeofday(&curr_time,&tzp); */
2432: rcurr_time = time(NULL);
2433: curr_time = *localtime(&rcurr_time);
1.324 brouard 2434: 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);
2435: 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.157 brouard 2436: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.324 brouard 2437: fp=(*fret); /* From former iteration or initial value */
1.192 brouard 2438: for (i=1;i<=n;i++) {
1.126 brouard 2439: fprintf(ficrespow," %.12lf", p[i]);
2440: }
1.239 brouard 2441: fprintf(ficrespow,"\n");fflush(ficrespow);
2442: printf("\n#model= 1 + age ");
2443: fprintf(ficlog,"\n#model= 1 + age ");
2444: if(nagesqr==1){
1.241 brouard 2445: printf(" + age*age ");
2446: fprintf(ficlog," + age*age ");
1.239 brouard 2447: }
2448: for(j=1;j <=ncovmodel-2;j++){
2449: if(Typevar[j]==0) {
2450: printf(" + V%d ",Tvar[j]);
2451: fprintf(ficlog," + V%d ",Tvar[j]);
2452: }else if(Typevar[j]==1) {
2453: printf(" + V%d*age ",Tvar[j]);
2454: fprintf(ficlog," + V%d*age ",Tvar[j]);
2455: }else if(Typevar[j]==2) {
2456: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2457: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2458: }
2459: }
1.126 brouard 2460: printf("\n");
1.239 brouard 2461: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
2462: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 2463: fprintf(ficlog,"\n");
1.239 brouard 2464: for(i=1,jk=1; i <=nlstate; i++){
2465: for(k=1; k <=(nlstate+ndeath); k++){
2466: if (k != i) {
2467: printf("%d%d ",i,k);
2468: fprintf(ficlog,"%d%d ",i,k);
2469: for(j=1; j <=ncovmodel; j++){
2470: printf("%12.7f ",p[jk]);
2471: fprintf(ficlog,"%12.7f ",p[jk]);
2472: jk++;
2473: }
2474: printf("\n");
2475: fprintf(ficlog,"\n");
2476: }
2477: }
2478: }
1.241 brouard 2479: if(*iter <=3 && *iter >1){
1.157 brouard 2480: tml = *localtime(&rcurr_time);
2481: strcpy(strcurr,asctime(&tml));
2482: rforecast_time=rcurr_time;
1.126 brouard 2483: itmp = strlen(strcurr);
2484: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 2485: strcurr[itmp-1]='\0';
1.162 brouard 2486: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2487: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2488: for(niterf=10;niterf<=30;niterf+=10){
1.241 brouard 2489: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2490: forecast_time = *localtime(&rforecast_time);
2491: strcpy(strfor,asctime(&forecast_time));
2492: itmp = strlen(strfor);
2493: if(strfor[itmp-1]=='\n')
2494: strfor[itmp-1]='\0';
2495: printf(" - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
2496: fprintf(ficlog," - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
1.126 brouard 2497: }
2498: }
1.187 brouard 2499: for (i=1;i<=n;i++) { /* For each direction i */
2500: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2501: fptt=(*fret);
2502: #ifdef DEBUG
1.203 brouard 2503: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2504: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2505: #endif
1.203 brouard 2506: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2507: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2508: #ifdef LINMINORIGINAL
1.188 brouard 2509: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2510: #else
2511: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2512: flatdir[i]=flat; /* Function is vanishing in that direction i */
2513: #endif
2514: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2515: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2516: /* because that direction will be replaced unless the gain del is small */
2517: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2518: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2519: /* with the new direction. */
2520: del=fabs(fptt-(*fret));
2521: ibig=i;
1.126 brouard 2522: }
2523: #ifdef DEBUG
2524: printf("%d %.12e",i,(*fret));
2525: fprintf(ficlog,"%d %.12e",i,(*fret));
2526: for (j=1;j<=n;j++) {
1.224 brouard 2527: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2528: printf(" x(%d)=%.12e",j,xit[j]);
2529: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2530: }
2531: for(j=1;j<=n;j++) {
1.225 brouard 2532: printf(" p(%d)=%.12e",j,p[j]);
2533: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2534: }
2535: printf("\n");
2536: fprintf(ficlog,"\n");
2537: #endif
1.187 brouard 2538: } /* end loop on each direction i */
2539: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2540: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2541: /* New value of last point Pn is not computed, P(n-1) */
1.319 brouard 2542: for(j=1;j<=n;j++) {
2543: if(flatdir[j] >0){
2544: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2545: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
1.302 brouard 2546: }
1.319 brouard 2547: /* printf("\n"); */
2548: /* fprintf(ficlog,"\n"); */
2549: }
1.243 brouard 2550: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
2551: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 2552: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2553: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2554: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2555: /* decreased of more than 3.84 */
2556: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2557: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2558: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2559:
1.188 brouard 2560: /* Starting the program with initial values given by a former maximization will simply change */
2561: /* the scales of the directions and the directions, because the are reset to canonical directions */
2562: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2563: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2564: #ifdef DEBUG
2565: int k[2],l;
2566: k[0]=1;
2567: k[1]=-1;
2568: printf("Max: %.12e",(*func)(p));
2569: fprintf(ficlog,"Max: %.12e",(*func)(p));
2570: for (j=1;j<=n;j++) {
2571: printf(" %.12e",p[j]);
2572: fprintf(ficlog," %.12e",p[j]);
2573: }
2574: printf("\n");
2575: fprintf(ficlog,"\n");
2576: for(l=0;l<=1;l++) {
2577: for (j=1;j<=n;j++) {
2578: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2579: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2580: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2581: }
2582: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2583: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2584: }
2585: #endif
2586:
2587: free_vector(xit,1,n);
2588: free_vector(xits,1,n);
2589: free_vector(ptt,1,n);
2590: free_vector(pt,1,n);
2591: return;
1.192 brouard 2592: } /* enough precision */
1.240 brouard 2593: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2594: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2595: ptt[j]=2.0*p[j]-pt[j];
2596: xit[j]=p[j]-pt[j];
2597: pt[j]=p[j];
2598: }
1.181 brouard 2599: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2600: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2601: if (*iter <=4) {
1.225 brouard 2602: #else
2603: #endif
1.224 brouard 2604: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2605: #else
1.161 brouard 2606: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2607: #endif
1.162 brouard 2608: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2609: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2610: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2611: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2612: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2613: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2614: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2615: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2616: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2617: /* Even if f3 <f1, directest can be negative and t >0 */
2618: /* mu² and del² are equal when f3=f1 */
2619: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2620: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2621: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2622: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2623: #ifdef NRCORIGINAL
2624: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2625: #else
2626: 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 2627: t= t- del*SQR(fp-fptt);
1.183 brouard 2628: #endif
1.202 brouard 2629: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2630: #ifdef DEBUG
1.181 brouard 2631: 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);
2632: 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 2633: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2634: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2635: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2636: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2637: 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);
2638: 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);
2639: #endif
1.183 brouard 2640: #ifdef POWELLORIGINAL
2641: if (t < 0.0) { /* Then we use it for new direction */
2642: #else
1.182 brouard 2643: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2644: 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 2645: 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 2646: 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 2647: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2648: }
1.181 brouard 2649: if (directest < 0.0) { /* Then we use it for new direction */
2650: #endif
1.191 brouard 2651: #ifdef DEBUGLINMIN
1.234 brouard 2652: printf("Before linmin in direction P%d-P0\n",n);
2653: for (j=1;j<=n;j++) {
2654: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2655: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2656: if(j % ncovmodel == 0){
2657: printf("\n");
2658: fprintf(ficlog,"\n");
2659: }
2660: }
1.224 brouard 2661: #endif
2662: #ifdef LINMINORIGINAL
1.234 brouard 2663: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2664: #else
1.234 brouard 2665: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2666: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2667: #endif
1.234 brouard 2668:
1.191 brouard 2669: #ifdef DEBUGLINMIN
1.234 brouard 2670: for (j=1;j<=n;j++) {
2671: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2672: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2673: if(j % ncovmodel == 0){
2674: printf("\n");
2675: fprintf(ficlog,"\n");
2676: }
2677: }
1.224 brouard 2678: #endif
1.234 brouard 2679: for (j=1;j<=n;j++) {
2680: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2681: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2682: }
1.224 brouard 2683: #ifdef LINMINORIGINAL
2684: #else
1.234 brouard 2685: for (j=1, flatd=0;j<=n;j++) {
2686: if(flatdir[j]>0)
2687: flatd++;
2688: }
2689: if(flatd >0){
1.255 brouard 2690: printf("%d flat directions: ",flatd);
2691: fprintf(ficlog,"%d flat directions :",flatd);
1.234 brouard 2692: for (j=1;j<=n;j++) {
2693: if(flatdir[j]>0){
2694: printf("%d ",j);
2695: fprintf(ficlog,"%d ",j);
2696: }
2697: }
2698: printf("\n");
2699: fprintf(ficlog,"\n");
1.319 brouard 2700: #ifdef FLATSUP
2701: free_vector(xit,1,n);
2702: free_vector(xits,1,n);
2703: free_vector(ptt,1,n);
2704: free_vector(pt,1,n);
2705: return;
2706: #endif
1.234 brouard 2707: }
1.191 brouard 2708: #endif
1.234 brouard 2709: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2710: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2711:
1.126 brouard 2712: #ifdef DEBUG
1.234 brouard 2713: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2714: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2715: for(j=1;j<=n;j++){
2716: printf(" %lf",xit[j]);
2717: fprintf(ficlog," %lf",xit[j]);
2718: }
2719: printf("\n");
2720: fprintf(ficlog,"\n");
1.126 brouard 2721: #endif
1.192 brouard 2722: } /* end of t or directest negative */
1.224 brouard 2723: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2724: #else
1.234 brouard 2725: } /* end if (fptt < fp) */
1.192 brouard 2726: #endif
1.225 brouard 2727: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 2728: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2729: #else
1.224 brouard 2730: #endif
1.234 brouard 2731: } /* loop iteration */
1.126 brouard 2732: }
1.234 brouard 2733:
1.126 brouard 2734: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 2735:
1.235 brouard 2736: 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 2737: {
1.279 brouard 2738: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij
2739: * (and selected quantitative values in nres)
2740: * by left multiplying the unit
2741: * matrix by transitions matrix until convergence is reached with precision ftolpl
2742: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
2743: * Wx is row vector: population in state 1, population in state 2, population dead
2744: * or prevalence in state 1, prevalence in state 2, 0
2745: * newm is the matrix after multiplications, its rows are identical at a factor.
2746: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
2747: * Output is prlim.
2748: * Initial matrix pimij
2749: */
1.206 brouard 2750: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2751: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2752: /* 0, 0 , 1} */
2753: /*
2754: * and after some iteration: */
2755: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2756: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2757: /* 0, 0 , 1} */
2758: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2759: /* {0.51571254859325999, 0.4842874514067399, */
2760: /* 0.51326036147820708, 0.48673963852179264} */
2761: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 2762:
1.126 brouard 2763: int i, ii,j,k;
1.209 brouard 2764: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2765: /* double **matprod2(); */ /* test */
1.218 brouard 2766: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2767: double **newm;
1.209 brouard 2768: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2769: int ncvloop=0;
1.288 brouard 2770: int first=0;
1.169 brouard 2771:
1.209 brouard 2772: min=vector(1,nlstate);
2773: max=vector(1,nlstate);
2774: meandiff=vector(1,nlstate);
2775:
1.218 brouard 2776: /* Starting with matrix unity */
1.126 brouard 2777: for (ii=1;ii<=nlstate+ndeath;ii++)
2778: for (j=1;j<=nlstate+ndeath;j++){
2779: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2780: }
1.169 brouard 2781:
2782: cov[1]=1.;
2783:
2784: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2785: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2786: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2787: ncvloop++;
1.126 brouard 2788: newm=savm;
2789: /* Covariates have to be included here again */
1.138 brouard 2790: cov[2]=agefin;
1.319 brouard 2791: if(nagesqr==1){
2792: cov[3]= agefin*agefin;
2793: }
1.234 brouard 2794: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2795: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2796: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.319 brouard 2797: /* cov[++k1]=nbcode[TvarsD[k]][codtabm(ij,k)]; */
1.235 brouard 2798: /* 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)); */
1.234 brouard 2799: }
2800: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2801: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.319 brouard 2802: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2803: /* cov[++k1]=Tqresult[nres][k]; */
1.235 brouard 2804: /* 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]); */
1.138 brouard 2805: }
1.237 brouard 2806: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.319 brouard 2807: if(Dummy[Tage[k]]==2){ /* dummy with age */
1.234 brouard 2808: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
1.319 brouard 2809: /* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
2810: } else if(Dummy[Tage[k]]==3){ /* quantitative with age */
2811: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
2812: /* cov[++k1]=Tqresult[nres][k]; */
1.234 brouard 2813: }
1.235 brouard 2814: /* 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]); */
1.234 brouard 2815: }
1.237 brouard 2816: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 2817: /* 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]); */
1.237 brouard 2818: if(Dummy[Tvard[k][1]==0]){
2819: if(Dummy[Tvard[k][2]==0]){
2820: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
1.319 brouard 2821: /* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.237 brouard 2822: }else{
2823: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
1.319 brouard 2824: /* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; */
1.237 brouard 2825: }
2826: }else{
2827: if(Dummy[Tvard[k][2]==0]){
2828: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
1.319 brouard 2829: /* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; */
1.237 brouard 2830: }else{
2831: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
1.319 brouard 2832: /* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
1.237 brouard 2833: }
2834: }
1.234 brouard 2835: }
1.138 brouard 2836: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2837: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2838: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2839: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2840: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.319 brouard 2841: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2842: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2843:
1.126 brouard 2844: savm=oldm;
2845: oldm=newm;
1.209 brouard 2846:
2847: for(j=1; j<=nlstate; j++){
2848: max[j]=0.;
2849: min[j]=1.;
2850: }
2851: for(i=1;i<=nlstate;i++){
2852: sumnew=0;
2853: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2854: for(j=1; j<=nlstate; j++){
2855: prlim[i][j]= newm[i][j]/(1-sumnew);
2856: max[j]=FMAX(max[j],prlim[i][j]);
2857: min[j]=FMIN(min[j],prlim[i][j]);
2858: }
2859: }
2860:
1.126 brouard 2861: maxmax=0.;
1.209 brouard 2862: for(j=1; j<=nlstate; j++){
2863: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2864: maxmax=FMAX(maxmax,meandiff[j]);
2865: /* 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 2866: } /* j loop */
1.203 brouard 2867: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2868: /* 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 2869: if(maxmax < ftolpl){
1.209 brouard 2870: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2871: free_vector(min,1,nlstate);
2872: free_vector(max,1,nlstate);
2873: free_vector(meandiff,1,nlstate);
1.126 brouard 2874: return prlim;
2875: }
1.288 brouard 2876: } /* agefin loop */
1.208 brouard 2877: /* After some age loop it doesn't converge */
1.288 brouard 2878: if(!first){
2879: first=1;
2880: 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 2881: 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);
2882: }else if (first >=1 && first <10){
2883: 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);
2884: first++;
2885: }else if (first ==10){
2886: 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);
2887: 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");
2888: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n");
2889: first++;
1.288 brouard 2890: }
2891:
1.209 brouard 2892: /* 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); */
2893: free_vector(min,1,nlstate);
2894: free_vector(max,1,nlstate);
2895: free_vector(meandiff,1,nlstate);
1.208 brouard 2896:
1.169 brouard 2897: return prlim; /* should not reach here */
1.126 brouard 2898: }
2899:
1.217 brouard 2900:
2901: /**** Back Prevalence limit (stable or period prevalence) ****************/
2902:
1.218 brouard 2903: /* 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) */
2904: /* 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 2905: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 2906: {
1.264 brouard 2907: /* 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 2908: matrix by transitions matrix until convergence is reached with precision ftolpl */
2909: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2910: /* Wx is row vector: population in state 1, population in state 2, population dead */
2911: /* or prevalence in state 1, prevalence in state 2, 0 */
2912: /* newm is the matrix after multiplications, its rows are identical at a factor */
2913: /* Initial matrix pimij */
2914: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2915: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2916: /* 0, 0 , 1} */
2917: /*
2918: * and after some iteration: */
2919: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2920: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2921: /* 0, 0 , 1} */
2922: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2923: /* {0.51571254859325999, 0.4842874514067399, */
2924: /* 0.51326036147820708, 0.48673963852179264} */
2925: /* If we start from prlim again, prlim tends to a constant matrix */
2926:
2927: int i, ii,j,k;
1.247 brouard 2928: int first=0;
1.217 brouard 2929: double *min, *max, *meandiff, maxmax,sumnew=0.;
2930: /* double **matprod2(); */ /* test */
2931: double **out, cov[NCOVMAX+1], **bmij();
2932: double **newm;
1.218 brouard 2933: double **dnewm, **doldm, **dsavm; /* for use */
2934: double **oldm, **savm; /* for use */
2935:
1.217 brouard 2936: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2937: int ncvloop=0;
2938:
2939: min=vector(1,nlstate);
2940: max=vector(1,nlstate);
2941: meandiff=vector(1,nlstate);
2942:
1.266 brouard 2943: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2944: oldm=oldms; savm=savms;
2945:
2946: /* Starting with matrix unity */
2947: for (ii=1;ii<=nlstate+ndeath;ii++)
2948: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2949: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2950: }
2951:
2952: cov[1]=1.;
2953:
2954: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2955: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2956: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
1.288 brouard 2957: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2958: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2959: ncvloop++;
1.218 brouard 2960: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2961: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2962: /* Covariates have to be included here again */
2963: cov[2]=agefin;
1.319 brouard 2964: if(nagesqr==1){
1.217 brouard 2965: cov[3]= agefin*agefin;;
1.319 brouard 2966: }
1.242 brouard 2967: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2968: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2969: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.264 brouard 2970: /* 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)); */
1.242 brouard 2971: }
2972: /* for (k=1; k<=cptcovn;k++) { */
2973: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
2974: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
2975: /* /\* 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])]); *\/ */
2976: /* } */
2977: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2978: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
2979: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2980: /* 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]); */
2981: }
2982: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; */
2983: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */
2984: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\/ */
2985: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
2986: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.319 brouard 2987: /* if(Dummy[Tvar[Tage[k]]]== 2){ /\* dummy with age *\/ ERROR ???*/
2988: if(Dummy[Tage[k]]== 2){ /* dummy with age */
1.242 brouard 2989: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
1.319 brouard 2990: } else if(Dummy[Tage[k]]== 3){ /* quantitative with age */
2991: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.242 brouard 2992: }
2993: /* 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]); */
2994: }
2995: for (k=1; k<=cptcovprod;k++){ /* For product without age */
2996: /* 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]); */
2997: if(Dummy[Tvard[k][1]==0]){
2998: if(Dummy[Tvard[k][2]==0]){
2999: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
3000: }else{
3001: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
3002: }
3003: }else{
3004: if(Dummy[Tvard[k][2]==0]){
3005: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
3006: }else{
3007: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
3008: }
3009: }
1.217 brouard 3010: }
3011:
3012: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
3013: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
3014: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
3015: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3016: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 3017: /* ij should be linked to the correct index of cov */
3018: /* age and covariate values ij are in 'cov', but we need to pass
3019: * ij for the observed prevalence at age and status and covariate
3020: * number: prevacurrent[(int)agefin][ii][ij]
3021: */
3022: /* 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 *\/ */
3023: /* 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 *\/ */
3024: 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 3025: /* if((int)age == 86 || (int)age == 87){ */
1.266 brouard 3026: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
3027: /* for(i=1; i<=nlstate+ndeath; i++) { */
3028: /* printf("%d newm= ",i); */
3029: /* for(j=1;j<=nlstate+ndeath;j++) { */
3030: /* printf("%f ",newm[i][j]); */
3031: /* } */
3032: /* printf("oldm * "); */
3033: /* for(j=1;j<=nlstate+ndeath;j++) { */
3034: /* printf("%f ",oldm[i][j]); */
3035: /* } */
1.268 brouard 3036: /* printf(" bmmij "); */
1.266 brouard 3037: /* for(j=1;j<=nlstate+ndeath;j++) { */
3038: /* printf("%f ",pmmij[i][j]); */
3039: /* } */
3040: /* printf("\n"); */
3041: /* } */
3042: /* } */
1.217 brouard 3043: savm=oldm;
3044: oldm=newm;
1.266 brouard 3045:
1.217 brouard 3046: for(j=1; j<=nlstate; j++){
3047: max[j]=0.;
3048: min[j]=1.;
3049: }
3050: for(j=1; j<=nlstate; j++){
3051: for(i=1;i<=nlstate;i++){
1.234 brouard 3052: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
3053: bprlim[i][j]= newm[i][j];
3054: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
3055: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 3056: }
3057: }
1.218 brouard 3058:
1.217 brouard 3059: maxmax=0.;
3060: for(i=1; i<=nlstate; i++){
1.318 brouard 3061: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column, could be nan! */
1.217 brouard 3062: maxmax=FMAX(maxmax,meandiff[i]);
3063: /* 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 3064: } /* i loop */
1.217 brouard 3065: *ncvyear= -( (int)age- (int)agefin);
1.268 brouard 3066: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 3067: if(maxmax < ftolpl){
1.220 brouard 3068: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 3069: free_vector(min,1,nlstate);
3070: free_vector(max,1,nlstate);
3071: free_vector(meandiff,1,nlstate);
3072: return bprlim;
3073: }
1.288 brouard 3074: } /* agefin loop */
1.217 brouard 3075: /* After some age loop it doesn't converge */
1.288 brouard 3076: if(!first){
1.247 brouard 3077: first=1;
3078: 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\
3079: 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);
3080: }
3081: 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 3082: 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);
3083: /* 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); */
3084: free_vector(min,1,nlstate);
3085: free_vector(max,1,nlstate);
3086: free_vector(meandiff,1,nlstate);
3087:
3088: return bprlim; /* should not reach here */
3089: }
3090:
1.126 brouard 3091: /*************** transition probabilities ***************/
3092:
3093: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
3094: {
1.138 brouard 3095: /* According to parameters values stored in x and the covariate's values stored in cov,
1.266 brouard 3096: computes the probability to be observed in state j (after stepm years) being in state i by appying the
1.138 brouard 3097: model to the ncovmodel covariates (including constant and age).
3098: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3099: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3100: ncth covariate in the global vector x is given by the formula:
3101: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3102: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3103: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3104: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1.266 brouard 3105: Outputs ps[i][j] or probability to be observed in j being in i according to
1.138 brouard 3106: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1.266 brouard 3107: Sum on j ps[i][j] should equal to 1.
1.138 brouard 3108: */
3109: double s1, lnpijopii;
1.126 brouard 3110: /*double t34;*/
1.164 brouard 3111: int i,j, nc, ii, jj;
1.126 brouard 3112:
1.223 brouard 3113: for(i=1; i<= nlstate; i++){
3114: for(j=1; j<i;j++){
3115: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3116: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3117: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3118: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3119: }
3120: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3121: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3122: }
3123: for(j=i+1; j<=nlstate+ndeath;j++){
3124: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3125: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3126: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3127: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3128: }
3129: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3130: }
3131: }
1.218 brouard 3132:
1.223 brouard 3133: for(i=1; i<= nlstate; i++){
3134: s1=0;
3135: for(j=1; j<i; j++){
3136: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3137: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3138: }
3139: for(j=i+1; j<=nlstate+ndeath; j++){
3140: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3141: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3142: }
3143: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3144: ps[i][i]=1./(s1+1.);
3145: /* Computing other pijs */
3146: for(j=1; j<i; j++)
1.325 brouard 3147: ps[i][j]= exp(ps[i][j])*ps[i][i];/* Bug valgrind */
1.223 brouard 3148: for(j=i+1; j<=nlstate+ndeath; j++)
3149: ps[i][j]= exp(ps[i][j])*ps[i][i];
3150: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3151: } /* end i */
1.218 brouard 3152:
1.223 brouard 3153: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3154: for(jj=1; jj<= nlstate+ndeath; jj++){
3155: ps[ii][jj]=0;
3156: ps[ii][ii]=1;
3157: }
3158: }
1.294 brouard 3159:
3160:
1.223 brouard 3161: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3162: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3163: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3164: /* } */
3165: /* printf("\n "); */
3166: /* } */
3167: /* printf("\n ");printf("%lf ",cov[2]);*/
3168: /*
3169: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 3170: goto end;*/
1.266 brouard 3171: return ps; /* Pointer is unchanged since its call */
1.126 brouard 3172: }
3173:
1.218 brouard 3174: /*************** backward transition probabilities ***************/
3175:
3176: /* 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 ) */
3177: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
3178: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
3179: {
1.302 brouard 3180: /* 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 3181: * 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 3182: */
1.218 brouard 3183: int i, ii, j,k;
1.222 brouard 3184:
3185: double **out, **pmij();
3186: double sumnew=0.;
1.218 brouard 3187: double agefin;
1.292 brouard 3188: 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 3189: double **dnewm, **dsavm, **doldm;
3190: double **bbmij;
3191:
1.218 brouard 3192: doldm=ddoldms; /* global pointers */
1.222 brouard 3193: dnewm=ddnewms;
3194: dsavm=ddsavms;
1.318 brouard 3195:
3196: /* Debug */
3197: /* printf("Bmij ij=%d, cov[2}=%f\n", ij, cov[2]); */
1.222 brouard 3198: agefin=cov[2];
1.268 brouard 3199: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
1.222 brouard 3200: /* bmij *//* age is cov[2], ij is included in cov, but we need for
1.266 brouard 3201: the observed prevalence (with this covariate ij) at beginning of transition */
3202: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.268 brouard 3203:
3204: /* P_x */
1.325 brouard 3205: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm *//* Bug valgrind */
1.268 brouard 3206: /* outputs pmmij which is a stochastic matrix in row */
3207:
3208: /* Diag(w_x) */
1.292 brouard 3209: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
1.268 brouard 3210: sumnew=0.;
1.269 brouard 3211: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
1.268 brouard 3212: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
1.297 brouard 3213: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
1.268 brouard 3214: sumnew+=prevacurrent[(int)agefin][ii][ij];
3215: }
3216: if(sumnew >0.01){ /* At least some value in the prevalence */
3217: for (ii=1;ii<=nlstate+ndeath;ii++){
3218: for (j=1;j<=nlstate+ndeath;j++)
1.269 brouard 3219: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
1.268 brouard 3220: }
3221: }else{
3222: for (ii=1;ii<=nlstate+ndeath;ii++){
3223: for (j=1;j<=nlstate+ndeath;j++)
3224: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
3225: }
3226: /* if(sumnew <0.9){ */
3227: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
3228: /* } */
3229: }
3230: k3=0.0; /* We put the last diagonal to 0 */
3231: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
3232: doldm[ii][ii]= k3;
3233: }
3234: /* End doldm, At the end doldm is diag[(w_i)] */
3235:
1.292 brouard 3236: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
3237: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
1.268 brouard 3238:
1.292 brouard 3239: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
1.268 brouard 3240: /* 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 3241: for (j=1;j<=nlstate+ndeath;j++){
1.268 brouard 3242: sumnew=0.;
1.222 brouard 3243: for (ii=1;ii<=nlstate;ii++){
1.266 brouard 3244: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
1.268 brouard 3245: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
1.222 brouard 3246: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
1.268 brouard 3247: for (ii=1;ii<=nlstate+ndeath;ii++){
1.222 brouard 3248: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
1.268 brouard 3249: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3250: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
1.268 brouard 3251: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3252: /* }else */
1.268 brouard 3253: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
3254: } /*End ii */
3255: } /* 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 */
3256:
1.292 brouard 3257: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
1.268 brouard 3258: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
1.222 brouard 3259: /* end bmij */
1.266 brouard 3260: return ps; /*pointer is unchanged */
1.218 brouard 3261: }
1.217 brouard 3262: /*************** transition probabilities ***************/
3263:
1.218 brouard 3264: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 3265: {
3266: /* According to parameters values stored in x and the covariate's values stored in cov,
3267: computes the probability to be observed in state j being in state i by appying the
3268: model to the ncovmodel covariates (including constant and age).
3269: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3270: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3271: ncth covariate in the global vector x is given by the formula:
3272: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3273: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3274: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3275: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3276: Outputs ps[i][j] the probability to be observed in j being in j according to
3277: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3278: */
3279: double s1, lnpijopii;
3280: /*double t34;*/
3281: int i,j, nc, ii, jj;
3282:
1.234 brouard 3283: for(i=1; i<= nlstate; i++){
3284: for(j=1; j<i;j++){
3285: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3286: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3287: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3288: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3289: }
3290: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3291: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3292: }
3293: for(j=i+1; j<=nlstate+ndeath;j++){
3294: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3295: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3296: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3297: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3298: }
3299: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3300: }
3301: }
3302:
3303: for(i=1; i<= nlstate; i++){
3304: s1=0;
3305: for(j=1; j<i; j++){
3306: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3307: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3308: }
3309: for(j=i+1; j<=nlstate+ndeath; j++){
3310: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3311: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3312: }
3313: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3314: ps[i][i]=1./(s1+1.);
3315: /* Computing other pijs */
3316: for(j=1; j<i; j++)
3317: ps[i][j]= exp(ps[i][j])*ps[i][i];
3318: for(j=i+1; j<=nlstate+ndeath; j++)
3319: ps[i][j]= exp(ps[i][j])*ps[i][i];
3320: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3321: } /* end i */
3322:
3323: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3324: for(jj=1; jj<= nlstate+ndeath; jj++){
3325: ps[ii][jj]=0;
3326: ps[ii][ii]=1;
3327: }
3328: }
1.296 brouard 3329: /* Added for prevbcast */ /* Transposed matrix too */
1.234 brouard 3330: for(jj=1; jj<= nlstate+ndeath; jj++){
3331: s1=0.;
3332: for(ii=1; ii<= nlstate+ndeath; ii++){
3333: s1+=ps[ii][jj];
3334: }
3335: for(ii=1; ii<= nlstate; ii++){
3336: ps[ii][jj]=ps[ii][jj]/s1;
3337: }
3338: }
3339: /* Transposition */
3340: for(jj=1; jj<= nlstate+ndeath; jj++){
3341: for(ii=jj; ii<= nlstate+ndeath; ii++){
3342: s1=ps[ii][jj];
3343: ps[ii][jj]=ps[jj][ii];
3344: ps[jj][ii]=s1;
3345: }
3346: }
3347: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3348: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3349: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3350: /* } */
3351: /* printf("\n "); */
3352: /* } */
3353: /* printf("\n ");printf("%lf ",cov[2]);*/
3354: /*
3355: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3356: goto end;*/
3357: return ps;
1.217 brouard 3358: }
3359:
3360:
1.126 brouard 3361: /**************** Product of 2 matrices ******************/
3362:
1.145 brouard 3363: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 3364: {
3365: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
3366: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
3367: /* in, b, out are matrice of pointers which should have been initialized
3368: before: only the contents of out is modified. The function returns
3369: a pointer to pointers identical to out */
1.145 brouard 3370: int i, j, k;
1.126 brouard 3371: for(i=nrl; i<= nrh; i++)
1.145 brouard 3372: for(k=ncolol; k<=ncoloh; k++){
3373: out[i][k]=0.;
3374: for(j=ncl; j<=nch; j++)
3375: out[i][k] +=in[i][j]*b[j][k];
3376: }
1.126 brouard 3377: return out;
3378: }
3379:
3380:
3381: /************* Higher Matrix Product ***************/
3382:
1.235 brouard 3383: 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 3384: {
1.218 brouard 3385: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 3386: 'nhstepm*hstepm*stepm' months (i.e. until
3387: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3388: nhstepm*hstepm matrices.
3389: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3390: (typically every 2 years instead of every month which is too big
3391: for the memory).
3392: Model is determined by parameters x and covariates have to be
3393: included manually here.
3394:
3395: */
3396:
3397: int i, j, d, h, k;
1.131 brouard 3398: double **out, cov[NCOVMAX+1];
1.126 brouard 3399: double **newm;
1.187 brouard 3400: double agexact;
1.214 brouard 3401: double agebegin, ageend;
1.126 brouard 3402:
3403: /* Hstepm could be zero and should return the unit matrix */
3404: for (i=1;i<=nlstate+ndeath;i++)
3405: for (j=1;j<=nlstate+ndeath;j++){
3406: oldm[i][j]=(i==j ? 1.0 : 0.0);
3407: po[i][j][0]=(i==j ? 1.0 : 0.0);
3408: }
3409: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3410: for(h=1; h <=nhstepm; h++){
3411: for(d=1; d <=hstepm; d++){
3412: newm=savm;
3413: /* Covariates have to be included here again */
3414: cov[1]=1.;
1.214 brouard 3415: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 3416: cov[2]=agexact;
1.319 brouard 3417: if(nagesqr==1){
1.227 brouard 3418: cov[3]= agexact*agexact;
1.319 brouard 3419: }
1.235 brouard 3420: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
1.319 brouard 3421: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
3422: /* codtabm(ij,k) (1 & (ij-1) >> (k-1))+1 */
3423: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
3424: /* k 1 2 3 4 5 6 7 8 9 */
3425: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
3426: /* nsd 1 2 3 */ /* Counting single dummies covar fixed or tv */
3427: /*TvarsD[nsd] 4 3 1 */ /* ID of single dummy cova fixed or timevary*/
3428: /*TvarsDind[k] 2 3 9 */ /* position K of single dummy cova */
1.235 brouard 3429: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3430: /* printf("hpxij 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)); */
3431: }
3432: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3433: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.319 brouard 3434: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
1.235 brouard 3435: /* 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]); */
3436: }
1.319 brouard 3437: for (k=1; k<=cptcovage;k++){ /* For product with age V1+V1*age +V4 +age*V3 */
3438: /* 1+2 Tage[1]=2 TVar[2]=1 Dummy[2]=2, Tage[2]=4 TVar[4]=3 Dummy[4]=3 quant*/
3439: /* */
3440: if(Dummy[Tage[k]]== 2){ /* dummy with age */
3441: /* if(Dummy[Tvar[Tage[k]]]== 2){ /\* dummy with age *\/ */
1.235 brouard 3442: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
1.319 brouard 3443: } else if(Dummy[Tage[k]]== 3){ /* quantitative with age */
3444: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.235 brouard 3445: }
3446: /* 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]); */
3447: }
1.319 brouard 3448: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 3449: /* printf("hPxij 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]); */
1.319 brouard 3450: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
3451: if(Dummy[Tvard[k][1]==0]){
3452: if(Dummy[Tvard[k][2]==0]){
3453: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
3454: }else{
3455: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
3456: }
3457: }else{
3458: if(Dummy[Tvard[k][2]==0]){
3459: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
3460: }else{
3461: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
3462: }
3463: }
1.235 brouard 3464: }
3465: /* for (k=1; k<=cptcovn;k++) */
3466: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3467: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
3468: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
3469: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
3470: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 3471:
3472:
1.126 brouard 3473: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3474: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.319 brouard 3475: /* right multiplication of oldm by the current matrix */
1.126 brouard 3476: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
3477: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 3478: /* if((int)age == 70){ */
3479: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3480: /* for(i=1; i<=nlstate+ndeath; i++) { */
3481: /* printf("%d pmmij ",i); */
3482: /* for(j=1;j<=nlstate+ndeath;j++) { */
3483: /* printf("%f ",pmmij[i][j]); */
3484: /* } */
3485: /* printf(" oldm "); */
3486: /* for(j=1;j<=nlstate+ndeath;j++) { */
3487: /* printf("%f ",oldm[i][j]); */
3488: /* } */
3489: /* printf("\n"); */
3490: /* } */
3491: /* } */
1.126 brouard 3492: savm=oldm;
3493: oldm=newm;
3494: }
3495: for(i=1; i<=nlstate+ndeath; i++)
3496: for(j=1;j<=nlstate+ndeath;j++) {
1.267 brouard 3497: po[i][j][h]=newm[i][j];
3498: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 3499: }
1.128 brouard 3500: /*printf("h=%d ",h);*/
1.126 brouard 3501: } /* end h */
1.267 brouard 3502: /* printf("\n H=%d \n",h); */
1.126 brouard 3503: return po;
3504: }
3505:
1.217 brouard 3506: /************* Higher Back Matrix Product ***************/
1.218 brouard 3507: /* 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 3508: 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 3509: {
1.266 brouard 3510: /* For a combination of dummy covariate ij, computes the transition matrix starting at age 'age' over
1.217 brouard 3511: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 3512: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3513: nhstepm*hstepm matrices.
3514: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3515: (typically every 2 years instead of every month which is too big
1.217 brouard 3516: for the memory).
1.218 brouard 3517: Model is determined by parameters x and covariates have to be
1.266 brouard 3518: included manually here. Then we use a call to bmij(x and cov)
3519: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
1.222 brouard 3520: */
1.217 brouard 3521:
3522: int i, j, d, h, k;
1.266 brouard 3523: double **out, cov[NCOVMAX+1], **bmij();
3524: double **newm, ***newmm;
1.217 brouard 3525: double agexact;
3526: double agebegin, ageend;
1.222 brouard 3527: double **oldm, **savm;
1.217 brouard 3528:
1.266 brouard 3529: newmm=po; /* To be saved */
3530: oldm=oldms;savm=savms; /* Global pointers */
1.217 brouard 3531: /* Hstepm could be zero and should return the unit matrix */
3532: for (i=1;i<=nlstate+ndeath;i++)
3533: for (j=1;j<=nlstate+ndeath;j++){
3534: oldm[i][j]=(i==j ? 1.0 : 0.0);
3535: po[i][j][0]=(i==j ? 1.0 : 0.0);
3536: }
3537: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3538: for(h=1; h <=nhstepm; h++){
3539: for(d=1; d <=hstepm; d++){
3540: newm=savm;
3541: /* Covariates have to be included here again */
3542: cov[1]=1.;
1.271 brouard 3543: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
1.217 brouard 3544: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
1.318 brouard 3545: /* Debug */
3546: /* printf("hBxij age=%lf, agexact=%lf\n", age, agexact); */
1.217 brouard 3547: cov[2]=agexact;
3548: if(nagesqr==1)
1.222 brouard 3549: cov[3]= agexact*agexact;
1.325 brouard 3550: for (k=1; k<=nsd;k++){ /* For single dummy covariates only *//* cptcovn error */
1.266 brouard 3551: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3552: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
1.325 brouard 3553: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];/* Bug valgrind */
1.266 brouard 3554: /* 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)); */
3555: }
1.267 brouard 3556: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3557: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3558: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3559: /* 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]); */
3560: }
1.319 brouard 3561: for (k=1; k<=cptcovage;k++){ /* Should start at cptcovn+1 *//* For product with age */
3562: /* if(Dummy[Tvar[Tage[k]]]== 2){ /\* dummy with age error!!!*\/ */
3563: if(Dummy[Tage[k]]== 2){ /* dummy with age */
1.267 brouard 3564: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
1.319 brouard 3565: } else if(Dummy[Tage[k]]== 3){ /* quantitative with age */
1.267 brouard 3566: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3567: }
3568: /* 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]); */
3569: }
3570: for (k=1; k<=cptcovprod;k++){ /* Useless because included in cptcovn */
1.222 brouard 3571: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.325 brouard 3572: if(Dummy[Tvard[k][1]==0]){
3573: if(Dummy[Tvard[k][2]==0]){
3574: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
3575: }else{
3576: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
3577: }
3578: }else{
3579: if(Dummy[Tvard[k][2]==0]){
3580: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
3581: }else{
3582: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
3583: }
3584: }
1.267 brouard 3585: }
1.217 brouard 3586: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3587: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.267 brouard 3588:
1.218 brouard 3589: /* Careful transposed matrix */
1.266 brouard 3590: /* age is in cov[2], prevacurrent at beginning of transition. */
1.218 brouard 3591: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 3592: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 3593: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.325 brouard 3594: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);/* Bug valgrind */
1.217 brouard 3595: /* if((int)age == 70){ */
3596: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3597: /* for(i=1; i<=nlstate+ndeath; i++) { */
3598: /* printf("%d pmmij ",i); */
3599: /* for(j=1;j<=nlstate+ndeath;j++) { */
3600: /* printf("%f ",pmmij[i][j]); */
3601: /* } */
3602: /* printf(" oldm "); */
3603: /* for(j=1;j<=nlstate+ndeath;j++) { */
3604: /* printf("%f ",oldm[i][j]); */
3605: /* } */
3606: /* printf("\n"); */
3607: /* } */
3608: /* } */
3609: savm=oldm;
3610: oldm=newm;
3611: }
3612: for(i=1; i<=nlstate+ndeath; i++)
3613: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3614: po[i][j][h]=newm[i][j];
1.268 brouard 3615: /* if(h==nhstepm) */
3616: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
1.217 brouard 3617: }
1.268 brouard 3618: /* printf("h=%d %.1f ",h, agexact); */
1.217 brouard 3619: } /* end h */
1.268 brouard 3620: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
1.217 brouard 3621: return po;
3622: }
3623:
3624:
1.162 brouard 3625: #ifdef NLOPT
3626: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3627: double fret;
3628: double *xt;
3629: int j;
3630: myfunc_data *d2 = (myfunc_data *) pd;
3631: /* xt = (p1-1); */
3632: xt=vector(1,n);
3633: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3634:
3635: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3636: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3637: printf("Function = %.12lf ",fret);
3638: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3639: printf("\n");
3640: free_vector(xt,1,n);
3641: return fret;
3642: }
3643: #endif
1.126 brouard 3644:
3645: /*************** log-likelihood *************/
3646: double func( double *x)
3647: {
1.226 brouard 3648: int i, ii, j, k, mi, d, kk;
3649: int ioffset=0;
3650: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3651: double **out;
3652: double lli; /* Individual log likelihood */
3653: int s1, s2;
1.228 brouard 3654: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
1.226 brouard 3655: double bbh, survp;
3656: long ipmx;
3657: double agexact;
3658: /*extern weight */
3659: /* We are differentiating ll according to initial status */
3660: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3661: /*for(i=1;i<imx;i++)
3662: printf(" %d\n",s[4][i]);
3663: */
1.162 brouard 3664:
1.226 brouard 3665: ++countcallfunc;
1.162 brouard 3666:
1.226 brouard 3667: cov[1]=1.;
1.126 brouard 3668:
1.226 brouard 3669: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3670: ioffset=0;
1.226 brouard 3671: if(mle==1){
3672: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3673: /* Computes the values of the ncovmodel covariates of the model
3674: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3675: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3676: to be observed in j being in i according to the model.
3677: */
1.243 brouard 3678: ioffset=2+nagesqr ;
1.233 brouard 3679: /* Fixed */
1.319 brouard 3680: for (k=1; k<=ncovf;k++){ /* For each fixed covariate dummu or quant or prod */
3681: /* # V1=sex, V2=raedyrs Quant Fixed, State=livarnb4..livarnb11, V3=iadl4..iald11, V4=adlw4..adlw11, V5=r4bmi..r11bmi */
3682: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
3683: /* 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 3684: /* TvarFind; TvarFind[1]=6, TvarFind[2]=7, TvarFind[3]=9 *//* Inverse V2(6) is first fixed (single or prod) */
1.319 brouard 3685: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (TvarFind[1]=6)*/
3686: /* V1*V2 (7) TvarFind[2]=7, TvarFind[3]=9 */
1.234 brouard 3687: }
1.226 brouard 3688: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
1.319 brouard 3689: is 5, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]=6
1.226 brouard 3690: has been calculated etc */
3691: /* For an individual i, wav[i] gives the number of effective waves */
3692: /* We compute the contribution to Likelihood of each effective transition
3693: mw[mi][i] is real wave of the mi th effectve wave */
3694: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3695: s2=s[mw[mi+1][i]][i];
3696: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3697: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3698: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3699: */
3700: for(mi=1; mi<= wav[i]-1; mi++){
1.319 brouard 3701: 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*/
3702: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; but where is the crossproduct? */
1.242 brouard 3703: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
1.234 brouard 3704: }
3705: for (ii=1;ii<=nlstate+ndeath;ii++)
3706: for (j=1;j<=nlstate+ndeath;j++){
3707: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3708: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3709: }
3710: for(d=0; d<dh[mi][i]; d++){
3711: newm=savm;
3712: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3713: cov[2]=agexact;
3714: if(nagesqr==1)
3715: cov[3]= agexact*agexact; /* Should be changed here */
3716: for (kk=1; kk<=cptcovage;kk++) {
1.318 brouard 3717: if(!FixedV[Tvar[Tage[kk]]])
3718: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
3719: else
3720: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
1.234 brouard 3721: }
3722: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3723: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3724: savm=oldm;
3725: oldm=newm;
3726: } /* end mult */
3727:
3728: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3729: /* But now since version 0.9 we anticipate for bias at large stepm.
3730: * If stepm is larger than one month (smallest stepm) and if the exact delay
3731: * (in months) between two waves is not a multiple of stepm, we rounded to
3732: * the nearest (and in case of equal distance, to the lowest) interval but now
3733: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3734: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3735: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3736: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3737: * -stepm/2 to stepm/2 .
3738: * For stepm=1 the results are the same as for previous versions of Imach.
3739: * For stepm > 1 the results are less biased than in previous versions.
3740: */
1.234 brouard 3741: s1=s[mw[mi][i]][i];
3742: s2=s[mw[mi+1][i]][i];
3743: bbh=(double)bh[mi][i]/(double)stepm;
3744: /* bias bh is positive if real duration
3745: * is higher than the multiple of stepm and negative otherwise.
3746: */
3747: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3748: if( s2 > nlstate){
3749: /* i.e. if s2 is a death state and if the date of death is known
3750: then the contribution to the likelihood is the probability to
3751: die between last step unit time and current step unit time,
3752: which is also equal to probability to die before dh
3753: minus probability to die before dh-stepm .
3754: In version up to 0.92 likelihood was computed
3755: as if date of death was unknown. Death was treated as any other
3756: health state: the date of the interview describes the actual state
3757: and not the date of a change in health state. The former idea was
3758: to consider that at each interview the state was recorded
3759: (healthy, disable or death) and IMaCh was corrected; but when we
3760: introduced the exact date of death then we should have modified
3761: the contribution of an exact death to the likelihood. This new
3762: contribution is smaller and very dependent of the step unit
3763: stepm. It is no more the probability to die between last interview
3764: and month of death but the probability to survive from last
3765: interview up to one month before death multiplied by the
3766: probability to die within a month. Thanks to Chris
3767: Jackson for correcting this bug. Former versions increased
3768: mortality artificially. The bad side is that we add another loop
3769: which slows down the processing. The difference can be up to 10%
3770: lower mortality.
3771: */
3772: /* If, at the beginning of the maximization mostly, the
3773: cumulative probability or probability to be dead is
3774: constant (ie = 1) over time d, the difference is equal to
3775: 0. out[s1][3] = savm[s1][3]: probability, being at state
3776: s1 at precedent wave, to be dead a month before current
3777: wave is equal to probability, being at state s1 at
3778: precedent wave, to be dead at mont of the current
3779: wave. Then the observed probability (that this person died)
3780: is null according to current estimated parameter. In fact,
3781: it should be very low but not zero otherwise the log go to
3782: infinity.
3783: */
1.183 brouard 3784: /* #ifdef INFINITYORIGINAL */
3785: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3786: /* #else */
3787: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3788: /* lli=log(mytinydouble); */
3789: /* else */
3790: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3791: /* #endif */
1.226 brouard 3792: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3793:
1.226 brouard 3794: } else if ( s2==-1 ) { /* alive */
3795: for (j=1,survp=0. ; j<=nlstate; j++)
3796: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3797: /*survp += out[s1][j]; */
3798: lli= log(survp);
3799: }
3800: else if (s2==-4) {
3801: for (j=3,survp=0. ; j<=nlstate; j++)
3802: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3803: lli= log(survp);
3804: }
3805: else if (s2==-5) {
3806: for (j=1,survp=0. ; j<=2; j++)
3807: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3808: lli= log(survp);
3809: }
3810: else{
3811: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3812: /* 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 */
3813: }
3814: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3815: /*if(lli ==000.0)*/
3816: /*printf("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); */
3817: ipmx +=1;
3818: sw += weight[i];
3819: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3820: /* if (lli < log(mytinydouble)){ */
3821: /* 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); */
3822: /* 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]); */
3823: /* } */
3824: } /* end of wave */
3825: } /* end of individual */
3826: } else if(mle==2){
3827: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.319 brouard 3828: ioffset=2+nagesqr ;
3829: for (k=1; k<=ncovf;k++)
3830: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];
1.226 brouard 3831: for(mi=1; mi<= wav[i]-1; mi++){
1.319 brouard 3832: for(k=1; k <= ncovv ; k++){
3833: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
3834: }
1.226 brouard 3835: for (ii=1;ii<=nlstate+ndeath;ii++)
3836: for (j=1;j<=nlstate+ndeath;j++){
3837: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3838: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3839: }
3840: for(d=0; d<=dh[mi][i]; d++){
3841: newm=savm;
3842: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3843: cov[2]=agexact;
3844: if(nagesqr==1)
3845: cov[3]= agexact*agexact;
3846: for (kk=1; kk<=cptcovage;kk++) {
3847: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3848: }
3849: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3850: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3851: savm=oldm;
3852: oldm=newm;
3853: } /* end mult */
3854:
3855: s1=s[mw[mi][i]][i];
3856: s2=s[mw[mi+1][i]][i];
3857: bbh=(double)bh[mi][i]/(double)stepm;
3858: 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 */
3859: ipmx +=1;
3860: sw += weight[i];
3861: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3862: } /* end of wave */
3863: } /* end of individual */
3864: } else if(mle==3){ /* exponential inter-extrapolation */
3865: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3866: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3867: for(mi=1; mi<= wav[i]-1; mi++){
3868: for (ii=1;ii<=nlstate+ndeath;ii++)
3869: for (j=1;j<=nlstate+ndeath;j++){
3870: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3871: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3872: }
3873: for(d=0; d<dh[mi][i]; d++){
3874: newm=savm;
3875: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3876: cov[2]=agexact;
3877: if(nagesqr==1)
3878: cov[3]= agexact*agexact;
3879: for (kk=1; kk<=cptcovage;kk++) {
3880: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3881: }
3882: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3883: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3884: savm=oldm;
3885: oldm=newm;
3886: } /* end mult */
3887:
3888: s1=s[mw[mi][i]][i];
3889: s2=s[mw[mi+1][i]][i];
3890: bbh=(double)bh[mi][i]/(double)stepm;
3891: 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 */
3892: ipmx +=1;
3893: sw += weight[i];
3894: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3895: } /* end of wave */
3896: } /* end of individual */
3897: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3898: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3899: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3900: for(mi=1; mi<= wav[i]-1; mi++){
3901: for (ii=1;ii<=nlstate+ndeath;ii++)
3902: for (j=1;j<=nlstate+ndeath;j++){
3903: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3904: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3905: }
3906: for(d=0; d<dh[mi][i]; d++){
3907: newm=savm;
3908: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3909: cov[2]=agexact;
3910: if(nagesqr==1)
3911: cov[3]= agexact*agexact;
3912: for (kk=1; kk<=cptcovage;kk++) {
3913: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3914: }
1.126 brouard 3915:
1.226 brouard 3916: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3917: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3918: savm=oldm;
3919: oldm=newm;
3920: } /* end mult */
3921:
3922: s1=s[mw[mi][i]][i];
3923: s2=s[mw[mi+1][i]][i];
3924: if( s2 > nlstate){
3925: lli=log(out[s1][s2] - savm[s1][s2]);
3926: } else if ( s2==-1 ) { /* alive */
3927: for (j=1,survp=0. ; j<=nlstate; j++)
3928: survp += out[s1][j];
3929: lli= log(survp);
3930: }else{
3931: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3932: }
3933: ipmx +=1;
3934: sw += weight[i];
3935: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3936: /* 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]); */
1.226 brouard 3937: } /* end of wave */
3938: } /* end of individual */
3939: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3940: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3941: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3942: for(mi=1; mi<= wav[i]-1; mi++){
3943: for (ii=1;ii<=nlstate+ndeath;ii++)
3944: for (j=1;j<=nlstate+ndeath;j++){
3945: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3946: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3947: }
3948: for(d=0; d<dh[mi][i]; d++){
3949: newm=savm;
3950: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3951: cov[2]=agexact;
3952: if(nagesqr==1)
3953: cov[3]= agexact*agexact;
3954: for (kk=1; kk<=cptcovage;kk++) {
3955: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3956: }
1.126 brouard 3957:
1.226 brouard 3958: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3959: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3960: savm=oldm;
3961: oldm=newm;
3962: } /* end mult */
3963:
3964: s1=s[mw[mi][i]][i];
3965: s2=s[mw[mi+1][i]][i];
3966: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3967: ipmx +=1;
3968: sw += weight[i];
3969: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3970: /*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]);*/
3971: } /* end of wave */
3972: } /* end of individual */
3973: } /* End of if */
3974: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3975: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3976: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3977: return -l;
1.126 brouard 3978: }
3979:
3980: /*************** log-likelihood *************/
3981: double funcone( double *x)
3982: {
1.228 brouard 3983: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3984: int i, ii, j, k, mi, d, kk;
1.228 brouard 3985: int ioffset=0;
1.131 brouard 3986: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3987: double **out;
3988: double lli; /* Individual log likelihood */
3989: double llt;
3990: int s1, s2;
1.228 brouard 3991: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3992:
1.126 brouard 3993: double bbh, survp;
1.187 brouard 3994: double agexact;
1.214 brouard 3995: double agebegin, ageend;
1.126 brouard 3996: /*extern weight */
3997: /* We are differentiating ll according to initial status */
3998: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3999: /*for(i=1;i<imx;i++)
4000: printf(" %d\n",s[4][i]);
4001: */
4002: cov[1]=1.;
4003:
4004: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 4005: ioffset=0;
4006: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.243 brouard 4007: /* ioffset=2+nagesqr+cptcovage; */
4008: ioffset=2+nagesqr;
1.232 brouard 4009: /* Fixed */
1.224 brouard 4010: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 4011: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
1.311 brouard 4012: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
1.232 brouard 4013: cov[ioffset+TvarFind[k]]=covar[Tvar[TvarFind[k]]][i];/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, only V1 is fixed (k=6)*/
4014: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
4015: /* cov[2+6]=covar[Tvar[6]][i]; */
4016: /* cov[2+6]=covar[2][i]; V2 */
4017: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
4018: /* cov[2+7]=covar[Tvar[7]][i]; */
4019: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
4020: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
4021: /* cov[2+9]=covar[Tvar[9]][i]; */
4022: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 4023: }
1.232 brouard 4024: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
4025: /* 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?)*\/ */
4026: /* } */
1.231 brouard 4027: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
4028: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
4029: /* } */
1.225 brouard 4030:
1.233 brouard 4031:
4032: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 4033: /* Wave varying (but not age varying) */
4034: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 4035: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
4036: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
4037: }
1.232 brouard 4038: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.242 brouard 4039: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
4040: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
4041: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
4042: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
4043: /* printf(" i=%d,mi=%d,itv=%d,TmodelInvind[itv]=%d,cotvar[mw[mi][i]][TmodelInvind[itv]][i]=%f\n", i, mi, itv, TmodelInvind[itv],cotvar[mw[mi][i]][TmodelInvind[itv]][i]); */
1.232 brouard 4044: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 4045: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
4046: /* /\* 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]); *\/ */
4047: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 4048: /* } */
1.126 brouard 4049: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 4050: for (j=1;j<=nlstate+ndeath;j++){
4051: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
4052: savm[ii][j]=(ii==j ? 1.0 : 0.0);
4053: }
1.214 brouard 4054:
4055: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
4056: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
4057: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.247 brouard 4058: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
1.242 brouard 4059: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4060: and mw[mi+1][i]. dh depends on stepm.*/
4061: newm=savm;
1.247 brouard 4062: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
1.242 brouard 4063: cov[2]=agexact;
4064: if(nagesqr==1)
4065: cov[3]= agexact*agexact;
4066: for (kk=1; kk<=cptcovage;kk++) {
4067: if(!FixedV[Tvar[Tage[kk]]])
4068: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
4069: else
4070: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
4071: }
4072: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
4073: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
4074: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
4075: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
4076: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
4077: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
4078: savm=oldm;
4079: oldm=newm;
1.126 brouard 4080: } /* end mult */
4081:
4082: s1=s[mw[mi][i]][i];
4083: s2=s[mw[mi+1][i]][i];
1.217 brouard 4084: /* if(s2==-1){ */
1.268 brouard 4085: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
1.217 brouard 4086: /* /\* exit(1); *\/ */
4087: /* } */
1.126 brouard 4088: bbh=(double)bh[mi][i]/(double)stepm;
4089: /* bias is positive if real duration
4090: * is higher than the multiple of stepm and negative otherwise.
4091: */
4092: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 4093: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 4094: } else if ( s2==-1 ) { /* alive */
1.242 brouard 4095: for (j=1,survp=0. ; j<=nlstate; j++)
4096: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
4097: lli= log(survp);
1.126 brouard 4098: }else if (mle==1){
1.242 brouard 4099: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 4100: } else if(mle==2){
1.242 brouard 4101: 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 4102: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 4103: 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 4104: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 4105: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 4106: } else{ /* mle=0 back to 1 */
1.242 brouard 4107: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
4108: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 4109: } /* End of if */
4110: ipmx +=1;
4111: sw += weight[i];
4112: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 4113: /*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]); */
1.126 brouard 4114: if(globpr){
1.246 brouard 4115: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 4116: %11.6f %11.6f %11.6f ", \
1.242 brouard 4117: 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 4118: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
1.242 brouard 4119: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
4120: llt +=ll[k]*gipmx/gsw;
4121: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
4122: }
4123: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 4124: }
1.232 brouard 4125: } /* end of wave */
4126: } /* end of individual */
4127: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
4128: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
4129: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
4130: if(globpr==0){ /* First time we count the contributions and weights */
4131: gipmx=ipmx;
4132: gsw=sw;
4133: }
4134: return -l;
1.126 brouard 4135: }
4136:
4137:
4138: /*************** function likelione ***********/
1.292 brouard 4139: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
1.126 brouard 4140: {
4141: /* This routine should help understanding what is done with
4142: the selection of individuals/waves and
4143: to check the exact contribution to the likelihood.
4144: Plotting could be done.
4145: */
4146: int k;
4147:
4148: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 4149: strcpy(fileresilk,"ILK_");
1.202 brouard 4150: strcat(fileresilk,fileresu);
1.126 brouard 4151: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
4152: printf("Problem with resultfile: %s\n", fileresilk);
4153: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
4154: }
1.214 brouard 4155: 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");
4156: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 4157: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
4158: for(k=1; k<=nlstate; k++)
4159: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
4160: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
4161: }
4162:
1.292 brouard 4163: *fretone=(*func)(p);
1.126 brouard 4164: if(*globpri !=0){
4165: fclose(ficresilk);
1.205 brouard 4166: if (mle ==0)
4167: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
4168: else if(mle >=1)
4169: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
4170: 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 4171: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
1.208 brouard 4172:
4173: for (k=1; k<= nlstate ; k++) {
1.211 brouard 4174: 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> \
1.208 brouard 4175: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
4176: }
1.207 brouard 4177: 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.204 brouard 4178: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4179: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 4180: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4181: fflush(fichtm);
1.205 brouard 4182: }
1.126 brouard 4183: return;
4184: }
4185:
4186:
4187: /*********** Maximum Likelihood Estimation ***************/
4188:
4189: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
4190: {
1.319 brouard 4191: int i,j,k, jk, jkk=0, iter=0;
1.126 brouard 4192: double **xi;
4193: double fret;
4194: double fretone; /* Only one call to likelihood */
4195: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 4196:
4197: #ifdef NLOPT
4198: int creturn;
4199: nlopt_opt opt;
4200: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
4201: double *lb;
4202: double minf; /* the minimum objective value, upon return */
4203: double * p1; /* Shifted parameters from 0 instead of 1 */
4204: myfunc_data dinst, *d = &dinst;
4205: #endif
4206:
4207:
1.126 brouard 4208: xi=matrix(1,npar,1,npar);
4209: for (i=1;i<=npar;i++)
4210: for (j=1;j<=npar;j++)
4211: xi[i][j]=(i==j ? 1.0 : 0.0);
4212: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 4213: strcpy(filerespow,"POW_");
1.126 brouard 4214: strcat(filerespow,fileres);
4215: if((ficrespow=fopen(filerespow,"w"))==NULL) {
4216: printf("Problem with resultfile: %s\n", filerespow);
4217: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
4218: }
4219: fprintf(ficrespow,"# Powell\n# iter -2*LL");
4220: for (i=1;i<=nlstate;i++)
4221: for(j=1;j<=nlstate+ndeath;j++)
4222: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
4223: fprintf(ficrespow,"\n");
1.162 brouard 4224: #ifdef POWELL
1.319 brouard 4225: #ifdef LINMINORIGINAL
4226: #else /* LINMINORIGINAL */
4227:
4228: flatdir=ivector(1,npar);
4229: for (j=1;j<=npar;j++) flatdir[j]=0;
4230: #endif /*LINMINORIGINAL */
4231:
4232: #ifdef FLATSUP
4233: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
4234: /* reorganizing p by suppressing flat directions */
4235: for(i=1, jk=1; i <=nlstate; i++){
4236: for(k=1; k <=(nlstate+ndeath); k++){
4237: if (k != i) {
4238: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
4239: if(flatdir[jk]==1){
4240: printf(" To be skipped %d%d flatdir[%d]=%d ",i,k,jk, flatdir[jk]);
4241: }
4242: for(j=1; j <=ncovmodel; j++){
4243: printf("%12.7f ",p[jk]);
4244: jk++;
4245: }
4246: printf("\n");
4247: }
4248: }
4249: }
4250: /* skipping */
4251: /* for(i=1, jk=1, jkk=1;(flatdir[jk]==0)&& (i <=nlstate); i++){ */
4252: for(i=1, jk=1, jkk=1;i <=nlstate; i++){
4253: for(k=1; k <=(nlstate+ndeath); k++){
4254: if (k != i) {
4255: printf("%d%d flatdir[%d]=%d",i,k,jk, flatdir[jk]);
4256: if(flatdir[jk]==1){
4257: printf(" To be skipped %d%d flatdir[%d]=%d jk=%d p[%d] ",i,k,jk, flatdir[jk],jk, jk);
4258: for(j=1; j <=ncovmodel; jk++,j++){
4259: printf(" p[%d]=%12.7f",jk, p[jk]);
4260: /*q[jjk]=p[jk];*/
4261: }
4262: }else{
4263: printf(" To be kept %d%d flatdir[%d]=%d jk=%d q[%d]=p[%d] ",i,k,jk, flatdir[jk],jk, jkk, jk);
4264: for(j=1; j <=ncovmodel; jk++,jkk++,j++){
4265: printf(" p[%d]=%12.7f=q[%d]",jk, p[jk],jkk);
4266: /*q[jjk]=p[jk];*/
4267: }
4268: }
4269: printf("\n");
4270: }
4271: fflush(stdout);
4272: }
4273: }
4274: powell(p,xi,npar,ftol,&iter,&fret,flatdir,func);
4275: #else /* FLATSUP */
1.126 brouard 4276: powell(p,xi,npar,ftol,&iter,&fret,func);
1.319 brouard 4277: #endif /* FLATSUP */
4278:
4279: #ifdef LINMINORIGINAL
4280: #else
4281: free_ivector(flatdir,1,npar);
4282: #endif /* LINMINORIGINAL*/
4283: #endif /* POWELL */
1.126 brouard 4284:
1.162 brouard 4285: #ifdef NLOPT
4286: #ifdef NEWUOA
4287: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
4288: #else
4289: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
4290: #endif
4291: lb=vector(0,npar-1);
4292: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
4293: nlopt_set_lower_bounds(opt, lb);
4294: nlopt_set_initial_step1(opt, 0.1);
4295:
4296: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
4297: d->function = func;
4298: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
4299: nlopt_set_min_objective(opt, myfunc, d);
4300: nlopt_set_xtol_rel(opt, ftol);
4301: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
4302: printf("nlopt failed! %d\n",creturn);
4303: }
4304: else {
4305: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
4306: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
4307: iter=1; /* not equal */
4308: }
4309: nlopt_destroy(opt);
4310: #endif
1.319 brouard 4311: #ifdef FLATSUP
4312: /* npared = npar -flatd/ncovmodel; */
4313: /* xired= matrix(1,npared,1,npared); */
4314: /* paramred= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */
4315: /* powell(pred,xired,npared,ftol,&iter,&fret,flatdir,func); */
4316: /* free_matrix(xire,1,npared,1,npared); */
4317: #else /* FLATSUP */
4318: #endif /* FLATSUP */
1.126 brouard 4319: free_matrix(xi,1,npar,1,npar);
4320: fclose(ficrespow);
1.203 brouard 4321: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4322: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 4323: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 4324:
4325: }
4326:
4327: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 4328: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 4329: {
4330: double **a,**y,*x,pd;
1.203 brouard 4331: /* double **hess; */
1.164 brouard 4332: int i, j;
1.126 brouard 4333: int *indx;
4334:
4335: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 4336: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 4337: void lubksb(double **a, int npar, int *indx, double b[]) ;
4338: void ludcmp(double **a, int npar, int *indx, double *d) ;
4339: double gompertz(double p[]);
1.203 brouard 4340: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 4341:
4342: printf("\nCalculation of the hessian matrix. Wait...\n");
4343: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
4344: for (i=1;i<=npar;i++){
1.203 brouard 4345: printf("%d-",i);fflush(stdout);
4346: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 4347:
4348: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
4349:
4350: /* printf(" %f ",p[i]);
4351: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
4352: }
4353:
4354: for (i=1;i<=npar;i++) {
4355: for (j=1;j<=npar;j++) {
4356: if (j>i) {
1.203 brouard 4357: printf(".%d-%d",i,j);fflush(stdout);
4358: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
4359: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 4360:
4361: hess[j][i]=hess[i][j];
4362: /*printf(" %lf ",hess[i][j]);*/
4363: }
4364: }
4365: }
4366: printf("\n");
4367: fprintf(ficlog,"\n");
4368:
4369: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
4370: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
4371:
4372: a=matrix(1,npar,1,npar);
4373: y=matrix(1,npar,1,npar);
4374: x=vector(1,npar);
4375: indx=ivector(1,npar);
4376: for (i=1;i<=npar;i++)
4377: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
4378: ludcmp(a,npar,indx,&pd);
4379:
4380: for (j=1;j<=npar;j++) {
4381: for (i=1;i<=npar;i++) x[i]=0;
4382: x[j]=1;
4383: lubksb(a,npar,indx,x);
4384: for (i=1;i<=npar;i++){
4385: matcov[i][j]=x[i];
4386: }
4387: }
4388:
4389: printf("\n#Hessian matrix#\n");
4390: fprintf(ficlog,"\n#Hessian matrix#\n");
4391: for (i=1;i<=npar;i++) {
4392: for (j=1;j<=npar;j++) {
1.203 brouard 4393: printf("%.6e ",hess[i][j]);
4394: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 4395: }
4396: printf("\n");
4397: fprintf(ficlog,"\n");
4398: }
4399:
1.203 brouard 4400: /* printf("\n#Covariance matrix#\n"); */
4401: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
4402: /* for (i=1;i<=npar;i++) { */
4403: /* for (j=1;j<=npar;j++) { */
4404: /* printf("%.6e ",matcov[i][j]); */
4405: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
4406: /* } */
4407: /* printf("\n"); */
4408: /* fprintf(ficlog,"\n"); */
4409: /* } */
4410:
1.126 brouard 4411: /* Recompute Inverse */
1.203 brouard 4412: /* for (i=1;i<=npar;i++) */
4413: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
4414: /* ludcmp(a,npar,indx,&pd); */
4415:
4416: /* printf("\n#Hessian matrix recomputed#\n"); */
4417:
4418: /* for (j=1;j<=npar;j++) { */
4419: /* for (i=1;i<=npar;i++) x[i]=0; */
4420: /* x[j]=1; */
4421: /* lubksb(a,npar,indx,x); */
4422: /* for (i=1;i<=npar;i++){ */
4423: /* y[i][j]=x[i]; */
4424: /* printf("%.3e ",y[i][j]); */
4425: /* fprintf(ficlog,"%.3e ",y[i][j]); */
4426: /* } */
4427: /* printf("\n"); */
4428: /* fprintf(ficlog,"\n"); */
4429: /* } */
4430:
4431: /* Verifying the inverse matrix */
4432: #ifdef DEBUGHESS
4433: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 4434:
1.203 brouard 4435: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
4436: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 4437:
4438: for (j=1;j<=npar;j++) {
4439: for (i=1;i<=npar;i++){
1.203 brouard 4440: printf("%.2f ",y[i][j]);
4441: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 4442: }
4443: printf("\n");
4444: fprintf(ficlog,"\n");
4445: }
1.203 brouard 4446: #endif
1.126 brouard 4447:
4448: free_matrix(a,1,npar,1,npar);
4449: free_matrix(y,1,npar,1,npar);
4450: free_vector(x,1,npar);
4451: free_ivector(indx,1,npar);
1.203 brouard 4452: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 4453:
4454:
4455: }
4456:
4457: /*************** hessian matrix ****************/
4458: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 4459: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 4460: int i;
4461: int l=1, lmax=20;
1.203 brouard 4462: double k1,k2, res, fx;
1.132 brouard 4463: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 4464: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
4465: int k=0,kmax=10;
4466: double l1;
4467:
4468: fx=func(x);
4469: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 4470: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 4471: l1=pow(10,l);
4472: delts=delt;
4473: for(k=1 ; k <kmax; k=k+1){
4474: delt = delta*(l1*k);
4475: p2[theta]=x[theta] +delt;
1.145 brouard 4476: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 4477: p2[theta]=x[theta]-delt;
4478: k2=func(p2)-fx;
4479: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 4480: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 4481:
1.203 brouard 4482: #ifdef DEBUGHESSII
1.126 brouard 4483: 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);
4484: 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);
4485: #endif
4486: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
4487: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
4488: k=kmax;
4489: }
4490: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 4491: k=kmax; l=lmax*10;
1.126 brouard 4492: }
4493: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
4494: delts=delt;
4495: }
1.203 brouard 4496: } /* End loop k */
1.126 brouard 4497: }
4498: delti[theta]=delts;
4499: return res;
4500:
4501: }
4502:
1.203 brouard 4503: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 4504: {
4505: int i;
1.164 brouard 4506: int l=1, lmax=20;
1.126 brouard 4507: double k1,k2,k3,k4,res,fx;
1.132 brouard 4508: double p2[MAXPARM+1];
1.203 brouard 4509: int k, kmax=1;
4510: double v1, v2, cv12, lc1, lc2;
1.208 brouard 4511:
4512: int firstime=0;
1.203 brouard 4513:
1.126 brouard 4514: fx=func(x);
1.203 brouard 4515: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 4516: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 4517: p2[thetai]=x[thetai]+delti[thetai]*k;
4518: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4519: k1=func(p2)-fx;
4520:
1.203 brouard 4521: p2[thetai]=x[thetai]+delti[thetai]*k;
4522: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4523: k2=func(p2)-fx;
4524:
1.203 brouard 4525: p2[thetai]=x[thetai]-delti[thetai]*k;
4526: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4527: k3=func(p2)-fx;
4528:
1.203 brouard 4529: p2[thetai]=x[thetai]-delti[thetai]*k;
4530: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4531: k4=func(p2)-fx;
1.203 brouard 4532: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
4533: if(k1*k2*k3*k4 <0.){
1.208 brouard 4534: firstime=1;
1.203 brouard 4535: kmax=kmax+10;
1.208 brouard 4536: }
4537: if(kmax >=10 || firstime ==1){
1.246 brouard 4538: 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);
4539: 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 4540: 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);
4541: 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);
4542: }
4543: #ifdef DEBUGHESSIJ
4544: v1=hess[thetai][thetai];
4545: v2=hess[thetaj][thetaj];
4546: cv12=res;
4547: /* Computing eigen value of Hessian matrix */
4548: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4549: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4550: if ((lc2 <0) || (lc1 <0) ){
4551: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4552: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4553: 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);
4554: 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);
4555: }
1.126 brouard 4556: #endif
4557: }
4558: return res;
4559: }
4560:
1.203 brouard 4561: /* Not done yet: Was supposed to fix if not exactly at the maximum */
4562: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
4563: /* { */
4564: /* int i; */
4565: /* int l=1, lmax=20; */
4566: /* double k1,k2,k3,k4,res,fx; */
4567: /* double p2[MAXPARM+1]; */
4568: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
4569: /* int k=0,kmax=10; */
4570: /* double l1; */
4571:
4572: /* fx=func(x); */
4573: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
4574: /* l1=pow(10,l); */
4575: /* delts=delt; */
4576: /* for(k=1 ; k <kmax; k=k+1){ */
4577: /* delt = delti*(l1*k); */
4578: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
4579: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4580: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4581: /* k1=func(p2)-fx; */
4582:
4583: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4584: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4585: /* k2=func(p2)-fx; */
4586:
4587: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4588: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4589: /* k3=func(p2)-fx; */
4590:
4591: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4592: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4593: /* k4=func(p2)-fx; */
4594: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
4595: /* #ifdef DEBUGHESSIJ */
4596: /* 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); */
4597: /* 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); */
4598: /* #endif */
4599: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
4600: /* k=kmax; */
4601: /* } */
4602: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
4603: /* k=kmax; l=lmax*10; */
4604: /* } */
4605: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
4606: /* delts=delt; */
4607: /* } */
4608: /* } /\* End loop k *\/ */
4609: /* } */
4610: /* delti[theta]=delts; */
4611: /* return res; */
4612: /* } */
4613:
4614:
1.126 brouard 4615: /************** Inverse of matrix **************/
4616: void ludcmp(double **a, int n, int *indx, double *d)
4617: {
4618: int i,imax,j,k;
4619: double big,dum,sum,temp;
4620: double *vv;
4621:
4622: vv=vector(1,n);
4623: *d=1.0;
4624: for (i=1;i<=n;i++) {
4625: big=0.0;
4626: for (j=1;j<=n;j++)
4627: if ((temp=fabs(a[i][j])) > big) big=temp;
1.256 brouard 4628: if (big == 0.0){
4629: printf(" Singular Hessian matrix at row %d:\n",i);
4630: for (j=1;j<=n;j++) {
4631: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
4632: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
4633: }
4634: fflush(ficlog);
4635: fclose(ficlog);
4636: nrerror("Singular matrix in routine ludcmp");
4637: }
1.126 brouard 4638: vv[i]=1.0/big;
4639: }
4640: for (j=1;j<=n;j++) {
4641: for (i=1;i<j;i++) {
4642: sum=a[i][j];
4643: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
4644: a[i][j]=sum;
4645: }
4646: big=0.0;
4647: for (i=j;i<=n;i++) {
4648: sum=a[i][j];
4649: for (k=1;k<j;k++)
4650: sum -= a[i][k]*a[k][j];
4651: a[i][j]=sum;
4652: if ( (dum=vv[i]*fabs(sum)) >= big) {
4653: big=dum;
4654: imax=i;
4655: }
4656: }
4657: if (j != imax) {
4658: for (k=1;k<=n;k++) {
4659: dum=a[imax][k];
4660: a[imax][k]=a[j][k];
4661: a[j][k]=dum;
4662: }
4663: *d = -(*d);
4664: vv[imax]=vv[j];
4665: }
4666: indx[j]=imax;
4667: if (a[j][j] == 0.0) a[j][j]=TINY;
4668: if (j != n) {
4669: dum=1.0/(a[j][j]);
4670: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4671: }
4672: }
4673: free_vector(vv,1,n); /* Doesn't work */
4674: ;
4675: }
4676:
4677: void lubksb(double **a, int n, int *indx, double b[])
4678: {
4679: int i,ii=0,ip,j;
4680: double sum;
4681:
4682: for (i=1;i<=n;i++) {
4683: ip=indx[i];
4684: sum=b[ip];
4685: b[ip]=b[i];
4686: if (ii)
4687: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4688: else if (sum) ii=i;
4689: b[i]=sum;
4690: }
4691: for (i=n;i>=1;i--) {
4692: sum=b[i];
4693: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4694: b[i]=sum/a[i][i];
4695: }
4696: }
4697:
4698: void pstamp(FILE *fichier)
4699: {
1.196 brouard 4700: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4701: }
4702:
1.297 brouard 4703: void date2dmy(double date,double *day, double *month, double *year){
4704: double yp=0., yp1=0., yp2=0.;
4705:
4706: yp1=modf(date,&yp);/* extracts integral of date in yp and
4707: fractional in yp1 */
4708: *year=yp;
4709: yp2=modf((yp1*12),&yp);
4710: *month=yp;
4711: yp1=modf((yp2*30.5),&yp);
4712: *day=yp;
4713: if(*day==0) *day=1;
4714: if(*month==0) *month=1;
4715: }
4716:
1.253 brouard 4717:
4718:
1.126 brouard 4719: /************ Frequencies ********************/
1.251 brouard 4720: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
1.226 brouard 4721: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4722: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.250 brouard 4723: { /* Some frequencies as well as proposing some starting values */
1.226 brouard 4724:
1.265 brouard 4725: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
1.226 brouard 4726: int iind=0, iage=0;
4727: int mi; /* Effective wave */
4728: int first;
4729: double ***freq; /* Frequencies */
1.268 brouard 4730: 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 */
4731: 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 4732: double *meanq, *stdq, *idq;
1.226 brouard 4733: double **meanqt;
4734: double *pp, **prop, *posprop, *pospropt;
4735: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4736: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4737: double agebegin, ageend;
4738:
4739: pp=vector(1,nlstate);
1.251 brouard 4740: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4741: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4742: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4743: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4744: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.284 brouard 4745: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.283 brouard 4746: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.226 brouard 4747: meanqt=matrix(1,lastpass,1,nqtveff);
4748: strcpy(fileresp,"P_");
4749: strcat(fileresp,fileresu);
4750: /*strcat(fileresphtm,fileresu);*/
4751: if((ficresp=fopen(fileresp,"w"))==NULL) {
4752: printf("Problem with prevalence resultfile: %s\n", fileresp);
4753: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4754: exit(0);
4755: }
1.240 brouard 4756:
1.226 brouard 4757: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4758: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4759: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4760: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4761: fflush(ficlog);
4762: exit(70);
4763: }
4764: else{
4765: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 4766: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4767: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4768: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4769: }
1.319 brouard 4770: 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 4771:
1.226 brouard 4772: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4773: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4774: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4775: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4776: fflush(ficlog);
4777: exit(70);
1.240 brouard 4778: } else{
1.226 brouard 4779: 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 4780: ,<hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4781: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4782: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4783: }
1.319 brouard 4784: 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 4785:
1.253 brouard 4786: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
4787: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.251 brouard 4788: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4789: j1=0;
1.126 brouard 4790:
1.227 brouard 4791: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4792: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4793: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 4794:
4795:
1.226 brouard 4796: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4797: reference=low_education V1=0,V2=0
4798: med_educ V1=1 V2=0,
4799: high_educ V1=0 V2=1
4800: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4801: */
1.249 brouard 4802: dateintsum=0;
4803: k2cpt=0;
4804:
1.253 brouard 4805: if(cptcoveff == 0 )
1.265 brouard 4806: nl=1; /* Constant and age model only */
1.253 brouard 4807: else
4808: nl=2;
1.265 brouard 4809:
4810: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
4811: /* Loop on nj=1 or 2 if dummy covariates j!=0
4812: * Loop on j1(1 to 2**cptcoveff) covariate combination
4813: * freq[s1][s2][iage] =0.
4814: * Loop on iind
4815: * ++freq[s1][s2][iage] weighted
4816: * end iind
4817: * if covariate and j!0
4818: * headers Variable on one line
4819: * endif cov j!=0
4820: * header of frequency table by age
4821: * Loop on age
4822: * pp[s1]+=freq[s1][s2][iage] weighted
4823: * pos+=freq[s1][s2][iage] weighted
4824: * Loop on s1 initial state
4825: * fprintf(ficresp
4826: * end s1
4827: * end age
4828: * if j!=0 computes starting values
4829: * end compute starting values
4830: * end j1
4831: * end nl
4832: */
1.253 brouard 4833: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
4834: if(nj==1)
4835: j=0; /* First pass for the constant */
1.265 brouard 4836: else{
1.253 brouard 4837: j=cptcoveff; /* Other passes for the covariate values */
1.265 brouard 4838: }
1.251 brouard 4839: first=1;
1.265 brouard 4840: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on all covariates combination of the model, excluding quantitatives, V4=0, V3=0 for example, fixed or varying covariates */
1.251 brouard 4841: posproptt=0.;
4842: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4843: scanf("%d", i);*/
4844: for (i=-5; i<=nlstate+ndeath; i++)
1.265 brouard 4845: for (s2=-5; s2<=nlstate+ndeath; s2++)
1.251 brouard 4846: for(m=iagemin; m <= iagemax+3; m++)
1.265 brouard 4847: freq[i][s2][m]=0;
1.251 brouard 4848:
4849: for (i=1; i<=nlstate; i++) {
1.240 brouard 4850: for(m=iagemin; m <= iagemax+3; m++)
1.251 brouard 4851: prop[i][m]=0;
4852: posprop[i]=0;
4853: pospropt[i]=0;
4854: }
1.283 brouard 4855: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
1.284 brouard 4856: idq[z1]=0.;
4857: meanq[z1]=0.;
4858: stdq[z1]=0.;
1.283 brouard 4859: }
4860: /* for (z1=1; z1<= nqtveff; z1++) { */
1.251 brouard 4861: /* for(m=1;m<=lastpass;m++){ */
1.283 brouard 4862: /* meanqt[m][z1]=0.; */
4863: /* } */
4864: /* } */
1.251 brouard 4865: /* dateintsum=0; */
4866: /* k2cpt=0; */
4867:
1.265 brouard 4868: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
1.251 brouard 4869: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4870: bool=1;
4871: if(j !=0){
4872: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
4873: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
4874: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
4875: /* if(Tvaraff[z1] ==-20){ */
4876: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
4877: /* }else if(Tvaraff[z1] ==-10){ */
4878: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
4879: /* }else */
4880: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){ /* for combination j1 of covariates */
1.265 brouard 4881: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
1.251 brouard 4882: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
4883: /* 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",
4884: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
4885: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
4886: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
4887: } /* Onlyf fixed */
4888: } /* end z1 */
4889: } /* cptcovn > 0 */
4890: } /* end any */
4891: }/* end j==0 */
1.265 brouard 4892: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
1.251 brouard 4893: /* for(m=firstpass; m<=lastpass; m++){ */
1.284 brouard 4894: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
1.251 brouard 4895: m=mw[mi][iind];
4896: if(j!=0){
4897: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
4898: for (z1=1; z1<=cptcoveff; z1++) {
4899: if( Fixed[Tmodelind[z1]]==1){
4900: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4901: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality. If covariate's
4902: value is -1, we don't select. It differs from the
4903: constant and age model which counts them. */
4904: bool=0; /* not selected */
4905: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
4906: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4907: bool=0;
4908: }
4909: }
4910: }
4911: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
4912: } /* end j==0 */
4913: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
1.284 brouard 4914: if(bool==1){ /*Selected */
1.251 brouard 4915: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
4916: and mw[mi+1][iind]. dh depends on stepm. */
4917: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
4918: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
4919: if(m >=firstpass && m <=lastpass){
4920: k2=anint[m][iind]+(mint[m][iind]/12.);
4921: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
4922: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
4923: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
4924: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
4925: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4926: if (m<lastpass) {
4927: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
4928: /* 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]); */
4929: if(s[m][iind]==-1)
4930: 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.));
4931: 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 4932: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
4933: if(!isnan(covar[ncovcol+z1][iind])){
4934: idq[z1]=idq[z1]+weight[iind];
4935: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
4936: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
4937: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
4938: }
1.284 brouard 4939: }
1.251 brouard 4940: /* if((int)agev[m][iind] == 55) */
4941: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
4942: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
4943: 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 4944: }
1.251 brouard 4945: } /* end if between passes */
4946: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
4947: dateintsum=dateintsum+k2; /* on all covariates ?*/
4948: k2cpt++;
4949: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
1.234 brouard 4950: }
1.251 brouard 4951: }else{
4952: bool=1;
4953: }/* end bool 2 */
4954: } /* end m */
1.284 brouard 4955: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
4956: /* idq[z1]=idq[z1]+weight[iind]; */
4957: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
4958: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
4959: /* } */
1.251 brouard 4960: } /* end bool */
4961: } /* end iind = 1 to imx */
1.319 brouard 4962: /* prop[s][age] is fed for any initial and valid live state as well as
1.251 brouard 4963: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
4964:
4965:
4966: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.265 brouard 4967: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
4968: pstamp(ficresp);
1.251 brouard 4969: if (cptcoveff>0 && j!=0){
1.265 brouard 4970: pstamp(ficresp);
1.251 brouard 4971: printf( "\n#********** Variable ");
4972: fprintf(ficresp, "\n#********** Variable ");
4973: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4974: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
4975: fprintf(ficlog, "\n#********** Variable ");
4976: for (z1=1; z1<=cptcoveff; z1++){
4977: if(!FixedV[Tvaraff[z1]]){
4978: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4979: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4980: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4981: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4982: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.250 brouard 4983: }else{
1.251 brouard 4984: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4985: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4986: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4987: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4988: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4989: }
4990: }
4991: printf( "**********\n#");
4992: fprintf(ficresp, "**********\n#");
4993: fprintf(ficresphtm, "**********</h3>\n");
4994: fprintf(ficresphtmfr, "**********</h3>\n");
4995: fprintf(ficlog, "**********\n");
4996: }
1.284 brouard 4997: /*
4998: Printing means of quantitative variables if any
4999: */
5000: for (z1=1; z1<= nqfveff; z1++) {
1.311 brouard 5001: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
1.312 brouard 5002: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
1.284 brouard 5003: if(weightopt==1){
5004: printf(" Weighted mean and standard deviation of");
5005: fprintf(ficlog," Weighted mean and standard deviation of");
5006: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
5007: }
1.311 brouard 5008: /* mu = \frac{w x}{\sum w}
5009: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
5010: */
5011: 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]));
5012: 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]));
5013: 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 5014: }
5015: /* for (z1=1; z1<= nqtveff; z1++) { */
5016: /* for(m=1;m<=lastpass;m++){ */
5017: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
5018: /* } */
5019: /* } */
1.283 brouard 5020:
1.251 brouard 5021: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.265 brouard 5022: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
5023: fprintf(ficresp, " Age");
5024: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.251 brouard 5025: for(i=1; i<=nlstate;i++) {
1.265 brouard 5026: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
1.251 brouard 5027: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
5028: }
1.265 brouard 5029: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
1.251 brouard 5030: fprintf(ficresphtm, "\n");
5031:
5032: /* Header of frequency table by age */
5033: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
5034: fprintf(ficresphtmfr,"<th>Age</th> ");
1.265 brouard 5035: for(s2=-1; s2 <=nlstate+ndeath; s2++){
1.251 brouard 5036: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 5037: if(s2!=0 && m!=0)
5038: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
1.240 brouard 5039: }
1.226 brouard 5040: }
1.251 brouard 5041: fprintf(ficresphtmfr, "\n");
5042:
5043: /* For each age */
5044: for(iage=iagemin; iage <= iagemax+3; iage++){
5045: fprintf(ficresphtm,"<tr>");
5046: if(iage==iagemax+1){
5047: fprintf(ficlog,"1");
5048: fprintf(ficresphtmfr,"<tr><th>0</th> ");
5049: }else if(iage==iagemax+2){
5050: fprintf(ficlog,"0");
5051: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
5052: }else if(iage==iagemax+3){
5053: fprintf(ficlog,"Total");
5054: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
5055: }else{
1.240 brouard 5056: if(first==1){
1.251 brouard 5057: first=0;
5058: printf("See log file for details...\n");
5059: }
5060: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
5061: fprintf(ficlog,"Age %d", iage);
5062: }
1.265 brouard 5063: for(s1=1; s1 <=nlstate ; s1++){
5064: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
5065: pp[s1] += freq[s1][m][iage];
1.251 brouard 5066: }
1.265 brouard 5067: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 5068: for(m=-1, pos=0; m <=0 ; m++)
1.265 brouard 5069: pos += freq[s1][m][iage];
5070: if(pp[s1]>=1.e-10){
1.251 brouard 5071: if(first==1){
1.265 brouard 5072: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 5073: }
1.265 brouard 5074: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 5075: }else{
5076: if(first==1)
1.265 brouard 5077: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
5078: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
1.240 brouard 5079: }
5080: }
5081:
1.265 brouard 5082: for(s1=1; s1 <=nlstate ; s1++){
5083: /* posprop[s1]=0; */
5084: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
5085: pp[s1] += freq[s1][m][iage];
5086: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
5087:
5088: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
5089: pos += pp[s1]; /* pos is the total number of transitions until this age */
5090: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
5091: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
5092: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
5093: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
5094: }
5095:
5096: /* Writing ficresp */
5097: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
5098: if( iage <= iagemax){
5099: fprintf(ficresp," %d",iage);
5100: }
5101: }else if( nj==2){
5102: if( iage <= iagemax){
5103: fprintf(ficresp," %d",iage);
5104: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
5105: }
1.240 brouard 5106: }
1.265 brouard 5107: for(s1=1; s1 <=nlstate ; s1++){
1.240 brouard 5108: if(pos>=1.e-5){
1.251 brouard 5109: if(first==1)
1.265 brouard 5110: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
5111: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
1.251 brouard 5112: }else{
5113: if(first==1)
1.265 brouard 5114: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
5115: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
1.251 brouard 5116: }
5117: if( iage <= iagemax){
5118: if(pos>=1.e-5){
1.265 brouard 5119: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
5120: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5121: }else if( nj==2){
5122: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5123: }
5124: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
5125: /*probs[iage][s1][j1]= pp[s1]/pos;*/
5126: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
5127: } else{
5128: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
5129: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
1.251 brouard 5130: }
1.240 brouard 5131: }
1.265 brouard 5132: pospropt[s1] +=posprop[s1];
5133: } /* end loop s1 */
1.251 brouard 5134: /* pospropt=0.; */
1.265 brouard 5135: for(s1=-1; s1 <=nlstate+ndeath; s1++){
1.251 brouard 5136: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 5137: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
1.251 brouard 5138: if(first==1){
1.265 brouard 5139: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 5140: }
1.265 brouard 5141: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
5142: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 5143: }
1.265 brouard 5144: if(s1!=0 && m!=0)
5145: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
1.240 brouard 5146: }
1.265 brouard 5147: } /* end loop s1 */
1.251 brouard 5148: posproptt=0.;
1.265 brouard 5149: for(s1=1; s1 <=nlstate; s1++){
5150: posproptt += pospropt[s1];
1.251 brouard 5151: }
5152: fprintf(ficresphtmfr,"</tr>\n ");
1.265 brouard 5153: fprintf(ficresphtm,"</tr>\n");
5154: if((cptcoveff==0 && nj==1)|| nj==2 ) {
5155: if(iage <= iagemax)
5156: fprintf(ficresp,"\n");
1.240 brouard 5157: }
1.251 brouard 5158: if(first==1)
5159: printf("Others in log...\n");
5160: fprintf(ficlog,"\n");
5161: } /* end loop age iage */
1.265 brouard 5162:
1.251 brouard 5163: fprintf(ficresphtm,"<tr><th>Tot</th>");
1.265 brouard 5164: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 5165: if(posproptt < 1.e-5){
1.265 brouard 5166: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
1.251 brouard 5167: }else{
1.265 brouard 5168: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
1.240 brouard 5169: }
1.226 brouard 5170: }
1.251 brouard 5171: fprintf(ficresphtm,"</tr>\n");
5172: fprintf(ficresphtm,"</table>\n");
5173: fprintf(ficresphtmfr,"</table>\n");
1.226 brouard 5174: if(posproptt < 1.e-5){
1.251 brouard 5175: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
5176: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
1.260 brouard 5177: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
5178: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
1.251 brouard 5179: invalidvarcomb[j1]=1;
1.226 brouard 5180: }else{
1.251 brouard 5181: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
5182: invalidvarcomb[j1]=0;
1.226 brouard 5183: }
1.251 brouard 5184: fprintf(ficresphtmfr,"</table>\n");
5185: fprintf(ficlog,"\n");
5186: if(j!=0){
5187: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
1.265 brouard 5188: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 5189: for(k=1; k <=(nlstate+ndeath); k++){
5190: if (k != i) {
1.265 brouard 5191: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
1.253 brouard 5192: if(jj==1){ /* Constant case (in fact cste + age) */
1.251 brouard 5193: if(j1==1){ /* All dummy covariates to zero */
5194: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
5195: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
1.252 brouard 5196: printf("%d%d ",i,k);
5197: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 5198: 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]));
5199: 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]));
5200: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
1.251 brouard 5201: }
1.253 brouard 5202: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
5203: for(iage=iagemin; iage <= iagemax+3; iage++){
5204: x[iage]= (double)iage;
5205: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
1.265 brouard 5206: /* 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 5207: }
1.268 brouard 5208: /* Some are not finite, but linreg will ignore these ages */
5209: no=0;
1.253 brouard 5210: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
1.265 brouard 5211: pstart[s1]=b;
5212: pstart[s1-1]=a;
1.252 brouard 5213: }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 */
5214: 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]);
5215: 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 5216: 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 5217: printf("%d%d ",i,k);
5218: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 5219: 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 5220: }else{ /* Other cases, like quantitative fixed or varying covariates */
5221: ;
5222: }
5223: /* printf("%12.7f )", param[i][jj][k]); */
5224: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 5225: s1++;
1.251 brouard 5226: } /* end jj */
5227: } /* end k!= i */
5228: } /* end k */
1.265 brouard 5229: } /* end i, s1 */
1.251 brouard 5230: } /* end j !=0 */
5231: } /* end selected combination of covariate j1 */
5232: if(j==0){ /* We can estimate starting values from the occurences in each case */
5233: printf("#Freqsummary: Starting values for the constants:\n");
5234: fprintf(ficlog,"\n");
1.265 brouard 5235: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 5236: for(k=1; k <=(nlstate+ndeath); k++){
5237: if (k != i) {
5238: printf("%d%d ",i,k);
5239: fprintf(ficlog,"%d%d ",i,k);
5240: for(jj=1; jj <=ncovmodel; jj++){
1.265 brouard 5241: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
1.253 brouard 5242: if(jj==1){ /* Age has to be done */
1.265 brouard 5243: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5244: 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]));
5245: 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 5246: }
5247: /* printf("%12.7f )", param[i][jj][k]); */
5248: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 5249: s1++;
1.250 brouard 5250: }
1.251 brouard 5251: printf("\n");
5252: fprintf(ficlog,"\n");
1.250 brouard 5253: }
5254: }
1.284 brouard 5255: } /* end of state i */
1.251 brouard 5256: printf("#Freqsummary\n");
5257: fprintf(ficlog,"\n");
1.265 brouard 5258: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5259: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5260: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
5261: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5262: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5263: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
5264: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5265: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
1.251 brouard 5266: /* } */
5267: }
1.265 brouard 5268: } /* end loop s1 */
1.251 brouard 5269:
5270: printf("\n");
5271: fprintf(ficlog,"\n");
5272: } /* end j=0 */
1.249 brouard 5273: } /* end j */
1.252 brouard 5274:
1.253 brouard 5275: if(mle == -2){ /* We want to use these values as starting values */
1.252 brouard 5276: for(i=1, jk=1; i <=nlstate; i++){
5277: for(j=1; j <=nlstate+ndeath; j++){
5278: if(j!=i){
5279: /*ca[0]= k+'a'-1;ca[1]='\0';*/
5280: printf("%1d%1d",i,j);
5281: fprintf(ficparo,"%1d%1d",i,j);
5282: for(k=1; k<=ncovmodel;k++){
5283: /* printf(" %lf",param[i][j][k]); */
5284: /* fprintf(ficparo," %lf",param[i][j][k]); */
5285: p[jk]=pstart[jk];
5286: printf(" %f ",pstart[jk]);
5287: fprintf(ficparo," %f ",pstart[jk]);
5288: jk++;
5289: }
5290: printf("\n");
5291: fprintf(ficparo,"\n");
5292: }
5293: }
5294: }
5295: } /* end mle=-2 */
1.226 brouard 5296: dateintmean=dateintsum/k2cpt;
1.296 brouard 5297: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
1.240 brouard 5298:
1.226 brouard 5299: fclose(ficresp);
5300: fclose(ficresphtm);
5301: fclose(ficresphtmfr);
1.283 brouard 5302: free_vector(idq,1,nqfveff);
1.226 brouard 5303: free_vector(meanq,1,nqfveff);
1.284 brouard 5304: free_vector(stdq,1,nqfveff);
1.226 brouard 5305: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.253 brouard 5306: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5307: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.251 brouard 5308: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5309: free_vector(pospropt,1,nlstate);
5310: free_vector(posprop,1,nlstate);
1.251 brouard 5311: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5312: free_vector(pp,1,nlstate);
5313: /* End of freqsummary */
5314: }
1.126 brouard 5315:
1.268 brouard 5316: /* Simple linear regression */
5317: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
5318:
5319: /* y=a+bx regression */
5320: double sumx = 0.0; /* sum of x */
5321: double sumx2 = 0.0; /* sum of x**2 */
5322: double sumxy = 0.0; /* sum of x * y */
5323: double sumy = 0.0; /* sum of y */
5324: double sumy2 = 0.0; /* sum of y**2 */
5325: double sume2 = 0.0; /* sum of square or residuals */
5326: double yhat;
5327:
5328: double denom=0;
5329: int i;
5330: int ne=*no;
5331:
5332: for ( i=ifi, ne=0;i<=ila;i++) {
5333: if(!isfinite(x[i]) || !isfinite(y[i])){
5334: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5335: continue;
5336: }
5337: ne=ne+1;
5338: sumx += x[i];
5339: sumx2 += x[i]*x[i];
5340: sumxy += x[i] * y[i];
5341: sumy += y[i];
5342: sumy2 += y[i]*y[i];
5343: denom = (ne * sumx2 - sumx*sumx);
5344: /* 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); */
5345: }
5346:
5347: denom = (ne * sumx2 - sumx*sumx);
5348: if (denom == 0) {
5349: // vertical, slope m is infinity
5350: *b = INFINITY;
5351: *a = 0;
5352: if (r) *r = 0;
5353: return 1;
5354: }
5355:
5356: *b = (ne * sumxy - sumx * sumy) / denom;
5357: *a = (sumy * sumx2 - sumx * sumxy) / denom;
5358: if (r!=NULL) {
5359: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
5360: sqrt((sumx2 - sumx*sumx/ne) *
5361: (sumy2 - sumy*sumy/ne));
5362: }
5363: *no=ne;
5364: for ( i=ifi, ne=0;i<=ila;i++) {
5365: if(!isfinite(x[i]) || !isfinite(y[i])){
5366: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5367: continue;
5368: }
5369: ne=ne+1;
5370: yhat = y[i] - *a -*b* x[i];
5371: sume2 += yhat * yhat ;
5372:
5373: denom = (ne * sumx2 - sumx*sumx);
5374: /* 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); */
5375: }
5376: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
5377: *sa= *sb * sqrt(sumx2/ne);
5378:
5379: return 0;
5380: }
5381:
1.126 brouard 5382: /************ Prevalence ********************/
1.227 brouard 5383: 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)
5384: {
5385: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
5386: in each health status at the date of interview (if between dateprev1 and dateprev2).
5387: We still use firstpass and lastpass as another selection.
5388: */
1.126 brouard 5389:
1.227 brouard 5390: int i, m, jk, j1, bool, z1,j, iv;
5391: int mi; /* Effective wave */
5392: int iage;
5393: double agebegin, ageend;
5394:
5395: double **prop;
5396: double posprop;
5397: double y2; /* in fractional years */
5398: int iagemin, iagemax;
5399: int first; /** to stop verbosity which is redirected to log file */
5400:
5401: iagemin= (int) agemin;
5402: iagemax= (int) agemax;
5403: /*pp=vector(1,nlstate);*/
1.251 brouard 5404: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5405: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
5406: j1=0;
1.222 brouard 5407:
1.227 brouard 5408: /*j=cptcoveff;*/
5409: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 5410:
1.288 brouard 5411: first=0;
1.227 brouard 5412: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
5413: for (i=1; i<=nlstate; i++)
1.251 brouard 5414: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
1.227 brouard 5415: prop[i][iage]=0.0;
5416: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
5417: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
5418: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
5419:
5420: for (i=1; i<=imx; i++) { /* Each individual */
5421: bool=1;
5422: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
5423: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
5424: m=mw[mi][i];
5425: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
5426: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
5427: for (z1=1; z1<=cptcoveff; z1++){
5428: if( Fixed[Tmodelind[z1]]==1){
5429: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
5430: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
5431: bool=0;
5432: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
5433: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
5434: bool=0;
5435: }
5436: }
5437: if(bool==1){ /* Otherwise we skip that wave/person */
5438: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
5439: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
5440: if(m >=firstpass && m <=lastpass){
5441: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
5442: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
5443: if(agev[m][i]==0) agev[m][i]=iagemax+1;
5444: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.251 brouard 5445: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
1.227 brouard 5446: 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);
5447: exit(1);
5448: }
5449: if (s[m][i]>0 && s[m][i]<=nlstate) {
5450: /*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]]);*/
5451: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
5452: prop[s[m][i]][iagemax+3] += weight[i];
5453: } /* end valid statuses */
5454: } /* end selection of dates */
5455: } /* end selection of waves */
5456: } /* end bool */
5457: } /* end wave */
5458: } /* end individual */
5459: for(i=iagemin; i <= iagemax+3; i++){
5460: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
5461: posprop += prop[jk][i];
5462: }
5463:
5464: for(jk=1; jk <=nlstate ; jk++){
5465: if( i <= iagemax){
5466: if(posprop>=1.e-5){
5467: probs[i][jk][j1]= prop[jk][i]/posprop;
5468: } else{
1.288 brouard 5469: if(!first){
5470: first=1;
1.266 brouard 5471: 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]);
5472: }else{
1.288 brouard 5473: 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 5474: }
5475: }
5476: }
5477: }/* end jk */
5478: }/* end i */
1.222 brouard 5479: /*} *//* end i1 */
1.227 brouard 5480: } /* end j1 */
1.222 brouard 5481:
1.227 brouard 5482: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
5483: /*free_vector(pp,1,nlstate);*/
1.251 brouard 5484: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5485: } /* End of prevalence */
1.126 brouard 5486:
5487: /************* Waves Concatenation ***************/
5488:
5489: 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)
5490: {
1.298 brouard 5491: /* 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 5492: Death is a valid wave (if date is known).
5493: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
5494: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.298 brouard 5495: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
1.227 brouard 5496: */
1.126 brouard 5497:
1.224 brouard 5498: int i=0, mi=0, m=0, mli=0;
1.126 brouard 5499: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
5500: double sum=0., jmean=0.;*/
1.224 brouard 5501: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 5502: int j, k=0,jk, ju, jl;
5503: double sum=0.;
5504: first=0;
1.214 brouard 5505: firstwo=0;
1.217 brouard 5506: firsthree=0;
1.218 brouard 5507: firstfour=0;
1.164 brouard 5508: jmin=100000;
1.126 brouard 5509: jmax=-1;
5510: jmean=0.;
1.224 brouard 5511:
5512: /* Treating live states */
1.214 brouard 5513: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 5514: mi=0; /* First valid wave */
1.227 brouard 5515: mli=0; /* Last valid wave */
1.309 brouard 5516: m=firstpass; /* Loop on waves */
5517: while(s[m][i] <= nlstate){ /* a live state or unknown state */
1.227 brouard 5518: 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 */
5519: mli=m-1;/* mw[++mi][i]=m-1; */
5520: }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 5521: 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 5522: mli=m;
1.224 brouard 5523: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
5524: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 5525: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 5526: }
1.309 brouard 5527: else{ /* m = lastpass, eventual special issue with warning */
1.224 brouard 5528: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 5529: break;
1.224 brouard 5530: #else
1.317 brouard 5531: 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 5532: if(firsthree == 0){
1.302 brouard 5533: 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 5534: firsthree=1;
1.317 brouard 5535: }else if(firsthree >=1 && firsthree < 10){
5536: 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);
5537: firsthree++;
5538: }else if(firsthree == 10){
5539: printf("Information, too many Information flags: no more reported to log either\n");
5540: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n");
5541: firsthree++;
5542: }else{
5543: firsthree++;
1.227 brouard 5544: }
1.309 brouard 5545: mw[++mi][i]=m; /* Valid transition with unknown status */
1.227 brouard 5546: mli=m;
5547: }
5548: if(s[m][i]==-2){ /* Vital status is really unknown */
5549: nbwarn++;
1.309 brouard 5550: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
1.227 brouard 5551: 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);
5552: 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);
5553: }
5554: break;
5555: }
5556: break;
1.224 brouard 5557: #endif
1.227 brouard 5558: }/* End m >= lastpass */
1.126 brouard 5559: }/* end while */
1.224 brouard 5560:
1.227 brouard 5561: /* 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 5562: /* After last pass */
1.224 brouard 5563: /* Treating death states */
1.214 brouard 5564: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 5565: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
5566: /* } */
1.126 brouard 5567: mi++; /* Death is another wave */
5568: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 5569: /* Only death is a correct wave */
1.126 brouard 5570: mw[mi][i]=m;
1.257 brouard 5571: } /* else not in a death state */
1.224 brouard 5572: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.257 brouard 5573: else if ((int) andc[i] != 9999) { /* Date of death is known */
1.218 brouard 5574: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.309 brouard 5575: 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 5576: nbwarn++;
5577: if(firstfiv==0){
1.309 brouard 5578: 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 5579: firstfiv=1;
5580: }else{
1.309 brouard 5581: 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 5582: }
1.309 brouard 5583: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
5584: }else{ /* Month of Death occured afer last wave month, potential bias */
1.227 brouard 5585: nberr++;
5586: if(firstwo==0){
1.309 brouard 5587: 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 5588: firstwo=1;
5589: }
1.309 brouard 5590: 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 5591: }
1.257 brouard 5592: }else{ /* if date of interview is unknown */
1.227 brouard 5593: /* death is known but not confirmed by death status at any wave */
5594: if(firstfour==0){
1.309 brouard 5595: 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 5596: firstfour=1;
5597: }
1.309 brouard 5598: 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 5599: }
1.224 brouard 5600: } /* end if date of death is known */
5601: #endif
1.309 brouard 5602: wav[i]=mi; /* mi should be the last effective wave (or mli), */
5603: /* wav[i]=mw[mi][i]; */
1.126 brouard 5604: if(mi==0){
5605: nbwarn++;
5606: if(first==0){
1.227 brouard 5607: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
5608: first=1;
1.126 brouard 5609: }
5610: if(first==1){
1.227 brouard 5611: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 5612: }
5613: } /* end mi==0 */
5614: } /* End individuals */
1.214 brouard 5615: /* wav and mw are no more changed */
1.223 brouard 5616:
1.317 brouard 5617: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree);
5618: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree);
5619:
5620:
1.126 brouard 5621: for(i=1; i<=imx; i++){
5622: for(mi=1; mi<wav[i];mi++){
5623: if (stepm <=0)
1.227 brouard 5624: dh[mi][i]=1;
1.126 brouard 5625: else{
1.260 brouard 5626: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
1.227 brouard 5627: if (agedc[i] < 2*AGESUP) {
5628: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
5629: if(j==0) j=1; /* Survives at least one month after exam */
5630: else if(j<0){
5631: nberr++;
5632: printf("Error! Negative delay (%d to death) between waves %d and %d of individual %ld at 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]);
5633: j=1; /* Temporary Dangerous patch */
5634: 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);
5635: fprintf(ficlog,"Error! Negative delay (%d to death) between waves %d and %d of individual %ld at 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]);
5636: 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);
5637: }
5638: k=k+1;
5639: if (j >= jmax){
5640: jmax=j;
5641: ijmax=i;
5642: }
5643: if (j <= jmin){
5644: jmin=j;
5645: ijmin=i;
5646: }
5647: sum=sum+j;
5648: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
5649: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
5650: }
5651: }
5652: else{
5653: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 5654: /* 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 5655:
1.227 brouard 5656: k=k+1;
5657: if (j >= jmax) {
5658: jmax=j;
5659: ijmax=i;
5660: }
5661: else if (j <= jmin){
5662: jmin=j;
5663: ijmin=i;
5664: }
5665: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
5666: /*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]);*/
5667: if(j<0){
5668: nberr++;
5669: printf("Error! Negative delay (%d) between waves %d and %d of individual %ld at 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]);
5670: fprintf(ficlog,"Error! Negative delay (%d) between waves %d and %d of individual %ld at 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]);
5671: }
5672: sum=sum+j;
5673: }
5674: jk= j/stepm;
5675: jl= j -jk*stepm;
5676: ju= j -(jk+1)*stepm;
5677: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
5678: if(jl==0){
5679: dh[mi][i]=jk;
5680: bh[mi][i]=0;
5681: }else{ /* We want a negative bias in order to only have interpolation ie
5682: * to avoid the price of an extra matrix product in likelihood */
5683: dh[mi][i]=jk+1;
5684: bh[mi][i]=ju;
5685: }
5686: }else{
5687: if(jl <= -ju){
5688: dh[mi][i]=jk;
5689: bh[mi][i]=jl; /* bias is positive if real duration
5690: * is higher than the multiple of stepm and negative otherwise.
5691: */
5692: }
5693: else{
5694: dh[mi][i]=jk+1;
5695: bh[mi][i]=ju;
5696: }
5697: if(dh[mi][i]==0){
5698: dh[mi][i]=1; /* At least one step */
5699: bh[mi][i]=ju; /* At least one step */
5700: /* 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);*/
5701: }
5702: } /* end if mle */
1.126 brouard 5703: }
5704: } /* end wave */
5705: }
5706: jmean=sum/k;
5707: 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 5708: 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 5709: }
1.126 brouard 5710:
5711: /*********** Tricode ****************************/
1.220 brouard 5712: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 5713: {
5714: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
5715: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
5716: * Boring subroutine which should only output nbcode[Tvar[j]][k]
5717: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
5718: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
5719: */
1.130 brouard 5720:
1.242 brouard 5721: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
5722: int modmaxcovj=0; /* Modality max of covariates j */
5723: int cptcode=0; /* Modality max of covariates j */
5724: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 5725:
5726:
1.242 brouard 5727: /* cptcoveff=0; */
5728: /* *cptcov=0; */
1.126 brouard 5729:
1.242 brouard 5730: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.285 brouard 5731: for (k=1; k <= maxncov; k++)
5732: for(j=1; j<=2; j++)
5733: nbcode[k][j]=0; /* Valgrind */
1.126 brouard 5734:
1.242 brouard 5735: /* Loop on covariates without age and products and no quantitative variable */
5736: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
5737: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
5738: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
5739: switch(Fixed[k]) {
5740: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.311 brouard 5741: modmaxcovj=0;
5742: modmincovj=0;
1.242 brouard 5743: 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*/
5744: ij=(int)(covar[Tvar[k]][i]);
5745: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
5746: * If product of Vn*Vm, still boolean *:
5747: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
5748: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
5749: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
5750: modality of the nth covariate of individual i. */
5751: if (ij > modmaxcovj)
5752: modmaxcovj=ij;
5753: else if (ij < modmincovj)
5754: modmincovj=ij;
1.287 brouard 5755: if (ij <0 || ij >1 ){
1.311 brouard 5756: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
5757: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
5758: fflush(ficlog);
5759: exit(1);
1.287 brouard 5760: }
5761: if ((ij < -1) || (ij > NCOVMAX)){
1.242 brouard 5762: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
5763: exit(1);
5764: }else
5765: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
5766: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
5767: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
5768: /* getting the maximum value of the modality of the covariate
5769: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
5770: female ies 1, then modmaxcovj=1.
5771: */
5772: } /* end for loop on individuals i */
5773: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5774: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5775: cptcode=modmaxcovj;
5776: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
5777: /*for (i=0; i<=cptcode; i++) {*/
5778: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
5779: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5780: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5781: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
5782: if( j != -1){
5783: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
5784: covariate for which somebody answered excluding
5785: undefined. Usually 2: 0 and 1. */
5786: }
5787: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
5788: covariate for which somebody answered including
5789: undefined. Usually 3: -1, 0 and 1. */
5790: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
5791: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
5792: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 5793:
1.242 brouard 5794: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
5795: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
5796: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
5797: /* modmincovj=3; modmaxcovj = 7; */
5798: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
5799: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
5800: /* defining two dummy variables: variables V1_1 and V1_2.*/
5801: /* nbcode[Tvar[j]][ij]=k; */
5802: /* nbcode[Tvar[j]][1]=0; */
5803: /* nbcode[Tvar[j]][2]=1; */
5804: /* nbcode[Tvar[j]][3]=2; */
5805: /* To be continued (not working yet). */
5806: ij=0; /* ij is similar to i but can jump over null modalities */
1.287 brouard 5807:
5808: /* 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*/
5809: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
5810: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
5811: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
5812: /*, could be restored in the future */
5813: 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 5814: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
5815: break;
5816: }
5817: ij++;
1.287 brouard 5818: 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 5819: cptcode = ij; /* New max modality for covar j */
5820: } /* end of loop on modality i=-1 to 1 or more */
5821: break;
5822: case 1: /* Testing on varying covariate, could be simple and
5823: * should look at waves or product of fixed *
5824: * varying. No time to test -1, assuming 0 and 1 only */
5825: ij=0;
5826: for(i=0; i<=1;i++){
5827: nbcode[Tvar[k]][++ij]=i;
5828: }
5829: break;
5830: default:
5831: break;
5832: } /* end switch */
5833: } /* end dummy test */
1.311 brouard 5834: if(Dummy[k]==1 && Typevar[k] !=1){ /* Dummy covariate and not age product */
5835: 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*/
5836: if(isnan(covar[Tvar[k]][i])){
5837: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
5838: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
5839: fflush(ficlog);
5840: exit(1);
5841: }
5842: }
5843: }
1.287 brouard 5844: } /* 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 5845:
5846: for (k=-1; k< maxncov; k++) Ndum[k]=0;
5847: /* Look at fixed dummy (single or product) covariates to check empty modalities */
5848: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
5849: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
5850: 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 */
5851: 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 */
5852: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
5853: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
5854:
5855: ij=0;
5856: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
5857: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
5858: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
5859: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
5860: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
5861: /* If product not in single variable we don't print results */
5862: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
5863: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
5864: 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*/
5865: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
5866: 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 */
5867: if(Fixed[k]!=0)
5868: anyvaryingduminmodel=1;
5869: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
5870: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
5871: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
5872: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
5873: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
5874: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
5875: }
5876: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
5877: /* ij--; */
5878: /* cptcoveff=ij; /\*Number of total covariates*\/ */
5879: *cptcov=ij; /*Number of total real effective covariates: effective
5880: * because they can be excluded from the model and real
5881: * if in the model but excluded because missing values, but how to get k from ij?*/
5882: for(j=ij+1; j<= cptcovt; j++){
5883: Tvaraff[j]=0;
5884: Tmodelind[j]=0;
5885: }
5886: for(j=ntveff+1; j<= cptcovt; j++){
5887: TmodelInvind[j]=0;
5888: }
5889: /* To be sorted */
5890: ;
5891: }
1.126 brouard 5892:
1.145 brouard 5893:
1.126 brouard 5894: /*********** Health Expectancies ****************/
5895:
1.235 brouard 5896: 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 5897:
5898: {
5899: /* Health expectancies, no variances */
1.164 brouard 5900: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 5901: int nhstepma, nstepma; /* Decreasing with age */
5902: double age, agelim, hf;
5903: double ***p3mat;
5904: double eip;
5905:
1.238 brouard 5906: /* pstamp(ficreseij); */
1.126 brouard 5907: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
5908: fprintf(ficreseij,"# Age");
5909: for(i=1; i<=nlstate;i++){
5910: for(j=1; j<=nlstate;j++){
5911: fprintf(ficreseij," e%1d%1d ",i,j);
5912: }
5913: fprintf(ficreseij," e%1d. ",i);
5914: }
5915: fprintf(ficreseij,"\n");
5916:
5917:
5918: if(estepm < stepm){
5919: printf ("Problem %d lower than %d\n",estepm, stepm);
5920: }
5921: else hstepm=estepm;
5922: /* We compute the life expectancy from trapezoids spaced every estepm months
5923: * This is mainly to measure the difference between two models: for example
5924: * if stepm=24 months pijx are given only every 2 years and by summing them
5925: * we are calculating an estimate of the Life Expectancy assuming a linear
5926: * progression in between and thus overestimating or underestimating according
5927: * to the curvature of the survival function. If, for the same date, we
5928: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5929: * to compare the new estimate of Life expectancy with the same linear
5930: * hypothesis. A more precise result, taking into account a more precise
5931: * curvature will be obtained if estepm is as small as stepm. */
5932:
5933: /* For example we decided to compute the life expectancy with the smallest unit */
5934: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5935: nhstepm is the number of hstepm from age to agelim
5936: nstepm is the number of stepm from age to agelin.
1.270 brouard 5937: Look at hpijx to understand the reason which relies in memory size consideration
1.126 brouard 5938: and note for a fixed period like estepm months */
5939: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5940: survival function given by stepm (the optimization length). Unfortunately it
5941: means that if the survival funtion is printed only each two years of age and if
5942: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5943: results. So we changed our mind and took the option of the best precision.
5944: */
5945: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5946:
5947: agelim=AGESUP;
5948: /* If stepm=6 months */
5949: /* Computed by stepm unit matrices, product of hstepm matrices, stored
5950: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
5951:
5952: /* nhstepm age range expressed in number of stepm */
5953: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5954: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5955: /* if (stepm >= YEARM) hstepm=1;*/
5956: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5957: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5958:
5959: for (age=bage; age<=fage; age ++){
5960: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5961: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5962: /* if (stepm >= YEARM) hstepm=1;*/
5963: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
5964:
5965: /* If stepm=6 months */
5966: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5967: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5968:
1.235 brouard 5969: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 5970:
5971: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5972:
5973: printf("%d|",(int)age);fflush(stdout);
5974: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5975:
5976: /* Computing expectancies */
5977: for(i=1; i<=nlstate;i++)
5978: for(j=1; j<=nlstate;j++)
5979: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5980: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
5981:
5982: /* 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]);*/
5983:
5984: }
5985:
5986: fprintf(ficreseij,"%3.0f",age );
5987: for(i=1; i<=nlstate;i++){
5988: eip=0;
5989: for(j=1; j<=nlstate;j++){
5990: eip +=eij[i][j][(int)age];
5991: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
5992: }
5993: fprintf(ficreseij,"%9.4f", eip );
5994: }
5995: fprintf(ficreseij,"\n");
5996:
5997: }
5998: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5999: printf("\n");
6000: fprintf(ficlog,"\n");
6001:
6002: }
6003:
1.235 brouard 6004: 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 6005:
6006: {
6007: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 6008: to initial status i, ei. .
1.126 brouard 6009: */
6010: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
6011: int nhstepma, nstepma; /* Decreasing with age */
6012: double age, agelim, hf;
6013: double ***p3matp, ***p3matm, ***varhe;
6014: double **dnewm,**doldm;
6015: double *xp, *xm;
6016: double **gp, **gm;
6017: double ***gradg, ***trgradg;
6018: int theta;
6019:
6020: double eip, vip;
6021:
6022: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
6023: xp=vector(1,npar);
6024: xm=vector(1,npar);
6025: dnewm=matrix(1,nlstate*nlstate,1,npar);
6026: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
6027:
6028: pstamp(ficresstdeij);
6029: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
6030: fprintf(ficresstdeij,"# Age");
6031: for(i=1; i<=nlstate;i++){
6032: for(j=1; j<=nlstate;j++)
6033: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
6034: fprintf(ficresstdeij," e%1d. ",i);
6035: }
6036: fprintf(ficresstdeij,"\n");
6037:
6038: pstamp(ficrescveij);
6039: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
6040: fprintf(ficrescveij,"# Age");
6041: for(i=1; i<=nlstate;i++)
6042: for(j=1; j<=nlstate;j++){
6043: cptj= (j-1)*nlstate+i;
6044: for(i2=1; i2<=nlstate;i2++)
6045: for(j2=1; j2<=nlstate;j2++){
6046: cptj2= (j2-1)*nlstate+i2;
6047: if(cptj2 <= cptj)
6048: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
6049: }
6050: }
6051: fprintf(ficrescveij,"\n");
6052:
6053: if(estepm < stepm){
6054: printf ("Problem %d lower than %d\n",estepm, stepm);
6055: }
6056: else hstepm=estepm;
6057: /* We compute the life expectancy from trapezoids spaced every estepm months
6058: * This is mainly to measure the difference between two models: for example
6059: * if stepm=24 months pijx are given only every 2 years and by summing them
6060: * we are calculating an estimate of the Life Expectancy assuming a linear
6061: * progression in between and thus overestimating or underestimating according
6062: * to the curvature of the survival function. If, for the same date, we
6063: * estimate the model with stepm=1 month, we can keep estepm to 24 months
6064: * to compare the new estimate of Life expectancy with the same linear
6065: * hypothesis. A more precise result, taking into account a more precise
6066: * curvature will be obtained if estepm is as small as stepm. */
6067:
6068: /* For example we decided to compute the life expectancy with the smallest unit */
6069: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6070: nhstepm is the number of hstepm from age to agelim
6071: nstepm is the number of stepm from age to agelin.
6072: Look at hpijx to understand the reason of that which relies in memory size
6073: and note for a fixed period like estepm months */
6074: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
6075: survival function given by stepm (the optimization length). Unfortunately it
6076: means that if the survival funtion is printed only each two years of age and if
6077: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6078: results. So we changed our mind and took the option of the best precision.
6079: */
6080: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6081:
6082: /* If stepm=6 months */
6083: /* nhstepm age range expressed in number of stepm */
6084: agelim=AGESUP;
6085: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
6086: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6087: /* if (stepm >= YEARM) hstepm=1;*/
6088: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6089:
6090: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6091: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6092: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
6093: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
6094: gp=matrix(0,nhstepm,1,nlstate*nlstate);
6095: gm=matrix(0,nhstepm,1,nlstate*nlstate);
6096:
6097: for (age=bage; age<=fage; age ++){
6098: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
6099: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
6100: /* if (stepm >= YEARM) hstepm=1;*/
6101: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 6102:
1.126 brouard 6103: /* If stepm=6 months */
6104: /* Computed by stepm unit matrices, product of hstepma matrices, stored
6105: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
6106:
6107: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 6108:
1.126 brouard 6109: /* Computing Variances of health expectancies */
6110: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
6111: decrease memory allocation */
6112: for(theta=1; theta <=npar; theta++){
6113: for(i=1; i<=npar; i++){
1.222 brouard 6114: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6115: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 6116: }
1.235 brouard 6117: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
6118: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 6119:
1.126 brouard 6120: for(j=1; j<= nlstate; j++){
1.222 brouard 6121: for(i=1; i<=nlstate; i++){
6122: for(h=0; h<=nhstepm-1; h++){
6123: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
6124: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
6125: }
6126: }
1.126 brouard 6127: }
1.218 brouard 6128:
1.126 brouard 6129: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 6130: for(h=0; h<=nhstepm-1; h++){
6131: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
6132: }
1.126 brouard 6133: }/* End theta */
6134:
6135:
6136: for(h=0; h<=nhstepm-1; h++)
6137: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 6138: for(theta=1; theta <=npar; theta++)
6139: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 6140:
1.218 brouard 6141:
1.222 brouard 6142: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 6143: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 6144: varhe[ij][ji][(int)age] =0.;
1.218 brouard 6145:
1.222 brouard 6146: printf("%d|",(int)age);fflush(stdout);
6147: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
6148: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 6149: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 6150: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
6151: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
6152: for(ij=1;ij<=nlstate*nlstate;ij++)
6153: for(ji=1;ji<=nlstate*nlstate;ji++)
6154: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 6155: }
6156: }
1.320 brouard 6157: /* if((int)age ==50){ */
6158: /* printf(" age=%d cij=%d nres=%d varhe[%d][%d]=%f ",(int)age, cij, nres, 1,2,varhe[1][2]); */
6159: /* } */
1.126 brouard 6160: /* Computing expectancies */
1.235 brouard 6161: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 6162: for(i=1; i<=nlstate;i++)
6163: for(j=1; j<=nlstate;j++)
1.222 brouard 6164: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
6165: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 6166:
1.222 brouard 6167: /* 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 6168:
1.222 brouard 6169: }
1.269 brouard 6170:
6171: /* Standard deviation of expectancies ij */
1.126 brouard 6172: fprintf(ficresstdeij,"%3.0f",age );
6173: for(i=1; i<=nlstate;i++){
6174: eip=0.;
6175: vip=0.;
6176: for(j=1; j<=nlstate;j++){
1.222 brouard 6177: eip += eij[i][j][(int)age];
6178: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
6179: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
6180: 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 6181: }
6182: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
6183: }
6184: fprintf(ficresstdeij,"\n");
1.218 brouard 6185:
1.269 brouard 6186: /* Variance of expectancies ij */
1.126 brouard 6187: fprintf(ficrescveij,"%3.0f",age );
6188: for(i=1; i<=nlstate;i++)
6189: for(j=1; j<=nlstate;j++){
1.222 brouard 6190: cptj= (j-1)*nlstate+i;
6191: for(i2=1; i2<=nlstate;i2++)
6192: for(j2=1; j2<=nlstate;j2++){
6193: cptj2= (j2-1)*nlstate+i2;
6194: if(cptj2 <= cptj)
6195: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
6196: }
1.126 brouard 6197: }
6198: fprintf(ficrescveij,"\n");
1.218 brouard 6199:
1.126 brouard 6200: }
6201: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
6202: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
6203: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
6204: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
6205: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6206: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6207: printf("\n");
6208: fprintf(ficlog,"\n");
1.218 brouard 6209:
1.126 brouard 6210: free_vector(xm,1,npar);
6211: free_vector(xp,1,npar);
6212: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
6213: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
6214: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
6215: }
1.218 brouard 6216:
1.126 brouard 6217: /************ Variance ******************/
1.235 brouard 6218: 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 6219: {
1.279 brouard 6220: /** Variance of health expectancies
6221: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
6222: * double **newm;
6223: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
6224: */
1.218 brouard 6225:
6226: /* int movingaverage(); */
6227: double **dnewm,**doldm;
6228: double **dnewmp,**doldmp;
6229: int i, j, nhstepm, hstepm, h, nstepm ;
1.288 brouard 6230: int first=0;
1.218 brouard 6231: int k;
6232: double *xp;
1.279 brouard 6233: double **gp, **gm; /**< for var eij */
6234: double ***gradg, ***trgradg; /**< for var eij */
6235: double **gradgp, **trgradgp; /**< for var p point j */
6236: double *gpp, *gmp; /**< for var p point j */
6237: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
1.218 brouard 6238: double ***p3mat;
6239: double age,agelim, hf;
6240: /* double ***mobaverage; */
6241: int theta;
6242: char digit[4];
6243: char digitp[25];
6244:
6245: char fileresprobmorprev[FILENAMELENGTH];
6246:
6247: if(popbased==1){
6248: if(mobilav!=0)
6249: strcpy(digitp,"-POPULBASED-MOBILAV_");
6250: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
6251: }
6252: else
6253: strcpy(digitp,"-STABLBASED_");
1.126 brouard 6254:
1.218 brouard 6255: /* if (mobilav!=0) { */
6256: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6257: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
6258: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
6259: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
6260: /* } */
6261: /* } */
6262:
6263: strcpy(fileresprobmorprev,"PRMORPREV-");
6264: sprintf(digit,"%-d",ij);
6265: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
6266: strcat(fileresprobmorprev,digit); /* Tvar to be done */
6267: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
6268: strcat(fileresprobmorprev,fileresu);
6269: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
6270: printf("Problem with resultfile: %s\n", fileresprobmorprev);
6271: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
6272: }
6273: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6274: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6275: pstamp(ficresprobmorprev);
6276: 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 6277: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
6278: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
6279: fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
6280: }
6281: for(j=1;j<=cptcoveff;j++)
6282: fprintf(ficresprobmorprev,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,j)]);
6283: fprintf(ficresprobmorprev,"\n");
6284:
1.218 brouard 6285: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
6286: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6287: fprintf(ficresprobmorprev," p.%-d SE",j);
6288: for(i=1; i<=nlstate;i++)
6289: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
6290: }
6291: fprintf(ficresprobmorprev,"\n");
6292:
6293: fprintf(ficgp,"\n# Routine varevsij");
6294: fprintf(ficgp,"\nunset title \n");
6295: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
6296: 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");
6297: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
1.279 brouard 6298:
1.218 brouard 6299: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6300: pstamp(ficresvij);
6301: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
6302: if(popbased==1)
6303: 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);
6304: else
6305: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
6306: fprintf(ficresvij,"# Age");
6307: for(i=1; i<=nlstate;i++)
6308: for(j=1; j<=nlstate;j++)
6309: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
6310: fprintf(ficresvij,"\n");
6311:
6312: xp=vector(1,npar);
6313: dnewm=matrix(1,nlstate,1,npar);
6314: doldm=matrix(1,nlstate,1,nlstate);
6315: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
6316: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6317:
6318: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
6319: gpp=vector(nlstate+1,nlstate+ndeath);
6320: gmp=vector(nlstate+1,nlstate+ndeath);
6321: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 6322:
1.218 brouard 6323: if(estepm < stepm){
6324: printf ("Problem %d lower than %d\n",estepm, stepm);
6325: }
6326: else hstepm=estepm;
6327: /* For example we decided to compute the life expectancy with the smallest unit */
6328: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6329: nhstepm is the number of hstepm from age to agelim
6330: nstepm is the number of stepm from age to agelim.
6331: Look at function hpijx to understand why because of memory size limitations,
6332: we decided (b) to get a life expectancy respecting the most precise curvature of the
6333: survival function given by stepm (the optimization length). Unfortunately it
6334: means that if the survival funtion is printed every two years of age and if
6335: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6336: results. So we changed our mind and took the option of the best precision.
6337: */
6338: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6339: agelim = AGESUP;
6340: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6341: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6342: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6343: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6344: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
6345: gp=matrix(0,nhstepm,1,nlstate);
6346: gm=matrix(0,nhstepm,1,nlstate);
6347:
6348:
6349: for(theta=1; theta <=npar; theta++){
6350: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
6351: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6352: }
1.279 brouard 6353: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
6354: * returns into prlim .
1.288 brouard 6355: */
1.242 brouard 6356: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.279 brouard 6357:
6358: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
1.218 brouard 6359: if (popbased==1) {
6360: if(mobilav ==0){
6361: for(i=1; i<=nlstate;i++)
6362: prlim[i][i]=probs[(int)age][i][ij];
6363: }else{ /* mobilav */
6364: for(i=1; i<=nlstate;i++)
6365: prlim[i][i]=mobaverage[(int)age][i][ij];
6366: }
6367: }
1.295 brouard 6368: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
1.279 brouard 6369: */
6370: 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 6371: /**< 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 6372: * at horizon h in state j including mortality.
6373: */
1.218 brouard 6374: for(j=1; j<= nlstate; j++){
6375: for(h=0; h<=nhstepm; h++){
6376: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
6377: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
6378: }
6379: }
1.279 brouard 6380: /* Next for computing shifted+ probability of death (h=1 means
1.218 brouard 6381: computed over hstepm matrices product = hstepm*stepm months)
1.279 brouard 6382: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
1.218 brouard 6383: */
6384: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6385: for(i=1,gpp[j]=0.; i<= nlstate; i++)
6386: gpp[j] += prlim[i][i]*p3mat[i][j][1];
1.279 brouard 6387: }
6388:
6389: /* Again with minus shift */
1.218 brouard 6390:
6391: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
6392: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6393:
1.242 brouard 6394: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 6395:
6396: if (popbased==1) {
6397: if(mobilav ==0){
6398: for(i=1; i<=nlstate;i++)
6399: prlim[i][i]=probs[(int)age][i][ij];
6400: }else{ /* mobilav */
6401: for(i=1; i<=nlstate;i++)
6402: prlim[i][i]=mobaverage[(int)age][i][ij];
6403: }
6404: }
6405:
1.235 brouard 6406: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 6407:
6408: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
6409: for(h=0; h<=nhstepm; h++){
6410: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
6411: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
6412: }
6413: }
6414: /* This for computing probability of death (h=1 means
6415: computed over hstepm matrices product = hstepm*stepm months)
6416: as a weighted average of prlim.
6417: */
6418: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6419: for(i=1,gmp[j]=0.; i<= nlstate; i++)
6420: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6421: }
1.279 brouard 6422: /* end shifting computations */
6423:
6424: /**< Computing gradient matrix at horizon h
6425: */
1.218 brouard 6426: for(j=1; j<= nlstate; j++) /* vareij */
6427: for(h=0; h<=nhstepm; h++){
6428: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
6429: }
1.279 brouard 6430: /**< Gradient of overall mortality p.3 (or p.j)
6431: */
6432: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
1.218 brouard 6433: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
6434: }
6435:
6436: } /* End theta */
1.279 brouard 6437:
6438: /* We got the gradient matrix for each theta and state j */
1.218 brouard 6439: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
6440:
6441: for(h=0; h<=nhstepm; h++) /* veij */
6442: for(j=1; j<=nlstate;j++)
6443: for(theta=1; theta <=npar; theta++)
6444: trgradg[h][j][theta]=gradg[h][theta][j];
6445:
6446: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
6447: for(theta=1; theta <=npar; theta++)
6448: trgradgp[j][theta]=gradgp[theta][j];
1.279 brouard 6449: /**< as well as its transposed matrix
6450: */
1.218 brouard 6451:
6452: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6453: for(i=1;i<=nlstate;i++)
6454: for(j=1;j<=nlstate;j++)
6455: vareij[i][j][(int)age] =0.;
1.279 brouard 6456:
6457: /* Computing trgradg by matcov by gradg at age and summing over h
6458: * and k (nhstepm) formula 15 of article
6459: * Lievre-Brouard-Heathcote
6460: */
6461:
1.218 brouard 6462: for(h=0;h<=nhstepm;h++){
6463: for(k=0;k<=nhstepm;k++){
6464: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
6465: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
6466: for(i=1;i<=nlstate;i++)
6467: for(j=1;j<=nlstate;j++)
6468: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
6469: }
6470: }
6471:
1.279 brouard 6472: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
6473: * p.j overall mortality formula 49 but computed directly because
6474: * we compute the grad (wix pijx) instead of grad (pijx),even if
6475: * wix is independent of theta.
6476: */
1.218 brouard 6477: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
6478: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
6479: for(j=nlstate+1;j<=nlstate+ndeath;j++)
6480: for(i=nlstate+1;i<=nlstate+ndeath;i++)
6481: varppt[j][i]=doldmp[j][i];
6482: /* end ppptj */
6483: /* x centered again */
6484:
1.242 brouard 6485: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 6486:
6487: if (popbased==1) {
6488: if(mobilav ==0){
6489: for(i=1; i<=nlstate;i++)
6490: prlim[i][i]=probs[(int)age][i][ij];
6491: }else{ /* mobilav */
6492: for(i=1; i<=nlstate;i++)
6493: prlim[i][i]=mobaverage[(int)age][i][ij];
6494: }
6495: }
6496:
6497: /* This for computing probability of death (h=1 means
6498: computed over hstepm (estepm) matrices product = hstepm*stepm months)
6499: as a weighted average of prlim.
6500: */
1.235 brouard 6501: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 6502: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6503: for(i=1,gmp[j]=0.;i<= nlstate; i++)
6504: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6505: }
6506: /* end probability of death */
6507:
6508: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
6509: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6510: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
6511: for(i=1; i<=nlstate;i++){
6512: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
6513: }
6514: }
6515: fprintf(ficresprobmorprev,"\n");
6516:
6517: fprintf(ficresvij,"%.0f ",age );
6518: for(i=1; i<=nlstate;i++)
6519: for(j=1; j<=nlstate;j++){
6520: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
6521: }
6522: fprintf(ficresvij,"\n");
6523: free_matrix(gp,0,nhstepm,1,nlstate);
6524: free_matrix(gm,0,nhstepm,1,nlstate);
6525: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
6526: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
6527: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6528: } /* End age */
6529: free_vector(gpp,nlstate+1,nlstate+ndeath);
6530: free_vector(gmp,nlstate+1,nlstate+ndeath);
6531: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
6532: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
6533: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
6534: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
6535: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
6536: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
6537: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
6538: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
6539: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
6540: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
6541: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
6542: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
6543: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
6544: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
6545: 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);
6546: /* 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 6547: */
1.218 brouard 6548: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
6549: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 6550:
1.218 brouard 6551: free_vector(xp,1,npar);
6552: free_matrix(doldm,1,nlstate,1,nlstate);
6553: free_matrix(dnewm,1,nlstate,1,npar);
6554: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6555: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
6556: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6557: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6558: fclose(ficresprobmorprev);
6559: fflush(ficgp);
6560: fflush(fichtm);
6561: } /* end varevsij */
1.126 brouard 6562:
6563: /************ Variance of prevlim ******************/
1.269 brouard 6564: 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 6565: {
1.205 brouard 6566: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 6567: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 6568:
1.268 brouard 6569: double **dnewmpar,**doldm;
1.126 brouard 6570: int i, j, nhstepm, hstepm;
6571: double *xp;
6572: double *gp, *gm;
6573: double **gradg, **trgradg;
1.208 brouard 6574: double **mgm, **mgp;
1.126 brouard 6575: double age,agelim;
6576: int theta;
6577:
6578: pstamp(ficresvpl);
1.288 brouard 6579: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
1.241 brouard 6580: fprintf(ficresvpl,"# Age ");
6581: if(nresult >=1)
6582: fprintf(ficresvpl," Result# ");
1.126 brouard 6583: for(i=1; i<=nlstate;i++)
6584: fprintf(ficresvpl," %1d-%1d",i,i);
6585: fprintf(ficresvpl,"\n");
6586:
6587: xp=vector(1,npar);
1.268 brouard 6588: dnewmpar=matrix(1,nlstate,1,npar);
1.126 brouard 6589: doldm=matrix(1,nlstate,1,nlstate);
6590:
6591: hstepm=1*YEARM; /* Every year of age */
6592: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6593: agelim = AGESUP;
6594: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6595: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6596: if (stepm >= YEARM) hstepm=1;
6597: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6598: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 6599: mgp=matrix(1,npar,1,nlstate);
6600: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 6601: gp=vector(1,nlstate);
6602: gm=vector(1,nlstate);
6603:
6604: for(theta=1; theta <=npar; theta++){
6605: for(i=1; i<=npar; i++){ /* Computes gradient */
6606: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6607: }
1.288 brouard 6608: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6609: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6610: /* else */
6611: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6612: for(i=1;i<=nlstate;i++){
1.126 brouard 6613: gp[i] = prlim[i][i];
1.208 brouard 6614: mgp[theta][i] = prlim[i][i];
6615: }
1.126 brouard 6616: for(i=1; i<=npar; i++) /* Computes gradient */
6617: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6618: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6619: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6620: /* else */
6621: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6622: for(i=1;i<=nlstate;i++){
1.126 brouard 6623: gm[i] = prlim[i][i];
1.208 brouard 6624: mgm[theta][i] = prlim[i][i];
6625: }
1.126 brouard 6626: for(i=1;i<=nlstate;i++)
6627: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 6628: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 6629: } /* End theta */
6630:
6631: trgradg =matrix(1,nlstate,1,npar);
6632:
6633: for(j=1; j<=nlstate;j++)
6634: for(theta=1; theta <=npar; theta++)
6635: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 6636: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6637: /* printf("\nmgm mgp %d ",(int)age); */
6638: /* for(j=1; j<=nlstate;j++){ */
6639: /* printf(" %d ",j); */
6640: /* for(theta=1; theta <=npar; theta++) */
6641: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6642: /* printf("\n "); */
6643: /* } */
6644: /* } */
6645: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6646: /* printf("\n gradg %d ",(int)age); */
6647: /* for(j=1; j<=nlstate;j++){ */
6648: /* printf("%d ",j); */
6649: /* for(theta=1; theta <=npar; theta++) */
6650: /* printf("%d %lf ",theta,gradg[theta][j]); */
6651: /* printf("\n "); */
6652: /* } */
6653: /* } */
1.126 brouard 6654:
6655: for(i=1;i<=nlstate;i++)
6656: varpl[i][(int)age] =0.;
1.209 brouard 6657: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.268 brouard 6658: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6659: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6660: }else{
1.268 brouard 6661: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6662: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6663: }
1.126 brouard 6664: for(i=1;i<=nlstate;i++)
6665: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6666:
6667: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 6668: if(nresult >=1)
6669: fprintf(ficresvpl,"%d ",nres );
1.288 brouard 6670: for(i=1; i<=nlstate;i++){
1.126 brouard 6671: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1.288 brouard 6672: /* for(j=1;j<=nlstate;j++) */
6673: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
6674: }
1.126 brouard 6675: fprintf(ficresvpl,"\n");
6676: free_vector(gp,1,nlstate);
6677: free_vector(gm,1,nlstate);
1.208 brouard 6678: free_matrix(mgm,1,npar,1,nlstate);
6679: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 6680: free_matrix(gradg,1,npar,1,nlstate);
6681: free_matrix(trgradg,1,nlstate,1,npar);
6682: } /* End age */
6683:
6684: free_vector(xp,1,npar);
6685: free_matrix(doldm,1,nlstate,1,npar);
1.268 brouard 6686: free_matrix(dnewmpar,1,nlstate,1,nlstate);
6687:
6688: }
6689:
6690:
6691: /************ Variance of backprevalence limit ******************/
1.269 brouard 6692: 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 6693: {
6694: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
6695: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
6696:
6697: double **dnewmpar,**doldm;
6698: int i, j, nhstepm, hstepm;
6699: double *xp;
6700: double *gp, *gm;
6701: double **gradg, **trgradg;
6702: double **mgm, **mgp;
6703: double age,agelim;
6704: int theta;
6705:
6706: pstamp(ficresvbl);
6707: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
6708: fprintf(ficresvbl,"# Age ");
6709: if(nresult >=1)
6710: fprintf(ficresvbl," Result# ");
6711: for(i=1; i<=nlstate;i++)
6712: fprintf(ficresvbl," %1d-%1d",i,i);
6713: fprintf(ficresvbl,"\n");
6714:
6715: xp=vector(1,npar);
6716: dnewmpar=matrix(1,nlstate,1,npar);
6717: doldm=matrix(1,nlstate,1,nlstate);
6718:
6719: hstepm=1*YEARM; /* Every year of age */
6720: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6721: agelim = AGEINF;
6722: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
6723: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6724: if (stepm >= YEARM) hstepm=1;
6725: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6726: gradg=matrix(1,npar,1,nlstate);
6727: mgp=matrix(1,npar,1,nlstate);
6728: mgm=matrix(1,npar,1,nlstate);
6729: gp=vector(1,nlstate);
6730: gm=vector(1,nlstate);
6731:
6732: for(theta=1; theta <=npar; theta++){
6733: for(i=1; i<=npar; i++){ /* Computes gradient */
6734: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6735: }
6736: if(mobilavproj > 0 )
6737: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6738: else
6739: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6740: for(i=1;i<=nlstate;i++){
6741: gp[i] = bprlim[i][i];
6742: mgp[theta][i] = bprlim[i][i];
6743: }
6744: for(i=1; i<=npar; i++) /* Computes gradient */
6745: xp[i] = x[i] - (i==theta ?delti[theta]:0);
6746: if(mobilavproj > 0 )
6747: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6748: else
6749: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6750: for(i=1;i<=nlstate;i++){
6751: gm[i] = bprlim[i][i];
6752: mgm[theta][i] = bprlim[i][i];
6753: }
6754: for(i=1;i<=nlstate;i++)
6755: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
6756: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
6757: } /* End theta */
6758:
6759: trgradg =matrix(1,nlstate,1,npar);
6760:
6761: for(j=1; j<=nlstate;j++)
6762: for(theta=1; theta <=npar; theta++)
6763: trgradg[j][theta]=gradg[theta][j];
6764: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6765: /* printf("\nmgm mgp %d ",(int)age); */
6766: /* for(j=1; j<=nlstate;j++){ */
6767: /* printf(" %d ",j); */
6768: /* for(theta=1; theta <=npar; theta++) */
6769: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6770: /* printf("\n "); */
6771: /* } */
6772: /* } */
6773: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6774: /* printf("\n gradg %d ",(int)age); */
6775: /* for(j=1; j<=nlstate;j++){ */
6776: /* printf("%d ",j); */
6777: /* for(theta=1; theta <=npar; theta++) */
6778: /* printf("%d %lf ",theta,gradg[theta][j]); */
6779: /* printf("\n "); */
6780: /* } */
6781: /* } */
6782:
6783: for(i=1;i<=nlstate;i++)
6784: varbpl[i][(int)age] =0.;
6785: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
6786: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6787: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6788: }else{
6789: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6790: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6791: }
6792: for(i=1;i<=nlstate;i++)
6793: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6794:
6795: fprintf(ficresvbl,"%.0f ",age );
6796: if(nresult >=1)
6797: fprintf(ficresvbl,"%d ",nres );
6798: for(i=1; i<=nlstate;i++)
6799: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
6800: fprintf(ficresvbl,"\n");
6801: free_vector(gp,1,nlstate);
6802: free_vector(gm,1,nlstate);
6803: free_matrix(mgm,1,npar,1,nlstate);
6804: free_matrix(mgp,1,npar,1,nlstate);
6805: free_matrix(gradg,1,npar,1,nlstate);
6806: free_matrix(trgradg,1,nlstate,1,npar);
6807: } /* End age */
6808:
6809: free_vector(xp,1,npar);
6810: free_matrix(doldm,1,nlstate,1,npar);
6811: free_matrix(dnewmpar,1,nlstate,1,nlstate);
1.126 brouard 6812:
6813: }
6814:
6815: /************ Variance of one-step probabilities ******************/
6816: 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 6817: {
6818: int i, j=0, k1, l1, tj;
6819: int k2, l2, j1, z1;
6820: int k=0, l;
6821: int first=1, first1, first2;
1.326 ! brouard 6822: int nres=0; /* New */
1.222 brouard 6823: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
6824: double **dnewm,**doldm;
6825: double *xp;
6826: double *gp, *gm;
6827: double **gradg, **trgradg;
6828: double **mu;
6829: double age, cov[NCOVMAX+1];
6830: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
6831: int theta;
6832: char fileresprob[FILENAMELENGTH];
6833: char fileresprobcov[FILENAMELENGTH];
6834: char fileresprobcor[FILENAMELENGTH];
6835: double ***varpij;
6836:
6837: strcpy(fileresprob,"PROB_");
6838: strcat(fileresprob,fileres);
6839: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
6840: printf("Problem with resultfile: %s\n", fileresprob);
6841: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
6842: }
6843: strcpy(fileresprobcov,"PROBCOV_");
6844: strcat(fileresprobcov,fileresu);
6845: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
6846: printf("Problem with resultfile: %s\n", fileresprobcov);
6847: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
6848: }
6849: strcpy(fileresprobcor,"PROBCOR_");
6850: strcat(fileresprobcor,fileresu);
6851: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
6852: printf("Problem with resultfile: %s\n", fileresprobcor);
6853: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
6854: }
6855: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6856: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6857: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6858: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6859: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6860: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6861: pstamp(ficresprob);
6862: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
6863: fprintf(ficresprob,"# Age");
6864: pstamp(ficresprobcov);
6865: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
6866: fprintf(ficresprobcov,"# Age");
6867: pstamp(ficresprobcor);
6868: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
6869: fprintf(ficresprobcor,"# Age");
1.126 brouard 6870:
6871:
1.222 brouard 6872: for(i=1; i<=nlstate;i++)
6873: for(j=1; j<=(nlstate+ndeath);j++){
6874: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
6875: fprintf(ficresprobcov," p%1d-%1d ",i,j);
6876: fprintf(ficresprobcor," p%1d-%1d ",i,j);
6877: }
6878: /* fprintf(ficresprob,"\n");
6879: fprintf(ficresprobcov,"\n");
6880: fprintf(ficresprobcor,"\n");
6881: */
6882: xp=vector(1,npar);
6883: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6884: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6885: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
6886: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
6887: first=1;
6888: fprintf(ficgp,"\n# Routine varprob");
6889: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
6890: fprintf(fichtm,"\n");
6891:
1.288 brouard 6892: 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 6893: 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);
6894: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 6895: and drawn. It helps understanding how is the covariance between two incidences.\
6896: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 6897: 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 6898: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
6899: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
6900: standard deviations wide on each axis. <br>\
6901: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
6902: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
6903: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
6904:
1.222 brouard 6905: cov[1]=1;
6906: /* tj=cptcoveff; */
1.225 brouard 6907: tj = (int) pow(2,cptcoveff);
1.222 brouard 6908: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
6909: j1=0;
1.224 brouard 6910: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.326 ! brouard 6911: for(nres=1;nres <=1; nres++){ /* For each resultline */
! 6912: /* for(nres=1;nres <=nresult; nres++){ /\* For each resultline *\/ */
1.222 brouard 6913: if (cptcovn>0) {
6914: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 6915: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6916: fprintf(ficresprob, "**********\n#\n");
6917: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 6918: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6919: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 6920:
1.222 brouard 6921: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 6922: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6923: fprintf(ficgp, "**********\n#\n");
1.220 brouard 6924:
6925:
1.222 brouard 6926: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.319 brouard 6927: /* for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); */
6928: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtmcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6929: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6930:
1.222 brouard 6931: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 6932: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6933: fprintf(ficresprobcor, "**********\n#");
6934: if(invalidvarcomb[j1]){
6935: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
6936: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
6937: continue;
6938: }
6939: }
6940: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
6941: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6942: gp=vector(1,(nlstate)*(nlstate+ndeath));
6943: gm=vector(1,(nlstate)*(nlstate+ndeath));
6944: for (age=bage; age<=fage; age ++){
6945: cov[2]=age;
6946: if(nagesqr==1)
6947: cov[3]= age*age;
1.326 ! brouard 6948: /* for (k=1; k<=cptcovn;k++) { */
! 6949: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; */
! 6950: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
! 6951: /* Here comes the value of the covariate 'j1' after renumbering k with single dummy covariates */
! 6952: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(j1,k)];
1.222 brouard 6953: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
6954: * 1 1 1 1 1
6955: * 2 2 1 1 1
6956: * 3 1 2 1 1
6957: */
6958: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
6959: }
1.319 brouard 6960: /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1, Tage[1]=2 */
6961: /* ) p nbcode[Tvar[Tage[k]]][(1 & (ij-1) >> (k-1))+1] */
6962: /*for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.326 ! brouard 6963: for (k=1; k<=cptcovage;k++){ /* For product with age */
! 6964: if(Dummy[Tage[k]]==2){ /* dummy with age */
! 6965: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(j1,k)]*cov[2];
! 6966: /* cov[++k1]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
! 6967: } else if(Dummy[Tage[k]]==3){ /* quantitative with age */
! 6968: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
! 6969: /* cov[++k1]=Tqresult[nres][k]; */
! 6970: }
! 6971: /* cov[2+Tage[k]+nagesqr]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
! 6972: }
! 6973: for (k=1; k<=cptcovprod;k++){/* For product without age */
! 6974: if(Dummy[Tvard[k][1]==0]){
! 6975: if(Dummy[Tvard[k][2]==0]){
! 6976: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,k)] * nbcode[Tvard[k][2]][codtabm(j1,k)];
! 6977: /* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
! 6978: }else{ /* Should we use the mean of the quantitative variables? */
! 6979: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(j1,k)] * Tqresult[nres][k];
! 6980: /* cov[++k1]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; */
! 6981: }
! 6982: }else{
! 6983: if(Dummy[Tvard[k][2]==0]){
! 6984: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(j1,k)] * Tqinvresult[nres][Tvard[k][1]];
! 6985: /* cov[++k1]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; */
! 6986: }else{
! 6987: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
! 6988: /* cov[++k1]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; */
! 6989: }
! 6990: }
! 6991: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
! 6992: }
! 6993: /* For each age and combination of dummy covariates we slightly move the parameters of delti in order to get the gradient*/
1.222 brouard 6994: for(theta=1; theta <=npar; theta++){
6995: for(i=1; i<=npar; i++)
6996: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 6997:
1.222 brouard 6998: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 6999:
1.222 brouard 7000: k=0;
7001: for(i=1; i<= (nlstate); i++){
7002: for(j=1; j<=(nlstate+ndeath);j++){
7003: k=k+1;
7004: gp[k]=pmmij[i][j];
7005: }
7006: }
1.220 brouard 7007:
1.222 brouard 7008: for(i=1; i<=npar; i++)
7009: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 7010:
1.222 brouard 7011: pmij(pmmij,cov,ncovmodel,xp,nlstate);
7012: k=0;
7013: for(i=1; i<=(nlstate); i++){
7014: for(j=1; j<=(nlstate+ndeath);j++){
7015: k=k+1;
7016: gm[k]=pmmij[i][j];
7017: }
7018: }
1.220 brouard 7019:
1.222 brouard 7020: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
7021: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
7022: }
1.126 brouard 7023:
1.222 brouard 7024: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
7025: for(theta=1; theta <=npar; theta++)
7026: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 7027:
1.222 brouard 7028: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
7029: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 7030:
1.222 brouard 7031: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 7032:
1.222 brouard 7033: k=0;
7034: for(i=1; i<=(nlstate); i++){
7035: for(j=1; j<=(nlstate+ndeath);j++){
7036: k=k+1;
7037: mu[k][(int) age]=pmmij[i][j];
7038: }
7039: }
7040: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
7041: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
7042: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 7043:
1.222 brouard 7044: /*printf("\n%d ",(int)age);
7045: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
7046: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
7047: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
7048: }*/
1.220 brouard 7049:
1.222 brouard 7050: fprintf(ficresprob,"\n%d ",(int)age);
7051: fprintf(ficresprobcov,"\n%d ",(int)age);
7052: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 7053:
1.222 brouard 7054: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
7055: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
7056: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
7057: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
7058: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
7059: }
7060: i=0;
7061: for (k=1; k<=(nlstate);k++){
7062: for (l=1; l<=(nlstate+ndeath);l++){
7063: i++;
7064: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
7065: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
7066: for (j=1; j<=i;j++){
7067: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
7068: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
7069: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
7070: }
7071: }
7072: }/* end of loop for state */
7073: } /* end of loop for age */
7074: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
7075: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
7076: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
7077: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
7078:
7079: /* Confidence intervalle of pij */
7080: /*
7081: fprintf(ficgp,"\nunset parametric;unset label");
7082: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
7083: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
7084: 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);
7085: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
7086: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
7087: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
7088: */
7089:
7090: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
7091: first1=1;first2=2;
7092: for (k2=1; k2<=(nlstate);k2++){
7093: for (l2=1; l2<=(nlstate+ndeath);l2++){
7094: if(l2==k2) continue;
7095: j=(k2-1)*(nlstate+ndeath)+l2;
7096: for (k1=1; k1<=(nlstate);k1++){
7097: for (l1=1; l1<=(nlstate+ndeath);l1++){
7098: if(l1==k1) continue;
7099: i=(k1-1)*(nlstate+ndeath)+l1;
7100: if(i<=j) continue;
7101: for (age=bage; age<=fage; age ++){
7102: if ((int)age %5==0){
7103: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
7104: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
7105: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
7106: mu1=mu[i][(int) age]/stepm*YEARM ;
7107: mu2=mu[j][(int) age]/stepm*YEARM;
7108: c12=cv12/sqrt(v1*v2);
7109: /* Computing eigen value of matrix of covariance */
7110: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7111: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
7112: if ((lc2 <0) || (lc1 <0) ){
7113: if(first2==1){
7114: first1=0;
7115: 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);
7116: }
7117: 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);
7118: /* lc1=fabs(lc1); */ /* If we want to have them positive */
7119: /* lc2=fabs(lc2); */
7120: }
1.220 brouard 7121:
1.222 brouard 7122: /* Eigen vectors */
1.280 brouard 7123: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
7124: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
7125: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
7126: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
7127: }else
7128: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
1.222 brouard 7129: /*v21=sqrt(1.-v11*v11); *//* error */
7130: v21=(lc1-v1)/cv12*v11;
7131: v12=-v21;
7132: v22=v11;
7133: tnalp=v21/v11;
7134: if(first1==1){
7135: first1=0;
7136: 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);
7137: }
7138: 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);
7139: /*printf(fignu*/
7140: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
7141: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
7142: if(first==1){
7143: first=0;
7144: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
7145: fprintf(ficgp,"\nset parametric;unset label");
7146: 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);
7147: fprintf(ficgp,"\nset ter svg size 640, 480");
1.266 brouard 7148: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 7149: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 7150: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 7151: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
7152: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7153: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7154: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
7155: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7156: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
7157: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
7158: 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 7159: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
7160: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
1.222 brouard 7161: }else{
7162: first=0;
7163: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
7164: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
7165: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
7166: 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 7167: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
7168: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
1.222 brouard 7169: }/* if first */
7170: } /* age mod 5 */
7171: } /* end loop age */
7172: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
7173: first=1;
7174: } /*l12 */
7175: } /* k12 */
7176: } /*l1 */
7177: }/* k1 */
1.326 ! brouard 7178: } /* loop on nres */
1.222 brouard 7179: } /* loop on combination of covariates j1 */
7180: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
7181: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
7182: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
7183: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
7184: free_vector(xp,1,npar);
7185: fclose(ficresprob);
7186: fclose(ficresprobcov);
7187: fclose(ficresprobcor);
7188: fflush(ficgp);
7189: fflush(fichtmcov);
7190: }
1.126 brouard 7191:
7192:
7193: /******************* Printing html file ***********/
1.201 brouard 7194: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7195: int lastpass, int stepm, int weightopt, char model[],\
7196: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.296 brouard 7197: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
7198: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
7199: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
1.237 brouard 7200: int jj1, k1, i1, cpt, k4, nres;
1.319 brouard 7201: /* In fact some results are already printed in fichtm which is open */
1.126 brouard 7202: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
7203: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
7204: </ul>");
1.319 brouard 7205: /* fprintf(fichtm,"<ul><li> model=1+age+%s\n \ */
7206: /* </ul>", model); */
1.214 brouard 7207: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
7208: 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",
7209: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
7210: fprintf(fichtm,"<li> - Observed 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 7211: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
7212: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 7213: fprintf(fichtm,"\
7214: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 7215: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 7216: fprintf(fichtm,"\
1.217 brouard 7217: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
7218: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
7219: fprintf(fichtm,"\
1.288 brouard 7220: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 7221: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 7222: fprintf(fichtm,"\
1.288 brouard 7223: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.217 brouard 7224: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
7225: fprintf(fichtm,"\
1.211 brouard 7226: - (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 7227: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 7228: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 7229: if(prevfcast==1){
7230: fprintf(fichtm,"\
7231: - Prevalence projections by age and states: \
1.201 brouard 7232: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 7233: }
1.126 brouard 7234:
7235:
1.225 brouard 7236: m=pow(2,cptcoveff);
1.222 brouard 7237: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 7238:
1.317 brouard 7239: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>");
1.264 brouard 7240:
7241: jj1=0;
7242:
7243: fprintf(fichtm," \n<ul>");
7244: for(nres=1; nres <= nresult; nres++) /* For each resultline */
7245: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
7246: if(m != 1 && TKresult[nres]!= k1)
7247: continue;
7248: jj1++;
7249: if (cptcovn > 0) {
7250: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
7251: for (cpt=1; cpt<=cptcoveff;cpt++){
7252: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7253: }
7254: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7255: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
7256: }
7257: fprintf(fichtm,"\">");
7258:
7259: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7260: fprintf(fichtm,"************ Results for covariates");
7261: for (cpt=1; cpt<=cptcoveff;cpt++){
7262: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7263: }
7264: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7265: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7266: }
7267: if(invalidvarcomb[k1]){
7268: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
7269: continue;
7270: }
7271: fprintf(fichtm,"</a></li>");
7272: } /* cptcovn >0 */
7273: }
1.317 brouard 7274: fprintf(fichtm," \n</ul>");
1.264 brouard 7275:
1.222 brouard 7276: jj1=0;
1.237 brouard 7277:
7278: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.241 brouard 7279: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
1.253 brouard 7280: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7281: continue;
1.220 brouard 7282:
1.222 brouard 7283: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7284: jj1++;
7285: if (cptcovn > 0) {
1.264 brouard 7286: fprintf(fichtm,"\n<p><a name=\"rescov");
7287: for (cpt=1; cpt<=cptcoveff;cpt++){
7288: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7289: }
7290: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7291: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
7292: }
7293: fprintf(fichtm,"\"</a>");
7294:
1.222 brouard 7295: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 7296: for (cpt=1; cpt<=cptcoveff;cpt++){
1.237 brouard 7297: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7298: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
7299: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7300: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 7301: }
1.237 brouard 7302: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7303: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7304: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout);
7305: }
7306:
1.230 brouard 7307: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.321 brouard 7308: fprintf(fichtm," (model=%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.222 brouard 7309: if(invalidvarcomb[k1]){
7310: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
7311: printf("\nCombination (%d) ignored because no cases \n",k1);
7312: continue;
7313: }
7314: }
7315: /* aij, bij */
1.259 brouard 7316: 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 7317: <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 7318: /* Pij */
1.241 brouard 7319: 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> \
7320: <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 7321: /* Quasi-incidences */
7322: 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 7323: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 7324: 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 7325: 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> \
7326: <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 7327: /* Survival functions (period) in state j */
7328: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7329: 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. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> \
1.241 brouard 7330: <img src=\"%s_%d-%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.222 brouard 7331: }
7332: /* State specific survival functions (period) */
7333: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7334: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
7335: And probability to be observed in various states (up to %d) being in state %d at different ages. \
1.283 brouard 7336: <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> <img src=\"%s_%d-%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres,subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.222 brouard 7337: }
1.288 brouard 7338: /* Period (forward stable) prevalence in each health state */
1.222 brouard 7339: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7340: 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 in state %d some years after. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> \
7341: <img src=\"%s_%d-%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres,subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.222 brouard 7342: }
1.296 brouard 7343: if(prevbcast==1){
1.288 brouard 7344: /* Backward prevalence in each health state */
1.222 brouard 7345: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7346: 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> \
1.241 brouard 7347: <img src=\"%s_%d-%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.222 brouard 7348: }
1.217 brouard 7349: }
1.222 brouard 7350: if(prevfcast==1){
1.288 brouard 7351: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
1.222 brouard 7352: for(cpt=1; cpt<=nlstate;cpt++){
1.314 brouard 7353: 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);
7354: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
7355: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
7356: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.222 brouard 7357: }
7358: }
1.296 brouard 7359: if(prevbcast==1){
1.268 brouard 7360: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
7361: for(cpt=1; cpt<=nlstate;cpt++){
1.273 brouard 7362: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
7363: 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 \
7364: 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 7365: 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);
7366: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
7367: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
1.268 brouard 7368: }
7369: }
1.220 brouard 7370:
1.222 brouard 7371: for(cpt=1; cpt<=nlstate;cpt++) {
1.314 brouard 7372: 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);
7373: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
7374: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
1.222 brouard 7375: }
7376: /* } /\* end i1 *\/ */
7377: }/* End k1 */
7378: fprintf(fichtm,"</ul>");
1.126 brouard 7379:
1.222 brouard 7380: fprintf(fichtm,"\
1.126 brouard 7381: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 7382: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 7383: - 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 7384: But because parameters are usually highly correlated (a higher incidence of disability \
7385: and a higher incidence of recovery can give very close observed transition) it might \
7386: be very useful to look not only at linear confidence intervals estimated from the \
7387: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
7388: (parameters) of the logistic regression, it might be more meaningful to visualize the \
7389: covariance matrix of the one-step probabilities. \
7390: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 7391:
1.222 brouard 7392: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
7393: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
7394: fprintf(fichtm,"\
1.126 brouard 7395: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7396: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 7397:
1.222 brouard 7398: fprintf(fichtm,"\
1.126 brouard 7399: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7400: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
7401: fprintf(fichtm,"\
1.126 brouard 7402: - 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): \
7403: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7404: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 7405: fprintf(fichtm,"\
1.126 brouard 7406: - (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): \
7407: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7408: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 7409: fprintf(fichtm,"\
1.288 brouard 7410: - 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 7411: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
7412: fprintf(fichtm,"\
1.128 brouard 7413: - 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 7414: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
7415: fprintf(fichtm,"\
1.288 brouard 7416: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 7417: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 7418:
7419: /* if(popforecast==1) fprintf(fichtm,"\n */
7420: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
7421: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
7422: /* <br>",fileres,fileres,fileres,fileres); */
7423: /* else */
7424: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
1.222 brouard 7425: fflush(fichtm);
1.126 brouard 7426:
1.225 brouard 7427: m=pow(2,cptcoveff);
1.222 brouard 7428: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 7429:
1.317 brouard 7430: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>");
7431:
7432: jj1=0;
7433:
7434: fprintf(fichtm," \n<ul>");
7435: for(nres=1; nres <= nresult; nres++) /* For each resultline */
7436: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
7437: if(m != 1 && TKresult[nres]!= k1)
7438: continue;
7439: jj1++;
7440: if (cptcovn > 0) {
7441: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond");
7442: for (cpt=1; cpt<=cptcoveff;cpt++){
7443: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7444: }
7445: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7446: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
7447: }
7448: fprintf(fichtm,"\">");
7449:
7450: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7451: fprintf(fichtm,"************ Results for covariates");
7452: for (cpt=1; cpt<=cptcoveff;cpt++){
7453: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7454: }
7455: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7456: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7457: }
7458: if(invalidvarcomb[k1]){
7459: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
7460: continue;
7461: }
7462: fprintf(fichtm,"</a></li>");
7463: } /* cptcovn >0 */
7464: }
7465: fprintf(fichtm," \n</ul>");
7466:
1.222 brouard 7467: jj1=0;
1.237 brouard 7468:
1.241 brouard 7469: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.222 brouard 7470: for(k1=1; k1<=m;k1++){
1.253 brouard 7471: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7472: continue;
1.222 brouard 7473: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7474: jj1++;
1.126 brouard 7475: if (cptcovn > 0) {
1.317 brouard 7476: fprintf(fichtm,"\n<p><a name=\"rescovsecond");
7477: for (cpt=1; cpt<=cptcoveff;cpt++){
7478: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7479: }
7480: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7481: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
7482: }
7483: fprintf(fichtm,"\"</a>");
7484:
1.126 brouard 7485: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.317 brouard 7486: for (cpt=1; cpt<=cptcoveff;cpt++){ /**< cptcoveff number of variables */
1.237 brouard 7487: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);
1.317 brouard 7488: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
1.237 brouard 7489: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
1.317 brouard 7490: }
1.237 brouard 7491: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7492: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7493: }
7494:
1.321 brouard 7495: fprintf(fichtm," (model=%s) ************\n<hr size=\"2\" color=\"#EC5E5E\">",model);
1.220 brouard 7496:
1.222 brouard 7497: if(invalidvarcomb[k1]){
7498: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
7499: continue;
7500: }
1.126 brouard 7501: }
7502: for(cpt=1; cpt<=nlstate;cpt++) {
1.258 brouard 7503: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
1.314 brouard 7504: 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);
7505: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
7506: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
1.126 brouard 7507: }
7508: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.314 brouard 7509: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \
1.128 brouard 7510: true period expectancies (those weighted with period prevalences are also\
7511: drawn in addition to the population based expectancies computed using\
1.314 brouard 7512: 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);
7513: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
7514: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 7515: /* } /\* end i1 *\/ */
7516: }/* End k1 */
1.241 brouard 7517: }/* End nres */
1.222 brouard 7518: fprintf(fichtm,"</ul>");
7519: fflush(fichtm);
1.126 brouard 7520: }
7521:
7522: /******************* Gnuplot file **************/
1.296 brouard 7523: 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 7524:
7525: char dirfileres[132],optfileres[132];
1.264 brouard 7526: char gplotcondition[132], gplotlabel[132];
1.237 brouard 7527: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,k4=0,ij=0, ijp=0, l=0;
1.211 brouard 7528: int lv=0, vlv=0, kl=0;
1.130 brouard 7529: int ng=0;
1.201 brouard 7530: int vpopbased;
1.223 brouard 7531: int ioffset; /* variable offset for columns */
1.270 brouard 7532: int iyearc=1; /* variable column for year of projection */
7533: int iagec=1; /* variable column for age of projection */
1.235 brouard 7534: int nres=0; /* Index of resultline */
1.266 brouard 7535: int istart=1; /* For starting graphs in projections */
1.219 brouard 7536:
1.126 brouard 7537: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
7538: /* printf("Problem with file %s",optionfilegnuplot); */
7539: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
7540: /* } */
7541:
7542: /*#ifdef windows */
7543: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 7544: /*#endif */
1.225 brouard 7545: m=pow(2,cptcoveff);
1.126 brouard 7546:
1.274 brouard 7547: /* diagram of the model */
7548: fprintf(ficgp,"\n#Diagram of the model \n");
7549: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
7550: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
7551: 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);
7552:
7553: fprintf(ficgp,"\n#Centripete arrows (turning in other direction (1-i) instead of (i-1)) \nset for [i=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);
7554: fprintf(ficgp,"\n#show arrow\nunset label\n");
7555: 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);
7556: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
7557: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
7558: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
7559: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
7560:
1.202 brouard 7561: /* Contribution to likelihood */
7562: /* Plot the probability implied in the likelihood */
1.223 brouard 7563: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
7564: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
7565: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
7566: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 7567: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 7568: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
7569: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 7570: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
7571: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
7572: 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));
7573: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
7574: 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));
7575: for (i=1; i<= nlstate ; i ++) {
7576: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
7577: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
7578: 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);
7579: for (j=2; j<= nlstate+ndeath ; j ++) {
7580: 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);
7581: }
7582: fprintf(ficgp,";\nset out; unset ylabel;\n");
7583: }
7584: /* 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 */
7585: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
7586: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
7587: fprintf(ficgp,"\nset out;unset log\n");
7588: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 7589:
1.126 brouard 7590: strcpy(dirfileres,optionfilefiname);
7591: strcpy(optfileres,"vpl");
1.223 brouard 7592: /* 1eme*/
1.238 brouard 7593: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
7594: for (k1=1; k1<= m ; k1 ++){ /* For each valid combination of covariate */
1.236 brouard 7595: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.238 brouard 7596: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
1.253 brouard 7597: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7598: continue;
7599: /* We are interested in selected combination by the resultline */
1.246 brouard 7600: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
1.288 brouard 7601: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.264 brouard 7602: strcpy(gplotlabel,"(");
1.238 brouard 7603: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
7604: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
7605: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7606: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7607: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7608: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
7609: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
1.246 brouard 7610: /* printf(" V%d=%d ",Tvaraff[k],vlv); */
1.238 brouard 7611: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7612: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7613: }
7614: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.246 brouard 7615: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.238 brouard 7616: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7617: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7618: }
7619: strcpy(gplotlabel+strlen(gplotlabel),")");
1.246 brouard 7620: /* printf("\n#\n"); */
1.238 brouard 7621: fprintf(ficgp,"\n#\n");
7622: if(invalidvarcomb[k1]){
1.260 brouard 7623: /*k1=k1-1;*/ /* To be checked */
1.238 brouard 7624: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7625: continue;
7626: }
1.235 brouard 7627:
1.241 brouard 7628: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
7629: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
1.276 brouard 7630: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
1.321 brouard 7631: fprintf(ficgp,"set title \"Alive state %d %s model=%s\" font \"Helvetica,12\"\n",cpt,gplotlabel,model);
1.260 brouard 7632: 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);
7633: /* 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); */
7634: /* k1-1 error should be nres-1*/
1.238 brouard 7635: for (i=1; i<= nlstate ; i ++) {
7636: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7637: else fprintf(ficgp," %%*lf (%%*lf)");
7638: }
1.288 brouard 7639: 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 7640: for (i=1; i<= nlstate ; i ++) {
7641: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7642: else fprintf(ficgp," %%*lf (%%*lf)");
7643: }
1.260 brouard 7644: 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 7645: for (i=1; i<= nlstate ; i ++) {
7646: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7647: else fprintf(ficgp," %%*lf (%%*lf)");
7648: }
1.265 brouard 7649: /* 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)); */
7650:
7651: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
7652: if(cptcoveff ==0){
1.271 brouard 7653: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
1.265 brouard 7654: }else{
7655: kl=0;
7656: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7657: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7658: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7659: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7660: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7661: vlv= nbcode[Tvaraff[k]][lv];
7662: kl++;
7663: /* 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 *\/ */
7664: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7665: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7666: /* '' 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*/
7667: if(k==cptcoveff){
7668: 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], \
7669: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
7670: }else{
7671: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7672: kl++;
7673: }
7674: } /* end covariate */
7675: } /* end if no covariate */
7676:
1.296 brouard 7677: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
1.238 brouard 7678: /* 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 7679: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 7680: if(cptcoveff ==0){
1.245 brouard 7681: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
1.238 brouard 7682: }else{
7683: kl=0;
7684: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7685: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7686: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7687: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7688: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7689: vlv= nbcode[Tvaraff[k]][lv];
1.223 brouard 7690: kl++;
1.238 brouard 7691: /* 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 *\/ */
7692: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7693: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7694: /* '' 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*/
7695: if(k==cptcoveff){
1.245 brouard 7696: 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 7697: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 7698: }else{
7699: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7700: kl++;
7701: }
7702: } /* end covariate */
7703: } /* end if no covariate */
1.296 brouard 7704: if(prevbcast == 1){
1.268 brouard 7705: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
7706: /* k1-1 error should be nres-1*/
7707: for (i=1; i<= nlstate ; i ++) {
7708: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7709: else fprintf(ficgp," %%*lf (%%*lf)");
7710: }
1.271 brouard 7711: 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 7712: for (i=1; i<= nlstate ; i ++) {
7713: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7714: else fprintf(ficgp," %%*lf (%%*lf)");
7715: }
1.276 brouard 7716: 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 7717: for (i=1; i<= nlstate ; i ++) {
7718: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7719: else fprintf(ficgp," %%*lf (%%*lf)");
7720: }
1.274 brouard 7721: fprintf(ficgp,"\" t\"\" w l lt 4");
1.268 brouard 7722: } /* end if backprojcast */
1.296 brouard 7723: } /* end if prevbcast */
1.276 brouard 7724: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
7725: fprintf(ficgp,"\nset out ;unset title;\n");
1.238 brouard 7726: } /* nres */
1.201 brouard 7727: } /* k1 */
7728: } /* cpt */
1.235 brouard 7729:
7730:
1.126 brouard 7731: /*2 eme*/
1.238 brouard 7732: for (k1=1; k1<= m ; k1 ++){
7733: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7734: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7735: continue;
7736: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.264 brouard 7737: strcpy(gplotlabel,"(");
1.238 brouard 7738: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.225 brouard 7739: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 7740: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7741: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7742: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7743: vlv= nbcode[Tvaraff[k]][lv];
7744: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7745: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7746: }
1.237 brouard 7747: /* for(k=1; k <= ncovds; k++){ */
1.236 brouard 7748: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 7749: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.236 brouard 7750: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7751: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7752: }
1.264 brouard 7753: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7754: fprintf(ficgp,"\n#\n");
1.223 brouard 7755: if(invalidvarcomb[k1]){
7756: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7757: continue;
7758: }
1.219 brouard 7759:
1.241 brouard 7760: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 7761: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.264 brouard 7762: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
7763: if(vpopbased==0){
1.238 brouard 7764: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
1.264 brouard 7765: }else
1.238 brouard 7766: fprintf(ficgp,"\nreplot ");
7767: for (i=1; i<= nlstate+1 ; i ++) {
7768: k=2*i;
1.261 brouard 7769: 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 7770: for (j=1; j<= nlstate+1 ; j ++) {
7771: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7772: else fprintf(ficgp," %%*lf (%%*lf)");
7773: }
7774: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
7775: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
1.261 brouard 7776: 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 7777: for (j=1; j<= nlstate+1 ; j ++) {
7778: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7779: else fprintf(ficgp," %%*lf (%%*lf)");
7780: }
7781: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.261 brouard 7782: 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 7783: for (j=1; j<= nlstate+1 ; j ++) {
7784: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7785: else fprintf(ficgp," %%*lf (%%*lf)");
7786: }
7787: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
7788: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
7789: } /* state */
7790: } /* vpopbased */
1.264 brouard 7791: 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 7792: } /* end nres */
7793: } /* k1 end 2 eme*/
7794:
7795:
7796: /*3eme*/
7797: for (k1=1; k1<= m ; k1 ++){
7798: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7799: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7800: continue;
7801:
7802: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.261 brouard 7803: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.264 brouard 7804: strcpy(gplotlabel,"(");
1.238 brouard 7805: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7806: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7807: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7808: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7809: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7810: vlv= nbcode[Tvaraff[k]][lv];
7811: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7812: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7813: }
7814: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7815: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7816: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7817: }
1.264 brouard 7818: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7819: fprintf(ficgp,"\n#\n");
7820: if(invalidvarcomb[k1]){
7821: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7822: continue;
7823: }
7824:
7825: /* k=2+nlstate*(2*cpt-2); */
7826: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 7827: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.264 brouard 7828: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
1.238 brouard 7829: fprintf(ficgp,"set ter svg size 640, 480\n\
1.261 brouard 7830: 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 7831: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7832: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7833: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
7834: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7835: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7836: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 7837:
1.238 brouard 7838: */
7839: for (i=1; i< nlstate ; i ++) {
1.261 brouard 7840: 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 7841: /* 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 7842:
1.238 brouard 7843: }
1.261 brouard 7844: 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 7845: }
1.264 brouard 7846: fprintf(ficgp,"\nunset label;\n");
1.238 brouard 7847: } /* end nres */
7848: } /* end kl 3eme */
1.126 brouard 7849:
1.223 brouard 7850: /* 4eme */
1.201 brouard 7851: /* Survival functions (period) from state i in state j by initial state i */
1.238 brouard 7852: for (k1=1; k1<=m; k1++){ /* For each covariate and each value */
7853: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7854: if(m != 1 && TKresult[nres]!= k1)
1.223 brouard 7855: continue;
1.238 brouard 7856: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
1.264 brouard 7857: strcpy(gplotlabel,"(");
1.238 brouard 7858: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
7859: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7860: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7861: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7862: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7863: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7864: vlv= nbcode[Tvaraff[k]][lv];
7865: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7866: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7867: }
7868: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7869: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7870: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7871: }
1.264 brouard 7872: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7873: fprintf(ficgp,"\n#\n");
7874: if(invalidvarcomb[k1]){
7875: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7876: continue;
1.223 brouard 7877: }
1.238 brouard 7878:
1.241 brouard 7879: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.264 brouard 7880: 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 7881: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7882: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7883: k=3;
7884: for (i=1; i<= nlstate ; i ++){
7885: if(i==1){
7886: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7887: }else{
7888: fprintf(ficgp,", '' ");
7889: }
7890: l=(nlstate+ndeath)*(i-1)+1;
7891: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
7892: for (j=2; j<= nlstate+ndeath ; j ++)
7893: fprintf(ficgp,"+$%d",k+l+j-1);
7894: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
7895: } /* nlstate */
1.264 brouard 7896: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7897: } /* end cpt state*/
7898: } /* end nres */
7899: } /* end covariate k1 */
7900:
1.220 brouard 7901: /* 5eme */
1.201 brouard 7902: /* Survival functions (period) from state i in state j by final state j */
1.238 brouard 7903: for (k1=1; k1<= m ; k1++){ /* For each covariate combination if any */
7904: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7905: if(m != 1 && TKresult[nres]!= k1)
1.227 brouard 7906: continue;
1.238 brouard 7907: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.264 brouard 7908: strcpy(gplotlabel,"(");
1.238 brouard 7909: 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);
7910: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7911: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7912: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7913: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7914: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7915: vlv= nbcode[Tvaraff[k]][lv];
7916: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7917: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7918: }
7919: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7920: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7921: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7922: }
1.264 brouard 7923: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7924: fprintf(ficgp,"\n#\n");
7925: if(invalidvarcomb[k1]){
7926: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7927: continue;
7928: }
1.227 brouard 7929:
1.241 brouard 7930: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.264 brouard 7931: 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 7932: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7933: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7934: k=3;
7935: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7936: if(j==1)
7937: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7938: else
7939: fprintf(ficgp,", '' ");
7940: l=(nlstate+ndeath)*(cpt-1) +j;
7941: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
7942: /* for (i=2; i<= nlstate+ndeath ; i ++) */
7943: /* fprintf(ficgp,"+$%d",k+l+i-1); */
7944: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
7945: } /* nlstate */
7946: fprintf(ficgp,", '' ");
7947: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
7948: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7949: l=(nlstate+ndeath)*(cpt-1) +j;
7950: if(j < nlstate)
7951: fprintf(ficgp,"$%d +",k+l);
7952: else
7953: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
7954: }
1.264 brouard 7955: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7956: } /* end cpt state*/
7957: } /* end covariate */
7958: } /* end nres */
1.227 brouard 7959:
1.220 brouard 7960: /* 6eme */
1.202 brouard 7961: /* CV preval stable (period) for each covariate */
1.237 brouard 7962: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7963: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7964: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7965: continue;
1.255 brouard 7966: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
1.264 brouard 7967: strcpy(gplotlabel,"(");
1.288 brouard 7968: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 7969: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 7970: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7971: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7972: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7973: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7974: vlv= nbcode[Tvaraff[k]][lv];
7975: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7976: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7977: }
1.237 brouard 7978: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7979: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7980: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7981: }
1.264 brouard 7982: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7983: fprintf(ficgp,"\n#\n");
1.223 brouard 7984: if(invalidvarcomb[k1]){
1.227 brouard 7985: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7986: continue;
1.223 brouard 7987: }
1.227 brouard 7988:
1.241 brouard 7989: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.264 brouard 7990: 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 7991: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 7992: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 7993: k=3; /* Offset */
1.255 brouard 7994: for (i=1; i<= nlstate ; i ++){ /* State of origin */
1.227 brouard 7995: if(i==1)
7996: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7997: else
7998: fprintf(ficgp,", '' ");
1.255 brouard 7999: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 8000: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
8001: for (j=2; j<= nlstate ; j ++)
8002: fprintf(ficgp,"+$%d",k+l+j-1);
8003: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 8004: } /* nlstate */
1.264 brouard 8005: fprintf(ficgp,"\nset out; unset label;\n");
1.153 brouard 8006: } /* end cpt state*/
8007: } /* end covariate */
1.227 brouard 8008:
8009:
1.220 brouard 8010: /* 7eme */
1.296 brouard 8011: if(prevbcast == 1){
1.288 brouard 8012: /* CV backward prevalence for each covariate */
1.237 brouard 8013: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
8014: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 8015: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 8016: continue;
1.268 brouard 8017: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
1.264 brouard 8018: strcpy(gplotlabel,"(");
1.288 brouard 8019: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 8020: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
8021: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
8022: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8023: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 8024: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 8025: vlv= nbcode[Tvaraff[k]][lv];
8026: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 8027: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 8028: }
1.237 brouard 8029: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8030: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 8031: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 8032: }
1.264 brouard 8033: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 8034: fprintf(ficgp,"\n#\n");
8035: if(invalidvarcomb[k1]){
8036: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8037: continue;
8038: }
8039:
1.241 brouard 8040: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.268 brouard 8041: 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 8042: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 8043: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 8044: k=3; /* Offset */
1.268 brouard 8045: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
1.227 brouard 8046: if(i==1)
8047: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
8048: else
8049: fprintf(ficgp,", '' ");
8050: /* l=(nlstate+ndeath)*(i-1)+1; */
1.255 brouard 8051: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.324 brouard 8052: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
8053: /* 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 8054: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
1.227 brouard 8055: /* for (j=2; j<= nlstate ; j ++) */
8056: /* fprintf(ficgp,"+$%d",k+l+j-1); */
8057: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
1.268 brouard 8058: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
1.227 brouard 8059: } /* nlstate */
1.264 brouard 8060: fprintf(ficgp,"\nset out; unset label;\n");
1.218 brouard 8061: } /* end cpt state*/
8062: } /* end covariate */
1.296 brouard 8063: } /* End if prevbcast */
1.218 brouard 8064:
1.223 brouard 8065: /* 8eme */
1.218 brouard 8066: if(prevfcast==1){
1.288 brouard 8067: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
1.218 brouard 8068:
1.237 brouard 8069: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
8070: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 8071: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 8072: continue;
1.211 brouard 8073: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.264 brouard 8074: strcpy(gplotlabel,"(");
1.288 brouard 8075: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 8076: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
8077: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
8078: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8079: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8080: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8081: vlv= nbcode[Tvaraff[k]][lv];
8082: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 8083: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 8084: }
1.237 brouard 8085: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8086: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 8087: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 8088: }
1.264 brouard 8089: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 8090: fprintf(ficgp,"\n#\n");
8091: if(invalidvarcomb[k1]){
8092: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8093: continue;
8094: }
8095:
8096: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 8097: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.264 brouard 8098: 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 8099: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 8100: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.266 brouard 8101:
8102: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
8103: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
8104: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
8105: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
1.227 brouard 8106: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8107: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8108: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8109: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.266 brouard 8110: if(i==istart){
1.227 brouard 8111: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
8112: }else{
8113: fprintf(ficgp,",\\\n '' ");
8114: }
8115: if(cptcoveff ==0){ /* No covariate */
8116: ioffset=2; /* Age is in 2 */
8117: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8118: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8119: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8120: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8121: fprintf(ficgp," u %d:(", ioffset);
1.266 brouard 8122: if(i==nlstate+1){
1.270 brouard 8123: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
1.266 brouard 8124: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
8125: fprintf(ficgp,",\\\n '' ");
8126: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 8127: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
1.266 brouard 8128: offyear, \
1.268 brouard 8129: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
1.266 brouard 8130: }else
1.227 brouard 8131: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
8132: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
8133: }else{ /* more than 2 covariates */
1.270 brouard 8134: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
8135: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8136: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8137: iyearc=ioffset-1;
8138: iagec=ioffset;
1.227 brouard 8139: fprintf(ficgp," u %d:(",ioffset);
8140: kl=0;
8141: strcpy(gplotcondition,"(");
8142: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
8143: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
8144: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8145: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8146: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8147: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
8148: kl++;
8149: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
8150: kl++;
8151: if(k <cptcoveff && cptcoveff>1)
8152: sprintf(gplotcondition+strlen(gplotcondition)," && ");
8153: }
8154: strcpy(gplotcondition+strlen(gplotcondition),")");
8155: /* 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 *\/ */
8156: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8157: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8158: /* '' 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*/
8159: if(i==nlstate+1){
1.270 brouard 8160: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
8161: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
1.266 brouard 8162: fprintf(ficgp,",\\\n '' ");
1.270 brouard 8163: fprintf(ficgp," u %d:(",iagec);
8164: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
8165: iyearc, iagec, offyear, \
8166: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
1.266 brouard 8167: /* '' 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 8168: }else{
8169: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
8170: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
8171: }
8172: } /* end if covariate */
8173: } /* nlstate */
1.264 brouard 8174: fprintf(ficgp,"\nset out; unset label;\n");
1.223 brouard 8175: } /* end cpt state*/
8176: } /* end covariate */
8177: } /* End if prevfcast */
1.227 brouard 8178:
1.296 brouard 8179: if(prevbcast==1){
1.268 brouard 8180: /* Back projection from cross-sectional to stable (mixed) for each covariate */
8181:
8182: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
8183: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
8184: if(m != 1 && TKresult[nres]!= k1)
8185: continue;
8186: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
8187: strcpy(gplotlabel,"(");
8188: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
8189: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
8190: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
8191: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8192: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8193: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8194: vlv= nbcode[Tvaraff[k]][lv];
8195: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
8196: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
8197: }
8198: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8199: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
8200: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
8201: }
8202: strcpy(gplotlabel+strlen(gplotlabel),")");
8203: fprintf(ficgp,"\n#\n");
8204: if(invalidvarcomb[k1]){
8205: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
8206: continue;
8207: }
8208:
8209: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
8210: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
8211: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
8212: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
8213: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
8214:
8215: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
8216: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
8217: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
8218: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
8219: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8220: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8221: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8222: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8223: if(i==istart){
8224: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
8225: }else{
8226: fprintf(ficgp,",\\\n '' ");
8227: }
8228: if(cptcoveff ==0){ /* No covariate */
8229: ioffset=2; /* Age is in 2 */
8230: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8231: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8232: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
8233: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
8234: fprintf(ficgp," u %d:(", ioffset);
8235: if(i==nlstate+1){
1.270 brouard 8236: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
1.268 brouard 8237: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
8238: fprintf(ficgp,",\\\n '' ");
8239: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 8240: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
1.268 brouard 8241: offbyear, \
8242: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
8243: }else
8244: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
8245: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
8246: }else{ /* more than 2 covariates */
1.270 brouard 8247: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
8248: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
8249: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
8250: iyearc=ioffset-1;
8251: iagec=ioffset;
1.268 brouard 8252: fprintf(ficgp," u %d:(",ioffset);
8253: kl=0;
8254: strcpy(gplotcondition,"(");
8255: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
8256: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
8257: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8258: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8259: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8260: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
8261: kl++;
8262: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
8263: kl++;
8264: if(k <cptcoveff && cptcoveff>1)
8265: sprintf(gplotcondition+strlen(gplotcondition)," && ");
8266: }
8267: strcpy(gplotcondition+strlen(gplotcondition),")");
8268: /* 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 *\/ */
8269: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
8270: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
8271: /* '' 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*/
8272: if(i==nlstate+1){
1.270 brouard 8273: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
8274: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
1.268 brouard 8275: fprintf(ficgp,",\\\n '' ");
1.270 brouard 8276: fprintf(ficgp," u %d:(",iagec);
1.268 brouard 8277: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
1.270 brouard 8278: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
8279: iyearc,iagec,offbyear, \
8280: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
1.268 brouard 8281: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
8282: }else{
8283: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
8284: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
8285: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
8286: }
8287: } /* end if covariate */
8288: } /* nlstate */
8289: fprintf(ficgp,"\nset out; unset label;\n");
8290: } /* end cpt state*/
8291: } /* end covariate */
1.296 brouard 8292: } /* End if prevbcast */
1.268 brouard 8293:
1.227 brouard 8294:
1.238 brouard 8295: /* 9eme writing MLE parameters */
8296: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 8297: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 8298: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 8299: for(k=1; k <=(nlstate+ndeath); k++){
8300: if (k != i) {
1.227 brouard 8301: fprintf(ficgp,"# current state %d\n",k);
8302: for(j=1; j <=ncovmodel; j++){
8303: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
8304: jk++;
8305: }
8306: fprintf(ficgp,"\n");
1.126 brouard 8307: }
8308: }
1.223 brouard 8309: }
1.187 brouard 8310: fprintf(ficgp,"##############\n#\n");
1.227 brouard 8311:
1.145 brouard 8312: /*goto avoid;*/
1.238 brouard 8313: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
8314: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 8315: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
8316: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
8317: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
8318: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
8319: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8320: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
8321: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8322: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
8323: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
8324: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8325: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
8326: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
8327: fprintf(ficgp,"#\n");
1.223 brouard 8328: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 8329: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.237 brouard 8330: fprintf(ficgp,"#model=%s \n",model);
1.238 brouard 8331: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.264 brouard 8332: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
8333: for(k1=1; k1 <=m; k1++) /* For each combination of covariate */
1.237 brouard 8334: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.264 brouard 8335: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 8336: continue;
1.264 brouard 8337: fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1);
8338: strcpy(gplotlabel,"(");
1.276 brouard 8339: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
1.264 brouard 8340: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
8341: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
8342: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8343: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8344: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8345: vlv= nbcode[Tvaraff[k]][lv];
8346: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
8347: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
8348: }
1.237 brouard 8349: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8350: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 8351: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 8352: }
1.264 brouard 8353: strcpy(gplotlabel+strlen(gplotlabel),")");
1.237 brouard 8354: fprintf(ficgp,"\n#\n");
1.264 brouard 8355: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
1.276 brouard 8356: fprintf(ficgp,"\nset key outside ");
8357: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
8358: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
1.223 brouard 8359: fprintf(ficgp,"\nset ter svg size 640, 480 ");
8360: if (ng==1){
8361: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
8362: fprintf(ficgp,"\nunset log y");
8363: }else if (ng==2){
8364: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
8365: fprintf(ficgp,"\nset log y");
8366: }else if (ng==3){
8367: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
8368: fprintf(ficgp,"\nset log y");
8369: }else
8370: fprintf(ficgp,"\nunset title ");
8371: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
8372: i=1;
8373: for(k2=1; k2<=nlstate; k2++) {
8374: k3=i;
8375: for(k=1; k<=(nlstate+ndeath); k++) {
8376: if (k != k2){
8377: switch( ng) {
8378: case 1:
8379: if(nagesqr==0)
8380: fprintf(ficgp," p%d+p%d*x",i,i+1);
8381: else /* nagesqr =1 */
8382: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8383: break;
8384: case 2: /* ng=2 */
8385: if(nagesqr==0)
8386: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
8387: else /* nagesqr =1 */
8388: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8389: break;
8390: case 3:
8391: if(nagesqr==0)
8392: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
8393: else /* nagesqr =1 */
8394: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
8395: break;
8396: }
8397: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 8398: ijp=1; /* product no age */
8399: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
8400: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 8401: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.268 brouard 8402: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
1.325 brouard 8403: if(j==Tage[ij]) { /* Product by age To be looked at!!*//* Bug valgrind */
1.268 brouard 8404: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
1.325 brouard 8405: if(DummyV[j]==0){/* Bug valgrind */
1.268 brouard 8406: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
8407: }else{ /* quantitative */
8408: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
8409: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8410: }
8411: ij++;
1.237 brouard 8412: }
1.268 brouard 8413: }
8414: }else if(cptcovprod >0){
8415: if(j==Tprod[ijp]) { /* */
8416: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
8417: if(ijp <=cptcovprod) { /* Product */
8418: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
8419: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
8420: /* 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)]); */
8421: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
8422: }else{ /* Vn is dummy and Vm is quanti */
8423: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
8424: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8425: }
8426: }else{ /* Vn*Vm Vn is quanti */
8427: if(DummyV[Tvard[ijp][2]]==0){
8428: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
8429: }else{ /* Both quanti */
8430: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8431: }
1.237 brouard 8432: }
1.268 brouard 8433: ijp++;
1.237 brouard 8434: }
1.268 brouard 8435: } /* end Tprod */
1.237 brouard 8436: } else{ /* simple covariate */
1.264 brouard 8437: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
1.237 brouard 8438: if(Dummy[j]==0){
8439: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
8440: }else{ /* quantitative */
8441: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.264 brouard 8442: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
1.223 brouard 8443: }
1.237 brouard 8444: } /* end simple */
8445: } /* end j */
1.223 brouard 8446: }else{
8447: i=i-ncovmodel;
8448: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
8449: fprintf(ficgp," (1.");
8450: }
1.227 brouard 8451:
1.223 brouard 8452: if(ng != 1){
8453: fprintf(ficgp,")/(1");
1.227 brouard 8454:
1.264 brouard 8455: for(cpt=1; cpt <=nlstate; cpt++){
1.223 brouard 8456: if(nagesqr==0)
1.264 brouard 8457: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
1.223 brouard 8458: else /* nagesqr =1 */
1.264 brouard 8459: 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 8460:
1.223 brouard 8461: ij=1;
8462: for(j=3; j <=ncovmodel-nagesqr; j++){
1.268 brouard 8463: if(cptcovage >0){
8464: if((j-2)==Tage[ij]) { /* Bug valgrind */
8465: if(ij <=cptcovage) { /* Bug valgrind */
8466: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);
8467: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8468: ij++;
8469: }
8470: }
8471: }else
8472: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/* Valgrind bug nbcode */
1.223 brouard 8473: }
8474: fprintf(ficgp,")");
8475: }
8476: fprintf(ficgp,")");
8477: if(ng ==2)
1.276 brouard 8478: 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 8479: else /* ng= 3 */
1.276 brouard 8480: 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.223 brouard 8481: }else{ /* end ng <> 1 */
8482: if( k !=k2) /* logit p11 is hard to draw */
1.276 brouard 8483: 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 8484: }
8485: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
8486: fprintf(ficgp,",");
8487: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
8488: fprintf(ficgp,",");
8489: i=i+ncovmodel;
8490: } /* end k */
8491: } /* end k2 */
1.276 brouard 8492: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
8493: fprintf(ficgp,"\n set out; unset title;set key default;\n");
1.264 brouard 8494: } /* end k1 */
1.223 brouard 8495: } /* end ng */
8496: /* avoid: */
8497: fflush(ficgp);
1.126 brouard 8498: } /* end gnuplot */
8499:
8500:
8501: /*************** Moving average **************/
1.219 brouard 8502: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 8503: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 8504:
1.222 brouard 8505: int i, cpt, cptcod;
8506: int modcovmax =1;
8507: int mobilavrange, mob;
8508: int iage=0;
1.288 brouard 8509: int firstA1=0, firstA2=0;
1.222 brouard 8510:
1.266 brouard 8511: double sum=0., sumr=0.;
1.222 brouard 8512: double age;
1.266 brouard 8513: double *sumnewp, *sumnewm, *sumnewmr;
8514: double *agemingood, *agemaxgood;
8515: double *agemingoodr, *agemaxgoodr;
1.222 brouard 8516:
8517:
1.278 brouard 8518: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
8519: /* a covariate has 2 modalities, should be equal to ncovcombmax */
1.222 brouard 8520:
8521: sumnewp = vector(1,ncovcombmax);
8522: sumnewm = vector(1,ncovcombmax);
1.266 brouard 8523: sumnewmr = vector(1,ncovcombmax);
1.222 brouard 8524: agemingood = vector(1,ncovcombmax);
1.266 brouard 8525: agemingoodr = vector(1,ncovcombmax);
1.222 brouard 8526: agemaxgood = vector(1,ncovcombmax);
1.266 brouard 8527: agemaxgoodr = vector(1,ncovcombmax);
1.222 brouard 8528:
8529: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
1.266 brouard 8530: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
1.222 brouard 8531: sumnewp[cptcod]=0.;
1.266 brouard 8532: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
8533: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
1.222 brouard 8534: }
8535: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
8536:
1.266 brouard 8537: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
8538: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
1.222 brouard 8539: else mobilavrange=mobilav;
8540: for (age=bage; age<=fage; age++)
8541: for (i=1; i<=nlstate;i++)
8542: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
8543: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8544: /* We keep the original values on the extreme ages bage, fage and for
8545: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
8546: we use a 5 terms etc. until the borders are no more concerned.
8547: */
8548: for (mob=3;mob <=mobilavrange;mob=mob+2){
8549: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
1.266 brouard 8550: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
8551: sumnewm[cptcod]=0.;
8552: for (i=1; i<=nlstate;i++){
1.222 brouard 8553: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
8554: for (cpt=1;cpt<=(mob-1)/2;cpt++){
8555: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
8556: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
8557: }
8558: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
1.266 brouard 8559: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8560: } /* end i */
8561: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
8562: } /* end cptcod */
1.222 brouard 8563: }/* end age */
8564: }/* end mob */
1.266 brouard 8565: }else{
8566: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
1.222 brouard 8567: return -1;
1.266 brouard 8568: }
8569:
8570: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
1.222 brouard 8571: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
8572: if(invalidvarcomb[cptcod]){
8573: printf("\nCombination (%d) ignored because no cases \n",cptcod);
8574: continue;
8575: }
1.219 brouard 8576:
1.266 brouard 8577: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
8578: sumnewm[cptcod]=0.;
8579: sumnewmr[cptcod]=0.;
8580: for (i=1; i<=nlstate;i++){
8581: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8582: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8583: }
8584: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8585: agemingoodr[cptcod]=age;
8586: }
8587: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8588: agemingood[cptcod]=age;
8589: }
8590: } /* age */
8591: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
1.222 brouard 8592: sumnewm[cptcod]=0.;
1.266 brouard 8593: sumnewmr[cptcod]=0.;
1.222 brouard 8594: for (i=1; i<=nlstate;i++){
8595: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8596: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8597: }
8598: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8599: agemaxgoodr[cptcod]=age;
1.222 brouard 8600: }
8601: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
1.266 brouard 8602: agemaxgood[cptcod]=age;
8603: }
8604: } /* age */
8605: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
8606: /* but they will change */
1.288 brouard 8607: firstA1=0;firstA2=0;
1.266 brouard 8608: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
8609: sumnewm[cptcod]=0.;
8610: sumnewmr[cptcod]=0.;
8611: for (i=1; i<=nlstate;i++){
8612: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8613: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8614: }
8615: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8616: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8617: agemaxgoodr[cptcod]=age; /* age min */
8618: for (i=1; i<=nlstate;i++)
8619: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8620: }else{ /* bad we change the value with the values of good ages */
8621: for (i=1; i<=nlstate;i++){
8622: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
8623: } /* i */
8624: } /* end bad */
8625: }else{
8626: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8627: agemaxgood[cptcod]=age;
8628: }else{ /* bad we change the value with the values of good ages */
8629: for (i=1; i<=nlstate;i++){
8630: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
8631: } /* i */
8632: } /* end bad */
8633: }/* end else */
8634: sum=0.;sumr=0.;
8635: for (i=1; i<=nlstate;i++){
8636: sum+=mobaverage[(int)age][i][cptcod];
8637: sumr+=probs[(int)age][i][cptcod];
8638: }
8639: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.288 brouard 8640: if(!firstA1){
8641: firstA1=1;
8642: 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);
8643: }
8644: 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 8645: } /* end bad */
8646: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8647: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.288 brouard 8648: if(!firstA2){
8649: firstA2=1;
8650: 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);
8651: }
8652: 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 8653: } /* end bad */
8654: }/* age */
1.266 brouard 8655:
8656: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
1.222 brouard 8657: sumnewm[cptcod]=0.;
1.266 brouard 8658: sumnewmr[cptcod]=0.;
1.222 brouard 8659: for (i=1; i<=nlstate;i++){
8660: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8661: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8662: }
8663: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8664: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
8665: agemingoodr[cptcod]=age;
8666: for (i=1; i<=nlstate;i++)
8667: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8668: }else{ /* bad we change the value with the values of good ages */
8669: for (i=1; i<=nlstate;i++){
8670: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
8671: } /* i */
8672: } /* end bad */
8673: }else{
8674: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8675: agemingood[cptcod]=age;
8676: }else{ /* bad */
8677: for (i=1; i<=nlstate;i++){
8678: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
8679: } /* i */
8680: } /* end bad */
8681: }/* end else */
8682: sum=0.;sumr=0.;
8683: for (i=1; i<=nlstate;i++){
8684: sum+=mobaverage[(int)age][i][cptcod];
8685: sumr+=mobaverage[(int)age][i][cptcod];
1.222 brouard 8686: }
1.266 brouard 8687: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.268 brouard 8688: 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 8689: } /* end bad */
8690: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8691: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.268 brouard 8692: 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 8693: } /* end bad */
8694: }/* age */
1.266 brouard 8695:
1.222 brouard 8696:
8697: for (age=bage; age<=fage; age++){
1.235 brouard 8698: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 8699: sumnewp[cptcod]=0.;
8700: sumnewm[cptcod]=0.;
8701: for (i=1; i<=nlstate;i++){
8702: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
8703: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8704: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
8705: }
8706: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
8707: }
8708: /* printf("\n"); */
8709: /* } */
1.266 brouard 8710:
1.222 brouard 8711: /* brutal averaging */
1.266 brouard 8712: /* for (i=1; i<=nlstate;i++){ */
8713: /* for (age=1; age<=bage; age++){ */
8714: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
8715: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8716: /* } */
8717: /* for (age=fage; age<=AGESUP; age++){ */
8718: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
8719: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8720: /* } */
8721: /* } /\* end i status *\/ */
8722: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
8723: /* for (age=1; age<=AGESUP; age++){ */
8724: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
8725: /* mobaverage[(int)age][i][cptcod]=0.; */
8726: /* } */
8727: /* } */
1.222 brouard 8728: }/* end cptcod */
1.266 brouard 8729: free_vector(agemaxgoodr,1, ncovcombmax);
8730: free_vector(agemaxgood,1, ncovcombmax);
8731: free_vector(agemingood,1, ncovcombmax);
8732: free_vector(agemingoodr,1, ncovcombmax);
8733: free_vector(sumnewmr,1, ncovcombmax);
1.222 brouard 8734: free_vector(sumnewm,1, ncovcombmax);
8735: free_vector(sumnewp,1, ncovcombmax);
8736: return 0;
8737: }/* End movingaverage */
1.218 brouard 8738:
1.126 brouard 8739:
1.296 brouard 8740:
1.126 brouard 8741: /************** Forecasting ******************/
1.296 brouard 8742: /* 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)*/
8743: 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){
8744: /* dateintemean, mean date of interviews
8745: dateprojd, year, month, day of starting projection
8746: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
1.126 brouard 8747: agemin, agemax range of age
8748: dateprev1 dateprev2 range of dates during which prevalence is computed
8749: */
1.296 brouard 8750: /* double anprojd, mprojd, jprojd; */
8751: /* double anprojf, mprojf, jprojf; */
1.267 brouard 8752: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
1.126 brouard 8753: double agec; /* generic age */
1.296 brouard 8754: double agelim, ppij, yp,yp1,yp2;
1.126 brouard 8755: double *popeffectif,*popcount;
8756: double ***p3mat;
1.218 brouard 8757: /* double ***mobaverage; */
1.126 brouard 8758: char fileresf[FILENAMELENGTH];
8759:
8760: agelim=AGESUP;
1.211 brouard 8761: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8762: in each health status at the date of interview (if between dateprev1 and dateprev2).
8763: We still use firstpass and lastpass as another selection.
8764: */
1.214 brouard 8765: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8766: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 8767:
1.201 brouard 8768: strcpy(fileresf,"F_");
8769: strcat(fileresf,fileresu);
1.126 brouard 8770: if((ficresf=fopen(fileresf,"w"))==NULL) {
8771: printf("Problem with forecast resultfile: %s\n", fileresf);
8772: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
8773: }
1.235 brouard 8774: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
8775: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 8776:
1.225 brouard 8777: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 8778:
8779:
8780: stepsize=(int) (stepm+YEARM-1)/YEARM;
8781: if (stepm<=12) stepsize=1;
8782: if(estepm < stepm){
8783: printf ("Problem %d lower than %d\n",estepm, stepm);
8784: }
1.270 brouard 8785: else{
8786: hstepm=estepm;
8787: }
8788: if(estepm > stepm){ /* Yes every two year */
8789: stepsize=2;
8790: }
1.296 brouard 8791: hstepm=hstepm/stepm;
1.126 brouard 8792:
1.296 brouard 8793:
8794: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8795: /* fractional in yp1 *\/ */
8796: /* aintmean=yp; */
8797: /* yp2=modf((yp1*12),&yp); */
8798: /* mintmean=yp; */
8799: /* yp1=modf((yp2*30.5),&yp); */
8800: /* jintmean=yp; */
8801: /* if(jintmean==0) jintmean=1; */
8802: /* if(mintmean==0) mintmean=1; */
1.126 brouard 8803:
1.296 brouard 8804:
8805: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
8806: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
8807: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
1.227 brouard 8808: i1=pow(2,cptcoveff);
1.126 brouard 8809: if (cptcovn < 1){i1=1;}
8810:
1.296 brouard 8811: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.126 brouard 8812:
8813: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 8814:
1.126 brouard 8815: /* if (h==(int)(YEARM*yearp)){ */
1.235 brouard 8816: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8817: for(k=1; k<=i1;k++){
1.253 brouard 8818: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 8819: continue;
1.227 brouard 8820: if(invalidvarcomb[k]){
8821: printf("\nCombination (%d) projection ignored because no cases \n",k);
8822: continue;
8823: }
8824: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
8825: for(j=1;j<=cptcoveff;j++) {
8826: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8827: }
1.235 brouard 8828: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 8829: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.235 brouard 8830: }
1.227 brouard 8831: fprintf(ficresf," yearproj age");
8832: for(j=1; j<=nlstate+ndeath;j++){
8833: for(i=1; i<=nlstate;i++)
8834: fprintf(ficresf," p%d%d",i,j);
8835: fprintf(ficresf," wp.%d",j);
8836: }
1.296 brouard 8837: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
1.227 brouard 8838: fprintf(ficresf,"\n");
1.296 brouard 8839: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
1.270 brouard 8840: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
8841: for (agec=fage; agec>=(bage); agec--){
1.227 brouard 8842: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
8843: nhstepm = nhstepm/hstepm;
8844: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8845: oldm=oldms;savm=savms;
1.268 brouard 8846: /* We compute pii at age agec over nhstepm);*/
1.235 brouard 8847: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.268 brouard 8848: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
1.227 brouard 8849: for (h=0; h<=nhstepm; h++){
8850: if (h*hstepm/YEARM*stepm ==yearp) {
1.268 brouard 8851: break;
8852: }
8853: }
8854: fprintf(ficresf,"\n");
8855: for(j=1;j<=cptcoveff;j++)
8856: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 8857: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
1.268 brouard 8858:
8859: for(j=1; j<=nlstate+ndeath;j++) {
8860: ppij=0.;
8861: for(i=1; i<=nlstate;i++) {
1.278 brouard 8862: if (mobilav>=1)
8863: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
8864: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
8865: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
8866: }
1.268 brouard 8867: fprintf(ficresf," %.3f", p3mat[i][j][h]);
8868: } /* end i */
8869: fprintf(ficresf," %.3f", ppij);
8870: }/* end j */
1.227 brouard 8871: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8872: } /* end agec */
1.266 brouard 8873: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
8874: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
1.227 brouard 8875: } /* end yearp */
8876: } /* end k */
1.219 brouard 8877:
1.126 brouard 8878: fclose(ficresf);
1.215 brouard 8879: printf("End of Computing forecasting \n");
8880: fprintf(ficlog,"End of Computing forecasting\n");
8881:
1.126 brouard 8882: }
8883:
1.269 brouard 8884: /************** Back Forecasting ******************/
1.296 brouard 8885: /* 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){ */
8886: 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){
8887: /* back1, year, month, day of starting backprojection
1.267 brouard 8888: agemin, agemax range of age
8889: dateprev1 dateprev2 range of dates during which prevalence is computed
1.269 brouard 8890: anback2 year of end of backprojection (same day and month as back1).
8891: prevacurrent and prev are prevalences.
1.267 brouard 8892: */
8893: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
8894: double agec; /* generic age */
1.302 brouard 8895: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
1.267 brouard 8896: double *popeffectif,*popcount;
8897: double ***p3mat;
8898: /* double ***mobaverage; */
8899: char fileresfb[FILENAMELENGTH];
8900:
1.268 brouard 8901: agelim=AGEINF;
1.267 brouard 8902: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8903: in each health status at the date of interview (if between dateprev1 and dateprev2).
8904: We still use firstpass and lastpass as another selection.
8905: */
8906: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8907: /* firstpass, lastpass, stepm, weightopt, model); */
8908:
8909: /*Do we need to compute prevalence again?*/
8910:
8911: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
8912:
8913: strcpy(fileresfb,"FB_");
8914: strcat(fileresfb,fileresu);
8915: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
8916: printf("Problem with back forecast resultfile: %s\n", fileresfb);
8917: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
8918: }
8919: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8920: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8921:
8922: if (cptcoveff==0) ncodemax[cptcoveff]=1;
8923:
8924:
8925: stepsize=(int) (stepm+YEARM-1)/YEARM;
8926: if (stepm<=12) stepsize=1;
8927: if(estepm < stepm){
8928: printf ("Problem %d lower than %d\n",estepm, stepm);
8929: }
1.270 brouard 8930: else{
8931: hstepm=estepm;
8932: }
8933: if(estepm >= stepm){ /* Yes every two year */
8934: stepsize=2;
8935: }
1.267 brouard 8936:
8937: hstepm=hstepm/stepm;
1.296 brouard 8938: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8939: /* fractional in yp1 *\/ */
8940: /* aintmean=yp; */
8941: /* yp2=modf((yp1*12),&yp); */
8942: /* mintmean=yp; */
8943: /* yp1=modf((yp2*30.5),&yp); */
8944: /* jintmean=yp; */
8945: /* if(jintmean==0) jintmean=1; */
8946: /* if(mintmean==0) jintmean=1; */
1.267 brouard 8947:
8948: i1=pow(2,cptcoveff);
8949: if (cptcovn < 1){i1=1;}
8950:
1.296 brouard 8951: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
8952: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.267 brouard 8953:
8954: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
8955:
8956: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8957: for(k=1; k<=i1;k++){
8958: if(i1 != 1 && TKresult[nres]!= k)
8959: continue;
8960: if(invalidvarcomb[k]){
8961: printf("\nCombination (%d) projection ignored because no cases \n",k);
8962: continue;
8963: }
1.268 brouard 8964: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
1.267 brouard 8965: for(j=1;j<=cptcoveff;j++) {
8966: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8967: }
8968: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8969: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
8970: }
8971: fprintf(ficresfb," yearbproj age");
8972: for(j=1; j<=nlstate+ndeath;j++){
8973: for(i=1; i<=nlstate;i++)
1.268 brouard 8974: fprintf(ficresfb," b%d%d",i,j);
8975: fprintf(ficresfb," b.%d",j);
1.267 brouard 8976: }
1.296 brouard 8977: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
1.267 brouard 8978: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
8979: fprintf(ficresfb,"\n");
1.296 brouard 8980: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
1.273 brouard 8981: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
1.270 brouard 8982: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
8983: for (agec=bage; agec<=fage; agec++){ /* testing */
1.268 brouard 8984: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
1.271 brouard 8985: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
1.267 brouard 8986: nhstepm = nhstepm/hstepm;
8987: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8988: oldm=oldms;savm=savms;
1.268 brouard 8989: /* computes hbxij at age agec over 1 to nhstepm */
1.271 brouard 8990: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
1.267 brouard 8991: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.268 brouard 8992: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
8993: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
8994: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
1.267 brouard 8995: for (h=0; h<=nhstepm; h++){
1.268 brouard 8996: if (h*hstepm/YEARM*stepm ==-yearp) {
8997: break;
8998: }
8999: }
9000: fprintf(ficresfb,"\n");
9001: for(j=1;j<=cptcoveff;j++)
9002: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 9003: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
1.268 brouard 9004: for(i=1; i<=nlstate+ndeath;i++) {
9005: ppij=0.;ppi=0.;
9006: for(j=1; j<=nlstate;j++) {
9007: /* if (mobilav==1) */
1.269 brouard 9008: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
9009: ppi=ppi+prevacurrent[(int)agec][j][k];
9010: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
9011: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
1.267 brouard 9012: /* else { */
9013: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
9014: /* } */
1.268 brouard 9015: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
9016: } /* end j */
9017: if(ppi <0.99){
9018: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
9019: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
9020: }
9021: fprintf(ficresfb," %.3f", ppij);
9022: }/* end j */
1.267 brouard 9023: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
9024: } /* end agec */
9025: } /* end yearp */
9026: } /* end k */
1.217 brouard 9027:
1.267 brouard 9028: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
1.217 brouard 9029:
1.267 brouard 9030: fclose(ficresfb);
9031: printf("End of Computing Back forecasting \n");
9032: fprintf(ficlog,"End of Computing Back forecasting\n");
1.218 brouard 9033:
1.267 brouard 9034: }
1.217 brouard 9035:
1.269 brouard 9036: /* Variance of prevalence limit: varprlim */
9037: 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 9038: /*------- Variance of forward period (stable) prevalence------*/
1.269 brouard 9039:
9040: char fileresvpl[FILENAMELENGTH];
9041: FILE *ficresvpl;
9042: double **oldm, **savm;
9043: double **varpl; /* Variances of prevalence limits by age */
9044: int i1, k, nres, j ;
9045:
9046: strcpy(fileresvpl,"VPL_");
9047: strcat(fileresvpl,fileresu);
9048: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.288 brouard 9049: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
1.269 brouard 9050: exit(0);
9051: }
1.288 brouard 9052: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
9053: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.269 brouard 9054:
9055: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
9056: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
9057:
9058: i1=pow(2,cptcoveff);
9059: if (cptcovn < 1){i1=1;}
9060:
9061: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9062: for(k=1; k<=i1;k++){
9063: if(i1 != 1 && TKresult[nres]!= k)
9064: continue;
9065: fprintf(ficresvpl,"\n#****** ");
9066: printf("\n#****** ");
9067: fprintf(ficlog,"\n#****** ");
9068: for(j=1;j<=cptcoveff;j++) {
9069: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9070: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9071: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9072: }
9073: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
9074: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9075: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9076: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9077: }
9078: fprintf(ficresvpl,"******\n");
9079: printf("******\n");
9080: fprintf(ficlog,"******\n");
9081:
9082: varpl=matrix(1,nlstate,(int) bage, (int) fage);
9083: oldm=oldms;savm=savms;
9084: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
9085: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
9086: /*}*/
9087: }
9088:
9089: fclose(ficresvpl);
1.288 brouard 9090: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
9091: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
1.269 brouard 9092:
9093: }
9094: /* Variance of back prevalence: varbprlim */
9095: 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){
9096: /*------- Variance of back (stable) prevalence------*/
9097:
9098: char fileresvbl[FILENAMELENGTH];
9099: FILE *ficresvbl;
9100:
9101: double **oldm, **savm;
9102: double **varbpl; /* Variances of back prevalence limits by age */
9103: int i1, k, nres, j ;
9104:
9105: strcpy(fileresvbl,"VBL_");
9106: strcat(fileresvbl,fileresu);
9107: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
9108: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
9109: exit(0);
9110: }
9111: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
9112: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
9113:
9114:
9115: i1=pow(2,cptcoveff);
9116: if (cptcovn < 1){i1=1;}
9117:
9118: for(nres=1; nres <= nresult; nres++) /* For each resultline */
9119: for(k=1; k<=i1;k++){
9120: if(i1 != 1 && TKresult[nres]!= k)
9121: continue;
9122: fprintf(ficresvbl,"\n#****** ");
9123: printf("\n#****** ");
9124: fprintf(ficlog,"\n#****** ");
9125: for(j=1;j<=cptcoveff;j++) {
9126: fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9127: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9128: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
9129: }
9130: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
9131: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9132: fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9133: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
9134: }
9135: fprintf(ficresvbl,"******\n");
9136: printf("******\n");
9137: fprintf(ficlog,"******\n");
9138:
9139: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
9140: oldm=oldms;savm=savms;
9141:
9142: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
9143: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
9144: /*}*/
9145: }
9146:
9147: fclose(ficresvbl);
9148: printf("done variance-covariance of back prevalence\n");fflush(stdout);
9149: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
9150:
9151: } /* End of varbprlim */
9152:
1.126 brouard 9153: /************** Forecasting *****not tested NB*************/
1.227 brouard 9154: /* 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 9155:
1.227 brouard 9156: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
9157: /* int *popage; */
9158: /* double calagedatem, agelim, kk1, kk2; */
9159: /* double *popeffectif,*popcount; */
9160: /* double ***p3mat,***tabpop,***tabpopprev; */
9161: /* /\* double ***mobaverage; *\/ */
9162: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 9163:
1.227 brouard 9164: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9165: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9166: /* agelim=AGESUP; */
9167: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 9168:
1.227 brouard 9169: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 9170:
9171:
1.227 brouard 9172: /* strcpy(filerespop,"POP_"); */
9173: /* strcat(filerespop,fileresu); */
9174: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
9175: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
9176: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
9177: /* } */
9178: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
9179: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 9180:
1.227 brouard 9181: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 9182:
1.227 brouard 9183: /* /\* if (mobilav!=0) { *\/ */
9184: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
9185: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
9186: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
9187: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
9188: /* /\* } *\/ */
9189: /* /\* } *\/ */
1.126 brouard 9190:
1.227 brouard 9191: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
9192: /* if (stepm<=12) stepsize=1; */
1.126 brouard 9193:
1.227 brouard 9194: /* agelim=AGESUP; */
1.126 brouard 9195:
1.227 brouard 9196: /* hstepm=1; */
9197: /* hstepm=hstepm/stepm; */
1.218 brouard 9198:
1.227 brouard 9199: /* if (popforecast==1) { */
9200: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
9201: /* printf("Problem with population file : %s\n",popfile);exit(0); */
9202: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
9203: /* } */
9204: /* popage=ivector(0,AGESUP); */
9205: /* popeffectif=vector(0,AGESUP); */
9206: /* popcount=vector(0,AGESUP); */
1.126 brouard 9207:
1.227 brouard 9208: /* i=1; */
9209: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 9210:
1.227 brouard 9211: /* imx=i; */
9212: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
9213: /* } */
1.218 brouard 9214:
1.227 brouard 9215: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
9216: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
9217: /* k=k+1; */
9218: /* fprintf(ficrespop,"\n#******"); */
9219: /* for(j=1;j<=cptcoveff;j++) { */
9220: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
9221: /* } */
9222: /* fprintf(ficrespop,"******\n"); */
9223: /* fprintf(ficrespop,"# Age"); */
9224: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
9225: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 9226:
1.227 brouard 9227: /* for (cpt=0; cpt<=0;cpt++) { */
9228: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 9229:
1.227 brouard 9230: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
9231: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
9232: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 9233:
1.227 brouard 9234: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9235: /* oldm=oldms;savm=savms; */
9236: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 9237:
1.227 brouard 9238: /* for (h=0; h<=nhstepm; h++){ */
9239: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
9240: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
9241: /* } */
9242: /* for(j=1; j<=nlstate+ndeath;j++) { */
9243: /* kk1=0.;kk2=0; */
9244: /* for(i=1; i<=nlstate;i++) { */
9245: /* if (mobilav==1) */
9246: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
9247: /* else { */
9248: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
9249: /* } */
9250: /* } */
9251: /* if (h==(int)(calagedatem+12*cpt)){ */
9252: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
9253: /* /\*fprintf(ficrespop," %.3f", kk1); */
9254: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
9255: /* } */
9256: /* } */
9257: /* for(i=1; i<=nlstate;i++){ */
9258: /* kk1=0.; */
9259: /* for(j=1; j<=nlstate;j++){ */
9260: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
9261: /* } */
9262: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
9263: /* } */
1.218 brouard 9264:
1.227 brouard 9265: /* if (h==(int)(calagedatem+12*cpt)) */
9266: /* for(j=1; j<=nlstate;j++) */
9267: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
9268: /* } */
9269: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9270: /* } */
9271: /* } */
1.218 brouard 9272:
1.227 brouard 9273: /* /\******\/ */
1.218 brouard 9274:
1.227 brouard 9275: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
9276: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
9277: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
9278: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
9279: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 9280:
1.227 brouard 9281: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9282: /* oldm=oldms;savm=savms; */
9283: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
9284: /* for (h=0; h<=nhstepm; h++){ */
9285: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
9286: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
9287: /* } */
9288: /* for(j=1; j<=nlstate+ndeath;j++) { */
9289: /* kk1=0.;kk2=0; */
9290: /* for(i=1; i<=nlstate;i++) { */
9291: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
9292: /* } */
9293: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
9294: /* } */
9295: /* } */
9296: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
9297: /* } */
9298: /* } */
9299: /* } */
9300: /* } */
1.218 brouard 9301:
1.227 brouard 9302: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 9303:
1.227 brouard 9304: /* if (popforecast==1) { */
9305: /* free_ivector(popage,0,AGESUP); */
9306: /* free_vector(popeffectif,0,AGESUP); */
9307: /* free_vector(popcount,0,AGESUP); */
9308: /* } */
9309: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9310: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9311: /* fclose(ficrespop); */
9312: /* } /\* End of popforecast *\/ */
1.218 brouard 9313:
1.126 brouard 9314: int fileappend(FILE *fichier, char *optionfich)
9315: {
9316: if((fichier=fopen(optionfich,"a"))==NULL) {
9317: printf("Problem with file: %s\n", optionfich);
9318: fprintf(ficlog,"Problem with file: %s\n", optionfich);
9319: return (0);
9320: }
9321: fflush(fichier);
9322: return (1);
9323: }
9324:
9325:
9326: /**************** function prwizard **********************/
9327: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
9328: {
9329:
9330: /* Wizard to print covariance matrix template */
9331:
1.164 brouard 9332: char ca[32], cb[32];
9333: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 9334: int numlinepar;
9335:
9336: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9337: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9338: for(i=1; i <=nlstate; i++){
9339: jj=0;
9340: for(j=1; j <=nlstate+ndeath; j++){
9341: if(j==i) continue;
9342: jj++;
9343: /*ca[0]= k+'a'-1;ca[1]='\0';*/
9344: printf("%1d%1d",i,j);
9345: fprintf(ficparo,"%1d%1d",i,j);
9346: for(k=1; k<=ncovmodel;k++){
9347: /* printf(" %lf",param[i][j][k]); */
9348: /* fprintf(ficparo," %lf",param[i][j][k]); */
9349: printf(" 0.");
9350: fprintf(ficparo," 0.");
9351: }
9352: printf("\n");
9353: fprintf(ficparo,"\n");
9354: }
9355: }
9356: printf("# Scales (for hessian or gradient estimation)\n");
9357: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
9358: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
9359: for(i=1; i <=nlstate; i++){
9360: jj=0;
9361: for(j=1; j <=nlstate+ndeath; j++){
9362: if(j==i) continue;
9363: jj++;
9364: fprintf(ficparo,"%1d%1d",i,j);
9365: printf("%1d%1d",i,j);
9366: fflush(stdout);
9367: for(k=1; k<=ncovmodel;k++){
9368: /* printf(" %le",delti3[i][j][k]); */
9369: /* fprintf(ficparo," %le",delti3[i][j][k]); */
9370: printf(" 0.");
9371: fprintf(ficparo," 0.");
9372: }
9373: numlinepar++;
9374: printf("\n");
9375: fprintf(ficparo,"\n");
9376: }
9377: }
9378: printf("# Covariance matrix\n");
9379: /* # 121 Var(a12)\n\ */
9380: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9381: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9382: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9383: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9384: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9385: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9386: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9387: fflush(stdout);
9388: fprintf(ficparo,"# Covariance matrix\n");
9389: /* # 121 Var(a12)\n\ */
9390: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9391: /* # ...\n\ */
9392: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9393:
9394: for(itimes=1;itimes<=2;itimes++){
9395: jj=0;
9396: for(i=1; i <=nlstate; i++){
9397: for(j=1; j <=nlstate+ndeath; j++){
9398: if(j==i) continue;
9399: for(k=1; k<=ncovmodel;k++){
9400: jj++;
9401: ca[0]= k+'a'-1;ca[1]='\0';
9402: if(itimes==1){
9403: printf("#%1d%1d%d",i,j,k);
9404: fprintf(ficparo,"#%1d%1d%d",i,j,k);
9405: }else{
9406: printf("%1d%1d%d",i,j,k);
9407: fprintf(ficparo,"%1d%1d%d",i,j,k);
9408: /* printf(" %.5le",matcov[i][j]); */
9409: }
9410: ll=0;
9411: for(li=1;li <=nlstate; li++){
9412: for(lj=1;lj <=nlstate+ndeath; lj++){
9413: if(lj==li) continue;
9414: for(lk=1;lk<=ncovmodel;lk++){
9415: ll++;
9416: if(ll<=jj){
9417: cb[0]= lk +'a'-1;cb[1]='\0';
9418: if(ll<jj){
9419: if(itimes==1){
9420: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9421: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9422: }else{
9423: printf(" 0.");
9424: fprintf(ficparo," 0.");
9425: }
9426: }else{
9427: if(itimes==1){
9428: printf(" Var(%s%1d%1d)",ca,i,j);
9429: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
9430: }else{
9431: printf(" 0.");
9432: fprintf(ficparo," 0.");
9433: }
9434: }
9435: }
9436: } /* end lk */
9437: } /* end lj */
9438: } /* end li */
9439: printf("\n");
9440: fprintf(ficparo,"\n");
9441: numlinepar++;
9442: } /* end k*/
9443: } /*end j */
9444: } /* end i */
9445: } /* end itimes */
9446:
9447: } /* end of prwizard */
9448: /******************* Gompertz Likelihood ******************************/
9449: double gompertz(double x[])
9450: {
1.302 brouard 9451: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
1.126 brouard 9452: int i,n=0; /* n is the size of the sample */
9453:
1.220 brouard 9454: for (i=1;i<=imx ; i++) {
1.126 brouard 9455: sump=sump+weight[i];
9456: /* sump=sump+1;*/
9457: num=num+1;
9458: }
1.302 brouard 9459: L=0.0;
9460: /* agegomp=AGEGOMP; */
1.126 brouard 9461: /* for (i=0; i<=imx; i++)
9462: 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]);*/
9463:
1.302 brouard 9464: for (i=1;i<=imx ; i++) {
9465: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
9466: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
9467: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
9468: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
9469: * +
9470: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
9471: */
9472: if (wav[i] > 1 || agedc[i] < AGESUP) {
9473: if (cens[i] == 1){
9474: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
9475: } else if (cens[i] == 0){
1.126 brouard 9476: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
1.302 brouard 9477: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
9478: } else
9479: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
1.126 brouard 9480: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
1.302 brouard 9481: L=L+A*weight[i];
1.126 brouard 9482: /* 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 9483: }
9484: }
1.126 brouard 9485:
1.302 brouard 9486: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
1.126 brouard 9487:
9488: return -2*L*num/sump;
9489: }
9490:
1.136 brouard 9491: #ifdef GSL
9492: /******************* Gompertz_f Likelihood ******************************/
9493: double gompertz_f(const gsl_vector *v, void *params)
9494: {
1.302 brouard 9495: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
1.136 brouard 9496: double *x= (double *) v->data;
9497: int i,n=0; /* n is the size of the sample */
9498:
9499: for (i=0;i<=imx-1 ; i++) {
9500: sump=sump+weight[i];
9501: /* sump=sump+1;*/
9502: num=num+1;
9503: }
9504:
9505:
9506: /* for (i=0; i<=imx; i++)
9507: 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]);*/
9508: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
9509: for (i=1;i<=imx ; i++)
9510: {
9511: if (cens[i] == 1 && wav[i]>1)
9512: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
9513:
9514: if (cens[i] == 0 && wav[i]>1)
9515: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
9516: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
9517:
9518: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
9519: if (wav[i] > 1 ) { /* ??? */
9520: LL=LL+A*weight[i];
9521: /* 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]);*/
9522: }
9523: }
9524:
9525: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
9526: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
9527:
9528: return -2*LL*num/sump;
9529: }
9530: #endif
9531:
1.126 brouard 9532: /******************* Printing html file ***********/
1.201 brouard 9533: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 9534: int lastpass, int stepm, int weightopt, char model[],\
9535: int imx, double p[],double **matcov,double agemortsup){
9536: int i,k;
9537:
9538: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
9539: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
9540: for (i=1;i<=2;i++)
9541: 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 9542: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 9543: fprintf(fichtm,"</ul>");
9544:
9545: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
9546:
9547: 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>");
9548:
9549: for (k=agegomp;k<(agemortsup-2);k++)
9550: 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]);
9551:
9552:
9553: fflush(fichtm);
9554: }
9555:
9556: /******************* Gnuplot file **************/
1.201 brouard 9557: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 9558:
9559: char dirfileres[132],optfileres[132];
1.164 brouard 9560:
1.126 brouard 9561: int ng;
9562:
9563:
9564: /*#ifdef windows */
9565: fprintf(ficgp,"cd \"%s\" \n",pathc);
9566: /*#endif */
9567:
9568:
9569: strcpy(dirfileres,optionfilefiname);
9570: strcpy(optfileres,"vpl");
1.199 brouard 9571: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 9572: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 9573: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 9574: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 9575: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
9576:
9577: }
9578:
1.136 brouard 9579: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
9580: {
1.126 brouard 9581:
1.136 brouard 9582: /*-------- data file ----------*/
9583: FILE *fic;
9584: char dummy[]=" ";
1.240 brouard 9585: int i=0, j=0, n=0, iv=0, v;
1.223 brouard 9586: int lstra;
1.136 brouard 9587: int linei, month, year,iout;
1.302 brouard 9588: int noffset=0; /* This is the offset if BOM data file */
1.136 brouard 9589: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 9590: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 9591: char *stratrunc;
1.223 brouard 9592:
1.240 brouard 9593: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
9594: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
1.126 brouard 9595:
1.240 brouard 9596: for(v=1; v <=ncovcol;v++){
9597: DummyV[v]=0;
9598: FixedV[v]=0;
9599: }
9600: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
9601: DummyV[v]=1;
9602: FixedV[v]=0;
9603: }
9604: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
9605: DummyV[v]=0;
9606: FixedV[v]=1;
9607: }
9608: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
9609: DummyV[v]=1;
9610: FixedV[v]=1;
9611: }
9612: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
9613: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
9614: fprintf(ficlog,"Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
9615: }
1.126 brouard 9616:
1.136 brouard 9617: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 9618: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9619: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 9620: }
1.126 brouard 9621:
1.302 brouard 9622: /* Is it a BOM UTF-8 Windows file? */
9623: /* First data line */
9624: linei=0;
9625: while(fgets(line, MAXLINE, fic)) {
9626: noffset=0;
9627: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
9628: {
9629: noffset=noffset+3;
9630: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
9631: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
9632: fflush(ficlog); return 1;
9633: }
9634: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
9635: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
9636: {
9637: noffset=noffset+2;
1.304 brouard 9638: 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);
9639: 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 9640: fflush(ficlog); return 1;
9641: }
9642: else if( line[0] == 0 && line[1] == 0)
9643: {
9644: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
9645: noffset=noffset+4;
1.304 brouard 9646: 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);
9647: 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 9648: fflush(ficlog); return 1;
9649: }
9650: } else{
9651: ;/*printf(" Not a BOM file\n");*/
9652: }
9653: /* If line starts with a # it is a comment */
9654: if (line[noffset] == '#') {
9655: linei=linei+1;
9656: break;
9657: }else{
9658: break;
9659: }
9660: }
9661: fclose(fic);
9662: if((fic=fopen(datafile,"r"))==NULL) {
9663: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9664: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
9665: }
9666: /* Not a Bom file */
9667:
1.136 brouard 9668: i=1;
9669: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
9670: linei=linei+1;
9671: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
9672: if(line[j] == '\t')
9673: line[j] = ' ';
9674: }
9675: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
9676: ;
9677: };
9678: line[j+1]=0; /* Trims blanks at end of line */
9679: if(line[0]=='#'){
9680: fprintf(ficlog,"Comment line\n%s\n",line);
9681: printf("Comment line\n%s\n",line);
9682: continue;
9683: }
9684: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 9685: strcpy(line, linetmp);
1.223 brouard 9686:
9687: /* Loops on waves */
9688: for (j=maxwav;j>=1;j--){
9689: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 9690: cutv(stra, strb, line, ' ');
9691: if(strb[0]=='.') { /* Missing value */
9692: lval=-1;
9693: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
9694: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
9695: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
9696: 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);
9697: 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);
9698: return 1;
9699: }
9700: }else{
9701: errno=0;
9702: /* what_kind_of_number(strb); */
9703: dval=strtod(strb,&endptr);
9704: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
9705: /* if(strb != endptr && *endptr == '\0') */
9706: /* dval=dlval; */
9707: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9708: if( strb[0]=='\0' || (*endptr != '\0')){
9709: 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);
9710: 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);
9711: return 1;
9712: }
9713: cotqvar[j][iv][i]=dval;
9714: cotvar[j][ntv+iv][i]=dval;
9715: }
9716: strcpy(line,stra);
1.223 brouard 9717: }/* end loop ntqv */
1.225 brouard 9718:
1.223 brouard 9719: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 9720: cutv(stra, strb, line, ' ');
9721: if(strb[0]=='.') { /* Missing value */
9722: lval=-1;
9723: }else{
9724: errno=0;
9725: lval=strtol(strb,&endptr,10);
9726: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9727: if( strb[0]=='\0' || (*endptr != '\0')){
9728: 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);
9729: 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);
9730: return 1;
9731: }
9732: }
9733: if(lval <-1 || lval >1){
9734: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 9735: 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 9736: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9737: For example, for multinomial values like 1, 2 and 3,\n \
9738: build V1=0 V2=0 for the reference value (1),\n \
9739: V1=1 V2=0 for (2) \n \
1.223 brouard 9740: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9741: output of IMaCh is often meaningless.\n \
1.319 brouard 9742: Exiting.\n",lval,linei, i,line,iv,j);
1.238 brouard 9743: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.319 brouard 9744: 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 9745: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9746: For example, for multinomial values like 1, 2 and 3,\n \
9747: build V1=0 V2=0 for the reference value (1),\n \
9748: V1=1 V2=0 for (2) \n \
1.223 brouard 9749: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9750: output of IMaCh is often meaningless.\n \
1.319 brouard 9751: Exiting.\n",lval,linei, i,line,iv,j);fflush(ficlog);
1.238 brouard 9752: return 1;
9753: }
9754: cotvar[j][iv][i]=(double)(lval);
9755: strcpy(line,stra);
1.223 brouard 9756: }/* end loop ntv */
1.225 brouard 9757:
1.223 brouard 9758: /* Statuses at wave */
1.137 brouard 9759: cutv(stra, strb, line, ' ');
1.223 brouard 9760: if(strb[0]=='.') { /* Missing value */
1.238 brouard 9761: lval=-1;
1.136 brouard 9762: }else{
1.238 brouard 9763: errno=0;
9764: lval=strtol(strb,&endptr,10);
9765: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9766: if( strb[0]=='\0' || (*endptr != '\0')){
9767: 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);
9768: 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);
9769: return 1;
9770: }
1.136 brouard 9771: }
1.225 brouard 9772:
1.136 brouard 9773: s[j][i]=lval;
1.225 brouard 9774:
1.223 brouard 9775: /* Date of Interview */
1.136 brouard 9776: strcpy(line,stra);
9777: cutv(stra, strb,line,' ');
1.169 brouard 9778: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9779: }
1.169 brouard 9780: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 9781: month=99;
9782: year=9999;
1.136 brouard 9783: }else{
1.225 brouard 9784: 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);
9785: 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);
9786: return 1;
1.136 brouard 9787: }
9788: anint[j][i]= (double) year;
1.302 brouard 9789: mint[j][i]= (double)month;
9790: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
9791: /* 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]); */
9792: /* 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]); */
9793: /* } */
1.136 brouard 9794: strcpy(line,stra);
1.223 brouard 9795: } /* End loop on waves */
1.225 brouard 9796:
1.223 brouard 9797: /* Date of death */
1.136 brouard 9798: cutv(stra, strb,line,' ');
1.169 brouard 9799: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9800: }
1.169 brouard 9801: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 9802: month=99;
9803: year=9999;
9804: }else{
1.141 brouard 9805: 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 9806: 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);
9807: return 1;
1.136 brouard 9808: }
9809: andc[i]=(double) year;
9810: moisdc[i]=(double) month;
9811: strcpy(line,stra);
9812:
1.223 brouard 9813: /* Date of birth */
1.136 brouard 9814: cutv(stra, strb,line,' ');
1.169 brouard 9815: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9816: }
1.169 brouard 9817: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 9818: month=99;
9819: year=9999;
9820: }else{
1.141 brouard 9821: 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);
9822: 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 9823: return 1;
1.136 brouard 9824: }
9825: if (year==9999) {
1.141 brouard 9826: 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);
9827: 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 9828: return 1;
9829:
1.136 brouard 9830: }
9831: annais[i]=(double)(year);
1.302 brouard 9832: moisnais[i]=(double)(month);
9833: for (j=1;j<=maxwav;j++){
9834: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
9835: 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]);
9836: 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]);
9837: }
9838: }
9839:
1.136 brouard 9840: strcpy(line,stra);
1.225 brouard 9841:
1.223 brouard 9842: /* Sample weight */
1.136 brouard 9843: cutv(stra, strb,line,' ');
9844: errno=0;
9845: dval=strtod(strb,&endptr);
9846: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 9847: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
9848: 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 9849: fflush(ficlog);
9850: return 1;
9851: }
9852: weight[i]=dval;
9853: strcpy(line,stra);
1.225 brouard 9854:
1.223 brouard 9855: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
9856: cutv(stra, strb, line, ' ');
9857: if(strb[0]=='.') { /* Missing value */
1.225 brouard 9858: lval=-1;
1.311 brouard 9859: coqvar[iv][i]=NAN;
9860: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
1.223 brouard 9861: }else{
1.225 brouard 9862: errno=0;
9863: /* what_kind_of_number(strb); */
9864: dval=strtod(strb,&endptr);
9865: /* if(strb != endptr && *endptr == '\0') */
9866: /* dval=dlval; */
9867: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9868: if( strb[0]=='\0' || (*endptr != '\0')){
9869: 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);
9870: 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);
9871: return 1;
9872: }
9873: coqvar[iv][i]=dval;
1.226 brouard 9874: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 9875: }
9876: strcpy(line,stra);
9877: }/* end loop nqv */
1.136 brouard 9878:
1.223 brouard 9879: /* Covariate values */
1.136 brouard 9880: for (j=ncovcol;j>=1;j--){
9881: cutv(stra, strb,line,' ');
1.223 brouard 9882: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 9883: lval=-1;
1.136 brouard 9884: }else{
1.225 brouard 9885: errno=0;
9886: lval=strtol(strb,&endptr,10);
9887: if( strb[0]=='\0' || (*endptr != '\0')){
9888: 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);
9889: 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);
9890: return 1;
9891: }
1.136 brouard 9892: }
9893: if(lval <-1 || lval >1){
1.225 brouard 9894: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9895: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9896: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9897: For example, for multinomial values like 1, 2 and 3,\n \
9898: build V1=0 V2=0 for the reference value (1),\n \
9899: V1=1 V2=0 for (2) \n \
1.136 brouard 9900: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9901: output of IMaCh is often meaningless.\n \
1.136 brouard 9902: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 9903: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9904: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9905: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9906: For example, for multinomial values like 1, 2 and 3,\n \
9907: build V1=0 V2=0 for the reference value (1),\n \
9908: V1=1 V2=0 for (2) \n \
1.136 brouard 9909: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9910: output of IMaCh is often meaningless.\n \
1.136 brouard 9911: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 9912: return 1;
1.136 brouard 9913: }
9914: covar[j][i]=(double)(lval);
9915: strcpy(line,stra);
9916: }
9917: lstra=strlen(stra);
1.225 brouard 9918:
1.136 brouard 9919: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
9920: stratrunc = &(stra[lstra-9]);
9921: num[i]=atol(stratrunc);
9922: }
9923: else
9924: num[i]=atol(stra);
9925: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
9926: 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;}*/
9927:
9928: i=i+1;
9929: } /* End loop reading data */
1.225 brouard 9930:
1.136 brouard 9931: *imax=i-1; /* Number of individuals */
9932: fclose(fic);
1.225 brouard 9933:
1.136 brouard 9934: return (0);
1.164 brouard 9935: /* endread: */
1.225 brouard 9936: printf("Exiting readdata: ");
9937: fclose(fic);
9938: return (1);
1.223 brouard 9939: }
1.126 brouard 9940:
1.234 brouard 9941: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 9942: char *p1 = *stri, *p2 = *stri;
1.235 brouard 9943: while (*p2 == ' ')
1.234 brouard 9944: p2++;
9945: /* while ((*p1++ = *p2++) !=0) */
9946: /* ; */
9947: /* do */
9948: /* while (*p2 == ' ') */
9949: /* p2++; */
9950: /* while (*p1++ == *p2++); */
9951: *stri=p2;
1.145 brouard 9952: }
9953:
1.235 brouard 9954: int decoderesult ( char resultline[], int nres)
1.230 brouard 9955: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
9956: {
1.235 brouard 9957: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 9958: char resultsav[MAXLINE];
1.234 brouard 9959: int resultmodel[MAXLINE];
9960: int modelresult[MAXLINE];
1.230 brouard 9961: char stra[80], strb[80], strc[80], strd[80],stre[80];
9962:
1.234 brouard 9963: removefirstspace(&resultline);
1.230 brouard 9964:
9965: if (strstr(resultline,"v") !=0){
9966: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
9967: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
9968: return 1;
9969: }
9970: trimbb(resultsav, resultline);
9971: if (strlen(resultsav) >1){
9972: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
9973: }
1.253 brouard 9974: if(j == 0){ /* Resultline but no = */
9975: TKresult[nres]=0; /* Combination for the nresult and the model */
9976: return (0);
9977: }
1.234 brouard 9978: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
1.318 brouard 9979: printf("ERROR: the number of variables in this result line, %d, differs from the number of variables used in the model line, %d.\n",j, cptcovs);
1.310 brouard 9980: fprintf(ficlog,"ERROR: the number of variables in the resultline, %d, differs from the number of variables used in the model line, %d.\n",j, cptcovs);
1.234 brouard 9981: }
9982: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
9983: if(nbocc(resultsav,'=') >1){
1.318 brouard 9984: 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" */
9985: cutl(strc,strd,strb,'='); /* strb:"V4=1" strc="1" strd="V4" */
1.234 brouard 9986: }else
9987: cutl(strc,strd,resultsav,'=');
1.318 brouard 9988: Tvalsel[k]=atof(strc); /* 1 */ /* Tvalsel of k is the float value of the kth covariate appearing in this result line */
1.234 brouard 9989:
1.230 brouard 9990: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
1.318 brouard 9991: 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 9992: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
9993: /* cptcovsel++; */
9994: if (nbocc(stra,'=') >0)
9995: strcpy(resultsav,stra); /* and analyzes it */
9996: }
1.235 brouard 9997: /* Checking for missing or useless values in comparison of current model needs */
1.318 brouard 9998: for(k1=1; k1<= cptcovt ;k1++){ /* Loop on model. model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9999: if(Typevar[k1]==0){ /* Single covariate in model *//*0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.234 brouard 10000: match=0;
1.318 brouard 10001: for(k2=1; k2 <=j;k2++){/* Loop on resultline. In result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10002: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar is coming from the model, Tvarsel from the result. Tvar[1]=5 == Tvarsel[2]=5 */
1.236 brouard 10003: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.318 brouard 10004: match=1; /* modelresult of k2 variable of resultline is identical to k1 variable of the model good */
1.234 brouard 10005: break;
10006: }
10007: }
10008: if(match == 0){
1.310 brouard 10009: printf("Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model);
10010: fprintf(ficlog,"Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model);
10011: return 1;
1.234 brouard 10012: }
10013: }
10014: }
1.235 brouard 10015: /* Checking for missing or useless values in comparison of current model needs */
1.318 brouard 10016: for(k2=1; k2 <=j;k2++){ /* Loop on resultline variables: result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 10017: match=0;
1.318 brouard 10018: 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.235 brouard 10019: if(Typevar[k1]==0){ /* Single */
1.237 brouard 10020: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
1.318 brouard 10021: resultmodel[k1]=k2; /* k2th variable of the model corresponds to k1 variable of the model. resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.234 brouard 10022: ++match;
10023: }
10024: }
10025: }
10026: if(match == 0){
10027: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
1.310 brouard 10028: fprintf(ficlog,"Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
10029: return 1;
1.234 brouard 10030: }else if(match > 1){
10031: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
1.310 brouard 10032: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
10033: return 1;
1.234 brouard 10034: }
10035: }
1.235 brouard 10036:
1.234 brouard 10037: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 10038: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10039: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */
10040: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/
10041: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
10042: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
10043: /* 1 0 0 0 */
10044: /* 2 1 0 0 */
10045: /* 3 0 1 0 */
10046: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */
10047: /* 5 0 0 1 */
10048: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */
10049: /* 7 0 1 1 */
10050: /* 8 1 1 1 */
1.237 brouard 10051: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
10052: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
10053: /* V5*age V5 known which value for nres? */
10054: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.318 brouard 10055: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* loop on model line */
1.235 brouard 10056: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */
1.237 brouard 10057: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */
1.235 brouard 10058: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
10059: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */
1.237 brouard 10060: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */
10061: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
10062: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.235 brouard 10063: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4);
10064: k4++;;
10065: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */
1.318 brouard 10066: k3q= resultmodel[k1]; /* resultmodel[1(V5)] = 25.1=k3q */
10067: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[1]]= Tvarsel[1] = 4=k2 */
1.237 brouard 10068: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
10069: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
10070: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.235 brouard 10071: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
10072: k4q++;;
10073: }
10074: }
1.234 brouard 10075:
1.235 brouard 10076: TKresult[nres]=++k; /* Combination for the nresult and the model */
1.230 brouard 10077: return (0);
10078: }
1.235 brouard 10079:
1.230 brouard 10080: int decodemodel( char model[], int lastobs)
10081: /**< This routine decodes the model and returns:
1.224 brouard 10082: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
10083: * - nagesqr = 1 if age*age in the model, otherwise 0.
10084: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
10085: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
10086: * - cptcovage number of covariates with age*products =2
10087: * - cptcovs number of simple covariates
10088: * - 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
10089: * which is a new column after the 9 (ncovcol) variables.
1.319 brouard 10090: * - if k is a product Vn*Vm, covar[k][i] is filled with correct values for each individual
1.224 brouard 10091: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
10092: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
10093: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
10094: */
1.319 brouard 10095: /* 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 10096: {
1.238 brouard 10097: int i, j, k, ks, v;
1.227 brouard 10098: int j1, k1, k2, k3, k4;
1.136 brouard 10099: char modelsav[80];
1.145 brouard 10100: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 10101: char *strpt;
1.136 brouard 10102:
1.145 brouard 10103: /*removespace(model);*/
1.136 brouard 10104: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 10105: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 10106: if (strstr(model,"AGE") !=0){
1.192 brouard 10107: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
10108: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 10109: return 1;
10110: }
1.141 brouard 10111: if (strstr(model,"v") !=0){
10112: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
10113: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
10114: return 1;
10115: }
1.187 brouard 10116: strcpy(modelsav,model);
10117: if ((strpt=strstr(model,"age*age")) !=0){
10118: printf(" strpt=%s, model=%s\n",strpt, model);
10119: if(strpt != model){
1.234 brouard 10120: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 10121: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 10122: corresponding column of parameters.\n",model);
1.234 brouard 10123: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 10124: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 10125: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 10126: return 1;
1.225 brouard 10127: }
1.187 brouard 10128: nagesqr=1;
10129: if (strstr(model,"+age*age") !=0)
1.234 brouard 10130: substrchaine(modelsav, model, "+age*age");
1.187 brouard 10131: else if (strstr(model,"age*age+") !=0)
1.234 brouard 10132: substrchaine(modelsav, model, "age*age+");
1.187 brouard 10133: else
1.234 brouard 10134: substrchaine(modelsav, model, "age*age");
1.187 brouard 10135: }else
10136: nagesqr=0;
10137: if (strlen(modelsav) >1){
10138: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
10139: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 10140: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 10141: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 10142: * cst, age and age*age
10143: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
10144: /* including age products which are counted in cptcovage.
10145: * but the covariates which are products must be treated
10146: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 10147: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
10148: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 10149:
10150:
1.187 brouard 10151: /* Design
10152: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
10153: * < ncovcol=8 >
10154: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
10155: * k= 1 2 3 4 5 6 7 8
10156: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
10157: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 10158: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
10159: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 10160: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
10161: * Tage[++cptcovage]=k
10162: * if products, new covar are created after ncovcol with k1
10163: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
10164: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
10165: * 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
10166: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
10167: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
10168: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
10169: * < ncovcol=8 >
10170: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
10171: * k= 1 2 3 4 5 6 7 8 9 10 11 12
10172: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
1.319 brouard 10173: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
1.187 brouard 10174: * p Tprod[1]@2={ 6, 5}
10175: *p Tvard[1][1]@4= {7, 8, 5, 6}
10176: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
10177: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
1.319 brouard 10178: *How to reorganize? Tvars(orted)
1.187 brouard 10179: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
10180: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
10181: * {2, 1, 4, 8, 5, 6, 3, 7}
10182: * Struct []
10183: */
1.225 brouard 10184:
1.187 brouard 10185: /* This loop fills the array Tvar from the string 'model'.*/
10186: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
10187: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
10188: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
10189: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
10190: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
10191: /* k=1 Tvar[1]=2 (from V2) */
10192: /* k=5 Tvar[5] */
10193: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 10194: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 10195: /* } */
1.198 brouard 10196: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 10197: /*
10198: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 10199: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
10200: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
10201: }
1.187 brouard 10202: cptcovage=0;
1.319 brouard 10203: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model line */
10204: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' cutl from left to right
10205: 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" */
10206: if (nbocc(modelsav,'+')==0)
10207: strcpy(strb,modelsav); /* and analyzes it */
1.234 brouard 10208: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
10209: /*scanf("%d",i);*/
1.319 brouard 10210: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V5*age+ V4+V3*age strb=V3*age */
10211: cutl(strc,strd,strb,'*'); /**< k=1 strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
1.234 brouard 10212: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
10213: /* covar is not filled and then is empty */
10214: cptcovprod--;
10215: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
1.319 brouard 10216: 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 */
1.234 brouard 10217: Typevar[k]=1; /* 1 for age product */
1.319 brouard 10218: cptcovage++; /* Counts the number of covariates which include age as a product */
10219: Tage[cptcovage]=k; /* V2+V1+V4+V3*age Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
1.234 brouard 10220: /*printf("stre=%s ", stre);*/
10221: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
10222: cptcovprod--;
10223: cutl(stre,strb,strc,'V');
10224: Tvar[k]=atoi(stre);
10225: Typevar[k]=1; /* 1 for age product */
10226: cptcovage++;
10227: Tage[cptcovage]=k;
10228: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
10229: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
10230: cptcovn++;
10231: cptcovprodnoage++;k1++;
10232: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
10233: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
10234: because this model-covariate is a construction we invent a new column
10235: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
1.319 brouard 10236: If already ncovcol=4 and model=V2 + V1 +V1*V4 +age*V3 +V3*V2
10237: thus after V4 we invent V5 and V6 because age*V3 will be computed in 4
10238: Tvar[3=V1*V4]=4+1=5 Tvar[5=V3*V2]=4 + 2= 6, Tvar[4=age*V3]=4 etc */
1.234 brouard 10239: Typevar[k]=2; /* 2 for double fixed dummy covariates */
10240: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
10241: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
1.319 brouard 10242: Tposprod[k]=k1; /* Tposprod[3]=1, Tposprod[2]=5 */
1.234 brouard 10243: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
10244: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
10245: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
10246: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
10247: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 10248: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 brouard 10249: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
10250: for (i=1; i<=lastobs;i++){
10251: /* Computes the new covariate which is a product of
10252: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
10253: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
10254: }
10255: } /* End age is not in the model */
10256: } /* End if model includes a product */
1.319 brouard 10257: else { /* not a product */
1.234 brouard 10258: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
10259: /* scanf("%d",i);*/
10260: cutl(strd,strc,strb,'V');
10261: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
10262: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
10263: Tvar[k]=atoi(strd);
10264: Typevar[k]=0; /* 0 for simple covariates */
10265: }
10266: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 10267: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 10268: scanf("%d",i);*/
1.187 brouard 10269: } /* end of loop + on total covariates */
10270: } /* end if strlen(modelsave == 0) age*age might exist */
10271: } /* end if strlen(model == 0) */
1.136 brouard 10272:
10273: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
10274: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 10275:
1.136 brouard 10276: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 10277: printf("cptcovprod=%d ", cptcovprod);
10278: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
10279: scanf("%d ",i);*/
10280:
10281:
1.230 brouard 10282: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
10283: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 10284: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
10285: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
10286: k = 1 2 3 4 5 6 7 8 9
10287: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
1.319 brouard 10288: Typevar[k]= 0 0 0 2 1 0 2 1 0
1.227 brouard 10289: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
10290: Dummy[k] 1 0 0 0 3 1 1 2 3
10291: Tmodelind[combination of covar]=k;
1.225 brouard 10292: */
10293: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 10294: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 10295: /* 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 10296: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.318 brouard 10297: printf("Model=1+age+%s\n\
1.227 brouard 10298: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
10299: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
10300: 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 10301: fprintf(ficlog,"Model=1+age+%s\n\
1.227 brouard 10302: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
10303: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
10304: 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.285 brouard 10305: for(k=-1;k<=cptcovt; k++){ Fixed[k]=0; Dummy[k]=0;}
1.234 brouard 10306: for(k=1, ncovf=0, nsd=0, nsq=0, ncovv=0, ncova=0, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0;k<=cptcovt; k++){ /* or cptocvt */
10307: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 10308: Fixed[k]= 0;
10309: Dummy[k]= 0;
1.225 brouard 10310: ncoveff++;
1.232 brouard 10311: ncovf++;
1.234 brouard 10312: nsd++;
10313: modell[k].maintype= FTYPE;
10314: TvarsD[nsd]=Tvar[k];
10315: TvarsDind[nsd]=k;
10316: TvarF[ncovf]=Tvar[k];
10317: TvarFind[ncovf]=k;
10318: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10319: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10320: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
10321: Fixed[k]= 0;
10322: Dummy[k]= 0;
10323: ncoveff++;
10324: ncovf++;
10325: modell[k].maintype= FTYPE;
10326: TvarF[ncovf]=Tvar[k];
10327: TvarFind[ncovf]=k;
1.230 brouard 10328: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 10329: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.240 brouard 10330: }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 10331: Fixed[k]= 0;
10332: Dummy[k]= 1;
1.230 brouard 10333: nqfveff++;
1.234 brouard 10334: modell[k].maintype= FTYPE;
10335: modell[k].subtype= FQ;
10336: nsq++;
10337: TvarsQ[nsq]=Tvar[k];
10338: TvarsQind[nsq]=k;
1.232 brouard 10339: ncovf++;
1.234 brouard 10340: TvarF[ncovf]=Tvar[k];
10341: TvarFind[ncovf]=k;
1.231 brouard 10342: 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 10343: 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 10344: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.227 brouard 10345: Fixed[k]= 1;
10346: Dummy[k]= 0;
1.225 brouard 10347: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 10348: modell[k].maintype= VTYPE;
10349: modell[k].subtype= VD;
10350: nsd++;
10351: TvarsD[nsd]=Tvar[k];
10352: TvarsDind[nsd]=k;
10353: ncovv++; /* Only simple time varying variables */
10354: TvarV[ncovv]=Tvar[k];
1.242 brouard 10355: 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 10356: 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 */
10357: 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 10358: 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);
10359: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 10360: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 brouard 10361: Fixed[k]= 1;
10362: Dummy[k]= 1;
10363: nqtveff++;
10364: modell[k].maintype= VTYPE;
10365: modell[k].subtype= VQ;
10366: ncovv++; /* Only simple time varying variables */
10367: nsq++;
1.319 brouard 10368: TvarsQ[nsq]=Tvar[k]; /* k=1 Tvar=5 nsq=1 TvarsQ[1]=5 */
1.234 brouard 10369: TvarsQind[nsq]=k;
10370: TvarV[ncovv]=Tvar[k];
1.242 brouard 10371: 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 10372: 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 */
10373: 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 10374: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
10375: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
10376: printf("Quasi TmodelQind[%d]=%d,Tvar[TmodelQind[%d]]=V%d, ncovcol=%d, nqv=%d, ntv=%d,Tvar[k]- ncovcol-nqv-ntv=%d\n",nqtveff,k,nqtveff,Tvar[k], ncovcol, nqv, ntv, Tvar[k]- ncovcol-nqv-ntv);
1.228 brouard 10377: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 10378: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 10379: ncova++;
10380: TvarA[ncova]=Tvar[k];
10381: TvarAind[ncova]=k;
1.231 brouard 10382: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 10383: Fixed[k]= 2;
10384: Dummy[k]= 2;
10385: modell[k].maintype= ATYPE;
10386: modell[k].subtype= APFD;
10387: /* ncoveff++; */
1.227 brouard 10388: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 10389: Fixed[k]= 2;
10390: Dummy[k]= 3;
10391: modell[k].maintype= ATYPE;
10392: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
10393: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 10394: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 10395: Fixed[k]= 3;
10396: Dummy[k]= 2;
10397: modell[k].maintype= ATYPE;
10398: modell[k].subtype= APVD; /* Product age * varying dummy */
10399: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 10400: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 10401: Fixed[k]= 3;
10402: Dummy[k]= 3;
10403: modell[k].maintype= ATYPE;
10404: modell[k].subtype= APVQ; /* Product age * varying quantitative */
10405: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 10406: }
10407: }else if (Typevar[k] == 2) { /* product without age */
10408: k1=Tposprod[k];
10409: if(Tvard[k1][1] <=ncovcol){
1.240 brouard 10410: if(Tvard[k1][2] <=ncovcol){
10411: Fixed[k]= 1;
10412: Dummy[k]= 0;
10413: modell[k].maintype= FTYPE;
10414: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
10415: ncovf++; /* Fixed variables without age */
10416: TvarF[ncovf]=Tvar[k];
10417: TvarFind[ncovf]=k;
10418: }else if(Tvard[k1][2] <=ncovcol+nqv){
10419: Fixed[k]= 0; /* or 2 ?*/
10420: Dummy[k]= 1;
10421: modell[k].maintype= FTYPE;
10422: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
10423: ncovf++; /* Varying variables without age */
10424: TvarF[ncovf]=Tvar[k];
10425: TvarFind[ncovf]=k;
10426: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10427: Fixed[k]= 1;
10428: Dummy[k]= 0;
10429: modell[k].maintype= VTYPE;
10430: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
10431: ncovv++; /* Varying variables without age */
10432: TvarV[ncovv]=Tvar[k];
10433: TvarVind[ncovv]=k;
10434: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10435: Fixed[k]= 1;
10436: Dummy[k]= 1;
10437: modell[k].maintype= VTYPE;
10438: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
10439: ncovv++; /* Varying variables without age */
10440: TvarV[ncovv]=Tvar[k];
10441: TvarVind[ncovv]=k;
10442: }
1.227 brouard 10443: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.240 brouard 10444: if(Tvard[k1][2] <=ncovcol){
10445: Fixed[k]= 0; /* or 2 ?*/
10446: Dummy[k]= 1;
10447: modell[k].maintype= FTYPE;
10448: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
10449: ncovf++; /* Fixed variables without age */
10450: TvarF[ncovf]=Tvar[k];
10451: TvarFind[ncovf]=k;
10452: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10453: Fixed[k]= 1;
10454: Dummy[k]= 1;
10455: modell[k].maintype= VTYPE;
10456: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
10457: ncovv++; /* Varying variables without age */
10458: TvarV[ncovv]=Tvar[k];
10459: TvarVind[ncovv]=k;
10460: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10461: Fixed[k]= 1;
10462: Dummy[k]= 1;
10463: modell[k].maintype= VTYPE;
10464: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
10465: ncovv++; /* Varying variables without age */
10466: TvarV[ncovv]=Tvar[k];
10467: TvarVind[ncovv]=k;
10468: ncovv++; /* Varying variables without age */
10469: TvarV[ncovv]=Tvar[k];
10470: TvarVind[ncovv]=k;
10471: }
1.227 brouard 10472: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.240 brouard 10473: if(Tvard[k1][2] <=ncovcol){
10474: Fixed[k]= 1;
10475: Dummy[k]= 1;
10476: modell[k].maintype= VTYPE;
10477: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
10478: ncovv++; /* Varying variables without age */
10479: TvarV[ncovv]=Tvar[k];
10480: TvarVind[ncovv]=k;
10481: }else if(Tvard[k1][2] <=ncovcol+nqv){
10482: Fixed[k]= 1;
10483: Dummy[k]= 1;
10484: modell[k].maintype= VTYPE;
10485: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
10486: ncovv++; /* Varying variables without age */
10487: TvarV[ncovv]=Tvar[k];
10488: TvarVind[ncovv]=k;
10489: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10490: Fixed[k]= 1;
10491: Dummy[k]= 0;
10492: modell[k].maintype= VTYPE;
10493: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
10494: ncovv++; /* Varying variables without age */
10495: TvarV[ncovv]=Tvar[k];
10496: TvarVind[ncovv]=k;
10497: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10498: Fixed[k]= 1;
10499: Dummy[k]= 1;
10500: modell[k].maintype= VTYPE;
10501: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
10502: ncovv++; /* Varying variables without age */
10503: TvarV[ncovv]=Tvar[k];
10504: TvarVind[ncovv]=k;
10505: }
1.227 brouard 10506: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 10507: if(Tvard[k1][2] <=ncovcol){
10508: Fixed[k]= 1;
10509: Dummy[k]= 1;
10510: modell[k].maintype= VTYPE;
10511: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
10512: ncovv++; /* Varying variables without age */
10513: TvarV[ncovv]=Tvar[k];
10514: TvarVind[ncovv]=k;
10515: }else if(Tvard[k1][2] <=ncovcol+nqv){
10516: Fixed[k]= 1;
10517: Dummy[k]= 1;
10518: modell[k].maintype= VTYPE;
10519: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
10520: ncovv++; /* Varying variables without age */
10521: TvarV[ncovv]=Tvar[k];
10522: TvarVind[ncovv]=k;
10523: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10524: Fixed[k]= 1;
10525: Dummy[k]= 1;
10526: modell[k].maintype= VTYPE;
10527: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
10528: ncovv++; /* Varying variables without age */
10529: TvarV[ncovv]=Tvar[k];
10530: TvarVind[ncovv]=k;
10531: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10532: Fixed[k]= 1;
10533: Dummy[k]= 1;
10534: modell[k].maintype= VTYPE;
10535: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
10536: ncovv++; /* Varying variables without age */
10537: TvarV[ncovv]=Tvar[k];
10538: TvarVind[ncovv]=k;
10539: }
1.227 brouard 10540: }else{
1.240 brouard 10541: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10542: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10543: } /*end k1*/
1.225 brouard 10544: }else{
1.226 brouard 10545: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
10546: 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 10547: }
1.227 brouard 10548: 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]);
1.231 brouard 10549: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 10550: 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]);
10551: }
10552: /* Searching for doublons in the model */
10553: for(k1=1; k1<= cptcovt;k1++){
10554: for(k2=1; k2 <k1;k2++){
1.285 brouard 10555: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
10556: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
1.234 brouard 10557: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
10558: if(Tvar[k1]==Tvar[k2]){
1.285 brouard 10559: printf("Error duplication in the model=%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]);
10560: fprintf(ficlog,"Error duplication in the model=%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 10561: return(1);
10562: }
10563: }else if (Typevar[k1] ==2){
10564: k3=Tposprod[k1];
10565: k4=Tposprod[k2];
10566: 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])) ){
10567: printf("Error duplication in the model=%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]]);
10568: fprintf(ficlog,"Error duplication in the model=%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);
10569: return(1);
10570: }
10571: }
1.227 brouard 10572: }
10573: }
1.225 brouard 10574: }
10575: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
10576: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 10577: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
10578: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 10579: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 10580: /*endread:*/
1.225 brouard 10581: printf("Exiting decodemodel: ");
10582: return (1);
1.136 brouard 10583: }
10584:
1.169 brouard 10585: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.248 brouard 10586: {/* Check ages at death */
1.136 brouard 10587: int i, m;
1.218 brouard 10588: int firstone=0;
10589:
1.136 brouard 10590: for (i=1; i<=imx; i++) {
10591: for(m=2; (m<= maxwav); m++) {
10592: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
10593: anint[m][i]=9999;
1.216 brouard 10594: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
10595: s[m][i]=-1;
1.136 brouard 10596: }
10597: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.260 brouard 10598: *nberr = *nberr + 1;
1.218 brouard 10599: if(firstone == 0){
10600: firstone=1;
1.260 brouard 10601: 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 10602: }
1.262 brouard 10603: 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 10604: s[m][i]=-1; /* Droping the death status */
1.136 brouard 10605: }
10606: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 10607: (*nberr)++;
1.259 brouard 10608: 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 10609: 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 10610: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.136 brouard 10611: }
10612: }
10613: }
10614:
10615: for (i=1; i<=imx; i++) {
10616: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
10617: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 10618: 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 10619: if (s[m][i] >= nlstate+1) {
1.169 brouard 10620: if(agedc[i]>0){
10621: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 10622: agev[m][i]=agedc[i];
1.214 brouard 10623: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 10624: }else {
1.136 brouard 10625: if ((int)andc[i]!=9999){
10626: nbwarn++;
10627: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
10628: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
10629: agev[m][i]=-1;
10630: }
10631: }
1.169 brouard 10632: } /* agedc > 0 */
1.214 brouard 10633: } /* end if */
1.136 brouard 10634: else if(s[m][i] !=9){ /* Standard case, age in fractional
10635: years but with the precision of a month */
10636: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
10637: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
10638: agev[m][i]=1;
10639: else if(agev[m][i] < *agemin){
10640: *agemin=agev[m][i];
10641: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
10642: }
10643: else if(agev[m][i] >*agemax){
10644: *agemax=agev[m][i];
1.156 brouard 10645: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 10646: }
10647: /*agev[m][i]=anint[m][i]-annais[i];*/
10648: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 10649: } /* en if 9*/
1.136 brouard 10650: else { /* =9 */
1.214 brouard 10651: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 10652: agev[m][i]=1;
10653: s[m][i]=-1;
10654: }
10655: }
1.214 brouard 10656: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 10657: agev[m][i]=1;
1.214 brouard 10658: else{
10659: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10660: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10661: agev[m][i]=0;
10662: }
10663: } /* End for lastpass */
10664: }
1.136 brouard 10665:
10666: for (i=1; i<=imx; i++) {
10667: for(m=firstpass; (m<=lastpass); m++){
10668: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 10669: (*nberr)++;
1.136 brouard 10670: 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);
10671: 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);
10672: return 1;
10673: }
10674: }
10675: }
10676:
10677: /*for (i=1; i<=imx; i++){
10678: for (m=firstpass; (m<lastpass); m++){
10679: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
10680: }
10681:
10682: }*/
10683:
10684:
1.139 brouard 10685: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
10686: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 10687:
10688: return (0);
1.164 brouard 10689: /* endread:*/
1.136 brouard 10690: printf("Exiting calandcheckages: ");
10691: return (1);
10692: }
10693:
1.172 brouard 10694: #if defined(_MSC_VER)
10695: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10696: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10697: //#include "stdafx.h"
10698: //#include <stdio.h>
10699: //#include <tchar.h>
10700: //#include <windows.h>
10701: //#include <iostream>
10702: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
10703:
10704: LPFN_ISWOW64PROCESS fnIsWow64Process;
10705:
10706: BOOL IsWow64()
10707: {
10708: BOOL bIsWow64 = FALSE;
10709:
10710: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
10711: // (HANDLE, PBOOL);
10712:
10713: //LPFN_ISWOW64PROCESS fnIsWow64Process;
10714:
10715: HMODULE module = GetModuleHandle(_T("kernel32"));
10716: const char funcName[] = "IsWow64Process";
10717: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
10718: GetProcAddress(module, funcName);
10719:
10720: if (NULL != fnIsWow64Process)
10721: {
10722: if (!fnIsWow64Process(GetCurrentProcess(),
10723: &bIsWow64))
10724: //throw std::exception("Unknown error");
10725: printf("Unknown error\n");
10726: }
10727: return bIsWow64 != FALSE;
10728: }
10729: #endif
1.177 brouard 10730:
1.191 brouard 10731: void syscompilerinfo(int logged)
1.292 brouard 10732: {
10733: #include <stdint.h>
10734:
10735: /* #include "syscompilerinfo.h"*/
1.185 brouard 10736: /* command line Intel compiler 32bit windows, XP compatible:*/
10737: /* /GS /W3 /Gy
10738: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
10739: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
10740: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 10741: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
10742: */
10743: /* 64 bits */
1.185 brouard 10744: /*
10745: /GS /W3 /Gy
10746: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
10747: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
10748: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
10749: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
10750: /* Optimization are useless and O3 is slower than O2 */
10751: /*
10752: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
10753: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
10754: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
10755: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
10756: */
1.186 brouard 10757: /* Link is */ /* /OUT:"visual studio
1.185 brouard 10758: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
10759: /PDB:"visual studio
10760: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
10761: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
10762: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
10763: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
10764: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
10765: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
10766: uiAccess='false'"
10767: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
10768: /NOLOGO /TLBID:1
10769: */
1.292 brouard 10770:
10771:
1.177 brouard 10772: #if defined __INTEL_COMPILER
1.178 brouard 10773: #if defined(__GNUC__)
10774: struct utsname sysInfo; /* For Intel on Linux and OS/X */
10775: #endif
1.177 brouard 10776: #elif defined(__GNUC__)
1.179 brouard 10777: #ifndef __APPLE__
1.174 brouard 10778: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 10779: #endif
1.177 brouard 10780: struct utsname sysInfo;
1.178 brouard 10781: int cross = CROSS;
10782: if (cross){
10783: printf("Cross-");
1.191 brouard 10784: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 10785: }
1.174 brouard 10786: #endif
10787:
1.191 brouard 10788: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 10789: #if defined(__clang__)
1.191 brouard 10790: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 10791: #endif
10792: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 10793: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 10794: #endif
10795: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 10796: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 10797: #endif
10798: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 10799: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 10800: #endif
10801: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 10802: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 10803: #endif
10804: #if defined(_MSC_VER)
1.191 brouard 10805: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 10806: #endif
10807: #if defined(__PGI)
1.191 brouard 10808: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 10809: #endif
10810: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 10811: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 10812: #endif
1.191 brouard 10813: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 10814:
1.167 brouard 10815: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
10816: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
10817: // Windows (x64 and x86)
1.191 brouard 10818: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 10819: #elif __unix__ // all unices, not all compilers
10820: // Unix
1.191 brouard 10821: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 10822: #elif __linux__
10823: // linux
1.191 brouard 10824: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 10825: #elif __APPLE__
1.174 brouard 10826: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 10827: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 10828: #endif
10829:
10830: /* __MINGW32__ */
10831: /* __CYGWIN__ */
10832: /* __MINGW64__ */
10833: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
10834: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
10835: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
10836: /* _WIN64 // Defined for applications for Win64. */
10837: /* _M_X64 // Defined for compilations that target x64 processors. */
10838: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 10839:
1.167 brouard 10840: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 10841: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 10842: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 10843: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 10844: #else
1.191 brouard 10845: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 10846: #endif
10847:
1.169 brouard 10848: #if defined(__GNUC__)
10849: # if defined(__GNUC_PATCHLEVEL__)
10850: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10851: + __GNUC_MINOR__ * 100 \
10852: + __GNUC_PATCHLEVEL__)
10853: # else
10854: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10855: + __GNUC_MINOR__ * 100)
10856: # endif
1.174 brouard 10857: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 10858: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 10859:
10860: if (uname(&sysInfo) != -1) {
10861: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 10862: 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 10863: }
10864: else
10865: perror("uname() error");
1.179 brouard 10866: //#ifndef __INTEL_COMPILER
10867: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 10868: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 10869: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 10870: #endif
1.169 brouard 10871: #endif
1.172 brouard 10872:
1.286 brouard 10873: // void main ()
1.172 brouard 10874: // {
1.169 brouard 10875: #if defined(_MSC_VER)
1.174 brouard 10876: if (IsWow64()){
1.191 brouard 10877: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
10878: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 10879: }
10880: else{
1.191 brouard 10881: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
10882: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 10883: }
1.172 brouard 10884: // printf("\nPress Enter to continue...");
10885: // getchar();
10886: // }
10887:
1.169 brouard 10888: #endif
10889:
1.167 brouard 10890:
1.219 brouard 10891: }
1.136 brouard 10892:
1.219 brouard 10893: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.288 brouard 10894: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
1.235 brouard 10895: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 10896: /* double ftolpl = 1.e-10; */
1.180 brouard 10897: double age, agebase, agelim;
1.203 brouard 10898: double tot;
1.180 brouard 10899:
1.202 brouard 10900: strcpy(filerespl,"PL_");
10901: strcat(filerespl,fileresu);
10902: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.288 brouard 10903: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
10904: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
1.202 brouard 10905: }
1.288 brouard 10906: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
10907: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 10908: pstamp(ficrespl);
1.288 brouard 10909: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 10910: fprintf(ficrespl,"#Age ");
10911: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
10912: fprintf(ficrespl,"\n");
1.180 brouard 10913:
1.219 brouard 10914: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 10915:
1.219 brouard 10916: agebase=ageminpar;
10917: agelim=agemaxpar;
1.180 brouard 10918:
1.227 brouard 10919: /* i1=pow(2,ncoveff); */
1.234 brouard 10920: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 10921: if (cptcovn < 1){i1=1;}
1.180 brouard 10922:
1.238 brouard 10923: for(k=1; k<=i1;k++){ /* For each combination k of dummy covariates in the model */
10924: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 10925: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10926: continue;
1.235 brouard 10927:
1.238 brouard 10928: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10929: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
10930: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
10931: /* k=k+1; */
10932: /* to clean */
10933: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
10934: fprintf(ficrespl,"#******");
10935: printf("#******");
10936: fprintf(ficlog,"#******");
10937: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
10938: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
10939: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10940: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10941: }
10942: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
10943: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10944: fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10945: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10946: }
10947: fprintf(ficrespl,"******\n");
10948: printf("******\n");
10949: fprintf(ficlog,"******\n");
10950: if(invalidvarcomb[k]){
10951: printf("\nCombination (%d) ignored because no case \n",k);
10952: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
10953: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
10954: continue;
10955: }
1.219 brouard 10956:
1.238 brouard 10957: fprintf(ficrespl,"#Age ");
10958: for(j=1;j<=cptcoveff;j++) {
10959: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10960: }
10961: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
10962: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 10963:
1.238 brouard 10964: for (age=agebase; age<=agelim; age++){
10965: /* for (age=agebase; age<=agebase; age++){ */
10966: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres);
10967: fprintf(ficrespl,"%.0f ",age );
10968: for(j=1;j<=cptcoveff;j++)
10969: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10970: tot=0.;
10971: for(i=1; i<=nlstate;i++){
10972: tot += prlim[i][i];
10973: fprintf(ficrespl," %.5f", prlim[i][i]);
10974: }
10975: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
10976: } /* Age */
10977: /* was end of cptcod */
10978: } /* cptcov */
10979: } /* nres */
1.219 brouard 10980: return 0;
1.180 brouard 10981: }
10982:
1.218 brouard 10983: 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 10984: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
1.218 brouard 10985:
10986: /* Computes the back prevalence limit for any combination of covariate values
10987: * at any age between ageminpar and agemaxpar
10988: */
1.235 brouard 10989: int i, j, k, i1, nres=0 ;
1.217 brouard 10990: /* double ftolpl = 1.e-10; */
10991: double age, agebase, agelim;
10992: double tot;
1.218 brouard 10993: /* double ***mobaverage; */
10994: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 10995:
10996: strcpy(fileresplb,"PLB_");
10997: strcat(fileresplb,fileresu);
10998: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
1.288 brouard 10999: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
11000: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
1.217 brouard 11001: }
1.288 brouard 11002: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
11003: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
1.217 brouard 11004: pstamp(ficresplb);
1.288 brouard 11005: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.217 brouard 11006: fprintf(ficresplb,"#Age ");
11007: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
11008: fprintf(ficresplb,"\n");
11009:
1.218 brouard 11010:
11011: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
11012:
11013: agebase=ageminpar;
11014: agelim=agemaxpar;
11015:
11016:
1.227 brouard 11017: i1=pow(2,cptcoveff);
1.218 brouard 11018: if (cptcovn < 1){i1=1;}
1.227 brouard 11019:
1.238 brouard 11020: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11021: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 11022: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 11023: continue;
11024: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
11025: fprintf(ficresplb,"#******");
11026: printf("#******");
11027: fprintf(ficlog,"#******");
11028: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
11029: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11030: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11031: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11032: }
11033: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11034: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11035: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11036: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11037: }
11038: fprintf(ficresplb,"******\n");
11039: printf("******\n");
11040: fprintf(ficlog,"******\n");
11041: if(invalidvarcomb[k]){
11042: printf("\nCombination (%d) ignored because no cases \n",k);
11043: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
11044: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
11045: continue;
11046: }
1.218 brouard 11047:
1.238 brouard 11048: fprintf(ficresplb,"#Age ");
11049: for(j=1;j<=cptcoveff;j++) {
11050: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11051: }
11052: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
11053: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 11054:
11055:
1.238 brouard 11056: for (age=agebase; age<=agelim; age++){
11057: /* for (age=agebase; age<=agebase; age++){ */
11058: if(mobilavproj > 0){
11059: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
11060: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 11061: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 11062: }else if (mobilavproj == 0){
11063: 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);
11064: 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);
11065: exit(1);
11066: }else{
11067: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 11068: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.266 brouard 11069: /* printf("TOTOT\n"); */
11070: /* exit(1); */
1.238 brouard 11071: }
11072: fprintf(ficresplb,"%.0f ",age );
11073: for(j=1;j<=cptcoveff;j++)
11074: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11075: tot=0.;
11076: for(i=1; i<=nlstate;i++){
11077: tot += bprlim[i][i];
11078: fprintf(ficresplb," %.5f", bprlim[i][i]);
11079: }
11080: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
11081: } /* Age */
11082: /* was end of cptcod */
1.255 brouard 11083: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
1.238 brouard 11084: } /* end of any combination */
11085: } /* end of nres */
1.218 brouard 11086: /* hBijx(p, bage, fage); */
11087: /* fclose(ficrespijb); */
11088:
11089: return 0;
1.217 brouard 11090: }
1.218 brouard 11091:
1.180 brouard 11092: int hPijx(double *p, int bage, int fage){
11093: /*------------- h Pij x at various ages ------------*/
11094:
11095: int stepsize;
11096: int agelim;
11097: int hstepm;
11098: int nhstepm;
1.235 brouard 11099: int h, i, i1, j, k, k4, nres=0;
1.180 brouard 11100:
11101: double agedeb;
11102: double ***p3mat;
11103:
1.201 brouard 11104: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 11105: if((ficrespij=fopen(filerespij,"w"))==NULL) {
11106: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
11107: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
11108: }
11109: printf("Computing pij: result on file '%s' \n", filerespij);
11110: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
11111:
11112: stepsize=(int) (stepm+YEARM-1)/YEARM;
11113: /*if (stepm<=24) stepsize=2;*/
11114:
11115: agelim=AGESUP;
11116: hstepm=stepsize*YEARM; /* Every year of age */
11117: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 11118:
1.180 brouard 11119: /* hstepm=1; aff par mois*/
11120: pstamp(ficrespij);
11121: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 11122: i1= pow(2,cptcoveff);
1.218 brouard 11123: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
11124: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
11125: /* k=k+1; */
1.235 brouard 11126: for(nres=1; nres <= nresult; nres++) /* For each resultline */
11127: for(k=1; k<=i1;k++){
1.253 brouard 11128: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 11129: continue;
1.183 brouard 11130: fprintf(ficrespij,"\n#****** ");
1.227 brouard 11131: for(j=1;j<=cptcoveff;j++)
1.198 brouard 11132: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 11133: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
11134: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
11135: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
11136: }
1.183 brouard 11137: fprintf(ficrespij,"******\n");
11138:
11139: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
11140: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
11141: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
11142:
11143: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 11144:
1.183 brouard 11145: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11146: oldm=oldms;savm=savms;
1.235 brouard 11147: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.183 brouard 11148: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
11149: for(i=1; i<=nlstate;i++)
11150: for(j=1; j<=nlstate+ndeath;j++)
11151: fprintf(ficrespij," %1d-%1d",i,j);
11152: fprintf(ficrespij,"\n");
11153: for (h=0; h<=nhstepm; h++){
11154: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
11155: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 11156: for(i=1; i<=nlstate;i++)
11157: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 11158: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 11159: fprintf(ficrespij,"\n");
11160: }
1.183 brouard 11161: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11162: fprintf(ficrespij,"\n");
11163: }
1.180 brouard 11164: /*}*/
11165: }
1.218 brouard 11166: return 0;
1.180 brouard 11167: }
1.218 brouard 11168:
11169: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 11170: /*------------- h Bij x at various ages ------------*/
11171:
11172: int stepsize;
1.218 brouard 11173: /* int agelim; */
11174: int ageminl;
1.217 brouard 11175: int hstepm;
11176: int nhstepm;
1.238 brouard 11177: int h, i, i1, j, k, nres;
1.218 brouard 11178:
1.217 brouard 11179: double agedeb;
11180: double ***p3mat;
1.218 brouard 11181:
11182: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
11183: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
11184: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
11185: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
11186: }
11187: printf("Computing pij back: result on file '%s' \n", filerespijb);
11188: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
11189:
11190: stepsize=(int) (stepm+YEARM-1)/YEARM;
11191: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 11192:
1.218 brouard 11193: /* agelim=AGESUP; */
1.289 brouard 11194: ageminl=AGEINF; /* was 30 */
1.218 brouard 11195: hstepm=stepsize*YEARM; /* Every year of age */
11196: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
11197:
11198: /* hstepm=1; aff par mois*/
11199: pstamp(ficrespijb);
1.255 brouard 11200: 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 11201: i1= pow(2,cptcoveff);
1.218 brouard 11202: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
11203: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
11204: /* k=k+1; */
1.238 brouard 11205: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
11206: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 11207: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 11208: continue;
11209: fprintf(ficrespijb,"\n#****** ");
11210: for(j=1;j<=cptcoveff;j++)
11211: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
11212: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
11213: fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
11214: }
11215: fprintf(ficrespijb,"******\n");
1.264 brouard 11216: if(invalidvarcomb[k]){ /* Is it necessary here? */
1.238 brouard 11217: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
11218: continue;
11219: }
11220:
11221: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
11222: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
11223: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
1.297 brouard 11224: 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 */
11225: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
1.238 brouard 11226:
11227: /* nhstepm=nhstepm*YEARM; aff par mois*/
11228:
1.266 brouard 11229: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
11230: /* and memory limitations if stepm is small */
11231:
1.238 brouard 11232: /* oldm=oldms;savm=savms; */
11233: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.325 brouard 11234: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);/* Bug valgrind */
1.238 brouard 11235: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
1.255 brouard 11236: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
1.217 brouard 11237: for(i=1; i<=nlstate;i++)
11238: for(j=1; j<=nlstate+ndeath;j++)
1.238 brouard 11239: fprintf(ficrespijb," %1d-%1d",i,j);
1.217 brouard 11240: fprintf(ficrespijb,"\n");
1.238 brouard 11241: for (h=0; h<=nhstepm; h++){
11242: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
11243: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
11244: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
11245: for(i=1; i<=nlstate;i++)
11246: for(j=1; j<=nlstate+ndeath;j++)
1.325 brouard 11247: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);/* Bug valgrind */
1.238 brouard 11248: fprintf(ficrespijb,"\n");
11249: }
11250: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
11251: fprintf(ficrespijb,"\n");
11252: } /* end age deb */
11253: } /* end combination */
11254: } /* end nres */
1.218 brouard 11255: return 0;
11256: } /* hBijx */
1.217 brouard 11257:
1.180 brouard 11258:
1.136 brouard 11259: /***********************************************/
11260: /**************** Main Program *****************/
11261: /***********************************************/
11262:
11263: int main(int argc, char *argv[])
11264: {
11265: #ifdef GSL
11266: const gsl_multimin_fminimizer_type *T;
11267: size_t iteri = 0, it;
11268: int rval = GSL_CONTINUE;
11269: int status = GSL_SUCCESS;
11270: double ssval;
11271: #endif
11272: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.290 brouard 11273: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
11274: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
1.209 brouard 11275: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 11276: int jj, ll, li, lj, lk;
1.136 brouard 11277: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 11278: int num_filled;
1.136 brouard 11279: int itimes;
11280: int NDIM=2;
11281: int vpopbased=0;
1.235 brouard 11282: int nres=0;
1.258 brouard 11283: int endishere=0;
1.277 brouard 11284: int noffset=0;
1.274 brouard 11285: int ncurrv=0; /* Temporary variable */
11286:
1.164 brouard 11287: char ca[32], cb[32];
1.136 brouard 11288: /* FILE *fichtm; *//* Html File */
11289: /* FILE *ficgp;*/ /*Gnuplot File */
11290: struct stat info;
1.191 brouard 11291: double agedeb=0.;
1.194 brouard 11292:
11293: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 11294: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 11295:
1.165 brouard 11296: double fret;
1.191 brouard 11297: double dum=0.; /* Dummy variable */
1.136 brouard 11298: double ***p3mat;
1.218 brouard 11299: /* double ***mobaverage; */
1.319 brouard 11300: double wald;
1.164 brouard 11301:
11302: char line[MAXLINE];
1.197 brouard 11303: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
11304:
1.234 brouard 11305: char modeltemp[MAXLINE];
1.230 brouard 11306: char resultline[MAXLINE];
11307:
1.136 brouard 11308: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 11309: char *tok, *val; /* pathtot */
1.290 brouard 11310: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs declared globally ;*/
1.195 brouard 11311: int c, h , cpt, c2;
1.191 brouard 11312: int jl=0;
11313: int i1, j1, jk, stepsize=0;
1.194 brouard 11314: int count=0;
11315:
1.164 brouard 11316: int *tab;
1.136 brouard 11317: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.296 brouard 11318: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
11319: /* double anprojf, mprojf, jprojf; */
11320: /* double jintmean,mintmean,aintmean; */
11321: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
11322: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
11323: double yrfproj= 10.0; /* Number of years of forward projections */
11324: double yrbproj= 10.0; /* Number of years of backward projections */
11325: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
1.136 brouard 11326: int mobilav=0,popforecast=0;
1.191 brouard 11327: int hstepm=0, nhstepm=0;
1.136 brouard 11328: int agemortsup;
11329: float sumlpop=0.;
11330: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
11331: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
11332:
1.191 brouard 11333: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 11334: double ftolpl=FTOL;
11335: double **prlim;
1.217 brouard 11336: double **bprlim;
1.317 brouard 11337: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel)
11338: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */
1.251 brouard 11339: double ***paramstart; /* Matrix of starting parameter values */
11340: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
1.136 brouard 11341: double **matcov; /* Matrix of covariance */
1.203 brouard 11342: double **hess; /* Hessian matrix */
1.136 brouard 11343: double ***delti3; /* Scale */
11344: double *delti; /* Scale */
11345: double ***eij, ***vareij;
11346: double **varpl; /* Variances of prevalence limits by age */
1.269 brouard 11347:
1.136 brouard 11348: double *epj, vepp;
1.164 brouard 11349:
1.273 brouard 11350: double dateprev1, dateprev2;
1.296 brouard 11351: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
11352: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
11353:
1.217 brouard 11354:
1.136 brouard 11355: double **ximort;
1.145 brouard 11356: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 11357: int *dcwave;
11358:
1.164 brouard 11359: char z[1]="c";
1.136 brouard 11360:
11361: /*char *strt;*/
11362: char strtend[80];
1.126 brouard 11363:
1.164 brouard 11364:
1.126 brouard 11365: /* setlocale (LC_ALL, ""); */
11366: /* bindtextdomain (PACKAGE, LOCALEDIR); */
11367: /* textdomain (PACKAGE); */
11368: /* setlocale (LC_CTYPE, ""); */
11369: /* setlocale (LC_MESSAGES, ""); */
11370:
11371: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 11372: rstart_time = time(NULL);
11373: /* (void) gettimeofday(&start_time,&tzp);*/
11374: start_time = *localtime(&rstart_time);
1.126 brouard 11375: curr_time=start_time;
1.157 brouard 11376: /*tml = *localtime(&start_time.tm_sec);*/
11377: /* strcpy(strstart,asctime(&tml)); */
11378: strcpy(strstart,asctime(&start_time));
1.126 brouard 11379:
11380: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 11381: /* tp.tm_sec = tp.tm_sec +86400; */
11382: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 11383: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
11384: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
11385: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 11386: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 11387: /* strt=asctime(&tmg); */
11388: /* printf("Time(after) =%s",strstart); */
11389: /* (void) time (&time_value);
11390: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
11391: * tm = *localtime(&time_value);
11392: * strstart=asctime(&tm);
11393: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
11394: */
11395:
11396: nberr=0; /* Number of errors and warnings */
11397: nbwarn=0;
1.184 brouard 11398: #ifdef WIN32
11399: _getcwd(pathcd, size);
11400: #else
1.126 brouard 11401: getcwd(pathcd, size);
1.184 brouard 11402: #endif
1.191 brouard 11403: syscompilerinfo(0);
1.196 brouard 11404: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 11405: if(argc <=1){
11406: printf("\nEnter the parameter file name: ");
1.205 brouard 11407: if(!fgets(pathr,FILENAMELENGTH,stdin)){
11408: printf("ERROR Empty parameter file name\n");
11409: goto end;
11410: }
1.126 brouard 11411: i=strlen(pathr);
11412: if(pathr[i-1]=='\n')
11413: pathr[i-1]='\0';
1.156 brouard 11414: i=strlen(pathr);
1.205 brouard 11415: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 11416: pathr[i-1]='\0';
1.205 brouard 11417: }
11418: i=strlen(pathr);
11419: if( i==0 ){
11420: printf("ERROR Empty parameter file name\n");
11421: goto end;
11422: }
11423: for (tok = pathr; tok != NULL; ){
1.126 brouard 11424: printf("Pathr |%s|\n",pathr);
11425: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
11426: printf("val= |%s| pathr=%s\n",val,pathr);
11427: strcpy (pathtot, val);
11428: if(pathr[0] == '\0') break; /* Dirty */
11429: }
11430: }
1.281 brouard 11431: else if (argc<=2){
11432: strcpy(pathtot,argv[1]);
11433: }
1.126 brouard 11434: else{
11435: strcpy(pathtot,argv[1]);
1.281 brouard 11436: strcpy(z,argv[2]);
11437: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
1.126 brouard 11438: }
11439: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
11440: /*cygwin_split_path(pathtot,path,optionfile);
11441: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
11442: /* cutv(path,optionfile,pathtot,'\\');*/
11443:
11444: /* Split argv[0], imach program to get pathimach */
11445: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
11446: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11447: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11448: /* strcpy(pathimach,argv[0]); */
11449: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
11450: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
11451: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 11452: #ifdef WIN32
11453: _chdir(path); /* Can be a relative path */
11454: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
11455: #else
1.126 brouard 11456: chdir(path); /* Can be a relative path */
1.184 brouard 11457: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
11458: #endif
11459: printf("Current directory %s!\n",pathcd);
1.126 brouard 11460: strcpy(command,"mkdir ");
11461: strcat(command,optionfilefiname);
11462: if((outcmd=system(command)) != 0){
1.169 brouard 11463: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 11464: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
11465: /* fclose(ficlog); */
11466: /* exit(1); */
11467: }
11468: /* if((imk=mkdir(optionfilefiname))<0){ */
11469: /* perror("mkdir"); */
11470: /* } */
11471:
11472: /*-------- arguments in the command line --------*/
11473:
1.186 brouard 11474: /* Main Log file */
1.126 brouard 11475: strcat(filelog, optionfilefiname);
11476: strcat(filelog,".log"); /* */
11477: if((ficlog=fopen(filelog,"w"))==NULL) {
11478: printf("Problem with logfile %s\n",filelog);
11479: goto end;
11480: }
11481: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 11482: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 11483: fprintf(ficlog,"\nEnter the parameter file name: \n");
11484: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
11485: path=%s \n\
11486: optionfile=%s\n\
11487: optionfilext=%s\n\
1.156 brouard 11488: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 11489:
1.197 brouard 11490: syscompilerinfo(1);
1.167 brouard 11491:
1.126 brouard 11492: printf("Local time (at start):%s",strstart);
11493: fprintf(ficlog,"Local time (at start): %s",strstart);
11494: fflush(ficlog);
11495: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 11496: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 11497:
11498: /* */
11499: strcpy(fileres,"r");
11500: strcat(fileres, optionfilefiname);
1.201 brouard 11501: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 11502: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 11503: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 11504:
1.186 brouard 11505: /* Main ---------arguments file --------*/
1.126 brouard 11506:
11507: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 11508: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
11509: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 11510: fflush(ficlog);
1.149 brouard 11511: /* goto end; */
11512: exit(70);
1.126 brouard 11513: }
11514:
11515: strcpy(filereso,"o");
1.201 brouard 11516: strcat(filereso,fileresu);
1.126 brouard 11517: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
11518: printf("Problem with Output resultfile: %s\n", filereso);
11519: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
11520: fflush(ficlog);
11521: goto end;
11522: }
1.278 brouard 11523: /*-------- Rewriting parameter file ----------*/
11524: strcpy(rfileres,"r"); /* "Rparameterfile */
11525: strcat(rfileres,optionfilefiname); /* Parameter file first name */
11526: strcat(rfileres,"."); /* */
11527: strcat(rfileres,optionfilext); /* Other files have txt extension */
11528: if((ficres =fopen(rfileres,"w"))==NULL) {
11529: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
11530: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
11531: fflush(ficlog);
11532: goto end;
11533: }
11534: fprintf(ficres,"#IMaCh %s\n",version);
1.126 brouard 11535:
1.278 brouard 11536:
1.126 brouard 11537: /* Reads comments: lines beginning with '#' */
11538: numlinepar=0;
1.277 brouard 11539: /* Is it a BOM UTF-8 Windows file? */
11540: /* First parameter line */
1.197 brouard 11541: while(fgets(line, MAXLINE, ficpar)) {
1.277 brouard 11542: noffset=0;
11543: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
11544: {
11545: noffset=noffset+3;
11546: printf("# File is an UTF8 Bom.\n"); // 0xBF
11547: }
1.302 brouard 11548: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
11549: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
1.277 brouard 11550: {
11551: noffset=noffset+2;
11552: printf("# File is an UTF16BE BOM file\n");
11553: }
11554: else if( line[0] == 0 && line[1] == 0)
11555: {
11556: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
11557: noffset=noffset+4;
11558: printf("# File is an UTF16BE BOM file\n");
11559: }
11560: } else{
11561: ;/*printf(" Not a BOM file\n");*/
11562: }
11563:
1.197 brouard 11564: /* If line starts with a # it is a comment */
1.277 brouard 11565: if (line[noffset] == '#') {
1.197 brouard 11566: numlinepar++;
11567: fputs(line,stdout);
11568: fputs(line,ficparo);
1.278 brouard 11569: fputs(line,ficres);
1.197 brouard 11570: fputs(line,ficlog);
11571: continue;
11572: }else
11573: break;
11574: }
11575: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
11576: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
11577: if (num_filled != 5) {
11578: printf("Should be 5 parameters\n");
1.283 brouard 11579: fprintf(ficlog,"Should be 5 parameters\n");
1.197 brouard 11580: }
1.126 brouard 11581: numlinepar++;
1.197 brouard 11582: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.283 brouard 11583: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11584: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11585: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.197 brouard 11586: }
11587: /* Second parameter line */
11588: while(fgets(line, MAXLINE, ficpar)) {
1.283 brouard 11589: /* while(fscanf(ficpar,"%[^\n]", line)) { */
11590: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
1.197 brouard 11591: if (line[0] == '#') {
11592: numlinepar++;
1.283 brouard 11593: printf("%s",line);
11594: fprintf(ficres,"%s",line);
11595: fprintf(ficparo,"%s",line);
11596: fprintf(ficlog,"%s",line);
1.197 brouard 11597: continue;
11598: }else
11599: break;
11600: }
1.223 brouard 11601: 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", \
11602: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
11603: if (num_filled != 11) {
11604: 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 11605: printf("but line=%s\n",line);
1.283 brouard 11606: 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");
11607: fprintf(ficlog,"but line=%s\n",line);
1.197 brouard 11608: }
1.286 brouard 11609: if( lastpass > maxwav){
11610: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11611: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11612: fflush(ficlog);
11613: goto end;
11614: }
11615: 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 11616: 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 11617: 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 11618: 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 11619: }
1.203 brouard 11620: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 11621: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 11622: /* Third parameter line */
11623: while(fgets(line, MAXLINE, ficpar)) {
11624: /* If line starts with a # it is a comment */
11625: if (line[0] == '#') {
11626: numlinepar++;
1.283 brouard 11627: printf("%s",line);
11628: fprintf(ficres,"%s",line);
11629: fprintf(ficparo,"%s",line);
11630: fprintf(ficlog,"%s",line);
1.197 brouard 11631: continue;
11632: }else
11633: break;
11634: }
1.201 brouard 11635: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
1.279 brouard 11636: if (num_filled != 1){
1.302 brouard 11637: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
11638: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
1.197 brouard 11639: model[0]='\0';
11640: goto end;
11641: }
11642: else{
11643: if (model[0]=='+'){
11644: for(i=1; i<=strlen(model);i++)
11645: modeltemp[i-1]=model[i];
1.201 brouard 11646: strcpy(model,modeltemp);
1.197 brouard 11647: }
11648: }
1.199 brouard 11649: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 11650: printf("model=1+age+%s\n",model);fflush(stdout);
1.283 brouard 11651: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
11652: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
11653: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 11654: }
11655: /* 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); */
11656: /* numlinepar=numlinepar+3; /\* In general *\/ */
11657: /* 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 11658: /* 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); */
11659: /* 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 11660: fflush(ficlog);
1.190 brouard 11661: /* if(model[0]=='#'|| model[0]== '\0'){ */
11662: if(model[0]=='#'){
1.279 brouard 11663: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
11664: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
11665: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
1.187 brouard 11666: if(mle != -1){
1.279 brouard 11667: 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 11668: exit(1);
11669: }
11670: }
1.126 brouard 11671: while((c=getc(ficpar))=='#' && c!= EOF){
11672: ungetc(c,ficpar);
11673: fgets(line, MAXLINE, ficpar);
11674: numlinepar++;
1.195 brouard 11675: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
11676: z[0]=line[1];
11677: }
11678: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 11679: fputs(line, stdout);
11680: //puts(line);
1.126 brouard 11681: fputs(line,ficparo);
11682: fputs(line,ficlog);
11683: }
11684: ungetc(c,ficpar);
11685:
11686:
1.290 brouard 11687: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
11688: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
11689: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
11690: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /**< Time varying covariate (dummy and quantitative)*/
1.136 brouard 11691: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
11692: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
11693: v1+v2*age+v2*v3 makes cptcovn = 3
11694: */
11695: if (strlen(model)>1)
1.187 brouard 11696: 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 11697: else
1.187 brouard 11698: ncovmodel=2; /* Constant and age */
1.133 brouard 11699: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
11700: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 11701: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
11702: 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);
11703: 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);
11704: fflush(stdout);
11705: fclose (ficlog);
11706: goto end;
11707: }
1.126 brouard 11708: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11709: delti=delti3[1][1];
11710: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
11711: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
1.247 brouard 11712: /* We could also provide initial parameters values giving by simple logistic regression
11713: * only one way, that is without matrix product. We will have nlstate maximizations */
11714: /* for(i=1;i<nlstate;i++){ */
11715: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
11716: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
11717: /* } */
1.126 brouard 11718: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 11719: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
11720: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11721: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11722: fclose (ficparo);
11723: fclose (ficlog);
11724: goto end;
11725: exit(0);
1.220 brouard 11726: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 11727: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 11728: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
11729: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11730: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11731: matcov=matrix(1,npar,1,npar);
1.203 brouard 11732: hess=matrix(1,npar,1,npar);
1.220 brouard 11733: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 11734: /* Read guessed parameters */
1.126 brouard 11735: /* Reads comments: lines beginning with '#' */
11736: while((c=getc(ficpar))=='#' && c!= EOF){
11737: ungetc(c,ficpar);
11738: fgets(line, MAXLINE, ficpar);
11739: numlinepar++;
1.141 brouard 11740: fputs(line,stdout);
1.126 brouard 11741: fputs(line,ficparo);
11742: fputs(line,ficlog);
11743: }
11744: ungetc(c,ficpar);
11745:
11746: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.251 brouard 11747: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.126 brouard 11748: for(i=1; i <=nlstate; i++){
1.234 brouard 11749: j=0;
1.126 brouard 11750: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 11751: if(jj==i) continue;
11752: j++;
1.292 brouard 11753: while((c=getc(ficpar))=='#' && c!= EOF){
11754: ungetc(c,ficpar);
11755: fgets(line, MAXLINE, ficpar);
11756: numlinepar++;
11757: fputs(line,stdout);
11758: fputs(line,ficparo);
11759: fputs(line,ficlog);
11760: }
11761: ungetc(c,ficpar);
1.234 brouard 11762: fscanf(ficpar,"%1d%1d",&i1,&j1);
11763: if ((i1 != i) || (j1 != jj)){
11764: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 11765: It might be a problem of design; if ncovcol and the model are correct\n \
11766: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 11767: exit(1);
11768: }
11769: fprintf(ficparo,"%1d%1d",i1,j1);
11770: if(mle==1)
11771: printf("%1d%1d",i,jj);
11772: fprintf(ficlog,"%1d%1d",i,jj);
11773: for(k=1; k<=ncovmodel;k++){
11774: fscanf(ficpar," %lf",¶m[i][j][k]);
11775: if(mle==1){
11776: printf(" %lf",param[i][j][k]);
11777: fprintf(ficlog," %lf",param[i][j][k]);
11778: }
11779: else
11780: fprintf(ficlog," %lf",param[i][j][k]);
11781: fprintf(ficparo," %lf",param[i][j][k]);
11782: }
11783: fscanf(ficpar,"\n");
11784: numlinepar++;
11785: if(mle==1)
11786: printf("\n");
11787: fprintf(ficlog,"\n");
11788: fprintf(ficparo,"\n");
1.126 brouard 11789: }
11790: }
11791: fflush(ficlog);
1.234 brouard 11792:
1.251 brouard 11793: /* Reads parameters values */
1.126 brouard 11794: p=param[1][1];
1.251 brouard 11795: pstart=paramstart[1][1];
1.126 brouard 11796:
11797: /* Reads comments: lines beginning with '#' */
11798: while((c=getc(ficpar))=='#' && c!= EOF){
11799: ungetc(c,ficpar);
11800: fgets(line, MAXLINE, ficpar);
11801: numlinepar++;
1.141 brouard 11802: fputs(line,stdout);
1.126 brouard 11803: fputs(line,ficparo);
11804: fputs(line,ficlog);
11805: }
11806: ungetc(c,ficpar);
11807:
11808: for(i=1; i <=nlstate; i++){
11809: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 11810: fscanf(ficpar,"%1d%1d",&i1,&j1);
11811: if ( (i1-i) * (j1-j) != 0){
11812: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
11813: exit(1);
11814: }
11815: printf("%1d%1d",i,j);
11816: fprintf(ficparo,"%1d%1d",i1,j1);
11817: fprintf(ficlog,"%1d%1d",i1,j1);
11818: for(k=1; k<=ncovmodel;k++){
11819: fscanf(ficpar,"%le",&delti3[i][j][k]);
11820: printf(" %le",delti3[i][j][k]);
11821: fprintf(ficparo," %le",delti3[i][j][k]);
11822: fprintf(ficlog," %le",delti3[i][j][k]);
11823: }
11824: fscanf(ficpar,"\n");
11825: numlinepar++;
11826: printf("\n");
11827: fprintf(ficparo,"\n");
11828: fprintf(ficlog,"\n");
1.126 brouard 11829: }
11830: }
11831: fflush(ficlog);
1.234 brouard 11832:
1.145 brouard 11833: /* Reads covariance matrix */
1.126 brouard 11834: delti=delti3[1][1];
1.220 brouard 11835:
11836:
1.126 brouard 11837: /* 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 11838:
1.126 brouard 11839: /* Reads comments: lines beginning with '#' */
11840: while((c=getc(ficpar))=='#' && c!= EOF){
11841: ungetc(c,ficpar);
11842: fgets(line, MAXLINE, ficpar);
11843: numlinepar++;
1.141 brouard 11844: fputs(line,stdout);
1.126 brouard 11845: fputs(line,ficparo);
11846: fputs(line,ficlog);
11847: }
11848: ungetc(c,ficpar);
1.220 brouard 11849:
1.126 brouard 11850: matcov=matrix(1,npar,1,npar);
1.203 brouard 11851: hess=matrix(1,npar,1,npar);
1.131 brouard 11852: for(i=1; i <=npar; i++)
11853: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 11854:
1.194 brouard 11855: /* Scans npar lines */
1.126 brouard 11856: for(i=1; i <=npar; i++){
1.226 brouard 11857: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 11858: if(count != 3){
1.226 brouard 11859: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11860: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11861: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11862: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11863: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11864: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11865: exit(1);
1.220 brouard 11866: }else{
1.226 brouard 11867: if(mle==1)
11868: printf("%1d%1d%d",i1,j1,jk);
11869: }
11870: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
11871: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 11872: for(j=1; j <=i; j++){
1.226 brouard 11873: fscanf(ficpar," %le",&matcov[i][j]);
11874: if(mle==1){
11875: printf(" %.5le",matcov[i][j]);
11876: }
11877: fprintf(ficlog," %.5le",matcov[i][j]);
11878: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 11879: }
11880: fscanf(ficpar,"\n");
11881: numlinepar++;
11882: if(mle==1)
1.220 brouard 11883: printf("\n");
1.126 brouard 11884: fprintf(ficlog,"\n");
11885: fprintf(ficparo,"\n");
11886: }
1.194 brouard 11887: /* End of read covariance matrix npar lines */
1.126 brouard 11888: for(i=1; i <=npar; i++)
11889: for(j=i+1;j<=npar;j++)
1.226 brouard 11890: matcov[i][j]=matcov[j][i];
1.126 brouard 11891:
11892: if(mle==1)
11893: printf("\n");
11894: fprintf(ficlog,"\n");
11895:
11896: fflush(ficlog);
11897:
11898: } /* End of mle != -3 */
1.218 brouard 11899:
1.186 brouard 11900: /* Main data
11901: */
1.290 brouard 11902: nobs=lastobs-firstobs+1; /* was = lastobs;*/
11903: /* num=lvector(1,n); */
11904: /* moisnais=vector(1,n); */
11905: /* annais=vector(1,n); */
11906: /* moisdc=vector(1,n); */
11907: /* andc=vector(1,n); */
11908: /* weight=vector(1,n); */
11909: /* agedc=vector(1,n); */
11910: /* cod=ivector(1,n); */
11911: /* for(i=1;i<=n;i++){ */
11912: num=lvector(firstobs,lastobs);
11913: moisnais=vector(firstobs,lastobs);
11914: annais=vector(firstobs,lastobs);
11915: moisdc=vector(firstobs,lastobs);
11916: andc=vector(firstobs,lastobs);
11917: weight=vector(firstobs,lastobs);
11918: agedc=vector(firstobs,lastobs);
11919: cod=ivector(firstobs,lastobs);
11920: for(i=firstobs;i<=lastobs;i++){
1.234 brouard 11921: num[i]=0;
11922: moisnais[i]=0;
11923: annais[i]=0;
11924: moisdc[i]=0;
11925: andc[i]=0;
11926: agedc[i]=0;
11927: cod[i]=0;
11928: weight[i]=1.0; /* Equal weights, 1 by default */
11929: }
1.290 brouard 11930: mint=matrix(1,maxwav,firstobs,lastobs);
11931: anint=matrix(1,maxwav,firstobs,lastobs);
1.325 brouard 11932: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
11933: printf("BUG ncovmodel=%d NCOVMAX=%d 2**ncovmodel=%f BUG\n",ncovmodel,NCOVMAX,pow(2,ncovmodel));
1.126 brouard 11934: tab=ivector(1,NCOVMAX);
1.144 brouard 11935: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 11936: 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 11937:
1.136 brouard 11938: /* Reads data from file datafile */
11939: if (readdata(datafile, firstobs, lastobs, &imx)==1)
11940: goto end;
11941:
11942: /* Calculation of the number of parameters from char model */
1.234 brouard 11943: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 11944: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
11945: k=3 V4 Tvar[k=3]= 4 (from V4)
11946: k=2 V1 Tvar[k=2]= 1 (from V1)
11947: k=1 Tvar[1]=2 (from V2)
1.234 brouard 11948: */
11949:
11950: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
11951: TvarsDind=ivector(1,NCOVMAX); /* */
11952: TvarsD=ivector(1,NCOVMAX); /* */
11953: TvarsQind=ivector(1,NCOVMAX); /* */
11954: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 11955: TvarF=ivector(1,NCOVMAX); /* */
11956: TvarFind=ivector(1,NCOVMAX); /* */
11957: TvarV=ivector(1,NCOVMAX); /* */
11958: TvarVind=ivector(1,NCOVMAX); /* */
11959: TvarA=ivector(1,NCOVMAX); /* */
11960: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 11961: TvarFD=ivector(1,NCOVMAX); /* */
11962: TvarFDind=ivector(1,NCOVMAX); /* */
11963: TvarFQ=ivector(1,NCOVMAX); /* */
11964: TvarFQind=ivector(1,NCOVMAX); /* */
11965: TvarVD=ivector(1,NCOVMAX); /* */
11966: TvarVDind=ivector(1,NCOVMAX); /* */
11967: TvarVQ=ivector(1,NCOVMAX); /* */
11968: TvarVQind=ivector(1,NCOVMAX); /* */
11969:
1.230 brouard 11970: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 11971: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 11972: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
11973: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
11974: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 11975: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
11976: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
11977: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
11978: */
11979: /* For model-covariate k tells which data-covariate to use but
11980: because this model-covariate is a construction we invent a new column
11981: ncovcol + k1
11982: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
11983: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 11984: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
11985: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 11986: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
11987: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 11988: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 11989: */
1.145 brouard 11990: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
11991: 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 11992: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
11993: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 11994: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 11995: 4 covariates (3 plus signs)
11996: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
11997: */
1.230 brouard 11998: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 11999: * individual dummy, fixed or varying:
12000: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
12001: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 12002: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
12003: * V1 df, V2 qf, V3 & V4 dv, V5 qv
12004: * Tmodelind[1]@9={9,0,3,2,}*/
12005: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
12006: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 12007: * individual quantitative, fixed or varying:
12008: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
12009: * 3, 1, 0, 0, 0, 0, 0, 0},
12010: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 12011: /* Main decodemodel */
12012:
1.187 brouard 12013:
1.223 brouard 12014: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 12015: goto end;
12016:
1.137 brouard 12017: if((double)(lastobs-imx)/(double)imx > 1.10){
12018: nbwarn++;
12019: 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);
12020: 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);
12021: }
1.136 brouard 12022: /* if(mle==1){*/
1.137 brouard 12023: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
12024: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 12025: }
12026:
12027: /*-calculation of age at interview from date of interview and age at death -*/
12028: agev=matrix(1,maxwav,1,imx);
12029:
12030: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
12031: goto end;
12032:
1.126 brouard 12033:
1.136 brouard 12034: agegomp=(int)agemin;
1.290 brouard 12035: free_vector(moisnais,firstobs,lastobs);
12036: free_vector(annais,firstobs,lastobs);
1.126 brouard 12037: /* free_matrix(mint,1,maxwav,1,n);
12038: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 12039: /* free_vector(moisdc,1,n); */
12040: /* free_vector(andc,1,n); */
1.145 brouard 12041: /* */
12042:
1.126 brouard 12043: wav=ivector(1,imx);
1.214 brouard 12044: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
12045: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
12046: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
12047: 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.*/
12048: bh=imatrix(1,lastpass-firstpass+2,1,imx);
12049: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 12050:
12051: /* Concatenates waves */
1.214 brouard 12052: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
12053: Death is a valid wave (if date is known).
12054: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
12055: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
12056: and mw[mi+1][i]. dh depends on stepm.
12057: */
12058:
1.126 brouard 12059: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.248 brouard 12060: /* Concatenates waves */
1.145 brouard 12061:
1.290 brouard 12062: free_vector(moisdc,firstobs,lastobs);
12063: free_vector(andc,firstobs,lastobs);
1.215 brouard 12064:
1.126 brouard 12065: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
12066: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
12067: ncodemax[1]=1;
1.145 brouard 12068: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 12069: cptcoveff=0;
1.220 brouard 12070: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
12071: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 12072: }
12073:
12074: ncovcombmax=pow(2,cptcoveff);
12075: invalidvarcomb=ivector(1, ncovcombmax);
12076: for(i=1;i<ncovcombmax;i++)
12077: invalidvarcomb[i]=0;
12078:
1.211 brouard 12079: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 12080: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 12081: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 12082:
1.200 brouard 12083: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 12084: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 12085: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 12086: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
12087: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
12088: * (currently 0 or 1) in the data.
12089: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
12090: * corresponding modality (h,j).
12091: */
12092:
1.145 brouard 12093: h=0;
12094: /*if (cptcovn > 0) */
1.126 brouard 12095: m=pow(2,cptcoveff);
12096:
1.144 brouard 12097: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 12098: * For k=4 covariates, h goes from 1 to m=2**k
12099: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
12100: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 12101: * h\k 1 2 3 4
1.143 brouard 12102: *______________________________
12103: * 1 i=1 1 i=1 1 i=1 1 i=1 1
12104: * 2 2 1 1 1
12105: * 3 i=2 1 2 1 1
12106: * 4 2 2 1 1
12107: * 5 i=3 1 i=2 1 2 1
12108: * 6 2 1 2 1
12109: * 7 i=4 1 2 2 1
12110: * 8 2 2 2 1
1.197 brouard 12111: * 9 i=5 1 i=3 1 i=2 1 2
12112: * 10 2 1 1 2
12113: * 11 i=6 1 2 1 2
12114: * 12 2 2 1 2
12115: * 13 i=7 1 i=4 1 2 2
12116: * 14 2 1 2 2
12117: * 15 i=8 1 2 2 2
12118: * 16 2 2 2 2
1.143 brouard 12119: */
1.212 brouard 12120: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 12121: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
12122: * and the value of each covariate?
12123: * V1=1, V2=1, V3=2, V4=1 ?
12124: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
12125: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
12126: * In order to get the real value in the data, we use nbcode
12127: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
12128: * We are keeping this crazy system in order to be able (in the future?)
12129: * to have more than 2 values (0 or 1) for a covariate.
12130: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
12131: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
12132: * bbbbbbbb
12133: * 76543210
12134: * h-1 00000101 (6-1=5)
1.219 brouard 12135: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 12136: * &
12137: * 1 00000001 (1)
1.219 brouard 12138: * 00000000 = 1 & ((h-1) >> (k-1))
12139: * +1= 00000001 =1
1.211 brouard 12140: *
12141: * h=14, k=3 => h'=h-1=13, k'=k-1=2
12142: * h' 1101 =2^3+2^2+0x2^1+2^0
12143: * >>k' 11
12144: * & 00000001
12145: * = 00000001
12146: * +1 = 00000010=2 = codtabm(14,3)
12147: * Reverse h=6 and m=16?
12148: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
12149: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
12150: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
12151: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
12152: * V3=decodtabm(14,3,2**4)=2
12153: * h'=13 1101 =2^3+2^2+0x2^1+2^0
12154: *(h-1) >> (j-1) 0011 =13 >> 2
12155: * &1 000000001
12156: * = 000000001
12157: * +1= 000000010 =2
12158: * 2211
12159: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
12160: * V3=2
1.220 brouard 12161: * codtabm and decodtabm are identical
1.211 brouard 12162: */
12163:
1.145 brouard 12164:
12165: free_ivector(Ndum,-1,NCOVMAX);
12166:
12167:
1.126 brouard 12168:
1.186 brouard 12169: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 12170: strcpy(optionfilegnuplot,optionfilefiname);
12171: if(mle==-3)
1.201 brouard 12172: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 12173: strcat(optionfilegnuplot,".gp");
12174:
12175: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
12176: printf("Problem with file %s",optionfilegnuplot);
12177: }
12178: else{
1.204 brouard 12179: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 12180: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 12181: //fprintf(ficgp,"set missing 'NaNq'\n");
12182: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 12183: }
12184: /* fclose(ficgp);*/
1.186 brouard 12185:
12186:
12187: /* Initialisation of --------- index.htm --------*/
1.126 brouard 12188:
12189: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
12190: if(mle==-3)
1.201 brouard 12191: strcat(optionfilehtm,"-MORT_");
1.126 brouard 12192: strcat(optionfilehtm,".htm");
12193: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 12194: printf("Problem with %s \n",optionfilehtm);
12195: exit(0);
1.126 brouard 12196: }
12197:
12198: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
12199: strcat(optionfilehtmcov,"-cov.htm");
12200: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
12201: printf("Problem with %s \n",optionfilehtmcov), exit(0);
12202: }
12203: else{
12204: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
12205: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 12206: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 12207: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
12208: }
12209:
1.324 brouard 12210: fprintf(fichtm,"<html><head>\n<head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<title>IMaCh %s</title></head>\n <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n<font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>-EUROREVES-Institut de longévité-2013-2016-Japan Society for the Promotion of Sciences 日本学術振興会 (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \
1.204 brouard 12211: <hr size=\"2\" color=\"#EC5E5E\"> \n\
12212: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 12213: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 12214: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 12215: \n\
12216: <hr size=\"2\" color=\"#EC5E5E\">\
12217: <ul><li><h4>Parameter files</h4>\n\
12218: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
12219: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
12220: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
12221: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
12222: - Date and time at start: %s</ul>\n",\
12223: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
12224: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
12225: fileres,fileres,\
12226: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
12227: fflush(fichtm);
12228:
12229: strcpy(pathr,path);
12230: strcat(pathr,optionfilefiname);
1.184 brouard 12231: #ifdef WIN32
12232: _chdir(optionfilefiname); /* Move to directory named optionfile */
12233: #else
1.126 brouard 12234: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 12235: #endif
12236:
1.126 brouard 12237:
1.220 brouard 12238: /* Calculates basic frequencies. Computes observed prevalence at single age
12239: and for any valid combination of covariates
1.126 brouard 12240: and prints on file fileres'p'. */
1.251 brouard 12241: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.227 brouard 12242: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 12243:
12244: fprintf(fichtm,"\n");
1.286 brouard 12245: 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 12246: ftol, stepm);
12247: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
12248: ncurrv=1;
12249: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
12250: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
12251: ncurrv=i;
12252: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 12253: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
1.274 brouard 12254: ncurrv=i;
12255: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 12256: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
1.274 brouard 12257: ncurrv=i;
12258: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
12259: 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", \
12260: nlstate, ndeath, maxwav, mle, weightopt);
12261:
12262: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
12263: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
12264:
12265:
1.317 brouard 12266: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\
1.126 brouard 12267: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
12268: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.274 brouard 12269: imx,agemin,agemax,jmin,jmax,jmean);
1.126 brouard 12270: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.268 brouard 12271: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
12272: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
12273: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
12274: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 12275:
1.126 brouard 12276: /* For Powell, parameters are in a vector p[] starting at p[1]
12277: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
12278: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
12279:
12280: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 12281: /* For mortality only */
1.126 brouard 12282: if (mle==-3){
1.136 brouard 12283: ximort=matrix(1,NDIM,1,NDIM);
1.248 brouard 12284: for(i=1;i<=NDIM;i++)
12285: for(j=1;j<=NDIM;j++)
12286: ximort[i][j]=0.;
1.186 brouard 12287: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.290 brouard 12288: cens=ivector(firstobs,lastobs);
12289: ageexmed=vector(firstobs,lastobs);
12290: agecens=vector(firstobs,lastobs);
12291: dcwave=ivector(firstobs,lastobs);
1.223 brouard 12292:
1.126 brouard 12293: for (i=1; i<=imx; i++){
12294: dcwave[i]=-1;
12295: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 12296: if (s[m][i]>nlstate) {
12297: dcwave[i]=m;
12298: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
12299: break;
12300: }
1.126 brouard 12301: }
1.226 brouard 12302:
1.126 brouard 12303: for (i=1; i<=imx; i++) {
12304: if (wav[i]>0){
1.226 brouard 12305: ageexmed[i]=agev[mw[1][i]][i];
12306: j=wav[i];
12307: agecens[i]=1.;
12308:
12309: if (ageexmed[i]> 1 && wav[i] > 0){
12310: agecens[i]=agev[mw[j][i]][i];
12311: cens[i]= 1;
12312: }else if (ageexmed[i]< 1)
12313: cens[i]= -1;
12314: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
12315: cens[i]=0 ;
1.126 brouard 12316: }
12317: else cens[i]=-1;
12318: }
12319:
12320: for (i=1;i<=NDIM;i++) {
12321: for (j=1;j<=NDIM;j++)
1.226 brouard 12322: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 12323: }
12324:
1.302 brouard 12325: p[1]=0.0268; p[NDIM]=0.083;
12326: /* printf("%lf %lf", p[1], p[2]); */
1.126 brouard 12327:
12328:
1.136 brouard 12329: #ifdef GSL
12330: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 12331: #else
1.126 brouard 12332: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 12333: #endif
1.201 brouard 12334: strcpy(filerespow,"POW-MORT_");
12335: strcat(filerespow,fileresu);
1.126 brouard 12336: if((ficrespow=fopen(filerespow,"w"))==NULL) {
12337: printf("Problem with resultfile: %s\n", filerespow);
12338: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
12339: }
1.136 brouard 12340: #ifdef GSL
12341: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 12342: #else
1.126 brouard 12343: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 12344: #endif
1.126 brouard 12345: /* for (i=1;i<=nlstate;i++)
12346: for(j=1;j<=nlstate+ndeath;j++)
12347: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
12348: */
12349: fprintf(ficrespow,"\n");
1.136 brouard 12350: #ifdef GSL
12351: /* gsl starts here */
12352: T = gsl_multimin_fminimizer_nmsimplex;
12353: gsl_multimin_fminimizer *sfm = NULL;
12354: gsl_vector *ss, *x;
12355: gsl_multimin_function minex_func;
12356:
12357: /* Initial vertex size vector */
12358: ss = gsl_vector_alloc (NDIM);
12359:
12360: if (ss == NULL){
12361: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
12362: }
12363: /* Set all step sizes to 1 */
12364: gsl_vector_set_all (ss, 0.001);
12365:
12366: /* Starting point */
1.126 brouard 12367:
1.136 brouard 12368: x = gsl_vector_alloc (NDIM);
12369:
12370: if (x == NULL){
12371: gsl_vector_free(ss);
12372: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
12373: }
12374:
12375: /* Initialize method and iterate */
12376: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 12377: /* gsl_vector_set(x, 0, 0.0268); */
12378: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 12379: gsl_vector_set(x, 0, p[1]);
12380: gsl_vector_set(x, 1, p[2]);
12381:
12382: minex_func.f = &gompertz_f;
12383: minex_func.n = NDIM;
12384: minex_func.params = (void *)&p; /* ??? */
12385:
12386: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
12387: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
12388:
12389: printf("Iterations beginning .....\n\n");
12390: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
12391:
12392: iteri=0;
12393: while (rval == GSL_CONTINUE){
12394: iteri++;
12395: status = gsl_multimin_fminimizer_iterate(sfm);
12396:
12397: if (status) printf("error: %s\n", gsl_strerror (status));
12398: fflush(0);
12399:
12400: if (status)
12401: break;
12402:
12403: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
12404: ssval = gsl_multimin_fminimizer_size (sfm);
12405:
12406: if (rval == GSL_SUCCESS)
12407: printf ("converged to a local maximum at\n");
12408:
12409: printf("%5d ", iteri);
12410: for (it = 0; it < NDIM; it++){
12411: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
12412: }
12413: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
12414: }
12415:
12416: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
12417:
12418: gsl_vector_free(x); /* initial values */
12419: gsl_vector_free(ss); /* inital step size */
12420: for (it=0; it<NDIM; it++){
12421: p[it+1]=gsl_vector_get(sfm->x,it);
12422: fprintf(ficrespow," %.12lf", p[it]);
12423: }
12424: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
12425: #endif
12426: #ifdef POWELL
12427: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
12428: #endif
1.126 brouard 12429: fclose(ficrespow);
12430:
1.203 brouard 12431: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 12432:
12433: for(i=1; i <=NDIM; i++)
12434: for(j=i+1;j<=NDIM;j++)
1.220 brouard 12435: matcov[i][j]=matcov[j][i];
1.126 brouard 12436:
12437: printf("\nCovariance matrix\n ");
1.203 brouard 12438: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 12439: for(i=1; i <=NDIM; i++) {
12440: for(j=1;j<=NDIM;j++){
1.220 brouard 12441: printf("%f ",matcov[i][j]);
12442: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 12443: }
1.203 brouard 12444: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 12445: }
12446:
12447: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 12448: for (i=1;i<=NDIM;i++) {
1.126 brouard 12449: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 12450: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
12451: }
1.302 brouard 12452: lsurv=vector(agegomp,AGESUP);
12453: lpop=vector(agegomp,AGESUP);
12454: tpop=vector(agegomp,AGESUP);
1.126 brouard 12455: lsurv[agegomp]=100000;
12456:
12457: for (k=agegomp;k<=AGESUP;k++) {
12458: agemortsup=k;
12459: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
12460: }
12461:
12462: for (k=agegomp;k<agemortsup;k++)
12463: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
12464:
12465: for (k=agegomp;k<agemortsup;k++){
12466: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
12467: sumlpop=sumlpop+lpop[k];
12468: }
12469:
12470: tpop[agegomp]=sumlpop;
12471: for (k=agegomp;k<(agemortsup-3);k++){
12472: /* tpop[k+1]=2;*/
12473: tpop[k+1]=tpop[k]-lpop[k];
12474: }
12475:
12476:
12477: printf("\nAge lx qx dx Lx Tx e(x)\n");
12478: for (k=agegomp;k<(agemortsup-2);k++)
12479: 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]);
12480:
12481:
12482: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 12483: ageminpar=50;
12484: agemaxpar=100;
1.194 brouard 12485: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
12486: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12487: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12488: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
12489: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12490: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12491: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 12492: }else{
12493: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
12494: 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 12495: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 12496: }
1.201 brouard 12497: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 12498: stepm, weightopt,\
12499: model,imx,p,matcov,agemortsup);
12500:
1.302 brouard 12501: free_vector(lsurv,agegomp,AGESUP);
12502: free_vector(lpop,agegomp,AGESUP);
12503: free_vector(tpop,agegomp,AGESUP);
1.220 brouard 12504: free_matrix(ximort,1,NDIM,1,NDIM);
1.290 brouard 12505: free_ivector(dcwave,firstobs,lastobs);
12506: free_vector(agecens,firstobs,lastobs);
12507: free_vector(ageexmed,firstobs,lastobs);
12508: free_ivector(cens,firstobs,lastobs);
1.220 brouard 12509: #ifdef GSL
1.136 brouard 12510: #endif
1.186 brouard 12511: } /* Endof if mle==-3 mortality only */
1.205 brouard 12512: /* Standard */
12513: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
12514: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12515: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 12516: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 12517: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12518: for (k=1; k<=npar;k++)
12519: printf(" %d %8.5f",k,p[k]);
12520: printf("\n");
1.205 brouard 12521: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
12522: /* mlikeli uses func not funcone */
1.247 brouard 12523: /* for(i=1;i<nlstate;i++){ */
12524: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
12525: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
12526: /* } */
1.205 brouard 12527: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
12528: }
12529: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
12530: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12531: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
12532: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12533: }
12534: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 12535: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12536: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12537: for (k=1; k<=npar;k++)
12538: printf(" %d %8.5f",k,p[k]);
12539: printf("\n");
12540:
12541: /*--------- results files --------------*/
1.283 brouard 12542: /* 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 12543:
12544:
12545: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 12546: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); /* Printing model equation */
1.126 brouard 12547: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
1.319 brouard 12548:
12549: printf("#model= 1 + age ");
12550: fprintf(ficres,"#model= 1 + age ");
12551: fprintf(ficlog,"#model= 1 + age ");
12552: fprintf(fichtm,"\n<ul><li> model=1+age+%s\n \
12553: </ul>", model);
12554:
12555: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">\n");
12556: fprintf(fichtm, "<tr><th>Model=</th><th>1</th><th>+ age</th>");
12557: if(nagesqr==1){
12558: printf(" + age*age ");
12559: fprintf(ficres," + age*age ");
12560: fprintf(ficlog," + age*age ");
12561: fprintf(fichtm, "<th>+ age*age</th>");
12562: }
12563: for(j=1;j <=ncovmodel-2;j++){
12564: if(Typevar[j]==0) {
12565: printf(" + V%d ",Tvar[j]);
12566: fprintf(ficres," + V%d ",Tvar[j]);
12567: fprintf(ficlog," + V%d ",Tvar[j]);
12568: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
12569: }else if(Typevar[j]==1) {
12570: printf(" + V%d*age ",Tvar[j]);
12571: fprintf(ficres," + V%d*age ",Tvar[j]);
12572: fprintf(ficlog," + V%d*age ",Tvar[j]);
12573: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
12574: }else if(Typevar[j]==2) {
12575: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
12576: fprintf(ficres," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
12577: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
12578: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
12579: }
12580: }
12581: printf("\n");
12582: fprintf(ficres,"\n");
12583: fprintf(ficlog,"\n");
12584: fprintf(fichtm, "</tr>");
12585: fprintf(fichtm, "\n");
12586:
12587:
1.126 brouard 12588: for(i=1,jk=1; i <=nlstate; i++){
12589: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 12590: if (k != i) {
1.319 brouard 12591: fprintf(fichtm, "<tr>");
1.225 brouard 12592: printf("%d%d ",i,k);
12593: fprintf(ficlog,"%d%d ",i,k);
12594: fprintf(ficres,"%1d%1d ",i,k);
1.319 brouard 12595: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 12596: for(j=1; j <=ncovmodel; j++){
12597: printf("%12.7f ",p[jk]);
12598: fprintf(ficlog,"%12.7f ",p[jk]);
12599: fprintf(ficres,"%12.7f ",p[jk]);
1.319 brouard 12600: fprintf(fichtm, "<td>%12.7f</td>",p[jk]);
1.225 brouard 12601: jk++;
12602: }
12603: printf("\n");
12604: fprintf(ficlog,"\n");
12605: fprintf(ficres,"\n");
1.319 brouard 12606: fprintf(fichtm, "</tr>\n");
1.225 brouard 12607: }
1.126 brouard 12608: }
12609: }
1.319 brouard 12610: /* fprintf(fichtm,"</tr>\n"); */
12611: fprintf(fichtm,"</table>\n");
12612: fprintf(fichtm, "\n");
12613:
1.203 brouard 12614: if(mle != 0){
12615: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 12616: ftolhess=ftol; /* Usually correct */
1.203 brouard 12617: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
12618: 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");
12619: 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 12620: 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 12621: fprintf(fichtm,"\n<table style=\"text-align:center; border: 1px solid\">");
12622: fprintf(fichtm, "\n<tr><th>Model=</th><th>1</th><th>+ age</th>");
12623: if(nagesqr==1){
12624: printf(" + age*age ");
12625: fprintf(ficres," + age*age ");
12626: fprintf(ficlog," + age*age ");
12627: fprintf(fichtm, "<th>+ age*age</th>");
12628: }
12629: for(j=1;j <=ncovmodel-2;j++){
12630: if(Typevar[j]==0) {
12631: printf(" + V%d ",Tvar[j]);
12632: fprintf(fichtm, "<th>+ V%d</th>",Tvar[j]);
12633: }else if(Typevar[j]==1) {
12634: printf(" + V%d*age ",Tvar[j]);
12635: fprintf(fichtm, "<th>+ V%d*age</th>",Tvar[j]);
12636: }else if(Typevar[j]==2) {
12637: fprintf(fichtm, "<th>+ V%d*V%d</th>",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
12638: }
12639: }
12640: fprintf(fichtm, "</tr>\n");
12641:
1.203 brouard 12642: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 12643: for(k=1; k <=(nlstate+ndeath); k++){
12644: if (k != i) {
1.319 brouard 12645: fprintf(fichtm, "<tr valign=top>");
1.225 brouard 12646: printf("%d%d ",i,k);
12647: fprintf(ficlog,"%d%d ",i,k);
1.319 brouard 12648: fprintf(fichtm, "<td>%1d%1d</td>",i,k);
1.225 brouard 12649: for(j=1; j <=ncovmodel; j++){
1.319 brouard 12650: wald=p[jk]/sqrt(matcov[jk][jk]);
1.324 brouard 12651: 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]));
12652: 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 12653: if(fabs(wald) > 1.96){
1.321 brouard 12654: fprintf(fichtm, "<td><b>%12.7f</b></br> (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
1.319 brouard 12655: }else{
12656: fprintf(fichtm, "<td>%12.7f (%12.7f)</br>",p[jk],sqrt(matcov[jk][jk]));
12657: }
1.324 brouard 12658: fprintf(fichtm,"W=%8.3f</br>",wald);
1.319 brouard 12659: 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 12660: jk++;
12661: }
12662: printf("\n");
12663: fprintf(ficlog,"\n");
1.319 brouard 12664: fprintf(fichtm, "</tr>\n");
1.225 brouard 12665: }
12666: }
1.193 brouard 12667: }
1.203 brouard 12668: } /* end of hesscov and Wald tests */
1.319 brouard 12669: fprintf(fichtm,"</table>\n");
1.225 brouard 12670:
1.203 brouard 12671: /* */
1.126 brouard 12672: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
12673: printf("# Scales (for hessian or gradient estimation)\n");
12674: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
12675: for(i=1,jk=1; i <=nlstate; i++){
12676: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 12677: if (j!=i) {
12678: fprintf(ficres,"%1d%1d",i,j);
12679: printf("%1d%1d",i,j);
12680: fprintf(ficlog,"%1d%1d",i,j);
12681: for(k=1; k<=ncovmodel;k++){
12682: printf(" %.5e",delti[jk]);
12683: fprintf(ficlog," %.5e",delti[jk]);
12684: fprintf(ficres," %.5e",delti[jk]);
12685: jk++;
12686: }
12687: printf("\n");
12688: fprintf(ficlog,"\n");
12689: fprintf(ficres,"\n");
12690: }
1.126 brouard 12691: }
12692: }
12693:
12694: 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.203 brouard 12695: if(mle >= 1) /* To big for the screen */
1.126 brouard 12696: 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");
12697: 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");
12698: /* # 121 Var(a12)\n\ */
12699: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12700: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12701: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12702: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12703: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12704: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12705: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12706:
12707:
12708: /* Just to have a covariance matrix which will be more understandable
12709: even is we still don't want to manage dictionary of variables
12710: */
12711: for(itimes=1;itimes<=2;itimes++){
12712: jj=0;
12713: for(i=1; i <=nlstate; i++){
1.225 brouard 12714: for(j=1; j <=nlstate+ndeath; j++){
12715: if(j==i) continue;
12716: for(k=1; k<=ncovmodel;k++){
12717: jj++;
12718: ca[0]= k+'a'-1;ca[1]='\0';
12719: if(itimes==1){
12720: if(mle>=1)
12721: printf("#%1d%1d%d",i,j,k);
12722: fprintf(ficlog,"#%1d%1d%d",i,j,k);
12723: fprintf(ficres,"#%1d%1d%d",i,j,k);
12724: }else{
12725: if(mle>=1)
12726: printf("%1d%1d%d",i,j,k);
12727: fprintf(ficlog,"%1d%1d%d",i,j,k);
12728: fprintf(ficres,"%1d%1d%d",i,j,k);
12729: }
12730: ll=0;
12731: for(li=1;li <=nlstate; li++){
12732: for(lj=1;lj <=nlstate+ndeath; lj++){
12733: if(lj==li) continue;
12734: for(lk=1;lk<=ncovmodel;lk++){
12735: ll++;
12736: if(ll<=jj){
12737: cb[0]= lk +'a'-1;cb[1]='\0';
12738: if(ll<jj){
12739: if(itimes==1){
12740: if(mle>=1)
12741: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12742: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12743: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12744: }else{
12745: if(mle>=1)
12746: printf(" %.5e",matcov[jj][ll]);
12747: fprintf(ficlog," %.5e",matcov[jj][ll]);
12748: fprintf(ficres," %.5e",matcov[jj][ll]);
12749: }
12750: }else{
12751: if(itimes==1){
12752: if(mle>=1)
12753: printf(" Var(%s%1d%1d)",ca,i,j);
12754: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
12755: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
12756: }else{
12757: if(mle>=1)
12758: printf(" %.7e",matcov[jj][ll]);
12759: fprintf(ficlog," %.7e",matcov[jj][ll]);
12760: fprintf(ficres," %.7e",matcov[jj][ll]);
12761: }
12762: }
12763: }
12764: } /* end lk */
12765: } /* end lj */
12766: } /* end li */
12767: if(mle>=1)
12768: printf("\n");
12769: fprintf(ficlog,"\n");
12770: fprintf(ficres,"\n");
12771: numlinepar++;
12772: } /* end k*/
12773: } /*end j */
1.126 brouard 12774: } /* end i */
12775: } /* end itimes */
12776:
12777: fflush(ficlog);
12778: fflush(ficres);
1.225 brouard 12779: while(fgets(line, MAXLINE, ficpar)) {
12780: /* If line starts with a # it is a comment */
12781: if (line[0] == '#') {
12782: numlinepar++;
12783: fputs(line,stdout);
12784: fputs(line,ficparo);
12785: fputs(line,ficlog);
1.299 brouard 12786: fputs(line,ficres);
1.225 brouard 12787: continue;
12788: }else
12789: break;
12790: }
12791:
1.209 brouard 12792: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
12793: /* ungetc(c,ficpar); */
12794: /* fgets(line, MAXLINE, ficpar); */
12795: /* fputs(line,stdout); */
12796: /* fputs(line,ficparo); */
12797: /* } */
12798: /* ungetc(c,ficpar); */
1.126 brouard 12799:
12800: estepm=0;
1.209 brouard 12801: 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 12802:
12803: if (num_filled != 6) {
12804: 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);
12805: 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);
12806: goto end;
12807: }
12808: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
12809: }
12810: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
12811: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
12812:
1.209 brouard 12813: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 12814: if (estepm==0 || estepm < stepm) estepm=stepm;
12815: if (fage <= 2) {
12816: bage = ageminpar;
12817: fage = agemaxpar;
12818: }
12819:
12820: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 12821: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
12822: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 12823:
1.186 brouard 12824: /* Other stuffs, more or less useful */
1.254 brouard 12825: while(fgets(line, MAXLINE, ficpar)) {
12826: /* If line starts with a # it is a comment */
12827: if (line[0] == '#') {
12828: numlinepar++;
12829: fputs(line,stdout);
12830: fputs(line,ficparo);
12831: fputs(line,ficlog);
1.299 brouard 12832: fputs(line,ficres);
1.254 brouard 12833: continue;
12834: }else
12835: break;
12836: }
12837:
12838: 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){
12839:
12840: if (num_filled != 7) {
12841: 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);
12842: 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);
12843: goto end;
12844: }
12845: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
12846: 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);
12847: 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);
12848: 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 12849: }
1.254 brouard 12850:
12851: while(fgets(line, MAXLINE, ficpar)) {
12852: /* If line starts with a # it is a comment */
12853: if (line[0] == '#') {
12854: numlinepar++;
12855: fputs(line,stdout);
12856: fputs(line,ficparo);
12857: fputs(line,ficlog);
1.299 brouard 12858: fputs(line,ficres);
1.254 brouard 12859: continue;
12860: }else
12861: break;
1.126 brouard 12862: }
12863:
12864:
12865: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
12866: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
12867:
1.254 brouard 12868: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
12869: if (num_filled != 1) {
12870: 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);
12871: 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);
12872: goto end;
12873: }
12874: printf("pop_based=%d\n",popbased);
12875: fprintf(ficlog,"pop_based=%d\n",popbased);
12876: fprintf(ficparo,"pop_based=%d\n",popbased);
12877: fprintf(ficres,"pop_based=%d\n",popbased);
12878: }
12879:
1.258 brouard 12880: /* Results */
1.307 brouard 12881: endishere=0;
1.258 brouard 12882: nresult=0;
1.308 brouard 12883: parameterline=0;
1.258 brouard 12884: do{
12885: if(!fgets(line, MAXLINE, ficpar)){
12886: endishere=1;
1.308 brouard 12887: parameterline=15;
1.258 brouard 12888: }else if (line[0] == '#') {
12889: /* If line starts with a # it is a comment */
1.254 brouard 12890: numlinepar++;
12891: fputs(line,stdout);
12892: fputs(line,ficparo);
12893: fputs(line,ficlog);
1.299 brouard 12894: fputs(line,ficres);
1.254 brouard 12895: continue;
1.258 brouard 12896: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
12897: parameterline=11;
1.296 brouard 12898: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
1.258 brouard 12899: parameterline=12;
1.307 brouard 12900: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
1.258 brouard 12901: parameterline=13;
1.307 brouard 12902: }
1.258 brouard 12903: else{
12904: parameterline=14;
1.254 brouard 12905: }
1.308 brouard 12906: switch (parameterline){ /* =0 only if only comments */
1.258 brouard 12907: case 11:
1.296 brouard 12908: 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)){
12909: 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 12910: 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);
12911: 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);
12912: 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);
12913: /* day and month of proj2 are not used but only year anproj2.*/
1.273 brouard 12914: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
12915: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
1.296 brouard 12916: prvforecast = 1;
12917: }
12918: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
1.313 brouard 12919: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12920: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12921: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
1.296 brouard 12922: prvforecast = 2;
12923: }
12924: else {
12925: 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);
12926: 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);
12927: goto end;
1.258 brouard 12928: }
1.254 brouard 12929: break;
1.258 brouard 12930: case 12:
1.296 brouard 12931: 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)){
12932: 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);
12933: 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);
12934: 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);
12935: 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);
12936: /* day and month of back2 are not used but only year anback2.*/
1.273 brouard 12937: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
12938: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
1.296 brouard 12939: prvbackcast = 1;
12940: }
12941: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
1.313 brouard 12942: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12943: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12944: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
1.296 brouard 12945: prvbackcast = 2;
12946: }
12947: else {
12948: 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);
12949: 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);
12950: goto end;
1.258 brouard 12951: }
1.230 brouard 12952: break;
1.258 brouard 12953: case 13:
1.307 brouard 12954: num_filled=sscanf(line,"result:%[^\n]\n",resultline);
12955: nresult++; /* Sum of resultlines */
12956: printf("Result %d: result:%s\n",nresult, resultline);
1.318 brouard 12957: if(nresult > MAXRESULTLINESPONE-1){
12958: 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);
12959: 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 12960: goto end;
12961: }
1.310 brouard 12962: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.314 brouard 12963: fprintf(ficparo,"result: %s\n",resultline);
12964: fprintf(ficres,"result: %s\n",resultline);
12965: fprintf(ficlog,"result: %s\n",resultline);
1.310 brouard 12966: } else
12967: goto end;
1.307 brouard 12968: break;
12969: case 14:
12970: printf("Error: Unknown command '%s'\n",line);
12971: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
1.314 brouard 12972: if(line[0] == ' ' || line[0] == '\n'){
12973: printf("It should not be an empty line '%s'\n",line);
12974: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
12975: }
1.307 brouard 12976: if(ncovmodel >=2 && nresult==0 ){
12977: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
12978: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
1.258 brouard 12979: }
1.307 brouard 12980: /* goto end; */
12981: break;
1.308 brouard 12982: case 15:
12983: printf("End of resultlines.\n");
12984: fprintf(ficlog,"End of resultlines.\n");
12985: break;
12986: default: /* parameterline =0 */
1.307 brouard 12987: nresult=1;
12988: decoderesult(".",nresult ); /* No covariate */
1.258 brouard 12989: } /* End switch parameterline */
12990: }while(endishere==0); /* End do */
1.126 brouard 12991:
1.230 brouard 12992: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 12993: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 12994:
12995: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 12996: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 12997: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 12998: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12999: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 13000: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 13001: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
13002: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 13003: }else{
1.270 brouard 13004: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
1.296 brouard 13005: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
13006: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
13007: if(prvforecast==1){
13008: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
13009: jprojd=jproj1;
13010: mprojd=mproj1;
13011: anprojd=anproj1;
13012: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
13013: jprojf=jproj2;
13014: mprojf=mproj2;
13015: anprojf=anproj2;
13016: } else if(prvforecast == 2){
13017: dateprojd=dateintmean;
13018: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
13019: dateprojf=dateintmean+yrfproj;
13020: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
13021: }
13022: if(prvbackcast==1){
13023: datebackd=(jback1+12*mback1+365*anback1)/365;
13024: jbackd=jback1;
13025: mbackd=mback1;
13026: anbackd=anback1;
13027: datebackf=(jback2+12*mback2+365*anback2)/365;
13028: jbackf=jback2;
13029: mbackf=mback2;
13030: anbackf=anback2;
13031: } else if(prvbackcast == 2){
13032: datebackd=dateintmean;
13033: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
13034: datebackf=dateintmean-yrbproj;
13035: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
13036: }
13037:
13038: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);
1.220 brouard 13039: }
13040: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.296 brouard 13041: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
13042: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
1.220 brouard 13043:
1.225 brouard 13044: /*------------ free_vector -------------*/
13045: /* chdir(path); */
1.220 brouard 13046:
1.215 brouard 13047: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
13048: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
13049: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
13050: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.290 brouard 13051: free_lvector(num,firstobs,lastobs);
13052: free_vector(agedc,firstobs,lastobs);
1.126 brouard 13053: /*free_matrix(covar,0,NCOVMAX,1,n);*/
13054: /*free_matrix(covar,1,NCOVMAX,1,n);*/
13055: fclose(ficparo);
13056: fclose(ficres);
1.220 brouard 13057:
13058:
1.186 brouard 13059: /* Other results (useful)*/
1.220 brouard 13060:
13061:
1.126 brouard 13062: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 13063: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
13064: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 13065: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 13066: fclose(ficrespl);
13067:
13068: /*------------- h Pij x at various ages ------------*/
1.180 brouard 13069: /*#include "hpijx.h"*/
13070: hPijx(p, bage, fage);
1.145 brouard 13071: fclose(ficrespij);
1.227 brouard 13072:
1.220 brouard 13073: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 13074: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 13075: k=1;
1.126 brouard 13076: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 13077:
1.269 brouard 13078: /* Prevalence for each covariate combination in probs[age][status][cov] */
13079: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
13080: for(i=AGEINF;i<=AGESUP;i++)
1.219 brouard 13081: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 13082: for(k=1;k<=ncovcombmax;k++)
13083: probs[i][j][k]=0.;
1.269 brouard 13084: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
13085: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.219 brouard 13086: if (mobilav!=0 ||mobilavproj !=0 ) {
1.269 brouard 13087: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
13088: for(i=AGEINF;i<=AGESUP;i++)
1.268 brouard 13089: for(j=1;j<=nlstate+ndeath;j++)
1.227 brouard 13090: for(k=1;k<=ncovcombmax;k++)
13091: mobaverages[i][j][k]=0.;
1.219 brouard 13092: mobaverage=mobaverages;
13093: if (mobilav!=0) {
1.235 brouard 13094: printf("Movingaveraging observed prevalence\n");
1.258 brouard 13095: fprintf(ficlog,"Movingaveraging observed prevalence\n");
1.227 brouard 13096: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
13097: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
13098: printf(" Error in movingaverage mobilav=%d\n",mobilav);
13099: }
1.269 brouard 13100: } else if (mobilavproj !=0) {
1.235 brouard 13101: printf("Movingaveraging projected observed prevalence\n");
1.258 brouard 13102: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
1.227 brouard 13103: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
13104: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
13105: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
13106: }
1.269 brouard 13107: }else{
13108: printf("Internal error moving average\n");
13109: fflush(stdout);
13110: exit(1);
1.219 brouard 13111: }
13112: }/* end if moving average */
1.227 brouard 13113:
1.126 brouard 13114: /*---------- Forecasting ------------------*/
1.296 brouard 13115: if(prevfcast==1){
13116: /* /\* if(stepm ==1){*\/ */
13117: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
13118: /*This done previously after freqsummary.*/
13119: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
13120: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
13121:
13122: /* } else if (prvforecast==2){ */
13123: /* /\* if(stepm ==1){*\/ */
13124: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
13125: /* } */
13126: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
13127: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
1.126 brouard 13128: }
1.269 brouard 13129:
1.296 brouard 13130: /* Prevbcasting */
13131: if(prevbcast==1){
1.219 brouard 13132: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
13133: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
13134: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
13135:
13136: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
13137:
13138: bprlim=matrix(1,nlstate,1,nlstate);
1.269 brouard 13139:
1.219 brouard 13140: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
13141: fclose(ficresplb);
13142:
1.222 brouard 13143: hBijx(p, bage, fage, mobaverage);
13144: fclose(ficrespijb);
1.219 brouard 13145:
1.296 brouard 13146: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
13147: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
13148: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
13149: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
13150: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
13151: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
13152:
13153:
1.269 brouard 13154: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 13155:
13156:
1.269 brouard 13157: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.219 brouard 13158: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
13159: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
13160: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
1.296 brouard 13161: } /* end Prevbcasting */
1.268 brouard 13162:
1.186 brouard 13163:
13164: /* ------ Other prevalence ratios------------ */
1.126 brouard 13165:
1.215 brouard 13166: free_ivector(wav,1,imx);
13167: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
13168: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
13169: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 13170:
13171:
1.127 brouard 13172: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 13173:
1.201 brouard 13174: strcpy(filerese,"E_");
13175: strcat(filerese,fileresu);
1.126 brouard 13176: if((ficreseij=fopen(filerese,"w"))==NULL) {
13177: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
13178: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
13179: }
1.208 brouard 13180: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
13181: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 13182:
13183: pstamp(ficreseij);
1.219 brouard 13184:
1.235 brouard 13185: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
13186: if (cptcovn < 1){i1=1;}
13187:
13188: for(nres=1; nres <= nresult; nres++) /* For each resultline */
13189: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 13190: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 13191: continue;
1.219 brouard 13192: fprintf(ficreseij,"\n#****** ");
1.235 brouard 13193: printf("\n#****** ");
1.225 brouard 13194: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 13195: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 13196: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
13197: }
13198: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
13199: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
13200: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
1.219 brouard 13201: }
13202: fprintf(ficreseij,"******\n");
1.235 brouard 13203: printf("******\n");
1.219 brouard 13204:
13205: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
13206: oldm=oldms;savm=savms;
1.235 brouard 13207: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 13208:
1.219 brouard 13209: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 13210: }
13211: fclose(ficreseij);
1.208 brouard 13212: printf("done evsij\n");fflush(stdout);
13213: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.269 brouard 13214:
1.218 brouard 13215:
1.227 brouard 13216: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 13217:
1.201 brouard 13218: strcpy(filerest,"T_");
13219: strcat(filerest,fileresu);
1.127 brouard 13220: if((ficrest=fopen(filerest,"w"))==NULL) {
13221: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
13222: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
13223: }
1.208 brouard 13224: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
13225: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.201 brouard 13226: strcpy(fileresstde,"STDE_");
13227: strcat(fileresstde,fileresu);
1.126 brouard 13228: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 13229: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
13230: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 13231: }
1.227 brouard 13232: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
13233: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 13234:
1.201 brouard 13235: strcpy(filerescve,"CVE_");
13236: strcat(filerescve,fileresu);
1.126 brouard 13237: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 13238: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
13239: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 13240: }
1.227 brouard 13241: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
13242: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 13243:
1.201 brouard 13244: strcpy(fileresv,"V_");
13245: strcat(fileresv,fileresu);
1.126 brouard 13246: if((ficresvij=fopen(fileresv,"w"))==NULL) {
13247: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
13248: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
13249: }
1.227 brouard 13250: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
13251: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 13252:
1.235 brouard 13253: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
13254: if (cptcovn < 1){i1=1;}
13255:
13256: for(nres=1; nres <= nresult; nres++) /* For each resultline */
13257: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 13258: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 13259: continue;
1.321 brouard 13260: printf("\n# model %s \n#****** Result for:", model);
13261: fprintf(ficrest,"\n# model %s \n#****** Result for:", model);
13262: fprintf(ficlog,"\n# model %s \n#****** Result for:", model);
1.227 brouard 13263: for(j=1;j<=cptcoveff;j++){
13264: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
13265: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
13266: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
13267: }
1.235 brouard 13268: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
13269: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
13270: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
13271: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
13272: }
1.208 brouard 13273: fprintf(ficrest,"******\n");
1.227 brouard 13274: fprintf(ficlog,"******\n");
13275: printf("******\n");
1.208 brouard 13276:
13277: fprintf(ficresstdeij,"\n#****** ");
13278: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 13279: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 13280: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
13281: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 13282: }
1.235 brouard 13283: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
13284: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
13285: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
13286: }
1.208 brouard 13287: fprintf(ficresstdeij,"******\n");
13288: fprintf(ficrescveij,"******\n");
13289:
13290: fprintf(ficresvij,"\n#****** ");
1.238 brouard 13291: /* pstamp(ficresvij); */
1.225 brouard 13292: for(j=1;j<=cptcoveff;j++)
1.227 brouard 13293: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 13294: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
13295: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
13296: }
1.208 brouard 13297: fprintf(ficresvij,"******\n");
13298:
13299: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
13300: oldm=oldms;savm=savms;
1.235 brouard 13301: printf(" cvevsij ");
13302: fprintf(ficlog, " cvevsij ");
13303: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 13304: printf(" end cvevsij \n ");
13305: fprintf(ficlog, " end cvevsij \n ");
13306:
13307: /*
13308: */
13309: /* goto endfree; */
13310:
13311: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
13312: pstamp(ficrest);
13313:
1.269 brouard 13314: epj=vector(1,nlstate+1);
1.208 brouard 13315: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 13316: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
13317: cptcod= 0; /* To be deleted */
13318: printf("varevsij vpopbased=%d \n",vpopbased);
13319: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 13320: 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 13321: 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 ");
13322: if(vpopbased==1)
13323: 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);
13324: else
1.288 brouard 13325: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 13326: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
13327: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
13328: fprintf(ficrest,"\n");
13329: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
1.288 brouard 13330: printf("Computing age specific forward period (stable) prevalences in each health state \n");
13331: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 13332: for(age=bage; age <=fage ;age++){
1.235 brouard 13333: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 13334: if (vpopbased==1) {
13335: if(mobilav ==0){
13336: for(i=1; i<=nlstate;i++)
13337: prlim[i][i]=probs[(int)age][i][k];
13338: }else{ /* mobilav */
13339: for(i=1; i<=nlstate;i++)
13340: prlim[i][i]=mobaverage[(int)age][i][k];
13341: }
13342: }
1.219 brouard 13343:
1.227 brouard 13344: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
13345: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
13346: /* printf(" age %4.0f ",age); */
13347: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
13348: for(i=1, epj[j]=0.;i <=nlstate;i++) {
13349: epj[j] += prlim[i][i]*eij[i][j][(int)age];
13350: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
13351: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
13352: }
13353: epj[nlstate+1] +=epj[j];
13354: }
13355: /* printf(" age %4.0f \n",age); */
1.219 brouard 13356:
1.227 brouard 13357: for(i=1, vepp=0.;i <=nlstate;i++)
13358: for(j=1;j <=nlstate;j++)
13359: vepp += vareij[i][j][(int)age];
13360: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
13361: for(j=1;j <=nlstate;j++){
13362: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
13363: }
13364: fprintf(ficrest,"\n");
13365: }
1.208 brouard 13366: } /* End vpopbased */
1.269 brouard 13367: free_vector(epj,1,nlstate+1);
1.208 brouard 13368: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
13369: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.235 brouard 13370: printf("done selection\n");fflush(stdout);
13371: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 13372:
1.235 brouard 13373: } /* End k selection */
1.227 brouard 13374:
13375: printf("done State-specific expectancies\n");fflush(stdout);
13376: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
13377:
1.288 brouard 13378: /* variance-covariance of forward period prevalence*/
1.269 brouard 13379: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 13380:
1.227 brouard 13381:
1.290 brouard 13382: free_vector(weight,firstobs,lastobs);
1.227 brouard 13383: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.290 brouard 13384: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
13385: free_matrix(anint,1,maxwav,firstobs,lastobs);
13386: free_matrix(mint,1,maxwav,firstobs,lastobs);
13387: free_ivector(cod,firstobs,lastobs);
1.227 brouard 13388: free_ivector(tab,1,NCOVMAX);
13389: fclose(ficresstdeij);
13390: fclose(ficrescveij);
13391: fclose(ficresvij);
13392: fclose(ficrest);
13393: fclose(ficpar);
13394:
13395:
1.126 brouard 13396: /*---------- End : free ----------------*/
1.219 brouard 13397: if (mobilav!=0 ||mobilavproj !=0)
1.269 brouard 13398: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
13399: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 13400: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
13401: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 13402: } /* mle==-3 arrives here for freeing */
1.227 brouard 13403: /* endfree:*/
13404: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
13405: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
13406: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.290 brouard 13407: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs);
13408: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
13409: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
13410: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
1.227 brouard 13411: free_matrix(matcov,1,npar,1,npar);
13412: free_matrix(hess,1,npar,1,npar);
13413: /*free_vector(delti,1,npar);*/
13414: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
13415: free_matrix(agev,1,maxwav,1,imx);
1.269 brouard 13416: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.227 brouard 13417: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
13418:
13419: free_ivector(ncodemax,1,NCOVMAX);
13420: free_ivector(ncodemaxwundef,1,NCOVMAX);
13421: free_ivector(Dummy,-1,NCOVMAX);
13422: free_ivector(Fixed,-1,NCOVMAX);
1.238 brouard 13423: free_ivector(DummyV,1,NCOVMAX);
13424: free_ivector(FixedV,1,NCOVMAX);
1.227 brouard 13425: free_ivector(Typevar,-1,NCOVMAX);
13426: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 13427: free_ivector(TvarsQ,1,NCOVMAX);
13428: free_ivector(TvarsQind,1,NCOVMAX);
13429: free_ivector(TvarsD,1,NCOVMAX);
13430: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 13431: free_ivector(TvarFD,1,NCOVMAX);
13432: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 13433: free_ivector(TvarF,1,NCOVMAX);
13434: free_ivector(TvarFind,1,NCOVMAX);
13435: free_ivector(TvarV,1,NCOVMAX);
13436: free_ivector(TvarVind,1,NCOVMAX);
13437: free_ivector(TvarA,1,NCOVMAX);
13438: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 13439: free_ivector(TvarFQ,1,NCOVMAX);
13440: free_ivector(TvarFQind,1,NCOVMAX);
13441: free_ivector(TvarVD,1,NCOVMAX);
13442: free_ivector(TvarVDind,1,NCOVMAX);
13443: free_ivector(TvarVQ,1,NCOVMAX);
13444: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 13445: free_ivector(Tvarsel,1,NCOVMAX);
13446: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 13447: free_ivector(Tposprod,1,NCOVMAX);
13448: free_ivector(Tprod,1,NCOVMAX);
13449: free_ivector(Tvaraff,1,NCOVMAX);
13450: free_ivector(invalidvarcomb,1,ncovcombmax);
13451: free_ivector(Tage,1,NCOVMAX);
13452: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 13453: free_ivector(TmodelInvind,1,NCOVMAX);
13454: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 13455:
13456: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
13457: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 13458: fflush(fichtm);
13459: fflush(ficgp);
13460:
1.227 brouard 13461:
1.126 brouard 13462: if((nberr >0) || (nbwarn>0)){
1.216 brouard 13463: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
13464: 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 13465: }else{
13466: printf("End of Imach\n");
13467: fprintf(ficlog,"End of Imach\n");
13468: }
13469: printf("See log file on %s\n",filelog);
13470: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 13471: /*(void) gettimeofday(&end_time,&tzp);*/
13472: rend_time = time(NULL);
13473: end_time = *localtime(&rend_time);
13474: /* tml = *localtime(&end_time.tm_sec); */
13475: strcpy(strtend,asctime(&end_time));
1.126 brouard 13476: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
13477: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 13478: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 13479:
1.157 brouard 13480: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
13481: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
13482: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 13483: /* printf("Total time was %d uSec.\n", total_usecs);*/
13484: /* if(fileappend(fichtm,optionfilehtm)){ */
13485: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13486: fclose(fichtm);
13487: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13488: fclose(fichtmcov);
13489: fclose(ficgp);
13490: fclose(ficlog);
13491: /*------ End -----------*/
1.227 brouard 13492:
1.281 brouard 13493:
13494: /* Executes gnuplot */
1.227 brouard 13495:
13496: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 13497: #ifdef WIN32
1.227 brouard 13498: if (_chdir(pathcd) != 0)
13499: printf("Can't move to directory %s!\n",path);
13500: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 13501: #else
1.227 brouard 13502: if(chdir(pathcd) != 0)
13503: printf("Can't move to directory %s!\n", path);
13504: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 13505: #endif
1.126 brouard 13506: printf("Current directory %s!\n",pathcd);
13507: /*strcat(plotcmd,CHARSEPARATOR);*/
13508: sprintf(plotcmd,"gnuplot");
1.157 brouard 13509: #ifdef _WIN32
1.126 brouard 13510: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
13511: #endif
13512: if(!stat(plotcmd,&info)){
1.158 brouard 13513: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13514: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 13515: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 13516: }else
13517: strcpy(pplotcmd,plotcmd);
1.157 brouard 13518: #ifdef __unix
1.126 brouard 13519: strcpy(plotcmd,GNUPLOTPROGRAM);
13520: if(!stat(plotcmd,&info)){
1.158 brouard 13521: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13522: }else
13523: strcpy(pplotcmd,plotcmd);
13524: #endif
13525: }else
13526: strcpy(pplotcmd,plotcmd);
13527:
13528: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 13529: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.292 brouard 13530: strcpy(pplotcmd,plotcmd);
1.227 brouard 13531:
1.126 brouard 13532: if((outcmd=system(plotcmd)) != 0){
1.292 brouard 13533: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 13534: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 13535: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.292 brouard 13536: if((outcmd=system(plotcmd)) != 0){
1.153 brouard 13537: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.292 brouard 13538: strcpy(plotcmd,pplotcmd);
13539: }
1.126 brouard 13540: }
1.158 brouard 13541: printf(" Successful, please wait...");
1.126 brouard 13542: while (z[0] != 'q') {
13543: /* chdir(path); */
1.154 brouard 13544: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 13545: scanf("%s",z);
13546: /* if (z[0] == 'c') system("./imach"); */
13547: if (z[0] == 'e') {
1.158 brouard 13548: #ifdef __APPLE__
1.152 brouard 13549: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 13550: #elif __linux
13551: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 13552: #else
1.152 brouard 13553: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 13554: #endif
13555: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
13556: system(pplotcmd);
1.126 brouard 13557: }
13558: else if (z[0] == 'g') system(plotcmd);
13559: else if (z[0] == 'q') exit(0);
13560: }
1.227 brouard 13561: end:
1.126 brouard 13562: while (z[0] != 'q') {
1.195 brouard 13563: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 13564: scanf("%s",z);
13565: }
1.283 brouard 13566: printf("End\n");
1.282 brouard 13567: exit(0);
1.126 brouard 13568: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>