Annotation of imach/src/imach.c, revision 1.225
1.225 ! brouard 1: /* $Id: imach.c,v 1.224 2016/07/01 13:16:01 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.225 ! brouard 4: Revision 1.224 2016/07/01 13:16:01 brouard
! 5: Summary: Fixes
! 6:
1.224 brouard 7: Revision 1.223 2016/02/19 09:23:35 brouard
8: Summary: temporary
9:
1.223 brouard 10: Revision 1.222 2016/02/17 08:14:50 brouard
11: Summary: Probably last 0.98 stable version 0.98r6
12:
1.222 brouard 13: Revision 1.221 2016/02/15 23:35:36 brouard
14: Summary: minor bug
15:
1.220 brouard 16: Revision 1.219 2016/02/15 00:48:12 brouard
17: *** empty log message ***
18:
1.219 brouard 19: Revision 1.218 2016/02/12 11:29:23 brouard
20: Summary: 0.99 Back projections
21:
1.218 brouard 22: Revision 1.217 2015/12/23 17:18:31 brouard
23: Summary: Experimental backcast
24:
1.217 brouard 25: Revision 1.216 2015/12/18 17:32:11 brouard
26: Summary: 0.98r4 Warning and status=-2
27:
28: Version 0.98r4 is now:
29: - displaying an error when status is -1, date of interview unknown and date of death known;
30: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
31: Older changes concerning s=-2, dating from 2005 have been supersed.
32:
1.216 brouard 33: Revision 1.215 2015/12/16 08:52:24 brouard
34: Summary: 0.98r4 working
35:
1.215 brouard 36: Revision 1.214 2015/12/16 06:57:54 brouard
37: Summary: temporary not working
38:
1.214 brouard 39: Revision 1.213 2015/12/11 18:22:17 brouard
40: Summary: 0.98r4
41:
1.213 brouard 42: Revision 1.212 2015/11/21 12:47:24 brouard
43: Summary: minor typo
44:
1.212 brouard 45: Revision 1.211 2015/11/21 12:41:11 brouard
46: Summary: 0.98r3 with some graph of projected cross-sectional
47:
48: Author: Nicolas Brouard
49:
1.211 brouard 50: Revision 1.210 2015/11/18 17:41:20 brouard
51: Summary: Start working on projected prevalences
52:
1.210 brouard 53: Revision 1.209 2015/11/17 22:12:03 brouard
54: Summary: Adding ftolpl parameter
55: Author: N Brouard
56:
57: We had difficulties to get smoothed confidence intervals. It was due
58: to the period prevalence which wasn't computed accurately. The inner
59: parameter ftolpl is now an outer parameter of the .imach parameter
60: file after estepm. If ftolpl is small 1.e-4 and estepm too,
61: computation are long.
62:
1.209 brouard 63: Revision 1.208 2015/11/17 14:31:57 brouard
64: Summary: temporary
65:
1.208 brouard 66: Revision 1.207 2015/10/27 17:36:57 brouard
67: *** empty log message ***
68:
1.207 brouard 69: Revision 1.206 2015/10/24 07:14:11 brouard
70: *** empty log message ***
71:
1.206 brouard 72: Revision 1.205 2015/10/23 15:50:53 brouard
73: Summary: 0.98r3 some clarification for graphs on likelihood contributions
74:
1.205 brouard 75: Revision 1.204 2015/10/01 16:20:26 brouard
76: Summary: Some new graphs of contribution to likelihood
77:
1.204 brouard 78: Revision 1.203 2015/09/30 17:45:14 brouard
79: Summary: looking at better estimation of the hessian
80:
81: Also a better criteria for convergence to the period prevalence And
82: therefore adding the number of years needed to converge. (The
83: prevalence in any alive state shold sum to one
84:
1.203 brouard 85: Revision 1.202 2015/09/22 19:45:16 brouard
86: Summary: Adding some overall graph on contribution to likelihood. Might change
87:
1.202 brouard 88: Revision 1.201 2015/09/15 17:34:58 brouard
89: Summary: 0.98r0
90:
91: - Some new graphs like suvival functions
92: - Some bugs fixed like model=1+age+V2.
93:
1.201 brouard 94: Revision 1.200 2015/09/09 16:53:55 brouard
95: Summary: Big bug thanks to Flavia
96:
97: Even model=1+age+V2. did not work anymore
98:
1.200 brouard 99: Revision 1.199 2015/09/07 14:09:23 brouard
100: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
101:
1.199 brouard 102: Revision 1.198 2015/09/03 07:14:39 brouard
103: Summary: 0.98q5 Flavia
104:
1.198 brouard 105: Revision 1.197 2015/09/01 18:24:39 brouard
106: *** empty log message ***
107:
1.197 brouard 108: Revision 1.196 2015/08/18 23:17:52 brouard
109: Summary: 0.98q5
110:
1.196 brouard 111: Revision 1.195 2015/08/18 16:28:39 brouard
112: Summary: Adding a hack for testing purpose
113:
114: After reading the title, ftol and model lines, if the comment line has
115: a q, starting with #q, the answer at the end of the run is quit. It
116: permits to run test files in batch with ctest. The former workaround was
117: $ echo q | imach foo.imach
118:
1.195 brouard 119: Revision 1.194 2015/08/18 13:32:00 brouard
120: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
121:
1.194 brouard 122: Revision 1.193 2015/08/04 07:17:42 brouard
123: Summary: 0.98q4
124:
1.193 brouard 125: Revision 1.192 2015/07/16 16:49:02 brouard
126: Summary: Fixing some outputs
127:
1.192 brouard 128: Revision 1.191 2015/07/14 10:00:33 brouard
129: Summary: Some fixes
130:
1.191 brouard 131: Revision 1.190 2015/05/05 08:51:13 brouard
132: Summary: Adding digits in output parameters (7 digits instead of 6)
133:
134: Fix 1+age+.
135:
1.190 brouard 136: Revision 1.189 2015/04/30 14:45:16 brouard
137: Summary: 0.98q2
138:
1.189 brouard 139: Revision 1.188 2015/04/30 08:27:53 brouard
140: *** empty log message ***
141:
1.188 brouard 142: Revision 1.187 2015/04/29 09:11:15 brouard
143: *** empty log message ***
144:
1.187 brouard 145: Revision 1.186 2015/04/23 12:01:52 brouard
146: Summary: V1*age is working now, version 0.98q1
147:
148: Some codes had been disabled in order to simplify and Vn*age was
149: working in the optimization phase, ie, giving correct MLE parameters,
150: but, as usual, outputs were not correct and program core dumped.
151:
1.186 brouard 152: Revision 1.185 2015/03/11 13:26:42 brouard
153: Summary: Inclusion of compile and links command line for Intel Compiler
154:
1.185 brouard 155: Revision 1.184 2015/03/11 11:52:39 brouard
156: Summary: Back from Windows 8. Intel Compiler
157:
1.184 brouard 158: Revision 1.183 2015/03/10 20:34:32 brouard
159: Summary: 0.98q0, trying with directest, mnbrak fixed
160:
161: We use directest instead of original Powell test; probably no
162: incidence on the results, but better justifications;
163: We fixed Numerical Recipes mnbrak routine which was wrong and gave
164: wrong results.
165:
1.183 brouard 166: Revision 1.182 2015/02/12 08:19:57 brouard
167: Summary: Trying to keep directest which seems simpler and more general
168: Author: Nicolas Brouard
169:
1.182 brouard 170: Revision 1.181 2015/02/11 23:22:24 brouard
171: Summary: Comments on Powell added
172:
173: Author:
174:
1.181 brouard 175: Revision 1.180 2015/02/11 17:33:45 brouard
176: Summary: Finishing move from main to function (hpijx and prevalence_limit)
177:
1.180 brouard 178: Revision 1.179 2015/01/04 09:57:06 brouard
179: Summary: back to OS/X
180:
1.179 brouard 181: Revision 1.178 2015/01/04 09:35:48 brouard
182: *** empty log message ***
183:
1.178 brouard 184: Revision 1.177 2015/01/03 18:40:56 brouard
185: Summary: Still testing ilc32 on OSX
186:
1.177 brouard 187: Revision 1.176 2015/01/03 16:45:04 brouard
188: *** empty log message ***
189:
1.176 brouard 190: Revision 1.175 2015/01/03 16:33:42 brouard
191: *** empty log message ***
192:
1.175 brouard 193: Revision 1.174 2015/01/03 16:15:49 brouard
194: Summary: Still in cross-compilation
195:
1.174 brouard 196: Revision 1.173 2015/01/03 12:06:26 brouard
197: Summary: trying to detect cross-compilation
198:
1.173 brouard 199: Revision 1.172 2014/12/27 12:07:47 brouard
200: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
201:
1.172 brouard 202: Revision 1.171 2014/12/23 13:26:59 brouard
203: Summary: Back from Visual C
204:
205: Still problem with utsname.h on Windows
206:
1.171 brouard 207: Revision 1.170 2014/12/23 11:17:12 brouard
208: Summary: Cleaning some \%% back to %%
209:
210: The escape was mandatory for a specific compiler (which one?), but too many warnings.
211:
1.170 brouard 212: Revision 1.169 2014/12/22 23:08:31 brouard
213: Summary: 0.98p
214:
215: Outputs some informations on compiler used, OS etc. Testing on different platforms.
216:
1.169 brouard 217: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 218: Summary: update
1.169 brouard 219:
1.168 brouard 220: Revision 1.167 2014/12/22 13:50:56 brouard
221: Summary: Testing uname and compiler version and if compiled 32 or 64
222:
223: Testing on Linux 64
224:
1.167 brouard 225: Revision 1.166 2014/12/22 11:40:47 brouard
226: *** empty log message ***
227:
1.166 brouard 228: Revision 1.165 2014/12/16 11:20:36 brouard
229: Summary: After compiling on Visual C
230:
231: * imach.c (Module): Merging 1.61 to 1.162
232:
1.165 brouard 233: Revision 1.164 2014/12/16 10:52:11 brouard
234: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
235:
236: * imach.c (Module): Merging 1.61 to 1.162
237:
1.164 brouard 238: Revision 1.163 2014/12/16 10:30:11 brouard
239: * imach.c (Module): Merging 1.61 to 1.162
240:
1.163 brouard 241: Revision 1.162 2014/09/25 11:43:39 brouard
242: Summary: temporary backup 0.99!
243:
1.162 brouard 244: Revision 1.1 2014/09/16 11:06:58 brouard
245: Summary: With some code (wrong) for nlopt
246:
247: Author:
248:
249: Revision 1.161 2014/09/15 20:41:41 brouard
250: Summary: Problem with macro SQR on Intel compiler
251:
1.161 brouard 252: Revision 1.160 2014/09/02 09:24:05 brouard
253: *** empty log message ***
254:
1.160 brouard 255: Revision 1.159 2014/09/01 10:34:10 brouard
256: Summary: WIN32
257: Author: Brouard
258:
1.159 brouard 259: Revision 1.158 2014/08/27 17:11:51 brouard
260: *** empty log message ***
261:
1.158 brouard 262: Revision 1.157 2014/08/27 16:26:55 brouard
263: Summary: Preparing windows Visual studio version
264: Author: Brouard
265:
266: In order to compile on Visual studio, time.h is now correct and time_t
267: and tm struct should be used. difftime should be used but sometimes I
268: just make the differences in raw time format (time(&now).
269: Trying to suppress #ifdef LINUX
270: Add xdg-open for __linux in order to open default browser.
271:
1.157 brouard 272: Revision 1.156 2014/08/25 20:10:10 brouard
273: *** empty log message ***
274:
1.156 brouard 275: Revision 1.155 2014/08/25 18:32:34 brouard
276: Summary: New compile, minor changes
277: Author: Brouard
278:
1.155 brouard 279: Revision 1.154 2014/06/20 17:32:08 brouard
280: Summary: Outputs now all graphs of convergence to period prevalence
281:
1.154 brouard 282: Revision 1.153 2014/06/20 16:45:46 brouard
283: Summary: If 3 live state, convergence to period prevalence on same graph
284: Author: Brouard
285:
1.153 brouard 286: Revision 1.152 2014/06/18 17:54:09 brouard
287: Summary: open browser, use gnuplot on same dir than imach if not found in the path
288:
1.152 brouard 289: Revision 1.151 2014/06/18 16:43:30 brouard
290: *** empty log message ***
291:
1.151 brouard 292: Revision 1.150 2014/06/18 16:42:35 brouard
293: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
294: Author: brouard
295:
1.150 brouard 296: Revision 1.149 2014/06/18 15:51:14 brouard
297: Summary: Some fixes in parameter files errors
298: Author: Nicolas Brouard
299:
1.149 brouard 300: Revision 1.148 2014/06/17 17:38:48 brouard
301: Summary: Nothing new
302: Author: Brouard
303:
304: Just a new packaging for OS/X version 0.98nS
305:
1.148 brouard 306: Revision 1.147 2014/06/16 10:33:11 brouard
307: *** empty log message ***
308:
1.147 brouard 309: Revision 1.146 2014/06/16 10:20:28 brouard
310: Summary: Merge
311: Author: Brouard
312:
313: Merge, before building revised version.
314:
1.146 brouard 315: Revision 1.145 2014/06/10 21:23:15 brouard
316: Summary: Debugging with valgrind
317: Author: Nicolas Brouard
318:
319: Lot of changes in order to output the results with some covariates
320: After the Edimburgh REVES conference 2014, it seems mandatory to
321: improve the code.
322: No more memory valgrind error but a lot has to be done in order to
323: continue the work of splitting the code into subroutines.
324: Also, decodemodel has been improved. Tricode is still not
325: optimal. nbcode should be improved. Documentation has been added in
326: the source code.
327:
1.144 brouard 328: Revision 1.143 2014/01/26 09:45:38 brouard
329: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
330:
331: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
332: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
333:
1.143 brouard 334: Revision 1.142 2014/01/26 03:57:36 brouard
335: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
336:
337: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
338:
1.142 brouard 339: Revision 1.141 2014/01/26 02:42:01 brouard
340: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
341:
1.141 brouard 342: Revision 1.140 2011/09/02 10:37:54 brouard
343: Summary: times.h is ok with mingw32 now.
344:
1.140 brouard 345: Revision 1.139 2010/06/14 07:50:17 brouard
346: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
347: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
348:
1.139 brouard 349: Revision 1.138 2010/04/30 18:19:40 brouard
350: *** empty log message ***
351:
1.138 brouard 352: Revision 1.137 2010/04/29 18:11:38 brouard
353: (Module): Checking covariates for more complex models
354: than V1+V2. A lot of change to be done. Unstable.
355:
1.137 brouard 356: Revision 1.136 2010/04/26 20:30:53 brouard
357: (Module): merging some libgsl code. Fixing computation
358: of likelione (using inter/intrapolation if mle = 0) in order to
359: get same likelihood as if mle=1.
360: Some cleaning of code and comments added.
361:
1.136 brouard 362: Revision 1.135 2009/10/29 15:33:14 brouard
363: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
364:
1.135 brouard 365: Revision 1.134 2009/10/29 13:18:53 brouard
366: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
367:
1.134 brouard 368: Revision 1.133 2009/07/06 10:21:25 brouard
369: just nforces
370:
1.133 brouard 371: Revision 1.132 2009/07/06 08:22:05 brouard
372: Many tings
373:
1.132 brouard 374: Revision 1.131 2009/06/20 16:22:47 brouard
375: Some dimensions resccaled
376:
1.131 brouard 377: Revision 1.130 2009/05/26 06:44:34 brouard
378: (Module): Max Covariate is now set to 20 instead of 8. A
379: lot of cleaning with variables initialized to 0. Trying to make
380: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
381:
1.130 brouard 382: Revision 1.129 2007/08/31 13:49:27 lievre
383: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
384:
1.129 lievre 385: Revision 1.128 2006/06/30 13:02:05 brouard
386: (Module): Clarifications on computing e.j
387:
1.128 brouard 388: Revision 1.127 2006/04/28 18:11:50 brouard
389: (Module): Yes the sum of survivors was wrong since
390: imach-114 because nhstepm was no more computed in the age
391: loop. Now we define nhstepma in the age loop.
392: (Module): In order to speed up (in case of numerous covariates) we
393: compute health expectancies (without variances) in a first step
394: and then all the health expectancies with variances or standard
395: deviation (needs data from the Hessian matrices) which slows the
396: computation.
397: In the future we should be able to stop the program is only health
398: expectancies and graph are needed without standard deviations.
399:
1.127 brouard 400: Revision 1.126 2006/04/28 17:23:28 brouard
401: (Module): Yes the sum of survivors was wrong since
402: imach-114 because nhstepm was no more computed in the age
403: loop. Now we define nhstepma in the age loop.
404: Version 0.98h
405:
1.126 brouard 406: Revision 1.125 2006/04/04 15:20:31 lievre
407: Errors in calculation of health expectancies. Age was not initialized.
408: Forecasting file added.
409:
410: Revision 1.124 2006/03/22 17:13:53 lievre
411: Parameters are printed with %lf instead of %f (more numbers after the comma).
412: The log-likelihood is printed in the log file
413:
414: Revision 1.123 2006/03/20 10:52:43 brouard
415: * imach.c (Module): <title> changed, corresponds to .htm file
416: name. <head> headers where missing.
417:
418: * imach.c (Module): Weights can have a decimal point as for
419: English (a comma might work with a correct LC_NUMERIC environment,
420: otherwise the weight is truncated).
421: Modification of warning when the covariates values are not 0 or
422: 1.
423: Version 0.98g
424:
425: Revision 1.122 2006/03/20 09:45:41 brouard
426: (Module): Weights can have a decimal point as for
427: English (a comma might work with a correct LC_NUMERIC environment,
428: otherwise the weight is truncated).
429: Modification of warning when the covariates values are not 0 or
430: 1.
431: Version 0.98g
432:
433: Revision 1.121 2006/03/16 17:45:01 lievre
434: * imach.c (Module): Comments concerning covariates added
435:
436: * imach.c (Module): refinements in the computation of lli if
437: status=-2 in order to have more reliable computation if stepm is
438: not 1 month. Version 0.98f
439:
440: Revision 1.120 2006/03/16 15:10:38 lievre
441: (Module): refinements in the computation of lli if
442: status=-2 in order to have more reliable computation if stepm is
443: not 1 month. Version 0.98f
444:
445: Revision 1.119 2006/03/15 17:42:26 brouard
446: (Module): Bug if status = -2, the loglikelihood was
447: computed as likelihood omitting the logarithm. Version O.98e
448:
449: Revision 1.118 2006/03/14 18:20:07 brouard
450: (Module): varevsij Comments added explaining the second
451: table of variances if popbased=1 .
452: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
453: (Module): Function pstamp added
454: (Module): Version 0.98d
455:
456: Revision 1.117 2006/03/14 17:16:22 brouard
457: (Module): varevsij Comments added explaining the second
458: table of variances if popbased=1 .
459: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
460: (Module): Function pstamp added
461: (Module): Version 0.98d
462:
463: Revision 1.116 2006/03/06 10:29:27 brouard
464: (Module): Variance-covariance wrong links and
465: varian-covariance of ej. is needed (Saito).
466:
467: Revision 1.115 2006/02/27 12:17:45 brouard
468: (Module): One freematrix added in mlikeli! 0.98c
469:
470: Revision 1.114 2006/02/26 12:57:58 brouard
471: (Module): Some improvements in processing parameter
472: filename with strsep.
473:
474: Revision 1.113 2006/02/24 14:20:24 brouard
475: (Module): Memory leaks checks with valgrind and:
476: datafile was not closed, some imatrix were not freed and on matrix
477: allocation too.
478:
479: Revision 1.112 2006/01/30 09:55:26 brouard
480: (Module): Back to gnuplot.exe instead of wgnuplot.exe
481:
482: Revision 1.111 2006/01/25 20:38:18 brouard
483: (Module): Lots of cleaning and bugs added (Gompertz)
484: (Module): Comments can be added in data file. Missing date values
485: can be a simple dot '.'.
486:
487: Revision 1.110 2006/01/25 00:51:50 brouard
488: (Module): Lots of cleaning and bugs added (Gompertz)
489:
490: Revision 1.109 2006/01/24 19:37:15 brouard
491: (Module): Comments (lines starting with a #) are allowed in data.
492:
493: Revision 1.108 2006/01/19 18:05:42 lievre
494: Gnuplot problem appeared...
495: To be fixed
496:
497: Revision 1.107 2006/01/19 16:20:37 brouard
498: Test existence of gnuplot in imach path
499:
500: Revision 1.106 2006/01/19 13:24:36 brouard
501: Some cleaning and links added in html output
502:
503: Revision 1.105 2006/01/05 20:23:19 lievre
504: *** empty log message ***
505:
506: Revision 1.104 2005/09/30 16:11:43 lievre
507: (Module): sump fixed, loop imx fixed, and simplifications.
508: (Module): If the status is missing at the last wave but we know
509: that the person is alive, then we can code his/her status as -2
510: (instead of missing=-1 in earlier versions) and his/her
511: contributions to the likelihood is 1 - Prob of dying from last
512: health status (= 1-p13= p11+p12 in the easiest case of somebody in
513: the healthy state at last known wave). Version is 0.98
514:
515: Revision 1.103 2005/09/30 15:54:49 lievre
516: (Module): sump fixed, loop imx fixed, and simplifications.
517:
518: Revision 1.102 2004/09/15 17:31:30 brouard
519: Add the possibility to read data file including tab characters.
520:
521: Revision 1.101 2004/09/15 10:38:38 brouard
522: Fix on curr_time
523:
524: Revision 1.100 2004/07/12 18:29:06 brouard
525: Add version for Mac OS X. Just define UNIX in Makefile
526:
527: Revision 1.99 2004/06/05 08:57:40 brouard
528: *** empty log message ***
529:
530: Revision 1.98 2004/05/16 15:05:56 brouard
531: New version 0.97 . First attempt to estimate force of mortality
532: directly from the data i.e. without the need of knowing the health
533: state at each age, but using a Gompertz model: log u =a + b*age .
534: This is the basic analysis of mortality and should be done before any
535: other analysis, in order to test if the mortality estimated from the
536: cross-longitudinal survey is different from the mortality estimated
537: from other sources like vital statistic data.
538:
539: The same imach parameter file can be used but the option for mle should be -3.
540:
1.133 brouard 541: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 542: former routines in order to include the new code within the former code.
543:
544: The output is very simple: only an estimate of the intercept and of
545: the slope with 95% confident intervals.
546:
547: Current limitations:
548: A) Even if you enter covariates, i.e. with the
549: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
550: B) There is no computation of Life Expectancy nor Life Table.
551:
552: Revision 1.97 2004/02/20 13:25:42 lievre
553: Version 0.96d. Population forecasting command line is (temporarily)
554: suppressed.
555:
556: Revision 1.96 2003/07/15 15:38:55 brouard
557: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
558: rewritten within the same printf. Workaround: many printfs.
559:
560: Revision 1.95 2003/07/08 07:54:34 brouard
561: * imach.c (Repository):
562: (Repository): Using imachwizard code to output a more meaningful covariance
563: matrix (cov(a12,c31) instead of numbers.
564:
565: Revision 1.94 2003/06/27 13:00:02 brouard
566: Just cleaning
567:
568: Revision 1.93 2003/06/25 16:33:55 brouard
569: (Module): On windows (cygwin) function asctime_r doesn't
570: exist so I changed back to asctime which exists.
571: (Module): Version 0.96b
572:
573: Revision 1.92 2003/06/25 16:30:45 brouard
574: (Module): On windows (cygwin) function asctime_r doesn't
575: exist so I changed back to asctime which exists.
576:
577: Revision 1.91 2003/06/25 15:30:29 brouard
578: * imach.c (Repository): Duplicated warning errors corrected.
579: (Repository): Elapsed time after each iteration is now output. It
580: helps to forecast when convergence will be reached. Elapsed time
581: is stamped in powell. We created a new html file for the graphs
582: concerning matrix of covariance. It has extension -cov.htm.
583:
584: Revision 1.90 2003/06/24 12:34:15 brouard
585: (Module): Some bugs corrected for windows. Also, when
586: mle=-1 a template is output in file "or"mypar.txt with the design
587: of the covariance matrix to be input.
588:
589: Revision 1.89 2003/06/24 12:30:52 brouard
590: (Module): Some bugs corrected for windows. Also, when
591: mle=-1 a template is output in file "or"mypar.txt with the design
592: of the covariance matrix to be input.
593:
594: Revision 1.88 2003/06/23 17:54:56 brouard
595: * 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.
596:
597: Revision 1.87 2003/06/18 12:26:01 brouard
598: Version 0.96
599:
600: Revision 1.86 2003/06/17 20:04:08 brouard
601: (Module): Change position of html and gnuplot routines and added
602: routine fileappend.
603:
604: Revision 1.85 2003/06/17 13:12:43 brouard
605: * imach.c (Repository): Check when date of death was earlier that
606: current date of interview. It may happen when the death was just
607: prior to the death. In this case, dh was negative and likelihood
608: was wrong (infinity). We still send an "Error" but patch by
609: assuming that the date of death was just one stepm after the
610: interview.
611: (Repository): Because some people have very long ID (first column)
612: we changed int to long in num[] and we added a new lvector for
613: memory allocation. But we also truncated to 8 characters (left
614: truncation)
615: (Repository): No more line truncation errors.
616:
617: Revision 1.84 2003/06/13 21:44:43 brouard
618: * imach.c (Repository): Replace "freqsummary" at a correct
619: place. It differs from routine "prevalence" which may be called
620: many times. Probs is memory consuming and must be used with
621: parcimony.
622: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
623:
624: Revision 1.83 2003/06/10 13:39:11 lievre
625: *** empty log message ***
626:
627: Revision 1.82 2003/06/05 15:57:20 brouard
628: Add log in imach.c and fullversion number is now printed.
629:
630: */
631: /*
632: Interpolated Markov Chain
633:
634: Short summary of the programme:
635:
636: This program computes Healthy Life Expectancies from
637: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
638: first survey ("cross") where individuals from different ages are
639: interviewed on their health status or degree of disability (in the
640: case of a health survey which is our main interest) -2- at least a
641: second wave of interviews ("longitudinal") which measure each change
642: (if any) in individual health status. Health expectancies are
643: computed from the time spent in each health state according to a
644: model. More health states you consider, more time is necessary to reach the
645: Maximum Likelihood of the parameters involved in the model. The
646: simplest model is the multinomial logistic model where pij is the
647: probability to be observed in state j at the second wave
648: conditional to be observed in state i at the first wave. Therefore
649: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
650: 'age' is age and 'sex' is a covariate. If you want to have a more
651: complex model than "constant and age", you should modify the program
652: where the markup *Covariates have to be included here again* invites
653: you to do it. More covariates you add, slower the
654: convergence.
655:
656: The advantage of this computer programme, compared to a simple
657: multinomial logistic model, is clear when the delay between waves is not
658: identical for each individual. Also, if a individual missed an
659: intermediate interview, the information is lost, but taken into
660: account using an interpolation or extrapolation.
661:
662: hPijx is the probability to be observed in state i at age x+h
663: conditional to the observed state i at age x. The delay 'h' can be
664: split into an exact number (nh*stepm) of unobserved intermediate
665: states. This elementary transition (by month, quarter,
666: semester or year) is modelled as a multinomial logistic. The hPx
667: matrix is simply the matrix product of nh*stepm elementary matrices
668: and the contribution of each individual to the likelihood is simply
669: hPijx.
670:
671: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 672: of the life expectancies. It also computes the period (stable) prevalence.
673:
674: Back prevalence and projections:
675: - 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)
676: Computes the back prevalence limit for any combination of covariate values k
677: at any age between ageminpar and agemaxpar and returns it in **bprlim. In the loops,
678: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm, **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
679: - hBijx Back Probability to be in state i at age x-h being in j at x
680: Computes for any combination of covariates k and any age between bage and fage
681: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
682: oldm=oldms;savm=savms;
683: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
684: Computes the transition matrix starting at age 'age' over
685: 'nhstepm*hstepm*stepm' months (i.e. until
686: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
687: nhstepm*hstepm matrices. Returns p3mat[i][j][h] after calling
688: p3mat[i][j][h]=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\
689: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
690:
1.133 brouard 691: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
692: Institut national d'études démographiques, Paris.
1.126 brouard 693: This software have been partly granted by Euro-REVES, a concerted action
694: from the European Union.
695: It is copyrighted identically to a GNU software product, ie programme and
696: software can be distributed freely for non commercial use. Latest version
697: can be accessed at http://euroreves.ined.fr/imach .
698:
699: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
700: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
701:
702: **********************************************************************/
703: /*
704: main
705: read parameterfile
706: read datafile
707: concatwav
708: freqsummary
709: if (mle >= 1)
710: mlikeli
711: print results files
712: if mle==1
713: computes hessian
714: read end of parameter file: agemin, agemax, bage, fage, estepm
715: begin-prev-date,...
716: open gnuplot file
717: open html file
1.145 brouard 718: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
719: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
720: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
721: freexexit2 possible for memory heap.
722:
723: h Pij x | pij_nom ficrestpij
724: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
725: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
726: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
727:
728: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
729: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
730: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
731: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
732: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
733:
1.126 brouard 734: forecasting if prevfcast==1 prevforecast call prevalence()
735: health expectancies
736: Variance-covariance of DFLE
737: prevalence()
738: movingaverage()
739: varevsij()
740: if popbased==1 varevsij(,popbased)
741: total life expectancies
742: Variance of period (stable) prevalence
743: end
744: */
745:
1.187 brouard 746: /* #define DEBUG */
747: /* #define DEBUGBRENT */
1.203 brouard 748: /* #define DEBUGLINMIN */
749: /* #define DEBUGHESS */
750: #define DEBUGHESSIJ
1.224 brouard 751: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 752: #define POWELL /* Instead of NLOPT */
1.224 brouard 753: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 754: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
755: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 756:
757: #include <math.h>
758: #include <stdio.h>
759: #include <stdlib.h>
760: #include <string.h>
1.159 brouard 761:
762: #ifdef _WIN32
763: #include <io.h>
1.172 brouard 764: #include <windows.h>
765: #include <tchar.h>
1.159 brouard 766: #else
1.126 brouard 767: #include <unistd.h>
1.159 brouard 768: #endif
1.126 brouard 769:
770: #include <limits.h>
771: #include <sys/types.h>
1.171 brouard 772:
773: #if defined(__GNUC__)
774: #include <sys/utsname.h> /* Doesn't work on Windows */
775: #endif
776:
1.126 brouard 777: #include <sys/stat.h>
778: #include <errno.h>
1.159 brouard 779: /* extern int errno; */
1.126 brouard 780:
1.157 brouard 781: /* #ifdef LINUX */
782: /* #include <time.h> */
783: /* #include "timeval.h" */
784: /* #else */
785: /* #include <sys/time.h> */
786: /* #endif */
787:
1.126 brouard 788: #include <time.h>
789:
1.136 brouard 790: #ifdef GSL
791: #include <gsl/gsl_errno.h>
792: #include <gsl/gsl_multimin.h>
793: #endif
794:
1.167 brouard 795:
1.162 brouard 796: #ifdef NLOPT
797: #include <nlopt.h>
798: typedef struct {
799: double (* function)(double [] );
800: } myfunc_data ;
801: #endif
802:
1.126 brouard 803: /* #include <libintl.h> */
804: /* #define _(String) gettext (String) */
805:
1.141 brouard 806: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 807:
808: #define GNUPLOTPROGRAM "gnuplot"
809: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
810: #define FILENAMELENGTH 132
811:
812: #define GLOCK_ERROR_NOPATH -1 /* empty path */
813: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
814:
1.144 brouard 815: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
816: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 817:
818: #define NINTERVMAX 8
1.144 brouard 819: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
820: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
821: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 822: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 823: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
824: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 825: #define MAXN 20000
1.144 brouard 826: #define YEARM 12. /**< Number of months per year */
1.218 brouard 827: /* #define AGESUP 130 */
828: #define AGESUP 150
829: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 830: #define AGEBASE 40
1.194 brouard 831: #define AGEOVERFLOW 1.e20
1.164 brouard 832: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 833: #ifdef _WIN32
834: #define DIRSEPARATOR '\\'
835: #define CHARSEPARATOR "\\"
836: #define ODIRSEPARATOR '/'
837: #else
1.126 brouard 838: #define DIRSEPARATOR '/'
839: #define CHARSEPARATOR "/"
840: #define ODIRSEPARATOR '\\'
841: #endif
842:
1.225 ! brouard 843: /* $Id: imach.c,v 1.224 2016/07/01 13:16:01 brouard Exp $ */
1.126 brouard 844: /* $State: Exp $ */
1.196 brouard 845: #include "version.h"
846: char version[]=__IMACH_VERSION__;
1.224 brouard 847: char copyright[]="February 2016,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015-2018";
1.225 ! brouard 848: char fullversion[]="$Revision: 1.224 $ $Date: 2016/07/01 13:16:01 $";
1.126 brouard 849: char strstart[80];
850: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 851: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 852: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 853: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
854: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
855: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 ! brouard 856: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
! 857: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 858: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
859: int cptcovprodnoage=0; /**< Number of covariate products without age */
860: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.224 brouard 861: int ncoveff=0; /* Total number of effective covariates in the model */
1.225 ! brouard 862: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 863: int ntveff=0; /**< ntveff number of effective time varying variables */
864: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 865: int cptcov=0; /* Working variable */
1.218 brouard 866: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 867: int npar=NPARMAX;
868: int nlstate=2; /* Number of live states */
869: int ndeath=1; /* Number of dead states */
1.130 brouard 870: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 871: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 872: int popbased=0;
873:
874: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 875: int maxwav=0; /* Maxim number of waves */
876: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
877: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
878: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 879: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 880: int mle=1, weightopt=0;
1.126 brouard 881: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
882: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
883: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
884: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 885: int countcallfunc=0; /* Count the number of calls to func */
1.130 brouard 886: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 887: double **matprod2(); /* test */
1.126 brouard 888: double **oldm, **newm, **savm; /* Working pointers to matrices */
889: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 890: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
891:
1.136 brouard 892: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 893: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 894: FILE *ficlog, *ficrespow;
1.130 brouard 895: int globpr=0; /* Global variable for printing or not */
1.126 brouard 896: double fretone; /* Only one call to likelihood */
1.130 brouard 897: long ipmx=0; /* Number of contributions */
1.126 brouard 898: double sw; /* Sum of weights */
899: char filerespow[FILENAMELENGTH];
900: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
901: FILE *ficresilk;
902: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
903: FILE *ficresprobmorprev;
904: FILE *fichtm, *fichtmcov; /* Html File */
905: FILE *ficreseij;
906: char filerese[FILENAMELENGTH];
907: FILE *ficresstdeij;
908: char fileresstde[FILENAMELENGTH];
909: FILE *ficrescveij;
910: char filerescve[FILENAMELENGTH];
911: FILE *ficresvij;
912: char fileresv[FILENAMELENGTH];
913: FILE *ficresvpl;
914: char fileresvpl[FILENAMELENGTH];
915: char title[MAXLINE];
1.217 brouard 916: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 917: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
918: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
919: char command[FILENAMELENGTH];
920: int outcmd=0;
921:
1.217 brouard 922: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 923: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 924: char filelog[FILENAMELENGTH]; /* Log file */
925: char filerest[FILENAMELENGTH];
926: char fileregp[FILENAMELENGTH];
927: char popfile[FILENAMELENGTH];
928:
929: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
930:
1.157 brouard 931: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
932: /* struct timezone tzp; */
933: /* extern int gettimeofday(); */
934: struct tm tml, *gmtime(), *localtime();
935:
936: extern time_t time();
937:
938: struct tm start_time, end_time, curr_time, last_time, forecast_time;
939: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
940: struct tm tm;
941:
1.126 brouard 942: char strcurr[80], strfor[80];
943:
944: char *endptr;
945: long lval;
946: double dval;
947:
948: #define NR_END 1
949: #define FREE_ARG char*
950: #define FTOL 1.0e-10
951:
952: #define NRANSI
953: #define ITMAX 200
954:
955: #define TOL 2.0e-4
956:
957: #define CGOLD 0.3819660
958: #define ZEPS 1.0e-10
959: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
960:
961: #define GOLD 1.618034
962: #define GLIMIT 100.0
963: #define TINY 1.0e-20
964:
965: static double maxarg1,maxarg2;
966: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
967: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
968:
969: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
970: #define rint(a) floor(a+0.5)
1.166 brouard 971: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 972: #define mytinydouble 1.0e-16
1.166 brouard 973: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
974: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
975: /* static double dsqrarg; */
976: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 977: static double sqrarg;
978: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
979: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
980: int agegomp= AGEGOMP;
981:
982: int imx;
983: int stepm=1;
984: /* Stepm, step in month: minimum step interpolation*/
985:
986: int estepm;
987: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
988:
989: int m,nb;
990: long *num;
1.197 brouard 991: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 992: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
993: covariate for which somebody answered excluding
994: undefined. Usually 2: 0 and 1. */
995: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
996: covariate for which somebody answered including
997: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 998: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 999: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1000: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1001: double *ageexmed,*agecens;
1002: double dateintmean=0;
1003:
1004: double *weight;
1005: int **s; /* Status */
1.141 brouard 1006: double *agedc;
1.145 brouard 1007: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1008: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1009: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.225 ! brouard 1010: double **coqvar; /* Fixed quantitative covariate iqv */
! 1011: double ***cotvar; /* Time varying covariate itv */
! 1012: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1013: double idx;
1014: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.225 ! brouard 1015: int *Typevar; /**< 1 for qualitative fixed, 2 for quantitative fixed, 3 for qualitive varying, 4 for quanti varying*/
1.197 brouard 1016: int *Tage;
1.145 brouard 1017: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1018: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.220 brouard 1019: int **Tvard, *Tprod, cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1020: double *lsurv, *lpop, *tpop;
1021:
1.143 brouard 1022: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1023: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1024:
1025: /**************** split *************************/
1026: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1027: {
1028: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1029: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1030: */
1031: char *ss; /* pointer */
1.186 brouard 1032: int l1=0, l2=0; /* length counters */
1.126 brouard 1033:
1034: l1 = strlen(path ); /* length of path */
1035: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1036: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1037: if ( ss == NULL ) { /* no directory, so determine current directory */
1038: strcpy( name, path ); /* we got the fullname name because no directory */
1039: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1040: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1041: /* get current working directory */
1042: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1043: #ifdef WIN32
1044: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1045: #else
1046: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1047: #endif
1.126 brouard 1048: return( GLOCK_ERROR_GETCWD );
1049: }
1050: /* got dirc from getcwd*/
1051: printf(" DIRC = %s \n",dirc);
1.205 brouard 1052: } else { /* strip directory from path */
1.126 brouard 1053: ss++; /* after this, the filename */
1054: l2 = strlen( ss ); /* length of filename */
1055: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1056: strcpy( name, ss ); /* save file name */
1057: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1058: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1059: printf(" DIRC2 = %s \n",dirc);
1060: }
1061: /* We add a separator at the end of dirc if not exists */
1062: l1 = strlen( dirc ); /* length of directory */
1063: if( dirc[l1-1] != DIRSEPARATOR ){
1064: dirc[l1] = DIRSEPARATOR;
1065: dirc[l1+1] = 0;
1066: printf(" DIRC3 = %s \n",dirc);
1067: }
1068: ss = strrchr( name, '.' ); /* find last / */
1069: if (ss >0){
1070: ss++;
1071: strcpy(ext,ss); /* save extension */
1072: l1= strlen( name);
1073: l2= strlen(ss)+1;
1074: strncpy( finame, name, l1-l2);
1075: finame[l1-l2]= 0;
1076: }
1077:
1078: return( 0 ); /* we're done */
1079: }
1080:
1081:
1082: /******************************************/
1083:
1084: void replace_back_to_slash(char *s, char*t)
1085: {
1086: int i;
1087: int lg=0;
1088: i=0;
1089: lg=strlen(t);
1090: for(i=0; i<= lg; i++) {
1091: (s[i] = t[i]);
1092: if (t[i]== '\\') s[i]='/';
1093: }
1094: }
1095:
1.132 brouard 1096: char *trimbb(char *out, char *in)
1.137 brouard 1097: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1098: char *s;
1099: s=out;
1100: while (*in != '\0'){
1.137 brouard 1101: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1102: in++;
1103: }
1104: *out++ = *in++;
1105: }
1106: *out='\0';
1107: return s;
1108: }
1109:
1.187 brouard 1110: /* char *substrchaine(char *out, char *in, char *chain) */
1111: /* { */
1112: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1113: /* char *s, *t; */
1114: /* t=in;s=out; */
1115: /* while ((*in != *chain) && (*in != '\0')){ */
1116: /* *out++ = *in++; */
1117: /* } */
1118:
1119: /* /\* *in matches *chain *\/ */
1120: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1121: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1122: /* } */
1123: /* in--; chain--; */
1124: /* while ( (*in != '\0')){ */
1125: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1126: /* *out++ = *in++; */
1127: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1128: /* } */
1129: /* *out='\0'; */
1130: /* out=s; */
1131: /* return out; */
1132: /* } */
1133: char *substrchaine(char *out, char *in, char *chain)
1134: {
1135: /* Substract chain 'chain' from 'in', return and output 'out' */
1136: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1137:
1138: char *strloc;
1139:
1140: strcpy (out, in);
1141: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1142: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1143: if(strloc != NULL){
1144: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1145: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1146: /* strcpy (strloc, strloc +strlen(chain));*/
1147: }
1148: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1149: return out;
1150: }
1151:
1152:
1.145 brouard 1153: char *cutl(char *blocc, char *alocc, char *in, char occ)
1154: {
1.187 brouard 1155: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1156: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1157: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1158: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1159: */
1.160 brouard 1160: char *s, *t;
1.145 brouard 1161: t=in;s=in;
1162: while ((*in != occ) && (*in != '\0')){
1163: *alocc++ = *in++;
1164: }
1165: if( *in == occ){
1166: *(alocc)='\0';
1167: s=++in;
1168: }
1169:
1170: if (s == t) {/* occ not found */
1171: *(alocc-(in-s))='\0';
1172: in=s;
1173: }
1174: while ( *in != '\0'){
1175: *blocc++ = *in++;
1176: }
1177:
1178: *blocc='\0';
1179: return t;
1180: }
1.137 brouard 1181: char *cutv(char *blocc, char *alocc, char *in, char occ)
1182: {
1.187 brouard 1183: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1184: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1185: gives blocc="abcdef2ghi" and alocc="j".
1186: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1187: */
1188: char *s, *t;
1189: t=in;s=in;
1190: while (*in != '\0'){
1191: while( *in == occ){
1192: *blocc++ = *in++;
1193: s=in;
1194: }
1195: *blocc++ = *in++;
1196: }
1197: if (s == t) /* occ not found */
1198: *(blocc-(in-s))='\0';
1199: else
1200: *(blocc-(in-s)-1)='\0';
1201: in=s;
1202: while ( *in != '\0'){
1203: *alocc++ = *in++;
1204: }
1205:
1206: *alocc='\0';
1207: return s;
1208: }
1209:
1.126 brouard 1210: int nbocc(char *s, char occ)
1211: {
1212: int i,j=0;
1213: int lg=20;
1214: i=0;
1215: lg=strlen(s);
1216: for(i=0; i<= lg; i++) {
1217: if (s[i] == occ ) j++;
1218: }
1219: return j;
1220: }
1221:
1.137 brouard 1222: /* void cutv(char *u,char *v, char*t, char occ) */
1223: /* { */
1224: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1225: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1226: /* gives u="abcdef2ghi" and v="j" *\/ */
1227: /* int i,lg,j,p=0; */
1228: /* i=0; */
1229: /* lg=strlen(t); */
1230: /* for(j=0; j<=lg-1; j++) { */
1231: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1232: /* } */
1.126 brouard 1233:
1.137 brouard 1234: /* for(j=0; j<p; j++) { */
1235: /* (u[j] = t[j]); */
1236: /* } */
1237: /* u[p]='\0'; */
1.126 brouard 1238:
1.137 brouard 1239: /* for(j=0; j<= lg; j++) { */
1240: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1241: /* } */
1242: /* } */
1.126 brouard 1243:
1.160 brouard 1244: #ifdef _WIN32
1245: char * strsep(char **pp, const char *delim)
1246: {
1247: char *p, *q;
1248:
1249: if ((p = *pp) == NULL)
1250: return 0;
1251: if ((q = strpbrk (p, delim)) != NULL)
1252: {
1253: *pp = q + 1;
1254: *q = '\0';
1255: }
1256: else
1257: *pp = 0;
1258: return p;
1259: }
1260: #endif
1261:
1.126 brouard 1262: /********************** nrerror ********************/
1263:
1264: void nrerror(char error_text[])
1265: {
1266: fprintf(stderr,"ERREUR ...\n");
1267: fprintf(stderr,"%s\n",error_text);
1268: exit(EXIT_FAILURE);
1269: }
1270: /*********************** vector *******************/
1271: double *vector(int nl, int nh)
1272: {
1273: double *v;
1274: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1275: if (!v) nrerror("allocation failure in vector");
1276: return v-nl+NR_END;
1277: }
1278:
1279: /************************ free vector ******************/
1280: void free_vector(double*v, int nl, int nh)
1281: {
1282: free((FREE_ARG)(v+nl-NR_END));
1283: }
1284:
1285: /************************ivector *******************************/
1286: int *ivector(long nl,long nh)
1287: {
1288: int *v;
1289: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1290: if (!v) nrerror("allocation failure in ivector");
1291: return v-nl+NR_END;
1292: }
1293:
1294: /******************free ivector **************************/
1295: void free_ivector(int *v, long nl, long nh)
1296: {
1297: free((FREE_ARG)(v+nl-NR_END));
1298: }
1299:
1300: /************************lvector *******************************/
1301: long *lvector(long nl,long nh)
1302: {
1303: long *v;
1304: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1305: if (!v) nrerror("allocation failure in ivector");
1306: return v-nl+NR_END;
1307: }
1308:
1309: /******************free lvector **************************/
1310: void free_lvector(long *v, long nl, long nh)
1311: {
1312: free((FREE_ARG)(v+nl-NR_END));
1313: }
1314:
1315: /******************* imatrix *******************************/
1316: int **imatrix(long nrl, long nrh, long ncl, long nch)
1317: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1318: {
1319: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1320: int **m;
1321:
1322: /* allocate pointers to rows */
1323: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1324: if (!m) nrerror("allocation failure 1 in matrix()");
1325: m += NR_END;
1326: m -= nrl;
1327:
1328:
1329: /* allocate rows and set pointers to them */
1330: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1331: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1332: m[nrl] += NR_END;
1333: m[nrl] -= ncl;
1334:
1335: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1336:
1337: /* return pointer to array of pointers to rows */
1338: return m;
1339: }
1340:
1341: /****************** free_imatrix *************************/
1342: void free_imatrix(m,nrl,nrh,ncl,nch)
1343: int **m;
1344: long nch,ncl,nrh,nrl;
1345: /* free an int matrix allocated by imatrix() */
1346: {
1347: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1348: free((FREE_ARG) (m+nrl-NR_END));
1349: }
1350:
1351: /******************* matrix *******************************/
1352: double **matrix(long nrl, long nrh, long ncl, long nch)
1353: {
1354: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1355: double **m;
1356:
1357: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1358: if (!m) nrerror("allocation failure 1 in matrix()");
1359: m += NR_END;
1360: m -= nrl;
1361:
1362: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1363: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1364: m[nrl] += NR_END;
1365: m[nrl] -= ncl;
1366:
1367: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1368: return m;
1.145 brouard 1369: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1370: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1371: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1372: */
1373: }
1374:
1375: /*************************free matrix ************************/
1376: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1377: {
1378: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1379: free((FREE_ARG)(m+nrl-NR_END));
1380: }
1381:
1382: /******************* ma3x *******************************/
1383: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1384: {
1385: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1386: double ***m;
1387:
1388: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1389: if (!m) nrerror("allocation failure 1 in matrix()");
1390: m += NR_END;
1391: m -= nrl;
1392:
1393: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1394: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1395: m[nrl] += NR_END;
1396: m[nrl] -= ncl;
1397:
1398: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1399:
1400: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1401: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1402: m[nrl][ncl] += NR_END;
1403: m[nrl][ncl] -= nll;
1404: for (j=ncl+1; j<=nch; j++)
1405: m[nrl][j]=m[nrl][j-1]+nlay;
1406:
1407: for (i=nrl+1; i<=nrh; i++) {
1408: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1409: for (j=ncl+1; j<=nch; j++)
1410: m[i][j]=m[i][j-1]+nlay;
1411: }
1412: return m;
1413: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1414: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1415: */
1416: }
1417:
1418: /*************************free ma3x ************************/
1419: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1420: {
1421: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1422: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1423: free((FREE_ARG)(m+nrl-NR_END));
1424: }
1425:
1426: /*************** function subdirf ***********/
1427: char *subdirf(char fileres[])
1428: {
1429: /* Caution optionfilefiname is hidden */
1430: strcpy(tmpout,optionfilefiname);
1431: strcat(tmpout,"/"); /* Add to the right */
1432: strcat(tmpout,fileres);
1433: return tmpout;
1434: }
1435:
1436: /*************** function subdirf2 ***********/
1437: char *subdirf2(char fileres[], char *preop)
1438: {
1439:
1440: /* Caution optionfilefiname is hidden */
1441: strcpy(tmpout,optionfilefiname);
1442: strcat(tmpout,"/");
1443: strcat(tmpout,preop);
1444: strcat(tmpout,fileres);
1445: return tmpout;
1446: }
1447:
1448: /*************** function subdirf3 ***********/
1449: char *subdirf3(char fileres[], char *preop, char *preop2)
1450: {
1451:
1452: /* Caution optionfilefiname is hidden */
1453: strcpy(tmpout,optionfilefiname);
1454: strcat(tmpout,"/");
1455: strcat(tmpout,preop);
1456: strcat(tmpout,preop2);
1457: strcat(tmpout,fileres);
1458: return tmpout;
1459: }
1.213 brouard 1460:
1461: /*************** function subdirfext ***********/
1462: char *subdirfext(char fileres[], char *preop, char *postop)
1463: {
1464:
1465: strcpy(tmpout,preop);
1466: strcat(tmpout,fileres);
1467: strcat(tmpout,postop);
1468: return tmpout;
1469: }
1.126 brouard 1470:
1.213 brouard 1471: /*************** function subdirfext3 ***********/
1472: char *subdirfext3(char fileres[], char *preop, char *postop)
1473: {
1474:
1475: /* Caution optionfilefiname is hidden */
1476: strcpy(tmpout,optionfilefiname);
1477: strcat(tmpout,"/");
1478: strcat(tmpout,preop);
1479: strcat(tmpout,fileres);
1480: strcat(tmpout,postop);
1481: return tmpout;
1482: }
1483:
1.162 brouard 1484: char *asc_diff_time(long time_sec, char ascdiff[])
1485: {
1486: long sec_left, days, hours, minutes;
1487: days = (time_sec) / (60*60*24);
1488: sec_left = (time_sec) % (60*60*24);
1489: hours = (sec_left) / (60*60) ;
1490: sec_left = (sec_left) %(60*60);
1491: minutes = (sec_left) /60;
1492: sec_left = (sec_left) % (60);
1493: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1494: return ascdiff;
1495: }
1496:
1.126 brouard 1497: /***************** f1dim *************************/
1498: extern int ncom;
1499: extern double *pcom,*xicom;
1500: extern double (*nrfunc)(double []);
1501:
1502: double f1dim(double x)
1503: {
1504: int j;
1505: double f;
1506: double *xt;
1507:
1508: xt=vector(1,ncom);
1509: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1510: f=(*nrfunc)(xt);
1511: free_vector(xt,1,ncom);
1512: return f;
1513: }
1514:
1515: /*****************brent *************************/
1516: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1517: {
1518: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1519: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1520: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1521: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1522: * returned function value.
1523: */
1.126 brouard 1524: int iter;
1525: double a,b,d,etemp;
1.159 brouard 1526: double fu=0,fv,fw,fx;
1.164 brouard 1527: double ftemp=0.;
1.126 brouard 1528: double p,q,r,tol1,tol2,u,v,w,x,xm;
1529: double e=0.0;
1530:
1531: a=(ax < cx ? ax : cx);
1532: b=(ax > cx ? ax : cx);
1533: x=w=v=bx;
1534: fw=fv=fx=(*f)(x);
1535: for (iter=1;iter<=ITMAX;iter++) {
1536: xm=0.5*(a+b);
1537: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1538: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1539: printf(".");fflush(stdout);
1540: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1541: #ifdef DEBUGBRENT
1.126 brouard 1542: 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);
1543: 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);
1544: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1545: #endif
1546: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1547: *xmin=x;
1548: return fx;
1549: }
1550: ftemp=fu;
1551: if (fabs(e) > tol1) {
1552: r=(x-w)*(fx-fv);
1553: q=(x-v)*(fx-fw);
1554: p=(x-v)*q-(x-w)*r;
1555: q=2.0*(q-r);
1556: if (q > 0.0) p = -p;
1557: q=fabs(q);
1558: etemp=e;
1559: e=d;
1560: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1561: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1562: else {
1.224 brouard 1563: d=p/q;
1564: u=x+d;
1565: if (u-a < tol2 || b-u < tol2)
1566: d=SIGN(tol1,xm-x);
1.126 brouard 1567: }
1568: } else {
1569: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1570: }
1571: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1572: fu=(*f)(u);
1573: if (fu <= fx) {
1574: if (u >= x) a=x; else b=x;
1575: SHFT(v,w,x,u)
1.183 brouard 1576: SHFT(fv,fw,fx,fu)
1577: } else {
1578: if (u < x) a=u; else b=u;
1579: if (fu <= fw || w == x) {
1.224 brouard 1580: v=w;
1581: w=u;
1582: fv=fw;
1583: fw=fu;
1.183 brouard 1584: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 1585: v=u;
1586: fv=fu;
1.183 brouard 1587: }
1588: }
1.126 brouard 1589: }
1590: nrerror("Too many iterations in brent");
1591: *xmin=x;
1592: return fx;
1593: }
1594:
1595: /****************** mnbrak ***********************/
1596:
1597: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1598: double (*func)(double))
1.183 brouard 1599: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1600: the downhill direction (defined by the function as evaluated at the initial points) and returns
1601: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1602: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1603: */
1.126 brouard 1604: double ulim,u,r,q, dum;
1605: double fu;
1.187 brouard 1606:
1607: double scale=10.;
1608: int iterscale=0;
1609:
1610: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1611: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1612:
1613:
1614: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1615: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1616: /* *bx = *ax - (*ax - *bx)/scale; */
1617: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1618: /* } */
1619:
1.126 brouard 1620: if (*fb > *fa) {
1621: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1622: SHFT(dum,*fb,*fa,dum)
1623: }
1.126 brouard 1624: *cx=(*bx)+GOLD*(*bx-*ax);
1625: *fc=(*func)(*cx);
1.183 brouard 1626: #ifdef DEBUG
1.224 brouard 1627: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1628: fprintf(ficlog,"mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1.183 brouard 1629: #endif
1.224 brouard 1630: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc. If fc=inf it exits and if flat fb=fc it exits too.*/
1.126 brouard 1631: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 1632: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 1633: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1634: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1635: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1636: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1637: fu=(*func)(u);
1.163 brouard 1638: #ifdef DEBUG
1639: /* f(x)=A(x-u)**2+f(u) */
1640: double A, fparabu;
1641: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1642: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 1643: 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);
1644: fprintf(ficlog,"\nmnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f, q=%lf < %lf=r)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu,q,r);
1.183 brouard 1645: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1646: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1647: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1648: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1649: #endif
1.184 brouard 1650: #ifdef MNBRAKORIGINAL
1.183 brouard 1651: #else
1.191 brouard 1652: /* if (fu > *fc) { */
1653: /* #ifdef DEBUG */
1654: /* printf("mnbrak4 fu > fc \n"); */
1655: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1656: /* #endif */
1657: /* /\* 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 *\\/ *\/ */
1658: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1659: /* dum=u; /\* Shifting c and u *\/ */
1660: /* u = *cx; */
1661: /* *cx = dum; */
1662: /* dum = fu; */
1663: /* fu = *fc; */
1664: /* *fc =dum; */
1665: /* } else { /\* end *\/ */
1666: /* #ifdef DEBUG */
1667: /* printf("mnbrak3 fu < fc \n"); */
1668: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1669: /* #endif */
1670: /* dum=u; /\* Shifting c and u *\/ */
1671: /* u = *cx; */
1672: /* *cx = dum; */
1673: /* dum = fu; */
1674: /* fu = *fc; */
1675: /* *fc =dum; */
1676: /* } */
1.224 brouard 1677: #ifdef DEBUGMNBRAK
1678: double A, fparabu;
1679: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1680: fparabu= *fa - A*(*ax-u)*(*ax-u);
1681: 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);
1682: fprintf(ficlog,"\nmnbrak35 ax=%lf fa=%lf bx=%lf fb=%lf, u=%lf fp=%lf fu=%lf < or >= fc=%lf cx=%lf, q=%lf < %lf=r \n",*ax, *fa, *bx,*fb,u,fparabu,fu,*fc,*cx,q,r);
1.183 brouard 1683: #endif
1.191 brouard 1684: dum=u; /* Shifting c and u */
1685: u = *cx;
1686: *cx = dum;
1687: dum = fu;
1688: fu = *fc;
1689: *fc =dum;
1.183 brouard 1690: #endif
1.162 brouard 1691: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1692: #ifdef DEBUG
1.224 brouard 1693: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1694: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 1695: #endif
1.126 brouard 1696: fu=(*func)(u);
1697: if (fu < *fc) {
1.183 brouard 1698: #ifdef DEBUG
1.224 brouard 1699: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1700: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1701: #endif
1702: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1703: SHFT(*fb,*fc,fu,(*func)(u))
1704: #ifdef DEBUG
1705: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 1706: #endif
1707: }
1.162 brouard 1708: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1709: #ifdef DEBUG
1.224 brouard 1710: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1711: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 1712: #endif
1.126 brouard 1713: u=ulim;
1714: fu=(*func)(u);
1.183 brouard 1715: } else { /* u could be left to b (if r > q parabola has a maximum) */
1716: #ifdef DEBUG
1.224 brouard 1717: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1718: fprintf(ficlog,"\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1.183 brouard 1719: #endif
1.126 brouard 1720: u=(*cx)+GOLD*(*cx-*bx);
1721: fu=(*func)(u);
1.224 brouard 1722: #ifdef DEBUG
1723: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1724: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1725: #endif
1.183 brouard 1726: } /* end tests */
1.126 brouard 1727: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1728: SHFT(*fa,*fb,*fc,fu)
1729: #ifdef DEBUG
1.224 brouard 1730: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1731: fprintf(ficlog, "\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1.183 brouard 1732: #endif
1733: } /* end while; ie return (a, b, c, fa, fb, fc) such that a < b < c with f(a) > f(b) and fb < f(c) */
1.126 brouard 1734: }
1735:
1736: /*************** linmin ************************/
1.162 brouard 1737: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1738: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1739: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1740: the value of func at the returned location p . This is actually all accomplished by calling the
1741: routines mnbrak and brent .*/
1.126 brouard 1742: int ncom;
1743: double *pcom,*xicom;
1744: double (*nrfunc)(double []);
1745:
1.224 brouard 1746: #ifdef LINMINORIGINAL
1.126 brouard 1747: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 1748: #else
1749: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
1750: #endif
1.126 brouard 1751: {
1752: double brent(double ax, double bx, double cx,
1753: double (*f)(double), double tol, double *xmin);
1754: double f1dim(double x);
1755: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1756: double *fc, double (*func)(double));
1757: int j;
1758: double xx,xmin,bx,ax;
1759: double fx,fb,fa;
1.187 brouard 1760:
1.203 brouard 1761: #ifdef LINMINORIGINAL
1762: #else
1763: double scale=10., axs, xxs; /* Scale added for infinity */
1764: #endif
1765:
1.126 brouard 1766: ncom=n;
1767: pcom=vector(1,n);
1768: xicom=vector(1,n);
1769: nrfunc=func;
1770: for (j=1;j<=n;j++) {
1771: pcom[j]=p[j];
1.202 brouard 1772: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1773: }
1.187 brouard 1774:
1.203 brouard 1775: #ifdef LINMINORIGINAL
1776: xx=1.;
1777: #else
1778: axs=0.0;
1779: xxs=1.;
1780: do{
1781: xx= xxs;
1782: #endif
1.187 brouard 1783: ax=0.;
1784: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1785: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1786: /* 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)) */
1787: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1788: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1789: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1790: /* Find a bracket a,x,b in direction n=xi ie xicom, order may change. Scale is [0:xxs*xi[j]] et non plus [0:xi[j]]*/
1.203 brouard 1791: #ifdef LINMINORIGINAL
1792: #else
1793: if (fx != fx){
1.224 brouard 1794: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1795: printf("|");
1796: fprintf(ficlog,"|");
1.203 brouard 1797: #ifdef DEBUGLINMIN
1.224 brouard 1798: printf("\nLinmin NAN : input [axs=%lf:xxs=%lf], mnbrak outputs fx=%lf <(fb=%lf and fa=%lf) with xx=%lf in [ax=%lf:bx=%lf] \n", axs, xxs, fx,fb, fa, xx, ax, bx);
1.203 brouard 1799: #endif
1800: }
1.224 brouard 1801: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 1802: #endif
1803:
1.191 brouard 1804: #ifdef DEBUGLINMIN
1805: printf("\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.202 brouard 1806: fprintf(ficlog,"\nLinmin after mnbrak: ax=%12.7f xx=%12.7f bx=%12.7f fa=%12.2f fx=%12.2f fb=%12.2f\n", ax,xx,bx,fa,fx,fb);
1.191 brouard 1807: #endif
1.224 brouard 1808: #ifdef LINMINORIGINAL
1809: #else
1810: if(fb == fx){ /* Flat function in the direction */
1811: xmin=xx;
1812: *flat=1;
1813: }else{
1814: *flat=0;
1815: #endif
1816: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 1817: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
1818: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
1819: /* fmin = f(p[j] + xmin * xi[j]) */
1820: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
1821: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 1822: #ifdef DEBUG
1.224 brouard 1823: 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);
1824: 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);
1825: #endif
1826: #ifdef LINMINORIGINAL
1827: #else
1828: }
1.126 brouard 1829: #endif
1.191 brouard 1830: #ifdef DEBUGLINMIN
1831: printf("linmin end ");
1.202 brouard 1832: fprintf(ficlog,"linmin end ");
1.191 brouard 1833: #endif
1.126 brouard 1834: for (j=1;j<=n;j++) {
1.203 brouard 1835: #ifdef LINMINORIGINAL
1836: xi[j] *= xmin;
1837: #else
1838: #ifdef DEBUGLINMIN
1839: if(xxs <1.0)
1840: printf(" before xi[%d]=%12.8f", j,xi[j]);
1841: #endif
1842: 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) */
1843: #ifdef DEBUGLINMIN
1844: if(xxs <1.0)
1845: 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 );
1846: #endif
1847: #endif
1.187 brouard 1848: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 1849: }
1.191 brouard 1850: #ifdef DEBUGLINMIN
1.203 brouard 1851: printf("\n");
1.191 brouard 1852: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 1853: fprintf(ficlog,"Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.191 brouard 1854: for (j=1;j<=n;j++) {
1.202 brouard 1855: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1856: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1857: if(j % ncovmodel == 0){
1.191 brouard 1858: printf("\n");
1.202 brouard 1859: fprintf(ficlog,"\n");
1860: }
1.191 brouard 1861: }
1.203 brouard 1862: #else
1.191 brouard 1863: #endif
1.126 brouard 1864: free_vector(xicom,1,n);
1865: free_vector(pcom,1,n);
1866: }
1867:
1868:
1869: /*************** powell ************************/
1.162 brouard 1870: /*
1871: Minimization of a function func of n variables. Input consists of an initial starting point
1872: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
1873: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
1874: such that failure to decrease by more than this amount on one iteration signals doneness. On
1875: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
1876: function value at p , and iter is the number of iterations taken. The routine linmin is used.
1877: */
1.224 brouard 1878: #ifdef LINMINORIGINAL
1879: #else
1880: int *flatdir; /* Function is vanishing in that direction */
1.225 ! brouard 1881: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 1882: #endif
1.126 brouard 1883: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
1884: double (*func)(double []))
1885: {
1.224 brouard 1886: #ifdef LINMINORIGINAL
1887: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 1888: double (*func)(double []));
1.224 brouard 1889: #else
1890: void linmin(double p[], double xi[], int n, double *fret,
1891: double (*func)(double []),int *flat);
1892: #endif
1.126 brouard 1893: int i,ibig,j;
1894: double del,t,*pt,*ptt,*xit;
1.181 brouard 1895: double directest;
1.126 brouard 1896: double fp,fptt;
1897: double *xits;
1898: int niterf, itmp;
1.224 brouard 1899: #ifdef LINMINORIGINAL
1900: #else
1901:
1902: flatdir=ivector(1,n);
1903: for (j=1;j<=n;j++) flatdir[j]=0;
1904: #endif
1.126 brouard 1905:
1906: pt=vector(1,n);
1907: ptt=vector(1,n);
1908: xit=vector(1,n);
1909: xits=vector(1,n);
1910: *fret=(*func)(p);
1911: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 1912: rcurr_time = time(NULL);
1.126 brouard 1913: for (*iter=1;;++(*iter)) {
1.187 brouard 1914: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 1915: ibig=0;
1916: del=0.0;
1.157 brouard 1917: rlast_time=rcurr_time;
1918: /* (void) gettimeofday(&curr_time,&tzp); */
1919: rcurr_time = time(NULL);
1920: curr_time = *localtime(&rcurr_time);
1921: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
1922: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
1923: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 1924: for (i=1;i<=n;i++) {
1.126 brouard 1925: printf(" %d %.12f",i, p[i]);
1926: fprintf(ficlog," %d %.12lf",i, p[i]);
1927: fprintf(ficrespow," %.12lf", p[i]);
1928: }
1929: printf("\n");
1930: fprintf(ficlog,"\n");
1931: fprintf(ficrespow,"\n");fflush(ficrespow);
1932: if(*iter <=3){
1.157 brouard 1933: tml = *localtime(&rcurr_time);
1934: strcpy(strcurr,asctime(&tml));
1935: rforecast_time=rcurr_time;
1.126 brouard 1936: itmp = strlen(strcurr);
1937: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.224 brouard 1938: strcurr[itmp-1]='\0';
1.162 brouard 1939: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 1940: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 1941: for(niterf=10;niterf<=30;niterf+=10){
1.224 brouard 1942: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
1943: forecast_time = *localtime(&rforecast_time);
1944: strcpy(strfor,asctime(&forecast_time));
1945: itmp = strlen(strfor);
1946: if(strfor[itmp-1]=='\n')
1947: strfor[itmp-1]='\0';
1948: 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);
1949: fprintf(ficlog," - if your program needs %d iterations to converge, convergence will be \n reached in %s i.e.\n on %s (current time is %s);\n",niterf, asc_diff_time(rforecast_time-rcurr_time,tmpout),strfor,strcurr);
1.126 brouard 1950: }
1951: }
1.187 brouard 1952: for (i=1;i<=n;i++) { /* For each direction i */
1953: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 1954: fptt=(*fret);
1955: #ifdef DEBUG
1.203 brouard 1956: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1957: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 1958: #endif
1.203 brouard 1959: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 1960: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 1961: #ifdef LINMINORIGINAL
1.188 brouard 1962: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 1963: #else
1964: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1965: flatdir[i]=flat; /* Function is vanishing in that direction i */
1966: #endif
1967: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 1968: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 1969: /* because that direction will be replaced unless the gain del is small */
1970: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
1971: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
1972: /* with the new direction. */
1973: del=fabs(fptt-(*fret));
1974: ibig=i;
1.126 brouard 1975: }
1976: #ifdef DEBUG
1977: printf("%d %.12e",i,(*fret));
1978: fprintf(ficlog,"%d %.12e",i,(*fret));
1979: for (j=1;j<=n;j++) {
1.224 brouard 1980: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
1981: printf(" x(%d)=%.12e",j,xit[j]);
1982: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 1983: }
1984: for(j=1;j<=n;j++) {
1.225 ! brouard 1985: printf(" p(%d)=%.12e",j,p[j]);
! 1986: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 1987: }
1988: printf("\n");
1989: fprintf(ficlog,"\n");
1990: #endif
1.187 brouard 1991: } /* end loop on each direction i */
1992: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 1993: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 1994: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 1995: for(j=1;j<=n;j++) {
1.225 ! brouard 1996: if(flatdir[j] >0){
! 1997: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
! 1998: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
! 1999: }
! 2000: /* printf("\n"); */
! 2001: /* fprintf(ficlog,"\n"); */
! 2002: }
1.182 brouard 2003: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /* Did we reach enough precision? */
1.188 brouard 2004: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2005: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2006: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2007: /* decreased of more than 3.84 */
2008: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2009: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2010: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2011:
1.188 brouard 2012: /* Starting the program with initial values given by a former maximization will simply change */
2013: /* the scales of the directions and the directions, because the are reset to canonical directions */
2014: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2015: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2016: #ifdef DEBUG
2017: int k[2],l;
2018: k[0]=1;
2019: k[1]=-1;
2020: printf("Max: %.12e",(*func)(p));
2021: fprintf(ficlog,"Max: %.12e",(*func)(p));
2022: for (j=1;j<=n;j++) {
2023: printf(" %.12e",p[j]);
2024: fprintf(ficlog," %.12e",p[j]);
2025: }
2026: printf("\n");
2027: fprintf(ficlog,"\n");
2028: for(l=0;l<=1;l++) {
2029: for (j=1;j<=n;j++) {
2030: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2031: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2032: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2033: }
2034: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2035: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2036: }
2037: #endif
2038:
1.224 brouard 2039: #ifdef LINMINORIGINAL
2040: #else
2041: free_ivector(flatdir,1,n);
2042: #endif
1.126 brouard 2043: free_vector(xit,1,n);
2044: free_vector(xits,1,n);
2045: free_vector(ptt,1,n);
2046: free_vector(pt,1,n);
2047: return;
1.192 brouard 2048: } /* enough precision */
1.126 brouard 2049: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2050: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2051: ptt[j]=2.0*p[j]-pt[j];
2052: xit[j]=p[j]-pt[j];
2053: pt[j]=p[j];
2054: }
1.181 brouard 2055: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2056: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2057: if (*iter <=4) {
1.225 ! brouard 2058: #else
! 2059: #endif
1.224 brouard 2060: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2061: #else
1.161 brouard 2062: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2063: #endif
1.162 brouard 2064: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2065: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2066: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2067: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2068: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2069: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2070: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2071: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2072: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2073: /* Even if f3 <f1, directest can be negative and t >0 */
2074: /* mu² and del² are equal when f3=f1 */
2075: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2076: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2077: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2078: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2079: #ifdef NRCORIGINAL
2080: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2081: #else
2082: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del); /* Intel compiler doesn't work on one line; bug reported */
1.161 brouard 2083: t= t- del*SQR(fp-fptt);
1.183 brouard 2084: #endif
1.202 brouard 2085: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2086: #ifdef DEBUG
1.181 brouard 2087: 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);
2088: fprintf(ficlog,"t1= %.12lf, t2= %.12lf, t=%.12lf directest=%.12lf\n", 2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del),del*SQR(fp-fptt),t,directest);
1.161 brouard 2089: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2090: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2091: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2092: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2093: 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);
2094: 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);
2095: #endif
1.183 brouard 2096: #ifdef POWELLORIGINAL
2097: if (t < 0.0) { /* Then we use it for new direction */
2098: #else
1.182 brouard 2099: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2100: printf("directest= %.12lf (if <0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt,del);
1.192 brouard 2101: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
1.224 brouard 2102: fprintf(ficlog,"directest= %.12lf (if directest<0 or t<0 we include P0 Pn as new direction), t= %.12lf, f1= %.12lf,f2= %.12lf,f3= %.12lf, del= %.12lf\n",directest, t, fp,(*fret),fptt, del);
1.192 brouard 2103: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2104: }
1.181 brouard 2105: if (directest < 0.0) { /* Then we use it for new direction */
2106: #endif
1.191 brouard 2107: #ifdef DEBUGLINMIN
1.224 brouard 2108: printf("Before linmin in direction P%d-P0\n",n);
2109: for (j=1;j<=n;j++) {
2110: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2111: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2112: if(j % ncovmodel == 0){
2113: printf("\n");
2114: fprintf(ficlog,"\n");
2115: }
2116: }
2117: #endif
2118: #ifdef LINMINORIGINAL
2119: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2120: #else
2121: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2122: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2123: #endif
1.224 brouard 2124:
1.191 brouard 2125: #ifdef DEBUGLINMIN
1.224 brouard 2126: for (j=1;j<=n;j++) {
2127: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2128: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2129: if(j % ncovmodel == 0){
2130: printf("\n");
2131: fprintf(ficlog,"\n");
2132: }
2133: }
2134: #endif
2135: for (j=1;j<=n;j++) {
2136: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2137: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2138: }
2139: #ifdef LINMINORIGINAL
2140: #else
1.225 ! brouard 2141: for (j=1, flatd=0;j<=n;j++) {
! 2142: if(flatdir[j]>0)
! 2143: flatd++;
! 2144: }
! 2145: if(flatd >0){
! 2146: printf("%d flat directions\n",flatd);
! 2147: fprintf(ficlog,"%d flat directions\n",flatd);
! 2148: for (j=1;j<=n;j++) {
! 2149: if(flatdir[j]>0){
! 2150: printf("%d ",j);
! 2151: fprintf(ficlog,"%d ",j);
! 2152: }
! 2153: }
! 2154: printf("\n");
! 2155: fprintf(ficlog,"\n");
! 2156: }
1.191 brouard 2157: #endif
1.224 brouard 2158: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2159: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2160:
1.126 brouard 2161: #ifdef DEBUG
1.224 brouard 2162: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2163: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2164: for(j=1;j<=n;j++){
2165: printf(" %lf",xit[j]);
2166: fprintf(ficlog," %lf",xit[j]);
2167: }
2168: printf("\n");
2169: fprintf(ficlog,"\n");
1.126 brouard 2170: #endif
1.192 brouard 2171: } /* end of t or directest negative */
1.224 brouard 2172: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2173: #else
1.162 brouard 2174: } /* end if (fptt < fp) */
1.192 brouard 2175: #endif
1.225 ! brouard 2176: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.224 brouard 2177: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 ! brouard 2178: #else
1.224 brouard 2179: #endif
1.192 brouard 2180: } /* loop iteration */
1.126 brouard 2181: }
2182:
2183: /**** Prevalence limit (stable or period prevalence) ****************/
2184:
1.203 brouard 2185: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij)
1.126 brouard 2186: {
1.218 brouard 2187: /* Computes the prevalence limit in each live state at age x and for covariate ij by left multiplying the unit
1.203 brouard 2188: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2189: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2190: /* Wx is row vector: population in state 1, population in state 2, population dead */
2191: /* or prevalence in state 1, prevalence in state 2, 0 */
2192: /* newm is the matrix after multiplications, its rows are identical at a factor */
2193: /* Initial matrix pimij */
2194: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2195: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2196: /* 0, 0 , 1} */
2197: /*
2198: * and after some iteration: */
2199: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2200: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2201: /* 0, 0 , 1} */
2202: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2203: /* {0.51571254859325999, 0.4842874514067399, */
2204: /* 0.51326036147820708, 0.48673963852179264} */
2205: /* If we start from prlim again, prlim tends to a constant matrix */
2206:
1.126 brouard 2207: int i, ii,j,k;
1.209 brouard 2208: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2209: /* double **matprod2(); */ /* test */
1.218 brouard 2210: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2211: double **newm;
1.209 brouard 2212: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2213: int ncvloop=0;
1.169 brouard 2214:
1.209 brouard 2215: min=vector(1,nlstate);
2216: max=vector(1,nlstate);
2217: meandiff=vector(1,nlstate);
2218:
1.218 brouard 2219: /* Starting with matrix unity */
1.126 brouard 2220: for (ii=1;ii<=nlstate+ndeath;ii++)
2221: for (j=1;j<=nlstate+ndeath;j++){
2222: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2223: }
1.169 brouard 2224:
2225: cov[1]=1.;
2226:
2227: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2228: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2229: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2230: ncvloop++;
1.126 brouard 2231: newm=savm;
2232: /* Covariates have to be included here again */
1.138 brouard 2233: cov[2]=agefin;
1.187 brouard 2234: if(nagesqr==1)
2235: cov[3]= agefin*agefin;;
1.138 brouard 2236: for (k=1; k<=cptcovn;k++) {
1.200 brouard 2237: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.218 brouard 2238: /* Here comes the value of the covariate 'ij' */
1.200 brouard 2239: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
1.198 brouard 2240: /* 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])]); */
1.138 brouard 2241: }
1.186 brouard 2242: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.200 brouard 2243: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2244: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
1.186 brouard 2245: for (k=1; k<=cptcovprod;k++) /* Useless */
1.200 brouard 2246: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2247: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
1.138 brouard 2248:
2249: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2250: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2251: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2252: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2253: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2254: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2255: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2256:
1.126 brouard 2257: savm=oldm;
2258: oldm=newm;
1.209 brouard 2259:
2260: for(j=1; j<=nlstate; j++){
2261: max[j]=0.;
2262: min[j]=1.;
2263: }
2264: for(i=1;i<=nlstate;i++){
2265: sumnew=0;
2266: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2267: for(j=1; j<=nlstate; j++){
2268: prlim[i][j]= newm[i][j]/(1-sumnew);
2269: max[j]=FMAX(max[j],prlim[i][j]);
2270: min[j]=FMIN(min[j],prlim[i][j]);
2271: }
2272: }
2273:
1.126 brouard 2274: maxmax=0.;
1.209 brouard 2275: for(j=1; j<=nlstate; j++){
2276: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2277: maxmax=FMAX(maxmax,meandiff[j]);
2278: /* printf(" age= %d meandiff[%d]=%f, agefin=%d max[%d]=%f min[%d]=%f maxmax=%f\n", (int)age, j, meandiff[j],(int)agefin, j, max[j], j, min[j],maxmax); */
1.169 brouard 2279: } /* j loop */
1.203 brouard 2280: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2281: /* printf("maxmax=%lf maxmin=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, maxmin, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.126 brouard 2282: if(maxmax < ftolpl){
1.209 brouard 2283: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2284: free_vector(min,1,nlstate);
2285: free_vector(max,1,nlstate);
2286: free_vector(meandiff,1,nlstate);
1.126 brouard 2287: return prlim;
2288: }
1.169 brouard 2289: } /* age loop */
1.208 brouard 2290: /* After some age loop it doesn't converge */
1.209 brouard 2291: printf("Warning: the stable prevalence at age %d did not converge with the required precision (%g > ftolpl=%g) within %.0f years. Try to lower 'ftolpl'. \n\
1.208 brouard 2292: Earliest 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);
1.209 brouard 2293: /* 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); */
2294: free_vector(min,1,nlstate);
2295: free_vector(max,1,nlstate);
2296: free_vector(meandiff,1,nlstate);
1.208 brouard 2297:
1.169 brouard 2298: return prlim; /* should not reach here */
1.126 brouard 2299: }
2300:
1.217 brouard 2301:
2302: /**** Back Prevalence limit (stable or period prevalence) ****************/
2303:
1.218 brouard 2304: /* 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) */
2305: /* 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) */
2306: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij)
1.217 brouard 2307: {
1.218 brouard 2308: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2309: matrix by transitions matrix until convergence is reached with precision ftolpl */
2310: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2311: /* Wx is row vector: population in state 1, population in state 2, population dead */
2312: /* or prevalence in state 1, prevalence in state 2, 0 */
2313: /* newm is the matrix after multiplications, its rows are identical at a factor */
2314: /* Initial matrix pimij */
2315: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2316: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2317: /* 0, 0 , 1} */
2318: /*
2319: * and after some iteration: */
2320: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2321: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2322: /* 0, 0 , 1} */
2323: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2324: /* {0.51571254859325999, 0.4842874514067399, */
2325: /* 0.51326036147820708, 0.48673963852179264} */
2326: /* If we start from prlim again, prlim tends to a constant matrix */
2327:
2328: int i, ii,j,k;
2329: double *min, *max, *meandiff, maxmax,sumnew=0.;
2330: /* double **matprod2(); */ /* test */
2331: double **out, cov[NCOVMAX+1], **bmij();
2332: double **newm;
1.218 brouard 2333: double **dnewm, **doldm, **dsavm; /* for use */
2334: double **oldm, **savm; /* for use */
2335:
1.217 brouard 2336: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2337: int ncvloop=0;
2338:
2339: min=vector(1,nlstate);
2340: max=vector(1,nlstate);
2341: meandiff=vector(1,nlstate);
2342:
1.218 brouard 2343: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2344: oldm=oldms; savm=savms;
2345:
2346: /* Starting with matrix unity */
2347: for (ii=1;ii<=nlstate+ndeath;ii++)
2348: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2349: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2350: }
2351:
2352: cov[1]=1.;
2353:
2354: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2355: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2356: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2357: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2358: ncvloop++;
1.218 brouard 2359: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2360: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2361: /* Covariates have to be included here again */
2362: cov[2]=agefin;
2363: if(nagesqr==1)
2364: cov[3]= agefin*agefin;;
2365: for (k=1; k<=cptcovn;k++) {
2366: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
2367: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2368: /* 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])]); */
2369: }
2370: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2371: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2372: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
2373: for (k=1; k<=cptcovprod;k++) /* Useless */
2374: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2375: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2376:
2377: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2378: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2379: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2380: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2381: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2382: /* ij should be linked to the correct index of cov */
2383: /* age and covariate values ij are in 'cov', but we need to pass
2384: * ij for the observed prevalence at age and status and covariate
2385: * number: prevacurrent[(int)agefin][ii][ij]
2386: */
2387: /* 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 *\/ */
2388: /* 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 *\/ */
2389: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij)); /* Bug Valgrind */
1.217 brouard 2390: savm=oldm;
2391: oldm=newm;
2392: for(j=1; j<=nlstate; j++){
2393: max[j]=0.;
2394: min[j]=1.;
2395: }
2396: for(j=1; j<=nlstate; j++){
2397: for(i=1;i<=nlstate;i++){
1.218 brouard 2398: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2399: bprlim[i][j]= newm[i][j];
2400: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2401: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2402: }
2403: }
1.218 brouard 2404:
1.217 brouard 2405: maxmax=0.;
2406: for(i=1; i<=nlstate; i++){
2407: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2408: maxmax=FMAX(maxmax,meandiff[i]);
2409: /* 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); */
2410: } /* j loop */
2411: *ncvyear= -( (int)age- (int)agefin);
1.218 brouard 2412: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2413: if(maxmax < ftolpl){
1.220 brouard 2414: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2415: free_vector(min,1,nlstate);
2416: free_vector(max,1,nlstate);
2417: free_vector(meandiff,1,nlstate);
2418: return bprlim;
2419: }
2420: } /* age loop */
2421: /* After some age loop it doesn't converge */
2422: 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'. \n\
2423: 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);
2424: /* 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); */
2425: free_vector(min,1,nlstate);
2426: free_vector(max,1,nlstate);
2427: free_vector(meandiff,1,nlstate);
2428:
2429: return bprlim; /* should not reach here */
2430: }
2431:
1.126 brouard 2432: /*************** transition probabilities ***************/
2433:
2434: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2435: {
1.138 brouard 2436: /* According to parameters values stored in x and the covariate's values stored in cov,
2437: computes the probability to be observed in state j being in state i by appying the
2438: model to the ncovmodel covariates (including constant and age).
2439: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2440: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2441: ncth covariate in the global vector x is given by the formula:
2442: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2443: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2444: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2445: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2446: Outputs ps[i][j] the probability to be observed in j being in j according to
2447: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2448: */
2449: double s1, lnpijopii;
1.126 brouard 2450: /*double t34;*/
1.164 brouard 2451: int i,j, nc, ii, jj;
1.126 brouard 2452:
1.223 brouard 2453: for(i=1; i<= nlstate; i++){
2454: for(j=1; j<i;j++){
2455: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2456: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2457: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2458: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2459: }
2460: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2461: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2462: }
2463: for(j=i+1; j<=nlstate+ndeath;j++){
2464: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2465: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2466: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2467: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2468: }
2469: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2470: }
2471: }
1.218 brouard 2472:
1.223 brouard 2473: for(i=1; i<= nlstate; i++){
2474: s1=0;
2475: for(j=1; j<i; j++){
2476: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2477: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2478: }
2479: for(j=i+1; j<=nlstate+ndeath; j++){
2480: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2481: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2482: }
2483: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2484: ps[i][i]=1./(s1+1.);
2485: /* Computing other pijs */
2486: for(j=1; j<i; j++)
2487: ps[i][j]= exp(ps[i][j])*ps[i][i];
2488: for(j=i+1; j<=nlstate+ndeath; j++)
2489: ps[i][j]= exp(ps[i][j])*ps[i][i];
2490: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2491: } /* end i */
1.218 brouard 2492:
1.223 brouard 2493: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2494: for(jj=1; jj<= nlstate+ndeath; jj++){
2495: ps[ii][jj]=0;
2496: ps[ii][ii]=1;
2497: }
2498: }
1.218 brouard 2499:
2500:
1.223 brouard 2501: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2502: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2503: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2504: /* } */
2505: /* printf("\n "); */
2506: /* } */
2507: /* printf("\n ");printf("%lf ",cov[2]);*/
2508: /*
2509: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 2510: goto end;*/
1.223 brouard 2511: return ps;
1.126 brouard 2512: }
2513:
1.218 brouard 2514: /*************** backward transition probabilities ***************/
2515:
2516: /* 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 ) */
2517: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2518: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
2519: {
1.222 brouard 2520: /* Computes the backward probability at age agefin and covariate ij
2521: * and returns in **ps as well as **bmij.
2522: */
1.218 brouard 2523: int i, ii, j,k;
1.222 brouard 2524:
2525: double **out, **pmij();
2526: double sumnew=0.;
1.218 brouard 2527: double agefin;
1.222 brouard 2528:
2529: double **dnewm, **dsavm, **doldm;
2530: double **bbmij;
2531:
1.218 brouard 2532: doldm=ddoldms; /* global pointers */
1.222 brouard 2533: dnewm=ddnewms;
2534: dsavm=ddsavms;
2535:
2536: agefin=cov[2];
2537: /* bmij *//* age is cov[2], ij is included in cov, but we need for
2538: the observed prevalence (with this covariate ij) */
2539: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
2540: /* We do have the matrix Px in savm and we need pij */
2541: for (j=1;j<=nlstate+ndeath;j++){
2542: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
2543: for (ii=1;ii<=nlstate;ii++){
2544: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
2545: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
2546: for (ii=1;ii<=nlstate+ndeath;ii++){
2547: if(sumnew >= 1.e-10){
2548: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
2549: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2550: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
2551: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2552: /* }else */
2553: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
2554: }else{
2555: printf("ii=%d, i=%d, doldm=%lf dsavm=%lf, probs=%lf, sumnew=%lf,agefin=%d\n",ii,j,doldm[ii][j],dsavm[ii][j],prevacurrent[(int)agefin][ii][ij],sumnew, (int)agefin);
2556: }
2557: } /*End ii */
2558: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
2559: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
2560: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
2561: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
2562: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2563: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2564: /* left Product of this matrix by diag matrix of prevalences (savm) */
2565: for (j=1;j<=nlstate+ndeath;j++){
2566: for (ii=1;ii<=nlstate+ndeath;ii++){
2567: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
2568: }
2569: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
2570: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
2571: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
2572: /* end bmij */
2573: return ps;
1.218 brouard 2574: }
1.217 brouard 2575: /*************** transition probabilities ***************/
2576:
1.218 brouard 2577: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2578: {
2579: /* According to parameters values stored in x and the covariate's values stored in cov,
2580: computes the probability to be observed in state j being in state i by appying the
2581: model to the ncovmodel covariates (including constant and age).
2582: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2583: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2584: ncth covariate in the global vector x is given by the formula:
2585: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2586: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2587: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2588: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2589: Outputs ps[i][j] the probability to be observed in j being in j according to
2590: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2591: */
2592: double s1, lnpijopii;
2593: /*double t34;*/
2594: int i,j, nc, ii, jj;
2595:
1.218 brouard 2596: for(i=1; i<= nlstate; i++){
2597: for(j=1; j<i;j++){
2598: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2599: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2600: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2601: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2602: }
2603: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2604: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2605: }
2606: for(j=i+1; j<=nlstate+ndeath;j++){
2607: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2608: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2609: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2610: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2611: }
2612: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2613: }
2614: }
2615:
2616: for(i=1; i<= nlstate; i++){
2617: s1=0;
2618: for(j=1; j<i; j++){
2619: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2620: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2621: }
2622: for(j=i+1; j<=nlstate+ndeath; j++){
2623: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2624: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2625: }
2626: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2627: ps[i][i]=1./(s1+1.);
2628: /* Computing other pijs */
2629: for(j=1; j<i; j++)
2630: ps[i][j]= exp(ps[i][j])*ps[i][i];
2631: for(j=i+1; j<=nlstate+ndeath; j++)
2632: ps[i][j]= exp(ps[i][j])*ps[i][i];
2633: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2634: } /* end i */
2635:
2636: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2637: for(jj=1; jj<= nlstate+ndeath; jj++){
2638: ps[ii][jj]=0;
2639: ps[ii][ii]=1;
2640: }
2641: }
2642: /* Added for backcast */ /* Transposed matrix too */
2643: for(jj=1; jj<= nlstate+ndeath; jj++){
2644: s1=0.;
2645: for(ii=1; ii<= nlstate+ndeath; ii++){
2646: s1+=ps[ii][jj];
2647: }
2648: for(ii=1; ii<= nlstate; ii++){
2649: ps[ii][jj]=ps[ii][jj]/s1;
2650: }
2651: }
2652: /* Transposition */
2653: for(jj=1; jj<= nlstate+ndeath; jj++){
2654: for(ii=jj; ii<= nlstate+ndeath; ii++){
2655: s1=ps[ii][jj];
2656: ps[ii][jj]=ps[jj][ii];
2657: ps[jj][ii]=s1;
2658: }
2659: }
2660: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2661: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2662: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2663: /* } */
2664: /* printf("\n "); */
2665: /* } */
2666: /* printf("\n ");printf("%lf ",cov[2]);*/
2667: /*
2668: for(i=1; i<= npar; i++) printf("%f ",x[i]);
2669: goto end;*/
2670: return ps;
1.217 brouard 2671: }
2672:
2673:
1.126 brouard 2674: /**************** Product of 2 matrices ******************/
2675:
1.145 brouard 2676: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2677: {
2678: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2679: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2680: /* in, b, out are matrice of pointers which should have been initialized
2681: before: only the contents of out is modified. The function returns
2682: a pointer to pointers identical to out */
1.145 brouard 2683: int i, j, k;
1.126 brouard 2684: for(i=nrl; i<= nrh; i++)
1.145 brouard 2685: for(k=ncolol; k<=ncoloh; k++){
2686: out[i][k]=0.;
2687: for(j=ncl; j<=nch; j++)
2688: out[i][k] +=in[i][j]*b[j][k];
2689: }
1.126 brouard 2690: return out;
2691: }
2692:
2693:
2694: /************* Higher Matrix Product ***************/
2695:
2696: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
2697: {
1.218 brouard 2698: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2699: 'nhstepm*hstepm*stepm' months (i.e. until
2700: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2701: nhstepm*hstepm matrices.
2702: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2703: (typically every 2 years instead of every month which is too big
2704: for the memory).
2705: Model is determined by parameters x and covariates have to be
2706: included manually here.
2707:
2708: */
2709:
2710: int i, j, d, h, k;
1.131 brouard 2711: double **out, cov[NCOVMAX+1];
1.126 brouard 2712: double **newm;
1.187 brouard 2713: double agexact;
1.214 brouard 2714: double agebegin, ageend;
1.126 brouard 2715:
2716: /* Hstepm could be zero and should return the unit matrix */
2717: for (i=1;i<=nlstate+ndeath;i++)
2718: for (j=1;j<=nlstate+ndeath;j++){
2719: oldm[i][j]=(i==j ? 1.0 : 0.0);
2720: po[i][j][0]=(i==j ? 1.0 : 0.0);
2721: }
2722: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2723: for(h=1; h <=nhstepm; h++){
2724: for(d=1; d <=hstepm; d++){
2725: newm=savm;
2726: /* Covariates have to be included here again */
2727: cov[1]=1.;
1.214 brouard 2728: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 2729: cov[2]=agexact;
2730: if(nagesqr==1)
1.218 brouard 2731: cov[3]= agexact*agexact;
1.131 brouard 2732: for (k=1; k<=cptcovn;k++)
1.218 brouard 2733: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2734: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.186 brouard 2735: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.218 brouard 2736: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2737: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2738: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.145 brouard 2739: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.218 brouard 2740: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2741: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
1.126 brouard 2742:
2743:
2744: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2745: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2746: /* right multiplication of oldm by the current matrix */
1.126 brouard 2747: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
2748: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 2749: /* if((int)age == 70){ */
2750: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2751: /* for(i=1; i<=nlstate+ndeath; i++) { */
2752: /* printf("%d pmmij ",i); */
2753: /* for(j=1;j<=nlstate+ndeath;j++) { */
2754: /* printf("%f ",pmmij[i][j]); */
2755: /* } */
2756: /* printf(" oldm "); */
2757: /* for(j=1;j<=nlstate+ndeath;j++) { */
2758: /* printf("%f ",oldm[i][j]); */
2759: /* } */
2760: /* printf("\n"); */
2761: /* } */
2762: /* } */
1.126 brouard 2763: savm=oldm;
2764: oldm=newm;
2765: }
2766: for(i=1; i<=nlstate+ndeath; i++)
2767: for(j=1;j<=nlstate+ndeath;j++) {
1.218 brouard 2768: po[i][j][h]=newm[i][j];
2769: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 2770: }
1.128 brouard 2771: /*printf("h=%d ",h);*/
1.126 brouard 2772: } /* end h */
1.218 brouard 2773: /* printf("\n H=%d \n",h); */
1.126 brouard 2774: return po;
2775: }
2776:
1.217 brouard 2777: /************* Higher Back Matrix Product ***************/
1.218 brouard 2778: /* double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, double **oldm, double **savm, double **dnewm, double **doldm, double **dsavm, int ij ) */
1.222 brouard 2779: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 2780: {
1.218 brouard 2781: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 2782: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 2783: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2784: nhstepm*hstepm matrices.
2785: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2786: (typically every 2 years instead of every month which is too big
1.217 brouard 2787: for the memory).
1.218 brouard 2788: Model is determined by parameters x and covariates have to be
2789: included manually here.
1.217 brouard 2790:
1.222 brouard 2791: */
1.217 brouard 2792:
2793: int i, j, d, h, k;
2794: double **out, cov[NCOVMAX+1];
2795: double **newm;
2796: double agexact;
2797: double agebegin, ageend;
1.222 brouard 2798: double **oldm, **savm;
1.217 brouard 2799:
1.222 brouard 2800: oldm=oldms;savm=savms;
1.217 brouard 2801: /* Hstepm could be zero and should return the unit matrix */
2802: for (i=1;i<=nlstate+ndeath;i++)
2803: for (j=1;j<=nlstate+ndeath;j++){
2804: oldm[i][j]=(i==j ? 1.0 : 0.0);
2805: po[i][j][0]=(i==j ? 1.0 : 0.0);
2806: }
2807: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2808: for(h=1; h <=nhstepm; h++){
2809: for(d=1; d <=hstepm; d++){
2810: newm=savm;
2811: /* Covariates have to be included here again */
2812: cov[1]=1.;
2813: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
2814: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
2815: cov[2]=agexact;
2816: if(nagesqr==1)
1.222 brouard 2817: cov[3]= agexact*agexact;
1.218 brouard 2818: for (k=1; k<=cptcovn;k++)
1.222 brouard 2819: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2820: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 2821: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.222 brouard 2822: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2823: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2824: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 2825: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.222 brouard 2826: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2827: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
1.218 brouard 2828:
2829:
1.217 brouard 2830: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2831: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2832: /* Careful transposed matrix */
1.222 brouard 2833: /* age is in cov[2] */
1.218 brouard 2834: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 2835: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 2836: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 2837: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 2838: /* if((int)age == 70){ */
2839: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2840: /* for(i=1; i<=nlstate+ndeath; i++) { */
2841: /* printf("%d pmmij ",i); */
2842: /* for(j=1;j<=nlstate+ndeath;j++) { */
2843: /* printf("%f ",pmmij[i][j]); */
2844: /* } */
2845: /* printf(" oldm "); */
2846: /* for(j=1;j<=nlstate+ndeath;j++) { */
2847: /* printf("%f ",oldm[i][j]); */
2848: /* } */
2849: /* printf("\n"); */
2850: /* } */
2851: /* } */
2852: savm=oldm;
2853: oldm=newm;
2854: }
2855: for(i=1; i<=nlstate+ndeath; i++)
2856: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 2857: po[i][j][h]=newm[i][j];
2858: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 2859: }
2860: /*printf("h=%d ",h);*/
2861: } /* end h */
1.222 brouard 2862: /* printf("\n H=%d \n",h); */
1.217 brouard 2863: return po;
2864: }
2865:
2866:
1.162 brouard 2867: #ifdef NLOPT
2868: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
2869: double fret;
2870: double *xt;
2871: int j;
2872: myfunc_data *d2 = (myfunc_data *) pd;
2873: /* xt = (p1-1); */
2874: xt=vector(1,n);
2875: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
2876:
2877: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
2878: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
2879: printf("Function = %.12lf ",fret);
2880: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
2881: printf("\n");
2882: free_vector(xt,1,n);
2883: return fret;
2884: }
2885: #endif
1.126 brouard 2886:
2887: /*************** log-likelihood *************/
2888: double func( double *x)
2889: {
1.224 brouard 2890: int i, ii, j, k, mi, d, kk;
2891: int ioffset=0;
2892: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
2893: double **out;
2894: double sw; /* Sum of weights */
2895: double lli; /* Individual log likelihood */
2896: int s1, s2;
1.225 ! brouard 2897: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quatitative time varying covariate */
1.224 brouard 2898: double bbh, survp;
2899: long ipmx;
2900: double agexact;
2901: /*extern weight */
2902: /* We are differentiating ll according to initial status */
2903: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
2904: /*for(i=1;i<imx;i++)
2905: printf(" %d\n",s[4][i]);
2906: */
1.162 brouard 2907:
1.224 brouard 2908: ++countcallfunc;
1.162 brouard 2909:
1.224 brouard 2910: cov[1]=1.;
1.126 brouard 2911:
1.224 brouard 2912: for(k=1; k<=nlstate; k++) ll[k]=0.;
2913: ioffset=0;
2914: if(mle==1){
2915: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
2916: /* Computes the values of the ncovmodel covariates of the model
2917: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
2918: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
2919: to be observed in j being in i according to the model.
2920: */
2921: ioffset=2+nagesqr+cptcovage;
2922: /* for (k=1; k<=cptcovn;k++){ /\* Simple and product covariates without age* products *\/ */
2923: for (k=1; k<=ncoveff;k++){ /* Simple and product covariates without age* products */
2924: cov[++ioffset]=covar[Tvar[k]][i];
2925: }
1.225 ! brouard 2926: for(iqv=1; iqv <= nqfveff; iqv++){ /* Quantitatives and Fixed covariates */
1.224 brouard 2927: cov[++ioffset]=coqvar[iqv][i];
2928: }
1.126 brouard 2929:
1.224 brouard 2930: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
2931: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
2932: has been calculated etc */
2933: /* For an individual i, wav[i] gives the number of effective waves */
2934: /* We compute the contribution to Likelihood of each effective transition
2935: mw[mi][i] is real wave of the mi th effectve wave */
2936: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
2937: s2=s[mw[mi+1][i]][i];
2938: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
2939: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
2940: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
2941: */
2942: for(mi=1; mi<= wav[i]-1; mi++){
2943: for(itv=1; itv <= ntveff; itv++){ /* Varying dummy covariates */
2944: cov[ioffset+itv]=cotvar[mw[mi][i]][itv][i];
2945: }
2946: for(iqtv=1; iqtv <= nqtveff; iqtv++){ /* Varying quantitatives covariates */
1.225 ! brouard 2947: if(cotqvar[mw[mi][i]][iqtv][i] == -1){
! 2948: printf("i=%d, mi=%d, iqtv=%d, cotqvar[mw[mi][i]][iqtv][i]=%f",i,mi,iqtv,cotqvar[mw[mi][i]][iqtv][i]);
! 2949: }
1.224 brouard 2950: cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][iqtv][i];
2951: }
2952: /* ioffset=2+nagesqr+cptcovn+nqv+ntv+nqtv; */
2953: for (ii=1;ii<=nlstate+ndeath;ii++)
2954: for (j=1;j<=nlstate+ndeath;j++){
2955: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2956: savm[ii][j]=(ii==j ? 1.0 : 0.0);
2957: }
2958: for(d=0; d<dh[mi][i]; d++){
2959: newm=savm;
2960: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
2961: cov[2]=agexact;
2962: if(nagesqr==1)
2963: cov[3]= agexact*agexact; /* Should be changed here */
2964: for (kk=1; kk<=cptcovage;kk++) {
2965: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
2966: }
2967: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
2968: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
2969: savm=oldm;
2970: oldm=newm;
2971: } /* end mult */
2972:
2973: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
2974: /* But now since version 0.9 we anticipate for bias at large stepm.
2975: * If stepm is larger than one month (smallest stepm) and if the exact delay
2976: * (in months) between two waves is not a multiple of stepm, we rounded to
2977: * the nearest (and in case of equal distance, to the lowest) interval but now
2978: * we keep into memory the bias bh[mi][i] and also the previous matrix product
2979: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
2980: * probability in order to take into account the bias as a fraction of the way
2981: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
2982: * -stepm/2 to stepm/2 .
2983: * For stepm=1 the results are the same as for previous versions of Imach.
2984: * For stepm > 1 the results are less biased than in previous versions.
2985: */
2986: s1=s[mw[mi][i]][i];
2987: s2=s[mw[mi+1][i]][i];
2988: bbh=(double)bh[mi][i]/(double)stepm;
2989: /* bias bh is positive if real duration
2990: * is higher than the multiple of stepm and negative otherwise.
2991: */
2992: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
2993: if( s2 > nlstate){
2994: /* i.e. if s2 is a death state and if the date of death is known
2995: then the contribution to the likelihood is the probability to
2996: die between last step unit time and current step unit time,
2997: which is also equal to probability to die before dh
2998: minus probability to die before dh-stepm .
2999: In version up to 0.92 likelihood was computed
3000: as if date of death was unknown. Death was treated as any other
3001: health state: the date of the interview describes the actual state
3002: and not the date of a change in health state. The former idea was
3003: to consider that at each interview the state was recorded
3004: (healthy, disable or death) and IMaCh was corrected; but when we
3005: introduced the exact date of death then we should have modified
3006: the contribution of an exact death to the likelihood. This new
3007: contribution is smaller and very dependent of the step unit
3008: stepm. It is no more the probability to die between last interview
3009: and month of death but the probability to survive from last
3010: interview up to one month before death multiplied by the
3011: probability to die within a month. Thanks to Chris
3012: Jackson for correcting this bug. Former versions increased
3013: mortality artificially. The bad side is that we add another loop
3014: which slows down the processing. The difference can be up to 10%
3015: lower mortality.
3016: */
3017: /* If, at the beginning of the maximization mostly, the
3018: cumulative probability or probability to be dead is
3019: constant (ie = 1) over time d, the difference is equal to
3020: 0. out[s1][3] = savm[s1][3]: probability, being at state
3021: s1 at precedent wave, to be dead a month before current
3022: wave is equal to probability, being at state s1 at
3023: precedent wave, to be dead at mont of the current
3024: wave. Then the observed probability (that this person died)
3025: is null according to current estimated parameter. In fact,
3026: it should be very low but not zero otherwise the log go to
3027: infinity.
3028: */
1.183 brouard 3029: /* #ifdef INFINITYORIGINAL */
3030: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3031: /* #else */
3032: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3033: /* lli=log(mytinydouble); */
3034: /* else */
3035: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3036: /* #endif */
1.224 brouard 3037: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3038:
1.224 brouard 3039: } else if ( s2==-1 ) { /* alive */
3040: for (j=1,survp=0. ; j<=nlstate; j++)
3041: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3042: /*survp += out[s1][j]; */
3043: lli= log(survp);
3044: }
3045: else if (s2==-4) {
3046: for (j=3,survp=0. ; j<=nlstate; j++)
3047: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3048: lli= log(survp);
3049: }
3050: else if (s2==-5) {
3051: for (j=1,survp=0. ; j<=2; j++)
3052: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3053: lli= log(survp);
3054: }
3055: else{
3056: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3057: /* 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 */
3058: }
3059: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
3060: /*if(lli ==000.0)*/
3061: /*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); */
3062: ipmx +=1;
3063: sw += weight[i];
3064: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3065: /* if (lli < log(mytinydouble)){ */
3066: /* 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); */
3067: /* 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]); */
3068: /* } */
3069: } /* end of wave */
3070: } /* end of individual */
3071: } else if(mle==2){
3072: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3073: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3074: for(mi=1; mi<= wav[i]-1; mi++){
3075: for (ii=1;ii<=nlstate+ndeath;ii++)
3076: for (j=1;j<=nlstate+ndeath;j++){
3077: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3078: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3079: }
3080: for(d=0; d<=dh[mi][i]; d++){
3081: newm=savm;
3082: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3083: cov[2]=agexact;
3084: if(nagesqr==1)
3085: cov[3]= agexact*agexact;
3086: for (kk=1; kk<=cptcovage;kk++) {
3087: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3088: }
3089: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3090: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3091: savm=oldm;
3092: oldm=newm;
3093: } /* end mult */
3094:
3095: s1=s[mw[mi][i]][i];
3096: s2=s[mw[mi+1][i]][i];
3097: bbh=(double)bh[mi][i]/(double)stepm;
3098: 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 */
3099: ipmx +=1;
3100: sw += weight[i];
3101: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3102: } /* end of wave */
3103: } /* end of individual */
3104: } else if(mle==3){ /* exponential inter-extrapolation */
3105: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3106: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3107: for(mi=1; mi<= wav[i]-1; mi++){
3108: for (ii=1;ii<=nlstate+ndeath;ii++)
3109: for (j=1;j<=nlstate+ndeath;j++){
3110: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3111: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3112: }
3113: for(d=0; d<dh[mi][i]; d++){
3114: newm=savm;
3115: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3116: cov[2]=agexact;
3117: if(nagesqr==1)
3118: cov[3]= agexact*agexact;
3119: for (kk=1; kk<=cptcovage;kk++) {
3120: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3121: }
3122: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3123: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3124: savm=oldm;
3125: oldm=newm;
3126: } /* end mult */
3127:
3128: s1=s[mw[mi][i]][i];
3129: s2=s[mw[mi+1][i]][i];
3130: bbh=(double)bh[mi][i]/(double)stepm;
3131: 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 */
3132: ipmx +=1;
3133: sw += weight[i];
3134: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3135: } /* end of wave */
3136: } /* end of individual */
3137: }else if (mle==4){ /* ml=4 no inter-extrapolation */
3138: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3139: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3140: for(mi=1; mi<= wav[i]-1; mi++){
3141: for (ii=1;ii<=nlstate+ndeath;ii++)
3142: for (j=1;j<=nlstate+ndeath;j++){
3143: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3144: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3145: }
3146: for(d=0; d<dh[mi][i]; d++){
3147: newm=savm;
3148: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3149: cov[2]=agexact;
3150: if(nagesqr==1)
3151: cov[3]= agexact*agexact;
3152: for (kk=1; kk<=cptcovage;kk++) {
3153: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3154: }
1.126 brouard 3155:
1.224 brouard 3156: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3157: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3158: savm=oldm;
3159: oldm=newm;
3160: } /* end mult */
3161:
3162: s1=s[mw[mi][i]][i];
3163: s2=s[mw[mi+1][i]][i];
3164: if( s2 > nlstate){
3165: lli=log(out[s1][s2] - savm[s1][s2]);
3166: } else if ( s2==-1 ) { /* alive */
3167: for (j=1,survp=0. ; j<=nlstate; j++)
3168: survp += out[s1][j];
3169: lli= log(survp);
3170: }else{
3171: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3172: }
3173: ipmx +=1;
3174: sw += weight[i];
3175: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3176: /* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
1.224 brouard 3177: } /* end of wave */
3178: } /* end of individual */
3179: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3180: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
3181: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
3182: for(mi=1; mi<= wav[i]-1; mi++){
3183: for (ii=1;ii<=nlstate+ndeath;ii++)
3184: for (j=1;j<=nlstate+ndeath;j++){
3185: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3186: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3187: }
3188: for(d=0; d<dh[mi][i]; d++){
3189: newm=savm;
3190: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3191: cov[2]=agexact;
3192: if(nagesqr==1)
3193: cov[3]= agexact*agexact;
3194: for (kk=1; kk<=cptcovage;kk++) {
3195: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3196: }
1.126 brouard 3197:
1.224 brouard 3198: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3199: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3200: savm=oldm;
3201: oldm=newm;
3202: } /* end mult */
3203:
3204: s1=s[mw[mi][i]][i];
3205: s2=s[mw[mi+1][i]][i];
3206: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3207: ipmx +=1;
3208: sw += weight[i];
3209: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3210: /*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]);*/
3211: } /* end of wave */
3212: } /* end of individual */
3213: } /* End of if */
3214: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3215: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3216: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3217: return -l;
1.126 brouard 3218: }
3219:
3220: /*************** log-likelihood *************/
3221: double funcone( double *x)
3222: {
3223: /* Same as likeli but slower because of a lot of printf and if */
3224: int i, ii, j, k, mi, d, kk;
1.224 brouard 3225: int ioffset=0;
1.131 brouard 3226: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3227: double **out;
3228: double lli; /* Individual log likelihood */
3229: double llt;
3230: int s1, s2;
1.224 brouard 3231: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate */
1.126 brouard 3232: double bbh, survp;
1.187 brouard 3233: double agexact;
1.214 brouard 3234: double agebegin, ageend;
1.126 brouard 3235: /*extern weight */
3236: /* We are differentiating ll according to initial status */
3237: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3238: /*for(i=1;i<imx;i++)
3239: printf(" %d\n",s[4][i]);
3240: */
3241: cov[1]=1.;
3242:
3243: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3244: ioffset=0;
3245: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.225 ! brouard 3246: ioffset=2+nagesqr+cptcovage;
1.224 brouard 3247: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.225 ! brouard 3248: for (k=1; k<=ncoveff;k++){ /* Simple and product covariates without age* products */
! 3249: cov[++ioffset]=covar[Tvar[k]][i];
! 3250: }
! 3251: for(iqv=1; iqv <= nqfveff; iqv++){ /* Quantitatives Fixed covariates */
! 3252: cov[++ioffset]=coqvar[iqv][i];
! 3253: }
! 3254:
1.126 brouard 3255: for(mi=1; mi<= wav[i]-1; mi++){
1.225 ! brouard 3256: for(itv=1; itv <= ntveff; itv++){ /* Varying dummy covariates */
! 3257: cov[ioffset+itv]=cotvar[mw[mi][i]][itv][i];
! 3258: }
! 3259: for(iqtv=1; iqtv <= nqtveff; iqtv++){ /* Varying quantitatives covariates */
! 3260: cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][iqtv][i];
! 3261: }
1.126 brouard 3262: for (ii=1;ii<=nlstate+ndeath;ii++)
1.225 ! brouard 3263: for (j=1;j<=nlstate+ndeath;j++){
! 3264: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3265: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3266: }
1.214 brouard 3267:
3268: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3269: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3270: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.225 ! brouard 3271: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
! 3272: and mw[mi+1][i]. dh depends on stepm.*/
! 3273: newm=savm;
! 3274: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3275: cov[2]=agexact;
! 3276: if(nagesqr==1)
! 3277: cov[3]= agexact*agexact;
! 3278: for (kk=1; kk<=cptcovage;kk++) {
! 3279: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
! 3280: }
! 3281: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
! 3282: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
! 3283: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3284: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3285: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
! 3286: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
! 3287: savm=oldm;
! 3288: oldm=newm;
1.126 brouard 3289: } /* end mult */
3290:
3291: s1=s[mw[mi][i]][i];
3292: s2=s[mw[mi+1][i]][i];
1.217 brouard 3293: /* if(s2==-1){ */
3294: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3295: /* /\* exit(1); *\/ */
3296: /* } */
1.126 brouard 3297: bbh=(double)bh[mi][i]/(double)stepm;
3298: /* bias is positive if real duration
3299: * is higher than the multiple of stepm and negative otherwise.
3300: */
3301: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.225 ! brouard 3302: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3303: } else if ( s2==-1 ) { /* alive */
1.225 ! brouard 3304: for (j=1,survp=0. ; j<=nlstate; j++)
! 3305: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 3306: lli= log(survp);
1.126 brouard 3307: }else if (mle==1){
1.225 ! brouard 3308: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3309: } else if(mle==2){
1.225 ! brouard 3310: lli= (savm[s1][s2]>(double)1.e-8 ?log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* linear interpolation */
1.126 brouard 3311: } else if(mle==3){ /* exponential inter-extrapolation */
1.225 ! brouard 3312: lli= (savm[s1][s2]>(double)1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2])); /* exponential inter-extrapolation */
1.126 brouard 3313: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.225 ! brouard 3314: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3315: } else{ /* mle=0 back to 1 */
1.225 ! brouard 3316: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
! 3317: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3318: } /* End of if */
3319: ipmx +=1;
3320: sw += weight[i];
3321: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3322: /*printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
1.126 brouard 3323: if(globpr){
1.225 ! brouard 3324: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3325: %11.6f %11.6f %11.6f ", \
1.225 ! brouard 3326: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
! 3327: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
! 3328: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
! 3329: llt +=ll[k]*gipmx/gsw;
! 3330: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
! 3331: }
! 3332: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3333: }
3334: } /* end of wave */
3335: } /* end of individual */
3336: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3337: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3338: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3339: if(globpr==0){ /* First time we count the contributions and weights */
3340: gipmx=ipmx;
3341: gsw=sw;
3342: }
3343: return -l;
3344: }
3345:
3346:
3347: /*************** function likelione ***********/
3348: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3349: {
3350: /* This routine should help understanding what is done with
3351: the selection of individuals/waves and
3352: to check the exact contribution to the likelihood.
3353: Plotting could be done.
3354: */
3355: int k;
3356:
3357: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3358: strcpy(fileresilk,"ILK_");
1.202 brouard 3359: strcat(fileresilk,fileresu);
1.126 brouard 3360: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3361: printf("Problem with resultfile: %s\n", fileresilk);
3362: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3363: }
1.214 brouard 3364: 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");
3365: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3366: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3367: for(k=1; k<=nlstate; k++)
3368: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3369: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3370: }
3371:
3372: *fretone=(*funcone)(p);
3373: if(*globpri !=0){
3374: fclose(ficresilk);
1.205 brouard 3375: if (mle ==0)
3376: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3377: else if(mle >=1)
3378: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3379: fprintf(fichtm," You should at least run with mle >= 1 to get starting values corresponding to the optimized parameters in order to visualize the real contribution of each individual/wave: <a href=\"%s\">%s</a><br>\n",subdirf(fileresilk),subdirf(fileresilk));
1.207 brouard 3380:
1.208 brouard 3381:
3382: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3383: fprintf(fichtm,"<br>- Probability p<sub>%dj</sub> by origin %d and destination j. Dot's sizes are related to corresponding weight: <a href=\"%s-p%dj.png\">%s-p%dj.png</a><br> \
1.208 brouard 3384: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3385: }
1.207 brouard 3386: fprintf(fichtm,"<br>- The function drawn is -2Log(L) in Log scale: by state of origin <a href=\"%s-ori.png\">%s-ori.png</a><br> \
1.204 brouard 3387: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3388: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3389: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3390: fflush(fichtm);
1.205 brouard 3391: }
1.126 brouard 3392: return;
3393: }
3394:
3395:
3396: /*********** Maximum Likelihood Estimation ***************/
3397:
3398: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3399: {
1.165 brouard 3400: int i,j, iter=0;
1.126 brouard 3401: double **xi;
3402: double fret;
3403: double fretone; /* Only one call to likelihood */
3404: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3405:
3406: #ifdef NLOPT
3407: int creturn;
3408: nlopt_opt opt;
3409: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3410: double *lb;
3411: double minf; /* the minimum objective value, upon return */
3412: double * p1; /* Shifted parameters from 0 instead of 1 */
3413: myfunc_data dinst, *d = &dinst;
3414: #endif
3415:
3416:
1.126 brouard 3417: xi=matrix(1,npar,1,npar);
3418: for (i=1;i<=npar;i++)
3419: for (j=1;j<=npar;j++)
3420: xi[i][j]=(i==j ? 1.0 : 0.0);
3421: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3422: strcpy(filerespow,"POW_");
1.126 brouard 3423: strcat(filerespow,fileres);
3424: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3425: printf("Problem with resultfile: %s\n", filerespow);
3426: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3427: }
3428: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3429: for (i=1;i<=nlstate;i++)
3430: for(j=1;j<=nlstate+ndeath;j++)
3431: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3432: fprintf(ficrespow,"\n");
1.162 brouard 3433: #ifdef POWELL
1.126 brouard 3434: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3435: #endif
1.126 brouard 3436:
1.162 brouard 3437: #ifdef NLOPT
3438: #ifdef NEWUOA
3439: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3440: #else
3441: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3442: #endif
3443: lb=vector(0,npar-1);
3444: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3445: nlopt_set_lower_bounds(opt, lb);
3446: nlopt_set_initial_step1(opt, 0.1);
3447:
3448: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3449: d->function = func;
3450: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3451: nlopt_set_min_objective(opt, myfunc, d);
3452: nlopt_set_xtol_rel(opt, ftol);
3453: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3454: printf("nlopt failed! %d\n",creturn);
3455: }
3456: else {
3457: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3458: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3459: iter=1; /* not equal */
3460: }
3461: nlopt_destroy(opt);
3462: #endif
1.126 brouard 3463: free_matrix(xi,1,npar,1,npar);
3464: fclose(ficrespow);
1.203 brouard 3465: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3466: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3467: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3468:
3469: }
3470:
3471: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3472: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3473: {
3474: double **a,**y,*x,pd;
1.203 brouard 3475: /* double **hess; */
1.164 brouard 3476: int i, j;
1.126 brouard 3477: int *indx;
3478:
3479: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3480: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3481: void lubksb(double **a, int npar, int *indx, double b[]) ;
3482: void ludcmp(double **a, int npar, int *indx, double *d) ;
3483: double gompertz(double p[]);
1.203 brouard 3484: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3485:
3486: printf("\nCalculation of the hessian matrix. Wait...\n");
3487: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3488: for (i=1;i<=npar;i++){
1.203 brouard 3489: printf("%d-",i);fflush(stdout);
3490: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3491:
3492: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3493:
3494: /* printf(" %f ",p[i]);
3495: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3496: }
3497:
3498: for (i=1;i<=npar;i++) {
3499: for (j=1;j<=npar;j++) {
3500: if (j>i) {
1.203 brouard 3501: printf(".%d-%d",i,j);fflush(stdout);
3502: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3503: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3504:
3505: hess[j][i]=hess[i][j];
3506: /*printf(" %lf ",hess[i][j]);*/
3507: }
3508: }
3509: }
3510: printf("\n");
3511: fprintf(ficlog,"\n");
3512:
3513: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3514: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3515:
3516: a=matrix(1,npar,1,npar);
3517: y=matrix(1,npar,1,npar);
3518: x=vector(1,npar);
3519: indx=ivector(1,npar);
3520: for (i=1;i<=npar;i++)
3521: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3522: ludcmp(a,npar,indx,&pd);
3523:
3524: for (j=1;j<=npar;j++) {
3525: for (i=1;i<=npar;i++) x[i]=0;
3526: x[j]=1;
3527: lubksb(a,npar,indx,x);
3528: for (i=1;i<=npar;i++){
3529: matcov[i][j]=x[i];
3530: }
3531: }
3532:
3533: printf("\n#Hessian matrix#\n");
3534: fprintf(ficlog,"\n#Hessian matrix#\n");
3535: for (i=1;i<=npar;i++) {
3536: for (j=1;j<=npar;j++) {
1.203 brouard 3537: printf("%.6e ",hess[i][j]);
3538: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3539: }
3540: printf("\n");
3541: fprintf(ficlog,"\n");
3542: }
3543:
1.203 brouard 3544: /* printf("\n#Covariance matrix#\n"); */
3545: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3546: /* for (i=1;i<=npar;i++) { */
3547: /* for (j=1;j<=npar;j++) { */
3548: /* printf("%.6e ",matcov[i][j]); */
3549: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3550: /* } */
3551: /* printf("\n"); */
3552: /* fprintf(ficlog,"\n"); */
3553: /* } */
3554:
1.126 brouard 3555: /* Recompute Inverse */
1.203 brouard 3556: /* for (i=1;i<=npar;i++) */
3557: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3558: /* ludcmp(a,npar,indx,&pd); */
3559:
3560: /* printf("\n#Hessian matrix recomputed#\n"); */
3561:
3562: /* for (j=1;j<=npar;j++) { */
3563: /* for (i=1;i<=npar;i++) x[i]=0; */
3564: /* x[j]=1; */
3565: /* lubksb(a,npar,indx,x); */
3566: /* for (i=1;i<=npar;i++){ */
3567: /* y[i][j]=x[i]; */
3568: /* printf("%.3e ",y[i][j]); */
3569: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3570: /* } */
3571: /* printf("\n"); */
3572: /* fprintf(ficlog,"\n"); */
3573: /* } */
3574:
3575: /* Verifying the inverse matrix */
3576: #ifdef DEBUGHESS
3577: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3578:
1.203 brouard 3579: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3580: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3581:
3582: for (j=1;j<=npar;j++) {
3583: for (i=1;i<=npar;i++){
1.203 brouard 3584: printf("%.2f ",y[i][j]);
3585: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3586: }
3587: printf("\n");
3588: fprintf(ficlog,"\n");
3589: }
1.203 brouard 3590: #endif
1.126 brouard 3591:
3592: free_matrix(a,1,npar,1,npar);
3593: free_matrix(y,1,npar,1,npar);
3594: free_vector(x,1,npar);
3595: free_ivector(indx,1,npar);
1.203 brouard 3596: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3597:
3598:
3599: }
3600:
3601: /*************** hessian matrix ****************/
3602: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3603: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3604: int i;
3605: int l=1, lmax=20;
1.203 brouard 3606: double k1,k2, res, fx;
1.132 brouard 3607: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3608: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3609: int k=0,kmax=10;
3610: double l1;
3611:
3612: fx=func(x);
3613: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3614: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3615: l1=pow(10,l);
3616: delts=delt;
3617: for(k=1 ; k <kmax; k=k+1){
3618: delt = delta*(l1*k);
3619: p2[theta]=x[theta] +delt;
1.145 brouard 3620: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3621: p2[theta]=x[theta]-delt;
3622: k2=func(p2)-fx;
3623: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3624: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3625:
1.203 brouard 3626: #ifdef DEBUGHESSII
1.126 brouard 3627: 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);
3628: 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);
3629: #endif
3630: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3631: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3632: k=kmax;
3633: }
3634: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3635: k=kmax; l=lmax*10;
1.126 brouard 3636: }
3637: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3638: delts=delt;
3639: }
1.203 brouard 3640: } /* End loop k */
1.126 brouard 3641: }
3642: delti[theta]=delts;
3643: return res;
3644:
3645: }
3646:
1.203 brouard 3647: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3648: {
3649: int i;
1.164 brouard 3650: int l=1, lmax=20;
1.126 brouard 3651: double k1,k2,k3,k4,res,fx;
1.132 brouard 3652: double p2[MAXPARM+1];
1.203 brouard 3653: int k, kmax=1;
3654: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3655:
3656: int firstime=0;
1.203 brouard 3657:
1.126 brouard 3658: fx=func(x);
1.203 brouard 3659: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3660: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3661: p2[thetai]=x[thetai]+delti[thetai]*k;
3662: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3663: k1=func(p2)-fx;
3664:
1.203 brouard 3665: p2[thetai]=x[thetai]+delti[thetai]*k;
3666: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3667: k2=func(p2)-fx;
3668:
1.203 brouard 3669: p2[thetai]=x[thetai]-delti[thetai]*k;
3670: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3671: k3=func(p2)-fx;
3672:
1.203 brouard 3673: p2[thetai]=x[thetai]-delti[thetai]*k;
3674: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3675: k4=func(p2)-fx;
1.203 brouard 3676: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3677: if(k1*k2*k3*k4 <0.){
1.208 brouard 3678: firstime=1;
1.203 brouard 3679: kmax=kmax+10;
1.208 brouard 3680: }
3681: if(kmax >=10 || firstime ==1){
1.218 brouard 3682: printf("Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you may increase ftol=%.2e\n",thetai,thetaj, ftol);
3683: fprintf(ficlog,"Warning: directions %d-%d, you are not estimating the Hessian at the exact maximum likelihood; you may increase ftol=%.2e\n",thetai,thetaj, ftol);
1.203 brouard 3684: 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);
3685: 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);
3686: }
3687: #ifdef DEBUGHESSIJ
3688: v1=hess[thetai][thetai];
3689: v2=hess[thetaj][thetaj];
3690: cv12=res;
3691: /* Computing eigen value of Hessian matrix */
3692: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3693: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3694: if ((lc2 <0) || (lc1 <0) ){
3695: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3696: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3697: 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);
3698: 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);
3699: }
1.126 brouard 3700: #endif
3701: }
3702: return res;
3703: }
3704:
1.203 brouard 3705: /* Not done yet: Was supposed to fix if not exactly at the maximum */
3706: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
3707: /* { */
3708: /* int i; */
3709: /* int l=1, lmax=20; */
3710: /* double k1,k2,k3,k4,res,fx; */
3711: /* double p2[MAXPARM+1]; */
3712: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
3713: /* int k=0,kmax=10; */
3714: /* double l1; */
3715:
3716: /* fx=func(x); */
3717: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
3718: /* l1=pow(10,l); */
3719: /* delts=delt; */
3720: /* for(k=1 ; k <kmax; k=k+1){ */
3721: /* delt = delti*(l1*k); */
3722: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
3723: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3724: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3725: /* k1=func(p2)-fx; */
3726:
3727: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3728: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3729: /* k2=func(p2)-fx; */
3730:
3731: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3732: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3733: /* k3=func(p2)-fx; */
3734:
3735: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3736: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3737: /* k4=func(p2)-fx; */
3738: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
3739: /* #ifdef DEBUGHESSIJ */
3740: /* 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); */
3741: /* 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); */
3742: /* #endif */
3743: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
3744: /* k=kmax; */
3745: /* } */
3746: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
3747: /* k=kmax; l=lmax*10; */
3748: /* } */
3749: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
3750: /* delts=delt; */
3751: /* } */
3752: /* } /\* End loop k *\/ */
3753: /* } */
3754: /* delti[theta]=delts; */
3755: /* return res; */
3756: /* } */
3757:
3758:
1.126 brouard 3759: /************** Inverse of matrix **************/
3760: void ludcmp(double **a, int n, int *indx, double *d)
3761: {
3762: int i,imax,j,k;
3763: double big,dum,sum,temp;
3764: double *vv;
3765:
3766: vv=vector(1,n);
3767: *d=1.0;
3768: for (i=1;i<=n;i++) {
3769: big=0.0;
3770: for (j=1;j<=n;j++)
3771: if ((temp=fabs(a[i][j])) > big) big=temp;
3772: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
3773: vv[i]=1.0/big;
3774: }
3775: for (j=1;j<=n;j++) {
3776: for (i=1;i<j;i++) {
3777: sum=a[i][j];
3778: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
3779: a[i][j]=sum;
3780: }
3781: big=0.0;
3782: for (i=j;i<=n;i++) {
3783: sum=a[i][j];
3784: for (k=1;k<j;k++)
3785: sum -= a[i][k]*a[k][j];
3786: a[i][j]=sum;
3787: if ( (dum=vv[i]*fabs(sum)) >= big) {
3788: big=dum;
3789: imax=i;
3790: }
3791: }
3792: if (j != imax) {
3793: for (k=1;k<=n;k++) {
3794: dum=a[imax][k];
3795: a[imax][k]=a[j][k];
3796: a[j][k]=dum;
3797: }
3798: *d = -(*d);
3799: vv[imax]=vv[j];
3800: }
3801: indx[j]=imax;
3802: if (a[j][j] == 0.0) a[j][j]=TINY;
3803: if (j != n) {
3804: dum=1.0/(a[j][j]);
3805: for (i=j+1;i<=n;i++) a[i][j] *= dum;
3806: }
3807: }
3808: free_vector(vv,1,n); /* Doesn't work */
3809: ;
3810: }
3811:
3812: void lubksb(double **a, int n, int *indx, double b[])
3813: {
3814: int i,ii=0,ip,j;
3815: double sum;
3816:
3817: for (i=1;i<=n;i++) {
3818: ip=indx[i];
3819: sum=b[ip];
3820: b[ip]=b[i];
3821: if (ii)
3822: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
3823: else if (sum) ii=i;
3824: b[i]=sum;
3825: }
3826: for (i=n;i>=1;i--) {
3827: sum=b[i];
3828: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
3829: b[i]=sum/a[i][i];
3830: }
3831: }
3832:
3833: void pstamp(FILE *fichier)
3834: {
1.196 brouard 3835: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 3836: }
3837:
3838: /************ Frequencies ********************/
1.220 brouard 3839: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
3840: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
3841: int firstpass, int lastpass, int stepm, int weightopt, char model[])
3842: { /* Some frequencies */
3843:
3844: int i, m, jk, j1, bool, z1,j;
3845: int iind=0, iage=0;
3846: int mi; /* Effective wave */
3847: int first;
3848: double ***freq; /* Frequencies */
1.224 brouard 3849: double *meanq;
3850: double **meanqt;
1.220 brouard 3851: double *pp, **prop, *posprop, *pospropt;
3852: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
3853: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
3854: double agebegin, ageend;
3855:
3856: pp=vector(1,nlstate);
3857: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
3858: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
3859: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
3860: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
1.225 ! brouard 3861: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
1.224 brouard 3862: meanqt=matrix(1,lastpass,1,nqtveff);
1.220 brouard 3863: strcpy(fileresp,"P_");
3864: strcat(fileresp,fileresu);
3865: /*strcat(fileresphtm,fileresu);*/
3866: if((ficresp=fopen(fileresp,"w"))==NULL) {
3867: printf("Problem with prevalence resultfile: %s\n", fileresp);
3868: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
3869: exit(0);
3870: }
1.214 brouard 3871:
1.220 brouard 3872: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
3873: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
3874: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
3875: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
3876: fflush(ficlog);
3877: exit(70);
3878: }
3879: else{
3880: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 3881: <hr size=\"2\" color=\"#EC5E5E\"> \n\
3882: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.220 brouard 3883: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
3884: }
3885: fprintf(ficresphtm,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies and prevalence by age at begin of transition</h4>\n",fileresphtm, fileresphtm);
1.214 brouard 3886:
1.220 brouard 3887: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
3888: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
3889: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
3890: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
3891: fflush(ficlog);
3892: exit(70);
3893: }
3894: else{
3895: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 3896: <hr size=\"2\" color=\"#EC5E5E\"> \n\
3897: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.220 brouard 3898: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
3899: }
3900: fprintf(ficresphtmfr,"Current page is file <a href=\"%s\">%s</a><br>\n\n<h4>Frequencies of all effective transitions by age at begin of transition </h4>Unknown status is -1<br/>\n",fileresphtmfr, fileresphtmfr);
1.214 brouard 3901:
1.220 brouard 3902: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
3903: j1=0;
1.126 brouard 3904:
1.224 brouard 3905: j=ncoveff;
1.220 brouard 3906: if (cptcovn<1) {j=1;ncodemax[1]=1;}
3907:
3908: first=1;
3909:
3910: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
3911: reference=low_education V1=0,V2=0
3912: med_educ V1=1 V2=0,
3913: high_educ V1=0 V2=1
3914: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
3915: */
1.126 brouard 3916:
1.224 brouard 3917: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on covariates combination excluding varying and quantitatives */
1.220 brouard 3918: posproptt=0.;
3919: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
3920: scanf("%d", i);*/
3921: for (i=-5; i<=nlstate+ndeath; i++)
3922: for (jk=-5; jk<=nlstate+ndeath; jk++)
3923: for(m=iagemin; m <= iagemax+3; m++)
3924: freq[i][jk][m]=0;
3925:
3926: for (i=1; i<=nlstate; i++) {
3927: for(m=iagemin; m <= iagemax+3; m++)
3928: prop[i][m]=0;
3929: posprop[i]=0;
3930: pospropt[i]=0;
3931: }
1.225 ! brouard 3932: for (z1=1; z1<= nqfveff; z1++) {
1.224 brouard 3933: meanq[z1]+=0.;
3934: for(m=1;m<=lastpass;m++){
3935: meanqt[m][z1]=0.;
3936: }
3937: }
1.220 brouard 3938:
3939: dateintsum=0;
3940: k2cpt=0;
1.224 brouard 3941: /* For that comination of covariate j1, we count and print the frequencies */
1.220 brouard 3942: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
3943: bool=1;
1.225 ! brouard 3944: if (nqfveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
! 3945: for (z1=1; z1<= nqfveff; z1++) {
1.224 brouard 3946: meanq[z1]+=coqvar[Tvar[z1]][iind];
3947: }
3948: for (z1=1; z1<=ncoveff; z1++) {
3949: /* if(Tvaraff[z1] ==-20){ */
3950: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
3951: /* }else if(Tvaraff[z1] ==-10){ */
3952: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
3953: /* }else */
1.220 brouard 3954: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
1.224 brouard 3955: /* Tests if this individual i responded to j1 (V4=1 V3=0) */
1.220 brouard 3956: bool=0;
3957: /* 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",
1.198 brouard 3958: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
3959: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
1.220 brouard 3960: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
3961: }
3962: } /* end z1 */
3963: } /* cptcovn > 0 */
3964:
1.224 brouard 3965: if (bool==1){ /* We selected an individual iin satisfying combination j1 */
1.220 brouard 3966: /* for(m=firstpass; m<=lastpass; m++){ */
3967: for(mi=1; mi<wav[iind];mi++){
3968: m=mw[mi][iind];
3969: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
3970: and mw[mi+1][iind]. dh depends on stepm. */
3971: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
3972: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
3973: if(m >=firstpass && m <=lastpass){
3974: k2=anint[m][iind]+(mint[m][iind]/12.);
3975: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
3976: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
3977: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
3978: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
3979: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
3980: if (m<lastpass) {
3981: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
3982: /* 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]); */
3983: if(s[m][iind]==-1)
3984: 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.));
3985: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
3986: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
3987: 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 */
3988: }
3989: }
3990: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99)) {
3991: dateintsum=dateintsum+k2;
3992: k2cpt++;
3993: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
3994: }
3995: /*}*/
3996: } /* end m */
3997: } /* end bool */
3998: } /* end iind = 1 to imx */
3999: /* prop[s][age] is feeded for any initial and valid live state as well as
4000: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
4001:
4002:
4003: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
4004: pstamp(ficresp);
1.224 brouard 4005: if (ncoveff>0) {
1.220 brouard 4006: fprintf(ficresp, "\n#********** Variable ");
4007: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
4008: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
1.224 brouard 4009: for (z1=1; z1<=ncoveff; z1++){
1.220 brouard 4010: fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4011: fprintf(ficresphtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4012: fprintf(ficresphtmfr, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
4013: }
4014: fprintf(ficresp, "**********\n#");
4015: fprintf(ficresphtm, "**********</h3>\n");
4016: fprintf(ficresphtmfr, "**********</h3>\n");
4017: fprintf(ficlog, "\n#********** Variable ");
1.224 brouard 4018: for (z1=1; z1<=ncoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.220 brouard 4019: fprintf(ficlog, "**********\n");
4020: }
4021: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
4022: for(i=1; i<=nlstate;i++) {
4023: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
4024: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
4025: }
4026: fprintf(ficresp, "\n");
4027: fprintf(ficresphtm, "\n");
4028:
4029: /* Header of frequency table by age */
4030: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
4031: fprintf(ficresphtmfr,"<th>Age</th> ");
4032: for(jk=-1; jk <=nlstate+ndeath; jk++){
4033: for(m=-1; m <=nlstate+ndeath; m++){
4034: if(jk!=0 && m!=0)
4035: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
4036: }
4037: }
4038: fprintf(ficresphtmfr, "\n");
1.214 brouard 4039:
1.220 brouard 4040: /* For each age */
4041: for(iage=iagemin; iage <= iagemax+3; iage++){
4042: fprintf(ficresphtm,"<tr>");
4043: if(iage==iagemax+1){
4044: fprintf(ficlog,"1");
4045: fprintf(ficresphtmfr,"<tr><th>0</th> ");
4046: }else if(iage==iagemax+2){
4047: fprintf(ficlog,"0");
4048: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
4049: }else if(iage==iagemax+3){
4050: fprintf(ficlog,"Total");
4051: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
4052: }else{
4053: if(first==1){
4054: first=0;
4055: printf("See log file for details...\n");
4056: }
4057: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
4058: fprintf(ficlog,"Age %d", iage);
4059: }
4060: for(jk=1; jk <=nlstate ; jk++){
4061: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
4062: pp[jk] += freq[jk][m][iage];
4063: }
4064: for(jk=1; jk <=nlstate ; jk++){
4065: for(m=-1, pos=0; m <=0 ; m++)
4066: pos += freq[jk][m][iage];
4067: if(pp[jk]>=1.e-10){
4068: if(first==1){
4069: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4070: }
4071: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
4072: }else{
4073: if(first==1)
4074: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4075: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
4076: }
4077: }
4078:
4079: for(jk=1; jk <=nlstate ; jk++){
4080: /* posprop[jk]=0; */
4081: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
4082: pp[jk] += freq[jk][m][iage];
4083: } /* pp[jk] is the total number of transitions starting from state jk and any ending status until this age */
4084:
4085: for(jk=1,pos=0, pospropta=0.; jk <=nlstate ; jk++){
4086: pos += pp[jk]; /* pos is the total number of transitions until this age */
4087: posprop[jk] += prop[jk][iage]; /* prop is the number of transitions from a live state
4088: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4089: pospropta += prop[jk][iage]; /* prop is the number of transitions from a live state
4090: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
4091: }
4092: for(jk=1; jk <=nlstate ; jk++){
4093: if(pos>=1.e-5){
4094: if(first==1)
4095: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4096: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
4097: }else{
4098: if(first==1)
4099: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4100: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
4101: }
4102: if( iage <= iagemax){
4103: if(pos>=1.e-5){
4104: fprintf(ficresp," %d %.5f %.0f %.0f",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4105: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
4106: /*probs[iage][jk][j1]= pp[jk]/pos;*/
4107: /*printf("\niage=%d jk=%d j1=%d %.5f %.0f %.0f %f",iage,jk,j1,pp[jk]/pos, pp[jk],pos,probs[iage][jk][j1]);*/
4108: }
4109: else{
4110: fprintf(ficresp," %d NaNq %.0f %.0f",iage,prop[jk][iage],pospropta);
4111: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[jk][iage],pospropta);
4112: }
4113: }
4114: pospropt[jk] +=posprop[jk];
4115: } /* end loop jk */
4116: /* pospropt=0.; */
4117: for(jk=-1; jk <=nlstate+ndeath; jk++){
4118: for(m=-1; m <=nlstate+ndeath; m++){
4119: if(freq[jk][m][iage] !=0 ) { /* minimizing output */
4120: if(first==1){
4121: printf(" %d%d=%.0f",jk,m,freq[jk][m][iage]);
4122: }
4123: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][iage]);
4124: }
4125: if(jk!=0 && m!=0)
4126: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][iage]);
4127: }
4128: } /* end loop jk */
4129: posproptt=0.;
4130: for(jk=1; jk <=nlstate; jk++){
4131: posproptt += pospropt[jk];
4132: }
4133: fprintf(ficresphtmfr,"</tr>\n ");
4134: if(iage <= iagemax){
4135: fprintf(ficresp,"\n");
4136: fprintf(ficresphtm,"</tr>\n");
4137: }
4138: if(first==1)
4139: printf("Others in log...\n");
4140: fprintf(ficlog,"\n");
4141: } /* end loop age iage */
4142: fprintf(ficresphtm,"<tr><th>Tot</th>");
4143: for(jk=1; jk <=nlstate ; jk++){
4144: if(posproptt < 1.e-5){
1.221 brouard 4145: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[jk],posproptt);
4146: }else{
1.220 brouard 4147: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[jk]/posproptt,pospropt[jk],posproptt);
4148: }
4149: }
4150: fprintf(ficresphtm,"</tr>\n");
4151: fprintf(ficresphtm,"</table>\n");
4152: fprintf(ficresphtmfr,"</table>\n");
4153: if(posproptt < 1.e-5){
4154: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4155: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
4156: fprintf(ficres,"\n This combination (%d) is not valid and no result will be produced\n\n",j1);
4157: invalidvarcomb[j1]=1;
4158: }else{
4159: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
4160: invalidvarcomb[j1]=0;
4161: }
4162: fprintf(ficresphtmfr,"</table>\n");
4163: } /* end selected combination of covariate j1 */
4164: dateintmean=dateintsum/k2cpt;
4165:
4166: fclose(ficresp);
4167: fclose(ficresphtm);
4168: fclose(ficresphtmfr);
1.225 ! brouard 4169: free_vector(meanq,1,nqfveff);
1.224 brouard 4170: free_matrix(meanqt,1,lastpass,1,nqtveff);
1.220 brouard 4171: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4172: free_vector(pospropt,1,nlstate);
4173: free_vector(posprop,1,nlstate);
4174: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
4175: free_vector(pp,1,nlstate);
1.224 brouard 4176: /* End of freqsummary */
1.220 brouard 4177: }
1.126 brouard 4178:
4179: /************ Prevalence ********************/
1.222 brouard 4180: 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)
4181: {
4182: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
4183: in each health status at the date of interview (if between dateprev1 and dateprev2).
4184: We still use firstpass and lastpass as another selection.
4185: */
1.126 brouard 4186:
1.222 brouard 4187: int i, m, jk, j1, bool, z1,j;
4188: int mi; /* Effective wave */
4189: int iage;
4190: double agebegin, ageend;
4191:
4192: double **prop;
4193: double posprop;
4194: double y2; /* in fractional years */
4195: int iagemin, iagemax;
4196: int first; /** to stop verbosity which is redirected to log file */
4197:
4198: iagemin= (int) agemin;
4199: iagemax= (int) agemax;
4200: /*pp=vector(1,nlstate);*/
4201: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4202: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
4203: j1=0;
4204:
4205: /*j=cptcoveff;*/
4206: if (cptcovn<1) {j=1;ncodemax[1]=1;}
4207:
4208: first=1;
1.225 ! brouard 4209: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
1.222 brouard 4210: for (i=1; i<=nlstate; i++)
4211: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
4212: prop[i][iage]=0.0;
4213:
4214: for (i=1; i<=imx; i++) { /* Each individual */
4215: bool=1;
4216: if (cptcovn>0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.225 ! brouard 4217: for (z1=1; z1<=cptcoveff; z1++) /* For each covariate, look at the value for individual i and checks if it is equal to the corresponding value of this covariate according to current combination j1*/
1.222 brouard 4218: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)])
4219: bool=0;
4220: }
4221: if (bool==1) { /* For this combination of covariates values, this individual fits */
4222: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
4223: for(mi=1; mi<wav[i];mi++){
4224: m=mw[mi][i];
4225: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
4226: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
4227: if(m >=firstpass && m <=lastpass){
4228: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
4229: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
4230: if(agev[m][i]==0) agev[m][i]=iagemax+1;
4231: if(agev[m][i]==1) agev[m][i]=iagemax+2;
4232: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
4233: 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);
4234: exit(1);
4235: }
4236: if (s[m][i]>0 && s[m][i]<=nlstate) {
4237: /*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]]);*/
4238: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
4239: prop[s[m][i]][iagemax+3] += weight[i];
4240: } /* end valid statuses */
4241: } /* end selection of dates */
4242: } /* end selection of waves */
4243: } /* end effective waves */
4244: } /* end bool */
4245: }
4246: for(i=iagemin; i <= iagemax+3; i++){
4247: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
4248: posprop += prop[jk][i];
4249: }
4250:
4251: for(jk=1; jk <=nlstate ; jk++){
4252: if( i <= iagemax){
4253: if(posprop>=1.e-5){
4254: probs[i][jk][j1]= prop[jk][i]/posprop;
4255: } else{
4256: if(first==1){
4257: first=0;
4258: printf("Warning Observed prevalence probs[%d][%d][%d]=%lf because of lack of cases\nSee others on log file...\n",jk,i,j1,probs[i][jk][j1]);
4259: }
4260: }
4261: }
4262: }/* end jk */
4263: }/* end i */
4264: /*} *//* end i1 */
4265: } /* end j1 */
4266:
4267: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4268: /*free_vector(pp,1,nlstate);*/
4269: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4270: } /* End of prevalence */
1.126 brouard 4271:
4272: /************* Waves Concatenation ***************/
4273:
4274: 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)
4275: {
4276: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4277: Death is a valid wave (if date is known).
4278: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4279: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4280: and mw[mi+1][i]. dh depends on stepm.
4281: */
4282:
1.224 brouard 4283: int i=0, mi=0, m=0, mli=0;
1.126 brouard 4284: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4285: double sum=0., jmean=0.;*/
1.224 brouard 4286: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 4287: int j, k=0,jk, ju, jl;
4288: double sum=0.;
4289: first=0;
1.214 brouard 4290: firstwo=0;
1.217 brouard 4291: firsthree=0;
1.218 brouard 4292: firstfour=0;
1.164 brouard 4293: jmin=100000;
1.126 brouard 4294: jmax=-1;
4295: jmean=0.;
1.224 brouard 4296:
4297: /* Treating live states */
1.214 brouard 4298: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 4299: mi=0; /* First valid wave */
4300: mli=0; /* Last valid wave */
1.126 brouard 4301: m=firstpass;
1.214 brouard 4302: while(s[m][i] <= nlstate){ /* a live state */
1.224 brouard 4303: 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 */
4304: mli=m-1;/* mw[++mi][i]=m-1; */
4305: }else if(s[m][i]>=1 || s[m][i]==-4 || s[m][i]==-5){ /* Since 0.98r4 if status=-2 vital status is really unknown, wave should be skipped */
1.223 brouard 4306: mw[++mi][i]=m;
1.224 brouard 4307: mli=m;
4308: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
4309: if(m < lastpass){ /* m < lastpass, standard case */
4310: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 4311: }
1.224 brouard 4312: else{ /* m >= lastpass, eventual special issue with warning */
4313: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
4314: break;
4315: #else
1.223 brouard 4316: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
4317: if(firsthree == 0){
1.224 brouard 4318: 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 pi. .\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);
1.223 brouard 4319: firsthree=1;
4320: }
1.224 brouard 4321: 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 pi. .\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);
1.223 brouard 4322: mw[++mi][i]=m;
1.224 brouard 4323: mli=m;
1.223 brouard 4324: }
4325: if(s[m][i]==-2){ /* Vital status is really unknown */
4326: nbwarn++;
4327: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4328: 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);
1.224 brouard 4329: 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);
1.223 brouard 4330: }
4331: break;
4332: }
4333: break;
1.224 brouard 4334: #endif
4335: }/* End m >= lastpass */
1.126 brouard 4336: }/* end while */
1.224 brouard 4337:
4338: /* mi is the last effective wave, m is lastpass, mw[j][i] gives the # of j-th effective wave for individual i */
1.216 brouard 4339: /* After last pass */
1.224 brouard 4340: /* Treating death states */
1.214 brouard 4341: if (s[m][i] > nlstate){ /* In a death state */
1.224 brouard 4342: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
4343: /* } */
1.126 brouard 4344: mi++; /* Death is another wave */
4345: /* if(mi==0) never been interviewed correctly before death */
1.223 brouard 4346: /* Only death is a correct wave */
1.126 brouard 4347: mw[mi][i]=m;
1.224 brouard 4348: }
4349: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
4350: else if ((int) andc[i] != 9999) { /* Status is negative. A death occured after lastpass, we can't take it into account because of potential bias */
1.216 brouard 4351: /* m++; */
4352: /* mi++; */
4353: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4354: /* mw[mi][i]=m; */
1.218 brouard 4355: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.224 brouard 4356: if((andc[i]+moisdc[i]/12.) <=(anint[m][i]+mint[m][i]/12.)){ /* death occured before last wave and status should have been death instead of -1 */
4357: nbwarn++;
4358: if(firstfiv==0){
4359: printf("Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %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 );
4360: firstfiv=1;
4361: }else{
4362: fprintf(ficlog,"Warning! Death for individual %ld line=%d occurred at %d/%d before last wave %d interviewed at %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 );
4363: }
4364: }else{ /* Death occured afer last wave potential bias */
4365: nberr++;
4366: if(firstwo==0){
4367: printf("Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%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], i,m );
4368: firstwo=1;
4369: }
4370: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred at %d/%d after last wave %d interviewed at %d/%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], i,m );
1.223 brouard 4371: }
1.218 brouard 4372: }else{ /* end date of interview is known */
1.223 brouard 4373: /* death is known but not confirmed by death status at any wave */
4374: if(firstfour==0){
4375: 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. 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], i,m );
4376: firstfour=1;
4377: }
4378: 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. 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], i,m );
1.214 brouard 4379: }
1.224 brouard 4380: } /* end if date of death is known */
4381: #endif
4382: wav[i]=mi; /* mi should be the last effective wave (or mli) */
4383: /* wav[i]=mw[mi][i]; */
1.126 brouard 4384: if(mi==0){
4385: nbwarn++;
4386: if(first==0){
1.223 brouard 4387: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4388: first=1;
1.126 brouard 4389: }
4390: if(first==1){
1.223 brouard 4391: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 4392: }
4393: } /* end mi==0 */
4394: } /* End individuals */
1.214 brouard 4395: /* wav and mw are no more changed */
1.223 brouard 4396:
1.214 brouard 4397:
1.126 brouard 4398: for(i=1; i<=imx; i++){
4399: for(mi=1; mi<wav[i];mi++){
4400: if (stepm <=0)
1.223 brouard 4401: dh[mi][i]=1;
1.126 brouard 4402: else{
1.223 brouard 4403: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4404: if (agedc[i] < 2*AGESUP) {
4405: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4406: if(j==0) j=1; /* Survives at least one month after exam */
4407: else if(j<0){
4408: nberr++;
4409: 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]);
4410: j=1; /* Temporary Dangerous patch */
4411: 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);
4412: 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]);
4413: 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);
4414: }
4415: k=k+1;
4416: if (j >= jmax){
4417: jmax=j;
4418: ijmax=i;
4419: }
4420: if (j <= jmin){
4421: jmin=j;
4422: ijmin=i;
4423: }
4424: sum=sum+j;
4425: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4426: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4427: }
4428: }
4429: else{
4430: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 4431: /* if (j<0) printf("%d %lf %lf %d %d %d\n", i,agev[mw[mi+1][i]][i], agev[mw[mi][i]][i],j,s[mw[mi][i]][i] ,s[mw[mi+1][i]][i]); */
1.223 brouard 4432:
4433: k=k+1;
4434: if (j >= jmax) {
4435: jmax=j;
4436: ijmax=i;
4437: }
4438: else if (j <= jmin){
4439: jmin=j;
4440: ijmin=i;
4441: }
4442: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4443: /*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]);*/
4444: if(j<0){
4445: nberr++;
4446: 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]);
4447: 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]);
4448: }
4449: sum=sum+j;
4450: }
4451: jk= j/stepm;
4452: jl= j -jk*stepm;
4453: ju= j -(jk+1)*stepm;
4454: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4455: if(jl==0){
4456: dh[mi][i]=jk;
4457: bh[mi][i]=0;
4458: }else{ /* We want a negative bias in order to only have interpolation ie
4459: * to avoid the price of an extra matrix product in likelihood */
4460: dh[mi][i]=jk+1;
4461: bh[mi][i]=ju;
4462: }
4463: }else{
4464: if(jl <= -ju){
4465: dh[mi][i]=jk;
4466: bh[mi][i]=jl; /* bias is positive if real duration
4467: * is higher than the multiple of stepm and negative otherwise.
4468: */
4469: }
4470: else{
4471: dh[mi][i]=jk+1;
4472: bh[mi][i]=ju;
4473: }
4474: if(dh[mi][i]==0){
4475: dh[mi][i]=1; /* At least one step */
4476: bh[mi][i]=ju; /* At least one step */
4477: /* 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);*/
4478: }
4479: } /* end if mle */
1.126 brouard 4480: }
4481: } /* end wave */
4482: }
4483: jmean=sum/k;
4484: printf("Delay (in months) between two waves Min=%d (for indiviudal %ld) Max=%d (%ld) Mean=%f\n\n ",jmin, num[ijmin], jmax, num[ijmax], jmean);
1.141 brouard 4485: fprintf(ficlog,"Delay (in months) between two waves Min=%d (for indiviudal %d) Max=%d (%d) Mean=%f\n\n ",jmin, ijmin, jmax, ijmax, jmean);
1.126 brouard 4486: }
4487:
4488: /*********** Tricode ****************************/
1.220 brouard 4489: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 4490: {
1.144 brouard 4491: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4492: /* Tvar[i]=atoi(stre); find 'n' in Vn and stores in Tvar. If model=V2+V1 Tvar[1]=2 and Tvar[2]=1
1.169 brouard 4493: * Boring subroutine which should only output nbcode[Tvar[j]][k]
1.224 brouard 4494: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
4495: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
1.144 brouard 4496: */
1.130 brouard 4497:
1.145 brouard 4498: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 4499: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 4500: int cptcode=0; /* Modality max of covariates j */
4501: int modmincovj=0; /* Modality min of covariates j */
4502:
4503:
1.220 brouard 4504: /* cptcoveff=0; */
1.224 brouard 4505: /* *cptcov=0; */
1.126 brouard 4506:
1.144 brouard 4507: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4508:
1.224 brouard 4509: /* Loop on covariates without age and products and no quantitative variable */
4510: /* for (j=1; j<=(cptcovs); j++) { /\* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only *\/ */
1.225 ! brouard 4511: for (j=1; j<=cptcovsnq; j++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
1.192 brouard 4512: for (k=-1; k < maxncov; k++) Ndum[k]=0;
1.186 brouard 4513: for (i=1; i<=imx; i++) { /* Loop on individuals: reads the data file to get the maximum value of the
1.225 ! brouard 4514: modality of this covariate Vj*/
! 4515: switch(Typevar[j]) {
! 4516: case 1: /* A real fixed dummy covariate */
! 4517: ij=(int)(covar[Tvar[j]][i]); /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
! 4518: * If product of Vn*Vm, still boolean *:
! 4519: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
! 4520: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
! 4521: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
! 4522: modality of the nth covariate of individual i. */
! 4523: if (ij > modmaxcovj)
! 4524: modmaxcovj=ij;
! 4525: else if (ij < modmincovj)
! 4526: modmincovj=ij;
! 4527: if ((ij < -1) && (ij > NCOVMAX)){
! 4528: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
! 4529: exit(1);
! 4530: }else
! 4531: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
! 4532: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
! 4533: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
! 4534: /* getting the maximum value of the modality of the covariate
! 4535: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
! 4536: female ies 1, then modmaxcovj=1.*/
! 4537: break;
! 4538: case 2:
! 4539: break;
! 4540:
! 4541: }
! 4542: } /* end for loop on individuals i */
1.145 brouard 4543: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", j, Tvar[j], modmincovj, modmaxcovj);
1.192 brouard 4544: fprintf(ficlog," Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", j, Tvar[j], modmincovj, modmaxcovj);
1.145 brouard 4545: cptcode=modmaxcovj;
1.137 brouard 4546: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
1.225 ! brouard 4547: /*for (i=0; i<=cptcode; i++) {*/
1.192 brouard 4548: for (k=modmincovj; k<=modmaxcovj; k++) { /* k=-1 ? 0 and 1*//* For each value k of the modality of model-cov j */
4549: printf("Frequencies of covariates %d ie V%d with value %d: %d\n", j, Tvar[j], k, Ndum[k]);
4550: fprintf(ficlog, "Frequencies of covariates %d ie V%d with value %d: %d\n", j, Tvar[j], k, Ndum[k]);
4551: if( Ndum[k] != 0 ){ /* Counts if nobody answered modality k ie empty modality, we skip it and reorder */
1.225 ! brouard 4552: if( k != -1){
! 4553: ncodemax[j]++; /* ncodemax[j]= Number of modalities of the j th
! 4554: covariate for which somebody answered excluding
! 4555: undefined. Usually 2: 0 and 1. */
! 4556: }
! 4557: ncodemaxwundef[j]++; /* ncodemax[j]= Number of modalities of the j th
! 4558: covariate for which somebody answered including
! 4559: undefined. Usually 3: -1, 0 and 1. */
1.145 brouard 4560: }
4561: /* In fact ncodemax[j]=2 (dichotom. variables only) but it could be more for
1.225 ! brouard 4562: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
1.131 brouard 4563: } /* Ndum[-1] number of undefined modalities */
1.225 ! brouard 4564:
1.136 brouard 4565: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
1.186 brouard 4566: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7.
4567: If Ndum[1]=0, Ndum[2]=0, Ndum[3]= 635, Ndum[4]=0, Ndum[5]=0, Ndum[6]=27, Ndum[7]=125;
1.145 brouard 4568: modmincovj=3; modmaxcovj = 7;
1.186 brouard 4569: There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3;
4570: which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10;
4571: defining two dummy variables: variables V1_1 and V1_2.
1.145 brouard 4572: nbcode[Tvar[j]][ij]=k;
4573: nbcode[Tvar[j]][1]=0;
4574: nbcode[Tvar[j]][2]=1;
4575: nbcode[Tvar[j]][3]=2;
1.197 brouard 4576: To be continued (not working yet).
1.145 brouard 4577: */
1.197 brouard 4578: ij=0; /* ij is similar to i but can jump over null modalities */
4579: 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*/
1.225 ! brouard 4580: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
! 4581: break;
! 4582: }
! 4583: ij++;
! 4584: nbcode[Tvar[j]][ij]=i; /* stores the original value of modality i in an array nbcode, ij modality from 1 to last non-nul modality.*/
! 4585: cptcode = ij; /* New max modality for covar j */
1.192 brouard 4586: } /* end of loop on modality i=-1 to 1 or more */
1.225 ! brouard 4587:
1.192 brouard 4588: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4589: /* /\*recode from 0 *\/ */
4590: /* k is a modality. If we have model=V1+V1*sex */
4591: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4592: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4593: /* } */
4594: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4595: /* if (ij > ncodemax[j]) { */
4596: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4597: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4598: /* break; */
4599: /* } */
4600: /* } /\* end of loop on modality k *\/ */
1.137 brouard 4601: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4602:
1.225 ! brouard 4603: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.137 brouard 4604:
1.187 brouard 4605: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
1.225 ! brouard 4606: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
! 4607: ij=Tvar[i]; /* Tvar might be -1 if status was unknown */
! 4608: Ndum[ij]++; /* Might be supersed V1 + V1*age */
! 4609: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
! 4610:
! 4611: ij=0;
! 4612: for (i=0; i<= maxncov-1; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
! 4613: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
! 4614: if((Ndum[i]!=0) && (i<=ncovcol)){
! 4615: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
! 4616: Tvaraff[++ij]=i; /*For printing (unclear) */
! 4617: }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){
! 4618: Tvaraff[++ij]=-10; /* Dont'n know how to treat quantitative variables yet */
! 4619: }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){
! 4620: Tvaraff[++ij]=i; /*For printing (unclear) */
! 4621: }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){
! 4622: Tvaraff[++ij]=-20; /* Dont'n know how to treat quantitative variables yet */
! 4623: }
! 4624: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
! 4625: /* ij--; */
! 4626: /* cptcoveff=ij; /\*Number of total covariates*\/ */
! 4627: *cptcov=ij; /*Number of total real effective covariates: effective
! 4628: * because they can be excluded from the model and real
! 4629: * if in the model but excluded because missing values*/
1.126 brouard 4630: }
4631:
1.145 brouard 4632:
1.126 brouard 4633: /*********** Health Expectancies ****************/
4634:
1.127 brouard 4635: void evsij(double ***eij, double x[], int nlstate, int stepm, int bage, int fage, double **oldm, double **savm, int cij, int estepm,char strstart[] )
1.126 brouard 4636:
4637: {
4638: /* Health expectancies, no variances */
1.164 brouard 4639: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 4640: int nhstepma, nstepma; /* Decreasing with age */
4641: double age, agelim, hf;
4642: double ***p3mat;
4643: double eip;
4644:
4645: pstamp(ficreseij);
4646: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
4647: fprintf(ficreseij,"# Age");
4648: for(i=1; i<=nlstate;i++){
4649: for(j=1; j<=nlstate;j++){
4650: fprintf(ficreseij," e%1d%1d ",i,j);
4651: }
4652: fprintf(ficreseij," e%1d. ",i);
4653: }
4654: fprintf(ficreseij,"\n");
4655:
4656:
4657: if(estepm < stepm){
4658: printf ("Problem %d lower than %d\n",estepm, stepm);
4659: }
4660: else hstepm=estepm;
4661: /* We compute the life expectancy from trapezoids spaced every estepm months
4662: * This is mainly to measure the difference between two models: for example
4663: * if stepm=24 months pijx are given only every 2 years and by summing them
4664: * we are calculating an estimate of the Life Expectancy assuming a linear
4665: * progression in between and thus overestimating or underestimating according
4666: * to the curvature of the survival function. If, for the same date, we
4667: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4668: * to compare the new estimate of Life expectancy with the same linear
4669: * hypothesis. A more precise result, taking into account a more precise
4670: * curvature will be obtained if estepm is as small as stepm. */
4671:
4672: /* For example we decided to compute the life expectancy with the smallest unit */
4673: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4674: nhstepm is the number of hstepm from age to agelim
4675: nstepm is the number of stepm from age to agelin.
4676: Look at hpijx to understand the reason of that which relies in memory size
4677: and note for a fixed period like estepm months */
4678: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4679: survival function given by stepm (the optimization length). Unfortunately it
4680: means that if the survival funtion is printed only each two years of age and if
4681: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4682: results. So we changed our mind and took the option of the best precision.
4683: */
4684: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4685:
4686: agelim=AGESUP;
4687: /* If stepm=6 months */
4688: /* Computed by stepm unit matrices, product of hstepm matrices, stored
4689: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
4690:
4691: /* nhstepm age range expressed in number of stepm */
4692: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4693: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4694: /* if (stepm >= YEARM) hstepm=1;*/
4695: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4696: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4697:
4698: for (age=bage; age<=fage; age ++){
4699: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4700: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4701: /* if (stepm >= YEARM) hstepm=1;*/
4702: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
4703:
4704: /* If stepm=6 months */
4705: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4706: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4707:
4708: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4709:
4710: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
4711:
4712: printf("%d|",(int)age);fflush(stdout);
4713: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4714:
4715: /* Computing expectancies */
4716: for(i=1; i<=nlstate;i++)
4717: for(j=1; j<=nlstate;j++)
4718: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4719: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
4720:
4721: /* 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]);*/
4722:
4723: }
4724:
4725: fprintf(ficreseij,"%3.0f",age );
4726: for(i=1; i<=nlstate;i++){
4727: eip=0;
4728: for(j=1; j<=nlstate;j++){
4729: eip +=eij[i][j][(int)age];
4730: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
4731: }
4732: fprintf(ficreseij,"%9.4f", eip );
4733: }
4734: fprintf(ficreseij,"\n");
4735:
4736: }
4737: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4738: printf("\n");
4739: fprintf(ficlog,"\n");
4740:
4741: }
4742:
1.127 brouard 4743: 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[] )
1.126 brouard 4744:
4745: {
4746: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 4747: to initial status i, ei. .
1.126 brouard 4748: */
4749: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
4750: int nhstepma, nstepma; /* Decreasing with age */
4751: double age, agelim, hf;
4752: double ***p3matp, ***p3matm, ***varhe;
4753: double **dnewm,**doldm;
4754: double *xp, *xm;
4755: double **gp, **gm;
4756: double ***gradg, ***trgradg;
4757: int theta;
4758:
4759: double eip, vip;
4760:
4761: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
4762: xp=vector(1,npar);
4763: xm=vector(1,npar);
4764: dnewm=matrix(1,nlstate*nlstate,1,npar);
4765: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
4766:
4767: pstamp(ficresstdeij);
4768: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
4769: fprintf(ficresstdeij,"# Age");
4770: for(i=1; i<=nlstate;i++){
4771: for(j=1; j<=nlstate;j++)
4772: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
4773: fprintf(ficresstdeij," e%1d. ",i);
4774: }
4775: fprintf(ficresstdeij,"\n");
4776:
4777: pstamp(ficrescveij);
4778: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
4779: fprintf(ficrescveij,"# Age");
4780: for(i=1; i<=nlstate;i++)
4781: for(j=1; j<=nlstate;j++){
4782: cptj= (j-1)*nlstate+i;
4783: for(i2=1; i2<=nlstate;i2++)
4784: for(j2=1; j2<=nlstate;j2++){
4785: cptj2= (j2-1)*nlstate+i2;
4786: if(cptj2 <= cptj)
4787: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
4788: }
4789: }
4790: fprintf(ficrescveij,"\n");
4791:
4792: if(estepm < stepm){
4793: printf ("Problem %d lower than %d\n",estepm, stepm);
4794: }
4795: else hstepm=estepm;
4796: /* We compute the life expectancy from trapezoids spaced every estepm months
4797: * This is mainly to measure the difference between two models: for example
4798: * if stepm=24 months pijx are given only every 2 years and by summing them
4799: * we are calculating an estimate of the Life Expectancy assuming a linear
4800: * progression in between and thus overestimating or underestimating according
4801: * to the curvature of the survival function. If, for the same date, we
4802: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4803: * to compare the new estimate of Life expectancy with the same linear
4804: * hypothesis. A more precise result, taking into account a more precise
4805: * curvature will be obtained if estepm is as small as stepm. */
4806:
4807: /* For example we decided to compute the life expectancy with the smallest unit */
4808: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4809: nhstepm is the number of hstepm from age to agelim
4810: nstepm is the number of stepm from age to agelin.
4811: Look at hpijx to understand the reason of that which relies in memory size
4812: and note for a fixed period like estepm months */
4813: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4814: survival function given by stepm (the optimization length). Unfortunately it
4815: means that if the survival funtion is printed only each two years of age and if
4816: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4817: results. So we changed our mind and took the option of the best precision.
4818: */
4819: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4820:
4821: /* If stepm=6 months */
4822: /* nhstepm age range expressed in number of stepm */
4823: agelim=AGESUP;
4824: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
4825: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4826: /* if (stepm >= YEARM) hstepm=1;*/
4827: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4828:
4829: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4830: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4831: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
4832: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
4833: gp=matrix(0,nhstepm,1,nlstate*nlstate);
4834: gm=matrix(0,nhstepm,1,nlstate*nlstate);
4835:
4836: for (age=bage; age<=fage; age ++){
4837: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4838: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4839: /* if (stepm >= YEARM) hstepm=1;*/
4840: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 4841:
1.126 brouard 4842: /* If stepm=6 months */
4843: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4844: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4845:
4846: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 4847:
1.126 brouard 4848: /* Computing Variances of health expectancies */
4849: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
4850: decrease memory allocation */
4851: for(theta=1; theta <=npar; theta++){
4852: for(i=1; i<=npar; i++){
1.222 brouard 4853: xp[i] = x[i] + (i==theta ?delti[theta]:0);
4854: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 4855: }
4856: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
4857: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
1.218 brouard 4858:
1.126 brouard 4859: for(j=1; j<= nlstate; j++){
1.222 brouard 4860: for(i=1; i<=nlstate; i++){
4861: for(h=0; h<=nhstepm-1; h++){
4862: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
4863: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
4864: }
4865: }
1.126 brouard 4866: }
1.218 brouard 4867:
1.126 brouard 4868: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 4869: for(h=0; h<=nhstepm-1; h++){
4870: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
4871: }
1.126 brouard 4872: }/* End theta */
4873:
4874:
4875: for(h=0; h<=nhstepm-1; h++)
4876: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 4877: for(theta=1; theta <=npar; theta++)
4878: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 4879:
1.218 brouard 4880:
1.222 brouard 4881: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 4882: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 4883: varhe[ij][ji][(int)age] =0.;
1.218 brouard 4884:
1.222 brouard 4885: printf("%d|",(int)age);fflush(stdout);
4886: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4887: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 4888: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 4889: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
4890: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
4891: for(ij=1;ij<=nlstate*nlstate;ij++)
4892: for(ji=1;ji<=nlstate*nlstate;ji++)
4893: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 4894: }
4895: }
1.218 brouard 4896:
1.126 brouard 4897: /* Computing expectancies */
4898: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4899: for(i=1; i<=nlstate;i++)
4900: for(j=1; j<=nlstate;j++)
1.222 brouard 4901: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4902: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 4903:
1.222 brouard 4904: /* if((int)age==70)printf("i=%2d,j=%2d,h=%2d,age=%3d,%9.4f,%9.4f,%9.4f\n",i,j,h,(int)age,p3mat[i][j][h],hf,eij[i][j][(int)age]);*/
1.218 brouard 4905:
1.222 brouard 4906: }
1.218 brouard 4907:
1.126 brouard 4908: fprintf(ficresstdeij,"%3.0f",age );
4909: for(i=1; i<=nlstate;i++){
4910: eip=0.;
4911: vip=0.;
4912: for(j=1; j<=nlstate;j++){
1.222 brouard 4913: eip += eij[i][j][(int)age];
4914: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
4915: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
4916: fprintf(ficresstdeij," %9.4f (%.4f)", eij[i][j][(int)age], sqrt(varhe[(j-1)*nlstate+i][(j-1)*nlstate+i][(int)age]) );
1.126 brouard 4917: }
4918: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
4919: }
4920: fprintf(ficresstdeij,"\n");
1.218 brouard 4921:
1.126 brouard 4922: fprintf(ficrescveij,"%3.0f",age );
4923: for(i=1; i<=nlstate;i++)
4924: for(j=1; j<=nlstate;j++){
1.222 brouard 4925: cptj= (j-1)*nlstate+i;
4926: for(i2=1; i2<=nlstate;i2++)
4927: for(j2=1; j2<=nlstate;j2++){
4928: cptj2= (j2-1)*nlstate+i2;
4929: if(cptj2 <= cptj)
4930: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
4931: }
1.126 brouard 4932: }
4933: fprintf(ficrescveij,"\n");
1.218 brouard 4934:
1.126 brouard 4935: }
4936: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
4937: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
4938: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
4939: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
4940: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4941: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4942: printf("\n");
4943: fprintf(ficlog,"\n");
1.218 brouard 4944:
1.126 brouard 4945: free_vector(xm,1,npar);
4946: free_vector(xp,1,npar);
4947: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
4948: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
4949: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
4950: }
1.218 brouard 4951:
1.126 brouard 4952: /************ Variance ******************/
1.209 brouard 4953: 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[])
1.218 brouard 4954: {
4955: /* Variance of health expectancies */
4956: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
4957: /* double **newm;*/
4958: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
4959:
4960: /* int movingaverage(); */
4961: double **dnewm,**doldm;
4962: double **dnewmp,**doldmp;
4963: int i, j, nhstepm, hstepm, h, nstepm ;
4964: int k;
4965: double *xp;
4966: double **gp, **gm; /* for var eij */
4967: double ***gradg, ***trgradg; /*for var eij */
4968: double **gradgp, **trgradgp; /* for var p point j */
4969: double *gpp, *gmp; /* for var p point j */
4970: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
4971: double ***p3mat;
4972: double age,agelim, hf;
4973: /* double ***mobaverage; */
4974: int theta;
4975: char digit[4];
4976: char digitp[25];
4977:
4978: char fileresprobmorprev[FILENAMELENGTH];
4979:
4980: if(popbased==1){
4981: if(mobilav!=0)
4982: strcpy(digitp,"-POPULBASED-MOBILAV_");
4983: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
4984: }
4985: else
4986: strcpy(digitp,"-STABLBASED_");
1.126 brouard 4987:
1.218 brouard 4988: /* if (mobilav!=0) { */
4989: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
4990: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
4991: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
4992: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
4993: /* } */
4994: /* } */
4995:
4996: strcpy(fileresprobmorprev,"PRMORPREV-");
4997: sprintf(digit,"%-d",ij);
4998: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
4999: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5000: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5001: strcat(fileresprobmorprev,fileresu);
5002: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
5003: printf("Problem with resultfile: %s\n", fileresprobmorprev);
5004: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
5005: }
5006: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5007: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5008: pstamp(ficresprobmorprev);
5009: 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);
5010: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
5011: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5012: fprintf(ficresprobmorprev," p.%-d SE",j);
5013: for(i=1; i<=nlstate;i++)
5014: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
5015: }
5016: fprintf(ficresprobmorprev,"\n");
5017:
5018: fprintf(ficgp,"\n# Routine varevsij");
5019: fprintf(ficgp,"\nunset title \n");
5020: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
5021: 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");
5022: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
5023: /* } */
5024: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5025: pstamp(ficresvij);
5026: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
5027: if(popbased==1)
5028: 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);
5029: else
5030: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
5031: fprintf(ficresvij,"# Age");
5032: for(i=1; i<=nlstate;i++)
5033: for(j=1; j<=nlstate;j++)
5034: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
5035: fprintf(ficresvij,"\n");
5036:
5037: xp=vector(1,npar);
5038: dnewm=matrix(1,nlstate,1,npar);
5039: doldm=matrix(1,nlstate,1,nlstate);
5040: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
5041: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5042:
5043: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
5044: gpp=vector(nlstate+1,nlstate+ndeath);
5045: gmp=vector(nlstate+1,nlstate+ndeath);
5046: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 5047:
1.218 brouard 5048: if(estepm < stepm){
5049: printf ("Problem %d lower than %d\n",estepm, stepm);
5050: }
5051: else hstepm=estepm;
5052: /* For example we decided to compute the life expectancy with the smallest unit */
5053: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5054: nhstepm is the number of hstepm from age to agelim
5055: nstepm is the number of stepm from age to agelim.
5056: Look at function hpijx to understand why because of memory size limitations,
5057: we decided (b) to get a life expectancy respecting the most precise curvature of the
5058: survival function given by stepm (the optimization length). Unfortunately it
5059: means that if the survival funtion is printed every two years of age and if
5060: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5061: results. So we changed our mind and took the option of the best precision.
5062: */
5063: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5064: agelim = AGESUP;
5065: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5066: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5067: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5068: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5069: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
5070: gp=matrix(0,nhstepm,1,nlstate);
5071: gm=matrix(0,nhstepm,1,nlstate);
5072:
5073:
5074: for(theta=1; theta <=npar; theta++){
5075: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
5076: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5077: }
5078:
5079: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5080:
5081: if (popbased==1) {
5082: if(mobilav ==0){
5083: for(i=1; i<=nlstate;i++)
5084: prlim[i][i]=probs[(int)age][i][ij];
5085: }else{ /* mobilav */
5086: for(i=1; i<=nlstate;i++)
5087: prlim[i][i]=mobaverage[(int)age][i][ij];
5088: }
5089: }
5090:
5091: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij); /* Returns p3mat[i][j][h] for h=1 to nhstepm */
5092: for(j=1; j<= nlstate; j++){
5093: for(h=0; h<=nhstepm; h++){
5094: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
5095: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
5096: }
5097: }
5098: /* Next for computing probability of death (h=1 means
5099: computed over hstepm matrices product = hstepm*stepm months)
5100: as a weighted average of prlim.
5101: */
5102: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5103: for(i=1,gpp[j]=0.; i<= nlstate; i++)
5104: gpp[j] += prlim[i][i]*p3mat[i][j][1];
5105: }
5106: /* end probability of death */
5107:
5108: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
5109: xp[i] = x[i] - (i==theta ?delti[theta]:0);
5110:
5111: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij);
5112:
5113: if (popbased==1) {
5114: if(mobilav ==0){
5115: for(i=1; i<=nlstate;i++)
5116: prlim[i][i]=probs[(int)age][i][ij];
5117: }else{ /* mobilav */
5118: for(i=1; i<=nlstate;i++)
5119: prlim[i][i]=mobaverage[(int)age][i][ij];
5120: }
5121: }
5122:
5123: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
5124:
5125: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
5126: for(h=0; h<=nhstepm; h++){
5127: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
5128: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
5129: }
5130: }
5131: /* This for computing probability of death (h=1 means
5132: computed over hstepm matrices product = hstepm*stepm months)
5133: as a weighted average of prlim.
5134: */
5135: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5136: for(i=1,gmp[j]=0.; i<= nlstate; i++)
5137: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5138: }
5139: /* end probability of death */
5140:
5141: for(j=1; j<= nlstate; j++) /* vareij */
5142: for(h=0; h<=nhstepm; h++){
5143: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
5144: }
5145:
5146: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
5147: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
5148: }
5149:
5150: } /* End theta */
5151:
5152: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
5153:
5154: for(h=0; h<=nhstepm; h++) /* veij */
5155: for(j=1; j<=nlstate;j++)
5156: for(theta=1; theta <=npar; theta++)
5157: trgradg[h][j][theta]=gradg[h][theta][j];
5158:
5159: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
5160: for(theta=1; theta <=npar; theta++)
5161: trgradgp[j][theta]=gradgp[theta][j];
5162:
5163:
5164: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5165: for(i=1;i<=nlstate;i++)
5166: for(j=1;j<=nlstate;j++)
5167: vareij[i][j][(int)age] =0.;
5168:
5169: for(h=0;h<=nhstepm;h++){
5170: for(k=0;k<=nhstepm;k++){
5171: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
5172: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
5173: for(i=1;i<=nlstate;i++)
5174: for(j=1;j<=nlstate;j++)
5175: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
5176: }
5177: }
5178:
5179: /* pptj */
5180: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
5181: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
5182: for(j=nlstate+1;j<=nlstate+ndeath;j++)
5183: for(i=nlstate+1;i<=nlstate+ndeath;i++)
5184: varppt[j][i]=doldmp[j][i];
5185: /* end ppptj */
5186: /* x centered again */
5187:
5188: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij);
5189:
5190: if (popbased==1) {
5191: if(mobilav ==0){
5192: for(i=1; i<=nlstate;i++)
5193: prlim[i][i]=probs[(int)age][i][ij];
5194: }else{ /* mobilav */
5195: for(i=1; i<=nlstate;i++)
5196: prlim[i][i]=mobaverage[(int)age][i][ij];
5197: }
5198: }
5199:
5200: /* This for computing probability of death (h=1 means
5201: computed over hstepm (estepm) matrices product = hstepm*stepm months)
5202: as a weighted average of prlim.
5203: */
5204: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
5205: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5206: for(i=1,gmp[j]=0.;i<= nlstate; i++)
5207: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5208: }
5209: /* end probability of death */
5210:
5211: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
5212: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5213: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
5214: for(i=1; i<=nlstate;i++){
5215: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
5216: }
5217: }
5218: fprintf(ficresprobmorprev,"\n");
5219:
5220: fprintf(ficresvij,"%.0f ",age );
5221: for(i=1; i<=nlstate;i++)
5222: for(j=1; j<=nlstate;j++){
5223: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
5224: }
5225: fprintf(ficresvij,"\n");
5226: free_matrix(gp,0,nhstepm,1,nlstate);
5227: free_matrix(gm,0,nhstepm,1,nlstate);
5228: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
5229: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
5230: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5231: } /* End age */
5232: free_vector(gpp,nlstate+1,nlstate+ndeath);
5233: free_vector(gmp,nlstate+1,nlstate+ndeath);
5234: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
5235: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
5236: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
5237: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
5238: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
5239: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
5240: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5241: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
5242: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
5243: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
5244: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
5245: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
5246: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
5247: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
5248: 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);
5249: /* fprintf(fichtm,"\n<br> Probability is computed over estepm=%d months and then divided by estepm and multiplied by %.0f in order to have the probability to die over a year <br> <img src=\"varmuptjgr%s%s.svg\"> <br>\n", stepm,YEARM,digitp,digit);
1.126 brouard 5250: */
1.218 brouard 5251: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
5252: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 5253:
1.218 brouard 5254: free_vector(xp,1,npar);
5255: free_matrix(doldm,1,nlstate,1,nlstate);
5256: free_matrix(dnewm,1,nlstate,1,npar);
5257: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5258: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
5259: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5260: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5261: fclose(ficresprobmorprev);
5262: fflush(ficgp);
5263: fflush(fichtm);
5264: } /* end varevsij */
1.126 brouard 5265:
5266: /************ Variance of prevlim ******************/
1.209 brouard 5267: void varprevlim(char fileres[], 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[])
1.126 brouard 5268: {
1.205 brouard 5269: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 5270: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 5271:
1.126 brouard 5272: double **dnewm,**doldm;
5273: int i, j, nhstepm, hstepm;
5274: double *xp;
5275: double *gp, *gm;
5276: double **gradg, **trgradg;
1.208 brouard 5277: double **mgm, **mgp;
1.126 brouard 5278: double age,agelim;
5279: int theta;
5280:
5281: pstamp(ficresvpl);
5282: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
5283: fprintf(ficresvpl,"# Age");
5284: for(i=1; i<=nlstate;i++)
5285: fprintf(ficresvpl," %1d-%1d",i,i);
5286: fprintf(ficresvpl,"\n");
5287:
5288: xp=vector(1,npar);
5289: dnewm=matrix(1,nlstate,1,npar);
5290: doldm=matrix(1,nlstate,1,nlstate);
5291:
5292: hstepm=1*YEARM; /* Every year of age */
5293: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5294: agelim = AGESUP;
5295: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5296: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5297: if (stepm >= YEARM) hstepm=1;
5298: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5299: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5300: mgp=matrix(1,npar,1,nlstate);
5301: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5302: gp=vector(1,nlstate);
5303: gm=vector(1,nlstate);
5304:
5305: for(theta=1; theta <=npar; theta++){
5306: for(i=1; i<=npar; i++){ /* Computes gradient */
5307: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5308: }
1.209 brouard 5309: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5310: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5311: else
5312: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5313: for(i=1;i<=nlstate;i++){
1.126 brouard 5314: gp[i] = prlim[i][i];
1.208 brouard 5315: mgp[theta][i] = prlim[i][i];
5316: }
1.126 brouard 5317: for(i=1; i<=npar; i++) /* Computes gradient */
5318: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5319: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5320: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5321: else
5322: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5323: for(i=1;i<=nlstate;i++){
1.126 brouard 5324: gm[i] = prlim[i][i];
1.208 brouard 5325: mgm[theta][i] = prlim[i][i];
5326: }
1.126 brouard 5327: for(i=1;i<=nlstate;i++)
5328: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5329: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5330: } /* End theta */
5331:
5332: trgradg =matrix(1,nlstate,1,npar);
5333:
5334: for(j=1; j<=nlstate;j++)
5335: for(theta=1; theta <=npar; theta++)
5336: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5337: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5338: /* printf("\nmgm mgp %d ",(int)age); */
5339: /* for(j=1; j<=nlstate;j++){ */
5340: /* printf(" %d ",j); */
5341: /* for(theta=1; theta <=npar; theta++) */
5342: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5343: /* printf("\n "); */
5344: /* } */
5345: /* } */
5346: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5347: /* printf("\n gradg %d ",(int)age); */
5348: /* for(j=1; j<=nlstate;j++){ */
5349: /* printf("%d ",j); */
5350: /* for(theta=1; theta <=npar; theta++) */
5351: /* printf("%d %lf ",theta,gradg[theta][j]); */
5352: /* printf("\n "); */
5353: /* } */
5354: /* } */
1.126 brouard 5355:
5356: for(i=1;i<=nlstate;i++)
5357: varpl[i][(int)age] =0.;
1.209 brouard 5358: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5359: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5360: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5361: }else{
1.126 brouard 5362: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5363: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5364: }
1.126 brouard 5365: for(i=1;i<=nlstate;i++)
5366: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5367:
5368: fprintf(ficresvpl,"%.0f ",age );
5369: for(i=1; i<=nlstate;i++)
5370: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5371: fprintf(ficresvpl,"\n");
5372: free_vector(gp,1,nlstate);
5373: free_vector(gm,1,nlstate);
1.208 brouard 5374: free_matrix(mgm,1,npar,1,nlstate);
5375: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5376: free_matrix(gradg,1,npar,1,nlstate);
5377: free_matrix(trgradg,1,nlstate,1,npar);
5378: } /* End age */
5379:
5380: free_vector(xp,1,npar);
5381: free_matrix(doldm,1,nlstate,1,npar);
5382: free_matrix(dnewm,1,nlstate,1,nlstate);
5383:
5384: }
5385:
5386: /************ Variance of one-step probabilities ******************/
5387: void varprob(char optionfilefiname[], double **matcov, double x[], double delti[], int nlstate, double bage, double fage, int ij, int *Tvar, int **nbcode, int *ncodemax, char strstart[])
1.222 brouard 5388: {
5389: int i, j=0, k1, l1, tj;
5390: int k2, l2, j1, z1;
5391: int k=0, l;
5392: int first=1, first1, first2;
5393: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5394: double **dnewm,**doldm;
5395: double *xp;
5396: double *gp, *gm;
5397: double **gradg, **trgradg;
5398: double **mu;
5399: double age, cov[NCOVMAX+1];
5400: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5401: int theta;
5402: char fileresprob[FILENAMELENGTH];
5403: char fileresprobcov[FILENAMELENGTH];
5404: char fileresprobcor[FILENAMELENGTH];
5405: double ***varpij;
5406:
5407: strcpy(fileresprob,"PROB_");
5408: strcat(fileresprob,fileres);
5409: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5410: printf("Problem with resultfile: %s\n", fileresprob);
5411: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5412: }
5413: strcpy(fileresprobcov,"PROBCOV_");
5414: strcat(fileresprobcov,fileresu);
5415: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5416: printf("Problem with resultfile: %s\n", fileresprobcov);
5417: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5418: }
5419: strcpy(fileresprobcor,"PROBCOR_");
5420: strcat(fileresprobcor,fileresu);
5421: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5422: printf("Problem with resultfile: %s\n", fileresprobcor);
5423: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5424: }
5425: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5426: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5427: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5428: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5429: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5430: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5431: pstamp(ficresprob);
5432: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5433: fprintf(ficresprob,"# Age");
5434: pstamp(ficresprobcov);
5435: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5436: fprintf(ficresprobcov,"# Age");
5437: pstamp(ficresprobcor);
5438: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5439: fprintf(ficresprobcor,"# Age");
1.126 brouard 5440:
5441:
1.222 brouard 5442: for(i=1; i<=nlstate;i++)
5443: for(j=1; j<=(nlstate+ndeath);j++){
5444: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5445: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5446: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5447: }
5448: /* fprintf(ficresprob,"\n");
5449: fprintf(ficresprobcov,"\n");
5450: fprintf(ficresprobcor,"\n");
5451: */
5452: xp=vector(1,npar);
5453: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5454: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5455: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5456: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5457: first=1;
5458: fprintf(ficgp,"\n# Routine varprob");
5459: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5460: fprintf(fichtm,"\n");
5461:
5462: 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.</li>\n",optionfilehtmcov);
5463: 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);
5464: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5465: and drawn. It helps understanding how is the covariance between two incidences.\
5466: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 5467: fprintf(fichtmcov,"\n<br> Contour plot corresponding to x'cov<sup>-1</sup>x = 4 (where x is the column vector (pij,pkl)) are drawn. \
1.126 brouard 5468: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5469: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5470: standard deviations wide on each axis. <br>\
5471: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5472: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5473: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5474:
1.222 brouard 5475: cov[1]=1;
5476: /* tj=cptcoveff; */
1.225 ! brouard 5477: tj = (int) pow(2,cptcoveff);
1.222 brouard 5478: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5479: j1=0;
1.224 brouard 5480: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 5481: if (cptcovn>0) {
5482: fprintf(ficresprob, "\n#********** Variable ");
1.225 ! brouard 5483: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5484: fprintf(ficresprob, "**********\n#\n");
5485: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 ! brouard 5486: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5487: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 5488:
1.222 brouard 5489: fprintf(ficgp, "\n#********** Variable ");
1.225 ! brouard 5490: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5491: fprintf(ficgp, "**********\n#\n");
1.220 brouard 5492:
5493:
1.222 brouard 5494: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 ! brouard 5495: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5496: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5497:
1.222 brouard 5498: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 ! brouard 5499: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5500: fprintf(ficresprobcor, "**********\n#");
5501: if(invalidvarcomb[j1]){
5502: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
5503: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
5504: continue;
5505: }
5506: }
5507: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5508: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5509: gp=vector(1,(nlstate)*(nlstate+ndeath));
5510: gm=vector(1,(nlstate)*(nlstate+ndeath));
5511: for (age=bage; age<=fage; age ++){
5512: cov[2]=age;
5513: if(nagesqr==1)
5514: cov[3]= age*age;
5515: for (k=1; k<=cptcovn;k++) {
5516: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5517: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
5518: * 1 1 1 1 1
5519: * 2 2 1 1 1
5520: * 3 1 2 1 1
5521: */
5522: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
5523: }
5524: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5525: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
5526: for (k=1; k<=cptcovprod;k++)
5527: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 5528:
5529:
1.222 brouard 5530: for(theta=1; theta <=npar; theta++){
5531: for(i=1; i<=npar; i++)
5532: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 5533:
1.222 brouard 5534: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 5535:
1.222 brouard 5536: k=0;
5537: for(i=1; i<= (nlstate); i++){
5538: for(j=1; j<=(nlstate+ndeath);j++){
5539: k=k+1;
5540: gp[k]=pmmij[i][j];
5541: }
5542: }
1.220 brouard 5543:
1.222 brouard 5544: for(i=1; i<=npar; i++)
5545: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 5546:
1.222 brouard 5547: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5548: k=0;
5549: for(i=1; i<=(nlstate); i++){
5550: for(j=1; j<=(nlstate+ndeath);j++){
5551: k=k+1;
5552: gm[k]=pmmij[i][j];
5553: }
5554: }
1.220 brouard 5555:
1.222 brouard 5556: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5557: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5558: }
1.126 brouard 5559:
1.222 brouard 5560: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5561: for(theta=1; theta <=npar; theta++)
5562: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 5563:
1.222 brouard 5564: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5565: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 5566:
1.222 brouard 5567: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 5568:
1.222 brouard 5569: k=0;
5570: for(i=1; i<=(nlstate); i++){
5571: for(j=1; j<=(nlstate+ndeath);j++){
5572: k=k+1;
5573: mu[k][(int) age]=pmmij[i][j];
5574: }
5575: }
5576: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5577: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5578: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 5579:
1.222 brouard 5580: /*printf("\n%d ",(int)age);
5581: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5582: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5583: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5584: }*/
1.220 brouard 5585:
1.222 brouard 5586: fprintf(ficresprob,"\n%d ",(int)age);
5587: fprintf(ficresprobcov,"\n%d ",(int)age);
5588: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 5589:
1.222 brouard 5590: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5591: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5592: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5593: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5594: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5595: }
5596: i=0;
5597: for (k=1; k<=(nlstate);k++){
5598: for (l=1; l<=(nlstate+ndeath);l++){
5599: i++;
5600: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5601: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5602: for (j=1; j<=i;j++){
5603: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
5604: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5605: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5606: }
5607: }
5608: }/* end of loop for state */
5609: } /* end of loop for age */
5610: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
5611: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
5612: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5613: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5614:
5615: /* Confidence intervalle of pij */
5616: /*
5617: fprintf(ficgp,"\nunset parametric;unset label");
5618: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
5619: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
5620: 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);
5621: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
5622: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
5623: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
5624: */
5625:
5626: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
5627: first1=1;first2=2;
5628: for (k2=1; k2<=(nlstate);k2++){
5629: for (l2=1; l2<=(nlstate+ndeath);l2++){
5630: if(l2==k2) continue;
5631: j=(k2-1)*(nlstate+ndeath)+l2;
5632: for (k1=1; k1<=(nlstate);k1++){
5633: for (l1=1; l1<=(nlstate+ndeath);l1++){
5634: if(l1==k1) continue;
5635: i=(k1-1)*(nlstate+ndeath)+l1;
5636: if(i<=j) continue;
5637: for (age=bage; age<=fage; age ++){
5638: if ((int)age %5==0){
5639: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
5640: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
5641: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
5642: mu1=mu[i][(int) age]/stepm*YEARM ;
5643: mu2=mu[j][(int) age]/stepm*YEARM;
5644: c12=cv12/sqrt(v1*v2);
5645: /* Computing eigen value of matrix of covariance */
5646: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5647: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5648: if ((lc2 <0) || (lc1 <0) ){
5649: if(first2==1){
5650: first1=0;
5651: 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);
5652: }
5653: 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);
5654: /* lc1=fabs(lc1); */ /* If we want to have them positive */
5655: /* lc2=fabs(lc2); */
5656: }
1.220 brouard 5657:
1.222 brouard 5658: /* Eigen vectors */
5659: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
5660: /*v21=sqrt(1.-v11*v11); *//* error */
5661: v21=(lc1-v1)/cv12*v11;
5662: v12=-v21;
5663: v22=v11;
5664: tnalp=v21/v11;
5665: if(first1==1){
5666: first1=0;
5667: 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);
5668: }
5669: 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);
5670: /*printf(fignu*/
5671: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
5672: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
5673: if(first==1){
5674: first=0;
5675: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
5676: fprintf(ficgp,"\nset parametric;unset label");
5677: 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);
5678: fprintf(ficgp,"\nset ter svg size 640, 480");
5679: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 5680: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 5681: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 5682: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
5683: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5684: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5685: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
5686: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5687: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5688: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5689: 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", \
5690: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5691: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5692: }else{
5693: first=0;
5694: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
5695: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5696: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5697: 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", \
5698: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5699: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5700: }/* if first */
5701: } /* age mod 5 */
5702: } /* end loop age */
5703: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5704: first=1;
5705: } /*l12 */
5706: } /* k12 */
5707: } /*l1 */
5708: }/* k1 */
5709: } /* loop on combination of covariates j1 */
5710: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
5711: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
5712: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5713: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
5714: free_vector(xp,1,npar);
5715: fclose(ficresprob);
5716: fclose(ficresprobcov);
5717: fclose(ficresprobcor);
5718: fflush(ficgp);
5719: fflush(fichtmcov);
5720: }
1.126 brouard 5721:
5722:
5723: /******************* Printing html file ***********/
1.201 brouard 5724: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 5725: int lastpass, int stepm, int weightopt, char model[],\
5726: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 5727: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 5728: double jprev1, double mprev1,double anprev1, double dateprev1, \
5729: double jprev2, double mprev2,double anprev2, double dateprev2){
1.126 brouard 5730: int jj1, k1, i1, cpt;
5731:
5732: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
5733: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
5734: </ul>");
1.214 brouard 5735: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
5736: 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",
5737: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
5738: fprintf(fichtm,"<li> - Observed prevalence in each state (during the period defined between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf): <a href=\"%s\">%s</a> (html file) ",
1.213 brouard 5739: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
5740: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 5741: fprintf(fichtm,"\
5742: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 5743: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 5744: fprintf(fichtm,"\
1.217 brouard 5745: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
5746: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
5747: fprintf(fichtm,"\
1.126 brouard 5748: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5749: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 5750: fprintf(fichtm,"\
1.217 brouard 5751: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
5752: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
5753: fprintf(fichtm,"\
1.211 brouard 5754: - (a) Life expectancies by health status at initial age, e<sub>i.</sub> (b) health expectancies by health status at initial age, e<sub>ij</sub> . If one or more covariates are included, specific tables for each value of the covariate are output in sequences within the same file (estepm=%2d months): \
1.126 brouard 5755: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5756: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 5757: if(prevfcast==1){
5758: fprintf(fichtm,"\
5759: - Prevalence projections by age and states: \
1.201 brouard 5760: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 5761: }
1.126 brouard 5762:
1.222 brouard 5763: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
1.126 brouard 5764:
1.225 ! brouard 5765: m=pow(2,cptcoveff);
1.222 brouard 5766: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 5767:
1.222 brouard 5768: jj1=0;
5769: for(k1=1; k1<=m;k1++){
1.220 brouard 5770:
1.222 brouard 5771: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
5772: jj1++;
5773: if (cptcovn > 0) {
5774: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 ! brouard 5775: for (cpt=1; cpt<=cptcoveff;cpt++){
1.222 brouard 5776: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
5777: printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout);
5778: }
5779: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
5780: if(invalidvarcomb[k1]){
5781: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
5782: printf("\nCombination (%d) ignored because no cases \n",k1);
5783: continue;
5784: }
5785: }
5786: /* aij, bij */
5787: fprintf(fichtm,"<br>- Logit model (yours is: 1+age+%s), for example: logit(pij)=log(pij/pii)= aij+ bij age + V1 age + etc. as a function of age: <a href=\"%s_%d-1.svg\">%s_%d-1.svg</a><br> \
1.211 brouard 5788: <img src=\"%s_%d-1.svg\">",model,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5789: /* Pij */
5790: 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.svg\">%s_%d-2.svg</a><br> \
1.201 brouard 5791: <img src=\"%s_%d-2.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5792: /* Quasi-incidences */
5793: fprintf(fichtm,"<br>\n- I<sub>ij</sub> or Conditional probabilities to be observed in state j being in state i %d (stepm) months\
1.220 brouard 5794: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 5795: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
5796: divided by h: <sub>h</sub>P<sub>ij</sub>/h : <a href=\"%s_%d-3.svg\">%s_%d-3.svg</a><br> \
1.201 brouard 5797: <img src=\"%s_%d-3.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5798: /* Survival functions (period) in state j */
5799: for(cpt=1; cpt<=nlstate;cpt++){
5800: fprintf(fichtm,"<br>\n- Survival functions in state %d. Or probability to survive in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.201 brouard 5801: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1);
1.222 brouard 5802: }
5803: /* State specific survival functions (period) */
5804: for(cpt=1; cpt<=nlstate;cpt++){
5805: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.220 brouard 5806: Or probability to survive in various states (1 to %d) being in state %d at different ages. \
1.201 brouard 5807: <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> <img src=\"%s_%d-%d.svg\">", cpt, nlstate, cpt, subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1,subdirf2(optionfilefiname,"LIJT_"),cpt,jj1);
1.222 brouard 5808: }
5809: /* Period (stable) prevalence in each health state */
5810: for(cpt=1; cpt<=nlstate;cpt++){
5811: fprintf(fichtm,"<br>\n- Convergence to period (stable) prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a><br> \
1.201 brouard 5812: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1);
1.222 brouard 5813: }
5814: if(backcast==1){
5815: /* Period (stable) back prevalence in each health state */
5816: for(cpt=1; cpt<=nlstate;cpt++){
5817: fprintf(fichtm,"<br>\n- Convergence to period (stable) back prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s_%d-%d.svg\">%s_%d-%d.svg</a><br> \
1.217 brouard 5818: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1);
1.222 brouard 5819: }
1.217 brouard 5820: }
1.222 brouard 5821: if(prevfcast==1){
5822: /* Projection of prevalence up to period (stable) prevalence in each health state */
5823: for(cpt=1; cpt<=nlstate;cpt++){
5824: fprintf(fichtm,"<br>\n- Projection of cross-sectional prevalence (estimated with cases observed from %.1f to %.1f) up to period (stable) prevalence in state %d. Or probability to be in state %d being in state (1 to %d) at different ages. <a href=\"%s%d_%d.svg\">%s%d_%d.svg</a><br> \
1.213 brouard 5825: <img src=\"%s_%d-%d.svg\">", dateprev1, dateprev2, cpt, cpt, nlstate, subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1,subdirf2(optionfilefiname,"PROJ_"),cpt,jj1);
1.222 brouard 5826: }
5827: }
1.220 brouard 5828:
1.222 brouard 5829: for(cpt=1; cpt<=nlstate;cpt++) {
5830: 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.svg\">%s_%d%d.svg</a> <br> \
1.201 brouard 5831: <img src=\"%s_%d%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1);
1.222 brouard 5832: }
5833: /* } /\* end i1 *\/ */
5834: }/* End k1 */
5835: fprintf(fichtm,"</ul>");
1.126 brouard 5836:
1.222 brouard 5837: fprintf(fichtm,"\
1.126 brouard 5838: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 5839: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 5840: - 95%% confidence intervals and Wald tests of the estimated parameters are in the log file if optimization has been done (mle != 0).<br> \
1.197 brouard 5841: But because parameters are usually highly correlated (a higher incidence of disability \
5842: and a higher incidence of recovery can give very close observed transition) it might \
5843: be very useful to look not only at linear confidence intervals estimated from the \
5844: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
5845: (parameters) of the logistic regression, it might be more meaningful to visualize the \
5846: covariance matrix of the one-step probabilities. \
5847: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 5848:
1.222 brouard 5849: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
5850: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
5851: fprintf(fichtm,"\
1.126 brouard 5852: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 5853: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 5854:
1.222 brouard 5855: fprintf(fichtm,"\
1.126 brouard 5856: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 5857: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
5858: fprintf(fichtm,"\
1.126 brouard 5859: - 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): \
5860: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 5861: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 5862: fprintf(fichtm,"\
1.126 brouard 5863: - (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): \
5864: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 5865: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 5866: fprintf(fichtm,"\
1.128 brouard 5867: - Variances and covariances of health expectancies by age. Status (i) based health expectancies (in state j), e<sup>ij</sup> are weighted by the period prevalences in each state i (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a><br>\n",
1.222 brouard 5868: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
5869: fprintf(fichtm,"\
1.128 brouard 5870: - Total life expectancy and total health expectancies to be spent in each health state e<sup>.j</sup> with their standard errors (if popbased=1, an additional computation is done using the cross-sectional prevalences, i.e population based) (estepm=%d months): <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 5871: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
5872: fprintf(fichtm,"\
1.126 brouard 5873: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 5874: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 5875:
5876: /* if(popforecast==1) fprintf(fichtm,"\n */
5877: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
5878: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
5879: /* <br>",fileres,fileres,fileres,fileres); */
5880: /* else */
5881: /* fprintf(fichtm,"\n No population forecast: popforecast = %d (instead of 1) or stepm = %d (instead of 1) or model=%s (instead of .)<br><br></li>\n",popforecast, stepm, model); */
1.222 brouard 5882: fflush(fichtm);
5883: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 5884:
1.225 ! brouard 5885: m=pow(2,cptcoveff);
1.222 brouard 5886: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 5887:
1.222 brouard 5888: jj1=0;
5889: for(k1=1; k1<=m;k1++){
5890: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
5891: jj1++;
1.126 brouard 5892: if (cptcovn > 0) {
5893: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 ! brouard 5894: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.222 brouard 5895: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
1.126 brouard 5896: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5897:
1.222 brouard 5898: if(invalidvarcomb[k1]){
5899: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
5900: continue;
5901: }
1.126 brouard 5902: }
5903: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 brouard 5904: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
5905: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d.svg\"> %s_%d-%d.svg</a>\n <br>\
1.205 brouard 5906: <img src=\"%s_%d-%d.svg\">",cpt,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1,subdirf2(optionfilefiname,"V_"),cpt,jj1);
1.126 brouard 5907: }
5908: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 5909: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
5910: true period expectancies (those weighted with period prevalences are also\
5911: drawn in addition to the population based expectancies computed using\
1.218 brouard 5912: observed and cahotic prevalences: <a href=\"%s_%d.svg\">%s_%d.svg</a>\n<br>\
1.205 brouard 5913: <img src=\"%s_%d.svg\">",subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1);
1.222 brouard 5914: /* } /\* end i1 *\/ */
5915: }/* End k1 */
5916: fprintf(fichtm,"</ul>");
5917: fflush(fichtm);
1.126 brouard 5918: }
5919:
5920: /******************* Gnuplot file **************/
1.223 brouard 5921: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 5922:
5923: char dirfileres[132],optfileres[132];
1.223 brouard 5924: char gplotcondition[132];
1.164 brouard 5925: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
1.211 brouard 5926: int lv=0, vlv=0, kl=0;
1.130 brouard 5927: int ng=0;
1.201 brouard 5928: int vpopbased;
1.223 brouard 5929: int ioffset; /* variable offset for columns */
1.219 brouard 5930:
1.126 brouard 5931: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
5932: /* printf("Problem with file %s",optionfilegnuplot); */
5933: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
5934: /* } */
5935:
5936: /*#ifdef windows */
5937: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 5938: /*#endif */
1.225 ! brouard 5939: m=pow(2,cptcoveff);
1.126 brouard 5940:
1.202 brouard 5941: /* Contribution to likelihood */
5942: /* Plot the probability implied in the likelihood */
1.223 brouard 5943: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
5944: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
5945: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
5946: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 5947: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 5948: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
5949: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 5950: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
5951: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
5952: 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));
5953: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
5954: 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));
5955: for (i=1; i<= nlstate ; i ++) {
5956: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
5957: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
5958: 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);
5959: for (j=2; j<= nlstate+ndeath ; j ++) {
5960: 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);
5961: }
5962: fprintf(ficgp,";\nset out; unset ylabel;\n");
5963: }
5964: /* 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 */
5965: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
5966: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
5967: fprintf(ficgp,"\nset out;unset log\n");
5968: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 5969:
1.126 brouard 5970: strcpy(dirfileres,optionfilefiname);
5971: strcpy(optfileres,"vpl");
1.223 brouard 5972: /* 1eme*/
1.211 brouard 5973: for (cpt=1; cpt<= nlstate ; cpt ++) { /* For each live state */
1.220 brouard 5974: for (k1=1; k1<= m ; k1 ++) { /* For each valid combination of covariate */
1.211 brouard 5975: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
5976: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files ");
1.225 ! brouard 5977: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
! 5978: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
1.223 brouard 5979: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5980: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5981: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5982: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
5983: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
5984: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 5985: }
5986: fprintf(ficgp,"\n#\n");
1.223 brouard 5987: if(invalidvarcomb[k1]){
5988: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
5989: continue;
5990: }
1.211 brouard 5991:
1.223 brouard 5992: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1);
5993: fprintf(ficgp,"\n#set out \"V_%s_%d-%d.svg\" \n",optionfilefiname,cpt,k1);
5994: fprintf(ficgp,"set xlabel \"Age\" \n\
1.219 brouard 5995: set ylabel \"Probability\" \n \
5996: set ter svg size 640, 480\n \
1.201 brouard 5997: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"%%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1);
1.219 brouard 5998:
1.223 brouard 5999: for (i=1; i<= nlstate ; i ++) {
6000: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6001: else fprintf(ficgp," %%*lf (%%*lf)");
6002: }
6003: fprintf(ficgp,"\" t\"Period (stable) prevalence\" w l lt 0,\"%s\" every :::%d::%d u 1:($2+1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6004: for (i=1; i<= nlstate ; i ++) {
6005: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6006: else fprintf(ficgp," %%*lf (%%*lf)");
6007: }
6008: fprintf(ficgp,"\" t\"95%% CI\" w l lt 1,\"%s\" every :::%d::%d u 1:($2-1.96*$3) \"%%lf",subdirf2(fileresu,"VPL_"),k1-1,k1-1);
6009: for (i=1; i<= nlstate ; i ++) {
6010: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6011: else fprintf(ficgp," %%*lf (%%*lf)");
6012: }
6013: 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));
6014: if(backcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
6015: /* 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); */
6016: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1 */
1.225 ! brouard 6017: if(cptcoveff ==0){
1.223 brouard 6018: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line ", 2+(cpt-1), cpt );
6019: }else{
6020: kl=0;
1.225 ! brouard 6021: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
! 6022: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
1.223 brouard 6023: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6024: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6025: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6026: vlv= nbcode[Tvaraff[k]][lv];
6027: kl++;
6028: /* 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 *\/ */
6029: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6030: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6031: /* '' 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*/
1.225 ! brouard 6032: if(k==cptcoveff){
1.223 brouard 6033: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d : 1/0) t 'Backward prevalence in state %d' with line ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv], \
6034: 6+(cpt-1), cpt );
6035: }else{
6036: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
6037: kl++;
6038: }
6039: } /* end covariate */
6040: } /* end if no covariate */
6041: } /* end if backcast */
6042: fprintf(ficgp,"\nset out \n");
1.201 brouard 6043: } /* k1 */
6044: } /* cpt */
1.126 brouard 6045: /*2 eme*/
6046: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6047:
1.223 brouard 6048: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.225 ! brouard 6049: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
! 6050: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6051: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6052: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6053: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6054: vlv= nbcode[Tvaraff[k]][lv];
6055: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6056: }
6057: fprintf(ficgp,"\n#\n");
6058: if(invalidvarcomb[k1]){
6059: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6060: continue;
6061: }
1.219 brouard 6062:
1.223 brouard 6063: fprintf(ficgp,"\nset out \"%s_%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1);
6064: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6065: if(vpopbased==0)
6066: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
6067: else
6068: fprintf(ficgp,"\nreplot ");
6069: for (i=1; i<= nlstate+1 ; i ++) {
6070: k=2*i;
6071: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ?$4 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1, vpopbased);
6072: for (j=1; j<= nlstate+1 ; j ++) {
6073: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6074: else fprintf(ficgp," %%*lf (%%*lf)");
6075: }
6076: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
6077: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
6078: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4-$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1,vpopbased);
6079: for (j=1; j<= nlstate+1 ; j ++) {
6080: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6081: else fprintf(ficgp," %%*lf (%%*lf)");
6082: }
6083: fprintf(ficgp,"\" t\"\" w l lt 0,");
6084: fprintf(ficgp,"\"%s\" every :::%d::%d u 1:($2==%d && $4!=0 ? $4+$5*2 : 1/0) \"%%lf %%lf %%lf",subdirf2(fileresu,"T_"),k1-1,k1-1,vpopbased);
6085: for (j=1; j<= nlstate+1 ; j ++) {
6086: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6087: else fprintf(ficgp," %%*lf (%%*lf)");
6088: }
6089: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
6090: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
6091: } /* state */
6092: } /* vpopbased */
6093: fprintf(ficgp,"\nset out;set out \"%s_%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1); /* Buggy gnuplot */
1.201 brouard 6094: } /* k1 */
1.219 brouard 6095:
6096:
1.126 brouard 6097: /*3eme*/
6098: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6099:
1.126 brouard 6100: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.211 brouard 6101: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: cov=%d state=%d",k1, cpt);
1.225 ! brouard 6102: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
! 6103: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6104: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6105: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6106: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6107: vlv= nbcode[Tvaraff[k]][lv];
6108: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6109: }
6110: fprintf(ficgp,"\n#\n");
1.223 brouard 6111: if(invalidvarcomb[k1]){
6112: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6113: continue;
6114: }
1.219 brouard 6115:
1.126 brouard 6116: /* k=2+nlstate*(2*cpt-2); */
6117: k=2+(nlstate+1)*(cpt-1);
1.201 brouard 6118: fprintf(ficgp,"\nset out \"%s_%d%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1);
1.199 brouard 6119: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 6120: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:%d t \"e%d1\" w l",ageminpar,fage,subdirf2(fileresu,"E_"),k1-1,k1-1,k,cpt);
1.126 brouard 6121: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
1.223 brouard 6122: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6123: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
6124: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6125: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6126: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 6127:
1.126 brouard 6128: */
6129: for (i=1; i< nlstate ; i ++) {
1.223 brouard 6130: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+i,cpt,i+1);
6131: /* fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d%d\" w l",subdirf2(fileres,"e"),k1-1,k1-1,k+2*i,cpt,i+1);*/
1.219 brouard 6132:
1.126 brouard 6133: }
1.201 brouard 6134: fprintf(ficgp," ,\"%s\" every :::%d::%d u 1:%d t \"e%d.\" w l",subdirf2(fileresu,"E_"),k1-1,k1-1,k+nlstate,cpt);
1.126 brouard 6135: }
6136: }
6137:
1.223 brouard 6138: /* 4eme */
1.201 brouard 6139: /* Survival functions (period) from state i in state j by initial state i */
6140: for (k1=1; k1<= m ; k1 ++) { /* For each multivariate if any */
1.220 brouard 6141:
1.201 brouard 6142: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.211 brouard 6143: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
1.225 ! brouard 6144: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
! 6145: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6146: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6147: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6148: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6149: vlv= nbcode[Tvaraff[k]][lv];
6150: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6151: }
6152: fprintf(ficgp,"\n#\n");
1.223 brouard 6153: if(invalidvarcomb[k1]){
6154: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6155: continue;
6156: }
1.220 brouard 6157:
1.201 brouard 6158: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1);
6159: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.220 brouard 6160: set ter svg size 640, 480\n \
6161: unset log y\n \
1.201 brouard 6162: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6163: k=3;
1.201 brouard 6164: for (i=1; i<= nlstate ; i ++){
1.223 brouard 6165: if(i==1){
6166: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6167: }else{
6168: fprintf(ficgp,", '' ");
6169: }
6170: l=(nlstate+ndeath)*(i-1)+1;
6171: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6172: for (j=2; j<= nlstate+ndeath ; j ++)
6173: fprintf(ficgp,"+$%d",k+l+j-1);
6174: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
1.201 brouard 6175: } /* nlstate */
6176: fprintf(ficgp,"\nset out\n");
6177: } /* end cpt state*/
6178: } /* end covariate */
1.220 brouard 6179:
6180: /* 5eme */
1.201 brouard 6181: /* Survival functions (period) from state i in state j by final state j */
1.202 brouard 6182: for (k1=1; k1<= m ; k1 ++) { /* For each covariate if any */
1.201 brouard 6183: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.223 brouard 6184:
1.201 brouard 6185: 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);
1.225 ! brouard 6186: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
! 6187: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.220 brouard 6188: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6189: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6190: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6191: vlv= nbcode[Tvaraff[k]][lv];
6192: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6193: }
6194: fprintf(ficgp,"\n#\n");
1.223 brouard 6195: if(invalidvarcomb[k1]){
6196: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6197: continue;
6198: }
1.220 brouard 6199:
1.201 brouard 6200: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1);
6201: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.220 brouard 6202: set ter svg size 640, 480\n \
6203: unset log y\n \
1.201 brouard 6204: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6205: k=3;
1.201 brouard 6206: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.220 brouard 6207: if(j==1)
6208: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6209: else
6210: fprintf(ficgp,", '' ");
6211: l=(nlstate+ndeath)*(cpt-1) +j;
6212: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
6213: /* for (i=2; i<= nlstate+ndeath ; i ++) */
6214: /* fprintf(ficgp,"+$%d",k+l+i-1); */
6215: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
1.201 brouard 6216: } /* nlstate */
6217: fprintf(ficgp,", '' ");
6218: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
6219: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.220 brouard 6220: l=(nlstate+ndeath)*(cpt-1) +j;
6221: if(j < nlstate)
6222: fprintf(ficgp,"$%d +",k+l);
6223: else
6224: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
1.201 brouard 6225: }
6226: fprintf(ficgp,"\nset out\n");
6227: } /* end cpt state*/
6228: } /* end covariate */
1.220 brouard 6229:
6230: /* 6eme */
1.202 brouard 6231: /* CV preval stable (period) for each covariate */
1.211 brouard 6232: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.153 brouard 6233: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.223 brouard 6234:
1.211 brouard 6235: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 ! brouard 6236: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
! 6237: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.220 brouard 6238: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6239: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6240: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6241: vlv= nbcode[Tvaraff[k]][lv];
6242: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6243: }
6244: fprintf(ficgp,"\n#\n");
1.223 brouard 6245: if(invalidvarcomb[k1]){
6246: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6247: continue;
6248: }
6249:
1.201 brouard 6250: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1);
1.126 brouard 6251: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.223 brouard 6252: set ter svg size 640, 480\n \
6253: unset log y\n \
1.153 brouard 6254: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6255: k=3; /* Offset */
1.153 brouard 6256: for (i=1; i<= nlstate ; i ++){
1.220 brouard 6257: if(i==1)
6258: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6259: else
6260: fprintf(ficgp,", '' ");
6261: l=(nlstate+ndeath)*(i-1)+1;
6262: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6263: for (j=2; j<= nlstate ; j ++)
6264: fprintf(ficgp,"+$%d",k+l+j-1);
6265: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 6266: } /* nlstate */
1.201 brouard 6267: fprintf(ficgp,"\nset out\n");
1.153 brouard 6268: } /* end cpt state*/
6269: } /* end covariate */
1.223 brouard 6270:
6271:
1.220 brouard 6272: /* 7eme */
1.218 brouard 6273: if(backcast == 1){
1.217 brouard 6274: /* CV back preval stable (period) for each covariate */
1.218 brouard 6275: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
6276: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.220 brouard 6277: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 ! brouard 6278: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
! 6279: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.220 brouard 6280: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6281: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 6282: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.220 brouard 6283: vlv= nbcode[Tvaraff[k]][lv];
6284: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6285: }
6286: fprintf(ficgp,"\n#\n");
6287: if(invalidvarcomb[k1]){
1.223 brouard 6288: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6289: continue;
1.220 brouard 6290: }
6291:
6292: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1);
6293: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
6294: set ter svg size 640, 480\n \
6295: unset log y\n \
1.218 brouard 6296: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.220 brouard 6297: k=3; /* Offset */
6298: for (i=1; i<= nlstate ; i ++){
6299: if(i==1)
6300: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
6301: else
6302: fprintf(ficgp,", '' ");
6303: /* l=(nlstate+ndeath)*(i-1)+1; */
6304: l=(nlstate+ndeath)*(cpt-1)+1;
6305: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
6306: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
6307: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
6308: /* for (j=2; j<= nlstate ; j ++) */
6309: /* fprintf(ficgp,"+$%d",k+l+j-1); */
6310: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
6311: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
6312: } /* nlstate */
6313: fprintf(ficgp,"\nset out\n");
1.218 brouard 6314: } /* end cpt state*/
6315: } /* end covariate */
6316: } /* End if backcast */
6317:
1.223 brouard 6318: /* 8eme */
1.218 brouard 6319: if(prevfcast==1){
6320: /* Projection from cross-sectional to stable (period) for each covariate */
6321:
6322: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.211 brouard 6323: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.219 brouard 6324: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 ! brouard 6325: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
! 6326: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
1.219 brouard 6327: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6328: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6329: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6330: vlv= nbcode[Tvaraff[k]][lv];
1.220 brouard 6331: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.219 brouard 6332: }
6333: fprintf(ficgp,"\n#\n");
1.220 brouard 6334: if(invalidvarcomb[k1]){
1.223 brouard 6335: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6336: continue;
1.220 brouard 6337: }
1.219 brouard 6338:
6339: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
6340: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1);
6341: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.223 brouard 6342: set ter svg size 640, 480\n \
6343: unset log y\n \
1.219 brouard 6344: plot [%.f:%.f] ", ageminpar, agemaxpar);
6345: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
6346: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6347: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6348: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6349: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6350: if(i==1){
6351: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
6352: }else{
6353: fprintf(ficgp,",\\\n '' ");
6354: }
1.225 ! brouard 6355: if(cptcoveff ==0){ /* No covariate */
1.219 brouard 6356: ioffset=2; /* Age is in 2 */
6357: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6358: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6359: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6360: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6361: fprintf(ficgp," u %d:(", ioffset);
6362: if(i==nlstate+1)
6363: fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ", \
6364: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6365: else
6366: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
6367: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6368: }else{ /* more than 2 covariates */
1.225 ! brouard 6369: if(cptcoveff ==1){
1.219 brouard 6370: ioffset=4; /* Age is in 4 */
6371: }else{
6372: ioffset=6; /* Age is in 6 */
1.223 brouard 6373: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6374: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.219 brouard 6375: }
1.220 brouard 6376: fprintf(ficgp," u %d:(",ioffset);
1.219 brouard 6377: kl=0;
1.220 brouard 6378: strcpy(gplotcondition,"(");
1.225 ! brouard 6379: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
! 6380: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
1.219 brouard 6381: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6382: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6383: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.220 brouard 6384: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
6385: kl++;
6386: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
1.219 brouard 6387: kl++;
1.225 ! brouard 6388: if(k <cptcoveff && cptcoveff>1)
1.220 brouard 6389: sprintf(gplotcondition+strlen(gplotcondition)," && ");
6390: }
6391: strcpy(gplotcondition+strlen(gplotcondition),")");
6392: /* 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 *\/ */
6393: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6394: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6395: /* '' 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*/
6396: if(i==nlstate+1){
6397: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \
6398: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6399: }else{
1.223 brouard 6400: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
6401: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
1.220 brouard 6402: }
1.219 brouard 6403: } /* end if covariate */
6404: } /* nlstate */
6405: fprintf(ficgp,"\nset out\n");
1.223 brouard 6406: } /* end cpt state*/
6407: } /* end covariate */
6408: } /* End if prevfcast */
1.219 brouard 6409:
1.211 brouard 6410:
1.223 brouard 6411: /* proba elementaires */
6412: fprintf(ficgp,"\n##############\n#MLE estimated parameters\n#############\n");
1.126 brouard 6413: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6414: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6415: for(k=1; k <=(nlstate+ndeath); k++){
6416: if (k != i) {
1.223 brouard 6417: fprintf(ficgp,"# current state %d\n",k);
6418: for(j=1; j <=ncovmodel; j++){
6419: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
6420: jk++;
6421: }
6422: fprintf(ficgp,"\n");
1.126 brouard 6423: }
6424: }
1.223 brouard 6425: }
1.187 brouard 6426: fprintf(ficgp,"##############\n#\n");
1.223 brouard 6427:
1.145 brouard 6428: /*goto avoid;*/
1.200 brouard 6429: fprintf(ficgp,"\n##############\n#Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6430: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6431: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6432: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6433: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6434: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6435: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6436: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6437: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6438: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6439: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6440: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6441: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6442: fprintf(ficgp,"#\n");
1.223 brouard 6443: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
6444: fprintf(ficgp,"# ng=%d\n",ng);
1.225 ! brouard 6445: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);
1.223 brouard 6446: for(jk=1; jk <=m; jk++) {
6447: fprintf(ficgp,"# jk=%d\n",jk);
6448: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng);
6449: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6450: if (ng==1){
6451: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6452: fprintf(ficgp,"\nunset log y");
6453: }else if (ng==2){
6454: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6455: fprintf(ficgp,"\nset log y");
6456: }else if (ng==3){
6457: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
6458: fprintf(ficgp,"\nset log y");
6459: }else
6460: fprintf(ficgp,"\nunset title ");
6461: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
6462: i=1;
6463: for(k2=1; k2<=nlstate; k2++) {
6464: k3=i;
6465: for(k=1; k<=(nlstate+ndeath); k++) {
6466: if (k != k2){
6467: switch( ng) {
6468: case 1:
6469: if(nagesqr==0)
6470: fprintf(ficgp," p%d+p%d*x",i,i+1);
6471: else /* nagesqr =1 */
6472: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6473: break;
6474: case 2: /* ng=2 */
6475: if(nagesqr==0)
6476: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6477: else /* nagesqr =1 */
6478: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6479: break;
6480: case 3:
6481: if(nagesqr==0)
6482: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6483: else /* nagesqr =1 */
6484: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6485: break;
6486: }
6487: ij=1;/* To be checked else nbcode[0][0] wrong */
6488: for(j=3; j <=ncovmodel-nagesqr; j++) {
6489: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
6490: if(ij <=cptcovage) { /* Bug valgrind */
6491: if((j-2)==Tage[ij]) { /* Bug valgrind */
6492: fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6493: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6494: ij++;
6495: }
6496: }
6497: else
1.225 ! brouard 6498: fprintf(ficgp,"+p%d*%d",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]); /* Valgrind bug nbcode */
1.223 brouard 6499: }
6500: }else{
6501: i=i-ncovmodel;
6502: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
6503: fprintf(ficgp," (1.");
6504: }
1.217 brouard 6505:
1.223 brouard 6506: if(ng != 1){
6507: fprintf(ficgp,")/(1");
1.126 brouard 6508:
1.223 brouard 6509: for(k1=1; k1 <=nlstate; k1++){
6510: if(nagesqr==0)
6511: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
6512: else /* nagesqr =1 */
6513: fprintf(ficgp,"+exp(p%d+p%d*x+p%d*x*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1,k3+(k1-1)*ncovmodel+1+nagesqr);
1.217 brouard 6514:
1.223 brouard 6515: ij=1;
6516: for(j=3; j <=ncovmodel-nagesqr; j++){
6517: if(ij <=cptcovage) { /* Bug valgrind */
6518: if((j-2)==Tage[ij]) { /* Bug valgrind */
6519: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6520: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6521: ij++;
6522: }
6523: }
6524: else
1.225 ! brouard 6525: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);/* Valgrind bug nbcode */
1.223 brouard 6526: }
6527: fprintf(ficgp,")");
6528: }
6529: fprintf(ficgp,")");
6530: if(ng ==2)
6531: fprintf(ficgp," t \"p%d%d\" ", k2,k);
6532: else /* ng= 3 */
6533: fprintf(ficgp," t \"i%d%d\" ", k2,k);
6534: }else{ /* end ng <> 1 */
6535: if( k !=k2) /* logit p11 is hard to draw */
6536: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
6537: }
6538: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
6539: fprintf(ficgp,",");
6540: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
6541: fprintf(ficgp,",");
6542: i=i+ncovmodel;
6543: } /* end k */
6544: } /* end k2 */
6545: fprintf(ficgp,"\n set out\n");
6546: } /* end jk */
6547: } /* end ng */
6548: /* avoid: */
6549: fflush(ficgp);
1.126 brouard 6550: } /* end gnuplot */
6551:
6552:
6553: /*************** Moving average **************/
1.219 brouard 6554: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 6555: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 6556:
1.222 brouard 6557: int i, cpt, cptcod;
6558: int modcovmax =1;
6559: int mobilavrange, mob;
6560: int iage=0;
6561:
6562: double sum=0.;
6563: double age;
6564: double *sumnewp, *sumnewm;
6565: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
6566:
6567:
1.225 ! brouard 6568: /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose */
1.222 brouard 6569: /* a covariate has 2 modalities, should be equal to ncovcombmax *\/ */
6570:
6571: sumnewp = vector(1,ncovcombmax);
6572: sumnewm = vector(1,ncovcombmax);
6573: agemingood = vector(1,ncovcombmax);
6574: agemaxgood = vector(1,ncovcombmax);
6575:
6576: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6577: sumnewm[cptcod]=0.;
6578: sumnewp[cptcod]=0.;
6579: agemingood[cptcod]=0;
6580: agemaxgood[cptcod]=0;
6581: }
6582: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
6583:
6584: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
6585: if(mobilav==1) mobilavrange=5; /* default */
6586: else mobilavrange=mobilav;
6587: for (age=bage; age<=fage; age++)
6588: for (i=1; i<=nlstate;i++)
6589: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
6590: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
6591: /* We keep the original values on the extreme ages bage, fage and for
6592: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
6593: we use a 5 terms etc. until the borders are no more concerned.
6594: */
6595: for (mob=3;mob <=mobilavrange;mob=mob+2){
6596: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
6597: for (i=1; i<=nlstate;i++){
6598: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6599: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
6600: for (cpt=1;cpt<=(mob-1)/2;cpt++){
6601: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
6602: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
6603: }
6604: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
6605: }
6606: }
6607: }/* end age */
6608: }/* end mob */
6609: }else
6610: return -1;
6611: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6612: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
6613: if(invalidvarcomb[cptcod]){
6614: printf("\nCombination (%d) ignored because no cases \n",cptcod);
6615: continue;
6616: }
1.219 brouard 6617:
1.222 brouard 6618: agemingood[cptcod]=fage-(mob-1)/2;
6619: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
6620: sumnewm[cptcod]=0.;
6621: for (i=1; i<=nlstate;i++){
6622: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6623: }
6624: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6625: agemingood[cptcod]=age;
6626: }else{ /* bad */
6627: for (i=1; i<=nlstate;i++){
6628: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6629: } /* i */
6630: } /* end bad */
6631: }/* age */
6632: sum=0.;
6633: for (i=1; i<=nlstate;i++){
6634: sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6635: }
6636: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6637: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any descending age!\n",cptcod);
6638: /* for (i=1; i<=nlstate;i++){ */
6639: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6640: /* } /\* i *\/ */
6641: } /* end bad */
6642: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
6643: /* From youngest, finding the oldest wrong */
6644: agemaxgood[cptcod]=bage+(mob-1)/2;
6645: for (age=bage+(mob-1)/2; age<=fage; age++){
6646: sumnewm[cptcod]=0.;
6647: for (i=1; i<=nlstate;i++){
6648: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6649: }
6650: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6651: agemaxgood[cptcod]=age;
6652: }else{ /* bad */
6653: for (i=1; i<=nlstate;i++){
6654: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6655: } /* i */
6656: } /* end bad */
6657: }/* age */
6658: sum=0.;
6659: for (i=1; i<=nlstate;i++){
6660: sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6661: }
6662: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6663: printf("For this combination of covariate cptcod=%d, we can't get a smoothed prevalence which sums to one at any ascending age!\n",cptcod);
6664: /* for (i=1; i<=nlstate;i++){ */
6665: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6666: /* } /\* i *\/ */
6667: } /* end bad */
6668:
6669: for (age=bage; age<=fage; age++){
6670: printf("%d %d ", cptcod, (int)age);
6671: sumnewp[cptcod]=0.;
6672: sumnewm[cptcod]=0.;
6673: for (i=1; i<=nlstate;i++){
6674: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
6675: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6676: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
6677: }
6678: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
6679: }
6680: /* printf("\n"); */
6681: /* } */
6682: /* brutal averaging */
6683: for (i=1; i<=nlstate;i++){
6684: for (age=1; age<=bage; age++){
6685: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6686: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6687: }
6688: for (age=fage; age<=AGESUP; age++){
6689: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6690: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6691: }
6692: } /* end i status */
6693: for (i=nlstate+1; i<=nlstate+ndeath;i++){
6694: for (age=1; age<=AGESUP; age++){
6695: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
6696: mobaverage[(int)age][i][cptcod]=0.;
6697: }
6698: }
6699: }/* end cptcod */
6700: free_vector(sumnewm,1, ncovcombmax);
6701: free_vector(sumnewp,1, ncovcombmax);
6702: free_vector(agemaxgood,1, ncovcombmax);
6703: free_vector(agemingood,1, ncovcombmax);
6704: return 0;
6705: }/* End movingaverage */
1.218 brouard 6706:
1.126 brouard 6707:
6708: /************** Forecasting ******************/
1.225 ! brouard 6709: void prevforecast(char fileres[], double anproj1, double mproj1, double jproj1, double ageminpar, double agemax, double dateprev1, double dateprev2, int mobilav, double bage, double fage, int firstpass, int lastpass, double anproj2, double p[], int cptcoveff){
1.126 brouard 6710: /* proj1, year, month, day of starting projection
6711: agemin, agemax range of age
6712: dateprev1 dateprev2 range of dates during which prevalence is computed
6713: anproj2 year of en of projection (same day and month as proj1).
6714: */
1.164 brouard 6715: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1;
1.126 brouard 6716: double agec; /* generic age */
6717: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
6718: double *popeffectif,*popcount;
6719: double ***p3mat;
1.218 brouard 6720: /* double ***mobaverage; */
1.126 brouard 6721: char fileresf[FILENAMELENGTH];
6722:
6723: agelim=AGESUP;
1.211 brouard 6724: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
6725: in each health status at the date of interview (if between dateprev1 and dateprev2).
6726: We still use firstpass and lastpass as another selection.
6727: */
1.214 brouard 6728: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
6729: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 6730:
1.201 brouard 6731: strcpy(fileresf,"F_");
6732: strcat(fileresf,fileresu);
1.126 brouard 6733: if((ficresf=fopen(fileresf,"w"))==NULL) {
6734: printf("Problem with forecast resultfile: %s\n", fileresf);
6735: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
6736: }
1.215 brouard 6737: printf("Computing forecasting: result on file '%s', please wait... \n", fileresf);
6738: fprintf(ficlog,"Computing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 6739:
1.225 ! brouard 6740: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 6741:
6742:
6743: stepsize=(int) (stepm+YEARM-1)/YEARM;
6744: if (stepm<=12) stepsize=1;
6745: if(estepm < stepm){
6746: printf ("Problem %d lower than %d\n",estepm, stepm);
6747: }
6748: else hstepm=estepm;
6749:
6750: hstepm=hstepm/stepm;
6751: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
6752: fractional in yp1 */
6753: anprojmean=yp;
6754: yp2=modf((yp1*12),&yp);
6755: mprojmean=yp;
6756: yp1=modf((yp2*30.5),&yp);
6757: jprojmean=yp;
6758: if(jprojmean==0) jprojmean=1;
6759: if(mprojmean==0) jprojmean=1;
6760:
1.225 ! brouard 6761: i1=cptcoveff;
1.126 brouard 6762: if (cptcovn < 1){i1=1;}
6763:
6764: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
6765:
6766: fprintf(ficresf,"#****** Routine prevforecast **\n");
6767:
6768: /* if (h==(int)(YEARM*yearp)){ */
6769: for(cptcov=1, k=0;cptcov<=i1;cptcov++){
1.225 ! brouard 6770: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
1.126 brouard 6771: k=k+1;
1.211 brouard 6772: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
1.225 ! brouard 6773: for(j=1;j<=cptcoveff;j++) {
1.219 brouard 6774: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.126 brouard 6775: }
1.211 brouard 6776: fprintf(ficresf," yearproj age");
1.126 brouard 6777: for(j=1; j<=nlstate+ndeath;j++){
1.219 brouard 6778: for(i=1; i<=nlstate;i++)
1.126 brouard 6779: fprintf(ficresf," p%d%d",i,j);
1.219 brouard 6780: fprintf(ficresf," wp.%d",j);
1.126 brouard 6781: }
1.217 brouard 6782: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
1.219 brouard 6783: fprintf(ficresf,"\n");
6784: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
6785: for (agec=fage; agec>=(ageminpar-1); agec--){
6786: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
6787: nhstepm = nhstepm/hstepm;
6788: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6789: oldm=oldms;savm=savms;
6790: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
6791:
6792: for (h=0; h<=nhstepm; h++){
6793: if (h*hstepm/YEARM*stepm ==yearp) {
1.126 brouard 6794: fprintf(ficresf,"\n");
1.225 ! brouard 6795: for(j=1;j<=cptcoveff;j++)
1.198 brouard 6796: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 6797: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
6798: }
6799: for(j=1; j<=nlstate+ndeath;j++) {
6800: ppij=0.;
6801: for(i=1; i<=nlstate;i++) {
6802: if (mobilav==1)
6803: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
6804: else {
6805: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
6806: }
6807: if (h*hstepm/YEARM*stepm== yearp) {
6808: fprintf(ficresf," %.3f", p3mat[i][j][h]);
6809: }
6810: } /* end i */
6811: if (h*hstepm/YEARM*stepm==yearp) {
6812: fprintf(ficresf," %.3f", ppij);
6813: }
6814: }/* end j */
6815: } /* end h */
6816: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6817: } /* end agec */
1.126 brouard 6818: } /* end yearp */
6819: } /* end cptcod */
6820: } /* end cptcov */
1.219 brouard 6821:
1.126 brouard 6822: fclose(ficresf);
1.215 brouard 6823: printf("End of Computing forecasting \n");
6824: fprintf(ficlog,"End of Computing forecasting\n");
6825:
1.126 brouard 6826: }
6827:
1.218 brouard 6828: /* /\************** Back Forecasting ******************\/ */
1.225 ! brouard 6829: /* void prevbackforecast(char fileres[], 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){ */
1.218 brouard 6830: /* /\* back1, year, month, day of starting backection */
6831: /* agemin, agemax range of age */
6832: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
6833: /* anback2 year of en of backection (same day and month as back1). */
6834: /* *\/ */
6835: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
6836: /* double agec; /\* generic age *\/ */
6837: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
6838: /* double *popeffectif,*popcount; */
6839: /* double ***p3mat; */
6840: /* /\* double ***mobaverage; *\/ */
6841: /* char fileresfb[FILENAMELENGTH]; */
6842:
6843: /* agelim=AGESUP; */
6844: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
6845: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
6846: /* We still use firstpass and lastpass as another selection. */
6847: /* *\/ */
6848: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
6849: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
6850: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
6851:
6852: /* strcpy(fileresfb,"FB_"); */
6853: /* strcat(fileresfb,fileresu); */
6854: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
6855: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
6856: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
6857: /* } */
6858: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
6859: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
6860:
1.225 ! brouard 6861: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.218 brouard 6862:
6863: /* /\* if (mobilav!=0) { *\/ */
6864: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
6865: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
6866: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
6867: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
6868: /* /\* } *\/ */
6869: /* /\* } *\/ */
6870:
6871: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
6872: /* if (stepm<=12) stepsize=1; */
6873: /* if(estepm < stepm){ */
6874: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
6875: /* } */
6876: /* else hstepm=estepm; */
6877:
6878: /* hstepm=hstepm/stepm; */
6879: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
6880: /* fractional in yp1 *\/ */
6881: /* anprojmean=yp; */
6882: /* yp2=modf((yp1*12),&yp); */
6883: /* mprojmean=yp; */
6884: /* yp1=modf((yp2*30.5),&yp); */
6885: /* jprojmean=yp; */
6886: /* if(jprojmean==0) jprojmean=1; */
6887: /* if(mprojmean==0) jprojmean=1; */
6888:
1.225 ! brouard 6889: /* i1=cptcoveff; */
1.218 brouard 6890: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 6891:
1.218 brouard 6892: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 6893:
1.218 brouard 6894: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
6895:
6896: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
6897: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
1.225 ! brouard 6898: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
1.218 brouard 6899: /* k=k+1; */
6900: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
1.225 ! brouard 6901: /* for(j=1;j<=cptcoveff;j++) { */
1.218 brouard 6902: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
6903: /* } */
6904: /* fprintf(ficresfb," yearbproj age"); */
6905: /* for(j=1; j<=nlstate+ndeath;j++){ */
6906: /* for(i=1; i<=nlstate;i++) */
6907: /* fprintf(ficresfb," p%d%d",i,j); */
6908: /* fprintf(ficresfb," p.%d",j); */
6909: /* } */
6910: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
6911: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
6912: /* fprintf(ficresfb,"\n"); */
6913: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
6914: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
6915: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
6916: /* nhstepm = nhstepm/hstepm; */
6917: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
6918: /* oldm=oldms;savm=savms; */
6919: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
6920: /* for (h=0; h<=nhstepm; h++){ */
6921: /* if (h*hstepm/YEARM*stepm ==yearp) { */
6922: /* fprintf(ficresfb,"\n"); */
1.225 ! brouard 6923: /* for(j=1;j<=cptcoveff;j++) */
1.218 brouard 6924: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
6925: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
6926: /* } */
6927: /* for(j=1; j<=nlstate+ndeath;j++) { */
6928: /* ppij=0.; */
6929: /* for(i=1; i<=nlstate;i++) { */
6930: /* if (mobilav==1) */
6931: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
6932: /* else { */
6933: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
6934: /* } */
6935: /* if (h*hstepm/YEARM*stepm== yearp) { */
6936: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
6937: /* } */
6938: /* } /\* end i *\/ */
6939: /* if (h*hstepm/YEARM*stepm==yearp) { */
6940: /* fprintf(ficresfb," %.3f", ppij); */
6941: /* } */
6942: /* }/\* end j *\/ */
6943: /* } /\* end h *\/ */
6944: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
6945: /* } /\* end agec *\/ */
6946: /* } /\* end yearp *\/ */
6947: /* } /\* end cptcod *\/ */
6948: /* } /\* end cptcov *\/ */
6949:
6950: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
6951:
6952: /* fclose(ficresfb); */
6953: /* printf("End of Computing Back forecasting \n"); */
6954: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 6955:
1.218 brouard 6956: /* } */
1.217 brouard 6957:
1.126 brouard 6958: /************** Forecasting *****not tested NB*************/
1.169 brouard 6959: void populforecast(char fileres[], double anpyram,double mpyram,double jpyram,double ageminpar, double agemax,double dateprev1, double dateprev2, int mobilav, double agedeb, double fage, int popforecast, char popfile[], double anpyram1,double p[], int i2){
1.126 brouard 6960:
6961: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
6962: int *popage;
6963: double calagedatem, agelim, kk1, kk2;
6964: double *popeffectif,*popcount;
6965: double ***p3mat,***tabpop,***tabpopprev;
1.218 brouard 6966: /* double ***mobaverage; */
1.126 brouard 6967: char filerespop[FILENAMELENGTH];
6968:
6969: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6970: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6971: agelim=AGESUP;
6972: calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
6973:
6974: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
6975:
6976:
1.201 brouard 6977: strcpy(filerespop,"POP_");
6978: strcat(filerespop,fileresu);
1.126 brouard 6979: if((ficrespop=fopen(filerespop,"w"))==NULL) {
6980: printf("Problem with forecast resultfile: %s\n", filerespop);
6981: fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
6982: }
6983: printf("Computing forecasting: result on file '%s' \n", filerespop);
6984: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
6985:
1.225 ! brouard 6986: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 6987:
1.218 brouard 6988: /* if (mobilav!=0) { */
6989: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
6990: /* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ */
6991: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
6992: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
6993: /* } */
6994: /* } */
1.126 brouard 6995:
6996: stepsize=(int) (stepm+YEARM-1)/YEARM;
6997: if (stepm<=12) stepsize=1;
6998:
6999: agelim=AGESUP;
7000:
7001: hstepm=1;
7002: hstepm=hstepm/stepm;
1.218 brouard 7003:
1.126 brouard 7004: if (popforecast==1) {
7005: if((ficpop=fopen(popfile,"r"))==NULL) {
7006: printf("Problem with population file : %s\n",popfile);exit(0);
7007: fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
7008: }
7009: popage=ivector(0,AGESUP);
7010: popeffectif=vector(0,AGESUP);
7011: popcount=vector(0,AGESUP);
7012:
7013: i=1;
7014: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
1.218 brouard 7015:
1.126 brouard 7016: imx=i;
7017: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
7018: }
1.218 brouard 7019:
1.126 brouard 7020: for(cptcov=1,k=0;cptcov<=i2;cptcov++){
1.225 ! brouard 7021: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
1.126 brouard 7022: k=k+1;
7023: fprintf(ficrespop,"\n#******");
1.225 ! brouard 7024: for(j=1;j<=cptcoveff;j++) {
1.198 brouard 7025: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.126 brouard 7026: }
7027: fprintf(ficrespop,"******\n");
7028: fprintf(ficrespop,"# Age");
7029: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
7030: if (popforecast==1) fprintf(ficrespop," [Population]");
7031:
7032: for (cpt=0; cpt<=0;cpt++) {
7033: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
7034:
1.218 brouard 7035: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
1.126 brouard 7036: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
7037: nhstepm = nhstepm/hstepm;
7038:
7039: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7040: oldm=oldms;savm=savms;
7041: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 brouard 7042:
1.126 brouard 7043: for (h=0; h<=nhstepm; h++){
7044: if (h==(int) (calagedatem+YEARM*cpt)) {
7045: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
7046: }
7047: for(j=1; j<=nlstate+ndeath;j++) {
7048: kk1=0.;kk2=0;
7049: for(i=1; i<=nlstate;i++) {
7050: if (mobilav==1)
7051: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
7052: else {
7053: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
7054: }
7055: }
7056: if (h==(int)(calagedatem+12*cpt)){
7057: tabpop[(int)(agedeb)][j][cptcod]=kk1;
1.218 brouard 7058: /*fprintf(ficrespop," %.3f", kk1);
7059: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
1.126 brouard 7060: }
7061: }
7062: for(i=1; i<=nlstate;i++){
7063: kk1=0.;
1.218 brouard 7064: for(j=1; j<=nlstate;j++){
7065: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
7066: }
7067: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)];
1.126 brouard 7068: }
1.218 brouard 7069:
7070: if (h==(int)(calagedatem+12*cpt))
7071: for(j=1; j<=nlstate;j++)
7072: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
1.126 brouard 7073: }
7074: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7075: }
7076: }
1.218 brouard 7077:
7078: /******/
7079:
1.126 brouard 7080: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
7081: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
7082: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
7083: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
7084: nhstepm = nhstepm/hstepm;
7085:
7086: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7087: oldm=oldms;savm=savms;
7088: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
7089: for (h=0; h<=nhstepm; h++){
7090: if (h==(int) (calagedatem+YEARM*cpt)) {
7091: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
7092: }
7093: for(j=1; j<=nlstate+ndeath;j++) {
7094: kk1=0.;kk2=0;
7095: for(i=1; i<=nlstate;i++) {
7096: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
7097: }
7098: if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1);
7099: }
7100: }
7101: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7102: }
7103: }
1.218 brouard 7104: }
1.126 brouard 7105: }
1.218 brouard 7106:
7107: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7108:
1.126 brouard 7109: if (popforecast==1) {
7110: free_ivector(popage,0,AGESUP);
7111: free_vector(popeffectif,0,AGESUP);
7112: free_vector(popcount,0,AGESUP);
7113: }
7114: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
7115: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
7116: fclose(ficrespop);
7117: } /* End of popforecast */
1.218 brouard 7118:
1.126 brouard 7119: int fileappend(FILE *fichier, char *optionfich)
7120: {
7121: if((fichier=fopen(optionfich,"a"))==NULL) {
7122: printf("Problem with file: %s\n", optionfich);
7123: fprintf(ficlog,"Problem with file: %s\n", optionfich);
7124: return (0);
7125: }
7126: fflush(fichier);
7127: return (1);
7128: }
7129:
7130:
7131: /**************** function prwizard **********************/
7132: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
7133: {
7134:
7135: /* Wizard to print covariance matrix template */
7136:
1.164 brouard 7137: char ca[32], cb[32];
7138: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 7139: int numlinepar;
7140:
7141: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7142: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7143: for(i=1; i <=nlstate; i++){
7144: jj=0;
7145: for(j=1; j <=nlstate+ndeath; j++){
7146: if(j==i) continue;
7147: jj++;
7148: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7149: printf("%1d%1d",i,j);
7150: fprintf(ficparo,"%1d%1d",i,j);
7151: for(k=1; k<=ncovmodel;k++){
7152: /* printf(" %lf",param[i][j][k]); */
7153: /* fprintf(ficparo," %lf",param[i][j][k]); */
7154: printf(" 0.");
7155: fprintf(ficparo," 0.");
7156: }
7157: printf("\n");
7158: fprintf(ficparo,"\n");
7159: }
7160: }
7161: printf("# Scales (for hessian or gradient estimation)\n");
7162: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
7163: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
7164: for(i=1; i <=nlstate; i++){
7165: jj=0;
7166: for(j=1; j <=nlstate+ndeath; j++){
7167: if(j==i) continue;
7168: jj++;
7169: fprintf(ficparo,"%1d%1d",i,j);
7170: printf("%1d%1d",i,j);
7171: fflush(stdout);
7172: for(k=1; k<=ncovmodel;k++){
7173: /* printf(" %le",delti3[i][j][k]); */
7174: /* fprintf(ficparo," %le",delti3[i][j][k]); */
7175: printf(" 0.");
7176: fprintf(ficparo," 0.");
7177: }
7178: numlinepar++;
7179: printf("\n");
7180: fprintf(ficparo,"\n");
7181: }
7182: }
7183: printf("# Covariance matrix\n");
7184: /* # 121 Var(a12)\n\ */
7185: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7186: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
7187: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
7188: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
7189: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
7190: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
7191: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7192: fflush(stdout);
7193: fprintf(ficparo,"# Covariance matrix\n");
7194: /* # 121 Var(a12)\n\ */
7195: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7196: /* # ...\n\ */
7197: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7198:
7199: for(itimes=1;itimes<=2;itimes++){
7200: jj=0;
7201: for(i=1; i <=nlstate; i++){
7202: for(j=1; j <=nlstate+ndeath; j++){
7203: if(j==i) continue;
7204: for(k=1; k<=ncovmodel;k++){
7205: jj++;
7206: ca[0]= k+'a'-1;ca[1]='\0';
7207: if(itimes==1){
7208: printf("#%1d%1d%d",i,j,k);
7209: fprintf(ficparo,"#%1d%1d%d",i,j,k);
7210: }else{
7211: printf("%1d%1d%d",i,j,k);
7212: fprintf(ficparo,"%1d%1d%d",i,j,k);
7213: /* printf(" %.5le",matcov[i][j]); */
7214: }
7215: ll=0;
7216: for(li=1;li <=nlstate; li++){
7217: for(lj=1;lj <=nlstate+ndeath; lj++){
7218: if(lj==li) continue;
7219: for(lk=1;lk<=ncovmodel;lk++){
7220: ll++;
7221: if(ll<=jj){
7222: cb[0]= lk +'a'-1;cb[1]='\0';
7223: if(ll<jj){
7224: if(itimes==1){
7225: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7226: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7227: }else{
7228: printf(" 0.");
7229: fprintf(ficparo," 0.");
7230: }
7231: }else{
7232: if(itimes==1){
7233: printf(" Var(%s%1d%1d)",ca,i,j);
7234: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
7235: }else{
7236: printf(" 0.");
7237: fprintf(ficparo," 0.");
7238: }
7239: }
7240: }
7241: } /* end lk */
7242: } /* end lj */
7243: } /* end li */
7244: printf("\n");
7245: fprintf(ficparo,"\n");
7246: numlinepar++;
7247: } /* end k*/
7248: } /*end j */
7249: } /* end i */
7250: } /* end itimes */
7251:
7252: } /* end of prwizard */
7253: /******************* Gompertz Likelihood ******************************/
7254: double gompertz(double x[])
7255: {
7256: double A,B,L=0.0,sump=0.,num=0.;
7257: int i,n=0; /* n is the size of the sample */
7258:
1.220 brouard 7259: for (i=1;i<=imx ; i++) {
1.126 brouard 7260: sump=sump+weight[i];
7261: /* sump=sump+1;*/
7262: num=num+1;
7263: }
7264:
7265:
7266: /* for (i=0; i<=imx; i++)
7267: 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]);*/
7268:
7269: for (i=1;i<=imx ; i++)
7270: {
7271: if (cens[i] == 1 && wav[i]>1)
7272: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
7273:
7274: if (cens[i] == 0 && wav[i]>1)
7275: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
7276: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
7277:
7278: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7279: if (wav[i] > 1 ) { /* ??? */
7280: L=L+A*weight[i];
7281: /* 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]);*/
7282: }
7283: }
7284:
7285: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7286:
7287: return -2*L*num/sump;
7288: }
7289:
1.136 brouard 7290: #ifdef GSL
7291: /******************* Gompertz_f Likelihood ******************************/
7292: double gompertz_f(const gsl_vector *v, void *params)
7293: {
7294: double A,B,LL=0.0,sump=0.,num=0.;
7295: double *x= (double *) v->data;
7296: int i,n=0; /* n is the size of the sample */
7297:
7298: for (i=0;i<=imx-1 ; i++) {
7299: sump=sump+weight[i];
7300: /* sump=sump+1;*/
7301: num=num+1;
7302: }
7303:
7304:
7305: /* for (i=0; i<=imx; i++)
7306: 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]);*/
7307: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
7308: for (i=1;i<=imx ; i++)
7309: {
7310: if (cens[i] == 1 && wav[i]>1)
7311: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
7312:
7313: if (cens[i] == 0 && wav[i]>1)
7314: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
7315: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
7316:
7317: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7318: if (wav[i] > 1 ) { /* ??? */
7319: LL=LL+A*weight[i];
7320: /* 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]);*/
7321: }
7322: }
7323:
7324: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7325: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
7326:
7327: return -2*LL*num/sump;
7328: }
7329: #endif
7330:
1.126 brouard 7331: /******************* Printing html file ***********/
1.201 brouard 7332: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7333: int lastpass, int stepm, int weightopt, char model[],\
7334: int imx, double p[],double **matcov,double agemortsup){
7335: int i,k;
7336:
7337: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
7338: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
7339: for (i=1;i<=2;i++)
7340: fprintf(fichtm," p[%d] = %lf [%f ; %f]<br>\n",i,p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.199 brouard 7341: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 7342: fprintf(fichtm,"</ul>");
7343:
7344: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
7345:
7346: 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>");
7347:
7348: for (k=agegomp;k<(agemortsup-2);k++)
7349: 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]);
7350:
7351:
7352: fflush(fichtm);
7353: }
7354:
7355: /******************* Gnuplot file **************/
1.201 brouard 7356: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 7357:
7358: char dirfileres[132],optfileres[132];
1.164 brouard 7359:
1.126 brouard 7360: int ng;
7361:
7362:
7363: /*#ifdef windows */
7364: fprintf(ficgp,"cd \"%s\" \n",pathc);
7365: /*#endif */
7366:
7367:
7368: strcpy(dirfileres,optionfilefiname);
7369: strcpy(optfileres,"vpl");
1.199 brouard 7370: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 7371: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 7372: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 7373: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 7374: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
7375:
7376: }
7377:
1.136 brouard 7378: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
7379: {
1.126 brouard 7380:
1.136 brouard 7381: /*-------- data file ----------*/
7382: FILE *fic;
7383: char dummy[]=" ";
1.223 brouard 7384: int i=0, j=0, n=0, iv=0;
7385: int lstra;
1.136 brouard 7386: int linei, month, year,iout;
7387: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 7388: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 7389: char *stratrunc;
1.223 brouard 7390:
1.126 brouard 7391:
7392:
1.136 brouard 7393: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 7394: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
7395: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 7396: }
1.126 brouard 7397:
1.136 brouard 7398: i=1;
7399: linei=0;
7400: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
7401: linei=linei+1;
7402: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
7403: if(line[j] == '\t')
7404: line[j] = ' ';
7405: }
7406: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
7407: ;
7408: };
7409: line[j+1]=0; /* Trims blanks at end of line */
7410: if(line[0]=='#'){
7411: fprintf(ficlog,"Comment line\n%s\n",line);
7412: printf("Comment line\n%s\n",line);
7413: continue;
7414: }
7415: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 7416: strcpy(line, linetmp);
1.223 brouard 7417:
7418: /* Loops on waves */
7419: for (j=maxwav;j>=1;j--){
7420: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.225 ! brouard 7421: cutv(stra, strb, line, ' ');
! 7422: if(strb[0]=='.') { /* Missing value */
! 7423: lval=-1;
! 7424: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
! 7425: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
! 7426: 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);
! 7427: 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);
! 7428: return 1;
! 7429: }
! 7430: }else{
! 7431: errno=0;
! 7432: /* what_kind_of_number(strb); */
! 7433: dval=strtod(strb,&endptr);
! 7434: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
! 7435: /* if(strb != endptr && *endptr == '\0') */
! 7436: /* dval=dlval; */
! 7437: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
! 7438: if( strb[0]=='\0' || (*endptr != '\0')){
! 7439: 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);
! 7440: 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);
! 7441: return 1;
! 7442: }
! 7443: cotqvar[j][iv][i]=dval;
! 7444: }
! 7445: strcpy(line,stra);
1.223 brouard 7446: }/* end loop ntqv */
1.225 ! brouard 7447:
1.223 brouard 7448: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.225 ! brouard 7449: cutv(stra, strb, line, ' ');
! 7450: if(strb[0]=='.') { /* Missing value */
! 7451: lval=-1;
! 7452: }else{
! 7453: errno=0;
! 7454: lval=strtol(strb,&endptr,10);
! 7455: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
! 7456: if( strb[0]=='\0' || (*endptr != '\0')){
! 7457: 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);
! 7458: 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);
! 7459: return 1;
! 7460: }
! 7461: }
! 7462: if(lval <-1 || lval >1){
! 7463: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7464: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7465: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 ! brouard 7466: For example, for multinomial values like 1, 2 and 3,\n \
! 7467: build V1=0 V2=0 for the reference value (1),\n \
! 7468: V1=1 V2=0 for (2) \n \
1.223 brouard 7469: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 ! brouard 7470: output of IMaCh is often meaningless.\n \
1.223 brouard 7471: Exiting.\n",lval,linei, i,line,j);
1.225 ! brouard 7472: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7473: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7474: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 ! brouard 7475: For example, for multinomial values like 1, 2 and 3,\n \
! 7476: build V1=0 V2=0 for the reference value (1),\n \
! 7477: V1=1 V2=0 for (2) \n \
1.223 brouard 7478: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
7479: output of IMaCh is often meaningless.\n \
7480: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 ! brouard 7481: return 1;
! 7482: }
! 7483: cotvar[j][iv][i]=(double)(lval);
! 7484: strcpy(line,stra);
1.223 brouard 7485: }/* end loop ntv */
1.225 ! brouard 7486:
1.223 brouard 7487: /* Statuses at wave */
1.137 brouard 7488: cutv(stra, strb, line, ' ');
1.223 brouard 7489: if(strb[0]=='.') { /* Missing value */
1.225 ! brouard 7490: lval=-1;
1.136 brouard 7491: }else{
1.225 ! brouard 7492: errno=0;
! 7493: lval=strtol(strb,&endptr,10);
! 7494: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
! 7495: if( strb[0]=='\0' || (*endptr != '\0')){
! 7496: 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);
! 7497: 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);
! 7498: return 1;
! 7499: }
1.136 brouard 7500: }
1.225 ! brouard 7501:
1.136 brouard 7502: s[j][i]=lval;
1.225 ! brouard 7503:
1.223 brouard 7504: /* Date of Interview */
1.136 brouard 7505: strcpy(line,stra);
7506: cutv(stra, strb,line,' ');
1.169 brouard 7507: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7508: }
1.169 brouard 7509: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 ! brouard 7510: month=99;
! 7511: year=9999;
1.136 brouard 7512: }else{
1.225 ! brouard 7513: 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);
! 7514: 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);
! 7515: return 1;
1.136 brouard 7516: }
7517: anint[j][i]= (double) year;
7518: mint[j][i]= (double)month;
7519: strcpy(line,stra);
1.223 brouard 7520: } /* End loop on waves */
1.225 ! brouard 7521:
1.223 brouard 7522: /* Date of death */
1.136 brouard 7523: cutv(stra, strb,line,' ');
1.169 brouard 7524: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7525: }
1.169 brouard 7526: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7527: month=99;
7528: year=9999;
7529: }else{
1.141 brouard 7530: printf("Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of death (mm/yyyy or .). Exiting.\n",strb, linei,i,line);
1.225 ! brouard 7531: 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);
! 7532: return 1;
1.136 brouard 7533: }
7534: andc[i]=(double) year;
7535: moisdc[i]=(double) month;
7536: strcpy(line,stra);
7537:
1.223 brouard 7538: /* Date of birth */
1.136 brouard 7539: cutv(stra, strb,line,' ');
1.169 brouard 7540: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7541: }
1.169 brouard 7542: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 7543: month=99;
7544: year=9999;
7545: }else{
1.141 brouard 7546: 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);
7547: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy or .). Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 ! brouard 7548: return 1;
1.136 brouard 7549: }
7550: if (year==9999) {
1.141 brouard 7551: 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);
7552: fprintf(ficlog,"Error reading data around '%s' at line number %d for individual %d, '%s'\nShould be a date of birth (mm/yyyy) but at least the year of birth should be given. Exiting.\n",strb, linei,i,line);fflush(ficlog);
1.225 ! brouard 7553: return 1;
! 7554:
1.136 brouard 7555: }
7556: annais[i]=(double)(year);
7557: moisnais[i]=(double)(month);
7558: strcpy(line,stra);
1.225 ! brouard 7559:
1.223 brouard 7560: /* Sample weight */
1.136 brouard 7561: cutv(stra, strb,line,' ');
7562: errno=0;
7563: dval=strtod(strb,&endptr);
7564: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7565: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
7566: fprintf(ficlog,"Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
1.136 brouard 7567: fflush(ficlog);
7568: return 1;
7569: }
7570: weight[i]=dval;
7571: strcpy(line,stra);
1.225 ! brouard 7572:
1.223 brouard 7573: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
7574: cutv(stra, strb, line, ' ');
7575: if(strb[0]=='.') { /* Missing value */
1.225 ! brouard 7576: lval=-1;
1.223 brouard 7577: }else{
1.225 ! brouard 7578: errno=0;
! 7579: /* what_kind_of_number(strb); */
! 7580: dval=strtod(strb,&endptr);
! 7581: /* if(strb != endptr && *endptr == '\0') */
! 7582: /* dval=dlval; */
! 7583: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
! 7584: if( strb[0]=='\0' || (*endptr != '\0')){
! 7585: 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);
! 7586: 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);
! 7587: return 1;
! 7588: }
! 7589: coqvar[iv][i]=dval;
1.223 brouard 7590: }
7591: strcpy(line,stra);
7592: }/* end loop nqv */
1.136 brouard 7593:
1.223 brouard 7594: /* Covariate values */
1.136 brouard 7595: for (j=ncovcol;j>=1;j--){
7596: cutv(stra, strb,line,' ');
1.223 brouard 7597: if(strb[0]=='.') { /* Missing covariate value */
1.225 ! brouard 7598: lval=-1;
1.136 brouard 7599: }else{
1.225 ! brouard 7600: errno=0;
! 7601: lval=strtol(strb,&endptr,10);
! 7602: if( strb[0]=='\0' || (*endptr != '\0')){
! 7603: 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);
! 7604: 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);
! 7605: return 1;
! 7606: }
1.136 brouard 7607: }
7608: if(lval <-1 || lval >1){
1.225 ! brouard 7609: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7610: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7611: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 ! brouard 7612: For example, for multinomial values like 1, 2 and 3,\n \
! 7613: build V1=0 V2=0 for the reference value (1),\n \
! 7614: V1=1 V2=0 for (2) \n \
1.136 brouard 7615: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 ! brouard 7616: output of IMaCh is often meaningless.\n \
1.136 brouard 7617: Exiting.\n",lval,linei, i,line,j);
1.225 ! brouard 7618: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7619: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7620: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 ! brouard 7621: For example, for multinomial values like 1, 2 and 3,\n \
! 7622: build V1=0 V2=0 for the reference value (1),\n \
! 7623: V1=1 V2=0 for (2) \n \
1.136 brouard 7624: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 ! brouard 7625: output of IMaCh is often meaningless.\n \
1.136 brouard 7626: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 ! brouard 7627: return 1;
1.136 brouard 7628: }
7629: covar[j][i]=(double)(lval);
7630: strcpy(line,stra);
7631: }
7632: lstra=strlen(stra);
1.225 ! brouard 7633:
1.136 brouard 7634: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
7635: stratrunc = &(stra[lstra-9]);
7636: num[i]=atol(stratrunc);
7637: }
7638: else
7639: num[i]=atol(stra);
7640: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
7641: 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;}*/
7642:
7643: i=i+1;
7644: } /* End loop reading data */
1.225 ! brouard 7645:
1.136 brouard 7646: *imax=i-1; /* Number of individuals */
7647: fclose(fic);
1.225 ! brouard 7648:
1.136 brouard 7649: return (0);
1.164 brouard 7650: /* endread: */
1.225 ! brouard 7651: printf("Exiting readdata: ");
! 7652: fclose(fic);
! 7653: return (1);
1.223 brouard 7654: }
1.126 brouard 7655:
1.145 brouard 7656: void removespace(char *str) {
7657: char *p1 = str, *p2 = str;
7658: do
7659: while (*p2 == ' ')
7660: p2++;
1.169 brouard 7661: while (*p1++ == *p2++);
1.145 brouard 7662: }
7663:
1.224 brouard 7664: int decodemodel ( char model[], int lastobs)
7665: /**< This routine decode the model and returns:
7666: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
7667: * - nagesqr = 1 if age*age in the model, otherwise 0.
7668: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
7669: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
7670: * - cptcovage number of covariates with age*products =2
7671: * - cptcovs number of simple covariates
7672: * - 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
7673: * which is a new column after the 9 (ncovcol) variables.
7674: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
7675: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
7676: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
7677: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
7678: */
1.136 brouard 7679: {
1.145 brouard 7680: int i, j, k, ks;
1.164 brouard 7681: int j1, k1, k2;
1.136 brouard 7682: char modelsav[80];
1.145 brouard 7683: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 7684: char *strpt;
1.136 brouard 7685:
1.145 brouard 7686: /*removespace(model);*/
1.136 brouard 7687: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 7688: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 7689: if (strstr(model,"AGE") !=0){
1.192 brouard 7690: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
7691: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 7692: return 1;
7693: }
1.141 brouard 7694: if (strstr(model,"v") !=0){
7695: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
7696: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
7697: return 1;
7698: }
1.187 brouard 7699: strcpy(modelsav,model);
7700: if ((strpt=strstr(model,"age*age")) !=0){
7701: printf(" strpt=%s, model=%s\n",strpt, model);
7702: if(strpt != model){
1.225 ! brouard 7703: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7704: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7705: corresponding column of parameters.\n",model);
1.225 ! brouard 7706: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7707: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7708: corresponding column of parameters.\n",model); fflush(ficlog);
1.225 ! brouard 7709: return 1;
! 7710: }
1.187 brouard 7711: nagesqr=1;
7712: if (strstr(model,"+age*age") !=0)
1.225 ! brouard 7713: substrchaine(modelsav, model, "+age*age");
1.187 brouard 7714: else if (strstr(model,"age*age+") !=0)
1.225 ! brouard 7715: substrchaine(modelsav, model, "age*age+");
1.187 brouard 7716: else
1.225 ! brouard 7717: substrchaine(modelsav, model, "age*age");
1.187 brouard 7718: }else
7719: nagesqr=0;
7720: if (strlen(modelsav) >1){
7721: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
7722: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 7723: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 7724: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 ! brouard 7725: * cst, age and age*age
! 7726: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
! 7727: /* including age products which are counted in cptcovage.
! 7728: * but the covariates which are products must be treated
! 7729: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 7730: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
7731: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 ! brouard 7732:
! 7733:
1.187 brouard 7734: /* Design
7735: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
7736: * < ncovcol=8 >
7737: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
7738: * k= 1 2 3 4 5 6 7 8
7739: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
7740: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 7741: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
7742: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 7743: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
7744: * Tage[++cptcovage]=k
7745: * if products, new covar are created after ncovcol with k1
7746: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
7747: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
7748: * 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
7749: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
7750: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
7751: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
7752: * < ncovcol=8 >
7753: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
7754: * k= 1 2 3 4 5 6 7 8 9 10 11 12
7755: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
7756: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7757: * p Tprod[1]@2={ 6, 5}
7758: *p Tvard[1][1]@4= {7, 8, 5, 6}
7759: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
7760: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
7761: *How to reorganize?
7762: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
7763: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7764: * {2, 1, 4, 8, 5, 6, 3, 7}
7765: * Struct []
7766: */
1.225 ! brouard 7767:
1.187 brouard 7768: /* This loop fills the array Tvar from the string 'model'.*/
7769: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
7770: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
7771: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
7772: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
7773: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
7774: /* k=1 Tvar[1]=2 (from V2) */
7775: /* k=5 Tvar[5] */
7776: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 7777: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 7778: /* } */
1.198 brouard 7779: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 7780: /*
7781: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.225 ! brouard 7782: for(k=cptcovt; k>=1;k--) /**< Number of covariates not including constant and age, neither age*age*/
1.145 brouard 7783: Tvar[k]=0;
1.187 brouard 7784: cptcovage=0;
7785: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.225 ! brouard 7786: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
! 7787: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
! 7788: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
! 7789: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
! 7790: /*scanf("%d",i);*/
! 7791: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
! 7792: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
! 7793: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
! 7794: /* covar is not filled and then is empty */
! 7795: cptcovprod--;
! 7796: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
! 7797: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
! 7798: Typevar[k]=1; /* 2 for age product */
! 7799: cptcovage++; /* Sums the number of covariates which include age as a product */
! 7800: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
! 7801: /*printf("stre=%s ", stre);*/
! 7802: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
! 7803: cptcovprod--;
! 7804: cutl(stre,strb,strc,'V');
! 7805: Tvar[k]=atoi(stre);
! 7806: Typevar[k]=1; /* 1 for age product */
! 7807: cptcovage++;
! 7808: Tage[cptcovage]=k;
! 7809: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
! 7810: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
! 7811: cptcovn++;
! 7812: cptcovprodnoage++;k1++;
! 7813: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
! 7814: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
! 7815: because this model-covariate is a construction we invent a new column
! 7816: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
! 7817: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
! 7818: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
! 7819: Typevar[k]=2; /* 2 for double fixed dummy covariates */
! 7820: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
! 7821: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
! 7822: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
! 7823: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
! 7824: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
! 7825: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
! 7826: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
! 7827: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
! 7828: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
! 7829: for (i=1; i<=lastobs;i++){
! 7830: /* Computes the new covariate which is a product of
! 7831: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
! 7832: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
! 7833: }
! 7834: } /* End age is not in the model */
! 7835: } /* End if model includes a product */
! 7836: else { /* no more sum */
! 7837: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
! 7838: /* scanf("%d",i);*/
! 7839: cutl(strd,strc,strb,'V');
! 7840: ks++; /**< Number of simple covariates*/
! 7841: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
! 7842: Tvar[k]=atoi(strd);
! 7843: Typevar[k]=0; /* 0 for simple covariates */
! 7844: }
! 7845: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 7846: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 ! brouard 7847: scanf("%d",i);*/
1.187 brouard 7848: } /* end of loop + on total covariates */
7849: } /* end if strlen(modelsave == 0) age*age might exist */
7850: } /* end if strlen(model == 0) */
1.136 brouard 7851:
7852: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
7853: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 ! brouard 7854:
1.136 brouard 7855: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 ! brouard 7856: printf("cptcovprod=%d ", cptcovprod);
! 7857: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
! 7858: scanf("%d ",i);*/
! 7859:
! 7860:
! 7861: /* Decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
! 7862: of variable (dummy vs quantitative, fixed vs time varying) is behind */
! 7863: /* ncovcol= 1, nqv=1, ntv=2, nqtv= 1 = 5 possible variables data
! 7864: model= V2 + V4 +V3 + V4*V3 + V5*age + V5 , V1 is not used saving its place
! 7865: k = 1 2 3 4 5 6
! 7866: Tvar[k]= 2 4 3 1+1+2+1+1=6 5 5
! 7867: Typevar[k]=0 0 0 2 1 0
! 7868: */
! 7869: /* Dispatching between quantitative and time varying covariates */
! 7870: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
! 7871: for(k=1, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0;k<=cptcovt; k++){ /* or cptocvt */
! 7872: if (Tvar[k] <=ncovcol){ /* Simple fixed dummy covariatee */
! 7873: ncoveff++;
! 7874: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){ /* Remind that product Vn*Vm are added in k*/
! 7875: nqfveff++; /* Only simple fixed quantitative variable */
! 7876: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){
! 7877: ntveff++; /* Only simple time varying dummy variable */
! 7878: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv && Typevar[k]==0){
! 7879: nqtveff++;/* Only simple time varying quantitative variable */
! 7880: }else{
! 7881: printf("Other types in effective covariates \n");
! 7882: }
! 7883: }
! 7884:
! 7885: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
! 7886: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.137 brouard 7887: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 7888: /*endread:*/
1.225 ! brouard 7889: printf("Exiting decodemodel: ");
! 7890: return (1);
1.136 brouard 7891: }
7892:
1.169 brouard 7893: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 7894: {
7895: int i, m;
1.218 brouard 7896: int firstone=0;
7897:
1.136 brouard 7898: for (i=1; i<=imx; i++) {
7899: for(m=2; (m<= maxwav); m++) {
7900: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
7901: anint[m][i]=9999;
1.216 brouard 7902: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
7903: s[m][i]=-1;
1.136 brouard 7904: }
7905: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 7906: *nberr = *nberr + 1;
1.218 brouard 7907: if(firstone == 0){
7908: firstone=1;
7909: printf("Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results can be biased (%d) because status is a death state %d at wave %d. Wave dropped.\nOther similar cases in log file\n",(int)moisdc[i],(int)andc[i],num[i],i, *nberr,s[m][i],m);
7910: }
7911: fprintf(ficlog,"Error! Date of death (month %2d and year %4d) of individual %ld on line %d was unknown, you must set an arbitrary year of death or he/she is skipped and results can be biased (%d) because status is a death state %d at wave %d. Wave dropped.\n",(int)moisdc[i],(int)andc[i],num[i],i, *nberr,s[m][i],m);
1.136 brouard 7912: s[m][i]=-1;
7913: }
7914: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 7915: (*nberr)++;
1.136 brouard 7916: printf("Error! Month of death of individual %ld on line %d was unknown %2d, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,(int)moisdc[i]);
7917: fprintf(ficlog,"Error! Month of death of individual %ld on line %d was unknown %f, you should set it otherwise the information on the death is skipped and results are biased.\n",num[i],i,moisdc[i]);
7918: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
7919: }
7920: }
7921: }
7922:
7923: for (i=1; i<=imx; i++) {
7924: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
7925: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 7926: if(s[m][i] >0 || s[m][i]==-1 || s[m][i]==-2 || s[m][i]==-4 || s[m][i]==-5){ /* What if s[m][i]=-1 */
1.136 brouard 7927: if (s[m][i] >= nlstate+1) {
1.169 brouard 7928: if(agedc[i]>0){
7929: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 7930: agev[m][i]=agedc[i];
1.214 brouard 7931: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 7932: }else {
1.136 brouard 7933: if ((int)andc[i]!=9999){
7934: nbwarn++;
7935: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
7936: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
7937: agev[m][i]=-1;
7938: }
7939: }
1.169 brouard 7940: } /* agedc > 0 */
1.214 brouard 7941: } /* end if */
1.136 brouard 7942: else if(s[m][i] !=9){ /* Standard case, age in fractional
7943: years but with the precision of a month */
7944: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
7945: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
7946: agev[m][i]=1;
7947: else if(agev[m][i] < *agemin){
7948: *agemin=agev[m][i];
7949: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
7950: }
7951: else if(agev[m][i] >*agemax){
7952: *agemax=agev[m][i];
1.156 brouard 7953: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 7954: }
7955: /*agev[m][i]=anint[m][i]-annais[i];*/
7956: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 7957: } /* en if 9*/
1.136 brouard 7958: else { /* =9 */
1.214 brouard 7959: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 7960: agev[m][i]=1;
7961: s[m][i]=-1;
7962: }
7963: }
1.214 brouard 7964: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 7965: agev[m][i]=1;
1.214 brouard 7966: else{
7967: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
7968: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
7969: agev[m][i]=0;
7970: }
7971: } /* End for lastpass */
7972: }
1.136 brouard 7973:
7974: for (i=1; i<=imx; i++) {
7975: for(m=firstpass; (m<=lastpass); m++){
7976: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 7977: (*nberr)++;
1.136 brouard 7978: 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);
7979: 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);
7980: return 1;
7981: }
7982: }
7983: }
7984:
7985: /*for (i=1; i<=imx; i++){
7986: for (m=firstpass; (m<lastpass); m++){
7987: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
7988: }
7989:
7990: }*/
7991:
7992:
1.139 brouard 7993: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
7994: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 7995:
7996: return (0);
1.164 brouard 7997: /* endread:*/
1.136 brouard 7998: printf("Exiting calandcheckages: ");
7999: return (1);
8000: }
8001:
1.172 brouard 8002: #if defined(_MSC_VER)
8003: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8004: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8005: //#include "stdafx.h"
8006: //#include <stdio.h>
8007: //#include <tchar.h>
8008: //#include <windows.h>
8009: //#include <iostream>
8010: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
8011:
8012: LPFN_ISWOW64PROCESS fnIsWow64Process;
8013:
8014: BOOL IsWow64()
8015: {
8016: BOOL bIsWow64 = FALSE;
8017:
8018: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
8019: // (HANDLE, PBOOL);
8020:
8021: //LPFN_ISWOW64PROCESS fnIsWow64Process;
8022:
8023: HMODULE module = GetModuleHandle(_T("kernel32"));
8024: const char funcName[] = "IsWow64Process";
8025: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
8026: GetProcAddress(module, funcName);
8027:
8028: if (NULL != fnIsWow64Process)
8029: {
8030: if (!fnIsWow64Process(GetCurrentProcess(),
8031: &bIsWow64))
8032: //throw std::exception("Unknown error");
8033: printf("Unknown error\n");
8034: }
8035: return bIsWow64 != FALSE;
8036: }
8037: #endif
1.177 brouard 8038:
1.191 brouard 8039: void syscompilerinfo(int logged)
1.167 brouard 8040: {
8041: /* #include "syscompilerinfo.h"*/
1.185 brouard 8042: /* command line Intel compiler 32bit windows, XP compatible:*/
8043: /* /GS /W3 /Gy
8044: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
8045: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
8046: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 8047: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
8048: */
8049: /* 64 bits */
1.185 brouard 8050: /*
8051: /GS /W3 /Gy
8052: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
8053: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
8054: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
8055: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
8056: /* Optimization are useless and O3 is slower than O2 */
8057: /*
8058: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
8059: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
8060: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
8061: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
8062: */
1.186 brouard 8063: /* Link is */ /* /OUT:"visual studio
1.185 brouard 8064: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
8065: /PDB:"visual studio
8066: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
8067: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
8068: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
8069: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
8070: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
8071: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
8072: uiAccess='false'"
8073: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
8074: /NOLOGO /TLBID:1
8075: */
1.177 brouard 8076: #if defined __INTEL_COMPILER
1.178 brouard 8077: #if defined(__GNUC__)
8078: struct utsname sysInfo; /* For Intel on Linux and OS/X */
8079: #endif
1.177 brouard 8080: #elif defined(__GNUC__)
1.179 brouard 8081: #ifndef __APPLE__
1.174 brouard 8082: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 8083: #endif
1.177 brouard 8084: struct utsname sysInfo;
1.178 brouard 8085: int cross = CROSS;
8086: if (cross){
8087: printf("Cross-");
1.191 brouard 8088: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 8089: }
1.174 brouard 8090: #endif
8091:
1.171 brouard 8092: #include <stdint.h>
1.178 brouard 8093:
1.191 brouard 8094: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 8095: #if defined(__clang__)
1.191 brouard 8096: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 8097: #endif
8098: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 8099: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 8100: #endif
8101: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 8102: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 8103: #endif
8104: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 8105: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 8106: #endif
8107: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 8108: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 8109: #endif
8110: #if defined(_MSC_VER)
1.191 brouard 8111: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 8112: #endif
8113: #if defined(__PGI)
1.191 brouard 8114: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 8115: #endif
8116: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 8117: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 8118: #endif
1.191 brouard 8119: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 8120:
1.167 brouard 8121: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
8122: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
8123: // Windows (x64 and x86)
1.191 brouard 8124: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 8125: #elif __unix__ // all unices, not all compilers
8126: // Unix
1.191 brouard 8127: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 8128: #elif __linux__
8129: // linux
1.191 brouard 8130: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 8131: #elif __APPLE__
1.174 brouard 8132: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 8133: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 8134: #endif
8135:
8136: /* __MINGW32__ */
8137: /* __CYGWIN__ */
8138: /* __MINGW64__ */
8139: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
8140: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
8141: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
8142: /* _WIN64 // Defined for applications for Win64. */
8143: /* _M_X64 // Defined for compilations that target x64 processors. */
8144: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 8145:
1.167 brouard 8146: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 8147: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 8148: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 8149: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 8150: #else
1.191 brouard 8151: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 8152: #endif
8153:
1.169 brouard 8154: #if defined(__GNUC__)
8155: # if defined(__GNUC_PATCHLEVEL__)
8156: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8157: + __GNUC_MINOR__ * 100 \
8158: + __GNUC_PATCHLEVEL__)
8159: # else
8160: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8161: + __GNUC_MINOR__ * 100)
8162: # endif
1.174 brouard 8163: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 8164: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 8165:
8166: if (uname(&sysInfo) != -1) {
8167: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 8168: if(logged) fprintf(ficlog,"Running on: %s %s %s %s %s\n ",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.176 brouard 8169: }
8170: else
8171: perror("uname() error");
1.179 brouard 8172: //#ifndef __INTEL_COMPILER
8173: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 8174: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 8175: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 8176: #endif
1.169 brouard 8177: #endif
1.172 brouard 8178:
8179: // void main()
8180: // {
1.169 brouard 8181: #if defined(_MSC_VER)
1.174 brouard 8182: if (IsWow64()){
1.191 brouard 8183: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
8184: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 8185: }
8186: else{
1.191 brouard 8187: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
8188: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 8189: }
1.172 brouard 8190: // printf("\nPress Enter to continue...");
8191: // getchar();
8192: // }
8193:
1.169 brouard 8194: #endif
8195:
1.167 brouard 8196:
1.219 brouard 8197: }
1.136 brouard 8198:
1.219 brouard 8199: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 8200: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
8201: int i, j, k, i1 ;
1.202 brouard 8202: /* double ftolpl = 1.e-10; */
1.180 brouard 8203: double age, agebase, agelim;
1.203 brouard 8204: double tot;
1.180 brouard 8205:
1.202 brouard 8206: strcpy(filerespl,"PL_");
8207: strcat(filerespl,fileresu);
8208: if((ficrespl=fopen(filerespl,"w"))==NULL) {
8209: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8210: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8211: }
8212: printf("Computing period (stable) prevalence: result on file '%s' \n", filerespl);
8213: fprintf(ficlog,"Computing period (stable) prevalence: result on file '%s' \n", filerespl);
8214: pstamp(ficrespl);
1.203 brouard 8215: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 8216: fprintf(ficrespl,"#Age ");
8217: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
8218: fprintf(ficrespl,"\n");
1.180 brouard 8219:
1.219 brouard 8220: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 8221:
1.219 brouard 8222: agebase=ageminpar;
8223: agelim=agemaxpar;
1.180 brouard 8224:
1.224 brouard 8225: i1=pow(2,ncoveff);
1.219 brouard 8226: if (cptcovn < 1){i1=1;}
1.180 brouard 8227:
1.220 brouard 8228: for(k=1; k<=i1;k++){
8229: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
1.180 brouard 8230: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.219 brouard 8231: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
1.220 brouard 8232: /* k=k+1; */
1.219 brouard 8233: /* to clean */
8234: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8235: fprintf(ficrespl,"#******");
8236: printf("#******");
8237: fprintf(ficlog,"#******");
1.225 ! brouard 8238: for(j=1;j<=nqfveff;j++) {
1.219 brouard 8239: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8240: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8241: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8242: }
8243: fprintf(ficrespl,"******\n");
8244: printf("******\n");
8245: fprintf(ficlog,"******\n");
1.220 brouard 8246: if(invalidvarcomb[k]){
8247: printf("\nCombination (%d) ignored because no cases \n",k);
8248: fprintf(ficrespl,"#Combination (%d) ignored because no cases \n",k);
8249: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
8250: continue;
8251: }
1.219 brouard 8252:
8253: fprintf(ficrespl,"#Age ");
1.225 ! brouard 8254: for(j=1;j<=nqfveff;j++) {
1.219 brouard 8255: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8256: }
8257: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
8258: fprintf(ficrespl,"Total Years_to_converge\n");
8259:
8260: for (age=agebase; age<=agelim; age++){
8261: /* for (age=agebase; age<=agebase; age++){ */
8262: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k);
8263: fprintf(ficrespl,"%.0f ",age );
1.225 ! brouard 8264: for(j=1;j<=nqfveff;j++)
1.220 brouard 8265: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 8266: tot=0.;
8267: for(i=1; i<=nlstate;i++){
1.220 brouard 8268: tot += prlim[i][i];
8269: fprintf(ficrespl," %.5f", prlim[i][i]);
1.219 brouard 8270: }
8271: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
8272: } /* Age */
8273: /* was end of cptcod */
8274: } /* cptcov */
8275: return 0;
1.180 brouard 8276: }
8277:
1.218 brouard 8278: 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){
8279: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
8280:
8281: /* Computes the back prevalence limit for any combination of covariate values
8282: * at any age between ageminpar and agemaxpar
8283: */
1.217 brouard 8284: int i, j, k, i1 ;
8285: /* double ftolpl = 1.e-10; */
8286: double age, agebase, agelim;
8287: double tot;
1.218 brouard 8288: /* double ***mobaverage; */
8289: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 8290:
8291: strcpy(fileresplb,"PLB_");
8292: strcat(fileresplb,fileresu);
8293: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
8294: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8295: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8296: }
8297: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8298: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8299: pstamp(ficresplb);
8300: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
8301: fprintf(ficresplb,"#Age ");
8302: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
8303: fprintf(ficresplb,"\n");
8304:
1.218 brouard 8305:
8306: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
8307:
8308: agebase=ageminpar;
8309: agelim=agemaxpar;
8310:
8311:
1.225 ! brouard 8312: i1=pow(2,nqfveff);
1.218 brouard 8313: if (cptcovn < 1){i1=1;}
1.220 brouard 8314:
8315: for(k=1; k<=i1;k++){
8316: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
1.217 brouard 8317: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.218 brouard 8318: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
1.220 brouard 8319: /* k=k+1; */
1.218 brouard 8320: /* to clean */
8321: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8322: fprintf(ficresplb,"#******");
8323: printf("#******");
8324: fprintf(ficlog,"#******");
1.225 ! brouard 8325: for(j=1;j<=nqfveff;j++) {
1.218 brouard 8326: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8327: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8328: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8329: }
8330: fprintf(ficresplb,"******\n");
8331: printf("******\n");
8332: fprintf(ficlog,"******\n");
1.220 brouard 8333: if(invalidvarcomb[k]){
8334: printf("\nCombination (%d) ignored because no cases \n",k);
8335: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
8336: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
8337: continue;
8338: }
1.218 brouard 8339:
8340: fprintf(ficresplb,"#Age ");
1.225 ! brouard 8341: for(j=1;j<=nqfveff;j++) {
1.218 brouard 8342: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8343: }
8344: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
8345: fprintf(ficresplb,"Total Years_to_converge\n");
8346:
8347:
8348: for (age=agebase; age<=agelim; age++){
8349: /* for (age=agebase; age<=agebase; age++){ */
8350: if(mobilavproj > 0){
8351: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
8352: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.219 brouard 8353: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8354: }else if (mobilavproj == 0){
1.219 brouard 8355: 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);
8356: 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);
8357: exit(1);
1.218 brouard 8358: }else{
1.219 brouard 8359: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
8360: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8361: }
8362: fprintf(ficresplb,"%.0f ",age );
1.225 ! brouard 8363: for(j=1;j<=nqfveff;j++)
1.219 brouard 8364: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.218 brouard 8365: tot=0.;
8366: for(i=1; i<=nlstate;i++){
1.219 brouard 8367: tot += bprlim[i][i];
8368: fprintf(ficresplb," %.5f", bprlim[i][i]);
1.218 brouard 8369: }
8370: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
8371: } /* Age */
8372: /* was end of cptcod */
8373: } /* cptcov */
8374:
8375: /* hBijx(p, bage, fage); */
8376: /* fclose(ficrespijb); */
8377:
8378: return 0;
1.217 brouard 8379: }
1.218 brouard 8380:
1.180 brouard 8381: int hPijx(double *p, int bage, int fage){
8382: /*------------- h Pij x at various ages ------------*/
8383:
8384: int stepsize;
8385: int agelim;
8386: int hstepm;
8387: int nhstepm;
8388: int h, i, i1, j, k;
8389:
8390: double agedeb;
8391: double ***p3mat;
8392:
1.201 brouard 8393: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 8394: if((ficrespij=fopen(filerespij,"w"))==NULL) {
8395: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
8396: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
8397: }
8398: printf("Computing pij: result on file '%s' \n", filerespij);
8399: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
8400:
8401: stepsize=(int) (stepm+YEARM-1)/YEARM;
8402: /*if (stepm<=24) stepsize=2;*/
8403:
8404: agelim=AGESUP;
8405: hstepm=stepsize*YEARM; /* Every year of age */
8406: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 8407:
1.180 brouard 8408: /* hstepm=1; aff par mois*/
8409: pstamp(ficrespij);
8410: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.225 ! brouard 8411: i1= pow(2,nqfveff);
1.218 brouard 8412: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
8413: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
8414: /* k=k+1; */
1.225 ! brouard 8415: for (k=1; k <= (int) pow(2,nqfveff); k++){
1.183 brouard 8416: fprintf(ficrespij,"\n#****** ");
1.225 ! brouard 8417: for(j=1;j<=nqfveff;j++)
1.198 brouard 8418: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.183 brouard 8419: fprintf(ficrespij,"******\n");
8420:
8421: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
8422: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8423: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
8424:
8425: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 8426:
1.183 brouard 8427: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8428: oldm=oldms;savm=savms;
8429: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
8430: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
8431: for(i=1; i<=nlstate;i++)
8432: for(j=1; j<=nlstate+ndeath;j++)
8433: fprintf(ficrespij," %1d-%1d",i,j);
8434: fprintf(ficrespij,"\n");
8435: for (h=0; h<=nhstepm; h++){
8436: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
8437: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 8438: for(i=1; i<=nlstate;i++)
8439: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 8440: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 8441: fprintf(ficrespij,"\n");
8442: }
1.183 brouard 8443: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8444: fprintf(ficrespij,"\n");
8445: }
1.180 brouard 8446: /*}*/
8447: }
1.218 brouard 8448: return 0;
1.180 brouard 8449: }
1.218 brouard 8450:
8451: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 8452: /*------------- h Bij x at various ages ------------*/
8453:
8454: int stepsize;
1.218 brouard 8455: /* int agelim; */
8456: int ageminl;
1.217 brouard 8457: int hstepm;
8458: int nhstepm;
8459: int h, i, i1, j, k;
1.218 brouard 8460:
1.217 brouard 8461: double agedeb;
8462: double ***p3mat;
1.218 brouard 8463:
8464: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
8465: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
8466: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
8467: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
8468: }
8469: printf("Computing pij back: result on file '%s' \n", filerespijb);
8470: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
8471:
8472: stepsize=(int) (stepm+YEARM-1)/YEARM;
8473: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 8474:
1.218 brouard 8475: /* agelim=AGESUP; */
8476: ageminl=30;
8477: hstepm=stepsize*YEARM; /* Every year of age */
8478: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
8479:
8480: /* hstepm=1; aff par mois*/
8481: pstamp(ficrespijb);
8482: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
1.225 ! brouard 8483: i1= pow(2,nqfveff);
1.218 brouard 8484: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
8485: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
8486: /* k=k+1; */
1.225 ! brouard 8487: for (k=1; k <= (int) pow(2,nqfveff); k++){
1.218 brouard 8488: fprintf(ficrespijb,"\n#****** ");
1.225 ! brouard 8489: for(j=1;j<=nqfveff;j++)
1.218 brouard 8490: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8491: fprintf(ficrespijb,"******\n");
1.222 brouard 8492: if(invalidvarcomb[k]){
8493: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
8494: continue;
8495: }
1.218 brouard 8496:
8497: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
8498: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
8499: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
8500: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8501: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
8502:
8503: /* nhstepm=nhstepm*YEARM; aff par mois*/
8504:
8505: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8506: /* oldm=oldms;savm=savms; */
8507: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
8508: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
8509: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
8510: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
8511: for(i=1; i<=nlstate;i++)
8512: for(j=1; j<=nlstate+ndeath;j++)
8513: fprintf(ficrespijb," %1d-%1d",i,j);
8514: fprintf(ficrespijb,"\n");
8515: for (h=0; h<=nhstepm; h++){
8516: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
8517: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
8518: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 8519: for(i=1; i<=nlstate;i++)
8520: for(j=1; j<=nlstate+ndeath;j++)
1.218 brouard 8521: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
1.217 brouard 8522: fprintf(ficrespijb,"\n");
8523: }
1.218 brouard 8524: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8525: fprintf(ficrespijb,"\n");
1.217 brouard 8526: }
1.218 brouard 8527: /*}*/
8528: }
8529: return 0;
8530: } /* hBijx */
1.217 brouard 8531:
1.180 brouard 8532:
1.136 brouard 8533: /***********************************************/
8534: /**************** Main Program *****************/
8535: /***********************************************/
8536:
8537: int main(int argc, char *argv[])
8538: {
8539: #ifdef GSL
8540: const gsl_multimin_fminimizer_type *T;
8541: size_t iteri = 0, it;
8542: int rval = GSL_CONTINUE;
8543: int status = GSL_SUCCESS;
8544: double ssval;
8545: #endif
8546: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 8547: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 8548: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 8549: int jj, ll, li, lj, lk;
1.136 brouard 8550: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 8551: int num_filled;
1.136 brouard 8552: int itimes;
8553: int NDIM=2;
8554: int vpopbased=0;
8555:
1.164 brouard 8556: char ca[32], cb[32];
1.136 brouard 8557: /* FILE *fichtm; *//* Html File */
8558: /* FILE *ficgp;*/ /*Gnuplot File */
8559: struct stat info;
1.191 brouard 8560: double agedeb=0.;
1.194 brouard 8561:
8562: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 8563: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 8564:
1.165 brouard 8565: double fret;
1.191 brouard 8566: double dum=0.; /* Dummy variable */
1.136 brouard 8567: double ***p3mat;
1.218 brouard 8568: /* double ***mobaverage; */
1.164 brouard 8569:
8570: char line[MAXLINE];
1.197 brouard 8571: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
8572:
8573: char model[MAXLINE], modeltemp[MAXLINE];
1.136 brouard 8574: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 8575: char *tok, *val; /* pathtot */
1.136 brouard 8576: int firstobs=1, lastobs=10;
1.195 brouard 8577: int c, h , cpt, c2;
1.191 brouard 8578: int jl=0;
8579: int i1, j1, jk, stepsize=0;
1.194 brouard 8580: int count=0;
8581:
1.164 brouard 8582: int *tab;
1.136 brouard 8583: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 8584: int backcast=0;
1.136 brouard 8585: int mobilav=0,popforecast=0;
1.191 brouard 8586: int hstepm=0, nhstepm=0;
1.136 brouard 8587: int agemortsup;
8588: float sumlpop=0.;
8589: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
8590: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
8591:
1.191 brouard 8592: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 8593: double ftolpl=FTOL;
8594: double **prlim;
1.217 brouard 8595: double **bprlim;
1.136 brouard 8596: double ***param; /* Matrix of parameters */
8597: double *p;
8598: double **matcov; /* Matrix of covariance */
1.203 brouard 8599: double **hess; /* Hessian matrix */
1.136 brouard 8600: double ***delti3; /* Scale */
8601: double *delti; /* Scale */
8602: double ***eij, ***vareij;
8603: double **varpl; /* Variances of prevalence limits by age */
8604: double *epj, vepp;
1.164 brouard 8605:
1.136 brouard 8606: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 8607: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
8608:
1.136 brouard 8609: double **ximort;
1.145 brouard 8610: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 8611: int *dcwave;
8612:
1.164 brouard 8613: char z[1]="c";
1.136 brouard 8614:
8615: /*char *strt;*/
8616: char strtend[80];
1.126 brouard 8617:
1.164 brouard 8618:
1.126 brouard 8619: /* setlocale (LC_ALL, ""); */
8620: /* bindtextdomain (PACKAGE, LOCALEDIR); */
8621: /* textdomain (PACKAGE); */
8622: /* setlocale (LC_CTYPE, ""); */
8623: /* setlocale (LC_MESSAGES, ""); */
8624:
8625: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 8626: rstart_time = time(NULL);
8627: /* (void) gettimeofday(&start_time,&tzp);*/
8628: start_time = *localtime(&rstart_time);
1.126 brouard 8629: curr_time=start_time;
1.157 brouard 8630: /*tml = *localtime(&start_time.tm_sec);*/
8631: /* strcpy(strstart,asctime(&tml)); */
8632: strcpy(strstart,asctime(&start_time));
1.126 brouard 8633:
8634: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 8635: /* tp.tm_sec = tp.tm_sec +86400; */
8636: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 8637: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
8638: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
8639: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 8640: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 8641: /* strt=asctime(&tmg); */
8642: /* printf("Time(after) =%s",strstart); */
8643: /* (void) time (&time_value);
8644: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
8645: * tm = *localtime(&time_value);
8646: * strstart=asctime(&tm);
8647: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
8648: */
8649:
8650: nberr=0; /* Number of errors and warnings */
8651: nbwarn=0;
1.184 brouard 8652: #ifdef WIN32
8653: _getcwd(pathcd, size);
8654: #else
1.126 brouard 8655: getcwd(pathcd, size);
1.184 brouard 8656: #endif
1.191 brouard 8657: syscompilerinfo(0);
1.196 brouard 8658: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 8659: if(argc <=1){
8660: printf("\nEnter the parameter file name: ");
1.205 brouard 8661: if(!fgets(pathr,FILENAMELENGTH,stdin)){
8662: printf("ERROR Empty parameter file name\n");
8663: goto end;
8664: }
1.126 brouard 8665: i=strlen(pathr);
8666: if(pathr[i-1]=='\n')
8667: pathr[i-1]='\0';
1.156 brouard 8668: i=strlen(pathr);
1.205 brouard 8669: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 8670: pathr[i-1]='\0';
1.205 brouard 8671: }
8672: i=strlen(pathr);
8673: if( i==0 ){
8674: printf("ERROR Empty parameter file name\n");
8675: goto end;
8676: }
8677: for (tok = pathr; tok != NULL; ){
1.126 brouard 8678: printf("Pathr |%s|\n",pathr);
8679: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
8680: printf("val= |%s| pathr=%s\n",val,pathr);
8681: strcpy (pathtot, val);
8682: if(pathr[0] == '\0') break; /* Dirty */
8683: }
8684: }
8685: else{
8686: strcpy(pathtot,argv[1]);
8687: }
8688: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
8689: /*cygwin_split_path(pathtot,path,optionfile);
8690: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
8691: /* cutv(path,optionfile,pathtot,'\\');*/
8692:
8693: /* Split argv[0], imach program to get pathimach */
8694: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
8695: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
8696: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
8697: /* strcpy(pathimach,argv[0]); */
8698: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
8699: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
8700: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 8701: #ifdef WIN32
8702: _chdir(path); /* Can be a relative path */
8703: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
8704: #else
1.126 brouard 8705: chdir(path); /* Can be a relative path */
1.184 brouard 8706: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
8707: #endif
8708: printf("Current directory %s!\n",pathcd);
1.126 brouard 8709: strcpy(command,"mkdir ");
8710: strcat(command,optionfilefiname);
8711: if((outcmd=system(command)) != 0){
1.169 brouard 8712: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 8713: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
8714: /* fclose(ficlog); */
8715: /* exit(1); */
8716: }
8717: /* if((imk=mkdir(optionfilefiname))<0){ */
8718: /* perror("mkdir"); */
8719: /* } */
8720:
8721: /*-------- arguments in the command line --------*/
8722:
1.186 brouard 8723: /* Main Log file */
1.126 brouard 8724: strcat(filelog, optionfilefiname);
8725: strcat(filelog,".log"); /* */
8726: if((ficlog=fopen(filelog,"w"))==NULL) {
8727: printf("Problem with logfile %s\n",filelog);
8728: goto end;
8729: }
8730: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 8731: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 8732: fprintf(ficlog,"\nEnter the parameter file name: \n");
8733: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
8734: path=%s \n\
8735: optionfile=%s\n\
8736: optionfilext=%s\n\
1.156 brouard 8737: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 8738:
1.197 brouard 8739: syscompilerinfo(1);
1.167 brouard 8740:
1.126 brouard 8741: printf("Local time (at start):%s",strstart);
8742: fprintf(ficlog,"Local time (at start): %s",strstart);
8743: fflush(ficlog);
8744: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 8745: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 8746:
8747: /* */
8748: strcpy(fileres,"r");
8749: strcat(fileres, optionfilefiname);
1.201 brouard 8750: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 8751: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 8752: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 8753:
1.186 brouard 8754: /* Main ---------arguments file --------*/
1.126 brouard 8755:
8756: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 8757: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
8758: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 8759: fflush(ficlog);
1.149 brouard 8760: /* goto end; */
8761: exit(70);
1.126 brouard 8762: }
8763:
8764:
8765:
8766: strcpy(filereso,"o");
1.201 brouard 8767: strcat(filereso,fileresu);
1.126 brouard 8768: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
8769: printf("Problem with Output resultfile: %s\n", filereso);
8770: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
8771: fflush(ficlog);
8772: goto end;
8773: }
8774:
8775: /* Reads comments: lines beginning with '#' */
8776: numlinepar=0;
1.197 brouard 8777:
8778: /* First parameter line */
8779: while(fgets(line, MAXLINE, ficpar)) {
8780: /* If line starts with a # it is a comment */
8781: if (line[0] == '#') {
8782: numlinepar++;
8783: fputs(line,stdout);
8784: fputs(line,ficparo);
8785: fputs(line,ficlog);
8786: continue;
8787: }else
8788: break;
8789: }
8790: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
8791: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
8792: if (num_filled != 5) {
8793: printf("Should be 5 parameters\n");
8794: }
1.126 brouard 8795: numlinepar++;
1.197 brouard 8796: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
8797: }
8798: /* Second parameter line */
8799: while(fgets(line, MAXLINE, ficpar)) {
8800: /* If line starts with a # it is a comment */
8801: if (line[0] == '#') {
8802: numlinepar++;
8803: fputs(line,stdout);
8804: fputs(line,ficparo);
8805: fputs(line,ficlog);
8806: continue;
8807: }else
8808: break;
8809: }
1.223 brouard 8810: 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", \
8811: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
8812: if (num_filled != 11) {
8813: printf("Not 11 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nqv=1 ntv=2 nqtv=1 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
1.209 brouard 8814: printf("but line=%s\n",line);
1.197 brouard 8815: }
1.223 brouard 8816: printf("ftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, mle, weightopt);
1.126 brouard 8817: }
1.203 brouard 8818: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 8819: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 8820: /* Third parameter line */
8821: while(fgets(line, MAXLINE, ficpar)) {
8822: /* If line starts with a # it is a comment */
8823: if (line[0] == '#') {
8824: numlinepar++;
8825: fputs(line,stdout);
8826: fputs(line,ficparo);
8827: fputs(line,ficlog);
8828: continue;
8829: }else
8830: break;
8831: }
1.201 brouard 8832: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
8833: if (num_filled == 0)
8834: model[0]='\0';
8835: else if (num_filled != 1){
1.197 brouard 8836: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
8837: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
8838: model[0]='\0';
8839: goto end;
8840: }
8841: else{
8842: if (model[0]=='+'){
8843: for(i=1; i<=strlen(model);i++)
8844: modeltemp[i-1]=model[i];
1.201 brouard 8845: strcpy(model,modeltemp);
1.197 brouard 8846: }
8847: }
1.199 brouard 8848: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 8849: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 8850: }
8851: /* 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); */
8852: /* numlinepar=numlinepar+3; /\* In general *\/ */
8853: /* printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt,model); */
1.223 brouard 8854: 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);
8855: fprintf(ficlog,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol,stepm,ncovcol, nqv, ntv, nqtv, nlstate,ndeath,maxwav, mle, weightopt,model);
1.126 brouard 8856: fflush(ficlog);
1.190 brouard 8857: /* if(model[0]=='#'|| model[0]== '\0'){ */
8858: if(model[0]=='#'){
1.187 brouard 8859: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
8860: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
8861: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
8862: if(mle != -1){
8863: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
8864: exit(1);
8865: }
8866: }
1.126 brouard 8867: while((c=getc(ficpar))=='#' && c!= EOF){
8868: ungetc(c,ficpar);
8869: fgets(line, MAXLINE, ficpar);
8870: numlinepar++;
1.195 brouard 8871: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
8872: z[0]=line[1];
8873: }
8874: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 8875: fputs(line, stdout);
8876: //puts(line);
1.126 brouard 8877: fputs(line,ficparo);
8878: fputs(line,ficlog);
8879: }
8880: ungetc(c,ficpar);
8881:
8882:
1.145 brouard 8883: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.225 ! brouard 8884: coqvar=matrix(1,nqv,1,n); /**< Fixed quantitative covariate */
! 8885: cotvar=ma3x(1,maxwav,1,ntv,1,n); /**< Time varying covariate */
! 8886: cotqvar=ma3x(1,maxwav,1,nqtv,1,n); /**< Time varying quantitative covariate */
1.136 brouard 8887: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
8888: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
8889: v1+v2*age+v2*v3 makes cptcovn = 3
8890: */
8891: if (strlen(model)>1)
1.187 brouard 8892: ncovmodel=2+nbocc(model,'+')+1; /*Number of variables including intercept and age = cptcovn + intercept + age : v1+v2+v3+v2*v4+v5*age makes 5+2=7,age*age makes 3*/
1.145 brouard 8893: else
1.187 brouard 8894: ncovmodel=2; /* Constant and age */
1.133 brouard 8895: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
8896: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 8897: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
8898: 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);
8899: 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);
8900: fflush(stdout);
8901: fclose (ficlog);
8902: goto end;
8903: }
1.126 brouard 8904: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
8905: delti=delti3[1][1];
8906: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
8907: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
8908: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 8909: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
8910: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 8911: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
8912: fclose (ficparo);
8913: fclose (ficlog);
8914: goto end;
8915: exit(0);
1.220 brouard 8916: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 8917: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 8918: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
8919: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 8920: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
8921: matcov=matrix(1,npar,1,npar);
1.203 brouard 8922: hess=matrix(1,npar,1,npar);
1.220 brouard 8923: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 8924: /* Read guessed parameters */
1.126 brouard 8925: /* Reads comments: lines beginning with '#' */
8926: while((c=getc(ficpar))=='#' && c!= EOF){
8927: ungetc(c,ficpar);
8928: fgets(line, MAXLINE, ficpar);
8929: numlinepar++;
1.141 brouard 8930: fputs(line,stdout);
1.126 brouard 8931: fputs(line,ficparo);
8932: fputs(line,ficlog);
8933: }
8934: ungetc(c,ficpar);
8935:
8936: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
8937: for(i=1; i <=nlstate; i++){
1.220 brouard 8938: j=0;
1.126 brouard 8939: for(jj=1; jj <=nlstate+ndeath; jj++){
1.220 brouard 8940: if(jj==i) continue;
8941: j++;
8942: fscanf(ficpar,"%1d%1d",&i1,&j1);
8943: if ((i1 != i) || (j1 != jj)){
8944: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 8945: It might be a problem of design; if ncovcol and the model are correct\n \
8946: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.220 brouard 8947: exit(1);
8948: }
8949: fprintf(ficparo,"%1d%1d",i1,j1);
8950: if(mle==1)
8951: printf("%1d%1d",i,jj);
8952: fprintf(ficlog,"%1d%1d",i,jj);
8953: for(k=1; k<=ncovmodel;k++){
8954: fscanf(ficpar," %lf",¶m[i][j][k]);
8955: if(mle==1){
8956: printf(" %lf",param[i][j][k]);
8957: fprintf(ficlog," %lf",param[i][j][k]);
8958: }
8959: else
8960: fprintf(ficlog," %lf",param[i][j][k]);
8961: fprintf(ficparo," %lf",param[i][j][k]);
8962: }
8963: fscanf(ficpar,"\n");
8964: numlinepar++;
8965: if(mle==1)
8966: printf("\n");
8967: fprintf(ficlog,"\n");
8968: fprintf(ficparo,"\n");
1.126 brouard 8969: }
8970: }
8971: fflush(ficlog);
8972:
1.145 brouard 8973: /* Reads scales values */
1.126 brouard 8974: p=param[1][1];
8975:
8976: /* Reads comments: lines beginning with '#' */
8977: while((c=getc(ficpar))=='#' && c!= EOF){
8978: ungetc(c,ficpar);
8979: fgets(line, MAXLINE, ficpar);
8980: numlinepar++;
1.141 brouard 8981: fputs(line,stdout);
1.126 brouard 8982: fputs(line,ficparo);
8983: fputs(line,ficlog);
8984: }
8985: ungetc(c,ficpar);
8986:
8987: for(i=1; i <=nlstate; i++){
8988: for(j=1; j <=nlstate+ndeath-1; j++){
1.220 brouard 8989: fscanf(ficpar,"%1d%1d",&i1,&j1);
8990: if ( (i1-i) * (j1-j) != 0){
8991: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
8992: exit(1);
8993: }
8994: printf("%1d%1d",i,j);
8995: fprintf(ficparo,"%1d%1d",i1,j1);
8996: fprintf(ficlog,"%1d%1d",i1,j1);
8997: for(k=1; k<=ncovmodel;k++){
8998: fscanf(ficpar,"%le",&delti3[i][j][k]);
8999: printf(" %le",delti3[i][j][k]);
9000: fprintf(ficparo," %le",delti3[i][j][k]);
9001: fprintf(ficlog," %le",delti3[i][j][k]);
9002: }
9003: fscanf(ficpar,"\n");
9004: numlinepar++;
9005: printf("\n");
9006: fprintf(ficparo,"\n");
9007: fprintf(ficlog,"\n");
1.126 brouard 9008: }
9009: }
9010: fflush(ficlog);
1.220 brouard 9011:
1.145 brouard 9012: /* Reads covariance matrix */
1.126 brouard 9013: delti=delti3[1][1];
1.220 brouard 9014:
9015:
1.126 brouard 9016: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
1.220 brouard 9017:
1.126 brouard 9018: /* Reads comments: lines beginning with '#' */
9019: while((c=getc(ficpar))=='#' && c!= EOF){
9020: ungetc(c,ficpar);
9021: fgets(line, MAXLINE, ficpar);
9022: numlinepar++;
1.141 brouard 9023: fputs(line,stdout);
1.126 brouard 9024: fputs(line,ficparo);
9025: fputs(line,ficlog);
9026: }
9027: ungetc(c,ficpar);
1.220 brouard 9028:
1.126 brouard 9029: matcov=matrix(1,npar,1,npar);
1.203 brouard 9030: hess=matrix(1,npar,1,npar);
1.131 brouard 9031: for(i=1; i <=npar; i++)
9032: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 9033:
1.194 brouard 9034: /* Scans npar lines */
1.126 brouard 9035: for(i=1; i <=npar; i++){
1.194 brouard 9036: count=fscanf(ficpar,"%1d%1d%1d",&i1,&j1,&jk);
9037: if(count != 3){
1.220 brouard 9038: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9039: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9040: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.220 brouard 9041: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9042: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9043: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.220 brouard 9044: exit(1);
9045: }else{
9046: if(mle==1)
9047: printf("%1d%1d%1d",i1,j1,jk);
9048: }
1.194 brouard 9049: fprintf(ficlog,"%1d%1d%1d",i1,j1,jk);
9050: fprintf(ficparo,"%1d%1d%1d",i1,j1,jk);
1.126 brouard 9051: for(j=1; j <=i; j++){
1.220 brouard 9052: fscanf(ficpar," %le",&matcov[i][j]);
9053: if(mle==1){
9054: printf(" %.5le",matcov[i][j]);
9055: }
9056: fprintf(ficlog," %.5le",matcov[i][j]);
9057: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 9058: }
9059: fscanf(ficpar,"\n");
9060: numlinepar++;
9061: if(mle==1)
1.220 brouard 9062: printf("\n");
1.126 brouard 9063: fprintf(ficlog,"\n");
9064: fprintf(ficparo,"\n");
9065: }
1.194 brouard 9066: /* End of read covariance matrix npar lines */
1.126 brouard 9067: for(i=1; i <=npar; i++)
9068: for(j=i+1;j<=npar;j++)
1.220 brouard 9069: matcov[i][j]=matcov[j][i];
1.126 brouard 9070:
9071: if(mle==1)
9072: printf("\n");
9073: fprintf(ficlog,"\n");
9074:
9075: fflush(ficlog);
9076:
9077: /*-------- Rewriting parameter file ----------*/
9078: strcpy(rfileres,"r"); /* "Rparameterfile */
9079: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
9080: strcat(rfileres,"."); /* */
9081: strcat(rfileres,optionfilext); /* Other files have txt extension */
9082: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 9083: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
9084: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 9085: }
9086: fprintf(ficres,"#%s\n",version);
9087: } /* End of mle != -3 */
1.218 brouard 9088:
1.186 brouard 9089: /* Main data
9090: */
1.126 brouard 9091: n= lastobs;
9092: num=lvector(1,n);
9093: moisnais=vector(1,n);
9094: annais=vector(1,n);
9095: moisdc=vector(1,n);
9096: andc=vector(1,n);
1.220 brouard 9097: weight=vector(1,n);
1.126 brouard 9098: agedc=vector(1,n);
9099: cod=ivector(1,n);
1.220 brouard 9100: for(i=1;i<=n;i++){
9101: num[i]=0;
9102: moisnais[i]=0;
9103: annais[i]=0;
9104: moisdc[i]=0;
9105: andc[i]=0;
9106: agedc[i]=0;
9107: cod[i]=0;
9108: weight[i]=1.0; /* Equal weights, 1 by default */
9109: }
1.126 brouard 9110: mint=matrix(1,maxwav,1,n);
9111: anint=matrix(1,maxwav,1,n);
1.131 brouard 9112: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 9113: tab=ivector(1,NCOVMAX);
1.144 brouard 9114: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 9115: ncodemaxwundef=ivector(1,NCOVMAX); /* Number of code per covariate; if - 1 O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.126 brouard 9116:
1.136 brouard 9117: /* Reads data from file datafile */
9118: if (readdata(datafile, firstobs, lastobs, &imx)==1)
9119: goto end;
9120:
9121: /* Calculation of the number of parameters from char model */
1.137 brouard 9122: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
9123: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
9124: k=3 V4 Tvar[k=3]= 4 (from V4)
9125: k=2 V1 Tvar[k=2]= 1 (from V1)
9126: k=1 Tvar[1]=2 (from V2)
9127: */
9128: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
1.225 ! brouard 9129: Typevar=ivector(1,NCOVMAX); /* -1 to 4 */
1.137 brouard 9130: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
9131: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
9132: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
9133: */
9134: /* For model-covariate k tells which data-covariate to use but
9135: because this model-covariate is a construction we invent a new column
9136: ncovcol + k1
9137: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
9138: Tvar[3=V1*V4]=4+1 etc */
1.145 brouard 9139: Tprod=ivector(1,NCOVMAX); /* Gives the position of a product */
1.137 brouard 9140: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
9141: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
9142: */
1.145 brouard 9143: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
9144: Tvard=imatrix(1,NCOVMAX,1,2); /* n=Tvard[k1][1] and m=Tvard[k1][2] gives the couple n,m of the k1 th product Vn*Vm
1.141 brouard 9145: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
9146: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 9147: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 9148: 4 covariates (3 plus signs)
9149: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
9150: */
1.136 brouard 9151:
1.186 brouard 9152: /* Main decodemodel */
9153:
1.187 brouard 9154:
1.223 brouard 9155: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 9156: goto end;
9157:
1.137 brouard 9158: if((double)(lastobs-imx)/(double)imx > 1.10){
9159: nbwarn++;
9160: 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);
9161: 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);
9162: }
1.136 brouard 9163: /* if(mle==1){*/
1.137 brouard 9164: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
9165: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 9166: }
9167:
9168: /*-calculation of age at interview from date of interview and age at death -*/
9169: agev=matrix(1,maxwav,1,imx);
9170:
9171: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
9172: goto end;
9173:
1.126 brouard 9174:
1.136 brouard 9175: agegomp=(int)agemin;
9176: free_vector(moisnais,1,n);
9177: free_vector(annais,1,n);
1.126 brouard 9178: /* free_matrix(mint,1,maxwav,1,n);
9179: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 9180: /* free_vector(moisdc,1,n); */
9181: /* free_vector(andc,1,n); */
1.145 brouard 9182: /* */
9183:
1.126 brouard 9184: wav=ivector(1,imx);
1.214 brouard 9185: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
9186: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
9187: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
9188: 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.*/
9189: bh=imatrix(1,lastpass-firstpass+2,1,imx);
9190: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 9191:
9192: /* Concatenates waves */
1.214 brouard 9193: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
9194: Death is a valid wave (if date is known).
9195: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
9196: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
9197: and mw[mi+1][i]. dh depends on stepm.
9198: */
9199:
1.126 brouard 9200: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 9201: /* */
9202:
1.215 brouard 9203: free_vector(moisdc,1,n);
9204: free_vector(andc,1,n);
9205:
1.126 brouard 9206: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
9207: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
9208: ncodemax[1]=1;
1.145 brouard 9209: Ndum =ivector(-1,NCOVMAX);
1.225 ! brouard 9210: cptcoveff=0;
1.220 brouard 9211: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
9212: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
9213: }
9214:
9215: ncovcombmax=pow(2,cptcoveff);
9216: invalidvarcomb=ivector(1, ncovcombmax);
9217: for(i=1;i<ncovcombmax;i++)
9218: invalidvarcomb[i]=0;
9219:
1.211 brouard 9220: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 9221: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 9222: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.145 brouard 9223:
1.200 brouard 9224: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 9225: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 9226: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 9227: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
9228: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
9229: * (currently 0 or 1) in the data.
9230: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
9231: * corresponding modality (h,j).
9232: */
9233:
1.145 brouard 9234: h=0;
9235: /*if (cptcovn > 0) */
1.126 brouard 9236: m=pow(2,cptcoveff);
9237:
1.144 brouard 9238: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 9239: * For k=4 covariates, h goes from 1 to m=2**k
9240: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
9241: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 9242: * h\k 1 2 3 4
1.143 brouard 9243: *______________________________
9244: * 1 i=1 1 i=1 1 i=1 1 i=1 1
9245: * 2 2 1 1 1
9246: * 3 i=2 1 2 1 1
9247: * 4 2 2 1 1
9248: * 5 i=3 1 i=2 1 2 1
9249: * 6 2 1 2 1
9250: * 7 i=4 1 2 2 1
9251: * 8 2 2 2 1
1.197 brouard 9252: * 9 i=5 1 i=3 1 i=2 1 2
9253: * 10 2 1 1 2
9254: * 11 i=6 1 2 1 2
9255: * 12 2 2 1 2
9256: * 13 i=7 1 i=4 1 2 2
9257: * 14 2 1 2 2
9258: * 15 i=8 1 2 2 2
9259: * 16 2 2 2 2
1.143 brouard 9260: */
1.212 brouard 9261: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 9262: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
9263: * and the value of each covariate?
9264: * V1=1, V2=1, V3=2, V4=1 ?
9265: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
9266: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
9267: * In order to get the real value in the data, we use nbcode
9268: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
9269: * We are keeping this crazy system in order to be able (in the future?)
9270: * to have more than 2 values (0 or 1) for a covariate.
9271: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
9272: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
9273: * bbbbbbbb
9274: * 76543210
9275: * h-1 00000101 (6-1=5)
1.219 brouard 9276: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 9277: * &
9278: * 1 00000001 (1)
1.219 brouard 9279: * 00000000 = 1 & ((h-1) >> (k-1))
9280: * +1= 00000001 =1
1.211 brouard 9281: *
9282: * h=14, k=3 => h'=h-1=13, k'=k-1=2
9283: * h' 1101 =2^3+2^2+0x2^1+2^0
9284: * >>k' 11
9285: * & 00000001
9286: * = 00000001
9287: * +1 = 00000010=2 = codtabm(14,3)
9288: * Reverse h=6 and m=16?
9289: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
9290: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
9291: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
9292: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
9293: * V3=decodtabm(14,3,2**4)=2
9294: * h'=13 1101 =2^3+2^2+0x2^1+2^0
9295: *(h-1) >> (j-1) 0011 =13 >> 2
9296: * &1 000000001
9297: * = 000000001
9298: * +1= 000000010 =2
9299: * 2211
9300: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
9301: * V3=2
1.220 brouard 9302: * codtabm and decodtabm are identical
1.211 brouard 9303: */
9304:
1.145 brouard 9305:
9306: free_ivector(Ndum,-1,NCOVMAX);
9307:
9308:
1.126 brouard 9309:
1.186 brouard 9310: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 9311: strcpy(optionfilegnuplot,optionfilefiname);
9312: if(mle==-3)
1.201 brouard 9313: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 9314: strcat(optionfilegnuplot,".gp");
9315:
9316: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
9317: printf("Problem with file %s",optionfilegnuplot);
9318: }
9319: else{
1.204 brouard 9320: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 9321: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 9322: //fprintf(ficgp,"set missing 'NaNq'\n");
9323: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 9324: }
9325: /* fclose(ficgp);*/
1.186 brouard 9326:
9327:
9328: /* Initialisation of --------- index.htm --------*/
1.126 brouard 9329:
9330: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
9331: if(mle==-3)
1.201 brouard 9332: strcat(optionfilehtm,"-MORT_");
1.126 brouard 9333: strcat(optionfilehtm,".htm");
9334: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 9335: printf("Problem with %s \n",optionfilehtm);
9336: exit(0);
1.126 brouard 9337: }
9338:
9339: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
9340: strcat(optionfilehtmcov,"-cov.htm");
9341: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
9342: printf("Problem with %s \n",optionfilehtmcov), exit(0);
9343: }
9344: else{
9345: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
9346: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9347: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 9348: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
9349: }
9350:
1.213 brouard 9351: fprintf(fichtm,"<html><head>\n<head>\n<meta charset=\"utf-8\"/><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n<title>IMaCh %s</title></head>\n <body><font size=\"7\"><a href=http:/euroreves.ined.fr/imach>IMaCh for Interpolated Markov Chain</a> </font><br>\n<font size=\"3\">Sponsored by Copyright (C) 2002-2015 <a href=http://www.ined.fr>INED</a>-EUROREVES-Institut de longévité-2013-2016-Japan Society for the Promotion of Sciences 日本学術振興会 (<a href=https://www.jsps.go.jp/english/e-grants/>Grant-in-Aid for Scientific Research 25293121</a>) - <a href=https://software.intel.com/en-us>Intel Software 2015-2018</a></font><br> \
1.204 brouard 9352: <hr size=\"2\" color=\"#EC5E5E\"> \n\
9353: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 9354: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9355: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 9356: \n\
9357: <hr size=\"2\" color=\"#EC5E5E\">\
9358: <ul><li><h4>Parameter files</h4>\n\
9359: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
9360: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
9361: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
9362: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
9363: - Date and time at start: %s</ul>\n",\
9364: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
9365: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
9366: fileres,fileres,\
9367: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
9368: fflush(fichtm);
9369:
9370: strcpy(pathr,path);
9371: strcat(pathr,optionfilefiname);
1.184 brouard 9372: #ifdef WIN32
9373: _chdir(optionfilefiname); /* Move to directory named optionfile */
9374: #else
1.126 brouard 9375: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 9376: #endif
9377:
1.126 brouard 9378:
1.220 brouard 9379: /* Calculates basic frequencies. Computes observed prevalence at single age
9380: and for any valid combination of covariates
1.126 brouard 9381: and prints on file fileres'p'. */
1.220 brouard 9382: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.223 brouard 9383: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 9384:
9385: fprintf(fichtm,"\n");
9386: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
9387: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
9388: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
9389: imx,agemin,agemax,jmin,jmax,jmean);
9390: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.220 brouard 9391: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9392: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9393: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9394: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 9395:
1.126 brouard 9396: /* For Powell, parameters are in a vector p[] starting at p[1]
9397: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
9398: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
9399:
9400: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 9401: /* For mortality only */
1.126 brouard 9402: if (mle==-3){
1.136 brouard 9403: ximort=matrix(1,NDIM,1,NDIM);
1.220 brouard 9404: for(i=1;i<=NDIM;i++)
9405: for(j=1;j<=NDIM;j++)
9406: ximort[i][j]=0.;
1.186 brouard 9407: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 9408: cens=ivector(1,n);
9409: ageexmed=vector(1,n);
9410: agecens=vector(1,n);
9411: dcwave=ivector(1,n);
1.223 brouard 9412:
1.126 brouard 9413: for (i=1; i<=imx; i++){
9414: dcwave[i]=-1;
9415: for (m=firstpass; m<=lastpass; m++)
1.218 brouard 9416: if (s[m][i]>nlstate) {
9417: dcwave[i]=m;
9418: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
9419: break;
9420: }
1.126 brouard 9421: }
1.218 brouard 9422:
1.126 brouard 9423: for (i=1; i<=imx; i++) {
9424: if (wav[i]>0){
1.218 brouard 9425: ageexmed[i]=agev[mw[1][i]][i];
9426: j=wav[i];
9427: agecens[i]=1.;
9428:
9429: if (ageexmed[i]> 1 && wav[i] > 0){
9430: agecens[i]=agev[mw[j][i]][i];
9431: cens[i]= 1;
9432: }else if (ageexmed[i]< 1)
9433: cens[i]= -1;
9434: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
9435: cens[i]=0 ;
1.126 brouard 9436: }
9437: else cens[i]=-1;
9438: }
9439:
9440: for (i=1;i<=NDIM;i++) {
9441: for (j=1;j<=NDIM;j++)
1.218 brouard 9442: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 9443: }
9444:
1.145 brouard 9445: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 9446: /*printf("%lf %lf", p[1], p[2]);*/
9447:
9448:
1.136 brouard 9449: #ifdef GSL
9450: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 9451: #else
1.126 brouard 9452: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 9453: #endif
1.201 brouard 9454: strcpy(filerespow,"POW-MORT_");
9455: strcat(filerespow,fileresu);
1.126 brouard 9456: if((ficrespow=fopen(filerespow,"w"))==NULL) {
9457: printf("Problem with resultfile: %s\n", filerespow);
9458: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
9459: }
1.136 brouard 9460: #ifdef GSL
9461: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 9462: #else
1.126 brouard 9463: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 9464: #endif
1.126 brouard 9465: /* for (i=1;i<=nlstate;i++)
9466: for(j=1;j<=nlstate+ndeath;j++)
9467: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
9468: */
9469: fprintf(ficrespow,"\n");
1.136 brouard 9470: #ifdef GSL
9471: /* gsl starts here */
9472: T = gsl_multimin_fminimizer_nmsimplex;
9473: gsl_multimin_fminimizer *sfm = NULL;
9474: gsl_vector *ss, *x;
9475: gsl_multimin_function minex_func;
9476:
9477: /* Initial vertex size vector */
9478: ss = gsl_vector_alloc (NDIM);
9479:
9480: if (ss == NULL){
9481: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
9482: }
9483: /* Set all step sizes to 1 */
9484: gsl_vector_set_all (ss, 0.001);
9485:
9486: /* Starting point */
1.126 brouard 9487:
1.136 brouard 9488: x = gsl_vector_alloc (NDIM);
9489:
9490: if (x == NULL){
9491: gsl_vector_free(ss);
9492: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
9493: }
9494:
9495: /* Initialize method and iterate */
9496: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 9497: /* gsl_vector_set(x, 0, 0.0268); */
9498: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 9499: gsl_vector_set(x, 0, p[1]);
9500: gsl_vector_set(x, 1, p[2]);
9501:
9502: minex_func.f = &gompertz_f;
9503: minex_func.n = NDIM;
9504: minex_func.params = (void *)&p; /* ??? */
9505:
9506: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
9507: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
9508:
9509: printf("Iterations beginning .....\n\n");
9510: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
9511:
9512: iteri=0;
9513: while (rval == GSL_CONTINUE){
9514: iteri++;
9515: status = gsl_multimin_fminimizer_iterate(sfm);
9516:
9517: if (status) printf("error: %s\n", gsl_strerror (status));
9518: fflush(0);
9519:
9520: if (status)
9521: break;
9522:
9523: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
9524: ssval = gsl_multimin_fminimizer_size (sfm);
9525:
9526: if (rval == GSL_SUCCESS)
9527: printf ("converged to a local maximum at\n");
9528:
9529: printf("%5d ", iteri);
9530: for (it = 0; it < NDIM; it++){
9531: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
9532: }
9533: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
9534: }
9535:
9536: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
9537:
9538: gsl_vector_free(x); /* initial values */
9539: gsl_vector_free(ss); /* inital step size */
9540: for (it=0; it<NDIM; it++){
9541: p[it+1]=gsl_vector_get(sfm->x,it);
9542: fprintf(ficrespow," %.12lf", p[it]);
9543: }
9544: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
9545: #endif
9546: #ifdef POWELL
9547: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
9548: #endif
1.126 brouard 9549: fclose(ficrespow);
9550:
1.203 brouard 9551: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 9552:
9553: for(i=1; i <=NDIM; i++)
9554: for(j=i+1;j<=NDIM;j++)
1.220 brouard 9555: matcov[i][j]=matcov[j][i];
1.126 brouard 9556:
9557: printf("\nCovariance matrix\n ");
1.203 brouard 9558: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 9559: for(i=1; i <=NDIM; i++) {
9560: for(j=1;j<=NDIM;j++){
1.220 brouard 9561: printf("%f ",matcov[i][j]);
9562: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 9563: }
1.203 brouard 9564: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 9565: }
9566:
9567: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 9568: for (i=1;i<=NDIM;i++) {
1.126 brouard 9569: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 9570: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
9571: }
1.126 brouard 9572: lsurv=vector(1,AGESUP);
9573: lpop=vector(1,AGESUP);
9574: tpop=vector(1,AGESUP);
9575: lsurv[agegomp]=100000;
9576:
9577: for (k=agegomp;k<=AGESUP;k++) {
9578: agemortsup=k;
9579: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
9580: }
9581:
9582: for (k=agegomp;k<agemortsup;k++)
9583: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
9584:
9585: for (k=agegomp;k<agemortsup;k++){
9586: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
9587: sumlpop=sumlpop+lpop[k];
9588: }
9589:
9590: tpop[agegomp]=sumlpop;
9591: for (k=agegomp;k<(agemortsup-3);k++){
9592: /* tpop[k+1]=2;*/
9593: tpop[k+1]=tpop[k]-lpop[k];
9594: }
9595:
9596:
9597: printf("\nAge lx qx dx Lx Tx e(x)\n");
9598: for (k=agegomp;k<(agemortsup-2);k++)
9599: 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]);
9600:
9601:
9602: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 9603: ageminpar=50;
9604: agemaxpar=100;
1.194 brouard 9605: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
9606: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9607: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9608: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
9609: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9610: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9611: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 9612: }else{
9613: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
9614: fprintf(ficlog,"Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
1.201 brouard 9615: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 9616: }
1.201 brouard 9617: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 9618: stepm, weightopt,\
9619: model,imx,p,matcov,agemortsup);
9620:
9621: free_vector(lsurv,1,AGESUP);
9622: free_vector(lpop,1,AGESUP);
9623: free_vector(tpop,1,AGESUP);
1.220 brouard 9624: free_matrix(ximort,1,NDIM,1,NDIM);
1.136 brouard 9625: free_ivector(cens,1,n);
9626: free_vector(agecens,1,n);
9627: free_ivector(dcwave,1,n);
1.220 brouard 9628: #ifdef GSL
1.136 brouard 9629: #endif
1.186 brouard 9630: } /* Endof if mle==-3 mortality only */
1.205 brouard 9631: /* Standard */
9632: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
9633: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
9634: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 9635: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 9636: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
9637: for (k=1; k<=npar;k++)
9638: printf(" %d %8.5f",k,p[k]);
9639: printf("\n");
1.205 brouard 9640: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
9641: /* mlikeli uses func not funcone */
9642: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
9643: }
9644: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
9645: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
9646: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
9647: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
9648: }
9649: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 9650: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
9651: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
9652: for (k=1; k<=npar;k++)
9653: printf(" %d %8.5f",k,p[k]);
9654: printf("\n");
9655:
9656: /*--------- results files --------------*/
1.224 brouard 9657: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nqv=%d ntv=%d nqtv=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nqv, ntv, nqtv, nlstate, ndeath, maxwav, weightopt,model);
1.126 brouard 9658:
9659:
9660: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9661: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9662: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9663: for(i=1,jk=1; i <=nlstate; i++){
9664: for(k=1; k <=(nlstate+ndeath); k++){
1.225 ! brouard 9665: if (k != i) {
! 9666: printf("%d%d ",i,k);
! 9667: fprintf(ficlog,"%d%d ",i,k);
! 9668: fprintf(ficres,"%1d%1d ",i,k);
! 9669: for(j=1; j <=ncovmodel; j++){
! 9670: printf("%12.7f ",p[jk]);
! 9671: fprintf(ficlog,"%12.7f ",p[jk]);
! 9672: fprintf(ficres,"%12.7f ",p[jk]);
! 9673: jk++;
! 9674: }
! 9675: printf("\n");
! 9676: fprintf(ficlog,"\n");
! 9677: fprintf(ficres,"\n");
! 9678: }
1.126 brouard 9679: }
9680: }
1.203 brouard 9681: if(mle != 0){
9682: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 9683: ftolhess=ftol; /* Usually correct */
1.203 brouard 9684: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
9685: 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");
9686: 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");
9687: for(i=1,jk=1; i <=nlstate; i++){
1.225 ! brouard 9688: for(k=1; k <=(nlstate+ndeath); k++){
! 9689: if (k != i) {
! 9690: printf("%d%d ",i,k);
! 9691: fprintf(ficlog,"%d%d ",i,k);
! 9692: for(j=1; j <=ncovmodel; j++){
! 9693: 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]));
! 9694: 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]));
! 9695: jk++;
! 9696: }
! 9697: printf("\n");
! 9698: fprintf(ficlog,"\n");
! 9699: }
! 9700: }
1.193 brouard 9701: }
1.203 brouard 9702: } /* end of hesscov and Wald tests */
1.225 ! brouard 9703:
1.203 brouard 9704: /* */
1.126 brouard 9705: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
9706: printf("# Scales (for hessian or gradient estimation)\n");
9707: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
9708: for(i=1,jk=1; i <=nlstate; i++){
9709: for(j=1; j <=nlstate+ndeath; j++){
1.225 ! brouard 9710: if (j!=i) {
! 9711: fprintf(ficres,"%1d%1d",i,j);
! 9712: printf("%1d%1d",i,j);
! 9713: fprintf(ficlog,"%1d%1d",i,j);
! 9714: for(k=1; k<=ncovmodel;k++){
! 9715: printf(" %.5e",delti[jk]);
! 9716: fprintf(ficlog," %.5e",delti[jk]);
! 9717: fprintf(ficres," %.5e",delti[jk]);
! 9718: jk++;
! 9719: }
! 9720: printf("\n");
! 9721: fprintf(ficlog,"\n");
! 9722: fprintf(ficres,"\n");
! 9723: }
1.126 brouard 9724: }
9725: }
9726:
9727: fprintf(ficres,"# Covariance matrix \n# 121 Var(a12)\n# 122 Cov(b12,a12) Var(b12)\n# ...\n# 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n");
1.203 brouard 9728: if(mle >= 1) /* To big for the screen */
1.126 brouard 9729: 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");
9730: 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");
9731: /* # 121 Var(a12)\n\ */
9732: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9733: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9734: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9735: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9736: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9737: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9738: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9739:
9740:
9741: /* Just to have a covariance matrix which will be more understandable
9742: even is we still don't want to manage dictionary of variables
9743: */
9744: for(itimes=1;itimes<=2;itimes++){
9745: jj=0;
9746: for(i=1; i <=nlstate; i++){
1.225 ! brouard 9747: for(j=1; j <=nlstate+ndeath; j++){
! 9748: if(j==i) continue;
! 9749: for(k=1; k<=ncovmodel;k++){
! 9750: jj++;
! 9751: ca[0]= k+'a'-1;ca[1]='\0';
! 9752: if(itimes==1){
! 9753: if(mle>=1)
! 9754: printf("#%1d%1d%d",i,j,k);
! 9755: fprintf(ficlog,"#%1d%1d%d",i,j,k);
! 9756: fprintf(ficres,"#%1d%1d%d",i,j,k);
! 9757: }else{
! 9758: if(mle>=1)
! 9759: printf("%1d%1d%d",i,j,k);
! 9760: fprintf(ficlog,"%1d%1d%d",i,j,k);
! 9761: fprintf(ficres,"%1d%1d%d",i,j,k);
! 9762: }
! 9763: ll=0;
! 9764: for(li=1;li <=nlstate; li++){
! 9765: for(lj=1;lj <=nlstate+ndeath; lj++){
! 9766: if(lj==li) continue;
! 9767: for(lk=1;lk<=ncovmodel;lk++){
! 9768: ll++;
! 9769: if(ll<=jj){
! 9770: cb[0]= lk +'a'-1;cb[1]='\0';
! 9771: if(ll<jj){
! 9772: if(itimes==1){
! 9773: if(mle>=1)
! 9774: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 9775: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 9776: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
! 9777: }else{
! 9778: if(mle>=1)
! 9779: printf(" %.5e",matcov[jj][ll]);
! 9780: fprintf(ficlog," %.5e",matcov[jj][ll]);
! 9781: fprintf(ficres," %.5e",matcov[jj][ll]);
! 9782: }
! 9783: }else{
! 9784: if(itimes==1){
! 9785: if(mle>=1)
! 9786: printf(" Var(%s%1d%1d)",ca,i,j);
! 9787: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
! 9788: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
! 9789: }else{
! 9790: if(mle>=1)
! 9791: printf(" %.7e",matcov[jj][ll]);
! 9792: fprintf(ficlog," %.7e",matcov[jj][ll]);
! 9793: fprintf(ficres," %.7e",matcov[jj][ll]);
! 9794: }
! 9795: }
! 9796: }
! 9797: } /* end lk */
! 9798: } /* end lj */
! 9799: } /* end li */
! 9800: if(mle>=1)
! 9801: printf("\n");
! 9802: fprintf(ficlog,"\n");
! 9803: fprintf(ficres,"\n");
! 9804: numlinepar++;
! 9805: } /* end k*/
! 9806: } /*end j */
1.126 brouard 9807: } /* end i */
9808: } /* end itimes */
9809:
9810: fflush(ficlog);
9811: fflush(ficres);
1.225 ! brouard 9812: while(fgets(line, MAXLINE, ficpar)) {
! 9813: /* If line starts with a # it is a comment */
! 9814: if (line[0] == '#') {
! 9815: numlinepar++;
! 9816: fputs(line,stdout);
! 9817: fputs(line,ficparo);
! 9818: fputs(line,ficlog);
! 9819: continue;
! 9820: }else
! 9821: break;
! 9822: }
! 9823:
1.209 brouard 9824: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
9825: /* ungetc(c,ficpar); */
9826: /* fgets(line, MAXLINE, ficpar); */
9827: /* fputs(line,stdout); */
9828: /* fputs(line,ficparo); */
9829: /* } */
9830: /* ungetc(c,ficpar); */
1.126 brouard 9831:
9832: estepm=0;
1.209 brouard 9833: if((num_filled=sscanf(line,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm, &ftolpl)) !=EOF){
1.225 ! brouard 9834:
! 9835: if (num_filled != 6) {
! 9836: 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);
! 9837: 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);
! 9838: goto end;
! 9839: }
! 9840: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
! 9841: }
! 9842: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
! 9843: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
! 9844:
1.209 brouard 9845: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 9846: if (estepm==0 || estepm < stepm) estepm=stepm;
9847: if (fage <= 2) {
9848: bage = ageminpar;
9849: fage = agemaxpar;
9850: }
9851:
9852: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 9853: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
9854: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 9855:
1.186 brouard 9856: /* Other stuffs, more or less useful */
1.126 brouard 9857: while((c=getc(ficpar))=='#' && c!= EOF){
9858: ungetc(c,ficpar);
9859: fgets(line, MAXLINE, ficpar);
1.141 brouard 9860: fputs(line,stdout);
1.126 brouard 9861: fputs(line,ficparo);
9862: }
9863: ungetc(c,ficpar);
9864:
9865: fscanf(ficpar,"begin-prev-date=%lf/%lf/%lf end-prev-date=%lf/%lf/%lf mov_average=%d\n",&jprev1, &mprev1,&anprev1,&jprev2, &mprev2,&anprev2,&mobilav);
9866: 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);
9867: 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);
9868: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
9869: 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);
9870:
9871: while((c=getc(ficpar))=='#' && c!= EOF){
9872: ungetc(c,ficpar);
9873: fgets(line, MAXLINE, ficpar);
1.141 brouard 9874: fputs(line,stdout);
1.126 brouard 9875: fputs(line,ficparo);
9876: }
9877: ungetc(c,ficpar);
9878:
9879:
9880: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
9881: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
9882:
9883: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 9884: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 9885: fprintf(ficparo,"pop_based=%d\n",popbased);
9886: fprintf(ficres,"pop_based=%d\n",popbased);
9887:
9888: while((c=getc(ficpar))=='#' && c!= EOF){
9889: ungetc(c,ficpar);
9890: fgets(line, MAXLINE, ficpar);
1.141 brouard 9891: fputs(line,stdout);
1.126 brouard 9892: fputs(line,ficparo);
9893: }
9894: ungetc(c,ficpar);
9895:
9896: fscanf(ficpar,"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);
9897: 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);
9898: 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);
9899: 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);
9900: 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);
9901: /* day and month of proj2 are not used but only year anproj2.*/
9902:
1.217 brouard 9903: while((c=getc(ficpar))=='#' && c!= EOF){
9904: ungetc(c,ficpar);
9905: fgets(line, MAXLINE, ficpar);
9906: fputs(line,stdout);
9907: fputs(line,ficparo);
9908: }
9909: ungetc(c,ficpar);
9910:
9911: fscanf(ficpar,"backcast=%d starting-back-date=%lf/%lf/%lf final-back-date=%lf/%lf/%lf mobil_average=%d\n",&backcast,&jback1,&mback1,&anback1,&jback2,&mback2,&anback2,&mobilavproj);
1.223 brouard 9912: fprintf(ficparo,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
9913: fprintf(ficlog,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
9914: fprintf(ficres,"backcast=%d starting-back-date=%.lf/%.lf/%.lf final-back-date=%.lf/%.lf/%.lf mobil_average=%d\n",backcast,jback1,mback1,anback1,jback2,mback2,anback2,mobilavproj);
1.217 brouard 9915: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 9916:
9917:
1.220 brouard 9918: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 9919: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 9920:
9921: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 9922: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.220 brouard 9923: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 9924: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9925: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 9926: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 9927: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9928: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 9929: }else{
1.218 brouard 9930: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.220 brouard 9931: }
9932: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.225 ! brouard 9933: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
! 9934: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.220 brouard 9935:
1.225 ! brouard 9936: /*------------ free_vector -------------*/
! 9937: /* chdir(path); */
1.220 brouard 9938:
1.215 brouard 9939: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
9940: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
9941: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
9942: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 9943: free_lvector(num,1,n);
9944: free_vector(agedc,1,n);
9945: /*free_matrix(covar,0,NCOVMAX,1,n);*/
9946: /*free_matrix(covar,1,NCOVMAX,1,n);*/
9947: fclose(ficparo);
9948: fclose(ficres);
1.220 brouard 9949:
9950:
1.186 brouard 9951: /* Other results (useful)*/
1.220 brouard 9952:
9953:
1.126 brouard 9954: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 9955: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
9956: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 9957: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 9958: fclose(ficrespl);
9959:
9960: /*------------- h Pij x at various ages ------------*/
1.180 brouard 9961: /*#include "hpijx.h"*/
9962: hPijx(p, bage, fage);
1.145 brouard 9963: fclose(ficrespij);
1.126 brouard 9964:
1.220 brouard 9965: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 9966: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 9967: k=1;
1.126 brouard 9968: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
9969:
1.219 brouard 9970: /* Prevalence for each covariates in probs[age][status][cov] */
1.218 brouard 9971: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 9972: for(i=1;i<=AGESUP;i++)
1.219 brouard 9973: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 ! brouard 9974: for(k=1;k<=ncovcombmax;k++)
! 9975: probs[i][j][k]=0.;
1.219 brouard 9976: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
9977: if (mobilav!=0 ||mobilavproj !=0 ) {
9978: mobaverages= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
9979: for(i=1;i<=AGESUP;i++)
9980: for(j=1;j<=nlstate;j++)
9981: for(k=1;k<=ncovcombmax;k++)
9982: mobaverages[i][j][k]=0.;
9983: mobaverage=mobaverages;
9984: if (mobilav!=0) {
1.218 brouard 9985: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
9986: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
9987: printf(" Error in movingaverage mobilav=%d\n",mobilav);
9988: }
1.219 brouard 9989: }
9990: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
9991: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
9992: else if (mobilavproj !=0) {
1.218 brouard 9993: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
9994: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
9995: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
9996: }
1.219 brouard 9997: }
9998: }/* end if moving average */
9999:
1.126 brouard 10000: /*---------- Forecasting ------------------*/
10001: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
10002: if(prevfcast==1){
10003: /* if(stepm ==1){*/
1.225 ! brouard 10004: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 10005: }
1.217 brouard 10006: if(backcast==1){
1.219 brouard 10007: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10008: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10009: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10010:
10011: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
10012:
10013: bprlim=matrix(1,nlstate,1,nlstate);
10014: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
10015: fclose(ficresplb);
10016:
1.222 brouard 10017: hBijx(p, bage, fage, mobaverage);
10018: fclose(ficrespijb);
1.219 brouard 10019: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
10020:
10021: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj,
1.225 ! brouard 10022: bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
1.219 brouard 10023: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10024: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10025: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10026: }
1.217 brouard 10027:
1.186 brouard 10028:
10029: /* ------ Other prevalence ratios------------ */
1.126 brouard 10030:
1.215 brouard 10031: free_ivector(wav,1,imx);
10032: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
10033: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
10034: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 10035:
10036:
1.127 brouard 10037: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 10038:
1.201 brouard 10039: strcpy(filerese,"E_");
10040: strcat(filerese,fileresu);
1.126 brouard 10041: if((ficreseij=fopen(filerese,"w"))==NULL) {
10042: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10043: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10044: }
1.208 brouard 10045: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
10046: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.219 brouard 10047:
1.225 ! brouard 10048: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.219 brouard 10049: fprintf(ficreseij,"\n#****** ");
1.225 ! brouard 10050: for(j=1;j<=cptcoveff;j++) {
1.220 brouard 10051: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 10052: }
10053: fprintf(ficreseij,"******\n");
10054:
10055: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10056: oldm=oldms;savm=savms;
10057: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
1.127 brouard 10058:
1.219 brouard 10059: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 10060: }
10061: fclose(ficreseij);
1.208 brouard 10062: printf("done evsij\n");fflush(stdout);
10063: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 brouard 10064:
1.127 brouard 10065: /*---------- Health expectancies and variances ------------*/
1.218 brouard 10066:
10067:
1.201 brouard 10068: strcpy(filerest,"T_");
10069: strcat(filerest,fileresu);
1.127 brouard 10070: if((ficrest=fopen(filerest,"w"))==NULL) {
10071: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
10072: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
10073: }
1.208 brouard 10074: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
10075: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 brouard 10076:
1.126 brouard 10077:
1.201 brouard 10078: strcpy(fileresstde,"STDE_");
10079: strcat(fileresstde,fileresu);
1.126 brouard 10080: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
10081: printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
10082: fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
10083: }
1.208 brouard 10084: printf(" Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
10085: fprintf(ficlog," Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 10086:
1.201 brouard 10087: strcpy(filerescve,"CVE_");
10088: strcat(filerescve,fileresu);
1.126 brouard 10089: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
10090: printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
10091: fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
10092: }
1.208 brouard 10093: printf(" Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
10094: fprintf(ficlog," Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 10095:
1.201 brouard 10096: strcpy(fileresv,"V_");
10097: strcat(fileresv,fileresu);
1.126 brouard 10098: if((ficresvij=fopen(fileresv,"w"))==NULL) {
10099: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
10100: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
10101: }
1.208 brouard 10102: printf(" Computing Variance-covariance of DFLEs: file '%s' ... ", fileresv);fflush(stdout);
10103: fprintf(ficlog," Computing Variance-covariance of DFLEs: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 10104:
1.145 brouard 10105: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10106: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
10107:
1.225 ! brouard 10108: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.208 brouard 10109: fprintf(ficrest,"\n#****** ");
1.225 ! brouard 10110: for(j=1;j<=cptcoveff;j++)
1.218 brouard 10111: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10112: fprintf(ficrest,"******\n");
10113:
10114: fprintf(ficresstdeij,"\n#****** ");
10115: fprintf(ficrescveij,"\n#****** ");
1.225 ! brouard 10116: for(j=1;j<=cptcoveff;j++) {
1.218 brouard 10117: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10118: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10119: }
10120: fprintf(ficresstdeij,"******\n");
10121: fprintf(ficrescveij,"******\n");
10122:
10123: fprintf(ficresvij,"\n#****** ");
1.225 ! brouard 10124: for(j=1;j<=cptcoveff;j++)
1.218 brouard 10125: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10126: fprintf(ficresvij,"******\n");
10127:
10128: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10129: oldm=oldms;savm=savms;
10130: printf(" cvevsij %d, ",k);
10131: fprintf(ficlog, " cvevsij %d, ",k);
10132: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
10133: printf(" end cvevsij \n ");
10134: fprintf(ficlog, " end cvevsij \n ");
10135:
10136: /*
10137: */
10138: /* goto endfree; */
10139:
10140: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10141: pstamp(ficrest);
10142:
10143:
10144: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.220 brouard 10145: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
10146: cptcod= 0; /* To be deleted */
10147: printf("varevsij %d \n",vpopbased);
10148: fprintf(ficlog, "varevsij %d \n",vpopbased);
10149: varevsij(optionfilefiname, vareij, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, estepm, cptcov,cptcod,vpopbased,mobilav, strstart); /* cptcod not initialized Intel */
10150: 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 ");
10151: if(vpopbased==1)
10152: 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);
10153: else
10154: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
10155: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
10156: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
10157: fprintf(ficrest,"\n");
10158: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
10159: epj=vector(1,nlstate+1);
10160: printf("Computing age specific period (stable) prevalences in each health state \n");
10161: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
10162: for(age=bage; age <=fage ;age++){
10163: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k); /*ZZ Is it the correct prevalim */
10164: if (vpopbased==1) {
10165: if(mobilav ==0){
10166: for(i=1; i<=nlstate;i++)
10167: prlim[i][i]=probs[(int)age][i][k];
10168: }else{ /* mobilav */
10169: for(i=1; i<=nlstate;i++)
10170: prlim[i][i]=mobaverage[(int)age][i][k];
10171: }
10172: }
1.219 brouard 10173:
1.220 brouard 10174: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
10175: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
10176: /* printf(" age %4.0f ",age); */
10177: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
10178: for(i=1, epj[j]=0.;i <=nlstate;i++) {
10179: epj[j] += prlim[i][i]*eij[i][j][(int)age];
10180: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
10181: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
10182: }
10183: epj[nlstate+1] +=epj[j];
10184: }
10185: /* printf(" age %4.0f \n",age); */
1.219 brouard 10186:
1.220 brouard 10187: for(i=1, vepp=0.;i <=nlstate;i++)
10188: for(j=1;j <=nlstate;j++)
10189: vepp += vareij[i][j][(int)age];
10190: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
10191: for(j=1;j <=nlstate;j++){
10192: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
10193: }
10194: fprintf(ficrest,"\n");
10195: }
1.208 brouard 10196: } /* End vpopbased */
10197: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10198: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10199: free_vector(epj,1,nlstate+1);
10200: printf("done \n");fflush(stdout);
10201: fprintf(ficlog,"done\n");fflush(ficlog);
10202:
1.145 brouard 10203: /*}*/
1.208 brouard 10204: } /* End k */
1.126 brouard 10205: free_vector(weight,1,n);
1.145 brouard 10206: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.126 brouard 10207: free_imatrix(s,1,maxwav+1,1,n);
10208: free_matrix(anint,1,maxwav,1,n);
10209: free_matrix(mint,1,maxwav,1,n);
10210: free_ivector(cod,1,n);
10211: free_ivector(tab,1,NCOVMAX);
10212: fclose(ficresstdeij);
10213: fclose(ficrescveij);
10214: fclose(ficresvij);
10215: fclose(ficrest);
1.208 brouard 10216: printf("done Health expectancies\n");fflush(stdout);
10217: fprintf(ficlog,"done Health expectancies\n");fflush(ficlog);
1.126 brouard 10218: fclose(ficpar);
10219:
10220: /*------- Variance of period (stable) prevalence------*/
10221:
1.201 brouard 10222: strcpy(fileresvpl,"VPL_");
10223: strcat(fileresvpl,fileresu);
1.126 brouard 10224: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
10225: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
10226: exit(0);
10227: }
1.208 brouard 10228: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
10229: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.126 brouard 10230:
1.145 brouard 10231: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10232: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
10233:
1.225 ! brouard 10234: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.145 brouard 10235: fprintf(ficresvpl,"\n#****** ");
1.225 ! brouard 10236: for(j=1;j<=cptcoveff;j++)
1.218 brouard 10237: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10238: fprintf(ficresvpl,"******\n");
10239:
10240: varpl=matrix(1,nlstate,(int) bage, (int) fage);
10241: oldm=oldms;savm=savms;
10242: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart);
10243: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 10244: /*}*/
1.126 brouard 10245: }
1.218 brouard 10246:
1.126 brouard 10247: fclose(ficresvpl);
1.208 brouard 10248: printf("done variance-covariance of period prevalence\n");fflush(stdout);
10249: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.126 brouard 10250:
10251: /*---------- End : free ----------------*/
1.219 brouard 10252: if (mobilav!=0 ||mobilavproj !=0)
10253: free_ma3x(mobaverages,1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
1.218 brouard 10254: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 10255: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
10256: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 10257: } /* mle==-3 arrives here for freeing */
1.164 brouard 10258: /* endfree:*/
1.126 brouard 10259: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
10260: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
10261: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.224 brouard 10262: free_ma3x(cotqvar,1,maxwav,1,nqtv,1,n);
1.223 brouard 10263: free_ma3x(cotvar,1,maxwav,1,ntv,1,n);
10264: free_matrix(coqvar,1,maxwav,1,n);
1.126 brouard 10265: free_matrix(covar,0,NCOVMAX,1,n);
10266: free_matrix(matcov,1,npar,1,npar);
1.203 brouard 10267: free_matrix(hess,1,npar,1,npar);
1.126 brouard 10268: /*free_vector(delti,1,npar);*/
10269: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10270: free_matrix(agev,1,maxwav,1,imx);
10271: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10272:
1.145 brouard 10273: free_ivector(ncodemax,1,NCOVMAX);
1.192 brouard 10274: free_ivector(ncodemaxwundef,1,NCOVMAX);
1.225 ! brouard 10275: free_ivector(Typevar,-1,NCOVMAX);
1.145 brouard 10276: free_ivector(Tvar,1,NCOVMAX);
10277: free_ivector(Tprod,1,NCOVMAX);
10278: free_ivector(Tvaraff,1,NCOVMAX);
1.220 brouard 10279: free_ivector(invalidvarcomb,1,ncovcombmax);
1.145 brouard 10280: free_ivector(Tage,1,NCOVMAX);
1.126 brouard 10281:
10282: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
1.200 brouard 10283: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 10284: fflush(fichtm);
10285: fflush(ficgp);
10286:
10287:
10288: if((nberr >0) || (nbwarn>0)){
1.216 brouard 10289: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
10290: fprintf(ficlog,"End of Imach with %d errors and/or warnings %d. Please look at the log file for details.\n",nberr,nbwarn);
1.126 brouard 10291: }else{
10292: printf("End of Imach\n");
10293: fprintf(ficlog,"End of Imach\n");
10294: }
10295: printf("See log file on %s\n",filelog);
10296: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 10297: /*(void) gettimeofday(&end_time,&tzp);*/
10298: rend_time = time(NULL);
10299: end_time = *localtime(&rend_time);
10300: /* tml = *localtime(&end_time.tm_sec); */
10301: strcpy(strtend,asctime(&end_time));
1.126 brouard 10302: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
10303: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 10304: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.126 brouard 10305:
1.157 brouard 10306: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
10307: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
10308: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 10309: /* printf("Total time was %d uSec.\n", total_usecs);*/
10310: /* if(fileappend(fichtm,optionfilehtm)){ */
10311: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
10312: fclose(fichtm);
10313: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
10314: fclose(fichtmcov);
10315: fclose(ficgp);
10316: fclose(ficlog);
10317: /*------ End -----------*/
10318:
10319:
10320: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 10321: #ifdef WIN32
10322: if (_chdir(pathcd) != 0)
10323: printf("Can't move to directory %s!\n",path);
10324: if(_getcwd(pathcd,MAXLINE) > 0)
10325: #else
1.126 brouard 10326: if(chdir(pathcd) != 0)
1.184 brouard 10327: printf("Can't move to directory %s!\n", path);
10328: if (getcwd(pathcd, MAXLINE) > 0)
10329: #endif
1.126 brouard 10330: printf("Current directory %s!\n",pathcd);
10331: /*strcat(plotcmd,CHARSEPARATOR);*/
10332: sprintf(plotcmd,"gnuplot");
1.157 brouard 10333: #ifdef _WIN32
1.126 brouard 10334: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
10335: #endif
10336: if(!stat(plotcmd,&info)){
1.158 brouard 10337: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10338: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 10339: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 10340: }else
10341: strcpy(pplotcmd,plotcmd);
1.157 brouard 10342: #ifdef __unix
1.126 brouard 10343: strcpy(plotcmd,GNUPLOTPROGRAM);
10344: if(!stat(plotcmd,&info)){
1.158 brouard 10345: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10346: }else
10347: strcpy(pplotcmd,plotcmd);
10348: #endif
10349: }else
10350: strcpy(pplotcmd,plotcmd);
10351:
10352: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 10353: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10354:
10355: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 10356: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 10357: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 10358: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 10359: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 10360: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 10361: }
1.158 brouard 10362: printf(" Successful, please wait...");
1.126 brouard 10363: while (z[0] != 'q') {
10364: /* chdir(path); */
1.154 brouard 10365: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 10366: scanf("%s",z);
10367: /* if (z[0] == 'c') system("./imach"); */
10368: if (z[0] == 'e') {
1.158 brouard 10369: #ifdef __APPLE__
1.152 brouard 10370: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 10371: #elif __linux
10372: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 10373: #else
1.152 brouard 10374: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 10375: #endif
10376: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
10377: system(pplotcmd);
1.126 brouard 10378: }
10379: else if (z[0] == 'g') system(plotcmd);
10380: else if (z[0] == 'q') exit(0);
10381: }
10382: end:
10383: while (z[0] != 'q') {
1.195 brouard 10384: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 10385: scanf("%s",z);
10386: }
10387: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>