Annotation of imach/src/imach.c, revision 1.309
1.309 ! brouard 1: /* $Id: imach.c,v 1.308 2021/03/31 13:11:57 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.309 ! brouard 4: Revision 1.308 2021/03/31 13:11:57 brouard
! 5: Summary: Version 0.99r23
! 6:
! 7:
! 8: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
! 9:
1.308 brouard 10: Revision 1.307 2021/03/08 18:11:32 brouard
11: Summary: 0.99r22 fixed bug on result:
12:
1.307 brouard 13: Revision 1.306 2021/02/20 15:44:02 brouard
14: Summary: Version 0.99r21
15:
16: * imach.c (Module): Fix bug on quitting after result lines!
17: (Module): Version 0.99r21
18:
1.306 brouard 19: Revision 1.305 2021/02/20 15:28:30 brouard
20: * imach.c (Module): Fix bug on quitting after result lines!
21:
1.305 brouard 22: Revision 1.304 2021/02/12 11:34:20 brouard
23: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
24:
1.304 brouard 25: Revision 1.303 2021/02/11 19:50:15 brouard
26: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
27:
1.303 brouard 28: Revision 1.302 2020/02/22 21:00:05 brouard
29: * (Module): imach.c Update mle=-3 (for computing Life expectancy
30: and life table from the data without any state)
31:
1.302 brouard 32: Revision 1.301 2019/06/04 13:51:20 brouard
33: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
34:
1.301 brouard 35: Revision 1.300 2019/05/22 19:09:45 brouard
36: Summary: version 0.99r19 of May 2019
37:
1.300 brouard 38: Revision 1.299 2019/05/22 18:37:08 brouard
39: Summary: Cleaned 0.99r19
40:
1.299 brouard 41: Revision 1.298 2019/05/22 18:19:56 brouard
42: *** empty log message ***
43:
1.298 brouard 44: Revision 1.297 2019/05/22 17:56:10 brouard
45: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
46:
1.297 brouard 47: Revision 1.296 2019/05/20 13:03:18 brouard
48: Summary: Projection syntax simplified
49:
50:
51: We can now start projections, forward or backward, from the mean date
52: of inteviews up to or down to a number of years of projection:
53: prevforecast=1 yearsfproj=15.3 mobil_average=0
54: or
55: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
56: or
57: prevbackcast=1 yearsbproj=12.3 mobil_average=1
58: or
59: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
60:
1.296 brouard 61: Revision 1.295 2019/05/18 09:52:50 brouard
62: Summary: doxygen tex bug
63:
1.295 brouard 64: Revision 1.294 2019/05/16 14:54:33 brouard
65: Summary: There was some wrong lines added
66:
1.294 brouard 67: Revision 1.293 2019/05/09 15:17:34 brouard
68: *** empty log message ***
69:
1.293 brouard 70: Revision 1.292 2019/05/09 14:17:20 brouard
71: Summary: Some updates
72:
1.292 brouard 73: Revision 1.291 2019/05/09 13:44:18 brouard
74: Summary: Before ncovmax
75:
1.291 brouard 76: Revision 1.290 2019/05/09 13:39:37 brouard
77: Summary: 0.99r18 unlimited number of individuals
78:
79: 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.
80:
1.290 brouard 81: Revision 1.289 2018/12/13 09:16:26 brouard
82: Summary: Bug for young ages (<-30) will be in r17
83:
1.289 brouard 84: Revision 1.288 2018/05/02 20:58:27 brouard
85: Summary: Some bugs fixed
86:
1.288 brouard 87: Revision 1.287 2018/05/01 17:57:25 brouard
88: Summary: Bug fixed by providing frequencies only for non missing covariates
89:
1.287 brouard 90: Revision 1.286 2018/04/27 14:27:04 brouard
91: Summary: some minor bugs
92:
1.286 brouard 93: Revision 1.285 2018/04/21 21:02:16 brouard
94: Summary: Some bugs fixed, valgrind tested
95:
1.285 brouard 96: Revision 1.284 2018/04/20 05:22:13 brouard
97: Summary: Computing mean and stdeviation of fixed quantitative variables
98:
1.284 brouard 99: Revision 1.283 2018/04/19 14:49:16 brouard
100: Summary: Some minor bugs fixed
101:
1.283 brouard 102: Revision 1.282 2018/02/27 22:50:02 brouard
103: *** empty log message ***
104:
1.282 brouard 105: Revision 1.281 2018/02/27 19:25:23 brouard
106: Summary: Adding second argument for quitting
107:
1.281 brouard 108: Revision 1.280 2018/02/21 07:58:13 brouard
109: Summary: 0.99r15
110:
111: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
112:
1.280 brouard 113: Revision 1.279 2017/07/20 13:35:01 brouard
114: Summary: temporary working
115:
1.279 brouard 116: Revision 1.278 2017/07/19 14:09:02 brouard
117: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
118:
1.278 brouard 119: Revision 1.277 2017/07/17 08:53:49 brouard
120: Summary: BOM files can be read now
121:
1.277 brouard 122: Revision 1.276 2017/06/30 15:48:31 brouard
123: Summary: Graphs improvements
124:
1.276 brouard 125: Revision 1.275 2017/06/30 13:39:33 brouard
126: Summary: Saito's color
127:
1.275 brouard 128: Revision 1.274 2017/06/29 09:47:08 brouard
129: Summary: Version 0.99r14
130:
1.274 brouard 131: Revision 1.273 2017/06/27 11:06:02 brouard
132: Summary: More documentation on projections
133:
1.273 brouard 134: Revision 1.272 2017/06/27 10:22:40 brouard
135: Summary: Color of backprojection changed from 6 to 5(yellow)
136:
1.272 brouard 137: Revision 1.271 2017/06/27 10:17:50 brouard
138: Summary: Some bug with rint
139:
1.271 brouard 140: Revision 1.270 2017/05/24 05:45:29 brouard
141: *** empty log message ***
142:
1.270 brouard 143: Revision 1.269 2017/05/23 08:39:25 brouard
144: Summary: Code into subroutine, cleanings
145:
1.269 brouard 146: Revision 1.268 2017/05/18 20:09:32 brouard
147: Summary: backprojection and confidence intervals of backprevalence
148:
1.268 brouard 149: Revision 1.267 2017/05/13 10:25:05 brouard
150: Summary: temporary save for backprojection
151:
1.267 brouard 152: Revision 1.266 2017/05/13 07:26:12 brouard
153: Summary: Version 0.99r13 (improvements and bugs fixed)
154:
1.266 brouard 155: Revision 1.265 2017/04/26 16:22:11 brouard
156: Summary: imach 0.99r13 Some bugs fixed
157:
1.265 brouard 158: Revision 1.264 2017/04/26 06:01:29 brouard
159: Summary: Labels in graphs
160:
1.264 brouard 161: Revision 1.263 2017/04/24 15:23:15 brouard
162: Summary: to save
163:
1.263 brouard 164: Revision 1.262 2017/04/18 16:48:12 brouard
165: *** empty log message ***
166:
1.262 brouard 167: Revision 1.261 2017/04/05 10:14:09 brouard
168: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
169:
1.261 brouard 170: Revision 1.260 2017/04/04 17:46:59 brouard
171: Summary: Gnuplot indexations fixed (humm)
172:
1.260 brouard 173: Revision 1.259 2017/04/04 13:01:16 brouard
174: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
175:
1.259 brouard 176: Revision 1.258 2017/04/03 10:17:47 brouard
177: Summary: Version 0.99r12
178:
179: Some cleanings, conformed with updated documentation.
180:
1.258 brouard 181: Revision 1.257 2017/03/29 16:53:30 brouard
182: Summary: Temp
183:
1.257 brouard 184: Revision 1.256 2017/03/27 05:50:23 brouard
185: Summary: Temporary
186:
1.256 brouard 187: Revision 1.255 2017/03/08 16:02:28 brouard
188: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
189:
1.255 brouard 190: Revision 1.254 2017/03/08 07:13:00 brouard
191: Summary: Fixing data parameter line
192:
1.254 brouard 193: Revision 1.253 2016/12/15 11:59:41 brouard
194: Summary: 0.99 in progress
195:
1.253 brouard 196: Revision 1.252 2016/09/15 21:15:37 brouard
197: *** empty log message ***
198:
1.252 brouard 199: Revision 1.251 2016/09/15 15:01:13 brouard
200: Summary: not working
201:
1.251 brouard 202: Revision 1.250 2016/09/08 16:07:27 brouard
203: Summary: continue
204:
1.250 brouard 205: Revision 1.249 2016/09/07 17:14:18 brouard
206: Summary: Starting values from frequencies
207:
1.249 brouard 208: Revision 1.248 2016/09/07 14:10:18 brouard
209: *** empty log message ***
210:
1.248 brouard 211: Revision 1.247 2016/09/02 11:11:21 brouard
212: *** empty log message ***
213:
1.247 brouard 214: Revision 1.246 2016/09/02 08:49:22 brouard
215: *** empty log message ***
216:
1.246 brouard 217: Revision 1.245 2016/09/02 07:25:01 brouard
218: *** empty log message ***
219:
1.245 brouard 220: Revision 1.244 2016/09/02 07:17:34 brouard
221: *** empty log message ***
222:
1.244 brouard 223: Revision 1.243 2016/09/02 06:45:35 brouard
224: *** empty log message ***
225:
1.243 brouard 226: Revision 1.242 2016/08/30 15:01:20 brouard
227: Summary: Fixing a lots
228:
1.242 brouard 229: Revision 1.241 2016/08/29 17:17:25 brouard
230: Summary: gnuplot problem in Back projection to fix
231:
1.241 brouard 232: Revision 1.240 2016/08/29 07:53:18 brouard
233: Summary: Better
234:
1.240 brouard 235: Revision 1.239 2016/08/26 15:51:03 brouard
236: Summary: Improvement in Powell output in order to copy and paste
237:
238: Author:
239:
1.239 brouard 240: Revision 1.238 2016/08/26 14:23:35 brouard
241: Summary: Starting tests of 0.99
242:
1.238 brouard 243: Revision 1.237 2016/08/26 09:20:19 brouard
244: Summary: to valgrind
245:
1.237 brouard 246: Revision 1.236 2016/08/25 10:50:18 brouard
247: *** empty log message ***
248:
1.236 brouard 249: Revision 1.235 2016/08/25 06:59:23 brouard
250: *** empty log message ***
251:
1.235 brouard 252: Revision 1.234 2016/08/23 16:51:20 brouard
253: *** empty log message ***
254:
1.234 brouard 255: Revision 1.233 2016/08/23 07:40:50 brouard
256: Summary: not working
257:
1.233 brouard 258: Revision 1.232 2016/08/22 14:20:21 brouard
259: Summary: not working
260:
1.232 brouard 261: Revision 1.231 2016/08/22 07:17:15 brouard
262: Summary: not working
263:
1.231 brouard 264: Revision 1.230 2016/08/22 06:55:53 brouard
265: Summary: Not working
266:
1.230 brouard 267: Revision 1.229 2016/07/23 09:45:53 brouard
268: Summary: Completing for func too
269:
1.229 brouard 270: Revision 1.228 2016/07/22 17:45:30 brouard
271: Summary: Fixing some arrays, still debugging
272:
1.227 brouard 273: Revision 1.226 2016/07/12 18:42:34 brouard
274: Summary: temp
275:
1.226 brouard 276: Revision 1.225 2016/07/12 08:40:03 brouard
277: Summary: saving but not running
278:
1.225 brouard 279: Revision 1.224 2016/07/01 13:16:01 brouard
280: Summary: Fixes
281:
1.224 brouard 282: Revision 1.223 2016/02/19 09:23:35 brouard
283: Summary: temporary
284:
1.223 brouard 285: Revision 1.222 2016/02/17 08:14:50 brouard
286: Summary: Probably last 0.98 stable version 0.98r6
287:
1.222 brouard 288: Revision 1.221 2016/02/15 23:35:36 brouard
289: Summary: minor bug
290:
1.220 brouard 291: Revision 1.219 2016/02/15 00:48:12 brouard
292: *** empty log message ***
293:
1.219 brouard 294: Revision 1.218 2016/02/12 11:29:23 brouard
295: Summary: 0.99 Back projections
296:
1.218 brouard 297: Revision 1.217 2015/12/23 17:18:31 brouard
298: Summary: Experimental backcast
299:
1.217 brouard 300: Revision 1.216 2015/12/18 17:32:11 brouard
301: Summary: 0.98r4 Warning and status=-2
302:
303: Version 0.98r4 is now:
304: - displaying an error when status is -1, date of interview unknown and date of death known;
305: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
306: Older changes concerning s=-2, dating from 2005 have been supersed.
307:
1.216 brouard 308: Revision 1.215 2015/12/16 08:52:24 brouard
309: Summary: 0.98r4 working
310:
1.215 brouard 311: Revision 1.214 2015/12/16 06:57:54 brouard
312: Summary: temporary not working
313:
1.214 brouard 314: Revision 1.213 2015/12/11 18:22:17 brouard
315: Summary: 0.98r4
316:
1.213 brouard 317: Revision 1.212 2015/11/21 12:47:24 brouard
318: Summary: minor typo
319:
1.212 brouard 320: Revision 1.211 2015/11/21 12:41:11 brouard
321: Summary: 0.98r3 with some graph of projected cross-sectional
322:
323: Author: Nicolas Brouard
324:
1.211 brouard 325: Revision 1.210 2015/11/18 17:41:20 brouard
1.252 brouard 326: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
1.210 brouard 327: Summary: Adding ftolpl parameter
328: Author: N Brouard
329:
330: We had difficulties to get smoothed confidence intervals. It was due
331: to the period prevalence which wasn't computed accurately. The inner
332: parameter ftolpl is now an outer parameter of the .imach parameter
333: file after estepm. If ftolpl is small 1.e-4 and estepm too,
334: computation are long.
335:
1.209 brouard 336: Revision 1.208 2015/11/17 14:31:57 brouard
337: Summary: temporary
338:
1.208 brouard 339: Revision 1.207 2015/10/27 17:36:57 brouard
340: *** empty log message ***
341:
1.207 brouard 342: Revision 1.206 2015/10/24 07:14:11 brouard
343: *** empty log message ***
344:
1.206 brouard 345: Revision 1.205 2015/10/23 15:50:53 brouard
346: Summary: 0.98r3 some clarification for graphs on likelihood contributions
347:
1.205 brouard 348: Revision 1.204 2015/10/01 16:20:26 brouard
349: Summary: Some new graphs of contribution to likelihood
350:
1.204 brouard 351: Revision 1.203 2015/09/30 17:45:14 brouard
352: Summary: looking at better estimation of the hessian
353:
354: Also a better criteria for convergence to the period prevalence And
355: therefore adding the number of years needed to converge. (The
356: prevalence in any alive state shold sum to one
357:
1.203 brouard 358: Revision 1.202 2015/09/22 19:45:16 brouard
359: Summary: Adding some overall graph on contribution to likelihood. Might change
360:
1.202 brouard 361: Revision 1.201 2015/09/15 17:34:58 brouard
362: Summary: 0.98r0
363:
364: - Some new graphs like suvival functions
365: - Some bugs fixed like model=1+age+V2.
366:
1.201 brouard 367: Revision 1.200 2015/09/09 16:53:55 brouard
368: Summary: Big bug thanks to Flavia
369:
370: Even model=1+age+V2. did not work anymore
371:
1.200 brouard 372: Revision 1.199 2015/09/07 14:09:23 brouard
373: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
374:
1.199 brouard 375: Revision 1.198 2015/09/03 07:14:39 brouard
376: Summary: 0.98q5 Flavia
377:
1.198 brouard 378: Revision 1.197 2015/09/01 18:24:39 brouard
379: *** empty log message ***
380:
1.197 brouard 381: Revision 1.196 2015/08/18 23:17:52 brouard
382: Summary: 0.98q5
383:
1.196 brouard 384: Revision 1.195 2015/08/18 16:28:39 brouard
385: Summary: Adding a hack for testing purpose
386:
387: After reading the title, ftol and model lines, if the comment line has
388: a q, starting with #q, the answer at the end of the run is quit. It
389: permits to run test files in batch with ctest. The former workaround was
390: $ echo q | imach foo.imach
391:
1.195 brouard 392: Revision 1.194 2015/08/18 13:32:00 brouard
393: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
394:
1.194 brouard 395: Revision 1.193 2015/08/04 07:17:42 brouard
396: Summary: 0.98q4
397:
1.193 brouard 398: Revision 1.192 2015/07/16 16:49:02 brouard
399: Summary: Fixing some outputs
400:
1.192 brouard 401: Revision 1.191 2015/07/14 10:00:33 brouard
402: Summary: Some fixes
403:
1.191 brouard 404: Revision 1.190 2015/05/05 08:51:13 brouard
405: Summary: Adding digits in output parameters (7 digits instead of 6)
406:
407: Fix 1+age+.
408:
1.190 brouard 409: Revision 1.189 2015/04/30 14:45:16 brouard
410: Summary: 0.98q2
411:
1.189 brouard 412: Revision 1.188 2015/04/30 08:27:53 brouard
413: *** empty log message ***
414:
1.188 brouard 415: Revision 1.187 2015/04/29 09:11:15 brouard
416: *** empty log message ***
417:
1.187 brouard 418: Revision 1.186 2015/04/23 12:01:52 brouard
419: Summary: V1*age is working now, version 0.98q1
420:
421: Some codes had been disabled in order to simplify and Vn*age was
422: working in the optimization phase, ie, giving correct MLE parameters,
423: but, as usual, outputs were not correct and program core dumped.
424:
1.186 brouard 425: Revision 1.185 2015/03/11 13:26:42 brouard
426: Summary: Inclusion of compile and links command line for Intel Compiler
427:
1.185 brouard 428: Revision 1.184 2015/03/11 11:52:39 brouard
429: Summary: Back from Windows 8. Intel Compiler
430:
1.184 brouard 431: Revision 1.183 2015/03/10 20:34:32 brouard
432: Summary: 0.98q0, trying with directest, mnbrak fixed
433:
434: We use directest instead of original Powell test; probably no
435: incidence on the results, but better justifications;
436: We fixed Numerical Recipes mnbrak routine which was wrong and gave
437: wrong results.
438:
1.183 brouard 439: Revision 1.182 2015/02/12 08:19:57 brouard
440: Summary: Trying to keep directest which seems simpler and more general
441: Author: Nicolas Brouard
442:
1.182 brouard 443: Revision 1.181 2015/02/11 23:22:24 brouard
444: Summary: Comments on Powell added
445:
446: Author:
447:
1.181 brouard 448: Revision 1.180 2015/02/11 17:33:45 brouard
449: Summary: Finishing move from main to function (hpijx and prevalence_limit)
450:
1.180 brouard 451: Revision 1.179 2015/01/04 09:57:06 brouard
452: Summary: back to OS/X
453:
1.179 brouard 454: Revision 1.178 2015/01/04 09:35:48 brouard
455: *** empty log message ***
456:
1.178 brouard 457: Revision 1.177 2015/01/03 18:40:56 brouard
458: Summary: Still testing ilc32 on OSX
459:
1.177 brouard 460: Revision 1.176 2015/01/03 16:45:04 brouard
461: *** empty log message ***
462:
1.176 brouard 463: Revision 1.175 2015/01/03 16:33:42 brouard
464: *** empty log message ***
465:
1.175 brouard 466: Revision 1.174 2015/01/03 16:15:49 brouard
467: Summary: Still in cross-compilation
468:
1.174 brouard 469: Revision 1.173 2015/01/03 12:06:26 brouard
470: Summary: trying to detect cross-compilation
471:
1.173 brouard 472: Revision 1.172 2014/12/27 12:07:47 brouard
473: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
474:
1.172 brouard 475: Revision 1.171 2014/12/23 13:26:59 brouard
476: Summary: Back from Visual C
477:
478: Still problem with utsname.h on Windows
479:
1.171 brouard 480: Revision 1.170 2014/12/23 11:17:12 brouard
481: Summary: Cleaning some \%% back to %%
482:
483: The escape was mandatory for a specific compiler (which one?), but too many warnings.
484:
1.170 brouard 485: Revision 1.169 2014/12/22 23:08:31 brouard
486: Summary: 0.98p
487:
488: Outputs some informations on compiler used, OS etc. Testing on different platforms.
489:
1.169 brouard 490: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 491: Summary: update
1.169 brouard 492:
1.168 brouard 493: Revision 1.167 2014/12/22 13:50:56 brouard
494: Summary: Testing uname and compiler version and if compiled 32 or 64
495:
496: Testing on Linux 64
497:
1.167 brouard 498: Revision 1.166 2014/12/22 11:40:47 brouard
499: *** empty log message ***
500:
1.166 brouard 501: Revision 1.165 2014/12/16 11:20:36 brouard
502: Summary: After compiling on Visual C
503:
504: * imach.c (Module): Merging 1.61 to 1.162
505:
1.165 brouard 506: Revision 1.164 2014/12/16 10:52:11 brouard
507: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
508:
509: * imach.c (Module): Merging 1.61 to 1.162
510:
1.164 brouard 511: Revision 1.163 2014/12/16 10:30:11 brouard
512: * imach.c (Module): Merging 1.61 to 1.162
513:
1.163 brouard 514: Revision 1.162 2014/09/25 11:43:39 brouard
515: Summary: temporary backup 0.99!
516:
1.162 brouard 517: Revision 1.1 2014/09/16 11:06:58 brouard
518: Summary: With some code (wrong) for nlopt
519:
520: Author:
521:
522: Revision 1.161 2014/09/15 20:41:41 brouard
523: Summary: Problem with macro SQR on Intel compiler
524:
1.161 brouard 525: Revision 1.160 2014/09/02 09:24:05 brouard
526: *** empty log message ***
527:
1.160 brouard 528: Revision 1.159 2014/09/01 10:34:10 brouard
529: Summary: WIN32
530: Author: Brouard
531:
1.159 brouard 532: Revision 1.158 2014/08/27 17:11:51 brouard
533: *** empty log message ***
534:
1.158 brouard 535: Revision 1.157 2014/08/27 16:26:55 brouard
536: Summary: Preparing windows Visual studio version
537: Author: Brouard
538:
539: In order to compile on Visual studio, time.h is now correct and time_t
540: and tm struct should be used. difftime should be used but sometimes I
541: just make the differences in raw time format (time(&now).
542: Trying to suppress #ifdef LINUX
543: Add xdg-open for __linux in order to open default browser.
544:
1.157 brouard 545: Revision 1.156 2014/08/25 20:10:10 brouard
546: *** empty log message ***
547:
1.156 brouard 548: Revision 1.155 2014/08/25 18:32:34 brouard
549: Summary: New compile, minor changes
550: Author: Brouard
551:
1.155 brouard 552: Revision 1.154 2014/06/20 17:32:08 brouard
553: Summary: Outputs now all graphs of convergence to period prevalence
554:
1.154 brouard 555: Revision 1.153 2014/06/20 16:45:46 brouard
556: Summary: If 3 live state, convergence to period prevalence on same graph
557: Author: Brouard
558:
1.153 brouard 559: Revision 1.152 2014/06/18 17:54:09 brouard
560: Summary: open browser, use gnuplot on same dir than imach if not found in the path
561:
1.152 brouard 562: Revision 1.151 2014/06/18 16:43:30 brouard
563: *** empty log message ***
564:
1.151 brouard 565: Revision 1.150 2014/06/18 16:42:35 brouard
566: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
567: Author: brouard
568:
1.150 brouard 569: Revision 1.149 2014/06/18 15:51:14 brouard
570: Summary: Some fixes in parameter files errors
571: Author: Nicolas Brouard
572:
1.149 brouard 573: Revision 1.148 2014/06/17 17:38:48 brouard
574: Summary: Nothing new
575: Author: Brouard
576:
577: Just a new packaging for OS/X version 0.98nS
578:
1.148 brouard 579: Revision 1.147 2014/06/16 10:33:11 brouard
580: *** empty log message ***
581:
1.147 brouard 582: Revision 1.146 2014/06/16 10:20:28 brouard
583: Summary: Merge
584: Author: Brouard
585:
586: Merge, before building revised version.
587:
1.146 brouard 588: Revision 1.145 2014/06/10 21:23:15 brouard
589: Summary: Debugging with valgrind
590: Author: Nicolas Brouard
591:
592: Lot of changes in order to output the results with some covariates
593: After the Edimburgh REVES conference 2014, it seems mandatory to
594: improve the code.
595: No more memory valgrind error but a lot has to be done in order to
596: continue the work of splitting the code into subroutines.
597: Also, decodemodel has been improved. Tricode is still not
598: optimal. nbcode should be improved. Documentation has been added in
599: the source code.
600:
1.144 brouard 601: Revision 1.143 2014/01/26 09:45:38 brouard
602: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
603:
604: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
605: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
606:
1.143 brouard 607: Revision 1.142 2014/01/26 03:57:36 brouard
608: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
609:
610: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
611:
1.142 brouard 612: Revision 1.141 2014/01/26 02:42:01 brouard
613: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
614:
1.141 brouard 615: Revision 1.140 2011/09/02 10:37:54 brouard
616: Summary: times.h is ok with mingw32 now.
617:
1.140 brouard 618: Revision 1.139 2010/06/14 07:50:17 brouard
619: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
620: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
621:
1.139 brouard 622: Revision 1.138 2010/04/30 18:19:40 brouard
623: *** empty log message ***
624:
1.138 brouard 625: Revision 1.137 2010/04/29 18:11:38 brouard
626: (Module): Checking covariates for more complex models
627: than V1+V2. A lot of change to be done. Unstable.
628:
1.137 brouard 629: Revision 1.136 2010/04/26 20:30:53 brouard
630: (Module): merging some libgsl code. Fixing computation
631: of likelione (using inter/intrapolation if mle = 0) in order to
632: get same likelihood as if mle=1.
633: Some cleaning of code and comments added.
634:
1.136 brouard 635: Revision 1.135 2009/10/29 15:33:14 brouard
636: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
637:
1.135 brouard 638: Revision 1.134 2009/10/29 13:18:53 brouard
639: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
640:
1.134 brouard 641: Revision 1.133 2009/07/06 10:21:25 brouard
642: just nforces
643:
1.133 brouard 644: Revision 1.132 2009/07/06 08:22:05 brouard
645: Many tings
646:
1.132 brouard 647: Revision 1.131 2009/06/20 16:22:47 brouard
648: Some dimensions resccaled
649:
1.131 brouard 650: Revision 1.130 2009/05/26 06:44:34 brouard
651: (Module): Max Covariate is now set to 20 instead of 8. A
652: lot of cleaning with variables initialized to 0. Trying to make
653: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
654:
1.130 brouard 655: Revision 1.129 2007/08/31 13:49:27 lievre
656: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
657:
1.129 lievre 658: Revision 1.128 2006/06/30 13:02:05 brouard
659: (Module): Clarifications on computing e.j
660:
1.128 brouard 661: Revision 1.127 2006/04/28 18:11:50 brouard
662: (Module): Yes the sum of survivors was wrong since
663: imach-114 because nhstepm was no more computed in the age
664: loop. Now we define nhstepma in the age loop.
665: (Module): In order to speed up (in case of numerous covariates) we
666: compute health expectancies (without variances) in a first step
667: and then all the health expectancies with variances or standard
668: deviation (needs data from the Hessian matrices) which slows the
669: computation.
670: In the future we should be able to stop the program is only health
671: expectancies and graph are needed without standard deviations.
672:
1.127 brouard 673: Revision 1.126 2006/04/28 17:23:28 brouard
674: (Module): Yes the sum of survivors was wrong since
675: imach-114 because nhstepm was no more computed in the age
676: loop. Now we define nhstepma in the age loop.
677: Version 0.98h
678:
1.126 brouard 679: Revision 1.125 2006/04/04 15:20:31 lievre
680: Errors in calculation of health expectancies. Age was not initialized.
681: Forecasting file added.
682:
683: Revision 1.124 2006/03/22 17:13:53 lievre
684: Parameters are printed with %lf instead of %f (more numbers after the comma).
685: The log-likelihood is printed in the log file
686:
687: Revision 1.123 2006/03/20 10:52:43 brouard
688: * imach.c (Module): <title> changed, corresponds to .htm file
689: name. <head> headers where missing.
690:
691: * imach.c (Module): Weights can have a decimal point as for
692: English (a comma might work with a correct LC_NUMERIC environment,
693: otherwise the weight is truncated).
694: Modification of warning when the covariates values are not 0 or
695: 1.
696: Version 0.98g
697:
698: Revision 1.122 2006/03/20 09:45:41 brouard
699: (Module): Weights can have a decimal point as for
700: English (a comma might work with a correct LC_NUMERIC environment,
701: otherwise the weight is truncated).
702: Modification of warning when the covariates values are not 0 or
703: 1.
704: Version 0.98g
705:
706: Revision 1.121 2006/03/16 17:45:01 lievre
707: * imach.c (Module): Comments concerning covariates added
708:
709: * imach.c (Module): refinements in the computation of lli if
710: status=-2 in order to have more reliable computation if stepm is
711: not 1 month. Version 0.98f
712:
713: Revision 1.120 2006/03/16 15:10:38 lievre
714: (Module): refinements in the computation of lli if
715: status=-2 in order to have more reliable computation if stepm is
716: not 1 month. Version 0.98f
717:
718: Revision 1.119 2006/03/15 17:42:26 brouard
719: (Module): Bug if status = -2, the loglikelihood was
720: computed as likelihood omitting the logarithm. Version O.98e
721:
722: Revision 1.118 2006/03/14 18:20:07 brouard
723: (Module): varevsij Comments added explaining the second
724: table of variances if popbased=1 .
725: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
726: (Module): Function pstamp added
727: (Module): Version 0.98d
728:
729: Revision 1.117 2006/03/14 17:16:22 brouard
730: (Module): varevsij Comments added explaining the second
731: table of variances if popbased=1 .
732: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
733: (Module): Function pstamp added
734: (Module): Version 0.98d
735:
736: Revision 1.116 2006/03/06 10:29:27 brouard
737: (Module): Variance-covariance wrong links and
738: varian-covariance of ej. is needed (Saito).
739:
740: Revision 1.115 2006/02/27 12:17:45 brouard
741: (Module): One freematrix added in mlikeli! 0.98c
742:
743: Revision 1.114 2006/02/26 12:57:58 brouard
744: (Module): Some improvements in processing parameter
745: filename with strsep.
746:
747: Revision 1.113 2006/02/24 14:20:24 brouard
748: (Module): Memory leaks checks with valgrind and:
749: datafile was not closed, some imatrix were not freed and on matrix
750: allocation too.
751:
752: Revision 1.112 2006/01/30 09:55:26 brouard
753: (Module): Back to gnuplot.exe instead of wgnuplot.exe
754:
755: Revision 1.111 2006/01/25 20:38:18 brouard
756: (Module): Lots of cleaning and bugs added (Gompertz)
757: (Module): Comments can be added in data file. Missing date values
758: can be a simple dot '.'.
759:
760: Revision 1.110 2006/01/25 00:51:50 brouard
761: (Module): Lots of cleaning and bugs added (Gompertz)
762:
763: Revision 1.109 2006/01/24 19:37:15 brouard
764: (Module): Comments (lines starting with a #) are allowed in data.
765:
766: Revision 1.108 2006/01/19 18:05:42 lievre
767: Gnuplot problem appeared...
768: To be fixed
769:
770: Revision 1.107 2006/01/19 16:20:37 brouard
771: Test existence of gnuplot in imach path
772:
773: Revision 1.106 2006/01/19 13:24:36 brouard
774: Some cleaning and links added in html output
775:
776: Revision 1.105 2006/01/05 20:23:19 lievre
777: *** empty log message ***
778:
779: Revision 1.104 2005/09/30 16:11:43 lievre
780: (Module): sump fixed, loop imx fixed, and simplifications.
781: (Module): If the status is missing at the last wave but we know
782: that the person is alive, then we can code his/her status as -2
783: (instead of missing=-1 in earlier versions) and his/her
784: contributions to the likelihood is 1 - Prob of dying from last
785: health status (= 1-p13= p11+p12 in the easiest case of somebody in
786: the healthy state at last known wave). Version is 0.98
787:
788: Revision 1.103 2005/09/30 15:54:49 lievre
789: (Module): sump fixed, loop imx fixed, and simplifications.
790:
791: Revision 1.102 2004/09/15 17:31:30 brouard
792: Add the possibility to read data file including tab characters.
793:
794: Revision 1.101 2004/09/15 10:38:38 brouard
795: Fix on curr_time
796:
797: Revision 1.100 2004/07/12 18:29:06 brouard
798: Add version for Mac OS X. Just define UNIX in Makefile
799:
800: Revision 1.99 2004/06/05 08:57:40 brouard
801: *** empty log message ***
802:
803: Revision 1.98 2004/05/16 15:05:56 brouard
804: New version 0.97 . First attempt to estimate force of mortality
805: directly from the data i.e. without the need of knowing the health
806: state at each age, but using a Gompertz model: log u =a + b*age .
807: This is the basic analysis of mortality and should be done before any
808: other analysis, in order to test if the mortality estimated from the
809: cross-longitudinal survey is different from the mortality estimated
810: from other sources like vital statistic data.
811:
812: The same imach parameter file can be used but the option for mle should be -3.
813:
1.133 brouard 814: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 815: former routines in order to include the new code within the former code.
816:
817: The output is very simple: only an estimate of the intercept and of
818: the slope with 95% confident intervals.
819:
820: Current limitations:
821: A) Even if you enter covariates, i.e. with the
822: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
823: B) There is no computation of Life Expectancy nor Life Table.
824:
825: Revision 1.97 2004/02/20 13:25:42 lievre
826: Version 0.96d. Population forecasting command line is (temporarily)
827: suppressed.
828:
829: Revision 1.96 2003/07/15 15:38:55 brouard
830: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
831: rewritten within the same printf. Workaround: many printfs.
832:
833: Revision 1.95 2003/07/08 07:54:34 brouard
834: * imach.c (Repository):
835: (Repository): Using imachwizard code to output a more meaningful covariance
836: matrix (cov(a12,c31) instead of numbers.
837:
838: Revision 1.94 2003/06/27 13:00:02 brouard
839: Just cleaning
840:
841: Revision 1.93 2003/06/25 16:33:55 brouard
842: (Module): On windows (cygwin) function asctime_r doesn't
843: exist so I changed back to asctime which exists.
844: (Module): Version 0.96b
845:
846: Revision 1.92 2003/06/25 16:30:45 brouard
847: (Module): On windows (cygwin) function asctime_r doesn't
848: exist so I changed back to asctime which exists.
849:
850: Revision 1.91 2003/06/25 15:30:29 brouard
851: * imach.c (Repository): Duplicated warning errors corrected.
852: (Repository): Elapsed time after each iteration is now output. It
853: helps to forecast when convergence will be reached. Elapsed time
854: is stamped in powell. We created a new html file for the graphs
855: concerning matrix of covariance. It has extension -cov.htm.
856:
857: Revision 1.90 2003/06/24 12:34:15 brouard
858: (Module): Some bugs corrected for windows. Also, when
859: mle=-1 a template is output in file "or"mypar.txt with the design
860: of the covariance matrix to be input.
861:
862: Revision 1.89 2003/06/24 12:30:52 brouard
863: (Module): Some bugs corrected for windows. Also, when
864: mle=-1 a template is output in file "or"mypar.txt with the design
865: of the covariance matrix to be input.
866:
867: Revision 1.88 2003/06/23 17:54:56 brouard
868: * 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.
869:
870: Revision 1.87 2003/06/18 12:26:01 brouard
871: Version 0.96
872:
873: Revision 1.86 2003/06/17 20:04:08 brouard
874: (Module): Change position of html and gnuplot routines and added
875: routine fileappend.
876:
877: Revision 1.85 2003/06/17 13:12:43 brouard
878: * imach.c (Repository): Check when date of death was earlier that
879: current date of interview. It may happen when the death was just
880: prior to the death. In this case, dh was negative and likelihood
881: was wrong (infinity). We still send an "Error" but patch by
882: assuming that the date of death was just one stepm after the
883: interview.
884: (Repository): Because some people have very long ID (first column)
885: we changed int to long in num[] and we added a new lvector for
886: memory allocation. But we also truncated to 8 characters (left
887: truncation)
888: (Repository): No more line truncation errors.
889:
890: Revision 1.84 2003/06/13 21:44:43 brouard
891: * imach.c (Repository): Replace "freqsummary" at a correct
892: place. It differs from routine "prevalence" which may be called
893: many times. Probs is memory consuming and must be used with
894: parcimony.
895: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
896:
897: Revision 1.83 2003/06/10 13:39:11 lievre
898: *** empty log message ***
899:
900: Revision 1.82 2003/06/05 15:57:20 brouard
901: Add log in imach.c and fullversion number is now printed.
902:
903: */
904: /*
905: Interpolated Markov Chain
906:
907: Short summary of the programme:
908:
1.227 brouard 909: This program computes Healthy Life Expectancies or State-specific
910: (if states aren't health statuses) Expectancies from
911: cross-longitudinal data. Cross-longitudinal data consist in:
912:
913: -1- a first survey ("cross") where individuals from different ages
914: are interviewed on their health status or degree of disability (in
915: the case of a health survey which is our main interest)
916:
917: -2- at least a second wave of interviews ("longitudinal") which
918: measure each change (if any) in individual health status. Health
919: expectancies are computed from the time spent in each health state
920: according to a model. More health states you consider, more time is
921: necessary to reach the Maximum Likelihood of the parameters involved
922: in the model. The simplest model is the multinomial logistic model
923: where pij is the probability to be observed in state j at the second
924: wave conditional to be observed in state i at the first
925: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
926: etc , where 'age' is age and 'sex' is a covariate. If you want to
927: have a more complex model than "constant and age", you should modify
928: the program where the markup *Covariates have to be included here
929: again* invites you to do it. More covariates you add, slower the
1.126 brouard 930: convergence.
931:
932: The advantage of this computer programme, compared to a simple
933: multinomial logistic model, is clear when the delay between waves is not
934: identical for each individual. Also, if a individual missed an
935: intermediate interview, the information is lost, but taken into
936: account using an interpolation or extrapolation.
937:
938: hPijx is the probability to be observed in state i at age x+h
939: conditional to the observed state i at age x. The delay 'h' can be
940: split into an exact number (nh*stepm) of unobserved intermediate
941: states. This elementary transition (by month, quarter,
942: semester or year) is modelled as a multinomial logistic. The hPx
943: matrix is simply the matrix product of nh*stepm elementary matrices
944: and the contribution of each individual to the likelihood is simply
945: hPijx.
946:
947: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 948: of the life expectancies. It also computes the period (stable) prevalence.
949:
950: Back prevalence and projections:
1.227 brouard 951:
952: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
953: double agemaxpar, double ftolpl, int *ncvyearp, double
954: dateprev1,double dateprev2, int firstpass, int lastpass, int
955: mobilavproj)
956:
957: Computes the back prevalence limit for any combination of
958: covariate values k at any age between ageminpar and agemaxpar and
959: returns it in **bprlim. In the loops,
960:
961: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
962: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
963:
964: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 965: Computes for any combination of covariates k and any age between bage and fage
966: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
967: oldm=oldms;savm=savms;
1.227 brouard 968:
1.267 brouard 969: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.218 brouard 970: Computes the transition matrix starting at age 'age' over
971: 'nhstepm*hstepm*stepm' months (i.e. until
972: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 973: nhstepm*hstepm matrices.
974:
975: Returns p3mat[i][j][h] after calling
976: p3mat[i][j][h]=matprod2(newm,
977: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
978: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
979: oldm);
1.226 brouard 980:
981: Important routines
982:
983: - func (or funcone), computes logit (pij) distinguishing
984: o fixed variables (single or product dummies or quantitative);
985: o varying variables by:
986: (1) wave (single, product dummies, quantitative),
987: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
988: % fixed dummy (treated) or quantitative (not done because time-consuming);
989: % varying dummy (not done) or quantitative (not done);
990: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
991: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
992: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
993: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
994: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 995:
1.226 brouard 996:
997:
1.133 brouard 998: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
999: Institut national d'études démographiques, Paris.
1.126 brouard 1000: This software have been partly granted by Euro-REVES, a concerted action
1001: from the European Union.
1002: It is copyrighted identically to a GNU software product, ie programme and
1003: software can be distributed freely for non commercial use. Latest version
1004: can be accessed at http://euroreves.ined.fr/imach .
1005:
1006: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1007: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1008:
1009: **********************************************************************/
1010: /*
1011: main
1012: read parameterfile
1013: read datafile
1014: concatwav
1015: freqsummary
1016: if (mle >= 1)
1017: mlikeli
1018: print results files
1019: if mle==1
1020: computes hessian
1021: read end of parameter file: agemin, agemax, bage, fage, estepm
1022: begin-prev-date,...
1023: open gnuplot file
1024: open html file
1.145 brouard 1025: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1026: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1027: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1028: freexexit2 possible for memory heap.
1029:
1030: h Pij x | pij_nom ficrestpij
1031: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1032: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1033: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1034:
1035: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1036: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1037: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1038: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1039: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1040:
1.126 brouard 1041: forecasting if prevfcast==1 prevforecast call prevalence()
1042: health expectancies
1043: Variance-covariance of DFLE
1044: prevalence()
1045: movingaverage()
1046: varevsij()
1047: if popbased==1 varevsij(,popbased)
1048: total life expectancies
1049: Variance of period (stable) prevalence
1050: end
1051: */
1052:
1.187 brouard 1053: /* #define DEBUG */
1054: /* #define DEBUGBRENT */
1.203 brouard 1055: /* #define DEBUGLINMIN */
1056: /* #define DEBUGHESS */
1057: #define DEBUGHESSIJ
1.224 brouard 1058: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 1059: #define POWELL /* Instead of NLOPT */
1.224 brouard 1060: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 1061: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1062: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 1063:
1064: #include <math.h>
1065: #include <stdio.h>
1066: #include <stdlib.h>
1067: #include <string.h>
1.226 brouard 1068: #include <ctype.h>
1.159 brouard 1069:
1070: #ifdef _WIN32
1071: #include <io.h>
1.172 brouard 1072: #include <windows.h>
1073: #include <tchar.h>
1.159 brouard 1074: #else
1.126 brouard 1075: #include <unistd.h>
1.159 brouard 1076: #endif
1.126 brouard 1077:
1078: #include <limits.h>
1079: #include <sys/types.h>
1.171 brouard 1080:
1081: #if defined(__GNUC__)
1082: #include <sys/utsname.h> /* Doesn't work on Windows */
1083: #endif
1084:
1.126 brouard 1085: #include <sys/stat.h>
1086: #include <errno.h>
1.159 brouard 1087: /* extern int errno; */
1.126 brouard 1088:
1.157 brouard 1089: /* #ifdef LINUX */
1090: /* #include <time.h> */
1091: /* #include "timeval.h" */
1092: /* #else */
1093: /* #include <sys/time.h> */
1094: /* #endif */
1095:
1.126 brouard 1096: #include <time.h>
1097:
1.136 brouard 1098: #ifdef GSL
1099: #include <gsl/gsl_errno.h>
1100: #include <gsl/gsl_multimin.h>
1101: #endif
1102:
1.167 brouard 1103:
1.162 brouard 1104: #ifdef NLOPT
1105: #include <nlopt.h>
1106: typedef struct {
1107: double (* function)(double [] );
1108: } myfunc_data ;
1109: #endif
1110:
1.126 brouard 1111: /* #include <libintl.h> */
1112: /* #define _(String) gettext (String) */
1113:
1.251 brouard 1114: #define MAXLINE 2048 /* Was 256 and 1024. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 1115:
1116: #define GNUPLOTPROGRAM "gnuplot"
1117: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1118: #define FILENAMELENGTH 132
1119:
1120: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1121: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1122:
1.144 brouard 1123: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
1124: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 1125:
1126: #define NINTERVMAX 8
1.144 brouard 1127: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1128: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1.291 brouard 1129: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 1130: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 1131: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1132: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.290 brouard 1133: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1.144 brouard 1134: #define YEARM 12. /**< Number of months per year */
1.218 brouard 1135: /* #define AGESUP 130 */
1.288 brouard 1136: /* #define AGESUP 150 */
1137: #define AGESUP 200
1.268 brouard 1138: #define AGEINF 0
1.218 brouard 1139: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 1140: #define AGEBASE 40
1.194 brouard 1141: #define AGEOVERFLOW 1.e20
1.164 brouard 1142: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 1143: #ifdef _WIN32
1144: #define DIRSEPARATOR '\\'
1145: #define CHARSEPARATOR "\\"
1146: #define ODIRSEPARATOR '/'
1147: #else
1.126 brouard 1148: #define DIRSEPARATOR '/'
1149: #define CHARSEPARATOR "/"
1150: #define ODIRSEPARATOR '\\'
1151: #endif
1152:
1.309 ! brouard 1153: /* $Id: imach.c,v 1.308 2021/03/31 13:11:57 brouard Exp $ */
1.126 brouard 1154: /* $State: Exp $ */
1.196 brouard 1155: #include "version.h"
1156: char version[]=__IMACH_VERSION__;
1.308 brouard 1157: 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.309 ! brouard 1158: char fullversion[]="$Revision: 1.308 $ $Date: 2021/03/31 13:11:57 $";
1.126 brouard 1159: char strstart[80];
1160: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 1161: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 1162: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 1163: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
1164: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
1165: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 1166: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
1167: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 1168: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1169: int cptcovprodnoage=0; /**< Number of covariate products without age */
1170: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 1171: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1172: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 1173: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 brouard 1174: int nsd=0; /**< Total number of single dummy variables (output) */
1175: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 1176: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 1177: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 1178: int ntveff=0; /**< ntveff number of effective time varying variables */
1179: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 1180: int cptcov=0; /* Working variable */
1.290 brouard 1181: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1.218 brouard 1182: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.302 brouard 1183: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1.126 brouard 1184: int nlstate=2; /* Number of live states */
1185: int ndeath=1; /* Number of dead states */
1.130 brouard 1186: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 1187: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 1188: int popbased=0;
1189:
1190: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 1191: int maxwav=0; /* Maxim number of waves */
1192: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1193: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1194: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 1195: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 1196: int mle=1, weightopt=0;
1.126 brouard 1197: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1198: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1199: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1200: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 1201: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 1202: int selected(int kvar); /* Is covariate kvar selected for printing results */
1203:
1.130 brouard 1204: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 1205: double **matprod2(); /* test */
1.126 brouard 1206: double **oldm, **newm, **savm; /* Working pointers to matrices */
1207: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 1208: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1209:
1.136 brouard 1210: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 1211: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 1212: FILE *ficlog, *ficrespow;
1.130 brouard 1213: int globpr=0; /* Global variable for printing or not */
1.126 brouard 1214: double fretone; /* Only one call to likelihood */
1.130 brouard 1215: long ipmx=0; /* Number of contributions */
1.126 brouard 1216: double sw; /* Sum of weights */
1217: char filerespow[FILENAMELENGTH];
1218: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1219: FILE *ficresilk;
1220: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1221: FILE *ficresprobmorprev;
1222: FILE *fichtm, *fichtmcov; /* Html File */
1223: FILE *ficreseij;
1224: char filerese[FILENAMELENGTH];
1225: FILE *ficresstdeij;
1226: char fileresstde[FILENAMELENGTH];
1227: FILE *ficrescveij;
1228: char filerescve[FILENAMELENGTH];
1229: FILE *ficresvij;
1230: char fileresv[FILENAMELENGTH];
1.269 brouard 1231:
1.126 brouard 1232: char title[MAXLINE];
1.234 brouard 1233: char model[MAXLINE]; /**< The model line */
1.217 brouard 1234: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1235: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1236: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1237: char command[FILENAMELENGTH];
1238: int outcmd=0;
1239:
1.217 brouard 1240: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1241: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1242: char filelog[FILENAMELENGTH]; /* Log file */
1243: char filerest[FILENAMELENGTH];
1244: char fileregp[FILENAMELENGTH];
1245: char popfile[FILENAMELENGTH];
1246:
1247: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1248:
1.157 brouard 1249: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1250: /* struct timezone tzp; */
1251: /* extern int gettimeofday(); */
1252: struct tm tml, *gmtime(), *localtime();
1253:
1254: extern time_t time();
1255:
1256: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1257: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1258: struct tm tm;
1259:
1.126 brouard 1260: char strcurr[80], strfor[80];
1261:
1262: char *endptr;
1263: long lval;
1264: double dval;
1265:
1266: #define NR_END 1
1267: #define FREE_ARG char*
1268: #define FTOL 1.0e-10
1269:
1270: #define NRANSI
1.240 brouard 1271: #define ITMAX 200
1272: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1273:
1274: #define TOL 2.0e-4
1275:
1276: #define CGOLD 0.3819660
1277: #define ZEPS 1.0e-10
1278: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1279:
1280: #define GOLD 1.618034
1281: #define GLIMIT 100.0
1282: #define TINY 1.0e-20
1283:
1284: static double maxarg1,maxarg2;
1285: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1286: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1287:
1288: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1289: #define rint(a) floor(a+0.5)
1.166 brouard 1290: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1291: #define mytinydouble 1.0e-16
1.166 brouard 1292: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1293: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1294: /* static double dsqrarg; */
1295: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1296: static double sqrarg;
1297: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1298: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1299: int agegomp= AGEGOMP;
1300:
1301: int imx;
1302: int stepm=1;
1303: /* Stepm, step in month: minimum step interpolation*/
1304:
1305: int estepm;
1306: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1307:
1308: int m,nb;
1309: long *num;
1.197 brouard 1310: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1311: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1312: covariate for which somebody answered excluding
1313: undefined. Usually 2: 0 and 1. */
1314: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1315: covariate for which somebody answered including
1316: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1317: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1318: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1319: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1320: double *ageexmed,*agecens;
1321: double dateintmean=0;
1.296 brouard 1322: double anprojd, mprojd, jprojd; /* For eventual projections */
1323: double anprojf, mprojf, jprojf;
1.126 brouard 1324:
1.296 brouard 1325: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1326: double anbackf, mbackf, jbackf;
1327: double jintmean,mintmean,aintmean;
1.126 brouard 1328: double *weight;
1329: int **s; /* Status */
1.141 brouard 1330: double *agedc;
1.145 brouard 1331: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1332: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1333: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.268 brouard 1334: double **coqvar; /* Fixed quantitative covariate nqv */
1335: double ***cotvar; /* Time varying covariate ntv */
1.225 brouard 1336: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1337: double idx;
1338: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.234 brouard 1339: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1340: /*k 1 2 3 4 5 6 7 8 9 */
1341: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1342: /* Tndvar[k] 1 2 3 4 5 */
1343: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */
1344: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */
1345: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */
1346: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */
1347: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */
1348: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1349: /* Tprod[i]=k 4 7 */
1350: /* Tage[i]=k 5 8 */
1351: /* */
1352: /* Type */
1353: /* V 1 2 3 4 5 */
1354: /* F F V V V */
1355: /* D Q D D Q */
1356: /* */
1357: int *TvarsD;
1358: int *TvarsDind;
1359: int *TvarsQ;
1360: int *TvarsQind;
1361:
1.235 brouard 1362: #define MAXRESULTLINES 10
1363: int nresult=0;
1.258 brouard 1364: int parameterline=0; /* # of the parameter (type) line */
1.235 brouard 1365: int TKresult[MAXRESULTLINES];
1.237 brouard 1366: int Tresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1367: int Tinvresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1.235 brouard 1368: int Tvresult[MAXRESULTLINES][NCOVMAX]; /* For dummy variable , variable # (output) */
1369: double Tqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.237 brouard 1370: double Tqinvresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.235 brouard 1371: int Tvqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , variable # (output) */
1372:
1.234 brouard 1373: /* 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 1374: 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 */
1375: 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 */
1376: 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 */
1377: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1378: 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 */
1379: 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 1380: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1381: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1382: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1383: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1384: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1385: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1386: 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 */
1387: 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 */
1388:
1.230 brouard 1389: int *Tvarsel; /**< Selected covariates for output */
1390: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1391: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1392: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1393: 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 1394: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1395: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1396: int *Tage;
1.227 brouard 1397: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1398: 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 1399: 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*/
1400: 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 1401: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1402: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1403: int **Tvard;
1404: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1405: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1406: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1407: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1408: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1409: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1410: double *lsurv, *lpop, *tpop;
1411:
1.231 brouard 1412: #define FD 1; /* Fixed dummy covariate */
1413: #define FQ 2; /* Fixed quantitative covariate */
1414: #define FP 3; /* Fixed product covariate */
1415: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1416: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1417: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1418: #define VD 10; /* Varying dummy covariate */
1419: #define VQ 11; /* Varying quantitative covariate */
1420: #define VP 12; /* Varying product covariate */
1421: #define VPDD 13; /* Varying product dummy*dummy covariate */
1422: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1423: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1424: #define APFD 16; /* Age product * fixed dummy covariate */
1425: #define APFQ 17; /* Age product * fixed quantitative covariate */
1426: #define APVD 18; /* Age product * varying dummy covariate */
1427: #define APVQ 19; /* Age product * varying quantitative covariate */
1428:
1429: #define FTYPE 1; /* Fixed covariate */
1430: #define VTYPE 2; /* Varying covariate (loop in wave) */
1431: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1432:
1433: struct kmodel{
1434: int maintype; /* main type */
1435: int subtype; /* subtype */
1436: };
1437: struct kmodel modell[NCOVMAX];
1438:
1.143 brouard 1439: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1440: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1441:
1442: /**************** split *************************/
1443: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1444: {
1445: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1446: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1447: */
1448: char *ss; /* pointer */
1.186 brouard 1449: int l1=0, l2=0; /* length counters */
1.126 brouard 1450:
1451: l1 = strlen(path ); /* length of path */
1452: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1453: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1454: if ( ss == NULL ) { /* no directory, so determine current directory */
1455: strcpy( name, path ); /* we got the fullname name because no directory */
1456: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1457: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1458: /* get current working directory */
1459: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1460: #ifdef WIN32
1461: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1462: #else
1463: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1464: #endif
1.126 brouard 1465: return( GLOCK_ERROR_GETCWD );
1466: }
1467: /* got dirc from getcwd*/
1468: printf(" DIRC = %s \n",dirc);
1.205 brouard 1469: } else { /* strip directory from path */
1.126 brouard 1470: ss++; /* after this, the filename */
1471: l2 = strlen( ss ); /* length of filename */
1472: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1473: strcpy( name, ss ); /* save file name */
1474: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1475: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1476: printf(" DIRC2 = %s \n",dirc);
1477: }
1478: /* We add a separator at the end of dirc if not exists */
1479: l1 = strlen( dirc ); /* length of directory */
1480: if( dirc[l1-1] != DIRSEPARATOR ){
1481: dirc[l1] = DIRSEPARATOR;
1482: dirc[l1+1] = 0;
1483: printf(" DIRC3 = %s \n",dirc);
1484: }
1485: ss = strrchr( name, '.' ); /* find last / */
1486: if (ss >0){
1487: ss++;
1488: strcpy(ext,ss); /* save extension */
1489: l1= strlen( name);
1490: l2= strlen(ss)+1;
1491: strncpy( finame, name, l1-l2);
1492: finame[l1-l2]= 0;
1493: }
1494:
1495: return( 0 ); /* we're done */
1496: }
1497:
1498:
1499: /******************************************/
1500:
1501: void replace_back_to_slash(char *s, char*t)
1502: {
1503: int i;
1504: int lg=0;
1505: i=0;
1506: lg=strlen(t);
1507: for(i=0; i<= lg; i++) {
1508: (s[i] = t[i]);
1509: if (t[i]== '\\') s[i]='/';
1510: }
1511: }
1512:
1.132 brouard 1513: char *trimbb(char *out, char *in)
1.137 brouard 1514: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1515: char *s;
1516: s=out;
1517: while (*in != '\0'){
1.137 brouard 1518: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1519: in++;
1520: }
1521: *out++ = *in++;
1522: }
1523: *out='\0';
1524: return s;
1525: }
1526:
1.187 brouard 1527: /* char *substrchaine(char *out, char *in, char *chain) */
1528: /* { */
1529: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1530: /* char *s, *t; */
1531: /* t=in;s=out; */
1532: /* while ((*in != *chain) && (*in != '\0')){ */
1533: /* *out++ = *in++; */
1534: /* } */
1535:
1536: /* /\* *in matches *chain *\/ */
1537: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1538: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1539: /* } */
1540: /* in--; chain--; */
1541: /* while ( (*in != '\0')){ */
1542: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1543: /* *out++ = *in++; */
1544: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1545: /* } */
1546: /* *out='\0'; */
1547: /* out=s; */
1548: /* return out; */
1549: /* } */
1550: char *substrchaine(char *out, char *in, char *chain)
1551: {
1552: /* Substract chain 'chain' from 'in', return and output 'out' */
1553: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1554:
1555: char *strloc;
1556:
1557: strcpy (out, in);
1558: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1559: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1560: if(strloc != NULL){
1561: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1562: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1563: /* strcpy (strloc, strloc +strlen(chain));*/
1564: }
1565: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1566: return out;
1567: }
1568:
1569:
1.145 brouard 1570: char *cutl(char *blocc, char *alocc, char *in, char occ)
1571: {
1.187 brouard 1572: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1573: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1574: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1575: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1576: */
1.160 brouard 1577: char *s, *t;
1.145 brouard 1578: t=in;s=in;
1579: while ((*in != occ) && (*in != '\0')){
1580: *alocc++ = *in++;
1581: }
1582: if( *in == occ){
1583: *(alocc)='\0';
1584: s=++in;
1585: }
1586:
1587: if (s == t) {/* occ not found */
1588: *(alocc-(in-s))='\0';
1589: in=s;
1590: }
1591: while ( *in != '\0'){
1592: *blocc++ = *in++;
1593: }
1594:
1595: *blocc='\0';
1596: return t;
1597: }
1.137 brouard 1598: char *cutv(char *blocc, char *alocc, char *in, char occ)
1599: {
1.187 brouard 1600: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1601: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1602: gives blocc="abcdef2ghi" and alocc="j".
1603: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1604: */
1605: char *s, *t;
1606: t=in;s=in;
1607: while (*in != '\0'){
1608: while( *in == occ){
1609: *blocc++ = *in++;
1610: s=in;
1611: }
1612: *blocc++ = *in++;
1613: }
1614: if (s == t) /* occ not found */
1615: *(blocc-(in-s))='\0';
1616: else
1617: *(blocc-(in-s)-1)='\0';
1618: in=s;
1619: while ( *in != '\0'){
1620: *alocc++ = *in++;
1621: }
1622:
1623: *alocc='\0';
1624: return s;
1625: }
1626:
1.126 brouard 1627: int nbocc(char *s, char occ)
1628: {
1629: int i,j=0;
1630: int lg=20;
1631: i=0;
1632: lg=strlen(s);
1633: for(i=0; i<= lg; i++) {
1.234 brouard 1634: if (s[i] == occ ) j++;
1.126 brouard 1635: }
1636: return j;
1637: }
1638:
1.137 brouard 1639: /* void cutv(char *u,char *v, char*t, char occ) */
1640: /* { */
1641: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1642: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1643: /* gives u="abcdef2ghi" and v="j" *\/ */
1644: /* int i,lg,j,p=0; */
1645: /* i=0; */
1646: /* lg=strlen(t); */
1647: /* for(j=0; j<=lg-1; j++) { */
1648: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1649: /* } */
1.126 brouard 1650:
1.137 brouard 1651: /* for(j=0; j<p; j++) { */
1652: /* (u[j] = t[j]); */
1653: /* } */
1654: /* u[p]='\0'; */
1.126 brouard 1655:
1.137 brouard 1656: /* for(j=0; j<= lg; j++) { */
1657: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1658: /* } */
1659: /* } */
1.126 brouard 1660:
1.160 brouard 1661: #ifdef _WIN32
1662: char * strsep(char **pp, const char *delim)
1663: {
1664: char *p, *q;
1665:
1666: if ((p = *pp) == NULL)
1667: return 0;
1668: if ((q = strpbrk (p, delim)) != NULL)
1669: {
1670: *pp = q + 1;
1671: *q = '\0';
1672: }
1673: else
1674: *pp = 0;
1675: return p;
1676: }
1677: #endif
1678:
1.126 brouard 1679: /********************** nrerror ********************/
1680:
1681: void nrerror(char error_text[])
1682: {
1683: fprintf(stderr,"ERREUR ...\n");
1684: fprintf(stderr,"%s\n",error_text);
1685: exit(EXIT_FAILURE);
1686: }
1687: /*********************** vector *******************/
1688: double *vector(int nl, int nh)
1689: {
1690: double *v;
1691: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1692: if (!v) nrerror("allocation failure in vector");
1693: return v-nl+NR_END;
1694: }
1695:
1696: /************************ free vector ******************/
1697: void free_vector(double*v, int nl, int nh)
1698: {
1699: free((FREE_ARG)(v+nl-NR_END));
1700: }
1701:
1702: /************************ivector *******************************/
1703: int *ivector(long nl,long nh)
1704: {
1705: int *v;
1706: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1707: if (!v) nrerror("allocation failure in ivector");
1708: return v-nl+NR_END;
1709: }
1710:
1711: /******************free ivector **************************/
1712: void free_ivector(int *v, long nl, long nh)
1713: {
1714: free((FREE_ARG)(v+nl-NR_END));
1715: }
1716:
1717: /************************lvector *******************************/
1718: long *lvector(long nl,long nh)
1719: {
1720: long *v;
1721: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1722: if (!v) nrerror("allocation failure in ivector");
1723: return v-nl+NR_END;
1724: }
1725:
1726: /******************free lvector **************************/
1727: void free_lvector(long *v, long nl, long nh)
1728: {
1729: free((FREE_ARG)(v+nl-NR_END));
1730: }
1731:
1732: /******************* imatrix *******************************/
1733: int **imatrix(long nrl, long nrh, long ncl, long nch)
1734: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1735: {
1736: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1737: int **m;
1738:
1739: /* allocate pointers to rows */
1740: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1741: if (!m) nrerror("allocation failure 1 in matrix()");
1742: m += NR_END;
1743: m -= nrl;
1744:
1745:
1746: /* allocate rows and set pointers to them */
1747: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1748: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1749: m[nrl] += NR_END;
1750: m[nrl] -= ncl;
1751:
1752: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1753:
1754: /* return pointer to array of pointers to rows */
1755: return m;
1756: }
1757:
1758: /****************** free_imatrix *************************/
1759: void free_imatrix(m,nrl,nrh,ncl,nch)
1760: int **m;
1761: long nch,ncl,nrh,nrl;
1762: /* free an int matrix allocated by imatrix() */
1763: {
1764: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1765: free((FREE_ARG) (m+nrl-NR_END));
1766: }
1767:
1768: /******************* matrix *******************************/
1769: double **matrix(long nrl, long nrh, long ncl, long nch)
1770: {
1771: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1772: double **m;
1773:
1774: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1775: if (!m) nrerror("allocation failure 1 in matrix()");
1776: m += NR_END;
1777: m -= nrl;
1778:
1779: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1780: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1781: m[nrl] += NR_END;
1782: m[nrl] -= ncl;
1783:
1784: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1785: return m;
1.145 brouard 1786: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1787: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1788: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1789: */
1790: }
1791:
1792: /*************************free matrix ************************/
1793: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1794: {
1795: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1796: free((FREE_ARG)(m+nrl-NR_END));
1797: }
1798:
1799: /******************* ma3x *******************************/
1800: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1801: {
1802: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1803: double ***m;
1804:
1805: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1806: if (!m) nrerror("allocation failure 1 in matrix()");
1807: m += NR_END;
1808: m -= nrl;
1809:
1810: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1811: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1812: m[nrl] += NR_END;
1813: m[nrl] -= ncl;
1814:
1815: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1816:
1817: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1818: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1819: m[nrl][ncl] += NR_END;
1820: m[nrl][ncl] -= nll;
1821: for (j=ncl+1; j<=nch; j++)
1822: m[nrl][j]=m[nrl][j-1]+nlay;
1823:
1824: for (i=nrl+1; i<=nrh; i++) {
1825: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1826: for (j=ncl+1; j<=nch; j++)
1827: m[i][j]=m[i][j-1]+nlay;
1828: }
1829: return m;
1830: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1831: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1832: */
1833: }
1834:
1835: /*************************free ma3x ************************/
1836: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1837: {
1838: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1839: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1840: free((FREE_ARG)(m+nrl-NR_END));
1841: }
1842:
1843: /*************** function subdirf ***********/
1844: char *subdirf(char fileres[])
1845: {
1846: /* Caution optionfilefiname is hidden */
1847: strcpy(tmpout,optionfilefiname);
1848: strcat(tmpout,"/"); /* Add to the right */
1849: strcat(tmpout,fileres);
1850: return tmpout;
1851: }
1852:
1853: /*************** function subdirf2 ***********/
1854: char *subdirf2(char fileres[], char *preop)
1855: {
1856:
1857: /* Caution optionfilefiname is hidden */
1858: strcpy(tmpout,optionfilefiname);
1859: strcat(tmpout,"/");
1860: strcat(tmpout,preop);
1861: strcat(tmpout,fileres);
1862: return tmpout;
1863: }
1864:
1865: /*************** function subdirf3 ***********/
1866: char *subdirf3(char fileres[], char *preop, char *preop2)
1867: {
1868:
1869: /* Caution optionfilefiname is hidden */
1870: strcpy(tmpout,optionfilefiname);
1871: strcat(tmpout,"/");
1872: strcat(tmpout,preop);
1873: strcat(tmpout,preop2);
1874: strcat(tmpout,fileres);
1875: return tmpout;
1876: }
1.213 brouard 1877:
1878: /*************** function subdirfext ***********/
1879: char *subdirfext(char fileres[], char *preop, char *postop)
1880: {
1881:
1882: strcpy(tmpout,preop);
1883: strcat(tmpout,fileres);
1884: strcat(tmpout,postop);
1885: return tmpout;
1886: }
1.126 brouard 1887:
1.213 brouard 1888: /*************** function subdirfext3 ***********/
1889: char *subdirfext3(char fileres[], char *preop, char *postop)
1890: {
1891:
1892: /* Caution optionfilefiname is hidden */
1893: strcpy(tmpout,optionfilefiname);
1894: strcat(tmpout,"/");
1895: strcat(tmpout,preop);
1896: strcat(tmpout,fileres);
1897: strcat(tmpout,postop);
1898: return tmpout;
1899: }
1900:
1.162 brouard 1901: char *asc_diff_time(long time_sec, char ascdiff[])
1902: {
1903: long sec_left, days, hours, minutes;
1904: days = (time_sec) / (60*60*24);
1905: sec_left = (time_sec) % (60*60*24);
1906: hours = (sec_left) / (60*60) ;
1907: sec_left = (sec_left) %(60*60);
1908: minutes = (sec_left) /60;
1909: sec_left = (sec_left) % (60);
1910: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1911: return ascdiff;
1912: }
1913:
1.126 brouard 1914: /***************** f1dim *************************/
1915: extern int ncom;
1916: extern double *pcom,*xicom;
1917: extern double (*nrfunc)(double []);
1918:
1919: double f1dim(double x)
1920: {
1921: int j;
1922: double f;
1923: double *xt;
1924:
1925: xt=vector(1,ncom);
1926: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1927: f=(*nrfunc)(xt);
1928: free_vector(xt,1,ncom);
1929: return f;
1930: }
1931:
1932: /*****************brent *************************/
1933: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1934: {
1935: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1936: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1937: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1938: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1939: * returned function value.
1940: */
1.126 brouard 1941: int iter;
1942: double a,b,d,etemp;
1.159 brouard 1943: double fu=0,fv,fw,fx;
1.164 brouard 1944: double ftemp=0.;
1.126 brouard 1945: double p,q,r,tol1,tol2,u,v,w,x,xm;
1946: double e=0.0;
1947:
1948: a=(ax < cx ? ax : cx);
1949: b=(ax > cx ? ax : cx);
1950: x=w=v=bx;
1951: fw=fv=fx=(*f)(x);
1952: for (iter=1;iter<=ITMAX;iter++) {
1953: xm=0.5*(a+b);
1954: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1955: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1956: printf(".");fflush(stdout);
1957: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1958: #ifdef DEBUGBRENT
1.126 brouard 1959: 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);
1960: 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);
1961: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1962: #endif
1963: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1964: *xmin=x;
1965: return fx;
1966: }
1967: ftemp=fu;
1968: if (fabs(e) > tol1) {
1969: r=(x-w)*(fx-fv);
1970: q=(x-v)*(fx-fw);
1971: p=(x-v)*q-(x-w)*r;
1972: q=2.0*(q-r);
1973: if (q > 0.0) p = -p;
1974: q=fabs(q);
1975: etemp=e;
1976: e=d;
1977: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1978: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1979: else {
1.224 brouard 1980: d=p/q;
1981: u=x+d;
1982: if (u-a < tol2 || b-u < tol2)
1983: d=SIGN(tol1,xm-x);
1.126 brouard 1984: }
1985: } else {
1986: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1987: }
1988: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1989: fu=(*f)(u);
1990: if (fu <= fx) {
1991: if (u >= x) a=x; else b=x;
1992: SHFT(v,w,x,u)
1.183 brouard 1993: SHFT(fv,fw,fx,fu)
1994: } else {
1995: if (u < x) a=u; else b=u;
1996: if (fu <= fw || w == x) {
1.224 brouard 1997: v=w;
1998: w=u;
1999: fv=fw;
2000: fw=fu;
1.183 brouard 2001: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 2002: v=u;
2003: fv=fu;
1.183 brouard 2004: }
2005: }
1.126 brouard 2006: }
2007: nrerror("Too many iterations in brent");
2008: *xmin=x;
2009: return fx;
2010: }
2011:
2012: /****************** mnbrak ***********************/
2013:
2014: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2015: double (*func)(double))
1.183 brouard 2016: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2017: the downhill direction (defined by the function as evaluated at the initial points) and returns
2018: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2019: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2020: */
1.126 brouard 2021: double ulim,u,r,q, dum;
2022: double fu;
1.187 brouard 2023:
2024: double scale=10.;
2025: int iterscale=0;
2026:
2027: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2028: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2029:
2030:
2031: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2032: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2033: /* *bx = *ax - (*ax - *bx)/scale; */
2034: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2035: /* } */
2036:
1.126 brouard 2037: if (*fb > *fa) {
2038: SHFT(dum,*ax,*bx,dum)
1.183 brouard 2039: SHFT(dum,*fb,*fa,dum)
2040: }
1.126 brouard 2041: *cx=(*bx)+GOLD*(*bx-*ax);
2042: *fc=(*func)(*cx);
1.183 brouard 2043: #ifdef DEBUG
1.224 brouard 2044: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2045: 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 2046: #endif
1.224 brouard 2047: 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 2048: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 2049: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 2050: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 2051: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2052: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2053: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 2054: fu=(*func)(u);
1.163 brouard 2055: #ifdef DEBUG
2056: /* f(x)=A(x-u)**2+f(u) */
2057: double A, fparabu;
2058: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2059: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 2060: 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);
2061: 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 2062: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2063: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2064: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2065: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 2066: #endif
1.184 brouard 2067: #ifdef MNBRAKORIGINAL
1.183 brouard 2068: #else
1.191 brouard 2069: /* if (fu > *fc) { */
2070: /* #ifdef DEBUG */
2071: /* printf("mnbrak4 fu > fc \n"); */
2072: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2073: /* #endif */
2074: /* /\* 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 *\\/ *\/ */
2075: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2076: /* dum=u; /\* Shifting c and u *\/ */
2077: /* u = *cx; */
2078: /* *cx = dum; */
2079: /* dum = fu; */
2080: /* fu = *fc; */
2081: /* *fc =dum; */
2082: /* } else { /\* end *\/ */
2083: /* #ifdef DEBUG */
2084: /* printf("mnbrak3 fu < fc \n"); */
2085: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2086: /* #endif */
2087: /* dum=u; /\* Shifting c and u *\/ */
2088: /* u = *cx; */
2089: /* *cx = dum; */
2090: /* dum = fu; */
2091: /* fu = *fc; */
2092: /* *fc =dum; */
2093: /* } */
1.224 brouard 2094: #ifdef DEBUGMNBRAK
2095: double A, fparabu;
2096: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2097: fparabu= *fa - A*(*ax-u)*(*ax-u);
2098: 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);
2099: 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 2100: #endif
1.191 brouard 2101: dum=u; /* Shifting c and u */
2102: u = *cx;
2103: *cx = dum;
2104: dum = fu;
2105: fu = *fc;
2106: *fc =dum;
1.183 brouard 2107: #endif
1.162 brouard 2108: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 2109: #ifdef DEBUG
1.224 brouard 2110: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2111: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 2112: #endif
1.126 brouard 2113: fu=(*func)(u);
2114: if (fu < *fc) {
1.183 brouard 2115: #ifdef DEBUG
1.224 brouard 2116: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2117: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2118: #endif
2119: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2120: SHFT(*fb,*fc,fu,(*func)(u))
2121: #ifdef DEBUG
2122: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 2123: #endif
2124: }
1.162 brouard 2125: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 2126: #ifdef DEBUG
1.224 brouard 2127: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2128: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 2129: #endif
1.126 brouard 2130: u=ulim;
2131: fu=(*func)(u);
1.183 brouard 2132: } else { /* u could be left to b (if r > q parabola has a maximum) */
2133: #ifdef DEBUG
1.224 brouard 2134: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2135: 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 2136: #endif
1.126 brouard 2137: u=(*cx)+GOLD*(*cx-*bx);
2138: fu=(*func)(u);
1.224 brouard 2139: #ifdef DEBUG
2140: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2141: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2142: #endif
1.183 brouard 2143: } /* end tests */
1.126 brouard 2144: SHFT(*ax,*bx,*cx,u)
1.183 brouard 2145: SHFT(*fa,*fb,*fc,fu)
2146: #ifdef DEBUG
1.224 brouard 2147: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2148: 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 2149: #endif
2150: } /* 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 2151: }
2152:
2153: /*************** linmin ************************/
1.162 brouard 2154: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2155: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2156: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2157: the value of func at the returned location p . This is actually all accomplished by calling the
2158: routines mnbrak and brent .*/
1.126 brouard 2159: int ncom;
2160: double *pcom,*xicom;
2161: double (*nrfunc)(double []);
2162:
1.224 brouard 2163: #ifdef LINMINORIGINAL
1.126 brouard 2164: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 2165: #else
2166: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2167: #endif
1.126 brouard 2168: {
2169: double brent(double ax, double bx, double cx,
2170: double (*f)(double), double tol, double *xmin);
2171: double f1dim(double x);
2172: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2173: double *fc, double (*func)(double));
2174: int j;
2175: double xx,xmin,bx,ax;
2176: double fx,fb,fa;
1.187 brouard 2177:
1.203 brouard 2178: #ifdef LINMINORIGINAL
2179: #else
2180: double scale=10., axs, xxs; /* Scale added for infinity */
2181: #endif
2182:
1.126 brouard 2183: ncom=n;
2184: pcom=vector(1,n);
2185: xicom=vector(1,n);
2186: nrfunc=func;
2187: for (j=1;j<=n;j++) {
2188: pcom[j]=p[j];
1.202 brouard 2189: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 2190: }
1.187 brouard 2191:
1.203 brouard 2192: #ifdef LINMINORIGINAL
2193: xx=1.;
2194: #else
2195: axs=0.0;
2196: xxs=1.;
2197: do{
2198: xx= xxs;
2199: #endif
1.187 brouard 2200: ax=0.;
2201: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2202: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2203: /* 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)) */
2204: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2205: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2206: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2207: /* 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 2208: #ifdef LINMINORIGINAL
2209: #else
2210: if (fx != fx){
1.224 brouard 2211: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2212: printf("|");
2213: fprintf(ficlog,"|");
1.203 brouard 2214: #ifdef DEBUGLINMIN
1.224 brouard 2215: 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 2216: #endif
2217: }
1.224 brouard 2218: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 2219: #endif
2220:
1.191 brouard 2221: #ifdef DEBUGLINMIN
2222: 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 2223: 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 2224: #endif
1.224 brouard 2225: #ifdef LINMINORIGINAL
2226: #else
2227: if(fb == fx){ /* Flat function in the direction */
2228: xmin=xx;
2229: *flat=1;
2230: }else{
2231: *flat=0;
2232: #endif
2233: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2234: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2235: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2236: /* fmin = f(p[j] + xmin * xi[j]) */
2237: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2238: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2239: #ifdef DEBUG
1.224 brouard 2240: 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);
2241: 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);
2242: #endif
2243: #ifdef LINMINORIGINAL
2244: #else
2245: }
1.126 brouard 2246: #endif
1.191 brouard 2247: #ifdef DEBUGLINMIN
2248: printf("linmin end ");
1.202 brouard 2249: fprintf(ficlog,"linmin end ");
1.191 brouard 2250: #endif
1.126 brouard 2251: for (j=1;j<=n;j++) {
1.203 brouard 2252: #ifdef LINMINORIGINAL
2253: xi[j] *= xmin;
2254: #else
2255: #ifdef DEBUGLINMIN
2256: if(xxs <1.0)
2257: printf(" before xi[%d]=%12.8f", j,xi[j]);
2258: #endif
2259: 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) */
2260: #ifdef DEBUGLINMIN
2261: if(xxs <1.0)
2262: 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 );
2263: #endif
2264: #endif
1.187 brouard 2265: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2266: }
1.191 brouard 2267: #ifdef DEBUGLINMIN
1.203 brouard 2268: printf("\n");
1.191 brouard 2269: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2270: 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 2271: for (j=1;j<=n;j++) {
1.202 brouard 2272: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2273: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2274: if(j % ncovmodel == 0){
1.191 brouard 2275: printf("\n");
1.202 brouard 2276: fprintf(ficlog,"\n");
2277: }
1.191 brouard 2278: }
1.203 brouard 2279: #else
1.191 brouard 2280: #endif
1.126 brouard 2281: free_vector(xicom,1,n);
2282: free_vector(pcom,1,n);
2283: }
2284:
2285:
2286: /*************** powell ************************/
1.162 brouard 2287: /*
2288: Minimization of a function func of n variables. Input consists of an initial starting point
2289: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
2290: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
2291: such that failure to decrease by more than this amount on one iteration signals doneness. On
2292: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2293: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2294: */
1.224 brouard 2295: #ifdef LINMINORIGINAL
2296: #else
2297: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2298: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2299: #endif
1.126 brouard 2300: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2301: double (*func)(double []))
2302: {
1.224 brouard 2303: #ifdef LINMINORIGINAL
2304: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2305: double (*func)(double []));
1.224 brouard 2306: #else
1.241 brouard 2307: void linmin(double p[], double xi[], int n, double *fret,
2308: double (*func)(double []),int *flat);
1.224 brouard 2309: #endif
1.239 brouard 2310: int i,ibig,j,jk,k;
1.126 brouard 2311: double del,t,*pt,*ptt,*xit;
1.181 brouard 2312: double directest;
1.126 brouard 2313: double fp,fptt;
2314: double *xits;
2315: int niterf, itmp;
1.224 brouard 2316: #ifdef LINMINORIGINAL
2317: #else
2318:
2319: flatdir=ivector(1,n);
2320: for (j=1;j<=n;j++) flatdir[j]=0;
2321: #endif
1.126 brouard 2322:
2323: pt=vector(1,n);
2324: ptt=vector(1,n);
2325: xit=vector(1,n);
2326: xits=vector(1,n);
2327: *fret=(*func)(p);
2328: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2329: rcurr_time = time(NULL);
1.126 brouard 2330: for (*iter=1;;++(*iter)) {
1.187 brouard 2331: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2332: ibig=0;
2333: del=0.0;
1.157 brouard 2334: rlast_time=rcurr_time;
2335: /* (void) gettimeofday(&curr_time,&tzp); */
2336: rcurr_time = time(NULL);
2337: curr_time = *localtime(&rcurr_time);
2338: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2339: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2340: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2341: for (i=1;i<=n;i++) {
1.126 brouard 2342: fprintf(ficrespow," %.12lf", p[i]);
2343: }
1.239 brouard 2344: fprintf(ficrespow,"\n");fflush(ficrespow);
2345: printf("\n#model= 1 + age ");
2346: fprintf(ficlog,"\n#model= 1 + age ");
2347: if(nagesqr==1){
1.241 brouard 2348: printf(" + age*age ");
2349: fprintf(ficlog," + age*age ");
1.239 brouard 2350: }
2351: for(j=1;j <=ncovmodel-2;j++){
2352: if(Typevar[j]==0) {
2353: printf(" + V%d ",Tvar[j]);
2354: fprintf(ficlog," + V%d ",Tvar[j]);
2355: }else if(Typevar[j]==1) {
2356: printf(" + V%d*age ",Tvar[j]);
2357: fprintf(ficlog," + V%d*age ",Tvar[j]);
2358: }else if(Typevar[j]==2) {
2359: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2360: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2361: }
2362: }
1.126 brouard 2363: printf("\n");
1.239 brouard 2364: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
2365: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 2366: fprintf(ficlog,"\n");
1.239 brouard 2367: for(i=1,jk=1; i <=nlstate; i++){
2368: for(k=1; k <=(nlstate+ndeath); k++){
2369: if (k != i) {
2370: printf("%d%d ",i,k);
2371: fprintf(ficlog,"%d%d ",i,k);
2372: for(j=1; j <=ncovmodel; j++){
2373: printf("%12.7f ",p[jk]);
2374: fprintf(ficlog,"%12.7f ",p[jk]);
2375: jk++;
2376: }
2377: printf("\n");
2378: fprintf(ficlog,"\n");
2379: }
2380: }
2381: }
1.241 brouard 2382: if(*iter <=3 && *iter >1){
1.157 brouard 2383: tml = *localtime(&rcurr_time);
2384: strcpy(strcurr,asctime(&tml));
2385: rforecast_time=rcurr_time;
1.126 brouard 2386: itmp = strlen(strcurr);
2387: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 2388: strcurr[itmp-1]='\0';
1.162 brouard 2389: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2390: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2391: for(niterf=10;niterf<=30;niterf+=10){
1.241 brouard 2392: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2393: forecast_time = *localtime(&rforecast_time);
2394: strcpy(strfor,asctime(&forecast_time));
2395: itmp = strlen(strfor);
2396: if(strfor[itmp-1]=='\n')
2397: strfor[itmp-1]='\0';
2398: 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);
2399: 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 2400: }
2401: }
1.187 brouard 2402: for (i=1;i<=n;i++) { /* For each direction i */
2403: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2404: fptt=(*fret);
2405: #ifdef DEBUG
1.203 brouard 2406: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2407: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2408: #endif
1.203 brouard 2409: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2410: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2411: #ifdef LINMINORIGINAL
1.188 brouard 2412: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2413: #else
2414: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2415: flatdir[i]=flat; /* Function is vanishing in that direction i */
2416: #endif
2417: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2418: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2419: /* because that direction will be replaced unless the gain del is small */
2420: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2421: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2422: /* with the new direction. */
2423: del=fabs(fptt-(*fret));
2424: ibig=i;
1.126 brouard 2425: }
2426: #ifdef DEBUG
2427: printf("%d %.12e",i,(*fret));
2428: fprintf(ficlog,"%d %.12e",i,(*fret));
2429: for (j=1;j<=n;j++) {
1.224 brouard 2430: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2431: printf(" x(%d)=%.12e",j,xit[j]);
2432: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2433: }
2434: for(j=1;j<=n;j++) {
1.225 brouard 2435: printf(" p(%d)=%.12e",j,p[j]);
2436: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2437: }
2438: printf("\n");
2439: fprintf(ficlog,"\n");
2440: #endif
1.187 brouard 2441: } /* end loop on each direction i */
2442: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2443: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2444: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2445: for(j=1;j<=n;j++) {
1.302 brouard 2446: if(flatdir[j] >0){
2447: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2448: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2449: }
2450: /* printf("\n"); */
2451: /* fprintf(ficlog,"\n"); */
2452: }
1.243 brouard 2453: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
2454: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 2455: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2456: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2457: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2458: /* decreased of more than 3.84 */
2459: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2460: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2461: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2462:
1.188 brouard 2463: /* Starting the program with initial values given by a former maximization will simply change */
2464: /* the scales of the directions and the directions, because the are reset to canonical directions */
2465: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2466: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2467: #ifdef DEBUG
2468: int k[2],l;
2469: k[0]=1;
2470: k[1]=-1;
2471: printf("Max: %.12e",(*func)(p));
2472: fprintf(ficlog,"Max: %.12e",(*func)(p));
2473: for (j=1;j<=n;j++) {
2474: printf(" %.12e",p[j]);
2475: fprintf(ficlog," %.12e",p[j]);
2476: }
2477: printf("\n");
2478: fprintf(ficlog,"\n");
2479: for(l=0;l<=1;l++) {
2480: for (j=1;j<=n;j++) {
2481: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2482: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2483: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2484: }
2485: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2486: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2487: }
2488: #endif
2489:
1.224 brouard 2490: #ifdef LINMINORIGINAL
2491: #else
2492: free_ivector(flatdir,1,n);
2493: #endif
1.126 brouard 2494: free_vector(xit,1,n);
2495: free_vector(xits,1,n);
2496: free_vector(ptt,1,n);
2497: free_vector(pt,1,n);
2498: return;
1.192 brouard 2499: } /* enough precision */
1.240 brouard 2500: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2501: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2502: ptt[j]=2.0*p[j]-pt[j];
2503: xit[j]=p[j]-pt[j];
2504: pt[j]=p[j];
2505: }
1.181 brouard 2506: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2507: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2508: if (*iter <=4) {
1.225 brouard 2509: #else
2510: #endif
1.224 brouard 2511: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2512: #else
1.161 brouard 2513: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2514: #endif
1.162 brouard 2515: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2516: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2517: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2518: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2519: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2520: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2521: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2522: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2523: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2524: /* Even if f3 <f1, directest can be negative and t >0 */
2525: /* mu² and del² are equal when f3=f1 */
2526: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2527: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2528: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2529: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2530: #ifdef NRCORIGINAL
2531: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2532: #else
2533: 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 2534: t= t- del*SQR(fp-fptt);
1.183 brouard 2535: #endif
1.202 brouard 2536: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2537: #ifdef DEBUG
1.181 brouard 2538: 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);
2539: 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 2540: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2541: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2542: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2543: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2544: 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);
2545: 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);
2546: #endif
1.183 brouard 2547: #ifdef POWELLORIGINAL
2548: if (t < 0.0) { /* Then we use it for new direction */
2549: #else
1.182 brouard 2550: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2551: 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 2552: 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 2553: 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 2554: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2555: }
1.181 brouard 2556: if (directest < 0.0) { /* Then we use it for new direction */
2557: #endif
1.191 brouard 2558: #ifdef DEBUGLINMIN
1.234 brouard 2559: printf("Before linmin in direction P%d-P0\n",n);
2560: for (j=1;j<=n;j++) {
2561: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2562: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2563: if(j % ncovmodel == 0){
2564: printf("\n");
2565: fprintf(ficlog,"\n");
2566: }
2567: }
1.224 brouard 2568: #endif
2569: #ifdef LINMINORIGINAL
1.234 brouard 2570: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2571: #else
1.234 brouard 2572: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2573: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2574: #endif
1.234 brouard 2575:
1.191 brouard 2576: #ifdef DEBUGLINMIN
1.234 brouard 2577: for (j=1;j<=n;j++) {
2578: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2579: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2580: if(j % ncovmodel == 0){
2581: printf("\n");
2582: fprintf(ficlog,"\n");
2583: }
2584: }
1.224 brouard 2585: #endif
1.234 brouard 2586: for (j=1;j<=n;j++) {
2587: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2588: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2589: }
1.224 brouard 2590: #ifdef LINMINORIGINAL
2591: #else
1.234 brouard 2592: for (j=1, flatd=0;j<=n;j++) {
2593: if(flatdir[j]>0)
2594: flatd++;
2595: }
2596: if(flatd >0){
1.255 brouard 2597: printf("%d flat directions: ",flatd);
2598: fprintf(ficlog,"%d flat directions :",flatd);
1.234 brouard 2599: for (j=1;j<=n;j++) {
2600: if(flatdir[j]>0){
2601: printf("%d ",j);
2602: fprintf(ficlog,"%d ",j);
2603: }
2604: }
2605: printf("\n");
2606: fprintf(ficlog,"\n");
2607: }
1.191 brouard 2608: #endif
1.234 brouard 2609: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2610: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2611:
1.126 brouard 2612: #ifdef DEBUG
1.234 brouard 2613: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2614: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2615: for(j=1;j<=n;j++){
2616: printf(" %lf",xit[j]);
2617: fprintf(ficlog," %lf",xit[j]);
2618: }
2619: printf("\n");
2620: fprintf(ficlog,"\n");
1.126 brouard 2621: #endif
1.192 brouard 2622: } /* end of t or directest negative */
1.224 brouard 2623: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2624: #else
1.234 brouard 2625: } /* end if (fptt < fp) */
1.192 brouard 2626: #endif
1.225 brouard 2627: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 2628: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2629: #else
1.224 brouard 2630: #endif
1.234 brouard 2631: } /* loop iteration */
1.126 brouard 2632: }
1.234 brouard 2633:
1.126 brouard 2634: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 2635:
1.235 brouard 2636: 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 2637: {
1.279 brouard 2638: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij
2639: * (and selected quantitative values in nres)
2640: * by left multiplying the unit
2641: * matrix by transitions matrix until convergence is reached with precision ftolpl
2642: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
2643: * Wx is row vector: population in state 1, population in state 2, population dead
2644: * or prevalence in state 1, prevalence in state 2, 0
2645: * newm is the matrix after multiplications, its rows are identical at a factor.
2646: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
2647: * Output is prlim.
2648: * Initial matrix pimij
2649: */
1.206 brouard 2650: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2651: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2652: /* 0, 0 , 1} */
2653: /*
2654: * and after some iteration: */
2655: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2656: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2657: /* 0, 0 , 1} */
2658: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2659: /* {0.51571254859325999, 0.4842874514067399, */
2660: /* 0.51326036147820708, 0.48673963852179264} */
2661: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 2662:
1.126 brouard 2663: int i, ii,j,k;
1.209 brouard 2664: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2665: /* double **matprod2(); */ /* test */
1.218 brouard 2666: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2667: double **newm;
1.209 brouard 2668: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2669: int ncvloop=0;
1.288 brouard 2670: int first=0;
1.169 brouard 2671:
1.209 brouard 2672: min=vector(1,nlstate);
2673: max=vector(1,nlstate);
2674: meandiff=vector(1,nlstate);
2675:
1.218 brouard 2676: /* Starting with matrix unity */
1.126 brouard 2677: for (ii=1;ii<=nlstate+ndeath;ii++)
2678: for (j=1;j<=nlstate+ndeath;j++){
2679: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2680: }
1.169 brouard 2681:
2682: cov[1]=1.;
2683:
2684: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2685: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2686: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2687: ncvloop++;
1.126 brouard 2688: newm=savm;
2689: /* Covariates have to be included here again */
1.138 brouard 2690: cov[2]=agefin;
1.187 brouard 2691: if(nagesqr==1)
2692: cov[3]= agefin*agefin;;
1.234 brouard 2693: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2694: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2695: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.235 brouard 2696: /* 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 2697: }
2698: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2699: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.235 brouard 2700: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2701: /* 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 2702: }
1.237 brouard 2703: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.234 brouard 2704: if(Dummy[Tvar[Tage[k]]]){
2705: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2706: } else{
1.235 brouard 2707: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.234 brouard 2708: }
1.235 brouard 2709: /* 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 2710: }
1.237 brouard 2711: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 2712: /* 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 2713: if(Dummy[Tvard[k][1]==0]){
2714: if(Dummy[Tvard[k][2]==0]){
2715: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2716: }else{
2717: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2718: }
2719: }else{
2720: if(Dummy[Tvard[k][2]==0]){
2721: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2722: }else{
2723: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2724: }
2725: }
1.234 brouard 2726: }
1.138 brouard 2727: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2728: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2729: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2730: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2731: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2732: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2733: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2734:
1.126 brouard 2735: savm=oldm;
2736: oldm=newm;
1.209 brouard 2737:
2738: for(j=1; j<=nlstate; j++){
2739: max[j]=0.;
2740: min[j]=1.;
2741: }
2742: for(i=1;i<=nlstate;i++){
2743: sumnew=0;
2744: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2745: for(j=1; j<=nlstate; j++){
2746: prlim[i][j]= newm[i][j]/(1-sumnew);
2747: max[j]=FMAX(max[j],prlim[i][j]);
2748: min[j]=FMIN(min[j],prlim[i][j]);
2749: }
2750: }
2751:
1.126 brouard 2752: maxmax=0.;
1.209 brouard 2753: for(j=1; j<=nlstate; j++){
2754: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2755: maxmax=FMAX(maxmax,meandiff[j]);
2756: /* 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 2757: } /* j loop */
1.203 brouard 2758: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2759: /* 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 2760: if(maxmax < ftolpl){
1.209 brouard 2761: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2762: free_vector(min,1,nlstate);
2763: free_vector(max,1,nlstate);
2764: free_vector(meandiff,1,nlstate);
1.126 brouard 2765: return prlim;
2766: }
1.288 brouard 2767: } /* agefin loop */
1.208 brouard 2768: /* After some age loop it doesn't converge */
1.288 brouard 2769: if(!first){
2770: first=1;
2771: 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);
2772: }
2773: 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);
2774:
1.209 brouard 2775: /* 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); */
2776: free_vector(min,1,nlstate);
2777: free_vector(max,1,nlstate);
2778: free_vector(meandiff,1,nlstate);
1.208 brouard 2779:
1.169 brouard 2780: return prlim; /* should not reach here */
1.126 brouard 2781: }
2782:
1.217 brouard 2783:
2784: /**** Back Prevalence limit (stable or period prevalence) ****************/
2785:
1.218 brouard 2786: /* 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) */
2787: /* 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 2788: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 2789: {
1.264 brouard 2790: /* 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 2791: matrix by transitions matrix until convergence is reached with precision ftolpl */
2792: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2793: /* Wx is row vector: population in state 1, population in state 2, population dead */
2794: /* or prevalence in state 1, prevalence in state 2, 0 */
2795: /* newm is the matrix after multiplications, its rows are identical at a factor */
2796: /* Initial matrix pimij */
2797: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2798: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2799: /* 0, 0 , 1} */
2800: /*
2801: * and after some iteration: */
2802: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2803: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2804: /* 0, 0 , 1} */
2805: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2806: /* {0.51571254859325999, 0.4842874514067399, */
2807: /* 0.51326036147820708, 0.48673963852179264} */
2808: /* If we start from prlim again, prlim tends to a constant matrix */
2809:
2810: int i, ii,j,k;
1.247 brouard 2811: int first=0;
1.217 brouard 2812: double *min, *max, *meandiff, maxmax,sumnew=0.;
2813: /* double **matprod2(); */ /* test */
2814: double **out, cov[NCOVMAX+1], **bmij();
2815: double **newm;
1.218 brouard 2816: double **dnewm, **doldm, **dsavm; /* for use */
2817: double **oldm, **savm; /* for use */
2818:
1.217 brouard 2819: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2820: int ncvloop=0;
2821:
2822: min=vector(1,nlstate);
2823: max=vector(1,nlstate);
2824: meandiff=vector(1,nlstate);
2825:
1.266 brouard 2826: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2827: oldm=oldms; savm=savms;
2828:
2829: /* Starting with matrix unity */
2830: for (ii=1;ii<=nlstate+ndeath;ii++)
2831: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2832: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2833: }
2834:
2835: cov[1]=1.;
2836:
2837: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2838: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2839: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
1.288 brouard 2840: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2841: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2842: ncvloop++;
1.218 brouard 2843: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2844: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2845: /* Covariates have to be included here again */
2846: cov[2]=agefin;
2847: if(nagesqr==1)
2848: cov[3]= agefin*agefin;;
1.242 brouard 2849: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2850: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2851: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.264 brouard 2852: /* 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 2853: }
2854: /* for (k=1; k<=cptcovn;k++) { */
2855: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
2856: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
2857: /* /\* 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])]); *\/ */
2858: /* } */
2859: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2860: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
2861: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2862: /* 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]); */
2863: }
2864: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; */
2865: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */
2866: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\/ */
2867: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
2868: for (k=1; k<=cptcovage;k++){ /* For product with age */
2869: if(Dummy[Tvar[Tage[k]]]){
2870: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2871: } else{
2872: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
2873: }
2874: /* 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]); */
2875: }
2876: for (k=1; k<=cptcovprod;k++){ /* For product without age */
2877: /* 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]); */
2878: if(Dummy[Tvard[k][1]==0]){
2879: if(Dummy[Tvard[k][2]==0]){
2880: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2881: }else{
2882: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2883: }
2884: }else{
2885: if(Dummy[Tvard[k][2]==0]){
2886: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2887: }else{
2888: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2889: }
2890: }
1.217 brouard 2891: }
2892:
2893: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2894: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2895: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2896: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2897: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2898: /* ij should be linked to the correct index of cov */
2899: /* age and covariate values ij are in 'cov', but we need to pass
2900: * ij for the observed prevalence at age and status and covariate
2901: * number: prevacurrent[(int)agefin][ii][ij]
2902: */
2903: /* 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 *\/ */
2904: /* 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 *\/ */
2905: 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 2906: /* if((int)age == 86 || (int)age == 87){ */
1.266 brouard 2907: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
2908: /* for(i=1; i<=nlstate+ndeath; i++) { */
2909: /* printf("%d newm= ",i); */
2910: /* for(j=1;j<=nlstate+ndeath;j++) { */
2911: /* printf("%f ",newm[i][j]); */
2912: /* } */
2913: /* printf("oldm * "); */
2914: /* for(j=1;j<=nlstate+ndeath;j++) { */
2915: /* printf("%f ",oldm[i][j]); */
2916: /* } */
1.268 brouard 2917: /* printf(" bmmij "); */
1.266 brouard 2918: /* for(j=1;j<=nlstate+ndeath;j++) { */
2919: /* printf("%f ",pmmij[i][j]); */
2920: /* } */
2921: /* printf("\n"); */
2922: /* } */
2923: /* } */
1.217 brouard 2924: savm=oldm;
2925: oldm=newm;
1.266 brouard 2926:
1.217 brouard 2927: for(j=1; j<=nlstate; j++){
2928: max[j]=0.;
2929: min[j]=1.;
2930: }
2931: for(j=1; j<=nlstate; j++){
2932: for(i=1;i<=nlstate;i++){
1.234 brouard 2933: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2934: bprlim[i][j]= newm[i][j];
2935: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2936: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2937: }
2938: }
1.218 brouard 2939:
1.217 brouard 2940: maxmax=0.;
2941: for(i=1; i<=nlstate; i++){
2942: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2943: maxmax=FMAX(maxmax,meandiff[i]);
2944: /* 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 2945: } /* i loop */
1.217 brouard 2946: *ncvyear= -( (int)age- (int)agefin);
1.268 brouard 2947: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2948: if(maxmax < ftolpl){
1.220 brouard 2949: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2950: free_vector(min,1,nlstate);
2951: free_vector(max,1,nlstate);
2952: free_vector(meandiff,1,nlstate);
2953: return bprlim;
2954: }
1.288 brouard 2955: } /* agefin loop */
1.217 brouard 2956: /* After some age loop it doesn't converge */
1.288 brouard 2957: if(!first){
1.247 brouard 2958: first=1;
2959: 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\
2960: 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);
2961: }
2962: 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 2963: 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);
2964: /* 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); */
2965: free_vector(min,1,nlstate);
2966: free_vector(max,1,nlstate);
2967: free_vector(meandiff,1,nlstate);
2968:
2969: return bprlim; /* should not reach here */
2970: }
2971:
1.126 brouard 2972: /*************** transition probabilities ***************/
2973:
2974: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2975: {
1.138 brouard 2976: /* According to parameters values stored in x and the covariate's values stored in cov,
1.266 brouard 2977: computes the probability to be observed in state j (after stepm years) being in state i by appying the
1.138 brouard 2978: model to the ncovmodel covariates (including constant and age).
2979: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2980: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2981: ncth covariate in the global vector x is given by the formula:
2982: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2983: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2984: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2985: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1.266 brouard 2986: Outputs ps[i][j] or probability to be observed in j being in i according to
1.138 brouard 2987: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1.266 brouard 2988: Sum on j ps[i][j] should equal to 1.
1.138 brouard 2989: */
2990: double s1, lnpijopii;
1.126 brouard 2991: /*double t34;*/
1.164 brouard 2992: int i,j, nc, ii, jj;
1.126 brouard 2993:
1.223 brouard 2994: for(i=1; i<= nlstate; i++){
2995: for(j=1; j<i;j++){
2996: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2997: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2998: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2999: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3000: }
3001: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3002: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3003: }
3004: for(j=i+1; j<=nlstate+ndeath;j++){
3005: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3006: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3007: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3008: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3009: }
3010: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3011: }
3012: }
1.218 brouard 3013:
1.223 brouard 3014: for(i=1; i<= nlstate; i++){
3015: s1=0;
3016: for(j=1; j<i; j++){
3017: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3018: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3019: }
3020: for(j=i+1; j<=nlstate+ndeath; j++){
3021: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3022: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3023: }
3024: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3025: ps[i][i]=1./(s1+1.);
3026: /* Computing other pijs */
3027: for(j=1; j<i; j++)
3028: ps[i][j]= exp(ps[i][j])*ps[i][i];
3029: for(j=i+1; j<=nlstate+ndeath; j++)
3030: ps[i][j]= exp(ps[i][j])*ps[i][i];
3031: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3032: } /* end i */
1.218 brouard 3033:
1.223 brouard 3034: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3035: for(jj=1; jj<= nlstate+ndeath; jj++){
3036: ps[ii][jj]=0;
3037: ps[ii][ii]=1;
3038: }
3039: }
1.294 brouard 3040:
3041:
1.223 brouard 3042: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3043: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3044: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3045: /* } */
3046: /* printf("\n "); */
3047: /* } */
3048: /* printf("\n ");printf("%lf ",cov[2]);*/
3049: /*
3050: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 3051: goto end;*/
1.266 brouard 3052: return ps; /* Pointer is unchanged since its call */
1.126 brouard 3053: }
3054:
1.218 brouard 3055: /*************** backward transition probabilities ***************/
3056:
3057: /* 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 ) */
3058: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
3059: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
3060: {
1.302 brouard 3061: /* 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 3062: * 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 3063: */
1.218 brouard 3064: int i, ii, j,k;
1.222 brouard 3065:
3066: double **out, **pmij();
3067: double sumnew=0.;
1.218 brouard 3068: double agefin;
1.292 brouard 3069: 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 3070: double **dnewm, **dsavm, **doldm;
3071: double **bbmij;
3072:
1.218 brouard 3073: doldm=ddoldms; /* global pointers */
1.222 brouard 3074: dnewm=ddnewms;
3075: dsavm=ddsavms;
3076:
3077: agefin=cov[2];
1.268 brouard 3078: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
1.222 brouard 3079: /* bmij *//* age is cov[2], ij is included in cov, but we need for
1.266 brouard 3080: the observed prevalence (with this covariate ij) at beginning of transition */
3081: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.268 brouard 3082:
3083: /* P_x */
1.266 brouard 3084: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm */
1.268 brouard 3085: /* outputs pmmij which is a stochastic matrix in row */
3086:
3087: /* Diag(w_x) */
1.292 brouard 3088: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
1.268 brouard 3089: sumnew=0.;
1.269 brouard 3090: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
1.268 brouard 3091: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
1.297 brouard 3092: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
1.268 brouard 3093: sumnew+=prevacurrent[(int)agefin][ii][ij];
3094: }
3095: if(sumnew >0.01){ /* At least some value in the prevalence */
3096: for (ii=1;ii<=nlstate+ndeath;ii++){
3097: for (j=1;j<=nlstate+ndeath;j++)
1.269 brouard 3098: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
1.268 brouard 3099: }
3100: }else{
3101: for (ii=1;ii<=nlstate+ndeath;ii++){
3102: for (j=1;j<=nlstate+ndeath;j++)
3103: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
3104: }
3105: /* if(sumnew <0.9){ */
3106: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
3107: /* } */
3108: }
3109: k3=0.0; /* We put the last diagonal to 0 */
3110: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
3111: doldm[ii][ii]= k3;
3112: }
3113: /* End doldm, At the end doldm is diag[(w_i)] */
3114:
1.292 brouard 3115: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
3116: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
1.268 brouard 3117:
1.292 brouard 3118: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
1.268 brouard 3119: /* 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 3120: for (j=1;j<=nlstate+ndeath;j++){
1.268 brouard 3121: sumnew=0.;
1.222 brouard 3122: for (ii=1;ii<=nlstate;ii++){
1.266 brouard 3123: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
1.268 brouard 3124: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
1.222 brouard 3125: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
1.268 brouard 3126: for (ii=1;ii<=nlstate+ndeath;ii++){
1.222 brouard 3127: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
1.268 brouard 3128: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3129: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
1.268 brouard 3130: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3131: /* }else */
1.268 brouard 3132: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
3133: } /*End ii */
3134: } /* 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 */
3135:
1.292 brouard 3136: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
1.268 brouard 3137: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
1.222 brouard 3138: /* end bmij */
1.266 brouard 3139: return ps; /*pointer is unchanged */
1.218 brouard 3140: }
1.217 brouard 3141: /*************** transition probabilities ***************/
3142:
1.218 brouard 3143: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 3144: {
3145: /* According to parameters values stored in x and the covariate's values stored in cov,
3146: computes the probability to be observed in state j being in state i by appying the
3147: model to the ncovmodel covariates (including constant and age).
3148: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3149: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3150: ncth covariate in the global vector x is given by the formula:
3151: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3152: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3153: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3154: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3155: Outputs ps[i][j] the probability to be observed in j being in j according to
3156: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3157: */
3158: double s1, lnpijopii;
3159: /*double t34;*/
3160: int i,j, nc, ii, jj;
3161:
1.234 brouard 3162: for(i=1; i<= nlstate; i++){
3163: for(j=1; j<i;j++){
3164: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3165: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3166: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3167: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3168: }
3169: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3170: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3171: }
3172: for(j=i+1; j<=nlstate+ndeath;j++){
3173: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3174: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3175: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3176: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3177: }
3178: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3179: }
3180: }
3181:
3182: for(i=1; i<= nlstate; i++){
3183: s1=0;
3184: for(j=1; j<i; j++){
3185: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3186: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3187: }
3188: for(j=i+1; j<=nlstate+ndeath; j++){
3189: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3190: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3191: }
3192: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3193: ps[i][i]=1./(s1+1.);
3194: /* Computing other pijs */
3195: for(j=1; j<i; j++)
3196: ps[i][j]= exp(ps[i][j])*ps[i][i];
3197: for(j=i+1; j<=nlstate+ndeath; j++)
3198: ps[i][j]= exp(ps[i][j])*ps[i][i];
3199: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3200: } /* end i */
3201:
3202: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3203: for(jj=1; jj<= nlstate+ndeath; jj++){
3204: ps[ii][jj]=0;
3205: ps[ii][ii]=1;
3206: }
3207: }
1.296 brouard 3208: /* Added for prevbcast */ /* Transposed matrix too */
1.234 brouard 3209: for(jj=1; jj<= nlstate+ndeath; jj++){
3210: s1=0.;
3211: for(ii=1; ii<= nlstate+ndeath; ii++){
3212: s1+=ps[ii][jj];
3213: }
3214: for(ii=1; ii<= nlstate; ii++){
3215: ps[ii][jj]=ps[ii][jj]/s1;
3216: }
3217: }
3218: /* Transposition */
3219: for(jj=1; jj<= nlstate+ndeath; jj++){
3220: for(ii=jj; ii<= nlstate+ndeath; ii++){
3221: s1=ps[ii][jj];
3222: ps[ii][jj]=ps[jj][ii];
3223: ps[jj][ii]=s1;
3224: }
3225: }
3226: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3227: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3228: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3229: /* } */
3230: /* printf("\n "); */
3231: /* } */
3232: /* printf("\n ");printf("%lf ",cov[2]);*/
3233: /*
3234: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3235: goto end;*/
3236: return ps;
1.217 brouard 3237: }
3238:
3239:
1.126 brouard 3240: /**************** Product of 2 matrices ******************/
3241:
1.145 brouard 3242: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 3243: {
3244: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
3245: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
3246: /* in, b, out are matrice of pointers which should have been initialized
3247: before: only the contents of out is modified. The function returns
3248: a pointer to pointers identical to out */
1.145 brouard 3249: int i, j, k;
1.126 brouard 3250: for(i=nrl; i<= nrh; i++)
1.145 brouard 3251: for(k=ncolol; k<=ncoloh; k++){
3252: out[i][k]=0.;
3253: for(j=ncl; j<=nch; j++)
3254: out[i][k] +=in[i][j]*b[j][k];
3255: }
1.126 brouard 3256: return out;
3257: }
3258:
3259:
3260: /************* Higher Matrix Product ***************/
3261:
1.235 brouard 3262: 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 3263: {
1.218 brouard 3264: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 3265: 'nhstepm*hstepm*stepm' months (i.e. until
3266: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3267: nhstepm*hstepm matrices.
3268: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3269: (typically every 2 years instead of every month which is too big
3270: for the memory).
3271: Model is determined by parameters x and covariates have to be
3272: included manually here.
3273:
3274: */
3275:
3276: int i, j, d, h, k;
1.131 brouard 3277: double **out, cov[NCOVMAX+1];
1.126 brouard 3278: double **newm;
1.187 brouard 3279: double agexact;
1.214 brouard 3280: double agebegin, ageend;
1.126 brouard 3281:
3282: /* Hstepm could be zero and should return the unit matrix */
3283: for (i=1;i<=nlstate+ndeath;i++)
3284: for (j=1;j<=nlstate+ndeath;j++){
3285: oldm[i][j]=(i==j ? 1.0 : 0.0);
3286: po[i][j][0]=(i==j ? 1.0 : 0.0);
3287: }
3288: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3289: for(h=1; h <=nhstepm; h++){
3290: for(d=1; d <=hstepm; d++){
3291: newm=savm;
3292: /* Covariates have to be included here again */
3293: cov[1]=1.;
1.214 brouard 3294: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 3295: cov[2]=agexact;
3296: if(nagesqr==1)
1.227 brouard 3297: cov[3]= agexact*agexact;
1.235 brouard 3298: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
3299: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
3300: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3301: /* 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)); */
3302: }
3303: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3304: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3305: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3306: /* 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]); */
3307: }
3308: for (k=1; k<=cptcovage;k++){
3309: if(Dummy[Tvar[Tage[k]]]){
3310: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3311: } else{
3312: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3313: }
3314: /* 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]); */
3315: }
3316: for (k=1; k<=cptcovprod;k++){ /* */
3317: /* 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]); */
3318: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
3319: }
3320: /* for (k=1; k<=cptcovn;k++) */
3321: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3322: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
3323: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
3324: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
3325: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 3326:
3327:
1.126 brouard 3328: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3329: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 3330: /* right multiplication of oldm by the current matrix */
1.126 brouard 3331: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
3332: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 3333: /* if((int)age == 70){ */
3334: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3335: /* for(i=1; i<=nlstate+ndeath; i++) { */
3336: /* printf("%d pmmij ",i); */
3337: /* for(j=1;j<=nlstate+ndeath;j++) { */
3338: /* printf("%f ",pmmij[i][j]); */
3339: /* } */
3340: /* printf(" oldm "); */
3341: /* for(j=1;j<=nlstate+ndeath;j++) { */
3342: /* printf("%f ",oldm[i][j]); */
3343: /* } */
3344: /* printf("\n"); */
3345: /* } */
3346: /* } */
1.126 brouard 3347: savm=oldm;
3348: oldm=newm;
3349: }
3350: for(i=1; i<=nlstate+ndeath; i++)
3351: for(j=1;j<=nlstate+ndeath;j++) {
1.267 brouard 3352: po[i][j][h]=newm[i][j];
3353: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 3354: }
1.128 brouard 3355: /*printf("h=%d ",h);*/
1.126 brouard 3356: } /* end h */
1.267 brouard 3357: /* printf("\n H=%d \n",h); */
1.126 brouard 3358: return po;
3359: }
3360:
1.217 brouard 3361: /************* Higher Back Matrix Product ***************/
1.218 brouard 3362: /* 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 3363: 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 3364: {
1.266 brouard 3365: /* For a combination of dummy covariate ij, computes the transition matrix starting at age 'age' over
1.217 brouard 3366: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 3367: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3368: nhstepm*hstepm matrices.
3369: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3370: (typically every 2 years instead of every month which is too big
1.217 brouard 3371: for the memory).
1.218 brouard 3372: Model is determined by parameters x and covariates have to be
1.266 brouard 3373: included manually here. Then we use a call to bmij(x and cov)
3374: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
1.222 brouard 3375: */
1.217 brouard 3376:
3377: int i, j, d, h, k;
1.266 brouard 3378: double **out, cov[NCOVMAX+1], **bmij();
3379: double **newm, ***newmm;
1.217 brouard 3380: double agexact;
3381: double agebegin, ageend;
1.222 brouard 3382: double **oldm, **savm;
1.217 brouard 3383:
1.266 brouard 3384: newmm=po; /* To be saved */
3385: oldm=oldms;savm=savms; /* Global pointers */
1.217 brouard 3386: /* Hstepm could be zero and should return the unit matrix */
3387: for (i=1;i<=nlstate+ndeath;i++)
3388: for (j=1;j<=nlstate+ndeath;j++){
3389: oldm[i][j]=(i==j ? 1.0 : 0.0);
3390: po[i][j][0]=(i==j ? 1.0 : 0.0);
3391: }
3392: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3393: for(h=1; h <=nhstepm; h++){
3394: for(d=1; d <=hstepm; d++){
3395: newm=savm;
3396: /* Covariates have to be included here again */
3397: cov[1]=1.;
1.271 brouard 3398: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
1.217 brouard 3399: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3400: cov[2]=agexact;
3401: if(nagesqr==1)
1.222 brouard 3402: cov[3]= agexact*agexact;
1.266 brouard 3403: for (k=1; k<=cptcovn;k++){
3404: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3405: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
3406: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3407: /* 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)); */
3408: }
1.267 brouard 3409: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3410: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3411: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3412: /* 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]); */
3413: }
3414: for (k=1; k<=cptcovage;k++){ /* Should start at cptcovn+1 */
3415: if(Dummy[Tvar[Tage[k]]]){
3416: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3417: } else{
3418: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3419: }
3420: /* 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]); */
3421: }
3422: for (k=1; k<=cptcovprod;k++){ /* Useless because included in cptcovn */
1.222 brouard 3423: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.267 brouard 3424: }
1.217 brouard 3425: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3426: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.267 brouard 3427:
1.218 brouard 3428: /* Careful transposed matrix */
1.266 brouard 3429: /* age is in cov[2], prevacurrent at beginning of transition. */
1.218 brouard 3430: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 3431: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 3432: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 3433: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 3434: /* if((int)age == 70){ */
3435: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3436: /* for(i=1; i<=nlstate+ndeath; i++) { */
3437: /* printf("%d pmmij ",i); */
3438: /* for(j=1;j<=nlstate+ndeath;j++) { */
3439: /* printf("%f ",pmmij[i][j]); */
3440: /* } */
3441: /* printf(" oldm "); */
3442: /* for(j=1;j<=nlstate+ndeath;j++) { */
3443: /* printf("%f ",oldm[i][j]); */
3444: /* } */
3445: /* printf("\n"); */
3446: /* } */
3447: /* } */
3448: savm=oldm;
3449: oldm=newm;
3450: }
3451: for(i=1; i<=nlstate+ndeath; i++)
3452: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3453: po[i][j][h]=newm[i][j];
1.268 brouard 3454: /* if(h==nhstepm) */
3455: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
1.217 brouard 3456: }
1.268 brouard 3457: /* printf("h=%d %.1f ",h, agexact); */
1.217 brouard 3458: } /* end h */
1.268 brouard 3459: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
1.217 brouard 3460: return po;
3461: }
3462:
3463:
1.162 brouard 3464: #ifdef NLOPT
3465: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3466: double fret;
3467: double *xt;
3468: int j;
3469: myfunc_data *d2 = (myfunc_data *) pd;
3470: /* xt = (p1-1); */
3471: xt=vector(1,n);
3472: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3473:
3474: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3475: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3476: printf("Function = %.12lf ",fret);
3477: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3478: printf("\n");
3479: free_vector(xt,1,n);
3480: return fret;
3481: }
3482: #endif
1.126 brouard 3483:
3484: /*************** log-likelihood *************/
3485: double func( double *x)
3486: {
1.226 brouard 3487: int i, ii, j, k, mi, d, kk;
3488: int ioffset=0;
3489: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3490: double **out;
3491: double lli; /* Individual log likelihood */
3492: int s1, s2;
1.228 brouard 3493: 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 3494: double bbh, survp;
3495: long ipmx;
3496: double agexact;
3497: /*extern weight */
3498: /* We are differentiating ll according to initial status */
3499: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3500: /*for(i=1;i<imx;i++)
3501: printf(" %d\n",s[4][i]);
3502: */
1.162 brouard 3503:
1.226 brouard 3504: ++countcallfunc;
1.162 brouard 3505:
1.226 brouard 3506: cov[1]=1.;
1.126 brouard 3507:
1.226 brouard 3508: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3509: ioffset=0;
1.226 brouard 3510: if(mle==1){
3511: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3512: /* Computes the values of the ncovmodel covariates of the model
3513: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3514: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3515: to be observed in j being in i according to the model.
3516: */
1.243 brouard 3517: ioffset=2+nagesqr ;
1.233 brouard 3518: /* Fixed */
1.234 brouard 3519: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3520: 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)*/
3521: }
1.226 brouard 3522: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3523: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3524: has been calculated etc */
3525: /* For an individual i, wav[i] gives the number of effective waves */
3526: /* We compute the contribution to Likelihood of each effective transition
3527: mw[mi][i] is real wave of the mi th effectve wave */
3528: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3529: s2=s[mw[mi+1][i]][i];
3530: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3531: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3532: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3533: */
3534: for(mi=1; mi<= wav[i]-1; mi++){
1.234 brouard 3535: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3536: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3537: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
1.234 brouard 3538: }
3539: for (ii=1;ii<=nlstate+ndeath;ii++)
3540: for (j=1;j<=nlstate+ndeath;j++){
3541: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3542: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3543: }
3544: for(d=0; d<dh[mi][i]; d++){
3545: newm=savm;
3546: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3547: cov[2]=agexact;
3548: if(nagesqr==1)
3549: cov[3]= agexact*agexact; /* Should be changed here */
3550: for (kk=1; kk<=cptcovage;kk++) {
1.242 brouard 3551: if(!FixedV[Tvar[Tage[kk]]])
1.234 brouard 3552: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
1.242 brouard 3553: else
3554: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
1.234 brouard 3555: }
3556: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3557: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3558: savm=oldm;
3559: oldm=newm;
3560: } /* end mult */
3561:
3562: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3563: /* But now since version 0.9 we anticipate for bias at large stepm.
3564: * If stepm is larger than one month (smallest stepm) and if the exact delay
3565: * (in months) between two waves is not a multiple of stepm, we rounded to
3566: * the nearest (and in case of equal distance, to the lowest) interval but now
3567: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3568: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3569: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3570: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3571: * -stepm/2 to stepm/2 .
3572: * For stepm=1 the results are the same as for previous versions of Imach.
3573: * For stepm > 1 the results are less biased than in previous versions.
3574: */
1.234 brouard 3575: s1=s[mw[mi][i]][i];
3576: s2=s[mw[mi+1][i]][i];
3577: bbh=(double)bh[mi][i]/(double)stepm;
3578: /* bias bh is positive if real duration
3579: * is higher than the multiple of stepm and negative otherwise.
3580: */
3581: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3582: if( s2 > nlstate){
3583: /* i.e. if s2 is a death state and if the date of death is known
3584: then the contribution to the likelihood is the probability to
3585: die between last step unit time and current step unit time,
3586: which is also equal to probability to die before dh
3587: minus probability to die before dh-stepm .
3588: In version up to 0.92 likelihood was computed
3589: as if date of death was unknown. Death was treated as any other
3590: health state: the date of the interview describes the actual state
3591: and not the date of a change in health state. The former idea was
3592: to consider that at each interview the state was recorded
3593: (healthy, disable or death) and IMaCh was corrected; but when we
3594: introduced the exact date of death then we should have modified
3595: the contribution of an exact death to the likelihood. This new
3596: contribution is smaller and very dependent of the step unit
3597: stepm. It is no more the probability to die between last interview
3598: and month of death but the probability to survive from last
3599: interview up to one month before death multiplied by the
3600: probability to die within a month. Thanks to Chris
3601: Jackson for correcting this bug. Former versions increased
3602: mortality artificially. The bad side is that we add another loop
3603: which slows down the processing. The difference can be up to 10%
3604: lower mortality.
3605: */
3606: /* If, at the beginning of the maximization mostly, the
3607: cumulative probability or probability to be dead is
3608: constant (ie = 1) over time d, the difference is equal to
3609: 0. out[s1][3] = savm[s1][3]: probability, being at state
3610: s1 at precedent wave, to be dead a month before current
3611: wave is equal to probability, being at state s1 at
3612: precedent wave, to be dead at mont of the current
3613: wave. Then the observed probability (that this person died)
3614: is null according to current estimated parameter. In fact,
3615: it should be very low but not zero otherwise the log go to
3616: infinity.
3617: */
1.183 brouard 3618: /* #ifdef INFINITYORIGINAL */
3619: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3620: /* #else */
3621: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3622: /* lli=log(mytinydouble); */
3623: /* else */
3624: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3625: /* #endif */
1.226 brouard 3626: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3627:
1.226 brouard 3628: } else if ( s2==-1 ) { /* alive */
3629: for (j=1,survp=0. ; j<=nlstate; j++)
3630: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3631: /*survp += out[s1][j]; */
3632: lli= log(survp);
3633: }
3634: else if (s2==-4) {
3635: for (j=3,survp=0. ; j<=nlstate; j++)
3636: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3637: lli= log(survp);
3638: }
3639: else if (s2==-5) {
3640: for (j=1,survp=0. ; j<=2; j++)
3641: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3642: lli= log(survp);
3643: }
3644: else{
3645: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3646: /* 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 */
3647: }
3648: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3649: /*if(lli ==000.0)*/
3650: /*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); */
3651: ipmx +=1;
3652: sw += weight[i];
3653: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3654: /* if (lli < log(mytinydouble)){ */
3655: /* 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); */
3656: /* 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]); */
3657: /* } */
3658: } /* end of wave */
3659: } /* end of individual */
3660: } else if(mle==2){
3661: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3662: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3663: for(mi=1; mi<= wav[i]-1; mi++){
3664: for (ii=1;ii<=nlstate+ndeath;ii++)
3665: for (j=1;j<=nlstate+ndeath;j++){
3666: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3667: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3668: }
3669: for(d=0; d<=dh[mi][i]; d++){
3670: newm=savm;
3671: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3672: cov[2]=agexact;
3673: if(nagesqr==1)
3674: cov[3]= agexact*agexact;
3675: for (kk=1; kk<=cptcovage;kk++) {
3676: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3677: }
3678: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3679: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3680: savm=oldm;
3681: oldm=newm;
3682: } /* end mult */
3683:
3684: s1=s[mw[mi][i]][i];
3685: s2=s[mw[mi+1][i]][i];
3686: bbh=(double)bh[mi][i]/(double)stepm;
3687: 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 */
3688: ipmx +=1;
3689: sw += weight[i];
3690: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3691: } /* end of wave */
3692: } /* end of individual */
3693: } else if(mle==3){ /* exponential inter-extrapolation */
3694: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3695: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3696: for(mi=1; mi<= wav[i]-1; mi++){
3697: for (ii=1;ii<=nlstate+ndeath;ii++)
3698: for (j=1;j<=nlstate+ndeath;j++){
3699: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3700: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3701: }
3702: for(d=0; d<dh[mi][i]; d++){
3703: newm=savm;
3704: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3705: cov[2]=agexact;
3706: if(nagesqr==1)
3707: cov[3]= agexact*agexact;
3708: for (kk=1; kk<=cptcovage;kk++) {
3709: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3710: }
3711: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3712: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3713: savm=oldm;
3714: oldm=newm;
3715: } /* end mult */
3716:
3717: s1=s[mw[mi][i]][i];
3718: s2=s[mw[mi+1][i]][i];
3719: bbh=(double)bh[mi][i]/(double)stepm;
3720: 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 */
3721: ipmx +=1;
3722: sw += weight[i];
3723: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3724: } /* end of wave */
3725: } /* end of individual */
3726: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3727: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3728: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3729: for(mi=1; mi<= wav[i]-1; mi++){
3730: for (ii=1;ii<=nlstate+ndeath;ii++)
3731: for (j=1;j<=nlstate+ndeath;j++){
3732: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3733: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3734: }
3735: for(d=0; d<dh[mi][i]; d++){
3736: newm=savm;
3737: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3738: cov[2]=agexact;
3739: if(nagesqr==1)
3740: cov[3]= agexact*agexact;
3741: for (kk=1; kk<=cptcovage;kk++) {
3742: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3743: }
1.126 brouard 3744:
1.226 brouard 3745: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3746: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3747: savm=oldm;
3748: oldm=newm;
3749: } /* end mult */
3750:
3751: s1=s[mw[mi][i]][i];
3752: s2=s[mw[mi+1][i]][i];
3753: if( s2 > nlstate){
3754: lli=log(out[s1][s2] - savm[s1][s2]);
3755: } else if ( s2==-1 ) { /* alive */
3756: for (j=1,survp=0. ; j<=nlstate; j++)
3757: survp += out[s1][j];
3758: lli= log(survp);
3759: }else{
3760: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3761: }
3762: ipmx +=1;
3763: sw += weight[i];
3764: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3765: /* 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 3766: } /* end of wave */
3767: } /* end of individual */
3768: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3769: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3770: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3771: for(mi=1; mi<= wav[i]-1; mi++){
3772: for (ii=1;ii<=nlstate+ndeath;ii++)
3773: for (j=1;j<=nlstate+ndeath;j++){
3774: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3775: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3776: }
3777: for(d=0; d<dh[mi][i]; d++){
3778: newm=savm;
3779: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3780: cov[2]=agexact;
3781: if(nagesqr==1)
3782: cov[3]= agexact*agexact;
3783: for (kk=1; kk<=cptcovage;kk++) {
3784: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3785: }
1.126 brouard 3786:
1.226 brouard 3787: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3788: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3789: savm=oldm;
3790: oldm=newm;
3791: } /* end mult */
3792:
3793: s1=s[mw[mi][i]][i];
3794: s2=s[mw[mi+1][i]][i];
3795: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3796: ipmx +=1;
3797: sw += weight[i];
3798: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3799: /*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]);*/
3800: } /* end of wave */
3801: } /* end of individual */
3802: } /* End of if */
3803: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3804: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3805: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3806: return -l;
1.126 brouard 3807: }
3808:
3809: /*************** log-likelihood *************/
3810: double funcone( double *x)
3811: {
1.228 brouard 3812: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3813: int i, ii, j, k, mi, d, kk;
1.228 brouard 3814: int ioffset=0;
1.131 brouard 3815: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3816: double **out;
3817: double lli; /* Individual log likelihood */
3818: double llt;
3819: int s1, s2;
1.228 brouard 3820: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3821:
1.126 brouard 3822: double bbh, survp;
1.187 brouard 3823: double agexact;
1.214 brouard 3824: double agebegin, ageend;
1.126 brouard 3825: /*extern weight */
3826: /* We are differentiating ll according to initial status */
3827: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3828: /*for(i=1;i<imx;i++)
3829: printf(" %d\n",s[4][i]);
3830: */
3831: cov[1]=1.;
3832:
3833: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3834: ioffset=0;
3835: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.243 brouard 3836: /* ioffset=2+nagesqr+cptcovage; */
3837: ioffset=2+nagesqr;
1.232 brouard 3838: /* Fixed */
1.224 brouard 3839: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 3840: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
3841: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3842: 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)*/
3843: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
3844: /* cov[2+6]=covar[Tvar[6]][i]; */
3845: /* cov[2+6]=covar[2][i]; V2 */
3846: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
3847: /* cov[2+7]=covar[Tvar[7]][i]; */
3848: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
3849: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
3850: /* cov[2+9]=covar[Tvar[9]][i]; */
3851: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 3852: }
1.232 brouard 3853: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
3854: /* 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?)*\/ */
3855: /* } */
1.231 brouard 3856: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
3857: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
3858: /* } */
1.225 brouard 3859:
1.233 brouard 3860:
3861: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 3862: /* Wave varying (but not age varying) */
3863: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3864: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3865: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
3866: }
1.232 brouard 3867: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.242 brouard 3868: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3869: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
3870: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
3871: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
3872: /* 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 3873: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 3874: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3875: /* /\* 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]); *\/ */
3876: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 3877: /* } */
1.126 brouard 3878: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 3879: for (j=1;j<=nlstate+ndeath;j++){
3880: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3881: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3882: }
1.214 brouard 3883:
3884: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3885: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3886: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.247 brouard 3887: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
1.242 brouard 3888: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3889: and mw[mi+1][i]. dh depends on stepm.*/
3890: newm=savm;
1.247 brouard 3891: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
1.242 brouard 3892: cov[2]=agexact;
3893: if(nagesqr==1)
3894: cov[3]= agexact*agexact;
3895: for (kk=1; kk<=cptcovage;kk++) {
3896: if(!FixedV[Tvar[Tage[kk]]])
3897: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3898: else
3899: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
3900: }
3901: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3902: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3903: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3904: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3905: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3906: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3907: savm=oldm;
3908: oldm=newm;
1.126 brouard 3909: } /* end mult */
3910:
3911: s1=s[mw[mi][i]][i];
3912: s2=s[mw[mi+1][i]][i];
1.217 brouard 3913: /* if(s2==-1){ */
1.268 brouard 3914: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
1.217 brouard 3915: /* /\* exit(1); *\/ */
3916: /* } */
1.126 brouard 3917: bbh=(double)bh[mi][i]/(double)stepm;
3918: /* bias is positive if real duration
3919: * is higher than the multiple of stepm and negative otherwise.
3920: */
3921: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 3922: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3923: } else if ( s2==-1 ) { /* alive */
1.242 brouard 3924: for (j=1,survp=0. ; j<=nlstate; j++)
3925: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3926: lli= log(survp);
1.126 brouard 3927: }else if (mle==1){
1.242 brouard 3928: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3929: } else if(mle==2){
1.242 brouard 3930: 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 3931: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 3932: 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 3933: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 3934: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3935: } else{ /* mle=0 back to 1 */
1.242 brouard 3936: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3937: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3938: } /* End of if */
3939: ipmx +=1;
3940: sw += weight[i];
3941: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3942: /*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 3943: if(globpr){
1.246 brouard 3944: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3945: %11.6f %11.6f %11.6f ", \
1.242 brouard 3946: 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 3947: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
1.242 brouard 3948: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3949: llt +=ll[k]*gipmx/gsw;
3950: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3951: }
3952: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3953: }
1.232 brouard 3954: } /* end of wave */
3955: } /* end of individual */
3956: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3957: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3958: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3959: if(globpr==0){ /* First time we count the contributions and weights */
3960: gipmx=ipmx;
3961: gsw=sw;
3962: }
3963: return -l;
1.126 brouard 3964: }
3965:
3966:
3967: /*************** function likelione ***********/
1.292 brouard 3968: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
1.126 brouard 3969: {
3970: /* This routine should help understanding what is done with
3971: the selection of individuals/waves and
3972: to check the exact contribution to the likelihood.
3973: Plotting could be done.
3974: */
3975: int k;
3976:
3977: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3978: strcpy(fileresilk,"ILK_");
1.202 brouard 3979: strcat(fileresilk,fileresu);
1.126 brouard 3980: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3981: printf("Problem with resultfile: %s\n", fileresilk);
3982: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3983: }
1.214 brouard 3984: 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");
3985: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3986: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3987: for(k=1; k<=nlstate; k++)
3988: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3989: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3990: }
3991:
1.292 brouard 3992: *fretone=(*func)(p);
1.126 brouard 3993: if(*globpri !=0){
3994: fclose(ficresilk);
1.205 brouard 3995: if (mle ==0)
3996: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3997: else if(mle >=1)
3998: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3999: 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 4000: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
1.208 brouard 4001:
4002: for (k=1; k<= nlstate ; k++) {
1.211 brouard 4003: 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 4004: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
4005: }
1.207 brouard 4006: 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 4007: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4008: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 4009: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4010: fflush(fichtm);
1.205 brouard 4011: }
1.126 brouard 4012: return;
4013: }
4014:
4015:
4016: /*********** Maximum Likelihood Estimation ***************/
4017:
4018: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
4019: {
1.165 brouard 4020: int i,j, iter=0;
1.126 brouard 4021: double **xi;
4022: double fret;
4023: double fretone; /* Only one call to likelihood */
4024: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 4025:
4026: #ifdef NLOPT
4027: int creturn;
4028: nlopt_opt opt;
4029: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
4030: double *lb;
4031: double minf; /* the minimum objective value, upon return */
4032: double * p1; /* Shifted parameters from 0 instead of 1 */
4033: myfunc_data dinst, *d = &dinst;
4034: #endif
4035:
4036:
1.126 brouard 4037: xi=matrix(1,npar,1,npar);
4038: for (i=1;i<=npar;i++)
4039: for (j=1;j<=npar;j++)
4040: xi[i][j]=(i==j ? 1.0 : 0.0);
4041: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 4042: strcpy(filerespow,"POW_");
1.126 brouard 4043: strcat(filerespow,fileres);
4044: if((ficrespow=fopen(filerespow,"w"))==NULL) {
4045: printf("Problem with resultfile: %s\n", filerespow);
4046: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
4047: }
4048: fprintf(ficrespow,"# Powell\n# iter -2*LL");
4049: for (i=1;i<=nlstate;i++)
4050: for(j=1;j<=nlstate+ndeath;j++)
4051: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
4052: fprintf(ficrespow,"\n");
1.162 brouard 4053: #ifdef POWELL
1.126 brouard 4054: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 4055: #endif
1.126 brouard 4056:
1.162 brouard 4057: #ifdef NLOPT
4058: #ifdef NEWUOA
4059: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
4060: #else
4061: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
4062: #endif
4063: lb=vector(0,npar-1);
4064: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
4065: nlopt_set_lower_bounds(opt, lb);
4066: nlopt_set_initial_step1(opt, 0.1);
4067:
4068: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
4069: d->function = func;
4070: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
4071: nlopt_set_min_objective(opt, myfunc, d);
4072: nlopt_set_xtol_rel(opt, ftol);
4073: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
4074: printf("nlopt failed! %d\n",creturn);
4075: }
4076: else {
4077: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
4078: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
4079: iter=1; /* not equal */
4080: }
4081: nlopt_destroy(opt);
4082: #endif
1.126 brouard 4083: free_matrix(xi,1,npar,1,npar);
4084: fclose(ficrespow);
1.203 brouard 4085: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4086: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 4087: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 4088:
4089: }
4090:
4091: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 4092: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 4093: {
4094: double **a,**y,*x,pd;
1.203 brouard 4095: /* double **hess; */
1.164 brouard 4096: int i, j;
1.126 brouard 4097: int *indx;
4098:
4099: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 4100: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 4101: void lubksb(double **a, int npar, int *indx, double b[]) ;
4102: void ludcmp(double **a, int npar, int *indx, double *d) ;
4103: double gompertz(double p[]);
1.203 brouard 4104: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 4105:
4106: printf("\nCalculation of the hessian matrix. Wait...\n");
4107: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
4108: for (i=1;i<=npar;i++){
1.203 brouard 4109: printf("%d-",i);fflush(stdout);
4110: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 4111:
4112: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
4113:
4114: /* printf(" %f ",p[i]);
4115: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
4116: }
4117:
4118: for (i=1;i<=npar;i++) {
4119: for (j=1;j<=npar;j++) {
4120: if (j>i) {
1.203 brouard 4121: printf(".%d-%d",i,j);fflush(stdout);
4122: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
4123: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 4124:
4125: hess[j][i]=hess[i][j];
4126: /*printf(" %lf ",hess[i][j]);*/
4127: }
4128: }
4129: }
4130: printf("\n");
4131: fprintf(ficlog,"\n");
4132:
4133: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
4134: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
4135:
4136: a=matrix(1,npar,1,npar);
4137: y=matrix(1,npar,1,npar);
4138: x=vector(1,npar);
4139: indx=ivector(1,npar);
4140: for (i=1;i<=npar;i++)
4141: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
4142: ludcmp(a,npar,indx,&pd);
4143:
4144: for (j=1;j<=npar;j++) {
4145: for (i=1;i<=npar;i++) x[i]=0;
4146: x[j]=1;
4147: lubksb(a,npar,indx,x);
4148: for (i=1;i<=npar;i++){
4149: matcov[i][j]=x[i];
4150: }
4151: }
4152:
4153: printf("\n#Hessian matrix#\n");
4154: fprintf(ficlog,"\n#Hessian matrix#\n");
4155: for (i=1;i<=npar;i++) {
4156: for (j=1;j<=npar;j++) {
1.203 brouard 4157: printf("%.6e ",hess[i][j]);
4158: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 4159: }
4160: printf("\n");
4161: fprintf(ficlog,"\n");
4162: }
4163:
1.203 brouard 4164: /* printf("\n#Covariance matrix#\n"); */
4165: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
4166: /* for (i=1;i<=npar;i++) { */
4167: /* for (j=1;j<=npar;j++) { */
4168: /* printf("%.6e ",matcov[i][j]); */
4169: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
4170: /* } */
4171: /* printf("\n"); */
4172: /* fprintf(ficlog,"\n"); */
4173: /* } */
4174:
1.126 brouard 4175: /* Recompute Inverse */
1.203 brouard 4176: /* for (i=1;i<=npar;i++) */
4177: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
4178: /* ludcmp(a,npar,indx,&pd); */
4179:
4180: /* printf("\n#Hessian matrix recomputed#\n"); */
4181:
4182: /* for (j=1;j<=npar;j++) { */
4183: /* for (i=1;i<=npar;i++) x[i]=0; */
4184: /* x[j]=1; */
4185: /* lubksb(a,npar,indx,x); */
4186: /* for (i=1;i<=npar;i++){ */
4187: /* y[i][j]=x[i]; */
4188: /* printf("%.3e ",y[i][j]); */
4189: /* fprintf(ficlog,"%.3e ",y[i][j]); */
4190: /* } */
4191: /* printf("\n"); */
4192: /* fprintf(ficlog,"\n"); */
4193: /* } */
4194:
4195: /* Verifying the inverse matrix */
4196: #ifdef DEBUGHESS
4197: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 4198:
1.203 brouard 4199: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
4200: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 4201:
4202: for (j=1;j<=npar;j++) {
4203: for (i=1;i<=npar;i++){
1.203 brouard 4204: printf("%.2f ",y[i][j]);
4205: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 4206: }
4207: printf("\n");
4208: fprintf(ficlog,"\n");
4209: }
1.203 brouard 4210: #endif
1.126 brouard 4211:
4212: free_matrix(a,1,npar,1,npar);
4213: free_matrix(y,1,npar,1,npar);
4214: free_vector(x,1,npar);
4215: free_ivector(indx,1,npar);
1.203 brouard 4216: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 4217:
4218:
4219: }
4220:
4221: /*************** hessian matrix ****************/
4222: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 4223: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 4224: int i;
4225: int l=1, lmax=20;
1.203 brouard 4226: double k1,k2, res, fx;
1.132 brouard 4227: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 4228: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
4229: int k=0,kmax=10;
4230: double l1;
4231:
4232: fx=func(x);
4233: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 4234: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 4235: l1=pow(10,l);
4236: delts=delt;
4237: for(k=1 ; k <kmax; k=k+1){
4238: delt = delta*(l1*k);
4239: p2[theta]=x[theta] +delt;
1.145 brouard 4240: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 4241: p2[theta]=x[theta]-delt;
4242: k2=func(p2)-fx;
4243: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 4244: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 4245:
1.203 brouard 4246: #ifdef DEBUGHESSII
1.126 brouard 4247: 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);
4248: 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);
4249: #endif
4250: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
4251: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
4252: k=kmax;
4253: }
4254: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 4255: k=kmax; l=lmax*10;
1.126 brouard 4256: }
4257: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
4258: delts=delt;
4259: }
1.203 brouard 4260: } /* End loop k */
1.126 brouard 4261: }
4262: delti[theta]=delts;
4263: return res;
4264:
4265: }
4266:
1.203 brouard 4267: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 4268: {
4269: int i;
1.164 brouard 4270: int l=1, lmax=20;
1.126 brouard 4271: double k1,k2,k3,k4,res,fx;
1.132 brouard 4272: double p2[MAXPARM+1];
1.203 brouard 4273: int k, kmax=1;
4274: double v1, v2, cv12, lc1, lc2;
1.208 brouard 4275:
4276: int firstime=0;
1.203 brouard 4277:
1.126 brouard 4278: fx=func(x);
1.203 brouard 4279: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 4280: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 4281: p2[thetai]=x[thetai]+delti[thetai]*k;
4282: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4283: k1=func(p2)-fx;
4284:
1.203 brouard 4285: p2[thetai]=x[thetai]+delti[thetai]*k;
4286: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4287: k2=func(p2)-fx;
4288:
1.203 brouard 4289: p2[thetai]=x[thetai]-delti[thetai]*k;
4290: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4291: k3=func(p2)-fx;
4292:
1.203 brouard 4293: p2[thetai]=x[thetai]-delti[thetai]*k;
4294: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4295: k4=func(p2)-fx;
1.203 brouard 4296: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
4297: if(k1*k2*k3*k4 <0.){
1.208 brouard 4298: firstime=1;
1.203 brouard 4299: kmax=kmax+10;
1.208 brouard 4300: }
4301: if(kmax >=10 || firstime ==1){
1.246 brouard 4302: 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);
4303: 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 4304: 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);
4305: 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);
4306: }
4307: #ifdef DEBUGHESSIJ
4308: v1=hess[thetai][thetai];
4309: v2=hess[thetaj][thetaj];
4310: cv12=res;
4311: /* Computing eigen value of Hessian matrix */
4312: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4313: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4314: if ((lc2 <0) || (lc1 <0) ){
4315: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4316: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4317: 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);
4318: 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);
4319: }
1.126 brouard 4320: #endif
4321: }
4322: return res;
4323: }
4324:
1.203 brouard 4325: /* Not done yet: Was supposed to fix if not exactly at the maximum */
4326: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
4327: /* { */
4328: /* int i; */
4329: /* int l=1, lmax=20; */
4330: /* double k1,k2,k3,k4,res,fx; */
4331: /* double p2[MAXPARM+1]; */
4332: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
4333: /* int k=0,kmax=10; */
4334: /* double l1; */
4335:
4336: /* fx=func(x); */
4337: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
4338: /* l1=pow(10,l); */
4339: /* delts=delt; */
4340: /* for(k=1 ; k <kmax; k=k+1){ */
4341: /* delt = delti*(l1*k); */
4342: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
4343: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4344: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4345: /* k1=func(p2)-fx; */
4346:
4347: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4348: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4349: /* k2=func(p2)-fx; */
4350:
4351: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4352: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4353: /* k3=func(p2)-fx; */
4354:
4355: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4356: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4357: /* k4=func(p2)-fx; */
4358: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
4359: /* #ifdef DEBUGHESSIJ */
4360: /* 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); */
4361: /* 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); */
4362: /* #endif */
4363: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
4364: /* k=kmax; */
4365: /* } */
4366: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
4367: /* k=kmax; l=lmax*10; */
4368: /* } */
4369: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
4370: /* delts=delt; */
4371: /* } */
4372: /* } /\* End loop k *\/ */
4373: /* } */
4374: /* delti[theta]=delts; */
4375: /* return res; */
4376: /* } */
4377:
4378:
1.126 brouard 4379: /************** Inverse of matrix **************/
4380: void ludcmp(double **a, int n, int *indx, double *d)
4381: {
4382: int i,imax,j,k;
4383: double big,dum,sum,temp;
4384: double *vv;
4385:
4386: vv=vector(1,n);
4387: *d=1.0;
4388: for (i=1;i<=n;i++) {
4389: big=0.0;
4390: for (j=1;j<=n;j++)
4391: if ((temp=fabs(a[i][j])) > big) big=temp;
1.256 brouard 4392: if (big == 0.0){
4393: printf(" Singular Hessian matrix at row %d:\n",i);
4394: for (j=1;j<=n;j++) {
4395: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
4396: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
4397: }
4398: fflush(ficlog);
4399: fclose(ficlog);
4400: nrerror("Singular matrix in routine ludcmp");
4401: }
1.126 brouard 4402: vv[i]=1.0/big;
4403: }
4404: for (j=1;j<=n;j++) {
4405: for (i=1;i<j;i++) {
4406: sum=a[i][j];
4407: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
4408: a[i][j]=sum;
4409: }
4410: big=0.0;
4411: for (i=j;i<=n;i++) {
4412: sum=a[i][j];
4413: for (k=1;k<j;k++)
4414: sum -= a[i][k]*a[k][j];
4415: a[i][j]=sum;
4416: if ( (dum=vv[i]*fabs(sum)) >= big) {
4417: big=dum;
4418: imax=i;
4419: }
4420: }
4421: if (j != imax) {
4422: for (k=1;k<=n;k++) {
4423: dum=a[imax][k];
4424: a[imax][k]=a[j][k];
4425: a[j][k]=dum;
4426: }
4427: *d = -(*d);
4428: vv[imax]=vv[j];
4429: }
4430: indx[j]=imax;
4431: if (a[j][j] == 0.0) a[j][j]=TINY;
4432: if (j != n) {
4433: dum=1.0/(a[j][j]);
4434: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4435: }
4436: }
4437: free_vector(vv,1,n); /* Doesn't work */
4438: ;
4439: }
4440:
4441: void lubksb(double **a, int n, int *indx, double b[])
4442: {
4443: int i,ii=0,ip,j;
4444: double sum;
4445:
4446: for (i=1;i<=n;i++) {
4447: ip=indx[i];
4448: sum=b[ip];
4449: b[ip]=b[i];
4450: if (ii)
4451: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4452: else if (sum) ii=i;
4453: b[i]=sum;
4454: }
4455: for (i=n;i>=1;i--) {
4456: sum=b[i];
4457: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4458: b[i]=sum/a[i][i];
4459: }
4460: }
4461:
4462: void pstamp(FILE *fichier)
4463: {
1.196 brouard 4464: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4465: }
4466:
1.297 brouard 4467: void date2dmy(double date,double *day, double *month, double *year){
4468: double yp=0., yp1=0., yp2=0.;
4469:
4470: yp1=modf(date,&yp);/* extracts integral of date in yp and
4471: fractional in yp1 */
4472: *year=yp;
4473: yp2=modf((yp1*12),&yp);
4474: *month=yp;
4475: yp1=modf((yp2*30.5),&yp);
4476: *day=yp;
4477: if(*day==0) *day=1;
4478: if(*month==0) *month=1;
4479: }
4480:
1.253 brouard 4481:
4482:
1.126 brouard 4483: /************ Frequencies ********************/
1.251 brouard 4484: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
1.226 brouard 4485: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4486: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.250 brouard 4487: { /* Some frequencies as well as proposing some starting values */
1.226 brouard 4488:
1.265 brouard 4489: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
1.226 brouard 4490: int iind=0, iage=0;
4491: int mi; /* Effective wave */
4492: int first;
4493: double ***freq; /* Frequencies */
1.268 brouard 4494: 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 */
4495: 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 4496: double *meanq, *stdq, *idq;
1.226 brouard 4497: double **meanqt;
4498: double *pp, **prop, *posprop, *pospropt;
4499: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4500: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4501: double agebegin, ageend;
4502:
4503: pp=vector(1,nlstate);
1.251 brouard 4504: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4505: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4506: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4507: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4508: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.284 brouard 4509: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.283 brouard 4510: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.226 brouard 4511: meanqt=matrix(1,lastpass,1,nqtveff);
4512: strcpy(fileresp,"P_");
4513: strcat(fileresp,fileresu);
4514: /*strcat(fileresphtm,fileresu);*/
4515: if((ficresp=fopen(fileresp,"w"))==NULL) {
4516: printf("Problem with prevalence resultfile: %s\n", fileresp);
4517: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4518: exit(0);
4519: }
1.240 brouard 4520:
1.226 brouard 4521: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4522: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4523: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4524: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4525: fflush(ficlog);
4526: exit(70);
4527: }
4528: else{
4529: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 4530: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4531: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4532: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4533: }
1.237 brouard 4534: 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 4535:
1.226 brouard 4536: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4537: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4538: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4539: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4540: fflush(ficlog);
4541: exit(70);
1.240 brouard 4542: } else{
1.226 brouard 4543: 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 4544: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4545: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4546: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4547: }
1.240 brouard 4548: 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);
4549:
1.253 brouard 4550: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
4551: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.251 brouard 4552: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4553: j1=0;
1.126 brouard 4554:
1.227 brouard 4555: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4556: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4557: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 4558:
4559:
1.226 brouard 4560: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4561: reference=low_education V1=0,V2=0
4562: med_educ V1=1 V2=0,
4563: high_educ V1=0 V2=1
4564: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4565: */
1.249 brouard 4566: dateintsum=0;
4567: k2cpt=0;
4568:
1.253 brouard 4569: if(cptcoveff == 0 )
1.265 brouard 4570: nl=1; /* Constant and age model only */
1.253 brouard 4571: else
4572: nl=2;
1.265 brouard 4573:
4574: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
4575: /* Loop on nj=1 or 2 if dummy covariates j!=0
4576: * Loop on j1(1 to 2**cptcoveff) covariate combination
4577: * freq[s1][s2][iage] =0.
4578: * Loop on iind
4579: * ++freq[s1][s2][iage] weighted
4580: * end iind
4581: * if covariate and j!0
4582: * headers Variable on one line
4583: * endif cov j!=0
4584: * header of frequency table by age
4585: * Loop on age
4586: * pp[s1]+=freq[s1][s2][iage] weighted
4587: * pos+=freq[s1][s2][iage] weighted
4588: * Loop on s1 initial state
4589: * fprintf(ficresp
4590: * end s1
4591: * end age
4592: * if j!=0 computes starting values
4593: * end compute starting values
4594: * end j1
4595: * end nl
4596: */
1.253 brouard 4597: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
4598: if(nj==1)
4599: j=0; /* First pass for the constant */
1.265 brouard 4600: else{
1.253 brouard 4601: j=cptcoveff; /* Other passes for the covariate values */
1.265 brouard 4602: }
1.251 brouard 4603: first=1;
1.265 brouard 4604: 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 4605: posproptt=0.;
4606: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4607: scanf("%d", i);*/
4608: for (i=-5; i<=nlstate+ndeath; i++)
1.265 brouard 4609: for (s2=-5; s2<=nlstate+ndeath; s2++)
1.251 brouard 4610: for(m=iagemin; m <= iagemax+3; m++)
1.265 brouard 4611: freq[i][s2][m]=0;
1.251 brouard 4612:
4613: for (i=1; i<=nlstate; i++) {
1.240 brouard 4614: for(m=iagemin; m <= iagemax+3; m++)
1.251 brouard 4615: prop[i][m]=0;
4616: posprop[i]=0;
4617: pospropt[i]=0;
4618: }
1.283 brouard 4619: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
1.284 brouard 4620: idq[z1]=0.;
4621: meanq[z1]=0.;
4622: stdq[z1]=0.;
1.283 brouard 4623: }
4624: /* for (z1=1; z1<= nqtveff; z1++) { */
1.251 brouard 4625: /* for(m=1;m<=lastpass;m++){ */
1.283 brouard 4626: /* meanqt[m][z1]=0.; */
4627: /* } */
4628: /* } */
1.251 brouard 4629: /* dateintsum=0; */
4630: /* k2cpt=0; */
4631:
1.265 brouard 4632: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
1.251 brouard 4633: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4634: bool=1;
4635: if(j !=0){
4636: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
4637: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
4638: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
4639: /* if(Tvaraff[z1] ==-20){ */
4640: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
4641: /* }else if(Tvaraff[z1] ==-10){ */
4642: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
4643: /* }else */
4644: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){ /* for combination j1 of covariates */
1.265 brouard 4645: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
1.251 brouard 4646: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
4647: /* 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",
4648: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
4649: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
4650: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
4651: } /* Onlyf fixed */
4652: } /* end z1 */
4653: } /* cptcovn > 0 */
4654: } /* end any */
4655: }/* end j==0 */
1.265 brouard 4656: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
1.251 brouard 4657: /* for(m=firstpass; m<=lastpass; m++){ */
1.284 brouard 4658: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
1.251 brouard 4659: m=mw[mi][iind];
4660: if(j!=0){
4661: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
4662: for (z1=1; z1<=cptcoveff; z1++) {
4663: if( Fixed[Tmodelind[z1]]==1){
4664: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4665: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality. If covariate's
4666: value is -1, we don't select. It differs from the
4667: constant and age model which counts them. */
4668: bool=0; /* not selected */
4669: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
4670: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4671: bool=0;
4672: }
4673: }
4674: }
4675: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
4676: } /* end j==0 */
4677: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
1.284 brouard 4678: if(bool==1){ /*Selected */
1.251 brouard 4679: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
4680: and mw[mi+1][iind]. dh depends on stepm. */
4681: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
4682: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
4683: if(m >=firstpass && m <=lastpass){
4684: k2=anint[m][iind]+(mint[m][iind]/12.);
4685: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
4686: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
4687: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
4688: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
4689: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4690: if (m<lastpass) {
4691: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
4692: /* 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]); */
4693: if(s[m][iind]==-1)
4694: 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.));
4695: 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.284 brouard 4696: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean */
4697: idq[z1]=idq[z1]+weight[iind];
4698: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
4699: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
4700: }
1.251 brouard 4701: /* if((int)agev[m][iind] == 55) */
4702: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
4703: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
4704: 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 4705: }
1.251 brouard 4706: } /* end if between passes */
4707: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
4708: dateintsum=dateintsum+k2; /* on all covariates ?*/
4709: k2cpt++;
4710: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
1.234 brouard 4711: }
1.251 brouard 4712: }else{
4713: bool=1;
4714: }/* end bool 2 */
4715: } /* end m */
1.284 brouard 4716: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
4717: /* idq[z1]=idq[z1]+weight[iind]; */
4718: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
4719: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
4720: /* } */
1.251 brouard 4721: } /* end bool */
4722: } /* end iind = 1 to imx */
4723: /* prop[s][age] is feeded for any initial and valid live state as well as
4724: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
4725:
4726:
4727: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.265 brouard 4728: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
4729: pstamp(ficresp);
1.251 brouard 4730: if (cptcoveff>0 && j!=0){
1.265 brouard 4731: pstamp(ficresp);
1.251 brouard 4732: printf( "\n#********** Variable ");
4733: fprintf(ficresp, "\n#********** Variable ");
4734: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4735: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
4736: fprintf(ficlog, "\n#********** Variable ");
4737: for (z1=1; z1<=cptcoveff; z1++){
4738: if(!FixedV[Tvaraff[z1]]){
4739: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4740: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4741: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4742: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4743: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.250 brouard 4744: }else{
1.251 brouard 4745: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4746: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4747: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4748: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4749: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4750: }
4751: }
4752: printf( "**********\n#");
4753: fprintf(ficresp, "**********\n#");
4754: fprintf(ficresphtm, "**********</h3>\n");
4755: fprintf(ficresphtmfr, "**********</h3>\n");
4756: fprintf(ficlog, "**********\n");
4757: }
1.284 brouard 4758: /*
4759: Printing means of quantitative variables if any
4760: */
4761: for (z1=1; z1<= nqfveff; z1++) {
1.285 brouard 4762: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.0f individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
1.284 brouard 4763: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]);
4764: if(weightopt==1){
4765: printf(" Weighted mean and standard deviation of");
4766: fprintf(ficlog," Weighted mean and standard deviation of");
4767: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
4768: }
1.285 brouard 4769: printf(" fixed quantitative variable V%d on %.0f representatives of the population : %6.3g (%6.3g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt((stdq[z1]-meanq[z1]*meanq[z1]/idq[z1])/idq[z1]));
4770: fprintf(ficlog," fixed quantitative variable V%d on %.0f representatives of the population : %6.3g (%6.3g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt((stdq[z1]-meanq[z1]*meanq[z1]/idq[z1])/idq[z1]));
4771: fprintf(ficresphtmfr," fixed quantitative variable V%d on %.0f representatives of the population : %6.3g (%6.3g)<p>\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt((stdq[z1]-meanq[z1]*meanq[z1]/idq[z1])/idq[z1]));
1.284 brouard 4772: }
4773: /* for (z1=1; z1<= nqtveff; z1++) { */
4774: /* for(m=1;m<=lastpass;m++){ */
4775: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
4776: /* } */
4777: /* } */
1.283 brouard 4778:
1.251 brouard 4779: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.265 brouard 4780: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
4781: fprintf(ficresp, " Age");
4782: 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 4783: for(i=1; i<=nlstate;i++) {
1.265 brouard 4784: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
1.251 brouard 4785: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4786: }
1.265 brouard 4787: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
1.251 brouard 4788: fprintf(ficresphtm, "\n");
4789:
4790: /* Header of frequency table by age */
4791: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4792: fprintf(ficresphtmfr,"<th>Age</th> ");
1.265 brouard 4793: for(s2=-1; s2 <=nlstate+ndeath; s2++){
1.251 brouard 4794: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 4795: if(s2!=0 && m!=0)
4796: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
1.240 brouard 4797: }
1.226 brouard 4798: }
1.251 brouard 4799: fprintf(ficresphtmfr, "\n");
4800:
4801: /* For each age */
4802: for(iage=iagemin; iage <= iagemax+3; iage++){
4803: fprintf(ficresphtm,"<tr>");
4804: if(iage==iagemax+1){
4805: fprintf(ficlog,"1");
4806: fprintf(ficresphtmfr,"<tr><th>0</th> ");
4807: }else if(iage==iagemax+2){
4808: fprintf(ficlog,"0");
4809: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
4810: }else if(iage==iagemax+3){
4811: fprintf(ficlog,"Total");
4812: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
4813: }else{
1.240 brouard 4814: if(first==1){
1.251 brouard 4815: first=0;
4816: printf("See log file for details...\n");
4817: }
4818: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4819: fprintf(ficlog,"Age %d", iage);
4820: }
1.265 brouard 4821: for(s1=1; s1 <=nlstate ; s1++){
4822: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
4823: pp[s1] += freq[s1][m][iage];
1.251 brouard 4824: }
1.265 brouard 4825: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 4826: for(m=-1, pos=0; m <=0 ; m++)
1.265 brouard 4827: pos += freq[s1][m][iage];
4828: if(pp[s1]>=1.e-10){
1.251 brouard 4829: if(first==1){
1.265 brouard 4830: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 4831: }
1.265 brouard 4832: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 4833: }else{
4834: if(first==1)
1.265 brouard 4835: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
4836: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
1.240 brouard 4837: }
4838: }
4839:
1.265 brouard 4840: for(s1=1; s1 <=nlstate ; s1++){
4841: /* posprop[s1]=0; */
4842: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4843: pp[s1] += freq[s1][m][iage];
4844: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
4845:
4846: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
4847: pos += pp[s1]; /* pos is the total number of transitions until this age */
4848: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
4849: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4850: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
4851: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4852: }
4853:
4854: /* Writing ficresp */
4855: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
4856: if( iage <= iagemax){
4857: fprintf(ficresp," %d",iage);
4858: }
4859: }else if( nj==2){
4860: if( iage <= iagemax){
4861: fprintf(ficresp," %d",iage);
4862: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4863: }
1.240 brouard 4864: }
1.265 brouard 4865: for(s1=1; s1 <=nlstate ; s1++){
1.240 brouard 4866: if(pos>=1.e-5){
1.251 brouard 4867: if(first==1)
1.265 brouard 4868: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
4869: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
1.251 brouard 4870: }else{
4871: if(first==1)
1.265 brouard 4872: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
4873: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
1.251 brouard 4874: }
4875: if( iage <= iagemax){
4876: if(pos>=1.e-5){
1.265 brouard 4877: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
4878: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4879: }else if( nj==2){
4880: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4881: }
4882: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4883: /*probs[iage][s1][j1]= pp[s1]/pos;*/
4884: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
4885: } else{
4886: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
4887: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
1.251 brouard 4888: }
1.240 brouard 4889: }
1.265 brouard 4890: pospropt[s1] +=posprop[s1];
4891: } /* end loop s1 */
1.251 brouard 4892: /* pospropt=0.; */
1.265 brouard 4893: for(s1=-1; s1 <=nlstate+ndeath; s1++){
1.251 brouard 4894: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 4895: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
1.251 brouard 4896: if(first==1){
1.265 brouard 4897: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 4898: }
1.265 brouard 4899: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
4900: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 4901: }
1.265 brouard 4902: if(s1!=0 && m!=0)
4903: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
1.240 brouard 4904: }
1.265 brouard 4905: } /* end loop s1 */
1.251 brouard 4906: posproptt=0.;
1.265 brouard 4907: for(s1=1; s1 <=nlstate; s1++){
4908: posproptt += pospropt[s1];
1.251 brouard 4909: }
4910: fprintf(ficresphtmfr,"</tr>\n ");
1.265 brouard 4911: fprintf(ficresphtm,"</tr>\n");
4912: if((cptcoveff==0 && nj==1)|| nj==2 ) {
4913: if(iage <= iagemax)
4914: fprintf(ficresp,"\n");
1.240 brouard 4915: }
1.251 brouard 4916: if(first==1)
4917: printf("Others in log...\n");
4918: fprintf(ficlog,"\n");
4919: } /* end loop age iage */
1.265 brouard 4920:
1.251 brouard 4921: fprintf(ficresphtm,"<tr><th>Tot</th>");
1.265 brouard 4922: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 4923: if(posproptt < 1.e-5){
1.265 brouard 4924: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
1.251 brouard 4925: }else{
1.265 brouard 4926: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
1.240 brouard 4927: }
1.226 brouard 4928: }
1.251 brouard 4929: fprintf(ficresphtm,"</tr>\n");
4930: fprintf(ficresphtm,"</table>\n");
4931: fprintf(ficresphtmfr,"</table>\n");
1.226 brouard 4932: if(posproptt < 1.e-5){
1.251 brouard 4933: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4934: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
1.260 brouard 4935: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
4936: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
1.251 brouard 4937: invalidvarcomb[j1]=1;
1.226 brouard 4938: }else{
1.251 brouard 4939: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4940: invalidvarcomb[j1]=0;
1.226 brouard 4941: }
1.251 brouard 4942: fprintf(ficresphtmfr,"</table>\n");
4943: fprintf(ficlog,"\n");
4944: if(j!=0){
4945: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
1.265 brouard 4946: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 4947: for(k=1; k <=(nlstate+ndeath); k++){
4948: if (k != i) {
1.265 brouard 4949: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
1.253 brouard 4950: if(jj==1){ /* Constant case (in fact cste + age) */
1.251 brouard 4951: if(j1==1){ /* All dummy covariates to zero */
4952: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
4953: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
1.252 brouard 4954: printf("%d%d ",i,k);
4955: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 4956: 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]));
4957: 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]));
4958: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
1.251 brouard 4959: }
1.253 brouard 4960: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
4961: for(iage=iagemin; iage <= iagemax+3; iage++){
4962: x[iage]= (double)iage;
4963: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
1.265 brouard 4964: /* 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 4965: }
1.268 brouard 4966: /* Some are not finite, but linreg will ignore these ages */
4967: no=0;
1.253 brouard 4968: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
1.265 brouard 4969: pstart[s1]=b;
4970: pstart[s1-1]=a;
1.252 brouard 4971: }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 */
4972: 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]);
4973: 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 4974: 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 4975: printf("%d%d ",i,k);
4976: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 4977: 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 4978: }else{ /* Other cases, like quantitative fixed or varying covariates */
4979: ;
4980: }
4981: /* printf("%12.7f )", param[i][jj][k]); */
4982: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 4983: s1++;
1.251 brouard 4984: } /* end jj */
4985: } /* end k!= i */
4986: } /* end k */
1.265 brouard 4987: } /* end i, s1 */
1.251 brouard 4988: } /* end j !=0 */
4989: } /* end selected combination of covariate j1 */
4990: if(j==0){ /* We can estimate starting values from the occurences in each case */
4991: printf("#Freqsummary: Starting values for the constants:\n");
4992: fprintf(ficlog,"\n");
1.265 brouard 4993: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 4994: for(k=1; k <=(nlstate+ndeath); k++){
4995: if (k != i) {
4996: printf("%d%d ",i,k);
4997: fprintf(ficlog,"%d%d ",i,k);
4998: for(jj=1; jj <=ncovmodel; jj++){
1.265 brouard 4999: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
1.253 brouard 5000: if(jj==1){ /* Age has to be done */
1.265 brouard 5001: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5002: 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]));
5003: 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 5004: }
5005: /* printf("%12.7f )", param[i][jj][k]); */
5006: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 5007: s1++;
1.250 brouard 5008: }
1.251 brouard 5009: printf("\n");
5010: fprintf(ficlog,"\n");
1.250 brouard 5011: }
5012: }
1.284 brouard 5013: } /* end of state i */
1.251 brouard 5014: printf("#Freqsummary\n");
5015: fprintf(ficlog,"\n");
1.265 brouard 5016: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5017: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5018: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
5019: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5020: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5021: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
5022: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5023: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
1.251 brouard 5024: /* } */
5025: }
1.265 brouard 5026: } /* end loop s1 */
1.251 brouard 5027:
5028: printf("\n");
5029: fprintf(ficlog,"\n");
5030: } /* end j=0 */
1.249 brouard 5031: } /* end j */
1.252 brouard 5032:
1.253 brouard 5033: if(mle == -2){ /* We want to use these values as starting values */
1.252 brouard 5034: for(i=1, jk=1; i <=nlstate; i++){
5035: for(j=1; j <=nlstate+ndeath; j++){
5036: if(j!=i){
5037: /*ca[0]= k+'a'-1;ca[1]='\0';*/
5038: printf("%1d%1d",i,j);
5039: fprintf(ficparo,"%1d%1d",i,j);
5040: for(k=1; k<=ncovmodel;k++){
5041: /* printf(" %lf",param[i][j][k]); */
5042: /* fprintf(ficparo," %lf",param[i][j][k]); */
5043: p[jk]=pstart[jk];
5044: printf(" %f ",pstart[jk]);
5045: fprintf(ficparo," %f ",pstart[jk]);
5046: jk++;
5047: }
5048: printf("\n");
5049: fprintf(ficparo,"\n");
5050: }
5051: }
5052: }
5053: } /* end mle=-2 */
1.226 brouard 5054: dateintmean=dateintsum/k2cpt;
1.296 brouard 5055: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
1.240 brouard 5056:
1.226 brouard 5057: fclose(ficresp);
5058: fclose(ficresphtm);
5059: fclose(ficresphtmfr);
1.283 brouard 5060: free_vector(idq,1,nqfveff);
1.226 brouard 5061: free_vector(meanq,1,nqfveff);
1.284 brouard 5062: free_vector(stdq,1,nqfveff);
1.226 brouard 5063: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.253 brouard 5064: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5065: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.251 brouard 5066: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5067: free_vector(pospropt,1,nlstate);
5068: free_vector(posprop,1,nlstate);
1.251 brouard 5069: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5070: free_vector(pp,1,nlstate);
5071: /* End of freqsummary */
5072: }
1.126 brouard 5073:
1.268 brouard 5074: /* Simple linear regression */
5075: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
5076:
5077: /* y=a+bx regression */
5078: double sumx = 0.0; /* sum of x */
5079: double sumx2 = 0.0; /* sum of x**2 */
5080: double sumxy = 0.0; /* sum of x * y */
5081: double sumy = 0.0; /* sum of y */
5082: double sumy2 = 0.0; /* sum of y**2 */
5083: double sume2 = 0.0; /* sum of square or residuals */
5084: double yhat;
5085:
5086: double denom=0;
5087: int i;
5088: int ne=*no;
5089:
5090: for ( i=ifi, ne=0;i<=ila;i++) {
5091: if(!isfinite(x[i]) || !isfinite(y[i])){
5092: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5093: continue;
5094: }
5095: ne=ne+1;
5096: sumx += x[i];
5097: sumx2 += x[i]*x[i];
5098: sumxy += x[i] * y[i];
5099: sumy += y[i];
5100: sumy2 += y[i]*y[i];
5101: denom = (ne * sumx2 - sumx*sumx);
5102: /* 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); */
5103: }
5104:
5105: denom = (ne * sumx2 - sumx*sumx);
5106: if (denom == 0) {
5107: // vertical, slope m is infinity
5108: *b = INFINITY;
5109: *a = 0;
5110: if (r) *r = 0;
5111: return 1;
5112: }
5113:
5114: *b = (ne * sumxy - sumx * sumy) / denom;
5115: *a = (sumy * sumx2 - sumx * sumxy) / denom;
5116: if (r!=NULL) {
5117: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
5118: sqrt((sumx2 - sumx*sumx/ne) *
5119: (sumy2 - sumy*sumy/ne));
5120: }
5121: *no=ne;
5122: for ( i=ifi, ne=0;i<=ila;i++) {
5123: if(!isfinite(x[i]) || !isfinite(y[i])){
5124: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5125: continue;
5126: }
5127: ne=ne+1;
5128: yhat = y[i] - *a -*b* x[i];
5129: sume2 += yhat * yhat ;
5130:
5131: denom = (ne * sumx2 - sumx*sumx);
5132: /* 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); */
5133: }
5134: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
5135: *sa= *sb * sqrt(sumx2/ne);
5136:
5137: return 0;
5138: }
5139:
1.126 brouard 5140: /************ Prevalence ********************/
1.227 brouard 5141: 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)
5142: {
5143: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
5144: in each health status at the date of interview (if between dateprev1 and dateprev2).
5145: We still use firstpass and lastpass as another selection.
5146: */
1.126 brouard 5147:
1.227 brouard 5148: int i, m, jk, j1, bool, z1,j, iv;
5149: int mi; /* Effective wave */
5150: int iage;
5151: double agebegin, ageend;
5152:
5153: double **prop;
5154: double posprop;
5155: double y2; /* in fractional years */
5156: int iagemin, iagemax;
5157: int first; /** to stop verbosity which is redirected to log file */
5158:
5159: iagemin= (int) agemin;
5160: iagemax= (int) agemax;
5161: /*pp=vector(1,nlstate);*/
1.251 brouard 5162: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5163: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
5164: j1=0;
1.222 brouard 5165:
1.227 brouard 5166: /*j=cptcoveff;*/
5167: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 5168:
1.288 brouard 5169: first=0;
1.227 brouard 5170: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
5171: for (i=1; i<=nlstate; i++)
1.251 brouard 5172: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
1.227 brouard 5173: prop[i][iage]=0.0;
5174: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
5175: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
5176: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
5177:
5178: for (i=1; i<=imx; i++) { /* Each individual */
5179: bool=1;
5180: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
5181: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
5182: m=mw[mi][i];
5183: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
5184: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
5185: for (z1=1; z1<=cptcoveff; z1++){
5186: if( Fixed[Tmodelind[z1]]==1){
5187: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
5188: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
5189: bool=0;
5190: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
5191: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
5192: bool=0;
5193: }
5194: }
5195: if(bool==1){ /* Otherwise we skip that wave/person */
5196: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
5197: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
5198: if(m >=firstpass && m <=lastpass){
5199: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
5200: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
5201: if(agev[m][i]==0) agev[m][i]=iagemax+1;
5202: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.251 brouard 5203: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
1.227 brouard 5204: 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);
5205: exit(1);
5206: }
5207: if (s[m][i]>0 && s[m][i]<=nlstate) {
5208: /*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]]);*/
5209: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
5210: prop[s[m][i]][iagemax+3] += weight[i];
5211: } /* end valid statuses */
5212: } /* end selection of dates */
5213: } /* end selection of waves */
5214: } /* end bool */
5215: } /* end wave */
5216: } /* end individual */
5217: for(i=iagemin; i <= iagemax+3; i++){
5218: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
5219: posprop += prop[jk][i];
5220: }
5221:
5222: for(jk=1; jk <=nlstate ; jk++){
5223: if( i <= iagemax){
5224: if(posprop>=1.e-5){
5225: probs[i][jk][j1]= prop[jk][i]/posprop;
5226: } else{
1.288 brouard 5227: if(!first){
5228: first=1;
1.266 brouard 5229: 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]);
5230: }else{
1.288 brouard 5231: 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 5232: }
5233: }
5234: }
5235: }/* end jk */
5236: }/* end i */
1.222 brouard 5237: /*} *//* end i1 */
1.227 brouard 5238: } /* end j1 */
1.222 brouard 5239:
1.227 brouard 5240: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
5241: /*free_vector(pp,1,nlstate);*/
1.251 brouard 5242: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5243: } /* End of prevalence */
1.126 brouard 5244:
5245: /************* Waves Concatenation ***************/
5246:
5247: 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)
5248: {
1.298 brouard 5249: /* 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 5250: Death is a valid wave (if date is known).
5251: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
5252: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.298 brouard 5253: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
1.227 brouard 5254: */
1.126 brouard 5255:
1.224 brouard 5256: int i=0, mi=0, m=0, mli=0;
1.126 brouard 5257: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
5258: double sum=0., jmean=0.;*/
1.224 brouard 5259: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 5260: int j, k=0,jk, ju, jl;
5261: double sum=0.;
5262: first=0;
1.214 brouard 5263: firstwo=0;
1.217 brouard 5264: firsthree=0;
1.218 brouard 5265: firstfour=0;
1.164 brouard 5266: jmin=100000;
1.126 brouard 5267: jmax=-1;
5268: jmean=0.;
1.224 brouard 5269:
5270: /* Treating live states */
1.214 brouard 5271: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 5272: mi=0; /* First valid wave */
1.227 brouard 5273: mli=0; /* Last valid wave */
1.309 ! brouard 5274: m=firstpass; /* Loop on waves */
! 5275: while(s[m][i] <= nlstate){ /* a live state or unknown state */
1.227 brouard 5276: 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 */
5277: mli=m-1;/* mw[++mi][i]=m-1; */
5278: }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 5279: 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 5280: mli=m;
1.224 brouard 5281: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
5282: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 5283: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 5284: }
1.309 ! brouard 5285: else{ /* m = lastpass, eventual special issue with warning */
1.224 brouard 5286: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 5287: break;
1.224 brouard 5288: #else
1.309 ! brouard 5289: 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 5290: if(firsthree == 0){
1.302 brouard 5291: 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 5292: firsthree=1;
5293: }
1.302 brouard 5294: 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 5295: mw[++mi][i]=m; /* Valid transition with unknown status */
1.227 brouard 5296: mli=m;
5297: }
5298: if(s[m][i]==-2){ /* Vital status is really unknown */
5299: nbwarn++;
1.309 ! brouard 5300: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
1.227 brouard 5301: 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);
5302: 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);
5303: }
5304: break;
5305: }
5306: break;
1.224 brouard 5307: #endif
1.227 brouard 5308: }/* End m >= lastpass */
1.126 brouard 5309: }/* end while */
1.224 brouard 5310:
1.227 brouard 5311: /* 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 5312: /* After last pass */
1.224 brouard 5313: /* Treating death states */
1.214 brouard 5314: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 5315: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
5316: /* } */
1.126 brouard 5317: mi++; /* Death is another wave */
5318: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 5319: /* Only death is a correct wave */
1.126 brouard 5320: mw[mi][i]=m;
1.257 brouard 5321: } /* else not in a death state */
1.224 brouard 5322: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.257 brouard 5323: else if ((int) andc[i] != 9999) { /* Date of death is known */
1.218 brouard 5324: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.309 ! brouard 5325: 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 5326: nbwarn++;
5327: if(firstfiv==0){
1.309 ! brouard 5328: 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 5329: firstfiv=1;
5330: }else{
1.309 ! brouard 5331: 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 5332: }
1.309 ! brouard 5333: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
! 5334: }else{ /* Month of Death occured afer last wave month, potential bias */
1.227 brouard 5335: nberr++;
5336: if(firstwo==0){
1.309 ! brouard 5337: 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 5338: firstwo=1;
5339: }
1.309 ! brouard 5340: 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 5341: }
1.257 brouard 5342: }else{ /* if date of interview is unknown */
1.227 brouard 5343: /* death is known but not confirmed by death status at any wave */
5344: if(firstfour==0){
1.309 ! brouard 5345: 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 5346: firstfour=1;
5347: }
1.309 ! brouard 5348: 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 5349: }
1.224 brouard 5350: } /* end if date of death is known */
5351: #endif
1.309 ! brouard 5352: wav[i]=mi; /* mi should be the last effective wave (or mli), */
! 5353: /* wav[i]=mw[mi][i]; */
1.126 brouard 5354: if(mi==0){
5355: nbwarn++;
5356: if(first==0){
1.227 brouard 5357: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
5358: first=1;
1.126 brouard 5359: }
5360: if(first==1){
1.227 brouard 5361: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 5362: }
5363: } /* end mi==0 */
5364: } /* End individuals */
1.214 brouard 5365: /* wav and mw are no more changed */
1.223 brouard 5366:
1.214 brouard 5367:
1.126 brouard 5368: for(i=1; i<=imx; i++){
5369: for(mi=1; mi<wav[i];mi++){
5370: if (stepm <=0)
1.227 brouard 5371: dh[mi][i]=1;
1.126 brouard 5372: else{
1.260 brouard 5373: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
1.227 brouard 5374: if (agedc[i] < 2*AGESUP) {
5375: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
5376: if(j==0) j=1; /* Survives at least one month after exam */
5377: else if(j<0){
5378: nberr++;
5379: 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]);
5380: j=1; /* Temporary Dangerous patch */
5381: 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);
5382: 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]);
5383: 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);
5384: }
5385: k=k+1;
5386: if (j >= jmax){
5387: jmax=j;
5388: ijmax=i;
5389: }
5390: if (j <= jmin){
5391: jmin=j;
5392: ijmin=i;
5393: }
5394: sum=sum+j;
5395: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
5396: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
5397: }
5398: }
5399: else{
5400: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 5401: /* 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 5402:
1.227 brouard 5403: k=k+1;
5404: if (j >= jmax) {
5405: jmax=j;
5406: ijmax=i;
5407: }
5408: else if (j <= jmin){
5409: jmin=j;
5410: ijmin=i;
5411: }
5412: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
5413: /*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]);*/
5414: if(j<0){
5415: nberr++;
5416: 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]);
5417: 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]);
5418: }
5419: sum=sum+j;
5420: }
5421: jk= j/stepm;
5422: jl= j -jk*stepm;
5423: ju= j -(jk+1)*stepm;
5424: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
5425: if(jl==0){
5426: dh[mi][i]=jk;
5427: bh[mi][i]=0;
5428: }else{ /* We want a negative bias in order to only have interpolation ie
5429: * to avoid the price of an extra matrix product in likelihood */
5430: dh[mi][i]=jk+1;
5431: bh[mi][i]=ju;
5432: }
5433: }else{
5434: if(jl <= -ju){
5435: dh[mi][i]=jk;
5436: bh[mi][i]=jl; /* bias is positive if real duration
5437: * is higher than the multiple of stepm and negative otherwise.
5438: */
5439: }
5440: else{
5441: dh[mi][i]=jk+1;
5442: bh[mi][i]=ju;
5443: }
5444: if(dh[mi][i]==0){
5445: dh[mi][i]=1; /* At least one step */
5446: bh[mi][i]=ju; /* At least one step */
5447: /* 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);*/
5448: }
5449: } /* end if mle */
1.126 brouard 5450: }
5451: } /* end wave */
5452: }
5453: jmean=sum/k;
5454: 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 5455: 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 5456: }
1.126 brouard 5457:
5458: /*********** Tricode ****************************/
1.220 brouard 5459: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 5460: {
5461: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
5462: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
5463: * Boring subroutine which should only output nbcode[Tvar[j]][k]
5464: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
5465: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
5466: */
1.130 brouard 5467:
1.242 brouard 5468: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
5469: int modmaxcovj=0; /* Modality max of covariates j */
5470: int cptcode=0; /* Modality max of covariates j */
5471: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 5472:
5473:
1.242 brouard 5474: /* cptcoveff=0; */
5475: /* *cptcov=0; */
1.126 brouard 5476:
1.242 brouard 5477: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.285 brouard 5478: for (k=1; k <= maxncov; k++)
5479: for(j=1; j<=2; j++)
5480: nbcode[k][j]=0; /* Valgrind */
1.126 brouard 5481:
1.242 brouard 5482: /* Loop on covariates without age and products and no quantitative variable */
5483: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
5484: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
5485: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
5486: switch(Fixed[k]) {
5487: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
5488: 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*/
5489: ij=(int)(covar[Tvar[k]][i]);
5490: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
5491: * If product of Vn*Vm, still boolean *:
5492: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
5493: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
5494: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
5495: modality of the nth covariate of individual i. */
5496: if (ij > modmaxcovj)
5497: modmaxcovj=ij;
5498: else if (ij < modmincovj)
5499: modmincovj=ij;
1.287 brouard 5500: if (ij <0 || ij >1 ){
5501: printf("Information, IMaCh doesn't treat covariate with missing values (-1), individual %d will be skipped.\n",i);
5502: fprintf(ficlog,"Information, currently IMaCh doesn't treat covariate with missing values (-1), individual %d will be skipped.\n",i);
5503: }
5504: if ((ij < -1) || (ij > NCOVMAX)){
1.242 brouard 5505: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
5506: exit(1);
5507: }else
5508: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
5509: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
5510: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
5511: /* getting the maximum value of the modality of the covariate
5512: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
5513: female ies 1, then modmaxcovj=1.
5514: */
5515: } /* end for loop on individuals i */
5516: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5517: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5518: cptcode=modmaxcovj;
5519: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
5520: /*for (i=0; i<=cptcode; i++) {*/
5521: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
5522: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5523: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5524: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
5525: if( j != -1){
5526: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
5527: covariate for which somebody answered excluding
5528: undefined. Usually 2: 0 and 1. */
5529: }
5530: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
5531: covariate for which somebody answered including
5532: undefined. Usually 3: -1, 0 and 1. */
5533: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
5534: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
5535: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 5536:
1.242 brouard 5537: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
5538: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
5539: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
5540: /* modmincovj=3; modmaxcovj = 7; */
5541: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
5542: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
5543: /* defining two dummy variables: variables V1_1 and V1_2.*/
5544: /* nbcode[Tvar[j]][ij]=k; */
5545: /* nbcode[Tvar[j]][1]=0; */
5546: /* nbcode[Tvar[j]][2]=1; */
5547: /* nbcode[Tvar[j]][3]=2; */
5548: /* To be continued (not working yet). */
5549: ij=0; /* ij is similar to i but can jump over null modalities */
1.287 brouard 5550:
5551: /* 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*/
5552: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
5553: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
5554: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
5555: /*, could be restored in the future */
5556: 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 5557: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
5558: break;
5559: }
5560: ij++;
1.287 brouard 5561: 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 5562: cptcode = ij; /* New max modality for covar j */
5563: } /* end of loop on modality i=-1 to 1 or more */
5564: break;
5565: case 1: /* Testing on varying covariate, could be simple and
5566: * should look at waves or product of fixed *
5567: * varying. No time to test -1, assuming 0 and 1 only */
5568: ij=0;
5569: for(i=0; i<=1;i++){
5570: nbcode[Tvar[k]][++ij]=i;
5571: }
5572: break;
5573: default:
5574: break;
5575: } /* end switch */
5576: } /* end dummy test */
1.287 brouard 5577: } /* 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 5578:
5579: for (k=-1; k< maxncov; k++) Ndum[k]=0;
5580: /* Look at fixed dummy (single or product) covariates to check empty modalities */
5581: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
5582: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
5583: 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 */
5584: 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 */
5585: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
5586: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
5587:
5588: ij=0;
5589: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
5590: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
5591: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
5592: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
5593: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
5594: /* If product not in single variable we don't print results */
5595: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
5596: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
5597: 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*/
5598: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
5599: 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 */
5600: if(Fixed[k]!=0)
5601: anyvaryingduminmodel=1;
5602: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
5603: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
5604: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
5605: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
5606: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
5607: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
5608: }
5609: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
5610: /* ij--; */
5611: /* cptcoveff=ij; /\*Number of total covariates*\/ */
5612: *cptcov=ij; /*Number of total real effective covariates: effective
5613: * because they can be excluded from the model and real
5614: * if in the model but excluded because missing values, but how to get k from ij?*/
5615: for(j=ij+1; j<= cptcovt; j++){
5616: Tvaraff[j]=0;
5617: Tmodelind[j]=0;
5618: }
5619: for(j=ntveff+1; j<= cptcovt; j++){
5620: TmodelInvind[j]=0;
5621: }
5622: /* To be sorted */
5623: ;
5624: }
1.126 brouard 5625:
1.145 brouard 5626:
1.126 brouard 5627: /*********** Health Expectancies ****************/
5628:
1.235 brouard 5629: 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 5630:
5631: {
5632: /* Health expectancies, no variances */
1.164 brouard 5633: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 5634: int nhstepma, nstepma; /* Decreasing with age */
5635: double age, agelim, hf;
5636: double ***p3mat;
5637: double eip;
5638:
1.238 brouard 5639: /* pstamp(ficreseij); */
1.126 brouard 5640: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
5641: fprintf(ficreseij,"# Age");
5642: for(i=1; i<=nlstate;i++){
5643: for(j=1; j<=nlstate;j++){
5644: fprintf(ficreseij," e%1d%1d ",i,j);
5645: }
5646: fprintf(ficreseij," e%1d. ",i);
5647: }
5648: fprintf(ficreseij,"\n");
5649:
5650:
5651: if(estepm < stepm){
5652: printf ("Problem %d lower than %d\n",estepm, stepm);
5653: }
5654: else hstepm=estepm;
5655: /* We compute the life expectancy from trapezoids spaced every estepm months
5656: * This is mainly to measure the difference between two models: for example
5657: * if stepm=24 months pijx are given only every 2 years and by summing them
5658: * we are calculating an estimate of the Life Expectancy assuming a linear
5659: * progression in between and thus overestimating or underestimating according
5660: * to the curvature of the survival function. If, for the same date, we
5661: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5662: * to compare the new estimate of Life expectancy with the same linear
5663: * hypothesis. A more precise result, taking into account a more precise
5664: * curvature will be obtained if estepm is as small as stepm. */
5665:
5666: /* For example we decided to compute the life expectancy with the smallest unit */
5667: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5668: nhstepm is the number of hstepm from age to agelim
5669: nstepm is the number of stepm from age to agelin.
1.270 brouard 5670: Look at hpijx to understand the reason which relies in memory size consideration
1.126 brouard 5671: and note for a fixed period like estepm months */
5672: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5673: survival function given by stepm (the optimization length). Unfortunately it
5674: means that if the survival funtion is printed only each two years of age and if
5675: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5676: results. So we changed our mind and took the option of the best precision.
5677: */
5678: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5679:
5680: agelim=AGESUP;
5681: /* If stepm=6 months */
5682: /* Computed by stepm unit matrices, product of hstepm matrices, stored
5683: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
5684:
5685: /* nhstepm age range expressed in number of stepm */
5686: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5687: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5688: /* if (stepm >= YEARM) hstepm=1;*/
5689: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5690: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5691:
5692: for (age=bage; age<=fage; age ++){
5693: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5694: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5695: /* if (stepm >= YEARM) hstepm=1;*/
5696: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
5697:
5698: /* If stepm=6 months */
5699: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5700: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5701:
1.235 brouard 5702: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 5703:
5704: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5705:
5706: printf("%d|",(int)age);fflush(stdout);
5707: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5708:
5709: /* Computing expectancies */
5710: for(i=1; i<=nlstate;i++)
5711: for(j=1; j<=nlstate;j++)
5712: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5713: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
5714:
5715: /* 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]);*/
5716:
5717: }
5718:
5719: fprintf(ficreseij,"%3.0f",age );
5720: for(i=1; i<=nlstate;i++){
5721: eip=0;
5722: for(j=1; j<=nlstate;j++){
5723: eip +=eij[i][j][(int)age];
5724: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
5725: }
5726: fprintf(ficreseij,"%9.4f", eip );
5727: }
5728: fprintf(ficreseij,"\n");
5729:
5730: }
5731: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5732: printf("\n");
5733: fprintf(ficlog,"\n");
5734:
5735: }
5736:
1.235 brouard 5737: 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 5738:
5739: {
5740: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 5741: to initial status i, ei. .
1.126 brouard 5742: */
5743: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
5744: int nhstepma, nstepma; /* Decreasing with age */
5745: double age, agelim, hf;
5746: double ***p3matp, ***p3matm, ***varhe;
5747: double **dnewm,**doldm;
5748: double *xp, *xm;
5749: double **gp, **gm;
5750: double ***gradg, ***trgradg;
5751: int theta;
5752:
5753: double eip, vip;
5754:
5755: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
5756: xp=vector(1,npar);
5757: xm=vector(1,npar);
5758: dnewm=matrix(1,nlstate*nlstate,1,npar);
5759: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
5760:
5761: pstamp(ficresstdeij);
5762: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
5763: fprintf(ficresstdeij,"# Age");
5764: for(i=1; i<=nlstate;i++){
5765: for(j=1; j<=nlstate;j++)
5766: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
5767: fprintf(ficresstdeij," e%1d. ",i);
5768: }
5769: fprintf(ficresstdeij,"\n");
5770:
5771: pstamp(ficrescveij);
5772: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
5773: fprintf(ficrescveij,"# Age");
5774: for(i=1; i<=nlstate;i++)
5775: for(j=1; j<=nlstate;j++){
5776: cptj= (j-1)*nlstate+i;
5777: for(i2=1; i2<=nlstate;i2++)
5778: for(j2=1; j2<=nlstate;j2++){
5779: cptj2= (j2-1)*nlstate+i2;
5780: if(cptj2 <= cptj)
5781: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
5782: }
5783: }
5784: fprintf(ficrescveij,"\n");
5785:
5786: if(estepm < stepm){
5787: printf ("Problem %d lower than %d\n",estepm, stepm);
5788: }
5789: else hstepm=estepm;
5790: /* We compute the life expectancy from trapezoids spaced every estepm months
5791: * This is mainly to measure the difference between two models: for example
5792: * if stepm=24 months pijx are given only every 2 years and by summing them
5793: * we are calculating an estimate of the Life Expectancy assuming a linear
5794: * progression in between and thus overestimating or underestimating according
5795: * to the curvature of the survival function. If, for the same date, we
5796: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5797: * to compare the new estimate of Life expectancy with the same linear
5798: * hypothesis. A more precise result, taking into account a more precise
5799: * curvature will be obtained if estepm is as small as stepm. */
5800:
5801: /* For example we decided to compute the life expectancy with the smallest unit */
5802: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5803: nhstepm is the number of hstepm from age to agelim
5804: nstepm is the number of stepm from age to agelin.
5805: Look at hpijx to understand the reason of that which relies in memory size
5806: and note for a fixed period like estepm months */
5807: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5808: survival function given by stepm (the optimization length). Unfortunately it
5809: means that if the survival funtion is printed only each two years of age and if
5810: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5811: results. So we changed our mind and took the option of the best precision.
5812: */
5813: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5814:
5815: /* If stepm=6 months */
5816: /* nhstepm age range expressed in number of stepm */
5817: agelim=AGESUP;
5818: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
5819: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5820: /* if (stepm >= YEARM) hstepm=1;*/
5821: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5822:
5823: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5824: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5825: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5826: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5827: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5828: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5829:
5830: for (age=bage; age<=fage; age ++){
5831: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5832: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5833: /* if (stepm >= YEARM) hstepm=1;*/
5834: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5835:
1.126 brouard 5836: /* If stepm=6 months */
5837: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5838: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5839:
5840: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5841:
1.126 brouard 5842: /* Computing Variances of health expectancies */
5843: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5844: decrease memory allocation */
5845: for(theta=1; theta <=npar; theta++){
5846: for(i=1; i<=npar; i++){
1.222 brouard 5847: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5848: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5849: }
1.235 brouard 5850: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
5851: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 5852:
1.126 brouard 5853: for(j=1; j<= nlstate; j++){
1.222 brouard 5854: for(i=1; i<=nlstate; i++){
5855: for(h=0; h<=nhstepm-1; h++){
5856: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5857: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5858: }
5859: }
1.126 brouard 5860: }
1.218 brouard 5861:
1.126 brouard 5862: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5863: for(h=0; h<=nhstepm-1; h++){
5864: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5865: }
1.126 brouard 5866: }/* End theta */
5867:
5868:
5869: for(h=0; h<=nhstepm-1; h++)
5870: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5871: for(theta=1; theta <=npar; theta++)
5872: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5873:
1.218 brouard 5874:
1.222 brouard 5875: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5876: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5877: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5878:
1.222 brouard 5879: printf("%d|",(int)age);fflush(stdout);
5880: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5881: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5882: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5883: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5884: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5885: for(ij=1;ij<=nlstate*nlstate;ij++)
5886: for(ji=1;ji<=nlstate*nlstate;ji++)
5887: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5888: }
5889: }
1.218 brouard 5890:
1.126 brouard 5891: /* Computing expectancies */
1.235 brouard 5892: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 5893: for(i=1; i<=nlstate;i++)
5894: for(j=1; j<=nlstate;j++)
1.222 brouard 5895: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5896: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5897:
1.222 brouard 5898: /* 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 5899:
1.222 brouard 5900: }
1.269 brouard 5901:
5902: /* Standard deviation of expectancies ij */
1.126 brouard 5903: fprintf(ficresstdeij,"%3.0f",age );
5904: for(i=1; i<=nlstate;i++){
5905: eip=0.;
5906: vip=0.;
5907: for(j=1; j<=nlstate;j++){
1.222 brouard 5908: eip += eij[i][j][(int)age];
5909: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5910: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5911: 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 5912: }
5913: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5914: }
5915: fprintf(ficresstdeij,"\n");
1.218 brouard 5916:
1.269 brouard 5917: /* Variance of expectancies ij */
1.126 brouard 5918: fprintf(ficrescveij,"%3.0f",age );
5919: for(i=1; i<=nlstate;i++)
5920: for(j=1; j<=nlstate;j++){
1.222 brouard 5921: cptj= (j-1)*nlstate+i;
5922: for(i2=1; i2<=nlstate;i2++)
5923: for(j2=1; j2<=nlstate;j2++){
5924: cptj2= (j2-1)*nlstate+i2;
5925: if(cptj2 <= cptj)
5926: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5927: }
1.126 brouard 5928: }
5929: fprintf(ficrescveij,"\n");
1.218 brouard 5930:
1.126 brouard 5931: }
5932: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5933: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5934: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5935: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5936: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5937: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5938: printf("\n");
5939: fprintf(ficlog,"\n");
1.218 brouard 5940:
1.126 brouard 5941: free_vector(xm,1,npar);
5942: free_vector(xp,1,npar);
5943: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5944: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5945: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5946: }
1.218 brouard 5947:
1.126 brouard 5948: /************ Variance ******************/
1.235 brouard 5949: 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 5950: {
1.279 brouard 5951: /** Variance of health expectancies
5952: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
5953: * double **newm;
5954: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
5955: */
1.218 brouard 5956:
5957: /* int movingaverage(); */
5958: double **dnewm,**doldm;
5959: double **dnewmp,**doldmp;
5960: int i, j, nhstepm, hstepm, h, nstepm ;
1.288 brouard 5961: int first=0;
1.218 brouard 5962: int k;
5963: double *xp;
1.279 brouard 5964: double **gp, **gm; /**< for var eij */
5965: double ***gradg, ***trgradg; /**< for var eij */
5966: double **gradgp, **trgradgp; /**< for var p point j */
5967: double *gpp, *gmp; /**< for var p point j */
5968: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
1.218 brouard 5969: double ***p3mat;
5970: double age,agelim, hf;
5971: /* double ***mobaverage; */
5972: int theta;
5973: char digit[4];
5974: char digitp[25];
5975:
5976: char fileresprobmorprev[FILENAMELENGTH];
5977:
5978: if(popbased==1){
5979: if(mobilav!=0)
5980: strcpy(digitp,"-POPULBASED-MOBILAV_");
5981: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
5982: }
5983: else
5984: strcpy(digitp,"-STABLBASED_");
1.126 brouard 5985:
1.218 brouard 5986: /* if (mobilav!=0) { */
5987: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5988: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
5989: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
5990: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
5991: /* } */
5992: /* } */
5993:
5994: strcpy(fileresprobmorprev,"PRMORPREV-");
5995: sprintf(digit,"%-d",ij);
5996: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
5997: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5998: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5999: strcat(fileresprobmorprev,fileresu);
6000: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
6001: printf("Problem with resultfile: %s\n", fileresprobmorprev);
6002: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
6003: }
6004: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6005: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6006: pstamp(ficresprobmorprev);
6007: 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 6008: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
6009: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
6010: fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
6011: }
6012: for(j=1;j<=cptcoveff;j++)
6013: fprintf(ficresprobmorprev,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,j)]);
6014: fprintf(ficresprobmorprev,"\n");
6015:
1.218 brouard 6016: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
6017: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6018: fprintf(ficresprobmorprev," p.%-d SE",j);
6019: for(i=1; i<=nlstate;i++)
6020: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
6021: }
6022: fprintf(ficresprobmorprev,"\n");
6023:
6024: fprintf(ficgp,"\n# Routine varevsij");
6025: fprintf(ficgp,"\nunset title \n");
6026: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
6027: 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");
6028: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
1.279 brouard 6029:
1.218 brouard 6030: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6031: pstamp(ficresvij);
6032: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
6033: if(popbased==1)
6034: 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);
6035: else
6036: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
6037: fprintf(ficresvij,"# Age");
6038: for(i=1; i<=nlstate;i++)
6039: for(j=1; j<=nlstate;j++)
6040: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
6041: fprintf(ficresvij,"\n");
6042:
6043: xp=vector(1,npar);
6044: dnewm=matrix(1,nlstate,1,npar);
6045: doldm=matrix(1,nlstate,1,nlstate);
6046: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
6047: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6048:
6049: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
6050: gpp=vector(nlstate+1,nlstate+ndeath);
6051: gmp=vector(nlstate+1,nlstate+ndeath);
6052: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 6053:
1.218 brouard 6054: if(estepm < stepm){
6055: printf ("Problem %d lower than %d\n",estepm, stepm);
6056: }
6057: else hstepm=estepm;
6058: /* For example we decided to compute the life expectancy with the smallest unit */
6059: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6060: nhstepm is the number of hstepm from age to agelim
6061: nstepm is the number of stepm from age to agelim.
6062: Look at function hpijx to understand why because of memory size limitations,
6063: we decided (b) to get a life expectancy respecting the most precise curvature of the
6064: survival function given by stepm (the optimization length). Unfortunately it
6065: means that if the survival funtion is printed every two years of age and if
6066: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6067: results. So we changed our mind and took the option of the best precision.
6068: */
6069: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6070: agelim = AGESUP;
6071: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6072: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6073: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6074: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6075: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
6076: gp=matrix(0,nhstepm,1,nlstate);
6077: gm=matrix(0,nhstepm,1,nlstate);
6078:
6079:
6080: for(theta=1; theta <=npar; theta++){
6081: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
6082: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6083: }
1.279 brouard 6084: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
6085: * returns into prlim .
1.288 brouard 6086: */
1.242 brouard 6087: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.279 brouard 6088:
6089: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
1.218 brouard 6090: if (popbased==1) {
6091: if(mobilav ==0){
6092: for(i=1; i<=nlstate;i++)
6093: prlim[i][i]=probs[(int)age][i][ij];
6094: }else{ /* mobilav */
6095: for(i=1; i<=nlstate;i++)
6096: prlim[i][i]=mobaverage[(int)age][i][ij];
6097: }
6098: }
1.295 brouard 6099: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
1.279 brouard 6100: */
6101: 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 6102: /**< 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 6103: * at horizon h in state j including mortality.
6104: */
1.218 brouard 6105: for(j=1; j<= nlstate; j++){
6106: for(h=0; h<=nhstepm; h++){
6107: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
6108: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
6109: }
6110: }
1.279 brouard 6111: /* Next for computing shifted+ probability of death (h=1 means
1.218 brouard 6112: computed over hstepm matrices product = hstepm*stepm months)
1.279 brouard 6113: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
1.218 brouard 6114: */
6115: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6116: for(i=1,gpp[j]=0.; i<= nlstate; i++)
6117: gpp[j] += prlim[i][i]*p3mat[i][j][1];
1.279 brouard 6118: }
6119:
6120: /* Again with minus shift */
1.218 brouard 6121:
6122: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
6123: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6124:
1.242 brouard 6125: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 6126:
6127: if (popbased==1) {
6128: if(mobilav ==0){
6129: for(i=1; i<=nlstate;i++)
6130: prlim[i][i]=probs[(int)age][i][ij];
6131: }else{ /* mobilav */
6132: for(i=1; i<=nlstate;i++)
6133: prlim[i][i]=mobaverage[(int)age][i][ij];
6134: }
6135: }
6136:
1.235 brouard 6137: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 6138:
6139: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
6140: for(h=0; h<=nhstepm; h++){
6141: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
6142: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
6143: }
6144: }
6145: /* This for computing probability of death (h=1 means
6146: computed over hstepm matrices product = hstepm*stepm months)
6147: as a weighted average of prlim.
6148: */
6149: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6150: for(i=1,gmp[j]=0.; i<= nlstate; i++)
6151: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6152: }
1.279 brouard 6153: /* end shifting computations */
6154:
6155: /**< Computing gradient matrix at horizon h
6156: */
1.218 brouard 6157: for(j=1; j<= nlstate; j++) /* vareij */
6158: for(h=0; h<=nhstepm; h++){
6159: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
6160: }
1.279 brouard 6161: /**< Gradient of overall mortality p.3 (or p.j)
6162: */
6163: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
1.218 brouard 6164: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
6165: }
6166:
6167: } /* End theta */
1.279 brouard 6168:
6169: /* We got the gradient matrix for each theta and state j */
1.218 brouard 6170: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
6171:
6172: for(h=0; h<=nhstepm; h++) /* veij */
6173: for(j=1; j<=nlstate;j++)
6174: for(theta=1; theta <=npar; theta++)
6175: trgradg[h][j][theta]=gradg[h][theta][j];
6176:
6177: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
6178: for(theta=1; theta <=npar; theta++)
6179: trgradgp[j][theta]=gradgp[theta][j];
1.279 brouard 6180: /**< as well as its transposed matrix
6181: */
1.218 brouard 6182:
6183: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6184: for(i=1;i<=nlstate;i++)
6185: for(j=1;j<=nlstate;j++)
6186: vareij[i][j][(int)age] =0.;
1.279 brouard 6187:
6188: /* Computing trgradg by matcov by gradg at age and summing over h
6189: * and k (nhstepm) formula 15 of article
6190: * Lievre-Brouard-Heathcote
6191: */
6192:
1.218 brouard 6193: for(h=0;h<=nhstepm;h++){
6194: for(k=0;k<=nhstepm;k++){
6195: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
6196: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
6197: for(i=1;i<=nlstate;i++)
6198: for(j=1;j<=nlstate;j++)
6199: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
6200: }
6201: }
6202:
1.279 brouard 6203: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
6204: * p.j overall mortality formula 49 but computed directly because
6205: * we compute the grad (wix pijx) instead of grad (pijx),even if
6206: * wix is independent of theta.
6207: */
1.218 brouard 6208: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
6209: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
6210: for(j=nlstate+1;j<=nlstate+ndeath;j++)
6211: for(i=nlstate+1;i<=nlstate+ndeath;i++)
6212: varppt[j][i]=doldmp[j][i];
6213: /* end ppptj */
6214: /* x centered again */
6215:
1.242 brouard 6216: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 6217:
6218: if (popbased==1) {
6219: if(mobilav ==0){
6220: for(i=1; i<=nlstate;i++)
6221: prlim[i][i]=probs[(int)age][i][ij];
6222: }else{ /* mobilav */
6223: for(i=1; i<=nlstate;i++)
6224: prlim[i][i]=mobaverage[(int)age][i][ij];
6225: }
6226: }
6227:
6228: /* This for computing probability of death (h=1 means
6229: computed over hstepm (estepm) matrices product = hstepm*stepm months)
6230: as a weighted average of prlim.
6231: */
1.235 brouard 6232: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 6233: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6234: for(i=1,gmp[j]=0.;i<= nlstate; i++)
6235: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6236: }
6237: /* end probability of death */
6238:
6239: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
6240: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6241: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
6242: for(i=1; i<=nlstate;i++){
6243: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
6244: }
6245: }
6246: fprintf(ficresprobmorprev,"\n");
6247:
6248: fprintf(ficresvij,"%.0f ",age );
6249: for(i=1; i<=nlstate;i++)
6250: for(j=1; j<=nlstate;j++){
6251: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
6252: }
6253: fprintf(ficresvij,"\n");
6254: free_matrix(gp,0,nhstepm,1,nlstate);
6255: free_matrix(gm,0,nhstepm,1,nlstate);
6256: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
6257: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
6258: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6259: } /* End age */
6260: free_vector(gpp,nlstate+1,nlstate+ndeath);
6261: free_vector(gmp,nlstate+1,nlstate+ndeath);
6262: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
6263: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
6264: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
6265: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
6266: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
6267: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
6268: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
6269: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
6270: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
6271: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
6272: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
6273: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
6274: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
6275: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
6276: 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);
6277: /* 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 6278: */
1.218 brouard 6279: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
6280: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 6281:
1.218 brouard 6282: free_vector(xp,1,npar);
6283: free_matrix(doldm,1,nlstate,1,nlstate);
6284: free_matrix(dnewm,1,nlstate,1,npar);
6285: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6286: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
6287: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6288: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6289: fclose(ficresprobmorprev);
6290: fflush(ficgp);
6291: fflush(fichtm);
6292: } /* end varevsij */
1.126 brouard 6293:
6294: /************ Variance of prevlim ******************/
1.269 brouard 6295: 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 6296: {
1.205 brouard 6297: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 6298: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 6299:
1.268 brouard 6300: double **dnewmpar,**doldm;
1.126 brouard 6301: int i, j, nhstepm, hstepm;
6302: double *xp;
6303: double *gp, *gm;
6304: double **gradg, **trgradg;
1.208 brouard 6305: double **mgm, **mgp;
1.126 brouard 6306: double age,agelim;
6307: int theta;
6308:
6309: pstamp(ficresvpl);
1.288 brouard 6310: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
1.241 brouard 6311: fprintf(ficresvpl,"# Age ");
6312: if(nresult >=1)
6313: fprintf(ficresvpl," Result# ");
1.126 brouard 6314: for(i=1; i<=nlstate;i++)
6315: fprintf(ficresvpl," %1d-%1d",i,i);
6316: fprintf(ficresvpl,"\n");
6317:
6318: xp=vector(1,npar);
1.268 brouard 6319: dnewmpar=matrix(1,nlstate,1,npar);
1.126 brouard 6320: doldm=matrix(1,nlstate,1,nlstate);
6321:
6322: hstepm=1*YEARM; /* Every year of age */
6323: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6324: agelim = AGESUP;
6325: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6326: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6327: if (stepm >= YEARM) hstepm=1;
6328: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6329: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 6330: mgp=matrix(1,npar,1,nlstate);
6331: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 6332: gp=vector(1,nlstate);
6333: gm=vector(1,nlstate);
6334:
6335: for(theta=1; theta <=npar; theta++){
6336: for(i=1; i<=npar; i++){ /* Computes gradient */
6337: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6338: }
1.288 brouard 6339: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6340: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6341: /* else */
6342: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6343: for(i=1;i<=nlstate;i++){
1.126 brouard 6344: gp[i] = prlim[i][i];
1.208 brouard 6345: mgp[theta][i] = prlim[i][i];
6346: }
1.126 brouard 6347: for(i=1; i<=npar; i++) /* Computes gradient */
6348: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6349: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6350: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6351: /* else */
6352: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6353: for(i=1;i<=nlstate;i++){
1.126 brouard 6354: gm[i] = prlim[i][i];
1.208 brouard 6355: mgm[theta][i] = prlim[i][i];
6356: }
1.126 brouard 6357: for(i=1;i<=nlstate;i++)
6358: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 6359: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 6360: } /* End theta */
6361:
6362: trgradg =matrix(1,nlstate,1,npar);
6363:
6364: for(j=1; j<=nlstate;j++)
6365: for(theta=1; theta <=npar; theta++)
6366: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 6367: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6368: /* printf("\nmgm mgp %d ",(int)age); */
6369: /* for(j=1; j<=nlstate;j++){ */
6370: /* printf(" %d ",j); */
6371: /* for(theta=1; theta <=npar; theta++) */
6372: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6373: /* printf("\n "); */
6374: /* } */
6375: /* } */
6376: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6377: /* printf("\n gradg %d ",(int)age); */
6378: /* for(j=1; j<=nlstate;j++){ */
6379: /* printf("%d ",j); */
6380: /* for(theta=1; theta <=npar; theta++) */
6381: /* printf("%d %lf ",theta,gradg[theta][j]); */
6382: /* printf("\n "); */
6383: /* } */
6384: /* } */
1.126 brouard 6385:
6386: for(i=1;i<=nlstate;i++)
6387: varpl[i][(int)age] =0.;
1.209 brouard 6388: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.268 brouard 6389: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6390: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6391: }else{
1.268 brouard 6392: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6393: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6394: }
1.126 brouard 6395: for(i=1;i<=nlstate;i++)
6396: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6397:
6398: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 6399: if(nresult >=1)
6400: fprintf(ficresvpl,"%d ",nres );
1.288 brouard 6401: for(i=1; i<=nlstate;i++){
1.126 brouard 6402: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1.288 brouard 6403: /* for(j=1;j<=nlstate;j++) */
6404: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
6405: }
1.126 brouard 6406: fprintf(ficresvpl,"\n");
6407: free_vector(gp,1,nlstate);
6408: free_vector(gm,1,nlstate);
1.208 brouard 6409: free_matrix(mgm,1,npar,1,nlstate);
6410: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 6411: free_matrix(gradg,1,npar,1,nlstate);
6412: free_matrix(trgradg,1,nlstate,1,npar);
6413: } /* End age */
6414:
6415: free_vector(xp,1,npar);
6416: free_matrix(doldm,1,nlstate,1,npar);
1.268 brouard 6417: free_matrix(dnewmpar,1,nlstate,1,nlstate);
6418:
6419: }
6420:
6421:
6422: /************ Variance of backprevalence limit ******************/
1.269 brouard 6423: 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 6424: {
6425: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
6426: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
6427:
6428: double **dnewmpar,**doldm;
6429: int i, j, nhstepm, hstepm;
6430: double *xp;
6431: double *gp, *gm;
6432: double **gradg, **trgradg;
6433: double **mgm, **mgp;
6434: double age,agelim;
6435: int theta;
6436:
6437: pstamp(ficresvbl);
6438: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
6439: fprintf(ficresvbl,"# Age ");
6440: if(nresult >=1)
6441: fprintf(ficresvbl," Result# ");
6442: for(i=1; i<=nlstate;i++)
6443: fprintf(ficresvbl," %1d-%1d",i,i);
6444: fprintf(ficresvbl,"\n");
6445:
6446: xp=vector(1,npar);
6447: dnewmpar=matrix(1,nlstate,1,npar);
6448: doldm=matrix(1,nlstate,1,nlstate);
6449:
6450: hstepm=1*YEARM; /* Every year of age */
6451: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6452: agelim = AGEINF;
6453: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
6454: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6455: if (stepm >= YEARM) hstepm=1;
6456: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6457: gradg=matrix(1,npar,1,nlstate);
6458: mgp=matrix(1,npar,1,nlstate);
6459: mgm=matrix(1,npar,1,nlstate);
6460: gp=vector(1,nlstate);
6461: gm=vector(1,nlstate);
6462:
6463: for(theta=1; theta <=npar; theta++){
6464: for(i=1; i<=npar; i++){ /* Computes gradient */
6465: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6466: }
6467: if(mobilavproj > 0 )
6468: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6469: else
6470: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6471: for(i=1;i<=nlstate;i++){
6472: gp[i] = bprlim[i][i];
6473: mgp[theta][i] = bprlim[i][i];
6474: }
6475: for(i=1; i<=npar; i++) /* Computes gradient */
6476: xp[i] = x[i] - (i==theta ?delti[theta]:0);
6477: if(mobilavproj > 0 )
6478: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6479: else
6480: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6481: for(i=1;i<=nlstate;i++){
6482: gm[i] = bprlim[i][i];
6483: mgm[theta][i] = bprlim[i][i];
6484: }
6485: for(i=1;i<=nlstate;i++)
6486: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
6487: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
6488: } /* End theta */
6489:
6490: trgradg =matrix(1,nlstate,1,npar);
6491:
6492: for(j=1; j<=nlstate;j++)
6493: for(theta=1; theta <=npar; theta++)
6494: trgradg[j][theta]=gradg[theta][j];
6495: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6496: /* printf("\nmgm mgp %d ",(int)age); */
6497: /* for(j=1; j<=nlstate;j++){ */
6498: /* printf(" %d ",j); */
6499: /* for(theta=1; theta <=npar; theta++) */
6500: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6501: /* printf("\n "); */
6502: /* } */
6503: /* } */
6504: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6505: /* printf("\n gradg %d ",(int)age); */
6506: /* for(j=1; j<=nlstate;j++){ */
6507: /* printf("%d ",j); */
6508: /* for(theta=1; theta <=npar; theta++) */
6509: /* printf("%d %lf ",theta,gradg[theta][j]); */
6510: /* printf("\n "); */
6511: /* } */
6512: /* } */
6513:
6514: for(i=1;i<=nlstate;i++)
6515: varbpl[i][(int)age] =0.;
6516: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
6517: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6518: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6519: }else{
6520: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6521: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6522: }
6523: for(i=1;i<=nlstate;i++)
6524: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6525:
6526: fprintf(ficresvbl,"%.0f ",age );
6527: if(nresult >=1)
6528: fprintf(ficresvbl,"%d ",nres );
6529: for(i=1; i<=nlstate;i++)
6530: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
6531: fprintf(ficresvbl,"\n");
6532: free_vector(gp,1,nlstate);
6533: free_vector(gm,1,nlstate);
6534: free_matrix(mgm,1,npar,1,nlstate);
6535: free_matrix(mgp,1,npar,1,nlstate);
6536: free_matrix(gradg,1,npar,1,nlstate);
6537: free_matrix(trgradg,1,nlstate,1,npar);
6538: } /* End age */
6539:
6540: free_vector(xp,1,npar);
6541: free_matrix(doldm,1,nlstate,1,npar);
6542: free_matrix(dnewmpar,1,nlstate,1,nlstate);
1.126 brouard 6543:
6544: }
6545:
6546: /************ Variance of one-step probabilities ******************/
6547: 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 6548: {
6549: int i, j=0, k1, l1, tj;
6550: int k2, l2, j1, z1;
6551: int k=0, l;
6552: int first=1, first1, first2;
6553: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
6554: double **dnewm,**doldm;
6555: double *xp;
6556: double *gp, *gm;
6557: double **gradg, **trgradg;
6558: double **mu;
6559: double age, cov[NCOVMAX+1];
6560: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
6561: int theta;
6562: char fileresprob[FILENAMELENGTH];
6563: char fileresprobcov[FILENAMELENGTH];
6564: char fileresprobcor[FILENAMELENGTH];
6565: double ***varpij;
6566:
6567: strcpy(fileresprob,"PROB_");
6568: strcat(fileresprob,fileres);
6569: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
6570: printf("Problem with resultfile: %s\n", fileresprob);
6571: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
6572: }
6573: strcpy(fileresprobcov,"PROBCOV_");
6574: strcat(fileresprobcov,fileresu);
6575: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
6576: printf("Problem with resultfile: %s\n", fileresprobcov);
6577: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
6578: }
6579: strcpy(fileresprobcor,"PROBCOR_");
6580: strcat(fileresprobcor,fileresu);
6581: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
6582: printf("Problem with resultfile: %s\n", fileresprobcor);
6583: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
6584: }
6585: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6586: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6587: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6588: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6589: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6590: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6591: pstamp(ficresprob);
6592: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
6593: fprintf(ficresprob,"# Age");
6594: pstamp(ficresprobcov);
6595: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
6596: fprintf(ficresprobcov,"# Age");
6597: pstamp(ficresprobcor);
6598: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
6599: fprintf(ficresprobcor,"# Age");
1.126 brouard 6600:
6601:
1.222 brouard 6602: for(i=1; i<=nlstate;i++)
6603: for(j=1; j<=(nlstate+ndeath);j++){
6604: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
6605: fprintf(ficresprobcov," p%1d-%1d ",i,j);
6606: fprintf(ficresprobcor," p%1d-%1d ",i,j);
6607: }
6608: /* fprintf(ficresprob,"\n");
6609: fprintf(ficresprobcov,"\n");
6610: fprintf(ficresprobcor,"\n");
6611: */
6612: xp=vector(1,npar);
6613: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6614: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6615: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
6616: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
6617: first=1;
6618: fprintf(ficgp,"\n# Routine varprob");
6619: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
6620: fprintf(fichtm,"\n");
6621:
1.288 brouard 6622: 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 6623: 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);
6624: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 6625: and drawn. It helps understanding how is the covariance between two incidences.\
6626: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 6627: 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 6628: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
6629: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
6630: standard deviations wide on each axis. <br>\
6631: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
6632: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
6633: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
6634:
1.222 brouard 6635: cov[1]=1;
6636: /* tj=cptcoveff; */
1.225 brouard 6637: tj = (int) pow(2,cptcoveff);
1.222 brouard 6638: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
6639: j1=0;
1.224 brouard 6640: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 6641: if (cptcovn>0) {
6642: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 6643: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6644: fprintf(ficresprob, "**********\n#\n");
6645: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 6646: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6647: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 6648:
1.222 brouard 6649: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 6650: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6651: fprintf(ficgp, "**********\n#\n");
1.220 brouard 6652:
6653:
1.222 brouard 6654: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 6655: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6656: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6657:
1.222 brouard 6658: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 6659: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6660: fprintf(ficresprobcor, "**********\n#");
6661: if(invalidvarcomb[j1]){
6662: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
6663: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
6664: continue;
6665: }
6666: }
6667: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
6668: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6669: gp=vector(1,(nlstate)*(nlstate+ndeath));
6670: gm=vector(1,(nlstate)*(nlstate+ndeath));
6671: for (age=bage; age<=fage; age ++){
6672: cov[2]=age;
6673: if(nagesqr==1)
6674: cov[3]= age*age;
6675: for (k=1; k<=cptcovn;k++) {
6676: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
6677: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
6678: * 1 1 1 1 1
6679: * 2 2 1 1 1
6680: * 3 1 2 1 1
6681: */
6682: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
6683: }
6684: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
6685: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
6686: for (k=1; k<=cptcovprod;k++)
6687: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 6688:
6689:
1.222 brouard 6690: for(theta=1; theta <=npar; theta++){
6691: for(i=1; i<=npar; i++)
6692: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 6693:
1.222 brouard 6694: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 6695:
1.222 brouard 6696: k=0;
6697: for(i=1; i<= (nlstate); i++){
6698: for(j=1; j<=(nlstate+ndeath);j++){
6699: k=k+1;
6700: gp[k]=pmmij[i][j];
6701: }
6702: }
1.220 brouard 6703:
1.222 brouard 6704: for(i=1; i<=npar; i++)
6705: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 6706:
1.222 brouard 6707: pmij(pmmij,cov,ncovmodel,xp,nlstate);
6708: k=0;
6709: for(i=1; i<=(nlstate); i++){
6710: for(j=1; j<=(nlstate+ndeath);j++){
6711: k=k+1;
6712: gm[k]=pmmij[i][j];
6713: }
6714: }
1.220 brouard 6715:
1.222 brouard 6716: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
6717: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
6718: }
1.126 brouard 6719:
1.222 brouard 6720: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
6721: for(theta=1; theta <=npar; theta++)
6722: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 6723:
1.222 brouard 6724: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
6725: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 6726:
1.222 brouard 6727: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 6728:
1.222 brouard 6729: k=0;
6730: for(i=1; i<=(nlstate); i++){
6731: for(j=1; j<=(nlstate+ndeath);j++){
6732: k=k+1;
6733: mu[k][(int) age]=pmmij[i][j];
6734: }
6735: }
6736: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
6737: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
6738: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 6739:
1.222 brouard 6740: /*printf("\n%d ",(int)age);
6741: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
6742: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
6743: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
6744: }*/
1.220 brouard 6745:
1.222 brouard 6746: fprintf(ficresprob,"\n%d ",(int)age);
6747: fprintf(ficresprobcov,"\n%d ",(int)age);
6748: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 6749:
1.222 brouard 6750: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
6751: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
6752: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
6753: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
6754: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
6755: }
6756: i=0;
6757: for (k=1; k<=(nlstate);k++){
6758: for (l=1; l<=(nlstate+ndeath);l++){
6759: i++;
6760: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
6761: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
6762: for (j=1; j<=i;j++){
6763: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
6764: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
6765: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
6766: }
6767: }
6768: }/* end of loop for state */
6769: } /* end of loop for age */
6770: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
6771: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
6772: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6773: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6774:
6775: /* Confidence intervalle of pij */
6776: /*
6777: fprintf(ficgp,"\nunset parametric;unset label");
6778: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
6779: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
6780: 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);
6781: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
6782: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
6783: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
6784: */
6785:
6786: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
6787: first1=1;first2=2;
6788: for (k2=1; k2<=(nlstate);k2++){
6789: for (l2=1; l2<=(nlstate+ndeath);l2++){
6790: if(l2==k2) continue;
6791: j=(k2-1)*(nlstate+ndeath)+l2;
6792: for (k1=1; k1<=(nlstate);k1++){
6793: for (l1=1; l1<=(nlstate+ndeath);l1++){
6794: if(l1==k1) continue;
6795: i=(k1-1)*(nlstate+ndeath)+l1;
6796: if(i<=j) continue;
6797: for (age=bage; age<=fage; age ++){
6798: if ((int)age %5==0){
6799: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
6800: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
6801: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
6802: mu1=mu[i][(int) age]/stepm*YEARM ;
6803: mu2=mu[j][(int) age]/stepm*YEARM;
6804: c12=cv12/sqrt(v1*v2);
6805: /* Computing eigen value of matrix of covariance */
6806: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6807: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6808: if ((lc2 <0) || (lc1 <0) ){
6809: if(first2==1){
6810: first1=0;
6811: 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);
6812: }
6813: 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);
6814: /* lc1=fabs(lc1); */ /* If we want to have them positive */
6815: /* lc2=fabs(lc2); */
6816: }
1.220 brouard 6817:
1.222 brouard 6818: /* Eigen vectors */
1.280 brouard 6819: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
6820: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
6821: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
6822: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
6823: }else
6824: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
1.222 brouard 6825: /*v21=sqrt(1.-v11*v11); *//* error */
6826: v21=(lc1-v1)/cv12*v11;
6827: v12=-v21;
6828: v22=v11;
6829: tnalp=v21/v11;
6830: if(first1==1){
6831: first1=0;
6832: 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);
6833: }
6834: 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);
6835: /*printf(fignu*/
6836: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
6837: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
6838: if(first==1){
6839: first=0;
6840: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
6841: fprintf(ficgp,"\nset parametric;unset label");
6842: 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);
6843: fprintf(ficgp,"\nset ter svg size 640, 480");
1.266 brouard 6844: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 6845: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 6846: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 6847: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
6848: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6849: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6850: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
6851: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6852: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6853: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6854: 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 6855: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
6856: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
1.222 brouard 6857: }else{
6858: first=0;
6859: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
6860: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6861: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6862: 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 6863: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
6864: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
1.222 brouard 6865: }/* if first */
6866: } /* age mod 5 */
6867: } /* end loop age */
6868: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6869: first=1;
6870: } /*l12 */
6871: } /* k12 */
6872: } /*l1 */
6873: }/* k1 */
6874: } /* loop on combination of covariates j1 */
6875: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
6876: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
6877: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6878: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
6879: free_vector(xp,1,npar);
6880: fclose(ficresprob);
6881: fclose(ficresprobcov);
6882: fclose(ficresprobcor);
6883: fflush(ficgp);
6884: fflush(fichtmcov);
6885: }
1.126 brouard 6886:
6887:
6888: /******************* Printing html file ***********/
1.201 brouard 6889: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 6890: int lastpass, int stepm, int weightopt, char model[],\
6891: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.296 brouard 6892: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
6893: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
6894: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
1.237 brouard 6895: int jj1, k1, i1, cpt, k4, nres;
1.126 brouard 6896:
6897: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
6898: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
6899: </ul>");
1.237 brouard 6900: fprintf(fichtm,"<ul><li> model=1+age+%s\n \
6901: </ul>", model);
1.214 brouard 6902: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
6903: 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",
6904: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
6905: 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 6906: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
6907: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 6908: fprintf(fichtm,"\
6909: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 6910: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 6911: fprintf(fichtm,"\
1.217 brouard 6912: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
6913: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
6914: fprintf(fichtm,"\
1.288 brouard 6915: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6916: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 6917: fprintf(fichtm,"\
1.288 brouard 6918: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.217 brouard 6919: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
6920: fprintf(fichtm,"\
1.211 brouard 6921: - (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 6922: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6923: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 6924: if(prevfcast==1){
6925: fprintf(fichtm,"\
6926: - Prevalence projections by age and states: \
1.201 brouard 6927: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 6928: }
1.126 brouard 6929:
6930:
1.225 brouard 6931: m=pow(2,cptcoveff);
1.222 brouard 6932: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6933:
1.264 brouard 6934: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
6935:
6936: jj1=0;
6937:
6938: fprintf(fichtm," \n<ul>");
6939: for(nres=1; nres <= nresult; nres++) /* For each resultline */
6940: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
6941: if(m != 1 && TKresult[nres]!= k1)
6942: continue;
6943: jj1++;
6944: if (cptcovn > 0) {
6945: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
6946: for (cpt=1; cpt<=cptcoveff;cpt++){
6947: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6948: }
6949: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6950: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
6951: }
6952: fprintf(fichtm,"\">");
6953:
6954: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
6955: fprintf(fichtm,"************ Results for covariates");
6956: for (cpt=1; cpt<=cptcoveff;cpt++){
6957: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6958: }
6959: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6960: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6961: }
6962: if(invalidvarcomb[k1]){
6963: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
6964: continue;
6965: }
6966: fprintf(fichtm,"</a></li>");
6967: } /* cptcovn >0 */
6968: }
6969: fprintf(fichtm," \n</ul>");
6970:
1.222 brouard 6971: jj1=0;
1.237 brouard 6972:
6973: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.241 brouard 6974: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
1.253 brouard 6975: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 6976: continue;
1.220 brouard 6977:
1.222 brouard 6978: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
6979: jj1++;
6980: if (cptcovn > 0) {
1.264 brouard 6981: fprintf(fichtm,"\n<p><a name=\"rescov");
6982: for (cpt=1; cpt<=cptcoveff;cpt++){
6983: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6984: }
6985: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6986: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
6987: }
6988: fprintf(fichtm,"\"</a>");
6989:
1.222 brouard 6990: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 6991: for (cpt=1; cpt<=cptcoveff;cpt++){
1.237 brouard 6992: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6993: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
6994: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
6995: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 6996: }
1.237 brouard 6997: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6998: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6999: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout);
7000: }
7001:
1.230 brouard 7002: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 7003: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
7004: if(invalidvarcomb[k1]){
7005: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
7006: printf("\nCombination (%d) ignored because no cases \n",k1);
7007: continue;
7008: }
7009: }
7010: /* aij, bij */
1.259 brouard 7011: 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 7012: <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 7013: /* Pij */
1.241 brouard 7014: 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> \
7015: <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 7016: /* Quasi-incidences */
7017: 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 7018: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 7019: 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 7020: 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> \
7021: <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 7022: /* Survival functions (period) in state j */
7023: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7024: 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 7025: <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 7026: }
7027: /* State specific survival functions (period) */
7028: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7029: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
7030: And probability to be observed in various states (up to %d) being in state %d at different ages. \
1.283 brouard 7031: <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 7032: }
1.288 brouard 7033: /* Period (forward stable) prevalence in each health state */
1.222 brouard 7034: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7035: 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> \
7036: <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 7037: }
1.296 brouard 7038: if(prevbcast==1){
1.288 brouard 7039: /* Backward prevalence in each health state */
1.222 brouard 7040: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7041: 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 7042: <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 7043: }
1.217 brouard 7044: }
1.222 brouard 7045: if(prevfcast==1){
1.288 brouard 7046: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
1.222 brouard 7047: for(cpt=1; cpt<=nlstate;cpt++){
1.288 brouard 7048: 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><br> \
1.296 brouard 7049: <img src=\"%s_%d-%d-%d.svg\">", dateprev1, dateprev2, mobilavproj, dateprojd, dateprojf, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.222 brouard 7050: }
7051: }
1.296 brouard 7052: if(prevbcast==1){
1.268 brouard 7053: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
7054: for(cpt=1; cpt<=nlstate;cpt++){
1.273 brouard 7055: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
7056: 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 \
7057: 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) \
7058: with weights corresponding to observed prevalence at different ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> \
7059: <img src=\"%s_%d-%d-%d.svg\">", dateprev1, dateprev2, mobilavproj, dateback1, dateback2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
1.268 brouard 7060: }
7061: }
1.220 brouard 7062:
1.222 brouard 7063: for(cpt=1; cpt<=nlstate;cpt++) {
1.241 brouard 7064: 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> <br> \
7065: <img src=\"%s_%d-%d-%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.222 brouard 7066: }
7067: /* } /\* end i1 *\/ */
7068: }/* End k1 */
7069: fprintf(fichtm,"</ul>");
1.126 brouard 7070:
1.222 brouard 7071: fprintf(fichtm,"\
1.126 brouard 7072: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 7073: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 7074: - 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 7075: But because parameters are usually highly correlated (a higher incidence of disability \
7076: and a higher incidence of recovery can give very close observed transition) it might \
7077: be very useful to look not only at linear confidence intervals estimated from the \
7078: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
7079: (parameters) of the logistic regression, it might be more meaningful to visualize the \
7080: covariance matrix of the one-step probabilities. \
7081: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 7082:
1.222 brouard 7083: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
7084: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
7085: fprintf(fichtm,"\
1.126 brouard 7086: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7087: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 7088:
1.222 brouard 7089: fprintf(fichtm,"\
1.126 brouard 7090: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7091: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
7092: fprintf(fichtm,"\
1.126 brouard 7093: - 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): \
7094: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7095: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 7096: fprintf(fichtm,"\
1.126 brouard 7097: - (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): \
7098: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7099: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 7100: fprintf(fichtm,"\
1.288 brouard 7101: - 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 7102: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
7103: fprintf(fichtm,"\
1.128 brouard 7104: - 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 7105: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
7106: fprintf(fichtm,"\
1.288 brouard 7107: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 7108: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 7109:
7110: /* if(popforecast==1) fprintf(fichtm,"\n */
7111: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
7112: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
7113: /* <br>",fileres,fileres,fileres,fileres); */
7114: /* else */
7115: /* 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 7116: fflush(fichtm);
7117: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 7118:
1.225 brouard 7119: m=pow(2,cptcoveff);
1.222 brouard 7120: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 7121:
1.222 brouard 7122: jj1=0;
1.237 brouard 7123:
1.241 brouard 7124: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.222 brouard 7125: for(k1=1; k1<=m;k1++){
1.253 brouard 7126: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7127: continue;
1.222 brouard 7128: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7129: jj1++;
1.126 brouard 7130: if (cptcovn > 0) {
7131: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 7132: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.237 brouard 7133: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);
7134: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7135: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7136: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7137: }
7138:
1.126 brouard 7139: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 7140:
1.222 brouard 7141: if(invalidvarcomb[k1]){
7142: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
7143: continue;
7144: }
1.126 brouard 7145: }
7146: for(cpt=1; cpt<=nlstate;cpt++) {
1.258 brouard 7147: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
1.241 brouard 7148: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d-%d.svg\"> %s_%d-%d-%d.svg</a>\n <br>\
1.258 brouard 7149: <img src=\"%s_%d-%d-%d.svg\">",mobilav,cpt,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
1.126 brouard 7150: }
7151: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 7152: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
7153: true period expectancies (those weighted with period prevalences are also\
7154: drawn in addition to the population based expectancies computed using\
1.241 brouard 7155: observed and cahotic prevalences: <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a>\n<br>\
7156: <img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 7157: /* } /\* end i1 *\/ */
7158: }/* End k1 */
1.241 brouard 7159: }/* End nres */
1.222 brouard 7160: fprintf(fichtm,"</ul>");
7161: fflush(fichtm);
1.126 brouard 7162: }
7163:
7164: /******************* Gnuplot file **************/
1.296 brouard 7165: 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 7166:
7167: char dirfileres[132],optfileres[132];
1.264 brouard 7168: char gplotcondition[132], gplotlabel[132];
1.237 brouard 7169: 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 7170: int lv=0, vlv=0, kl=0;
1.130 brouard 7171: int ng=0;
1.201 brouard 7172: int vpopbased;
1.223 brouard 7173: int ioffset; /* variable offset for columns */
1.270 brouard 7174: int iyearc=1; /* variable column for year of projection */
7175: int iagec=1; /* variable column for age of projection */
1.235 brouard 7176: int nres=0; /* Index of resultline */
1.266 brouard 7177: int istart=1; /* For starting graphs in projections */
1.219 brouard 7178:
1.126 brouard 7179: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
7180: /* printf("Problem with file %s",optionfilegnuplot); */
7181: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
7182: /* } */
7183:
7184: /*#ifdef windows */
7185: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 7186: /*#endif */
1.225 brouard 7187: m=pow(2,cptcoveff);
1.126 brouard 7188:
1.274 brouard 7189: /* diagram of the model */
7190: fprintf(ficgp,"\n#Diagram of the model \n");
7191: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
7192: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
7193: 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);
7194:
7195: 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);
7196: fprintf(ficgp,"\n#show arrow\nunset label\n");
7197: 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);
7198: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
7199: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
7200: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
7201: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
7202:
1.202 brouard 7203: /* Contribution to likelihood */
7204: /* Plot the probability implied in the likelihood */
1.223 brouard 7205: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
7206: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
7207: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
7208: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 7209: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 7210: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
7211: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 7212: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
7213: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
7214: 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));
7215: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
7216: 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));
7217: for (i=1; i<= nlstate ; i ++) {
7218: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
7219: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
7220: 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);
7221: for (j=2; j<= nlstate+ndeath ; j ++) {
7222: 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);
7223: }
7224: fprintf(ficgp,";\nset out; unset ylabel;\n");
7225: }
7226: /* 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 */
7227: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
7228: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
7229: fprintf(ficgp,"\nset out;unset log\n");
7230: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 7231:
1.126 brouard 7232: strcpy(dirfileres,optionfilefiname);
7233: strcpy(optfileres,"vpl");
1.223 brouard 7234: /* 1eme*/
1.238 brouard 7235: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
7236: for (k1=1; k1<= m ; k1 ++){ /* For each valid combination of covariate */
1.236 brouard 7237: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.238 brouard 7238: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
1.253 brouard 7239: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7240: continue;
7241: /* We are interested in selected combination by the resultline */
1.246 brouard 7242: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
1.288 brouard 7243: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.264 brouard 7244: strcpy(gplotlabel,"(");
1.238 brouard 7245: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
7246: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
7247: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7248: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7249: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7250: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
7251: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
1.246 brouard 7252: /* printf(" V%d=%d ",Tvaraff[k],vlv); */
1.238 brouard 7253: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7254: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7255: }
7256: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.246 brouard 7257: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.238 brouard 7258: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7259: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7260: }
7261: strcpy(gplotlabel+strlen(gplotlabel),")");
1.246 brouard 7262: /* printf("\n#\n"); */
1.238 brouard 7263: fprintf(ficgp,"\n#\n");
7264: if(invalidvarcomb[k1]){
1.260 brouard 7265: /*k1=k1-1;*/ /* To be checked */
1.238 brouard 7266: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7267: continue;
7268: }
1.235 brouard 7269:
1.241 brouard 7270: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
7271: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
1.276 brouard 7272: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
7273: fprintf(ficgp,"set title \"Alive state %d %s\" font \"Helvetica,12\"\n",cpt,gplotlabel);
1.260 brouard 7274: 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);
7275: /* 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); */
7276: /* k1-1 error should be nres-1*/
1.238 brouard 7277: for (i=1; i<= nlstate ; i ++) {
7278: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7279: else fprintf(ficgp," %%*lf (%%*lf)");
7280: }
1.288 brouard 7281: 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 7282: for (i=1; i<= nlstate ; i ++) {
7283: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7284: else fprintf(ficgp," %%*lf (%%*lf)");
7285: }
1.260 brouard 7286: 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 7287: for (i=1; i<= nlstate ; i ++) {
7288: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7289: else fprintf(ficgp," %%*lf (%%*lf)");
7290: }
1.265 brouard 7291: /* 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)); */
7292:
7293: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
7294: if(cptcoveff ==0){
1.271 brouard 7295: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
1.265 brouard 7296: }else{
7297: kl=0;
7298: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7299: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7300: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7301: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7302: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7303: vlv= nbcode[Tvaraff[k]][lv];
7304: kl++;
7305: /* 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 *\/ */
7306: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7307: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7308: /* '' 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*/
7309: if(k==cptcoveff){
7310: 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], \
7311: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
7312: }else{
7313: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7314: kl++;
7315: }
7316: } /* end covariate */
7317: } /* end if no covariate */
7318:
1.296 brouard 7319: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
1.238 brouard 7320: /* 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 7321: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 7322: if(cptcoveff ==0){
1.245 brouard 7323: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
1.238 brouard 7324: }else{
7325: kl=0;
7326: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7327: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7328: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7329: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7330: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7331: vlv= nbcode[Tvaraff[k]][lv];
1.223 brouard 7332: kl++;
1.238 brouard 7333: /* 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 *\/ */
7334: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7335: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7336: /* '' 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*/
7337: if(k==cptcoveff){
1.245 brouard 7338: 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 7339: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 7340: }else{
7341: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7342: kl++;
7343: }
7344: } /* end covariate */
7345: } /* end if no covariate */
1.296 brouard 7346: if(prevbcast == 1){
1.268 brouard 7347: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
7348: /* k1-1 error should be nres-1*/
7349: for (i=1; i<= nlstate ; i ++) {
7350: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7351: else fprintf(ficgp," %%*lf (%%*lf)");
7352: }
1.271 brouard 7353: 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 7354: for (i=1; i<= nlstate ; i ++) {
7355: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7356: else fprintf(ficgp," %%*lf (%%*lf)");
7357: }
1.276 brouard 7358: 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 7359: for (i=1; i<= nlstate ; i ++) {
7360: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7361: else fprintf(ficgp," %%*lf (%%*lf)");
7362: }
1.274 brouard 7363: fprintf(ficgp,"\" t\"\" w l lt 4");
1.268 brouard 7364: } /* end if backprojcast */
1.296 brouard 7365: } /* end if prevbcast */
1.276 brouard 7366: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
7367: fprintf(ficgp,"\nset out ;unset title;\n");
1.238 brouard 7368: } /* nres */
1.201 brouard 7369: } /* k1 */
7370: } /* cpt */
1.235 brouard 7371:
7372:
1.126 brouard 7373: /*2 eme*/
1.238 brouard 7374: for (k1=1; k1<= m ; k1 ++){
7375: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7376: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7377: continue;
7378: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.264 brouard 7379: strcpy(gplotlabel,"(");
1.238 brouard 7380: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.225 brouard 7381: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 7382: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7383: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7384: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7385: vlv= nbcode[Tvaraff[k]][lv];
7386: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7387: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7388: }
1.237 brouard 7389: /* for(k=1; k <= ncovds; k++){ */
1.236 brouard 7390: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 7391: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.236 brouard 7392: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7393: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7394: }
1.264 brouard 7395: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7396: fprintf(ficgp,"\n#\n");
1.223 brouard 7397: if(invalidvarcomb[k1]){
7398: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7399: continue;
7400: }
1.219 brouard 7401:
1.241 brouard 7402: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 7403: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.264 brouard 7404: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
7405: if(vpopbased==0){
1.238 brouard 7406: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
1.264 brouard 7407: }else
1.238 brouard 7408: fprintf(ficgp,"\nreplot ");
7409: for (i=1; i<= nlstate+1 ; i ++) {
7410: k=2*i;
1.261 brouard 7411: 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 7412: for (j=1; j<= nlstate+1 ; j ++) {
7413: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7414: else fprintf(ficgp," %%*lf (%%*lf)");
7415: }
7416: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
7417: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
1.261 brouard 7418: 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 7419: for (j=1; j<= nlstate+1 ; j ++) {
7420: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7421: else fprintf(ficgp," %%*lf (%%*lf)");
7422: }
7423: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.261 brouard 7424: 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 7425: for (j=1; j<= nlstate+1 ; j ++) {
7426: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7427: else fprintf(ficgp," %%*lf (%%*lf)");
7428: }
7429: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
7430: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
7431: } /* state */
7432: } /* vpopbased */
1.264 brouard 7433: 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 7434: } /* end nres */
7435: } /* k1 end 2 eme*/
7436:
7437:
7438: /*3eme*/
7439: for (k1=1; k1<= m ; k1 ++){
7440: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7441: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7442: continue;
7443:
7444: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.261 brouard 7445: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.264 brouard 7446: strcpy(gplotlabel,"(");
1.238 brouard 7447: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7448: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7449: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7450: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7451: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7452: vlv= nbcode[Tvaraff[k]][lv];
7453: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7454: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7455: }
7456: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7457: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7458: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7459: }
1.264 brouard 7460: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7461: fprintf(ficgp,"\n#\n");
7462: if(invalidvarcomb[k1]){
7463: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7464: continue;
7465: }
7466:
7467: /* k=2+nlstate*(2*cpt-2); */
7468: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 7469: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.264 brouard 7470: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
1.238 brouard 7471: fprintf(ficgp,"set ter svg size 640, 480\n\
1.261 brouard 7472: 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 7473: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7474: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7475: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
7476: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7477: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7478: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 7479:
1.238 brouard 7480: */
7481: for (i=1; i< nlstate ; i ++) {
1.261 brouard 7482: 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 7483: /* 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 7484:
1.238 brouard 7485: }
1.261 brouard 7486: 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 7487: }
1.264 brouard 7488: fprintf(ficgp,"\nunset label;\n");
1.238 brouard 7489: } /* end nres */
7490: } /* end kl 3eme */
1.126 brouard 7491:
1.223 brouard 7492: /* 4eme */
1.201 brouard 7493: /* Survival functions (period) from state i in state j by initial state i */
1.238 brouard 7494: for (k1=1; k1<=m; k1++){ /* For each covariate and each value */
7495: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7496: if(m != 1 && TKresult[nres]!= k1)
1.223 brouard 7497: continue;
1.238 brouard 7498: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
1.264 brouard 7499: strcpy(gplotlabel,"(");
1.238 brouard 7500: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
7501: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7502: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7503: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7504: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7505: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7506: vlv= nbcode[Tvaraff[k]][lv];
7507: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7508: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7509: }
7510: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7511: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7512: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7513: }
1.264 brouard 7514: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7515: fprintf(ficgp,"\n#\n");
7516: if(invalidvarcomb[k1]){
7517: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7518: continue;
1.223 brouard 7519: }
1.238 brouard 7520:
1.241 brouard 7521: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.264 brouard 7522: 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 7523: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7524: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7525: k=3;
7526: for (i=1; i<= nlstate ; i ++){
7527: if(i==1){
7528: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7529: }else{
7530: fprintf(ficgp,", '' ");
7531: }
7532: l=(nlstate+ndeath)*(i-1)+1;
7533: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
7534: for (j=2; j<= nlstate+ndeath ; j ++)
7535: fprintf(ficgp,"+$%d",k+l+j-1);
7536: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
7537: } /* nlstate */
1.264 brouard 7538: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7539: } /* end cpt state*/
7540: } /* end nres */
7541: } /* end covariate k1 */
7542:
1.220 brouard 7543: /* 5eme */
1.201 brouard 7544: /* Survival functions (period) from state i in state j by final state j */
1.238 brouard 7545: for (k1=1; k1<= m ; k1++){ /* For each covariate combination if any */
7546: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7547: if(m != 1 && TKresult[nres]!= k1)
1.227 brouard 7548: continue;
1.238 brouard 7549: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.264 brouard 7550: strcpy(gplotlabel,"(");
1.238 brouard 7551: 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);
7552: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7553: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7554: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7555: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7556: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7557: vlv= nbcode[Tvaraff[k]][lv];
7558: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7559: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7560: }
7561: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7562: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7563: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7564: }
1.264 brouard 7565: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7566: fprintf(ficgp,"\n#\n");
7567: if(invalidvarcomb[k1]){
7568: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7569: continue;
7570: }
1.227 brouard 7571:
1.241 brouard 7572: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.264 brouard 7573: 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 7574: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7575: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7576: k=3;
7577: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7578: if(j==1)
7579: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7580: else
7581: fprintf(ficgp,", '' ");
7582: l=(nlstate+ndeath)*(cpt-1) +j;
7583: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
7584: /* for (i=2; i<= nlstate+ndeath ; i ++) */
7585: /* fprintf(ficgp,"+$%d",k+l+i-1); */
7586: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
7587: } /* nlstate */
7588: fprintf(ficgp,", '' ");
7589: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
7590: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7591: l=(nlstate+ndeath)*(cpt-1) +j;
7592: if(j < nlstate)
7593: fprintf(ficgp,"$%d +",k+l);
7594: else
7595: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
7596: }
1.264 brouard 7597: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7598: } /* end cpt state*/
7599: } /* end covariate */
7600: } /* end nres */
1.227 brouard 7601:
1.220 brouard 7602: /* 6eme */
1.202 brouard 7603: /* CV preval stable (period) for each covariate */
1.237 brouard 7604: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7605: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7606: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7607: continue;
1.255 brouard 7608: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
1.264 brouard 7609: strcpy(gplotlabel,"(");
1.288 brouard 7610: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 7611: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 7612: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7613: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7614: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7615: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7616: vlv= nbcode[Tvaraff[k]][lv];
7617: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7618: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7619: }
1.237 brouard 7620: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7621: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7622: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7623: }
1.264 brouard 7624: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7625: fprintf(ficgp,"\n#\n");
1.223 brouard 7626: if(invalidvarcomb[k1]){
1.227 brouard 7627: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7628: continue;
1.223 brouard 7629: }
1.227 brouard 7630:
1.241 brouard 7631: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.264 brouard 7632: 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 7633: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 7634: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 7635: k=3; /* Offset */
1.255 brouard 7636: for (i=1; i<= nlstate ; i ++){ /* State of origin */
1.227 brouard 7637: if(i==1)
7638: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7639: else
7640: fprintf(ficgp,", '' ");
1.255 brouard 7641: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 7642: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
7643: for (j=2; j<= nlstate ; j ++)
7644: fprintf(ficgp,"+$%d",k+l+j-1);
7645: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 7646: } /* nlstate */
1.264 brouard 7647: fprintf(ficgp,"\nset out; unset label;\n");
1.153 brouard 7648: } /* end cpt state*/
7649: } /* end covariate */
1.227 brouard 7650:
7651:
1.220 brouard 7652: /* 7eme */
1.296 brouard 7653: if(prevbcast == 1){
1.288 brouard 7654: /* CV backward prevalence for each covariate */
1.237 brouard 7655: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7656: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7657: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7658: continue;
1.268 brouard 7659: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
1.264 brouard 7660: strcpy(gplotlabel,"(");
1.288 brouard 7661: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 7662: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7663: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7664: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7665: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 7666: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 7667: vlv= nbcode[Tvaraff[k]][lv];
7668: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7669: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 7670: }
1.237 brouard 7671: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7672: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7673: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7674: }
1.264 brouard 7675: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 7676: fprintf(ficgp,"\n#\n");
7677: if(invalidvarcomb[k1]){
7678: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7679: continue;
7680: }
7681:
1.241 brouard 7682: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.268 brouard 7683: 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 7684: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 7685: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 7686: k=3; /* Offset */
1.268 brouard 7687: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
1.227 brouard 7688: if(i==1)
7689: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
7690: else
7691: fprintf(ficgp,", '' ");
7692: /* l=(nlstate+ndeath)*(i-1)+1; */
1.255 brouard 7693: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 7694: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
7695: /* 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 7696: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
1.227 brouard 7697: /* for (j=2; j<= nlstate ; j ++) */
7698: /* fprintf(ficgp,"+$%d",k+l+j-1); */
7699: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
1.268 brouard 7700: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
1.227 brouard 7701: } /* nlstate */
1.264 brouard 7702: fprintf(ficgp,"\nset out; unset label;\n");
1.218 brouard 7703: } /* end cpt state*/
7704: } /* end covariate */
1.296 brouard 7705: } /* End if prevbcast */
1.218 brouard 7706:
1.223 brouard 7707: /* 8eme */
1.218 brouard 7708: if(prevfcast==1){
1.288 brouard 7709: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
1.218 brouard 7710:
1.237 brouard 7711: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7712: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7713: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7714: continue;
1.211 brouard 7715: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.264 brouard 7716: strcpy(gplotlabel,"(");
1.288 brouard 7717: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 7718: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
7719: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7720: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7721: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7722: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7723: vlv= nbcode[Tvaraff[k]][lv];
7724: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7725: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 7726: }
1.237 brouard 7727: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7728: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7729: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7730: }
1.264 brouard 7731: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 7732: fprintf(ficgp,"\n#\n");
7733: if(invalidvarcomb[k1]){
7734: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7735: continue;
7736: }
7737:
7738: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 7739: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.264 brouard 7740: 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 7741: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 7742: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.266 brouard 7743:
7744: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
7745: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
7746: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
7747: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
1.227 brouard 7748: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7749: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7750: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7751: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.266 brouard 7752: if(i==istart){
1.227 brouard 7753: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
7754: }else{
7755: fprintf(ficgp,",\\\n '' ");
7756: }
7757: if(cptcoveff ==0){ /* No covariate */
7758: ioffset=2; /* Age is in 2 */
7759: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7760: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7761: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7762: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7763: fprintf(ficgp," u %d:(", ioffset);
1.266 brouard 7764: if(i==nlstate+1){
1.270 brouard 7765: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
1.266 brouard 7766: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
7767: fprintf(ficgp,",\\\n '' ");
7768: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 7769: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
1.266 brouard 7770: offyear, \
1.268 brouard 7771: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
1.266 brouard 7772: }else
1.227 brouard 7773: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
7774: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
7775: }else{ /* more than 2 covariates */
1.270 brouard 7776: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
7777: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7778: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7779: iyearc=ioffset-1;
7780: iagec=ioffset;
1.227 brouard 7781: fprintf(ficgp," u %d:(",ioffset);
7782: kl=0;
7783: strcpy(gplotcondition,"(");
7784: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
7785: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
7786: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7787: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7788: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7789: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
7790: kl++;
7791: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
7792: kl++;
7793: if(k <cptcoveff && cptcoveff>1)
7794: sprintf(gplotcondition+strlen(gplotcondition)," && ");
7795: }
7796: strcpy(gplotcondition+strlen(gplotcondition),")");
7797: /* 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 *\/ */
7798: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7799: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7800: /* '' 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*/
7801: if(i==nlstate+1){
1.270 brouard 7802: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
7803: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
1.266 brouard 7804: fprintf(ficgp,",\\\n '' ");
1.270 brouard 7805: fprintf(ficgp," u %d:(",iagec);
7806: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
7807: iyearc, iagec, offyear, \
7808: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
1.266 brouard 7809: /* '' 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 7810: }else{
7811: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
7812: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
7813: }
7814: } /* end if covariate */
7815: } /* nlstate */
1.264 brouard 7816: fprintf(ficgp,"\nset out; unset label;\n");
1.223 brouard 7817: } /* end cpt state*/
7818: } /* end covariate */
7819: } /* End if prevfcast */
1.227 brouard 7820:
1.296 brouard 7821: if(prevbcast==1){
1.268 brouard 7822: /* Back projection from cross-sectional to stable (mixed) for each covariate */
7823:
7824: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7825: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7826: if(m != 1 && TKresult[nres]!= k1)
7827: continue;
7828: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
7829: strcpy(gplotlabel,"(");
7830: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
7831: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
7832: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7833: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7834: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7835: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7836: vlv= nbcode[Tvaraff[k]][lv];
7837: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
7838: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
7839: }
7840: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7841: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7842: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7843: }
7844: strcpy(gplotlabel+strlen(gplotlabel),")");
7845: fprintf(ficgp,"\n#\n");
7846: if(invalidvarcomb[k1]){
7847: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7848: continue;
7849: }
7850:
7851: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
7852: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
7853: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
7854: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
7855: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7856:
7857: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
7858: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
7859: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
7860: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
7861: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7862: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7863: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7864: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7865: if(i==istart){
7866: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
7867: }else{
7868: fprintf(ficgp,",\\\n '' ");
7869: }
7870: if(cptcoveff ==0){ /* No covariate */
7871: ioffset=2; /* Age is in 2 */
7872: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7873: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7874: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7875: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7876: fprintf(ficgp," u %d:(", ioffset);
7877: if(i==nlstate+1){
1.270 brouard 7878: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
1.268 brouard 7879: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
7880: fprintf(ficgp,",\\\n '' ");
7881: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 7882: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
1.268 brouard 7883: offbyear, \
7884: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
7885: }else
7886: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
7887: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
7888: }else{ /* more than 2 covariates */
1.270 brouard 7889: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
7890: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7891: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7892: iyearc=ioffset-1;
7893: iagec=ioffset;
1.268 brouard 7894: fprintf(ficgp," u %d:(",ioffset);
7895: kl=0;
7896: strcpy(gplotcondition,"(");
7897: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
7898: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
7899: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7900: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7901: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7902: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
7903: kl++;
7904: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
7905: kl++;
7906: if(k <cptcoveff && cptcoveff>1)
7907: sprintf(gplotcondition+strlen(gplotcondition)," && ");
7908: }
7909: strcpy(gplotcondition+strlen(gplotcondition),")");
7910: /* 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 *\/ */
7911: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7912: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7913: /* '' 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*/
7914: if(i==nlstate+1){
1.270 brouard 7915: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
7916: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
1.268 brouard 7917: fprintf(ficgp,",\\\n '' ");
1.270 brouard 7918: fprintf(ficgp," u %d:(",iagec);
1.268 brouard 7919: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
1.270 brouard 7920: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
7921: iyearc,iagec,offbyear, \
7922: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
1.268 brouard 7923: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
7924: }else{
7925: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
7926: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
7927: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
7928: }
7929: } /* end if covariate */
7930: } /* nlstate */
7931: fprintf(ficgp,"\nset out; unset label;\n");
7932: } /* end cpt state*/
7933: } /* end covariate */
1.296 brouard 7934: } /* End if prevbcast */
1.268 brouard 7935:
1.227 brouard 7936:
1.238 brouard 7937: /* 9eme writing MLE parameters */
7938: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 7939: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 7940: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 7941: for(k=1; k <=(nlstate+ndeath); k++){
7942: if (k != i) {
1.227 brouard 7943: fprintf(ficgp,"# current state %d\n",k);
7944: for(j=1; j <=ncovmodel; j++){
7945: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
7946: jk++;
7947: }
7948: fprintf(ficgp,"\n");
1.126 brouard 7949: }
7950: }
1.223 brouard 7951: }
1.187 brouard 7952: fprintf(ficgp,"##############\n#\n");
1.227 brouard 7953:
1.145 brouard 7954: /*goto avoid;*/
1.238 brouard 7955: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
7956: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 7957: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
7958: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
7959: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
7960: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
7961: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
7962: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
7963: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
7964: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
7965: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
7966: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
7967: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
7968: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
7969: fprintf(ficgp,"#\n");
1.223 brouard 7970: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 7971: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.237 brouard 7972: fprintf(ficgp,"#model=%s \n",model);
1.238 brouard 7973: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.264 brouard 7974: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
7975: for(k1=1; k1 <=m; k1++) /* For each combination of covariate */
1.237 brouard 7976: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.264 brouard 7977: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7978: continue;
1.264 brouard 7979: fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1);
7980: strcpy(gplotlabel,"(");
1.276 brouard 7981: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
1.264 brouard 7982: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
7983: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7984: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7985: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7986: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7987: vlv= nbcode[Tvaraff[k]][lv];
7988: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
7989: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
7990: }
1.237 brouard 7991: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7992: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7993: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7994: }
1.264 brouard 7995: strcpy(gplotlabel+strlen(gplotlabel),")");
1.237 brouard 7996: fprintf(ficgp,"\n#\n");
1.264 brouard 7997: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
1.276 brouard 7998: fprintf(ficgp,"\nset key outside ");
7999: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
8000: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
1.223 brouard 8001: fprintf(ficgp,"\nset ter svg size 640, 480 ");
8002: if (ng==1){
8003: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
8004: fprintf(ficgp,"\nunset log y");
8005: }else if (ng==2){
8006: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
8007: fprintf(ficgp,"\nset log y");
8008: }else if (ng==3){
8009: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
8010: fprintf(ficgp,"\nset log y");
8011: }else
8012: fprintf(ficgp,"\nunset title ");
8013: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
8014: i=1;
8015: for(k2=1; k2<=nlstate; k2++) {
8016: k3=i;
8017: for(k=1; k<=(nlstate+ndeath); k++) {
8018: if (k != k2){
8019: switch( ng) {
8020: case 1:
8021: if(nagesqr==0)
8022: fprintf(ficgp," p%d+p%d*x",i,i+1);
8023: else /* nagesqr =1 */
8024: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8025: break;
8026: case 2: /* ng=2 */
8027: if(nagesqr==0)
8028: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
8029: else /* nagesqr =1 */
8030: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8031: break;
8032: case 3:
8033: if(nagesqr==0)
8034: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
8035: else /* nagesqr =1 */
8036: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
8037: break;
8038: }
8039: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 8040: ijp=1; /* product no age */
8041: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
8042: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 8043: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.268 brouard 8044: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8045: if(j==Tage[ij]) { /* Product by age To be looked at!!*/
8046: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8047: if(DummyV[j]==0){
8048: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
8049: }else{ /* quantitative */
8050: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
8051: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8052: }
8053: ij++;
1.237 brouard 8054: }
1.268 brouard 8055: }
8056: }else if(cptcovprod >0){
8057: if(j==Tprod[ijp]) { /* */
8058: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
8059: if(ijp <=cptcovprod) { /* Product */
8060: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
8061: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
8062: /* 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)]); */
8063: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
8064: }else{ /* Vn is dummy and Vm is quanti */
8065: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
8066: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8067: }
8068: }else{ /* Vn*Vm Vn is quanti */
8069: if(DummyV[Tvard[ijp][2]]==0){
8070: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
8071: }else{ /* Both quanti */
8072: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8073: }
1.237 brouard 8074: }
1.268 brouard 8075: ijp++;
1.237 brouard 8076: }
1.268 brouard 8077: } /* end Tprod */
1.237 brouard 8078: } else{ /* simple covariate */
1.264 brouard 8079: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
1.237 brouard 8080: if(Dummy[j]==0){
8081: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
8082: }else{ /* quantitative */
8083: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.264 brouard 8084: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
1.223 brouard 8085: }
1.237 brouard 8086: } /* end simple */
8087: } /* end j */
1.223 brouard 8088: }else{
8089: i=i-ncovmodel;
8090: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
8091: fprintf(ficgp," (1.");
8092: }
1.227 brouard 8093:
1.223 brouard 8094: if(ng != 1){
8095: fprintf(ficgp,")/(1");
1.227 brouard 8096:
1.264 brouard 8097: for(cpt=1; cpt <=nlstate; cpt++){
1.223 brouard 8098: if(nagesqr==0)
1.264 brouard 8099: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
1.223 brouard 8100: else /* nagesqr =1 */
1.264 brouard 8101: 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 8102:
1.223 brouard 8103: ij=1;
8104: for(j=3; j <=ncovmodel-nagesqr; j++){
1.268 brouard 8105: if(cptcovage >0){
8106: if((j-2)==Tage[ij]) { /* Bug valgrind */
8107: if(ij <=cptcovage) { /* Bug valgrind */
8108: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);
8109: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8110: ij++;
8111: }
8112: }
8113: }else
8114: 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 8115: }
8116: fprintf(ficgp,")");
8117: }
8118: fprintf(ficgp,")");
8119: if(ng ==2)
1.276 brouard 8120: 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 8121: else /* ng= 3 */
1.276 brouard 8122: 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 8123: }else{ /* end ng <> 1 */
8124: if( k !=k2) /* logit p11 is hard to draw */
1.276 brouard 8125: 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 8126: }
8127: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
8128: fprintf(ficgp,",");
8129: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
8130: fprintf(ficgp,",");
8131: i=i+ncovmodel;
8132: } /* end k */
8133: } /* end k2 */
1.276 brouard 8134: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
8135: fprintf(ficgp,"\n set out; unset title;set key default;\n");
1.264 brouard 8136: } /* end k1 */
1.223 brouard 8137: } /* end ng */
8138: /* avoid: */
8139: fflush(ficgp);
1.126 brouard 8140: } /* end gnuplot */
8141:
8142:
8143: /*************** Moving average **************/
1.219 brouard 8144: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 8145: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 8146:
1.222 brouard 8147: int i, cpt, cptcod;
8148: int modcovmax =1;
8149: int mobilavrange, mob;
8150: int iage=0;
1.288 brouard 8151: int firstA1=0, firstA2=0;
1.222 brouard 8152:
1.266 brouard 8153: double sum=0., sumr=0.;
1.222 brouard 8154: double age;
1.266 brouard 8155: double *sumnewp, *sumnewm, *sumnewmr;
8156: double *agemingood, *agemaxgood;
8157: double *agemingoodr, *agemaxgoodr;
1.222 brouard 8158:
8159:
1.278 brouard 8160: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
8161: /* a covariate has 2 modalities, should be equal to ncovcombmax */
1.222 brouard 8162:
8163: sumnewp = vector(1,ncovcombmax);
8164: sumnewm = vector(1,ncovcombmax);
1.266 brouard 8165: sumnewmr = vector(1,ncovcombmax);
1.222 brouard 8166: agemingood = vector(1,ncovcombmax);
1.266 brouard 8167: agemingoodr = vector(1,ncovcombmax);
1.222 brouard 8168: agemaxgood = vector(1,ncovcombmax);
1.266 brouard 8169: agemaxgoodr = vector(1,ncovcombmax);
1.222 brouard 8170:
8171: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
1.266 brouard 8172: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
1.222 brouard 8173: sumnewp[cptcod]=0.;
1.266 brouard 8174: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
8175: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
1.222 brouard 8176: }
8177: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
8178:
1.266 brouard 8179: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
8180: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
1.222 brouard 8181: else mobilavrange=mobilav;
8182: for (age=bage; age<=fage; age++)
8183: for (i=1; i<=nlstate;i++)
8184: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
8185: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8186: /* We keep the original values on the extreme ages bage, fage and for
8187: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
8188: we use a 5 terms etc. until the borders are no more concerned.
8189: */
8190: for (mob=3;mob <=mobilavrange;mob=mob+2){
8191: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
1.266 brouard 8192: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
8193: sumnewm[cptcod]=0.;
8194: for (i=1; i<=nlstate;i++){
1.222 brouard 8195: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
8196: for (cpt=1;cpt<=(mob-1)/2;cpt++){
8197: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
8198: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
8199: }
8200: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
1.266 brouard 8201: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8202: } /* end i */
8203: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
8204: } /* end cptcod */
1.222 brouard 8205: }/* end age */
8206: }/* end mob */
1.266 brouard 8207: }else{
8208: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
1.222 brouard 8209: return -1;
1.266 brouard 8210: }
8211:
8212: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
1.222 brouard 8213: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
8214: if(invalidvarcomb[cptcod]){
8215: printf("\nCombination (%d) ignored because no cases \n",cptcod);
8216: continue;
8217: }
1.219 brouard 8218:
1.266 brouard 8219: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
8220: sumnewm[cptcod]=0.;
8221: sumnewmr[cptcod]=0.;
8222: for (i=1; i<=nlstate;i++){
8223: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8224: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8225: }
8226: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8227: agemingoodr[cptcod]=age;
8228: }
8229: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8230: agemingood[cptcod]=age;
8231: }
8232: } /* age */
8233: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
1.222 brouard 8234: sumnewm[cptcod]=0.;
1.266 brouard 8235: sumnewmr[cptcod]=0.;
1.222 brouard 8236: for (i=1; i<=nlstate;i++){
8237: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8238: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8239: }
8240: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8241: agemaxgoodr[cptcod]=age;
1.222 brouard 8242: }
8243: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
1.266 brouard 8244: agemaxgood[cptcod]=age;
8245: }
8246: } /* age */
8247: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
8248: /* but they will change */
1.288 brouard 8249: firstA1=0;firstA2=0;
1.266 brouard 8250: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
8251: sumnewm[cptcod]=0.;
8252: sumnewmr[cptcod]=0.;
8253: for (i=1; i<=nlstate;i++){
8254: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8255: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8256: }
8257: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8258: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8259: agemaxgoodr[cptcod]=age; /* age min */
8260: for (i=1; i<=nlstate;i++)
8261: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8262: }else{ /* bad we change the value with the values of good ages */
8263: for (i=1; i<=nlstate;i++){
8264: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
8265: } /* i */
8266: } /* end bad */
8267: }else{
8268: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8269: agemaxgood[cptcod]=age;
8270: }else{ /* bad we change the value with the values of good ages */
8271: for (i=1; i<=nlstate;i++){
8272: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
8273: } /* i */
8274: } /* end bad */
8275: }/* end else */
8276: sum=0.;sumr=0.;
8277: for (i=1; i<=nlstate;i++){
8278: sum+=mobaverage[(int)age][i][cptcod];
8279: sumr+=probs[(int)age][i][cptcod];
8280: }
8281: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.288 brouard 8282: if(!firstA1){
8283: firstA1=1;
8284: 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);
8285: }
8286: 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 8287: } /* end bad */
8288: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8289: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.288 brouard 8290: if(!firstA2){
8291: firstA2=1;
8292: 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);
8293: }
8294: 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 8295: } /* end bad */
8296: }/* age */
1.266 brouard 8297:
8298: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
1.222 brouard 8299: sumnewm[cptcod]=0.;
1.266 brouard 8300: sumnewmr[cptcod]=0.;
1.222 brouard 8301: for (i=1; i<=nlstate;i++){
8302: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8303: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8304: }
8305: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8306: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
8307: agemingoodr[cptcod]=age;
8308: for (i=1; i<=nlstate;i++)
8309: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8310: }else{ /* bad we change the value with the values of good ages */
8311: for (i=1; i<=nlstate;i++){
8312: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
8313: } /* i */
8314: } /* end bad */
8315: }else{
8316: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8317: agemingood[cptcod]=age;
8318: }else{ /* bad */
8319: for (i=1; i<=nlstate;i++){
8320: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
8321: } /* i */
8322: } /* end bad */
8323: }/* end else */
8324: sum=0.;sumr=0.;
8325: for (i=1; i<=nlstate;i++){
8326: sum+=mobaverage[(int)age][i][cptcod];
8327: sumr+=mobaverage[(int)age][i][cptcod];
1.222 brouard 8328: }
1.266 brouard 8329: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.268 brouard 8330: 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 8331: } /* end bad */
8332: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8333: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.268 brouard 8334: 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 8335: } /* end bad */
8336: }/* age */
1.266 brouard 8337:
1.222 brouard 8338:
8339: for (age=bage; age<=fage; age++){
1.235 brouard 8340: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 8341: sumnewp[cptcod]=0.;
8342: sumnewm[cptcod]=0.;
8343: for (i=1; i<=nlstate;i++){
8344: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
8345: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8346: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
8347: }
8348: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
8349: }
8350: /* printf("\n"); */
8351: /* } */
1.266 brouard 8352:
1.222 brouard 8353: /* brutal averaging */
1.266 brouard 8354: /* for (i=1; i<=nlstate;i++){ */
8355: /* for (age=1; age<=bage; age++){ */
8356: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
8357: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8358: /* } */
8359: /* for (age=fage; age<=AGESUP; age++){ */
8360: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
8361: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8362: /* } */
8363: /* } /\* end i status *\/ */
8364: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
8365: /* for (age=1; age<=AGESUP; age++){ */
8366: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
8367: /* mobaverage[(int)age][i][cptcod]=0.; */
8368: /* } */
8369: /* } */
1.222 brouard 8370: }/* end cptcod */
1.266 brouard 8371: free_vector(agemaxgoodr,1, ncovcombmax);
8372: free_vector(agemaxgood,1, ncovcombmax);
8373: free_vector(agemingood,1, ncovcombmax);
8374: free_vector(agemingoodr,1, ncovcombmax);
8375: free_vector(sumnewmr,1, ncovcombmax);
1.222 brouard 8376: free_vector(sumnewm,1, ncovcombmax);
8377: free_vector(sumnewp,1, ncovcombmax);
8378: return 0;
8379: }/* End movingaverage */
1.218 brouard 8380:
1.126 brouard 8381:
1.296 brouard 8382:
1.126 brouard 8383: /************** Forecasting ******************/
1.296 brouard 8384: /* 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)*/
8385: 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){
8386: /* dateintemean, mean date of interviews
8387: dateprojd, year, month, day of starting projection
8388: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
1.126 brouard 8389: agemin, agemax range of age
8390: dateprev1 dateprev2 range of dates during which prevalence is computed
8391: */
1.296 brouard 8392: /* double anprojd, mprojd, jprojd; */
8393: /* double anprojf, mprojf, jprojf; */
1.267 brouard 8394: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
1.126 brouard 8395: double agec; /* generic age */
1.296 brouard 8396: double agelim, ppij, yp,yp1,yp2;
1.126 brouard 8397: double *popeffectif,*popcount;
8398: double ***p3mat;
1.218 brouard 8399: /* double ***mobaverage; */
1.126 brouard 8400: char fileresf[FILENAMELENGTH];
8401:
8402: agelim=AGESUP;
1.211 brouard 8403: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8404: in each health status at the date of interview (if between dateprev1 and dateprev2).
8405: We still use firstpass and lastpass as another selection.
8406: */
1.214 brouard 8407: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8408: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 8409:
1.201 brouard 8410: strcpy(fileresf,"F_");
8411: strcat(fileresf,fileresu);
1.126 brouard 8412: if((ficresf=fopen(fileresf,"w"))==NULL) {
8413: printf("Problem with forecast resultfile: %s\n", fileresf);
8414: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
8415: }
1.235 brouard 8416: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
8417: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 8418:
1.225 brouard 8419: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 8420:
8421:
8422: stepsize=(int) (stepm+YEARM-1)/YEARM;
8423: if (stepm<=12) stepsize=1;
8424: if(estepm < stepm){
8425: printf ("Problem %d lower than %d\n",estepm, stepm);
8426: }
1.270 brouard 8427: else{
8428: hstepm=estepm;
8429: }
8430: if(estepm > stepm){ /* Yes every two year */
8431: stepsize=2;
8432: }
1.296 brouard 8433: hstepm=hstepm/stepm;
1.126 brouard 8434:
1.296 brouard 8435:
8436: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8437: /* fractional in yp1 *\/ */
8438: /* aintmean=yp; */
8439: /* yp2=modf((yp1*12),&yp); */
8440: /* mintmean=yp; */
8441: /* yp1=modf((yp2*30.5),&yp); */
8442: /* jintmean=yp; */
8443: /* if(jintmean==0) jintmean=1; */
8444: /* if(mintmean==0) mintmean=1; */
1.126 brouard 8445:
1.296 brouard 8446:
8447: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
8448: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
8449: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
1.227 brouard 8450: i1=pow(2,cptcoveff);
1.126 brouard 8451: if (cptcovn < 1){i1=1;}
8452:
1.296 brouard 8453: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.126 brouard 8454:
8455: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 8456:
1.126 brouard 8457: /* if (h==(int)(YEARM*yearp)){ */
1.235 brouard 8458: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8459: for(k=1; k<=i1;k++){
1.253 brouard 8460: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 8461: continue;
1.227 brouard 8462: if(invalidvarcomb[k]){
8463: printf("\nCombination (%d) projection ignored because no cases \n",k);
8464: continue;
8465: }
8466: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
8467: for(j=1;j<=cptcoveff;j++) {
8468: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8469: }
1.235 brouard 8470: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 8471: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.235 brouard 8472: }
1.227 brouard 8473: fprintf(ficresf," yearproj age");
8474: for(j=1; j<=nlstate+ndeath;j++){
8475: for(i=1; i<=nlstate;i++)
8476: fprintf(ficresf," p%d%d",i,j);
8477: fprintf(ficresf," wp.%d",j);
8478: }
1.296 brouard 8479: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
1.227 brouard 8480: fprintf(ficresf,"\n");
1.296 brouard 8481: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
1.270 brouard 8482: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
8483: for (agec=fage; agec>=(bage); agec--){
1.227 brouard 8484: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
8485: nhstepm = nhstepm/hstepm;
8486: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8487: oldm=oldms;savm=savms;
1.268 brouard 8488: /* We compute pii at age agec over nhstepm);*/
1.235 brouard 8489: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.268 brouard 8490: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
1.227 brouard 8491: for (h=0; h<=nhstepm; h++){
8492: if (h*hstepm/YEARM*stepm ==yearp) {
1.268 brouard 8493: break;
8494: }
8495: }
8496: fprintf(ficresf,"\n");
8497: for(j=1;j<=cptcoveff;j++)
8498: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 8499: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
1.268 brouard 8500:
8501: for(j=1; j<=nlstate+ndeath;j++) {
8502: ppij=0.;
8503: for(i=1; i<=nlstate;i++) {
1.278 brouard 8504: if (mobilav>=1)
8505: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
8506: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
8507: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
8508: }
1.268 brouard 8509: fprintf(ficresf," %.3f", p3mat[i][j][h]);
8510: } /* end i */
8511: fprintf(ficresf," %.3f", ppij);
8512: }/* end j */
1.227 brouard 8513: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8514: } /* end agec */
1.266 brouard 8515: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
8516: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
1.227 brouard 8517: } /* end yearp */
8518: } /* end k */
1.219 brouard 8519:
1.126 brouard 8520: fclose(ficresf);
1.215 brouard 8521: printf("End of Computing forecasting \n");
8522: fprintf(ficlog,"End of Computing forecasting\n");
8523:
1.126 brouard 8524: }
8525:
1.269 brouard 8526: /************** Back Forecasting ******************/
1.296 brouard 8527: /* 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){ */
8528: 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){
8529: /* back1, year, month, day of starting backprojection
1.267 brouard 8530: agemin, agemax range of age
8531: dateprev1 dateprev2 range of dates during which prevalence is computed
1.269 brouard 8532: anback2 year of end of backprojection (same day and month as back1).
8533: prevacurrent and prev are prevalences.
1.267 brouard 8534: */
8535: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
8536: double agec; /* generic age */
1.302 brouard 8537: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
1.267 brouard 8538: double *popeffectif,*popcount;
8539: double ***p3mat;
8540: /* double ***mobaverage; */
8541: char fileresfb[FILENAMELENGTH];
8542:
1.268 brouard 8543: agelim=AGEINF;
1.267 brouard 8544: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8545: in each health status at the date of interview (if between dateprev1 and dateprev2).
8546: We still use firstpass and lastpass as another selection.
8547: */
8548: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8549: /* firstpass, lastpass, stepm, weightopt, model); */
8550:
8551: /*Do we need to compute prevalence again?*/
8552:
8553: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
8554:
8555: strcpy(fileresfb,"FB_");
8556: strcat(fileresfb,fileresu);
8557: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
8558: printf("Problem with back forecast resultfile: %s\n", fileresfb);
8559: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
8560: }
8561: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8562: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8563:
8564: if (cptcoveff==0) ncodemax[cptcoveff]=1;
8565:
8566:
8567: stepsize=(int) (stepm+YEARM-1)/YEARM;
8568: if (stepm<=12) stepsize=1;
8569: if(estepm < stepm){
8570: printf ("Problem %d lower than %d\n",estepm, stepm);
8571: }
1.270 brouard 8572: else{
8573: hstepm=estepm;
8574: }
8575: if(estepm >= stepm){ /* Yes every two year */
8576: stepsize=2;
8577: }
1.267 brouard 8578:
8579: hstepm=hstepm/stepm;
1.296 brouard 8580: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8581: /* fractional in yp1 *\/ */
8582: /* aintmean=yp; */
8583: /* yp2=modf((yp1*12),&yp); */
8584: /* mintmean=yp; */
8585: /* yp1=modf((yp2*30.5),&yp); */
8586: /* jintmean=yp; */
8587: /* if(jintmean==0) jintmean=1; */
8588: /* if(mintmean==0) jintmean=1; */
1.267 brouard 8589:
8590: i1=pow(2,cptcoveff);
8591: if (cptcovn < 1){i1=1;}
8592:
1.296 brouard 8593: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
8594: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.267 brouard 8595:
8596: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
8597:
8598: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8599: for(k=1; k<=i1;k++){
8600: if(i1 != 1 && TKresult[nres]!= k)
8601: continue;
8602: if(invalidvarcomb[k]){
8603: printf("\nCombination (%d) projection ignored because no cases \n",k);
8604: continue;
8605: }
1.268 brouard 8606: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
1.267 brouard 8607: for(j=1;j<=cptcoveff;j++) {
8608: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8609: }
8610: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8611: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
8612: }
8613: fprintf(ficresfb," yearbproj age");
8614: for(j=1; j<=nlstate+ndeath;j++){
8615: for(i=1; i<=nlstate;i++)
1.268 brouard 8616: fprintf(ficresfb," b%d%d",i,j);
8617: fprintf(ficresfb," b.%d",j);
1.267 brouard 8618: }
1.296 brouard 8619: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
1.267 brouard 8620: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
8621: fprintf(ficresfb,"\n");
1.296 brouard 8622: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
1.273 brouard 8623: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
1.270 brouard 8624: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
8625: for (agec=bage; agec<=fage; agec++){ /* testing */
1.268 brouard 8626: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
1.271 brouard 8627: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
1.267 brouard 8628: nhstepm = nhstepm/hstepm;
8629: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8630: oldm=oldms;savm=savms;
1.268 brouard 8631: /* computes hbxij at age agec over 1 to nhstepm */
1.271 brouard 8632: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
1.267 brouard 8633: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.268 brouard 8634: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
8635: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
8636: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
1.267 brouard 8637: for (h=0; h<=nhstepm; h++){
1.268 brouard 8638: if (h*hstepm/YEARM*stepm ==-yearp) {
8639: break;
8640: }
8641: }
8642: fprintf(ficresfb,"\n");
8643: for(j=1;j<=cptcoveff;j++)
8644: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 8645: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
1.268 brouard 8646: for(i=1; i<=nlstate+ndeath;i++) {
8647: ppij=0.;ppi=0.;
8648: for(j=1; j<=nlstate;j++) {
8649: /* if (mobilav==1) */
1.269 brouard 8650: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
8651: ppi=ppi+prevacurrent[(int)agec][j][k];
8652: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
8653: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
1.267 brouard 8654: /* else { */
8655: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
8656: /* } */
1.268 brouard 8657: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
8658: } /* end j */
8659: if(ppi <0.99){
8660: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
8661: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
8662: }
8663: fprintf(ficresfb," %.3f", ppij);
8664: }/* end j */
1.267 brouard 8665: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8666: } /* end agec */
8667: } /* end yearp */
8668: } /* end k */
1.217 brouard 8669:
1.267 brouard 8670: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
1.217 brouard 8671:
1.267 brouard 8672: fclose(ficresfb);
8673: printf("End of Computing Back forecasting \n");
8674: fprintf(ficlog,"End of Computing Back forecasting\n");
1.218 brouard 8675:
1.267 brouard 8676: }
1.217 brouard 8677:
1.269 brouard 8678: /* Variance of prevalence limit: varprlim */
8679: 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 8680: /*------- Variance of forward period (stable) prevalence------*/
1.269 brouard 8681:
8682: char fileresvpl[FILENAMELENGTH];
8683: FILE *ficresvpl;
8684: double **oldm, **savm;
8685: double **varpl; /* Variances of prevalence limits by age */
8686: int i1, k, nres, j ;
8687:
8688: strcpy(fileresvpl,"VPL_");
8689: strcat(fileresvpl,fileresu);
8690: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.288 brouard 8691: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
1.269 brouard 8692: exit(0);
8693: }
1.288 brouard 8694: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
8695: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.269 brouard 8696:
8697: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
8698: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
8699:
8700: i1=pow(2,cptcoveff);
8701: if (cptcovn < 1){i1=1;}
8702:
8703: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8704: for(k=1; k<=i1;k++){
8705: if(i1 != 1 && TKresult[nres]!= k)
8706: continue;
8707: fprintf(ficresvpl,"\n#****** ");
8708: printf("\n#****** ");
8709: fprintf(ficlog,"\n#****** ");
8710: for(j=1;j<=cptcoveff;j++) {
8711: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8712: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8713: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8714: }
8715: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
8716: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8717: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8718: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8719: }
8720: fprintf(ficresvpl,"******\n");
8721: printf("******\n");
8722: fprintf(ficlog,"******\n");
8723:
8724: varpl=matrix(1,nlstate,(int) bage, (int) fage);
8725: oldm=oldms;savm=savms;
8726: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
8727: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
8728: /*}*/
8729: }
8730:
8731: fclose(ficresvpl);
1.288 brouard 8732: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
8733: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
1.269 brouard 8734:
8735: }
8736: /* Variance of back prevalence: varbprlim */
8737: 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){
8738: /*------- Variance of back (stable) prevalence------*/
8739:
8740: char fileresvbl[FILENAMELENGTH];
8741: FILE *ficresvbl;
8742:
8743: double **oldm, **savm;
8744: double **varbpl; /* Variances of back prevalence limits by age */
8745: int i1, k, nres, j ;
8746:
8747: strcpy(fileresvbl,"VBL_");
8748: strcat(fileresvbl,fileresu);
8749: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
8750: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
8751: exit(0);
8752: }
8753: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
8754: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
8755:
8756:
8757: i1=pow(2,cptcoveff);
8758: if (cptcovn < 1){i1=1;}
8759:
8760: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8761: for(k=1; k<=i1;k++){
8762: if(i1 != 1 && TKresult[nres]!= k)
8763: continue;
8764: fprintf(ficresvbl,"\n#****** ");
8765: printf("\n#****** ");
8766: fprintf(ficlog,"\n#****** ");
8767: for(j=1;j<=cptcoveff;j++) {
8768: fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8769: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8770: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8771: }
8772: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
8773: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8774: fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8775: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8776: }
8777: fprintf(ficresvbl,"******\n");
8778: printf("******\n");
8779: fprintf(ficlog,"******\n");
8780:
8781: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
8782: oldm=oldms;savm=savms;
8783:
8784: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
8785: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
8786: /*}*/
8787: }
8788:
8789: fclose(ficresvbl);
8790: printf("done variance-covariance of back prevalence\n");fflush(stdout);
8791: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
8792:
8793: } /* End of varbprlim */
8794:
1.126 brouard 8795: /************** Forecasting *****not tested NB*************/
1.227 brouard 8796: /* 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 8797:
1.227 brouard 8798: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
8799: /* int *popage; */
8800: /* double calagedatem, agelim, kk1, kk2; */
8801: /* double *popeffectif,*popcount; */
8802: /* double ***p3mat,***tabpop,***tabpopprev; */
8803: /* /\* double ***mobaverage; *\/ */
8804: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 8805:
1.227 brouard 8806: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8807: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8808: /* agelim=AGESUP; */
8809: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 8810:
1.227 brouard 8811: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 8812:
8813:
1.227 brouard 8814: /* strcpy(filerespop,"POP_"); */
8815: /* strcat(filerespop,fileresu); */
8816: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
8817: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
8818: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
8819: /* } */
8820: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
8821: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 8822:
1.227 brouard 8823: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 8824:
1.227 brouard 8825: /* /\* if (mobilav!=0) { *\/ */
8826: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
8827: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
8828: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
8829: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
8830: /* /\* } *\/ */
8831: /* /\* } *\/ */
1.126 brouard 8832:
1.227 brouard 8833: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
8834: /* if (stepm<=12) stepsize=1; */
1.126 brouard 8835:
1.227 brouard 8836: /* agelim=AGESUP; */
1.126 brouard 8837:
1.227 brouard 8838: /* hstepm=1; */
8839: /* hstepm=hstepm/stepm; */
1.218 brouard 8840:
1.227 brouard 8841: /* if (popforecast==1) { */
8842: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
8843: /* printf("Problem with population file : %s\n",popfile);exit(0); */
8844: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
8845: /* } */
8846: /* popage=ivector(0,AGESUP); */
8847: /* popeffectif=vector(0,AGESUP); */
8848: /* popcount=vector(0,AGESUP); */
1.126 brouard 8849:
1.227 brouard 8850: /* i=1; */
8851: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 8852:
1.227 brouard 8853: /* imx=i; */
8854: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
8855: /* } */
1.218 brouard 8856:
1.227 brouard 8857: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
8858: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
8859: /* k=k+1; */
8860: /* fprintf(ficrespop,"\n#******"); */
8861: /* for(j=1;j<=cptcoveff;j++) { */
8862: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
8863: /* } */
8864: /* fprintf(ficrespop,"******\n"); */
8865: /* fprintf(ficrespop,"# Age"); */
8866: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
8867: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 8868:
1.227 brouard 8869: /* for (cpt=0; cpt<=0;cpt++) { */
8870: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 8871:
1.227 brouard 8872: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
8873: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
8874: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 8875:
1.227 brouard 8876: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8877: /* oldm=oldms;savm=savms; */
8878: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 8879:
1.227 brouard 8880: /* for (h=0; h<=nhstepm; h++){ */
8881: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
8882: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
8883: /* } */
8884: /* for(j=1; j<=nlstate+ndeath;j++) { */
8885: /* kk1=0.;kk2=0; */
8886: /* for(i=1; i<=nlstate;i++) { */
8887: /* if (mobilav==1) */
8888: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
8889: /* else { */
8890: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
8891: /* } */
8892: /* } */
8893: /* if (h==(int)(calagedatem+12*cpt)){ */
8894: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
8895: /* /\*fprintf(ficrespop," %.3f", kk1); */
8896: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
8897: /* } */
8898: /* } */
8899: /* for(i=1; i<=nlstate;i++){ */
8900: /* kk1=0.; */
8901: /* for(j=1; j<=nlstate;j++){ */
8902: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
8903: /* } */
8904: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
8905: /* } */
1.218 brouard 8906:
1.227 brouard 8907: /* if (h==(int)(calagedatem+12*cpt)) */
8908: /* for(j=1; j<=nlstate;j++) */
8909: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
8910: /* } */
8911: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8912: /* } */
8913: /* } */
1.218 brouard 8914:
1.227 brouard 8915: /* /\******\/ */
1.218 brouard 8916:
1.227 brouard 8917: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
8918: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
8919: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
8920: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
8921: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 8922:
1.227 brouard 8923: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8924: /* oldm=oldms;savm=savms; */
8925: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
8926: /* for (h=0; h<=nhstepm; h++){ */
8927: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
8928: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
8929: /* } */
8930: /* for(j=1; j<=nlstate+ndeath;j++) { */
8931: /* kk1=0.;kk2=0; */
8932: /* for(i=1; i<=nlstate;i++) { */
8933: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
8934: /* } */
8935: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
8936: /* } */
8937: /* } */
8938: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8939: /* } */
8940: /* } */
8941: /* } */
8942: /* } */
1.218 brouard 8943:
1.227 brouard 8944: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 8945:
1.227 brouard 8946: /* if (popforecast==1) { */
8947: /* free_ivector(popage,0,AGESUP); */
8948: /* free_vector(popeffectif,0,AGESUP); */
8949: /* free_vector(popcount,0,AGESUP); */
8950: /* } */
8951: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8952: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8953: /* fclose(ficrespop); */
8954: /* } /\* End of popforecast *\/ */
1.218 brouard 8955:
1.126 brouard 8956: int fileappend(FILE *fichier, char *optionfich)
8957: {
8958: if((fichier=fopen(optionfich,"a"))==NULL) {
8959: printf("Problem with file: %s\n", optionfich);
8960: fprintf(ficlog,"Problem with file: %s\n", optionfich);
8961: return (0);
8962: }
8963: fflush(fichier);
8964: return (1);
8965: }
8966:
8967:
8968: /**************** function prwizard **********************/
8969: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
8970: {
8971:
8972: /* Wizard to print covariance matrix template */
8973:
1.164 brouard 8974: char ca[32], cb[32];
8975: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 8976: int numlinepar;
8977:
8978: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
8979: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
8980: for(i=1; i <=nlstate; i++){
8981: jj=0;
8982: for(j=1; j <=nlstate+ndeath; j++){
8983: if(j==i) continue;
8984: jj++;
8985: /*ca[0]= k+'a'-1;ca[1]='\0';*/
8986: printf("%1d%1d",i,j);
8987: fprintf(ficparo,"%1d%1d",i,j);
8988: for(k=1; k<=ncovmodel;k++){
8989: /* printf(" %lf",param[i][j][k]); */
8990: /* fprintf(ficparo," %lf",param[i][j][k]); */
8991: printf(" 0.");
8992: fprintf(ficparo," 0.");
8993: }
8994: printf("\n");
8995: fprintf(ficparo,"\n");
8996: }
8997: }
8998: printf("# Scales (for hessian or gradient estimation)\n");
8999: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
9000: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
9001: for(i=1; i <=nlstate; i++){
9002: jj=0;
9003: for(j=1; j <=nlstate+ndeath; j++){
9004: if(j==i) continue;
9005: jj++;
9006: fprintf(ficparo,"%1d%1d",i,j);
9007: printf("%1d%1d",i,j);
9008: fflush(stdout);
9009: for(k=1; k<=ncovmodel;k++){
9010: /* printf(" %le",delti3[i][j][k]); */
9011: /* fprintf(ficparo," %le",delti3[i][j][k]); */
9012: printf(" 0.");
9013: fprintf(ficparo," 0.");
9014: }
9015: numlinepar++;
9016: printf("\n");
9017: fprintf(ficparo,"\n");
9018: }
9019: }
9020: printf("# Covariance matrix\n");
9021: /* # 121 Var(a12)\n\ */
9022: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9023: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9024: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9025: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9026: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9027: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9028: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9029: fflush(stdout);
9030: fprintf(ficparo,"# Covariance matrix\n");
9031: /* # 121 Var(a12)\n\ */
9032: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9033: /* # ...\n\ */
9034: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9035:
9036: for(itimes=1;itimes<=2;itimes++){
9037: jj=0;
9038: for(i=1; i <=nlstate; i++){
9039: for(j=1; j <=nlstate+ndeath; j++){
9040: if(j==i) continue;
9041: for(k=1; k<=ncovmodel;k++){
9042: jj++;
9043: ca[0]= k+'a'-1;ca[1]='\0';
9044: if(itimes==1){
9045: printf("#%1d%1d%d",i,j,k);
9046: fprintf(ficparo,"#%1d%1d%d",i,j,k);
9047: }else{
9048: printf("%1d%1d%d",i,j,k);
9049: fprintf(ficparo,"%1d%1d%d",i,j,k);
9050: /* printf(" %.5le",matcov[i][j]); */
9051: }
9052: ll=0;
9053: for(li=1;li <=nlstate; li++){
9054: for(lj=1;lj <=nlstate+ndeath; lj++){
9055: if(lj==li) continue;
9056: for(lk=1;lk<=ncovmodel;lk++){
9057: ll++;
9058: if(ll<=jj){
9059: cb[0]= lk +'a'-1;cb[1]='\0';
9060: if(ll<jj){
9061: if(itimes==1){
9062: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9063: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9064: }else{
9065: printf(" 0.");
9066: fprintf(ficparo," 0.");
9067: }
9068: }else{
9069: if(itimes==1){
9070: printf(" Var(%s%1d%1d)",ca,i,j);
9071: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
9072: }else{
9073: printf(" 0.");
9074: fprintf(ficparo," 0.");
9075: }
9076: }
9077: }
9078: } /* end lk */
9079: } /* end lj */
9080: } /* end li */
9081: printf("\n");
9082: fprintf(ficparo,"\n");
9083: numlinepar++;
9084: } /* end k*/
9085: } /*end j */
9086: } /* end i */
9087: } /* end itimes */
9088:
9089: } /* end of prwizard */
9090: /******************* Gompertz Likelihood ******************************/
9091: double gompertz(double x[])
9092: {
1.302 brouard 9093: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
1.126 brouard 9094: int i,n=0; /* n is the size of the sample */
9095:
1.220 brouard 9096: for (i=1;i<=imx ; i++) {
1.126 brouard 9097: sump=sump+weight[i];
9098: /* sump=sump+1;*/
9099: num=num+1;
9100: }
1.302 brouard 9101: L=0.0;
9102: /* agegomp=AGEGOMP; */
1.126 brouard 9103: /* for (i=0; i<=imx; i++)
9104: 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]);*/
9105:
1.302 brouard 9106: for (i=1;i<=imx ; i++) {
9107: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
9108: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
9109: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
9110: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
9111: * +
9112: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
9113: */
9114: if (wav[i] > 1 || agedc[i] < AGESUP) {
9115: if (cens[i] == 1){
9116: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
9117: } else if (cens[i] == 0){
1.126 brouard 9118: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
1.302 brouard 9119: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
9120: } else
9121: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
1.126 brouard 9122: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
1.302 brouard 9123: L=L+A*weight[i];
1.126 brouard 9124: /* 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 9125: }
9126: }
1.126 brouard 9127:
1.302 brouard 9128: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
1.126 brouard 9129:
9130: return -2*L*num/sump;
9131: }
9132:
1.136 brouard 9133: #ifdef GSL
9134: /******************* Gompertz_f Likelihood ******************************/
9135: double gompertz_f(const gsl_vector *v, void *params)
9136: {
1.302 brouard 9137: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
1.136 brouard 9138: double *x= (double *) v->data;
9139: int i,n=0; /* n is the size of the sample */
9140:
9141: for (i=0;i<=imx-1 ; i++) {
9142: sump=sump+weight[i];
9143: /* sump=sump+1;*/
9144: num=num+1;
9145: }
9146:
9147:
9148: /* for (i=0; i<=imx; i++)
9149: 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]);*/
9150: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
9151: for (i=1;i<=imx ; i++)
9152: {
9153: if (cens[i] == 1 && wav[i]>1)
9154: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
9155:
9156: if (cens[i] == 0 && wav[i]>1)
9157: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
9158: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
9159:
9160: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
9161: if (wav[i] > 1 ) { /* ??? */
9162: LL=LL+A*weight[i];
9163: /* 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]);*/
9164: }
9165: }
9166:
9167: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
9168: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
9169:
9170: return -2*LL*num/sump;
9171: }
9172: #endif
9173:
1.126 brouard 9174: /******************* Printing html file ***********/
1.201 brouard 9175: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 9176: int lastpass, int stepm, int weightopt, char model[],\
9177: int imx, double p[],double **matcov,double agemortsup){
9178: int i,k;
9179:
9180: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
9181: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
9182: for (i=1;i<=2;i++)
9183: 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 9184: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 9185: fprintf(fichtm,"</ul>");
9186:
9187: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
9188:
9189: 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>");
9190:
9191: for (k=agegomp;k<(agemortsup-2);k++)
9192: 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]);
9193:
9194:
9195: fflush(fichtm);
9196: }
9197:
9198: /******************* Gnuplot file **************/
1.201 brouard 9199: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 9200:
9201: char dirfileres[132],optfileres[132];
1.164 brouard 9202:
1.126 brouard 9203: int ng;
9204:
9205:
9206: /*#ifdef windows */
9207: fprintf(ficgp,"cd \"%s\" \n",pathc);
9208: /*#endif */
9209:
9210:
9211: strcpy(dirfileres,optionfilefiname);
9212: strcpy(optfileres,"vpl");
1.199 brouard 9213: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 9214: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 9215: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 9216: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 9217: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
9218:
9219: }
9220:
1.136 brouard 9221: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
9222: {
1.126 brouard 9223:
1.136 brouard 9224: /*-------- data file ----------*/
9225: FILE *fic;
9226: char dummy[]=" ";
1.240 brouard 9227: int i=0, j=0, n=0, iv=0, v;
1.223 brouard 9228: int lstra;
1.136 brouard 9229: int linei, month, year,iout;
1.302 brouard 9230: int noffset=0; /* This is the offset if BOM data file */
1.136 brouard 9231: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 9232: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 9233: char *stratrunc;
1.223 brouard 9234:
1.240 brouard 9235: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
9236: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
1.126 brouard 9237:
1.240 brouard 9238: for(v=1; v <=ncovcol;v++){
9239: DummyV[v]=0;
9240: FixedV[v]=0;
9241: }
9242: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
9243: DummyV[v]=1;
9244: FixedV[v]=0;
9245: }
9246: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
9247: DummyV[v]=0;
9248: FixedV[v]=1;
9249: }
9250: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
9251: DummyV[v]=1;
9252: FixedV[v]=1;
9253: }
9254: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
9255: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
9256: 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]);
9257: }
1.126 brouard 9258:
1.136 brouard 9259: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 9260: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9261: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 9262: }
1.126 brouard 9263:
1.302 brouard 9264: /* Is it a BOM UTF-8 Windows file? */
9265: /* First data line */
9266: linei=0;
9267: while(fgets(line, MAXLINE, fic)) {
9268: noffset=0;
9269: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
9270: {
9271: noffset=noffset+3;
9272: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
9273: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
9274: fflush(ficlog); return 1;
9275: }
9276: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
9277: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
9278: {
9279: noffset=noffset+2;
1.304 brouard 9280: 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);
9281: 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 9282: fflush(ficlog); return 1;
9283: }
9284: else if( line[0] == 0 && line[1] == 0)
9285: {
9286: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
9287: noffset=noffset+4;
1.304 brouard 9288: 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);
9289: 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 9290: fflush(ficlog); return 1;
9291: }
9292: } else{
9293: ;/*printf(" Not a BOM file\n");*/
9294: }
9295: /* If line starts with a # it is a comment */
9296: if (line[noffset] == '#') {
9297: linei=linei+1;
9298: break;
9299: }else{
9300: break;
9301: }
9302: }
9303: fclose(fic);
9304: if((fic=fopen(datafile,"r"))==NULL) {
9305: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9306: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
9307: }
9308: /* Not a Bom file */
9309:
1.136 brouard 9310: i=1;
9311: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
9312: linei=linei+1;
9313: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
9314: if(line[j] == '\t')
9315: line[j] = ' ';
9316: }
9317: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
9318: ;
9319: };
9320: line[j+1]=0; /* Trims blanks at end of line */
9321: if(line[0]=='#'){
9322: fprintf(ficlog,"Comment line\n%s\n",line);
9323: printf("Comment line\n%s\n",line);
9324: continue;
9325: }
9326: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 9327: strcpy(line, linetmp);
1.223 brouard 9328:
9329: /* Loops on waves */
9330: for (j=maxwav;j>=1;j--){
9331: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 9332: cutv(stra, strb, line, ' ');
9333: if(strb[0]=='.') { /* Missing value */
9334: lval=-1;
9335: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
9336: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
9337: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
9338: 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);
9339: 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);
9340: return 1;
9341: }
9342: }else{
9343: errno=0;
9344: /* what_kind_of_number(strb); */
9345: dval=strtod(strb,&endptr);
9346: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
9347: /* if(strb != endptr && *endptr == '\0') */
9348: /* dval=dlval; */
9349: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9350: if( strb[0]=='\0' || (*endptr != '\0')){
9351: 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);
9352: 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);
9353: return 1;
9354: }
9355: cotqvar[j][iv][i]=dval;
9356: cotvar[j][ntv+iv][i]=dval;
9357: }
9358: strcpy(line,stra);
1.223 brouard 9359: }/* end loop ntqv */
1.225 brouard 9360:
1.223 brouard 9361: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 9362: cutv(stra, strb, line, ' ');
9363: if(strb[0]=='.') { /* Missing value */
9364: lval=-1;
9365: }else{
9366: errno=0;
9367: lval=strtol(strb,&endptr,10);
9368: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9369: if( strb[0]=='\0' || (*endptr != '\0')){
9370: 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);
9371: 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);
9372: return 1;
9373: }
9374: }
9375: if(lval <-1 || lval >1){
9376: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 9377: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9378: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9379: For example, for multinomial values like 1, 2 and 3,\n \
9380: build V1=0 V2=0 for the reference value (1),\n \
9381: V1=1 V2=0 for (2) \n \
1.223 brouard 9382: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9383: output of IMaCh is often meaningless.\n \
1.223 brouard 9384: Exiting.\n",lval,linei, i,line,j);
1.238 brouard 9385: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 9386: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9387: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9388: For example, for multinomial values like 1, 2 and 3,\n \
9389: build V1=0 V2=0 for the reference value (1),\n \
9390: V1=1 V2=0 for (2) \n \
1.223 brouard 9391: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9392: output of IMaCh is often meaningless.\n \
1.223 brouard 9393: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.238 brouard 9394: return 1;
9395: }
9396: cotvar[j][iv][i]=(double)(lval);
9397: strcpy(line,stra);
1.223 brouard 9398: }/* end loop ntv */
1.225 brouard 9399:
1.223 brouard 9400: /* Statuses at wave */
1.137 brouard 9401: cutv(stra, strb, line, ' ');
1.223 brouard 9402: if(strb[0]=='.') { /* Missing value */
1.238 brouard 9403: lval=-1;
1.136 brouard 9404: }else{
1.238 brouard 9405: errno=0;
9406: lval=strtol(strb,&endptr,10);
9407: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9408: if( strb[0]=='\0' || (*endptr != '\0')){
9409: 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);
9410: 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);
9411: return 1;
9412: }
1.136 brouard 9413: }
1.225 brouard 9414:
1.136 brouard 9415: s[j][i]=lval;
1.225 brouard 9416:
1.223 brouard 9417: /* Date of Interview */
1.136 brouard 9418: strcpy(line,stra);
9419: cutv(stra, strb,line,' ');
1.169 brouard 9420: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9421: }
1.169 brouard 9422: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 9423: month=99;
9424: year=9999;
1.136 brouard 9425: }else{
1.225 brouard 9426: 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);
9427: 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);
9428: return 1;
1.136 brouard 9429: }
9430: anint[j][i]= (double) year;
1.302 brouard 9431: mint[j][i]= (double)month;
9432: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
9433: /* 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]); */
9434: /* 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]); */
9435: /* } */
1.136 brouard 9436: strcpy(line,stra);
1.223 brouard 9437: } /* End loop on waves */
1.225 brouard 9438:
1.223 brouard 9439: /* Date of death */
1.136 brouard 9440: cutv(stra, strb,line,' ');
1.169 brouard 9441: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9442: }
1.169 brouard 9443: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 9444: month=99;
9445: year=9999;
9446: }else{
1.141 brouard 9447: 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 9448: 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);
9449: return 1;
1.136 brouard 9450: }
9451: andc[i]=(double) year;
9452: moisdc[i]=(double) month;
9453: strcpy(line,stra);
9454:
1.223 brouard 9455: /* Date of birth */
1.136 brouard 9456: cutv(stra, strb,line,' ');
1.169 brouard 9457: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9458: }
1.169 brouard 9459: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 9460: month=99;
9461: year=9999;
9462: }else{
1.141 brouard 9463: 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);
9464: 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 9465: return 1;
1.136 brouard 9466: }
9467: if (year==9999) {
1.141 brouard 9468: 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);
9469: 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 9470: return 1;
9471:
1.136 brouard 9472: }
9473: annais[i]=(double)(year);
1.302 brouard 9474: moisnais[i]=(double)(month);
9475: for (j=1;j<=maxwav;j++){
9476: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
9477: 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]);
9478: 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]);
9479: }
9480: }
9481:
1.136 brouard 9482: strcpy(line,stra);
1.225 brouard 9483:
1.223 brouard 9484: /* Sample weight */
1.136 brouard 9485: cutv(stra, strb,line,' ');
9486: errno=0;
9487: dval=strtod(strb,&endptr);
9488: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 9489: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
9490: 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 9491: fflush(ficlog);
9492: return 1;
9493: }
9494: weight[i]=dval;
9495: strcpy(line,stra);
1.225 brouard 9496:
1.223 brouard 9497: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
9498: cutv(stra, strb, line, ' ');
9499: if(strb[0]=='.') { /* Missing value */
1.225 brouard 9500: lval=-1;
1.223 brouard 9501: }else{
1.225 brouard 9502: errno=0;
9503: /* what_kind_of_number(strb); */
9504: dval=strtod(strb,&endptr);
9505: /* if(strb != endptr && *endptr == '\0') */
9506: /* dval=dlval; */
9507: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9508: if( strb[0]=='\0' || (*endptr != '\0')){
9509: 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);
9510: 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);
9511: return 1;
9512: }
9513: coqvar[iv][i]=dval;
1.226 brouard 9514: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 9515: }
9516: strcpy(line,stra);
9517: }/* end loop nqv */
1.136 brouard 9518:
1.223 brouard 9519: /* Covariate values */
1.136 brouard 9520: for (j=ncovcol;j>=1;j--){
9521: cutv(stra, strb,line,' ');
1.223 brouard 9522: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 9523: lval=-1;
1.136 brouard 9524: }else{
1.225 brouard 9525: errno=0;
9526: lval=strtol(strb,&endptr,10);
9527: if( strb[0]=='\0' || (*endptr != '\0')){
9528: 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);
9529: 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);
9530: return 1;
9531: }
1.136 brouard 9532: }
9533: if(lval <-1 || lval >1){
1.225 brouard 9534: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9535: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9536: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9537: For example, for multinomial values like 1, 2 and 3,\n \
9538: build V1=0 V2=0 for the reference value (1),\n \
9539: V1=1 V2=0 for (2) \n \
1.136 brouard 9540: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9541: output of IMaCh is often meaningless.\n \
1.136 brouard 9542: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 9543: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9544: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9545: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9546: For example, for multinomial values like 1, 2 and 3,\n \
9547: build V1=0 V2=0 for the reference value (1),\n \
9548: V1=1 V2=0 for (2) \n \
1.136 brouard 9549: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9550: output of IMaCh is often meaningless.\n \
1.136 brouard 9551: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 9552: return 1;
1.136 brouard 9553: }
9554: covar[j][i]=(double)(lval);
9555: strcpy(line,stra);
9556: }
9557: lstra=strlen(stra);
1.225 brouard 9558:
1.136 brouard 9559: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
9560: stratrunc = &(stra[lstra-9]);
9561: num[i]=atol(stratrunc);
9562: }
9563: else
9564: num[i]=atol(stra);
9565: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
9566: 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;}*/
9567:
9568: i=i+1;
9569: } /* End loop reading data */
1.225 brouard 9570:
1.136 brouard 9571: *imax=i-1; /* Number of individuals */
9572: fclose(fic);
1.225 brouard 9573:
1.136 brouard 9574: return (0);
1.164 brouard 9575: /* endread: */
1.225 brouard 9576: printf("Exiting readdata: ");
9577: fclose(fic);
9578: return (1);
1.223 brouard 9579: }
1.126 brouard 9580:
1.234 brouard 9581: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 9582: char *p1 = *stri, *p2 = *stri;
1.235 brouard 9583: while (*p2 == ' ')
1.234 brouard 9584: p2++;
9585: /* while ((*p1++ = *p2++) !=0) */
9586: /* ; */
9587: /* do */
9588: /* while (*p2 == ' ') */
9589: /* p2++; */
9590: /* while (*p1++ == *p2++); */
9591: *stri=p2;
1.145 brouard 9592: }
9593:
1.235 brouard 9594: int decoderesult ( char resultline[], int nres)
1.230 brouard 9595: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
9596: {
1.235 brouard 9597: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 9598: char resultsav[MAXLINE];
1.234 brouard 9599: int resultmodel[MAXLINE];
9600: int modelresult[MAXLINE];
1.230 brouard 9601: char stra[80], strb[80], strc[80], strd[80],stre[80];
9602:
1.234 brouard 9603: removefirstspace(&resultline);
1.230 brouard 9604:
9605: if (strstr(resultline,"v") !=0){
9606: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
9607: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
9608: return 1;
9609: }
9610: trimbb(resultsav, resultline);
9611: if (strlen(resultsav) >1){
9612: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
9613: }
1.253 brouard 9614: if(j == 0){ /* Resultline but no = */
9615: TKresult[nres]=0; /* Combination for the nresult and the model */
9616: return (0);
9617: }
1.234 brouard 9618: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
9619: printf("ERROR: the number of variable in the resultline, %d, differs from the number of variable used in the model line, %d.\n",j, cptcovs);
9620: fprintf(ficlog,"ERROR: the number of variable in the resultline, %d, differs from the number of variable used in the model line, %d.\n",j, cptcovs);
9621: }
9622: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
9623: if(nbocc(resultsav,'=') >1){
9624: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
9625: resultsav= V4=1 V5=25.1 V3=0 strb=V3=0 stra= V4=1 V5=25.1 */
9626: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
9627: }else
9628: cutl(strc,strd,resultsav,'=');
1.230 brouard 9629: Tvalsel[k]=atof(strc); /* 1 */
1.234 brouard 9630:
1.230 brouard 9631: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
9632: Tvarsel[k]=atoi(strc);
9633: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
9634: /* cptcovsel++; */
9635: if (nbocc(stra,'=') >0)
9636: strcpy(resultsav,stra); /* and analyzes it */
9637: }
1.235 brouard 9638: /* Checking for missing or useless values in comparison of current model needs */
1.236 brouard 9639: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9640: if(Typevar[k1]==0){ /* Single covariate in model */
1.234 brouard 9641: match=0;
1.236 brouard 9642: for(k2=1; k2 <=j;k2++){/* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.237 brouard 9643: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar[1]=5 == Tvarsel[2]=5 */
1.236 brouard 9644: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.234 brouard 9645: match=1;
9646: break;
9647: }
9648: }
9649: if(match == 0){
9650: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
9651: }
9652: }
9653: }
1.235 brouard 9654: /* Checking for missing or useless values in comparison of current model needs */
9655: for(k2=1; k2 <=j;k2++){ /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 9656: match=0;
1.235 brouard 9657: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9658: if(Typevar[k1]==0){ /* Single */
1.237 brouard 9659: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
1.235 brouard 9660: resultmodel[k1]=k2; /* resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.234 brouard 9661: ++match;
9662: }
9663: }
9664: }
9665: if(match == 0){
9666: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
9667: }else if(match > 1){
9668: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
9669: }
9670: }
1.235 brouard 9671:
1.234 brouard 9672: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 9673: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9674: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */
9675: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/
9676: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
9677: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
9678: /* 1 0 0 0 */
9679: /* 2 1 0 0 */
9680: /* 3 0 1 0 */
9681: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */
9682: /* 5 0 0 1 */
9683: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */
9684: /* 7 0 1 1 */
9685: /* 8 1 1 1 */
1.237 brouard 9686: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
9687: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
9688: /* V5*age V5 known which value for nres? */
9689: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.235 brouard 9690: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* model line */
9691: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */
1.237 brouard 9692: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */
1.235 brouard 9693: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
9694: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */
1.237 brouard 9695: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */
9696: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
9697: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.235 brouard 9698: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4);
9699: k4++;;
9700: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */
9701: k3q= resultmodel[k1]; /* resultmodel[2] = 1=k3 */
9702: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
1.237 brouard 9703: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
9704: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
9705: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.235 brouard 9706: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
9707: k4q++;;
9708: }
9709: }
1.234 brouard 9710:
1.235 brouard 9711: TKresult[nres]=++k; /* Combination for the nresult and the model */
1.230 brouard 9712: return (0);
9713: }
1.235 brouard 9714:
1.230 brouard 9715: int decodemodel( char model[], int lastobs)
9716: /**< This routine decodes the model and returns:
1.224 brouard 9717: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
9718: * - nagesqr = 1 if age*age in the model, otherwise 0.
9719: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
9720: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
9721: * - cptcovage number of covariates with age*products =2
9722: * - cptcovs number of simple covariates
9723: * - 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
9724: * which is a new column after the 9 (ncovcol) variables.
9725: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
9726: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
9727: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
9728: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
9729: */
1.136 brouard 9730: {
1.238 brouard 9731: int i, j, k, ks, v;
1.227 brouard 9732: int j1, k1, k2, k3, k4;
1.136 brouard 9733: char modelsav[80];
1.145 brouard 9734: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 9735: char *strpt;
1.136 brouard 9736:
1.145 brouard 9737: /*removespace(model);*/
1.136 brouard 9738: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 9739: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 9740: if (strstr(model,"AGE") !=0){
1.192 brouard 9741: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
9742: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 9743: return 1;
9744: }
1.141 brouard 9745: if (strstr(model,"v") !=0){
9746: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
9747: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
9748: return 1;
9749: }
1.187 brouard 9750: strcpy(modelsav,model);
9751: if ((strpt=strstr(model,"age*age")) !=0){
9752: printf(" strpt=%s, model=%s\n",strpt, model);
9753: if(strpt != model){
1.234 brouard 9754: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 9755: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 9756: corresponding column of parameters.\n",model);
1.234 brouard 9757: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 9758: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 9759: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 9760: return 1;
1.225 brouard 9761: }
1.187 brouard 9762: nagesqr=1;
9763: if (strstr(model,"+age*age") !=0)
1.234 brouard 9764: substrchaine(modelsav, model, "+age*age");
1.187 brouard 9765: else if (strstr(model,"age*age+") !=0)
1.234 brouard 9766: substrchaine(modelsav, model, "age*age+");
1.187 brouard 9767: else
1.234 brouard 9768: substrchaine(modelsav, model, "age*age");
1.187 brouard 9769: }else
9770: nagesqr=0;
9771: if (strlen(modelsav) >1){
9772: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
9773: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 9774: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 9775: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 9776: * cst, age and age*age
9777: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
9778: /* including age products which are counted in cptcovage.
9779: * but the covariates which are products must be treated
9780: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 9781: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
9782: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 9783:
9784:
1.187 brouard 9785: /* Design
9786: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
9787: * < ncovcol=8 >
9788: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
9789: * k= 1 2 3 4 5 6 7 8
9790: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
9791: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 9792: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
9793: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 9794: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
9795: * Tage[++cptcovage]=k
9796: * if products, new covar are created after ncovcol with k1
9797: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
9798: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
9799: * 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
9800: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
9801: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
9802: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
9803: * < ncovcol=8 >
9804: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
9805: * k= 1 2 3 4 5 6 7 8 9 10 11 12
9806: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
9807: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
9808: * p Tprod[1]@2={ 6, 5}
9809: *p Tvard[1][1]@4= {7, 8, 5, 6}
9810: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
9811: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
9812: *How to reorganize?
9813: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
9814: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
9815: * {2, 1, 4, 8, 5, 6, 3, 7}
9816: * Struct []
9817: */
1.225 brouard 9818:
1.187 brouard 9819: /* This loop fills the array Tvar from the string 'model'.*/
9820: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
9821: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
9822: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
9823: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
9824: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
9825: /* k=1 Tvar[1]=2 (from V2) */
9826: /* k=5 Tvar[5] */
9827: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 9828: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 9829: /* } */
1.198 brouard 9830: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 9831: /*
9832: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 9833: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
9834: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
9835: }
1.187 brouard 9836: cptcovage=0;
9837: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.234 brouard 9838: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
1.225 brouard 9839: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.234 brouard 9840: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
9841: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
9842: /*scanf("%d",i);*/
9843: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
9844: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
9845: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
9846: /* covar is not filled and then is empty */
9847: cptcovprod--;
9848: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
9849: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
9850: Typevar[k]=1; /* 1 for age product */
9851: cptcovage++; /* Sums the number of covariates which include age as a product */
9852: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
9853: /*printf("stre=%s ", stre);*/
9854: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
9855: cptcovprod--;
9856: cutl(stre,strb,strc,'V');
9857: Tvar[k]=atoi(stre);
9858: Typevar[k]=1; /* 1 for age product */
9859: cptcovage++;
9860: Tage[cptcovage]=k;
9861: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
9862: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
9863: cptcovn++;
9864: cptcovprodnoage++;k1++;
9865: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
9866: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
9867: because this model-covariate is a construction we invent a new column
9868: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
9869: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
9870: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
9871: Typevar[k]=2; /* 2 for double fixed dummy covariates */
9872: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
9873: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
9874: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
9875: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
9876: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
9877: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
9878: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
9879: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 9880: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 brouard 9881: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
9882: for (i=1; i<=lastobs;i++){
9883: /* Computes the new covariate which is a product of
9884: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
9885: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
9886: }
9887: } /* End age is not in the model */
9888: } /* End if model includes a product */
9889: else { /* no more sum */
9890: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
9891: /* scanf("%d",i);*/
9892: cutl(strd,strc,strb,'V');
9893: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
9894: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
9895: Tvar[k]=atoi(strd);
9896: Typevar[k]=0; /* 0 for simple covariates */
9897: }
9898: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 9899: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 9900: scanf("%d",i);*/
1.187 brouard 9901: } /* end of loop + on total covariates */
9902: } /* end if strlen(modelsave == 0) age*age might exist */
9903: } /* end if strlen(model == 0) */
1.136 brouard 9904:
9905: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
9906: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 9907:
1.136 brouard 9908: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 9909: printf("cptcovprod=%d ", cptcovprod);
9910: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
9911: scanf("%d ",i);*/
9912:
9913:
1.230 brouard 9914: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
9915: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 9916: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
9917: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
9918: k = 1 2 3 4 5 6 7 8 9
9919: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
9920: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 9921: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
9922: Dummy[k] 1 0 0 0 3 1 1 2 3
9923: Tmodelind[combination of covar]=k;
1.225 brouard 9924: */
9925: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 9926: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 9927: /* 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 9928: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 9929: printf("Model=%s\n\
9930: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
9931: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
9932: 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);
9933: fprintf(ficlog,"Model=%s\n\
9934: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
9935: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
9936: 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 9937: for(k=-1;k<=cptcovt; k++){ Fixed[k]=0; Dummy[k]=0;}
1.234 brouard 9938: 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 */
9939: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 9940: Fixed[k]= 0;
9941: Dummy[k]= 0;
1.225 brouard 9942: ncoveff++;
1.232 brouard 9943: ncovf++;
1.234 brouard 9944: nsd++;
9945: modell[k].maintype= FTYPE;
9946: TvarsD[nsd]=Tvar[k];
9947: TvarsDind[nsd]=k;
9948: TvarF[ncovf]=Tvar[k];
9949: TvarFind[ncovf]=k;
9950: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9951: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9952: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
9953: Fixed[k]= 0;
9954: Dummy[k]= 0;
9955: ncoveff++;
9956: ncovf++;
9957: modell[k].maintype= FTYPE;
9958: TvarF[ncovf]=Tvar[k];
9959: TvarFind[ncovf]=k;
1.230 brouard 9960: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 9961: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.240 brouard 9962: }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 9963: Fixed[k]= 0;
9964: Dummy[k]= 1;
1.230 brouard 9965: nqfveff++;
1.234 brouard 9966: modell[k].maintype= FTYPE;
9967: modell[k].subtype= FQ;
9968: nsq++;
9969: TvarsQ[nsq]=Tvar[k];
9970: TvarsQind[nsq]=k;
1.232 brouard 9971: ncovf++;
1.234 brouard 9972: TvarF[ncovf]=Tvar[k];
9973: TvarFind[ncovf]=k;
1.231 brouard 9974: 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 9975: 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 9976: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.227 brouard 9977: Fixed[k]= 1;
9978: Dummy[k]= 0;
1.225 brouard 9979: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 9980: modell[k].maintype= VTYPE;
9981: modell[k].subtype= VD;
9982: nsd++;
9983: TvarsD[nsd]=Tvar[k];
9984: TvarsDind[nsd]=k;
9985: ncovv++; /* Only simple time varying variables */
9986: TvarV[ncovv]=Tvar[k];
1.242 brouard 9987: 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 9988: 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 */
9989: 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 9990: 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);
9991: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 9992: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 brouard 9993: Fixed[k]= 1;
9994: Dummy[k]= 1;
9995: nqtveff++;
9996: modell[k].maintype= VTYPE;
9997: modell[k].subtype= VQ;
9998: ncovv++; /* Only simple time varying variables */
9999: nsq++;
10000: TvarsQ[nsq]=Tvar[k];
10001: TvarsQind[nsq]=k;
10002: TvarV[ncovv]=Tvar[k];
1.242 brouard 10003: 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 10004: 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 */
10005: 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 10006: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
10007: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
10008: 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 10009: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 10010: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 10011: ncova++;
10012: TvarA[ncova]=Tvar[k];
10013: TvarAind[ncova]=k;
1.231 brouard 10014: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 10015: Fixed[k]= 2;
10016: Dummy[k]= 2;
10017: modell[k].maintype= ATYPE;
10018: modell[k].subtype= APFD;
10019: /* ncoveff++; */
1.227 brouard 10020: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 10021: Fixed[k]= 2;
10022: Dummy[k]= 3;
10023: modell[k].maintype= ATYPE;
10024: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
10025: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 10026: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 10027: Fixed[k]= 3;
10028: Dummy[k]= 2;
10029: modell[k].maintype= ATYPE;
10030: modell[k].subtype= APVD; /* Product age * varying dummy */
10031: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 10032: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 10033: Fixed[k]= 3;
10034: Dummy[k]= 3;
10035: modell[k].maintype= ATYPE;
10036: modell[k].subtype= APVQ; /* Product age * varying quantitative */
10037: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 10038: }
10039: }else if (Typevar[k] == 2) { /* product without age */
10040: k1=Tposprod[k];
10041: if(Tvard[k1][1] <=ncovcol){
1.240 brouard 10042: if(Tvard[k1][2] <=ncovcol){
10043: Fixed[k]= 1;
10044: Dummy[k]= 0;
10045: modell[k].maintype= FTYPE;
10046: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
10047: ncovf++; /* Fixed variables without age */
10048: TvarF[ncovf]=Tvar[k];
10049: TvarFind[ncovf]=k;
10050: }else if(Tvard[k1][2] <=ncovcol+nqv){
10051: Fixed[k]= 0; /* or 2 ?*/
10052: Dummy[k]= 1;
10053: modell[k].maintype= FTYPE;
10054: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
10055: ncovf++; /* Varying variables without age */
10056: TvarF[ncovf]=Tvar[k];
10057: TvarFind[ncovf]=k;
10058: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10059: Fixed[k]= 1;
10060: Dummy[k]= 0;
10061: modell[k].maintype= VTYPE;
10062: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
10063: ncovv++; /* Varying variables without age */
10064: TvarV[ncovv]=Tvar[k];
10065: TvarVind[ncovv]=k;
10066: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10067: Fixed[k]= 1;
10068: Dummy[k]= 1;
10069: modell[k].maintype= VTYPE;
10070: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
10071: ncovv++; /* Varying variables without age */
10072: TvarV[ncovv]=Tvar[k];
10073: TvarVind[ncovv]=k;
10074: }
1.227 brouard 10075: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.240 brouard 10076: if(Tvard[k1][2] <=ncovcol){
10077: Fixed[k]= 0; /* or 2 ?*/
10078: Dummy[k]= 1;
10079: modell[k].maintype= FTYPE;
10080: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
10081: ncovf++; /* Fixed variables without age */
10082: TvarF[ncovf]=Tvar[k];
10083: TvarFind[ncovf]=k;
10084: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10085: Fixed[k]= 1;
10086: Dummy[k]= 1;
10087: modell[k].maintype= VTYPE;
10088: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */
10089: ncovv++; /* Varying variables without age */
10090: TvarV[ncovv]=Tvar[k];
10091: TvarVind[ncovv]=k;
10092: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10093: Fixed[k]= 1;
10094: Dummy[k]= 1;
10095: modell[k].maintype= VTYPE;
10096: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */
10097: ncovv++; /* Varying variables without age */
10098: TvarV[ncovv]=Tvar[k];
10099: TvarVind[ncovv]=k;
10100: ncovv++; /* Varying variables without age */
10101: TvarV[ncovv]=Tvar[k];
10102: TvarVind[ncovv]=k;
10103: }
1.227 brouard 10104: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.240 brouard 10105: if(Tvard[k1][2] <=ncovcol){
10106: Fixed[k]= 1;
10107: Dummy[k]= 1;
10108: modell[k].maintype= VTYPE;
10109: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
10110: ncovv++; /* Varying variables without age */
10111: TvarV[ncovv]=Tvar[k];
10112: TvarVind[ncovv]=k;
10113: }else if(Tvard[k1][2] <=ncovcol+nqv){
10114: Fixed[k]= 1;
10115: Dummy[k]= 1;
10116: modell[k].maintype= VTYPE;
10117: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
10118: ncovv++; /* Varying variables without age */
10119: TvarV[ncovv]=Tvar[k];
10120: TvarVind[ncovv]=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 time varying dummy * time 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 time varying dummy * time 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+ntv+nqtv){
1.240 brouard 10139: if(Tvard[k1][2] <=ncovcol){
10140: Fixed[k]= 1;
10141: Dummy[k]= 1;
10142: modell[k].maintype= VTYPE;
10143: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
10144: ncovv++; /* Varying variables without age */
10145: TvarV[ncovv]=Tvar[k];
10146: TvarVind[ncovv]=k;
10147: }else if(Tvard[k1][2] <=ncovcol+nqv){
10148: Fixed[k]= 1;
10149: Dummy[k]= 1;
10150: modell[k].maintype= VTYPE;
10151: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
10152: ncovv++; /* Varying variables without age */
10153: TvarV[ncovv]=Tvar[k];
10154: TvarVind[ncovv]=k;
10155: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10156: Fixed[k]= 1;
10157: Dummy[k]= 1;
10158: modell[k].maintype= VTYPE;
10159: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
10160: ncovv++; /* Varying variables without age */
10161: TvarV[ncovv]=Tvar[k];
10162: TvarVind[ncovv]=k;
10163: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10164: Fixed[k]= 1;
10165: Dummy[k]= 1;
10166: modell[k].maintype= VTYPE;
10167: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
10168: ncovv++; /* Varying variables without age */
10169: TvarV[ncovv]=Tvar[k];
10170: TvarVind[ncovv]=k;
10171: }
1.227 brouard 10172: }else{
1.240 brouard 10173: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10174: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10175: } /*end k1*/
1.225 brouard 10176: }else{
1.226 brouard 10177: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
10178: 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 10179: }
1.227 brouard 10180: 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 10181: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 10182: 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]);
10183: }
10184: /* Searching for doublons in the model */
10185: for(k1=1; k1<= cptcovt;k1++){
10186: for(k2=1; k2 <k1;k2++){
1.285 brouard 10187: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
10188: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
1.234 brouard 10189: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
10190: if(Tvar[k1]==Tvar[k2]){
1.285 brouard 10191: 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]);
10192: 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 10193: return(1);
10194: }
10195: }else if (Typevar[k1] ==2){
10196: k3=Tposprod[k1];
10197: k4=Tposprod[k2];
10198: 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])) ){
10199: 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]]);
10200: 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);
10201: return(1);
10202: }
10203: }
1.227 brouard 10204: }
10205: }
1.225 brouard 10206: }
10207: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
10208: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 10209: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
10210: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 10211: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 10212: /*endread:*/
1.225 brouard 10213: printf("Exiting decodemodel: ");
10214: return (1);
1.136 brouard 10215: }
10216:
1.169 brouard 10217: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.248 brouard 10218: {/* Check ages at death */
1.136 brouard 10219: int i, m;
1.218 brouard 10220: int firstone=0;
10221:
1.136 brouard 10222: for (i=1; i<=imx; i++) {
10223: for(m=2; (m<= maxwav); m++) {
10224: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
10225: anint[m][i]=9999;
1.216 brouard 10226: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
10227: s[m][i]=-1;
1.136 brouard 10228: }
10229: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.260 brouard 10230: *nberr = *nberr + 1;
1.218 brouard 10231: if(firstone == 0){
10232: firstone=1;
1.260 brouard 10233: 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 10234: }
1.262 brouard 10235: 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 10236: s[m][i]=-1; /* Droping the death status */
1.136 brouard 10237: }
10238: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 10239: (*nberr)++;
1.259 brouard 10240: 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 10241: 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 10242: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.136 brouard 10243: }
10244: }
10245: }
10246:
10247: for (i=1; i<=imx; i++) {
10248: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
10249: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 10250: 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 10251: if (s[m][i] >= nlstate+1) {
1.169 brouard 10252: if(agedc[i]>0){
10253: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 10254: agev[m][i]=agedc[i];
1.214 brouard 10255: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 10256: }else {
1.136 brouard 10257: if ((int)andc[i]!=9999){
10258: nbwarn++;
10259: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
10260: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
10261: agev[m][i]=-1;
10262: }
10263: }
1.169 brouard 10264: } /* agedc > 0 */
1.214 brouard 10265: } /* end if */
1.136 brouard 10266: else if(s[m][i] !=9){ /* Standard case, age in fractional
10267: years but with the precision of a month */
10268: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
10269: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
10270: agev[m][i]=1;
10271: else if(agev[m][i] < *agemin){
10272: *agemin=agev[m][i];
10273: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
10274: }
10275: else if(agev[m][i] >*agemax){
10276: *agemax=agev[m][i];
1.156 brouard 10277: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 10278: }
10279: /*agev[m][i]=anint[m][i]-annais[i];*/
10280: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 10281: } /* en if 9*/
1.136 brouard 10282: else { /* =9 */
1.214 brouard 10283: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 10284: agev[m][i]=1;
10285: s[m][i]=-1;
10286: }
10287: }
1.214 brouard 10288: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 10289: agev[m][i]=1;
1.214 brouard 10290: else{
10291: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10292: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10293: agev[m][i]=0;
10294: }
10295: } /* End for lastpass */
10296: }
1.136 brouard 10297:
10298: for (i=1; i<=imx; i++) {
10299: for(m=firstpass; (m<=lastpass); m++){
10300: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 10301: (*nberr)++;
1.136 brouard 10302: 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);
10303: 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);
10304: return 1;
10305: }
10306: }
10307: }
10308:
10309: /*for (i=1; i<=imx; i++){
10310: for (m=firstpass; (m<lastpass); m++){
10311: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
10312: }
10313:
10314: }*/
10315:
10316:
1.139 brouard 10317: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
10318: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 10319:
10320: return (0);
1.164 brouard 10321: /* endread:*/
1.136 brouard 10322: printf("Exiting calandcheckages: ");
10323: return (1);
10324: }
10325:
1.172 brouard 10326: #if defined(_MSC_VER)
10327: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10328: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10329: //#include "stdafx.h"
10330: //#include <stdio.h>
10331: //#include <tchar.h>
10332: //#include <windows.h>
10333: //#include <iostream>
10334: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
10335:
10336: LPFN_ISWOW64PROCESS fnIsWow64Process;
10337:
10338: BOOL IsWow64()
10339: {
10340: BOOL bIsWow64 = FALSE;
10341:
10342: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
10343: // (HANDLE, PBOOL);
10344:
10345: //LPFN_ISWOW64PROCESS fnIsWow64Process;
10346:
10347: HMODULE module = GetModuleHandle(_T("kernel32"));
10348: const char funcName[] = "IsWow64Process";
10349: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
10350: GetProcAddress(module, funcName);
10351:
10352: if (NULL != fnIsWow64Process)
10353: {
10354: if (!fnIsWow64Process(GetCurrentProcess(),
10355: &bIsWow64))
10356: //throw std::exception("Unknown error");
10357: printf("Unknown error\n");
10358: }
10359: return bIsWow64 != FALSE;
10360: }
10361: #endif
1.177 brouard 10362:
1.191 brouard 10363: void syscompilerinfo(int logged)
1.292 brouard 10364: {
10365: #include <stdint.h>
10366:
10367: /* #include "syscompilerinfo.h"*/
1.185 brouard 10368: /* command line Intel compiler 32bit windows, XP compatible:*/
10369: /* /GS /W3 /Gy
10370: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
10371: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
10372: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 10373: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
10374: */
10375: /* 64 bits */
1.185 brouard 10376: /*
10377: /GS /W3 /Gy
10378: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
10379: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
10380: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
10381: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
10382: /* Optimization are useless and O3 is slower than O2 */
10383: /*
10384: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
10385: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
10386: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
10387: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
10388: */
1.186 brouard 10389: /* Link is */ /* /OUT:"visual studio
1.185 brouard 10390: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
10391: /PDB:"visual studio
10392: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
10393: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
10394: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
10395: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
10396: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
10397: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
10398: uiAccess='false'"
10399: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
10400: /NOLOGO /TLBID:1
10401: */
1.292 brouard 10402:
10403:
1.177 brouard 10404: #if defined __INTEL_COMPILER
1.178 brouard 10405: #if defined(__GNUC__)
10406: struct utsname sysInfo; /* For Intel on Linux and OS/X */
10407: #endif
1.177 brouard 10408: #elif defined(__GNUC__)
1.179 brouard 10409: #ifndef __APPLE__
1.174 brouard 10410: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 10411: #endif
1.177 brouard 10412: struct utsname sysInfo;
1.178 brouard 10413: int cross = CROSS;
10414: if (cross){
10415: printf("Cross-");
1.191 brouard 10416: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 10417: }
1.174 brouard 10418: #endif
10419:
1.191 brouard 10420: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 10421: #if defined(__clang__)
1.191 brouard 10422: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 10423: #endif
10424: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 10425: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 10426: #endif
10427: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 10428: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 10429: #endif
10430: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 10431: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 10432: #endif
10433: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 10434: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 10435: #endif
10436: #if defined(_MSC_VER)
1.191 brouard 10437: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 10438: #endif
10439: #if defined(__PGI)
1.191 brouard 10440: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 10441: #endif
10442: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 10443: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 10444: #endif
1.191 brouard 10445: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 10446:
1.167 brouard 10447: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
10448: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
10449: // Windows (x64 and x86)
1.191 brouard 10450: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 10451: #elif __unix__ // all unices, not all compilers
10452: // Unix
1.191 brouard 10453: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 10454: #elif __linux__
10455: // linux
1.191 brouard 10456: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 10457: #elif __APPLE__
1.174 brouard 10458: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 10459: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 10460: #endif
10461:
10462: /* __MINGW32__ */
10463: /* __CYGWIN__ */
10464: /* __MINGW64__ */
10465: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
10466: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
10467: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
10468: /* _WIN64 // Defined for applications for Win64. */
10469: /* _M_X64 // Defined for compilations that target x64 processors. */
10470: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 10471:
1.167 brouard 10472: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 10473: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 10474: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 10475: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 10476: #else
1.191 brouard 10477: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 10478: #endif
10479:
1.169 brouard 10480: #if defined(__GNUC__)
10481: # if defined(__GNUC_PATCHLEVEL__)
10482: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10483: + __GNUC_MINOR__ * 100 \
10484: + __GNUC_PATCHLEVEL__)
10485: # else
10486: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10487: + __GNUC_MINOR__ * 100)
10488: # endif
1.174 brouard 10489: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 10490: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 10491:
10492: if (uname(&sysInfo) != -1) {
10493: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 10494: 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 10495: }
10496: else
10497: perror("uname() error");
1.179 brouard 10498: //#ifndef __INTEL_COMPILER
10499: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 10500: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 10501: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 10502: #endif
1.169 brouard 10503: #endif
1.172 brouard 10504:
1.286 brouard 10505: // void main ()
1.172 brouard 10506: // {
1.169 brouard 10507: #if defined(_MSC_VER)
1.174 brouard 10508: if (IsWow64()){
1.191 brouard 10509: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
10510: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 10511: }
10512: else{
1.191 brouard 10513: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
10514: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 10515: }
1.172 brouard 10516: // printf("\nPress Enter to continue...");
10517: // getchar();
10518: // }
10519:
1.169 brouard 10520: #endif
10521:
1.167 brouard 10522:
1.219 brouard 10523: }
1.136 brouard 10524:
1.219 brouard 10525: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.288 brouard 10526: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
1.235 brouard 10527: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 10528: /* double ftolpl = 1.e-10; */
1.180 brouard 10529: double age, agebase, agelim;
1.203 brouard 10530: double tot;
1.180 brouard 10531:
1.202 brouard 10532: strcpy(filerespl,"PL_");
10533: strcat(filerespl,fileresu);
10534: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.288 brouard 10535: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
10536: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
1.202 brouard 10537: }
1.288 brouard 10538: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
10539: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 10540: pstamp(ficrespl);
1.288 brouard 10541: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 10542: fprintf(ficrespl,"#Age ");
10543: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
10544: fprintf(ficrespl,"\n");
1.180 brouard 10545:
1.219 brouard 10546: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 10547:
1.219 brouard 10548: agebase=ageminpar;
10549: agelim=agemaxpar;
1.180 brouard 10550:
1.227 brouard 10551: /* i1=pow(2,ncoveff); */
1.234 brouard 10552: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 10553: if (cptcovn < 1){i1=1;}
1.180 brouard 10554:
1.238 brouard 10555: for(k=1; k<=i1;k++){ /* For each combination k of dummy covariates in the model */
10556: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 10557: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10558: continue;
1.235 brouard 10559:
1.238 brouard 10560: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10561: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
10562: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
10563: /* k=k+1; */
10564: /* to clean */
10565: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
10566: fprintf(ficrespl,"#******");
10567: printf("#******");
10568: fprintf(ficlog,"#******");
10569: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
10570: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
10571: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10572: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10573: }
10574: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
10575: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10576: fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10577: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10578: }
10579: fprintf(ficrespl,"******\n");
10580: printf("******\n");
10581: fprintf(ficlog,"******\n");
10582: if(invalidvarcomb[k]){
10583: printf("\nCombination (%d) ignored because no case \n",k);
10584: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
10585: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
10586: continue;
10587: }
1.219 brouard 10588:
1.238 brouard 10589: fprintf(ficrespl,"#Age ");
10590: for(j=1;j<=cptcoveff;j++) {
10591: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10592: }
10593: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
10594: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 10595:
1.238 brouard 10596: for (age=agebase; age<=agelim; age++){
10597: /* for (age=agebase; age<=agebase; age++){ */
10598: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres);
10599: fprintf(ficrespl,"%.0f ",age );
10600: for(j=1;j<=cptcoveff;j++)
10601: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10602: tot=0.;
10603: for(i=1; i<=nlstate;i++){
10604: tot += prlim[i][i];
10605: fprintf(ficrespl," %.5f", prlim[i][i]);
10606: }
10607: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
10608: } /* Age */
10609: /* was end of cptcod */
10610: } /* cptcov */
10611: } /* nres */
1.219 brouard 10612: return 0;
1.180 brouard 10613: }
10614:
1.218 brouard 10615: 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 10616: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
1.218 brouard 10617:
10618: /* Computes the back prevalence limit for any combination of covariate values
10619: * at any age between ageminpar and agemaxpar
10620: */
1.235 brouard 10621: int i, j, k, i1, nres=0 ;
1.217 brouard 10622: /* double ftolpl = 1.e-10; */
10623: double age, agebase, agelim;
10624: double tot;
1.218 brouard 10625: /* double ***mobaverage; */
10626: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 10627:
10628: strcpy(fileresplb,"PLB_");
10629: strcat(fileresplb,fileresu);
10630: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
1.288 brouard 10631: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
10632: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
1.217 brouard 10633: }
1.288 brouard 10634: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
10635: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
1.217 brouard 10636: pstamp(ficresplb);
1.288 brouard 10637: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.217 brouard 10638: fprintf(ficresplb,"#Age ");
10639: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
10640: fprintf(ficresplb,"\n");
10641:
1.218 brouard 10642:
10643: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
10644:
10645: agebase=ageminpar;
10646: agelim=agemaxpar;
10647:
10648:
1.227 brouard 10649: i1=pow(2,cptcoveff);
1.218 brouard 10650: if (cptcovn < 1){i1=1;}
1.227 brouard 10651:
1.238 brouard 10652: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10653: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 10654: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10655: continue;
10656: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
10657: fprintf(ficresplb,"#******");
10658: printf("#******");
10659: fprintf(ficlog,"#******");
10660: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
10661: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10662: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10663: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10664: }
10665: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
10666: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10667: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10668: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10669: }
10670: fprintf(ficresplb,"******\n");
10671: printf("******\n");
10672: fprintf(ficlog,"******\n");
10673: if(invalidvarcomb[k]){
10674: printf("\nCombination (%d) ignored because no cases \n",k);
10675: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
10676: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
10677: continue;
10678: }
1.218 brouard 10679:
1.238 brouard 10680: fprintf(ficresplb,"#Age ");
10681: for(j=1;j<=cptcoveff;j++) {
10682: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10683: }
10684: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
10685: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 10686:
10687:
1.238 brouard 10688: for (age=agebase; age<=agelim; age++){
10689: /* for (age=agebase; age<=agebase; age++){ */
10690: if(mobilavproj > 0){
10691: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
10692: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 10693: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 10694: }else if (mobilavproj == 0){
10695: 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);
10696: 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);
10697: exit(1);
10698: }else{
10699: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 10700: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.266 brouard 10701: /* printf("TOTOT\n"); */
10702: /* exit(1); */
1.238 brouard 10703: }
10704: fprintf(ficresplb,"%.0f ",age );
10705: for(j=1;j<=cptcoveff;j++)
10706: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10707: tot=0.;
10708: for(i=1; i<=nlstate;i++){
10709: tot += bprlim[i][i];
10710: fprintf(ficresplb," %.5f", bprlim[i][i]);
10711: }
10712: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
10713: } /* Age */
10714: /* was end of cptcod */
1.255 brouard 10715: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
1.238 brouard 10716: } /* end of any combination */
10717: } /* end of nres */
1.218 brouard 10718: /* hBijx(p, bage, fage); */
10719: /* fclose(ficrespijb); */
10720:
10721: return 0;
1.217 brouard 10722: }
1.218 brouard 10723:
1.180 brouard 10724: int hPijx(double *p, int bage, int fage){
10725: /*------------- h Pij x at various ages ------------*/
10726:
10727: int stepsize;
10728: int agelim;
10729: int hstepm;
10730: int nhstepm;
1.235 brouard 10731: int h, i, i1, j, k, k4, nres=0;
1.180 brouard 10732:
10733: double agedeb;
10734: double ***p3mat;
10735:
1.201 brouard 10736: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 10737: if((ficrespij=fopen(filerespij,"w"))==NULL) {
10738: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
10739: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
10740: }
10741: printf("Computing pij: result on file '%s' \n", filerespij);
10742: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
10743:
10744: stepsize=(int) (stepm+YEARM-1)/YEARM;
10745: /*if (stepm<=24) stepsize=2;*/
10746:
10747: agelim=AGESUP;
10748: hstepm=stepsize*YEARM; /* Every year of age */
10749: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 10750:
1.180 brouard 10751: /* hstepm=1; aff par mois*/
10752: pstamp(ficrespij);
10753: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 10754: i1= pow(2,cptcoveff);
1.218 brouard 10755: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10756: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
10757: /* k=k+1; */
1.235 brouard 10758: for(nres=1; nres <= nresult; nres++) /* For each resultline */
10759: for(k=1; k<=i1;k++){
1.253 brouard 10760: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 10761: continue;
1.183 brouard 10762: fprintf(ficrespij,"\n#****** ");
1.227 brouard 10763: for(j=1;j<=cptcoveff;j++)
1.198 brouard 10764: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 10765: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
10766: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10767: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10768: }
1.183 brouard 10769: fprintf(ficrespij,"******\n");
10770:
10771: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
10772: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
10773: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
10774:
10775: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 10776:
1.183 brouard 10777: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10778: oldm=oldms;savm=savms;
1.235 brouard 10779: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.183 brouard 10780: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
10781: for(i=1; i<=nlstate;i++)
10782: for(j=1; j<=nlstate+ndeath;j++)
10783: fprintf(ficrespij," %1d-%1d",i,j);
10784: fprintf(ficrespij,"\n");
10785: for (h=0; h<=nhstepm; h++){
10786: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
10787: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 10788: for(i=1; i<=nlstate;i++)
10789: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 10790: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 10791: fprintf(ficrespij,"\n");
10792: }
1.183 brouard 10793: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10794: fprintf(ficrespij,"\n");
10795: }
1.180 brouard 10796: /*}*/
10797: }
1.218 brouard 10798: return 0;
1.180 brouard 10799: }
1.218 brouard 10800:
10801: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 10802: /*------------- h Bij x at various ages ------------*/
10803:
10804: int stepsize;
1.218 brouard 10805: /* int agelim; */
10806: int ageminl;
1.217 brouard 10807: int hstepm;
10808: int nhstepm;
1.238 brouard 10809: int h, i, i1, j, k, nres;
1.218 brouard 10810:
1.217 brouard 10811: double agedeb;
10812: double ***p3mat;
1.218 brouard 10813:
10814: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
10815: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
10816: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
10817: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
10818: }
10819: printf("Computing pij back: result on file '%s' \n", filerespijb);
10820: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
10821:
10822: stepsize=(int) (stepm+YEARM-1)/YEARM;
10823: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 10824:
1.218 brouard 10825: /* agelim=AGESUP; */
1.289 brouard 10826: ageminl=AGEINF; /* was 30 */
1.218 brouard 10827: hstepm=stepsize*YEARM; /* Every year of age */
10828: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
10829:
10830: /* hstepm=1; aff par mois*/
10831: pstamp(ficrespijb);
1.255 brouard 10832: 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 10833: i1= pow(2,cptcoveff);
1.218 brouard 10834: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10835: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
10836: /* k=k+1; */
1.238 brouard 10837: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10838: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 10839: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10840: continue;
10841: fprintf(ficrespijb,"\n#****** ");
10842: for(j=1;j<=cptcoveff;j++)
10843: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10844: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
10845: fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10846: }
10847: fprintf(ficrespijb,"******\n");
1.264 brouard 10848: if(invalidvarcomb[k]){ /* Is it necessary here? */
1.238 brouard 10849: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
10850: continue;
10851: }
10852:
10853: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
10854: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
10855: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
1.297 brouard 10856: 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 */
10857: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
1.238 brouard 10858:
10859: /* nhstepm=nhstepm*YEARM; aff par mois*/
10860:
1.266 brouard 10861: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
10862: /* and memory limitations if stepm is small */
10863:
1.238 brouard 10864: /* oldm=oldms;savm=savms; */
10865: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.267 brouard 10866: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.238 brouard 10867: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
1.255 brouard 10868: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
1.217 brouard 10869: for(i=1; i<=nlstate;i++)
10870: for(j=1; j<=nlstate+ndeath;j++)
1.238 brouard 10871: fprintf(ficrespijb," %1d-%1d",i,j);
1.217 brouard 10872: fprintf(ficrespijb,"\n");
1.238 brouard 10873: for (h=0; h<=nhstepm; h++){
10874: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
10875: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
10876: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
10877: for(i=1; i<=nlstate;i++)
10878: for(j=1; j<=nlstate+ndeath;j++)
10879: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
10880: fprintf(ficrespijb,"\n");
10881: }
10882: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10883: fprintf(ficrespijb,"\n");
10884: } /* end age deb */
10885: } /* end combination */
10886: } /* end nres */
1.218 brouard 10887: return 0;
10888: } /* hBijx */
1.217 brouard 10889:
1.180 brouard 10890:
1.136 brouard 10891: /***********************************************/
10892: /**************** Main Program *****************/
10893: /***********************************************/
10894:
10895: int main(int argc, char *argv[])
10896: {
10897: #ifdef GSL
10898: const gsl_multimin_fminimizer_type *T;
10899: size_t iteri = 0, it;
10900: int rval = GSL_CONTINUE;
10901: int status = GSL_SUCCESS;
10902: double ssval;
10903: #endif
10904: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.290 brouard 10905: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
10906: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
1.209 brouard 10907: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 10908: int jj, ll, li, lj, lk;
1.136 brouard 10909: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 10910: int num_filled;
1.136 brouard 10911: int itimes;
10912: int NDIM=2;
10913: int vpopbased=0;
1.235 brouard 10914: int nres=0;
1.258 brouard 10915: int endishere=0;
1.277 brouard 10916: int noffset=0;
1.274 brouard 10917: int ncurrv=0; /* Temporary variable */
10918:
1.164 brouard 10919: char ca[32], cb[32];
1.136 brouard 10920: /* FILE *fichtm; *//* Html File */
10921: /* FILE *ficgp;*/ /*Gnuplot File */
10922: struct stat info;
1.191 brouard 10923: double agedeb=0.;
1.194 brouard 10924:
10925: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 10926: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 10927:
1.165 brouard 10928: double fret;
1.191 brouard 10929: double dum=0.; /* Dummy variable */
1.136 brouard 10930: double ***p3mat;
1.218 brouard 10931: /* double ***mobaverage; */
1.164 brouard 10932:
10933: char line[MAXLINE];
1.197 brouard 10934: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
10935:
1.234 brouard 10936: char modeltemp[MAXLINE];
1.230 brouard 10937: char resultline[MAXLINE];
10938:
1.136 brouard 10939: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 10940: char *tok, *val; /* pathtot */
1.290 brouard 10941: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs declared globally ;*/
1.195 brouard 10942: int c, h , cpt, c2;
1.191 brouard 10943: int jl=0;
10944: int i1, j1, jk, stepsize=0;
1.194 brouard 10945: int count=0;
10946:
1.164 brouard 10947: int *tab;
1.136 brouard 10948: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.296 brouard 10949: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
10950: /* double anprojf, mprojf, jprojf; */
10951: /* double jintmean,mintmean,aintmean; */
10952: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
10953: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
10954: double yrfproj= 10.0; /* Number of years of forward projections */
10955: double yrbproj= 10.0; /* Number of years of backward projections */
10956: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
1.136 brouard 10957: int mobilav=0,popforecast=0;
1.191 brouard 10958: int hstepm=0, nhstepm=0;
1.136 brouard 10959: int agemortsup;
10960: float sumlpop=0.;
10961: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
10962: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
10963:
1.191 brouard 10964: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 10965: double ftolpl=FTOL;
10966: double **prlim;
1.217 brouard 10967: double **bprlim;
1.136 brouard 10968: double ***param; /* Matrix of parameters */
1.251 brouard 10969: double ***paramstart; /* Matrix of starting parameter values */
10970: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
1.136 brouard 10971: double **matcov; /* Matrix of covariance */
1.203 brouard 10972: double **hess; /* Hessian matrix */
1.136 brouard 10973: double ***delti3; /* Scale */
10974: double *delti; /* Scale */
10975: double ***eij, ***vareij;
10976: double **varpl; /* Variances of prevalence limits by age */
1.269 brouard 10977:
1.136 brouard 10978: double *epj, vepp;
1.164 brouard 10979:
1.273 brouard 10980: double dateprev1, dateprev2;
1.296 brouard 10981: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
10982: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
10983:
1.217 brouard 10984:
1.136 brouard 10985: double **ximort;
1.145 brouard 10986: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 10987: int *dcwave;
10988:
1.164 brouard 10989: char z[1]="c";
1.136 brouard 10990:
10991: /*char *strt;*/
10992: char strtend[80];
1.126 brouard 10993:
1.164 brouard 10994:
1.126 brouard 10995: /* setlocale (LC_ALL, ""); */
10996: /* bindtextdomain (PACKAGE, LOCALEDIR); */
10997: /* textdomain (PACKAGE); */
10998: /* setlocale (LC_CTYPE, ""); */
10999: /* setlocale (LC_MESSAGES, ""); */
11000:
11001: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 11002: rstart_time = time(NULL);
11003: /* (void) gettimeofday(&start_time,&tzp);*/
11004: start_time = *localtime(&rstart_time);
1.126 brouard 11005: curr_time=start_time;
1.157 brouard 11006: /*tml = *localtime(&start_time.tm_sec);*/
11007: /* strcpy(strstart,asctime(&tml)); */
11008: strcpy(strstart,asctime(&start_time));
1.126 brouard 11009:
11010: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 11011: /* tp.tm_sec = tp.tm_sec +86400; */
11012: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 11013: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
11014: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
11015: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 11016: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 11017: /* strt=asctime(&tmg); */
11018: /* printf("Time(after) =%s",strstart); */
11019: /* (void) time (&time_value);
11020: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
11021: * tm = *localtime(&time_value);
11022: * strstart=asctime(&tm);
11023: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
11024: */
11025:
11026: nberr=0; /* Number of errors and warnings */
11027: nbwarn=0;
1.184 brouard 11028: #ifdef WIN32
11029: _getcwd(pathcd, size);
11030: #else
1.126 brouard 11031: getcwd(pathcd, size);
1.184 brouard 11032: #endif
1.191 brouard 11033: syscompilerinfo(0);
1.196 brouard 11034: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 11035: if(argc <=1){
11036: printf("\nEnter the parameter file name: ");
1.205 brouard 11037: if(!fgets(pathr,FILENAMELENGTH,stdin)){
11038: printf("ERROR Empty parameter file name\n");
11039: goto end;
11040: }
1.126 brouard 11041: i=strlen(pathr);
11042: if(pathr[i-1]=='\n')
11043: pathr[i-1]='\0';
1.156 brouard 11044: i=strlen(pathr);
1.205 brouard 11045: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 11046: pathr[i-1]='\0';
1.205 brouard 11047: }
11048: i=strlen(pathr);
11049: if( i==0 ){
11050: printf("ERROR Empty parameter file name\n");
11051: goto end;
11052: }
11053: for (tok = pathr; tok != NULL; ){
1.126 brouard 11054: printf("Pathr |%s|\n",pathr);
11055: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
11056: printf("val= |%s| pathr=%s\n",val,pathr);
11057: strcpy (pathtot, val);
11058: if(pathr[0] == '\0') break; /* Dirty */
11059: }
11060: }
1.281 brouard 11061: else if (argc<=2){
11062: strcpy(pathtot,argv[1]);
11063: }
1.126 brouard 11064: else{
11065: strcpy(pathtot,argv[1]);
1.281 brouard 11066: strcpy(z,argv[2]);
11067: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
1.126 brouard 11068: }
11069: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
11070: /*cygwin_split_path(pathtot,path,optionfile);
11071: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
11072: /* cutv(path,optionfile,pathtot,'\\');*/
11073:
11074: /* Split argv[0], imach program to get pathimach */
11075: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
11076: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11077: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11078: /* strcpy(pathimach,argv[0]); */
11079: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
11080: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
11081: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 11082: #ifdef WIN32
11083: _chdir(path); /* Can be a relative path */
11084: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
11085: #else
1.126 brouard 11086: chdir(path); /* Can be a relative path */
1.184 brouard 11087: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
11088: #endif
11089: printf("Current directory %s!\n",pathcd);
1.126 brouard 11090: strcpy(command,"mkdir ");
11091: strcat(command,optionfilefiname);
11092: if((outcmd=system(command)) != 0){
1.169 brouard 11093: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 11094: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
11095: /* fclose(ficlog); */
11096: /* exit(1); */
11097: }
11098: /* if((imk=mkdir(optionfilefiname))<0){ */
11099: /* perror("mkdir"); */
11100: /* } */
11101:
11102: /*-------- arguments in the command line --------*/
11103:
1.186 brouard 11104: /* Main Log file */
1.126 brouard 11105: strcat(filelog, optionfilefiname);
11106: strcat(filelog,".log"); /* */
11107: if((ficlog=fopen(filelog,"w"))==NULL) {
11108: printf("Problem with logfile %s\n",filelog);
11109: goto end;
11110: }
11111: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 11112: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 11113: fprintf(ficlog,"\nEnter the parameter file name: \n");
11114: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
11115: path=%s \n\
11116: optionfile=%s\n\
11117: optionfilext=%s\n\
1.156 brouard 11118: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 11119:
1.197 brouard 11120: syscompilerinfo(1);
1.167 brouard 11121:
1.126 brouard 11122: printf("Local time (at start):%s",strstart);
11123: fprintf(ficlog,"Local time (at start): %s",strstart);
11124: fflush(ficlog);
11125: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 11126: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 11127:
11128: /* */
11129: strcpy(fileres,"r");
11130: strcat(fileres, optionfilefiname);
1.201 brouard 11131: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 11132: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 11133: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 11134:
1.186 brouard 11135: /* Main ---------arguments file --------*/
1.126 brouard 11136:
11137: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 11138: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
11139: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 11140: fflush(ficlog);
1.149 brouard 11141: /* goto end; */
11142: exit(70);
1.126 brouard 11143: }
11144:
11145: strcpy(filereso,"o");
1.201 brouard 11146: strcat(filereso,fileresu);
1.126 brouard 11147: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
11148: printf("Problem with Output resultfile: %s\n", filereso);
11149: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
11150: fflush(ficlog);
11151: goto end;
11152: }
1.278 brouard 11153: /*-------- Rewriting parameter file ----------*/
11154: strcpy(rfileres,"r"); /* "Rparameterfile */
11155: strcat(rfileres,optionfilefiname); /* Parameter file first name */
11156: strcat(rfileres,"."); /* */
11157: strcat(rfileres,optionfilext); /* Other files have txt extension */
11158: if((ficres =fopen(rfileres,"w"))==NULL) {
11159: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
11160: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
11161: fflush(ficlog);
11162: goto end;
11163: }
11164: fprintf(ficres,"#IMaCh %s\n",version);
1.126 brouard 11165:
1.278 brouard 11166:
1.126 brouard 11167: /* Reads comments: lines beginning with '#' */
11168: numlinepar=0;
1.277 brouard 11169: /* Is it a BOM UTF-8 Windows file? */
11170: /* First parameter line */
1.197 brouard 11171: while(fgets(line, MAXLINE, ficpar)) {
1.277 brouard 11172: noffset=0;
11173: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
11174: {
11175: noffset=noffset+3;
11176: printf("# File is an UTF8 Bom.\n"); // 0xBF
11177: }
1.302 brouard 11178: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
11179: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
1.277 brouard 11180: {
11181: noffset=noffset+2;
11182: printf("# File is an UTF16BE BOM file\n");
11183: }
11184: else if( line[0] == 0 && line[1] == 0)
11185: {
11186: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
11187: noffset=noffset+4;
11188: printf("# File is an UTF16BE BOM file\n");
11189: }
11190: } else{
11191: ;/*printf(" Not a BOM file\n");*/
11192: }
11193:
1.197 brouard 11194: /* If line starts with a # it is a comment */
1.277 brouard 11195: if (line[noffset] == '#') {
1.197 brouard 11196: numlinepar++;
11197: fputs(line,stdout);
11198: fputs(line,ficparo);
1.278 brouard 11199: fputs(line,ficres);
1.197 brouard 11200: fputs(line,ficlog);
11201: continue;
11202: }else
11203: break;
11204: }
11205: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
11206: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
11207: if (num_filled != 5) {
11208: printf("Should be 5 parameters\n");
1.283 brouard 11209: fprintf(ficlog,"Should be 5 parameters\n");
1.197 brouard 11210: }
1.126 brouard 11211: numlinepar++;
1.197 brouard 11212: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.283 brouard 11213: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11214: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11215: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.197 brouard 11216: }
11217: /* Second parameter line */
11218: while(fgets(line, MAXLINE, ficpar)) {
1.283 brouard 11219: /* while(fscanf(ficpar,"%[^\n]", line)) { */
11220: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
1.197 brouard 11221: if (line[0] == '#') {
11222: numlinepar++;
1.283 brouard 11223: printf("%s",line);
11224: fprintf(ficres,"%s",line);
11225: fprintf(ficparo,"%s",line);
11226: fprintf(ficlog,"%s",line);
1.197 brouard 11227: continue;
11228: }else
11229: break;
11230: }
1.223 brouard 11231: 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", \
11232: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
11233: if (num_filled != 11) {
11234: 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 11235: printf("but line=%s\n",line);
1.283 brouard 11236: 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");
11237: fprintf(ficlog,"but line=%s\n",line);
1.197 brouard 11238: }
1.286 brouard 11239: if( lastpass > maxwav){
11240: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11241: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11242: fflush(ficlog);
11243: goto end;
11244: }
11245: 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 11246: 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 11247: 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 11248: 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 11249: }
1.203 brouard 11250: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 11251: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 11252: /* Third parameter line */
11253: while(fgets(line, MAXLINE, ficpar)) {
11254: /* If line starts with a # it is a comment */
11255: if (line[0] == '#') {
11256: numlinepar++;
1.283 brouard 11257: printf("%s",line);
11258: fprintf(ficres,"%s",line);
11259: fprintf(ficparo,"%s",line);
11260: fprintf(ficlog,"%s",line);
1.197 brouard 11261: continue;
11262: }else
11263: break;
11264: }
1.201 brouard 11265: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
1.279 brouard 11266: if (num_filled != 1){
1.302 brouard 11267: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
11268: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
1.197 brouard 11269: model[0]='\0';
11270: goto end;
11271: }
11272: else{
11273: if (model[0]=='+'){
11274: for(i=1; i<=strlen(model);i++)
11275: modeltemp[i-1]=model[i];
1.201 brouard 11276: strcpy(model,modeltemp);
1.197 brouard 11277: }
11278: }
1.199 brouard 11279: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 11280: printf("model=1+age+%s\n",model);fflush(stdout);
1.283 brouard 11281: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
11282: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
11283: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 11284: }
11285: /* 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); */
11286: /* numlinepar=numlinepar+3; /\* In general *\/ */
11287: /* 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 11288: /* 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); */
11289: /* 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 11290: fflush(ficlog);
1.190 brouard 11291: /* if(model[0]=='#'|| model[0]== '\0'){ */
11292: if(model[0]=='#'){
1.279 brouard 11293: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
11294: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
11295: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
1.187 brouard 11296: if(mle != -1){
1.279 brouard 11297: 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 11298: exit(1);
11299: }
11300: }
1.126 brouard 11301: while((c=getc(ficpar))=='#' && c!= EOF){
11302: ungetc(c,ficpar);
11303: fgets(line, MAXLINE, ficpar);
11304: numlinepar++;
1.195 brouard 11305: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
11306: z[0]=line[1];
11307: }
11308: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 11309: fputs(line, stdout);
11310: //puts(line);
1.126 brouard 11311: fputs(line,ficparo);
11312: fputs(line,ficlog);
11313: }
11314: ungetc(c,ficpar);
11315:
11316:
1.290 brouard 11317: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
11318: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
11319: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
11320: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /**< Time varying covariate (dummy and quantitative)*/
1.136 brouard 11321: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
11322: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
11323: v1+v2*age+v2*v3 makes cptcovn = 3
11324: */
11325: if (strlen(model)>1)
1.187 brouard 11326: 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 11327: else
1.187 brouard 11328: ncovmodel=2; /* Constant and age */
1.133 brouard 11329: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
11330: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 11331: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
11332: 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);
11333: 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);
11334: fflush(stdout);
11335: fclose (ficlog);
11336: goto end;
11337: }
1.126 brouard 11338: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11339: delti=delti3[1][1];
11340: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
11341: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
1.247 brouard 11342: /* We could also provide initial parameters values giving by simple logistic regression
11343: * only one way, that is without matrix product. We will have nlstate maximizations */
11344: /* for(i=1;i<nlstate;i++){ */
11345: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
11346: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
11347: /* } */
1.126 brouard 11348: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 11349: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
11350: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11351: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11352: fclose (ficparo);
11353: fclose (ficlog);
11354: goto end;
11355: exit(0);
1.220 brouard 11356: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 11357: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 11358: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
11359: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11360: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11361: matcov=matrix(1,npar,1,npar);
1.203 brouard 11362: hess=matrix(1,npar,1,npar);
1.220 brouard 11363: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 11364: /* Read guessed parameters */
1.126 brouard 11365: /* Reads comments: lines beginning with '#' */
11366: while((c=getc(ficpar))=='#' && c!= EOF){
11367: ungetc(c,ficpar);
11368: fgets(line, MAXLINE, ficpar);
11369: numlinepar++;
1.141 brouard 11370: fputs(line,stdout);
1.126 brouard 11371: fputs(line,ficparo);
11372: fputs(line,ficlog);
11373: }
11374: ungetc(c,ficpar);
11375:
11376: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.251 brouard 11377: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.126 brouard 11378: for(i=1; i <=nlstate; i++){
1.234 brouard 11379: j=0;
1.126 brouard 11380: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 11381: if(jj==i) continue;
11382: j++;
1.292 brouard 11383: while((c=getc(ficpar))=='#' && c!= EOF){
11384: ungetc(c,ficpar);
11385: fgets(line, MAXLINE, ficpar);
11386: numlinepar++;
11387: fputs(line,stdout);
11388: fputs(line,ficparo);
11389: fputs(line,ficlog);
11390: }
11391: ungetc(c,ficpar);
1.234 brouard 11392: fscanf(ficpar,"%1d%1d",&i1,&j1);
11393: if ((i1 != i) || (j1 != jj)){
11394: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 11395: It might be a problem of design; if ncovcol and the model are correct\n \
11396: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 11397: exit(1);
11398: }
11399: fprintf(ficparo,"%1d%1d",i1,j1);
11400: if(mle==1)
11401: printf("%1d%1d",i,jj);
11402: fprintf(ficlog,"%1d%1d",i,jj);
11403: for(k=1; k<=ncovmodel;k++){
11404: fscanf(ficpar," %lf",¶m[i][j][k]);
11405: if(mle==1){
11406: printf(" %lf",param[i][j][k]);
11407: fprintf(ficlog," %lf",param[i][j][k]);
11408: }
11409: else
11410: fprintf(ficlog," %lf",param[i][j][k]);
11411: fprintf(ficparo," %lf",param[i][j][k]);
11412: }
11413: fscanf(ficpar,"\n");
11414: numlinepar++;
11415: if(mle==1)
11416: printf("\n");
11417: fprintf(ficlog,"\n");
11418: fprintf(ficparo,"\n");
1.126 brouard 11419: }
11420: }
11421: fflush(ficlog);
1.234 brouard 11422:
1.251 brouard 11423: /* Reads parameters values */
1.126 brouard 11424: p=param[1][1];
1.251 brouard 11425: pstart=paramstart[1][1];
1.126 brouard 11426:
11427: /* Reads comments: lines beginning with '#' */
11428: while((c=getc(ficpar))=='#' && c!= EOF){
11429: ungetc(c,ficpar);
11430: fgets(line, MAXLINE, ficpar);
11431: numlinepar++;
1.141 brouard 11432: fputs(line,stdout);
1.126 brouard 11433: fputs(line,ficparo);
11434: fputs(line,ficlog);
11435: }
11436: ungetc(c,ficpar);
11437:
11438: for(i=1; i <=nlstate; i++){
11439: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 11440: fscanf(ficpar,"%1d%1d",&i1,&j1);
11441: if ( (i1-i) * (j1-j) != 0){
11442: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
11443: exit(1);
11444: }
11445: printf("%1d%1d",i,j);
11446: fprintf(ficparo,"%1d%1d",i1,j1);
11447: fprintf(ficlog,"%1d%1d",i1,j1);
11448: for(k=1; k<=ncovmodel;k++){
11449: fscanf(ficpar,"%le",&delti3[i][j][k]);
11450: printf(" %le",delti3[i][j][k]);
11451: fprintf(ficparo," %le",delti3[i][j][k]);
11452: fprintf(ficlog," %le",delti3[i][j][k]);
11453: }
11454: fscanf(ficpar,"\n");
11455: numlinepar++;
11456: printf("\n");
11457: fprintf(ficparo,"\n");
11458: fprintf(ficlog,"\n");
1.126 brouard 11459: }
11460: }
11461: fflush(ficlog);
1.234 brouard 11462:
1.145 brouard 11463: /* Reads covariance matrix */
1.126 brouard 11464: delti=delti3[1][1];
1.220 brouard 11465:
11466:
1.126 brouard 11467: /* 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 11468:
1.126 brouard 11469: /* Reads comments: lines beginning with '#' */
11470: while((c=getc(ficpar))=='#' && c!= EOF){
11471: ungetc(c,ficpar);
11472: fgets(line, MAXLINE, ficpar);
11473: numlinepar++;
1.141 brouard 11474: fputs(line,stdout);
1.126 brouard 11475: fputs(line,ficparo);
11476: fputs(line,ficlog);
11477: }
11478: ungetc(c,ficpar);
1.220 brouard 11479:
1.126 brouard 11480: matcov=matrix(1,npar,1,npar);
1.203 brouard 11481: hess=matrix(1,npar,1,npar);
1.131 brouard 11482: for(i=1; i <=npar; i++)
11483: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 11484:
1.194 brouard 11485: /* Scans npar lines */
1.126 brouard 11486: for(i=1; i <=npar; i++){
1.226 brouard 11487: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 11488: if(count != 3){
1.226 brouard 11489: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11490: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11491: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11492: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11493: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11494: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11495: exit(1);
1.220 brouard 11496: }else{
1.226 brouard 11497: if(mle==1)
11498: printf("%1d%1d%d",i1,j1,jk);
11499: }
11500: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
11501: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 11502: for(j=1; j <=i; j++){
1.226 brouard 11503: fscanf(ficpar," %le",&matcov[i][j]);
11504: if(mle==1){
11505: printf(" %.5le",matcov[i][j]);
11506: }
11507: fprintf(ficlog," %.5le",matcov[i][j]);
11508: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 11509: }
11510: fscanf(ficpar,"\n");
11511: numlinepar++;
11512: if(mle==1)
1.220 brouard 11513: printf("\n");
1.126 brouard 11514: fprintf(ficlog,"\n");
11515: fprintf(ficparo,"\n");
11516: }
1.194 brouard 11517: /* End of read covariance matrix npar lines */
1.126 brouard 11518: for(i=1; i <=npar; i++)
11519: for(j=i+1;j<=npar;j++)
1.226 brouard 11520: matcov[i][j]=matcov[j][i];
1.126 brouard 11521:
11522: if(mle==1)
11523: printf("\n");
11524: fprintf(ficlog,"\n");
11525:
11526: fflush(ficlog);
11527:
11528: } /* End of mle != -3 */
1.218 brouard 11529:
1.186 brouard 11530: /* Main data
11531: */
1.290 brouard 11532: nobs=lastobs-firstobs+1; /* was = lastobs;*/
11533: /* num=lvector(1,n); */
11534: /* moisnais=vector(1,n); */
11535: /* annais=vector(1,n); */
11536: /* moisdc=vector(1,n); */
11537: /* andc=vector(1,n); */
11538: /* weight=vector(1,n); */
11539: /* agedc=vector(1,n); */
11540: /* cod=ivector(1,n); */
11541: /* for(i=1;i<=n;i++){ */
11542: num=lvector(firstobs,lastobs);
11543: moisnais=vector(firstobs,lastobs);
11544: annais=vector(firstobs,lastobs);
11545: moisdc=vector(firstobs,lastobs);
11546: andc=vector(firstobs,lastobs);
11547: weight=vector(firstobs,lastobs);
11548: agedc=vector(firstobs,lastobs);
11549: cod=ivector(firstobs,lastobs);
11550: for(i=firstobs;i<=lastobs;i++){
1.234 brouard 11551: num[i]=0;
11552: moisnais[i]=0;
11553: annais[i]=0;
11554: moisdc[i]=0;
11555: andc[i]=0;
11556: agedc[i]=0;
11557: cod[i]=0;
11558: weight[i]=1.0; /* Equal weights, 1 by default */
11559: }
1.290 brouard 11560: mint=matrix(1,maxwav,firstobs,lastobs);
11561: anint=matrix(1,maxwav,firstobs,lastobs);
11562: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
1.126 brouard 11563: tab=ivector(1,NCOVMAX);
1.144 brouard 11564: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 11565: 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 11566:
1.136 brouard 11567: /* Reads data from file datafile */
11568: if (readdata(datafile, firstobs, lastobs, &imx)==1)
11569: goto end;
11570:
11571: /* Calculation of the number of parameters from char model */
1.234 brouard 11572: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 11573: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
11574: k=3 V4 Tvar[k=3]= 4 (from V4)
11575: k=2 V1 Tvar[k=2]= 1 (from V1)
11576: k=1 Tvar[1]=2 (from V2)
1.234 brouard 11577: */
11578:
11579: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
11580: TvarsDind=ivector(1,NCOVMAX); /* */
11581: TvarsD=ivector(1,NCOVMAX); /* */
11582: TvarsQind=ivector(1,NCOVMAX); /* */
11583: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 11584: TvarF=ivector(1,NCOVMAX); /* */
11585: TvarFind=ivector(1,NCOVMAX); /* */
11586: TvarV=ivector(1,NCOVMAX); /* */
11587: TvarVind=ivector(1,NCOVMAX); /* */
11588: TvarA=ivector(1,NCOVMAX); /* */
11589: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 11590: TvarFD=ivector(1,NCOVMAX); /* */
11591: TvarFDind=ivector(1,NCOVMAX); /* */
11592: TvarFQ=ivector(1,NCOVMAX); /* */
11593: TvarFQind=ivector(1,NCOVMAX); /* */
11594: TvarVD=ivector(1,NCOVMAX); /* */
11595: TvarVDind=ivector(1,NCOVMAX); /* */
11596: TvarVQ=ivector(1,NCOVMAX); /* */
11597: TvarVQind=ivector(1,NCOVMAX); /* */
11598:
1.230 brouard 11599: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 11600: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 11601: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
11602: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
11603: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 11604: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
11605: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
11606: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
11607: */
11608: /* For model-covariate k tells which data-covariate to use but
11609: because this model-covariate is a construction we invent a new column
11610: ncovcol + k1
11611: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
11612: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 11613: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
11614: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 11615: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
11616: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 11617: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 11618: */
1.145 brouard 11619: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
11620: 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 11621: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
11622: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 11623: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 11624: 4 covariates (3 plus signs)
11625: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
11626: */
1.230 brouard 11627: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 11628: * individual dummy, fixed or varying:
11629: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
11630: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 11631: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
11632: * V1 df, V2 qf, V3 & V4 dv, V5 qv
11633: * Tmodelind[1]@9={9,0,3,2,}*/
11634: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
11635: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 11636: * individual quantitative, fixed or varying:
11637: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
11638: * 3, 1, 0, 0, 0, 0, 0, 0},
11639: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 11640: /* Main decodemodel */
11641:
1.187 brouard 11642:
1.223 brouard 11643: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 11644: goto end;
11645:
1.137 brouard 11646: if((double)(lastobs-imx)/(double)imx > 1.10){
11647: nbwarn++;
11648: 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);
11649: 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);
11650: }
1.136 brouard 11651: /* if(mle==1){*/
1.137 brouard 11652: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
11653: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 11654: }
11655:
11656: /*-calculation of age at interview from date of interview and age at death -*/
11657: agev=matrix(1,maxwav,1,imx);
11658:
11659: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
11660: goto end;
11661:
1.126 brouard 11662:
1.136 brouard 11663: agegomp=(int)agemin;
1.290 brouard 11664: free_vector(moisnais,firstobs,lastobs);
11665: free_vector(annais,firstobs,lastobs);
1.126 brouard 11666: /* free_matrix(mint,1,maxwav,1,n);
11667: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 11668: /* free_vector(moisdc,1,n); */
11669: /* free_vector(andc,1,n); */
1.145 brouard 11670: /* */
11671:
1.126 brouard 11672: wav=ivector(1,imx);
1.214 brouard 11673: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
11674: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
11675: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
11676: 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.*/
11677: bh=imatrix(1,lastpass-firstpass+2,1,imx);
11678: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 11679:
11680: /* Concatenates waves */
1.214 brouard 11681: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
11682: Death is a valid wave (if date is known).
11683: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
11684: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
11685: and mw[mi+1][i]. dh depends on stepm.
11686: */
11687:
1.126 brouard 11688: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.248 brouard 11689: /* Concatenates waves */
1.145 brouard 11690:
1.290 brouard 11691: free_vector(moisdc,firstobs,lastobs);
11692: free_vector(andc,firstobs,lastobs);
1.215 brouard 11693:
1.126 brouard 11694: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
11695: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
11696: ncodemax[1]=1;
1.145 brouard 11697: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 11698: cptcoveff=0;
1.220 brouard 11699: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
11700: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 11701: }
11702:
11703: ncovcombmax=pow(2,cptcoveff);
11704: invalidvarcomb=ivector(1, ncovcombmax);
11705: for(i=1;i<ncovcombmax;i++)
11706: invalidvarcomb[i]=0;
11707:
1.211 brouard 11708: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 11709: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 11710: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 11711:
1.200 brouard 11712: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 11713: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 11714: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 11715: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
11716: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
11717: * (currently 0 or 1) in the data.
11718: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
11719: * corresponding modality (h,j).
11720: */
11721:
1.145 brouard 11722: h=0;
11723: /*if (cptcovn > 0) */
1.126 brouard 11724: m=pow(2,cptcoveff);
11725:
1.144 brouard 11726: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 11727: * For k=4 covariates, h goes from 1 to m=2**k
11728: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
11729: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 11730: * h\k 1 2 3 4
1.143 brouard 11731: *______________________________
11732: * 1 i=1 1 i=1 1 i=1 1 i=1 1
11733: * 2 2 1 1 1
11734: * 3 i=2 1 2 1 1
11735: * 4 2 2 1 1
11736: * 5 i=3 1 i=2 1 2 1
11737: * 6 2 1 2 1
11738: * 7 i=4 1 2 2 1
11739: * 8 2 2 2 1
1.197 brouard 11740: * 9 i=5 1 i=3 1 i=2 1 2
11741: * 10 2 1 1 2
11742: * 11 i=6 1 2 1 2
11743: * 12 2 2 1 2
11744: * 13 i=7 1 i=4 1 2 2
11745: * 14 2 1 2 2
11746: * 15 i=8 1 2 2 2
11747: * 16 2 2 2 2
1.143 brouard 11748: */
1.212 brouard 11749: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 11750: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
11751: * and the value of each covariate?
11752: * V1=1, V2=1, V3=2, V4=1 ?
11753: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
11754: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
11755: * In order to get the real value in the data, we use nbcode
11756: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
11757: * We are keeping this crazy system in order to be able (in the future?)
11758: * to have more than 2 values (0 or 1) for a covariate.
11759: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
11760: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
11761: * bbbbbbbb
11762: * 76543210
11763: * h-1 00000101 (6-1=5)
1.219 brouard 11764: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 11765: * &
11766: * 1 00000001 (1)
1.219 brouard 11767: * 00000000 = 1 & ((h-1) >> (k-1))
11768: * +1= 00000001 =1
1.211 brouard 11769: *
11770: * h=14, k=3 => h'=h-1=13, k'=k-1=2
11771: * h' 1101 =2^3+2^2+0x2^1+2^0
11772: * >>k' 11
11773: * & 00000001
11774: * = 00000001
11775: * +1 = 00000010=2 = codtabm(14,3)
11776: * Reverse h=6 and m=16?
11777: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
11778: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
11779: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
11780: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
11781: * V3=decodtabm(14,3,2**4)=2
11782: * h'=13 1101 =2^3+2^2+0x2^1+2^0
11783: *(h-1) >> (j-1) 0011 =13 >> 2
11784: * &1 000000001
11785: * = 000000001
11786: * +1= 000000010 =2
11787: * 2211
11788: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
11789: * V3=2
1.220 brouard 11790: * codtabm and decodtabm are identical
1.211 brouard 11791: */
11792:
1.145 brouard 11793:
11794: free_ivector(Ndum,-1,NCOVMAX);
11795:
11796:
1.126 brouard 11797:
1.186 brouard 11798: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 11799: strcpy(optionfilegnuplot,optionfilefiname);
11800: if(mle==-3)
1.201 brouard 11801: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 11802: strcat(optionfilegnuplot,".gp");
11803:
11804: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
11805: printf("Problem with file %s",optionfilegnuplot);
11806: }
11807: else{
1.204 brouard 11808: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 11809: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 11810: //fprintf(ficgp,"set missing 'NaNq'\n");
11811: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 11812: }
11813: /* fclose(ficgp);*/
1.186 brouard 11814:
11815:
11816: /* Initialisation of --------- index.htm --------*/
1.126 brouard 11817:
11818: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
11819: if(mle==-3)
1.201 brouard 11820: strcat(optionfilehtm,"-MORT_");
1.126 brouard 11821: strcat(optionfilehtm,".htm");
11822: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 11823: printf("Problem with %s \n",optionfilehtm);
11824: exit(0);
1.126 brouard 11825: }
11826:
11827: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
11828: strcat(optionfilehtmcov,"-cov.htm");
11829: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
11830: printf("Problem with %s \n",optionfilehtmcov), exit(0);
11831: }
11832: else{
11833: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
11834: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 11835: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 11836: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
11837: }
11838:
1.213 brouard 11839: 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 11840: <hr size=\"2\" color=\"#EC5E5E\"> \n\
11841: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 11842: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 11843: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 11844: \n\
11845: <hr size=\"2\" color=\"#EC5E5E\">\
11846: <ul><li><h4>Parameter files</h4>\n\
11847: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
11848: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
11849: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
11850: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
11851: - Date and time at start: %s</ul>\n",\
11852: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
11853: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
11854: fileres,fileres,\
11855: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
11856: fflush(fichtm);
11857:
11858: strcpy(pathr,path);
11859: strcat(pathr,optionfilefiname);
1.184 brouard 11860: #ifdef WIN32
11861: _chdir(optionfilefiname); /* Move to directory named optionfile */
11862: #else
1.126 brouard 11863: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 11864: #endif
11865:
1.126 brouard 11866:
1.220 brouard 11867: /* Calculates basic frequencies. Computes observed prevalence at single age
11868: and for any valid combination of covariates
1.126 brouard 11869: and prints on file fileres'p'. */
1.251 brouard 11870: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.227 brouard 11871: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 11872:
11873: fprintf(fichtm,"\n");
1.286 brouard 11874: 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 11875: ftol, stepm);
11876: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
11877: ncurrv=1;
11878: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
11879: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
11880: ncurrv=i;
11881: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 11882: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
1.274 brouard 11883: ncurrv=i;
11884: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 11885: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
1.274 brouard 11886: ncurrv=i;
11887: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
11888: 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", \
11889: nlstate, ndeath, maxwav, mle, weightopt);
11890:
11891: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
11892: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
11893:
11894:
11895: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Total number of observations=%d <br>\n\
1.126 brouard 11896: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
11897: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.274 brouard 11898: imx,agemin,agemax,jmin,jmax,jmean);
1.126 brouard 11899: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.268 brouard 11900: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11901: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11902: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11903: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 11904:
1.126 brouard 11905: /* For Powell, parameters are in a vector p[] starting at p[1]
11906: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
11907: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
11908:
11909: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 11910: /* For mortality only */
1.126 brouard 11911: if (mle==-3){
1.136 brouard 11912: ximort=matrix(1,NDIM,1,NDIM);
1.248 brouard 11913: for(i=1;i<=NDIM;i++)
11914: for(j=1;j<=NDIM;j++)
11915: ximort[i][j]=0.;
1.186 brouard 11916: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.290 brouard 11917: cens=ivector(firstobs,lastobs);
11918: ageexmed=vector(firstobs,lastobs);
11919: agecens=vector(firstobs,lastobs);
11920: dcwave=ivector(firstobs,lastobs);
1.223 brouard 11921:
1.126 brouard 11922: for (i=1; i<=imx; i++){
11923: dcwave[i]=-1;
11924: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 11925: if (s[m][i]>nlstate) {
11926: dcwave[i]=m;
11927: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
11928: break;
11929: }
1.126 brouard 11930: }
1.226 brouard 11931:
1.126 brouard 11932: for (i=1; i<=imx; i++) {
11933: if (wav[i]>0){
1.226 brouard 11934: ageexmed[i]=agev[mw[1][i]][i];
11935: j=wav[i];
11936: agecens[i]=1.;
11937:
11938: if (ageexmed[i]> 1 && wav[i] > 0){
11939: agecens[i]=agev[mw[j][i]][i];
11940: cens[i]= 1;
11941: }else if (ageexmed[i]< 1)
11942: cens[i]= -1;
11943: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
11944: cens[i]=0 ;
1.126 brouard 11945: }
11946: else cens[i]=-1;
11947: }
11948:
11949: for (i=1;i<=NDIM;i++) {
11950: for (j=1;j<=NDIM;j++)
1.226 brouard 11951: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 11952: }
11953:
1.302 brouard 11954: p[1]=0.0268; p[NDIM]=0.083;
11955: /* printf("%lf %lf", p[1], p[2]); */
1.126 brouard 11956:
11957:
1.136 brouard 11958: #ifdef GSL
11959: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 11960: #else
1.126 brouard 11961: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 11962: #endif
1.201 brouard 11963: strcpy(filerespow,"POW-MORT_");
11964: strcat(filerespow,fileresu);
1.126 brouard 11965: if((ficrespow=fopen(filerespow,"w"))==NULL) {
11966: printf("Problem with resultfile: %s\n", filerespow);
11967: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
11968: }
1.136 brouard 11969: #ifdef GSL
11970: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 11971: #else
1.126 brouard 11972: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 11973: #endif
1.126 brouard 11974: /* for (i=1;i<=nlstate;i++)
11975: for(j=1;j<=nlstate+ndeath;j++)
11976: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
11977: */
11978: fprintf(ficrespow,"\n");
1.136 brouard 11979: #ifdef GSL
11980: /* gsl starts here */
11981: T = gsl_multimin_fminimizer_nmsimplex;
11982: gsl_multimin_fminimizer *sfm = NULL;
11983: gsl_vector *ss, *x;
11984: gsl_multimin_function minex_func;
11985:
11986: /* Initial vertex size vector */
11987: ss = gsl_vector_alloc (NDIM);
11988:
11989: if (ss == NULL){
11990: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
11991: }
11992: /* Set all step sizes to 1 */
11993: gsl_vector_set_all (ss, 0.001);
11994:
11995: /* Starting point */
1.126 brouard 11996:
1.136 brouard 11997: x = gsl_vector_alloc (NDIM);
11998:
11999: if (x == NULL){
12000: gsl_vector_free(ss);
12001: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
12002: }
12003:
12004: /* Initialize method and iterate */
12005: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 12006: /* gsl_vector_set(x, 0, 0.0268); */
12007: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 12008: gsl_vector_set(x, 0, p[1]);
12009: gsl_vector_set(x, 1, p[2]);
12010:
12011: minex_func.f = &gompertz_f;
12012: minex_func.n = NDIM;
12013: minex_func.params = (void *)&p; /* ??? */
12014:
12015: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
12016: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
12017:
12018: printf("Iterations beginning .....\n\n");
12019: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
12020:
12021: iteri=0;
12022: while (rval == GSL_CONTINUE){
12023: iteri++;
12024: status = gsl_multimin_fminimizer_iterate(sfm);
12025:
12026: if (status) printf("error: %s\n", gsl_strerror (status));
12027: fflush(0);
12028:
12029: if (status)
12030: break;
12031:
12032: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
12033: ssval = gsl_multimin_fminimizer_size (sfm);
12034:
12035: if (rval == GSL_SUCCESS)
12036: printf ("converged to a local maximum at\n");
12037:
12038: printf("%5d ", iteri);
12039: for (it = 0; it < NDIM; it++){
12040: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
12041: }
12042: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
12043: }
12044:
12045: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
12046:
12047: gsl_vector_free(x); /* initial values */
12048: gsl_vector_free(ss); /* inital step size */
12049: for (it=0; it<NDIM; it++){
12050: p[it+1]=gsl_vector_get(sfm->x,it);
12051: fprintf(ficrespow," %.12lf", p[it]);
12052: }
12053: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
12054: #endif
12055: #ifdef POWELL
12056: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
12057: #endif
1.126 brouard 12058: fclose(ficrespow);
12059:
1.203 brouard 12060: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 12061:
12062: for(i=1; i <=NDIM; i++)
12063: for(j=i+1;j<=NDIM;j++)
1.220 brouard 12064: matcov[i][j]=matcov[j][i];
1.126 brouard 12065:
12066: printf("\nCovariance matrix\n ");
1.203 brouard 12067: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 12068: for(i=1; i <=NDIM; i++) {
12069: for(j=1;j<=NDIM;j++){
1.220 brouard 12070: printf("%f ",matcov[i][j]);
12071: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 12072: }
1.203 brouard 12073: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 12074: }
12075:
12076: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 12077: for (i=1;i<=NDIM;i++) {
1.126 brouard 12078: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 12079: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
12080: }
1.302 brouard 12081: lsurv=vector(agegomp,AGESUP);
12082: lpop=vector(agegomp,AGESUP);
12083: tpop=vector(agegomp,AGESUP);
1.126 brouard 12084: lsurv[agegomp]=100000;
12085:
12086: for (k=agegomp;k<=AGESUP;k++) {
12087: agemortsup=k;
12088: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
12089: }
12090:
12091: for (k=agegomp;k<agemortsup;k++)
12092: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
12093:
12094: for (k=agegomp;k<agemortsup;k++){
12095: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
12096: sumlpop=sumlpop+lpop[k];
12097: }
12098:
12099: tpop[agegomp]=sumlpop;
12100: for (k=agegomp;k<(agemortsup-3);k++){
12101: /* tpop[k+1]=2;*/
12102: tpop[k+1]=tpop[k]-lpop[k];
12103: }
12104:
12105:
12106: printf("\nAge lx qx dx Lx Tx e(x)\n");
12107: for (k=agegomp;k<(agemortsup-2);k++)
12108: 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]);
12109:
12110:
12111: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 12112: ageminpar=50;
12113: agemaxpar=100;
1.194 brouard 12114: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
12115: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12116: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12117: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
12118: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12119: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12120: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 12121: }else{
12122: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
12123: 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 12124: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 12125: }
1.201 brouard 12126: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 12127: stepm, weightopt,\
12128: model,imx,p,matcov,agemortsup);
12129:
1.302 brouard 12130: free_vector(lsurv,agegomp,AGESUP);
12131: free_vector(lpop,agegomp,AGESUP);
12132: free_vector(tpop,agegomp,AGESUP);
1.220 brouard 12133: free_matrix(ximort,1,NDIM,1,NDIM);
1.290 brouard 12134: free_ivector(dcwave,firstobs,lastobs);
12135: free_vector(agecens,firstobs,lastobs);
12136: free_vector(ageexmed,firstobs,lastobs);
12137: free_ivector(cens,firstobs,lastobs);
1.220 brouard 12138: #ifdef GSL
1.136 brouard 12139: #endif
1.186 brouard 12140: } /* Endof if mle==-3 mortality only */
1.205 brouard 12141: /* Standard */
12142: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
12143: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12144: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 12145: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 12146: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12147: for (k=1; k<=npar;k++)
12148: printf(" %d %8.5f",k,p[k]);
12149: printf("\n");
1.205 brouard 12150: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
12151: /* mlikeli uses func not funcone */
1.247 brouard 12152: /* for(i=1;i<nlstate;i++){ */
12153: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
12154: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
12155: /* } */
1.205 brouard 12156: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
12157: }
12158: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
12159: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12160: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
12161: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12162: }
12163: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 12164: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12165: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12166: for (k=1; k<=npar;k++)
12167: printf(" %d %8.5f",k,p[k]);
12168: printf("\n");
12169:
12170: /*--------- results files --------------*/
1.283 brouard 12171: /* 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 12172:
12173:
12174: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12175: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12176: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12177: for(i=1,jk=1; i <=nlstate; i++){
12178: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 12179: if (k != i) {
12180: printf("%d%d ",i,k);
12181: fprintf(ficlog,"%d%d ",i,k);
12182: fprintf(ficres,"%1d%1d ",i,k);
12183: for(j=1; j <=ncovmodel; j++){
12184: printf("%12.7f ",p[jk]);
12185: fprintf(ficlog,"%12.7f ",p[jk]);
12186: fprintf(ficres,"%12.7f ",p[jk]);
12187: jk++;
12188: }
12189: printf("\n");
12190: fprintf(ficlog,"\n");
12191: fprintf(ficres,"\n");
12192: }
1.126 brouard 12193: }
12194: }
1.203 brouard 12195: if(mle != 0){
12196: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 12197: ftolhess=ftol; /* Usually correct */
1.203 brouard 12198: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
12199: 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");
12200: 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");
12201: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 12202: for(k=1; k <=(nlstate+ndeath); k++){
12203: if (k != i) {
12204: printf("%d%d ",i,k);
12205: fprintf(ficlog,"%d%d ",i,k);
12206: for(j=1; j <=ncovmodel; j++){
12207: 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]));
12208: 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]));
12209: jk++;
12210: }
12211: printf("\n");
12212: fprintf(ficlog,"\n");
12213: }
12214: }
1.193 brouard 12215: }
1.203 brouard 12216: } /* end of hesscov and Wald tests */
1.225 brouard 12217:
1.203 brouard 12218: /* */
1.126 brouard 12219: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
12220: printf("# Scales (for hessian or gradient estimation)\n");
12221: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
12222: for(i=1,jk=1; i <=nlstate; i++){
12223: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 12224: if (j!=i) {
12225: fprintf(ficres,"%1d%1d",i,j);
12226: printf("%1d%1d",i,j);
12227: fprintf(ficlog,"%1d%1d",i,j);
12228: for(k=1; k<=ncovmodel;k++){
12229: printf(" %.5e",delti[jk]);
12230: fprintf(ficlog," %.5e",delti[jk]);
12231: fprintf(ficres," %.5e",delti[jk]);
12232: jk++;
12233: }
12234: printf("\n");
12235: fprintf(ficlog,"\n");
12236: fprintf(ficres,"\n");
12237: }
1.126 brouard 12238: }
12239: }
12240:
12241: 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 12242: if(mle >= 1) /* To big for the screen */
1.126 brouard 12243: 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");
12244: 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");
12245: /* # 121 Var(a12)\n\ */
12246: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12247: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12248: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12249: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12250: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12251: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12252: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12253:
12254:
12255: /* Just to have a covariance matrix which will be more understandable
12256: even is we still don't want to manage dictionary of variables
12257: */
12258: for(itimes=1;itimes<=2;itimes++){
12259: jj=0;
12260: for(i=1; i <=nlstate; i++){
1.225 brouard 12261: for(j=1; j <=nlstate+ndeath; j++){
12262: if(j==i) continue;
12263: for(k=1; k<=ncovmodel;k++){
12264: jj++;
12265: ca[0]= k+'a'-1;ca[1]='\0';
12266: if(itimes==1){
12267: if(mle>=1)
12268: printf("#%1d%1d%d",i,j,k);
12269: fprintf(ficlog,"#%1d%1d%d",i,j,k);
12270: fprintf(ficres,"#%1d%1d%d",i,j,k);
12271: }else{
12272: if(mle>=1)
12273: printf("%1d%1d%d",i,j,k);
12274: fprintf(ficlog,"%1d%1d%d",i,j,k);
12275: fprintf(ficres,"%1d%1d%d",i,j,k);
12276: }
12277: ll=0;
12278: for(li=1;li <=nlstate; li++){
12279: for(lj=1;lj <=nlstate+ndeath; lj++){
12280: if(lj==li) continue;
12281: for(lk=1;lk<=ncovmodel;lk++){
12282: ll++;
12283: if(ll<=jj){
12284: cb[0]= lk +'a'-1;cb[1]='\0';
12285: if(ll<jj){
12286: if(itimes==1){
12287: if(mle>=1)
12288: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12289: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12290: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12291: }else{
12292: if(mle>=1)
12293: printf(" %.5e",matcov[jj][ll]);
12294: fprintf(ficlog," %.5e",matcov[jj][ll]);
12295: fprintf(ficres," %.5e",matcov[jj][ll]);
12296: }
12297: }else{
12298: if(itimes==1){
12299: if(mle>=1)
12300: printf(" Var(%s%1d%1d)",ca,i,j);
12301: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
12302: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
12303: }else{
12304: if(mle>=1)
12305: printf(" %.7e",matcov[jj][ll]);
12306: fprintf(ficlog," %.7e",matcov[jj][ll]);
12307: fprintf(ficres," %.7e",matcov[jj][ll]);
12308: }
12309: }
12310: }
12311: } /* end lk */
12312: } /* end lj */
12313: } /* end li */
12314: if(mle>=1)
12315: printf("\n");
12316: fprintf(ficlog,"\n");
12317: fprintf(ficres,"\n");
12318: numlinepar++;
12319: } /* end k*/
12320: } /*end j */
1.126 brouard 12321: } /* end i */
12322: } /* end itimes */
12323:
12324: fflush(ficlog);
12325: fflush(ficres);
1.225 brouard 12326: while(fgets(line, MAXLINE, ficpar)) {
12327: /* If line starts with a # it is a comment */
12328: if (line[0] == '#') {
12329: numlinepar++;
12330: fputs(line,stdout);
12331: fputs(line,ficparo);
12332: fputs(line,ficlog);
1.299 brouard 12333: fputs(line,ficres);
1.225 brouard 12334: continue;
12335: }else
12336: break;
12337: }
12338:
1.209 brouard 12339: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
12340: /* ungetc(c,ficpar); */
12341: /* fgets(line, MAXLINE, ficpar); */
12342: /* fputs(line,stdout); */
12343: /* fputs(line,ficparo); */
12344: /* } */
12345: /* ungetc(c,ficpar); */
1.126 brouard 12346:
12347: estepm=0;
1.209 brouard 12348: 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 12349:
12350: if (num_filled != 6) {
12351: 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);
12352: 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);
12353: goto end;
12354: }
12355: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
12356: }
12357: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
12358: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
12359:
1.209 brouard 12360: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 12361: if (estepm==0 || estepm < stepm) estepm=stepm;
12362: if (fage <= 2) {
12363: bage = ageminpar;
12364: fage = agemaxpar;
12365: }
12366:
12367: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 12368: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
12369: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 12370:
1.186 brouard 12371: /* Other stuffs, more or less useful */
1.254 brouard 12372: while(fgets(line, MAXLINE, ficpar)) {
12373: /* If line starts with a # it is a comment */
12374: if (line[0] == '#') {
12375: numlinepar++;
12376: fputs(line,stdout);
12377: fputs(line,ficparo);
12378: fputs(line,ficlog);
1.299 brouard 12379: fputs(line,ficres);
1.254 brouard 12380: continue;
12381: }else
12382: break;
12383: }
12384:
12385: 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){
12386:
12387: if (num_filled != 7) {
12388: 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);
12389: 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);
12390: goto end;
12391: }
12392: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
12393: 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);
12394: 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);
12395: 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 12396: }
1.254 brouard 12397:
12398: while(fgets(line, MAXLINE, ficpar)) {
12399: /* If line starts with a # it is a comment */
12400: if (line[0] == '#') {
12401: numlinepar++;
12402: fputs(line,stdout);
12403: fputs(line,ficparo);
12404: fputs(line,ficlog);
1.299 brouard 12405: fputs(line,ficres);
1.254 brouard 12406: continue;
12407: }else
12408: break;
1.126 brouard 12409: }
12410:
12411:
12412: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
12413: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
12414:
1.254 brouard 12415: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
12416: if (num_filled != 1) {
12417: 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);
12418: 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);
12419: goto end;
12420: }
12421: printf("pop_based=%d\n",popbased);
12422: fprintf(ficlog,"pop_based=%d\n",popbased);
12423: fprintf(ficparo,"pop_based=%d\n",popbased);
12424: fprintf(ficres,"pop_based=%d\n",popbased);
12425: }
12426:
1.258 brouard 12427: /* Results */
1.307 brouard 12428: endishere=0;
1.258 brouard 12429: nresult=0;
1.308 brouard 12430: parameterline=0;
1.258 brouard 12431: do{
12432: if(!fgets(line, MAXLINE, ficpar)){
12433: endishere=1;
1.308 brouard 12434: parameterline=15;
1.258 brouard 12435: }else if (line[0] == '#') {
12436: /* If line starts with a # it is a comment */
1.254 brouard 12437: numlinepar++;
12438: fputs(line,stdout);
12439: fputs(line,ficparo);
12440: fputs(line,ficlog);
1.299 brouard 12441: fputs(line,ficres);
1.254 brouard 12442: continue;
1.258 brouard 12443: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
12444: parameterline=11;
1.296 brouard 12445: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
1.258 brouard 12446: parameterline=12;
1.307 brouard 12447: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
1.258 brouard 12448: parameterline=13;
1.307 brouard 12449: }
1.258 brouard 12450: else{
12451: parameterline=14;
1.254 brouard 12452: }
1.308 brouard 12453: switch (parameterline){ /* =0 only if only comments */
1.258 brouard 12454: case 11:
1.296 brouard 12455: 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)){
12456: 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 12457: 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);
12458: 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);
12459: 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);
12460: /* day and month of proj2 are not used but only year anproj2.*/
1.273 brouard 12461: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
12462: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
1.296 brouard 12463: prvforecast = 1;
12464: }
12465: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
1.302 brouard 12466: printf("prevforecast=%d yearsfproj=%lf.2 mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12467: fprintf(ficlog,"prevforecast=%d yearsfproj=%lf.2 mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12468: fprintf(ficres,"prevforecast=%d yearsfproj=%lf.2 mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
1.296 brouard 12469: prvforecast = 2;
12470: }
12471: else {
12472: 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);
12473: 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);
12474: goto end;
1.258 brouard 12475: }
1.254 brouard 12476: break;
1.258 brouard 12477: case 12:
1.296 brouard 12478: 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)){
12479: 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);
12480: 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);
12481: 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);
12482: 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);
12483: /* day and month of back2 are not used but only year anback2.*/
1.273 brouard 12484: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
12485: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
1.296 brouard 12486: prvbackcast = 1;
12487: }
12488: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
1.302 brouard 12489: printf("prevbackcast=%d yearsbproj=%lf.2 mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12490: fprintf(ficlog,"prevbackcast=%d yearsbproj=%lf.2 mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12491: fprintf(ficres,"prevbackcast=%d yearsbproj=%lf.2 mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
1.296 brouard 12492: prvbackcast = 2;
12493: }
12494: else {
12495: 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);
12496: 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);
12497: goto end;
1.258 brouard 12498: }
1.230 brouard 12499: break;
1.258 brouard 12500: case 13:
1.307 brouard 12501: num_filled=sscanf(line,"result:%[^\n]\n",resultline);
12502: nresult++; /* Sum of resultlines */
12503: printf("Result %d: result:%s\n",nresult, resultline);
12504: if(nresult > MAXRESULTLINES){
12505: 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);
12506: 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);
12507: goto end;
12508: }
12509: decoderesult(resultline, nresult); /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
12510: fprintf(ficparo,"result: %s\n",resultline);
12511: fprintf(ficres,"result: %s\n",resultline);
12512: fprintf(ficlog,"result: %s\n",resultline);
12513: break;
12514: case 14:
12515: printf("Error: Unknown command '%s'\n",line);
12516: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
12517: if(ncovmodel >=2 && nresult==0 ){
12518: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
12519: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
1.258 brouard 12520: }
1.307 brouard 12521: /* goto end; */
12522: break;
1.308 brouard 12523: case 15:
12524: printf("End of resultlines.\n");
12525: fprintf(ficlog,"End of resultlines.\n");
12526: break;
12527: default: /* parameterline =0 */
1.307 brouard 12528: nresult=1;
12529: decoderesult(".",nresult ); /* No covariate */
1.258 brouard 12530: } /* End switch parameterline */
12531: }while(endishere==0); /* End do */
1.126 brouard 12532:
1.230 brouard 12533: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 12534: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 12535:
12536: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 12537: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 12538: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 12539: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12540: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 12541: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 12542: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12543: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 12544: }else{
1.270 brouard 12545: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
1.296 brouard 12546: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
12547: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
12548: if(prvforecast==1){
12549: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
12550: jprojd=jproj1;
12551: mprojd=mproj1;
12552: anprojd=anproj1;
12553: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
12554: jprojf=jproj2;
12555: mprojf=mproj2;
12556: anprojf=anproj2;
12557: } else if(prvforecast == 2){
12558: dateprojd=dateintmean;
12559: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
12560: dateprojf=dateintmean+yrfproj;
12561: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
12562: }
12563: if(prvbackcast==1){
12564: datebackd=(jback1+12*mback1+365*anback1)/365;
12565: jbackd=jback1;
12566: mbackd=mback1;
12567: anbackd=anback1;
12568: datebackf=(jback2+12*mback2+365*anback2)/365;
12569: jbackf=jback2;
12570: mbackf=mback2;
12571: anbackf=anback2;
12572: } else if(prvbackcast == 2){
12573: datebackd=dateintmean;
12574: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
12575: datebackf=dateintmean-yrbproj;
12576: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
12577: }
12578:
12579: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);
1.220 brouard 12580: }
12581: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.296 brouard 12582: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
12583: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
1.220 brouard 12584:
1.225 brouard 12585: /*------------ free_vector -------------*/
12586: /* chdir(path); */
1.220 brouard 12587:
1.215 brouard 12588: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
12589: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
12590: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
12591: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.290 brouard 12592: free_lvector(num,firstobs,lastobs);
12593: free_vector(agedc,firstobs,lastobs);
1.126 brouard 12594: /*free_matrix(covar,0,NCOVMAX,1,n);*/
12595: /*free_matrix(covar,1,NCOVMAX,1,n);*/
12596: fclose(ficparo);
12597: fclose(ficres);
1.220 brouard 12598:
12599:
1.186 brouard 12600: /* Other results (useful)*/
1.220 brouard 12601:
12602:
1.126 brouard 12603: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 12604: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
12605: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 12606: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 12607: fclose(ficrespl);
12608:
12609: /*------------- h Pij x at various ages ------------*/
1.180 brouard 12610: /*#include "hpijx.h"*/
12611: hPijx(p, bage, fage);
1.145 brouard 12612: fclose(ficrespij);
1.227 brouard 12613:
1.220 brouard 12614: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 12615: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 12616: k=1;
1.126 brouard 12617: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 12618:
1.269 brouard 12619: /* Prevalence for each covariate combination in probs[age][status][cov] */
12620: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
12621: for(i=AGEINF;i<=AGESUP;i++)
1.219 brouard 12622: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 12623: for(k=1;k<=ncovcombmax;k++)
12624: probs[i][j][k]=0.;
1.269 brouard 12625: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
12626: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.219 brouard 12627: if (mobilav!=0 ||mobilavproj !=0 ) {
1.269 brouard 12628: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
12629: for(i=AGEINF;i<=AGESUP;i++)
1.268 brouard 12630: for(j=1;j<=nlstate+ndeath;j++)
1.227 brouard 12631: for(k=1;k<=ncovcombmax;k++)
12632: mobaverages[i][j][k]=0.;
1.219 brouard 12633: mobaverage=mobaverages;
12634: if (mobilav!=0) {
1.235 brouard 12635: printf("Movingaveraging observed prevalence\n");
1.258 brouard 12636: fprintf(ficlog,"Movingaveraging observed prevalence\n");
1.227 brouard 12637: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
12638: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
12639: printf(" Error in movingaverage mobilav=%d\n",mobilav);
12640: }
1.269 brouard 12641: } else if (mobilavproj !=0) {
1.235 brouard 12642: printf("Movingaveraging projected observed prevalence\n");
1.258 brouard 12643: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
1.227 brouard 12644: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
12645: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
12646: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
12647: }
1.269 brouard 12648: }else{
12649: printf("Internal error moving average\n");
12650: fflush(stdout);
12651: exit(1);
1.219 brouard 12652: }
12653: }/* end if moving average */
1.227 brouard 12654:
1.126 brouard 12655: /*---------- Forecasting ------------------*/
1.296 brouard 12656: if(prevfcast==1){
12657: /* /\* if(stepm ==1){*\/ */
12658: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
12659: /*This done previously after freqsummary.*/
12660: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
12661: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
12662:
12663: /* } else if (prvforecast==2){ */
12664: /* /\* if(stepm ==1){*\/ */
12665: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
12666: /* } */
12667: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
12668: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
1.126 brouard 12669: }
1.269 brouard 12670:
1.296 brouard 12671: /* Prevbcasting */
12672: if(prevbcast==1){
1.219 brouard 12673: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12674: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12675: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12676:
12677: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
12678:
12679: bprlim=matrix(1,nlstate,1,nlstate);
1.269 brouard 12680:
1.219 brouard 12681: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
12682: fclose(ficresplb);
12683:
1.222 brouard 12684: hBijx(p, bage, fage, mobaverage);
12685: fclose(ficrespijb);
1.219 brouard 12686:
1.296 brouard 12687: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
12688: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
12689: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
12690: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
12691: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
12692: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
12693:
12694:
1.269 brouard 12695: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 12696:
12697:
1.269 brouard 12698: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.219 brouard 12699: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
12700: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
12701: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
1.296 brouard 12702: } /* end Prevbcasting */
1.268 brouard 12703:
1.186 brouard 12704:
12705: /* ------ Other prevalence ratios------------ */
1.126 brouard 12706:
1.215 brouard 12707: free_ivector(wav,1,imx);
12708: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
12709: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
12710: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 12711:
12712:
1.127 brouard 12713: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 12714:
1.201 brouard 12715: strcpy(filerese,"E_");
12716: strcat(filerese,fileresu);
1.126 brouard 12717: if((ficreseij=fopen(filerese,"w"))==NULL) {
12718: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
12719: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
12720: }
1.208 brouard 12721: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
12722: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 12723:
12724: pstamp(ficreseij);
1.219 brouard 12725:
1.235 brouard 12726: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
12727: if (cptcovn < 1){i1=1;}
12728:
12729: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12730: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 12731: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 12732: continue;
1.219 brouard 12733: fprintf(ficreseij,"\n#****** ");
1.235 brouard 12734: printf("\n#****** ");
1.225 brouard 12735: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 12736: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 12737: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12738: }
12739: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12740: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12741: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
1.219 brouard 12742: }
12743: fprintf(ficreseij,"******\n");
1.235 brouard 12744: printf("******\n");
1.219 brouard 12745:
12746: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12747: oldm=oldms;savm=savms;
1.235 brouard 12748: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 12749:
1.219 brouard 12750: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 12751: }
12752: fclose(ficreseij);
1.208 brouard 12753: printf("done evsij\n");fflush(stdout);
12754: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.269 brouard 12755:
1.218 brouard 12756:
1.227 brouard 12757: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 12758:
1.201 brouard 12759: strcpy(filerest,"T_");
12760: strcat(filerest,fileresu);
1.127 brouard 12761: if((ficrest=fopen(filerest,"w"))==NULL) {
12762: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
12763: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
12764: }
1.208 brouard 12765: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
12766: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.201 brouard 12767: strcpy(fileresstde,"STDE_");
12768: strcat(fileresstde,fileresu);
1.126 brouard 12769: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 12770: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
12771: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 12772: }
1.227 brouard 12773: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
12774: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 12775:
1.201 brouard 12776: strcpy(filerescve,"CVE_");
12777: strcat(filerescve,fileresu);
1.126 brouard 12778: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 12779: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
12780: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 12781: }
1.227 brouard 12782: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
12783: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 12784:
1.201 brouard 12785: strcpy(fileresv,"V_");
12786: strcat(fileresv,fileresu);
1.126 brouard 12787: if((ficresvij=fopen(fileresv,"w"))==NULL) {
12788: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
12789: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
12790: }
1.227 brouard 12791: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
12792: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 12793:
1.235 brouard 12794: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
12795: if (cptcovn < 1){i1=1;}
12796:
12797: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12798: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 12799: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 12800: continue;
1.242 brouard 12801: printf("\n#****** Result for:");
12802: fprintf(ficrest,"\n#****** Result for:");
12803: fprintf(ficlog,"\n#****** Result for:");
1.227 brouard 12804: for(j=1;j<=cptcoveff;j++){
12805: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12806: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12807: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12808: }
1.235 brouard 12809: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12810: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12811: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12812: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12813: }
1.208 brouard 12814: fprintf(ficrest,"******\n");
1.227 brouard 12815: fprintf(ficlog,"******\n");
12816: printf("******\n");
1.208 brouard 12817:
12818: fprintf(ficresstdeij,"\n#****** ");
12819: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 12820: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 12821: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12822: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 12823: }
1.235 brouard 12824: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12825: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12826: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12827: }
1.208 brouard 12828: fprintf(ficresstdeij,"******\n");
12829: fprintf(ficrescveij,"******\n");
12830:
12831: fprintf(ficresvij,"\n#****** ");
1.238 brouard 12832: /* pstamp(ficresvij); */
1.225 brouard 12833: for(j=1;j<=cptcoveff;j++)
1.227 brouard 12834: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 12835: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12836: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12837: }
1.208 brouard 12838: fprintf(ficresvij,"******\n");
12839:
12840: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12841: oldm=oldms;savm=savms;
1.235 brouard 12842: printf(" cvevsij ");
12843: fprintf(ficlog, " cvevsij ");
12844: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 12845: printf(" end cvevsij \n ");
12846: fprintf(ficlog, " end cvevsij \n ");
12847:
12848: /*
12849: */
12850: /* goto endfree; */
12851:
12852: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12853: pstamp(ficrest);
12854:
1.269 brouard 12855: epj=vector(1,nlstate+1);
1.208 brouard 12856: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 12857: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
12858: cptcod= 0; /* To be deleted */
12859: printf("varevsij vpopbased=%d \n",vpopbased);
12860: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 12861: 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 12862: 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 ");
12863: if(vpopbased==1)
12864: 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);
12865: else
1.288 brouard 12866: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 12867: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
12868: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
12869: fprintf(ficrest,"\n");
12870: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
1.288 brouard 12871: printf("Computing age specific forward period (stable) prevalences in each health state \n");
12872: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 12873: for(age=bage; age <=fage ;age++){
1.235 brouard 12874: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 12875: if (vpopbased==1) {
12876: if(mobilav ==0){
12877: for(i=1; i<=nlstate;i++)
12878: prlim[i][i]=probs[(int)age][i][k];
12879: }else{ /* mobilav */
12880: for(i=1; i<=nlstate;i++)
12881: prlim[i][i]=mobaverage[(int)age][i][k];
12882: }
12883: }
1.219 brouard 12884:
1.227 brouard 12885: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
12886: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
12887: /* printf(" age %4.0f ",age); */
12888: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
12889: for(i=1, epj[j]=0.;i <=nlstate;i++) {
12890: epj[j] += prlim[i][i]*eij[i][j][(int)age];
12891: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
12892: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
12893: }
12894: epj[nlstate+1] +=epj[j];
12895: }
12896: /* printf(" age %4.0f \n",age); */
1.219 brouard 12897:
1.227 brouard 12898: for(i=1, vepp=0.;i <=nlstate;i++)
12899: for(j=1;j <=nlstate;j++)
12900: vepp += vareij[i][j][(int)age];
12901: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
12902: for(j=1;j <=nlstate;j++){
12903: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
12904: }
12905: fprintf(ficrest,"\n");
12906: }
1.208 brouard 12907: } /* End vpopbased */
1.269 brouard 12908: free_vector(epj,1,nlstate+1);
1.208 brouard 12909: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
12910: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.235 brouard 12911: printf("done selection\n");fflush(stdout);
12912: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 12913:
1.235 brouard 12914: } /* End k selection */
1.227 brouard 12915:
12916: printf("done State-specific expectancies\n");fflush(stdout);
12917: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
12918:
1.288 brouard 12919: /* variance-covariance of forward period prevalence*/
1.269 brouard 12920: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 12921:
1.227 brouard 12922:
1.290 brouard 12923: free_vector(weight,firstobs,lastobs);
1.227 brouard 12924: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.290 brouard 12925: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
12926: free_matrix(anint,1,maxwav,firstobs,lastobs);
12927: free_matrix(mint,1,maxwav,firstobs,lastobs);
12928: free_ivector(cod,firstobs,lastobs);
1.227 brouard 12929: free_ivector(tab,1,NCOVMAX);
12930: fclose(ficresstdeij);
12931: fclose(ficrescveij);
12932: fclose(ficresvij);
12933: fclose(ficrest);
12934: fclose(ficpar);
12935:
12936:
1.126 brouard 12937: /*---------- End : free ----------------*/
1.219 brouard 12938: if (mobilav!=0 ||mobilavproj !=0)
1.269 brouard 12939: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
12940: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 12941: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
12942: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 12943: } /* mle==-3 arrives here for freeing */
1.227 brouard 12944: /* endfree:*/
12945: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
12946: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
12947: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.290 brouard 12948: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs);
12949: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
12950: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
12951: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
1.227 brouard 12952: free_matrix(matcov,1,npar,1,npar);
12953: free_matrix(hess,1,npar,1,npar);
12954: /*free_vector(delti,1,npar);*/
12955: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
12956: free_matrix(agev,1,maxwav,1,imx);
1.269 brouard 12957: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.227 brouard 12958: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
12959:
12960: free_ivector(ncodemax,1,NCOVMAX);
12961: free_ivector(ncodemaxwundef,1,NCOVMAX);
12962: free_ivector(Dummy,-1,NCOVMAX);
12963: free_ivector(Fixed,-1,NCOVMAX);
1.238 brouard 12964: free_ivector(DummyV,1,NCOVMAX);
12965: free_ivector(FixedV,1,NCOVMAX);
1.227 brouard 12966: free_ivector(Typevar,-1,NCOVMAX);
12967: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 12968: free_ivector(TvarsQ,1,NCOVMAX);
12969: free_ivector(TvarsQind,1,NCOVMAX);
12970: free_ivector(TvarsD,1,NCOVMAX);
12971: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 12972: free_ivector(TvarFD,1,NCOVMAX);
12973: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 12974: free_ivector(TvarF,1,NCOVMAX);
12975: free_ivector(TvarFind,1,NCOVMAX);
12976: free_ivector(TvarV,1,NCOVMAX);
12977: free_ivector(TvarVind,1,NCOVMAX);
12978: free_ivector(TvarA,1,NCOVMAX);
12979: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 12980: free_ivector(TvarFQ,1,NCOVMAX);
12981: free_ivector(TvarFQind,1,NCOVMAX);
12982: free_ivector(TvarVD,1,NCOVMAX);
12983: free_ivector(TvarVDind,1,NCOVMAX);
12984: free_ivector(TvarVQ,1,NCOVMAX);
12985: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 12986: free_ivector(Tvarsel,1,NCOVMAX);
12987: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 12988: free_ivector(Tposprod,1,NCOVMAX);
12989: free_ivector(Tprod,1,NCOVMAX);
12990: free_ivector(Tvaraff,1,NCOVMAX);
12991: free_ivector(invalidvarcomb,1,ncovcombmax);
12992: free_ivector(Tage,1,NCOVMAX);
12993: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 12994: free_ivector(TmodelInvind,1,NCOVMAX);
12995: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 12996:
12997: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
12998: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 12999: fflush(fichtm);
13000: fflush(ficgp);
13001:
1.227 brouard 13002:
1.126 brouard 13003: if((nberr >0) || (nbwarn>0)){
1.216 brouard 13004: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
13005: 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 13006: }else{
13007: printf("End of Imach\n");
13008: fprintf(ficlog,"End of Imach\n");
13009: }
13010: printf("See log file on %s\n",filelog);
13011: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 13012: /*(void) gettimeofday(&end_time,&tzp);*/
13013: rend_time = time(NULL);
13014: end_time = *localtime(&rend_time);
13015: /* tml = *localtime(&end_time.tm_sec); */
13016: strcpy(strtend,asctime(&end_time));
1.126 brouard 13017: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
13018: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 13019: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 13020:
1.157 brouard 13021: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
13022: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
13023: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 13024: /* printf("Total time was %d uSec.\n", total_usecs);*/
13025: /* if(fileappend(fichtm,optionfilehtm)){ */
13026: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13027: fclose(fichtm);
13028: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13029: fclose(fichtmcov);
13030: fclose(ficgp);
13031: fclose(ficlog);
13032: /*------ End -----------*/
1.227 brouard 13033:
1.281 brouard 13034:
13035: /* Executes gnuplot */
1.227 brouard 13036:
13037: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 13038: #ifdef WIN32
1.227 brouard 13039: if (_chdir(pathcd) != 0)
13040: printf("Can't move to directory %s!\n",path);
13041: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 13042: #else
1.227 brouard 13043: if(chdir(pathcd) != 0)
13044: printf("Can't move to directory %s!\n", path);
13045: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 13046: #endif
1.126 brouard 13047: printf("Current directory %s!\n",pathcd);
13048: /*strcat(plotcmd,CHARSEPARATOR);*/
13049: sprintf(plotcmd,"gnuplot");
1.157 brouard 13050: #ifdef _WIN32
1.126 brouard 13051: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
13052: #endif
13053: if(!stat(plotcmd,&info)){
1.158 brouard 13054: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13055: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 13056: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 13057: }else
13058: strcpy(pplotcmd,plotcmd);
1.157 brouard 13059: #ifdef __unix
1.126 brouard 13060: strcpy(plotcmd,GNUPLOTPROGRAM);
13061: if(!stat(plotcmd,&info)){
1.158 brouard 13062: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13063: }else
13064: strcpy(pplotcmd,plotcmd);
13065: #endif
13066: }else
13067: strcpy(pplotcmd,plotcmd);
13068:
13069: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 13070: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.292 brouard 13071: strcpy(pplotcmd,plotcmd);
1.227 brouard 13072:
1.126 brouard 13073: if((outcmd=system(plotcmd)) != 0){
1.292 brouard 13074: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 13075: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 13076: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.292 brouard 13077: if((outcmd=system(plotcmd)) != 0){
1.153 brouard 13078: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.292 brouard 13079: strcpy(plotcmd,pplotcmd);
13080: }
1.126 brouard 13081: }
1.158 brouard 13082: printf(" Successful, please wait...");
1.126 brouard 13083: while (z[0] != 'q') {
13084: /* chdir(path); */
1.154 brouard 13085: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 13086: scanf("%s",z);
13087: /* if (z[0] == 'c') system("./imach"); */
13088: if (z[0] == 'e') {
1.158 brouard 13089: #ifdef __APPLE__
1.152 brouard 13090: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 13091: #elif __linux
13092: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 13093: #else
1.152 brouard 13094: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 13095: #endif
13096: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
13097: system(pplotcmd);
1.126 brouard 13098: }
13099: else if (z[0] == 'g') system(plotcmd);
13100: else if (z[0] == 'q') exit(0);
13101: }
1.227 brouard 13102: end:
1.126 brouard 13103: while (z[0] != 'q') {
1.195 brouard 13104: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 13105: scanf("%s",z);
13106: }
1.283 brouard 13107: printf("End\n");
1.282 brouard 13108: exit(0);
1.126 brouard 13109: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>