Annotation of imach/src/imach.c, revision 1.315
1.315 ! brouard 1: /* $Id: imach.c,v 1.314 2022/04/13 17:43:09 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.315 ! brouard 4: Revision 1.314 2022/04/13 17:43:09 brouard
! 5: * imach.c (Module): Adding link to text data files
! 6:
1.314 brouard 7: Revision 1.313 2022/04/11 15:57:42 brouard
8: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed
9:
1.313 brouard 10: Revision 1.312 2022/04/05 21:24:39 brouard
11: *** empty log message ***
12:
1.312 brouard 13: Revision 1.311 2022/04/05 21:03:51 brouard
14: Summary: Fixed quantitative covariates
15:
16: Fixed covariates (dummy or quantitative)
17: with missing values have never been allowed but are ERRORS and
18: program quits. Standard deviations of fixed covariates were
19: wrongly computed. Mean and standard deviations of time varying
20: covariates are still not computed.
21:
1.311 brouard 22: Revision 1.310 2022/03/17 08:45:53 brouard
23: Summary: 99r25
24:
25: Improving detection of errors: result lines should be compatible with
26: the model.
27:
1.310 brouard 28: Revision 1.309 2021/05/20 12:39:14 brouard
29: Summary: Version 0.99r24
30:
1.309 brouard 31: Revision 1.308 2021/03/31 13:11:57 brouard
32: Summary: Version 0.99r23
33:
34:
35: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
36:
1.308 brouard 37: Revision 1.307 2021/03/08 18:11:32 brouard
38: Summary: 0.99r22 fixed bug on result:
39:
1.307 brouard 40: Revision 1.306 2021/02/20 15:44:02 brouard
41: Summary: Version 0.99r21
42:
43: * imach.c (Module): Fix bug on quitting after result lines!
44: (Module): Version 0.99r21
45:
1.306 brouard 46: Revision 1.305 2021/02/20 15:28:30 brouard
47: * imach.c (Module): Fix bug on quitting after result lines!
48:
1.305 brouard 49: Revision 1.304 2021/02/12 11:34:20 brouard
50: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
51:
1.304 brouard 52: Revision 1.303 2021/02/11 19:50:15 brouard
53: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
54:
1.303 brouard 55: Revision 1.302 2020/02/22 21:00:05 brouard
56: * (Module): imach.c Update mle=-3 (for computing Life expectancy
57: and life table from the data without any state)
58:
1.302 brouard 59: Revision 1.301 2019/06/04 13:51:20 brouard
60: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
61:
1.301 brouard 62: Revision 1.300 2019/05/22 19:09:45 brouard
63: Summary: version 0.99r19 of May 2019
64:
1.300 brouard 65: Revision 1.299 2019/05/22 18:37:08 brouard
66: Summary: Cleaned 0.99r19
67:
1.299 brouard 68: Revision 1.298 2019/05/22 18:19:56 brouard
69: *** empty log message ***
70:
1.298 brouard 71: Revision 1.297 2019/05/22 17:56:10 brouard
72: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
73:
1.297 brouard 74: Revision 1.296 2019/05/20 13:03:18 brouard
75: Summary: Projection syntax simplified
76:
77:
78: We can now start projections, forward or backward, from the mean date
79: of inteviews up to or down to a number of years of projection:
80: prevforecast=1 yearsfproj=15.3 mobil_average=0
81: or
82: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
83: or
84: prevbackcast=1 yearsbproj=12.3 mobil_average=1
85: or
86: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
87:
1.296 brouard 88: Revision 1.295 2019/05/18 09:52:50 brouard
89: Summary: doxygen tex bug
90:
1.295 brouard 91: Revision 1.294 2019/05/16 14:54:33 brouard
92: Summary: There was some wrong lines added
93:
1.294 brouard 94: Revision 1.293 2019/05/09 15:17:34 brouard
95: *** empty log message ***
96:
1.293 brouard 97: Revision 1.292 2019/05/09 14:17:20 brouard
98: Summary: Some updates
99:
1.292 brouard 100: Revision 1.291 2019/05/09 13:44:18 brouard
101: Summary: Before ncovmax
102:
1.291 brouard 103: Revision 1.290 2019/05/09 13:39:37 brouard
104: Summary: 0.99r18 unlimited number of individuals
105:
106: 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.
107:
1.290 brouard 108: Revision 1.289 2018/12/13 09:16:26 brouard
109: Summary: Bug for young ages (<-30) will be in r17
110:
1.289 brouard 111: Revision 1.288 2018/05/02 20:58:27 brouard
112: Summary: Some bugs fixed
113:
1.288 brouard 114: Revision 1.287 2018/05/01 17:57:25 brouard
115: Summary: Bug fixed by providing frequencies only for non missing covariates
116:
1.287 brouard 117: Revision 1.286 2018/04/27 14:27:04 brouard
118: Summary: some minor bugs
119:
1.286 brouard 120: Revision 1.285 2018/04/21 21:02:16 brouard
121: Summary: Some bugs fixed, valgrind tested
122:
1.285 brouard 123: Revision 1.284 2018/04/20 05:22:13 brouard
124: Summary: Computing mean and stdeviation of fixed quantitative variables
125:
1.284 brouard 126: Revision 1.283 2018/04/19 14:49:16 brouard
127: Summary: Some minor bugs fixed
128:
1.283 brouard 129: Revision 1.282 2018/02/27 22:50:02 brouard
130: *** empty log message ***
131:
1.282 brouard 132: Revision 1.281 2018/02/27 19:25:23 brouard
133: Summary: Adding second argument for quitting
134:
1.281 brouard 135: Revision 1.280 2018/02/21 07:58:13 brouard
136: Summary: 0.99r15
137:
138: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
139:
1.280 brouard 140: Revision 1.279 2017/07/20 13:35:01 brouard
141: Summary: temporary working
142:
1.279 brouard 143: Revision 1.278 2017/07/19 14:09:02 brouard
144: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
145:
1.278 brouard 146: Revision 1.277 2017/07/17 08:53:49 brouard
147: Summary: BOM files can be read now
148:
1.277 brouard 149: Revision 1.276 2017/06/30 15:48:31 brouard
150: Summary: Graphs improvements
151:
1.276 brouard 152: Revision 1.275 2017/06/30 13:39:33 brouard
153: Summary: Saito's color
154:
1.275 brouard 155: Revision 1.274 2017/06/29 09:47:08 brouard
156: Summary: Version 0.99r14
157:
1.274 brouard 158: Revision 1.273 2017/06/27 11:06:02 brouard
159: Summary: More documentation on projections
160:
1.273 brouard 161: Revision 1.272 2017/06/27 10:22:40 brouard
162: Summary: Color of backprojection changed from 6 to 5(yellow)
163:
1.272 brouard 164: Revision 1.271 2017/06/27 10:17:50 brouard
165: Summary: Some bug with rint
166:
1.271 brouard 167: Revision 1.270 2017/05/24 05:45:29 brouard
168: *** empty log message ***
169:
1.270 brouard 170: Revision 1.269 2017/05/23 08:39:25 brouard
171: Summary: Code into subroutine, cleanings
172:
1.269 brouard 173: Revision 1.268 2017/05/18 20:09:32 brouard
174: Summary: backprojection and confidence intervals of backprevalence
175:
1.268 brouard 176: Revision 1.267 2017/05/13 10:25:05 brouard
177: Summary: temporary save for backprojection
178:
1.267 brouard 179: Revision 1.266 2017/05/13 07:26:12 brouard
180: Summary: Version 0.99r13 (improvements and bugs fixed)
181:
1.266 brouard 182: Revision 1.265 2017/04/26 16:22:11 brouard
183: Summary: imach 0.99r13 Some bugs fixed
184:
1.265 brouard 185: Revision 1.264 2017/04/26 06:01:29 brouard
186: Summary: Labels in graphs
187:
1.264 brouard 188: Revision 1.263 2017/04/24 15:23:15 brouard
189: Summary: to save
190:
1.263 brouard 191: Revision 1.262 2017/04/18 16:48:12 brouard
192: *** empty log message ***
193:
1.262 brouard 194: Revision 1.261 2017/04/05 10:14:09 brouard
195: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
196:
1.261 brouard 197: Revision 1.260 2017/04/04 17:46:59 brouard
198: Summary: Gnuplot indexations fixed (humm)
199:
1.260 brouard 200: Revision 1.259 2017/04/04 13:01:16 brouard
201: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
202:
1.259 brouard 203: Revision 1.258 2017/04/03 10:17:47 brouard
204: Summary: Version 0.99r12
205:
206: Some cleanings, conformed with updated documentation.
207:
1.258 brouard 208: Revision 1.257 2017/03/29 16:53:30 brouard
209: Summary: Temp
210:
1.257 brouard 211: Revision 1.256 2017/03/27 05:50:23 brouard
212: Summary: Temporary
213:
1.256 brouard 214: Revision 1.255 2017/03/08 16:02:28 brouard
215: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
216:
1.255 brouard 217: Revision 1.254 2017/03/08 07:13:00 brouard
218: Summary: Fixing data parameter line
219:
1.254 brouard 220: Revision 1.253 2016/12/15 11:59:41 brouard
221: Summary: 0.99 in progress
222:
1.253 brouard 223: Revision 1.252 2016/09/15 21:15:37 brouard
224: *** empty log message ***
225:
1.252 brouard 226: Revision 1.251 2016/09/15 15:01:13 brouard
227: Summary: not working
228:
1.251 brouard 229: Revision 1.250 2016/09/08 16:07:27 brouard
230: Summary: continue
231:
1.250 brouard 232: Revision 1.249 2016/09/07 17:14:18 brouard
233: Summary: Starting values from frequencies
234:
1.249 brouard 235: Revision 1.248 2016/09/07 14:10:18 brouard
236: *** empty log message ***
237:
1.248 brouard 238: Revision 1.247 2016/09/02 11:11:21 brouard
239: *** empty log message ***
240:
1.247 brouard 241: Revision 1.246 2016/09/02 08:49:22 brouard
242: *** empty log message ***
243:
1.246 brouard 244: Revision 1.245 2016/09/02 07:25:01 brouard
245: *** empty log message ***
246:
1.245 brouard 247: Revision 1.244 2016/09/02 07:17:34 brouard
248: *** empty log message ***
249:
1.244 brouard 250: Revision 1.243 2016/09/02 06:45:35 brouard
251: *** empty log message ***
252:
1.243 brouard 253: Revision 1.242 2016/08/30 15:01:20 brouard
254: Summary: Fixing a lots
255:
1.242 brouard 256: Revision 1.241 2016/08/29 17:17:25 brouard
257: Summary: gnuplot problem in Back projection to fix
258:
1.241 brouard 259: Revision 1.240 2016/08/29 07:53:18 brouard
260: Summary: Better
261:
1.240 brouard 262: Revision 1.239 2016/08/26 15:51:03 brouard
263: Summary: Improvement in Powell output in order to copy and paste
264:
265: Author:
266:
1.239 brouard 267: Revision 1.238 2016/08/26 14:23:35 brouard
268: Summary: Starting tests of 0.99
269:
1.238 brouard 270: Revision 1.237 2016/08/26 09:20:19 brouard
271: Summary: to valgrind
272:
1.237 brouard 273: Revision 1.236 2016/08/25 10:50:18 brouard
274: *** empty log message ***
275:
1.236 brouard 276: Revision 1.235 2016/08/25 06:59:23 brouard
277: *** empty log message ***
278:
1.235 brouard 279: Revision 1.234 2016/08/23 16:51:20 brouard
280: *** empty log message ***
281:
1.234 brouard 282: Revision 1.233 2016/08/23 07:40:50 brouard
283: Summary: not working
284:
1.233 brouard 285: Revision 1.232 2016/08/22 14:20:21 brouard
286: Summary: not working
287:
1.232 brouard 288: Revision 1.231 2016/08/22 07:17:15 brouard
289: Summary: not working
290:
1.231 brouard 291: Revision 1.230 2016/08/22 06:55:53 brouard
292: Summary: Not working
293:
1.230 brouard 294: Revision 1.229 2016/07/23 09:45:53 brouard
295: Summary: Completing for func too
296:
1.229 brouard 297: Revision 1.228 2016/07/22 17:45:30 brouard
298: Summary: Fixing some arrays, still debugging
299:
1.227 brouard 300: Revision 1.226 2016/07/12 18:42:34 brouard
301: Summary: temp
302:
1.226 brouard 303: Revision 1.225 2016/07/12 08:40:03 brouard
304: Summary: saving but not running
305:
1.225 brouard 306: Revision 1.224 2016/07/01 13:16:01 brouard
307: Summary: Fixes
308:
1.224 brouard 309: Revision 1.223 2016/02/19 09:23:35 brouard
310: Summary: temporary
311:
1.223 brouard 312: Revision 1.222 2016/02/17 08:14:50 brouard
313: Summary: Probably last 0.98 stable version 0.98r6
314:
1.222 brouard 315: Revision 1.221 2016/02/15 23:35:36 brouard
316: Summary: minor bug
317:
1.220 brouard 318: Revision 1.219 2016/02/15 00:48:12 brouard
319: *** empty log message ***
320:
1.219 brouard 321: Revision 1.218 2016/02/12 11:29:23 brouard
322: Summary: 0.99 Back projections
323:
1.218 brouard 324: Revision 1.217 2015/12/23 17:18:31 brouard
325: Summary: Experimental backcast
326:
1.217 brouard 327: Revision 1.216 2015/12/18 17:32:11 brouard
328: Summary: 0.98r4 Warning and status=-2
329:
330: Version 0.98r4 is now:
331: - displaying an error when status is -1, date of interview unknown and date of death known;
332: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
333: Older changes concerning s=-2, dating from 2005 have been supersed.
334:
1.216 brouard 335: Revision 1.215 2015/12/16 08:52:24 brouard
336: Summary: 0.98r4 working
337:
1.215 brouard 338: Revision 1.214 2015/12/16 06:57:54 brouard
339: Summary: temporary not working
340:
1.214 brouard 341: Revision 1.213 2015/12/11 18:22:17 brouard
342: Summary: 0.98r4
343:
1.213 brouard 344: Revision 1.212 2015/11/21 12:47:24 brouard
345: Summary: minor typo
346:
1.212 brouard 347: Revision 1.211 2015/11/21 12:41:11 brouard
348: Summary: 0.98r3 with some graph of projected cross-sectional
349:
350: Author: Nicolas Brouard
351:
1.211 brouard 352: Revision 1.210 2015/11/18 17:41:20 brouard
1.252 brouard 353: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
1.210 brouard 354: Summary: Adding ftolpl parameter
355: Author: N Brouard
356:
357: We had difficulties to get smoothed confidence intervals. It was due
358: to the period prevalence which wasn't computed accurately. The inner
359: parameter ftolpl is now an outer parameter of the .imach parameter
360: file after estepm. If ftolpl is small 1.e-4 and estepm too,
361: computation are long.
362:
1.209 brouard 363: Revision 1.208 2015/11/17 14:31:57 brouard
364: Summary: temporary
365:
1.208 brouard 366: Revision 1.207 2015/10/27 17:36:57 brouard
367: *** empty log message ***
368:
1.207 brouard 369: Revision 1.206 2015/10/24 07:14:11 brouard
370: *** empty log message ***
371:
1.206 brouard 372: Revision 1.205 2015/10/23 15:50:53 brouard
373: Summary: 0.98r3 some clarification for graphs on likelihood contributions
374:
1.205 brouard 375: Revision 1.204 2015/10/01 16:20:26 brouard
376: Summary: Some new graphs of contribution to likelihood
377:
1.204 brouard 378: Revision 1.203 2015/09/30 17:45:14 brouard
379: Summary: looking at better estimation of the hessian
380:
381: Also a better criteria for convergence to the period prevalence And
382: therefore adding the number of years needed to converge. (The
383: prevalence in any alive state shold sum to one
384:
1.203 brouard 385: Revision 1.202 2015/09/22 19:45:16 brouard
386: Summary: Adding some overall graph on contribution to likelihood. Might change
387:
1.202 brouard 388: Revision 1.201 2015/09/15 17:34:58 brouard
389: Summary: 0.98r0
390:
391: - Some new graphs like suvival functions
392: - Some bugs fixed like model=1+age+V2.
393:
1.201 brouard 394: Revision 1.200 2015/09/09 16:53:55 brouard
395: Summary: Big bug thanks to Flavia
396:
397: Even model=1+age+V2. did not work anymore
398:
1.200 brouard 399: Revision 1.199 2015/09/07 14:09:23 brouard
400: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
401:
1.199 brouard 402: Revision 1.198 2015/09/03 07:14:39 brouard
403: Summary: 0.98q5 Flavia
404:
1.198 brouard 405: Revision 1.197 2015/09/01 18:24:39 brouard
406: *** empty log message ***
407:
1.197 brouard 408: Revision 1.196 2015/08/18 23:17:52 brouard
409: Summary: 0.98q5
410:
1.196 brouard 411: Revision 1.195 2015/08/18 16:28:39 brouard
412: Summary: Adding a hack for testing purpose
413:
414: After reading the title, ftol and model lines, if the comment line has
415: a q, starting with #q, the answer at the end of the run is quit. It
416: permits to run test files in batch with ctest. The former workaround was
417: $ echo q | imach foo.imach
418:
1.195 brouard 419: Revision 1.194 2015/08/18 13:32:00 brouard
420: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
421:
1.194 brouard 422: Revision 1.193 2015/08/04 07:17:42 brouard
423: Summary: 0.98q4
424:
1.193 brouard 425: Revision 1.192 2015/07/16 16:49:02 brouard
426: Summary: Fixing some outputs
427:
1.192 brouard 428: Revision 1.191 2015/07/14 10:00:33 brouard
429: Summary: Some fixes
430:
1.191 brouard 431: Revision 1.190 2015/05/05 08:51:13 brouard
432: Summary: Adding digits in output parameters (7 digits instead of 6)
433:
434: Fix 1+age+.
435:
1.190 brouard 436: Revision 1.189 2015/04/30 14:45:16 brouard
437: Summary: 0.98q2
438:
1.189 brouard 439: Revision 1.188 2015/04/30 08:27:53 brouard
440: *** empty log message ***
441:
1.188 brouard 442: Revision 1.187 2015/04/29 09:11:15 brouard
443: *** empty log message ***
444:
1.187 brouard 445: Revision 1.186 2015/04/23 12:01:52 brouard
446: Summary: V1*age is working now, version 0.98q1
447:
448: Some codes had been disabled in order to simplify and Vn*age was
449: working in the optimization phase, ie, giving correct MLE parameters,
450: but, as usual, outputs were not correct and program core dumped.
451:
1.186 brouard 452: Revision 1.185 2015/03/11 13:26:42 brouard
453: Summary: Inclusion of compile and links command line for Intel Compiler
454:
1.185 brouard 455: Revision 1.184 2015/03/11 11:52:39 brouard
456: Summary: Back from Windows 8. Intel Compiler
457:
1.184 brouard 458: Revision 1.183 2015/03/10 20:34:32 brouard
459: Summary: 0.98q0, trying with directest, mnbrak fixed
460:
461: We use directest instead of original Powell test; probably no
462: incidence on the results, but better justifications;
463: We fixed Numerical Recipes mnbrak routine which was wrong and gave
464: wrong results.
465:
1.183 brouard 466: Revision 1.182 2015/02/12 08:19:57 brouard
467: Summary: Trying to keep directest which seems simpler and more general
468: Author: Nicolas Brouard
469:
1.182 brouard 470: Revision 1.181 2015/02/11 23:22:24 brouard
471: Summary: Comments on Powell added
472:
473: Author:
474:
1.181 brouard 475: Revision 1.180 2015/02/11 17:33:45 brouard
476: Summary: Finishing move from main to function (hpijx and prevalence_limit)
477:
1.180 brouard 478: Revision 1.179 2015/01/04 09:57:06 brouard
479: Summary: back to OS/X
480:
1.179 brouard 481: Revision 1.178 2015/01/04 09:35:48 brouard
482: *** empty log message ***
483:
1.178 brouard 484: Revision 1.177 2015/01/03 18:40:56 brouard
485: Summary: Still testing ilc32 on OSX
486:
1.177 brouard 487: Revision 1.176 2015/01/03 16:45:04 brouard
488: *** empty log message ***
489:
1.176 brouard 490: Revision 1.175 2015/01/03 16:33:42 brouard
491: *** empty log message ***
492:
1.175 brouard 493: Revision 1.174 2015/01/03 16:15:49 brouard
494: Summary: Still in cross-compilation
495:
1.174 brouard 496: Revision 1.173 2015/01/03 12:06:26 brouard
497: Summary: trying to detect cross-compilation
498:
1.173 brouard 499: Revision 1.172 2014/12/27 12:07:47 brouard
500: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
501:
1.172 brouard 502: Revision 1.171 2014/12/23 13:26:59 brouard
503: Summary: Back from Visual C
504:
505: Still problem with utsname.h on Windows
506:
1.171 brouard 507: Revision 1.170 2014/12/23 11:17:12 brouard
508: Summary: Cleaning some \%% back to %%
509:
510: The escape was mandatory for a specific compiler (which one?), but too many warnings.
511:
1.170 brouard 512: Revision 1.169 2014/12/22 23:08:31 brouard
513: Summary: 0.98p
514:
515: Outputs some informations on compiler used, OS etc. Testing on different platforms.
516:
1.169 brouard 517: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 518: Summary: update
1.169 brouard 519:
1.168 brouard 520: Revision 1.167 2014/12/22 13:50:56 brouard
521: Summary: Testing uname and compiler version and if compiled 32 or 64
522:
523: Testing on Linux 64
524:
1.167 brouard 525: Revision 1.166 2014/12/22 11:40:47 brouard
526: *** empty log message ***
527:
1.166 brouard 528: Revision 1.165 2014/12/16 11:20:36 brouard
529: Summary: After compiling on Visual C
530:
531: * imach.c (Module): Merging 1.61 to 1.162
532:
1.165 brouard 533: Revision 1.164 2014/12/16 10:52:11 brouard
534: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
535:
536: * imach.c (Module): Merging 1.61 to 1.162
537:
1.164 brouard 538: Revision 1.163 2014/12/16 10:30:11 brouard
539: * imach.c (Module): Merging 1.61 to 1.162
540:
1.163 brouard 541: Revision 1.162 2014/09/25 11:43:39 brouard
542: Summary: temporary backup 0.99!
543:
1.162 brouard 544: Revision 1.1 2014/09/16 11:06:58 brouard
545: Summary: With some code (wrong) for nlopt
546:
547: Author:
548:
549: Revision 1.161 2014/09/15 20:41:41 brouard
550: Summary: Problem with macro SQR on Intel compiler
551:
1.161 brouard 552: Revision 1.160 2014/09/02 09:24:05 brouard
553: *** empty log message ***
554:
1.160 brouard 555: Revision 1.159 2014/09/01 10:34:10 brouard
556: Summary: WIN32
557: Author: Brouard
558:
1.159 brouard 559: Revision 1.158 2014/08/27 17:11:51 brouard
560: *** empty log message ***
561:
1.158 brouard 562: Revision 1.157 2014/08/27 16:26:55 brouard
563: Summary: Preparing windows Visual studio version
564: Author: Brouard
565:
566: In order to compile on Visual studio, time.h is now correct and time_t
567: and tm struct should be used. difftime should be used but sometimes I
568: just make the differences in raw time format (time(&now).
569: Trying to suppress #ifdef LINUX
570: Add xdg-open for __linux in order to open default browser.
571:
1.157 brouard 572: Revision 1.156 2014/08/25 20:10:10 brouard
573: *** empty log message ***
574:
1.156 brouard 575: Revision 1.155 2014/08/25 18:32:34 brouard
576: Summary: New compile, minor changes
577: Author: Brouard
578:
1.155 brouard 579: Revision 1.154 2014/06/20 17:32:08 brouard
580: Summary: Outputs now all graphs of convergence to period prevalence
581:
1.154 brouard 582: Revision 1.153 2014/06/20 16:45:46 brouard
583: Summary: If 3 live state, convergence to period prevalence on same graph
584: Author: Brouard
585:
1.153 brouard 586: Revision 1.152 2014/06/18 17:54:09 brouard
587: Summary: open browser, use gnuplot on same dir than imach if not found in the path
588:
1.152 brouard 589: Revision 1.151 2014/06/18 16:43:30 brouard
590: *** empty log message ***
591:
1.151 brouard 592: Revision 1.150 2014/06/18 16:42:35 brouard
593: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
594: Author: brouard
595:
1.150 brouard 596: Revision 1.149 2014/06/18 15:51:14 brouard
597: Summary: Some fixes in parameter files errors
598: Author: Nicolas Brouard
599:
1.149 brouard 600: Revision 1.148 2014/06/17 17:38:48 brouard
601: Summary: Nothing new
602: Author: Brouard
603:
604: Just a new packaging for OS/X version 0.98nS
605:
1.148 brouard 606: Revision 1.147 2014/06/16 10:33:11 brouard
607: *** empty log message ***
608:
1.147 brouard 609: Revision 1.146 2014/06/16 10:20:28 brouard
610: Summary: Merge
611: Author: Brouard
612:
613: Merge, before building revised version.
614:
1.146 brouard 615: Revision 1.145 2014/06/10 21:23:15 brouard
616: Summary: Debugging with valgrind
617: Author: Nicolas Brouard
618:
619: Lot of changes in order to output the results with some covariates
620: After the Edimburgh REVES conference 2014, it seems mandatory to
621: improve the code.
622: No more memory valgrind error but a lot has to be done in order to
623: continue the work of splitting the code into subroutines.
624: Also, decodemodel has been improved. Tricode is still not
625: optimal. nbcode should be improved. Documentation has been added in
626: the source code.
627:
1.144 brouard 628: Revision 1.143 2014/01/26 09:45:38 brouard
629: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
630:
631: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
632: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
633:
1.143 brouard 634: Revision 1.142 2014/01/26 03:57:36 brouard
635: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
636:
637: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
638:
1.142 brouard 639: Revision 1.141 2014/01/26 02:42:01 brouard
640: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
641:
1.141 brouard 642: Revision 1.140 2011/09/02 10:37:54 brouard
643: Summary: times.h is ok with mingw32 now.
644:
1.140 brouard 645: Revision 1.139 2010/06/14 07:50:17 brouard
646: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
647: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
648:
1.139 brouard 649: Revision 1.138 2010/04/30 18:19:40 brouard
650: *** empty log message ***
651:
1.138 brouard 652: Revision 1.137 2010/04/29 18:11:38 brouard
653: (Module): Checking covariates for more complex models
654: than V1+V2. A lot of change to be done. Unstable.
655:
1.137 brouard 656: Revision 1.136 2010/04/26 20:30:53 brouard
657: (Module): merging some libgsl code. Fixing computation
658: of likelione (using inter/intrapolation if mle = 0) in order to
659: get same likelihood as if mle=1.
660: Some cleaning of code and comments added.
661:
1.136 brouard 662: Revision 1.135 2009/10/29 15:33:14 brouard
663: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
664:
1.135 brouard 665: Revision 1.134 2009/10/29 13:18:53 brouard
666: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
667:
1.134 brouard 668: Revision 1.133 2009/07/06 10:21:25 brouard
669: just nforces
670:
1.133 brouard 671: Revision 1.132 2009/07/06 08:22:05 brouard
672: Many tings
673:
1.132 brouard 674: Revision 1.131 2009/06/20 16:22:47 brouard
675: Some dimensions resccaled
676:
1.131 brouard 677: Revision 1.130 2009/05/26 06:44:34 brouard
678: (Module): Max Covariate is now set to 20 instead of 8. A
679: lot of cleaning with variables initialized to 0. Trying to make
680: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
681:
1.130 brouard 682: Revision 1.129 2007/08/31 13:49:27 lievre
683: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
684:
1.129 lievre 685: Revision 1.128 2006/06/30 13:02:05 brouard
686: (Module): Clarifications on computing e.j
687:
1.128 brouard 688: Revision 1.127 2006/04/28 18:11:50 brouard
689: (Module): Yes the sum of survivors was wrong since
690: imach-114 because nhstepm was no more computed in the age
691: loop. Now we define nhstepma in the age loop.
692: (Module): In order to speed up (in case of numerous covariates) we
693: compute health expectancies (without variances) in a first step
694: and then all the health expectancies with variances or standard
695: deviation (needs data from the Hessian matrices) which slows the
696: computation.
697: In the future we should be able to stop the program is only health
698: expectancies and graph are needed without standard deviations.
699:
1.127 brouard 700: Revision 1.126 2006/04/28 17:23:28 brouard
701: (Module): Yes the sum of survivors was wrong since
702: imach-114 because nhstepm was no more computed in the age
703: loop. Now we define nhstepma in the age loop.
704: Version 0.98h
705:
1.126 brouard 706: Revision 1.125 2006/04/04 15:20:31 lievre
707: Errors in calculation of health expectancies. Age was not initialized.
708: Forecasting file added.
709:
710: Revision 1.124 2006/03/22 17:13:53 lievre
711: Parameters are printed with %lf instead of %f (more numbers after the comma).
712: The log-likelihood is printed in the log file
713:
714: Revision 1.123 2006/03/20 10:52:43 brouard
715: * imach.c (Module): <title> changed, corresponds to .htm file
716: name. <head> headers where missing.
717:
718: * imach.c (Module): Weights can have a decimal point as for
719: English (a comma might work with a correct LC_NUMERIC environment,
720: otherwise the weight is truncated).
721: Modification of warning when the covariates values are not 0 or
722: 1.
723: Version 0.98g
724:
725: Revision 1.122 2006/03/20 09:45:41 brouard
726: (Module): Weights can have a decimal point as for
727: English (a comma might work with a correct LC_NUMERIC environment,
728: otherwise the weight is truncated).
729: Modification of warning when the covariates values are not 0 or
730: 1.
731: Version 0.98g
732:
733: Revision 1.121 2006/03/16 17:45:01 lievre
734: * imach.c (Module): Comments concerning covariates added
735:
736: * imach.c (Module): refinements in the computation of lli if
737: status=-2 in order to have more reliable computation if stepm is
738: not 1 month. Version 0.98f
739:
740: Revision 1.120 2006/03/16 15:10:38 lievre
741: (Module): refinements in the computation of lli if
742: status=-2 in order to have more reliable computation if stepm is
743: not 1 month. Version 0.98f
744:
745: Revision 1.119 2006/03/15 17:42:26 brouard
746: (Module): Bug if status = -2, the loglikelihood was
747: computed as likelihood omitting the logarithm. Version O.98e
748:
749: Revision 1.118 2006/03/14 18:20:07 brouard
750: (Module): varevsij Comments added explaining the second
751: table of variances if popbased=1 .
752: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
753: (Module): Function pstamp added
754: (Module): Version 0.98d
755:
756: Revision 1.117 2006/03/14 17:16:22 brouard
757: (Module): varevsij Comments added explaining the second
758: table of variances if popbased=1 .
759: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
760: (Module): Function pstamp added
761: (Module): Version 0.98d
762:
763: Revision 1.116 2006/03/06 10:29:27 brouard
764: (Module): Variance-covariance wrong links and
765: varian-covariance of ej. is needed (Saito).
766:
767: Revision 1.115 2006/02/27 12:17:45 brouard
768: (Module): One freematrix added in mlikeli! 0.98c
769:
770: Revision 1.114 2006/02/26 12:57:58 brouard
771: (Module): Some improvements in processing parameter
772: filename with strsep.
773:
774: Revision 1.113 2006/02/24 14:20:24 brouard
775: (Module): Memory leaks checks with valgrind and:
776: datafile was not closed, some imatrix were not freed and on matrix
777: allocation too.
778:
779: Revision 1.112 2006/01/30 09:55:26 brouard
780: (Module): Back to gnuplot.exe instead of wgnuplot.exe
781:
782: Revision 1.111 2006/01/25 20:38:18 brouard
783: (Module): Lots of cleaning and bugs added (Gompertz)
784: (Module): Comments can be added in data file. Missing date values
785: can be a simple dot '.'.
786:
787: Revision 1.110 2006/01/25 00:51:50 brouard
788: (Module): Lots of cleaning and bugs added (Gompertz)
789:
790: Revision 1.109 2006/01/24 19:37:15 brouard
791: (Module): Comments (lines starting with a #) are allowed in data.
792:
793: Revision 1.108 2006/01/19 18:05:42 lievre
794: Gnuplot problem appeared...
795: To be fixed
796:
797: Revision 1.107 2006/01/19 16:20:37 brouard
798: Test existence of gnuplot in imach path
799:
800: Revision 1.106 2006/01/19 13:24:36 brouard
801: Some cleaning and links added in html output
802:
803: Revision 1.105 2006/01/05 20:23:19 lievre
804: *** empty log message ***
805:
806: Revision 1.104 2005/09/30 16:11:43 lievre
807: (Module): sump fixed, loop imx fixed, and simplifications.
808: (Module): If the status is missing at the last wave but we know
809: that the person is alive, then we can code his/her status as -2
810: (instead of missing=-1 in earlier versions) and his/her
811: contributions to the likelihood is 1 - Prob of dying from last
812: health status (= 1-p13= p11+p12 in the easiest case of somebody in
813: the healthy state at last known wave). Version is 0.98
814:
815: Revision 1.103 2005/09/30 15:54:49 lievre
816: (Module): sump fixed, loop imx fixed, and simplifications.
817:
818: Revision 1.102 2004/09/15 17:31:30 brouard
819: Add the possibility to read data file including tab characters.
820:
821: Revision 1.101 2004/09/15 10:38:38 brouard
822: Fix on curr_time
823:
824: Revision 1.100 2004/07/12 18:29:06 brouard
825: Add version for Mac OS X. Just define UNIX in Makefile
826:
827: Revision 1.99 2004/06/05 08:57:40 brouard
828: *** empty log message ***
829:
830: Revision 1.98 2004/05/16 15:05:56 brouard
831: New version 0.97 . First attempt to estimate force of mortality
832: directly from the data i.e. without the need of knowing the health
833: state at each age, but using a Gompertz model: log u =a + b*age .
834: This is the basic analysis of mortality and should be done before any
835: other analysis, in order to test if the mortality estimated from the
836: cross-longitudinal survey is different from the mortality estimated
837: from other sources like vital statistic data.
838:
839: The same imach parameter file can be used but the option for mle should be -3.
840:
1.133 brouard 841: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 842: former routines in order to include the new code within the former code.
843:
844: The output is very simple: only an estimate of the intercept and of
845: the slope with 95% confident intervals.
846:
847: Current limitations:
848: A) Even if you enter covariates, i.e. with the
849: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
850: B) There is no computation of Life Expectancy nor Life Table.
851:
852: Revision 1.97 2004/02/20 13:25:42 lievre
853: Version 0.96d. Population forecasting command line is (temporarily)
854: suppressed.
855:
856: Revision 1.96 2003/07/15 15:38:55 brouard
857: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
858: rewritten within the same printf. Workaround: many printfs.
859:
860: Revision 1.95 2003/07/08 07:54:34 brouard
861: * imach.c (Repository):
862: (Repository): Using imachwizard code to output a more meaningful covariance
863: matrix (cov(a12,c31) instead of numbers.
864:
865: Revision 1.94 2003/06/27 13:00:02 brouard
866: Just cleaning
867:
868: Revision 1.93 2003/06/25 16:33:55 brouard
869: (Module): On windows (cygwin) function asctime_r doesn't
870: exist so I changed back to asctime which exists.
871: (Module): Version 0.96b
872:
873: Revision 1.92 2003/06/25 16:30:45 brouard
874: (Module): On windows (cygwin) function asctime_r doesn't
875: exist so I changed back to asctime which exists.
876:
877: Revision 1.91 2003/06/25 15:30:29 brouard
878: * imach.c (Repository): Duplicated warning errors corrected.
879: (Repository): Elapsed time after each iteration is now output. It
880: helps to forecast when convergence will be reached. Elapsed time
881: is stamped in powell. We created a new html file for the graphs
882: concerning matrix of covariance. It has extension -cov.htm.
883:
884: Revision 1.90 2003/06/24 12:34:15 brouard
885: (Module): Some bugs corrected for windows. Also, when
886: mle=-1 a template is output in file "or"mypar.txt with the design
887: of the covariance matrix to be input.
888:
889: Revision 1.89 2003/06/24 12:30:52 brouard
890: (Module): Some bugs corrected for windows. Also, when
891: mle=-1 a template is output in file "or"mypar.txt with the design
892: of the covariance matrix to be input.
893:
894: Revision 1.88 2003/06/23 17:54:56 brouard
895: * 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.
896:
897: Revision 1.87 2003/06/18 12:26:01 brouard
898: Version 0.96
899:
900: Revision 1.86 2003/06/17 20:04:08 brouard
901: (Module): Change position of html and gnuplot routines and added
902: routine fileappend.
903:
904: Revision 1.85 2003/06/17 13:12:43 brouard
905: * imach.c (Repository): Check when date of death was earlier that
906: current date of interview. It may happen when the death was just
907: prior to the death. In this case, dh was negative and likelihood
908: was wrong (infinity). We still send an "Error" but patch by
909: assuming that the date of death was just one stepm after the
910: interview.
911: (Repository): Because some people have very long ID (first column)
912: we changed int to long in num[] and we added a new lvector for
913: memory allocation. But we also truncated to 8 characters (left
914: truncation)
915: (Repository): No more line truncation errors.
916:
917: Revision 1.84 2003/06/13 21:44:43 brouard
918: * imach.c (Repository): Replace "freqsummary" at a correct
919: place. It differs from routine "prevalence" which may be called
920: many times. Probs is memory consuming and must be used with
921: parcimony.
922: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
923:
924: Revision 1.83 2003/06/10 13:39:11 lievre
925: *** empty log message ***
926:
927: Revision 1.82 2003/06/05 15:57:20 brouard
928: Add log in imach.c and fullversion number is now printed.
929:
930: */
931: /*
932: Interpolated Markov Chain
933:
934: Short summary of the programme:
935:
1.227 brouard 936: This program computes Healthy Life Expectancies or State-specific
937: (if states aren't health statuses) Expectancies from
938: cross-longitudinal data. Cross-longitudinal data consist in:
939:
940: -1- a first survey ("cross") where individuals from different ages
941: are interviewed on their health status or degree of disability (in
942: the case of a health survey which is our main interest)
943:
944: -2- at least a second wave of interviews ("longitudinal") which
945: measure each change (if any) in individual health status. Health
946: expectancies are computed from the time spent in each health state
947: according to a model. More health states you consider, more time is
948: necessary to reach the Maximum Likelihood of the parameters involved
949: in the model. The simplest model is the multinomial logistic model
950: where pij is the probability to be observed in state j at the second
951: wave conditional to be observed in state i at the first
952: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
953: etc , where 'age' is age and 'sex' is a covariate. If you want to
954: have a more complex model than "constant and age", you should modify
955: the program where the markup *Covariates have to be included here
956: again* invites you to do it. More covariates you add, slower the
1.126 brouard 957: convergence.
958:
959: The advantage of this computer programme, compared to a simple
960: multinomial logistic model, is clear when the delay between waves is not
961: identical for each individual. Also, if a individual missed an
962: intermediate interview, the information is lost, but taken into
963: account using an interpolation or extrapolation.
964:
965: hPijx is the probability to be observed in state i at age x+h
966: conditional to the observed state i at age x. The delay 'h' can be
967: split into an exact number (nh*stepm) of unobserved intermediate
968: states. This elementary transition (by month, quarter,
969: semester or year) is modelled as a multinomial logistic. The hPx
970: matrix is simply the matrix product of nh*stepm elementary matrices
971: and the contribution of each individual to the likelihood is simply
972: hPijx.
973:
974: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 975: of the life expectancies. It also computes the period (stable) prevalence.
976:
977: Back prevalence and projections:
1.227 brouard 978:
979: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
980: double agemaxpar, double ftolpl, int *ncvyearp, double
981: dateprev1,double dateprev2, int firstpass, int lastpass, int
982: mobilavproj)
983:
984: Computes the back prevalence limit for any combination of
985: covariate values k at any age between ageminpar and agemaxpar and
986: returns it in **bprlim. In the loops,
987:
988: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
989: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
990:
991: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 992: Computes for any combination of covariates k and any age between bage and fage
993: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
994: oldm=oldms;savm=savms;
1.227 brouard 995:
1.267 brouard 996: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.218 brouard 997: Computes the transition matrix starting at age 'age' over
998: 'nhstepm*hstepm*stepm' months (i.e. until
999: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 1000: nhstepm*hstepm matrices.
1001:
1002: Returns p3mat[i][j][h] after calling
1003: p3mat[i][j][h]=matprod2(newm,
1004: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
1005: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
1006: oldm);
1.226 brouard 1007:
1008: Important routines
1009:
1010: - func (or funcone), computes logit (pij) distinguishing
1011: o fixed variables (single or product dummies or quantitative);
1012: o varying variables by:
1013: (1) wave (single, product dummies, quantitative),
1014: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
1015: % fixed dummy (treated) or quantitative (not done because time-consuming);
1016: % varying dummy (not done) or quantitative (not done);
1017: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1018: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1019: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1020: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1021: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 1022:
1.226 brouard 1023:
1024:
1.133 brouard 1025: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1026: Institut national d'études démographiques, Paris.
1.126 brouard 1027: This software have been partly granted by Euro-REVES, a concerted action
1028: from the European Union.
1029: It is copyrighted identically to a GNU software product, ie programme and
1030: software can be distributed freely for non commercial use. Latest version
1031: can be accessed at http://euroreves.ined.fr/imach .
1032:
1033: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1034: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1035:
1036: **********************************************************************/
1037: /*
1038: main
1039: read parameterfile
1040: read datafile
1041: concatwav
1042: freqsummary
1043: if (mle >= 1)
1044: mlikeli
1045: print results files
1046: if mle==1
1047: computes hessian
1048: read end of parameter file: agemin, agemax, bage, fage, estepm
1049: begin-prev-date,...
1050: open gnuplot file
1051: open html file
1.145 brouard 1052: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1053: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1054: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1055: freexexit2 possible for memory heap.
1056:
1057: h Pij x | pij_nom ficrestpij
1058: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1059: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1060: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1061:
1062: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1063: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1064: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1065: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1066: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1067:
1.126 brouard 1068: forecasting if prevfcast==1 prevforecast call prevalence()
1069: health expectancies
1070: Variance-covariance of DFLE
1071: prevalence()
1072: movingaverage()
1073: varevsij()
1074: if popbased==1 varevsij(,popbased)
1075: total life expectancies
1076: Variance of period (stable) prevalence
1077: end
1078: */
1079:
1.187 brouard 1080: /* #define DEBUG */
1081: /* #define DEBUGBRENT */
1.203 brouard 1082: /* #define DEBUGLINMIN */
1083: /* #define DEBUGHESS */
1084: #define DEBUGHESSIJ
1.224 brouard 1085: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 1086: #define POWELL /* Instead of NLOPT */
1.224 brouard 1087: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 1088: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1089: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 1090:
1091: #include <math.h>
1092: #include <stdio.h>
1093: #include <stdlib.h>
1094: #include <string.h>
1.226 brouard 1095: #include <ctype.h>
1.159 brouard 1096:
1097: #ifdef _WIN32
1098: #include <io.h>
1.172 brouard 1099: #include <windows.h>
1100: #include <tchar.h>
1.159 brouard 1101: #else
1.126 brouard 1102: #include <unistd.h>
1.159 brouard 1103: #endif
1.126 brouard 1104:
1105: #include <limits.h>
1106: #include <sys/types.h>
1.171 brouard 1107:
1108: #if defined(__GNUC__)
1109: #include <sys/utsname.h> /* Doesn't work on Windows */
1110: #endif
1111:
1.126 brouard 1112: #include <sys/stat.h>
1113: #include <errno.h>
1.159 brouard 1114: /* extern int errno; */
1.126 brouard 1115:
1.157 brouard 1116: /* #ifdef LINUX */
1117: /* #include <time.h> */
1118: /* #include "timeval.h" */
1119: /* #else */
1120: /* #include <sys/time.h> */
1121: /* #endif */
1122:
1.126 brouard 1123: #include <time.h>
1124:
1.136 brouard 1125: #ifdef GSL
1126: #include <gsl/gsl_errno.h>
1127: #include <gsl/gsl_multimin.h>
1128: #endif
1129:
1.167 brouard 1130:
1.162 brouard 1131: #ifdef NLOPT
1132: #include <nlopt.h>
1133: typedef struct {
1134: double (* function)(double [] );
1135: } myfunc_data ;
1136: #endif
1137:
1.126 brouard 1138: /* #include <libintl.h> */
1139: /* #define _(String) gettext (String) */
1140:
1.251 brouard 1141: #define MAXLINE 2048 /* Was 256 and 1024. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 1142:
1143: #define GNUPLOTPROGRAM "gnuplot"
1144: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1145: #define FILENAMELENGTH 132
1146:
1147: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1148: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1149:
1.144 brouard 1150: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
1151: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 1152:
1153: #define NINTERVMAX 8
1.144 brouard 1154: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1155: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1.291 brouard 1156: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 1157: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 1158: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1159: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.290 brouard 1160: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1.144 brouard 1161: #define YEARM 12. /**< Number of months per year */
1.218 brouard 1162: /* #define AGESUP 130 */
1.288 brouard 1163: /* #define AGESUP 150 */
1164: #define AGESUP 200
1.268 brouard 1165: #define AGEINF 0
1.218 brouard 1166: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 1167: #define AGEBASE 40
1.194 brouard 1168: #define AGEOVERFLOW 1.e20
1.164 brouard 1169: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 1170: #ifdef _WIN32
1171: #define DIRSEPARATOR '\\'
1172: #define CHARSEPARATOR "\\"
1173: #define ODIRSEPARATOR '/'
1174: #else
1.126 brouard 1175: #define DIRSEPARATOR '/'
1176: #define CHARSEPARATOR "/"
1177: #define ODIRSEPARATOR '\\'
1178: #endif
1179:
1.315 ! brouard 1180: /* $Id: imach.c,v 1.314 2022/04/13 17:43:09 brouard Exp $ */
1.126 brouard 1181: /* $State: Exp $ */
1.196 brouard 1182: #include "version.h"
1183: char version[]=__IMACH_VERSION__;
1.308 brouard 1184: char copyright[]="March 2021,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, INED 2000-2021";
1.315 ! brouard 1185: char fullversion[]="$Revision: 1.314 $ $Date: 2022/04/13 17:43:09 $";
1.126 brouard 1186: char strstart[80];
1187: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 1188: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 1189: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 1190: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
1191: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
1192: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 1193: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
1194: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 1195: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1196: int cptcovprodnoage=0; /**< Number of covariate products without age */
1197: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 1198: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1199: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 1200: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 brouard 1201: int nsd=0; /**< Total number of single dummy variables (output) */
1202: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 1203: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 1204: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 1205: int ntveff=0; /**< ntveff number of effective time varying variables */
1206: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 1207: int cptcov=0; /* Working variable */
1.290 brouard 1208: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1.218 brouard 1209: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.302 brouard 1210: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1.126 brouard 1211: int nlstate=2; /* Number of live states */
1212: int ndeath=1; /* Number of dead states */
1.130 brouard 1213: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 1214: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 1215: int popbased=0;
1216:
1217: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 1218: int maxwav=0; /* Maxim number of waves */
1219: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1220: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1221: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 1222: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 1223: int mle=1, weightopt=0;
1.126 brouard 1224: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1225: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1226: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1227: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 1228: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 1229: int selected(int kvar); /* Is covariate kvar selected for printing results */
1230:
1.130 brouard 1231: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 1232: double **matprod2(); /* test */
1.126 brouard 1233: double **oldm, **newm, **savm; /* Working pointers to matrices */
1234: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 1235: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1236:
1.136 brouard 1237: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 1238: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 1239: FILE *ficlog, *ficrespow;
1.130 brouard 1240: int globpr=0; /* Global variable for printing or not */
1.126 brouard 1241: double fretone; /* Only one call to likelihood */
1.130 brouard 1242: long ipmx=0; /* Number of contributions */
1.126 brouard 1243: double sw; /* Sum of weights */
1244: char filerespow[FILENAMELENGTH];
1245: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1246: FILE *ficresilk;
1247: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1248: FILE *ficresprobmorprev;
1249: FILE *fichtm, *fichtmcov; /* Html File */
1250: FILE *ficreseij;
1251: char filerese[FILENAMELENGTH];
1252: FILE *ficresstdeij;
1253: char fileresstde[FILENAMELENGTH];
1254: FILE *ficrescveij;
1255: char filerescve[FILENAMELENGTH];
1256: FILE *ficresvij;
1257: char fileresv[FILENAMELENGTH];
1.269 brouard 1258:
1.126 brouard 1259: char title[MAXLINE];
1.234 brouard 1260: char model[MAXLINE]; /**< The model line */
1.217 brouard 1261: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1262: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1263: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1264: char command[FILENAMELENGTH];
1265: int outcmd=0;
1266:
1.217 brouard 1267: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1268: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1269: char filelog[FILENAMELENGTH]; /* Log file */
1270: char filerest[FILENAMELENGTH];
1271: char fileregp[FILENAMELENGTH];
1272: char popfile[FILENAMELENGTH];
1273:
1274: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1275:
1.157 brouard 1276: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1277: /* struct timezone tzp; */
1278: /* extern int gettimeofday(); */
1279: struct tm tml, *gmtime(), *localtime();
1280:
1281: extern time_t time();
1282:
1283: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1284: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1285: struct tm tm;
1286:
1.126 brouard 1287: char strcurr[80], strfor[80];
1288:
1289: char *endptr;
1290: long lval;
1291: double dval;
1292:
1293: #define NR_END 1
1294: #define FREE_ARG char*
1295: #define FTOL 1.0e-10
1296:
1297: #define NRANSI
1.240 brouard 1298: #define ITMAX 200
1299: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1300:
1301: #define TOL 2.0e-4
1302:
1303: #define CGOLD 0.3819660
1304: #define ZEPS 1.0e-10
1305: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1306:
1307: #define GOLD 1.618034
1308: #define GLIMIT 100.0
1309: #define TINY 1.0e-20
1310:
1311: static double maxarg1,maxarg2;
1312: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1313: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1314:
1315: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1316: #define rint(a) floor(a+0.5)
1.166 brouard 1317: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1318: #define mytinydouble 1.0e-16
1.166 brouard 1319: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1320: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1321: /* static double dsqrarg; */
1322: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1323: static double sqrarg;
1324: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1325: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1326: int agegomp= AGEGOMP;
1327:
1328: int imx;
1329: int stepm=1;
1330: /* Stepm, step in month: minimum step interpolation*/
1331:
1332: int estepm;
1333: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1334:
1335: int m,nb;
1336: long *num;
1.197 brouard 1337: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1338: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1339: covariate for which somebody answered excluding
1340: undefined. Usually 2: 0 and 1. */
1341: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1342: covariate for which somebody answered including
1343: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1344: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1345: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1346: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1347: double *ageexmed,*agecens;
1348: double dateintmean=0;
1.296 brouard 1349: double anprojd, mprojd, jprojd; /* For eventual projections */
1350: double anprojf, mprojf, jprojf;
1.126 brouard 1351:
1.296 brouard 1352: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1353: double anbackf, mbackf, jbackf;
1354: double jintmean,mintmean,aintmean;
1.126 brouard 1355: double *weight;
1356: int **s; /* Status */
1.141 brouard 1357: double *agedc;
1.145 brouard 1358: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1359: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1360: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.268 brouard 1361: double **coqvar; /* Fixed quantitative covariate nqv */
1362: double ***cotvar; /* Time varying covariate ntv */
1.225 brouard 1363: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1364: double idx;
1365: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.234 brouard 1366: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1367: /*k 1 2 3 4 5 6 7 8 9 */
1368: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1369: /* Tndvar[k] 1 2 3 4 5 */
1370: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */
1371: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */
1372: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */
1373: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */
1374: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */
1375: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1376: /* Tprod[i]=k 4 7 */
1377: /* Tage[i]=k 5 8 */
1378: /* */
1379: /* Type */
1380: /* V 1 2 3 4 5 */
1381: /* F F V V V */
1382: /* D Q D D Q */
1383: /* */
1384: int *TvarsD;
1385: int *TvarsDind;
1386: int *TvarsQ;
1387: int *TvarsQind;
1388:
1.235 brouard 1389: #define MAXRESULTLINES 10
1390: int nresult=0;
1.258 brouard 1391: int parameterline=0; /* # of the parameter (type) line */
1.235 brouard 1392: int TKresult[MAXRESULTLINES];
1.237 brouard 1393: int Tresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1394: int Tinvresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1.235 brouard 1395: int Tvresult[MAXRESULTLINES][NCOVMAX]; /* For dummy variable , variable # (output) */
1396: double Tqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.237 brouard 1397: double Tqinvresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.235 brouard 1398: int Tvqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , variable # (output) */
1399:
1.234 brouard 1400: /* 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 1401: 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 */
1402: 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 */
1403: 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 */
1404: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1405: 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 */
1406: 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 1407: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1408: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1409: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1410: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1411: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1412: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1413: 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 */
1414: 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 */
1415:
1.230 brouard 1416: int *Tvarsel; /**< Selected covariates for output */
1417: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1418: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1419: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1420: 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 1421: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1422: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1423: int *Tage;
1.227 brouard 1424: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1425: 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 1426: 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*/
1427: 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 1428: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1429: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1430: int **Tvard;
1431: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1432: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1433: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1434: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1435: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1436: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1437: double *lsurv, *lpop, *tpop;
1438:
1.231 brouard 1439: #define FD 1; /* Fixed dummy covariate */
1440: #define FQ 2; /* Fixed quantitative covariate */
1441: #define FP 3; /* Fixed product covariate */
1442: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1443: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1444: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1445: #define VD 10; /* Varying dummy covariate */
1446: #define VQ 11; /* Varying quantitative covariate */
1447: #define VP 12; /* Varying product covariate */
1448: #define VPDD 13; /* Varying product dummy*dummy covariate */
1449: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1450: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1451: #define APFD 16; /* Age product * fixed dummy covariate */
1452: #define APFQ 17; /* Age product * fixed quantitative covariate */
1453: #define APVD 18; /* Age product * varying dummy covariate */
1454: #define APVQ 19; /* Age product * varying quantitative covariate */
1455:
1456: #define FTYPE 1; /* Fixed covariate */
1457: #define VTYPE 2; /* Varying covariate (loop in wave) */
1458: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1459:
1460: struct kmodel{
1461: int maintype; /* main type */
1462: int subtype; /* subtype */
1463: };
1464: struct kmodel modell[NCOVMAX];
1465:
1.143 brouard 1466: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1467: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1468:
1469: /**************** split *************************/
1470: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1471: {
1472: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1473: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1474: */
1475: char *ss; /* pointer */
1.186 brouard 1476: int l1=0, l2=0; /* length counters */
1.126 brouard 1477:
1478: l1 = strlen(path ); /* length of path */
1479: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1480: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1481: if ( ss == NULL ) { /* no directory, so determine current directory */
1482: strcpy( name, path ); /* we got the fullname name because no directory */
1483: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1484: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1485: /* get current working directory */
1486: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1487: #ifdef WIN32
1488: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1489: #else
1490: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1491: #endif
1.126 brouard 1492: return( GLOCK_ERROR_GETCWD );
1493: }
1494: /* got dirc from getcwd*/
1495: printf(" DIRC = %s \n",dirc);
1.205 brouard 1496: } else { /* strip directory from path */
1.126 brouard 1497: ss++; /* after this, the filename */
1498: l2 = strlen( ss ); /* length of filename */
1499: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1500: strcpy( name, ss ); /* save file name */
1501: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1502: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1503: printf(" DIRC2 = %s \n",dirc);
1504: }
1505: /* We add a separator at the end of dirc if not exists */
1506: l1 = strlen( dirc ); /* length of directory */
1507: if( dirc[l1-1] != DIRSEPARATOR ){
1508: dirc[l1] = DIRSEPARATOR;
1509: dirc[l1+1] = 0;
1510: printf(" DIRC3 = %s \n",dirc);
1511: }
1512: ss = strrchr( name, '.' ); /* find last / */
1513: if (ss >0){
1514: ss++;
1515: strcpy(ext,ss); /* save extension */
1516: l1= strlen( name);
1517: l2= strlen(ss)+1;
1518: strncpy( finame, name, l1-l2);
1519: finame[l1-l2]= 0;
1520: }
1521:
1522: return( 0 ); /* we're done */
1523: }
1524:
1525:
1526: /******************************************/
1527:
1528: void replace_back_to_slash(char *s, char*t)
1529: {
1530: int i;
1531: int lg=0;
1532: i=0;
1533: lg=strlen(t);
1534: for(i=0; i<= lg; i++) {
1535: (s[i] = t[i]);
1536: if (t[i]== '\\') s[i]='/';
1537: }
1538: }
1539:
1.132 brouard 1540: char *trimbb(char *out, char *in)
1.137 brouard 1541: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1542: char *s;
1543: s=out;
1544: while (*in != '\0'){
1.137 brouard 1545: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1546: in++;
1547: }
1548: *out++ = *in++;
1549: }
1550: *out='\0';
1551: return s;
1552: }
1553:
1.187 brouard 1554: /* char *substrchaine(char *out, char *in, char *chain) */
1555: /* { */
1556: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1557: /* char *s, *t; */
1558: /* t=in;s=out; */
1559: /* while ((*in != *chain) && (*in != '\0')){ */
1560: /* *out++ = *in++; */
1561: /* } */
1562:
1563: /* /\* *in matches *chain *\/ */
1564: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1565: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1566: /* } */
1567: /* in--; chain--; */
1568: /* while ( (*in != '\0')){ */
1569: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1570: /* *out++ = *in++; */
1571: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1572: /* } */
1573: /* *out='\0'; */
1574: /* out=s; */
1575: /* return out; */
1576: /* } */
1577: char *substrchaine(char *out, char *in, char *chain)
1578: {
1579: /* Substract chain 'chain' from 'in', return and output 'out' */
1580: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1581:
1582: char *strloc;
1583:
1584: strcpy (out, in);
1585: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1586: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1587: if(strloc != NULL){
1588: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1589: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1590: /* strcpy (strloc, strloc +strlen(chain));*/
1591: }
1592: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1593: return out;
1594: }
1595:
1596:
1.145 brouard 1597: char *cutl(char *blocc, char *alocc, char *in, char occ)
1598: {
1.187 brouard 1599: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1600: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.310 brouard 1601: gives alocc="abcdef" and blocc="ghi2j".
1.145 brouard 1602: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1603: */
1.160 brouard 1604: char *s, *t;
1.145 brouard 1605: t=in;s=in;
1606: while ((*in != occ) && (*in != '\0')){
1607: *alocc++ = *in++;
1608: }
1609: if( *in == occ){
1610: *(alocc)='\0';
1611: s=++in;
1612: }
1613:
1614: if (s == t) {/* occ not found */
1615: *(alocc-(in-s))='\0';
1616: in=s;
1617: }
1618: while ( *in != '\0'){
1619: *blocc++ = *in++;
1620: }
1621:
1622: *blocc='\0';
1623: return t;
1624: }
1.137 brouard 1625: char *cutv(char *blocc, char *alocc, char *in, char occ)
1626: {
1.187 brouard 1627: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1628: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1629: gives blocc="abcdef2ghi" and alocc="j".
1630: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1631: */
1632: char *s, *t;
1633: t=in;s=in;
1634: while (*in != '\0'){
1635: while( *in == occ){
1636: *blocc++ = *in++;
1637: s=in;
1638: }
1639: *blocc++ = *in++;
1640: }
1641: if (s == t) /* occ not found */
1642: *(blocc-(in-s))='\0';
1643: else
1644: *(blocc-(in-s)-1)='\0';
1645: in=s;
1646: while ( *in != '\0'){
1647: *alocc++ = *in++;
1648: }
1649:
1650: *alocc='\0';
1651: return s;
1652: }
1653:
1.126 brouard 1654: int nbocc(char *s, char occ)
1655: {
1656: int i,j=0;
1657: int lg=20;
1658: i=0;
1659: lg=strlen(s);
1660: for(i=0; i<= lg; i++) {
1.234 brouard 1661: if (s[i] == occ ) j++;
1.126 brouard 1662: }
1663: return j;
1664: }
1665:
1.137 brouard 1666: /* void cutv(char *u,char *v, char*t, char occ) */
1667: /* { */
1668: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1669: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1670: /* gives u="abcdef2ghi" and v="j" *\/ */
1671: /* int i,lg,j,p=0; */
1672: /* i=0; */
1673: /* lg=strlen(t); */
1674: /* for(j=0; j<=lg-1; j++) { */
1675: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1676: /* } */
1.126 brouard 1677:
1.137 brouard 1678: /* for(j=0; j<p; j++) { */
1679: /* (u[j] = t[j]); */
1680: /* } */
1681: /* u[p]='\0'; */
1.126 brouard 1682:
1.137 brouard 1683: /* for(j=0; j<= lg; j++) { */
1684: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1685: /* } */
1686: /* } */
1.126 brouard 1687:
1.160 brouard 1688: #ifdef _WIN32
1689: char * strsep(char **pp, const char *delim)
1690: {
1691: char *p, *q;
1692:
1693: if ((p = *pp) == NULL)
1694: return 0;
1695: if ((q = strpbrk (p, delim)) != NULL)
1696: {
1697: *pp = q + 1;
1698: *q = '\0';
1699: }
1700: else
1701: *pp = 0;
1702: return p;
1703: }
1704: #endif
1705:
1.126 brouard 1706: /********************** nrerror ********************/
1707:
1708: void nrerror(char error_text[])
1709: {
1710: fprintf(stderr,"ERREUR ...\n");
1711: fprintf(stderr,"%s\n",error_text);
1712: exit(EXIT_FAILURE);
1713: }
1714: /*********************** vector *******************/
1715: double *vector(int nl, int nh)
1716: {
1717: double *v;
1718: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1719: if (!v) nrerror("allocation failure in vector");
1720: return v-nl+NR_END;
1721: }
1722:
1723: /************************ free vector ******************/
1724: void free_vector(double*v, int nl, int nh)
1725: {
1726: free((FREE_ARG)(v+nl-NR_END));
1727: }
1728:
1729: /************************ivector *******************************/
1730: int *ivector(long nl,long nh)
1731: {
1732: int *v;
1733: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1734: if (!v) nrerror("allocation failure in ivector");
1735: return v-nl+NR_END;
1736: }
1737:
1738: /******************free ivector **************************/
1739: void free_ivector(int *v, long nl, long nh)
1740: {
1741: free((FREE_ARG)(v+nl-NR_END));
1742: }
1743:
1744: /************************lvector *******************************/
1745: long *lvector(long nl,long nh)
1746: {
1747: long *v;
1748: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1749: if (!v) nrerror("allocation failure in ivector");
1750: return v-nl+NR_END;
1751: }
1752:
1753: /******************free lvector **************************/
1754: void free_lvector(long *v, long nl, long nh)
1755: {
1756: free((FREE_ARG)(v+nl-NR_END));
1757: }
1758:
1759: /******************* imatrix *******************************/
1760: int **imatrix(long nrl, long nrh, long ncl, long nch)
1761: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1762: {
1763: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1764: int **m;
1765:
1766: /* allocate pointers to rows */
1767: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1768: if (!m) nrerror("allocation failure 1 in matrix()");
1769: m += NR_END;
1770: m -= nrl;
1771:
1772:
1773: /* allocate rows and set pointers to them */
1774: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1775: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1776: m[nrl] += NR_END;
1777: m[nrl] -= ncl;
1778:
1779: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1780:
1781: /* return pointer to array of pointers to rows */
1782: return m;
1783: }
1784:
1785: /****************** free_imatrix *************************/
1786: void free_imatrix(m,nrl,nrh,ncl,nch)
1787: int **m;
1788: long nch,ncl,nrh,nrl;
1789: /* free an int matrix allocated by imatrix() */
1790: {
1791: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1792: free((FREE_ARG) (m+nrl-NR_END));
1793: }
1794:
1795: /******************* matrix *******************************/
1796: double **matrix(long nrl, long nrh, long ncl, long nch)
1797: {
1798: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1799: double **m;
1800:
1801: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1802: if (!m) nrerror("allocation failure 1 in matrix()");
1803: m += NR_END;
1804: m -= nrl;
1805:
1806: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1807: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1808: m[nrl] += NR_END;
1809: m[nrl] -= ncl;
1810:
1811: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1812: return m;
1.145 brouard 1813: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1814: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1815: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1816: */
1817: }
1818:
1819: /*************************free matrix ************************/
1820: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1821: {
1822: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1823: free((FREE_ARG)(m+nrl-NR_END));
1824: }
1825:
1826: /******************* ma3x *******************************/
1827: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1828: {
1829: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1830: double ***m;
1831:
1832: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1833: if (!m) nrerror("allocation failure 1 in matrix()");
1834: m += NR_END;
1835: m -= nrl;
1836:
1837: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1838: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1839: m[nrl] += NR_END;
1840: m[nrl] -= ncl;
1841:
1842: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1843:
1844: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1845: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1846: m[nrl][ncl] += NR_END;
1847: m[nrl][ncl] -= nll;
1848: for (j=ncl+1; j<=nch; j++)
1849: m[nrl][j]=m[nrl][j-1]+nlay;
1850:
1851: for (i=nrl+1; i<=nrh; i++) {
1852: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1853: for (j=ncl+1; j<=nch; j++)
1854: m[i][j]=m[i][j-1]+nlay;
1855: }
1856: return m;
1857: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1858: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1859: */
1860: }
1861:
1862: /*************************free ma3x ************************/
1863: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1864: {
1865: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1866: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1867: free((FREE_ARG)(m+nrl-NR_END));
1868: }
1869:
1870: /*************** function subdirf ***********/
1871: char *subdirf(char fileres[])
1872: {
1873: /* Caution optionfilefiname is hidden */
1874: strcpy(tmpout,optionfilefiname);
1875: strcat(tmpout,"/"); /* Add to the right */
1876: strcat(tmpout,fileres);
1877: return tmpout;
1878: }
1879:
1880: /*************** function subdirf2 ***********/
1881: char *subdirf2(char fileres[], char *preop)
1882: {
1.314 brouard 1883: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte"
1884: Errors in subdirf, 2, 3 while printing tmpout is
1.315 ! brouard 1885: rewritten within the same printf. Workaround: many printfs */
1.126 brouard 1886: /* Caution optionfilefiname is hidden */
1887: strcpy(tmpout,optionfilefiname);
1888: strcat(tmpout,"/");
1889: strcat(tmpout,preop);
1890: strcat(tmpout,fileres);
1891: return tmpout;
1892: }
1893:
1894: /*************** function subdirf3 ***********/
1895: char *subdirf3(char fileres[], char *preop, char *preop2)
1896: {
1897:
1898: /* Caution optionfilefiname is hidden */
1899: strcpy(tmpout,optionfilefiname);
1900: strcat(tmpout,"/");
1901: strcat(tmpout,preop);
1902: strcat(tmpout,preop2);
1903: strcat(tmpout,fileres);
1904: return tmpout;
1905: }
1.213 brouard 1906:
1907: /*************** function subdirfext ***********/
1908: char *subdirfext(char fileres[], char *preop, char *postop)
1909: {
1910:
1911: strcpy(tmpout,preop);
1912: strcat(tmpout,fileres);
1913: strcat(tmpout,postop);
1914: return tmpout;
1915: }
1.126 brouard 1916:
1.213 brouard 1917: /*************** function subdirfext3 ***********/
1918: char *subdirfext3(char fileres[], char *preop, char *postop)
1919: {
1920:
1921: /* Caution optionfilefiname is hidden */
1922: strcpy(tmpout,optionfilefiname);
1923: strcat(tmpout,"/");
1924: strcat(tmpout,preop);
1925: strcat(tmpout,fileres);
1926: strcat(tmpout,postop);
1927: return tmpout;
1928: }
1929:
1.162 brouard 1930: char *asc_diff_time(long time_sec, char ascdiff[])
1931: {
1932: long sec_left, days, hours, minutes;
1933: days = (time_sec) / (60*60*24);
1934: sec_left = (time_sec) % (60*60*24);
1935: hours = (sec_left) / (60*60) ;
1936: sec_left = (sec_left) %(60*60);
1937: minutes = (sec_left) /60;
1938: sec_left = (sec_left) % (60);
1939: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1940: return ascdiff;
1941: }
1942:
1.126 brouard 1943: /***************** f1dim *************************/
1944: extern int ncom;
1945: extern double *pcom,*xicom;
1946: extern double (*nrfunc)(double []);
1947:
1948: double f1dim(double x)
1949: {
1950: int j;
1951: double f;
1952: double *xt;
1953:
1954: xt=vector(1,ncom);
1955: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1956: f=(*nrfunc)(xt);
1957: free_vector(xt,1,ncom);
1958: return f;
1959: }
1960:
1961: /*****************brent *************************/
1962: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1963: {
1964: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1965: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1966: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1967: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1968: * returned function value.
1969: */
1.126 brouard 1970: int iter;
1971: double a,b,d,etemp;
1.159 brouard 1972: double fu=0,fv,fw,fx;
1.164 brouard 1973: double ftemp=0.;
1.126 brouard 1974: double p,q,r,tol1,tol2,u,v,w,x,xm;
1975: double e=0.0;
1976:
1977: a=(ax < cx ? ax : cx);
1978: b=(ax > cx ? ax : cx);
1979: x=w=v=bx;
1980: fw=fv=fx=(*f)(x);
1981: for (iter=1;iter<=ITMAX;iter++) {
1982: xm=0.5*(a+b);
1983: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1984: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1985: printf(".");fflush(stdout);
1986: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1987: #ifdef DEBUGBRENT
1.126 brouard 1988: 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);
1989: 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);
1990: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1991: #endif
1992: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1993: *xmin=x;
1994: return fx;
1995: }
1996: ftemp=fu;
1997: if (fabs(e) > tol1) {
1998: r=(x-w)*(fx-fv);
1999: q=(x-v)*(fx-fw);
2000: p=(x-v)*q-(x-w)*r;
2001: q=2.0*(q-r);
2002: if (q > 0.0) p = -p;
2003: q=fabs(q);
2004: etemp=e;
2005: e=d;
2006: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 2007: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 2008: else {
1.224 brouard 2009: d=p/q;
2010: u=x+d;
2011: if (u-a < tol2 || b-u < tol2)
2012: d=SIGN(tol1,xm-x);
1.126 brouard 2013: }
2014: } else {
2015: d=CGOLD*(e=(x >= xm ? a-x : b-x));
2016: }
2017: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
2018: fu=(*f)(u);
2019: if (fu <= fx) {
2020: if (u >= x) a=x; else b=x;
2021: SHFT(v,w,x,u)
1.183 brouard 2022: SHFT(fv,fw,fx,fu)
2023: } else {
2024: if (u < x) a=u; else b=u;
2025: if (fu <= fw || w == x) {
1.224 brouard 2026: v=w;
2027: w=u;
2028: fv=fw;
2029: fw=fu;
1.183 brouard 2030: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 2031: v=u;
2032: fv=fu;
1.183 brouard 2033: }
2034: }
1.126 brouard 2035: }
2036: nrerror("Too many iterations in brent");
2037: *xmin=x;
2038: return fx;
2039: }
2040:
2041: /****************** mnbrak ***********************/
2042:
2043: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2044: double (*func)(double))
1.183 brouard 2045: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2046: the downhill direction (defined by the function as evaluated at the initial points) and returns
2047: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2048: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2049: */
1.126 brouard 2050: double ulim,u,r,q, dum;
2051: double fu;
1.187 brouard 2052:
2053: double scale=10.;
2054: int iterscale=0;
2055:
2056: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2057: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2058:
2059:
2060: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2061: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2062: /* *bx = *ax - (*ax - *bx)/scale; */
2063: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2064: /* } */
2065:
1.126 brouard 2066: if (*fb > *fa) {
2067: SHFT(dum,*ax,*bx,dum)
1.183 brouard 2068: SHFT(dum,*fb,*fa,dum)
2069: }
1.126 brouard 2070: *cx=(*bx)+GOLD*(*bx-*ax);
2071: *fc=(*func)(*cx);
1.183 brouard 2072: #ifdef DEBUG
1.224 brouard 2073: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2074: 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 2075: #endif
1.224 brouard 2076: 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 2077: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 2078: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 2079: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 2080: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2081: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2082: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 2083: fu=(*func)(u);
1.163 brouard 2084: #ifdef DEBUG
2085: /* f(x)=A(x-u)**2+f(u) */
2086: double A, fparabu;
2087: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2088: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 2089: 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);
2090: 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 2091: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2092: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2093: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2094: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 2095: #endif
1.184 brouard 2096: #ifdef MNBRAKORIGINAL
1.183 brouard 2097: #else
1.191 brouard 2098: /* if (fu > *fc) { */
2099: /* #ifdef DEBUG */
2100: /* printf("mnbrak4 fu > fc \n"); */
2101: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2102: /* #endif */
2103: /* /\* 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 *\\/ *\/ */
2104: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2105: /* dum=u; /\* Shifting c and u *\/ */
2106: /* u = *cx; */
2107: /* *cx = dum; */
2108: /* dum = fu; */
2109: /* fu = *fc; */
2110: /* *fc =dum; */
2111: /* } else { /\* end *\/ */
2112: /* #ifdef DEBUG */
2113: /* printf("mnbrak3 fu < fc \n"); */
2114: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2115: /* #endif */
2116: /* dum=u; /\* Shifting c and u *\/ */
2117: /* u = *cx; */
2118: /* *cx = dum; */
2119: /* dum = fu; */
2120: /* fu = *fc; */
2121: /* *fc =dum; */
2122: /* } */
1.224 brouard 2123: #ifdef DEBUGMNBRAK
2124: double A, fparabu;
2125: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2126: fparabu= *fa - A*(*ax-u)*(*ax-u);
2127: 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);
2128: 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 2129: #endif
1.191 brouard 2130: dum=u; /* Shifting c and u */
2131: u = *cx;
2132: *cx = dum;
2133: dum = fu;
2134: fu = *fc;
2135: *fc =dum;
1.183 brouard 2136: #endif
1.162 brouard 2137: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 2138: #ifdef DEBUG
1.224 brouard 2139: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2140: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 2141: #endif
1.126 brouard 2142: fu=(*func)(u);
2143: if (fu < *fc) {
1.183 brouard 2144: #ifdef DEBUG
1.224 brouard 2145: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2146: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2147: #endif
2148: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2149: SHFT(*fb,*fc,fu,(*func)(u))
2150: #ifdef DEBUG
2151: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 2152: #endif
2153: }
1.162 brouard 2154: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 2155: #ifdef DEBUG
1.224 brouard 2156: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2157: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 2158: #endif
1.126 brouard 2159: u=ulim;
2160: fu=(*func)(u);
1.183 brouard 2161: } else { /* u could be left to b (if r > q parabola has a maximum) */
2162: #ifdef DEBUG
1.224 brouard 2163: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2164: 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 2165: #endif
1.126 brouard 2166: u=(*cx)+GOLD*(*cx-*bx);
2167: fu=(*func)(u);
1.224 brouard 2168: #ifdef DEBUG
2169: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2170: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2171: #endif
1.183 brouard 2172: } /* end tests */
1.126 brouard 2173: SHFT(*ax,*bx,*cx,u)
1.183 brouard 2174: SHFT(*fa,*fb,*fc,fu)
2175: #ifdef DEBUG
1.224 brouard 2176: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2177: 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 2178: #endif
2179: } /* 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 2180: }
2181:
2182: /*************** linmin ************************/
1.162 brouard 2183: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2184: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2185: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2186: the value of func at the returned location p . This is actually all accomplished by calling the
2187: routines mnbrak and brent .*/
1.126 brouard 2188: int ncom;
2189: double *pcom,*xicom;
2190: double (*nrfunc)(double []);
2191:
1.224 brouard 2192: #ifdef LINMINORIGINAL
1.126 brouard 2193: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 2194: #else
2195: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2196: #endif
1.126 brouard 2197: {
2198: double brent(double ax, double bx, double cx,
2199: double (*f)(double), double tol, double *xmin);
2200: double f1dim(double x);
2201: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2202: double *fc, double (*func)(double));
2203: int j;
2204: double xx,xmin,bx,ax;
2205: double fx,fb,fa;
1.187 brouard 2206:
1.203 brouard 2207: #ifdef LINMINORIGINAL
2208: #else
2209: double scale=10., axs, xxs; /* Scale added for infinity */
2210: #endif
2211:
1.126 brouard 2212: ncom=n;
2213: pcom=vector(1,n);
2214: xicom=vector(1,n);
2215: nrfunc=func;
2216: for (j=1;j<=n;j++) {
2217: pcom[j]=p[j];
1.202 brouard 2218: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 2219: }
1.187 brouard 2220:
1.203 brouard 2221: #ifdef LINMINORIGINAL
2222: xx=1.;
2223: #else
2224: axs=0.0;
2225: xxs=1.;
2226: do{
2227: xx= xxs;
2228: #endif
1.187 brouard 2229: ax=0.;
2230: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2231: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2232: /* 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)) */
2233: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2234: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2235: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2236: /* 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 2237: #ifdef LINMINORIGINAL
2238: #else
2239: if (fx != fx){
1.224 brouard 2240: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2241: printf("|");
2242: fprintf(ficlog,"|");
1.203 brouard 2243: #ifdef DEBUGLINMIN
1.224 brouard 2244: 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 2245: #endif
2246: }
1.224 brouard 2247: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 2248: #endif
2249:
1.191 brouard 2250: #ifdef DEBUGLINMIN
2251: 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 2252: 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 2253: #endif
1.224 brouard 2254: #ifdef LINMINORIGINAL
2255: #else
2256: if(fb == fx){ /* Flat function in the direction */
2257: xmin=xx;
2258: *flat=1;
2259: }else{
2260: *flat=0;
2261: #endif
2262: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2263: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2264: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2265: /* fmin = f(p[j] + xmin * xi[j]) */
2266: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2267: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2268: #ifdef DEBUG
1.224 brouard 2269: 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);
2270: 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);
2271: #endif
2272: #ifdef LINMINORIGINAL
2273: #else
2274: }
1.126 brouard 2275: #endif
1.191 brouard 2276: #ifdef DEBUGLINMIN
2277: printf("linmin end ");
1.202 brouard 2278: fprintf(ficlog,"linmin end ");
1.191 brouard 2279: #endif
1.126 brouard 2280: for (j=1;j<=n;j++) {
1.203 brouard 2281: #ifdef LINMINORIGINAL
2282: xi[j] *= xmin;
2283: #else
2284: #ifdef DEBUGLINMIN
2285: if(xxs <1.0)
2286: printf(" before xi[%d]=%12.8f", j,xi[j]);
2287: #endif
2288: 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) */
2289: #ifdef DEBUGLINMIN
2290: if(xxs <1.0)
2291: 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 );
2292: #endif
2293: #endif
1.187 brouard 2294: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2295: }
1.191 brouard 2296: #ifdef DEBUGLINMIN
1.203 brouard 2297: printf("\n");
1.191 brouard 2298: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2299: 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 2300: for (j=1;j<=n;j++) {
1.202 brouard 2301: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2302: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2303: if(j % ncovmodel == 0){
1.191 brouard 2304: printf("\n");
1.202 brouard 2305: fprintf(ficlog,"\n");
2306: }
1.191 brouard 2307: }
1.203 brouard 2308: #else
1.191 brouard 2309: #endif
1.126 brouard 2310: free_vector(xicom,1,n);
2311: free_vector(pcom,1,n);
2312: }
2313:
2314:
2315: /*************** powell ************************/
1.162 brouard 2316: /*
2317: Minimization of a function func of n variables. Input consists of an initial starting point
2318: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
2319: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
2320: such that failure to decrease by more than this amount on one iteration signals doneness. On
2321: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2322: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2323: */
1.224 brouard 2324: #ifdef LINMINORIGINAL
2325: #else
2326: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2327: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2328: #endif
1.126 brouard 2329: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2330: double (*func)(double []))
2331: {
1.224 brouard 2332: #ifdef LINMINORIGINAL
2333: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2334: double (*func)(double []));
1.224 brouard 2335: #else
1.241 brouard 2336: void linmin(double p[], double xi[], int n, double *fret,
2337: double (*func)(double []),int *flat);
1.224 brouard 2338: #endif
1.239 brouard 2339: int i,ibig,j,jk,k;
1.126 brouard 2340: double del,t,*pt,*ptt,*xit;
1.181 brouard 2341: double directest;
1.126 brouard 2342: double fp,fptt;
2343: double *xits;
2344: int niterf, itmp;
1.224 brouard 2345: #ifdef LINMINORIGINAL
2346: #else
2347:
2348: flatdir=ivector(1,n);
2349: for (j=1;j<=n;j++) flatdir[j]=0;
2350: #endif
1.126 brouard 2351:
2352: pt=vector(1,n);
2353: ptt=vector(1,n);
2354: xit=vector(1,n);
2355: xits=vector(1,n);
2356: *fret=(*func)(p);
2357: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2358: rcurr_time = time(NULL);
1.126 brouard 2359: for (*iter=1;;++(*iter)) {
1.187 brouard 2360: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2361: ibig=0;
2362: del=0.0;
1.157 brouard 2363: rlast_time=rcurr_time;
2364: /* (void) gettimeofday(&curr_time,&tzp); */
2365: rcurr_time = time(NULL);
2366: curr_time = *localtime(&rcurr_time);
2367: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2368: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2369: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2370: for (i=1;i<=n;i++) {
1.126 brouard 2371: fprintf(ficrespow," %.12lf", p[i]);
2372: }
1.239 brouard 2373: fprintf(ficrespow,"\n");fflush(ficrespow);
2374: printf("\n#model= 1 + age ");
2375: fprintf(ficlog,"\n#model= 1 + age ");
2376: if(nagesqr==1){
1.241 brouard 2377: printf(" + age*age ");
2378: fprintf(ficlog," + age*age ");
1.239 brouard 2379: }
2380: for(j=1;j <=ncovmodel-2;j++){
2381: if(Typevar[j]==0) {
2382: printf(" + V%d ",Tvar[j]);
2383: fprintf(ficlog," + V%d ",Tvar[j]);
2384: }else if(Typevar[j]==1) {
2385: printf(" + V%d*age ",Tvar[j]);
2386: fprintf(ficlog," + V%d*age ",Tvar[j]);
2387: }else if(Typevar[j]==2) {
2388: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2389: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2390: }
2391: }
1.126 brouard 2392: printf("\n");
1.239 brouard 2393: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
2394: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 2395: fprintf(ficlog,"\n");
1.239 brouard 2396: for(i=1,jk=1; i <=nlstate; i++){
2397: for(k=1; k <=(nlstate+ndeath); k++){
2398: if (k != i) {
2399: printf("%d%d ",i,k);
2400: fprintf(ficlog,"%d%d ",i,k);
2401: for(j=1; j <=ncovmodel; j++){
2402: printf("%12.7f ",p[jk]);
2403: fprintf(ficlog,"%12.7f ",p[jk]);
2404: jk++;
2405: }
2406: printf("\n");
2407: fprintf(ficlog,"\n");
2408: }
2409: }
2410: }
1.241 brouard 2411: if(*iter <=3 && *iter >1){
1.157 brouard 2412: tml = *localtime(&rcurr_time);
2413: strcpy(strcurr,asctime(&tml));
2414: rforecast_time=rcurr_time;
1.126 brouard 2415: itmp = strlen(strcurr);
2416: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 2417: strcurr[itmp-1]='\0';
1.162 brouard 2418: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2419: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2420: for(niterf=10;niterf<=30;niterf+=10){
1.241 brouard 2421: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2422: forecast_time = *localtime(&rforecast_time);
2423: strcpy(strfor,asctime(&forecast_time));
2424: itmp = strlen(strfor);
2425: if(strfor[itmp-1]=='\n')
2426: strfor[itmp-1]='\0';
2427: 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);
2428: 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 2429: }
2430: }
1.187 brouard 2431: for (i=1;i<=n;i++) { /* For each direction i */
2432: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2433: fptt=(*fret);
2434: #ifdef DEBUG
1.203 brouard 2435: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2436: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2437: #endif
1.203 brouard 2438: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2439: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2440: #ifdef LINMINORIGINAL
1.188 brouard 2441: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2442: #else
2443: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2444: flatdir[i]=flat; /* Function is vanishing in that direction i */
2445: #endif
2446: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2447: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2448: /* because that direction will be replaced unless the gain del is small */
2449: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2450: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2451: /* with the new direction. */
2452: del=fabs(fptt-(*fret));
2453: ibig=i;
1.126 brouard 2454: }
2455: #ifdef DEBUG
2456: printf("%d %.12e",i,(*fret));
2457: fprintf(ficlog,"%d %.12e",i,(*fret));
2458: for (j=1;j<=n;j++) {
1.224 brouard 2459: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2460: printf(" x(%d)=%.12e",j,xit[j]);
2461: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2462: }
2463: for(j=1;j<=n;j++) {
1.225 brouard 2464: printf(" p(%d)=%.12e",j,p[j]);
2465: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2466: }
2467: printf("\n");
2468: fprintf(ficlog,"\n");
2469: #endif
1.187 brouard 2470: } /* end loop on each direction i */
2471: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2472: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2473: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2474: for(j=1;j<=n;j++) {
1.302 brouard 2475: if(flatdir[j] >0){
2476: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2477: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2478: }
2479: /* printf("\n"); */
2480: /* fprintf(ficlog,"\n"); */
2481: }
1.243 brouard 2482: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
2483: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 2484: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2485: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2486: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2487: /* decreased of more than 3.84 */
2488: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2489: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2490: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2491:
1.188 brouard 2492: /* Starting the program with initial values given by a former maximization will simply change */
2493: /* the scales of the directions and the directions, because the are reset to canonical directions */
2494: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2495: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2496: #ifdef DEBUG
2497: int k[2],l;
2498: k[0]=1;
2499: k[1]=-1;
2500: printf("Max: %.12e",(*func)(p));
2501: fprintf(ficlog,"Max: %.12e",(*func)(p));
2502: for (j=1;j<=n;j++) {
2503: printf(" %.12e",p[j]);
2504: fprintf(ficlog," %.12e",p[j]);
2505: }
2506: printf("\n");
2507: fprintf(ficlog,"\n");
2508: for(l=0;l<=1;l++) {
2509: for (j=1;j<=n;j++) {
2510: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2511: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2512: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2513: }
2514: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2515: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2516: }
2517: #endif
2518:
1.224 brouard 2519: #ifdef LINMINORIGINAL
2520: #else
2521: free_ivector(flatdir,1,n);
2522: #endif
1.126 brouard 2523: free_vector(xit,1,n);
2524: free_vector(xits,1,n);
2525: free_vector(ptt,1,n);
2526: free_vector(pt,1,n);
2527: return;
1.192 brouard 2528: } /* enough precision */
1.240 brouard 2529: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2530: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2531: ptt[j]=2.0*p[j]-pt[j];
2532: xit[j]=p[j]-pt[j];
2533: pt[j]=p[j];
2534: }
1.181 brouard 2535: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2536: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2537: if (*iter <=4) {
1.225 brouard 2538: #else
2539: #endif
1.224 brouard 2540: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2541: #else
1.161 brouard 2542: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2543: #endif
1.162 brouard 2544: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2545: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2546: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2547: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2548: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2549: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2550: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2551: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2552: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2553: /* Even if f3 <f1, directest can be negative and t >0 */
2554: /* mu² and del² are equal when f3=f1 */
2555: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2556: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2557: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2558: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2559: #ifdef NRCORIGINAL
2560: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2561: #else
2562: 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 2563: t= t- del*SQR(fp-fptt);
1.183 brouard 2564: #endif
1.202 brouard 2565: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2566: #ifdef DEBUG
1.181 brouard 2567: 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);
2568: 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 2569: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2570: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2571: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2572: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2573: 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);
2574: 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);
2575: #endif
1.183 brouard 2576: #ifdef POWELLORIGINAL
2577: if (t < 0.0) { /* Then we use it for new direction */
2578: #else
1.182 brouard 2579: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2580: 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 2581: 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 2582: 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 2583: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2584: }
1.181 brouard 2585: if (directest < 0.0) { /* Then we use it for new direction */
2586: #endif
1.191 brouard 2587: #ifdef DEBUGLINMIN
1.234 brouard 2588: printf("Before linmin in direction P%d-P0\n",n);
2589: for (j=1;j<=n;j++) {
2590: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2591: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2592: if(j % ncovmodel == 0){
2593: printf("\n");
2594: fprintf(ficlog,"\n");
2595: }
2596: }
1.224 brouard 2597: #endif
2598: #ifdef LINMINORIGINAL
1.234 brouard 2599: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2600: #else
1.234 brouard 2601: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2602: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2603: #endif
1.234 brouard 2604:
1.191 brouard 2605: #ifdef DEBUGLINMIN
1.234 brouard 2606: for (j=1;j<=n;j++) {
2607: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2608: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2609: if(j % ncovmodel == 0){
2610: printf("\n");
2611: fprintf(ficlog,"\n");
2612: }
2613: }
1.224 brouard 2614: #endif
1.234 brouard 2615: for (j=1;j<=n;j++) {
2616: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2617: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2618: }
1.224 brouard 2619: #ifdef LINMINORIGINAL
2620: #else
1.234 brouard 2621: for (j=1, flatd=0;j<=n;j++) {
2622: if(flatdir[j]>0)
2623: flatd++;
2624: }
2625: if(flatd >0){
1.255 brouard 2626: printf("%d flat directions: ",flatd);
2627: fprintf(ficlog,"%d flat directions :",flatd);
1.234 brouard 2628: for (j=1;j<=n;j++) {
2629: if(flatdir[j]>0){
2630: printf("%d ",j);
2631: fprintf(ficlog,"%d ",j);
2632: }
2633: }
2634: printf("\n");
2635: fprintf(ficlog,"\n");
2636: }
1.191 brouard 2637: #endif
1.234 brouard 2638: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2639: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2640:
1.126 brouard 2641: #ifdef DEBUG
1.234 brouard 2642: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2643: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2644: for(j=1;j<=n;j++){
2645: printf(" %lf",xit[j]);
2646: fprintf(ficlog," %lf",xit[j]);
2647: }
2648: printf("\n");
2649: fprintf(ficlog,"\n");
1.126 brouard 2650: #endif
1.192 brouard 2651: } /* end of t or directest negative */
1.224 brouard 2652: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2653: #else
1.234 brouard 2654: } /* end if (fptt < fp) */
1.192 brouard 2655: #endif
1.225 brouard 2656: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 2657: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2658: #else
1.224 brouard 2659: #endif
1.234 brouard 2660: } /* loop iteration */
1.126 brouard 2661: }
1.234 brouard 2662:
1.126 brouard 2663: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 2664:
1.235 brouard 2665: 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 2666: {
1.279 brouard 2667: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij
2668: * (and selected quantitative values in nres)
2669: * by left multiplying the unit
2670: * matrix by transitions matrix until convergence is reached with precision ftolpl
2671: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
2672: * Wx is row vector: population in state 1, population in state 2, population dead
2673: * or prevalence in state 1, prevalence in state 2, 0
2674: * newm is the matrix after multiplications, its rows are identical at a factor.
2675: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
2676: * Output is prlim.
2677: * Initial matrix pimij
2678: */
1.206 brouard 2679: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2680: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2681: /* 0, 0 , 1} */
2682: /*
2683: * and after some iteration: */
2684: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2685: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2686: /* 0, 0 , 1} */
2687: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2688: /* {0.51571254859325999, 0.4842874514067399, */
2689: /* 0.51326036147820708, 0.48673963852179264} */
2690: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 2691:
1.126 brouard 2692: int i, ii,j,k;
1.209 brouard 2693: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2694: /* double **matprod2(); */ /* test */
1.218 brouard 2695: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2696: double **newm;
1.209 brouard 2697: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2698: int ncvloop=0;
1.288 brouard 2699: int first=0;
1.169 brouard 2700:
1.209 brouard 2701: min=vector(1,nlstate);
2702: max=vector(1,nlstate);
2703: meandiff=vector(1,nlstate);
2704:
1.218 brouard 2705: /* Starting with matrix unity */
1.126 brouard 2706: for (ii=1;ii<=nlstate+ndeath;ii++)
2707: for (j=1;j<=nlstate+ndeath;j++){
2708: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2709: }
1.169 brouard 2710:
2711: cov[1]=1.;
2712:
2713: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2714: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2715: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2716: ncvloop++;
1.126 brouard 2717: newm=savm;
2718: /* Covariates have to be included here again */
1.138 brouard 2719: cov[2]=agefin;
1.187 brouard 2720: if(nagesqr==1)
2721: cov[3]= agefin*agefin;;
1.234 brouard 2722: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2723: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2724: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.235 brouard 2725: /* 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 2726: }
2727: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2728: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.235 brouard 2729: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2730: /* 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 2731: }
1.237 brouard 2732: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.234 brouard 2733: if(Dummy[Tvar[Tage[k]]]){
2734: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2735: } else{
1.235 brouard 2736: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.234 brouard 2737: }
1.235 brouard 2738: /* 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 2739: }
1.237 brouard 2740: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 2741: /* 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 2742: if(Dummy[Tvard[k][1]==0]){
2743: if(Dummy[Tvard[k][2]==0]){
2744: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2745: }else{
2746: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2747: }
2748: }else{
2749: if(Dummy[Tvard[k][2]==0]){
2750: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2751: }else{
2752: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2753: }
2754: }
1.234 brouard 2755: }
1.138 brouard 2756: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2757: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2758: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2759: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2760: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2761: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2762: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2763:
1.126 brouard 2764: savm=oldm;
2765: oldm=newm;
1.209 brouard 2766:
2767: for(j=1; j<=nlstate; j++){
2768: max[j]=0.;
2769: min[j]=1.;
2770: }
2771: for(i=1;i<=nlstate;i++){
2772: sumnew=0;
2773: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2774: for(j=1; j<=nlstate; j++){
2775: prlim[i][j]= newm[i][j]/(1-sumnew);
2776: max[j]=FMAX(max[j],prlim[i][j]);
2777: min[j]=FMIN(min[j],prlim[i][j]);
2778: }
2779: }
2780:
1.126 brouard 2781: maxmax=0.;
1.209 brouard 2782: for(j=1; j<=nlstate; j++){
2783: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2784: maxmax=FMAX(maxmax,meandiff[j]);
2785: /* 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 2786: } /* j loop */
1.203 brouard 2787: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2788: /* 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 2789: if(maxmax < ftolpl){
1.209 brouard 2790: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2791: free_vector(min,1,nlstate);
2792: free_vector(max,1,nlstate);
2793: free_vector(meandiff,1,nlstate);
1.126 brouard 2794: return prlim;
2795: }
1.288 brouard 2796: } /* agefin loop */
1.208 brouard 2797: /* After some age loop it doesn't converge */
1.288 brouard 2798: if(!first){
2799: first=1;
2800: 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);
2801: }
2802: 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);
2803:
1.209 brouard 2804: /* 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); */
2805: free_vector(min,1,nlstate);
2806: free_vector(max,1,nlstate);
2807: free_vector(meandiff,1,nlstate);
1.208 brouard 2808:
1.169 brouard 2809: return prlim; /* should not reach here */
1.126 brouard 2810: }
2811:
1.217 brouard 2812:
2813: /**** Back Prevalence limit (stable or period prevalence) ****************/
2814:
1.218 brouard 2815: /* 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) */
2816: /* 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 2817: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 2818: {
1.264 brouard 2819: /* 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 2820: matrix by transitions matrix until convergence is reached with precision ftolpl */
2821: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2822: /* Wx is row vector: population in state 1, population in state 2, population dead */
2823: /* or prevalence in state 1, prevalence in state 2, 0 */
2824: /* newm is the matrix after multiplications, its rows are identical at a factor */
2825: /* Initial matrix pimij */
2826: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2827: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2828: /* 0, 0 , 1} */
2829: /*
2830: * and after some iteration: */
2831: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2832: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2833: /* 0, 0 , 1} */
2834: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2835: /* {0.51571254859325999, 0.4842874514067399, */
2836: /* 0.51326036147820708, 0.48673963852179264} */
2837: /* If we start from prlim again, prlim tends to a constant matrix */
2838:
2839: int i, ii,j,k;
1.247 brouard 2840: int first=0;
1.217 brouard 2841: double *min, *max, *meandiff, maxmax,sumnew=0.;
2842: /* double **matprod2(); */ /* test */
2843: double **out, cov[NCOVMAX+1], **bmij();
2844: double **newm;
1.218 brouard 2845: double **dnewm, **doldm, **dsavm; /* for use */
2846: double **oldm, **savm; /* for use */
2847:
1.217 brouard 2848: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2849: int ncvloop=0;
2850:
2851: min=vector(1,nlstate);
2852: max=vector(1,nlstate);
2853: meandiff=vector(1,nlstate);
2854:
1.266 brouard 2855: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2856: oldm=oldms; savm=savms;
2857:
2858: /* Starting with matrix unity */
2859: for (ii=1;ii<=nlstate+ndeath;ii++)
2860: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2861: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2862: }
2863:
2864: cov[1]=1.;
2865:
2866: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2867: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2868: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
1.288 brouard 2869: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2870: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2871: ncvloop++;
1.218 brouard 2872: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2873: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2874: /* Covariates have to be included here again */
2875: cov[2]=agefin;
2876: if(nagesqr==1)
2877: cov[3]= agefin*agefin;;
1.242 brouard 2878: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2879: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2880: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.264 brouard 2881: /* 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 2882: }
2883: /* for (k=1; k<=cptcovn;k++) { */
2884: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
2885: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
2886: /* /\* 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])]); *\/ */
2887: /* } */
2888: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2889: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
2890: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2891: /* 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]); */
2892: }
2893: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; */
2894: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */
2895: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\/ */
2896: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
2897: for (k=1; k<=cptcovage;k++){ /* For product with age */
2898: if(Dummy[Tvar[Tage[k]]]){
2899: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2900: } else{
2901: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
2902: }
2903: /* 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]); */
2904: }
2905: for (k=1; k<=cptcovprod;k++){ /* For product without age */
2906: /* 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]); */
2907: if(Dummy[Tvard[k][1]==0]){
2908: if(Dummy[Tvard[k][2]==0]){
2909: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2910: }else{
2911: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2912: }
2913: }else{
2914: if(Dummy[Tvard[k][2]==0]){
2915: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2916: }else{
2917: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2918: }
2919: }
1.217 brouard 2920: }
2921:
2922: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2923: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2924: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2925: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2926: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2927: /* ij should be linked to the correct index of cov */
2928: /* age and covariate values ij are in 'cov', but we need to pass
2929: * ij for the observed prevalence at age and status and covariate
2930: * number: prevacurrent[(int)agefin][ii][ij]
2931: */
2932: /* 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 *\/ */
2933: /* 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 *\/ */
2934: 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 2935: /* if((int)age == 86 || (int)age == 87){ */
1.266 brouard 2936: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
2937: /* for(i=1; i<=nlstate+ndeath; i++) { */
2938: /* printf("%d newm= ",i); */
2939: /* for(j=1;j<=nlstate+ndeath;j++) { */
2940: /* printf("%f ",newm[i][j]); */
2941: /* } */
2942: /* printf("oldm * "); */
2943: /* for(j=1;j<=nlstate+ndeath;j++) { */
2944: /* printf("%f ",oldm[i][j]); */
2945: /* } */
1.268 brouard 2946: /* printf(" bmmij "); */
1.266 brouard 2947: /* for(j=1;j<=nlstate+ndeath;j++) { */
2948: /* printf("%f ",pmmij[i][j]); */
2949: /* } */
2950: /* printf("\n"); */
2951: /* } */
2952: /* } */
1.217 brouard 2953: savm=oldm;
2954: oldm=newm;
1.266 brouard 2955:
1.217 brouard 2956: for(j=1; j<=nlstate; j++){
2957: max[j]=0.;
2958: min[j]=1.;
2959: }
2960: for(j=1; j<=nlstate; j++){
2961: for(i=1;i<=nlstate;i++){
1.234 brouard 2962: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2963: bprlim[i][j]= newm[i][j];
2964: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2965: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2966: }
2967: }
1.218 brouard 2968:
1.217 brouard 2969: maxmax=0.;
2970: for(i=1; i<=nlstate; i++){
2971: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2972: maxmax=FMAX(maxmax,meandiff[i]);
2973: /* 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 2974: } /* i loop */
1.217 brouard 2975: *ncvyear= -( (int)age- (int)agefin);
1.268 brouard 2976: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2977: if(maxmax < ftolpl){
1.220 brouard 2978: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2979: free_vector(min,1,nlstate);
2980: free_vector(max,1,nlstate);
2981: free_vector(meandiff,1,nlstate);
2982: return bprlim;
2983: }
1.288 brouard 2984: } /* agefin loop */
1.217 brouard 2985: /* After some age loop it doesn't converge */
1.288 brouard 2986: if(!first){
1.247 brouard 2987: first=1;
2988: 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\
2989: 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);
2990: }
2991: 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 2992: 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);
2993: /* 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); */
2994: free_vector(min,1,nlstate);
2995: free_vector(max,1,nlstate);
2996: free_vector(meandiff,1,nlstate);
2997:
2998: return bprlim; /* should not reach here */
2999: }
3000:
1.126 brouard 3001: /*************** transition probabilities ***************/
3002:
3003: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
3004: {
1.138 brouard 3005: /* According to parameters values stored in x and the covariate's values stored in cov,
1.266 brouard 3006: computes the probability to be observed in state j (after stepm years) being in state i by appying the
1.138 brouard 3007: model to the ncovmodel covariates (including constant and age).
3008: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3009: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3010: ncth covariate in the global vector x is given by the formula:
3011: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3012: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3013: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3014: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1.266 brouard 3015: Outputs ps[i][j] or probability to be observed in j being in i according to
1.138 brouard 3016: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1.266 brouard 3017: Sum on j ps[i][j] should equal to 1.
1.138 brouard 3018: */
3019: double s1, lnpijopii;
1.126 brouard 3020: /*double t34;*/
1.164 brouard 3021: int i,j, nc, ii, jj;
1.126 brouard 3022:
1.223 brouard 3023: for(i=1; i<= nlstate; i++){
3024: for(j=1; j<i;j++){
3025: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3026: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3027: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3028: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3029: }
3030: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3031: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3032: }
3033: for(j=i+1; j<=nlstate+ndeath;j++){
3034: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3035: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3036: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3037: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3038: }
3039: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3040: }
3041: }
1.218 brouard 3042:
1.223 brouard 3043: for(i=1; i<= nlstate; i++){
3044: s1=0;
3045: for(j=1; j<i; j++){
3046: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3047: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3048: }
3049: for(j=i+1; j<=nlstate+ndeath; j++){
3050: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3051: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3052: }
3053: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3054: ps[i][i]=1./(s1+1.);
3055: /* Computing other pijs */
3056: for(j=1; j<i; j++)
3057: ps[i][j]= exp(ps[i][j])*ps[i][i];
3058: for(j=i+1; j<=nlstate+ndeath; j++)
3059: ps[i][j]= exp(ps[i][j])*ps[i][i];
3060: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3061: } /* end i */
1.218 brouard 3062:
1.223 brouard 3063: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3064: for(jj=1; jj<= nlstate+ndeath; jj++){
3065: ps[ii][jj]=0;
3066: ps[ii][ii]=1;
3067: }
3068: }
1.294 brouard 3069:
3070:
1.223 brouard 3071: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3072: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3073: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3074: /* } */
3075: /* printf("\n "); */
3076: /* } */
3077: /* printf("\n ");printf("%lf ",cov[2]);*/
3078: /*
3079: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 3080: goto end;*/
1.266 brouard 3081: return ps; /* Pointer is unchanged since its call */
1.126 brouard 3082: }
3083:
1.218 brouard 3084: /*************** backward transition probabilities ***************/
3085:
3086: /* 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 ) */
3087: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
3088: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
3089: {
1.302 brouard 3090: /* 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 3091: * 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 3092: */
1.218 brouard 3093: int i, ii, j,k;
1.222 brouard 3094:
3095: double **out, **pmij();
3096: double sumnew=0.;
1.218 brouard 3097: double agefin;
1.292 brouard 3098: 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 3099: double **dnewm, **dsavm, **doldm;
3100: double **bbmij;
3101:
1.218 brouard 3102: doldm=ddoldms; /* global pointers */
1.222 brouard 3103: dnewm=ddnewms;
3104: dsavm=ddsavms;
3105:
3106: agefin=cov[2];
1.268 brouard 3107: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
1.222 brouard 3108: /* bmij *//* age is cov[2], ij is included in cov, but we need for
1.266 brouard 3109: the observed prevalence (with this covariate ij) at beginning of transition */
3110: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.268 brouard 3111:
3112: /* P_x */
1.266 brouard 3113: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm */
1.268 brouard 3114: /* outputs pmmij which is a stochastic matrix in row */
3115:
3116: /* Diag(w_x) */
1.292 brouard 3117: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
1.268 brouard 3118: sumnew=0.;
1.269 brouard 3119: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
1.268 brouard 3120: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
1.297 brouard 3121: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
1.268 brouard 3122: sumnew+=prevacurrent[(int)agefin][ii][ij];
3123: }
3124: if(sumnew >0.01){ /* At least some value in the prevalence */
3125: for (ii=1;ii<=nlstate+ndeath;ii++){
3126: for (j=1;j<=nlstate+ndeath;j++)
1.269 brouard 3127: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
1.268 brouard 3128: }
3129: }else{
3130: for (ii=1;ii<=nlstate+ndeath;ii++){
3131: for (j=1;j<=nlstate+ndeath;j++)
3132: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
3133: }
3134: /* if(sumnew <0.9){ */
3135: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
3136: /* } */
3137: }
3138: k3=0.0; /* We put the last diagonal to 0 */
3139: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
3140: doldm[ii][ii]= k3;
3141: }
3142: /* End doldm, At the end doldm is diag[(w_i)] */
3143:
1.292 brouard 3144: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
3145: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
1.268 brouard 3146:
1.292 brouard 3147: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
1.268 brouard 3148: /* 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 3149: for (j=1;j<=nlstate+ndeath;j++){
1.268 brouard 3150: sumnew=0.;
1.222 brouard 3151: for (ii=1;ii<=nlstate;ii++){
1.266 brouard 3152: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
1.268 brouard 3153: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
1.222 brouard 3154: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
1.268 brouard 3155: for (ii=1;ii<=nlstate+ndeath;ii++){
1.222 brouard 3156: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
1.268 brouard 3157: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3158: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
1.268 brouard 3159: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3160: /* }else */
1.268 brouard 3161: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
3162: } /*End ii */
3163: } /* 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 */
3164:
1.292 brouard 3165: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
1.268 brouard 3166: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
1.222 brouard 3167: /* end bmij */
1.266 brouard 3168: return ps; /*pointer is unchanged */
1.218 brouard 3169: }
1.217 brouard 3170: /*************** transition probabilities ***************/
3171:
1.218 brouard 3172: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 3173: {
3174: /* According to parameters values stored in x and the covariate's values stored in cov,
3175: computes the probability to be observed in state j being in state i by appying the
3176: model to the ncovmodel covariates (including constant and age).
3177: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3178: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3179: ncth covariate in the global vector x is given by the formula:
3180: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3181: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3182: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3183: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3184: Outputs ps[i][j] the probability to be observed in j being in j according to
3185: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3186: */
3187: double s1, lnpijopii;
3188: /*double t34;*/
3189: int i,j, nc, ii, jj;
3190:
1.234 brouard 3191: for(i=1; i<= nlstate; i++){
3192: for(j=1; j<i;j++){
3193: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3194: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3195: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3196: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3197: }
3198: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3199: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3200: }
3201: for(j=i+1; j<=nlstate+ndeath;j++){
3202: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3203: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3204: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3205: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3206: }
3207: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3208: }
3209: }
3210:
3211: for(i=1; i<= nlstate; i++){
3212: s1=0;
3213: for(j=1; j<i; j++){
3214: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3215: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3216: }
3217: for(j=i+1; j<=nlstate+ndeath; j++){
3218: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3219: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3220: }
3221: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3222: ps[i][i]=1./(s1+1.);
3223: /* Computing other pijs */
3224: for(j=1; j<i; j++)
3225: ps[i][j]= exp(ps[i][j])*ps[i][i];
3226: for(j=i+1; j<=nlstate+ndeath; j++)
3227: ps[i][j]= exp(ps[i][j])*ps[i][i];
3228: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3229: } /* end i */
3230:
3231: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3232: for(jj=1; jj<= nlstate+ndeath; jj++){
3233: ps[ii][jj]=0;
3234: ps[ii][ii]=1;
3235: }
3236: }
1.296 brouard 3237: /* Added for prevbcast */ /* Transposed matrix too */
1.234 brouard 3238: for(jj=1; jj<= nlstate+ndeath; jj++){
3239: s1=0.;
3240: for(ii=1; ii<= nlstate+ndeath; ii++){
3241: s1+=ps[ii][jj];
3242: }
3243: for(ii=1; ii<= nlstate; ii++){
3244: ps[ii][jj]=ps[ii][jj]/s1;
3245: }
3246: }
3247: /* Transposition */
3248: for(jj=1; jj<= nlstate+ndeath; jj++){
3249: for(ii=jj; ii<= nlstate+ndeath; ii++){
3250: s1=ps[ii][jj];
3251: ps[ii][jj]=ps[jj][ii];
3252: ps[jj][ii]=s1;
3253: }
3254: }
3255: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3256: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3257: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3258: /* } */
3259: /* printf("\n "); */
3260: /* } */
3261: /* printf("\n ");printf("%lf ",cov[2]);*/
3262: /*
3263: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3264: goto end;*/
3265: return ps;
1.217 brouard 3266: }
3267:
3268:
1.126 brouard 3269: /**************** Product of 2 matrices ******************/
3270:
1.145 brouard 3271: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 3272: {
3273: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
3274: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
3275: /* in, b, out are matrice of pointers which should have been initialized
3276: before: only the contents of out is modified. The function returns
3277: a pointer to pointers identical to out */
1.145 brouard 3278: int i, j, k;
1.126 brouard 3279: for(i=nrl; i<= nrh; i++)
1.145 brouard 3280: for(k=ncolol; k<=ncoloh; k++){
3281: out[i][k]=0.;
3282: for(j=ncl; j<=nch; j++)
3283: out[i][k] +=in[i][j]*b[j][k];
3284: }
1.126 brouard 3285: return out;
3286: }
3287:
3288:
3289: /************* Higher Matrix Product ***************/
3290:
1.235 brouard 3291: 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 3292: {
1.218 brouard 3293: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 3294: 'nhstepm*hstepm*stepm' months (i.e. until
3295: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3296: nhstepm*hstepm matrices.
3297: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3298: (typically every 2 years instead of every month which is too big
3299: for the memory).
3300: Model is determined by parameters x and covariates have to be
3301: included manually here.
3302:
3303: */
3304:
3305: int i, j, d, h, k;
1.131 brouard 3306: double **out, cov[NCOVMAX+1];
1.126 brouard 3307: double **newm;
1.187 brouard 3308: double agexact;
1.214 brouard 3309: double agebegin, ageend;
1.126 brouard 3310:
3311: /* Hstepm could be zero and should return the unit matrix */
3312: for (i=1;i<=nlstate+ndeath;i++)
3313: for (j=1;j<=nlstate+ndeath;j++){
3314: oldm[i][j]=(i==j ? 1.0 : 0.0);
3315: po[i][j][0]=(i==j ? 1.0 : 0.0);
3316: }
3317: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3318: for(h=1; h <=nhstepm; h++){
3319: for(d=1; d <=hstepm; d++){
3320: newm=savm;
3321: /* Covariates have to be included here again */
3322: cov[1]=1.;
1.214 brouard 3323: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 3324: cov[2]=agexact;
3325: if(nagesqr==1)
1.227 brouard 3326: cov[3]= agexact*agexact;
1.235 brouard 3327: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
3328: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
3329: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3330: /* 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)); */
3331: }
3332: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3333: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3334: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3335: /* 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]); */
3336: }
3337: for (k=1; k<=cptcovage;k++){
3338: if(Dummy[Tvar[Tage[k]]]){
3339: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3340: } else{
3341: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3342: }
3343: /* 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]); */
3344: }
3345: for (k=1; k<=cptcovprod;k++){ /* */
3346: /* 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]); */
3347: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
3348: }
3349: /* for (k=1; k<=cptcovn;k++) */
3350: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3351: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
3352: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
3353: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
3354: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 3355:
3356:
1.126 brouard 3357: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3358: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 3359: /* right multiplication of oldm by the current matrix */
1.126 brouard 3360: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
3361: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 3362: /* if((int)age == 70){ */
3363: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3364: /* for(i=1; i<=nlstate+ndeath; i++) { */
3365: /* printf("%d pmmij ",i); */
3366: /* for(j=1;j<=nlstate+ndeath;j++) { */
3367: /* printf("%f ",pmmij[i][j]); */
3368: /* } */
3369: /* printf(" oldm "); */
3370: /* for(j=1;j<=nlstate+ndeath;j++) { */
3371: /* printf("%f ",oldm[i][j]); */
3372: /* } */
3373: /* printf("\n"); */
3374: /* } */
3375: /* } */
1.126 brouard 3376: savm=oldm;
3377: oldm=newm;
3378: }
3379: for(i=1; i<=nlstate+ndeath; i++)
3380: for(j=1;j<=nlstate+ndeath;j++) {
1.267 brouard 3381: po[i][j][h]=newm[i][j];
3382: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 3383: }
1.128 brouard 3384: /*printf("h=%d ",h);*/
1.126 brouard 3385: } /* end h */
1.267 brouard 3386: /* printf("\n H=%d \n",h); */
1.126 brouard 3387: return po;
3388: }
3389:
1.217 brouard 3390: /************* Higher Back Matrix Product ***************/
1.218 brouard 3391: /* 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 3392: 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 3393: {
1.266 brouard 3394: /* For a combination of dummy covariate ij, computes the transition matrix starting at age 'age' over
1.217 brouard 3395: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 3396: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3397: nhstepm*hstepm matrices.
3398: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3399: (typically every 2 years instead of every month which is too big
1.217 brouard 3400: for the memory).
1.218 brouard 3401: Model is determined by parameters x and covariates have to be
1.266 brouard 3402: included manually here. Then we use a call to bmij(x and cov)
3403: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
1.222 brouard 3404: */
1.217 brouard 3405:
3406: int i, j, d, h, k;
1.266 brouard 3407: double **out, cov[NCOVMAX+1], **bmij();
3408: double **newm, ***newmm;
1.217 brouard 3409: double agexact;
3410: double agebegin, ageend;
1.222 brouard 3411: double **oldm, **savm;
1.217 brouard 3412:
1.266 brouard 3413: newmm=po; /* To be saved */
3414: oldm=oldms;savm=savms; /* Global pointers */
1.217 brouard 3415: /* Hstepm could be zero and should return the unit matrix */
3416: for (i=1;i<=nlstate+ndeath;i++)
3417: for (j=1;j<=nlstate+ndeath;j++){
3418: oldm[i][j]=(i==j ? 1.0 : 0.0);
3419: po[i][j][0]=(i==j ? 1.0 : 0.0);
3420: }
3421: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3422: for(h=1; h <=nhstepm; h++){
3423: for(d=1; d <=hstepm; d++){
3424: newm=savm;
3425: /* Covariates have to be included here again */
3426: cov[1]=1.;
1.271 brouard 3427: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
1.217 brouard 3428: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3429: cov[2]=agexact;
3430: if(nagesqr==1)
1.222 brouard 3431: cov[3]= agexact*agexact;
1.266 brouard 3432: for (k=1; k<=cptcovn;k++){
3433: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3434: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
3435: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3436: /* 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)); */
3437: }
1.267 brouard 3438: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3439: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3440: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3441: /* 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]); */
3442: }
3443: for (k=1; k<=cptcovage;k++){ /* Should start at cptcovn+1 */
3444: if(Dummy[Tvar[Tage[k]]]){
3445: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3446: } else{
3447: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3448: }
3449: /* 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]); */
3450: }
3451: for (k=1; k<=cptcovprod;k++){ /* Useless because included in cptcovn */
1.222 brouard 3452: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.267 brouard 3453: }
1.217 brouard 3454: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3455: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.267 brouard 3456:
1.218 brouard 3457: /* Careful transposed matrix */
1.266 brouard 3458: /* age is in cov[2], prevacurrent at beginning of transition. */
1.218 brouard 3459: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 3460: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 3461: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 3462: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 3463: /* if((int)age == 70){ */
3464: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3465: /* for(i=1; i<=nlstate+ndeath; i++) { */
3466: /* printf("%d pmmij ",i); */
3467: /* for(j=1;j<=nlstate+ndeath;j++) { */
3468: /* printf("%f ",pmmij[i][j]); */
3469: /* } */
3470: /* printf(" oldm "); */
3471: /* for(j=1;j<=nlstate+ndeath;j++) { */
3472: /* printf("%f ",oldm[i][j]); */
3473: /* } */
3474: /* printf("\n"); */
3475: /* } */
3476: /* } */
3477: savm=oldm;
3478: oldm=newm;
3479: }
3480: for(i=1; i<=nlstate+ndeath; i++)
3481: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3482: po[i][j][h]=newm[i][j];
1.268 brouard 3483: /* if(h==nhstepm) */
3484: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
1.217 brouard 3485: }
1.268 brouard 3486: /* printf("h=%d %.1f ",h, agexact); */
1.217 brouard 3487: } /* end h */
1.268 brouard 3488: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
1.217 brouard 3489: return po;
3490: }
3491:
3492:
1.162 brouard 3493: #ifdef NLOPT
3494: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3495: double fret;
3496: double *xt;
3497: int j;
3498: myfunc_data *d2 = (myfunc_data *) pd;
3499: /* xt = (p1-1); */
3500: xt=vector(1,n);
3501: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3502:
3503: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3504: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3505: printf("Function = %.12lf ",fret);
3506: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3507: printf("\n");
3508: free_vector(xt,1,n);
3509: return fret;
3510: }
3511: #endif
1.126 brouard 3512:
3513: /*************** log-likelihood *************/
3514: double func( double *x)
3515: {
1.226 brouard 3516: int i, ii, j, k, mi, d, kk;
3517: int ioffset=0;
3518: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3519: double **out;
3520: double lli; /* Individual log likelihood */
3521: int s1, s2;
1.228 brouard 3522: 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 3523: double bbh, survp;
3524: long ipmx;
3525: double agexact;
3526: /*extern weight */
3527: /* We are differentiating ll according to initial status */
3528: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3529: /*for(i=1;i<imx;i++)
3530: printf(" %d\n",s[4][i]);
3531: */
1.162 brouard 3532:
1.226 brouard 3533: ++countcallfunc;
1.162 brouard 3534:
1.226 brouard 3535: cov[1]=1.;
1.126 brouard 3536:
1.226 brouard 3537: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3538: ioffset=0;
1.226 brouard 3539: if(mle==1){
3540: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3541: /* Computes the values of the ncovmodel covariates of the model
3542: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3543: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3544: to be observed in j being in i according to the model.
3545: */
1.243 brouard 3546: ioffset=2+nagesqr ;
1.233 brouard 3547: /* Fixed */
1.234 brouard 3548: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3549: 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)*/
3550: }
1.226 brouard 3551: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3552: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3553: has been calculated etc */
3554: /* For an individual i, wav[i] gives the number of effective waves */
3555: /* We compute the contribution to Likelihood of each effective transition
3556: mw[mi][i] is real wave of the mi th effectve wave */
3557: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3558: s2=s[mw[mi+1][i]][i];
3559: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3560: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3561: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3562: */
3563: for(mi=1; mi<= wav[i]-1; mi++){
1.234 brouard 3564: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3565: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3566: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
1.234 brouard 3567: }
3568: for (ii=1;ii<=nlstate+ndeath;ii++)
3569: for (j=1;j<=nlstate+ndeath;j++){
3570: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3571: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3572: }
3573: for(d=0; d<dh[mi][i]; d++){
3574: newm=savm;
3575: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3576: cov[2]=agexact;
3577: if(nagesqr==1)
3578: cov[3]= agexact*agexact; /* Should be changed here */
3579: for (kk=1; kk<=cptcovage;kk++) {
1.242 brouard 3580: if(!FixedV[Tvar[Tage[kk]]])
1.234 brouard 3581: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
1.242 brouard 3582: else
3583: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
1.234 brouard 3584: }
3585: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3586: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3587: savm=oldm;
3588: oldm=newm;
3589: } /* end mult */
3590:
3591: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3592: /* But now since version 0.9 we anticipate for bias at large stepm.
3593: * If stepm is larger than one month (smallest stepm) and if the exact delay
3594: * (in months) between two waves is not a multiple of stepm, we rounded to
3595: * the nearest (and in case of equal distance, to the lowest) interval but now
3596: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3597: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3598: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3599: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3600: * -stepm/2 to stepm/2 .
3601: * For stepm=1 the results are the same as for previous versions of Imach.
3602: * For stepm > 1 the results are less biased than in previous versions.
3603: */
1.234 brouard 3604: s1=s[mw[mi][i]][i];
3605: s2=s[mw[mi+1][i]][i];
3606: bbh=(double)bh[mi][i]/(double)stepm;
3607: /* bias bh is positive if real duration
3608: * is higher than the multiple of stepm and negative otherwise.
3609: */
3610: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3611: if( s2 > nlstate){
3612: /* i.e. if s2 is a death state and if the date of death is known
3613: then the contribution to the likelihood is the probability to
3614: die between last step unit time and current step unit time,
3615: which is also equal to probability to die before dh
3616: minus probability to die before dh-stepm .
3617: In version up to 0.92 likelihood was computed
3618: as if date of death was unknown. Death was treated as any other
3619: health state: the date of the interview describes the actual state
3620: and not the date of a change in health state. The former idea was
3621: to consider that at each interview the state was recorded
3622: (healthy, disable or death) and IMaCh was corrected; but when we
3623: introduced the exact date of death then we should have modified
3624: the contribution of an exact death to the likelihood. This new
3625: contribution is smaller and very dependent of the step unit
3626: stepm. It is no more the probability to die between last interview
3627: and month of death but the probability to survive from last
3628: interview up to one month before death multiplied by the
3629: probability to die within a month. Thanks to Chris
3630: Jackson for correcting this bug. Former versions increased
3631: mortality artificially. The bad side is that we add another loop
3632: which slows down the processing. The difference can be up to 10%
3633: lower mortality.
3634: */
3635: /* If, at the beginning of the maximization mostly, the
3636: cumulative probability or probability to be dead is
3637: constant (ie = 1) over time d, the difference is equal to
3638: 0. out[s1][3] = savm[s1][3]: probability, being at state
3639: s1 at precedent wave, to be dead a month before current
3640: wave is equal to probability, being at state s1 at
3641: precedent wave, to be dead at mont of the current
3642: wave. Then the observed probability (that this person died)
3643: is null according to current estimated parameter. In fact,
3644: it should be very low but not zero otherwise the log go to
3645: infinity.
3646: */
1.183 brouard 3647: /* #ifdef INFINITYORIGINAL */
3648: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3649: /* #else */
3650: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3651: /* lli=log(mytinydouble); */
3652: /* else */
3653: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3654: /* #endif */
1.226 brouard 3655: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3656:
1.226 brouard 3657: } else if ( s2==-1 ) { /* alive */
3658: for (j=1,survp=0. ; j<=nlstate; j++)
3659: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3660: /*survp += out[s1][j]; */
3661: lli= log(survp);
3662: }
3663: else if (s2==-4) {
3664: for (j=3,survp=0. ; j<=nlstate; j++)
3665: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3666: lli= log(survp);
3667: }
3668: else if (s2==-5) {
3669: for (j=1,survp=0. ; j<=2; j++)
3670: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3671: lli= log(survp);
3672: }
3673: else{
3674: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3675: /* 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 */
3676: }
3677: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3678: /*if(lli ==000.0)*/
3679: /*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); */
3680: ipmx +=1;
3681: sw += weight[i];
3682: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3683: /* if (lli < log(mytinydouble)){ */
3684: /* 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); */
3685: /* 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]); */
3686: /* } */
3687: } /* end of wave */
3688: } /* end of individual */
3689: } else if(mle==2){
3690: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3691: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3692: for(mi=1; mi<= wav[i]-1; mi++){
3693: for (ii=1;ii<=nlstate+ndeath;ii++)
3694: for (j=1;j<=nlstate+ndeath;j++){
3695: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3696: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3697: }
3698: for(d=0; d<=dh[mi][i]; d++){
3699: newm=savm;
3700: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3701: cov[2]=agexact;
3702: if(nagesqr==1)
3703: cov[3]= agexact*agexact;
3704: for (kk=1; kk<=cptcovage;kk++) {
3705: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3706: }
3707: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3708: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3709: savm=oldm;
3710: oldm=newm;
3711: } /* end mult */
3712:
3713: s1=s[mw[mi][i]][i];
3714: s2=s[mw[mi+1][i]][i];
3715: bbh=(double)bh[mi][i]/(double)stepm;
3716: 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 */
3717: ipmx +=1;
3718: sw += weight[i];
3719: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3720: } /* end of wave */
3721: } /* end of individual */
3722: } else if(mle==3){ /* exponential inter-extrapolation */
3723: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3724: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3725: for(mi=1; mi<= wav[i]-1; mi++){
3726: for (ii=1;ii<=nlstate+ndeath;ii++)
3727: for (j=1;j<=nlstate+ndeath;j++){
3728: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3729: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3730: }
3731: for(d=0; d<dh[mi][i]; d++){
3732: newm=savm;
3733: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3734: cov[2]=agexact;
3735: if(nagesqr==1)
3736: cov[3]= agexact*agexact;
3737: for (kk=1; kk<=cptcovage;kk++) {
3738: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3739: }
3740: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3741: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3742: savm=oldm;
3743: oldm=newm;
3744: } /* end mult */
3745:
3746: s1=s[mw[mi][i]][i];
3747: s2=s[mw[mi+1][i]][i];
3748: bbh=(double)bh[mi][i]/(double)stepm;
3749: 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 */
3750: ipmx +=1;
3751: sw += weight[i];
3752: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3753: } /* end of wave */
3754: } /* end of individual */
3755: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3756: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3757: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3758: for(mi=1; mi<= wav[i]-1; mi++){
3759: for (ii=1;ii<=nlstate+ndeath;ii++)
3760: for (j=1;j<=nlstate+ndeath;j++){
3761: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3762: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3763: }
3764: for(d=0; d<dh[mi][i]; d++){
3765: newm=savm;
3766: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3767: cov[2]=agexact;
3768: if(nagesqr==1)
3769: cov[3]= agexact*agexact;
3770: for (kk=1; kk<=cptcovage;kk++) {
3771: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3772: }
1.126 brouard 3773:
1.226 brouard 3774: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3775: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3776: savm=oldm;
3777: oldm=newm;
3778: } /* end mult */
3779:
3780: s1=s[mw[mi][i]][i];
3781: s2=s[mw[mi+1][i]][i];
3782: if( s2 > nlstate){
3783: lli=log(out[s1][s2] - savm[s1][s2]);
3784: } else if ( s2==-1 ) { /* alive */
3785: for (j=1,survp=0. ; j<=nlstate; j++)
3786: survp += out[s1][j];
3787: lli= log(survp);
3788: }else{
3789: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3790: }
3791: ipmx +=1;
3792: sw += weight[i];
3793: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3794: /* 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 3795: } /* end of wave */
3796: } /* end of individual */
3797: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3798: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3799: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3800: for(mi=1; mi<= wav[i]-1; mi++){
3801: for (ii=1;ii<=nlstate+ndeath;ii++)
3802: for (j=1;j<=nlstate+ndeath;j++){
3803: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3804: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3805: }
3806: for(d=0; d<dh[mi][i]; d++){
3807: newm=savm;
3808: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3809: cov[2]=agexact;
3810: if(nagesqr==1)
3811: cov[3]= agexact*agexact;
3812: for (kk=1; kk<=cptcovage;kk++) {
3813: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3814: }
1.126 brouard 3815:
1.226 brouard 3816: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3817: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3818: savm=oldm;
3819: oldm=newm;
3820: } /* end mult */
3821:
3822: s1=s[mw[mi][i]][i];
3823: s2=s[mw[mi+1][i]][i];
3824: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3825: ipmx +=1;
3826: sw += weight[i];
3827: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3828: /*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]);*/
3829: } /* end of wave */
3830: } /* end of individual */
3831: } /* End of if */
3832: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3833: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3834: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3835: return -l;
1.126 brouard 3836: }
3837:
3838: /*************** log-likelihood *************/
3839: double funcone( double *x)
3840: {
1.228 brouard 3841: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3842: int i, ii, j, k, mi, d, kk;
1.228 brouard 3843: int ioffset=0;
1.131 brouard 3844: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3845: double **out;
3846: double lli; /* Individual log likelihood */
3847: double llt;
3848: int s1, s2;
1.228 brouard 3849: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3850:
1.126 brouard 3851: double bbh, survp;
1.187 brouard 3852: double agexact;
1.214 brouard 3853: double agebegin, ageend;
1.126 brouard 3854: /*extern weight */
3855: /* We are differentiating ll according to initial status */
3856: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3857: /*for(i=1;i<imx;i++)
3858: printf(" %d\n",s[4][i]);
3859: */
3860: cov[1]=1.;
3861:
3862: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3863: ioffset=0;
3864: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.243 brouard 3865: /* ioffset=2+nagesqr+cptcovage; */
3866: ioffset=2+nagesqr;
1.232 brouard 3867: /* Fixed */
1.224 brouard 3868: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 3869: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
1.311 brouard 3870: 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 3871: 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)*/
3872: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
3873: /* cov[2+6]=covar[Tvar[6]][i]; */
3874: /* cov[2+6]=covar[2][i]; V2 */
3875: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
3876: /* cov[2+7]=covar[Tvar[7]][i]; */
3877: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
3878: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
3879: /* cov[2+9]=covar[Tvar[9]][i]; */
3880: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 3881: }
1.232 brouard 3882: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
3883: /* 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?)*\/ */
3884: /* } */
1.231 brouard 3885: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
3886: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
3887: /* } */
1.225 brouard 3888:
1.233 brouard 3889:
3890: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 3891: /* Wave varying (but not age varying) */
3892: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3893: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3894: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
3895: }
1.232 brouard 3896: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.242 brouard 3897: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3898: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
3899: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
3900: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
3901: /* 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 3902: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 3903: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3904: /* /\* 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]); *\/ */
3905: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 3906: /* } */
1.126 brouard 3907: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 3908: for (j=1;j<=nlstate+ndeath;j++){
3909: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3910: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3911: }
1.214 brouard 3912:
3913: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3914: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3915: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.247 brouard 3916: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
1.242 brouard 3917: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3918: and mw[mi+1][i]. dh depends on stepm.*/
3919: newm=savm;
1.247 brouard 3920: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
1.242 brouard 3921: cov[2]=agexact;
3922: if(nagesqr==1)
3923: cov[3]= agexact*agexact;
3924: for (kk=1; kk<=cptcovage;kk++) {
3925: if(!FixedV[Tvar[Tage[kk]]])
3926: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3927: else
3928: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
3929: }
3930: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3931: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3932: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3933: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3934: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3935: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3936: savm=oldm;
3937: oldm=newm;
1.126 brouard 3938: } /* end mult */
3939:
3940: s1=s[mw[mi][i]][i];
3941: s2=s[mw[mi+1][i]][i];
1.217 brouard 3942: /* if(s2==-1){ */
1.268 brouard 3943: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
1.217 brouard 3944: /* /\* exit(1); *\/ */
3945: /* } */
1.126 brouard 3946: bbh=(double)bh[mi][i]/(double)stepm;
3947: /* bias is positive if real duration
3948: * is higher than the multiple of stepm and negative otherwise.
3949: */
3950: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 3951: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3952: } else if ( s2==-1 ) { /* alive */
1.242 brouard 3953: for (j=1,survp=0. ; j<=nlstate; j++)
3954: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3955: lli= log(survp);
1.126 brouard 3956: }else if (mle==1){
1.242 brouard 3957: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3958: } else if(mle==2){
1.242 brouard 3959: 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 3960: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 3961: 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 3962: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 3963: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3964: } else{ /* mle=0 back to 1 */
1.242 brouard 3965: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3966: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3967: } /* End of if */
3968: ipmx +=1;
3969: sw += weight[i];
3970: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3971: /*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 3972: if(globpr){
1.246 brouard 3973: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3974: %11.6f %11.6f %11.6f ", \
1.242 brouard 3975: 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 3976: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
1.242 brouard 3977: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3978: llt +=ll[k]*gipmx/gsw;
3979: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3980: }
3981: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3982: }
1.232 brouard 3983: } /* end of wave */
3984: } /* end of individual */
3985: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3986: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3987: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3988: if(globpr==0){ /* First time we count the contributions and weights */
3989: gipmx=ipmx;
3990: gsw=sw;
3991: }
3992: return -l;
1.126 brouard 3993: }
3994:
3995:
3996: /*************** function likelione ***********/
1.292 brouard 3997: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
1.126 brouard 3998: {
3999: /* This routine should help understanding what is done with
4000: the selection of individuals/waves and
4001: to check the exact contribution to the likelihood.
4002: Plotting could be done.
4003: */
4004: int k;
4005:
4006: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 4007: strcpy(fileresilk,"ILK_");
1.202 brouard 4008: strcat(fileresilk,fileresu);
1.126 brouard 4009: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
4010: printf("Problem with resultfile: %s\n", fileresilk);
4011: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
4012: }
1.214 brouard 4013: 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");
4014: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 4015: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
4016: for(k=1; k<=nlstate; k++)
4017: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
4018: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
4019: }
4020:
1.292 brouard 4021: *fretone=(*func)(p);
1.126 brouard 4022: if(*globpri !=0){
4023: fclose(ficresilk);
1.205 brouard 4024: if (mle ==0)
4025: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
4026: else if(mle >=1)
4027: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
4028: 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 4029: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
1.208 brouard 4030:
4031: for (k=1; k<= nlstate ; k++) {
1.211 brouard 4032: 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 4033: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
4034: }
1.207 brouard 4035: 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 4036: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4037: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 4038: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4039: fflush(fichtm);
1.205 brouard 4040: }
1.126 brouard 4041: return;
4042: }
4043:
4044:
4045: /*********** Maximum Likelihood Estimation ***************/
4046:
4047: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
4048: {
1.165 brouard 4049: int i,j, iter=0;
1.126 brouard 4050: double **xi;
4051: double fret;
4052: double fretone; /* Only one call to likelihood */
4053: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 4054:
4055: #ifdef NLOPT
4056: int creturn;
4057: nlopt_opt opt;
4058: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
4059: double *lb;
4060: double minf; /* the minimum objective value, upon return */
4061: double * p1; /* Shifted parameters from 0 instead of 1 */
4062: myfunc_data dinst, *d = &dinst;
4063: #endif
4064:
4065:
1.126 brouard 4066: xi=matrix(1,npar,1,npar);
4067: for (i=1;i<=npar;i++)
4068: for (j=1;j<=npar;j++)
4069: xi[i][j]=(i==j ? 1.0 : 0.0);
4070: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 4071: strcpy(filerespow,"POW_");
1.126 brouard 4072: strcat(filerespow,fileres);
4073: if((ficrespow=fopen(filerespow,"w"))==NULL) {
4074: printf("Problem with resultfile: %s\n", filerespow);
4075: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
4076: }
4077: fprintf(ficrespow,"# Powell\n# iter -2*LL");
4078: for (i=1;i<=nlstate;i++)
4079: for(j=1;j<=nlstate+ndeath;j++)
4080: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
4081: fprintf(ficrespow,"\n");
1.162 brouard 4082: #ifdef POWELL
1.126 brouard 4083: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 4084: #endif
1.126 brouard 4085:
1.162 brouard 4086: #ifdef NLOPT
4087: #ifdef NEWUOA
4088: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
4089: #else
4090: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
4091: #endif
4092: lb=vector(0,npar-1);
4093: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
4094: nlopt_set_lower_bounds(opt, lb);
4095: nlopt_set_initial_step1(opt, 0.1);
4096:
4097: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
4098: d->function = func;
4099: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
4100: nlopt_set_min_objective(opt, myfunc, d);
4101: nlopt_set_xtol_rel(opt, ftol);
4102: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
4103: printf("nlopt failed! %d\n",creturn);
4104: }
4105: else {
4106: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
4107: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
4108: iter=1; /* not equal */
4109: }
4110: nlopt_destroy(opt);
4111: #endif
1.126 brouard 4112: free_matrix(xi,1,npar,1,npar);
4113: fclose(ficrespow);
1.203 brouard 4114: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4115: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 4116: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 4117:
4118: }
4119:
4120: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 4121: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 4122: {
4123: double **a,**y,*x,pd;
1.203 brouard 4124: /* double **hess; */
1.164 brouard 4125: int i, j;
1.126 brouard 4126: int *indx;
4127:
4128: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 4129: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 4130: void lubksb(double **a, int npar, int *indx, double b[]) ;
4131: void ludcmp(double **a, int npar, int *indx, double *d) ;
4132: double gompertz(double p[]);
1.203 brouard 4133: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 4134:
4135: printf("\nCalculation of the hessian matrix. Wait...\n");
4136: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
4137: for (i=1;i<=npar;i++){
1.203 brouard 4138: printf("%d-",i);fflush(stdout);
4139: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 4140:
4141: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
4142:
4143: /* printf(" %f ",p[i]);
4144: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
4145: }
4146:
4147: for (i=1;i<=npar;i++) {
4148: for (j=1;j<=npar;j++) {
4149: if (j>i) {
1.203 brouard 4150: printf(".%d-%d",i,j);fflush(stdout);
4151: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
4152: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 4153:
4154: hess[j][i]=hess[i][j];
4155: /*printf(" %lf ",hess[i][j]);*/
4156: }
4157: }
4158: }
4159: printf("\n");
4160: fprintf(ficlog,"\n");
4161:
4162: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
4163: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
4164:
4165: a=matrix(1,npar,1,npar);
4166: y=matrix(1,npar,1,npar);
4167: x=vector(1,npar);
4168: indx=ivector(1,npar);
4169: for (i=1;i<=npar;i++)
4170: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
4171: ludcmp(a,npar,indx,&pd);
4172:
4173: for (j=1;j<=npar;j++) {
4174: for (i=1;i<=npar;i++) x[i]=0;
4175: x[j]=1;
4176: lubksb(a,npar,indx,x);
4177: for (i=1;i<=npar;i++){
4178: matcov[i][j]=x[i];
4179: }
4180: }
4181:
4182: printf("\n#Hessian matrix#\n");
4183: fprintf(ficlog,"\n#Hessian matrix#\n");
4184: for (i=1;i<=npar;i++) {
4185: for (j=1;j<=npar;j++) {
1.203 brouard 4186: printf("%.6e ",hess[i][j]);
4187: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 4188: }
4189: printf("\n");
4190: fprintf(ficlog,"\n");
4191: }
4192:
1.203 brouard 4193: /* printf("\n#Covariance matrix#\n"); */
4194: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
4195: /* for (i=1;i<=npar;i++) { */
4196: /* for (j=1;j<=npar;j++) { */
4197: /* printf("%.6e ",matcov[i][j]); */
4198: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
4199: /* } */
4200: /* printf("\n"); */
4201: /* fprintf(ficlog,"\n"); */
4202: /* } */
4203:
1.126 brouard 4204: /* Recompute Inverse */
1.203 brouard 4205: /* for (i=1;i<=npar;i++) */
4206: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
4207: /* ludcmp(a,npar,indx,&pd); */
4208:
4209: /* printf("\n#Hessian matrix recomputed#\n"); */
4210:
4211: /* for (j=1;j<=npar;j++) { */
4212: /* for (i=1;i<=npar;i++) x[i]=0; */
4213: /* x[j]=1; */
4214: /* lubksb(a,npar,indx,x); */
4215: /* for (i=1;i<=npar;i++){ */
4216: /* y[i][j]=x[i]; */
4217: /* printf("%.3e ",y[i][j]); */
4218: /* fprintf(ficlog,"%.3e ",y[i][j]); */
4219: /* } */
4220: /* printf("\n"); */
4221: /* fprintf(ficlog,"\n"); */
4222: /* } */
4223:
4224: /* Verifying the inverse matrix */
4225: #ifdef DEBUGHESS
4226: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 4227:
1.203 brouard 4228: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
4229: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 4230:
4231: for (j=1;j<=npar;j++) {
4232: for (i=1;i<=npar;i++){
1.203 brouard 4233: printf("%.2f ",y[i][j]);
4234: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 4235: }
4236: printf("\n");
4237: fprintf(ficlog,"\n");
4238: }
1.203 brouard 4239: #endif
1.126 brouard 4240:
4241: free_matrix(a,1,npar,1,npar);
4242: free_matrix(y,1,npar,1,npar);
4243: free_vector(x,1,npar);
4244: free_ivector(indx,1,npar);
1.203 brouard 4245: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 4246:
4247:
4248: }
4249:
4250: /*************** hessian matrix ****************/
4251: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 4252: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 4253: int i;
4254: int l=1, lmax=20;
1.203 brouard 4255: double k1,k2, res, fx;
1.132 brouard 4256: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 4257: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
4258: int k=0,kmax=10;
4259: double l1;
4260:
4261: fx=func(x);
4262: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 4263: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 4264: l1=pow(10,l);
4265: delts=delt;
4266: for(k=1 ; k <kmax; k=k+1){
4267: delt = delta*(l1*k);
4268: p2[theta]=x[theta] +delt;
1.145 brouard 4269: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 4270: p2[theta]=x[theta]-delt;
4271: k2=func(p2)-fx;
4272: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 4273: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 4274:
1.203 brouard 4275: #ifdef DEBUGHESSII
1.126 brouard 4276: 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);
4277: 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);
4278: #endif
4279: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
4280: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
4281: k=kmax;
4282: }
4283: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 4284: k=kmax; l=lmax*10;
1.126 brouard 4285: }
4286: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
4287: delts=delt;
4288: }
1.203 brouard 4289: } /* End loop k */
1.126 brouard 4290: }
4291: delti[theta]=delts;
4292: return res;
4293:
4294: }
4295:
1.203 brouard 4296: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 4297: {
4298: int i;
1.164 brouard 4299: int l=1, lmax=20;
1.126 brouard 4300: double k1,k2,k3,k4,res,fx;
1.132 brouard 4301: double p2[MAXPARM+1];
1.203 brouard 4302: int k, kmax=1;
4303: double v1, v2, cv12, lc1, lc2;
1.208 brouard 4304:
4305: int firstime=0;
1.203 brouard 4306:
1.126 brouard 4307: fx=func(x);
1.203 brouard 4308: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 4309: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 4310: p2[thetai]=x[thetai]+delti[thetai]*k;
4311: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4312: k1=func(p2)-fx;
4313:
1.203 brouard 4314: p2[thetai]=x[thetai]+delti[thetai]*k;
4315: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4316: k2=func(p2)-fx;
4317:
1.203 brouard 4318: p2[thetai]=x[thetai]-delti[thetai]*k;
4319: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4320: k3=func(p2)-fx;
4321:
1.203 brouard 4322: p2[thetai]=x[thetai]-delti[thetai]*k;
4323: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4324: k4=func(p2)-fx;
1.203 brouard 4325: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
4326: if(k1*k2*k3*k4 <0.){
1.208 brouard 4327: firstime=1;
1.203 brouard 4328: kmax=kmax+10;
1.208 brouard 4329: }
4330: if(kmax >=10 || firstime ==1){
1.246 brouard 4331: 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);
4332: 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 4333: 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);
4334: 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);
4335: }
4336: #ifdef DEBUGHESSIJ
4337: v1=hess[thetai][thetai];
4338: v2=hess[thetaj][thetaj];
4339: cv12=res;
4340: /* Computing eigen value of Hessian matrix */
4341: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4342: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4343: if ((lc2 <0) || (lc1 <0) ){
4344: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4345: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4346: 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);
4347: 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);
4348: }
1.126 brouard 4349: #endif
4350: }
4351: return res;
4352: }
4353:
1.203 brouard 4354: /* Not done yet: Was supposed to fix if not exactly at the maximum */
4355: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
4356: /* { */
4357: /* int i; */
4358: /* int l=1, lmax=20; */
4359: /* double k1,k2,k3,k4,res,fx; */
4360: /* double p2[MAXPARM+1]; */
4361: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
4362: /* int k=0,kmax=10; */
4363: /* double l1; */
4364:
4365: /* fx=func(x); */
4366: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
4367: /* l1=pow(10,l); */
4368: /* delts=delt; */
4369: /* for(k=1 ; k <kmax; k=k+1){ */
4370: /* delt = delti*(l1*k); */
4371: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
4372: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4373: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4374: /* k1=func(p2)-fx; */
4375:
4376: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4377: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4378: /* k2=func(p2)-fx; */
4379:
4380: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4381: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4382: /* k3=func(p2)-fx; */
4383:
4384: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4385: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4386: /* k4=func(p2)-fx; */
4387: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
4388: /* #ifdef DEBUGHESSIJ */
4389: /* 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); */
4390: /* 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); */
4391: /* #endif */
4392: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
4393: /* k=kmax; */
4394: /* } */
4395: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
4396: /* k=kmax; l=lmax*10; */
4397: /* } */
4398: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
4399: /* delts=delt; */
4400: /* } */
4401: /* } /\* End loop k *\/ */
4402: /* } */
4403: /* delti[theta]=delts; */
4404: /* return res; */
4405: /* } */
4406:
4407:
1.126 brouard 4408: /************** Inverse of matrix **************/
4409: void ludcmp(double **a, int n, int *indx, double *d)
4410: {
4411: int i,imax,j,k;
4412: double big,dum,sum,temp;
4413: double *vv;
4414:
4415: vv=vector(1,n);
4416: *d=1.0;
4417: for (i=1;i<=n;i++) {
4418: big=0.0;
4419: for (j=1;j<=n;j++)
4420: if ((temp=fabs(a[i][j])) > big) big=temp;
1.256 brouard 4421: if (big == 0.0){
4422: printf(" Singular Hessian matrix at row %d:\n",i);
4423: for (j=1;j<=n;j++) {
4424: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
4425: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
4426: }
4427: fflush(ficlog);
4428: fclose(ficlog);
4429: nrerror("Singular matrix in routine ludcmp");
4430: }
1.126 brouard 4431: vv[i]=1.0/big;
4432: }
4433: for (j=1;j<=n;j++) {
4434: for (i=1;i<j;i++) {
4435: sum=a[i][j];
4436: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
4437: a[i][j]=sum;
4438: }
4439: big=0.0;
4440: for (i=j;i<=n;i++) {
4441: sum=a[i][j];
4442: for (k=1;k<j;k++)
4443: sum -= a[i][k]*a[k][j];
4444: a[i][j]=sum;
4445: if ( (dum=vv[i]*fabs(sum)) >= big) {
4446: big=dum;
4447: imax=i;
4448: }
4449: }
4450: if (j != imax) {
4451: for (k=1;k<=n;k++) {
4452: dum=a[imax][k];
4453: a[imax][k]=a[j][k];
4454: a[j][k]=dum;
4455: }
4456: *d = -(*d);
4457: vv[imax]=vv[j];
4458: }
4459: indx[j]=imax;
4460: if (a[j][j] == 0.0) a[j][j]=TINY;
4461: if (j != n) {
4462: dum=1.0/(a[j][j]);
4463: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4464: }
4465: }
4466: free_vector(vv,1,n); /* Doesn't work */
4467: ;
4468: }
4469:
4470: void lubksb(double **a, int n, int *indx, double b[])
4471: {
4472: int i,ii=0,ip,j;
4473: double sum;
4474:
4475: for (i=1;i<=n;i++) {
4476: ip=indx[i];
4477: sum=b[ip];
4478: b[ip]=b[i];
4479: if (ii)
4480: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4481: else if (sum) ii=i;
4482: b[i]=sum;
4483: }
4484: for (i=n;i>=1;i--) {
4485: sum=b[i];
4486: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4487: b[i]=sum/a[i][i];
4488: }
4489: }
4490:
4491: void pstamp(FILE *fichier)
4492: {
1.196 brouard 4493: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4494: }
4495:
1.297 brouard 4496: void date2dmy(double date,double *day, double *month, double *year){
4497: double yp=0., yp1=0., yp2=0.;
4498:
4499: yp1=modf(date,&yp);/* extracts integral of date in yp and
4500: fractional in yp1 */
4501: *year=yp;
4502: yp2=modf((yp1*12),&yp);
4503: *month=yp;
4504: yp1=modf((yp2*30.5),&yp);
4505: *day=yp;
4506: if(*day==0) *day=1;
4507: if(*month==0) *month=1;
4508: }
4509:
1.253 brouard 4510:
4511:
1.126 brouard 4512: /************ Frequencies ********************/
1.251 brouard 4513: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
1.226 brouard 4514: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4515: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.250 brouard 4516: { /* Some frequencies as well as proposing some starting values */
1.226 brouard 4517:
1.265 brouard 4518: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
1.226 brouard 4519: int iind=0, iage=0;
4520: int mi; /* Effective wave */
4521: int first;
4522: double ***freq; /* Frequencies */
1.268 brouard 4523: 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 */
4524: 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 4525: double *meanq, *stdq, *idq;
1.226 brouard 4526: double **meanqt;
4527: double *pp, **prop, *posprop, *pospropt;
4528: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4529: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4530: double agebegin, ageend;
4531:
4532: pp=vector(1,nlstate);
1.251 brouard 4533: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4534: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4535: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4536: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4537: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.284 brouard 4538: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.283 brouard 4539: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.226 brouard 4540: meanqt=matrix(1,lastpass,1,nqtveff);
4541: strcpy(fileresp,"P_");
4542: strcat(fileresp,fileresu);
4543: /*strcat(fileresphtm,fileresu);*/
4544: if((ficresp=fopen(fileresp,"w"))==NULL) {
4545: printf("Problem with prevalence resultfile: %s\n", fileresp);
4546: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4547: exit(0);
4548: }
1.240 brouard 4549:
1.226 brouard 4550: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4551: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4552: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4553: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4554: fflush(ficlog);
4555: exit(70);
4556: }
4557: else{
4558: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 4559: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4560: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4561: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4562: }
1.237 brouard 4563: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies and prevalence by age at begin of transition and dummy covariate value at beginning of transition</h4>\n",fileresphtm, fileresphtm);
1.240 brouard 4564:
1.226 brouard 4565: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4566: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4567: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4568: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4569: fflush(ficlog);
4570: exit(70);
1.240 brouard 4571: } else{
1.226 brouard 4572: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 4573: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4574: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4575: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4576: }
1.240 brouard 4577: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>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);
4578:
1.253 brouard 4579: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
4580: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.251 brouard 4581: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4582: j1=0;
1.126 brouard 4583:
1.227 brouard 4584: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4585: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4586: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 4587:
4588:
1.226 brouard 4589: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4590: reference=low_education V1=0,V2=0
4591: med_educ V1=1 V2=0,
4592: high_educ V1=0 V2=1
4593: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4594: */
1.249 brouard 4595: dateintsum=0;
4596: k2cpt=0;
4597:
1.253 brouard 4598: if(cptcoveff == 0 )
1.265 brouard 4599: nl=1; /* Constant and age model only */
1.253 brouard 4600: else
4601: nl=2;
1.265 brouard 4602:
4603: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
4604: /* Loop on nj=1 or 2 if dummy covariates j!=0
4605: * Loop on j1(1 to 2**cptcoveff) covariate combination
4606: * freq[s1][s2][iage] =0.
4607: * Loop on iind
4608: * ++freq[s1][s2][iage] weighted
4609: * end iind
4610: * if covariate and j!0
4611: * headers Variable on one line
4612: * endif cov j!=0
4613: * header of frequency table by age
4614: * Loop on age
4615: * pp[s1]+=freq[s1][s2][iage] weighted
4616: * pos+=freq[s1][s2][iage] weighted
4617: * Loop on s1 initial state
4618: * fprintf(ficresp
4619: * end s1
4620: * end age
4621: * if j!=0 computes starting values
4622: * end compute starting values
4623: * end j1
4624: * end nl
4625: */
1.253 brouard 4626: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
4627: if(nj==1)
4628: j=0; /* First pass for the constant */
1.265 brouard 4629: else{
1.253 brouard 4630: j=cptcoveff; /* Other passes for the covariate values */
1.265 brouard 4631: }
1.251 brouard 4632: first=1;
1.265 brouard 4633: 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 4634: posproptt=0.;
4635: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4636: scanf("%d", i);*/
4637: for (i=-5; i<=nlstate+ndeath; i++)
1.265 brouard 4638: for (s2=-5; s2<=nlstate+ndeath; s2++)
1.251 brouard 4639: for(m=iagemin; m <= iagemax+3; m++)
1.265 brouard 4640: freq[i][s2][m]=0;
1.251 brouard 4641:
4642: for (i=1; i<=nlstate; i++) {
1.240 brouard 4643: for(m=iagemin; m <= iagemax+3; m++)
1.251 brouard 4644: prop[i][m]=0;
4645: posprop[i]=0;
4646: pospropt[i]=0;
4647: }
1.283 brouard 4648: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
1.284 brouard 4649: idq[z1]=0.;
4650: meanq[z1]=0.;
4651: stdq[z1]=0.;
1.283 brouard 4652: }
4653: /* for (z1=1; z1<= nqtveff; z1++) { */
1.251 brouard 4654: /* for(m=1;m<=lastpass;m++){ */
1.283 brouard 4655: /* meanqt[m][z1]=0.; */
4656: /* } */
4657: /* } */
1.251 brouard 4658: /* dateintsum=0; */
4659: /* k2cpt=0; */
4660:
1.265 brouard 4661: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
1.251 brouard 4662: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4663: bool=1;
4664: if(j !=0){
4665: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
4666: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
4667: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
4668: /* if(Tvaraff[z1] ==-20){ */
4669: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
4670: /* }else if(Tvaraff[z1] ==-10){ */
4671: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
4672: /* }else */
4673: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){ /* for combination j1 of covariates */
1.265 brouard 4674: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
1.251 brouard 4675: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
4676: /* 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",
4677: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
4678: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
4679: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
4680: } /* Onlyf fixed */
4681: } /* end z1 */
4682: } /* cptcovn > 0 */
4683: } /* end any */
4684: }/* end j==0 */
1.265 brouard 4685: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
1.251 brouard 4686: /* for(m=firstpass; m<=lastpass; m++){ */
1.284 brouard 4687: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
1.251 brouard 4688: m=mw[mi][iind];
4689: if(j!=0){
4690: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
4691: for (z1=1; z1<=cptcoveff; z1++) {
4692: if( Fixed[Tmodelind[z1]]==1){
4693: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4694: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality. If covariate's
4695: value is -1, we don't select. It differs from the
4696: constant and age model which counts them. */
4697: bool=0; /* not selected */
4698: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
4699: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4700: bool=0;
4701: }
4702: }
4703: }
4704: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
4705: } /* end j==0 */
4706: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
1.284 brouard 4707: if(bool==1){ /*Selected */
1.251 brouard 4708: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
4709: and mw[mi+1][iind]. dh depends on stepm. */
4710: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
4711: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
4712: if(m >=firstpass && m <=lastpass){
4713: k2=anint[m][iind]+(mint[m][iind]/12.);
4714: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
4715: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
4716: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
4717: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
4718: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4719: if (m<lastpass) {
4720: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
4721: /* 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]); */
4722: if(s[m][iind]==-1)
4723: 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.));
4724: 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 4725: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
4726: if(!isnan(covar[ncovcol+z1][iind])){
4727: idq[z1]=idq[z1]+weight[iind];
4728: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
4729: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
4730: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
4731: }
1.284 brouard 4732: }
1.251 brouard 4733: /* if((int)agev[m][iind] == 55) */
4734: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
4735: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
4736: 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 4737: }
1.251 brouard 4738: } /* end if between passes */
4739: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
4740: dateintsum=dateintsum+k2; /* on all covariates ?*/
4741: k2cpt++;
4742: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
1.234 brouard 4743: }
1.251 brouard 4744: }else{
4745: bool=1;
4746: }/* end bool 2 */
4747: } /* end m */
1.284 brouard 4748: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
4749: /* idq[z1]=idq[z1]+weight[iind]; */
4750: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
4751: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
4752: /* } */
1.251 brouard 4753: } /* end bool */
4754: } /* end iind = 1 to imx */
4755: /* prop[s][age] is feeded for any initial and valid live state as well as
4756: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
4757:
4758:
4759: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.265 brouard 4760: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
4761: pstamp(ficresp);
1.251 brouard 4762: if (cptcoveff>0 && j!=0){
1.265 brouard 4763: pstamp(ficresp);
1.251 brouard 4764: printf( "\n#********** Variable ");
4765: fprintf(ficresp, "\n#********** Variable ");
4766: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4767: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
4768: fprintf(ficlog, "\n#********** Variable ");
4769: for (z1=1; z1<=cptcoveff; z1++){
4770: if(!FixedV[Tvaraff[z1]]){
4771: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4772: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4773: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4774: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4775: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.250 brouard 4776: }else{
1.251 brouard 4777: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4778: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4779: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4780: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4781: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4782: }
4783: }
4784: printf( "**********\n#");
4785: fprintf(ficresp, "**********\n#");
4786: fprintf(ficresphtm, "**********</h3>\n");
4787: fprintf(ficresphtmfr, "**********</h3>\n");
4788: fprintf(ficlog, "**********\n");
4789: }
1.284 brouard 4790: /*
4791: Printing means of quantitative variables if any
4792: */
4793: for (z1=1; z1<= nqfveff; z1++) {
1.311 brouard 4794: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
1.312 brouard 4795: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
1.284 brouard 4796: if(weightopt==1){
4797: printf(" Weighted mean and standard deviation of");
4798: fprintf(ficlog," Weighted mean and standard deviation of");
4799: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
4800: }
1.311 brouard 4801: /* mu = \frac{w x}{\sum w}
4802: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
4803: */
4804: 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]));
4805: 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]));
4806: 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 4807: }
4808: /* for (z1=1; z1<= nqtveff; z1++) { */
4809: /* for(m=1;m<=lastpass;m++){ */
4810: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
4811: /* } */
4812: /* } */
1.283 brouard 4813:
1.251 brouard 4814: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.265 brouard 4815: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
4816: fprintf(ficresp, " Age");
4817: 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 4818: for(i=1; i<=nlstate;i++) {
1.265 brouard 4819: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
1.251 brouard 4820: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4821: }
1.265 brouard 4822: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
1.251 brouard 4823: fprintf(ficresphtm, "\n");
4824:
4825: /* Header of frequency table by age */
4826: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4827: fprintf(ficresphtmfr,"<th>Age</th> ");
1.265 brouard 4828: for(s2=-1; s2 <=nlstate+ndeath; s2++){
1.251 brouard 4829: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 4830: if(s2!=0 && m!=0)
4831: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
1.240 brouard 4832: }
1.226 brouard 4833: }
1.251 brouard 4834: fprintf(ficresphtmfr, "\n");
4835:
4836: /* For each age */
4837: for(iage=iagemin; iage <= iagemax+3; iage++){
4838: fprintf(ficresphtm,"<tr>");
4839: if(iage==iagemax+1){
4840: fprintf(ficlog,"1");
4841: fprintf(ficresphtmfr,"<tr><th>0</th> ");
4842: }else if(iage==iagemax+2){
4843: fprintf(ficlog,"0");
4844: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
4845: }else if(iage==iagemax+3){
4846: fprintf(ficlog,"Total");
4847: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
4848: }else{
1.240 brouard 4849: if(first==1){
1.251 brouard 4850: first=0;
4851: printf("See log file for details...\n");
4852: }
4853: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4854: fprintf(ficlog,"Age %d", iage);
4855: }
1.265 brouard 4856: for(s1=1; s1 <=nlstate ; s1++){
4857: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
4858: pp[s1] += freq[s1][m][iage];
1.251 brouard 4859: }
1.265 brouard 4860: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 4861: for(m=-1, pos=0; m <=0 ; m++)
1.265 brouard 4862: pos += freq[s1][m][iage];
4863: if(pp[s1]>=1.e-10){
1.251 brouard 4864: if(first==1){
1.265 brouard 4865: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 4866: }
1.265 brouard 4867: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 4868: }else{
4869: if(first==1)
1.265 brouard 4870: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
4871: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
1.240 brouard 4872: }
4873: }
4874:
1.265 brouard 4875: for(s1=1; s1 <=nlstate ; s1++){
4876: /* posprop[s1]=0; */
4877: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4878: pp[s1] += freq[s1][m][iage];
4879: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
4880:
4881: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
4882: pos += pp[s1]; /* pos is the total number of transitions until this age */
4883: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
4884: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4885: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
4886: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4887: }
4888:
4889: /* Writing ficresp */
4890: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
4891: if( iage <= iagemax){
4892: fprintf(ficresp," %d",iage);
4893: }
4894: }else if( nj==2){
4895: if( iage <= iagemax){
4896: fprintf(ficresp," %d",iage);
4897: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4898: }
1.240 brouard 4899: }
1.265 brouard 4900: for(s1=1; s1 <=nlstate ; s1++){
1.240 brouard 4901: if(pos>=1.e-5){
1.251 brouard 4902: if(first==1)
1.265 brouard 4903: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
4904: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
1.251 brouard 4905: }else{
4906: if(first==1)
1.265 brouard 4907: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
4908: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
1.251 brouard 4909: }
4910: if( iage <= iagemax){
4911: if(pos>=1.e-5){
1.265 brouard 4912: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
4913: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4914: }else if( nj==2){
4915: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4916: }
4917: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4918: /*probs[iage][s1][j1]= pp[s1]/pos;*/
4919: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
4920: } else{
4921: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
4922: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
1.251 brouard 4923: }
1.240 brouard 4924: }
1.265 brouard 4925: pospropt[s1] +=posprop[s1];
4926: } /* end loop s1 */
1.251 brouard 4927: /* pospropt=0.; */
1.265 brouard 4928: for(s1=-1; s1 <=nlstate+ndeath; s1++){
1.251 brouard 4929: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 4930: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
1.251 brouard 4931: if(first==1){
1.265 brouard 4932: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 4933: }
1.265 brouard 4934: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
4935: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 4936: }
1.265 brouard 4937: if(s1!=0 && m!=0)
4938: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
1.240 brouard 4939: }
1.265 brouard 4940: } /* end loop s1 */
1.251 brouard 4941: posproptt=0.;
1.265 brouard 4942: for(s1=1; s1 <=nlstate; s1++){
4943: posproptt += pospropt[s1];
1.251 brouard 4944: }
4945: fprintf(ficresphtmfr,"</tr>\n ");
1.265 brouard 4946: fprintf(ficresphtm,"</tr>\n");
4947: if((cptcoveff==0 && nj==1)|| nj==2 ) {
4948: if(iage <= iagemax)
4949: fprintf(ficresp,"\n");
1.240 brouard 4950: }
1.251 brouard 4951: if(first==1)
4952: printf("Others in log...\n");
4953: fprintf(ficlog,"\n");
4954: } /* end loop age iage */
1.265 brouard 4955:
1.251 brouard 4956: fprintf(ficresphtm,"<tr><th>Tot</th>");
1.265 brouard 4957: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 4958: if(posproptt < 1.e-5){
1.265 brouard 4959: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
1.251 brouard 4960: }else{
1.265 brouard 4961: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
1.240 brouard 4962: }
1.226 brouard 4963: }
1.251 brouard 4964: fprintf(ficresphtm,"</tr>\n");
4965: fprintf(ficresphtm,"</table>\n");
4966: fprintf(ficresphtmfr,"</table>\n");
1.226 brouard 4967: if(posproptt < 1.e-5){
1.251 brouard 4968: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4969: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
1.260 brouard 4970: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
4971: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
1.251 brouard 4972: invalidvarcomb[j1]=1;
1.226 brouard 4973: }else{
1.251 brouard 4974: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4975: invalidvarcomb[j1]=0;
1.226 brouard 4976: }
1.251 brouard 4977: fprintf(ficresphtmfr,"</table>\n");
4978: fprintf(ficlog,"\n");
4979: if(j!=0){
4980: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
1.265 brouard 4981: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 4982: for(k=1; k <=(nlstate+ndeath); k++){
4983: if (k != i) {
1.265 brouard 4984: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
1.253 brouard 4985: if(jj==1){ /* Constant case (in fact cste + age) */
1.251 brouard 4986: if(j1==1){ /* All dummy covariates to zero */
4987: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
4988: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
1.252 brouard 4989: printf("%d%d ",i,k);
4990: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 4991: 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]));
4992: 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]));
4993: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
1.251 brouard 4994: }
1.253 brouard 4995: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
4996: for(iage=iagemin; iage <= iagemax+3; iage++){
4997: x[iage]= (double)iage;
4998: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
1.265 brouard 4999: /* 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 5000: }
1.268 brouard 5001: /* Some are not finite, but linreg will ignore these ages */
5002: no=0;
1.253 brouard 5003: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
1.265 brouard 5004: pstart[s1]=b;
5005: pstart[s1-1]=a;
1.252 brouard 5006: }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 */
5007: 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]);
5008: 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 5009: 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 5010: printf("%d%d ",i,k);
5011: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 5012: 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 5013: }else{ /* Other cases, like quantitative fixed or varying covariates */
5014: ;
5015: }
5016: /* printf("%12.7f )", param[i][jj][k]); */
5017: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 5018: s1++;
1.251 brouard 5019: } /* end jj */
5020: } /* end k!= i */
5021: } /* end k */
1.265 brouard 5022: } /* end i, s1 */
1.251 brouard 5023: } /* end j !=0 */
5024: } /* end selected combination of covariate j1 */
5025: if(j==0){ /* We can estimate starting values from the occurences in each case */
5026: printf("#Freqsummary: Starting values for the constants:\n");
5027: fprintf(ficlog,"\n");
1.265 brouard 5028: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 5029: for(k=1; k <=(nlstate+ndeath); k++){
5030: if (k != i) {
5031: printf("%d%d ",i,k);
5032: fprintf(ficlog,"%d%d ",i,k);
5033: for(jj=1; jj <=ncovmodel; jj++){
1.265 brouard 5034: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
1.253 brouard 5035: if(jj==1){ /* Age has to be done */
1.265 brouard 5036: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5037: 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]));
5038: 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 5039: }
5040: /* printf("%12.7f )", param[i][jj][k]); */
5041: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 5042: s1++;
1.250 brouard 5043: }
1.251 brouard 5044: printf("\n");
5045: fprintf(ficlog,"\n");
1.250 brouard 5046: }
5047: }
1.284 brouard 5048: } /* end of state i */
1.251 brouard 5049: printf("#Freqsummary\n");
5050: fprintf(ficlog,"\n");
1.265 brouard 5051: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5052: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5053: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
5054: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5055: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5056: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
5057: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5058: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
1.251 brouard 5059: /* } */
5060: }
1.265 brouard 5061: } /* end loop s1 */
1.251 brouard 5062:
5063: printf("\n");
5064: fprintf(ficlog,"\n");
5065: } /* end j=0 */
1.249 brouard 5066: } /* end j */
1.252 brouard 5067:
1.253 brouard 5068: if(mle == -2){ /* We want to use these values as starting values */
1.252 brouard 5069: for(i=1, jk=1; i <=nlstate; i++){
5070: for(j=1; j <=nlstate+ndeath; j++){
5071: if(j!=i){
5072: /*ca[0]= k+'a'-1;ca[1]='\0';*/
5073: printf("%1d%1d",i,j);
5074: fprintf(ficparo,"%1d%1d",i,j);
5075: for(k=1; k<=ncovmodel;k++){
5076: /* printf(" %lf",param[i][j][k]); */
5077: /* fprintf(ficparo," %lf",param[i][j][k]); */
5078: p[jk]=pstart[jk];
5079: printf(" %f ",pstart[jk]);
5080: fprintf(ficparo," %f ",pstart[jk]);
5081: jk++;
5082: }
5083: printf("\n");
5084: fprintf(ficparo,"\n");
5085: }
5086: }
5087: }
5088: } /* end mle=-2 */
1.226 brouard 5089: dateintmean=dateintsum/k2cpt;
1.296 brouard 5090: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
1.240 brouard 5091:
1.226 brouard 5092: fclose(ficresp);
5093: fclose(ficresphtm);
5094: fclose(ficresphtmfr);
1.283 brouard 5095: free_vector(idq,1,nqfveff);
1.226 brouard 5096: free_vector(meanq,1,nqfveff);
1.284 brouard 5097: free_vector(stdq,1,nqfveff);
1.226 brouard 5098: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.253 brouard 5099: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5100: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.251 brouard 5101: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5102: free_vector(pospropt,1,nlstate);
5103: free_vector(posprop,1,nlstate);
1.251 brouard 5104: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5105: free_vector(pp,1,nlstate);
5106: /* End of freqsummary */
5107: }
1.126 brouard 5108:
1.268 brouard 5109: /* Simple linear regression */
5110: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
5111:
5112: /* y=a+bx regression */
5113: double sumx = 0.0; /* sum of x */
5114: double sumx2 = 0.0; /* sum of x**2 */
5115: double sumxy = 0.0; /* sum of x * y */
5116: double sumy = 0.0; /* sum of y */
5117: double sumy2 = 0.0; /* sum of y**2 */
5118: double sume2 = 0.0; /* sum of square or residuals */
5119: double yhat;
5120:
5121: double denom=0;
5122: int i;
5123: int ne=*no;
5124:
5125: for ( i=ifi, ne=0;i<=ila;i++) {
5126: if(!isfinite(x[i]) || !isfinite(y[i])){
5127: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5128: continue;
5129: }
5130: ne=ne+1;
5131: sumx += x[i];
5132: sumx2 += x[i]*x[i];
5133: sumxy += x[i] * y[i];
5134: sumy += y[i];
5135: sumy2 += y[i]*y[i];
5136: denom = (ne * sumx2 - sumx*sumx);
5137: /* 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); */
5138: }
5139:
5140: denom = (ne * sumx2 - sumx*sumx);
5141: if (denom == 0) {
5142: // vertical, slope m is infinity
5143: *b = INFINITY;
5144: *a = 0;
5145: if (r) *r = 0;
5146: return 1;
5147: }
5148:
5149: *b = (ne * sumxy - sumx * sumy) / denom;
5150: *a = (sumy * sumx2 - sumx * sumxy) / denom;
5151: if (r!=NULL) {
5152: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
5153: sqrt((sumx2 - sumx*sumx/ne) *
5154: (sumy2 - sumy*sumy/ne));
5155: }
5156: *no=ne;
5157: for ( i=ifi, ne=0;i<=ila;i++) {
5158: if(!isfinite(x[i]) || !isfinite(y[i])){
5159: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5160: continue;
5161: }
5162: ne=ne+1;
5163: yhat = y[i] - *a -*b* x[i];
5164: sume2 += yhat * yhat ;
5165:
5166: denom = (ne * sumx2 - sumx*sumx);
5167: /* 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); */
5168: }
5169: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
5170: *sa= *sb * sqrt(sumx2/ne);
5171:
5172: return 0;
5173: }
5174:
1.126 brouard 5175: /************ Prevalence ********************/
1.227 brouard 5176: 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)
5177: {
5178: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
5179: in each health status at the date of interview (if between dateprev1 and dateprev2).
5180: We still use firstpass and lastpass as another selection.
5181: */
1.126 brouard 5182:
1.227 brouard 5183: int i, m, jk, j1, bool, z1,j, iv;
5184: int mi; /* Effective wave */
5185: int iage;
5186: double agebegin, ageend;
5187:
5188: double **prop;
5189: double posprop;
5190: double y2; /* in fractional years */
5191: int iagemin, iagemax;
5192: int first; /** to stop verbosity which is redirected to log file */
5193:
5194: iagemin= (int) agemin;
5195: iagemax= (int) agemax;
5196: /*pp=vector(1,nlstate);*/
1.251 brouard 5197: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5198: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
5199: j1=0;
1.222 brouard 5200:
1.227 brouard 5201: /*j=cptcoveff;*/
5202: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 5203:
1.288 brouard 5204: first=0;
1.227 brouard 5205: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
5206: for (i=1; i<=nlstate; i++)
1.251 brouard 5207: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
1.227 brouard 5208: prop[i][iage]=0.0;
5209: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
5210: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
5211: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
5212:
5213: for (i=1; i<=imx; i++) { /* Each individual */
5214: bool=1;
5215: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
5216: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
5217: m=mw[mi][i];
5218: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
5219: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
5220: for (z1=1; z1<=cptcoveff; z1++){
5221: if( Fixed[Tmodelind[z1]]==1){
5222: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
5223: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
5224: bool=0;
5225: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
5226: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
5227: bool=0;
5228: }
5229: }
5230: if(bool==1){ /* Otherwise we skip that wave/person */
5231: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
5232: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
5233: if(m >=firstpass && m <=lastpass){
5234: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
5235: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
5236: if(agev[m][i]==0) agev[m][i]=iagemax+1;
5237: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.251 brouard 5238: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
1.227 brouard 5239: 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);
5240: exit(1);
5241: }
5242: if (s[m][i]>0 && s[m][i]<=nlstate) {
5243: /*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]]);*/
5244: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
5245: prop[s[m][i]][iagemax+3] += weight[i];
5246: } /* end valid statuses */
5247: } /* end selection of dates */
5248: } /* end selection of waves */
5249: } /* end bool */
5250: } /* end wave */
5251: } /* end individual */
5252: for(i=iagemin; i <= iagemax+3; i++){
5253: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
5254: posprop += prop[jk][i];
5255: }
5256:
5257: for(jk=1; jk <=nlstate ; jk++){
5258: if( i <= iagemax){
5259: if(posprop>=1.e-5){
5260: probs[i][jk][j1]= prop[jk][i]/posprop;
5261: } else{
1.288 brouard 5262: if(!first){
5263: first=1;
1.266 brouard 5264: 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]);
5265: }else{
1.288 brouard 5266: 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 5267: }
5268: }
5269: }
5270: }/* end jk */
5271: }/* end i */
1.222 brouard 5272: /*} *//* end i1 */
1.227 brouard 5273: } /* end j1 */
1.222 brouard 5274:
1.227 brouard 5275: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
5276: /*free_vector(pp,1,nlstate);*/
1.251 brouard 5277: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5278: } /* End of prevalence */
1.126 brouard 5279:
5280: /************* Waves Concatenation ***************/
5281:
5282: 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)
5283: {
1.298 brouard 5284: /* 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 5285: Death is a valid wave (if date is known).
5286: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
5287: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.298 brouard 5288: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
1.227 brouard 5289: */
1.126 brouard 5290:
1.224 brouard 5291: int i=0, mi=0, m=0, mli=0;
1.126 brouard 5292: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
5293: double sum=0., jmean=0.;*/
1.224 brouard 5294: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 5295: int j, k=0,jk, ju, jl;
5296: double sum=0.;
5297: first=0;
1.214 brouard 5298: firstwo=0;
1.217 brouard 5299: firsthree=0;
1.218 brouard 5300: firstfour=0;
1.164 brouard 5301: jmin=100000;
1.126 brouard 5302: jmax=-1;
5303: jmean=0.;
1.224 brouard 5304:
5305: /* Treating live states */
1.214 brouard 5306: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 5307: mi=0; /* First valid wave */
1.227 brouard 5308: mli=0; /* Last valid wave */
1.309 brouard 5309: m=firstpass; /* Loop on waves */
5310: while(s[m][i] <= nlstate){ /* a live state or unknown state */
1.227 brouard 5311: 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 */
5312: mli=m-1;/* mw[++mi][i]=m-1; */
5313: }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 5314: 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 5315: mli=m;
1.224 brouard 5316: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
5317: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 5318: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 5319: }
1.309 brouard 5320: else{ /* m = lastpass, eventual special issue with warning */
1.224 brouard 5321: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 5322: break;
1.224 brouard 5323: #else
1.309 brouard 5324: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){ /* case -2 (vital status unknown is warned later */
1.227 brouard 5325: if(firsthree == 0){
1.302 brouard 5326: 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 5327: firsthree=1;
5328: }
1.302 brouard 5329: 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);
1.309 brouard 5330: mw[++mi][i]=m; /* Valid transition with unknown status */
1.227 brouard 5331: mli=m;
5332: }
5333: if(s[m][i]==-2){ /* Vital status is really unknown */
5334: nbwarn++;
1.309 brouard 5335: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
1.227 brouard 5336: 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);
5337: 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);
5338: }
5339: break;
5340: }
5341: break;
1.224 brouard 5342: #endif
1.227 brouard 5343: }/* End m >= lastpass */
1.126 brouard 5344: }/* end while */
1.224 brouard 5345:
1.227 brouard 5346: /* 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 5347: /* After last pass */
1.224 brouard 5348: /* Treating death states */
1.214 brouard 5349: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 5350: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
5351: /* } */
1.126 brouard 5352: mi++; /* Death is another wave */
5353: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 5354: /* Only death is a correct wave */
1.126 brouard 5355: mw[mi][i]=m;
1.257 brouard 5356: } /* else not in a death state */
1.224 brouard 5357: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.257 brouard 5358: else if ((int) andc[i] != 9999) { /* Date of death is known */
1.218 brouard 5359: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.309 brouard 5360: 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 5361: nbwarn++;
5362: if(firstfiv==0){
1.309 brouard 5363: 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 5364: firstfiv=1;
5365: }else{
1.309 brouard 5366: 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 5367: }
1.309 brouard 5368: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
5369: }else{ /* Month of Death occured afer last wave month, potential bias */
1.227 brouard 5370: nberr++;
5371: if(firstwo==0){
1.309 brouard 5372: 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 5373: firstwo=1;
5374: }
1.309 brouard 5375: 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 5376: }
1.257 brouard 5377: }else{ /* if date of interview is unknown */
1.227 brouard 5378: /* death is known but not confirmed by death status at any wave */
5379: if(firstfour==0){
1.309 brouard 5380: 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 5381: firstfour=1;
5382: }
1.309 brouard 5383: 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 5384: }
1.224 brouard 5385: } /* end if date of death is known */
5386: #endif
1.309 brouard 5387: wav[i]=mi; /* mi should be the last effective wave (or mli), */
5388: /* wav[i]=mw[mi][i]; */
1.126 brouard 5389: if(mi==0){
5390: nbwarn++;
5391: if(first==0){
1.227 brouard 5392: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
5393: first=1;
1.126 brouard 5394: }
5395: if(first==1){
1.227 brouard 5396: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 5397: }
5398: } /* end mi==0 */
5399: } /* End individuals */
1.214 brouard 5400: /* wav and mw are no more changed */
1.223 brouard 5401:
1.214 brouard 5402:
1.126 brouard 5403: for(i=1; i<=imx; i++){
5404: for(mi=1; mi<wav[i];mi++){
5405: if (stepm <=0)
1.227 brouard 5406: dh[mi][i]=1;
1.126 brouard 5407: else{
1.260 brouard 5408: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
1.227 brouard 5409: if (agedc[i] < 2*AGESUP) {
5410: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
5411: if(j==0) j=1; /* Survives at least one month after exam */
5412: else if(j<0){
5413: nberr++;
5414: 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]);
5415: j=1; /* Temporary Dangerous patch */
5416: 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);
5417: 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]);
5418: 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);
5419: }
5420: k=k+1;
5421: if (j >= jmax){
5422: jmax=j;
5423: ijmax=i;
5424: }
5425: if (j <= jmin){
5426: jmin=j;
5427: ijmin=i;
5428: }
5429: sum=sum+j;
5430: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
5431: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
5432: }
5433: }
5434: else{
5435: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 5436: /* 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 5437:
1.227 brouard 5438: k=k+1;
5439: if (j >= jmax) {
5440: jmax=j;
5441: ijmax=i;
5442: }
5443: else if (j <= jmin){
5444: jmin=j;
5445: ijmin=i;
5446: }
5447: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
5448: /*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]);*/
5449: if(j<0){
5450: nberr++;
5451: 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]);
5452: 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]);
5453: }
5454: sum=sum+j;
5455: }
5456: jk= j/stepm;
5457: jl= j -jk*stepm;
5458: ju= j -(jk+1)*stepm;
5459: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
5460: if(jl==0){
5461: dh[mi][i]=jk;
5462: bh[mi][i]=0;
5463: }else{ /* We want a negative bias in order to only have interpolation ie
5464: * to avoid the price of an extra matrix product in likelihood */
5465: dh[mi][i]=jk+1;
5466: bh[mi][i]=ju;
5467: }
5468: }else{
5469: if(jl <= -ju){
5470: dh[mi][i]=jk;
5471: bh[mi][i]=jl; /* bias is positive if real duration
5472: * is higher than the multiple of stepm and negative otherwise.
5473: */
5474: }
5475: else{
5476: dh[mi][i]=jk+1;
5477: bh[mi][i]=ju;
5478: }
5479: if(dh[mi][i]==0){
5480: dh[mi][i]=1; /* At least one step */
5481: bh[mi][i]=ju; /* At least one step */
5482: /* 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);*/
5483: }
5484: } /* end if mle */
1.126 brouard 5485: }
5486: } /* end wave */
5487: }
5488: jmean=sum/k;
5489: 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 5490: 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 5491: }
1.126 brouard 5492:
5493: /*********** Tricode ****************************/
1.220 brouard 5494: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 5495: {
5496: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
5497: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
5498: * Boring subroutine which should only output nbcode[Tvar[j]][k]
5499: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
5500: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
5501: */
1.130 brouard 5502:
1.242 brouard 5503: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
5504: int modmaxcovj=0; /* Modality max of covariates j */
5505: int cptcode=0; /* Modality max of covariates j */
5506: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 5507:
5508:
1.242 brouard 5509: /* cptcoveff=0; */
5510: /* *cptcov=0; */
1.126 brouard 5511:
1.242 brouard 5512: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.285 brouard 5513: for (k=1; k <= maxncov; k++)
5514: for(j=1; j<=2; j++)
5515: nbcode[k][j]=0; /* Valgrind */
1.126 brouard 5516:
1.242 brouard 5517: /* Loop on covariates without age and products and no quantitative variable */
5518: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
5519: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
5520: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
5521: switch(Fixed[k]) {
5522: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.311 brouard 5523: modmaxcovj=0;
5524: modmincovj=0;
1.242 brouard 5525: 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*/
5526: ij=(int)(covar[Tvar[k]][i]);
5527: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
5528: * If product of Vn*Vm, still boolean *:
5529: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
5530: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
5531: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
5532: modality of the nth covariate of individual i. */
5533: if (ij > modmaxcovj)
5534: modmaxcovj=ij;
5535: else if (ij < modmincovj)
5536: modmincovj=ij;
1.287 brouard 5537: if (ij <0 || ij >1 ){
1.311 brouard 5538: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
5539: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
5540: fflush(ficlog);
5541: exit(1);
1.287 brouard 5542: }
5543: if ((ij < -1) || (ij > NCOVMAX)){
1.242 brouard 5544: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
5545: exit(1);
5546: }else
5547: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
5548: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
5549: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
5550: /* getting the maximum value of the modality of the covariate
5551: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
5552: female ies 1, then modmaxcovj=1.
5553: */
5554: } /* end for loop on individuals i */
5555: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5556: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5557: cptcode=modmaxcovj;
5558: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
5559: /*for (i=0; i<=cptcode; i++) {*/
5560: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
5561: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5562: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5563: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
5564: if( j != -1){
5565: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
5566: covariate for which somebody answered excluding
5567: undefined. Usually 2: 0 and 1. */
5568: }
5569: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
5570: covariate for which somebody answered including
5571: undefined. Usually 3: -1, 0 and 1. */
5572: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
5573: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
5574: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 5575:
1.242 brouard 5576: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
5577: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
5578: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
5579: /* modmincovj=3; modmaxcovj = 7; */
5580: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
5581: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
5582: /* defining two dummy variables: variables V1_1 and V1_2.*/
5583: /* nbcode[Tvar[j]][ij]=k; */
5584: /* nbcode[Tvar[j]][1]=0; */
5585: /* nbcode[Tvar[j]][2]=1; */
5586: /* nbcode[Tvar[j]][3]=2; */
5587: /* To be continued (not working yet). */
5588: ij=0; /* ij is similar to i but can jump over null modalities */
1.287 brouard 5589:
5590: /* 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*/
5591: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
5592: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
5593: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
5594: /*, could be restored in the future */
5595: 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 5596: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
5597: break;
5598: }
5599: ij++;
1.287 brouard 5600: 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 5601: cptcode = ij; /* New max modality for covar j */
5602: } /* end of loop on modality i=-1 to 1 or more */
5603: break;
5604: case 1: /* Testing on varying covariate, could be simple and
5605: * should look at waves or product of fixed *
5606: * varying. No time to test -1, assuming 0 and 1 only */
5607: ij=0;
5608: for(i=0; i<=1;i++){
5609: nbcode[Tvar[k]][++ij]=i;
5610: }
5611: break;
5612: default:
5613: break;
5614: } /* end switch */
5615: } /* end dummy test */
1.311 brouard 5616: if(Dummy[k]==1 && Typevar[k] !=1){ /* Dummy covariate and not age product */
5617: 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*/
5618: if(isnan(covar[Tvar[k]][i])){
5619: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
5620: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
5621: fflush(ficlog);
5622: exit(1);
5623: }
5624: }
5625: }
1.287 brouard 5626: } /* 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 5627:
5628: for (k=-1; k< maxncov; k++) Ndum[k]=0;
5629: /* Look at fixed dummy (single or product) covariates to check empty modalities */
5630: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
5631: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
5632: 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 */
5633: 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 */
5634: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
5635: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
5636:
5637: ij=0;
5638: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
5639: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
5640: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
5641: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
5642: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
5643: /* If product not in single variable we don't print results */
5644: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
5645: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
5646: 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*/
5647: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
5648: 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 */
5649: if(Fixed[k]!=0)
5650: anyvaryingduminmodel=1;
5651: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
5652: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
5653: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
5654: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
5655: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
5656: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
5657: }
5658: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
5659: /* ij--; */
5660: /* cptcoveff=ij; /\*Number of total covariates*\/ */
5661: *cptcov=ij; /*Number of total real effective covariates: effective
5662: * because they can be excluded from the model and real
5663: * if in the model but excluded because missing values, but how to get k from ij?*/
5664: for(j=ij+1; j<= cptcovt; j++){
5665: Tvaraff[j]=0;
5666: Tmodelind[j]=0;
5667: }
5668: for(j=ntveff+1; j<= cptcovt; j++){
5669: TmodelInvind[j]=0;
5670: }
5671: /* To be sorted */
5672: ;
5673: }
1.126 brouard 5674:
1.145 brouard 5675:
1.126 brouard 5676: /*********** Health Expectancies ****************/
5677:
1.235 brouard 5678: 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 5679:
5680: {
5681: /* Health expectancies, no variances */
1.164 brouard 5682: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 5683: int nhstepma, nstepma; /* Decreasing with age */
5684: double age, agelim, hf;
5685: double ***p3mat;
5686: double eip;
5687:
1.238 brouard 5688: /* pstamp(ficreseij); */
1.126 brouard 5689: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
5690: fprintf(ficreseij,"# Age");
5691: for(i=1; i<=nlstate;i++){
5692: for(j=1; j<=nlstate;j++){
5693: fprintf(ficreseij," e%1d%1d ",i,j);
5694: }
5695: fprintf(ficreseij," e%1d. ",i);
5696: }
5697: fprintf(ficreseij,"\n");
5698:
5699:
5700: if(estepm < stepm){
5701: printf ("Problem %d lower than %d\n",estepm, stepm);
5702: }
5703: else hstepm=estepm;
5704: /* We compute the life expectancy from trapezoids spaced every estepm months
5705: * This is mainly to measure the difference between two models: for example
5706: * if stepm=24 months pijx are given only every 2 years and by summing them
5707: * we are calculating an estimate of the Life Expectancy assuming a linear
5708: * progression in between and thus overestimating or underestimating according
5709: * to the curvature of the survival function. If, for the same date, we
5710: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5711: * to compare the new estimate of Life expectancy with the same linear
5712: * hypothesis. A more precise result, taking into account a more precise
5713: * curvature will be obtained if estepm is as small as stepm. */
5714:
5715: /* For example we decided to compute the life expectancy with the smallest unit */
5716: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5717: nhstepm is the number of hstepm from age to agelim
5718: nstepm is the number of stepm from age to agelin.
1.270 brouard 5719: Look at hpijx to understand the reason which relies in memory size consideration
1.126 brouard 5720: and note for a fixed period like estepm months */
5721: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5722: survival function given by stepm (the optimization length). Unfortunately it
5723: means that if the survival funtion is printed only each two years of age and if
5724: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5725: results. So we changed our mind and took the option of the best precision.
5726: */
5727: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5728:
5729: agelim=AGESUP;
5730: /* If stepm=6 months */
5731: /* Computed by stepm unit matrices, product of hstepm matrices, stored
5732: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
5733:
5734: /* nhstepm age range expressed in number of stepm */
5735: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5736: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5737: /* if (stepm >= YEARM) hstepm=1;*/
5738: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5739: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5740:
5741: for (age=bage; age<=fage; age ++){
5742: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5743: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5744: /* if (stepm >= YEARM) hstepm=1;*/
5745: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
5746:
5747: /* If stepm=6 months */
5748: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5749: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5750:
1.235 brouard 5751: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 5752:
5753: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5754:
5755: printf("%d|",(int)age);fflush(stdout);
5756: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5757:
5758: /* Computing expectancies */
5759: for(i=1; i<=nlstate;i++)
5760: for(j=1; j<=nlstate;j++)
5761: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5762: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
5763:
5764: /* 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]);*/
5765:
5766: }
5767:
5768: fprintf(ficreseij,"%3.0f",age );
5769: for(i=1; i<=nlstate;i++){
5770: eip=0;
5771: for(j=1; j<=nlstate;j++){
5772: eip +=eij[i][j][(int)age];
5773: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
5774: }
5775: fprintf(ficreseij,"%9.4f", eip );
5776: }
5777: fprintf(ficreseij,"\n");
5778:
5779: }
5780: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5781: printf("\n");
5782: fprintf(ficlog,"\n");
5783:
5784: }
5785:
1.235 brouard 5786: 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 5787:
5788: {
5789: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 5790: to initial status i, ei. .
1.126 brouard 5791: */
5792: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
5793: int nhstepma, nstepma; /* Decreasing with age */
5794: double age, agelim, hf;
5795: double ***p3matp, ***p3matm, ***varhe;
5796: double **dnewm,**doldm;
5797: double *xp, *xm;
5798: double **gp, **gm;
5799: double ***gradg, ***trgradg;
5800: int theta;
5801:
5802: double eip, vip;
5803:
5804: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
5805: xp=vector(1,npar);
5806: xm=vector(1,npar);
5807: dnewm=matrix(1,nlstate*nlstate,1,npar);
5808: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
5809:
5810: pstamp(ficresstdeij);
5811: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
5812: fprintf(ficresstdeij,"# Age");
5813: for(i=1; i<=nlstate;i++){
5814: for(j=1; j<=nlstate;j++)
5815: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
5816: fprintf(ficresstdeij," e%1d. ",i);
5817: }
5818: fprintf(ficresstdeij,"\n");
5819:
5820: pstamp(ficrescveij);
5821: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
5822: fprintf(ficrescveij,"# Age");
5823: for(i=1; i<=nlstate;i++)
5824: for(j=1; j<=nlstate;j++){
5825: cptj= (j-1)*nlstate+i;
5826: for(i2=1; i2<=nlstate;i2++)
5827: for(j2=1; j2<=nlstate;j2++){
5828: cptj2= (j2-1)*nlstate+i2;
5829: if(cptj2 <= cptj)
5830: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
5831: }
5832: }
5833: fprintf(ficrescveij,"\n");
5834:
5835: if(estepm < stepm){
5836: printf ("Problem %d lower than %d\n",estepm, stepm);
5837: }
5838: else hstepm=estepm;
5839: /* We compute the life expectancy from trapezoids spaced every estepm months
5840: * This is mainly to measure the difference between two models: for example
5841: * if stepm=24 months pijx are given only every 2 years and by summing them
5842: * we are calculating an estimate of the Life Expectancy assuming a linear
5843: * progression in between and thus overestimating or underestimating according
5844: * to the curvature of the survival function. If, for the same date, we
5845: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5846: * to compare the new estimate of Life expectancy with the same linear
5847: * hypothesis. A more precise result, taking into account a more precise
5848: * curvature will be obtained if estepm is as small as stepm. */
5849:
5850: /* For example we decided to compute the life expectancy with the smallest unit */
5851: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5852: nhstepm is the number of hstepm from age to agelim
5853: nstepm is the number of stepm from age to agelin.
5854: Look at hpijx to understand the reason of that which relies in memory size
5855: and note for a fixed period like estepm months */
5856: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5857: survival function given by stepm (the optimization length). Unfortunately it
5858: means that if the survival funtion is printed only each two years of age and if
5859: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5860: results. So we changed our mind and took the option of the best precision.
5861: */
5862: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5863:
5864: /* If stepm=6 months */
5865: /* nhstepm age range expressed in number of stepm */
5866: agelim=AGESUP;
5867: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
5868: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5869: /* if (stepm >= YEARM) hstepm=1;*/
5870: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5871:
5872: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5873: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5874: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5875: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5876: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5877: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5878:
5879: for (age=bage; age<=fage; age ++){
5880: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5881: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5882: /* if (stepm >= YEARM) hstepm=1;*/
5883: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5884:
1.126 brouard 5885: /* If stepm=6 months */
5886: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5887: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5888:
5889: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5890:
1.126 brouard 5891: /* Computing Variances of health expectancies */
5892: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5893: decrease memory allocation */
5894: for(theta=1; theta <=npar; theta++){
5895: for(i=1; i<=npar; i++){
1.222 brouard 5896: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5897: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5898: }
1.235 brouard 5899: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
5900: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 5901:
1.126 brouard 5902: for(j=1; j<= nlstate; j++){
1.222 brouard 5903: for(i=1; i<=nlstate; i++){
5904: for(h=0; h<=nhstepm-1; h++){
5905: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5906: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5907: }
5908: }
1.126 brouard 5909: }
1.218 brouard 5910:
1.126 brouard 5911: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5912: for(h=0; h<=nhstepm-1; h++){
5913: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5914: }
1.126 brouard 5915: }/* End theta */
5916:
5917:
5918: for(h=0; h<=nhstepm-1; h++)
5919: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5920: for(theta=1; theta <=npar; theta++)
5921: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5922:
1.218 brouard 5923:
1.222 brouard 5924: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5925: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5926: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5927:
1.222 brouard 5928: printf("%d|",(int)age);fflush(stdout);
5929: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5930: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5931: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5932: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5933: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5934: for(ij=1;ij<=nlstate*nlstate;ij++)
5935: for(ji=1;ji<=nlstate*nlstate;ji++)
5936: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5937: }
5938: }
1.218 brouard 5939:
1.126 brouard 5940: /* Computing expectancies */
1.235 brouard 5941: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 5942: for(i=1; i<=nlstate;i++)
5943: for(j=1; j<=nlstate;j++)
1.222 brouard 5944: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5945: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5946:
1.222 brouard 5947: /* 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 5948:
1.222 brouard 5949: }
1.269 brouard 5950:
5951: /* Standard deviation of expectancies ij */
1.126 brouard 5952: fprintf(ficresstdeij,"%3.0f",age );
5953: for(i=1; i<=nlstate;i++){
5954: eip=0.;
5955: vip=0.;
5956: for(j=1; j<=nlstate;j++){
1.222 brouard 5957: eip += eij[i][j][(int)age];
5958: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5959: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5960: 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 5961: }
5962: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5963: }
5964: fprintf(ficresstdeij,"\n");
1.218 brouard 5965:
1.269 brouard 5966: /* Variance of expectancies ij */
1.126 brouard 5967: fprintf(ficrescveij,"%3.0f",age );
5968: for(i=1; i<=nlstate;i++)
5969: for(j=1; j<=nlstate;j++){
1.222 brouard 5970: cptj= (j-1)*nlstate+i;
5971: for(i2=1; i2<=nlstate;i2++)
5972: for(j2=1; j2<=nlstate;j2++){
5973: cptj2= (j2-1)*nlstate+i2;
5974: if(cptj2 <= cptj)
5975: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5976: }
1.126 brouard 5977: }
5978: fprintf(ficrescveij,"\n");
1.218 brouard 5979:
1.126 brouard 5980: }
5981: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5982: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5983: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5984: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5985: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5986: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5987: printf("\n");
5988: fprintf(ficlog,"\n");
1.218 brouard 5989:
1.126 brouard 5990: free_vector(xm,1,npar);
5991: free_vector(xp,1,npar);
5992: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5993: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5994: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5995: }
1.218 brouard 5996:
1.126 brouard 5997: /************ Variance ******************/
1.235 brouard 5998: 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 5999: {
1.279 brouard 6000: /** Variance of health expectancies
6001: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
6002: * double **newm;
6003: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
6004: */
1.218 brouard 6005:
6006: /* int movingaverage(); */
6007: double **dnewm,**doldm;
6008: double **dnewmp,**doldmp;
6009: int i, j, nhstepm, hstepm, h, nstepm ;
1.288 brouard 6010: int first=0;
1.218 brouard 6011: int k;
6012: double *xp;
1.279 brouard 6013: double **gp, **gm; /**< for var eij */
6014: double ***gradg, ***trgradg; /**< for var eij */
6015: double **gradgp, **trgradgp; /**< for var p point j */
6016: double *gpp, *gmp; /**< for var p point j */
6017: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
1.218 brouard 6018: double ***p3mat;
6019: double age,agelim, hf;
6020: /* double ***mobaverage; */
6021: int theta;
6022: char digit[4];
6023: char digitp[25];
6024:
6025: char fileresprobmorprev[FILENAMELENGTH];
6026:
6027: if(popbased==1){
6028: if(mobilav!=0)
6029: strcpy(digitp,"-POPULBASED-MOBILAV_");
6030: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
6031: }
6032: else
6033: strcpy(digitp,"-STABLBASED_");
1.126 brouard 6034:
1.218 brouard 6035: /* if (mobilav!=0) { */
6036: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6037: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
6038: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
6039: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
6040: /* } */
6041: /* } */
6042:
6043: strcpy(fileresprobmorprev,"PRMORPREV-");
6044: sprintf(digit,"%-d",ij);
6045: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
6046: strcat(fileresprobmorprev,digit); /* Tvar to be done */
6047: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
6048: strcat(fileresprobmorprev,fileresu);
6049: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
6050: printf("Problem with resultfile: %s\n", fileresprobmorprev);
6051: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
6052: }
6053: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6054: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6055: pstamp(ficresprobmorprev);
6056: 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 6057: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
6058: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
6059: fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
6060: }
6061: for(j=1;j<=cptcoveff;j++)
6062: fprintf(ficresprobmorprev,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,j)]);
6063: fprintf(ficresprobmorprev,"\n");
6064:
1.218 brouard 6065: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
6066: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6067: fprintf(ficresprobmorprev," p.%-d SE",j);
6068: for(i=1; i<=nlstate;i++)
6069: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
6070: }
6071: fprintf(ficresprobmorprev,"\n");
6072:
6073: fprintf(ficgp,"\n# Routine varevsij");
6074: fprintf(ficgp,"\nunset title \n");
6075: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
6076: 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");
6077: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
1.279 brouard 6078:
1.218 brouard 6079: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6080: pstamp(ficresvij);
6081: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
6082: if(popbased==1)
6083: 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);
6084: else
6085: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
6086: fprintf(ficresvij,"# Age");
6087: for(i=1; i<=nlstate;i++)
6088: for(j=1; j<=nlstate;j++)
6089: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
6090: fprintf(ficresvij,"\n");
6091:
6092: xp=vector(1,npar);
6093: dnewm=matrix(1,nlstate,1,npar);
6094: doldm=matrix(1,nlstate,1,nlstate);
6095: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
6096: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6097:
6098: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
6099: gpp=vector(nlstate+1,nlstate+ndeath);
6100: gmp=vector(nlstate+1,nlstate+ndeath);
6101: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 6102:
1.218 brouard 6103: if(estepm < stepm){
6104: printf ("Problem %d lower than %d\n",estepm, stepm);
6105: }
6106: else hstepm=estepm;
6107: /* For example we decided to compute the life expectancy with the smallest unit */
6108: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6109: nhstepm is the number of hstepm from age to agelim
6110: nstepm is the number of stepm from age to agelim.
6111: Look at function hpijx to understand why because of memory size limitations,
6112: we decided (b) to get a life expectancy respecting the most precise curvature of the
6113: survival function given by stepm (the optimization length). Unfortunately it
6114: means that if the survival funtion is printed every two years of age and if
6115: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6116: results. So we changed our mind and took the option of the best precision.
6117: */
6118: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6119: agelim = AGESUP;
6120: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6121: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6122: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6123: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6124: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
6125: gp=matrix(0,nhstepm,1,nlstate);
6126: gm=matrix(0,nhstepm,1,nlstate);
6127:
6128:
6129: for(theta=1; theta <=npar; theta++){
6130: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
6131: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6132: }
1.279 brouard 6133: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
6134: * returns into prlim .
1.288 brouard 6135: */
1.242 brouard 6136: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.279 brouard 6137:
6138: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
1.218 brouard 6139: if (popbased==1) {
6140: if(mobilav ==0){
6141: for(i=1; i<=nlstate;i++)
6142: prlim[i][i]=probs[(int)age][i][ij];
6143: }else{ /* mobilav */
6144: for(i=1; i<=nlstate;i++)
6145: prlim[i][i]=mobaverage[(int)age][i][ij];
6146: }
6147: }
1.295 brouard 6148: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
1.279 brouard 6149: */
6150: 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 6151: /**< 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 6152: * at horizon h in state j including mortality.
6153: */
1.218 brouard 6154: for(j=1; j<= nlstate; j++){
6155: for(h=0; h<=nhstepm; h++){
6156: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
6157: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
6158: }
6159: }
1.279 brouard 6160: /* Next for computing shifted+ probability of death (h=1 means
1.218 brouard 6161: computed over hstepm matrices product = hstepm*stepm months)
1.279 brouard 6162: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
1.218 brouard 6163: */
6164: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6165: for(i=1,gpp[j]=0.; i<= nlstate; i++)
6166: gpp[j] += prlim[i][i]*p3mat[i][j][1];
1.279 brouard 6167: }
6168:
6169: /* Again with minus shift */
1.218 brouard 6170:
6171: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
6172: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6173:
1.242 brouard 6174: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 6175:
6176: if (popbased==1) {
6177: if(mobilav ==0){
6178: for(i=1; i<=nlstate;i++)
6179: prlim[i][i]=probs[(int)age][i][ij];
6180: }else{ /* mobilav */
6181: for(i=1; i<=nlstate;i++)
6182: prlim[i][i]=mobaverage[(int)age][i][ij];
6183: }
6184: }
6185:
1.235 brouard 6186: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 6187:
6188: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
6189: for(h=0; h<=nhstepm; h++){
6190: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
6191: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
6192: }
6193: }
6194: /* This for computing probability of death (h=1 means
6195: computed over hstepm matrices product = hstepm*stepm months)
6196: as a weighted average of prlim.
6197: */
6198: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6199: for(i=1,gmp[j]=0.; i<= nlstate; i++)
6200: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6201: }
1.279 brouard 6202: /* end shifting computations */
6203:
6204: /**< Computing gradient matrix at horizon h
6205: */
1.218 brouard 6206: for(j=1; j<= nlstate; j++) /* vareij */
6207: for(h=0; h<=nhstepm; h++){
6208: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
6209: }
1.279 brouard 6210: /**< Gradient of overall mortality p.3 (or p.j)
6211: */
6212: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
1.218 brouard 6213: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
6214: }
6215:
6216: } /* End theta */
1.279 brouard 6217:
6218: /* We got the gradient matrix for each theta and state j */
1.218 brouard 6219: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
6220:
6221: for(h=0; h<=nhstepm; h++) /* veij */
6222: for(j=1; j<=nlstate;j++)
6223: for(theta=1; theta <=npar; theta++)
6224: trgradg[h][j][theta]=gradg[h][theta][j];
6225:
6226: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
6227: for(theta=1; theta <=npar; theta++)
6228: trgradgp[j][theta]=gradgp[theta][j];
1.279 brouard 6229: /**< as well as its transposed matrix
6230: */
1.218 brouard 6231:
6232: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6233: for(i=1;i<=nlstate;i++)
6234: for(j=1;j<=nlstate;j++)
6235: vareij[i][j][(int)age] =0.;
1.279 brouard 6236:
6237: /* Computing trgradg by matcov by gradg at age and summing over h
6238: * and k (nhstepm) formula 15 of article
6239: * Lievre-Brouard-Heathcote
6240: */
6241:
1.218 brouard 6242: for(h=0;h<=nhstepm;h++){
6243: for(k=0;k<=nhstepm;k++){
6244: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
6245: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
6246: for(i=1;i<=nlstate;i++)
6247: for(j=1;j<=nlstate;j++)
6248: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
6249: }
6250: }
6251:
1.279 brouard 6252: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
6253: * p.j overall mortality formula 49 but computed directly because
6254: * we compute the grad (wix pijx) instead of grad (pijx),even if
6255: * wix is independent of theta.
6256: */
1.218 brouard 6257: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
6258: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
6259: for(j=nlstate+1;j<=nlstate+ndeath;j++)
6260: for(i=nlstate+1;i<=nlstate+ndeath;i++)
6261: varppt[j][i]=doldmp[j][i];
6262: /* end ppptj */
6263: /* x centered again */
6264:
1.242 brouard 6265: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 6266:
6267: if (popbased==1) {
6268: if(mobilav ==0){
6269: for(i=1; i<=nlstate;i++)
6270: prlim[i][i]=probs[(int)age][i][ij];
6271: }else{ /* mobilav */
6272: for(i=1; i<=nlstate;i++)
6273: prlim[i][i]=mobaverage[(int)age][i][ij];
6274: }
6275: }
6276:
6277: /* This for computing probability of death (h=1 means
6278: computed over hstepm (estepm) matrices product = hstepm*stepm months)
6279: as a weighted average of prlim.
6280: */
1.235 brouard 6281: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 6282: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6283: for(i=1,gmp[j]=0.;i<= nlstate; i++)
6284: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6285: }
6286: /* end probability of death */
6287:
6288: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
6289: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6290: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
6291: for(i=1; i<=nlstate;i++){
6292: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
6293: }
6294: }
6295: fprintf(ficresprobmorprev,"\n");
6296:
6297: fprintf(ficresvij,"%.0f ",age );
6298: for(i=1; i<=nlstate;i++)
6299: for(j=1; j<=nlstate;j++){
6300: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
6301: }
6302: fprintf(ficresvij,"\n");
6303: free_matrix(gp,0,nhstepm,1,nlstate);
6304: free_matrix(gm,0,nhstepm,1,nlstate);
6305: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
6306: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
6307: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6308: } /* End age */
6309: free_vector(gpp,nlstate+1,nlstate+ndeath);
6310: free_vector(gmp,nlstate+1,nlstate+ndeath);
6311: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
6312: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
6313: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
6314: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
6315: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
6316: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
6317: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
6318: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
6319: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
6320: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
6321: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
6322: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
6323: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
6324: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
6325: 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);
6326: /* 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 6327: */
1.218 brouard 6328: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
6329: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 6330:
1.218 brouard 6331: free_vector(xp,1,npar);
6332: free_matrix(doldm,1,nlstate,1,nlstate);
6333: free_matrix(dnewm,1,nlstate,1,npar);
6334: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6335: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
6336: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6337: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6338: fclose(ficresprobmorprev);
6339: fflush(ficgp);
6340: fflush(fichtm);
6341: } /* end varevsij */
1.126 brouard 6342:
6343: /************ Variance of prevlim ******************/
1.269 brouard 6344: 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 6345: {
1.205 brouard 6346: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 6347: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 6348:
1.268 brouard 6349: double **dnewmpar,**doldm;
1.126 brouard 6350: int i, j, nhstepm, hstepm;
6351: double *xp;
6352: double *gp, *gm;
6353: double **gradg, **trgradg;
1.208 brouard 6354: double **mgm, **mgp;
1.126 brouard 6355: double age,agelim;
6356: int theta;
6357:
6358: pstamp(ficresvpl);
1.288 brouard 6359: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
1.241 brouard 6360: fprintf(ficresvpl,"# Age ");
6361: if(nresult >=1)
6362: fprintf(ficresvpl," Result# ");
1.126 brouard 6363: for(i=1; i<=nlstate;i++)
6364: fprintf(ficresvpl," %1d-%1d",i,i);
6365: fprintf(ficresvpl,"\n");
6366:
6367: xp=vector(1,npar);
1.268 brouard 6368: dnewmpar=matrix(1,nlstate,1,npar);
1.126 brouard 6369: doldm=matrix(1,nlstate,1,nlstate);
6370:
6371: hstepm=1*YEARM; /* Every year of age */
6372: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6373: agelim = AGESUP;
6374: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6375: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6376: if (stepm >= YEARM) hstepm=1;
6377: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6378: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 6379: mgp=matrix(1,npar,1,nlstate);
6380: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 6381: gp=vector(1,nlstate);
6382: gm=vector(1,nlstate);
6383:
6384: for(theta=1; theta <=npar; theta++){
6385: for(i=1; i<=npar; i++){ /* Computes gradient */
6386: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6387: }
1.288 brouard 6388: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6389: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6390: /* else */
6391: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6392: for(i=1;i<=nlstate;i++){
1.126 brouard 6393: gp[i] = prlim[i][i];
1.208 brouard 6394: mgp[theta][i] = prlim[i][i];
6395: }
1.126 brouard 6396: for(i=1; i<=npar; i++) /* Computes gradient */
6397: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6398: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6399: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6400: /* else */
6401: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6402: for(i=1;i<=nlstate;i++){
1.126 brouard 6403: gm[i] = prlim[i][i];
1.208 brouard 6404: mgm[theta][i] = prlim[i][i];
6405: }
1.126 brouard 6406: for(i=1;i<=nlstate;i++)
6407: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 6408: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 6409: } /* End theta */
6410:
6411: trgradg =matrix(1,nlstate,1,npar);
6412:
6413: for(j=1; j<=nlstate;j++)
6414: for(theta=1; theta <=npar; theta++)
6415: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 6416: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6417: /* printf("\nmgm mgp %d ",(int)age); */
6418: /* for(j=1; j<=nlstate;j++){ */
6419: /* printf(" %d ",j); */
6420: /* for(theta=1; theta <=npar; theta++) */
6421: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6422: /* printf("\n "); */
6423: /* } */
6424: /* } */
6425: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6426: /* printf("\n gradg %d ",(int)age); */
6427: /* for(j=1; j<=nlstate;j++){ */
6428: /* printf("%d ",j); */
6429: /* for(theta=1; theta <=npar; theta++) */
6430: /* printf("%d %lf ",theta,gradg[theta][j]); */
6431: /* printf("\n "); */
6432: /* } */
6433: /* } */
1.126 brouard 6434:
6435: for(i=1;i<=nlstate;i++)
6436: varpl[i][(int)age] =0.;
1.209 brouard 6437: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.268 brouard 6438: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6439: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6440: }else{
1.268 brouard 6441: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6442: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6443: }
1.126 brouard 6444: for(i=1;i<=nlstate;i++)
6445: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6446:
6447: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 6448: if(nresult >=1)
6449: fprintf(ficresvpl,"%d ",nres );
1.288 brouard 6450: for(i=1; i<=nlstate;i++){
1.126 brouard 6451: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1.288 brouard 6452: /* for(j=1;j<=nlstate;j++) */
6453: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
6454: }
1.126 brouard 6455: fprintf(ficresvpl,"\n");
6456: free_vector(gp,1,nlstate);
6457: free_vector(gm,1,nlstate);
1.208 brouard 6458: free_matrix(mgm,1,npar,1,nlstate);
6459: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 6460: free_matrix(gradg,1,npar,1,nlstate);
6461: free_matrix(trgradg,1,nlstate,1,npar);
6462: } /* End age */
6463:
6464: free_vector(xp,1,npar);
6465: free_matrix(doldm,1,nlstate,1,npar);
1.268 brouard 6466: free_matrix(dnewmpar,1,nlstate,1,nlstate);
6467:
6468: }
6469:
6470:
6471: /************ Variance of backprevalence limit ******************/
1.269 brouard 6472: 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 6473: {
6474: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
6475: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
6476:
6477: double **dnewmpar,**doldm;
6478: int i, j, nhstepm, hstepm;
6479: double *xp;
6480: double *gp, *gm;
6481: double **gradg, **trgradg;
6482: double **mgm, **mgp;
6483: double age,agelim;
6484: int theta;
6485:
6486: pstamp(ficresvbl);
6487: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
6488: fprintf(ficresvbl,"# Age ");
6489: if(nresult >=1)
6490: fprintf(ficresvbl," Result# ");
6491: for(i=1; i<=nlstate;i++)
6492: fprintf(ficresvbl," %1d-%1d",i,i);
6493: fprintf(ficresvbl,"\n");
6494:
6495: xp=vector(1,npar);
6496: dnewmpar=matrix(1,nlstate,1,npar);
6497: doldm=matrix(1,nlstate,1,nlstate);
6498:
6499: hstepm=1*YEARM; /* Every year of age */
6500: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6501: agelim = AGEINF;
6502: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
6503: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6504: if (stepm >= YEARM) hstepm=1;
6505: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6506: gradg=matrix(1,npar,1,nlstate);
6507: mgp=matrix(1,npar,1,nlstate);
6508: mgm=matrix(1,npar,1,nlstate);
6509: gp=vector(1,nlstate);
6510: gm=vector(1,nlstate);
6511:
6512: for(theta=1; theta <=npar; theta++){
6513: for(i=1; i<=npar; i++){ /* Computes gradient */
6514: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6515: }
6516: if(mobilavproj > 0 )
6517: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6518: else
6519: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6520: for(i=1;i<=nlstate;i++){
6521: gp[i] = bprlim[i][i];
6522: mgp[theta][i] = bprlim[i][i];
6523: }
6524: for(i=1; i<=npar; i++) /* Computes gradient */
6525: xp[i] = x[i] - (i==theta ?delti[theta]:0);
6526: if(mobilavproj > 0 )
6527: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6528: else
6529: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6530: for(i=1;i<=nlstate;i++){
6531: gm[i] = bprlim[i][i];
6532: mgm[theta][i] = bprlim[i][i];
6533: }
6534: for(i=1;i<=nlstate;i++)
6535: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
6536: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
6537: } /* End theta */
6538:
6539: trgradg =matrix(1,nlstate,1,npar);
6540:
6541: for(j=1; j<=nlstate;j++)
6542: for(theta=1; theta <=npar; theta++)
6543: trgradg[j][theta]=gradg[theta][j];
6544: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6545: /* printf("\nmgm mgp %d ",(int)age); */
6546: /* for(j=1; j<=nlstate;j++){ */
6547: /* printf(" %d ",j); */
6548: /* for(theta=1; theta <=npar; theta++) */
6549: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6550: /* printf("\n "); */
6551: /* } */
6552: /* } */
6553: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6554: /* printf("\n gradg %d ",(int)age); */
6555: /* for(j=1; j<=nlstate;j++){ */
6556: /* printf("%d ",j); */
6557: /* for(theta=1; theta <=npar; theta++) */
6558: /* printf("%d %lf ",theta,gradg[theta][j]); */
6559: /* printf("\n "); */
6560: /* } */
6561: /* } */
6562:
6563: for(i=1;i<=nlstate;i++)
6564: varbpl[i][(int)age] =0.;
6565: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
6566: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6567: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6568: }else{
6569: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6570: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6571: }
6572: for(i=1;i<=nlstate;i++)
6573: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6574:
6575: fprintf(ficresvbl,"%.0f ",age );
6576: if(nresult >=1)
6577: fprintf(ficresvbl,"%d ",nres );
6578: for(i=1; i<=nlstate;i++)
6579: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
6580: fprintf(ficresvbl,"\n");
6581: free_vector(gp,1,nlstate);
6582: free_vector(gm,1,nlstate);
6583: free_matrix(mgm,1,npar,1,nlstate);
6584: free_matrix(mgp,1,npar,1,nlstate);
6585: free_matrix(gradg,1,npar,1,nlstate);
6586: free_matrix(trgradg,1,nlstate,1,npar);
6587: } /* End age */
6588:
6589: free_vector(xp,1,npar);
6590: free_matrix(doldm,1,nlstate,1,npar);
6591: free_matrix(dnewmpar,1,nlstate,1,nlstate);
1.126 brouard 6592:
6593: }
6594:
6595: /************ Variance of one-step probabilities ******************/
6596: 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 6597: {
6598: int i, j=0, k1, l1, tj;
6599: int k2, l2, j1, z1;
6600: int k=0, l;
6601: int first=1, first1, first2;
6602: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
6603: double **dnewm,**doldm;
6604: double *xp;
6605: double *gp, *gm;
6606: double **gradg, **trgradg;
6607: double **mu;
6608: double age, cov[NCOVMAX+1];
6609: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
6610: int theta;
6611: char fileresprob[FILENAMELENGTH];
6612: char fileresprobcov[FILENAMELENGTH];
6613: char fileresprobcor[FILENAMELENGTH];
6614: double ***varpij;
6615:
6616: strcpy(fileresprob,"PROB_");
6617: strcat(fileresprob,fileres);
6618: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
6619: printf("Problem with resultfile: %s\n", fileresprob);
6620: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
6621: }
6622: strcpy(fileresprobcov,"PROBCOV_");
6623: strcat(fileresprobcov,fileresu);
6624: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
6625: printf("Problem with resultfile: %s\n", fileresprobcov);
6626: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
6627: }
6628: strcpy(fileresprobcor,"PROBCOR_");
6629: strcat(fileresprobcor,fileresu);
6630: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
6631: printf("Problem with resultfile: %s\n", fileresprobcor);
6632: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
6633: }
6634: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6635: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6636: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6637: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6638: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6639: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6640: pstamp(ficresprob);
6641: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
6642: fprintf(ficresprob,"# Age");
6643: pstamp(ficresprobcov);
6644: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
6645: fprintf(ficresprobcov,"# Age");
6646: pstamp(ficresprobcor);
6647: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
6648: fprintf(ficresprobcor,"# Age");
1.126 brouard 6649:
6650:
1.222 brouard 6651: for(i=1; i<=nlstate;i++)
6652: for(j=1; j<=(nlstate+ndeath);j++){
6653: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
6654: fprintf(ficresprobcov," p%1d-%1d ",i,j);
6655: fprintf(ficresprobcor," p%1d-%1d ",i,j);
6656: }
6657: /* fprintf(ficresprob,"\n");
6658: fprintf(ficresprobcov,"\n");
6659: fprintf(ficresprobcor,"\n");
6660: */
6661: xp=vector(1,npar);
6662: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6663: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6664: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
6665: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
6666: first=1;
6667: fprintf(ficgp,"\n# Routine varprob");
6668: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
6669: fprintf(fichtm,"\n");
6670:
1.288 brouard 6671: 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 6672: 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);
6673: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 6674: and drawn. It helps understanding how is the covariance between two incidences.\
6675: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 6676: 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 6677: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
6678: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
6679: standard deviations wide on each axis. <br>\
6680: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
6681: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
6682: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
6683:
1.222 brouard 6684: cov[1]=1;
6685: /* tj=cptcoveff; */
1.225 brouard 6686: tj = (int) pow(2,cptcoveff);
1.222 brouard 6687: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
6688: j1=0;
1.224 brouard 6689: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 6690: if (cptcovn>0) {
6691: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 6692: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6693: fprintf(ficresprob, "**********\n#\n");
6694: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 6695: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6696: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 6697:
1.222 brouard 6698: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 6699: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6700: fprintf(ficgp, "**********\n#\n");
1.220 brouard 6701:
6702:
1.222 brouard 6703: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 6704: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6705: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6706:
1.222 brouard 6707: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 6708: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6709: fprintf(ficresprobcor, "**********\n#");
6710: if(invalidvarcomb[j1]){
6711: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
6712: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
6713: continue;
6714: }
6715: }
6716: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
6717: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6718: gp=vector(1,(nlstate)*(nlstate+ndeath));
6719: gm=vector(1,(nlstate)*(nlstate+ndeath));
6720: for (age=bage; age<=fage; age ++){
6721: cov[2]=age;
6722: if(nagesqr==1)
6723: cov[3]= age*age;
6724: for (k=1; k<=cptcovn;k++) {
6725: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
6726: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
6727: * 1 1 1 1 1
6728: * 2 2 1 1 1
6729: * 3 1 2 1 1
6730: */
6731: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
6732: }
6733: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
6734: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
6735: for (k=1; k<=cptcovprod;k++)
6736: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 6737:
6738:
1.222 brouard 6739: for(theta=1; theta <=npar; theta++){
6740: for(i=1; i<=npar; i++)
6741: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 6742:
1.222 brouard 6743: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 6744:
1.222 brouard 6745: k=0;
6746: for(i=1; i<= (nlstate); i++){
6747: for(j=1; j<=(nlstate+ndeath);j++){
6748: k=k+1;
6749: gp[k]=pmmij[i][j];
6750: }
6751: }
1.220 brouard 6752:
1.222 brouard 6753: for(i=1; i<=npar; i++)
6754: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 6755:
1.222 brouard 6756: pmij(pmmij,cov,ncovmodel,xp,nlstate);
6757: k=0;
6758: for(i=1; i<=(nlstate); i++){
6759: for(j=1; j<=(nlstate+ndeath);j++){
6760: k=k+1;
6761: gm[k]=pmmij[i][j];
6762: }
6763: }
1.220 brouard 6764:
1.222 brouard 6765: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
6766: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
6767: }
1.126 brouard 6768:
1.222 brouard 6769: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
6770: for(theta=1; theta <=npar; theta++)
6771: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 6772:
1.222 brouard 6773: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
6774: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 6775:
1.222 brouard 6776: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 6777:
1.222 brouard 6778: k=0;
6779: for(i=1; i<=(nlstate); i++){
6780: for(j=1; j<=(nlstate+ndeath);j++){
6781: k=k+1;
6782: mu[k][(int) age]=pmmij[i][j];
6783: }
6784: }
6785: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
6786: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
6787: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 6788:
1.222 brouard 6789: /*printf("\n%d ",(int)age);
6790: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
6791: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
6792: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
6793: }*/
1.220 brouard 6794:
1.222 brouard 6795: fprintf(ficresprob,"\n%d ",(int)age);
6796: fprintf(ficresprobcov,"\n%d ",(int)age);
6797: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 6798:
1.222 brouard 6799: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
6800: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
6801: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
6802: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
6803: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
6804: }
6805: i=0;
6806: for (k=1; k<=(nlstate);k++){
6807: for (l=1; l<=(nlstate+ndeath);l++){
6808: i++;
6809: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
6810: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
6811: for (j=1; j<=i;j++){
6812: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
6813: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
6814: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
6815: }
6816: }
6817: }/* end of loop for state */
6818: } /* end of loop for age */
6819: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
6820: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
6821: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6822: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6823:
6824: /* Confidence intervalle of pij */
6825: /*
6826: fprintf(ficgp,"\nunset parametric;unset label");
6827: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
6828: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
6829: 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);
6830: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
6831: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
6832: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
6833: */
6834:
6835: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
6836: first1=1;first2=2;
6837: for (k2=1; k2<=(nlstate);k2++){
6838: for (l2=1; l2<=(nlstate+ndeath);l2++){
6839: if(l2==k2) continue;
6840: j=(k2-1)*(nlstate+ndeath)+l2;
6841: for (k1=1; k1<=(nlstate);k1++){
6842: for (l1=1; l1<=(nlstate+ndeath);l1++){
6843: if(l1==k1) continue;
6844: i=(k1-1)*(nlstate+ndeath)+l1;
6845: if(i<=j) continue;
6846: for (age=bage; age<=fage; age ++){
6847: if ((int)age %5==0){
6848: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
6849: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
6850: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
6851: mu1=mu[i][(int) age]/stepm*YEARM ;
6852: mu2=mu[j][(int) age]/stepm*YEARM;
6853: c12=cv12/sqrt(v1*v2);
6854: /* Computing eigen value of matrix of covariance */
6855: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6856: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6857: if ((lc2 <0) || (lc1 <0) ){
6858: if(first2==1){
6859: first1=0;
6860: 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);
6861: }
6862: 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);
6863: /* lc1=fabs(lc1); */ /* If we want to have them positive */
6864: /* lc2=fabs(lc2); */
6865: }
1.220 brouard 6866:
1.222 brouard 6867: /* Eigen vectors */
1.280 brouard 6868: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
6869: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
6870: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
6871: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
6872: }else
6873: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
1.222 brouard 6874: /*v21=sqrt(1.-v11*v11); *//* error */
6875: v21=(lc1-v1)/cv12*v11;
6876: v12=-v21;
6877: v22=v11;
6878: tnalp=v21/v11;
6879: if(first1==1){
6880: first1=0;
6881: 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);
6882: }
6883: 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);
6884: /*printf(fignu*/
6885: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
6886: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
6887: if(first==1){
6888: first=0;
6889: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
6890: fprintf(ficgp,"\nset parametric;unset label");
6891: 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);
6892: fprintf(ficgp,"\nset ter svg size 640, 480");
1.266 brouard 6893: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 6894: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 6895: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 6896: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
6897: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6898: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6899: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
6900: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6901: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6902: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6903: 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 6904: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
6905: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
1.222 brouard 6906: }else{
6907: first=0;
6908: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
6909: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6910: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6911: 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 6912: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
6913: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
1.222 brouard 6914: }/* if first */
6915: } /* age mod 5 */
6916: } /* end loop age */
6917: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6918: first=1;
6919: } /*l12 */
6920: } /* k12 */
6921: } /*l1 */
6922: }/* k1 */
6923: } /* loop on combination of covariates j1 */
6924: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
6925: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
6926: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6927: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
6928: free_vector(xp,1,npar);
6929: fclose(ficresprob);
6930: fclose(ficresprobcov);
6931: fclose(ficresprobcor);
6932: fflush(ficgp);
6933: fflush(fichtmcov);
6934: }
1.126 brouard 6935:
6936:
6937: /******************* Printing html file ***********/
1.201 brouard 6938: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 6939: int lastpass, int stepm, int weightopt, char model[],\
6940: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.296 brouard 6941: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
6942: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
6943: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
1.237 brouard 6944: int jj1, k1, i1, cpt, k4, nres;
1.126 brouard 6945:
6946: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
6947: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
6948: </ul>");
1.237 brouard 6949: fprintf(fichtm,"<ul><li> model=1+age+%s\n \
6950: </ul>", model);
1.214 brouard 6951: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
6952: 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",
6953: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
6954: 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 6955: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
6956: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 6957: fprintf(fichtm,"\
6958: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 6959: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 6960: fprintf(fichtm,"\
1.217 brouard 6961: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
6962: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
6963: fprintf(fichtm,"\
1.288 brouard 6964: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6965: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 6966: fprintf(fichtm,"\
1.288 brouard 6967: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.217 brouard 6968: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
6969: fprintf(fichtm,"\
1.211 brouard 6970: - (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 6971: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6972: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 6973: if(prevfcast==1){
6974: fprintf(fichtm,"\
6975: - Prevalence projections by age and states: \
1.201 brouard 6976: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 6977: }
1.126 brouard 6978:
6979:
1.225 brouard 6980: m=pow(2,cptcoveff);
1.222 brouard 6981: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6982:
1.264 brouard 6983: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
6984:
6985: jj1=0;
6986:
6987: fprintf(fichtm," \n<ul>");
6988: for(nres=1; nres <= nresult; nres++) /* For each resultline */
6989: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
6990: if(m != 1 && TKresult[nres]!= k1)
6991: continue;
6992: jj1++;
6993: if (cptcovn > 0) {
6994: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
6995: for (cpt=1; cpt<=cptcoveff;cpt++){
6996: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6997: }
6998: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6999: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
7000: }
7001: fprintf(fichtm,"\">");
7002:
7003: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
7004: fprintf(fichtm,"************ Results for covariates");
7005: for (cpt=1; cpt<=cptcoveff;cpt++){
7006: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7007: }
7008: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7009: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7010: }
7011: if(invalidvarcomb[k1]){
7012: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
7013: continue;
7014: }
7015: fprintf(fichtm,"</a></li>");
7016: } /* cptcovn >0 */
7017: }
7018: fprintf(fichtm," \n</ul>");
7019:
1.222 brouard 7020: jj1=0;
1.237 brouard 7021:
7022: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.241 brouard 7023: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
1.253 brouard 7024: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7025: continue;
1.220 brouard 7026:
1.222 brouard 7027: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7028: jj1++;
7029: if (cptcovn > 0) {
1.264 brouard 7030: fprintf(fichtm,"\n<p><a name=\"rescov");
7031: for (cpt=1; cpt<=cptcoveff;cpt++){
7032: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7033: }
7034: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7035: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
7036: }
7037: fprintf(fichtm,"\"</a>");
7038:
1.222 brouard 7039: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 7040: for (cpt=1; cpt<=cptcoveff;cpt++){
1.237 brouard 7041: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7042: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
7043: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7044: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 7045: }
1.237 brouard 7046: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7047: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7048: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout);
7049: }
7050:
1.230 brouard 7051: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 7052: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
7053: if(invalidvarcomb[k1]){
7054: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
7055: printf("\nCombination (%d) ignored because no cases \n",k1);
7056: continue;
7057: }
7058: }
7059: /* aij, bij */
1.259 brouard 7060: 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 7061: <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 7062: /* Pij */
1.241 brouard 7063: 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> \
7064: <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 7065: /* Quasi-incidences */
7066: 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 7067: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 7068: 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 7069: 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> \
7070: <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 7071: /* Survival functions (period) in state j */
7072: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7073: 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 7074: <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 7075: }
7076: /* State specific survival functions (period) */
7077: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7078: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
7079: And probability to be observed in various states (up to %d) being in state %d at different ages. \
1.283 brouard 7080: <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 7081: }
1.288 brouard 7082: /* Period (forward stable) prevalence in each health state */
1.222 brouard 7083: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7084: 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> \
7085: <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 7086: }
1.296 brouard 7087: if(prevbcast==1){
1.288 brouard 7088: /* Backward prevalence in each health state */
1.222 brouard 7089: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7090: 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 7091: <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 7092: }
1.217 brouard 7093: }
1.222 brouard 7094: if(prevfcast==1){
1.288 brouard 7095: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
1.222 brouard 7096: for(cpt=1; cpt<=nlstate;cpt++){
1.314 brouard 7097: 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);
7098: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_"));
7099: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",
7100: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.222 brouard 7101: }
7102: }
1.296 brouard 7103: if(prevbcast==1){
1.268 brouard 7104: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
7105: for(cpt=1; cpt<=nlstate;cpt++){
1.273 brouard 7106: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
7107: 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 \
7108: 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 7109: 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);
7110: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_"));
7111: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
1.268 brouard 7112: }
7113: }
1.220 brouard 7114:
1.222 brouard 7115: for(cpt=1; cpt<=nlstate;cpt++) {
1.314 brouard 7116: 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);
7117: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_"));
7118: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres );
1.222 brouard 7119: }
7120: /* } /\* end i1 *\/ */
7121: }/* End k1 */
7122: fprintf(fichtm,"</ul>");
1.126 brouard 7123:
1.222 brouard 7124: fprintf(fichtm,"\
1.126 brouard 7125: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 7126: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 7127: - 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 7128: But because parameters are usually highly correlated (a higher incidence of disability \
7129: and a higher incidence of recovery can give very close observed transition) it might \
7130: be very useful to look not only at linear confidence intervals estimated from the \
7131: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
7132: (parameters) of the logistic regression, it might be more meaningful to visualize the \
7133: covariance matrix of the one-step probabilities. \
7134: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 7135:
1.222 brouard 7136: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
7137: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
7138: fprintf(fichtm,"\
1.126 brouard 7139: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7140: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 7141:
1.222 brouard 7142: fprintf(fichtm,"\
1.126 brouard 7143: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7144: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
7145: fprintf(fichtm,"\
1.126 brouard 7146: - 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): \
7147: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7148: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 7149: fprintf(fichtm,"\
1.126 brouard 7150: - (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): \
7151: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7152: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 7153: fprintf(fichtm,"\
1.288 brouard 7154: - 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 7155: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
7156: fprintf(fichtm,"\
1.128 brouard 7157: - 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 7158: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
7159: fprintf(fichtm,"\
1.288 brouard 7160: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 7161: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 7162:
7163: /* if(popforecast==1) fprintf(fichtm,"\n */
7164: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
7165: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
7166: /* <br>",fileres,fileres,fileres,fileres); */
7167: /* else */
7168: /* 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 7169: fflush(fichtm);
7170: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 7171:
1.225 brouard 7172: m=pow(2,cptcoveff);
1.222 brouard 7173: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 7174:
1.222 brouard 7175: jj1=0;
1.237 brouard 7176:
1.241 brouard 7177: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.222 brouard 7178: for(k1=1; k1<=m;k1++){
1.253 brouard 7179: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7180: continue;
1.222 brouard 7181: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7182: jj1++;
1.126 brouard 7183: if (cptcovn > 0) {
7184: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 7185: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.237 brouard 7186: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);
7187: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7188: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7189: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7190: }
7191:
1.126 brouard 7192: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 7193:
1.222 brouard 7194: if(invalidvarcomb[k1]){
7195: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
7196: continue;
7197: }
1.126 brouard 7198: }
7199: for(cpt=1; cpt<=nlstate;cpt++) {
1.258 brouard 7200: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
1.314 brouard 7201: 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);
7202: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
7203: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres);
1.126 brouard 7204: }
7205: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.314 brouard 7206: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \
1.128 brouard 7207: true period expectancies (those weighted with period prevalences are also\
7208: drawn in addition to the population based expectancies computed using\
1.314 brouard 7209: 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);
7210: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_"));
7211: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 7212: /* } /\* end i1 *\/ */
7213: }/* End k1 */
1.241 brouard 7214: }/* End nres */
1.222 brouard 7215: fprintf(fichtm,"</ul>");
7216: fflush(fichtm);
1.126 brouard 7217: }
7218:
7219: /******************* Gnuplot file **************/
1.296 brouard 7220: 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 7221:
7222: char dirfileres[132],optfileres[132];
1.264 brouard 7223: char gplotcondition[132], gplotlabel[132];
1.237 brouard 7224: 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 7225: int lv=0, vlv=0, kl=0;
1.130 brouard 7226: int ng=0;
1.201 brouard 7227: int vpopbased;
1.223 brouard 7228: int ioffset; /* variable offset for columns */
1.270 brouard 7229: int iyearc=1; /* variable column for year of projection */
7230: int iagec=1; /* variable column for age of projection */
1.235 brouard 7231: int nres=0; /* Index of resultline */
1.266 brouard 7232: int istart=1; /* For starting graphs in projections */
1.219 brouard 7233:
1.126 brouard 7234: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
7235: /* printf("Problem with file %s",optionfilegnuplot); */
7236: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
7237: /* } */
7238:
7239: /*#ifdef windows */
7240: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 7241: /*#endif */
1.225 brouard 7242: m=pow(2,cptcoveff);
1.126 brouard 7243:
1.274 brouard 7244: /* diagram of the model */
7245: fprintf(ficgp,"\n#Diagram of the model \n");
7246: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
7247: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
7248: 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);
7249:
7250: 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);
7251: fprintf(ficgp,"\n#show arrow\nunset label\n");
7252: 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);
7253: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
7254: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
7255: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
7256: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
7257:
1.202 brouard 7258: /* Contribution to likelihood */
7259: /* Plot the probability implied in the likelihood */
1.223 brouard 7260: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
7261: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
7262: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
7263: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 7264: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 7265: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
7266: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 7267: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
7268: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
7269: 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));
7270: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
7271: 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));
7272: for (i=1; i<= nlstate ; i ++) {
7273: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
7274: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
7275: 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);
7276: for (j=2; j<= nlstate+ndeath ; j ++) {
7277: 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);
7278: }
7279: fprintf(ficgp,";\nset out; unset ylabel;\n");
7280: }
7281: /* 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 */
7282: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
7283: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
7284: fprintf(ficgp,"\nset out;unset log\n");
7285: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 7286:
1.126 brouard 7287: strcpy(dirfileres,optionfilefiname);
7288: strcpy(optfileres,"vpl");
1.223 brouard 7289: /* 1eme*/
1.238 brouard 7290: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
7291: for (k1=1; k1<= m ; k1 ++){ /* For each valid combination of covariate */
1.236 brouard 7292: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.238 brouard 7293: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
1.253 brouard 7294: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7295: continue;
7296: /* We are interested in selected combination by the resultline */
1.246 brouard 7297: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
1.288 brouard 7298: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.264 brouard 7299: strcpy(gplotlabel,"(");
1.238 brouard 7300: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
7301: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
7302: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7303: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7304: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7305: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
7306: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
1.246 brouard 7307: /* printf(" V%d=%d ",Tvaraff[k],vlv); */
1.238 brouard 7308: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7309: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7310: }
7311: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.246 brouard 7312: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.238 brouard 7313: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7314: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7315: }
7316: strcpy(gplotlabel+strlen(gplotlabel),")");
1.246 brouard 7317: /* printf("\n#\n"); */
1.238 brouard 7318: fprintf(ficgp,"\n#\n");
7319: if(invalidvarcomb[k1]){
1.260 brouard 7320: /*k1=k1-1;*/ /* To be checked */
1.238 brouard 7321: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7322: continue;
7323: }
1.235 brouard 7324:
1.241 brouard 7325: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
7326: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
1.276 brouard 7327: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
7328: fprintf(ficgp,"set title \"Alive state %d %s\" font \"Helvetica,12\"\n",cpt,gplotlabel);
1.260 brouard 7329: 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);
7330: /* 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); */
7331: /* k1-1 error should be nres-1*/
1.238 brouard 7332: for (i=1; i<= nlstate ; i ++) {
7333: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7334: else fprintf(ficgp," %%*lf (%%*lf)");
7335: }
1.288 brouard 7336: 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 7337: for (i=1; i<= nlstate ; i ++) {
7338: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7339: else fprintf(ficgp," %%*lf (%%*lf)");
7340: }
1.260 brouard 7341: 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 7342: for (i=1; i<= nlstate ; i ++) {
7343: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7344: else fprintf(ficgp," %%*lf (%%*lf)");
7345: }
1.265 brouard 7346: /* 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)); */
7347:
7348: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
7349: if(cptcoveff ==0){
1.271 brouard 7350: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
1.265 brouard 7351: }else{
7352: kl=0;
7353: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7354: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7355: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7356: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7357: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7358: vlv= nbcode[Tvaraff[k]][lv];
7359: kl++;
7360: /* 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 *\/ */
7361: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7362: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7363: /* '' 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*/
7364: if(k==cptcoveff){
7365: 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], \
7366: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
7367: }else{
7368: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7369: kl++;
7370: }
7371: } /* end covariate */
7372: } /* end if no covariate */
7373:
1.296 brouard 7374: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
1.238 brouard 7375: /* 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 7376: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 7377: if(cptcoveff ==0){
1.245 brouard 7378: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
1.238 brouard 7379: }else{
7380: kl=0;
7381: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7382: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7383: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7384: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7385: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7386: vlv= nbcode[Tvaraff[k]][lv];
1.223 brouard 7387: kl++;
1.238 brouard 7388: /* 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 *\/ */
7389: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7390: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7391: /* '' 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*/
7392: if(k==cptcoveff){
1.245 brouard 7393: 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 7394: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 7395: }else{
7396: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7397: kl++;
7398: }
7399: } /* end covariate */
7400: } /* end if no covariate */
1.296 brouard 7401: if(prevbcast == 1){
1.268 brouard 7402: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
7403: /* k1-1 error should be nres-1*/
7404: for (i=1; i<= nlstate ; i ++) {
7405: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7406: else fprintf(ficgp," %%*lf (%%*lf)");
7407: }
1.271 brouard 7408: 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 7409: for (i=1; i<= nlstate ; i ++) {
7410: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7411: else fprintf(ficgp," %%*lf (%%*lf)");
7412: }
1.276 brouard 7413: 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 7414: for (i=1; i<= nlstate ; i ++) {
7415: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7416: else fprintf(ficgp," %%*lf (%%*lf)");
7417: }
1.274 brouard 7418: fprintf(ficgp,"\" t\"\" w l lt 4");
1.268 brouard 7419: } /* end if backprojcast */
1.296 brouard 7420: } /* end if prevbcast */
1.276 brouard 7421: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
7422: fprintf(ficgp,"\nset out ;unset title;\n");
1.238 brouard 7423: } /* nres */
1.201 brouard 7424: } /* k1 */
7425: } /* cpt */
1.235 brouard 7426:
7427:
1.126 brouard 7428: /*2 eme*/
1.238 brouard 7429: for (k1=1; k1<= m ; k1 ++){
7430: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7431: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7432: continue;
7433: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.264 brouard 7434: strcpy(gplotlabel,"(");
1.238 brouard 7435: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.225 brouard 7436: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 7437: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7438: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7439: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7440: vlv= nbcode[Tvaraff[k]][lv];
7441: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7442: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7443: }
1.237 brouard 7444: /* for(k=1; k <= ncovds; k++){ */
1.236 brouard 7445: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 7446: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.236 brouard 7447: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7448: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7449: }
1.264 brouard 7450: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7451: fprintf(ficgp,"\n#\n");
1.223 brouard 7452: if(invalidvarcomb[k1]){
7453: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7454: continue;
7455: }
1.219 brouard 7456:
1.241 brouard 7457: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 7458: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.264 brouard 7459: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
7460: if(vpopbased==0){
1.238 brouard 7461: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
1.264 brouard 7462: }else
1.238 brouard 7463: fprintf(ficgp,"\nreplot ");
7464: for (i=1; i<= nlstate+1 ; i ++) {
7465: k=2*i;
1.261 brouard 7466: 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 7467: for (j=1; j<= nlstate+1 ; j ++) {
7468: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7469: else fprintf(ficgp," %%*lf (%%*lf)");
7470: }
7471: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
7472: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
1.261 brouard 7473: 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 7474: for (j=1; j<= nlstate+1 ; j ++) {
7475: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7476: else fprintf(ficgp," %%*lf (%%*lf)");
7477: }
7478: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.261 brouard 7479: 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 7480: for (j=1; j<= nlstate+1 ; j ++) {
7481: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7482: else fprintf(ficgp," %%*lf (%%*lf)");
7483: }
7484: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
7485: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
7486: } /* state */
7487: } /* vpopbased */
1.264 brouard 7488: 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 7489: } /* end nres */
7490: } /* k1 end 2 eme*/
7491:
7492:
7493: /*3eme*/
7494: for (k1=1; k1<= m ; k1 ++){
7495: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7496: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7497: continue;
7498:
7499: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.261 brouard 7500: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.264 brouard 7501: strcpy(gplotlabel,"(");
1.238 brouard 7502: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7503: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7504: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7505: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7506: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7507: vlv= nbcode[Tvaraff[k]][lv];
7508: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7509: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7510: }
7511: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7512: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7513: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7514: }
1.264 brouard 7515: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7516: fprintf(ficgp,"\n#\n");
7517: if(invalidvarcomb[k1]){
7518: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7519: continue;
7520: }
7521:
7522: /* k=2+nlstate*(2*cpt-2); */
7523: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 7524: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.264 brouard 7525: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
1.238 brouard 7526: fprintf(ficgp,"set ter svg size 640, 480\n\
1.261 brouard 7527: 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 7528: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7529: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7530: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
7531: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7532: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7533: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 7534:
1.238 brouard 7535: */
7536: for (i=1; i< nlstate ; i ++) {
1.261 brouard 7537: 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 7538: /* 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 7539:
1.238 brouard 7540: }
1.261 brouard 7541: 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 7542: }
1.264 brouard 7543: fprintf(ficgp,"\nunset label;\n");
1.238 brouard 7544: } /* end nres */
7545: } /* end kl 3eme */
1.126 brouard 7546:
1.223 brouard 7547: /* 4eme */
1.201 brouard 7548: /* Survival functions (period) from state i in state j by initial state i */
1.238 brouard 7549: for (k1=1; k1<=m; k1++){ /* For each covariate and each value */
7550: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7551: if(m != 1 && TKresult[nres]!= k1)
1.223 brouard 7552: continue;
1.238 brouard 7553: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
1.264 brouard 7554: strcpy(gplotlabel,"(");
1.238 brouard 7555: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
7556: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7557: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7558: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7559: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7560: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7561: vlv= nbcode[Tvaraff[k]][lv];
7562: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7563: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7564: }
7565: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7566: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7567: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7568: }
1.264 brouard 7569: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7570: fprintf(ficgp,"\n#\n");
7571: if(invalidvarcomb[k1]){
7572: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7573: continue;
1.223 brouard 7574: }
1.238 brouard 7575:
1.241 brouard 7576: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.264 brouard 7577: 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 7578: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7579: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7580: k=3;
7581: for (i=1; i<= nlstate ; i ++){
7582: if(i==1){
7583: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7584: }else{
7585: fprintf(ficgp,", '' ");
7586: }
7587: l=(nlstate+ndeath)*(i-1)+1;
7588: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
7589: for (j=2; j<= nlstate+ndeath ; j ++)
7590: fprintf(ficgp,"+$%d",k+l+j-1);
7591: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
7592: } /* nlstate */
1.264 brouard 7593: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7594: } /* end cpt state*/
7595: } /* end nres */
7596: } /* end covariate k1 */
7597:
1.220 brouard 7598: /* 5eme */
1.201 brouard 7599: /* Survival functions (period) from state i in state j by final state j */
1.238 brouard 7600: for (k1=1; k1<= m ; k1++){ /* For each covariate combination if any */
7601: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7602: if(m != 1 && TKresult[nres]!= k1)
1.227 brouard 7603: continue;
1.238 brouard 7604: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.264 brouard 7605: strcpy(gplotlabel,"(");
1.238 brouard 7606: 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);
7607: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7608: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7609: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7610: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7611: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7612: vlv= nbcode[Tvaraff[k]][lv];
7613: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7614: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7615: }
7616: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7617: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7618: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7619: }
1.264 brouard 7620: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7621: fprintf(ficgp,"\n#\n");
7622: if(invalidvarcomb[k1]){
7623: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7624: continue;
7625: }
1.227 brouard 7626:
1.241 brouard 7627: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.264 brouard 7628: 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 7629: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7630: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7631: k=3;
7632: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7633: if(j==1)
7634: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7635: else
7636: fprintf(ficgp,", '' ");
7637: l=(nlstate+ndeath)*(cpt-1) +j;
7638: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
7639: /* for (i=2; i<= nlstate+ndeath ; i ++) */
7640: /* fprintf(ficgp,"+$%d",k+l+i-1); */
7641: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
7642: } /* nlstate */
7643: fprintf(ficgp,", '' ");
7644: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
7645: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7646: l=(nlstate+ndeath)*(cpt-1) +j;
7647: if(j < nlstate)
7648: fprintf(ficgp,"$%d +",k+l);
7649: else
7650: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
7651: }
1.264 brouard 7652: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7653: } /* end cpt state*/
7654: } /* end covariate */
7655: } /* end nres */
1.227 brouard 7656:
1.220 brouard 7657: /* 6eme */
1.202 brouard 7658: /* CV preval stable (period) for each covariate */
1.237 brouard 7659: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7660: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7661: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7662: continue;
1.255 brouard 7663: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
1.264 brouard 7664: strcpy(gplotlabel,"(");
1.288 brouard 7665: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 7666: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 7667: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7668: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7669: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7670: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7671: vlv= nbcode[Tvaraff[k]][lv];
7672: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7673: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7674: }
1.237 brouard 7675: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7676: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7677: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7678: }
1.264 brouard 7679: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7680: fprintf(ficgp,"\n#\n");
1.223 brouard 7681: if(invalidvarcomb[k1]){
1.227 brouard 7682: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7683: continue;
1.223 brouard 7684: }
1.227 brouard 7685:
1.241 brouard 7686: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.264 brouard 7687: 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 7688: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 7689: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 7690: k=3; /* Offset */
1.255 brouard 7691: for (i=1; i<= nlstate ; i ++){ /* State of origin */
1.227 brouard 7692: if(i==1)
7693: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7694: else
7695: fprintf(ficgp,", '' ");
1.255 brouard 7696: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 7697: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
7698: for (j=2; j<= nlstate ; j ++)
7699: fprintf(ficgp,"+$%d",k+l+j-1);
7700: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 7701: } /* nlstate */
1.264 brouard 7702: fprintf(ficgp,"\nset out; unset label;\n");
1.153 brouard 7703: } /* end cpt state*/
7704: } /* end covariate */
1.227 brouard 7705:
7706:
1.220 brouard 7707: /* 7eme */
1.296 brouard 7708: if(prevbcast == 1){
1.288 brouard 7709: /* CV backward prevalence for each covariate */
1.237 brouard 7710: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7711: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7712: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7713: continue;
1.268 brouard 7714: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
1.264 brouard 7715: strcpy(gplotlabel,"(");
1.288 brouard 7716: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 7717: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7718: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7719: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7720: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 7721: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 7722: vlv= nbcode[Tvaraff[k]][lv];
7723: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7724: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 7725: }
1.237 brouard 7726: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7727: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7728: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7729: }
1.264 brouard 7730: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 7731: fprintf(ficgp,"\n#\n");
7732: if(invalidvarcomb[k1]){
7733: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7734: continue;
7735: }
7736:
1.241 brouard 7737: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.268 brouard 7738: 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 7739: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 7740: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 7741: k=3; /* Offset */
1.268 brouard 7742: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
1.227 brouard 7743: if(i==1)
7744: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
7745: else
7746: fprintf(ficgp,", '' ");
7747: /* l=(nlstate+ndeath)*(i-1)+1; */
1.255 brouard 7748: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 7749: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
7750: /* 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 7751: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
1.227 brouard 7752: /* for (j=2; j<= nlstate ; j ++) */
7753: /* fprintf(ficgp,"+$%d",k+l+j-1); */
7754: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
1.268 brouard 7755: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
1.227 brouard 7756: } /* nlstate */
1.264 brouard 7757: fprintf(ficgp,"\nset out; unset label;\n");
1.218 brouard 7758: } /* end cpt state*/
7759: } /* end covariate */
1.296 brouard 7760: } /* End if prevbcast */
1.218 brouard 7761:
1.223 brouard 7762: /* 8eme */
1.218 brouard 7763: if(prevfcast==1){
1.288 brouard 7764: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
1.218 brouard 7765:
1.237 brouard 7766: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7767: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7768: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7769: continue;
1.211 brouard 7770: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.264 brouard 7771: strcpy(gplotlabel,"(");
1.288 brouard 7772: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 7773: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
7774: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7775: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7776: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7777: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7778: vlv= nbcode[Tvaraff[k]][lv];
7779: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7780: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 7781: }
1.237 brouard 7782: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7783: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7784: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7785: }
1.264 brouard 7786: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 7787: fprintf(ficgp,"\n#\n");
7788: if(invalidvarcomb[k1]){
7789: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7790: continue;
7791: }
7792:
7793: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 7794: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.264 brouard 7795: 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 7796: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 7797: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.266 brouard 7798:
7799: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
7800: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
7801: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
7802: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
1.227 brouard 7803: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7804: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7805: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7806: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.266 brouard 7807: if(i==istart){
1.227 brouard 7808: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
7809: }else{
7810: fprintf(ficgp,",\\\n '' ");
7811: }
7812: if(cptcoveff ==0){ /* No covariate */
7813: ioffset=2; /* Age is in 2 */
7814: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7815: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7816: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7817: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7818: fprintf(ficgp," u %d:(", ioffset);
1.266 brouard 7819: if(i==nlstate+1){
1.270 brouard 7820: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
1.266 brouard 7821: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
7822: fprintf(ficgp,",\\\n '' ");
7823: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 7824: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
1.266 brouard 7825: offyear, \
1.268 brouard 7826: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
1.266 brouard 7827: }else
1.227 brouard 7828: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
7829: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
7830: }else{ /* more than 2 covariates */
1.270 brouard 7831: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
7832: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7833: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7834: iyearc=ioffset-1;
7835: iagec=ioffset;
1.227 brouard 7836: fprintf(ficgp," u %d:(",ioffset);
7837: kl=0;
7838: strcpy(gplotcondition,"(");
7839: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
7840: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
7841: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7842: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7843: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7844: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
7845: kl++;
7846: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
7847: kl++;
7848: if(k <cptcoveff && cptcoveff>1)
7849: sprintf(gplotcondition+strlen(gplotcondition)," && ");
7850: }
7851: strcpy(gplotcondition+strlen(gplotcondition),")");
7852: /* 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 *\/ */
7853: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7854: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7855: /* '' 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*/
7856: if(i==nlstate+1){
1.270 brouard 7857: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
7858: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
1.266 brouard 7859: fprintf(ficgp,",\\\n '' ");
1.270 brouard 7860: fprintf(ficgp," u %d:(",iagec);
7861: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
7862: iyearc, iagec, offyear, \
7863: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
1.266 brouard 7864: /* '' 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 7865: }else{
7866: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
7867: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
7868: }
7869: } /* end if covariate */
7870: } /* nlstate */
1.264 brouard 7871: fprintf(ficgp,"\nset out; unset label;\n");
1.223 brouard 7872: } /* end cpt state*/
7873: } /* end covariate */
7874: } /* End if prevfcast */
1.227 brouard 7875:
1.296 brouard 7876: if(prevbcast==1){
1.268 brouard 7877: /* Back projection from cross-sectional to stable (mixed) for each covariate */
7878:
7879: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7880: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7881: if(m != 1 && TKresult[nres]!= k1)
7882: continue;
7883: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
7884: strcpy(gplotlabel,"(");
7885: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
7886: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
7887: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7888: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7889: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7890: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7891: vlv= nbcode[Tvaraff[k]][lv];
7892: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
7893: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
7894: }
7895: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7896: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7897: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7898: }
7899: strcpy(gplotlabel+strlen(gplotlabel),")");
7900: fprintf(ficgp,"\n#\n");
7901: if(invalidvarcomb[k1]){
7902: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7903: continue;
7904: }
7905:
7906: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
7907: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
7908: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
7909: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
7910: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7911:
7912: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
7913: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
7914: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
7915: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
7916: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7917: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7918: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7919: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7920: if(i==istart){
7921: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
7922: }else{
7923: fprintf(ficgp,",\\\n '' ");
7924: }
7925: if(cptcoveff ==0){ /* No covariate */
7926: ioffset=2; /* Age is in 2 */
7927: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7928: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7929: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7930: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7931: fprintf(ficgp," u %d:(", ioffset);
7932: if(i==nlstate+1){
1.270 brouard 7933: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
1.268 brouard 7934: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
7935: fprintf(ficgp,",\\\n '' ");
7936: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 7937: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
1.268 brouard 7938: offbyear, \
7939: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
7940: }else
7941: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
7942: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
7943: }else{ /* more than 2 covariates */
1.270 brouard 7944: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
7945: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7946: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7947: iyearc=ioffset-1;
7948: iagec=ioffset;
1.268 brouard 7949: fprintf(ficgp," u %d:(",ioffset);
7950: kl=0;
7951: strcpy(gplotcondition,"(");
7952: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
7953: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
7954: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7955: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7956: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7957: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
7958: kl++;
7959: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
7960: kl++;
7961: if(k <cptcoveff && cptcoveff>1)
7962: sprintf(gplotcondition+strlen(gplotcondition)," && ");
7963: }
7964: strcpy(gplotcondition+strlen(gplotcondition),")");
7965: /* 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 *\/ */
7966: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7967: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7968: /* '' 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*/
7969: if(i==nlstate+1){
1.270 brouard 7970: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
7971: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
1.268 brouard 7972: fprintf(ficgp,",\\\n '' ");
1.270 brouard 7973: fprintf(ficgp," u %d:(",iagec);
1.268 brouard 7974: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
1.270 brouard 7975: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
7976: iyearc,iagec,offbyear, \
7977: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
1.268 brouard 7978: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
7979: }else{
7980: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
7981: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
7982: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
7983: }
7984: } /* end if covariate */
7985: } /* nlstate */
7986: fprintf(ficgp,"\nset out; unset label;\n");
7987: } /* end cpt state*/
7988: } /* end covariate */
1.296 brouard 7989: } /* End if prevbcast */
1.268 brouard 7990:
1.227 brouard 7991:
1.238 brouard 7992: /* 9eme writing MLE parameters */
7993: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 7994: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 7995: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 7996: for(k=1; k <=(nlstate+ndeath); k++){
7997: if (k != i) {
1.227 brouard 7998: fprintf(ficgp,"# current state %d\n",k);
7999: for(j=1; j <=ncovmodel; j++){
8000: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
8001: jk++;
8002: }
8003: fprintf(ficgp,"\n");
1.126 brouard 8004: }
8005: }
1.223 brouard 8006: }
1.187 brouard 8007: fprintf(ficgp,"##############\n#\n");
1.227 brouard 8008:
1.145 brouard 8009: /*goto avoid;*/
1.238 brouard 8010: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
8011: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 8012: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
8013: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
8014: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
8015: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
8016: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8017: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
8018: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8019: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
8020: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
8021: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
8022: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
8023: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
8024: fprintf(ficgp,"#\n");
1.223 brouard 8025: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 8026: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.237 brouard 8027: fprintf(ficgp,"#model=%s \n",model);
1.238 brouard 8028: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.264 brouard 8029: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
8030: for(k1=1; k1 <=m; k1++) /* For each combination of covariate */
1.237 brouard 8031: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.264 brouard 8032: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 8033: continue;
1.264 brouard 8034: fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1);
8035: strcpy(gplotlabel,"(");
1.276 brouard 8036: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
1.264 brouard 8037: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
8038: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
8039: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8040: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8041: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8042: vlv= nbcode[Tvaraff[k]][lv];
8043: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
8044: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
8045: }
1.237 brouard 8046: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8047: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 8048: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 8049: }
1.264 brouard 8050: strcpy(gplotlabel+strlen(gplotlabel),")");
1.237 brouard 8051: fprintf(ficgp,"\n#\n");
1.264 brouard 8052: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
1.276 brouard 8053: fprintf(ficgp,"\nset key outside ");
8054: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
8055: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
1.223 brouard 8056: fprintf(ficgp,"\nset ter svg size 640, 480 ");
8057: if (ng==1){
8058: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
8059: fprintf(ficgp,"\nunset log y");
8060: }else if (ng==2){
8061: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
8062: fprintf(ficgp,"\nset log y");
8063: }else if (ng==3){
8064: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
8065: fprintf(ficgp,"\nset log y");
8066: }else
8067: fprintf(ficgp,"\nunset title ");
8068: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
8069: i=1;
8070: for(k2=1; k2<=nlstate; k2++) {
8071: k3=i;
8072: for(k=1; k<=(nlstate+ndeath); k++) {
8073: if (k != k2){
8074: switch( ng) {
8075: case 1:
8076: if(nagesqr==0)
8077: fprintf(ficgp," p%d+p%d*x",i,i+1);
8078: else /* nagesqr =1 */
8079: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8080: break;
8081: case 2: /* ng=2 */
8082: if(nagesqr==0)
8083: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
8084: else /* nagesqr =1 */
8085: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8086: break;
8087: case 3:
8088: if(nagesqr==0)
8089: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
8090: else /* nagesqr =1 */
8091: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
8092: break;
8093: }
8094: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 8095: ijp=1; /* product no age */
8096: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
8097: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 8098: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.268 brouard 8099: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8100: if(j==Tage[ij]) { /* Product by age To be looked at!!*/
8101: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8102: if(DummyV[j]==0){
8103: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
8104: }else{ /* quantitative */
8105: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
8106: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8107: }
8108: ij++;
1.237 brouard 8109: }
1.268 brouard 8110: }
8111: }else if(cptcovprod >0){
8112: if(j==Tprod[ijp]) { /* */
8113: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
8114: if(ijp <=cptcovprod) { /* Product */
8115: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
8116: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
8117: /* 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)]); */
8118: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
8119: }else{ /* Vn is dummy and Vm is quanti */
8120: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
8121: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8122: }
8123: }else{ /* Vn*Vm Vn is quanti */
8124: if(DummyV[Tvard[ijp][2]]==0){
8125: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
8126: }else{ /* Both quanti */
8127: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8128: }
1.237 brouard 8129: }
1.268 brouard 8130: ijp++;
1.237 brouard 8131: }
1.268 brouard 8132: } /* end Tprod */
1.237 brouard 8133: } else{ /* simple covariate */
1.264 brouard 8134: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
1.237 brouard 8135: if(Dummy[j]==0){
8136: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
8137: }else{ /* quantitative */
8138: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.264 brouard 8139: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
1.223 brouard 8140: }
1.237 brouard 8141: } /* end simple */
8142: } /* end j */
1.223 brouard 8143: }else{
8144: i=i-ncovmodel;
8145: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
8146: fprintf(ficgp," (1.");
8147: }
1.227 brouard 8148:
1.223 brouard 8149: if(ng != 1){
8150: fprintf(ficgp,")/(1");
1.227 brouard 8151:
1.264 brouard 8152: for(cpt=1; cpt <=nlstate; cpt++){
1.223 brouard 8153: if(nagesqr==0)
1.264 brouard 8154: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
1.223 brouard 8155: else /* nagesqr =1 */
1.264 brouard 8156: 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 8157:
1.223 brouard 8158: ij=1;
8159: for(j=3; j <=ncovmodel-nagesqr; j++){
1.268 brouard 8160: if(cptcovage >0){
8161: if((j-2)==Tage[ij]) { /* Bug valgrind */
8162: if(ij <=cptcovage) { /* Bug valgrind */
8163: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);
8164: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8165: ij++;
8166: }
8167: }
8168: }else
8169: 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 8170: }
8171: fprintf(ficgp,")");
8172: }
8173: fprintf(ficgp,")");
8174: if(ng ==2)
1.276 brouard 8175: 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 8176: else /* ng= 3 */
1.276 brouard 8177: 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 8178: }else{ /* end ng <> 1 */
8179: if( k !=k2) /* logit p11 is hard to draw */
1.276 brouard 8180: 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 8181: }
8182: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
8183: fprintf(ficgp,",");
8184: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
8185: fprintf(ficgp,",");
8186: i=i+ncovmodel;
8187: } /* end k */
8188: } /* end k2 */
1.276 brouard 8189: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
8190: fprintf(ficgp,"\n set out; unset title;set key default;\n");
1.264 brouard 8191: } /* end k1 */
1.223 brouard 8192: } /* end ng */
8193: /* avoid: */
8194: fflush(ficgp);
1.126 brouard 8195: } /* end gnuplot */
8196:
8197:
8198: /*************** Moving average **************/
1.219 brouard 8199: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 8200: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 8201:
1.222 brouard 8202: int i, cpt, cptcod;
8203: int modcovmax =1;
8204: int mobilavrange, mob;
8205: int iage=0;
1.288 brouard 8206: int firstA1=0, firstA2=0;
1.222 brouard 8207:
1.266 brouard 8208: double sum=0., sumr=0.;
1.222 brouard 8209: double age;
1.266 brouard 8210: double *sumnewp, *sumnewm, *sumnewmr;
8211: double *agemingood, *agemaxgood;
8212: double *agemingoodr, *agemaxgoodr;
1.222 brouard 8213:
8214:
1.278 brouard 8215: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
8216: /* a covariate has 2 modalities, should be equal to ncovcombmax */
1.222 brouard 8217:
8218: sumnewp = vector(1,ncovcombmax);
8219: sumnewm = vector(1,ncovcombmax);
1.266 brouard 8220: sumnewmr = vector(1,ncovcombmax);
1.222 brouard 8221: agemingood = vector(1,ncovcombmax);
1.266 brouard 8222: agemingoodr = vector(1,ncovcombmax);
1.222 brouard 8223: agemaxgood = vector(1,ncovcombmax);
1.266 brouard 8224: agemaxgoodr = vector(1,ncovcombmax);
1.222 brouard 8225:
8226: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
1.266 brouard 8227: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
1.222 brouard 8228: sumnewp[cptcod]=0.;
1.266 brouard 8229: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
8230: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
1.222 brouard 8231: }
8232: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
8233:
1.266 brouard 8234: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
8235: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
1.222 brouard 8236: else mobilavrange=mobilav;
8237: for (age=bage; age<=fage; age++)
8238: for (i=1; i<=nlstate;i++)
8239: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
8240: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8241: /* We keep the original values on the extreme ages bage, fage and for
8242: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
8243: we use a 5 terms etc. until the borders are no more concerned.
8244: */
8245: for (mob=3;mob <=mobilavrange;mob=mob+2){
8246: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
1.266 brouard 8247: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
8248: sumnewm[cptcod]=0.;
8249: for (i=1; i<=nlstate;i++){
1.222 brouard 8250: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
8251: for (cpt=1;cpt<=(mob-1)/2;cpt++){
8252: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
8253: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
8254: }
8255: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
1.266 brouard 8256: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8257: } /* end i */
8258: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
8259: } /* end cptcod */
1.222 brouard 8260: }/* end age */
8261: }/* end mob */
1.266 brouard 8262: }else{
8263: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
1.222 brouard 8264: return -1;
1.266 brouard 8265: }
8266:
8267: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
1.222 brouard 8268: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
8269: if(invalidvarcomb[cptcod]){
8270: printf("\nCombination (%d) ignored because no cases \n",cptcod);
8271: continue;
8272: }
1.219 brouard 8273:
1.266 brouard 8274: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
8275: sumnewm[cptcod]=0.;
8276: sumnewmr[cptcod]=0.;
8277: for (i=1; i<=nlstate;i++){
8278: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8279: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8280: }
8281: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8282: agemingoodr[cptcod]=age;
8283: }
8284: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8285: agemingood[cptcod]=age;
8286: }
8287: } /* age */
8288: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
1.222 brouard 8289: sumnewm[cptcod]=0.;
1.266 brouard 8290: sumnewmr[cptcod]=0.;
1.222 brouard 8291: for (i=1; i<=nlstate;i++){
8292: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8293: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8294: }
8295: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8296: agemaxgoodr[cptcod]=age;
1.222 brouard 8297: }
8298: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
1.266 brouard 8299: agemaxgood[cptcod]=age;
8300: }
8301: } /* age */
8302: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
8303: /* but they will change */
1.288 brouard 8304: firstA1=0;firstA2=0;
1.266 brouard 8305: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
8306: sumnewm[cptcod]=0.;
8307: sumnewmr[cptcod]=0.;
8308: for (i=1; i<=nlstate;i++){
8309: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8310: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8311: }
8312: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8313: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8314: agemaxgoodr[cptcod]=age; /* age min */
8315: for (i=1; i<=nlstate;i++)
8316: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8317: }else{ /* bad we change the value with the values of good ages */
8318: for (i=1; i<=nlstate;i++){
8319: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
8320: } /* i */
8321: } /* end bad */
8322: }else{
8323: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8324: agemaxgood[cptcod]=age;
8325: }else{ /* bad we change the value with the values of good ages */
8326: for (i=1; i<=nlstate;i++){
8327: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
8328: } /* i */
8329: } /* end bad */
8330: }/* end else */
8331: sum=0.;sumr=0.;
8332: for (i=1; i<=nlstate;i++){
8333: sum+=mobaverage[(int)age][i][cptcod];
8334: sumr+=probs[(int)age][i][cptcod];
8335: }
8336: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.288 brouard 8337: if(!firstA1){
8338: firstA1=1;
8339: 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);
8340: }
8341: 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 8342: } /* end bad */
8343: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8344: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.288 brouard 8345: if(!firstA2){
8346: firstA2=1;
8347: 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);
8348: }
8349: 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 8350: } /* end bad */
8351: }/* age */
1.266 brouard 8352:
8353: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
1.222 brouard 8354: sumnewm[cptcod]=0.;
1.266 brouard 8355: sumnewmr[cptcod]=0.;
1.222 brouard 8356: for (i=1; i<=nlstate;i++){
8357: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8358: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8359: }
8360: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8361: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
8362: agemingoodr[cptcod]=age;
8363: for (i=1; i<=nlstate;i++)
8364: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8365: }else{ /* bad we change the value with the values of good ages */
8366: for (i=1; i<=nlstate;i++){
8367: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
8368: } /* i */
8369: } /* end bad */
8370: }else{
8371: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8372: agemingood[cptcod]=age;
8373: }else{ /* bad */
8374: for (i=1; i<=nlstate;i++){
8375: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
8376: } /* i */
8377: } /* end bad */
8378: }/* end else */
8379: sum=0.;sumr=0.;
8380: for (i=1; i<=nlstate;i++){
8381: sum+=mobaverage[(int)age][i][cptcod];
8382: sumr+=mobaverage[(int)age][i][cptcod];
1.222 brouard 8383: }
1.266 brouard 8384: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.268 brouard 8385: 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 8386: } /* end bad */
8387: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8388: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.268 brouard 8389: 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 8390: } /* end bad */
8391: }/* age */
1.266 brouard 8392:
1.222 brouard 8393:
8394: for (age=bage; age<=fage; age++){
1.235 brouard 8395: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 8396: sumnewp[cptcod]=0.;
8397: sumnewm[cptcod]=0.;
8398: for (i=1; i<=nlstate;i++){
8399: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
8400: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8401: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
8402: }
8403: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
8404: }
8405: /* printf("\n"); */
8406: /* } */
1.266 brouard 8407:
1.222 brouard 8408: /* brutal averaging */
1.266 brouard 8409: /* for (i=1; i<=nlstate;i++){ */
8410: /* for (age=1; age<=bage; age++){ */
8411: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
8412: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8413: /* } */
8414: /* for (age=fage; age<=AGESUP; age++){ */
8415: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
8416: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8417: /* } */
8418: /* } /\* end i status *\/ */
8419: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
8420: /* for (age=1; age<=AGESUP; age++){ */
8421: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
8422: /* mobaverage[(int)age][i][cptcod]=0.; */
8423: /* } */
8424: /* } */
1.222 brouard 8425: }/* end cptcod */
1.266 brouard 8426: free_vector(agemaxgoodr,1, ncovcombmax);
8427: free_vector(agemaxgood,1, ncovcombmax);
8428: free_vector(agemingood,1, ncovcombmax);
8429: free_vector(agemingoodr,1, ncovcombmax);
8430: free_vector(sumnewmr,1, ncovcombmax);
1.222 brouard 8431: free_vector(sumnewm,1, ncovcombmax);
8432: free_vector(sumnewp,1, ncovcombmax);
8433: return 0;
8434: }/* End movingaverage */
1.218 brouard 8435:
1.126 brouard 8436:
1.296 brouard 8437:
1.126 brouard 8438: /************** Forecasting ******************/
1.296 brouard 8439: /* 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)*/
8440: 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){
8441: /* dateintemean, mean date of interviews
8442: dateprojd, year, month, day of starting projection
8443: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
1.126 brouard 8444: agemin, agemax range of age
8445: dateprev1 dateprev2 range of dates during which prevalence is computed
8446: */
1.296 brouard 8447: /* double anprojd, mprojd, jprojd; */
8448: /* double anprojf, mprojf, jprojf; */
1.267 brouard 8449: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
1.126 brouard 8450: double agec; /* generic age */
1.296 brouard 8451: double agelim, ppij, yp,yp1,yp2;
1.126 brouard 8452: double *popeffectif,*popcount;
8453: double ***p3mat;
1.218 brouard 8454: /* double ***mobaverage; */
1.126 brouard 8455: char fileresf[FILENAMELENGTH];
8456:
8457: agelim=AGESUP;
1.211 brouard 8458: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8459: in each health status at the date of interview (if between dateprev1 and dateprev2).
8460: We still use firstpass and lastpass as another selection.
8461: */
1.214 brouard 8462: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8463: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 8464:
1.201 brouard 8465: strcpy(fileresf,"F_");
8466: strcat(fileresf,fileresu);
1.126 brouard 8467: if((ficresf=fopen(fileresf,"w"))==NULL) {
8468: printf("Problem with forecast resultfile: %s\n", fileresf);
8469: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
8470: }
1.235 brouard 8471: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
8472: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 8473:
1.225 brouard 8474: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 8475:
8476:
8477: stepsize=(int) (stepm+YEARM-1)/YEARM;
8478: if (stepm<=12) stepsize=1;
8479: if(estepm < stepm){
8480: printf ("Problem %d lower than %d\n",estepm, stepm);
8481: }
1.270 brouard 8482: else{
8483: hstepm=estepm;
8484: }
8485: if(estepm > stepm){ /* Yes every two year */
8486: stepsize=2;
8487: }
1.296 brouard 8488: hstepm=hstepm/stepm;
1.126 brouard 8489:
1.296 brouard 8490:
8491: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8492: /* fractional in yp1 *\/ */
8493: /* aintmean=yp; */
8494: /* yp2=modf((yp1*12),&yp); */
8495: /* mintmean=yp; */
8496: /* yp1=modf((yp2*30.5),&yp); */
8497: /* jintmean=yp; */
8498: /* if(jintmean==0) jintmean=1; */
8499: /* if(mintmean==0) mintmean=1; */
1.126 brouard 8500:
1.296 brouard 8501:
8502: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
8503: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
8504: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
1.227 brouard 8505: i1=pow(2,cptcoveff);
1.126 brouard 8506: if (cptcovn < 1){i1=1;}
8507:
1.296 brouard 8508: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.126 brouard 8509:
8510: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 8511:
1.126 brouard 8512: /* if (h==(int)(YEARM*yearp)){ */
1.235 brouard 8513: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8514: for(k=1; k<=i1;k++){
1.253 brouard 8515: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 8516: continue;
1.227 brouard 8517: if(invalidvarcomb[k]){
8518: printf("\nCombination (%d) projection ignored because no cases \n",k);
8519: continue;
8520: }
8521: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
8522: for(j=1;j<=cptcoveff;j++) {
8523: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8524: }
1.235 brouard 8525: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 8526: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.235 brouard 8527: }
1.227 brouard 8528: fprintf(ficresf," yearproj age");
8529: for(j=1; j<=nlstate+ndeath;j++){
8530: for(i=1; i<=nlstate;i++)
8531: fprintf(ficresf," p%d%d",i,j);
8532: fprintf(ficresf," wp.%d",j);
8533: }
1.296 brouard 8534: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
1.227 brouard 8535: fprintf(ficresf,"\n");
1.296 brouard 8536: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
1.270 brouard 8537: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
8538: for (agec=fage; agec>=(bage); agec--){
1.227 brouard 8539: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
8540: nhstepm = nhstepm/hstepm;
8541: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8542: oldm=oldms;savm=savms;
1.268 brouard 8543: /* We compute pii at age agec over nhstepm);*/
1.235 brouard 8544: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.268 brouard 8545: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
1.227 brouard 8546: for (h=0; h<=nhstepm; h++){
8547: if (h*hstepm/YEARM*stepm ==yearp) {
1.268 brouard 8548: break;
8549: }
8550: }
8551: fprintf(ficresf,"\n");
8552: for(j=1;j<=cptcoveff;j++)
8553: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 8554: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
1.268 brouard 8555:
8556: for(j=1; j<=nlstate+ndeath;j++) {
8557: ppij=0.;
8558: for(i=1; i<=nlstate;i++) {
1.278 brouard 8559: if (mobilav>=1)
8560: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
8561: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
8562: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
8563: }
1.268 brouard 8564: fprintf(ficresf," %.3f", p3mat[i][j][h]);
8565: } /* end i */
8566: fprintf(ficresf," %.3f", ppij);
8567: }/* end j */
1.227 brouard 8568: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8569: } /* end agec */
1.266 brouard 8570: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
8571: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
1.227 brouard 8572: } /* end yearp */
8573: } /* end k */
1.219 brouard 8574:
1.126 brouard 8575: fclose(ficresf);
1.215 brouard 8576: printf("End of Computing forecasting \n");
8577: fprintf(ficlog,"End of Computing forecasting\n");
8578:
1.126 brouard 8579: }
8580:
1.269 brouard 8581: /************** Back Forecasting ******************/
1.296 brouard 8582: /* 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){ */
8583: 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){
8584: /* back1, year, month, day of starting backprojection
1.267 brouard 8585: agemin, agemax range of age
8586: dateprev1 dateprev2 range of dates during which prevalence is computed
1.269 brouard 8587: anback2 year of end of backprojection (same day and month as back1).
8588: prevacurrent and prev are prevalences.
1.267 brouard 8589: */
8590: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
8591: double agec; /* generic age */
1.302 brouard 8592: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
1.267 brouard 8593: double *popeffectif,*popcount;
8594: double ***p3mat;
8595: /* double ***mobaverage; */
8596: char fileresfb[FILENAMELENGTH];
8597:
1.268 brouard 8598: agelim=AGEINF;
1.267 brouard 8599: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8600: in each health status at the date of interview (if between dateprev1 and dateprev2).
8601: We still use firstpass and lastpass as another selection.
8602: */
8603: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8604: /* firstpass, lastpass, stepm, weightopt, model); */
8605:
8606: /*Do we need to compute prevalence again?*/
8607:
8608: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
8609:
8610: strcpy(fileresfb,"FB_");
8611: strcat(fileresfb,fileresu);
8612: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
8613: printf("Problem with back forecast resultfile: %s\n", fileresfb);
8614: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
8615: }
8616: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8617: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8618:
8619: if (cptcoveff==0) ncodemax[cptcoveff]=1;
8620:
8621:
8622: stepsize=(int) (stepm+YEARM-1)/YEARM;
8623: if (stepm<=12) stepsize=1;
8624: if(estepm < stepm){
8625: printf ("Problem %d lower than %d\n",estepm, stepm);
8626: }
1.270 brouard 8627: else{
8628: hstepm=estepm;
8629: }
8630: if(estepm >= stepm){ /* Yes every two year */
8631: stepsize=2;
8632: }
1.267 brouard 8633:
8634: hstepm=hstepm/stepm;
1.296 brouard 8635: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8636: /* fractional in yp1 *\/ */
8637: /* aintmean=yp; */
8638: /* yp2=modf((yp1*12),&yp); */
8639: /* mintmean=yp; */
8640: /* yp1=modf((yp2*30.5),&yp); */
8641: /* jintmean=yp; */
8642: /* if(jintmean==0) jintmean=1; */
8643: /* if(mintmean==0) jintmean=1; */
1.267 brouard 8644:
8645: i1=pow(2,cptcoveff);
8646: if (cptcovn < 1){i1=1;}
8647:
1.296 brouard 8648: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
8649: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.267 brouard 8650:
8651: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
8652:
8653: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8654: for(k=1; k<=i1;k++){
8655: if(i1 != 1 && TKresult[nres]!= k)
8656: continue;
8657: if(invalidvarcomb[k]){
8658: printf("\nCombination (%d) projection ignored because no cases \n",k);
8659: continue;
8660: }
1.268 brouard 8661: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
1.267 brouard 8662: for(j=1;j<=cptcoveff;j++) {
8663: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8664: }
8665: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8666: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
8667: }
8668: fprintf(ficresfb," yearbproj age");
8669: for(j=1; j<=nlstate+ndeath;j++){
8670: for(i=1; i<=nlstate;i++)
1.268 brouard 8671: fprintf(ficresfb," b%d%d",i,j);
8672: fprintf(ficresfb," b.%d",j);
1.267 brouard 8673: }
1.296 brouard 8674: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
1.267 brouard 8675: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
8676: fprintf(ficresfb,"\n");
1.296 brouard 8677: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
1.273 brouard 8678: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
1.270 brouard 8679: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
8680: for (agec=bage; agec<=fage; agec++){ /* testing */
1.268 brouard 8681: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
1.271 brouard 8682: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
1.267 brouard 8683: nhstepm = nhstepm/hstepm;
8684: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8685: oldm=oldms;savm=savms;
1.268 brouard 8686: /* computes hbxij at age agec over 1 to nhstepm */
1.271 brouard 8687: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
1.267 brouard 8688: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.268 brouard 8689: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
8690: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
8691: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
1.267 brouard 8692: for (h=0; h<=nhstepm; h++){
1.268 brouard 8693: if (h*hstepm/YEARM*stepm ==-yearp) {
8694: break;
8695: }
8696: }
8697: fprintf(ficresfb,"\n");
8698: for(j=1;j<=cptcoveff;j++)
8699: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 8700: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
1.268 brouard 8701: for(i=1; i<=nlstate+ndeath;i++) {
8702: ppij=0.;ppi=0.;
8703: for(j=1; j<=nlstate;j++) {
8704: /* if (mobilav==1) */
1.269 brouard 8705: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
8706: ppi=ppi+prevacurrent[(int)agec][j][k];
8707: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
8708: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
1.267 brouard 8709: /* else { */
8710: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
8711: /* } */
1.268 brouard 8712: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
8713: } /* end j */
8714: if(ppi <0.99){
8715: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
8716: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
8717: }
8718: fprintf(ficresfb," %.3f", ppij);
8719: }/* end j */
1.267 brouard 8720: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8721: } /* end agec */
8722: } /* end yearp */
8723: } /* end k */
1.217 brouard 8724:
1.267 brouard 8725: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
1.217 brouard 8726:
1.267 brouard 8727: fclose(ficresfb);
8728: printf("End of Computing Back forecasting \n");
8729: fprintf(ficlog,"End of Computing Back forecasting\n");
1.218 brouard 8730:
1.267 brouard 8731: }
1.217 brouard 8732:
1.269 brouard 8733: /* Variance of prevalence limit: varprlim */
8734: 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 8735: /*------- Variance of forward period (stable) prevalence------*/
1.269 brouard 8736:
8737: char fileresvpl[FILENAMELENGTH];
8738: FILE *ficresvpl;
8739: double **oldm, **savm;
8740: double **varpl; /* Variances of prevalence limits by age */
8741: int i1, k, nres, j ;
8742:
8743: strcpy(fileresvpl,"VPL_");
8744: strcat(fileresvpl,fileresu);
8745: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.288 brouard 8746: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
1.269 brouard 8747: exit(0);
8748: }
1.288 brouard 8749: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
8750: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.269 brouard 8751:
8752: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
8753: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
8754:
8755: i1=pow(2,cptcoveff);
8756: if (cptcovn < 1){i1=1;}
8757:
8758: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8759: for(k=1; k<=i1;k++){
8760: if(i1 != 1 && TKresult[nres]!= k)
8761: continue;
8762: fprintf(ficresvpl,"\n#****** ");
8763: printf("\n#****** ");
8764: fprintf(ficlog,"\n#****** ");
8765: for(j=1;j<=cptcoveff;j++) {
8766: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8767: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8768: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8769: }
8770: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
8771: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8772: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8773: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8774: }
8775: fprintf(ficresvpl,"******\n");
8776: printf("******\n");
8777: fprintf(ficlog,"******\n");
8778:
8779: varpl=matrix(1,nlstate,(int) bage, (int) fage);
8780: oldm=oldms;savm=savms;
8781: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
8782: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
8783: /*}*/
8784: }
8785:
8786: fclose(ficresvpl);
1.288 brouard 8787: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
8788: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
1.269 brouard 8789:
8790: }
8791: /* Variance of back prevalence: varbprlim */
8792: 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){
8793: /*------- Variance of back (stable) prevalence------*/
8794:
8795: char fileresvbl[FILENAMELENGTH];
8796: FILE *ficresvbl;
8797:
8798: double **oldm, **savm;
8799: double **varbpl; /* Variances of back prevalence limits by age */
8800: int i1, k, nres, j ;
8801:
8802: strcpy(fileresvbl,"VBL_");
8803: strcat(fileresvbl,fileresu);
8804: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
8805: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
8806: exit(0);
8807: }
8808: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
8809: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
8810:
8811:
8812: i1=pow(2,cptcoveff);
8813: if (cptcovn < 1){i1=1;}
8814:
8815: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8816: for(k=1; k<=i1;k++){
8817: if(i1 != 1 && TKresult[nres]!= k)
8818: continue;
8819: fprintf(ficresvbl,"\n#****** ");
8820: printf("\n#****** ");
8821: fprintf(ficlog,"\n#****** ");
8822: for(j=1;j<=cptcoveff;j++) {
8823: fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8824: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8825: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8826: }
8827: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
8828: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8829: fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8830: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8831: }
8832: fprintf(ficresvbl,"******\n");
8833: printf("******\n");
8834: fprintf(ficlog,"******\n");
8835:
8836: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
8837: oldm=oldms;savm=savms;
8838:
8839: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
8840: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
8841: /*}*/
8842: }
8843:
8844: fclose(ficresvbl);
8845: printf("done variance-covariance of back prevalence\n");fflush(stdout);
8846: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
8847:
8848: } /* End of varbprlim */
8849:
1.126 brouard 8850: /************** Forecasting *****not tested NB*************/
1.227 brouard 8851: /* 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 8852:
1.227 brouard 8853: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
8854: /* int *popage; */
8855: /* double calagedatem, agelim, kk1, kk2; */
8856: /* double *popeffectif,*popcount; */
8857: /* double ***p3mat,***tabpop,***tabpopprev; */
8858: /* /\* double ***mobaverage; *\/ */
8859: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 8860:
1.227 brouard 8861: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8862: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8863: /* agelim=AGESUP; */
8864: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 8865:
1.227 brouard 8866: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 8867:
8868:
1.227 brouard 8869: /* strcpy(filerespop,"POP_"); */
8870: /* strcat(filerespop,fileresu); */
8871: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
8872: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
8873: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
8874: /* } */
8875: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
8876: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 8877:
1.227 brouard 8878: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 8879:
1.227 brouard 8880: /* /\* if (mobilav!=0) { *\/ */
8881: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
8882: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
8883: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
8884: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
8885: /* /\* } *\/ */
8886: /* /\* } *\/ */
1.126 brouard 8887:
1.227 brouard 8888: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
8889: /* if (stepm<=12) stepsize=1; */
1.126 brouard 8890:
1.227 brouard 8891: /* agelim=AGESUP; */
1.126 brouard 8892:
1.227 brouard 8893: /* hstepm=1; */
8894: /* hstepm=hstepm/stepm; */
1.218 brouard 8895:
1.227 brouard 8896: /* if (popforecast==1) { */
8897: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
8898: /* printf("Problem with population file : %s\n",popfile);exit(0); */
8899: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
8900: /* } */
8901: /* popage=ivector(0,AGESUP); */
8902: /* popeffectif=vector(0,AGESUP); */
8903: /* popcount=vector(0,AGESUP); */
1.126 brouard 8904:
1.227 brouard 8905: /* i=1; */
8906: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 8907:
1.227 brouard 8908: /* imx=i; */
8909: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
8910: /* } */
1.218 brouard 8911:
1.227 brouard 8912: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
8913: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
8914: /* k=k+1; */
8915: /* fprintf(ficrespop,"\n#******"); */
8916: /* for(j=1;j<=cptcoveff;j++) { */
8917: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
8918: /* } */
8919: /* fprintf(ficrespop,"******\n"); */
8920: /* fprintf(ficrespop,"# Age"); */
8921: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
8922: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 8923:
1.227 brouard 8924: /* for (cpt=0; cpt<=0;cpt++) { */
8925: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 8926:
1.227 brouard 8927: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
8928: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
8929: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 8930:
1.227 brouard 8931: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8932: /* oldm=oldms;savm=savms; */
8933: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 8934:
1.227 brouard 8935: /* for (h=0; h<=nhstepm; h++){ */
8936: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
8937: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
8938: /* } */
8939: /* for(j=1; j<=nlstate+ndeath;j++) { */
8940: /* kk1=0.;kk2=0; */
8941: /* for(i=1; i<=nlstate;i++) { */
8942: /* if (mobilav==1) */
8943: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
8944: /* else { */
8945: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
8946: /* } */
8947: /* } */
8948: /* if (h==(int)(calagedatem+12*cpt)){ */
8949: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
8950: /* /\*fprintf(ficrespop," %.3f", kk1); */
8951: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
8952: /* } */
8953: /* } */
8954: /* for(i=1; i<=nlstate;i++){ */
8955: /* kk1=0.; */
8956: /* for(j=1; j<=nlstate;j++){ */
8957: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
8958: /* } */
8959: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
8960: /* } */
1.218 brouard 8961:
1.227 brouard 8962: /* if (h==(int)(calagedatem+12*cpt)) */
8963: /* for(j=1; j<=nlstate;j++) */
8964: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
8965: /* } */
8966: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8967: /* } */
8968: /* } */
1.218 brouard 8969:
1.227 brouard 8970: /* /\******\/ */
1.218 brouard 8971:
1.227 brouard 8972: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
8973: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
8974: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
8975: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
8976: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 8977:
1.227 brouard 8978: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8979: /* oldm=oldms;savm=savms; */
8980: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
8981: /* for (h=0; h<=nhstepm; h++){ */
8982: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
8983: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
8984: /* } */
8985: /* for(j=1; j<=nlstate+ndeath;j++) { */
8986: /* kk1=0.;kk2=0; */
8987: /* for(i=1; i<=nlstate;i++) { */
8988: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
8989: /* } */
8990: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
8991: /* } */
8992: /* } */
8993: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8994: /* } */
8995: /* } */
8996: /* } */
8997: /* } */
1.218 brouard 8998:
1.227 brouard 8999: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 9000:
1.227 brouard 9001: /* if (popforecast==1) { */
9002: /* free_ivector(popage,0,AGESUP); */
9003: /* free_vector(popeffectif,0,AGESUP); */
9004: /* free_vector(popcount,0,AGESUP); */
9005: /* } */
9006: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9007: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
9008: /* fclose(ficrespop); */
9009: /* } /\* End of popforecast *\/ */
1.218 brouard 9010:
1.126 brouard 9011: int fileappend(FILE *fichier, char *optionfich)
9012: {
9013: if((fichier=fopen(optionfich,"a"))==NULL) {
9014: printf("Problem with file: %s\n", optionfich);
9015: fprintf(ficlog,"Problem with file: %s\n", optionfich);
9016: return (0);
9017: }
9018: fflush(fichier);
9019: return (1);
9020: }
9021:
9022:
9023: /**************** function prwizard **********************/
9024: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
9025: {
9026:
9027: /* Wizard to print covariance matrix template */
9028:
1.164 brouard 9029: char ca[32], cb[32];
9030: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 9031: int numlinepar;
9032:
9033: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9034: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9035: for(i=1; i <=nlstate; i++){
9036: jj=0;
9037: for(j=1; j <=nlstate+ndeath; j++){
9038: if(j==i) continue;
9039: jj++;
9040: /*ca[0]= k+'a'-1;ca[1]='\0';*/
9041: printf("%1d%1d",i,j);
9042: fprintf(ficparo,"%1d%1d",i,j);
9043: for(k=1; k<=ncovmodel;k++){
9044: /* printf(" %lf",param[i][j][k]); */
9045: /* fprintf(ficparo," %lf",param[i][j][k]); */
9046: printf(" 0.");
9047: fprintf(ficparo," 0.");
9048: }
9049: printf("\n");
9050: fprintf(ficparo,"\n");
9051: }
9052: }
9053: printf("# Scales (for hessian or gradient estimation)\n");
9054: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
9055: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
9056: for(i=1; i <=nlstate; i++){
9057: jj=0;
9058: for(j=1; j <=nlstate+ndeath; j++){
9059: if(j==i) continue;
9060: jj++;
9061: fprintf(ficparo,"%1d%1d",i,j);
9062: printf("%1d%1d",i,j);
9063: fflush(stdout);
9064: for(k=1; k<=ncovmodel;k++){
9065: /* printf(" %le",delti3[i][j][k]); */
9066: /* fprintf(ficparo," %le",delti3[i][j][k]); */
9067: printf(" 0.");
9068: fprintf(ficparo," 0.");
9069: }
9070: numlinepar++;
9071: printf("\n");
9072: fprintf(ficparo,"\n");
9073: }
9074: }
9075: printf("# Covariance matrix\n");
9076: /* # 121 Var(a12)\n\ */
9077: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9078: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9079: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9080: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9081: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9082: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9083: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9084: fflush(stdout);
9085: fprintf(ficparo,"# Covariance matrix\n");
9086: /* # 121 Var(a12)\n\ */
9087: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9088: /* # ...\n\ */
9089: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9090:
9091: for(itimes=1;itimes<=2;itimes++){
9092: jj=0;
9093: for(i=1; i <=nlstate; i++){
9094: for(j=1; j <=nlstate+ndeath; j++){
9095: if(j==i) continue;
9096: for(k=1; k<=ncovmodel;k++){
9097: jj++;
9098: ca[0]= k+'a'-1;ca[1]='\0';
9099: if(itimes==1){
9100: printf("#%1d%1d%d",i,j,k);
9101: fprintf(ficparo,"#%1d%1d%d",i,j,k);
9102: }else{
9103: printf("%1d%1d%d",i,j,k);
9104: fprintf(ficparo,"%1d%1d%d",i,j,k);
9105: /* printf(" %.5le",matcov[i][j]); */
9106: }
9107: ll=0;
9108: for(li=1;li <=nlstate; li++){
9109: for(lj=1;lj <=nlstate+ndeath; lj++){
9110: if(lj==li) continue;
9111: for(lk=1;lk<=ncovmodel;lk++){
9112: ll++;
9113: if(ll<=jj){
9114: cb[0]= lk +'a'-1;cb[1]='\0';
9115: if(ll<jj){
9116: if(itimes==1){
9117: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9118: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9119: }else{
9120: printf(" 0.");
9121: fprintf(ficparo," 0.");
9122: }
9123: }else{
9124: if(itimes==1){
9125: printf(" Var(%s%1d%1d)",ca,i,j);
9126: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
9127: }else{
9128: printf(" 0.");
9129: fprintf(ficparo," 0.");
9130: }
9131: }
9132: }
9133: } /* end lk */
9134: } /* end lj */
9135: } /* end li */
9136: printf("\n");
9137: fprintf(ficparo,"\n");
9138: numlinepar++;
9139: } /* end k*/
9140: } /*end j */
9141: } /* end i */
9142: } /* end itimes */
9143:
9144: } /* end of prwizard */
9145: /******************* Gompertz Likelihood ******************************/
9146: double gompertz(double x[])
9147: {
1.302 brouard 9148: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
1.126 brouard 9149: int i,n=0; /* n is the size of the sample */
9150:
1.220 brouard 9151: for (i=1;i<=imx ; i++) {
1.126 brouard 9152: sump=sump+weight[i];
9153: /* sump=sump+1;*/
9154: num=num+1;
9155: }
1.302 brouard 9156: L=0.0;
9157: /* agegomp=AGEGOMP; */
1.126 brouard 9158: /* for (i=0; i<=imx; i++)
9159: 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]);*/
9160:
1.302 brouard 9161: for (i=1;i<=imx ; i++) {
9162: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
9163: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
9164: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
9165: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
9166: * +
9167: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
9168: */
9169: if (wav[i] > 1 || agedc[i] < AGESUP) {
9170: if (cens[i] == 1){
9171: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
9172: } else if (cens[i] == 0){
1.126 brouard 9173: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
1.302 brouard 9174: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
9175: } else
9176: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
1.126 brouard 9177: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
1.302 brouard 9178: L=L+A*weight[i];
1.126 brouard 9179: /* 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 9180: }
9181: }
1.126 brouard 9182:
1.302 brouard 9183: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
1.126 brouard 9184:
9185: return -2*L*num/sump;
9186: }
9187:
1.136 brouard 9188: #ifdef GSL
9189: /******************* Gompertz_f Likelihood ******************************/
9190: double gompertz_f(const gsl_vector *v, void *params)
9191: {
1.302 brouard 9192: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
1.136 brouard 9193: double *x= (double *) v->data;
9194: int i,n=0; /* n is the size of the sample */
9195:
9196: for (i=0;i<=imx-1 ; i++) {
9197: sump=sump+weight[i];
9198: /* sump=sump+1;*/
9199: num=num+1;
9200: }
9201:
9202:
9203: /* for (i=0; i<=imx; i++)
9204: 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]);*/
9205: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
9206: for (i=1;i<=imx ; i++)
9207: {
9208: if (cens[i] == 1 && wav[i]>1)
9209: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
9210:
9211: if (cens[i] == 0 && wav[i]>1)
9212: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
9213: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
9214:
9215: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
9216: if (wav[i] > 1 ) { /* ??? */
9217: LL=LL+A*weight[i];
9218: /* 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]);*/
9219: }
9220: }
9221:
9222: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
9223: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
9224:
9225: return -2*LL*num/sump;
9226: }
9227: #endif
9228:
1.126 brouard 9229: /******************* Printing html file ***********/
1.201 brouard 9230: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 9231: int lastpass, int stepm, int weightopt, char model[],\
9232: int imx, double p[],double **matcov,double agemortsup){
9233: int i,k;
9234:
9235: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
9236: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
9237: for (i=1;i<=2;i++)
9238: 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 9239: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 9240: fprintf(fichtm,"</ul>");
9241:
9242: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
9243:
9244: 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>");
9245:
9246: for (k=agegomp;k<(agemortsup-2);k++)
9247: 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]);
9248:
9249:
9250: fflush(fichtm);
9251: }
9252:
9253: /******************* Gnuplot file **************/
1.201 brouard 9254: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 9255:
9256: char dirfileres[132],optfileres[132];
1.164 brouard 9257:
1.126 brouard 9258: int ng;
9259:
9260:
9261: /*#ifdef windows */
9262: fprintf(ficgp,"cd \"%s\" \n",pathc);
9263: /*#endif */
9264:
9265:
9266: strcpy(dirfileres,optionfilefiname);
9267: strcpy(optfileres,"vpl");
1.199 brouard 9268: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 9269: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 9270: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 9271: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 9272: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
9273:
9274: }
9275:
1.136 brouard 9276: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
9277: {
1.126 brouard 9278:
1.136 brouard 9279: /*-------- data file ----------*/
9280: FILE *fic;
9281: char dummy[]=" ";
1.240 brouard 9282: int i=0, j=0, n=0, iv=0, v;
1.223 brouard 9283: int lstra;
1.136 brouard 9284: int linei, month, year,iout;
1.302 brouard 9285: int noffset=0; /* This is the offset if BOM data file */
1.136 brouard 9286: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 9287: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 9288: char *stratrunc;
1.223 brouard 9289:
1.240 brouard 9290: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
9291: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
1.126 brouard 9292:
1.240 brouard 9293: for(v=1; v <=ncovcol;v++){
9294: DummyV[v]=0;
9295: FixedV[v]=0;
9296: }
9297: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
9298: DummyV[v]=1;
9299: FixedV[v]=0;
9300: }
9301: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
9302: DummyV[v]=0;
9303: FixedV[v]=1;
9304: }
9305: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
9306: DummyV[v]=1;
9307: FixedV[v]=1;
9308: }
9309: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
9310: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
9311: 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]);
9312: }
1.126 brouard 9313:
1.136 brouard 9314: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 9315: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9316: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 9317: }
1.126 brouard 9318:
1.302 brouard 9319: /* Is it a BOM UTF-8 Windows file? */
9320: /* First data line */
9321: linei=0;
9322: while(fgets(line, MAXLINE, fic)) {
9323: noffset=0;
9324: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
9325: {
9326: noffset=noffset+3;
9327: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
9328: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
9329: fflush(ficlog); return 1;
9330: }
9331: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
9332: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
9333: {
9334: noffset=noffset+2;
1.304 brouard 9335: 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);
9336: 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 9337: fflush(ficlog); return 1;
9338: }
9339: else if( line[0] == 0 && line[1] == 0)
9340: {
9341: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
9342: noffset=noffset+4;
1.304 brouard 9343: 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);
9344: 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 9345: fflush(ficlog); return 1;
9346: }
9347: } else{
9348: ;/*printf(" Not a BOM file\n");*/
9349: }
9350: /* If line starts with a # it is a comment */
9351: if (line[noffset] == '#') {
9352: linei=linei+1;
9353: break;
9354: }else{
9355: break;
9356: }
9357: }
9358: fclose(fic);
9359: if((fic=fopen(datafile,"r"))==NULL) {
9360: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9361: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
9362: }
9363: /* Not a Bom file */
9364:
1.136 brouard 9365: i=1;
9366: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
9367: linei=linei+1;
9368: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
9369: if(line[j] == '\t')
9370: line[j] = ' ';
9371: }
9372: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
9373: ;
9374: };
9375: line[j+1]=0; /* Trims blanks at end of line */
9376: if(line[0]=='#'){
9377: fprintf(ficlog,"Comment line\n%s\n",line);
9378: printf("Comment line\n%s\n",line);
9379: continue;
9380: }
9381: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 9382: strcpy(line, linetmp);
1.223 brouard 9383:
9384: /* Loops on waves */
9385: for (j=maxwav;j>=1;j--){
9386: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 9387: cutv(stra, strb, line, ' ');
9388: if(strb[0]=='.') { /* Missing value */
9389: lval=-1;
9390: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
9391: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
9392: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
9393: 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);
9394: 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);
9395: return 1;
9396: }
9397: }else{
9398: errno=0;
9399: /* what_kind_of_number(strb); */
9400: dval=strtod(strb,&endptr);
9401: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
9402: /* if(strb != endptr && *endptr == '\0') */
9403: /* dval=dlval; */
9404: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9405: if( strb[0]=='\0' || (*endptr != '\0')){
9406: 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);
9407: 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);
9408: return 1;
9409: }
9410: cotqvar[j][iv][i]=dval;
9411: cotvar[j][ntv+iv][i]=dval;
9412: }
9413: strcpy(line,stra);
1.223 brouard 9414: }/* end loop ntqv */
1.225 brouard 9415:
1.223 brouard 9416: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 9417: cutv(stra, strb, line, ' ');
9418: if(strb[0]=='.') { /* Missing value */
9419: lval=-1;
9420: }else{
9421: errno=0;
9422: lval=strtol(strb,&endptr,10);
9423: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9424: if( strb[0]=='\0' || (*endptr != '\0')){
9425: 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);
9426: 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);
9427: return 1;
9428: }
9429: }
9430: if(lval <-1 || lval >1){
9431: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 9432: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9433: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9434: For example, for multinomial values like 1, 2 and 3,\n \
9435: build V1=0 V2=0 for the reference value (1),\n \
9436: V1=1 V2=0 for (2) \n \
1.223 brouard 9437: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9438: output of IMaCh is often meaningless.\n \
1.223 brouard 9439: Exiting.\n",lval,linei, i,line,j);
1.238 brouard 9440: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 9441: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9442: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9443: For example, for multinomial values like 1, 2 and 3,\n \
9444: build V1=0 V2=0 for the reference value (1),\n \
9445: V1=1 V2=0 for (2) \n \
1.223 brouard 9446: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9447: output of IMaCh is often meaningless.\n \
1.223 brouard 9448: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.238 brouard 9449: return 1;
9450: }
9451: cotvar[j][iv][i]=(double)(lval);
9452: strcpy(line,stra);
1.223 brouard 9453: }/* end loop ntv */
1.225 brouard 9454:
1.223 brouard 9455: /* Statuses at wave */
1.137 brouard 9456: cutv(stra, strb, line, ' ');
1.223 brouard 9457: if(strb[0]=='.') { /* Missing value */
1.238 brouard 9458: lval=-1;
1.136 brouard 9459: }else{
1.238 brouard 9460: errno=0;
9461: lval=strtol(strb,&endptr,10);
9462: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9463: if( strb[0]=='\0' || (*endptr != '\0')){
9464: 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);
9465: 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);
9466: return 1;
9467: }
1.136 brouard 9468: }
1.225 brouard 9469:
1.136 brouard 9470: s[j][i]=lval;
1.225 brouard 9471:
1.223 brouard 9472: /* Date of Interview */
1.136 brouard 9473: strcpy(line,stra);
9474: cutv(stra, strb,line,' ');
1.169 brouard 9475: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9476: }
1.169 brouard 9477: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 9478: month=99;
9479: year=9999;
1.136 brouard 9480: }else{
1.225 brouard 9481: 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);
9482: 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);
9483: return 1;
1.136 brouard 9484: }
9485: anint[j][i]= (double) year;
1.302 brouard 9486: mint[j][i]= (double)month;
9487: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
9488: /* 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]); */
9489: /* 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]); */
9490: /* } */
1.136 brouard 9491: strcpy(line,stra);
1.223 brouard 9492: } /* End loop on waves */
1.225 brouard 9493:
1.223 brouard 9494: /* Date of death */
1.136 brouard 9495: cutv(stra, strb,line,' ');
1.169 brouard 9496: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9497: }
1.169 brouard 9498: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 9499: month=99;
9500: year=9999;
9501: }else{
1.141 brouard 9502: 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 9503: 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);
9504: return 1;
1.136 brouard 9505: }
9506: andc[i]=(double) year;
9507: moisdc[i]=(double) month;
9508: strcpy(line,stra);
9509:
1.223 brouard 9510: /* Date of birth */
1.136 brouard 9511: cutv(stra, strb,line,' ');
1.169 brouard 9512: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9513: }
1.169 brouard 9514: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 9515: month=99;
9516: year=9999;
9517: }else{
1.141 brouard 9518: 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);
9519: 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 9520: return 1;
1.136 brouard 9521: }
9522: if (year==9999) {
1.141 brouard 9523: 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);
9524: 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 9525: return 1;
9526:
1.136 brouard 9527: }
9528: annais[i]=(double)(year);
1.302 brouard 9529: moisnais[i]=(double)(month);
9530: for (j=1;j<=maxwav;j++){
9531: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
9532: 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]);
9533: 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]);
9534: }
9535: }
9536:
1.136 brouard 9537: strcpy(line,stra);
1.225 brouard 9538:
1.223 brouard 9539: /* Sample weight */
1.136 brouard 9540: cutv(stra, strb,line,' ');
9541: errno=0;
9542: dval=strtod(strb,&endptr);
9543: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 9544: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
9545: 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 9546: fflush(ficlog);
9547: return 1;
9548: }
9549: weight[i]=dval;
9550: strcpy(line,stra);
1.225 brouard 9551:
1.223 brouard 9552: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
9553: cutv(stra, strb, line, ' ');
9554: if(strb[0]=='.') { /* Missing value */
1.225 brouard 9555: lval=-1;
1.311 brouard 9556: coqvar[iv][i]=NAN;
9557: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
1.223 brouard 9558: }else{
1.225 brouard 9559: errno=0;
9560: /* what_kind_of_number(strb); */
9561: dval=strtod(strb,&endptr);
9562: /* if(strb != endptr && *endptr == '\0') */
9563: /* dval=dlval; */
9564: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9565: if( strb[0]=='\0' || (*endptr != '\0')){
9566: 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);
9567: 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);
9568: return 1;
9569: }
9570: coqvar[iv][i]=dval;
1.226 brouard 9571: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 9572: }
9573: strcpy(line,stra);
9574: }/* end loop nqv */
1.136 brouard 9575:
1.223 brouard 9576: /* Covariate values */
1.136 brouard 9577: for (j=ncovcol;j>=1;j--){
9578: cutv(stra, strb,line,' ');
1.223 brouard 9579: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 9580: lval=-1;
1.136 brouard 9581: }else{
1.225 brouard 9582: errno=0;
9583: lval=strtol(strb,&endptr,10);
9584: if( strb[0]=='\0' || (*endptr != '\0')){
9585: 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);
9586: 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);
9587: return 1;
9588: }
1.136 brouard 9589: }
9590: if(lval <-1 || lval >1){
1.225 brouard 9591: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9592: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9593: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9594: For example, for multinomial values like 1, 2 and 3,\n \
9595: build V1=0 V2=0 for the reference value (1),\n \
9596: V1=1 V2=0 for (2) \n \
1.136 brouard 9597: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9598: output of IMaCh is often meaningless.\n \
1.136 brouard 9599: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 9600: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9601: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9602: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9603: For example, for multinomial values like 1, 2 and 3,\n \
9604: build V1=0 V2=0 for the reference value (1),\n \
9605: V1=1 V2=0 for (2) \n \
1.136 brouard 9606: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9607: output of IMaCh is often meaningless.\n \
1.136 brouard 9608: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 9609: return 1;
1.136 brouard 9610: }
9611: covar[j][i]=(double)(lval);
9612: strcpy(line,stra);
9613: }
9614: lstra=strlen(stra);
1.225 brouard 9615:
1.136 brouard 9616: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
9617: stratrunc = &(stra[lstra-9]);
9618: num[i]=atol(stratrunc);
9619: }
9620: else
9621: num[i]=atol(stra);
9622: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
9623: 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;}*/
9624:
9625: i=i+1;
9626: } /* End loop reading data */
1.225 brouard 9627:
1.136 brouard 9628: *imax=i-1; /* Number of individuals */
9629: fclose(fic);
1.225 brouard 9630:
1.136 brouard 9631: return (0);
1.164 brouard 9632: /* endread: */
1.225 brouard 9633: printf("Exiting readdata: ");
9634: fclose(fic);
9635: return (1);
1.223 brouard 9636: }
1.126 brouard 9637:
1.234 brouard 9638: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 9639: char *p1 = *stri, *p2 = *stri;
1.235 brouard 9640: while (*p2 == ' ')
1.234 brouard 9641: p2++;
9642: /* while ((*p1++ = *p2++) !=0) */
9643: /* ; */
9644: /* do */
9645: /* while (*p2 == ' ') */
9646: /* p2++; */
9647: /* while (*p1++ == *p2++); */
9648: *stri=p2;
1.145 brouard 9649: }
9650:
1.235 brouard 9651: int decoderesult ( char resultline[], int nres)
1.230 brouard 9652: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
9653: {
1.235 brouard 9654: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 9655: char resultsav[MAXLINE];
1.234 brouard 9656: int resultmodel[MAXLINE];
9657: int modelresult[MAXLINE];
1.230 brouard 9658: char stra[80], strb[80], strc[80], strd[80],stre[80];
9659:
1.234 brouard 9660: removefirstspace(&resultline);
1.230 brouard 9661:
9662: if (strstr(resultline,"v") !=0){
9663: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
9664: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
9665: return 1;
9666: }
9667: trimbb(resultsav, resultline);
9668: if (strlen(resultsav) >1){
9669: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
9670: }
1.253 brouard 9671: if(j == 0){ /* Resultline but no = */
9672: TKresult[nres]=0; /* Combination for the nresult and the model */
9673: return (0);
9674: }
1.234 brouard 9675: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
1.310 brouard 9676: printf("ERROR: the number of variables in the resultline, %d, differs from the number of variables used in the model line, %d.\n",j, cptcovs);
9677: 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 9678: }
9679: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
9680: if(nbocc(resultsav,'=') >1){
9681: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
1.310 brouard 9682: resultsav= V4=1 V5=25.1 V3=0 stra= V5=25.1 V3=0 strb= V4=1 */
1.234 brouard 9683: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
9684: }else
9685: cutl(strc,strd,resultsav,'=');
1.230 brouard 9686: Tvalsel[k]=atof(strc); /* 1 */
1.234 brouard 9687:
1.230 brouard 9688: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
9689: Tvarsel[k]=atoi(strc);
9690: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
9691: /* cptcovsel++; */
9692: if (nbocc(stra,'=') >0)
9693: strcpy(resultsav,stra); /* and analyzes it */
9694: }
1.235 brouard 9695: /* Checking for missing or useless values in comparison of current model needs */
1.236 brouard 9696: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9697: if(Typevar[k1]==0){ /* Single covariate in model */
1.234 brouard 9698: match=0;
1.236 brouard 9699: for(k2=1; k2 <=j;k2++){/* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.237 brouard 9700: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar[1]=5 == Tvarsel[2]=5 */
1.236 brouard 9701: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.234 brouard 9702: match=1;
9703: break;
9704: }
9705: }
9706: if(match == 0){
1.310 brouard 9707: printf("Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model);
9708: fprintf(ficlog,"Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model);
9709: return 1;
1.234 brouard 9710: }
9711: }
9712: }
1.235 brouard 9713: /* Checking for missing or useless values in comparison of current model needs */
9714: for(k2=1; k2 <=j;k2++){ /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 9715: match=0;
1.235 brouard 9716: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9717: if(Typevar[k1]==0){ /* Single */
1.237 brouard 9718: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
1.235 brouard 9719: resultmodel[k1]=k2; /* resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.234 brouard 9720: ++match;
9721: }
9722: }
9723: }
9724: if(match == 0){
9725: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
1.310 brouard 9726: fprintf(ficlog,"Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
9727: return 1;
1.234 brouard 9728: }else if(match > 1){
9729: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
1.310 brouard 9730: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
9731: return 1;
1.234 brouard 9732: }
9733: }
1.235 brouard 9734:
1.234 brouard 9735: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 9736: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9737: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */
9738: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/
9739: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
9740: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
9741: /* 1 0 0 0 */
9742: /* 2 1 0 0 */
9743: /* 3 0 1 0 */
9744: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */
9745: /* 5 0 0 1 */
9746: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */
9747: /* 7 0 1 1 */
9748: /* 8 1 1 1 */
1.237 brouard 9749: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
9750: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
9751: /* V5*age V5 known which value for nres? */
9752: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.235 brouard 9753: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* model line */
9754: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */
1.237 brouard 9755: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */
1.235 brouard 9756: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
9757: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */
1.237 brouard 9758: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */
9759: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
9760: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.235 brouard 9761: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4);
9762: k4++;;
9763: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */
9764: k3q= resultmodel[k1]; /* resultmodel[2] = 1=k3 */
9765: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
1.237 brouard 9766: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
9767: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
9768: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.235 brouard 9769: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
9770: k4q++;;
9771: }
9772: }
1.234 brouard 9773:
1.235 brouard 9774: TKresult[nres]=++k; /* Combination for the nresult and the model */
1.230 brouard 9775: return (0);
9776: }
1.235 brouard 9777:
1.230 brouard 9778: int decodemodel( char model[], int lastobs)
9779: /**< This routine decodes the model and returns:
1.224 brouard 9780: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
9781: * - nagesqr = 1 if age*age in the model, otherwise 0.
9782: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
9783: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
9784: * - cptcovage number of covariates with age*products =2
9785: * - cptcovs number of simple covariates
9786: * - 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
9787: * which is a new column after the 9 (ncovcol) variables.
9788: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
9789: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
9790: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
9791: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
9792: */
1.136 brouard 9793: {
1.238 brouard 9794: int i, j, k, ks, v;
1.227 brouard 9795: int j1, k1, k2, k3, k4;
1.136 brouard 9796: char modelsav[80];
1.145 brouard 9797: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 9798: char *strpt;
1.136 brouard 9799:
1.145 brouard 9800: /*removespace(model);*/
1.136 brouard 9801: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 9802: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 9803: if (strstr(model,"AGE") !=0){
1.192 brouard 9804: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
9805: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 9806: return 1;
9807: }
1.141 brouard 9808: if (strstr(model,"v") !=0){
9809: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
9810: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
9811: return 1;
9812: }
1.187 brouard 9813: strcpy(modelsav,model);
9814: if ((strpt=strstr(model,"age*age")) !=0){
9815: printf(" strpt=%s, model=%s\n",strpt, model);
9816: if(strpt != model){
1.234 brouard 9817: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 9818: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 9819: corresponding column of parameters.\n",model);
1.234 brouard 9820: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 9821: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 9822: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 9823: return 1;
1.225 brouard 9824: }
1.187 brouard 9825: nagesqr=1;
9826: if (strstr(model,"+age*age") !=0)
1.234 brouard 9827: substrchaine(modelsav, model, "+age*age");
1.187 brouard 9828: else if (strstr(model,"age*age+") !=0)
1.234 brouard 9829: substrchaine(modelsav, model, "age*age+");
1.187 brouard 9830: else
1.234 brouard 9831: substrchaine(modelsav, model, "age*age");
1.187 brouard 9832: }else
9833: nagesqr=0;
9834: if (strlen(modelsav) >1){
9835: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
9836: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 9837: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 9838: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 9839: * cst, age and age*age
9840: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
9841: /* including age products which are counted in cptcovage.
9842: * but the covariates which are products must be treated
9843: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 9844: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
9845: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 9846:
9847:
1.187 brouard 9848: /* Design
9849: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
9850: * < ncovcol=8 >
9851: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
9852: * k= 1 2 3 4 5 6 7 8
9853: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
9854: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 9855: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
9856: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 9857: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
9858: * Tage[++cptcovage]=k
9859: * if products, new covar are created after ncovcol with k1
9860: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
9861: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
9862: * 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
9863: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
9864: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
9865: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
9866: * < ncovcol=8 >
9867: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
9868: * k= 1 2 3 4 5 6 7 8 9 10 11 12
9869: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
9870: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
9871: * p Tprod[1]@2={ 6, 5}
9872: *p Tvard[1][1]@4= {7, 8, 5, 6}
9873: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
9874: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
9875: *How to reorganize?
9876: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
9877: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
9878: * {2, 1, 4, 8, 5, 6, 3, 7}
9879: * Struct []
9880: */
1.225 brouard 9881:
1.187 brouard 9882: /* This loop fills the array Tvar from the string 'model'.*/
9883: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
9884: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
9885: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
9886: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
9887: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
9888: /* k=1 Tvar[1]=2 (from V2) */
9889: /* k=5 Tvar[5] */
9890: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 9891: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 9892: /* } */
1.198 brouard 9893: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 9894: /*
9895: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 9896: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
9897: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
9898: }
1.187 brouard 9899: cptcovage=0;
9900: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.234 brouard 9901: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
1.225 brouard 9902: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.234 brouard 9903: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
9904: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
9905: /*scanf("%d",i);*/
9906: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
9907: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
9908: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
9909: /* covar is not filled and then is empty */
9910: cptcovprod--;
9911: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
9912: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
9913: Typevar[k]=1; /* 1 for age product */
9914: cptcovage++; /* Sums the number of covariates which include age as a product */
9915: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
9916: /*printf("stre=%s ", stre);*/
9917: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
9918: cptcovprod--;
9919: cutl(stre,strb,strc,'V');
9920: Tvar[k]=atoi(stre);
9921: Typevar[k]=1; /* 1 for age product */
9922: cptcovage++;
9923: Tage[cptcovage]=k;
9924: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
9925: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
9926: cptcovn++;
9927: cptcovprodnoage++;k1++;
9928: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
9929: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
9930: because this model-covariate is a construction we invent a new column
9931: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
9932: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
9933: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
9934: Typevar[k]=2; /* 2 for double fixed dummy covariates */
9935: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
9936: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
9937: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
9938: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
9939: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
9940: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
9941: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
9942: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 9943: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 brouard 9944: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
9945: for (i=1; i<=lastobs;i++){
9946: /* Computes the new covariate which is a product of
9947: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
9948: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
9949: }
9950: } /* End age is not in the model */
9951: } /* End if model includes a product */
9952: else { /* no more sum */
9953: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
9954: /* scanf("%d",i);*/
9955: cutl(strd,strc,strb,'V');
9956: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
9957: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
9958: Tvar[k]=atoi(strd);
9959: Typevar[k]=0; /* 0 for simple covariates */
9960: }
9961: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 9962: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 9963: scanf("%d",i);*/
1.187 brouard 9964: } /* end of loop + on total covariates */
9965: } /* end if strlen(modelsave == 0) age*age might exist */
9966: } /* end if strlen(model == 0) */
1.136 brouard 9967:
9968: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
9969: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 9970:
1.136 brouard 9971: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 9972: printf("cptcovprod=%d ", cptcovprod);
9973: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
9974: scanf("%d ",i);*/
9975:
9976:
1.230 brouard 9977: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
9978: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 9979: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
9980: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
9981: k = 1 2 3 4 5 6 7 8 9
9982: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
9983: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 9984: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
9985: Dummy[k] 1 0 0 0 3 1 1 2 3
9986: Tmodelind[combination of covar]=k;
1.225 brouard 9987: */
9988: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 9989: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 9990: /* 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 9991: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 9992: printf("Model=%s\n\
9993: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
9994: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
9995: 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);
9996: fprintf(ficlog,"Model=%s\n\
9997: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
9998: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
9999: 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 10000: for(k=-1;k<=cptcovt; k++){ Fixed[k]=0; Dummy[k]=0;}
1.234 brouard 10001: 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 */
10002: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 10003: Fixed[k]= 0;
10004: Dummy[k]= 0;
1.225 brouard 10005: ncoveff++;
1.232 brouard 10006: ncovf++;
1.234 brouard 10007: nsd++;
10008: modell[k].maintype= FTYPE;
10009: TvarsD[nsd]=Tvar[k];
10010: TvarsDind[nsd]=k;
10011: TvarF[ncovf]=Tvar[k];
10012: TvarFind[ncovf]=k;
10013: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10014: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
10015: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
10016: Fixed[k]= 0;
10017: Dummy[k]= 0;
10018: ncoveff++;
10019: ncovf++;
10020: modell[k].maintype= FTYPE;
10021: TvarF[ncovf]=Tvar[k];
10022: TvarFind[ncovf]=k;
1.230 brouard 10023: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 10024: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.240 brouard 10025: }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 10026: Fixed[k]= 0;
10027: Dummy[k]= 1;
1.230 brouard 10028: nqfveff++;
1.234 brouard 10029: modell[k].maintype= FTYPE;
10030: modell[k].subtype= FQ;
10031: nsq++;
10032: TvarsQ[nsq]=Tvar[k];
10033: TvarsQind[nsq]=k;
1.232 brouard 10034: ncovf++;
1.234 brouard 10035: TvarF[ncovf]=Tvar[k];
10036: TvarFind[ncovf]=k;
1.231 brouard 10037: 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 10038: 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 10039: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.227 brouard 10040: Fixed[k]= 1;
10041: Dummy[k]= 0;
1.225 brouard 10042: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 10043: modell[k].maintype= VTYPE;
10044: modell[k].subtype= VD;
10045: nsd++;
10046: TvarsD[nsd]=Tvar[k];
10047: TvarsDind[nsd]=k;
10048: ncovv++; /* Only simple time varying variables */
10049: TvarV[ncovv]=Tvar[k];
1.242 brouard 10050: 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 10051: 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 */
10052: 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 10053: 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);
10054: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 10055: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 brouard 10056: Fixed[k]= 1;
10057: Dummy[k]= 1;
10058: nqtveff++;
10059: modell[k].maintype= VTYPE;
10060: modell[k].subtype= VQ;
10061: ncovv++; /* Only simple time varying variables */
10062: nsq++;
10063: TvarsQ[nsq]=Tvar[k];
10064: TvarsQind[nsq]=k;
10065: TvarV[ncovv]=Tvar[k];
1.242 brouard 10066: 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 10067: 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 */
10068: 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 10069: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
10070: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
10071: 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 10072: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 10073: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 10074: ncova++;
10075: TvarA[ncova]=Tvar[k];
10076: TvarAind[ncova]=k;
1.231 brouard 10077: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 10078: Fixed[k]= 2;
10079: Dummy[k]= 2;
10080: modell[k].maintype= ATYPE;
10081: modell[k].subtype= APFD;
10082: /* ncoveff++; */
1.227 brouard 10083: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 10084: Fixed[k]= 2;
10085: Dummy[k]= 3;
10086: modell[k].maintype= ATYPE;
10087: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
10088: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 10089: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 10090: Fixed[k]= 3;
10091: Dummy[k]= 2;
10092: modell[k].maintype= ATYPE;
10093: modell[k].subtype= APVD; /* Product age * varying dummy */
10094: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 10095: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 10096: Fixed[k]= 3;
10097: Dummy[k]= 3;
10098: modell[k].maintype= ATYPE;
10099: modell[k].subtype= APVQ; /* Product age * varying quantitative */
10100: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 10101: }
10102: }else if (Typevar[k] == 2) { /* product without age */
10103: k1=Tposprod[k];
10104: if(Tvard[k1][1] <=ncovcol){
1.240 brouard 10105: if(Tvard[k1][2] <=ncovcol){
10106: Fixed[k]= 1;
10107: Dummy[k]= 0;
10108: modell[k].maintype= FTYPE;
10109: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
10110: ncovf++; /* Fixed variables without age */
10111: TvarF[ncovf]=Tvar[k];
10112: TvarFind[ncovf]=k;
10113: }else if(Tvard[k1][2] <=ncovcol+nqv){
10114: Fixed[k]= 0; /* or 2 ?*/
10115: Dummy[k]= 1;
10116: modell[k].maintype= FTYPE;
10117: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
10118: ncovf++; /* Varying variables without age */
10119: TvarF[ncovf]=Tvar[k];
10120: TvarFind[ncovf]=k;
10121: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10122: Fixed[k]= 1;
10123: Dummy[k]= 0;
10124: modell[k].maintype= VTYPE;
10125: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
10126: ncovv++; /* Varying variables without age */
10127: TvarV[ncovv]=Tvar[k];
10128: TvarVind[ncovv]=k;
10129: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10130: Fixed[k]= 1;
10131: Dummy[k]= 1;
10132: modell[k].maintype= VTYPE;
10133: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
10134: ncovv++; /* Varying variables without age */
10135: TvarV[ncovv]=Tvar[k];
10136: TvarVind[ncovv]=k;
10137: }
1.227 brouard 10138: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.240 brouard 10139: if(Tvard[k1][2] <=ncovcol){
10140: Fixed[k]= 0; /* or 2 ?*/
10141: Dummy[k]= 1;
10142: modell[k].maintype= FTYPE;
10143: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
10144: ncovf++; /* Fixed variables without age */
10145: TvarF[ncovf]=Tvar[k];
10146: TvarFind[ncovf]=k;
10147: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10148: Fixed[k]= 1;
10149: Dummy[k]= 1;
10150: modell[k].maintype= VTYPE;
10151: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
10152: ncovv++; /* Varying variables without age */
10153: TvarV[ncovv]=Tvar[k];
10154: TvarVind[ncovv]=k;
10155: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10156: Fixed[k]= 1;
10157: Dummy[k]= 1;
10158: modell[k].maintype= VTYPE;
10159: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
10160: ncovv++; /* Varying variables without age */
10161: TvarV[ncovv]=Tvar[k];
10162: TvarVind[ncovv]=k;
10163: ncovv++; /* Varying variables without age */
10164: TvarV[ncovv]=Tvar[k];
10165: TvarVind[ncovv]=k;
10166: }
1.227 brouard 10167: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.240 brouard 10168: if(Tvard[k1][2] <=ncovcol){
10169: Fixed[k]= 1;
10170: Dummy[k]= 1;
10171: modell[k].maintype= VTYPE;
10172: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
10173: ncovv++; /* Varying variables without age */
10174: TvarV[ncovv]=Tvar[k];
10175: TvarVind[ncovv]=k;
10176: }else if(Tvard[k1][2] <=ncovcol+nqv){
10177: Fixed[k]= 1;
10178: Dummy[k]= 1;
10179: modell[k].maintype= VTYPE;
10180: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
10181: ncovv++; /* Varying variables without age */
10182: TvarV[ncovv]=Tvar[k];
10183: TvarVind[ncovv]=k;
10184: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10185: Fixed[k]= 1;
10186: Dummy[k]= 0;
10187: modell[k].maintype= VTYPE;
10188: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
10189: ncovv++; /* Varying variables without age */
10190: TvarV[ncovv]=Tvar[k];
10191: TvarVind[ncovv]=k;
10192: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10193: Fixed[k]= 1;
10194: Dummy[k]= 1;
10195: modell[k].maintype= VTYPE;
10196: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
10197: ncovv++; /* Varying variables without age */
10198: TvarV[ncovv]=Tvar[k];
10199: TvarVind[ncovv]=k;
10200: }
1.227 brouard 10201: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 10202: if(Tvard[k1][2] <=ncovcol){
10203: Fixed[k]= 1;
10204: Dummy[k]= 1;
10205: modell[k].maintype= VTYPE;
10206: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
10207: ncovv++; /* Varying variables without age */
10208: TvarV[ncovv]=Tvar[k];
10209: TvarVind[ncovv]=k;
10210: }else if(Tvard[k1][2] <=ncovcol+nqv){
10211: Fixed[k]= 1;
10212: Dummy[k]= 1;
10213: modell[k].maintype= VTYPE;
10214: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
10215: ncovv++; /* Varying variables without age */
10216: TvarV[ncovv]=Tvar[k];
10217: TvarVind[ncovv]=k;
10218: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10219: Fixed[k]= 1;
10220: Dummy[k]= 1;
10221: modell[k].maintype= VTYPE;
10222: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
10223: ncovv++; /* Varying variables without age */
10224: TvarV[ncovv]=Tvar[k];
10225: TvarVind[ncovv]=k;
10226: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10227: Fixed[k]= 1;
10228: Dummy[k]= 1;
10229: modell[k].maintype= VTYPE;
10230: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
10231: ncovv++; /* Varying variables without age */
10232: TvarV[ncovv]=Tvar[k];
10233: TvarVind[ncovv]=k;
10234: }
1.227 brouard 10235: }else{
1.240 brouard 10236: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10237: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10238: } /*end k1*/
1.225 brouard 10239: }else{
1.226 brouard 10240: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
10241: 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 10242: }
1.227 brouard 10243: 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 10244: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 10245: 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]);
10246: }
10247: /* Searching for doublons in the model */
10248: for(k1=1; k1<= cptcovt;k1++){
10249: for(k2=1; k2 <k1;k2++){
1.285 brouard 10250: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
10251: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
1.234 brouard 10252: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
10253: if(Tvar[k1]==Tvar[k2]){
1.285 brouard 10254: 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]);
10255: 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 10256: return(1);
10257: }
10258: }else if (Typevar[k1] ==2){
10259: k3=Tposprod[k1];
10260: k4=Tposprod[k2];
10261: 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])) ){
10262: 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]]);
10263: 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);
10264: return(1);
10265: }
10266: }
1.227 brouard 10267: }
10268: }
1.225 brouard 10269: }
10270: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
10271: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 10272: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
10273: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 10274: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 10275: /*endread:*/
1.225 brouard 10276: printf("Exiting decodemodel: ");
10277: return (1);
1.136 brouard 10278: }
10279:
1.169 brouard 10280: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.248 brouard 10281: {/* Check ages at death */
1.136 brouard 10282: int i, m;
1.218 brouard 10283: int firstone=0;
10284:
1.136 brouard 10285: for (i=1; i<=imx; i++) {
10286: for(m=2; (m<= maxwav); m++) {
10287: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
10288: anint[m][i]=9999;
1.216 brouard 10289: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
10290: s[m][i]=-1;
1.136 brouard 10291: }
10292: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.260 brouard 10293: *nberr = *nberr + 1;
1.218 brouard 10294: if(firstone == 0){
10295: firstone=1;
1.260 brouard 10296: 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 10297: }
1.262 brouard 10298: 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 10299: s[m][i]=-1; /* Droping the death status */
1.136 brouard 10300: }
10301: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 10302: (*nberr)++;
1.259 brouard 10303: 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 10304: 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 10305: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.136 brouard 10306: }
10307: }
10308: }
10309:
10310: for (i=1; i<=imx; i++) {
10311: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
10312: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 10313: 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 10314: if (s[m][i] >= nlstate+1) {
1.169 brouard 10315: if(agedc[i]>0){
10316: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 10317: agev[m][i]=agedc[i];
1.214 brouard 10318: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 10319: }else {
1.136 brouard 10320: if ((int)andc[i]!=9999){
10321: nbwarn++;
10322: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
10323: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
10324: agev[m][i]=-1;
10325: }
10326: }
1.169 brouard 10327: } /* agedc > 0 */
1.214 brouard 10328: } /* end if */
1.136 brouard 10329: else if(s[m][i] !=9){ /* Standard case, age in fractional
10330: years but with the precision of a month */
10331: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
10332: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
10333: agev[m][i]=1;
10334: else if(agev[m][i] < *agemin){
10335: *agemin=agev[m][i];
10336: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
10337: }
10338: else if(agev[m][i] >*agemax){
10339: *agemax=agev[m][i];
1.156 brouard 10340: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 10341: }
10342: /*agev[m][i]=anint[m][i]-annais[i];*/
10343: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 10344: } /* en if 9*/
1.136 brouard 10345: else { /* =9 */
1.214 brouard 10346: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 10347: agev[m][i]=1;
10348: s[m][i]=-1;
10349: }
10350: }
1.214 brouard 10351: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 10352: agev[m][i]=1;
1.214 brouard 10353: else{
10354: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10355: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10356: agev[m][i]=0;
10357: }
10358: } /* End for lastpass */
10359: }
1.136 brouard 10360:
10361: for (i=1; i<=imx; i++) {
10362: for(m=firstpass; (m<=lastpass); m++){
10363: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 10364: (*nberr)++;
1.136 brouard 10365: 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);
10366: 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);
10367: return 1;
10368: }
10369: }
10370: }
10371:
10372: /*for (i=1; i<=imx; i++){
10373: for (m=firstpass; (m<lastpass); m++){
10374: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
10375: }
10376:
10377: }*/
10378:
10379:
1.139 brouard 10380: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
10381: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 10382:
10383: return (0);
1.164 brouard 10384: /* endread:*/
1.136 brouard 10385: printf("Exiting calandcheckages: ");
10386: return (1);
10387: }
10388:
1.172 brouard 10389: #if defined(_MSC_VER)
10390: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10391: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10392: //#include "stdafx.h"
10393: //#include <stdio.h>
10394: //#include <tchar.h>
10395: //#include <windows.h>
10396: //#include <iostream>
10397: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
10398:
10399: LPFN_ISWOW64PROCESS fnIsWow64Process;
10400:
10401: BOOL IsWow64()
10402: {
10403: BOOL bIsWow64 = FALSE;
10404:
10405: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
10406: // (HANDLE, PBOOL);
10407:
10408: //LPFN_ISWOW64PROCESS fnIsWow64Process;
10409:
10410: HMODULE module = GetModuleHandle(_T("kernel32"));
10411: const char funcName[] = "IsWow64Process";
10412: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
10413: GetProcAddress(module, funcName);
10414:
10415: if (NULL != fnIsWow64Process)
10416: {
10417: if (!fnIsWow64Process(GetCurrentProcess(),
10418: &bIsWow64))
10419: //throw std::exception("Unknown error");
10420: printf("Unknown error\n");
10421: }
10422: return bIsWow64 != FALSE;
10423: }
10424: #endif
1.177 brouard 10425:
1.191 brouard 10426: void syscompilerinfo(int logged)
1.292 brouard 10427: {
10428: #include <stdint.h>
10429:
10430: /* #include "syscompilerinfo.h"*/
1.185 brouard 10431: /* command line Intel compiler 32bit windows, XP compatible:*/
10432: /* /GS /W3 /Gy
10433: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
10434: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
10435: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 10436: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
10437: */
10438: /* 64 bits */
1.185 brouard 10439: /*
10440: /GS /W3 /Gy
10441: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
10442: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
10443: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
10444: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
10445: /* Optimization are useless and O3 is slower than O2 */
10446: /*
10447: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
10448: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
10449: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
10450: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
10451: */
1.186 brouard 10452: /* Link is */ /* /OUT:"visual studio
1.185 brouard 10453: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
10454: /PDB:"visual studio
10455: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
10456: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
10457: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
10458: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
10459: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
10460: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
10461: uiAccess='false'"
10462: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
10463: /NOLOGO /TLBID:1
10464: */
1.292 brouard 10465:
10466:
1.177 brouard 10467: #if defined __INTEL_COMPILER
1.178 brouard 10468: #if defined(__GNUC__)
10469: struct utsname sysInfo; /* For Intel on Linux and OS/X */
10470: #endif
1.177 brouard 10471: #elif defined(__GNUC__)
1.179 brouard 10472: #ifndef __APPLE__
1.174 brouard 10473: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 10474: #endif
1.177 brouard 10475: struct utsname sysInfo;
1.178 brouard 10476: int cross = CROSS;
10477: if (cross){
10478: printf("Cross-");
1.191 brouard 10479: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 10480: }
1.174 brouard 10481: #endif
10482:
1.191 brouard 10483: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 10484: #if defined(__clang__)
1.191 brouard 10485: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 10486: #endif
10487: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 10488: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 10489: #endif
10490: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 10491: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 10492: #endif
10493: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 10494: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 10495: #endif
10496: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 10497: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 10498: #endif
10499: #if defined(_MSC_VER)
1.191 brouard 10500: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 10501: #endif
10502: #if defined(__PGI)
1.191 brouard 10503: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 10504: #endif
10505: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 10506: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 10507: #endif
1.191 brouard 10508: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 10509:
1.167 brouard 10510: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
10511: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
10512: // Windows (x64 and x86)
1.191 brouard 10513: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 10514: #elif __unix__ // all unices, not all compilers
10515: // Unix
1.191 brouard 10516: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 10517: #elif __linux__
10518: // linux
1.191 brouard 10519: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 10520: #elif __APPLE__
1.174 brouard 10521: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 10522: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 10523: #endif
10524:
10525: /* __MINGW32__ */
10526: /* __CYGWIN__ */
10527: /* __MINGW64__ */
10528: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
10529: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
10530: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
10531: /* _WIN64 // Defined for applications for Win64. */
10532: /* _M_X64 // Defined for compilations that target x64 processors. */
10533: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 10534:
1.167 brouard 10535: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 10536: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 10537: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 10538: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 10539: #else
1.191 brouard 10540: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 10541: #endif
10542:
1.169 brouard 10543: #if defined(__GNUC__)
10544: # if defined(__GNUC_PATCHLEVEL__)
10545: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10546: + __GNUC_MINOR__ * 100 \
10547: + __GNUC_PATCHLEVEL__)
10548: # else
10549: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10550: + __GNUC_MINOR__ * 100)
10551: # endif
1.174 brouard 10552: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 10553: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 10554:
10555: if (uname(&sysInfo) != -1) {
10556: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 10557: 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 10558: }
10559: else
10560: perror("uname() error");
1.179 brouard 10561: //#ifndef __INTEL_COMPILER
10562: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 10563: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 10564: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 10565: #endif
1.169 brouard 10566: #endif
1.172 brouard 10567:
1.286 brouard 10568: // void main ()
1.172 brouard 10569: // {
1.169 brouard 10570: #if defined(_MSC_VER)
1.174 brouard 10571: if (IsWow64()){
1.191 brouard 10572: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
10573: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 10574: }
10575: else{
1.191 brouard 10576: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
10577: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 10578: }
1.172 brouard 10579: // printf("\nPress Enter to continue...");
10580: // getchar();
10581: // }
10582:
1.169 brouard 10583: #endif
10584:
1.167 brouard 10585:
1.219 brouard 10586: }
1.136 brouard 10587:
1.219 brouard 10588: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.288 brouard 10589: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
1.235 brouard 10590: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 10591: /* double ftolpl = 1.e-10; */
1.180 brouard 10592: double age, agebase, agelim;
1.203 brouard 10593: double tot;
1.180 brouard 10594:
1.202 brouard 10595: strcpy(filerespl,"PL_");
10596: strcat(filerespl,fileresu);
10597: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.288 brouard 10598: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
10599: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
1.202 brouard 10600: }
1.288 brouard 10601: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
10602: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 10603: pstamp(ficrespl);
1.288 brouard 10604: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 10605: fprintf(ficrespl,"#Age ");
10606: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
10607: fprintf(ficrespl,"\n");
1.180 brouard 10608:
1.219 brouard 10609: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 10610:
1.219 brouard 10611: agebase=ageminpar;
10612: agelim=agemaxpar;
1.180 brouard 10613:
1.227 brouard 10614: /* i1=pow(2,ncoveff); */
1.234 brouard 10615: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 10616: if (cptcovn < 1){i1=1;}
1.180 brouard 10617:
1.238 brouard 10618: for(k=1; k<=i1;k++){ /* For each combination k of dummy covariates in the model */
10619: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 10620: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10621: continue;
1.235 brouard 10622:
1.238 brouard 10623: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10624: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
10625: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
10626: /* k=k+1; */
10627: /* to clean */
10628: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
10629: fprintf(ficrespl,"#******");
10630: printf("#******");
10631: fprintf(ficlog,"#******");
10632: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
10633: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
10634: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10635: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10636: }
10637: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
10638: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10639: fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10640: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10641: }
10642: fprintf(ficrespl,"******\n");
10643: printf("******\n");
10644: fprintf(ficlog,"******\n");
10645: if(invalidvarcomb[k]){
10646: printf("\nCombination (%d) ignored because no case \n",k);
10647: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
10648: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
10649: continue;
10650: }
1.219 brouard 10651:
1.238 brouard 10652: fprintf(ficrespl,"#Age ");
10653: for(j=1;j<=cptcoveff;j++) {
10654: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10655: }
10656: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
10657: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 10658:
1.238 brouard 10659: for (age=agebase; age<=agelim; age++){
10660: /* for (age=agebase; age<=agebase; age++){ */
10661: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres);
10662: fprintf(ficrespl,"%.0f ",age );
10663: for(j=1;j<=cptcoveff;j++)
10664: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10665: tot=0.;
10666: for(i=1; i<=nlstate;i++){
10667: tot += prlim[i][i];
10668: fprintf(ficrespl," %.5f", prlim[i][i]);
10669: }
10670: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
10671: } /* Age */
10672: /* was end of cptcod */
10673: } /* cptcov */
10674: } /* nres */
1.219 brouard 10675: return 0;
1.180 brouard 10676: }
10677:
1.218 brouard 10678: 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 10679: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
1.218 brouard 10680:
10681: /* Computes the back prevalence limit for any combination of covariate values
10682: * at any age between ageminpar and agemaxpar
10683: */
1.235 brouard 10684: int i, j, k, i1, nres=0 ;
1.217 brouard 10685: /* double ftolpl = 1.e-10; */
10686: double age, agebase, agelim;
10687: double tot;
1.218 brouard 10688: /* double ***mobaverage; */
10689: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 10690:
10691: strcpy(fileresplb,"PLB_");
10692: strcat(fileresplb,fileresu);
10693: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
1.288 brouard 10694: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
10695: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
1.217 brouard 10696: }
1.288 brouard 10697: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
10698: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
1.217 brouard 10699: pstamp(ficresplb);
1.288 brouard 10700: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.217 brouard 10701: fprintf(ficresplb,"#Age ");
10702: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
10703: fprintf(ficresplb,"\n");
10704:
1.218 brouard 10705:
10706: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
10707:
10708: agebase=ageminpar;
10709: agelim=agemaxpar;
10710:
10711:
1.227 brouard 10712: i1=pow(2,cptcoveff);
1.218 brouard 10713: if (cptcovn < 1){i1=1;}
1.227 brouard 10714:
1.238 brouard 10715: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10716: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 10717: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10718: continue;
10719: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
10720: fprintf(ficresplb,"#******");
10721: printf("#******");
10722: fprintf(ficlog,"#******");
10723: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
10724: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10725: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10726: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10727: }
10728: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
10729: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10730: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10731: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10732: }
10733: fprintf(ficresplb,"******\n");
10734: printf("******\n");
10735: fprintf(ficlog,"******\n");
10736: if(invalidvarcomb[k]){
10737: printf("\nCombination (%d) ignored because no cases \n",k);
10738: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
10739: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
10740: continue;
10741: }
1.218 brouard 10742:
1.238 brouard 10743: fprintf(ficresplb,"#Age ");
10744: for(j=1;j<=cptcoveff;j++) {
10745: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10746: }
10747: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
10748: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 10749:
10750:
1.238 brouard 10751: for (age=agebase; age<=agelim; age++){
10752: /* for (age=agebase; age<=agebase; age++){ */
10753: if(mobilavproj > 0){
10754: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
10755: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 10756: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 10757: }else if (mobilavproj == 0){
10758: 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);
10759: 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);
10760: exit(1);
10761: }else{
10762: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 10763: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.266 brouard 10764: /* printf("TOTOT\n"); */
10765: /* exit(1); */
1.238 brouard 10766: }
10767: fprintf(ficresplb,"%.0f ",age );
10768: for(j=1;j<=cptcoveff;j++)
10769: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10770: tot=0.;
10771: for(i=1; i<=nlstate;i++){
10772: tot += bprlim[i][i];
10773: fprintf(ficresplb," %.5f", bprlim[i][i]);
10774: }
10775: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
10776: } /* Age */
10777: /* was end of cptcod */
1.255 brouard 10778: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
1.238 brouard 10779: } /* end of any combination */
10780: } /* end of nres */
1.218 brouard 10781: /* hBijx(p, bage, fage); */
10782: /* fclose(ficrespijb); */
10783:
10784: return 0;
1.217 brouard 10785: }
1.218 brouard 10786:
1.180 brouard 10787: int hPijx(double *p, int bage, int fage){
10788: /*------------- h Pij x at various ages ------------*/
10789:
10790: int stepsize;
10791: int agelim;
10792: int hstepm;
10793: int nhstepm;
1.235 brouard 10794: int h, i, i1, j, k, k4, nres=0;
1.180 brouard 10795:
10796: double agedeb;
10797: double ***p3mat;
10798:
1.201 brouard 10799: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 10800: if((ficrespij=fopen(filerespij,"w"))==NULL) {
10801: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
10802: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
10803: }
10804: printf("Computing pij: result on file '%s' \n", filerespij);
10805: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
10806:
10807: stepsize=(int) (stepm+YEARM-1)/YEARM;
10808: /*if (stepm<=24) stepsize=2;*/
10809:
10810: agelim=AGESUP;
10811: hstepm=stepsize*YEARM; /* Every year of age */
10812: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 10813:
1.180 brouard 10814: /* hstepm=1; aff par mois*/
10815: pstamp(ficrespij);
10816: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 10817: i1= pow(2,cptcoveff);
1.218 brouard 10818: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10819: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
10820: /* k=k+1; */
1.235 brouard 10821: for(nres=1; nres <= nresult; nres++) /* For each resultline */
10822: for(k=1; k<=i1;k++){
1.253 brouard 10823: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 10824: continue;
1.183 brouard 10825: fprintf(ficrespij,"\n#****** ");
1.227 brouard 10826: for(j=1;j<=cptcoveff;j++)
1.198 brouard 10827: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 10828: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
10829: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10830: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10831: }
1.183 brouard 10832: fprintf(ficrespij,"******\n");
10833:
10834: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
10835: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
10836: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
10837:
10838: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 10839:
1.183 brouard 10840: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10841: oldm=oldms;savm=savms;
1.235 brouard 10842: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.183 brouard 10843: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
10844: for(i=1; i<=nlstate;i++)
10845: for(j=1; j<=nlstate+ndeath;j++)
10846: fprintf(ficrespij," %1d-%1d",i,j);
10847: fprintf(ficrespij,"\n");
10848: for (h=0; h<=nhstepm; h++){
10849: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
10850: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 10851: for(i=1; i<=nlstate;i++)
10852: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 10853: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 10854: fprintf(ficrespij,"\n");
10855: }
1.183 brouard 10856: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10857: fprintf(ficrespij,"\n");
10858: }
1.180 brouard 10859: /*}*/
10860: }
1.218 brouard 10861: return 0;
1.180 brouard 10862: }
1.218 brouard 10863:
10864: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 10865: /*------------- h Bij x at various ages ------------*/
10866:
10867: int stepsize;
1.218 brouard 10868: /* int agelim; */
10869: int ageminl;
1.217 brouard 10870: int hstepm;
10871: int nhstepm;
1.238 brouard 10872: int h, i, i1, j, k, nres;
1.218 brouard 10873:
1.217 brouard 10874: double agedeb;
10875: double ***p3mat;
1.218 brouard 10876:
10877: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
10878: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
10879: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
10880: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
10881: }
10882: printf("Computing pij back: result on file '%s' \n", filerespijb);
10883: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
10884:
10885: stepsize=(int) (stepm+YEARM-1)/YEARM;
10886: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 10887:
1.218 brouard 10888: /* agelim=AGESUP; */
1.289 brouard 10889: ageminl=AGEINF; /* was 30 */
1.218 brouard 10890: hstepm=stepsize*YEARM; /* Every year of age */
10891: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
10892:
10893: /* hstepm=1; aff par mois*/
10894: pstamp(ficrespijb);
1.255 brouard 10895: 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 10896: i1= pow(2,cptcoveff);
1.218 brouard 10897: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10898: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
10899: /* k=k+1; */
1.238 brouard 10900: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10901: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 10902: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10903: continue;
10904: fprintf(ficrespijb,"\n#****** ");
10905: for(j=1;j<=cptcoveff;j++)
10906: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10907: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
10908: fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10909: }
10910: fprintf(ficrespijb,"******\n");
1.264 brouard 10911: if(invalidvarcomb[k]){ /* Is it necessary here? */
1.238 brouard 10912: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
10913: continue;
10914: }
10915:
10916: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
10917: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
10918: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
1.297 brouard 10919: 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 */
10920: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
1.238 brouard 10921:
10922: /* nhstepm=nhstepm*YEARM; aff par mois*/
10923:
1.266 brouard 10924: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
10925: /* and memory limitations if stepm is small */
10926:
1.238 brouard 10927: /* oldm=oldms;savm=savms; */
10928: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.267 brouard 10929: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.238 brouard 10930: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
1.255 brouard 10931: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
1.217 brouard 10932: for(i=1; i<=nlstate;i++)
10933: for(j=1; j<=nlstate+ndeath;j++)
1.238 brouard 10934: fprintf(ficrespijb," %1d-%1d",i,j);
1.217 brouard 10935: fprintf(ficrespijb,"\n");
1.238 brouard 10936: for (h=0; h<=nhstepm; h++){
10937: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
10938: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
10939: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
10940: for(i=1; i<=nlstate;i++)
10941: for(j=1; j<=nlstate+ndeath;j++)
10942: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
10943: fprintf(ficrespijb,"\n");
10944: }
10945: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10946: fprintf(ficrespijb,"\n");
10947: } /* end age deb */
10948: } /* end combination */
10949: } /* end nres */
1.218 brouard 10950: return 0;
10951: } /* hBijx */
1.217 brouard 10952:
1.180 brouard 10953:
1.136 brouard 10954: /***********************************************/
10955: /**************** Main Program *****************/
10956: /***********************************************/
10957:
10958: int main(int argc, char *argv[])
10959: {
10960: #ifdef GSL
10961: const gsl_multimin_fminimizer_type *T;
10962: size_t iteri = 0, it;
10963: int rval = GSL_CONTINUE;
10964: int status = GSL_SUCCESS;
10965: double ssval;
10966: #endif
10967: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.290 brouard 10968: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
10969: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
1.209 brouard 10970: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 10971: int jj, ll, li, lj, lk;
1.136 brouard 10972: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 10973: int num_filled;
1.136 brouard 10974: int itimes;
10975: int NDIM=2;
10976: int vpopbased=0;
1.235 brouard 10977: int nres=0;
1.258 brouard 10978: int endishere=0;
1.277 brouard 10979: int noffset=0;
1.274 brouard 10980: int ncurrv=0; /* Temporary variable */
10981:
1.164 brouard 10982: char ca[32], cb[32];
1.136 brouard 10983: /* FILE *fichtm; *//* Html File */
10984: /* FILE *ficgp;*/ /*Gnuplot File */
10985: struct stat info;
1.191 brouard 10986: double agedeb=0.;
1.194 brouard 10987:
10988: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 10989: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 10990:
1.165 brouard 10991: double fret;
1.191 brouard 10992: double dum=0.; /* Dummy variable */
1.136 brouard 10993: double ***p3mat;
1.218 brouard 10994: /* double ***mobaverage; */
1.164 brouard 10995:
10996: char line[MAXLINE];
1.197 brouard 10997: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
10998:
1.234 brouard 10999: char modeltemp[MAXLINE];
1.230 brouard 11000: char resultline[MAXLINE];
11001:
1.136 brouard 11002: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 11003: char *tok, *val; /* pathtot */
1.290 brouard 11004: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs declared globally ;*/
1.195 brouard 11005: int c, h , cpt, c2;
1.191 brouard 11006: int jl=0;
11007: int i1, j1, jk, stepsize=0;
1.194 brouard 11008: int count=0;
11009:
1.164 brouard 11010: int *tab;
1.136 brouard 11011: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.296 brouard 11012: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
11013: /* double anprojf, mprojf, jprojf; */
11014: /* double jintmean,mintmean,aintmean; */
11015: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
11016: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
11017: double yrfproj= 10.0; /* Number of years of forward projections */
11018: double yrbproj= 10.0; /* Number of years of backward projections */
11019: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
1.136 brouard 11020: int mobilav=0,popforecast=0;
1.191 brouard 11021: int hstepm=0, nhstepm=0;
1.136 brouard 11022: int agemortsup;
11023: float sumlpop=0.;
11024: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
11025: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
11026:
1.191 brouard 11027: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 11028: double ftolpl=FTOL;
11029: double **prlim;
1.217 brouard 11030: double **bprlim;
1.136 brouard 11031: double ***param; /* Matrix of parameters */
1.251 brouard 11032: double ***paramstart; /* Matrix of starting parameter values */
11033: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
1.136 brouard 11034: double **matcov; /* Matrix of covariance */
1.203 brouard 11035: double **hess; /* Hessian matrix */
1.136 brouard 11036: double ***delti3; /* Scale */
11037: double *delti; /* Scale */
11038: double ***eij, ***vareij;
11039: double **varpl; /* Variances of prevalence limits by age */
1.269 brouard 11040:
1.136 brouard 11041: double *epj, vepp;
1.164 brouard 11042:
1.273 brouard 11043: double dateprev1, dateprev2;
1.296 brouard 11044: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
11045: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
11046:
1.217 brouard 11047:
1.136 brouard 11048: double **ximort;
1.145 brouard 11049: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 11050: int *dcwave;
11051:
1.164 brouard 11052: char z[1]="c";
1.136 brouard 11053:
11054: /*char *strt;*/
11055: char strtend[80];
1.126 brouard 11056:
1.164 brouard 11057:
1.126 brouard 11058: /* setlocale (LC_ALL, ""); */
11059: /* bindtextdomain (PACKAGE, LOCALEDIR); */
11060: /* textdomain (PACKAGE); */
11061: /* setlocale (LC_CTYPE, ""); */
11062: /* setlocale (LC_MESSAGES, ""); */
11063:
11064: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 11065: rstart_time = time(NULL);
11066: /* (void) gettimeofday(&start_time,&tzp);*/
11067: start_time = *localtime(&rstart_time);
1.126 brouard 11068: curr_time=start_time;
1.157 brouard 11069: /*tml = *localtime(&start_time.tm_sec);*/
11070: /* strcpy(strstart,asctime(&tml)); */
11071: strcpy(strstart,asctime(&start_time));
1.126 brouard 11072:
11073: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 11074: /* tp.tm_sec = tp.tm_sec +86400; */
11075: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 11076: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
11077: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
11078: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 11079: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 11080: /* strt=asctime(&tmg); */
11081: /* printf("Time(after) =%s",strstart); */
11082: /* (void) time (&time_value);
11083: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
11084: * tm = *localtime(&time_value);
11085: * strstart=asctime(&tm);
11086: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
11087: */
11088:
11089: nberr=0; /* Number of errors and warnings */
11090: nbwarn=0;
1.184 brouard 11091: #ifdef WIN32
11092: _getcwd(pathcd, size);
11093: #else
1.126 brouard 11094: getcwd(pathcd, size);
1.184 brouard 11095: #endif
1.191 brouard 11096: syscompilerinfo(0);
1.196 brouard 11097: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 11098: if(argc <=1){
11099: printf("\nEnter the parameter file name: ");
1.205 brouard 11100: if(!fgets(pathr,FILENAMELENGTH,stdin)){
11101: printf("ERROR Empty parameter file name\n");
11102: goto end;
11103: }
1.126 brouard 11104: i=strlen(pathr);
11105: if(pathr[i-1]=='\n')
11106: pathr[i-1]='\0';
1.156 brouard 11107: i=strlen(pathr);
1.205 brouard 11108: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 11109: pathr[i-1]='\0';
1.205 brouard 11110: }
11111: i=strlen(pathr);
11112: if( i==0 ){
11113: printf("ERROR Empty parameter file name\n");
11114: goto end;
11115: }
11116: for (tok = pathr; tok != NULL; ){
1.126 brouard 11117: printf("Pathr |%s|\n",pathr);
11118: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
11119: printf("val= |%s| pathr=%s\n",val,pathr);
11120: strcpy (pathtot, val);
11121: if(pathr[0] == '\0') break; /* Dirty */
11122: }
11123: }
1.281 brouard 11124: else if (argc<=2){
11125: strcpy(pathtot,argv[1]);
11126: }
1.126 brouard 11127: else{
11128: strcpy(pathtot,argv[1]);
1.281 brouard 11129: strcpy(z,argv[2]);
11130: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
1.126 brouard 11131: }
11132: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
11133: /*cygwin_split_path(pathtot,path,optionfile);
11134: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
11135: /* cutv(path,optionfile,pathtot,'\\');*/
11136:
11137: /* Split argv[0], imach program to get pathimach */
11138: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
11139: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11140: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11141: /* strcpy(pathimach,argv[0]); */
11142: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
11143: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
11144: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 11145: #ifdef WIN32
11146: _chdir(path); /* Can be a relative path */
11147: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
11148: #else
1.126 brouard 11149: chdir(path); /* Can be a relative path */
1.184 brouard 11150: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
11151: #endif
11152: printf("Current directory %s!\n",pathcd);
1.126 brouard 11153: strcpy(command,"mkdir ");
11154: strcat(command,optionfilefiname);
11155: if((outcmd=system(command)) != 0){
1.169 brouard 11156: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 11157: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
11158: /* fclose(ficlog); */
11159: /* exit(1); */
11160: }
11161: /* if((imk=mkdir(optionfilefiname))<0){ */
11162: /* perror("mkdir"); */
11163: /* } */
11164:
11165: /*-------- arguments in the command line --------*/
11166:
1.186 brouard 11167: /* Main Log file */
1.126 brouard 11168: strcat(filelog, optionfilefiname);
11169: strcat(filelog,".log"); /* */
11170: if((ficlog=fopen(filelog,"w"))==NULL) {
11171: printf("Problem with logfile %s\n",filelog);
11172: goto end;
11173: }
11174: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 11175: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 11176: fprintf(ficlog,"\nEnter the parameter file name: \n");
11177: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
11178: path=%s \n\
11179: optionfile=%s\n\
11180: optionfilext=%s\n\
1.156 brouard 11181: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 11182:
1.197 brouard 11183: syscompilerinfo(1);
1.167 brouard 11184:
1.126 brouard 11185: printf("Local time (at start):%s",strstart);
11186: fprintf(ficlog,"Local time (at start): %s",strstart);
11187: fflush(ficlog);
11188: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 11189: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 11190:
11191: /* */
11192: strcpy(fileres,"r");
11193: strcat(fileres, optionfilefiname);
1.201 brouard 11194: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 11195: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 11196: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 11197:
1.186 brouard 11198: /* Main ---------arguments file --------*/
1.126 brouard 11199:
11200: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 11201: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
11202: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 11203: fflush(ficlog);
1.149 brouard 11204: /* goto end; */
11205: exit(70);
1.126 brouard 11206: }
11207:
11208: strcpy(filereso,"o");
1.201 brouard 11209: strcat(filereso,fileresu);
1.126 brouard 11210: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
11211: printf("Problem with Output resultfile: %s\n", filereso);
11212: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
11213: fflush(ficlog);
11214: goto end;
11215: }
1.278 brouard 11216: /*-------- Rewriting parameter file ----------*/
11217: strcpy(rfileres,"r"); /* "Rparameterfile */
11218: strcat(rfileres,optionfilefiname); /* Parameter file first name */
11219: strcat(rfileres,"."); /* */
11220: strcat(rfileres,optionfilext); /* Other files have txt extension */
11221: if((ficres =fopen(rfileres,"w"))==NULL) {
11222: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
11223: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
11224: fflush(ficlog);
11225: goto end;
11226: }
11227: fprintf(ficres,"#IMaCh %s\n",version);
1.126 brouard 11228:
1.278 brouard 11229:
1.126 brouard 11230: /* Reads comments: lines beginning with '#' */
11231: numlinepar=0;
1.277 brouard 11232: /* Is it a BOM UTF-8 Windows file? */
11233: /* First parameter line */
1.197 brouard 11234: while(fgets(line, MAXLINE, ficpar)) {
1.277 brouard 11235: noffset=0;
11236: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
11237: {
11238: noffset=noffset+3;
11239: printf("# File is an UTF8 Bom.\n"); // 0xBF
11240: }
1.302 brouard 11241: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
11242: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
1.277 brouard 11243: {
11244: noffset=noffset+2;
11245: printf("# File is an UTF16BE BOM file\n");
11246: }
11247: else if( line[0] == 0 && line[1] == 0)
11248: {
11249: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
11250: noffset=noffset+4;
11251: printf("# File is an UTF16BE BOM file\n");
11252: }
11253: } else{
11254: ;/*printf(" Not a BOM file\n");*/
11255: }
11256:
1.197 brouard 11257: /* If line starts with a # it is a comment */
1.277 brouard 11258: if (line[noffset] == '#') {
1.197 brouard 11259: numlinepar++;
11260: fputs(line,stdout);
11261: fputs(line,ficparo);
1.278 brouard 11262: fputs(line,ficres);
1.197 brouard 11263: fputs(line,ficlog);
11264: continue;
11265: }else
11266: break;
11267: }
11268: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
11269: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
11270: if (num_filled != 5) {
11271: printf("Should be 5 parameters\n");
1.283 brouard 11272: fprintf(ficlog,"Should be 5 parameters\n");
1.197 brouard 11273: }
1.126 brouard 11274: numlinepar++;
1.197 brouard 11275: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.283 brouard 11276: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11277: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11278: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.197 brouard 11279: }
11280: /* Second parameter line */
11281: while(fgets(line, MAXLINE, ficpar)) {
1.283 brouard 11282: /* while(fscanf(ficpar,"%[^\n]", line)) { */
11283: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
1.197 brouard 11284: if (line[0] == '#') {
11285: numlinepar++;
1.283 brouard 11286: printf("%s",line);
11287: fprintf(ficres,"%s",line);
11288: fprintf(ficparo,"%s",line);
11289: fprintf(ficlog,"%s",line);
1.197 brouard 11290: continue;
11291: }else
11292: break;
11293: }
1.223 brouard 11294: 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", \
11295: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
11296: if (num_filled != 11) {
11297: 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 11298: printf("but line=%s\n",line);
1.283 brouard 11299: 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");
11300: fprintf(ficlog,"but line=%s\n",line);
1.197 brouard 11301: }
1.286 brouard 11302: if( lastpass > maxwav){
11303: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11304: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11305: fflush(ficlog);
11306: goto end;
11307: }
11308: 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 11309: 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 11310: 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 11311: 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 11312: }
1.203 brouard 11313: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 11314: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 11315: /* Third parameter line */
11316: while(fgets(line, MAXLINE, ficpar)) {
11317: /* If line starts with a # it is a comment */
11318: if (line[0] == '#') {
11319: numlinepar++;
1.283 brouard 11320: printf("%s",line);
11321: fprintf(ficres,"%s",line);
11322: fprintf(ficparo,"%s",line);
11323: fprintf(ficlog,"%s",line);
1.197 brouard 11324: continue;
11325: }else
11326: break;
11327: }
1.201 brouard 11328: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
1.279 brouard 11329: if (num_filled != 1){
1.302 brouard 11330: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
11331: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
1.197 brouard 11332: model[0]='\0';
11333: goto end;
11334: }
11335: else{
11336: if (model[0]=='+'){
11337: for(i=1; i<=strlen(model);i++)
11338: modeltemp[i-1]=model[i];
1.201 brouard 11339: strcpy(model,modeltemp);
1.197 brouard 11340: }
11341: }
1.199 brouard 11342: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 11343: printf("model=1+age+%s\n",model);fflush(stdout);
1.283 brouard 11344: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
11345: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
11346: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 11347: }
11348: /* 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); */
11349: /* numlinepar=numlinepar+3; /\* In general *\/ */
11350: /* 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 11351: /* 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); */
11352: /* 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 11353: fflush(ficlog);
1.190 brouard 11354: /* if(model[0]=='#'|| model[0]== '\0'){ */
11355: if(model[0]=='#'){
1.279 brouard 11356: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
11357: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
11358: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
1.187 brouard 11359: if(mle != -1){
1.279 brouard 11360: 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 11361: exit(1);
11362: }
11363: }
1.126 brouard 11364: while((c=getc(ficpar))=='#' && c!= EOF){
11365: ungetc(c,ficpar);
11366: fgets(line, MAXLINE, ficpar);
11367: numlinepar++;
1.195 brouard 11368: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
11369: z[0]=line[1];
11370: }
11371: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 11372: fputs(line, stdout);
11373: //puts(line);
1.126 brouard 11374: fputs(line,ficparo);
11375: fputs(line,ficlog);
11376: }
11377: ungetc(c,ficpar);
11378:
11379:
1.290 brouard 11380: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
11381: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
11382: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
11383: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /**< Time varying covariate (dummy and quantitative)*/
1.136 brouard 11384: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
11385: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
11386: v1+v2*age+v2*v3 makes cptcovn = 3
11387: */
11388: if (strlen(model)>1)
1.187 brouard 11389: 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 11390: else
1.187 brouard 11391: ncovmodel=2; /* Constant and age */
1.133 brouard 11392: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
11393: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 11394: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
11395: 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);
11396: 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);
11397: fflush(stdout);
11398: fclose (ficlog);
11399: goto end;
11400: }
1.126 brouard 11401: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11402: delti=delti3[1][1];
11403: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
11404: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
1.247 brouard 11405: /* We could also provide initial parameters values giving by simple logistic regression
11406: * only one way, that is without matrix product. We will have nlstate maximizations */
11407: /* for(i=1;i<nlstate;i++){ */
11408: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
11409: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
11410: /* } */
1.126 brouard 11411: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 11412: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
11413: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11414: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11415: fclose (ficparo);
11416: fclose (ficlog);
11417: goto end;
11418: exit(0);
1.220 brouard 11419: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 11420: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 11421: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
11422: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11423: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11424: matcov=matrix(1,npar,1,npar);
1.203 brouard 11425: hess=matrix(1,npar,1,npar);
1.220 brouard 11426: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 11427: /* Read guessed parameters */
1.126 brouard 11428: /* Reads comments: lines beginning with '#' */
11429: while((c=getc(ficpar))=='#' && c!= EOF){
11430: ungetc(c,ficpar);
11431: fgets(line, MAXLINE, ficpar);
11432: numlinepar++;
1.141 brouard 11433: fputs(line,stdout);
1.126 brouard 11434: fputs(line,ficparo);
11435: fputs(line,ficlog);
11436: }
11437: ungetc(c,ficpar);
11438:
11439: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.251 brouard 11440: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.126 brouard 11441: for(i=1; i <=nlstate; i++){
1.234 brouard 11442: j=0;
1.126 brouard 11443: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 11444: if(jj==i) continue;
11445: j++;
1.292 brouard 11446: while((c=getc(ficpar))=='#' && c!= EOF){
11447: ungetc(c,ficpar);
11448: fgets(line, MAXLINE, ficpar);
11449: numlinepar++;
11450: fputs(line,stdout);
11451: fputs(line,ficparo);
11452: fputs(line,ficlog);
11453: }
11454: ungetc(c,ficpar);
1.234 brouard 11455: fscanf(ficpar,"%1d%1d",&i1,&j1);
11456: if ((i1 != i) || (j1 != jj)){
11457: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 11458: It might be a problem of design; if ncovcol and the model are correct\n \
11459: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 11460: exit(1);
11461: }
11462: fprintf(ficparo,"%1d%1d",i1,j1);
11463: if(mle==1)
11464: printf("%1d%1d",i,jj);
11465: fprintf(ficlog,"%1d%1d",i,jj);
11466: for(k=1; k<=ncovmodel;k++){
11467: fscanf(ficpar," %lf",¶m[i][j][k]);
11468: if(mle==1){
11469: printf(" %lf",param[i][j][k]);
11470: fprintf(ficlog," %lf",param[i][j][k]);
11471: }
11472: else
11473: fprintf(ficlog," %lf",param[i][j][k]);
11474: fprintf(ficparo," %lf",param[i][j][k]);
11475: }
11476: fscanf(ficpar,"\n");
11477: numlinepar++;
11478: if(mle==1)
11479: printf("\n");
11480: fprintf(ficlog,"\n");
11481: fprintf(ficparo,"\n");
1.126 brouard 11482: }
11483: }
11484: fflush(ficlog);
1.234 brouard 11485:
1.251 brouard 11486: /* Reads parameters values */
1.126 brouard 11487: p=param[1][1];
1.251 brouard 11488: pstart=paramstart[1][1];
1.126 brouard 11489:
11490: /* Reads comments: lines beginning with '#' */
11491: while((c=getc(ficpar))=='#' && c!= EOF){
11492: ungetc(c,ficpar);
11493: fgets(line, MAXLINE, ficpar);
11494: numlinepar++;
1.141 brouard 11495: fputs(line,stdout);
1.126 brouard 11496: fputs(line,ficparo);
11497: fputs(line,ficlog);
11498: }
11499: ungetc(c,ficpar);
11500:
11501: for(i=1; i <=nlstate; i++){
11502: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 11503: fscanf(ficpar,"%1d%1d",&i1,&j1);
11504: if ( (i1-i) * (j1-j) != 0){
11505: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
11506: exit(1);
11507: }
11508: printf("%1d%1d",i,j);
11509: fprintf(ficparo,"%1d%1d",i1,j1);
11510: fprintf(ficlog,"%1d%1d",i1,j1);
11511: for(k=1; k<=ncovmodel;k++){
11512: fscanf(ficpar,"%le",&delti3[i][j][k]);
11513: printf(" %le",delti3[i][j][k]);
11514: fprintf(ficparo," %le",delti3[i][j][k]);
11515: fprintf(ficlog," %le",delti3[i][j][k]);
11516: }
11517: fscanf(ficpar,"\n");
11518: numlinepar++;
11519: printf("\n");
11520: fprintf(ficparo,"\n");
11521: fprintf(ficlog,"\n");
1.126 brouard 11522: }
11523: }
11524: fflush(ficlog);
1.234 brouard 11525:
1.145 brouard 11526: /* Reads covariance matrix */
1.126 brouard 11527: delti=delti3[1][1];
1.220 brouard 11528:
11529:
1.126 brouard 11530: /* 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 11531:
1.126 brouard 11532: /* Reads comments: lines beginning with '#' */
11533: while((c=getc(ficpar))=='#' && c!= EOF){
11534: ungetc(c,ficpar);
11535: fgets(line, MAXLINE, ficpar);
11536: numlinepar++;
1.141 brouard 11537: fputs(line,stdout);
1.126 brouard 11538: fputs(line,ficparo);
11539: fputs(line,ficlog);
11540: }
11541: ungetc(c,ficpar);
1.220 brouard 11542:
1.126 brouard 11543: matcov=matrix(1,npar,1,npar);
1.203 brouard 11544: hess=matrix(1,npar,1,npar);
1.131 brouard 11545: for(i=1; i <=npar; i++)
11546: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 11547:
1.194 brouard 11548: /* Scans npar lines */
1.126 brouard 11549: for(i=1; i <=npar; i++){
1.226 brouard 11550: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 11551: if(count != 3){
1.226 brouard 11552: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11553: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11554: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11555: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11556: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11557: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11558: exit(1);
1.220 brouard 11559: }else{
1.226 brouard 11560: if(mle==1)
11561: printf("%1d%1d%d",i1,j1,jk);
11562: }
11563: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
11564: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 11565: for(j=1; j <=i; j++){
1.226 brouard 11566: fscanf(ficpar," %le",&matcov[i][j]);
11567: if(mle==1){
11568: printf(" %.5le",matcov[i][j]);
11569: }
11570: fprintf(ficlog," %.5le",matcov[i][j]);
11571: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 11572: }
11573: fscanf(ficpar,"\n");
11574: numlinepar++;
11575: if(mle==1)
1.220 brouard 11576: printf("\n");
1.126 brouard 11577: fprintf(ficlog,"\n");
11578: fprintf(ficparo,"\n");
11579: }
1.194 brouard 11580: /* End of read covariance matrix npar lines */
1.126 brouard 11581: for(i=1; i <=npar; i++)
11582: for(j=i+1;j<=npar;j++)
1.226 brouard 11583: matcov[i][j]=matcov[j][i];
1.126 brouard 11584:
11585: if(mle==1)
11586: printf("\n");
11587: fprintf(ficlog,"\n");
11588:
11589: fflush(ficlog);
11590:
11591: } /* End of mle != -3 */
1.218 brouard 11592:
1.186 brouard 11593: /* Main data
11594: */
1.290 brouard 11595: nobs=lastobs-firstobs+1; /* was = lastobs;*/
11596: /* num=lvector(1,n); */
11597: /* moisnais=vector(1,n); */
11598: /* annais=vector(1,n); */
11599: /* moisdc=vector(1,n); */
11600: /* andc=vector(1,n); */
11601: /* weight=vector(1,n); */
11602: /* agedc=vector(1,n); */
11603: /* cod=ivector(1,n); */
11604: /* for(i=1;i<=n;i++){ */
11605: num=lvector(firstobs,lastobs);
11606: moisnais=vector(firstobs,lastobs);
11607: annais=vector(firstobs,lastobs);
11608: moisdc=vector(firstobs,lastobs);
11609: andc=vector(firstobs,lastobs);
11610: weight=vector(firstobs,lastobs);
11611: agedc=vector(firstobs,lastobs);
11612: cod=ivector(firstobs,lastobs);
11613: for(i=firstobs;i<=lastobs;i++){
1.234 brouard 11614: num[i]=0;
11615: moisnais[i]=0;
11616: annais[i]=0;
11617: moisdc[i]=0;
11618: andc[i]=0;
11619: agedc[i]=0;
11620: cod[i]=0;
11621: weight[i]=1.0; /* Equal weights, 1 by default */
11622: }
1.290 brouard 11623: mint=matrix(1,maxwav,firstobs,lastobs);
11624: anint=matrix(1,maxwav,firstobs,lastobs);
11625: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
1.126 brouard 11626: tab=ivector(1,NCOVMAX);
1.144 brouard 11627: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 11628: 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 11629:
1.136 brouard 11630: /* Reads data from file datafile */
11631: if (readdata(datafile, firstobs, lastobs, &imx)==1)
11632: goto end;
11633:
11634: /* Calculation of the number of parameters from char model */
1.234 brouard 11635: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 11636: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
11637: k=3 V4 Tvar[k=3]= 4 (from V4)
11638: k=2 V1 Tvar[k=2]= 1 (from V1)
11639: k=1 Tvar[1]=2 (from V2)
1.234 brouard 11640: */
11641:
11642: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
11643: TvarsDind=ivector(1,NCOVMAX); /* */
11644: TvarsD=ivector(1,NCOVMAX); /* */
11645: TvarsQind=ivector(1,NCOVMAX); /* */
11646: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 11647: TvarF=ivector(1,NCOVMAX); /* */
11648: TvarFind=ivector(1,NCOVMAX); /* */
11649: TvarV=ivector(1,NCOVMAX); /* */
11650: TvarVind=ivector(1,NCOVMAX); /* */
11651: TvarA=ivector(1,NCOVMAX); /* */
11652: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 11653: TvarFD=ivector(1,NCOVMAX); /* */
11654: TvarFDind=ivector(1,NCOVMAX); /* */
11655: TvarFQ=ivector(1,NCOVMAX); /* */
11656: TvarFQind=ivector(1,NCOVMAX); /* */
11657: TvarVD=ivector(1,NCOVMAX); /* */
11658: TvarVDind=ivector(1,NCOVMAX); /* */
11659: TvarVQ=ivector(1,NCOVMAX); /* */
11660: TvarVQind=ivector(1,NCOVMAX); /* */
11661:
1.230 brouard 11662: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 11663: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 11664: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
11665: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
11666: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 11667: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
11668: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
11669: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
11670: */
11671: /* For model-covariate k tells which data-covariate to use but
11672: because this model-covariate is a construction we invent a new column
11673: ncovcol + k1
11674: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
11675: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 11676: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
11677: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 11678: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
11679: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 11680: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 11681: */
1.145 brouard 11682: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
11683: 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 11684: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
11685: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 11686: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 11687: 4 covariates (3 plus signs)
11688: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
11689: */
1.230 brouard 11690: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 11691: * individual dummy, fixed or varying:
11692: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
11693: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 11694: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
11695: * V1 df, V2 qf, V3 & V4 dv, V5 qv
11696: * Tmodelind[1]@9={9,0,3,2,}*/
11697: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
11698: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 11699: * individual quantitative, fixed or varying:
11700: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
11701: * 3, 1, 0, 0, 0, 0, 0, 0},
11702: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 11703: /* Main decodemodel */
11704:
1.187 brouard 11705:
1.223 brouard 11706: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 11707: goto end;
11708:
1.137 brouard 11709: if((double)(lastobs-imx)/(double)imx > 1.10){
11710: nbwarn++;
11711: 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);
11712: 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);
11713: }
1.136 brouard 11714: /* if(mle==1){*/
1.137 brouard 11715: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
11716: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 11717: }
11718:
11719: /*-calculation of age at interview from date of interview and age at death -*/
11720: agev=matrix(1,maxwav,1,imx);
11721:
11722: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
11723: goto end;
11724:
1.126 brouard 11725:
1.136 brouard 11726: agegomp=(int)agemin;
1.290 brouard 11727: free_vector(moisnais,firstobs,lastobs);
11728: free_vector(annais,firstobs,lastobs);
1.126 brouard 11729: /* free_matrix(mint,1,maxwav,1,n);
11730: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 11731: /* free_vector(moisdc,1,n); */
11732: /* free_vector(andc,1,n); */
1.145 brouard 11733: /* */
11734:
1.126 brouard 11735: wav=ivector(1,imx);
1.214 brouard 11736: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
11737: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
11738: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
11739: 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.*/
11740: bh=imatrix(1,lastpass-firstpass+2,1,imx);
11741: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 11742:
11743: /* Concatenates waves */
1.214 brouard 11744: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
11745: Death is a valid wave (if date is known).
11746: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
11747: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
11748: and mw[mi+1][i]. dh depends on stepm.
11749: */
11750:
1.126 brouard 11751: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.248 brouard 11752: /* Concatenates waves */
1.145 brouard 11753:
1.290 brouard 11754: free_vector(moisdc,firstobs,lastobs);
11755: free_vector(andc,firstobs,lastobs);
1.215 brouard 11756:
1.126 brouard 11757: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
11758: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
11759: ncodemax[1]=1;
1.145 brouard 11760: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 11761: cptcoveff=0;
1.220 brouard 11762: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
11763: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 11764: }
11765:
11766: ncovcombmax=pow(2,cptcoveff);
11767: invalidvarcomb=ivector(1, ncovcombmax);
11768: for(i=1;i<ncovcombmax;i++)
11769: invalidvarcomb[i]=0;
11770:
1.211 brouard 11771: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 11772: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 11773: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 11774:
1.200 brouard 11775: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 11776: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 11777: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 11778: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
11779: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
11780: * (currently 0 or 1) in the data.
11781: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
11782: * corresponding modality (h,j).
11783: */
11784:
1.145 brouard 11785: h=0;
11786: /*if (cptcovn > 0) */
1.126 brouard 11787: m=pow(2,cptcoveff);
11788:
1.144 brouard 11789: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 11790: * For k=4 covariates, h goes from 1 to m=2**k
11791: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
11792: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 11793: * h\k 1 2 3 4
1.143 brouard 11794: *______________________________
11795: * 1 i=1 1 i=1 1 i=1 1 i=1 1
11796: * 2 2 1 1 1
11797: * 3 i=2 1 2 1 1
11798: * 4 2 2 1 1
11799: * 5 i=3 1 i=2 1 2 1
11800: * 6 2 1 2 1
11801: * 7 i=4 1 2 2 1
11802: * 8 2 2 2 1
1.197 brouard 11803: * 9 i=5 1 i=3 1 i=2 1 2
11804: * 10 2 1 1 2
11805: * 11 i=6 1 2 1 2
11806: * 12 2 2 1 2
11807: * 13 i=7 1 i=4 1 2 2
11808: * 14 2 1 2 2
11809: * 15 i=8 1 2 2 2
11810: * 16 2 2 2 2
1.143 brouard 11811: */
1.212 brouard 11812: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 11813: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
11814: * and the value of each covariate?
11815: * V1=1, V2=1, V3=2, V4=1 ?
11816: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
11817: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
11818: * In order to get the real value in the data, we use nbcode
11819: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
11820: * We are keeping this crazy system in order to be able (in the future?)
11821: * to have more than 2 values (0 or 1) for a covariate.
11822: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
11823: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
11824: * bbbbbbbb
11825: * 76543210
11826: * h-1 00000101 (6-1=5)
1.219 brouard 11827: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 11828: * &
11829: * 1 00000001 (1)
1.219 brouard 11830: * 00000000 = 1 & ((h-1) >> (k-1))
11831: * +1= 00000001 =1
1.211 brouard 11832: *
11833: * h=14, k=3 => h'=h-1=13, k'=k-1=2
11834: * h' 1101 =2^3+2^2+0x2^1+2^0
11835: * >>k' 11
11836: * & 00000001
11837: * = 00000001
11838: * +1 = 00000010=2 = codtabm(14,3)
11839: * Reverse h=6 and m=16?
11840: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
11841: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
11842: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
11843: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
11844: * V3=decodtabm(14,3,2**4)=2
11845: * h'=13 1101 =2^3+2^2+0x2^1+2^0
11846: *(h-1) >> (j-1) 0011 =13 >> 2
11847: * &1 000000001
11848: * = 000000001
11849: * +1= 000000010 =2
11850: * 2211
11851: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
11852: * V3=2
1.220 brouard 11853: * codtabm and decodtabm are identical
1.211 brouard 11854: */
11855:
1.145 brouard 11856:
11857: free_ivector(Ndum,-1,NCOVMAX);
11858:
11859:
1.126 brouard 11860:
1.186 brouard 11861: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 11862: strcpy(optionfilegnuplot,optionfilefiname);
11863: if(mle==-3)
1.201 brouard 11864: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 11865: strcat(optionfilegnuplot,".gp");
11866:
11867: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
11868: printf("Problem with file %s",optionfilegnuplot);
11869: }
11870: else{
1.204 brouard 11871: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 11872: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 11873: //fprintf(ficgp,"set missing 'NaNq'\n");
11874: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 11875: }
11876: /* fclose(ficgp);*/
1.186 brouard 11877:
11878:
11879: /* Initialisation of --------- index.htm --------*/
1.126 brouard 11880:
11881: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
11882: if(mle==-3)
1.201 brouard 11883: strcat(optionfilehtm,"-MORT_");
1.126 brouard 11884: strcat(optionfilehtm,".htm");
11885: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 11886: printf("Problem with %s \n",optionfilehtm);
11887: exit(0);
1.126 brouard 11888: }
11889:
11890: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
11891: strcat(optionfilehtmcov,"-cov.htm");
11892: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
11893: printf("Problem with %s \n",optionfilehtmcov), exit(0);
11894: }
11895: else{
11896: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
11897: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 11898: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 11899: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
11900: }
11901:
1.213 brouard 11902: 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 11903: <hr size=\"2\" color=\"#EC5E5E\"> \n\
11904: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 11905: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 11906: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 11907: \n\
11908: <hr size=\"2\" color=\"#EC5E5E\">\
11909: <ul><li><h4>Parameter files</h4>\n\
11910: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
11911: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
11912: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
11913: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
11914: - Date and time at start: %s</ul>\n",\
11915: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
11916: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
11917: fileres,fileres,\
11918: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
11919: fflush(fichtm);
11920:
11921: strcpy(pathr,path);
11922: strcat(pathr,optionfilefiname);
1.184 brouard 11923: #ifdef WIN32
11924: _chdir(optionfilefiname); /* Move to directory named optionfile */
11925: #else
1.126 brouard 11926: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 11927: #endif
11928:
1.126 brouard 11929:
1.220 brouard 11930: /* Calculates basic frequencies. Computes observed prevalence at single age
11931: and for any valid combination of covariates
1.126 brouard 11932: and prints on file fileres'p'. */
1.251 brouard 11933: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.227 brouard 11934: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 11935:
11936: fprintf(fichtm,"\n");
1.286 brouard 11937: 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 11938: ftol, stepm);
11939: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
11940: ncurrv=1;
11941: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
11942: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
11943: ncurrv=i;
11944: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 11945: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
1.274 brouard 11946: ncurrv=i;
11947: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 11948: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
1.274 brouard 11949: ncurrv=i;
11950: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
11951: 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", \
11952: nlstate, ndeath, maxwav, mle, weightopt);
11953:
11954: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
11955: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
11956:
11957:
11958: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Total number of observations=%d <br>\n\
1.126 brouard 11959: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
11960: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.274 brouard 11961: imx,agemin,agemax,jmin,jmax,jmean);
1.126 brouard 11962: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.268 brouard 11963: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11964: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11965: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11966: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 11967:
1.126 brouard 11968: /* For Powell, parameters are in a vector p[] starting at p[1]
11969: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
11970: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
11971:
11972: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 11973: /* For mortality only */
1.126 brouard 11974: if (mle==-3){
1.136 brouard 11975: ximort=matrix(1,NDIM,1,NDIM);
1.248 brouard 11976: for(i=1;i<=NDIM;i++)
11977: for(j=1;j<=NDIM;j++)
11978: ximort[i][j]=0.;
1.186 brouard 11979: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.290 brouard 11980: cens=ivector(firstobs,lastobs);
11981: ageexmed=vector(firstobs,lastobs);
11982: agecens=vector(firstobs,lastobs);
11983: dcwave=ivector(firstobs,lastobs);
1.223 brouard 11984:
1.126 brouard 11985: for (i=1; i<=imx; i++){
11986: dcwave[i]=-1;
11987: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 11988: if (s[m][i]>nlstate) {
11989: dcwave[i]=m;
11990: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
11991: break;
11992: }
1.126 brouard 11993: }
1.226 brouard 11994:
1.126 brouard 11995: for (i=1; i<=imx; i++) {
11996: if (wav[i]>0){
1.226 brouard 11997: ageexmed[i]=agev[mw[1][i]][i];
11998: j=wav[i];
11999: agecens[i]=1.;
12000:
12001: if (ageexmed[i]> 1 && wav[i] > 0){
12002: agecens[i]=agev[mw[j][i]][i];
12003: cens[i]= 1;
12004: }else if (ageexmed[i]< 1)
12005: cens[i]= -1;
12006: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
12007: cens[i]=0 ;
1.126 brouard 12008: }
12009: else cens[i]=-1;
12010: }
12011:
12012: for (i=1;i<=NDIM;i++) {
12013: for (j=1;j<=NDIM;j++)
1.226 brouard 12014: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 12015: }
12016:
1.302 brouard 12017: p[1]=0.0268; p[NDIM]=0.083;
12018: /* printf("%lf %lf", p[1], p[2]); */
1.126 brouard 12019:
12020:
1.136 brouard 12021: #ifdef GSL
12022: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 12023: #else
1.126 brouard 12024: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 12025: #endif
1.201 brouard 12026: strcpy(filerespow,"POW-MORT_");
12027: strcat(filerespow,fileresu);
1.126 brouard 12028: if((ficrespow=fopen(filerespow,"w"))==NULL) {
12029: printf("Problem with resultfile: %s\n", filerespow);
12030: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
12031: }
1.136 brouard 12032: #ifdef GSL
12033: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 12034: #else
1.126 brouard 12035: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 12036: #endif
1.126 brouard 12037: /* for (i=1;i<=nlstate;i++)
12038: for(j=1;j<=nlstate+ndeath;j++)
12039: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
12040: */
12041: fprintf(ficrespow,"\n");
1.136 brouard 12042: #ifdef GSL
12043: /* gsl starts here */
12044: T = gsl_multimin_fminimizer_nmsimplex;
12045: gsl_multimin_fminimizer *sfm = NULL;
12046: gsl_vector *ss, *x;
12047: gsl_multimin_function minex_func;
12048:
12049: /* Initial vertex size vector */
12050: ss = gsl_vector_alloc (NDIM);
12051:
12052: if (ss == NULL){
12053: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
12054: }
12055: /* Set all step sizes to 1 */
12056: gsl_vector_set_all (ss, 0.001);
12057:
12058: /* Starting point */
1.126 brouard 12059:
1.136 brouard 12060: x = gsl_vector_alloc (NDIM);
12061:
12062: if (x == NULL){
12063: gsl_vector_free(ss);
12064: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
12065: }
12066:
12067: /* Initialize method and iterate */
12068: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 12069: /* gsl_vector_set(x, 0, 0.0268); */
12070: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 12071: gsl_vector_set(x, 0, p[1]);
12072: gsl_vector_set(x, 1, p[2]);
12073:
12074: minex_func.f = &gompertz_f;
12075: minex_func.n = NDIM;
12076: minex_func.params = (void *)&p; /* ??? */
12077:
12078: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
12079: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
12080:
12081: printf("Iterations beginning .....\n\n");
12082: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
12083:
12084: iteri=0;
12085: while (rval == GSL_CONTINUE){
12086: iteri++;
12087: status = gsl_multimin_fminimizer_iterate(sfm);
12088:
12089: if (status) printf("error: %s\n", gsl_strerror (status));
12090: fflush(0);
12091:
12092: if (status)
12093: break;
12094:
12095: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
12096: ssval = gsl_multimin_fminimizer_size (sfm);
12097:
12098: if (rval == GSL_SUCCESS)
12099: printf ("converged to a local maximum at\n");
12100:
12101: printf("%5d ", iteri);
12102: for (it = 0; it < NDIM; it++){
12103: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
12104: }
12105: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
12106: }
12107:
12108: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
12109:
12110: gsl_vector_free(x); /* initial values */
12111: gsl_vector_free(ss); /* inital step size */
12112: for (it=0; it<NDIM; it++){
12113: p[it+1]=gsl_vector_get(sfm->x,it);
12114: fprintf(ficrespow," %.12lf", p[it]);
12115: }
12116: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
12117: #endif
12118: #ifdef POWELL
12119: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
12120: #endif
1.126 brouard 12121: fclose(ficrespow);
12122:
1.203 brouard 12123: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 12124:
12125: for(i=1; i <=NDIM; i++)
12126: for(j=i+1;j<=NDIM;j++)
1.220 brouard 12127: matcov[i][j]=matcov[j][i];
1.126 brouard 12128:
12129: printf("\nCovariance matrix\n ");
1.203 brouard 12130: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 12131: for(i=1; i <=NDIM; i++) {
12132: for(j=1;j<=NDIM;j++){
1.220 brouard 12133: printf("%f ",matcov[i][j]);
12134: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 12135: }
1.203 brouard 12136: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 12137: }
12138:
12139: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 12140: for (i=1;i<=NDIM;i++) {
1.126 brouard 12141: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 12142: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
12143: }
1.302 brouard 12144: lsurv=vector(agegomp,AGESUP);
12145: lpop=vector(agegomp,AGESUP);
12146: tpop=vector(agegomp,AGESUP);
1.126 brouard 12147: lsurv[agegomp]=100000;
12148:
12149: for (k=agegomp;k<=AGESUP;k++) {
12150: agemortsup=k;
12151: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
12152: }
12153:
12154: for (k=agegomp;k<agemortsup;k++)
12155: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
12156:
12157: for (k=agegomp;k<agemortsup;k++){
12158: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
12159: sumlpop=sumlpop+lpop[k];
12160: }
12161:
12162: tpop[agegomp]=sumlpop;
12163: for (k=agegomp;k<(agemortsup-3);k++){
12164: /* tpop[k+1]=2;*/
12165: tpop[k+1]=tpop[k]-lpop[k];
12166: }
12167:
12168:
12169: printf("\nAge lx qx dx Lx Tx e(x)\n");
12170: for (k=agegomp;k<(agemortsup-2);k++)
12171: 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]);
12172:
12173:
12174: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 12175: ageminpar=50;
12176: agemaxpar=100;
1.194 brouard 12177: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
12178: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12179: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12180: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
12181: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12182: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12183: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 12184: }else{
12185: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
12186: 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 12187: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 12188: }
1.201 brouard 12189: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 12190: stepm, weightopt,\
12191: model,imx,p,matcov,agemortsup);
12192:
1.302 brouard 12193: free_vector(lsurv,agegomp,AGESUP);
12194: free_vector(lpop,agegomp,AGESUP);
12195: free_vector(tpop,agegomp,AGESUP);
1.220 brouard 12196: free_matrix(ximort,1,NDIM,1,NDIM);
1.290 brouard 12197: free_ivector(dcwave,firstobs,lastobs);
12198: free_vector(agecens,firstobs,lastobs);
12199: free_vector(ageexmed,firstobs,lastobs);
12200: free_ivector(cens,firstobs,lastobs);
1.220 brouard 12201: #ifdef GSL
1.136 brouard 12202: #endif
1.186 brouard 12203: } /* Endof if mle==-3 mortality only */
1.205 brouard 12204: /* Standard */
12205: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
12206: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12207: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 12208: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 12209: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12210: for (k=1; k<=npar;k++)
12211: printf(" %d %8.5f",k,p[k]);
12212: printf("\n");
1.205 brouard 12213: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
12214: /* mlikeli uses func not funcone */
1.247 brouard 12215: /* for(i=1;i<nlstate;i++){ */
12216: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
12217: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
12218: /* } */
1.205 brouard 12219: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
12220: }
12221: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
12222: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12223: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
12224: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12225: }
12226: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 12227: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12228: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12229: for (k=1; k<=npar;k++)
12230: printf(" %d %8.5f",k,p[k]);
12231: printf("\n");
12232:
12233: /*--------- results files --------------*/
1.283 brouard 12234: /* 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 12235:
12236:
12237: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12238: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12239: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12240: for(i=1,jk=1; i <=nlstate; i++){
12241: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 12242: if (k != i) {
12243: printf("%d%d ",i,k);
12244: fprintf(ficlog,"%d%d ",i,k);
12245: fprintf(ficres,"%1d%1d ",i,k);
12246: for(j=1; j <=ncovmodel; j++){
12247: printf("%12.7f ",p[jk]);
12248: fprintf(ficlog,"%12.7f ",p[jk]);
12249: fprintf(ficres,"%12.7f ",p[jk]);
12250: jk++;
12251: }
12252: printf("\n");
12253: fprintf(ficlog,"\n");
12254: fprintf(ficres,"\n");
12255: }
1.126 brouard 12256: }
12257: }
1.203 brouard 12258: if(mle != 0){
12259: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 12260: ftolhess=ftol; /* Usually correct */
1.203 brouard 12261: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
12262: 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");
12263: 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");
12264: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 12265: for(k=1; k <=(nlstate+ndeath); k++){
12266: if (k != i) {
12267: printf("%d%d ",i,k);
12268: fprintf(ficlog,"%d%d ",i,k);
12269: for(j=1; j <=ncovmodel; j++){
12270: printf("%12.7f W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk], p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
12271: fprintf(ficlog,"%12.7f W=%8.3f CI=[%12.7f ; %12.7f] ",p[jk], p[jk]/sqrt(matcov[jk][jk]), p[jk]-1.96*sqrt(matcov[jk][jk]),p[jk]+1.96*sqrt(matcov[jk][jk]));
12272: jk++;
12273: }
12274: printf("\n");
12275: fprintf(ficlog,"\n");
12276: }
12277: }
1.193 brouard 12278: }
1.203 brouard 12279: } /* end of hesscov and Wald tests */
1.225 brouard 12280:
1.203 brouard 12281: /* */
1.126 brouard 12282: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
12283: printf("# Scales (for hessian or gradient estimation)\n");
12284: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
12285: for(i=1,jk=1; i <=nlstate; i++){
12286: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 12287: if (j!=i) {
12288: fprintf(ficres,"%1d%1d",i,j);
12289: printf("%1d%1d",i,j);
12290: fprintf(ficlog,"%1d%1d",i,j);
12291: for(k=1; k<=ncovmodel;k++){
12292: printf(" %.5e",delti[jk]);
12293: fprintf(ficlog," %.5e",delti[jk]);
12294: fprintf(ficres," %.5e",delti[jk]);
12295: jk++;
12296: }
12297: printf("\n");
12298: fprintf(ficlog,"\n");
12299: fprintf(ficres,"\n");
12300: }
1.126 brouard 12301: }
12302: }
12303:
12304: 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 12305: if(mle >= 1) /* To big for the screen */
1.126 brouard 12306: 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");
12307: 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");
12308: /* # 121 Var(a12)\n\ */
12309: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12310: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12311: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12312: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12313: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12314: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12315: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12316:
12317:
12318: /* Just to have a covariance matrix which will be more understandable
12319: even is we still don't want to manage dictionary of variables
12320: */
12321: for(itimes=1;itimes<=2;itimes++){
12322: jj=0;
12323: for(i=1; i <=nlstate; i++){
1.225 brouard 12324: for(j=1; j <=nlstate+ndeath; j++){
12325: if(j==i) continue;
12326: for(k=1; k<=ncovmodel;k++){
12327: jj++;
12328: ca[0]= k+'a'-1;ca[1]='\0';
12329: if(itimes==1){
12330: if(mle>=1)
12331: printf("#%1d%1d%d",i,j,k);
12332: fprintf(ficlog,"#%1d%1d%d",i,j,k);
12333: fprintf(ficres,"#%1d%1d%d",i,j,k);
12334: }else{
12335: if(mle>=1)
12336: printf("%1d%1d%d",i,j,k);
12337: fprintf(ficlog,"%1d%1d%d",i,j,k);
12338: fprintf(ficres,"%1d%1d%d",i,j,k);
12339: }
12340: ll=0;
12341: for(li=1;li <=nlstate; li++){
12342: for(lj=1;lj <=nlstate+ndeath; lj++){
12343: if(lj==li) continue;
12344: for(lk=1;lk<=ncovmodel;lk++){
12345: ll++;
12346: if(ll<=jj){
12347: cb[0]= lk +'a'-1;cb[1]='\0';
12348: if(ll<jj){
12349: if(itimes==1){
12350: if(mle>=1)
12351: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12352: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12353: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12354: }else{
12355: if(mle>=1)
12356: printf(" %.5e",matcov[jj][ll]);
12357: fprintf(ficlog," %.5e",matcov[jj][ll]);
12358: fprintf(ficres," %.5e",matcov[jj][ll]);
12359: }
12360: }else{
12361: if(itimes==1){
12362: if(mle>=1)
12363: printf(" Var(%s%1d%1d)",ca,i,j);
12364: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
12365: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
12366: }else{
12367: if(mle>=1)
12368: printf(" %.7e",matcov[jj][ll]);
12369: fprintf(ficlog," %.7e",matcov[jj][ll]);
12370: fprintf(ficres," %.7e",matcov[jj][ll]);
12371: }
12372: }
12373: }
12374: } /* end lk */
12375: } /* end lj */
12376: } /* end li */
12377: if(mle>=1)
12378: printf("\n");
12379: fprintf(ficlog,"\n");
12380: fprintf(ficres,"\n");
12381: numlinepar++;
12382: } /* end k*/
12383: } /*end j */
1.126 brouard 12384: } /* end i */
12385: } /* end itimes */
12386:
12387: fflush(ficlog);
12388: fflush(ficres);
1.225 brouard 12389: while(fgets(line, MAXLINE, ficpar)) {
12390: /* If line starts with a # it is a comment */
12391: if (line[0] == '#') {
12392: numlinepar++;
12393: fputs(line,stdout);
12394: fputs(line,ficparo);
12395: fputs(line,ficlog);
1.299 brouard 12396: fputs(line,ficres);
1.225 brouard 12397: continue;
12398: }else
12399: break;
12400: }
12401:
1.209 brouard 12402: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
12403: /* ungetc(c,ficpar); */
12404: /* fgets(line, MAXLINE, ficpar); */
12405: /* fputs(line,stdout); */
12406: /* fputs(line,ficparo); */
12407: /* } */
12408: /* ungetc(c,ficpar); */
1.126 brouard 12409:
12410: estepm=0;
1.209 brouard 12411: 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 12412:
12413: if (num_filled != 6) {
12414: 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);
12415: 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);
12416: goto end;
12417: }
12418: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
12419: }
12420: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
12421: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
12422:
1.209 brouard 12423: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 12424: if (estepm==0 || estepm < stepm) estepm=stepm;
12425: if (fage <= 2) {
12426: bage = ageminpar;
12427: fage = agemaxpar;
12428: }
12429:
12430: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 12431: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
12432: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 12433:
1.186 brouard 12434: /* Other stuffs, more or less useful */
1.254 brouard 12435: while(fgets(line, MAXLINE, ficpar)) {
12436: /* If line starts with a # it is a comment */
12437: if (line[0] == '#') {
12438: numlinepar++;
12439: fputs(line,stdout);
12440: fputs(line,ficparo);
12441: fputs(line,ficlog);
1.299 brouard 12442: fputs(line,ficres);
1.254 brouard 12443: continue;
12444: }else
12445: break;
12446: }
12447:
12448: 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){
12449:
12450: if (num_filled != 7) {
12451: 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);
12452: 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);
12453: goto end;
12454: }
12455: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
12456: 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);
12457: 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);
12458: 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 12459: }
1.254 brouard 12460:
12461: while(fgets(line, MAXLINE, ficpar)) {
12462: /* If line starts with a # it is a comment */
12463: if (line[0] == '#') {
12464: numlinepar++;
12465: fputs(line,stdout);
12466: fputs(line,ficparo);
12467: fputs(line,ficlog);
1.299 brouard 12468: fputs(line,ficres);
1.254 brouard 12469: continue;
12470: }else
12471: break;
1.126 brouard 12472: }
12473:
12474:
12475: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
12476: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
12477:
1.254 brouard 12478: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
12479: if (num_filled != 1) {
12480: 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);
12481: 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);
12482: goto end;
12483: }
12484: printf("pop_based=%d\n",popbased);
12485: fprintf(ficlog,"pop_based=%d\n",popbased);
12486: fprintf(ficparo,"pop_based=%d\n",popbased);
12487: fprintf(ficres,"pop_based=%d\n",popbased);
12488: }
12489:
1.258 brouard 12490: /* Results */
1.307 brouard 12491: endishere=0;
1.258 brouard 12492: nresult=0;
1.308 brouard 12493: parameterline=0;
1.258 brouard 12494: do{
12495: if(!fgets(line, MAXLINE, ficpar)){
12496: endishere=1;
1.308 brouard 12497: parameterline=15;
1.258 brouard 12498: }else if (line[0] == '#') {
12499: /* If line starts with a # it is a comment */
1.254 brouard 12500: numlinepar++;
12501: fputs(line,stdout);
12502: fputs(line,ficparo);
12503: fputs(line,ficlog);
1.299 brouard 12504: fputs(line,ficres);
1.254 brouard 12505: continue;
1.258 brouard 12506: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
12507: parameterline=11;
1.296 brouard 12508: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
1.258 brouard 12509: parameterline=12;
1.307 brouard 12510: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
1.258 brouard 12511: parameterline=13;
1.307 brouard 12512: }
1.258 brouard 12513: else{
12514: parameterline=14;
1.254 brouard 12515: }
1.308 brouard 12516: switch (parameterline){ /* =0 only if only comments */
1.258 brouard 12517: case 11:
1.296 brouard 12518: 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)){
12519: 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 12520: 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);
12521: 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);
12522: 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);
12523: /* day and month of proj2 are not used but only year anproj2.*/
1.273 brouard 12524: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
12525: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
1.296 brouard 12526: prvforecast = 1;
12527: }
12528: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
1.313 brouard 12529: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12530: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12531: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
1.296 brouard 12532: prvforecast = 2;
12533: }
12534: else {
12535: 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);
12536: 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);
12537: goto end;
1.258 brouard 12538: }
1.254 brouard 12539: break;
1.258 brouard 12540: case 12:
1.296 brouard 12541: 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)){
12542: 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);
12543: 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);
12544: 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);
12545: 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);
12546: /* day and month of back2 are not used but only year anback2.*/
1.273 brouard 12547: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
12548: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
1.296 brouard 12549: prvbackcast = 1;
12550: }
12551: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
1.313 brouard 12552: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12553: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12554: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
1.296 brouard 12555: prvbackcast = 2;
12556: }
12557: else {
12558: 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);
12559: 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);
12560: goto end;
1.258 brouard 12561: }
1.230 brouard 12562: break;
1.258 brouard 12563: case 13:
1.307 brouard 12564: num_filled=sscanf(line,"result:%[^\n]\n",resultline);
12565: nresult++; /* Sum of resultlines */
12566: printf("Result %d: result:%s\n",nresult, resultline);
12567: if(nresult > MAXRESULTLINES){
12568: 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. ",MAXRESULTLINES,nresult,rfileres);
12569: 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. ",MAXRESULTLINES,nresult,rfileres);
12570: goto end;
12571: }
1.310 brouard 12572: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.314 brouard 12573: fprintf(ficparo,"result: %s\n",resultline);
12574: fprintf(ficres,"result: %s\n",resultline);
12575: fprintf(ficlog,"result: %s\n",resultline);
1.310 brouard 12576: } else
12577: goto end;
1.307 brouard 12578: break;
12579: case 14:
12580: printf("Error: Unknown command '%s'\n",line);
12581: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
1.314 brouard 12582: if(line[0] == ' ' || line[0] == '\n'){
12583: printf("It should not be an empty line '%s'\n",line);
12584: fprintf(ficlog,"It should not be an empty line '%s'\n",line);
12585: }
1.307 brouard 12586: if(ncovmodel >=2 && nresult==0 ){
12587: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
12588: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
1.258 brouard 12589: }
1.307 brouard 12590: /* goto end; */
12591: break;
1.308 brouard 12592: case 15:
12593: printf("End of resultlines.\n");
12594: fprintf(ficlog,"End of resultlines.\n");
12595: break;
12596: default: /* parameterline =0 */
1.307 brouard 12597: nresult=1;
12598: decoderesult(".",nresult ); /* No covariate */
1.258 brouard 12599: } /* End switch parameterline */
12600: }while(endishere==0); /* End do */
1.126 brouard 12601:
1.230 brouard 12602: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 12603: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 12604:
12605: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 12606: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 12607: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 12608: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12609: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 12610: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 12611: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12612: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 12613: }else{
1.270 brouard 12614: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
1.296 brouard 12615: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
12616: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
12617: if(prvforecast==1){
12618: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
12619: jprojd=jproj1;
12620: mprojd=mproj1;
12621: anprojd=anproj1;
12622: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
12623: jprojf=jproj2;
12624: mprojf=mproj2;
12625: anprojf=anproj2;
12626: } else if(prvforecast == 2){
12627: dateprojd=dateintmean;
12628: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
12629: dateprojf=dateintmean+yrfproj;
12630: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
12631: }
12632: if(prvbackcast==1){
12633: datebackd=(jback1+12*mback1+365*anback1)/365;
12634: jbackd=jback1;
12635: mbackd=mback1;
12636: anbackd=anback1;
12637: datebackf=(jback2+12*mback2+365*anback2)/365;
12638: jbackf=jback2;
12639: mbackf=mback2;
12640: anbackf=anback2;
12641: } else if(prvbackcast == 2){
12642: datebackd=dateintmean;
12643: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
12644: datebackf=dateintmean-yrbproj;
12645: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
12646: }
12647:
12648: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);
1.220 brouard 12649: }
12650: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.296 brouard 12651: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
12652: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
1.220 brouard 12653:
1.225 brouard 12654: /*------------ free_vector -------------*/
12655: /* chdir(path); */
1.220 brouard 12656:
1.215 brouard 12657: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
12658: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
12659: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
12660: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.290 brouard 12661: free_lvector(num,firstobs,lastobs);
12662: free_vector(agedc,firstobs,lastobs);
1.126 brouard 12663: /*free_matrix(covar,0,NCOVMAX,1,n);*/
12664: /*free_matrix(covar,1,NCOVMAX,1,n);*/
12665: fclose(ficparo);
12666: fclose(ficres);
1.220 brouard 12667:
12668:
1.186 brouard 12669: /* Other results (useful)*/
1.220 brouard 12670:
12671:
1.126 brouard 12672: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 12673: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
12674: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 12675: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 12676: fclose(ficrespl);
12677:
12678: /*------------- h Pij x at various ages ------------*/
1.180 brouard 12679: /*#include "hpijx.h"*/
12680: hPijx(p, bage, fage);
1.145 brouard 12681: fclose(ficrespij);
1.227 brouard 12682:
1.220 brouard 12683: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 12684: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 12685: k=1;
1.126 brouard 12686: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 12687:
1.269 brouard 12688: /* Prevalence for each covariate combination in probs[age][status][cov] */
12689: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
12690: for(i=AGEINF;i<=AGESUP;i++)
1.219 brouard 12691: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 12692: for(k=1;k<=ncovcombmax;k++)
12693: probs[i][j][k]=0.;
1.269 brouard 12694: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
12695: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.219 brouard 12696: if (mobilav!=0 ||mobilavproj !=0 ) {
1.269 brouard 12697: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
12698: for(i=AGEINF;i<=AGESUP;i++)
1.268 brouard 12699: for(j=1;j<=nlstate+ndeath;j++)
1.227 brouard 12700: for(k=1;k<=ncovcombmax;k++)
12701: mobaverages[i][j][k]=0.;
1.219 brouard 12702: mobaverage=mobaverages;
12703: if (mobilav!=0) {
1.235 brouard 12704: printf("Movingaveraging observed prevalence\n");
1.258 brouard 12705: fprintf(ficlog,"Movingaveraging observed prevalence\n");
1.227 brouard 12706: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
12707: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
12708: printf(" Error in movingaverage mobilav=%d\n",mobilav);
12709: }
1.269 brouard 12710: } else if (mobilavproj !=0) {
1.235 brouard 12711: printf("Movingaveraging projected observed prevalence\n");
1.258 brouard 12712: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
1.227 brouard 12713: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
12714: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
12715: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
12716: }
1.269 brouard 12717: }else{
12718: printf("Internal error moving average\n");
12719: fflush(stdout);
12720: exit(1);
1.219 brouard 12721: }
12722: }/* end if moving average */
1.227 brouard 12723:
1.126 brouard 12724: /*---------- Forecasting ------------------*/
1.296 brouard 12725: if(prevfcast==1){
12726: /* /\* if(stepm ==1){*\/ */
12727: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
12728: /*This done previously after freqsummary.*/
12729: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
12730: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
12731:
12732: /* } else if (prvforecast==2){ */
12733: /* /\* if(stepm ==1){*\/ */
12734: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
12735: /* } */
12736: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
12737: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
1.126 brouard 12738: }
1.269 brouard 12739:
1.296 brouard 12740: /* Prevbcasting */
12741: if(prevbcast==1){
1.219 brouard 12742: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12743: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12744: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12745:
12746: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
12747:
12748: bprlim=matrix(1,nlstate,1,nlstate);
1.269 brouard 12749:
1.219 brouard 12750: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
12751: fclose(ficresplb);
12752:
1.222 brouard 12753: hBijx(p, bage, fage, mobaverage);
12754: fclose(ficrespijb);
1.219 brouard 12755:
1.296 brouard 12756: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
12757: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
12758: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
12759: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
12760: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
12761: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
12762:
12763:
1.269 brouard 12764: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 12765:
12766:
1.269 brouard 12767: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.219 brouard 12768: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
12769: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
12770: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
1.296 brouard 12771: } /* end Prevbcasting */
1.268 brouard 12772:
1.186 brouard 12773:
12774: /* ------ Other prevalence ratios------------ */
1.126 brouard 12775:
1.215 brouard 12776: free_ivector(wav,1,imx);
12777: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
12778: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
12779: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 12780:
12781:
1.127 brouard 12782: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 12783:
1.201 brouard 12784: strcpy(filerese,"E_");
12785: strcat(filerese,fileresu);
1.126 brouard 12786: if((ficreseij=fopen(filerese,"w"))==NULL) {
12787: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
12788: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
12789: }
1.208 brouard 12790: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
12791: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 12792:
12793: pstamp(ficreseij);
1.219 brouard 12794:
1.235 brouard 12795: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
12796: if (cptcovn < 1){i1=1;}
12797:
12798: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12799: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 12800: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 12801: continue;
1.219 brouard 12802: fprintf(ficreseij,"\n#****** ");
1.235 brouard 12803: printf("\n#****** ");
1.225 brouard 12804: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 12805: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 12806: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12807: }
12808: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12809: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12810: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
1.219 brouard 12811: }
12812: fprintf(ficreseij,"******\n");
1.235 brouard 12813: printf("******\n");
1.219 brouard 12814:
12815: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12816: oldm=oldms;savm=savms;
1.235 brouard 12817: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 12818:
1.219 brouard 12819: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 12820: }
12821: fclose(ficreseij);
1.208 brouard 12822: printf("done evsij\n");fflush(stdout);
12823: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.269 brouard 12824:
1.218 brouard 12825:
1.227 brouard 12826: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 12827:
1.201 brouard 12828: strcpy(filerest,"T_");
12829: strcat(filerest,fileresu);
1.127 brouard 12830: if((ficrest=fopen(filerest,"w"))==NULL) {
12831: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
12832: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
12833: }
1.208 brouard 12834: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
12835: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.201 brouard 12836: strcpy(fileresstde,"STDE_");
12837: strcat(fileresstde,fileresu);
1.126 brouard 12838: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 12839: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
12840: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 12841: }
1.227 brouard 12842: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
12843: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 12844:
1.201 brouard 12845: strcpy(filerescve,"CVE_");
12846: strcat(filerescve,fileresu);
1.126 brouard 12847: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 12848: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
12849: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 12850: }
1.227 brouard 12851: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
12852: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 12853:
1.201 brouard 12854: strcpy(fileresv,"V_");
12855: strcat(fileresv,fileresu);
1.126 brouard 12856: if((ficresvij=fopen(fileresv,"w"))==NULL) {
12857: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
12858: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
12859: }
1.227 brouard 12860: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
12861: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 12862:
1.235 brouard 12863: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
12864: if (cptcovn < 1){i1=1;}
12865:
12866: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12867: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 12868: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 12869: continue;
1.242 brouard 12870: printf("\n#****** Result for:");
12871: fprintf(ficrest,"\n#****** Result for:");
12872: fprintf(ficlog,"\n#****** Result for:");
1.227 brouard 12873: for(j=1;j<=cptcoveff;j++){
12874: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12875: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12876: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12877: }
1.235 brouard 12878: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12879: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12880: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12881: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12882: }
1.208 brouard 12883: fprintf(ficrest,"******\n");
1.227 brouard 12884: fprintf(ficlog,"******\n");
12885: printf("******\n");
1.208 brouard 12886:
12887: fprintf(ficresstdeij,"\n#****** ");
12888: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 12889: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 12890: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12891: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 12892: }
1.235 brouard 12893: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12894: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12895: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12896: }
1.208 brouard 12897: fprintf(ficresstdeij,"******\n");
12898: fprintf(ficrescveij,"******\n");
12899:
12900: fprintf(ficresvij,"\n#****** ");
1.238 brouard 12901: /* pstamp(ficresvij); */
1.225 brouard 12902: for(j=1;j<=cptcoveff;j++)
1.227 brouard 12903: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 12904: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12905: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12906: }
1.208 brouard 12907: fprintf(ficresvij,"******\n");
12908:
12909: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12910: oldm=oldms;savm=savms;
1.235 brouard 12911: printf(" cvevsij ");
12912: fprintf(ficlog, " cvevsij ");
12913: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 12914: printf(" end cvevsij \n ");
12915: fprintf(ficlog, " end cvevsij \n ");
12916:
12917: /*
12918: */
12919: /* goto endfree; */
12920:
12921: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12922: pstamp(ficrest);
12923:
1.269 brouard 12924: epj=vector(1,nlstate+1);
1.208 brouard 12925: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 12926: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
12927: cptcod= 0; /* To be deleted */
12928: printf("varevsij vpopbased=%d \n",vpopbased);
12929: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 12930: 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 12931: 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 ");
12932: if(vpopbased==1)
12933: 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);
12934: else
1.288 brouard 12935: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 12936: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
12937: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
12938: fprintf(ficrest,"\n");
12939: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
1.288 brouard 12940: printf("Computing age specific forward period (stable) prevalences in each health state \n");
12941: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 12942: for(age=bage; age <=fage ;age++){
1.235 brouard 12943: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 12944: if (vpopbased==1) {
12945: if(mobilav ==0){
12946: for(i=1; i<=nlstate;i++)
12947: prlim[i][i]=probs[(int)age][i][k];
12948: }else{ /* mobilav */
12949: for(i=1; i<=nlstate;i++)
12950: prlim[i][i]=mobaverage[(int)age][i][k];
12951: }
12952: }
1.219 brouard 12953:
1.227 brouard 12954: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
12955: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
12956: /* printf(" age %4.0f ",age); */
12957: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
12958: for(i=1, epj[j]=0.;i <=nlstate;i++) {
12959: epj[j] += prlim[i][i]*eij[i][j][(int)age];
12960: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
12961: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
12962: }
12963: epj[nlstate+1] +=epj[j];
12964: }
12965: /* printf(" age %4.0f \n",age); */
1.219 brouard 12966:
1.227 brouard 12967: for(i=1, vepp=0.;i <=nlstate;i++)
12968: for(j=1;j <=nlstate;j++)
12969: vepp += vareij[i][j][(int)age];
12970: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
12971: for(j=1;j <=nlstate;j++){
12972: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
12973: }
12974: fprintf(ficrest,"\n");
12975: }
1.208 brouard 12976: } /* End vpopbased */
1.269 brouard 12977: free_vector(epj,1,nlstate+1);
1.208 brouard 12978: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
12979: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.235 brouard 12980: printf("done selection\n");fflush(stdout);
12981: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 12982:
1.235 brouard 12983: } /* End k selection */
1.227 brouard 12984:
12985: printf("done State-specific expectancies\n");fflush(stdout);
12986: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
12987:
1.288 brouard 12988: /* variance-covariance of forward period prevalence*/
1.269 brouard 12989: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 12990:
1.227 brouard 12991:
1.290 brouard 12992: free_vector(weight,firstobs,lastobs);
1.227 brouard 12993: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.290 brouard 12994: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
12995: free_matrix(anint,1,maxwav,firstobs,lastobs);
12996: free_matrix(mint,1,maxwav,firstobs,lastobs);
12997: free_ivector(cod,firstobs,lastobs);
1.227 brouard 12998: free_ivector(tab,1,NCOVMAX);
12999: fclose(ficresstdeij);
13000: fclose(ficrescveij);
13001: fclose(ficresvij);
13002: fclose(ficrest);
13003: fclose(ficpar);
13004:
13005:
1.126 brouard 13006: /*---------- End : free ----------------*/
1.219 brouard 13007: if (mobilav!=0 ||mobilavproj !=0)
1.269 brouard 13008: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
13009: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 13010: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
13011: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 13012: } /* mle==-3 arrives here for freeing */
1.227 brouard 13013: /* endfree:*/
13014: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
13015: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
13016: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.290 brouard 13017: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs);
13018: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
13019: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
13020: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
1.227 brouard 13021: free_matrix(matcov,1,npar,1,npar);
13022: free_matrix(hess,1,npar,1,npar);
13023: /*free_vector(delti,1,npar);*/
13024: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
13025: free_matrix(agev,1,maxwav,1,imx);
1.269 brouard 13026: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.227 brouard 13027: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
13028:
13029: free_ivector(ncodemax,1,NCOVMAX);
13030: free_ivector(ncodemaxwundef,1,NCOVMAX);
13031: free_ivector(Dummy,-1,NCOVMAX);
13032: free_ivector(Fixed,-1,NCOVMAX);
1.238 brouard 13033: free_ivector(DummyV,1,NCOVMAX);
13034: free_ivector(FixedV,1,NCOVMAX);
1.227 brouard 13035: free_ivector(Typevar,-1,NCOVMAX);
13036: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 13037: free_ivector(TvarsQ,1,NCOVMAX);
13038: free_ivector(TvarsQind,1,NCOVMAX);
13039: free_ivector(TvarsD,1,NCOVMAX);
13040: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 13041: free_ivector(TvarFD,1,NCOVMAX);
13042: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 13043: free_ivector(TvarF,1,NCOVMAX);
13044: free_ivector(TvarFind,1,NCOVMAX);
13045: free_ivector(TvarV,1,NCOVMAX);
13046: free_ivector(TvarVind,1,NCOVMAX);
13047: free_ivector(TvarA,1,NCOVMAX);
13048: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 13049: free_ivector(TvarFQ,1,NCOVMAX);
13050: free_ivector(TvarFQind,1,NCOVMAX);
13051: free_ivector(TvarVD,1,NCOVMAX);
13052: free_ivector(TvarVDind,1,NCOVMAX);
13053: free_ivector(TvarVQ,1,NCOVMAX);
13054: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 13055: free_ivector(Tvarsel,1,NCOVMAX);
13056: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 13057: free_ivector(Tposprod,1,NCOVMAX);
13058: free_ivector(Tprod,1,NCOVMAX);
13059: free_ivector(Tvaraff,1,NCOVMAX);
13060: free_ivector(invalidvarcomb,1,ncovcombmax);
13061: free_ivector(Tage,1,NCOVMAX);
13062: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 13063: free_ivector(TmodelInvind,1,NCOVMAX);
13064: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 13065:
13066: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
13067: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 13068: fflush(fichtm);
13069: fflush(ficgp);
13070:
1.227 brouard 13071:
1.126 brouard 13072: if((nberr >0) || (nbwarn>0)){
1.216 brouard 13073: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
13074: 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 13075: }else{
13076: printf("End of Imach\n");
13077: fprintf(ficlog,"End of Imach\n");
13078: }
13079: printf("See log file on %s\n",filelog);
13080: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 13081: /*(void) gettimeofday(&end_time,&tzp);*/
13082: rend_time = time(NULL);
13083: end_time = *localtime(&rend_time);
13084: /* tml = *localtime(&end_time.tm_sec); */
13085: strcpy(strtend,asctime(&end_time));
1.126 brouard 13086: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
13087: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 13088: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 13089:
1.157 brouard 13090: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
13091: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
13092: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 13093: /* printf("Total time was %d uSec.\n", total_usecs);*/
13094: /* if(fileappend(fichtm,optionfilehtm)){ */
13095: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13096: fclose(fichtm);
13097: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13098: fclose(fichtmcov);
13099: fclose(ficgp);
13100: fclose(ficlog);
13101: /*------ End -----------*/
1.227 brouard 13102:
1.281 brouard 13103:
13104: /* Executes gnuplot */
1.227 brouard 13105:
13106: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 13107: #ifdef WIN32
1.227 brouard 13108: if (_chdir(pathcd) != 0)
13109: printf("Can't move to directory %s!\n",path);
13110: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 13111: #else
1.227 brouard 13112: if(chdir(pathcd) != 0)
13113: printf("Can't move to directory %s!\n", path);
13114: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 13115: #endif
1.126 brouard 13116: printf("Current directory %s!\n",pathcd);
13117: /*strcat(plotcmd,CHARSEPARATOR);*/
13118: sprintf(plotcmd,"gnuplot");
1.157 brouard 13119: #ifdef _WIN32
1.126 brouard 13120: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
13121: #endif
13122: if(!stat(plotcmd,&info)){
1.158 brouard 13123: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13124: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 13125: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 13126: }else
13127: strcpy(pplotcmd,plotcmd);
1.157 brouard 13128: #ifdef __unix
1.126 brouard 13129: strcpy(plotcmd,GNUPLOTPROGRAM);
13130: if(!stat(plotcmd,&info)){
1.158 brouard 13131: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13132: }else
13133: strcpy(pplotcmd,plotcmd);
13134: #endif
13135: }else
13136: strcpy(pplotcmd,plotcmd);
13137:
13138: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 13139: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.292 brouard 13140: strcpy(pplotcmd,plotcmd);
1.227 brouard 13141:
1.126 brouard 13142: if((outcmd=system(plotcmd)) != 0){
1.292 brouard 13143: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 13144: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 13145: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.292 brouard 13146: if((outcmd=system(plotcmd)) != 0){
1.153 brouard 13147: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.292 brouard 13148: strcpy(plotcmd,pplotcmd);
13149: }
1.126 brouard 13150: }
1.158 brouard 13151: printf(" Successful, please wait...");
1.126 brouard 13152: while (z[0] != 'q') {
13153: /* chdir(path); */
1.154 brouard 13154: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 13155: scanf("%s",z);
13156: /* if (z[0] == 'c') system("./imach"); */
13157: if (z[0] == 'e') {
1.158 brouard 13158: #ifdef __APPLE__
1.152 brouard 13159: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 13160: #elif __linux
13161: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 13162: #else
1.152 brouard 13163: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 13164: #endif
13165: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
13166: system(pplotcmd);
1.126 brouard 13167: }
13168: else if (z[0] == 'g') system(plotcmd);
13169: else if (z[0] == 'q') exit(0);
13170: }
1.227 brouard 13171: end:
1.126 brouard 13172: while (z[0] != 'q') {
1.195 brouard 13173: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 13174: scanf("%s",z);
13175: }
1.283 brouard 13176: printf("End\n");
1.282 brouard 13177: exit(0);
1.126 brouard 13178: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>