Annotation of imach/src/imach.c, revision 1.311
1.311 ! brouard 1: /* $Id: imach.c,v 1.310 2022/03/17 08:45:53 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.311 ! brouard 4: Revision 1.310 2022/03/17 08:45:53 brouard
! 5: Summary: 99r25
! 6:
! 7: Improving detection of errors: result lines should be compatible with
! 8: the model.
! 9:
1.310 brouard 10: Revision 1.309 2021/05/20 12:39:14 brouard
11: Summary: Version 0.99r24
12:
1.309 brouard 13: Revision 1.308 2021/03/31 13:11:57 brouard
14: Summary: Version 0.99r23
15:
16:
17: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett
18:
1.308 brouard 19: Revision 1.307 2021/03/08 18:11:32 brouard
20: Summary: 0.99r22 fixed bug on result:
21:
1.307 brouard 22: Revision 1.306 2021/02/20 15:44:02 brouard
23: Summary: Version 0.99r21
24:
25: * imach.c (Module): Fix bug on quitting after result lines!
26: (Module): Version 0.99r21
27:
1.306 brouard 28: Revision 1.305 2021/02/20 15:28:30 brouard
29: * imach.c (Module): Fix bug on quitting after result lines!
30:
1.305 brouard 31: Revision 1.304 2021/02/12 11:34:20 brouard
32: * imach.c (Module): The use of a Windows BOM (huge) file is now an error
33:
1.304 brouard 34: Revision 1.303 2021/02/11 19:50:15 brouard
35: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed.
36:
1.303 brouard 37: Revision 1.302 2020/02/22 21:00:05 brouard
38: * (Module): imach.c Update mle=-3 (for computing Life expectancy
39: and life table from the data without any state)
40:
1.302 brouard 41: Revision 1.301 2019/06/04 13:51:20 brouard
42: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj
43:
1.301 brouard 44: Revision 1.300 2019/05/22 19:09:45 brouard
45: Summary: version 0.99r19 of May 2019
46:
1.300 brouard 47: Revision 1.299 2019/05/22 18:37:08 brouard
48: Summary: Cleaned 0.99r19
49:
1.299 brouard 50: Revision 1.298 2019/05/22 18:19:56 brouard
51: *** empty log message ***
52:
1.298 brouard 53: Revision 1.297 2019/05/22 17:56:10 brouard
54: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1
55:
1.297 brouard 56: Revision 1.296 2019/05/20 13:03:18 brouard
57: Summary: Projection syntax simplified
58:
59:
60: We can now start projections, forward or backward, from the mean date
61: of inteviews up to or down to a number of years of projection:
62: prevforecast=1 yearsfproj=15.3 mobil_average=0
63: or
64: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0
65: or
66: prevbackcast=1 yearsbproj=12.3 mobil_average=1
67: or
68: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1
69:
1.296 brouard 70: Revision 1.295 2019/05/18 09:52:50 brouard
71: Summary: doxygen tex bug
72:
1.295 brouard 73: Revision 1.294 2019/05/16 14:54:33 brouard
74: Summary: There was some wrong lines added
75:
1.294 brouard 76: Revision 1.293 2019/05/09 15:17:34 brouard
77: *** empty log message ***
78:
1.293 brouard 79: Revision 1.292 2019/05/09 14:17:20 brouard
80: Summary: Some updates
81:
1.292 brouard 82: Revision 1.291 2019/05/09 13:44:18 brouard
83: Summary: Before ncovmax
84:
1.291 brouard 85: Revision 1.290 2019/05/09 13:39:37 brouard
86: Summary: 0.99r18 unlimited number of individuals
87:
88: 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.
89:
1.290 brouard 90: Revision 1.289 2018/12/13 09:16:26 brouard
91: Summary: Bug for young ages (<-30) will be in r17
92:
1.289 brouard 93: Revision 1.288 2018/05/02 20:58:27 brouard
94: Summary: Some bugs fixed
95:
1.288 brouard 96: Revision 1.287 2018/05/01 17:57:25 brouard
97: Summary: Bug fixed by providing frequencies only for non missing covariates
98:
1.287 brouard 99: Revision 1.286 2018/04/27 14:27:04 brouard
100: Summary: some minor bugs
101:
1.286 brouard 102: Revision 1.285 2018/04/21 21:02:16 brouard
103: Summary: Some bugs fixed, valgrind tested
104:
1.285 brouard 105: Revision 1.284 2018/04/20 05:22:13 brouard
106: Summary: Computing mean and stdeviation of fixed quantitative variables
107:
1.284 brouard 108: Revision 1.283 2018/04/19 14:49:16 brouard
109: Summary: Some minor bugs fixed
110:
1.283 brouard 111: Revision 1.282 2018/02/27 22:50:02 brouard
112: *** empty log message ***
113:
1.282 brouard 114: Revision 1.281 2018/02/27 19:25:23 brouard
115: Summary: Adding second argument for quitting
116:
1.281 brouard 117: Revision 1.280 2018/02/21 07:58:13 brouard
118: Summary: 0.99r15
119:
120: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c
121:
1.280 brouard 122: Revision 1.279 2017/07/20 13:35:01 brouard
123: Summary: temporary working
124:
1.279 brouard 125: Revision 1.278 2017/07/19 14:09:02 brouard
126: Summary: Bug for mobil_average=0 and prevforecast fixed(?)
127:
1.278 brouard 128: Revision 1.277 2017/07/17 08:53:49 brouard
129: Summary: BOM files can be read now
130:
1.277 brouard 131: Revision 1.276 2017/06/30 15:48:31 brouard
132: Summary: Graphs improvements
133:
1.276 brouard 134: Revision 1.275 2017/06/30 13:39:33 brouard
135: Summary: Saito's color
136:
1.275 brouard 137: Revision 1.274 2017/06/29 09:47:08 brouard
138: Summary: Version 0.99r14
139:
1.274 brouard 140: Revision 1.273 2017/06/27 11:06:02 brouard
141: Summary: More documentation on projections
142:
1.273 brouard 143: Revision 1.272 2017/06/27 10:22:40 brouard
144: Summary: Color of backprojection changed from 6 to 5(yellow)
145:
1.272 brouard 146: Revision 1.271 2017/06/27 10:17:50 brouard
147: Summary: Some bug with rint
148:
1.271 brouard 149: Revision 1.270 2017/05/24 05:45:29 brouard
150: *** empty log message ***
151:
1.270 brouard 152: Revision 1.269 2017/05/23 08:39:25 brouard
153: Summary: Code into subroutine, cleanings
154:
1.269 brouard 155: Revision 1.268 2017/05/18 20:09:32 brouard
156: Summary: backprojection and confidence intervals of backprevalence
157:
1.268 brouard 158: Revision 1.267 2017/05/13 10:25:05 brouard
159: Summary: temporary save for backprojection
160:
1.267 brouard 161: Revision 1.266 2017/05/13 07:26:12 brouard
162: Summary: Version 0.99r13 (improvements and bugs fixed)
163:
1.266 brouard 164: Revision 1.265 2017/04/26 16:22:11 brouard
165: Summary: imach 0.99r13 Some bugs fixed
166:
1.265 brouard 167: Revision 1.264 2017/04/26 06:01:29 brouard
168: Summary: Labels in graphs
169:
1.264 brouard 170: Revision 1.263 2017/04/24 15:23:15 brouard
171: Summary: to save
172:
1.263 brouard 173: Revision 1.262 2017/04/18 16:48:12 brouard
174: *** empty log message ***
175:
1.262 brouard 176: Revision 1.261 2017/04/05 10:14:09 brouard
177: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1
178:
1.261 brouard 179: Revision 1.260 2017/04/04 17:46:59 brouard
180: Summary: Gnuplot indexations fixed (humm)
181:
1.260 brouard 182: Revision 1.259 2017/04/04 13:01:16 brouard
183: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3
184:
1.259 brouard 185: Revision 1.258 2017/04/03 10:17:47 brouard
186: Summary: Version 0.99r12
187:
188: Some cleanings, conformed with updated documentation.
189:
1.258 brouard 190: Revision 1.257 2017/03/29 16:53:30 brouard
191: Summary: Temp
192:
1.257 brouard 193: Revision 1.256 2017/03/27 05:50:23 brouard
194: Summary: Temporary
195:
1.256 brouard 196: Revision 1.255 2017/03/08 16:02:28 brouard
197: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed
198:
1.255 brouard 199: Revision 1.254 2017/03/08 07:13:00 brouard
200: Summary: Fixing data parameter line
201:
1.254 brouard 202: Revision 1.253 2016/12/15 11:59:41 brouard
203: Summary: 0.99 in progress
204:
1.253 brouard 205: Revision 1.252 2016/09/15 21:15:37 brouard
206: *** empty log message ***
207:
1.252 brouard 208: Revision 1.251 2016/09/15 15:01:13 brouard
209: Summary: not working
210:
1.251 brouard 211: Revision 1.250 2016/09/08 16:07:27 brouard
212: Summary: continue
213:
1.250 brouard 214: Revision 1.249 2016/09/07 17:14:18 brouard
215: Summary: Starting values from frequencies
216:
1.249 brouard 217: Revision 1.248 2016/09/07 14:10:18 brouard
218: *** empty log message ***
219:
1.248 brouard 220: Revision 1.247 2016/09/02 11:11:21 brouard
221: *** empty log message ***
222:
1.247 brouard 223: Revision 1.246 2016/09/02 08:49:22 brouard
224: *** empty log message ***
225:
1.246 brouard 226: Revision 1.245 2016/09/02 07:25:01 brouard
227: *** empty log message ***
228:
1.245 brouard 229: Revision 1.244 2016/09/02 07:17:34 brouard
230: *** empty log message ***
231:
1.244 brouard 232: Revision 1.243 2016/09/02 06:45:35 brouard
233: *** empty log message ***
234:
1.243 brouard 235: Revision 1.242 2016/08/30 15:01:20 brouard
236: Summary: Fixing a lots
237:
1.242 brouard 238: Revision 1.241 2016/08/29 17:17:25 brouard
239: Summary: gnuplot problem in Back projection to fix
240:
1.241 brouard 241: Revision 1.240 2016/08/29 07:53:18 brouard
242: Summary: Better
243:
1.240 brouard 244: Revision 1.239 2016/08/26 15:51:03 brouard
245: Summary: Improvement in Powell output in order to copy and paste
246:
247: Author:
248:
1.239 brouard 249: Revision 1.238 2016/08/26 14:23:35 brouard
250: Summary: Starting tests of 0.99
251:
1.238 brouard 252: Revision 1.237 2016/08/26 09:20:19 brouard
253: Summary: to valgrind
254:
1.237 brouard 255: Revision 1.236 2016/08/25 10:50:18 brouard
256: *** empty log message ***
257:
1.236 brouard 258: Revision 1.235 2016/08/25 06:59:23 brouard
259: *** empty log message ***
260:
1.235 brouard 261: Revision 1.234 2016/08/23 16:51:20 brouard
262: *** empty log message ***
263:
1.234 brouard 264: Revision 1.233 2016/08/23 07:40:50 brouard
265: Summary: not working
266:
1.233 brouard 267: Revision 1.232 2016/08/22 14:20:21 brouard
268: Summary: not working
269:
1.232 brouard 270: Revision 1.231 2016/08/22 07:17:15 brouard
271: Summary: not working
272:
1.231 brouard 273: Revision 1.230 2016/08/22 06:55:53 brouard
274: Summary: Not working
275:
1.230 brouard 276: Revision 1.229 2016/07/23 09:45:53 brouard
277: Summary: Completing for func too
278:
1.229 brouard 279: Revision 1.228 2016/07/22 17:45:30 brouard
280: Summary: Fixing some arrays, still debugging
281:
1.227 brouard 282: Revision 1.226 2016/07/12 18:42:34 brouard
283: Summary: temp
284:
1.226 brouard 285: Revision 1.225 2016/07/12 08:40:03 brouard
286: Summary: saving but not running
287:
1.225 brouard 288: Revision 1.224 2016/07/01 13:16:01 brouard
289: Summary: Fixes
290:
1.224 brouard 291: Revision 1.223 2016/02/19 09:23:35 brouard
292: Summary: temporary
293:
1.223 brouard 294: Revision 1.222 2016/02/17 08:14:50 brouard
295: Summary: Probably last 0.98 stable version 0.98r6
296:
1.222 brouard 297: Revision 1.221 2016/02/15 23:35:36 brouard
298: Summary: minor bug
299:
1.220 brouard 300: Revision 1.219 2016/02/15 00:48:12 brouard
301: *** empty log message ***
302:
1.219 brouard 303: Revision 1.218 2016/02/12 11:29:23 brouard
304: Summary: 0.99 Back projections
305:
1.218 brouard 306: Revision 1.217 2015/12/23 17:18:31 brouard
307: Summary: Experimental backcast
308:
1.217 brouard 309: Revision 1.216 2015/12/18 17:32:11 brouard
310: Summary: 0.98r4 Warning and status=-2
311:
312: Version 0.98r4 is now:
313: - displaying an error when status is -1, date of interview unknown and date of death known;
314: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
315: Older changes concerning s=-2, dating from 2005 have been supersed.
316:
1.216 brouard 317: Revision 1.215 2015/12/16 08:52:24 brouard
318: Summary: 0.98r4 working
319:
1.215 brouard 320: Revision 1.214 2015/12/16 06:57:54 brouard
321: Summary: temporary not working
322:
1.214 brouard 323: Revision 1.213 2015/12/11 18:22:17 brouard
324: Summary: 0.98r4
325:
1.213 brouard 326: Revision 1.212 2015/11/21 12:47:24 brouard
327: Summary: minor typo
328:
1.212 brouard 329: Revision 1.211 2015/11/21 12:41:11 brouard
330: Summary: 0.98r3 with some graph of projected cross-sectional
331:
332: Author: Nicolas Brouard
333:
1.211 brouard 334: Revision 1.210 2015/11/18 17:41:20 brouard
1.252 brouard 335: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard
1.210 brouard 336: Summary: Adding ftolpl parameter
337: Author: N Brouard
338:
339: We had difficulties to get smoothed confidence intervals. It was due
340: to the period prevalence which wasn't computed accurately. The inner
341: parameter ftolpl is now an outer parameter of the .imach parameter
342: file after estepm. If ftolpl is small 1.e-4 and estepm too,
343: computation are long.
344:
1.209 brouard 345: Revision 1.208 2015/11/17 14:31:57 brouard
346: Summary: temporary
347:
1.208 brouard 348: Revision 1.207 2015/10/27 17:36:57 brouard
349: *** empty log message ***
350:
1.207 brouard 351: Revision 1.206 2015/10/24 07:14:11 brouard
352: *** empty log message ***
353:
1.206 brouard 354: Revision 1.205 2015/10/23 15:50:53 brouard
355: Summary: 0.98r3 some clarification for graphs on likelihood contributions
356:
1.205 brouard 357: Revision 1.204 2015/10/01 16:20:26 brouard
358: Summary: Some new graphs of contribution to likelihood
359:
1.204 brouard 360: Revision 1.203 2015/09/30 17:45:14 brouard
361: Summary: looking at better estimation of the hessian
362:
363: Also a better criteria for convergence to the period prevalence And
364: therefore adding the number of years needed to converge. (The
365: prevalence in any alive state shold sum to one
366:
1.203 brouard 367: Revision 1.202 2015/09/22 19:45:16 brouard
368: Summary: Adding some overall graph on contribution to likelihood. Might change
369:
1.202 brouard 370: Revision 1.201 2015/09/15 17:34:58 brouard
371: Summary: 0.98r0
372:
373: - Some new graphs like suvival functions
374: - Some bugs fixed like model=1+age+V2.
375:
1.201 brouard 376: Revision 1.200 2015/09/09 16:53:55 brouard
377: Summary: Big bug thanks to Flavia
378:
379: Even model=1+age+V2. did not work anymore
380:
1.200 brouard 381: Revision 1.199 2015/09/07 14:09:23 brouard
382: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
383:
1.199 brouard 384: Revision 1.198 2015/09/03 07:14:39 brouard
385: Summary: 0.98q5 Flavia
386:
1.198 brouard 387: Revision 1.197 2015/09/01 18:24:39 brouard
388: *** empty log message ***
389:
1.197 brouard 390: Revision 1.196 2015/08/18 23:17:52 brouard
391: Summary: 0.98q5
392:
1.196 brouard 393: Revision 1.195 2015/08/18 16:28:39 brouard
394: Summary: Adding a hack for testing purpose
395:
396: After reading the title, ftol and model lines, if the comment line has
397: a q, starting with #q, the answer at the end of the run is quit. It
398: permits to run test files in batch with ctest. The former workaround was
399: $ echo q | imach foo.imach
400:
1.195 brouard 401: Revision 1.194 2015/08/18 13:32:00 brouard
402: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
403:
1.194 brouard 404: Revision 1.193 2015/08/04 07:17:42 brouard
405: Summary: 0.98q4
406:
1.193 brouard 407: Revision 1.192 2015/07/16 16:49:02 brouard
408: Summary: Fixing some outputs
409:
1.192 brouard 410: Revision 1.191 2015/07/14 10:00:33 brouard
411: Summary: Some fixes
412:
1.191 brouard 413: Revision 1.190 2015/05/05 08:51:13 brouard
414: Summary: Adding digits in output parameters (7 digits instead of 6)
415:
416: Fix 1+age+.
417:
1.190 brouard 418: Revision 1.189 2015/04/30 14:45:16 brouard
419: Summary: 0.98q2
420:
1.189 brouard 421: Revision 1.188 2015/04/30 08:27:53 brouard
422: *** empty log message ***
423:
1.188 brouard 424: Revision 1.187 2015/04/29 09:11:15 brouard
425: *** empty log message ***
426:
1.187 brouard 427: Revision 1.186 2015/04/23 12:01:52 brouard
428: Summary: V1*age is working now, version 0.98q1
429:
430: Some codes had been disabled in order to simplify and Vn*age was
431: working in the optimization phase, ie, giving correct MLE parameters,
432: but, as usual, outputs were not correct and program core dumped.
433:
1.186 brouard 434: Revision 1.185 2015/03/11 13:26:42 brouard
435: Summary: Inclusion of compile and links command line for Intel Compiler
436:
1.185 brouard 437: Revision 1.184 2015/03/11 11:52:39 brouard
438: Summary: Back from Windows 8. Intel Compiler
439:
1.184 brouard 440: Revision 1.183 2015/03/10 20:34:32 brouard
441: Summary: 0.98q0, trying with directest, mnbrak fixed
442:
443: We use directest instead of original Powell test; probably no
444: incidence on the results, but better justifications;
445: We fixed Numerical Recipes mnbrak routine which was wrong and gave
446: wrong results.
447:
1.183 brouard 448: Revision 1.182 2015/02/12 08:19:57 brouard
449: Summary: Trying to keep directest which seems simpler and more general
450: Author: Nicolas Brouard
451:
1.182 brouard 452: Revision 1.181 2015/02/11 23:22:24 brouard
453: Summary: Comments on Powell added
454:
455: Author:
456:
1.181 brouard 457: Revision 1.180 2015/02/11 17:33:45 brouard
458: Summary: Finishing move from main to function (hpijx and prevalence_limit)
459:
1.180 brouard 460: Revision 1.179 2015/01/04 09:57:06 brouard
461: Summary: back to OS/X
462:
1.179 brouard 463: Revision 1.178 2015/01/04 09:35:48 brouard
464: *** empty log message ***
465:
1.178 brouard 466: Revision 1.177 2015/01/03 18:40:56 brouard
467: Summary: Still testing ilc32 on OSX
468:
1.177 brouard 469: Revision 1.176 2015/01/03 16:45:04 brouard
470: *** empty log message ***
471:
1.176 brouard 472: Revision 1.175 2015/01/03 16:33:42 brouard
473: *** empty log message ***
474:
1.175 brouard 475: Revision 1.174 2015/01/03 16:15:49 brouard
476: Summary: Still in cross-compilation
477:
1.174 brouard 478: Revision 1.173 2015/01/03 12:06:26 brouard
479: Summary: trying to detect cross-compilation
480:
1.173 brouard 481: Revision 1.172 2014/12/27 12:07:47 brouard
482: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
483:
1.172 brouard 484: Revision 1.171 2014/12/23 13:26:59 brouard
485: Summary: Back from Visual C
486:
487: Still problem with utsname.h on Windows
488:
1.171 brouard 489: Revision 1.170 2014/12/23 11:17:12 brouard
490: Summary: Cleaning some \%% back to %%
491:
492: The escape was mandatory for a specific compiler (which one?), but too many warnings.
493:
1.170 brouard 494: Revision 1.169 2014/12/22 23:08:31 brouard
495: Summary: 0.98p
496:
497: Outputs some informations on compiler used, OS etc. Testing on different platforms.
498:
1.169 brouard 499: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 500: Summary: update
1.169 brouard 501:
1.168 brouard 502: Revision 1.167 2014/12/22 13:50:56 brouard
503: Summary: Testing uname and compiler version and if compiled 32 or 64
504:
505: Testing on Linux 64
506:
1.167 brouard 507: Revision 1.166 2014/12/22 11:40:47 brouard
508: *** empty log message ***
509:
1.166 brouard 510: Revision 1.165 2014/12/16 11:20:36 brouard
511: Summary: After compiling on Visual C
512:
513: * imach.c (Module): Merging 1.61 to 1.162
514:
1.165 brouard 515: Revision 1.164 2014/12/16 10:52:11 brouard
516: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
517:
518: * imach.c (Module): Merging 1.61 to 1.162
519:
1.164 brouard 520: Revision 1.163 2014/12/16 10:30:11 brouard
521: * imach.c (Module): Merging 1.61 to 1.162
522:
1.163 brouard 523: Revision 1.162 2014/09/25 11:43:39 brouard
524: Summary: temporary backup 0.99!
525:
1.162 brouard 526: Revision 1.1 2014/09/16 11:06:58 brouard
527: Summary: With some code (wrong) for nlopt
528:
529: Author:
530:
531: Revision 1.161 2014/09/15 20:41:41 brouard
532: Summary: Problem with macro SQR on Intel compiler
533:
1.161 brouard 534: Revision 1.160 2014/09/02 09:24:05 brouard
535: *** empty log message ***
536:
1.160 brouard 537: Revision 1.159 2014/09/01 10:34:10 brouard
538: Summary: WIN32
539: Author: Brouard
540:
1.159 brouard 541: Revision 1.158 2014/08/27 17:11:51 brouard
542: *** empty log message ***
543:
1.158 brouard 544: Revision 1.157 2014/08/27 16:26:55 brouard
545: Summary: Preparing windows Visual studio version
546: Author: Brouard
547:
548: In order to compile on Visual studio, time.h is now correct and time_t
549: and tm struct should be used. difftime should be used but sometimes I
550: just make the differences in raw time format (time(&now).
551: Trying to suppress #ifdef LINUX
552: Add xdg-open for __linux in order to open default browser.
553:
1.157 brouard 554: Revision 1.156 2014/08/25 20:10:10 brouard
555: *** empty log message ***
556:
1.156 brouard 557: Revision 1.155 2014/08/25 18:32:34 brouard
558: Summary: New compile, minor changes
559: Author: Brouard
560:
1.155 brouard 561: Revision 1.154 2014/06/20 17:32:08 brouard
562: Summary: Outputs now all graphs of convergence to period prevalence
563:
1.154 brouard 564: Revision 1.153 2014/06/20 16:45:46 brouard
565: Summary: If 3 live state, convergence to period prevalence on same graph
566: Author: Brouard
567:
1.153 brouard 568: Revision 1.152 2014/06/18 17:54:09 brouard
569: Summary: open browser, use gnuplot on same dir than imach if not found in the path
570:
1.152 brouard 571: Revision 1.151 2014/06/18 16:43:30 brouard
572: *** empty log message ***
573:
1.151 brouard 574: Revision 1.150 2014/06/18 16:42:35 brouard
575: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
576: Author: brouard
577:
1.150 brouard 578: Revision 1.149 2014/06/18 15:51:14 brouard
579: Summary: Some fixes in parameter files errors
580: Author: Nicolas Brouard
581:
1.149 brouard 582: Revision 1.148 2014/06/17 17:38:48 brouard
583: Summary: Nothing new
584: Author: Brouard
585:
586: Just a new packaging for OS/X version 0.98nS
587:
1.148 brouard 588: Revision 1.147 2014/06/16 10:33:11 brouard
589: *** empty log message ***
590:
1.147 brouard 591: Revision 1.146 2014/06/16 10:20:28 brouard
592: Summary: Merge
593: Author: Brouard
594:
595: Merge, before building revised version.
596:
1.146 brouard 597: Revision 1.145 2014/06/10 21:23:15 brouard
598: Summary: Debugging with valgrind
599: Author: Nicolas Brouard
600:
601: Lot of changes in order to output the results with some covariates
602: After the Edimburgh REVES conference 2014, it seems mandatory to
603: improve the code.
604: No more memory valgrind error but a lot has to be done in order to
605: continue the work of splitting the code into subroutines.
606: Also, decodemodel has been improved. Tricode is still not
607: optimal. nbcode should be improved. Documentation has been added in
608: the source code.
609:
1.144 brouard 610: Revision 1.143 2014/01/26 09:45:38 brouard
611: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
612:
613: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
614: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
615:
1.143 brouard 616: Revision 1.142 2014/01/26 03:57:36 brouard
617: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
618:
619: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
620:
1.142 brouard 621: Revision 1.141 2014/01/26 02:42:01 brouard
622: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
623:
1.141 brouard 624: Revision 1.140 2011/09/02 10:37:54 brouard
625: Summary: times.h is ok with mingw32 now.
626:
1.140 brouard 627: Revision 1.139 2010/06/14 07:50:17 brouard
628: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
629: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
630:
1.139 brouard 631: Revision 1.138 2010/04/30 18:19:40 brouard
632: *** empty log message ***
633:
1.138 brouard 634: Revision 1.137 2010/04/29 18:11:38 brouard
635: (Module): Checking covariates for more complex models
636: than V1+V2. A lot of change to be done. Unstable.
637:
1.137 brouard 638: Revision 1.136 2010/04/26 20:30:53 brouard
639: (Module): merging some libgsl code. Fixing computation
640: of likelione (using inter/intrapolation if mle = 0) in order to
641: get same likelihood as if mle=1.
642: Some cleaning of code and comments added.
643:
1.136 brouard 644: Revision 1.135 2009/10/29 15:33:14 brouard
645: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
646:
1.135 brouard 647: Revision 1.134 2009/10/29 13:18:53 brouard
648: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
649:
1.134 brouard 650: Revision 1.133 2009/07/06 10:21:25 brouard
651: just nforces
652:
1.133 brouard 653: Revision 1.132 2009/07/06 08:22:05 brouard
654: Many tings
655:
1.132 brouard 656: Revision 1.131 2009/06/20 16:22:47 brouard
657: Some dimensions resccaled
658:
1.131 brouard 659: Revision 1.130 2009/05/26 06:44:34 brouard
660: (Module): Max Covariate is now set to 20 instead of 8. A
661: lot of cleaning with variables initialized to 0. Trying to make
662: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
663:
1.130 brouard 664: Revision 1.129 2007/08/31 13:49:27 lievre
665: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
666:
1.129 lievre 667: Revision 1.128 2006/06/30 13:02:05 brouard
668: (Module): Clarifications on computing e.j
669:
1.128 brouard 670: Revision 1.127 2006/04/28 18:11:50 brouard
671: (Module): Yes the sum of survivors was wrong since
672: imach-114 because nhstepm was no more computed in the age
673: loop. Now we define nhstepma in the age loop.
674: (Module): In order to speed up (in case of numerous covariates) we
675: compute health expectancies (without variances) in a first step
676: and then all the health expectancies with variances or standard
677: deviation (needs data from the Hessian matrices) which slows the
678: computation.
679: In the future we should be able to stop the program is only health
680: expectancies and graph are needed without standard deviations.
681:
1.127 brouard 682: Revision 1.126 2006/04/28 17:23:28 brouard
683: (Module): Yes the sum of survivors was wrong since
684: imach-114 because nhstepm was no more computed in the age
685: loop. Now we define nhstepma in the age loop.
686: Version 0.98h
687:
1.126 brouard 688: Revision 1.125 2006/04/04 15:20:31 lievre
689: Errors in calculation of health expectancies. Age was not initialized.
690: Forecasting file added.
691:
692: Revision 1.124 2006/03/22 17:13:53 lievre
693: Parameters are printed with %lf instead of %f (more numbers after the comma).
694: The log-likelihood is printed in the log file
695:
696: Revision 1.123 2006/03/20 10:52:43 brouard
697: * imach.c (Module): <title> changed, corresponds to .htm file
698: name. <head> headers where missing.
699:
700: * imach.c (Module): Weights can have a decimal point as for
701: English (a comma might work with a correct LC_NUMERIC environment,
702: otherwise the weight is truncated).
703: Modification of warning when the covariates values are not 0 or
704: 1.
705: Version 0.98g
706:
707: Revision 1.122 2006/03/20 09:45:41 brouard
708: (Module): Weights can have a decimal point as for
709: English (a comma might work with a correct LC_NUMERIC environment,
710: otherwise the weight is truncated).
711: Modification of warning when the covariates values are not 0 or
712: 1.
713: Version 0.98g
714:
715: Revision 1.121 2006/03/16 17:45:01 lievre
716: * imach.c (Module): Comments concerning covariates added
717:
718: * imach.c (Module): refinements in the computation of lli if
719: status=-2 in order to have more reliable computation if stepm is
720: not 1 month. Version 0.98f
721:
722: Revision 1.120 2006/03/16 15:10:38 lievre
723: (Module): refinements in the computation of lli if
724: status=-2 in order to have more reliable computation if stepm is
725: not 1 month. Version 0.98f
726:
727: Revision 1.119 2006/03/15 17:42:26 brouard
728: (Module): Bug if status = -2, the loglikelihood was
729: computed as likelihood omitting the logarithm. Version O.98e
730:
731: Revision 1.118 2006/03/14 18:20:07 brouard
732: (Module): varevsij Comments added explaining the second
733: table of variances if popbased=1 .
734: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
735: (Module): Function pstamp added
736: (Module): Version 0.98d
737:
738: Revision 1.117 2006/03/14 17:16:22 brouard
739: (Module): varevsij Comments added explaining the second
740: table of variances if popbased=1 .
741: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
742: (Module): Function pstamp added
743: (Module): Version 0.98d
744:
745: Revision 1.116 2006/03/06 10:29:27 brouard
746: (Module): Variance-covariance wrong links and
747: varian-covariance of ej. is needed (Saito).
748:
749: Revision 1.115 2006/02/27 12:17:45 brouard
750: (Module): One freematrix added in mlikeli! 0.98c
751:
752: Revision 1.114 2006/02/26 12:57:58 brouard
753: (Module): Some improvements in processing parameter
754: filename with strsep.
755:
756: Revision 1.113 2006/02/24 14:20:24 brouard
757: (Module): Memory leaks checks with valgrind and:
758: datafile was not closed, some imatrix were not freed and on matrix
759: allocation too.
760:
761: Revision 1.112 2006/01/30 09:55:26 brouard
762: (Module): Back to gnuplot.exe instead of wgnuplot.exe
763:
764: Revision 1.111 2006/01/25 20:38:18 brouard
765: (Module): Lots of cleaning and bugs added (Gompertz)
766: (Module): Comments can be added in data file. Missing date values
767: can be a simple dot '.'.
768:
769: Revision 1.110 2006/01/25 00:51:50 brouard
770: (Module): Lots of cleaning and bugs added (Gompertz)
771:
772: Revision 1.109 2006/01/24 19:37:15 brouard
773: (Module): Comments (lines starting with a #) are allowed in data.
774:
775: Revision 1.108 2006/01/19 18:05:42 lievre
776: Gnuplot problem appeared...
777: To be fixed
778:
779: Revision 1.107 2006/01/19 16:20:37 brouard
780: Test existence of gnuplot in imach path
781:
782: Revision 1.106 2006/01/19 13:24:36 brouard
783: Some cleaning and links added in html output
784:
785: Revision 1.105 2006/01/05 20:23:19 lievre
786: *** empty log message ***
787:
788: Revision 1.104 2005/09/30 16:11:43 lievre
789: (Module): sump fixed, loop imx fixed, and simplifications.
790: (Module): If the status is missing at the last wave but we know
791: that the person is alive, then we can code his/her status as -2
792: (instead of missing=-1 in earlier versions) and his/her
793: contributions to the likelihood is 1 - Prob of dying from last
794: health status (= 1-p13= p11+p12 in the easiest case of somebody in
795: the healthy state at last known wave). Version is 0.98
796:
797: Revision 1.103 2005/09/30 15:54:49 lievre
798: (Module): sump fixed, loop imx fixed, and simplifications.
799:
800: Revision 1.102 2004/09/15 17:31:30 brouard
801: Add the possibility to read data file including tab characters.
802:
803: Revision 1.101 2004/09/15 10:38:38 brouard
804: Fix on curr_time
805:
806: Revision 1.100 2004/07/12 18:29:06 brouard
807: Add version for Mac OS X. Just define UNIX in Makefile
808:
809: Revision 1.99 2004/06/05 08:57:40 brouard
810: *** empty log message ***
811:
812: Revision 1.98 2004/05/16 15:05:56 brouard
813: New version 0.97 . First attempt to estimate force of mortality
814: directly from the data i.e. without the need of knowing the health
815: state at each age, but using a Gompertz model: log u =a + b*age .
816: This is the basic analysis of mortality and should be done before any
817: other analysis, in order to test if the mortality estimated from the
818: cross-longitudinal survey is different from the mortality estimated
819: from other sources like vital statistic data.
820:
821: The same imach parameter file can be used but the option for mle should be -3.
822:
1.133 brouard 823: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 824: former routines in order to include the new code within the former code.
825:
826: The output is very simple: only an estimate of the intercept and of
827: the slope with 95% confident intervals.
828:
829: Current limitations:
830: A) Even if you enter covariates, i.e. with the
831: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
832: B) There is no computation of Life Expectancy nor Life Table.
833:
834: Revision 1.97 2004/02/20 13:25:42 lievre
835: Version 0.96d. Population forecasting command line is (temporarily)
836: suppressed.
837:
838: Revision 1.96 2003/07/15 15:38:55 brouard
839: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
840: rewritten within the same printf. Workaround: many printfs.
841:
842: Revision 1.95 2003/07/08 07:54:34 brouard
843: * imach.c (Repository):
844: (Repository): Using imachwizard code to output a more meaningful covariance
845: matrix (cov(a12,c31) instead of numbers.
846:
847: Revision 1.94 2003/06/27 13:00:02 brouard
848: Just cleaning
849:
850: Revision 1.93 2003/06/25 16:33:55 brouard
851: (Module): On windows (cygwin) function asctime_r doesn't
852: exist so I changed back to asctime which exists.
853: (Module): Version 0.96b
854:
855: Revision 1.92 2003/06/25 16:30:45 brouard
856: (Module): On windows (cygwin) function asctime_r doesn't
857: exist so I changed back to asctime which exists.
858:
859: Revision 1.91 2003/06/25 15:30:29 brouard
860: * imach.c (Repository): Duplicated warning errors corrected.
861: (Repository): Elapsed time after each iteration is now output. It
862: helps to forecast when convergence will be reached. Elapsed time
863: is stamped in powell. We created a new html file for the graphs
864: concerning matrix of covariance. It has extension -cov.htm.
865:
866: Revision 1.90 2003/06/24 12:34:15 brouard
867: (Module): Some bugs corrected for windows. Also, when
868: mle=-1 a template is output in file "or"mypar.txt with the design
869: of the covariance matrix to be input.
870:
871: Revision 1.89 2003/06/24 12:30:52 brouard
872: (Module): Some bugs corrected for windows. Also, when
873: mle=-1 a template is output in file "or"mypar.txt with the design
874: of the covariance matrix to be input.
875:
876: Revision 1.88 2003/06/23 17:54:56 brouard
877: * 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.
878:
879: Revision 1.87 2003/06/18 12:26:01 brouard
880: Version 0.96
881:
882: Revision 1.86 2003/06/17 20:04:08 brouard
883: (Module): Change position of html and gnuplot routines and added
884: routine fileappend.
885:
886: Revision 1.85 2003/06/17 13:12:43 brouard
887: * imach.c (Repository): Check when date of death was earlier that
888: current date of interview. It may happen when the death was just
889: prior to the death. In this case, dh was negative and likelihood
890: was wrong (infinity). We still send an "Error" but patch by
891: assuming that the date of death was just one stepm after the
892: interview.
893: (Repository): Because some people have very long ID (first column)
894: we changed int to long in num[] and we added a new lvector for
895: memory allocation. But we also truncated to 8 characters (left
896: truncation)
897: (Repository): No more line truncation errors.
898:
899: Revision 1.84 2003/06/13 21:44:43 brouard
900: * imach.c (Repository): Replace "freqsummary" at a correct
901: place. It differs from routine "prevalence" which may be called
902: many times. Probs is memory consuming and must be used with
903: parcimony.
904: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
905:
906: Revision 1.83 2003/06/10 13:39:11 lievre
907: *** empty log message ***
908:
909: Revision 1.82 2003/06/05 15:57:20 brouard
910: Add log in imach.c and fullversion number is now printed.
911:
912: */
913: /*
914: Interpolated Markov Chain
915:
916: Short summary of the programme:
917:
1.227 brouard 918: This program computes Healthy Life Expectancies or State-specific
919: (if states aren't health statuses) Expectancies from
920: cross-longitudinal data. Cross-longitudinal data consist in:
921:
922: -1- a first survey ("cross") where individuals from different ages
923: are interviewed on their health status or degree of disability (in
924: the case of a health survey which is our main interest)
925:
926: -2- at least a second wave of interviews ("longitudinal") which
927: measure each change (if any) in individual health status. Health
928: expectancies are computed from the time spent in each health state
929: according to a model. More health states you consider, more time is
930: necessary to reach the Maximum Likelihood of the parameters involved
931: in the model. The simplest model is the multinomial logistic model
932: where pij is the probability to be observed in state j at the second
933: wave conditional to be observed in state i at the first
934: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex +
935: etc , where 'age' is age and 'sex' is a covariate. If you want to
936: have a more complex model than "constant and age", you should modify
937: the program where the markup *Covariates have to be included here
938: again* invites you to do it. More covariates you add, slower the
1.126 brouard 939: convergence.
940:
941: The advantage of this computer programme, compared to a simple
942: multinomial logistic model, is clear when the delay between waves is not
943: identical for each individual. Also, if a individual missed an
944: intermediate interview, the information is lost, but taken into
945: account using an interpolation or extrapolation.
946:
947: hPijx is the probability to be observed in state i at age x+h
948: conditional to the observed state i at age x. The delay 'h' can be
949: split into an exact number (nh*stepm) of unobserved intermediate
950: states. This elementary transition (by month, quarter,
951: semester or year) is modelled as a multinomial logistic. The hPx
952: matrix is simply the matrix product of nh*stepm elementary matrices
953: and the contribution of each individual to the likelihood is simply
954: hPijx.
955:
956: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 957: of the life expectancies. It also computes the period (stable) prevalence.
958:
959: Back prevalence and projections:
1.227 brouard 960:
961: - back_prevalence_limit(double *p, double **bprlim, double ageminpar,
962: double agemaxpar, double ftolpl, int *ncvyearp, double
963: dateprev1,double dateprev2, int firstpass, int lastpass, int
964: mobilavproj)
965:
966: Computes the back prevalence limit for any combination of
967: covariate values k at any age between ageminpar and agemaxpar and
968: returns it in **bprlim. In the loops,
969:
970: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm,
971: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
972:
973: - hBijx Back Probability to be in state i at age x-h being in j at x
1.218 brouard 974: Computes for any combination of covariates k and any age between bage and fage
975: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
976: oldm=oldms;savm=savms;
1.227 brouard 977:
1.267 brouard 978: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.218 brouard 979: Computes the transition matrix starting at age 'age' over
980: 'nhstepm*hstepm*stepm' months (i.e. until
981: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
1.227 brouard 982: nhstepm*hstepm matrices.
983:
984: Returns p3mat[i][j][h] after calling
985: p3mat[i][j][h]=matprod2(newm,
986: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm,
987: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
988: oldm);
1.226 brouard 989:
990: Important routines
991:
992: - func (or funcone), computes logit (pij) distinguishing
993: o fixed variables (single or product dummies or quantitative);
994: o varying variables by:
995: (1) wave (single, product dummies, quantitative),
996: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
997: % fixed dummy (treated) or quantitative (not done because time-consuming);
998: % varying dummy (not done) or quantitative (not done);
999: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
1000: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
1001: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
1002: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
1003: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 1004:
1.226 brouard 1005:
1006:
1.133 brouard 1007: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
1008: Institut national d'études démographiques, Paris.
1.126 brouard 1009: This software have been partly granted by Euro-REVES, a concerted action
1010: from the European Union.
1011: It is copyrighted identically to a GNU software product, ie programme and
1012: software can be distributed freely for non commercial use. Latest version
1013: can be accessed at http://euroreves.ined.fr/imach .
1014:
1015: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
1016: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
1017:
1018: **********************************************************************/
1019: /*
1020: main
1021: read parameterfile
1022: read datafile
1023: concatwav
1024: freqsummary
1025: if (mle >= 1)
1026: mlikeli
1027: print results files
1028: if mle==1
1029: computes hessian
1030: read end of parameter file: agemin, agemax, bage, fage, estepm
1031: begin-prev-date,...
1032: open gnuplot file
1033: open html file
1.145 brouard 1034: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
1035: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
1036: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
1037: freexexit2 possible for memory heap.
1038:
1039: h Pij x | pij_nom ficrestpij
1040: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
1041: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
1042: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
1043:
1044: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
1045: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
1046: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
1047: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
1048: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
1049:
1.126 brouard 1050: forecasting if prevfcast==1 prevforecast call prevalence()
1051: health expectancies
1052: Variance-covariance of DFLE
1053: prevalence()
1054: movingaverage()
1055: varevsij()
1056: if popbased==1 varevsij(,popbased)
1057: total life expectancies
1058: Variance of period (stable) prevalence
1059: end
1060: */
1061:
1.187 brouard 1062: /* #define DEBUG */
1063: /* #define DEBUGBRENT */
1.203 brouard 1064: /* #define DEBUGLINMIN */
1065: /* #define DEBUGHESS */
1066: #define DEBUGHESSIJ
1.224 brouard 1067: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 1068: #define POWELL /* Instead of NLOPT */
1.224 brouard 1069: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 1070: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
1071: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 1072:
1073: #include <math.h>
1074: #include <stdio.h>
1075: #include <stdlib.h>
1076: #include <string.h>
1.226 brouard 1077: #include <ctype.h>
1.159 brouard 1078:
1079: #ifdef _WIN32
1080: #include <io.h>
1.172 brouard 1081: #include <windows.h>
1082: #include <tchar.h>
1.159 brouard 1083: #else
1.126 brouard 1084: #include <unistd.h>
1.159 brouard 1085: #endif
1.126 brouard 1086:
1087: #include <limits.h>
1088: #include <sys/types.h>
1.171 brouard 1089:
1090: #if defined(__GNUC__)
1091: #include <sys/utsname.h> /* Doesn't work on Windows */
1092: #endif
1093:
1.126 brouard 1094: #include <sys/stat.h>
1095: #include <errno.h>
1.159 brouard 1096: /* extern int errno; */
1.126 brouard 1097:
1.157 brouard 1098: /* #ifdef LINUX */
1099: /* #include <time.h> */
1100: /* #include "timeval.h" */
1101: /* #else */
1102: /* #include <sys/time.h> */
1103: /* #endif */
1104:
1.126 brouard 1105: #include <time.h>
1106:
1.136 brouard 1107: #ifdef GSL
1108: #include <gsl/gsl_errno.h>
1109: #include <gsl/gsl_multimin.h>
1110: #endif
1111:
1.167 brouard 1112:
1.162 brouard 1113: #ifdef NLOPT
1114: #include <nlopt.h>
1115: typedef struct {
1116: double (* function)(double [] );
1117: } myfunc_data ;
1118: #endif
1119:
1.126 brouard 1120: /* #include <libintl.h> */
1121: /* #define _(String) gettext (String) */
1122:
1.251 brouard 1123: #define MAXLINE 2048 /* Was 256 and 1024. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 1124:
1125: #define GNUPLOTPROGRAM "gnuplot"
1126: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
1127: #define FILENAMELENGTH 132
1128:
1129: #define GLOCK_ERROR_NOPATH -1 /* empty path */
1130: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
1131:
1.144 brouard 1132: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
1133: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 1134:
1135: #define NINTERVMAX 8
1.144 brouard 1136: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
1137: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
1.291 brouard 1138: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 1139: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 1140: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
1141: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.290 brouard 1142: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */
1.144 brouard 1143: #define YEARM 12. /**< Number of months per year */
1.218 brouard 1144: /* #define AGESUP 130 */
1.288 brouard 1145: /* #define AGESUP 150 */
1146: #define AGESUP 200
1.268 brouard 1147: #define AGEINF 0
1.218 brouard 1148: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 1149: #define AGEBASE 40
1.194 brouard 1150: #define AGEOVERFLOW 1.e20
1.164 brouard 1151: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 1152: #ifdef _WIN32
1153: #define DIRSEPARATOR '\\'
1154: #define CHARSEPARATOR "\\"
1155: #define ODIRSEPARATOR '/'
1156: #else
1.126 brouard 1157: #define DIRSEPARATOR '/'
1158: #define CHARSEPARATOR "/"
1159: #define ODIRSEPARATOR '\\'
1160: #endif
1161:
1.311 ! brouard 1162: /* $Id: imach.c,v 1.310 2022/03/17 08:45:53 brouard Exp $ */
1.126 brouard 1163: /* $State: Exp $ */
1.196 brouard 1164: #include "version.h"
1165: char version[]=__IMACH_VERSION__;
1.308 brouard 1166: 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.311 ! brouard 1167: char fullversion[]="$Revision: 1.310 $ $Date: 2022/03/17 08:45:53 $";
1.126 brouard 1168: char strstart[80];
1169: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 1170: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 1171: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 1172: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
1173: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
1174: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 1175: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
1176: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 1177: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
1178: int cptcovprodnoage=0; /**< Number of covariate products without age */
1179: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.233 brouard 1180: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */
1181: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */
1.232 brouard 1182: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */
1.234 brouard 1183: int nsd=0; /**< Total number of single dummy variables (output) */
1184: int nsq=0; /**< Total number of single quantitative variables (output) */
1.232 brouard 1185: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */
1.225 brouard 1186: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 1187: int ntveff=0; /**< ntveff number of effective time varying variables */
1188: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 1189: int cptcov=0; /* Working variable */
1.290 brouard 1190: int nobs=10; /* Number of observations in the data lastobs-firstobs */
1.218 brouard 1191: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.302 brouard 1192: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */
1.126 brouard 1193: int nlstate=2; /* Number of live states */
1194: int ndeath=1; /* Number of dead states */
1.130 brouard 1195: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 1196: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 1197: int popbased=0;
1198:
1199: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 1200: int maxwav=0; /* Maxim number of waves */
1201: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
1202: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
1203: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 1204: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 1205: int mle=1, weightopt=0;
1.126 brouard 1206: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
1207: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
1208: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
1209: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 1210: int countcallfunc=0; /* Count the number of calls to func */
1.230 brouard 1211: int selected(int kvar); /* Is covariate kvar selected for printing results */
1212:
1.130 brouard 1213: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 1214: double **matprod2(); /* test */
1.126 brouard 1215: double **oldm, **newm, **savm; /* Working pointers to matrices */
1216: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 1217: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
1218:
1.136 brouard 1219: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 1220: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 1221: FILE *ficlog, *ficrespow;
1.130 brouard 1222: int globpr=0; /* Global variable for printing or not */
1.126 brouard 1223: double fretone; /* Only one call to likelihood */
1.130 brouard 1224: long ipmx=0; /* Number of contributions */
1.126 brouard 1225: double sw; /* Sum of weights */
1226: char filerespow[FILENAMELENGTH];
1227: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
1228: FILE *ficresilk;
1229: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
1230: FILE *ficresprobmorprev;
1231: FILE *fichtm, *fichtmcov; /* Html File */
1232: FILE *ficreseij;
1233: char filerese[FILENAMELENGTH];
1234: FILE *ficresstdeij;
1235: char fileresstde[FILENAMELENGTH];
1236: FILE *ficrescveij;
1237: char filerescve[FILENAMELENGTH];
1238: FILE *ficresvij;
1239: char fileresv[FILENAMELENGTH];
1.269 brouard 1240:
1.126 brouard 1241: char title[MAXLINE];
1.234 brouard 1242: char model[MAXLINE]; /**< The model line */
1.217 brouard 1243: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 1244: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
1245: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
1246: char command[FILENAMELENGTH];
1247: int outcmd=0;
1248:
1.217 brouard 1249: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 1250: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 1251: char filelog[FILENAMELENGTH]; /* Log file */
1252: char filerest[FILENAMELENGTH];
1253: char fileregp[FILENAMELENGTH];
1254: char popfile[FILENAMELENGTH];
1255:
1256: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
1257:
1.157 brouard 1258: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
1259: /* struct timezone tzp; */
1260: /* extern int gettimeofday(); */
1261: struct tm tml, *gmtime(), *localtime();
1262:
1263: extern time_t time();
1264:
1265: struct tm start_time, end_time, curr_time, last_time, forecast_time;
1266: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
1267: struct tm tm;
1268:
1.126 brouard 1269: char strcurr[80], strfor[80];
1270:
1271: char *endptr;
1272: long lval;
1273: double dval;
1274:
1275: #define NR_END 1
1276: #define FREE_ARG char*
1277: #define FTOL 1.0e-10
1278:
1279: #define NRANSI
1.240 brouard 1280: #define ITMAX 200
1281: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */
1.126 brouard 1282:
1283: #define TOL 2.0e-4
1284:
1285: #define CGOLD 0.3819660
1286: #define ZEPS 1.0e-10
1287: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
1288:
1289: #define GOLD 1.618034
1290: #define GLIMIT 100.0
1291: #define TINY 1.0e-20
1292:
1293: static double maxarg1,maxarg2;
1294: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
1295: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
1296:
1297: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
1298: #define rint(a) floor(a+0.5)
1.166 brouard 1299: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 1300: #define mytinydouble 1.0e-16
1.166 brouard 1301: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
1302: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
1303: /* static double dsqrarg; */
1304: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 1305: static double sqrarg;
1306: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1307: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1308: int agegomp= AGEGOMP;
1309:
1310: int imx;
1311: int stepm=1;
1312: /* Stepm, step in month: minimum step interpolation*/
1313:
1314: int estepm;
1315: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1316:
1317: int m,nb;
1318: long *num;
1.197 brouard 1319: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1320: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1321: covariate for which somebody answered excluding
1322: undefined. Usually 2: 0 and 1. */
1323: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1324: covariate for which somebody answered including
1325: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1326: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1327: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1328: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1329: double *ageexmed,*agecens;
1330: double dateintmean=0;
1.296 brouard 1331: double anprojd, mprojd, jprojd; /* For eventual projections */
1332: double anprojf, mprojf, jprojf;
1.126 brouard 1333:
1.296 brouard 1334: double anbackd, mbackd, jbackd; /* For eventual backprojections */
1335: double anbackf, mbackf, jbackf;
1336: double jintmean,mintmean,aintmean;
1.126 brouard 1337: double *weight;
1338: int **s; /* Status */
1.141 brouard 1339: double *agedc;
1.145 brouard 1340: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1341: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1342: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.268 brouard 1343: double **coqvar; /* Fixed quantitative covariate nqv */
1344: double ***cotvar; /* Time varying covariate ntv */
1.225 brouard 1345: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1346: double idx;
1347: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.234 brouard 1348: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1349: /*k 1 2 3 4 5 6 7 8 9 */
1350: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */
1351: /* Tndvar[k] 1 2 3 4 5 */
1352: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */
1353: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */
1354: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */
1355: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */
1356: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */
1357: /* TvarsQind 1 6 */ /* position K of single quantitative cova */
1358: /* Tprod[i]=k 4 7 */
1359: /* Tage[i]=k 5 8 */
1360: /* */
1361: /* Type */
1362: /* V 1 2 3 4 5 */
1363: /* F F V V V */
1364: /* D Q D D Q */
1365: /* */
1366: int *TvarsD;
1367: int *TvarsDind;
1368: int *TvarsQ;
1369: int *TvarsQind;
1370:
1.235 brouard 1371: #define MAXRESULTLINES 10
1372: int nresult=0;
1.258 brouard 1373: int parameterline=0; /* # of the parameter (type) line */
1.235 brouard 1374: int TKresult[MAXRESULTLINES];
1.237 brouard 1375: int Tresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1376: int Tinvresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */
1.235 brouard 1377: int Tvresult[MAXRESULTLINES][NCOVMAX]; /* For dummy variable , variable # (output) */
1378: double Tqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.237 brouard 1379: double Tqinvresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */
1.235 brouard 1380: int Tvqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , variable # (output) */
1381:
1.234 brouard 1382: /* 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 1383: 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 */
1384: 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 */
1385: 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 */
1386: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1387: 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 */
1388: 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 1389: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1390: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1391: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1392: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1393: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1394: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */
1395: 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 */
1396: 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 */
1397:
1.230 brouard 1398: int *Tvarsel; /**< Selected covariates for output */
1399: double *Tvalsel; /**< Selected modality value of covariate for output */
1.226 brouard 1400: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
1.227 brouard 1401: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
1402: 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 1403: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */
1404: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */
1.197 brouard 1405: int *Tage;
1.227 brouard 1406: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */
1.228 brouard 1407: 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 1408: 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*/
1409: 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 1410: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1411: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.227 brouard 1412: int **Tvard;
1413: int *Tprod;/**< Gives the k position of the k1 product */
1.238 brouard 1414: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */
1.227 brouard 1415: int *Tposprod; /**< Gives the k1 product from the k position */
1.238 brouard 1416: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */
1417: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */
1.227 brouard 1418: int cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1419: double *lsurv, *lpop, *tpop;
1420:
1.231 brouard 1421: #define FD 1; /* Fixed dummy covariate */
1422: #define FQ 2; /* Fixed quantitative covariate */
1423: #define FP 3; /* Fixed product covariate */
1424: #define FPDD 7; /* Fixed product dummy*dummy covariate */
1425: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */
1426: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */
1427: #define VD 10; /* Varying dummy covariate */
1428: #define VQ 11; /* Varying quantitative covariate */
1429: #define VP 12; /* Varying product covariate */
1430: #define VPDD 13; /* Varying product dummy*dummy covariate */
1431: #define VPDQ 14; /* Varying product dummy*quantitative covariate */
1432: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */
1433: #define APFD 16; /* Age product * fixed dummy covariate */
1434: #define APFQ 17; /* Age product * fixed quantitative covariate */
1435: #define APVD 18; /* Age product * varying dummy covariate */
1436: #define APVQ 19; /* Age product * varying quantitative covariate */
1437:
1438: #define FTYPE 1; /* Fixed covariate */
1439: #define VTYPE 2; /* Varying covariate (loop in wave) */
1440: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/
1441:
1442: struct kmodel{
1443: int maintype; /* main type */
1444: int subtype; /* subtype */
1445: };
1446: struct kmodel modell[NCOVMAX];
1447:
1.143 brouard 1448: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1449: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1450:
1451: /**************** split *************************/
1452: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1453: {
1454: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1455: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1456: */
1457: char *ss; /* pointer */
1.186 brouard 1458: int l1=0, l2=0; /* length counters */
1.126 brouard 1459:
1460: l1 = strlen(path ); /* length of path */
1461: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1462: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1463: if ( ss == NULL ) { /* no directory, so determine current directory */
1464: strcpy( name, path ); /* we got the fullname name because no directory */
1465: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1466: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1467: /* get current working directory */
1468: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1469: #ifdef WIN32
1470: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1471: #else
1472: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1473: #endif
1.126 brouard 1474: return( GLOCK_ERROR_GETCWD );
1475: }
1476: /* got dirc from getcwd*/
1477: printf(" DIRC = %s \n",dirc);
1.205 brouard 1478: } else { /* strip directory from path */
1.126 brouard 1479: ss++; /* after this, the filename */
1480: l2 = strlen( ss ); /* length of filename */
1481: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1482: strcpy( name, ss ); /* save file name */
1483: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1484: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1485: printf(" DIRC2 = %s \n",dirc);
1486: }
1487: /* We add a separator at the end of dirc if not exists */
1488: l1 = strlen( dirc ); /* length of directory */
1489: if( dirc[l1-1] != DIRSEPARATOR ){
1490: dirc[l1] = DIRSEPARATOR;
1491: dirc[l1+1] = 0;
1492: printf(" DIRC3 = %s \n",dirc);
1493: }
1494: ss = strrchr( name, '.' ); /* find last / */
1495: if (ss >0){
1496: ss++;
1497: strcpy(ext,ss); /* save extension */
1498: l1= strlen( name);
1499: l2= strlen(ss)+1;
1500: strncpy( finame, name, l1-l2);
1501: finame[l1-l2]= 0;
1502: }
1503:
1504: return( 0 ); /* we're done */
1505: }
1506:
1507:
1508: /******************************************/
1509:
1510: void replace_back_to_slash(char *s, char*t)
1511: {
1512: int i;
1513: int lg=0;
1514: i=0;
1515: lg=strlen(t);
1516: for(i=0; i<= lg; i++) {
1517: (s[i] = t[i]);
1518: if (t[i]== '\\') s[i]='/';
1519: }
1520: }
1521:
1.132 brouard 1522: char *trimbb(char *out, char *in)
1.137 brouard 1523: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1524: char *s;
1525: s=out;
1526: while (*in != '\0'){
1.137 brouard 1527: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1528: in++;
1529: }
1530: *out++ = *in++;
1531: }
1532: *out='\0';
1533: return s;
1534: }
1535:
1.187 brouard 1536: /* char *substrchaine(char *out, char *in, char *chain) */
1537: /* { */
1538: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1539: /* char *s, *t; */
1540: /* t=in;s=out; */
1541: /* while ((*in != *chain) && (*in != '\0')){ */
1542: /* *out++ = *in++; */
1543: /* } */
1544:
1545: /* /\* *in matches *chain *\/ */
1546: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1547: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1548: /* } */
1549: /* in--; chain--; */
1550: /* while ( (*in != '\0')){ */
1551: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1552: /* *out++ = *in++; */
1553: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1554: /* } */
1555: /* *out='\0'; */
1556: /* out=s; */
1557: /* return out; */
1558: /* } */
1559: char *substrchaine(char *out, char *in, char *chain)
1560: {
1561: /* Substract chain 'chain' from 'in', return and output 'out' */
1562: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1563:
1564: char *strloc;
1565:
1566: strcpy (out, in);
1567: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1568: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1569: if(strloc != NULL){
1570: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1571: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1572: /* strcpy (strloc, strloc +strlen(chain));*/
1573: }
1574: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1575: return out;
1576: }
1577:
1578:
1.145 brouard 1579: char *cutl(char *blocc, char *alocc, char *in, char occ)
1580: {
1.187 brouard 1581: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1582: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.310 brouard 1583: gives alocc="abcdef" and blocc="ghi2j".
1.145 brouard 1584: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1585: */
1.160 brouard 1586: char *s, *t;
1.145 brouard 1587: t=in;s=in;
1588: while ((*in != occ) && (*in != '\0')){
1589: *alocc++ = *in++;
1590: }
1591: if( *in == occ){
1592: *(alocc)='\0';
1593: s=++in;
1594: }
1595:
1596: if (s == t) {/* occ not found */
1597: *(alocc-(in-s))='\0';
1598: in=s;
1599: }
1600: while ( *in != '\0'){
1601: *blocc++ = *in++;
1602: }
1603:
1604: *blocc='\0';
1605: return t;
1606: }
1.137 brouard 1607: char *cutv(char *blocc, char *alocc, char *in, char occ)
1608: {
1.187 brouard 1609: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1610: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1611: gives blocc="abcdef2ghi" and alocc="j".
1612: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1613: */
1614: char *s, *t;
1615: t=in;s=in;
1616: while (*in != '\0'){
1617: while( *in == occ){
1618: *blocc++ = *in++;
1619: s=in;
1620: }
1621: *blocc++ = *in++;
1622: }
1623: if (s == t) /* occ not found */
1624: *(blocc-(in-s))='\0';
1625: else
1626: *(blocc-(in-s)-1)='\0';
1627: in=s;
1628: while ( *in != '\0'){
1629: *alocc++ = *in++;
1630: }
1631:
1632: *alocc='\0';
1633: return s;
1634: }
1635:
1.126 brouard 1636: int nbocc(char *s, char occ)
1637: {
1638: int i,j=0;
1639: int lg=20;
1640: i=0;
1641: lg=strlen(s);
1642: for(i=0; i<= lg; i++) {
1.234 brouard 1643: if (s[i] == occ ) j++;
1.126 brouard 1644: }
1645: return j;
1646: }
1647:
1.137 brouard 1648: /* void cutv(char *u,char *v, char*t, char occ) */
1649: /* { */
1650: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1651: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1652: /* gives u="abcdef2ghi" and v="j" *\/ */
1653: /* int i,lg,j,p=0; */
1654: /* i=0; */
1655: /* lg=strlen(t); */
1656: /* for(j=0; j<=lg-1; j++) { */
1657: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1658: /* } */
1.126 brouard 1659:
1.137 brouard 1660: /* for(j=0; j<p; j++) { */
1661: /* (u[j] = t[j]); */
1662: /* } */
1663: /* u[p]='\0'; */
1.126 brouard 1664:
1.137 brouard 1665: /* for(j=0; j<= lg; j++) { */
1666: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1667: /* } */
1668: /* } */
1.126 brouard 1669:
1.160 brouard 1670: #ifdef _WIN32
1671: char * strsep(char **pp, const char *delim)
1672: {
1673: char *p, *q;
1674:
1675: if ((p = *pp) == NULL)
1676: return 0;
1677: if ((q = strpbrk (p, delim)) != NULL)
1678: {
1679: *pp = q + 1;
1680: *q = '\0';
1681: }
1682: else
1683: *pp = 0;
1684: return p;
1685: }
1686: #endif
1687:
1.126 brouard 1688: /********************** nrerror ********************/
1689:
1690: void nrerror(char error_text[])
1691: {
1692: fprintf(stderr,"ERREUR ...\n");
1693: fprintf(stderr,"%s\n",error_text);
1694: exit(EXIT_FAILURE);
1695: }
1696: /*********************** vector *******************/
1697: double *vector(int nl, int nh)
1698: {
1699: double *v;
1700: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1701: if (!v) nrerror("allocation failure in vector");
1702: return v-nl+NR_END;
1703: }
1704:
1705: /************************ free vector ******************/
1706: void free_vector(double*v, int nl, int nh)
1707: {
1708: free((FREE_ARG)(v+nl-NR_END));
1709: }
1710:
1711: /************************ivector *******************************/
1712: int *ivector(long nl,long nh)
1713: {
1714: int *v;
1715: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1716: if (!v) nrerror("allocation failure in ivector");
1717: return v-nl+NR_END;
1718: }
1719:
1720: /******************free ivector **************************/
1721: void free_ivector(int *v, long nl, long nh)
1722: {
1723: free((FREE_ARG)(v+nl-NR_END));
1724: }
1725:
1726: /************************lvector *******************************/
1727: long *lvector(long nl,long nh)
1728: {
1729: long *v;
1730: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1731: if (!v) nrerror("allocation failure in ivector");
1732: return v-nl+NR_END;
1733: }
1734:
1735: /******************free lvector **************************/
1736: void free_lvector(long *v, long nl, long nh)
1737: {
1738: free((FREE_ARG)(v+nl-NR_END));
1739: }
1740:
1741: /******************* imatrix *******************************/
1742: int **imatrix(long nrl, long nrh, long ncl, long nch)
1743: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1744: {
1745: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1746: int **m;
1747:
1748: /* allocate pointers to rows */
1749: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1750: if (!m) nrerror("allocation failure 1 in matrix()");
1751: m += NR_END;
1752: m -= nrl;
1753:
1754:
1755: /* allocate rows and set pointers to them */
1756: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1757: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1758: m[nrl] += NR_END;
1759: m[nrl] -= ncl;
1760:
1761: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1762:
1763: /* return pointer to array of pointers to rows */
1764: return m;
1765: }
1766:
1767: /****************** free_imatrix *************************/
1768: void free_imatrix(m,nrl,nrh,ncl,nch)
1769: int **m;
1770: long nch,ncl,nrh,nrl;
1771: /* free an int matrix allocated by imatrix() */
1772: {
1773: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1774: free((FREE_ARG) (m+nrl-NR_END));
1775: }
1776:
1777: /******************* matrix *******************************/
1778: double **matrix(long nrl, long nrh, long ncl, long nch)
1779: {
1780: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1781: double **m;
1782:
1783: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1784: if (!m) nrerror("allocation failure 1 in matrix()");
1785: m += NR_END;
1786: m -= nrl;
1787:
1788: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1789: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1790: m[nrl] += NR_END;
1791: m[nrl] -= ncl;
1792:
1793: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1794: return m;
1.145 brouard 1795: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1796: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1797: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1798: */
1799: }
1800:
1801: /*************************free matrix ************************/
1802: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1803: {
1804: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1805: free((FREE_ARG)(m+nrl-NR_END));
1806: }
1807:
1808: /******************* ma3x *******************************/
1809: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1810: {
1811: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1812: double ***m;
1813:
1814: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1815: if (!m) nrerror("allocation failure 1 in matrix()");
1816: m += NR_END;
1817: m -= nrl;
1818:
1819: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1820: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1821: m[nrl] += NR_END;
1822: m[nrl] -= ncl;
1823:
1824: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1825:
1826: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1827: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1828: m[nrl][ncl] += NR_END;
1829: m[nrl][ncl] -= nll;
1830: for (j=ncl+1; j<=nch; j++)
1831: m[nrl][j]=m[nrl][j-1]+nlay;
1832:
1833: for (i=nrl+1; i<=nrh; i++) {
1834: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1835: for (j=ncl+1; j<=nch; j++)
1836: m[i][j]=m[i][j-1]+nlay;
1837: }
1838: return m;
1839: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1840: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1841: */
1842: }
1843:
1844: /*************************free ma3x ************************/
1845: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1846: {
1847: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1848: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1849: free((FREE_ARG)(m+nrl-NR_END));
1850: }
1851:
1852: /*************** function subdirf ***********/
1853: char *subdirf(char fileres[])
1854: {
1855: /* Caution optionfilefiname is hidden */
1856: strcpy(tmpout,optionfilefiname);
1857: strcat(tmpout,"/"); /* Add to the right */
1858: strcat(tmpout,fileres);
1859: return tmpout;
1860: }
1861:
1862: /*************** function subdirf2 ***********/
1863: char *subdirf2(char fileres[], char *preop)
1864: {
1865:
1866: /* Caution optionfilefiname is hidden */
1867: strcpy(tmpout,optionfilefiname);
1868: strcat(tmpout,"/");
1869: strcat(tmpout,preop);
1870: strcat(tmpout,fileres);
1871: return tmpout;
1872: }
1873:
1874: /*************** function subdirf3 ***********/
1875: char *subdirf3(char fileres[], char *preop, char *preop2)
1876: {
1877:
1878: /* Caution optionfilefiname is hidden */
1879: strcpy(tmpout,optionfilefiname);
1880: strcat(tmpout,"/");
1881: strcat(tmpout,preop);
1882: strcat(tmpout,preop2);
1883: strcat(tmpout,fileres);
1884: return tmpout;
1885: }
1.213 brouard 1886:
1887: /*************** function subdirfext ***********/
1888: char *subdirfext(char fileres[], char *preop, char *postop)
1889: {
1890:
1891: strcpy(tmpout,preop);
1892: strcat(tmpout,fileres);
1893: strcat(tmpout,postop);
1894: return tmpout;
1895: }
1.126 brouard 1896:
1.213 brouard 1897: /*************** function subdirfext3 ***********/
1898: char *subdirfext3(char fileres[], char *preop, char *postop)
1899: {
1900:
1901: /* Caution optionfilefiname is hidden */
1902: strcpy(tmpout,optionfilefiname);
1903: strcat(tmpout,"/");
1904: strcat(tmpout,preop);
1905: strcat(tmpout,fileres);
1906: strcat(tmpout,postop);
1907: return tmpout;
1908: }
1909:
1.162 brouard 1910: char *asc_diff_time(long time_sec, char ascdiff[])
1911: {
1912: long sec_left, days, hours, minutes;
1913: days = (time_sec) / (60*60*24);
1914: sec_left = (time_sec) % (60*60*24);
1915: hours = (sec_left) / (60*60) ;
1916: sec_left = (sec_left) %(60*60);
1917: minutes = (sec_left) /60;
1918: sec_left = (sec_left) % (60);
1919: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1920: return ascdiff;
1921: }
1922:
1.126 brouard 1923: /***************** f1dim *************************/
1924: extern int ncom;
1925: extern double *pcom,*xicom;
1926: extern double (*nrfunc)(double []);
1927:
1928: double f1dim(double x)
1929: {
1930: int j;
1931: double f;
1932: double *xt;
1933:
1934: xt=vector(1,ncom);
1935: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1936: f=(*nrfunc)(xt);
1937: free_vector(xt,1,ncom);
1938: return f;
1939: }
1940:
1941: /*****************brent *************************/
1942: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1943: {
1944: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1945: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1946: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1947: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1948: * returned function value.
1949: */
1.126 brouard 1950: int iter;
1951: double a,b,d,etemp;
1.159 brouard 1952: double fu=0,fv,fw,fx;
1.164 brouard 1953: double ftemp=0.;
1.126 brouard 1954: double p,q,r,tol1,tol2,u,v,w,x,xm;
1955: double e=0.0;
1956:
1957: a=(ax < cx ? ax : cx);
1958: b=(ax > cx ? ax : cx);
1959: x=w=v=bx;
1960: fw=fv=fx=(*f)(x);
1961: for (iter=1;iter<=ITMAX;iter++) {
1962: xm=0.5*(a+b);
1963: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1964: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1965: printf(".");fflush(stdout);
1966: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1967: #ifdef DEBUGBRENT
1.126 brouard 1968: 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);
1969: 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);
1970: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1971: #endif
1972: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1973: *xmin=x;
1974: return fx;
1975: }
1976: ftemp=fu;
1977: if (fabs(e) > tol1) {
1978: r=(x-w)*(fx-fv);
1979: q=(x-v)*(fx-fw);
1980: p=(x-v)*q-(x-w)*r;
1981: q=2.0*(q-r);
1982: if (q > 0.0) p = -p;
1983: q=fabs(q);
1984: etemp=e;
1985: e=d;
1986: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1987: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1988: else {
1.224 brouard 1989: d=p/q;
1990: u=x+d;
1991: if (u-a < tol2 || b-u < tol2)
1992: d=SIGN(tol1,xm-x);
1.126 brouard 1993: }
1994: } else {
1995: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1996: }
1997: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1998: fu=(*f)(u);
1999: if (fu <= fx) {
2000: if (u >= x) a=x; else b=x;
2001: SHFT(v,w,x,u)
1.183 brouard 2002: SHFT(fv,fw,fx,fu)
2003: } else {
2004: if (u < x) a=u; else b=u;
2005: if (fu <= fw || w == x) {
1.224 brouard 2006: v=w;
2007: w=u;
2008: fv=fw;
2009: fw=fu;
1.183 brouard 2010: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 2011: v=u;
2012: fv=fu;
1.183 brouard 2013: }
2014: }
1.126 brouard 2015: }
2016: nrerror("Too many iterations in brent");
2017: *xmin=x;
2018: return fx;
2019: }
2020:
2021: /****************** mnbrak ***********************/
2022:
2023: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
2024: double (*func)(double))
1.183 brouard 2025: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
2026: the downhill direction (defined by the function as evaluated at the initial points) and returns
2027: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
2028: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
2029: */
1.126 brouard 2030: double ulim,u,r,q, dum;
2031: double fu;
1.187 brouard 2032:
2033: double scale=10.;
2034: int iterscale=0;
2035:
2036: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
2037: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
2038:
2039:
2040: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
2041: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
2042: /* *bx = *ax - (*ax - *bx)/scale; */
2043: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
2044: /* } */
2045:
1.126 brouard 2046: if (*fb > *fa) {
2047: SHFT(dum,*ax,*bx,dum)
1.183 brouard 2048: SHFT(dum,*fb,*fa,dum)
2049: }
1.126 brouard 2050: *cx=(*bx)+GOLD*(*bx-*ax);
2051: *fc=(*func)(*cx);
1.183 brouard 2052: #ifdef DEBUG
1.224 brouard 2053: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
2054: 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 2055: #endif
1.224 brouard 2056: 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 2057: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 2058: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 2059: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 2060: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
2061: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
2062: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 2063: fu=(*func)(u);
1.163 brouard 2064: #ifdef DEBUG
2065: /* f(x)=A(x-u)**2+f(u) */
2066: double A, fparabu;
2067: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2068: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 2069: 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);
2070: 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 2071: /* And thus,it can be that fu > *fc even if fparabu < *fc */
2072: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
2073: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
2074: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 2075: #endif
1.184 brouard 2076: #ifdef MNBRAKORIGINAL
1.183 brouard 2077: #else
1.191 brouard 2078: /* if (fu > *fc) { */
2079: /* #ifdef DEBUG */
2080: /* printf("mnbrak4 fu > fc \n"); */
2081: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
2082: /* #endif */
2083: /* /\* 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 *\\/ *\/ */
2084: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
2085: /* dum=u; /\* Shifting c and u *\/ */
2086: /* u = *cx; */
2087: /* *cx = dum; */
2088: /* dum = fu; */
2089: /* fu = *fc; */
2090: /* *fc =dum; */
2091: /* } else { /\* end *\/ */
2092: /* #ifdef DEBUG */
2093: /* printf("mnbrak3 fu < fc \n"); */
2094: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
2095: /* #endif */
2096: /* dum=u; /\* Shifting c and u *\/ */
2097: /* u = *cx; */
2098: /* *cx = dum; */
2099: /* dum = fu; */
2100: /* fu = *fc; */
2101: /* *fc =dum; */
2102: /* } */
1.224 brouard 2103: #ifdef DEBUGMNBRAK
2104: double A, fparabu;
2105: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
2106: fparabu= *fa - A*(*ax-u)*(*ax-u);
2107: 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);
2108: 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 2109: #endif
1.191 brouard 2110: dum=u; /* Shifting c and u */
2111: u = *cx;
2112: *cx = dum;
2113: dum = fu;
2114: fu = *fc;
2115: *fc =dum;
1.183 brouard 2116: #endif
1.162 brouard 2117: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 2118: #ifdef DEBUG
1.224 brouard 2119: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
2120: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 2121: #endif
1.126 brouard 2122: fu=(*func)(u);
2123: if (fu < *fc) {
1.183 brouard 2124: #ifdef DEBUG
1.224 brouard 2125: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2126: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
2127: #endif
2128: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
2129: SHFT(*fb,*fc,fu,(*func)(u))
2130: #ifdef DEBUG
2131: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 2132: #endif
2133: }
1.162 brouard 2134: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 2135: #ifdef DEBUG
1.224 brouard 2136: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
2137: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 2138: #endif
1.126 brouard 2139: u=ulim;
2140: fu=(*func)(u);
1.183 brouard 2141: } else { /* u could be left to b (if r > q parabola has a maximum) */
2142: #ifdef DEBUG
1.224 brouard 2143: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
2144: 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 2145: #endif
1.126 brouard 2146: u=(*cx)+GOLD*(*cx-*bx);
2147: fu=(*func)(u);
1.224 brouard 2148: #ifdef DEBUG
2149: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2150: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
2151: #endif
1.183 brouard 2152: } /* end tests */
1.126 brouard 2153: SHFT(*ax,*bx,*cx,u)
1.183 brouard 2154: SHFT(*fa,*fb,*fc,fu)
2155: #ifdef DEBUG
1.224 brouard 2156: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
2157: 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 2158: #endif
2159: } /* 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 2160: }
2161:
2162: /*************** linmin ************************/
1.162 brouard 2163: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
2164: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
2165: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
2166: the value of func at the returned location p . This is actually all accomplished by calling the
2167: routines mnbrak and brent .*/
1.126 brouard 2168: int ncom;
2169: double *pcom,*xicom;
2170: double (*nrfunc)(double []);
2171:
1.224 brouard 2172: #ifdef LINMINORIGINAL
1.126 brouard 2173: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 2174: #else
2175: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
2176: #endif
1.126 brouard 2177: {
2178: double brent(double ax, double bx, double cx,
2179: double (*f)(double), double tol, double *xmin);
2180: double f1dim(double x);
2181: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
2182: double *fc, double (*func)(double));
2183: int j;
2184: double xx,xmin,bx,ax;
2185: double fx,fb,fa;
1.187 brouard 2186:
1.203 brouard 2187: #ifdef LINMINORIGINAL
2188: #else
2189: double scale=10., axs, xxs; /* Scale added for infinity */
2190: #endif
2191:
1.126 brouard 2192: ncom=n;
2193: pcom=vector(1,n);
2194: xicom=vector(1,n);
2195: nrfunc=func;
2196: for (j=1;j<=n;j++) {
2197: pcom[j]=p[j];
1.202 brouard 2198: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 2199: }
1.187 brouard 2200:
1.203 brouard 2201: #ifdef LINMINORIGINAL
2202: xx=1.;
2203: #else
2204: axs=0.0;
2205: xxs=1.;
2206: do{
2207: xx= xxs;
2208: #endif
1.187 brouard 2209: ax=0.;
2210: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
2211: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
2212: /* 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)) */
2213: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
2214: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
2215: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
2216: /* 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 2217: #ifdef LINMINORIGINAL
2218: #else
2219: if (fx != fx){
1.224 brouard 2220: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
2221: printf("|");
2222: fprintf(ficlog,"|");
1.203 brouard 2223: #ifdef DEBUGLINMIN
1.224 brouard 2224: 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 2225: #endif
2226: }
1.224 brouard 2227: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 2228: #endif
2229:
1.191 brouard 2230: #ifdef DEBUGLINMIN
2231: 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 2232: 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 2233: #endif
1.224 brouard 2234: #ifdef LINMINORIGINAL
2235: #else
2236: if(fb == fx){ /* Flat function in the direction */
2237: xmin=xx;
2238: *flat=1;
2239: }else{
2240: *flat=0;
2241: #endif
2242: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 2243: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
2244: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
2245: /* fmin = f(p[j] + xmin * xi[j]) */
2246: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
2247: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 2248: #ifdef DEBUG
1.224 brouard 2249: 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);
2250: 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);
2251: #endif
2252: #ifdef LINMINORIGINAL
2253: #else
2254: }
1.126 brouard 2255: #endif
1.191 brouard 2256: #ifdef DEBUGLINMIN
2257: printf("linmin end ");
1.202 brouard 2258: fprintf(ficlog,"linmin end ");
1.191 brouard 2259: #endif
1.126 brouard 2260: for (j=1;j<=n;j++) {
1.203 brouard 2261: #ifdef LINMINORIGINAL
2262: xi[j] *= xmin;
2263: #else
2264: #ifdef DEBUGLINMIN
2265: if(xxs <1.0)
2266: printf(" before xi[%d]=%12.8f", j,xi[j]);
2267: #endif
2268: 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) */
2269: #ifdef DEBUGLINMIN
2270: if(xxs <1.0)
2271: 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 );
2272: #endif
2273: #endif
1.187 brouard 2274: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 2275: }
1.191 brouard 2276: #ifdef DEBUGLINMIN
1.203 brouard 2277: printf("\n");
1.191 brouard 2278: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 2279: 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 2280: for (j=1;j<=n;j++) {
1.202 brouard 2281: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2282: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
2283: if(j % ncovmodel == 0){
1.191 brouard 2284: printf("\n");
1.202 brouard 2285: fprintf(ficlog,"\n");
2286: }
1.191 brouard 2287: }
1.203 brouard 2288: #else
1.191 brouard 2289: #endif
1.126 brouard 2290: free_vector(xicom,1,n);
2291: free_vector(pcom,1,n);
2292: }
2293:
2294:
2295: /*************** powell ************************/
1.162 brouard 2296: /*
2297: Minimization of a function func of n variables. Input consists of an initial starting point
2298: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
2299: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
2300: such that failure to decrease by more than this amount on one iteration signals doneness. On
2301: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
2302: function value at p , and iter is the number of iterations taken. The routine linmin is used.
2303: */
1.224 brouard 2304: #ifdef LINMINORIGINAL
2305: #else
2306: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 2307: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 2308: #endif
1.126 brouard 2309: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
2310: double (*func)(double []))
2311: {
1.224 brouard 2312: #ifdef LINMINORIGINAL
2313: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 2314: double (*func)(double []));
1.224 brouard 2315: #else
1.241 brouard 2316: void linmin(double p[], double xi[], int n, double *fret,
2317: double (*func)(double []),int *flat);
1.224 brouard 2318: #endif
1.239 brouard 2319: int i,ibig,j,jk,k;
1.126 brouard 2320: double del,t,*pt,*ptt,*xit;
1.181 brouard 2321: double directest;
1.126 brouard 2322: double fp,fptt;
2323: double *xits;
2324: int niterf, itmp;
1.224 brouard 2325: #ifdef LINMINORIGINAL
2326: #else
2327:
2328: flatdir=ivector(1,n);
2329: for (j=1;j<=n;j++) flatdir[j]=0;
2330: #endif
1.126 brouard 2331:
2332: pt=vector(1,n);
2333: ptt=vector(1,n);
2334: xit=vector(1,n);
2335: xits=vector(1,n);
2336: *fret=(*func)(p);
2337: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 2338: rcurr_time = time(NULL);
1.126 brouard 2339: for (*iter=1;;++(*iter)) {
1.187 brouard 2340: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 2341: ibig=0;
2342: del=0.0;
1.157 brouard 2343: rlast_time=rcurr_time;
2344: /* (void) gettimeofday(&curr_time,&tzp); */
2345: rcurr_time = time(NULL);
2346: curr_time = *localtime(&rcurr_time);
2347: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
2348: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
2349: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 2350: for (i=1;i<=n;i++) {
1.126 brouard 2351: fprintf(ficrespow," %.12lf", p[i]);
2352: }
1.239 brouard 2353: fprintf(ficrespow,"\n");fflush(ficrespow);
2354: printf("\n#model= 1 + age ");
2355: fprintf(ficlog,"\n#model= 1 + age ");
2356: if(nagesqr==1){
1.241 brouard 2357: printf(" + age*age ");
2358: fprintf(ficlog," + age*age ");
1.239 brouard 2359: }
2360: for(j=1;j <=ncovmodel-2;j++){
2361: if(Typevar[j]==0) {
2362: printf(" + V%d ",Tvar[j]);
2363: fprintf(ficlog," + V%d ",Tvar[j]);
2364: }else if(Typevar[j]==1) {
2365: printf(" + V%d*age ",Tvar[j]);
2366: fprintf(ficlog," + V%d*age ",Tvar[j]);
2367: }else if(Typevar[j]==2) {
2368: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2369: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]);
2370: }
2371: }
1.126 brouard 2372: printf("\n");
1.239 brouard 2373: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */
2374: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */
1.126 brouard 2375: fprintf(ficlog,"\n");
1.239 brouard 2376: for(i=1,jk=1; i <=nlstate; i++){
2377: for(k=1; k <=(nlstate+ndeath); k++){
2378: if (k != i) {
2379: printf("%d%d ",i,k);
2380: fprintf(ficlog,"%d%d ",i,k);
2381: for(j=1; j <=ncovmodel; j++){
2382: printf("%12.7f ",p[jk]);
2383: fprintf(ficlog,"%12.7f ",p[jk]);
2384: jk++;
2385: }
2386: printf("\n");
2387: fprintf(ficlog,"\n");
2388: }
2389: }
2390: }
1.241 brouard 2391: if(*iter <=3 && *iter >1){
1.157 brouard 2392: tml = *localtime(&rcurr_time);
2393: strcpy(strcurr,asctime(&tml));
2394: rforecast_time=rcurr_time;
1.126 brouard 2395: itmp = strlen(strcurr);
2396: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.241 brouard 2397: strcurr[itmp-1]='\0';
1.162 brouard 2398: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 2399: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 2400: for(niterf=10;niterf<=30;niterf+=10){
1.241 brouard 2401: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
2402: forecast_time = *localtime(&rforecast_time);
2403: strcpy(strfor,asctime(&forecast_time));
2404: itmp = strlen(strfor);
2405: if(strfor[itmp-1]=='\n')
2406: strfor[itmp-1]='\0';
2407: 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);
2408: 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 2409: }
2410: }
1.187 brouard 2411: for (i=1;i<=n;i++) { /* For each direction i */
2412: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 2413: fptt=(*fret);
2414: #ifdef DEBUG
1.203 brouard 2415: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
2416: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 2417: #endif
1.203 brouard 2418: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 2419: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 2420: #ifdef LINMINORIGINAL
1.188 brouard 2421: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 2422: #else
2423: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
2424: flatdir[i]=flat; /* Function is vanishing in that direction i */
2425: #endif
2426: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 2427: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 2428: /* because that direction will be replaced unless the gain del is small */
2429: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
2430: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
2431: /* with the new direction. */
2432: del=fabs(fptt-(*fret));
2433: ibig=i;
1.126 brouard 2434: }
2435: #ifdef DEBUG
2436: printf("%d %.12e",i,(*fret));
2437: fprintf(ficlog,"%d %.12e",i,(*fret));
2438: for (j=1;j<=n;j++) {
1.224 brouard 2439: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2440: printf(" x(%d)=%.12e",j,xit[j]);
2441: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2442: }
2443: for(j=1;j<=n;j++) {
1.225 brouard 2444: printf(" p(%d)=%.12e",j,p[j]);
2445: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2446: }
2447: printf("\n");
2448: fprintf(ficlog,"\n");
2449: #endif
1.187 brouard 2450: } /* end loop on each direction i */
2451: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2452: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2453: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2454: for(j=1;j<=n;j++) {
1.302 brouard 2455: if(flatdir[j] >0){
2456: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2457: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2458: }
2459: /* printf("\n"); */
2460: /* fprintf(ficlog,"\n"); */
2461: }
1.243 brouard 2462: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */
2463: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */
1.188 brouard 2464: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2465: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2466: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2467: /* decreased of more than 3.84 */
2468: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2469: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2470: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2471:
1.188 brouard 2472: /* Starting the program with initial values given by a former maximization will simply change */
2473: /* the scales of the directions and the directions, because the are reset to canonical directions */
2474: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2475: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2476: #ifdef DEBUG
2477: int k[2],l;
2478: k[0]=1;
2479: k[1]=-1;
2480: printf("Max: %.12e",(*func)(p));
2481: fprintf(ficlog,"Max: %.12e",(*func)(p));
2482: for (j=1;j<=n;j++) {
2483: printf(" %.12e",p[j]);
2484: fprintf(ficlog," %.12e",p[j]);
2485: }
2486: printf("\n");
2487: fprintf(ficlog,"\n");
2488: for(l=0;l<=1;l++) {
2489: for (j=1;j<=n;j++) {
2490: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2491: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2492: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2493: }
2494: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2495: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2496: }
2497: #endif
2498:
1.224 brouard 2499: #ifdef LINMINORIGINAL
2500: #else
2501: free_ivector(flatdir,1,n);
2502: #endif
1.126 brouard 2503: free_vector(xit,1,n);
2504: free_vector(xits,1,n);
2505: free_vector(ptt,1,n);
2506: free_vector(pt,1,n);
2507: return;
1.192 brouard 2508: } /* enough precision */
1.240 brouard 2509: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2510: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2511: ptt[j]=2.0*p[j]-pt[j];
2512: xit[j]=p[j]-pt[j];
2513: pt[j]=p[j];
2514: }
1.181 brouard 2515: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2516: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2517: if (*iter <=4) {
1.225 brouard 2518: #else
2519: #endif
1.224 brouard 2520: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2521: #else
1.161 brouard 2522: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2523: #endif
1.162 brouard 2524: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2525: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2526: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2527: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2528: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2529: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2530: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2531: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2532: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2533: /* Even if f3 <f1, directest can be negative and t >0 */
2534: /* mu² and del² are equal when f3=f1 */
2535: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2536: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2537: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2538: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2539: #ifdef NRCORIGINAL
2540: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2541: #else
2542: 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 2543: t= t- del*SQR(fp-fptt);
1.183 brouard 2544: #endif
1.202 brouard 2545: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2546: #ifdef DEBUG
1.181 brouard 2547: 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);
2548: 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 2549: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2550: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2551: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2552: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2553: 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);
2554: 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);
2555: #endif
1.183 brouard 2556: #ifdef POWELLORIGINAL
2557: if (t < 0.0) { /* Then we use it for new direction */
2558: #else
1.182 brouard 2559: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2560: 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 2561: 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 2562: 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 2563: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2564: }
1.181 brouard 2565: if (directest < 0.0) { /* Then we use it for new direction */
2566: #endif
1.191 brouard 2567: #ifdef DEBUGLINMIN
1.234 brouard 2568: printf("Before linmin in direction P%d-P0\n",n);
2569: for (j=1;j<=n;j++) {
2570: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2571: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2572: if(j % ncovmodel == 0){
2573: printf("\n");
2574: fprintf(ficlog,"\n");
2575: }
2576: }
1.224 brouard 2577: #endif
2578: #ifdef LINMINORIGINAL
1.234 brouard 2579: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.224 brouard 2580: #else
1.234 brouard 2581: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2582: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2583: #endif
1.234 brouard 2584:
1.191 brouard 2585: #ifdef DEBUGLINMIN
1.234 brouard 2586: for (j=1;j<=n;j++) {
2587: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2588: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2589: if(j % ncovmodel == 0){
2590: printf("\n");
2591: fprintf(ficlog,"\n");
2592: }
2593: }
1.224 brouard 2594: #endif
1.234 brouard 2595: for (j=1;j<=n;j++) {
2596: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2597: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2598: }
1.224 brouard 2599: #ifdef LINMINORIGINAL
2600: #else
1.234 brouard 2601: for (j=1, flatd=0;j<=n;j++) {
2602: if(flatdir[j]>0)
2603: flatd++;
2604: }
2605: if(flatd >0){
1.255 brouard 2606: printf("%d flat directions: ",flatd);
2607: fprintf(ficlog,"%d flat directions :",flatd);
1.234 brouard 2608: for (j=1;j<=n;j++) {
2609: if(flatdir[j]>0){
2610: printf("%d ",j);
2611: fprintf(ficlog,"%d ",j);
2612: }
2613: }
2614: printf("\n");
2615: fprintf(ficlog,"\n");
2616: }
1.191 brouard 2617: #endif
1.234 brouard 2618: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2619: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2620:
1.126 brouard 2621: #ifdef DEBUG
1.234 brouard 2622: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2623: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2624: for(j=1;j<=n;j++){
2625: printf(" %lf",xit[j]);
2626: fprintf(ficlog," %lf",xit[j]);
2627: }
2628: printf("\n");
2629: fprintf(ficlog,"\n");
1.126 brouard 2630: #endif
1.192 brouard 2631: } /* end of t or directest negative */
1.224 brouard 2632: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2633: #else
1.234 brouard 2634: } /* end if (fptt < fp) */
1.192 brouard 2635: #endif
1.225 brouard 2636: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.234 brouard 2637: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2638: #else
1.224 brouard 2639: #endif
1.234 brouard 2640: } /* loop iteration */
1.126 brouard 2641: }
1.234 brouard 2642:
1.126 brouard 2643: /**** Prevalence limit (stable or period prevalence) ****************/
1.234 brouard 2644:
1.235 brouard 2645: 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 2646: {
1.279 brouard 2647: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij
2648: * (and selected quantitative values in nres)
2649: * by left multiplying the unit
2650: * matrix by transitions matrix until convergence is reached with precision ftolpl
2651: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I
2652: * Wx is row vector: population in state 1, population in state 2, population dead
2653: * or prevalence in state 1, prevalence in state 2, 0
2654: * newm is the matrix after multiplications, its rows are identical at a factor.
2655: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl.
2656: * Output is prlim.
2657: * Initial matrix pimij
2658: */
1.206 brouard 2659: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2660: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2661: /* 0, 0 , 1} */
2662: /*
2663: * and after some iteration: */
2664: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2665: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2666: /* 0, 0 , 1} */
2667: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2668: /* {0.51571254859325999, 0.4842874514067399, */
2669: /* 0.51326036147820708, 0.48673963852179264} */
2670: /* If we start from prlim again, prlim tends to a constant matrix */
1.234 brouard 2671:
1.126 brouard 2672: int i, ii,j,k;
1.209 brouard 2673: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2674: /* double **matprod2(); */ /* test */
1.218 brouard 2675: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2676: double **newm;
1.209 brouard 2677: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2678: int ncvloop=0;
1.288 brouard 2679: int first=0;
1.169 brouard 2680:
1.209 brouard 2681: min=vector(1,nlstate);
2682: max=vector(1,nlstate);
2683: meandiff=vector(1,nlstate);
2684:
1.218 brouard 2685: /* Starting with matrix unity */
1.126 brouard 2686: for (ii=1;ii<=nlstate+ndeath;ii++)
2687: for (j=1;j<=nlstate+ndeath;j++){
2688: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2689: }
1.169 brouard 2690:
2691: cov[1]=1.;
2692:
2693: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2694: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2695: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2696: ncvloop++;
1.126 brouard 2697: newm=savm;
2698: /* Covariates have to be included here again */
1.138 brouard 2699: cov[2]=agefin;
1.187 brouard 2700: if(nagesqr==1)
2701: cov[3]= agefin*agefin;;
1.234 brouard 2702: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2703: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2704: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.235 brouard 2705: /* 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 2706: }
2707: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2708: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
1.235 brouard 2709: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2710: /* 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 2711: }
1.237 brouard 2712: for (k=1; k<=cptcovage;k++){ /* For product with age */
1.234 brouard 2713: if(Dummy[Tvar[Tage[k]]]){
2714: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2715: } else{
1.235 brouard 2716: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
1.234 brouard 2717: }
1.235 brouard 2718: /* 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 2719: }
1.237 brouard 2720: for (k=1; k<=cptcovprod;k++){ /* For product without age */
1.235 brouard 2721: /* 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 2722: if(Dummy[Tvard[k][1]==0]){
2723: if(Dummy[Tvard[k][2]==0]){
2724: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2725: }else{
2726: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2727: }
2728: }else{
2729: if(Dummy[Tvard[k][2]==0]){
2730: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2731: }else{
2732: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2733: }
2734: }
1.234 brouard 2735: }
1.138 brouard 2736: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2737: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2738: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2739: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2740: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2741: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2742: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2743:
1.126 brouard 2744: savm=oldm;
2745: oldm=newm;
1.209 brouard 2746:
2747: for(j=1; j<=nlstate; j++){
2748: max[j]=0.;
2749: min[j]=1.;
2750: }
2751: for(i=1;i<=nlstate;i++){
2752: sumnew=0;
2753: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2754: for(j=1; j<=nlstate; j++){
2755: prlim[i][j]= newm[i][j]/(1-sumnew);
2756: max[j]=FMAX(max[j],prlim[i][j]);
2757: min[j]=FMIN(min[j],prlim[i][j]);
2758: }
2759: }
2760:
1.126 brouard 2761: maxmax=0.;
1.209 brouard 2762: for(j=1; j<=nlstate; j++){
2763: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2764: maxmax=FMAX(maxmax,meandiff[j]);
2765: /* 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 2766: } /* j loop */
1.203 brouard 2767: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2768: /* 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 2769: if(maxmax < ftolpl){
1.209 brouard 2770: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2771: free_vector(min,1,nlstate);
2772: free_vector(max,1,nlstate);
2773: free_vector(meandiff,1,nlstate);
1.126 brouard 2774: return prlim;
2775: }
1.288 brouard 2776: } /* agefin loop */
1.208 brouard 2777: /* After some age loop it doesn't converge */
1.288 brouard 2778: if(!first){
2779: first=1;
2780: 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);
2781: }
2782: 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);
2783:
1.209 brouard 2784: /* 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); */
2785: free_vector(min,1,nlstate);
2786: free_vector(max,1,nlstate);
2787: free_vector(meandiff,1,nlstate);
1.208 brouard 2788:
1.169 brouard 2789: return prlim; /* should not reach here */
1.126 brouard 2790: }
2791:
1.217 brouard 2792:
2793: /**** Back Prevalence limit (stable or period prevalence) ****************/
2794:
1.218 brouard 2795: /* 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) */
2796: /* 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 2797: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres)
1.217 brouard 2798: {
1.264 brouard 2799: /* 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 2800: matrix by transitions matrix until convergence is reached with precision ftolpl */
2801: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2802: /* Wx is row vector: population in state 1, population in state 2, population dead */
2803: /* or prevalence in state 1, prevalence in state 2, 0 */
2804: /* newm is the matrix after multiplications, its rows are identical at a factor */
2805: /* Initial matrix pimij */
2806: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2807: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2808: /* 0, 0 , 1} */
2809: /*
2810: * and after some iteration: */
2811: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2812: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2813: /* 0, 0 , 1} */
2814: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2815: /* {0.51571254859325999, 0.4842874514067399, */
2816: /* 0.51326036147820708, 0.48673963852179264} */
2817: /* If we start from prlim again, prlim tends to a constant matrix */
2818:
2819: int i, ii,j,k;
1.247 brouard 2820: int first=0;
1.217 brouard 2821: double *min, *max, *meandiff, maxmax,sumnew=0.;
2822: /* double **matprod2(); */ /* test */
2823: double **out, cov[NCOVMAX+1], **bmij();
2824: double **newm;
1.218 brouard 2825: double **dnewm, **doldm, **dsavm; /* for use */
2826: double **oldm, **savm; /* for use */
2827:
1.217 brouard 2828: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2829: int ncvloop=0;
2830:
2831: min=vector(1,nlstate);
2832: max=vector(1,nlstate);
2833: meandiff=vector(1,nlstate);
2834:
1.266 brouard 2835: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2836: oldm=oldms; savm=savms;
2837:
2838: /* Starting with matrix unity */
2839: for (ii=1;ii<=nlstate+ndeath;ii++)
2840: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2841: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2842: }
2843:
2844: cov[1]=1.;
2845:
2846: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2847: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2848: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
1.288 brouard 2849: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2850: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2851: ncvloop++;
1.218 brouard 2852: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2853: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2854: /* Covariates have to be included here again */
2855: cov[2]=agefin;
2856: if(nagesqr==1)
2857: cov[3]= agefin*agefin;;
1.242 brouard 2858: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
2859: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
2860: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
1.264 brouard 2861: /* 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 2862: }
2863: /* for (k=1; k<=cptcovn;k++) { */
2864: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
2865: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
2866: /* /\* 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])]); *\/ */
2867: /* } */
2868: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
2869: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
2870: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
2871: /* 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]); */
2872: }
2873: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; */
2874: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */
2875: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\/ */
2876: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */
2877: for (k=1; k<=cptcovage;k++){ /* For product with age */
2878: if(Dummy[Tvar[Tage[k]]]){
2879: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2880: } else{
2881: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
2882: }
2883: /* 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]); */
2884: }
2885: for (k=1; k<=cptcovprod;k++){ /* For product without age */
2886: /* 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]); */
2887: if(Dummy[Tvard[k][1]==0]){
2888: if(Dummy[Tvard[k][2]==0]){
2889: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2890: }else{
2891: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k];
2892: }
2893: }else{
2894: if(Dummy[Tvard[k][2]==0]){
2895: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]];
2896: }else{
2897: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]];
2898: }
2899: }
1.217 brouard 2900: }
2901:
2902: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2903: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2904: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2905: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2906: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2907: /* ij should be linked to the correct index of cov */
2908: /* age and covariate values ij are in 'cov', but we need to pass
2909: * ij for the observed prevalence at age and status and covariate
2910: * number: prevacurrent[(int)agefin][ii][ij]
2911: */
2912: /* 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 *\/ */
2913: /* 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 *\/ */
2914: 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 2915: /* if((int)age == 86 || (int)age == 87){ */
1.266 brouard 2916: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */
2917: /* for(i=1; i<=nlstate+ndeath; i++) { */
2918: /* printf("%d newm= ",i); */
2919: /* for(j=1;j<=nlstate+ndeath;j++) { */
2920: /* printf("%f ",newm[i][j]); */
2921: /* } */
2922: /* printf("oldm * "); */
2923: /* for(j=1;j<=nlstate+ndeath;j++) { */
2924: /* printf("%f ",oldm[i][j]); */
2925: /* } */
1.268 brouard 2926: /* printf(" bmmij "); */
1.266 brouard 2927: /* for(j=1;j<=nlstate+ndeath;j++) { */
2928: /* printf("%f ",pmmij[i][j]); */
2929: /* } */
2930: /* printf("\n"); */
2931: /* } */
2932: /* } */
1.217 brouard 2933: savm=oldm;
2934: oldm=newm;
1.266 brouard 2935:
1.217 brouard 2936: for(j=1; j<=nlstate; j++){
2937: max[j]=0.;
2938: min[j]=1.;
2939: }
2940: for(j=1; j<=nlstate; j++){
2941: for(i=1;i<=nlstate;i++){
1.234 brouard 2942: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2943: bprlim[i][j]= newm[i][j];
2944: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2945: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2946: }
2947: }
1.218 brouard 2948:
1.217 brouard 2949: maxmax=0.;
2950: for(i=1; i<=nlstate; i++){
2951: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2952: maxmax=FMAX(maxmax,meandiff[i]);
2953: /* 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 2954: } /* i loop */
1.217 brouard 2955: *ncvyear= -( (int)age- (int)agefin);
1.268 brouard 2956: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2957: if(maxmax < ftolpl){
1.220 brouard 2958: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2959: free_vector(min,1,nlstate);
2960: free_vector(max,1,nlstate);
2961: free_vector(meandiff,1,nlstate);
2962: return bprlim;
2963: }
1.288 brouard 2964: } /* agefin loop */
1.217 brouard 2965: /* After some age loop it doesn't converge */
1.288 brouard 2966: if(!first){
1.247 brouard 2967: first=1;
2968: 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\
2969: 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);
2970: }
2971: 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 2972: 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);
2973: /* 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); */
2974: free_vector(min,1,nlstate);
2975: free_vector(max,1,nlstate);
2976: free_vector(meandiff,1,nlstate);
2977:
2978: return bprlim; /* should not reach here */
2979: }
2980:
1.126 brouard 2981: /*************** transition probabilities ***************/
2982:
2983: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2984: {
1.138 brouard 2985: /* According to parameters values stored in x and the covariate's values stored in cov,
1.266 brouard 2986: computes the probability to be observed in state j (after stepm years) being in state i by appying the
1.138 brouard 2987: model to the ncovmodel covariates (including constant and age).
2988: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2989: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2990: ncth covariate in the global vector x is given by the formula:
2991: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2992: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2993: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2994: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
1.266 brouard 2995: Outputs ps[i][j] or probability to be observed in j being in i according to
1.138 brouard 2996: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
1.266 brouard 2997: Sum on j ps[i][j] should equal to 1.
1.138 brouard 2998: */
2999: double s1, lnpijopii;
1.126 brouard 3000: /*double t34;*/
1.164 brouard 3001: int i,j, nc, ii, jj;
1.126 brouard 3002:
1.223 brouard 3003: for(i=1; i<= nlstate; i++){
3004: for(j=1; j<i;j++){
3005: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3006: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3007: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3008: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3009: }
3010: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3011: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3012: }
3013: for(j=i+1; j<=nlstate+ndeath;j++){
3014: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3015: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3016: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3017: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3018: }
3019: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3020: }
3021: }
1.218 brouard 3022:
1.223 brouard 3023: for(i=1; i<= nlstate; i++){
3024: s1=0;
3025: for(j=1; j<i; j++){
3026: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3027: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3028: }
3029: for(j=i+1; j<=nlstate+ndeath; j++){
3030: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3031: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3032: }
3033: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3034: ps[i][i]=1./(s1+1.);
3035: /* Computing other pijs */
3036: for(j=1; j<i; j++)
3037: ps[i][j]= exp(ps[i][j])*ps[i][i];
3038: for(j=i+1; j<=nlstate+ndeath; j++)
3039: ps[i][j]= exp(ps[i][j])*ps[i][i];
3040: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3041: } /* end i */
1.218 brouard 3042:
1.223 brouard 3043: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3044: for(jj=1; jj<= nlstate+ndeath; jj++){
3045: ps[ii][jj]=0;
3046: ps[ii][ii]=1;
3047: }
3048: }
1.294 brouard 3049:
3050:
1.223 brouard 3051: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3052: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3053: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3054: /* } */
3055: /* printf("\n "); */
3056: /* } */
3057: /* printf("\n ");printf("%lf ",cov[2]);*/
3058: /*
3059: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 3060: goto end;*/
1.266 brouard 3061: return ps; /* Pointer is unchanged since its call */
1.126 brouard 3062: }
3063:
1.218 brouard 3064: /*************** backward transition probabilities ***************/
3065:
3066: /* 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 ) */
3067: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
3068: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
3069: {
1.302 brouard 3070: /* 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 3071: * 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 3072: */
1.218 brouard 3073: int i, ii, j,k;
1.222 brouard 3074:
3075: double **out, **pmij();
3076: double sumnew=0.;
1.218 brouard 3077: double agefin;
1.292 brouard 3078: 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 3079: double **dnewm, **dsavm, **doldm;
3080: double **bbmij;
3081:
1.218 brouard 3082: doldm=ddoldms; /* global pointers */
1.222 brouard 3083: dnewm=ddnewms;
3084: dsavm=ddsavms;
3085:
3086: agefin=cov[2];
1.268 brouard 3087: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */
1.222 brouard 3088: /* bmij *//* age is cov[2], ij is included in cov, but we need for
1.266 brouard 3089: the observed prevalence (with this covariate ij) at beginning of transition */
3090: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.268 brouard 3091:
3092: /* P_x */
1.266 brouard 3093: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm */
1.268 brouard 3094: /* outputs pmmij which is a stochastic matrix in row */
3095:
3096: /* Diag(w_x) */
1.292 brouard 3097: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */
1.268 brouard 3098: sumnew=0.;
1.269 brouard 3099: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/
1.268 brouard 3100: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */
1.297 brouard 3101: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */
1.268 brouard 3102: sumnew+=prevacurrent[(int)agefin][ii][ij];
3103: }
3104: if(sumnew >0.01){ /* At least some value in the prevalence */
3105: for (ii=1;ii<=nlstate+ndeath;ii++){
3106: for (j=1;j<=nlstate+ndeath;j++)
1.269 brouard 3107: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0);
1.268 brouard 3108: }
3109: }else{
3110: for (ii=1;ii<=nlstate+ndeath;ii++){
3111: for (j=1;j<=nlstate+ndeath;j++)
3112: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0);
3113: }
3114: /* if(sumnew <0.9){ */
3115: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */
3116: /* } */
3117: }
3118: k3=0.0; /* We put the last diagonal to 0 */
3119: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){
3120: doldm[ii][ii]= k3;
3121: }
3122: /* End doldm, At the end doldm is diag[(w_i)] */
3123:
1.292 brouard 3124: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */
3125: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */
1.268 brouard 3126:
1.292 brouard 3127: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */
1.268 brouard 3128: /* 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 3129: for (j=1;j<=nlstate+ndeath;j++){
1.268 brouard 3130: sumnew=0.;
1.222 brouard 3131: for (ii=1;ii<=nlstate;ii++){
1.266 brouard 3132: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */
1.268 brouard 3133: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */
1.222 brouard 3134: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
1.268 brouard 3135: for (ii=1;ii<=nlstate+ndeath;ii++){
1.222 brouard 3136: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
1.268 brouard 3137: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3138: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
1.268 brouard 3139: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
1.222 brouard 3140: /* }else */
1.268 brouard 3141: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0);
3142: } /*End ii */
3143: } /* 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 */
3144:
1.292 brouard 3145: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */
1.268 brouard 3146: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
1.222 brouard 3147: /* end bmij */
1.266 brouard 3148: return ps; /*pointer is unchanged */
1.218 brouard 3149: }
1.217 brouard 3150: /*************** transition probabilities ***************/
3151:
1.218 brouard 3152: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 3153: {
3154: /* According to parameters values stored in x and the covariate's values stored in cov,
3155: computes the probability to be observed in state j being in state i by appying the
3156: model to the ncovmodel covariates (including constant and age).
3157: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
3158: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
3159: ncth covariate in the global vector x is given by the formula:
3160: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
3161: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
3162: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
3163: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
3164: Outputs ps[i][j] the probability to be observed in j being in j according to
3165: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
3166: */
3167: double s1, lnpijopii;
3168: /*double t34;*/
3169: int i,j, nc, ii, jj;
3170:
1.234 brouard 3171: for(i=1; i<= nlstate; i++){
3172: for(j=1; j<i;j++){
3173: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3174: /*lnpijopii += param[i][j][nc]*cov[nc];*/
3175: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
3176: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3177: }
3178: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3179: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
3180: }
3181: for(j=i+1; j<=nlstate+ndeath;j++){
3182: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
3183: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
3184: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
3185: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
3186: }
3187: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
3188: }
3189: }
3190:
3191: for(i=1; i<= nlstate; i++){
3192: s1=0;
3193: for(j=1; j<i; j++){
3194: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3195: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3196: }
3197: for(j=i+1; j<=nlstate+ndeath; j++){
3198: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
3199: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
3200: }
3201: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
3202: ps[i][i]=1./(s1+1.);
3203: /* Computing other pijs */
3204: for(j=1; j<i; j++)
3205: ps[i][j]= exp(ps[i][j])*ps[i][i];
3206: for(j=i+1; j<=nlstate+ndeath; j++)
3207: ps[i][j]= exp(ps[i][j])*ps[i][i];
3208: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
3209: } /* end i */
3210:
3211: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
3212: for(jj=1; jj<= nlstate+ndeath; jj++){
3213: ps[ii][jj]=0;
3214: ps[ii][ii]=1;
3215: }
3216: }
1.296 brouard 3217: /* Added for prevbcast */ /* Transposed matrix too */
1.234 brouard 3218: for(jj=1; jj<= nlstate+ndeath; jj++){
3219: s1=0.;
3220: for(ii=1; ii<= nlstate+ndeath; ii++){
3221: s1+=ps[ii][jj];
3222: }
3223: for(ii=1; ii<= nlstate; ii++){
3224: ps[ii][jj]=ps[ii][jj]/s1;
3225: }
3226: }
3227: /* Transposition */
3228: for(jj=1; jj<= nlstate+ndeath; jj++){
3229: for(ii=jj; ii<= nlstate+ndeath; ii++){
3230: s1=ps[ii][jj];
3231: ps[ii][jj]=ps[jj][ii];
3232: ps[jj][ii]=s1;
3233: }
3234: }
3235: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
3236: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
3237: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
3238: /* } */
3239: /* printf("\n "); */
3240: /* } */
3241: /* printf("\n ");printf("%lf ",cov[2]);*/
3242: /*
3243: for(i=1; i<= npar; i++) printf("%f ",x[i]);
3244: goto end;*/
3245: return ps;
1.217 brouard 3246: }
3247:
3248:
1.126 brouard 3249: /**************** Product of 2 matrices ******************/
3250:
1.145 brouard 3251: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 3252: {
3253: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
3254: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
3255: /* in, b, out are matrice of pointers which should have been initialized
3256: before: only the contents of out is modified. The function returns
3257: a pointer to pointers identical to out */
1.145 brouard 3258: int i, j, k;
1.126 brouard 3259: for(i=nrl; i<= nrh; i++)
1.145 brouard 3260: for(k=ncolol; k<=ncoloh; k++){
3261: out[i][k]=0.;
3262: for(j=ncl; j<=nch; j++)
3263: out[i][k] +=in[i][j]*b[j][k];
3264: }
1.126 brouard 3265: return out;
3266: }
3267:
3268:
3269: /************* Higher Matrix Product ***************/
3270:
1.235 brouard 3271: 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 3272: {
1.218 brouard 3273: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 3274: 'nhstepm*hstepm*stepm' months (i.e. until
3275: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3276: nhstepm*hstepm matrices.
3277: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3278: (typically every 2 years instead of every month which is too big
3279: for the memory).
3280: Model is determined by parameters x and covariates have to be
3281: included manually here.
3282:
3283: */
3284:
3285: int i, j, d, h, k;
1.131 brouard 3286: double **out, cov[NCOVMAX+1];
1.126 brouard 3287: double **newm;
1.187 brouard 3288: double agexact;
1.214 brouard 3289: double agebegin, ageend;
1.126 brouard 3290:
3291: /* Hstepm could be zero and should return the unit matrix */
3292: for (i=1;i<=nlstate+ndeath;i++)
3293: for (j=1;j<=nlstate+ndeath;j++){
3294: oldm[i][j]=(i==j ? 1.0 : 0.0);
3295: po[i][j][0]=(i==j ? 1.0 : 0.0);
3296: }
3297: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3298: for(h=1; h <=nhstepm; h++){
3299: for(d=1; d <=hstepm; d++){
3300: newm=savm;
3301: /* Covariates have to be included here again */
3302: cov[1]=1.;
1.214 brouard 3303: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 3304: cov[2]=agexact;
3305: if(nagesqr==1)
1.227 brouard 3306: cov[3]= agexact*agexact;
1.235 brouard 3307: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */
3308: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */
3309: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3310: /* 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)); */
3311: }
3312: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3313: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3314: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3315: /* 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]); */
3316: }
3317: for (k=1; k<=cptcovage;k++){
3318: if(Dummy[Tvar[Tage[k]]]){
3319: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3320: } else{
3321: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3322: }
3323: /* 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]); */
3324: }
3325: for (k=1; k<=cptcovprod;k++){ /* */
3326: /* 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]); */
3327: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
3328: }
3329: /* for (k=1; k<=cptcovn;k++) */
3330: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3331: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */
3332: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */
3333: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */
3334: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */
1.227 brouard 3335:
3336:
1.126 brouard 3337: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3338: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 3339: /* right multiplication of oldm by the current matrix */
1.126 brouard 3340: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
3341: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 3342: /* if((int)age == 70){ */
3343: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3344: /* for(i=1; i<=nlstate+ndeath; i++) { */
3345: /* printf("%d pmmij ",i); */
3346: /* for(j=1;j<=nlstate+ndeath;j++) { */
3347: /* printf("%f ",pmmij[i][j]); */
3348: /* } */
3349: /* printf(" oldm "); */
3350: /* for(j=1;j<=nlstate+ndeath;j++) { */
3351: /* printf("%f ",oldm[i][j]); */
3352: /* } */
3353: /* printf("\n"); */
3354: /* } */
3355: /* } */
1.126 brouard 3356: savm=oldm;
3357: oldm=newm;
3358: }
3359: for(i=1; i<=nlstate+ndeath; i++)
3360: for(j=1;j<=nlstate+ndeath;j++) {
1.267 brouard 3361: po[i][j][h]=newm[i][j];
3362: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 3363: }
1.128 brouard 3364: /*printf("h=%d ",h);*/
1.126 brouard 3365: } /* end h */
1.267 brouard 3366: /* printf("\n H=%d \n",h); */
1.126 brouard 3367: return po;
3368: }
3369:
1.217 brouard 3370: /************* Higher Back Matrix Product ***************/
1.218 brouard 3371: /* 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 3372: 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 3373: {
1.266 brouard 3374: /* For a combination of dummy covariate ij, computes the transition matrix starting at age 'age' over
1.217 brouard 3375: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 3376: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
3377: nhstepm*hstepm matrices.
3378: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
3379: (typically every 2 years instead of every month which is too big
1.217 brouard 3380: for the memory).
1.218 brouard 3381: Model is determined by parameters x and covariates have to be
1.266 brouard 3382: included manually here. Then we use a call to bmij(x and cov)
3383: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output
1.222 brouard 3384: */
1.217 brouard 3385:
3386: int i, j, d, h, k;
1.266 brouard 3387: double **out, cov[NCOVMAX+1], **bmij();
3388: double **newm, ***newmm;
1.217 brouard 3389: double agexact;
3390: double agebegin, ageend;
1.222 brouard 3391: double **oldm, **savm;
1.217 brouard 3392:
1.266 brouard 3393: newmm=po; /* To be saved */
3394: oldm=oldms;savm=savms; /* Global pointers */
1.217 brouard 3395: /* Hstepm could be zero and should return the unit matrix */
3396: for (i=1;i<=nlstate+ndeath;i++)
3397: for (j=1;j<=nlstate+ndeath;j++){
3398: oldm[i][j]=(i==j ? 1.0 : 0.0);
3399: po[i][j][0]=(i==j ? 1.0 : 0.0);
3400: }
3401: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
3402: for(h=1; h <=nhstepm; h++){
3403: for(d=1; d <=hstepm; d++){
3404: newm=savm;
3405: /* Covariates have to be included here again */
3406: cov[1]=1.;
1.271 brouard 3407: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */
1.217 brouard 3408: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
3409: cov[2]=agexact;
3410: if(nagesqr==1)
1.222 brouard 3411: cov[3]= agexact*agexact;
1.266 brouard 3412: for (k=1; k<=cptcovn;k++){
3413: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */
3414: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */
3415: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)];
3416: /* 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)); */
3417: }
1.267 brouard 3418: for (k=1; k<=nsq;k++) { /* For single varying covariates only */
3419: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */
3420: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k];
3421: /* 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]); */
3422: }
3423: for (k=1; k<=cptcovage;k++){ /* Should start at cptcovn+1 */
3424: if(Dummy[Tvar[Tage[k]]]){
3425: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
3426: } else{
3427: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k];
3428: }
3429: /* 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]); */
3430: }
3431: for (k=1; k<=cptcovprod;k++){ /* Useless because included in cptcovn */
1.222 brouard 3432: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.267 brouard 3433: }
1.217 brouard 3434: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
3435: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.267 brouard 3436:
1.218 brouard 3437: /* Careful transposed matrix */
1.266 brouard 3438: /* age is in cov[2], prevacurrent at beginning of transition. */
1.218 brouard 3439: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 3440: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 3441: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 3442: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 3443: /* if((int)age == 70){ */
3444: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
3445: /* for(i=1; i<=nlstate+ndeath; i++) { */
3446: /* printf("%d pmmij ",i); */
3447: /* for(j=1;j<=nlstate+ndeath;j++) { */
3448: /* printf("%f ",pmmij[i][j]); */
3449: /* } */
3450: /* printf(" oldm "); */
3451: /* for(j=1;j<=nlstate+ndeath;j++) { */
3452: /* printf("%f ",oldm[i][j]); */
3453: /* } */
3454: /* printf("\n"); */
3455: /* } */
3456: /* } */
3457: savm=oldm;
3458: oldm=newm;
3459: }
3460: for(i=1; i<=nlstate+ndeath; i++)
3461: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 3462: po[i][j][h]=newm[i][j];
1.268 brouard 3463: /* if(h==nhstepm) */
3464: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */
1.217 brouard 3465: }
1.268 brouard 3466: /* printf("h=%d %.1f ",h, agexact); */
1.217 brouard 3467: } /* end h */
1.268 brouard 3468: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */
1.217 brouard 3469: return po;
3470: }
3471:
3472:
1.162 brouard 3473: #ifdef NLOPT
3474: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
3475: double fret;
3476: double *xt;
3477: int j;
3478: myfunc_data *d2 = (myfunc_data *) pd;
3479: /* xt = (p1-1); */
3480: xt=vector(1,n);
3481: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
3482:
3483: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
3484: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
3485: printf("Function = %.12lf ",fret);
3486: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
3487: printf("\n");
3488: free_vector(xt,1,n);
3489: return fret;
3490: }
3491: #endif
1.126 brouard 3492:
3493: /*************** log-likelihood *************/
3494: double func( double *x)
3495: {
1.226 brouard 3496: int i, ii, j, k, mi, d, kk;
3497: int ioffset=0;
3498: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
3499: double **out;
3500: double lli; /* Individual log likelihood */
3501: int s1, s2;
1.228 brouard 3502: 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 3503: double bbh, survp;
3504: long ipmx;
3505: double agexact;
3506: /*extern weight */
3507: /* We are differentiating ll according to initial status */
3508: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3509: /*for(i=1;i<imx;i++)
3510: printf(" %d\n",s[4][i]);
3511: */
1.162 brouard 3512:
1.226 brouard 3513: ++countcallfunc;
1.162 brouard 3514:
1.226 brouard 3515: cov[1]=1.;
1.126 brouard 3516:
1.226 brouard 3517: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3518: ioffset=0;
1.226 brouard 3519: if(mle==1){
3520: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3521: /* Computes the values of the ncovmodel covariates of the model
3522: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
3523: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
3524: to be observed in j being in i according to the model.
3525: */
1.243 brouard 3526: ioffset=2+nagesqr ;
1.233 brouard 3527: /* Fixed */
1.234 brouard 3528: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */
3529: 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)*/
3530: }
1.226 brouard 3531: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
3532: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
3533: has been calculated etc */
3534: /* For an individual i, wav[i] gives the number of effective waves */
3535: /* We compute the contribution to Likelihood of each effective transition
3536: mw[mi][i] is real wave of the mi th effectve wave */
3537: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
3538: s2=s[mw[mi+1][i]][i];
3539: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
3540: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
3541: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
3542: */
3543: for(mi=1; mi<= wav[i]-1; mi++){
1.234 brouard 3544: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3545: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3546: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
1.234 brouard 3547: }
3548: for (ii=1;ii<=nlstate+ndeath;ii++)
3549: for (j=1;j<=nlstate+ndeath;j++){
3550: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3551: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3552: }
3553: for(d=0; d<dh[mi][i]; d++){
3554: newm=savm;
3555: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3556: cov[2]=agexact;
3557: if(nagesqr==1)
3558: cov[3]= agexact*agexact; /* Should be changed here */
3559: for (kk=1; kk<=cptcovage;kk++) {
1.242 brouard 3560: if(!FixedV[Tvar[Tage[kk]]])
1.234 brouard 3561: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
1.242 brouard 3562: else
3563: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
1.234 brouard 3564: }
3565: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3566: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3567: savm=oldm;
3568: oldm=newm;
3569: } /* end mult */
3570:
3571: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
3572: /* But now since version 0.9 we anticipate for bias at large stepm.
3573: * If stepm is larger than one month (smallest stepm) and if the exact delay
3574: * (in months) between two waves is not a multiple of stepm, we rounded to
3575: * the nearest (and in case of equal distance, to the lowest) interval but now
3576: * we keep into memory the bias bh[mi][i] and also the previous matrix product
3577: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
3578: * probability in order to take into account the bias as a fraction of the way
1.231 brouard 3579: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
3580: * -stepm/2 to stepm/2 .
3581: * For stepm=1 the results are the same as for previous versions of Imach.
3582: * For stepm > 1 the results are less biased than in previous versions.
3583: */
1.234 brouard 3584: s1=s[mw[mi][i]][i];
3585: s2=s[mw[mi+1][i]][i];
3586: bbh=(double)bh[mi][i]/(double)stepm;
3587: /* bias bh is positive if real duration
3588: * is higher than the multiple of stepm and negative otherwise.
3589: */
3590: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
3591: if( s2 > nlstate){
3592: /* i.e. if s2 is a death state and if the date of death is known
3593: then the contribution to the likelihood is the probability to
3594: die between last step unit time and current step unit time,
3595: which is also equal to probability to die before dh
3596: minus probability to die before dh-stepm .
3597: In version up to 0.92 likelihood was computed
3598: as if date of death was unknown. Death was treated as any other
3599: health state: the date of the interview describes the actual state
3600: and not the date of a change in health state. The former idea was
3601: to consider that at each interview the state was recorded
3602: (healthy, disable or death) and IMaCh was corrected; but when we
3603: introduced the exact date of death then we should have modified
3604: the contribution of an exact death to the likelihood. This new
3605: contribution is smaller and very dependent of the step unit
3606: stepm. It is no more the probability to die between last interview
3607: and month of death but the probability to survive from last
3608: interview up to one month before death multiplied by the
3609: probability to die within a month. Thanks to Chris
3610: Jackson for correcting this bug. Former versions increased
3611: mortality artificially. The bad side is that we add another loop
3612: which slows down the processing. The difference can be up to 10%
3613: lower mortality.
3614: */
3615: /* If, at the beginning of the maximization mostly, the
3616: cumulative probability or probability to be dead is
3617: constant (ie = 1) over time d, the difference is equal to
3618: 0. out[s1][3] = savm[s1][3]: probability, being at state
3619: s1 at precedent wave, to be dead a month before current
3620: wave is equal to probability, being at state s1 at
3621: precedent wave, to be dead at mont of the current
3622: wave. Then the observed probability (that this person died)
3623: is null according to current estimated parameter. In fact,
3624: it should be very low but not zero otherwise the log go to
3625: infinity.
3626: */
1.183 brouard 3627: /* #ifdef INFINITYORIGINAL */
3628: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3629: /* #else */
3630: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3631: /* lli=log(mytinydouble); */
3632: /* else */
3633: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3634: /* #endif */
1.226 brouard 3635: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3636:
1.226 brouard 3637: } else if ( s2==-1 ) { /* alive */
3638: for (j=1,survp=0. ; j<=nlstate; j++)
3639: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3640: /*survp += out[s1][j]; */
3641: lli= log(survp);
3642: }
3643: else if (s2==-4) {
3644: for (j=3,survp=0. ; j<=nlstate; j++)
3645: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3646: lli= log(survp);
3647: }
3648: else if (s2==-5) {
3649: for (j=1,survp=0. ; j<=2; j++)
3650: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3651: lli= log(survp);
3652: }
3653: else{
3654: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3655: /* 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 */
3656: }
3657: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3658: /*if(lli ==000.0)*/
3659: /*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); */
3660: ipmx +=1;
3661: sw += weight[i];
3662: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3663: /* if (lli < log(mytinydouble)){ */
3664: /* 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); */
3665: /* 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]); */
3666: /* } */
3667: } /* end of wave */
3668: } /* end of individual */
3669: } else if(mle==2){
3670: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3671: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3672: for(mi=1; mi<= wav[i]-1; mi++){
3673: for (ii=1;ii<=nlstate+ndeath;ii++)
3674: for (j=1;j<=nlstate+ndeath;j++){
3675: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3676: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3677: }
3678: for(d=0; d<=dh[mi][i]; d++){
3679: newm=savm;
3680: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3681: cov[2]=agexact;
3682: if(nagesqr==1)
3683: cov[3]= agexact*agexact;
3684: for (kk=1; kk<=cptcovage;kk++) {
3685: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3686: }
3687: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3688: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3689: savm=oldm;
3690: oldm=newm;
3691: } /* end mult */
3692:
3693: s1=s[mw[mi][i]][i];
3694: s2=s[mw[mi+1][i]][i];
3695: bbh=(double)bh[mi][i]/(double)stepm;
3696: 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 */
3697: ipmx +=1;
3698: sw += weight[i];
3699: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3700: } /* end of wave */
3701: } /* end of individual */
3702: } else if(mle==3){ /* exponential inter-extrapolation */
3703: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3704: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3705: for(mi=1; mi<= wav[i]-1; mi++){
3706: for (ii=1;ii<=nlstate+ndeath;ii++)
3707: for (j=1;j<=nlstate+ndeath;j++){
3708: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3709: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3710: }
3711: for(d=0; d<dh[mi][i]; d++){
3712: newm=savm;
3713: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3714: cov[2]=agexact;
3715: if(nagesqr==1)
3716: cov[3]= agexact*agexact;
3717: for (kk=1; kk<=cptcovage;kk++) {
3718: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3719: }
3720: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3721: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3722: savm=oldm;
3723: oldm=newm;
3724: } /* end mult */
3725:
3726: s1=s[mw[mi][i]][i];
3727: s2=s[mw[mi+1][i]][i];
3728: bbh=(double)bh[mi][i]/(double)stepm;
3729: 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 */
3730: ipmx +=1;
3731: sw += weight[i];
3732: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3733: } /* end of wave */
3734: } /* end of individual */
3735: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3736: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3737: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3738: for(mi=1; mi<= wav[i]-1; mi++){
3739: for (ii=1;ii<=nlstate+ndeath;ii++)
3740: for (j=1;j<=nlstate+ndeath;j++){
3741: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3742: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3743: }
3744: for(d=0; d<dh[mi][i]; d++){
3745: newm=savm;
3746: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3747: cov[2]=agexact;
3748: if(nagesqr==1)
3749: cov[3]= agexact*agexact;
3750: for (kk=1; kk<=cptcovage;kk++) {
3751: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3752: }
1.126 brouard 3753:
1.226 brouard 3754: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3755: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3756: savm=oldm;
3757: oldm=newm;
3758: } /* end mult */
3759:
3760: s1=s[mw[mi][i]][i];
3761: s2=s[mw[mi+1][i]][i];
3762: if( s2 > nlstate){
3763: lli=log(out[s1][s2] - savm[s1][s2]);
3764: } else if ( s2==-1 ) { /* alive */
3765: for (j=1,survp=0. ; j<=nlstate; j++)
3766: survp += out[s1][j];
3767: lli= log(survp);
3768: }else{
3769: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3770: }
3771: ipmx +=1;
3772: sw += weight[i];
3773: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3774: /* 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 3775: } /* end of wave */
3776: } /* end of individual */
3777: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3778: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3779: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3780: for(mi=1; mi<= wav[i]-1; mi++){
3781: for (ii=1;ii<=nlstate+ndeath;ii++)
3782: for (j=1;j<=nlstate+ndeath;j++){
3783: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3784: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3785: }
3786: for(d=0; d<dh[mi][i]; d++){
3787: newm=savm;
3788: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3789: cov[2]=agexact;
3790: if(nagesqr==1)
3791: cov[3]= agexact*agexact;
3792: for (kk=1; kk<=cptcovage;kk++) {
3793: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3794: }
1.126 brouard 3795:
1.226 brouard 3796: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3797: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3798: savm=oldm;
3799: oldm=newm;
3800: } /* end mult */
3801:
3802: s1=s[mw[mi][i]][i];
3803: s2=s[mw[mi+1][i]][i];
3804: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3805: ipmx +=1;
3806: sw += weight[i];
3807: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3808: /*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]);*/
3809: } /* end of wave */
3810: } /* end of individual */
3811: } /* End of if */
3812: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3813: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3814: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3815: return -l;
1.126 brouard 3816: }
3817:
3818: /*************** log-likelihood *************/
3819: double funcone( double *x)
3820: {
1.228 brouard 3821: /* Same as func but slower because of a lot of printf and if */
1.126 brouard 3822: int i, ii, j, k, mi, d, kk;
1.228 brouard 3823: int ioffset=0;
1.131 brouard 3824: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3825: double **out;
3826: double lli; /* Individual log likelihood */
3827: double llt;
3828: int s1, s2;
1.228 brouard 3829: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */
3830:
1.126 brouard 3831: double bbh, survp;
1.187 brouard 3832: double agexact;
1.214 brouard 3833: double agebegin, ageend;
1.126 brouard 3834: /*extern weight */
3835: /* We are differentiating ll according to initial status */
3836: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3837: /*for(i=1;i<imx;i++)
3838: printf(" %d\n",s[4][i]);
3839: */
3840: cov[1]=1.;
3841:
3842: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3843: ioffset=0;
3844: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.243 brouard 3845: /* ioffset=2+nagesqr+cptcovage; */
3846: ioffset=2+nagesqr;
1.232 brouard 3847: /* Fixed */
1.224 brouard 3848: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.232 brouard 3849: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */
1.311 ! brouard 3850: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */
1.232 brouard 3851: 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)*/
3852: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */
3853: /* cov[2+6]=covar[Tvar[6]][i]; */
3854: /* cov[2+6]=covar[2][i]; V2 */
3855: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */
3856: /* cov[2+7]=covar[Tvar[7]][i]; */
3857: /* cov[2+7]=covar[7][i]; V7=V1*V2 */
3858: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */
3859: /* cov[2+9]=covar[Tvar[9]][i]; */
3860: /* cov[2+9]=covar[1][i]; V1 */
1.225 brouard 3861: }
1.232 brouard 3862: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */
3863: /* 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?)*\/ */
3864: /* } */
1.231 brouard 3865: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */
3866: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */
3867: /* } */
1.225 brouard 3868:
1.233 brouard 3869:
3870: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.232 brouard 3871: /* Wave varying (but not age varying) */
3872: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/
1.242 brouard 3873: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */
3874: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i];
3875: }
1.232 brouard 3876: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */
1.242 brouard 3877: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3878: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */
3879: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */
3880: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */
3881: /* 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 3882: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */
1.242 brouard 3883: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */
3884: /* /\* 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]); *\/ */
3885: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */
1.232 brouard 3886: /* } */
1.126 brouard 3887: for (ii=1;ii<=nlstate+ndeath;ii++)
1.242 brouard 3888: for (j=1;j<=nlstate+ndeath;j++){
3889: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3890: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3891: }
1.214 brouard 3892:
3893: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3894: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3895: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.247 brouard 3896: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */
1.242 brouard 3897: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3898: and mw[mi+1][i]. dh depends on stepm.*/
3899: newm=savm;
1.247 brouard 3900: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */
1.242 brouard 3901: cov[2]=agexact;
3902: if(nagesqr==1)
3903: cov[3]= agexact*agexact;
3904: for (kk=1; kk<=cptcovage;kk++) {
3905: if(!FixedV[Tvar[Tage[kk]]])
3906: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3907: else
3908: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact;
3909: }
3910: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3911: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3912: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3913: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3914: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3915: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3916: savm=oldm;
3917: oldm=newm;
1.126 brouard 3918: } /* end mult */
3919:
3920: s1=s[mw[mi][i]][i];
3921: s2=s[mw[mi+1][i]][i];
1.217 brouard 3922: /* if(s2==-1){ */
1.268 brouard 3923: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */
1.217 brouard 3924: /* /\* exit(1); *\/ */
3925: /* } */
1.126 brouard 3926: bbh=(double)bh[mi][i]/(double)stepm;
3927: /* bias is positive if real duration
3928: * is higher than the multiple of stepm and negative otherwise.
3929: */
3930: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.242 brouard 3931: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3932: } else if ( s2==-1 ) { /* alive */
1.242 brouard 3933: for (j=1,survp=0. ; j<=nlstate; j++)
3934: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3935: lli= log(survp);
1.126 brouard 3936: }else if (mle==1){
1.242 brouard 3937: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3938: } else if(mle==2){
1.242 brouard 3939: 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 3940: } else if(mle==3){ /* exponential inter-extrapolation */
1.242 brouard 3941: 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 3942: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.242 brouard 3943: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3944: } else{ /* mle=0 back to 1 */
1.242 brouard 3945: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3946: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3947: } /* End of if */
3948: ipmx +=1;
3949: sw += weight[i];
3950: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3951: /*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 3952: if(globpr){
1.246 brouard 3953: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3954: %11.6f %11.6f %11.6f ", \
1.242 brouard 3955: 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 3956: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2]));
1.242 brouard 3957: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3958: llt +=ll[k]*gipmx/gsw;
3959: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3960: }
3961: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3962: }
1.232 brouard 3963: } /* end of wave */
3964: } /* end of individual */
3965: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3966: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3967: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3968: if(globpr==0){ /* First time we count the contributions and weights */
3969: gipmx=ipmx;
3970: gsw=sw;
3971: }
3972: return -l;
1.126 brouard 3973: }
3974:
3975:
3976: /*************** function likelione ***********/
1.292 brouard 3977: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double []))
1.126 brouard 3978: {
3979: /* This routine should help understanding what is done with
3980: the selection of individuals/waves and
3981: to check the exact contribution to the likelihood.
3982: Plotting could be done.
3983: */
3984: int k;
3985:
3986: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3987: strcpy(fileresilk,"ILK_");
1.202 brouard 3988: strcat(fileresilk,fileresu);
1.126 brouard 3989: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3990: printf("Problem with resultfile: %s\n", fileresilk);
3991: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3992: }
1.214 brouard 3993: 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");
3994: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3995: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3996: for(k=1; k<=nlstate; k++)
3997: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3998: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3999: }
4000:
1.292 brouard 4001: *fretone=(*func)(p);
1.126 brouard 4002: if(*globpri !=0){
4003: fclose(ficresilk);
1.205 brouard 4004: if (mle ==0)
4005: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
4006: else if(mle >=1)
4007: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
4008: 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 4009: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model);
1.208 brouard 4010:
4011: for (k=1; k<= nlstate ; k++) {
1.211 brouard 4012: 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 4013: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
4014: }
1.207 brouard 4015: 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 4016: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4017: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 4018: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 4019: fflush(fichtm);
1.205 brouard 4020: }
1.126 brouard 4021: return;
4022: }
4023:
4024:
4025: /*********** Maximum Likelihood Estimation ***************/
4026:
4027: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
4028: {
1.165 brouard 4029: int i,j, iter=0;
1.126 brouard 4030: double **xi;
4031: double fret;
4032: double fretone; /* Only one call to likelihood */
4033: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 4034:
4035: #ifdef NLOPT
4036: int creturn;
4037: nlopt_opt opt;
4038: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
4039: double *lb;
4040: double minf; /* the minimum objective value, upon return */
4041: double * p1; /* Shifted parameters from 0 instead of 1 */
4042: myfunc_data dinst, *d = &dinst;
4043: #endif
4044:
4045:
1.126 brouard 4046: xi=matrix(1,npar,1,npar);
4047: for (i=1;i<=npar;i++)
4048: for (j=1;j<=npar;j++)
4049: xi[i][j]=(i==j ? 1.0 : 0.0);
4050: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 4051: strcpy(filerespow,"POW_");
1.126 brouard 4052: strcat(filerespow,fileres);
4053: if((ficrespow=fopen(filerespow,"w"))==NULL) {
4054: printf("Problem with resultfile: %s\n", filerespow);
4055: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
4056: }
4057: fprintf(ficrespow,"# Powell\n# iter -2*LL");
4058: for (i=1;i<=nlstate;i++)
4059: for(j=1;j<=nlstate+ndeath;j++)
4060: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
4061: fprintf(ficrespow,"\n");
1.162 brouard 4062: #ifdef POWELL
1.126 brouard 4063: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 4064: #endif
1.126 brouard 4065:
1.162 brouard 4066: #ifdef NLOPT
4067: #ifdef NEWUOA
4068: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
4069: #else
4070: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
4071: #endif
4072: lb=vector(0,npar-1);
4073: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
4074: nlopt_set_lower_bounds(opt, lb);
4075: nlopt_set_initial_step1(opt, 0.1);
4076:
4077: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
4078: d->function = func;
4079: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
4080: nlopt_set_min_objective(opt, myfunc, d);
4081: nlopt_set_xtol_rel(opt, ftol);
4082: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
4083: printf("nlopt failed! %d\n",creturn);
4084: }
4085: else {
4086: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
4087: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
4088: iter=1; /* not equal */
4089: }
4090: nlopt_destroy(opt);
4091: #endif
1.126 brouard 4092: free_matrix(xi,1,npar,1,npar);
4093: fclose(ficrespow);
1.203 brouard 4094: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
4095: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 4096: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 4097:
4098: }
4099:
4100: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 4101: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 4102: {
4103: double **a,**y,*x,pd;
1.203 brouard 4104: /* double **hess; */
1.164 brouard 4105: int i, j;
1.126 brouard 4106: int *indx;
4107:
4108: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 4109: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 4110: void lubksb(double **a, int npar, int *indx, double b[]) ;
4111: void ludcmp(double **a, int npar, int *indx, double *d) ;
4112: double gompertz(double p[]);
1.203 brouard 4113: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 4114:
4115: printf("\nCalculation of the hessian matrix. Wait...\n");
4116: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
4117: for (i=1;i<=npar;i++){
1.203 brouard 4118: printf("%d-",i);fflush(stdout);
4119: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 4120:
4121: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
4122:
4123: /* printf(" %f ",p[i]);
4124: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
4125: }
4126:
4127: for (i=1;i<=npar;i++) {
4128: for (j=1;j<=npar;j++) {
4129: if (j>i) {
1.203 brouard 4130: printf(".%d-%d",i,j);fflush(stdout);
4131: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
4132: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 4133:
4134: hess[j][i]=hess[i][j];
4135: /*printf(" %lf ",hess[i][j]);*/
4136: }
4137: }
4138: }
4139: printf("\n");
4140: fprintf(ficlog,"\n");
4141:
4142: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
4143: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
4144:
4145: a=matrix(1,npar,1,npar);
4146: y=matrix(1,npar,1,npar);
4147: x=vector(1,npar);
4148: indx=ivector(1,npar);
4149: for (i=1;i<=npar;i++)
4150: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
4151: ludcmp(a,npar,indx,&pd);
4152:
4153: for (j=1;j<=npar;j++) {
4154: for (i=1;i<=npar;i++) x[i]=0;
4155: x[j]=1;
4156: lubksb(a,npar,indx,x);
4157: for (i=1;i<=npar;i++){
4158: matcov[i][j]=x[i];
4159: }
4160: }
4161:
4162: printf("\n#Hessian matrix#\n");
4163: fprintf(ficlog,"\n#Hessian matrix#\n");
4164: for (i=1;i<=npar;i++) {
4165: for (j=1;j<=npar;j++) {
1.203 brouard 4166: printf("%.6e ",hess[i][j]);
4167: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 4168: }
4169: printf("\n");
4170: fprintf(ficlog,"\n");
4171: }
4172:
1.203 brouard 4173: /* printf("\n#Covariance matrix#\n"); */
4174: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
4175: /* for (i=1;i<=npar;i++) { */
4176: /* for (j=1;j<=npar;j++) { */
4177: /* printf("%.6e ",matcov[i][j]); */
4178: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
4179: /* } */
4180: /* printf("\n"); */
4181: /* fprintf(ficlog,"\n"); */
4182: /* } */
4183:
1.126 brouard 4184: /* Recompute Inverse */
1.203 brouard 4185: /* for (i=1;i<=npar;i++) */
4186: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
4187: /* ludcmp(a,npar,indx,&pd); */
4188:
4189: /* printf("\n#Hessian matrix recomputed#\n"); */
4190:
4191: /* for (j=1;j<=npar;j++) { */
4192: /* for (i=1;i<=npar;i++) x[i]=0; */
4193: /* x[j]=1; */
4194: /* lubksb(a,npar,indx,x); */
4195: /* for (i=1;i<=npar;i++){ */
4196: /* y[i][j]=x[i]; */
4197: /* printf("%.3e ",y[i][j]); */
4198: /* fprintf(ficlog,"%.3e ",y[i][j]); */
4199: /* } */
4200: /* printf("\n"); */
4201: /* fprintf(ficlog,"\n"); */
4202: /* } */
4203:
4204: /* Verifying the inverse matrix */
4205: #ifdef DEBUGHESS
4206: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 4207:
1.203 brouard 4208: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
4209: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 4210:
4211: for (j=1;j<=npar;j++) {
4212: for (i=1;i<=npar;i++){
1.203 brouard 4213: printf("%.2f ",y[i][j]);
4214: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 4215: }
4216: printf("\n");
4217: fprintf(ficlog,"\n");
4218: }
1.203 brouard 4219: #endif
1.126 brouard 4220:
4221: free_matrix(a,1,npar,1,npar);
4222: free_matrix(y,1,npar,1,npar);
4223: free_vector(x,1,npar);
4224: free_ivector(indx,1,npar);
1.203 brouard 4225: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 4226:
4227:
4228: }
4229:
4230: /*************** hessian matrix ****************/
4231: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 4232: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 4233: int i;
4234: int l=1, lmax=20;
1.203 brouard 4235: double k1,k2, res, fx;
1.132 brouard 4236: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 4237: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
4238: int k=0,kmax=10;
4239: double l1;
4240:
4241: fx=func(x);
4242: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 4243: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 4244: l1=pow(10,l);
4245: delts=delt;
4246: for(k=1 ; k <kmax; k=k+1){
4247: delt = delta*(l1*k);
4248: p2[theta]=x[theta] +delt;
1.145 brouard 4249: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 4250: p2[theta]=x[theta]-delt;
4251: k2=func(p2)-fx;
4252: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 4253: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 4254:
1.203 brouard 4255: #ifdef DEBUGHESSII
1.126 brouard 4256: 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);
4257: 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);
4258: #endif
4259: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
4260: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
4261: k=kmax;
4262: }
4263: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 4264: k=kmax; l=lmax*10;
1.126 brouard 4265: }
4266: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
4267: delts=delt;
4268: }
1.203 brouard 4269: } /* End loop k */
1.126 brouard 4270: }
4271: delti[theta]=delts;
4272: return res;
4273:
4274: }
4275:
1.203 brouard 4276: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 4277: {
4278: int i;
1.164 brouard 4279: int l=1, lmax=20;
1.126 brouard 4280: double k1,k2,k3,k4,res,fx;
1.132 brouard 4281: double p2[MAXPARM+1];
1.203 brouard 4282: int k, kmax=1;
4283: double v1, v2, cv12, lc1, lc2;
1.208 brouard 4284:
4285: int firstime=0;
1.203 brouard 4286:
1.126 brouard 4287: fx=func(x);
1.203 brouard 4288: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 4289: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 4290: p2[thetai]=x[thetai]+delti[thetai]*k;
4291: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4292: k1=func(p2)-fx;
4293:
1.203 brouard 4294: p2[thetai]=x[thetai]+delti[thetai]*k;
4295: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4296: k2=func(p2)-fx;
4297:
1.203 brouard 4298: p2[thetai]=x[thetai]-delti[thetai]*k;
4299: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 4300: k3=func(p2)-fx;
4301:
1.203 brouard 4302: p2[thetai]=x[thetai]-delti[thetai]*k;
4303: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 4304: k4=func(p2)-fx;
1.203 brouard 4305: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
4306: if(k1*k2*k3*k4 <0.){
1.208 brouard 4307: firstime=1;
1.203 brouard 4308: kmax=kmax+10;
1.208 brouard 4309: }
4310: if(kmax >=10 || firstime ==1){
1.246 brouard 4311: 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);
4312: 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 4313: 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);
4314: 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);
4315: }
4316: #ifdef DEBUGHESSIJ
4317: v1=hess[thetai][thetai];
4318: v2=hess[thetaj][thetaj];
4319: cv12=res;
4320: /* Computing eigen value of Hessian matrix */
4321: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4322: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
4323: if ((lc2 <0) || (lc1 <0) ){
4324: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4325: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
4326: 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);
4327: 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);
4328: }
1.126 brouard 4329: #endif
4330: }
4331: return res;
4332: }
4333:
1.203 brouard 4334: /* Not done yet: Was supposed to fix if not exactly at the maximum */
4335: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
4336: /* { */
4337: /* int i; */
4338: /* int l=1, lmax=20; */
4339: /* double k1,k2,k3,k4,res,fx; */
4340: /* double p2[MAXPARM+1]; */
4341: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
4342: /* int k=0,kmax=10; */
4343: /* double l1; */
4344:
4345: /* fx=func(x); */
4346: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
4347: /* l1=pow(10,l); */
4348: /* delts=delt; */
4349: /* for(k=1 ; k <kmax; k=k+1){ */
4350: /* delt = delti*(l1*k); */
4351: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
4352: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4353: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4354: /* k1=func(p2)-fx; */
4355:
4356: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
4357: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4358: /* k2=func(p2)-fx; */
4359:
4360: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4361: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
4362: /* k3=func(p2)-fx; */
4363:
4364: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
4365: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
4366: /* k4=func(p2)-fx; */
4367: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
4368: /* #ifdef DEBUGHESSIJ */
4369: /* 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); */
4370: /* 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); */
4371: /* #endif */
4372: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
4373: /* k=kmax; */
4374: /* } */
4375: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
4376: /* k=kmax; l=lmax*10; */
4377: /* } */
4378: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
4379: /* delts=delt; */
4380: /* } */
4381: /* } /\* End loop k *\/ */
4382: /* } */
4383: /* delti[theta]=delts; */
4384: /* return res; */
4385: /* } */
4386:
4387:
1.126 brouard 4388: /************** Inverse of matrix **************/
4389: void ludcmp(double **a, int n, int *indx, double *d)
4390: {
4391: int i,imax,j,k;
4392: double big,dum,sum,temp;
4393: double *vv;
4394:
4395: vv=vector(1,n);
4396: *d=1.0;
4397: for (i=1;i<=n;i++) {
4398: big=0.0;
4399: for (j=1;j<=n;j++)
4400: if ((temp=fabs(a[i][j])) > big) big=temp;
1.256 brouard 4401: if (big == 0.0){
4402: printf(" Singular Hessian matrix at row %d:\n",i);
4403: for (j=1;j<=n;j++) {
4404: printf(" a[%d][%d]=%f,",i,j,a[i][j]);
4405: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]);
4406: }
4407: fflush(ficlog);
4408: fclose(ficlog);
4409: nrerror("Singular matrix in routine ludcmp");
4410: }
1.126 brouard 4411: vv[i]=1.0/big;
4412: }
4413: for (j=1;j<=n;j++) {
4414: for (i=1;i<j;i++) {
4415: sum=a[i][j];
4416: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
4417: a[i][j]=sum;
4418: }
4419: big=0.0;
4420: for (i=j;i<=n;i++) {
4421: sum=a[i][j];
4422: for (k=1;k<j;k++)
4423: sum -= a[i][k]*a[k][j];
4424: a[i][j]=sum;
4425: if ( (dum=vv[i]*fabs(sum)) >= big) {
4426: big=dum;
4427: imax=i;
4428: }
4429: }
4430: if (j != imax) {
4431: for (k=1;k<=n;k++) {
4432: dum=a[imax][k];
4433: a[imax][k]=a[j][k];
4434: a[j][k]=dum;
4435: }
4436: *d = -(*d);
4437: vv[imax]=vv[j];
4438: }
4439: indx[j]=imax;
4440: if (a[j][j] == 0.0) a[j][j]=TINY;
4441: if (j != n) {
4442: dum=1.0/(a[j][j]);
4443: for (i=j+1;i<=n;i++) a[i][j] *= dum;
4444: }
4445: }
4446: free_vector(vv,1,n); /* Doesn't work */
4447: ;
4448: }
4449:
4450: void lubksb(double **a, int n, int *indx, double b[])
4451: {
4452: int i,ii=0,ip,j;
4453: double sum;
4454:
4455: for (i=1;i<=n;i++) {
4456: ip=indx[i];
4457: sum=b[ip];
4458: b[ip]=b[i];
4459: if (ii)
4460: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
4461: else if (sum) ii=i;
4462: b[i]=sum;
4463: }
4464: for (i=n;i>=1;i--) {
4465: sum=b[i];
4466: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
4467: b[i]=sum/a[i][i];
4468: }
4469: }
4470:
4471: void pstamp(FILE *fichier)
4472: {
1.196 brouard 4473: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 4474: }
4475:
1.297 brouard 4476: void date2dmy(double date,double *day, double *month, double *year){
4477: double yp=0., yp1=0., yp2=0.;
4478:
4479: yp1=modf(date,&yp);/* extracts integral of date in yp and
4480: fractional in yp1 */
4481: *year=yp;
4482: yp2=modf((yp1*12),&yp);
4483: *month=yp;
4484: yp1=modf((yp2*30.5),&yp);
4485: *day=yp;
4486: if(*day==0) *day=1;
4487: if(*month==0) *month=1;
4488: }
4489:
1.253 brouard 4490:
4491:
1.126 brouard 4492: /************ Frequencies ********************/
1.251 brouard 4493: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
1.226 brouard 4494: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
4495: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.250 brouard 4496: { /* Some frequencies as well as proposing some starting values */
1.226 brouard 4497:
1.265 brouard 4498: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1;
1.226 brouard 4499: int iind=0, iage=0;
4500: int mi; /* Effective wave */
4501: int first;
4502: double ***freq; /* Frequencies */
1.268 brouard 4503: 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 */
4504: 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 4505: double *meanq, *stdq, *idq;
1.226 brouard 4506: double **meanqt;
4507: double *pp, **prop, *posprop, *pospropt;
4508: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
4509: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
4510: double agebegin, ageend;
4511:
4512: pp=vector(1,nlstate);
1.251 brouard 4513: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4514: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
4515: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
4516: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
4517: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.284 brouard 4518: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.283 brouard 4519: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.226 brouard 4520: meanqt=matrix(1,lastpass,1,nqtveff);
4521: strcpy(fileresp,"P_");
4522: strcat(fileresp,fileresu);
4523: /*strcat(fileresphtm,fileresu);*/
4524: if((ficresp=fopen(fileresp,"w"))==NULL) {
4525: printf("Problem with prevalence resultfile: %s\n", fileresp);
4526: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
4527: exit(0);
4528: }
1.240 brouard 4529:
1.226 brouard 4530: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
4531: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
4532: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4533: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
4534: fflush(ficlog);
4535: exit(70);
4536: }
4537: else{
4538: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.240 brouard 4539: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4540: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4541: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4542: }
1.237 brouard 4543: 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 4544:
1.226 brouard 4545: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
4546: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
4547: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4548: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
4549: fflush(ficlog);
4550: exit(70);
1.240 brouard 4551: } else{
1.226 brouard 4552: 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 4553: <hr size=\"2\" color=\"#EC5E5E\"> \n \
1.214 brouard 4554: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 brouard 4555: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
4556: }
1.240 brouard 4557: 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);
4558:
1.253 brouard 4559: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
4560: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.251 brouard 4561: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.226 brouard 4562: j1=0;
1.126 brouard 4563:
1.227 brouard 4564: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */
4565: j=cptcoveff; /* Only dummy covariates of the model */
1.226 brouard 4566: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.240 brouard 4567:
4568:
1.226 brouard 4569: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
4570: reference=low_education V1=0,V2=0
4571: med_educ V1=1 V2=0,
4572: high_educ V1=0 V2=1
4573: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
4574: */
1.249 brouard 4575: dateintsum=0;
4576: k2cpt=0;
4577:
1.253 brouard 4578: if(cptcoveff == 0 )
1.265 brouard 4579: nl=1; /* Constant and age model only */
1.253 brouard 4580: else
4581: nl=2;
1.265 brouard 4582:
4583: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */
4584: /* Loop on nj=1 or 2 if dummy covariates j!=0
4585: * Loop on j1(1 to 2**cptcoveff) covariate combination
4586: * freq[s1][s2][iage] =0.
4587: * Loop on iind
4588: * ++freq[s1][s2][iage] weighted
4589: * end iind
4590: * if covariate and j!0
4591: * headers Variable on one line
4592: * endif cov j!=0
4593: * header of frequency table by age
4594: * Loop on age
4595: * pp[s1]+=freq[s1][s2][iage] weighted
4596: * pos+=freq[s1][s2][iage] weighted
4597: * Loop on s1 initial state
4598: * fprintf(ficresp
4599: * end s1
4600: * end age
4601: * if j!=0 computes starting values
4602: * end compute starting values
4603: * end j1
4604: * end nl
4605: */
1.253 brouard 4606: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */
4607: if(nj==1)
4608: j=0; /* First pass for the constant */
1.265 brouard 4609: else{
1.253 brouard 4610: j=cptcoveff; /* Other passes for the covariate values */
1.265 brouard 4611: }
1.251 brouard 4612: first=1;
1.265 brouard 4613: 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 4614: posproptt=0.;
4615: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
4616: scanf("%d", i);*/
4617: for (i=-5; i<=nlstate+ndeath; i++)
1.265 brouard 4618: for (s2=-5; s2<=nlstate+ndeath; s2++)
1.251 brouard 4619: for(m=iagemin; m <= iagemax+3; m++)
1.265 brouard 4620: freq[i][s2][m]=0;
1.251 brouard 4621:
4622: for (i=1; i<=nlstate; i++) {
1.240 brouard 4623: for(m=iagemin; m <= iagemax+3; m++)
1.251 brouard 4624: prop[i][m]=0;
4625: posprop[i]=0;
4626: pospropt[i]=0;
4627: }
1.283 brouard 4628: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */
1.284 brouard 4629: idq[z1]=0.;
4630: meanq[z1]=0.;
4631: stdq[z1]=0.;
1.283 brouard 4632: }
4633: /* for (z1=1; z1<= nqtveff; z1++) { */
1.251 brouard 4634: /* for(m=1;m<=lastpass;m++){ */
1.283 brouard 4635: /* meanqt[m][z1]=0.; */
4636: /* } */
4637: /* } */
1.251 brouard 4638: /* dateintsum=0; */
4639: /* k2cpt=0; */
4640:
1.265 brouard 4641: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */
1.251 brouard 4642: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
4643: bool=1;
4644: if(j !=0){
4645: if(anyvaryingduminmodel==0){ /* If All fixed covariates */
4646: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
4647: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */
4648: /* if(Tvaraff[z1] ==-20){ */
4649: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
4650: /* }else if(Tvaraff[z1] ==-10){ */
4651: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
4652: /* }else */
4653: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){ /* for combination j1 of covariates */
1.265 brouard 4654: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */
1.251 brouard 4655: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */
4656: /* 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",
4657: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
4658: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
4659: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
4660: } /* Onlyf fixed */
4661: } /* end z1 */
4662: } /* cptcovn > 0 */
4663: } /* end any */
4664: }/* end j==0 */
1.265 brouard 4665: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */
1.251 brouard 4666: /* for(m=firstpass; m<=lastpass; m++){ */
1.284 brouard 4667: for(mi=1; mi<wav[iind];mi++){ /* For each wave */
1.251 brouard 4668: m=mw[mi][iind];
4669: if(j!=0){
4670: if(anyvaryingduminmodel==1){ /* Some are varying covariates */
4671: for (z1=1; z1<=cptcoveff; z1++) {
4672: if( Fixed[Tmodelind[z1]]==1){
4673: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
4674: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality. If covariate's
4675: value is -1, we don't select. It differs from the
4676: constant and age model which counts them. */
4677: bool=0; /* not selected */
4678: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */
4679: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
4680: bool=0;
4681: }
4682: }
4683: }
4684: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */
4685: } /* end j==0 */
4686: /* bool =0 we keep that guy which corresponds to the combination of dummy values */
1.284 brouard 4687: if(bool==1){ /*Selected */
1.251 brouard 4688: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
4689: and mw[mi+1][iind]. dh depends on stepm. */
4690: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
4691: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
4692: if(m >=firstpass && m <=lastpass){
4693: k2=anint[m][iind]+(mint[m][iind]/12.);
4694: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
4695: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
4696: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
4697: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
4698: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
4699: if (m<lastpass) {
4700: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
4701: /* 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]); */
4702: if(s[m][iind]==-1)
4703: 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.));
4704: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
1.311 ! brouard 4705: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */
! 4706: if(!isnan(covar[ncovcol+z1][iind])){
! 4707: idq[z1]=idq[z1]+weight[iind];
! 4708: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */
! 4709: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/
! 4710: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */
! 4711: }
1.284 brouard 4712: }
1.251 brouard 4713: /* if((int)agev[m][iind] == 55) */
4714: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */
4715: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
4716: 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 4717: }
1.251 brouard 4718: } /* end if between passes */
4719: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) {
4720: dateintsum=dateintsum+k2; /* on all covariates ?*/
4721: k2cpt++;
4722: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
1.234 brouard 4723: }
1.251 brouard 4724: }else{
4725: bool=1;
4726: }/* end bool 2 */
4727: } /* end m */
1.284 brouard 4728: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */
4729: /* idq[z1]=idq[z1]+weight[iind]; */
4730: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */
4731: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */
4732: /* } */
1.251 brouard 4733: } /* end bool */
4734: } /* end iind = 1 to imx */
4735: /* prop[s][age] is feeded for any initial and valid live state as well as
4736: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
4737:
4738:
4739: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
1.265 brouard 4740: if(cptcoveff==0 && nj==1) /* no covariate and first pass */
4741: pstamp(ficresp);
1.251 brouard 4742: if (cptcoveff>0 && j!=0){
1.265 brouard 4743: pstamp(ficresp);
1.251 brouard 4744: printf( "\n#********** Variable ");
4745: fprintf(ficresp, "\n#********** Variable ");
4746: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4747: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
4748: fprintf(ficlog, "\n#********** Variable ");
4749: for (z1=1; z1<=cptcoveff; z1++){
4750: if(!FixedV[Tvaraff[z1]]){
4751: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4752: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4753: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4754: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4755: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.250 brouard 4756: }else{
1.251 brouard 4757: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4758: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4759: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4760: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4761: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4762: }
4763: }
4764: printf( "**********\n#");
4765: fprintf(ficresp, "**********\n#");
4766: fprintf(ficresphtm, "**********</h3>\n");
4767: fprintf(ficresphtmfr, "**********</h3>\n");
4768: fprintf(ficlog, "**********\n");
4769: }
1.284 brouard 4770: /*
4771: Printing means of quantitative variables if any
4772: */
4773: for (z1=1; z1<= nqfveff; z1++) {
1.311 ! brouard 4774: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]);
! 4775: fprintf(ficlog,", mean=%.3g",meanq[z1]/idq[z1]," stdeviation=%.3g\n",stdq[z1]);
1.284 brouard 4776: if(weightopt==1){
4777: printf(" Weighted mean and standard deviation of");
4778: fprintf(ficlog," Weighted mean and standard deviation of");
4779: fprintf(ficresphtmfr," Weighted mean and standard deviation of");
4780: }
1.311 ! brouard 4781: /* mu = \frac{w x}{\sum w}
! 4782: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2
! 4783: */
! 4784: printf(" fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
! 4785: fprintf(ficlog," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
! 4786: fprintf(ficresphtmfr," fixed quantitative variable V%d on %.3g (weighted) representatives of the population : %8.5g (%8.5g)<p>\n", ncovcol+z1, idq[z1],meanq[z1]/idq[z1], sqrt(stdq[z1]/idq[z1]-meanq[z1]*meanq[z1]/idq[z1]/idq[z1]));
1.284 brouard 4787: }
4788: /* for (z1=1; z1<= nqtveff; z1++) { */
4789: /* for(m=1;m<=lastpass;m++){ */
4790: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */
4791: /* } */
4792: /* } */
1.283 brouard 4793:
1.251 brouard 4794: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.265 brouard 4795: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */
4796: fprintf(ficresp, " Age");
4797: 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 4798: for(i=1; i<=nlstate;i++) {
1.265 brouard 4799: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i);
1.251 brouard 4800: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4801: }
1.265 brouard 4802: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n");
1.251 brouard 4803: fprintf(ficresphtm, "\n");
4804:
4805: /* Header of frequency table by age */
4806: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4807: fprintf(ficresphtmfr,"<th>Age</th> ");
1.265 brouard 4808: for(s2=-1; s2 <=nlstate+ndeath; s2++){
1.251 brouard 4809: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 4810: if(s2!=0 && m!=0)
4811: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m);
1.240 brouard 4812: }
1.226 brouard 4813: }
1.251 brouard 4814: fprintf(ficresphtmfr, "\n");
4815:
4816: /* For each age */
4817: for(iage=iagemin; iage <= iagemax+3; iage++){
4818: fprintf(ficresphtm,"<tr>");
4819: if(iage==iagemax+1){
4820: fprintf(ficlog,"1");
4821: fprintf(ficresphtmfr,"<tr><th>0</th> ");
4822: }else if(iage==iagemax+2){
4823: fprintf(ficlog,"0");
4824: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
4825: }else if(iage==iagemax+3){
4826: fprintf(ficlog,"Total");
4827: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
4828: }else{
1.240 brouard 4829: if(first==1){
1.251 brouard 4830: first=0;
4831: printf("See log file for details...\n");
4832: }
4833: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4834: fprintf(ficlog,"Age %d", iage);
4835: }
1.265 brouard 4836: for(s1=1; s1 <=nlstate ; s1++){
4837: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++)
4838: pp[s1] += freq[s1][m][iage];
1.251 brouard 4839: }
1.265 brouard 4840: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 4841: for(m=-1, pos=0; m <=0 ; m++)
1.265 brouard 4842: pos += freq[s1][m][iage];
4843: if(pp[s1]>=1.e-10){
1.251 brouard 4844: if(first==1){
1.265 brouard 4845: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 4846: }
1.265 brouard 4847: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]);
1.251 brouard 4848: }else{
4849: if(first==1)
1.265 brouard 4850: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
4851: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1);
1.240 brouard 4852: }
4853: }
4854:
1.265 brouard 4855: for(s1=1; s1 <=nlstate ; s1++){
4856: /* posprop[s1]=0; */
4857: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4858: pp[s1] += freq[s1][m][iage];
4859: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */
4860:
4861: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){
4862: pos += pp[s1]; /* pos is the total number of transitions until this age */
4863: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state
4864: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4865: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state
4866: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4867: }
4868:
4869: /* Writing ficresp */
4870: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
4871: if( iage <= iagemax){
4872: fprintf(ficresp," %d",iage);
4873: }
4874: }else if( nj==2){
4875: if( iage <= iagemax){
4876: fprintf(ficresp," %d",iage);
4877: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4878: }
1.240 brouard 4879: }
1.265 brouard 4880: for(s1=1; s1 <=nlstate ; s1++){
1.240 brouard 4881: if(pos>=1.e-5){
1.251 brouard 4882: if(first==1)
1.265 brouard 4883: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
4884: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos);
1.251 brouard 4885: }else{
4886: if(first==1)
1.265 brouard 4887: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
4888: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1);
1.251 brouard 4889: }
4890: if( iage <= iagemax){
4891: if(pos>=1.e-5){
1.265 brouard 4892: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */
4893: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4894: }else if( nj==2){
4895: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4896: }
4897: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta);
4898: /*probs[iage][s1][j1]= pp[s1]/pos;*/
4899: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/
4900: } else{
4901: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta);
4902: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta);
1.251 brouard 4903: }
1.240 brouard 4904: }
1.265 brouard 4905: pospropt[s1] +=posprop[s1];
4906: } /* end loop s1 */
1.251 brouard 4907: /* pospropt=0.; */
1.265 brouard 4908: for(s1=-1; s1 <=nlstate+ndeath; s1++){
1.251 brouard 4909: for(m=-1; m <=nlstate+ndeath; m++){
1.265 brouard 4910: if(freq[s1][m][iage] !=0 ) { /* minimizing output */
1.251 brouard 4911: if(first==1){
1.265 brouard 4912: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 4913: }
1.265 brouard 4914: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */
4915: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]);
1.251 brouard 4916: }
1.265 brouard 4917: if(s1!=0 && m!=0)
4918: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]);
1.240 brouard 4919: }
1.265 brouard 4920: } /* end loop s1 */
1.251 brouard 4921: posproptt=0.;
1.265 brouard 4922: for(s1=1; s1 <=nlstate; s1++){
4923: posproptt += pospropt[s1];
1.251 brouard 4924: }
4925: fprintf(ficresphtmfr,"</tr>\n ");
1.265 brouard 4926: fprintf(ficresphtm,"</tr>\n");
4927: if((cptcoveff==0 && nj==1)|| nj==2 ) {
4928: if(iage <= iagemax)
4929: fprintf(ficresp,"\n");
1.240 brouard 4930: }
1.251 brouard 4931: if(first==1)
4932: printf("Others in log...\n");
4933: fprintf(ficlog,"\n");
4934: } /* end loop age iage */
1.265 brouard 4935:
1.251 brouard 4936: fprintf(ficresphtm,"<tr><th>Tot</th>");
1.265 brouard 4937: for(s1=1; s1 <=nlstate ; s1++){
1.251 brouard 4938: if(posproptt < 1.e-5){
1.265 brouard 4939: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt);
1.251 brouard 4940: }else{
1.265 brouard 4941: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt);
1.240 brouard 4942: }
1.226 brouard 4943: }
1.251 brouard 4944: fprintf(ficresphtm,"</tr>\n");
4945: fprintf(ficresphtm,"</table>\n");
4946: fprintf(ficresphtmfr,"</table>\n");
1.226 brouard 4947: if(posproptt < 1.e-5){
1.251 brouard 4948: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4949: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
1.260 brouard 4950: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1);
4951: printf("# This combination (%d) is not valid and no result will be produced\n",j1);
1.251 brouard 4952: invalidvarcomb[j1]=1;
1.226 brouard 4953: }else{
1.251 brouard 4954: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4955: invalidvarcomb[j1]=0;
1.226 brouard 4956: }
1.251 brouard 4957: fprintf(ficresphtmfr,"</table>\n");
4958: fprintf(ficlog,"\n");
4959: if(j!=0){
4960: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1);
1.265 brouard 4961: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 4962: for(k=1; k <=(nlstate+ndeath); k++){
4963: if (k != i) {
1.265 brouard 4964: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */
1.253 brouard 4965: if(jj==1){ /* Constant case (in fact cste + age) */
1.251 brouard 4966: if(j1==1){ /* All dummy covariates to zero */
4967: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */
4968: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */
1.252 brouard 4969: printf("%d%d ",i,k);
4970: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 4971: 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]));
4972: 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]));
4973: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
1.251 brouard 4974: }
1.253 brouard 4975: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */
4976: for(iage=iagemin; iage <= iagemax+3; iage++){
4977: x[iage]= (double)iage;
4978: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]);
1.265 brouard 4979: /* 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 4980: }
1.268 brouard 4981: /* Some are not finite, but linreg will ignore these ages */
4982: no=0;
1.253 brouard 4983: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */
1.265 brouard 4984: pstart[s1]=b;
4985: pstart[s1-1]=a;
1.252 brouard 4986: }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 */
4987: 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]);
4988: 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 4989: 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 4990: printf("%d%d ",i,k);
4991: fprintf(ficlog,"%d%d ",i,k);
1.265 brouard 4992: 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 4993: }else{ /* Other cases, like quantitative fixed or varying covariates */
4994: ;
4995: }
4996: /* printf("%12.7f )", param[i][jj][k]); */
4997: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 4998: s1++;
1.251 brouard 4999: } /* end jj */
5000: } /* end k!= i */
5001: } /* end k */
1.265 brouard 5002: } /* end i, s1 */
1.251 brouard 5003: } /* end j !=0 */
5004: } /* end selected combination of covariate j1 */
5005: if(j==0){ /* We can estimate starting values from the occurences in each case */
5006: printf("#Freqsummary: Starting values for the constants:\n");
5007: fprintf(ficlog,"\n");
1.265 brouard 5008: for(i=1,s1=1; i <=nlstate; i++){
1.251 brouard 5009: for(k=1; k <=(nlstate+ndeath); k++){
5010: if (k != i) {
5011: printf("%d%d ",i,k);
5012: fprintf(ficlog,"%d%d ",i,k);
5013: for(jj=1; jj <=ncovmodel; jj++){
1.265 brouard 5014: pstart[s1]=p[s1]; /* Setting pstart to p values by default */
1.253 brouard 5015: if(jj==1){ /* Age has to be done */
1.265 brouard 5016: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]);
5017: 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]));
5018: 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 5019: }
5020: /* printf("%12.7f )", param[i][jj][k]); */
5021: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */
1.265 brouard 5022: s1++;
1.250 brouard 5023: }
1.251 brouard 5024: printf("\n");
5025: fprintf(ficlog,"\n");
1.250 brouard 5026: }
5027: }
1.284 brouard 5028: } /* end of state i */
1.251 brouard 5029: printf("#Freqsummary\n");
5030: fprintf(ficlog,"\n");
1.265 brouard 5031: for(s1=-1; s1 <=nlstate+ndeath; s1++){
5032: for(s2=-1; s2 <=nlstate+ndeath; s2++){
5033: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */
5034: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5035: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]);
5036: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */
5037: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
5038: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */
1.251 brouard 5039: /* } */
5040: }
1.265 brouard 5041: } /* end loop s1 */
1.251 brouard 5042:
5043: printf("\n");
5044: fprintf(ficlog,"\n");
5045: } /* end j=0 */
1.249 brouard 5046: } /* end j */
1.252 brouard 5047:
1.253 brouard 5048: if(mle == -2){ /* We want to use these values as starting values */
1.252 brouard 5049: for(i=1, jk=1; i <=nlstate; i++){
5050: for(j=1; j <=nlstate+ndeath; j++){
5051: if(j!=i){
5052: /*ca[0]= k+'a'-1;ca[1]='\0';*/
5053: printf("%1d%1d",i,j);
5054: fprintf(ficparo,"%1d%1d",i,j);
5055: for(k=1; k<=ncovmodel;k++){
5056: /* printf(" %lf",param[i][j][k]); */
5057: /* fprintf(ficparo," %lf",param[i][j][k]); */
5058: p[jk]=pstart[jk];
5059: printf(" %f ",pstart[jk]);
5060: fprintf(ficparo," %f ",pstart[jk]);
5061: jk++;
5062: }
5063: printf("\n");
5064: fprintf(ficparo,"\n");
5065: }
5066: }
5067: }
5068: } /* end mle=-2 */
1.226 brouard 5069: dateintmean=dateintsum/k2cpt;
1.296 brouard 5070: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean);
1.240 brouard 5071:
1.226 brouard 5072: fclose(ficresp);
5073: fclose(ficresphtm);
5074: fclose(ficresphtmfr);
1.283 brouard 5075: free_vector(idq,1,nqfveff);
1.226 brouard 5076: free_vector(meanq,1,nqfveff);
1.284 brouard 5077: free_vector(stdq,1,nqfveff);
1.226 brouard 5078: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.253 brouard 5079: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
5080: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.251 brouard 5081: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5082: free_vector(pospropt,1,nlstate);
5083: free_vector(posprop,1,nlstate);
1.251 brouard 5084: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE);
1.226 brouard 5085: free_vector(pp,1,nlstate);
5086: /* End of freqsummary */
5087: }
1.126 brouard 5088:
1.268 brouard 5089: /* Simple linear regression */
5090: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) {
5091:
5092: /* y=a+bx regression */
5093: double sumx = 0.0; /* sum of x */
5094: double sumx2 = 0.0; /* sum of x**2 */
5095: double sumxy = 0.0; /* sum of x * y */
5096: double sumy = 0.0; /* sum of y */
5097: double sumy2 = 0.0; /* sum of y**2 */
5098: double sume2 = 0.0; /* sum of square or residuals */
5099: double yhat;
5100:
5101: double denom=0;
5102: int i;
5103: int ne=*no;
5104:
5105: for ( i=ifi, ne=0;i<=ila;i++) {
5106: if(!isfinite(x[i]) || !isfinite(y[i])){
5107: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5108: continue;
5109: }
5110: ne=ne+1;
5111: sumx += x[i];
5112: sumx2 += x[i]*x[i];
5113: sumxy += x[i] * y[i];
5114: sumy += y[i];
5115: sumy2 += y[i]*y[i];
5116: denom = (ne * sumx2 - sumx*sumx);
5117: /* 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); */
5118: }
5119:
5120: denom = (ne * sumx2 - sumx*sumx);
5121: if (denom == 0) {
5122: // vertical, slope m is infinity
5123: *b = INFINITY;
5124: *a = 0;
5125: if (r) *r = 0;
5126: return 1;
5127: }
5128:
5129: *b = (ne * sumxy - sumx * sumy) / denom;
5130: *a = (sumy * sumx2 - sumx * sumxy) / denom;
5131: if (r!=NULL) {
5132: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */
5133: sqrt((sumx2 - sumx*sumx/ne) *
5134: (sumy2 - sumy*sumy/ne));
5135: }
5136: *no=ne;
5137: for ( i=ifi, ne=0;i<=ila;i++) {
5138: if(!isfinite(x[i]) || !isfinite(y[i])){
5139: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */
5140: continue;
5141: }
5142: ne=ne+1;
5143: yhat = y[i] - *a -*b* x[i];
5144: sume2 += yhat * yhat ;
5145:
5146: denom = (ne * sumx2 - sumx*sumx);
5147: /* 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); */
5148: }
5149: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne));
5150: *sa= *sb * sqrt(sumx2/ne);
5151:
5152: return 0;
5153: }
5154:
1.126 brouard 5155: /************ Prevalence ********************/
1.227 brouard 5156: 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)
5157: {
5158: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
5159: in each health status at the date of interview (if between dateprev1 and dateprev2).
5160: We still use firstpass and lastpass as another selection.
5161: */
1.126 brouard 5162:
1.227 brouard 5163: int i, m, jk, j1, bool, z1,j, iv;
5164: int mi; /* Effective wave */
5165: int iage;
5166: double agebegin, ageend;
5167:
5168: double **prop;
5169: double posprop;
5170: double y2; /* in fractional years */
5171: int iagemin, iagemax;
5172: int first; /** to stop verbosity which is redirected to log file */
5173:
5174: iagemin= (int) agemin;
5175: iagemax= (int) agemax;
5176: /*pp=vector(1,nlstate);*/
1.251 brouard 5177: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5178: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
5179: j1=0;
1.222 brouard 5180:
1.227 brouard 5181: /*j=cptcoveff;*/
5182: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.222 brouard 5183:
1.288 brouard 5184: first=0;
1.227 brouard 5185: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
5186: for (i=1; i<=nlstate; i++)
1.251 brouard 5187: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++)
1.227 brouard 5188: prop[i][iage]=0.0;
5189: printf("Prevalence combination of varying and fixed dummies %d\n",j1);
5190: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */
5191: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1);
5192:
5193: for (i=1; i<=imx; i++) { /* Each individual */
5194: bool=1;
5195: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
5196: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */
5197: m=mw[mi][i];
5198: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */
5199: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */
5200: for (z1=1; z1<=cptcoveff; z1++){
5201: if( Fixed[Tmodelind[z1]]==1){
5202: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv;
5203: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */
5204: bool=0;
5205: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */
5206: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) {
5207: bool=0;
5208: }
5209: }
5210: if(bool==1){ /* Otherwise we skip that wave/person */
5211: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
5212: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
5213: if(m >=firstpass && m <=lastpass){
5214: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
5215: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
5216: if(agev[m][i]==0) agev[m][i]=iagemax+1;
5217: if(agev[m][i]==1) agev[m][i]=iagemax+2;
1.251 brouard 5218: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){
1.227 brouard 5219: 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);
5220: exit(1);
5221: }
5222: if (s[m][i]>0 && s[m][i]<=nlstate) {
5223: /*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]]);*/
5224: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
5225: prop[s[m][i]][iagemax+3] += weight[i];
5226: } /* end valid statuses */
5227: } /* end selection of dates */
5228: } /* end selection of waves */
5229: } /* end bool */
5230: } /* end wave */
5231: } /* end individual */
5232: for(i=iagemin; i <= iagemax+3; i++){
5233: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
5234: posprop += prop[jk][i];
5235: }
5236:
5237: for(jk=1; jk <=nlstate ; jk++){
5238: if( i <= iagemax){
5239: if(posprop>=1.e-5){
5240: probs[i][jk][j1]= prop[jk][i]/posprop;
5241: } else{
1.288 brouard 5242: if(!first){
5243: first=1;
1.266 brouard 5244: 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]);
5245: }else{
1.288 brouard 5246: 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 5247: }
5248: }
5249: }
5250: }/* end jk */
5251: }/* end i */
1.222 brouard 5252: /*} *//* end i1 */
1.227 brouard 5253: } /* end j1 */
1.222 brouard 5254:
1.227 brouard 5255: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
5256: /*free_vector(pp,1,nlstate);*/
1.251 brouard 5257: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE);
1.227 brouard 5258: } /* End of prevalence */
1.126 brouard 5259:
5260: /************* Waves Concatenation ***************/
5261:
5262: 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)
5263: {
1.298 brouard 5264: /* 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 5265: Death is a valid wave (if date is known).
5266: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
5267: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
1.298 brouard 5268: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass
1.227 brouard 5269: */
1.126 brouard 5270:
1.224 brouard 5271: int i=0, mi=0, m=0, mli=0;
1.126 brouard 5272: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
5273: double sum=0., jmean=0.;*/
1.224 brouard 5274: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 5275: int j, k=0,jk, ju, jl;
5276: double sum=0.;
5277: first=0;
1.214 brouard 5278: firstwo=0;
1.217 brouard 5279: firsthree=0;
1.218 brouard 5280: firstfour=0;
1.164 brouard 5281: jmin=100000;
1.126 brouard 5282: jmax=-1;
5283: jmean=0.;
1.224 brouard 5284:
5285: /* Treating live states */
1.214 brouard 5286: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 5287: mi=0; /* First valid wave */
1.227 brouard 5288: mli=0; /* Last valid wave */
1.309 brouard 5289: m=firstpass; /* Loop on waves */
5290: while(s[m][i] <= nlstate){ /* a live state or unknown state */
1.227 brouard 5291: 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 */
5292: mli=m-1;/* mw[++mi][i]=m-1; */
5293: }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 5294: 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 5295: mli=m;
1.224 brouard 5296: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
5297: if(m < lastpass){ /* m < lastpass, standard case */
1.227 brouard 5298: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 5299: }
1.309 brouard 5300: else{ /* m = lastpass, eventual special issue with warning */
1.224 brouard 5301: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
1.227 brouard 5302: break;
1.224 brouard 5303: #else
1.309 brouard 5304: 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 5305: if(firsthree == 0){
1.302 brouard 5306: 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 5307: firsthree=1;
5308: }
1.302 brouard 5309: 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 5310: mw[++mi][i]=m; /* Valid transition with unknown status */
1.227 brouard 5311: mli=m;
5312: }
5313: if(s[m][i]==-2){ /* Vital status is really unknown */
5314: nbwarn++;
1.309 brouard 5315: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */
1.227 brouard 5316: 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);
5317: 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);
5318: }
5319: break;
5320: }
5321: break;
1.224 brouard 5322: #endif
1.227 brouard 5323: }/* End m >= lastpass */
1.126 brouard 5324: }/* end while */
1.224 brouard 5325:
1.227 brouard 5326: /* 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 5327: /* After last pass */
1.224 brouard 5328: /* Treating death states */
1.214 brouard 5329: if (s[m][i] > nlstate){ /* In a death state */
1.227 brouard 5330: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
5331: /* } */
1.126 brouard 5332: mi++; /* Death is another wave */
5333: /* if(mi==0) never been interviewed correctly before death */
1.227 brouard 5334: /* Only death is a correct wave */
1.126 brouard 5335: mw[mi][i]=m;
1.257 brouard 5336: } /* else not in a death state */
1.224 brouard 5337: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
1.257 brouard 5338: else if ((int) andc[i] != 9999) { /* Date of death is known */
1.218 brouard 5339: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.309 brouard 5340: 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 5341: nbwarn++;
5342: if(firstfiv==0){
1.309 brouard 5343: 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 5344: firstfiv=1;
5345: }else{
1.309 brouard 5346: 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 5347: }
1.309 brouard 5348: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */
5349: }else{ /* Month of Death occured afer last wave month, potential bias */
1.227 brouard 5350: nberr++;
5351: if(firstwo==0){
1.309 brouard 5352: 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 5353: firstwo=1;
5354: }
1.309 brouard 5355: 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 5356: }
1.257 brouard 5357: }else{ /* if date of interview is unknown */
1.227 brouard 5358: /* death is known but not confirmed by death status at any wave */
5359: if(firstfour==0){
1.309 brouard 5360: 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 5361: firstfour=1;
5362: }
1.309 brouard 5363: 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 5364: }
1.224 brouard 5365: } /* end if date of death is known */
5366: #endif
1.309 brouard 5367: wav[i]=mi; /* mi should be the last effective wave (or mli), */
5368: /* wav[i]=mw[mi][i]; */
1.126 brouard 5369: if(mi==0){
5370: nbwarn++;
5371: if(first==0){
1.227 brouard 5372: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
5373: first=1;
1.126 brouard 5374: }
5375: if(first==1){
1.227 brouard 5376: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 5377: }
5378: } /* end mi==0 */
5379: } /* End individuals */
1.214 brouard 5380: /* wav and mw are no more changed */
1.223 brouard 5381:
1.214 brouard 5382:
1.126 brouard 5383: for(i=1; i<=imx; i++){
5384: for(mi=1; mi<wav[i];mi++){
5385: if (stepm <=0)
1.227 brouard 5386: dh[mi][i]=1;
1.126 brouard 5387: else{
1.260 brouard 5388: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */
1.227 brouard 5389: if (agedc[i] < 2*AGESUP) {
5390: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
5391: if(j==0) j=1; /* Survives at least one month after exam */
5392: else if(j<0){
5393: nberr++;
5394: 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]);
5395: j=1; /* Temporary Dangerous patch */
5396: 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);
5397: 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]);
5398: 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);
5399: }
5400: k=k+1;
5401: if (j >= jmax){
5402: jmax=j;
5403: ijmax=i;
5404: }
5405: if (j <= jmin){
5406: jmin=j;
5407: ijmin=i;
5408: }
5409: sum=sum+j;
5410: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
5411: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
5412: }
5413: }
5414: else{
5415: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 5416: /* 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 5417:
1.227 brouard 5418: k=k+1;
5419: if (j >= jmax) {
5420: jmax=j;
5421: ijmax=i;
5422: }
5423: else if (j <= jmin){
5424: jmin=j;
5425: ijmin=i;
5426: }
5427: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
5428: /*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]);*/
5429: if(j<0){
5430: nberr++;
5431: 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]);
5432: 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]);
5433: }
5434: sum=sum+j;
5435: }
5436: jk= j/stepm;
5437: jl= j -jk*stepm;
5438: ju= j -(jk+1)*stepm;
5439: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
5440: if(jl==0){
5441: dh[mi][i]=jk;
5442: bh[mi][i]=0;
5443: }else{ /* We want a negative bias in order to only have interpolation ie
5444: * to avoid the price of an extra matrix product in likelihood */
5445: dh[mi][i]=jk+1;
5446: bh[mi][i]=ju;
5447: }
5448: }else{
5449: if(jl <= -ju){
5450: dh[mi][i]=jk;
5451: bh[mi][i]=jl; /* bias is positive if real duration
5452: * is higher than the multiple of stepm and negative otherwise.
5453: */
5454: }
5455: else{
5456: dh[mi][i]=jk+1;
5457: bh[mi][i]=ju;
5458: }
5459: if(dh[mi][i]==0){
5460: dh[mi][i]=1; /* At least one step */
5461: bh[mi][i]=ju; /* At least one step */
5462: /* 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);*/
5463: }
5464: } /* end if mle */
1.126 brouard 5465: }
5466: } /* end wave */
5467: }
5468: jmean=sum/k;
5469: 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 5470: 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 5471: }
1.126 brouard 5472:
5473: /*********** Tricode ****************************/
1.220 brouard 5474: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.242 brouard 5475: {
5476: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
5477: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
5478: * Boring subroutine which should only output nbcode[Tvar[j]][k]
5479: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
5480: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
5481: */
1.130 brouard 5482:
1.242 brouard 5483: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
5484: int modmaxcovj=0; /* Modality max of covariates j */
5485: int cptcode=0; /* Modality max of covariates j */
5486: int modmincovj=0; /* Modality min of covariates j */
1.145 brouard 5487:
5488:
1.242 brouard 5489: /* cptcoveff=0; */
5490: /* *cptcov=0; */
1.126 brouard 5491:
1.242 brouard 5492: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.285 brouard 5493: for (k=1; k <= maxncov; k++)
5494: for(j=1; j<=2; j++)
5495: nbcode[k][j]=0; /* Valgrind */
1.126 brouard 5496:
1.242 brouard 5497: /* Loop on covariates without age and products and no quantitative variable */
5498: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
5499: for (j=-1; (j < maxncov); j++) Ndum[j]=0;
5500: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */
5501: switch(Fixed[k]) {
5502: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */
1.311 ! brouard 5503: modmaxcovj=0;
! 5504: modmincovj=0;
1.242 brouard 5505: 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*/
5506: ij=(int)(covar[Tvar[k]][i]);
5507: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
5508: * If product of Vn*Vm, still boolean *:
5509: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
5510: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
5511: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
5512: modality of the nth covariate of individual i. */
5513: if (ij > modmaxcovj)
5514: modmaxcovj=ij;
5515: else if (ij < modmincovj)
5516: modmincovj=ij;
1.287 brouard 5517: if (ij <0 || ij >1 ){
1.311 ! brouard 5518: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
! 5519: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i);
! 5520: fflush(ficlog);
! 5521: exit(1);
1.287 brouard 5522: }
5523: if ((ij < -1) || (ij > NCOVMAX)){
1.242 brouard 5524: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
5525: exit(1);
5526: }else
5527: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
5528: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
5529: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
5530: /* getting the maximum value of the modality of the covariate
5531: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
5532: female ies 1, then modmaxcovj=1.
5533: */
5534: } /* end for loop on individuals i */
5535: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5536: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj);
5537: cptcode=modmaxcovj;
5538: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
5539: /*for (i=0; i<=cptcode; i++) {*/
5540: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */
5541: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5542: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]);
5543: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */
5544: if( j != -1){
5545: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th
5546: covariate for which somebody answered excluding
5547: undefined. Usually 2: 0 and 1. */
5548: }
5549: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th
5550: covariate for which somebody answered including
5551: undefined. Usually 3: -1, 0 and 1. */
5552: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for
5553: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
5554: } /* Ndum[-1] number of undefined modalities */
1.231 brouard 5555:
1.242 brouard 5556: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
5557: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */
5558: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */
5559: /* modmincovj=3; modmaxcovj = 7; */
5560: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */
5561: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */
5562: /* defining two dummy variables: variables V1_1 and V1_2.*/
5563: /* nbcode[Tvar[j]][ij]=k; */
5564: /* nbcode[Tvar[j]][1]=0; */
5565: /* nbcode[Tvar[j]][2]=1; */
5566: /* nbcode[Tvar[j]][3]=2; */
5567: /* To be continued (not working yet). */
5568: ij=0; /* ij is similar to i but can jump over null modalities */
1.287 brouard 5569:
5570: /* 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*/
5571: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */
5572: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of
5573: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */
5574: /*, could be restored in the future */
5575: 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 5576: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
5577: break;
5578: }
5579: ij++;
1.287 brouard 5580: 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 5581: cptcode = ij; /* New max modality for covar j */
5582: } /* end of loop on modality i=-1 to 1 or more */
5583: break;
5584: case 1: /* Testing on varying covariate, could be simple and
5585: * should look at waves or product of fixed *
5586: * varying. No time to test -1, assuming 0 and 1 only */
5587: ij=0;
5588: for(i=0; i<=1;i++){
5589: nbcode[Tvar[k]][++ij]=i;
5590: }
5591: break;
5592: default:
5593: break;
5594: } /* end switch */
5595: } /* end dummy test */
1.311 ! brouard 5596: if(Dummy[k]==1 && Typevar[k] !=1){ /* Dummy covariate and not age product */
! 5597: 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*/
! 5598: if(isnan(covar[Tvar[k]][i])){
! 5599: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
! 5600: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i);
! 5601: fflush(ficlog);
! 5602: exit(1);
! 5603: }
! 5604: }
! 5605: }
1.287 brouard 5606: } /* 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 5607:
5608: for (k=-1; k< maxncov; k++) Ndum[k]=0;
5609: /* Look at fixed dummy (single or product) covariates to check empty modalities */
5610: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
5611: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
5612: 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 */
5613: 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 */
5614: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */
5615: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
5616:
5617: ij=0;
5618: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */
5619: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
5620: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
5621: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */
5622: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */
5623: /* If product not in single variable we don't print results */
5624: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
5625: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */
5626: 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*/
5627: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */
5628: 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 */
5629: if(Fixed[k]!=0)
5630: anyvaryingduminmodel=1;
5631: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */
5632: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */
5633: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */
5634: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */
5635: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */
5636: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */
5637: }
5638: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
5639: /* ij--; */
5640: /* cptcoveff=ij; /\*Number of total covariates*\/ */
5641: *cptcov=ij; /*Number of total real effective covariates: effective
5642: * because they can be excluded from the model and real
5643: * if in the model but excluded because missing values, but how to get k from ij?*/
5644: for(j=ij+1; j<= cptcovt; j++){
5645: Tvaraff[j]=0;
5646: Tmodelind[j]=0;
5647: }
5648: for(j=ntveff+1; j<= cptcovt; j++){
5649: TmodelInvind[j]=0;
5650: }
5651: /* To be sorted */
5652: ;
5653: }
1.126 brouard 5654:
1.145 brouard 5655:
1.126 brouard 5656: /*********** Health Expectancies ****************/
5657:
1.235 brouard 5658: 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 5659:
5660: {
5661: /* Health expectancies, no variances */
1.164 brouard 5662: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 5663: int nhstepma, nstepma; /* Decreasing with age */
5664: double age, agelim, hf;
5665: double ***p3mat;
5666: double eip;
5667:
1.238 brouard 5668: /* pstamp(ficreseij); */
1.126 brouard 5669: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
5670: fprintf(ficreseij,"# Age");
5671: for(i=1; i<=nlstate;i++){
5672: for(j=1; j<=nlstate;j++){
5673: fprintf(ficreseij," e%1d%1d ",i,j);
5674: }
5675: fprintf(ficreseij," e%1d. ",i);
5676: }
5677: fprintf(ficreseij,"\n");
5678:
5679:
5680: if(estepm < stepm){
5681: printf ("Problem %d lower than %d\n",estepm, stepm);
5682: }
5683: else hstepm=estepm;
5684: /* We compute the life expectancy from trapezoids spaced every estepm months
5685: * This is mainly to measure the difference between two models: for example
5686: * if stepm=24 months pijx are given only every 2 years and by summing them
5687: * we are calculating an estimate of the Life Expectancy assuming a linear
5688: * progression in between and thus overestimating or underestimating according
5689: * to the curvature of the survival function. If, for the same date, we
5690: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5691: * to compare the new estimate of Life expectancy with the same linear
5692: * hypothesis. A more precise result, taking into account a more precise
5693: * curvature will be obtained if estepm is as small as stepm. */
5694:
5695: /* For example we decided to compute the life expectancy with the smallest unit */
5696: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5697: nhstepm is the number of hstepm from age to agelim
5698: nstepm is the number of stepm from age to agelin.
1.270 brouard 5699: Look at hpijx to understand the reason which relies in memory size consideration
1.126 brouard 5700: and note for a fixed period like estepm months */
5701: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5702: survival function given by stepm (the optimization length). Unfortunately it
5703: means that if the survival funtion is printed only each two years of age and if
5704: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5705: results. So we changed our mind and took the option of the best precision.
5706: */
5707: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5708:
5709: agelim=AGESUP;
5710: /* If stepm=6 months */
5711: /* Computed by stepm unit matrices, product of hstepm matrices, stored
5712: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
5713:
5714: /* nhstepm age range expressed in number of stepm */
5715: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5716: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5717: /* if (stepm >= YEARM) hstepm=1;*/
5718: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5719: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5720:
5721: for (age=bage; age<=fage; age ++){
5722: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5723: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5724: /* if (stepm >= YEARM) hstepm=1;*/
5725: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
5726:
5727: /* If stepm=6 months */
5728: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5729: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5730:
1.235 brouard 5731: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres);
1.126 brouard 5732:
5733: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5734:
5735: printf("%d|",(int)age);fflush(stdout);
5736: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5737:
5738: /* Computing expectancies */
5739: for(i=1; i<=nlstate;i++)
5740: for(j=1; j<=nlstate;j++)
5741: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5742: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
5743:
5744: /* 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]);*/
5745:
5746: }
5747:
5748: fprintf(ficreseij,"%3.0f",age );
5749: for(i=1; i<=nlstate;i++){
5750: eip=0;
5751: for(j=1; j<=nlstate;j++){
5752: eip +=eij[i][j][(int)age];
5753: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
5754: }
5755: fprintf(ficreseij,"%9.4f", eip );
5756: }
5757: fprintf(ficreseij,"\n");
5758:
5759: }
5760: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5761: printf("\n");
5762: fprintf(ficlog,"\n");
5763:
5764: }
5765:
1.235 brouard 5766: 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 5767:
5768: {
5769: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 5770: to initial status i, ei. .
1.126 brouard 5771: */
5772: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
5773: int nhstepma, nstepma; /* Decreasing with age */
5774: double age, agelim, hf;
5775: double ***p3matp, ***p3matm, ***varhe;
5776: double **dnewm,**doldm;
5777: double *xp, *xm;
5778: double **gp, **gm;
5779: double ***gradg, ***trgradg;
5780: int theta;
5781:
5782: double eip, vip;
5783:
5784: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
5785: xp=vector(1,npar);
5786: xm=vector(1,npar);
5787: dnewm=matrix(1,nlstate*nlstate,1,npar);
5788: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
5789:
5790: pstamp(ficresstdeij);
5791: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
5792: fprintf(ficresstdeij,"# Age");
5793: for(i=1; i<=nlstate;i++){
5794: for(j=1; j<=nlstate;j++)
5795: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
5796: fprintf(ficresstdeij," e%1d. ",i);
5797: }
5798: fprintf(ficresstdeij,"\n");
5799:
5800: pstamp(ficrescveij);
5801: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
5802: fprintf(ficrescveij,"# Age");
5803: for(i=1; i<=nlstate;i++)
5804: for(j=1; j<=nlstate;j++){
5805: cptj= (j-1)*nlstate+i;
5806: for(i2=1; i2<=nlstate;i2++)
5807: for(j2=1; j2<=nlstate;j2++){
5808: cptj2= (j2-1)*nlstate+i2;
5809: if(cptj2 <= cptj)
5810: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
5811: }
5812: }
5813: fprintf(ficrescveij,"\n");
5814:
5815: if(estepm < stepm){
5816: printf ("Problem %d lower than %d\n",estepm, stepm);
5817: }
5818: else hstepm=estepm;
5819: /* We compute the life expectancy from trapezoids spaced every estepm months
5820: * This is mainly to measure the difference between two models: for example
5821: * if stepm=24 months pijx are given only every 2 years and by summing them
5822: * we are calculating an estimate of the Life Expectancy assuming a linear
5823: * progression in between and thus overestimating or underestimating according
5824: * to the curvature of the survival function. If, for the same date, we
5825: * estimate the model with stepm=1 month, we can keep estepm to 24 months
5826: * to compare the new estimate of Life expectancy with the same linear
5827: * hypothesis. A more precise result, taking into account a more precise
5828: * curvature will be obtained if estepm is as small as stepm. */
5829:
5830: /* For example we decided to compute the life expectancy with the smallest unit */
5831: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5832: nhstepm is the number of hstepm from age to agelim
5833: nstepm is the number of stepm from age to agelin.
5834: Look at hpijx to understand the reason of that which relies in memory size
5835: and note for a fixed period like estepm months */
5836: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
5837: survival function given by stepm (the optimization length). Unfortunately it
5838: means that if the survival funtion is printed only each two years of age and if
5839: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5840: results. So we changed our mind and took the option of the best precision.
5841: */
5842: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5843:
5844: /* If stepm=6 months */
5845: /* nhstepm age range expressed in number of stepm */
5846: agelim=AGESUP;
5847: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
5848: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5849: /* if (stepm >= YEARM) hstepm=1;*/
5850: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5851:
5852: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5853: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5854: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
5855: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
5856: gp=matrix(0,nhstepm,1,nlstate*nlstate);
5857: gm=matrix(0,nhstepm,1,nlstate*nlstate);
5858:
5859: for (age=bage; age<=fage; age ++){
5860: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
5861: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
5862: /* if (stepm >= YEARM) hstepm=1;*/
5863: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 5864:
1.126 brouard 5865: /* If stepm=6 months */
5866: /* Computed by stepm unit matrices, product of hstepma matrices, stored
5867: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
5868:
5869: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 5870:
1.126 brouard 5871: /* Computing Variances of health expectancies */
5872: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
5873: decrease memory allocation */
5874: for(theta=1; theta <=npar; theta++){
5875: for(i=1; i<=npar; i++){
1.222 brouard 5876: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5877: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 5878: }
1.235 brouard 5879: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres);
5880: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres);
1.218 brouard 5881:
1.126 brouard 5882: for(j=1; j<= nlstate; j++){
1.222 brouard 5883: for(i=1; i<=nlstate; i++){
5884: for(h=0; h<=nhstepm-1; h++){
5885: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
5886: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
5887: }
5888: }
1.126 brouard 5889: }
1.218 brouard 5890:
1.126 brouard 5891: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 5892: for(h=0; h<=nhstepm-1; h++){
5893: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
5894: }
1.126 brouard 5895: }/* End theta */
5896:
5897:
5898: for(h=0; h<=nhstepm-1; h++)
5899: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 5900: for(theta=1; theta <=npar; theta++)
5901: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 5902:
1.218 brouard 5903:
1.222 brouard 5904: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 5905: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 5906: varhe[ij][ji][(int)age] =0.;
1.218 brouard 5907:
1.222 brouard 5908: printf("%d|",(int)age);fflush(stdout);
5909: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
5910: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 5911: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 5912: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
5913: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
5914: for(ij=1;ij<=nlstate*nlstate;ij++)
5915: for(ji=1;ji<=nlstate*nlstate;ji++)
5916: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 5917: }
5918: }
1.218 brouard 5919:
1.126 brouard 5920: /* Computing expectancies */
1.235 brouard 5921: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres);
1.126 brouard 5922: for(i=1; i<=nlstate;i++)
5923: for(j=1; j<=nlstate;j++)
1.222 brouard 5924: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
5925: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 5926:
1.222 brouard 5927: /* 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 5928:
1.222 brouard 5929: }
1.269 brouard 5930:
5931: /* Standard deviation of expectancies ij */
1.126 brouard 5932: fprintf(ficresstdeij,"%3.0f",age );
5933: for(i=1; i<=nlstate;i++){
5934: eip=0.;
5935: vip=0.;
5936: for(j=1; j<=nlstate;j++){
1.222 brouard 5937: eip += eij[i][j][(int)age];
5938: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
5939: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
5940: 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 5941: }
5942: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
5943: }
5944: fprintf(ficresstdeij,"\n");
1.218 brouard 5945:
1.269 brouard 5946: /* Variance of expectancies ij */
1.126 brouard 5947: fprintf(ficrescveij,"%3.0f",age );
5948: for(i=1; i<=nlstate;i++)
5949: for(j=1; j<=nlstate;j++){
1.222 brouard 5950: cptj= (j-1)*nlstate+i;
5951: for(i2=1; i2<=nlstate;i2++)
5952: for(j2=1; j2<=nlstate;j2++){
5953: cptj2= (j2-1)*nlstate+i2;
5954: if(cptj2 <= cptj)
5955: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
5956: }
1.126 brouard 5957: }
5958: fprintf(ficrescveij,"\n");
1.218 brouard 5959:
1.126 brouard 5960: }
5961: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
5962: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
5963: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
5964: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
5965: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5966: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5967: printf("\n");
5968: fprintf(ficlog,"\n");
1.218 brouard 5969:
1.126 brouard 5970: free_vector(xm,1,npar);
5971: free_vector(xp,1,npar);
5972: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
5973: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
5974: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
5975: }
1.218 brouard 5976:
1.126 brouard 5977: /************ Variance ******************/
1.235 brouard 5978: 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 5979: {
1.279 brouard 5980: /** Variance of health expectancies
5981: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);
5982: * double **newm;
5983: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)
5984: */
1.218 brouard 5985:
5986: /* int movingaverage(); */
5987: double **dnewm,**doldm;
5988: double **dnewmp,**doldmp;
5989: int i, j, nhstepm, hstepm, h, nstepm ;
1.288 brouard 5990: int first=0;
1.218 brouard 5991: int k;
5992: double *xp;
1.279 brouard 5993: double **gp, **gm; /**< for var eij */
5994: double ***gradg, ***trgradg; /**< for var eij */
5995: double **gradgp, **trgradgp; /**< for var p point j */
5996: double *gpp, *gmp; /**< for var p point j */
5997: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */
1.218 brouard 5998: double ***p3mat;
5999: double age,agelim, hf;
6000: /* double ***mobaverage; */
6001: int theta;
6002: char digit[4];
6003: char digitp[25];
6004:
6005: char fileresprobmorprev[FILENAMELENGTH];
6006:
6007: if(popbased==1){
6008: if(mobilav!=0)
6009: strcpy(digitp,"-POPULBASED-MOBILAV_");
6010: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
6011: }
6012: else
6013: strcpy(digitp,"-STABLBASED_");
1.126 brouard 6014:
1.218 brouard 6015: /* if (mobilav!=0) { */
6016: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6017: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
6018: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
6019: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
6020: /* } */
6021: /* } */
6022:
6023: strcpy(fileresprobmorprev,"PRMORPREV-");
6024: sprintf(digit,"%-d",ij);
6025: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
6026: strcat(fileresprobmorprev,digit); /* Tvar to be done */
6027: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
6028: strcat(fileresprobmorprev,fileresu);
6029: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
6030: printf("Problem with resultfile: %s\n", fileresprobmorprev);
6031: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
6032: }
6033: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6034: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
6035: pstamp(ficresprobmorprev);
6036: 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 6037: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies");
6038: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
6039: fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
6040: }
6041: for(j=1;j<=cptcoveff;j++)
6042: fprintf(ficresprobmorprev,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,j)]);
6043: fprintf(ficresprobmorprev,"\n");
6044:
1.218 brouard 6045: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
6046: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6047: fprintf(ficresprobmorprev," p.%-d SE",j);
6048: for(i=1; i<=nlstate;i++)
6049: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
6050: }
6051: fprintf(ficresprobmorprev,"\n");
6052:
6053: fprintf(ficgp,"\n# Routine varevsij");
6054: fprintf(ficgp,"\nunset title \n");
6055: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
6056: 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");
6057: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
1.279 brouard 6058:
1.218 brouard 6059: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6060: pstamp(ficresvij);
6061: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
6062: if(popbased==1)
6063: 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);
6064: else
6065: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
6066: fprintf(ficresvij,"# Age");
6067: for(i=1; i<=nlstate;i++)
6068: for(j=1; j<=nlstate;j++)
6069: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
6070: fprintf(ficresvij,"\n");
6071:
6072: xp=vector(1,npar);
6073: dnewm=matrix(1,nlstate,1,npar);
6074: doldm=matrix(1,nlstate,1,nlstate);
6075: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
6076: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6077:
6078: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
6079: gpp=vector(nlstate+1,nlstate+ndeath);
6080: gmp=vector(nlstate+1,nlstate+ndeath);
6081: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 6082:
1.218 brouard 6083: if(estepm < stepm){
6084: printf ("Problem %d lower than %d\n",estepm, stepm);
6085: }
6086: else hstepm=estepm;
6087: /* For example we decided to compute the life expectancy with the smallest unit */
6088: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
6089: nhstepm is the number of hstepm from age to agelim
6090: nstepm is the number of stepm from age to agelim.
6091: Look at function hpijx to understand why because of memory size limitations,
6092: we decided (b) to get a life expectancy respecting the most precise curvature of the
6093: survival function given by stepm (the optimization length). Unfortunately it
6094: means that if the survival funtion is printed every two years of age and if
6095: you sum them up and add 1 year (area under the trapezoids) you won't get the same
6096: results. So we changed our mind and took the option of the best precision.
6097: */
6098: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
6099: agelim = AGESUP;
6100: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6101: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6102: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
6103: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6104: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
6105: gp=matrix(0,nhstepm,1,nlstate);
6106: gm=matrix(0,nhstepm,1,nlstate);
6107:
6108:
6109: for(theta=1; theta <=npar; theta++){
6110: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
6111: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6112: }
1.279 brouard 6113: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and
6114: * returns into prlim .
1.288 brouard 6115: */
1.242 brouard 6116: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.279 brouard 6117:
6118: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */
1.218 brouard 6119: if (popbased==1) {
6120: if(mobilav ==0){
6121: for(i=1; i<=nlstate;i++)
6122: prlim[i][i]=probs[(int)age][i][ij];
6123: }else{ /* mobilav */
6124: for(i=1; i<=nlstate;i++)
6125: prlim[i][i]=mobaverage[(int)age][i][ij];
6126: }
6127: }
1.295 brouard 6128: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h.
1.279 brouard 6129: */
6130: 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 6131: /**< 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 6132: * at horizon h in state j including mortality.
6133: */
1.218 brouard 6134: for(j=1; j<= nlstate; j++){
6135: for(h=0; h<=nhstepm; h++){
6136: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
6137: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
6138: }
6139: }
1.279 brouard 6140: /* Next for computing shifted+ probability of death (h=1 means
1.218 brouard 6141: computed over hstepm matrices product = hstepm*stepm months)
1.279 brouard 6142: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 .
1.218 brouard 6143: */
6144: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6145: for(i=1,gpp[j]=0.; i<= nlstate; i++)
6146: gpp[j] += prlim[i][i]*p3mat[i][j][1];
1.279 brouard 6147: }
6148:
6149: /* Again with minus shift */
1.218 brouard 6150:
6151: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
6152: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6153:
1.242 brouard 6154: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres);
1.218 brouard 6155:
6156: if (popbased==1) {
6157: if(mobilav ==0){
6158: for(i=1; i<=nlstate;i++)
6159: prlim[i][i]=probs[(int)age][i][ij];
6160: }else{ /* mobilav */
6161: for(i=1; i<=nlstate;i++)
6162: prlim[i][i]=mobaverage[(int)age][i][ij];
6163: }
6164: }
6165:
1.235 brouard 6166: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres);
1.218 brouard 6167:
6168: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
6169: for(h=0; h<=nhstepm; h++){
6170: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
6171: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
6172: }
6173: }
6174: /* This for computing probability of death (h=1 means
6175: computed over hstepm matrices product = hstepm*stepm months)
6176: as a weighted average of prlim.
6177: */
6178: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6179: for(i=1,gmp[j]=0.; i<= nlstate; i++)
6180: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6181: }
1.279 brouard 6182: /* end shifting computations */
6183:
6184: /**< Computing gradient matrix at horizon h
6185: */
1.218 brouard 6186: for(j=1; j<= nlstate; j++) /* vareij */
6187: for(h=0; h<=nhstepm; h++){
6188: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
6189: }
1.279 brouard 6190: /**< Gradient of overall mortality p.3 (or p.j)
6191: */
6192: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */
1.218 brouard 6193: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
6194: }
6195:
6196: } /* End theta */
1.279 brouard 6197:
6198: /* We got the gradient matrix for each theta and state j */
1.218 brouard 6199: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
6200:
6201: for(h=0; h<=nhstepm; h++) /* veij */
6202: for(j=1; j<=nlstate;j++)
6203: for(theta=1; theta <=npar; theta++)
6204: trgradg[h][j][theta]=gradg[h][theta][j];
6205:
6206: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
6207: for(theta=1; theta <=npar; theta++)
6208: trgradgp[j][theta]=gradgp[theta][j];
1.279 brouard 6209: /**< as well as its transposed matrix
6210: */
1.218 brouard 6211:
6212: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
6213: for(i=1;i<=nlstate;i++)
6214: for(j=1;j<=nlstate;j++)
6215: vareij[i][j][(int)age] =0.;
1.279 brouard 6216:
6217: /* Computing trgradg by matcov by gradg at age and summing over h
6218: * and k (nhstepm) formula 15 of article
6219: * Lievre-Brouard-Heathcote
6220: */
6221:
1.218 brouard 6222: for(h=0;h<=nhstepm;h++){
6223: for(k=0;k<=nhstepm;k++){
6224: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
6225: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
6226: for(i=1;i<=nlstate;i++)
6227: for(j=1;j<=nlstate;j++)
6228: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
6229: }
6230: }
6231:
1.279 brouard 6232: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of
6233: * p.j overall mortality formula 49 but computed directly because
6234: * we compute the grad (wix pijx) instead of grad (pijx),even if
6235: * wix is independent of theta.
6236: */
1.218 brouard 6237: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
6238: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
6239: for(j=nlstate+1;j<=nlstate+ndeath;j++)
6240: for(i=nlstate+1;i<=nlstate+ndeath;i++)
6241: varppt[j][i]=doldmp[j][i];
6242: /* end ppptj */
6243: /* x centered again */
6244:
1.242 brouard 6245: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres);
1.218 brouard 6246:
6247: if (popbased==1) {
6248: if(mobilav ==0){
6249: for(i=1; i<=nlstate;i++)
6250: prlim[i][i]=probs[(int)age][i][ij];
6251: }else{ /* mobilav */
6252: for(i=1; i<=nlstate;i++)
6253: prlim[i][i]=mobaverage[(int)age][i][ij];
6254: }
6255: }
6256:
6257: /* This for computing probability of death (h=1 means
6258: computed over hstepm (estepm) matrices product = hstepm*stepm months)
6259: as a weighted average of prlim.
6260: */
1.235 brouard 6261: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres);
1.218 brouard 6262: for(j=nlstate+1;j<=nlstate+ndeath;j++){
6263: for(i=1,gmp[j]=0.;i<= nlstate; i++)
6264: gmp[j] += prlim[i][i]*p3mat[i][j][1];
6265: }
6266: /* end probability of death */
6267:
6268: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
6269: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
6270: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
6271: for(i=1; i<=nlstate;i++){
6272: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
6273: }
6274: }
6275: fprintf(ficresprobmorprev,"\n");
6276:
6277: fprintf(ficresvij,"%.0f ",age );
6278: for(i=1; i<=nlstate;i++)
6279: for(j=1; j<=nlstate;j++){
6280: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
6281: }
6282: fprintf(ficresvij,"\n");
6283: free_matrix(gp,0,nhstepm,1,nlstate);
6284: free_matrix(gm,0,nhstepm,1,nlstate);
6285: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
6286: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
6287: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6288: } /* End age */
6289: free_vector(gpp,nlstate+1,nlstate+ndeath);
6290: free_vector(gmp,nlstate+1,nlstate+ndeath);
6291: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
6292: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
6293: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
6294: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
6295: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
6296: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
6297: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
6298: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
6299: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
6300: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
6301: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
6302: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
6303: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
6304: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
6305: 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);
6306: /* 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 6307: */
1.218 brouard 6308: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
6309: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 6310:
1.218 brouard 6311: free_vector(xp,1,npar);
6312: free_matrix(doldm,1,nlstate,1,nlstate);
6313: free_matrix(dnewm,1,nlstate,1,npar);
6314: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6315: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
6316: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
6317: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6318: fclose(ficresprobmorprev);
6319: fflush(ficgp);
6320: fflush(fichtm);
6321: } /* end varevsij */
1.126 brouard 6322:
6323: /************ Variance of prevlim ******************/
1.269 brouard 6324: 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 6325: {
1.205 brouard 6326: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 6327: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 6328:
1.268 brouard 6329: double **dnewmpar,**doldm;
1.126 brouard 6330: int i, j, nhstepm, hstepm;
6331: double *xp;
6332: double *gp, *gm;
6333: double **gradg, **trgradg;
1.208 brouard 6334: double **mgm, **mgp;
1.126 brouard 6335: double age,agelim;
6336: int theta;
6337:
6338: pstamp(ficresvpl);
1.288 brouard 6339: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n");
1.241 brouard 6340: fprintf(ficresvpl,"# Age ");
6341: if(nresult >=1)
6342: fprintf(ficresvpl," Result# ");
1.126 brouard 6343: for(i=1; i<=nlstate;i++)
6344: fprintf(ficresvpl," %1d-%1d",i,i);
6345: fprintf(ficresvpl,"\n");
6346:
6347: xp=vector(1,npar);
1.268 brouard 6348: dnewmpar=matrix(1,nlstate,1,npar);
1.126 brouard 6349: doldm=matrix(1,nlstate,1,nlstate);
6350:
6351: hstepm=1*YEARM; /* Every year of age */
6352: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6353: agelim = AGESUP;
6354: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
6355: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6356: if (stepm >= YEARM) hstepm=1;
6357: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6358: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 6359: mgp=matrix(1,npar,1,nlstate);
6360: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 6361: gp=vector(1,nlstate);
6362: gm=vector(1,nlstate);
6363:
6364: for(theta=1; theta <=npar; theta++){
6365: for(i=1; i<=npar; i++){ /* Computes gradient */
6366: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6367: }
1.288 brouard 6368: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6369: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6370: /* else */
6371: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6372: for(i=1;i<=nlstate;i++){
1.126 brouard 6373: gp[i] = prlim[i][i];
1.208 brouard 6374: mgp[theta][i] = prlim[i][i];
6375: }
1.126 brouard 6376: for(i=1; i<=npar; i++) /* Computes gradient */
6377: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.288 brouard 6378: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */
6379: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */
6380: /* else */
6381: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres);
1.208 brouard 6382: for(i=1;i<=nlstate;i++){
1.126 brouard 6383: gm[i] = prlim[i][i];
1.208 brouard 6384: mgm[theta][i] = prlim[i][i];
6385: }
1.126 brouard 6386: for(i=1;i<=nlstate;i++)
6387: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 6388: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 6389: } /* End theta */
6390:
6391: trgradg =matrix(1,nlstate,1,npar);
6392:
6393: for(j=1; j<=nlstate;j++)
6394: for(theta=1; theta <=npar; theta++)
6395: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 6396: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6397: /* printf("\nmgm mgp %d ",(int)age); */
6398: /* for(j=1; j<=nlstate;j++){ */
6399: /* printf(" %d ",j); */
6400: /* for(theta=1; theta <=npar; theta++) */
6401: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6402: /* printf("\n "); */
6403: /* } */
6404: /* } */
6405: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6406: /* printf("\n gradg %d ",(int)age); */
6407: /* for(j=1; j<=nlstate;j++){ */
6408: /* printf("%d ",j); */
6409: /* for(theta=1; theta <=npar; theta++) */
6410: /* printf("%d %lf ",theta,gradg[theta][j]); */
6411: /* printf("\n "); */
6412: /* } */
6413: /* } */
1.126 brouard 6414:
6415: for(i=1;i<=nlstate;i++)
6416: varpl[i][(int)age] =0.;
1.209 brouard 6417: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.268 brouard 6418: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6419: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6420: }else{
1.268 brouard 6421: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6422: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 6423: }
1.126 brouard 6424: for(i=1;i<=nlstate;i++)
6425: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6426:
6427: fprintf(ficresvpl,"%.0f ",age );
1.241 brouard 6428: if(nresult >=1)
6429: fprintf(ficresvpl,"%d ",nres );
1.288 brouard 6430: for(i=1; i<=nlstate;i++){
1.126 brouard 6431: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
1.288 brouard 6432: /* for(j=1;j<=nlstate;j++) */
6433: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */
6434: }
1.126 brouard 6435: fprintf(ficresvpl,"\n");
6436: free_vector(gp,1,nlstate);
6437: free_vector(gm,1,nlstate);
1.208 brouard 6438: free_matrix(mgm,1,npar,1,nlstate);
6439: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 6440: free_matrix(gradg,1,npar,1,nlstate);
6441: free_matrix(trgradg,1,nlstate,1,npar);
6442: } /* End age */
6443:
6444: free_vector(xp,1,npar);
6445: free_matrix(doldm,1,nlstate,1,npar);
1.268 brouard 6446: free_matrix(dnewmpar,1,nlstate,1,nlstate);
6447:
6448: }
6449:
6450:
6451: /************ Variance of backprevalence limit ******************/
1.269 brouard 6452: 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 6453: {
6454: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
6455: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
6456:
6457: double **dnewmpar,**doldm;
6458: int i, j, nhstepm, hstepm;
6459: double *xp;
6460: double *gp, *gm;
6461: double **gradg, **trgradg;
6462: double **mgm, **mgp;
6463: double age,agelim;
6464: int theta;
6465:
6466: pstamp(ficresvbl);
6467: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n");
6468: fprintf(ficresvbl,"# Age ");
6469: if(nresult >=1)
6470: fprintf(ficresvbl," Result# ");
6471: for(i=1; i<=nlstate;i++)
6472: fprintf(ficresvbl," %1d-%1d",i,i);
6473: fprintf(ficresvbl,"\n");
6474:
6475: xp=vector(1,npar);
6476: dnewmpar=matrix(1,nlstate,1,npar);
6477: doldm=matrix(1,nlstate,1,nlstate);
6478:
6479: hstepm=1*YEARM; /* Every year of age */
6480: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
6481: agelim = AGEINF;
6482: for (age=fage; age>=bage; age --){ /* If stepm=6 months */
6483: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
6484: if (stepm >= YEARM) hstepm=1;
6485: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
6486: gradg=matrix(1,npar,1,nlstate);
6487: mgp=matrix(1,npar,1,nlstate);
6488: mgm=matrix(1,npar,1,nlstate);
6489: gp=vector(1,nlstate);
6490: gm=vector(1,nlstate);
6491:
6492: for(theta=1; theta <=npar; theta++){
6493: for(i=1; i<=npar; i++){ /* Computes gradient */
6494: xp[i] = x[i] + (i==theta ?delti[theta]:0);
6495: }
6496: if(mobilavproj > 0 )
6497: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6498: else
6499: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6500: for(i=1;i<=nlstate;i++){
6501: gp[i] = bprlim[i][i];
6502: mgp[theta][i] = bprlim[i][i];
6503: }
6504: for(i=1; i<=npar; i++) /* Computes gradient */
6505: xp[i] = x[i] - (i==theta ?delti[theta]:0);
6506: if(mobilavproj > 0 )
6507: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6508: else
6509: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres);
6510: for(i=1;i<=nlstate;i++){
6511: gm[i] = bprlim[i][i];
6512: mgm[theta][i] = bprlim[i][i];
6513: }
6514: for(i=1;i<=nlstate;i++)
6515: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
6516: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
6517: } /* End theta */
6518:
6519: trgradg =matrix(1,nlstate,1,npar);
6520:
6521: for(j=1; j<=nlstate;j++)
6522: for(theta=1; theta <=npar; theta++)
6523: trgradg[j][theta]=gradg[theta][j];
6524: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6525: /* printf("\nmgm mgp %d ",(int)age); */
6526: /* for(j=1; j<=nlstate;j++){ */
6527: /* printf(" %d ",j); */
6528: /* for(theta=1; theta <=npar; theta++) */
6529: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
6530: /* printf("\n "); */
6531: /* } */
6532: /* } */
6533: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
6534: /* printf("\n gradg %d ",(int)age); */
6535: /* for(j=1; j<=nlstate;j++){ */
6536: /* printf("%d ",j); */
6537: /* for(theta=1; theta <=npar; theta++) */
6538: /* printf("%d %lf ",theta,gradg[theta][j]); */
6539: /* printf("\n "); */
6540: /* } */
6541: /* } */
6542:
6543: for(i=1;i<=nlstate;i++)
6544: varbpl[i][(int)age] =0.;
6545: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
6546: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6547: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6548: }else{
6549: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov);
6550: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg);
6551: }
6552: for(i=1;i<=nlstate;i++)
6553: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
6554:
6555: fprintf(ficresvbl,"%.0f ",age );
6556: if(nresult >=1)
6557: fprintf(ficresvbl,"%d ",nres );
6558: for(i=1; i<=nlstate;i++)
6559: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age]));
6560: fprintf(ficresvbl,"\n");
6561: free_vector(gp,1,nlstate);
6562: free_vector(gm,1,nlstate);
6563: free_matrix(mgm,1,npar,1,nlstate);
6564: free_matrix(mgp,1,npar,1,nlstate);
6565: free_matrix(gradg,1,npar,1,nlstate);
6566: free_matrix(trgradg,1,nlstate,1,npar);
6567: } /* End age */
6568:
6569: free_vector(xp,1,npar);
6570: free_matrix(doldm,1,nlstate,1,npar);
6571: free_matrix(dnewmpar,1,nlstate,1,nlstate);
1.126 brouard 6572:
6573: }
6574:
6575: /************ Variance of one-step probabilities ******************/
6576: 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 6577: {
6578: int i, j=0, k1, l1, tj;
6579: int k2, l2, j1, z1;
6580: int k=0, l;
6581: int first=1, first1, first2;
6582: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
6583: double **dnewm,**doldm;
6584: double *xp;
6585: double *gp, *gm;
6586: double **gradg, **trgradg;
6587: double **mu;
6588: double age, cov[NCOVMAX+1];
6589: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
6590: int theta;
6591: char fileresprob[FILENAMELENGTH];
6592: char fileresprobcov[FILENAMELENGTH];
6593: char fileresprobcor[FILENAMELENGTH];
6594: double ***varpij;
6595:
6596: strcpy(fileresprob,"PROB_");
6597: strcat(fileresprob,fileres);
6598: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
6599: printf("Problem with resultfile: %s\n", fileresprob);
6600: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
6601: }
6602: strcpy(fileresprobcov,"PROBCOV_");
6603: strcat(fileresprobcov,fileresu);
6604: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
6605: printf("Problem with resultfile: %s\n", fileresprobcov);
6606: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
6607: }
6608: strcpy(fileresprobcor,"PROBCOR_");
6609: strcat(fileresprobcor,fileresu);
6610: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
6611: printf("Problem with resultfile: %s\n", fileresprobcor);
6612: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
6613: }
6614: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6615: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
6616: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6617: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
6618: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6619: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
6620: pstamp(ficresprob);
6621: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
6622: fprintf(ficresprob,"# Age");
6623: pstamp(ficresprobcov);
6624: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
6625: fprintf(ficresprobcov,"# Age");
6626: pstamp(ficresprobcor);
6627: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
6628: fprintf(ficresprobcor,"# Age");
1.126 brouard 6629:
6630:
1.222 brouard 6631: for(i=1; i<=nlstate;i++)
6632: for(j=1; j<=(nlstate+ndeath);j++){
6633: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
6634: fprintf(ficresprobcov," p%1d-%1d ",i,j);
6635: fprintf(ficresprobcor," p%1d-%1d ",i,j);
6636: }
6637: /* fprintf(ficresprob,"\n");
6638: fprintf(ficresprobcov,"\n");
6639: fprintf(ficresprobcor,"\n");
6640: */
6641: xp=vector(1,npar);
6642: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6643: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6644: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
6645: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
6646: first=1;
6647: fprintf(ficgp,"\n# Routine varprob");
6648: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
6649: fprintf(fichtm,"\n");
6650:
1.288 brouard 6651: 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 6652: 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);
6653: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 6654: and drawn. It helps understanding how is the covariance between two incidences.\
6655: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 6656: 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 6657: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
6658: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
6659: standard deviations wide on each axis. <br>\
6660: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
6661: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
6662: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
6663:
1.222 brouard 6664: cov[1]=1;
6665: /* tj=cptcoveff; */
1.225 brouard 6666: tj = (int) pow(2,cptcoveff);
1.222 brouard 6667: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
6668: j1=0;
1.224 brouard 6669: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 6670: if (cptcovn>0) {
6671: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 6672: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6673: fprintf(ficresprob, "**********\n#\n");
6674: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 6675: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6676: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 6677:
1.222 brouard 6678: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 6679: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6680: fprintf(ficgp, "**********\n#\n");
1.220 brouard 6681:
6682:
1.222 brouard 6683: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 6684: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6685: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 6686:
1.222 brouard 6687: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 6688: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 6689: fprintf(ficresprobcor, "**********\n#");
6690: if(invalidvarcomb[j1]){
6691: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
6692: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
6693: continue;
6694: }
6695: }
6696: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
6697: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
6698: gp=vector(1,(nlstate)*(nlstate+ndeath));
6699: gm=vector(1,(nlstate)*(nlstate+ndeath));
6700: for (age=bage; age<=fage; age ++){
6701: cov[2]=age;
6702: if(nagesqr==1)
6703: cov[3]= age*age;
6704: for (k=1; k<=cptcovn;k++) {
6705: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
6706: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
6707: * 1 1 1 1 1
6708: * 2 2 1 1 1
6709: * 3 1 2 1 1
6710: */
6711: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
6712: }
6713: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
6714: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
6715: for (k=1; k<=cptcovprod;k++)
6716: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 6717:
6718:
1.222 brouard 6719: for(theta=1; theta <=npar; theta++){
6720: for(i=1; i<=npar; i++)
6721: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 6722:
1.222 brouard 6723: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 6724:
1.222 brouard 6725: k=0;
6726: for(i=1; i<= (nlstate); i++){
6727: for(j=1; j<=(nlstate+ndeath);j++){
6728: k=k+1;
6729: gp[k]=pmmij[i][j];
6730: }
6731: }
1.220 brouard 6732:
1.222 brouard 6733: for(i=1; i<=npar; i++)
6734: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 6735:
1.222 brouard 6736: pmij(pmmij,cov,ncovmodel,xp,nlstate);
6737: k=0;
6738: for(i=1; i<=(nlstate); i++){
6739: for(j=1; j<=(nlstate+ndeath);j++){
6740: k=k+1;
6741: gm[k]=pmmij[i][j];
6742: }
6743: }
1.220 brouard 6744:
1.222 brouard 6745: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
6746: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
6747: }
1.126 brouard 6748:
1.222 brouard 6749: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
6750: for(theta=1; theta <=npar; theta++)
6751: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 6752:
1.222 brouard 6753: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
6754: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 6755:
1.222 brouard 6756: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 6757:
1.222 brouard 6758: k=0;
6759: for(i=1; i<=(nlstate); i++){
6760: for(j=1; j<=(nlstate+ndeath);j++){
6761: k=k+1;
6762: mu[k][(int) age]=pmmij[i][j];
6763: }
6764: }
6765: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
6766: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
6767: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 6768:
1.222 brouard 6769: /*printf("\n%d ",(int)age);
6770: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
6771: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
6772: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
6773: }*/
1.220 brouard 6774:
1.222 brouard 6775: fprintf(ficresprob,"\n%d ",(int)age);
6776: fprintf(ficresprobcov,"\n%d ",(int)age);
6777: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 6778:
1.222 brouard 6779: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
6780: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
6781: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
6782: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
6783: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
6784: }
6785: i=0;
6786: for (k=1; k<=(nlstate);k++){
6787: for (l=1; l<=(nlstate+ndeath);l++){
6788: i++;
6789: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
6790: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
6791: for (j=1; j<=i;j++){
6792: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
6793: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
6794: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
6795: }
6796: }
6797: }/* end of loop for state */
6798: } /* end of loop for age */
6799: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
6800: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
6801: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6802: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
6803:
6804: /* Confidence intervalle of pij */
6805: /*
6806: fprintf(ficgp,"\nunset parametric;unset label");
6807: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
6808: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
6809: 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);
6810: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
6811: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
6812: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
6813: */
6814:
6815: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
6816: first1=1;first2=2;
6817: for (k2=1; k2<=(nlstate);k2++){
6818: for (l2=1; l2<=(nlstate+ndeath);l2++){
6819: if(l2==k2) continue;
6820: j=(k2-1)*(nlstate+ndeath)+l2;
6821: for (k1=1; k1<=(nlstate);k1++){
6822: for (l1=1; l1<=(nlstate+ndeath);l1++){
6823: if(l1==k1) continue;
6824: i=(k1-1)*(nlstate+ndeath)+l1;
6825: if(i<=j) continue;
6826: for (age=bage; age<=fage; age ++){
6827: if ((int)age %5==0){
6828: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
6829: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
6830: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
6831: mu1=mu[i][(int) age]/stepm*YEARM ;
6832: mu2=mu[j][(int) age]/stepm*YEARM;
6833: c12=cv12/sqrt(v1*v2);
6834: /* Computing eigen value of matrix of covariance */
6835: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6836: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
6837: if ((lc2 <0) || (lc1 <0) ){
6838: if(first2==1){
6839: first1=0;
6840: 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);
6841: }
6842: 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);
6843: /* lc1=fabs(lc1); */ /* If we want to have them positive */
6844: /* lc2=fabs(lc2); */
6845: }
1.220 brouard 6846:
1.222 brouard 6847: /* Eigen vectors */
1.280 brouard 6848: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){
6849: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
6850: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12);
6851: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12)));
6852: }else
6853: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
1.222 brouard 6854: /*v21=sqrt(1.-v11*v11); *//* error */
6855: v21=(lc1-v1)/cv12*v11;
6856: v12=-v21;
6857: v22=v11;
6858: tnalp=v21/v11;
6859: if(first1==1){
6860: first1=0;
6861: 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);
6862: }
6863: 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);
6864: /*printf(fignu*/
6865: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
6866: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
6867: if(first==1){
6868: first=0;
6869: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
6870: fprintf(ficgp,"\nset parametric;unset label");
6871: 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);
6872: fprintf(ficgp,"\nset ter svg size 640, 480");
1.266 brouard 6873: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 6874: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 6875: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 6876: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
6877: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6878: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6879: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
6880: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6881: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6882: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6883: 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 6884: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \
6885: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */
1.222 brouard 6886: }else{
6887: first=0;
6888: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
6889: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
6890: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
6891: 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 6892: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \
6893: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2)));
1.222 brouard 6894: }/* if first */
6895: } /* age mod 5 */
6896: } /* end loop age */
6897: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
6898: first=1;
6899: } /*l12 */
6900: } /* k12 */
6901: } /*l1 */
6902: }/* k1 */
6903: } /* loop on combination of covariates j1 */
6904: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
6905: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
6906: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
6907: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
6908: free_vector(xp,1,npar);
6909: fclose(ficresprob);
6910: fclose(ficresprobcov);
6911: fclose(ficresprobcor);
6912: fflush(ficgp);
6913: fflush(fichtmcov);
6914: }
1.126 brouard 6915:
6916:
6917: /******************* Printing html file ***********/
1.201 brouard 6918: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 6919: int lastpass, int stepm, int weightopt, char model[],\
6920: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.296 brouard 6921: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \
6922: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \
6923: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){
1.237 brouard 6924: int jj1, k1, i1, cpt, k4, nres;
1.126 brouard 6925:
6926: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
6927: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
6928: </ul>");
1.237 brouard 6929: fprintf(fichtm,"<ul><li> model=1+age+%s\n \
6930: </ul>", model);
1.214 brouard 6931: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
6932: 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",
6933: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
6934: 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 6935: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
6936: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 6937: fprintf(fichtm,"\
6938: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 6939: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 6940: fprintf(fichtm,"\
1.217 brouard 6941: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
6942: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
6943: fprintf(fichtm,"\
1.288 brouard 6944: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6945: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 6946: fprintf(fichtm,"\
1.288 brouard 6947: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.217 brouard 6948: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
6949: fprintf(fichtm,"\
1.211 brouard 6950: - (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 6951: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 6952: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 6953: if(prevfcast==1){
6954: fprintf(fichtm,"\
6955: - Prevalence projections by age and states: \
1.201 brouard 6956: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 6957: }
1.126 brouard 6958:
6959:
1.225 brouard 6960: m=pow(2,cptcoveff);
1.222 brouard 6961: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 6962:
1.264 brouard 6963: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
6964:
6965: jj1=0;
6966:
6967: fprintf(fichtm," \n<ul>");
6968: for(nres=1; nres <= nresult; nres++) /* For each resultline */
6969: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
6970: if(m != 1 && TKresult[nres]!= k1)
6971: continue;
6972: jj1++;
6973: if (cptcovn > 0) {
6974: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov");
6975: for (cpt=1; cpt<=cptcoveff;cpt++){
6976: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6977: }
6978: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6979: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
6980: }
6981: fprintf(fichtm,"\">");
6982:
6983: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
6984: fprintf(fichtm,"************ Results for covariates");
6985: for (cpt=1; cpt<=cptcoveff;cpt++){
6986: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
6987: }
6988: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
6989: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
6990: }
6991: if(invalidvarcomb[k1]){
6992: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1);
6993: continue;
6994: }
6995: fprintf(fichtm,"</a></li>");
6996: } /* cptcovn >0 */
6997: }
6998: fprintf(fichtm," \n</ul>");
6999:
1.222 brouard 7000: jj1=0;
1.237 brouard 7001:
7002: for(nres=1; nres <= nresult; nres++) /* For each resultline */
1.241 brouard 7003: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */
1.253 brouard 7004: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7005: continue;
1.220 brouard 7006:
1.222 brouard 7007: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7008: jj1++;
7009: if (cptcovn > 0) {
1.264 brouard 7010: fprintf(fichtm,"\n<p><a name=\"rescov");
7011: for (cpt=1; cpt<=cptcoveff;cpt++){
7012: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7013: }
7014: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7015: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]);
7016: }
7017: fprintf(fichtm,"\"</a>");
7018:
1.222 brouard 7019: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 7020: for (cpt=1; cpt<=cptcoveff;cpt++){
1.237 brouard 7021: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]);
7022: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout);
7023: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7024: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */
1.222 brouard 7025: }
1.237 brouard 7026: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7027: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7028: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout);
7029: }
7030:
1.230 brouard 7031: /* if(nqfveff+nqtveff 0) */ /* Test to be done */
1.222 brouard 7032: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
7033: if(invalidvarcomb[k1]){
7034: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
7035: printf("\nCombination (%d) ignored because no cases \n",k1);
7036: continue;
7037: }
7038: }
7039: /* aij, bij */
1.259 brouard 7040: 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 7041: <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 7042: /* Pij */
1.241 brouard 7043: 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> \
7044: <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 7045: /* Quasi-incidences */
7046: 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 7047: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 7048: 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 7049: 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> \
7050: <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 7051: /* Survival functions (period) in state j */
7052: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7053: 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 7054: <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 7055: }
7056: /* State specific survival functions (period) */
7057: for(cpt=1; cpt<=nlstate;cpt++){
1.292 brouard 7058: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\
7059: And probability to be observed in various states (up to %d) being in state %d at different ages. \
1.283 brouard 7060: <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 7061: }
1.288 brouard 7062: /* Period (forward stable) prevalence in each health state */
1.222 brouard 7063: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7064: 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> \
7065: <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 7066: }
1.296 brouard 7067: if(prevbcast==1){
1.288 brouard 7068: /* Backward prevalence in each health state */
1.222 brouard 7069: for(cpt=1; cpt<=nlstate;cpt++){
1.264 brouard 7070: 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 7071: <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 7072: }
1.217 brouard 7073: }
1.222 brouard 7074: if(prevfcast==1){
1.288 brouard 7075: /* Projection of prevalence up to period (forward stable) prevalence in each health state */
1.222 brouard 7076: for(cpt=1; cpt<=nlstate;cpt++){
1.288 brouard 7077: 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 7078: <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 7079: }
7080: }
1.296 brouard 7081: if(prevbcast==1){
1.268 brouard 7082: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */
7083: for(cpt=1; cpt<=nlstate;cpt++){
1.273 brouard 7084: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \
7085: 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 \
7086: 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) \
7087: with weights corresponding to observed prevalence at different ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a><br> \
7088: <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 7089: }
7090: }
1.220 brouard 7091:
1.222 brouard 7092: for(cpt=1; cpt<=nlstate;cpt++) {
1.241 brouard 7093: 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> \
7094: <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 7095: }
7096: /* } /\* end i1 *\/ */
7097: }/* End k1 */
7098: fprintf(fichtm,"</ul>");
1.126 brouard 7099:
1.222 brouard 7100: fprintf(fichtm,"\
1.126 brouard 7101: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 7102: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 7103: - 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 7104: But because parameters are usually highly correlated (a higher incidence of disability \
7105: and a higher incidence of recovery can give very close observed transition) it might \
7106: be very useful to look not only at linear confidence intervals estimated from the \
7107: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
7108: (parameters) of the logistic regression, it might be more meaningful to visualize the \
7109: covariance matrix of the one-step probabilities. \
7110: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 7111:
1.222 brouard 7112: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
7113: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
7114: fprintf(fichtm,"\
1.126 brouard 7115: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7116: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 7117:
1.222 brouard 7118: fprintf(fichtm,"\
1.126 brouard 7119: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 7120: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
7121: fprintf(fichtm,"\
1.126 brouard 7122: - 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): \
7123: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7124: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 7125: fprintf(fichtm,"\
1.126 brouard 7126: - (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): \
7127: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 7128: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 7129: fprintf(fichtm,"\
1.288 brouard 7130: - 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 7131: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
7132: fprintf(fichtm,"\
1.128 brouard 7133: - 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 7134: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
7135: fprintf(fichtm,"\
1.288 brouard 7136: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 7137: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 7138:
7139: /* if(popforecast==1) fprintf(fichtm,"\n */
7140: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
7141: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
7142: /* <br>",fileres,fileres,fileres,fileres); */
7143: /* else */
7144: /* 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 7145: fflush(fichtm);
7146: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 7147:
1.225 brouard 7148: m=pow(2,cptcoveff);
1.222 brouard 7149: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 7150:
1.222 brouard 7151: jj1=0;
1.237 brouard 7152:
1.241 brouard 7153: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.222 brouard 7154: for(k1=1; k1<=m;k1++){
1.253 brouard 7155: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7156: continue;
1.222 brouard 7157: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
7158: jj1++;
1.126 brouard 7159: if (cptcovn > 0) {
7160: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 7161: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.237 brouard 7162: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);
7163: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */
7164: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7165: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7166: }
7167:
1.126 brouard 7168: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 7169:
1.222 brouard 7170: if(invalidvarcomb[k1]){
7171: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
7172: continue;
7173: }
1.126 brouard 7174: }
7175: for(cpt=1; cpt<=nlstate;cpt++) {
1.258 brouard 7176: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \
1.241 brouard 7177: 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 7178: <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 7179: }
7180: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 7181: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
7182: true period expectancies (those weighted with period prevalences are also\
7183: drawn in addition to the population based expectancies computed using\
1.241 brouard 7184: observed and cahotic prevalences: <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a>\n<br>\
7185: <img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres);
1.222 brouard 7186: /* } /\* end i1 *\/ */
7187: }/* End k1 */
1.241 brouard 7188: }/* End nres */
1.222 brouard 7189: fprintf(fichtm,"</ul>");
7190: fflush(fichtm);
1.126 brouard 7191: }
7192:
7193: /******************* Gnuplot file **************/
1.296 brouard 7194: 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 7195:
7196: char dirfileres[132],optfileres[132];
1.264 brouard 7197: char gplotcondition[132], gplotlabel[132];
1.237 brouard 7198: 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 7199: int lv=0, vlv=0, kl=0;
1.130 brouard 7200: int ng=0;
1.201 brouard 7201: int vpopbased;
1.223 brouard 7202: int ioffset; /* variable offset for columns */
1.270 brouard 7203: int iyearc=1; /* variable column for year of projection */
7204: int iagec=1; /* variable column for age of projection */
1.235 brouard 7205: int nres=0; /* Index of resultline */
1.266 brouard 7206: int istart=1; /* For starting graphs in projections */
1.219 brouard 7207:
1.126 brouard 7208: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
7209: /* printf("Problem with file %s",optionfilegnuplot); */
7210: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
7211: /* } */
7212:
7213: /*#ifdef windows */
7214: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 7215: /*#endif */
1.225 brouard 7216: m=pow(2,cptcoveff);
1.126 brouard 7217:
1.274 brouard 7218: /* diagram of the model */
7219: fprintf(ficgp,"\n#Diagram of the model \n");
7220: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n");
7221: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate);
7222: 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);
7223:
7224: 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);
7225: fprintf(ficgp,"\n#show arrow\nunset label\n");
7226: 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);
7227: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate);
7228: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n");
7229: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_"));
7230: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n");
7231:
1.202 brouard 7232: /* Contribution to likelihood */
7233: /* Plot the probability implied in the likelihood */
1.223 brouard 7234: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
7235: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
7236: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
7237: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 7238: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 7239: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
7240: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 7241: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
7242: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
7243: 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));
7244: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
7245: 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));
7246: for (i=1; i<= nlstate ; i ++) {
7247: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
7248: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
7249: 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);
7250: for (j=2; j<= nlstate+ndeath ; j ++) {
7251: 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);
7252: }
7253: fprintf(ficgp,";\nset out; unset ylabel;\n");
7254: }
7255: /* 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 */
7256: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
7257: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
7258: fprintf(ficgp,"\nset out;unset log\n");
7259: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 7260:
1.126 brouard 7261: strcpy(dirfileres,optionfilefiname);
7262: strcpy(optfileres,"vpl");
1.223 brouard 7263: /* 1eme*/
1.238 brouard 7264: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */
7265: for (k1=1; k1<= m ; k1 ++){ /* For each valid combination of covariate */
1.236 brouard 7266: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.238 brouard 7267: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
1.253 brouard 7268: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7269: continue;
7270: /* We are interested in selected combination by the resultline */
1.246 brouard 7271: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */
1.288 brouard 7272: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt);
1.264 brouard 7273: strcpy(gplotlabel,"(");
1.238 brouard 7274: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
7275: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
7276: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7277: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7278: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7279: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
7280: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
1.246 brouard 7281: /* printf(" V%d=%d ",Tvaraff[k],vlv); */
1.238 brouard 7282: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7283: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7284: }
7285: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.246 brouard 7286: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */
1.238 brouard 7287: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7288: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7289: }
7290: strcpy(gplotlabel+strlen(gplotlabel),")");
1.246 brouard 7291: /* printf("\n#\n"); */
1.238 brouard 7292: fprintf(ficgp,"\n#\n");
7293: if(invalidvarcomb[k1]){
1.260 brouard 7294: /*k1=k1-1;*/ /* To be checked */
1.238 brouard 7295: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7296: continue;
7297: }
1.235 brouard 7298:
1.241 brouard 7299: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres);
7300: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres);
1.276 brouard 7301: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */
7302: fprintf(ficgp,"set title \"Alive state %d %s\" font \"Helvetica,12\"\n",cpt,gplotlabel);
1.260 brouard 7303: 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);
7304: /* 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); */
7305: /* k1-1 error should be nres-1*/
1.238 brouard 7306: for (i=1; i<= nlstate ; i ++) {
7307: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7308: else fprintf(ficgp," %%*lf (%%*lf)");
7309: }
1.288 brouard 7310: 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 7311: for (i=1; i<= nlstate ; i ++) {
7312: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7313: else fprintf(ficgp," %%*lf (%%*lf)");
7314: }
1.260 brouard 7315: 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 7316: for (i=1; i<= nlstate ; i ++) {
7317: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7318: else fprintf(ficgp," %%*lf (%%*lf)");
7319: }
1.265 brouard 7320: /* 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)); */
7321:
7322: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_"));
7323: if(cptcoveff ==0){
1.271 brouard 7324: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt );
1.265 brouard 7325: }else{
7326: kl=0;
7327: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7328: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7329: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7330: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7331: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7332: vlv= nbcode[Tvaraff[k]][lv];
7333: kl++;
7334: /* 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 *\/ */
7335: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7336: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7337: /* '' 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*/
7338: if(k==cptcoveff){
7339: 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], \
7340: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/
7341: }else{
7342: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7343: kl++;
7344: }
7345: } /* end covariate */
7346: } /* end if no covariate */
7347:
1.296 brouard 7348: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
1.238 brouard 7349: /* 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 7350: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */
1.238 brouard 7351: if(cptcoveff ==0){
1.245 brouard 7352: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt );
1.238 brouard 7353: }else{
7354: kl=0;
7355: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
7356: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7357: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7358: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7359: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7360: vlv= nbcode[Tvaraff[k]][lv];
1.223 brouard 7361: kl++;
1.238 brouard 7362: /* 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 *\/ */
7363: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7364: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7365: /* '' 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*/
7366: if(k==cptcoveff){
1.245 brouard 7367: 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 7368: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/
1.238 brouard 7369: }else{
7370: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
7371: kl++;
7372: }
7373: } /* end covariate */
7374: } /* end if no covariate */
1.296 brouard 7375: if(prevbcast == 1){
1.268 brouard 7376: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres);
7377: /* k1-1 error should be nres-1*/
7378: for (i=1; i<= nlstate ; i ++) {
7379: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7380: else fprintf(ficgp," %%*lf (%%*lf)");
7381: }
1.271 brouard 7382: 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 7383: for (i=1; i<= nlstate ; i ++) {
7384: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7385: else fprintf(ficgp," %%*lf (%%*lf)");
7386: }
1.276 brouard 7387: 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 7388: for (i=1; i<= nlstate ; i ++) {
7389: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
7390: else fprintf(ficgp," %%*lf (%%*lf)");
7391: }
1.274 brouard 7392: fprintf(ficgp,"\" t\"\" w l lt 4");
1.268 brouard 7393: } /* end if backprojcast */
1.296 brouard 7394: } /* end if prevbcast */
1.276 brouard 7395: /* fprintf(ficgp,"\nset out ;unset label;\n"); */
7396: fprintf(ficgp,"\nset out ;unset title;\n");
1.238 brouard 7397: } /* nres */
1.201 brouard 7398: } /* k1 */
7399: } /* cpt */
1.235 brouard 7400:
7401:
1.126 brouard 7402: /*2 eme*/
1.238 brouard 7403: for (k1=1; k1<= m ; k1 ++){
7404: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7405: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7406: continue;
7407: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.264 brouard 7408: strcpy(gplotlabel,"(");
1.238 brouard 7409: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.225 brouard 7410: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 7411: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7412: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7413: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7414: vlv= nbcode[Tvaraff[k]][lv];
7415: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7416: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7417: }
1.237 brouard 7418: /* for(k=1; k <= ncovds; k++){ */
1.236 brouard 7419: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 7420: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.236 brouard 7421: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7422: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7423: }
1.264 brouard 7424: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7425: fprintf(ficgp,"\n#\n");
1.223 brouard 7426: if(invalidvarcomb[k1]){
7427: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7428: continue;
7429: }
1.219 brouard 7430:
1.241 brouard 7431: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres);
1.238 brouard 7432: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.264 brouard 7433: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel);
7434: if(vpopbased==0){
1.238 brouard 7435: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
1.264 brouard 7436: }else
1.238 brouard 7437: fprintf(ficgp,"\nreplot ");
7438: for (i=1; i<= nlstate+1 ; i ++) {
7439: k=2*i;
1.261 brouard 7440: 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 7441: for (j=1; j<= nlstate+1 ; j ++) {
7442: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7443: else fprintf(ficgp," %%*lf (%%*lf)");
7444: }
7445: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
7446: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
1.261 brouard 7447: 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 7448: for (j=1; j<= nlstate+1 ; j ++) {
7449: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7450: else fprintf(ficgp," %%*lf (%%*lf)");
7451: }
7452: fprintf(ficgp,"\" t\"\" w l lt 0,");
1.261 brouard 7453: 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 7454: for (j=1; j<= nlstate+1 ; j ++) {
7455: if (j==i) fprintf(ficgp," %%lf (%%lf)");
7456: else fprintf(ficgp," %%*lf (%%*lf)");
7457: }
7458: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
7459: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
7460: } /* state */
7461: } /* vpopbased */
1.264 brouard 7462: 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 7463: } /* end nres */
7464: } /* k1 end 2 eme*/
7465:
7466:
7467: /*3eme*/
7468: for (k1=1; k1<= m ; k1 ++){
7469: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7470: if(m != 1 && TKresult[nres]!= k1)
1.238 brouard 7471: continue;
7472:
7473: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.261 brouard 7474: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt);
1.264 brouard 7475: strcpy(gplotlabel,"(");
1.238 brouard 7476: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7477: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7478: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7479: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7480: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7481: vlv= nbcode[Tvaraff[k]][lv];
7482: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7483: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7484: }
7485: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7486: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7487: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7488: }
1.264 brouard 7489: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7490: fprintf(ficgp,"\n#\n");
7491: if(invalidvarcomb[k1]){
7492: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7493: continue;
7494: }
7495:
7496: /* k=2+nlstate*(2*cpt-2); */
7497: k=2+(nlstate+1)*(cpt-1);
1.241 brouard 7498: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres);
1.264 brouard 7499: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel);
1.238 brouard 7500: fprintf(ficgp,"set ter svg size 640, 480\n\
1.261 brouard 7501: 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 7502: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7503: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7504: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
7505: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
7506: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
7507: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 7508:
1.238 brouard 7509: */
7510: for (i=1; i< nlstate ; i ++) {
1.261 brouard 7511: 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 7512: /* 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 7513:
1.238 brouard 7514: }
1.261 brouard 7515: 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 7516: }
1.264 brouard 7517: fprintf(ficgp,"\nunset label;\n");
1.238 brouard 7518: } /* end nres */
7519: } /* end kl 3eme */
1.126 brouard 7520:
1.223 brouard 7521: /* 4eme */
1.201 brouard 7522: /* Survival functions (period) from state i in state j by initial state i */
1.238 brouard 7523: for (k1=1; k1<=m; k1++){ /* For each covariate and each value */
7524: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7525: if(m != 1 && TKresult[nres]!= k1)
1.223 brouard 7526: continue;
1.238 brouard 7527: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/
1.264 brouard 7528: strcpy(gplotlabel,"(");
1.238 brouard 7529: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
7530: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7531: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7532: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7533: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7534: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7535: vlv= nbcode[Tvaraff[k]][lv];
7536: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7537: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7538: }
7539: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7540: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7541: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7542: }
1.264 brouard 7543: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7544: fprintf(ficgp,"\n#\n");
7545: if(invalidvarcomb[k1]){
7546: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7547: continue;
1.223 brouard 7548: }
1.238 brouard 7549:
1.241 brouard 7550: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres);
1.264 brouard 7551: 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 7552: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7553: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7554: k=3;
7555: for (i=1; i<= nlstate ; i ++){
7556: if(i==1){
7557: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7558: }else{
7559: fprintf(ficgp,", '' ");
7560: }
7561: l=(nlstate+ndeath)*(i-1)+1;
7562: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
7563: for (j=2; j<= nlstate+ndeath ; j ++)
7564: fprintf(ficgp,"+$%d",k+l+j-1);
7565: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
7566: } /* nlstate */
1.264 brouard 7567: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7568: } /* end cpt state*/
7569: } /* end nres */
7570: } /* end covariate k1 */
7571:
1.220 brouard 7572: /* 5eme */
1.201 brouard 7573: /* Survival functions (period) from state i in state j by final state j */
1.238 brouard 7574: for (k1=1; k1<= m ; k1++){ /* For each covariate combination if any */
7575: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7576: if(m != 1 && TKresult[nres]!= k1)
1.227 brouard 7577: continue;
1.238 brouard 7578: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.264 brouard 7579: strcpy(gplotlabel,"(");
1.238 brouard 7580: 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);
7581: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7582: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7583: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7584: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7585: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7586: vlv= nbcode[Tvaraff[k]][lv];
7587: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7588: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.238 brouard 7589: }
7590: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7591: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7592: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.238 brouard 7593: }
1.264 brouard 7594: strcpy(gplotlabel+strlen(gplotlabel),")");
1.238 brouard 7595: fprintf(ficgp,"\n#\n");
7596: if(invalidvarcomb[k1]){
7597: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7598: continue;
7599: }
1.227 brouard 7600:
1.241 brouard 7601: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres);
1.264 brouard 7602: 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 7603: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
7604: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7605: k=3;
7606: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7607: if(j==1)
7608: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7609: else
7610: fprintf(ficgp,", '' ");
7611: l=(nlstate+ndeath)*(cpt-1) +j;
7612: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
7613: /* for (i=2; i<= nlstate+ndeath ; i ++) */
7614: /* fprintf(ficgp,"+$%d",k+l+i-1); */
7615: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
7616: } /* nlstate */
7617: fprintf(ficgp,", '' ");
7618: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
7619: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
7620: l=(nlstate+ndeath)*(cpt-1) +j;
7621: if(j < nlstate)
7622: fprintf(ficgp,"$%d +",k+l);
7623: else
7624: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
7625: }
1.264 brouard 7626: fprintf(ficgp,"\nset out; unset label;\n");
1.238 brouard 7627: } /* end cpt state*/
7628: } /* end covariate */
7629: } /* end nres */
1.227 brouard 7630:
1.220 brouard 7631: /* 6eme */
1.202 brouard 7632: /* CV preval stable (period) for each covariate */
1.237 brouard 7633: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7634: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7635: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7636: continue;
1.255 brouard 7637: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */
1.264 brouard 7638: strcpy(gplotlabel,"(");
1.288 brouard 7639: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 7640: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
1.227 brouard 7641: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7642: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7643: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7644: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7645: vlv= nbcode[Tvaraff[k]][lv];
7646: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7647: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 7648: }
1.237 brouard 7649: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7650: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7651: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7652: }
1.264 brouard 7653: strcpy(gplotlabel+strlen(gplotlabel),")");
1.211 brouard 7654: fprintf(ficgp,"\n#\n");
1.223 brouard 7655: if(invalidvarcomb[k1]){
1.227 brouard 7656: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7657: continue;
1.223 brouard 7658: }
1.227 brouard 7659:
1.241 brouard 7660: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres);
1.264 brouard 7661: 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 7662: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 7663: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 7664: k=3; /* Offset */
1.255 brouard 7665: for (i=1; i<= nlstate ; i ++){ /* State of origin */
1.227 brouard 7666: if(i==1)
7667: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
7668: else
7669: fprintf(ficgp,", '' ");
1.255 brouard 7670: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 7671: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
7672: for (j=2; j<= nlstate ; j ++)
7673: fprintf(ficgp,"+$%d",k+l+j-1);
7674: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 7675: } /* nlstate */
1.264 brouard 7676: fprintf(ficgp,"\nset out; unset label;\n");
1.153 brouard 7677: } /* end cpt state*/
7678: } /* end covariate */
1.227 brouard 7679:
7680:
1.220 brouard 7681: /* 7eme */
1.296 brouard 7682: if(prevbcast == 1){
1.288 brouard 7683: /* CV backward prevalence for each covariate */
1.237 brouard 7684: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7685: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7686: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7687: continue;
1.268 brouard 7688: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */
1.264 brouard 7689: strcpy(gplotlabel,"(");
1.288 brouard 7690: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 7691: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
7692: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
7693: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7694: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 7695: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.227 brouard 7696: vlv= nbcode[Tvaraff[k]][lv];
7697: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7698: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 7699: }
1.237 brouard 7700: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7701: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7702: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7703: }
1.264 brouard 7704: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 7705: fprintf(ficgp,"\n#\n");
7706: if(invalidvarcomb[k1]){
7707: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7708: continue;
7709: }
7710:
1.241 brouard 7711: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres);
1.268 brouard 7712: 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 7713: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.238 brouard 7714: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.227 brouard 7715: k=3; /* Offset */
1.268 brouard 7716: for (i=1; i<= nlstate ; i ++){ /* State of arrival */
1.227 brouard 7717: if(i==1)
7718: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
7719: else
7720: fprintf(ficgp,", '' ");
7721: /* l=(nlstate+ndeath)*(i-1)+1; */
1.255 brouard 7722: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */
1.227 brouard 7723: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
7724: /* 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 7725: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */
1.227 brouard 7726: /* for (j=2; j<= nlstate ; j ++) */
7727: /* fprintf(ficgp,"+$%d",k+l+j-1); */
7728: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
1.268 brouard 7729: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i);
1.227 brouard 7730: } /* nlstate */
1.264 brouard 7731: fprintf(ficgp,"\nset out; unset label;\n");
1.218 brouard 7732: } /* end cpt state*/
7733: } /* end covariate */
1.296 brouard 7734: } /* End if prevbcast */
1.218 brouard 7735:
1.223 brouard 7736: /* 8eme */
1.218 brouard 7737: if(prevfcast==1){
1.288 brouard 7738: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */
1.218 brouard 7739:
1.237 brouard 7740: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7741: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 7742: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 7743: continue;
1.211 brouard 7744: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.264 brouard 7745: strcpy(gplotlabel,"(");
1.288 brouard 7746: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.227 brouard 7747: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
7748: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7749: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7750: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7751: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7752: vlv= nbcode[Tvaraff[k]][lv];
7753: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.264 brouard 7754: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
1.227 brouard 7755: }
1.237 brouard 7756: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7757: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 7758: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 7759: }
1.264 brouard 7760: strcpy(gplotlabel+strlen(gplotlabel),")");
1.227 brouard 7761: fprintf(ficgp,"\n#\n");
7762: if(invalidvarcomb[k1]){
7763: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7764: continue;
7765: }
7766:
7767: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
1.241 brouard 7768: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres);
1.264 brouard 7769: 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 7770: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.238 brouard 7771: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
1.266 brouard 7772:
7773: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
7774: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
7775: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
7776: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
1.227 brouard 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: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7780: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.266 brouard 7781: if(i==istart){
1.227 brouard 7782: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
7783: }else{
7784: fprintf(ficgp,",\\\n '' ");
7785: }
7786: if(cptcoveff ==0){ /* No covariate */
7787: ioffset=2; /* Age is in 2 */
7788: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7789: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7790: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7791: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7792: fprintf(ficgp," u %d:(", ioffset);
1.266 brouard 7793: if(i==nlstate+1){
1.270 brouard 7794: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \
1.266 brouard 7795: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
7796: fprintf(ficgp,",\\\n '' ");
7797: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 7798: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \
1.266 brouard 7799: offyear, \
1.268 brouard 7800: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate );
1.266 brouard 7801: }else
1.227 brouard 7802: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
7803: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
7804: }else{ /* more than 2 covariates */
1.270 brouard 7805: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
7806: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7807: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7808: iyearc=ioffset-1;
7809: iagec=ioffset;
1.227 brouard 7810: fprintf(ficgp," u %d:(",ioffset);
7811: kl=0;
7812: strcpy(gplotcondition,"(");
7813: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
7814: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
7815: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7816: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7817: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7818: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
7819: kl++;
7820: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
7821: kl++;
7822: if(k <cptcoveff && cptcoveff>1)
7823: sprintf(gplotcondition+strlen(gplotcondition)," && ");
7824: }
7825: strcpy(gplotcondition+strlen(gplotcondition),")");
7826: /* 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 *\/ */
7827: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7828: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7829: /* '' 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*/
7830: if(i==nlstate+1){
1.270 brouard 7831: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \
7832: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt );
1.266 brouard 7833: fprintf(ficgp,",\\\n '' ");
1.270 brouard 7834: fprintf(ficgp," u %d:(",iagec);
7835: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \
7836: iyearc, iagec, offyear, \
7837: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc );
1.266 brouard 7838: /* '' 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 7839: }else{
7840: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
7841: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
7842: }
7843: } /* end if covariate */
7844: } /* nlstate */
1.264 brouard 7845: fprintf(ficgp,"\nset out; unset label;\n");
1.223 brouard 7846: } /* end cpt state*/
7847: } /* end covariate */
7848: } /* End if prevfcast */
1.227 brouard 7849:
1.296 brouard 7850: if(prevbcast==1){
1.268 brouard 7851: /* Back projection from cross-sectional to stable (mixed) for each covariate */
7852:
7853: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */
7854: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
7855: if(m != 1 && TKresult[nres]!= k1)
7856: continue;
7857: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
7858: strcpy(gplotlabel,"(");
7859: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt);
7860: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
7861: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
7862: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7863: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7864: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7865: vlv= nbcode[Tvaraff[k]][lv];
7866: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
7867: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
7868: }
7869: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
7870: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7871: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
7872: }
7873: strcpy(gplotlabel+strlen(gplotlabel),")");
7874: fprintf(ficgp,"\n#\n");
7875: if(invalidvarcomb[k1]){
7876: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
7877: continue;
7878: }
7879:
7880: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n ");
7881: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres);
7882: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel);
7883: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
7884: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar);
7885:
7886: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */
7887: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */
7888: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */
7889: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
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: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7893: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7894: if(i==istart){
7895: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_"));
7896: }else{
7897: fprintf(ficgp,",\\\n '' ");
7898: }
7899: if(cptcoveff ==0){ /* No covariate */
7900: ioffset=2; /* Age is in 2 */
7901: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7902: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7903: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
7904: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
7905: fprintf(ficgp," u %d:(", ioffset);
7906: if(i==nlstate+1){
1.270 brouard 7907: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \
1.268 brouard 7908: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
7909: fprintf(ficgp,",\\\n '' ");
7910: fprintf(ficgp," u %d:(",ioffset);
1.270 brouard 7911: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \
1.268 brouard 7912: offbyear, \
7913: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) );
7914: }else
7915: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \
7916: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i );
7917: }else{ /* more than 2 covariates */
1.270 brouard 7918: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/
7919: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
7920: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
7921: iyearc=ioffset-1;
7922: iagec=ioffset;
1.268 brouard 7923: fprintf(ficgp," u %d:(",ioffset);
7924: kl=0;
7925: strcpy(gplotcondition,"(");
7926: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
7927: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
7928: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
7929: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
7930: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
7931: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
7932: kl++;
7933: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
7934: kl++;
7935: if(k <cptcoveff && cptcoveff>1)
7936: sprintf(gplotcondition+strlen(gplotcondition)," && ");
7937: }
7938: strcpy(gplotcondition+strlen(gplotcondition),")");
7939: /* 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 *\/ */
7940: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
7941: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
7942: /* '' 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*/
7943: if(i==nlstate+1){
1.270 brouard 7944: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \
7945: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt );
1.268 brouard 7946: fprintf(ficgp,",\\\n '' ");
1.270 brouard 7947: fprintf(ficgp," u %d:(",iagec);
1.268 brouard 7948: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */
1.270 brouard 7949: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \
7950: iyearc,iagec,offbyear, \
7951: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc );
1.268 brouard 7952: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/
7953: }else{
7954: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */
7955: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \
7956: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i );
7957: }
7958: } /* end if covariate */
7959: } /* nlstate */
7960: fprintf(ficgp,"\nset out; unset label;\n");
7961: } /* end cpt state*/
7962: } /* end covariate */
1.296 brouard 7963: } /* End if prevbcast */
1.268 brouard 7964:
1.227 brouard 7965:
1.238 brouard 7966: /* 9eme writing MLE parameters */
7967: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n");
1.126 brouard 7968: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 7969: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 7970: for(k=1; k <=(nlstate+ndeath); k++){
7971: if (k != i) {
1.227 brouard 7972: fprintf(ficgp,"# current state %d\n",k);
7973: for(j=1; j <=ncovmodel; j++){
7974: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
7975: jk++;
7976: }
7977: fprintf(ficgp,"\n");
1.126 brouard 7978: }
7979: }
1.223 brouard 7980: }
1.187 brouard 7981: fprintf(ficgp,"##############\n#\n");
1.227 brouard 7982:
1.145 brouard 7983: /*goto avoid;*/
1.238 brouard 7984: /* 10eme Graphics of probabilities or incidences using written MLE parameters */
7985: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n");
1.187 brouard 7986: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
7987: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
7988: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
7989: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
7990: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
7991: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
7992: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
7993: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
7994: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
7995: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
7996: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
7997: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
7998: fprintf(ficgp,"#\n");
1.223 brouard 7999: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.238 brouard 8000: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n");
1.237 brouard 8001: fprintf(ficgp,"#model=%s \n",model);
1.238 brouard 8002: fprintf(ficgp,"# Type of graphic ng=%d\n",ng);
1.264 brouard 8003: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */
8004: for(k1=1; k1 <=m; k1++) /* For each combination of covariate */
1.237 brouard 8005: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.264 brouard 8006: if(m != 1 && TKresult[nres]!= k1)
1.237 brouard 8007: continue;
1.264 brouard 8008: fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1);
8009: strcpy(gplotlabel,"(");
1.276 brouard 8010: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/
1.264 brouard 8011: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
8012: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
8013: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
8014: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
8015: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
8016: vlv= nbcode[Tvaraff[k]][lv];
8017: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
8018: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv);
8019: }
1.237 brouard 8020: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8021: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.264 brouard 8022: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.237 brouard 8023: }
1.264 brouard 8024: strcpy(gplotlabel+strlen(gplotlabel),")");
1.237 brouard 8025: fprintf(ficgp,"\n#\n");
1.264 brouard 8026: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres);
1.276 brouard 8027: fprintf(ficgp,"\nset key outside ");
8028: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */
8029: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel);
1.223 brouard 8030: fprintf(ficgp,"\nset ter svg size 640, 480 ");
8031: if (ng==1){
8032: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
8033: fprintf(ficgp,"\nunset log y");
8034: }else if (ng==2){
8035: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
8036: fprintf(ficgp,"\nset log y");
8037: }else if (ng==3){
8038: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
8039: fprintf(ficgp,"\nset log y");
8040: }else
8041: fprintf(ficgp,"\nunset title ");
8042: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
8043: i=1;
8044: for(k2=1; k2<=nlstate; k2++) {
8045: k3=i;
8046: for(k=1; k<=(nlstate+ndeath); k++) {
8047: if (k != k2){
8048: switch( ng) {
8049: case 1:
8050: if(nagesqr==0)
8051: fprintf(ficgp," p%d+p%d*x",i,i+1);
8052: else /* nagesqr =1 */
8053: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8054: break;
8055: case 2: /* ng=2 */
8056: if(nagesqr==0)
8057: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
8058: else /* nagesqr =1 */
8059: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
8060: break;
8061: case 3:
8062: if(nagesqr==0)
8063: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
8064: else /* nagesqr =1 */
8065: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
8066: break;
8067: }
8068: ij=1;/* To be checked else nbcode[0][0] wrong */
1.237 brouard 8069: ijp=1; /* product no age */
8070: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */
8071: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */
1.223 brouard 8072: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
1.268 brouard 8073: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8074: if(j==Tage[ij]) { /* Product by age To be looked at!!*/
8075: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */
8076: if(DummyV[j]==0){
8077: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);;
8078: }else{ /* quantitative */
8079: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */
8080: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8081: }
8082: ij++;
1.237 brouard 8083: }
1.268 brouard 8084: }
8085: }else if(cptcovprod >0){
8086: if(j==Tprod[ijp]) { /* */
8087: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */
8088: if(ijp <=cptcovprod) { /* Product */
8089: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */
8090: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */
8091: /* 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)]); */
8092: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]);
8093: }else{ /* Vn is dummy and Vm is quanti */
8094: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */
8095: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8096: }
8097: }else{ /* Vn*Vm Vn is quanti */
8098: if(DummyV[Tvard[ijp][2]]==0){
8099: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]);
8100: }else{ /* Both quanti */
8101: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]);
8102: }
1.237 brouard 8103: }
1.268 brouard 8104: ijp++;
1.237 brouard 8105: }
1.268 brouard 8106: } /* end Tprod */
1.237 brouard 8107: } else{ /* simple covariate */
1.264 brouard 8108: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */
1.237 brouard 8109: if(Dummy[j]==0){
8110: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */
8111: }else{ /* quantitative */
8112: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */
1.264 brouard 8113: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
1.223 brouard 8114: }
1.237 brouard 8115: } /* end simple */
8116: } /* end j */
1.223 brouard 8117: }else{
8118: i=i-ncovmodel;
8119: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
8120: fprintf(ficgp," (1.");
8121: }
1.227 brouard 8122:
1.223 brouard 8123: if(ng != 1){
8124: fprintf(ficgp,")/(1");
1.227 brouard 8125:
1.264 brouard 8126: for(cpt=1; cpt <=nlstate; cpt++){
1.223 brouard 8127: if(nagesqr==0)
1.264 brouard 8128: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1);
1.223 brouard 8129: else /* nagesqr =1 */
1.264 brouard 8130: 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 8131:
1.223 brouard 8132: ij=1;
8133: for(j=3; j <=ncovmodel-nagesqr; j++){
1.268 brouard 8134: if(cptcovage >0){
8135: if((j-2)==Tage[ij]) { /* Bug valgrind */
8136: if(ij <=cptcovage) { /* Bug valgrind */
8137: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);
8138: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */
8139: ij++;
8140: }
8141: }
8142: }else
8143: 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 8144: }
8145: fprintf(ficgp,")");
8146: }
8147: fprintf(ficgp,")");
8148: if(ng ==2)
1.276 brouard 8149: 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 8150: else /* ng= 3 */
1.276 brouard 8151: 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 8152: }else{ /* end ng <> 1 */
8153: if( k !=k2) /* logit p11 is hard to draw */
1.276 brouard 8154: 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 8155: }
8156: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
8157: fprintf(ficgp,",");
8158: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
8159: fprintf(ficgp,",");
8160: i=i+ncovmodel;
8161: } /* end k */
8162: } /* end k2 */
1.276 brouard 8163: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */
8164: fprintf(ficgp,"\n set out; unset title;set key default;\n");
1.264 brouard 8165: } /* end k1 */
1.223 brouard 8166: } /* end ng */
8167: /* avoid: */
8168: fflush(ficgp);
1.126 brouard 8169: } /* end gnuplot */
8170:
8171:
8172: /*************** Moving average **************/
1.219 brouard 8173: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 8174: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 8175:
1.222 brouard 8176: int i, cpt, cptcod;
8177: int modcovmax =1;
8178: int mobilavrange, mob;
8179: int iage=0;
1.288 brouard 8180: int firstA1=0, firstA2=0;
1.222 brouard 8181:
1.266 brouard 8182: double sum=0., sumr=0.;
1.222 brouard 8183: double age;
1.266 brouard 8184: double *sumnewp, *sumnewm, *sumnewmr;
8185: double *agemingood, *agemaxgood;
8186: double *agemingoodr, *agemaxgoodr;
1.222 brouard 8187:
8188:
1.278 brouard 8189: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */
8190: /* a covariate has 2 modalities, should be equal to ncovcombmax */
1.222 brouard 8191:
8192: sumnewp = vector(1,ncovcombmax);
8193: sumnewm = vector(1,ncovcombmax);
1.266 brouard 8194: sumnewmr = vector(1,ncovcombmax);
1.222 brouard 8195: agemingood = vector(1,ncovcombmax);
1.266 brouard 8196: agemingoodr = vector(1,ncovcombmax);
1.222 brouard 8197: agemaxgood = vector(1,ncovcombmax);
1.266 brouard 8198: agemaxgoodr = vector(1,ncovcombmax);
1.222 brouard 8199:
8200: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
1.266 brouard 8201: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.;
1.222 brouard 8202: sumnewp[cptcod]=0.;
1.266 brouard 8203: agemingood[cptcod]=0, agemingoodr[cptcod]=0;
8204: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0;
1.222 brouard 8205: }
8206: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
8207:
1.266 brouard 8208: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
8209: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */
1.222 brouard 8210: else mobilavrange=mobilav;
8211: for (age=bage; age<=fage; age++)
8212: for (i=1; i<=nlstate;i++)
8213: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
8214: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8215: /* We keep the original values on the extreme ages bage, fage and for
8216: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
8217: we use a 5 terms etc. until the borders are no more concerned.
8218: */
8219: for (mob=3;mob <=mobilavrange;mob=mob+2){
8220: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
1.266 brouard 8221: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
8222: sumnewm[cptcod]=0.;
8223: for (i=1; i<=nlstate;i++){
1.222 brouard 8224: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
8225: for (cpt=1;cpt<=(mob-1)/2;cpt++){
8226: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
8227: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
8228: }
8229: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
1.266 brouard 8230: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8231: } /* end i */
8232: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */
8233: } /* end cptcod */
1.222 brouard 8234: }/* end age */
8235: }/* end mob */
1.266 brouard 8236: }else{
8237: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav);
1.222 brouard 8238: return -1;
1.266 brouard 8239: }
8240:
8241: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */
1.222 brouard 8242: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
8243: if(invalidvarcomb[cptcod]){
8244: printf("\nCombination (%d) ignored because no cases \n",cptcod);
8245: continue;
8246: }
1.219 brouard 8247:
1.266 brouard 8248: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */
8249: sumnewm[cptcod]=0.;
8250: sumnewmr[cptcod]=0.;
8251: for (i=1; i<=nlstate;i++){
8252: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8253: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8254: }
8255: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8256: agemingoodr[cptcod]=age;
8257: }
8258: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8259: agemingood[cptcod]=age;
8260: }
8261: } /* age */
8262: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */
1.222 brouard 8263: sumnewm[cptcod]=0.;
1.266 brouard 8264: sumnewmr[cptcod]=0.;
1.222 brouard 8265: for (i=1; i<=nlstate;i++){
8266: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8267: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8268: }
8269: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8270: agemaxgoodr[cptcod]=age;
1.222 brouard 8271: }
8272: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
1.266 brouard 8273: agemaxgood[cptcod]=age;
8274: }
8275: } /* age */
8276: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */
8277: /* but they will change */
1.288 brouard 8278: firstA1=0;firstA2=0;
1.266 brouard 8279: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */
8280: sumnewm[cptcod]=0.;
8281: sumnewmr[cptcod]=0.;
8282: for (i=1; i<=nlstate;i++){
8283: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8284: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8285: }
8286: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8287: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */
8288: agemaxgoodr[cptcod]=age; /* age min */
8289: for (i=1; i<=nlstate;i++)
8290: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8291: }else{ /* bad we change the value with the values of good ages */
8292: for (i=1; i<=nlstate;i++){
8293: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod];
8294: } /* i */
8295: } /* end bad */
8296: }else{
8297: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8298: agemaxgood[cptcod]=age;
8299: }else{ /* bad we change the value with the values of good ages */
8300: for (i=1; i<=nlstate;i++){
8301: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
8302: } /* i */
8303: } /* end bad */
8304: }/* end else */
8305: sum=0.;sumr=0.;
8306: for (i=1; i<=nlstate;i++){
8307: sum+=mobaverage[(int)age][i][cptcod];
8308: sumr+=probs[(int)age][i][cptcod];
8309: }
8310: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.288 brouard 8311: if(!firstA1){
8312: firstA1=1;
8313: 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);
8314: }
8315: 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 8316: } /* end bad */
8317: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8318: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.288 brouard 8319: if(!firstA2){
8320: firstA2=1;
8321: 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);
8322: }
8323: 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 8324: } /* end bad */
8325: }/* age */
1.266 brouard 8326:
8327: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */
1.222 brouard 8328: sumnewm[cptcod]=0.;
1.266 brouard 8329: sumnewmr[cptcod]=0.;
1.222 brouard 8330: for (i=1; i<=nlstate;i++){
8331: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
1.266 brouard 8332: sumnewmr[cptcod]+=probs[(int)age][i][cptcod];
8333: }
8334: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */
8335: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */
8336: agemingoodr[cptcod]=age;
8337: for (i=1; i<=nlstate;i++)
8338: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
8339: }else{ /* bad we change the value with the values of good ages */
8340: for (i=1; i<=nlstate;i++){
8341: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod];
8342: } /* i */
8343: } /* end bad */
8344: }else{
8345: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
8346: agemingood[cptcod]=age;
8347: }else{ /* bad */
8348: for (i=1; i<=nlstate;i++){
8349: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
8350: } /* i */
8351: } /* end bad */
8352: }/* end else */
8353: sum=0.;sumr=0.;
8354: for (i=1; i<=nlstate;i++){
8355: sum+=mobaverage[(int)age][i][cptcod];
8356: sumr+=mobaverage[(int)age][i][cptcod];
1.222 brouard 8357: }
1.266 brouard 8358: if(fabs(sum - 1.) > 1.e-3) { /* bad */
1.268 brouard 8359: 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 8360: } /* end bad */
8361: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
8362: if(fabs(sumr - 1.) > 1.e-3) { /* bad */
1.268 brouard 8363: 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 8364: } /* end bad */
8365: }/* age */
1.266 brouard 8366:
1.222 brouard 8367:
8368: for (age=bage; age<=fage; age++){
1.235 brouard 8369: /* printf("%d %d ", cptcod, (int)age); */
1.222 brouard 8370: sumnewp[cptcod]=0.;
8371: sumnewm[cptcod]=0.;
8372: for (i=1; i<=nlstate;i++){
8373: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
8374: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
8375: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
8376: }
8377: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
8378: }
8379: /* printf("\n"); */
8380: /* } */
1.266 brouard 8381:
1.222 brouard 8382: /* brutal averaging */
1.266 brouard 8383: /* for (i=1; i<=nlstate;i++){ */
8384: /* for (age=1; age<=bage; age++){ */
8385: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
8386: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8387: /* } */
8388: /* for (age=fage; age<=AGESUP; age++){ */
8389: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */
8390: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */
8391: /* } */
8392: /* } /\* end i status *\/ */
8393: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */
8394: /* for (age=1; age<=AGESUP; age++){ */
8395: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */
8396: /* mobaverage[(int)age][i][cptcod]=0.; */
8397: /* } */
8398: /* } */
1.222 brouard 8399: }/* end cptcod */
1.266 brouard 8400: free_vector(agemaxgoodr,1, ncovcombmax);
8401: free_vector(agemaxgood,1, ncovcombmax);
8402: free_vector(agemingood,1, ncovcombmax);
8403: free_vector(agemingoodr,1, ncovcombmax);
8404: free_vector(sumnewmr,1, ncovcombmax);
1.222 brouard 8405: free_vector(sumnewm,1, ncovcombmax);
8406: free_vector(sumnewp,1, ncovcombmax);
8407: return 0;
8408: }/* End movingaverage */
1.218 brouard 8409:
1.126 brouard 8410:
1.296 brouard 8411:
1.126 brouard 8412: /************** Forecasting ******************/
1.296 brouard 8413: /* 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)*/
8414: 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){
8415: /* dateintemean, mean date of interviews
8416: dateprojd, year, month, day of starting projection
8417: dateprojf date of end of projection;year of end of projection (same day and month as proj1).
1.126 brouard 8418: agemin, agemax range of age
8419: dateprev1 dateprev2 range of dates during which prevalence is computed
8420: */
1.296 brouard 8421: /* double anprojd, mprojd, jprojd; */
8422: /* double anprojf, mprojf, jprojf; */
1.267 brouard 8423: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
1.126 brouard 8424: double agec; /* generic age */
1.296 brouard 8425: double agelim, ppij, yp,yp1,yp2;
1.126 brouard 8426: double *popeffectif,*popcount;
8427: double ***p3mat;
1.218 brouard 8428: /* double ***mobaverage; */
1.126 brouard 8429: char fileresf[FILENAMELENGTH];
8430:
8431: agelim=AGESUP;
1.211 brouard 8432: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8433: in each health status at the date of interview (if between dateprev1 and dateprev2).
8434: We still use firstpass and lastpass as another selection.
8435: */
1.214 brouard 8436: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8437: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 8438:
1.201 brouard 8439: strcpy(fileresf,"F_");
8440: strcat(fileresf,fileresu);
1.126 brouard 8441: if((ficresf=fopen(fileresf,"w"))==NULL) {
8442: printf("Problem with forecast resultfile: %s\n", fileresf);
8443: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
8444: }
1.235 brouard 8445: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
8446: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 8447:
1.225 brouard 8448: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 8449:
8450:
8451: stepsize=(int) (stepm+YEARM-1)/YEARM;
8452: if (stepm<=12) stepsize=1;
8453: if(estepm < stepm){
8454: printf ("Problem %d lower than %d\n",estepm, stepm);
8455: }
1.270 brouard 8456: else{
8457: hstepm=estepm;
8458: }
8459: if(estepm > stepm){ /* Yes every two year */
8460: stepsize=2;
8461: }
1.296 brouard 8462: hstepm=hstepm/stepm;
1.126 brouard 8463:
1.296 brouard 8464:
8465: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8466: /* fractional in yp1 *\/ */
8467: /* aintmean=yp; */
8468: /* yp2=modf((yp1*12),&yp); */
8469: /* mintmean=yp; */
8470: /* yp1=modf((yp2*30.5),&yp); */
8471: /* jintmean=yp; */
8472: /* if(jintmean==0) jintmean=1; */
8473: /* if(mintmean==0) mintmean=1; */
1.126 brouard 8474:
1.296 brouard 8475:
8476: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */
8477: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */
8478: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */
1.227 brouard 8479: i1=pow(2,cptcoveff);
1.126 brouard 8480: if (cptcovn < 1){i1=1;}
8481:
1.296 brouard 8482: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.126 brouard 8483:
8484: fprintf(ficresf,"#****** Routine prevforecast **\n");
1.227 brouard 8485:
1.126 brouard 8486: /* if (h==(int)(YEARM*yearp)){ */
1.235 brouard 8487: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8488: for(k=1; k<=i1;k++){
1.253 brouard 8489: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 8490: continue;
1.227 brouard 8491: if(invalidvarcomb[k]){
8492: printf("\nCombination (%d) projection ignored because no cases \n",k);
8493: continue;
8494: }
8495: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
8496: for(j=1;j<=cptcoveff;j++) {
8497: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8498: }
1.235 brouard 8499: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
1.238 brouard 8500: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
1.235 brouard 8501: }
1.227 brouard 8502: fprintf(ficresf," yearproj age");
8503: for(j=1; j<=nlstate+ndeath;j++){
8504: for(i=1; i<=nlstate;i++)
8505: fprintf(ficresf," p%d%d",i,j);
8506: fprintf(ficresf," wp.%d",j);
8507: }
1.296 brouard 8508: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) {
1.227 brouard 8509: fprintf(ficresf,"\n");
1.296 brouard 8510: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp);
1.270 brouard 8511: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
8512: for (agec=fage; agec>=(bage); agec--){
1.227 brouard 8513: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
8514: nhstepm = nhstepm/hstepm;
8515: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8516: oldm=oldms;savm=savms;
1.268 brouard 8517: /* We compute pii at age agec over nhstepm);*/
1.235 brouard 8518: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres);
1.268 brouard 8519: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
1.227 brouard 8520: for (h=0; h<=nhstepm; h++){
8521: if (h*hstepm/YEARM*stepm ==yearp) {
1.268 brouard 8522: break;
8523: }
8524: }
8525: fprintf(ficresf,"\n");
8526: for(j=1;j<=cptcoveff;j++)
8527: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 8528: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm);
1.268 brouard 8529:
8530: for(j=1; j<=nlstate+ndeath;j++) {
8531: ppij=0.;
8532: for(i=1; i<=nlstate;i++) {
1.278 brouard 8533: if (mobilav>=1)
8534: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k];
8535: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */
8536: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k];
8537: }
1.268 brouard 8538: fprintf(ficresf," %.3f", p3mat[i][j][h]);
8539: } /* end i */
8540: fprintf(ficresf," %.3f", ppij);
8541: }/* end j */
1.227 brouard 8542: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8543: } /* end agec */
1.266 brouard 8544: /* diffyear=(int) anproj1+yearp-ageminpar-1; */
8545: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/
1.227 brouard 8546: } /* end yearp */
8547: } /* end k */
1.219 brouard 8548:
1.126 brouard 8549: fclose(ficresf);
1.215 brouard 8550: printf("End of Computing forecasting \n");
8551: fprintf(ficlog,"End of Computing forecasting\n");
8552:
1.126 brouard 8553: }
8554:
1.269 brouard 8555: /************** Back Forecasting ******************/
1.296 brouard 8556: /* 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){ */
8557: 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){
8558: /* back1, year, month, day of starting backprojection
1.267 brouard 8559: agemin, agemax range of age
8560: dateprev1 dateprev2 range of dates during which prevalence is computed
1.269 brouard 8561: anback2 year of end of backprojection (same day and month as back1).
8562: prevacurrent and prev are prevalences.
1.267 brouard 8563: */
8564: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0;
8565: double agec; /* generic age */
1.302 brouard 8566: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/
1.267 brouard 8567: double *popeffectif,*popcount;
8568: double ***p3mat;
8569: /* double ***mobaverage; */
8570: char fileresfb[FILENAMELENGTH];
8571:
1.268 brouard 8572: agelim=AGEINF;
1.267 brouard 8573: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
8574: in each health status at the date of interview (if between dateprev1 and dateprev2).
8575: We still use firstpass and lastpass as another selection.
8576: */
8577: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
8578: /* firstpass, lastpass, stepm, weightopt, model); */
8579:
8580: /*Do we need to compute prevalence again?*/
8581:
8582: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
8583:
8584: strcpy(fileresfb,"FB_");
8585: strcat(fileresfb,fileresu);
8586: if((ficresfb=fopen(fileresfb,"w"))==NULL) {
8587: printf("Problem with back forecast resultfile: %s\n", fileresfb);
8588: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb);
8589: }
8590: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8591: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb);
8592:
8593: if (cptcoveff==0) ncodemax[cptcoveff]=1;
8594:
8595:
8596: stepsize=(int) (stepm+YEARM-1)/YEARM;
8597: if (stepm<=12) stepsize=1;
8598: if(estepm < stepm){
8599: printf ("Problem %d lower than %d\n",estepm, stepm);
8600: }
1.270 brouard 8601: else{
8602: hstepm=estepm;
8603: }
8604: if(estepm >= stepm){ /* Yes every two year */
8605: stepsize=2;
8606: }
1.267 brouard 8607:
8608: hstepm=hstepm/stepm;
1.296 brouard 8609: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
8610: /* fractional in yp1 *\/ */
8611: /* aintmean=yp; */
8612: /* yp2=modf((yp1*12),&yp); */
8613: /* mintmean=yp; */
8614: /* yp1=modf((yp2*30.5),&yp); */
8615: /* jintmean=yp; */
8616: /* if(jintmean==0) jintmean=1; */
8617: /* if(mintmean==0) jintmean=1; */
1.267 brouard 8618:
8619: i1=pow(2,cptcoveff);
8620: if (cptcovn < 1){i1=1;}
8621:
1.296 brouard 8622: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
8623: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2);
1.267 brouard 8624:
8625: fprintf(ficresfb,"#****** Routine prevbackforecast **\n");
8626:
8627: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8628: for(k=1; k<=i1;k++){
8629: if(i1 != 1 && TKresult[nres]!= k)
8630: continue;
8631: if(invalidvarcomb[k]){
8632: printf("\nCombination (%d) projection ignored because no cases \n",k);
8633: continue;
8634: }
1.268 brouard 8635: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#");
1.267 brouard 8636: for(j=1;j<=cptcoveff;j++) {
8637: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8638: }
8639: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
8640: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
8641: }
8642: fprintf(ficresfb," yearbproj age");
8643: for(j=1; j<=nlstate+ndeath;j++){
8644: for(i=1; i<=nlstate;i++)
1.268 brouard 8645: fprintf(ficresfb," b%d%d",i,j);
8646: fprintf(ficresfb," b.%d",j);
1.267 brouard 8647: }
1.296 brouard 8648: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) {
1.267 brouard 8649: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */
8650: fprintf(ficresfb,"\n");
1.296 brouard 8651: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp);
1.273 brouard 8652: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
1.270 brouard 8653: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */
8654: for (agec=bage; agec<=fage; agec++){ /* testing */
1.268 brouard 8655: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/
1.271 brouard 8656: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/
1.267 brouard 8657: nhstepm = nhstepm/hstepm;
8658: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8659: oldm=oldms;savm=savms;
1.268 brouard 8660: /* computes hbxij at age agec over 1 to nhstepm */
1.271 brouard 8661: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */
1.267 brouard 8662: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.268 brouard 8663: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */
8664: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */
8665: /* printf(" agec=%.2f\n",agec);fflush(stdout); */
1.267 brouard 8666: for (h=0; h<=nhstepm; h++){
1.268 brouard 8667: if (h*hstepm/YEARM*stepm ==-yearp) {
8668: break;
8669: }
8670: }
8671: fprintf(ficresfb,"\n");
8672: for(j=1;j<=cptcoveff;j++)
8673: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.296 brouard 8674: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm);
1.268 brouard 8675: for(i=1; i<=nlstate+ndeath;i++) {
8676: ppij=0.;ppi=0.;
8677: for(j=1; j<=nlstate;j++) {
8678: /* if (mobilav==1) */
1.269 brouard 8679: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k];
8680: ppi=ppi+prevacurrent[(int)agec][j][k];
8681: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */
8682: /* ppi=ppi+mobaverage[(int)agec][j][k]; */
1.267 brouard 8683: /* else { */
8684: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */
8685: /* } */
1.268 brouard 8686: fprintf(ficresfb," %.3f", p3mat[i][j][h]);
8687: } /* end j */
8688: if(ppi <0.99){
8689: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
8690: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi);
8691: }
8692: fprintf(ficresfb," %.3f", ppij);
8693: }/* end j */
1.267 brouard 8694: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8695: } /* end agec */
8696: } /* end yearp */
8697: } /* end k */
1.217 brouard 8698:
1.267 brouard 8699: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
1.217 brouard 8700:
1.267 brouard 8701: fclose(ficresfb);
8702: printf("End of Computing Back forecasting \n");
8703: fprintf(ficlog,"End of Computing Back forecasting\n");
1.218 brouard 8704:
1.267 brouard 8705: }
1.217 brouard 8706:
1.269 brouard 8707: /* Variance of prevalence limit: varprlim */
8708: 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 8709: /*------- Variance of forward period (stable) prevalence------*/
1.269 brouard 8710:
8711: char fileresvpl[FILENAMELENGTH];
8712: FILE *ficresvpl;
8713: double **oldm, **savm;
8714: double **varpl; /* Variances of prevalence limits by age */
8715: int i1, k, nres, j ;
8716:
8717: strcpy(fileresvpl,"VPL_");
8718: strcat(fileresvpl,fileresu);
8719: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
1.288 brouard 8720: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl);
1.269 brouard 8721: exit(0);
8722: }
1.288 brouard 8723: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
8724: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.269 brouard 8725:
8726: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
8727: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
8728:
8729: i1=pow(2,cptcoveff);
8730: if (cptcovn < 1){i1=1;}
8731:
8732: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8733: for(k=1; k<=i1;k++){
8734: if(i1 != 1 && TKresult[nres]!= k)
8735: continue;
8736: fprintf(ficresvpl,"\n#****** ");
8737: printf("\n#****** ");
8738: fprintf(ficlog,"\n#****** ");
8739: for(j=1;j<=cptcoveff;j++) {
8740: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8741: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8742: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8743: }
8744: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
8745: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8746: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8747: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8748: }
8749: fprintf(ficresvpl,"******\n");
8750: printf("******\n");
8751: fprintf(ficlog,"******\n");
8752:
8753: varpl=matrix(1,nlstate,(int) bage, (int) fage);
8754: oldm=oldms;savm=savms;
8755: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres);
8756: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
8757: /*}*/
8758: }
8759:
8760: fclose(ficresvpl);
1.288 brouard 8761: printf("done variance-covariance of forward period prevalence\n");fflush(stdout);
8762: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog);
1.269 brouard 8763:
8764: }
8765: /* Variance of back prevalence: varbprlim */
8766: 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){
8767: /*------- Variance of back (stable) prevalence------*/
8768:
8769: char fileresvbl[FILENAMELENGTH];
8770: FILE *ficresvbl;
8771:
8772: double **oldm, **savm;
8773: double **varbpl; /* Variances of back prevalence limits by age */
8774: int i1, k, nres, j ;
8775:
8776: strcpy(fileresvbl,"VBL_");
8777: strcat(fileresvbl,fileresu);
8778: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) {
8779: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl);
8780: exit(0);
8781: }
8782: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout);
8783: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog);
8784:
8785:
8786: i1=pow(2,cptcoveff);
8787: if (cptcovn < 1){i1=1;}
8788:
8789: for(nres=1; nres <= nresult; nres++) /* For each resultline */
8790: for(k=1; k<=i1;k++){
8791: if(i1 != 1 && TKresult[nres]!= k)
8792: continue;
8793: fprintf(ficresvbl,"\n#****** ");
8794: printf("\n#****** ");
8795: fprintf(ficlog,"\n#****** ");
8796: for(j=1;j<=cptcoveff;j++) {
8797: fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8798: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8799: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8800: }
8801: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
8802: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8803: fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8804: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
8805: }
8806: fprintf(ficresvbl,"******\n");
8807: printf("******\n");
8808: fprintf(ficlog,"******\n");
8809:
8810: varbpl=matrix(1,nlstate,(int) bage, (int) fage);
8811: oldm=oldms;savm=savms;
8812:
8813: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres);
8814: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage);
8815: /*}*/
8816: }
8817:
8818: fclose(ficresvbl);
8819: printf("done variance-covariance of back prevalence\n");fflush(stdout);
8820: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog);
8821:
8822: } /* End of varbprlim */
8823:
1.126 brouard 8824: /************** Forecasting *****not tested NB*************/
1.227 brouard 8825: /* 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 8826:
1.227 brouard 8827: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */
8828: /* int *popage; */
8829: /* double calagedatem, agelim, kk1, kk2; */
8830: /* double *popeffectif,*popcount; */
8831: /* double ***p3mat,***tabpop,***tabpopprev; */
8832: /* /\* double ***mobaverage; *\/ */
8833: /* char filerespop[FILENAMELENGTH]; */
1.126 brouard 8834:
1.227 brouard 8835: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8836: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8837: /* agelim=AGESUP; */
8838: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */
1.126 brouard 8839:
1.227 brouard 8840: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.126 brouard 8841:
8842:
1.227 brouard 8843: /* strcpy(filerespop,"POP_"); */
8844: /* strcat(filerespop,fileresu); */
8845: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */
8846: /* printf("Problem with forecast resultfile: %s\n", filerespop); */
8847: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */
8848: /* } */
8849: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */
8850: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */
1.126 brouard 8851:
1.227 brouard 8852: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.126 brouard 8853:
1.227 brouard 8854: /* /\* if (mobilav!=0) { *\/ */
8855: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
8856: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
8857: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
8858: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
8859: /* /\* } *\/ */
8860: /* /\* } *\/ */
1.126 brouard 8861:
1.227 brouard 8862: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
8863: /* if (stepm<=12) stepsize=1; */
1.126 brouard 8864:
1.227 brouard 8865: /* agelim=AGESUP; */
1.126 brouard 8866:
1.227 brouard 8867: /* hstepm=1; */
8868: /* hstepm=hstepm/stepm; */
1.218 brouard 8869:
1.227 brouard 8870: /* if (popforecast==1) { */
8871: /* if((ficpop=fopen(popfile,"r"))==NULL) { */
8872: /* printf("Problem with population file : %s\n",popfile);exit(0); */
8873: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */
8874: /* } */
8875: /* popage=ivector(0,AGESUP); */
8876: /* popeffectif=vector(0,AGESUP); */
8877: /* popcount=vector(0,AGESUP); */
1.126 brouard 8878:
1.227 brouard 8879: /* i=1; */
8880: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */
1.218 brouard 8881:
1.227 brouard 8882: /* imx=i; */
8883: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */
8884: /* } */
1.218 brouard 8885:
1.227 brouard 8886: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */
8887: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
8888: /* k=k+1; */
8889: /* fprintf(ficrespop,"\n#******"); */
8890: /* for(j=1;j<=cptcoveff;j++) { */
8891: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
8892: /* } */
8893: /* fprintf(ficrespop,"******\n"); */
8894: /* fprintf(ficrespop,"# Age"); */
8895: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */
8896: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */
1.126 brouard 8897:
1.227 brouard 8898: /* for (cpt=0; cpt<=0;cpt++) { */
8899: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
1.126 brouard 8900:
1.227 brouard 8901: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
8902: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
8903: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 8904:
1.227 brouard 8905: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8906: /* oldm=oldms;savm=savms; */
8907: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.218 brouard 8908:
1.227 brouard 8909: /* for (h=0; h<=nhstepm; h++){ */
8910: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
8911: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
8912: /* } */
8913: /* for(j=1; j<=nlstate+ndeath;j++) { */
8914: /* kk1=0.;kk2=0; */
8915: /* for(i=1; i<=nlstate;i++) { */
8916: /* if (mobilav==1) */
8917: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */
8918: /* else { */
8919: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */
8920: /* } */
8921: /* } */
8922: /* if (h==(int)(calagedatem+12*cpt)){ */
8923: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */
8924: /* /\*fprintf(ficrespop," %.3f", kk1); */
8925: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */
8926: /* } */
8927: /* } */
8928: /* for(i=1; i<=nlstate;i++){ */
8929: /* kk1=0.; */
8930: /* for(j=1; j<=nlstate;j++){ */
8931: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */
8932: /* } */
8933: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */
8934: /* } */
1.218 brouard 8935:
1.227 brouard 8936: /* if (h==(int)(calagedatem+12*cpt)) */
8937: /* for(j=1; j<=nlstate;j++) */
8938: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */
8939: /* } */
8940: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8941: /* } */
8942: /* } */
1.218 brouard 8943:
1.227 brouard 8944: /* /\******\/ */
1.218 brouard 8945:
1.227 brouard 8946: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */
8947: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */
8948: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */
8949: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */
8950: /* nhstepm = nhstepm/hstepm; */
1.126 brouard 8951:
1.227 brouard 8952: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8953: /* oldm=oldms;savm=savms; */
8954: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
8955: /* for (h=0; h<=nhstepm; h++){ */
8956: /* if (h==(int) (calagedatem+YEARM*cpt)) { */
8957: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */
8958: /* } */
8959: /* for(j=1; j<=nlstate+ndeath;j++) { */
8960: /* kk1=0.;kk2=0; */
8961: /* for(i=1; i<=nlstate;i++) { */
8962: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */
8963: /* } */
8964: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */
8965: /* } */
8966: /* } */
8967: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
8968: /* } */
8969: /* } */
8970: /* } */
8971: /* } */
1.218 brouard 8972:
1.227 brouard 8973: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
1.218 brouard 8974:
1.227 brouard 8975: /* if (popforecast==1) { */
8976: /* free_ivector(popage,0,AGESUP); */
8977: /* free_vector(popeffectif,0,AGESUP); */
8978: /* free_vector(popcount,0,AGESUP); */
8979: /* } */
8980: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8981: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
8982: /* fclose(ficrespop); */
8983: /* } /\* End of popforecast *\/ */
1.218 brouard 8984:
1.126 brouard 8985: int fileappend(FILE *fichier, char *optionfich)
8986: {
8987: if((fichier=fopen(optionfich,"a"))==NULL) {
8988: printf("Problem with file: %s\n", optionfich);
8989: fprintf(ficlog,"Problem with file: %s\n", optionfich);
8990: return (0);
8991: }
8992: fflush(fichier);
8993: return (1);
8994: }
8995:
8996:
8997: /**************** function prwizard **********************/
8998: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
8999: {
9000:
9001: /* Wizard to print covariance matrix template */
9002:
1.164 brouard 9003: char ca[32], cb[32];
9004: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 9005: int numlinepar;
9006:
9007: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9008: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9009: for(i=1; i <=nlstate; i++){
9010: jj=0;
9011: for(j=1; j <=nlstate+ndeath; j++){
9012: if(j==i) continue;
9013: jj++;
9014: /*ca[0]= k+'a'-1;ca[1]='\0';*/
9015: printf("%1d%1d",i,j);
9016: fprintf(ficparo,"%1d%1d",i,j);
9017: for(k=1; k<=ncovmodel;k++){
9018: /* printf(" %lf",param[i][j][k]); */
9019: /* fprintf(ficparo," %lf",param[i][j][k]); */
9020: printf(" 0.");
9021: fprintf(ficparo," 0.");
9022: }
9023: printf("\n");
9024: fprintf(ficparo,"\n");
9025: }
9026: }
9027: printf("# Scales (for hessian or gradient estimation)\n");
9028: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
9029: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
9030: for(i=1; i <=nlstate; i++){
9031: jj=0;
9032: for(j=1; j <=nlstate+ndeath; j++){
9033: if(j==i) continue;
9034: jj++;
9035: fprintf(ficparo,"%1d%1d",i,j);
9036: printf("%1d%1d",i,j);
9037: fflush(stdout);
9038: for(k=1; k<=ncovmodel;k++){
9039: /* printf(" %le",delti3[i][j][k]); */
9040: /* fprintf(ficparo," %le",delti3[i][j][k]); */
9041: printf(" 0.");
9042: fprintf(ficparo," 0.");
9043: }
9044: numlinepar++;
9045: printf("\n");
9046: fprintf(ficparo,"\n");
9047: }
9048: }
9049: printf("# Covariance matrix\n");
9050: /* # 121 Var(a12)\n\ */
9051: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9052: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9053: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9054: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9055: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9056: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9057: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9058: fflush(stdout);
9059: fprintf(ficparo,"# Covariance matrix\n");
9060: /* # 121 Var(a12)\n\ */
9061: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9062: /* # ...\n\ */
9063: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9064:
9065: for(itimes=1;itimes<=2;itimes++){
9066: jj=0;
9067: for(i=1; i <=nlstate; i++){
9068: for(j=1; j <=nlstate+ndeath; j++){
9069: if(j==i) continue;
9070: for(k=1; k<=ncovmodel;k++){
9071: jj++;
9072: ca[0]= k+'a'-1;ca[1]='\0';
9073: if(itimes==1){
9074: printf("#%1d%1d%d",i,j,k);
9075: fprintf(ficparo,"#%1d%1d%d",i,j,k);
9076: }else{
9077: printf("%1d%1d%d",i,j,k);
9078: fprintf(ficparo,"%1d%1d%d",i,j,k);
9079: /* printf(" %.5le",matcov[i][j]); */
9080: }
9081: ll=0;
9082: for(li=1;li <=nlstate; li++){
9083: for(lj=1;lj <=nlstate+ndeath; lj++){
9084: if(lj==li) continue;
9085: for(lk=1;lk<=ncovmodel;lk++){
9086: ll++;
9087: if(ll<=jj){
9088: cb[0]= lk +'a'-1;cb[1]='\0';
9089: if(ll<jj){
9090: if(itimes==1){
9091: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9092: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9093: }else{
9094: printf(" 0.");
9095: fprintf(ficparo," 0.");
9096: }
9097: }else{
9098: if(itimes==1){
9099: printf(" Var(%s%1d%1d)",ca,i,j);
9100: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
9101: }else{
9102: printf(" 0.");
9103: fprintf(ficparo," 0.");
9104: }
9105: }
9106: }
9107: } /* end lk */
9108: } /* end lj */
9109: } /* end li */
9110: printf("\n");
9111: fprintf(ficparo,"\n");
9112: numlinepar++;
9113: } /* end k*/
9114: } /*end j */
9115: } /* end i */
9116: } /* end itimes */
9117:
9118: } /* end of prwizard */
9119: /******************* Gompertz Likelihood ******************************/
9120: double gompertz(double x[])
9121: {
1.302 brouard 9122: double A=0.0,B=0.,L=0.0,sump=0.,num=0.;
1.126 brouard 9123: int i,n=0; /* n is the size of the sample */
9124:
1.220 brouard 9125: for (i=1;i<=imx ; i++) {
1.126 brouard 9126: sump=sump+weight[i];
9127: /* sump=sump+1;*/
9128: num=num+1;
9129: }
1.302 brouard 9130: L=0.0;
9131: /* agegomp=AGEGOMP; */
1.126 brouard 9132: /* for (i=0; i<=imx; i++)
9133: 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]);*/
9134:
1.302 brouard 9135: for (i=1;i<=imx ; i++) {
9136: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp))
9137: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year.
9138: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month)
9139: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM)
9140: * +
9141: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1
9142: */
9143: if (wav[i] > 1 || agedc[i] < AGESUP) {
9144: if (cens[i] == 1){
9145: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
9146: } else if (cens[i] == 0){
1.126 brouard 9147: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
1.302 brouard 9148: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM);
9149: } else
9150: printf("Gompertz cens[%d] neither 1 nor 0\n",i);
1.126 brouard 9151: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
1.302 brouard 9152: L=L+A*weight[i];
1.126 brouard 9153: /* 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 9154: }
9155: }
1.126 brouard 9156:
1.302 brouard 9157: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
1.126 brouard 9158:
9159: return -2*L*num/sump;
9160: }
9161:
1.136 brouard 9162: #ifdef GSL
9163: /******************* Gompertz_f Likelihood ******************************/
9164: double gompertz_f(const gsl_vector *v, void *params)
9165: {
1.302 brouard 9166: double A=0.,B=0.,LL=0.0,sump=0.,num=0.;
1.136 brouard 9167: double *x= (double *) v->data;
9168: int i,n=0; /* n is the size of the sample */
9169:
9170: for (i=0;i<=imx-1 ; i++) {
9171: sump=sump+weight[i];
9172: /* sump=sump+1;*/
9173: num=num+1;
9174: }
9175:
9176:
9177: /* for (i=0; i<=imx; i++)
9178: 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]);*/
9179: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
9180: for (i=1;i<=imx ; i++)
9181: {
9182: if (cens[i] == 1 && wav[i]>1)
9183: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
9184:
9185: if (cens[i] == 0 && wav[i]>1)
9186: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
9187: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
9188:
9189: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
9190: if (wav[i] > 1 ) { /* ??? */
9191: LL=LL+A*weight[i];
9192: /* 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]);*/
9193: }
9194: }
9195:
9196: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
9197: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
9198:
9199: return -2*LL*num/sump;
9200: }
9201: #endif
9202:
1.126 brouard 9203: /******************* Printing html file ***********/
1.201 brouard 9204: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 9205: int lastpass, int stepm, int weightopt, char model[],\
9206: int imx, double p[],double **matcov,double agemortsup){
9207: int i,k;
9208:
9209: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
9210: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
9211: for (i=1;i<=2;i++)
9212: 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 9213: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 9214: fprintf(fichtm,"</ul>");
9215:
9216: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
9217:
9218: 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>");
9219:
9220: for (k=agegomp;k<(agemortsup-2);k++)
9221: 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]);
9222:
9223:
9224: fflush(fichtm);
9225: }
9226:
9227: /******************* Gnuplot file **************/
1.201 brouard 9228: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 9229:
9230: char dirfileres[132],optfileres[132];
1.164 brouard 9231:
1.126 brouard 9232: int ng;
9233:
9234:
9235: /*#ifdef windows */
9236: fprintf(ficgp,"cd \"%s\" \n",pathc);
9237: /*#endif */
9238:
9239:
9240: strcpy(dirfileres,optionfilefiname);
9241: strcpy(optfileres,"vpl");
1.199 brouard 9242: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 9243: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 9244: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 9245: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 9246: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
9247:
9248: }
9249:
1.136 brouard 9250: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
9251: {
1.126 brouard 9252:
1.136 brouard 9253: /*-------- data file ----------*/
9254: FILE *fic;
9255: char dummy[]=" ";
1.240 brouard 9256: int i=0, j=0, n=0, iv=0, v;
1.223 brouard 9257: int lstra;
1.136 brouard 9258: int linei, month, year,iout;
1.302 brouard 9259: int noffset=0; /* This is the offset if BOM data file */
1.136 brouard 9260: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 9261: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 9262: char *stratrunc;
1.223 brouard 9263:
1.240 brouard 9264: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */
9265: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */
1.126 brouard 9266:
1.240 brouard 9267: for(v=1; v <=ncovcol;v++){
9268: DummyV[v]=0;
9269: FixedV[v]=0;
9270: }
9271: for(v=ncovcol+1; v <=ncovcol+nqv;v++){
9272: DummyV[v]=1;
9273: FixedV[v]=0;
9274: }
9275: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){
9276: DummyV[v]=0;
9277: FixedV[v]=1;
9278: }
9279: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){
9280: DummyV[v]=1;
9281: FixedV[v]=1;
9282: }
9283: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){
9284: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]);
9285: 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]);
9286: }
1.126 brouard 9287:
1.136 brouard 9288: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 9289: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9290: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 9291: }
1.126 brouard 9292:
1.302 brouard 9293: /* Is it a BOM UTF-8 Windows file? */
9294: /* First data line */
9295: linei=0;
9296: while(fgets(line, MAXLINE, fic)) {
9297: noffset=0;
9298: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
9299: {
9300: noffset=noffset+3;
9301: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout);
9302: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);
9303: fflush(ficlog); return 1;
9304: }
9305: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
9306: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
9307: {
9308: noffset=noffset+2;
1.304 brouard 9309: 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);
9310: 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 9311: fflush(ficlog); return 1;
9312: }
9313: else if( line[0] == 0 && line[1] == 0)
9314: {
9315: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
9316: noffset=noffset+4;
1.304 brouard 9317: 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);
9318: 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 9319: fflush(ficlog); return 1;
9320: }
9321: } else{
9322: ;/*printf(" Not a BOM file\n");*/
9323: }
9324: /* If line starts with a # it is a comment */
9325: if (line[noffset] == '#') {
9326: linei=linei+1;
9327: break;
9328: }else{
9329: break;
9330: }
9331: }
9332: fclose(fic);
9333: if((fic=fopen(datafile,"r"))==NULL) {
9334: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
9335: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
9336: }
9337: /* Not a Bom file */
9338:
1.136 brouard 9339: i=1;
9340: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
9341: linei=linei+1;
9342: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
9343: if(line[j] == '\t')
9344: line[j] = ' ';
9345: }
9346: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
9347: ;
9348: };
9349: line[j+1]=0; /* Trims blanks at end of line */
9350: if(line[0]=='#'){
9351: fprintf(ficlog,"Comment line\n%s\n",line);
9352: printf("Comment line\n%s\n",line);
9353: continue;
9354: }
9355: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 9356: strcpy(line, linetmp);
1.223 brouard 9357:
9358: /* Loops on waves */
9359: for (j=maxwav;j>=1;j--){
9360: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.238 brouard 9361: cutv(stra, strb, line, ' ');
9362: if(strb[0]=='.') { /* Missing value */
9363: lval=-1;
9364: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
9365: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */
9366: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
9367: 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);
9368: 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);
9369: return 1;
9370: }
9371: }else{
9372: errno=0;
9373: /* what_kind_of_number(strb); */
9374: dval=strtod(strb,&endptr);
9375: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
9376: /* if(strb != endptr && *endptr == '\0') */
9377: /* dval=dlval; */
9378: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9379: if( strb[0]=='\0' || (*endptr != '\0')){
9380: 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);
9381: 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);
9382: return 1;
9383: }
9384: cotqvar[j][iv][i]=dval;
9385: cotvar[j][ntv+iv][i]=dval;
9386: }
9387: strcpy(line,stra);
1.223 brouard 9388: }/* end loop ntqv */
1.225 brouard 9389:
1.223 brouard 9390: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.238 brouard 9391: cutv(stra, strb, line, ' ');
9392: if(strb[0]=='.') { /* Missing value */
9393: lval=-1;
9394: }else{
9395: errno=0;
9396: lval=strtol(strb,&endptr,10);
9397: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9398: if( strb[0]=='\0' || (*endptr != '\0')){
9399: 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);
9400: 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);
9401: return 1;
9402: }
9403: }
9404: if(lval <-1 || lval >1){
9405: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 9406: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9407: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9408: For example, for multinomial values like 1, 2 and 3,\n \
9409: build V1=0 V2=0 for the reference value (1),\n \
9410: V1=1 V2=0 for (2) \n \
1.223 brouard 9411: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9412: output of IMaCh is often meaningless.\n \
1.223 brouard 9413: Exiting.\n",lval,linei, i,line,j);
1.238 brouard 9414: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 9415: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9416: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.238 brouard 9417: For example, for multinomial values like 1, 2 and 3,\n \
9418: build V1=0 V2=0 for the reference value (1),\n \
9419: V1=1 V2=0 for (2) \n \
1.223 brouard 9420: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.238 brouard 9421: output of IMaCh is often meaningless.\n \
1.223 brouard 9422: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.238 brouard 9423: return 1;
9424: }
9425: cotvar[j][iv][i]=(double)(lval);
9426: strcpy(line,stra);
1.223 brouard 9427: }/* end loop ntv */
1.225 brouard 9428:
1.223 brouard 9429: /* Statuses at wave */
1.137 brouard 9430: cutv(stra, strb, line, ' ');
1.223 brouard 9431: if(strb[0]=='.') { /* Missing value */
1.238 brouard 9432: lval=-1;
1.136 brouard 9433: }else{
1.238 brouard 9434: errno=0;
9435: lval=strtol(strb,&endptr,10);
9436: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
9437: if( strb[0]=='\0' || (*endptr != '\0')){
9438: 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);
9439: 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);
9440: return 1;
9441: }
1.136 brouard 9442: }
1.225 brouard 9443:
1.136 brouard 9444: s[j][i]=lval;
1.225 brouard 9445:
1.223 brouard 9446: /* Date of Interview */
1.136 brouard 9447: strcpy(line,stra);
9448: cutv(stra, strb,line,' ');
1.169 brouard 9449: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9450: }
1.169 brouard 9451: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 9452: month=99;
9453: year=9999;
1.136 brouard 9454: }else{
1.225 brouard 9455: 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);
9456: 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);
9457: return 1;
1.136 brouard 9458: }
9459: anint[j][i]= (double) year;
1.302 brouard 9460: mint[j][i]= (double)month;
9461: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */
9462: /* 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]); */
9463: /* 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]); */
9464: /* } */
1.136 brouard 9465: strcpy(line,stra);
1.223 brouard 9466: } /* End loop on waves */
1.225 brouard 9467:
1.223 brouard 9468: /* Date of death */
1.136 brouard 9469: cutv(stra, strb,line,' ');
1.169 brouard 9470: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9471: }
1.169 brouard 9472: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 9473: month=99;
9474: year=9999;
9475: }else{
1.141 brouard 9476: 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 9477: 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);
9478: return 1;
1.136 brouard 9479: }
9480: andc[i]=(double) year;
9481: moisdc[i]=(double) month;
9482: strcpy(line,stra);
9483:
1.223 brouard 9484: /* Date of birth */
1.136 brouard 9485: cutv(stra, strb,line,' ');
1.169 brouard 9486: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 9487: }
1.169 brouard 9488: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 9489: month=99;
9490: year=9999;
9491: }else{
1.141 brouard 9492: 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);
9493: 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 9494: return 1;
1.136 brouard 9495: }
9496: if (year==9999) {
1.141 brouard 9497: 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);
9498: 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 9499: return 1;
9500:
1.136 brouard 9501: }
9502: annais[i]=(double)(year);
1.302 brouard 9503: moisnais[i]=(double)(month);
9504: for (j=1;j<=maxwav;j++){
9505: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){
9506: 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]);
9507: 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]);
9508: }
9509: }
9510:
1.136 brouard 9511: strcpy(line,stra);
1.225 brouard 9512:
1.223 brouard 9513: /* Sample weight */
1.136 brouard 9514: cutv(stra, strb,line,' ');
9515: errno=0;
9516: dval=strtod(strb,&endptr);
9517: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 9518: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
9519: 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 9520: fflush(ficlog);
9521: return 1;
9522: }
9523: weight[i]=dval;
9524: strcpy(line,stra);
1.225 brouard 9525:
1.223 brouard 9526: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
9527: cutv(stra, strb, line, ' ');
9528: if(strb[0]=='.') { /* Missing value */
1.225 brouard 9529: lval=-1;
1.311 ! brouard 9530: coqvar[iv][i]=NAN;
! 9531: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */
1.223 brouard 9532: }else{
1.225 brouard 9533: errno=0;
9534: /* what_kind_of_number(strb); */
9535: dval=strtod(strb,&endptr);
9536: /* if(strb != endptr && *endptr == '\0') */
9537: /* dval=dlval; */
9538: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
9539: if( strb[0]=='\0' || (*endptr != '\0')){
9540: 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);
9541: 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);
9542: return 1;
9543: }
9544: coqvar[iv][i]=dval;
1.226 brouard 9545: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 9546: }
9547: strcpy(line,stra);
9548: }/* end loop nqv */
1.136 brouard 9549:
1.223 brouard 9550: /* Covariate values */
1.136 brouard 9551: for (j=ncovcol;j>=1;j--){
9552: cutv(stra, strb,line,' ');
1.223 brouard 9553: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 9554: lval=-1;
1.136 brouard 9555: }else{
1.225 brouard 9556: errno=0;
9557: lval=strtol(strb,&endptr,10);
9558: if( strb[0]=='\0' || (*endptr != '\0')){
9559: 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);
9560: 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);
9561: return 1;
9562: }
1.136 brouard 9563: }
9564: if(lval <-1 || lval >1){
1.225 brouard 9565: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9566: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9567: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9568: For example, for multinomial values like 1, 2 and 3,\n \
9569: build V1=0 V2=0 for the reference value (1),\n \
9570: V1=1 V2=0 for (2) \n \
1.136 brouard 9571: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9572: output of IMaCh is often meaningless.\n \
1.136 brouard 9573: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 9574: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 9575: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
9576: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 9577: For example, for multinomial values like 1, 2 and 3,\n \
9578: build V1=0 V2=0 for the reference value (1),\n \
9579: V1=1 V2=0 for (2) \n \
1.136 brouard 9580: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 9581: output of IMaCh is often meaningless.\n \
1.136 brouard 9582: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 9583: return 1;
1.136 brouard 9584: }
9585: covar[j][i]=(double)(lval);
9586: strcpy(line,stra);
9587: }
9588: lstra=strlen(stra);
1.225 brouard 9589:
1.136 brouard 9590: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
9591: stratrunc = &(stra[lstra-9]);
9592: num[i]=atol(stratrunc);
9593: }
9594: else
9595: num[i]=atol(stra);
9596: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
9597: 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;}*/
9598:
9599: i=i+1;
9600: } /* End loop reading data */
1.225 brouard 9601:
1.136 brouard 9602: *imax=i-1; /* Number of individuals */
9603: fclose(fic);
1.225 brouard 9604:
1.136 brouard 9605: return (0);
1.164 brouard 9606: /* endread: */
1.225 brouard 9607: printf("Exiting readdata: ");
9608: fclose(fic);
9609: return (1);
1.223 brouard 9610: }
1.126 brouard 9611:
1.234 brouard 9612: void removefirstspace(char **stri){/*, char stro[]) {*/
1.230 brouard 9613: char *p1 = *stri, *p2 = *stri;
1.235 brouard 9614: while (*p2 == ' ')
1.234 brouard 9615: p2++;
9616: /* while ((*p1++ = *p2++) !=0) */
9617: /* ; */
9618: /* do */
9619: /* while (*p2 == ' ') */
9620: /* p2++; */
9621: /* while (*p1++ == *p2++); */
9622: *stri=p2;
1.145 brouard 9623: }
9624:
1.235 brouard 9625: int decoderesult ( char resultline[], int nres)
1.230 brouard 9626: /**< This routine decode one result line and returns the combination # of dummy covariates only **/
9627: {
1.235 brouard 9628: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0;
1.230 brouard 9629: char resultsav[MAXLINE];
1.234 brouard 9630: int resultmodel[MAXLINE];
9631: int modelresult[MAXLINE];
1.230 brouard 9632: char stra[80], strb[80], strc[80], strd[80],stre[80];
9633:
1.234 brouard 9634: removefirstspace(&resultline);
1.230 brouard 9635:
9636: if (strstr(resultline,"v") !=0){
9637: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline);
9638: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog);
9639: return 1;
9640: }
9641: trimbb(resultsav, resultline);
9642: if (strlen(resultsav) >1){
9643: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */
9644: }
1.253 brouard 9645: if(j == 0){ /* Resultline but no = */
9646: TKresult[nres]=0; /* Combination for the nresult and the model */
9647: return (0);
9648: }
1.234 brouard 9649: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */
1.310 brouard 9650: printf("ERROR: the number of variables in the resultline, %d, differs from the number of variables used in the model line, %d.\n",j, cptcovs);
9651: fprintf(ficlog,"ERROR: the number of variables in the resultline, %d, differs from the number of variables used in the model line, %d.\n",j, cptcovs);
1.234 brouard 9652: }
9653: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */
9654: if(nbocc(resultsav,'=') >1){
9655: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' '
1.310 brouard 9656: resultsav= V4=1 V5=25.1 V3=0 stra= V5=25.1 V3=0 strb= V4=1 */
1.234 brouard 9657: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */
9658: }else
9659: cutl(strc,strd,resultsav,'=');
1.230 brouard 9660: Tvalsel[k]=atof(strc); /* 1 */
1.234 brouard 9661:
1.230 brouard 9662: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */;
9663: Tvarsel[k]=atoi(strc);
9664: /* Typevarsel[k]=1; /\* 1 for age product *\/ */
9665: /* cptcovsel++; */
9666: if (nbocc(stra,'=') >0)
9667: strcpy(resultsav,stra); /* and analyzes it */
9668: }
1.235 brouard 9669: /* Checking for missing or useless values in comparison of current model needs */
1.236 brouard 9670: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9671: if(Typevar[k1]==0){ /* Single covariate in model */
1.234 brouard 9672: match=0;
1.236 brouard 9673: for(k2=1; k2 <=j;k2++){/* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.237 brouard 9674: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar[1]=5 == Tvarsel[2]=5 */
1.236 brouard 9675: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */
1.234 brouard 9676: match=1;
9677: break;
9678: }
9679: }
9680: if(match == 0){
1.310 brouard 9681: printf("Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model);
9682: fprintf(ficlog,"Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model);
9683: return 1;
1.234 brouard 9684: }
9685: }
9686: }
1.235 brouard 9687: /* Checking for missing or useless values in comparison of current model needs */
9688: for(k2=1; k2 <=j;k2++){ /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
1.234 brouard 9689: match=0;
1.235 brouard 9690: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9691: if(Typevar[k1]==0){ /* Single */
1.237 brouard 9692: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */
1.235 brouard 9693: resultmodel[k1]=k2; /* resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */
1.234 brouard 9694: ++match;
9695: }
9696: }
9697: }
9698: if(match == 0){
9699: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
1.310 brouard 9700: fprintf(ficlog,"Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model);
9701: return 1;
1.234 brouard 9702: }else if(match > 1){
9703: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
1.310 brouard 9704: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model);
9705: return 1;
1.234 brouard 9706: }
9707: }
1.235 brouard 9708:
1.234 brouard 9709: /* We need to deduce which combination number is chosen and save quantitative values */
1.235 brouard 9710: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9711: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */
9712: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/
9713: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */
9714: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/
9715: /* 1 0 0 0 */
9716: /* 2 1 0 0 */
9717: /* 3 0 1 0 */
9718: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */
9719: /* 5 0 0 1 */
9720: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */
9721: /* 7 0 1 1 */
9722: /* 8 1 1 1 */
1.237 brouard 9723: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */
9724: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */
9725: /* V5*age V5 known which value for nres? */
9726: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */
1.235 brouard 9727: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* model line */
9728: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */
1.237 brouard 9729: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */
1.235 brouard 9730: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
9731: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */
1.237 brouard 9732: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */
9733: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */
9734: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */
1.235 brouard 9735: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4);
9736: k4++;;
9737: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */
9738: k3q= resultmodel[k1]; /* resultmodel[2] = 1=k3 */
9739: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */
1.237 brouard 9740: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */
9741: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */
9742: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */
1.235 brouard 9743: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]);
9744: k4q++;;
9745: }
9746: }
1.234 brouard 9747:
1.235 brouard 9748: TKresult[nres]=++k; /* Combination for the nresult and the model */
1.230 brouard 9749: return (0);
9750: }
1.235 brouard 9751:
1.230 brouard 9752: int decodemodel( char model[], int lastobs)
9753: /**< This routine decodes the model and returns:
1.224 brouard 9754: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
9755: * - nagesqr = 1 if age*age in the model, otherwise 0.
9756: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
9757: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
9758: * - cptcovage number of covariates with age*products =2
9759: * - cptcovs number of simple covariates
9760: * - 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
9761: * which is a new column after the 9 (ncovcol) variables.
9762: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
9763: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
9764: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
9765: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
9766: */
1.136 brouard 9767: {
1.238 brouard 9768: int i, j, k, ks, v;
1.227 brouard 9769: int j1, k1, k2, k3, k4;
1.136 brouard 9770: char modelsav[80];
1.145 brouard 9771: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 9772: char *strpt;
1.136 brouard 9773:
1.145 brouard 9774: /*removespace(model);*/
1.136 brouard 9775: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 9776: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 9777: if (strstr(model,"AGE") !=0){
1.192 brouard 9778: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
9779: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 9780: return 1;
9781: }
1.141 brouard 9782: if (strstr(model,"v") !=0){
9783: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
9784: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
9785: return 1;
9786: }
1.187 brouard 9787: strcpy(modelsav,model);
9788: if ((strpt=strstr(model,"age*age")) !=0){
9789: printf(" strpt=%s, model=%s\n",strpt, model);
9790: if(strpt != model){
1.234 brouard 9791: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 9792: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 9793: corresponding column of parameters.\n",model);
1.234 brouard 9794: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 9795: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 9796: corresponding column of parameters.\n",model); fflush(ficlog);
1.234 brouard 9797: return 1;
1.225 brouard 9798: }
1.187 brouard 9799: nagesqr=1;
9800: if (strstr(model,"+age*age") !=0)
1.234 brouard 9801: substrchaine(modelsav, model, "+age*age");
1.187 brouard 9802: else if (strstr(model,"age*age+") !=0)
1.234 brouard 9803: substrchaine(modelsav, model, "age*age+");
1.187 brouard 9804: else
1.234 brouard 9805: substrchaine(modelsav, model, "age*age");
1.187 brouard 9806: }else
9807: nagesqr=0;
9808: if (strlen(modelsav) >1){
9809: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
9810: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 9811: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 9812: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 9813: * cst, age and age*age
9814: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
9815: /* including age products which are counted in cptcovage.
9816: * but the covariates which are products must be treated
9817: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 9818: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
9819: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 9820:
9821:
1.187 brouard 9822: /* Design
9823: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
9824: * < ncovcol=8 >
9825: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
9826: * k= 1 2 3 4 5 6 7 8
9827: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
9828: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 9829: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
9830: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 9831: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
9832: * Tage[++cptcovage]=k
9833: * if products, new covar are created after ncovcol with k1
9834: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
9835: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
9836: * 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
9837: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
9838: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
9839: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
9840: * < ncovcol=8 >
9841: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
9842: * k= 1 2 3 4 5 6 7 8 9 10 11 12
9843: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
9844: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
9845: * p Tprod[1]@2={ 6, 5}
9846: *p Tvard[1][1]@4= {7, 8, 5, 6}
9847: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
9848: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
9849: *How to reorganize?
9850: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
9851: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
9852: * {2, 1, 4, 8, 5, 6, 3, 7}
9853: * Struct []
9854: */
1.225 brouard 9855:
1.187 brouard 9856: /* This loop fills the array Tvar from the string 'model'.*/
9857: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
9858: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
9859: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
9860: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
9861: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
9862: /* k=1 Tvar[1]=2 (from V2) */
9863: /* k=5 Tvar[5] */
9864: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 9865: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 9866: /* } */
1.198 brouard 9867: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 9868: /*
9869: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.227 brouard 9870: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/
9871: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0;
9872: }
1.187 brouard 9873: cptcovage=0;
9874: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.234 brouard 9875: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
1.225 brouard 9876: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
1.234 brouard 9877: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
9878: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
9879: /*scanf("%d",i);*/
9880: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
9881: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
9882: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
9883: /* covar is not filled and then is empty */
9884: cptcovprod--;
9885: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
9886: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
9887: Typevar[k]=1; /* 1 for age product */
9888: cptcovage++; /* Sums the number of covariates which include age as a product */
9889: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
9890: /*printf("stre=%s ", stre);*/
9891: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
9892: cptcovprod--;
9893: cutl(stre,strb,strc,'V');
9894: Tvar[k]=atoi(stre);
9895: Typevar[k]=1; /* 1 for age product */
9896: cptcovage++;
9897: Tage[cptcovage]=k;
9898: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
9899: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
9900: cptcovn++;
9901: cptcovprodnoage++;k1++;
9902: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
9903: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
9904: because this model-covariate is a construction we invent a new column
9905: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
9906: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
9907: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
9908: Typevar[k]=2; /* 2 for double fixed dummy covariates */
9909: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
9910: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
9911: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */
9912: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
9913: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
9914: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
9915: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
9916: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
1.225 brouard 9917: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
1.234 brouard 9918: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
9919: for (i=1; i<=lastobs;i++){
9920: /* Computes the new covariate which is a product of
9921: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
9922: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
9923: }
9924: } /* End age is not in the model */
9925: } /* End if model includes a product */
9926: else { /* no more sum */
9927: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
9928: /* scanf("%d",i);*/
9929: cutl(strd,strc,strb,'V');
9930: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */
9931: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
9932: Tvar[k]=atoi(strd);
9933: Typevar[k]=0; /* 0 for simple covariates */
9934: }
9935: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 9936: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 9937: scanf("%d",i);*/
1.187 brouard 9938: } /* end of loop + on total covariates */
9939: } /* end if strlen(modelsave == 0) age*age might exist */
9940: } /* end if strlen(model == 0) */
1.136 brouard 9941:
9942: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
9943: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 9944:
1.136 brouard 9945: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 9946: printf("cptcovprod=%d ", cptcovprod);
9947: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
9948: scanf("%d ",i);*/
9949:
9950:
1.230 brouard 9951: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
9952: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */
1.226 brouard 9953: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
9954: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
9955: k = 1 2 3 4 5 6 7 8 9
9956: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
9957: Typevar[k]= 0 0 0 2 1 0 2 1 1
1.227 brouard 9958: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3
9959: Dummy[k] 1 0 0 0 3 1 1 2 3
9960: Tmodelind[combination of covar]=k;
1.225 brouard 9961: */
9962: /* Dispatching between quantitative and time varying covariates */
1.226 brouard 9963: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 9964: /* 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 9965: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.227 brouard 9966: printf("Model=%s\n\
9967: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
9968: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
9969: 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);
9970: fprintf(ficlog,"Model=%s\n\
9971: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
9972: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\
9973: 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 9974: for(k=-1;k<=cptcovt; k++){ Fixed[k]=0; Dummy[k]=0;}
1.234 brouard 9975: 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 */
9976: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */
1.227 brouard 9977: Fixed[k]= 0;
9978: Dummy[k]= 0;
1.225 brouard 9979: ncoveff++;
1.232 brouard 9980: ncovf++;
1.234 brouard 9981: nsd++;
9982: modell[k].maintype= FTYPE;
9983: TvarsD[nsd]=Tvar[k];
9984: TvarsDind[nsd]=k;
9985: TvarF[ncovf]=Tvar[k];
9986: TvarFind[ncovf]=k;
9987: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9988: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
9989: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */
9990: Fixed[k]= 0;
9991: Dummy[k]= 0;
9992: ncoveff++;
9993: ncovf++;
9994: modell[k].maintype= FTYPE;
9995: TvarF[ncovf]=Tvar[k];
9996: TvarFind[ncovf]=k;
1.230 brouard 9997: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.231 brouard 9998: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */
1.240 brouard 9999: }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 10000: Fixed[k]= 0;
10001: Dummy[k]= 1;
1.230 brouard 10002: nqfveff++;
1.234 brouard 10003: modell[k].maintype= FTYPE;
10004: modell[k].subtype= FQ;
10005: nsq++;
10006: TvarsQ[nsq]=Tvar[k];
10007: TvarsQind[nsq]=k;
1.232 brouard 10008: ncovf++;
1.234 brouard 10009: TvarF[ncovf]=Tvar[k];
10010: TvarFind[ncovf]=k;
1.231 brouard 10011: 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 10012: 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 10013: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */
1.227 brouard 10014: Fixed[k]= 1;
10015: Dummy[k]= 0;
1.225 brouard 10016: ntveff++; /* Only simple time varying dummy variable */
1.234 brouard 10017: modell[k].maintype= VTYPE;
10018: modell[k].subtype= VD;
10019: nsd++;
10020: TvarsD[nsd]=Tvar[k];
10021: TvarsDind[nsd]=k;
10022: ncovv++; /* Only simple time varying variables */
10023: TvarV[ncovv]=Tvar[k];
1.242 brouard 10024: 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 10025: 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 */
10026: 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 10027: 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);
10028: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv);
1.231 brouard 10029: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/
1.234 brouard 10030: Fixed[k]= 1;
10031: Dummy[k]= 1;
10032: nqtveff++;
10033: modell[k].maintype= VTYPE;
10034: modell[k].subtype= VQ;
10035: ncovv++; /* Only simple time varying variables */
10036: nsq++;
10037: TvarsQ[nsq]=Tvar[k];
10038: TvarsQind[nsq]=k;
10039: TvarV[ncovv]=Tvar[k];
1.242 brouard 10040: 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 10041: 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 */
10042: 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 10043: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */
10044: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */
10045: 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 10046: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv);
1.227 brouard 10047: }else if (Typevar[k] == 1) { /* product with age */
1.234 brouard 10048: ncova++;
10049: TvarA[ncova]=Tvar[k];
10050: TvarAind[ncova]=k;
1.231 brouard 10051: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */
1.240 brouard 10052: Fixed[k]= 2;
10053: Dummy[k]= 2;
10054: modell[k].maintype= ATYPE;
10055: modell[k].subtype= APFD;
10056: /* ncoveff++; */
1.227 brouard 10057: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/
1.240 brouard 10058: Fixed[k]= 2;
10059: Dummy[k]= 3;
10060: modell[k].maintype= ATYPE;
10061: modell[k].subtype= APFQ; /* Product age * fixed quantitative */
10062: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */
1.227 brouard 10063: }else if( Tvar[k] <=ncovcol+nqv+ntv ){
1.240 brouard 10064: Fixed[k]= 3;
10065: Dummy[k]= 2;
10066: modell[k].maintype= ATYPE;
10067: modell[k].subtype= APVD; /* Product age * varying dummy */
10068: /* ntveff++; /\* Only simple time varying dummy variable *\/ */
1.227 brouard 10069: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 10070: Fixed[k]= 3;
10071: Dummy[k]= 3;
10072: modell[k].maintype= ATYPE;
10073: modell[k].subtype= APVQ; /* Product age * varying quantitative */
10074: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */
1.227 brouard 10075: }
10076: }else if (Typevar[k] == 2) { /* product without age */
10077: k1=Tposprod[k];
10078: if(Tvard[k1][1] <=ncovcol){
1.240 brouard 10079: if(Tvard[k1][2] <=ncovcol){
10080: Fixed[k]= 1;
10081: Dummy[k]= 0;
10082: modell[k].maintype= FTYPE;
10083: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */
10084: ncovf++; /* Fixed variables without age */
10085: TvarF[ncovf]=Tvar[k];
10086: TvarFind[ncovf]=k;
10087: }else if(Tvard[k1][2] <=ncovcol+nqv){
10088: Fixed[k]= 0; /* or 2 ?*/
10089: Dummy[k]= 1;
10090: modell[k].maintype= FTYPE;
10091: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */
10092: ncovf++; /* Varying variables without age */
10093: TvarF[ncovf]=Tvar[k];
10094: TvarFind[ncovf]=k;
10095: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10096: Fixed[k]= 1;
10097: Dummy[k]= 0;
10098: modell[k].maintype= VTYPE;
10099: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */
10100: ncovv++; /* Varying variables without age */
10101: TvarV[ncovv]=Tvar[k];
10102: TvarVind[ncovv]=k;
10103: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10104: Fixed[k]= 1;
10105: Dummy[k]= 1;
10106: modell[k].maintype= VTYPE;
10107: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */
10108: ncovv++; /* Varying variables without age */
10109: TvarV[ncovv]=Tvar[k];
10110: TvarVind[ncovv]=k;
10111: }
1.227 brouard 10112: }else if(Tvard[k1][1] <=ncovcol+nqv){
1.240 brouard 10113: if(Tvard[k1][2] <=ncovcol){
10114: Fixed[k]= 0; /* or 2 ?*/
10115: Dummy[k]= 1;
10116: modell[k].maintype= FTYPE;
10117: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */
10118: ncovf++; /* Fixed variables without age */
10119: TvarF[ncovf]=Tvar[k];
10120: TvarFind[ncovf]=k;
10121: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10122: Fixed[k]= 1;
10123: Dummy[k]= 1;
10124: modell[k].maintype= VTYPE;
10125: modell[k].subtype= VPDQ; /* Product fixed quantitative * 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= VPQQ; /* Product fixed quantitative * varying quantitative */
10134: ncovv++; /* Varying variables without age */
10135: TvarV[ncovv]=Tvar[k];
10136: TvarVind[ncovv]=k;
10137: ncovv++; /* Varying variables without age */
10138: TvarV[ncovv]=Tvar[k];
10139: TvarVind[ncovv]=k;
10140: }
1.227 brouard 10141: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
1.240 brouard 10142: if(Tvard[k1][2] <=ncovcol){
10143: Fixed[k]= 1;
10144: Dummy[k]= 1;
10145: modell[k].maintype= VTYPE;
10146: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */
10147: ncovv++; /* Varying variables without age */
10148: TvarV[ncovv]=Tvar[k];
10149: TvarVind[ncovv]=k;
10150: }else if(Tvard[k1][2] <=ncovcol+nqv){
10151: Fixed[k]= 1;
10152: Dummy[k]= 1;
10153: modell[k].maintype= VTYPE;
10154: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */
10155: ncovv++; /* Varying variables without age */
10156: TvarV[ncovv]=Tvar[k];
10157: TvarVind[ncovv]=k;
10158: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10159: Fixed[k]= 1;
10160: Dummy[k]= 0;
10161: modell[k].maintype= VTYPE;
10162: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */
10163: ncovv++; /* Varying variables without age */
10164: TvarV[ncovv]=Tvar[k];
10165: TvarVind[ncovv]=k;
10166: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10167: Fixed[k]= 1;
10168: Dummy[k]= 1;
10169: modell[k].maintype= VTYPE;
10170: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */
10171: ncovv++; /* Varying variables without age */
10172: TvarV[ncovv]=Tvar[k];
10173: TvarVind[ncovv]=k;
10174: }
1.227 brouard 10175: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
1.240 brouard 10176: if(Tvard[k1][2] <=ncovcol){
10177: Fixed[k]= 1;
10178: Dummy[k]= 1;
10179: modell[k].maintype= VTYPE;
10180: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */
10181: ncovv++; /* Varying variables without age */
10182: TvarV[ncovv]=Tvar[k];
10183: TvarVind[ncovv]=k;
10184: }else if(Tvard[k1][2] <=ncovcol+nqv){
10185: Fixed[k]= 1;
10186: Dummy[k]= 1;
10187: modell[k].maintype= VTYPE;
10188: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */
10189: ncovv++; /* Varying variables without age */
10190: TvarV[ncovv]=Tvar[k];
10191: TvarVind[ncovv]=k;
10192: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
10193: Fixed[k]= 1;
10194: Dummy[k]= 1;
10195: modell[k].maintype= VTYPE;
10196: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */
10197: ncovv++; /* Varying variables without age */
10198: TvarV[ncovv]=Tvar[k];
10199: TvarVind[ncovv]=k;
10200: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
10201: Fixed[k]= 1;
10202: Dummy[k]= 1;
10203: modell[k].maintype= VTYPE;
10204: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */
10205: ncovv++; /* Varying variables without age */
10206: TvarV[ncovv]=Tvar[k];
10207: TvarVind[ncovv]=k;
10208: }
1.227 brouard 10209: }else{
1.240 brouard 10210: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10211: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
10212: } /*end k1*/
1.225 brouard 10213: }else{
1.226 brouard 10214: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
10215: 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 10216: }
1.227 brouard 10217: 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 10218: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype);
1.227 brouard 10219: 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]);
10220: }
10221: /* Searching for doublons in the model */
10222: for(k1=1; k1<= cptcovt;k1++){
10223: for(k2=1; k2 <k1;k2++){
1.285 brouard 10224: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */
10225: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){
1.234 brouard 10226: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */
10227: if(Tvar[k1]==Tvar[k2]){
1.285 brouard 10228: 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]);
10229: 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 10230: return(1);
10231: }
10232: }else if (Typevar[k1] ==2){
10233: k3=Tposprod[k1];
10234: k4=Tposprod[k2];
10235: 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])) ){
10236: 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]]);
10237: 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);
10238: return(1);
10239: }
10240: }
1.227 brouard 10241: }
10242: }
1.225 brouard 10243: }
10244: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
10245: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.234 brouard 10246: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq);
10247: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq);
1.137 brouard 10248: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 10249: /*endread:*/
1.225 brouard 10250: printf("Exiting decodemodel: ");
10251: return (1);
1.136 brouard 10252: }
10253:
1.169 brouard 10254: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.248 brouard 10255: {/* Check ages at death */
1.136 brouard 10256: int i, m;
1.218 brouard 10257: int firstone=0;
10258:
1.136 brouard 10259: for (i=1; i<=imx; i++) {
10260: for(m=2; (m<= maxwav); m++) {
10261: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
10262: anint[m][i]=9999;
1.216 brouard 10263: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
10264: s[m][i]=-1;
1.136 brouard 10265: }
10266: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.260 brouard 10267: *nberr = *nberr + 1;
1.218 brouard 10268: if(firstone == 0){
10269: firstone=1;
1.260 brouard 10270: 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 10271: }
1.262 brouard 10272: 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 10273: s[m][i]=-1; /* Droping the death status */
1.136 brouard 10274: }
10275: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 10276: (*nberr)++;
1.259 brouard 10277: 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 10278: 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 10279: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */
1.136 brouard 10280: }
10281: }
10282: }
10283:
10284: for (i=1; i<=imx; i++) {
10285: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
10286: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 10287: 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 10288: if (s[m][i] >= nlstate+1) {
1.169 brouard 10289: if(agedc[i]>0){
10290: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 10291: agev[m][i]=agedc[i];
1.214 brouard 10292: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 10293: }else {
1.136 brouard 10294: if ((int)andc[i]!=9999){
10295: nbwarn++;
10296: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
10297: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
10298: agev[m][i]=-1;
10299: }
10300: }
1.169 brouard 10301: } /* agedc > 0 */
1.214 brouard 10302: } /* end if */
1.136 brouard 10303: else if(s[m][i] !=9){ /* Standard case, age in fractional
10304: years but with the precision of a month */
10305: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
10306: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
10307: agev[m][i]=1;
10308: else if(agev[m][i] < *agemin){
10309: *agemin=agev[m][i];
10310: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
10311: }
10312: else if(agev[m][i] >*agemax){
10313: *agemax=agev[m][i];
1.156 brouard 10314: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 10315: }
10316: /*agev[m][i]=anint[m][i]-annais[i];*/
10317: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 10318: } /* en if 9*/
1.136 brouard 10319: else { /* =9 */
1.214 brouard 10320: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 10321: agev[m][i]=1;
10322: s[m][i]=-1;
10323: }
10324: }
1.214 brouard 10325: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 10326: agev[m][i]=1;
1.214 brouard 10327: else{
10328: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10329: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
10330: agev[m][i]=0;
10331: }
10332: } /* End for lastpass */
10333: }
1.136 brouard 10334:
10335: for (i=1; i<=imx; i++) {
10336: for(m=firstpass; (m<=lastpass); m++){
10337: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 10338: (*nberr)++;
1.136 brouard 10339: 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);
10340: 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);
10341: return 1;
10342: }
10343: }
10344: }
10345:
10346: /*for (i=1; i<=imx; i++){
10347: for (m=firstpass; (m<lastpass); m++){
10348: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
10349: }
10350:
10351: }*/
10352:
10353:
1.139 brouard 10354: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
10355: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 10356:
10357: return (0);
1.164 brouard 10358: /* endread:*/
1.136 brouard 10359: printf("Exiting calandcheckages: ");
10360: return (1);
10361: }
10362:
1.172 brouard 10363: #if defined(_MSC_VER)
10364: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10365: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
10366: //#include "stdafx.h"
10367: //#include <stdio.h>
10368: //#include <tchar.h>
10369: //#include <windows.h>
10370: //#include <iostream>
10371: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
10372:
10373: LPFN_ISWOW64PROCESS fnIsWow64Process;
10374:
10375: BOOL IsWow64()
10376: {
10377: BOOL bIsWow64 = FALSE;
10378:
10379: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
10380: // (HANDLE, PBOOL);
10381:
10382: //LPFN_ISWOW64PROCESS fnIsWow64Process;
10383:
10384: HMODULE module = GetModuleHandle(_T("kernel32"));
10385: const char funcName[] = "IsWow64Process";
10386: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
10387: GetProcAddress(module, funcName);
10388:
10389: if (NULL != fnIsWow64Process)
10390: {
10391: if (!fnIsWow64Process(GetCurrentProcess(),
10392: &bIsWow64))
10393: //throw std::exception("Unknown error");
10394: printf("Unknown error\n");
10395: }
10396: return bIsWow64 != FALSE;
10397: }
10398: #endif
1.177 brouard 10399:
1.191 brouard 10400: void syscompilerinfo(int logged)
1.292 brouard 10401: {
10402: #include <stdint.h>
10403:
10404: /* #include "syscompilerinfo.h"*/
1.185 brouard 10405: /* command line Intel compiler 32bit windows, XP compatible:*/
10406: /* /GS /W3 /Gy
10407: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
10408: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
10409: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 10410: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
10411: */
10412: /* 64 bits */
1.185 brouard 10413: /*
10414: /GS /W3 /Gy
10415: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
10416: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
10417: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
10418: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
10419: /* Optimization are useless and O3 is slower than O2 */
10420: /*
10421: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
10422: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
10423: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
10424: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
10425: */
1.186 brouard 10426: /* Link is */ /* /OUT:"visual studio
1.185 brouard 10427: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
10428: /PDB:"visual studio
10429: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
10430: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
10431: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
10432: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
10433: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
10434: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
10435: uiAccess='false'"
10436: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
10437: /NOLOGO /TLBID:1
10438: */
1.292 brouard 10439:
10440:
1.177 brouard 10441: #if defined __INTEL_COMPILER
1.178 brouard 10442: #if defined(__GNUC__)
10443: struct utsname sysInfo; /* For Intel on Linux and OS/X */
10444: #endif
1.177 brouard 10445: #elif defined(__GNUC__)
1.179 brouard 10446: #ifndef __APPLE__
1.174 brouard 10447: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 10448: #endif
1.177 brouard 10449: struct utsname sysInfo;
1.178 brouard 10450: int cross = CROSS;
10451: if (cross){
10452: printf("Cross-");
1.191 brouard 10453: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 10454: }
1.174 brouard 10455: #endif
10456:
1.191 brouard 10457: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 10458: #if defined(__clang__)
1.191 brouard 10459: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 10460: #endif
10461: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 10462: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 10463: #endif
10464: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 10465: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 10466: #endif
10467: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 10468: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 10469: #endif
10470: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 10471: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 10472: #endif
10473: #if defined(_MSC_VER)
1.191 brouard 10474: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 10475: #endif
10476: #if defined(__PGI)
1.191 brouard 10477: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 10478: #endif
10479: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 10480: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 10481: #endif
1.191 brouard 10482: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 10483:
1.167 brouard 10484: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
10485: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
10486: // Windows (x64 and x86)
1.191 brouard 10487: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 10488: #elif __unix__ // all unices, not all compilers
10489: // Unix
1.191 brouard 10490: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 10491: #elif __linux__
10492: // linux
1.191 brouard 10493: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 10494: #elif __APPLE__
1.174 brouard 10495: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 10496: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 10497: #endif
10498:
10499: /* __MINGW32__ */
10500: /* __CYGWIN__ */
10501: /* __MINGW64__ */
10502: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
10503: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
10504: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
10505: /* _WIN64 // Defined for applications for Win64. */
10506: /* _M_X64 // Defined for compilations that target x64 processors. */
10507: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 10508:
1.167 brouard 10509: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 10510: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 10511: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 10512: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 10513: #else
1.191 brouard 10514: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 10515: #endif
10516:
1.169 brouard 10517: #if defined(__GNUC__)
10518: # if defined(__GNUC_PATCHLEVEL__)
10519: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10520: + __GNUC_MINOR__ * 100 \
10521: + __GNUC_PATCHLEVEL__)
10522: # else
10523: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
10524: + __GNUC_MINOR__ * 100)
10525: # endif
1.174 brouard 10526: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 10527: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 10528:
10529: if (uname(&sysInfo) != -1) {
10530: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 10531: 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 10532: }
10533: else
10534: perror("uname() error");
1.179 brouard 10535: //#ifndef __INTEL_COMPILER
10536: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 10537: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 10538: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 10539: #endif
1.169 brouard 10540: #endif
1.172 brouard 10541:
1.286 brouard 10542: // void main ()
1.172 brouard 10543: // {
1.169 brouard 10544: #if defined(_MSC_VER)
1.174 brouard 10545: if (IsWow64()){
1.191 brouard 10546: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
10547: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 10548: }
10549: else{
1.191 brouard 10550: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
10551: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 10552: }
1.172 brouard 10553: // printf("\nPress Enter to continue...");
10554: // getchar();
10555: // }
10556:
1.169 brouard 10557: #endif
10558:
1.167 brouard 10559:
1.219 brouard 10560: }
1.136 brouard 10561:
1.219 brouard 10562: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.288 brouard 10563: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/
1.235 brouard 10564: int i, j, k, i1, k4=0, nres=0 ;
1.202 brouard 10565: /* double ftolpl = 1.e-10; */
1.180 brouard 10566: double age, agebase, agelim;
1.203 brouard 10567: double tot;
1.180 brouard 10568:
1.202 brouard 10569: strcpy(filerespl,"PL_");
10570: strcat(filerespl,fileresu);
10571: if((ficrespl=fopen(filerespl,"w"))==NULL) {
1.288 brouard 10572: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
10573: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1;
1.202 brouard 10574: }
1.288 brouard 10575: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
10576: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl);
1.202 brouard 10577: pstamp(ficrespl);
1.288 brouard 10578: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 10579: fprintf(ficrespl,"#Age ");
10580: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
10581: fprintf(ficrespl,"\n");
1.180 brouard 10582:
1.219 brouard 10583: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 10584:
1.219 brouard 10585: agebase=ageminpar;
10586: agelim=agemaxpar;
1.180 brouard 10587:
1.227 brouard 10588: /* i1=pow(2,ncoveff); */
1.234 brouard 10589: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
1.219 brouard 10590: if (cptcovn < 1){i1=1;}
1.180 brouard 10591:
1.238 brouard 10592: for(k=1; k<=i1;k++){ /* For each combination k of dummy covariates in the model */
10593: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
1.253 brouard 10594: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10595: continue;
1.235 brouard 10596:
1.238 brouard 10597: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10598: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
10599: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
10600: /* k=k+1; */
10601: /* to clean */
10602: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
10603: fprintf(ficrespl,"#******");
10604: printf("#******");
10605: fprintf(ficlog,"#******");
10606: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
10607: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/
10608: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10609: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10610: }
10611: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
10612: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10613: fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10614: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10615: }
10616: fprintf(ficrespl,"******\n");
10617: printf("******\n");
10618: fprintf(ficlog,"******\n");
10619: if(invalidvarcomb[k]){
10620: printf("\nCombination (%d) ignored because no case \n",k);
10621: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k);
10622: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k);
10623: continue;
10624: }
1.219 brouard 10625:
1.238 brouard 10626: fprintf(ficrespl,"#Age ");
10627: for(j=1;j<=cptcoveff;j++) {
10628: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10629: }
10630: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
10631: fprintf(ficrespl,"Total Years_to_converge\n");
1.227 brouard 10632:
1.238 brouard 10633: for (age=agebase; age<=agelim; age++){
10634: /* for (age=agebase; age<=agebase; age++){ */
10635: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres);
10636: fprintf(ficrespl,"%.0f ",age );
10637: for(j=1;j<=cptcoveff;j++)
10638: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10639: tot=0.;
10640: for(i=1; i<=nlstate;i++){
10641: tot += prlim[i][i];
10642: fprintf(ficrespl," %.5f", prlim[i][i]);
10643: }
10644: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
10645: } /* Age */
10646: /* was end of cptcod */
10647: } /* cptcov */
10648: } /* nres */
1.219 brouard 10649: return 0;
1.180 brouard 10650: }
10651:
1.218 brouard 10652: 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 10653: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/
1.218 brouard 10654:
10655: /* Computes the back prevalence limit for any combination of covariate values
10656: * at any age between ageminpar and agemaxpar
10657: */
1.235 brouard 10658: int i, j, k, i1, nres=0 ;
1.217 brouard 10659: /* double ftolpl = 1.e-10; */
10660: double age, agebase, agelim;
10661: double tot;
1.218 brouard 10662: /* double ***mobaverage; */
10663: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 10664:
10665: strcpy(fileresplb,"PLB_");
10666: strcat(fileresplb,fileresu);
10667: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
1.288 brouard 10668: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
10669: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1;
1.217 brouard 10670: }
1.288 brouard 10671: printf("Computing backward prevalence: result on file '%s' \n", fileresplb);
10672: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb);
1.217 brouard 10673: pstamp(ficresplb);
1.288 brouard 10674: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.217 brouard 10675: fprintf(ficresplb,"#Age ");
10676: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
10677: fprintf(ficresplb,"\n");
10678:
1.218 brouard 10679:
10680: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
10681:
10682: agebase=ageminpar;
10683: agelim=agemaxpar;
10684:
10685:
1.227 brouard 10686: i1=pow(2,cptcoveff);
1.218 brouard 10687: if (cptcovn < 1){i1=1;}
1.227 brouard 10688:
1.238 brouard 10689: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10690: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 10691: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10692: continue;
10693: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
10694: fprintf(ficresplb,"#******");
10695: printf("#******");
10696: fprintf(ficlog,"#******");
10697: for(j=1;j<=cptcoveff ;j++) {/* all covariates */
10698: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10699: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10700: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10701: }
10702: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
10703: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10704: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10705: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10706: }
10707: fprintf(ficresplb,"******\n");
10708: printf("******\n");
10709: fprintf(ficlog,"******\n");
10710: if(invalidvarcomb[k]){
10711: printf("\nCombination (%d) ignored because no cases \n",k);
10712: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
10713: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
10714: continue;
10715: }
1.218 brouard 10716:
1.238 brouard 10717: fprintf(ficresplb,"#Age ");
10718: for(j=1;j<=cptcoveff;j++) {
10719: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10720: }
10721: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
10722: fprintf(ficresplb,"Total Years_to_converge\n");
1.218 brouard 10723:
10724:
1.238 brouard 10725: for (age=agebase; age<=agelim; age++){
10726: /* for (age=agebase; age<=agebase; age++){ */
10727: if(mobilavproj > 0){
10728: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
10729: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 10730: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres);
1.238 brouard 10731: }else if (mobilavproj == 0){
10732: 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);
10733: 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);
10734: exit(1);
10735: }else{
10736: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.242 brouard 10737: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres);
1.266 brouard 10738: /* printf("TOTOT\n"); */
10739: /* exit(1); */
1.238 brouard 10740: }
10741: fprintf(ficresplb,"%.0f ",age );
10742: for(j=1;j<=cptcoveff;j++)
10743: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10744: tot=0.;
10745: for(i=1; i<=nlstate;i++){
10746: tot += bprlim[i][i];
10747: fprintf(ficresplb," %.5f", bprlim[i][i]);
10748: }
10749: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
10750: } /* Age */
10751: /* was end of cptcod */
1.255 brouard 10752: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */
1.238 brouard 10753: } /* end of any combination */
10754: } /* end of nres */
1.218 brouard 10755: /* hBijx(p, bage, fage); */
10756: /* fclose(ficrespijb); */
10757:
10758: return 0;
1.217 brouard 10759: }
1.218 brouard 10760:
1.180 brouard 10761: int hPijx(double *p, int bage, int fage){
10762: /*------------- h Pij x at various ages ------------*/
10763:
10764: int stepsize;
10765: int agelim;
10766: int hstepm;
10767: int nhstepm;
1.235 brouard 10768: int h, i, i1, j, k, k4, nres=0;
1.180 brouard 10769:
10770: double agedeb;
10771: double ***p3mat;
10772:
1.201 brouard 10773: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 10774: if((ficrespij=fopen(filerespij,"w"))==NULL) {
10775: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
10776: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
10777: }
10778: printf("Computing pij: result on file '%s' \n", filerespij);
10779: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
10780:
10781: stepsize=(int) (stepm+YEARM-1)/YEARM;
10782: /*if (stepm<=24) stepsize=2;*/
10783:
10784: agelim=AGESUP;
10785: hstepm=stepsize*YEARM; /* Every year of age */
10786: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 10787:
1.180 brouard 10788: /* hstepm=1; aff par mois*/
10789: pstamp(ficrespij);
10790: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.227 brouard 10791: i1= pow(2,cptcoveff);
1.218 brouard 10792: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10793: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
10794: /* k=k+1; */
1.235 brouard 10795: for(nres=1; nres <= nresult; nres++) /* For each resultline */
10796: for(k=1; k<=i1;k++){
1.253 brouard 10797: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 10798: continue;
1.183 brouard 10799: fprintf(ficrespij,"\n#****** ");
1.227 brouard 10800: for(j=1;j<=cptcoveff;j++)
1.198 brouard 10801: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 10802: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */
10803: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10804: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);
10805: }
1.183 brouard 10806: fprintf(ficrespij,"******\n");
10807:
10808: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
10809: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
10810: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
10811:
10812: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 10813:
1.183 brouard 10814: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10815: oldm=oldms;savm=savms;
1.235 brouard 10816: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres);
1.183 brouard 10817: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
10818: for(i=1; i<=nlstate;i++)
10819: for(j=1; j<=nlstate+ndeath;j++)
10820: fprintf(ficrespij," %1d-%1d",i,j);
10821: fprintf(ficrespij,"\n");
10822: for (h=0; h<=nhstepm; h++){
10823: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
10824: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 10825: for(i=1; i<=nlstate;i++)
10826: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 10827: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 10828: fprintf(ficrespij,"\n");
10829: }
1.183 brouard 10830: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10831: fprintf(ficrespij,"\n");
10832: }
1.180 brouard 10833: /*}*/
10834: }
1.218 brouard 10835: return 0;
1.180 brouard 10836: }
1.218 brouard 10837:
10838: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 10839: /*------------- h Bij x at various ages ------------*/
10840:
10841: int stepsize;
1.218 brouard 10842: /* int agelim; */
10843: int ageminl;
1.217 brouard 10844: int hstepm;
10845: int nhstepm;
1.238 brouard 10846: int h, i, i1, j, k, nres;
1.218 brouard 10847:
1.217 brouard 10848: double agedeb;
10849: double ***p3mat;
1.218 brouard 10850:
10851: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
10852: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
10853: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
10854: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
10855: }
10856: printf("Computing pij back: result on file '%s' \n", filerespijb);
10857: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
10858:
10859: stepsize=(int) (stepm+YEARM-1)/YEARM;
10860: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 10861:
1.218 brouard 10862: /* agelim=AGESUP; */
1.289 brouard 10863: ageminl=AGEINF; /* was 30 */
1.218 brouard 10864: hstepm=stepsize*YEARM; /* Every year of age */
10865: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
10866:
10867: /* hstepm=1; aff par mois*/
10868: pstamp(ficrespijb);
1.255 brouard 10869: 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 10870: i1= pow(2,cptcoveff);
1.218 brouard 10871: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
10872: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
10873: /* k=k+1; */
1.238 brouard 10874: for(nres=1; nres <= nresult; nres++){ /* For each resultline */
10875: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 10876: if(i1 != 1 && TKresult[nres]!= k)
1.238 brouard 10877: continue;
10878: fprintf(ficrespijb,"\n#****** ");
10879: for(j=1;j<=cptcoveff;j++)
10880: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10881: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
10882: fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
10883: }
10884: fprintf(ficrespijb,"******\n");
1.264 brouard 10885: if(invalidvarcomb[k]){ /* Is it necessary here? */
1.238 brouard 10886: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
10887: continue;
10888: }
10889:
10890: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
10891: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
10892: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
1.297 brouard 10893: 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 */
10894: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/
1.238 brouard 10895:
10896: /* nhstepm=nhstepm*YEARM; aff par mois*/
10897:
1.266 brouard 10898: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */
10899: /* and memory limitations if stepm is small */
10900:
1.238 brouard 10901: /* oldm=oldms;savm=savms; */
10902: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
1.267 brouard 10903: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres);
1.238 brouard 10904: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
1.255 brouard 10905: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j=");
1.217 brouard 10906: for(i=1; i<=nlstate;i++)
10907: for(j=1; j<=nlstate+ndeath;j++)
1.238 brouard 10908: fprintf(ficrespijb," %1d-%1d",i,j);
1.217 brouard 10909: fprintf(ficrespijb,"\n");
1.238 brouard 10910: for (h=0; h<=nhstepm; h++){
10911: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
10912: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
10913: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
10914: for(i=1; i<=nlstate;i++)
10915: for(j=1; j<=nlstate+ndeath;j++)
10916: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
10917: fprintf(ficrespijb,"\n");
10918: }
10919: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
10920: fprintf(ficrespijb,"\n");
10921: } /* end age deb */
10922: } /* end combination */
10923: } /* end nres */
1.218 brouard 10924: return 0;
10925: } /* hBijx */
1.217 brouard 10926:
1.180 brouard 10927:
1.136 brouard 10928: /***********************************************/
10929: /**************** Main Program *****************/
10930: /***********************************************/
10931:
10932: int main(int argc, char *argv[])
10933: {
10934: #ifdef GSL
10935: const gsl_multimin_fminimizer_type *T;
10936: size_t iteri = 0, it;
10937: int rval = GSL_CONTINUE;
10938: int status = GSL_SUCCESS;
10939: double ssval;
10940: #endif
10941: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.290 brouard 10942: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */
10943: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */
1.209 brouard 10944: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 10945: int jj, ll, li, lj, lk;
1.136 brouard 10946: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 10947: int num_filled;
1.136 brouard 10948: int itimes;
10949: int NDIM=2;
10950: int vpopbased=0;
1.235 brouard 10951: int nres=0;
1.258 brouard 10952: int endishere=0;
1.277 brouard 10953: int noffset=0;
1.274 brouard 10954: int ncurrv=0; /* Temporary variable */
10955:
1.164 brouard 10956: char ca[32], cb[32];
1.136 brouard 10957: /* FILE *fichtm; *//* Html File */
10958: /* FILE *ficgp;*/ /*Gnuplot File */
10959: struct stat info;
1.191 brouard 10960: double agedeb=0.;
1.194 brouard 10961:
10962: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 10963: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 10964:
1.165 brouard 10965: double fret;
1.191 brouard 10966: double dum=0.; /* Dummy variable */
1.136 brouard 10967: double ***p3mat;
1.218 brouard 10968: /* double ***mobaverage; */
1.164 brouard 10969:
10970: char line[MAXLINE];
1.197 brouard 10971: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
10972:
1.234 brouard 10973: char modeltemp[MAXLINE];
1.230 brouard 10974: char resultline[MAXLINE];
10975:
1.136 brouard 10976: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 10977: char *tok, *val; /* pathtot */
1.290 brouard 10978: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs declared globally ;*/
1.195 brouard 10979: int c, h , cpt, c2;
1.191 brouard 10980: int jl=0;
10981: int i1, j1, jk, stepsize=0;
1.194 brouard 10982: int count=0;
10983:
1.164 brouard 10984: int *tab;
1.136 brouard 10985: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.296 brouard 10986: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */
10987: /* double anprojf, mprojf, jprojf; */
10988: /* double jintmean,mintmean,aintmean; */
10989: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
10990: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */
10991: double yrfproj= 10.0; /* Number of years of forward projections */
10992: double yrbproj= 10.0; /* Number of years of backward projections */
10993: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */
1.136 brouard 10994: int mobilav=0,popforecast=0;
1.191 brouard 10995: int hstepm=0, nhstepm=0;
1.136 brouard 10996: int agemortsup;
10997: float sumlpop=0.;
10998: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
10999: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
11000:
1.191 brouard 11001: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 11002: double ftolpl=FTOL;
11003: double **prlim;
1.217 brouard 11004: double **bprlim;
1.136 brouard 11005: double ***param; /* Matrix of parameters */
1.251 brouard 11006: double ***paramstart; /* Matrix of starting parameter values */
11007: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */
1.136 brouard 11008: double **matcov; /* Matrix of covariance */
1.203 brouard 11009: double **hess; /* Hessian matrix */
1.136 brouard 11010: double ***delti3; /* Scale */
11011: double *delti; /* Scale */
11012: double ***eij, ***vareij;
11013: double **varpl; /* Variances of prevalence limits by age */
1.269 brouard 11014:
1.136 brouard 11015: double *epj, vepp;
1.164 brouard 11016:
1.273 brouard 11017: double dateprev1, dateprev2;
1.296 brouard 11018: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0;
11019: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0;
11020:
1.217 brouard 11021:
1.136 brouard 11022: double **ximort;
1.145 brouard 11023: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 11024: int *dcwave;
11025:
1.164 brouard 11026: char z[1]="c";
1.136 brouard 11027:
11028: /*char *strt;*/
11029: char strtend[80];
1.126 brouard 11030:
1.164 brouard 11031:
1.126 brouard 11032: /* setlocale (LC_ALL, ""); */
11033: /* bindtextdomain (PACKAGE, LOCALEDIR); */
11034: /* textdomain (PACKAGE); */
11035: /* setlocale (LC_CTYPE, ""); */
11036: /* setlocale (LC_MESSAGES, ""); */
11037:
11038: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 11039: rstart_time = time(NULL);
11040: /* (void) gettimeofday(&start_time,&tzp);*/
11041: start_time = *localtime(&rstart_time);
1.126 brouard 11042: curr_time=start_time;
1.157 brouard 11043: /*tml = *localtime(&start_time.tm_sec);*/
11044: /* strcpy(strstart,asctime(&tml)); */
11045: strcpy(strstart,asctime(&start_time));
1.126 brouard 11046:
11047: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 11048: /* tp.tm_sec = tp.tm_sec +86400; */
11049: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 11050: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
11051: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
11052: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 11053: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 11054: /* strt=asctime(&tmg); */
11055: /* printf("Time(after) =%s",strstart); */
11056: /* (void) time (&time_value);
11057: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
11058: * tm = *localtime(&time_value);
11059: * strstart=asctime(&tm);
11060: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
11061: */
11062:
11063: nberr=0; /* Number of errors and warnings */
11064: nbwarn=0;
1.184 brouard 11065: #ifdef WIN32
11066: _getcwd(pathcd, size);
11067: #else
1.126 brouard 11068: getcwd(pathcd, size);
1.184 brouard 11069: #endif
1.191 brouard 11070: syscompilerinfo(0);
1.196 brouard 11071: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 11072: if(argc <=1){
11073: printf("\nEnter the parameter file name: ");
1.205 brouard 11074: if(!fgets(pathr,FILENAMELENGTH,stdin)){
11075: printf("ERROR Empty parameter file name\n");
11076: goto end;
11077: }
1.126 brouard 11078: i=strlen(pathr);
11079: if(pathr[i-1]=='\n')
11080: pathr[i-1]='\0';
1.156 brouard 11081: i=strlen(pathr);
1.205 brouard 11082: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 11083: pathr[i-1]='\0';
1.205 brouard 11084: }
11085: i=strlen(pathr);
11086: if( i==0 ){
11087: printf("ERROR Empty parameter file name\n");
11088: goto end;
11089: }
11090: for (tok = pathr; tok != NULL; ){
1.126 brouard 11091: printf("Pathr |%s|\n",pathr);
11092: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
11093: printf("val= |%s| pathr=%s\n",val,pathr);
11094: strcpy (pathtot, val);
11095: if(pathr[0] == '\0') break; /* Dirty */
11096: }
11097: }
1.281 brouard 11098: else if (argc<=2){
11099: strcpy(pathtot,argv[1]);
11100: }
1.126 brouard 11101: else{
11102: strcpy(pathtot,argv[1]);
1.281 brouard 11103: strcpy(z,argv[2]);
11104: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]);
1.126 brouard 11105: }
11106: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
11107: /*cygwin_split_path(pathtot,path,optionfile);
11108: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
11109: /* cutv(path,optionfile,pathtot,'\\');*/
11110:
11111: /* Split argv[0], imach program to get pathimach */
11112: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
11113: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11114: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
11115: /* strcpy(pathimach,argv[0]); */
11116: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
11117: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
11118: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 11119: #ifdef WIN32
11120: _chdir(path); /* Can be a relative path */
11121: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
11122: #else
1.126 brouard 11123: chdir(path); /* Can be a relative path */
1.184 brouard 11124: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
11125: #endif
11126: printf("Current directory %s!\n",pathcd);
1.126 brouard 11127: strcpy(command,"mkdir ");
11128: strcat(command,optionfilefiname);
11129: if((outcmd=system(command)) != 0){
1.169 brouard 11130: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 11131: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
11132: /* fclose(ficlog); */
11133: /* exit(1); */
11134: }
11135: /* if((imk=mkdir(optionfilefiname))<0){ */
11136: /* perror("mkdir"); */
11137: /* } */
11138:
11139: /*-------- arguments in the command line --------*/
11140:
1.186 brouard 11141: /* Main Log file */
1.126 brouard 11142: strcat(filelog, optionfilefiname);
11143: strcat(filelog,".log"); /* */
11144: if((ficlog=fopen(filelog,"w"))==NULL) {
11145: printf("Problem with logfile %s\n",filelog);
11146: goto end;
11147: }
11148: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 11149: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 11150: fprintf(ficlog,"\nEnter the parameter file name: \n");
11151: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
11152: path=%s \n\
11153: optionfile=%s\n\
11154: optionfilext=%s\n\
1.156 brouard 11155: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 11156:
1.197 brouard 11157: syscompilerinfo(1);
1.167 brouard 11158:
1.126 brouard 11159: printf("Local time (at start):%s",strstart);
11160: fprintf(ficlog,"Local time (at start): %s",strstart);
11161: fflush(ficlog);
11162: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 11163: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 11164:
11165: /* */
11166: strcpy(fileres,"r");
11167: strcat(fileres, optionfilefiname);
1.201 brouard 11168: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 11169: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 11170: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 11171:
1.186 brouard 11172: /* Main ---------arguments file --------*/
1.126 brouard 11173:
11174: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 11175: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
11176: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 11177: fflush(ficlog);
1.149 brouard 11178: /* goto end; */
11179: exit(70);
1.126 brouard 11180: }
11181:
11182: strcpy(filereso,"o");
1.201 brouard 11183: strcat(filereso,fileresu);
1.126 brouard 11184: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
11185: printf("Problem with Output resultfile: %s\n", filereso);
11186: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
11187: fflush(ficlog);
11188: goto end;
11189: }
1.278 brouard 11190: /*-------- Rewriting parameter file ----------*/
11191: strcpy(rfileres,"r"); /* "Rparameterfile */
11192: strcat(rfileres,optionfilefiname); /* Parameter file first name */
11193: strcat(rfileres,"."); /* */
11194: strcat(rfileres,optionfilext); /* Other files have txt extension */
11195: if((ficres =fopen(rfileres,"w"))==NULL) {
11196: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
11197: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
11198: fflush(ficlog);
11199: goto end;
11200: }
11201: fprintf(ficres,"#IMaCh %s\n",version);
1.126 brouard 11202:
1.278 brouard 11203:
1.126 brouard 11204: /* Reads comments: lines beginning with '#' */
11205: numlinepar=0;
1.277 brouard 11206: /* Is it a BOM UTF-8 Windows file? */
11207: /* First parameter line */
1.197 brouard 11208: while(fgets(line, MAXLINE, ficpar)) {
1.277 brouard 11209: noffset=0;
11210: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */
11211: {
11212: noffset=noffset+3;
11213: printf("# File is an UTF8 Bom.\n"); // 0xBF
11214: }
1.302 brouard 11215: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/
11216: else if( line[0] == (char)0xFF && line[1] == (char)0xFE)
1.277 brouard 11217: {
11218: noffset=noffset+2;
11219: printf("# File is an UTF16BE BOM file\n");
11220: }
11221: else if( line[0] == 0 && line[1] == 0)
11222: {
11223: if( line[2] == (char)0xFE && line[3] == (char)0xFF){
11224: noffset=noffset+4;
11225: printf("# File is an UTF16BE BOM file\n");
11226: }
11227: } else{
11228: ;/*printf(" Not a BOM file\n");*/
11229: }
11230:
1.197 brouard 11231: /* If line starts with a # it is a comment */
1.277 brouard 11232: if (line[noffset] == '#') {
1.197 brouard 11233: numlinepar++;
11234: fputs(line,stdout);
11235: fputs(line,ficparo);
1.278 brouard 11236: fputs(line,ficres);
1.197 brouard 11237: fputs(line,ficlog);
11238: continue;
11239: }else
11240: break;
11241: }
11242: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
11243: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
11244: if (num_filled != 5) {
11245: printf("Should be 5 parameters\n");
1.283 brouard 11246: fprintf(ficlog,"Should be 5 parameters\n");
1.197 brouard 11247: }
1.126 brouard 11248: numlinepar++;
1.197 brouard 11249: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.283 brouard 11250: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11251: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
11252: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
1.197 brouard 11253: }
11254: /* Second parameter line */
11255: while(fgets(line, MAXLINE, ficpar)) {
1.283 brouard 11256: /* while(fscanf(ficpar,"%[^\n]", line)) { */
11257: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */
1.197 brouard 11258: if (line[0] == '#') {
11259: numlinepar++;
1.283 brouard 11260: printf("%s",line);
11261: fprintf(ficres,"%s",line);
11262: fprintf(ficparo,"%s",line);
11263: fprintf(ficlog,"%s",line);
1.197 brouard 11264: continue;
11265: }else
11266: break;
11267: }
1.223 brouard 11268: 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", \
11269: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
11270: if (num_filled != 11) {
11271: 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 11272: printf("but line=%s\n",line);
1.283 brouard 11273: 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");
11274: fprintf(ficlog,"but line=%s\n",line);
1.197 brouard 11275: }
1.286 brouard 11276: if( lastpass > maxwav){
11277: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11278: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav);
11279: fflush(ficlog);
11280: goto end;
11281: }
11282: 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 11283: 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 11284: 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 11285: 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 11286: }
1.203 brouard 11287: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 11288: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 11289: /* Third parameter line */
11290: while(fgets(line, MAXLINE, ficpar)) {
11291: /* If line starts with a # it is a comment */
11292: if (line[0] == '#') {
11293: numlinepar++;
1.283 brouard 11294: printf("%s",line);
11295: fprintf(ficres,"%s",line);
11296: fprintf(ficparo,"%s",line);
11297: fprintf(ficlog,"%s",line);
1.197 brouard 11298: continue;
11299: }else
11300: break;
11301: }
1.201 brouard 11302: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
1.279 brouard 11303: if (num_filled != 1){
1.302 brouard 11304: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
11305: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line);
1.197 brouard 11306: model[0]='\0';
11307: goto end;
11308: }
11309: else{
11310: if (model[0]=='+'){
11311: for(i=1; i<=strlen(model);i++)
11312: modeltemp[i-1]=model[i];
1.201 brouard 11313: strcpy(model,modeltemp);
1.197 brouard 11314: }
11315: }
1.199 brouard 11316: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 11317: printf("model=1+age+%s\n",model);fflush(stdout);
1.283 brouard 11318: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout);
11319: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout);
11320: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 11321: }
11322: /* 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); */
11323: /* numlinepar=numlinepar+3; /\* In general *\/ */
11324: /* 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 11325: /* 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); */
11326: /* 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 11327: fflush(ficlog);
1.190 brouard 11328: /* if(model[0]=='#'|| model[0]== '\0'){ */
11329: if(model[0]=='#'){
1.279 brouard 11330: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \
11331: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \
11332: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \
1.187 brouard 11333: if(mle != -1){
1.279 brouard 11334: 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 11335: exit(1);
11336: }
11337: }
1.126 brouard 11338: while((c=getc(ficpar))=='#' && c!= EOF){
11339: ungetc(c,ficpar);
11340: fgets(line, MAXLINE, ficpar);
11341: numlinepar++;
1.195 brouard 11342: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
11343: z[0]=line[1];
11344: }
11345: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 11346: fputs(line, stdout);
11347: //puts(line);
1.126 brouard 11348: fputs(line,ficparo);
11349: fputs(line,ficlog);
11350: }
11351: ungetc(c,ficpar);
11352:
11353:
1.290 brouard 11354: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */
11355: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */
11356: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */
11357: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /**< Time varying covariate (dummy and quantitative)*/
1.136 brouard 11358: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
11359: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
11360: v1+v2*age+v2*v3 makes cptcovn = 3
11361: */
11362: if (strlen(model)>1)
1.187 brouard 11363: 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 11364: else
1.187 brouard 11365: ncovmodel=2; /* Constant and age */
1.133 brouard 11366: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
11367: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 11368: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
11369: 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);
11370: 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);
11371: fflush(stdout);
11372: fclose (ficlog);
11373: goto end;
11374: }
1.126 brouard 11375: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11376: delti=delti3[1][1];
11377: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
11378: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
1.247 brouard 11379: /* We could also provide initial parameters values giving by simple logistic regression
11380: * only one way, that is without matrix product. We will have nlstate maximizations */
11381: /* for(i=1;i<nlstate;i++){ */
11382: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
11383: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
11384: /* } */
1.126 brouard 11385: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 11386: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
11387: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11388: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
11389: fclose (ficparo);
11390: fclose (ficlog);
11391: goto end;
11392: exit(0);
1.220 brouard 11393: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 11394: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 11395: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
11396: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 11397: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
11398: matcov=matrix(1,npar,1,npar);
1.203 brouard 11399: hess=matrix(1,npar,1,npar);
1.220 brouard 11400: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 11401: /* Read guessed parameters */
1.126 brouard 11402: /* Reads comments: lines beginning with '#' */
11403: while((c=getc(ficpar))=='#' && c!= EOF){
11404: ungetc(c,ficpar);
11405: fgets(line, MAXLINE, ficpar);
11406: numlinepar++;
1.141 brouard 11407: fputs(line,stdout);
1.126 brouard 11408: fputs(line,ficparo);
11409: fputs(line,ficlog);
11410: }
11411: ungetc(c,ficpar);
11412:
11413: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.251 brouard 11414: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
1.126 brouard 11415: for(i=1; i <=nlstate; i++){
1.234 brouard 11416: j=0;
1.126 brouard 11417: for(jj=1; jj <=nlstate+ndeath; jj++){
1.234 brouard 11418: if(jj==i) continue;
11419: j++;
1.292 brouard 11420: while((c=getc(ficpar))=='#' && c!= EOF){
11421: ungetc(c,ficpar);
11422: fgets(line, MAXLINE, ficpar);
11423: numlinepar++;
11424: fputs(line,stdout);
11425: fputs(line,ficparo);
11426: fputs(line,ficlog);
11427: }
11428: ungetc(c,ficpar);
1.234 brouard 11429: fscanf(ficpar,"%1d%1d",&i1,&j1);
11430: if ((i1 != i) || (j1 != jj)){
11431: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 11432: It might be a problem of design; if ncovcol and the model are correct\n \
11433: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.234 brouard 11434: exit(1);
11435: }
11436: fprintf(ficparo,"%1d%1d",i1,j1);
11437: if(mle==1)
11438: printf("%1d%1d",i,jj);
11439: fprintf(ficlog,"%1d%1d",i,jj);
11440: for(k=1; k<=ncovmodel;k++){
11441: fscanf(ficpar," %lf",¶m[i][j][k]);
11442: if(mle==1){
11443: printf(" %lf",param[i][j][k]);
11444: fprintf(ficlog," %lf",param[i][j][k]);
11445: }
11446: else
11447: fprintf(ficlog," %lf",param[i][j][k]);
11448: fprintf(ficparo," %lf",param[i][j][k]);
11449: }
11450: fscanf(ficpar,"\n");
11451: numlinepar++;
11452: if(mle==1)
11453: printf("\n");
11454: fprintf(ficlog,"\n");
11455: fprintf(ficparo,"\n");
1.126 brouard 11456: }
11457: }
11458: fflush(ficlog);
1.234 brouard 11459:
1.251 brouard 11460: /* Reads parameters values */
1.126 brouard 11461: p=param[1][1];
1.251 brouard 11462: pstart=paramstart[1][1];
1.126 brouard 11463:
11464: /* Reads comments: lines beginning with '#' */
11465: while((c=getc(ficpar))=='#' && c!= EOF){
11466: ungetc(c,ficpar);
11467: fgets(line, MAXLINE, ficpar);
11468: numlinepar++;
1.141 brouard 11469: fputs(line,stdout);
1.126 brouard 11470: fputs(line,ficparo);
11471: fputs(line,ficlog);
11472: }
11473: ungetc(c,ficpar);
11474:
11475: for(i=1; i <=nlstate; i++){
11476: for(j=1; j <=nlstate+ndeath-1; j++){
1.234 brouard 11477: fscanf(ficpar,"%1d%1d",&i1,&j1);
11478: if ( (i1-i) * (j1-j) != 0){
11479: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
11480: exit(1);
11481: }
11482: printf("%1d%1d",i,j);
11483: fprintf(ficparo,"%1d%1d",i1,j1);
11484: fprintf(ficlog,"%1d%1d",i1,j1);
11485: for(k=1; k<=ncovmodel;k++){
11486: fscanf(ficpar,"%le",&delti3[i][j][k]);
11487: printf(" %le",delti3[i][j][k]);
11488: fprintf(ficparo," %le",delti3[i][j][k]);
11489: fprintf(ficlog," %le",delti3[i][j][k]);
11490: }
11491: fscanf(ficpar,"\n");
11492: numlinepar++;
11493: printf("\n");
11494: fprintf(ficparo,"\n");
11495: fprintf(ficlog,"\n");
1.126 brouard 11496: }
11497: }
11498: fflush(ficlog);
1.234 brouard 11499:
1.145 brouard 11500: /* Reads covariance matrix */
1.126 brouard 11501: delti=delti3[1][1];
1.220 brouard 11502:
11503:
1.126 brouard 11504: /* 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 11505:
1.126 brouard 11506: /* Reads comments: lines beginning with '#' */
11507: while((c=getc(ficpar))=='#' && c!= EOF){
11508: ungetc(c,ficpar);
11509: fgets(line, MAXLINE, ficpar);
11510: numlinepar++;
1.141 brouard 11511: fputs(line,stdout);
1.126 brouard 11512: fputs(line,ficparo);
11513: fputs(line,ficlog);
11514: }
11515: ungetc(c,ficpar);
1.220 brouard 11516:
1.126 brouard 11517: matcov=matrix(1,npar,1,npar);
1.203 brouard 11518: hess=matrix(1,npar,1,npar);
1.131 brouard 11519: for(i=1; i <=npar; i++)
11520: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 11521:
1.194 brouard 11522: /* Scans npar lines */
1.126 brouard 11523: for(i=1; i <=npar; i++){
1.226 brouard 11524: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 11525: if(count != 3){
1.226 brouard 11526: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11527: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11528: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11529: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 11530: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
11531: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 brouard 11532: exit(1);
1.220 brouard 11533: }else{
1.226 brouard 11534: if(mle==1)
11535: printf("%1d%1d%d",i1,j1,jk);
11536: }
11537: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
11538: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 11539: for(j=1; j <=i; j++){
1.226 brouard 11540: fscanf(ficpar," %le",&matcov[i][j]);
11541: if(mle==1){
11542: printf(" %.5le",matcov[i][j]);
11543: }
11544: fprintf(ficlog," %.5le",matcov[i][j]);
11545: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 11546: }
11547: fscanf(ficpar,"\n");
11548: numlinepar++;
11549: if(mle==1)
1.220 brouard 11550: printf("\n");
1.126 brouard 11551: fprintf(ficlog,"\n");
11552: fprintf(ficparo,"\n");
11553: }
1.194 brouard 11554: /* End of read covariance matrix npar lines */
1.126 brouard 11555: for(i=1; i <=npar; i++)
11556: for(j=i+1;j<=npar;j++)
1.226 brouard 11557: matcov[i][j]=matcov[j][i];
1.126 brouard 11558:
11559: if(mle==1)
11560: printf("\n");
11561: fprintf(ficlog,"\n");
11562:
11563: fflush(ficlog);
11564:
11565: } /* End of mle != -3 */
1.218 brouard 11566:
1.186 brouard 11567: /* Main data
11568: */
1.290 brouard 11569: nobs=lastobs-firstobs+1; /* was = lastobs;*/
11570: /* num=lvector(1,n); */
11571: /* moisnais=vector(1,n); */
11572: /* annais=vector(1,n); */
11573: /* moisdc=vector(1,n); */
11574: /* andc=vector(1,n); */
11575: /* weight=vector(1,n); */
11576: /* agedc=vector(1,n); */
11577: /* cod=ivector(1,n); */
11578: /* for(i=1;i<=n;i++){ */
11579: num=lvector(firstobs,lastobs);
11580: moisnais=vector(firstobs,lastobs);
11581: annais=vector(firstobs,lastobs);
11582: moisdc=vector(firstobs,lastobs);
11583: andc=vector(firstobs,lastobs);
11584: weight=vector(firstobs,lastobs);
11585: agedc=vector(firstobs,lastobs);
11586: cod=ivector(firstobs,lastobs);
11587: for(i=firstobs;i<=lastobs;i++){
1.234 brouard 11588: num[i]=0;
11589: moisnais[i]=0;
11590: annais[i]=0;
11591: moisdc[i]=0;
11592: andc[i]=0;
11593: agedc[i]=0;
11594: cod[i]=0;
11595: weight[i]=1.0; /* Equal weights, 1 by default */
11596: }
1.290 brouard 11597: mint=matrix(1,maxwav,firstobs,lastobs);
11598: anint=matrix(1,maxwav,firstobs,lastobs);
11599: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */
1.126 brouard 11600: tab=ivector(1,NCOVMAX);
1.144 brouard 11601: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 11602: 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 11603:
1.136 brouard 11604: /* Reads data from file datafile */
11605: if (readdata(datafile, firstobs, lastobs, &imx)==1)
11606: goto end;
11607:
11608: /* Calculation of the number of parameters from char model */
1.234 brouard 11609: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
1.137 brouard 11610: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
11611: k=3 V4 Tvar[k=3]= 4 (from V4)
11612: k=2 V1 Tvar[k=2]= 1 (from V1)
11613: k=1 Tvar[1]=2 (from V2)
1.234 brouard 11614: */
11615:
11616: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
11617: TvarsDind=ivector(1,NCOVMAX); /* */
11618: TvarsD=ivector(1,NCOVMAX); /* */
11619: TvarsQind=ivector(1,NCOVMAX); /* */
11620: TvarsQ=ivector(1,NCOVMAX); /* */
1.232 brouard 11621: TvarF=ivector(1,NCOVMAX); /* */
11622: TvarFind=ivector(1,NCOVMAX); /* */
11623: TvarV=ivector(1,NCOVMAX); /* */
11624: TvarVind=ivector(1,NCOVMAX); /* */
11625: TvarA=ivector(1,NCOVMAX); /* */
11626: TvarAind=ivector(1,NCOVMAX); /* */
1.231 brouard 11627: TvarFD=ivector(1,NCOVMAX); /* */
11628: TvarFDind=ivector(1,NCOVMAX); /* */
11629: TvarFQ=ivector(1,NCOVMAX); /* */
11630: TvarFQind=ivector(1,NCOVMAX); /* */
11631: TvarVD=ivector(1,NCOVMAX); /* */
11632: TvarVDind=ivector(1,NCOVMAX); /* */
11633: TvarVQ=ivector(1,NCOVMAX); /* */
11634: TvarVQind=ivector(1,NCOVMAX); /* */
11635:
1.230 brouard 11636: Tvalsel=vector(1,NCOVMAX); /* */
1.233 brouard 11637: Tvarsel=ivector(1,NCOVMAX); /* */
1.226 brouard 11638: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
11639: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
11640: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 11641: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
11642: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
11643: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
11644: */
11645: /* For model-covariate k tells which data-covariate to use but
11646: because this model-covariate is a construction we invent a new column
11647: ncovcol + k1
11648: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
11649: Tvar[3=V1*V4]=4+1 etc */
1.227 brouard 11650: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */
11651: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */
1.137 brouard 11652: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
11653: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
1.227 brouard 11654: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2
1.137 brouard 11655: */
1.145 brouard 11656: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
11657: 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 11658: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
11659: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 11660: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 11661: 4 covariates (3 plus signs)
11662: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
11663: */
1.230 brouard 11664: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an
1.227 brouard 11665: * individual dummy, fixed or varying:
11666: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4,
11667: * 3, 1, 0, 0, 0, 0, 0, 0},
1.230 brouard 11668: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 ,
11669: * V1 df, V2 qf, V3 & V4 dv, V5 qv
11670: * Tmodelind[1]@9={9,0,3,2,}*/
11671: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/
11672: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an
1.228 brouard 11673: * individual quantitative, fixed or varying:
11674: * Tmodelqind[1]=1,Tvaraff[1]@9={4,
11675: * 3, 1, 0, 0, 0, 0, 0, 0},
11676: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/
1.186 brouard 11677: /* Main decodemodel */
11678:
1.187 brouard 11679:
1.223 brouard 11680: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 11681: goto end;
11682:
1.137 brouard 11683: if((double)(lastobs-imx)/(double)imx > 1.10){
11684: nbwarn++;
11685: 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);
11686: 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);
11687: }
1.136 brouard 11688: /* if(mle==1){*/
1.137 brouard 11689: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
11690: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 11691: }
11692:
11693: /*-calculation of age at interview from date of interview and age at death -*/
11694: agev=matrix(1,maxwav,1,imx);
11695:
11696: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
11697: goto end;
11698:
1.126 brouard 11699:
1.136 brouard 11700: agegomp=(int)agemin;
1.290 brouard 11701: free_vector(moisnais,firstobs,lastobs);
11702: free_vector(annais,firstobs,lastobs);
1.126 brouard 11703: /* free_matrix(mint,1,maxwav,1,n);
11704: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 11705: /* free_vector(moisdc,1,n); */
11706: /* free_vector(andc,1,n); */
1.145 brouard 11707: /* */
11708:
1.126 brouard 11709: wav=ivector(1,imx);
1.214 brouard 11710: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
11711: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
11712: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
11713: 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.*/
11714: bh=imatrix(1,lastpass-firstpass+2,1,imx);
11715: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 11716:
11717: /* Concatenates waves */
1.214 brouard 11718: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
11719: Death is a valid wave (if date is known).
11720: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
11721: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
11722: and mw[mi+1][i]. dh depends on stepm.
11723: */
11724:
1.126 brouard 11725: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.248 brouard 11726: /* Concatenates waves */
1.145 brouard 11727:
1.290 brouard 11728: free_vector(moisdc,firstobs,lastobs);
11729: free_vector(andc,firstobs,lastobs);
1.215 brouard 11730:
1.126 brouard 11731: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
11732: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
11733: ncodemax[1]=1;
1.145 brouard 11734: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 11735: cptcoveff=0;
1.220 brouard 11736: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
11737: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.227 brouard 11738: }
11739:
11740: ncovcombmax=pow(2,cptcoveff);
11741: invalidvarcomb=ivector(1, ncovcombmax);
11742: for(i=1;i<ncovcombmax;i++)
11743: invalidvarcomb[i]=0;
11744:
1.211 brouard 11745: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 11746: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 11747: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.227 brouard 11748:
1.200 brouard 11749: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 11750: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 11751: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 11752: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
11753: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
11754: * (currently 0 or 1) in the data.
11755: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
11756: * corresponding modality (h,j).
11757: */
11758:
1.145 brouard 11759: h=0;
11760: /*if (cptcovn > 0) */
1.126 brouard 11761: m=pow(2,cptcoveff);
11762:
1.144 brouard 11763: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 11764: * For k=4 covariates, h goes from 1 to m=2**k
11765: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
11766: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 11767: * h\k 1 2 3 4
1.143 brouard 11768: *______________________________
11769: * 1 i=1 1 i=1 1 i=1 1 i=1 1
11770: * 2 2 1 1 1
11771: * 3 i=2 1 2 1 1
11772: * 4 2 2 1 1
11773: * 5 i=3 1 i=2 1 2 1
11774: * 6 2 1 2 1
11775: * 7 i=4 1 2 2 1
11776: * 8 2 2 2 1
1.197 brouard 11777: * 9 i=5 1 i=3 1 i=2 1 2
11778: * 10 2 1 1 2
11779: * 11 i=6 1 2 1 2
11780: * 12 2 2 1 2
11781: * 13 i=7 1 i=4 1 2 2
11782: * 14 2 1 2 2
11783: * 15 i=8 1 2 2 2
11784: * 16 2 2 2 2
1.143 brouard 11785: */
1.212 brouard 11786: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 11787: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
11788: * and the value of each covariate?
11789: * V1=1, V2=1, V3=2, V4=1 ?
11790: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
11791: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
11792: * In order to get the real value in the data, we use nbcode
11793: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
11794: * We are keeping this crazy system in order to be able (in the future?)
11795: * to have more than 2 values (0 or 1) for a covariate.
11796: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
11797: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
11798: * bbbbbbbb
11799: * 76543210
11800: * h-1 00000101 (6-1=5)
1.219 brouard 11801: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 11802: * &
11803: * 1 00000001 (1)
1.219 brouard 11804: * 00000000 = 1 & ((h-1) >> (k-1))
11805: * +1= 00000001 =1
1.211 brouard 11806: *
11807: * h=14, k=3 => h'=h-1=13, k'=k-1=2
11808: * h' 1101 =2^3+2^2+0x2^1+2^0
11809: * >>k' 11
11810: * & 00000001
11811: * = 00000001
11812: * +1 = 00000010=2 = codtabm(14,3)
11813: * Reverse h=6 and m=16?
11814: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
11815: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
11816: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
11817: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
11818: * V3=decodtabm(14,3,2**4)=2
11819: * h'=13 1101 =2^3+2^2+0x2^1+2^0
11820: *(h-1) >> (j-1) 0011 =13 >> 2
11821: * &1 000000001
11822: * = 000000001
11823: * +1= 000000010 =2
11824: * 2211
11825: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
11826: * V3=2
1.220 brouard 11827: * codtabm and decodtabm are identical
1.211 brouard 11828: */
11829:
1.145 brouard 11830:
11831: free_ivector(Ndum,-1,NCOVMAX);
11832:
11833:
1.126 brouard 11834:
1.186 brouard 11835: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 11836: strcpy(optionfilegnuplot,optionfilefiname);
11837: if(mle==-3)
1.201 brouard 11838: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 11839: strcat(optionfilegnuplot,".gp");
11840:
11841: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
11842: printf("Problem with file %s",optionfilegnuplot);
11843: }
11844: else{
1.204 brouard 11845: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 11846: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 11847: //fprintf(ficgp,"set missing 'NaNq'\n");
11848: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 11849: }
11850: /* fclose(ficgp);*/
1.186 brouard 11851:
11852:
11853: /* Initialisation of --------- index.htm --------*/
1.126 brouard 11854:
11855: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
11856: if(mle==-3)
1.201 brouard 11857: strcat(optionfilehtm,"-MORT_");
1.126 brouard 11858: strcat(optionfilehtm,".htm");
11859: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 11860: printf("Problem with %s \n",optionfilehtm);
11861: exit(0);
1.126 brouard 11862: }
11863:
11864: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
11865: strcat(optionfilehtmcov,"-cov.htm");
11866: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
11867: printf("Problem with %s \n",optionfilehtmcov), exit(0);
11868: }
11869: else{
11870: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
11871: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 11872: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 11873: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
11874: }
11875:
1.213 brouard 11876: 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 11877: <hr size=\"2\" color=\"#EC5E5E\"> \n\
11878: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 11879: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 11880: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 11881: \n\
11882: <hr size=\"2\" color=\"#EC5E5E\">\
11883: <ul><li><h4>Parameter files</h4>\n\
11884: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
11885: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
11886: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
11887: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
11888: - Date and time at start: %s</ul>\n",\
11889: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
11890: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
11891: fileres,fileres,\
11892: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
11893: fflush(fichtm);
11894:
11895: strcpy(pathr,path);
11896: strcat(pathr,optionfilefiname);
1.184 brouard 11897: #ifdef WIN32
11898: _chdir(optionfilefiname); /* Move to directory named optionfile */
11899: #else
1.126 brouard 11900: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 11901: #endif
11902:
1.126 brouard 11903:
1.220 brouard 11904: /* Calculates basic frequencies. Computes observed prevalence at single age
11905: and for any valid combination of covariates
1.126 brouard 11906: and prints on file fileres'p'. */
1.251 brouard 11907: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.227 brouard 11908: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 11909:
11910: fprintf(fichtm,"\n");
1.286 brouard 11911: 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 11912: ftol, stepm);
11913: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol);
11914: ncurrv=1;
11915: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i);
11916: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv);
11917: ncurrv=i;
11918: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 11919: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv);
1.274 brouard 11920: ncurrv=i;
11921: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i);
1.290 brouard 11922: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv);
1.274 brouard 11923: ncurrv=i;
11924: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i);
11925: 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", \
11926: nlstate, ndeath, maxwav, mle, weightopt);
11927:
11928: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\
11929: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"));
11930:
11931:
11932: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Total number of observations=%d <br>\n\
1.126 brouard 11933: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
11934: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
1.274 brouard 11935: imx,agemin,agemax,jmin,jmax,jmean);
1.126 brouard 11936: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.268 brouard 11937: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11938: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11939: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
11940: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 11941:
1.126 brouard 11942: /* For Powell, parameters are in a vector p[] starting at p[1]
11943: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
11944: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
11945:
11946: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 11947: /* For mortality only */
1.126 brouard 11948: if (mle==-3){
1.136 brouard 11949: ximort=matrix(1,NDIM,1,NDIM);
1.248 brouard 11950: for(i=1;i<=NDIM;i++)
11951: for(j=1;j<=NDIM;j++)
11952: ximort[i][j]=0.;
1.186 brouard 11953: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.290 brouard 11954: cens=ivector(firstobs,lastobs);
11955: ageexmed=vector(firstobs,lastobs);
11956: agecens=vector(firstobs,lastobs);
11957: dcwave=ivector(firstobs,lastobs);
1.223 brouard 11958:
1.126 brouard 11959: for (i=1; i<=imx; i++){
11960: dcwave[i]=-1;
11961: for (m=firstpass; m<=lastpass; m++)
1.226 brouard 11962: if (s[m][i]>nlstate) {
11963: dcwave[i]=m;
11964: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
11965: break;
11966: }
1.126 brouard 11967: }
1.226 brouard 11968:
1.126 brouard 11969: for (i=1; i<=imx; i++) {
11970: if (wav[i]>0){
1.226 brouard 11971: ageexmed[i]=agev[mw[1][i]][i];
11972: j=wav[i];
11973: agecens[i]=1.;
11974:
11975: if (ageexmed[i]> 1 && wav[i] > 0){
11976: agecens[i]=agev[mw[j][i]][i];
11977: cens[i]= 1;
11978: }else if (ageexmed[i]< 1)
11979: cens[i]= -1;
11980: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
11981: cens[i]=0 ;
1.126 brouard 11982: }
11983: else cens[i]=-1;
11984: }
11985:
11986: for (i=1;i<=NDIM;i++) {
11987: for (j=1;j<=NDIM;j++)
1.226 brouard 11988: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 11989: }
11990:
1.302 brouard 11991: p[1]=0.0268; p[NDIM]=0.083;
11992: /* printf("%lf %lf", p[1], p[2]); */
1.126 brouard 11993:
11994:
1.136 brouard 11995: #ifdef GSL
11996: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 11997: #else
1.126 brouard 11998: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 11999: #endif
1.201 brouard 12000: strcpy(filerespow,"POW-MORT_");
12001: strcat(filerespow,fileresu);
1.126 brouard 12002: if((ficrespow=fopen(filerespow,"w"))==NULL) {
12003: printf("Problem with resultfile: %s\n", filerespow);
12004: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
12005: }
1.136 brouard 12006: #ifdef GSL
12007: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 12008: #else
1.126 brouard 12009: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 12010: #endif
1.126 brouard 12011: /* for (i=1;i<=nlstate;i++)
12012: for(j=1;j<=nlstate+ndeath;j++)
12013: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
12014: */
12015: fprintf(ficrespow,"\n");
1.136 brouard 12016: #ifdef GSL
12017: /* gsl starts here */
12018: T = gsl_multimin_fminimizer_nmsimplex;
12019: gsl_multimin_fminimizer *sfm = NULL;
12020: gsl_vector *ss, *x;
12021: gsl_multimin_function minex_func;
12022:
12023: /* Initial vertex size vector */
12024: ss = gsl_vector_alloc (NDIM);
12025:
12026: if (ss == NULL){
12027: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
12028: }
12029: /* Set all step sizes to 1 */
12030: gsl_vector_set_all (ss, 0.001);
12031:
12032: /* Starting point */
1.126 brouard 12033:
1.136 brouard 12034: x = gsl_vector_alloc (NDIM);
12035:
12036: if (x == NULL){
12037: gsl_vector_free(ss);
12038: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
12039: }
12040:
12041: /* Initialize method and iterate */
12042: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 12043: /* gsl_vector_set(x, 0, 0.0268); */
12044: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 12045: gsl_vector_set(x, 0, p[1]);
12046: gsl_vector_set(x, 1, p[2]);
12047:
12048: minex_func.f = &gompertz_f;
12049: minex_func.n = NDIM;
12050: minex_func.params = (void *)&p; /* ??? */
12051:
12052: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
12053: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
12054:
12055: printf("Iterations beginning .....\n\n");
12056: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
12057:
12058: iteri=0;
12059: while (rval == GSL_CONTINUE){
12060: iteri++;
12061: status = gsl_multimin_fminimizer_iterate(sfm);
12062:
12063: if (status) printf("error: %s\n", gsl_strerror (status));
12064: fflush(0);
12065:
12066: if (status)
12067: break;
12068:
12069: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
12070: ssval = gsl_multimin_fminimizer_size (sfm);
12071:
12072: if (rval == GSL_SUCCESS)
12073: printf ("converged to a local maximum at\n");
12074:
12075: printf("%5d ", iteri);
12076: for (it = 0; it < NDIM; it++){
12077: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
12078: }
12079: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
12080: }
12081:
12082: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
12083:
12084: gsl_vector_free(x); /* initial values */
12085: gsl_vector_free(ss); /* inital step size */
12086: for (it=0; it<NDIM; it++){
12087: p[it+1]=gsl_vector_get(sfm->x,it);
12088: fprintf(ficrespow," %.12lf", p[it]);
12089: }
12090: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
12091: #endif
12092: #ifdef POWELL
12093: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
12094: #endif
1.126 brouard 12095: fclose(ficrespow);
12096:
1.203 brouard 12097: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 12098:
12099: for(i=1; i <=NDIM; i++)
12100: for(j=i+1;j<=NDIM;j++)
1.220 brouard 12101: matcov[i][j]=matcov[j][i];
1.126 brouard 12102:
12103: printf("\nCovariance matrix\n ");
1.203 brouard 12104: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 12105: for(i=1; i <=NDIM; i++) {
12106: for(j=1;j<=NDIM;j++){
1.220 brouard 12107: printf("%f ",matcov[i][j]);
12108: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 12109: }
1.203 brouard 12110: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 12111: }
12112:
12113: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 12114: for (i=1;i<=NDIM;i++) {
1.126 brouard 12115: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 12116: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
12117: }
1.302 brouard 12118: lsurv=vector(agegomp,AGESUP);
12119: lpop=vector(agegomp,AGESUP);
12120: tpop=vector(agegomp,AGESUP);
1.126 brouard 12121: lsurv[agegomp]=100000;
12122:
12123: for (k=agegomp;k<=AGESUP;k++) {
12124: agemortsup=k;
12125: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
12126: }
12127:
12128: for (k=agegomp;k<agemortsup;k++)
12129: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
12130:
12131: for (k=agegomp;k<agemortsup;k++){
12132: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
12133: sumlpop=sumlpop+lpop[k];
12134: }
12135:
12136: tpop[agegomp]=sumlpop;
12137: for (k=agegomp;k<(agemortsup-3);k++){
12138: /* tpop[k+1]=2;*/
12139: tpop[k+1]=tpop[k]-lpop[k];
12140: }
12141:
12142:
12143: printf("\nAge lx qx dx Lx Tx e(x)\n");
12144: for (k=agegomp;k<(agemortsup-2);k++)
12145: 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]);
12146:
12147:
12148: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 12149: ageminpar=50;
12150: agemaxpar=100;
1.194 brouard 12151: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
12152: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12153: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12154: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
12155: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
12156: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12157: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 12158: }else{
12159: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
12160: 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 12161: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 12162: }
1.201 brouard 12163: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 12164: stepm, weightopt,\
12165: model,imx,p,matcov,agemortsup);
12166:
1.302 brouard 12167: free_vector(lsurv,agegomp,AGESUP);
12168: free_vector(lpop,agegomp,AGESUP);
12169: free_vector(tpop,agegomp,AGESUP);
1.220 brouard 12170: free_matrix(ximort,1,NDIM,1,NDIM);
1.290 brouard 12171: free_ivector(dcwave,firstobs,lastobs);
12172: free_vector(agecens,firstobs,lastobs);
12173: free_vector(ageexmed,firstobs,lastobs);
12174: free_ivector(cens,firstobs,lastobs);
1.220 brouard 12175: #ifdef GSL
1.136 brouard 12176: #endif
1.186 brouard 12177: } /* Endof if mle==-3 mortality only */
1.205 brouard 12178: /* Standard */
12179: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
12180: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12181: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 12182: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 12183: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12184: for (k=1; k<=npar;k++)
12185: printf(" %d %8.5f",k,p[k]);
12186: printf("\n");
1.205 brouard 12187: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
12188: /* mlikeli uses func not funcone */
1.247 brouard 12189: /* for(i=1;i<nlstate;i++){ */
12190: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */
12191: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */
12192: /* } */
1.205 brouard 12193: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
12194: }
12195: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
12196: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
12197: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
12198: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12199: }
12200: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 12201: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
12202: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
12203: for (k=1; k<=npar;k++)
12204: printf(" %d %8.5f",k,p[k]);
12205: printf("\n");
12206:
12207: /*--------- results files --------------*/
1.283 brouard 12208: /* 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 12209:
12210:
12211: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12212: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12213: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
12214: for(i=1,jk=1; i <=nlstate; i++){
12215: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 12216: if (k != i) {
12217: printf("%d%d ",i,k);
12218: fprintf(ficlog,"%d%d ",i,k);
12219: fprintf(ficres,"%1d%1d ",i,k);
12220: for(j=1; j <=ncovmodel; j++){
12221: printf("%12.7f ",p[jk]);
12222: fprintf(ficlog,"%12.7f ",p[jk]);
12223: fprintf(ficres,"%12.7f ",p[jk]);
12224: jk++;
12225: }
12226: printf("\n");
12227: fprintf(ficlog,"\n");
12228: fprintf(ficres,"\n");
12229: }
1.126 brouard 12230: }
12231: }
1.203 brouard 12232: if(mle != 0){
12233: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 12234: ftolhess=ftol; /* Usually correct */
1.203 brouard 12235: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
12236: 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");
12237: 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");
12238: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 12239: for(k=1; k <=(nlstate+ndeath); k++){
12240: if (k != i) {
12241: printf("%d%d ",i,k);
12242: fprintf(ficlog,"%d%d ",i,k);
12243: for(j=1; j <=ncovmodel; j++){
12244: 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]));
12245: 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]));
12246: jk++;
12247: }
12248: printf("\n");
12249: fprintf(ficlog,"\n");
12250: }
12251: }
1.193 brouard 12252: }
1.203 brouard 12253: } /* end of hesscov and Wald tests */
1.225 brouard 12254:
1.203 brouard 12255: /* */
1.126 brouard 12256: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
12257: printf("# Scales (for hessian or gradient estimation)\n");
12258: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
12259: for(i=1,jk=1; i <=nlstate; i++){
12260: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 12261: if (j!=i) {
12262: fprintf(ficres,"%1d%1d",i,j);
12263: printf("%1d%1d",i,j);
12264: fprintf(ficlog,"%1d%1d",i,j);
12265: for(k=1; k<=ncovmodel;k++){
12266: printf(" %.5e",delti[jk]);
12267: fprintf(ficlog," %.5e",delti[jk]);
12268: fprintf(ficres," %.5e",delti[jk]);
12269: jk++;
12270: }
12271: printf("\n");
12272: fprintf(ficlog,"\n");
12273: fprintf(ficres,"\n");
12274: }
1.126 brouard 12275: }
12276: }
12277:
12278: 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 12279: if(mle >= 1) /* To big for the screen */
1.126 brouard 12280: 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");
12281: 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");
12282: /* # 121 Var(a12)\n\ */
12283: /* # 122 Cov(b12,a12) Var(b12)\n\ */
12284: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
12285: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
12286: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
12287: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
12288: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
12289: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
12290:
12291:
12292: /* Just to have a covariance matrix which will be more understandable
12293: even is we still don't want to manage dictionary of variables
12294: */
12295: for(itimes=1;itimes<=2;itimes++){
12296: jj=0;
12297: for(i=1; i <=nlstate; i++){
1.225 brouard 12298: for(j=1; j <=nlstate+ndeath; j++){
12299: if(j==i) continue;
12300: for(k=1; k<=ncovmodel;k++){
12301: jj++;
12302: ca[0]= k+'a'-1;ca[1]='\0';
12303: if(itimes==1){
12304: if(mle>=1)
12305: printf("#%1d%1d%d",i,j,k);
12306: fprintf(ficlog,"#%1d%1d%d",i,j,k);
12307: fprintf(ficres,"#%1d%1d%d",i,j,k);
12308: }else{
12309: if(mle>=1)
12310: printf("%1d%1d%d",i,j,k);
12311: fprintf(ficlog,"%1d%1d%d",i,j,k);
12312: fprintf(ficres,"%1d%1d%d",i,j,k);
12313: }
12314: ll=0;
12315: for(li=1;li <=nlstate; li++){
12316: for(lj=1;lj <=nlstate+ndeath; lj++){
12317: if(lj==li) continue;
12318: for(lk=1;lk<=ncovmodel;lk++){
12319: ll++;
12320: if(ll<=jj){
12321: cb[0]= lk +'a'-1;cb[1]='\0';
12322: if(ll<jj){
12323: if(itimes==1){
12324: if(mle>=1)
12325: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12326: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12327: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
12328: }else{
12329: if(mle>=1)
12330: printf(" %.5e",matcov[jj][ll]);
12331: fprintf(ficlog," %.5e",matcov[jj][ll]);
12332: fprintf(ficres," %.5e",matcov[jj][ll]);
12333: }
12334: }else{
12335: if(itimes==1){
12336: if(mle>=1)
12337: printf(" Var(%s%1d%1d)",ca,i,j);
12338: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
12339: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
12340: }else{
12341: if(mle>=1)
12342: printf(" %.7e",matcov[jj][ll]);
12343: fprintf(ficlog," %.7e",matcov[jj][ll]);
12344: fprintf(ficres," %.7e",matcov[jj][ll]);
12345: }
12346: }
12347: }
12348: } /* end lk */
12349: } /* end lj */
12350: } /* end li */
12351: if(mle>=1)
12352: printf("\n");
12353: fprintf(ficlog,"\n");
12354: fprintf(ficres,"\n");
12355: numlinepar++;
12356: } /* end k*/
12357: } /*end j */
1.126 brouard 12358: } /* end i */
12359: } /* end itimes */
12360:
12361: fflush(ficlog);
12362: fflush(ficres);
1.225 brouard 12363: while(fgets(line, MAXLINE, ficpar)) {
12364: /* If line starts with a # it is a comment */
12365: if (line[0] == '#') {
12366: numlinepar++;
12367: fputs(line,stdout);
12368: fputs(line,ficparo);
12369: fputs(line,ficlog);
1.299 brouard 12370: fputs(line,ficres);
1.225 brouard 12371: continue;
12372: }else
12373: break;
12374: }
12375:
1.209 brouard 12376: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
12377: /* ungetc(c,ficpar); */
12378: /* fgets(line, MAXLINE, ficpar); */
12379: /* fputs(line,stdout); */
12380: /* fputs(line,ficparo); */
12381: /* } */
12382: /* ungetc(c,ficpar); */
1.126 brouard 12383:
12384: estepm=0;
1.209 brouard 12385: 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 12386:
12387: if (num_filled != 6) {
12388: 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);
12389: 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);
12390: goto end;
12391: }
12392: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
12393: }
12394: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
12395: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
12396:
1.209 brouard 12397: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 12398: if (estepm==0 || estepm < stepm) estepm=stepm;
12399: if (fage <= 2) {
12400: bage = ageminpar;
12401: fage = agemaxpar;
12402: }
12403:
12404: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 12405: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
12406: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 12407:
1.186 brouard 12408: /* Other stuffs, more or less useful */
1.254 brouard 12409: while(fgets(line, MAXLINE, ficpar)) {
12410: /* If line starts with a # it is a comment */
12411: if (line[0] == '#') {
12412: numlinepar++;
12413: fputs(line,stdout);
12414: fputs(line,ficparo);
12415: fputs(line,ficlog);
1.299 brouard 12416: fputs(line,ficres);
1.254 brouard 12417: continue;
12418: }else
12419: break;
12420: }
12421:
12422: 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){
12423:
12424: if (num_filled != 7) {
12425: 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);
12426: 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);
12427: goto end;
12428: }
12429: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
12430: 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);
12431: 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);
12432: 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 12433: }
1.254 brouard 12434:
12435: while(fgets(line, MAXLINE, ficpar)) {
12436: /* If line starts with a # it is a comment */
12437: if (line[0] == '#') {
12438: numlinepar++;
12439: fputs(line,stdout);
12440: fputs(line,ficparo);
12441: fputs(line,ficlog);
1.299 brouard 12442: fputs(line,ficres);
1.254 brouard 12443: continue;
12444: }else
12445: break;
1.126 brouard 12446: }
12447:
12448:
12449: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
12450: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
12451:
1.254 brouard 12452: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){
12453: if (num_filled != 1) {
12454: 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);
12455: 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);
12456: goto end;
12457: }
12458: printf("pop_based=%d\n",popbased);
12459: fprintf(ficlog,"pop_based=%d\n",popbased);
12460: fprintf(ficparo,"pop_based=%d\n",popbased);
12461: fprintf(ficres,"pop_based=%d\n",popbased);
12462: }
12463:
1.258 brouard 12464: /* Results */
1.307 brouard 12465: endishere=0;
1.258 brouard 12466: nresult=0;
1.308 brouard 12467: parameterline=0;
1.258 brouard 12468: do{
12469: if(!fgets(line, MAXLINE, ficpar)){
12470: endishere=1;
1.308 brouard 12471: parameterline=15;
1.258 brouard 12472: }else if (line[0] == '#') {
12473: /* If line starts with a # it is a comment */
1.254 brouard 12474: numlinepar++;
12475: fputs(line,stdout);
12476: fputs(line,ficparo);
12477: fputs(line,ficlog);
1.299 brouard 12478: fputs(line,ficres);
1.254 brouard 12479: continue;
1.258 brouard 12480: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp))
12481: parameterline=11;
1.296 brouard 12482: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp))
1.258 brouard 12483: parameterline=12;
1.307 brouard 12484: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){
1.258 brouard 12485: parameterline=13;
1.307 brouard 12486: }
1.258 brouard 12487: else{
12488: parameterline=14;
1.254 brouard 12489: }
1.308 brouard 12490: switch (parameterline){ /* =0 only if only comments */
1.258 brouard 12491: case 11:
1.296 brouard 12492: 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)){
12493: 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 12494: 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);
12495: 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);
12496: 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);
12497: /* day and month of proj2 are not used but only year anproj2.*/
1.273 brouard 12498: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.;
12499: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.;
1.296 brouard 12500: prvforecast = 1;
12501: }
12502: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/
1.302 brouard 12503: printf("prevforecast=%d yearsfproj=%lf.2 mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12504: fprintf(ficlog,"prevforecast=%d yearsfproj=%lf.2 mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
12505: fprintf(ficres,"prevforecast=%d yearsfproj=%lf.2 mobil_average=%d\n",prevfcast,yrfproj,mobilavproj);
1.296 brouard 12506: prvforecast = 2;
12507: }
12508: else {
12509: 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);
12510: 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);
12511: goto end;
1.258 brouard 12512: }
1.254 brouard 12513: break;
1.258 brouard 12514: case 12:
1.296 brouard 12515: 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)){
12516: 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);
12517: 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);
12518: 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);
12519: 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);
12520: /* day and month of back2 are not used but only year anback2.*/
1.273 brouard 12521: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.;
12522: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.;
1.296 brouard 12523: prvbackcast = 1;
12524: }
12525: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/
1.302 brouard 12526: printf("prevbackcast=%d yearsbproj=%lf.2 mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12527: fprintf(ficlog,"prevbackcast=%d yearsbproj=%lf.2 mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
12528: fprintf(ficres,"prevbackcast=%d yearsbproj=%lf.2 mobil_average=%d\n",prevbcast,yrbproj,mobilavproj);
1.296 brouard 12529: prvbackcast = 2;
12530: }
12531: else {
12532: 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);
12533: 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);
12534: goto end;
1.258 brouard 12535: }
1.230 brouard 12536: break;
1.258 brouard 12537: case 13:
1.307 brouard 12538: num_filled=sscanf(line,"result:%[^\n]\n",resultline);
12539: nresult++; /* Sum of resultlines */
12540: printf("Result %d: result:%s\n",nresult, resultline);
12541: if(nresult > MAXRESULTLINES){
12542: 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);
12543: 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);
12544: goto end;
12545: }
1.310 brouard 12546: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */
1.307 brouard 12547: fprintf(ficparo,"result: %s\n",resultline);
12548: fprintf(ficres,"result: %s\n",resultline);
12549: fprintf(ficlog,"result: %s\n",resultline);
1.310 brouard 12550: } else
12551: goto end;
1.307 brouard 12552: break;
12553: case 14:
12554: printf("Error: Unknown command '%s'\n",line);
12555: fprintf(ficlog,"Error: Unknown command '%s'\n",line);
12556: if(ncovmodel >=2 && nresult==0 ){
12557: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
12558: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line);
1.258 brouard 12559: }
1.307 brouard 12560: /* goto end; */
12561: break;
1.308 brouard 12562: case 15:
12563: printf("End of resultlines.\n");
12564: fprintf(ficlog,"End of resultlines.\n");
12565: break;
12566: default: /* parameterline =0 */
1.307 brouard 12567: nresult=1;
12568: decoderesult(".",nresult ); /* No covariate */
1.258 brouard 12569: } /* End switch parameterline */
12570: }while(endishere==0); /* End do */
1.126 brouard 12571:
1.230 brouard 12572: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 12573: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 12574:
12575: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 12576: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.230 brouard 12577: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 12578: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12579: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.230 brouard 12580: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 12581: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
12582: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 12583: }else{
1.270 brouard 12584: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */
1.296 brouard 12585: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */
12586: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */
12587: if(prvforecast==1){
12588: dateprojd=(jproj1+12*mproj1+365*anproj1)/365;
12589: jprojd=jproj1;
12590: mprojd=mproj1;
12591: anprojd=anproj1;
12592: dateprojf=(jproj2+12*mproj2+365*anproj2)/365;
12593: jprojf=jproj2;
12594: mprojf=mproj2;
12595: anprojf=anproj2;
12596: } else if(prvforecast == 2){
12597: dateprojd=dateintmean;
12598: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd);
12599: dateprojf=dateintmean+yrfproj;
12600: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf);
12601: }
12602: if(prvbackcast==1){
12603: datebackd=(jback1+12*mback1+365*anback1)/365;
12604: jbackd=jback1;
12605: mbackd=mback1;
12606: anbackd=anback1;
12607: datebackf=(jback2+12*mback2+365*anback2)/365;
12608: jbackf=jback2;
12609: mbackf=mback2;
12610: anbackf=anback2;
12611: } else if(prvbackcast == 2){
12612: datebackd=dateintmean;
12613: date2dmy(datebackd,&jbackd, &mbackd, &anbackd);
12614: datebackf=dateintmean-yrbproj;
12615: date2dmy(datebackf,&jbackf, &mbackf, &anbackf);
12616: }
12617:
12618: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage);
1.220 brouard 12619: }
12620: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.296 brouard 12621: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \
12622: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf);
1.220 brouard 12623:
1.225 brouard 12624: /*------------ free_vector -------------*/
12625: /* chdir(path); */
1.220 brouard 12626:
1.215 brouard 12627: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
12628: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
12629: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
12630: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.290 brouard 12631: free_lvector(num,firstobs,lastobs);
12632: free_vector(agedc,firstobs,lastobs);
1.126 brouard 12633: /*free_matrix(covar,0,NCOVMAX,1,n);*/
12634: /*free_matrix(covar,1,NCOVMAX,1,n);*/
12635: fclose(ficparo);
12636: fclose(ficres);
1.220 brouard 12637:
12638:
1.186 brouard 12639: /* Other results (useful)*/
1.220 brouard 12640:
12641:
1.126 brouard 12642: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 12643: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
12644: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 12645: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 12646: fclose(ficrespl);
12647:
12648: /*------------- h Pij x at various ages ------------*/
1.180 brouard 12649: /*#include "hpijx.h"*/
12650: hPijx(p, bage, fage);
1.145 brouard 12651: fclose(ficrespij);
1.227 brouard 12652:
1.220 brouard 12653: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 12654: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 12655: k=1;
1.126 brouard 12656: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
1.227 brouard 12657:
1.269 brouard 12658: /* Prevalence for each covariate combination in probs[age][status][cov] */
12659: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
12660: for(i=AGEINF;i<=AGESUP;i++)
1.219 brouard 12661: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 12662: for(k=1;k<=ncovcombmax;k++)
12663: probs[i][j][k]=0.;
1.269 brouard 12664: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode,
12665: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
1.219 brouard 12666: if (mobilav!=0 ||mobilavproj !=0 ) {
1.269 brouard 12667: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
12668: for(i=AGEINF;i<=AGESUP;i++)
1.268 brouard 12669: for(j=1;j<=nlstate+ndeath;j++)
1.227 brouard 12670: for(k=1;k<=ncovcombmax;k++)
12671: mobaverages[i][j][k]=0.;
1.219 brouard 12672: mobaverage=mobaverages;
12673: if (mobilav!=0) {
1.235 brouard 12674: printf("Movingaveraging observed prevalence\n");
1.258 brouard 12675: fprintf(ficlog,"Movingaveraging observed prevalence\n");
1.227 brouard 12676: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
12677: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
12678: printf(" Error in movingaverage mobilav=%d\n",mobilav);
12679: }
1.269 brouard 12680: } else if (mobilavproj !=0) {
1.235 brouard 12681: printf("Movingaveraging projected observed prevalence\n");
1.258 brouard 12682: fprintf(ficlog,"Movingaveraging projected observed prevalence\n");
1.227 brouard 12683: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
12684: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
12685: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
12686: }
1.269 brouard 12687: }else{
12688: printf("Internal error moving average\n");
12689: fflush(stdout);
12690: exit(1);
1.219 brouard 12691: }
12692: }/* end if moving average */
1.227 brouard 12693:
1.126 brouard 12694: /*---------- Forecasting ------------------*/
1.296 brouard 12695: if(prevfcast==1){
12696: /* /\* if(stepm ==1){*\/ */
12697: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
12698: /*This done previously after freqsummary.*/
12699: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */
12700: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */
12701:
12702: /* } else if (prvforecast==2){ */
12703: /* /\* if(stepm ==1){*\/ */
12704: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */
12705: /* } */
12706: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/
12707: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff);
1.126 brouard 12708: }
1.269 brouard 12709:
1.296 brouard 12710: /* Prevbcasting */
12711: if(prevbcast==1){
1.219 brouard 12712: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12713: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12714: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
12715:
12716: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
12717:
12718: bprlim=matrix(1,nlstate,1,nlstate);
1.269 brouard 12719:
1.219 brouard 12720: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
12721: fclose(ficresplb);
12722:
1.222 brouard 12723: hBijx(p, bage, fage, mobaverage);
12724: fclose(ficrespijb);
1.219 brouard 12725:
1.296 brouard 12726: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */
12727: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */
12728: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */
12729: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
12730: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2,
12731: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff);
12732:
12733:
1.269 brouard 12734: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 12735:
12736:
1.269 brouard 12737: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.219 brouard 12738: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
12739: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
12740: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
1.296 brouard 12741: } /* end Prevbcasting */
1.268 brouard 12742:
1.186 brouard 12743:
12744: /* ------ Other prevalence ratios------------ */
1.126 brouard 12745:
1.215 brouard 12746: free_ivector(wav,1,imx);
12747: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
12748: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
12749: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 12750:
12751:
1.127 brouard 12752: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 12753:
1.201 brouard 12754: strcpy(filerese,"E_");
12755: strcat(filerese,fileresu);
1.126 brouard 12756: if((ficreseij=fopen(filerese,"w"))==NULL) {
12757: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
12758: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
12759: }
1.208 brouard 12760: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
12761: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.238 brouard 12762:
12763: pstamp(ficreseij);
1.219 brouard 12764:
1.235 brouard 12765: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
12766: if (cptcovn < 1){i1=1;}
12767:
12768: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12769: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 12770: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 12771: continue;
1.219 brouard 12772: fprintf(ficreseij,"\n#****** ");
1.235 brouard 12773: printf("\n#****** ");
1.225 brouard 12774: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 12775: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 12776: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12777: }
12778: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12779: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12780: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
1.219 brouard 12781: }
12782: fprintf(ficreseij,"******\n");
1.235 brouard 12783: printf("******\n");
1.219 brouard 12784:
12785: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12786: oldm=oldms;savm=savms;
1.235 brouard 12787: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres);
1.127 brouard 12788:
1.219 brouard 12789: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 12790: }
12791: fclose(ficreseij);
1.208 brouard 12792: printf("done evsij\n");fflush(stdout);
12793: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.269 brouard 12794:
1.218 brouard 12795:
1.227 brouard 12796: /*---------- State-specific expectancies and variances ------------*/
1.218 brouard 12797:
1.201 brouard 12798: strcpy(filerest,"T_");
12799: strcat(filerest,fileresu);
1.127 brouard 12800: if((ficrest=fopen(filerest,"w"))==NULL) {
12801: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
12802: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
12803: }
1.208 brouard 12804: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
12805: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.201 brouard 12806: strcpy(fileresstde,"STDE_");
12807: strcat(fileresstde,fileresu);
1.126 brouard 12808: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
1.227 brouard 12809: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
12810: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
1.126 brouard 12811: }
1.227 brouard 12812: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
12813: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 12814:
1.201 brouard 12815: strcpy(filerescve,"CVE_");
12816: strcat(filerescve,fileresu);
1.126 brouard 12817: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
1.227 brouard 12818: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
12819: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0);
1.126 brouard 12820: }
1.227 brouard 12821: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
12822: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 12823:
1.201 brouard 12824: strcpy(fileresv,"V_");
12825: strcat(fileresv,fileresu);
1.126 brouard 12826: if((ficresvij=fopen(fileresv,"w"))==NULL) {
12827: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
12828: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
12829: }
1.227 brouard 12830: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout);
12831: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 12832:
1.235 brouard 12833: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */
12834: if (cptcovn < 1){i1=1;}
12835:
12836: for(nres=1; nres <= nresult; nres++) /* For each resultline */
12837: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */
1.253 brouard 12838: if(i1 != 1 && TKresult[nres]!= k)
1.235 brouard 12839: continue;
1.242 brouard 12840: printf("\n#****** Result for:");
12841: fprintf(ficrest,"\n#****** Result for:");
12842: fprintf(ficlog,"\n#****** Result for:");
1.227 brouard 12843: for(j=1;j<=cptcoveff;j++){
12844: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12845: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12846: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12847: }
1.235 brouard 12848: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12849: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12850: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12851: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12852: }
1.208 brouard 12853: fprintf(ficrest,"******\n");
1.227 brouard 12854: fprintf(ficlog,"******\n");
12855: printf("******\n");
1.208 brouard 12856:
12857: fprintf(ficresstdeij,"\n#****** ");
12858: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 12859: for(j=1;j<=cptcoveff;j++) {
1.227 brouard 12860: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
12861: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 12862: }
1.235 brouard 12863: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12864: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12865: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12866: }
1.208 brouard 12867: fprintf(ficresstdeij,"******\n");
12868: fprintf(ficrescveij,"******\n");
12869:
12870: fprintf(ficresvij,"\n#****** ");
1.238 brouard 12871: /* pstamp(ficresvij); */
1.225 brouard 12872: for(j=1;j<=cptcoveff;j++)
1.227 brouard 12873: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.235 brouard 12874: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */
12875: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]);
12876: }
1.208 brouard 12877: fprintf(ficresvij,"******\n");
12878:
12879: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12880: oldm=oldms;savm=savms;
1.235 brouard 12881: printf(" cvevsij ");
12882: fprintf(ficlog, " cvevsij ");
12883: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres);
1.208 brouard 12884: printf(" end cvevsij \n ");
12885: fprintf(ficlog, " end cvevsij \n ");
12886:
12887: /*
12888: */
12889: /* goto endfree; */
12890:
12891: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
12892: pstamp(ficrest);
12893:
1.269 brouard 12894: epj=vector(1,nlstate+1);
1.208 brouard 12895: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.227 brouard 12896: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
12897: cptcod= 0; /* To be deleted */
12898: printf("varevsij vpopbased=%d \n",vpopbased);
12899: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased);
1.235 brouard 12900: 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 12901: 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 ");
12902: if(vpopbased==1)
12903: 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);
12904: else
1.288 brouard 12905: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 12906: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
12907: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
12908: fprintf(ficrest,"\n");
12909: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
1.288 brouard 12910: printf("Computing age specific forward period (stable) prevalences in each health state \n");
12911: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n");
1.227 brouard 12912: for(age=bage; age <=fage ;age++){
1.235 brouard 12913: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */
1.227 brouard 12914: if (vpopbased==1) {
12915: if(mobilav ==0){
12916: for(i=1; i<=nlstate;i++)
12917: prlim[i][i]=probs[(int)age][i][k];
12918: }else{ /* mobilav */
12919: for(i=1; i<=nlstate;i++)
12920: prlim[i][i]=mobaverage[(int)age][i][k];
12921: }
12922: }
1.219 brouard 12923:
1.227 brouard 12924: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
12925: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
12926: /* printf(" age %4.0f ",age); */
12927: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
12928: for(i=1, epj[j]=0.;i <=nlstate;i++) {
12929: epj[j] += prlim[i][i]*eij[i][j][(int)age];
12930: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
12931: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
12932: }
12933: epj[nlstate+1] +=epj[j];
12934: }
12935: /* printf(" age %4.0f \n",age); */
1.219 brouard 12936:
1.227 brouard 12937: for(i=1, vepp=0.;i <=nlstate;i++)
12938: for(j=1;j <=nlstate;j++)
12939: vepp += vareij[i][j][(int)age];
12940: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
12941: for(j=1;j <=nlstate;j++){
12942: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
12943: }
12944: fprintf(ficrest,"\n");
12945: }
1.208 brouard 12946: } /* End vpopbased */
1.269 brouard 12947: free_vector(epj,1,nlstate+1);
1.208 brouard 12948: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
12949: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.235 brouard 12950: printf("done selection\n");fflush(stdout);
12951: fprintf(ficlog,"done selection\n");fflush(ficlog);
1.208 brouard 12952:
1.235 brouard 12953: } /* End k selection */
1.227 brouard 12954:
12955: printf("done State-specific expectancies\n");fflush(stdout);
12956: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog);
12957:
1.288 brouard 12958: /* variance-covariance of forward period prevalence*/
1.269 brouard 12959: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff);
1.268 brouard 12960:
1.227 brouard 12961:
1.290 brouard 12962: free_vector(weight,firstobs,lastobs);
1.227 brouard 12963: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.290 brouard 12964: free_imatrix(s,1,maxwav+1,firstobs,lastobs);
12965: free_matrix(anint,1,maxwav,firstobs,lastobs);
12966: free_matrix(mint,1,maxwav,firstobs,lastobs);
12967: free_ivector(cod,firstobs,lastobs);
1.227 brouard 12968: free_ivector(tab,1,NCOVMAX);
12969: fclose(ficresstdeij);
12970: fclose(ficrescveij);
12971: fclose(ficresvij);
12972: fclose(ficrest);
12973: fclose(ficpar);
12974:
12975:
1.126 brouard 12976: /*---------- End : free ----------------*/
1.219 brouard 12977: if (mobilav!=0 ||mobilavproj !=0)
1.269 brouard 12978: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
12979: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 12980: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
12981: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 12982: } /* mle==-3 arrives here for freeing */
1.227 brouard 12983: /* endfree:*/
12984: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
12985: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
12986: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.290 brouard 12987: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs);
12988: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs);
12989: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs);
12990: free_matrix(covar,0,NCOVMAX,firstobs,lastobs);
1.227 brouard 12991: free_matrix(matcov,1,npar,1,npar);
12992: free_matrix(hess,1,npar,1,npar);
12993: /*free_vector(delti,1,npar);*/
12994: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
12995: free_matrix(agev,1,maxwav,1,imx);
1.269 brouard 12996: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
1.227 brouard 12997: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
12998:
12999: free_ivector(ncodemax,1,NCOVMAX);
13000: free_ivector(ncodemaxwundef,1,NCOVMAX);
13001: free_ivector(Dummy,-1,NCOVMAX);
13002: free_ivector(Fixed,-1,NCOVMAX);
1.238 brouard 13003: free_ivector(DummyV,1,NCOVMAX);
13004: free_ivector(FixedV,1,NCOVMAX);
1.227 brouard 13005: free_ivector(Typevar,-1,NCOVMAX);
13006: free_ivector(Tvar,1,NCOVMAX);
1.234 brouard 13007: free_ivector(TvarsQ,1,NCOVMAX);
13008: free_ivector(TvarsQind,1,NCOVMAX);
13009: free_ivector(TvarsD,1,NCOVMAX);
13010: free_ivector(TvarsDind,1,NCOVMAX);
1.231 brouard 13011: free_ivector(TvarFD,1,NCOVMAX);
13012: free_ivector(TvarFDind,1,NCOVMAX);
1.232 brouard 13013: free_ivector(TvarF,1,NCOVMAX);
13014: free_ivector(TvarFind,1,NCOVMAX);
13015: free_ivector(TvarV,1,NCOVMAX);
13016: free_ivector(TvarVind,1,NCOVMAX);
13017: free_ivector(TvarA,1,NCOVMAX);
13018: free_ivector(TvarAind,1,NCOVMAX);
1.231 brouard 13019: free_ivector(TvarFQ,1,NCOVMAX);
13020: free_ivector(TvarFQind,1,NCOVMAX);
13021: free_ivector(TvarVD,1,NCOVMAX);
13022: free_ivector(TvarVDind,1,NCOVMAX);
13023: free_ivector(TvarVQ,1,NCOVMAX);
13024: free_ivector(TvarVQind,1,NCOVMAX);
1.230 brouard 13025: free_ivector(Tvarsel,1,NCOVMAX);
13026: free_vector(Tvalsel,1,NCOVMAX);
1.227 brouard 13027: free_ivector(Tposprod,1,NCOVMAX);
13028: free_ivector(Tprod,1,NCOVMAX);
13029: free_ivector(Tvaraff,1,NCOVMAX);
13030: free_ivector(invalidvarcomb,1,ncovcombmax);
13031: free_ivector(Tage,1,NCOVMAX);
13032: free_ivector(Tmodelind,1,NCOVMAX);
1.228 brouard 13033: free_ivector(TmodelInvind,1,NCOVMAX);
13034: free_ivector(TmodelInvQind,1,NCOVMAX);
1.227 brouard 13035:
13036: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
13037: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 13038: fflush(fichtm);
13039: fflush(ficgp);
13040:
1.227 brouard 13041:
1.126 brouard 13042: if((nberr >0) || (nbwarn>0)){
1.216 brouard 13043: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
13044: 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 13045: }else{
13046: printf("End of Imach\n");
13047: fprintf(ficlog,"End of Imach\n");
13048: }
13049: printf("See log file on %s\n",filelog);
13050: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 13051: /*(void) gettimeofday(&end_time,&tzp);*/
13052: rend_time = time(NULL);
13053: end_time = *localtime(&rend_time);
13054: /* tml = *localtime(&end_time.tm_sec); */
13055: strcpy(strtend,asctime(&end_time));
1.126 brouard 13056: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
13057: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 13058: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.227 brouard 13059:
1.157 brouard 13060: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
13061: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
13062: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 13063: /* printf("Total time was %d uSec.\n", total_usecs);*/
13064: /* if(fileappend(fichtm,optionfilehtm)){ */
13065: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13066: fclose(fichtm);
13067: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
13068: fclose(fichtmcov);
13069: fclose(ficgp);
13070: fclose(ficlog);
13071: /*------ End -----------*/
1.227 brouard 13072:
1.281 brouard 13073:
13074: /* Executes gnuplot */
1.227 brouard 13075:
13076: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 13077: #ifdef WIN32
1.227 brouard 13078: if (_chdir(pathcd) != 0)
13079: printf("Can't move to directory %s!\n",path);
13080: if(_getcwd(pathcd,MAXLINE) > 0)
1.184 brouard 13081: #else
1.227 brouard 13082: if(chdir(pathcd) != 0)
13083: printf("Can't move to directory %s!\n", path);
13084: if (getcwd(pathcd, MAXLINE) > 0)
1.184 brouard 13085: #endif
1.126 brouard 13086: printf("Current directory %s!\n",pathcd);
13087: /*strcat(plotcmd,CHARSEPARATOR);*/
13088: sprintf(plotcmd,"gnuplot");
1.157 brouard 13089: #ifdef _WIN32
1.126 brouard 13090: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
13091: #endif
13092: if(!stat(plotcmd,&info)){
1.158 brouard 13093: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13094: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 13095: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 13096: }else
13097: strcpy(pplotcmd,plotcmd);
1.157 brouard 13098: #ifdef __unix
1.126 brouard 13099: strcpy(plotcmd,GNUPLOTPROGRAM);
13100: if(!stat(plotcmd,&info)){
1.158 brouard 13101: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 13102: }else
13103: strcpy(pplotcmd,plotcmd);
13104: #endif
13105: }else
13106: strcpy(pplotcmd,plotcmd);
13107:
13108: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 13109: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.292 brouard 13110: strcpy(pplotcmd,plotcmd);
1.227 brouard 13111:
1.126 brouard 13112: if((outcmd=system(plotcmd)) != 0){
1.292 brouard 13113: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 13114: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 13115: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.292 brouard 13116: if((outcmd=system(plotcmd)) != 0){
1.153 brouard 13117: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.292 brouard 13118: strcpy(plotcmd,pplotcmd);
13119: }
1.126 brouard 13120: }
1.158 brouard 13121: printf(" Successful, please wait...");
1.126 brouard 13122: while (z[0] != 'q') {
13123: /* chdir(path); */
1.154 brouard 13124: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 13125: scanf("%s",z);
13126: /* if (z[0] == 'c') system("./imach"); */
13127: if (z[0] == 'e') {
1.158 brouard 13128: #ifdef __APPLE__
1.152 brouard 13129: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 13130: #elif __linux
13131: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 13132: #else
1.152 brouard 13133: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 13134: #endif
13135: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
13136: system(pplotcmd);
1.126 brouard 13137: }
13138: else if (z[0] == 'g') system(plotcmd);
13139: else if (z[0] == 'q') exit(0);
13140: }
1.227 brouard 13141: end:
1.126 brouard 13142: while (z[0] != 'q') {
1.195 brouard 13143: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 13144: scanf("%s",z);
13145: }
1.283 brouard 13146: printf("End\n");
1.282 brouard 13147: exit(0);
1.126 brouard 13148: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>