Annotation of imach/src/imach.c, revision 1.226
1.226 ! brouard 1: /* $Id: imach.c,v 1.225 2016/07/12 08:40:03 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.226 ! brouard 4: Revision 1.225 2016/07/12 08:40:03 brouard
! 5: Summary: saving but not running
! 6:
1.225 brouard 7: Revision 1.224 2016/07/01 13:16:01 brouard
8: Summary: Fixes
9:
1.224 brouard 10: Revision 1.223 2016/02/19 09:23:35 brouard
11: Summary: temporary
12:
1.223 brouard 13: Revision 1.222 2016/02/17 08:14:50 brouard
14: Summary: Probably last 0.98 stable version 0.98r6
15:
1.222 brouard 16: Revision 1.221 2016/02/15 23:35:36 brouard
17: Summary: minor bug
18:
1.220 brouard 19: Revision 1.219 2016/02/15 00:48:12 brouard
20: *** empty log message ***
21:
1.219 brouard 22: Revision 1.218 2016/02/12 11:29:23 brouard
23: Summary: 0.99 Back projections
24:
1.218 brouard 25: Revision 1.217 2015/12/23 17:18:31 brouard
26: Summary: Experimental backcast
27:
1.217 brouard 28: Revision 1.216 2015/12/18 17:32:11 brouard
29: Summary: 0.98r4 Warning and status=-2
30:
31: Version 0.98r4 is now:
32: - displaying an error when status is -1, date of interview unknown and date of death known;
33: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
34: Older changes concerning s=-2, dating from 2005 have been supersed.
35:
1.216 brouard 36: Revision 1.215 2015/12/16 08:52:24 brouard
37: Summary: 0.98r4 working
38:
1.215 brouard 39: Revision 1.214 2015/12/16 06:57:54 brouard
40: Summary: temporary not working
41:
1.214 brouard 42: Revision 1.213 2015/12/11 18:22:17 brouard
43: Summary: 0.98r4
44:
1.213 brouard 45: Revision 1.212 2015/11/21 12:47:24 brouard
46: Summary: minor typo
47:
1.212 brouard 48: Revision 1.211 2015/11/21 12:41:11 brouard
49: Summary: 0.98r3 with some graph of projected cross-sectional
50:
51: Author: Nicolas Brouard
52:
1.211 brouard 53: Revision 1.210 2015/11/18 17:41:20 brouard
54: Summary: Start working on projected prevalences
55:
1.210 brouard 56: Revision 1.209 2015/11/17 22:12:03 brouard
57: Summary: Adding ftolpl parameter
58: Author: N Brouard
59:
60: We had difficulties to get smoothed confidence intervals. It was due
61: to the period prevalence which wasn't computed accurately. The inner
62: parameter ftolpl is now an outer parameter of the .imach parameter
63: file after estepm. If ftolpl is small 1.e-4 and estepm too,
64: computation are long.
65:
1.209 brouard 66: Revision 1.208 2015/11/17 14:31:57 brouard
67: Summary: temporary
68:
1.208 brouard 69: Revision 1.207 2015/10/27 17:36:57 brouard
70: *** empty log message ***
71:
1.207 brouard 72: Revision 1.206 2015/10/24 07:14:11 brouard
73: *** empty log message ***
74:
1.206 brouard 75: Revision 1.205 2015/10/23 15:50:53 brouard
76: Summary: 0.98r3 some clarification for graphs on likelihood contributions
77:
1.205 brouard 78: Revision 1.204 2015/10/01 16:20:26 brouard
79: Summary: Some new graphs of contribution to likelihood
80:
1.204 brouard 81: Revision 1.203 2015/09/30 17:45:14 brouard
82: Summary: looking at better estimation of the hessian
83:
84: Also a better criteria for convergence to the period prevalence And
85: therefore adding the number of years needed to converge. (The
86: prevalence in any alive state shold sum to one
87:
1.203 brouard 88: Revision 1.202 2015/09/22 19:45:16 brouard
89: Summary: Adding some overall graph on contribution to likelihood. Might change
90:
1.202 brouard 91: Revision 1.201 2015/09/15 17:34:58 brouard
92: Summary: 0.98r0
93:
94: - Some new graphs like suvival functions
95: - Some bugs fixed like model=1+age+V2.
96:
1.201 brouard 97: Revision 1.200 2015/09/09 16:53:55 brouard
98: Summary: Big bug thanks to Flavia
99:
100: Even model=1+age+V2. did not work anymore
101:
1.200 brouard 102: Revision 1.199 2015/09/07 14:09:23 brouard
103: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
104:
1.199 brouard 105: Revision 1.198 2015/09/03 07:14:39 brouard
106: Summary: 0.98q5 Flavia
107:
1.198 brouard 108: Revision 1.197 2015/09/01 18:24:39 brouard
109: *** empty log message ***
110:
1.197 brouard 111: Revision 1.196 2015/08/18 23:17:52 brouard
112: Summary: 0.98q5
113:
1.196 brouard 114: Revision 1.195 2015/08/18 16:28:39 brouard
115: Summary: Adding a hack for testing purpose
116:
117: After reading the title, ftol and model lines, if the comment line has
118: a q, starting with #q, the answer at the end of the run is quit. It
119: permits to run test files in batch with ctest. The former workaround was
120: $ echo q | imach foo.imach
121:
1.195 brouard 122: Revision 1.194 2015/08/18 13:32:00 brouard
123: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
124:
1.194 brouard 125: Revision 1.193 2015/08/04 07:17:42 brouard
126: Summary: 0.98q4
127:
1.193 brouard 128: Revision 1.192 2015/07/16 16:49:02 brouard
129: Summary: Fixing some outputs
130:
1.192 brouard 131: Revision 1.191 2015/07/14 10:00:33 brouard
132: Summary: Some fixes
133:
1.191 brouard 134: Revision 1.190 2015/05/05 08:51:13 brouard
135: Summary: Adding digits in output parameters (7 digits instead of 6)
136:
137: Fix 1+age+.
138:
1.190 brouard 139: Revision 1.189 2015/04/30 14:45:16 brouard
140: Summary: 0.98q2
141:
1.189 brouard 142: Revision 1.188 2015/04/30 08:27:53 brouard
143: *** empty log message ***
144:
1.188 brouard 145: Revision 1.187 2015/04/29 09:11:15 brouard
146: *** empty log message ***
147:
1.187 brouard 148: Revision 1.186 2015/04/23 12:01:52 brouard
149: Summary: V1*age is working now, version 0.98q1
150:
151: Some codes had been disabled in order to simplify and Vn*age was
152: working in the optimization phase, ie, giving correct MLE parameters,
153: but, as usual, outputs were not correct and program core dumped.
154:
1.186 brouard 155: Revision 1.185 2015/03/11 13:26:42 brouard
156: Summary: Inclusion of compile and links command line for Intel Compiler
157:
1.185 brouard 158: Revision 1.184 2015/03/11 11:52:39 brouard
159: Summary: Back from Windows 8. Intel Compiler
160:
1.184 brouard 161: Revision 1.183 2015/03/10 20:34:32 brouard
162: Summary: 0.98q0, trying with directest, mnbrak fixed
163:
164: We use directest instead of original Powell test; probably no
165: incidence on the results, but better justifications;
166: We fixed Numerical Recipes mnbrak routine which was wrong and gave
167: wrong results.
168:
1.183 brouard 169: Revision 1.182 2015/02/12 08:19:57 brouard
170: Summary: Trying to keep directest which seems simpler and more general
171: Author: Nicolas Brouard
172:
1.182 brouard 173: Revision 1.181 2015/02/11 23:22:24 brouard
174: Summary: Comments on Powell added
175:
176: Author:
177:
1.181 brouard 178: Revision 1.180 2015/02/11 17:33:45 brouard
179: Summary: Finishing move from main to function (hpijx and prevalence_limit)
180:
1.180 brouard 181: Revision 1.179 2015/01/04 09:57:06 brouard
182: Summary: back to OS/X
183:
1.179 brouard 184: Revision 1.178 2015/01/04 09:35:48 brouard
185: *** empty log message ***
186:
1.178 brouard 187: Revision 1.177 2015/01/03 18:40:56 brouard
188: Summary: Still testing ilc32 on OSX
189:
1.177 brouard 190: Revision 1.176 2015/01/03 16:45:04 brouard
191: *** empty log message ***
192:
1.176 brouard 193: Revision 1.175 2015/01/03 16:33:42 brouard
194: *** empty log message ***
195:
1.175 brouard 196: Revision 1.174 2015/01/03 16:15:49 brouard
197: Summary: Still in cross-compilation
198:
1.174 brouard 199: Revision 1.173 2015/01/03 12:06:26 brouard
200: Summary: trying to detect cross-compilation
201:
1.173 brouard 202: Revision 1.172 2014/12/27 12:07:47 brouard
203: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
204:
1.172 brouard 205: Revision 1.171 2014/12/23 13:26:59 brouard
206: Summary: Back from Visual C
207:
208: Still problem with utsname.h on Windows
209:
1.171 brouard 210: Revision 1.170 2014/12/23 11:17:12 brouard
211: Summary: Cleaning some \%% back to %%
212:
213: The escape was mandatory for a specific compiler (which one?), but too many warnings.
214:
1.170 brouard 215: Revision 1.169 2014/12/22 23:08:31 brouard
216: Summary: 0.98p
217:
218: Outputs some informations on compiler used, OS etc. Testing on different platforms.
219:
1.169 brouard 220: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 221: Summary: update
1.169 brouard 222:
1.168 brouard 223: Revision 1.167 2014/12/22 13:50:56 brouard
224: Summary: Testing uname and compiler version and if compiled 32 or 64
225:
226: Testing on Linux 64
227:
1.167 brouard 228: Revision 1.166 2014/12/22 11:40:47 brouard
229: *** empty log message ***
230:
1.166 brouard 231: Revision 1.165 2014/12/16 11:20:36 brouard
232: Summary: After compiling on Visual C
233:
234: * imach.c (Module): Merging 1.61 to 1.162
235:
1.165 brouard 236: Revision 1.164 2014/12/16 10:52:11 brouard
237: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
238:
239: * imach.c (Module): Merging 1.61 to 1.162
240:
1.164 brouard 241: Revision 1.163 2014/12/16 10:30:11 brouard
242: * imach.c (Module): Merging 1.61 to 1.162
243:
1.163 brouard 244: Revision 1.162 2014/09/25 11:43:39 brouard
245: Summary: temporary backup 0.99!
246:
1.162 brouard 247: Revision 1.1 2014/09/16 11:06:58 brouard
248: Summary: With some code (wrong) for nlopt
249:
250: Author:
251:
252: Revision 1.161 2014/09/15 20:41:41 brouard
253: Summary: Problem with macro SQR on Intel compiler
254:
1.161 brouard 255: Revision 1.160 2014/09/02 09:24:05 brouard
256: *** empty log message ***
257:
1.160 brouard 258: Revision 1.159 2014/09/01 10:34:10 brouard
259: Summary: WIN32
260: Author: Brouard
261:
1.159 brouard 262: Revision 1.158 2014/08/27 17:11:51 brouard
263: *** empty log message ***
264:
1.158 brouard 265: Revision 1.157 2014/08/27 16:26:55 brouard
266: Summary: Preparing windows Visual studio version
267: Author: Brouard
268:
269: In order to compile on Visual studio, time.h is now correct and time_t
270: and tm struct should be used. difftime should be used but sometimes I
271: just make the differences in raw time format (time(&now).
272: Trying to suppress #ifdef LINUX
273: Add xdg-open for __linux in order to open default browser.
274:
1.157 brouard 275: Revision 1.156 2014/08/25 20:10:10 brouard
276: *** empty log message ***
277:
1.156 brouard 278: Revision 1.155 2014/08/25 18:32:34 brouard
279: Summary: New compile, minor changes
280: Author: Brouard
281:
1.155 brouard 282: Revision 1.154 2014/06/20 17:32:08 brouard
283: Summary: Outputs now all graphs of convergence to period prevalence
284:
1.154 brouard 285: Revision 1.153 2014/06/20 16:45:46 brouard
286: Summary: If 3 live state, convergence to period prevalence on same graph
287: Author: Brouard
288:
1.153 brouard 289: Revision 1.152 2014/06/18 17:54:09 brouard
290: Summary: open browser, use gnuplot on same dir than imach if not found in the path
291:
1.152 brouard 292: Revision 1.151 2014/06/18 16:43:30 brouard
293: *** empty log message ***
294:
1.151 brouard 295: Revision 1.150 2014/06/18 16:42:35 brouard
296: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
297: Author: brouard
298:
1.150 brouard 299: Revision 1.149 2014/06/18 15:51:14 brouard
300: Summary: Some fixes in parameter files errors
301: Author: Nicolas Brouard
302:
1.149 brouard 303: Revision 1.148 2014/06/17 17:38:48 brouard
304: Summary: Nothing new
305: Author: Brouard
306:
307: Just a new packaging for OS/X version 0.98nS
308:
1.148 brouard 309: Revision 1.147 2014/06/16 10:33:11 brouard
310: *** empty log message ***
311:
1.147 brouard 312: Revision 1.146 2014/06/16 10:20:28 brouard
313: Summary: Merge
314: Author: Brouard
315:
316: Merge, before building revised version.
317:
1.146 brouard 318: Revision 1.145 2014/06/10 21:23:15 brouard
319: Summary: Debugging with valgrind
320: Author: Nicolas Brouard
321:
322: Lot of changes in order to output the results with some covariates
323: After the Edimburgh REVES conference 2014, it seems mandatory to
324: improve the code.
325: No more memory valgrind error but a lot has to be done in order to
326: continue the work of splitting the code into subroutines.
327: Also, decodemodel has been improved. Tricode is still not
328: optimal. nbcode should be improved. Documentation has been added in
329: the source code.
330:
1.144 brouard 331: Revision 1.143 2014/01/26 09:45:38 brouard
332: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
333:
334: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
335: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
336:
1.143 brouard 337: Revision 1.142 2014/01/26 03:57:36 brouard
338: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
339:
340: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
341:
1.142 brouard 342: Revision 1.141 2014/01/26 02:42:01 brouard
343: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
344:
1.141 brouard 345: Revision 1.140 2011/09/02 10:37:54 brouard
346: Summary: times.h is ok with mingw32 now.
347:
1.140 brouard 348: Revision 1.139 2010/06/14 07:50:17 brouard
349: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
350: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
351:
1.139 brouard 352: Revision 1.138 2010/04/30 18:19:40 brouard
353: *** empty log message ***
354:
1.138 brouard 355: Revision 1.137 2010/04/29 18:11:38 brouard
356: (Module): Checking covariates for more complex models
357: than V1+V2. A lot of change to be done. Unstable.
358:
1.137 brouard 359: Revision 1.136 2010/04/26 20:30:53 brouard
360: (Module): merging some libgsl code. Fixing computation
361: of likelione (using inter/intrapolation if mle = 0) in order to
362: get same likelihood as if mle=1.
363: Some cleaning of code and comments added.
364:
1.136 brouard 365: Revision 1.135 2009/10/29 15:33:14 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.135 brouard 368: Revision 1.134 2009/10/29 13:18:53 brouard
369: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
370:
1.134 brouard 371: Revision 1.133 2009/07/06 10:21:25 brouard
372: just nforces
373:
1.133 brouard 374: Revision 1.132 2009/07/06 08:22:05 brouard
375: Many tings
376:
1.132 brouard 377: Revision 1.131 2009/06/20 16:22:47 brouard
378: Some dimensions resccaled
379:
1.131 brouard 380: Revision 1.130 2009/05/26 06:44:34 brouard
381: (Module): Max Covariate is now set to 20 instead of 8. A
382: lot of cleaning with variables initialized to 0. Trying to make
383: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
384:
1.130 brouard 385: Revision 1.129 2007/08/31 13:49:27 lievre
386: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
387:
1.129 lievre 388: Revision 1.128 2006/06/30 13:02:05 brouard
389: (Module): Clarifications on computing e.j
390:
1.128 brouard 391: Revision 1.127 2006/04/28 18:11:50 brouard
392: (Module): Yes the sum of survivors was wrong since
393: imach-114 because nhstepm was no more computed in the age
394: loop. Now we define nhstepma in the age loop.
395: (Module): In order to speed up (in case of numerous covariates) we
396: compute health expectancies (without variances) in a first step
397: and then all the health expectancies with variances or standard
398: deviation (needs data from the Hessian matrices) which slows the
399: computation.
400: In the future we should be able to stop the program is only health
401: expectancies and graph are needed without standard deviations.
402:
1.127 brouard 403: Revision 1.126 2006/04/28 17:23:28 brouard
404: (Module): Yes the sum of survivors was wrong since
405: imach-114 because nhstepm was no more computed in the age
406: loop. Now we define nhstepma in the age loop.
407: Version 0.98h
408:
1.126 brouard 409: Revision 1.125 2006/04/04 15:20:31 lievre
410: Errors in calculation of health expectancies. Age was not initialized.
411: Forecasting file added.
412:
413: Revision 1.124 2006/03/22 17:13:53 lievre
414: Parameters are printed with %lf instead of %f (more numbers after the comma).
415: The log-likelihood is printed in the log file
416:
417: Revision 1.123 2006/03/20 10:52:43 brouard
418: * imach.c (Module): <title> changed, corresponds to .htm file
419: name. <head> headers where missing.
420:
421: * imach.c (Module): Weights can have a decimal point as for
422: English (a comma might work with a correct LC_NUMERIC environment,
423: otherwise the weight is truncated).
424: Modification of warning when the covariates values are not 0 or
425: 1.
426: Version 0.98g
427:
428: Revision 1.122 2006/03/20 09:45:41 brouard
429: (Module): Weights can have a decimal point as for
430: English (a comma might work with a correct LC_NUMERIC environment,
431: otherwise the weight is truncated).
432: Modification of warning when the covariates values are not 0 or
433: 1.
434: Version 0.98g
435:
436: Revision 1.121 2006/03/16 17:45:01 lievre
437: * imach.c (Module): Comments concerning covariates added
438:
439: * imach.c (Module): refinements in the computation of lli if
440: status=-2 in order to have more reliable computation if stepm is
441: not 1 month. Version 0.98f
442:
443: Revision 1.120 2006/03/16 15:10:38 lievre
444: (Module): refinements in the computation of lli if
445: status=-2 in order to have more reliable computation if stepm is
446: not 1 month. Version 0.98f
447:
448: Revision 1.119 2006/03/15 17:42:26 brouard
449: (Module): Bug if status = -2, the loglikelihood was
450: computed as likelihood omitting the logarithm. Version O.98e
451:
452: Revision 1.118 2006/03/14 18:20:07 brouard
453: (Module): varevsij Comments added explaining the second
454: table of variances if popbased=1 .
455: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
456: (Module): Function pstamp added
457: (Module): Version 0.98d
458:
459: Revision 1.117 2006/03/14 17:16:22 brouard
460: (Module): varevsij Comments added explaining the second
461: table of variances if popbased=1 .
462: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
463: (Module): Function pstamp added
464: (Module): Version 0.98d
465:
466: Revision 1.116 2006/03/06 10:29:27 brouard
467: (Module): Variance-covariance wrong links and
468: varian-covariance of ej. is needed (Saito).
469:
470: Revision 1.115 2006/02/27 12:17:45 brouard
471: (Module): One freematrix added in mlikeli! 0.98c
472:
473: Revision 1.114 2006/02/26 12:57:58 brouard
474: (Module): Some improvements in processing parameter
475: filename with strsep.
476:
477: Revision 1.113 2006/02/24 14:20:24 brouard
478: (Module): Memory leaks checks with valgrind and:
479: datafile was not closed, some imatrix were not freed and on matrix
480: allocation too.
481:
482: Revision 1.112 2006/01/30 09:55:26 brouard
483: (Module): Back to gnuplot.exe instead of wgnuplot.exe
484:
485: Revision 1.111 2006/01/25 20:38:18 brouard
486: (Module): Lots of cleaning and bugs added (Gompertz)
487: (Module): Comments can be added in data file. Missing date values
488: can be a simple dot '.'.
489:
490: Revision 1.110 2006/01/25 00:51:50 brouard
491: (Module): Lots of cleaning and bugs added (Gompertz)
492:
493: Revision 1.109 2006/01/24 19:37:15 brouard
494: (Module): Comments (lines starting with a #) are allowed in data.
495:
496: Revision 1.108 2006/01/19 18:05:42 lievre
497: Gnuplot problem appeared...
498: To be fixed
499:
500: Revision 1.107 2006/01/19 16:20:37 brouard
501: Test existence of gnuplot in imach path
502:
503: Revision 1.106 2006/01/19 13:24:36 brouard
504: Some cleaning and links added in html output
505:
506: Revision 1.105 2006/01/05 20:23:19 lievre
507: *** empty log message ***
508:
509: Revision 1.104 2005/09/30 16:11:43 lievre
510: (Module): sump fixed, loop imx fixed, and simplifications.
511: (Module): If the status is missing at the last wave but we know
512: that the person is alive, then we can code his/her status as -2
513: (instead of missing=-1 in earlier versions) and his/her
514: contributions to the likelihood is 1 - Prob of dying from last
515: health status (= 1-p13= p11+p12 in the easiest case of somebody in
516: the healthy state at last known wave). Version is 0.98
517:
518: Revision 1.103 2005/09/30 15:54:49 lievre
519: (Module): sump fixed, loop imx fixed, and simplifications.
520:
521: Revision 1.102 2004/09/15 17:31:30 brouard
522: Add the possibility to read data file including tab characters.
523:
524: Revision 1.101 2004/09/15 10:38:38 brouard
525: Fix on curr_time
526:
527: Revision 1.100 2004/07/12 18:29:06 brouard
528: Add version for Mac OS X. Just define UNIX in Makefile
529:
530: Revision 1.99 2004/06/05 08:57:40 brouard
531: *** empty log message ***
532:
533: Revision 1.98 2004/05/16 15:05:56 brouard
534: New version 0.97 . First attempt to estimate force of mortality
535: directly from the data i.e. without the need of knowing the health
536: state at each age, but using a Gompertz model: log u =a + b*age .
537: This is the basic analysis of mortality and should be done before any
538: other analysis, in order to test if the mortality estimated from the
539: cross-longitudinal survey is different from the mortality estimated
540: from other sources like vital statistic data.
541:
542: The same imach parameter file can be used but the option for mle should be -3.
543:
1.133 brouard 544: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 545: former routines in order to include the new code within the former code.
546:
547: The output is very simple: only an estimate of the intercept and of
548: the slope with 95% confident intervals.
549:
550: Current limitations:
551: A) Even if you enter covariates, i.e. with the
552: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
553: B) There is no computation of Life Expectancy nor Life Table.
554:
555: Revision 1.97 2004/02/20 13:25:42 lievre
556: Version 0.96d. Population forecasting command line is (temporarily)
557: suppressed.
558:
559: Revision 1.96 2003/07/15 15:38:55 brouard
560: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
561: rewritten within the same printf. Workaround: many printfs.
562:
563: Revision 1.95 2003/07/08 07:54:34 brouard
564: * imach.c (Repository):
565: (Repository): Using imachwizard code to output a more meaningful covariance
566: matrix (cov(a12,c31) instead of numbers.
567:
568: Revision 1.94 2003/06/27 13:00:02 brouard
569: Just cleaning
570:
571: Revision 1.93 2003/06/25 16:33:55 brouard
572: (Module): On windows (cygwin) function asctime_r doesn't
573: exist so I changed back to asctime which exists.
574: (Module): Version 0.96b
575:
576: Revision 1.92 2003/06/25 16:30:45 brouard
577: (Module): On windows (cygwin) function asctime_r doesn't
578: exist so I changed back to asctime which exists.
579:
580: Revision 1.91 2003/06/25 15:30:29 brouard
581: * imach.c (Repository): Duplicated warning errors corrected.
582: (Repository): Elapsed time after each iteration is now output. It
583: helps to forecast when convergence will be reached. Elapsed time
584: is stamped in powell. We created a new html file for the graphs
585: concerning matrix of covariance. It has extension -cov.htm.
586:
587: Revision 1.90 2003/06/24 12:34:15 brouard
588: (Module): Some bugs corrected for windows. Also, when
589: mle=-1 a template is output in file "or"mypar.txt with the design
590: of the covariance matrix to be input.
591:
592: Revision 1.89 2003/06/24 12:30:52 brouard
593: (Module): Some bugs corrected for windows. Also, when
594: mle=-1 a template is output in file "or"mypar.txt with the design
595: of the covariance matrix to be input.
596:
597: Revision 1.88 2003/06/23 17:54:56 brouard
598: * 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.
599:
600: Revision 1.87 2003/06/18 12:26:01 brouard
601: Version 0.96
602:
603: Revision 1.86 2003/06/17 20:04:08 brouard
604: (Module): Change position of html and gnuplot routines and added
605: routine fileappend.
606:
607: Revision 1.85 2003/06/17 13:12:43 brouard
608: * imach.c (Repository): Check when date of death was earlier that
609: current date of interview. It may happen when the death was just
610: prior to the death. In this case, dh was negative and likelihood
611: was wrong (infinity). We still send an "Error" but patch by
612: assuming that the date of death was just one stepm after the
613: interview.
614: (Repository): Because some people have very long ID (first column)
615: we changed int to long in num[] and we added a new lvector for
616: memory allocation. But we also truncated to 8 characters (left
617: truncation)
618: (Repository): No more line truncation errors.
619:
620: Revision 1.84 2003/06/13 21:44:43 brouard
621: * imach.c (Repository): Replace "freqsummary" at a correct
622: place. It differs from routine "prevalence" which may be called
623: many times. Probs is memory consuming and must be used with
624: parcimony.
625: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
626:
627: Revision 1.83 2003/06/10 13:39:11 lievre
628: *** empty log message ***
629:
630: Revision 1.82 2003/06/05 15:57:20 brouard
631: Add log in imach.c and fullversion number is now printed.
632:
633: */
634: /*
635: Interpolated Markov Chain
636:
637: Short summary of the programme:
638:
639: This program computes Healthy Life Expectancies from
640: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
641: first survey ("cross") where individuals from different ages are
642: interviewed on their health status or degree of disability (in the
643: case of a health survey which is our main interest) -2- at least a
644: second wave of interviews ("longitudinal") which measure each change
645: (if any) in individual health status. Health expectancies are
646: computed from the time spent in each health state according to a
647: model. More health states you consider, more time is necessary to reach the
648: Maximum Likelihood of the parameters involved in the model. The
649: simplest model is the multinomial logistic model where pij is the
650: probability to be observed in state j at the second wave
651: conditional to be observed in state i at the first wave. Therefore
652: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
653: 'age' is age and 'sex' is a covariate. If you want to have a more
654: complex model than "constant and age", you should modify the program
655: where the markup *Covariates have to be included here again* invites
656: you to do it. More covariates you add, slower the
657: convergence.
658:
659: The advantage of this computer programme, compared to a simple
660: multinomial logistic model, is clear when the delay between waves is not
661: identical for each individual. Also, if a individual missed an
662: intermediate interview, the information is lost, but taken into
663: account using an interpolation or extrapolation.
664:
665: hPijx is the probability to be observed in state i at age x+h
666: conditional to the observed state i at age x. The delay 'h' can be
667: split into an exact number (nh*stepm) of unobserved intermediate
668: states. This elementary transition (by month, quarter,
669: semester or year) is modelled as a multinomial logistic. The hPx
670: matrix is simply the matrix product of nh*stepm elementary matrices
671: and the contribution of each individual to the likelihood is simply
672: hPijx.
673:
674: Also this programme outputs the covariance matrix of the parameters but also
1.218 brouard 675: of the life expectancies. It also computes the period (stable) prevalence.
676:
677: Back prevalence and projections:
678: - 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)
679: Computes the back prevalence limit for any combination of covariate values k
680: at any age between ageminpar and agemaxpar and returns it in **bprlim. In the loops,
681: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm, **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
682: - hBijx Back Probability to be in state i at age x-h being in j at x
683: Computes for any combination of covariates k and any age between bage and fage
684: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
685: oldm=oldms;savm=savms;
686: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
687: Computes the transition matrix starting at age 'age' over
688: 'nhstepm*hstepm*stepm' months (i.e. until
689: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
690: nhstepm*hstepm matrices. Returns p3mat[i][j][h] after calling
691: p3mat[i][j][h]=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\
1.226 ! brouard 692: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
! 693:
! 694: Important routines
! 695:
! 696: - func (or funcone), computes logit (pij) distinguishing
! 697: o fixed variables (single or product dummies or quantitative);
! 698: o varying variables by:
! 699: (1) wave (single, product dummies, quantitative),
! 700: (2) by age (can be month) age (done), age*age (done), age*Vn where Vn can be:
! 701: % fixed dummy (treated) or quantitative (not done because time-consuming);
! 702: % varying dummy (not done) or quantitative (not done);
! 703: - Tricode which tests the modality of dummy variables (in order to warn with wrong or empty modalities)
! 704: and returns the number of efficient covariates cptcoveff and modalities nbcode[Tvar[k]][1]= 0 and nbcode[Tvar[k]][2]= 1 usually.
! 705: - printinghtml which outputs results like life expectancy in and from a state for a combination of modalities of dummy variables
! 706: o There are 2*cptcoveff combinations of (0,1) for cptcoveff variables. Outputting only combinations with people, éliminating 1 1 if
! 707: race White (0 0), Black vs White (1 0), Hispanic (0 1) and 1 1 being meaningless.
1.218 brouard 708:
1.226 ! brouard 709:
! 710:
1.133 brouard 711: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
712: Institut national d'études démographiques, Paris.
1.126 brouard 713: This software have been partly granted by Euro-REVES, a concerted action
714: from the European Union.
715: It is copyrighted identically to a GNU software product, ie programme and
716: software can be distributed freely for non commercial use. Latest version
717: can be accessed at http://euroreves.ined.fr/imach .
718:
719: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
720: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
721:
722: **********************************************************************/
723: /*
724: main
725: read parameterfile
726: read datafile
727: concatwav
728: freqsummary
729: if (mle >= 1)
730: mlikeli
731: print results files
732: if mle==1
733: computes hessian
734: read end of parameter file: agemin, agemax, bage, fage, estepm
735: begin-prev-date,...
736: open gnuplot file
737: open html file
1.145 brouard 738: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
739: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
740: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
741: freexexit2 possible for memory heap.
742:
743: h Pij x | pij_nom ficrestpij
744: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
745: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
746: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
747:
748: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
749: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
750: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
751: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
752: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
753:
1.126 brouard 754: forecasting if prevfcast==1 prevforecast call prevalence()
755: health expectancies
756: Variance-covariance of DFLE
757: prevalence()
758: movingaverage()
759: varevsij()
760: if popbased==1 varevsij(,popbased)
761: total life expectancies
762: Variance of period (stable) prevalence
763: end
764: */
765:
1.187 brouard 766: /* #define DEBUG */
767: /* #define DEBUGBRENT */
1.203 brouard 768: /* #define DEBUGLINMIN */
769: /* #define DEBUGHESS */
770: #define DEBUGHESSIJ
1.224 brouard 771: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan) *\/ */
1.165 brouard 772: #define POWELL /* Instead of NLOPT */
1.224 brouard 773: #define POWELLNOF3INFF1TEST /* Skip test */
1.186 brouard 774: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
775: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 776:
777: #include <math.h>
778: #include <stdio.h>
779: #include <stdlib.h>
780: #include <string.h>
1.226 ! brouard 781: #include <ctype.h>
1.159 brouard 782:
783: #ifdef _WIN32
784: #include <io.h>
1.172 brouard 785: #include <windows.h>
786: #include <tchar.h>
1.159 brouard 787: #else
1.126 brouard 788: #include <unistd.h>
1.159 brouard 789: #endif
1.126 brouard 790:
791: #include <limits.h>
792: #include <sys/types.h>
1.171 brouard 793:
794: #if defined(__GNUC__)
795: #include <sys/utsname.h> /* Doesn't work on Windows */
796: #endif
797:
1.126 brouard 798: #include <sys/stat.h>
799: #include <errno.h>
1.159 brouard 800: /* extern int errno; */
1.126 brouard 801:
1.157 brouard 802: /* #ifdef LINUX */
803: /* #include <time.h> */
804: /* #include "timeval.h" */
805: /* #else */
806: /* #include <sys/time.h> */
807: /* #endif */
808:
1.126 brouard 809: #include <time.h>
810:
1.136 brouard 811: #ifdef GSL
812: #include <gsl/gsl_errno.h>
813: #include <gsl/gsl_multimin.h>
814: #endif
815:
1.167 brouard 816:
1.162 brouard 817: #ifdef NLOPT
818: #include <nlopt.h>
819: typedef struct {
820: double (* function)(double [] );
821: } myfunc_data ;
822: #endif
823:
1.126 brouard 824: /* #include <libintl.h> */
825: /* #define _(String) gettext (String) */
826:
1.141 brouard 827: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 828:
829: #define GNUPLOTPROGRAM "gnuplot"
830: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
831: #define FILENAMELENGTH 132
832:
833: #define GLOCK_ERROR_NOPATH -1 /* empty path */
834: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
835:
1.144 brouard 836: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
837: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 838:
839: #define NINTERVMAX 8
1.144 brouard 840: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
841: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
842: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 843: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 844: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
845: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 846: #define MAXN 20000
1.144 brouard 847: #define YEARM 12. /**< Number of months per year */
1.218 brouard 848: /* #define AGESUP 130 */
849: #define AGESUP 150
850: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 851: #define AGEBASE 40
1.194 brouard 852: #define AGEOVERFLOW 1.e20
1.164 brouard 853: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 854: #ifdef _WIN32
855: #define DIRSEPARATOR '\\'
856: #define CHARSEPARATOR "\\"
857: #define ODIRSEPARATOR '/'
858: #else
1.126 brouard 859: #define DIRSEPARATOR '/'
860: #define CHARSEPARATOR "/"
861: #define ODIRSEPARATOR '\\'
862: #endif
863:
1.226 ! brouard 864: /* $Id: imach.c,v 1.225 2016/07/12 08:40:03 brouard Exp $ */
1.126 brouard 865: /* $State: Exp $ */
1.196 brouard 866: #include "version.h"
867: char version[]=__IMACH_VERSION__;
1.224 brouard 868: 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.226 ! brouard 869: char fullversion[]="$Revision: 1.225 $ $Date: 2016/07/12 08:40:03 $";
1.126 brouard 870: char strstart[80];
871: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 872: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 873: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 874: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
875: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
876: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
1.225 brouard 877: int cptcovs=0; /**< cptcovs number of simple covariates in the model V2+V1 =2 */
878: int cptcovsnq=0; /**< cptcovsnq number of simple covariates in the model but non quantitative V2+V1 =2 */
1.145 brouard 879: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
880: int cptcovprodnoage=0; /**< Number of covariate products without age */
881: int cptcoveff=0; /* Total number of covariates to vary for printing results */
1.224 brouard 882: int ncoveff=0; /* Total number of effective covariates in the model */
1.225 brouard 883: int nqfveff=0; /**< nqfveff Number of Quantitative Fixed Variables Effective */
1.224 brouard 884: int ntveff=0; /**< ntveff number of effective time varying variables */
885: int nqtveff=0; /**< ntqveff number of effective time varying quantitative variables */
1.145 brouard 886: int cptcov=0; /* Working variable */
1.218 brouard 887: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 888: int npar=NPARMAX;
889: int nlstate=2; /* Number of live states */
890: int ndeath=1; /* Number of dead states */
1.130 brouard 891: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.223 brouard 892: int nqv=0, ntv=0, nqtv=0; /* Total number of quantitative variables, time variable (dummy), quantitative and time variable */
1.126 brouard 893: int popbased=0;
894:
895: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 896: int maxwav=0; /* Maxim number of waves */
897: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
898: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
899: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 900: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 901: int mle=1, weightopt=0;
1.126 brouard 902: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
903: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
904: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
905: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 906: int countcallfunc=0; /* Count the number of calls to func */
1.130 brouard 907: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 908: double **matprod2(); /* test */
1.126 brouard 909: double **oldm, **newm, **savm; /* Working pointers to matrices */
910: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 brouard 911: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
912:
1.136 brouard 913: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 914: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 915: FILE *ficlog, *ficrespow;
1.130 brouard 916: int globpr=0; /* Global variable for printing or not */
1.126 brouard 917: double fretone; /* Only one call to likelihood */
1.130 brouard 918: long ipmx=0; /* Number of contributions */
1.126 brouard 919: double sw; /* Sum of weights */
920: char filerespow[FILENAMELENGTH];
921: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
922: FILE *ficresilk;
923: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
924: FILE *ficresprobmorprev;
925: FILE *fichtm, *fichtmcov; /* Html File */
926: FILE *ficreseij;
927: char filerese[FILENAMELENGTH];
928: FILE *ficresstdeij;
929: char fileresstde[FILENAMELENGTH];
930: FILE *ficrescveij;
931: char filerescve[FILENAMELENGTH];
932: FILE *ficresvij;
933: char fileresv[FILENAMELENGTH];
934: FILE *ficresvpl;
935: char fileresvpl[FILENAMELENGTH];
936: char title[MAXLINE];
1.217 brouard 937: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 938: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
939: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
940: char command[FILENAMELENGTH];
941: int outcmd=0;
942:
1.217 brouard 943: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 944: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 945: char filelog[FILENAMELENGTH]; /* Log file */
946: char filerest[FILENAMELENGTH];
947: char fileregp[FILENAMELENGTH];
948: char popfile[FILENAMELENGTH];
949:
950: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
951:
1.157 brouard 952: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
953: /* struct timezone tzp; */
954: /* extern int gettimeofday(); */
955: struct tm tml, *gmtime(), *localtime();
956:
957: extern time_t time();
958:
959: struct tm start_time, end_time, curr_time, last_time, forecast_time;
960: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
961: struct tm tm;
962:
1.126 brouard 963: char strcurr[80], strfor[80];
964:
965: char *endptr;
966: long lval;
967: double dval;
968:
969: #define NR_END 1
970: #define FREE_ARG char*
971: #define FTOL 1.0e-10
972:
973: #define NRANSI
974: #define ITMAX 200
975:
976: #define TOL 2.0e-4
977:
978: #define CGOLD 0.3819660
979: #define ZEPS 1.0e-10
980: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
981:
982: #define GOLD 1.618034
983: #define GLIMIT 100.0
984: #define TINY 1.0e-20
985:
986: static double maxarg1,maxarg2;
987: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
988: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
989:
990: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
991: #define rint(a) floor(a+0.5)
1.166 brouard 992: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 993: #define mytinydouble 1.0e-16
1.166 brouard 994: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
995: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
996: /* static double dsqrarg; */
997: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 998: static double sqrarg;
999: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
1000: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
1001: int agegomp= AGEGOMP;
1002:
1003: int imx;
1004: int stepm=1;
1005: /* Stepm, step in month: minimum step interpolation*/
1006:
1007: int estepm;
1008: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
1009:
1010: int m,nb;
1011: long *num;
1.197 brouard 1012: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 1013: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
1014: covariate for which somebody answered excluding
1015: undefined. Usually 2: 0 and 1. */
1016: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
1017: covariate for which somebody answered including
1018: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 1019: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 brouard 1020: double **pmmij, ***probs; /* Global pointer */
1.219 brouard 1021: double ***mobaverage, ***mobaverages; /* New global variable */
1.126 brouard 1022: double *ageexmed,*agecens;
1023: double dateintmean=0;
1024:
1025: double *weight;
1026: int **s; /* Status */
1.141 brouard 1027: double *agedc;
1.145 brouard 1028: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 1029: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 1030: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.225 brouard 1031: double **coqvar; /* Fixed quantitative covariate iqv */
1032: double ***cotvar; /* Time varying covariate itv */
1033: double ***cotqvar; /* Time varying quantitative covariate itqv */
1.141 brouard 1034: double idx;
1035: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.226 ! brouard 1036: int *Typevar; /**< 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product */
! 1037: int *Fixed; /** Fixed[Tvar[k]] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product */
! 1038: int *Dummy; /** Dummy[Tvar[k]] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product */
1.197 brouard 1039: int *Tage;
1.145 brouard 1040: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 1041: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.220 brouard 1042: int **Tvard, *Tprod, cptcovprod, *Tvaraff, *invalidvarcomb;
1.126 brouard 1043: double *lsurv, *lpop, *tpop;
1044:
1.143 brouard 1045: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
1046: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 1047:
1048: /**************** split *************************/
1049: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
1050: {
1051: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1052: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1053: */
1054: char *ss; /* pointer */
1.186 brouard 1055: int l1=0, l2=0; /* length counters */
1.126 brouard 1056:
1057: l1 = strlen(path ); /* length of path */
1058: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1059: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1060: if ( ss == NULL ) { /* no directory, so determine current directory */
1061: strcpy( name, path ); /* we got the fullname name because no directory */
1062: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1063: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1064: /* get current working directory */
1065: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1066: #ifdef WIN32
1067: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1068: #else
1069: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1070: #endif
1.126 brouard 1071: return( GLOCK_ERROR_GETCWD );
1072: }
1073: /* got dirc from getcwd*/
1074: printf(" DIRC = %s \n",dirc);
1.205 brouard 1075: } else { /* strip directory from path */
1.126 brouard 1076: ss++; /* after this, the filename */
1077: l2 = strlen( ss ); /* length of filename */
1078: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1079: strcpy( name, ss ); /* save file name */
1080: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1081: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1082: printf(" DIRC2 = %s \n",dirc);
1083: }
1084: /* We add a separator at the end of dirc if not exists */
1085: l1 = strlen( dirc ); /* length of directory */
1086: if( dirc[l1-1] != DIRSEPARATOR ){
1087: dirc[l1] = DIRSEPARATOR;
1088: dirc[l1+1] = 0;
1089: printf(" DIRC3 = %s \n",dirc);
1090: }
1091: ss = strrchr( name, '.' ); /* find last / */
1092: if (ss >0){
1093: ss++;
1094: strcpy(ext,ss); /* save extension */
1095: l1= strlen( name);
1096: l2= strlen(ss)+1;
1097: strncpy( finame, name, l1-l2);
1098: finame[l1-l2]= 0;
1099: }
1100:
1101: return( 0 ); /* we're done */
1102: }
1103:
1104:
1105: /******************************************/
1106:
1107: void replace_back_to_slash(char *s, char*t)
1108: {
1109: int i;
1110: int lg=0;
1111: i=0;
1112: lg=strlen(t);
1113: for(i=0; i<= lg; i++) {
1114: (s[i] = t[i]);
1115: if (t[i]== '\\') s[i]='/';
1116: }
1117: }
1118:
1.132 brouard 1119: char *trimbb(char *out, char *in)
1.137 brouard 1120: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1121: char *s;
1122: s=out;
1123: while (*in != '\0'){
1.137 brouard 1124: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1125: in++;
1126: }
1127: *out++ = *in++;
1128: }
1129: *out='\0';
1130: return s;
1131: }
1132:
1.187 brouard 1133: /* char *substrchaine(char *out, char *in, char *chain) */
1134: /* { */
1135: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1136: /* char *s, *t; */
1137: /* t=in;s=out; */
1138: /* while ((*in != *chain) && (*in != '\0')){ */
1139: /* *out++ = *in++; */
1140: /* } */
1141:
1142: /* /\* *in matches *chain *\/ */
1143: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1144: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1145: /* } */
1146: /* in--; chain--; */
1147: /* while ( (*in != '\0')){ */
1148: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1149: /* *out++ = *in++; */
1150: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1151: /* } */
1152: /* *out='\0'; */
1153: /* out=s; */
1154: /* return out; */
1155: /* } */
1156: char *substrchaine(char *out, char *in, char *chain)
1157: {
1158: /* Substract chain 'chain' from 'in', return and output 'out' */
1159: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1160:
1161: char *strloc;
1162:
1163: strcpy (out, in);
1164: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1165: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1166: if(strloc != NULL){
1167: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1168: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1169: /* strcpy (strloc, strloc +strlen(chain));*/
1170: }
1171: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1172: return out;
1173: }
1174:
1175:
1.145 brouard 1176: char *cutl(char *blocc, char *alocc, char *in, char occ)
1177: {
1.187 brouard 1178: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1179: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1180: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1181: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1182: */
1.160 brouard 1183: char *s, *t;
1.145 brouard 1184: t=in;s=in;
1185: while ((*in != occ) && (*in != '\0')){
1186: *alocc++ = *in++;
1187: }
1188: if( *in == occ){
1189: *(alocc)='\0';
1190: s=++in;
1191: }
1192:
1193: if (s == t) {/* occ not found */
1194: *(alocc-(in-s))='\0';
1195: in=s;
1196: }
1197: while ( *in != '\0'){
1198: *blocc++ = *in++;
1199: }
1200:
1201: *blocc='\0';
1202: return t;
1203: }
1.137 brouard 1204: char *cutv(char *blocc, char *alocc, char *in, char occ)
1205: {
1.187 brouard 1206: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1207: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1208: gives blocc="abcdef2ghi" and alocc="j".
1209: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1210: */
1211: char *s, *t;
1212: t=in;s=in;
1213: while (*in != '\0'){
1214: while( *in == occ){
1215: *blocc++ = *in++;
1216: s=in;
1217: }
1218: *blocc++ = *in++;
1219: }
1220: if (s == t) /* occ not found */
1221: *(blocc-(in-s))='\0';
1222: else
1223: *(blocc-(in-s)-1)='\0';
1224: in=s;
1225: while ( *in != '\0'){
1226: *alocc++ = *in++;
1227: }
1228:
1229: *alocc='\0';
1230: return s;
1231: }
1232:
1.126 brouard 1233: int nbocc(char *s, char occ)
1234: {
1235: int i,j=0;
1236: int lg=20;
1237: i=0;
1238: lg=strlen(s);
1239: for(i=0; i<= lg; i++) {
1240: if (s[i] == occ ) j++;
1241: }
1242: return j;
1243: }
1244:
1.137 brouard 1245: /* void cutv(char *u,char *v, char*t, char occ) */
1246: /* { */
1247: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1248: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1249: /* gives u="abcdef2ghi" and v="j" *\/ */
1250: /* int i,lg,j,p=0; */
1251: /* i=0; */
1252: /* lg=strlen(t); */
1253: /* for(j=0; j<=lg-1; j++) { */
1254: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1255: /* } */
1.126 brouard 1256:
1.137 brouard 1257: /* for(j=0; j<p; j++) { */
1258: /* (u[j] = t[j]); */
1259: /* } */
1260: /* u[p]='\0'; */
1.126 brouard 1261:
1.137 brouard 1262: /* for(j=0; j<= lg; j++) { */
1263: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1264: /* } */
1265: /* } */
1.126 brouard 1266:
1.160 brouard 1267: #ifdef _WIN32
1268: char * strsep(char **pp, const char *delim)
1269: {
1270: char *p, *q;
1271:
1272: if ((p = *pp) == NULL)
1273: return 0;
1274: if ((q = strpbrk (p, delim)) != NULL)
1275: {
1276: *pp = q + 1;
1277: *q = '\0';
1278: }
1279: else
1280: *pp = 0;
1281: return p;
1282: }
1283: #endif
1284:
1.126 brouard 1285: /********************** nrerror ********************/
1286:
1287: void nrerror(char error_text[])
1288: {
1289: fprintf(stderr,"ERREUR ...\n");
1290: fprintf(stderr,"%s\n",error_text);
1291: exit(EXIT_FAILURE);
1292: }
1293: /*********************** vector *******************/
1294: double *vector(int nl, int nh)
1295: {
1296: double *v;
1297: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1298: if (!v) nrerror("allocation failure in vector");
1299: return v-nl+NR_END;
1300: }
1301:
1302: /************************ free vector ******************/
1303: void free_vector(double*v, int nl, int nh)
1304: {
1305: free((FREE_ARG)(v+nl-NR_END));
1306: }
1307:
1308: /************************ivector *******************************/
1309: int *ivector(long nl,long nh)
1310: {
1311: int *v;
1312: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1313: if (!v) nrerror("allocation failure in ivector");
1314: return v-nl+NR_END;
1315: }
1316:
1317: /******************free ivector **************************/
1318: void free_ivector(int *v, long nl, long nh)
1319: {
1320: free((FREE_ARG)(v+nl-NR_END));
1321: }
1322:
1323: /************************lvector *******************************/
1324: long *lvector(long nl,long nh)
1325: {
1326: long *v;
1327: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1328: if (!v) nrerror("allocation failure in ivector");
1329: return v-nl+NR_END;
1330: }
1331:
1332: /******************free lvector **************************/
1333: void free_lvector(long *v, long nl, long nh)
1334: {
1335: free((FREE_ARG)(v+nl-NR_END));
1336: }
1337:
1338: /******************* imatrix *******************************/
1339: int **imatrix(long nrl, long nrh, long ncl, long nch)
1340: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1341: {
1342: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1343: int **m;
1344:
1345: /* allocate pointers to rows */
1346: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1347: if (!m) nrerror("allocation failure 1 in matrix()");
1348: m += NR_END;
1349: m -= nrl;
1350:
1351:
1352: /* allocate rows and set pointers to them */
1353: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1354: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1355: m[nrl] += NR_END;
1356: m[nrl] -= ncl;
1357:
1358: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1359:
1360: /* return pointer to array of pointers to rows */
1361: return m;
1362: }
1363:
1364: /****************** free_imatrix *************************/
1365: void free_imatrix(m,nrl,nrh,ncl,nch)
1366: int **m;
1367: long nch,ncl,nrh,nrl;
1368: /* free an int matrix allocated by imatrix() */
1369: {
1370: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1371: free((FREE_ARG) (m+nrl-NR_END));
1372: }
1373:
1374: /******************* matrix *******************************/
1375: double **matrix(long nrl, long nrh, long ncl, long nch)
1376: {
1377: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1378: double **m;
1379:
1380: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1381: if (!m) nrerror("allocation failure 1 in matrix()");
1382: m += NR_END;
1383: m -= nrl;
1384:
1385: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1386: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1387: m[nrl] += NR_END;
1388: m[nrl] -= ncl;
1389:
1390: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1391: return m;
1.145 brouard 1392: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1393: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1394: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1395: */
1396: }
1397:
1398: /*************************free matrix ************************/
1399: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1400: {
1401: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1402: free((FREE_ARG)(m+nrl-NR_END));
1403: }
1404:
1405: /******************* ma3x *******************************/
1406: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1407: {
1408: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1409: double ***m;
1410:
1411: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1412: if (!m) nrerror("allocation failure 1 in matrix()");
1413: m += NR_END;
1414: m -= nrl;
1415:
1416: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1417: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1418: m[nrl] += NR_END;
1419: m[nrl] -= ncl;
1420:
1421: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1422:
1423: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1424: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1425: m[nrl][ncl] += NR_END;
1426: m[nrl][ncl] -= nll;
1427: for (j=ncl+1; j<=nch; j++)
1428: m[nrl][j]=m[nrl][j-1]+nlay;
1429:
1430: for (i=nrl+1; i<=nrh; i++) {
1431: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1432: for (j=ncl+1; j<=nch; j++)
1433: m[i][j]=m[i][j-1]+nlay;
1434: }
1435: return m;
1436: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1437: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1438: */
1439: }
1440:
1441: /*************************free ma3x ************************/
1442: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1443: {
1444: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1445: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1446: free((FREE_ARG)(m+nrl-NR_END));
1447: }
1448:
1449: /*************** function subdirf ***********/
1450: char *subdirf(char fileres[])
1451: {
1452: /* Caution optionfilefiname is hidden */
1453: strcpy(tmpout,optionfilefiname);
1454: strcat(tmpout,"/"); /* Add to the right */
1455: strcat(tmpout,fileres);
1456: return tmpout;
1457: }
1458:
1459: /*************** function subdirf2 ***********/
1460: char *subdirf2(char fileres[], char *preop)
1461: {
1462:
1463: /* Caution optionfilefiname is hidden */
1464: strcpy(tmpout,optionfilefiname);
1465: strcat(tmpout,"/");
1466: strcat(tmpout,preop);
1467: strcat(tmpout,fileres);
1468: return tmpout;
1469: }
1470:
1471: /*************** function subdirf3 ***********/
1472: char *subdirf3(char fileres[], char *preop, char *preop2)
1473: {
1474:
1475: /* Caution optionfilefiname is hidden */
1476: strcpy(tmpout,optionfilefiname);
1477: strcat(tmpout,"/");
1478: strcat(tmpout,preop);
1479: strcat(tmpout,preop2);
1480: strcat(tmpout,fileres);
1481: return tmpout;
1482: }
1.213 brouard 1483:
1484: /*************** function subdirfext ***********/
1485: char *subdirfext(char fileres[], char *preop, char *postop)
1486: {
1487:
1488: strcpy(tmpout,preop);
1489: strcat(tmpout,fileres);
1490: strcat(tmpout,postop);
1491: return tmpout;
1492: }
1.126 brouard 1493:
1.213 brouard 1494: /*************** function subdirfext3 ***********/
1495: char *subdirfext3(char fileres[], char *preop, char *postop)
1496: {
1497:
1498: /* Caution optionfilefiname is hidden */
1499: strcpy(tmpout,optionfilefiname);
1500: strcat(tmpout,"/");
1501: strcat(tmpout,preop);
1502: strcat(tmpout,fileres);
1503: strcat(tmpout,postop);
1504: return tmpout;
1505: }
1506:
1.162 brouard 1507: char *asc_diff_time(long time_sec, char ascdiff[])
1508: {
1509: long sec_left, days, hours, minutes;
1510: days = (time_sec) / (60*60*24);
1511: sec_left = (time_sec) % (60*60*24);
1512: hours = (sec_left) / (60*60) ;
1513: sec_left = (sec_left) %(60*60);
1514: minutes = (sec_left) /60;
1515: sec_left = (sec_left) % (60);
1516: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1517: return ascdiff;
1518: }
1519:
1.126 brouard 1520: /***************** f1dim *************************/
1521: extern int ncom;
1522: extern double *pcom,*xicom;
1523: extern double (*nrfunc)(double []);
1524:
1525: double f1dim(double x)
1526: {
1527: int j;
1528: double f;
1529: double *xt;
1530:
1531: xt=vector(1,ncom);
1532: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1533: f=(*nrfunc)(xt);
1534: free_vector(xt,1,ncom);
1535: return f;
1536: }
1537:
1538: /*****************brent *************************/
1539: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1540: {
1541: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1542: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1543: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1544: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1545: * returned function value.
1546: */
1.126 brouard 1547: int iter;
1548: double a,b,d,etemp;
1.159 brouard 1549: double fu=0,fv,fw,fx;
1.164 brouard 1550: double ftemp=0.;
1.126 brouard 1551: double p,q,r,tol1,tol2,u,v,w,x,xm;
1552: double e=0.0;
1553:
1554: a=(ax < cx ? ax : cx);
1555: b=(ax > cx ? ax : cx);
1556: x=w=v=bx;
1557: fw=fv=fx=(*f)(x);
1558: for (iter=1;iter<=ITMAX;iter++) {
1559: xm=0.5*(a+b);
1560: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1561: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1562: printf(".");fflush(stdout);
1563: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1564: #ifdef DEBUGBRENT
1.126 brouard 1565: 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);
1566: 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);
1567: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1568: #endif
1569: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1570: *xmin=x;
1571: return fx;
1572: }
1573: ftemp=fu;
1574: if (fabs(e) > tol1) {
1575: r=(x-w)*(fx-fv);
1576: q=(x-v)*(fx-fw);
1577: p=(x-v)*q-(x-w)*r;
1578: q=2.0*(q-r);
1579: if (q > 0.0) p = -p;
1580: q=fabs(q);
1581: etemp=e;
1582: e=d;
1583: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1.224 brouard 1584: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1.126 brouard 1585: else {
1.224 brouard 1586: d=p/q;
1587: u=x+d;
1588: if (u-a < tol2 || b-u < tol2)
1589: d=SIGN(tol1,xm-x);
1.126 brouard 1590: }
1591: } else {
1592: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1593: }
1594: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1595: fu=(*f)(u);
1596: if (fu <= fx) {
1597: if (u >= x) a=x; else b=x;
1598: SHFT(v,w,x,u)
1.183 brouard 1599: SHFT(fv,fw,fx,fu)
1600: } else {
1601: if (u < x) a=u; else b=u;
1602: if (fu <= fw || w == x) {
1.224 brouard 1603: v=w;
1604: w=u;
1605: fv=fw;
1606: fw=fu;
1.183 brouard 1607: } else if (fu <= fv || v == x || v == w) {
1.224 brouard 1608: v=u;
1609: fv=fu;
1.183 brouard 1610: }
1611: }
1.126 brouard 1612: }
1613: nrerror("Too many iterations in brent");
1614: *xmin=x;
1615: return fx;
1616: }
1617:
1618: /****************** mnbrak ***********************/
1619:
1620: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1621: double (*func)(double))
1.183 brouard 1622: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1623: the downhill direction (defined by the function as evaluated at the initial points) and returns
1624: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1625: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1626: */
1.126 brouard 1627: double ulim,u,r,q, dum;
1628: double fu;
1.187 brouard 1629:
1630: double scale=10.;
1631: int iterscale=0;
1632:
1633: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1634: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1635:
1636:
1637: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1638: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1639: /* *bx = *ax - (*ax - *bx)/scale; */
1640: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1641: /* } */
1642:
1.126 brouard 1643: if (*fb > *fa) {
1644: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1645: SHFT(dum,*fb,*fa,dum)
1646: }
1.126 brouard 1647: *cx=(*bx)+GOLD*(*bx-*ax);
1648: *fc=(*func)(*cx);
1.183 brouard 1649: #ifdef DEBUG
1.224 brouard 1650: printf("mnbrak0 a=%lf *fa=%lf, b=%lf *fb=%lf, c=%lf *fc=%lf\n",*ax,*fa,*bx,*fb,*cx, *fc);
1651: 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 1652: #endif
1.224 brouard 1653: 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 1654: r=(*bx-*ax)*(*fb-*fc);
1.224 brouard 1655: q=(*bx-*cx)*(*fb-*fa); /* What if fa=inf */
1.126 brouard 1656: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1657: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1658: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1659: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1660: fu=(*func)(u);
1.163 brouard 1661: #ifdef DEBUG
1662: /* f(x)=A(x-u)**2+f(u) */
1663: double A, fparabu;
1664: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1665: fparabu= *fa - A*(*ax-u)*(*ax-u);
1.224 brouard 1666: 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);
1667: 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 1668: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1669: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1670: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1671: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1672: #endif
1.184 brouard 1673: #ifdef MNBRAKORIGINAL
1.183 brouard 1674: #else
1.191 brouard 1675: /* if (fu > *fc) { */
1676: /* #ifdef DEBUG */
1677: /* printf("mnbrak4 fu > fc \n"); */
1678: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1679: /* #endif */
1680: /* /\* 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 *\\/ *\/ */
1681: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1682: /* dum=u; /\* Shifting c and u *\/ */
1683: /* u = *cx; */
1684: /* *cx = dum; */
1685: /* dum = fu; */
1686: /* fu = *fc; */
1687: /* *fc =dum; */
1688: /* } else { /\* end *\/ */
1689: /* #ifdef DEBUG */
1690: /* printf("mnbrak3 fu < fc \n"); */
1691: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1692: /* #endif */
1693: /* dum=u; /\* Shifting c and u *\/ */
1694: /* u = *cx; */
1695: /* *cx = dum; */
1696: /* dum = fu; */
1697: /* fu = *fc; */
1698: /* *fc =dum; */
1699: /* } */
1.224 brouard 1700: #ifdef DEBUGMNBRAK
1701: double A, fparabu;
1702: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1703: fparabu= *fa - A*(*ax-u)*(*ax-u);
1704: 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);
1705: 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 1706: #endif
1.191 brouard 1707: dum=u; /* Shifting c and u */
1708: u = *cx;
1709: *cx = dum;
1710: dum = fu;
1711: fu = *fc;
1712: *fc =dum;
1.183 brouard 1713: #endif
1.162 brouard 1714: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1715: #ifdef DEBUG
1.224 brouard 1716: printf("\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1717: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim\n",u,*cx);
1.183 brouard 1718: #endif
1.126 brouard 1719: fu=(*func)(u);
1720: if (fu < *fc) {
1.183 brouard 1721: #ifdef DEBUG
1.224 brouard 1722: printf("\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1723: fprintf(ficlog,"\nmnbrak2 u=%lf after c=%lf but before ulim=%lf AND fu=%lf < %lf=fc\n",u,*cx,ulim,fu, *fc);
1724: #endif
1725: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1726: SHFT(*fb,*fc,fu,(*func)(u))
1727: #ifdef DEBUG
1728: printf("\nmnbrak2 shift GOLD c=%lf",*cx+GOLD*(*cx-*bx));
1.183 brouard 1729: #endif
1730: }
1.162 brouard 1731: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1732: #ifdef DEBUG
1.224 brouard 1733: printf("\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1734: fprintf(ficlog,"\nmnbrak2 u=%lf outside ulim=%lf (verifying that ulim is beyond c=%lf)\n",u,ulim,*cx);
1.183 brouard 1735: #endif
1.126 brouard 1736: u=ulim;
1737: fu=(*func)(u);
1.183 brouard 1738: } else { /* u could be left to b (if r > q parabola has a maximum) */
1739: #ifdef DEBUG
1.224 brouard 1740: printf("\nmnbrak2 u=%lf could be left to b=%lf (if r=%lf > q=%lf parabola has a maximum)\n",u,*bx,r,q);
1741: 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 1742: #endif
1.126 brouard 1743: u=(*cx)+GOLD*(*cx-*bx);
1744: fu=(*func)(u);
1.224 brouard 1745: #ifdef DEBUG
1746: printf("\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1747: fprintf(ficlog,"\nmnbrak2 new u=%lf fu=%lf shifted gold left from c=%lf and b=%lf \n",u,fu,*cx,*bx);
1748: #endif
1.183 brouard 1749: } /* end tests */
1.126 brouard 1750: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1751: SHFT(*fa,*fb,*fc,fu)
1752: #ifdef DEBUG
1.224 brouard 1753: printf("\nmnbrak2 shift (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc);
1754: 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 1755: #endif
1756: } /* 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 1757: }
1758:
1759: /*************** linmin ************************/
1.162 brouard 1760: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1761: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1762: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1763: the value of func at the returned location p . This is actually all accomplished by calling the
1764: routines mnbrak and brent .*/
1.126 brouard 1765: int ncom;
1766: double *pcom,*xicom;
1767: double (*nrfunc)(double []);
1768:
1.224 brouard 1769: #ifdef LINMINORIGINAL
1.126 brouard 1770: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1.224 brouard 1771: #else
1772: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []), int *flat)
1773: #endif
1.126 brouard 1774: {
1775: double brent(double ax, double bx, double cx,
1776: double (*f)(double), double tol, double *xmin);
1777: double f1dim(double x);
1778: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1779: double *fc, double (*func)(double));
1780: int j;
1781: double xx,xmin,bx,ax;
1782: double fx,fb,fa;
1.187 brouard 1783:
1.203 brouard 1784: #ifdef LINMINORIGINAL
1785: #else
1786: double scale=10., axs, xxs; /* Scale added for infinity */
1787: #endif
1788:
1.126 brouard 1789: ncom=n;
1790: pcom=vector(1,n);
1791: xicom=vector(1,n);
1792: nrfunc=func;
1793: for (j=1;j<=n;j++) {
1794: pcom[j]=p[j];
1.202 brouard 1795: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1796: }
1.187 brouard 1797:
1.203 brouard 1798: #ifdef LINMINORIGINAL
1799: xx=1.;
1800: #else
1801: axs=0.0;
1802: xxs=1.;
1803: do{
1804: xx= xxs;
1805: #endif
1.187 brouard 1806: ax=0.;
1807: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1808: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1809: /* 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)) */
1810: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1811: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1812: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1813: /* 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 1814: #ifdef LINMINORIGINAL
1815: #else
1816: if (fx != fx){
1.224 brouard 1817: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1818: printf("|");
1819: fprintf(ficlog,"|");
1.203 brouard 1820: #ifdef DEBUGLINMIN
1.224 brouard 1821: 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 1822: #endif
1823: }
1.224 brouard 1824: }while(fx != fx && xxs > 1.e-5);
1.203 brouard 1825: #endif
1826:
1.191 brouard 1827: #ifdef DEBUGLINMIN
1828: 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 1829: 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 1830: #endif
1.224 brouard 1831: #ifdef LINMINORIGINAL
1832: #else
1833: if(fb == fx){ /* Flat function in the direction */
1834: xmin=xx;
1835: *flat=1;
1836: }else{
1837: *flat=0;
1838: #endif
1839: /*Flat mnbrak2 shift (*ax=0.000000000000, *fa=51626.272983130431), (*bx=-1.618034000000, *fb=51590.149499362531), (*cx=-4.236068025156, *fc=51590.149499362531) */
1.187 brouard 1840: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
1841: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
1842: /* fmin = f(p[j] + xmin * xi[j]) */
1843: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
1844: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 1845: #ifdef DEBUG
1.224 brouard 1846: 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);
1847: 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);
1848: #endif
1849: #ifdef LINMINORIGINAL
1850: #else
1851: }
1.126 brouard 1852: #endif
1.191 brouard 1853: #ifdef DEBUGLINMIN
1854: printf("linmin end ");
1.202 brouard 1855: fprintf(ficlog,"linmin end ");
1.191 brouard 1856: #endif
1.126 brouard 1857: for (j=1;j<=n;j++) {
1.203 brouard 1858: #ifdef LINMINORIGINAL
1859: xi[j] *= xmin;
1860: #else
1861: #ifdef DEBUGLINMIN
1862: if(xxs <1.0)
1863: printf(" before xi[%d]=%12.8f", j,xi[j]);
1864: #endif
1865: 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) */
1866: #ifdef DEBUGLINMIN
1867: if(xxs <1.0)
1868: 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 );
1869: #endif
1870: #endif
1.187 brouard 1871: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 1872: }
1.191 brouard 1873: #ifdef DEBUGLINMIN
1.203 brouard 1874: printf("\n");
1.191 brouard 1875: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 1876: 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 1877: for (j=1;j<=n;j++) {
1.202 brouard 1878: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1879: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1880: if(j % ncovmodel == 0){
1.191 brouard 1881: printf("\n");
1.202 brouard 1882: fprintf(ficlog,"\n");
1883: }
1.191 brouard 1884: }
1.203 brouard 1885: #else
1.191 brouard 1886: #endif
1.126 brouard 1887: free_vector(xicom,1,n);
1888: free_vector(pcom,1,n);
1889: }
1890:
1891:
1892: /*************** powell ************************/
1.162 brouard 1893: /*
1894: Minimization of a function func of n variables. Input consists of an initial starting point
1895: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
1896: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
1897: such that failure to decrease by more than this amount on one iteration signals doneness. On
1898: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
1899: function value at p , and iter is the number of iterations taken. The routine linmin is used.
1900: */
1.224 brouard 1901: #ifdef LINMINORIGINAL
1902: #else
1903: int *flatdir; /* Function is vanishing in that direction */
1.225 brouard 1904: int flat=0, flatd=0; /* Function is vanishing in that direction */
1.224 brouard 1905: #endif
1.126 brouard 1906: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
1907: double (*func)(double []))
1908: {
1.224 brouard 1909: #ifdef LINMINORIGINAL
1910: void linmin(double p[], double xi[], int n, double *fret,
1.126 brouard 1911: double (*func)(double []));
1.224 brouard 1912: #else
1913: void linmin(double p[], double xi[], int n, double *fret,
1914: double (*func)(double []),int *flat);
1915: #endif
1.126 brouard 1916: int i,ibig,j;
1917: double del,t,*pt,*ptt,*xit;
1.181 brouard 1918: double directest;
1.126 brouard 1919: double fp,fptt;
1920: double *xits;
1921: int niterf, itmp;
1.224 brouard 1922: #ifdef LINMINORIGINAL
1923: #else
1924:
1925: flatdir=ivector(1,n);
1926: for (j=1;j<=n;j++) flatdir[j]=0;
1927: #endif
1.126 brouard 1928:
1929: pt=vector(1,n);
1930: ptt=vector(1,n);
1931: xit=vector(1,n);
1932: xits=vector(1,n);
1933: *fret=(*func)(p);
1934: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 1935: rcurr_time = time(NULL);
1.126 brouard 1936: for (*iter=1;;++(*iter)) {
1.187 brouard 1937: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 1938: ibig=0;
1939: del=0.0;
1.157 brouard 1940: rlast_time=rcurr_time;
1941: /* (void) gettimeofday(&curr_time,&tzp); */
1942: rcurr_time = time(NULL);
1943: curr_time = *localtime(&rcurr_time);
1944: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
1945: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
1946: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 1947: for (i=1;i<=n;i++) {
1.126 brouard 1948: printf(" %d %.12f",i, p[i]);
1949: fprintf(ficlog," %d %.12lf",i, p[i]);
1950: fprintf(ficrespow," %.12lf", p[i]);
1951: }
1952: printf("\n");
1953: fprintf(ficlog,"\n");
1954: fprintf(ficrespow,"\n");fflush(ficrespow);
1955: if(*iter <=3){
1.157 brouard 1956: tml = *localtime(&rcurr_time);
1957: strcpy(strcurr,asctime(&tml));
1958: rforecast_time=rcurr_time;
1.126 brouard 1959: itmp = strlen(strcurr);
1960: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1.224 brouard 1961: strcurr[itmp-1]='\0';
1.162 brouard 1962: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 1963: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 1964: for(niterf=10;niterf<=30;niterf+=10){
1.224 brouard 1965: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
1966: forecast_time = *localtime(&rforecast_time);
1967: strcpy(strfor,asctime(&forecast_time));
1968: itmp = strlen(strfor);
1969: if(strfor[itmp-1]=='\n')
1970: strfor[itmp-1]='\0';
1971: 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);
1972: 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 1973: }
1974: }
1.187 brouard 1975: for (i=1;i<=n;i++) { /* For each direction i */
1976: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 1977: fptt=(*fret);
1978: #ifdef DEBUG
1.203 brouard 1979: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1980: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 1981: #endif
1.203 brouard 1982: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 1983: fprintf(ficlog,"%d",i);fflush(ficlog);
1.224 brouard 1984: #ifdef LINMINORIGINAL
1.188 brouard 1985: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1.224 brouard 1986: #else
1987: linmin(p,xit,n,fret,func,&flat); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1988: flatdir[i]=flat; /* Function is vanishing in that direction i */
1989: #endif
1990: /* Outputs are fret(new point p) p is updated and xit rescaled */
1.188 brouard 1991: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1.224 brouard 1992: /* because that direction will be replaced unless the gain del is small */
1993: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
1994: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
1995: /* with the new direction. */
1996: del=fabs(fptt-(*fret));
1997: ibig=i;
1.126 brouard 1998: }
1999: #ifdef DEBUG
2000: printf("%d %.12e",i,(*fret));
2001: fprintf(ficlog,"%d %.12e",i,(*fret));
2002: for (j=1;j<=n;j++) {
1.224 brouard 2003: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
2004: printf(" x(%d)=%.12e",j,xit[j]);
2005: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1.126 brouard 2006: }
2007: for(j=1;j<=n;j++) {
1.225 brouard 2008: printf(" p(%d)=%.12e",j,p[j]);
2009: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 2010: }
2011: printf("\n");
2012: fprintf(ficlog,"\n");
2013: #endif
1.187 brouard 2014: } /* end loop on each direction i */
2015: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 2016: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 2017: /* New value of last point Pn is not computed, P(n-1) */
1.224 brouard 2018: for(j=1;j<=n;j++) {
1.225 brouard 2019: if(flatdir[j] >0){
2020: printf(" p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2021: fprintf(ficlog," p(%d)=%lf flat=%d ",j,p[j],flatdir[j]);
2022: }
2023: /* printf("\n"); */
2024: /* fprintf(ficlog,"\n"); */
2025: }
1.182 brouard 2026: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /* Did we reach enough precision? */
1.188 brouard 2027: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
2028: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
2029: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
2030: /* decreased of more than 3.84 */
2031: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
2032: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
2033: /* By adding 10 parameters more the gain should be 18.31 */
1.224 brouard 2034:
1.188 brouard 2035: /* Starting the program with initial values given by a former maximization will simply change */
2036: /* the scales of the directions and the directions, because the are reset to canonical directions */
2037: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
2038: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 2039: #ifdef DEBUG
2040: int k[2],l;
2041: k[0]=1;
2042: k[1]=-1;
2043: printf("Max: %.12e",(*func)(p));
2044: fprintf(ficlog,"Max: %.12e",(*func)(p));
2045: for (j=1;j<=n;j++) {
2046: printf(" %.12e",p[j]);
2047: fprintf(ficlog," %.12e",p[j]);
2048: }
2049: printf("\n");
2050: fprintf(ficlog,"\n");
2051: for(l=0;l<=1;l++) {
2052: for (j=1;j<=n;j++) {
2053: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
2054: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2055: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
2056: }
2057: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2058: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
2059: }
2060: #endif
2061:
1.224 brouard 2062: #ifdef LINMINORIGINAL
2063: #else
2064: free_ivector(flatdir,1,n);
2065: #endif
1.126 brouard 2066: free_vector(xit,1,n);
2067: free_vector(xits,1,n);
2068: free_vector(ptt,1,n);
2069: free_vector(pt,1,n);
2070: return;
1.192 brouard 2071: } /* enough precision */
1.126 brouard 2072: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.181 brouard 2073: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 2074: ptt[j]=2.0*p[j]-pt[j];
2075: xit[j]=p[j]-pt[j];
2076: pt[j]=p[j];
2077: }
1.181 brouard 2078: fptt=(*func)(ptt); /* f_3 */
1.224 brouard 2079: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
2080: if (*iter <=4) {
1.225 brouard 2081: #else
2082: #endif
1.224 brouard 2083: #ifdef POWELLNOF3INFF1TEST /* skips test F3 <F1 */
1.192 brouard 2084: #else
1.161 brouard 2085: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 2086: #endif
1.162 brouard 2087: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 2088: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 2089: /* Let f"(x2) be the 2nd derivative equal everywhere. */
2090: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
2091: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.224 brouard 2092: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del or directest <0 */
2093: /* also lamda^2=(f1-f2)^2/mu² is a parasite solution of powell */
2094: /* For powell, inclusion of this average direction is only if t(del)<0 or del inbetween mu^2 and lambda^2 */
1.161 brouard 2095: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.224 brouard 2096: /* Even if f3 <f1, directest can be negative and t >0 */
2097: /* mu² and del² are equal when f3=f1 */
2098: /* f3 < f1 : mu² < del <= lambda^2 both test are equivalent */
2099: /* f3 < f1 : mu² < lambda^2 < del then directtest is negative and powell t is positive */
2100: /* f3 > f1 : lambda² < mu^2 < del then t is negative and directest >0 */
2101: /* f3 > f1 : lambda² < del < mu^2 then t is positive and directest >0 */
1.183 brouard 2102: #ifdef NRCORIGINAL
2103: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
2104: #else
2105: 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 2106: t= t- del*SQR(fp-fptt);
1.183 brouard 2107: #endif
1.202 brouard 2108: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 2109: #ifdef DEBUG
1.181 brouard 2110: 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);
2111: 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 2112: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2113: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2114: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
2115: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
2116: 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);
2117: 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);
2118: #endif
1.183 brouard 2119: #ifdef POWELLORIGINAL
2120: if (t < 0.0) { /* Then we use it for new direction */
2121: #else
1.182 brouard 2122: if (directest*t < 0.0) { /* Contradiction between both tests */
1.224 brouard 2123: 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 2124: 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 2125: 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 2126: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2127: }
1.181 brouard 2128: if (directest < 0.0) { /* Then we use it for new direction */
2129: #endif
1.191 brouard 2130: #ifdef DEBUGLINMIN
1.224 brouard 2131: printf("Before linmin in direction P%d-P0\n",n);
2132: for (j=1;j<=n;j++) {
2133: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2134: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2135: if(j % ncovmodel == 0){
2136: printf("\n");
2137: fprintf(ficlog,"\n");
2138: }
2139: }
2140: #endif
2141: #ifdef LINMINORIGINAL
2142: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2143: #else
2144: linmin(p,xit,n,fret,func,&flat); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
2145: flatdir[i]=flat; /* Function is vanishing in that direction i */
1.191 brouard 2146: #endif
1.224 brouard 2147:
1.191 brouard 2148: #ifdef DEBUGLINMIN
1.224 brouard 2149: for (j=1;j<=n;j++) {
2150: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2151: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2152: if(j % ncovmodel == 0){
2153: printf("\n");
2154: fprintf(ficlog,"\n");
2155: }
2156: }
2157: #endif
2158: for (j=1;j<=n;j++) {
2159: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2160: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
2161: }
2162: #ifdef LINMINORIGINAL
2163: #else
1.225 brouard 2164: for (j=1, flatd=0;j<=n;j++) {
2165: if(flatdir[j]>0)
2166: flatd++;
2167: }
2168: if(flatd >0){
2169: printf("%d flat directions\n",flatd);
2170: fprintf(ficlog,"%d flat directions\n",flatd);
2171: for (j=1;j<=n;j++) {
2172: if(flatdir[j]>0){
2173: printf("%d ",j);
2174: fprintf(ficlog,"%d ",j);
2175: }
2176: }
2177: printf("\n");
2178: fprintf(ficlog,"\n");
2179: }
1.191 brouard 2180: #endif
1.224 brouard 2181: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2182: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2183:
1.126 brouard 2184: #ifdef DEBUG
1.224 brouard 2185: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2186: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2187: for(j=1;j<=n;j++){
2188: printf(" %lf",xit[j]);
2189: fprintf(ficlog," %lf",xit[j]);
2190: }
2191: printf("\n");
2192: fprintf(ficlog,"\n");
1.126 brouard 2193: #endif
1.192 brouard 2194: } /* end of t or directest negative */
1.224 brouard 2195: #ifdef POWELLNOF3INFF1TEST
1.192 brouard 2196: #else
1.162 brouard 2197: } /* end if (fptt < fp) */
1.192 brouard 2198: #endif
1.225 brouard 2199: #ifdef NODIRECTIONCHANGEDUNTILNITER /* No change in drections until some iterations are done */
1.224 brouard 2200: } /*NODIRECTIONCHANGEDUNTILNITER No change in drections until some iterations are done */
1.225 brouard 2201: #else
1.224 brouard 2202: #endif
1.192 brouard 2203: } /* loop iteration */
1.126 brouard 2204: }
2205:
2206: /**** Prevalence limit (stable or period prevalence) ****************/
2207:
1.203 brouard 2208: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij)
1.126 brouard 2209: {
1.218 brouard 2210: /* Computes the prevalence limit in each live state at age x and for covariate ij by left multiplying the unit
1.203 brouard 2211: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2212: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2213: /* Wx is row vector: population in state 1, population in state 2, population dead */
2214: /* or prevalence in state 1, prevalence in state 2, 0 */
2215: /* newm is the matrix after multiplications, its rows are identical at a factor */
2216: /* Initial matrix pimij */
2217: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2218: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2219: /* 0, 0 , 1} */
2220: /*
2221: * and after some iteration: */
2222: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2223: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2224: /* 0, 0 , 1} */
2225: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2226: /* {0.51571254859325999, 0.4842874514067399, */
2227: /* 0.51326036147820708, 0.48673963852179264} */
2228: /* If we start from prlim again, prlim tends to a constant matrix */
2229:
1.126 brouard 2230: int i, ii,j,k;
1.209 brouard 2231: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2232: /* double **matprod2(); */ /* test */
1.218 brouard 2233: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2234: double **newm;
1.209 brouard 2235: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2236: int ncvloop=0;
1.169 brouard 2237:
1.209 brouard 2238: min=vector(1,nlstate);
2239: max=vector(1,nlstate);
2240: meandiff=vector(1,nlstate);
2241:
1.218 brouard 2242: /* Starting with matrix unity */
1.126 brouard 2243: for (ii=1;ii<=nlstate+ndeath;ii++)
2244: for (j=1;j<=nlstate+ndeath;j++){
2245: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2246: }
1.169 brouard 2247:
2248: cov[1]=1.;
2249:
2250: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2251: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2252: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2253: ncvloop++;
1.126 brouard 2254: newm=savm;
2255: /* Covariates have to be included here again */
1.138 brouard 2256: cov[2]=agefin;
1.187 brouard 2257: if(nagesqr==1)
2258: cov[3]= agefin*agefin;;
1.138 brouard 2259: for (k=1; k<=cptcovn;k++) {
1.200 brouard 2260: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.218 brouard 2261: /* Here comes the value of the covariate 'ij' */
1.200 brouard 2262: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
1.198 brouard 2263: /* 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 2264: }
1.186 brouard 2265: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.200 brouard 2266: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2267: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
1.186 brouard 2268: for (k=1; k<=cptcovprod;k++) /* Useless */
1.200 brouard 2269: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2270: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
1.138 brouard 2271:
2272: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2273: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2274: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2275: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2276: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2277: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2278: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2279:
1.126 brouard 2280: savm=oldm;
2281: oldm=newm;
1.209 brouard 2282:
2283: for(j=1; j<=nlstate; j++){
2284: max[j]=0.;
2285: min[j]=1.;
2286: }
2287: for(i=1;i<=nlstate;i++){
2288: sumnew=0;
2289: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2290: for(j=1; j<=nlstate; j++){
2291: prlim[i][j]= newm[i][j]/(1-sumnew);
2292: max[j]=FMAX(max[j],prlim[i][j]);
2293: min[j]=FMIN(min[j],prlim[i][j]);
2294: }
2295: }
2296:
1.126 brouard 2297: maxmax=0.;
1.209 brouard 2298: for(j=1; j<=nlstate; j++){
2299: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2300: maxmax=FMAX(maxmax,meandiff[j]);
2301: /* 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 2302: } /* j loop */
1.203 brouard 2303: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2304: /* 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 2305: if(maxmax < ftolpl){
1.209 brouard 2306: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2307: free_vector(min,1,nlstate);
2308: free_vector(max,1,nlstate);
2309: free_vector(meandiff,1,nlstate);
1.126 brouard 2310: return prlim;
2311: }
1.169 brouard 2312: } /* age loop */
1.208 brouard 2313: /* After some age loop it doesn't converge */
1.209 brouard 2314: 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 2315: 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 2316: /* 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); */
2317: free_vector(min,1,nlstate);
2318: free_vector(max,1,nlstate);
2319: free_vector(meandiff,1,nlstate);
1.208 brouard 2320:
1.169 brouard 2321: return prlim; /* should not reach here */
1.126 brouard 2322: }
2323:
1.217 brouard 2324:
2325: /**** Back Prevalence limit (stable or period prevalence) ****************/
2326:
1.218 brouard 2327: /* 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) */
2328: /* 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) */
2329: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij)
1.217 brouard 2330: {
1.218 brouard 2331: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2332: matrix by transitions matrix until convergence is reached with precision ftolpl */
2333: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2334: /* Wx is row vector: population in state 1, population in state 2, population dead */
2335: /* or prevalence in state 1, prevalence in state 2, 0 */
2336: /* newm is the matrix after multiplications, its rows are identical at a factor */
2337: /* Initial matrix pimij */
2338: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2339: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2340: /* 0, 0 , 1} */
2341: /*
2342: * and after some iteration: */
2343: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2344: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2345: /* 0, 0 , 1} */
2346: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2347: /* {0.51571254859325999, 0.4842874514067399, */
2348: /* 0.51326036147820708, 0.48673963852179264} */
2349: /* If we start from prlim again, prlim tends to a constant matrix */
2350:
2351: int i, ii,j,k;
2352: double *min, *max, *meandiff, maxmax,sumnew=0.;
2353: /* double **matprod2(); */ /* test */
2354: double **out, cov[NCOVMAX+1], **bmij();
2355: double **newm;
1.218 brouard 2356: double **dnewm, **doldm, **dsavm; /* for use */
2357: double **oldm, **savm; /* for use */
2358:
1.217 brouard 2359: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2360: int ncvloop=0;
2361:
2362: min=vector(1,nlstate);
2363: max=vector(1,nlstate);
2364: meandiff=vector(1,nlstate);
2365:
1.218 brouard 2366: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
2367: oldm=oldms; savm=savms;
2368:
2369: /* Starting with matrix unity */
2370: for (ii=1;ii<=nlstate+ndeath;ii++)
2371: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2372: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2373: }
2374:
2375: cov[1]=1.;
2376:
2377: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2378: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 brouard 2379: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
2380: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2381: ncvloop++;
1.218 brouard 2382: newm=savm; /* oldm should be kept from previous iteration or unity at start */
2383: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2384: /* Covariates have to be included here again */
2385: cov[2]=agefin;
2386: if(nagesqr==1)
2387: cov[3]= agefin*agefin;;
2388: for (k=1; k<=cptcovn;k++) {
2389: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
2390: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2391: /* 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])]); */
2392: }
2393: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2394: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2395: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
2396: for (k=1; k<=cptcovprod;k++) /* Useless */
2397: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2398: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2399:
2400: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2401: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2402: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2403: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2404: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 brouard 2405: /* ij should be linked to the correct index of cov */
2406: /* age and covariate values ij are in 'cov', but we need to pass
2407: * ij for the observed prevalence at age and status and covariate
2408: * number: prevacurrent[(int)agefin][ii][ij]
2409: */
2410: /* 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 *\/ */
2411: /* 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 *\/ */
2412: 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 2413: savm=oldm;
2414: oldm=newm;
2415: for(j=1; j<=nlstate; j++){
2416: max[j]=0.;
2417: min[j]=1.;
2418: }
2419: for(j=1; j<=nlstate; j++){
2420: for(i=1;i<=nlstate;i++){
1.218 brouard 2421: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
2422: bprlim[i][j]= newm[i][j];
2423: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
2424: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2425: }
2426: }
1.218 brouard 2427:
1.217 brouard 2428: maxmax=0.;
2429: for(i=1; i<=nlstate; i++){
2430: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2431: maxmax=FMAX(maxmax,meandiff[i]);
2432: /* 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); */
2433: } /* j loop */
2434: *ncvyear= -( (int)age- (int)agefin);
1.218 brouard 2435: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2436: if(maxmax < ftolpl){
1.220 brouard 2437: /* printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
1.217 brouard 2438: free_vector(min,1,nlstate);
2439: free_vector(max,1,nlstate);
2440: free_vector(meandiff,1,nlstate);
2441: return bprlim;
2442: }
2443: } /* age loop */
2444: /* After some age loop it doesn't converge */
2445: 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\
2446: 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);
2447: /* 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); */
2448: free_vector(min,1,nlstate);
2449: free_vector(max,1,nlstate);
2450: free_vector(meandiff,1,nlstate);
2451:
2452: return bprlim; /* should not reach here */
2453: }
2454:
1.126 brouard 2455: /*************** transition probabilities ***************/
2456:
2457: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2458: {
1.138 brouard 2459: /* According to parameters values stored in x and the covariate's values stored in cov,
2460: computes the probability to be observed in state j being in state i by appying the
2461: model to the ncovmodel covariates (including constant and age).
2462: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2463: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2464: ncth covariate in the global vector x is given by the formula:
2465: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2466: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2467: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2468: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2469: Outputs ps[i][j] the probability to be observed in j being in j according to
2470: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2471: */
2472: double s1, lnpijopii;
1.126 brouard 2473: /*double t34;*/
1.164 brouard 2474: int i,j, nc, ii, jj;
1.126 brouard 2475:
1.223 brouard 2476: for(i=1; i<= nlstate; i++){
2477: for(j=1; j<i;j++){
2478: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2479: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2480: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2481: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2482: }
2483: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2484: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2485: }
2486: for(j=i+1; j<=nlstate+ndeath;j++){
2487: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2488: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2489: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2490: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2491: }
2492: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2493: }
2494: }
1.218 brouard 2495:
1.223 brouard 2496: for(i=1; i<= nlstate; i++){
2497: s1=0;
2498: for(j=1; j<i; j++){
2499: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2500: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2501: }
2502: for(j=i+1; j<=nlstate+ndeath; j++){
2503: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2504: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2505: }
2506: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2507: ps[i][i]=1./(s1+1.);
2508: /* Computing other pijs */
2509: for(j=1; j<i; j++)
2510: ps[i][j]= exp(ps[i][j])*ps[i][i];
2511: for(j=i+1; j<=nlstate+ndeath; j++)
2512: ps[i][j]= exp(ps[i][j])*ps[i][i];
2513: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2514: } /* end i */
1.218 brouard 2515:
1.223 brouard 2516: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2517: for(jj=1; jj<= nlstate+ndeath; jj++){
2518: ps[ii][jj]=0;
2519: ps[ii][ii]=1;
2520: }
2521: }
1.218 brouard 2522:
2523:
1.223 brouard 2524: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2525: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2526: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2527: /* } */
2528: /* printf("\n "); */
2529: /* } */
2530: /* printf("\n ");printf("%lf ",cov[2]);*/
2531: /*
2532: for(i=1; i<= npar; i++) printf("%f ",x[i]);
1.218 brouard 2533: goto end;*/
1.223 brouard 2534: return ps;
1.126 brouard 2535: }
2536:
1.218 brouard 2537: /*************** backward transition probabilities ***************/
2538:
2539: /* 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 ) */
2540: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
2541: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
2542: {
1.222 brouard 2543: /* Computes the backward probability at age agefin and covariate ij
2544: * and returns in **ps as well as **bmij.
2545: */
1.218 brouard 2546: int i, ii, j,k;
1.222 brouard 2547:
2548: double **out, **pmij();
2549: double sumnew=0.;
1.218 brouard 2550: double agefin;
1.222 brouard 2551:
2552: double **dnewm, **dsavm, **doldm;
2553: double **bbmij;
2554:
1.218 brouard 2555: doldm=ddoldms; /* global pointers */
1.222 brouard 2556: dnewm=ddnewms;
2557: dsavm=ddsavms;
2558:
2559: agefin=cov[2];
2560: /* bmij *//* age is cov[2], ij is included in cov, but we need for
2561: the observed prevalence (with this covariate ij) */
2562: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
2563: /* We do have the matrix Px in savm and we need pij */
2564: for (j=1;j<=nlstate+ndeath;j++){
2565: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
2566: for (ii=1;ii<=nlstate;ii++){
2567: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
2568: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
2569: for (ii=1;ii<=nlstate+ndeath;ii++){
2570: if(sumnew >= 1.e-10){
2571: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
2572: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2573: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
2574: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
2575: /* }else */
2576: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
2577: }else{
2578: 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);
2579: }
2580: } /*End ii */
2581: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
2582: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
2583: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
2584: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
2585: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2586: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
2587: /* left Product of this matrix by diag matrix of prevalences (savm) */
2588: for (j=1;j<=nlstate+ndeath;j++){
2589: for (ii=1;ii<=nlstate+ndeath;ii++){
2590: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
2591: }
2592: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
2593: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
2594: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
2595: /* end bmij */
2596: return ps;
1.218 brouard 2597: }
1.217 brouard 2598: /*************** transition probabilities ***************/
2599:
1.218 brouard 2600: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2601: {
2602: /* According to parameters values stored in x and the covariate's values stored in cov,
2603: computes the probability to be observed in state j being in state i by appying the
2604: model to the ncovmodel covariates (including constant and age).
2605: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2606: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2607: ncth covariate in the global vector x is given by the formula:
2608: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2609: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2610: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2611: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2612: Outputs ps[i][j] the probability to be observed in j being in j according to
2613: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2614: */
2615: double s1, lnpijopii;
2616: /*double t34;*/
2617: int i,j, nc, ii, jj;
2618:
1.218 brouard 2619: for(i=1; i<= nlstate; i++){
2620: for(j=1; j<i;j++){
2621: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2622: /*lnpijopii += param[i][j][nc]*cov[nc];*/
2623: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
2624: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2625: }
2626: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2627: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
2628: }
2629: for(j=i+1; j<=nlstate+ndeath;j++){
2630: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
2631: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
2632: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
2633: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
2634: }
2635: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
2636: }
2637: }
2638:
2639: for(i=1; i<= nlstate; i++){
2640: s1=0;
2641: for(j=1; j<i; j++){
2642: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2643: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2644: }
2645: for(j=i+1; j<=nlstate+ndeath; j++){
2646: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
2647: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
2648: }
2649: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
2650: ps[i][i]=1./(s1+1.);
2651: /* Computing other pijs */
2652: for(j=1; j<i; j++)
2653: ps[i][j]= exp(ps[i][j])*ps[i][i];
2654: for(j=i+1; j<=nlstate+ndeath; j++)
2655: ps[i][j]= exp(ps[i][j])*ps[i][i];
2656: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
2657: } /* end i */
2658:
2659: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
2660: for(jj=1; jj<= nlstate+ndeath; jj++){
2661: ps[ii][jj]=0;
2662: ps[ii][ii]=1;
2663: }
2664: }
2665: /* Added for backcast */ /* Transposed matrix too */
2666: for(jj=1; jj<= nlstate+ndeath; jj++){
2667: s1=0.;
2668: for(ii=1; ii<= nlstate+ndeath; ii++){
2669: s1+=ps[ii][jj];
2670: }
2671: for(ii=1; ii<= nlstate; ii++){
2672: ps[ii][jj]=ps[ii][jj]/s1;
2673: }
2674: }
2675: /* Transposition */
2676: for(jj=1; jj<= nlstate+ndeath; jj++){
2677: for(ii=jj; ii<= nlstate+ndeath; ii++){
2678: s1=ps[ii][jj];
2679: ps[ii][jj]=ps[jj][ii];
2680: ps[jj][ii]=s1;
2681: }
2682: }
2683: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
2684: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
2685: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
2686: /* } */
2687: /* printf("\n "); */
2688: /* } */
2689: /* printf("\n ");printf("%lf ",cov[2]);*/
2690: /*
2691: for(i=1; i<= npar; i++) printf("%f ",x[i]);
2692: goto end;*/
2693: return ps;
1.217 brouard 2694: }
2695:
2696:
1.126 brouard 2697: /**************** Product of 2 matrices ******************/
2698:
1.145 brouard 2699: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2700: {
2701: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2702: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2703: /* in, b, out are matrice of pointers which should have been initialized
2704: before: only the contents of out is modified. The function returns
2705: a pointer to pointers identical to out */
1.145 brouard 2706: int i, j, k;
1.126 brouard 2707: for(i=nrl; i<= nrh; i++)
1.145 brouard 2708: for(k=ncolol; k<=ncoloh; k++){
2709: out[i][k]=0.;
2710: for(j=ncl; j<=nch; j++)
2711: out[i][k] +=in[i][j]*b[j][k];
2712: }
1.126 brouard 2713: return out;
2714: }
2715:
2716:
2717: /************* Higher Matrix Product ***************/
2718:
2719: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
2720: {
1.218 brouard 2721: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2722: 'nhstepm*hstepm*stepm' months (i.e. until
2723: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2724: nhstepm*hstepm matrices.
2725: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2726: (typically every 2 years instead of every month which is too big
2727: for the memory).
2728: Model is determined by parameters x and covariates have to be
2729: included manually here.
2730:
2731: */
2732:
2733: int i, j, d, h, k;
1.131 brouard 2734: double **out, cov[NCOVMAX+1];
1.126 brouard 2735: double **newm;
1.187 brouard 2736: double agexact;
1.214 brouard 2737: double agebegin, ageend;
1.126 brouard 2738:
2739: /* Hstepm could be zero and should return the unit matrix */
2740: for (i=1;i<=nlstate+ndeath;i++)
2741: for (j=1;j<=nlstate+ndeath;j++){
2742: oldm[i][j]=(i==j ? 1.0 : 0.0);
2743: po[i][j][0]=(i==j ? 1.0 : 0.0);
2744: }
2745: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2746: for(h=1; h <=nhstepm; h++){
2747: for(d=1; d <=hstepm; d++){
2748: newm=savm;
2749: /* Covariates have to be included here again */
2750: cov[1]=1.;
1.214 brouard 2751: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 2752: cov[2]=agexact;
2753: if(nagesqr==1)
1.218 brouard 2754: cov[3]= agexact*agexact;
1.131 brouard 2755: for (k=1; k<=cptcovn;k++)
1.218 brouard 2756: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2757: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.186 brouard 2758: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.218 brouard 2759: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2760: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2761: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.145 brouard 2762: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.218 brouard 2763: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2764: /* 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 2765:
2766:
2767: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2768: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2769: /* right multiplication of oldm by the current matrix */
1.126 brouard 2770: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
2771: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 2772: /* if((int)age == 70){ */
2773: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2774: /* for(i=1; i<=nlstate+ndeath; i++) { */
2775: /* printf("%d pmmij ",i); */
2776: /* for(j=1;j<=nlstate+ndeath;j++) { */
2777: /* printf("%f ",pmmij[i][j]); */
2778: /* } */
2779: /* printf(" oldm "); */
2780: /* for(j=1;j<=nlstate+ndeath;j++) { */
2781: /* printf("%f ",oldm[i][j]); */
2782: /* } */
2783: /* printf("\n"); */
2784: /* } */
2785: /* } */
1.126 brouard 2786: savm=oldm;
2787: oldm=newm;
2788: }
2789: for(i=1; i<=nlstate+ndeath; i++)
2790: for(j=1;j<=nlstate+ndeath;j++) {
1.218 brouard 2791: po[i][j][h]=newm[i][j];
2792: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 2793: }
1.128 brouard 2794: /*printf("h=%d ",h);*/
1.126 brouard 2795: } /* end h */
1.218 brouard 2796: /* printf("\n H=%d \n",h); */
1.126 brouard 2797: return po;
2798: }
2799:
1.217 brouard 2800: /************* Higher Back Matrix Product ***************/
1.218 brouard 2801: /* 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 2802: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 2803: {
1.218 brouard 2804: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 2805: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 brouard 2806: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2807: nhstepm*hstepm matrices.
2808: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2809: (typically every 2 years instead of every month which is too big
1.217 brouard 2810: for the memory).
1.218 brouard 2811: Model is determined by parameters x and covariates have to be
2812: included manually here.
1.217 brouard 2813:
1.222 brouard 2814: */
1.217 brouard 2815:
2816: int i, j, d, h, k;
2817: double **out, cov[NCOVMAX+1];
2818: double **newm;
2819: double agexact;
2820: double agebegin, ageend;
1.222 brouard 2821: double **oldm, **savm;
1.217 brouard 2822:
1.222 brouard 2823: oldm=oldms;savm=savms;
1.217 brouard 2824: /* Hstepm could be zero and should return the unit matrix */
2825: for (i=1;i<=nlstate+ndeath;i++)
2826: for (j=1;j<=nlstate+ndeath;j++){
2827: oldm[i][j]=(i==j ? 1.0 : 0.0);
2828: po[i][j][0]=(i==j ? 1.0 : 0.0);
2829: }
2830: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2831: for(h=1; h <=nhstepm; h++){
2832: for(d=1; d <=hstepm; d++){
2833: newm=savm;
2834: /* Covariates have to be included here again */
2835: cov[1]=1.;
2836: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
2837: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
2838: cov[2]=agexact;
2839: if(nagesqr==1)
1.222 brouard 2840: cov[3]= agexact*agexact;
1.218 brouard 2841: for (k=1; k<=cptcovn;k++)
1.222 brouard 2842: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2843: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 2844: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.222 brouard 2845: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2846: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
2847: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 2848: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.222 brouard 2849: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
2850: /* 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 2851:
2852:
1.217 brouard 2853: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2854: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 brouard 2855: /* Careful transposed matrix */
1.222 brouard 2856: /* age is in cov[2] */
1.218 brouard 2857: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
1.222 brouard 2858: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
1.218 brouard 2859: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
1.222 brouard 2860: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 2861: /* if((int)age == 70){ */
2862: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2863: /* for(i=1; i<=nlstate+ndeath; i++) { */
2864: /* printf("%d pmmij ",i); */
2865: /* for(j=1;j<=nlstate+ndeath;j++) { */
2866: /* printf("%f ",pmmij[i][j]); */
2867: /* } */
2868: /* printf(" oldm "); */
2869: /* for(j=1;j<=nlstate+ndeath;j++) { */
2870: /* printf("%f ",oldm[i][j]); */
2871: /* } */
2872: /* printf("\n"); */
2873: /* } */
2874: /* } */
2875: savm=oldm;
2876: oldm=newm;
2877: }
2878: for(i=1; i<=nlstate+ndeath; i++)
2879: for(j=1;j<=nlstate+ndeath;j++) {
1.222 brouard 2880: po[i][j][h]=newm[i][j];
2881: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 2882: }
2883: /*printf("h=%d ",h);*/
2884: } /* end h */
1.222 brouard 2885: /* printf("\n H=%d \n",h); */
1.217 brouard 2886: return po;
2887: }
2888:
2889:
1.162 brouard 2890: #ifdef NLOPT
2891: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
2892: double fret;
2893: double *xt;
2894: int j;
2895: myfunc_data *d2 = (myfunc_data *) pd;
2896: /* xt = (p1-1); */
2897: xt=vector(1,n);
2898: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
2899:
2900: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
2901: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
2902: printf("Function = %.12lf ",fret);
2903: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
2904: printf("\n");
2905: free_vector(xt,1,n);
2906: return fret;
2907: }
2908: #endif
1.126 brouard 2909:
2910: /*************** log-likelihood *************/
2911: double func( double *x)
2912: {
1.226 ! brouard 2913: int i, ii, j, k, mi, d, kk;
! 2914: int ioffset=0;
! 2915: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
! 2916: double **out;
! 2917: double sw; /* Sum of weights */
! 2918: double lli; /* Individual log likelihood */
! 2919: int s1, s2;
! 2920: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate, quatitative time varying covariate */
! 2921: double bbh, survp;
! 2922: long ipmx;
! 2923: double agexact;
! 2924: /*extern weight */
! 2925: /* We are differentiating ll according to initial status */
! 2926: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
! 2927: /*for(i=1;i<imx;i++)
! 2928: printf(" %d\n",s[4][i]);
! 2929: */
1.162 brouard 2930:
1.226 ! brouard 2931: ++countcallfunc;
1.162 brouard 2932:
1.226 ! brouard 2933: cov[1]=1.;
1.126 brouard 2934:
1.226 ! brouard 2935: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 2936: ioffset=0;
1.226 ! brouard 2937: if(mle==1){
! 2938: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 2939: /* Computes the values of the ncovmodel covariates of the model
! 2940: depending if the covariates are fixed or varying (age dependent) and stores them in cov[]
! 2941: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
! 2942: to be observed in j being in i according to the model.
! 2943: */
! 2944: ioffset=2+nagesqr+cptcovage;
! 2945: /* for (k=1; k<=cptcovn;k++){ /\* Simple and product covariates without age* products *\/ */
! 2946: for (k=1; k<=ncoveff;k++){ /* Simple and product covariates without age* products */
! 2947: cov[++ioffset]=covar[Tvar[k]][i];
! 2948: }
! 2949: for(iqv=1; iqv <= nqfveff; iqv++){ /* Quantitatives and Fixed covariates */
! 2950: cov[++ioffset]=coqvar[iqv][i];
! 2951: }
! 2952:
! 2953: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
! 2954: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
! 2955: has been calculated etc */
! 2956: /* For an individual i, wav[i] gives the number of effective waves */
! 2957: /* We compute the contribution to Likelihood of each effective transition
! 2958: mw[mi][i] is real wave of the mi th effectve wave */
! 2959: /* Then statuses are computed at each begin and end of an effective wave s1=s[ mw[mi][i] ][i];
! 2960: s2=s[mw[mi+1][i]][i];
! 2961: And the iv th varying covariate is the cotvar[mw[mi+1][i]][iv][i]
! 2962: But if the variable is not in the model TTvar[iv] is the real variable effective in the model:
! 2963: meaning that decodemodel should be used cotvar[mw[mi+1][i]][TTvar[iv]][i]
! 2964: */
! 2965: for(mi=1; mi<= wav[i]-1; mi++){
! 2966: for(itv=1; itv <= ntveff; itv++){ /* Varying dummy covariates */
! 2967: cov[ioffset+itv]=cotvar[mw[mi][i]][itv][i];
! 2968: }
! 2969: for(iqtv=1; iqtv <= nqtveff; iqtv++){ /* Varying quantitatives covariates */
! 2970: if(cotqvar[mw[mi][i]][iqtv][i] == -1){
! 2971: printf("i=%d, mi=%d, iqtv=%d, cotqvar[mw[mi][i]][iqtv][i]=%f",i,mi,iqtv,cotqvar[mw[mi][i]][iqtv][i]);
! 2972: }
! 2973: cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][iqtv][i];
! 2974: }
! 2975: /* ioffset=2+nagesqr+cptcovn+nqv+ntv+nqtv; */
! 2976: for (ii=1;ii<=nlstate+ndeath;ii++)
! 2977: for (j=1;j<=nlstate+ndeath;j++){
! 2978: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 2979: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 2980: }
! 2981: for(d=0; d<dh[mi][i]; d++){
! 2982: newm=savm;
! 2983: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 2984: cov[2]=agexact;
! 2985: if(nagesqr==1)
! 2986: cov[3]= agexact*agexact; /* Should be changed here */
! 2987: for (kk=1; kk<=cptcovage;kk++) {
! 2988: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
! 2989: }
! 2990: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 2991: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 2992: savm=oldm;
! 2993: oldm=newm;
! 2994: } /* end mult */
1.224 brouard 2995:
1.226 ! brouard 2996: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
! 2997: /* But now since version 0.9 we anticipate for bias at large stepm.
! 2998: * If stepm is larger than one month (smallest stepm) and if the exact delay
! 2999: * (in months) between two waves is not a multiple of stepm, we rounded to
! 3000: * the nearest (and in case of equal distance, to the lowest) interval but now
! 3001: * we keep into memory the bias bh[mi][i] and also the previous matrix product
! 3002: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
! 3003: * probability in order to take into account the bias as a fraction of the way
! 3004: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
! 3005: * -stepm/2 to stepm/2 .
! 3006: * For stepm=1 the results are the same as for previous versions of Imach.
! 3007: * For stepm > 1 the results are less biased than in previous versions.
! 3008: */
! 3009: s1=s[mw[mi][i]][i];
! 3010: s2=s[mw[mi+1][i]][i];
! 3011: bbh=(double)bh[mi][i]/(double)stepm;
! 3012: /* bias bh is positive if real duration
! 3013: * is higher than the multiple of stepm and negative otherwise.
! 3014: */
! 3015: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
! 3016: if( s2 > nlstate){
! 3017: /* i.e. if s2 is a death state and if the date of death is known
! 3018: then the contribution to the likelihood is the probability to
! 3019: die between last step unit time and current step unit time,
! 3020: which is also equal to probability to die before dh
! 3021: minus probability to die before dh-stepm .
! 3022: In version up to 0.92 likelihood was computed
! 3023: as if date of death was unknown. Death was treated as any other
! 3024: health state: the date of the interview describes the actual state
! 3025: and not the date of a change in health state. The former idea was
! 3026: to consider that at each interview the state was recorded
! 3027: (healthy, disable or death) and IMaCh was corrected; but when we
! 3028: introduced the exact date of death then we should have modified
! 3029: the contribution of an exact death to the likelihood. This new
! 3030: contribution is smaller and very dependent of the step unit
! 3031: stepm. It is no more the probability to die between last interview
! 3032: and month of death but the probability to survive from last
! 3033: interview up to one month before death multiplied by the
! 3034: probability to die within a month. Thanks to Chris
! 3035: Jackson for correcting this bug. Former versions increased
! 3036: mortality artificially. The bad side is that we add another loop
! 3037: which slows down the processing. The difference can be up to 10%
! 3038: lower mortality.
! 3039: */
! 3040: /* If, at the beginning of the maximization mostly, the
! 3041: cumulative probability or probability to be dead is
! 3042: constant (ie = 1) over time d, the difference is equal to
! 3043: 0. out[s1][3] = savm[s1][3]: probability, being at state
! 3044: s1 at precedent wave, to be dead a month before current
! 3045: wave is equal to probability, being at state s1 at
! 3046: precedent wave, to be dead at mont of the current
! 3047: wave. Then the observed probability (that this person died)
! 3048: is null according to current estimated parameter. In fact,
! 3049: it should be very low but not zero otherwise the log go to
! 3050: infinity.
! 3051: */
1.183 brouard 3052: /* #ifdef INFINITYORIGINAL */
3053: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3054: /* #else */
3055: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
3056: /* lli=log(mytinydouble); */
3057: /* else */
3058: /* lli=log(out[s1][s2] - savm[s1][s2]); */
3059: /* #endif */
1.226 ! brouard 3060: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3061:
1.226 ! brouard 3062: } else if ( s2==-1 ) { /* alive */
! 3063: for (j=1,survp=0. ; j<=nlstate; j++)
! 3064: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 3065: /*survp += out[s1][j]; */
! 3066: lli= log(survp);
! 3067: }
! 3068: else if (s2==-4) {
! 3069: for (j=3,survp=0. ; j<=nlstate; j++)
! 3070: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 3071: lli= log(survp);
! 3072: }
! 3073: else if (s2==-5) {
! 3074: for (j=1,survp=0. ; j<=2; j++)
! 3075: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
! 3076: lli= log(survp);
! 3077: }
! 3078: else{
! 3079: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
! 3080: /* 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 */
! 3081: }
! 3082: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
! 3083: /*if(lli ==000.0)*/
! 3084: /*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); */
! 3085: ipmx +=1;
! 3086: sw += weight[i];
! 3087: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 3088: /* if (lli < log(mytinydouble)){ */
! 3089: /* 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); */
! 3090: /* 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]); */
! 3091: /* } */
! 3092: } /* end of wave */
! 3093: } /* end of individual */
! 3094: } else if(mle==2){
! 3095: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 3096: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
! 3097: for(mi=1; mi<= wav[i]-1; mi++){
! 3098: for (ii=1;ii<=nlstate+ndeath;ii++)
! 3099: for (j=1;j<=nlstate+ndeath;j++){
! 3100: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3101: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3102: }
! 3103: for(d=0; d<=dh[mi][i]; d++){
! 3104: newm=savm;
! 3105: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3106: cov[2]=agexact;
! 3107: if(nagesqr==1)
! 3108: cov[3]= agexact*agexact;
! 3109: for (kk=1; kk<=cptcovage;kk++) {
! 3110: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
! 3111: }
! 3112: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3113: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3114: savm=oldm;
! 3115: oldm=newm;
! 3116: } /* end mult */
! 3117:
! 3118: s1=s[mw[mi][i]][i];
! 3119: s2=s[mw[mi+1][i]][i];
! 3120: bbh=(double)bh[mi][i]/(double)stepm;
! 3121: 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 */
! 3122: ipmx +=1;
! 3123: sw += weight[i];
! 3124: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 3125: } /* end of wave */
! 3126: } /* end of individual */
! 3127: } else if(mle==3){ /* exponential inter-extrapolation */
! 3128: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 3129: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
! 3130: for(mi=1; mi<= wav[i]-1; mi++){
! 3131: for (ii=1;ii<=nlstate+ndeath;ii++)
! 3132: for (j=1;j<=nlstate+ndeath;j++){
! 3133: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3134: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3135: }
! 3136: for(d=0; d<dh[mi][i]; d++){
! 3137: newm=savm;
! 3138: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3139: cov[2]=agexact;
! 3140: if(nagesqr==1)
! 3141: cov[3]= agexact*agexact;
! 3142: for (kk=1; kk<=cptcovage;kk++) {
! 3143: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
! 3144: }
! 3145: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3146: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3147: savm=oldm;
! 3148: oldm=newm;
! 3149: } /* end mult */
! 3150:
! 3151: s1=s[mw[mi][i]][i];
! 3152: s2=s[mw[mi+1][i]][i];
! 3153: bbh=(double)bh[mi][i]/(double)stepm;
! 3154: 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 */
! 3155: ipmx +=1;
! 3156: sw += weight[i];
! 3157: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 3158: } /* end of wave */
! 3159: } /* end of individual */
! 3160: }else if (mle==4){ /* ml=4 no inter-extrapolation */
! 3161: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 3162: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
! 3163: for(mi=1; mi<= wav[i]-1; mi++){
! 3164: for (ii=1;ii<=nlstate+ndeath;ii++)
! 3165: for (j=1;j<=nlstate+ndeath;j++){
! 3166: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3167: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3168: }
! 3169: for(d=0; d<dh[mi][i]; d++){
! 3170: newm=savm;
! 3171: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3172: cov[2]=agexact;
! 3173: if(nagesqr==1)
! 3174: cov[3]= agexact*agexact;
! 3175: for (kk=1; kk<=cptcovage;kk++) {
! 3176: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
! 3177: }
1.126 brouard 3178:
1.226 ! brouard 3179: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3180: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3181: savm=oldm;
! 3182: oldm=newm;
! 3183: } /* end mult */
! 3184:
! 3185: s1=s[mw[mi][i]][i];
! 3186: s2=s[mw[mi+1][i]][i];
! 3187: if( s2 > nlstate){
! 3188: lli=log(out[s1][s2] - savm[s1][s2]);
! 3189: } else if ( s2==-1 ) { /* alive */
! 3190: for (j=1,survp=0. ; j<=nlstate; j++)
! 3191: survp += out[s1][j];
! 3192: lli= log(survp);
! 3193: }else{
! 3194: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
! 3195: }
! 3196: ipmx +=1;
! 3197: sw += weight[i];
! 3198: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.126 brouard 3199: /* printf("i=%6d s1=%1d s2=%1d mi=%1d mw=%1d dh=%3d prob=%10.6f w=%6.4f out=%10.6f sav=%10.6f\n",i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],out[s1][s2],savm[s1][s2]); */
1.226 ! brouard 3200: } /* end of wave */
! 3201: } /* end of individual */
! 3202: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
! 3203: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
! 3204: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
! 3205: for(mi=1; mi<= wav[i]-1; mi++){
! 3206: for (ii=1;ii<=nlstate+ndeath;ii++)
! 3207: for (j=1;j<=nlstate+ndeath;j++){
! 3208: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3209: savm[ii][j]=(ii==j ? 1.0 : 0.0);
! 3210: }
! 3211: for(d=0; d<dh[mi][i]; d++){
! 3212: newm=savm;
! 3213: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
! 3214: cov[2]=agexact;
! 3215: if(nagesqr==1)
! 3216: cov[3]= agexact*agexact;
! 3217: for (kk=1; kk<=cptcovage;kk++) {
! 3218: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
! 3219: }
1.126 brouard 3220:
1.226 ! brouard 3221: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
! 3222: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
! 3223: savm=oldm;
! 3224: oldm=newm;
! 3225: } /* end mult */
! 3226:
! 3227: s1=s[mw[mi][i]][i];
! 3228: s2=s[mw[mi+1][i]][i];
! 3229: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
! 3230: ipmx +=1;
! 3231: sw += weight[i];
! 3232: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
! 3233: /*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]);*/
! 3234: } /* end of wave */
! 3235: } /* end of individual */
! 3236: } /* End of if */
! 3237: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
! 3238: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
! 3239: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
! 3240: return -l;
1.126 brouard 3241: }
3242:
3243: /*************** log-likelihood *************/
3244: double funcone( double *x)
3245: {
3246: /* Same as likeli but slower because of a lot of printf and if */
3247: int i, ii, j, k, mi, d, kk;
1.224 brouard 3248: int ioffset=0;
1.131 brouard 3249: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3250: double **out;
3251: double lli; /* Individual log likelihood */
3252: double llt;
3253: int s1, s2;
1.224 brouard 3254: int iv=0, iqv=0, itv=0, iqtv=0 ; /* Index of varying covariate, fixed quantitative cov, time varying covariate */
1.126 brouard 3255: double bbh, survp;
1.187 brouard 3256: double agexact;
1.214 brouard 3257: double agebegin, ageend;
1.126 brouard 3258: /*extern weight */
3259: /* We are differentiating ll according to initial status */
3260: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3261: /*for(i=1;i<imx;i++)
3262: printf(" %d\n",s[4][i]);
3263: */
3264: cov[1]=1.;
3265:
3266: for(k=1; k<=nlstate; k++) ll[k]=0.;
1.224 brouard 3267: ioffset=0;
3268: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.225 brouard 3269: ioffset=2+nagesqr+cptcovage;
1.224 brouard 3270: /* for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i]; */
1.226 ! brouard 3271: for (k=1; k<=ncoveff+nqfveff;k++){ /* Simple and product fixed covariates without age* products */
1.225 brouard 3272: cov[++ioffset]=covar[Tvar[k]][i];
3273: }
1.226 ! brouard 3274: for(iqv=1; iqv <= nqfveff; iqv++){ /* Quantitative fixed covariates */
! 3275: cov[++ioffset]=coqvar[Tvar[iqv]][i];
1.225 brouard 3276: }
3277:
1.226 ! brouard 3278: for(mi=1; mi<= wav[i]-1; mi++){ /* Varying with waves */
1.225 brouard 3279: for(itv=1; itv <= ntveff; itv++){ /* Varying dummy covariates */
3280: cov[ioffset+itv]=cotvar[mw[mi][i]][itv][i];
3281: }
3282: for(iqtv=1; iqtv <= nqtveff; iqtv++){ /* Varying quantitatives covariates */
3283: cov[ioffset+ntveff+iqtv]=cotqvar[mw[mi][i]][iqtv][i];
3284: }
1.126 brouard 3285: for (ii=1;ii<=nlstate+ndeath;ii++)
1.225 brouard 3286: for (j=1;j<=nlstate+ndeath;j++){
3287: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3288: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3289: }
1.214 brouard 3290:
3291: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3292: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3293: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
1.225 brouard 3294: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3295: and mw[mi+1][i]. dh depends on stepm.*/
3296: newm=savm;
3297: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3298: cov[2]=agexact;
3299: if(nagesqr==1)
3300: cov[3]= agexact*agexact;
3301: for (kk=1; kk<=cptcovage;kk++) {
3302: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
3303: }
3304: /* printf("i=%d,mi=%d,d=%d,mw[mi][i]=%d\n",i, mi,d,mw[mi][i]); */
3305: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
3306: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3307: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3308: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3309: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
3310: savm=oldm;
3311: oldm=newm;
1.126 brouard 3312: } /* end mult */
3313:
3314: s1=s[mw[mi][i]][i];
3315: s2=s[mw[mi+1][i]][i];
1.217 brouard 3316: /* if(s2==-1){ */
3317: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3318: /* /\* exit(1); *\/ */
3319: /* } */
1.126 brouard 3320: bbh=(double)bh[mi][i]/(double)stepm;
3321: /* bias is positive if real duration
3322: * is higher than the multiple of stepm and negative otherwise.
3323: */
3324: if( s2 > nlstate && (mle <5) ){ /* Jackson */
1.225 brouard 3325: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3326: } else if ( s2==-1 ) { /* alive */
1.225 brouard 3327: for (j=1,survp=0. ; j<=nlstate; j++)
3328: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3329: lli= log(survp);
1.126 brouard 3330: }else if (mle==1){
1.225 brouard 3331: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
1.126 brouard 3332: } else if(mle==2){
1.225 brouard 3333: 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 3334: } else if(mle==3){ /* exponential inter-extrapolation */
1.225 brouard 3335: 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 3336: } else if (mle==4){ /* mle=4 no inter-extrapolation */
1.225 brouard 3337: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3338: } else{ /* mle=0 back to 1 */
1.225 brouard 3339: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3340: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3341: } /* End of if */
3342: ipmx +=1;
3343: sw += weight[i];
3344: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3345: /*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 3346: if(globpr){
1.225 brouard 3347: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %15.6f %8.4f %8.3f\
1.126 brouard 3348: %11.6f %11.6f %11.6f ", \
1.225 brouard 3349: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
3350: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
3351: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3352: llt +=ll[k]*gipmx/gsw;
3353: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3354: }
3355: fprintf(ficresilk," %10.6f\n", -llt);
1.126 brouard 3356: }
3357: } /* end of wave */
3358: } /* end of individual */
3359: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3360: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3361: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3362: if(globpr==0){ /* First time we count the contributions and weights */
3363: gipmx=ipmx;
3364: gsw=sw;
3365: }
3366: return -l;
3367: }
3368:
3369:
3370: /*************** function likelione ***********/
3371: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3372: {
3373: /* This routine should help understanding what is done with
3374: the selection of individuals/waves and
3375: to check the exact contribution to the likelihood.
3376: Plotting could be done.
3377: */
3378: int k;
3379:
3380: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3381: strcpy(fileresilk,"ILK_");
1.202 brouard 3382: strcat(fileresilk,fileresu);
1.126 brouard 3383: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3384: printf("Problem with resultfile: %s\n", fileresilk);
3385: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3386: }
1.214 brouard 3387: 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");
3388: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3389: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3390: for(k=1; k<=nlstate; k++)
3391: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3392: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3393: }
3394:
3395: *fretone=(*funcone)(p);
3396: if(*globpri !=0){
3397: fclose(ficresilk);
1.205 brouard 3398: if (mle ==0)
3399: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3400: else if(mle >=1)
3401: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3402: 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 3403:
1.208 brouard 3404:
3405: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3406: 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 3407: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3408: }
1.207 brouard 3409: 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 3410: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3411: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3412: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3413: fflush(fichtm);
1.205 brouard 3414: }
1.126 brouard 3415: return;
3416: }
3417:
3418:
3419: /*********** Maximum Likelihood Estimation ***************/
3420:
3421: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3422: {
1.165 brouard 3423: int i,j, iter=0;
1.126 brouard 3424: double **xi;
3425: double fret;
3426: double fretone; /* Only one call to likelihood */
3427: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3428:
3429: #ifdef NLOPT
3430: int creturn;
3431: nlopt_opt opt;
3432: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3433: double *lb;
3434: double minf; /* the minimum objective value, upon return */
3435: double * p1; /* Shifted parameters from 0 instead of 1 */
3436: myfunc_data dinst, *d = &dinst;
3437: #endif
3438:
3439:
1.126 brouard 3440: xi=matrix(1,npar,1,npar);
3441: for (i=1;i<=npar;i++)
3442: for (j=1;j<=npar;j++)
3443: xi[i][j]=(i==j ? 1.0 : 0.0);
3444: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3445: strcpy(filerespow,"POW_");
1.126 brouard 3446: strcat(filerespow,fileres);
3447: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3448: printf("Problem with resultfile: %s\n", filerespow);
3449: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3450: }
3451: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3452: for (i=1;i<=nlstate;i++)
3453: for(j=1;j<=nlstate+ndeath;j++)
3454: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3455: fprintf(ficrespow,"\n");
1.162 brouard 3456: #ifdef POWELL
1.126 brouard 3457: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3458: #endif
1.126 brouard 3459:
1.162 brouard 3460: #ifdef NLOPT
3461: #ifdef NEWUOA
3462: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3463: #else
3464: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3465: #endif
3466: lb=vector(0,npar-1);
3467: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3468: nlopt_set_lower_bounds(opt, lb);
3469: nlopt_set_initial_step1(opt, 0.1);
3470:
3471: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3472: d->function = func;
3473: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3474: nlopt_set_min_objective(opt, myfunc, d);
3475: nlopt_set_xtol_rel(opt, ftol);
3476: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3477: printf("nlopt failed! %d\n",creturn);
3478: }
3479: else {
3480: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3481: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3482: iter=1; /* not equal */
3483: }
3484: nlopt_destroy(opt);
3485: #endif
1.126 brouard 3486: free_matrix(xi,1,npar,1,npar);
3487: fclose(ficrespow);
1.203 brouard 3488: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3489: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3490: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3491:
3492: }
3493:
3494: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3495: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3496: {
3497: double **a,**y,*x,pd;
1.203 brouard 3498: /* double **hess; */
1.164 brouard 3499: int i, j;
1.126 brouard 3500: int *indx;
3501:
3502: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3503: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3504: void lubksb(double **a, int npar, int *indx, double b[]) ;
3505: void ludcmp(double **a, int npar, int *indx, double *d) ;
3506: double gompertz(double p[]);
1.203 brouard 3507: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3508:
3509: printf("\nCalculation of the hessian matrix. Wait...\n");
3510: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3511: for (i=1;i<=npar;i++){
1.203 brouard 3512: printf("%d-",i);fflush(stdout);
3513: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3514:
3515: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3516:
3517: /* printf(" %f ",p[i]);
3518: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3519: }
3520:
3521: for (i=1;i<=npar;i++) {
3522: for (j=1;j<=npar;j++) {
3523: if (j>i) {
1.203 brouard 3524: printf(".%d-%d",i,j);fflush(stdout);
3525: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3526: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3527:
3528: hess[j][i]=hess[i][j];
3529: /*printf(" %lf ",hess[i][j]);*/
3530: }
3531: }
3532: }
3533: printf("\n");
3534: fprintf(ficlog,"\n");
3535:
3536: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3537: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3538:
3539: a=matrix(1,npar,1,npar);
3540: y=matrix(1,npar,1,npar);
3541: x=vector(1,npar);
3542: indx=ivector(1,npar);
3543: for (i=1;i<=npar;i++)
3544: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3545: ludcmp(a,npar,indx,&pd);
3546:
3547: for (j=1;j<=npar;j++) {
3548: for (i=1;i<=npar;i++) x[i]=0;
3549: x[j]=1;
3550: lubksb(a,npar,indx,x);
3551: for (i=1;i<=npar;i++){
3552: matcov[i][j]=x[i];
3553: }
3554: }
3555:
3556: printf("\n#Hessian matrix#\n");
3557: fprintf(ficlog,"\n#Hessian matrix#\n");
3558: for (i=1;i<=npar;i++) {
3559: for (j=1;j<=npar;j++) {
1.203 brouard 3560: printf("%.6e ",hess[i][j]);
3561: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3562: }
3563: printf("\n");
3564: fprintf(ficlog,"\n");
3565: }
3566:
1.203 brouard 3567: /* printf("\n#Covariance matrix#\n"); */
3568: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3569: /* for (i=1;i<=npar;i++) { */
3570: /* for (j=1;j<=npar;j++) { */
3571: /* printf("%.6e ",matcov[i][j]); */
3572: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3573: /* } */
3574: /* printf("\n"); */
3575: /* fprintf(ficlog,"\n"); */
3576: /* } */
3577:
1.126 brouard 3578: /* Recompute Inverse */
1.203 brouard 3579: /* for (i=1;i<=npar;i++) */
3580: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3581: /* ludcmp(a,npar,indx,&pd); */
3582:
3583: /* printf("\n#Hessian matrix recomputed#\n"); */
3584:
3585: /* for (j=1;j<=npar;j++) { */
3586: /* for (i=1;i<=npar;i++) x[i]=0; */
3587: /* x[j]=1; */
3588: /* lubksb(a,npar,indx,x); */
3589: /* for (i=1;i<=npar;i++){ */
3590: /* y[i][j]=x[i]; */
3591: /* printf("%.3e ",y[i][j]); */
3592: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3593: /* } */
3594: /* printf("\n"); */
3595: /* fprintf(ficlog,"\n"); */
3596: /* } */
3597:
3598: /* Verifying the inverse matrix */
3599: #ifdef DEBUGHESS
3600: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3601:
1.203 brouard 3602: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3603: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3604:
3605: for (j=1;j<=npar;j++) {
3606: for (i=1;i<=npar;i++){
1.203 brouard 3607: printf("%.2f ",y[i][j]);
3608: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3609: }
3610: printf("\n");
3611: fprintf(ficlog,"\n");
3612: }
1.203 brouard 3613: #endif
1.126 brouard 3614:
3615: free_matrix(a,1,npar,1,npar);
3616: free_matrix(y,1,npar,1,npar);
3617: free_vector(x,1,npar);
3618: free_ivector(indx,1,npar);
1.203 brouard 3619: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3620:
3621:
3622: }
3623:
3624: /*************** hessian matrix ****************/
3625: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3626: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3627: int i;
3628: int l=1, lmax=20;
1.203 brouard 3629: double k1,k2, res, fx;
1.132 brouard 3630: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3631: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3632: int k=0,kmax=10;
3633: double l1;
3634:
3635: fx=func(x);
3636: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3637: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3638: l1=pow(10,l);
3639: delts=delt;
3640: for(k=1 ; k <kmax; k=k+1){
3641: delt = delta*(l1*k);
3642: p2[theta]=x[theta] +delt;
1.145 brouard 3643: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3644: p2[theta]=x[theta]-delt;
3645: k2=func(p2)-fx;
3646: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3647: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3648:
1.203 brouard 3649: #ifdef DEBUGHESSII
1.126 brouard 3650: 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);
3651: 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);
3652: #endif
3653: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3654: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3655: k=kmax;
3656: }
3657: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3658: k=kmax; l=lmax*10;
1.126 brouard 3659: }
3660: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3661: delts=delt;
3662: }
1.203 brouard 3663: } /* End loop k */
1.126 brouard 3664: }
3665: delti[theta]=delts;
3666: return res;
3667:
3668: }
3669:
1.203 brouard 3670: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3671: {
3672: int i;
1.164 brouard 3673: int l=1, lmax=20;
1.126 brouard 3674: double k1,k2,k3,k4,res,fx;
1.132 brouard 3675: double p2[MAXPARM+1];
1.203 brouard 3676: int k, kmax=1;
3677: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3678:
3679: int firstime=0;
1.203 brouard 3680:
1.126 brouard 3681: fx=func(x);
1.203 brouard 3682: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3683: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3684: p2[thetai]=x[thetai]+delti[thetai]*k;
3685: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3686: k1=func(p2)-fx;
3687:
1.203 brouard 3688: p2[thetai]=x[thetai]+delti[thetai]*k;
3689: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3690: k2=func(p2)-fx;
3691:
1.203 brouard 3692: p2[thetai]=x[thetai]-delti[thetai]*k;
3693: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3694: k3=func(p2)-fx;
3695:
1.203 brouard 3696: p2[thetai]=x[thetai]-delti[thetai]*k;
3697: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3698: k4=func(p2)-fx;
1.203 brouard 3699: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3700: if(k1*k2*k3*k4 <0.){
1.208 brouard 3701: firstime=1;
1.203 brouard 3702: kmax=kmax+10;
1.208 brouard 3703: }
3704: if(kmax >=10 || firstime ==1){
1.218 brouard 3705: 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);
3706: 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 3707: 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);
3708: 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);
3709: }
3710: #ifdef DEBUGHESSIJ
3711: v1=hess[thetai][thetai];
3712: v2=hess[thetaj][thetaj];
3713: cv12=res;
3714: /* Computing eigen value of Hessian matrix */
3715: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3716: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3717: if ((lc2 <0) || (lc1 <0) ){
3718: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3719: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3720: 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);
3721: 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);
3722: }
1.126 brouard 3723: #endif
3724: }
3725: return res;
3726: }
3727:
1.203 brouard 3728: /* Not done yet: Was supposed to fix if not exactly at the maximum */
3729: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
3730: /* { */
3731: /* int i; */
3732: /* int l=1, lmax=20; */
3733: /* double k1,k2,k3,k4,res,fx; */
3734: /* double p2[MAXPARM+1]; */
3735: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
3736: /* int k=0,kmax=10; */
3737: /* double l1; */
3738:
3739: /* fx=func(x); */
3740: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
3741: /* l1=pow(10,l); */
3742: /* delts=delt; */
3743: /* for(k=1 ; k <kmax; k=k+1){ */
3744: /* delt = delti*(l1*k); */
3745: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
3746: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3747: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3748: /* k1=func(p2)-fx; */
3749:
3750: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3751: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3752: /* k2=func(p2)-fx; */
3753:
3754: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3755: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3756: /* k3=func(p2)-fx; */
3757:
3758: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3759: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3760: /* k4=func(p2)-fx; */
3761: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
3762: /* #ifdef DEBUGHESSIJ */
3763: /* 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); */
3764: /* 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); */
3765: /* #endif */
3766: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
3767: /* k=kmax; */
3768: /* } */
3769: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
3770: /* k=kmax; l=lmax*10; */
3771: /* } */
3772: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
3773: /* delts=delt; */
3774: /* } */
3775: /* } /\* End loop k *\/ */
3776: /* } */
3777: /* delti[theta]=delts; */
3778: /* return res; */
3779: /* } */
3780:
3781:
1.126 brouard 3782: /************** Inverse of matrix **************/
3783: void ludcmp(double **a, int n, int *indx, double *d)
3784: {
3785: int i,imax,j,k;
3786: double big,dum,sum,temp;
3787: double *vv;
3788:
3789: vv=vector(1,n);
3790: *d=1.0;
3791: for (i=1;i<=n;i++) {
3792: big=0.0;
3793: for (j=1;j<=n;j++)
3794: if ((temp=fabs(a[i][j])) > big) big=temp;
3795: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
3796: vv[i]=1.0/big;
3797: }
3798: for (j=1;j<=n;j++) {
3799: for (i=1;i<j;i++) {
3800: sum=a[i][j];
3801: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
3802: a[i][j]=sum;
3803: }
3804: big=0.0;
3805: for (i=j;i<=n;i++) {
3806: sum=a[i][j];
3807: for (k=1;k<j;k++)
3808: sum -= a[i][k]*a[k][j];
3809: a[i][j]=sum;
3810: if ( (dum=vv[i]*fabs(sum)) >= big) {
3811: big=dum;
3812: imax=i;
3813: }
3814: }
3815: if (j != imax) {
3816: for (k=1;k<=n;k++) {
3817: dum=a[imax][k];
3818: a[imax][k]=a[j][k];
3819: a[j][k]=dum;
3820: }
3821: *d = -(*d);
3822: vv[imax]=vv[j];
3823: }
3824: indx[j]=imax;
3825: if (a[j][j] == 0.0) a[j][j]=TINY;
3826: if (j != n) {
3827: dum=1.0/(a[j][j]);
3828: for (i=j+1;i<=n;i++) a[i][j] *= dum;
3829: }
3830: }
3831: free_vector(vv,1,n); /* Doesn't work */
3832: ;
3833: }
3834:
3835: void lubksb(double **a, int n, int *indx, double b[])
3836: {
3837: int i,ii=0,ip,j;
3838: double sum;
3839:
3840: for (i=1;i<=n;i++) {
3841: ip=indx[i];
3842: sum=b[ip];
3843: b[ip]=b[i];
3844: if (ii)
3845: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
3846: else if (sum) ii=i;
3847: b[i]=sum;
3848: }
3849: for (i=n;i>=1;i--) {
3850: sum=b[i];
3851: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
3852: b[i]=sum/a[i][i];
3853: }
3854: }
3855:
3856: void pstamp(FILE *fichier)
3857: {
1.196 brouard 3858: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 3859: }
3860:
3861: /************ Frequencies ********************/
1.226 ! brouard 3862: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
! 3863: int *Tvaraff, int *invalidvarcomb, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[], \
! 3864: int firstpass, int lastpass, int stepm, int weightopt, char model[])
! 3865: { /* Some frequencies */
! 3866:
! 3867: int i, m, jk, j1, bool, z1,j;
! 3868: int iind=0, iage=0;
! 3869: int mi; /* Effective wave */
! 3870: int first;
! 3871: double ***freq; /* Frequencies */
! 3872: double *meanq;
! 3873: double **meanqt;
! 3874: double *pp, **prop, *posprop, *pospropt;
! 3875: double pos=0., posproptt=0., pospropta=0., k2, dateintsum=0,k2cpt=0;
! 3876: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
! 3877: double agebegin, ageend;
! 3878:
! 3879: pp=vector(1,nlstate);
! 3880: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
! 3881: posprop=vector(1,nlstate); /* Counting the number of transition starting from a live state per age */
! 3882: pospropt=vector(1,nlstate); /* Counting the number of transition starting from a live state */
! 3883: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
! 3884: meanq=vector(1,nqfveff); /* Number of Quantitative Fixed Variables Effective */
! 3885: meanqt=matrix(1,lastpass,1,nqtveff);
! 3886: strcpy(fileresp,"P_");
! 3887: strcat(fileresp,fileresu);
! 3888: /*strcat(fileresphtm,fileresu);*/
! 3889: if((ficresp=fopen(fileresp,"w"))==NULL) {
! 3890: printf("Problem with prevalence resultfile: %s\n", fileresp);
! 3891: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
! 3892: exit(0);
! 3893: }
1.214 brouard 3894:
1.226 ! brouard 3895: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
! 3896: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
! 3897: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
! 3898: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
! 3899: fflush(ficlog);
! 3900: exit(70);
! 3901: }
! 3902: else{
! 3903: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
1.214 brouard 3904: <hr size=\"2\" color=\"#EC5E5E\"> \n\
3905: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 ! brouard 3906: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
! 3907: }
! 3908: 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 3909:
1.226 ! brouard 3910: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
! 3911: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
! 3912: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
! 3913: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
! 3914: fflush(ficlog);
! 3915: exit(70);
! 3916: }
! 3917: else{
! 3918: 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 3919: <hr size=\"2\" color=\"#EC5E5E\"> \n\
3920: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.226 ! brouard 3921: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
! 3922: }
! 3923: 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 3924:
1.226 ! brouard 3925: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
! 3926: j1=0;
1.126 brouard 3927:
1.226 ! brouard 3928: j=ncoveff;
! 3929: if (cptcovn<1) {j=1;ncodemax[1]=1;}
1.220 brouard 3930:
1.226 ! brouard 3931: first=1;
1.220 brouard 3932:
1.226 ! brouard 3933: /* Detects if a combination j1 is empty: for a multinomial variable like 3 education levels:
! 3934: reference=low_education V1=0,V2=0
! 3935: med_educ V1=1 V2=0,
! 3936: high_educ V1=0 V2=1
! 3937: Then V1=1 and V2=1 is a noisy combination that we want to exclude for the list 2**cptcoveff
! 3938: */
1.126 brouard 3939:
1.226 ! brouard 3940: for (j1 = 1; j1 <= (int) pow(2,j); j1++){ /* Loop on covariates combination excluding varying and quantitatives */
! 3941: posproptt=0.;
! 3942: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
! 3943: scanf("%d", i);*/
! 3944: for (i=-5; i<=nlstate+ndeath; i++)
! 3945: for (jk=-5; jk<=nlstate+ndeath; jk++)
! 3946: for(m=iagemin; m <= iagemax+3; m++)
! 3947: freq[i][jk][m]=0;
! 3948:
! 3949: for (i=1; i<=nlstate; i++) {
! 3950: for(m=iagemin; m <= iagemax+3; m++)
! 3951: prop[i][m]=0;
! 3952: posprop[i]=0;
! 3953: pospropt[i]=0;
! 3954: }
! 3955: for (z1=1; z1<= nqfveff; z1++) {
! 3956: meanq[z1]+=0.;
! 3957: for(m=1;m<=lastpass;m++){
! 3958: meanqt[m][z1]=0.;
! 3959: }
! 3960: }
1.220 brouard 3961:
1.226 ! brouard 3962: dateintsum=0;
! 3963: k2cpt=0;
! 3964: /* For that comination of covariate j1, we count and print the frequencies */
! 3965: for (iind=1; iind<=imx; iind++) { /* For each individual iind */
! 3966: bool=1;
! 3967: if (nqfveff >0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
! 3968: for (z1=1; z1<= nqfveff; z1++) {
! 3969: meanq[z1]+=coqvar[Tvar[z1]][iind];
! 3970: }
! 3971: for (z1=1; z1<=ncoveff; z1++) {
! 3972: /* if(Tvaraff[z1] ==-20){ */
! 3973: /* /\* sumnew+=cotvar[mw[mi][iind]][z1][iind]; *\/ */
! 3974: /* }else if(Tvaraff[z1] ==-10){ */
! 3975: /* /\* sumnew+=coqvar[z1][iind]; *\/ */
! 3976: /* }else */
! 3977: if (covar[Tvaraff[z1]][iind]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
! 3978: /* Tests if this individual i responded to j1 (V4=1 V3=0) */
! 3979: bool=0;
! 3980: /* 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",
! 3981: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
! 3982: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
! 3983: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
! 3984: }
! 3985: } /* end z1 */
! 3986: } /* cptcovn > 0 */
! 3987:
! 3988: if (bool==1){ /* We selected an individual iin satisfying combination j1 */
! 3989: /* for(m=firstpass; m<=lastpass; m++){ */
! 3990: for(mi=1; mi<wav[iind];mi++){
! 3991: m=mw[mi][iind];
! 3992: /* dh[m][iind] or dh[mw[mi][iind]][iind] is the delay between two effective (mi) waves m=mw[mi][iind]
! 3993: and mw[mi+1][iind]. dh depends on stepm. */
! 3994: agebegin=agev[m][iind]; /* Age at beginning of wave before transition*/
! 3995: ageend=agev[m][iind]+(dh[m][iind])*stepm/YEARM; /* Age at end of wave and transition */
! 3996: if(m >=firstpass && m <=lastpass){
! 3997: k2=anint[m][iind]+(mint[m][iind]/12.);
! 3998: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
! 3999: if(agev[m][iind]==0) agev[m][iind]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
! 4000: if(agev[m][iind]==1) agev[m][iind]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
! 4001: if (s[m][iind]>0 && s[m][iind]<=nlstate) /* If status at wave m is known and a live state */
! 4002: prop[s[m][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
! 4003: if (m<lastpass) {
! 4004: /* if(s[m][iind]==4 && s[m+1][iind]==4) */
! 4005: /* 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]); */
! 4006: if(s[m][iind]==-1)
! 4007: 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.));
! 4008: freq[s[m][iind]][s[m+1][iind]][(int)agev[m][iind]] += weight[iind]; /* At age of beginning of transition, where status is known */
! 4009: /* freq[s[m][iind]][s[m+1][iind]][(int)((agebegin+ageend)/2.)] += weight[iind]; */
! 4010: 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 */
! 4011: }
! 4012: }
! 4013: if ((agev[m][iind]>1) && (agev[m][iind]< (iagemax+3)) && (anint[m][iind]!=9999) && (mint[m][iind]!=99)) {
! 4014: dateintsum=dateintsum+k2;
! 4015: k2cpt++;
! 4016: /* printf("iind=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",iind, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
! 4017: }
! 4018: /*}*/
! 4019: } /* end m */
! 4020: } /* end bool */
! 4021: } /* end iind = 1 to imx */
! 4022: /* prop[s][age] is feeded for any initial and valid live state as well as
! 4023: freq[s1][s2][age] at single age of beginning the transition, for a combination j1 */
! 4024:
! 4025:
! 4026: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
! 4027: pstamp(ficresp);
! 4028: if (ncoveff>0) {
! 4029: fprintf(ficresp, "\n#********** Variable ");
! 4030: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
! 4031: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
! 4032: for (z1=1; z1<=ncoveff; z1++){
! 4033: fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4034: fprintf(ficresphtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4035: fprintf(ficresphtmfr, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4036: }
! 4037: fprintf(ficresp, "**********\n#");
! 4038: fprintf(ficresphtm, "**********</h3>\n");
! 4039: fprintf(ficresphtmfr, "**********</h3>\n");
! 4040: fprintf(ficlog, "\n#********** Variable ");
! 4041: for (z1=1; z1<=ncoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
! 4042: fprintf(ficlog, "**********\n");
! 4043: }
! 4044: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
! 4045: for(i=1; i<=nlstate;i++) {
! 4046: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
! 4047: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
! 4048: }
! 4049: fprintf(ficresp, "\n");
! 4050: fprintf(ficresphtm, "\n");
! 4051:
! 4052: /* Header of frequency table by age */
! 4053: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
! 4054: fprintf(ficresphtmfr,"<th>Age</th> ");
! 4055: for(jk=-1; jk <=nlstate+ndeath; jk++){
! 4056: for(m=-1; m <=nlstate+ndeath; m++){
! 4057: if(jk!=0 && m!=0)
! 4058: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
! 4059: }
! 4060: }
! 4061: fprintf(ficresphtmfr, "\n");
! 4062:
! 4063: /* For each age */
! 4064: for(iage=iagemin; iage <= iagemax+3; iage++){
! 4065: fprintf(ficresphtm,"<tr>");
! 4066: if(iage==iagemax+1){
! 4067: fprintf(ficlog,"1");
! 4068: fprintf(ficresphtmfr,"<tr><th>0</th> ");
! 4069: }else if(iage==iagemax+2){
! 4070: fprintf(ficlog,"0");
! 4071: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
! 4072: }else if(iage==iagemax+3){
! 4073: fprintf(ficlog,"Total");
! 4074: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
! 4075: }else{
! 4076: if(first==1){
! 4077: first=0;
! 4078: printf("See log file for details...\n");
! 4079: }
! 4080: fprintf(ficresphtmfr,"<tr><th>%d</th> ",iage);
! 4081: fprintf(ficlog,"Age %d", iage);
! 4082: }
! 4083: for(jk=1; jk <=nlstate ; jk++){
! 4084: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
! 4085: pp[jk] += freq[jk][m][iage];
! 4086: }
! 4087: for(jk=1; jk <=nlstate ; jk++){
! 4088: for(m=-1, pos=0; m <=0 ; m++)
! 4089: pos += freq[jk][m][iage];
! 4090: if(pp[jk]>=1.e-10){
! 4091: if(first==1){
! 4092: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
! 4093: }
! 4094: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
! 4095: }else{
! 4096: if(first==1)
! 4097: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
! 4098: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
! 4099: }
! 4100: }
! 4101:
! 4102: for(jk=1; jk <=nlstate ; jk++){
! 4103: /* posprop[jk]=0; */
! 4104: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)/* Summing on all ages */
! 4105: pp[jk] += freq[jk][m][iage];
! 4106: } /* pp[jk] is the total number of transitions starting from state jk and any ending status until this age */
! 4107:
! 4108: for(jk=1,pos=0, pospropta=0.; jk <=nlstate ; jk++){
! 4109: pos += pp[jk]; /* pos is the total number of transitions until this age */
! 4110: posprop[jk] += prop[jk][iage]; /* prop is the number of transitions from a live state
! 4111: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
! 4112: pospropta += prop[jk][iage]; /* prop is the number of transitions from a live state
! 4113: from jk at age iage prop[s[m][iind]][(int)agev[m][iind]] += weight[iind] */
! 4114: }
! 4115: for(jk=1; jk <=nlstate ; jk++){
! 4116: if(pos>=1.e-5){
! 4117: if(first==1)
! 4118: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
! 4119: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
! 4120: }else{
! 4121: if(first==1)
! 4122: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
! 4123: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
! 4124: }
! 4125: if( iage <= iagemax){
! 4126: if(pos>=1.e-5){
! 4127: fprintf(ficresp," %d %.5f %.0f %.0f",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
! 4128: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",iage,prop[jk][iage]/pospropta, prop[jk][iage],pospropta);
! 4129: /*probs[iage][jk][j1]= pp[jk]/pos;*/
! 4130: /*printf("\niage=%d jk=%d j1=%d %.5f %.0f %.0f %f",iage,jk,j1,pp[jk]/pos, pp[jk],pos,probs[iage][jk][j1]);*/
! 4131: }
! 4132: else{
! 4133: fprintf(ficresp," %d NaNq %.0f %.0f",iage,prop[jk][iage],pospropta);
! 4134: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",iage, prop[jk][iage],pospropta);
! 4135: }
! 4136: }
! 4137: pospropt[jk] +=posprop[jk];
! 4138: } /* end loop jk */
! 4139: /* pospropt=0.; */
! 4140: for(jk=-1; jk <=nlstate+ndeath; jk++){
! 4141: for(m=-1; m <=nlstate+ndeath; m++){
! 4142: if(freq[jk][m][iage] !=0 ) { /* minimizing output */
! 4143: if(first==1){
! 4144: printf(" %d%d=%.0f",jk,m,freq[jk][m][iage]);
! 4145: }
! 4146: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][iage]);
! 4147: }
! 4148: if(jk!=0 && m!=0)
! 4149: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][iage]);
! 4150: }
! 4151: } /* end loop jk */
! 4152: posproptt=0.;
! 4153: for(jk=1; jk <=nlstate; jk++){
! 4154: posproptt += pospropt[jk];
! 4155: }
! 4156: fprintf(ficresphtmfr,"</tr>\n ");
! 4157: if(iage <= iagemax){
! 4158: fprintf(ficresp,"\n");
! 4159: fprintf(ficresphtm,"</tr>\n");
! 4160: }
! 4161: if(first==1)
! 4162: printf("Others in log...\n");
! 4163: fprintf(ficlog,"\n");
! 4164: } /* end loop age iage */
! 4165: fprintf(ficresphtm,"<tr><th>Tot</th>");
! 4166: for(jk=1; jk <=nlstate ; jk++){
! 4167: if(posproptt < 1.e-5){
! 4168: fprintf(ficresphtm,"<td>Nanq</td><td>%.0f</td><td>%.0f</td>",pospropt[jk],posproptt);
! 4169: }else{
! 4170: fprintf(ficresphtm,"<td>%.5f</td><td>%.0f</td><td>%.0f</td>",pospropt[jk]/posproptt,pospropt[jk],posproptt);
! 4171: }
! 4172: }
! 4173: fprintf(ficresphtm,"</tr>\n");
! 4174: fprintf(ficresphtm,"</table>\n");
! 4175: fprintf(ficresphtmfr,"</table>\n");
! 4176: if(posproptt < 1.e-5){
! 4177: fprintf(ficresphtm,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
! 4178: fprintf(ficresphtmfr,"\n <p><b> This combination (%d) is not valid and no result will be produced</b></p>",j1);
! 4179: fprintf(ficres,"\n This combination (%d) is not valid and no result will be produced\n\n",j1);
! 4180: invalidvarcomb[j1]=1;
! 4181: }else{
! 4182: fprintf(ficresphtm,"\n <p> This combination (%d) is valid and result will be produced.</p>",j1);
! 4183: invalidvarcomb[j1]=0;
! 4184: }
! 4185: fprintf(ficresphtmfr,"</table>\n");
! 4186: } /* end selected combination of covariate j1 */
! 4187: dateintmean=dateintsum/k2cpt;
1.220 brouard 4188:
1.226 ! brouard 4189: fclose(ficresp);
! 4190: fclose(ficresphtm);
! 4191: fclose(ficresphtmfr);
! 4192: free_vector(meanq,1,nqfveff);
! 4193: free_matrix(meanqt,1,lastpass,1,nqtveff);
! 4194: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
! 4195: free_vector(pospropt,1,nlstate);
! 4196: free_vector(posprop,1,nlstate);
! 4197: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
! 4198: free_vector(pp,1,nlstate);
! 4199: /* End of freqsummary */
! 4200: }
1.126 brouard 4201:
4202: /************ Prevalence ********************/
1.222 brouard 4203: 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)
4204: {
4205: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
4206: in each health status at the date of interview (if between dateprev1 and dateprev2).
4207: We still use firstpass and lastpass as another selection.
4208: */
1.126 brouard 4209:
1.222 brouard 4210: int i, m, jk, j1, bool, z1,j;
4211: int mi; /* Effective wave */
4212: int iage;
4213: double agebegin, ageend;
4214:
4215: double **prop;
4216: double posprop;
4217: double y2; /* in fractional years */
4218: int iagemin, iagemax;
4219: int first; /** to stop verbosity which is redirected to log file */
4220:
4221: iagemin= (int) agemin;
4222: iagemax= (int) agemax;
4223: /*pp=vector(1,nlstate);*/
4224: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4225: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
4226: j1=0;
4227:
4228: /*j=cptcoveff;*/
4229: if (cptcovn<1) {j=1;ncodemax[1]=1;}
4230:
4231: first=1;
1.225 brouard 4232: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){ /* For each combination of covariate */
1.222 brouard 4233: for (i=1; i<=nlstate; i++)
4234: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
4235: prop[i][iage]=0.0;
4236:
4237: for (i=1; i<=imx; i++) { /* Each individual */
4238: bool=1;
4239: if (cptcovn>0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.225 brouard 4240: 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 4241: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)])
4242: bool=0;
4243: }
4244: if (bool==1) { /* For this combination of covariates values, this individual fits */
4245: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
4246: for(mi=1; mi<wav[i];mi++){
4247: m=mw[mi][i];
4248: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
4249: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
4250: if(m >=firstpass && m <=lastpass){
4251: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
4252: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
4253: if(agev[m][i]==0) agev[m][i]=iagemax+1;
4254: if(agev[m][i]==1) agev[m][i]=iagemax+2;
4255: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
4256: 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);
4257: exit(1);
4258: }
4259: if (s[m][i]>0 && s[m][i]<=nlstate) {
4260: /*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]]);*/
4261: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
4262: prop[s[m][i]][iagemax+3] += weight[i];
4263: } /* end valid statuses */
4264: } /* end selection of dates */
4265: } /* end selection of waves */
4266: } /* end effective waves */
4267: } /* end bool */
4268: }
4269: for(i=iagemin; i <= iagemax+3; i++){
4270: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
4271: posprop += prop[jk][i];
4272: }
4273:
4274: for(jk=1; jk <=nlstate ; jk++){
4275: if( i <= iagemax){
4276: if(posprop>=1.e-5){
4277: probs[i][jk][j1]= prop[jk][i]/posprop;
4278: } else{
4279: if(first==1){
4280: first=0;
4281: 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]);
4282: }
4283: }
4284: }
4285: }/* end jk */
4286: }/* end i */
4287: /*} *//* end i1 */
4288: } /* end j1 */
4289:
4290: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4291: /*free_vector(pp,1,nlstate);*/
4292: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
4293: } /* End of prevalence */
1.126 brouard 4294:
4295: /************* Waves Concatenation ***************/
4296:
4297: 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)
4298: {
4299: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4300: Death is a valid wave (if date is known).
4301: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4302: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4303: and mw[mi+1][i]. dh depends on stepm.
4304: */
4305:
1.224 brouard 4306: int i=0, mi=0, m=0, mli=0;
1.126 brouard 4307: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4308: double sum=0., jmean=0.;*/
1.224 brouard 4309: int first=0, firstwo=0, firsthree=0, firstfour=0, firstfiv=0;
1.126 brouard 4310: int j, k=0,jk, ju, jl;
4311: double sum=0.;
4312: first=0;
1.214 brouard 4313: firstwo=0;
1.217 brouard 4314: firsthree=0;
1.218 brouard 4315: firstfour=0;
1.164 brouard 4316: jmin=100000;
1.126 brouard 4317: jmax=-1;
4318: jmean=0.;
1.224 brouard 4319:
4320: /* Treating live states */
1.214 brouard 4321: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.224 brouard 4322: mi=0; /* First valid wave */
4323: mli=0; /* Last valid wave */
1.126 brouard 4324: m=firstpass;
1.214 brouard 4325: while(s[m][i] <= nlstate){ /* a live state */
1.224 brouard 4326: 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 */
4327: mli=m-1;/* mw[++mi][i]=m-1; */
4328: }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 4329: mw[++mi][i]=m;
1.224 brouard 4330: mli=m;
4331: } /* else might be a useless wave -1 and mi is not incremented and mw[mi] not updated */
4332: if(m < lastpass){ /* m < lastpass, standard case */
4333: m++; /* mi gives the "effective" current wave, m the current wave, go to next wave by incrementing m */
1.216 brouard 4334: }
1.224 brouard 4335: else{ /* m >= lastpass, eventual special issue with warning */
4336: #ifdef UNKNOWNSTATUSNOTCONTRIBUTING
4337: break;
4338: #else
1.223 brouard 4339: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
4340: if(firsthree == 0){
1.224 brouard 4341: 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 4342: firsthree=1;
4343: }
1.224 brouard 4344: 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 4345: mw[++mi][i]=m;
1.224 brouard 4346: mli=m;
1.223 brouard 4347: }
4348: if(s[m][i]==-2){ /* Vital status is really unknown */
4349: nbwarn++;
4350: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4351: 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 4352: 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 4353: }
4354: break;
4355: }
4356: break;
1.224 brouard 4357: #endif
4358: }/* End m >= lastpass */
1.126 brouard 4359: }/* end while */
1.224 brouard 4360:
4361: /* 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 4362: /* After last pass */
1.224 brouard 4363: /* Treating death states */
1.214 brouard 4364: if (s[m][i] > nlstate){ /* In a death state */
1.224 brouard 4365: /* if( mint[m][i]==mdc[m][i] && anint[m][i]==andc[m][i]){ /\* same date of death and date of interview *\/ */
4366: /* } */
1.126 brouard 4367: mi++; /* Death is another wave */
4368: /* if(mi==0) never been interviewed correctly before death */
1.223 brouard 4369: /* Only death is a correct wave */
1.126 brouard 4370: mw[mi][i]=m;
1.224 brouard 4371: }
4372: #ifndef DISPATCHINGKNOWNDEATHAFTERLASTWAVE
4373: 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 4374: /* m++; */
4375: /* mi++; */
4376: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4377: /* mw[mi][i]=m; */
1.218 brouard 4378: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
1.224 brouard 4379: 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 */
4380: nbwarn++;
4381: if(firstfiv==0){
4382: 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 );
4383: firstfiv=1;
4384: }else{
4385: 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 );
4386: }
4387: }else{ /* Death occured afer last wave potential bias */
4388: nberr++;
4389: if(firstwo==0){
4390: 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 );
4391: firstwo=1;
4392: }
4393: 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 4394: }
1.218 brouard 4395: }else{ /* end date of interview is known */
1.223 brouard 4396: /* death is known but not confirmed by death status at any wave */
4397: if(firstfour==0){
4398: 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 );
4399: firstfour=1;
4400: }
4401: 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 4402: }
1.224 brouard 4403: } /* end if date of death is known */
4404: #endif
4405: wav[i]=mi; /* mi should be the last effective wave (or mli) */
4406: /* wav[i]=mw[mi][i]; */
1.126 brouard 4407: if(mi==0){
4408: nbwarn++;
4409: if(first==0){
1.223 brouard 4410: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4411: first=1;
1.126 brouard 4412: }
4413: if(first==1){
1.223 brouard 4414: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
1.126 brouard 4415: }
4416: } /* end mi==0 */
4417: } /* End individuals */
1.214 brouard 4418: /* wav and mw are no more changed */
1.223 brouard 4419:
1.214 brouard 4420:
1.126 brouard 4421: for(i=1; i<=imx; i++){
4422: for(mi=1; mi<wav[i];mi++){
4423: if (stepm <=0)
1.223 brouard 4424: dh[mi][i]=1;
1.126 brouard 4425: else{
1.223 brouard 4426: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4427: if (agedc[i] < 2*AGESUP) {
4428: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4429: if(j==0) j=1; /* Survives at least one month after exam */
4430: else if(j<0){
4431: nberr++;
4432: 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]);
4433: j=1; /* Temporary Dangerous patch */
4434: 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);
4435: 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]);
4436: 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);
4437: }
4438: k=k+1;
4439: if (j >= jmax){
4440: jmax=j;
4441: ijmax=i;
4442: }
4443: if (j <= jmin){
4444: jmin=j;
4445: ijmin=i;
4446: }
4447: sum=sum+j;
4448: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4449: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4450: }
4451: }
4452: else{
4453: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
1.126 brouard 4454: /* 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 4455:
4456: k=k+1;
4457: if (j >= jmax) {
4458: jmax=j;
4459: ijmax=i;
4460: }
4461: else if (j <= jmin){
4462: jmin=j;
4463: ijmin=i;
4464: }
4465: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4466: /*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]);*/
4467: if(j<0){
4468: nberr++;
4469: 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]);
4470: 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]);
4471: }
4472: sum=sum+j;
4473: }
4474: jk= j/stepm;
4475: jl= j -jk*stepm;
4476: ju= j -(jk+1)*stepm;
4477: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4478: if(jl==0){
4479: dh[mi][i]=jk;
4480: bh[mi][i]=0;
4481: }else{ /* We want a negative bias in order to only have interpolation ie
4482: * to avoid the price of an extra matrix product in likelihood */
4483: dh[mi][i]=jk+1;
4484: bh[mi][i]=ju;
4485: }
4486: }else{
4487: if(jl <= -ju){
4488: dh[mi][i]=jk;
4489: bh[mi][i]=jl; /* bias is positive if real duration
4490: * is higher than the multiple of stepm and negative otherwise.
4491: */
4492: }
4493: else{
4494: dh[mi][i]=jk+1;
4495: bh[mi][i]=ju;
4496: }
4497: if(dh[mi][i]==0){
4498: dh[mi][i]=1; /* At least one step */
4499: bh[mi][i]=ju; /* At least one step */
4500: /* 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);*/
4501: }
4502: } /* end if mle */
1.126 brouard 4503: }
4504: } /* end wave */
4505: }
4506: jmean=sum/k;
4507: 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 4508: 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 4509: }
4510:
4511: /*********** Tricode ****************************/
1.220 brouard 4512: void tricode(int *cptcov, int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 4513: {
1.144 brouard 4514: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4515: /* 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 4516: * Boring subroutine which should only output nbcode[Tvar[j]][k]
1.224 brouard 4517: * Tvar[5] in V2+V1+V3*age+V2*V4 is 4 (V4) even it is a time varying or quantitative variable
4518: * nbcode[Tvar[5]][1]= nbcode[4][1]=0, nbcode[4][2]=1 (usually);
1.144 brouard 4519: */
1.130 brouard 4520:
1.145 brouard 4521: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 4522: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 4523: int cptcode=0; /* Modality max of covariates j */
4524: int modmincovj=0; /* Modality min of covariates j */
4525:
4526:
1.220 brouard 4527: /* cptcoveff=0; */
1.224 brouard 4528: /* *cptcov=0; */
1.126 brouard 4529:
1.144 brouard 4530: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4531:
1.224 brouard 4532: /* Loop on covariates without age and products and no quantitative variable */
4533: /* for (j=1; j<=(cptcovs); j++) { /\* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only *\/ */
1.225 brouard 4534: for (j=1; j<=cptcovsnq; j++) { /* From model V1 + V2*age + V3 + V3*V4 keeps V1 + V3 = 2 only */
1.192 brouard 4535: for (k=-1; k < maxncov; k++) Ndum[k]=0;
1.186 brouard 4536: for (i=1; i<=imx; i++) { /* Loop on individuals: reads the data file to get the maximum value of the
1.225 brouard 4537: modality of this covariate Vj*/
4538: switch(Typevar[j]) {
4539: case 1: /* A real fixed dummy covariate */
4540: ij=(int)(covar[Tvar[j]][i]); /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
4541: * If product of Vn*Vm, still boolean *:
4542: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
4543: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
4544: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
4545: modality of the nth covariate of individual i. */
4546: if (ij > modmaxcovj)
4547: modmaxcovj=ij;
4548: else if (ij < modmincovj)
4549: modmincovj=ij;
4550: if ((ij < -1) && (ij > NCOVMAX)){
4551: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
4552: exit(1);
4553: }else
4554: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
4555: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
4556: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
4557: /* getting the maximum value of the modality of the covariate
4558: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
4559: female ies 1, then modmaxcovj=1.*/
4560: break;
4561: case 2:
4562: break;
4563:
4564: }
4565: } /* end for loop on individuals i */
1.145 brouard 4566: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", j, Tvar[j], modmincovj, modmaxcovj);
1.192 brouard 4567: 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 4568: cptcode=modmaxcovj;
1.137 brouard 4569: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
1.225 brouard 4570: /*for (i=0; i<=cptcode; i++) {*/
1.192 brouard 4571: for (k=modmincovj; k<=modmaxcovj; k++) { /* k=-1 ? 0 and 1*//* For each value k of the modality of model-cov j */
4572: printf("Frequencies of covariates %d ie V%d with value %d: %d\n", j, Tvar[j], k, Ndum[k]);
4573: fprintf(ficlog, "Frequencies of covariates %d ie V%d with value %d: %d\n", j, Tvar[j], k, Ndum[k]);
4574: if( Ndum[k] != 0 ){ /* Counts if nobody answered modality k ie empty modality, we skip it and reorder */
1.225 brouard 4575: if( k != -1){
4576: ncodemax[j]++; /* ncodemax[j]= Number of modalities of the j th
4577: covariate for which somebody answered excluding
4578: undefined. Usually 2: 0 and 1. */
4579: }
4580: ncodemaxwundef[j]++; /* ncodemax[j]= Number of modalities of the j th
4581: covariate for which somebody answered including
4582: undefined. Usually 3: -1, 0 and 1. */
1.145 brouard 4583: }
4584: /* In fact ncodemax[j]=2 (dichotom. variables only) but it could be more for
1.225 brouard 4585: * historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
1.131 brouard 4586: } /* Ndum[-1] number of undefined modalities */
1.225 brouard 4587:
1.136 brouard 4588: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
1.186 brouard 4589: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7.
4590: 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 4591: modmincovj=3; modmaxcovj = 7;
1.186 brouard 4592: There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3;
4593: which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10;
4594: defining two dummy variables: variables V1_1 and V1_2.
1.145 brouard 4595: nbcode[Tvar[j]][ij]=k;
4596: nbcode[Tvar[j]][1]=0;
4597: nbcode[Tvar[j]][2]=1;
4598: nbcode[Tvar[j]][3]=2;
1.197 brouard 4599: To be continued (not working yet).
1.145 brouard 4600: */
1.197 brouard 4601: ij=0; /* ij is similar to i but can jump over null modalities */
4602: 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 4603: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
4604: break;
4605: }
4606: ij++;
4607: 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.*/
4608: cptcode = ij; /* New max modality for covar j */
1.192 brouard 4609: } /* end of loop on modality i=-1 to 1 or more */
1.225 brouard 4610:
1.192 brouard 4611: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4612: /* /\*recode from 0 *\/ */
4613: /* k is a modality. If we have model=V1+V1*sex */
4614: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4615: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4616: /* } */
4617: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4618: /* if (ij > ncodemax[j]) { */
4619: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4620: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4621: /* break; */
4622: /* } */
4623: /* } /\* end of loop on modality k *\/ */
1.137 brouard 4624: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4625:
1.225 brouard 4626: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.137 brouard 4627:
1.187 brouard 4628: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
1.225 brouard 4629: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
4630: ij=Tvar[i]; /* Tvar might be -1 if status was unknown */
4631: Ndum[ij]++; /* Might be supersed V1 + V1*age */
4632: } /* V4+V3+V5, Ndum[1]@5={0, 0, 1, 1, 1} */
4633:
4634: ij=0;
4635: for (i=0; i<= maxncov-1; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
4636: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
4637: if((Ndum[i]!=0) && (i<=ncovcol)){
4638: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
4639: Tvaraff[++ij]=i; /*For printing (unclear) */
4640: }else if((Ndum[i]!=0) && (i<=ncovcol+nqv)){
4641: Tvaraff[++ij]=-10; /* Dont'n know how to treat quantitative variables yet */
4642: }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv)){
4643: Tvaraff[++ij]=i; /*For printing (unclear) */
4644: }else if((Ndum[i]!=0) && (i<=ncovcol+nqv+ntv+nqtv)){
4645: Tvaraff[++ij]=-20; /* Dont'n know how to treat quantitative variables yet */
4646: }
4647: } /* Tvaraff[1]@5 {3, 4, -20, 0, 0} Very strange */
4648: /* ij--; */
4649: /* cptcoveff=ij; /\*Number of total covariates*\/ */
4650: *cptcov=ij; /*Number of total real effective covariates: effective
4651: * because they can be excluded from the model and real
4652: * if in the model but excluded because missing values*/
1.126 brouard 4653: }
4654:
1.145 brouard 4655:
1.126 brouard 4656: /*********** Health Expectancies ****************/
4657:
1.127 brouard 4658: 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 4659:
4660: {
4661: /* Health expectancies, no variances */
1.164 brouard 4662: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 4663: int nhstepma, nstepma; /* Decreasing with age */
4664: double age, agelim, hf;
4665: double ***p3mat;
4666: double eip;
4667:
4668: pstamp(ficreseij);
4669: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
4670: fprintf(ficreseij,"# Age");
4671: for(i=1; i<=nlstate;i++){
4672: for(j=1; j<=nlstate;j++){
4673: fprintf(ficreseij," e%1d%1d ",i,j);
4674: }
4675: fprintf(ficreseij," e%1d. ",i);
4676: }
4677: fprintf(ficreseij,"\n");
4678:
4679:
4680: if(estepm < stepm){
4681: printf ("Problem %d lower than %d\n",estepm, stepm);
4682: }
4683: else hstepm=estepm;
4684: /* We compute the life expectancy from trapezoids spaced every estepm months
4685: * This is mainly to measure the difference between two models: for example
4686: * if stepm=24 months pijx are given only every 2 years and by summing them
4687: * we are calculating an estimate of the Life Expectancy assuming a linear
4688: * progression in between and thus overestimating or underestimating according
4689: * to the curvature of the survival function. If, for the same date, we
4690: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4691: * to compare the new estimate of Life expectancy with the same linear
4692: * hypothesis. A more precise result, taking into account a more precise
4693: * curvature will be obtained if estepm is as small as stepm. */
4694:
4695: /* For example we decided to compute the life expectancy with the smallest unit */
4696: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4697: nhstepm is the number of hstepm from age to agelim
4698: nstepm is the number of stepm from age to agelin.
4699: Look at hpijx to understand the reason of that which relies in memory size
4700: and note for a fixed period like estepm months */
4701: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4702: survival function given by stepm (the optimization length). Unfortunately it
4703: means that if the survival funtion is printed only each two years of age and if
4704: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4705: results. So we changed our mind and took the option of the best precision.
4706: */
4707: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4708:
4709: agelim=AGESUP;
4710: /* If stepm=6 months */
4711: /* Computed by stepm unit matrices, product of hstepm matrices, stored
4712: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
4713:
4714: /* nhstepm age range expressed in number of stepm */
4715: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4716: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4717: /* if (stepm >= YEARM) hstepm=1;*/
4718: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4719: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4720:
4721: for (age=bage; age<=fage; age ++){
4722: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4723: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4724: /* if (stepm >= YEARM) hstepm=1;*/
4725: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
4726:
4727: /* If stepm=6 months */
4728: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4729: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4730:
4731: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4732:
4733: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
4734:
4735: printf("%d|",(int)age);fflush(stdout);
4736: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4737:
4738: /* Computing expectancies */
4739: for(i=1; i<=nlstate;i++)
4740: for(j=1; j<=nlstate;j++)
4741: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4742: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
4743:
4744: /* 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]);*/
4745:
4746: }
4747:
4748: fprintf(ficreseij,"%3.0f",age );
4749: for(i=1; i<=nlstate;i++){
4750: eip=0;
4751: for(j=1; j<=nlstate;j++){
4752: eip +=eij[i][j][(int)age];
4753: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
4754: }
4755: fprintf(ficreseij,"%9.4f", eip );
4756: }
4757: fprintf(ficreseij,"\n");
4758:
4759: }
4760: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4761: printf("\n");
4762: fprintf(ficlog,"\n");
4763:
4764: }
4765:
1.127 brouard 4766: 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 4767:
4768: {
4769: /* Covariances of health expectancies eij and of total life expectancies according
1.222 brouard 4770: to initial status i, ei. .
1.126 brouard 4771: */
4772: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
4773: int nhstepma, nstepma; /* Decreasing with age */
4774: double age, agelim, hf;
4775: double ***p3matp, ***p3matm, ***varhe;
4776: double **dnewm,**doldm;
4777: double *xp, *xm;
4778: double **gp, **gm;
4779: double ***gradg, ***trgradg;
4780: int theta;
4781:
4782: double eip, vip;
4783:
4784: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
4785: xp=vector(1,npar);
4786: xm=vector(1,npar);
4787: dnewm=matrix(1,nlstate*nlstate,1,npar);
4788: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
4789:
4790: pstamp(ficresstdeij);
4791: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
4792: fprintf(ficresstdeij,"# Age");
4793: for(i=1; i<=nlstate;i++){
4794: for(j=1; j<=nlstate;j++)
4795: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
4796: fprintf(ficresstdeij," e%1d. ",i);
4797: }
4798: fprintf(ficresstdeij,"\n");
4799:
4800: pstamp(ficrescveij);
4801: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
4802: fprintf(ficrescveij,"# Age");
4803: for(i=1; i<=nlstate;i++)
4804: for(j=1; j<=nlstate;j++){
4805: cptj= (j-1)*nlstate+i;
4806: for(i2=1; i2<=nlstate;i2++)
4807: for(j2=1; j2<=nlstate;j2++){
4808: cptj2= (j2-1)*nlstate+i2;
4809: if(cptj2 <= cptj)
4810: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
4811: }
4812: }
4813: fprintf(ficrescveij,"\n");
4814:
4815: if(estepm < stepm){
4816: printf ("Problem %d lower than %d\n",estepm, stepm);
4817: }
4818: else hstepm=estepm;
4819: /* We compute the life expectancy from trapezoids spaced every estepm months
4820: * This is mainly to measure the difference between two models: for example
4821: * if stepm=24 months pijx are given only every 2 years and by summing them
4822: * we are calculating an estimate of the Life Expectancy assuming a linear
4823: * progression in between and thus overestimating or underestimating according
4824: * to the curvature of the survival function. If, for the same date, we
4825: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4826: * to compare the new estimate of Life expectancy with the same linear
4827: * hypothesis. A more precise result, taking into account a more precise
4828: * curvature will be obtained if estepm is as small as stepm. */
4829:
4830: /* For example we decided to compute the life expectancy with the smallest unit */
4831: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4832: nhstepm is the number of hstepm from age to agelim
4833: nstepm is the number of stepm from age to agelin.
4834: Look at hpijx to understand the reason of that which relies in memory size
4835: and note for a fixed period like estepm months */
4836: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4837: survival function given by stepm (the optimization length). Unfortunately it
4838: means that if the survival funtion is printed only each two years of age and if
4839: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4840: results. So we changed our mind and took the option of the best precision.
4841: */
4842: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4843:
4844: /* If stepm=6 months */
4845: /* nhstepm age range expressed in number of stepm */
4846: agelim=AGESUP;
4847: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
4848: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4849: /* if (stepm >= YEARM) hstepm=1;*/
4850: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4851:
4852: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4853: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4854: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
4855: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
4856: gp=matrix(0,nhstepm,1,nlstate*nlstate);
4857: gm=matrix(0,nhstepm,1,nlstate*nlstate);
4858:
4859: for (age=bage; age<=fage; age ++){
4860: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4861: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4862: /* if (stepm >= YEARM) hstepm=1;*/
4863: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 brouard 4864:
1.126 brouard 4865: /* If stepm=6 months */
4866: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4867: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4868:
4869: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 brouard 4870:
1.126 brouard 4871: /* Computing Variances of health expectancies */
4872: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
4873: decrease memory allocation */
4874: for(theta=1; theta <=npar; theta++){
4875: for(i=1; i<=npar; i++){
1.222 brouard 4876: xp[i] = x[i] + (i==theta ?delti[theta]:0);
4877: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 4878: }
4879: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
4880: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
1.218 brouard 4881:
1.126 brouard 4882: for(j=1; j<= nlstate; j++){
1.222 brouard 4883: for(i=1; i<=nlstate; i++){
4884: for(h=0; h<=nhstepm-1; h++){
4885: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
4886: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
4887: }
4888: }
1.126 brouard 4889: }
1.218 brouard 4890:
1.126 brouard 4891: for(ij=1; ij<= nlstate*nlstate; ij++)
1.222 brouard 4892: for(h=0; h<=nhstepm-1; h++){
4893: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
4894: }
1.126 brouard 4895: }/* End theta */
4896:
4897:
4898: for(h=0; h<=nhstepm-1; h++)
4899: for(j=1; j<=nlstate*nlstate;j++)
1.222 brouard 4900: for(theta=1; theta <=npar; theta++)
4901: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 4902:
1.218 brouard 4903:
1.222 brouard 4904: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 4905: for(ji=1;ji<=nlstate*nlstate;ji++)
1.222 brouard 4906: varhe[ij][ji][(int)age] =0.;
1.218 brouard 4907:
1.222 brouard 4908: printf("%d|",(int)age);fflush(stdout);
4909: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4910: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 4911: for(k=0;k<=nhstepm-1;k++){
1.222 brouard 4912: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
4913: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
4914: for(ij=1;ij<=nlstate*nlstate;ij++)
4915: for(ji=1;ji<=nlstate*nlstate;ji++)
4916: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 4917: }
4918: }
1.218 brouard 4919:
1.126 brouard 4920: /* Computing expectancies */
4921: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4922: for(i=1; i<=nlstate;i++)
4923: for(j=1; j<=nlstate;j++)
1.222 brouard 4924: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4925: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
1.218 brouard 4926:
1.222 brouard 4927: /* 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 4928:
1.222 brouard 4929: }
1.218 brouard 4930:
1.126 brouard 4931: fprintf(ficresstdeij,"%3.0f",age );
4932: for(i=1; i<=nlstate;i++){
4933: eip=0.;
4934: vip=0.;
4935: for(j=1; j<=nlstate;j++){
1.222 brouard 4936: eip += eij[i][j][(int)age];
4937: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
4938: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
4939: 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 4940: }
4941: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
4942: }
4943: fprintf(ficresstdeij,"\n");
1.218 brouard 4944:
1.126 brouard 4945: fprintf(ficrescveij,"%3.0f",age );
4946: for(i=1; i<=nlstate;i++)
4947: for(j=1; j<=nlstate;j++){
1.222 brouard 4948: cptj= (j-1)*nlstate+i;
4949: for(i2=1; i2<=nlstate;i2++)
4950: for(j2=1; j2<=nlstate;j2++){
4951: cptj2= (j2-1)*nlstate+i2;
4952: if(cptj2 <= cptj)
4953: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
4954: }
1.126 brouard 4955: }
4956: fprintf(ficrescveij,"\n");
1.218 brouard 4957:
1.126 brouard 4958: }
4959: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
4960: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
4961: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
4962: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
4963: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4964: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4965: printf("\n");
4966: fprintf(ficlog,"\n");
1.218 brouard 4967:
1.126 brouard 4968: free_vector(xm,1,npar);
4969: free_vector(xp,1,npar);
4970: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
4971: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
4972: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
4973: }
1.218 brouard 4974:
1.126 brouard 4975: /************ Variance ******************/
1.209 brouard 4976: 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 4977: {
4978: /* Variance of health expectancies */
4979: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
4980: /* double **newm;*/
4981: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
4982:
4983: /* int movingaverage(); */
4984: double **dnewm,**doldm;
4985: double **dnewmp,**doldmp;
4986: int i, j, nhstepm, hstepm, h, nstepm ;
4987: int k;
4988: double *xp;
4989: double **gp, **gm; /* for var eij */
4990: double ***gradg, ***trgradg; /*for var eij */
4991: double **gradgp, **trgradgp; /* for var p point j */
4992: double *gpp, *gmp; /* for var p point j */
4993: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
4994: double ***p3mat;
4995: double age,agelim, hf;
4996: /* double ***mobaverage; */
4997: int theta;
4998: char digit[4];
4999: char digitp[25];
5000:
5001: char fileresprobmorprev[FILENAMELENGTH];
5002:
5003: if(popbased==1){
5004: if(mobilav!=0)
5005: strcpy(digitp,"-POPULBASED-MOBILAV_");
5006: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
5007: }
5008: else
5009: strcpy(digitp,"-STABLBASED_");
1.126 brouard 5010:
1.218 brouard 5011: /* if (mobilav!=0) { */
5012: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5013: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
5014: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
5015: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
5016: /* } */
5017: /* } */
5018:
5019: strcpy(fileresprobmorprev,"PRMORPREV-");
5020: sprintf(digit,"%-d",ij);
5021: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
5022: strcat(fileresprobmorprev,digit); /* Tvar to be done */
5023: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
5024: strcat(fileresprobmorprev,fileresu);
5025: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
5026: printf("Problem with resultfile: %s\n", fileresprobmorprev);
5027: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
5028: }
5029: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5030: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
5031: pstamp(ficresprobmorprev);
5032: 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);
5033: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
5034: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5035: fprintf(ficresprobmorprev," p.%-d SE",j);
5036: for(i=1; i<=nlstate;i++)
5037: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
5038: }
5039: fprintf(ficresprobmorprev,"\n");
5040:
5041: fprintf(ficgp,"\n# Routine varevsij");
5042: fprintf(ficgp,"\nunset title \n");
5043: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
5044: 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");
5045: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
5046: /* } */
5047: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5048: pstamp(ficresvij);
5049: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
5050: if(popbased==1)
5051: 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);
5052: else
5053: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
5054: fprintf(ficresvij,"# Age");
5055: for(i=1; i<=nlstate;i++)
5056: for(j=1; j<=nlstate;j++)
5057: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
5058: fprintf(ficresvij,"\n");
5059:
5060: xp=vector(1,npar);
5061: dnewm=matrix(1,nlstate,1,npar);
5062: doldm=matrix(1,nlstate,1,nlstate);
5063: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
5064: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5065:
5066: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
5067: gpp=vector(nlstate+1,nlstate+ndeath);
5068: gmp=vector(nlstate+1,nlstate+ndeath);
5069: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 5070:
1.218 brouard 5071: if(estepm < stepm){
5072: printf ("Problem %d lower than %d\n",estepm, stepm);
5073: }
5074: else hstepm=estepm;
5075: /* For example we decided to compute the life expectancy with the smallest unit */
5076: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
5077: nhstepm is the number of hstepm from age to agelim
5078: nstepm is the number of stepm from age to agelim.
5079: Look at function hpijx to understand why because of memory size limitations,
5080: we decided (b) to get a life expectancy respecting the most precise curvature of the
5081: survival function given by stepm (the optimization length). Unfortunately it
5082: means that if the survival funtion is printed every two years of age and if
5083: you sum them up and add 1 year (area under the trapezoids) you won't get the same
5084: results. So we changed our mind and took the option of the best precision.
5085: */
5086: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
5087: agelim = AGESUP;
5088: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5089: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5090: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
5091: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5092: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
5093: gp=matrix(0,nhstepm,1,nlstate);
5094: gm=matrix(0,nhstepm,1,nlstate);
5095:
5096:
5097: for(theta=1; theta <=npar; theta++){
5098: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
5099: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5100: }
5101:
5102: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5103:
5104: if (popbased==1) {
5105: if(mobilav ==0){
5106: for(i=1; i<=nlstate;i++)
5107: prlim[i][i]=probs[(int)age][i][ij];
5108: }else{ /* mobilav */
5109: for(i=1; i<=nlstate;i++)
5110: prlim[i][i]=mobaverage[(int)age][i][ij];
5111: }
5112: }
5113:
5114: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij); /* Returns p3mat[i][j][h] for h=1 to nhstepm */
5115: for(j=1; j<= nlstate; j++){
5116: for(h=0; h<=nhstepm; h++){
5117: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
5118: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
5119: }
5120: }
5121: /* Next for computing probability of death (h=1 means
5122: computed over hstepm matrices product = hstepm*stepm months)
5123: as a weighted average of prlim.
5124: */
5125: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5126: for(i=1,gpp[j]=0.; i<= nlstate; i++)
5127: gpp[j] += prlim[i][i]*p3mat[i][j][1];
5128: }
5129: /* end probability of death */
5130:
5131: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
5132: xp[i] = x[i] - (i==theta ?delti[theta]:0);
5133:
5134: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij);
5135:
5136: if (popbased==1) {
5137: if(mobilav ==0){
5138: for(i=1; i<=nlstate;i++)
5139: prlim[i][i]=probs[(int)age][i][ij];
5140: }else{ /* mobilav */
5141: for(i=1; i<=nlstate;i++)
5142: prlim[i][i]=mobaverage[(int)age][i][ij];
5143: }
5144: }
5145:
5146: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
5147:
5148: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
5149: for(h=0; h<=nhstepm; h++){
5150: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
5151: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
5152: }
5153: }
5154: /* This for computing probability of death (h=1 means
5155: computed over hstepm matrices product = hstepm*stepm months)
5156: as a weighted average of prlim.
5157: */
5158: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5159: for(i=1,gmp[j]=0.; i<= nlstate; i++)
5160: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5161: }
5162: /* end probability of death */
5163:
5164: for(j=1; j<= nlstate; j++) /* vareij */
5165: for(h=0; h<=nhstepm; h++){
5166: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
5167: }
5168:
5169: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
5170: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
5171: }
5172:
5173: } /* End theta */
5174:
5175: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
5176:
5177: for(h=0; h<=nhstepm; h++) /* veij */
5178: for(j=1; j<=nlstate;j++)
5179: for(theta=1; theta <=npar; theta++)
5180: trgradg[h][j][theta]=gradg[h][theta][j];
5181:
5182: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
5183: for(theta=1; theta <=npar; theta++)
5184: trgradgp[j][theta]=gradgp[theta][j];
5185:
5186:
5187: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
5188: for(i=1;i<=nlstate;i++)
5189: for(j=1;j<=nlstate;j++)
5190: vareij[i][j][(int)age] =0.;
5191:
5192: for(h=0;h<=nhstepm;h++){
5193: for(k=0;k<=nhstepm;k++){
5194: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
5195: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
5196: for(i=1;i<=nlstate;i++)
5197: for(j=1;j<=nlstate;j++)
5198: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
5199: }
5200: }
5201:
5202: /* pptj */
5203: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
5204: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
5205: for(j=nlstate+1;j<=nlstate+ndeath;j++)
5206: for(i=nlstate+1;i<=nlstate+ndeath;i++)
5207: varppt[j][i]=doldmp[j][i];
5208: /* end ppptj */
5209: /* x centered again */
5210:
5211: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij);
5212:
5213: if (popbased==1) {
5214: if(mobilav ==0){
5215: for(i=1; i<=nlstate;i++)
5216: prlim[i][i]=probs[(int)age][i][ij];
5217: }else{ /* mobilav */
5218: for(i=1; i<=nlstate;i++)
5219: prlim[i][i]=mobaverage[(int)age][i][ij];
5220: }
5221: }
5222:
5223: /* This for computing probability of death (h=1 means
5224: computed over hstepm (estepm) matrices product = hstepm*stepm months)
5225: as a weighted average of prlim.
5226: */
5227: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
5228: for(j=nlstate+1;j<=nlstate+ndeath;j++){
5229: for(i=1,gmp[j]=0.;i<= nlstate; i++)
5230: gmp[j] += prlim[i][i]*p3mat[i][j][1];
5231: }
5232: /* end probability of death */
5233:
5234: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
5235: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
5236: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
5237: for(i=1; i<=nlstate;i++){
5238: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
5239: }
5240: }
5241: fprintf(ficresprobmorprev,"\n");
5242:
5243: fprintf(ficresvij,"%.0f ",age );
5244: for(i=1; i<=nlstate;i++)
5245: for(j=1; j<=nlstate;j++){
5246: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
5247: }
5248: fprintf(ficresvij,"\n");
5249: free_matrix(gp,0,nhstepm,1,nlstate);
5250: free_matrix(gm,0,nhstepm,1,nlstate);
5251: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
5252: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
5253: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
5254: } /* End age */
5255: free_vector(gpp,nlstate+1,nlstate+ndeath);
5256: free_vector(gmp,nlstate+1,nlstate+ndeath);
5257: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
5258: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
5259: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
5260: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
5261: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
5262: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
5263: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
5264: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
5265: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
5266: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
5267: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
5268: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
5269: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
5270: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
5271: 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);
5272: /* 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 5273: */
1.218 brouard 5274: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
5275: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 5276:
1.218 brouard 5277: free_vector(xp,1,npar);
5278: free_matrix(doldm,1,nlstate,1,nlstate);
5279: free_matrix(dnewm,1,nlstate,1,npar);
5280: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5281: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
5282: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
5283: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
5284: fclose(ficresprobmorprev);
5285: fflush(ficgp);
5286: fflush(fichtm);
5287: } /* end varevsij */
1.126 brouard 5288:
5289: /************ Variance of prevlim ******************/
1.209 brouard 5290: 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 5291: {
1.205 brouard 5292: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 5293: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 5294:
1.126 brouard 5295: double **dnewm,**doldm;
5296: int i, j, nhstepm, hstepm;
5297: double *xp;
5298: double *gp, *gm;
5299: double **gradg, **trgradg;
1.208 brouard 5300: double **mgm, **mgp;
1.126 brouard 5301: double age,agelim;
5302: int theta;
5303:
5304: pstamp(ficresvpl);
5305: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
5306: fprintf(ficresvpl,"# Age");
5307: for(i=1; i<=nlstate;i++)
5308: fprintf(ficresvpl," %1d-%1d",i,i);
5309: fprintf(ficresvpl,"\n");
5310:
5311: xp=vector(1,npar);
5312: dnewm=matrix(1,nlstate,1,npar);
5313: doldm=matrix(1,nlstate,1,nlstate);
5314:
5315: hstepm=1*YEARM; /* Every year of age */
5316: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5317: agelim = AGESUP;
5318: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5319: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5320: if (stepm >= YEARM) hstepm=1;
5321: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5322: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5323: mgp=matrix(1,npar,1,nlstate);
5324: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5325: gp=vector(1,nlstate);
5326: gm=vector(1,nlstate);
5327:
5328: for(theta=1; theta <=npar; theta++){
5329: for(i=1; i<=npar; i++){ /* Computes gradient */
5330: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5331: }
1.209 brouard 5332: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5333: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5334: else
5335: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5336: for(i=1;i<=nlstate;i++){
1.126 brouard 5337: gp[i] = prlim[i][i];
1.208 brouard 5338: mgp[theta][i] = prlim[i][i];
5339: }
1.126 brouard 5340: for(i=1; i<=npar; i++) /* Computes gradient */
5341: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5342: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5343: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5344: else
5345: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5346: for(i=1;i<=nlstate;i++){
1.126 brouard 5347: gm[i] = prlim[i][i];
1.208 brouard 5348: mgm[theta][i] = prlim[i][i];
5349: }
1.126 brouard 5350: for(i=1;i<=nlstate;i++)
5351: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5352: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5353: } /* End theta */
5354:
5355: trgradg =matrix(1,nlstate,1,npar);
5356:
5357: for(j=1; j<=nlstate;j++)
5358: for(theta=1; theta <=npar; theta++)
5359: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5360: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5361: /* printf("\nmgm mgp %d ",(int)age); */
5362: /* for(j=1; j<=nlstate;j++){ */
5363: /* printf(" %d ",j); */
5364: /* for(theta=1; theta <=npar; theta++) */
5365: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5366: /* printf("\n "); */
5367: /* } */
5368: /* } */
5369: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5370: /* printf("\n gradg %d ",(int)age); */
5371: /* for(j=1; j<=nlstate;j++){ */
5372: /* printf("%d ",j); */
5373: /* for(theta=1; theta <=npar; theta++) */
5374: /* printf("%d %lf ",theta,gradg[theta][j]); */
5375: /* printf("\n "); */
5376: /* } */
5377: /* } */
1.126 brouard 5378:
5379: for(i=1;i<=nlstate;i++)
5380: varpl[i][(int)age] =0.;
1.209 brouard 5381: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5382: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5383: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5384: }else{
1.126 brouard 5385: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5386: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5387: }
1.126 brouard 5388: for(i=1;i<=nlstate;i++)
5389: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5390:
5391: fprintf(ficresvpl,"%.0f ",age );
5392: for(i=1; i<=nlstate;i++)
5393: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5394: fprintf(ficresvpl,"\n");
5395: free_vector(gp,1,nlstate);
5396: free_vector(gm,1,nlstate);
1.208 brouard 5397: free_matrix(mgm,1,npar,1,nlstate);
5398: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5399: free_matrix(gradg,1,npar,1,nlstate);
5400: free_matrix(trgradg,1,nlstate,1,npar);
5401: } /* End age */
5402:
5403: free_vector(xp,1,npar);
5404: free_matrix(doldm,1,nlstate,1,npar);
5405: free_matrix(dnewm,1,nlstate,1,nlstate);
5406:
5407: }
5408:
5409: /************ Variance of one-step probabilities ******************/
5410: 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 5411: {
5412: int i, j=0, k1, l1, tj;
5413: int k2, l2, j1, z1;
5414: int k=0, l;
5415: int first=1, first1, first2;
5416: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5417: double **dnewm,**doldm;
5418: double *xp;
5419: double *gp, *gm;
5420: double **gradg, **trgradg;
5421: double **mu;
5422: double age, cov[NCOVMAX+1];
5423: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5424: int theta;
5425: char fileresprob[FILENAMELENGTH];
5426: char fileresprobcov[FILENAMELENGTH];
5427: char fileresprobcor[FILENAMELENGTH];
5428: double ***varpij;
5429:
5430: strcpy(fileresprob,"PROB_");
5431: strcat(fileresprob,fileres);
5432: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5433: printf("Problem with resultfile: %s\n", fileresprob);
5434: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5435: }
5436: strcpy(fileresprobcov,"PROBCOV_");
5437: strcat(fileresprobcov,fileresu);
5438: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5439: printf("Problem with resultfile: %s\n", fileresprobcov);
5440: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5441: }
5442: strcpy(fileresprobcor,"PROBCOR_");
5443: strcat(fileresprobcor,fileresu);
5444: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5445: printf("Problem with resultfile: %s\n", fileresprobcor);
5446: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5447: }
5448: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5449: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5450: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5451: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5452: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5453: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5454: pstamp(ficresprob);
5455: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5456: fprintf(ficresprob,"# Age");
5457: pstamp(ficresprobcov);
5458: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5459: fprintf(ficresprobcov,"# Age");
5460: pstamp(ficresprobcor);
5461: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5462: fprintf(ficresprobcor,"# Age");
1.126 brouard 5463:
5464:
1.222 brouard 5465: for(i=1; i<=nlstate;i++)
5466: for(j=1; j<=(nlstate+ndeath);j++){
5467: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5468: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5469: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5470: }
5471: /* fprintf(ficresprob,"\n");
5472: fprintf(ficresprobcov,"\n");
5473: fprintf(ficresprobcor,"\n");
5474: */
5475: xp=vector(1,npar);
5476: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5477: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5478: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5479: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5480: first=1;
5481: fprintf(ficgp,"\n# Routine varprob");
5482: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5483: fprintf(fichtm,"\n");
5484:
5485: 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);
5486: 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);
5487: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5488: and drawn. It helps understanding how is the covariance between two incidences.\
5489: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
1.222 brouard 5490: 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 5491: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5492: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5493: standard deviations wide on each axis. <br>\
5494: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5495: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5496: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5497:
1.222 brouard 5498: cov[1]=1;
5499: /* tj=cptcoveff; */
1.225 brouard 5500: tj = (int) pow(2,cptcoveff);
1.222 brouard 5501: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5502: j1=0;
1.224 brouard 5503: for(j1=1; j1<=tj;j1++){ /* For each valid combination of covariates or only once*/
1.222 brouard 5504: if (cptcovn>0) {
5505: fprintf(ficresprob, "\n#********** Variable ");
1.225 brouard 5506: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5507: fprintf(ficresprob, "**********\n#\n");
5508: fprintf(ficresprobcov, "\n#********** Variable ");
1.225 brouard 5509: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5510: fprintf(ficresprobcov, "**********\n#\n");
1.220 brouard 5511:
1.222 brouard 5512: fprintf(ficgp, "\n#********** Variable ");
1.225 brouard 5513: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5514: fprintf(ficgp, "**********\n#\n");
1.220 brouard 5515:
5516:
1.222 brouard 5517: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.225 brouard 5518: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5519: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5520:
1.222 brouard 5521: fprintf(ficresprobcor, "\n#********** Variable ");
1.225 brouard 5522: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.222 brouard 5523: fprintf(ficresprobcor, "**********\n#");
5524: if(invalidvarcomb[j1]){
5525: fprintf(ficgp,"\n#Combination (%d) ignored because no cases \n",j1);
5526: fprintf(fichtmcov,"\n<h3>Combination (%d) ignored because no cases </h3>\n",j1);
5527: continue;
5528: }
5529: }
5530: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5531: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5532: gp=vector(1,(nlstate)*(nlstate+ndeath));
5533: gm=vector(1,(nlstate)*(nlstate+ndeath));
5534: for (age=bage; age<=fage; age ++){
5535: cov[2]=age;
5536: if(nagesqr==1)
5537: cov[3]= age*age;
5538: for (k=1; k<=cptcovn;k++) {
5539: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5540: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
5541: * 1 1 1 1 1
5542: * 2 2 1 1 1
5543: * 3 1 2 1 1
5544: */
5545: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
5546: }
5547: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
5548: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
5549: for (k=1; k<=cptcovprod;k++)
5550: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.220 brouard 5551:
5552:
1.222 brouard 5553: for(theta=1; theta <=npar; theta++){
5554: for(i=1; i<=npar; i++)
5555: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
1.220 brouard 5556:
1.222 brouard 5557: pmij(pmmij,cov,ncovmodel,xp,nlstate);
1.220 brouard 5558:
1.222 brouard 5559: k=0;
5560: for(i=1; i<= (nlstate); i++){
5561: for(j=1; j<=(nlstate+ndeath);j++){
5562: k=k+1;
5563: gp[k]=pmmij[i][j];
5564: }
5565: }
1.220 brouard 5566:
1.222 brouard 5567: for(i=1; i<=npar; i++)
5568: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
1.220 brouard 5569:
1.222 brouard 5570: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5571: k=0;
5572: for(i=1; i<=(nlstate); i++){
5573: for(j=1; j<=(nlstate+ndeath);j++){
5574: k=k+1;
5575: gm[k]=pmmij[i][j];
5576: }
5577: }
1.220 brouard 5578:
1.222 brouard 5579: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5580: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5581: }
1.126 brouard 5582:
1.222 brouard 5583: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5584: for(theta=1; theta <=npar; theta++)
5585: trgradg[j][theta]=gradg[theta][j];
1.220 brouard 5586:
1.222 brouard 5587: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5588: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
1.220 brouard 5589:
1.222 brouard 5590: pmij(pmmij,cov,ncovmodel,x,nlstate);
1.220 brouard 5591:
1.222 brouard 5592: k=0;
5593: for(i=1; i<=(nlstate); i++){
5594: for(j=1; j<=(nlstate+ndeath);j++){
5595: k=k+1;
5596: mu[k][(int) age]=pmmij[i][j];
5597: }
5598: }
5599: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5600: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5601: varpij[i][j][(int)age] = doldm[i][j];
1.220 brouard 5602:
1.222 brouard 5603: /*printf("\n%d ",(int)age);
5604: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5605: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5606: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5607: }*/
1.220 brouard 5608:
1.222 brouard 5609: fprintf(ficresprob,"\n%d ",(int)age);
5610: fprintf(ficresprobcov,"\n%d ",(int)age);
5611: fprintf(ficresprobcor,"\n%d ",(int)age);
1.220 brouard 5612:
1.222 brouard 5613: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5614: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5615: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5616: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5617: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5618: }
5619: i=0;
5620: for (k=1; k<=(nlstate);k++){
5621: for (l=1; l<=(nlstate+ndeath);l++){
5622: i++;
5623: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5624: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5625: for (j=1; j<=i;j++){
5626: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
5627: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5628: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5629: }
5630: }
5631: }/* end of loop for state */
5632: } /* end of loop for age */
5633: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
5634: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
5635: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5636: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5637:
5638: /* Confidence intervalle of pij */
5639: /*
5640: fprintf(ficgp,"\nunset parametric;unset label");
5641: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
5642: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
5643: 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);
5644: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
5645: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
5646: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
5647: */
5648:
5649: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
5650: first1=1;first2=2;
5651: for (k2=1; k2<=(nlstate);k2++){
5652: for (l2=1; l2<=(nlstate+ndeath);l2++){
5653: if(l2==k2) continue;
5654: j=(k2-1)*(nlstate+ndeath)+l2;
5655: for (k1=1; k1<=(nlstate);k1++){
5656: for (l1=1; l1<=(nlstate+ndeath);l1++){
5657: if(l1==k1) continue;
5658: i=(k1-1)*(nlstate+ndeath)+l1;
5659: if(i<=j) continue;
5660: for (age=bage; age<=fage; age ++){
5661: if ((int)age %5==0){
5662: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
5663: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
5664: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
5665: mu1=mu[i][(int) age]/stepm*YEARM ;
5666: mu2=mu[j][(int) age]/stepm*YEARM;
5667: c12=cv12/sqrt(v1*v2);
5668: /* Computing eigen value of matrix of covariance */
5669: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5670: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5671: if ((lc2 <0) || (lc1 <0) ){
5672: if(first2==1){
5673: first1=0;
5674: 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);
5675: }
5676: 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);
5677: /* lc1=fabs(lc1); */ /* If we want to have them positive */
5678: /* lc2=fabs(lc2); */
5679: }
1.220 brouard 5680:
1.222 brouard 5681: /* Eigen vectors */
5682: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
5683: /*v21=sqrt(1.-v11*v11); *//* error */
5684: v21=(lc1-v1)/cv12*v11;
5685: v12=-v21;
5686: v22=v11;
5687: tnalp=v21/v11;
5688: if(first1==1){
5689: first1=0;
5690: 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);
5691: }
5692: 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);
5693: /*printf(fignu*/
5694: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
5695: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
5696: if(first==1){
5697: first=0;
5698: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
5699: fprintf(ficgp,"\nset parametric;unset label");
5700: 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);
5701: fprintf(ficgp,"\nset ter svg size 640, 480");
5702: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.220 brouard 5703: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\"> \
1.201 brouard 5704: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
1.222 brouard 5705: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2, \
5706: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5707: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5708: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
5709: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5710: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5711: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5712: 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", \
5713: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5714: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5715: }else{
5716: first=0;
5717: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
5718: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5719: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5720: 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", \
5721: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2), \
5722: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5723: }/* if first */
5724: } /* age mod 5 */
5725: } /* end loop age */
5726: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5727: first=1;
5728: } /*l12 */
5729: } /* k12 */
5730: } /*l1 */
5731: }/* k1 */
5732: } /* loop on combination of covariates j1 */
5733: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
5734: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
5735: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5736: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
5737: free_vector(xp,1,npar);
5738: fclose(ficresprob);
5739: fclose(ficresprobcov);
5740: fclose(ficresprobcor);
5741: fflush(ficgp);
5742: fflush(fichtmcov);
5743: }
1.126 brouard 5744:
5745:
5746: /******************* Printing html file ***********/
1.201 brouard 5747: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 5748: int lastpass, int stepm, int weightopt, char model[],\
5749: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 5750: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 5751: double jprev1, double mprev1,double anprev1, double dateprev1, \
5752: double jprev2, double mprev2,double anprev2, double dateprev2){
1.126 brouard 5753: int jj1, k1, i1, cpt;
5754:
5755: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
5756: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
5757: </ul>");
1.214 brouard 5758: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
5759: 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",
5760: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
5761: 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 5762: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
5763: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 5764: fprintf(fichtm,"\
5765: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 5766: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 5767: fprintf(fichtm,"\
1.217 brouard 5768: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
5769: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
5770: fprintf(fichtm,"\
1.126 brouard 5771: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5772: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 5773: fprintf(fichtm,"\
1.217 brouard 5774: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
5775: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
5776: fprintf(fichtm,"\
1.211 brouard 5777: - (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 5778: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5779: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 5780: if(prevfcast==1){
5781: fprintf(fichtm,"\
5782: - Prevalence projections by age and states: \
1.201 brouard 5783: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 5784: }
1.126 brouard 5785:
1.222 brouard 5786: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
1.126 brouard 5787:
1.225 brouard 5788: m=pow(2,cptcoveff);
1.222 brouard 5789: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 5790:
1.222 brouard 5791: jj1=0;
5792: for(k1=1; k1<=m;k1++){
1.220 brouard 5793:
1.222 brouard 5794: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
5795: jj1++;
5796: if (cptcovn > 0) {
5797: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 5798: for (cpt=1; cpt<=cptcoveff;cpt++){
1.222 brouard 5799: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
5800: printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout);
5801: }
5802: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
5803: if(invalidvarcomb[k1]){
5804: fprintf(fichtm,"\n<h3>Combination (%d) ignored because no cases </h3>\n",k1);
5805: printf("\nCombination (%d) ignored because no cases \n",k1);
5806: continue;
5807: }
5808: }
5809: /* aij, bij */
5810: 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 5811: <img src=\"%s_%d-1.svg\">",model,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5812: /* Pij */
5813: 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 5814: <img src=\"%s_%d-2.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5815: /* Quasi-incidences */
5816: 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 5817: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too, \
1.211 brouard 5818: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
5819: 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 5820: <img src=\"%s_%d-3.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.222 brouard 5821: /* Survival functions (period) in state j */
5822: for(cpt=1; cpt<=nlstate;cpt++){
5823: 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 5824: <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 5825: }
5826: /* State specific survival functions (period) */
5827: for(cpt=1; cpt<=nlstate;cpt++){
5828: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.220 brouard 5829: Or probability to survive in various states (1 to %d) being in state %d at different ages. \
1.201 brouard 5830: <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 5831: }
5832: /* Period (stable) prevalence in each health state */
5833: for(cpt=1; cpt<=nlstate;cpt++){
5834: 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 5835: <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 5836: }
5837: if(backcast==1){
5838: /* Period (stable) back prevalence in each health state */
5839: for(cpt=1; cpt<=nlstate;cpt++){
5840: 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 5841: <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 5842: }
1.217 brouard 5843: }
1.222 brouard 5844: if(prevfcast==1){
5845: /* Projection of prevalence up to period (stable) prevalence in each health state */
5846: for(cpt=1; cpt<=nlstate;cpt++){
5847: 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 5848: <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 5849: }
5850: }
1.220 brouard 5851:
1.222 brouard 5852: for(cpt=1; cpt<=nlstate;cpt++) {
5853: 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 5854: <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 5855: }
5856: /* } /\* end i1 *\/ */
5857: }/* End k1 */
5858: fprintf(fichtm,"</ul>");
1.126 brouard 5859:
1.222 brouard 5860: fprintf(fichtm,"\
1.126 brouard 5861: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 5862: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 5863: - 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 5864: But because parameters are usually highly correlated (a higher incidence of disability \
5865: and a higher incidence of recovery can give very close observed transition) it might \
5866: be very useful to look not only at linear confidence intervals estimated from the \
5867: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
5868: (parameters) of the logistic regression, it might be more meaningful to visualize the \
5869: covariance matrix of the one-step probabilities. \
5870: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 5871:
1.222 brouard 5872: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
5873: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
5874: fprintf(fichtm,"\
1.126 brouard 5875: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 5876: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 5877:
1.222 brouard 5878: fprintf(fichtm,"\
1.126 brouard 5879: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.222 brouard 5880: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
5881: fprintf(fichtm,"\
1.126 brouard 5882: - 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): \
5883: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 5884: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.222 brouard 5885: fprintf(fichtm,"\
1.126 brouard 5886: - (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): \
5887: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 5888: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.222 brouard 5889: fprintf(fichtm,"\
1.128 brouard 5890: - 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 5891: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
5892: fprintf(fichtm,"\
1.128 brouard 5893: - 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 5894: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
5895: fprintf(fichtm,"\
1.126 brouard 5896: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.222 brouard 5897: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 5898:
5899: /* if(popforecast==1) fprintf(fichtm,"\n */
5900: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
5901: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
5902: /* <br>",fileres,fileres,fileres,fileres); */
5903: /* else */
5904: /* 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 5905: fflush(fichtm);
5906: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
1.126 brouard 5907:
1.225 brouard 5908: m=pow(2,cptcoveff);
1.222 brouard 5909: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
1.126 brouard 5910:
1.222 brouard 5911: jj1=0;
5912: for(k1=1; k1<=m;k1++){
5913: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
5914: jj1++;
1.126 brouard 5915: if (cptcovn > 0) {
5916: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.225 brouard 5917: for (cpt=1; cpt<=cptcoveff;cpt++) /**< cptcoveff number of variables */
1.222 brouard 5918: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
1.126 brouard 5919: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
1.220 brouard 5920:
1.222 brouard 5921: if(invalidvarcomb[k1]){
5922: fprintf(fichtm,"\n<h4>Combination (%d) ignored because no cases </h4>\n",k1);
5923: continue;
5924: }
1.126 brouard 5925: }
5926: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 brouard 5927: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
5928: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d.svg\"> %s_%d-%d.svg</a>\n <br>\
1.205 brouard 5929: <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 5930: }
5931: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 5932: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
5933: true period expectancies (those weighted with period prevalences are also\
5934: drawn in addition to the population based expectancies computed using\
1.218 brouard 5935: observed and cahotic prevalences: <a href=\"%s_%d.svg\">%s_%d.svg</a>\n<br>\
1.205 brouard 5936: <img src=\"%s_%d.svg\">",subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1);
1.222 brouard 5937: /* } /\* end i1 *\/ */
5938: }/* End k1 */
5939: fprintf(fichtm,"</ul>");
5940: fflush(fichtm);
1.126 brouard 5941: }
5942:
5943: /******************* Gnuplot file **************/
1.223 brouard 5944: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 5945:
5946: char dirfileres[132],optfileres[132];
1.223 brouard 5947: char gplotcondition[132];
1.164 brouard 5948: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
1.211 brouard 5949: int lv=0, vlv=0, kl=0;
1.130 brouard 5950: int ng=0;
1.201 brouard 5951: int vpopbased;
1.223 brouard 5952: int ioffset; /* variable offset for columns */
1.219 brouard 5953:
1.126 brouard 5954: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
5955: /* printf("Problem with file %s",optionfilegnuplot); */
5956: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
5957: /* } */
5958:
5959: /*#ifdef windows */
5960: fprintf(ficgp,"cd \"%s\" \n",pathc);
1.223 brouard 5961: /*#endif */
1.225 brouard 5962: m=pow(2,cptcoveff);
1.126 brouard 5963:
1.202 brouard 5964: /* Contribution to likelihood */
5965: /* Plot the probability implied in the likelihood */
1.223 brouard 5966: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
5967: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
5968: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
5969: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 5970: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 5971: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
5972: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
1.223 brouard 5973: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
5974: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
5975: 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));
5976: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
5977: 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));
5978: for (i=1; i<= nlstate ; i ++) {
5979: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
5980: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
5981: 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);
5982: for (j=2; j<= nlstate+ndeath ; j ++) {
5983: 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);
5984: }
5985: fprintf(ficgp,";\nset out; unset ylabel;\n");
5986: }
5987: /* 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 */
5988: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
5989: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
5990: fprintf(ficgp,"\nset out;unset log\n");
5991: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
1.202 brouard 5992:
1.126 brouard 5993: strcpy(dirfileres,optionfilefiname);
5994: strcpy(optfileres,"vpl");
1.223 brouard 5995: /* 1eme*/
1.211 brouard 5996: for (cpt=1; cpt<= nlstate ; cpt ++) { /* For each live state */
1.220 brouard 5997: for (k1=1; k1<= m ; k1 ++) { /* For each valid combination of covariate */
1.211 brouard 5998: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
5999: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files ");
1.225 brouard 6000: for (k=1; k<=cptcoveff; k++){ /* For each covariate k get corresponding value lv for combination k1 */
6001: lv= decodtabm(k1,k,cptcoveff); /* Should be the value of the covariate corresponding to k1 combination */
1.223 brouard 6002: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6003: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6004: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6005: vlv= nbcode[Tvaraff[k]][lv]; /* vlv is the value of the covariate lv, 0 or 1 */
6006: /* For each combination of covariate k1 (V1=1, V3=0), we printed the current covariate k and its value vlv */
6007: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6008: }
6009: fprintf(ficgp,"\n#\n");
1.223 brouard 6010: if(invalidvarcomb[k1]){
6011: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6012: continue;
6013: }
1.211 brouard 6014:
1.223 brouard 6015: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1);
6016: fprintf(ficgp,"\n#set out \"V_%s_%d-%d.svg\" \n",optionfilefiname,cpt,k1);
6017: fprintf(ficgp,"set xlabel \"Age\" \n\
1.219 brouard 6018: set ylabel \"Probability\" \n \
6019: set ter svg size 640, 480\n \
1.201 brouard 6020: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"%%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1);
1.219 brouard 6021:
1.223 brouard 6022: for (i=1; i<= nlstate ; i ++) {
6023: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6024: else fprintf(ficgp," %%*lf (%%*lf)");
6025: }
6026: 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);
6027: for (i=1; i<= nlstate ; i ++) {
6028: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6029: else fprintf(ficgp," %%*lf (%%*lf)");
6030: }
6031: 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);
6032: for (i=1; i<= nlstate ; i ++) {
6033: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
6034: else fprintf(ficgp," %%*lf (%%*lf)");
6035: }
6036: 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));
6037: if(backcast==1){ /* We need to get the corresponding values of the covariates involved in this combination k1 */
6038: /* 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); */
6039: fprintf(ficgp,",\"%s\" u 1:((",subdirf2(fileresu,"PLB_")); /* Age is in 1 */
1.225 brouard 6040: if(cptcoveff ==0){
1.223 brouard 6041: fprintf(ficgp,"$%d)) t 'Backward prevalence in state %d' with line ", 2+(cpt-1), cpt );
6042: }else{
6043: kl=0;
1.225 brouard 6044: for (k=1; k<=cptcoveff; k++){ /* For each combination of covariate */
6045: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
1.223 brouard 6046: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6047: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6048: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6049: vlv= nbcode[Tvaraff[k]][lv];
6050: kl++;
6051: /* 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 *\/ */
6052: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6053: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6054: /* '' 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 6055: if(k==cptcoveff){
1.223 brouard 6056: 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], \
6057: 6+(cpt-1), cpt );
6058: }else{
6059: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl+1, Tvaraff[k],kl+1+1,nbcode[Tvaraff[k]][lv]);
6060: kl++;
6061: }
6062: } /* end covariate */
6063: } /* end if no covariate */
6064: } /* end if backcast */
6065: fprintf(ficgp,"\nset out \n");
1.201 brouard 6066: } /* k1 */
6067: } /* cpt */
1.126 brouard 6068: /*2 eme*/
6069: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6070:
1.223 brouard 6071: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
1.225 brouard 6072: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6073: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6074: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6075: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6076: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6077: vlv= nbcode[Tvaraff[k]][lv];
6078: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6079: }
6080: fprintf(ficgp,"\n#\n");
6081: if(invalidvarcomb[k1]){
6082: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6083: continue;
6084: }
1.219 brouard 6085:
1.223 brouard 6086: fprintf(ficgp,"\nset out \"%s_%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1);
6087: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
6088: if(vpopbased==0)
6089: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
6090: else
6091: fprintf(ficgp,"\nreplot ");
6092: for (i=1; i<= nlstate+1 ; i ++) {
6093: k=2*i;
6094: 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);
6095: for (j=1; j<= nlstate+1 ; j ++) {
6096: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6097: else fprintf(ficgp," %%*lf (%%*lf)");
6098: }
6099: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
6100: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
6101: 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);
6102: for (j=1; j<= nlstate+1 ; j ++) {
6103: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6104: else fprintf(ficgp," %%*lf (%%*lf)");
6105: }
6106: fprintf(ficgp,"\" t\"\" w l lt 0,");
6107: 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);
6108: for (j=1; j<= nlstate+1 ; j ++) {
6109: if (j==i) fprintf(ficgp," %%lf (%%lf)");
6110: else fprintf(ficgp," %%*lf (%%*lf)");
6111: }
6112: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
6113: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
6114: } /* state */
6115: } /* vpopbased */
6116: fprintf(ficgp,"\nset out;set out \"%s_%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1); /* Buggy gnuplot */
1.201 brouard 6117: } /* k1 */
1.219 brouard 6118:
6119:
1.126 brouard 6120: /*3eme*/
6121: for (k1=1; k1<= m ; k1 ++) {
1.220 brouard 6122:
1.126 brouard 6123: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.211 brouard 6124: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: cov=%d state=%d",k1, cpt);
1.225 brouard 6125: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6126: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6127: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6128: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6129: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6130: vlv= nbcode[Tvaraff[k]][lv];
6131: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6132: }
6133: fprintf(ficgp,"\n#\n");
1.223 brouard 6134: if(invalidvarcomb[k1]){
6135: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6136: continue;
6137: }
1.219 brouard 6138:
1.126 brouard 6139: /* k=2+nlstate*(2*cpt-2); */
6140: k=2+(nlstate+1)*(cpt-1);
1.201 brouard 6141: fprintf(ficgp,"\nset out \"%s_%d%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1);
1.199 brouard 6142: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 6143: 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 6144: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
1.223 brouard 6145: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6146: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
6147: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
6148: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
6149: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
1.219 brouard 6150:
1.126 brouard 6151: */
6152: for (i=1; i< nlstate ; i ++) {
1.223 brouard 6153: 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);
6154: /* 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 6155:
1.126 brouard 6156: }
1.201 brouard 6157: 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 6158: }
6159: }
6160:
1.223 brouard 6161: /* 4eme */
1.201 brouard 6162: /* Survival functions (period) from state i in state j by initial state i */
6163: for (k1=1; k1<= m ; k1 ++) { /* For each multivariate if any */
1.220 brouard 6164:
1.201 brouard 6165: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.211 brouard 6166: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
1.225 brouard 6167: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6168: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.223 brouard 6169: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6170: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6171: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6172: vlv= nbcode[Tvaraff[k]][lv];
6173: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6174: }
6175: fprintf(ficgp,"\n#\n");
1.223 brouard 6176: if(invalidvarcomb[k1]){
6177: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6178: continue;
6179: }
1.220 brouard 6180:
1.201 brouard 6181: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1);
6182: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.220 brouard 6183: set ter svg size 640, 480\n \
6184: unset log y\n \
1.201 brouard 6185: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6186: k=3;
1.201 brouard 6187: for (i=1; i<= nlstate ; i ++){
1.223 brouard 6188: if(i==1){
6189: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6190: }else{
6191: fprintf(ficgp,", '' ");
6192: }
6193: l=(nlstate+ndeath)*(i-1)+1;
6194: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6195: for (j=2; j<= nlstate+ndeath ; j ++)
6196: fprintf(ficgp,"+$%d",k+l+j-1);
6197: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
1.201 brouard 6198: } /* nlstate */
6199: fprintf(ficgp,"\nset out\n");
6200: } /* end cpt state*/
6201: } /* end covariate */
1.220 brouard 6202:
6203: /* 5eme */
1.201 brouard 6204: /* Survival functions (period) from state i in state j by final state j */
1.202 brouard 6205: for (k1=1; k1<= m ; k1 ++) { /* For each covariate if any */
1.201 brouard 6206: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
1.223 brouard 6207:
1.201 brouard 6208: 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 6209: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6210: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.220 brouard 6211: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6212: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6213: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6214: vlv= nbcode[Tvaraff[k]][lv];
6215: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6216: }
6217: fprintf(ficgp,"\n#\n");
1.223 brouard 6218: if(invalidvarcomb[k1]){
6219: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6220: continue;
6221: }
1.220 brouard 6222:
1.201 brouard 6223: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1);
6224: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
1.220 brouard 6225: set ter svg size 640, 480\n \
6226: unset log y\n \
1.201 brouard 6227: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6228: k=3;
1.201 brouard 6229: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.220 brouard 6230: if(j==1)
6231: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6232: else
6233: fprintf(ficgp,", '' ");
6234: l=(nlstate+ndeath)*(cpt-1) +j;
6235: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
6236: /* for (i=2; i<= nlstate+ndeath ; i ++) */
6237: /* fprintf(ficgp,"+$%d",k+l+i-1); */
6238: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
1.201 brouard 6239: } /* nlstate */
6240: fprintf(ficgp,", '' ");
6241: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
6242: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
1.220 brouard 6243: l=(nlstate+ndeath)*(cpt-1) +j;
6244: if(j < nlstate)
6245: fprintf(ficgp,"$%d +",k+l);
6246: else
6247: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
1.201 brouard 6248: }
6249: fprintf(ficgp,"\nset out\n");
6250: } /* end cpt state*/
6251: } /* end covariate */
1.220 brouard 6252:
6253: /* 6eme */
1.202 brouard 6254: /* CV preval stable (period) for each covariate */
1.211 brouard 6255: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.153 brouard 6256: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.223 brouard 6257:
1.211 brouard 6258: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6259: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6260: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.220 brouard 6261: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6262: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6263: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6264: vlv= nbcode[Tvaraff[k]][lv];
6265: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.211 brouard 6266: }
6267: fprintf(ficgp,"\n#\n");
1.223 brouard 6268: if(invalidvarcomb[k1]){
6269: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6270: continue;
6271: }
6272:
1.201 brouard 6273: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1);
1.126 brouard 6274: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.223 brouard 6275: set ter svg size 640, 480\n \
6276: unset log y\n \
1.153 brouard 6277: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 6278: k=3; /* Offset */
1.153 brouard 6279: for (i=1; i<= nlstate ; i ++){
1.220 brouard 6280: if(i==1)
6281: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
6282: else
6283: fprintf(ficgp,", '' ");
6284: l=(nlstate+ndeath)*(i-1)+1;
6285: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
6286: for (j=2; j<= nlstate ; j ++)
6287: fprintf(ficgp,"+$%d",k+l+j-1);
6288: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
1.153 brouard 6289: } /* nlstate */
1.201 brouard 6290: fprintf(ficgp,"\nset out\n");
1.153 brouard 6291: } /* end cpt state*/
6292: } /* end covariate */
1.223 brouard 6293:
6294:
1.220 brouard 6295: /* 7eme */
1.218 brouard 6296: if(backcast == 1){
1.217 brouard 6297: /* CV back preval stable (period) for each covariate */
1.218 brouard 6298: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
6299: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.220 brouard 6300: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6301: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
6302: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
1.220 brouard 6303: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6304: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
1.223 brouard 6305: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.220 brouard 6306: vlv= nbcode[Tvaraff[k]][lv];
6307: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
6308: }
6309: fprintf(ficgp,"\n#\n");
6310: if(invalidvarcomb[k1]){
1.223 brouard 6311: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6312: continue;
1.220 brouard 6313: }
6314:
6315: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1);
6316: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
6317: set ter svg size 640, 480\n \
6318: unset log y\n \
1.218 brouard 6319: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.220 brouard 6320: k=3; /* Offset */
6321: for (i=1; i<= nlstate ; i ++){
6322: if(i==1)
6323: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
6324: else
6325: fprintf(ficgp,", '' ");
6326: /* l=(nlstate+ndeath)*(i-1)+1; */
6327: l=(nlstate+ndeath)*(cpt-1)+1;
6328: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
6329: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
6330: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
6331: /* for (j=2; j<= nlstate ; j ++) */
6332: /* fprintf(ficgp,"+$%d",k+l+j-1); */
6333: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
6334: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
6335: } /* nlstate */
6336: fprintf(ficgp,"\nset out\n");
1.218 brouard 6337: } /* end cpt state*/
6338: } /* end covariate */
6339: } /* End if backcast */
6340:
1.223 brouard 6341: /* 8eme */
1.218 brouard 6342: if(prevfcast==1){
6343: /* Projection from cross-sectional to stable (period) for each covariate */
6344:
6345: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.211 brouard 6346: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.219 brouard 6347: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
1.225 brouard 6348: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
6349: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
1.219 brouard 6350: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6351: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6352: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6353: vlv= nbcode[Tvaraff[k]][lv];
1.220 brouard 6354: fprintf(ficgp," V%d=%d ",Tvaraff[k],vlv);
1.219 brouard 6355: }
6356: fprintf(ficgp,"\n#\n");
1.220 brouard 6357: if(invalidvarcomb[k1]){
1.223 brouard 6358: fprintf(ficgp,"#Combination (%d) ignored because no cases \n",k1);
6359: continue;
1.220 brouard 6360: }
1.219 brouard 6361:
6362: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
6363: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1);
6364: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
1.223 brouard 6365: set ter svg size 640, 480\n \
6366: unset log y\n \
1.219 brouard 6367: plot [%.f:%.f] ", ageminpar, agemaxpar);
6368: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
6369: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6370: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6371: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6372: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6373: if(i==1){
6374: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
6375: }else{
6376: fprintf(ficgp,",\\\n '' ");
6377: }
1.225 brouard 6378: if(cptcoveff ==0){ /* No covariate */
1.219 brouard 6379: ioffset=2; /* Age is in 2 */
6380: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6381: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6382: /*# V1 = 1 yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
6383: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
6384: fprintf(ficgp," u %d:(", ioffset);
6385: if(i==nlstate+1)
6386: fprintf(ficgp," $%d/(1.-$%d)) t 'pw.%d' with line ", \
6387: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6388: else
6389: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
6390: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6391: }else{ /* more than 2 covariates */
1.225 brouard 6392: if(cptcoveff ==1){
1.219 brouard 6393: ioffset=4; /* Age is in 4 */
6394: }else{
6395: ioffset=6; /* Age is in 6 */
1.223 brouard 6396: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
6397: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
1.219 brouard 6398: }
1.220 brouard 6399: fprintf(ficgp," u %d:(",ioffset);
1.219 brouard 6400: kl=0;
1.220 brouard 6401: strcpy(gplotcondition,"(");
1.225 brouard 6402: for (k=1; k<=cptcoveff; k++){ /* For each covariate writing the chain of conditions */
6403: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to combination k1 and covariate k */
1.219 brouard 6404: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6405: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6406: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
1.220 brouard 6407: vlv= nbcode[Tvaraff[k]][lv]; /* Value of the modality of Tvaraff[k] */
6408: kl++;
6409: sprintf(gplotcondition+strlen(gplotcondition),"$%d==%d && $%d==%d " ,kl,Tvaraff[k], kl+1, nbcode[Tvaraff[k]][lv]);
1.219 brouard 6410: kl++;
1.225 brouard 6411: if(k <cptcoveff && cptcoveff>1)
1.220 brouard 6412: sprintf(gplotcondition+strlen(gplotcondition)," && ");
6413: }
6414: strcpy(gplotcondition+strlen(gplotcondition),")");
6415: /* 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 *\/ */
6416: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6417: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6418: /* '' 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*/
6419: if(i==nlstate+1){
6420: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ", gplotcondition, \
6421: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset+1+(i-1)+(nlstate+1)*nlstate,cpt );
6422: }else{
1.223 brouard 6423: fprintf(ficgp,"%s ? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ", gplotcondition, \
6424: ioffset+(cpt-1)*(nlstate+1)+1+(i-1), ioffset +1+(i-1)+(nlstate+1)*nlstate,i,cpt );
1.220 brouard 6425: }
1.219 brouard 6426: } /* end if covariate */
6427: } /* nlstate */
6428: fprintf(ficgp,"\nset out\n");
1.223 brouard 6429: } /* end cpt state*/
6430: } /* end covariate */
6431: } /* End if prevfcast */
1.219 brouard 6432:
1.211 brouard 6433:
1.223 brouard 6434: /* proba elementaires */
6435: fprintf(ficgp,"\n##############\n#MLE estimated parameters\n#############\n");
1.126 brouard 6436: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6437: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6438: for(k=1; k <=(nlstate+ndeath); k++){
6439: if (k != i) {
1.223 brouard 6440: fprintf(ficgp,"# current state %d\n",k);
6441: for(j=1; j <=ncovmodel; j++){
6442: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
6443: jk++;
6444: }
6445: fprintf(ficgp,"\n");
1.126 brouard 6446: }
6447: }
1.223 brouard 6448: }
1.187 brouard 6449: fprintf(ficgp,"##############\n#\n");
1.223 brouard 6450:
1.145 brouard 6451: /*goto avoid;*/
1.200 brouard 6452: fprintf(ficgp,"\n##############\n#Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6453: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6454: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6455: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6456: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6457: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6458: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6459: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6460: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6461: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6462: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6463: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6464: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6465: fprintf(ficgp,"#\n");
1.223 brouard 6466: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
6467: fprintf(ficgp,"# ng=%d\n",ng);
1.225 brouard 6468: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);
1.223 brouard 6469: for(jk=1; jk <=m; jk++) {
6470: fprintf(ficgp,"# jk=%d\n",jk);
6471: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng);
6472: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6473: if (ng==1){
6474: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6475: fprintf(ficgp,"\nunset log y");
6476: }else if (ng==2){
6477: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6478: fprintf(ficgp,"\nset log y");
6479: }else if (ng==3){
6480: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
6481: fprintf(ficgp,"\nset log y");
6482: }else
6483: fprintf(ficgp,"\nunset title ");
6484: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
6485: i=1;
6486: for(k2=1; k2<=nlstate; k2++) {
6487: k3=i;
6488: for(k=1; k<=(nlstate+ndeath); k++) {
6489: if (k != k2){
6490: switch( ng) {
6491: case 1:
6492: if(nagesqr==0)
6493: fprintf(ficgp," p%d+p%d*x",i,i+1);
6494: else /* nagesqr =1 */
6495: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6496: break;
6497: case 2: /* ng=2 */
6498: if(nagesqr==0)
6499: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6500: else /* nagesqr =1 */
6501: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6502: break;
6503: case 3:
6504: if(nagesqr==0)
6505: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6506: else /* nagesqr =1 */
6507: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6508: break;
6509: }
6510: ij=1;/* To be checked else nbcode[0][0] wrong */
6511: for(j=3; j <=ncovmodel-nagesqr; j++) {
6512: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
6513: if(ij <=cptcovage) { /* Bug valgrind */
6514: if((j-2)==Tage[ij]) { /* Bug valgrind */
6515: fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6516: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6517: ij++;
6518: }
6519: }
6520: else
1.225 brouard 6521: fprintf(ficgp,"+p%d*%d",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]); /* Valgrind bug nbcode */
1.223 brouard 6522: }
6523: }else{
6524: i=i-ncovmodel;
6525: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
6526: fprintf(ficgp," (1.");
6527: }
1.217 brouard 6528:
1.223 brouard 6529: if(ng != 1){
6530: fprintf(ficgp,")/(1");
1.126 brouard 6531:
1.223 brouard 6532: for(k1=1; k1 <=nlstate; k1++){
6533: if(nagesqr==0)
6534: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
6535: else /* nagesqr =1 */
6536: 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 6537:
1.223 brouard 6538: ij=1;
6539: for(j=3; j <=ncovmodel-nagesqr; j++){
6540: if(ij <=cptcovage) { /* Bug valgrind */
6541: if((j-2)==Tage[ij]) { /* Bug valgrind */
6542: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6543: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6544: ij++;
6545: }
6546: }
6547: else
1.225 brouard 6548: 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 6549: }
6550: fprintf(ficgp,")");
6551: }
6552: fprintf(ficgp,")");
6553: if(ng ==2)
6554: fprintf(ficgp," t \"p%d%d\" ", k2,k);
6555: else /* ng= 3 */
6556: fprintf(ficgp," t \"i%d%d\" ", k2,k);
6557: }else{ /* end ng <> 1 */
6558: if( k !=k2) /* logit p11 is hard to draw */
6559: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
6560: }
6561: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
6562: fprintf(ficgp,",");
6563: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
6564: fprintf(ficgp,",");
6565: i=i+ncovmodel;
6566: } /* end k */
6567: } /* end k2 */
6568: fprintf(ficgp,"\n set out\n");
6569: } /* end jk */
6570: } /* end ng */
6571: /* avoid: */
6572: fflush(ficgp);
1.126 brouard 6573: } /* end gnuplot */
6574:
6575:
6576: /*************** Moving average **************/
1.219 brouard 6577: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
1.222 brouard 6578: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
1.218 brouard 6579:
1.222 brouard 6580: int i, cpt, cptcod;
6581: int modcovmax =1;
6582: int mobilavrange, mob;
6583: int iage=0;
6584:
6585: double sum=0.;
6586: double age;
6587: double *sumnewp, *sumnewm;
6588: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
6589:
6590:
1.225 brouard 6591: /* modcovmax=2*cptcoveff;/\* Max number of modalities. We suppose */
1.222 brouard 6592: /* a covariate has 2 modalities, should be equal to ncovcombmax *\/ */
6593:
6594: sumnewp = vector(1,ncovcombmax);
6595: sumnewm = vector(1,ncovcombmax);
6596: agemingood = vector(1,ncovcombmax);
6597: agemaxgood = vector(1,ncovcombmax);
6598:
6599: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6600: sumnewm[cptcod]=0.;
6601: sumnewp[cptcod]=0.;
6602: agemingood[cptcod]=0;
6603: agemaxgood[cptcod]=0;
6604: }
6605: if (cptcovn<1) ncovcombmax=1; /* At least 1 pass */
6606:
6607: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
6608: if(mobilav==1) mobilavrange=5; /* default */
6609: else mobilavrange=mobilav;
6610: for (age=bage; age<=fage; age++)
6611: for (i=1; i<=nlstate;i++)
6612: for (cptcod=1;cptcod<=ncovcombmax;cptcod++)
6613: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
6614: /* We keep the original values on the extreme ages bage, fage and for
6615: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
6616: we use a 5 terms etc. until the borders are no more concerned.
6617: */
6618: for (mob=3;mob <=mobilavrange;mob=mob+2){
6619: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
6620: for (i=1; i<=nlstate;i++){
6621: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6622: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
6623: for (cpt=1;cpt<=(mob-1)/2;cpt++){
6624: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
6625: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
6626: }
6627: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
6628: }
6629: }
6630: }/* end age */
6631: }/* end mob */
6632: }else
6633: return -1;
6634: for (cptcod=1;cptcod<=ncovcombmax;cptcod++){
6635: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
6636: if(invalidvarcomb[cptcod]){
6637: printf("\nCombination (%d) ignored because no cases \n",cptcod);
6638: continue;
6639: }
1.219 brouard 6640:
1.222 brouard 6641: agemingood[cptcod]=fage-(mob-1)/2;
6642: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
6643: sumnewm[cptcod]=0.;
6644: for (i=1; i<=nlstate;i++){
6645: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6646: }
6647: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6648: agemingood[cptcod]=age;
6649: }else{ /* bad */
6650: for (i=1; i<=nlstate;i++){
6651: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6652: } /* i */
6653: } /* end bad */
6654: }/* age */
6655: sum=0.;
6656: for (i=1; i<=nlstate;i++){
6657: sum+=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6658: }
6659: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6660: 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);
6661: /* for (i=1; i<=nlstate;i++){ */
6662: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6663: /* } /\* i *\/ */
6664: } /* end bad */
6665: /* else{ /\* We found some ages summing to one, we will smooth the oldest *\/ */
6666: /* From youngest, finding the oldest wrong */
6667: agemaxgood[cptcod]=bage+(mob-1)/2;
6668: for (age=bage+(mob-1)/2; age<=fage; age++){
6669: sumnewm[cptcod]=0.;
6670: for (i=1; i<=nlstate;i++){
6671: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6672: }
6673: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
6674: agemaxgood[cptcod]=age;
6675: }else{ /* bad */
6676: for (i=1; i<=nlstate;i++){
6677: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6678: } /* i */
6679: } /* end bad */
6680: }/* age */
6681: sum=0.;
6682: for (i=1; i<=nlstate;i++){
6683: sum+=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6684: }
6685: if(fabs(sum - 1.) > 1.e-3) { /* bad */
6686: 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);
6687: /* for (i=1; i<=nlstate;i++){ */
6688: /* mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod]; */
6689: /* } /\* i *\/ */
6690: } /* end bad */
6691:
6692: for (age=bage; age<=fage; age++){
6693: printf("%d %d ", cptcod, (int)age);
6694: sumnewp[cptcod]=0.;
6695: sumnewm[cptcod]=0.;
6696: for (i=1; i<=nlstate;i++){
6697: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
6698: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
6699: /* printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]); */
6700: }
6701: /* printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]); */
6702: }
6703: /* printf("\n"); */
6704: /* } */
6705: /* brutal averaging */
6706: for (i=1; i<=nlstate;i++){
6707: for (age=1; age<=bage; age++){
6708: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
6709: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6710: }
6711: for (age=fage; age<=AGESUP; age++){
6712: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
6713: /* printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]); */
6714: }
6715: } /* end i status */
6716: for (i=nlstate+1; i<=nlstate+ndeath;i++){
6717: for (age=1; age<=AGESUP; age++){
6718: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
6719: mobaverage[(int)age][i][cptcod]=0.;
6720: }
6721: }
6722: }/* end cptcod */
6723: free_vector(sumnewm,1, ncovcombmax);
6724: free_vector(sumnewp,1, ncovcombmax);
6725: free_vector(agemaxgood,1, ncovcombmax);
6726: free_vector(agemingood,1, ncovcombmax);
6727: return 0;
6728: }/* End movingaverage */
1.218 brouard 6729:
1.126 brouard 6730:
6731: /************** Forecasting ******************/
1.225 brouard 6732: 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 6733: /* proj1, year, month, day of starting projection
6734: agemin, agemax range of age
6735: dateprev1 dateprev2 range of dates during which prevalence is computed
6736: anproj2 year of en of projection (same day and month as proj1).
6737: */
1.164 brouard 6738: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1;
1.126 brouard 6739: double agec; /* generic age */
6740: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
6741: double *popeffectif,*popcount;
6742: double ***p3mat;
1.218 brouard 6743: /* double ***mobaverage; */
1.126 brouard 6744: char fileresf[FILENAMELENGTH];
6745:
6746: agelim=AGESUP;
1.211 brouard 6747: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
6748: in each health status at the date of interview (if between dateprev1 and dateprev2).
6749: We still use firstpass and lastpass as another selection.
6750: */
1.214 brouard 6751: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
6752: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 6753:
1.201 brouard 6754: strcpy(fileresf,"F_");
6755: strcat(fileresf,fileresu);
1.126 brouard 6756: if((ficresf=fopen(fileresf,"w"))==NULL) {
6757: printf("Problem with forecast resultfile: %s\n", fileresf);
6758: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
6759: }
1.215 brouard 6760: printf("Computing forecasting: result on file '%s', please wait... \n", fileresf);
6761: fprintf(ficlog,"Computing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 6762:
1.225 brouard 6763: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 6764:
6765:
6766: stepsize=(int) (stepm+YEARM-1)/YEARM;
6767: if (stepm<=12) stepsize=1;
6768: if(estepm < stepm){
6769: printf ("Problem %d lower than %d\n",estepm, stepm);
6770: }
6771: else hstepm=estepm;
6772:
6773: hstepm=hstepm/stepm;
6774: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
6775: fractional in yp1 */
6776: anprojmean=yp;
6777: yp2=modf((yp1*12),&yp);
6778: mprojmean=yp;
6779: yp1=modf((yp2*30.5),&yp);
6780: jprojmean=yp;
6781: if(jprojmean==0) jprojmean=1;
6782: if(mprojmean==0) jprojmean=1;
6783:
1.225 brouard 6784: i1=cptcoveff;
1.126 brouard 6785: if (cptcovn < 1){i1=1;}
6786:
6787: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
6788:
6789: fprintf(ficresf,"#****** Routine prevforecast **\n");
6790:
6791: /* if (h==(int)(YEARM*yearp)){ */
6792: for(cptcov=1, k=0;cptcov<=i1;cptcov++){
1.225 brouard 6793: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
1.126 brouard 6794: k=k+1;
1.211 brouard 6795: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
1.225 brouard 6796: for(j=1;j<=cptcoveff;j++) {
1.219 brouard 6797: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.126 brouard 6798: }
1.211 brouard 6799: fprintf(ficresf," yearproj age");
1.126 brouard 6800: for(j=1; j<=nlstate+ndeath;j++){
1.219 brouard 6801: for(i=1; i<=nlstate;i++)
1.126 brouard 6802: fprintf(ficresf," p%d%d",i,j);
1.219 brouard 6803: fprintf(ficresf," wp.%d",j);
1.126 brouard 6804: }
1.217 brouard 6805: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
1.219 brouard 6806: fprintf(ficresf,"\n");
6807: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
6808: for (agec=fage; agec>=(ageminpar-1); agec--){
6809: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
6810: nhstepm = nhstepm/hstepm;
6811: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6812: oldm=oldms;savm=savms;
6813: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
6814:
6815: for (h=0; h<=nhstepm; h++){
6816: if (h*hstepm/YEARM*stepm ==yearp) {
1.126 brouard 6817: fprintf(ficresf,"\n");
1.225 brouard 6818: for(j=1;j<=cptcoveff;j++)
1.198 brouard 6819: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 6820: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
6821: }
6822: for(j=1; j<=nlstate+ndeath;j++) {
6823: ppij=0.;
6824: for(i=1; i<=nlstate;i++) {
6825: if (mobilav==1)
6826: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
6827: else {
6828: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
6829: }
6830: if (h*hstepm/YEARM*stepm== yearp) {
6831: fprintf(ficresf," %.3f", p3mat[i][j][h]);
6832: }
6833: } /* end i */
6834: if (h*hstepm/YEARM*stepm==yearp) {
6835: fprintf(ficresf," %.3f", ppij);
6836: }
6837: }/* end j */
6838: } /* end h */
6839: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6840: } /* end agec */
1.126 brouard 6841: } /* end yearp */
6842: } /* end cptcod */
6843: } /* end cptcov */
1.219 brouard 6844:
1.126 brouard 6845: fclose(ficresf);
1.215 brouard 6846: printf("End of Computing forecasting \n");
6847: fprintf(ficlog,"End of Computing forecasting\n");
6848:
1.126 brouard 6849: }
6850:
1.218 brouard 6851: /* /\************** Back Forecasting ******************\/ */
1.225 brouard 6852: /* 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 6853: /* /\* back1, year, month, day of starting backection */
6854: /* agemin, agemax range of age */
6855: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
6856: /* anback2 year of en of backection (same day and month as back1). */
6857: /* *\/ */
6858: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
6859: /* double agec; /\* generic age *\/ */
6860: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
6861: /* double *popeffectif,*popcount; */
6862: /* double ***p3mat; */
6863: /* /\* double ***mobaverage; *\/ */
6864: /* char fileresfb[FILENAMELENGTH]; */
6865:
6866: /* agelim=AGESUP; */
6867: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
6868: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
6869: /* We still use firstpass and lastpass as another selection. */
6870: /* *\/ */
6871: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
6872: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
6873: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
6874:
6875: /* strcpy(fileresfb,"FB_"); */
6876: /* strcat(fileresfb,fileresu); */
6877: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
6878: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
6879: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
6880: /* } */
6881: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
6882: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
6883:
1.225 brouard 6884: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
1.218 brouard 6885:
6886: /* /\* if (mobilav!=0) { *\/ */
6887: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
6888: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
6889: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
6890: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
6891: /* /\* } *\/ */
6892: /* /\* } *\/ */
6893:
6894: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
6895: /* if (stepm<=12) stepsize=1; */
6896: /* if(estepm < stepm){ */
6897: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
6898: /* } */
6899: /* else hstepm=estepm; */
6900:
6901: /* hstepm=hstepm/stepm; */
6902: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
6903: /* fractional in yp1 *\/ */
6904: /* anprojmean=yp; */
6905: /* yp2=modf((yp1*12),&yp); */
6906: /* mprojmean=yp; */
6907: /* yp1=modf((yp2*30.5),&yp); */
6908: /* jprojmean=yp; */
6909: /* if(jprojmean==0) jprojmean=1; */
6910: /* if(mprojmean==0) jprojmean=1; */
6911:
1.225 brouard 6912: /* i1=cptcoveff; */
1.218 brouard 6913: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 6914:
1.218 brouard 6915: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 6916:
1.218 brouard 6917: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
6918:
6919: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
6920: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
1.225 brouard 6921: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
1.218 brouard 6922: /* k=k+1; */
6923: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
1.225 brouard 6924: /* for(j=1;j<=cptcoveff;j++) { */
1.218 brouard 6925: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
6926: /* } */
6927: /* fprintf(ficresfb," yearbproj age"); */
6928: /* for(j=1; j<=nlstate+ndeath;j++){ */
6929: /* for(i=1; i<=nlstate;i++) */
6930: /* fprintf(ficresfb," p%d%d",i,j); */
6931: /* fprintf(ficresfb," p.%d",j); */
6932: /* } */
6933: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
6934: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
6935: /* fprintf(ficresfb,"\n"); */
6936: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
6937: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
6938: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
6939: /* nhstepm = nhstepm/hstepm; */
6940: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
6941: /* oldm=oldms;savm=savms; */
6942: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
6943: /* for (h=0; h<=nhstepm; h++){ */
6944: /* if (h*hstepm/YEARM*stepm ==yearp) { */
6945: /* fprintf(ficresfb,"\n"); */
1.225 brouard 6946: /* for(j=1;j<=cptcoveff;j++) */
1.218 brouard 6947: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
6948: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
6949: /* } */
6950: /* for(j=1; j<=nlstate+ndeath;j++) { */
6951: /* ppij=0.; */
6952: /* for(i=1; i<=nlstate;i++) { */
6953: /* if (mobilav==1) */
6954: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
6955: /* else { */
6956: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
6957: /* } */
6958: /* if (h*hstepm/YEARM*stepm== yearp) { */
6959: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
6960: /* } */
6961: /* } /\* end i *\/ */
6962: /* if (h*hstepm/YEARM*stepm==yearp) { */
6963: /* fprintf(ficresfb," %.3f", ppij); */
6964: /* } */
6965: /* }/\* end j *\/ */
6966: /* } /\* end h *\/ */
6967: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
6968: /* } /\* end agec *\/ */
6969: /* } /\* end yearp *\/ */
6970: /* } /\* end cptcod *\/ */
6971: /* } /\* end cptcov *\/ */
6972:
6973: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
6974:
6975: /* fclose(ficresfb); */
6976: /* printf("End of Computing Back forecasting \n"); */
6977: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 6978:
1.218 brouard 6979: /* } */
1.217 brouard 6980:
1.126 brouard 6981: /************** Forecasting *****not tested NB*************/
1.169 brouard 6982: 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 6983:
6984: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
6985: int *popage;
6986: double calagedatem, agelim, kk1, kk2;
6987: double *popeffectif,*popcount;
6988: double ***p3mat,***tabpop,***tabpopprev;
1.218 brouard 6989: /* double ***mobaverage; */
1.126 brouard 6990: char filerespop[FILENAMELENGTH];
6991:
6992: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6993: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6994: agelim=AGESUP;
6995: calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
6996:
6997: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
6998:
6999:
1.201 brouard 7000: strcpy(filerespop,"POP_");
7001: strcat(filerespop,fileresu);
1.126 brouard 7002: if((ficrespop=fopen(filerespop,"w"))==NULL) {
7003: printf("Problem with forecast resultfile: %s\n", filerespop);
7004: fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
7005: }
7006: printf("Computing forecasting: result on file '%s' \n", filerespop);
7007: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
7008:
1.225 brouard 7009: if (cptcoveff==0) ncodemax[cptcoveff]=1;
1.126 brouard 7010:
1.218 brouard 7011: /* if (mobilav!=0) { */
7012: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7013: /* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ */
7014: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
7015: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
7016: /* } */
7017: /* } */
1.126 brouard 7018:
7019: stepsize=(int) (stepm+YEARM-1)/YEARM;
7020: if (stepm<=12) stepsize=1;
7021:
7022: agelim=AGESUP;
7023:
7024: hstepm=1;
7025: hstepm=hstepm/stepm;
1.218 brouard 7026:
1.126 brouard 7027: if (popforecast==1) {
7028: if((ficpop=fopen(popfile,"r"))==NULL) {
7029: printf("Problem with population file : %s\n",popfile);exit(0);
7030: fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
7031: }
7032: popage=ivector(0,AGESUP);
7033: popeffectif=vector(0,AGESUP);
7034: popcount=vector(0,AGESUP);
7035:
7036: i=1;
7037: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
1.218 brouard 7038:
1.126 brouard 7039: imx=i;
7040: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
7041: }
1.218 brouard 7042:
1.126 brouard 7043: for(cptcov=1,k=0;cptcov<=i2;cptcov++){
1.225 brouard 7044: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
1.126 brouard 7045: k=k+1;
7046: fprintf(ficrespop,"\n#******");
1.225 brouard 7047: for(j=1;j<=cptcoveff;j++) {
1.198 brouard 7048: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.126 brouard 7049: }
7050: fprintf(ficrespop,"******\n");
7051: fprintf(ficrespop,"# Age");
7052: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
7053: if (popforecast==1) fprintf(ficrespop," [Population]");
7054:
7055: for (cpt=0; cpt<=0;cpt++) {
7056: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
7057:
1.218 brouard 7058: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
1.126 brouard 7059: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
7060: nhstepm = nhstepm/hstepm;
7061:
7062: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7063: oldm=oldms;savm=savms;
7064: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 brouard 7065:
1.126 brouard 7066: for (h=0; h<=nhstepm; h++){
7067: if (h==(int) (calagedatem+YEARM*cpt)) {
7068: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
7069: }
7070: for(j=1; j<=nlstate+ndeath;j++) {
7071: kk1=0.;kk2=0;
7072: for(i=1; i<=nlstate;i++) {
7073: if (mobilav==1)
7074: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
7075: else {
7076: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
7077: }
7078: }
7079: if (h==(int)(calagedatem+12*cpt)){
7080: tabpop[(int)(agedeb)][j][cptcod]=kk1;
1.218 brouard 7081: /*fprintf(ficrespop," %.3f", kk1);
7082: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
1.126 brouard 7083: }
7084: }
7085: for(i=1; i<=nlstate;i++){
7086: kk1=0.;
1.218 brouard 7087: for(j=1; j<=nlstate;j++){
7088: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
7089: }
7090: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)];
1.126 brouard 7091: }
1.218 brouard 7092:
7093: if (h==(int)(calagedatem+12*cpt))
7094: for(j=1; j<=nlstate;j++)
7095: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
1.126 brouard 7096: }
7097: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7098: }
7099: }
1.218 brouard 7100:
7101: /******/
7102:
1.126 brouard 7103: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
7104: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
7105: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
7106: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
7107: nhstepm = nhstepm/hstepm;
7108:
7109: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7110: oldm=oldms;savm=savms;
7111: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
7112: for (h=0; h<=nhstepm; h++){
7113: if (h==(int) (calagedatem+YEARM*cpt)) {
7114: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
7115: }
7116: for(j=1; j<=nlstate+ndeath;j++) {
7117: kk1=0.;kk2=0;
7118: for(i=1; i<=nlstate;i++) {
7119: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
7120: }
7121: if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1);
7122: }
7123: }
7124: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7125: }
7126: }
1.218 brouard 7127: }
1.126 brouard 7128: }
1.218 brouard 7129:
7130: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
7131:
1.126 brouard 7132: if (popforecast==1) {
7133: free_ivector(popage,0,AGESUP);
7134: free_vector(popeffectif,0,AGESUP);
7135: free_vector(popcount,0,AGESUP);
7136: }
7137: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
7138: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
7139: fclose(ficrespop);
7140: } /* End of popforecast */
1.218 brouard 7141:
1.126 brouard 7142: int fileappend(FILE *fichier, char *optionfich)
7143: {
7144: if((fichier=fopen(optionfich,"a"))==NULL) {
7145: printf("Problem with file: %s\n", optionfich);
7146: fprintf(ficlog,"Problem with file: %s\n", optionfich);
7147: return (0);
7148: }
7149: fflush(fichier);
7150: return (1);
7151: }
7152:
7153:
7154: /**************** function prwizard **********************/
7155: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
7156: {
7157:
7158: /* Wizard to print covariance matrix template */
7159:
1.164 brouard 7160: char ca[32], cb[32];
7161: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 7162: int numlinepar;
7163:
7164: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7165: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
7166: for(i=1; i <=nlstate; i++){
7167: jj=0;
7168: for(j=1; j <=nlstate+ndeath; j++){
7169: if(j==i) continue;
7170: jj++;
7171: /*ca[0]= k+'a'-1;ca[1]='\0';*/
7172: printf("%1d%1d",i,j);
7173: fprintf(ficparo,"%1d%1d",i,j);
7174: for(k=1; k<=ncovmodel;k++){
7175: /* printf(" %lf",param[i][j][k]); */
7176: /* fprintf(ficparo," %lf",param[i][j][k]); */
7177: printf(" 0.");
7178: fprintf(ficparo," 0.");
7179: }
7180: printf("\n");
7181: fprintf(ficparo,"\n");
7182: }
7183: }
7184: printf("# Scales (for hessian or gradient estimation)\n");
7185: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
7186: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
7187: for(i=1; i <=nlstate; i++){
7188: jj=0;
7189: for(j=1; j <=nlstate+ndeath; j++){
7190: if(j==i) continue;
7191: jj++;
7192: fprintf(ficparo,"%1d%1d",i,j);
7193: printf("%1d%1d",i,j);
7194: fflush(stdout);
7195: for(k=1; k<=ncovmodel;k++){
7196: /* printf(" %le",delti3[i][j][k]); */
7197: /* fprintf(ficparo," %le",delti3[i][j][k]); */
7198: printf(" 0.");
7199: fprintf(ficparo," 0.");
7200: }
7201: numlinepar++;
7202: printf("\n");
7203: fprintf(ficparo,"\n");
7204: }
7205: }
7206: printf("# Covariance matrix\n");
7207: /* # 121 Var(a12)\n\ */
7208: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7209: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
7210: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
7211: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
7212: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
7213: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
7214: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7215: fflush(stdout);
7216: fprintf(ficparo,"# Covariance matrix\n");
7217: /* # 121 Var(a12)\n\ */
7218: /* # 122 Cov(b12,a12) Var(b12)\n\ */
7219: /* # ...\n\ */
7220: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
7221:
7222: for(itimes=1;itimes<=2;itimes++){
7223: jj=0;
7224: for(i=1; i <=nlstate; i++){
7225: for(j=1; j <=nlstate+ndeath; j++){
7226: if(j==i) continue;
7227: for(k=1; k<=ncovmodel;k++){
7228: jj++;
7229: ca[0]= k+'a'-1;ca[1]='\0';
7230: if(itimes==1){
7231: printf("#%1d%1d%d",i,j,k);
7232: fprintf(ficparo,"#%1d%1d%d",i,j,k);
7233: }else{
7234: printf("%1d%1d%d",i,j,k);
7235: fprintf(ficparo,"%1d%1d%d",i,j,k);
7236: /* printf(" %.5le",matcov[i][j]); */
7237: }
7238: ll=0;
7239: for(li=1;li <=nlstate; li++){
7240: for(lj=1;lj <=nlstate+ndeath; lj++){
7241: if(lj==li) continue;
7242: for(lk=1;lk<=ncovmodel;lk++){
7243: ll++;
7244: if(ll<=jj){
7245: cb[0]= lk +'a'-1;cb[1]='\0';
7246: if(ll<jj){
7247: if(itimes==1){
7248: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7249: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
7250: }else{
7251: printf(" 0.");
7252: fprintf(ficparo," 0.");
7253: }
7254: }else{
7255: if(itimes==1){
7256: printf(" Var(%s%1d%1d)",ca,i,j);
7257: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
7258: }else{
7259: printf(" 0.");
7260: fprintf(ficparo," 0.");
7261: }
7262: }
7263: }
7264: } /* end lk */
7265: } /* end lj */
7266: } /* end li */
7267: printf("\n");
7268: fprintf(ficparo,"\n");
7269: numlinepar++;
7270: } /* end k*/
7271: } /*end j */
7272: } /* end i */
7273: } /* end itimes */
7274:
7275: } /* end of prwizard */
7276: /******************* Gompertz Likelihood ******************************/
7277: double gompertz(double x[])
7278: {
7279: double A,B,L=0.0,sump=0.,num=0.;
7280: int i,n=0; /* n is the size of the sample */
7281:
1.220 brouard 7282: for (i=1;i<=imx ; i++) {
1.126 brouard 7283: sump=sump+weight[i];
7284: /* sump=sump+1;*/
7285: num=num+1;
7286: }
7287:
7288:
7289: /* for (i=0; i<=imx; i++)
7290: 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]);*/
7291:
7292: for (i=1;i<=imx ; i++)
7293: {
7294: if (cens[i] == 1 && wav[i]>1)
7295: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
7296:
7297: if (cens[i] == 0 && wav[i]>1)
7298: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
7299: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
7300:
7301: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7302: if (wav[i] > 1 ) { /* ??? */
7303: L=L+A*weight[i];
7304: /* 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]);*/
7305: }
7306: }
7307:
7308: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7309:
7310: return -2*L*num/sump;
7311: }
7312:
1.136 brouard 7313: #ifdef GSL
7314: /******************* Gompertz_f Likelihood ******************************/
7315: double gompertz_f(const gsl_vector *v, void *params)
7316: {
7317: double A,B,LL=0.0,sump=0.,num=0.;
7318: double *x= (double *) v->data;
7319: int i,n=0; /* n is the size of the sample */
7320:
7321: for (i=0;i<=imx-1 ; i++) {
7322: sump=sump+weight[i];
7323: /* sump=sump+1;*/
7324: num=num+1;
7325: }
7326:
7327:
7328: /* for (i=0; i<=imx; i++)
7329: 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]);*/
7330: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
7331: for (i=1;i<=imx ; i++)
7332: {
7333: if (cens[i] == 1 && wav[i]>1)
7334: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
7335:
7336: if (cens[i] == 0 && wav[i]>1)
7337: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
7338: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
7339:
7340: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
7341: if (wav[i] > 1 ) { /* ??? */
7342: LL=LL+A*weight[i];
7343: /* 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]);*/
7344: }
7345: }
7346:
7347: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
7348: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
7349:
7350: return -2*LL*num/sump;
7351: }
7352: #endif
7353:
1.126 brouard 7354: /******************* Printing html file ***********/
1.201 brouard 7355: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 7356: int lastpass, int stepm, int weightopt, char model[],\
7357: int imx, double p[],double **matcov,double agemortsup){
7358: int i,k;
7359:
7360: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
7361: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
7362: for (i=1;i<=2;i++)
7363: 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 7364: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 7365: fprintf(fichtm,"</ul>");
7366:
7367: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
7368:
7369: 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>");
7370:
7371: for (k=agegomp;k<(agemortsup-2);k++)
7372: 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]);
7373:
7374:
7375: fflush(fichtm);
7376: }
7377:
7378: /******************* Gnuplot file **************/
1.201 brouard 7379: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 7380:
7381: char dirfileres[132],optfileres[132];
1.164 brouard 7382:
1.126 brouard 7383: int ng;
7384:
7385:
7386: /*#ifdef windows */
7387: fprintf(ficgp,"cd \"%s\" \n",pathc);
7388: /*#endif */
7389:
7390:
7391: strcpy(dirfileres,optionfilefiname);
7392: strcpy(optfileres,"vpl");
1.199 brouard 7393: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 7394: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 7395: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 7396: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 7397: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
7398:
7399: }
7400:
1.136 brouard 7401: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
7402: {
1.126 brouard 7403:
1.136 brouard 7404: /*-------- data file ----------*/
7405: FILE *fic;
7406: char dummy[]=" ";
1.223 brouard 7407: int i=0, j=0, n=0, iv=0;
7408: int lstra;
1.136 brouard 7409: int linei, month, year,iout;
7410: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 7411: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 7412: char *stratrunc;
1.223 brouard 7413:
1.126 brouard 7414:
7415:
1.136 brouard 7416: if((fic=fopen(datafile,"r"))==NULL) {
1.218 brouard 7417: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
7418: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 7419: }
1.126 brouard 7420:
1.136 brouard 7421: i=1;
7422: linei=0;
7423: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
7424: linei=linei+1;
7425: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
7426: if(line[j] == '\t')
7427: line[j] = ' ';
7428: }
7429: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
7430: ;
7431: };
7432: line[j+1]=0; /* Trims blanks at end of line */
7433: if(line[0]=='#'){
7434: fprintf(ficlog,"Comment line\n%s\n",line);
7435: printf("Comment line\n%s\n",line);
7436: continue;
7437: }
7438: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 7439: strcpy(line, linetmp);
1.223 brouard 7440:
7441: /* Loops on waves */
7442: for (j=maxwav;j>=1;j--){
7443: for (iv=nqtv;iv>=1;iv--){ /* Loop on time varying quantitative variables */
1.225 brouard 7444: cutv(stra, strb, line, ' ');
7445: if(strb[0]=='.') { /* Missing value */
7446: lval=-1;
7447: cotqvar[j][iv][i]=-1; /* 0.0/0.0 */
7448: if(isalpha(strb[1])) { /* .m or .d Really Missing value */
7449: 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);
7450: 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);
7451: return 1;
7452: }
7453: }else{
7454: errno=0;
7455: /* what_kind_of_number(strb); */
7456: dval=strtod(strb,&endptr);
7457: /* if( strb[0]=='\0' || (*endptr != '\0')){ */
7458: /* if(strb != endptr && *endptr == '\0') */
7459: /* dval=dlval; */
7460: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7461: if( strb[0]=='\0' || (*endptr != '\0')){
7462: 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);
7463: 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);
7464: return 1;
7465: }
7466: cotqvar[j][iv][i]=dval;
7467: }
7468: strcpy(line,stra);
1.223 brouard 7469: }/* end loop ntqv */
1.225 brouard 7470:
1.223 brouard 7471: for (iv=ntv;iv>=1;iv--){ /* Loop on time varying dummies */
1.225 brouard 7472: cutv(stra, strb, line, ' ');
7473: if(strb[0]=='.') { /* Missing value */
7474: lval=-1;
7475: }else{
7476: errno=0;
7477: lval=strtol(strb,&endptr,10);
7478: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7479: if( strb[0]=='\0' || (*endptr != '\0')){
7480: 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);
7481: 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);
7482: return 1;
7483: }
7484: }
7485: if(lval <-1 || lval >1){
7486: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7487: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7488: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7489: For example, for multinomial values like 1, 2 and 3,\n \
7490: build V1=0 V2=0 for the reference value (1),\n \
7491: V1=1 V2=0 for (2) \n \
1.223 brouard 7492: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7493: output of IMaCh is often meaningless.\n \
1.223 brouard 7494: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 7495: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.223 brouard 7496: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7497: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7498: For example, for multinomial values like 1, 2 and 3,\n \
7499: build V1=0 V2=0 for the reference value (1),\n \
7500: V1=1 V2=0 for (2) \n \
1.223 brouard 7501: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
7502: output of IMaCh is often meaningless.\n \
7503: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 7504: return 1;
7505: }
7506: cotvar[j][iv][i]=(double)(lval);
7507: strcpy(line,stra);
1.223 brouard 7508: }/* end loop ntv */
1.225 brouard 7509:
1.223 brouard 7510: /* Statuses at wave */
1.137 brouard 7511: cutv(stra, strb, line, ' ');
1.223 brouard 7512: if(strb[0]=='.') { /* Missing value */
1.225 brouard 7513: lval=-1;
1.136 brouard 7514: }else{
1.225 brouard 7515: errno=0;
7516: lval=strtol(strb,&endptr,10);
7517: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7518: if( strb[0]=='\0' || (*endptr != '\0')){
7519: 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);
7520: 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);
7521: return 1;
7522: }
1.136 brouard 7523: }
1.225 brouard 7524:
1.136 brouard 7525: s[j][i]=lval;
1.225 brouard 7526:
1.223 brouard 7527: /* Date of Interview */
1.136 brouard 7528: strcpy(line,stra);
7529: cutv(stra, strb,line,' ');
1.169 brouard 7530: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7531: }
1.169 brouard 7532: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.225 brouard 7533: month=99;
7534: year=9999;
1.136 brouard 7535: }else{
1.225 brouard 7536: 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);
7537: 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);
7538: return 1;
1.136 brouard 7539: }
7540: anint[j][i]= (double) year;
7541: mint[j][i]= (double)month;
7542: strcpy(line,stra);
1.223 brouard 7543: } /* End loop on waves */
1.225 brouard 7544:
1.223 brouard 7545: /* Date of death */
1.136 brouard 7546: cutv(stra, strb,line,' ');
1.169 brouard 7547: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7548: }
1.169 brouard 7549: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7550: month=99;
7551: year=9999;
7552: }else{
1.141 brouard 7553: 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 7554: 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);
7555: return 1;
1.136 brouard 7556: }
7557: andc[i]=(double) year;
7558: moisdc[i]=(double) month;
7559: strcpy(line,stra);
7560:
1.223 brouard 7561: /* Date of birth */
1.136 brouard 7562: cutv(stra, strb,line,' ');
1.169 brouard 7563: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7564: }
1.169 brouard 7565: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 7566: month=99;
7567: year=9999;
7568: }else{
1.141 brouard 7569: 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);
7570: 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 7571: return 1;
1.136 brouard 7572: }
7573: if (year==9999) {
1.141 brouard 7574: 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);
7575: 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 7576: return 1;
7577:
1.136 brouard 7578: }
7579: annais[i]=(double)(year);
7580: moisnais[i]=(double)(month);
7581: strcpy(line,stra);
1.225 brouard 7582:
1.223 brouard 7583: /* Sample weight */
1.136 brouard 7584: cutv(stra, strb,line,' ');
7585: errno=0;
7586: dval=strtod(strb,&endptr);
7587: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7588: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
7589: 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 7590: fflush(ficlog);
7591: return 1;
7592: }
7593: weight[i]=dval;
7594: strcpy(line,stra);
1.225 brouard 7595:
1.223 brouard 7596: for (iv=nqv;iv>=1;iv--){ /* Loop on fixed quantitative variables */
7597: cutv(stra, strb, line, ' ');
7598: if(strb[0]=='.') { /* Missing value */
1.225 brouard 7599: lval=-1;
1.223 brouard 7600: }else{
1.225 brouard 7601: errno=0;
7602: /* what_kind_of_number(strb); */
7603: dval=strtod(strb,&endptr);
7604: /* if(strb != endptr && *endptr == '\0') */
7605: /* dval=dlval; */
7606: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN)) */
7607: if( strb[0]=='\0' || (*endptr != '\0')){
7608: 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);
7609: 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);
7610: return 1;
7611: }
7612: coqvar[iv][i]=dval;
1.226 ! brouard 7613: covar[ncovcol+iv][i]=dval; /* including qvar in standard covar for performance reasons */
1.223 brouard 7614: }
7615: strcpy(line,stra);
7616: }/* end loop nqv */
1.136 brouard 7617:
1.223 brouard 7618: /* Covariate values */
1.136 brouard 7619: for (j=ncovcol;j>=1;j--){
7620: cutv(stra, strb,line,' ');
1.223 brouard 7621: if(strb[0]=='.') { /* Missing covariate value */
1.225 brouard 7622: lval=-1;
1.136 brouard 7623: }else{
1.225 brouard 7624: errno=0;
7625: lval=strtol(strb,&endptr,10);
7626: if( strb[0]=='\0' || (*endptr != '\0')){
7627: 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);
7628: 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);
7629: return 1;
7630: }
1.136 brouard 7631: }
7632: if(lval <-1 || lval >1){
1.225 brouard 7633: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7634: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7635: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7636: For example, for multinomial values like 1, 2 and 3,\n \
7637: build V1=0 V2=0 for the reference value (1),\n \
7638: V1=1 V2=0 for (2) \n \
1.136 brouard 7639: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7640: output of IMaCh is often meaningless.\n \
1.136 brouard 7641: Exiting.\n",lval,linei, i,line,j);
1.225 brouard 7642: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7643: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7644: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
1.225 brouard 7645: For example, for multinomial values like 1, 2 and 3,\n \
7646: build V1=0 V2=0 for the reference value (1),\n \
7647: V1=1 V2=0 for (2) \n \
1.136 brouard 7648: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
1.225 brouard 7649: output of IMaCh is often meaningless.\n \
1.136 brouard 7650: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
1.225 brouard 7651: return 1;
1.136 brouard 7652: }
7653: covar[j][i]=(double)(lval);
7654: strcpy(line,stra);
7655: }
7656: lstra=strlen(stra);
1.225 brouard 7657:
1.136 brouard 7658: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
7659: stratrunc = &(stra[lstra-9]);
7660: num[i]=atol(stratrunc);
7661: }
7662: else
7663: num[i]=atol(stra);
7664: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
7665: 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;}*/
7666:
7667: i=i+1;
7668: } /* End loop reading data */
1.225 brouard 7669:
1.136 brouard 7670: *imax=i-1; /* Number of individuals */
7671: fclose(fic);
1.225 brouard 7672:
1.136 brouard 7673: return (0);
1.164 brouard 7674: /* endread: */
1.225 brouard 7675: printf("Exiting readdata: ");
7676: fclose(fic);
7677: return (1);
1.223 brouard 7678: }
1.126 brouard 7679:
1.145 brouard 7680: void removespace(char *str) {
7681: char *p1 = str, *p2 = str;
7682: do
7683: while (*p2 == ' ')
7684: p2++;
1.169 brouard 7685: while (*p1++ == *p2++);
1.145 brouard 7686: }
7687:
1.224 brouard 7688: int decodemodel ( char model[], int lastobs)
7689: /**< This routine decode the model and returns:
7690: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
7691: * - nagesqr = 1 if age*age in the model, otherwise 0.
7692: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
7693: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
7694: * - cptcovage number of covariates with age*products =2
7695: * - cptcovs number of simple covariates
7696: * - 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
7697: * which is a new column after the 9 (ncovcol) variables.
7698: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
7699: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
7700: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
7701: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
7702: */
1.136 brouard 7703: {
1.145 brouard 7704: int i, j, k, ks;
1.164 brouard 7705: int j1, k1, k2;
1.136 brouard 7706: char modelsav[80];
1.145 brouard 7707: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 7708: char *strpt;
1.136 brouard 7709:
1.145 brouard 7710: /*removespace(model);*/
1.136 brouard 7711: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 7712: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 7713: if (strstr(model,"AGE") !=0){
1.192 brouard 7714: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
7715: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 7716: return 1;
7717: }
1.141 brouard 7718: if (strstr(model,"v") !=0){
7719: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
7720: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
7721: return 1;
7722: }
1.187 brouard 7723: strcpy(modelsav,model);
7724: if ((strpt=strstr(model,"age*age")) !=0){
7725: printf(" strpt=%s, model=%s\n",strpt, model);
7726: if(strpt != model){
1.225 brouard 7727: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7728: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7729: corresponding column of parameters.\n",model);
1.225 brouard 7730: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7731: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7732: corresponding column of parameters.\n",model); fflush(ficlog);
1.225 brouard 7733: return 1;
7734: }
1.187 brouard 7735: nagesqr=1;
7736: if (strstr(model,"+age*age") !=0)
1.225 brouard 7737: substrchaine(modelsav, model, "+age*age");
1.187 brouard 7738: else if (strstr(model,"age*age+") !=0)
1.225 brouard 7739: substrchaine(modelsav, model, "age*age+");
1.187 brouard 7740: else
1.225 brouard 7741: substrchaine(modelsav, model, "age*age");
1.187 brouard 7742: }else
7743: nagesqr=0;
7744: if (strlen(modelsav) >1){
7745: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
7746: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
1.224 brouard 7747: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =5-3=2 */
1.187 brouard 7748: cptcovt= j+1; /* Number of total covariates in the model, not including
1.225 brouard 7749: * cst, age and age*age
7750: * V1+V1*age+ V3 + V3*V4+age*age=> 3+1=4*/
7751: /* including age products which are counted in cptcovage.
7752: * but the covariates which are products must be treated
7753: * separately: ncovn=4- 2=2 (V1+V3). */
1.187 brouard 7754: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
7755: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
1.225 brouard 7756:
7757:
1.187 brouard 7758: /* Design
7759: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
7760: * < ncovcol=8 >
7761: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
7762: * k= 1 2 3 4 5 6 7 8
7763: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
7764: * covar[k,i], value of kth covariate if not including age for individual i:
1.224 brouard 7765: * covar[1][i]= (V1), covar[4][i]=(V4), covar[8][i]=(V8)
7766: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[2]=1 Tvar[4]=3 Tvar[8]=8
1.187 brouard 7767: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
7768: * Tage[++cptcovage]=k
7769: * if products, new covar are created after ncovcol with k1
7770: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
7771: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
7772: * 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
7773: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
7774: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
7775: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
7776: * < ncovcol=8 >
7777: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
7778: * k= 1 2 3 4 5 6 7 8 9 10 11 12
7779: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
7780: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7781: * p Tprod[1]@2={ 6, 5}
7782: *p Tvard[1][1]@4= {7, 8, 5, 6}
7783: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
7784: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
7785: *How to reorganize?
7786: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
7787: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7788: * {2, 1, 4, 8, 5, 6, 3, 7}
7789: * Struct []
7790: */
1.225 brouard 7791:
1.187 brouard 7792: /* This loop fills the array Tvar from the string 'model'.*/
7793: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
7794: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
7795: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
7796: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
7797: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
7798: /* k=1 Tvar[1]=2 (from V2) */
7799: /* k=5 Tvar[5] */
7800: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 7801: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 7802: /* } */
1.198 brouard 7803: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 7804: /*
7805: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
1.225 brouard 7806: for(k=cptcovt; k>=1;k--) /**< Number of covariates not including constant and age, neither age*age*/
1.145 brouard 7807: Tvar[k]=0;
1.187 brouard 7808: cptcovage=0;
7809: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
1.225 brouard 7810: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
7811: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
7812: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
7813: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
7814: /*scanf("%d",i);*/
7815: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
7816: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
7817: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
7818: /* covar is not filled and then is empty */
7819: cptcovprod--;
7820: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
7821: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
1.226 ! brouard 7822: Typevar[k]=1; /* 1 for age product */
1.225 brouard 7823: cptcovage++; /* Sums the number of covariates which include age as a product */
7824: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
7825: /*printf("stre=%s ", stre);*/
7826: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
7827: cptcovprod--;
7828: cutl(stre,strb,strc,'V');
7829: Tvar[k]=atoi(stre);
7830: Typevar[k]=1; /* 1 for age product */
7831: cptcovage++;
7832: Tage[cptcovage]=k;
7833: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
7834: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
7835: cptcovn++;
7836: cptcovprodnoage++;k1++;
7837: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
7838: Tvar[k]=ncovcol+nqv+ntv+nqtv+k1; /* For model-covariate k tells which data-covariate to use but
7839: because this model-covariate is a construction we invent a new column
7840: which is after existing variables ncovcol+nqv+ntv+nqtv + k1
7841: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
7842: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
7843: Typevar[k]=2; /* 2 for double fixed dummy covariates */
7844: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
7845: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
7846: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
7847: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
7848: k2=k2+2; /* k2 is initialize to -1, We want to store the n and m in Vn*Vm at the end of Tvar */
7849: /* Tvar[cptcovt+k2]=Tvard[k1][1]; /\* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) *\/ */
7850: /* Tvar[cptcovt+k2+1]=Tvard[k1][2]; /\* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) *\/ */
7851: /*ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2, Tvar[3]=5, Tvar[4]=6, cptcovt=5 */
7852: /* 1 2 3 4 5 | Tvar[5+1)=1, Tvar[7]=2 */
7853: for (i=1; i<=lastobs;i++){
7854: /* Computes the new covariate which is a product of
7855: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
7856: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
7857: }
7858: } /* End age is not in the model */
7859: } /* End if model includes a product */
7860: else { /* no more sum */
7861: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
7862: /* scanf("%d",i);*/
7863: cutl(strd,strc,strb,'V');
7864: ks++; /**< Number of simple covariates*/
7865: cptcovn++; /** V4+V3+V5: V4 and V3 timevarying dummy covariates, V5 timevarying quantitative */
7866: Tvar[k]=atoi(strd);
7867: Typevar[k]=0; /* 0 for simple covariates */
7868: }
7869: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
1.223 brouard 7870: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
1.225 brouard 7871: scanf("%d",i);*/
1.187 brouard 7872: } /* end of loop + on total covariates */
7873: } /* end if strlen(modelsave == 0) age*age might exist */
7874: } /* end if strlen(model == 0) */
1.136 brouard 7875:
7876: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
7877: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
1.225 brouard 7878:
1.136 brouard 7879: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
1.225 brouard 7880: printf("cptcovprod=%d ", cptcovprod);
7881: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
7882: scanf("%d ",i);*/
7883:
7884:
7885: /* Decodemodel knows only the grammar (simple, product, age*) of the model but not what kind
7886: of variable (dummy vs quantitative, fixed vs time varying) is behind */
1.226 ! brouard 7887: /* ncovcol= 1, nqv=1 | ntv=2, nqtv= 1 = 5 possible variables data: 2 fixed 3, varying
! 7888: model= V5 + V4 +V3 + V4*V3 + V5*age + V2 + V1*V2 + V1*age + V5*age, V1 is not used saving its place
! 7889: k = 1 2 3 4 5 6 7 8 9
! 7890: Tvar[k]= 5 4 3 1+1+2+1+1=6 5 2 7 1 5
! 7891: Typevar[k]= 0 0 0 2 1 0 2 1 1
! 7892: Fixed[Tvar[k]]1 1 1 1 2 0 1 2 3
! 7893: Dummy[Tvar[k]]1 0 0 0 2 1 1 2 3
1.225 brouard 7894: */
7895: /* Dispatching between quantitative and time varying covariates */
1.226 ! brouard 7896: /* If Tvar[k] >ncovcol it is a product */
1.225 brouard 7897: /* Tvar[k] is the value n of Vn with n varying for 1 to nvcol, or p Vp=Vn*Vm for product */
1.226 ! brouard 7898: /* Computing effective variables, ie used by the model, that is from the cptcovt variables */
1.225 brouard 7899: for(k=1, ncoveff=0, nqfveff=0, ntveff=0, nqtveff=0;k<=cptcovt; k++){ /* or cptocvt */
1.226 ! brouard 7900: if (Tvar[k] <=ncovcol && (Typevar[k]==0 || Typevar[k]==2)){ /* Simple or product fixed dummy covariatee */
! 7901: Fixed[Tvar[k]]= 0;
! 7902: Dummy[Tvar[k]]= 0;
1.225 brouard 7903: ncoveff++;
7904: }else if( Tvar[k] <=ncovcol+nqv && Typevar[k]==0){ /* Remind that product Vn*Vm are added in k*/
1.226 ! brouard 7905: Fixed[Tvar[k]]= 0;
! 7906: Dummy[Tvar[k]]= 1;
1.225 brouard 7907: nqfveff++; /* Only simple fixed quantitative variable */
7908: }else if( Tvar[k] <=ncovcol+nqv+ntv && Typevar[k]==0){
1.226 ! brouard 7909: Fixed[Tvar[k]]= 1;
! 7910: Dummy[Tvar[k]]= 0;
1.225 brouard 7911: ntveff++; /* Only simple time varying dummy variable */
1.226 ! brouard 7912: }else if( Tvar[k] <=ncovcol+nqv+ntv+nqtv){
! 7913: if( Typevar[k]==0){
! 7914: Fixed[Tvar[k]]= 1;
! 7915: Dummy[Tvar[k]]= 1;
! 7916: nqtveff++;/* Only simple time varying quantitative variable */
! 7917: }
! 7918: }else if (Typevar[k] == 2) {
! 7919: for(k1=1; k1 <= cptcovprodnoage; k1++){
! 7920: if(Tvard[k1][1] <=ncovcol){
! 7921: if(Tvard[k1][2] <=ncovcol){
! 7922: Fixed[Tvar[k]]= 1;
! 7923: Dummy[Tvar[k]]= 0;
! 7924: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 7925: Fixed[Tvar[k]]= 0;
! 7926: Dummy[Tvar[k]]= 1;
! 7927: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 7928: Fixed[Tvar[k]]= 1;
! 7929: Dummy[Tvar[k]]= 0;
! 7930: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 7931: Fixed[Tvar[k]]= 1;
! 7932: Dummy[Tvar[k]]= 1;
! 7933: }
! 7934: }else if(Tvard[k1][1] <=ncovcol+nqv){
! 7935: if(Tvard[k1][2] <=ncovcol){
! 7936: Fixed[Tvar[k]]= 0;
! 7937: Dummy[Tvar[k]]= 1;
! 7938: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 7939: Fixed[Tvar[k]]= 0;
! 7940: Dummy[Tvar[k]]= 1;
! 7941: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 7942: Fixed[Tvar[k]]= 1;
! 7943: Dummy[Tvar[k]]= 1;
! 7944: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 7945: Fixed[Tvar[k]]= 1;
! 7946: Dummy[Tvar[k]]= 1;
! 7947: }
! 7948: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv){
! 7949: if(Tvard[k1][2] <=ncovcol){
! 7950: Fixed[Tvar[k]]= 1;
! 7951: Dummy[Tvar[k]]= 1;
! 7952: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 7953: Fixed[Tvar[k]]= 1;
! 7954: Dummy[Tvar[k]]= 1;
! 7955: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 7956: Fixed[Tvar[k]]= 1;
! 7957: Dummy[Tvar[k]]= 0;
! 7958: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 7959: Fixed[Tvar[k]]= 1;
! 7960: Dummy[Tvar[k]]= 1;
! 7961: }
! 7962: }else if(Tvard[k1][1] <=ncovcol+nqv+ntv+nqtv){
! 7963: if(Tvard[k1][2] <=ncovcol){
! 7964: Fixed[Tvar[k]]= 1;
! 7965: Dummy[Tvar[k]]= 1;
! 7966: }else if(Tvard[k1][2] <=ncovcol+nqv){
! 7967: Fixed[Tvar[k]]= 1;
! 7968: Dummy[Tvar[k]]= 1;
! 7969: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv){
! 7970: Fixed[Tvar[k]]= 1;
! 7971: Dummy[Tvar[k]]= 1;
! 7972: }else if(Tvard[k1][2] <=ncovcol+nqv+ntv+nqtv){
! 7973: Fixed[Tvar[k]]= 1;
! 7974: Dummy[Tvar[k]]= 1;
! 7975: }
! 7976: }else{
! 7977: printf("Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
! 7978: fprintf(ficlog,"Error unknown type of covariate: Tvard[%d][1]=%d,Tvard[%d][2]=%d\n",k1,Tvard[k1][1],k1,Tvard[k1][2]);
! 7979: }
! 7980: } /* end k1 */
1.225 brouard 7981: }else{
1.226 ! brouard 7982: printf("Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
! 7983: fprintf(ficlog,"Error, current version can't treat for performance reasons, Tvar[%d]=%d, Typevar[%d]=%d\n", k, Tvar[k], k, Typevar[k]);
1.225 brouard 7984: }
1.226 ! brouard 7985: printf("Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[Tvar[k]],Dummy[Tvar[k]]);
! 7986: fprintf(ficlog,"Decodemodel, k=%d, Tvar[%d]=V%d,Typevar=%d, Fixed=%d, Dummy=%d\n",k, k,Tvar[k],Typevar[k],Fixed[Tvar[k]],Dummy[Tvar[k]]);
1.225 brouard 7987: }
1.226 ! brouard 7988: printf("Model=%s\n\
! 7989: Typevar: 0 for simple covariate (dummy, quantitative, fixed or varying), 1 for age product, 2 for product \n\
! 7990: Fixed[Tvar[k]] 0=fixed, 1 varying, 2 fixed with age product, 3 varying with age product \n\
! 7991: Dummy[Tvar[k]] 0=dummy (0 1), 1 quantitative (single or product without age), 2 dummy with age product, 3 quant with age product\n",model);
! 7992:
1.225 brouard 7993: printf("ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
7994: fprintf(ficlog,"ncoveff=%d, nqfveff=%d, ntveff=%d, nqtveff=%d, cptcovn=%d\n",ncoveff,nqfveff,ntveff,nqtveff,cptcovn);
1.137 brouard 7995: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 7996: /*endread:*/
1.225 brouard 7997: printf("Exiting decodemodel: ");
7998: return (1);
1.136 brouard 7999: }
8000:
1.169 brouard 8001: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 8002: {
8003: int i, m;
1.218 brouard 8004: int firstone=0;
8005:
1.136 brouard 8006: for (i=1; i<=imx; i++) {
8007: for(m=2; (m<= maxwav); m++) {
8008: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
8009: anint[m][i]=9999;
1.216 brouard 8010: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
8011: s[m][i]=-1;
1.136 brouard 8012: }
8013: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 8014: *nberr = *nberr + 1;
1.218 brouard 8015: if(firstone == 0){
8016: firstone=1;
8017: 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);
8018: }
8019: 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 8020: s[m][i]=-1;
8021: }
8022: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 8023: (*nberr)++;
1.136 brouard 8024: 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]);
8025: 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]);
8026: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
8027: }
8028: }
8029: }
8030:
8031: for (i=1; i<=imx; i++) {
8032: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
8033: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 8034: 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 8035: if (s[m][i] >= nlstate+1) {
1.169 brouard 8036: if(agedc[i]>0){
8037: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 8038: agev[m][i]=agedc[i];
1.214 brouard 8039: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 8040: }else {
1.136 brouard 8041: if ((int)andc[i]!=9999){
8042: nbwarn++;
8043: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
8044: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
8045: agev[m][i]=-1;
8046: }
8047: }
1.169 brouard 8048: } /* agedc > 0 */
1.214 brouard 8049: } /* end if */
1.136 brouard 8050: else if(s[m][i] !=9){ /* Standard case, age in fractional
8051: years but with the precision of a month */
8052: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
8053: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
8054: agev[m][i]=1;
8055: else if(agev[m][i] < *agemin){
8056: *agemin=agev[m][i];
8057: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
8058: }
8059: else if(agev[m][i] >*agemax){
8060: *agemax=agev[m][i];
1.156 brouard 8061: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 8062: }
8063: /*agev[m][i]=anint[m][i]-annais[i];*/
8064: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 8065: } /* en if 9*/
1.136 brouard 8066: else { /* =9 */
1.214 brouard 8067: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 8068: agev[m][i]=1;
8069: s[m][i]=-1;
8070: }
8071: }
1.214 brouard 8072: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 8073: agev[m][i]=1;
1.214 brouard 8074: else{
8075: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8076: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
8077: agev[m][i]=0;
8078: }
8079: } /* End for lastpass */
8080: }
1.136 brouard 8081:
8082: for (i=1; i<=imx; i++) {
8083: for(m=firstpass; (m<=lastpass); m++){
8084: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 8085: (*nberr)++;
1.136 brouard 8086: 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);
8087: 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);
8088: return 1;
8089: }
8090: }
8091: }
8092:
8093: /*for (i=1; i<=imx; i++){
8094: for (m=firstpass; (m<lastpass); m++){
8095: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
8096: }
8097:
8098: }*/
8099:
8100:
1.139 brouard 8101: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
8102: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 8103:
8104: return (0);
1.164 brouard 8105: /* endread:*/
1.136 brouard 8106: printf("Exiting calandcheckages: ");
8107: return (1);
8108: }
8109:
1.172 brouard 8110: #if defined(_MSC_VER)
8111: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8112: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
8113: //#include "stdafx.h"
8114: //#include <stdio.h>
8115: //#include <tchar.h>
8116: //#include <windows.h>
8117: //#include <iostream>
8118: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
8119:
8120: LPFN_ISWOW64PROCESS fnIsWow64Process;
8121:
8122: BOOL IsWow64()
8123: {
8124: BOOL bIsWow64 = FALSE;
8125:
8126: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
8127: // (HANDLE, PBOOL);
8128:
8129: //LPFN_ISWOW64PROCESS fnIsWow64Process;
8130:
8131: HMODULE module = GetModuleHandle(_T("kernel32"));
8132: const char funcName[] = "IsWow64Process";
8133: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
8134: GetProcAddress(module, funcName);
8135:
8136: if (NULL != fnIsWow64Process)
8137: {
8138: if (!fnIsWow64Process(GetCurrentProcess(),
8139: &bIsWow64))
8140: //throw std::exception("Unknown error");
8141: printf("Unknown error\n");
8142: }
8143: return bIsWow64 != FALSE;
8144: }
8145: #endif
1.177 brouard 8146:
1.191 brouard 8147: void syscompilerinfo(int logged)
1.167 brouard 8148: {
8149: /* #include "syscompilerinfo.h"*/
1.185 brouard 8150: /* command line Intel compiler 32bit windows, XP compatible:*/
8151: /* /GS /W3 /Gy
8152: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
8153: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
8154: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 8155: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
8156: */
8157: /* 64 bits */
1.185 brouard 8158: /*
8159: /GS /W3 /Gy
8160: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
8161: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
8162: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
8163: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
8164: /* Optimization are useless and O3 is slower than O2 */
8165: /*
8166: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
8167: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
8168: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
8169: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
8170: */
1.186 brouard 8171: /* Link is */ /* /OUT:"visual studio
1.185 brouard 8172: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
8173: /PDB:"visual studio
8174: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
8175: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
8176: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
8177: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
8178: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
8179: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
8180: uiAccess='false'"
8181: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
8182: /NOLOGO /TLBID:1
8183: */
1.177 brouard 8184: #if defined __INTEL_COMPILER
1.178 brouard 8185: #if defined(__GNUC__)
8186: struct utsname sysInfo; /* For Intel on Linux and OS/X */
8187: #endif
1.177 brouard 8188: #elif defined(__GNUC__)
1.179 brouard 8189: #ifndef __APPLE__
1.174 brouard 8190: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 8191: #endif
1.177 brouard 8192: struct utsname sysInfo;
1.178 brouard 8193: int cross = CROSS;
8194: if (cross){
8195: printf("Cross-");
1.191 brouard 8196: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 8197: }
1.174 brouard 8198: #endif
8199:
1.171 brouard 8200: #include <stdint.h>
1.178 brouard 8201:
1.191 brouard 8202: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 8203: #if defined(__clang__)
1.191 brouard 8204: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 8205: #endif
8206: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 8207: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 8208: #endif
8209: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 8210: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 8211: #endif
8212: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 8213: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 8214: #endif
8215: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 8216: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 8217: #endif
8218: #if defined(_MSC_VER)
1.191 brouard 8219: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 8220: #endif
8221: #if defined(__PGI)
1.191 brouard 8222: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 8223: #endif
8224: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 8225: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 8226: #endif
1.191 brouard 8227: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 8228:
1.167 brouard 8229: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
8230: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
8231: // Windows (x64 and x86)
1.191 brouard 8232: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 8233: #elif __unix__ // all unices, not all compilers
8234: // Unix
1.191 brouard 8235: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 8236: #elif __linux__
8237: // linux
1.191 brouard 8238: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 8239: #elif __APPLE__
1.174 brouard 8240: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 8241: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 8242: #endif
8243:
8244: /* __MINGW32__ */
8245: /* __CYGWIN__ */
8246: /* __MINGW64__ */
8247: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
8248: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
8249: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
8250: /* _WIN64 // Defined for applications for Win64. */
8251: /* _M_X64 // Defined for compilations that target x64 processors. */
8252: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 8253:
1.167 brouard 8254: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 8255: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 8256: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 8257: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 8258: #else
1.191 brouard 8259: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 8260: #endif
8261:
1.169 brouard 8262: #if defined(__GNUC__)
8263: # if defined(__GNUC_PATCHLEVEL__)
8264: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8265: + __GNUC_MINOR__ * 100 \
8266: + __GNUC_PATCHLEVEL__)
8267: # else
8268: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
8269: + __GNUC_MINOR__ * 100)
8270: # endif
1.174 brouard 8271: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 8272: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 8273:
8274: if (uname(&sysInfo) != -1) {
8275: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 8276: 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 8277: }
8278: else
8279: perror("uname() error");
1.179 brouard 8280: //#ifndef __INTEL_COMPILER
8281: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 8282: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 8283: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 8284: #endif
1.169 brouard 8285: #endif
1.172 brouard 8286:
8287: // void main()
8288: // {
1.169 brouard 8289: #if defined(_MSC_VER)
1.174 brouard 8290: if (IsWow64()){
1.191 brouard 8291: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
8292: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 8293: }
8294: else{
1.191 brouard 8295: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
8296: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 8297: }
1.172 brouard 8298: // printf("\nPress Enter to continue...");
8299: // getchar();
8300: // }
8301:
1.169 brouard 8302: #endif
8303:
1.167 brouard 8304:
1.219 brouard 8305: }
1.136 brouard 8306:
1.219 brouard 8307: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 8308: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
8309: int i, j, k, i1 ;
1.202 brouard 8310: /* double ftolpl = 1.e-10; */
1.180 brouard 8311: double age, agebase, agelim;
1.203 brouard 8312: double tot;
1.180 brouard 8313:
1.202 brouard 8314: strcpy(filerespl,"PL_");
8315: strcat(filerespl,fileresu);
8316: if((ficrespl=fopen(filerespl,"w"))==NULL) {
8317: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8318: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
8319: }
8320: printf("Computing period (stable) prevalence: result on file '%s' \n", filerespl);
8321: fprintf(ficlog,"Computing period (stable) prevalence: result on file '%s' \n", filerespl);
8322: pstamp(ficrespl);
1.203 brouard 8323: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 8324: fprintf(ficrespl,"#Age ");
8325: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
8326: fprintf(ficrespl,"\n");
1.180 brouard 8327:
1.219 brouard 8328: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
1.180 brouard 8329:
1.219 brouard 8330: agebase=ageminpar;
8331: agelim=agemaxpar;
1.180 brouard 8332:
1.224 brouard 8333: i1=pow(2,ncoveff);
1.219 brouard 8334: if (cptcovn < 1){i1=1;}
1.180 brouard 8335:
1.220 brouard 8336: for(k=1; k<=i1;k++){
8337: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
1.180 brouard 8338: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.219 brouard 8339: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
1.220 brouard 8340: /* k=k+1; */
1.219 brouard 8341: /* to clean */
8342: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8343: fprintf(ficrespl,"#******");
8344: printf("#******");
8345: fprintf(ficlog,"#******");
1.225 brouard 8346: for(j=1;j<=nqfveff;j++) {
1.219 brouard 8347: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8348: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8349: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8350: }
8351: fprintf(ficrespl,"******\n");
8352: printf("******\n");
8353: fprintf(ficlog,"******\n");
1.220 brouard 8354: if(invalidvarcomb[k]){
8355: printf("\nCombination (%d) ignored because no cases \n",k);
8356: fprintf(ficrespl,"#Combination (%d) ignored because no cases \n",k);
8357: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
8358: continue;
8359: }
1.219 brouard 8360:
8361: fprintf(ficrespl,"#Age ");
1.225 brouard 8362: for(j=1;j<=nqfveff;j++) {
1.219 brouard 8363: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8364: }
8365: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
8366: fprintf(ficrespl,"Total Years_to_converge\n");
8367:
8368: for (age=agebase; age<=agelim; age++){
8369: /* for (age=agebase; age<=agebase; age++){ */
8370: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k);
8371: fprintf(ficrespl,"%.0f ",age );
1.225 brouard 8372: for(j=1;j<=nqfveff;j++)
1.220 brouard 8373: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 8374: tot=0.;
8375: for(i=1; i<=nlstate;i++){
1.220 brouard 8376: tot += prlim[i][i];
8377: fprintf(ficrespl," %.5f", prlim[i][i]);
1.219 brouard 8378: }
8379: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
8380: } /* Age */
8381: /* was end of cptcod */
8382: } /* cptcov */
8383: return 0;
1.180 brouard 8384: }
8385:
1.218 brouard 8386: 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){
8387: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
8388:
8389: /* Computes the back prevalence limit for any combination of covariate values
8390: * at any age between ageminpar and agemaxpar
8391: */
1.217 brouard 8392: int i, j, k, i1 ;
8393: /* double ftolpl = 1.e-10; */
8394: double age, agebase, agelim;
8395: double tot;
1.218 brouard 8396: /* double ***mobaverage; */
8397: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 8398:
8399: strcpy(fileresplb,"PLB_");
8400: strcat(fileresplb,fileresu);
8401: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
8402: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8403: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
8404: }
8405: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8406: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
8407: pstamp(ficresplb);
8408: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
8409: fprintf(ficresplb,"#Age ");
8410: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
8411: fprintf(ficresplb,"\n");
8412:
1.218 brouard 8413:
8414: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
8415:
8416: agebase=ageminpar;
8417: agelim=agemaxpar;
8418:
8419:
1.225 brouard 8420: i1=pow(2,nqfveff);
1.218 brouard 8421: if (cptcovn < 1){i1=1;}
1.220 brouard 8422:
8423: for(k=1; k<=i1;k++){
8424: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
1.217 brouard 8425: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.218 brouard 8426: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
1.220 brouard 8427: /* k=k+1; */
1.218 brouard 8428: /* to clean */
8429: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
8430: fprintf(ficresplb,"#******");
8431: printf("#******");
8432: fprintf(ficlog,"#******");
1.225 brouard 8433: for(j=1;j<=nqfveff;j++) {
1.218 brouard 8434: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8435: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8436: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8437: }
8438: fprintf(ficresplb,"******\n");
8439: printf("******\n");
8440: fprintf(ficlog,"******\n");
1.220 brouard 8441: if(invalidvarcomb[k]){
8442: printf("\nCombination (%d) ignored because no cases \n",k);
8443: fprintf(ficresplb,"#Combination (%d) ignored because no cases \n",k);
8444: fprintf(ficlog,"\nCombination (%d) ignored because no cases \n",k);
8445: continue;
8446: }
1.218 brouard 8447:
8448: fprintf(ficresplb,"#Age ");
1.225 brouard 8449: for(j=1;j<=nqfveff;j++) {
1.218 brouard 8450: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8451: }
8452: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
8453: fprintf(ficresplb,"Total Years_to_converge\n");
8454:
8455:
8456: for (age=agebase; age<=agelim; age++){
8457: /* for (age=agebase; age<=agebase; age++){ */
8458: if(mobilavproj > 0){
8459: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
8460: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
1.219 brouard 8461: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8462: }else if (mobilavproj == 0){
1.219 brouard 8463: 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);
8464: 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);
8465: exit(1);
1.218 brouard 8466: }else{
1.219 brouard 8467: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
8468: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k);
1.218 brouard 8469: }
8470: fprintf(ficresplb,"%.0f ",age );
1.225 brouard 8471: for(j=1;j<=nqfveff;j++)
1.219 brouard 8472: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.218 brouard 8473: tot=0.;
8474: for(i=1; i<=nlstate;i++){
1.219 brouard 8475: tot += bprlim[i][i];
8476: fprintf(ficresplb," %.5f", bprlim[i][i]);
1.218 brouard 8477: }
8478: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
8479: } /* Age */
8480: /* was end of cptcod */
8481: } /* cptcov */
8482:
8483: /* hBijx(p, bage, fage); */
8484: /* fclose(ficrespijb); */
8485:
8486: return 0;
1.217 brouard 8487: }
1.218 brouard 8488:
1.180 brouard 8489: int hPijx(double *p, int bage, int fage){
8490: /*------------- h Pij x at various ages ------------*/
8491:
8492: int stepsize;
8493: int agelim;
8494: int hstepm;
8495: int nhstepm;
8496: int h, i, i1, j, k;
8497:
8498: double agedeb;
8499: double ***p3mat;
8500:
1.201 brouard 8501: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 8502: if((ficrespij=fopen(filerespij,"w"))==NULL) {
8503: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
8504: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
8505: }
8506: printf("Computing pij: result on file '%s' \n", filerespij);
8507: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
8508:
8509: stepsize=(int) (stepm+YEARM-1)/YEARM;
8510: /*if (stepm<=24) stepsize=2;*/
8511:
8512: agelim=AGESUP;
8513: hstepm=stepsize*YEARM; /* Every year of age */
8514: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 brouard 8515:
1.180 brouard 8516: /* hstepm=1; aff par mois*/
8517: pstamp(ficrespij);
8518: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
1.225 brouard 8519: i1= pow(2,nqfveff);
1.218 brouard 8520: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
8521: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
8522: /* k=k+1; */
1.225 brouard 8523: for (k=1; k <= (int) pow(2,nqfveff); k++){
1.183 brouard 8524: fprintf(ficrespij,"\n#****** ");
1.225 brouard 8525: for(j=1;j<=nqfveff;j++)
1.198 brouard 8526: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.183 brouard 8527: fprintf(ficrespij,"******\n");
8528:
8529: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
8530: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8531: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
8532:
8533: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 8534:
1.183 brouard 8535: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8536: oldm=oldms;savm=savms;
8537: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
8538: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
8539: for(i=1; i<=nlstate;i++)
8540: for(j=1; j<=nlstate+ndeath;j++)
8541: fprintf(ficrespij," %1d-%1d",i,j);
8542: fprintf(ficrespij,"\n");
8543: for (h=0; h<=nhstepm; h++){
8544: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
8545: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 8546: for(i=1; i<=nlstate;i++)
8547: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 8548: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 8549: fprintf(ficrespij,"\n");
8550: }
1.183 brouard 8551: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8552: fprintf(ficrespij,"\n");
8553: }
1.180 brouard 8554: /*}*/
8555: }
1.218 brouard 8556: return 0;
1.180 brouard 8557: }
1.218 brouard 8558:
8559: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 8560: /*------------- h Bij x at various ages ------------*/
8561:
8562: int stepsize;
1.218 brouard 8563: /* int agelim; */
8564: int ageminl;
1.217 brouard 8565: int hstepm;
8566: int nhstepm;
8567: int h, i, i1, j, k;
1.218 brouard 8568:
1.217 brouard 8569: double agedeb;
8570: double ***p3mat;
1.218 brouard 8571:
8572: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
8573: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
8574: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
8575: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
8576: }
8577: printf("Computing pij back: result on file '%s' \n", filerespijb);
8578: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
8579:
8580: stepsize=(int) (stepm+YEARM-1)/YEARM;
8581: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 8582:
1.218 brouard 8583: /* agelim=AGESUP; */
8584: ageminl=30;
8585: hstepm=stepsize*YEARM; /* Every year of age */
8586: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
8587:
8588: /* hstepm=1; aff par mois*/
8589: pstamp(ficrespijb);
8590: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
1.225 brouard 8591: i1= pow(2,nqfveff);
1.218 brouard 8592: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
8593: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
8594: /* k=k+1; */
1.225 brouard 8595: for (k=1; k <= (int) pow(2,nqfveff); k++){
1.218 brouard 8596: fprintf(ficrespijb,"\n#****** ");
1.225 brouard 8597: for(j=1;j<=nqfveff;j++)
1.218 brouard 8598: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
8599: fprintf(ficrespijb,"******\n");
1.222 brouard 8600: if(invalidvarcomb[k]){
8601: fprintf(ficrespijb,"\n#Combination (%d) ignored because no cases \n",k);
8602: continue;
8603: }
1.218 brouard 8604:
8605: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
8606: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
8607: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
8608: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
8609: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
8610:
8611: /* nhstepm=nhstepm*YEARM; aff par mois*/
8612:
8613: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8614: /* oldm=oldms;savm=savms; */
8615: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
8616: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
8617: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
8618: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
8619: for(i=1; i<=nlstate;i++)
8620: for(j=1; j<=nlstate+ndeath;j++)
8621: fprintf(ficrespijb," %1d-%1d",i,j);
8622: fprintf(ficrespijb,"\n");
8623: for (h=0; h<=nhstepm; h++){
8624: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
8625: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
8626: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 8627: for(i=1; i<=nlstate;i++)
8628: for(j=1; j<=nlstate+ndeath;j++)
1.218 brouard 8629: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
1.217 brouard 8630: fprintf(ficrespijb,"\n");
8631: }
1.218 brouard 8632: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
8633: fprintf(ficrespijb,"\n");
1.217 brouard 8634: }
1.218 brouard 8635: /*}*/
8636: }
8637: return 0;
8638: } /* hBijx */
1.217 brouard 8639:
1.180 brouard 8640:
1.136 brouard 8641: /***********************************************/
8642: /**************** Main Program *****************/
8643: /***********************************************/
8644:
8645: int main(int argc, char *argv[])
8646: {
8647: #ifdef GSL
8648: const gsl_multimin_fminimizer_type *T;
8649: size_t iteri = 0, it;
8650: int rval = GSL_CONTINUE;
8651: int status = GSL_SUCCESS;
8652: double ssval;
8653: #endif
8654: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 8655: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 8656: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 8657: int jj, ll, li, lj, lk;
1.136 brouard 8658: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 8659: int num_filled;
1.136 brouard 8660: int itimes;
8661: int NDIM=2;
8662: int vpopbased=0;
8663:
1.164 brouard 8664: char ca[32], cb[32];
1.136 brouard 8665: /* FILE *fichtm; *//* Html File */
8666: /* FILE *ficgp;*/ /*Gnuplot File */
8667: struct stat info;
1.191 brouard 8668: double agedeb=0.;
1.194 brouard 8669:
8670: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.219 brouard 8671: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 8672:
1.165 brouard 8673: double fret;
1.191 brouard 8674: double dum=0.; /* Dummy variable */
1.136 brouard 8675: double ***p3mat;
1.218 brouard 8676: /* double ***mobaverage; */
1.164 brouard 8677:
8678: char line[MAXLINE];
1.197 brouard 8679: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
8680:
8681: char model[MAXLINE], modeltemp[MAXLINE];
1.136 brouard 8682: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 8683: char *tok, *val; /* pathtot */
1.136 brouard 8684: int firstobs=1, lastobs=10;
1.195 brouard 8685: int c, h , cpt, c2;
1.191 brouard 8686: int jl=0;
8687: int i1, j1, jk, stepsize=0;
1.194 brouard 8688: int count=0;
8689:
1.164 brouard 8690: int *tab;
1.136 brouard 8691: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 8692: int backcast=0;
1.136 brouard 8693: int mobilav=0,popforecast=0;
1.191 brouard 8694: int hstepm=0, nhstepm=0;
1.136 brouard 8695: int agemortsup;
8696: float sumlpop=0.;
8697: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
8698: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
8699:
1.191 brouard 8700: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 8701: double ftolpl=FTOL;
8702: double **prlim;
1.217 brouard 8703: double **bprlim;
1.136 brouard 8704: double ***param; /* Matrix of parameters */
8705: double *p;
8706: double **matcov; /* Matrix of covariance */
1.203 brouard 8707: double **hess; /* Hessian matrix */
1.136 brouard 8708: double ***delti3; /* Scale */
8709: double *delti; /* Scale */
8710: double ***eij, ***vareij;
8711: double **varpl; /* Variances of prevalence limits by age */
8712: double *epj, vepp;
1.164 brouard 8713:
1.136 brouard 8714: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 8715: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
8716:
1.136 brouard 8717: double **ximort;
1.145 brouard 8718: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 8719: int *dcwave;
8720:
1.164 brouard 8721: char z[1]="c";
1.136 brouard 8722:
8723: /*char *strt;*/
8724: char strtend[80];
1.126 brouard 8725:
1.164 brouard 8726:
1.126 brouard 8727: /* setlocale (LC_ALL, ""); */
8728: /* bindtextdomain (PACKAGE, LOCALEDIR); */
8729: /* textdomain (PACKAGE); */
8730: /* setlocale (LC_CTYPE, ""); */
8731: /* setlocale (LC_MESSAGES, ""); */
8732:
8733: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 8734: rstart_time = time(NULL);
8735: /* (void) gettimeofday(&start_time,&tzp);*/
8736: start_time = *localtime(&rstart_time);
1.126 brouard 8737: curr_time=start_time;
1.157 brouard 8738: /*tml = *localtime(&start_time.tm_sec);*/
8739: /* strcpy(strstart,asctime(&tml)); */
8740: strcpy(strstart,asctime(&start_time));
1.126 brouard 8741:
8742: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 8743: /* tp.tm_sec = tp.tm_sec +86400; */
8744: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 8745: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
8746: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
8747: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 8748: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 8749: /* strt=asctime(&tmg); */
8750: /* printf("Time(after) =%s",strstart); */
8751: /* (void) time (&time_value);
8752: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
8753: * tm = *localtime(&time_value);
8754: * strstart=asctime(&tm);
8755: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
8756: */
8757:
8758: nberr=0; /* Number of errors and warnings */
8759: nbwarn=0;
1.184 brouard 8760: #ifdef WIN32
8761: _getcwd(pathcd, size);
8762: #else
1.126 brouard 8763: getcwd(pathcd, size);
1.184 brouard 8764: #endif
1.191 brouard 8765: syscompilerinfo(0);
1.196 brouard 8766: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 8767: if(argc <=1){
8768: printf("\nEnter the parameter file name: ");
1.205 brouard 8769: if(!fgets(pathr,FILENAMELENGTH,stdin)){
8770: printf("ERROR Empty parameter file name\n");
8771: goto end;
8772: }
1.126 brouard 8773: i=strlen(pathr);
8774: if(pathr[i-1]=='\n')
8775: pathr[i-1]='\0';
1.156 brouard 8776: i=strlen(pathr);
1.205 brouard 8777: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 8778: pathr[i-1]='\0';
1.205 brouard 8779: }
8780: i=strlen(pathr);
8781: if( i==0 ){
8782: printf("ERROR Empty parameter file name\n");
8783: goto end;
8784: }
8785: for (tok = pathr; tok != NULL; ){
1.126 brouard 8786: printf("Pathr |%s|\n",pathr);
8787: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
8788: printf("val= |%s| pathr=%s\n",val,pathr);
8789: strcpy (pathtot, val);
8790: if(pathr[0] == '\0') break; /* Dirty */
8791: }
8792: }
8793: else{
8794: strcpy(pathtot,argv[1]);
8795: }
8796: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
8797: /*cygwin_split_path(pathtot,path,optionfile);
8798: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
8799: /* cutv(path,optionfile,pathtot,'\\');*/
8800:
8801: /* Split argv[0], imach program to get pathimach */
8802: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
8803: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
8804: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
8805: /* strcpy(pathimach,argv[0]); */
8806: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
8807: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
8808: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 8809: #ifdef WIN32
8810: _chdir(path); /* Can be a relative path */
8811: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
8812: #else
1.126 brouard 8813: chdir(path); /* Can be a relative path */
1.184 brouard 8814: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
8815: #endif
8816: printf("Current directory %s!\n",pathcd);
1.126 brouard 8817: strcpy(command,"mkdir ");
8818: strcat(command,optionfilefiname);
8819: if((outcmd=system(command)) != 0){
1.169 brouard 8820: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 8821: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
8822: /* fclose(ficlog); */
8823: /* exit(1); */
8824: }
8825: /* if((imk=mkdir(optionfilefiname))<0){ */
8826: /* perror("mkdir"); */
8827: /* } */
8828:
8829: /*-------- arguments in the command line --------*/
8830:
1.186 brouard 8831: /* Main Log file */
1.126 brouard 8832: strcat(filelog, optionfilefiname);
8833: strcat(filelog,".log"); /* */
8834: if((ficlog=fopen(filelog,"w"))==NULL) {
8835: printf("Problem with logfile %s\n",filelog);
8836: goto end;
8837: }
8838: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 8839: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 8840: fprintf(ficlog,"\nEnter the parameter file name: \n");
8841: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
8842: path=%s \n\
8843: optionfile=%s\n\
8844: optionfilext=%s\n\
1.156 brouard 8845: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 8846:
1.197 brouard 8847: syscompilerinfo(1);
1.167 brouard 8848:
1.126 brouard 8849: printf("Local time (at start):%s",strstart);
8850: fprintf(ficlog,"Local time (at start): %s",strstart);
8851: fflush(ficlog);
8852: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 8853: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 8854:
8855: /* */
8856: strcpy(fileres,"r");
8857: strcat(fileres, optionfilefiname);
1.201 brouard 8858: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 8859: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 8860: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 8861:
1.186 brouard 8862: /* Main ---------arguments file --------*/
1.126 brouard 8863:
8864: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 8865: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
8866: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 8867: fflush(ficlog);
1.149 brouard 8868: /* goto end; */
8869: exit(70);
1.126 brouard 8870: }
8871:
8872:
8873:
8874: strcpy(filereso,"o");
1.201 brouard 8875: strcat(filereso,fileresu);
1.126 brouard 8876: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
8877: printf("Problem with Output resultfile: %s\n", filereso);
8878: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
8879: fflush(ficlog);
8880: goto end;
8881: }
8882:
8883: /* Reads comments: lines beginning with '#' */
8884: numlinepar=0;
1.197 brouard 8885:
8886: /* First parameter line */
8887: while(fgets(line, MAXLINE, ficpar)) {
8888: /* If line starts with a # it is a comment */
8889: if (line[0] == '#') {
8890: numlinepar++;
8891: fputs(line,stdout);
8892: fputs(line,ficparo);
8893: fputs(line,ficlog);
8894: continue;
8895: }else
8896: break;
8897: }
8898: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
8899: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
8900: if (num_filled != 5) {
8901: printf("Should be 5 parameters\n");
8902: }
1.126 brouard 8903: numlinepar++;
1.197 brouard 8904: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
8905: }
8906: /* Second parameter line */
8907: while(fgets(line, MAXLINE, ficpar)) {
8908: /* If line starts with a # it is a comment */
8909: if (line[0] == '#') {
8910: numlinepar++;
8911: fputs(line,stdout);
8912: fputs(line,ficparo);
8913: fputs(line,ficlog);
8914: continue;
8915: }else
8916: break;
8917: }
1.223 brouard 8918: 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", \
8919: &ftol, &stepm, &ncovcol, &nqv, &ntv, &nqtv, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
8920: if (num_filled != 11) {
8921: 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 8922: printf("but line=%s\n",line);
1.197 brouard 8923: }
1.223 brouard 8924: 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 8925: }
1.203 brouard 8926: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 8927: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 8928: /* Third parameter line */
8929: while(fgets(line, MAXLINE, ficpar)) {
8930: /* If line starts with a # it is a comment */
8931: if (line[0] == '#') {
8932: numlinepar++;
8933: fputs(line,stdout);
8934: fputs(line,ficparo);
8935: fputs(line,ficlog);
8936: continue;
8937: }else
8938: break;
8939: }
1.201 brouard 8940: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
8941: if (num_filled == 0)
8942: model[0]='\0';
8943: else if (num_filled != 1){
1.197 brouard 8944: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
8945: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
8946: model[0]='\0';
8947: goto end;
8948: }
8949: else{
8950: if (model[0]=='+'){
8951: for(i=1; i<=strlen(model);i++)
8952: modeltemp[i-1]=model[i];
1.201 brouard 8953: strcpy(model,modeltemp);
1.197 brouard 8954: }
8955: }
1.199 brouard 8956: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 8957: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 8958: }
8959: /* 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); */
8960: /* numlinepar=numlinepar+3; /\* In general *\/ */
8961: /* 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 8962: 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);
8963: 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 8964: fflush(ficlog);
1.190 brouard 8965: /* if(model[0]=='#'|| model[0]== '\0'){ */
8966: if(model[0]=='#'){
1.187 brouard 8967: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
8968: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
8969: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
8970: if(mle != -1){
8971: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
8972: exit(1);
8973: }
8974: }
1.126 brouard 8975: while((c=getc(ficpar))=='#' && c!= EOF){
8976: ungetc(c,ficpar);
8977: fgets(line, MAXLINE, ficpar);
8978: numlinepar++;
1.195 brouard 8979: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
8980: z[0]=line[1];
8981: }
8982: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 8983: fputs(line, stdout);
8984: //puts(line);
1.126 brouard 8985: fputs(line,ficparo);
8986: fputs(line,ficlog);
8987: }
8988: ungetc(c,ficpar);
8989:
8990:
1.145 brouard 8991: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.225 brouard 8992: coqvar=matrix(1,nqv,1,n); /**< Fixed quantitative covariate */
8993: cotvar=ma3x(1,maxwav,1,ntv,1,n); /**< Time varying covariate */
8994: cotqvar=ma3x(1,maxwav,1,nqtv,1,n); /**< Time varying quantitative covariate */
1.136 brouard 8995: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
8996: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
8997: v1+v2*age+v2*v3 makes cptcovn = 3
8998: */
8999: if (strlen(model)>1)
1.187 brouard 9000: 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 9001: else
1.187 brouard 9002: ncovmodel=2; /* Constant and age */
1.133 brouard 9003: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
9004: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 9005: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
9006: 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);
9007: 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);
9008: fflush(stdout);
9009: fclose (ficlog);
9010: goto end;
9011: }
1.126 brouard 9012: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9013: delti=delti3[1][1];
9014: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
9015: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
9016: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 9017: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
9018: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9019: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9020: fclose (ficparo);
9021: fclose (ficlog);
9022: goto end;
9023: exit(0);
1.220 brouard 9024: } else if(mle==-5) { /* Main Wizard */
1.126 brouard 9025: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 9026: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
9027: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 9028: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9029: matcov=matrix(1,npar,1,npar);
1.203 brouard 9030: hess=matrix(1,npar,1,npar);
1.220 brouard 9031: } else{ /* Begin of mle != -1 or -5 */
1.145 brouard 9032: /* Read guessed parameters */
1.126 brouard 9033: /* Reads comments: lines beginning with '#' */
9034: while((c=getc(ficpar))=='#' && c!= EOF){
9035: ungetc(c,ficpar);
9036: fgets(line, MAXLINE, ficpar);
9037: numlinepar++;
1.141 brouard 9038: fputs(line,stdout);
1.126 brouard 9039: fputs(line,ficparo);
9040: fputs(line,ficlog);
9041: }
9042: ungetc(c,ficpar);
9043:
9044: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
9045: for(i=1; i <=nlstate; i++){
1.220 brouard 9046: j=0;
1.126 brouard 9047: for(jj=1; jj <=nlstate+ndeath; jj++){
1.220 brouard 9048: if(jj==i) continue;
9049: j++;
9050: fscanf(ficpar,"%1d%1d",&i1,&j1);
9051: if ((i1 != i) || (j1 != jj)){
9052: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
1.126 brouard 9053: It might be a problem of design; if ncovcol and the model are correct\n \
9054: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
1.220 brouard 9055: exit(1);
9056: }
9057: fprintf(ficparo,"%1d%1d",i1,j1);
9058: if(mle==1)
9059: printf("%1d%1d",i,jj);
9060: fprintf(ficlog,"%1d%1d",i,jj);
9061: for(k=1; k<=ncovmodel;k++){
9062: fscanf(ficpar," %lf",¶m[i][j][k]);
9063: if(mle==1){
9064: printf(" %lf",param[i][j][k]);
9065: fprintf(ficlog," %lf",param[i][j][k]);
9066: }
9067: else
9068: fprintf(ficlog," %lf",param[i][j][k]);
9069: fprintf(ficparo," %lf",param[i][j][k]);
9070: }
9071: fscanf(ficpar,"\n");
9072: numlinepar++;
9073: if(mle==1)
9074: printf("\n");
9075: fprintf(ficlog,"\n");
9076: fprintf(ficparo,"\n");
1.126 brouard 9077: }
9078: }
9079: fflush(ficlog);
9080:
1.145 brouard 9081: /* Reads scales values */
1.126 brouard 9082: p=param[1][1];
9083:
9084: /* Reads comments: lines beginning with '#' */
9085: while((c=getc(ficpar))=='#' && c!= EOF){
9086: ungetc(c,ficpar);
9087: fgets(line, MAXLINE, ficpar);
9088: numlinepar++;
1.141 brouard 9089: fputs(line,stdout);
1.126 brouard 9090: fputs(line,ficparo);
9091: fputs(line,ficlog);
9092: }
9093: ungetc(c,ficpar);
9094:
9095: for(i=1; i <=nlstate; i++){
9096: for(j=1; j <=nlstate+ndeath-1; j++){
1.220 brouard 9097: fscanf(ficpar,"%1d%1d",&i1,&j1);
9098: if ( (i1-i) * (j1-j) != 0){
9099: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
9100: exit(1);
9101: }
9102: printf("%1d%1d",i,j);
9103: fprintf(ficparo,"%1d%1d",i1,j1);
9104: fprintf(ficlog,"%1d%1d",i1,j1);
9105: for(k=1; k<=ncovmodel;k++){
9106: fscanf(ficpar,"%le",&delti3[i][j][k]);
9107: printf(" %le",delti3[i][j][k]);
9108: fprintf(ficparo," %le",delti3[i][j][k]);
9109: fprintf(ficlog," %le",delti3[i][j][k]);
9110: }
9111: fscanf(ficpar,"\n");
9112: numlinepar++;
9113: printf("\n");
9114: fprintf(ficparo,"\n");
9115: fprintf(ficlog,"\n");
1.126 brouard 9116: }
9117: }
9118: fflush(ficlog);
1.220 brouard 9119:
1.145 brouard 9120: /* Reads covariance matrix */
1.126 brouard 9121: delti=delti3[1][1];
1.220 brouard 9122:
9123:
1.126 brouard 9124: /* 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 9125:
1.126 brouard 9126: /* Reads comments: lines beginning with '#' */
9127: while((c=getc(ficpar))=='#' && c!= EOF){
9128: ungetc(c,ficpar);
9129: fgets(line, MAXLINE, ficpar);
9130: numlinepar++;
1.141 brouard 9131: fputs(line,stdout);
1.126 brouard 9132: fputs(line,ficparo);
9133: fputs(line,ficlog);
9134: }
9135: ungetc(c,ficpar);
1.220 brouard 9136:
1.126 brouard 9137: matcov=matrix(1,npar,1,npar);
1.203 brouard 9138: hess=matrix(1,npar,1,npar);
1.131 brouard 9139: for(i=1; i <=npar; i++)
9140: for(j=1; j <=npar; j++) matcov[i][j]=0.;
1.220 brouard 9141:
1.194 brouard 9142: /* Scans npar lines */
1.126 brouard 9143: for(i=1; i <=npar; i++){
1.226 ! brouard 9144: count=fscanf(ficpar,"%1d%1d%d",&i1,&j1,&jk);
1.194 brouard 9145: if(count != 3){
1.226 ! brouard 9146: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9147: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9148: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 ! brouard 9149: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
1.194 brouard 9150: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
9151: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
1.226 ! brouard 9152: exit(1);
1.220 brouard 9153: }else{
1.226 ! brouard 9154: if(mle==1)
! 9155: printf("%1d%1d%d",i1,j1,jk);
! 9156: }
! 9157: fprintf(ficlog,"%1d%1d%d",i1,j1,jk);
! 9158: fprintf(ficparo,"%1d%1d%d",i1,j1,jk);
1.126 brouard 9159: for(j=1; j <=i; j++){
1.226 ! brouard 9160: fscanf(ficpar," %le",&matcov[i][j]);
! 9161: if(mle==1){
! 9162: printf(" %.5le",matcov[i][j]);
! 9163: }
! 9164: fprintf(ficlog," %.5le",matcov[i][j]);
! 9165: fprintf(ficparo," %.5le",matcov[i][j]);
1.126 brouard 9166: }
9167: fscanf(ficpar,"\n");
9168: numlinepar++;
9169: if(mle==1)
1.220 brouard 9170: printf("\n");
1.126 brouard 9171: fprintf(ficlog,"\n");
9172: fprintf(ficparo,"\n");
9173: }
1.194 brouard 9174: /* End of read covariance matrix npar lines */
1.126 brouard 9175: for(i=1; i <=npar; i++)
9176: for(j=i+1;j<=npar;j++)
1.226 ! brouard 9177: matcov[i][j]=matcov[j][i];
1.126 brouard 9178:
9179: if(mle==1)
9180: printf("\n");
9181: fprintf(ficlog,"\n");
9182:
9183: fflush(ficlog);
9184:
9185: /*-------- Rewriting parameter file ----------*/
9186: strcpy(rfileres,"r"); /* "Rparameterfile */
9187: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
9188: strcat(rfileres,"."); /* */
9189: strcat(rfileres,optionfilext); /* Other files have txt extension */
9190: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 9191: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
9192: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 9193: }
9194: fprintf(ficres,"#%s\n",version);
9195: } /* End of mle != -3 */
1.218 brouard 9196:
1.186 brouard 9197: /* Main data
9198: */
1.126 brouard 9199: n= lastobs;
9200: num=lvector(1,n);
9201: moisnais=vector(1,n);
9202: annais=vector(1,n);
9203: moisdc=vector(1,n);
9204: andc=vector(1,n);
1.220 brouard 9205: weight=vector(1,n);
1.126 brouard 9206: agedc=vector(1,n);
9207: cod=ivector(1,n);
1.220 brouard 9208: for(i=1;i<=n;i++){
9209: num[i]=0;
9210: moisnais[i]=0;
9211: annais[i]=0;
9212: moisdc[i]=0;
9213: andc[i]=0;
9214: agedc[i]=0;
9215: cod[i]=0;
9216: weight[i]=1.0; /* Equal weights, 1 by default */
9217: }
1.126 brouard 9218: mint=matrix(1,maxwav,1,n);
9219: anint=matrix(1,maxwav,1,n);
1.131 brouard 9220: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 9221: tab=ivector(1,NCOVMAX);
1.144 brouard 9222: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 9223: 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 9224:
1.136 brouard 9225: /* Reads data from file datafile */
9226: if (readdata(datafile, firstobs, lastobs, &imx)==1)
9227: goto end;
9228:
9229: /* Calculation of the number of parameters from char model */
1.137 brouard 9230: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
9231: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
9232: k=3 V4 Tvar[k=3]= 4 (from V4)
9233: k=2 V1 Tvar[k=2]= 1 (from V1)
9234: k=1 Tvar[1]=2 (from V2)
9235: */
9236: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
1.226 ! brouard 9237: Typevar=ivector(-1,NCOVMAX); /* -1 to 2 */
! 9238: Fixed=ivector(-1,NCOVMAX); /* -1 to 3 */
! 9239: Dummy=ivector(-1,NCOVMAX); /* -1 to 3 */
1.137 brouard 9240: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
9241: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
9242: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
9243: */
9244: /* For model-covariate k tells which data-covariate to use but
9245: because this model-covariate is a construction we invent a new column
9246: ncovcol + k1
9247: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
9248: Tvar[3=V1*V4]=4+1 etc */
1.145 brouard 9249: Tprod=ivector(1,NCOVMAX); /* Gives the position of a product */
1.137 brouard 9250: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
9251: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
9252: */
1.145 brouard 9253: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
9254: 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 9255: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
9256: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 9257: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 9258: 4 covariates (3 plus signs)
9259: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
9260: */
1.136 brouard 9261:
1.186 brouard 9262: /* Main decodemodel */
9263:
1.187 brouard 9264:
1.223 brouard 9265: if(decodemodel(model, lastobs) == 1) /* In order to get Tvar[k] V4+V3+V5 p Tvar[1]@3 = {4, 3, 5}*/
1.136 brouard 9266: goto end;
9267:
1.137 brouard 9268: if((double)(lastobs-imx)/(double)imx > 1.10){
9269: nbwarn++;
9270: 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);
9271: 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);
9272: }
1.136 brouard 9273: /* if(mle==1){*/
1.137 brouard 9274: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
9275: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 9276: }
9277:
9278: /*-calculation of age at interview from date of interview and age at death -*/
9279: agev=matrix(1,maxwav,1,imx);
9280:
9281: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
9282: goto end;
9283:
1.126 brouard 9284:
1.136 brouard 9285: agegomp=(int)agemin;
9286: free_vector(moisnais,1,n);
9287: free_vector(annais,1,n);
1.126 brouard 9288: /* free_matrix(mint,1,maxwav,1,n);
9289: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 9290: /* free_vector(moisdc,1,n); */
9291: /* free_vector(andc,1,n); */
1.145 brouard 9292: /* */
9293:
1.126 brouard 9294: wav=ivector(1,imx);
1.214 brouard 9295: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
9296: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
9297: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
9298: 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.*/
9299: bh=imatrix(1,lastpass-firstpass+2,1,imx);
9300: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 9301:
9302: /* Concatenates waves */
1.214 brouard 9303: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
9304: Death is a valid wave (if date is known).
9305: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
9306: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
9307: and mw[mi+1][i]. dh depends on stepm.
9308: */
9309:
1.126 brouard 9310: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 9311: /* */
9312:
1.215 brouard 9313: free_vector(moisdc,1,n);
9314: free_vector(andc,1,n);
9315:
1.126 brouard 9316: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
9317: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
9318: ncodemax[1]=1;
1.145 brouard 9319: Ndum =ivector(-1,NCOVMAX);
1.225 brouard 9320: cptcoveff=0;
1.220 brouard 9321: if (ncovmodel-nagesqr > 2 ){ /* That is if covariate other than cst, age and age*age */
9322: tricode(&cptcoveff,Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
9323: }
9324:
9325: ncovcombmax=pow(2,cptcoveff);
9326: invalidvarcomb=ivector(1, ncovcombmax);
9327: for(i=1;i<ncovcombmax;i++)
9328: invalidvarcomb[i]=0;
9329:
1.211 brouard 9330: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 9331: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 9332: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.145 brouard 9333:
1.200 brouard 9334: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 9335: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 9336: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 9337: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
9338: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
9339: * (currently 0 or 1) in the data.
9340: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
9341: * corresponding modality (h,j).
9342: */
9343:
1.145 brouard 9344: h=0;
9345: /*if (cptcovn > 0) */
1.126 brouard 9346: m=pow(2,cptcoveff);
9347:
1.144 brouard 9348: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 9349: * For k=4 covariates, h goes from 1 to m=2**k
9350: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
9351: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 9352: * h\k 1 2 3 4
1.143 brouard 9353: *______________________________
9354: * 1 i=1 1 i=1 1 i=1 1 i=1 1
9355: * 2 2 1 1 1
9356: * 3 i=2 1 2 1 1
9357: * 4 2 2 1 1
9358: * 5 i=3 1 i=2 1 2 1
9359: * 6 2 1 2 1
9360: * 7 i=4 1 2 2 1
9361: * 8 2 2 2 1
1.197 brouard 9362: * 9 i=5 1 i=3 1 i=2 1 2
9363: * 10 2 1 1 2
9364: * 11 i=6 1 2 1 2
9365: * 12 2 2 1 2
9366: * 13 i=7 1 i=4 1 2 2
9367: * 14 2 1 2 2
9368: * 15 i=8 1 2 2 2
9369: * 16 2 2 2 2
1.143 brouard 9370: */
1.212 brouard 9371: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 9372: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
9373: * and the value of each covariate?
9374: * V1=1, V2=1, V3=2, V4=1 ?
9375: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
9376: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
9377: * In order to get the real value in the data, we use nbcode
9378: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
9379: * We are keeping this crazy system in order to be able (in the future?)
9380: * to have more than 2 values (0 or 1) for a covariate.
9381: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
9382: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
9383: * bbbbbbbb
9384: * 76543210
9385: * h-1 00000101 (6-1=5)
1.219 brouard 9386: *(h-1)>>(k-1)= 00000010 >> (2-1) = 1 right shift
1.211 brouard 9387: * &
9388: * 1 00000001 (1)
1.219 brouard 9389: * 00000000 = 1 & ((h-1) >> (k-1))
9390: * +1= 00000001 =1
1.211 brouard 9391: *
9392: * h=14, k=3 => h'=h-1=13, k'=k-1=2
9393: * h' 1101 =2^3+2^2+0x2^1+2^0
9394: * >>k' 11
9395: * & 00000001
9396: * = 00000001
9397: * +1 = 00000010=2 = codtabm(14,3)
9398: * Reverse h=6 and m=16?
9399: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
9400: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
9401: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
9402: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
9403: * V3=decodtabm(14,3,2**4)=2
9404: * h'=13 1101 =2^3+2^2+0x2^1+2^0
9405: *(h-1) >> (j-1) 0011 =13 >> 2
9406: * &1 000000001
9407: * = 000000001
9408: * +1= 000000010 =2
9409: * 2211
9410: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
9411: * V3=2
1.220 brouard 9412: * codtabm and decodtabm are identical
1.211 brouard 9413: */
9414:
1.145 brouard 9415:
9416: free_ivector(Ndum,-1,NCOVMAX);
9417:
9418:
1.126 brouard 9419:
1.186 brouard 9420: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 9421: strcpy(optionfilegnuplot,optionfilefiname);
9422: if(mle==-3)
1.201 brouard 9423: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 9424: strcat(optionfilegnuplot,".gp");
9425:
9426: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
9427: printf("Problem with file %s",optionfilegnuplot);
9428: }
9429: else{
1.204 brouard 9430: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 9431: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 9432: //fprintf(ficgp,"set missing 'NaNq'\n");
9433: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 9434: }
9435: /* fclose(ficgp);*/
1.186 brouard 9436:
9437:
9438: /* Initialisation of --------- index.htm --------*/
1.126 brouard 9439:
9440: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
9441: if(mle==-3)
1.201 brouard 9442: strcat(optionfilehtm,"-MORT_");
1.126 brouard 9443: strcat(optionfilehtm,".htm");
9444: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 9445: printf("Problem with %s \n",optionfilehtm);
9446: exit(0);
1.126 brouard 9447: }
9448:
9449: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
9450: strcat(optionfilehtmcov,"-cov.htm");
9451: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
9452: printf("Problem with %s \n",optionfilehtmcov), exit(0);
9453: }
9454: else{
9455: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
9456: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9457: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 9458: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
9459: }
9460:
1.213 brouard 9461: 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 9462: <hr size=\"2\" color=\"#EC5E5E\"> \n\
9463: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 9464: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 9465: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 9466: \n\
9467: <hr size=\"2\" color=\"#EC5E5E\">\
9468: <ul><li><h4>Parameter files</h4>\n\
9469: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
9470: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
9471: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
9472: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
9473: - Date and time at start: %s</ul>\n",\
9474: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
9475: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
9476: fileres,fileres,\
9477: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
9478: fflush(fichtm);
9479:
9480: strcpy(pathr,path);
9481: strcat(pathr,optionfilefiname);
1.184 brouard 9482: #ifdef WIN32
9483: _chdir(optionfilefiname); /* Move to directory named optionfile */
9484: #else
1.126 brouard 9485: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 9486: #endif
9487:
1.126 brouard 9488:
1.220 brouard 9489: /* Calculates basic frequencies. Computes observed prevalence at single age
9490: and for any valid combination of covariates
1.126 brouard 9491: and prints on file fileres'p'. */
1.220 brouard 9492: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx, Tvaraff, invalidvarcomb, nbcode, ncodemax,mint,anint,strstart, \
1.223 brouard 9493: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 9494:
9495: fprintf(fichtm,"\n");
9496: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
9497: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
9498: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
9499: imx,agemin,agemax,jmin,jmax,jmean);
9500: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
1.220 brouard 9501: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9502: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9503: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
9504: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 brouard 9505:
1.126 brouard 9506: /* For Powell, parameters are in a vector p[] starting at p[1]
9507: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
9508: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
9509:
9510: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 9511: /* For mortality only */
1.126 brouard 9512: if (mle==-3){
1.136 brouard 9513: ximort=matrix(1,NDIM,1,NDIM);
1.220 brouard 9514: for(i=1;i<=NDIM;i++)
9515: for(j=1;j<=NDIM;j++)
9516: ximort[i][j]=0.;
1.186 brouard 9517: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 9518: cens=ivector(1,n);
9519: ageexmed=vector(1,n);
9520: agecens=vector(1,n);
9521: dcwave=ivector(1,n);
1.223 brouard 9522:
1.126 brouard 9523: for (i=1; i<=imx; i++){
9524: dcwave[i]=-1;
9525: for (m=firstpass; m<=lastpass; m++)
1.226 ! brouard 9526: if (s[m][i]>nlstate) {
! 9527: dcwave[i]=m;
! 9528: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
! 9529: break;
! 9530: }
1.126 brouard 9531: }
1.226 ! brouard 9532:
1.126 brouard 9533: for (i=1; i<=imx; i++) {
9534: if (wav[i]>0){
1.226 ! brouard 9535: ageexmed[i]=agev[mw[1][i]][i];
! 9536: j=wav[i];
! 9537: agecens[i]=1.;
! 9538:
! 9539: if (ageexmed[i]> 1 && wav[i] > 0){
! 9540: agecens[i]=agev[mw[j][i]][i];
! 9541: cens[i]= 1;
! 9542: }else if (ageexmed[i]< 1)
! 9543: cens[i]= -1;
! 9544: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
! 9545: cens[i]=0 ;
1.126 brouard 9546: }
9547: else cens[i]=-1;
9548: }
9549:
9550: for (i=1;i<=NDIM;i++) {
9551: for (j=1;j<=NDIM;j++)
1.226 ! brouard 9552: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 9553: }
9554:
1.145 brouard 9555: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 9556: /*printf("%lf %lf", p[1], p[2]);*/
9557:
9558:
1.136 brouard 9559: #ifdef GSL
9560: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 9561: #else
1.126 brouard 9562: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 9563: #endif
1.201 brouard 9564: strcpy(filerespow,"POW-MORT_");
9565: strcat(filerespow,fileresu);
1.126 brouard 9566: if((ficrespow=fopen(filerespow,"w"))==NULL) {
9567: printf("Problem with resultfile: %s\n", filerespow);
9568: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
9569: }
1.136 brouard 9570: #ifdef GSL
9571: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 9572: #else
1.126 brouard 9573: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 9574: #endif
1.126 brouard 9575: /* for (i=1;i<=nlstate;i++)
9576: for(j=1;j<=nlstate+ndeath;j++)
9577: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
9578: */
9579: fprintf(ficrespow,"\n");
1.136 brouard 9580: #ifdef GSL
9581: /* gsl starts here */
9582: T = gsl_multimin_fminimizer_nmsimplex;
9583: gsl_multimin_fminimizer *sfm = NULL;
9584: gsl_vector *ss, *x;
9585: gsl_multimin_function minex_func;
9586:
9587: /* Initial vertex size vector */
9588: ss = gsl_vector_alloc (NDIM);
9589:
9590: if (ss == NULL){
9591: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
9592: }
9593: /* Set all step sizes to 1 */
9594: gsl_vector_set_all (ss, 0.001);
9595:
9596: /* Starting point */
1.126 brouard 9597:
1.136 brouard 9598: x = gsl_vector_alloc (NDIM);
9599:
9600: if (x == NULL){
9601: gsl_vector_free(ss);
9602: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
9603: }
9604:
9605: /* Initialize method and iterate */
9606: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 9607: /* gsl_vector_set(x, 0, 0.0268); */
9608: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 9609: gsl_vector_set(x, 0, p[1]);
9610: gsl_vector_set(x, 1, p[2]);
9611:
9612: minex_func.f = &gompertz_f;
9613: minex_func.n = NDIM;
9614: minex_func.params = (void *)&p; /* ??? */
9615:
9616: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
9617: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
9618:
9619: printf("Iterations beginning .....\n\n");
9620: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
9621:
9622: iteri=0;
9623: while (rval == GSL_CONTINUE){
9624: iteri++;
9625: status = gsl_multimin_fminimizer_iterate(sfm);
9626:
9627: if (status) printf("error: %s\n", gsl_strerror (status));
9628: fflush(0);
9629:
9630: if (status)
9631: break;
9632:
9633: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
9634: ssval = gsl_multimin_fminimizer_size (sfm);
9635:
9636: if (rval == GSL_SUCCESS)
9637: printf ("converged to a local maximum at\n");
9638:
9639: printf("%5d ", iteri);
9640: for (it = 0; it < NDIM; it++){
9641: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
9642: }
9643: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
9644: }
9645:
9646: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
9647:
9648: gsl_vector_free(x); /* initial values */
9649: gsl_vector_free(ss); /* inital step size */
9650: for (it=0; it<NDIM; it++){
9651: p[it+1]=gsl_vector_get(sfm->x,it);
9652: fprintf(ficrespow," %.12lf", p[it]);
9653: }
9654: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
9655: #endif
9656: #ifdef POWELL
9657: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
9658: #endif
1.126 brouard 9659: fclose(ficrespow);
9660:
1.203 brouard 9661: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 9662:
9663: for(i=1; i <=NDIM; i++)
9664: for(j=i+1;j<=NDIM;j++)
1.220 brouard 9665: matcov[i][j]=matcov[j][i];
1.126 brouard 9666:
9667: printf("\nCovariance matrix\n ");
1.203 brouard 9668: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 9669: for(i=1; i <=NDIM; i++) {
9670: for(j=1;j<=NDIM;j++){
1.220 brouard 9671: printf("%f ",matcov[i][j]);
9672: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 9673: }
1.203 brouard 9674: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 9675: }
9676:
9677: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 9678: for (i=1;i<=NDIM;i++) {
1.126 brouard 9679: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 9680: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
9681: }
1.126 brouard 9682: lsurv=vector(1,AGESUP);
9683: lpop=vector(1,AGESUP);
9684: tpop=vector(1,AGESUP);
9685: lsurv[agegomp]=100000;
9686:
9687: for (k=agegomp;k<=AGESUP;k++) {
9688: agemortsup=k;
9689: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
9690: }
9691:
9692: for (k=agegomp;k<agemortsup;k++)
9693: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
9694:
9695: for (k=agegomp;k<agemortsup;k++){
9696: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
9697: sumlpop=sumlpop+lpop[k];
9698: }
9699:
9700: tpop[agegomp]=sumlpop;
9701: for (k=agegomp;k<(agemortsup-3);k++){
9702: /* tpop[k+1]=2;*/
9703: tpop[k+1]=tpop[k]-lpop[k];
9704: }
9705:
9706:
9707: printf("\nAge lx qx dx Lx Tx e(x)\n");
9708: for (k=agegomp;k<(agemortsup-2);k++)
9709: 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]);
9710:
9711:
9712: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.220 brouard 9713: ageminpar=50;
9714: agemaxpar=100;
1.194 brouard 9715: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
9716: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9717: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9718: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
9719: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9720: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9721: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 9722: }else{
9723: printf("Warning! ageminpar %f and agemaxpar %f have been fixed because for simplification until it is fixed...\n\n",ageminpar,agemaxpar);
9724: 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 9725: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
1.220 brouard 9726: }
1.201 brouard 9727: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 9728: stepm, weightopt,\
9729: model,imx,p,matcov,agemortsup);
9730:
9731: free_vector(lsurv,1,AGESUP);
9732: free_vector(lpop,1,AGESUP);
9733: free_vector(tpop,1,AGESUP);
1.220 brouard 9734: free_matrix(ximort,1,NDIM,1,NDIM);
1.136 brouard 9735: free_ivector(cens,1,n);
9736: free_vector(agecens,1,n);
9737: free_ivector(dcwave,1,n);
1.220 brouard 9738: #ifdef GSL
1.136 brouard 9739: #endif
1.186 brouard 9740: } /* Endof if mle==-3 mortality only */
1.205 brouard 9741: /* Standard */
9742: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
9743: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
9744: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 9745: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 9746: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
9747: for (k=1; k<=npar;k++)
9748: printf(" %d %8.5f",k,p[k]);
9749: printf("\n");
1.205 brouard 9750: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
9751: /* mlikeli uses func not funcone */
9752: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
9753: }
9754: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
9755: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
9756: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
9757: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
9758: }
9759: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 9760: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
9761: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
9762: for (k=1; k<=npar;k++)
9763: printf(" %d %8.5f",k,p[k]);
9764: printf("\n");
9765:
9766: /*--------- results files --------------*/
1.224 brouard 9767: 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 9768:
9769:
9770: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9771: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9772: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9773: for(i=1,jk=1; i <=nlstate; i++){
9774: for(k=1; k <=(nlstate+ndeath); k++){
1.225 brouard 9775: if (k != i) {
9776: printf("%d%d ",i,k);
9777: fprintf(ficlog,"%d%d ",i,k);
9778: fprintf(ficres,"%1d%1d ",i,k);
9779: for(j=1; j <=ncovmodel; j++){
9780: printf("%12.7f ",p[jk]);
9781: fprintf(ficlog,"%12.7f ",p[jk]);
9782: fprintf(ficres,"%12.7f ",p[jk]);
9783: jk++;
9784: }
9785: printf("\n");
9786: fprintf(ficlog,"\n");
9787: fprintf(ficres,"\n");
9788: }
1.126 brouard 9789: }
9790: }
1.203 brouard 9791: if(mle != 0){
9792: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 9793: ftolhess=ftol; /* Usually correct */
1.203 brouard 9794: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
9795: 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");
9796: 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");
9797: for(i=1,jk=1; i <=nlstate; i++){
1.225 brouard 9798: for(k=1; k <=(nlstate+ndeath); k++){
9799: if (k != i) {
9800: printf("%d%d ",i,k);
9801: fprintf(ficlog,"%d%d ",i,k);
9802: for(j=1; j <=ncovmodel; j++){
9803: 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]));
9804: 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]));
9805: jk++;
9806: }
9807: printf("\n");
9808: fprintf(ficlog,"\n");
9809: }
9810: }
1.193 brouard 9811: }
1.203 brouard 9812: } /* end of hesscov and Wald tests */
1.225 brouard 9813:
1.203 brouard 9814: /* */
1.126 brouard 9815: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
9816: printf("# Scales (for hessian or gradient estimation)\n");
9817: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
9818: for(i=1,jk=1; i <=nlstate; i++){
9819: for(j=1; j <=nlstate+ndeath; j++){
1.225 brouard 9820: if (j!=i) {
9821: fprintf(ficres,"%1d%1d",i,j);
9822: printf("%1d%1d",i,j);
9823: fprintf(ficlog,"%1d%1d",i,j);
9824: for(k=1; k<=ncovmodel;k++){
9825: printf(" %.5e",delti[jk]);
9826: fprintf(ficlog," %.5e",delti[jk]);
9827: fprintf(ficres," %.5e",delti[jk]);
9828: jk++;
9829: }
9830: printf("\n");
9831: fprintf(ficlog,"\n");
9832: fprintf(ficres,"\n");
9833: }
1.126 brouard 9834: }
9835: }
9836:
9837: 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 9838: if(mle >= 1) /* To big for the screen */
1.126 brouard 9839: 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");
9840: 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");
9841: /* # 121 Var(a12)\n\ */
9842: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9843: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9844: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9845: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9846: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9847: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9848: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9849:
9850:
9851: /* Just to have a covariance matrix which will be more understandable
9852: even is we still don't want to manage dictionary of variables
9853: */
9854: for(itimes=1;itimes<=2;itimes++){
9855: jj=0;
9856: for(i=1; i <=nlstate; i++){
1.225 brouard 9857: for(j=1; j <=nlstate+ndeath; j++){
9858: if(j==i) continue;
9859: for(k=1; k<=ncovmodel;k++){
9860: jj++;
9861: ca[0]= k+'a'-1;ca[1]='\0';
9862: if(itimes==1){
9863: if(mle>=1)
9864: printf("#%1d%1d%d",i,j,k);
9865: fprintf(ficlog,"#%1d%1d%d",i,j,k);
9866: fprintf(ficres,"#%1d%1d%d",i,j,k);
9867: }else{
9868: if(mle>=1)
9869: printf("%1d%1d%d",i,j,k);
9870: fprintf(ficlog,"%1d%1d%d",i,j,k);
9871: fprintf(ficres,"%1d%1d%d",i,j,k);
9872: }
9873: ll=0;
9874: for(li=1;li <=nlstate; li++){
9875: for(lj=1;lj <=nlstate+ndeath; lj++){
9876: if(lj==li) continue;
9877: for(lk=1;lk<=ncovmodel;lk++){
9878: ll++;
9879: if(ll<=jj){
9880: cb[0]= lk +'a'-1;cb[1]='\0';
9881: if(ll<jj){
9882: if(itimes==1){
9883: if(mle>=1)
9884: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9885: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9886: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9887: }else{
9888: if(mle>=1)
9889: printf(" %.5e",matcov[jj][ll]);
9890: fprintf(ficlog," %.5e",matcov[jj][ll]);
9891: fprintf(ficres," %.5e",matcov[jj][ll]);
9892: }
9893: }else{
9894: if(itimes==1){
9895: if(mle>=1)
9896: printf(" Var(%s%1d%1d)",ca,i,j);
9897: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
9898: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
9899: }else{
9900: if(mle>=1)
9901: printf(" %.7e",matcov[jj][ll]);
9902: fprintf(ficlog," %.7e",matcov[jj][ll]);
9903: fprintf(ficres," %.7e",matcov[jj][ll]);
9904: }
9905: }
9906: }
9907: } /* end lk */
9908: } /* end lj */
9909: } /* end li */
9910: if(mle>=1)
9911: printf("\n");
9912: fprintf(ficlog,"\n");
9913: fprintf(ficres,"\n");
9914: numlinepar++;
9915: } /* end k*/
9916: } /*end j */
1.126 brouard 9917: } /* end i */
9918: } /* end itimes */
9919:
9920: fflush(ficlog);
9921: fflush(ficres);
1.225 brouard 9922: while(fgets(line, MAXLINE, ficpar)) {
9923: /* If line starts with a # it is a comment */
9924: if (line[0] == '#') {
9925: numlinepar++;
9926: fputs(line,stdout);
9927: fputs(line,ficparo);
9928: fputs(line,ficlog);
9929: continue;
9930: }else
9931: break;
9932: }
9933:
1.209 brouard 9934: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
9935: /* ungetc(c,ficpar); */
9936: /* fgets(line, MAXLINE, ficpar); */
9937: /* fputs(line,stdout); */
9938: /* fputs(line,ficparo); */
9939: /* } */
9940: /* ungetc(c,ficpar); */
1.126 brouard 9941:
9942: estepm=0;
1.209 brouard 9943: 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 9944:
9945: if (num_filled != 6) {
9946: 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);
9947: 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);
9948: goto end;
9949: }
9950: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
9951: }
9952: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
9953: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
9954:
1.209 brouard 9955: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 9956: if (estepm==0 || estepm < stepm) estepm=stepm;
9957: if (fage <= 2) {
9958: bage = ageminpar;
9959: fage = agemaxpar;
9960: }
9961:
9962: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 9963: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
9964: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.220 brouard 9965:
1.186 brouard 9966: /* Other stuffs, more or less useful */
1.126 brouard 9967: while((c=getc(ficpar))=='#' && c!= EOF){
9968: ungetc(c,ficpar);
9969: fgets(line, MAXLINE, ficpar);
1.141 brouard 9970: fputs(line,stdout);
1.126 brouard 9971: fputs(line,ficparo);
9972: }
9973: ungetc(c,ficpar);
9974:
9975: 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);
9976: 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);
9977: 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);
9978: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
9979: 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);
9980:
9981: while((c=getc(ficpar))=='#' && c!= EOF){
9982: ungetc(c,ficpar);
9983: fgets(line, MAXLINE, ficpar);
1.141 brouard 9984: fputs(line,stdout);
1.126 brouard 9985: fputs(line,ficparo);
9986: }
9987: ungetc(c,ficpar);
9988:
9989:
9990: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
9991: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
9992:
9993: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 9994: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 9995: fprintf(ficparo,"pop_based=%d\n",popbased);
9996: fprintf(ficres,"pop_based=%d\n",popbased);
9997:
9998: while((c=getc(ficpar))=='#' && c!= EOF){
9999: ungetc(c,ficpar);
10000: fgets(line, MAXLINE, ficpar);
1.141 brouard 10001: fputs(line,stdout);
1.126 brouard 10002: fputs(line,ficparo);
10003: }
10004: ungetc(c,ficpar);
10005:
10006: 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);
10007: 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);
10008: 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);
10009: 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);
10010: 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);
10011: /* day and month of proj2 are not used but only year anproj2.*/
10012:
1.217 brouard 10013: while((c=getc(ficpar))=='#' && c!= EOF){
10014: ungetc(c,ficpar);
10015: fgets(line, MAXLINE, ficpar);
10016: fputs(line,stdout);
10017: fputs(line,ficparo);
10018: }
10019: ungetc(c,ficpar);
10020:
10021: 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 10022: 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);
10023: 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);
10024: 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 10025: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 10026:
10027:
1.220 brouard 10028: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
1.145 brouard 10029: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 10030:
10031: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 10032: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
1.220 brouard 10033: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10034: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10035: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10036: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
1.194 brouard 10037: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
10038: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
1.220 brouard 10039: }else{
1.218 brouard 10040: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.220 brouard 10041: }
10042: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt, \
1.225 brouard 10043: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
10044: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.220 brouard 10045:
1.225 brouard 10046: /*------------ free_vector -------------*/
10047: /* chdir(path); */
1.220 brouard 10048:
1.215 brouard 10049: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
10050: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
10051: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
10052: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 10053: free_lvector(num,1,n);
10054: free_vector(agedc,1,n);
10055: /*free_matrix(covar,0,NCOVMAX,1,n);*/
10056: /*free_matrix(covar,1,NCOVMAX,1,n);*/
10057: fclose(ficparo);
10058: fclose(ficres);
1.220 brouard 10059:
10060:
1.186 brouard 10061: /* Other results (useful)*/
1.220 brouard 10062:
10063:
1.126 brouard 10064: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 10065: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
10066: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 10067: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 10068: fclose(ficrespl);
10069:
10070: /*------------- h Pij x at various ages ------------*/
1.180 brouard 10071: /*#include "hpijx.h"*/
10072: hPijx(p, bage, fage);
1.145 brouard 10073: fclose(ficrespij);
1.126 brouard 10074:
1.220 brouard 10075: /* ncovcombmax= pow(2,cptcoveff); */
1.219 brouard 10076: /*-------------- Variance of one-step probabilities---*/
1.145 brouard 10077: k=1;
1.126 brouard 10078: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
10079:
1.219 brouard 10080: /* Prevalence for each covariates in probs[age][status][cov] */
1.218 brouard 10081: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 10082: for(i=1;i<=AGESUP;i++)
1.219 brouard 10083: for(j=1;j<=nlstate+ndeath;j++) /* ndeath is useless but a necessity to be compared with mobaverages */
1.225 brouard 10084: for(k=1;k<=ncovcombmax;k++)
10085: probs[i][j][k]=0.;
1.219 brouard 10086: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
10087: if (mobilav!=0 ||mobilavproj !=0 ) {
10088: mobaverages= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
10089: for(i=1;i<=AGESUP;i++)
10090: for(j=1;j<=nlstate;j++)
10091: for(k=1;k<=ncovcombmax;k++)
10092: mobaverages[i][j][k]=0.;
10093: mobaverage=mobaverages;
10094: if (mobilav!=0) {
1.218 brouard 10095: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
10096: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
10097: printf(" Error in movingaverage mobilav=%d\n",mobilav);
10098: }
1.219 brouard 10099: }
10100: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
10101: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
10102: else if (mobilavproj !=0) {
1.218 brouard 10103: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
10104: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
10105: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
10106: }
1.219 brouard 10107: }
10108: }/* end if moving average */
10109:
1.126 brouard 10110: /*---------- Forecasting ------------------*/
10111: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
10112: if(prevfcast==1){
10113: /* if(stepm ==1){*/
1.225 brouard 10114: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 10115: }
1.217 brouard 10116: if(backcast==1){
1.219 brouard 10117: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10118: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10119: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
10120:
10121: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
10122:
10123: bprlim=matrix(1,nlstate,1,nlstate);
10124: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
10125: fclose(ficresplb);
10126:
1.222 brouard 10127: hBijx(p, bage, fage, mobaverage);
10128: fclose(ficrespijb);
1.219 brouard 10129: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
10130:
10131: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj,
1.225 brouard 10132: bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
1.219 brouard 10133: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10134: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10135: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
10136: }
1.217 brouard 10137:
1.186 brouard 10138:
10139: /* ------ Other prevalence ratios------------ */
1.126 brouard 10140:
1.215 brouard 10141: free_ivector(wav,1,imx);
10142: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
10143: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
10144: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 brouard 10145:
10146:
1.127 brouard 10147: /*---------- Health expectancies, no variances ------------*/
1.218 brouard 10148:
1.201 brouard 10149: strcpy(filerese,"E_");
10150: strcat(filerese,fileresu);
1.126 brouard 10151: if((ficreseij=fopen(filerese,"w"))==NULL) {
10152: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10153: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
10154: }
1.208 brouard 10155: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
10156: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.219 brouard 10157:
1.225 brouard 10158: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.219 brouard 10159: fprintf(ficreseij,"\n#****** ");
1.225 brouard 10160: for(j=1;j<=cptcoveff;j++) {
1.220 brouard 10161: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.219 brouard 10162: }
10163: fprintf(ficreseij,"******\n");
10164:
10165: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10166: oldm=oldms;savm=savms;
10167: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
1.127 brouard 10168:
1.219 brouard 10169: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.127 brouard 10170: }
10171: fclose(ficreseij);
1.208 brouard 10172: printf("done evsij\n");fflush(stdout);
10173: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 brouard 10174:
1.127 brouard 10175: /*---------- Health expectancies and variances ------------*/
1.218 brouard 10176:
10177:
1.201 brouard 10178: strcpy(filerest,"T_");
10179: strcat(filerest,fileresu);
1.127 brouard 10180: if((ficrest=fopen(filerest,"w"))==NULL) {
10181: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
10182: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
10183: }
1.208 brouard 10184: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
10185: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 brouard 10186:
1.126 brouard 10187:
1.201 brouard 10188: strcpy(fileresstde,"STDE_");
10189: strcat(fileresstde,fileresu);
1.126 brouard 10190: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
10191: printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
10192: fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
10193: }
1.208 brouard 10194: printf(" Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
10195: fprintf(ficlog," Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 10196:
1.201 brouard 10197: strcpy(filerescve,"CVE_");
10198: strcat(filerescve,fileresu);
1.126 brouard 10199: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
10200: printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
10201: fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
10202: }
1.208 brouard 10203: printf(" Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
10204: fprintf(ficlog," Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 10205:
1.201 brouard 10206: strcpy(fileresv,"V_");
10207: strcat(fileresv,fileresu);
1.126 brouard 10208: if((ficresvij=fopen(fileresv,"w"))==NULL) {
10209: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
10210: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
10211: }
1.208 brouard 10212: printf(" Computing Variance-covariance of DFLEs: file '%s' ... ", fileresv);fflush(stdout);
10213: fprintf(ficlog," Computing Variance-covariance of DFLEs: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 10214:
1.145 brouard 10215: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10216: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
10217:
1.225 brouard 10218: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.208 brouard 10219: fprintf(ficrest,"\n#****** ");
1.225 brouard 10220: for(j=1;j<=cptcoveff;j++)
1.218 brouard 10221: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10222: fprintf(ficrest,"******\n");
10223:
10224: fprintf(ficresstdeij,"\n#****** ");
10225: fprintf(ficrescveij,"\n#****** ");
1.225 brouard 10226: for(j=1;j<=cptcoveff;j++) {
1.218 brouard 10227: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10228: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10229: }
10230: fprintf(ficresstdeij,"******\n");
10231: fprintf(ficrescveij,"******\n");
10232:
10233: fprintf(ficresvij,"\n#****** ");
1.225 brouard 10234: for(j=1;j<=cptcoveff;j++)
1.218 brouard 10235: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 10236: fprintf(ficresvij,"******\n");
10237:
10238: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10239: oldm=oldms;savm=savms;
10240: printf(" cvevsij %d, ",k);
10241: fprintf(ficlog, " cvevsij %d, ",k);
10242: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
10243: printf(" end cvevsij \n ");
10244: fprintf(ficlog, " end cvevsij \n ");
10245:
10246: /*
10247: */
10248: /* goto endfree; */
10249:
10250: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
10251: pstamp(ficrest);
10252:
10253:
10254: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.220 brouard 10255: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
10256: cptcod= 0; /* To be deleted */
10257: printf("varevsij %d \n",vpopbased);
10258: fprintf(ficlog, "varevsij %d \n",vpopbased);
10259: 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 */
10260: 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 ");
10261: if(vpopbased==1)
10262: 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);
10263: else
10264: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
10265: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
10266: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
10267: fprintf(ficrest,"\n");
10268: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
10269: epj=vector(1,nlstate+1);
10270: printf("Computing age specific period (stable) prevalences in each health state \n");
10271: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
10272: for(age=bage; age <=fage ;age++){
10273: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k); /*ZZ Is it the correct prevalim */
10274: if (vpopbased==1) {
10275: if(mobilav ==0){
10276: for(i=1; i<=nlstate;i++)
10277: prlim[i][i]=probs[(int)age][i][k];
10278: }else{ /* mobilav */
10279: for(i=1; i<=nlstate;i++)
10280: prlim[i][i]=mobaverage[(int)age][i][k];
10281: }
10282: }
1.219 brouard 10283:
1.220 brouard 10284: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
10285: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
10286: /* printf(" age %4.0f ",age); */
10287: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
10288: for(i=1, epj[j]=0.;i <=nlstate;i++) {
10289: epj[j] += prlim[i][i]*eij[i][j][(int)age];
10290: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
10291: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
10292: }
10293: epj[nlstate+1] +=epj[j];
10294: }
10295: /* printf(" age %4.0f \n",age); */
1.219 brouard 10296:
1.220 brouard 10297: for(i=1, vepp=0.;i <=nlstate;i++)
10298: for(j=1;j <=nlstate;j++)
10299: vepp += vareij[i][j][(int)age];
10300: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
10301: for(j=1;j <=nlstate;j++){
10302: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
10303: }
10304: fprintf(ficrest,"\n");
10305: }
1.208 brouard 10306: } /* End vpopbased */
10307: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10308: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
10309: free_vector(epj,1,nlstate+1);
10310: printf("done \n");fflush(stdout);
10311: fprintf(ficlog,"done\n");fflush(ficlog);
10312:
1.145 brouard 10313: /*}*/
1.208 brouard 10314: } /* End k */
1.126 brouard 10315: free_vector(weight,1,n);
1.145 brouard 10316: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.126 brouard 10317: free_imatrix(s,1,maxwav+1,1,n);
10318: free_matrix(anint,1,maxwav,1,n);
10319: free_matrix(mint,1,maxwav,1,n);
10320: free_ivector(cod,1,n);
10321: free_ivector(tab,1,NCOVMAX);
10322: fclose(ficresstdeij);
10323: fclose(ficrescveij);
10324: fclose(ficresvij);
10325: fclose(ficrest);
1.208 brouard 10326: printf("done Health expectancies\n");fflush(stdout);
10327: fprintf(ficlog,"done Health expectancies\n");fflush(ficlog);
1.126 brouard 10328: fclose(ficpar);
10329:
10330: /*------- Variance of period (stable) prevalence------*/
10331:
1.201 brouard 10332: strcpy(fileresvpl,"VPL_");
10333: strcat(fileresvpl,fileresu);
1.126 brouard 10334: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
10335: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
10336: exit(0);
10337: }
1.208 brouard 10338: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
10339: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.126 brouard 10340:
1.145 brouard 10341: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
10342: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
10343:
1.225 brouard 10344: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.145 brouard 10345: fprintf(ficresvpl,"\n#****** ");
1.225 brouard 10346: for(j=1;j<=cptcoveff;j++)
1.218 brouard 10347: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
10348: fprintf(ficresvpl,"******\n");
10349:
10350: varpl=matrix(1,nlstate,(int) bage, (int) fage);
10351: oldm=oldms;savm=savms;
10352: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart);
10353: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 10354: /*}*/
1.126 brouard 10355: }
1.218 brouard 10356:
1.126 brouard 10357: fclose(ficresvpl);
1.208 brouard 10358: printf("done variance-covariance of period prevalence\n");fflush(stdout);
10359: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.126 brouard 10360:
10361: /*---------- End : free ----------------*/
1.219 brouard 10362: if (mobilav!=0 ||mobilavproj !=0)
10363: 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 10364: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.220 brouard 10365: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
10366: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
1.126 brouard 10367: } /* mle==-3 arrives here for freeing */
1.164 brouard 10368: /* endfree:*/
1.126 brouard 10369: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
10370: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
10371: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
1.224 brouard 10372: free_ma3x(cotqvar,1,maxwav,1,nqtv,1,n);
1.223 brouard 10373: free_ma3x(cotvar,1,maxwav,1,ntv,1,n);
10374: free_matrix(coqvar,1,maxwav,1,n);
1.126 brouard 10375: free_matrix(covar,0,NCOVMAX,1,n);
10376: free_matrix(matcov,1,npar,1,npar);
1.203 brouard 10377: free_matrix(hess,1,npar,1,npar);
1.126 brouard 10378: /*free_vector(delti,1,npar);*/
10379: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10380: free_matrix(agev,1,maxwav,1,imx);
10381: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
10382:
1.145 brouard 10383: free_ivector(ncodemax,1,NCOVMAX);
1.192 brouard 10384: free_ivector(ncodemaxwundef,1,NCOVMAX);
1.226 ! brouard 10385: free_ivector(Dummy,-1,NCOVMAX);
! 10386: free_ivector(Fixed,-1,NCOVMAX);
1.225 brouard 10387: free_ivector(Typevar,-1,NCOVMAX);
1.145 brouard 10388: free_ivector(Tvar,1,NCOVMAX);
10389: free_ivector(Tprod,1,NCOVMAX);
10390: free_ivector(Tvaraff,1,NCOVMAX);
1.220 brouard 10391: free_ivector(invalidvarcomb,1,ncovcombmax);
1.145 brouard 10392: free_ivector(Tage,1,NCOVMAX);
1.126 brouard 10393:
10394: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
1.200 brouard 10395: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 10396: fflush(fichtm);
10397: fflush(ficgp);
10398:
10399:
10400: if((nberr >0) || (nbwarn>0)){
1.216 brouard 10401: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
10402: 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 10403: }else{
10404: printf("End of Imach\n");
10405: fprintf(ficlog,"End of Imach\n");
10406: }
10407: printf("See log file on %s\n",filelog);
10408: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 10409: /*(void) gettimeofday(&end_time,&tzp);*/
10410: rend_time = time(NULL);
10411: end_time = *localtime(&rend_time);
10412: /* tml = *localtime(&end_time.tm_sec); */
10413: strcpy(strtend,asctime(&end_time));
1.126 brouard 10414: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
10415: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 10416: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.126 brouard 10417:
1.157 brouard 10418: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
10419: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
10420: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 10421: /* printf("Total time was %d uSec.\n", total_usecs);*/
10422: /* if(fileappend(fichtm,optionfilehtm)){ */
10423: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
10424: fclose(fichtm);
10425: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
10426: fclose(fichtmcov);
10427: fclose(ficgp);
10428: fclose(ficlog);
10429: /*------ End -----------*/
10430:
10431:
10432: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 10433: #ifdef WIN32
10434: if (_chdir(pathcd) != 0)
10435: printf("Can't move to directory %s!\n",path);
10436: if(_getcwd(pathcd,MAXLINE) > 0)
10437: #else
1.126 brouard 10438: if(chdir(pathcd) != 0)
1.184 brouard 10439: printf("Can't move to directory %s!\n", path);
10440: if (getcwd(pathcd, MAXLINE) > 0)
10441: #endif
1.126 brouard 10442: printf("Current directory %s!\n",pathcd);
10443: /*strcat(plotcmd,CHARSEPARATOR);*/
10444: sprintf(plotcmd,"gnuplot");
1.157 brouard 10445: #ifdef _WIN32
1.126 brouard 10446: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
10447: #endif
10448: if(!stat(plotcmd,&info)){
1.158 brouard 10449: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10450: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 10451: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 10452: }else
10453: strcpy(pplotcmd,plotcmd);
1.157 brouard 10454: #ifdef __unix
1.126 brouard 10455: strcpy(plotcmd,GNUPLOTPROGRAM);
10456: if(!stat(plotcmd,&info)){
1.158 brouard 10457: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10458: }else
10459: strcpy(pplotcmd,plotcmd);
10460: #endif
10461: }else
10462: strcpy(pplotcmd,plotcmd);
10463:
10464: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 10465: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 10466:
10467: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 10468: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 10469: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 10470: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 10471: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 10472: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 10473: }
1.158 brouard 10474: printf(" Successful, please wait...");
1.126 brouard 10475: while (z[0] != 'q') {
10476: /* chdir(path); */
1.154 brouard 10477: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 10478: scanf("%s",z);
10479: /* if (z[0] == 'c') system("./imach"); */
10480: if (z[0] == 'e') {
1.158 brouard 10481: #ifdef __APPLE__
1.152 brouard 10482: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 10483: #elif __linux
10484: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 10485: #else
1.152 brouard 10486: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 10487: #endif
10488: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
10489: system(pplotcmd);
1.126 brouard 10490: }
10491: else if (z[0] == 'g') system(plotcmd);
10492: else if (z[0] == 'q') exit(0);
10493: }
10494: end:
10495: while (z[0] != 'q') {
1.195 brouard 10496: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 10497: scanf("%s",z);
10498: }
10499: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>