![]() ![]() | ![]() |
* imach.c (Module): Some minor improvements
1: /* $Id: imach.c,v 1.317 2022/05/15 15:06:23 brouard Exp $ 2: $State: Exp $ 3: $Log: imach.c,v $ 4: Revision 1.317 2022/05/15 15:06:23 brouard 5: * imach.c (Module): Some minor improvements 6: 7: Revision 1.316 2022/05/11 15:11:31 brouard 8: Summary: r27 9: 10: Revision 1.315 2022/05/11 15:06:32 brouard 11: *** empty log message *** 12: 13: Revision 1.314 2022/04/13 17:43:09 brouard 14: * imach.c (Module): Adding link to text data files 15: 16: Revision 1.313 2022/04/11 15:57:42 brouard 17: * imach.c (Module): Error in rewriting the 'r' file with yearsfproj or yearsbproj fixed 18: 19: Revision 1.312 2022/04/05 21:24:39 brouard 20: *** empty log message *** 21: 22: Revision 1.311 2022/04/05 21:03:51 brouard 23: Summary: Fixed quantitative covariates 24: 25: Fixed covariates (dummy or quantitative) 26: with missing values have never been allowed but are ERRORS and 27: program quits. Standard deviations of fixed covariates were 28: wrongly computed. Mean and standard deviations of time varying 29: covariates are still not computed. 30: 31: Revision 1.310 2022/03/17 08:45:53 brouard 32: Summary: 99r25 33: 34: Improving detection of errors: result lines should be compatible with 35: the model. 36: 37: Revision 1.309 2021/05/20 12:39:14 brouard 38: Summary: Version 0.99r24 39: 40: Revision 1.308 2021/03/31 13:11:57 brouard 41: Summary: Version 0.99r23 42: 43: 44: * imach.c (Module): Still bugs in the result loop. Thank to Holly Benett 45: 46: Revision 1.307 2021/03/08 18:11:32 brouard 47: Summary: 0.99r22 fixed bug on result: 48: 49: Revision 1.306 2021/02/20 15:44:02 brouard 50: Summary: Version 0.99r21 51: 52: * imach.c (Module): Fix bug on quitting after result lines! 53: (Module): Version 0.99r21 54: 55: Revision 1.305 2021/02/20 15:28:30 brouard 56: * imach.c (Module): Fix bug on quitting after result lines! 57: 58: Revision 1.304 2021/02/12 11:34:20 brouard 59: * imach.c (Module): The use of a Windows BOM (huge) file is now an error 60: 61: Revision 1.303 2021/02/11 19:50:15 brouard 62: * (Module): imach.c Someone entered 'results:' instead of 'result:'. Now it is an error which is printed. 63: 64: Revision 1.302 2020/02/22 21:00:05 brouard 65: * (Module): imach.c Update mle=-3 (for computing Life expectancy 66: and life table from the data without any state) 67: 68: Revision 1.301 2019/06/04 13:51:20 brouard 69: Summary: Error in 'r'parameter file backcast yearsbproj instead of yearsfproj 70: 71: Revision 1.300 2019/05/22 19:09:45 brouard 72: Summary: version 0.99r19 of May 2019 73: 74: Revision 1.299 2019/05/22 18:37:08 brouard 75: Summary: Cleaned 0.99r19 76: 77: Revision 1.298 2019/05/22 18:19:56 brouard 78: *** empty log message *** 79: 80: Revision 1.297 2019/05/22 17:56:10 brouard 81: Summary: Fix bug by moving date2dmy and nhstepm which gaefin=-1 82: 83: Revision 1.296 2019/05/20 13:03:18 brouard 84: Summary: Projection syntax simplified 85: 86: 87: We can now start projections, forward or backward, from the mean date 88: of inteviews up to or down to a number of years of projection: 89: prevforecast=1 yearsfproj=15.3 mobil_average=0 90: or 91: prevforecast=1 starting-proj-date=1/1/2007 final-proj-date=12/31/2017 mobil_average=0 92: or 93: prevbackcast=1 yearsbproj=12.3 mobil_average=1 94: or 95: prevbackcast=1 starting-back-date=1/10/1999 final-back-date=1/1/1985 mobil_average=1 96: 97: Revision 1.295 2019/05/18 09:52:50 brouard 98: Summary: doxygen tex bug 99: 100: Revision 1.294 2019/05/16 14:54:33 brouard 101: Summary: There was some wrong lines added 102: 103: Revision 1.293 2019/05/09 15:17:34 brouard 104: *** empty log message *** 105: 106: Revision 1.292 2019/05/09 14:17:20 brouard 107: Summary: Some updates 108: 109: Revision 1.291 2019/05/09 13:44:18 brouard 110: Summary: Before ncovmax 111: 112: Revision 1.290 2019/05/09 13:39:37 brouard 113: Summary: 0.99r18 unlimited number of individuals 114: 115: 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. 116: 117: Revision 1.289 2018/12/13 09:16:26 brouard 118: Summary: Bug for young ages (<-30) will be in r17 119: 120: Revision 1.288 2018/05/02 20:58:27 brouard 121: Summary: Some bugs fixed 122: 123: Revision 1.287 2018/05/01 17:57:25 brouard 124: Summary: Bug fixed by providing frequencies only for non missing covariates 125: 126: Revision 1.286 2018/04/27 14:27:04 brouard 127: Summary: some minor bugs 128: 129: Revision 1.285 2018/04/21 21:02:16 brouard 130: Summary: Some bugs fixed, valgrind tested 131: 132: Revision 1.284 2018/04/20 05:22:13 brouard 133: Summary: Computing mean and stdeviation of fixed quantitative variables 134: 135: Revision 1.283 2018/04/19 14:49:16 brouard 136: Summary: Some minor bugs fixed 137: 138: Revision 1.282 2018/02/27 22:50:02 brouard 139: *** empty log message *** 140: 141: Revision 1.281 2018/02/27 19:25:23 brouard 142: Summary: Adding second argument for quitting 143: 144: Revision 1.280 2018/02/21 07:58:13 brouard 145: Summary: 0.99r15 146: 147: New Makefile with recent VirtualBox 5.26. Bug in sqrt negatve in imach.c 148: 149: Revision 1.279 2017/07/20 13:35:01 brouard 150: Summary: temporary working 151: 152: Revision 1.278 2017/07/19 14:09:02 brouard 153: Summary: Bug for mobil_average=0 and prevforecast fixed(?) 154: 155: Revision 1.277 2017/07/17 08:53:49 brouard 156: Summary: BOM files can be read now 157: 158: Revision 1.276 2017/06/30 15:48:31 brouard 159: Summary: Graphs improvements 160: 161: Revision 1.275 2017/06/30 13:39:33 brouard 162: Summary: Saito's color 163: 164: Revision 1.274 2017/06/29 09:47:08 brouard 165: Summary: Version 0.99r14 166: 167: Revision 1.273 2017/06/27 11:06:02 brouard 168: Summary: More documentation on projections 169: 170: Revision 1.272 2017/06/27 10:22:40 brouard 171: Summary: Color of backprojection changed from 6 to 5(yellow) 172: 173: Revision 1.271 2017/06/27 10:17:50 brouard 174: Summary: Some bug with rint 175: 176: Revision 1.270 2017/05/24 05:45:29 brouard 177: *** empty log message *** 178: 179: Revision 1.269 2017/05/23 08:39:25 brouard 180: Summary: Code into subroutine, cleanings 181: 182: Revision 1.268 2017/05/18 20:09:32 brouard 183: Summary: backprojection and confidence intervals of backprevalence 184: 185: Revision 1.267 2017/05/13 10:25:05 brouard 186: Summary: temporary save for backprojection 187: 188: Revision 1.266 2017/05/13 07:26:12 brouard 189: Summary: Version 0.99r13 (improvements and bugs fixed) 190: 191: Revision 1.265 2017/04/26 16:22:11 brouard 192: Summary: imach 0.99r13 Some bugs fixed 193: 194: Revision 1.264 2017/04/26 06:01:29 brouard 195: Summary: Labels in graphs 196: 197: Revision 1.263 2017/04/24 15:23:15 brouard 198: Summary: to save 199: 200: Revision 1.262 2017/04/18 16:48:12 brouard 201: *** empty log message *** 202: 203: Revision 1.261 2017/04/05 10:14:09 brouard 204: Summary: Bug in E_ as well as in T_ fixed nres-1 vs k1-1 205: 206: Revision 1.260 2017/04/04 17:46:59 brouard 207: Summary: Gnuplot indexations fixed (humm) 208: 209: Revision 1.259 2017/04/04 13:01:16 brouard 210: Summary: Some errors to warnings only if date of death is unknown but status is death we could set to pi3 211: 212: Revision 1.258 2017/04/03 10:17:47 brouard 213: Summary: Version 0.99r12 214: 215: Some cleanings, conformed with updated documentation. 216: 217: Revision 1.257 2017/03/29 16:53:30 brouard 218: Summary: Temp 219: 220: Revision 1.256 2017/03/27 05:50:23 brouard 221: Summary: Temporary 222: 223: Revision 1.255 2017/03/08 16:02:28 brouard 224: Summary: IMaCh version 0.99r10 bugs in gnuplot fixed 225: 226: Revision 1.254 2017/03/08 07:13:00 brouard 227: Summary: Fixing data parameter line 228: 229: Revision 1.253 2016/12/15 11:59:41 brouard 230: Summary: 0.99 in progress 231: 232: Revision 1.252 2016/09/15 21:15:37 brouard 233: *** empty log message *** 234: 235: Revision 1.251 2016/09/15 15:01:13 brouard 236: Summary: not working 237: 238: Revision 1.250 2016/09/08 16:07:27 brouard 239: Summary: continue 240: 241: Revision 1.249 2016/09/07 17:14:18 brouard 242: Summary: Starting values from frequencies 243: 244: Revision 1.248 2016/09/07 14:10:18 brouard 245: *** empty log message *** 246: 247: Revision 1.247 2016/09/02 11:11:21 brouard 248: *** empty log message *** 249: 250: Revision 1.246 2016/09/02 08:49:22 brouard 251: *** empty log message *** 252: 253: Revision 1.245 2016/09/02 07:25:01 brouard 254: *** empty log message *** 255: 256: Revision 1.244 2016/09/02 07:17:34 brouard 257: *** empty log message *** 258: 259: Revision 1.243 2016/09/02 06:45:35 brouard 260: *** empty log message *** 261: 262: Revision 1.242 2016/08/30 15:01:20 brouard 263: Summary: Fixing a lots 264: 265: Revision 1.241 2016/08/29 17:17:25 brouard 266: Summary: gnuplot problem in Back projection to fix 267: 268: Revision 1.240 2016/08/29 07:53:18 brouard 269: Summary: Better 270: 271: Revision 1.239 2016/08/26 15:51:03 brouard 272: Summary: Improvement in Powell output in order to copy and paste 273: 274: Author: 275: 276: Revision 1.238 2016/08/26 14:23:35 brouard 277: Summary: Starting tests of 0.99 278: 279: Revision 1.237 2016/08/26 09:20:19 brouard 280: Summary: to valgrind 281: 282: Revision 1.236 2016/08/25 10:50:18 brouard 283: *** empty log message *** 284: 285: Revision 1.235 2016/08/25 06:59:23 brouard 286: *** empty log message *** 287: 288: Revision 1.234 2016/08/23 16:51:20 brouard 289: *** empty log message *** 290: 291: Revision 1.233 2016/08/23 07:40:50 brouard 292: Summary: not working 293: 294: Revision 1.232 2016/08/22 14:20:21 brouard 295: Summary: not working 296: 297: Revision 1.231 2016/08/22 07:17:15 brouard 298: Summary: not working 299: 300: Revision 1.230 2016/08/22 06:55:53 brouard 301: Summary: Not working 302: 303: Revision 1.229 2016/07/23 09:45:53 brouard 304: Summary: Completing for func too 305: 306: Revision 1.228 2016/07/22 17:45:30 brouard 307: Summary: Fixing some arrays, still debugging 308: 309: Revision 1.226 2016/07/12 18:42:34 brouard 310: Summary: temp 311: 312: Revision 1.225 2016/07/12 08:40:03 brouard 313: Summary: saving but not running 314: 315: Revision 1.224 2016/07/01 13:16:01 brouard 316: Summary: Fixes 317: 318: Revision 1.223 2016/02/19 09:23:35 brouard 319: Summary: temporary 320: 321: Revision 1.222 2016/02/17 08:14:50 brouard 322: Summary: Probably last 0.98 stable version 0.98r6 323: 324: Revision 1.221 2016/02/15 23:35:36 brouard 325: Summary: minor bug 326: 327: Revision 1.219 2016/02/15 00:48:12 brouard 328: *** empty log message *** 329: 330: Revision 1.218 2016/02/12 11:29:23 brouard 331: Summary: 0.99 Back projections 332: 333: Revision 1.217 2015/12/23 17:18:31 brouard 334: Summary: Experimental backcast 335: 336: Revision 1.216 2015/12/18 17:32:11 brouard 337: Summary: 0.98r4 Warning and status=-2 338: 339: Version 0.98r4 is now: 340: - displaying an error when status is -1, date of interview unknown and date of death known; 341: - permitting a status -2 when the vital status is unknown at a known date of right truncation. 342: Older changes concerning s=-2, dating from 2005 have been supersed. 343: 344: Revision 1.215 2015/12/16 08:52:24 brouard 345: Summary: 0.98r4 working 346: 347: Revision 1.214 2015/12/16 06:57:54 brouard 348: Summary: temporary not working 349: 350: Revision 1.213 2015/12/11 18:22:17 brouard 351: Summary: 0.98r4 352: 353: Revision 1.212 2015/11/21 12:47:24 brouard 354: Summary: minor typo 355: 356: Revision 1.211 2015/11/21 12:41:11 brouard 357: Summary: 0.98r3 with some graph of projected cross-sectional 358: 359: Author: Nicolas Brouard 360: 361: Revision 1.210 2015/11/18 17:41:20 brouard 362: Summary: Start working on projected prevalences Revision 1.209 2015/11/17 22:12:03 brouard 363: Summary: Adding ftolpl parameter 364: Author: N Brouard 365: 366: We had difficulties to get smoothed confidence intervals. It was due 367: to the period prevalence which wasn't computed accurately. The inner 368: parameter ftolpl is now an outer parameter of the .imach parameter 369: file after estepm. If ftolpl is small 1.e-4 and estepm too, 370: computation are long. 371: 372: Revision 1.208 2015/11/17 14:31:57 brouard 373: Summary: temporary 374: 375: Revision 1.207 2015/10/27 17:36:57 brouard 376: *** empty log message *** 377: 378: Revision 1.206 2015/10/24 07:14:11 brouard 379: *** empty log message *** 380: 381: Revision 1.205 2015/10/23 15:50:53 brouard 382: Summary: 0.98r3 some clarification for graphs on likelihood contributions 383: 384: Revision 1.204 2015/10/01 16:20:26 brouard 385: Summary: Some new graphs of contribution to likelihood 386: 387: Revision 1.203 2015/09/30 17:45:14 brouard 388: Summary: looking at better estimation of the hessian 389: 390: Also a better criteria for convergence to the period prevalence And 391: therefore adding the number of years needed to converge. (The 392: prevalence in any alive state shold sum to one 393: 394: Revision 1.202 2015/09/22 19:45:16 brouard 395: Summary: Adding some overall graph on contribution to likelihood. Might change 396: 397: Revision 1.201 2015/09/15 17:34:58 brouard 398: Summary: 0.98r0 399: 400: - Some new graphs like suvival functions 401: - Some bugs fixed like model=1+age+V2. 402: 403: Revision 1.200 2015/09/09 16:53:55 brouard 404: Summary: Big bug thanks to Flavia 405: 406: Even model=1+age+V2. did not work anymore 407: 408: Revision 1.199 2015/09/07 14:09:23 brouard 409: Summary: 0.98q6 changing default small png format for graph to vectorized svg. 410: 411: Revision 1.198 2015/09/03 07:14:39 brouard 412: Summary: 0.98q5 Flavia 413: 414: Revision 1.197 2015/09/01 18:24:39 brouard 415: *** empty log message *** 416: 417: Revision 1.196 2015/08/18 23:17:52 brouard 418: Summary: 0.98q5 419: 420: Revision 1.195 2015/08/18 16:28:39 brouard 421: Summary: Adding a hack for testing purpose 422: 423: After reading the title, ftol and model lines, if the comment line has 424: a q, starting with #q, the answer at the end of the run is quit. It 425: permits to run test files in batch with ctest. The former workaround was 426: $ echo q | imach foo.imach 427: 428: Revision 1.194 2015/08/18 13:32:00 brouard 429: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line. 430: 431: Revision 1.193 2015/08/04 07:17:42 brouard 432: Summary: 0.98q4 433: 434: Revision 1.192 2015/07/16 16:49:02 brouard 435: Summary: Fixing some outputs 436: 437: Revision 1.191 2015/07/14 10:00:33 brouard 438: Summary: Some fixes 439: 440: Revision 1.190 2015/05/05 08:51:13 brouard 441: Summary: Adding digits in output parameters (7 digits instead of 6) 442: 443: Fix 1+age+. 444: 445: Revision 1.189 2015/04/30 14:45:16 brouard 446: Summary: 0.98q2 447: 448: Revision 1.188 2015/04/30 08:27:53 brouard 449: *** empty log message *** 450: 451: Revision 1.187 2015/04/29 09:11:15 brouard 452: *** empty log message *** 453: 454: Revision 1.186 2015/04/23 12:01:52 brouard 455: Summary: V1*age is working now, version 0.98q1 456: 457: Some codes had been disabled in order to simplify and Vn*age was 458: working in the optimization phase, ie, giving correct MLE parameters, 459: but, as usual, outputs were not correct and program core dumped. 460: 461: Revision 1.185 2015/03/11 13:26:42 brouard 462: Summary: Inclusion of compile and links command line for Intel Compiler 463: 464: Revision 1.184 2015/03/11 11:52:39 brouard 465: Summary: Back from Windows 8. Intel Compiler 466: 467: Revision 1.183 2015/03/10 20:34:32 brouard 468: Summary: 0.98q0, trying with directest, mnbrak fixed 469: 470: We use directest instead of original Powell test; probably no 471: incidence on the results, but better justifications; 472: We fixed Numerical Recipes mnbrak routine which was wrong and gave 473: wrong results. 474: 475: Revision 1.182 2015/02/12 08:19:57 brouard 476: Summary: Trying to keep directest which seems simpler and more general 477: Author: Nicolas Brouard 478: 479: Revision 1.181 2015/02/11 23:22:24 brouard 480: Summary: Comments on Powell added 481: 482: Author: 483: 484: Revision 1.180 2015/02/11 17:33:45 brouard 485: Summary: Finishing move from main to function (hpijx and prevalence_limit) 486: 487: Revision 1.179 2015/01/04 09:57:06 brouard 488: Summary: back to OS/X 489: 490: Revision 1.178 2015/01/04 09:35:48 brouard 491: *** empty log message *** 492: 493: Revision 1.177 2015/01/03 18:40:56 brouard 494: Summary: Still testing ilc32 on OSX 495: 496: Revision 1.176 2015/01/03 16:45:04 brouard 497: *** empty log message *** 498: 499: Revision 1.175 2015/01/03 16:33:42 brouard 500: *** empty log message *** 501: 502: Revision 1.174 2015/01/03 16:15:49 brouard 503: Summary: Still in cross-compilation 504: 505: Revision 1.173 2015/01/03 12:06:26 brouard 506: Summary: trying to detect cross-compilation 507: 508: Revision 1.172 2014/12/27 12:07:47 brouard 509: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP 510: 511: Revision 1.171 2014/12/23 13:26:59 brouard 512: Summary: Back from Visual C 513: 514: Still problem with utsname.h on Windows 515: 516: Revision 1.170 2014/12/23 11:17:12 brouard 517: Summary: Cleaning some \%% back to %% 518: 519: The escape was mandatory for a specific compiler (which one?), but too many warnings. 520: 521: Revision 1.169 2014/12/22 23:08:31 brouard 522: Summary: 0.98p 523: 524: Outputs some informations on compiler used, OS etc. Testing on different platforms. 525: 526: Revision 1.168 2014/12/22 15:17:42 brouard 527: Summary: update 528: 529: Revision 1.167 2014/12/22 13:50:56 brouard 530: Summary: Testing uname and compiler version and if compiled 32 or 64 531: 532: Testing on Linux 64 533: 534: Revision 1.166 2014/12/22 11:40:47 brouard 535: *** empty log message *** 536: 537: Revision 1.165 2014/12/16 11:20:36 brouard 538: Summary: After compiling on Visual C 539: 540: * imach.c (Module): Merging 1.61 to 1.162 541: 542: Revision 1.164 2014/12/16 10:52:11 brouard 543: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn 544: 545: * imach.c (Module): Merging 1.61 to 1.162 546: 547: Revision 1.163 2014/12/16 10:30:11 brouard 548: * imach.c (Module): Merging 1.61 to 1.162 549: 550: Revision 1.162 2014/09/25 11:43:39 brouard 551: Summary: temporary backup 0.99! 552: 553: Revision 1.1 2014/09/16 11:06:58 brouard 554: Summary: With some code (wrong) for nlopt 555: 556: Author: 557: 558: Revision 1.161 2014/09/15 20:41:41 brouard 559: Summary: Problem with macro SQR on Intel compiler 560: 561: Revision 1.160 2014/09/02 09:24:05 brouard 562: *** empty log message *** 563: 564: Revision 1.159 2014/09/01 10:34:10 brouard 565: Summary: WIN32 566: Author: Brouard 567: 568: Revision 1.158 2014/08/27 17:11:51 brouard 569: *** empty log message *** 570: 571: Revision 1.157 2014/08/27 16:26:55 brouard 572: Summary: Preparing windows Visual studio version 573: Author: Brouard 574: 575: In order to compile on Visual studio, time.h is now correct and time_t 576: and tm struct should be used. difftime should be used but sometimes I 577: just make the differences in raw time format (time(&now). 578: Trying to suppress #ifdef LINUX 579: Add xdg-open for __linux in order to open default browser. 580: 581: Revision 1.156 2014/08/25 20:10:10 brouard 582: *** empty log message *** 583: 584: Revision 1.155 2014/08/25 18:32:34 brouard 585: Summary: New compile, minor changes 586: Author: Brouard 587: 588: Revision 1.154 2014/06/20 17:32:08 brouard 589: Summary: Outputs now all graphs of convergence to period prevalence 590: 591: Revision 1.153 2014/06/20 16:45:46 brouard 592: Summary: If 3 live state, convergence to period prevalence on same graph 593: Author: Brouard 594: 595: Revision 1.152 2014/06/18 17:54:09 brouard 596: Summary: open browser, use gnuplot on same dir than imach if not found in the path 597: 598: Revision 1.151 2014/06/18 16:43:30 brouard 599: *** empty log message *** 600: 601: Revision 1.150 2014/06/18 16:42:35 brouard 602: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX) 603: Author: brouard 604: 605: Revision 1.149 2014/06/18 15:51:14 brouard 606: Summary: Some fixes in parameter files errors 607: Author: Nicolas Brouard 608: 609: Revision 1.148 2014/06/17 17:38:48 brouard 610: Summary: Nothing new 611: Author: Brouard 612: 613: Just a new packaging for OS/X version 0.98nS 614: 615: Revision 1.147 2014/06/16 10:33:11 brouard 616: *** empty log message *** 617: 618: Revision 1.146 2014/06/16 10:20:28 brouard 619: Summary: Merge 620: Author: Brouard 621: 622: Merge, before building revised version. 623: 624: Revision 1.145 2014/06/10 21:23:15 brouard 625: Summary: Debugging with valgrind 626: Author: Nicolas Brouard 627: 628: Lot of changes in order to output the results with some covariates 629: After the Edimburgh REVES conference 2014, it seems mandatory to 630: improve the code. 631: No more memory valgrind error but a lot has to be done in order to 632: continue the work of splitting the code into subroutines. 633: Also, decodemodel has been improved. Tricode is still not 634: optimal. nbcode should be improved. Documentation has been added in 635: the source code. 636: 637: Revision 1.143 2014/01/26 09:45:38 brouard 638: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising 639: 640: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested... 641: (Module): Version 0.98nR Running ok, but output format still only works for three covariates. 642: 643: Revision 1.142 2014/01/26 03:57:36 brouard 644: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2 645: 646: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested... 647: 648: Revision 1.141 2014/01/26 02:42:01 brouard 649: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested... 650: 651: Revision 1.140 2011/09/02 10:37:54 brouard 652: Summary: times.h is ok with mingw32 now. 653: 654: Revision 1.139 2010/06/14 07:50:17 brouard 655: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree. 656: I remember having already fixed agemin agemax which are pointers now but not cvs saved. 657: 658: Revision 1.138 2010/04/30 18:19:40 brouard 659: *** empty log message *** 660: 661: Revision 1.137 2010/04/29 18:11:38 brouard 662: (Module): Checking covariates for more complex models 663: than V1+V2. A lot of change to be done. Unstable. 664: 665: Revision 1.136 2010/04/26 20:30:53 brouard 666: (Module): merging some libgsl code. Fixing computation 667: of likelione (using inter/intrapolation if mle = 0) in order to 668: get same likelihood as if mle=1. 669: Some cleaning of code and comments added. 670: 671: Revision 1.135 2009/10/29 15:33:14 brouard 672: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code. 673: 674: Revision 1.134 2009/10/29 13:18:53 brouard 675: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code. 676: 677: Revision 1.133 2009/07/06 10:21:25 brouard 678: just nforces 679: 680: Revision 1.132 2009/07/06 08:22:05 brouard 681: Many tings 682: 683: Revision 1.131 2009/06/20 16:22:47 brouard 684: Some dimensions resccaled 685: 686: Revision 1.130 2009/05/26 06:44:34 brouard 687: (Module): Max Covariate is now set to 20 instead of 8. A 688: lot of cleaning with variables initialized to 0. Trying to make 689: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better. 690: 691: Revision 1.129 2007/08/31 13:49:27 lievre 692: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting 693: 694: Revision 1.128 2006/06/30 13:02:05 brouard 695: (Module): Clarifications on computing e.j 696: 697: Revision 1.127 2006/04/28 18:11:50 brouard 698: (Module): Yes the sum of survivors was wrong since 699: imach-114 because nhstepm was no more computed in the age 700: loop. Now we define nhstepma in the age loop. 701: (Module): In order to speed up (in case of numerous covariates) we 702: compute health expectancies (without variances) in a first step 703: and then all the health expectancies with variances or standard 704: deviation (needs data from the Hessian matrices) which slows the 705: computation. 706: In the future we should be able to stop the program is only health 707: expectancies and graph are needed without standard deviations. 708: 709: Revision 1.126 2006/04/28 17:23:28 brouard 710: (Module): Yes the sum of survivors was wrong since 711: imach-114 because nhstepm was no more computed in the age 712: loop. Now we define nhstepma in the age loop. 713: Version 0.98h 714: 715: Revision 1.125 2006/04/04 15:20:31 lievre 716: Errors in calculation of health expectancies. Age was not initialized. 717: Forecasting file added. 718: 719: Revision 1.124 2006/03/22 17:13:53 lievre 720: Parameters are printed with %lf instead of %f (more numbers after the comma). 721: The log-likelihood is printed in the log file 722: 723: Revision 1.123 2006/03/20 10:52:43 brouard 724: * imach.c (Module): <title> changed, corresponds to .htm file 725: name. <head> headers where missing. 726: 727: * imach.c (Module): Weights can have a decimal point as for 728: English (a comma might work with a correct LC_NUMERIC environment, 729: otherwise the weight is truncated). 730: Modification of warning when the covariates values are not 0 or 731: 1. 732: Version 0.98g 733: 734: Revision 1.122 2006/03/20 09:45:41 brouard 735: (Module): Weights can have a decimal point as for 736: English (a comma might work with a correct LC_NUMERIC environment, 737: otherwise the weight is truncated). 738: Modification of warning when the covariates values are not 0 or 739: 1. 740: Version 0.98g 741: 742: Revision 1.121 2006/03/16 17:45:01 lievre 743: * imach.c (Module): Comments concerning covariates added 744: 745: * imach.c (Module): refinements in the computation of lli if 746: status=-2 in order to have more reliable computation if stepm is 747: not 1 month. Version 0.98f 748: 749: Revision 1.120 2006/03/16 15:10:38 lievre 750: (Module): refinements in the computation of lli if 751: status=-2 in order to have more reliable computation if stepm is 752: not 1 month. Version 0.98f 753: 754: Revision 1.119 2006/03/15 17:42:26 brouard 755: (Module): Bug if status = -2, the loglikelihood was 756: computed as likelihood omitting the logarithm. Version O.98e 757: 758: Revision 1.118 2006/03/14 18:20:07 brouard 759: (Module): varevsij Comments added explaining the second 760: table of variances if popbased=1 . 761: (Module): Covariances of eij, ekl added, graphs fixed, new html link. 762: (Module): Function pstamp added 763: (Module): Version 0.98d 764: 765: Revision 1.117 2006/03/14 17:16:22 brouard 766: (Module): varevsij Comments added explaining the second 767: table of variances if popbased=1 . 768: (Module): Covariances of eij, ekl added, graphs fixed, new html link. 769: (Module): Function pstamp added 770: (Module): Version 0.98d 771: 772: Revision 1.116 2006/03/06 10:29:27 brouard 773: (Module): Variance-covariance wrong links and 774: varian-covariance of ej. is needed (Saito). 775: 776: Revision 1.115 2006/02/27 12:17:45 brouard 777: (Module): One freematrix added in mlikeli! 0.98c 778: 779: Revision 1.114 2006/02/26 12:57:58 brouard 780: (Module): Some improvements in processing parameter 781: filename with strsep. 782: 783: Revision 1.113 2006/02/24 14:20:24 brouard 784: (Module): Memory leaks checks with valgrind and: 785: datafile was not closed, some imatrix were not freed and on matrix 786: allocation too. 787: 788: Revision 1.112 2006/01/30 09:55:26 brouard 789: (Module): Back to gnuplot.exe instead of wgnuplot.exe 790: 791: Revision 1.111 2006/01/25 20:38:18 brouard 792: (Module): Lots of cleaning and bugs added (Gompertz) 793: (Module): Comments can be added in data file. Missing date values 794: can be a simple dot '.'. 795: 796: Revision 1.110 2006/01/25 00:51:50 brouard 797: (Module): Lots of cleaning and bugs added (Gompertz) 798: 799: Revision 1.109 2006/01/24 19:37:15 brouard 800: (Module): Comments (lines starting with a #) are allowed in data. 801: 802: Revision 1.108 2006/01/19 18:05:42 lievre 803: Gnuplot problem appeared... 804: To be fixed 805: 806: Revision 1.107 2006/01/19 16:20:37 brouard 807: Test existence of gnuplot in imach path 808: 809: Revision 1.106 2006/01/19 13:24:36 brouard 810: Some cleaning and links added in html output 811: 812: Revision 1.105 2006/01/05 20:23:19 lievre 813: *** empty log message *** 814: 815: Revision 1.104 2005/09/30 16:11:43 lievre 816: (Module): sump fixed, loop imx fixed, and simplifications. 817: (Module): If the status is missing at the last wave but we know 818: that the person is alive, then we can code his/her status as -2 819: (instead of missing=-1 in earlier versions) and his/her 820: contributions to the likelihood is 1 - Prob of dying from last 821: health status (= 1-p13= p11+p12 in the easiest case of somebody in 822: the healthy state at last known wave). Version is 0.98 823: 824: Revision 1.103 2005/09/30 15:54:49 lievre 825: (Module): sump fixed, loop imx fixed, and simplifications. 826: 827: Revision 1.102 2004/09/15 17:31:30 brouard 828: Add the possibility to read data file including tab characters. 829: 830: Revision 1.101 2004/09/15 10:38:38 brouard 831: Fix on curr_time 832: 833: Revision 1.100 2004/07/12 18:29:06 brouard 834: Add version for Mac OS X. Just define UNIX in Makefile 835: 836: Revision 1.99 2004/06/05 08:57:40 brouard 837: *** empty log message *** 838: 839: Revision 1.98 2004/05/16 15:05:56 brouard 840: New version 0.97 . First attempt to estimate force of mortality 841: directly from the data i.e. without the need of knowing the health 842: state at each age, but using a Gompertz model: log u =a + b*age . 843: This is the basic analysis of mortality and should be done before any 844: other analysis, in order to test if the mortality estimated from the 845: cross-longitudinal survey is different from the mortality estimated 846: from other sources like vital statistic data. 847: 848: The same imach parameter file can be used but the option for mle should be -3. 849: 850: Agnès, who wrote this part of the code, tried to keep most of the 851: former routines in order to include the new code within the former code. 852: 853: The output is very simple: only an estimate of the intercept and of 854: the slope with 95% confident intervals. 855: 856: Current limitations: 857: A) Even if you enter covariates, i.e. with the 858: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates. 859: B) There is no computation of Life Expectancy nor Life Table. 860: 861: Revision 1.97 2004/02/20 13:25:42 lievre 862: Version 0.96d. Population forecasting command line is (temporarily) 863: suppressed. 864: 865: Revision 1.96 2003/07/15 15:38:55 brouard 866: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is 867: rewritten within the same printf. Workaround: many printfs. 868: 869: Revision 1.95 2003/07/08 07:54:34 brouard 870: * imach.c (Repository): 871: (Repository): Using imachwizard code to output a more meaningful covariance 872: matrix (cov(a12,c31) instead of numbers. 873: 874: Revision 1.94 2003/06/27 13:00:02 brouard 875: Just cleaning 876: 877: Revision 1.93 2003/06/25 16:33:55 brouard 878: (Module): On windows (cygwin) function asctime_r doesn't 879: exist so I changed back to asctime which exists. 880: (Module): Version 0.96b 881: 882: Revision 1.92 2003/06/25 16:30:45 brouard 883: (Module): On windows (cygwin) function asctime_r doesn't 884: exist so I changed back to asctime which exists. 885: 886: Revision 1.91 2003/06/25 15:30:29 brouard 887: * imach.c (Repository): Duplicated warning errors corrected. 888: (Repository): Elapsed time after each iteration is now output. It 889: helps to forecast when convergence will be reached. Elapsed time 890: is stamped in powell. We created a new html file for the graphs 891: concerning matrix of covariance. It has extension -cov.htm. 892: 893: Revision 1.90 2003/06/24 12:34:15 brouard 894: (Module): Some bugs corrected for windows. Also, when 895: mle=-1 a template is output in file "or"mypar.txt with the design 896: of the covariance matrix to be input. 897: 898: Revision 1.89 2003/06/24 12:30:52 brouard 899: (Module): Some bugs corrected for windows. Also, when 900: mle=-1 a template is output in file "or"mypar.txt with the design 901: of the covariance matrix to be input. 902: 903: Revision 1.88 2003/06/23 17:54:56 brouard 904: * 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. 905: 906: Revision 1.87 2003/06/18 12:26:01 brouard 907: Version 0.96 908: 909: Revision 1.86 2003/06/17 20:04:08 brouard 910: (Module): Change position of html and gnuplot routines and added 911: routine fileappend. 912: 913: Revision 1.85 2003/06/17 13:12:43 brouard 914: * imach.c (Repository): Check when date of death was earlier that 915: current date of interview. It may happen when the death was just 916: prior to the death. In this case, dh was negative and likelihood 917: was wrong (infinity). We still send an "Error" but patch by 918: assuming that the date of death was just one stepm after the 919: interview. 920: (Repository): Because some people have very long ID (first column) 921: we changed int to long in num[] and we added a new lvector for 922: memory allocation. But we also truncated to 8 characters (left 923: truncation) 924: (Repository): No more line truncation errors. 925: 926: Revision 1.84 2003/06/13 21:44:43 brouard 927: * imach.c (Repository): Replace "freqsummary" at a correct 928: place. It differs from routine "prevalence" which may be called 929: many times. Probs is memory consuming and must be used with 930: parcimony. 931: Version 0.95a3 (should output exactly the same maximization than 0.8a2) 932: 933: Revision 1.83 2003/06/10 13:39:11 lievre 934: *** empty log message *** 935: 936: Revision 1.82 2003/06/05 15:57:20 brouard 937: Add log in imach.c and fullversion number is now printed. 938: 939: */ 940: /* 941: Interpolated Markov Chain 942: 943: Short summary of the programme: 944: 945: This program computes Healthy Life Expectancies or State-specific 946: (if states aren't health statuses) Expectancies from 947: cross-longitudinal data. Cross-longitudinal data consist in: 948: 949: -1- a first survey ("cross") where individuals from different ages 950: are interviewed on their health status or degree of disability (in 951: the case of a health survey which is our main interest) 952: 953: -2- at least a second wave of interviews ("longitudinal") which 954: measure each change (if any) in individual health status. Health 955: expectancies are computed from the time spent in each health state 956: according to a model. More health states you consider, more time is 957: necessary to reach the Maximum Likelihood of the parameters involved 958: in the model. The simplest model is the multinomial logistic model 959: where pij is the probability to be observed in state j at the second 960: wave conditional to be observed in state i at the first 961: wave. Therefore the model is: log(pij/pii)= aij + bij*age+ cij*sex + 962: etc , where 'age' is age and 'sex' is a covariate. If you want to 963: have a more complex model than "constant and age", you should modify 964: the program where the markup *Covariates have to be included here 965: again* invites you to do it. More covariates you add, slower the 966: convergence. 967: 968: The advantage of this computer programme, compared to a simple 969: multinomial logistic model, is clear when the delay between waves is not 970: identical for each individual. Also, if a individual missed an 971: intermediate interview, the information is lost, but taken into 972: account using an interpolation or extrapolation. 973: 974: hPijx is the probability to be observed in state i at age x+h 975: conditional to the observed state i at age x. The delay 'h' can be 976: split into an exact number (nh*stepm) of unobserved intermediate 977: states. This elementary transition (by month, quarter, 978: semester or year) is modelled as a multinomial logistic. The hPx 979: matrix is simply the matrix product of nh*stepm elementary matrices 980: and the contribution of each individual to the likelihood is simply 981: hPijx. 982: 983: Also this programme outputs the covariance matrix of the parameters but also 984: of the life expectancies. It also computes the period (stable) prevalence. 985: 986: Back prevalence and projections: 987: 988: - back_prevalence_limit(double *p, double **bprlim, double ageminpar, 989: double agemaxpar, double ftolpl, int *ncvyearp, double 990: dateprev1,double dateprev2, int firstpass, int lastpass, int 991: mobilavproj) 992: 993: Computes the back prevalence limit for any combination of 994: covariate values k at any age between ageminpar and agemaxpar and 995: returns it in **bprlim. In the loops, 996: 997: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm, 998: **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k); 999: 1000: - hBijx Back Probability to be in state i at age x-h being in j at x 1001: Computes for any combination of covariates k and any age between bage and fage 1002: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 1003: oldm=oldms;savm=savms; 1004: 1005: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres); 1006: Computes the transition matrix starting at age 'age' over 1007: 'nhstepm*hstepm*stepm' months (i.e. until 1008: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying 1009: nhstepm*hstepm matrices. 1010: 1011: Returns p3mat[i][j][h] after calling 1012: p3mat[i][j][h]=matprod2(newm, 1013: bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, 1014: dsavm,ij),\ 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, 1015: oldm); 1016: 1017: Important routines 1018: 1019: - func (or funcone), computes logit (pij) distinguishing 1020: o fixed variables (single or product dummies or quantitative); 1021: o varying variables by: 1022: (1) wave (single, product dummies, quantitative), 1023: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be: 1024: % fixed dummy (treated) or quantitative (not done because time-consuming); 1025: % varying dummy (not done) or quantitative (not done); 1026: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities) 1027: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually. 1028: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables 1029: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if 1030: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless. 1031: 1032: 1033: 1034: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr). 1035: Institut national d'études démographiques, Paris. 1036: This software have been partly granted by Euro-REVES, a concerted action 1037: from the European Union. 1038: It is copyrighted identically to a GNU software product, ie programme and 1039: software can be distributed freely for non commercial use. Latest version 1040: can be accessed at http://euroreves.ined.fr/imach . 1041: 1042: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach 1043: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so 1044: 1045: **********************************************************************/ 1046: /* 1047: main 1048: read parameterfile 1049: read datafile 1050: concatwav 1051: freqsummary 1052: if (mle >= 1) 1053: mlikeli 1054: print results files 1055: if mle==1 1056: computes hessian 1057: read end of parameter file: agemin, agemax, bage, fage, estepm 1058: begin-prev-date,... 1059: open gnuplot file 1060: open html file 1061: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate 1062: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ****** 1063: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674 1064: freexexit2 possible for memory heap. 1065: 1066: h Pij x | pij_nom ficrestpij 1067: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3 1068: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000 1069: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907 1070: 1071: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340 1072: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597 1073: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in () 1074: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix 1075: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix 1076: 1077: forecasting if prevfcast==1 prevforecast call prevalence() 1078: health expectancies 1079: Variance-covariance of DFLE 1080: prevalence() 1081: movingaverage() 1082: varevsij() 1083: if popbased==1 varevsij(,popbased) 1084: total life expectancies 1085: Variance of period (stable) prevalence 1086: end 1087: */ 1088: 1089: /* #define DEBUG */ 1090: /* #define DEBUGBRENT */ 1091: /* #define DEBUGLINMIN */ 1092: /* #define DEBUGHESS */ 1093: #define DEBUGHESSIJ 1094: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */ 1095: #define POWELL /* Instead of NLOPT */ 1096: #define POWELLNOF3INFF1TEST /* Skip test */ 1097: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */ 1098: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */ 1099: 1100: #include <math.h> 1101: #include <stdio.h> 1102: #include <stdlib.h> 1103: #include <string.h> 1104: #include <ctype.h> 1105: 1106: #ifdef _WIN32 1107: #include <io.h> 1108: #include <windows.h> 1109: #include <tchar.h> 1110: #else 1111: #include <unistd.h> 1112: #endif 1113: 1114: #include <limits.h> 1115: #include <sys/types.h> 1116: 1117: #if defined(__GNUC__) 1118: #include <sys/utsname.h> /* Doesn't work on Windows */ 1119: #endif 1120: 1121: #include <sys/stat.h> 1122: #include <errno.h> 1123: /* extern int errno; */ 1124: 1125: /* #ifdef LINUX */ 1126: /* #include <time.h> */ 1127: /* #include "timeval.h" */ 1128: /* #else */ 1129: /* #include <sys/time.h> */ 1130: /* #endif */ 1131: 1132: #include <time.h> 1133: 1134: #ifdef GSL 1135: #include <gsl/gsl_errno.h> 1136: #include <gsl/gsl_multimin.h> 1137: #endif 1138: 1139: 1140: #ifdef NLOPT 1141: #include <nlopt.h> 1142: typedef struct { 1143: double (* function)(double [] ); 1144: } myfunc_data ; 1145: #endif 1146: 1147: /* #include <libintl.h> */ 1148: /* #define _(String) gettext (String) */ 1149: 1150: #define MAXLINE 2048 /* Was 256 and 1024. Overflow with 312 with 2 states and 4 covariates. Should be ok */ 1151: 1152: #define GNUPLOTPROGRAM "gnuplot" 1153: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/ 1154: #define FILENAMELENGTH 132 1155: 1156: #define GLOCK_ERROR_NOPATH -1 /* empty path */ 1157: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */ 1158: 1159: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */ 1160: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */ 1161: 1162: #define NINTERVMAX 8 1163: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */ 1164: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */ 1165: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */ 1166: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1 1167: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/ 1168: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1 1169: /*#define MAXN 20000 */ /* Should by replaced by nobs, real number of observations and unlimited */ 1170: #define YEARM 12. /**< Number of months per year */ 1171: /* #define AGESUP 130 */ 1172: /* #define AGESUP 150 */ 1173: #define AGESUP 200 1174: #define AGEINF 0 1175: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */ 1176: #define AGEBASE 40 1177: #define AGEOVERFLOW 1.e20 1178: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */ 1179: #ifdef _WIN32 1180: #define DIRSEPARATOR '\\' 1181: #define CHARSEPARATOR "\\" 1182: #define ODIRSEPARATOR '/' 1183: #else 1184: #define DIRSEPARATOR '/' 1185: #define CHARSEPARATOR "/" 1186: #define ODIRSEPARATOR '\\' 1187: #endif 1188: 1189: /* $Id: imach.c,v 1.317 2022/05/15 15:06:23 brouard Exp $ */ 1190: /* $State: Exp $ */ 1191: #include "version.h" 1192: char version[]=__IMACH_VERSION__; 1193: char copyright[]="May 2022,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2020, Nihon University 2021-202, INED 2000-2022"; 1194: char fullversion[]="$Revision: 1.317 $ $Date: 2022/05/15 15:06:23 $"; 1195: char strstart[80]; 1196: char optionfilext[10], optionfilefiname[FILENAMELENGTH]; 1197: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */ 1198: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */ 1199: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */ 1200: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */ 1201: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */ 1202: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */ 1203: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */ 1204: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */ 1205: int cptcovprodnoage=0; /**< Number of covariate products without age */ 1206: int cptcoveff=0; /* Total number of covariates to vary for printing results */ 1207: int ncovf=0; /* Total number of effective fixed covariates (dummy or quantitative) in the model */ 1208: int ncovv=0; /* Total number of effective (wave) varying covariates (dummy or quantitative) in the model */ 1209: int ncova=0; /* Total number of effective (wave and stepm) varying with age covariates (dummy of quantitative) in the model */ 1210: int nsd=0; /**< Total number of single dummy variables (output) */ 1211: int nsq=0; /**< Total number of single quantitative variables (output) */ 1212: int ncoveff=0; /* Total number of effective fixed dummy covariates in the model */ 1213: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */ 1214: int ntveff=0; /**< ntveff number of effective time varying variables */ 1215: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */ 1216: int cptcov=0; /* Working variable */ 1217: int nobs=10; /* Number of observations in the data lastobs-firstobs */ 1218: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */ 1219: int npar=NPARMAX; /* Number of parameters (nlstate+ndeath-1)*nlstate*ncovmodel; */ 1220: int nlstate=2; /* Number of live states */ 1221: int ndeath=1; /* Number of dead states */ 1222: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */ 1223: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */ 1224: int popbased=0; 1225: 1226: int *wav; /* Number of waves for this individuual 0 is possible */ 1227: int maxwav=0; /* Maxim number of waves */ 1228: int jmin=0, jmax=0; /* min, max spacing between 2 waves */ 1229: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */ 1230: int gipmx=0, gsw=0; /* Global variables on the number of contributions 1231: to the likelihood and the sum of weights (done by funcone)*/ 1232: int mle=1, weightopt=0; 1233: int **mw; /* mw[mi][i] is number of the mi wave for this individual */ 1234: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */ 1235: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between 1236: * wave mi and wave mi+1 is not an exact multiple of stepm. */ 1237: int countcallfunc=0; /* Count the number of calls to func */ 1238: int selected(int kvar); /* Is covariate kvar selected for printing results */ 1239: 1240: double jmean=1; /* Mean space between 2 waves */ 1241: double **matprod2(); /* test */ 1242: double **oldm, **newm, **savm; /* Working pointers to matrices */ 1243: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */ 1244: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */ 1245: 1246: /*FILE *fic ; */ /* Used in readdata only */ 1247: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop; 1248: FILE *ficlog, *ficrespow; 1249: int globpr=0; /* Global variable for printing or not */ 1250: double fretone; /* Only one call to likelihood */ 1251: long ipmx=0; /* Number of contributions */ 1252: double sw; /* Sum of weights */ 1253: char filerespow[FILENAMELENGTH]; 1254: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */ 1255: FILE *ficresilk; 1256: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor; 1257: FILE *ficresprobmorprev; 1258: FILE *fichtm, *fichtmcov; /* Html File */ 1259: FILE *ficreseij; 1260: char filerese[FILENAMELENGTH]; 1261: FILE *ficresstdeij; 1262: char fileresstde[FILENAMELENGTH]; 1263: FILE *ficrescveij; 1264: char filerescve[FILENAMELENGTH]; 1265: FILE *ficresvij; 1266: char fileresv[FILENAMELENGTH]; 1267: 1268: char title[MAXLINE]; 1269: char model[MAXLINE]; /**< The model line */ 1270: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH]; 1271: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH]; 1272: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH]; 1273: char command[FILENAMELENGTH]; 1274: int outcmd=0; 1275: 1276: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH]; 1277: char fileresu[FILENAMELENGTH]; /* fileres without r in front */ 1278: char filelog[FILENAMELENGTH]; /* Log file */ 1279: char filerest[FILENAMELENGTH]; 1280: char fileregp[FILENAMELENGTH]; 1281: char popfile[FILENAMELENGTH]; 1282: 1283: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ; 1284: 1285: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */ 1286: /* struct timezone tzp; */ 1287: /* extern int gettimeofday(); */ 1288: struct tm tml, *gmtime(), *localtime(); 1289: 1290: extern time_t time(); 1291: 1292: struct tm start_time, end_time, curr_time, last_time, forecast_time; 1293: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */ 1294: struct tm tm; 1295: 1296: char strcurr[80], strfor[80]; 1297: 1298: char *endptr; 1299: long lval; 1300: double dval; 1301: 1302: #define NR_END 1 1303: #define FREE_ARG char* 1304: #define FTOL 1.0e-10 1305: 1306: #define NRANSI 1307: #define ITMAX 200 1308: #define ITPOWMAX 20 /* This is now multiplied by the number of parameters */ 1309: 1310: #define TOL 2.0e-4 1311: 1312: #define CGOLD 0.3819660 1313: #define ZEPS 1.0e-10 1314: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d); 1315: 1316: #define GOLD 1.618034 1317: #define GLIMIT 100.0 1318: #define TINY 1.0e-20 1319: 1320: static double maxarg1,maxarg2; 1321: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2)) 1322: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2)) 1323: 1324: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a)) 1325: #define rint(a) floor(a+0.5) 1326: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */ 1327: #define mytinydouble 1.0e-16 1328: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */ 1329: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */ 1330: /* static double dsqrarg; */ 1331: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */ 1332: static double sqrarg; 1333: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg) 1334: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;} 1335: int agegomp= AGEGOMP; 1336: 1337: int imx; 1338: int stepm=1; 1339: /* Stepm, step in month: minimum step interpolation*/ 1340: 1341: int estepm; 1342: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/ 1343: 1344: int m,nb; 1345: long *num; 1346: int firstpass=0, lastpass=4,*cod, *cens; 1347: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th 1348: covariate for which somebody answered excluding 1349: undefined. Usually 2: 0 and 1. */ 1350: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th 1351: covariate for which somebody answered including 1352: undefined. Usually 3: -1, 0 and 1. */ 1353: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint; 1354: double **pmmij, ***probs; /* Global pointer */ 1355: double ***mobaverage, ***mobaverages; /* New global variable */ 1356: double *ageexmed,*agecens; 1357: double dateintmean=0; 1358: double anprojd, mprojd, jprojd; /* For eventual projections */ 1359: double anprojf, mprojf, jprojf; 1360: 1361: double anbackd, mbackd, jbackd; /* For eventual backprojections */ 1362: double anbackf, mbackf, jbackf; 1363: double jintmean,mintmean,aintmean; 1364: double *weight; 1365: int **s; /* Status */ 1366: double *agedc; 1367: double **covar; /**< covar[j,i], value of jth covariate for individual i, 1368: * covar=matrix(0,NCOVMAX,1,n); 1369: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */ 1370: double **coqvar; /* Fixed quantitative covariate nqv */ 1371: double ***cotvar; /* Time varying covariate ntv */ 1372: double ***cotqvar; /* Time varying quantitative covariate itqv */ 1373: double idx; 1374: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */ 1375: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 1376: /*k 1 2 3 4 5 6 7 8 9 */ 1377: /*Tvar[k]= 5 4 3 6 5 2 7 1 1 */ 1378: /* Tndvar[k] 1 2 3 4 5 */ 1379: /*TDvar 4 3 6 7 1 */ /* For outputs only; combination of dummies fixed or varying */ 1380: /* Tns[k] 1 2 2 4 5 */ /* Number of single cova */ 1381: /* TvarsD[k] 1 2 3 */ /* Number of single dummy cova */ 1382: /* TvarsDind 2 3 9 */ /* position K of single dummy cova */ 1383: /* TvarsQ[k] 1 2 */ /* Number of single quantitative cova */ 1384: /* TvarsQind 1 6 */ /* position K of single quantitative cova */ 1385: /* Tprod[i]=k 4 7 */ 1386: /* Tage[i]=k 5 8 */ 1387: /* */ 1388: /* Type */ 1389: /* V 1 2 3 4 5 */ 1390: /* F F V V V */ 1391: /* D Q D D Q */ 1392: /* */ 1393: int *TvarsD; 1394: int *TvarsDind; 1395: int *TvarsQ; 1396: int *TvarsQind; 1397: 1398: #define MAXRESULTLINES 10 1399: int nresult=0; 1400: int parameterline=0; /* # of the parameter (type) line */ 1401: int TKresult[MAXRESULTLINES]; 1402: int Tresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */ 1403: int Tinvresult[MAXRESULTLINES][NCOVMAX];/* For dummy variable , value (output) */ 1404: int Tvresult[MAXRESULTLINES][NCOVMAX]; /* For dummy variable , variable # (output) */ 1405: double Tqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */ 1406: double Tqinvresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , value (output) */ 1407: int Tvqresult[MAXRESULTLINES][NCOVMAX]; /* For quantitative variable , variable # (output) */ 1408: 1409: /* 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 *\/ */ 1410: 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 */ 1411: 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 */ 1412: 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 */ 1413: int *TvarVind; /**< TvarVind[1]=1, TvarVind[2]=2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 1414: 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 */ 1415: int *TvarAind; /**< TvarindA[1]=5, TvarAind[2]=8 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 1416: int *TvarFD; /**< TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 1417: int *TvarFDind; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 1418: int *TvarFQ; /* TvarFQ[1]=V2 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */ 1419: int *TvarFQind; /* TvarFQind[1]=6 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */ 1420: int *TvarVD; /* TvarVD[1]=V5 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */ 1421: int *TvarVDind; /* TvarVDind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Only simple fixed quantitative variable */ 1422: 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 */ 1423: 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 */ 1424: 1425: int *Tvarsel; /**< Selected covariates for output */ 1426: double *Tvalsel; /**< Selected modality value of covariate for output */ 1427: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */ 1428: int *Fixed; /** Fixed[k] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */ 1429: 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 */ 1430: int *DummyV; /** Dummy[v] 0=dummy (0 1), 1 quantitative */ 1431: int *FixedV; /** FixedV[v] 0 fixed, 1 varying */ 1432: int *Tage; 1433: int anyvaryingduminmodel=0; /**< Any varying dummy in Model=1 yes, 0 no, to avoid a loop on waves in freq */ 1434: 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*/ 1435: 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*/ 1436: 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 */ 1437: int *Ndum; /** Freq of modality (tricode */ 1438: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */ 1439: int **Tvard; 1440: int *Tprod;/**< Gives the k position of the k1 product */ 1441: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 */ 1442: int *Tposprod; /**< Gives the k1 product from the k position */ 1443: /* if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) */ 1444: /* Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5(V3*V2)]=2 (2nd product without age) */ 1445: int cptcovprod, *Tvaraff, *invalidvarcomb; 1446: double *lsurv, *lpop, *tpop; 1447: 1448: #define FD 1; /* Fixed dummy covariate */ 1449: #define FQ 2; /* Fixed quantitative covariate */ 1450: #define FP 3; /* Fixed product covariate */ 1451: #define FPDD 7; /* Fixed product dummy*dummy covariate */ 1452: #define FPDQ 8; /* Fixed product dummy*quantitative covariate */ 1453: #define FPQQ 9; /* Fixed product quantitative*quantitative covariate */ 1454: #define VD 10; /* Varying dummy covariate */ 1455: #define VQ 11; /* Varying quantitative covariate */ 1456: #define VP 12; /* Varying product covariate */ 1457: #define VPDD 13; /* Varying product dummy*dummy covariate */ 1458: #define VPDQ 14; /* Varying product dummy*quantitative covariate */ 1459: #define VPQQ 15; /* Varying product quantitative*quantitative covariate */ 1460: #define APFD 16; /* Age product * fixed dummy covariate */ 1461: #define APFQ 17; /* Age product * fixed quantitative covariate */ 1462: #define APVD 18; /* Age product * varying dummy covariate */ 1463: #define APVQ 19; /* Age product * varying quantitative covariate */ 1464: 1465: #define FTYPE 1; /* Fixed covariate */ 1466: #define VTYPE 2; /* Varying covariate (loop in wave) */ 1467: #define ATYPE 2; /* Age product covariate (loop in dh within wave)*/ 1468: 1469: struct kmodel{ 1470: int maintype; /* main type */ 1471: int subtype; /* subtype */ 1472: }; 1473: struct kmodel modell[NCOVMAX]; 1474: 1475: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */ 1476: double ftolhess; /**< Tolerance for computing hessian */ 1477: 1478: /**************** split *************************/ 1479: static int split( char *path, char *dirc, char *name, char *ext, char *finame ) 1480: { 1481: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc) 1482: the name of the file (name), its extension only (ext) and its first part of the name (finame) 1483: */ 1484: char *ss; /* pointer */ 1485: int l1=0, l2=0; /* length counters */ 1486: 1487: l1 = strlen(path ); /* length of path */ 1488: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH ); 1489: ss= strrchr( path, DIRSEPARATOR ); /* find last / */ 1490: if ( ss == NULL ) { /* no directory, so determine current directory */ 1491: strcpy( name, path ); /* we got the fullname name because no directory */ 1492: /*if(strrchr(path, ODIRSEPARATOR )==NULL) 1493: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/ 1494: /* get current working directory */ 1495: /* extern char* getcwd ( char *buf , int len);*/ 1496: #ifdef WIN32 1497: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) { 1498: #else 1499: if (getcwd(dirc, FILENAME_MAX) == NULL) { 1500: #endif 1501: return( GLOCK_ERROR_GETCWD ); 1502: } 1503: /* got dirc from getcwd*/ 1504: printf(" DIRC = %s \n",dirc); 1505: } else { /* strip directory from path */ 1506: ss++; /* after this, the filename */ 1507: l2 = strlen( ss ); /* length of filename */ 1508: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH ); 1509: strcpy( name, ss ); /* save file name */ 1510: strncpy( dirc, path, l1 - l2 ); /* now the directory */ 1511: dirc[l1-l2] = '\0'; /* add zero */ 1512: printf(" DIRC2 = %s \n",dirc); 1513: } 1514: /* We add a separator at the end of dirc if not exists */ 1515: l1 = strlen( dirc ); /* length of directory */ 1516: if( dirc[l1-1] != DIRSEPARATOR ){ 1517: dirc[l1] = DIRSEPARATOR; 1518: dirc[l1+1] = 0; 1519: printf(" DIRC3 = %s \n",dirc); 1520: } 1521: ss = strrchr( name, '.' ); /* find last / */ 1522: if (ss >0){ 1523: ss++; 1524: strcpy(ext,ss); /* save extension */ 1525: l1= strlen( name); 1526: l2= strlen(ss)+1; 1527: strncpy( finame, name, l1-l2); 1528: finame[l1-l2]= 0; 1529: } 1530: 1531: return( 0 ); /* we're done */ 1532: } 1533: 1534: 1535: /******************************************/ 1536: 1537: void replace_back_to_slash(char *s, char*t) 1538: { 1539: int i; 1540: int lg=0; 1541: i=0; 1542: lg=strlen(t); 1543: for(i=0; i<= lg; i++) { 1544: (s[i] = t[i]); 1545: if (t[i]== '\\') s[i]='/'; 1546: } 1547: } 1548: 1549: char *trimbb(char *out, char *in) 1550: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */ 1551: char *s; 1552: s=out; 1553: while (*in != '\0'){ 1554: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/ 1555: in++; 1556: } 1557: *out++ = *in++; 1558: } 1559: *out='\0'; 1560: return s; 1561: } 1562: 1563: /* char *substrchaine(char *out, char *in, char *chain) */ 1564: /* { */ 1565: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */ 1566: /* char *s, *t; */ 1567: /* t=in;s=out; */ 1568: /* while ((*in != *chain) && (*in != '\0')){ */ 1569: /* *out++ = *in++; */ 1570: /* } */ 1571: 1572: /* /\* *in matches *chain *\/ */ 1573: /* while ((*in++ == *chain++) && (*in != '\0')){ */ 1574: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */ 1575: /* } */ 1576: /* in--; chain--; */ 1577: /* while ( (*in != '\0')){ */ 1578: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */ 1579: /* *out++ = *in++; */ 1580: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */ 1581: /* } */ 1582: /* *out='\0'; */ 1583: /* out=s; */ 1584: /* return out; */ 1585: /* } */ 1586: char *substrchaine(char *out, char *in, char *chain) 1587: { 1588: /* Substract chain 'chain' from 'in', return and output 'out' */ 1589: /* in="V1+V1*age+age*age+V2", chain="age*age" */ 1590: 1591: char *strloc; 1592: 1593: strcpy (out, in); 1594: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */ 1595: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out); 1596: if(strloc != NULL){ 1597: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */ 1598: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1); 1599: /* strcpy (strloc, strloc +strlen(chain));*/ 1600: } 1601: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out); 1602: return out; 1603: } 1604: 1605: 1606: char *cutl(char *blocc, char *alocc, char *in, char occ) 1607: { 1608: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ' 1609: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2') 1610: gives alocc="abcdef" and blocc="ghi2j". 1611: If occ is not found blocc is null and alocc is equal to in. Returns blocc 1612: */ 1613: char *s, *t; 1614: t=in;s=in; 1615: while ((*in != occ) && (*in != '\0')){ 1616: *alocc++ = *in++; 1617: } 1618: if( *in == occ){ 1619: *(alocc)='\0'; 1620: s=++in; 1621: } 1622: 1623: if (s == t) {/* occ not found */ 1624: *(alocc-(in-s))='\0'; 1625: in=s; 1626: } 1627: while ( *in != '\0'){ 1628: *blocc++ = *in++; 1629: } 1630: 1631: *blocc='\0'; 1632: return t; 1633: } 1634: char *cutv(char *blocc, char *alocc, char *in, char occ) 1635: { 1636: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ' 1637: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2') 1638: gives blocc="abcdef2ghi" and alocc="j". 1639: If occ is not found blocc is null and alocc is equal to in. Returns alocc 1640: */ 1641: char *s, *t; 1642: t=in;s=in; 1643: while (*in != '\0'){ 1644: while( *in == occ){ 1645: *blocc++ = *in++; 1646: s=in; 1647: } 1648: *blocc++ = *in++; 1649: } 1650: if (s == t) /* occ not found */ 1651: *(blocc-(in-s))='\0'; 1652: else 1653: *(blocc-(in-s)-1)='\0'; 1654: in=s; 1655: while ( *in != '\0'){ 1656: *alocc++ = *in++; 1657: } 1658: 1659: *alocc='\0'; 1660: return s; 1661: } 1662: 1663: int nbocc(char *s, char occ) 1664: { 1665: int i,j=0; 1666: int lg=20; 1667: i=0; 1668: lg=strlen(s); 1669: for(i=0; i<= lg; i++) { 1670: if (s[i] == occ ) j++; 1671: } 1672: return j; 1673: } 1674: 1675: /* void cutv(char *u,char *v, char*t, char occ) */ 1676: /* { */ 1677: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */ 1678: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */ 1679: /* gives u="abcdef2ghi" and v="j" *\/ */ 1680: /* int i,lg,j,p=0; */ 1681: /* i=0; */ 1682: /* lg=strlen(t); */ 1683: /* for(j=0; j<=lg-1; j++) { */ 1684: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */ 1685: /* } */ 1686: 1687: /* for(j=0; j<p; j++) { */ 1688: /* (u[j] = t[j]); */ 1689: /* } */ 1690: /* u[p]='\0'; */ 1691: 1692: /* for(j=0; j<= lg; j++) { */ 1693: /* if (j>=(p+1))(v[j-p-1] = t[j]); */ 1694: /* } */ 1695: /* } */ 1696: 1697: #ifdef _WIN32 1698: char * strsep(char **pp, const char *delim) 1699: { 1700: char *p, *q; 1701: 1702: if ((p = *pp) == NULL) 1703: return 0; 1704: if ((q = strpbrk (p, delim)) != NULL) 1705: { 1706: *pp = q + 1; 1707: *q = '\0'; 1708: } 1709: else 1710: *pp = 0; 1711: return p; 1712: } 1713: #endif 1714: 1715: /********************** nrerror ********************/ 1716: 1717: void nrerror(char error_text[]) 1718: { 1719: fprintf(stderr,"ERREUR ...\n"); 1720: fprintf(stderr,"%s\n",error_text); 1721: exit(EXIT_FAILURE); 1722: } 1723: /*********************** vector *******************/ 1724: double *vector(int nl, int nh) 1725: { 1726: double *v; 1727: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double))); 1728: if (!v) nrerror("allocation failure in vector"); 1729: return v-nl+NR_END; 1730: } 1731: 1732: /************************ free vector ******************/ 1733: void free_vector(double*v, int nl, int nh) 1734: { 1735: free((FREE_ARG)(v+nl-NR_END)); 1736: } 1737: 1738: /************************ivector *******************************/ 1739: int *ivector(long nl,long nh) 1740: { 1741: int *v; 1742: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int))); 1743: if (!v) nrerror("allocation failure in ivector"); 1744: return v-nl+NR_END; 1745: } 1746: 1747: /******************free ivector **************************/ 1748: void free_ivector(int *v, long nl, long nh) 1749: { 1750: free((FREE_ARG)(v+nl-NR_END)); 1751: } 1752: 1753: /************************lvector *******************************/ 1754: long *lvector(long nl,long nh) 1755: { 1756: long *v; 1757: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long))); 1758: if (!v) nrerror("allocation failure in ivector"); 1759: return v-nl+NR_END; 1760: } 1761: 1762: /******************free lvector **************************/ 1763: void free_lvector(long *v, long nl, long nh) 1764: { 1765: free((FREE_ARG)(v+nl-NR_END)); 1766: } 1767: 1768: /******************* imatrix *******************************/ 1769: int **imatrix(long nrl, long nrh, long ncl, long nch) 1770: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */ 1771: { 1772: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1; 1773: int **m; 1774: 1775: /* allocate pointers to rows */ 1776: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*))); 1777: if (!m) nrerror("allocation failure 1 in matrix()"); 1778: m += NR_END; 1779: m -= nrl; 1780: 1781: 1782: /* allocate rows and set pointers to them */ 1783: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int))); 1784: if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); 1785: m[nrl] += NR_END; 1786: m[nrl] -= ncl; 1787: 1788: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol; 1789: 1790: /* return pointer to array of pointers to rows */ 1791: return m; 1792: } 1793: 1794: /****************** free_imatrix *************************/ 1795: void free_imatrix(m,nrl,nrh,ncl,nch) 1796: int **m; 1797: long nch,ncl,nrh,nrl; 1798: /* free an int matrix allocated by imatrix() */ 1799: { 1800: free((FREE_ARG) (m[nrl]+ncl-NR_END)); 1801: free((FREE_ARG) (m+nrl-NR_END)); 1802: } 1803: 1804: /******************* matrix *******************************/ 1805: double **matrix(long nrl, long nrh, long ncl, long nch) 1806: { 1807: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1; 1808: double **m; 1809: 1810: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*))); 1811: if (!m) nrerror("allocation failure 1 in matrix()"); 1812: m += NR_END; 1813: m -= nrl; 1814: 1815: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double))); 1816: if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); 1817: m[nrl] += NR_END; 1818: m[nrl] -= ncl; 1819: 1820: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol; 1821: return m; 1822: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0]) 1823: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress 1824: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized. 1825: */ 1826: } 1827: 1828: /*************************free matrix ************************/ 1829: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch) 1830: { 1831: free((FREE_ARG)(m[nrl]+ncl-NR_END)); 1832: free((FREE_ARG)(m+nrl-NR_END)); 1833: } 1834: 1835: /******************* ma3x *******************************/ 1836: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh) 1837: { 1838: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1; 1839: double ***m; 1840: 1841: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*))); 1842: if (!m) nrerror("allocation failure 1 in matrix()"); 1843: m += NR_END; 1844: m -= nrl; 1845: 1846: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double))); 1847: if (!m[nrl]) nrerror("allocation failure 2 in matrix()"); 1848: m[nrl] += NR_END; 1849: m[nrl] -= ncl; 1850: 1851: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol; 1852: 1853: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double))); 1854: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()"); 1855: m[nrl][ncl] += NR_END; 1856: m[nrl][ncl] -= nll; 1857: for (j=ncl+1; j<=nch; j++) 1858: m[nrl][j]=m[nrl][j-1]+nlay; 1859: 1860: for (i=nrl+1; i<=nrh; i++) { 1861: m[i][ncl]=m[i-1l][ncl]+ncol*nlay; 1862: for (j=ncl+1; j<=nch; j++) 1863: m[i][j]=m[i][j-1]+nlay; 1864: } 1865: return m; 1866: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1]) 1867: &(m[i][j][k]) <=> *((*(m+i) + j)+k) 1868: */ 1869: } 1870: 1871: /*************************free ma3x ************************/ 1872: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh) 1873: { 1874: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END)); 1875: free((FREE_ARG)(m[nrl]+ncl-NR_END)); 1876: free((FREE_ARG)(m+nrl-NR_END)); 1877: } 1878: 1879: /*************** function subdirf ***********/ 1880: char *subdirf(char fileres[]) 1881: { 1882: /* Caution optionfilefiname is hidden */ 1883: strcpy(tmpout,optionfilefiname); 1884: strcat(tmpout,"/"); /* Add to the right */ 1885: strcat(tmpout,fileres); 1886: return tmpout; 1887: } 1888: 1889: /*************** function subdirf2 ***********/ 1890: char *subdirf2(char fileres[], char *preop) 1891: { 1892: /* Example subdirf2(optionfilefiname,"FB_") with optionfilefiname="texte", result="texte/FB_texte" 1893: Errors in subdirf, 2, 3 while printing tmpout is 1894: rewritten within the same printf. Workaround: many printfs */ 1895: /* Caution optionfilefiname is hidden */ 1896: strcpy(tmpout,optionfilefiname); 1897: strcat(tmpout,"/"); 1898: strcat(tmpout,preop); 1899: strcat(tmpout,fileres); 1900: return tmpout; 1901: } 1902: 1903: /*************** function subdirf3 ***********/ 1904: char *subdirf3(char fileres[], char *preop, char *preop2) 1905: { 1906: 1907: /* Caution optionfilefiname is hidden */ 1908: strcpy(tmpout,optionfilefiname); 1909: strcat(tmpout,"/"); 1910: strcat(tmpout,preop); 1911: strcat(tmpout,preop2); 1912: strcat(tmpout,fileres); 1913: return tmpout; 1914: } 1915: 1916: /*************** function subdirfext ***********/ 1917: char *subdirfext(char fileres[], char *preop, char *postop) 1918: { 1919: 1920: strcpy(tmpout,preop); 1921: strcat(tmpout,fileres); 1922: strcat(tmpout,postop); 1923: return tmpout; 1924: } 1925: 1926: /*************** function subdirfext3 ***********/ 1927: char *subdirfext3(char fileres[], char *preop, char *postop) 1928: { 1929: 1930: /* Caution optionfilefiname is hidden */ 1931: strcpy(tmpout,optionfilefiname); 1932: strcat(tmpout,"/"); 1933: strcat(tmpout,preop); 1934: strcat(tmpout,fileres); 1935: strcat(tmpout,postop); 1936: return tmpout; 1937: } 1938: 1939: char *asc_diff_time(long time_sec, char ascdiff[]) 1940: { 1941: long sec_left, days, hours, minutes; 1942: days = (time_sec) / (60*60*24); 1943: sec_left = (time_sec) % (60*60*24); 1944: hours = (sec_left) / (60*60) ; 1945: sec_left = (sec_left) %(60*60); 1946: minutes = (sec_left) /60; 1947: sec_left = (sec_left) % (60); 1948: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left); 1949: return ascdiff; 1950: } 1951: 1952: /***************** f1dim *************************/ 1953: extern int ncom; 1954: extern double *pcom,*xicom; 1955: extern double (*nrfunc)(double []); 1956: 1957: double f1dim(double x) 1958: { 1959: int j; 1960: double f; 1961: double *xt; 1962: 1963: xt=vector(1,ncom); 1964: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j]; 1965: f=(*nrfunc)(xt); 1966: free_vector(xt,1,ncom); 1967: return f; 1968: } 1969: 1970: /*****************brent *************************/ 1971: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin) 1972: { 1973: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is 1974: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates 1975: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of 1976: * the minimum is returned as xmin, and the minimum function value is returned as brent , the 1977: * returned function value. 1978: */ 1979: int iter; 1980: double a,b,d,etemp; 1981: double fu=0,fv,fw,fx; 1982: double ftemp=0.; 1983: double p,q,r,tol1,tol2,u,v,w,x,xm; 1984: double e=0.0; 1985: 1986: a=(ax < cx ? ax : cx); 1987: b=(ax > cx ? ax : cx); 1988: x=w=v=bx; 1989: fw=fv=fx=(*f)(x); 1990: for (iter=1;iter<=ITMAX;iter++) { 1991: xm=0.5*(a+b); 1992: tol2=2.0*(tol1=tol*fabs(x)+ZEPS); 1993: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/ 1994: printf(".");fflush(stdout); 1995: fprintf(ficlog,".");fflush(ficlog); 1996: #ifdef DEBUGBRENT 1997: 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); 1998: 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); 1999: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */ 2000: #endif 2001: if (fabs(x-xm) <= (tol2-0.5*(b-a))){ 2002: *xmin=x; 2003: return fx; 2004: } 2005: ftemp=fu; 2006: if (fabs(e) > tol1) { 2007: r=(x-w)*(fx-fv); 2008: q=(x-v)*(fx-fw); 2009: p=(x-v)*q-(x-w)*r; 2010: q=2.0*(q-r); 2011: if (q > 0.0) p = -p; 2012: q=fabs(q); 2013: etemp=e; 2014: e=d; 2015: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x)) 2016: d=CGOLD*(e=(x >= xm ? a-x : b-x)); 2017: else { 2018: d=p/q; 2019: u=x+d; 2020: if (u-a < tol2 || b-u < tol2) 2021: d=SIGN(tol1,xm-x); 2022: } 2023: } else { 2024: d=CGOLD*(e=(x >= xm ? a-x : b-x)); 2025: } 2026: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d)); 2027: fu=(*f)(u); 2028: if (fu <= fx) { 2029: if (u >= x) a=x; else b=x; 2030: SHFT(v,w,x,u) 2031: SHFT(fv,fw,fx,fu) 2032: } else { 2033: if (u < x) a=u; else b=u; 2034: if (fu <= fw || w == x) { 2035: v=w; 2036: w=u; 2037: fv=fw; 2038: fw=fu; 2039: } else if (fu <= fv || v == x || v == w) { 2040: v=u; 2041: fv=fu; 2042: } 2043: } 2044: } 2045: nrerror("Too many iterations in brent"); 2046: *xmin=x; 2047: return fx; 2048: } 2049: 2050: /****************** mnbrak ***********************/ 2051: 2052: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc, 2053: double (*func)(double)) 2054: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in 2055: the downhill direction (defined by the function as evaluated at the initial points) and returns 2056: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function 2057: values at the three points, fa, fb , and fc such that fa > fb and fb < fc. 2058: */ 2059: double ulim,u,r,q, dum; 2060: double fu; 2061: 2062: double scale=10.; 2063: int iterscale=0; 2064: 2065: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/ 2066: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */ 2067: 2068: 2069: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */ 2070: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */ 2071: /* *bx = *ax - (*ax - *bx)/scale; */ 2072: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */ 2073: /* } */ 2074: 2075: if (*fb > *fa) { 2076: SHFT(dum,*ax,*bx,dum) 2077: SHFT(dum,*fb,*fa,dum) 2078: } 2079: *cx=(*bx)+GOLD*(*bx-*ax); 2080: *fc=(*func)(*cx); 2081: #ifdef DEBUG 2082: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc); 2083: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc); 2084: #endif 2085: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/ 2086: r=(*bx-*ax)*(*fb-*fc); 2087: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */ 2088: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/ 2089: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */ 2090: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */ 2091: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */ 2092: fu=(*func)(u); 2093: #ifdef DEBUG 2094: /* f(x)=A(x-u)**2+f(u) */ 2095: double A, fparabu; 2096: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u); 2097: fparabu= *fa - A*(*ax-u)*(*ax-u); 2098: 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); 2099: 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); 2100: /* And thus,it can be that fu > *fc even if fparabu < *fc */ 2101: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489), 2102: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */ 2103: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/ 2104: #endif 2105: #ifdef MNBRAKORIGINAL 2106: #else 2107: /* if (fu > *fc) { */ 2108: /* #ifdef DEBUG */ 2109: /* printf("mnbrak4 fu > fc \n"); */ 2110: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */ 2111: /* #endif */ 2112: /* /\* 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 *\\/ *\/ */ 2113: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */ 2114: /* dum=u; /\* Shifting c and u *\/ */ 2115: /* u = *cx; */ 2116: /* *cx = dum; */ 2117: /* dum = fu; */ 2118: /* fu = *fc; */ 2119: /* *fc =dum; */ 2120: /* } else { /\* end *\/ */ 2121: /* #ifdef DEBUG */ 2122: /* printf("mnbrak3 fu < fc \n"); */ 2123: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */ 2124: /* #endif */ 2125: /* dum=u; /\* Shifting c and u *\/ */ 2126: /* u = *cx; */ 2127: /* *cx = dum; */ 2128: /* dum = fu; */ 2129: /* fu = *fc; */ 2130: /* *fc =dum; */ 2131: /* } */ 2132: #ifdef DEBUGMNBRAK 2133: double A, fparabu; 2134: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u); 2135: fparabu= *fa - A*(*ax-u)*(*ax-u); 2136: 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); 2137: 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); 2138: #endif 2139: dum=u; /* Shifting c and u */ 2140: u = *cx; 2141: *cx = dum; 2142: dum = fu; 2143: fu = *fc; 2144: *fc =dum; 2145: #endif 2146: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */ 2147: #ifdef DEBUG 2148: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx); 2149: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx); 2150: #endif 2151: fu=(*func)(u); 2152: if (fu < *fc) { 2153: #ifdef DEBUG 2154: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc); 2155: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc); 2156: #endif 2157: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx)) 2158: SHFT(*fb,*fc,fu,(*func)(u)) 2159: #ifdef DEBUG 2160: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx)); 2161: #endif 2162: } 2163: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */ 2164: #ifdef DEBUG 2165: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx); 2166: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx); 2167: #endif 2168: u=ulim; 2169: fu=(*func)(u); 2170: } else { /* u could be left to b (if r > q parabola has a maximum) */ 2171: #ifdef DEBUG 2172: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q); 2173: 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); 2174: #endif 2175: u=(*cx)+GOLD*(*cx-*bx); 2176: fu=(*func)(u); 2177: #ifdef DEBUG 2178: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx); 2179: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx); 2180: #endif 2181: } /* end tests */ 2182: SHFT(*ax,*bx,*cx,u) 2183: SHFT(*fa,*fb,*fc,fu) 2184: #ifdef DEBUG 2185: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc); 2186: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc); 2187: #endif 2188: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */ 2189: } 2190: 2191: /*************** linmin ************************/ 2192: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and 2193: resets p to where the function func(p) takes on a minimum along the direction xi from p , 2194: and replaces xi by the actual vector displacement that p was moved. Also returns as fret 2195: the value of func at the returned location p . This is actually all accomplished by calling the 2196: routines mnbrak and brent .*/ 2197: int ncom; 2198: double *pcom,*xicom; 2199: double (*nrfunc)(double []); 2200: 2201: #ifdef LINMINORIGINAL 2202: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double [])) 2203: #else 2204: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat) 2205: #endif 2206: { 2207: double brent(double ax, double bx, double cx, 2208: double (*f)(double), double tol, double *xmin); 2209: double f1dim(double x); 2210: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, 2211: double *fc, double (*func)(double)); 2212: int j; 2213: double xx,xmin,bx,ax; 2214: double fx,fb,fa; 2215: 2216: #ifdef LINMINORIGINAL 2217: #else 2218: double scale=10., axs, xxs; /* Scale added for infinity */ 2219: #endif 2220: 2221: ncom=n; 2222: pcom=vector(1,n); 2223: xicom=vector(1,n); 2224: nrfunc=func; 2225: for (j=1;j<=n;j++) { 2226: pcom[j]=p[j]; 2227: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */ 2228: } 2229: 2230: #ifdef LINMINORIGINAL 2231: xx=1.; 2232: #else 2233: axs=0.0; 2234: xxs=1.; 2235: do{ 2236: xx= xxs; 2237: #endif 2238: ax=0.; 2239: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */ 2240: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */ 2241: /* 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)) */ 2242: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */ 2243: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */ 2244: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */ 2245: /* 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]]*/ 2246: #ifdef LINMINORIGINAL 2247: #else 2248: if (fx != fx){ 2249: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */ 2250: printf("|"); 2251: fprintf(ficlog,"|"); 2252: #ifdef DEBUGLINMIN 2253: 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); 2254: #endif 2255: } 2256: }while(fx != fx && xxs > 1.e-5); 2257: #endif 2258: 2259: #ifdef DEBUGLINMIN 2260: 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); 2261: 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); 2262: #endif 2263: #ifdef LINMINORIGINAL 2264: #else 2265: if(fb == fx){ /* Flat function in the direction */ 2266: xmin=xx; 2267: *flat=1; 2268: }else{ 2269: *flat=0; 2270: #endif 2271: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */ 2272: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/ 2273: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */ 2274: /* fmin = f(p[j] + xmin * xi[j]) */ 2275: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */ 2276: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */ 2277: #ifdef DEBUG 2278: 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); 2279: 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); 2280: #endif 2281: #ifdef LINMINORIGINAL 2282: #else 2283: } 2284: #endif 2285: #ifdef DEBUGLINMIN 2286: printf("linmin end "); 2287: fprintf(ficlog,"linmin end "); 2288: #endif 2289: for (j=1;j<=n;j++) { 2290: #ifdef LINMINORIGINAL 2291: xi[j] *= xmin; 2292: #else 2293: #ifdef DEBUGLINMIN 2294: if(xxs <1.0) 2295: printf(" before xi[%d]=%12.8f", j,xi[j]); 2296: #endif 2297: 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) */ 2298: #ifdef DEBUGLINMIN 2299: if(xxs <1.0) 2300: 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 ); 2301: #endif 2302: #endif 2303: p[j] += xi[j]; /* Parameters values are updated accordingly */ 2304: } 2305: #ifdef DEBUGLINMIN 2306: printf("\n"); 2307: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p)); 2308: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p)); 2309: for (j=1;j<=n;j++) { 2310: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]); 2311: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]); 2312: if(j % ncovmodel == 0){ 2313: printf("\n"); 2314: fprintf(ficlog,"\n"); 2315: } 2316: } 2317: #else 2318: #endif 2319: free_vector(xicom,1,n); 2320: free_vector(pcom,1,n); 2321: } 2322: 2323: 2324: /*************** powell ************************/ 2325: /* 2326: Minimization of a function func of n variables. Input consists in an initial starting point 2327: p[1..n] ; an initial matrix xi[1..n][1..n] whose columns contain the initial set of di- 2328: rections (usually the n unit vectors); and ftol, the fractional tolerance in the function value 2329: such that failure to decrease by more than this amount in one iteration signals doneness. On 2330: output, p is set to the best point found, xi is the then-current direction set, fret is the returned 2331: function value at p , and iter is the number of iterations taken. The routine linmin is used. 2332: */ 2333: #ifdef LINMINORIGINAL 2334: #else 2335: int *flatdir; /* Function is vanishing in that direction */ 2336: int flat=0, flatd=0; /* Function is vanishing in that direction */ 2337: #endif 2338: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret, 2339: double (*func)(double [])) 2340: { 2341: #ifdef LINMINORIGINAL 2342: void linmin(double p[], double xi[], int n, double *fret, 2343: double (*func)(double [])); 2344: #else 2345: void linmin(double p[], double xi[], int n, double *fret, 2346: double (*func)(double []),int *flat); 2347: #endif 2348: int i,ibig,j,jk,k; 2349: double del,t,*pt,*ptt,*xit; 2350: double directest; 2351: double fp,fptt; 2352: double *xits; 2353: int niterf, itmp; 2354: #ifdef LINMINORIGINAL 2355: #else 2356: 2357: flatdir=ivector(1,n); 2358: for (j=1;j<=n;j++) flatdir[j]=0; 2359: #endif 2360: 2361: pt=vector(1,n); 2362: ptt=vector(1,n); 2363: xit=vector(1,n); 2364: xits=vector(1,n); 2365: *fret=(*func)(p); 2366: for (j=1;j<=n;j++) pt[j]=p[j]; 2367: rcurr_time = time(NULL); 2368: for (*iter=1;;++(*iter)) { 2369: fp=(*fret); /* From former iteration or initial value */ 2370: ibig=0; 2371: del=0.0; 2372: rlast_time=rcurr_time; 2373: /* (void) gettimeofday(&curr_time,&tzp); */ 2374: rcurr_time = time(NULL); 2375: curr_time = *localtime(&rcurr_time); 2376: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout); 2377: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog); 2378: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */ 2379: for (i=1;i<=n;i++) { 2380: fprintf(ficrespow," %.12lf", p[i]); 2381: } 2382: fprintf(ficrespow,"\n");fflush(ficrespow); 2383: printf("\n#model= 1 + age "); 2384: fprintf(ficlog,"\n#model= 1 + age "); 2385: if(nagesqr==1){ 2386: printf(" + age*age "); 2387: fprintf(ficlog," + age*age "); 2388: } 2389: for(j=1;j <=ncovmodel-2;j++){ 2390: if(Typevar[j]==0) { 2391: printf(" + V%d ",Tvar[j]); 2392: fprintf(ficlog," + V%d ",Tvar[j]); 2393: }else if(Typevar[j]==1) { 2394: printf(" + V%d*age ",Tvar[j]); 2395: fprintf(ficlog," + V%d*age ",Tvar[j]); 2396: }else if(Typevar[j]==2) { 2397: printf(" + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]); 2398: fprintf(ficlog," + V%d*V%d ",Tvard[Tposprod[j]][1],Tvard[Tposprod[j]][2]); 2399: } 2400: } 2401: printf("\n"); 2402: /* printf("12 47.0114589 0.0154322 33.2424412 0.3279905 2.3731903 */ 2403: /* 13 -21.5392400 0.1118147 1.2680506 1.2973408 -1.0663662 */ 2404: fprintf(ficlog,"\n"); 2405: for(i=1,jk=1; i <=nlstate; i++){ 2406: for(k=1; k <=(nlstate+ndeath); k++){ 2407: if (k != i) { 2408: printf("%d%d ",i,k); 2409: fprintf(ficlog,"%d%d ",i,k); 2410: for(j=1; j <=ncovmodel; j++){ 2411: printf("%12.7f ",p[jk]); 2412: fprintf(ficlog,"%12.7f ",p[jk]); 2413: jk++; 2414: } 2415: printf("\n"); 2416: fprintf(ficlog,"\n"); 2417: } 2418: } 2419: } 2420: if(*iter <=3 && *iter >1){ 2421: tml = *localtime(&rcurr_time); 2422: strcpy(strcurr,asctime(&tml)); 2423: rforecast_time=rcurr_time; 2424: itmp = strlen(strcurr); 2425: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */ 2426: strcurr[itmp-1]='\0'; 2427: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time); 2428: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time); 2429: for(niterf=10;niterf<=30;niterf+=10){ 2430: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time); 2431: forecast_time = *localtime(&rforecast_time); 2432: strcpy(strfor,asctime(&forecast_time)); 2433: itmp = strlen(strfor); 2434: if(strfor[itmp-1]=='\n') 2435: strfor[itmp-1]='\0'; 2436: 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); 2437: 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); 2438: } 2439: } 2440: for (i=1;i<=n;i++) { /* For each direction i */ 2441: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */ 2442: fptt=(*fret); 2443: #ifdef DEBUG 2444: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret); 2445: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret); 2446: #endif 2447: printf("%d",i);fflush(stdout); /* print direction (parameter) i */ 2448: fprintf(ficlog,"%d",i);fflush(ficlog); 2449: #ifdef LINMINORIGINAL 2450: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/ 2451: #else 2452: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/ 2453: flatdir[i]=flat; /* Function is vanishing in that direction i */ 2454: #endif 2455: /* Outputs are fret(new point p) p is updated and xit rescaled */ 2456: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */ 2457: /* because that direction will be replaced unless the gain del is small */ 2458: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */ 2459: /* Unless the n directions are conjugate some gain in the determinant may be obtained */ 2460: /* with the new direction. */ 2461: del=fabs(fptt-(*fret)); 2462: ibig=i; 2463: } 2464: #ifdef DEBUG 2465: printf("%d %.12e",i,(*fret)); 2466: fprintf(ficlog,"%d %.12e",i,(*fret)); 2467: for (j=1;j<=n;j++) { 2468: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5); 2469: printf(" x(%d)=%.12e",j,xit[j]); 2470: fprintf(ficlog," x(%d)=%.12e",j,xit[j]); 2471: } 2472: for(j=1;j<=n;j++) { 2473: printf(" p(%d)=%.12e",j,p[j]); 2474: fprintf(ficlog," p(%d)=%.12e",j,p[j]); 2475: } 2476: printf("\n"); 2477: fprintf(ficlog,"\n"); 2478: #endif 2479: } /* end loop on each direction i */ 2480: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */ 2481: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */ 2482: /* New value of last point Pn is not computed, P(n-1) */ 2483: for(j=1;j<=n;j++) { 2484: if(flatdir[j] >0){ 2485: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]); 2486: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]); 2487: } 2488: /* printf("\n"); */ 2489: /* fprintf(ficlog,"\n"); */ 2490: } 2491: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /\* Did we reach enough precision? *\/ */ 2492: if (2.0*fabs(fp-(*fret)) <= ftol) { /* Did we reach enough precision? */ 2493: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */ 2494: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */ 2495: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */ 2496: /* decreased of more than 3.84 */ 2497: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */ 2498: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */ 2499: /* By adding 10 parameters more the gain should be 18.31 */ 2500: 2501: /* Starting the program with initial values given by a former maximization will simply change */ 2502: /* the scales of the directions and the directions, because the are reset to canonical directions */ 2503: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */ 2504: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */ 2505: #ifdef DEBUG 2506: int k[2],l; 2507: k[0]=1; 2508: k[1]=-1; 2509: printf("Max: %.12e",(*func)(p)); 2510: fprintf(ficlog,"Max: %.12e",(*func)(p)); 2511: for (j=1;j<=n;j++) { 2512: printf(" %.12e",p[j]); 2513: fprintf(ficlog," %.12e",p[j]); 2514: } 2515: printf("\n"); 2516: fprintf(ficlog,"\n"); 2517: for(l=0;l<=1;l++) { 2518: for (j=1;j<=n;j++) { 2519: ptt[j]=p[j]+(p[j]-pt[j])*k[l]; 2520: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]); 2521: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]); 2522: } 2523: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p))); 2524: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p))); 2525: } 2526: #endif 2527: 2528: #ifdef LINMINORIGINAL 2529: #else 2530: free_ivector(flatdir,1,n); 2531: #endif 2532: free_vector(xit,1,n); 2533: free_vector(xits,1,n); 2534: free_vector(ptt,1,n); 2535: free_vector(pt,1,n); 2536: return; 2537: } /* enough precision */ 2538: if (*iter == ITMAX*n) nrerror("powell exceeding maximum iterations."); 2539: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */ 2540: ptt[j]=2.0*p[j]-pt[j]; 2541: xit[j]=p[j]-pt[j]; 2542: pt[j]=p[j]; 2543: } 2544: fptt=(*func)(ptt); /* f_3 */ 2545: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */ 2546: if (*iter <=4) { 2547: #else 2548: #endif 2549: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */ 2550: #else 2551: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */ 2552: #endif 2553: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */ 2554: /* From x1 (P0) distance of x2 is at h and x3 is 2h */ 2555: /* Let f"(x2) be the 2nd derivative equal everywhere. */ 2556: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */ 2557: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */ 2558: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */ 2559: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */ 2560: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */ 2561: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */ 2562: /* Even if f3 <f1, directest can be negative and t >0 */ 2563: /* mu² and del² are equal when f3=f1 */ 2564: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */ 2565: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */ 2566: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */ 2567: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */ 2568: #ifdef NRCORIGINAL 2569: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/ 2570: #else 2571: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */ 2572: t= t- del*SQR(fp-fptt); 2573: #endif 2574: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */ 2575: #ifdef DEBUG 2576: 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); 2577: 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); 2578: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt), 2579: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt)); 2580: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt), 2581: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt)); 2582: 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); 2583: 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); 2584: #endif 2585: #ifdef POWELLORIGINAL 2586: if (t < 0.0) { /* Then we use it for new direction */ 2587: #else 2588: if (directest*t < 0.0) { /* Contradiction between both tests */ 2589: 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); 2590: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt); 2591: 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); 2592: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt); 2593: } 2594: if (directest < 0.0) { /* Then we use it for new direction */ 2595: #endif 2596: #ifdef DEBUGLINMIN 2597: printf("Before linmin in direction P%d-P0\n",n); 2598: for (j=1;j<=n;j++) { 2599: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]); 2600: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]); 2601: if(j % ncovmodel == 0){ 2602: printf("\n"); 2603: fprintf(ficlog,"\n"); 2604: } 2605: } 2606: #endif 2607: #ifdef LINMINORIGINAL 2608: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/ 2609: #else 2610: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/ 2611: flatdir[i]=flat; /* Function is vanishing in that direction i */ 2612: #endif 2613: 2614: #ifdef DEBUGLINMIN 2615: for (j=1;j<=n;j++) { 2616: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]); 2617: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]); 2618: if(j % ncovmodel == 0){ 2619: printf("\n"); 2620: fprintf(ficlog,"\n"); 2621: } 2622: } 2623: #endif 2624: for (j=1;j<=n;j++) { 2625: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */ 2626: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */ 2627: } 2628: #ifdef LINMINORIGINAL 2629: #else 2630: for (j=1, flatd=0;j<=n;j++) { 2631: if(flatdir[j]>0) 2632: flatd++; 2633: } 2634: if(flatd >0){ 2635: printf("%d flat directions: ",flatd); 2636: fprintf(ficlog,"%d flat directions :",flatd); 2637: for (j=1;j<=n;j++) { 2638: if(flatdir[j]>0){ 2639: printf("%d ",j); 2640: fprintf(ficlog,"%d ",j); 2641: } 2642: } 2643: printf("\n"); 2644: fprintf(ficlog,"\n"); 2645: } 2646: #endif 2647: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig); 2648: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig); 2649: 2650: #ifdef DEBUG 2651: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig); 2652: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig); 2653: for(j=1;j<=n;j++){ 2654: printf(" %lf",xit[j]); 2655: fprintf(ficlog," %lf",xit[j]); 2656: } 2657: printf("\n"); 2658: fprintf(ficlog,"\n"); 2659: #endif 2660: } /* end of t or directest negative */ 2661: #ifdef POWELLNOF3INFF1TEST 2662: #else 2663: } /* end if (fptt < fp) */ 2664: #endif 2665: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */ 2666: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */ 2667: #else 2668: #endif 2669: } /* loop iteration */ 2670: } 2671: 2672: /**** Prevalence limit (stable or period prevalence) ****************/ 2673: 2674: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij, int nres) 2675: { 2676: /**< Computes the prevalence limit in each live state at age x and for covariate combination ij 2677: * (and selected quantitative values in nres) 2678: * by left multiplying the unit 2679: * matrix by transitions matrix until convergence is reached with precision ftolpl 2680: * Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I 2681: * Wx is row vector: population in state 1, population in state 2, population dead 2682: * or prevalence in state 1, prevalence in state 2, 0 2683: * newm is the matrix after multiplications, its rows are identical at a factor. 2684: * Inputs are the parameter, age, a tolerance for the prevalence limit ftolpl. 2685: * Output is prlim. 2686: * Initial matrix pimij 2687: */ 2688: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */ 2689: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */ 2690: /* 0, 0 , 1} */ 2691: /* 2692: * and after some iteration: */ 2693: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */ 2694: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */ 2695: /* 0, 0 , 1} */ 2696: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */ 2697: /* {0.51571254859325999, 0.4842874514067399, */ 2698: /* 0.51326036147820708, 0.48673963852179264} */ 2699: /* If we start from prlim again, prlim tends to a constant matrix */ 2700: 2701: int i, ii,j,k; 2702: double *min, *max, *meandiff, maxmax,sumnew=0.; 2703: /* double **matprod2(); */ /* test */ 2704: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */ 2705: double **newm; 2706: double agefin, delaymax=200. ; /* 100 Max number of years to converge */ 2707: int ncvloop=0; 2708: int first=0; 2709: 2710: min=vector(1,nlstate); 2711: max=vector(1,nlstate); 2712: meandiff=vector(1,nlstate); 2713: 2714: /* Starting with matrix unity */ 2715: for (ii=1;ii<=nlstate+ndeath;ii++) 2716: for (j=1;j<=nlstate+ndeath;j++){ 2717: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 2718: } 2719: 2720: cov[1]=1.; 2721: 2722: /* Even if hstepm = 1, at least one multiplication by the unit matrix */ 2723: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */ 2724: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){ 2725: ncvloop++; 2726: newm=savm; 2727: /* Covariates have to be included here again */ 2728: cov[2]=agefin; 2729: if(nagesqr==1) 2730: cov[3]= agefin*agefin;; 2731: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */ 2732: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */ 2733: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)]; 2734: /* 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)); */ 2735: } 2736: for (k=1; k<=nsq;k++) { /* For single varying covariates only */ 2737: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */ 2738: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; 2739: /* 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]); */ 2740: } 2741: for (k=1; k<=cptcovage;k++){ /* For product with age */ 2742: if(Dummy[Tvar[Tage[k]]]){ 2743: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; 2744: } else{ 2745: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; 2746: } 2747: /* 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]); */ 2748: } 2749: for (k=1; k<=cptcovprod;k++){ /* For product without age */ 2750: /* 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]); */ 2751: if(Dummy[Tvard[k][1]==0]){ 2752: if(Dummy[Tvard[k][2]==0]){ 2753: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; 2754: }else{ 2755: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; 2756: } 2757: }else{ 2758: if(Dummy[Tvard[k][2]==0]){ 2759: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; 2760: }else{ 2761: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; 2762: } 2763: } 2764: } 2765: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/ 2766: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/ 2767: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/ 2768: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */ 2769: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */ 2770: /* age and covariate values of ij are in 'cov' */ 2771: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */ 2772: 2773: savm=oldm; 2774: oldm=newm; 2775: 2776: for(j=1; j<=nlstate; j++){ 2777: max[j]=0.; 2778: min[j]=1.; 2779: } 2780: for(i=1;i<=nlstate;i++){ 2781: sumnew=0; 2782: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k]; 2783: for(j=1; j<=nlstate; j++){ 2784: prlim[i][j]= newm[i][j]/(1-sumnew); 2785: max[j]=FMAX(max[j],prlim[i][j]); 2786: min[j]=FMIN(min[j],prlim[i][j]); 2787: } 2788: } 2789: 2790: maxmax=0.; 2791: for(j=1; j<=nlstate; j++){ 2792: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */ 2793: maxmax=FMAX(maxmax,meandiff[j]); 2794: /* 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); */ 2795: } /* j loop */ 2796: *ncvyear= (int)age- (int)agefin; 2797: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */ 2798: if(maxmax < ftolpl){ 2799: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */ 2800: free_vector(min,1,nlstate); 2801: free_vector(max,1,nlstate); 2802: free_vector(meandiff,1,nlstate); 2803: return prlim; 2804: } 2805: } /* agefin loop */ 2806: /* After some age loop it doesn't converge */ 2807: if(!first){ 2808: first=1; 2809: 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); 2810: 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); 2811: }else if (first >=1 && first <10){ 2812: 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); 2813: first++; 2814: }else if (first ==10){ 2815: 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); 2816: printf("Warning: the stable prevalence dit not converge. This warning came too often, IMaCh will stop notifying, even in its log file. Look at the graphs to appreciate the non convergence.\n"); 2817: fprintf(ficlog,"Warning: the stable prevalence no convergence; too many cases, giving up noticing, even in log file\n"); 2818: first++; 2819: } 2820: 2821: /* 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); */ 2822: free_vector(min,1,nlstate); 2823: free_vector(max,1,nlstate); 2824: free_vector(meandiff,1,nlstate); 2825: 2826: return prlim; /* should not reach here */ 2827: } 2828: 2829: 2830: /**** Back Prevalence limit (stable or period prevalence) ****************/ 2831: 2832: /* 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) */ 2833: /* 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) */ 2834: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij, int nres) 2835: { 2836: /* Computes the prevalence limit in each live state at age x and for covariate combination ij (<=2**cptcoveff) by left multiplying the unit 2837: matrix by transitions matrix until convergence is reached with precision ftolpl */ 2838: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */ 2839: /* Wx is row vector: population in state 1, population in state 2, population dead */ 2840: /* or prevalence in state 1, prevalence in state 2, 0 */ 2841: /* newm is the matrix after multiplications, its rows are identical at a factor */ 2842: /* Initial matrix pimij */ 2843: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */ 2844: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */ 2845: /* 0, 0 , 1} */ 2846: /* 2847: * and after some iteration: */ 2848: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */ 2849: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */ 2850: /* 0, 0 , 1} */ 2851: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */ 2852: /* {0.51571254859325999, 0.4842874514067399, */ 2853: /* 0.51326036147820708, 0.48673963852179264} */ 2854: /* If we start from prlim again, prlim tends to a constant matrix */ 2855: 2856: int i, ii,j,k; 2857: int first=0; 2858: double *min, *max, *meandiff, maxmax,sumnew=0.; 2859: /* double **matprod2(); */ /* test */ 2860: double **out, cov[NCOVMAX+1], **bmij(); 2861: double **newm; 2862: double **dnewm, **doldm, **dsavm; /* for use */ 2863: double **oldm, **savm; /* for use */ 2864: 2865: double agefin, delaymax=200. ; /* 100 Max number of years to converge */ 2866: int ncvloop=0; 2867: 2868: min=vector(1,nlstate); 2869: max=vector(1,nlstate); 2870: meandiff=vector(1,nlstate); 2871: 2872: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms; 2873: oldm=oldms; savm=savms; 2874: 2875: /* Starting with matrix unity */ 2876: for (ii=1;ii<=nlstate+ndeath;ii++) 2877: for (j=1;j<=nlstate+ndeath;j++){ 2878: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 2879: } 2880: 2881: cov[1]=1.; 2882: 2883: /* Even if hstepm = 1, at least one multiplication by the unit matrix */ 2884: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */ 2885: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */ 2886: /* for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */ 2887: for(agefin=age; agefin<FMIN(AGESUP,age+delaymax); agefin=agefin+stepm/YEARM){ /* A changer en age */ 2888: ncvloop++; 2889: newm=savm; /* oldm should be kept from previous iteration or unity at start */ 2890: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */ 2891: /* Covariates have to be included here again */ 2892: cov[2]=agefin; 2893: if(nagesqr==1) 2894: cov[3]= agefin*agefin;; 2895: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */ 2896: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */ 2897: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)]; 2898: /* 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)); */ 2899: } 2900: /* for (k=1; k<=cptcovn;k++) { */ 2901: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */ 2902: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */ 2903: /* /\* 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])]); *\/ */ 2904: /* } */ 2905: for (k=1; k<=nsq;k++) { /* For single varying covariates only */ 2906: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */ 2907: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; 2908: /* 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]); */ 2909: } 2910: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2]; */ 2911: /* for (k=1; k<=cptcovprod;k++) /\* Useless *\/ */ 2912: /* /\* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; *\/ */ 2913: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; */ 2914: for (k=1; k<=cptcovage;k++){ /* For product with age */ 2915: if(Dummy[Tvar[Tage[k]]]){ 2916: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; 2917: } else{ 2918: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; 2919: } 2920: /* 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]); */ 2921: } 2922: for (k=1; k<=cptcovprod;k++){ /* For product without age */ 2923: /* 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]); */ 2924: if(Dummy[Tvard[k][1]==0]){ 2925: if(Dummy[Tvard[k][2]==0]){ 2926: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; 2927: }else{ 2928: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * Tqresult[nres][k]; 2929: } 2930: }else{ 2931: if(Dummy[Tvard[k][2]==0]){ 2932: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][2]][codtabm(ij,k)] * Tqinvresult[nres][Tvard[k][1]]; 2933: }else{ 2934: cov[2+nagesqr+Tprod[k]]=Tqinvresult[nres][Tvard[k][1]]* Tqinvresult[nres][Tvard[k][2]]; 2935: } 2936: } 2937: } 2938: 2939: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/ 2940: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/ 2941: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/ 2942: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */ 2943: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */ 2944: /* ij should be linked to the correct index of cov */ 2945: /* age and covariate values ij are in 'cov', but we need to pass 2946: * ij for the observed prevalence at age and status and covariate 2947: * number: prevacurrent[(int)agefin][ii][ij] 2948: */ 2949: /* 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 *\/ */ 2950: /* 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 *\/ */ 2951: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */ 2952: /* if((int)age == 86 || (int)age == 87){ */ 2953: /* printf(" Backward prevalim age=%d agefin=%d \n", (int) age, (int) agefin); */ 2954: /* for(i=1; i<=nlstate+ndeath; i++) { */ 2955: /* printf("%d newm= ",i); */ 2956: /* for(j=1;j<=nlstate+ndeath;j++) { */ 2957: /* printf("%f ",newm[i][j]); */ 2958: /* } */ 2959: /* printf("oldm * "); */ 2960: /* for(j=1;j<=nlstate+ndeath;j++) { */ 2961: /* printf("%f ",oldm[i][j]); */ 2962: /* } */ 2963: /* printf(" bmmij "); */ 2964: /* for(j=1;j<=nlstate+ndeath;j++) { */ 2965: /* printf("%f ",pmmij[i][j]); */ 2966: /* } */ 2967: /* printf("\n"); */ 2968: /* } */ 2969: /* } */ 2970: savm=oldm; 2971: oldm=newm; 2972: 2973: for(j=1; j<=nlstate; j++){ 2974: max[j]=0.; 2975: min[j]=1.; 2976: } 2977: for(j=1; j<=nlstate; j++){ 2978: for(i=1;i<=nlstate;i++){ 2979: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */ 2980: bprlim[i][j]= newm[i][j]; 2981: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */ 2982: min[i]=FMIN(min[i],bprlim[i][j]); 2983: } 2984: } 2985: 2986: maxmax=0.; 2987: for(i=1; i<=nlstate; i++){ 2988: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */ 2989: maxmax=FMAX(maxmax,meandiff[i]); 2990: /* 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); */ 2991: } /* i loop */ 2992: *ncvyear= -( (int)age- (int)agefin); 2993: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */ 2994: if(maxmax < ftolpl){ 2995: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */ 2996: free_vector(min,1,nlstate); 2997: free_vector(max,1,nlstate); 2998: free_vector(meandiff,1,nlstate); 2999: return bprlim; 3000: } 3001: } /* agefin loop */ 3002: /* After some age loop it doesn't converge */ 3003: if(!first){ 3004: first=1; 3005: 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\ 3006: 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); 3007: } 3008: 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\ 3009: 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); 3010: /* 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); */ 3011: free_vector(min,1,nlstate); 3012: free_vector(max,1,nlstate); 3013: free_vector(meandiff,1,nlstate); 3014: 3015: return bprlim; /* should not reach here */ 3016: } 3017: 3018: /*************** transition probabilities ***************/ 3019: 3020: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate ) 3021: { 3022: /* According to parameters values stored in x and the covariate's values stored in cov, 3023: computes the probability to be observed in state j (after stepm years) being in state i by appying the 3024: model to the ncovmodel covariates (including constant and age). 3025: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc] 3026: and, according on how parameters are entered, the position of the coefficient xij(nc) of the 3027: ncth covariate in the global vector x is given by the formula: 3028: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel 3029: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel 3030: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation, 3031: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij. 3032: Outputs ps[i][j] or probability to be observed in j being in i according to 3033: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij] 3034: Sum on j ps[i][j] should equal to 1. 3035: */ 3036: double s1, lnpijopii; 3037: /*double t34;*/ 3038: int i,j, nc, ii, jj; 3039: 3040: for(i=1; i<= nlstate; i++){ 3041: for(j=1; j<i;j++){ 3042: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){ 3043: /*lnpijopii += param[i][j][nc]*cov[nc];*/ 3044: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc]; 3045: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */ 3046: } 3047: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */ 3048: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */ 3049: } 3050: for(j=i+1; j<=nlstate+ndeath;j++){ 3051: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){ 3052: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/ 3053: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc]; 3054: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */ 3055: } 3056: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */ 3057: } 3058: } 3059: 3060: for(i=1; i<= nlstate; i++){ 3061: s1=0; 3062: for(j=1; j<i; j++){ 3063: s1+=exp(ps[i][j]); /* In fact sums pij/pii */ 3064: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */ 3065: } 3066: for(j=i+1; j<=nlstate+ndeath; j++){ 3067: s1+=exp(ps[i][j]); /* In fact sums pij/pii */ 3068: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */ 3069: } 3070: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */ 3071: ps[i][i]=1./(s1+1.); 3072: /* Computing other pijs */ 3073: for(j=1; j<i; j++) 3074: ps[i][j]= exp(ps[i][j])*ps[i][i]; 3075: for(j=i+1; j<=nlstate+ndeath; j++) 3076: ps[i][j]= exp(ps[i][j])*ps[i][i]; 3077: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */ 3078: } /* end i */ 3079: 3080: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){ 3081: for(jj=1; jj<= nlstate+ndeath; jj++){ 3082: ps[ii][jj]=0; 3083: ps[ii][ii]=1; 3084: } 3085: } 3086: 3087: 3088: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */ 3089: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */ 3090: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */ 3091: /* } */ 3092: /* printf("\n "); */ 3093: /* } */ 3094: /* printf("\n ");printf("%lf ",cov[2]);*/ 3095: /* 3096: for(i=1; i<= npar; i++) printf("%f ",x[i]); 3097: goto end;*/ 3098: return ps; /* Pointer is unchanged since its call */ 3099: } 3100: 3101: /*************** backward transition probabilities ***************/ 3102: 3103: /* 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 ) */ 3104: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */ 3105: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij ) 3106: { 3107: /* Computes the backward probability at age agefin, cov[2], and covariate combination 'ij'. In fact cov is already filled and x too. 3108: * Call to pmij(cov and x), call to cross prevalence, sums and inverses, left multiply, and returns in **ps as well as **bmij. 3109: */ 3110: int i, ii, j,k; 3111: 3112: double **out, **pmij(); 3113: double sumnew=0.; 3114: double agefin; 3115: double k3=0.; /* constant of the w_x diagonal matrix (in order for B to sum to 1 even for death state) */ 3116: double **dnewm, **dsavm, **doldm; 3117: double **bbmij; 3118: 3119: doldm=ddoldms; /* global pointers */ 3120: dnewm=ddnewms; 3121: dsavm=ddsavms; 3122: 3123: agefin=cov[2]; 3124: /* Bx = Diag(w_x) P_x Diag(Sum_i w^i_x p^ij_x */ 3125: /* bmij *//* age is cov[2], ij is included in cov, but we need for 3126: the observed prevalence (with this covariate ij) at beginning of transition */ 3127: /* dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate); */ 3128: 3129: /* P_x */ 3130: pmmij=pmij(pmmij,cov,ncovmodel,x,nlstate); /*This is forward probability from agefin to agefin + stepm */ 3131: /* outputs pmmij which is a stochastic matrix in row */ 3132: 3133: /* Diag(w_x) */ 3134: /* Rescaling the cross-sectional prevalence: Problem with prevacurrent which can be zero */ 3135: sumnew=0.; 3136: /*for (ii=1;ii<=nlstate+ndeath;ii++){*/ 3137: for (ii=1;ii<=nlstate;ii++){ /* Only on live states */ 3138: /* printf(" agefin=%d, ii=%d, ij=%d, prev=%f\n",(int)agefin,ii, ij, prevacurrent[(int)agefin][ii][ij]); */ 3139: sumnew+=prevacurrent[(int)agefin][ii][ij]; 3140: } 3141: if(sumnew >0.01){ /* At least some value in the prevalence */ 3142: for (ii=1;ii<=nlstate+ndeath;ii++){ 3143: for (j=1;j<=nlstate+ndeath;j++) 3144: doldm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij]/sumnew : 0.0); 3145: } 3146: }else{ 3147: for (ii=1;ii<=nlstate+ndeath;ii++){ 3148: for (j=1;j<=nlstate+ndeath;j++) 3149: doldm[ii][j]=(ii==j ? 1./nlstate : 0.0); 3150: } 3151: /* if(sumnew <0.9){ */ 3152: /* printf("Problem internal bmij B: sum on i wi <0.9: j=%d, sum_i wi=%lf,agefin=%d\n",j,sumnew, (int)agefin); */ 3153: /* } */ 3154: } 3155: k3=0.0; /* We put the last diagonal to 0 */ 3156: for (ii=nlstate+1;ii<=nlstate+ndeath;ii++){ 3157: doldm[ii][ii]= k3; 3158: } 3159: /* End doldm, At the end doldm is diag[(w_i)] */ 3160: 3161: /* Left product of this diag matrix by pmmij=Px (dnewm=dsavm*doldm): diag[(w_i)*Px */ 3162: bbmij=matprod2(dnewm, doldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, pmmij); /* was a Bug Valgrind */ 3163: 3164: /* Diag(Sum_i w^i_x p^ij_x, should be the prevalence at age x+stepm */ 3165: /* w1 p11 + w2 p21 only on live states N1./N..*N11/N1. + N2./N..*N21/N2.=(N11+N21)/N..=N.1/N.. */ 3166: for (j=1;j<=nlstate+ndeath;j++){ 3167: sumnew=0.; 3168: for (ii=1;ii<=nlstate;ii++){ 3169: /* sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij]; */ 3170: sumnew+=pmmij[ii][j]*doldm[ii][ii]; /* Yes prevalence at beginning of transition */ 3171: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */ 3172: for (ii=1;ii<=nlstate+ndeath;ii++){ 3173: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */ 3174: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */ 3175: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */ 3176: /* dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); */ 3177: /* }else */ 3178: dsavm[ii][j]=(ii==j ? 1./sumnew : 0.0); 3179: } /*End ii */ 3180: } /* 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 */ 3181: 3182: ps=matprod2(ps, dnewm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dsavm); /* was a Bug Valgrind */ 3183: /* ps is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */ 3184: /* end bmij */ 3185: return ps; /*pointer is unchanged */ 3186: } 3187: /*************** transition probabilities ***************/ 3188: 3189: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate ) 3190: { 3191: /* According to parameters values stored in x and the covariate's values stored in cov, 3192: computes the probability to be observed in state j being in state i by appying the 3193: model to the ncovmodel covariates (including constant and age). 3194: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc] 3195: and, according on how parameters are entered, the position of the coefficient xij(nc) of the 3196: ncth covariate in the global vector x is given by the formula: 3197: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel 3198: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel 3199: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation, 3200: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij. 3201: Outputs ps[i][j] the probability to be observed in j being in j according to 3202: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij] 3203: */ 3204: double s1, lnpijopii; 3205: /*double t34;*/ 3206: int i,j, nc, ii, jj; 3207: 3208: for(i=1; i<= nlstate; i++){ 3209: for(j=1; j<i;j++){ 3210: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){ 3211: /*lnpijopii += param[i][j][nc]*cov[nc];*/ 3212: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc]; 3213: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */ 3214: } 3215: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */ 3216: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */ 3217: } 3218: for(j=i+1; j<=nlstate+ndeath;j++){ 3219: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){ 3220: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/ 3221: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc]; 3222: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */ 3223: } 3224: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */ 3225: } 3226: } 3227: 3228: for(i=1; i<= nlstate; i++){ 3229: s1=0; 3230: for(j=1; j<i; j++){ 3231: s1+=exp(ps[i][j]); /* In fact sums pij/pii */ 3232: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */ 3233: } 3234: for(j=i+1; j<=nlstate+ndeath; j++){ 3235: s1+=exp(ps[i][j]); /* In fact sums pij/pii */ 3236: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */ 3237: } 3238: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */ 3239: ps[i][i]=1./(s1+1.); 3240: /* Computing other pijs */ 3241: for(j=1; j<i; j++) 3242: ps[i][j]= exp(ps[i][j])*ps[i][i]; 3243: for(j=i+1; j<=nlstate+ndeath; j++) 3244: ps[i][j]= exp(ps[i][j])*ps[i][i]; 3245: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */ 3246: } /* end i */ 3247: 3248: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){ 3249: for(jj=1; jj<= nlstate+ndeath; jj++){ 3250: ps[ii][jj]=0; 3251: ps[ii][ii]=1; 3252: } 3253: } 3254: /* Added for prevbcast */ /* Transposed matrix too */ 3255: for(jj=1; jj<= nlstate+ndeath; jj++){ 3256: s1=0.; 3257: for(ii=1; ii<= nlstate+ndeath; ii++){ 3258: s1+=ps[ii][jj]; 3259: } 3260: for(ii=1; ii<= nlstate; ii++){ 3261: ps[ii][jj]=ps[ii][jj]/s1; 3262: } 3263: } 3264: /* Transposition */ 3265: for(jj=1; jj<= nlstate+ndeath; jj++){ 3266: for(ii=jj; ii<= nlstate+ndeath; ii++){ 3267: s1=ps[ii][jj]; 3268: ps[ii][jj]=ps[jj][ii]; 3269: ps[jj][ii]=s1; 3270: } 3271: } 3272: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */ 3273: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */ 3274: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */ 3275: /* } */ 3276: /* printf("\n "); */ 3277: /* } */ 3278: /* printf("\n ");printf("%lf ",cov[2]);*/ 3279: /* 3280: for(i=1; i<= npar; i++) printf("%f ",x[i]); 3281: goto end;*/ 3282: return ps; 3283: } 3284: 3285: 3286: /**************** Product of 2 matrices ******************/ 3287: 3288: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b) 3289: { 3290: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times 3291: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */ 3292: /* in, b, out are matrice of pointers which should have been initialized 3293: before: only the contents of out is modified. The function returns 3294: a pointer to pointers identical to out */ 3295: int i, j, k; 3296: for(i=nrl; i<= nrh; i++) 3297: for(k=ncolol; k<=ncoloh; k++){ 3298: out[i][k]=0.; 3299: for(j=ncl; j<=nch; j++) 3300: out[i][k] +=in[i][j]*b[j][k]; 3301: } 3302: return out; 3303: } 3304: 3305: 3306: /************* Higher Matrix Product ***************/ 3307: 3308: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij, int nres ) 3309: { 3310: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over 3311: 'nhstepm*hstepm*stepm' months (i.e. until 3312: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying 3313: nhstepm*hstepm matrices. 3314: Output is stored in matrix po[i][j][h] for h every 'hstepm' step 3315: (typically every 2 years instead of every month which is too big 3316: for the memory). 3317: Model is determined by parameters x and covariates have to be 3318: included manually here. 3319: 3320: */ 3321: 3322: int i, j, d, h, k; 3323: double **out, cov[NCOVMAX+1]; 3324: double **newm; 3325: double agexact; 3326: double agebegin, ageend; 3327: 3328: /* Hstepm could be zero and should return the unit matrix */ 3329: for (i=1;i<=nlstate+ndeath;i++) 3330: for (j=1;j<=nlstate+ndeath;j++){ 3331: oldm[i][j]=(i==j ? 1.0 : 0.0); 3332: po[i][j][0]=(i==j ? 1.0 : 0.0); 3333: } 3334: /* Even if hstepm = 1, at least one multiplication by the unit matrix */ 3335: for(h=1; h <=nhstepm; h++){ 3336: for(d=1; d <=hstepm; d++){ 3337: newm=savm; 3338: /* Covariates have to be included here again */ 3339: cov[1]=1.; 3340: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */ 3341: cov[2]=agexact; 3342: if(nagesqr==1) 3343: cov[3]= agexact*agexact; 3344: for (k=1; k<=nsd;k++) { /* For single dummy covariates only */ 3345: /* Here comes the value of the covariate 'ij' after renumbering k with single dummy covariates */ 3346: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)]; 3347: /* 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)); */ 3348: } 3349: for (k=1; k<=nsq;k++) { /* For single varying covariates only */ 3350: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */ 3351: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; 3352: /* 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]); */ 3353: } 3354: for (k=1; k<=cptcovage;k++){ 3355: if(Dummy[Tvar[Tage[k]]]){ 3356: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; 3357: } else{ 3358: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; 3359: } 3360: /* 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]); */ 3361: } 3362: for (k=1; k<=cptcovprod;k++){ /* */ 3363: /* 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]); */ 3364: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)]; 3365: } 3366: /* for (k=1; k<=cptcovn;k++) */ 3367: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */ 3368: /* for (k=1; k<=cptcovage;k++) /\* Should start at cptcovn+1 *\/ */ 3369: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; */ 3370: /* for (k=1; k<=cptcovprod;k++) /\* Useless because included in cptcovn *\/ */ 3371: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; */ 3372: 3373: 3374: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/ 3375: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/ 3376: /* right multiplication of oldm by the current matrix */ 3377: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, 3378: pmij(pmmij,cov,ncovmodel,x,nlstate)); 3379: /* if((int)age == 70){ */ 3380: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */ 3381: /* for(i=1; i<=nlstate+ndeath; i++) { */ 3382: /* printf("%d pmmij ",i); */ 3383: /* for(j=1;j<=nlstate+ndeath;j++) { */ 3384: /* printf("%f ",pmmij[i][j]); */ 3385: /* } */ 3386: /* printf(" oldm "); */ 3387: /* for(j=1;j<=nlstate+ndeath;j++) { */ 3388: /* printf("%f ",oldm[i][j]); */ 3389: /* } */ 3390: /* printf("\n"); */ 3391: /* } */ 3392: /* } */ 3393: savm=oldm; 3394: oldm=newm; 3395: } 3396: for(i=1; i<=nlstate+ndeath; i++) 3397: for(j=1;j<=nlstate+ndeath;j++) { 3398: po[i][j][h]=newm[i][j]; 3399: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/ 3400: } 3401: /*printf("h=%d ",h);*/ 3402: } /* end h */ 3403: /* printf("\n H=%d \n",h); */ 3404: return po; 3405: } 3406: 3407: /************* Higher Back Matrix Product ***************/ 3408: /* 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 ) */ 3409: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij, int nres ) 3410: { 3411: /* For a combination of dummy covariate ij, computes the transition matrix starting at age 'age' over 3412: 'nhstepm*hstepm*stepm' months (i.e. until 3413: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying 3414: nhstepm*hstepm matrices. 3415: Output is stored in matrix po[i][j][h] for h every 'hstepm' step 3416: (typically every 2 years instead of every month which is too big 3417: for the memory). 3418: Model is determined by parameters x and covariates have to be 3419: included manually here. Then we use a call to bmij(x and cov) 3420: The addresss of po (p3mat allocated to the dimension of nhstepm) should be stored for output 3421: */ 3422: 3423: int i, j, d, h, k; 3424: double **out, cov[NCOVMAX+1], **bmij(); 3425: double **newm, ***newmm; 3426: double agexact; 3427: double agebegin, ageend; 3428: double **oldm, **savm; 3429: 3430: newmm=po; /* To be saved */ 3431: oldm=oldms;savm=savms; /* Global pointers */ 3432: /* Hstepm could be zero and should return the unit matrix */ 3433: for (i=1;i<=nlstate+ndeath;i++) 3434: for (j=1;j<=nlstate+ndeath;j++){ 3435: oldm[i][j]=(i==j ? 1.0 : 0.0); 3436: po[i][j][0]=(i==j ? 1.0 : 0.0); 3437: } 3438: /* Even if hstepm = 1, at least one multiplication by the unit matrix */ 3439: for(h=1; h <=nhstepm; h++){ 3440: for(d=1; d <=hstepm; d++){ 3441: newm=savm; 3442: /* Covariates have to be included here again */ 3443: cov[1]=1.; 3444: agexact=age-( (h-1)*hstepm + (d) )*stepm/YEARM; /* age just before transition, d or d-1? */ 3445: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */ 3446: cov[2]=agexact; 3447: if(nagesqr==1) 3448: cov[3]= agexact*agexact; 3449: for (k=1; k<=cptcovn;k++){ 3450: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)]; */ 3451: /* /\* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; *\/ */ 3452: cov[2+nagesqr+TvarsDind[k]]=nbcode[TvarsD[k]][codtabm(ij,k)]; 3453: /* 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)); */ 3454: } 3455: for (k=1; k<=nsq;k++) { /* For single varying covariates only */ 3456: /* Here comes the value of quantitative after renumbering k with single quantitative covariates */ 3457: cov[2+nagesqr+TvarsQind[k]]=Tqresult[nres][k]; 3458: /* 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]); */ 3459: } 3460: for (k=1; k<=cptcovage;k++){ /* Should start at cptcovn+1 */ 3461: if(Dummy[Tvar[Tage[k]]]){ 3462: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; 3463: } else{ 3464: cov[2+nagesqr+Tage[k]]=Tqresult[nres][k]; 3465: } 3466: /* 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]); */ 3467: } 3468: for (k=1; k<=cptcovprod;k++){ /* Useless because included in cptcovn */ 3469: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; 3470: } 3471: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/ 3472: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/ 3473: 3474: /* Careful transposed matrix */ 3475: /* age is in cov[2], prevacurrent at beginning of transition. */ 3476: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */ 3477: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */ 3478: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\ 3479: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); 3480: /* if((int)age == 70){ */ 3481: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */ 3482: /* for(i=1; i<=nlstate+ndeath; i++) { */ 3483: /* printf("%d pmmij ",i); */ 3484: /* for(j=1;j<=nlstate+ndeath;j++) { */ 3485: /* printf("%f ",pmmij[i][j]); */ 3486: /* } */ 3487: /* printf(" oldm "); */ 3488: /* for(j=1;j<=nlstate+ndeath;j++) { */ 3489: /* printf("%f ",oldm[i][j]); */ 3490: /* } */ 3491: /* printf("\n"); */ 3492: /* } */ 3493: /* } */ 3494: savm=oldm; 3495: oldm=newm; 3496: } 3497: for(i=1; i<=nlstate+ndeath; i++) 3498: for(j=1;j<=nlstate+ndeath;j++) { 3499: po[i][j][h]=newm[i][j]; 3500: /* if(h==nhstepm) */ 3501: /* printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]); */ 3502: } 3503: /* printf("h=%d %.1f ",h, agexact); */ 3504: } /* end h */ 3505: /* printf("\n H=%d nhs=%d \n",h, nhstepm); */ 3506: return po; 3507: } 3508: 3509: 3510: #ifdef NLOPT 3511: double myfunc(unsigned n, const double *p1, double *grad, void *pd){ 3512: double fret; 3513: double *xt; 3514: int j; 3515: myfunc_data *d2 = (myfunc_data *) pd; 3516: /* xt = (p1-1); */ 3517: xt=vector(1,n); 3518: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */ 3519: 3520: fret=(d2->function)(xt); /* p xt[1]@8 is fine */ 3521: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */ 3522: printf("Function = %.12lf ",fret); 3523: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]); 3524: printf("\n"); 3525: free_vector(xt,1,n); 3526: return fret; 3527: } 3528: #endif 3529: 3530: /*************** log-likelihood *************/ 3531: double func( double *x) 3532: { 3533: int i, ii, j, k, mi, d, kk; 3534: int ioffset=0; 3535: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1]; 3536: double **out; 3537: double lli; /* Individual log likelihood */ 3538: int s1, s2; 3539: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */ 3540: double bbh, survp; 3541: long ipmx; 3542: double agexact; 3543: /*extern weight */ 3544: /* We are differentiating ll according to initial status */ 3545: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/ 3546: /*for(i=1;i<imx;i++) 3547: printf(" %d\n",s[4][i]); 3548: */ 3549: 3550: ++countcallfunc; 3551: 3552: cov[1]=1.; 3553: 3554: for(k=1; k<=nlstate; k++) ll[k]=0.; 3555: ioffset=0; 3556: if(mle==1){ 3557: for (i=1,ipmx=0, sw=0.; i<=imx; i++){ 3558: /* Computes the values of the ncovmodel covariates of the model 3559: depending if the covariates are fixed or varying (age dependent) and stores them in cov[] 3560: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability 3561: to be observed in j being in i according to the model. 3562: */ 3563: ioffset=2+nagesqr ; 3564: /* Fixed */ 3565: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products */ 3566: 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)*/ 3567: } 3568: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4] 3569: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2] 3570: has been calculated etc */ 3571: /* For an individual i, wav[i] gives the number of effective waves */ 3572: /* We compute the contribution to Likelihood of each effective transition 3573: mw[mi][i] is real wave of the mi th effectve wave */ 3574: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i]; 3575: s2=s[mw[mi+1][i]][i]; 3576: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i] 3577: But if the variable is not in the model TTvar[iv] is the real variable effective in the model: 3578: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i] 3579: */ 3580: for(mi=1; mi<= wav[i]-1; mi++){ 3581: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/ 3582: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */ 3583: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; 3584: } 3585: for (ii=1;ii<=nlstate+ndeath;ii++) 3586: for (j=1;j<=nlstate+ndeath;j++){ 3587: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 3588: savm[ii][j]=(ii==j ? 1.0 : 0.0); 3589: } 3590: for(d=0; d<dh[mi][i]; d++){ 3591: newm=savm; 3592: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; 3593: cov[2]=agexact; 3594: if(nagesqr==1) 3595: cov[3]= agexact*agexact; /* Should be changed here */ 3596: for (kk=1; kk<=cptcovage;kk++) { 3597: if(!FixedV[Tvar[Tage[kk]]]) 3598: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */ 3599: else 3600: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact; 3601: } 3602: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, 3603: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); 3604: savm=oldm; 3605: oldm=newm; 3606: } /* end mult */ 3607: 3608: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */ 3609: /* But now since version 0.9 we anticipate for bias at large stepm. 3610: * If stepm is larger than one month (smallest stepm) and if the exact delay 3611: * (in months) between two waves is not a multiple of stepm, we rounded to 3612: * the nearest (and in case of equal distance, to the lowest) interval but now 3613: * we keep into memory the bias bh[mi][i] and also the previous matrix product 3614: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the 3615: * probability in order to take into account the bias as a fraction of the way 3616: * from savm to out if bh is negative or even beyond if bh is positive. bh varies 3617: * -stepm/2 to stepm/2 . 3618: * For stepm=1 the results are the same as for previous versions of Imach. 3619: * For stepm > 1 the results are less biased than in previous versions. 3620: */ 3621: s1=s[mw[mi][i]][i]; 3622: s2=s[mw[mi+1][i]][i]; 3623: bbh=(double)bh[mi][i]/(double)stepm; 3624: /* bias bh is positive if real duration 3625: * is higher than the multiple of stepm and negative otherwise. 3626: */ 3627: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/ 3628: if( s2 > nlstate){ 3629: /* i.e. if s2 is a death state and if the date of death is known 3630: then the contribution to the likelihood is the probability to 3631: die between last step unit time and current step unit time, 3632: which is also equal to probability to die before dh 3633: minus probability to die before dh-stepm . 3634: In version up to 0.92 likelihood was computed 3635: as if date of death was unknown. Death was treated as any other 3636: health state: the date of the interview describes the actual state 3637: and not the date of a change in health state. The former idea was 3638: to consider that at each interview the state was recorded 3639: (healthy, disable or death) and IMaCh was corrected; but when we 3640: introduced the exact date of death then we should have modified 3641: the contribution of an exact death to the likelihood. This new 3642: contribution is smaller and very dependent of the step unit 3643: stepm. It is no more the probability to die between last interview 3644: and month of death but the probability to survive from last 3645: interview up to one month before death multiplied by the 3646: probability to die within a month. Thanks to Chris 3647: Jackson for correcting this bug. Former versions increased 3648: mortality artificially. The bad side is that we add another loop 3649: which slows down the processing. The difference can be up to 10% 3650: lower mortality. 3651: */ 3652: /* If, at the beginning of the maximization mostly, the 3653: cumulative probability or probability to be dead is 3654: constant (ie = 1) over time d, the difference is equal to 3655: 0. out[s1][3] = savm[s1][3]: probability, being at state 3656: s1 at precedent wave, to be dead a month before current 3657: wave is equal to probability, being at state s1 at 3658: precedent wave, to be dead at mont of the current 3659: wave. Then the observed probability (that this person died) 3660: is null according to current estimated parameter. In fact, 3661: it should be very low but not zero otherwise the log go to 3662: infinity. 3663: */ 3664: /* #ifdef INFINITYORIGINAL */ 3665: /* lli=log(out[s1][s2] - savm[s1][s2]); */ 3666: /* #else */ 3667: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */ 3668: /* lli=log(mytinydouble); */ 3669: /* else */ 3670: /* lli=log(out[s1][s2] - savm[s1][s2]); */ 3671: /* #endif */ 3672: lli=log(out[s1][s2] - savm[s1][s2]); 3673: 3674: } else if ( s2==-1 ) { /* alive */ 3675: for (j=1,survp=0. ; j<=nlstate; j++) 3676: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; 3677: /*survp += out[s1][j]; */ 3678: lli= log(survp); 3679: } 3680: else if (s2==-4) { 3681: for (j=3,survp=0. ; j<=nlstate; j++) 3682: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; 3683: lli= log(survp); 3684: } 3685: else if (s2==-5) { 3686: for (j=1,survp=0. ; j<=2; j++) 3687: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; 3688: lli= log(survp); 3689: } 3690: else{ 3691: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */ 3692: /* 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 */ 3693: } 3694: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/ 3695: /*if(lli ==000.0)*/ 3696: /*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); */ 3697: ipmx +=1; 3698: sw += weight[i]; 3699: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; 3700: /* if (lli < log(mytinydouble)){ */ 3701: /* 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); */ 3702: /* 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]); */ 3703: /* } */ 3704: } /* end of wave */ 3705: } /* end of individual */ 3706: } else if(mle==2){ 3707: for (i=1,ipmx=0, sw=0.; i<=imx; i++){ 3708: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; 3709: for(mi=1; mi<= wav[i]-1; mi++){ 3710: for (ii=1;ii<=nlstate+ndeath;ii++) 3711: for (j=1;j<=nlstate+ndeath;j++){ 3712: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 3713: savm[ii][j]=(ii==j ? 1.0 : 0.0); 3714: } 3715: for(d=0; d<=dh[mi][i]; d++){ 3716: newm=savm; 3717: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; 3718: cov[2]=agexact; 3719: if(nagesqr==1) 3720: cov[3]= agexact*agexact; 3721: for (kk=1; kk<=cptcovage;kk++) { 3722: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; 3723: } 3724: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, 3725: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); 3726: savm=oldm; 3727: oldm=newm; 3728: } /* end mult */ 3729: 3730: s1=s[mw[mi][i]][i]; 3731: s2=s[mw[mi+1][i]][i]; 3732: bbh=(double)bh[mi][i]/(double)stepm; 3733: 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 */ 3734: ipmx +=1; 3735: sw += weight[i]; 3736: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; 3737: } /* end of wave */ 3738: } /* end of individual */ 3739: } else if(mle==3){ /* exponential inter-extrapolation */ 3740: for (i=1,ipmx=0, sw=0.; i<=imx; i++){ 3741: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; 3742: for(mi=1; mi<= wav[i]-1; mi++){ 3743: for (ii=1;ii<=nlstate+ndeath;ii++) 3744: for (j=1;j<=nlstate+ndeath;j++){ 3745: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 3746: savm[ii][j]=(ii==j ? 1.0 : 0.0); 3747: } 3748: for(d=0; d<dh[mi][i]; d++){ 3749: newm=savm; 3750: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; 3751: cov[2]=agexact; 3752: if(nagesqr==1) 3753: cov[3]= agexact*agexact; 3754: for (kk=1; kk<=cptcovage;kk++) { 3755: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; 3756: } 3757: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, 3758: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); 3759: savm=oldm; 3760: oldm=newm; 3761: } /* end mult */ 3762: 3763: s1=s[mw[mi][i]][i]; 3764: s2=s[mw[mi+1][i]][i]; 3765: bbh=(double)bh[mi][i]/(double)stepm; 3766: 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 */ 3767: ipmx +=1; 3768: sw += weight[i]; 3769: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; 3770: } /* end of wave */ 3771: } /* end of individual */ 3772: }else if (mle==4){ /* ml=4 no inter-extrapolation */ 3773: for (i=1,ipmx=0, sw=0.; i<=imx; i++){ 3774: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; 3775: for(mi=1; mi<= wav[i]-1; mi++){ 3776: for (ii=1;ii<=nlstate+ndeath;ii++) 3777: for (j=1;j<=nlstate+ndeath;j++){ 3778: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 3779: savm[ii][j]=(ii==j ? 1.0 : 0.0); 3780: } 3781: for(d=0; d<dh[mi][i]; d++){ 3782: newm=savm; 3783: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; 3784: cov[2]=agexact; 3785: if(nagesqr==1) 3786: cov[3]= agexact*agexact; 3787: for (kk=1; kk<=cptcovage;kk++) { 3788: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; 3789: } 3790: 3791: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, 3792: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); 3793: savm=oldm; 3794: oldm=newm; 3795: } /* end mult */ 3796: 3797: s1=s[mw[mi][i]][i]; 3798: s2=s[mw[mi+1][i]][i]; 3799: if( s2 > nlstate){ 3800: lli=log(out[s1][s2] - savm[s1][s2]); 3801: } else if ( s2==-1 ) { /* alive */ 3802: for (j=1,survp=0. ; j<=nlstate; j++) 3803: survp += out[s1][j]; 3804: lli= log(survp); 3805: }else{ 3806: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */ 3807: } 3808: ipmx +=1; 3809: sw += weight[i]; 3810: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; 3811: /* 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]); */ 3812: } /* end of wave */ 3813: } /* end of individual */ 3814: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */ 3815: for (i=1,ipmx=0, sw=0.; i<=imx; i++){ 3816: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; 3817: for(mi=1; mi<= wav[i]-1; mi++){ 3818: for (ii=1;ii<=nlstate+ndeath;ii++) 3819: for (j=1;j<=nlstate+ndeath;j++){ 3820: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 3821: savm[ii][j]=(ii==j ? 1.0 : 0.0); 3822: } 3823: for(d=0; d<dh[mi][i]; d++){ 3824: newm=savm; 3825: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; 3826: cov[2]=agexact; 3827: if(nagesqr==1) 3828: cov[3]= agexact*agexact; 3829: for (kk=1; kk<=cptcovage;kk++) { 3830: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; 3831: } 3832: 3833: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, 3834: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); 3835: savm=oldm; 3836: oldm=newm; 3837: } /* end mult */ 3838: 3839: s1=s[mw[mi][i]][i]; 3840: s2=s[mw[mi+1][i]][i]; 3841: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */ 3842: ipmx +=1; 3843: sw += weight[i]; 3844: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; 3845: /*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]);*/ 3846: } /* end of wave */ 3847: } /* end of individual */ 3848: } /* End of if */ 3849: for(k=1,l=0.; k<=nlstate; k++) l += ll[k]; 3850: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */ 3851: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */ 3852: return -l; 3853: } 3854: 3855: /*************** log-likelihood *************/ 3856: double funcone( double *x) 3857: { 3858: /* Same as func but slower because of a lot of printf and if */ 3859: int i, ii, j, k, mi, d, kk; 3860: int ioffset=0; 3861: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1]; 3862: double **out; 3863: double lli; /* Individual log likelihood */ 3864: double llt; 3865: int s1, s2; 3866: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quantitative time varying covariate */ 3867: 3868: double bbh, survp; 3869: double agexact; 3870: double agebegin, ageend; 3871: /*extern weight */ 3872: /* We are differentiating ll according to initial status */ 3873: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/ 3874: /*for(i=1;i<imx;i++) 3875: printf(" %d\n",s[4][i]); 3876: */ 3877: cov[1]=1.; 3878: 3879: for(k=1; k<=nlstate; k++) ll[k]=0.; 3880: ioffset=0; 3881: for (i=1,ipmx=0, sw=0.; i<=imx; i++){ 3882: /* ioffset=2+nagesqr+cptcovage; */ 3883: ioffset=2+nagesqr; 3884: /* Fixed */ 3885: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */ 3886: /* for (k=1; k<=ncoveff;k++){ /\* Simple and product fixed Dummy covariates without age* products *\/ */ 3887: for (k=1; k<=ncovf;k++){ /* Simple and product fixed covariates without age* products *//* Missing values are set to -1 but should be dropped */ 3888: 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)*/ 3889: /* cov[ioffset+TvarFind[1]]=covar[Tvar[TvarFind[1]]][i]; */ 3890: /* cov[2+6]=covar[Tvar[6]][i]; */ 3891: /* cov[2+6]=covar[2][i]; V2 */ 3892: /* cov[TvarFind[2]]=covar[Tvar[TvarFind[2]]][i]; */ 3893: /* cov[2+7]=covar[Tvar[7]][i]; */ 3894: /* cov[2+7]=covar[7][i]; V7=V1*V2 */ 3895: /* cov[TvarFind[3]]=covar[Tvar[TvarFind[3]]][i]; */ 3896: /* cov[2+9]=covar[Tvar[9]][i]; */ 3897: /* cov[2+9]=covar[1][i]; V1 */ 3898: } 3899: /* for (k=1; k<=nqfveff;k++){ /\* Simple and product fixed Quantitative covariates without age* products *\/ */ 3900: /* 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?)*\/ */ 3901: /* } */ 3902: /* for(iqv=1; iqv <= nqfveff; iqv++){ /\* Quantitative fixed covariates *\/ */ 3903: /* cov[++ioffset]=coqvar[Tvar[iqv]][i]; /\* Only V2 k=6 and V1*V2 7 *\/ */ 3904: /* } */ 3905: 3906: 3907: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */ 3908: /* Wave varying (but not age varying) */ 3909: for(k=1; k <= ncovv ; k++){ /* Varying covariates (single and product but no age )*/ 3910: /* cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]][i]; */ 3911: cov[ioffset+TvarVind[k]]=cotvar[mw[mi][i]][Tvar[TvarVind[k]]-ncovcol-nqv][i]; 3912: } 3913: /* for(itv=1; itv <= ntveff; itv++){ /\* Varying dummy covariates (single??)*\/ */ 3914: /* iv= Tvar[Tmodelind[ioffset-2-nagesqr-cptcovage+itv]]-ncovcol-nqv; /\* Counting the # varying covariate from 1 to ntveff *\/ */ 3915: /* cov[ioffset+iv]=cotvar[mw[mi][i]][iv][i]; */ 3916: /* k=ioffset-2-nagesqr-cptcovage+itv; /\* position in simple model *\/ */ 3917: /* cov[ioffset+itv]=cotvar[mw[mi][i]][TmodelInvind[itv]][i]; */ 3918: /* 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]); */ 3919: /* for(iqtv=1; iqtv <= nqtveff; iqtv++){ /\* Varying quantitatives covariates *\/ */ 3920: /* iv=TmodelInvQind[iqtv]; /\* Counting the # varying covariate from 1 to ntveff *\/ */ 3921: /* /\* 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]); *\/ */ 3922: /* cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][TmodelInvQind[iqtv]][i]; */ 3923: /* } */ 3924: for (ii=1;ii<=nlstate+ndeath;ii++) 3925: for (j=1;j<=nlstate+ndeath;j++){ 3926: oldm[ii][j]=(ii==j ? 1.0 : 0.0); 3927: savm[ii][j]=(ii==j ? 1.0 : 0.0); 3928: } 3929: 3930: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */ 3931: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */ 3932: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */ 3933: /* for(d=0; d<=0; d++){ /\* Delay between two effective waves Only one matrix to speed up*\/ */ 3934: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i] 3935: and mw[mi+1][i]. dh depends on stepm.*/ 3936: newm=savm; 3937: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM; /* Here d is needed */ 3938: cov[2]=agexact; 3939: if(nagesqr==1) 3940: cov[3]= agexact*agexact; 3941: for (kk=1; kk<=cptcovage;kk++) { 3942: if(!FixedV[Tvar[Tage[kk]]]) 3943: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; 3944: else 3945: cov[Tage[kk]+2+nagesqr]=cotvar[mw[mi][i]][Tvar[Tage[kk]]-ncovcol-nqv][i]*agexact; 3946: } 3947: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */ 3948: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */ 3949: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, 3950: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); 3951: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */ 3952: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */ 3953: savm=oldm; 3954: oldm=newm; 3955: } /* end mult */ 3956: 3957: s1=s[mw[mi][i]][i]; 3958: s2=s[mw[mi+1][i]][i]; 3959: /* if(s2==-1){ */ 3960: /* printf(" ERROR s1=%d, s2=%d i=%d \n", s1, s2, i); */ 3961: /* /\* exit(1); *\/ */ 3962: /* } */ 3963: bbh=(double)bh[mi][i]/(double)stepm; 3964: /* bias is positive if real duration 3965: * is higher than the multiple of stepm and negative otherwise. 3966: */ 3967: if( s2 > nlstate && (mle <5) ){ /* Jackson */ 3968: lli=log(out[s1][s2] - savm[s1][s2]); 3969: } else if ( s2==-1 ) { /* alive */ 3970: for (j=1,survp=0. ; j<=nlstate; j++) 3971: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j]; 3972: lli= log(survp); 3973: }else if (mle==1){ 3974: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */ 3975: } else if(mle==2){ 3976: 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 */ 3977: } else if(mle==3){ /* exponential inter-extrapolation */ 3978: 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 */ 3979: } else if (mle==4){ /* mle=4 no inter-extrapolation */ 3980: lli=log(out[s1][s2]); /* Original formula */ 3981: } else{ /* mle=0 back to 1 */ 3982: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */ 3983: /*lli=log(out[s1][s2]); */ /* Original formula */ 3984: } /* End of if */ 3985: ipmx +=1; 3986: sw += weight[i]; 3987: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli; 3988: /*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]); */ 3989: if(globpr){ 3990: fprintf(ficresilk,"%09ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\ 3991: %11.6f %11.6f %11.6f ", \ 3992: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw, 3993: 2*weight[i]*lli,(s2==-1? -1: out[s1][s2]),(s2==-1? -1: savm[s1][s2])); 3994: for(k=1,llt=0.,l=0.; k<=nlstate; k++){ 3995: llt +=ll[k]*gipmx/gsw; 3996: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw); 3997: } 3998: fprintf(ficresilk," %10.6f\n", -llt); 3999: } 4000: } /* end of wave */ 4001: } /* end of individual */ 4002: for(k=1,l=0.; k<=nlstate; k++) l += ll[k]; 4003: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */ 4004: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */ 4005: if(globpr==0){ /* First time we count the contributions and weights */ 4006: gipmx=ipmx; 4007: gsw=sw; 4008: } 4009: return -l; 4010: } 4011: 4012: 4013: /*************** function likelione ***********/ 4014: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*func)(double [])) 4015: { 4016: /* This routine should help understanding what is done with 4017: the selection of individuals/waves and 4018: to check the exact contribution to the likelihood. 4019: Plotting could be done. 4020: */ 4021: int k; 4022: 4023: if(*globpri !=0){ /* Just counts and sums, no printings */ 4024: strcpy(fileresilk,"ILK_"); 4025: strcat(fileresilk,fileresu); 4026: if((ficresilk=fopen(fileresilk,"w"))==NULL) { 4027: printf("Problem with resultfile: %s\n", fileresilk); 4028: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk); 4029: } 4030: 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"); 4031: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav "); 4032: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */ 4033: for(k=1; k<=nlstate; k++) 4034: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k); 4035: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n"); 4036: } 4037: 4038: *fretone=(*func)(p); 4039: if(*globpri !=0){ 4040: fclose(ficresilk); 4041: if (mle ==0) 4042: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle); 4043: else if(mle >=1) 4044: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle); 4045: 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)); 4046: fprintf(fichtm,"\n<br>Equation of the model: <b>model=1+age+%s</b><br>\n",model); 4047: 4048: for (k=1; k<= nlstate ; k++) { 4049: 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> \ 4050: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k); 4051: } 4052: 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> \ 4053: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_")); 4054: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \ 4055: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_")); 4056: fflush(fichtm); 4057: } 4058: return; 4059: } 4060: 4061: 4062: /*********** Maximum Likelihood Estimation ***************/ 4063: 4064: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double [])) 4065: { 4066: int i,j, iter=0; 4067: double **xi; 4068: double fret; 4069: double fretone; /* Only one call to likelihood */ 4070: /* char filerespow[FILENAMELENGTH];*/ 4071: 4072: #ifdef NLOPT 4073: int creturn; 4074: nlopt_opt opt; 4075: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */ 4076: double *lb; 4077: double minf; /* the minimum objective value, upon return */ 4078: double * p1; /* Shifted parameters from 0 instead of 1 */ 4079: myfunc_data dinst, *d = &dinst; 4080: #endif 4081: 4082: 4083: xi=matrix(1,npar,1,npar); 4084: for (i=1;i<=npar;i++) 4085: for (j=1;j<=npar;j++) 4086: xi[i][j]=(i==j ? 1.0 : 0.0); 4087: printf("Powell\n"); fprintf(ficlog,"Powell\n"); 4088: strcpy(filerespow,"POW_"); 4089: strcat(filerespow,fileres); 4090: if((ficrespow=fopen(filerespow,"w"))==NULL) { 4091: printf("Problem with resultfile: %s\n", filerespow); 4092: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow); 4093: } 4094: fprintf(ficrespow,"# Powell\n# iter -2*LL"); 4095: for (i=1;i<=nlstate;i++) 4096: for(j=1;j<=nlstate+ndeath;j++) 4097: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j); 4098: fprintf(ficrespow,"\n"); 4099: #ifdef POWELL 4100: powell(p,xi,npar,ftol,&iter,&fret,func); 4101: #endif 4102: 4103: #ifdef NLOPT 4104: #ifdef NEWUOA 4105: opt = nlopt_create(NLOPT_LN_NEWUOA,npar); 4106: #else 4107: opt = nlopt_create(NLOPT_LN_BOBYQA,npar); 4108: #endif 4109: lb=vector(0,npar-1); 4110: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL; 4111: nlopt_set_lower_bounds(opt, lb); 4112: nlopt_set_initial_step1(opt, 0.1); 4113: 4114: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */ 4115: d->function = func; 4116: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d)); 4117: nlopt_set_min_objective(opt, myfunc, d); 4118: nlopt_set_xtol_rel(opt, ftol); 4119: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) { 4120: printf("nlopt failed! %d\n",creturn); 4121: } 4122: else { 4123: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT); 4124: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf); 4125: iter=1; /* not equal */ 4126: } 4127: nlopt_destroy(opt); 4128: #endif 4129: free_matrix(xi,1,npar,1,npar); 4130: fclose(ficrespow); 4131: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p)); 4132: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p)); 4133: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p)); 4134: 4135: } 4136: 4137: /**** Computes Hessian and covariance matrix ***/ 4138: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double [])) 4139: { 4140: double **a,**y,*x,pd; 4141: /* double **hess; */ 4142: int i, j; 4143: int *indx; 4144: 4145: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar); 4146: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar); 4147: void lubksb(double **a, int npar, int *indx, double b[]) ; 4148: void ludcmp(double **a, int npar, int *indx, double *d) ; 4149: double gompertz(double p[]); 4150: /* hess=matrix(1,npar,1,npar); */ 4151: 4152: printf("\nCalculation of the hessian matrix. Wait...\n"); 4153: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n"); 4154: for (i=1;i<=npar;i++){ 4155: printf("%d-",i);fflush(stdout); 4156: fprintf(ficlog,"%d-",i);fflush(ficlog); 4157: 4158: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar); 4159: 4160: /* printf(" %f ",p[i]); 4161: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/ 4162: } 4163: 4164: for (i=1;i<=npar;i++) { 4165: for (j=1;j<=npar;j++) { 4166: if (j>i) { 4167: printf(".%d-%d",i,j);fflush(stdout); 4168: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog); 4169: hess[i][j]=hessij(p,hess, delti,i,j,func,npar); 4170: 4171: hess[j][i]=hess[i][j]; 4172: /*printf(" %lf ",hess[i][j]);*/ 4173: } 4174: } 4175: } 4176: printf("\n"); 4177: fprintf(ficlog,"\n"); 4178: 4179: printf("\nInverting the hessian to get the covariance matrix. Wait...\n"); 4180: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n"); 4181: 4182: a=matrix(1,npar,1,npar); 4183: y=matrix(1,npar,1,npar); 4184: x=vector(1,npar); 4185: indx=ivector(1,npar); 4186: for (i=1;i<=npar;i++) 4187: for (j=1;j<=npar;j++) a[i][j]=hess[i][j]; 4188: ludcmp(a,npar,indx,&pd); 4189: 4190: for (j=1;j<=npar;j++) { 4191: for (i=1;i<=npar;i++) x[i]=0; 4192: x[j]=1; 4193: lubksb(a,npar,indx,x); 4194: for (i=1;i<=npar;i++){ 4195: matcov[i][j]=x[i]; 4196: } 4197: } 4198: 4199: printf("\n#Hessian matrix#\n"); 4200: fprintf(ficlog,"\n#Hessian matrix#\n"); 4201: for (i=1;i<=npar;i++) { 4202: for (j=1;j<=npar;j++) { 4203: printf("%.6e ",hess[i][j]); 4204: fprintf(ficlog,"%.6e ",hess[i][j]); 4205: } 4206: printf("\n"); 4207: fprintf(ficlog,"\n"); 4208: } 4209: 4210: /* printf("\n#Covariance matrix#\n"); */ 4211: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */ 4212: /* for (i=1;i<=npar;i++) { */ 4213: /* for (j=1;j<=npar;j++) { */ 4214: /* printf("%.6e ",matcov[i][j]); */ 4215: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */ 4216: /* } */ 4217: /* printf("\n"); */ 4218: /* fprintf(ficlog,"\n"); */ 4219: /* } */ 4220: 4221: /* Recompute Inverse */ 4222: /* for (i=1;i<=npar;i++) */ 4223: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */ 4224: /* ludcmp(a,npar,indx,&pd); */ 4225: 4226: /* printf("\n#Hessian matrix recomputed#\n"); */ 4227: 4228: /* for (j=1;j<=npar;j++) { */ 4229: /* for (i=1;i<=npar;i++) x[i]=0; */ 4230: /* x[j]=1; */ 4231: /* lubksb(a,npar,indx,x); */ 4232: /* for (i=1;i<=npar;i++){ */ 4233: /* y[i][j]=x[i]; */ 4234: /* printf("%.3e ",y[i][j]); */ 4235: /* fprintf(ficlog,"%.3e ",y[i][j]); */ 4236: /* } */ 4237: /* printf("\n"); */ 4238: /* fprintf(ficlog,"\n"); */ 4239: /* } */ 4240: 4241: /* Verifying the inverse matrix */ 4242: #ifdef DEBUGHESS 4243: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov); 4244: 4245: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n"); 4246: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n"); 4247: 4248: for (j=1;j<=npar;j++) { 4249: for (i=1;i<=npar;i++){ 4250: printf("%.2f ",y[i][j]); 4251: fprintf(ficlog,"%.2f ",y[i][j]); 4252: } 4253: printf("\n"); 4254: fprintf(ficlog,"\n"); 4255: } 4256: #endif 4257: 4258: free_matrix(a,1,npar,1,npar); 4259: free_matrix(y,1,npar,1,npar); 4260: free_vector(x,1,npar); 4261: free_ivector(indx,1,npar); 4262: /* free_matrix(hess,1,npar,1,npar); */ 4263: 4264: 4265: } 4266: 4267: /*************** hessian matrix ****************/ 4268: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar) 4269: { /* Around values of x, computes the function func and returns the scales delti and hessian */ 4270: int i; 4271: int l=1, lmax=20; 4272: double k1,k2, res, fx; 4273: double p2[MAXPARM+1]; /* identical to x */ 4274: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; 4275: int k=0,kmax=10; 4276: double l1; 4277: 4278: fx=func(x); 4279: for (i=1;i<=npar;i++) p2[i]=x[i]; 4280: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */ 4281: l1=pow(10,l); 4282: delts=delt; 4283: for(k=1 ; k <kmax; k=k+1){ 4284: delt = delta*(l1*k); 4285: p2[theta]=x[theta] +delt; 4286: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */ 4287: p2[theta]=x[theta]-delt; 4288: k2=func(p2)-fx; 4289: /*res= (k1-2.0*fx+k2)/delt/delt; */ 4290: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */ 4291: 4292: #ifdef DEBUGHESSII 4293: 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); 4294: 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); 4295: #endif 4296: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */ 4297: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){ 4298: k=kmax; 4299: } 4300: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */ 4301: k=kmax; l=lmax*10; 4302: } 4303: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ 4304: delts=delt; 4305: } 4306: } /* End loop k */ 4307: } 4308: delti[theta]=delts; 4309: return res; 4310: 4311: } 4312: 4313: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar) 4314: { 4315: int i; 4316: int l=1, lmax=20; 4317: double k1,k2,k3,k4,res,fx; 4318: double p2[MAXPARM+1]; 4319: int k, kmax=1; 4320: double v1, v2, cv12, lc1, lc2; 4321: 4322: int firstime=0; 4323: 4324: fx=func(x); 4325: for (k=1; k<=kmax; k=k+10) { 4326: for (i=1;i<=npar;i++) p2[i]=x[i]; 4327: p2[thetai]=x[thetai]+delti[thetai]*k; 4328: p2[thetaj]=x[thetaj]+delti[thetaj]*k; 4329: k1=func(p2)-fx; 4330: 4331: p2[thetai]=x[thetai]+delti[thetai]*k; 4332: p2[thetaj]=x[thetaj]-delti[thetaj]*k; 4333: k2=func(p2)-fx; 4334: 4335: p2[thetai]=x[thetai]-delti[thetai]*k; 4336: p2[thetaj]=x[thetaj]+delti[thetaj]*k; 4337: k3=func(p2)-fx; 4338: 4339: p2[thetai]=x[thetai]-delti[thetai]*k; 4340: p2[thetaj]=x[thetaj]-delti[thetaj]*k; 4341: k4=func(p2)-fx; 4342: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */ 4343: if(k1*k2*k3*k4 <0.){ 4344: firstime=1; 4345: kmax=kmax+10; 4346: } 4347: if(kmax >=10 || firstime ==1){ 4348: 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); 4349: 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); 4350: 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); 4351: 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); 4352: } 4353: #ifdef DEBUGHESSIJ 4354: v1=hess[thetai][thetai]; 4355: v2=hess[thetaj][thetaj]; 4356: cv12=res; 4357: /* Computing eigen value of Hessian matrix */ 4358: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.; 4359: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.; 4360: if ((lc2 <0) || (lc1 <0) ){ 4361: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj); 4362: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj); 4363: 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); 4364: 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); 4365: } 4366: #endif 4367: } 4368: return res; 4369: } 4370: 4371: /* Not done yet: Was supposed to fix if not exactly at the maximum */ 4372: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */ 4373: /* { */ 4374: /* int i; */ 4375: /* int l=1, lmax=20; */ 4376: /* double k1,k2,k3,k4,res,fx; */ 4377: /* double p2[MAXPARM+1]; */ 4378: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */ 4379: /* int k=0,kmax=10; */ 4380: /* double l1; */ 4381: 4382: /* fx=func(x); */ 4383: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */ 4384: /* l1=pow(10,l); */ 4385: /* delts=delt; */ 4386: /* for(k=1 ; k <kmax; k=k+1){ */ 4387: /* delt = delti*(l1*k); */ 4388: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */ 4389: /* p2[thetai]=x[thetai]+delti[thetai]/k; */ 4390: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */ 4391: /* k1=func(p2)-fx; */ 4392: 4393: /* p2[thetai]=x[thetai]+delti[thetai]/k; */ 4394: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */ 4395: /* k2=func(p2)-fx; */ 4396: 4397: /* p2[thetai]=x[thetai]-delti[thetai]/k; */ 4398: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */ 4399: /* k3=func(p2)-fx; */ 4400: 4401: /* p2[thetai]=x[thetai]-delti[thetai]/k; */ 4402: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */ 4403: /* k4=func(p2)-fx; */ 4404: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */ 4405: /* #ifdef DEBUGHESSIJ */ 4406: /* 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); */ 4407: /* 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); */ 4408: /* #endif */ 4409: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */ 4410: /* k=kmax; */ 4411: /* } */ 4412: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */ 4413: /* k=kmax; l=lmax*10; */ 4414: /* } */ 4415: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */ 4416: /* delts=delt; */ 4417: /* } */ 4418: /* } /\* End loop k *\/ */ 4419: /* } */ 4420: /* delti[theta]=delts; */ 4421: /* return res; */ 4422: /* } */ 4423: 4424: 4425: /************** Inverse of matrix **************/ 4426: void ludcmp(double **a, int n, int *indx, double *d) 4427: { 4428: int i,imax,j,k; 4429: double big,dum,sum,temp; 4430: double *vv; 4431: 4432: vv=vector(1,n); 4433: *d=1.0; 4434: for (i=1;i<=n;i++) { 4435: big=0.0; 4436: for (j=1;j<=n;j++) 4437: if ((temp=fabs(a[i][j])) > big) big=temp; 4438: if (big == 0.0){ 4439: printf(" Singular Hessian matrix at row %d:\n",i); 4440: for (j=1;j<=n;j++) { 4441: printf(" a[%d][%d]=%f,",i,j,a[i][j]); 4442: fprintf(ficlog," a[%d][%d]=%f,",i,j,a[i][j]); 4443: } 4444: fflush(ficlog); 4445: fclose(ficlog); 4446: nrerror("Singular matrix in routine ludcmp"); 4447: } 4448: vv[i]=1.0/big; 4449: } 4450: for (j=1;j<=n;j++) { 4451: for (i=1;i<j;i++) { 4452: sum=a[i][j]; 4453: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j]; 4454: a[i][j]=sum; 4455: } 4456: big=0.0; 4457: for (i=j;i<=n;i++) { 4458: sum=a[i][j]; 4459: for (k=1;k<j;k++) 4460: sum -= a[i][k]*a[k][j]; 4461: a[i][j]=sum; 4462: if ( (dum=vv[i]*fabs(sum)) >= big) { 4463: big=dum; 4464: imax=i; 4465: } 4466: } 4467: if (j != imax) { 4468: for (k=1;k<=n;k++) { 4469: dum=a[imax][k]; 4470: a[imax][k]=a[j][k]; 4471: a[j][k]=dum; 4472: } 4473: *d = -(*d); 4474: vv[imax]=vv[j]; 4475: } 4476: indx[j]=imax; 4477: if (a[j][j] == 0.0) a[j][j]=TINY; 4478: if (j != n) { 4479: dum=1.0/(a[j][j]); 4480: for (i=j+1;i<=n;i++) a[i][j] *= dum; 4481: } 4482: } 4483: free_vector(vv,1,n); /* Doesn't work */ 4484: ; 4485: } 4486: 4487: void lubksb(double **a, int n, int *indx, double b[]) 4488: { 4489: int i,ii=0,ip,j; 4490: double sum; 4491: 4492: for (i=1;i<=n;i++) { 4493: ip=indx[i]; 4494: sum=b[ip]; 4495: b[ip]=b[i]; 4496: if (ii) 4497: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j]; 4498: else if (sum) ii=i; 4499: b[i]=sum; 4500: } 4501: for (i=n;i>=1;i--) { 4502: sum=b[i]; 4503: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j]; 4504: b[i]=sum/a[i][i]; 4505: } 4506: } 4507: 4508: void pstamp(FILE *fichier) 4509: { 4510: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart); 4511: } 4512: 4513: void date2dmy(double date,double *day, double *month, double *year){ 4514: double yp=0., yp1=0., yp2=0.; 4515: 4516: yp1=modf(date,&yp);/* extracts integral of date in yp and 4517: fractional in yp1 */ 4518: *year=yp; 4519: yp2=modf((yp1*12),&yp); 4520: *month=yp; 4521: yp1=modf((yp2*30.5),&yp); 4522: *day=yp; 4523: if(*day==0) *day=1; 4524: if(*month==0) *month=1; 4525: } 4526: 4527: 4528: 4529: /************ Frequencies ********************/ 4530: void freqsummary(char fileres[], double p[], double pstart[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \ 4531: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \ 4532: int firstpass, int lastpass, int stepm, int weightopt, char model[]) 4533: { /* Some frequencies as well as proposing some starting values */ 4534: 4535: int i, m, jk, j1, bool, z1,j, nj, nl, k, iv, jj=0, s1=1, s2=1; 4536: int iind=0, iage=0; 4537: int mi; /* Effective wave */ 4538: int first; 4539: double ***freq; /* Frequencies */ 4540: 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 */ 4541: 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); 4542: double *meanq, *stdq, *idq; 4543: double **meanqt; 4544: double *pp, **prop, *posprop, *pospropt; 4545: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0; 4546: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH]; 4547: double agebegin, ageend; 4548: 4549: pp=vector(1,nlstate); 4550: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE); 4551: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */ 4552: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */ 4553: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */ 4554: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */ 4555: stdq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */ 4556: idq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */ 4557: meanqt=matrix(1,lastpass,1,nqtveff); 4558: strcpy(fileresp,"P_"); 4559: strcat(fileresp,fileresu); 4560: /*strcat(fileresphtm,fileresu);*/ 4561: if((ficresp=fopen(fileresp,"w"))==NULL) { 4562: printf("Problem with prevalence resultfile: %s\n", fileresp); 4563: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp); 4564: exit(0); 4565: } 4566: 4567: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm")); 4568: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) { 4569: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno)); 4570: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno)); 4571: fflush(ficlog); 4572: exit(70); 4573: } 4574: else{ 4575: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \ 4576: <hr size=\"2\" color=\"#EC5E5E\"> \n \ 4577: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\ 4578: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model); 4579: } 4580: 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); 4581: 4582: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm")); 4583: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) { 4584: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno)); 4585: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno)); 4586: fflush(ficlog); 4587: exit(70); 4588: } else{ 4589: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \ 4590: <hr size=\"2\" color=\"#EC5E5E\"> \n \ 4591: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\ 4592: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model); 4593: } 4594: 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); 4595: 4596: y= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE); 4597: x= vector(iagemin-AGEMARGE,iagemax+4+AGEMARGE); 4598: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+4+AGEMARGE); 4599: j1=0; 4600: 4601: /* j=ncoveff; /\* Only fixed dummy covariates *\/ */ 4602: j=cptcoveff; /* Only dummy covariates of the model */ 4603: if (cptcovn<1) {j=1;ncodemax[1]=1;} 4604: 4605: 4606: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels: 4607: reference=low_education V1=0,V2=0 4608: med_educ V1=1 V2=0, 4609: high_educ V1=0 V2=1 4610: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff 4611: */ 4612: dateintsum=0; 4613: k2cpt=0; 4614: 4615: if(cptcoveff == 0 ) 4616: nl=1; /* Constant and age model only */ 4617: else 4618: nl=2; 4619: 4620: /* if a constant only model, one pass to compute frequency tables and to write it on ficresp */ 4621: /* Loop on nj=1 or 2 if dummy covariates j!=0 4622: * Loop on j1(1 to 2**cptcoveff) covariate combination 4623: * freq[s1][s2][iage] =0. 4624: * Loop on iind 4625: * ++freq[s1][s2][iage] weighted 4626: * end iind 4627: * if covariate and j!0 4628: * headers Variable on one line 4629: * endif cov j!=0 4630: * header of frequency table by age 4631: * Loop on age 4632: * pp[s1]+=freq[s1][s2][iage] weighted 4633: * pos+=freq[s1][s2][iage] weighted 4634: * Loop on s1 initial state 4635: * fprintf(ficresp 4636: * end s1 4637: * end age 4638: * if j!=0 computes starting values 4639: * end compute starting values 4640: * end j1 4641: * end nl 4642: */ 4643: for (nj = 1; nj <= nl; nj++){ /* nj= 1 constant model, nl number of loops. */ 4644: if(nj==1) 4645: j=0; /* First pass for the constant */ 4646: else{ 4647: j=cptcoveff; /* Other passes for the covariate values */ 4648: } 4649: first=1; 4650: 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 */ 4651: posproptt=0.; 4652: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]); 4653: scanf("%d", i);*/ 4654: for (i=-5; i<=nlstate+ndeath; i++) 4655: for (s2=-5; s2<=nlstate+ndeath; s2++) 4656: for(m=iagemin; m <= iagemax+3; m++) 4657: freq[i][s2][m]=0; 4658: 4659: for (i=1; i<=nlstate; i++) { 4660: for(m=iagemin; m <= iagemax+3; m++) 4661: prop[i][m]=0; 4662: posprop[i]=0; 4663: pospropt[i]=0; 4664: } 4665: for (z1=1; z1<= nqfveff; z1++) { /* zeroing for each combination j1 as well as for the total */ 4666: idq[z1]=0.; 4667: meanq[z1]=0.; 4668: stdq[z1]=0.; 4669: } 4670: /* for (z1=1; z1<= nqtveff; z1++) { */ 4671: /* for(m=1;m<=lastpass;m++){ */ 4672: /* meanqt[m][z1]=0.; */ 4673: /* } */ 4674: /* } */ 4675: /* dateintsum=0; */ 4676: /* k2cpt=0; */ 4677: 4678: /* For that combination of covariates j1 (V4=1 V3=0 for example), we count and print the frequencies in one pass */ 4679: for (iind=1; iind<=imx; iind++) { /* For each individual iind */ 4680: bool=1; 4681: if(j !=0){ 4682: if(anyvaryingduminmodel==0){ /* If All fixed covariates */ 4683: if (cptcoveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */ 4684: for (z1=1; z1<=cptcoveff; z1++) { /* loops on covariates in the model */ 4685: /* if(Tvaraff[z1] ==-20){ */ 4686: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */ 4687: /* }else if(Tvaraff[z1] ==-10){ */ 4688: /* /\* sumnew+=coqvar[z1][iind]; *\/ */ 4689: /* }else */ 4690: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){ /* for combination j1 of covariates */ 4691: /* Tests if the value of the covariate z1 for this individual iind responded to combination j1 (V4=1 V3=0) */ 4692: bool=0; /* bool should be equal to 1 to be selected, one covariate value failed */ 4693: /* 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", 4694: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1), 4695: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/ 4696: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/ 4697: } /* Onlyf fixed */ 4698: } /* end z1 */ 4699: } /* cptcovn > 0 */ 4700: } /* end any */ 4701: }/* end j==0 */ 4702: if (bool==1){ /* We selected an individual iind satisfying combination j1 (V4=1 V3=0) or all fixed covariates */ 4703: /* for(m=firstpass; m<=lastpass; m++){ */ 4704: for(mi=1; mi<wav[iind];mi++){ /* For each wave */ 4705: m=mw[mi][iind]; 4706: if(j!=0){ 4707: if(anyvaryingduminmodel==1){ /* Some are varying covariates */ 4708: for (z1=1; z1<=cptcoveff; z1++) { 4709: if( Fixed[Tmodelind[z1]]==1){ 4710: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; 4711: if (cotvar[m][iv][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality. If covariate's 4712: value is -1, we don't select. It differs from the 4713: constant and age model which counts them. */ 4714: bool=0; /* not selected */ 4715: }else if( Fixed[Tmodelind[z1]]== 0) { /* fixed */ 4716: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) { 4717: bool=0; 4718: } 4719: } 4720: } 4721: }/* Some are varying covariates, we tried to speed up if all fixed covariates in the model, avoiding waves loop */ 4722: } /* end j==0 */ 4723: /* bool =0 we keep that guy which corresponds to the combination of dummy values */ 4724: if(bool==1){ /*Selected */ 4725: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind] 4726: and mw[mi+1][iind]. dh depends on stepm. */ 4727: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/ 4728: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */ 4729: if(m >=firstpass && m <=lastpass){ 4730: k2=anint[m][iind]+(mint[m][iind]/12.); 4731: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/ 4732: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */ 4733: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */ 4734: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */ 4735: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */ 4736: if (m<lastpass) { 4737: /* if(s[m][iind]==4 && s[m+1][iind]==4) */ 4738: /* 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]); */ 4739: if(s[m][iind]==-1) 4740: 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.)); 4741: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */ 4742: for (z1=1; z1<= nqfveff; z1++) { /* Quantitative variables, calculating mean on known values only */ 4743: if(!isnan(covar[ncovcol+z1][iind])){ 4744: idq[z1]=idq[z1]+weight[iind]; 4745: meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /* Computes mean of quantitative with selected filter */ 4746: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; *//*error*/ 4747: stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]; /* *weight[iind];*/ /* Computes mean of quantitative with selected filter */ 4748: } 4749: } 4750: /* if((int)agev[m][iind] == 55) */ 4751: /* printf("j=%d, j1=%d Age %d, iind=%d, num=%09ld m=%d\n",j,j1,(int)agev[m][iind],iind, num[iind],m); */ 4752: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */ 4753: 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 */ 4754: } 4755: } /* end if between passes */ 4756: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99) && (j==0)) { 4757: dateintsum=dateintsum+k2; /* on all covariates ?*/ 4758: k2cpt++; 4759: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */ 4760: } 4761: }else{ 4762: bool=1; 4763: }/* end bool 2 */ 4764: } /* end m */ 4765: /* for (z1=1; z1<= nqfveff; z1++) { /\* Quantitative variables, calculating mean *\/ */ 4766: /* idq[z1]=idq[z1]+weight[iind]; */ 4767: /* meanq[z1]+=covar[ncovcol+z1][iind]*weight[iind]; /\* Computes mean of quantitative with selected filter *\/ */ 4768: /* stdq[z1]+=covar[ncovcol+z1][iind]*covar[ncovcol+z1][iind]*weight[iind]*weight[iind]; /\* *weight[iind];*\/ /\* Computes mean of quantitative with selected filter *\/ */ 4769: /* } */ 4770: } /* end bool */ 4771: } /* end iind = 1 to imx */ 4772: /* prop[s][age] is feeded for any initial and valid live state as well as 4773: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */ 4774: 4775: 4776: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/ 4777: if(cptcoveff==0 && nj==1) /* no covariate and first pass */ 4778: pstamp(ficresp); 4779: if (cptcoveff>0 && j!=0){ 4780: pstamp(ficresp); 4781: printf( "\n#********** Variable "); 4782: fprintf(ficresp, "\n#********** Variable "); 4783: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable "); 4784: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable "); 4785: fprintf(ficlog, "\n#********** Variable "); 4786: for (z1=1; z1<=cptcoveff; z1++){ 4787: if(!FixedV[Tvaraff[z1]]){ 4788: printf( "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4789: fprintf(ficresp, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4790: fprintf(ficresphtm, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4791: fprintf(ficresphtmfr, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4792: fprintf(ficlog, "V%d(fixed)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4793: }else{ 4794: printf( "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4795: fprintf(ficresp, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4796: fprintf(ficresphtm, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4797: fprintf(ficresphtmfr, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4798: fprintf(ficlog, "V%d(varying)=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4799: } 4800: } 4801: printf( "**********\n#"); 4802: fprintf(ficresp, "**********\n#"); 4803: fprintf(ficresphtm, "**********</h3>\n"); 4804: fprintf(ficresphtmfr, "**********</h3>\n"); 4805: fprintf(ficlog, "**********\n"); 4806: } 4807: /* 4808: Printing means of quantitative variables if any 4809: */ 4810: for (z1=1; z1<= nqfveff; z1++) { 4811: fprintf(ficlog,"Mean of fixed quantitative variable V%d on %.3g (weighted) individuals sum=%f", ncovcol+z1, idq[z1], meanq[z1]); 4812: fprintf(ficlog,", mean=%.3g\n",meanq[z1]/idq[z1]); 4813: if(weightopt==1){ 4814: printf(" Weighted mean and standard deviation of"); 4815: fprintf(ficlog," Weighted mean and standard deviation of"); 4816: fprintf(ficresphtmfr," Weighted mean and standard deviation of"); 4817: } 4818: /* mu = \frac{w x}{\sum w} 4819: var = \frac{\sum w (x-mu)^2}{\sum w} = \frac{w x^2}{\sum w} - mu^2 4820: */ 4821: 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])); 4822: 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])); 4823: 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])); 4824: } 4825: /* for (z1=1; z1<= nqtveff; z1++) { */ 4826: /* for(m=1;m<=lastpass;m++){ */ 4827: /* fprintf(ficresphtmfr,"V quantitative id %d, pass id=%d, mean=%f<p>\n", z1, m, meanqt[m][z1]); */ 4828: /* } */ 4829: /* } */ 4830: 4831: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">"); 4832: if((cptcoveff==0 && nj==1)|| nj==2 ) /* no covariate and first pass */ 4833: fprintf(ficresp, " Age"); 4834: if(nj==2) for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " V%d=%d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4835: for(i=1; i<=nlstate;i++) { 4836: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," Prev(%d) N(%d) N ",i,i); 4837: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i); 4838: } 4839: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp, "\n"); 4840: fprintf(ficresphtm, "\n"); 4841: 4842: /* Header of frequency table by age */ 4843: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">"); 4844: fprintf(ficresphtmfr,"<th>Age</th> "); 4845: for(s2=-1; s2 <=nlstate+ndeath; s2++){ 4846: for(m=-1; m <=nlstate+ndeath; m++){ 4847: if(s2!=0 && m!=0) 4848: fprintf(ficresphtmfr,"<th>%d%d</th> ",s2,m); 4849: } 4850: } 4851: fprintf(ficresphtmfr, "\n"); 4852: 4853: /* For each age */ 4854: for(iage=iagemin; iage <= iagemax+3; iage++){ 4855: fprintf(ficresphtm,"<tr>"); 4856: if(iage==iagemax+1){ 4857: fprintf(ficlog,"1"); 4858: fprintf(ficresphtmfr,"<tr><th>0</th> "); 4859: }else if(iage==iagemax+2){ 4860: fprintf(ficlog,"0"); 4861: fprintf(ficresphtmfr,"<tr><th>Unknown</th> "); 4862: }else if(iage==iagemax+3){ 4863: fprintf(ficlog,"Total"); 4864: fprintf(ficresphtmfr,"<tr><th>Total</th> "); 4865: }else{ 4866: if(first==1){ 4867: first=0; 4868: printf("See log file for details...\n"); 4869: } 4870: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage); 4871: fprintf(ficlog,"Age %d", iage); 4872: } 4873: for(s1=1; s1 <=nlstate ; s1++){ 4874: for(m=-1, pp[s1]=0; m <=nlstate+ndeath ; m++) 4875: pp[s1] += freq[s1][m][iage]; 4876: } 4877: for(s1=1; s1 <=nlstate ; s1++){ 4878: for(m=-1, pos=0; m <=0 ; m++) 4879: pos += freq[s1][m][iage]; 4880: if(pp[s1]>=1.e-10){ 4881: if(first==1){ 4882: printf(" %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]); 4883: } 4884: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",s1,pp[s1],s1,100*pos/pp[s1]); 4885: }else{ 4886: if(first==1) 4887: printf(" %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1); 4888: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",s1,pp[s1],s1); 4889: } 4890: } 4891: 4892: for(s1=1; s1 <=nlstate ; s1++){ 4893: /* posprop[s1]=0; */ 4894: for(m=0, pp[s1]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */ 4895: pp[s1] += freq[s1][m][iage]; 4896: } /* pp[s1] is the total number of transitions starting from state s1 and any ending status until this age */ 4897: 4898: for(s1=1,pos=0, pospropta=0.; s1 <=nlstate ; s1++){ 4899: pos += pp[s1]; /* pos is the total number of transitions until this age */ 4900: posprop[s1] += prop[s1][iage]; /* prop is the number of transitions from a live state 4901: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */ 4902: pospropta += prop[s1][iage]; /* prop is the number of transitions from a live state 4903: from s1 at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */ 4904: } 4905: 4906: /* Writing ficresp */ 4907: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */ 4908: if( iage <= iagemax){ 4909: fprintf(ficresp," %d",iage); 4910: } 4911: }else if( nj==2){ 4912: if( iage <= iagemax){ 4913: fprintf(ficresp," %d",iage); 4914: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresp, " %d %d",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 4915: } 4916: } 4917: for(s1=1; s1 <=nlstate ; s1++){ 4918: if(pos>=1.e-5){ 4919: if(first==1) 4920: printf(" %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos); 4921: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",s1,pp[s1],s1,100*pp[s1]/pos); 4922: }else{ 4923: if(first==1) 4924: printf(" %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1); 4925: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",s1,pp[s1],s1); 4926: } 4927: if( iage <= iagemax){ 4928: if(pos>=1.e-5){ 4929: if(cptcoveff==0 && nj==1){ /* no covariate and first pass */ 4930: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta); 4931: }else if( nj==2){ 4932: fprintf(ficresp," %.5f %.0f %.0f",prop[s1][iage]/pospropta, prop[s1][iage],pospropta); 4933: } 4934: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[s1][iage]/pospropta, prop[s1][iage],pospropta); 4935: /*probs[iage][s1][j1]= pp[s1]/pos;*/ 4936: /*printf("\niage=%d s1=%d j1=%d %.5f %.0f %.0f %f",iage,s1,j1,pp[s1]/pos, pp[s1],pos,probs[iage][s1][j1]);*/ 4937: } else{ 4938: if((cptcoveff==0 && nj==1)|| nj==2 ) fprintf(ficresp," NaNq %.0f %.0f",prop[s1][iage],pospropta); 4939: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[s1][iage],pospropta); 4940: } 4941: } 4942: pospropt[s1] +=posprop[s1]; 4943: } /* end loop s1 */ 4944: /* pospropt=0.; */ 4945: for(s1=-1; s1 <=nlstate+ndeath; s1++){ 4946: for(m=-1; m <=nlstate+ndeath; m++){ 4947: if(freq[s1][m][iage] !=0 ) { /* minimizing output */ 4948: if(first==1){ 4949: printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); 4950: } 4951: /* printf(" %d%d=%.0f",s1,m,freq[s1][m][iage]); */ 4952: fprintf(ficlog," %d%d=%.0f",s1,m,freq[s1][m][iage]); 4953: } 4954: if(s1!=0 && m!=0) 4955: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[s1][m][iage]); 4956: } 4957: } /* end loop s1 */ 4958: posproptt=0.; 4959: for(s1=1; s1 <=nlstate; s1++){ 4960: posproptt += pospropt[s1]; 4961: } 4962: fprintf(ficresphtmfr,"</tr>\n "); 4963: fprintf(ficresphtm,"</tr>\n"); 4964: if((cptcoveff==0 && nj==1)|| nj==2 ) { 4965: if(iage <= iagemax) 4966: fprintf(ficresp,"\n"); 4967: } 4968: if(first==1) 4969: printf("Others in log...\n"); 4970: fprintf(ficlog,"\n"); 4971: } /* end loop age iage */ 4972: 4973: fprintf(ficresphtm,"<tr><th>Tot</th>"); 4974: for(s1=1; s1 <=nlstate ; s1++){ 4975: if(posproptt < 1.e-5){ 4976: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[s1],posproptt); 4977: }else{ 4978: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[s1]/posproptt,pospropt[s1],posproptt); 4979: } 4980: } 4981: fprintf(ficresphtm,"</tr>\n"); 4982: fprintf(ficresphtm,"</table>\n"); 4983: fprintf(ficresphtmfr,"</table>\n"); 4984: if(posproptt < 1.e-5){ 4985: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1); 4986: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1); 4987: fprintf(ficlog,"# This combination (%d) is not valid and no result will be produced\n",j1); 4988: printf("# This combination (%d) is not valid and no result will be produced\n",j1); 4989: invalidvarcomb[j1]=1; 4990: }else{ 4991: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1); 4992: invalidvarcomb[j1]=0; 4993: } 4994: fprintf(ficresphtmfr,"</table>\n"); 4995: fprintf(ficlog,"\n"); 4996: if(j!=0){ 4997: printf("#Freqsummary: Starting values for combination j1=%d:\n", j1); 4998: for(i=1,s1=1; i <=nlstate; i++){ 4999: for(k=1; k <=(nlstate+ndeath); k++){ 5000: if (k != i) { 5001: for(jj=1; jj <=ncovmodel; jj++){ /* For counting s1 */ 5002: if(jj==1){ /* Constant case (in fact cste + age) */ 5003: if(j1==1){ /* All dummy covariates to zero */ 5004: freq[i][k][iagemax+4]=freq[i][k][iagemax+3]; /* Stores case 0 0 0 */ 5005: freq[i][i][iagemax+4]=freq[i][i][iagemax+3]; /* Stores case 0 0 0 */ 5006: printf("%d%d ",i,k); 5007: fprintf(ficlog,"%d%d ",i,k); 5008: 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])); 5009: 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])); 5010: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]); 5011: } 5012: }else if((j1==1) && (jj==2 || nagesqr==1)){ /* age or age*age parameter without covariate V4*age (to be done later) */ 5013: for(iage=iagemin; iage <= iagemax+3; iage++){ 5014: x[iage]= (double)iage; 5015: y[iage]= log(freq[i][k][iage]/freq[i][i][iage]); 5016: /* printf("i=%d, k=%d, s1=%d, j1=%d, jj=%d, y[%d]=%f\n",i,k,s1,j1,jj, iage, y[iage]); */ 5017: } 5018: /* Some are not finite, but linreg will ignore these ages */ 5019: no=0; 5020: linreg(iagemin,iagemax,&no,x,y,&a,&b,&r, &sa, &sb ); /* y= a+b*x with standard errors */ 5021: pstart[s1]=b; 5022: pstart[s1-1]=a; 5023: }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 */ 5024: 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]); 5025: 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]); 5026: pstart[s1]= log((freq[i][k][iagemax+3]/freq[i][i][iagemax+3])/(freq[i][k][iagemax+4]/freq[i][i][iagemax+4])); 5027: printf("%d%d ",i,k); 5028: fprintf(ficlog,"%d%d ",i,k); 5029: 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])); 5030: }else{ /* Other cases, like quantitative fixed or varying covariates */ 5031: ; 5032: } 5033: /* printf("%12.7f )", param[i][jj][k]); */ 5034: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */ 5035: s1++; 5036: } /* end jj */ 5037: } /* end k!= i */ 5038: } /* end k */ 5039: } /* end i, s1 */ 5040: } /* end j !=0 */ 5041: } /* end selected combination of covariate j1 */ 5042: if(j==0){ /* We can estimate starting values from the occurences in each case */ 5043: printf("#Freqsummary: Starting values for the constants:\n"); 5044: fprintf(ficlog,"\n"); 5045: for(i=1,s1=1; i <=nlstate; i++){ 5046: for(k=1; k <=(nlstate+ndeath); k++){ 5047: if (k != i) { 5048: printf("%d%d ",i,k); 5049: fprintf(ficlog,"%d%d ",i,k); 5050: for(jj=1; jj <=ncovmodel; jj++){ 5051: pstart[s1]=p[s1]; /* Setting pstart to p values by default */ 5052: if(jj==1){ /* Age has to be done */ 5053: pstart[s1]= log(freq[i][k][iagemax+3]/freq[i][i][iagemax+3]); 5054: 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])); 5055: 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])); 5056: } 5057: /* printf("%12.7f )", param[i][jj][k]); */ 5058: /* fprintf(ficlog,"%12.7f )", param[i][jj][k]); */ 5059: s1++; 5060: } 5061: printf("\n"); 5062: fprintf(ficlog,"\n"); 5063: } 5064: } 5065: } /* end of state i */ 5066: printf("#Freqsummary\n"); 5067: fprintf(ficlog,"\n"); 5068: for(s1=-1; s1 <=nlstate+ndeath; s1++){ 5069: for(s2=-1; s2 <=nlstate+ndeath; s2++){ 5070: /* param[i]|j][k]= freq[s1][s2][iagemax+3] */ 5071: printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); 5072: fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); 5073: /* if(freq[s1][s2][iage] !=0 ) { /\* minimizing output *\/ */ 5074: /* printf(" %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */ 5075: /* fprintf(ficlog," %d%d=%.0f",s1,s2,freq[s1][s2][iagemax+3]); */ 5076: /* } */ 5077: } 5078: } /* end loop s1 */ 5079: 5080: printf("\n"); 5081: fprintf(ficlog,"\n"); 5082: } /* end j=0 */ 5083: } /* end j */ 5084: 5085: if(mle == -2){ /* We want to use these values as starting values */ 5086: for(i=1, jk=1; i <=nlstate; i++){ 5087: for(j=1; j <=nlstate+ndeath; j++){ 5088: if(j!=i){ 5089: /*ca[0]= k+'a'-1;ca[1]='\0';*/ 5090: printf("%1d%1d",i,j); 5091: fprintf(ficparo,"%1d%1d",i,j); 5092: for(k=1; k<=ncovmodel;k++){ 5093: /* printf(" %lf",param[i][j][k]); */ 5094: /* fprintf(ficparo," %lf",param[i][j][k]); */ 5095: p[jk]=pstart[jk]; 5096: printf(" %f ",pstart[jk]); 5097: fprintf(ficparo," %f ",pstart[jk]); 5098: jk++; 5099: } 5100: printf("\n"); 5101: fprintf(ficparo,"\n"); 5102: } 5103: } 5104: } 5105: } /* end mle=-2 */ 5106: dateintmean=dateintsum/k2cpt; 5107: date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); 5108: 5109: fclose(ficresp); 5110: fclose(ficresphtm); 5111: fclose(ficresphtmfr); 5112: free_vector(idq,1,nqfveff); 5113: free_vector(meanq,1,nqfveff); 5114: free_vector(stdq,1,nqfveff); 5115: free_matrix(meanqt,1,lastpass,1,nqtveff); 5116: free_vector(x, iagemin-AGEMARGE, iagemax+4+AGEMARGE); 5117: free_vector(y, iagemin-AGEMARGE, iagemax+4+AGEMARGE); 5118: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+4+AGEMARGE); 5119: free_vector(pospropt,1,nlstate); 5120: free_vector(posprop,1,nlstate); 5121: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+4+AGEMARGE); 5122: free_vector(pp,1,nlstate); 5123: /* End of freqsummary */ 5124: } 5125: 5126: /* Simple linear regression */ 5127: int linreg(int ifi, int ila, int *no, const double x[], const double y[], double* a, double* b, double* r, double* sa, double * sb) { 5128: 5129: /* y=a+bx regression */ 5130: double sumx = 0.0; /* sum of x */ 5131: double sumx2 = 0.0; /* sum of x**2 */ 5132: double sumxy = 0.0; /* sum of x * y */ 5133: double sumy = 0.0; /* sum of y */ 5134: double sumy2 = 0.0; /* sum of y**2 */ 5135: double sume2 = 0.0; /* sum of square or residuals */ 5136: double yhat; 5137: 5138: double denom=0; 5139: int i; 5140: int ne=*no; 5141: 5142: for ( i=ifi, ne=0;i<=ila;i++) { 5143: if(!isfinite(x[i]) || !isfinite(y[i])){ 5144: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */ 5145: continue; 5146: } 5147: ne=ne+1; 5148: sumx += x[i]; 5149: sumx2 += x[i]*x[i]; 5150: sumxy += x[i] * y[i]; 5151: sumy += y[i]; 5152: sumy2 += y[i]*y[i]; 5153: denom = (ne * sumx2 - sumx*sumx); 5154: /* 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); */ 5155: } 5156: 5157: denom = (ne * sumx2 - sumx*sumx); 5158: if (denom == 0) { 5159: // vertical, slope m is infinity 5160: *b = INFINITY; 5161: *a = 0; 5162: if (r) *r = 0; 5163: return 1; 5164: } 5165: 5166: *b = (ne * sumxy - sumx * sumy) / denom; 5167: *a = (sumy * sumx2 - sumx * sumxy) / denom; 5168: if (r!=NULL) { 5169: *r = (sumxy - sumx * sumy / ne) / /* compute correlation coeff */ 5170: sqrt((sumx2 - sumx*sumx/ne) * 5171: (sumy2 - sumy*sumy/ne)); 5172: } 5173: *no=ne; 5174: for ( i=ifi, ne=0;i<=ila;i++) { 5175: if(!isfinite(x[i]) || !isfinite(y[i])){ 5176: /* printf(" x[%d]=%f, y[%d]=%f\n",i,x[i],i,y[i]); */ 5177: continue; 5178: } 5179: ne=ne+1; 5180: yhat = y[i] - *a -*b* x[i]; 5181: sume2 += yhat * yhat ; 5182: 5183: denom = (ne * sumx2 - sumx*sumx); 5184: /* 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); */ 5185: } 5186: *sb = sqrt(sume2/(double)(ne-2)/(sumx2 - sumx * sumx /(double)ne)); 5187: *sa= *sb * sqrt(sumx2/ne); 5188: 5189: return 0; 5190: } 5191: 5192: /************ Prevalence ********************/ 5193: 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) 5194: { 5195: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people 5196: in each health status at the date of interview (if between dateprev1 and dateprev2). 5197: We still use firstpass and lastpass as another selection. 5198: */ 5199: 5200: int i, m, jk, j1, bool, z1,j, iv; 5201: int mi; /* Effective wave */ 5202: int iage; 5203: double agebegin, ageend; 5204: 5205: double **prop; 5206: double posprop; 5207: double y2; /* in fractional years */ 5208: int iagemin, iagemax; 5209: int first; /** to stop verbosity which is redirected to log file */ 5210: 5211: iagemin= (int) agemin; 5212: iagemax= (int) agemax; 5213: /*pp=vector(1,nlstate);*/ 5214: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+4+AGEMARGE); 5215: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/ 5216: j1=0; 5217: 5218: /*j=cptcoveff;*/ 5219: if (cptcovn<1) {j=1;ncodemax[1]=1;} 5220: 5221: first=0; 5222: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */ 5223: for (i=1; i<=nlstate; i++) 5224: for(iage=iagemin-AGEMARGE; iage <= iagemax+4+AGEMARGE; iage++) 5225: prop[i][iage]=0.0; 5226: printf("Prevalence combination of varying and fixed dummies %d\n",j1); 5227: /* fprintf(ficlog," V%d=%d ",Tvaraff[j1],nbcode[Tvaraff[j1]][codtabm(k,j1)]); */ 5228: fprintf(ficlog,"Prevalence combination of varying and fixed dummies %d\n",j1); 5229: 5230: for (i=1; i<=imx; i++) { /* Each individual */ 5231: bool=1; 5232: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */ 5233: for(mi=1; mi<wav[i];mi++){ /* For this wave too look where individual can be counted V4=0 V3=0 */ 5234: m=mw[mi][i]; 5235: /* Tmodelind[z1]=k is the position of the varying covariate in the model, but which # within 1 to ntv? */ 5236: /* Tvar[Tmodelind[z1]] is the n of Vn; n-ncovcol-nqv is the first time varying covariate or iv */ 5237: for (z1=1; z1<=cptcoveff; z1++){ 5238: if( Fixed[Tmodelind[z1]]==1){ 5239: iv= Tvar[Tmodelind[z1]]-ncovcol-nqv; 5240: if (cotvar[m][iv][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) /* iv=1 to ntv, right modality */ 5241: bool=0; 5242: }else if( Fixed[Tmodelind[z1]]== 0) /* fixed */ 5243: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]) { 5244: bool=0; 5245: } 5246: } 5247: if(bool==1){ /* Otherwise we skip that wave/person */ 5248: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/ 5249: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */ 5250: if(m >=firstpass && m <=lastpass){ 5251: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */ 5252: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */ 5253: if(agev[m][i]==0) agev[m][i]=iagemax+1; 5254: if(agev[m][i]==1) agev[m][i]=iagemax+2; 5255: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+4+AGEMARGE){ 5256: 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); 5257: exit(1); 5258: } 5259: if (s[m][i]>0 && s[m][i]<=nlstate) { 5260: /*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]]);*/ 5261: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */ 5262: prop[s[m][i]][iagemax+3] += weight[i]; 5263: } /* end valid statuses */ 5264: } /* end selection of dates */ 5265: } /* end selection of waves */ 5266: } /* end bool */ 5267: } /* end wave */ 5268: } /* end individual */ 5269: for(i=iagemin; i <= iagemax+3; i++){ 5270: for(jk=1,posprop=0; jk <=nlstate ; jk++) { 5271: posprop += prop[jk][i]; 5272: } 5273: 5274: for(jk=1; jk <=nlstate ; jk++){ 5275: if( i <= iagemax){ 5276: if(posprop>=1.e-5){ 5277: probs[i][jk][j1]= prop[jk][i]/posprop; 5278: } else{ 5279: if(!first){ 5280: first=1; 5281: 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]); 5282: }else{ 5283: 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]); 5284: } 5285: } 5286: } 5287: }/* end jk */ 5288: }/* end i */ 5289: /*} *//* end i1 */ 5290: } /* end j1 */ 5291: 5292: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/ 5293: /*free_vector(pp,1,nlstate);*/ 5294: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+4+AGEMARGE); 5295: } /* End of prevalence */ 5296: 5297: /************* Waves Concatenation ***************/ 5298: 5299: 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) 5300: { 5301: /* Concatenates waves: wav[i] is the number of effective (useful waves in the sense that a non interview is useless) of individual i. 5302: Death is a valid wave (if date is known). 5303: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i 5304: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i] 5305: and mw[mi+1][i]. dh depends on stepm. s[m][i] exists for any wave from firstpass to lastpass 5306: */ 5307: 5308: int i=0, mi=0, m=0, mli=0; 5309: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1; 5310: double sum=0., jmean=0.;*/ 5311: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0; 5312: int j, k=0,jk, ju, jl; 5313: double sum=0.; 5314: first=0; 5315: firstwo=0; 5316: firsthree=0; 5317: firstfour=0; 5318: jmin=100000; 5319: jmax=-1; 5320: jmean=0.; 5321: 5322: /* Treating live states */ 5323: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */ 5324: mi=0; /* First valid wave */ 5325: mli=0; /* Last valid wave */ 5326: m=firstpass; /* Loop on waves */ 5327: while(s[m][i] <= nlstate){ /* a live state or unknown state */ 5328: 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 */ 5329: mli=m-1;/* mw[++mi][i]=m-1; */ 5330: }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 */ 5331: mw[++mi][i]=m; /* Valid wave: incrementing mi and updating mi; mw[mi] is the wave number of mi_th valid transition */ 5332: mli=m; 5333: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */ 5334: if(m < lastpass){ /* m < lastpass, standard case */ 5335: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */ 5336: } 5337: else{ /* m = lastpass, eventual special issue with warning */ 5338: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING 5339: break; 5340: #else 5341: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){ /* no death date and known date of interview, case -2 (vital status unknown is warned later */ 5342: if(firsthree == 0){ 5343: 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); 5344: firsthree=1; 5345: }else if(firsthree >=1 && firsthree < 10){ 5346: 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); 5347: firsthree++; 5348: }else if(firsthree == 10){ 5349: printf("Information, too many Information flags: no more reported to log either\n"); 5350: fprintf(ficlog,"Information, too many Information flags: no more reported to log either\n"); 5351: firsthree++; 5352: }else{ 5353: firsthree++; 5354: } 5355: mw[++mi][i]=m; /* Valid transition with unknown status */ 5356: mli=m; 5357: } 5358: if(s[m][i]==-2){ /* Vital status is really unknown */ 5359: nbwarn++; 5360: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified?not a transition */ 5361: 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); 5362: 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); 5363: } 5364: break; 5365: } 5366: break; 5367: #endif 5368: }/* End m >= lastpass */ 5369: }/* end while */ 5370: 5371: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */ 5372: /* After last pass */ 5373: /* Treating death states */ 5374: if (s[m][i] > nlstate){ /* In a death state */ 5375: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */ 5376: /* } */ 5377: mi++; /* Death is another wave */ 5378: /* if(mi==0) never been interviewed correctly before death */ 5379: /* Only death is a correct wave */ 5380: mw[mi][i]=m; 5381: } /* else not in a death state */ 5382: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE 5383: else if ((int) andc[i] != 9999) { /* Date of death is known */ 5384: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */ 5385: 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 */ 5386: nbwarn++; 5387: if(firstfiv==0){ 5388: 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 ); 5389: firstfiv=1; 5390: }else{ 5391: 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 ); 5392: } 5393: s[m][i]=nlstate+1; /* Fixing the status as death. Be careful if multiple death states */ 5394: }else{ /* Month of Death occured afer last wave month, potential bias */ 5395: nberr++; 5396: if(firstwo==0){ 5397: 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 ); 5398: firstwo=1; 5399: } 5400: 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 ); 5401: } 5402: }else{ /* if date of interview is unknown */ 5403: /* death is known but not confirmed by death status at any wave */ 5404: if(firstfour==0){ 5405: 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 ); 5406: firstfour=1; 5407: } 5408: 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 ); 5409: } 5410: } /* end if date of death is known */ 5411: #endif 5412: wav[i]=mi; /* mi should be the last effective wave (or mli), */ 5413: /* wav[i]=mw[mi][i]; */ 5414: if(mi==0){ 5415: nbwarn++; 5416: if(first==0){ 5417: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i); 5418: first=1; 5419: } 5420: if(first==1){ 5421: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i); 5422: } 5423: } /* end mi==0 */ 5424: } /* End individuals */ 5425: /* wav and mw are no more changed */ 5426: 5427: printf("Information, you have to check %d informations which haven't been logged!\n",firsthree); 5428: fprintf(ficlog,"Information, you have to check %d informations which haven't been logged!\n",firsthree); 5429: 5430: 5431: for(i=1; i<=imx; i++){ 5432: for(mi=1; mi<wav[i];mi++){ 5433: if (stepm <=0) 5434: dh[mi][i]=1; 5435: else{ 5436: if (s[mw[mi+1][i]][i] > nlstate) { /* A death, but what if date is unknown? */ 5437: if (agedc[i] < 2*AGESUP) { 5438: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12); 5439: if(j==0) j=1; /* Survives at least one month after exam */ 5440: else if(j<0){ 5441: nberr++; 5442: 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]); 5443: j=1; /* Temporary Dangerous patch */ 5444: 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); 5445: 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]); 5446: 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); 5447: } 5448: k=k+1; 5449: if (j >= jmax){ 5450: jmax=j; 5451: ijmax=i; 5452: } 5453: if (j <= jmin){ 5454: jmin=j; 5455: ijmin=i; 5456: } 5457: sum=sum+j; 5458: /*if (j<0) printf("j=%d num=%d \n",j,i);*/ 5459: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/ 5460: } 5461: } 5462: else{ 5463: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12)); 5464: /* 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]); */ 5465: 5466: k=k+1; 5467: if (j >= jmax) { 5468: jmax=j; 5469: ijmax=i; 5470: } 5471: else if (j <= jmin){ 5472: jmin=j; 5473: ijmin=i; 5474: } 5475: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */ 5476: /*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]);*/ 5477: if(j<0){ 5478: nberr++; 5479: 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]); 5480: 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]); 5481: } 5482: sum=sum+j; 5483: } 5484: jk= j/stepm; 5485: jl= j -jk*stepm; 5486: ju= j -(jk+1)*stepm; 5487: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */ 5488: if(jl==0){ 5489: dh[mi][i]=jk; 5490: bh[mi][i]=0; 5491: }else{ /* We want a negative bias in order to only have interpolation ie 5492: * to avoid the price of an extra matrix product in likelihood */ 5493: dh[mi][i]=jk+1; 5494: bh[mi][i]=ju; 5495: } 5496: }else{ 5497: if(jl <= -ju){ 5498: dh[mi][i]=jk; 5499: bh[mi][i]=jl; /* bias is positive if real duration 5500: * is higher than the multiple of stepm and negative otherwise. 5501: */ 5502: } 5503: else{ 5504: dh[mi][i]=jk+1; 5505: bh[mi][i]=ju; 5506: } 5507: if(dh[mi][i]==0){ 5508: dh[mi][i]=1; /* At least one step */ 5509: bh[mi][i]=ju; /* At least one step */ 5510: /* 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);*/ 5511: } 5512: } /* end if mle */ 5513: } 5514: } /* end wave */ 5515: } 5516: jmean=sum/k; 5517: 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); 5518: 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); 5519: } 5520: 5521: /*********** Tricode ****************************/ 5522: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum) 5523: { 5524: /**< Uses cptcovn+2*cptcovprod as the number of covariates */ 5525: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1 5526: * Boring subroutine which should only output nbcode[Tvar[j]][k] 5527: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable 5528: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually); 5529: */ 5530: 5531: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX; 5532: int modmaxcovj=0; /* Modality max of covariates j */ 5533: int cptcode=0; /* Modality max of covariates j */ 5534: int modmincovj=0; /* Modality min of covariates j */ 5535: 5536: 5537: /* cptcoveff=0; */ 5538: /* *cptcov=0; */ 5539: 5540: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */ 5541: for (k=1; k <= maxncov; k++) 5542: for(j=1; j<=2; j++) 5543: nbcode[k][j]=0; /* Valgrind */ 5544: 5545: /* Loop on covariates without age and products and no quantitative variable */ 5546: for (k=1; k<=cptcovt; k++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */ 5547: for (j=-1; (j < maxncov); j++) Ndum[j]=0; 5548: if(Dummy[k]==0 && Typevar[k] !=1){ /* Dummy covariate and not age product */ 5549: switch(Fixed[k]) { 5550: case 0: /* Testing on fixed dummy covariate, simple or product of fixed */ 5551: modmaxcovj=0; 5552: modmincovj=0; 5553: 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*/ 5554: ij=(int)(covar[Tvar[k]][i]); 5555: /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i 5556: * If product of Vn*Vm, still boolean *: 5557: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables 5558: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */ 5559: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the 5560: modality of the nth covariate of individual i. */ 5561: if (ij > modmaxcovj) 5562: modmaxcovj=ij; 5563: else if (ij < modmincovj) 5564: modmincovj=ij; 5565: if (ij <0 || ij >1 ){ 5566: printf("ERROR, IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i); 5567: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=-1, individual %d will be skipped.\n",Tvar[k],i); 5568: fflush(ficlog); 5569: exit(1); 5570: } 5571: if ((ij < -1) || (ij > NCOVMAX)){ 5572: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX ); 5573: exit(1); 5574: }else 5575: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/ 5576: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */ 5577: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/ 5578: /* getting the maximum value of the modality of the covariate 5579: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and 5580: female ies 1, then modmaxcovj=1. 5581: */ 5582: } /* end for loop on individuals i */ 5583: printf(" Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj); 5584: fprintf(ficlog," Minimal and maximal values of %d th (fixed) covariate V%d: min=%d max=%d \n", k, Tvar[k], modmincovj, modmaxcovj); 5585: cptcode=modmaxcovj; 5586: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */ 5587: /*for (i=0; i<=cptcode; i++) {*/ 5588: for (j=modmincovj; j<=modmaxcovj; j++) { /* j=-1 ? 0 and 1*//* For each value j of the modality of model-cov k */ 5589: printf("Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]); 5590: fprintf(ficlog, "Frequencies of (fixed) covariate %d ie V%d with value %d: %d\n", k, Tvar[k], j, Ndum[j]); 5591: if( Ndum[j] != 0 ){ /* Counts if nobody answered modality j ie empty modality, we skip it and reorder */ 5592: if( j != -1){ 5593: ncodemax[k]++; /* ncodemax[k]= Number of modalities of the k th 5594: covariate for which somebody answered excluding 5595: undefined. Usually 2: 0 and 1. */ 5596: } 5597: ncodemaxwundef[k]++; /* ncodemax[j]= Number of modalities of the k th 5598: covariate for which somebody answered including 5599: undefined. Usually 3: -1, 0 and 1. */ 5600: } /* In fact ncodemax[k]=2 (dichotom. variables only) but it could be more for 5601: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */ 5602: } /* Ndum[-1] number of undefined modalities */ 5603: 5604: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */ 5605: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7. */ 5606: /* If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125; */ 5607: /* modmincovj=3; modmaxcovj = 7; */ 5608: /* There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3; */ 5609: /* which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10; */ 5610: /* defining two dummy variables: variables V1_1 and V1_2.*/ 5611: /* nbcode[Tvar[j]][ij]=k; */ 5612: /* nbcode[Tvar[j]][1]=0; */ 5613: /* nbcode[Tvar[j]][2]=1; */ 5614: /* nbcode[Tvar[j]][3]=2; */ 5615: /* To be continued (not working yet). */ 5616: ij=0; /* ij is similar to i but can jump over null modalities */ 5617: 5618: /* 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*/ 5619: /* Skipping the case of missing values by reducing nbcode to 0 and 1 and not -1, 0, 1 */ 5620: /* model=V1+V2+V3, if V2=-1, 0 or 1, then nbcode[2][1]=0 and nbcode[2][2]=1 instead of 5621: * nbcode[2][1]=-1, nbcode[2][2]=0 and nbcode[2][3]=1 */ 5622: /*, could be restored in the future */ 5623: 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*/ 5624: if (Ndum[i] == 0) { /* If nobody responded to this modality k */ 5625: break; 5626: } 5627: ij++; 5628: 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*/ 5629: cptcode = ij; /* New max modality for covar j */ 5630: } /* end of loop on modality i=-1 to 1 or more */ 5631: break; 5632: case 1: /* Testing on varying covariate, could be simple and 5633: * should look at waves or product of fixed * 5634: * varying. No time to test -1, assuming 0 and 1 only */ 5635: ij=0; 5636: for(i=0; i<=1;i++){ 5637: nbcode[Tvar[k]][++ij]=i; 5638: } 5639: break; 5640: default: 5641: break; 5642: } /* end switch */ 5643: } /* end dummy test */ 5644: if(Dummy[k]==1 && Typevar[k] !=1){ /* Dummy covariate and not age product */ 5645: 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*/ 5646: if(isnan(covar[Tvar[k]][i])){ 5647: printf("ERROR, IMaCh doesn't treat fixed quantitative covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i); 5648: fprintf(ficlog,"ERROR, currently IMaCh doesn't treat covariate with missing values V%d=., individual %d will be skipped.\n",Tvar[k],i); 5649: fflush(ficlog); 5650: exit(1); 5651: } 5652: } 5653: } 5654: } /* 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*/ 5655: 5656: for (k=-1; k< maxncov; k++) Ndum[k]=0; 5657: /* Look at fixed dummy (single or product) covariates to check empty modalities */ 5658: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */ 5659: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/ 5660: 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 */ 5661: 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 */ 5662: /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, {2, 1, 1, 1, 2, 1, 1, 0, 0} */ 5663: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */ 5664: 5665: ij=0; 5666: /* for (i=0; i<= maxncov-1; i++) { /\* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) *\/ */ 5667: for (k=1; k<= cptcovt; k++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */ 5668: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/ 5669: /* if((Ndum[i]!=0) && (i<=ncovcol)){ /\* Tvar[i] <= ncovmodel ? *\/ */ 5670: if(Ndum[Tvar[k]]!=0 && Dummy[k] == 0 && Typevar[k]==0){ /* Only Dummy and non empty in the model */ 5671: /* If product not in single variable we don't print results */ 5672: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/ 5673: ++ij;/* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, */ 5674: 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*/ 5675: Tmodelind[ij]=k; /* Tmodelind: index in model of dummies Tmodelind[1]=2 V4: pos=2; V3: pos=3, V1=9 {2, 3, 9, ?, ?,} */ 5676: 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 */ 5677: if(Fixed[k]!=0) 5678: anyvaryingduminmodel=1; 5679: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){ */ 5680: /* Tvaraff[++ij]=-10; /\* Dont'n know how to treat quantitative variables yet *\/ */ 5681: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){ */ 5682: /* Tvaraff[++ij]=i; /\*For printing (unclear) *\/ */ 5683: /* }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){ */ 5684: /* Tvaraff[++ij]=-20; /\* Dont'n know how to treat quantitative variables yet *\/ */ 5685: } 5686: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */ 5687: /* ij--; */ 5688: /* cptcoveff=ij; /\*Number of total covariates*\/ */ 5689: *cptcov=ij; /*Number of total real effective covariates: effective 5690: * because they can be excluded from the model and real 5691: * if in the model but excluded because missing values, but how to get k from ij?*/ 5692: for(j=ij+1; j<= cptcovt; j++){ 5693: Tvaraff[j]=0; 5694: Tmodelind[j]=0; 5695: } 5696: for(j=ntveff+1; j<= cptcovt; j++){ 5697: TmodelInvind[j]=0; 5698: } 5699: /* To be sorted */ 5700: ; 5701: } 5702: 5703: 5704: /*********** Health Expectancies ****************/ 5705: 5706: 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 ) 5707: 5708: { 5709: /* Health expectancies, no variances */ 5710: int i, j, nhstepm, hstepm, h, nstepm; 5711: int nhstepma, nstepma; /* Decreasing with age */ 5712: double age, agelim, hf; 5713: double ***p3mat; 5714: double eip; 5715: 5716: /* pstamp(ficreseij); */ 5717: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n"); 5718: fprintf(ficreseij,"# Age"); 5719: for(i=1; i<=nlstate;i++){ 5720: for(j=1; j<=nlstate;j++){ 5721: fprintf(ficreseij," e%1d%1d ",i,j); 5722: } 5723: fprintf(ficreseij," e%1d. ",i); 5724: } 5725: fprintf(ficreseij,"\n"); 5726: 5727: 5728: if(estepm < stepm){ 5729: printf ("Problem %d lower than %d\n",estepm, stepm); 5730: } 5731: else hstepm=estepm; 5732: /* We compute the life expectancy from trapezoids spaced every estepm months 5733: * This is mainly to measure the difference between two models: for example 5734: * if stepm=24 months pijx are given only every 2 years and by summing them 5735: * we are calculating an estimate of the Life Expectancy assuming a linear 5736: * progression in between and thus overestimating or underestimating according 5737: * to the curvature of the survival function. If, for the same date, we 5738: * estimate the model with stepm=1 month, we can keep estepm to 24 months 5739: * to compare the new estimate of Life expectancy with the same linear 5740: * hypothesis. A more precise result, taking into account a more precise 5741: * curvature will be obtained if estepm is as small as stepm. */ 5742: 5743: /* For example we decided to compute the life expectancy with the smallest unit */ 5744: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm. 5745: nhstepm is the number of hstepm from age to agelim 5746: nstepm is the number of stepm from age to agelin. 5747: Look at hpijx to understand the reason which relies in memory size consideration 5748: and note for a fixed period like estepm months */ 5749: /* We decided (b) to get a life expectancy respecting the most precise curvature of the 5750: survival function given by stepm (the optimization length). Unfortunately it 5751: means that if the survival funtion is printed only each two years of age and if 5752: you sum them up and add 1 year (area under the trapezoids) you won't get the same 5753: results. So we changed our mind and took the option of the best precision. 5754: */ 5755: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */ 5756: 5757: agelim=AGESUP; 5758: /* If stepm=6 months */ 5759: /* Computed by stepm unit matrices, product of hstepm matrices, stored 5760: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */ 5761: 5762: /* nhstepm age range expressed in number of stepm */ 5763: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */ 5764: /* Typically if 20 years nstepm = 20*12/6=40 stepm */ 5765: /* if (stepm >= YEARM) hstepm=1;*/ 5766: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */ 5767: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 5768: 5769: for (age=bage; age<=fage; age ++){ 5770: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */ 5771: /* Typically if 20 years nstepm = 20*12/6=40 stepm */ 5772: /* if (stepm >= YEARM) hstepm=1;*/ 5773: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */ 5774: 5775: /* If stepm=6 months */ 5776: /* Computed by stepm unit matrices, product of hstepma matrices, stored 5777: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */ 5778: 5779: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij, nres); 5780: 5781: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */ 5782: 5783: printf("%d|",(int)age);fflush(stdout); 5784: fprintf(ficlog,"%d|",(int)age);fflush(ficlog); 5785: 5786: /* Computing expectancies */ 5787: for(i=1; i<=nlstate;i++) 5788: for(j=1; j<=nlstate;j++) 5789: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){ 5790: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf; 5791: 5792: /* 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]);*/ 5793: 5794: } 5795: 5796: fprintf(ficreseij,"%3.0f",age ); 5797: for(i=1; i<=nlstate;i++){ 5798: eip=0; 5799: for(j=1; j<=nlstate;j++){ 5800: eip +=eij[i][j][(int)age]; 5801: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] ); 5802: } 5803: fprintf(ficreseij,"%9.4f", eip ); 5804: } 5805: fprintf(ficreseij,"\n"); 5806: 5807: } 5808: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 5809: printf("\n"); 5810: fprintf(ficlog,"\n"); 5811: 5812: } 5813: 5814: 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 ) 5815: 5816: { 5817: /* Covariances of health expectancies eij and of total life expectancies according 5818: to initial status i, ei. . 5819: */ 5820: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji; 5821: int nhstepma, nstepma; /* Decreasing with age */ 5822: double age, agelim, hf; 5823: double ***p3matp, ***p3matm, ***varhe; 5824: double **dnewm,**doldm; 5825: double *xp, *xm; 5826: double **gp, **gm; 5827: double ***gradg, ***trgradg; 5828: int theta; 5829: 5830: double eip, vip; 5831: 5832: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage); 5833: xp=vector(1,npar); 5834: xm=vector(1,npar); 5835: dnewm=matrix(1,nlstate*nlstate,1,npar); 5836: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate); 5837: 5838: pstamp(ficresstdeij); 5839: fprintf(ficresstdeij,"# Health expectancies with standard errors\n"); 5840: fprintf(ficresstdeij,"# Age"); 5841: for(i=1; i<=nlstate;i++){ 5842: for(j=1; j<=nlstate;j++) 5843: fprintf(ficresstdeij," e%1d%1d (SE)",i,j); 5844: fprintf(ficresstdeij," e%1d. ",i); 5845: } 5846: fprintf(ficresstdeij,"\n"); 5847: 5848: pstamp(ficrescveij); 5849: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n"); 5850: fprintf(ficrescveij,"# Age"); 5851: for(i=1; i<=nlstate;i++) 5852: for(j=1; j<=nlstate;j++){ 5853: cptj= (j-1)*nlstate+i; 5854: for(i2=1; i2<=nlstate;i2++) 5855: for(j2=1; j2<=nlstate;j2++){ 5856: cptj2= (j2-1)*nlstate+i2; 5857: if(cptj2 <= cptj) 5858: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2); 5859: } 5860: } 5861: fprintf(ficrescveij,"\n"); 5862: 5863: if(estepm < stepm){ 5864: printf ("Problem %d lower than %d\n",estepm, stepm); 5865: } 5866: else hstepm=estepm; 5867: /* We compute the life expectancy from trapezoids spaced every estepm months 5868: * This is mainly to measure the difference between two models: for example 5869: * if stepm=24 months pijx are given only every 2 years and by summing them 5870: * we are calculating an estimate of the Life Expectancy assuming a linear 5871: * progression in between and thus overestimating or underestimating according 5872: * to the curvature of the survival function. If, for the same date, we 5873: * estimate the model with stepm=1 month, we can keep estepm to 24 months 5874: * to compare the new estimate of Life expectancy with the same linear 5875: * hypothesis. A more precise result, taking into account a more precise 5876: * curvature will be obtained if estepm is as small as stepm. */ 5877: 5878: /* For example we decided to compute the life expectancy with the smallest unit */ 5879: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm. 5880: nhstepm is the number of hstepm from age to agelim 5881: nstepm is the number of stepm from age to agelin. 5882: Look at hpijx to understand the reason of that which relies in memory size 5883: and note for a fixed period like estepm months */ 5884: /* We decided (b) to get a life expectancy respecting the most precise curvature of the 5885: survival function given by stepm (the optimization length). Unfortunately it 5886: means that if the survival funtion is printed only each two years of age and if 5887: you sum them up and add 1 year (area under the trapezoids) you won't get the same 5888: results. So we changed our mind and took the option of the best precision. 5889: */ 5890: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */ 5891: 5892: /* If stepm=6 months */ 5893: /* nhstepm age range expressed in number of stepm */ 5894: agelim=AGESUP; 5895: nstepm=(int) rint((agelim-bage)*YEARM/stepm); 5896: /* Typically if 20 years nstepm = 20*12/6=40 stepm */ 5897: /* if (stepm >= YEARM) hstepm=1;*/ 5898: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */ 5899: 5900: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 5901: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 5902: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate); 5903: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar); 5904: gp=matrix(0,nhstepm,1,nlstate*nlstate); 5905: gm=matrix(0,nhstepm,1,nlstate*nlstate); 5906: 5907: for (age=bage; age<=fage; age ++){ 5908: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */ 5909: /* Typically if 20 years nstepm = 20*12/6=40 stepm */ 5910: /* if (stepm >= YEARM) hstepm=1;*/ 5911: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */ 5912: 5913: /* If stepm=6 months */ 5914: /* Computed by stepm unit matrices, product of hstepma matrices, stored 5915: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */ 5916: 5917: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */ 5918: 5919: /* Computing Variances of health expectancies */ 5920: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to 5921: decrease memory allocation */ 5922: for(theta=1; theta <=npar; theta++){ 5923: for(i=1; i<=npar; i++){ 5924: xp[i] = x[i] + (i==theta ?delti[theta]:0); 5925: xm[i] = x[i] - (i==theta ?delti[theta]:0); 5926: } 5927: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij, nres); 5928: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij, nres); 5929: 5930: for(j=1; j<= nlstate; j++){ 5931: for(i=1; i<=nlstate; i++){ 5932: for(h=0; h<=nhstepm-1; h++){ 5933: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.; 5934: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.; 5935: } 5936: } 5937: } 5938: 5939: for(ij=1; ij<= nlstate*nlstate; ij++) 5940: for(h=0; h<=nhstepm-1; h++){ 5941: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta]; 5942: } 5943: }/* End theta */ 5944: 5945: 5946: for(h=0; h<=nhstepm-1; h++) 5947: for(j=1; j<=nlstate*nlstate;j++) 5948: for(theta=1; theta <=npar; theta++) 5949: trgradg[h][j][theta]=gradg[h][theta][j]; 5950: 5951: 5952: for(ij=1;ij<=nlstate*nlstate;ij++) 5953: for(ji=1;ji<=nlstate*nlstate;ji++) 5954: varhe[ij][ji][(int)age] =0.; 5955: 5956: printf("%d|",(int)age);fflush(stdout); 5957: fprintf(ficlog,"%d|",(int)age);fflush(ficlog); 5958: for(h=0;h<=nhstepm-1;h++){ 5959: for(k=0;k<=nhstepm-1;k++){ 5960: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov); 5961: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]); 5962: for(ij=1;ij<=nlstate*nlstate;ij++) 5963: for(ji=1;ji<=nlstate*nlstate;ji++) 5964: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf; 5965: } 5966: } 5967: 5968: /* Computing expectancies */ 5969: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij,nres); 5970: for(i=1; i<=nlstate;i++) 5971: for(j=1; j<=nlstate;j++) 5972: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){ 5973: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf; 5974: 5975: /* 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]);*/ 5976: 5977: } 5978: 5979: /* Standard deviation of expectancies ij */ 5980: fprintf(ficresstdeij,"%3.0f",age ); 5981: for(i=1; i<=nlstate;i++){ 5982: eip=0.; 5983: vip=0.; 5984: for(j=1; j<=nlstate;j++){ 5985: eip += eij[i][j][(int)age]; 5986: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */ 5987: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age]; 5988: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) ); 5989: } 5990: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip)); 5991: } 5992: fprintf(ficresstdeij,"\n"); 5993: 5994: /* Variance of expectancies ij */ 5995: fprintf(ficrescveij,"%3.0f",age ); 5996: for(i=1; i<=nlstate;i++) 5997: for(j=1; j<=nlstate;j++){ 5998: cptj= (j-1)*nlstate+i; 5999: for(i2=1; i2<=nlstate;i2++) 6000: for(j2=1; j2<=nlstate;j2++){ 6001: cptj2= (j2-1)*nlstate+i2; 6002: if(cptj2 <= cptj) 6003: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]); 6004: } 6005: } 6006: fprintf(ficrescveij,"\n"); 6007: 6008: } 6009: free_matrix(gm,0,nhstepm,1,nlstate*nlstate); 6010: free_matrix(gp,0,nhstepm,1,nlstate*nlstate); 6011: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate); 6012: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar); 6013: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 6014: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 6015: printf("\n"); 6016: fprintf(ficlog,"\n"); 6017: 6018: free_vector(xm,1,npar); 6019: free_vector(xp,1,npar); 6020: free_matrix(dnewm,1,nlstate*nlstate,1,npar); 6021: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate); 6022: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage); 6023: } 6024: 6025: /************ Variance ******************/ 6026: 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) 6027: { 6028: /** Variance of health expectancies 6029: * double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl); 6030: * double **newm; 6031: * int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav) 6032: */ 6033: 6034: /* int movingaverage(); */ 6035: double **dnewm,**doldm; 6036: double **dnewmp,**doldmp; 6037: int i, j, nhstepm, hstepm, h, nstepm ; 6038: int first=0; 6039: int k; 6040: double *xp; 6041: double **gp, **gm; /**< for var eij */ 6042: double ***gradg, ***trgradg; /**< for var eij */ 6043: double **gradgp, **trgradgp; /**< for var p point j */ 6044: double *gpp, *gmp; /**< for var p point j */ 6045: double **varppt; /**< for var p point j nlstate to nlstate+ndeath */ 6046: double ***p3mat; 6047: double age,agelim, hf; 6048: /* double ***mobaverage; */ 6049: int theta; 6050: char digit[4]; 6051: char digitp[25]; 6052: 6053: char fileresprobmorprev[FILENAMELENGTH]; 6054: 6055: if(popbased==1){ 6056: if(mobilav!=0) 6057: strcpy(digitp,"-POPULBASED-MOBILAV_"); 6058: else strcpy(digitp,"-POPULBASED-NOMOBIL_"); 6059: } 6060: else 6061: strcpy(digitp,"-STABLBASED_"); 6062: 6063: /* if (mobilav!=0) { */ 6064: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */ 6065: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */ 6066: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */ 6067: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */ 6068: /* } */ 6069: /* } */ 6070: 6071: strcpy(fileresprobmorprev,"PRMORPREV-"); 6072: sprintf(digit,"%-d",ij); 6073: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/ 6074: strcat(fileresprobmorprev,digit); /* Tvar to be done */ 6075: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */ 6076: strcat(fileresprobmorprev,fileresu); 6077: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) { 6078: printf("Problem with resultfile: %s\n", fileresprobmorprev); 6079: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev); 6080: } 6081: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev); 6082: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev); 6083: pstamp(ficresprobmorprev); 6084: 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); 6085: fprintf(ficresprobmorprev,"# Selected quantitative variables and dummies"); 6086: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 6087: fprintf(ficresprobmorprev," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 6088: } 6089: for(j=1;j<=cptcoveff;j++) 6090: fprintf(ficresprobmorprev,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(ij,j)]); 6091: fprintf(ficresprobmorprev,"\n"); 6092: 6093: fprintf(ficresprobmorprev,"# Age cov=%-d",ij); 6094: for(j=nlstate+1; j<=(nlstate+ndeath);j++){ 6095: fprintf(ficresprobmorprev," p.%-d SE",j); 6096: for(i=1; i<=nlstate;i++) 6097: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j); 6098: } 6099: fprintf(ficresprobmorprev,"\n"); 6100: 6101: fprintf(ficgp,"\n# Routine varevsij"); 6102: fprintf(ficgp,"\nunset title \n"); 6103: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/ 6104: 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"); 6105: fprintf(fichtm,"\n<br>%s <br>\n",digitp); 6106: 6107: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); 6108: pstamp(ficresvij); 6109: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are "); 6110: if(popbased==1) 6111: 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); 6112: else 6113: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n"); 6114: fprintf(ficresvij,"# Age"); 6115: for(i=1; i<=nlstate;i++) 6116: for(j=1; j<=nlstate;j++) 6117: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j); 6118: fprintf(ficresvij,"\n"); 6119: 6120: xp=vector(1,npar); 6121: dnewm=matrix(1,nlstate,1,npar); 6122: doldm=matrix(1,nlstate,1,nlstate); 6123: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar); 6124: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); 6125: 6126: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath); 6127: gpp=vector(nlstate+1,nlstate+ndeath); 6128: gmp=vector(nlstate+1,nlstate+ndeath); 6129: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/ 6130: 6131: if(estepm < stepm){ 6132: printf ("Problem %d lower than %d\n",estepm, stepm); 6133: } 6134: else hstepm=estepm; 6135: /* For example we decided to compute the life expectancy with the smallest unit */ 6136: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm. 6137: nhstepm is the number of hstepm from age to agelim 6138: nstepm is the number of stepm from age to agelim. 6139: Look at function hpijx to understand why because of memory size limitations, 6140: we decided (b) to get a life expectancy respecting the most precise curvature of the 6141: survival function given by stepm (the optimization length). Unfortunately it 6142: means that if the survival funtion is printed every two years of age and if 6143: you sum them up and add 1 year (area under the trapezoids) you won't get the same 6144: results. So we changed our mind and took the option of the best precision. 6145: */ 6146: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */ 6147: agelim = AGESUP; 6148: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */ 6149: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 6150: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */ 6151: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 6152: gradg=ma3x(0,nhstepm,1,npar,1,nlstate); 6153: gp=matrix(0,nhstepm,1,nlstate); 6154: gm=matrix(0,nhstepm,1,nlstate); 6155: 6156: 6157: for(theta=1; theta <=npar; theta++){ 6158: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/ 6159: xp[i] = x[i] + (i==theta ?delti[theta]:0); 6160: } 6161: /**< Computes the prevalence limit with parameter theta shifted of delta up to ftolpl precision and 6162: * returns into prlim . 6163: */ 6164: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij, nres); 6165: 6166: /* If popbased = 1 we use crossection prevalences. Previous step is useless but prlim is created */ 6167: if (popbased==1) { 6168: if(mobilav ==0){ 6169: for(i=1; i<=nlstate;i++) 6170: prlim[i][i]=probs[(int)age][i][ij]; 6171: }else{ /* mobilav */ 6172: for(i=1; i<=nlstate;i++) 6173: prlim[i][i]=mobaverage[(int)age][i][ij]; 6174: } 6175: } 6176: /**< Computes the shifted transition matrix \f$ {}{h}_p^{ij}x\f$ at horizon h. 6177: */ 6178: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); /* Returns p3mat[i][j][h] for h=0 to nhstepm */ 6179: /**< And for each alive state j, sums over i \f$ w^i_x {}{h}_p^{ij}x\f$, which are the probability 6180: * at horizon h in state j including mortality. 6181: */ 6182: for(j=1; j<= nlstate; j++){ 6183: for(h=0; h<=nhstepm; h++){ 6184: for(i=1, gp[h][j]=0.;i<=nlstate;i++) 6185: gp[h][j] += prlim[i][i]*p3mat[i][j][h]; 6186: } 6187: } 6188: /* Next for computing shifted+ probability of death (h=1 means 6189: computed over hstepm matrices product = hstepm*stepm months) 6190: as a weighted average of prlim(i) * p(i,j) p.3=w1*p13 + w2*p23 . 6191: */ 6192: for(j=nlstate+1;j<=nlstate+ndeath;j++){ 6193: for(i=1,gpp[j]=0.; i<= nlstate; i++) 6194: gpp[j] += prlim[i][i]*p3mat[i][j][1]; 6195: } 6196: 6197: /* Again with minus shift */ 6198: 6199: for(i=1; i<=npar; i++) /* Computes gradient x - delta */ 6200: xp[i] = x[i] - (i==theta ?delti[theta]:0); 6201: 6202: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij, nres); 6203: 6204: if (popbased==1) { 6205: if(mobilav ==0){ 6206: for(i=1; i<=nlstate;i++) 6207: prlim[i][i]=probs[(int)age][i][ij]; 6208: }else{ /* mobilav */ 6209: for(i=1; i<=nlstate;i++) 6210: prlim[i][i]=mobaverage[(int)age][i][ij]; 6211: } 6212: } 6213: 6214: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij,nres); 6215: 6216: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */ 6217: for(h=0; h<=nhstepm; h++){ 6218: for(i=1, gm[h][j]=0.;i<=nlstate;i++) 6219: gm[h][j] += prlim[i][i]*p3mat[i][j][h]; 6220: } 6221: } 6222: /* This for computing probability of death (h=1 means 6223: computed over hstepm matrices product = hstepm*stepm months) 6224: as a weighted average of prlim. 6225: */ 6226: for(j=nlstate+1;j<=nlstate+ndeath;j++){ 6227: for(i=1,gmp[j]=0.; i<= nlstate; i++) 6228: gmp[j] += prlim[i][i]*p3mat[i][j][1]; 6229: } 6230: /* end shifting computations */ 6231: 6232: /**< Computing gradient matrix at horizon h 6233: */ 6234: for(j=1; j<= nlstate; j++) /* vareij */ 6235: for(h=0; h<=nhstepm; h++){ 6236: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta]; 6237: } 6238: /**< Gradient of overall mortality p.3 (or p.j) 6239: */ 6240: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu mortality from j */ 6241: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta]; 6242: } 6243: 6244: } /* End theta */ 6245: 6246: /* We got the gradient matrix for each theta and state j */ 6247: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */ 6248: 6249: for(h=0; h<=nhstepm; h++) /* veij */ 6250: for(j=1; j<=nlstate;j++) 6251: for(theta=1; theta <=npar; theta++) 6252: trgradg[h][j][theta]=gradg[h][theta][j]; 6253: 6254: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */ 6255: for(theta=1; theta <=npar; theta++) 6256: trgradgp[j][theta]=gradgp[theta][j]; 6257: /**< as well as its transposed matrix 6258: */ 6259: 6260: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */ 6261: for(i=1;i<=nlstate;i++) 6262: for(j=1;j<=nlstate;j++) 6263: vareij[i][j][(int)age] =0.; 6264: 6265: /* Computing trgradg by matcov by gradg at age and summing over h 6266: * and k (nhstepm) formula 15 of article 6267: * Lievre-Brouard-Heathcote 6268: */ 6269: 6270: for(h=0;h<=nhstepm;h++){ 6271: for(k=0;k<=nhstepm;k++){ 6272: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov); 6273: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]); 6274: for(i=1;i<=nlstate;i++) 6275: for(j=1;j<=nlstate;j++) 6276: vareij[i][j][(int)age] += doldm[i][j]*hf*hf; 6277: } 6278: } 6279: 6280: /* pptj is p.3 or p.j = trgradgp by cov by gradgp, variance of 6281: * p.j overall mortality formula 49 but computed directly because 6282: * we compute the grad (wix pijx) instead of grad (pijx),even if 6283: * wix is independent of theta. 6284: */ 6285: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov); 6286: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp); 6287: for(j=nlstate+1;j<=nlstate+ndeath;j++) 6288: for(i=nlstate+1;i<=nlstate+ndeath;i++) 6289: varppt[j][i]=doldmp[j][i]; 6290: /* end ppptj */ 6291: /* x centered again */ 6292: 6293: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij, nres); 6294: 6295: if (popbased==1) { 6296: if(mobilav ==0){ 6297: for(i=1; i<=nlstate;i++) 6298: prlim[i][i]=probs[(int)age][i][ij]; 6299: }else{ /* mobilav */ 6300: for(i=1; i<=nlstate;i++) 6301: prlim[i][i]=mobaverage[(int)age][i][ij]; 6302: } 6303: } 6304: 6305: /* This for computing probability of death (h=1 means 6306: computed over hstepm (estepm) matrices product = hstepm*stepm months) 6307: as a weighted average of prlim. 6308: */ 6309: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij, nres); 6310: for(j=nlstate+1;j<=nlstate+ndeath;j++){ 6311: for(i=1,gmp[j]=0.;i<= nlstate; i++) 6312: gmp[j] += prlim[i][i]*p3mat[i][j][1]; 6313: } 6314: /* end probability of death */ 6315: 6316: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij); 6317: for(j=nlstate+1; j<=(nlstate+ndeath);j++){ 6318: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j])); 6319: for(i=1; i<=nlstate;i++){ 6320: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]); 6321: } 6322: } 6323: fprintf(ficresprobmorprev,"\n"); 6324: 6325: fprintf(ficresvij,"%.0f ",age ); 6326: for(i=1; i<=nlstate;i++) 6327: for(j=1; j<=nlstate;j++){ 6328: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]); 6329: } 6330: fprintf(ficresvij,"\n"); 6331: free_matrix(gp,0,nhstepm,1,nlstate); 6332: free_matrix(gm,0,nhstepm,1,nlstate); 6333: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate); 6334: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar); 6335: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 6336: } /* End age */ 6337: free_vector(gpp,nlstate+1,nlstate+ndeath); 6338: free_vector(gmp,nlstate+1,nlstate+ndeath); 6339: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath); 6340: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/ 6341: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */ 6342: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480"); 6343: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */ 6344: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";"); 6345: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit); 6346: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */ 6347: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */ 6348: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */ 6349: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev)); 6350: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev)); 6351: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev)); 6352: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev)); 6353: 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); 6354: /* 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); 6355: */ 6356: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */ 6357: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit); 6358: 6359: free_vector(xp,1,npar); 6360: free_matrix(doldm,1,nlstate,1,nlstate); 6361: free_matrix(dnewm,1,nlstate,1,npar); 6362: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); 6363: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar); 6364: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath); 6365: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */ 6366: fclose(ficresprobmorprev); 6367: fflush(ficgp); 6368: fflush(fichtm); 6369: } /* end varevsij */ 6370: 6371: /************ Variance of prevlim ******************/ 6372: 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) 6373: { 6374: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/ 6375: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/ 6376: 6377: double **dnewmpar,**doldm; 6378: int i, j, nhstepm, hstepm; 6379: double *xp; 6380: double *gp, *gm; 6381: double **gradg, **trgradg; 6382: double **mgm, **mgp; 6383: double age,agelim; 6384: int theta; 6385: 6386: pstamp(ficresvpl); 6387: fprintf(ficresvpl,"# Standard deviation of period (forward stable) prevalences \n"); 6388: fprintf(ficresvpl,"# Age "); 6389: if(nresult >=1) 6390: fprintf(ficresvpl," Result# "); 6391: for(i=1; i<=nlstate;i++) 6392: fprintf(ficresvpl," %1d-%1d",i,i); 6393: fprintf(ficresvpl,"\n"); 6394: 6395: xp=vector(1,npar); 6396: dnewmpar=matrix(1,nlstate,1,npar); 6397: doldm=matrix(1,nlstate,1,nlstate); 6398: 6399: hstepm=1*YEARM; /* Every year of age */ 6400: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 6401: agelim = AGESUP; 6402: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */ 6403: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 6404: if (stepm >= YEARM) hstepm=1; 6405: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */ 6406: gradg=matrix(1,npar,1,nlstate); 6407: mgp=matrix(1,npar,1,nlstate); 6408: mgm=matrix(1,npar,1,nlstate); 6409: gp=vector(1,nlstate); 6410: gm=vector(1,nlstate); 6411: 6412: for(theta=1; theta <=npar; theta++){ 6413: for(i=1; i<=npar; i++){ /* Computes gradient */ 6414: xp[i] = x[i] + (i==theta ?delti[theta]:0); 6415: } 6416: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */ 6417: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */ 6418: /* else */ 6419: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); 6420: for(i=1;i<=nlstate;i++){ 6421: gp[i] = prlim[i][i]; 6422: mgp[theta][i] = prlim[i][i]; 6423: } 6424: for(i=1; i<=npar; i++) /* Computes gradient */ 6425: xp[i] = x[i] - (i==theta ?delti[theta]:0); 6426: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ) */ 6427: /* prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); */ 6428: /* else */ 6429: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij,nres); 6430: for(i=1;i<=nlstate;i++){ 6431: gm[i] = prlim[i][i]; 6432: mgm[theta][i] = prlim[i][i]; 6433: } 6434: for(i=1;i<=nlstate;i++) 6435: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta]; 6436: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */ 6437: } /* End theta */ 6438: 6439: trgradg =matrix(1,nlstate,1,npar); 6440: 6441: for(j=1; j<=nlstate;j++) 6442: for(theta=1; theta <=npar; theta++) 6443: trgradg[j][theta]=gradg[theta][j]; 6444: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */ 6445: /* printf("\nmgm mgp %d ",(int)age); */ 6446: /* for(j=1; j<=nlstate;j++){ */ 6447: /* printf(" %d ",j); */ 6448: /* for(theta=1; theta <=npar; theta++) */ 6449: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */ 6450: /* printf("\n "); */ 6451: /* } */ 6452: /* } */ 6453: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */ 6454: /* printf("\n gradg %d ",(int)age); */ 6455: /* for(j=1; j<=nlstate;j++){ */ 6456: /* printf("%d ",j); */ 6457: /* for(theta=1; theta <=npar; theta++) */ 6458: /* printf("%d %lf ",theta,gradg[theta][j]); */ 6459: /* printf("\n "); */ 6460: /* } */ 6461: /* } */ 6462: 6463: for(i=1;i<=nlstate;i++) 6464: varpl[i][(int)age] =0.; 6465: if((int)age==79 ||(int)age== 80 ||(int)age== 81){ 6466: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); 6467: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); 6468: }else{ 6469: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); 6470: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); 6471: } 6472: for(i=1;i<=nlstate;i++) 6473: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */ 6474: 6475: fprintf(ficresvpl,"%.0f ",age ); 6476: if(nresult >=1) 6477: fprintf(ficresvpl,"%d ",nres ); 6478: for(i=1; i<=nlstate;i++){ 6479: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age])); 6480: /* for(j=1;j<=nlstate;j++) */ 6481: /* fprintf(ficresvpl," %d %.5f ",j,prlim[j][i]); */ 6482: } 6483: fprintf(ficresvpl,"\n"); 6484: free_vector(gp,1,nlstate); 6485: free_vector(gm,1,nlstate); 6486: free_matrix(mgm,1,npar,1,nlstate); 6487: free_matrix(mgp,1,npar,1,nlstate); 6488: free_matrix(gradg,1,npar,1,nlstate); 6489: free_matrix(trgradg,1,nlstate,1,npar); 6490: } /* End age */ 6491: 6492: free_vector(xp,1,npar); 6493: free_matrix(doldm,1,nlstate,1,npar); 6494: free_matrix(dnewmpar,1,nlstate,1,nlstate); 6495: 6496: } 6497: 6498: 6499: /************ Variance of backprevalence limit ******************/ 6500: 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) 6501: { 6502: /* Variance of backward prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/ 6503: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/ 6504: 6505: double **dnewmpar,**doldm; 6506: int i, j, nhstepm, hstepm; 6507: double *xp; 6508: double *gp, *gm; 6509: double **gradg, **trgradg; 6510: double **mgm, **mgp; 6511: double age,agelim; 6512: int theta; 6513: 6514: pstamp(ficresvbl); 6515: fprintf(ficresvbl,"# Standard deviation of back (stable) prevalences \n"); 6516: fprintf(ficresvbl,"# Age "); 6517: if(nresult >=1) 6518: fprintf(ficresvbl," Result# "); 6519: for(i=1; i<=nlstate;i++) 6520: fprintf(ficresvbl," %1d-%1d",i,i); 6521: fprintf(ficresvbl,"\n"); 6522: 6523: xp=vector(1,npar); 6524: dnewmpar=matrix(1,nlstate,1,npar); 6525: doldm=matrix(1,nlstate,1,nlstate); 6526: 6527: hstepm=1*YEARM; /* Every year of age */ 6528: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */ 6529: agelim = AGEINF; 6530: for (age=fage; age>=bage; age --){ /* If stepm=6 months */ 6531: nhstepm=(int) rint((age-agelim)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 6532: if (stepm >= YEARM) hstepm=1; 6533: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */ 6534: gradg=matrix(1,npar,1,nlstate); 6535: mgp=matrix(1,npar,1,nlstate); 6536: mgm=matrix(1,npar,1,nlstate); 6537: gp=vector(1,nlstate); 6538: gm=vector(1,nlstate); 6539: 6540: for(theta=1; theta <=npar; theta++){ 6541: for(i=1; i<=npar; i++){ /* Computes gradient */ 6542: xp[i] = x[i] + (i==theta ?delti[theta]:0); 6543: } 6544: if(mobilavproj > 0 ) 6545: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); 6546: else 6547: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); 6548: for(i=1;i<=nlstate;i++){ 6549: gp[i] = bprlim[i][i]; 6550: mgp[theta][i] = bprlim[i][i]; 6551: } 6552: for(i=1; i<=npar; i++) /* Computes gradient */ 6553: xp[i] = x[i] - (i==theta ?delti[theta]:0); 6554: if(mobilavproj > 0 ) 6555: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); 6556: else 6557: bprevalim(bprlim, mobaverage,nlstate,xp,age,ftolpl,ncvyearp,ij,nres); 6558: for(i=1;i<=nlstate;i++){ 6559: gm[i] = bprlim[i][i]; 6560: mgm[theta][i] = bprlim[i][i]; 6561: } 6562: for(i=1;i<=nlstate;i++) 6563: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta]; 6564: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */ 6565: } /* End theta */ 6566: 6567: trgradg =matrix(1,nlstate,1,npar); 6568: 6569: for(j=1; j<=nlstate;j++) 6570: for(theta=1; theta <=npar; theta++) 6571: trgradg[j][theta]=gradg[theta][j]; 6572: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */ 6573: /* printf("\nmgm mgp %d ",(int)age); */ 6574: /* for(j=1; j<=nlstate;j++){ */ 6575: /* printf(" %d ",j); */ 6576: /* for(theta=1; theta <=npar; theta++) */ 6577: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */ 6578: /* printf("\n "); */ 6579: /* } */ 6580: /* } */ 6581: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */ 6582: /* printf("\n gradg %d ",(int)age); */ 6583: /* for(j=1; j<=nlstate;j++){ */ 6584: /* printf("%d ",j); */ 6585: /* for(theta=1; theta <=npar; theta++) */ 6586: /* printf("%d %lf ",theta,gradg[theta][j]); */ 6587: /* printf("\n "); */ 6588: /* } */ 6589: /* } */ 6590: 6591: for(i=1;i<=nlstate;i++) 6592: varbpl[i][(int)age] =0.; 6593: if((int)age==79 ||(int)age== 80 ||(int)age== 81){ 6594: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); 6595: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); 6596: }else{ 6597: matprod2(dnewmpar,trgradg,1,nlstate,1,npar,1,npar,matcov); 6598: matprod2(doldm,dnewmpar,1,nlstate,1,npar,1,nlstate,gradg); 6599: } 6600: for(i=1;i<=nlstate;i++) 6601: varbpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */ 6602: 6603: fprintf(ficresvbl,"%.0f ",age ); 6604: if(nresult >=1) 6605: fprintf(ficresvbl,"%d ",nres ); 6606: for(i=1; i<=nlstate;i++) 6607: fprintf(ficresvbl," %.5f (%.5f)",bprlim[i][i],sqrt(varbpl[i][(int)age])); 6608: fprintf(ficresvbl,"\n"); 6609: free_vector(gp,1,nlstate); 6610: free_vector(gm,1,nlstate); 6611: free_matrix(mgm,1,npar,1,nlstate); 6612: free_matrix(mgp,1,npar,1,nlstate); 6613: free_matrix(gradg,1,npar,1,nlstate); 6614: free_matrix(trgradg,1,nlstate,1,npar); 6615: } /* End age */ 6616: 6617: free_vector(xp,1,npar); 6618: free_matrix(doldm,1,nlstate,1,npar); 6619: free_matrix(dnewmpar,1,nlstate,1,nlstate); 6620: 6621: } 6622: 6623: /************ Variance of one-step probabilities ******************/ 6624: 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[]) 6625: { 6626: int i, j=0, k1, l1, tj; 6627: int k2, l2, j1, z1; 6628: int k=0, l; 6629: int first=1, first1, first2; 6630: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp; 6631: double **dnewm,**doldm; 6632: double *xp; 6633: double *gp, *gm; 6634: double **gradg, **trgradg; 6635: double **mu; 6636: double age, cov[NCOVMAX+1]; 6637: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */ 6638: int theta; 6639: char fileresprob[FILENAMELENGTH]; 6640: char fileresprobcov[FILENAMELENGTH]; 6641: char fileresprobcor[FILENAMELENGTH]; 6642: double ***varpij; 6643: 6644: strcpy(fileresprob,"PROB_"); 6645: strcat(fileresprob,fileres); 6646: if((ficresprob=fopen(fileresprob,"w"))==NULL) { 6647: printf("Problem with resultfile: %s\n", fileresprob); 6648: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob); 6649: } 6650: strcpy(fileresprobcov,"PROBCOV_"); 6651: strcat(fileresprobcov,fileresu); 6652: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) { 6653: printf("Problem with resultfile: %s\n", fileresprobcov); 6654: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov); 6655: } 6656: strcpy(fileresprobcor,"PROBCOR_"); 6657: strcat(fileresprobcor,fileresu); 6658: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) { 6659: printf("Problem with resultfile: %s\n", fileresprobcor); 6660: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor); 6661: } 6662: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob); 6663: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob); 6664: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov); 6665: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov); 6666: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor); 6667: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor); 6668: pstamp(ficresprob); 6669: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n"); 6670: fprintf(ficresprob,"# Age"); 6671: pstamp(ficresprobcov); 6672: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n"); 6673: fprintf(ficresprobcov,"# Age"); 6674: pstamp(ficresprobcor); 6675: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n"); 6676: fprintf(ficresprobcor,"# Age"); 6677: 6678: 6679: for(i=1; i<=nlstate;i++) 6680: for(j=1; j<=(nlstate+ndeath);j++){ 6681: fprintf(ficresprob," p%1d-%1d (SE)",i,j); 6682: fprintf(ficresprobcov," p%1d-%1d ",i,j); 6683: fprintf(ficresprobcor," p%1d-%1d ",i,j); 6684: } 6685: /* fprintf(ficresprob,"\n"); 6686: fprintf(ficresprobcov,"\n"); 6687: fprintf(ficresprobcor,"\n"); 6688: */ 6689: xp=vector(1,npar); 6690: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar); 6691: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath)); 6692: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage); 6693: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage); 6694: first=1; 6695: fprintf(ficgp,"\n# Routine varprob"); 6696: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n"); 6697: fprintf(fichtm,"\n"); 6698: 6699: 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); 6700: 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); 6701: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \ 6702: and drawn. It helps understanding how is the covariance between two incidences.\ 6703: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n"); 6704: 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. \ 6705: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \ 6706: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \ 6707: standard deviations wide on each axis. <br>\ 6708: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\ 6709: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\ 6710: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n"); 6711: 6712: cov[1]=1; 6713: /* tj=cptcoveff; */ 6714: tj = (int) pow(2,cptcoveff); 6715: if (cptcovn<1) {tj=1;ncodemax[1]=1;} 6716: j1=0; 6717: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/ 6718: if (cptcovn>0) { 6719: fprintf(ficresprob, "\n#********** Variable "); 6720: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 6721: fprintf(ficresprob, "**********\n#\n"); 6722: fprintf(ficresprobcov, "\n#********** Variable "); 6723: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 6724: fprintf(ficresprobcov, "**********\n#\n"); 6725: 6726: fprintf(ficgp, "\n#********** Variable "); 6727: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 6728: fprintf(ficgp, "**********\n#\n"); 6729: 6730: 6731: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable "); 6732: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 6733: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">"); 6734: 6735: fprintf(ficresprobcor, "\n#********** Variable "); 6736: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]); 6737: fprintf(ficresprobcor, "**********\n#"); 6738: if(invalidvarcomb[j1]){ 6739: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1); 6740: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1); 6741: continue; 6742: } 6743: } 6744: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath)); 6745: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar); 6746: gp=vector(1,(nlstate)*(nlstate+ndeath)); 6747: gm=vector(1,(nlstate)*(nlstate+ndeath)); 6748: for (age=bage; age<=fage; age ++){ 6749: cov[2]=age; 6750: if(nagesqr==1) 6751: cov[3]= age*age; 6752: for (k=1; k<=cptcovn;k++) { 6753: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)]; 6754: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4 6755: * 1 1 1 1 1 6756: * 2 2 1 1 1 6757: * 3 1 2 1 1 6758: */ 6759: /* nbcode[1][1]=0 nbcode[1][2]=1;*/ 6760: } 6761: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */ 6762: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2]; 6763: for (k=1; k<=cptcovprod;k++) 6764: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)]; 6765: 6766: 6767: for(theta=1; theta <=npar; theta++){ 6768: for(i=1; i<=npar; i++) 6769: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0); 6770: 6771: pmij(pmmij,cov,ncovmodel,xp,nlstate); 6772: 6773: k=0; 6774: for(i=1; i<= (nlstate); i++){ 6775: for(j=1; j<=(nlstate+ndeath);j++){ 6776: k=k+1; 6777: gp[k]=pmmij[i][j]; 6778: } 6779: } 6780: 6781: for(i=1; i<=npar; i++) 6782: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0); 6783: 6784: pmij(pmmij,cov,ncovmodel,xp,nlstate); 6785: k=0; 6786: for(i=1; i<=(nlstate); i++){ 6787: for(j=1; j<=(nlstate+ndeath);j++){ 6788: k=k+1; 6789: gm[k]=pmmij[i][j]; 6790: } 6791: } 6792: 6793: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++) 6794: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta]; 6795: } 6796: 6797: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++) 6798: for(theta=1; theta <=npar; theta++) 6799: trgradg[j][theta]=gradg[theta][j]; 6800: 6801: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov); 6802: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg); 6803: 6804: pmij(pmmij,cov,ncovmodel,x,nlstate); 6805: 6806: k=0; 6807: for(i=1; i<=(nlstate); i++){ 6808: for(j=1; j<=(nlstate+ndeath);j++){ 6809: k=k+1; 6810: mu[k][(int) age]=pmmij[i][j]; 6811: } 6812: } 6813: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++) 6814: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++) 6815: varpij[i][j][(int)age] = doldm[i][j]; 6816: 6817: /*printf("\n%d ",(int)age); 6818: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){ 6819: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i])); 6820: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i])); 6821: }*/ 6822: 6823: fprintf(ficresprob,"\n%d ",(int)age); 6824: fprintf(ficresprobcov,"\n%d ",(int)age); 6825: fprintf(ficresprobcor,"\n%d ",(int)age); 6826: 6827: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++) 6828: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age])); 6829: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){ 6830: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]); 6831: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]); 6832: } 6833: i=0; 6834: for (k=1; k<=(nlstate);k++){ 6835: for (l=1; l<=(nlstate+ndeath);l++){ 6836: i++; 6837: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l); 6838: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l); 6839: for (j=1; j<=i;j++){ 6840: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */ 6841: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]); 6842: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age])); 6843: } 6844: } 6845: }/* end of loop for state */ 6846: } /* end of loop for age */ 6847: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath)); 6848: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath)); 6849: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar); 6850: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar); 6851: 6852: /* Confidence intervalle of pij */ 6853: /* 6854: fprintf(ficgp,"\nunset parametric;unset label"); 6855: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\""); 6856: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65"); 6857: 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); 6858: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname); 6859: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname); 6860: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob); 6861: */ 6862: 6863: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/ 6864: first1=1;first2=2; 6865: for (k2=1; k2<=(nlstate);k2++){ 6866: for (l2=1; l2<=(nlstate+ndeath);l2++){ 6867: if(l2==k2) continue; 6868: j=(k2-1)*(nlstate+ndeath)+l2; 6869: for (k1=1; k1<=(nlstate);k1++){ 6870: for (l1=1; l1<=(nlstate+ndeath);l1++){ 6871: if(l1==k1) continue; 6872: i=(k1-1)*(nlstate+ndeath)+l1; 6873: if(i<=j) continue; 6874: for (age=bage; age<=fage; age ++){ 6875: if ((int)age %5==0){ 6876: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM; 6877: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM; 6878: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM; 6879: mu1=mu[i][(int) age]/stepm*YEARM ; 6880: mu2=mu[j][(int) age]/stepm*YEARM; 6881: c12=cv12/sqrt(v1*v2); 6882: /* Computing eigen value of matrix of covariance */ 6883: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.; 6884: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.; 6885: if ((lc2 <0) || (lc1 <0) ){ 6886: if(first2==1){ 6887: first1=0; 6888: 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); 6889: } 6890: 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); 6891: /* lc1=fabs(lc1); */ /* If we want to have them positive */ 6892: /* lc2=fabs(lc2); */ 6893: } 6894: 6895: /* Eigen vectors */ 6896: if(1+(v1-lc1)*(v1-lc1)/cv12/cv12 <1.e-5){ 6897: printf(" Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12); 6898: fprintf(ficlog," Error sqrt of a negative number: %lf\n",1+(v1-lc1)*(v1-lc1)/cv12/cv12); 6899: v11=(1./sqrt(fabs(1+(v1-lc1)*(v1-lc1)/cv12/cv12))); 6900: }else 6901: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12)); 6902: /*v21=sqrt(1.-v11*v11); *//* error */ 6903: v21=(lc1-v1)/cv12*v11; 6904: v12=-v21; 6905: v22=v11; 6906: tnalp=v21/v11; 6907: if(first1==1){ 6908: first1=0; 6909: 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); 6910: } 6911: 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); 6912: /*printf(fignu*/ 6913: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */ 6914: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */ 6915: if(first==1){ 6916: first=0; 6917: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n"); 6918: fprintf(ficgp,"\nset parametric;unset label"); 6919: 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); 6920: fprintf(ficgp,"\nset ter svg size 640, 480"); 6921: fprintf(fichtmcov,"\n<p><br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\ 6922: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \ 6923: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\ 6924: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \ 6925: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2); 6926: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2); 6927: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12); 6928: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2); 6929: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2); 6930: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2); 6931: 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", \ 6932: mu1,std,v11,sqrt(fabs(lc1)),v12,sqrt(fabs(lc2)), \ 6933: mu2,std,v21,sqrt(fabs(lc1)),v22,sqrt(fabs(lc2))); /* For gnuplot only */ 6934: }else{ 6935: first=0; 6936: fprintf(fichtmcov," %d (%.3f),",(int) age, c12); 6937: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2); 6938: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2); 6939: 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", \ 6940: mu1,std,v11,sqrt(lc1),v12,sqrt(fabs(lc2)), \ 6941: mu2,std,v21,sqrt(lc1),v22,sqrt(fabs(lc2))); 6942: }/* if first */ 6943: } /* age mod 5 */ 6944: } /* end loop age */ 6945: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2); 6946: first=1; 6947: } /*l12 */ 6948: } /* k12 */ 6949: } /*l1 */ 6950: }/* k1 */ 6951: } /* loop on combination of covariates j1 */ 6952: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage); 6953: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage); 6954: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath)); 6955: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar); 6956: free_vector(xp,1,npar); 6957: fclose(ficresprob); 6958: fclose(ficresprobcov); 6959: fclose(ficresprobcor); 6960: fflush(ficgp); 6961: fflush(fichtmcov); 6962: } 6963: 6964: 6965: /******************* Printing html file ***********/ 6966: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \ 6967: int lastpass, int stepm, int weightopt, char model[],\ 6968: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\ 6969: int popforecast, int mobilav, int prevfcast, int mobilavproj, int prevbcast, int estepm , \ 6970: double jprev1, double mprev1,double anprev1, double dateprev1, double dateprojd, double dateback1, \ 6971: double jprev2, double mprev2,double anprev2, double dateprev2, double dateprojf, double dateback2){ 6972: int jj1, k1, i1, cpt, k4, nres; 6973: 6974: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \ 6975: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \ 6976: </ul>"); 6977: fprintf(fichtm,"<ul><li> model=1+age+%s\n \ 6978: </ul>", model); 6979: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n"); 6980: 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", 6981: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm")); 6982: 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) ", 6983: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm")); 6984: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_")); 6985: fprintf(fichtm,"\ 6986: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ", 6987: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_")); 6988: fprintf(fichtm,"\ 6989: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ", 6990: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_")); 6991: fprintf(fichtm,"\ 6992: - Period (forward) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n", 6993: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_")); 6994: fprintf(fichtm,"\ 6995: - Backward prevalence in each health state: <a href=\"%s\">%s</a> <br>\n", 6996: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_")); 6997: fprintf(fichtm,"\ 6998: - (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): \ 6999: <a href=\"%s\">%s</a> <br>\n", 7000: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_")); 7001: if(prevfcast==1){ 7002: fprintf(fichtm,"\ 7003: - Prevalence projections by age and states: \ 7004: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_")); 7005: } 7006: 7007: 7008: m=pow(2,cptcoveff); 7009: if (cptcovn < 1) {m=1;ncodemax[1]=1;} 7010: 7011: fprintf(fichtm," \n<ul><li><b>Graphs (first order)</b></li><p>"); 7012: 7013: jj1=0; 7014: 7015: fprintf(fichtm," \n<ul>"); 7016: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 7017: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */ 7018: if(m != 1 && TKresult[nres]!= k1) 7019: continue; 7020: jj1++; 7021: if (cptcovn > 0) { 7022: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescov"); 7023: for (cpt=1; cpt<=cptcoveff;cpt++){ 7024: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); 7025: } 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: } 7029: fprintf(fichtm,"\">"); 7030: 7031: /* if(nqfveff+nqtveff 0) */ /* Test to be done */ 7032: fprintf(fichtm,"************ Results for covariates"); 7033: for (cpt=1; cpt<=cptcoveff;cpt++){ 7034: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); 7035: } 7036: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7037: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7038: } 7039: if(invalidvarcomb[k1]){ 7040: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1); 7041: continue; 7042: } 7043: fprintf(fichtm,"</a></li>"); 7044: } /* cptcovn >0 */ 7045: } 7046: fprintf(fichtm," \n</ul>"); 7047: 7048: jj1=0; 7049: 7050: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 7051: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */ 7052: if(m != 1 && TKresult[nres]!= k1) 7053: continue; 7054: 7055: /* for(i1=1; i1<=ncodemax[k1];i1++){ */ 7056: jj1++; 7057: if (cptcovn > 0) { 7058: fprintf(fichtm,"\n<p><a name=\"rescov"); 7059: for (cpt=1; cpt<=cptcoveff;cpt++){ 7060: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); 7061: } 7062: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7063: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); 7064: } 7065: fprintf(fichtm,"\"</a>"); 7066: 7067: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates"); 7068: for (cpt=1; cpt<=cptcoveff;cpt++){ 7069: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); 7070: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout); 7071: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */ 7072: /* printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout); */ 7073: } 7074: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7075: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7076: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]);fflush(stdout); 7077: } 7078: 7079: /* if(nqfveff+nqtveff 0) */ /* Test to be done */ 7080: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">"); 7081: if(invalidvarcomb[k1]){ 7082: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1); 7083: printf("\nCombination (%d) ignored because no cases \n",k1); 7084: continue; 7085: } 7086: } 7087: /* aij, bij */ 7088: 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> \ 7089: <img src=\"%s_%d-1-%d.svg\">",model,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres); 7090: /* Pij */ 7091: 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> \ 7092: <img src=\"%s_%d-2-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres); 7093: /* Quasi-incidences */ 7094: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\ 7095: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \ 7096: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \ 7097: 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> \ 7098: <img src=\"%s_%d-3-%d.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres,subdirf2(optionfilefiname,"PE_"),k1,nres); 7099: /* Survival functions (period) in state j */ 7100: for(cpt=1; cpt<=nlstate;cpt++){ 7101: 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> \ 7102: <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); 7103: } 7104: /* State specific survival functions (period) */ 7105: for(cpt=1; cpt<=nlstate;cpt++){ 7106: fprintf(fichtm,"<br>\n- Survival functions in state %d and in any other live state (total).\ 7107: And probability to be observed in various states (up to %d) being in state %d at different ages. \ 7108: <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); 7109: } 7110: /* Period (forward stable) prevalence in each health state */ 7111: for(cpt=1; cpt<=nlstate;cpt++){ 7112: 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> \ 7113: <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); 7114: } 7115: if(prevbcast==1){ 7116: /* Backward prevalence in each health state */ 7117: for(cpt=1; cpt<=nlstate;cpt++){ 7118: 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> \ 7119: <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); 7120: } 7121: } 7122: if(prevfcast==1){ 7123: /* Projection of prevalence up to period (forward stable) prevalence in each health state */ 7124: for(cpt=1; cpt<=nlstate;cpt++){ 7125: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), from year %.1f up to year %.1f tending to period (stable) forward prevalence in state %d. Or probability to be in state %d being in an observed weighted state (from 1 to %d). <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateprojd, dateprojf, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres); 7126: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"F_"),subdirf2(optionfilefiname,"F_")); 7127: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", 7128: subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres); 7129: } 7130: } 7131: if(prevbcast==1){ 7132: /* Back projection of prevalence up to stable (mixed) back-prevalence in each health state */ 7133: for(cpt=1; cpt<=nlstate;cpt++){ 7134: fprintf(fichtm,"<br>\n- Back projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f and mobil_average=%d), \ 7135: 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 \ 7136: 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) \ 7137: with weights corresponding to observed prevalence at different ages. <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>", dateprev1, dateprev2, mobilavproj, dateback1, dateback2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres,subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres); 7138: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>)\n<br>",subdirf2(optionfilefiname,"FB_"),subdirf2(optionfilefiname,"FB_")); 7139: fprintf(fichtm," <img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres); 7140: } 7141: } 7142: 7143: for(cpt=1; cpt<=nlstate;cpt++) { 7144: fprintf(fichtm,"\n<br>- Life expectancy by health state (%d) at initial age and its decomposition into health expectancies in each alive state (1 to %d) (or area under each survival functions): <a href=\"%s_%d-%d-%d.svg\">%s_%d-%d-%d.svg</a>",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres,subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres); 7145: fprintf(fichtm," (data from text file <a href=\"%s.txt\"> %s.txt</a>)\n<br>",subdirf2(optionfilefiname,"E_"),subdirf2(optionfilefiname,"E_")); 7146: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">", subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres ); 7147: } 7148: /* } /\* end i1 *\/ */ 7149: }/* End k1 */ 7150: fprintf(fichtm,"</ul>"); 7151: 7152: fprintf(fichtm,"\ 7153: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\ 7154: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \ 7155: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \ 7156: But because parameters are usually highly correlated (a higher incidence of disability \ 7157: and a higher incidence of recovery can give very close observed transition) it might \ 7158: be very useful to look not only at linear confidence intervals estimated from the \ 7159: variances but at the covariance matrix. And instead of looking at the estimated coefficients \ 7160: (parameters) of the logistic regression, it might be more meaningful to visualize the \ 7161: covariance matrix of the one-step probabilities. \ 7162: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres); 7163: 7164: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n", 7165: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_")); 7166: fprintf(fichtm,"\ 7167: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n", 7168: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_")); 7169: 7170: fprintf(fichtm,"\ 7171: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n", 7172: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_")); 7173: fprintf(fichtm,"\ 7174: - 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): \ 7175: <a href=\"%s\">%s</a> <br>\n</li>", 7176: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_")); 7177: fprintf(fichtm,"\ 7178: - (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): \ 7179: <a href=\"%s\">%s</a> <br>\n</li>", 7180: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_")); 7181: fprintf(fichtm,"\ 7182: - 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", 7183: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_")); 7184: fprintf(fichtm,"\ 7185: - 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", 7186: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_")); 7187: fprintf(fichtm,"\ 7188: - Standard deviation of forward (period) prevalences: <a href=\"%s\">%s</a> <br>\n",\ 7189: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_")); 7190: 7191: /* if(popforecast==1) fprintf(fichtm,"\n */ 7192: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */ 7193: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */ 7194: /* <br>",fileres,fileres,fileres,fileres); */ 7195: /* else */ 7196: /* 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); */ 7197: fflush(fichtm); 7198: 7199: m=pow(2,cptcoveff); 7200: if (cptcovn < 1) {m=1;ncodemax[1]=1;} 7201: 7202: fprintf(fichtm," <ul><li><b>Graphs (second order)</b></li><p>"); 7203: 7204: jj1=0; 7205: 7206: fprintf(fichtm," \n<ul>"); 7207: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 7208: for(k1=1; k1<=m;k1++){ /* For each combination of covariate */ 7209: if(m != 1 && TKresult[nres]!= k1) 7210: continue; 7211: jj1++; 7212: if (cptcovn > 0) { 7213: fprintf(fichtm,"\n<li><a size=\"1\" color=\"#EC5E5E\" href=\"#rescovsecond"); 7214: for (cpt=1; cpt<=cptcoveff;cpt++){ 7215: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); 7216: } 7217: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7218: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); 7219: } 7220: fprintf(fichtm,"\">"); 7221: 7222: /* if(nqfveff+nqtveff 0) */ /* Test to be done */ 7223: fprintf(fichtm,"************ Results for covariates"); 7224: for (cpt=1; cpt<=cptcoveff;cpt++){ 7225: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); 7226: } 7227: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7228: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7229: } 7230: if(invalidvarcomb[k1]){ 7231: fprintf(fichtm," Warning Combination (%d) ignored because no cases ",k1); 7232: continue; 7233: } 7234: fprintf(fichtm,"</a></li>"); 7235: } /* cptcovn >0 */ 7236: } 7237: fprintf(fichtm," \n</ul>"); 7238: 7239: jj1=0; 7240: 7241: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7242: for(k1=1; k1<=m;k1++){ 7243: if(m != 1 && TKresult[nres]!= k1) 7244: continue; 7245: /* for(i1=1; i1<=ncodemax[k1];i1++){ */ 7246: jj1++; 7247: if (cptcovn > 0) { 7248: fprintf(fichtm,"\n<p><a name=\"rescovsecond"); 7249: for (cpt=1; cpt<=cptcoveff;cpt++){ 7250: fprintf(fichtm,"_V%d=%d_",Tvresult[nres][cpt],(int)Tresult[nres][cpt]); 7251: } 7252: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7253: fprintf(fichtm,"_V%d=%f_",Tvqresult[nres][k4],Tqresult[nres][k4]); 7254: } 7255: fprintf(fichtm,"\"</a>"); 7256: 7257: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates"); 7258: for (cpt=1; cpt<=cptcoveff;cpt++){ /**< cptcoveff number of variables */ 7259: fprintf(fichtm," V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]); 7260: printf(" V%d=%d ",Tvresult[nres][cpt],Tresult[nres][cpt]);fflush(stdout); 7261: /* fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]); */ 7262: } 7263: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7264: fprintf(fichtm," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7265: } 7266: 7267: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">"); 7268: 7269: if(invalidvarcomb[k1]){ 7270: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1); 7271: continue; 7272: } 7273: } 7274: for(cpt=1; cpt<=nlstate;cpt++) { 7275: fprintf(fichtm,"\n<br>- Observed (cross-sectional with mov_average=%d) and period (incidence based) \ 7276: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d-%d.svg\"> %s_%d-%d-%d.svg</a>",mobilav,cpt,subdirf2(optionfilefiname,"V_"),cpt,k1,nres,subdirf2(optionfilefiname,"V_"),cpt,k1,nres); 7277: fprintf(fichtm," (data from text file <a href=\"%s\">%s</a>)\n <br>",subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_")); 7278: fprintf(fichtm,"<img src=\"%s_%d-%d-%d.svg\">",subdirf2(optionfilefiname,"V_"), cpt,k1,nres); 7279: } 7280: fprintf(fichtm,"\n<br>- Total life expectancy by age and \ 7281: health expectancies in each live states (1 to %d). If popbased=1 the smooth (due to the model) \ 7282: true period expectancies (those weighted with period prevalences are also\ 7283: drawn in addition to the population based expectancies computed using\ 7284: observed and cahotic prevalences: <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a>",nlstate, subdirf2(optionfilefiname,"E_"),k1,nres,subdirf2(optionfilefiname,"E_"),k1,nres); 7285: fprintf(fichtm," (data from text file <a href=\"%s.txt\">%s.txt</a>) \n<br>",subdirf2(optionfilefiname,"T_"),subdirf2(optionfilefiname,"T_")); 7286: fprintf(fichtm,"<img src=\"%s_%d-%d.svg\">",subdirf2(optionfilefiname,"E_"),k1,nres); 7287: /* } /\* end i1 *\/ */ 7288: }/* End k1 */ 7289: }/* End nres */ 7290: fprintf(fichtm,"</ul>"); 7291: fflush(fichtm); 7292: } 7293: 7294: /******************* Gnuplot file **************/ 7295: 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){ 7296: 7297: char dirfileres[132],optfileres[132]; 7298: char gplotcondition[132], gplotlabel[132]; 7299: 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; 7300: int lv=0, vlv=0, kl=0; 7301: int ng=0; 7302: int vpopbased; 7303: int ioffset; /* variable offset for columns */ 7304: int iyearc=1; /* variable column for year of projection */ 7305: int iagec=1; /* variable column for age of projection */ 7306: int nres=0; /* Index of resultline */ 7307: int istart=1; /* For starting graphs in projections */ 7308: 7309: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */ 7310: /* printf("Problem with file %s",optionfilegnuplot); */ 7311: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */ 7312: /* } */ 7313: 7314: /*#ifdef windows */ 7315: fprintf(ficgp,"cd \"%s\" \n",pathc); 7316: /*#endif */ 7317: m=pow(2,cptcoveff); 7318: 7319: /* diagram of the model */ 7320: fprintf(ficgp,"\n#Diagram of the model \n"); 7321: fprintf(ficgp,"\ndelta=0.03;delta2=0.07;unset arrow;\n"); 7322: fprintf(ficgp,"yoff=(%d > 2? 0:1);\n",nlstate); 7323: 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); 7324: 7325: 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); 7326: fprintf(ficgp,"\n#show arrow\nunset label\n"); 7327: 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); 7328: fprintf(ficgp,"\nset label %d+1 sprintf(\"State %%d\",%d+1) center at 0.,0. font \"helvetica, 16\" tc rgbcolor \"red\"\n",nlstate,nlstate); 7329: fprintf(ficgp,"\n#show label\nunset border;unset xtics; unset ytics;\n"); 7330: fprintf(ficgp,"\n\nset ter svg size 640, 480;set out \"%s_.svg\" \n",subdirf2(optionfilefiname,"D_")); 7331: fprintf(ficgp,"unset log y; plot [-1.2:1.2][yoff-1.2:1.2] 1/0 not; set out;reset;\n"); 7332: 7333: /* Contribution to likelihood */ 7334: /* Plot the probability implied in the likelihood */ 7335: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n"); 7336: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";"); 7337: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */ 7338: fprintf(ficgp,"\nset ter pngcairo size 640, 480"); 7339: /* nice for mle=4 plot by number of matrix products. 7340: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */ 7341: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */ 7342: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */ 7343: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_")); 7344: 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)); 7345: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_")); 7346: 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)); 7347: for (i=1; i<= nlstate ; i ++) { 7348: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i); 7349: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk)); 7350: 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); 7351: for (j=2; j<= nlstate+ndeath ; j ++) { 7352: 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); 7353: } 7354: fprintf(ficgp,";\nset out; unset ylabel;\n"); 7355: } 7356: /* 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 */ 7357: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */ 7358: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */ 7359: fprintf(ficgp,"\nset out;unset log\n"); 7360: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */ 7361: 7362: strcpy(dirfileres,optionfilefiname); 7363: strcpy(optfileres,"vpl"); 7364: /* 1eme*/ 7365: for (cpt=1; cpt<= nlstate ; cpt ++){ /* For each live state */ 7366: for (k1=1; k1<= m ; k1 ++){ /* For each valid combination of covariate */ 7367: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7368: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */ 7369: if(m != 1 && TKresult[nres]!= k1) 7370: continue; 7371: /* We are interested in selected combination by the resultline */ 7372: /* printf("\n# 1st: Period (stable) prevalence with CI: 'VPL_' files and live state =%d ", cpt); */ 7373: fprintf(ficgp,"\n# 1st: Forward (stable period) prevalence with CI: 'VPL_' files and live state =%d ", cpt); 7374: strcpy(gplotlabel,"("); 7375: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */ 7376: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */ 7377: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7378: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7379: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7380: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */ 7381: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */ 7382: /* printf(" V%d=%d ",Tvaraff[k],vlv); */ 7383: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7384: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7385: } 7386: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7387: /* printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); */ 7388: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7389: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7390: } 7391: strcpy(gplotlabel+strlen(gplotlabel),")"); 7392: /* printf("\n#\n"); */ 7393: fprintf(ficgp,"\n#\n"); 7394: if(invalidvarcomb[k1]){ 7395: /*k1=k1-1;*/ /* To be checked */ 7396: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7397: continue; 7398: } 7399: 7400: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1,nres); 7401: fprintf(ficgp,"\n#set out \"V_%s_%d-%d-%d.svg\" \n",optionfilefiname,cpt,k1,nres); 7402: /* fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); */ 7403: fprintf(ficgp,"set title \"Alive state %d %s\" font \"Helvetica,12\"\n",cpt,gplotlabel); 7404: 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); 7405: /* 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); */ 7406: /* k1-1 error should be nres-1*/ 7407: for (i=1; i<= nlstate ; i ++) { 7408: if (i==cpt) fprintf(ficgp," %%lf (%%lf)"); 7409: else fprintf(ficgp," %%*lf (%%*lf)"); 7410: } 7411: 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); 7412: for (i=1; i<= nlstate ; i ++) { 7413: if (i==cpt) fprintf(ficgp," %%lf (%%lf)"); 7414: else fprintf(ficgp," %%*lf (%%*lf)"); 7415: } 7416: 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); 7417: for (i=1; i<= nlstate ; i ++) { 7418: if (i==cpt) fprintf(ficgp," %%lf (%%lf)"); 7419: else fprintf(ficgp," %%*lf (%%*lf)"); 7420: } 7421: /* 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)); */ 7422: 7423: fprintf(ficgp,"\" t\"\" w l lt 1,\"%s\" u 1:((",subdirf2(fileresu,"P_")); 7424: if(cptcoveff ==0){ 7425: fprintf(ficgp,"$%d)) t 'Observed prevalence in state %d' with line lt 3", 2+3*(cpt-1), cpt ); 7426: }else{ 7427: kl=0; 7428: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */ 7429: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */ 7430: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7431: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7432: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7433: vlv= nbcode[Tvaraff[k]][lv]; 7434: kl++; 7435: /* 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 *\/ */ 7436: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */ 7437: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */ 7438: /* '' 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*/ 7439: if(k==cptcoveff){ 7440: 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], \ 7441: 2+cptcoveff*2+3*(cpt-1), cpt ); /* 4 or 6 ?*/ 7442: }else{ 7443: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]); 7444: kl++; 7445: } 7446: } /* end covariate */ 7447: } /* end if no covariate */ 7448: 7449: if(prevbcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */ 7450: /* 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); */ 7451: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1, nres in 2 to be fixed */ 7452: if(cptcoveff ==0){ 7453: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line lt 3", 2+(cpt-1), cpt ); 7454: }else{ 7455: kl=0; 7456: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */ 7457: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */ 7458: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7459: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7460: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7461: vlv= nbcode[Tvaraff[k]][lv]; 7462: kl++; 7463: /* 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 *\/ */ 7464: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */ 7465: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */ 7466: /* '' 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*/ 7467: if(k==cptcoveff){ 7468: 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], \ 7469: 2+cptcoveff*2+(cpt-1), cpt ); /* 4 or 6 ?*/ 7470: }else{ 7471: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]); 7472: kl++; 7473: } 7474: } /* end covariate */ 7475: } /* end if no covariate */ 7476: if(prevbcast == 1){ 7477: fprintf(ficgp,", \"%s\" every :::%d::%d u 1:($2==%d ? $3:1/0) \"%%lf %%lf",subdirf2(fileresu,"VBL_"),nres-1,nres-1,nres); 7478: /* k1-1 error should be nres-1*/ 7479: for (i=1; i<= nlstate ; i ++) { 7480: if (i==cpt) fprintf(ficgp," %%lf (%%lf)"); 7481: else fprintf(ficgp," %%*lf (%%*lf)"); 7482: } 7483: 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); 7484: for (i=1; i<= nlstate ; i ++) { 7485: if (i==cpt) fprintf(ficgp," %%lf (%%lf)"); 7486: else fprintf(ficgp," %%*lf (%%*lf)"); 7487: } 7488: 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); 7489: for (i=1; i<= nlstate ; i ++) { 7490: if (i==cpt) fprintf(ficgp," %%lf (%%lf)"); 7491: else fprintf(ficgp," %%*lf (%%*lf)"); 7492: } 7493: fprintf(ficgp,"\" t\"\" w l lt 4"); 7494: } /* end if backprojcast */ 7495: } /* end if prevbcast */ 7496: /* fprintf(ficgp,"\nset out ;unset label;\n"); */ 7497: fprintf(ficgp,"\nset out ;unset title;\n"); 7498: } /* nres */ 7499: } /* k1 */ 7500: } /* cpt */ 7501: 7502: 7503: /*2 eme*/ 7504: for (k1=1; k1<= m ; k1 ++){ 7505: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7506: if(m != 1 && TKresult[nres]!= k1) 7507: continue; 7508: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files "); 7509: strcpy(gplotlabel,"("); 7510: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */ 7511: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */ 7512: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7513: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7514: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7515: vlv= nbcode[Tvaraff[k]][lv]; 7516: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7517: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7518: } 7519: /* for(k=1; k <= ncovds; k++){ */ 7520: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7521: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7522: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7523: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7524: } 7525: strcpy(gplotlabel+strlen(gplotlabel),")"); 7526: fprintf(ficgp,"\n#\n"); 7527: if(invalidvarcomb[k1]){ 7528: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7529: continue; 7530: } 7531: 7532: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1,nres); 7533: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/ 7534: fprintf(ficgp,"\nset label \"popbased %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",vpopbased,gplotlabel); 7535: if(vpopbased==0){ 7536: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage); 7537: }else 7538: fprintf(ficgp,"\nreplot "); 7539: for (i=1; i<= nlstate+1 ; i ++) { 7540: k=2*i; 7541: 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); 7542: for (j=1; j<= nlstate+1 ; j ++) { 7543: if (j==i) fprintf(ficgp," %%lf (%%lf)"); 7544: else fprintf(ficgp," %%*lf (%%*lf)"); 7545: } 7546: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i); 7547: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1); 7548: 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); 7549: for (j=1; j<= nlstate+1 ; j ++) { 7550: if (j==i) fprintf(ficgp," %%lf (%%lf)"); 7551: else fprintf(ficgp," %%*lf (%%*lf)"); 7552: } 7553: fprintf(ficgp,"\" t\"\" w l lt 0,"); 7554: 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); 7555: for (j=1; j<= nlstate+1 ; j ++) { 7556: if (j==i) fprintf(ficgp," %%lf (%%lf)"); 7557: else fprintf(ficgp," %%*lf (%%*lf)"); 7558: } 7559: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0"); 7560: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n"); 7561: } /* state */ 7562: } /* vpopbased */ 7563: fprintf(ficgp,"\nset out;set out \"%s_%d-%d.svg\"; replot; set out; unset label;\n",subdirf2(optionfilefiname,"E_"),k1,nres); /* Buggy gnuplot */ 7564: } /* end nres */ 7565: } /* k1 end 2 eme*/ 7566: 7567: 7568: /*3eme*/ 7569: for (k1=1; k1<= m ; k1 ++){ 7570: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7571: if(m != 1 && TKresult[nres]!= k1) 7572: continue; 7573: 7574: for (cpt=1; cpt<= nlstate ; cpt ++) { 7575: fprintf(ficgp,"\n\n# 3d: Life expectancy with EXP_ files: combination=%d state=%d",k1, cpt); 7576: strcpy(gplotlabel,"("); 7577: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */ 7578: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */ 7579: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7580: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7581: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7582: vlv= nbcode[Tvaraff[k]][lv]; 7583: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7584: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7585: } 7586: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7587: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7588: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7589: } 7590: strcpy(gplotlabel+strlen(gplotlabel),")"); 7591: fprintf(ficgp,"\n#\n"); 7592: if(invalidvarcomb[k1]){ 7593: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7594: continue; 7595: } 7596: 7597: /* k=2+nlstate*(2*cpt-2); */ 7598: k=2+(nlstate+1)*(cpt-1); 7599: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1,nres); 7600: fprintf(ficgp,"set label \"%s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); 7601: fprintf(ficgp,"set ter svg size 640, 480\n\ 7602: 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); 7603: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1); 7604: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) "); 7605: fprintf(ficgp,"\" t \"e%d1\" w l",cpt); 7606: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1); 7607: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) "); 7608: fprintf(ficgp,"\" t \"e%d1\" w l",cpt); 7609: 7610: */ 7611: for (i=1; i< nlstate ; i ++) { 7612: 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); 7613: /* 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);*/ 7614: 7615: } 7616: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),nres-1,nres-1,k+nlstate,cpt); 7617: } 7618: fprintf(ficgp,"\nunset label;\n"); 7619: } /* end nres */ 7620: } /* end kl 3eme */ 7621: 7622: /* 4eme */ 7623: /* Survival functions (period) from state i in state j by initial state i */ 7624: for (k1=1; k1<=m; k1++){ /* For each covariate and each value */ 7625: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7626: if(m != 1 && TKresult[nres]!= k1) 7627: continue; 7628: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state cpt*/ 7629: strcpy(gplotlabel,"("); 7630: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt); 7631: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */ 7632: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */ 7633: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7634: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7635: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7636: vlv= nbcode[Tvaraff[k]][lv]; 7637: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7638: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7639: } 7640: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7641: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7642: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7643: } 7644: strcpy(gplotlabel+strlen(gplotlabel),")"); 7645: fprintf(ficgp,"\n#\n"); 7646: if(invalidvarcomb[k1]){ 7647: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7648: continue; 7649: } 7650: 7651: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1,nres); 7652: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); 7653: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\ 7654: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar); 7655: k=3; 7656: for (i=1; i<= nlstate ; i ++){ 7657: if(i==1){ 7658: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_")); 7659: }else{ 7660: fprintf(ficgp,", '' "); 7661: } 7662: l=(nlstate+ndeath)*(i-1)+1; 7663: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); 7664: for (j=2; j<= nlstate+ndeath ; j ++) 7665: fprintf(ficgp,"+$%d",k+l+j-1); 7666: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt); 7667: } /* nlstate */ 7668: fprintf(ficgp,"\nset out; unset label;\n"); 7669: } /* end cpt state*/ 7670: } /* end nres */ 7671: } /* end covariate k1 */ 7672: 7673: /* 5eme */ 7674: /* Survival functions (period) from state i in state j by final state j */ 7675: for (k1=1; k1<= m ; k1++){ /* For each covariate combination if any */ 7676: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7677: if(m != 1 && TKresult[nres]!= k1) 7678: continue; 7679: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */ 7680: strcpy(gplotlabel,"("); 7681: 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); 7682: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */ 7683: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */ 7684: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7685: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7686: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7687: vlv= nbcode[Tvaraff[k]][lv]; 7688: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7689: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7690: } 7691: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7692: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7693: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7694: } 7695: strcpy(gplotlabel+strlen(gplotlabel),")"); 7696: fprintf(ficgp,"\n#\n"); 7697: if(invalidvarcomb[k1]){ 7698: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7699: continue; 7700: } 7701: 7702: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1,nres); 7703: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); 7704: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\ 7705: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar); 7706: k=3; 7707: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */ 7708: if(j==1) 7709: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_")); 7710: else 7711: fprintf(ficgp,", '' "); 7712: l=(nlstate+ndeath)*(cpt-1) +j; 7713: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l); 7714: /* for (i=2; i<= nlstate+ndeath ; i ++) */ 7715: /* fprintf(ficgp,"+$%d",k+l+i-1); */ 7716: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j); 7717: } /* nlstate */ 7718: fprintf(ficgp,", '' "); 7719: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1); 7720: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */ 7721: l=(nlstate+ndeath)*(cpt-1) +j; 7722: if(j < nlstate) 7723: fprintf(ficgp,"$%d +",k+l); 7724: else 7725: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt); 7726: } 7727: fprintf(ficgp,"\nset out; unset label;\n"); 7728: } /* end cpt state*/ 7729: } /* end covariate */ 7730: } /* end nres */ 7731: 7732: /* 6eme */ 7733: /* CV preval stable (period) for each covariate */ 7734: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */ 7735: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7736: if(m != 1 && TKresult[nres]!= k1) 7737: continue; 7738: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state of arrival */ 7739: strcpy(gplotlabel,"("); 7740: fprintf(ficgp,"\n#\n#\n#CV preval stable (forward): 'pij' files, covariatecombination#=%d state=%d",k1, cpt); 7741: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */ 7742: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */ 7743: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7744: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7745: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7746: vlv= nbcode[Tvaraff[k]][lv]; 7747: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7748: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7749: } 7750: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7751: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7752: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7753: } 7754: strcpy(gplotlabel+strlen(gplotlabel),")"); 7755: fprintf(ficgp,"\n#\n"); 7756: if(invalidvarcomb[k1]){ 7757: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7758: continue; 7759: } 7760: 7761: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1,nres); 7762: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); 7763: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\ 7764: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar); 7765: k=3; /* Offset */ 7766: for (i=1; i<= nlstate ; i ++){ /* State of origin */ 7767: if(i==1) 7768: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_")); 7769: else 7770: fprintf(ficgp,", '' "); 7771: l=(nlstate+ndeath)*(i-1)+1; /* 1, 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */ 7772: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); 7773: for (j=2; j<= nlstate ; j ++) 7774: fprintf(ficgp,"+$%d",k+l+j-1); 7775: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt); 7776: } /* nlstate */ 7777: fprintf(ficgp,"\nset out; unset label;\n"); 7778: } /* end cpt state*/ 7779: } /* end covariate */ 7780: 7781: 7782: /* 7eme */ 7783: if(prevbcast == 1){ 7784: /* CV backward prevalence for each covariate */ 7785: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */ 7786: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7787: if(m != 1 && TKresult[nres]!= k1) 7788: continue; 7789: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life origin state */ 7790: strcpy(gplotlabel,"("); 7791: fprintf(ficgp,"\n#\n#\n#CV Backward stable prevalence: 'pijb' files, covariatecombination#=%d state=%d",k1, cpt); 7792: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */ 7793: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */ 7794: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7795: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7796: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7797: vlv= nbcode[Tvaraff[k]][lv]; 7798: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7799: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7800: } 7801: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7802: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7803: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7804: } 7805: strcpy(gplotlabel+strlen(gplotlabel),")"); 7806: fprintf(ficgp,"\n#\n"); 7807: if(invalidvarcomb[k1]){ 7808: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7809: continue; 7810: } 7811: 7812: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1,nres); 7813: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); 7814: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\ 7815: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar); 7816: k=3; /* Offset */ 7817: for (i=1; i<= nlstate ; i ++){ /* State of arrival */ 7818: if(i==1) 7819: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_")); 7820: else 7821: fprintf(ficgp,", '' "); 7822: /* l=(nlstate+ndeath)*(i-1)+1; */ 7823: l=(nlstate+ndeath)*(cpt-1)+1; /* fixed for i; cpt=1 1, cpt=2 1+ nlstate+ndeath, 1+2*(nlstate+ndeath) */ 7824: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */ 7825: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */ 7826: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+i-1); /* To be verified */ 7827: /* for (j=2; j<= nlstate ; j ++) */ 7828: /* fprintf(ficgp,"+$%d",k+l+j-1); */ 7829: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */ 7830: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",cpt,i); 7831: } /* nlstate */ 7832: fprintf(ficgp,"\nset out; unset label;\n"); 7833: } /* end cpt state*/ 7834: } /* end covariate */ 7835: } /* End if prevbcast */ 7836: 7837: /* 8eme */ 7838: if(prevfcast==1){ 7839: /* Projection from cross-sectional to forward stable (period) prevalence for each covariate */ 7840: 7841: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */ 7842: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7843: if(m != 1 && TKresult[nres]!= k1) 7844: continue; 7845: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */ 7846: strcpy(gplotlabel,"("); 7847: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to forward stable prevalence (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt); 7848: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */ 7849: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */ 7850: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7851: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7852: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7853: vlv= nbcode[Tvaraff[k]][lv]; 7854: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7855: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7856: } 7857: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7858: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7859: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7860: } 7861: strcpy(gplotlabel+strlen(gplotlabel),")"); 7862: fprintf(ficgp,"\n#\n"); 7863: if(invalidvarcomb[k1]){ 7864: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7865: continue; 7866: } 7867: 7868: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n "); 7869: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1,nres); 7870: fprintf(ficgp,"set label \"Alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); 7871: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\ 7872: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar); 7873: 7874: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */ 7875: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */ 7876: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */ 7877: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */ 7878: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/ 7879: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 7880: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/ 7881: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 7882: if(i==istart){ 7883: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_")); 7884: }else{ 7885: fprintf(ficgp,",\\\n '' "); 7886: } 7887: if(cptcoveff ==0){ /* No covariate */ 7888: ioffset=2; /* Age is in 2 */ 7889: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/ 7890: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */ 7891: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/ 7892: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */ 7893: fprintf(ficgp," u %d:(", ioffset); 7894: if(i==nlstate+1){ 7895: fprintf(ficgp," $%d/(1.-$%d)):1 t 'pw.%d' with line lc variable ", \ 7896: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt ); 7897: fprintf(ficgp,",\\\n '' "); 7898: fprintf(ficgp," u %d:(",ioffset); 7899: fprintf(ficgp," (($1-$2) == %d ) ? $%d/(1.-$%d) : 1/0):1 with labels center not ", \ 7900: offyear, \ 7901: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate ); 7902: }else 7903: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \ 7904: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt ); 7905: }else{ /* more than 2 covariates */ 7906: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/ 7907: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/ 7908: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 7909: iyearc=ioffset-1; 7910: iagec=ioffset; 7911: fprintf(ficgp," u %d:(",ioffset); 7912: kl=0; 7913: strcpy(gplotcondition,"("); 7914: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */ 7915: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */ 7916: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7917: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7918: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7919: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */ 7920: kl++; 7921: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); 7922: kl++; 7923: if(k <cptcoveff && cptcoveff>1) 7924: sprintf(gplotcondition+strlen(gplotcondition)," && "); 7925: } 7926: strcpy(gplotcondition+strlen(gplotcondition),")"); 7927: /* 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 *\/ */ 7928: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */ 7929: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */ 7930: /* '' 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*/ 7931: if(i==nlstate+1){ 7932: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0):%d t 'p.%d' with line lc variable", gplotcondition, \ 7933: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,iyearc, cpt ); 7934: fprintf(ficgp,",\\\n '' "); 7935: fprintf(ficgp," u %d:(",iagec); 7936: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d/(1.-$%d) : 1/0):%d with labels center not ", gplotcondition, \ 7937: iyearc, iagec, offyear, \ 7938: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate, iyearc ); 7939: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/ 7940: }else{ 7941: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ 7942: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt ); 7943: } 7944: } /* end if covariate */ 7945: } /* nlstate */ 7946: fprintf(ficgp,"\nset out; unset label;\n"); 7947: } /* end cpt state*/ 7948: } /* end covariate */ 7949: } /* End if prevfcast */ 7950: 7951: if(prevbcast==1){ 7952: /* Back projection from cross-sectional to stable (mixed) for each covariate */ 7953: 7954: for (k1=1; k1<= m ; k1 ++) /* For each covariate combination if any */ 7955: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 7956: if(m != 1 && TKresult[nres]!= k1) 7957: continue; 7958: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */ 7959: strcpy(gplotlabel,"("); 7960: fprintf(ficgp,"\n#\n#\n#Back projection of prevalence to stable (mixed) back prevalence: 'BPROJ_' files, covariatecombination#=%d originstate=%d",k1, cpt); 7961: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */ 7962: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */ 7963: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 7964: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 7965: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 7966: vlv= nbcode[Tvaraff[k]][lv]; 7967: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 7968: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 7969: } 7970: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 7971: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7972: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 7973: } 7974: strcpy(gplotlabel+strlen(gplotlabel),")"); 7975: fprintf(ficgp,"\n#\n"); 7976: if(invalidvarcomb[k1]){ 7977: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1); 7978: continue; 7979: } 7980: 7981: fprintf(ficgp,"# hbijx=backprobability over h years, hb.jx is weighted by observed prev at destination state\n "); 7982: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJB_"),cpt,k1,nres); 7983: fprintf(ficgp,"set label \"Origin alive state %d %s\" at graph 0.98,0.5 center rotate font \"Helvetica,12\"\n",cpt,gplotlabel); 7984: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\ 7985: set ter svg size 640, 480\nunset log y\nplot [%.f:%.f] ", ageminpar, agemaxpar); 7986: 7987: /* for (i=1; i<= nlstate+1 ; i ++){ /\* nlstate +1 p11 p21 p.1 *\/ */ 7988: istart=nlstate+1; /* Could be one if by state, but nlstate+1 is w.i projection only */ 7989: /*istart=1;*/ /* Could be one if by state, but nlstate+1 is w.i projection only */ 7990: for (i=istart; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */ 7991: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/ 7992: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 7993: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/ 7994: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 7995: if(i==istart){ 7996: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"FB_")); 7997: }else{ 7998: fprintf(ficgp,",\\\n '' "); 7999: } 8000: if(cptcoveff ==0){ /* No covariate */ 8001: ioffset=2; /* Age is in 2 */ 8002: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/ 8003: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */ 8004: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/ 8005: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */ 8006: fprintf(ficgp," u %d:(", ioffset); 8007: if(i==nlstate+1){ 8008: fprintf(ficgp," $%d/(1.-$%d)):1 t 'bw%d' with line lc variable ", \ 8009: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt ); 8010: fprintf(ficgp,",\\\n '' "); 8011: fprintf(ficgp," u %d:(",ioffset); 8012: fprintf(ficgp," (($1-$2) == %d ) ? $%d : 1/0):1 with labels center not ", \ 8013: offbyear, \ 8014: ioffset+(cpt-1)*(nlstate+1)+1+(i-1) ); 8015: }else 8016: fprintf(ficgp," $%d/(1.-$%d)) t 'b%d%d' with line ", \ 8017: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt,i ); 8018: }else{ /* more than 2 covariates */ 8019: ioffset=2*cptcoveff+2; /* Age is in 4 or 6 or etc.*/ 8020: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/ 8021: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */ 8022: iyearc=ioffset-1; 8023: iagec=ioffset; 8024: fprintf(ficgp," u %d:(",ioffset); 8025: kl=0; 8026: strcpy(gplotcondition,"("); 8027: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */ 8028: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */ 8029: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 8030: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 8031: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 8032: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */ 8033: kl++; 8034: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]); 8035: kl++; 8036: if(k <cptcoveff && cptcoveff>1) 8037: sprintf(gplotcondition+strlen(gplotcondition)," && "); 8038: } 8039: strcpy(gplotcondition+strlen(gplotcondition),")"); 8040: /* 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 *\/ */ 8041: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */ 8042: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */ 8043: /* '' 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*/ 8044: if(i==nlstate+1){ 8045: fprintf(ficgp,"%s ? $%d : 1/0):%d t 'bw%d' with line lc variable", gplotcondition, \ 8046: ioffset+(cpt-1)*(nlstate+1)+1+(i-1),iyearc,cpt ); 8047: fprintf(ficgp,",\\\n '' "); 8048: fprintf(ficgp," u %d:(",iagec); 8049: /* fprintf(ficgp,"%s && (($5-$6) == %d ) ? $%d/(1.-$%d) : 1/0):5 with labels center not ", gplotcondition, \ */ 8050: fprintf(ficgp,"%s && (($%d-$%d) == %d ) ? $%d : 1/0):%d with labels center not ", gplotcondition, \ 8051: iyearc,iagec,offbyear, \ 8052: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), iyearc ); 8053: /* '' u 6:(($1==1 && $2==0 && $3==2 && $4==0) && (($5-$6) == 1947) ? $10/(1.-$22) : 1/0):5 with labels center boxed not*/ 8054: }else{ 8055: /* fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \ */ 8056: fprintf(ficgp,"%s ? $%d : 1/0) t 'b%d%d' with line ", gplotcondition, \ 8057: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), cpt,i ); 8058: } 8059: } /* end if covariate */ 8060: } /* nlstate */ 8061: fprintf(ficgp,"\nset out; unset label;\n"); 8062: } /* end cpt state*/ 8063: } /* end covariate */ 8064: } /* End if prevbcast */ 8065: 8066: 8067: /* 9eme writing MLE parameters */ 8068: fprintf(ficgp,"\n##############\n#9eme MLE estimated parameters\n#############\n"); 8069: for(i=1,jk=1; i <=nlstate; i++){ 8070: fprintf(ficgp,"# initial state %d\n",i); 8071: for(k=1; k <=(nlstate+ndeath); k++){ 8072: if (k != i) { 8073: fprintf(ficgp,"# current state %d\n",k); 8074: for(j=1; j <=ncovmodel; j++){ 8075: fprintf(ficgp,"p%d=%f; ",jk,p[jk]); 8076: jk++; 8077: } 8078: fprintf(ficgp,"\n"); 8079: } 8080: } 8081: } 8082: fprintf(ficgp,"##############\n#\n"); 8083: 8084: /*goto avoid;*/ 8085: /* 10eme Graphics of probabilities or incidences using written MLE parameters */ 8086: fprintf(ficgp,"\n##############\n#10eme Graphics of probabilities or incidences\n#############\n"); 8087: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n"); 8088: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n"); 8089: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n"); 8090: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n"); 8091: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n"); 8092: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n"); 8093: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n"); 8094: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n"); 8095: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n"); 8096: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n"); 8097: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n"); 8098: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n"); 8099: fprintf(ficgp,"#\n"); 8100: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/ 8101: fprintf(ficgp,"#Number of graphics: first is logit, 2nd is probabilities, third is incidences per year\n"); 8102: fprintf(ficgp,"#model=%s \n",model); 8103: fprintf(ficgp,"# Type of graphic ng=%d\n",ng); 8104: fprintf(ficgp,"# k1=1 to 2^%d=%d\n",cptcoveff,m);/* to be checked */ 8105: for(k1=1; k1 <=m; k1++) /* For each combination of covariate */ 8106: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 8107: if(m != 1 && TKresult[nres]!= k1) 8108: continue; 8109: fprintf(ficgp,"\n\n# Combination of dummy k1=%d which is ",k1); 8110: strcpy(gplotlabel,"("); 8111: /*sprintf(gplotlabel+strlen(gplotlabel)," Dummy combination %d ",k1);*/ 8112: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */ 8113: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */ 8114: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */ 8115: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */ 8116: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */ 8117: vlv= nbcode[Tvaraff[k]][lv]; 8118: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv); 8119: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%d ",Tvaraff[k],vlv); 8120: } 8121: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 8122: fprintf(ficgp," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 8123: sprintf(gplotlabel+strlen(gplotlabel)," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 8124: } 8125: strcpy(gplotlabel+strlen(gplotlabel),")"); 8126: fprintf(ficgp,"\n#\n"); 8127: fprintf(ficgp,"\nset out \"%s_%d-%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),k1,ng,nres); 8128: fprintf(ficgp,"\nset key outside "); 8129: /* fprintf(ficgp,"\nset label \"%s\" at graph 1.2,0.5 center rotate font \"Helvetica,12\"\n",gplotlabel); */ 8130: fprintf(ficgp,"\nset title \"%s\" font \"Helvetica,12\"\n",gplotlabel); 8131: fprintf(ficgp,"\nset ter svg size 640, 480 "); 8132: if (ng==1){ 8133: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */ 8134: fprintf(ficgp,"\nunset log y"); 8135: }else if (ng==2){ 8136: fprintf(ficgp,"\nset ylabel \"Probability\"\n"); 8137: fprintf(ficgp,"\nset log y"); 8138: }else if (ng==3){ 8139: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n"); 8140: fprintf(ficgp,"\nset log y"); 8141: }else 8142: fprintf(ficgp,"\nunset title "); 8143: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar); 8144: i=1; 8145: for(k2=1; k2<=nlstate; k2++) { 8146: k3=i; 8147: for(k=1; k<=(nlstate+ndeath); k++) { 8148: if (k != k2){ 8149: switch( ng) { 8150: case 1: 8151: if(nagesqr==0) 8152: fprintf(ficgp," p%d+p%d*x",i,i+1); 8153: else /* nagesqr =1 */ 8154: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr); 8155: break; 8156: case 2: /* ng=2 */ 8157: if(nagesqr==0) 8158: fprintf(ficgp," exp(p%d+p%d*x",i,i+1); 8159: else /* nagesqr =1 */ 8160: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr); 8161: break; 8162: case 3: 8163: if(nagesqr==0) 8164: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1); 8165: else /* nagesqr =1 */ 8166: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr); 8167: break; 8168: } 8169: ij=1;/* To be checked else nbcode[0][0] wrong */ 8170: ijp=1; /* product no age */ 8171: /* for(j=3; j <=ncovmodel-nagesqr; j++) { */ 8172: for(j=1; j <=cptcovt; j++) { /* For each covariate of the simplified model */ 8173: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */ 8174: if(cptcovage >0){ /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */ 8175: if(j==Tage[ij]) { /* Product by age To be looked at!!*/ 8176: if(ij <=cptcovage) { /* V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1, 2 V5 and V1 */ 8177: if(DummyV[j]==0){ 8178: fprintf(ficgp,"+p%d*%d*x",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]);; 8179: }else{ /* quantitative */ 8180: fprintf(ficgp,"+p%d*%f*x",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* Tqinvresult in decoderesult */ 8181: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */ 8182: } 8183: ij++; 8184: } 8185: } 8186: }else if(cptcovprod >0){ 8187: if(j==Tprod[ijp]) { /* */ 8188: /* printf("Tprod[%d]=%d, j=%d\n", ij, Tprod[ijp], j); */ 8189: if(ijp <=cptcovprod) { /* Product */ 8190: if(DummyV[Tvard[ijp][1]]==0){/* Vn is dummy */ 8191: if(DummyV[Tvard[ijp][2]]==0){/* Vn and Vm are dummy */ 8192: /* 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)]); */ 8193: fprintf(ficgp,"+p%d*%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tinvresult[nres][Tvard[ijp][2]]); 8194: }else{ /* Vn is dummy and Vm is quanti */ 8195: /* fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,nbcode[Tvard[ijp][1]][codtabm(k1,j)],Tqinvresult[nres][Tvard[ijp][2]]); */ 8196: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); 8197: } 8198: }else{ /* Vn*Vm Vn is quanti */ 8199: if(DummyV[Tvard[ijp][2]]==0){ 8200: fprintf(ficgp,"+p%d*%d*%f",i+j+2+nagesqr-1,Tinvresult[nres][Tvard[ijp][2]],Tqinvresult[nres][Tvard[ijp][1]]); 8201: }else{ /* Both quanti */ 8202: fprintf(ficgp,"+p%d*%f*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvard[ijp][1]],Tqinvresult[nres][Tvard[ijp][2]]); 8203: } 8204: } 8205: ijp++; 8206: } 8207: } /* end Tprod */ 8208: } else{ /* simple covariate */ 8209: /* fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,nbcode[Tvar[j]][codtabm(k1,j)]); /\* Valgrind bug nbcode *\/ */ 8210: if(Dummy[j]==0){ 8211: fprintf(ficgp,"+p%d*%d",i+j+2+nagesqr-1,Tinvresult[nres][Tvar[j]]); /* */ 8212: }else{ /* quantitative */ 8213: fprintf(ficgp,"+p%d*%f",i+j+2+nagesqr-1,Tqinvresult[nres][Tvar[j]]); /* */ 8214: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */ 8215: } 8216: } /* end simple */ 8217: } /* end j */ 8218: }else{ 8219: i=i-ncovmodel; 8220: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */ 8221: fprintf(ficgp," (1."); 8222: } 8223: 8224: if(ng != 1){ 8225: fprintf(ficgp,")/(1"); 8226: 8227: for(cpt=1; cpt <=nlstate; cpt++){ 8228: if(nagesqr==0) 8229: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(cpt-1)*ncovmodel,k3+(cpt-1)*ncovmodel+1); 8230: else /* nagesqr =1 */ 8231: 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); 8232: 8233: ij=1; 8234: for(j=3; j <=ncovmodel-nagesqr; j++){ 8235: if(cptcovage >0){ 8236: if((j-2)==Tage[ij]) { /* Bug valgrind */ 8237: if(ij <=cptcovage) { /* Bug valgrind */ 8238: fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]); 8239: /* fprintf(ficgp,"+p%d*%d*x",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,Tvar[j-2])]); */ 8240: ij++; 8241: } 8242: } 8243: }else 8244: fprintf(ficgp,"+p%d*%d",k3+(cpt-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(k1,j-2)]);/* Valgrind bug nbcode */ 8245: } 8246: fprintf(ficgp,")"); 8247: } 8248: fprintf(ficgp,")"); 8249: if(ng ==2) 8250: 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); 8251: else /* ng= 3 */ 8252: 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); 8253: }else{ /* end ng <> 1 */ 8254: if( k !=k2) /* logit p11 is hard to draw */ 8255: 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); 8256: } 8257: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1) 8258: fprintf(ficgp,","); 8259: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath)) 8260: fprintf(ficgp,","); 8261: i=i+ncovmodel; 8262: } /* end k */ 8263: } /* end k2 */ 8264: /* fprintf(ficgp,"\n set out; unset label;set key default;\n"); */ 8265: fprintf(ficgp,"\n set out; unset title;set key default;\n"); 8266: } /* end k1 */ 8267: } /* end ng */ 8268: /* avoid: */ 8269: fflush(ficgp); 8270: } /* end gnuplot */ 8271: 8272: 8273: /*************** Moving average **************/ 8274: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */ 8275: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){ 8276: 8277: int i, cpt, cptcod; 8278: int modcovmax =1; 8279: int mobilavrange, mob; 8280: int iage=0; 8281: int firstA1=0, firstA2=0; 8282: 8283: double sum=0., sumr=0.; 8284: double age; 8285: double *sumnewp, *sumnewm, *sumnewmr; 8286: double *agemingood, *agemaxgood; 8287: double *agemingoodr, *agemaxgoodr; 8288: 8289: 8290: /* modcovmax=2*cptcoveff; Max number of modalities. We suppose */ 8291: /* a covariate has 2 modalities, should be equal to ncovcombmax */ 8292: 8293: sumnewp = vector(1,ncovcombmax); 8294: sumnewm = vector(1,ncovcombmax); 8295: sumnewmr = vector(1,ncovcombmax); 8296: agemingood = vector(1,ncovcombmax); 8297: agemingoodr = vector(1,ncovcombmax); 8298: agemaxgood = vector(1,ncovcombmax); 8299: agemaxgoodr = vector(1,ncovcombmax); 8300: 8301: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ 8302: sumnewm[cptcod]=0.; sumnewmr[cptcod]=0.; 8303: sumnewp[cptcod]=0.; 8304: agemingood[cptcod]=0, agemingoodr[cptcod]=0; 8305: agemaxgood[cptcod]=0, agemaxgoodr[cptcod]=0; 8306: } 8307: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */ 8308: 8309: if(mobilav==-1 || mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){ 8310: if(mobilav==1 || mobilav==-1) mobilavrange=5; /* default */ 8311: else mobilavrange=mobilav; 8312: for (age=bage; age<=fage; age++) 8313: for (i=1; i<=nlstate;i++) 8314: for (cptcod=1;cptcod<=ncovcombmax;cptcod++) 8315: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod]; 8316: /* We keep the original values on the extreme ages bage, fage and for 8317: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2 8318: we use a 5 terms etc. until the borders are no more concerned. 8319: */ 8320: for (mob=3;mob <=mobilavrange;mob=mob+2){ 8321: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ 8322: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ 8323: sumnewm[cptcod]=0.; 8324: for (i=1; i<=nlstate;i++){ 8325: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod]; 8326: for (cpt=1;cpt<=(mob-1)/2;cpt++){ 8327: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod]; 8328: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod]; 8329: } 8330: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob; 8331: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod]; 8332: } /* end i */ 8333: if(sumnewm[cptcod] >1.e-3) mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/sumnewm[cptcod]; /* Rescaling to sum one */ 8334: } /* end cptcod */ 8335: }/* end age */ 8336: }/* end mob */ 8337: }else{ 8338: printf("Error internal in movingaverage, mobilav=%d.\n",mobilav); 8339: return -1; 8340: } 8341: 8342: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){ /* for each combination */ 8343: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */ 8344: if(invalidvarcomb[cptcod]){ 8345: printf("\nCombination (%d) ignored because no cases \n",cptcod); 8346: continue; 8347: } 8348: 8349: for (age=fage-(mob-1)/2; age>=bage+(mob-1)/2; age--){ /*looking for the youngest and oldest good age */ 8350: sumnewm[cptcod]=0.; 8351: sumnewmr[cptcod]=0.; 8352: for (i=1; i<=nlstate;i++){ 8353: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod]; 8354: sumnewmr[cptcod]+=probs[(int)age][i][cptcod]; 8355: } 8356: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */ 8357: agemingoodr[cptcod]=age; 8358: } 8359: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */ 8360: agemingood[cptcod]=age; 8361: } 8362: } /* age */ 8363: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ /*looking for the youngest and oldest good age */ 8364: sumnewm[cptcod]=0.; 8365: sumnewmr[cptcod]=0.; 8366: for (i=1; i<=nlstate;i++){ 8367: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod]; 8368: sumnewmr[cptcod]+=probs[(int)age][i][cptcod]; 8369: } 8370: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */ 8371: agemaxgoodr[cptcod]=age; 8372: } 8373: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */ 8374: agemaxgood[cptcod]=age; 8375: } 8376: } /* age */ 8377: /* Thus we have agemingood and agemaxgood as well as goodr for raw (preobs) */ 8378: /* but they will change */ 8379: firstA1=0;firstA2=0; 8380: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, filling up to the youngest */ 8381: sumnewm[cptcod]=0.; 8382: sumnewmr[cptcod]=0.; 8383: for (i=1; i<=nlstate;i++){ 8384: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod]; 8385: sumnewmr[cptcod]+=probs[(int)age][i][cptcod]; 8386: } 8387: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */ 8388: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good without smoothing */ 8389: agemaxgoodr[cptcod]=age; /* age min */ 8390: for (i=1; i<=nlstate;i++) 8391: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod]; 8392: }else{ /* bad we change the value with the values of good ages */ 8393: for (i=1; i<=nlstate;i++){ 8394: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgoodr[cptcod]][i][cptcod]; 8395: } /* i */ 8396: } /* end bad */ 8397: }else{ 8398: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */ 8399: agemaxgood[cptcod]=age; 8400: }else{ /* bad we change the value with the values of good ages */ 8401: for (i=1; i<=nlstate;i++){ 8402: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; 8403: } /* i */ 8404: } /* end bad */ 8405: }/* end else */ 8406: sum=0.;sumr=0.; 8407: for (i=1; i<=nlstate;i++){ 8408: sum+=mobaverage[(int)age][i][cptcod]; 8409: sumr+=probs[(int)age][i][cptcod]; 8410: } 8411: if(fabs(sum - 1.) > 1.e-3) { /* bad */ 8412: if(!firstA1){ 8413: firstA1=1; 8414: 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); 8415: } 8416: 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); 8417: } /* end bad */ 8418: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */ 8419: if(fabs(sumr - 1.) > 1.e-3) { /* bad */ 8420: if(!firstA2){ 8421: firstA2=1; 8422: 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); 8423: } 8424: 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); 8425: } /* end bad */ 8426: }/* age */ 8427: 8428: for (age=bage+(mob-1)/2; age<=fage; age++){/* From youngest, finding the oldest wrong */ 8429: sumnewm[cptcod]=0.; 8430: sumnewmr[cptcod]=0.; 8431: for (i=1; i<=nlstate;i++){ 8432: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod]; 8433: sumnewmr[cptcod]+=probs[(int)age][i][cptcod]; 8434: } 8435: if(mobilav==-1){ /* Forcing raw ages if good else agemingood */ 8436: if(fabs(sumnewmr[cptcod] - 1.) <= 1.e-3) { /* good */ 8437: agemingoodr[cptcod]=age; 8438: for (i=1; i<=nlstate;i++) 8439: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod]; 8440: }else{ /* bad we change the value with the values of good ages */ 8441: for (i=1; i<=nlstate;i++){ 8442: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingoodr[cptcod]][i][cptcod]; 8443: } /* i */ 8444: } /* end bad */ 8445: }else{ 8446: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */ 8447: agemingood[cptcod]=age; 8448: }else{ /* bad */ 8449: for (i=1; i<=nlstate;i++){ 8450: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; 8451: } /* i */ 8452: } /* end bad */ 8453: }/* end else */ 8454: sum=0.;sumr=0.; 8455: for (i=1; i<=nlstate;i++){ 8456: sum+=mobaverage[(int)age][i][cptcod]; 8457: sumr+=mobaverage[(int)age][i][cptcod]; 8458: } 8459: if(fabs(sum - 1.) > 1.e-3) { /* bad */ 8460: 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); 8461: } /* end bad */ 8462: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */ 8463: if(fabs(sumr - 1.) > 1.e-3) { /* bad */ 8464: 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); 8465: } /* end bad */ 8466: }/* age */ 8467: 8468: 8469: for (age=bage; age<=fage; age++){ 8470: /* printf("%d %d ", cptcod, (int)age); */ 8471: sumnewp[cptcod]=0.; 8472: sumnewm[cptcod]=0.; 8473: for (i=1; i<=nlstate;i++){ 8474: sumnewp[cptcod]+=probs[(int)age][i][cptcod]; 8475: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod]; 8476: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */ 8477: } 8478: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */ 8479: } 8480: /* printf("\n"); */ 8481: /* } */ 8482: 8483: /* brutal averaging */ 8484: /* for (i=1; i<=nlstate;i++){ */ 8485: /* for (age=1; age<=bage; age++){ */ 8486: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */ 8487: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */ 8488: /* } */ 8489: /* for (age=fage; age<=AGESUP; age++){ */ 8490: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod]; */ 8491: /* /\* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); *\/ */ 8492: /* } */ 8493: /* } /\* end i status *\/ */ 8494: /* for (i=nlstate+1; i<=nlstate+ndeath;i++){ */ 8495: /* for (age=1; age<=AGESUP; age++){ */ 8496: /* /\*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*\/ */ 8497: /* mobaverage[(int)age][i][cptcod]=0.; */ 8498: /* } */ 8499: /* } */ 8500: }/* end cptcod */ 8501: free_vector(agemaxgoodr,1, ncovcombmax); 8502: free_vector(agemaxgood,1, ncovcombmax); 8503: free_vector(agemingood,1, ncovcombmax); 8504: free_vector(agemingoodr,1, ncovcombmax); 8505: free_vector(sumnewmr,1, ncovcombmax); 8506: free_vector(sumnewm,1, ncovcombmax); 8507: free_vector(sumnewp,1, ncovcombmax); 8508: return 0; 8509: }/* End movingaverage */ 8510: 8511: 8512: 8513: /************** Forecasting ******************/ 8514: /* 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)*/ 8515: 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){ 8516: /* dateintemean, mean date of interviews 8517: dateprojd, year, month, day of starting projection 8518: dateprojf date of end of projection;year of end of projection (same day and month as proj1). 8519: agemin, agemax range of age 8520: dateprev1 dateprev2 range of dates during which prevalence is computed 8521: */ 8522: /* double anprojd, mprojd, jprojd; */ 8523: /* double anprojf, mprojf, jprojf; */ 8524: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0; 8525: double agec; /* generic age */ 8526: double agelim, ppij, yp,yp1,yp2; 8527: double *popeffectif,*popcount; 8528: double ***p3mat; 8529: /* double ***mobaverage; */ 8530: char fileresf[FILENAMELENGTH]; 8531: 8532: agelim=AGESUP; 8533: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people 8534: in each health status at the date of interview (if between dateprev1 and dateprev2). 8535: We still use firstpass and lastpass as another selection. 8536: */ 8537: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */ 8538: /* firstpass, lastpass, stepm, weightopt, model); */ 8539: 8540: strcpy(fileresf,"F_"); 8541: strcat(fileresf,fileresu); 8542: if((ficresf=fopen(fileresf,"w"))==NULL) { 8543: printf("Problem with forecast resultfile: %s\n", fileresf); 8544: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf); 8545: } 8546: printf("\nComputing forecasting: result on file '%s', please wait... \n", fileresf); 8547: fprintf(ficlog,"\nComputing forecasting: result on file '%s', please wait... \n", fileresf); 8548: 8549: if (cptcoveff==0) ncodemax[cptcoveff]=1; 8550: 8551: 8552: stepsize=(int) (stepm+YEARM-1)/YEARM; 8553: if (stepm<=12) stepsize=1; 8554: if(estepm < stepm){ 8555: printf ("Problem %d lower than %d\n",estepm, stepm); 8556: } 8557: else{ 8558: hstepm=estepm; 8559: } 8560: if(estepm > stepm){ /* Yes every two year */ 8561: stepsize=2; 8562: } 8563: hstepm=hstepm/stepm; 8564: 8565: 8566: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */ 8567: /* fractional in yp1 *\/ */ 8568: /* aintmean=yp; */ 8569: /* yp2=modf((yp1*12),&yp); */ 8570: /* mintmean=yp; */ 8571: /* yp1=modf((yp2*30.5),&yp); */ 8572: /* jintmean=yp; */ 8573: /* if(jintmean==0) jintmean=1; */ 8574: /* if(mintmean==0) mintmean=1; */ 8575: 8576: 8577: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ 8578: /* date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); */ 8579: /* date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); */ 8580: i1=pow(2,cptcoveff); 8581: if (cptcovn < 1){i1=1;} 8582: 8583: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2); 8584: 8585: fprintf(ficresf,"#****** Routine prevforecast **\n"); 8586: 8587: /* if (h==(int)(YEARM*yearp)){ */ 8588: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 8589: for(k=1; k<=i1;k++){ 8590: if(i1 != 1 && TKresult[nres]!= k) 8591: continue; 8592: if(invalidvarcomb[k]){ 8593: printf("\nCombination (%d) projection ignored because no cases \n",k); 8594: continue; 8595: } 8596: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#"); 8597: for(j=1;j<=cptcoveff;j++) { 8598: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8599: } 8600: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 8601: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 8602: } 8603: fprintf(ficresf," yearproj age"); 8604: for(j=1; j<=nlstate+ndeath;j++){ 8605: for(i=1; i<=nlstate;i++) 8606: fprintf(ficresf," p%d%d",i,j); 8607: fprintf(ficresf," wp.%d",j); 8608: } 8609: for (yearp=0; yearp<=(anprojf-anprojd);yearp +=stepsize) { 8610: fprintf(ficresf,"\n"); 8611: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jprojd,mprojd,anprojd+yearp); 8612: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */ 8613: for (agec=fage; agec>=(bage); agec--){ 8614: nhstepm=(int) rint((agelim-agec)*YEARM/stepm); 8615: nhstepm = nhstepm/hstepm; 8616: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 8617: oldm=oldms;savm=savms; 8618: /* We compute pii at age agec over nhstepm);*/ 8619: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k,nres); 8620: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */ 8621: for (h=0; h<=nhstepm; h++){ 8622: if (h*hstepm/YEARM*stepm ==yearp) { 8623: break; 8624: } 8625: } 8626: fprintf(ficresf,"\n"); 8627: for(j=1;j<=cptcoveff;j++) 8628: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8629: fprintf(ficresf,"%.f %.f ",anprojd+yearp,agec+h*hstepm/YEARM*stepm); 8630: 8631: for(j=1; j<=nlstate+ndeath;j++) { 8632: ppij=0.; 8633: for(i=1; i<=nlstate;i++) { 8634: if (mobilav>=1) 8635: ppij=ppij+p3mat[i][j][h]*prev[(int)agec][i][k]; 8636: else { /* even if mobilav==-1 we use mobaverage, probs may not sums to 1 */ 8637: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; 8638: } 8639: fprintf(ficresf," %.3f", p3mat[i][j][h]); 8640: } /* end i */ 8641: fprintf(ficresf," %.3f", ppij); 8642: }/* end j */ 8643: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 8644: } /* end agec */ 8645: /* diffyear=(int) anproj1+yearp-ageminpar-1; */ 8646: /*printf("Prevforecast %d+%d-%d=diffyear=%d\n",(int) anproj1, (int)yearp,(int)ageminpar,(int) anproj1-(int)ageminpar);*/ 8647: } /* end yearp */ 8648: } /* end k */ 8649: 8650: fclose(ficresf); 8651: printf("End of Computing forecasting \n"); 8652: fprintf(ficlog,"End of Computing forecasting\n"); 8653: 8654: } 8655: 8656: /************** Back Forecasting ******************/ 8657: /* 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){ */ 8658: 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){ 8659: /* back1, year, month, day of starting backprojection 8660: agemin, agemax range of age 8661: dateprev1 dateprev2 range of dates during which prevalence is computed 8662: anback2 year of end of backprojection (same day and month as back1). 8663: prevacurrent and prev are prevalences. 8664: */ 8665: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1, k4, nres=0; 8666: double agec; /* generic age */ 8667: double agelim, ppij, ppi, yp,yp1,yp2; /* ,jintmean,mintmean,aintmean;*/ 8668: double *popeffectif,*popcount; 8669: double ***p3mat; 8670: /* double ***mobaverage; */ 8671: char fileresfb[FILENAMELENGTH]; 8672: 8673: agelim=AGEINF; 8674: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people 8675: in each health status at the date of interview (if between dateprev1 and dateprev2). 8676: We still use firstpass and lastpass as another selection. 8677: */ 8678: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */ 8679: /* firstpass, lastpass, stepm, weightopt, model); */ 8680: 8681: /*Do we need to compute prevalence again?*/ 8682: 8683: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */ 8684: 8685: strcpy(fileresfb,"FB_"); 8686: strcat(fileresfb,fileresu); 8687: if((ficresfb=fopen(fileresfb,"w"))==NULL) { 8688: printf("Problem with back forecast resultfile: %s\n", fileresfb); 8689: fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); 8690: } 8691: printf("\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb); 8692: fprintf(ficlog,"\nComputing back forecasting: result on file '%s', please wait... \n", fileresfb); 8693: 8694: if (cptcoveff==0) ncodemax[cptcoveff]=1; 8695: 8696: 8697: stepsize=(int) (stepm+YEARM-1)/YEARM; 8698: if (stepm<=12) stepsize=1; 8699: if(estepm < stepm){ 8700: printf ("Problem %d lower than %d\n",estepm, stepm); 8701: } 8702: else{ 8703: hstepm=estepm; 8704: } 8705: if(estepm >= stepm){ /* Yes every two year */ 8706: stepsize=2; 8707: } 8708: 8709: hstepm=hstepm/stepm; 8710: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */ 8711: /* fractional in yp1 *\/ */ 8712: /* aintmean=yp; */ 8713: /* yp2=modf((yp1*12),&yp); */ 8714: /* mintmean=yp; */ 8715: /* yp1=modf((yp2*30.5),&yp); */ 8716: /* jintmean=yp; */ 8717: /* if(jintmean==0) jintmean=1; */ 8718: /* if(mintmean==0) jintmean=1; */ 8719: 8720: i1=pow(2,cptcoveff); 8721: if (cptcovn < 1){i1=1;} 8722: 8723: fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2); 8724: printf("# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jintmean,mintmean,aintmean,dateintmean,dateprev1,dateprev2); 8725: 8726: fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); 8727: 8728: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 8729: for(k=1; k<=i1;k++){ 8730: if(i1 != 1 && TKresult[nres]!= k) 8731: continue; 8732: if(invalidvarcomb[k]){ 8733: printf("\nCombination (%d) projection ignored because no cases \n",k); 8734: continue; 8735: } 8736: fprintf(ficresfb,"\n#****** hbijx=probability over h years, hb.jx is weighted by observed prev \n#"); 8737: for(j=1;j<=cptcoveff;j++) { 8738: fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8739: } 8740: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 8741: fprintf(ficresf," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 8742: } 8743: fprintf(ficresfb," yearbproj age"); 8744: for(j=1; j<=nlstate+ndeath;j++){ 8745: for(i=1; i<=nlstate;i++) 8746: fprintf(ficresfb," b%d%d",i,j); 8747: fprintf(ficresfb," b.%d",j); 8748: } 8749: for (yearp=0; yearp>=(anbackf-anbackd);yearp -=stepsize) { 8750: /* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { */ 8751: fprintf(ficresfb,"\n"); 8752: fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jbackd,mbackd,anbackd+yearp); 8753: /* printf("\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */ 8754: /* for (agec=bage; agec<=agemax-1; agec++){ /\* testing *\/ */ 8755: for (agec=bage; agec<=fage; agec++){ /* testing */ 8756: /* We compute bij at age agec over nhstepm, nhstepm decreases when agec increases because of agemax;*/ 8757: nhstepm=(int) (agec-agelim) *YEARM/stepm;/* nhstepm=(int) rint((agec-agelim)*YEARM/stepm);*/ 8758: nhstepm = nhstepm/hstepm; 8759: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 8760: oldm=oldms;savm=savms; 8761: /* computes hbxij at age agec over 1 to nhstepm */ 8762: /* printf("####prevbackforecast debug agec=%.2f nhstepm=%d\n",agec, nhstepm);fflush(stdout); */ 8763: hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm, k, nres); 8764: /* hpxij(p3mat,nhstepm,agec,hstepm,p, nlstate,stepm,oldm,savm, k,nres); */ 8765: /* Then we print p3mat for h corresponding to the right agec+h*stepms=yearp */ 8766: /* printf(" agec=%.2f\n",agec);fflush(stdout); */ 8767: for (h=0; h<=nhstepm; h++){ 8768: if (h*hstepm/YEARM*stepm ==-yearp) { 8769: break; 8770: } 8771: } 8772: fprintf(ficresfb,"\n"); 8773: for(j=1;j<=cptcoveff;j++) 8774: fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8775: fprintf(ficresfb,"%.f %.f ",anbackd+yearp,agec-h*hstepm/YEARM*stepm); 8776: for(i=1; i<=nlstate+ndeath;i++) { 8777: ppij=0.;ppi=0.; 8778: for(j=1; j<=nlstate;j++) { 8779: /* if (mobilav==1) */ 8780: ppij=ppij+p3mat[i][j][h]*prevacurrent[(int)agec][j][k]; 8781: ppi=ppi+prevacurrent[(int)agec][j][k]; 8782: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][j][k]; */ 8783: /* ppi=ppi+mobaverage[(int)agec][j][k]; */ 8784: /* else { */ 8785: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][k]; */ 8786: /* } */ 8787: fprintf(ficresfb," %.3f", p3mat[i][j][h]); 8788: } /* end j */ 8789: if(ppi <0.99){ 8790: printf("Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi); 8791: fprintf(ficlog,"Error in prevbackforecast, prevalence doesn't sum to 1 for state %d: %3f\n",i, ppi); 8792: } 8793: fprintf(ficresfb," %.3f", ppij); 8794: }/* end j */ 8795: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 8796: } /* end agec */ 8797: } /* end yearp */ 8798: } /* end k */ 8799: 8800: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */ 8801: 8802: fclose(ficresfb); 8803: printf("End of Computing Back forecasting \n"); 8804: fprintf(ficlog,"End of Computing Back forecasting\n"); 8805: 8806: } 8807: 8808: /* Variance of prevalence limit: varprlim */ 8809: 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){ 8810: /*------- Variance of forward period (stable) prevalence------*/ 8811: 8812: char fileresvpl[FILENAMELENGTH]; 8813: FILE *ficresvpl; 8814: double **oldm, **savm; 8815: double **varpl; /* Variances of prevalence limits by age */ 8816: int i1, k, nres, j ; 8817: 8818: strcpy(fileresvpl,"VPL_"); 8819: strcat(fileresvpl,fileresu); 8820: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) { 8821: printf("Problem with variance of forward period (stable) prevalence resultfile: %s\n", fileresvpl); 8822: exit(0); 8823: } 8824: printf("Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout); 8825: fprintf(ficlog, "Computing Variance-covariance of forward period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog); 8826: 8827: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){ 8828: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/ 8829: 8830: i1=pow(2,cptcoveff); 8831: if (cptcovn < 1){i1=1;} 8832: 8833: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 8834: for(k=1; k<=i1;k++){ 8835: if(i1 != 1 && TKresult[nres]!= k) 8836: continue; 8837: fprintf(ficresvpl,"\n#****** "); 8838: printf("\n#****** "); 8839: fprintf(ficlog,"\n#****** "); 8840: for(j=1;j<=cptcoveff;j++) { 8841: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8842: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8843: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8844: } 8845: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 8846: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 8847: fprintf(ficresvpl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 8848: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 8849: } 8850: fprintf(ficresvpl,"******\n"); 8851: printf("******\n"); 8852: fprintf(ficlog,"******\n"); 8853: 8854: varpl=matrix(1,nlstate,(int) bage, (int) fage); 8855: oldm=oldms;savm=savms; 8856: varprevlim(fileresvpl, ficresvpl, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, ncvyearp, k, strstart, nres); 8857: free_matrix(varpl,1,nlstate,(int) bage, (int)fage); 8858: /*}*/ 8859: } 8860: 8861: fclose(ficresvpl); 8862: printf("done variance-covariance of forward period prevalence\n");fflush(stdout); 8863: fprintf(ficlog,"done variance-covariance of forward period prevalence\n");fflush(ficlog); 8864: 8865: } 8866: /* Variance of back prevalence: varbprlim */ 8867: 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){ 8868: /*------- Variance of back (stable) prevalence------*/ 8869: 8870: char fileresvbl[FILENAMELENGTH]; 8871: FILE *ficresvbl; 8872: 8873: double **oldm, **savm; 8874: double **varbpl; /* Variances of back prevalence limits by age */ 8875: int i1, k, nres, j ; 8876: 8877: strcpy(fileresvbl,"VBL_"); 8878: strcat(fileresvbl,fileresu); 8879: if((ficresvbl=fopen(fileresvbl,"w"))==NULL) { 8880: printf("Problem with variance of back (stable) prevalence resultfile: %s\n", fileresvbl); 8881: exit(0); 8882: } 8883: printf("Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(stdout); 8884: fprintf(ficlog, "Computing Variance-covariance of back (stable) prevalence: file '%s' ...", fileresvbl);fflush(ficlog); 8885: 8886: 8887: i1=pow(2,cptcoveff); 8888: if (cptcovn < 1){i1=1;} 8889: 8890: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 8891: for(k=1; k<=i1;k++){ 8892: if(i1 != 1 && TKresult[nres]!= k) 8893: continue; 8894: fprintf(ficresvbl,"\n#****** "); 8895: printf("\n#****** "); 8896: fprintf(ficlog,"\n#****** "); 8897: for(j=1;j<=cptcoveff;j++) { 8898: fprintf(ficresvbl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8899: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8900: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 8901: } 8902: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 8903: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 8904: fprintf(ficresvbl," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 8905: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 8906: } 8907: fprintf(ficresvbl,"******\n"); 8908: printf("******\n"); 8909: fprintf(ficlog,"******\n"); 8910: 8911: varbpl=matrix(1,nlstate,(int) bage, (int) fage); 8912: oldm=oldms;savm=savms; 8913: 8914: varbrevlim(fileresvbl, ficresvbl, varbpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, bprlim, ftolpl, mobilavproj, ncvyearp, k, strstart, nres); 8915: free_matrix(varbpl,1,nlstate,(int) bage, (int)fage); 8916: /*}*/ 8917: } 8918: 8919: fclose(ficresvbl); 8920: printf("done variance-covariance of back prevalence\n");fflush(stdout); 8921: fprintf(ficlog,"done variance-covariance of back prevalence\n");fflush(ficlog); 8922: 8923: } /* End of varbprlim */ 8924: 8925: /************** Forecasting *****not tested NB*************/ 8926: /* 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){ */ 8927: 8928: /* int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h; */ 8929: /* int *popage; */ 8930: /* double calagedatem, agelim, kk1, kk2; */ 8931: /* double *popeffectif,*popcount; */ 8932: /* double ***p3mat,***tabpop,***tabpopprev; */ 8933: /* /\* double ***mobaverage; *\/ */ 8934: /* char filerespop[FILENAMELENGTH]; */ 8935: 8936: /* tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */ 8937: /* tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */ 8938: /* agelim=AGESUP; */ 8939: /* calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM; */ 8940: 8941: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */ 8942: 8943: 8944: /* strcpy(filerespop,"POP_"); */ 8945: /* strcat(filerespop,fileresu); */ 8946: /* if((ficrespop=fopen(filerespop,"w"))==NULL) { */ 8947: /* printf("Problem with forecast resultfile: %s\n", filerespop); */ 8948: /* fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop); */ 8949: /* } */ 8950: /* printf("Computing forecasting: result on file '%s' \n", filerespop); */ 8951: /* fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop); */ 8952: 8953: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */ 8954: 8955: /* /\* if (mobilav!=0) { *\/ */ 8956: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */ 8957: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */ 8958: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */ 8959: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */ 8960: /* /\* } *\/ */ 8961: /* /\* } *\/ */ 8962: 8963: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */ 8964: /* if (stepm<=12) stepsize=1; */ 8965: 8966: /* agelim=AGESUP; */ 8967: 8968: /* hstepm=1; */ 8969: /* hstepm=hstepm/stepm; */ 8970: 8971: /* if (popforecast==1) { */ 8972: /* if((ficpop=fopen(popfile,"r"))==NULL) { */ 8973: /* printf("Problem with population file : %s\n",popfile);exit(0); */ 8974: /* fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0); */ 8975: /* } */ 8976: /* popage=ivector(0,AGESUP); */ 8977: /* popeffectif=vector(0,AGESUP); */ 8978: /* popcount=vector(0,AGESUP); */ 8979: 8980: /* i=1; */ 8981: /* while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1; */ 8982: 8983: /* imx=i; */ 8984: /* for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i]; */ 8985: /* } */ 8986: 8987: /* for(cptcov=1,k=0;cptcov<=i2;cptcov++){ */ 8988: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */ 8989: /* k=k+1; */ 8990: /* fprintf(ficrespop,"\n#******"); */ 8991: /* for(j=1;j<=cptcoveff;j++) { */ 8992: /* fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ 8993: /* } */ 8994: /* fprintf(ficrespop,"******\n"); */ 8995: /* fprintf(ficrespop,"# Age"); */ 8996: /* for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j); */ 8997: /* if (popforecast==1) fprintf(ficrespop," [Population]"); */ 8998: 8999: /* for (cpt=0; cpt<=0;cpt++) { */ 9000: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */ 9001: 9002: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */ 9003: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */ 9004: /* nhstepm = nhstepm/hstepm; */ 9005: 9006: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */ 9007: /* oldm=oldms;savm=savms; */ 9008: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */ 9009: 9010: /* for (h=0; h<=nhstepm; h++){ */ 9011: /* if (h==(int) (calagedatem+YEARM*cpt)) { */ 9012: /* fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */ 9013: /* } */ 9014: /* for(j=1; j<=nlstate+ndeath;j++) { */ 9015: /* kk1=0.;kk2=0; */ 9016: /* for(i=1; i<=nlstate;i++) { */ 9017: /* if (mobilav==1) */ 9018: /* kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod]; */ 9019: /* else { */ 9020: /* kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod]; */ 9021: /* } */ 9022: /* } */ 9023: /* if (h==(int)(calagedatem+12*cpt)){ */ 9024: /* tabpop[(int)(agedeb)][j][cptcod]=kk1; */ 9025: /* /\*fprintf(ficrespop," %.3f", kk1); */ 9026: /* if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*\/ */ 9027: /* } */ 9028: /* } */ 9029: /* for(i=1; i<=nlstate;i++){ */ 9030: /* kk1=0.; */ 9031: /* for(j=1; j<=nlstate;j++){ */ 9032: /* kk1= kk1+tabpop[(int)(agedeb)][j][cptcod]; */ 9033: /* } */ 9034: /* tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)]; */ 9035: /* } */ 9036: 9037: /* if (h==(int)(calagedatem+12*cpt)) */ 9038: /* for(j=1; j<=nlstate;j++) */ 9039: /* fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]); */ 9040: /* } */ 9041: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */ 9042: /* } */ 9043: /* } */ 9044: 9045: /* /\******\/ */ 9046: 9047: /* for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) { */ 9048: /* fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt); */ 9049: /* for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){ */ 9050: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); */ 9051: /* nhstepm = nhstepm/hstepm; */ 9052: 9053: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */ 9054: /* oldm=oldms;savm=savms; */ 9055: /* hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */ 9056: /* for (h=0; h<=nhstepm; h++){ */ 9057: /* if (h==(int) (calagedatem+YEARM*cpt)) { */ 9058: /* fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm); */ 9059: /* } */ 9060: /* for(j=1; j<=nlstate+ndeath;j++) { */ 9061: /* kk1=0.;kk2=0; */ 9062: /* for(i=1; i<=nlstate;i++) { */ 9063: /* kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod]; */ 9064: /* } */ 9065: /* if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1); */ 9066: /* } */ 9067: /* } */ 9068: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */ 9069: /* } */ 9070: /* } */ 9071: /* } */ 9072: /* } */ 9073: 9074: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */ 9075: 9076: /* if (popforecast==1) { */ 9077: /* free_ivector(popage,0,AGESUP); */ 9078: /* free_vector(popeffectif,0,AGESUP); */ 9079: /* free_vector(popcount,0,AGESUP); */ 9080: /* } */ 9081: /* free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */ 9082: /* free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */ 9083: /* fclose(ficrespop); */ 9084: /* } /\* End of popforecast *\/ */ 9085: 9086: int fileappend(FILE *fichier, char *optionfich) 9087: { 9088: if((fichier=fopen(optionfich,"a"))==NULL) { 9089: printf("Problem with file: %s\n", optionfich); 9090: fprintf(ficlog,"Problem with file: %s\n", optionfich); 9091: return (0); 9092: } 9093: fflush(fichier); 9094: return (1); 9095: } 9096: 9097: 9098: /**************** function prwizard **********************/ 9099: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo) 9100: { 9101: 9102: /* Wizard to print covariance matrix template */ 9103: 9104: char ca[32], cb[32]; 9105: int i,j, k, li, lj, lk, ll, jj, npar, itimes; 9106: int numlinepar; 9107: 9108: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); 9109: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); 9110: for(i=1; i <=nlstate; i++){ 9111: jj=0; 9112: for(j=1; j <=nlstate+ndeath; j++){ 9113: if(j==i) continue; 9114: jj++; 9115: /*ca[0]= k+'a'-1;ca[1]='\0';*/ 9116: printf("%1d%1d",i,j); 9117: fprintf(ficparo,"%1d%1d",i,j); 9118: for(k=1; k<=ncovmodel;k++){ 9119: /* printf(" %lf",param[i][j][k]); */ 9120: /* fprintf(ficparo," %lf",param[i][j][k]); */ 9121: printf(" 0."); 9122: fprintf(ficparo," 0."); 9123: } 9124: printf("\n"); 9125: fprintf(ficparo,"\n"); 9126: } 9127: } 9128: printf("# Scales (for hessian or gradient estimation)\n"); 9129: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n"); 9130: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/ 9131: for(i=1; i <=nlstate; i++){ 9132: jj=0; 9133: for(j=1; j <=nlstate+ndeath; j++){ 9134: if(j==i) continue; 9135: jj++; 9136: fprintf(ficparo,"%1d%1d",i,j); 9137: printf("%1d%1d",i,j); 9138: fflush(stdout); 9139: for(k=1; k<=ncovmodel;k++){ 9140: /* printf(" %le",delti3[i][j][k]); */ 9141: /* fprintf(ficparo," %le",delti3[i][j][k]); */ 9142: printf(" 0."); 9143: fprintf(ficparo," 0."); 9144: } 9145: numlinepar++; 9146: printf("\n"); 9147: fprintf(ficparo,"\n"); 9148: } 9149: } 9150: printf("# Covariance matrix\n"); 9151: /* # 121 Var(a12)\n\ */ 9152: /* # 122 Cov(b12,a12) Var(b12)\n\ */ 9153: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */ 9154: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */ 9155: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */ 9156: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */ 9157: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */ 9158: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */ 9159: fflush(stdout); 9160: fprintf(ficparo,"# Covariance matrix\n"); 9161: /* # 121 Var(a12)\n\ */ 9162: /* # 122 Cov(b12,a12) Var(b12)\n\ */ 9163: /* # ...\n\ */ 9164: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */ 9165: 9166: for(itimes=1;itimes<=2;itimes++){ 9167: jj=0; 9168: for(i=1; i <=nlstate; i++){ 9169: for(j=1; j <=nlstate+ndeath; j++){ 9170: if(j==i) continue; 9171: for(k=1; k<=ncovmodel;k++){ 9172: jj++; 9173: ca[0]= k+'a'-1;ca[1]='\0'; 9174: if(itimes==1){ 9175: printf("#%1d%1d%d",i,j,k); 9176: fprintf(ficparo,"#%1d%1d%d",i,j,k); 9177: }else{ 9178: printf("%1d%1d%d",i,j,k); 9179: fprintf(ficparo,"%1d%1d%d",i,j,k); 9180: /* printf(" %.5le",matcov[i][j]); */ 9181: } 9182: ll=0; 9183: for(li=1;li <=nlstate; li++){ 9184: for(lj=1;lj <=nlstate+ndeath; lj++){ 9185: if(lj==li) continue; 9186: for(lk=1;lk<=ncovmodel;lk++){ 9187: ll++; 9188: if(ll<=jj){ 9189: cb[0]= lk +'a'-1;cb[1]='\0'; 9190: if(ll<jj){ 9191: if(itimes==1){ 9192: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj); 9193: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj); 9194: }else{ 9195: printf(" 0."); 9196: fprintf(ficparo," 0."); 9197: } 9198: }else{ 9199: if(itimes==1){ 9200: printf(" Var(%s%1d%1d)",ca,i,j); 9201: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j); 9202: }else{ 9203: printf(" 0."); 9204: fprintf(ficparo," 0."); 9205: } 9206: } 9207: } 9208: } /* end lk */ 9209: } /* end lj */ 9210: } /* end li */ 9211: printf("\n"); 9212: fprintf(ficparo,"\n"); 9213: numlinepar++; 9214: } /* end k*/ 9215: } /*end j */ 9216: } /* end i */ 9217: } /* end itimes */ 9218: 9219: } /* end of prwizard */ 9220: /******************* Gompertz Likelihood ******************************/ 9221: double gompertz(double x[]) 9222: { 9223: double A=0.0,B=0.,L=0.0,sump=0.,num=0.; 9224: int i,n=0; /* n is the size of the sample */ 9225: 9226: for (i=1;i<=imx ; i++) { 9227: sump=sump+weight[i]; 9228: /* sump=sump+1;*/ 9229: num=num+1; 9230: } 9231: L=0.0; 9232: /* agegomp=AGEGOMP; */ 9233: /* for (i=0; i<=imx; i++) 9234: 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]);*/ 9235: 9236: for (i=1;i<=imx ; i++) { 9237: /* mu(a)=mu(agecomp)*exp(teta*(age-agegomp)) 9238: mu(a)=x[1]*exp(x[2]*(age-agegomp)); x[1] and x[2] are per year. 9239: * L= Product mu(agedeces)exp(-\int_ageexam^agedc mu(u) du ) for a death between agedc (in month) 9240: * and agedc +1 month, cens[i]=0: log(x[1]/YEARM) 9241: * + 9242: * exp(-\int_ageexam^agecens mu(u) du ) when censored, cens[i]=1 9243: */ 9244: if (wav[i] > 1 || agedc[i] < AGESUP) { 9245: if (cens[i] == 1){ 9246: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp))); 9247: } else if (cens[i] == 0){ 9248: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp))) 9249: +log(x[1]/YEARM) +x[2]*(agedc[i]-agegomp)+log(YEARM); 9250: } else 9251: printf("Gompertz cens[%d] neither 1 nor 0\n",i); 9252: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */ 9253: L=L+A*weight[i]; 9254: /* 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]);*/ 9255: } 9256: } 9257: 9258: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/ 9259: 9260: return -2*L*num/sump; 9261: } 9262: 9263: #ifdef GSL 9264: /******************* Gompertz_f Likelihood ******************************/ 9265: double gompertz_f(const gsl_vector *v, void *params) 9266: { 9267: double A=0.,B=0.,LL=0.0,sump=0.,num=0.; 9268: double *x= (double *) v->data; 9269: int i,n=0; /* n is the size of the sample */ 9270: 9271: for (i=0;i<=imx-1 ; i++) { 9272: sump=sump+weight[i]; 9273: /* sump=sump+1;*/ 9274: num=num+1; 9275: } 9276: 9277: 9278: /* for (i=0; i<=imx; i++) 9279: 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]);*/ 9280: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]); 9281: for (i=1;i<=imx ; i++) 9282: { 9283: if (cens[i] == 1 && wav[i]>1) 9284: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp))); 9285: 9286: if (cens[i] == 0 && wav[i]>1) 9287: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp))) 9288: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM); 9289: 9290: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */ 9291: if (wav[i] > 1 ) { /* ??? */ 9292: LL=LL+A*weight[i]; 9293: /* 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]);*/ 9294: } 9295: } 9296: 9297: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/ 9298: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump); 9299: 9300: return -2*LL*num/sump; 9301: } 9302: #endif 9303: 9304: /******************* Printing html file ***********/ 9305: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \ 9306: int lastpass, int stepm, int weightopt, char model[],\ 9307: int imx, double p[],double **matcov,double agemortsup){ 9308: int i,k; 9309: 9310: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>"); 9311: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp); 9312: for (i=1;i<=2;i++) 9313: 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])); 9314: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">"); 9315: fprintf(fichtm,"</ul>"); 9316: 9317: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>"); 9318: 9319: 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>"); 9320: 9321: for (k=agegomp;k<(agemortsup-2);k++) 9322: 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]); 9323: 9324: 9325: fflush(fichtm); 9326: } 9327: 9328: /******************* Gnuplot file **************/ 9329: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){ 9330: 9331: char dirfileres[132],optfileres[132]; 9332: 9333: int ng; 9334: 9335: 9336: /*#ifdef windows */ 9337: fprintf(ficgp,"cd \"%s\" \n",pathc); 9338: /*#endif */ 9339: 9340: 9341: strcpy(dirfileres,optionfilefiname); 9342: strcpy(optfileres,"vpl"); 9343: fprintf(ficgp,"set out \"graphmort.svg\"\n "); 9344: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n "); 9345: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n"); 9346: /* fprintf(ficgp, "set size 0.65,0.65\n"); */ 9347: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp); 9348: 9349: } 9350: 9351: int readdata(char datafile[], int firstobs, int lastobs, int *imax) 9352: { 9353: 9354: /*-------- data file ----------*/ 9355: FILE *fic; 9356: char dummy[]=" "; 9357: int i=0, j=0, n=0, iv=0, v; 9358: int lstra; 9359: int linei, month, year,iout; 9360: int noffset=0; /* This is the offset if BOM data file */ 9361: char line[MAXLINE], linetmp[MAXLINE]; 9362: char stra[MAXLINE], strb[MAXLINE]; 9363: char *stratrunc; 9364: 9365: DummyV=ivector(1,NCOVMAX); /* 1 to 3 */ 9366: FixedV=ivector(1,NCOVMAX); /* 1 to 3 */ 9367: 9368: for(v=1; v <=ncovcol;v++){ 9369: DummyV[v]=0; 9370: FixedV[v]=0; 9371: } 9372: for(v=ncovcol+1; v <=ncovcol+nqv;v++){ 9373: DummyV[v]=1; 9374: FixedV[v]=0; 9375: } 9376: for(v=ncovcol+nqv+1; v <=ncovcol+nqv+ntv;v++){ 9377: DummyV[v]=0; 9378: FixedV[v]=1; 9379: } 9380: for(v=ncovcol+nqv+ntv+1; v <=ncovcol+nqv+ntv+nqtv;v++){ 9381: DummyV[v]=1; 9382: FixedV[v]=1; 9383: } 9384: for(v=1; v <=ncovcol+nqv+ntv+nqtv;v++){ 9385: printf("Covariate type in the data: V%d, DummyV(V%d)=%d, FixedV(V%d)=%d\n",v,v,DummyV[v],v,FixedV[v]); 9386: 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]); 9387: } 9388: 9389: if((fic=fopen(datafile,"r"))==NULL) { 9390: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout); 9391: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1; 9392: } 9393: 9394: /* Is it a BOM UTF-8 Windows file? */ 9395: /* First data line */ 9396: linei=0; 9397: while(fgets(line, MAXLINE, fic)) { 9398: noffset=0; 9399: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */ 9400: { 9401: noffset=noffset+3; 9402: printf("# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile);fflush(stdout); 9403: fprintf(ficlog,"# Data file '%s' is an UTF8 BOM file, please convert to UTF8 or ascii file and rerun.\n",datafile); 9404: fflush(ficlog); return 1; 9405: } 9406: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/ 9407: else if( line[0] == (char)0xFF && line[1] == (char)0xFE) 9408: { 9409: noffset=noffset+2; 9410: 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); 9411: 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); 9412: fflush(ficlog); return 1; 9413: } 9414: else if( line[0] == 0 && line[1] == 0) 9415: { 9416: if( line[2] == (char)0xFE && line[3] == (char)0xFF){ 9417: noffset=noffset+4; 9418: 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); 9419: 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); 9420: fflush(ficlog); return 1; 9421: } 9422: } else{ 9423: ;/*printf(" Not a BOM file\n");*/ 9424: } 9425: /* If line starts with a # it is a comment */ 9426: if (line[noffset] == '#') { 9427: linei=linei+1; 9428: break; 9429: }else{ 9430: break; 9431: } 9432: } 9433: fclose(fic); 9434: if((fic=fopen(datafile,"r"))==NULL) { 9435: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout); 9436: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1; 9437: } 9438: /* Not a Bom file */ 9439: 9440: i=1; 9441: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) { 9442: linei=linei+1; 9443: for(j=strlen(line); j>=0;j--){ /* Untabifies line */ 9444: if(line[j] == '\t') 9445: line[j] = ' '; 9446: } 9447: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){ 9448: ; 9449: }; 9450: line[j+1]=0; /* Trims blanks at end of line */ 9451: if(line[0]=='#'){ 9452: fprintf(ficlog,"Comment line\n%s\n",line); 9453: printf("Comment line\n%s\n",line); 9454: continue; 9455: } 9456: trimbb(linetmp,line); /* Trims multiple blanks in line */ 9457: strcpy(line, linetmp); 9458: 9459: /* Loops on waves */ 9460: for (j=maxwav;j>=1;j--){ 9461: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */ 9462: cutv(stra, strb, line, ' '); 9463: if(strb[0]=='.') { /* Missing value */ 9464: lval=-1; 9465: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */ 9466: cotvar[j][ntv+iv][i]=-1; /* For performance reasons */ 9467: if(isalpha(strb[1])) { /* .m or .d Really Missing value */ 9468: 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); 9469: 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); 9470: return 1; 9471: } 9472: }else{ 9473: errno=0; 9474: /* what_kind_of_number(strb); */ 9475: dval=strtod(strb,&endptr); 9476: /* if( strb[0]=='\0' || (*endptr != '\0')){ */ 9477: /* if(strb != endptr && *endptr == '\0') */ 9478: /* dval=dlval; */ 9479: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */ 9480: if( strb[0]=='\0' || (*endptr != '\0')){ 9481: 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); 9482: 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); 9483: return 1; 9484: } 9485: cotqvar[j][iv][i]=dval; 9486: cotvar[j][ntv+iv][i]=dval; 9487: } 9488: strcpy(line,stra); 9489: }/* end loop ntqv */ 9490: 9491: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */ 9492: cutv(stra, strb, line, ' '); 9493: if(strb[0]=='.') { /* Missing value */ 9494: lval=-1; 9495: }else{ 9496: errno=0; 9497: lval=strtol(strb,&endptr,10); 9498: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/ 9499: if( strb[0]=='\0' || (*endptr != '\0')){ 9500: 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); 9501: 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); 9502: return 1; 9503: } 9504: } 9505: if(lval <-1 || lval >1){ 9506: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \ 9507: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \ 9508: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \ 9509: For example, for multinomial values like 1, 2 and 3,\n \ 9510: build V1=0 V2=0 for the reference value (1),\n \ 9511: V1=1 V2=0 for (2) \n \ 9512: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \ 9513: output of IMaCh is often meaningless.\n \ 9514: Exiting.\n",lval,linei, i,line,j); 9515: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \ 9516: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \ 9517: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \ 9518: For example, for multinomial values like 1, 2 and 3,\n \ 9519: build V1=0 V2=0 for the reference value (1),\n \ 9520: V1=1 V2=0 for (2) \n \ 9521: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \ 9522: output of IMaCh is often meaningless.\n \ 9523: Exiting.\n",lval,linei, i,line,j);fflush(ficlog); 9524: return 1; 9525: } 9526: cotvar[j][iv][i]=(double)(lval); 9527: strcpy(line,stra); 9528: }/* end loop ntv */ 9529: 9530: /* Statuses at wave */ 9531: cutv(stra, strb, line, ' '); 9532: if(strb[0]=='.') { /* Missing value */ 9533: lval=-1; 9534: }else{ 9535: errno=0; 9536: lval=strtol(strb,&endptr,10); 9537: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/ 9538: if( strb[0]=='\0' || (*endptr != '\0')){ 9539: 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); 9540: 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); 9541: return 1; 9542: } 9543: } 9544: 9545: s[j][i]=lval; 9546: 9547: /* Date of Interview */ 9548: strcpy(line,stra); 9549: cutv(stra, strb,line,' '); 9550: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){ 9551: } 9552: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){ 9553: month=99; 9554: year=9999; 9555: }else{ 9556: 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); 9557: 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); 9558: return 1; 9559: } 9560: anint[j][i]= (double) year; 9561: mint[j][i]= (double)month; 9562: /* if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ */ 9563: /* 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]); */ 9564: /* 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]); */ 9565: /* } */ 9566: strcpy(line,stra); 9567: } /* End loop on waves */ 9568: 9569: /* Date of death */ 9570: cutv(stra, strb,line,' '); 9571: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){ 9572: } 9573: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){ 9574: month=99; 9575: year=9999; 9576: }else{ 9577: 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); 9578: 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); 9579: return 1; 9580: } 9581: andc[i]=(double) year; 9582: moisdc[i]=(double) month; 9583: strcpy(line,stra); 9584: 9585: /* Date of birth */ 9586: cutv(stra, strb,line,' '); 9587: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){ 9588: } 9589: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){ 9590: month=99; 9591: year=9999; 9592: }else{ 9593: 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); 9594: 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); 9595: return 1; 9596: } 9597: if (year==9999) { 9598: 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); 9599: 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); 9600: return 1; 9601: 9602: } 9603: annais[i]=(double)(year); 9604: moisnais[i]=(double)(month); 9605: for (j=1;j<=maxwav;j++){ 9606: if( (int)anint[j][i]+ (int)(mint[j][i])/12. < (int) (moisnais[i]/12.+annais[i])){ 9607: 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]); 9608: 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]); 9609: } 9610: } 9611: 9612: strcpy(line,stra); 9613: 9614: /* Sample weight */ 9615: cutv(stra, strb,line,' '); 9616: errno=0; 9617: dval=strtod(strb,&endptr); 9618: if( strb[0]=='\0' || (*endptr != '\0')){ 9619: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei); 9620: 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); 9621: fflush(ficlog); 9622: return 1; 9623: } 9624: weight[i]=dval; 9625: strcpy(line,stra); 9626: 9627: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */ 9628: cutv(stra, strb, line, ' '); 9629: if(strb[0]=='.') { /* Missing value */ 9630: lval=-1; 9631: coqvar[iv][i]=NAN; 9632: covar[ncovcol+iv][i]=NAN; /* including qvar in standard covar for performance reasons */ 9633: }else{ 9634: errno=0; 9635: /* what_kind_of_number(strb); */ 9636: dval=strtod(strb,&endptr); 9637: /* if(strb != endptr && *endptr == '\0') */ 9638: /* dval=dlval; */ 9639: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */ 9640: if( strb[0]=='\0' || (*endptr != '\0')){ 9641: 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); 9642: 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); 9643: return 1; 9644: } 9645: coqvar[iv][i]=dval; 9646: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */ 9647: } 9648: strcpy(line,stra); 9649: }/* end loop nqv */ 9650: 9651: /* Covariate values */ 9652: for (j=ncovcol;j>=1;j--){ 9653: cutv(stra, strb,line,' '); 9654: if(strb[0]=='.') { /* Missing covariate value */ 9655: lval=-1; 9656: }else{ 9657: errno=0; 9658: lval=strtol(strb,&endptr,10); 9659: if( strb[0]=='\0' || (*endptr != '\0')){ 9660: 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); 9661: 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); 9662: return 1; 9663: } 9664: } 9665: if(lval <-1 || lval >1){ 9666: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \ 9667: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \ 9668: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \ 9669: For example, for multinomial values like 1, 2 and 3,\n \ 9670: build V1=0 V2=0 for the reference value (1),\n \ 9671: V1=1 V2=0 for (2) \n \ 9672: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \ 9673: output of IMaCh is often meaningless.\n \ 9674: Exiting.\n",lval,linei, i,line,j); 9675: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \ 9676: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \ 9677: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \ 9678: For example, for multinomial values like 1, 2 and 3,\n \ 9679: build V1=0 V2=0 for the reference value (1),\n \ 9680: V1=1 V2=0 for (2) \n \ 9681: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \ 9682: output of IMaCh is often meaningless.\n \ 9683: Exiting.\n",lval,linei, i,line,j);fflush(ficlog); 9684: return 1; 9685: } 9686: covar[j][i]=(double)(lval); 9687: strcpy(line,stra); 9688: } 9689: lstra=strlen(stra); 9690: 9691: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */ 9692: stratrunc = &(stra[lstra-9]); 9693: num[i]=atol(stratrunc); 9694: } 9695: else 9696: num[i]=atol(stra); 9697: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){ 9698: 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;}*/ 9699: 9700: i=i+1; 9701: } /* End loop reading data */ 9702: 9703: *imax=i-1; /* Number of individuals */ 9704: fclose(fic); 9705: 9706: return (0); 9707: /* endread: */ 9708: printf("Exiting readdata: "); 9709: fclose(fic); 9710: return (1); 9711: } 9712: 9713: void removefirstspace(char **stri){/*, char stro[]) {*/ 9714: char *p1 = *stri, *p2 = *stri; 9715: while (*p2 == ' ') 9716: p2++; 9717: /* while ((*p1++ = *p2++) !=0) */ 9718: /* ; */ 9719: /* do */ 9720: /* while (*p2 == ' ') */ 9721: /* p2++; */ 9722: /* while (*p1++ == *p2++); */ 9723: *stri=p2; 9724: } 9725: 9726: int decoderesult ( char resultline[], int nres) 9727: /**< This routine decode one result line and returns the combination # of dummy covariates only **/ 9728: { 9729: int j=0, k=0, k1=0, k2=0, k3=0, k4=0, match=0, k2q=0, k3q=0, k4q=0; 9730: char resultsav[MAXLINE]; 9731: int resultmodel[MAXLINE]; 9732: int modelresult[MAXLINE]; 9733: char stra[80], strb[80], strc[80], strd[80],stre[80]; 9734: 9735: removefirstspace(&resultline); 9736: 9737: if (strstr(resultline,"v") !=0){ 9738: printf("Error. 'v' must be in upper case 'V' result: %s ",resultline); 9739: fprintf(ficlog,"Error. 'v' must be in upper case result: %s ",resultline);fflush(ficlog); 9740: return 1; 9741: } 9742: trimbb(resultsav, resultline); 9743: if (strlen(resultsav) >1){ 9744: j=nbocc(resultsav,'='); /**< j=Number of covariate values'=' */ 9745: } 9746: if(j == 0){ /* Resultline but no = */ 9747: TKresult[nres]=0; /* Combination for the nresult and the model */ 9748: return (0); 9749: } 9750: if( j != cptcovs ){ /* Be careful if a variable is in a product but not single */ 9751: 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); 9752: 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); 9753: } 9754: for(k=1; k<=j;k++){ /* Loop on any covariate of the result line */ 9755: if(nbocc(resultsav,'=') >1){ 9756: cutl(stra,strb,resultsav,' '); /* keeps in strb after the first ' ' 9757: resultsav= V4=1 V5=25.1 V3=0 stra= V5=25.1 V3=0 strb= V4=1 */ 9758: cutl(strc,strd,strb,'='); /* strb:V4=1 strc=1 strd=V4 */ 9759: }else 9760: cutl(strc,strd,resultsav,'='); 9761: Tvalsel[k]=atof(strc); /* 1 */ 9762: 9763: cutl(strc,stre,strd,'V'); /* strd='V4' strc=4 stre='V' */; 9764: Tvarsel[k]=atoi(strc); 9765: /* Typevarsel[k]=1; /\* 1 for age product *\/ */ 9766: /* cptcovsel++; */ 9767: if (nbocc(stra,'=') >0) 9768: strcpy(resultsav,stra); /* and analyzes it */ 9769: } 9770: /* Checking for missing or useless values in comparison of current model needs */ 9771: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 9772: if(Typevar[k1]==0){ /* Single covariate in model */ 9773: match=0; 9774: for(k2=1; k2 <=j;k2++){/* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */ 9775: if(Tvar[k1]==Tvarsel[k2]) {/* Tvar[1]=5 == Tvarsel[2]=5 */ 9776: modelresult[k2]=k1;/* modelresult[2]=1 modelresult[1]=2 modelresult[3]=3 modelresult[6]=4 modelresult[9]=5 */ 9777: match=1; 9778: break; 9779: } 9780: } 9781: if(match == 0){ 9782: printf("Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model); 9783: fprintf(ficlog,"Error in result line: V%d is missing in result: %s according to model=%s\n",k1, resultline, model); 9784: return 1; 9785: } 9786: } 9787: } 9788: /* Checking for missing or useless values in comparison of current model needs */ 9789: for(k2=1; k2 <=j;k2++){ /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */ 9790: match=0; 9791: for(k1=1; k1<= cptcovt ;k1++){ /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 9792: if(Typevar[k1]==0){ /* Single */ 9793: if(Tvar[k1]==Tvarsel[k2]) { /* Tvar[2]=4 == Tvarsel[1]=4 */ 9794: resultmodel[k1]=k2; /* resultmodel[2]=1 resultmodel[1]=2 resultmodel[3]=3 resultmodel[6]=4 resultmodel[9]=5 */ 9795: ++match; 9796: } 9797: } 9798: } 9799: if(match == 0){ 9800: printf("Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model); 9801: fprintf(ficlog,"Error in result line: %d value missing; result: %s, model=%s\n",k1, resultline, model); 9802: return 1; 9803: }else if(match > 1){ 9804: printf("Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model); 9805: fprintf(ficlog,"Error in result line: %d doubled; result: %s, model=%s\n",k2, resultline, model); 9806: return 1; 9807: } 9808: } 9809: 9810: /* We need to deduce which combination number is chosen and save quantitative values */ 9811: /* model line V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 9812: /* result line V4=1 V5=25.1 V3=0 V2=8 V1=1 */ 9813: /* should give a combination of dummy V4=1, V3=0, V1=1 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 5 + (1offset) = 6*/ 9814: /* result line V4=1 V5=24.1 V3=1 V2=8 V1=0 */ 9815: /* should give a combination of dummy V4=1, V3=1, V1=0 => V4*2**(0) + V3*2**(1) + V1*2**(2) = 3 + (1offset) = 4*/ 9816: /* 1 0 0 0 */ 9817: /* 2 1 0 0 */ 9818: /* 3 0 1 0 */ 9819: /* 4 1 1 0 */ /* V4=1, V3=1, V1=0 */ 9820: /* 5 0 0 1 */ 9821: /* 6 1 0 1 */ /* V4=1, V3=0, V1=1 */ 9822: /* 7 0 1 1 */ 9823: /* 8 1 1 1 */ 9824: /* V(Tvresult)=Tresult V4=1 V3=0 V1=1 Tresult[nres=1][2]=0 */ 9825: /* V(Tvqresult)=Tqresult V5=25.1 V2=8 Tqresult[nres=1][1]=25.1 */ 9826: /* V5*age V5 known which value for nres? */ 9827: /* Tqinvresult[2]=8 Tqinvresult[1]=25.1 */ 9828: for(k1=1, k=0, k4=0, k4q=0; k1 <=cptcovt;k1++){ /* model line */ 9829: if( Dummy[k1]==0 && Typevar[k1]==0 ){ /* Single dummy */ 9830: k3= resultmodel[k1]; /* resultmodel[2(V4)] = 1=k3 */ 9831: k2=(int)Tvarsel[k3]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */ 9832: k+=Tvalsel[k3]*pow(2,k4); /* Tvalsel[1]=1 */ 9833: Tresult[nres][k4+1]=Tvalsel[k3];/* Tresult[nres][1]=1(V4=1) Tresult[nres][2]=0(V3=0) */ 9834: Tvresult[nres][k4+1]=(int)Tvarsel[k3];/* Tvresult[nres][1]=4 Tvresult[nres][3]=1 */ 9835: Tinvresult[nres][(int)Tvarsel[k3]]=Tvalsel[k3]; /* Tinvresult[nres][4]=1 */ 9836: printf("Decoderesult Dummy k=%d, V(k2=V%d)= Tvalsel[%d]=%d, 2**(%d)\n",k, k2, k3, (int)Tvalsel[k3], k4); 9837: k4++;; 9838: } else if( Dummy[k1]==1 && Typevar[k1]==0 ){ /* Single quantitative */ 9839: k3q= resultmodel[k1]; /* resultmodel[2] = 1=k3 */ 9840: k2q=(int)Tvarsel[k3q]; /* Tvarsel[resultmodel[2]]= Tvarsel[1] = 4=k2 */ 9841: Tqresult[nres][k4q+1]=Tvalsel[k3q]; /* Tqresult[nres][1]=25.1 */ 9842: Tvqresult[nres][k4q+1]=(int)Tvarsel[k3q]; /* Tvqresult[nres][1]=5 */ 9843: Tqinvresult[nres][(int)Tvarsel[k3q]]=Tvalsel[k3q]; /* Tqinvresult[nres][5]=25.1 */ 9844: printf("Decoderesult Quantitative nres=%d, V(k2q=V%d)= Tvalsel[%d]=%d, Tvarsel[%d]=%f\n",nres, k2q, k3q, Tvarsel[k3q], k3q, Tvalsel[k3q]); 9845: k4q++;; 9846: } 9847: } 9848: 9849: TKresult[nres]=++k; /* Combination for the nresult and the model */ 9850: return (0); 9851: } 9852: 9853: int decodemodel( char model[], int lastobs) 9854: /**< This routine decodes the model and returns: 9855: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age 9856: * - nagesqr = 1 if age*age in the model, otherwise 0. 9857: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age 9858: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age 9859: * - cptcovage number of covariates with age*products =2 9860: * - cptcovs number of simple covariates 9861: * - 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 9862: * which is a new column after the 9 (ncovcol) variables. 9863: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual 9864: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage 9865: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6. 9866: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 . 9867: */ 9868: { 9869: int i, j, k, ks, v; 9870: int j1, k1, k2, k3, k4; 9871: char modelsav[80]; 9872: char stra[80], strb[80], strc[80], strd[80],stre[80]; 9873: char *strpt; 9874: 9875: /*removespace(model);*/ 9876: if (strlen(model) >1){ /* If there is at least 1 covariate */ 9877: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0; 9878: if (strstr(model,"AGE") !=0){ 9879: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model); 9880: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog); 9881: return 1; 9882: } 9883: if (strstr(model,"v") !=0){ 9884: printf("Error. 'v' must be in upper case 'V' model=%s ",model); 9885: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog); 9886: return 1; 9887: } 9888: strcpy(modelsav,model); 9889: if ((strpt=strstr(model,"age*age")) !=0){ 9890: printf(" strpt=%s, model=%s\n",strpt, model); 9891: if(strpt != model){ 9892: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \ 9893: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \ 9894: corresponding column of parameters.\n",model); 9895: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \ 9896: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \ 9897: corresponding column of parameters.\n",model); fflush(ficlog); 9898: return 1; 9899: } 9900: nagesqr=1; 9901: if (strstr(model,"+age*age") !=0) 9902: substrchaine(modelsav, model, "+age*age"); 9903: else if (strstr(model,"age*age+") !=0) 9904: substrchaine(modelsav, model, "age*age+"); 9905: else 9906: substrchaine(modelsav, model, "age*age"); 9907: }else 9908: nagesqr=0; 9909: if (strlen(modelsav) >1){ 9910: j=nbocc(modelsav,'+'); /**< j=Number of '+' */ 9911: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */ 9912: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */ 9913: cptcovt= j+1; /* Number of total covariates in the model, not including 9914: * cst, age and age*age 9915: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/ 9916: /* including age products which are counted in cptcovage. 9917: * but the covariates which are products must be treated 9918: * separately: ncovn=4- 2=2 (V1+V3). */ 9919: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */ 9920: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */ 9921: 9922: 9923: /* Design 9924: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight 9925: * < ncovcol=8 > 9926: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 9927: * k= 1 2 3 4 5 6 7 8 9928: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8 9929: * covar[k,i], value of kth covariate if not including age for individual i: 9930: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8) 9931: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8 9932: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and 9933: * Tage[++cptcovage]=k 9934: * if products, new covar are created after ncovcol with k1 9935: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11 9936: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product 9937: * 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 9938: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2]; 9939: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted 9940: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 9941: * < ncovcol=8 > 9942: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2 9943: * k= 1 2 3 4 5 6 7 8 9 10 11 12 9944: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8 9945: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6} 9946: * p Tprod[1]@2={ 6, 5} 9947: *p Tvard[1][1]@4= {7, 8, 5, 6} 9948: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8 9949: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2]; 9950: *How to reorganize? 9951: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age 9952: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6} 9953: * {2, 1, 4, 8, 5, 6, 3, 7} 9954: * Struct [] 9955: */ 9956: 9957: /* This loop fills the array Tvar from the string 'model'.*/ 9958: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */ 9959: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */ 9960: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */ 9961: /* k=3 V4 Tvar[k=3]= 4 (from V4) */ 9962: /* k=2 V1 Tvar[k=2]= 1 (from V1) */ 9963: /* k=1 Tvar[1]=2 (from V2) */ 9964: /* k=5 Tvar[5] */ 9965: /* for (k=1; k<=cptcovn;k++) { */ 9966: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */ 9967: /* } */ 9968: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */ 9969: /* 9970: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */ 9971: for(k=cptcovt; k>=1;k--){ /**< Number of covariates not including constant and age, neither age*age*/ 9972: Tvar[k]=0; Tprod[k]=0; Tposprod[k]=0; 9973: } 9974: cptcovage=0; 9975: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */ 9976: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+' 9977: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */ 9978: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */ 9979: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/ 9980: /*scanf("%d",i);*/ 9981: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */ 9982: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */ 9983: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */ 9984: /* covar is not filled and then is empty */ 9985: cptcovprod--; 9986: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */ 9987: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */ 9988: Typevar[k]=1; /* 1 for age product */ 9989: cptcovage++; /* Sums the number of covariates which include age as a product */ 9990: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */ 9991: /*printf("stre=%s ", stre);*/ 9992: } else if (strcmp(strd,"age")==0) { /* or age*Vn */ 9993: cptcovprod--; 9994: cutl(stre,strb,strc,'V'); 9995: Tvar[k]=atoi(stre); 9996: Typevar[k]=1; /* 1 for age product */ 9997: cptcovage++; 9998: Tage[cptcovage]=k; 9999: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/ 10000: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */ 10001: cptcovn++; 10002: cptcovprodnoage++;k1++; 10003: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/ 10004: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but 10005: because this model-covariate is a construction we invent a new column 10006: which is after existing variables ncovcol+nqv+ntv+nqtv + k1 10007: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2 10008: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */ 10009: Typevar[k]=2; /* 2 for double fixed dummy covariates */ 10010: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */ 10011: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */ 10012: Tposprod[k]=k1; /* Tpsprod[3]=1, Tposprod[2]=5 */ 10013: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/ 10014: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/ 10015: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */ 10016: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */ 10017: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */ 10018: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */ 10019: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */ 10020: for (i=1; i<=lastobs;i++){ 10021: /* Computes the new covariate which is a product of 10022: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */ 10023: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i]; 10024: } 10025: } /* End age is not in the model */ 10026: } /* End if model includes a product */ 10027: else { /* no more sum */ 10028: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/ 10029: /* scanf("%d",i);*/ 10030: cutl(strd,strc,strb,'V'); 10031: ks++; /**< Number of simple covariates dummy or quantitative, fixe or varying */ 10032: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */ 10033: Tvar[k]=atoi(strd); 10034: Typevar[k]=0; /* 0 for simple covariates */ 10035: } 10036: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */ 10037: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav); 10038: scanf("%d",i);*/ 10039: } /* end of loop + on total covariates */ 10040: } /* end if strlen(modelsave == 0) age*age might exist */ 10041: } /* end if strlen(model == 0) */ 10042: 10043: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products. 10044: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/ 10045: 10046: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]); 10047: printf("cptcovprod=%d ", cptcovprod); 10048: fprintf(ficlog,"cptcovprod=%d ", cptcovprod); 10049: scanf("%d ",i);*/ 10050: 10051: 10052: /* Until here, decodemodel knows only the grammar (simple, product, age*) of the model but not what kind 10053: of variable (dummy vs quantitative, fixed vs time varying) is behind. But we know the # of each. */ 10054: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying 10055: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place 10056: k = 1 2 3 4 5 6 7 8 9 10057: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5 10058: Typevar[k]= 0 0 0 2 1 0 2 1 1 10059: Fixed[k] 1 1 1 1 3 0 0 or 2 2 3 10060: Dummy[k] 1 0 0 0 3 1 1 2 3 10061: Tmodelind[combination of covar]=k; 10062: */ 10063: /* Dispatching between quantitative and time varying covariates */ 10064: /* If Tvar[k] >ncovcol it is a product */ 10065: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */ 10066: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */ 10067: printf("Model=%s\n\ 10068: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\ 10069: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\ 10070: 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); 10071: fprintf(ficlog,"Model=%s\n\ 10072: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\ 10073: Fixed[k] 0=fixed (product or simple), 1 varying, 2 fixed with age product, 3 varying with age product \n\ 10074: 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); 10075: for(k=-1;k<=cptcovt; k++){ Fixed[k]=0; Dummy[k]=0;} 10076: 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 */ 10077: if (Tvar[k] <=ncovcol && Typevar[k]==0 ){ /* Simple fixed dummy (<=ncovcol) covariates */ 10078: Fixed[k]= 0; 10079: Dummy[k]= 0; 10080: ncoveff++; 10081: ncovf++; 10082: nsd++; 10083: modell[k].maintype= FTYPE; 10084: TvarsD[nsd]=Tvar[k]; 10085: TvarsDind[nsd]=k; 10086: TvarF[ncovf]=Tvar[k]; 10087: TvarFind[ncovf]=k; 10088: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 10089: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 10090: }else if( Tvar[k] <=ncovcol && Typevar[k]==2){ /* Product of fixed dummy (<=ncovcol) covariates */ 10091: Fixed[k]= 0; 10092: Dummy[k]= 0; 10093: ncoveff++; 10094: ncovf++; 10095: modell[k].maintype= FTYPE; 10096: TvarF[ncovf]=Tvar[k]; 10097: TvarFind[ncovf]=k; 10098: TvarFD[ncoveff]=Tvar[k]; /* TvarFD[1]=V1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 10099: TvarFDind[ncoveff]=k; /* TvarFDind[1]=9 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ 10100: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){/* Remind that product Vn*Vm are added in k Only simple fixed quantitative variable */ 10101: Fixed[k]= 0; 10102: Dummy[k]= 1; 10103: nqfveff++; 10104: modell[k].maintype= FTYPE; 10105: modell[k].subtype= FQ; 10106: nsq++; 10107: TvarsQ[nsq]=Tvar[k]; 10108: TvarsQind[nsq]=k; 10109: ncovf++; 10110: TvarF[ncovf]=Tvar[k]; 10111: TvarFind[ncovf]=k; 10112: 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 */ 10113: 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 */ 10114: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){/* Only simple time varying dummy variables */ 10115: Fixed[k]= 1; 10116: Dummy[k]= 0; 10117: ntveff++; /* Only simple time varying dummy variable */ 10118: modell[k].maintype= VTYPE; 10119: modell[k].subtype= VD; 10120: nsd++; 10121: TvarsD[nsd]=Tvar[k]; 10122: TvarsDind[nsd]=k; 10123: ncovv++; /* Only simple time varying variables */ 10124: TvarV[ncovv]=Tvar[k]; 10125: 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 */ 10126: 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 */ 10127: 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 */ 10128: 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); 10129: printf("Quasi TmodelInvind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv); 10130: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){ /* Only simple time varying quantitative variable V5*/ 10131: Fixed[k]= 1; 10132: Dummy[k]= 1; 10133: nqtveff++; 10134: modell[k].maintype= VTYPE; 10135: modell[k].subtype= VQ; 10136: ncovv++; /* Only simple time varying variables */ 10137: nsq++; 10138: TvarsQ[nsq]=Tvar[k]; 10139: TvarsQind[nsq]=k; 10140: TvarV[ncovv]=Tvar[k]; 10141: TvarVind[ncovv]=k; /* TvarVind[1]=1 in V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 */ /* Any time varying singele */ 10142: 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 */ 10143: 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 */ 10144: TmodelInvQind[nqtveff]=Tvar[k]- ncovcol-nqv-ntv;/* Only simple time varying quantitative variable */ 10145: /* Tmodeliqind[k]=nqtveff;/\* Only simple time varying quantitative variable *\/ */ 10146: 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); 10147: printf("Quasi TmodelInvQind[%d]=%d\n",k,Tvar[k]- ncovcol-nqv-ntv); 10148: }else if (Typevar[k] == 1) { /* product with age */ 10149: ncova++; 10150: TvarA[ncova]=Tvar[k]; 10151: TvarAind[ncova]=k; 10152: if (Tvar[k] <=ncovcol ){ /* Product age with fixed dummy covariatee */ 10153: Fixed[k]= 2; 10154: Dummy[k]= 2; 10155: modell[k].maintype= ATYPE; 10156: modell[k].subtype= APFD; 10157: /* ncoveff++; */ 10158: }else if( Tvar[k] <=ncovcol+nqv) { /* Remind that product Vn*Vm are added in k*/ 10159: Fixed[k]= 2; 10160: Dummy[k]= 3; 10161: modell[k].maintype= ATYPE; 10162: modell[k].subtype= APFQ; /* Product age * fixed quantitative */ 10163: /* nqfveff++; /\* Only simple fixed quantitative variable *\/ */ 10164: }else if( Tvar[k] <=ncovcol+nqv+ntv ){ 10165: Fixed[k]= 3; 10166: Dummy[k]= 2; 10167: modell[k].maintype= ATYPE; 10168: modell[k].subtype= APVD; /* Product age * varying dummy */ 10169: /* ntveff++; /\* Only simple time varying dummy variable *\/ */ 10170: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){ 10171: Fixed[k]= 3; 10172: Dummy[k]= 3; 10173: modell[k].maintype= ATYPE; 10174: modell[k].subtype= APVQ; /* Product age * varying quantitative */ 10175: /* nqtveff++;/\* Only simple time varying quantitative variable *\/ */ 10176: } 10177: }else if (Typevar[k] == 2) { /* product without age */ 10178: k1=Tposprod[k]; 10179: if(Tvard[k1][1] <=ncovcol){ 10180: if(Tvard[k1][2] <=ncovcol){ 10181: Fixed[k]= 1; 10182: Dummy[k]= 0; 10183: modell[k].maintype= FTYPE; 10184: modell[k].subtype= FPDD; /* Product fixed dummy * fixed dummy */ 10185: ncovf++; /* Fixed variables without age */ 10186: TvarF[ncovf]=Tvar[k]; 10187: TvarFind[ncovf]=k; 10188: }else if(Tvard[k1][2] <=ncovcol+nqv){ 10189: Fixed[k]= 0; /* or 2 ?*/ 10190: Dummy[k]= 1; 10191: modell[k].maintype= FTYPE; 10192: modell[k].subtype= FPDQ; /* Product fixed dummy * fixed quantitative */ 10193: ncovf++; /* Varying variables without age */ 10194: TvarF[ncovf]=Tvar[k]; 10195: TvarFind[ncovf]=k; 10196: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ 10197: Fixed[k]= 1; 10198: Dummy[k]= 0; 10199: modell[k].maintype= VTYPE; 10200: modell[k].subtype= VPDD; /* Product fixed dummy * varying dummy */ 10201: ncovv++; /* Varying variables without age */ 10202: TvarV[ncovv]=Tvar[k]; 10203: TvarVind[ncovv]=k; 10204: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ 10205: Fixed[k]= 1; 10206: Dummy[k]= 1; 10207: modell[k].maintype= VTYPE; 10208: modell[k].subtype= VPDQ; /* Product fixed dummy * varying quantitative */ 10209: ncovv++; /* Varying variables without age */ 10210: TvarV[ncovv]=Tvar[k]; 10211: TvarVind[ncovv]=k; 10212: } 10213: }else if(Tvard[k1][1] <=ncovcol+nqv){ 10214: if(Tvard[k1][2] <=ncovcol){ 10215: Fixed[k]= 0; /* or 2 ?*/ 10216: Dummy[k]= 1; 10217: modell[k].maintype= FTYPE; 10218: modell[k].subtype= FPDQ; /* Product fixed quantitative * fixed dummy */ 10219: ncovf++; /* Fixed variables without age */ 10220: TvarF[ncovf]=Tvar[k]; 10221: TvarFind[ncovf]=k; 10222: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ 10223: Fixed[k]= 1; 10224: Dummy[k]= 1; 10225: modell[k].maintype= VTYPE; 10226: modell[k].subtype= VPDQ; /* Product fixed quantitative * varying dummy */ 10227: ncovv++; /* Varying variables without age */ 10228: TvarV[ncovv]=Tvar[k]; 10229: TvarVind[ncovv]=k; 10230: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ 10231: Fixed[k]= 1; 10232: Dummy[k]= 1; 10233: modell[k].maintype= VTYPE; 10234: modell[k].subtype= VPQQ; /* Product fixed quantitative * varying quantitative */ 10235: ncovv++; /* Varying variables without age */ 10236: TvarV[ncovv]=Tvar[k]; 10237: TvarVind[ncovv]=k; 10238: ncovv++; /* Varying variables without age */ 10239: TvarV[ncovv]=Tvar[k]; 10240: TvarVind[ncovv]=k; 10241: } 10242: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){ 10243: if(Tvard[k1][2] <=ncovcol){ 10244: Fixed[k]= 1; 10245: Dummy[k]= 1; 10246: modell[k].maintype= VTYPE; 10247: modell[k].subtype= VPDD; /* Product time varying dummy * fixed dummy */ 10248: ncovv++; /* Varying variables without age */ 10249: TvarV[ncovv]=Tvar[k]; 10250: TvarVind[ncovv]=k; 10251: }else if(Tvard[k1][2] <=ncovcol+nqv){ 10252: Fixed[k]= 1; 10253: Dummy[k]= 1; 10254: modell[k].maintype= VTYPE; 10255: modell[k].subtype= VPDQ; /* Product time varying dummy * fixed quantitative */ 10256: ncovv++; /* Varying variables without age */ 10257: TvarV[ncovv]=Tvar[k]; 10258: TvarVind[ncovv]=k; 10259: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ 10260: Fixed[k]= 1; 10261: Dummy[k]= 0; 10262: modell[k].maintype= VTYPE; 10263: modell[k].subtype= VPDD; /* Product time varying dummy * time varying dummy */ 10264: ncovv++; /* Varying variables without age */ 10265: TvarV[ncovv]=Tvar[k]; 10266: TvarVind[ncovv]=k; 10267: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ 10268: Fixed[k]= 1; 10269: Dummy[k]= 1; 10270: modell[k].maintype= VTYPE; 10271: modell[k].subtype= VPDQ; /* Product time varying dummy * time varying quantitative */ 10272: ncovv++; /* Varying variables without age */ 10273: TvarV[ncovv]=Tvar[k]; 10274: TvarVind[ncovv]=k; 10275: } 10276: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){ 10277: if(Tvard[k1][2] <=ncovcol){ 10278: Fixed[k]= 1; 10279: Dummy[k]= 1; 10280: modell[k].maintype= VTYPE; 10281: modell[k].subtype= VPDQ; /* Product time varying quantitative * fixed dummy */ 10282: ncovv++; /* Varying variables without age */ 10283: TvarV[ncovv]=Tvar[k]; 10284: TvarVind[ncovv]=k; 10285: }else if(Tvard[k1][2] <=ncovcol+nqv){ 10286: Fixed[k]= 1; 10287: Dummy[k]= 1; 10288: modell[k].maintype= VTYPE; 10289: modell[k].subtype= VPQQ; /* Product time varying quantitative * fixed quantitative */ 10290: ncovv++; /* Varying variables without age */ 10291: TvarV[ncovv]=Tvar[k]; 10292: TvarVind[ncovv]=k; 10293: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){ 10294: Fixed[k]= 1; 10295: Dummy[k]= 1; 10296: modell[k].maintype= VTYPE; 10297: modell[k].subtype= VPDQ; /* Product time varying quantitative * time varying dummy */ 10298: ncovv++; /* Varying variables without age */ 10299: TvarV[ncovv]=Tvar[k]; 10300: TvarVind[ncovv]=k; 10301: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){ 10302: Fixed[k]= 1; 10303: Dummy[k]= 1; 10304: modell[k].maintype= VTYPE; 10305: modell[k].subtype= VPQQ; /* Product time varying quantitative * time varying quantitative */ 10306: ncovv++; /* Varying variables without age */ 10307: TvarV[ncovv]=Tvar[k]; 10308: TvarVind[ncovv]=k; 10309: } 10310: }else{ 10311: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]); 10312: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]); 10313: } /*end k1*/ 10314: }else{ 10315: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]); 10316: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]); 10317: } 10318: 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]); 10319: printf(" modell[%d].maintype=%d, modell[%d].subtype=%d\n",k,modell[k].maintype,k,modell[k].subtype); 10320: 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]); 10321: } 10322: /* Searching for doublons in the model */ 10323: for(k1=1; k1<= cptcovt;k1++){ 10324: for(k2=1; k2 <k1;k2++){ 10325: /* if((Typevar[k1]==Typevar[k2]) && (Fixed[Tvar[k1]]==Fixed[Tvar[k2]]) && (Dummy[Tvar[k1]]==Dummy[Tvar[k2]] )){ */ 10326: if((Typevar[k1]==Typevar[k2]) && (Fixed[k1]==Fixed[k2]) && (Dummy[k1]==Dummy[k2] )){ 10327: if((Typevar[k1] == 0 || Typevar[k1] == 1)){ /* Simple or age product */ 10328: if(Tvar[k1]==Tvar[k2]){ 10329: 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]); 10330: 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); 10331: return(1); 10332: } 10333: }else if (Typevar[k1] ==2){ 10334: k3=Tposprod[k1]; 10335: k4=Tposprod[k2]; 10336: 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])) ){ 10337: 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]]); 10338: 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); 10339: return(1); 10340: } 10341: } 10342: } 10343: } 10344: } 10345: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn); 10346: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn); 10347: printf("ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd,nsq); 10348: fprintf(ficlog,"ncovf=%d, ncovv=%d, ncova=%d, nsd=%d, nsq=%d\n",ncovf,ncovv,ncova,nsd, nsq); 10349: return (0); /* with covar[new additional covariate if product] and Tage if age */ 10350: /*endread:*/ 10351: printf("Exiting decodemodel: "); 10352: return (1); 10353: } 10354: 10355: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn ) 10356: {/* Check ages at death */ 10357: int i, m; 10358: int firstone=0; 10359: 10360: for (i=1; i<=imx; i++) { 10361: for(m=2; (m<= maxwav); m++) { 10362: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){ 10363: anint[m][i]=9999; 10364: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */ 10365: s[m][i]=-1; 10366: } 10367: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){ 10368: *nberr = *nberr + 1; 10369: if(firstone == 0){ 10370: firstone=1; 10371: 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); 10372: } 10373: 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); 10374: s[m][i]=-1; /* Droping the death status */ 10375: } 10376: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){ 10377: (*nberr)++; 10378: 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); 10379: 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); 10380: s[m][i]=-2; /* We prefer to skip it (and to skip it in version 0.8a1 too */ 10381: } 10382: } 10383: } 10384: 10385: for (i=1; i<=imx; i++) { 10386: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]); 10387: for(m=firstpass; (m<= lastpass); m++){ 10388: 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 */ 10389: if (s[m][i] >= nlstate+1) { 10390: if(agedc[i]>0){ 10391: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){ 10392: agev[m][i]=agedc[i]; 10393: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/ 10394: }else { 10395: if ((int)andc[i]!=9999){ 10396: nbwarn++; 10397: printf("Warning negative age at death: %ld line:%d\n",num[i],i); 10398: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i); 10399: agev[m][i]=-1; 10400: } 10401: } 10402: } /* agedc > 0 */ 10403: } /* end if */ 10404: else if(s[m][i] !=9){ /* Standard case, age in fractional 10405: years but with the precision of a month */ 10406: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]); 10407: if((int)mint[m][i]==99 || (int)anint[m][i]==9999) 10408: agev[m][i]=1; 10409: else if(agev[m][i] < *agemin){ 10410: *agemin=agev[m][i]; 10411: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin); 10412: } 10413: else if(agev[m][i] >*agemax){ 10414: *agemax=agev[m][i]; 10415: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/ 10416: } 10417: /*agev[m][i]=anint[m][i]-annais[i];*/ 10418: /* agev[m][i] = age[i]+2*m;*/ 10419: } /* en if 9*/ 10420: else { /* =9 */ 10421: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */ 10422: agev[m][i]=1; 10423: s[m][i]=-1; 10424: } 10425: } 10426: else if(s[m][i]==0) /*= 0 Unknown */ 10427: agev[m][i]=1; 10428: else{ 10429: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]); 10430: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]); 10431: agev[m][i]=0; 10432: } 10433: } /* End for lastpass */ 10434: } 10435: 10436: for (i=1; i<=imx; i++) { 10437: for(m=firstpass; (m<=lastpass); m++){ 10438: if (s[m][i] > (nlstate+ndeath)) { 10439: (*nberr)++; 10440: 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); 10441: 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); 10442: return 1; 10443: } 10444: } 10445: } 10446: 10447: /*for (i=1; i<=imx; i++){ 10448: for (m=firstpass; (m<lastpass); m++){ 10449: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]); 10450: } 10451: 10452: }*/ 10453: 10454: 10455: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax); 10456: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax); 10457: 10458: return (0); 10459: /* endread:*/ 10460: printf("Exiting calandcheckages: "); 10461: return (1); 10462: } 10463: 10464: #if defined(_MSC_VER) 10465: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/ 10466: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/ 10467: //#include "stdafx.h" 10468: //#include <stdio.h> 10469: //#include <tchar.h> 10470: //#include <windows.h> 10471: //#include <iostream> 10472: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); 10473: 10474: LPFN_ISWOW64PROCESS fnIsWow64Process; 10475: 10476: BOOL IsWow64() 10477: { 10478: BOOL bIsWow64 = FALSE; 10479: 10480: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS) 10481: // (HANDLE, PBOOL); 10482: 10483: //LPFN_ISWOW64PROCESS fnIsWow64Process; 10484: 10485: HMODULE module = GetModuleHandle(_T("kernel32")); 10486: const char funcName[] = "IsWow64Process"; 10487: fnIsWow64Process = (LPFN_ISWOW64PROCESS) 10488: GetProcAddress(module, funcName); 10489: 10490: if (NULL != fnIsWow64Process) 10491: { 10492: if (!fnIsWow64Process(GetCurrentProcess(), 10493: &bIsWow64)) 10494: //throw std::exception("Unknown error"); 10495: printf("Unknown error\n"); 10496: } 10497: return bIsWow64 != FALSE; 10498: } 10499: #endif 10500: 10501: void syscompilerinfo(int logged) 10502: { 10503: #include <stdint.h> 10504: 10505: /* #include "syscompilerinfo.h"*/ 10506: /* command line Intel compiler 32bit windows, XP compatible:*/ 10507: /* /GS /W3 /Gy 10508: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D 10509: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D 10510: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo 10511: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch" 10512: */ 10513: /* 64 bits */ 10514: /* 10515: /GS /W3 /Gy 10516: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG" 10517: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope 10518: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir 10519: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */ 10520: /* Optimization are useless and O3 is slower than O2 */ 10521: /* 10522: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32" 10523: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo 10524: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel 10525: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch" 10526: */ 10527: /* Link is */ /* /OUT:"visual studio 10528: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT 10529: /PDB:"visual studio 10530: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE 10531: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" 10532: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" 10533: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" 10534: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO 10535: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker' 10536: uiAccess='false'" 10537: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF 10538: /NOLOGO /TLBID:1 10539: */ 10540: 10541: 10542: #if defined __INTEL_COMPILER 10543: #if defined(__GNUC__) 10544: struct utsname sysInfo; /* For Intel on Linux and OS/X */ 10545: #endif 10546: #elif defined(__GNUC__) 10547: #ifndef __APPLE__ 10548: #include <gnu/libc-version.h> /* Only on gnu */ 10549: #endif 10550: struct utsname sysInfo; 10551: int cross = CROSS; 10552: if (cross){ 10553: printf("Cross-"); 10554: if(logged) fprintf(ficlog, "Cross-"); 10555: } 10556: #endif 10557: 10558: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:"); 10559: #if defined(__clang__) 10560: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */ 10561: #endif 10562: #if defined(__ICC) || defined(__INTEL_COMPILER) 10563: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */ 10564: #endif 10565: #if defined(__GNUC__) || defined(__GNUG__) 10566: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */ 10567: #endif 10568: #if defined(__HP_cc) || defined(__HP_aCC) 10569: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */ 10570: #endif 10571: #if defined(__IBMC__) || defined(__IBMCPP__) 10572: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */ 10573: #endif 10574: #if defined(_MSC_VER) 10575: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */ 10576: #endif 10577: #if defined(__PGI) 10578: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */ 10579: #endif 10580: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC) 10581: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */ 10582: #endif 10583: printf(" for "); if (logged) fprintf(ficlog, " for "); 10584: 10585: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros 10586: #ifdef _WIN32 // note the underscore: without it, it's not msdn official! 10587: // Windows (x64 and x86) 10588: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) "); 10589: #elif __unix__ // all unices, not all compilers 10590: // Unix 10591: printf("Unix ");if(logged) fprintf(ficlog,"Unix "); 10592: #elif __linux__ 10593: // linux 10594: printf("linux ");if(logged) fprintf(ficlog,"linux "); 10595: #elif __APPLE__ 10596: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though.. 10597: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS "); 10598: #endif 10599: 10600: /* __MINGW32__ */ 10601: /* __CYGWIN__ */ 10602: /* __MINGW64__ */ 10603: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx 10604: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */ 10605: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */ 10606: /* _WIN64 // Defined for applications for Win64. */ 10607: /* _M_X64 // Defined for compilations that target x64 processors. */ 10608: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */ 10609: 10610: #if UINTPTR_MAX == 0xffffffff 10611: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */ 10612: #elif UINTPTR_MAX == 0xffffffffffffffff 10613: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */ 10614: #else 10615: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */ 10616: #endif 10617: 10618: #if defined(__GNUC__) 10619: # if defined(__GNUC_PATCHLEVEL__) 10620: # define __GNUC_VERSION__ (__GNUC__ * 10000 \ 10621: + __GNUC_MINOR__ * 100 \ 10622: + __GNUC_PATCHLEVEL__) 10623: # else 10624: # define __GNUC_VERSION__ (__GNUC__ * 10000 \ 10625: + __GNUC_MINOR__ * 100) 10626: # endif 10627: printf(" using GNU C version %d.\n", __GNUC_VERSION__); 10628: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__); 10629: 10630: if (uname(&sysInfo) != -1) { 10631: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine); 10632: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine); 10633: } 10634: else 10635: perror("uname() error"); 10636: //#ifndef __INTEL_COMPILER 10637: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__) 10638: printf("GNU libc version: %s\n", gnu_get_libc_version()); 10639: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version()); 10640: #endif 10641: #endif 10642: 10643: // void main () 10644: // { 10645: #if defined(_MSC_VER) 10646: if (IsWow64()){ 10647: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n"); 10648: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n"); 10649: } 10650: else{ 10651: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n"); 10652: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n"); 10653: } 10654: // printf("\nPress Enter to continue..."); 10655: // getchar(); 10656: // } 10657: 10658: #endif 10659: 10660: 10661: } 10662: 10663: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){ 10664: /*--------------- Prevalence limit (forward period or forward stable prevalence) --------------*/ 10665: int i, j, k, i1, k4=0, nres=0 ; 10666: /* double ftolpl = 1.e-10; */ 10667: double age, agebase, agelim; 10668: double tot; 10669: 10670: strcpy(filerespl,"PL_"); 10671: strcat(filerespl,fileresu); 10672: if((ficrespl=fopen(filerespl,"w"))==NULL) { 10673: printf("Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1; 10674: fprintf(ficlog,"Problem with forward period (stable) prevalence resultfile: %s\n", filerespl);return 1; 10675: } 10676: printf("\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl); 10677: fprintf(ficlog,"\nComputing forward period (stable) prevalence: result on file '%s' \n", filerespl); 10678: pstamp(ficrespl); 10679: fprintf(ficrespl,"# Forward period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl); 10680: fprintf(ficrespl,"#Age "); 10681: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i); 10682: fprintf(ficrespl,"\n"); 10683: 10684: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */ 10685: 10686: agebase=ageminpar; 10687: agelim=agemaxpar; 10688: 10689: /* i1=pow(2,ncoveff); */ 10690: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */ 10691: if (cptcovn < 1){i1=1;} 10692: 10693: for(k=1; k<=i1;k++){ /* For each combination k of dummy covariates in the model */ 10694: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 10695: if(i1 != 1 && TKresult[nres]!= k) 10696: continue; 10697: 10698: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */ 10699: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */ 10700: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){ 10701: /* k=k+1; */ 10702: /* to clean */ 10703: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov)); 10704: fprintf(ficrespl,"#******"); 10705: printf("#******"); 10706: fprintf(ficlog,"#******"); 10707: for(j=1;j<=cptcoveff ;j++) {/* all covariates */ 10708: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); /* Here problem for varying dummy*/ 10709: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10710: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10711: } 10712: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 10713: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 10714: fprintf(ficrespl," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 10715: fprintf(ficlog," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 10716: } 10717: fprintf(ficrespl,"******\n"); 10718: printf("******\n"); 10719: fprintf(ficlog,"******\n"); 10720: if(invalidvarcomb[k]){ 10721: printf("\nCombination (%d) ignored because no case \n",k); 10722: fprintf(ficrespl,"#Combination (%d) ignored because no case \n",k); 10723: fprintf(ficlog,"\nCombination (%d) ignored because no case \n",k); 10724: continue; 10725: } 10726: 10727: fprintf(ficrespl,"#Age "); 10728: for(j=1;j<=cptcoveff;j++) { 10729: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10730: } 10731: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i); 10732: fprintf(ficrespl,"Total Years_to_converge\n"); 10733: 10734: for (age=agebase; age<=agelim; age++){ 10735: /* for (age=agebase; age<=agebase; age++){ */ 10736: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k, nres); 10737: fprintf(ficrespl,"%.0f ",age ); 10738: for(j=1;j<=cptcoveff;j++) 10739: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10740: tot=0.; 10741: for(i=1; i<=nlstate;i++){ 10742: tot += prlim[i][i]; 10743: fprintf(ficrespl," %.5f", prlim[i][i]); 10744: } 10745: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp); 10746: } /* Age */ 10747: /* was end of cptcod */ 10748: } /* cptcov */ 10749: } /* nres */ 10750: return 0; 10751: } 10752: 10753: 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){ 10754: /*--------------- Back Prevalence limit (backward stable prevalence) --------------*/ 10755: 10756: /* Computes the back prevalence limit for any combination of covariate values 10757: * at any age between ageminpar and agemaxpar 10758: */ 10759: int i, j, k, i1, nres=0 ; 10760: /* double ftolpl = 1.e-10; */ 10761: double age, agebase, agelim; 10762: double tot; 10763: /* double ***mobaverage; */ 10764: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */ 10765: 10766: strcpy(fileresplb,"PLB_"); 10767: strcat(fileresplb,fileresu); 10768: if((ficresplb=fopen(fileresplb,"w"))==NULL) { 10769: printf("Problem with backward prevalence resultfile: %s\n", fileresplb);return 1; 10770: fprintf(ficlog,"Problem with backward prevalence resultfile: %s\n", fileresplb);return 1; 10771: } 10772: printf("Computing backward prevalence: result on file '%s' \n", fileresplb); 10773: fprintf(ficlog,"Computing backward prevalence: result on file '%s' \n", fileresplb); 10774: pstamp(ficresplb); 10775: fprintf(ficresplb,"# Backward prevalence. Precision given by ftolpl=%g \n", ftolpl); 10776: fprintf(ficresplb,"#Age "); 10777: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i); 10778: fprintf(ficresplb,"\n"); 10779: 10780: 10781: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */ 10782: 10783: agebase=ageminpar; 10784: agelim=agemaxpar; 10785: 10786: 10787: i1=pow(2,cptcoveff); 10788: if (cptcovn < 1){i1=1;} 10789: 10790: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 10791: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */ 10792: if(i1 != 1 && TKresult[nres]!= k) 10793: continue; 10794: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov)); 10795: fprintf(ficresplb,"#******"); 10796: printf("#******"); 10797: fprintf(ficlog,"#******"); 10798: for(j=1;j<=cptcoveff ;j++) {/* all covariates */ 10799: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10800: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10801: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10802: } 10803: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 10804: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 10805: fprintf(ficresplb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 10806: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 10807: } 10808: fprintf(ficresplb,"******\n"); 10809: printf("******\n"); 10810: fprintf(ficlog,"******\n"); 10811: if(invalidvarcomb[k]){ 10812: printf("\nCombination (%d) ignored because no cases \n",k); 10813: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k); 10814: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k); 10815: continue; 10816: } 10817: 10818: fprintf(ficresplb,"#Age "); 10819: for(j=1;j<=cptcoveff;j++) { 10820: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10821: } 10822: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i); 10823: fprintf(ficresplb,"Total Years_to_converge\n"); 10824: 10825: 10826: for (age=agebase; age<=agelim; age++){ 10827: /* for (age=agebase; age<=agebase; age++){ */ 10828: if(mobilavproj > 0){ 10829: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */ 10830: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */ 10831: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k, nres); 10832: }else if (mobilavproj == 0){ 10833: 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); 10834: 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); 10835: exit(1); 10836: }else{ 10837: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */ 10838: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k,nres); 10839: /* printf("TOTOT\n"); */ 10840: /* exit(1); */ 10841: } 10842: fprintf(ficresplb,"%.0f ",age ); 10843: for(j=1;j<=cptcoveff;j++) 10844: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10845: tot=0.; 10846: for(i=1; i<=nlstate;i++){ 10847: tot += bprlim[i][i]; 10848: fprintf(ficresplb," %.5f", bprlim[i][i]); 10849: } 10850: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp); 10851: } /* Age */ 10852: /* was end of cptcod */ 10853: /*fprintf(ficresplb,"\n");*/ /* Seems to be necessary for gnuplot only if two result lines and no covariate. */ 10854: } /* end of any combination */ 10855: } /* end of nres */ 10856: /* hBijx(p, bage, fage); */ 10857: /* fclose(ficrespijb); */ 10858: 10859: return 0; 10860: } 10861: 10862: int hPijx(double *p, int bage, int fage){ 10863: /*------------- h Pij x at various ages ------------*/ 10864: 10865: int stepsize; 10866: int agelim; 10867: int hstepm; 10868: int nhstepm; 10869: int h, i, i1, j, k, k4, nres=0; 10870: 10871: double agedeb; 10872: double ***p3mat; 10873: 10874: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu); 10875: if((ficrespij=fopen(filerespij,"w"))==NULL) { 10876: printf("Problem with Pij resultfile: %s\n", filerespij); return 1; 10877: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1; 10878: } 10879: printf("Computing pij: result on file '%s' \n", filerespij); 10880: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij); 10881: 10882: stepsize=(int) (stepm+YEARM-1)/YEARM; 10883: /*if (stepm<=24) stepsize=2;*/ 10884: 10885: agelim=AGESUP; 10886: hstepm=stepsize*YEARM; /* Every year of age */ 10887: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */ 10888: 10889: /* hstepm=1; aff par mois*/ 10890: pstamp(ficrespij); 10891: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x "); 10892: i1= pow(2,cptcoveff); 10893: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */ 10894: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */ 10895: /* k=k+1; */ 10896: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 10897: for(k=1; k<=i1;k++){ 10898: if(i1 != 1 && TKresult[nres]!= k) 10899: continue; 10900: fprintf(ficrespij,"\n#****** "); 10901: for(j=1;j<=cptcoveff;j++) 10902: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10903: for (k4=1; k4<= nsq; k4++){ /* For each selected (single) quantitative value */ 10904: printf(" V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 10905: fprintf(ficrespij," V%d=%f ",Tvqresult[nres][k4],Tqresult[nres][k4]); 10906: } 10907: fprintf(ficrespij,"******\n"); 10908: 10909: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */ 10910: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */ 10911: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */ 10912: 10913: /* nhstepm=nhstepm*YEARM; aff par mois*/ 10914: 10915: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 10916: oldm=oldms;savm=savms; 10917: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k, nres); 10918: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j="); 10919: for(i=1; i<=nlstate;i++) 10920: for(j=1; j<=nlstate+ndeath;j++) 10921: fprintf(ficrespij," %1d-%1d",i,j); 10922: fprintf(ficrespij,"\n"); 10923: for (h=0; h<=nhstepm; h++){ 10924: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/ 10925: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); 10926: for(i=1; i<=nlstate;i++) 10927: for(j=1; j<=nlstate+ndeath;j++) 10928: fprintf(ficrespij," %.5f", p3mat[i][j][h]); 10929: fprintf(ficrespij,"\n"); 10930: } 10931: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 10932: fprintf(ficrespij,"\n"); 10933: } 10934: /*}*/ 10935: } 10936: return 0; 10937: } 10938: 10939: int hBijx(double *p, int bage, int fage, double ***prevacurrent){ 10940: /*------------- h Bij x at various ages ------------*/ 10941: 10942: int stepsize; 10943: /* int agelim; */ 10944: int ageminl; 10945: int hstepm; 10946: int nhstepm; 10947: int h, i, i1, j, k, nres; 10948: 10949: double agedeb; 10950: double ***p3mat; 10951: 10952: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu); 10953: if((ficrespijb=fopen(filerespijb,"w"))==NULL) { 10954: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1; 10955: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1; 10956: } 10957: printf("Computing pij back: result on file '%s' \n", filerespijb); 10958: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb); 10959: 10960: stepsize=(int) (stepm+YEARM-1)/YEARM; 10961: /*if (stepm<=24) stepsize=2;*/ 10962: 10963: /* agelim=AGESUP; */ 10964: ageminl=AGEINF; /* was 30 */ 10965: hstepm=stepsize*YEARM; /* Every year of age */ 10966: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */ 10967: 10968: /* hstepm=1; aff par mois*/ 10969: pstamp(ficrespijb); 10970: fprintf(ficrespijb,"#****** h Bij x Back probability to be in state i at age x-h being in j at x: B1j+B2j+...=1 "); 10971: i1= pow(2,cptcoveff); 10972: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */ 10973: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */ 10974: /* k=k+1; */ 10975: for(nres=1; nres <= nresult; nres++){ /* For each resultline */ 10976: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */ 10977: if(i1 != 1 && TKresult[nres]!= k) 10978: continue; 10979: fprintf(ficrespijb,"\n#****** "); 10980: for(j=1;j<=cptcoveff;j++) 10981: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 10982: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 10983: fprintf(ficrespijb," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 10984: } 10985: fprintf(ficrespijb,"******\n"); 10986: if(invalidvarcomb[k]){ /* Is it necessary here? */ 10987: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k); 10988: continue; 10989: } 10990: 10991: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */ 10992: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */ 10993: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */ 10994: 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 */ 10995: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 or 28*/ 10996: 10997: /* nhstepm=nhstepm*YEARM; aff par mois*/ 10998: 10999: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); /* We can't have it at an upper level because of nhstepm */ 11000: /* and memory limitations if stepm is small */ 11001: 11002: /* oldm=oldms;savm=savms; */ 11003: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */ 11004: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k, nres); 11005: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */ 11006: fprintf(ficrespijb,"# Cov Agex agex-h hbijx with i,j="); 11007: for(i=1; i<=nlstate;i++) 11008: for(j=1; j<=nlstate+ndeath;j++) 11009: fprintf(ficrespijb," %1d-%1d",i,j); 11010: fprintf(ficrespijb,"\n"); 11011: for (h=0; h<=nhstepm; h++){ 11012: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/ 11013: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm ); 11014: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */ 11015: for(i=1; i<=nlstate;i++) 11016: for(j=1; j<=nlstate+ndeath;j++) 11017: fprintf(ficrespijb," %.5f", p3mat[i][j][h]); 11018: fprintf(ficrespijb,"\n"); 11019: } 11020: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); 11021: fprintf(ficrespijb,"\n"); 11022: } /* end age deb */ 11023: } /* end combination */ 11024: } /* end nres */ 11025: return 0; 11026: } /* hBijx */ 11027: 11028: 11029: /***********************************************/ 11030: /**************** Main Program *****************/ 11031: /***********************************************/ 11032: 11033: int main(int argc, char *argv[]) 11034: { 11035: #ifdef GSL 11036: const gsl_multimin_fminimizer_type *T; 11037: size_t iteri = 0, it; 11038: int rval = GSL_CONTINUE; 11039: int status = GSL_SUCCESS; 11040: double ssval; 11041: #endif 11042: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav); 11043: int i,j, k, iter=0,m,size=100, cptcod; /* Suppressing because nobs */ 11044: /* int i,j, k, n=MAXN,iter=0,m,size=100, cptcod; */ 11045: int ncvyear=0; /* Number of years needed for the period prevalence to converge */ 11046: int jj, ll, li, lj, lk; 11047: int numlinepar=0; /* Current linenumber of parameter file */ 11048: int num_filled; 11049: int itimes; 11050: int NDIM=2; 11051: int vpopbased=0; 11052: int nres=0; 11053: int endishere=0; 11054: int noffset=0; 11055: int ncurrv=0; /* Temporary variable */ 11056: 11057: char ca[32], cb[32]; 11058: /* FILE *fichtm; *//* Html File */ 11059: /* FILE *ficgp;*/ /*Gnuplot File */ 11060: struct stat info; 11061: double agedeb=0.; 11062: 11063: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW; 11064: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */ 11065: 11066: double fret; 11067: double dum=0.; /* Dummy variable */ 11068: double ***p3mat; 11069: /* double ***mobaverage; */ 11070: 11071: char line[MAXLINE]; 11072: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE]; 11073: 11074: char modeltemp[MAXLINE]; 11075: char resultline[MAXLINE]; 11076: 11077: char pathr[MAXLINE], pathimach[MAXLINE]; 11078: char *tok, *val; /* pathtot */ 11079: int firstobs=1, lastobs=10; /* nobs = lastobs-firstobs declared globally ;*/ 11080: int c, h , cpt, c2; 11081: int jl=0; 11082: int i1, j1, jk, stepsize=0; 11083: int count=0; 11084: 11085: int *tab; 11086: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */ 11087: /* double anprojd, mprojd, jprojd; /\* For eventual projections *\/ */ 11088: /* double anprojf, mprojf, jprojf; */ 11089: /* double jintmean,mintmean,aintmean; */ 11090: int prvforecast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */ 11091: int prvbackcast = 0; /* Might be 1 (date of beginning of projection is a choice or 2 is the dateintmean */ 11092: double yrfproj= 10.0; /* Number of years of forward projections */ 11093: double yrbproj= 10.0; /* Number of years of backward projections */ 11094: int prevbcast=0; /* defined as global for mlikeli and mle, replacing backcast */ 11095: int mobilav=0,popforecast=0; 11096: int hstepm=0, nhstepm=0; 11097: int agemortsup; 11098: float sumlpop=0.; 11099: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000; 11100: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000; 11101: 11102: double bage=0, fage=110., age, agelim=0., agebase=0.; 11103: double ftolpl=FTOL; 11104: double **prlim; 11105: double **bprlim; 11106: double ***param; /* Matrix of parameters, param[i][j][k] param=ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel) 11107: state of origin, state of destination including death, for each covariate: constante, age, and V1 V2 etc. */ 11108: double ***paramstart; /* Matrix of starting parameter values */ 11109: double *p, *pstart; /* p=param[1][1] pstart is for starting values guessed by freqsummary */ 11110: double **matcov; /* Matrix of covariance */ 11111: double **hess; /* Hessian matrix */ 11112: double ***delti3; /* Scale */ 11113: double *delti; /* Scale */ 11114: double ***eij, ***vareij; 11115: double **varpl; /* Variances of prevalence limits by age */ 11116: 11117: double *epj, vepp; 11118: 11119: double dateprev1, dateprev2; 11120: double jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000, dateproj1=0, dateproj2=0, dateprojd=0, dateprojf=0; 11121: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000, dateback1=0, dateback2=0, datebackd=0, datebackf=0; 11122: 11123: 11124: double **ximort; 11125: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234"; 11126: int *dcwave; 11127: 11128: char z[1]="c"; 11129: 11130: /*char *strt;*/ 11131: char strtend[80]; 11132: 11133: 11134: /* setlocale (LC_ALL, ""); */ 11135: /* bindtextdomain (PACKAGE, LOCALEDIR); */ 11136: /* textdomain (PACKAGE); */ 11137: /* setlocale (LC_CTYPE, ""); */ 11138: /* setlocale (LC_MESSAGES, ""); */ 11139: 11140: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */ 11141: rstart_time = time(NULL); 11142: /* (void) gettimeofday(&start_time,&tzp);*/ 11143: start_time = *localtime(&rstart_time); 11144: curr_time=start_time; 11145: /*tml = *localtime(&start_time.tm_sec);*/ 11146: /* strcpy(strstart,asctime(&tml)); */ 11147: strcpy(strstart,asctime(&start_time)); 11148: 11149: /* printf("Localtime (at start)=%s",strstart); */ 11150: /* tp.tm_sec = tp.tm_sec +86400; */ 11151: /* tm = *localtime(&start_time.tm_sec); */ 11152: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */ 11153: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */ 11154: /* tmg.tm_hour=tmg.tm_hour + 1; */ 11155: /* tp.tm_sec = mktime(&tmg); */ 11156: /* strt=asctime(&tmg); */ 11157: /* printf("Time(after) =%s",strstart); */ 11158: /* (void) time (&time_value); 11159: * printf("time=%d,t-=%d\n",time_value,time_value-86400); 11160: * tm = *localtime(&time_value); 11161: * strstart=asctime(&tm); 11162: * printf("tim_value=%d,asctime=%s\n",time_value,strstart); 11163: */ 11164: 11165: nberr=0; /* Number of errors and warnings */ 11166: nbwarn=0; 11167: #ifdef WIN32 11168: _getcwd(pathcd, size); 11169: #else 11170: getcwd(pathcd, size); 11171: #endif 11172: syscompilerinfo(0); 11173: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion); 11174: if(argc <=1){ 11175: printf("\nEnter the parameter file name: "); 11176: if(!fgets(pathr,FILENAMELENGTH,stdin)){ 11177: printf("ERROR Empty parameter file name\n"); 11178: goto end; 11179: } 11180: i=strlen(pathr); 11181: if(pathr[i-1]=='\n') 11182: pathr[i-1]='\0'; 11183: i=strlen(pathr); 11184: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */ 11185: pathr[i-1]='\0'; 11186: } 11187: i=strlen(pathr); 11188: if( i==0 ){ 11189: printf("ERROR Empty parameter file name\n"); 11190: goto end; 11191: } 11192: for (tok = pathr; tok != NULL; ){ 11193: printf("Pathr |%s|\n",pathr); 11194: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0'); 11195: printf("val= |%s| pathr=%s\n",val,pathr); 11196: strcpy (pathtot, val); 11197: if(pathr[0] == '\0') break; /* Dirty */ 11198: } 11199: } 11200: else if (argc<=2){ 11201: strcpy(pathtot,argv[1]); 11202: } 11203: else{ 11204: strcpy(pathtot,argv[1]); 11205: strcpy(z,argv[2]); 11206: printf("\nargv[2]=%s z=%c\n",argv[2],z[0]); 11207: } 11208: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/ 11209: /*cygwin_split_path(pathtot,path,optionfile); 11210: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/ 11211: /* cutv(path,optionfile,pathtot,'\\');*/ 11212: 11213: /* Split argv[0], imach program to get pathimach */ 11214: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]); 11215: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname); 11216: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname); 11217: /* strcpy(pathimach,argv[0]); */ 11218: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */ 11219: split(pathtot,path,optionfile,optionfilext,optionfilefiname); 11220: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname); 11221: #ifdef WIN32 11222: _chdir(path); /* Can be a relative path */ 11223: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */ 11224: #else 11225: chdir(path); /* Can be a relative path */ 11226: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */ 11227: #endif 11228: printf("Current directory %s!\n",pathcd); 11229: strcpy(command,"mkdir "); 11230: strcat(command,optionfilefiname); 11231: if((outcmd=system(command)) != 0){ 11232: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd); 11233: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */ 11234: /* fclose(ficlog); */ 11235: /* exit(1); */ 11236: } 11237: /* if((imk=mkdir(optionfilefiname))<0){ */ 11238: /* perror("mkdir"); */ 11239: /* } */ 11240: 11241: /*-------- arguments in the command line --------*/ 11242: 11243: /* Main Log file */ 11244: strcat(filelog, optionfilefiname); 11245: strcat(filelog,".log"); /* */ 11246: if((ficlog=fopen(filelog,"w"))==NULL) { 11247: printf("Problem with logfile %s\n",filelog); 11248: goto end; 11249: } 11250: fprintf(ficlog,"Log filename:%s\n",filelog); 11251: fprintf(ficlog,"Version %s %s",version,fullversion); 11252: fprintf(ficlog,"\nEnter the parameter file name: \n"); 11253: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\ 11254: path=%s \n\ 11255: optionfile=%s\n\ 11256: optionfilext=%s\n\ 11257: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname); 11258: 11259: syscompilerinfo(1); 11260: 11261: printf("Local time (at start):%s",strstart); 11262: fprintf(ficlog,"Local time (at start): %s",strstart); 11263: fflush(ficlog); 11264: /* (void) gettimeofday(&curr_time,&tzp); */ 11265: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */ 11266: 11267: /* */ 11268: strcpy(fileres,"r"); 11269: strcat(fileres, optionfilefiname); 11270: strcat(fileresu, optionfilefiname); /* Without r in front */ 11271: strcat(fileres,".txt"); /* Other files have txt extension */ 11272: strcat(fileresu,".txt"); /* Other files have txt extension */ 11273: 11274: /* Main ---------arguments file --------*/ 11275: 11276: if((ficpar=fopen(optionfile,"r"))==NULL) { 11277: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno)); 11278: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno)); 11279: fflush(ficlog); 11280: /* goto end; */ 11281: exit(70); 11282: } 11283: 11284: strcpy(filereso,"o"); 11285: strcat(filereso,fileresu); 11286: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */ 11287: printf("Problem with Output resultfile: %s\n", filereso); 11288: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso); 11289: fflush(ficlog); 11290: goto end; 11291: } 11292: /*-------- Rewriting parameter file ----------*/ 11293: strcpy(rfileres,"r"); /* "Rparameterfile */ 11294: strcat(rfileres,optionfilefiname); /* Parameter file first name */ 11295: strcat(rfileres,"."); /* */ 11296: strcat(rfileres,optionfilext); /* Other files have txt extension */ 11297: if((ficres =fopen(rfileres,"w"))==NULL) { 11298: printf("Problem writing new parameter file: %s\n", rfileres);goto end; 11299: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end; 11300: fflush(ficlog); 11301: goto end; 11302: } 11303: fprintf(ficres,"#IMaCh %s\n",version); 11304: 11305: 11306: /* Reads comments: lines beginning with '#' */ 11307: numlinepar=0; 11308: /* Is it a BOM UTF-8 Windows file? */ 11309: /* First parameter line */ 11310: while(fgets(line, MAXLINE, ficpar)) { 11311: noffset=0; 11312: if( line[0] == (char)0xEF && line[1] == (char)0xBB) /* EF BB BF */ 11313: { 11314: noffset=noffset+3; 11315: printf("# File is an UTF8 Bom.\n"); // 0xBF 11316: } 11317: /* else if( line[0] == (char)0xFE && line[1] == (char)0xFF)*/ 11318: else if( line[0] == (char)0xFF && line[1] == (char)0xFE) 11319: { 11320: noffset=noffset+2; 11321: printf("# File is an UTF16BE BOM file\n"); 11322: } 11323: else if( line[0] == 0 && line[1] == 0) 11324: { 11325: if( line[2] == (char)0xFE && line[3] == (char)0xFF){ 11326: noffset=noffset+4; 11327: printf("# File is an UTF16BE BOM file\n"); 11328: } 11329: } else{ 11330: ;/*printf(" Not a BOM file\n");*/ 11331: } 11332: 11333: /* If line starts with a # it is a comment */ 11334: if (line[noffset] == '#') { 11335: numlinepar++; 11336: fputs(line,stdout); 11337: fputs(line,ficparo); 11338: fputs(line,ficres); 11339: fputs(line,ficlog); 11340: continue; 11341: }else 11342: break; 11343: } 11344: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \ 11345: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){ 11346: if (num_filled != 5) { 11347: printf("Should be 5 parameters\n"); 11348: fprintf(ficlog,"Should be 5 parameters\n"); 11349: } 11350: numlinepar++; 11351: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass); 11352: fprintf(ficparo,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass); 11353: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass); 11354: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass); 11355: } 11356: /* Second parameter line */ 11357: while(fgets(line, MAXLINE, ficpar)) { 11358: /* while(fscanf(ficpar,"%[^\n]", line)) { */ 11359: /* If line starts with a # it is a comment. Strangely fgets reads the EOL and fputs doesn't */ 11360: if (line[0] == '#') { 11361: numlinepar++; 11362: printf("%s",line); 11363: fprintf(ficres,"%s",line); 11364: fprintf(ficparo,"%s",line); 11365: fprintf(ficlog,"%s",line); 11366: continue; 11367: }else 11368: break; 11369: } 11370: 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", \ 11371: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){ 11372: if (num_filled != 11) { 11373: 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"); 11374: printf("but line=%s\n",line); 11375: 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"); 11376: fprintf(ficlog,"but line=%s\n",line); 11377: } 11378: if( lastpass > maxwav){ 11379: printf("Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav); 11380: fprintf(ficlog,"Error (lastpass = %d) > (maxwav = %d)\n",lastpass, maxwav); 11381: fflush(ficlog); 11382: goto end; 11383: } 11384: 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); 11385: 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); 11386: 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); 11387: 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); 11388: } 11389: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */ 11390: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */ 11391: /* Third parameter line */ 11392: while(fgets(line, MAXLINE, ficpar)) { 11393: /* If line starts with a # it is a comment */ 11394: if (line[0] == '#') { 11395: numlinepar++; 11396: printf("%s",line); 11397: fprintf(ficres,"%s",line); 11398: fprintf(ficparo,"%s",line); 11399: fprintf(ficlog,"%s",line); 11400: continue; 11401: }else 11402: break; 11403: } 11404: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){ 11405: if (num_filled != 1){ 11406: printf("ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line); 11407: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age+' instead of '%s'\n",num_filled, line); 11408: model[0]='\0'; 11409: goto end; 11410: } 11411: else{ 11412: if (model[0]=='+'){ 11413: for(i=1; i<=strlen(model);i++) 11414: modeltemp[i-1]=model[i]; 11415: strcpy(model,modeltemp); 11416: } 11417: } 11418: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */ 11419: printf("model=1+age+%s\n",model);fflush(stdout); 11420: fprintf(ficparo,"model=1+age+%s\n",model);fflush(stdout); 11421: fprintf(ficres,"model=1+age+%s\n",model);fflush(stdout); 11422: fprintf(ficlog,"model=1+age+%s\n",model);fflush(stdout); 11423: } 11424: /* 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); */ 11425: /* numlinepar=numlinepar+3; /\* In general *\/ */ 11426: /* 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); */ 11427: /* 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); */ 11428: /* 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); */ 11429: fflush(ficlog); 11430: /* if(model[0]=='#'|| model[0]== '\0'){ */ 11431: if(model[0]=='#'){ 11432: printf("Error in 'model' line: model should start with 'model=1+age+' and end without space \n \ 11433: 'model=1+age+' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age' or \n \ 11434: 'model=1+age+V1+V2' or 'model=1+age+V1+V2+V1*V2' etc. \n"); \ 11435: if(mle != -1){ 11436: 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"); 11437: exit(1); 11438: } 11439: } 11440: while((c=getc(ficpar))=='#' && c!= EOF){ 11441: ungetc(c,ficpar); 11442: fgets(line, MAXLINE, ficpar); 11443: numlinepar++; 11444: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */ 11445: z[0]=line[1]; 11446: } 11447: /* printf("****line [1] = %c \n",line[1]); */ 11448: fputs(line, stdout); 11449: //puts(line); 11450: fputs(line,ficparo); 11451: fputs(line,ficlog); 11452: } 11453: ungetc(c,ficpar); 11454: 11455: 11456: covar=matrix(0,NCOVMAX,firstobs,lastobs); /**< used in readdata */ 11457: if(nqv>=1)coqvar=matrix(1,nqv,firstobs,lastobs); /**< Fixed quantitative covariate */ 11458: if(nqtv>=1)cotqvar=ma3x(1,maxwav,1,nqtv,firstobs,lastobs); /**< Time varying quantitative covariate */ 11459: if(ntv+nqtv>=1)cotvar=ma3x(1,maxwav,1,ntv+nqtv,firstobs,lastobs); /**< Time varying covariate (dummy and quantitative)*/ 11460: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/ 11461: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5 11462: v1+v2*age+v2*v3 makes cptcovn = 3 11463: */ 11464: if (strlen(model)>1) 11465: 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*/ 11466: else 11467: ncovmodel=2; /* Constant and age */ 11468: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */ 11469: npar= nforce*ncovmodel; /* Number of parameters like aij*/ 11470: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){ 11471: 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); 11472: 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); 11473: fflush(stdout); 11474: fclose (ficlog); 11475: goto end; 11476: } 11477: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); 11478: delti=delti3[1][1]; 11479: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/ 11480: if(mle==-1){ /* Print a wizard for help writing covariance matrix */ 11481: /* We could also provide initial parameters values giving by simple logistic regression 11482: * only one way, that is without matrix product. We will have nlstate maximizations */ 11483: /* for(i=1;i<nlstate;i++){ */ 11484: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */ 11485: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */ 11486: /* } */ 11487: prwizard(ncovmodel, nlstate, ndeath, model, ficparo); 11488: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso); 11489: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso); 11490: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel); 11491: fclose (ficparo); 11492: fclose (ficlog); 11493: goto end; 11494: exit(0); 11495: } else if(mle==-5) { /* Main Wizard */ 11496: prwizard(ncovmodel, nlstate, ndeath, model, ficparo); 11497: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso); 11498: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso); 11499: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); 11500: matcov=matrix(1,npar,1,npar); 11501: hess=matrix(1,npar,1,npar); 11502: } else{ /* Begin of mle != -1 or -5 */ 11503: /* Read guessed parameters */ 11504: /* Reads comments: lines beginning with '#' */ 11505: while((c=getc(ficpar))=='#' && c!= EOF){ 11506: ungetc(c,ficpar); 11507: fgets(line, MAXLINE, ficpar); 11508: numlinepar++; 11509: fputs(line,stdout); 11510: fputs(line,ficparo); 11511: fputs(line,ficlog); 11512: } 11513: ungetc(c,ficpar); 11514: 11515: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); 11516: paramstart= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); 11517: for(i=1; i <=nlstate; i++){ 11518: j=0; 11519: for(jj=1; jj <=nlstate+ndeath; jj++){ 11520: if(jj==i) continue; 11521: j++; 11522: while((c=getc(ficpar))=='#' && c!= EOF){ 11523: ungetc(c,ficpar); 11524: fgets(line, MAXLINE, ficpar); 11525: numlinepar++; 11526: fputs(line,stdout); 11527: fputs(line,ficparo); 11528: fputs(line,ficlog); 11529: } 11530: ungetc(c,ficpar); 11531: fscanf(ficpar,"%1d%1d",&i1,&j1); 11532: if ((i1 != i) || (j1 != jj)){ 11533: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \ 11534: It might be a problem of design; if ncovcol and the model are correct\n \ 11535: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1); 11536: exit(1); 11537: } 11538: fprintf(ficparo,"%1d%1d",i1,j1); 11539: if(mle==1) 11540: printf("%1d%1d",i,jj); 11541: fprintf(ficlog,"%1d%1d",i,jj); 11542: for(k=1; k<=ncovmodel;k++){ 11543: fscanf(ficpar," %lf",¶m[i][j][k]); 11544: if(mle==1){ 11545: printf(" %lf",param[i][j][k]); 11546: fprintf(ficlog," %lf",param[i][j][k]); 11547: } 11548: else 11549: fprintf(ficlog," %lf",param[i][j][k]); 11550: fprintf(ficparo," %lf",param[i][j][k]); 11551: } 11552: fscanf(ficpar,"\n"); 11553: numlinepar++; 11554: if(mle==1) 11555: printf("\n"); 11556: fprintf(ficlog,"\n"); 11557: fprintf(ficparo,"\n"); 11558: } 11559: } 11560: fflush(ficlog); 11561: 11562: /* Reads parameters values */ 11563: p=param[1][1]; 11564: pstart=paramstart[1][1]; 11565: 11566: /* Reads comments: lines beginning with '#' */ 11567: while((c=getc(ficpar))=='#' && c!= EOF){ 11568: ungetc(c,ficpar); 11569: fgets(line, MAXLINE, ficpar); 11570: numlinepar++; 11571: fputs(line,stdout); 11572: fputs(line,ficparo); 11573: fputs(line,ficlog); 11574: } 11575: ungetc(c,ficpar); 11576: 11577: for(i=1; i <=nlstate; i++){ 11578: for(j=1; j <=nlstate+ndeath-1; j++){ 11579: fscanf(ficpar,"%1d%1d",&i1,&j1); 11580: if ( (i1-i) * (j1-j) != 0){ 11581: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1); 11582: exit(1); 11583: } 11584: printf("%1d%1d",i,j); 11585: fprintf(ficparo,"%1d%1d",i1,j1); 11586: fprintf(ficlog,"%1d%1d",i1,j1); 11587: for(k=1; k<=ncovmodel;k++){ 11588: fscanf(ficpar,"%le",&delti3[i][j][k]); 11589: printf(" %le",delti3[i][j][k]); 11590: fprintf(ficparo," %le",delti3[i][j][k]); 11591: fprintf(ficlog," %le",delti3[i][j][k]); 11592: } 11593: fscanf(ficpar,"\n"); 11594: numlinepar++; 11595: printf("\n"); 11596: fprintf(ficparo,"\n"); 11597: fprintf(ficlog,"\n"); 11598: } 11599: } 11600: fflush(ficlog); 11601: 11602: /* Reads covariance matrix */ 11603: delti=delti3[1][1]; 11604: 11605: 11606: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */ 11607: 11608: /* Reads comments: lines beginning with '#' */ 11609: while((c=getc(ficpar))=='#' && c!= EOF){ 11610: ungetc(c,ficpar); 11611: fgets(line, MAXLINE, ficpar); 11612: numlinepar++; 11613: fputs(line,stdout); 11614: fputs(line,ficparo); 11615: fputs(line,ficlog); 11616: } 11617: ungetc(c,ficpar); 11618: 11619: matcov=matrix(1,npar,1,npar); 11620: hess=matrix(1,npar,1,npar); 11621: for(i=1; i <=npar; i++) 11622: for(j=1; j <=npar; j++) matcov[i][j]=0.; 11623: 11624: /* Scans npar lines */ 11625: for(i=1; i <=npar; i++){ 11626: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk); 11627: if(count != 3){ 11628: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\ 11629: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\ 11630: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model); 11631: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\ 11632: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\ 11633: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model); 11634: exit(1); 11635: }else{ 11636: if(mle==1) 11637: printf("%1d%1d%d",i1,j1,jk); 11638: } 11639: fprintf(ficlog,"%1d%1d%d",i1,j1,jk); 11640: fprintf(ficparo,"%1d%1d%d",i1,j1,jk); 11641: for(j=1; j <=i; j++){ 11642: fscanf(ficpar," %le",&matcov[i][j]); 11643: if(mle==1){ 11644: printf(" %.5le",matcov[i][j]); 11645: } 11646: fprintf(ficlog," %.5le",matcov[i][j]); 11647: fprintf(ficparo," %.5le",matcov[i][j]); 11648: } 11649: fscanf(ficpar,"\n"); 11650: numlinepar++; 11651: if(mle==1) 11652: printf("\n"); 11653: fprintf(ficlog,"\n"); 11654: fprintf(ficparo,"\n"); 11655: } 11656: /* End of read covariance matrix npar lines */ 11657: for(i=1; i <=npar; i++) 11658: for(j=i+1;j<=npar;j++) 11659: matcov[i][j]=matcov[j][i]; 11660: 11661: if(mle==1) 11662: printf("\n"); 11663: fprintf(ficlog,"\n"); 11664: 11665: fflush(ficlog); 11666: 11667: } /* End of mle != -3 */ 11668: 11669: /* Main data 11670: */ 11671: nobs=lastobs-firstobs+1; /* was = lastobs;*/ 11672: /* num=lvector(1,n); */ 11673: /* moisnais=vector(1,n); */ 11674: /* annais=vector(1,n); */ 11675: /* moisdc=vector(1,n); */ 11676: /* andc=vector(1,n); */ 11677: /* weight=vector(1,n); */ 11678: /* agedc=vector(1,n); */ 11679: /* cod=ivector(1,n); */ 11680: /* for(i=1;i<=n;i++){ */ 11681: num=lvector(firstobs,lastobs); 11682: moisnais=vector(firstobs,lastobs); 11683: annais=vector(firstobs,lastobs); 11684: moisdc=vector(firstobs,lastobs); 11685: andc=vector(firstobs,lastobs); 11686: weight=vector(firstobs,lastobs); 11687: agedc=vector(firstobs,lastobs); 11688: cod=ivector(firstobs,lastobs); 11689: for(i=firstobs;i<=lastobs;i++){ 11690: num[i]=0; 11691: moisnais[i]=0; 11692: annais[i]=0; 11693: moisdc[i]=0; 11694: andc[i]=0; 11695: agedc[i]=0; 11696: cod[i]=0; 11697: weight[i]=1.0; /* Equal weights, 1 by default */ 11698: } 11699: mint=matrix(1,maxwav,firstobs,lastobs); 11700: anint=matrix(1,maxwav,firstobs,lastobs); 11701: s=imatrix(1,maxwav+1,firstobs,lastobs); /* s[i][j] health state for wave i and individual j */ 11702: tab=ivector(1,NCOVMAX); 11703: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */ 11704: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */ 11705: 11706: /* Reads data from file datafile */ 11707: if (readdata(datafile, firstobs, lastobs, &imx)==1) 11708: goto end; 11709: 11710: /* Calculation of the number of parameters from char model */ 11711: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 11712: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4 11713: k=3 V4 Tvar[k=3]= 4 (from V4) 11714: k=2 V1 Tvar[k=2]= 1 (from V1) 11715: k=1 Tvar[1]=2 (from V2) 11716: */ 11717: 11718: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */ 11719: TvarsDind=ivector(1,NCOVMAX); /* */ 11720: TvarsD=ivector(1,NCOVMAX); /* */ 11721: TvarsQind=ivector(1,NCOVMAX); /* */ 11722: TvarsQ=ivector(1,NCOVMAX); /* */ 11723: TvarF=ivector(1,NCOVMAX); /* */ 11724: TvarFind=ivector(1,NCOVMAX); /* */ 11725: TvarV=ivector(1,NCOVMAX); /* */ 11726: TvarVind=ivector(1,NCOVMAX); /* */ 11727: TvarA=ivector(1,NCOVMAX); /* */ 11728: TvarAind=ivector(1,NCOVMAX); /* */ 11729: TvarFD=ivector(1,NCOVMAX); /* */ 11730: TvarFDind=ivector(1,NCOVMAX); /* */ 11731: TvarFQ=ivector(1,NCOVMAX); /* */ 11732: TvarFQind=ivector(1,NCOVMAX); /* */ 11733: TvarVD=ivector(1,NCOVMAX); /* */ 11734: TvarVDind=ivector(1,NCOVMAX); /* */ 11735: TvarVQ=ivector(1,NCOVMAX); /* */ 11736: TvarVQind=ivector(1,NCOVMAX); /* */ 11737: 11738: Tvalsel=vector(1,NCOVMAX); /* */ 11739: Tvarsel=ivector(1,NCOVMAX); /* */ 11740: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */ 11741: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */ 11742: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */ 11743: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs). 11744: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4, 11745: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage. 11746: */ 11747: /* For model-covariate k tells which data-covariate to use but 11748: because this model-covariate is a construction we invent a new column 11749: ncovcol + k1 11750: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3 11751: Tvar[3=V1*V4]=4+1 etc */ 11752: Tprod=ivector(1,NCOVMAX); /* Gives the k position of the k1 product */ 11753: Tposprod=ivector(1,NCOVMAX); /* Gives the k1 product from the k position */ 11754: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3 11755: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2) 11756: Tposprod[k]=k1 , Tposprod[3]=1, Tposprod[5]=2 11757: */ 11758: Tvaraff=ivector(1,NCOVMAX); /* Unclear */ 11759: 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 11760: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd. 11761: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */ 11762: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age 11763: 4 covariates (3 plus signs) 11764: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3 11765: */ 11766: Tmodelind=ivector(1,NCOVMAX);/** gives the k model position of an 11767: * individual dummy, fixed or varying: 11768: * Tmodelind[Tvaraff[3]]=9,Tvaraff[1]@9={4, 11769: * 3, 1, 0, 0, 0, 0, 0, 0}, 11770: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1 , 11771: * V1 df, V2 qf, V3 & V4 dv, V5 qv 11772: * Tmodelind[1]@9={9,0,3,2,}*/ 11773: TmodelInvind=ivector(1,NCOVMAX); /* TmodelInvind=Tvar[k]- ncovcol-nqv={5-2-1=2,*/ 11774: TmodelInvQind=ivector(1,NCOVMAX);/** gives the k model position of an 11775: * individual quantitative, fixed or varying: 11776: * Tmodelqind[1]=1,Tvaraff[1]@9={4, 11777: * 3, 1, 0, 0, 0, 0, 0, 0}, 11778: * model=V5+V4+V3+V4*V3+V5*age+V2+V1*V2+V1*age+V1*/ 11779: /* Main decodemodel */ 11780: 11781: 11782: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/ 11783: goto end; 11784: 11785: if((double)(lastobs-imx)/(double)imx > 1.10){ 11786: nbwarn++; 11787: 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); 11788: 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); 11789: } 11790: /* if(mle==1){*/ 11791: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/ 11792: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */ 11793: } 11794: 11795: /*-calculation of age at interview from date of interview and age at death -*/ 11796: agev=matrix(1,maxwav,1,imx); 11797: 11798: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1) 11799: goto end; 11800: 11801: 11802: agegomp=(int)agemin; 11803: free_vector(moisnais,firstobs,lastobs); 11804: free_vector(annais,firstobs,lastobs); 11805: /* free_matrix(mint,1,maxwav,1,n); 11806: free_matrix(anint,1,maxwav,1,n);*/ 11807: /* free_vector(moisdc,1,n); */ 11808: /* free_vector(andc,1,n); */ 11809: /* */ 11810: 11811: wav=ivector(1,imx); 11812: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */ 11813: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */ 11814: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */ 11815: 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.*/ 11816: bh=imatrix(1,lastpass-firstpass+2,1,imx); 11817: mw=imatrix(1,lastpass-firstpass+2,1,imx); 11818: 11819: /* Concatenates waves */ 11820: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i. 11821: Death is a valid wave (if date is known). 11822: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i 11823: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i] 11824: and mw[mi+1][i]. dh depends on stepm. 11825: */ 11826: 11827: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm); 11828: /* Concatenates waves */ 11829: 11830: free_vector(moisdc,firstobs,lastobs); 11831: free_vector(andc,firstobs,lastobs); 11832: 11833: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */ 11834: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX); 11835: ncodemax[1]=1; 11836: Ndum =ivector(-1,NCOVMAX); 11837: cptcoveff=0; 11838: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */ 11839: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */ 11840: } 11841: 11842: ncovcombmax=pow(2,cptcoveff); 11843: invalidvarcomb=ivector(1, ncovcombmax); 11844: for(i=1;i<ncovcombmax;i++) 11845: invalidvarcomb[i]=0; 11846: 11847: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in 11848: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/ 11849: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */ 11850: 11851: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */ 11852: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/ 11853: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/ 11854: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j, 11855: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded 11856: * (currently 0 or 1) in the data. 11857: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of 11858: * corresponding modality (h,j). 11859: */ 11860: 11861: h=0; 11862: /*if (cptcovn > 0) */ 11863: m=pow(2,cptcoveff); 11864: 11865: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1 11866: * For k=4 covariates, h goes from 1 to m=2**k 11867: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1; 11868: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1 11869: * h\k 1 2 3 4 11870: *______________________________ 11871: * 1 i=1 1 i=1 1 i=1 1 i=1 1 11872: * 2 2 1 1 1 11873: * 3 i=2 1 2 1 1 11874: * 4 2 2 1 1 11875: * 5 i=3 1 i=2 1 2 1 11876: * 6 2 1 2 1 11877: * 7 i=4 1 2 2 1 11878: * 8 2 2 2 1 11879: * 9 i=5 1 i=3 1 i=2 1 2 11880: * 10 2 1 1 2 11881: * 11 i=6 1 2 1 2 11882: * 12 2 2 1 2 11883: * 13 i=7 1 i=4 1 2 2 11884: * 14 2 1 2 2 11885: * 15 i=8 1 2 2 2 11886: * 16 2 2 2 2 11887: */ 11888: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */ 11889: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4 11890: * and the value of each covariate? 11891: * V1=1, V2=1, V3=2, V4=1 ? 11892: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok. 11893: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st. 11894: * In order to get the real value in the data, we use nbcode 11895: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0 11896: * We are keeping this crazy system in order to be able (in the future?) 11897: * to have more than 2 values (0 or 1) for a covariate. 11898: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1 11899: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1 11900: * bbbbbbbb 11901: * 76543210 11902: * h-1 00000101 (6-1=5) 11903: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift 11904: * & 11905: * 1 00000001 (1) 11906: * 00000000 = 1 & ((h-1) >> (k-1)) 11907: * +1= 00000001 =1 11908: * 11909: * h=14, k=3 => h'=h-1=13, k'=k-1=2 11910: * h' 1101 =2^3+2^2+0x2^1+2^0 11911: * >>k' 11 11912: * & 00000001 11913: * = 00000001 11914: * +1 = 00000010=2 = codtabm(14,3) 11915: * Reverse h=6 and m=16? 11916: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1. 11917: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff) 11918: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1 11919: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1) 11920: * V3=decodtabm(14,3,2**4)=2 11921: * h'=13 1101 =2^3+2^2+0x2^1+2^0 11922: *(h-1) >> (j-1) 0011 =13 >> 2 11923: * &1 000000001 11924: * = 000000001 11925: * +1= 000000010 =2 11926: * 2211 11927: * V1=1+1, V2=0+1, V3=1+1, V4=1+1 11928: * V3=2 11929: * codtabm and decodtabm are identical 11930: */ 11931: 11932: 11933: free_ivector(Ndum,-1,NCOVMAX); 11934: 11935: 11936: 11937: /* Initialisation of ----------- gnuplot -------------*/ 11938: strcpy(optionfilegnuplot,optionfilefiname); 11939: if(mle==-3) 11940: strcat(optionfilegnuplot,"-MORT_"); 11941: strcat(optionfilegnuplot,".gp"); 11942: 11943: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) { 11944: printf("Problem with file %s",optionfilegnuplot); 11945: } 11946: else{ 11947: fprintf(ficgp,"\n# IMaCh-%s\n", version); 11948: fprintf(ficgp,"# %s\n", optionfilegnuplot); 11949: //fprintf(ficgp,"set missing 'NaNq'\n"); 11950: fprintf(ficgp,"set datafile missing 'NaNq'\n"); 11951: } 11952: /* fclose(ficgp);*/ 11953: 11954: 11955: /* Initialisation of --------- index.htm --------*/ 11956: 11957: strcpy(optionfilehtm,optionfilefiname); /* Main html file */ 11958: if(mle==-3) 11959: strcat(optionfilehtm,"-MORT_"); 11960: strcat(optionfilehtm,".htm"); 11961: if((fichtm=fopen(optionfilehtm,"w"))==NULL) { 11962: printf("Problem with %s \n",optionfilehtm); 11963: exit(0); 11964: } 11965: 11966: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */ 11967: strcat(optionfilehtmcov,"-cov.htm"); 11968: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) { 11969: printf("Problem with %s \n",optionfilehtmcov), exit(0); 11970: } 11971: else{ 11972: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \ 11973: <hr size=\"2\" color=\"#EC5E5E\"> \n\ 11974: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\ 11975: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model); 11976: } 11977: 11978: 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> \ 11979: <hr size=\"2\" color=\"#EC5E5E\"> \n\ 11980: <font size=\"2\">IMaCh-%s <br> %s</font> \ 11981: <hr size=\"2\" color=\"#EC5E5E\"> \n\ 11982: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\ 11983: \n\ 11984: <hr size=\"2\" color=\"#EC5E5E\">\ 11985: <ul><li><h4>Parameter files</h4>\n\ 11986: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\ 11987: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\ 11988: - Log file of the run: <a href=\"%s\">%s</a><br>\n\ 11989: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\ 11990: - Date and time at start: %s</ul>\n",\ 11991: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\ 11992: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\ 11993: fileres,fileres,\ 11994: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart); 11995: fflush(fichtm); 11996: 11997: strcpy(pathr,path); 11998: strcat(pathr,optionfilefiname); 11999: #ifdef WIN32 12000: _chdir(optionfilefiname); /* Move to directory named optionfile */ 12001: #else 12002: chdir(optionfilefiname); /* Move to directory named optionfile */ 12003: #endif 12004: 12005: 12006: /* Calculates basic frequencies. Computes observed prevalence at single age 12007: and for any valid combination of covariates 12008: and prints on file fileres'p'. */ 12009: freqsummary(fileres, p, pstart, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \ 12010: firstpass, lastpass, stepm, weightopt, model); 12011: 12012: fprintf(fichtm,"\n"); 12013: 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",\ 12014: ftol, stepm); 12015: fprintf(fichtm,"\n<li>Number of fixed dummy covariates: ncovcol=%d ", ncovcol); 12016: ncurrv=1; 12017: for(i=ncurrv; i <=ncovcol; i++) fprintf(fichtm,"V%d ", i); 12018: fprintf(fichtm,"\n<li> Number of fixed quantitative variables: nqv=%d ", nqv); 12019: ncurrv=i; 12020: for(i=ncurrv; i <=ncurrv-1+nqv; i++) fprintf(fichtm,"V%d ", i); 12021: fprintf(fichtm,"\n<li> Number of time varying (wave varying) dummy covariates: ntv=%d ", ntv); 12022: ncurrv=i; 12023: for(i=ncurrv; i <=ncurrv-1+ntv; i++) fprintf(fichtm,"V%d ", i); 12024: fprintf(fichtm,"\n<li>Number of time varying quantitative covariates: nqtv=%d ", nqtv); 12025: ncurrv=i; 12026: for(i=ncurrv; i <=ncurrv-1+nqtv; i++) fprintf(fichtm,"V%d ", i); 12027: 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", \ 12028: nlstate, ndeath, maxwav, mle, weightopt); 12029: 12030: fprintf(fichtm,"<h4> Diagram of states <a href=\"%s_.svg\">%s_.svg</a></h4> \n\ 12031: <img src=\"%s_.svg\">", subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_"),subdirf2(optionfilefiname,"D_")); 12032: 12033: 12034: fprintf(fichtm,"\n<h4>Some descriptive statistics </h4>\n<br>Number of (used) observations=%d <br>\n\ 12035: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\ 12036: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\ 12037: imx,agemin,agemax,jmin,jmax,jmean); 12038: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ 12039: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ 12040: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ 12041: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */ 12042: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */ 12043: 12044: /* For Powell, parameters are in a vector p[] starting at p[1] 12045: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */ 12046: p=param[1][1]; /* *(*(*(param +1)+1)+0) */ 12047: 12048: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/ 12049: /* For mortality only */ 12050: if (mle==-3){ 12051: ximort=matrix(1,NDIM,1,NDIM); 12052: for(i=1;i<=NDIM;i++) 12053: for(j=1;j<=NDIM;j++) 12054: ximort[i][j]=0.; 12055: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */ 12056: cens=ivector(firstobs,lastobs); 12057: ageexmed=vector(firstobs,lastobs); 12058: agecens=vector(firstobs,lastobs); 12059: dcwave=ivector(firstobs,lastobs); 12060: 12061: for (i=1; i<=imx; i++){ 12062: dcwave[i]=-1; 12063: for (m=firstpass; m<=lastpass; m++) 12064: if (s[m][i]>nlstate) { 12065: dcwave[i]=m; 12066: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/ 12067: break; 12068: } 12069: } 12070: 12071: for (i=1; i<=imx; i++) { 12072: if (wav[i]>0){ 12073: ageexmed[i]=agev[mw[1][i]][i]; 12074: j=wav[i]; 12075: agecens[i]=1.; 12076: 12077: if (ageexmed[i]> 1 && wav[i] > 0){ 12078: agecens[i]=agev[mw[j][i]][i]; 12079: cens[i]= 1; 12080: }else if (ageexmed[i]< 1) 12081: cens[i]= -1; 12082: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass) 12083: cens[i]=0 ; 12084: } 12085: else cens[i]=-1; 12086: } 12087: 12088: for (i=1;i<=NDIM;i++) { 12089: for (j=1;j<=NDIM;j++) 12090: ximort[i][j]=(i == j ? 1.0 : 0.0); 12091: } 12092: 12093: p[1]=0.0268; p[NDIM]=0.083; 12094: /* printf("%lf %lf", p[1], p[2]); */ 12095: 12096: 12097: #ifdef GSL 12098: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n"); 12099: #else 12100: printf("Powell\n"); fprintf(ficlog,"Powell\n"); 12101: #endif 12102: strcpy(filerespow,"POW-MORT_"); 12103: strcat(filerespow,fileresu); 12104: if((ficrespow=fopen(filerespow,"w"))==NULL) { 12105: printf("Problem with resultfile: %s\n", filerespow); 12106: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow); 12107: } 12108: #ifdef GSL 12109: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL"); 12110: #else 12111: fprintf(ficrespow,"# Powell\n# iter -2*LL"); 12112: #endif 12113: /* for (i=1;i<=nlstate;i++) 12114: for(j=1;j<=nlstate+ndeath;j++) 12115: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j); 12116: */ 12117: fprintf(ficrespow,"\n"); 12118: #ifdef GSL 12119: /* gsl starts here */ 12120: T = gsl_multimin_fminimizer_nmsimplex; 12121: gsl_multimin_fminimizer *sfm = NULL; 12122: gsl_vector *ss, *x; 12123: gsl_multimin_function minex_func; 12124: 12125: /* Initial vertex size vector */ 12126: ss = gsl_vector_alloc (NDIM); 12127: 12128: if (ss == NULL){ 12129: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0); 12130: } 12131: /* Set all step sizes to 1 */ 12132: gsl_vector_set_all (ss, 0.001); 12133: 12134: /* Starting point */ 12135: 12136: x = gsl_vector_alloc (NDIM); 12137: 12138: if (x == NULL){ 12139: gsl_vector_free(ss); 12140: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0); 12141: } 12142: 12143: /* Initialize method and iterate */ 12144: /* p[1]=0.0268; p[NDIM]=0.083; */ 12145: /* gsl_vector_set(x, 0, 0.0268); */ 12146: /* gsl_vector_set(x, 1, 0.083); */ 12147: gsl_vector_set(x, 0, p[1]); 12148: gsl_vector_set(x, 1, p[2]); 12149: 12150: minex_func.f = &gompertz_f; 12151: minex_func.n = NDIM; 12152: minex_func.params = (void *)&p; /* ??? */ 12153: 12154: sfm = gsl_multimin_fminimizer_alloc (T, NDIM); 12155: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss); 12156: 12157: printf("Iterations beginning .....\n\n"); 12158: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n"); 12159: 12160: iteri=0; 12161: while (rval == GSL_CONTINUE){ 12162: iteri++; 12163: status = gsl_multimin_fminimizer_iterate(sfm); 12164: 12165: if (status) printf("error: %s\n", gsl_strerror (status)); 12166: fflush(0); 12167: 12168: if (status) 12169: break; 12170: 12171: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6); 12172: ssval = gsl_multimin_fminimizer_size (sfm); 12173: 12174: if (rval == GSL_SUCCESS) 12175: printf ("converged to a local maximum at\n"); 12176: 12177: printf("%5d ", iteri); 12178: for (it = 0; it < NDIM; it++){ 12179: printf ("%10.5f ", gsl_vector_get (sfm->x, it)); 12180: } 12181: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval); 12182: } 12183: 12184: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n"); 12185: 12186: gsl_vector_free(x); /* initial values */ 12187: gsl_vector_free(ss); /* inital step size */ 12188: for (it=0; it<NDIM; it++){ 12189: p[it+1]=gsl_vector_get(sfm->x,it); 12190: fprintf(ficrespow," %.12lf", p[it]); 12191: } 12192: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */ 12193: #endif 12194: #ifdef POWELL 12195: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz); 12196: #endif 12197: fclose(ficrespow); 12198: 12199: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz); 12200: 12201: for(i=1; i <=NDIM; i++) 12202: for(j=i+1;j<=NDIM;j++) 12203: matcov[i][j]=matcov[j][i]; 12204: 12205: printf("\nCovariance matrix\n "); 12206: fprintf(ficlog,"\nCovariance matrix\n "); 12207: for(i=1; i <=NDIM; i++) { 12208: for(j=1;j<=NDIM;j++){ 12209: printf("%f ",matcov[i][j]); 12210: fprintf(ficlog,"%f ",matcov[i][j]); 12211: } 12212: printf("\n "); fprintf(ficlog,"\n "); 12213: } 12214: 12215: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp); 12216: for (i=1;i<=NDIM;i++) { 12217: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i])); 12218: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i])); 12219: } 12220: lsurv=vector(agegomp,AGESUP); 12221: lpop=vector(agegomp,AGESUP); 12222: tpop=vector(agegomp,AGESUP); 12223: lsurv[agegomp]=100000; 12224: 12225: for (k=agegomp;k<=AGESUP;k++) { 12226: agemortsup=k; 12227: if (p[1]*exp(p[2]*(k-agegomp))>1) break; 12228: } 12229: 12230: for (k=agegomp;k<agemortsup;k++) 12231: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp))); 12232: 12233: for (k=agegomp;k<agemortsup;k++){ 12234: lpop[k]=(lsurv[k]+lsurv[k+1])/2.; 12235: sumlpop=sumlpop+lpop[k]; 12236: } 12237: 12238: tpop[agegomp]=sumlpop; 12239: for (k=agegomp;k<(agemortsup-3);k++){ 12240: /* tpop[k+1]=2;*/ 12241: tpop[k+1]=tpop[k]-lpop[k]; 12242: } 12243: 12244: 12245: printf("\nAge lx qx dx Lx Tx e(x)\n"); 12246: for (k=agegomp;k<(agemortsup-2);k++) 12247: 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]); 12248: 12249: 12250: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */ 12251: ageminpar=50; 12252: agemaxpar=100; 12253: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){ 12254: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\ 12255: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\ 12256: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar); 12257: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\ 12258: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\ 12259: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar); 12260: }else{ 12261: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar); 12262: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar); 12263: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p); 12264: } 12265: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \ 12266: stepm, weightopt,\ 12267: model,imx,p,matcov,agemortsup); 12268: 12269: free_vector(lsurv,agegomp,AGESUP); 12270: free_vector(lpop,agegomp,AGESUP); 12271: free_vector(tpop,agegomp,AGESUP); 12272: free_matrix(ximort,1,NDIM,1,NDIM); 12273: free_ivector(dcwave,firstobs,lastobs); 12274: free_vector(agecens,firstobs,lastobs); 12275: free_vector(ageexmed,firstobs,lastobs); 12276: free_ivector(cens,firstobs,lastobs); 12277: #ifdef GSL 12278: #endif 12279: } /* Endof if mle==-3 mortality only */ 12280: /* Standard */ 12281: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */ 12282: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */ 12283: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */ 12284: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */ 12285: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw); 12286: for (k=1; k<=npar;k++) 12287: printf(" %d %8.5f",k,p[k]); 12288: printf("\n"); 12289: if(mle>=1){ /* Could be 1 or 2, Real Maximization */ 12290: /* mlikeli uses func not funcone */ 12291: /* for(i=1;i<nlstate;i++){ */ 12292: /* /\*reducing xi for 1 to npar to 1 to ncovmodel; *\/ */ 12293: /* mlikeli(ficres,p, ncovmodel, ncovmodel, nlstate, ftol, funcnoprod); */ 12294: /* } */ 12295: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func); 12296: } 12297: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */ 12298: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */ 12299: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */ 12300: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */ 12301: } 12302: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */ 12303: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */ 12304: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw); 12305: for (k=1; k<=npar;k++) 12306: printf(" %d %8.5f",k,p[k]); 12307: printf("\n"); 12308: 12309: /*--------- results files --------------*/ 12310: /* 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); */ 12311: 12312: 12313: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); 12314: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); 12315: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n"); 12316: for(i=1,jk=1; i <=nlstate; i++){ 12317: for(k=1; k <=(nlstate+ndeath); k++){ 12318: if (k != i) { 12319: printf("%d%d ",i,k); 12320: fprintf(ficlog,"%d%d ",i,k); 12321: fprintf(ficres,"%1d%1d ",i,k); 12322: for(j=1; j <=ncovmodel; j++){ 12323: printf("%12.7f ",p[jk]); 12324: fprintf(ficlog,"%12.7f ",p[jk]); 12325: fprintf(ficres,"%12.7f ",p[jk]); 12326: jk++; 12327: } 12328: printf("\n"); 12329: fprintf(ficlog,"\n"); 12330: fprintf(ficres,"\n"); 12331: } 12332: } 12333: } 12334: if(mle != 0){ 12335: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */ 12336: ftolhess=ftol; /* Usually correct */ 12337: hesscov(matcov, hess, p, npar, delti, ftolhess, func); 12338: 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"); 12339: 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"); 12340: for(i=1,jk=1; i <=nlstate; i++){ 12341: for(k=1; k <=(nlstate+ndeath); k++){ 12342: if (k != i) { 12343: printf("%d%d ",i,k); 12344: fprintf(ficlog,"%d%d ",i,k); 12345: for(j=1; j <=ncovmodel; j++){ 12346: 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])); 12347: 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])); 12348: jk++; 12349: } 12350: printf("\n"); 12351: fprintf(ficlog,"\n"); 12352: } 12353: } 12354: } 12355: } /* end of hesscov and Wald tests */ 12356: 12357: /* */ 12358: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n"); 12359: printf("# Scales (for hessian or gradient estimation)\n"); 12360: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n"); 12361: for(i=1,jk=1; i <=nlstate; i++){ 12362: for(j=1; j <=nlstate+ndeath; j++){ 12363: if (j!=i) { 12364: fprintf(ficres,"%1d%1d",i,j); 12365: printf("%1d%1d",i,j); 12366: fprintf(ficlog,"%1d%1d",i,j); 12367: for(k=1; k<=ncovmodel;k++){ 12368: printf(" %.5e",delti[jk]); 12369: fprintf(ficlog," %.5e",delti[jk]); 12370: fprintf(ficres," %.5e",delti[jk]); 12371: jk++; 12372: } 12373: printf("\n"); 12374: fprintf(ficlog,"\n"); 12375: fprintf(ficres,"\n"); 12376: } 12377: } 12378: } 12379: 12380: 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"); 12381: if(mle >= 1) /* To big for the screen */ 12382: 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"); 12383: 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"); 12384: /* # 121 Var(a12)\n\ */ 12385: /* # 122 Cov(b12,a12) Var(b12)\n\ */ 12386: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */ 12387: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */ 12388: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */ 12389: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */ 12390: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */ 12391: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */ 12392: 12393: 12394: /* Just to have a covariance matrix which will be more understandable 12395: even is we still don't want to manage dictionary of variables 12396: */ 12397: for(itimes=1;itimes<=2;itimes++){ 12398: jj=0; 12399: for(i=1; i <=nlstate; i++){ 12400: for(j=1; j <=nlstate+ndeath; j++){ 12401: if(j==i) continue; 12402: for(k=1; k<=ncovmodel;k++){ 12403: jj++; 12404: ca[0]= k+'a'-1;ca[1]='\0'; 12405: if(itimes==1){ 12406: if(mle>=1) 12407: printf("#%1d%1d%d",i,j,k); 12408: fprintf(ficlog,"#%1d%1d%d",i,j,k); 12409: fprintf(ficres,"#%1d%1d%d",i,j,k); 12410: }else{ 12411: if(mle>=1) 12412: printf("%1d%1d%d",i,j,k); 12413: fprintf(ficlog,"%1d%1d%d",i,j,k); 12414: fprintf(ficres,"%1d%1d%d",i,j,k); 12415: } 12416: ll=0; 12417: for(li=1;li <=nlstate; li++){ 12418: for(lj=1;lj <=nlstate+ndeath; lj++){ 12419: if(lj==li) continue; 12420: for(lk=1;lk<=ncovmodel;lk++){ 12421: ll++; 12422: if(ll<=jj){ 12423: cb[0]= lk +'a'-1;cb[1]='\0'; 12424: if(ll<jj){ 12425: if(itimes==1){ 12426: if(mle>=1) 12427: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj); 12428: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj); 12429: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj); 12430: }else{ 12431: if(mle>=1) 12432: printf(" %.5e",matcov[jj][ll]); 12433: fprintf(ficlog," %.5e",matcov[jj][ll]); 12434: fprintf(ficres," %.5e",matcov[jj][ll]); 12435: } 12436: }else{ 12437: if(itimes==1){ 12438: if(mle>=1) 12439: printf(" Var(%s%1d%1d)",ca,i,j); 12440: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j); 12441: fprintf(ficres," Var(%s%1d%1d)",ca,i,j); 12442: }else{ 12443: if(mle>=1) 12444: printf(" %.7e",matcov[jj][ll]); 12445: fprintf(ficlog," %.7e",matcov[jj][ll]); 12446: fprintf(ficres," %.7e",matcov[jj][ll]); 12447: } 12448: } 12449: } 12450: } /* end lk */ 12451: } /* end lj */ 12452: } /* end li */ 12453: if(mle>=1) 12454: printf("\n"); 12455: fprintf(ficlog,"\n"); 12456: fprintf(ficres,"\n"); 12457: numlinepar++; 12458: } /* end k*/ 12459: } /*end j */ 12460: } /* end i */ 12461: } /* end itimes */ 12462: 12463: fflush(ficlog); 12464: fflush(ficres); 12465: while(fgets(line, MAXLINE, ficpar)) { 12466: /* If line starts with a # it is a comment */ 12467: if (line[0] == '#') { 12468: numlinepar++; 12469: fputs(line,stdout); 12470: fputs(line,ficparo); 12471: fputs(line,ficlog); 12472: fputs(line,ficres); 12473: continue; 12474: }else 12475: break; 12476: } 12477: 12478: /* while((c=getc(ficpar))=='#' && c!= EOF){ */ 12479: /* ungetc(c,ficpar); */ 12480: /* fgets(line, MAXLINE, ficpar); */ 12481: /* fputs(line,stdout); */ 12482: /* fputs(line,ficparo); */ 12483: /* } */ 12484: /* ungetc(c,ficpar); */ 12485: 12486: estepm=0; 12487: 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){ 12488: 12489: if (num_filled != 6) { 12490: 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); 12491: 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); 12492: goto end; 12493: } 12494: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl); 12495: } 12496: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */ 12497: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */ 12498: 12499: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */ 12500: if (estepm==0 || estepm < stepm) estepm=stepm; 12501: if (fage <= 2) { 12502: bage = ageminpar; 12503: fage = agemaxpar; 12504: } 12505: 12506: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n"); 12507: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl); 12508: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl); 12509: 12510: /* Other stuffs, more or less useful */ 12511: while(fgets(line, MAXLINE, ficpar)) { 12512: /* If line starts with a # it is a comment */ 12513: if (line[0] == '#') { 12514: numlinepar++; 12515: fputs(line,stdout); 12516: fputs(line,ficparo); 12517: fputs(line,ficlog); 12518: fputs(line,ficres); 12519: continue; 12520: }else 12521: break; 12522: } 12523: 12524: 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){ 12525: 12526: if (num_filled != 7) { 12527: 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); 12528: 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); 12529: goto end; 12530: } 12531: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav); 12532: 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); 12533: 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); 12534: 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); 12535: } 12536: 12537: while(fgets(line, MAXLINE, ficpar)) { 12538: /* If line starts with a # it is a comment */ 12539: if (line[0] == '#') { 12540: numlinepar++; 12541: fputs(line,stdout); 12542: fputs(line,ficparo); 12543: fputs(line,ficlog); 12544: fputs(line,ficres); 12545: continue; 12546: }else 12547: break; 12548: } 12549: 12550: 12551: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.; 12552: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.; 12553: 12554: if((num_filled=sscanf(line,"pop_based=%d\n",&popbased)) !=EOF){ 12555: if (num_filled != 1) { 12556: 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); 12557: 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); 12558: goto end; 12559: } 12560: printf("pop_based=%d\n",popbased); 12561: fprintf(ficlog,"pop_based=%d\n",popbased); 12562: fprintf(ficparo,"pop_based=%d\n",popbased); 12563: fprintf(ficres,"pop_based=%d\n",popbased); 12564: } 12565: 12566: /* Results */ 12567: endishere=0; 12568: nresult=0; 12569: parameterline=0; 12570: do{ 12571: if(!fgets(line, MAXLINE, ficpar)){ 12572: endishere=1; 12573: parameterline=15; 12574: }else if (line[0] == '#') { 12575: /* If line starts with a # it is a comment */ 12576: numlinepar++; 12577: fputs(line,stdout); 12578: fputs(line,ficparo); 12579: fputs(line,ficlog); 12580: fputs(line,ficres); 12581: continue; 12582: }else if(sscanf(line,"prevforecast=%[^\n]\n",modeltemp)) 12583: parameterline=11; 12584: else if(sscanf(line,"prevbackcast=%[^\n]\n",modeltemp)) 12585: parameterline=12; 12586: else if(sscanf(line,"result:%[^\n]\n",modeltemp)){ 12587: parameterline=13; 12588: } 12589: else{ 12590: parameterline=14; 12591: } 12592: switch (parameterline){ /* =0 only if only comments */ 12593: case 11: 12594: 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)){ 12595: 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); 12596: 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); 12597: 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); 12598: 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); 12599: /* day and month of proj2 are not used but only year anproj2.*/ 12600: dateproj1=anproj1+(mproj1-1)/12.+(jproj1-1)/365.; 12601: dateproj2=anproj2+(mproj2-1)/12.+(jproj2-1)/365.; 12602: prvforecast = 1; 12603: } 12604: else if((num_filled=sscanf(line,"prevforecast=%d yearsfproj=%lf mobil_average=%d\n",&prevfcast,&yrfproj,&mobilavproj)) !=EOF){/* && (num_filled == 3))*/ 12605: printf("prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj); 12606: fprintf(ficlog,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj); 12607: fprintf(ficres,"prevforecast=%d yearsfproj=%.2lf mobil_average=%d\n",prevfcast,yrfproj,mobilavproj); 12608: prvforecast = 2; 12609: } 12610: else { 12611: 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); 12612: 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); 12613: goto end; 12614: } 12615: break; 12616: case 12: 12617: 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)){ 12618: 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); 12619: 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); 12620: 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); 12621: 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); 12622: /* day and month of back2 are not used but only year anback2.*/ 12623: dateback1=anback1+(mback1-1)/12.+(jback1-1)/365.; 12624: dateback2=anback2+(mback2-1)/12.+(jback2-1)/365.; 12625: prvbackcast = 1; 12626: } 12627: else if((num_filled=sscanf(line,"prevbackcast=%d yearsbproj=%lf mobil_average=%d\n",&prevbcast,&yrbproj,&mobilavproj)) ==3){/* && (num_filled == 3))*/ 12628: printf("prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj); 12629: fprintf(ficlog,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj); 12630: fprintf(ficres,"prevbackcast=%d yearsbproj=%.2lf mobil_average=%d\n",prevbcast,yrbproj,mobilavproj); 12631: prvbackcast = 2; 12632: } 12633: else { 12634: 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); 12635: 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); 12636: goto end; 12637: } 12638: break; 12639: case 13: 12640: num_filled=sscanf(line,"result:%[^\n]\n",resultline); 12641: nresult++; /* Sum of resultlines */ 12642: printf("Result %d: result:%s\n",nresult, resultline); 12643: if(nresult > MAXRESULTLINES){ 12644: 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); 12645: 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); 12646: goto end; 12647: } 12648: if(!decoderesult(resultline, nresult)){ /* Fills TKresult[nresult] combination and Tresult[nresult][k4+1] combination values */ 12649: fprintf(ficparo,"result: %s\n",resultline); 12650: fprintf(ficres,"result: %s\n",resultline); 12651: fprintf(ficlog,"result: %s\n",resultline); 12652: } else 12653: goto end; 12654: break; 12655: case 14: 12656: printf("Error: Unknown command '%s'\n",line); 12657: fprintf(ficlog,"Error: Unknown command '%s'\n",line); 12658: if(line[0] == ' ' || line[0] == '\n'){ 12659: printf("It should not be an empty line '%s'\n",line); 12660: fprintf(ficlog,"It should not be an empty line '%s'\n",line); 12661: } 12662: if(ncovmodel >=2 && nresult==0 ){ 12663: printf("ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line); 12664: fprintf(ficlog,"ERROR: no result lines! It should be at minimum 'result: V2=0 V1=1 or result:.' %s\n",line); 12665: } 12666: /* goto end; */ 12667: break; 12668: case 15: 12669: printf("End of resultlines.\n"); 12670: fprintf(ficlog,"End of resultlines.\n"); 12671: break; 12672: default: /* parameterline =0 */ 12673: nresult=1; 12674: decoderesult(".",nresult ); /* No covariate */ 12675: } /* End switch parameterline */ 12676: }while(endishere==0); /* End do */ 12677: 12678: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */ 12679: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */ 12680: 12681: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */ 12682: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){ 12683: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\ 12684: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\ 12685: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar); 12686: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\ 12687: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\ 12688: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar); 12689: }else{ 12690: /* printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p, (int)anproj1-(int)agemin, (int)anback1-(int)agemax+1); */ 12691: /* It seems that anprojd which is computed from the mean year at interview which is known yet because of freqsummary */ 12692: /* date2dmy(dateintmean,&jintmean,&mintmean,&aintmean); */ /* Done in freqsummary */ 12693: if(prvforecast==1){ 12694: dateprojd=(jproj1+12*mproj1+365*anproj1)/365; 12695: jprojd=jproj1; 12696: mprojd=mproj1; 12697: anprojd=anproj1; 12698: dateprojf=(jproj2+12*mproj2+365*anproj2)/365; 12699: jprojf=jproj2; 12700: mprojf=mproj2; 12701: anprojf=anproj2; 12702: } else if(prvforecast == 2){ 12703: dateprojd=dateintmean; 12704: date2dmy(dateprojd,&jprojd, &mprojd, &anprojd); 12705: dateprojf=dateintmean+yrfproj; 12706: date2dmy(dateprojf,&jprojf, &mprojf, &anprojf); 12707: } 12708: if(prvbackcast==1){ 12709: datebackd=(jback1+12*mback1+365*anback1)/365; 12710: jbackd=jback1; 12711: mbackd=mback1; 12712: anbackd=anback1; 12713: datebackf=(jback2+12*mback2+365*anback2)/365; 12714: jbackf=jback2; 12715: mbackf=mback2; 12716: anbackf=anback2; 12717: } else if(prvbackcast == 2){ 12718: datebackd=dateintmean; 12719: date2dmy(datebackd,&jbackd, &mbackd, &anbackd); 12720: datebackf=dateintmean-yrbproj; 12721: date2dmy(datebackf,&jbackf, &mbackf, &anbackf); 12722: } 12723: 12724: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,bage, fage, prevfcast, prevbcast, pathc,p, (int)anprojd-bage, (int)anbackd-fage); 12725: } 12726: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \ 12727: model,imx,jmin,jmax,jmean,rfileres,popforecast,mobilav,prevfcast,mobilavproj,prevbcast, estepm, \ 12728: jprev1,mprev1,anprev1,dateprev1, dateprojd, datebackd,jprev2,mprev2,anprev2,dateprev2,dateprojf, datebackf); 12729: 12730: /*------------ free_vector -------------*/ 12731: /* chdir(path); */ 12732: 12733: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */ 12734: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */ 12735: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */ 12736: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */ 12737: free_lvector(num,firstobs,lastobs); 12738: free_vector(agedc,firstobs,lastobs); 12739: /*free_matrix(covar,0,NCOVMAX,1,n);*/ 12740: /*free_matrix(covar,1,NCOVMAX,1,n);*/ 12741: fclose(ficparo); 12742: fclose(ficres); 12743: 12744: 12745: /* Other results (useful)*/ 12746: 12747: 12748: /*--------------- Prevalence limit (period or stable prevalence) --------------*/ 12749: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */ 12750: prlim=matrix(1,nlstate,1,nlstate); 12751: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear); 12752: fclose(ficrespl); 12753: 12754: /*------------- h Pij x at various ages ------------*/ 12755: /*#include "hpijx.h"*/ 12756: hPijx(p, bage, fage); 12757: fclose(ficrespij); 12758: 12759: /* ncovcombmax= pow(2,cptcoveff); */ 12760: /*-------------- Variance of one-step probabilities---*/ 12761: k=1; 12762: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart); 12763: 12764: /* Prevalence for each covariate combination in probs[age][status][cov] */ 12765: probs= ma3x(AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax); 12766: for(i=AGEINF;i<=AGESUP;i++) 12767: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */ 12768: for(k=1;k<=ncovcombmax;k++) 12769: probs[i][j][k]=0.; 12770: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, 12771: ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); 12772: if (mobilav!=0 ||mobilavproj !=0 ) { 12773: mobaverages= ma3x(AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); 12774: for(i=AGEINF;i<=AGESUP;i++) 12775: for(j=1;j<=nlstate+ndeath;j++) 12776: for(k=1;k<=ncovcombmax;k++) 12777: mobaverages[i][j][k]=0.; 12778: mobaverage=mobaverages; 12779: if (mobilav!=0) { 12780: printf("Movingaveraging observed prevalence\n"); 12781: fprintf(ficlog,"Movingaveraging observed prevalence\n"); 12782: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){ 12783: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); 12784: printf(" Error in movingaverage mobilav=%d\n",mobilav); 12785: } 12786: } else if (mobilavproj !=0) { 12787: printf("Movingaveraging projected observed prevalence\n"); 12788: fprintf(ficlog,"Movingaveraging projected observed prevalence\n"); 12789: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){ 12790: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj); 12791: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj); 12792: } 12793: }else{ 12794: printf("Internal error moving average\n"); 12795: fflush(stdout); 12796: exit(1); 12797: } 12798: }/* end if moving average */ 12799: 12800: /*---------- Forecasting ------------------*/ 12801: if(prevfcast==1){ 12802: /* /\* if(stepm ==1){*\/ */ 12803: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */ 12804: /*This done previously after freqsummary.*/ 12805: /* dateprojd=(jproj1+12*mproj1+365*anproj1)/365; */ 12806: /* dateprojf=(jproj2+12*mproj2+365*anproj2)/365; */ 12807: 12808: /* } else if (prvforecast==2){ */ 12809: /* /\* if(stepm ==1){*\/ */ 12810: /* /\* anproj1, mproj1, jproj1 either read explicitly or yrfproj *\/ */ 12811: /* } */ 12812: /*prevforecast(fileresu, dateintmean, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);*/ 12813: prevforecast(fileresu,dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, mobilavproj, mobaverage, bage, fage, firstpass, lastpass, p, cptcoveff); 12814: } 12815: 12816: /* Prevbcasting */ 12817: if(prevbcast==1){ 12818: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath); 12819: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath); 12820: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath); 12821: 12822: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/ 12823: 12824: bprlim=matrix(1,nlstate,1,nlstate); 12825: 12826: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj); 12827: fclose(ficresplb); 12828: 12829: hBijx(p, bage, fage, mobaverage); 12830: fclose(ficrespijb); 12831: 12832: /* /\* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, *\/ */ 12833: /* /\* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); *\/ */ 12834: /* prevbackforecast(fileresu, mobaverage, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, */ 12835: /* mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */ 12836: prevbackforecast(fileresu, mobaverage, dateintmean, dateprojd, dateprojf, agemin, agemax, dateprev1, dateprev2, 12837: mobilavproj, bage, fage, firstpass, lastpass, p, cptcoveff); 12838: 12839: 12840: varbprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, bprlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff); 12841: 12842: 12843: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */ 12844: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath); 12845: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath); 12846: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath); 12847: } /* end Prevbcasting */ 12848: 12849: 12850: /* ------ Other prevalence ratios------------ */ 12851: 12852: free_ivector(wav,1,imx); 12853: free_imatrix(dh,1,lastpass-firstpass+2,1,imx); 12854: free_imatrix(bh,1,lastpass-firstpass+2,1,imx); 12855: free_imatrix(mw,1,lastpass-firstpass+2,1,imx); 12856: 12857: 12858: /*---------- Health expectancies, no variances ------------*/ 12859: 12860: strcpy(filerese,"E_"); 12861: strcat(filerese,fileresu); 12862: if((ficreseij=fopen(filerese,"w"))==NULL) { 12863: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0); 12864: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0); 12865: } 12866: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout); 12867: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog); 12868: 12869: pstamp(ficreseij); 12870: 12871: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */ 12872: if (cptcovn < 1){i1=1;} 12873: 12874: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 12875: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */ 12876: if(i1 != 1 && TKresult[nres]!= k) 12877: continue; 12878: fprintf(ficreseij,"\n#****** "); 12879: printf("\n#****** "); 12880: for(j=1;j<=cptcoveff;j++) { 12881: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12882: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12883: } 12884: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 12885: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12886: fprintf(ficreseij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12887: } 12888: fprintf(ficreseij,"******\n"); 12889: printf("******\n"); 12890: 12891: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage); 12892: oldm=oldms;savm=savms; 12893: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart, nres); 12894: 12895: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage); 12896: } 12897: fclose(ficreseij); 12898: printf("done evsij\n");fflush(stdout); 12899: fprintf(ficlog,"done evsij\n");fflush(ficlog); 12900: 12901: 12902: /*---------- State-specific expectancies and variances ------------*/ 12903: 12904: strcpy(filerest,"T_"); 12905: strcat(filerest,fileresu); 12906: if((ficrest=fopen(filerest,"w"))==NULL) { 12907: printf("Problem with total LE resultfile: %s\n", filerest);goto end; 12908: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end; 12909: } 12910: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout); 12911: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog); 12912: strcpy(fileresstde,"STDE_"); 12913: strcat(fileresstde,fileresu); 12914: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) { 12915: printf("Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0); 12916: fprintf(ficlog,"Problem with State specific Exp. and std errors resultfile: %s\n", fileresstde); exit(0); 12917: } 12918: printf(" Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde); 12919: fprintf(ficlog," Computing State-specific Expectancies and standard errors: result on file '%s' \n", fileresstde); 12920: 12921: strcpy(filerescve,"CVE_"); 12922: strcat(filerescve,fileresu); 12923: if((ficrescveij=fopen(filerescve,"w"))==NULL) { 12924: printf("Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0); 12925: fprintf(ficlog,"Problem with Covar. State-specific Exp. resultfile: %s\n", filerescve); exit(0); 12926: } 12927: printf(" Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve); 12928: fprintf(ficlog," Computing Covar. of State-specific Expectancies: result on file '%s' \n", filerescve); 12929: 12930: strcpy(fileresv,"V_"); 12931: strcat(fileresv,fileresu); 12932: if((ficresvij=fopen(fileresv,"w"))==NULL) { 12933: printf("Problem with variance resultfile: %s\n", fileresv);exit(0); 12934: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0); 12935: } 12936: printf(" Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(stdout); 12937: fprintf(ficlog," Computing Variance-covariance of State-specific Expectancies: file '%s' ... ", fileresv);fflush(ficlog); 12938: 12939: i1=pow(2,cptcoveff); /* Number of combination of dummy covariates */ 12940: if (cptcovn < 1){i1=1;} 12941: 12942: for(nres=1; nres <= nresult; nres++) /* For each resultline */ 12943: for(k=1; k<=i1;k++){ /* For any combination of dummy covariates, fixed and varying */ 12944: if(i1 != 1 && TKresult[nres]!= k) 12945: continue; 12946: printf("\n#****** Result for:"); 12947: fprintf(ficrest,"\n#****** Result for:"); 12948: fprintf(ficlog,"\n#****** Result for:"); 12949: for(j=1;j<=cptcoveff;j++){ 12950: printf("V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12951: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12952: fprintf(ficlog,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12953: } 12954: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 12955: printf(" V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12956: fprintf(ficrest," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12957: fprintf(ficlog," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12958: } 12959: fprintf(ficrest,"******\n"); 12960: fprintf(ficlog,"******\n"); 12961: printf("******\n"); 12962: 12963: fprintf(ficresstdeij,"\n#****** "); 12964: fprintf(ficrescveij,"\n#****** "); 12965: for(j=1;j<=cptcoveff;j++) { 12966: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12967: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12968: } 12969: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 12970: fprintf(ficresstdeij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12971: fprintf(ficrescveij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12972: } 12973: fprintf(ficresstdeij,"******\n"); 12974: fprintf(ficrescveij,"******\n"); 12975: 12976: fprintf(ficresvij,"\n#****** "); 12977: /* pstamp(ficresvij); */ 12978: for(j=1;j<=cptcoveff;j++) 12979: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); 12980: for (j=1; j<= nsq; j++){ /* For each selected (single) quantitative value */ 12981: fprintf(ficresvij," V%d=%f ",Tvqresult[nres][j],Tqresult[nres][j]); 12982: } 12983: fprintf(ficresvij,"******\n"); 12984: 12985: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage); 12986: oldm=oldms;savm=savms; 12987: printf(" cvevsij "); 12988: fprintf(ficlog, " cvevsij "); 12989: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart, nres); 12990: printf(" end cvevsij \n "); 12991: fprintf(ficlog, " end cvevsij \n "); 12992: 12993: /* 12994: */ 12995: /* goto endfree; */ 12996: 12997: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage); 12998: pstamp(ficrest); 12999: 13000: epj=vector(1,nlstate+1); 13001: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/ 13002: oldm=oldms;savm=savms; /* ZZ Segmentation fault */ 13003: cptcod= 0; /* To be deleted */ 13004: printf("varevsij vpopbased=%d \n",vpopbased); 13005: fprintf(ficlog, "varevsij vpopbased=%d \n",vpopbased); 13006: 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 */ 13007: 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 "); 13008: if(vpopbased==1) 13009: 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); 13010: else 13011: fprintf(ficrest,"the age specific forward period (stable) prevalences in each health state \n"); 13012: fprintf(ficrest,"# Age popbased mobilav e.. (std) "); 13013: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i); 13014: fprintf(ficrest,"\n"); 13015: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */ 13016: printf("Computing age specific forward period (stable) prevalences in each health state \n"); 13017: fprintf(ficlog,"Computing age specific forward period (stable) prevalences in each health state \n"); 13018: for(age=bage; age <=fage ;age++){ 13019: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k, nres); /*ZZ Is it the correct prevalim */ 13020: if (vpopbased==1) { 13021: if(mobilav ==0){ 13022: for(i=1; i<=nlstate;i++) 13023: prlim[i][i]=probs[(int)age][i][k]; 13024: }else{ /* mobilav */ 13025: for(i=1; i<=nlstate;i++) 13026: prlim[i][i]=mobaverage[(int)age][i][k]; 13027: } 13028: } 13029: 13030: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav); 13031: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */ 13032: /* printf(" age %4.0f ",age); */ 13033: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){ 13034: for(i=1, epj[j]=0.;i <=nlstate;i++) { 13035: epj[j] += prlim[i][i]*eij[i][j][(int)age]; 13036: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/ 13037: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */ 13038: } 13039: epj[nlstate+1] +=epj[j]; 13040: } 13041: /* printf(" age %4.0f \n",age); */ 13042: 13043: for(i=1, vepp=0.;i <=nlstate;i++) 13044: for(j=1;j <=nlstate;j++) 13045: vepp += vareij[i][j][(int)age]; 13046: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp)); 13047: for(j=1;j <=nlstate;j++){ 13048: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age])); 13049: } 13050: fprintf(ficrest,"\n"); 13051: } 13052: } /* End vpopbased */ 13053: free_vector(epj,1,nlstate+1); 13054: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage); 13055: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage); 13056: printf("done selection\n");fflush(stdout); 13057: fprintf(ficlog,"done selection\n");fflush(ficlog); 13058: 13059: } /* End k selection */ 13060: 13061: printf("done State-specific expectancies\n");fflush(stdout); 13062: fprintf(ficlog,"done State-specific expectancies\n");fflush(ficlog); 13063: 13064: /* variance-covariance of forward period prevalence*/ 13065: varprlim(fileresu, nresult, mobaverage, mobilavproj, bage, fage, prlim, &ncvyear, ftolpl, p, matcov, delti, stepm, cptcoveff); 13066: 13067: 13068: free_vector(weight,firstobs,lastobs); 13069: free_imatrix(Tvard,1,NCOVMAX,1,2); 13070: free_imatrix(s,1,maxwav+1,firstobs,lastobs); 13071: free_matrix(anint,1,maxwav,firstobs,lastobs); 13072: free_matrix(mint,1,maxwav,firstobs,lastobs); 13073: free_ivector(cod,firstobs,lastobs); 13074: free_ivector(tab,1,NCOVMAX); 13075: fclose(ficresstdeij); 13076: fclose(ficrescveij); 13077: fclose(ficresvij); 13078: fclose(ficrest); 13079: fclose(ficpar); 13080: 13081: 13082: /*---------- End : free ----------------*/ 13083: if (mobilav!=0 ||mobilavproj !=0) 13084: free_ma3x(mobaverages,AGEINF, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */ 13085: free_ma3x(probs,AGEINF,AGESUP,1,nlstate+ndeath, 1,ncovcombmax); 13086: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */ 13087: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath); 13088: } /* mle==-3 arrives here for freeing */ 13089: /* endfree:*/ 13090: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath); 13091: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath); 13092: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath); 13093: if(ntv+nqtv>=1)free_ma3x(cotvar,1,maxwav,1,ntv+nqtv,firstobs,lastobs); 13094: if(nqtv>=1)free_ma3x(cotqvar,1,maxwav,1,nqtv,firstobs,lastobs); 13095: if(nqv>=1)free_matrix(coqvar,1,nqv,firstobs,lastobs); 13096: free_matrix(covar,0,NCOVMAX,firstobs,lastobs); 13097: free_matrix(matcov,1,npar,1,npar); 13098: free_matrix(hess,1,npar,1,npar); 13099: /*free_vector(delti,1,npar);*/ 13100: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel); 13101: free_matrix(agev,1,maxwav,1,imx); 13102: free_ma3x(paramstart,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel); 13103: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel); 13104: 13105: free_ivector(ncodemax,1,NCOVMAX); 13106: free_ivector(ncodemaxwundef,1,NCOVMAX); 13107: free_ivector(Dummy,-1,NCOVMAX); 13108: free_ivector(Fixed,-1,NCOVMAX); 13109: free_ivector(DummyV,1,NCOVMAX); 13110: free_ivector(FixedV,1,NCOVMAX); 13111: free_ivector(Typevar,-1,NCOVMAX); 13112: free_ivector(Tvar,1,NCOVMAX); 13113: free_ivector(TvarsQ,1,NCOVMAX); 13114: free_ivector(TvarsQind,1,NCOVMAX); 13115: free_ivector(TvarsD,1,NCOVMAX); 13116: free_ivector(TvarsDind,1,NCOVMAX); 13117: free_ivector(TvarFD,1,NCOVMAX); 13118: free_ivector(TvarFDind,1,NCOVMAX); 13119: free_ivector(TvarF,1,NCOVMAX); 13120: free_ivector(TvarFind,1,NCOVMAX); 13121: free_ivector(TvarV,1,NCOVMAX); 13122: free_ivector(TvarVind,1,NCOVMAX); 13123: free_ivector(TvarA,1,NCOVMAX); 13124: free_ivector(TvarAind,1,NCOVMAX); 13125: free_ivector(TvarFQ,1,NCOVMAX); 13126: free_ivector(TvarFQind,1,NCOVMAX); 13127: free_ivector(TvarVD,1,NCOVMAX); 13128: free_ivector(TvarVDind,1,NCOVMAX); 13129: free_ivector(TvarVQ,1,NCOVMAX); 13130: free_ivector(TvarVQind,1,NCOVMAX); 13131: free_ivector(Tvarsel,1,NCOVMAX); 13132: free_vector(Tvalsel,1,NCOVMAX); 13133: free_ivector(Tposprod,1,NCOVMAX); 13134: free_ivector(Tprod,1,NCOVMAX); 13135: free_ivector(Tvaraff,1,NCOVMAX); 13136: free_ivector(invalidvarcomb,1,ncovcombmax); 13137: free_ivector(Tage,1,NCOVMAX); 13138: free_ivector(Tmodelind,1,NCOVMAX); 13139: free_ivector(TmodelInvind,1,NCOVMAX); 13140: free_ivector(TmodelInvQind,1,NCOVMAX); 13141: 13142: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX); 13143: /* free_imatrix(codtab,1,100,1,10); */ 13144: fflush(fichtm); 13145: fflush(ficgp); 13146: 13147: 13148: if((nberr >0) || (nbwarn>0)){ 13149: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn); 13150: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn); 13151: }else{ 13152: printf("End of Imach\n"); 13153: fprintf(ficlog,"End of Imach\n"); 13154: } 13155: printf("See log file on %s\n",filelog); 13156: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */ 13157: /*(void) gettimeofday(&end_time,&tzp);*/ 13158: rend_time = time(NULL); 13159: end_time = *localtime(&rend_time); 13160: /* tml = *localtime(&end_time.tm_sec); */ 13161: strcpy(strtend,asctime(&end_time)); 13162: printf("Local time at start %s\nLocal time at end %s",strstart, strtend); 13163: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend); 13164: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout)); 13165: 13166: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time)); 13167: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout)); 13168: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time)); 13169: /* printf("Total time was %d uSec.\n", total_usecs);*/ 13170: /* if(fileappend(fichtm,optionfilehtm)){ */ 13171: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend); 13172: fclose(fichtm); 13173: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend); 13174: fclose(fichtmcov); 13175: fclose(ficgp); 13176: fclose(ficlog); 13177: /*------ End -----------*/ 13178: 13179: 13180: /* Executes gnuplot */ 13181: 13182: printf("Before Current directory %s!\n",pathcd); 13183: #ifdef WIN32 13184: if (_chdir(pathcd) != 0) 13185: printf("Can't move to directory %s!\n",path); 13186: if(_getcwd(pathcd,MAXLINE) > 0) 13187: #else 13188: if(chdir(pathcd) != 0) 13189: printf("Can't move to directory %s!\n", path); 13190: if (getcwd(pathcd, MAXLINE) > 0) 13191: #endif 13192: printf("Current directory %s!\n",pathcd); 13193: /*strcat(plotcmd,CHARSEPARATOR);*/ 13194: sprintf(plotcmd,"gnuplot"); 13195: #ifdef _WIN32 13196: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach); 13197: #endif 13198: if(!stat(plotcmd,&info)){ 13199: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout); 13200: if(!stat(getenv("GNUPLOTBIN"),&info)){ 13201: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout); 13202: }else 13203: strcpy(pplotcmd,plotcmd); 13204: #ifdef __unix 13205: strcpy(plotcmd,GNUPLOTPROGRAM); 13206: if(!stat(plotcmd,&info)){ 13207: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout); 13208: }else 13209: strcpy(pplotcmd,plotcmd); 13210: #endif 13211: }else 13212: strcpy(pplotcmd,plotcmd); 13213: 13214: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot); 13215: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout); 13216: strcpy(pplotcmd,plotcmd); 13217: 13218: if((outcmd=system(plotcmd)) != 0){ 13219: printf("Error in gnuplot, command might not be in your path: '%s', err=%d\n", plotcmd, outcmd); 13220: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n"); 13221: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot); 13222: if((outcmd=system(plotcmd)) != 0){ 13223: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd); 13224: strcpy(plotcmd,pplotcmd); 13225: } 13226: } 13227: printf(" Successful, please wait..."); 13228: while (z[0] != 'q') { 13229: /* chdir(path); */ 13230: printf("\nType e to edit results with your browser, g to graph again and q for exit: "); 13231: scanf("%s",z); 13232: /* if (z[0] == 'c') system("./imach"); */ 13233: if (z[0] == 'e') { 13234: #ifdef __APPLE__ 13235: sprintf(pplotcmd, "open %s", optionfilehtm); 13236: #elif __linux 13237: sprintf(pplotcmd, "xdg-open %s", optionfilehtm); 13238: #else 13239: sprintf(pplotcmd, "%s", optionfilehtm); 13240: #endif 13241: printf("Starting browser with: %s",pplotcmd);fflush(stdout); 13242: system(pplotcmd); 13243: } 13244: else if (z[0] == 'g') system(plotcmd); 13245: else if (z[0] == 'q') exit(0); 13246: } 13247: end: 13248: while (z[0] != 'q') { 13249: printf("\nType q for exiting: "); fflush(stdout); 13250: scanf("%s",z); 13251: } 13252: printf("End\n"); 13253: exit(0); 13254: }