Annotation of imach/src/imach.c, revision 1.218
1.218 ! brouard 1: /* $Id: imach.c,v 1.217 2015/12/23 17:18:31 brouard Exp $
1.126 brouard 2: $State: Exp $
1.163 brouard 3: $Log: imach.c,v $
1.218 ! brouard 4: Revision 1.217 2015/12/23 17:18:31 brouard
! 5: Summary: Experimental backcast
! 6:
1.217 brouard 7: Revision 1.216 2015/12/18 17:32:11 brouard
8: Summary: 0.98r4 Warning and status=-2
9:
10: Version 0.98r4 is now:
11: - displaying an error when status is -1, date of interview unknown and date of death known;
12: - permitting a status -2 when the vital status is unknown at a known date of right truncation.
13: Older changes concerning s=-2, dating from 2005 have been supersed.
14:
1.216 brouard 15: Revision 1.215 2015/12/16 08:52:24 brouard
16: Summary: 0.98r4 working
17:
1.215 brouard 18: Revision 1.214 2015/12/16 06:57:54 brouard
19: Summary: temporary not working
20:
1.214 brouard 21: Revision 1.213 2015/12/11 18:22:17 brouard
22: Summary: 0.98r4
23:
1.213 brouard 24: Revision 1.212 2015/11/21 12:47:24 brouard
25: Summary: minor typo
26:
1.212 brouard 27: Revision 1.211 2015/11/21 12:41:11 brouard
28: Summary: 0.98r3 with some graph of projected cross-sectional
29:
30: Author: Nicolas Brouard
31:
1.211 brouard 32: Revision 1.210 2015/11/18 17:41:20 brouard
33: Summary: Start working on projected prevalences
34:
1.210 brouard 35: Revision 1.209 2015/11/17 22:12:03 brouard
36: Summary: Adding ftolpl parameter
37: Author: N Brouard
38:
39: We had difficulties to get smoothed confidence intervals. It was due
40: to the period prevalence which wasn't computed accurately. The inner
41: parameter ftolpl is now an outer parameter of the .imach parameter
42: file after estepm. If ftolpl is small 1.e-4 and estepm too,
43: computation are long.
44:
1.209 brouard 45: Revision 1.208 2015/11/17 14:31:57 brouard
46: Summary: temporary
47:
1.208 brouard 48: Revision 1.207 2015/10/27 17:36:57 brouard
49: *** empty log message ***
50:
1.207 brouard 51: Revision 1.206 2015/10/24 07:14:11 brouard
52: *** empty log message ***
53:
1.206 brouard 54: Revision 1.205 2015/10/23 15:50:53 brouard
55: Summary: 0.98r3 some clarification for graphs on likelihood contributions
56:
1.205 brouard 57: Revision 1.204 2015/10/01 16:20:26 brouard
58: Summary: Some new graphs of contribution to likelihood
59:
1.204 brouard 60: Revision 1.203 2015/09/30 17:45:14 brouard
61: Summary: looking at better estimation of the hessian
62:
63: Also a better criteria for convergence to the period prevalence And
64: therefore adding the number of years needed to converge. (The
65: prevalence in any alive state shold sum to one
66:
1.203 brouard 67: Revision 1.202 2015/09/22 19:45:16 brouard
68: Summary: Adding some overall graph on contribution to likelihood. Might change
69:
1.202 brouard 70: Revision 1.201 2015/09/15 17:34:58 brouard
71: Summary: 0.98r0
72:
73: - Some new graphs like suvival functions
74: - Some bugs fixed like model=1+age+V2.
75:
1.201 brouard 76: Revision 1.200 2015/09/09 16:53:55 brouard
77: Summary: Big bug thanks to Flavia
78:
79: Even model=1+age+V2. did not work anymore
80:
1.200 brouard 81: Revision 1.199 2015/09/07 14:09:23 brouard
82: Summary: 0.98q6 changing default small png format for graph to vectorized svg.
83:
1.199 brouard 84: Revision 1.198 2015/09/03 07:14:39 brouard
85: Summary: 0.98q5 Flavia
86:
1.198 brouard 87: Revision 1.197 2015/09/01 18:24:39 brouard
88: *** empty log message ***
89:
1.197 brouard 90: Revision 1.196 2015/08/18 23:17:52 brouard
91: Summary: 0.98q5
92:
1.196 brouard 93: Revision 1.195 2015/08/18 16:28:39 brouard
94: Summary: Adding a hack for testing purpose
95:
96: After reading the title, ftol and model lines, if the comment line has
97: a q, starting with #q, the answer at the end of the run is quit. It
98: permits to run test files in batch with ctest. The former workaround was
99: $ echo q | imach foo.imach
100:
1.195 brouard 101: Revision 1.194 2015/08/18 13:32:00 brouard
102: Summary: Adding error when the covariance matrix doesn't contain the exact number of lines required by the model line.
103:
1.194 brouard 104: Revision 1.193 2015/08/04 07:17:42 brouard
105: Summary: 0.98q4
106:
1.193 brouard 107: Revision 1.192 2015/07/16 16:49:02 brouard
108: Summary: Fixing some outputs
109:
1.192 brouard 110: Revision 1.191 2015/07/14 10:00:33 brouard
111: Summary: Some fixes
112:
1.191 brouard 113: Revision 1.190 2015/05/05 08:51:13 brouard
114: Summary: Adding digits in output parameters (7 digits instead of 6)
115:
116: Fix 1+age+.
117:
1.190 brouard 118: Revision 1.189 2015/04/30 14:45:16 brouard
119: Summary: 0.98q2
120:
1.189 brouard 121: Revision 1.188 2015/04/30 08:27:53 brouard
122: *** empty log message ***
123:
1.188 brouard 124: Revision 1.187 2015/04/29 09:11:15 brouard
125: *** empty log message ***
126:
1.187 brouard 127: Revision 1.186 2015/04/23 12:01:52 brouard
128: Summary: V1*age is working now, version 0.98q1
129:
130: Some codes had been disabled in order to simplify and Vn*age was
131: working in the optimization phase, ie, giving correct MLE parameters,
132: but, as usual, outputs were not correct and program core dumped.
133:
1.186 brouard 134: Revision 1.185 2015/03/11 13:26:42 brouard
135: Summary: Inclusion of compile and links command line for Intel Compiler
136:
1.185 brouard 137: Revision 1.184 2015/03/11 11:52:39 brouard
138: Summary: Back from Windows 8. Intel Compiler
139:
1.184 brouard 140: Revision 1.183 2015/03/10 20:34:32 brouard
141: Summary: 0.98q0, trying with directest, mnbrak fixed
142:
143: We use directest instead of original Powell test; probably no
144: incidence on the results, but better justifications;
145: We fixed Numerical Recipes mnbrak routine which was wrong and gave
146: wrong results.
147:
1.183 brouard 148: Revision 1.182 2015/02/12 08:19:57 brouard
149: Summary: Trying to keep directest which seems simpler and more general
150: Author: Nicolas Brouard
151:
1.182 brouard 152: Revision 1.181 2015/02/11 23:22:24 brouard
153: Summary: Comments on Powell added
154:
155: Author:
156:
1.181 brouard 157: Revision 1.180 2015/02/11 17:33:45 brouard
158: Summary: Finishing move from main to function (hpijx and prevalence_limit)
159:
1.180 brouard 160: Revision 1.179 2015/01/04 09:57:06 brouard
161: Summary: back to OS/X
162:
1.179 brouard 163: Revision 1.178 2015/01/04 09:35:48 brouard
164: *** empty log message ***
165:
1.178 brouard 166: Revision 1.177 2015/01/03 18:40:56 brouard
167: Summary: Still testing ilc32 on OSX
168:
1.177 brouard 169: Revision 1.176 2015/01/03 16:45:04 brouard
170: *** empty log message ***
171:
1.176 brouard 172: Revision 1.175 2015/01/03 16:33:42 brouard
173: *** empty log message ***
174:
1.175 brouard 175: Revision 1.174 2015/01/03 16:15:49 brouard
176: Summary: Still in cross-compilation
177:
1.174 brouard 178: Revision 1.173 2015/01/03 12:06:26 brouard
179: Summary: trying to detect cross-compilation
180:
1.173 brouard 181: Revision 1.172 2014/12/27 12:07:47 brouard
182: Summary: Back from Visual Studio and Intel, options for compiling for Windows XP
183:
1.172 brouard 184: Revision 1.171 2014/12/23 13:26:59 brouard
185: Summary: Back from Visual C
186:
187: Still problem with utsname.h on Windows
188:
1.171 brouard 189: Revision 1.170 2014/12/23 11:17:12 brouard
190: Summary: Cleaning some \%% back to %%
191:
192: The escape was mandatory for a specific compiler (which one?), but too many warnings.
193:
1.170 brouard 194: Revision 1.169 2014/12/22 23:08:31 brouard
195: Summary: 0.98p
196:
197: Outputs some informations on compiler used, OS etc. Testing on different platforms.
198:
1.169 brouard 199: Revision 1.168 2014/12/22 15:17:42 brouard
1.170 brouard 200: Summary: update
1.169 brouard 201:
1.168 brouard 202: Revision 1.167 2014/12/22 13:50:56 brouard
203: Summary: Testing uname and compiler version and if compiled 32 or 64
204:
205: Testing on Linux 64
206:
1.167 brouard 207: Revision 1.166 2014/12/22 11:40:47 brouard
208: *** empty log message ***
209:
1.166 brouard 210: Revision 1.165 2014/12/16 11:20:36 brouard
211: Summary: After compiling on Visual C
212:
213: * imach.c (Module): Merging 1.61 to 1.162
214:
1.165 brouard 215: Revision 1.164 2014/12/16 10:52:11 brouard
216: Summary: Merging with Visual C after suppressing some warnings for unused variables. Also fixing Saito's bug 0.98Xn
217:
218: * imach.c (Module): Merging 1.61 to 1.162
219:
1.164 brouard 220: Revision 1.163 2014/12/16 10:30:11 brouard
221: * imach.c (Module): Merging 1.61 to 1.162
222:
1.163 brouard 223: Revision 1.162 2014/09/25 11:43:39 brouard
224: Summary: temporary backup 0.99!
225:
1.162 brouard 226: Revision 1.1 2014/09/16 11:06:58 brouard
227: Summary: With some code (wrong) for nlopt
228:
229: Author:
230:
231: Revision 1.161 2014/09/15 20:41:41 brouard
232: Summary: Problem with macro SQR on Intel compiler
233:
1.161 brouard 234: Revision 1.160 2014/09/02 09:24:05 brouard
235: *** empty log message ***
236:
1.160 brouard 237: Revision 1.159 2014/09/01 10:34:10 brouard
238: Summary: WIN32
239: Author: Brouard
240:
1.159 brouard 241: Revision 1.158 2014/08/27 17:11:51 brouard
242: *** empty log message ***
243:
1.158 brouard 244: Revision 1.157 2014/08/27 16:26:55 brouard
245: Summary: Preparing windows Visual studio version
246: Author: Brouard
247:
248: In order to compile on Visual studio, time.h is now correct and time_t
249: and tm struct should be used. difftime should be used but sometimes I
250: just make the differences in raw time format (time(&now).
251: Trying to suppress #ifdef LINUX
252: Add xdg-open for __linux in order to open default browser.
253:
1.157 brouard 254: Revision 1.156 2014/08/25 20:10:10 brouard
255: *** empty log message ***
256:
1.156 brouard 257: Revision 1.155 2014/08/25 18:32:34 brouard
258: Summary: New compile, minor changes
259: Author: Brouard
260:
1.155 brouard 261: Revision 1.154 2014/06/20 17:32:08 brouard
262: Summary: Outputs now all graphs of convergence to period prevalence
263:
1.154 brouard 264: Revision 1.153 2014/06/20 16:45:46 brouard
265: Summary: If 3 live state, convergence to period prevalence on same graph
266: Author: Brouard
267:
1.153 brouard 268: Revision 1.152 2014/06/18 17:54:09 brouard
269: Summary: open browser, use gnuplot on same dir than imach if not found in the path
270:
1.152 brouard 271: Revision 1.151 2014/06/18 16:43:30 brouard
272: *** empty log message ***
273:
1.151 brouard 274: Revision 1.150 2014/06/18 16:42:35 brouard
275: Summary: If gnuplot is not in the path try on same directory than imach binary (OSX)
276: Author: brouard
277:
1.150 brouard 278: Revision 1.149 2014/06/18 15:51:14 brouard
279: Summary: Some fixes in parameter files errors
280: Author: Nicolas Brouard
281:
1.149 brouard 282: Revision 1.148 2014/06/17 17:38:48 brouard
283: Summary: Nothing new
284: Author: Brouard
285:
286: Just a new packaging for OS/X version 0.98nS
287:
1.148 brouard 288: Revision 1.147 2014/06/16 10:33:11 brouard
289: *** empty log message ***
290:
1.147 brouard 291: Revision 1.146 2014/06/16 10:20:28 brouard
292: Summary: Merge
293: Author: Brouard
294:
295: Merge, before building revised version.
296:
1.146 brouard 297: Revision 1.145 2014/06/10 21:23:15 brouard
298: Summary: Debugging with valgrind
299: Author: Nicolas Brouard
300:
301: Lot of changes in order to output the results with some covariates
302: After the Edimburgh REVES conference 2014, it seems mandatory to
303: improve the code.
304: No more memory valgrind error but a lot has to be done in order to
305: continue the work of splitting the code into subroutines.
306: Also, decodemodel has been improved. Tricode is still not
307: optimal. nbcode should be improved. Documentation has been added in
308: the source code.
309:
1.144 brouard 310: Revision 1.143 2014/01/26 09:45:38 brouard
311: Summary: Version 0.98nR (to be improved, but gives same optimization results as 0.98k. Nice, promising
312:
313: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
314: (Module): Version 0.98nR Running ok, but output format still only works for three covariates.
315:
1.143 brouard 316: Revision 1.142 2014/01/26 03:57:36 brouard
317: Summary: gnuplot changed plot w l 1 has to be changed to plot w l lt 2
318:
319: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
320:
1.142 brouard 321: Revision 1.141 2014/01/26 02:42:01 brouard
322: * imach.c (Module): Trying to merge old staffs together while being at Tokyo. Not tested...
323:
1.141 brouard 324: Revision 1.140 2011/09/02 10:37:54 brouard
325: Summary: times.h is ok with mingw32 now.
326:
1.140 brouard 327: Revision 1.139 2010/06/14 07:50:17 brouard
328: After the theft of my laptop, I probably lost some lines of codes which were not uploaded to the CVS tree.
329: I remember having already fixed agemin agemax which are pointers now but not cvs saved.
330:
1.139 brouard 331: Revision 1.138 2010/04/30 18:19:40 brouard
332: *** empty log message ***
333:
1.138 brouard 334: Revision 1.137 2010/04/29 18:11:38 brouard
335: (Module): Checking covariates for more complex models
336: than V1+V2. A lot of change to be done. Unstable.
337:
1.137 brouard 338: Revision 1.136 2010/04/26 20:30:53 brouard
339: (Module): merging some libgsl code. Fixing computation
340: of likelione (using inter/intrapolation if mle = 0) in order to
341: get same likelihood as if mle=1.
342: Some cleaning of code and comments added.
343:
1.136 brouard 344: Revision 1.135 2009/10/29 15:33:14 brouard
345: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
346:
1.135 brouard 347: Revision 1.134 2009/10/29 13:18:53 brouard
348: (Module): Now imach stops if date of birth, at least year of birth, is not given. Some cleaning of the code.
349:
1.134 brouard 350: Revision 1.133 2009/07/06 10:21:25 brouard
351: just nforces
352:
1.133 brouard 353: Revision 1.132 2009/07/06 08:22:05 brouard
354: Many tings
355:
1.132 brouard 356: Revision 1.131 2009/06/20 16:22:47 brouard
357: Some dimensions resccaled
358:
1.131 brouard 359: Revision 1.130 2009/05/26 06:44:34 brouard
360: (Module): Max Covariate is now set to 20 instead of 8. A
361: lot of cleaning with variables initialized to 0. Trying to make
362: V2+V3*age+V1+V4 strb=V3*age+V1+V4 working better.
363:
1.130 brouard 364: Revision 1.129 2007/08/31 13:49:27 lievre
365: Modification of the way of exiting when the covariate is not binary in order to see on the window the error message before exiting
366:
1.129 lievre 367: Revision 1.128 2006/06/30 13:02:05 brouard
368: (Module): Clarifications on computing e.j
369:
1.128 brouard 370: Revision 1.127 2006/04/28 18:11:50 brouard
371: (Module): Yes the sum of survivors was wrong since
372: imach-114 because nhstepm was no more computed in the age
373: loop. Now we define nhstepma in the age loop.
374: (Module): In order to speed up (in case of numerous covariates) we
375: compute health expectancies (without variances) in a first step
376: and then all the health expectancies with variances or standard
377: deviation (needs data from the Hessian matrices) which slows the
378: computation.
379: In the future we should be able to stop the program is only health
380: expectancies and graph are needed without standard deviations.
381:
1.127 brouard 382: Revision 1.126 2006/04/28 17:23:28 brouard
383: (Module): Yes the sum of survivors was wrong since
384: imach-114 because nhstepm was no more computed in the age
385: loop. Now we define nhstepma in the age loop.
386: Version 0.98h
387:
1.126 brouard 388: Revision 1.125 2006/04/04 15:20:31 lievre
389: Errors in calculation of health expectancies. Age was not initialized.
390: Forecasting file added.
391:
392: Revision 1.124 2006/03/22 17:13:53 lievre
393: Parameters are printed with %lf instead of %f (more numbers after the comma).
394: The log-likelihood is printed in the log file
395:
396: Revision 1.123 2006/03/20 10:52:43 brouard
397: * imach.c (Module): <title> changed, corresponds to .htm file
398: name. <head> headers where missing.
399:
400: * imach.c (Module): Weights can have a decimal point as for
401: English (a comma might work with a correct LC_NUMERIC environment,
402: otherwise the weight is truncated).
403: Modification of warning when the covariates values are not 0 or
404: 1.
405: Version 0.98g
406:
407: Revision 1.122 2006/03/20 09:45:41 brouard
408: (Module): Weights can have a decimal point as for
409: English (a comma might work with a correct LC_NUMERIC environment,
410: otherwise the weight is truncated).
411: Modification of warning when the covariates values are not 0 or
412: 1.
413: Version 0.98g
414:
415: Revision 1.121 2006/03/16 17:45:01 lievre
416: * imach.c (Module): Comments concerning covariates added
417:
418: * imach.c (Module): refinements in the computation of lli if
419: status=-2 in order to have more reliable computation if stepm is
420: not 1 month. Version 0.98f
421:
422: Revision 1.120 2006/03/16 15:10:38 lievre
423: (Module): refinements in the computation of lli if
424: status=-2 in order to have more reliable computation if stepm is
425: not 1 month. Version 0.98f
426:
427: Revision 1.119 2006/03/15 17:42:26 brouard
428: (Module): Bug if status = -2, the loglikelihood was
429: computed as likelihood omitting the logarithm. Version O.98e
430:
431: Revision 1.118 2006/03/14 18:20:07 brouard
432: (Module): varevsij Comments added explaining the second
433: table of variances if popbased=1 .
434: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
435: (Module): Function pstamp added
436: (Module): Version 0.98d
437:
438: Revision 1.117 2006/03/14 17:16:22 brouard
439: (Module): varevsij Comments added explaining the second
440: table of variances if popbased=1 .
441: (Module): Covariances of eij, ekl added, graphs fixed, new html link.
442: (Module): Function pstamp added
443: (Module): Version 0.98d
444:
445: Revision 1.116 2006/03/06 10:29:27 brouard
446: (Module): Variance-covariance wrong links and
447: varian-covariance of ej. is needed (Saito).
448:
449: Revision 1.115 2006/02/27 12:17:45 brouard
450: (Module): One freematrix added in mlikeli! 0.98c
451:
452: Revision 1.114 2006/02/26 12:57:58 brouard
453: (Module): Some improvements in processing parameter
454: filename with strsep.
455:
456: Revision 1.113 2006/02/24 14:20:24 brouard
457: (Module): Memory leaks checks with valgrind and:
458: datafile was not closed, some imatrix were not freed and on matrix
459: allocation too.
460:
461: Revision 1.112 2006/01/30 09:55:26 brouard
462: (Module): Back to gnuplot.exe instead of wgnuplot.exe
463:
464: Revision 1.111 2006/01/25 20:38:18 brouard
465: (Module): Lots of cleaning and bugs added (Gompertz)
466: (Module): Comments can be added in data file. Missing date values
467: can be a simple dot '.'.
468:
469: Revision 1.110 2006/01/25 00:51:50 brouard
470: (Module): Lots of cleaning and bugs added (Gompertz)
471:
472: Revision 1.109 2006/01/24 19:37:15 brouard
473: (Module): Comments (lines starting with a #) are allowed in data.
474:
475: Revision 1.108 2006/01/19 18:05:42 lievre
476: Gnuplot problem appeared...
477: To be fixed
478:
479: Revision 1.107 2006/01/19 16:20:37 brouard
480: Test existence of gnuplot in imach path
481:
482: Revision 1.106 2006/01/19 13:24:36 brouard
483: Some cleaning and links added in html output
484:
485: Revision 1.105 2006/01/05 20:23:19 lievre
486: *** empty log message ***
487:
488: Revision 1.104 2005/09/30 16:11:43 lievre
489: (Module): sump fixed, loop imx fixed, and simplifications.
490: (Module): If the status is missing at the last wave but we know
491: that the person is alive, then we can code his/her status as -2
492: (instead of missing=-1 in earlier versions) and his/her
493: contributions to the likelihood is 1 - Prob of dying from last
494: health status (= 1-p13= p11+p12 in the easiest case of somebody in
495: the healthy state at last known wave). Version is 0.98
496:
497: Revision 1.103 2005/09/30 15:54:49 lievre
498: (Module): sump fixed, loop imx fixed, and simplifications.
499:
500: Revision 1.102 2004/09/15 17:31:30 brouard
501: Add the possibility to read data file including tab characters.
502:
503: Revision 1.101 2004/09/15 10:38:38 brouard
504: Fix on curr_time
505:
506: Revision 1.100 2004/07/12 18:29:06 brouard
507: Add version for Mac OS X. Just define UNIX in Makefile
508:
509: Revision 1.99 2004/06/05 08:57:40 brouard
510: *** empty log message ***
511:
512: Revision 1.98 2004/05/16 15:05:56 brouard
513: New version 0.97 . First attempt to estimate force of mortality
514: directly from the data i.e. without the need of knowing the health
515: state at each age, but using a Gompertz model: log u =a + b*age .
516: This is the basic analysis of mortality and should be done before any
517: other analysis, in order to test if the mortality estimated from the
518: cross-longitudinal survey is different from the mortality estimated
519: from other sources like vital statistic data.
520:
521: The same imach parameter file can be used but the option for mle should be -3.
522:
1.133 brouard 523: Agnès, who wrote this part of the code, tried to keep most of the
1.126 brouard 524: former routines in order to include the new code within the former code.
525:
526: The output is very simple: only an estimate of the intercept and of
527: the slope with 95% confident intervals.
528:
529: Current limitations:
530: A) Even if you enter covariates, i.e. with the
531: model= V1+V2 equation for example, the programm does only estimate a unique global model without covariates.
532: B) There is no computation of Life Expectancy nor Life Table.
533:
534: Revision 1.97 2004/02/20 13:25:42 lievre
535: Version 0.96d. Population forecasting command line is (temporarily)
536: suppressed.
537:
538: Revision 1.96 2003/07/15 15:38:55 brouard
539: * imach.c (Repository): Errors in subdirf, 2, 3 while printing tmpout is
540: rewritten within the same printf. Workaround: many printfs.
541:
542: Revision 1.95 2003/07/08 07:54:34 brouard
543: * imach.c (Repository):
544: (Repository): Using imachwizard code to output a more meaningful covariance
545: matrix (cov(a12,c31) instead of numbers.
546:
547: Revision 1.94 2003/06/27 13:00:02 brouard
548: Just cleaning
549:
550: Revision 1.93 2003/06/25 16:33:55 brouard
551: (Module): On windows (cygwin) function asctime_r doesn't
552: exist so I changed back to asctime which exists.
553: (Module): Version 0.96b
554:
555: Revision 1.92 2003/06/25 16:30:45 brouard
556: (Module): On windows (cygwin) function asctime_r doesn't
557: exist so I changed back to asctime which exists.
558:
559: Revision 1.91 2003/06/25 15:30:29 brouard
560: * imach.c (Repository): Duplicated warning errors corrected.
561: (Repository): Elapsed time after each iteration is now output. It
562: helps to forecast when convergence will be reached. Elapsed time
563: is stamped in powell. We created a new html file for the graphs
564: concerning matrix of covariance. It has extension -cov.htm.
565:
566: Revision 1.90 2003/06/24 12:34:15 brouard
567: (Module): Some bugs corrected for windows. Also, when
568: mle=-1 a template is output in file "or"mypar.txt with the design
569: of the covariance matrix to be input.
570:
571: Revision 1.89 2003/06/24 12:30:52 brouard
572: (Module): Some bugs corrected for windows. Also, when
573: mle=-1 a template is output in file "or"mypar.txt with the design
574: of the covariance matrix to be input.
575:
576: Revision 1.88 2003/06/23 17:54:56 brouard
577: * 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.
578:
579: Revision 1.87 2003/06/18 12:26:01 brouard
580: Version 0.96
581:
582: Revision 1.86 2003/06/17 20:04:08 brouard
583: (Module): Change position of html and gnuplot routines and added
584: routine fileappend.
585:
586: Revision 1.85 2003/06/17 13:12:43 brouard
587: * imach.c (Repository): Check when date of death was earlier that
588: current date of interview. It may happen when the death was just
589: prior to the death. In this case, dh was negative and likelihood
590: was wrong (infinity). We still send an "Error" but patch by
591: assuming that the date of death was just one stepm after the
592: interview.
593: (Repository): Because some people have very long ID (first column)
594: we changed int to long in num[] and we added a new lvector for
595: memory allocation. But we also truncated to 8 characters (left
596: truncation)
597: (Repository): No more line truncation errors.
598:
599: Revision 1.84 2003/06/13 21:44:43 brouard
600: * imach.c (Repository): Replace "freqsummary" at a correct
601: place. It differs from routine "prevalence" which may be called
602: many times. Probs is memory consuming and must be used with
603: parcimony.
604: Version 0.95a3 (should output exactly the same maximization than 0.8a2)
605:
606: Revision 1.83 2003/06/10 13:39:11 lievre
607: *** empty log message ***
608:
609: Revision 1.82 2003/06/05 15:57:20 brouard
610: Add log in imach.c and fullversion number is now printed.
611:
612: */
613: /*
614: Interpolated Markov Chain
615:
616: Short summary of the programme:
617:
618: This program computes Healthy Life Expectancies from
619: cross-longitudinal data. Cross-longitudinal data consist in: -1- a
620: first survey ("cross") where individuals from different ages are
621: interviewed on their health status or degree of disability (in the
622: case of a health survey which is our main interest) -2- at least a
623: second wave of interviews ("longitudinal") which measure each change
624: (if any) in individual health status. Health expectancies are
625: computed from the time spent in each health state according to a
626: model. More health states you consider, more time is necessary to reach the
627: Maximum Likelihood of the parameters involved in the model. The
628: simplest model is the multinomial logistic model where pij is the
629: probability to be observed in state j at the second wave
630: conditional to be observed in state i at the first wave. Therefore
631: the model is: log(pij/pii)= aij + bij*age+ cij*sex + etc , where
632: 'age' is age and 'sex' is a covariate. If you want to have a more
633: complex model than "constant and age", you should modify the program
634: where the markup *Covariates have to be included here again* invites
635: you to do it. More covariates you add, slower the
636: convergence.
637:
638: The advantage of this computer programme, compared to a simple
639: multinomial logistic model, is clear when the delay between waves is not
640: identical for each individual. Also, if a individual missed an
641: intermediate interview, the information is lost, but taken into
642: account using an interpolation or extrapolation.
643:
644: hPijx is the probability to be observed in state i at age x+h
645: conditional to the observed state i at age x. The delay 'h' can be
646: split into an exact number (nh*stepm) of unobserved intermediate
647: states. This elementary transition (by month, quarter,
648: semester or year) is modelled as a multinomial logistic. The hPx
649: matrix is simply the matrix product of nh*stepm elementary matrices
650: and the contribution of each individual to the likelihood is simply
651: hPijx.
652:
653: Also this programme outputs the covariance matrix of the parameters but also
1.218 ! brouard 654: of the life expectancies. It also computes the period (stable) prevalence.
! 655:
! 656: Back prevalence and projections:
! 657: - 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)
! 658: Computes the back prevalence limit for any combination of covariate values k
! 659: at any age between ageminpar and agemaxpar and returns it in **bprlim. In the loops,
! 660: - **bprevalim(**bprlim, ***mobaverage, nlstate, *p, age, **oldm, **savm, **dnewm, **doldm, **dsavm, ftolpl, ncvyearp, k);
! 661: - hBijx Back Probability to be in state i at age x-h being in j at x
! 662: Computes for any combination of covariates k and any age between bage and fage
! 663: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 664: oldm=oldms;savm=savms;
! 665: - hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
! 666: Computes the transition matrix starting at age 'age' over
! 667: 'nhstepm*hstepm*stepm' months (i.e. until
! 668: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
! 669: nhstepm*hstepm matrices. Returns p3mat[i][j][h] after calling
! 670: p3mat[i][j][h]=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\
! 671: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
! 672:
1.133 brouard 673: Authors: Nicolas Brouard (brouard@ined.fr) and Agnès Lièvre (lievre@ined.fr).
674: Institut national d'études démographiques, Paris.
1.126 brouard 675: This software have been partly granted by Euro-REVES, a concerted action
676: from the European Union.
677: It is copyrighted identically to a GNU software product, ie programme and
678: software can be distributed freely for non commercial use. Latest version
679: can be accessed at http://euroreves.ined.fr/imach .
680:
681: Help to debug: LD_PRELOAD=/usr/local/lib/libnjamd.so ./imach foo.imach
682: or better on gdb : set env LD_PRELOAD=/usr/local/lib/libnjamd.so
683:
684: **********************************************************************/
685: /*
686: main
687: read parameterfile
688: read datafile
689: concatwav
690: freqsummary
691: if (mle >= 1)
692: mlikeli
693: print results files
694: if mle==1
695: computes hessian
696: read end of parameter file: agemin, agemax, bage, fage, estepm
697: begin-prev-date,...
698: open gnuplot file
699: open html file
1.145 brouard 700: period (stable) prevalence | pl_nom 1-1 2-2 etc by covariate
701: for age prevalim() | #****** V1=0 V2=1 V3=1 V4=0 ******
702: | 65 1 0 2 1 3 1 4 0 0.96326 0.03674
703: freexexit2 possible for memory heap.
704:
705: h Pij x | pij_nom ficrestpij
706: # Cov Agex agex+h hpijx with i,j= 1-1 1-2 1-3 2-1 2-2 2-3
707: 1 85 85 1.00000 0.00000 0.00000 0.00000 1.00000 0.00000
708: 1 85 86 0.68299 0.22291 0.09410 0.71093 0.00000 0.28907
709:
710: 1 65 99 0.00364 0.00322 0.99314 0.00350 0.00310 0.99340
711: 1 65 100 0.00214 0.00204 0.99581 0.00206 0.00196 0.99597
712: variance of p one-step probabilities varprob | prob_nom ficresprob #One-step probabilities and stand. devi in ()
713: Standard deviation of one-step probabilities | probcor_nom ficresprobcor #One-step probabilities and correlation matrix
714: Matrix of variance covariance of one-step probabilities | probcov_nom ficresprobcov #One-step probabilities and covariance matrix
715:
1.126 brouard 716: forecasting if prevfcast==1 prevforecast call prevalence()
717: health expectancies
718: Variance-covariance of DFLE
719: prevalence()
720: movingaverage()
721: varevsij()
722: if popbased==1 varevsij(,popbased)
723: total life expectancies
724: Variance of period (stable) prevalence
725: end
726: */
727:
1.187 brouard 728: /* #define DEBUG */
729: /* #define DEBUGBRENT */
1.203 brouard 730: /* #define DEBUGLINMIN */
731: /* #define DEBUGHESS */
732: #define DEBUGHESSIJ
733: /* #define LINMINORIGINAL /\* Don't use loop on scale in linmin (accepting nan)*\/ */
1.165 brouard 734: #define POWELL /* Instead of NLOPT */
1.192 brouard 735: #define POWELLF1F3 /* Skip test */
1.186 brouard 736: /* #define POWELLORIGINAL /\* Don't use Directest to decide new direction but original Powell test *\/ */
737: /* #define MNBRAKORIGINAL /\* Don't use mnbrak fix *\/ */
1.126 brouard 738:
739: #include <math.h>
740: #include <stdio.h>
741: #include <stdlib.h>
742: #include <string.h>
1.159 brouard 743:
744: #ifdef _WIN32
745: #include <io.h>
1.172 brouard 746: #include <windows.h>
747: #include <tchar.h>
1.159 brouard 748: #else
1.126 brouard 749: #include <unistd.h>
1.159 brouard 750: #endif
1.126 brouard 751:
752: #include <limits.h>
753: #include <sys/types.h>
1.171 brouard 754:
755: #if defined(__GNUC__)
756: #include <sys/utsname.h> /* Doesn't work on Windows */
757: #endif
758:
1.126 brouard 759: #include <sys/stat.h>
760: #include <errno.h>
1.159 brouard 761: /* extern int errno; */
1.126 brouard 762:
1.157 brouard 763: /* #ifdef LINUX */
764: /* #include <time.h> */
765: /* #include "timeval.h" */
766: /* #else */
767: /* #include <sys/time.h> */
768: /* #endif */
769:
1.126 brouard 770: #include <time.h>
771:
1.136 brouard 772: #ifdef GSL
773: #include <gsl/gsl_errno.h>
774: #include <gsl/gsl_multimin.h>
775: #endif
776:
1.167 brouard 777:
1.162 brouard 778: #ifdef NLOPT
779: #include <nlopt.h>
780: typedef struct {
781: double (* function)(double [] );
782: } myfunc_data ;
783: #endif
784:
1.126 brouard 785: /* #include <libintl.h> */
786: /* #define _(String) gettext (String) */
787:
1.141 brouard 788: #define MAXLINE 1024 /* Was 256. Overflow with 312 with 2 states and 4 covariates. Should be ok */
1.126 brouard 789:
790: #define GNUPLOTPROGRAM "gnuplot"
791: /*#define GNUPLOTPROGRAM "..\\gp37mgw\\wgnuplot"*/
792: #define FILENAMELENGTH 132
793:
794: #define GLOCK_ERROR_NOPATH -1 /* empty path */
795: #define GLOCK_ERROR_GETCWD -2 /* cannot get cwd */
796:
1.144 brouard 797: #define MAXPARM 128 /**< Maximum number of parameters for the optimization */
798: #define NPARMAX 64 /**< (nlstate+ndeath-1)*nlstate*ncovmodel */
1.126 brouard 799:
800: #define NINTERVMAX 8
1.144 brouard 801: #define NLSTATEMAX 8 /**< Maximum number of live states (for func) */
802: #define NDEATHMAX 8 /**< Maximum number of dead states (for func) */
803: #define NCOVMAX 20 /**< Maximum number of covariates, including generated covariates V1*V2 */
1.197 brouard 804: #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.211 brouard 805: /*#define decodtabm(h,k,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (k-1)) & 1) +1 : -1)*/
806: #define decodtabm(h,k,cptcoveff) (((h-1) >> (k-1)) & 1) +1
1.126 brouard 807: #define MAXN 20000
1.144 brouard 808: #define YEARM 12. /**< Number of months per year */
1.218 ! brouard 809: /* #define AGESUP 130 */
! 810: #define AGESUP 150
! 811: #define AGEMARGE 25 /* Marge for agemin and agemax for(iage=agemin-AGEMARGE; iage <= agemax+3+AGEMARGE; iage++) */
1.126 brouard 812: #define AGEBASE 40
1.194 brouard 813: #define AGEOVERFLOW 1.e20
1.164 brouard 814: #define AGEGOMP 10 /**< Minimal age for Gompertz adjustment */
1.157 brouard 815: #ifdef _WIN32
816: #define DIRSEPARATOR '\\'
817: #define CHARSEPARATOR "\\"
818: #define ODIRSEPARATOR '/'
819: #else
1.126 brouard 820: #define DIRSEPARATOR '/'
821: #define CHARSEPARATOR "/"
822: #define ODIRSEPARATOR '\\'
823: #endif
824:
1.218 ! brouard 825: /* $Id: imach.c,v 1.217 2015/12/23 17:18:31 brouard Exp $ */
1.126 brouard 826: /* $State: Exp $ */
1.196 brouard 827: #include "version.h"
828: char version[]=__IMACH_VERSION__;
1.204 brouard 829: char copyright[]="October 2015,INED-EUROREVES-Institut de longevite-Japan Society for the Promotion of Science (Grant-in-Aid for Scientific Research 25293121), Intel Software 2015";
1.218 ! brouard 830: char fullversion[]="$Revision: 1.217 $ $Date: 2015/12/23 17:18:31 $";
1.126 brouard 831: char strstart[80];
832: char optionfilext[10], optionfilefiname[FILENAMELENGTH];
1.130 brouard 833: int erreur=0, nberr=0, nbwarn=0; /* Error number, number of errors number of warnings */
1.187 brouard 834: int nagesqr=0, nforce=0; /* nagesqr=1 if model is including age*age, number of forces */
1.145 brouard 835: /* Number of covariates model=V2+V1+ V3*age+V2*V4 */
836: int cptcovn=0; /**< cptcovn number of covariates added in the model (excepting constant and age and age*product) */
837: int cptcovt=0; /**< cptcovt number of covariates added in the model (excepting constant and age) */
838: int cptcovs=0; /**< cptcovs number of simple covariates V2+V1 =2 */
839: int cptcovage=0; /**< Number of covariates with age: V3*age only =1 */
840: int cptcovprodnoage=0; /**< Number of covariate products without age */
841: int cptcoveff=0; /* Total number of covariates to vary for printing results */
842: int cptcov=0; /* Working variable */
1.218 ! brouard 843: int ncovcombmax=NCOVMAX; /* Maximum calculated number of covariate combination = pow(2, cptcoveff) */
1.126 brouard 844: int npar=NPARMAX;
845: int nlstate=2; /* Number of live states */
846: int ndeath=1; /* Number of dead states */
1.130 brouard 847: int ncovmodel=0, ncovcol=0; /* Total number of covariables including constant a12*1 +b12*x ncovmodel=2 */
1.126 brouard 848: int popbased=0;
849:
850: int *wav; /* Number of waves for this individuual 0 is possible */
1.130 brouard 851: int maxwav=0; /* Maxim number of waves */
852: int jmin=0, jmax=0; /* min, max spacing between 2 waves */
853: int ijmin=0, ijmax=0; /* Individuals having jmin and jmax */
854: int gipmx=0, gsw=0; /* Global variables on the number of contributions
1.126 brouard 855: to the likelihood and the sum of weights (done by funcone)*/
1.130 brouard 856: int mle=1, weightopt=0;
1.126 brouard 857: int **mw; /* mw[mi][i] is number of the mi wave for this individual */
858: int **dh; /* dh[mi][i] is number of steps between mi,mi+1 for this individual */
859: int **bh; /* bh[mi][i] is the bias (+ or -) for this individual if the delay between
860: * wave mi and wave mi+1 is not an exact multiple of stepm. */
1.162 brouard 861: int countcallfunc=0; /* Count the number of calls to func */
1.130 brouard 862: double jmean=1; /* Mean space between 2 waves */
1.145 brouard 863: double **matprod2(); /* test */
1.126 brouard 864: double **oldm, **newm, **savm; /* Working pointers to matrices */
865: double **oldms, **newms, **savms; /* Fixed working pointers to matrices */
1.218 ! brouard 866: double **ddnewms, **ddoldms, **ddsavms; /* for freeing later */
! 867:
1.136 brouard 868: /*FILE *fic ; */ /* Used in readdata only */
1.217 brouard 869: FILE *ficpar, *ficparo,*ficres, *ficresp, *ficresphtm, *ficresphtmfr, *ficrespl, *ficresplb,*ficrespij, *ficrespijb, *ficrest,*ficresf, *ficresfb,*ficrespop;
1.126 brouard 870: FILE *ficlog, *ficrespow;
1.130 brouard 871: int globpr=0; /* Global variable for printing or not */
1.126 brouard 872: double fretone; /* Only one call to likelihood */
1.130 brouard 873: long ipmx=0; /* Number of contributions */
1.126 brouard 874: double sw; /* Sum of weights */
875: char filerespow[FILENAMELENGTH];
876: char fileresilk[FILENAMELENGTH]; /* File of individual contributions to the likelihood */
877: FILE *ficresilk;
878: FILE *ficgp,*ficresprob,*ficpop, *ficresprobcov, *ficresprobcor;
879: FILE *ficresprobmorprev;
880: FILE *fichtm, *fichtmcov; /* Html File */
881: FILE *ficreseij;
882: char filerese[FILENAMELENGTH];
883: FILE *ficresstdeij;
884: char fileresstde[FILENAMELENGTH];
885: FILE *ficrescveij;
886: char filerescve[FILENAMELENGTH];
887: FILE *ficresvij;
888: char fileresv[FILENAMELENGTH];
889: FILE *ficresvpl;
890: char fileresvpl[FILENAMELENGTH];
891: char title[MAXLINE];
1.217 brouard 892: char optionfile[FILENAMELENGTH], datafile[FILENAMELENGTH], filerespl[FILENAMELENGTH], fileresplb[FILENAMELENGTH];
1.126 brouard 893: char plotcmd[FILENAMELENGTH], pplotcmd[FILENAMELENGTH];
894: char tmpout[FILENAMELENGTH], tmpout2[FILENAMELENGTH];
895: char command[FILENAMELENGTH];
896: int outcmd=0;
897:
1.217 brouard 898: char fileres[FILENAMELENGTH], filerespij[FILENAMELENGTH], filerespijb[FILENAMELENGTH], filereso[FILENAMELENGTH], rfileres[FILENAMELENGTH];
1.202 brouard 899: char fileresu[FILENAMELENGTH]; /* fileres without r in front */
1.126 brouard 900: char filelog[FILENAMELENGTH]; /* Log file */
901: char filerest[FILENAMELENGTH];
902: char fileregp[FILENAMELENGTH];
903: char popfile[FILENAMELENGTH];
904:
905: char optionfilegnuplot[FILENAMELENGTH], optionfilehtm[FILENAMELENGTH], optionfilehtmcov[FILENAMELENGTH] ;
906:
1.157 brouard 907: /* struct timeval start_time, end_time, curr_time, last_time, forecast_time; */
908: /* struct timezone tzp; */
909: /* extern int gettimeofday(); */
910: struct tm tml, *gmtime(), *localtime();
911:
912: extern time_t time();
913:
914: struct tm start_time, end_time, curr_time, last_time, forecast_time;
915: time_t rstart_time, rend_time, rcurr_time, rlast_time, rforecast_time; /* raw time */
916: struct tm tm;
917:
1.126 brouard 918: char strcurr[80], strfor[80];
919:
920: char *endptr;
921: long lval;
922: double dval;
923:
924: #define NR_END 1
925: #define FREE_ARG char*
926: #define FTOL 1.0e-10
927:
928: #define NRANSI
929: #define ITMAX 200
930:
931: #define TOL 2.0e-4
932:
933: #define CGOLD 0.3819660
934: #define ZEPS 1.0e-10
935: #define SHFT(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
936:
937: #define GOLD 1.618034
938: #define GLIMIT 100.0
939: #define TINY 1.0e-20
940:
941: static double maxarg1,maxarg2;
942: #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)>(maxarg2)? (maxarg1):(maxarg2))
943: #define FMIN(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1)<(maxarg2)? (maxarg1):(maxarg2))
944:
945: #define SIGN(a,b) ((b)>0.0 ? fabs(a) : -fabs(a))
946: #define rint(a) floor(a+0.5)
1.166 brouard 947: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/myutils_8h-source.html */
1.183 brouard 948: #define mytinydouble 1.0e-16
1.166 brouard 949: /* #define DEQUAL(a,b) (fabs((a)-(b))<mytinydouble) */
950: /* http://www.thphys.uni-heidelberg.de/~robbers/cmbeasy/doc/html/mynrutils_8h-source.html */
951: /* static double dsqrarg; */
952: /* #define DSQR(a) (DEQUAL((dsqrarg=(a)),0.0) ? 0.0 : dsqrarg*dsqrarg) */
1.126 brouard 953: static double sqrarg;
954: #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 :sqrarg*sqrarg)
955: #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
956: int agegomp= AGEGOMP;
957:
958: int imx;
959: int stepm=1;
960: /* Stepm, step in month: minimum step interpolation*/
961:
962: int estepm;
963: /* Estepm, step in month to interpolate survival function in order to approximate Life Expectancy*/
964:
965: int m,nb;
966: long *num;
1.197 brouard 967: int firstpass=0, lastpass=4,*cod, *cens;
1.192 brouard 968: int *ncodemax; /* ncodemax[j]= Number of modalities of the j th
969: covariate for which somebody answered excluding
970: undefined. Usually 2: 0 and 1. */
971: int *ncodemaxwundef; /* ncodemax[j]= Number of modalities of the j th
972: covariate for which somebody answered including
973: undefined. Usually 3: -1, 0 and 1. */
1.126 brouard 974: double **agev,*moisnais, *annais, *moisdc, *andc,**mint, **anint;
1.218 ! brouard 975: double **pmmij, ***probs; /* Global pointer */
! 976: double ***mobaverage; /* New global variable */
1.126 brouard 977: double *ageexmed,*agecens;
978: double dateintmean=0;
979:
980: double *weight;
981: int **s; /* Status */
1.141 brouard 982: double *agedc;
1.145 brouard 983: double **covar; /**< covar[j,i], value of jth covariate for individual i,
1.141 brouard 984: * covar=matrix(0,NCOVMAX,1,n);
1.187 brouard 985: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*age; */
1.141 brouard 986: double idx;
987: int **nbcode, *Tvar; /**< model=V2 => Tvar[1]= 2 */
1.197 brouard 988: int *Tage;
1.145 brouard 989: int *Ndum; /** Freq of modality (tricode */
1.200 brouard 990: /* int **codtab;*/ /**< codtab=imatrix(1,100,1,10); */
1.141 brouard 991: int **Tvard, *Tprod, cptcovprod, *Tvaraff;
1.126 brouard 992: double *lsurv, *lpop, *tpop;
993:
1.143 brouard 994: double ftol=FTOL; /**< Tolerance for computing Max Likelihood */
995: double ftolhess; /**< Tolerance for computing hessian */
1.126 brouard 996:
997: /**************** split *************************/
998: static int split( char *path, char *dirc, char *name, char *ext, char *finame )
999: {
1000: /* From a file name with (full) path (either Unix or Windows) we extract the directory (dirc)
1001: the name of the file (name), its extension only (ext) and its first part of the name (finame)
1002: */
1003: char *ss; /* pointer */
1.186 brouard 1004: int l1=0, l2=0; /* length counters */
1.126 brouard 1005:
1006: l1 = strlen(path ); /* length of path */
1007: if ( l1 == 0 ) return( GLOCK_ERROR_NOPATH );
1008: ss= strrchr( path, DIRSEPARATOR ); /* find last / */
1009: if ( ss == NULL ) { /* no directory, so determine current directory */
1010: strcpy( name, path ); /* we got the fullname name because no directory */
1011: /*if(strrchr(path, ODIRSEPARATOR )==NULL)
1012: printf("Warning you should use %s as a separator\n",DIRSEPARATOR);*/
1013: /* get current working directory */
1014: /* extern char* getcwd ( char *buf , int len);*/
1.184 brouard 1015: #ifdef WIN32
1016: if (_getcwd( dirc, FILENAME_MAX ) == NULL ) {
1017: #else
1018: if (getcwd(dirc, FILENAME_MAX) == NULL) {
1019: #endif
1.126 brouard 1020: return( GLOCK_ERROR_GETCWD );
1021: }
1022: /* got dirc from getcwd*/
1023: printf(" DIRC = %s \n",dirc);
1.205 brouard 1024: } else { /* strip directory from path */
1.126 brouard 1025: ss++; /* after this, the filename */
1026: l2 = strlen( ss ); /* length of filename */
1027: if ( l2 == 0 ) return( GLOCK_ERROR_NOPATH );
1028: strcpy( name, ss ); /* save file name */
1029: strncpy( dirc, path, l1 - l2 ); /* now the directory */
1.186 brouard 1030: dirc[l1-l2] = '\0'; /* add zero */
1.126 brouard 1031: printf(" DIRC2 = %s \n",dirc);
1032: }
1033: /* We add a separator at the end of dirc if not exists */
1034: l1 = strlen( dirc ); /* length of directory */
1035: if( dirc[l1-1] != DIRSEPARATOR ){
1036: dirc[l1] = DIRSEPARATOR;
1037: dirc[l1+1] = 0;
1038: printf(" DIRC3 = %s \n",dirc);
1039: }
1040: ss = strrchr( name, '.' ); /* find last / */
1041: if (ss >0){
1042: ss++;
1043: strcpy(ext,ss); /* save extension */
1044: l1= strlen( name);
1045: l2= strlen(ss)+1;
1046: strncpy( finame, name, l1-l2);
1047: finame[l1-l2]= 0;
1048: }
1049:
1050: return( 0 ); /* we're done */
1051: }
1052:
1053:
1054: /******************************************/
1055:
1056: void replace_back_to_slash(char *s, char*t)
1057: {
1058: int i;
1059: int lg=0;
1060: i=0;
1061: lg=strlen(t);
1062: for(i=0; i<= lg; i++) {
1063: (s[i] = t[i]);
1064: if (t[i]== '\\') s[i]='/';
1065: }
1066: }
1067:
1.132 brouard 1068: char *trimbb(char *out, char *in)
1.137 brouard 1069: { /* Trim multiple blanks in line but keeps first blanks if line starts with blanks */
1.132 brouard 1070: char *s;
1071: s=out;
1072: while (*in != '\0'){
1.137 brouard 1073: while( *in == ' ' && *(in+1) == ' '){ /* && *(in+1) != '\0'){*/
1.132 brouard 1074: in++;
1075: }
1076: *out++ = *in++;
1077: }
1078: *out='\0';
1079: return s;
1080: }
1081:
1.187 brouard 1082: /* char *substrchaine(char *out, char *in, char *chain) */
1083: /* { */
1084: /* /\* Substract chain 'chain' from 'in', return and output 'out' *\/ */
1085: /* char *s, *t; */
1086: /* t=in;s=out; */
1087: /* while ((*in != *chain) && (*in != '\0')){ */
1088: /* *out++ = *in++; */
1089: /* } */
1090:
1091: /* /\* *in matches *chain *\/ */
1092: /* while ((*in++ == *chain++) && (*in != '\0')){ */
1093: /* printf("*in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1094: /* } */
1095: /* in--; chain--; */
1096: /* while ( (*in != '\0')){ */
1097: /* printf("Bef *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1098: /* *out++ = *in++; */
1099: /* printf("Aft *in = %c, *out= %c *chain= %c \n", *in, *out, *chain); */
1100: /* } */
1101: /* *out='\0'; */
1102: /* out=s; */
1103: /* return out; */
1104: /* } */
1105: char *substrchaine(char *out, char *in, char *chain)
1106: {
1107: /* Substract chain 'chain' from 'in', return and output 'out' */
1108: /* in="V1+V1*age+age*age+V2", chain="age*age" */
1109:
1110: char *strloc;
1111:
1112: strcpy (out, in);
1113: strloc = strstr(out, chain); /* strloc points to out at age*age+V2 */
1114: printf("Bef strloc=%s chain=%s out=%s \n", strloc, chain, out);
1115: if(strloc != NULL){
1116: /* will affect out */ /* strloc+strlenc(chain)=+V2 */ /* Will also work in Unicode */
1117: memmove(strloc,strloc+strlen(chain), strlen(strloc+strlen(chain))+1);
1118: /* strcpy (strloc, strloc +strlen(chain));*/
1119: }
1120: printf("Aft strloc=%s chain=%s in=%s out=%s \n", strloc, chain, in, out);
1121: return out;
1122: }
1123:
1124:
1.145 brouard 1125: char *cutl(char *blocc, char *alocc, char *in, char occ)
1126: {
1.187 brouard 1127: /* cuts string in into blocc and alocc where blocc ends before FIRST occurence of char 'occ'
1.145 brouard 1128: and alocc starts after first occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1.187 brouard 1129: gives blocc="abcdef" and alocc="ghi2j".
1.145 brouard 1130: If occ is not found blocc is null and alocc is equal to in. Returns blocc
1131: */
1.160 brouard 1132: char *s, *t;
1.145 brouard 1133: t=in;s=in;
1134: while ((*in != occ) && (*in != '\0')){
1135: *alocc++ = *in++;
1136: }
1137: if( *in == occ){
1138: *(alocc)='\0';
1139: s=++in;
1140: }
1141:
1142: if (s == t) {/* occ not found */
1143: *(alocc-(in-s))='\0';
1144: in=s;
1145: }
1146: while ( *in != '\0'){
1147: *blocc++ = *in++;
1148: }
1149:
1150: *blocc='\0';
1151: return t;
1152: }
1.137 brouard 1153: char *cutv(char *blocc, char *alocc, char *in, char occ)
1154: {
1.187 brouard 1155: /* cuts string in into blocc and alocc where blocc ends before LAST occurence of char 'occ'
1.137 brouard 1156: and alocc starts after last occurence of char 'occ' : ex cutv(blocc,alocc,"abcdef2ghi2j",'2')
1157: gives blocc="abcdef2ghi" and alocc="j".
1158: If occ is not found blocc is null and alocc is equal to in. Returns alocc
1159: */
1160: char *s, *t;
1161: t=in;s=in;
1162: while (*in != '\0'){
1163: while( *in == occ){
1164: *blocc++ = *in++;
1165: s=in;
1166: }
1167: *blocc++ = *in++;
1168: }
1169: if (s == t) /* occ not found */
1170: *(blocc-(in-s))='\0';
1171: else
1172: *(blocc-(in-s)-1)='\0';
1173: in=s;
1174: while ( *in != '\0'){
1175: *alocc++ = *in++;
1176: }
1177:
1178: *alocc='\0';
1179: return s;
1180: }
1181:
1.126 brouard 1182: int nbocc(char *s, char occ)
1183: {
1184: int i,j=0;
1185: int lg=20;
1186: i=0;
1187: lg=strlen(s);
1188: for(i=0; i<= lg; i++) {
1189: if (s[i] == occ ) j++;
1190: }
1191: return j;
1192: }
1193:
1.137 brouard 1194: /* void cutv(char *u,char *v, char*t, char occ) */
1195: /* { */
1196: /* /\* cuts string t into u and v where u ends before last occurence of char 'occ' */
1197: /* and v starts after last occurence of char 'occ' : ex cutv(u,v,"abcdef2ghi2j",'2') */
1198: /* gives u="abcdef2ghi" and v="j" *\/ */
1199: /* int i,lg,j,p=0; */
1200: /* i=0; */
1201: /* lg=strlen(t); */
1202: /* for(j=0; j<=lg-1; j++) { */
1203: /* if((t[j]!= occ) && (t[j+1]== occ)) p=j+1; */
1204: /* } */
1.126 brouard 1205:
1.137 brouard 1206: /* for(j=0; j<p; j++) { */
1207: /* (u[j] = t[j]); */
1208: /* } */
1209: /* u[p]='\0'; */
1.126 brouard 1210:
1.137 brouard 1211: /* for(j=0; j<= lg; j++) { */
1212: /* if (j>=(p+1))(v[j-p-1] = t[j]); */
1213: /* } */
1214: /* } */
1.126 brouard 1215:
1.160 brouard 1216: #ifdef _WIN32
1217: char * strsep(char **pp, const char *delim)
1218: {
1219: char *p, *q;
1220:
1221: if ((p = *pp) == NULL)
1222: return 0;
1223: if ((q = strpbrk (p, delim)) != NULL)
1224: {
1225: *pp = q + 1;
1226: *q = '\0';
1227: }
1228: else
1229: *pp = 0;
1230: return p;
1231: }
1232: #endif
1233:
1.126 brouard 1234: /********************** nrerror ********************/
1235:
1236: void nrerror(char error_text[])
1237: {
1238: fprintf(stderr,"ERREUR ...\n");
1239: fprintf(stderr,"%s\n",error_text);
1240: exit(EXIT_FAILURE);
1241: }
1242: /*********************** vector *******************/
1243: double *vector(int nl, int nh)
1244: {
1245: double *v;
1246: v=(double *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(double)));
1247: if (!v) nrerror("allocation failure in vector");
1248: return v-nl+NR_END;
1249: }
1250:
1251: /************************ free vector ******************/
1252: void free_vector(double*v, int nl, int nh)
1253: {
1254: free((FREE_ARG)(v+nl-NR_END));
1255: }
1256:
1257: /************************ivector *******************************/
1258: int *ivector(long nl,long nh)
1259: {
1260: int *v;
1261: v=(int *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(int)));
1262: if (!v) nrerror("allocation failure in ivector");
1263: return v-nl+NR_END;
1264: }
1265:
1266: /******************free ivector **************************/
1267: void free_ivector(int *v, long nl, long nh)
1268: {
1269: free((FREE_ARG)(v+nl-NR_END));
1270: }
1271:
1272: /************************lvector *******************************/
1273: long *lvector(long nl,long nh)
1274: {
1275: long *v;
1276: v=(long *) malloc((size_t)((nh-nl+1+NR_END)*sizeof(long)));
1277: if (!v) nrerror("allocation failure in ivector");
1278: return v-nl+NR_END;
1279: }
1280:
1281: /******************free lvector **************************/
1282: void free_lvector(long *v, long nl, long nh)
1283: {
1284: free((FREE_ARG)(v+nl-NR_END));
1285: }
1286:
1287: /******************* imatrix *******************************/
1288: int **imatrix(long nrl, long nrh, long ncl, long nch)
1289: /* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
1290: {
1291: long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
1292: int **m;
1293:
1294: /* allocate pointers to rows */
1295: m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
1296: if (!m) nrerror("allocation failure 1 in matrix()");
1297: m += NR_END;
1298: m -= nrl;
1299:
1300:
1301: /* allocate rows and set pointers to them */
1302: m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
1303: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1304: m[nrl] += NR_END;
1305: m[nrl] -= ncl;
1306:
1307: for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
1308:
1309: /* return pointer to array of pointers to rows */
1310: return m;
1311: }
1312:
1313: /****************** free_imatrix *************************/
1314: void free_imatrix(m,nrl,nrh,ncl,nch)
1315: int **m;
1316: long nch,ncl,nrh,nrl;
1317: /* free an int matrix allocated by imatrix() */
1318: {
1319: free((FREE_ARG) (m[nrl]+ncl-NR_END));
1320: free((FREE_ARG) (m+nrl-NR_END));
1321: }
1322:
1323: /******************* matrix *******************************/
1324: double **matrix(long nrl, long nrh, long ncl, long nch)
1325: {
1326: long i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
1327: double **m;
1328:
1329: m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1330: if (!m) nrerror("allocation failure 1 in matrix()");
1331: m += NR_END;
1332: m -= nrl;
1333:
1334: m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1335: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1336: m[nrl] += NR_END;
1337: m[nrl] -= ncl;
1338:
1339: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1340: return m;
1.145 brouard 1341: /* print *(*(m+1)+70) or print m[1][70]; print m+1 or print &(m[1]) or &(m[1][0])
1342: m[i] = address of ith row of the table. &(m[i]) is its value which is another adress
1343: that of m[i][0]. In order to get the value p m[i][0] but it is unitialized.
1.126 brouard 1344: */
1345: }
1346:
1347: /*************************free matrix ************************/
1348: void free_matrix(double **m, long nrl, long nrh, long ncl, long nch)
1349: {
1350: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1351: free((FREE_ARG)(m+nrl-NR_END));
1352: }
1353:
1354: /******************* ma3x *******************************/
1355: double ***ma3x(long nrl, long nrh, long ncl, long nch, long nll, long nlh)
1356: {
1357: long i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, nlay=nlh-nll+1;
1358: double ***m;
1359:
1360: m=(double ***) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
1361: if (!m) nrerror("allocation failure 1 in matrix()");
1362: m += NR_END;
1363: m -= nrl;
1364:
1365: m[nrl]=(double **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
1366: if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
1367: m[nrl] += NR_END;
1368: m[nrl] -= ncl;
1369:
1370: for (i=nrl+1; i<=nrh; i++) m[i]=m[i-1]+ncol;
1371:
1372: m[nrl][ncl]=(double *) malloc((size_t)((nrow*ncol*nlay+NR_END)*sizeof(double)));
1373: if (!m[nrl][ncl]) nrerror("allocation failure 3 in matrix()");
1374: m[nrl][ncl] += NR_END;
1375: m[nrl][ncl] -= nll;
1376: for (j=ncl+1; j<=nch; j++)
1377: m[nrl][j]=m[nrl][j-1]+nlay;
1378:
1379: for (i=nrl+1; i<=nrh; i++) {
1380: m[i][ncl]=m[i-1l][ncl]+ncol*nlay;
1381: for (j=ncl+1; j<=nch; j++)
1382: m[i][j]=m[i][j-1]+nlay;
1383: }
1384: return m;
1385: /* gdb: p *(m+1) <=> p m[1] and p (m+1) <=> p (m+1) <=> p &(m[1])
1386: &(m[i][j][k]) <=> *((*(m+i) + j)+k)
1387: */
1388: }
1389:
1390: /*************************free ma3x ************************/
1391: void free_ma3x(double ***m, long nrl, long nrh, long ncl, long nch,long nll, long nlh)
1392: {
1393: free((FREE_ARG)(m[nrl][ncl]+ nll-NR_END));
1394: free((FREE_ARG)(m[nrl]+ncl-NR_END));
1395: free((FREE_ARG)(m+nrl-NR_END));
1396: }
1397:
1398: /*************** function subdirf ***********/
1399: char *subdirf(char fileres[])
1400: {
1401: /* Caution optionfilefiname is hidden */
1402: strcpy(tmpout,optionfilefiname);
1403: strcat(tmpout,"/"); /* Add to the right */
1404: strcat(tmpout,fileres);
1405: return tmpout;
1406: }
1407:
1408: /*************** function subdirf2 ***********/
1409: char *subdirf2(char fileres[], char *preop)
1410: {
1411:
1412: /* Caution optionfilefiname is hidden */
1413: strcpy(tmpout,optionfilefiname);
1414: strcat(tmpout,"/");
1415: strcat(tmpout,preop);
1416: strcat(tmpout,fileres);
1417: return tmpout;
1418: }
1419:
1420: /*************** function subdirf3 ***********/
1421: char *subdirf3(char fileres[], char *preop, char *preop2)
1422: {
1423:
1424: /* Caution optionfilefiname is hidden */
1425: strcpy(tmpout,optionfilefiname);
1426: strcat(tmpout,"/");
1427: strcat(tmpout,preop);
1428: strcat(tmpout,preop2);
1429: strcat(tmpout,fileres);
1430: return tmpout;
1431: }
1.213 brouard 1432:
1433: /*************** function subdirfext ***********/
1434: char *subdirfext(char fileres[], char *preop, char *postop)
1435: {
1436:
1437: strcpy(tmpout,preop);
1438: strcat(tmpout,fileres);
1439: strcat(tmpout,postop);
1440: return tmpout;
1441: }
1.126 brouard 1442:
1.213 brouard 1443: /*************** function subdirfext3 ***********/
1444: char *subdirfext3(char fileres[], char *preop, char *postop)
1445: {
1446:
1447: /* Caution optionfilefiname is hidden */
1448: strcpy(tmpout,optionfilefiname);
1449: strcat(tmpout,"/");
1450: strcat(tmpout,preop);
1451: strcat(tmpout,fileres);
1452: strcat(tmpout,postop);
1453: return tmpout;
1454: }
1455:
1.162 brouard 1456: char *asc_diff_time(long time_sec, char ascdiff[])
1457: {
1458: long sec_left, days, hours, minutes;
1459: days = (time_sec) / (60*60*24);
1460: sec_left = (time_sec) % (60*60*24);
1461: hours = (sec_left) / (60*60) ;
1462: sec_left = (sec_left) %(60*60);
1463: minutes = (sec_left) /60;
1464: sec_left = (sec_left) % (60);
1465: sprintf(ascdiff,"%ld day(s) %ld hour(s) %ld minute(s) %ld second(s)",days, hours, minutes, sec_left);
1466: return ascdiff;
1467: }
1468:
1.126 brouard 1469: /***************** f1dim *************************/
1470: extern int ncom;
1471: extern double *pcom,*xicom;
1472: extern double (*nrfunc)(double []);
1473:
1474: double f1dim(double x)
1475: {
1476: int j;
1477: double f;
1478: double *xt;
1479:
1480: xt=vector(1,ncom);
1481: for (j=1;j<=ncom;j++) xt[j]=pcom[j]+x*xicom[j];
1482: f=(*nrfunc)(xt);
1483: free_vector(xt,1,ncom);
1484: return f;
1485: }
1486:
1487: /*****************brent *************************/
1488: double brent(double ax, double bx, double cx, double (*f)(double), double tol, double *xmin)
1.187 brouard 1489: {
1490: /* Given a function f, and given a bracketing triplet of abscissas ax, bx, cx (such that bx is
1491: * between ax and cx, and f(bx) is less than both f(ax) and f(cx) ), this routine isolates
1492: * the minimum to a fractional precision of about tol using Brent’s method. The abscissa of
1493: * the minimum is returned as xmin, and the minimum function value is returned as brent , the
1494: * returned function value.
1495: */
1.126 brouard 1496: int iter;
1497: double a,b,d,etemp;
1.159 brouard 1498: double fu=0,fv,fw,fx;
1.164 brouard 1499: double ftemp=0.;
1.126 brouard 1500: double p,q,r,tol1,tol2,u,v,w,x,xm;
1501: double e=0.0;
1502:
1503: a=(ax < cx ? ax : cx);
1504: b=(ax > cx ? ax : cx);
1505: x=w=v=bx;
1506: fw=fv=fx=(*f)(x);
1507: for (iter=1;iter<=ITMAX;iter++) {
1508: xm=0.5*(a+b);
1509: tol2=2.0*(tol1=tol*fabs(x)+ZEPS);
1510: /* if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret)))*/
1511: printf(".");fflush(stdout);
1512: fprintf(ficlog,".");fflush(ficlog);
1.162 brouard 1513: #ifdef DEBUGBRENT
1.126 brouard 1514: 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);
1515: 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);
1516: /* if ((fabs(x-xm) <= (tol2-0.5*(b-a)))||(2.0*fabs(fu-ftemp) <= ftol*1.e-2*(fabs(fu)+fabs(ftemp)))) { */
1517: #endif
1518: if (fabs(x-xm) <= (tol2-0.5*(b-a))){
1519: *xmin=x;
1520: return fx;
1521: }
1522: ftemp=fu;
1523: if (fabs(e) > tol1) {
1524: r=(x-w)*(fx-fv);
1525: q=(x-v)*(fx-fw);
1526: p=(x-v)*q-(x-w)*r;
1527: q=2.0*(q-r);
1528: if (q > 0.0) p = -p;
1529: q=fabs(q);
1530: etemp=e;
1531: e=d;
1532: if (fabs(p) >= fabs(0.5*q*etemp) || p <= q*(a-x) || p >= q*(b-x))
1533: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1534: else {
1535: d=p/q;
1536: u=x+d;
1537: if (u-a < tol2 || b-u < tol2)
1538: d=SIGN(tol1,xm-x);
1539: }
1540: } else {
1541: d=CGOLD*(e=(x >= xm ? a-x : b-x));
1542: }
1543: u=(fabs(d) >= tol1 ? x+d : x+SIGN(tol1,d));
1544: fu=(*f)(u);
1545: if (fu <= fx) {
1546: if (u >= x) a=x; else b=x;
1547: SHFT(v,w,x,u)
1.183 brouard 1548: SHFT(fv,fw,fx,fu)
1549: } else {
1550: if (u < x) a=u; else b=u;
1551: if (fu <= fw || w == x) {
1552: v=w;
1553: w=u;
1554: fv=fw;
1555: fw=fu;
1556: } else if (fu <= fv || v == x || v == w) {
1557: v=u;
1558: fv=fu;
1559: }
1560: }
1.126 brouard 1561: }
1562: nrerror("Too many iterations in brent");
1563: *xmin=x;
1564: return fx;
1565: }
1566:
1567: /****************** mnbrak ***********************/
1568:
1569: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb, double *fc,
1570: double (*func)(double))
1.183 brouard 1571: { /* Given a function func , and given distinct initial points ax and bx , this routine searches in
1572: the downhill direction (defined by the function as evaluated at the initial points) and returns
1573: new points ax , bx , cx that bracket a minimum of the function. Also returned are the function
1574: values at the three points, fa, fb , and fc such that fa > fb and fb < fc.
1575: */
1.126 brouard 1576: double ulim,u,r,q, dum;
1577: double fu;
1.187 brouard 1578:
1579: double scale=10.;
1580: int iterscale=0;
1581:
1582: *fa=(*func)(*ax); /* xta[j]=pcom[j]+(*ax)*xicom[j]; fa=f(xta[j])*/
1583: *fb=(*func)(*bx); /* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) */
1584:
1585:
1586: /* while(*fb != *fb){ /\* *ax should be ok, reducing distance to *ax *\/ */
1587: /* printf("Warning mnbrak *fb = %lf, *bx=%lf *ax=%lf *fa==%lf iter=%d\n",*fb, *bx, *ax, *fa, iterscale++); */
1588: /* *bx = *ax - (*ax - *bx)/scale; */
1589: /* *fb=(*func)(*bx); /\* xtb[j]=pcom[j]+(*bx)*xicom[j]; fb=f(xtb[j]) *\/ */
1590: /* } */
1591:
1.126 brouard 1592: if (*fb > *fa) {
1593: SHFT(dum,*ax,*bx,dum)
1.183 brouard 1594: SHFT(dum,*fb,*fa,dum)
1595: }
1.126 brouard 1596: *cx=(*bx)+GOLD*(*bx-*ax);
1597: *fc=(*func)(*cx);
1.183 brouard 1598: #ifdef DEBUG
1599: printf("mnbrak0 *fb=%.12e *fc=%.12e\n",*fb,*fc);
1600: fprintf(ficlog,"mnbrak0 *fb=%.12e *fc=%.12e\n",*fb,*fc);
1601: #endif
1602: while (*fb > *fc) { /* Declining a,b,c with fa> fb > fc */
1.126 brouard 1603: r=(*bx-*ax)*(*fb-*fc);
1604: q=(*bx-*cx)*(*fb-*fa);
1605: u=(*bx)-((*bx-*cx)*q-(*bx-*ax)*r)/
1.183 brouard 1606: (2.0*SIGN(FMAX(fabs(q-r),TINY),q-r)); /* Minimum abscissa of a parabolic estimated from (a,fa), (b,fb) and (c,fc). */
1607: ulim=(*bx)+GLIMIT*(*cx-*bx); /* Maximum abscissa where function should be evaluated */
1608: if ((*bx-u)*(u-*cx) > 0.0) { /* if u_p is between b and c */
1.126 brouard 1609: fu=(*func)(u);
1.163 brouard 1610: #ifdef DEBUG
1611: /* f(x)=A(x-u)**2+f(u) */
1612: double A, fparabu;
1613: A= (*fb - *fa)/(*bx-*ax)/(*bx+*ax-2*u);
1614: fparabu= *fa - A*(*ax-u)*(*ax-u);
1615: printf("mnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu);
1616: fprintf(ficlog, "mnbrak (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf, fparabu=%.12f)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu, fparabu);
1.183 brouard 1617: /* And thus,it can be that fu > *fc even if fparabu < *fc */
1618: /* mnbrak (*ax=7.666299858533, *fa=299039.693133272231), (*bx=8.595447774979, *fb=298976.598289369489),
1619: (*cx=10.098840694817, *fc=298946.631474258087), (*u=9.852501168332, fu=298948.773013752128, fparabu=298945.434711494134) */
1620: /* In that case, there is no bracket in the output! Routine is wrong with many consequences.*/
1.163 brouard 1621: #endif
1.184 brouard 1622: #ifdef MNBRAKORIGINAL
1.183 brouard 1623: #else
1.191 brouard 1624: /* if (fu > *fc) { */
1625: /* #ifdef DEBUG */
1626: /* printf("mnbrak4 fu > fc \n"); */
1627: /* fprintf(ficlog, "mnbrak4 fu > fc\n"); */
1628: /* #endif */
1629: /* /\* 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 *\\/ *\/ */
1630: /* /\* SHFT(*fa,*fc,fu,*fc) /\\* (b, u, c) is a bracket while test fb > fc will be fu > fc will exit *\\/ *\/ */
1631: /* dum=u; /\* Shifting c and u *\/ */
1632: /* u = *cx; */
1633: /* *cx = dum; */
1634: /* dum = fu; */
1635: /* fu = *fc; */
1636: /* *fc =dum; */
1637: /* } else { /\* end *\/ */
1638: /* #ifdef DEBUG */
1639: /* printf("mnbrak3 fu < fc \n"); */
1640: /* fprintf(ficlog, "mnbrak3 fu < fc\n"); */
1641: /* #endif */
1642: /* dum=u; /\* Shifting c and u *\/ */
1643: /* u = *cx; */
1644: /* *cx = dum; */
1645: /* dum = fu; */
1646: /* fu = *fc; */
1647: /* *fc =dum; */
1648: /* } */
1.183 brouard 1649: #ifdef DEBUG
1.191 brouard 1650: printf("mnbrak34 fu < or >= fc \n");
1651: fprintf(ficlog, "mnbrak34 fu < fc\n");
1.183 brouard 1652: #endif
1.191 brouard 1653: dum=u; /* Shifting c and u */
1654: u = *cx;
1655: *cx = dum;
1656: dum = fu;
1657: fu = *fc;
1658: *fc =dum;
1.183 brouard 1659: #endif
1.162 brouard 1660: } else if ((*cx-u)*(u-ulim) > 0.0) { /* u is after c but before ulim */
1.183 brouard 1661: #ifdef DEBUG
1662: printf("mnbrak2 u after c but before ulim\n");
1663: fprintf(ficlog, "mnbrak2 u after c but before ulim\n");
1664: #endif
1.126 brouard 1665: fu=(*func)(u);
1666: if (fu < *fc) {
1.183 brouard 1667: #ifdef DEBUG
1668: printf("mnbrak2 u after c but before ulim AND fu < fc\n");
1669: fprintf(ficlog, "mnbrak2 u after c but before ulim AND fu <fc \n");
1670: #endif
1.126 brouard 1671: SHFT(*bx,*cx,u,*cx+GOLD*(*cx-*bx))
1.183 brouard 1672: SHFT(*fb,*fc,fu,(*func)(u))
1673: }
1.162 brouard 1674: } else if ((u-ulim)*(ulim-*cx) >= 0.0) { /* u outside ulim (verifying that ulim is beyond c) */
1.183 brouard 1675: #ifdef DEBUG
1676: printf("mnbrak2 u outside ulim (verifying that ulim is beyond c)\n");
1677: fprintf(ficlog, "mnbrak2 u outside ulim (verifying that ulim is beyond c)\n");
1678: #endif
1.126 brouard 1679: u=ulim;
1680: fu=(*func)(u);
1.183 brouard 1681: } else { /* u could be left to b (if r > q parabola has a maximum) */
1682: #ifdef DEBUG
1683: printf("mnbrak2 u could be left to b (if r > q parabola has a maximum)\n");
1684: fprintf(ficlog, "mnbrak2 u could be left to b (if r > q parabola has a maximum)\n");
1685: #endif
1.126 brouard 1686: u=(*cx)+GOLD*(*cx-*bx);
1687: fu=(*func)(u);
1.183 brouard 1688: } /* end tests */
1.126 brouard 1689: SHFT(*ax,*bx,*cx,u)
1.183 brouard 1690: SHFT(*fa,*fb,*fc,fu)
1691: #ifdef DEBUG
1692: printf("mnbrak2 (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu);
1693: fprintf(ficlog, "mnbrak2 (*ax=%.12f, *fa=%.12lf), (*bx=%.12f, *fb=%.12lf), (*cx=%.12f, *fc=%.12lf), (*u=%.12f, fu=%.12lf)\n",*ax,*fa,*bx,*fb,*cx,*fc,u,fu);
1694: #endif
1695: } /* 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 1696: }
1697:
1698: /*************** linmin ************************/
1.162 brouard 1699: /* Given an n -dimensional point p[1..n] and an n -dimensional direction xi[1..n] , moves and
1700: resets p to where the function func(p) takes on a minimum along the direction xi from p ,
1701: and replaces xi by the actual vector displacement that p was moved. Also returns as fret
1702: the value of func at the returned location p . This is actually all accomplished by calling the
1703: routines mnbrak and brent .*/
1.126 brouard 1704: int ncom;
1705: double *pcom,*xicom;
1706: double (*nrfunc)(double []);
1707:
1708: void linmin(double p[], double xi[], int n, double *fret,double (*func)(double []))
1709: {
1710: double brent(double ax, double bx, double cx,
1711: double (*f)(double), double tol, double *xmin);
1712: double f1dim(double x);
1713: void mnbrak(double *ax, double *bx, double *cx, double *fa, double *fb,
1714: double *fc, double (*func)(double));
1715: int j;
1716: double xx,xmin,bx,ax;
1717: double fx,fb,fa;
1.187 brouard 1718:
1.203 brouard 1719: #ifdef LINMINORIGINAL
1720: #else
1721: double scale=10., axs, xxs; /* Scale added for infinity */
1722: #endif
1723:
1.126 brouard 1724: ncom=n;
1725: pcom=vector(1,n);
1726: xicom=vector(1,n);
1727: nrfunc=func;
1728: for (j=1;j<=n;j++) {
1729: pcom[j]=p[j];
1.202 brouard 1730: xicom[j]=xi[j]; /* Former scale xi[j] of currrent direction i */
1.126 brouard 1731: }
1.187 brouard 1732:
1.203 brouard 1733: #ifdef LINMINORIGINAL
1734: xx=1.;
1735: #else
1736: axs=0.0;
1737: xxs=1.;
1738: do{
1739: xx= xxs;
1740: #endif
1.187 brouard 1741: ax=0.;
1742: mnbrak(&ax,&xx,&bx,&fa,&fx,&fb,f1dim); /* Outputs: xtx[j]=pcom[j]+(*xx)*xicom[j]; fx=f(xtx[j]) */
1743: /* brackets with inputs ax=0 and xx=1, but points, pcom=p, and directions values, xicom=xi, are sent via f1dim(x) */
1744: /* 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)) */
1745: /* Outputs: fa=f(p(j)) and fx=f(p(j) + xxs * xi(j) ) and f(bx)= f(p(j)+ bx* xi(j)) */
1746: /* Given input ax=axs and xx=xxs, xx might be too far from ax to get a finite f(xx) */
1747: /* Searches on line, outputs (ax, xx, bx) such that fx < min(fa and fb) */
1748: /* 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 1749: #ifdef LINMINORIGINAL
1750: #else
1751: if (fx != fx){
1752: xxs=xxs/scale; /* Trying a smaller xx, closer to initial ax=0 */
1753: printf("|");
1754: fprintf(ficlog,"|");
1755: #ifdef DEBUGLINMIN
1756: 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);
1757: #endif
1758: }
1759: }while(fx != fx);
1760: #endif
1761:
1.191 brouard 1762: #ifdef DEBUGLINMIN
1763: 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 1764: 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 1765: #endif
1.187 brouard 1766: *fret=brent(ax,xx,bx,f1dim,TOL,&xmin); /* Giving a bracketting triplet (ax, xx, bx), find a minimum, xmin, according to f1dim, *fret(xmin),*/
1767: /* fa = f(p[j] + ax * xi[j]), fx = f(p[j] + xx * xi[j]), fb = f(p[j] + bx * xi[j]) */
1768: /* fmin = f(p[j] + xmin * xi[j]) */
1769: /* P+lambda n in that direction (lambdamin), with TOL between abscisses */
1770: /* f1dim(xmin): for (j=1;j<=ncom;j++) xt[j]=pcom[j]+xmin*xicom[j]; */
1.126 brouard 1771: #ifdef DEBUG
1772: printf("retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
1773: fprintf(ficlog,"retour brent fret=%.12e xmin=%.12e\n",*fret,xmin);
1774: #endif
1.191 brouard 1775: #ifdef DEBUGLINMIN
1776: printf("linmin end ");
1.202 brouard 1777: fprintf(ficlog,"linmin end ");
1.191 brouard 1778: #endif
1.126 brouard 1779: for (j=1;j<=n;j++) {
1.203 brouard 1780: #ifdef LINMINORIGINAL
1781: xi[j] *= xmin;
1782: #else
1783: #ifdef DEBUGLINMIN
1784: if(xxs <1.0)
1785: printf(" before xi[%d]=%12.8f", j,xi[j]);
1786: #endif
1787: 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) */
1788: #ifdef DEBUGLINMIN
1789: if(xxs <1.0)
1790: 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 );
1791: #endif
1792: #endif
1.187 brouard 1793: p[j] += xi[j]; /* Parameters values are updated accordingly */
1.126 brouard 1794: }
1.191 brouard 1795: #ifdef DEBUGLINMIN
1.203 brouard 1796: printf("\n");
1.191 brouard 1797: printf("Comparing last *frec(xmin=%12.8f)=%12.8f from Brent and frec(0.)=%12.8f \n", xmin, *fret, (*func)(p));
1.202 brouard 1798: 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 1799: for (j=1;j<=n;j++) {
1.202 brouard 1800: printf(" xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1801: fprintf(ficlog," xi[%d]= %14.10f p[%d]= %12.7f",j,xi[j],j,p[j]);
1802: if(j % ncovmodel == 0){
1.191 brouard 1803: printf("\n");
1.202 brouard 1804: fprintf(ficlog,"\n");
1805: }
1.191 brouard 1806: }
1.203 brouard 1807: #else
1.191 brouard 1808: #endif
1.126 brouard 1809: free_vector(xicom,1,n);
1810: free_vector(pcom,1,n);
1811: }
1812:
1813:
1814: /*************** powell ************************/
1.162 brouard 1815: /*
1816: Minimization of a function func of n variables. Input consists of an initial starting point
1817: p[1..n] ; an initial matrix xi[1..n][1..n] , whose columns contain the initial set of di-
1818: rections (usually the n unit vectors); and ftol , the fractional tolerance in the function value
1819: such that failure to decrease by more than this amount on one iteration signals doneness. On
1820: output, p is set to the best point found, xi is the then-current direction set, fret is the returned
1821: function value at p , and iter is the number of iterations taken. The routine linmin is used.
1822: */
1.126 brouard 1823: void powell(double p[], double **xi, int n, double ftol, int *iter, double *fret,
1824: double (*func)(double []))
1825: {
1826: void linmin(double p[], double xi[], int n, double *fret,
1827: double (*func)(double []));
1828: int i,ibig,j;
1829: double del,t,*pt,*ptt,*xit;
1.181 brouard 1830: double directest;
1.126 brouard 1831: double fp,fptt;
1832: double *xits;
1833: int niterf, itmp;
1834:
1835: pt=vector(1,n);
1836: ptt=vector(1,n);
1837: xit=vector(1,n);
1838: xits=vector(1,n);
1839: *fret=(*func)(p);
1840: for (j=1;j<=n;j++) pt[j]=p[j];
1.202 brouard 1841: rcurr_time = time(NULL);
1.126 brouard 1842: for (*iter=1;;++(*iter)) {
1.187 brouard 1843: fp=(*fret); /* From former iteration or initial value */
1.126 brouard 1844: ibig=0;
1845: del=0.0;
1.157 brouard 1846: rlast_time=rcurr_time;
1847: /* (void) gettimeofday(&curr_time,&tzp); */
1848: rcurr_time = time(NULL);
1849: curr_time = *localtime(&rcurr_time);
1850: printf("\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret, rcurr_time-rlast_time, rcurr_time-rstart_time);fflush(stdout);
1851: fprintf(ficlog,"\nPowell iter=%d -2*LL=%.12f %ld sec. %ld sec.",*iter,*fret,rcurr_time-rlast_time, rcurr_time-rstart_time); fflush(ficlog);
1852: /* fprintf(ficrespow,"%d %.12f %ld",*iter,*fret,curr_time.tm_sec-start_time.tm_sec); */
1.192 brouard 1853: for (i=1;i<=n;i++) {
1.126 brouard 1854: printf(" %d %.12f",i, p[i]);
1855: fprintf(ficlog," %d %.12lf",i, p[i]);
1856: fprintf(ficrespow," %.12lf", p[i]);
1857: }
1858: printf("\n");
1859: fprintf(ficlog,"\n");
1860: fprintf(ficrespow,"\n");fflush(ficrespow);
1861: if(*iter <=3){
1.157 brouard 1862: tml = *localtime(&rcurr_time);
1863: strcpy(strcurr,asctime(&tml));
1864: rforecast_time=rcurr_time;
1.126 brouard 1865: itmp = strlen(strcurr);
1866: if(strcurr[itmp-1]=='\n') /* Windows outputs with a new line */
1867: strcurr[itmp-1]='\0';
1.162 brouard 1868: printf("\nConsidering the time needed for the last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.157 brouard 1869: fprintf(ficlog,"\nConsidering the time needed for this last iteration #%d: %ld seconds,\n",*iter,rcurr_time-rlast_time);
1.126 brouard 1870: for(niterf=10;niterf<=30;niterf+=10){
1.157 brouard 1871: rforecast_time=rcurr_time+(niterf-*iter)*(rcurr_time-rlast_time);
1872: forecast_time = *localtime(&rforecast_time);
1873: strcpy(strfor,asctime(&forecast_time));
1.126 brouard 1874: itmp = strlen(strfor);
1875: if(strfor[itmp-1]=='\n')
1876: strfor[itmp-1]='\0';
1.157 brouard 1877: 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);
1878: 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 1879: }
1880: }
1.187 brouard 1881: for (i=1;i<=n;i++) { /* For each direction i */
1882: for (j=1;j<=n;j++) xit[j]=xi[j][i]; /* Directions stored from previous iteration with previous scales */
1.126 brouard 1883: fptt=(*fret);
1884: #ifdef DEBUG
1.203 brouard 1885: printf("fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1886: fprintf(ficlog, "fret=%lf, %lf, %lf \n", *fret, *fret, *fret);
1.126 brouard 1887: #endif
1.203 brouard 1888: printf("%d",i);fflush(stdout); /* print direction (parameter) i */
1.126 brouard 1889: fprintf(ficlog,"%d",i);fflush(ficlog);
1.188 brouard 1890: linmin(p,xit,n,fret,func); /* Point p[n]. xit[n] has been loaded for direction i as input.*/
1891: /* Outputs are fret(new point p) p is updated and xit rescaled */
1892: if (fabs(fptt-(*fret)) > del) { /* We are keeping the max gain on each of the n directions */
1893: /* because that direction will be replaced unless the gain del is small */
1894: /* in comparison with the 'probable' gain, mu^2, with the last average direction. */
1895: /* Unless the n directions are conjugate some gain in the determinant may be obtained */
1896: /* with the new direction. */
1.126 brouard 1897: del=fabs(fptt-(*fret));
1898: ibig=i;
1899: }
1900: #ifdef DEBUG
1901: printf("%d %.12e",i,(*fret));
1902: fprintf(ficlog,"%d %.12e",i,(*fret));
1903: for (j=1;j<=n;j++) {
1904: xits[j]=FMAX(fabs(p[j]-pt[j]),1.e-5);
1905: printf(" x(%d)=%.12e",j,xit[j]);
1906: fprintf(ficlog," x(%d)=%.12e",j,xit[j]);
1907: }
1908: for(j=1;j<=n;j++) {
1.162 brouard 1909: printf(" p(%d)=%.12e",j,p[j]);
1910: fprintf(ficlog," p(%d)=%.12e",j,p[j]);
1.126 brouard 1911: }
1912: printf("\n");
1913: fprintf(ficlog,"\n");
1914: #endif
1.187 brouard 1915: } /* end loop on each direction i */
1916: /* Convergence test will use last linmin estimation (fret) and compare former iteration (fp) */
1.188 brouard 1917: /* But p and xit have been updated at the end of linmin, *fret corresponds to new p, xit */
1.187 brouard 1918: /* New value of last point Pn is not computed, P(n-1) */
1.182 brouard 1919: if (2.0*fabs(fp-(*fret)) <= ftol*(fabs(fp)+fabs(*fret))) { /* Did we reach enough precision? */
1.188 brouard 1920: /* We could compare with a chi^2. chisquare(0.95,ddl=1)=3.84 */
1921: /* By adding age*age in a model, the new -2LL should be lower and the difference follows a */
1922: /* a chisquare statistics with 1 degree. To be significant at the 95% level, it should have */
1923: /* decreased of more than 3.84 */
1924: /* By adding age*age and V1*age the gain (-2LL) should be more than 5.99 (ddl=2) */
1925: /* By using V1+V2+V3, the gain should be 7.82, compared with basic 1+age. */
1926: /* By adding 10 parameters more the gain should be 18.31 */
1927:
1928: /* Starting the program with initial values given by a former maximization will simply change */
1929: /* the scales of the directions and the directions, because the are reset to canonical directions */
1930: /* Thus the first calls to linmin will give new points and better maximizations until fp-(*fret) is */
1931: /* under the tolerance value. If the tolerance is very small 1.e-9, it could last long. */
1.126 brouard 1932: #ifdef DEBUG
1933: int k[2],l;
1934: k[0]=1;
1935: k[1]=-1;
1936: printf("Max: %.12e",(*func)(p));
1937: fprintf(ficlog,"Max: %.12e",(*func)(p));
1938: for (j=1;j<=n;j++) {
1939: printf(" %.12e",p[j]);
1940: fprintf(ficlog," %.12e",p[j]);
1941: }
1942: printf("\n");
1943: fprintf(ficlog,"\n");
1944: for(l=0;l<=1;l++) {
1945: for (j=1;j<=n;j++) {
1946: ptt[j]=p[j]+(p[j]-pt[j])*k[l];
1947: printf("l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1948: fprintf(ficlog,"l=%d j=%d ptt=%.12e, xits=%.12e, p=%.12e, xit=%.12e", l,j,ptt[j],xits[j],p[j],xit[j]);
1949: }
1950: printf("func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1951: fprintf(ficlog,"func(ptt)=%.12e, deriv=%.12e\n",(*func)(ptt),(ptt[j]-p[j])/((*func)(ptt)-(*func)(p)));
1952: }
1953: #endif
1954:
1955:
1956: free_vector(xit,1,n);
1957: free_vector(xits,1,n);
1958: free_vector(ptt,1,n);
1959: free_vector(pt,1,n);
1960: return;
1.192 brouard 1961: } /* enough precision */
1.126 brouard 1962: if (*iter == ITMAX) nrerror("powell exceeding maximum iterations.");
1.181 brouard 1963: for (j=1;j<=n;j++) { /* Computes the extrapolated point P_0 + 2 (P_n-P_0) */
1.126 brouard 1964: ptt[j]=2.0*p[j]-pt[j];
1965: xit[j]=p[j]-pt[j];
1966: pt[j]=p[j];
1967: }
1.181 brouard 1968: fptt=(*func)(ptt); /* f_3 */
1.192 brouard 1969: #ifdef POWELLF1F3
1970: #else
1.161 brouard 1971: if (fptt < fp) { /* If extrapolated point is better, decide if we keep that new direction or not */
1.192 brouard 1972: #endif
1.162 brouard 1973: /* (x1 f1=fp), (x2 f2=*fret), (x3 f3=fptt), (xm fm) */
1.161 brouard 1974: /* From x1 (P0) distance of x2 is at h and x3 is 2h */
1.162 brouard 1975: /* Let f"(x2) be the 2nd derivative equal everywhere. */
1976: /* Then the parabolic through (x1,f1), (x2,f2) and (x3,f3) */
1977: /* will reach at f3 = fm + h^2/2 f"m ; f" = (f1 -2f2 +f3 ) / h**2 */
1.181 brouard 1978: /* Conditional for using this new direction is that mu^2 = (f1-2f2+f3)^2 /2 < del */
1.161 brouard 1979: /* t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)-del*SQR(fp-fptt); */
1.183 brouard 1980: #ifdef NRCORIGINAL
1981: t=2.0*(fp-2.0*(*fret)+fptt)*SQR(fp-(*fret)-del)- del*SQR(fp-fptt); /* Original Numerical Recipes in C*/
1982: #else
1983: 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 1984: t= t- del*SQR(fp-fptt);
1.183 brouard 1985: #endif
1.202 brouard 1986: directest = fp-2.0*(*fret)+fptt - 2.0 * del; /* If delta was big enough we change it for a new direction */
1.161 brouard 1987: #ifdef DEBUG
1.181 brouard 1988: 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);
1989: 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 1990: printf("t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
1991: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
1992: fprintf(ficlog,"t3= %.12lf, t4= %.12lf, t3*= %.12lf, t4*= %.12lf\n",SQR(fp-(*fret)-del),SQR(fp-fptt),
1993: (fp-(*fret)-del)*(fp-(*fret)-del),(fp-fptt)*(fp-fptt));
1994: 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);
1995: 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);
1996: #endif
1.183 brouard 1997: #ifdef POWELLORIGINAL
1998: if (t < 0.0) { /* Then we use it for new direction */
1999: #else
1.182 brouard 2000: if (directest*t < 0.0) { /* Contradiction between both tests */
1.202 brouard 2001: 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 2002: printf("f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
1.202 brouard 2003: fprintf(ficlog,"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 2004: fprintf(ficlog,"f1-2f2+f3= %.12lf, f1-f2-del= %.12lf, f1-f3= %.12lf\n",fp-2.0*(*fret)+fptt, fp -(*fret) -del, fp-fptt);
2005: }
1.181 brouard 2006: if (directest < 0.0) { /* Then we use it for new direction */
2007: #endif
1.191 brouard 2008: #ifdef DEBUGLINMIN
2009: printf("Before linmin in direction P%d-P0\n",n);
2010: for (j=1;j<=n;j++) {
1.202 brouard 2011: printf(" Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2012: fprintf(ficlog," Before xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2013: if(j % ncovmodel == 0){
1.191 brouard 2014: printf("\n");
1.202 brouard 2015: fprintf(ficlog,"\n");
2016: }
1.191 brouard 2017: }
2018: #endif
1.187 brouard 2019: linmin(p,xit,n,fret,func); /* computes minimum on the extrapolated direction: changes p and rescales xit.*/
1.191 brouard 2020: #ifdef DEBUGLINMIN
2021: for (j=1;j<=n;j++) {
2022: printf("After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
1.202 brouard 2023: fprintf(ficlog,"After xit[%d]= %12.7f p[%d]= %12.7f",j,xit[j],j,p[j]);
2024: if(j % ncovmodel == 0){
1.191 brouard 2025: printf("\n");
1.202 brouard 2026: fprintf(ficlog,"\n");
2027: }
1.191 brouard 2028: }
2029: #endif
1.126 brouard 2030: for (j=1;j<=n;j++) {
1.181 brouard 2031: xi[j][ibig]=xi[j][n]; /* Replace direction with biggest decrease by last direction n */
2032: xi[j][n]=xit[j]; /* and this nth direction by the by the average p_0 p_n */
1.126 brouard 2033: }
1.181 brouard 2034: printf("Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
2035: fprintf(ficlog,"Gaining to use new average direction of P0 P%d instead of biggest increase direction %d :\n",n,ibig);
1.161 brouard 2036:
1.126 brouard 2037: #ifdef DEBUG
1.164 brouard 2038: printf("Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
2039: fprintf(ficlog,"Direction changed last moved %d in place of ibig=%d, new last is the average:\n",n,ibig);
1.126 brouard 2040: for(j=1;j<=n;j++){
2041: printf(" %.12e",xit[j]);
2042: fprintf(ficlog," %.12e",xit[j]);
2043: }
2044: printf("\n");
2045: fprintf(ficlog,"\n");
2046: #endif
1.192 brouard 2047: } /* end of t or directest negative */
2048: #ifdef POWELLF1F3
2049: #else
1.162 brouard 2050: } /* end if (fptt < fp) */
1.192 brouard 2051: #endif
2052: } /* loop iteration */
1.126 brouard 2053: }
2054:
2055: /**** Prevalence limit (stable or period prevalence) ****************/
2056:
1.203 brouard 2057: double **prevalim(double **prlim, int nlstate, double x[], double age, double **oldm, double **savm, double ftolpl, int *ncvyear, int ij)
1.126 brouard 2058: {
1.218 ! brouard 2059: /* Computes the prevalence limit in each live state at age x and for covariate ij by left multiplying the unit
1.203 brouard 2060: matrix by transitions matrix until convergence is reached with precision ftolpl */
1.206 brouard 2061: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2062: /* Wx is row vector: population in state 1, population in state 2, population dead */
2063: /* or prevalence in state 1, prevalence in state 2, 0 */
2064: /* newm is the matrix after multiplications, its rows are identical at a factor */
2065: /* Initial matrix pimij */
2066: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2067: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2068: /* 0, 0 , 1} */
2069: /*
2070: * and after some iteration: */
2071: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2072: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2073: /* 0, 0 , 1} */
2074: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2075: /* {0.51571254859325999, 0.4842874514067399, */
2076: /* 0.51326036147820708, 0.48673963852179264} */
2077: /* If we start from prlim again, prlim tends to a constant matrix */
2078:
1.126 brouard 2079: int i, ii,j,k;
1.209 brouard 2080: double *min, *max, *meandiff, maxmax,sumnew=0.;
1.145 brouard 2081: /* double **matprod2(); */ /* test */
1.218 ! brouard 2082: double **out, cov[NCOVMAX+1], **pmij(); /* **pmmij is a global variable feeded with oldms etc */
1.126 brouard 2083: double **newm;
1.209 brouard 2084: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
1.203 brouard 2085: int ncvloop=0;
1.169 brouard 2086:
1.209 brouard 2087: min=vector(1,nlstate);
2088: max=vector(1,nlstate);
2089: meandiff=vector(1,nlstate);
2090:
1.218 ! brouard 2091: /* Starting with matrix unity */
1.126 brouard 2092: for (ii=1;ii<=nlstate+ndeath;ii++)
2093: for (j=1;j<=nlstate+ndeath;j++){
2094: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2095: }
1.169 brouard 2096:
2097: cov[1]=1.;
2098:
2099: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
1.202 brouard 2100: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.126 brouard 2101: for(agefin=age-stepm/YEARM; agefin>=age-delaymax; agefin=agefin-stepm/YEARM){
1.202 brouard 2102: ncvloop++;
1.126 brouard 2103: newm=savm;
2104: /* Covariates have to be included here again */
1.138 brouard 2105: cov[2]=agefin;
1.187 brouard 2106: if(nagesqr==1)
2107: cov[3]= agefin*agefin;;
1.138 brouard 2108: for (k=1; k<=cptcovn;k++) {
1.200 brouard 2109: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.218 ! brouard 2110: /* Here comes the value of the covariate 'ij' */
1.200 brouard 2111: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
1.198 brouard 2112: /* 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 2113: }
1.186 brouard 2114: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.200 brouard 2115: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2116: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
1.186 brouard 2117: for (k=1; k<=cptcovprod;k++) /* Useless */
1.200 brouard 2118: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2119: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
1.138 brouard 2120:
2121: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2122: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2123: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
1.145 brouard 2124: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2125: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 ! brouard 2126: /* age and covariate values of ij are in 'cov' */
1.142 brouard 2127: out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /* Bug Valgrind */
1.138 brouard 2128:
1.126 brouard 2129: savm=oldm;
2130: oldm=newm;
1.209 brouard 2131:
2132: for(j=1; j<=nlstate; j++){
2133: max[j]=0.;
2134: min[j]=1.;
2135: }
2136: for(i=1;i<=nlstate;i++){
2137: sumnew=0;
2138: for(k=1; k<=ndeath; k++) sumnew+=newm[i][nlstate+k];
2139: for(j=1; j<=nlstate; j++){
2140: prlim[i][j]= newm[i][j]/(1-sumnew);
2141: max[j]=FMAX(max[j],prlim[i][j]);
2142: min[j]=FMIN(min[j],prlim[i][j]);
2143: }
2144: }
2145:
1.126 brouard 2146: maxmax=0.;
1.209 brouard 2147: for(j=1; j<=nlstate; j++){
2148: meandiff[j]=(max[j]-min[j])/(max[j]+min[j])*2.; /* mean difference for each column */
2149: maxmax=FMAX(maxmax,meandiff[j]);
2150: /* 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 2151: } /* j loop */
1.203 brouard 2152: *ncvyear= (int)age- (int)agefin;
1.208 brouard 2153: /* 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 2154: if(maxmax < ftolpl){
1.209 brouard 2155: /* printf("maxmax=%lf ncvloop=%ld, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear); */
2156: free_vector(min,1,nlstate);
2157: free_vector(max,1,nlstate);
2158: free_vector(meandiff,1,nlstate);
1.126 brouard 2159: return prlim;
2160: }
1.169 brouard 2161: } /* age loop */
1.208 brouard 2162: /* After some age loop it doesn't converge */
1.209 brouard 2163: 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 2164: 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 2165: /* 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); */
2166: free_vector(min,1,nlstate);
2167: free_vector(max,1,nlstate);
2168: free_vector(meandiff,1,nlstate);
1.208 brouard 2169:
1.169 brouard 2170: return prlim; /* should not reach here */
1.126 brouard 2171: }
2172:
1.217 brouard 2173:
2174: /**** Back Prevalence limit (stable or period prevalence) ****************/
2175:
1.218 ! brouard 2176: /* 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) */
! 2177: /* 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) */
! 2178: double **bprevalim(double **bprlim, double ***prevacurrent, int nlstate, double x[], double age, double ftolpl, int *ncvyear, int ij)
1.217 brouard 2179: {
1.218 ! brouard 2180: /* Computes the prevalence limit in each live state at age x and covariate ij by left multiplying the unit
1.217 brouard 2181: matrix by transitions matrix until convergence is reached with precision ftolpl */
2182: /* Wx= Wx-1 Px-1= Wx-2 Px-2 Px-1 = Wx-n Px-n ... Px-2 Px-1 I */
2183: /* Wx is row vector: population in state 1, population in state 2, population dead */
2184: /* or prevalence in state 1, prevalence in state 2, 0 */
2185: /* newm is the matrix after multiplications, its rows are identical at a factor */
2186: /* Initial matrix pimij */
2187: /* {0.85204250825084937, 0.13044499163996345, 0.017512500109187184, */
2188: /* 0.090851990222114765, 0.88271245433047185, 0.026435555447413338, */
2189: /* 0, 0 , 1} */
2190: /*
2191: * and after some iteration: */
2192: /* {0.45504275246439968, 0.42731458730878791, 0.11764266022681241, */
2193: /* 0.45201005341706885, 0.42865420071559901, 0.11933574586733192, */
2194: /* 0, 0 , 1} */
2195: /* And prevalence by suppressing the deaths are close to identical rows in prlim: */
2196: /* {0.51571254859325999, 0.4842874514067399, */
2197: /* 0.51326036147820708, 0.48673963852179264} */
2198: /* If we start from prlim again, prlim tends to a constant matrix */
2199:
2200: int i, ii,j,k;
2201: double *min, *max, *meandiff, maxmax,sumnew=0.;
2202: /* double **matprod2(); */ /* test */
2203: double **out, cov[NCOVMAX+1], **bmij();
2204: double **newm;
1.218 ! brouard 2205: double **dnewm, **doldm, **dsavm; /* for use */
! 2206: double **oldm, **savm; /* for use */
! 2207:
1.217 brouard 2208: double agefin, delaymax=200. ; /* 100 Max number of years to converge */
2209: int ncvloop=0;
2210:
2211: min=vector(1,nlstate);
2212: max=vector(1,nlstate);
2213: meandiff=vector(1,nlstate);
2214:
1.218 ! brouard 2215: dnewm=ddnewms; doldm=ddoldms; dsavm=ddsavms;
! 2216: oldm=oldms; savm=savms;
! 2217:
! 2218: /* Starting with matrix unity */
! 2219: for (ii=1;ii<=nlstate+ndeath;ii++)
! 2220: for (j=1;j<=nlstate+ndeath;j++){
1.217 brouard 2221: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2222: }
2223:
2224: cov[1]=1.;
2225:
2226: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2227: /* Start at agefin= age, computes the matrix of passage and loops decreasing agefin until convergence is reached */
1.218 ! brouard 2228: /* for(agefin=age+stepm/YEARM; agefin<=age+delaymax; agefin=agefin+stepm/YEARM){ /\* A changer en age *\/ */
! 2229: for(agefin=age; agefin<AGESUP; agefin=agefin+stepm/YEARM){ /* A changer en age */
1.217 brouard 2230: ncvloop++;
1.218 ! brouard 2231: newm=savm; /* oldm should be kept from previous iteration or unity at start */
! 2232: /* newm points to the allocated table savm passed by the function it can be written, savm could be reallocated */
1.217 brouard 2233: /* Covariates have to be included here again */
2234: cov[2]=agefin;
2235: if(nagesqr==1)
2236: cov[3]= agefin*agefin;;
2237: for (k=1; k<=cptcovn;k++) {
2238: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
2239: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
2240: /* printf("prevalim ij=%d k=%d Tvar[%d]=%d nbcode=%d cov=%lf codtabm(%d,Tvar[%d])=%d \n",ij,k, k, Tvar[k],nbcode[Tvar[k]][codtabm(ij,Tvar[k])],cov[2+k], ij, k, codtabm(ij,Tvar[k])]); */
2241: }
2242: /*wrong? for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
2243: /* for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]*cov[2]; */
2244: for (k=1; k<=cptcovage;k++) cov[2+nagesqr+Tage[k]]=nbcode[Tvar[k]][codtabm(ij,k)]*cov[2];
2245: for (k=1; k<=cptcovprod;k++) /* Useless */
2246: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])] * nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
2247: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)] * nbcode[Tvard[k][2]][codtabm(ij,k)];
2248:
2249: /*printf("ij=%d cptcovprod=%d tvar=%d ", ij, cptcovprod, Tvar[1]);*/
2250: /*printf("ij=%d cov[3]=%lf cov[4]=%lf \n",ij, cov[3],cov[4]);*/
2251: /*printf("ij=%d cov[3]=%lf \n",ij, cov[3]);*/
2252: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
2253: /* out=matprod2(newm, pmij(pmmij,cov,ncovmodel,x,nlstate),1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); /\* Bug Valgrind *\/ */
1.218 ! brouard 2254: /* ij should be linked to the correct index of cov */
! 2255: /* age and covariate values ij are in 'cov', but we need to pass
! 2256: * ij for the observed prevalence at age and status and covariate
! 2257: * number: prevacurrent[(int)agefin][ii][ij]
! 2258: */
! 2259: /* 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 *\/ */
! 2260: /* 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 *\/ */
! 2261: 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 2262: savm=oldm;
2263: oldm=newm;
2264: for(j=1; j<=nlstate; j++){
2265: max[j]=0.;
2266: min[j]=1.;
2267: }
2268: for(j=1; j<=nlstate; j++){
2269: for(i=1;i<=nlstate;i++){
1.218 ! brouard 2270: /* bprlim[i][j]= newm[i][j]/(1-sumnew); */
! 2271: bprlim[i][j]= newm[i][j];
! 2272: max[i]=FMAX(max[i],bprlim[i][j]); /* Max in line */
! 2273: min[i]=FMIN(min[i],bprlim[i][j]);
1.217 brouard 2274: }
2275: }
1.218 ! brouard 2276:
1.217 brouard 2277: maxmax=0.;
2278: for(i=1; i<=nlstate; i++){
2279: meandiff[i]=(max[i]-min[i])/(max[i]+min[i])*2.; /* mean difference for each column */
2280: maxmax=FMAX(maxmax,meandiff[i]);
2281: /* 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); */
2282: } /* j loop */
2283: *ncvyear= -( (int)age- (int)agefin);
1.218 ! brouard 2284: /* printf("Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);*/
1.217 brouard 2285: if(maxmax < ftolpl){
2286: printf("OK Back maxmax=%lf ncvloop=%d, age=%d, agefin=%d ncvyear=%d \n", maxmax, ncvloop, (int)age, (int)agefin, *ncvyear);
2287: free_vector(min,1,nlstate);
2288: free_vector(max,1,nlstate);
2289: free_vector(meandiff,1,nlstate);
2290: return bprlim;
2291: }
2292: } /* age loop */
2293: /* After some age loop it doesn't converge */
2294: 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\
2295: 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);
2296: /* 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); */
2297: free_vector(min,1,nlstate);
2298: free_vector(max,1,nlstate);
2299: free_vector(meandiff,1,nlstate);
2300:
2301: return bprlim; /* should not reach here */
2302: }
2303:
1.126 brouard 2304: /*************** transition probabilities ***************/
2305:
2306: double **pmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
2307: {
1.138 brouard 2308: /* According to parameters values stored in x and the covariate's values stored in cov,
2309: computes the probability to be observed in state j being in state i by appying the
2310: model to the ncovmodel covariates (including constant and age).
2311: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2312: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2313: ncth covariate in the global vector x is given by the formula:
2314: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2315: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2316: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2317: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2318: Outputs ps[i][j] the probability to be observed in j being in j according to
2319: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2320: */
2321: double s1, lnpijopii;
1.126 brouard 2322: /*double t34;*/
1.164 brouard 2323: int i,j, nc, ii, jj;
1.126 brouard 2324:
1.218 ! brouard 2325: for(i=1; i<= nlstate; i++){
! 2326: for(j=1; j<i;j++){
! 2327: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
! 2328: /*lnpijopii += param[i][j][nc]*cov[nc];*/
! 2329: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
! 2330: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
! 2331: }
! 2332: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
! 2333: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
! 2334: }
! 2335: for(j=i+1; j<=nlstate+ndeath;j++){
! 2336: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
! 2337: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
! 2338: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
! 2339: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
! 2340: }
! 2341: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
! 2342: }
! 2343: }
! 2344:
! 2345: for(i=1; i<= nlstate; i++){
! 2346: s1=0;
! 2347: for(j=1; j<i; j++){
! 2348: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
! 2349: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
! 2350: }
! 2351: for(j=i+1; j<=nlstate+ndeath; j++){
! 2352: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
! 2353: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
! 2354: }
! 2355: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
! 2356: ps[i][i]=1./(s1+1.);
! 2357: /* Computing other pijs */
! 2358: for(j=1; j<i; j++)
! 2359: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 2360: for(j=i+1; j<=nlstate+ndeath; j++)
! 2361: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 2362: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
! 2363: } /* end i */
! 2364:
! 2365: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
! 2366: for(jj=1; jj<= nlstate+ndeath; jj++){
! 2367: ps[ii][jj]=0;
! 2368: ps[ii][ii]=1;
! 2369: }
! 2370: }
! 2371:
! 2372:
! 2373: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
! 2374: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
! 2375: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
! 2376: /* } */
! 2377: /* printf("\n "); */
! 2378: /* } */
! 2379: /* printf("\n ");printf("%lf ",cov[2]);*/
! 2380: /*
! 2381: for(i=1; i<= npar; i++) printf("%f ",x[i]);
! 2382: goto end;*/
! 2383: return ps;
1.126 brouard 2384: }
2385:
1.218 ! brouard 2386: /*************** backward transition probabilities ***************/
! 2387:
! 2388: /* 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 ) */
! 2389: /* double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, double ***dnewm, double **doldm, double **dsavm, int ij ) */
! 2390: double **bmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate, double ***prevacurrent, int ij )
! 2391: {
! 2392: /* Computes the backward probability at age agefin and covariate ij
! 2393: * and returns in **ps as well as **bmij.
! 2394: */
! 2395: int i, ii, j,k;
! 2396:
! 2397: double **out, **pmij();
! 2398: double sumnew=0.;
! 2399: double agefin;
! 2400:
! 2401: double **dnewm, **dsavm, **doldm;
! 2402: double **bbmij;
! 2403:
! 2404: doldm=ddoldms; /* global pointers */
! 2405: dnewm=ddnewms;
! 2406: dsavm=ddsavms;
! 2407:
! 2408: agefin=cov[2];
! 2409: /* bmij *//* age is cov[2], ij is included in cov, but we need for
! 2410: the observed prevalence (with this covariate ij) */
! 2411: dsavm=pmij(pmmij,cov,ncovmodel,x,nlstate);
! 2412: /* We do have the matrix Px in savm and we need pij */
! 2413: for (j=1;j<=nlstate+ndeath;j++){
! 2414: sumnew=0.; /* w1 p11 + w2 p21 only on live states */
! 2415: for (ii=1;ii<=nlstate;ii++){
! 2416: sumnew+=dsavm[ii][j]*prevacurrent[(int)agefin][ii][ij];
! 2417: } /* sumnew is (N11+N21)/N..= N.1/N.. = sum on i of w_i pij */
! 2418: for (ii=1;ii<=nlstate+ndeath;ii++){
! 2419: if(sumnew >= 1.e-10){
! 2420: /* if(agefin >= agemaxpar && agefin <= agemaxpar+stepm/YEARM){ */
! 2421: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
! 2422: /* }else if(agefin >= agemaxpar+stepm/YEARM){ */
! 2423: /* doldm[ii][j]=(ii==j ? 1./sumnew : 0.0); */
! 2424: /* }else */
! 2425: doldm[ii][j]=(ii==j ? 1./sumnew : 0.0);
! 2426: }else{
! 2427: 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);
! 2428: }
! 2429: } /*End ii */
! 2430: } /* End j, At the end doldm is diag[1/(w_1p1i+w_2 p2i)] */
! 2431: /* left Product of this diag matrix by dsavm=Px (newm=dsavm*doldm) */
! 2432: bbmij=matprod2(dnewm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, doldm); /* Bug Valgrind */
! 2433: /* dsavm=doldm; /\* dsavm is now diag [1/(w_1p1i+w_2 p2i)] but can be overwritten*\/ */
! 2434: /* doldm=dnewm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
! 2435: /* dnewm=dsavm; /\* doldm is now Px * diag [1/(w_1p1i+w_2 p2i)] *\/ */
! 2436: /* left Product of this matrix by diag matrix of prevalences (savm) */
! 2437: for (j=1;j<=nlstate+ndeath;j++){
! 2438: for (ii=1;ii<=nlstate+ndeath;ii++){
! 2439: dsavm[ii][j]=(ii==j ? prevacurrent[(int)agefin][ii][ij] : 0.0);
! 2440: }
! 2441: } /* End j, At the end oldm is diag[1/(w_1p1i+w_2 p2i)] */
! 2442: ps=matprod2(doldm, dsavm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, dnewm); /* Bug Valgrind */
! 2443: /* newm or out is now diag[w_i] * Px * diag [1/(w_1p1i+w_2 p2i)] */
! 2444: /* end bmij */
! 2445: return ps;
! 2446: }
1.217 brouard 2447: /*************** transition probabilities ***************/
2448:
1.218 ! brouard 2449: double **bpmij(double **ps, double *cov, int ncovmodel, double *x, int nlstate )
1.217 brouard 2450: {
2451: /* According to parameters values stored in x and the covariate's values stored in cov,
2452: computes the probability to be observed in state j being in state i by appying the
2453: model to the ncovmodel covariates (including constant and age).
2454: lnpijopii=ln(pij/pii)= aij+bij*age+cij*v1+dij*v2+... = sum_nc=1^ncovmodel xij(nc)*cov[nc]
2455: and, according on how parameters are entered, the position of the coefficient xij(nc) of the
2456: ncth covariate in the global vector x is given by the formula:
2457: j<i nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel
2458: j>=i nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel
2459: Computes ln(pij/pii) (lnpijopii), deduces pij/pii by exponentiation,
2460: sums on j different of i to get 1-pii/pii, deduces pii, and then all pij.
2461: Outputs ps[i][j] the probability to be observed in j being in j according to
2462: the values of the covariates cov[nc] and corresponding parameter values x[nc+shiftij]
2463: */
2464: double s1, lnpijopii;
2465: /*double t34;*/
2466: int i,j, nc, ii, jj;
2467:
1.218 ! brouard 2468: for(i=1; i<= nlstate; i++){
! 2469: for(j=1; j<i;j++){
! 2470: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
! 2471: /*lnpijopii += param[i][j][nc]*cov[nc];*/
! 2472: lnpijopii += x[nc+((i-1)*(nlstate+ndeath-1)+j-1)*ncovmodel]*cov[nc];
! 2473: /* printf("Int j<i s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
! 2474: }
! 2475: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
! 2476: /* printf("s1=%.17e, lnpijopii=%.17e\n",s1,lnpijopii); */
! 2477: }
! 2478: for(j=i+1; j<=nlstate+ndeath;j++){
! 2479: for (nc=1, lnpijopii=0.;nc <=ncovmodel; nc++){
! 2480: /*lnpijopii += x[(i-1)*nlstate*ncovmodel+(j-2)*ncovmodel+nc+(i-1)*(ndeath-1)*ncovmodel]*cov[nc];*/
! 2481: lnpijopii += x[nc + ((i-1)*(nlstate+ndeath-1)+(j-2))*ncovmodel]*cov[nc];
! 2482: /* printf("Int j>i s1=%.17e, lnpijopii=%.17e %lx %lx\n",s1,lnpijopii,s1,lnpijopii); */
! 2483: }
! 2484: ps[i][j]=lnpijopii; /* In fact ln(pij/pii) */
! 2485: }
! 2486: }
! 2487:
! 2488: for(i=1; i<= nlstate; i++){
! 2489: s1=0;
! 2490: for(j=1; j<i; j++){
! 2491: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
! 2492: /*printf("debug1 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
! 2493: }
! 2494: for(j=i+1; j<=nlstate+ndeath; j++){
! 2495: s1+=exp(ps[i][j]); /* In fact sums pij/pii */
! 2496: /*printf("debug2 %d %d ps=%lf exp(ps)=%lf s1+=%lf\n",i,j,ps[i][j],exp(ps[i][j]),s1); */
! 2497: }
! 2498: /* s1= sum_{j<>i} pij/pii=(1-pii)/pii and thus pii is known from s1 */
! 2499: ps[i][i]=1./(s1+1.);
! 2500: /* Computing other pijs */
! 2501: for(j=1; j<i; j++)
! 2502: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 2503: for(j=i+1; j<=nlstate+ndeath; j++)
! 2504: ps[i][j]= exp(ps[i][j])*ps[i][i];
! 2505: /* ps[i][nlstate+1]=1.-s1- ps[i][i];*/ /* Sum should be 1 */
! 2506: } /* end i */
! 2507:
! 2508: for(ii=nlstate+1; ii<= nlstate+ndeath; ii++){
! 2509: for(jj=1; jj<= nlstate+ndeath; jj++){
! 2510: ps[ii][jj]=0;
! 2511: ps[ii][ii]=1;
! 2512: }
! 2513: }
! 2514: /* Added for backcast */ /* Transposed matrix too */
! 2515: for(jj=1; jj<= nlstate+ndeath; jj++){
! 2516: s1=0.;
! 2517: for(ii=1; ii<= nlstate+ndeath; ii++){
! 2518: s1+=ps[ii][jj];
! 2519: }
! 2520: for(ii=1; ii<= nlstate; ii++){
! 2521: ps[ii][jj]=ps[ii][jj]/s1;
! 2522: }
! 2523: }
! 2524: /* Transposition */
! 2525: for(jj=1; jj<= nlstate+ndeath; jj++){
! 2526: for(ii=jj; ii<= nlstate+ndeath; ii++){
! 2527: s1=ps[ii][jj];
! 2528: ps[ii][jj]=ps[jj][ii];
! 2529: ps[jj][ii]=s1;
! 2530: }
! 2531: }
! 2532: /* for(ii=1; ii<= nlstate+ndeath; ii++){ */
! 2533: /* for(jj=1; jj<= nlstate+ndeath; jj++){ */
! 2534: /* printf(" pmij ps[%d][%d]=%lf ",ii,jj,ps[ii][jj]); */
! 2535: /* } */
! 2536: /* printf("\n "); */
! 2537: /* } */
! 2538: /* printf("\n ");printf("%lf ",cov[2]);*/
! 2539: /*
! 2540: for(i=1; i<= npar; i++) printf("%f ",x[i]);
! 2541: goto end;*/
! 2542: return ps;
1.217 brouard 2543: }
2544:
2545:
1.126 brouard 2546: /**************** Product of 2 matrices ******************/
2547:
1.145 brouard 2548: double **matprod2(double **out, double **in,int nrl, int nrh, int ncl, int nch, int ncolol, int ncoloh, double **b)
1.126 brouard 2549: {
2550: /* Computes the matrix product of in(1,nrh-nrl+1)(1,nch-ncl+1) times
2551: b(1,nch-ncl+1)(1,ncoloh-ncolol+1) into out(...) */
2552: /* in, b, out are matrice of pointers which should have been initialized
2553: before: only the contents of out is modified. The function returns
2554: a pointer to pointers identical to out */
1.145 brouard 2555: int i, j, k;
1.126 brouard 2556: for(i=nrl; i<= nrh; i++)
1.145 brouard 2557: for(k=ncolol; k<=ncoloh; k++){
2558: out[i][k]=0.;
2559: for(j=ncl; j<=nch; j++)
2560: out[i][k] +=in[i][j]*b[j][k];
2561: }
1.126 brouard 2562: return out;
2563: }
2564:
2565:
2566: /************* Higher Matrix Product ***************/
2567:
2568: double ***hpxij(double ***po, int nhstepm, double age, int hstepm, double *x, int nlstate, int stepm, double **oldm, double **savm, int ij )
2569: {
1.218 ! brouard 2570: /* Computes the transition matrix starting at age 'age' and combination of covariate values corresponding to ij over
1.126 brouard 2571: 'nhstepm*hstepm*stepm' months (i.e. until
2572: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
2573: nhstepm*hstepm matrices.
2574: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
2575: (typically every 2 years instead of every month which is too big
2576: for the memory).
2577: Model is determined by parameters x and covariates have to be
2578: included manually here.
2579:
2580: */
2581:
2582: int i, j, d, h, k;
1.131 brouard 2583: double **out, cov[NCOVMAX+1];
1.126 brouard 2584: double **newm;
1.187 brouard 2585: double agexact;
1.214 brouard 2586: double agebegin, ageend;
1.126 brouard 2587:
2588: /* Hstepm could be zero and should return the unit matrix */
2589: for (i=1;i<=nlstate+ndeath;i++)
2590: for (j=1;j<=nlstate+ndeath;j++){
2591: oldm[i][j]=(i==j ? 1.0 : 0.0);
2592: po[i][j][0]=(i==j ? 1.0 : 0.0);
2593: }
2594: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2595: for(h=1; h <=nhstepm; h++){
2596: for(d=1; d <=hstepm; d++){
2597: newm=savm;
2598: /* Covariates have to be included here again */
2599: cov[1]=1.;
1.214 brouard 2600: agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
1.187 brouard 2601: cov[2]=agexact;
2602: if(nagesqr==1)
1.218 ! brouard 2603: cov[3]= agexact*agexact;
1.131 brouard 2604: for (k=1; k<=cptcovn;k++)
1.218 ! brouard 2605: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
! 2606: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.186 brouard 2607: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.218 ! brouard 2608: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
! 2609: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
! 2610: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.145 brouard 2611: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.218 ! brouard 2612: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
! 2613: /* 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 2614:
2615:
2616: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2617: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 ! brouard 2618: /* right multiplication of oldm by the current matrix */
1.126 brouard 2619: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath,
2620: pmij(pmmij,cov,ncovmodel,x,nlstate));
1.217 brouard 2621: /* if((int)age == 70){ */
2622: /* printf(" Forward hpxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2623: /* for(i=1; i<=nlstate+ndeath; i++) { */
2624: /* printf("%d pmmij ",i); */
2625: /* for(j=1;j<=nlstate+ndeath;j++) { */
2626: /* printf("%f ",pmmij[i][j]); */
2627: /* } */
2628: /* printf(" oldm "); */
2629: /* for(j=1;j<=nlstate+ndeath;j++) { */
2630: /* printf("%f ",oldm[i][j]); */
2631: /* } */
2632: /* printf("\n"); */
2633: /* } */
2634: /* } */
1.126 brouard 2635: savm=oldm;
2636: oldm=newm;
2637: }
2638: for(i=1; i<=nlstate+ndeath; i++)
2639: for(j=1;j<=nlstate+ndeath;j++) {
1.218 ! brouard 2640: po[i][j][h]=newm[i][j];
! 2641: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.126 brouard 2642: }
1.128 brouard 2643: /*printf("h=%d ",h);*/
1.126 brouard 2644: } /* end h */
1.218 ! brouard 2645: /* printf("\n H=%d \n",h); */
1.126 brouard 2646: return po;
2647: }
2648:
1.217 brouard 2649: /************* Higher Back Matrix Product ***************/
1.218 ! brouard 2650: /* 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 ) */
! 2651: double ***hbxij(double ***po, int nhstepm, double age, int hstepm, double *x, double ***prevacurrent, int nlstate, int stepm, int ij )
1.217 brouard 2652: {
1.218 ! brouard 2653: /* Computes the transition matrix starting at age 'age' over
1.217 brouard 2654: 'nhstepm*hstepm*stepm' months (i.e. until
1.218 ! brouard 2655: age (in years) age+nhstepm*hstepm*stepm/12) by multiplying
! 2656: nhstepm*hstepm matrices.
! 2657: Output is stored in matrix po[i][j][h] for h every 'hstepm' step
! 2658: (typically every 2 years instead of every month which is too big
1.217 brouard 2659: for the memory).
1.218 ! brouard 2660: Model is determined by parameters x and covariates have to be
! 2661: included manually here.
1.217 brouard 2662:
2663: */
2664:
2665: int i, j, d, h, k;
2666: double **out, cov[NCOVMAX+1];
2667: double **newm;
2668: double agexact;
2669: double agebegin, ageend;
1.218 ! brouard 2670: double **oldm, **savm;
1.217 brouard 2671:
1.218 ! brouard 2672: oldm=oldms;savm=savms;
1.217 brouard 2673: /* Hstepm could be zero and should return the unit matrix */
2674: for (i=1;i<=nlstate+ndeath;i++)
2675: for (j=1;j<=nlstate+ndeath;j++){
2676: oldm[i][j]=(i==j ? 1.0 : 0.0);
2677: po[i][j][0]=(i==j ? 1.0 : 0.0);
2678: }
2679: /* Even if hstepm = 1, at least one multiplication by the unit matrix */
2680: for(h=1; h <=nhstepm; h++){
2681: for(d=1; d <=hstepm; d++){
2682: newm=savm;
2683: /* Covariates have to be included here again */
2684: cov[1]=1.;
2685: agexact=age-((h-1)*hstepm + (d-1))*stepm/YEARM; /* age just before transition */
2686: /* agexact=age+((h-1)*hstepm + (d-1))*stepm/YEARM; /\* age just before transition *\/ */
2687: cov[2]=agexact;
2688: if(nagesqr==1)
1.218 ! brouard 2689: cov[3]= agexact*agexact;
! 2690: for (k=1; k<=cptcovn;k++)
! 2691: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,k)];
! 2692: /* cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.217 brouard 2693: for (k=1; k<=cptcovage;k++) /* Should start at cptcovn+1 */
1.218 ! brouard 2694: /* cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
! 2695: cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
! 2696: /* cov[2+nagesqr+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k]])]*cov[2]; */
1.217 brouard 2697: for (k=1; k<=cptcovprod;k++) /* Useless because included in cptcovn */
1.218 ! brouard 2698: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
! 2699: /* cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,Tvard[k][1])]*nbcode[Tvard[k][2]][codtabm(ij,Tvard[k][2])]; */
! 2700:
! 2701:
1.217 brouard 2702: /*printf("hxi cptcov=%d cptcode=%d\n",cptcov,cptcode);*/
2703: /*printf("h=%d d=%d age=%f cov=%f\n",h,d,age,cov[2]);*/
1.218 ! brouard 2704: /* Careful transposed matrix */
! 2705: /* age is in cov[2] */
! 2706: /* out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent, dnewm, doldm, dsavm,ij),\ */
! 2707: /* 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm); */
! 2708: out=matprod2(newm, bmij(pmmij,cov,ncovmodel,x,nlstate,prevacurrent,ij),\
! 2709: 1,nlstate+ndeath,1,nlstate+ndeath,1,nlstate+ndeath, oldm);
1.217 brouard 2710: /* if((int)age == 70){ */
2711: /* printf(" Backward hbxij age=%d agexact=%f d=%d nhstepm=%d hstepm=%d\n", (int) age, agexact, d, nhstepm, hstepm); */
2712: /* for(i=1; i<=nlstate+ndeath; i++) { */
2713: /* printf("%d pmmij ",i); */
2714: /* for(j=1;j<=nlstate+ndeath;j++) { */
2715: /* printf("%f ",pmmij[i][j]); */
2716: /* } */
2717: /* printf(" oldm "); */
2718: /* for(j=1;j<=nlstate+ndeath;j++) { */
2719: /* printf("%f ",oldm[i][j]); */
2720: /* } */
2721: /* printf("\n"); */
2722: /* } */
2723: /* } */
2724: savm=oldm;
2725: oldm=newm;
2726: }
2727: for(i=1; i<=nlstate+ndeath; i++)
2728: for(j=1;j<=nlstate+ndeath;j++) {
1.218 ! brouard 2729: po[i][j][h]=newm[i][j];
! 2730: /*if(h==nhstepm) printf("po[%d][%d][%d]=%f ",i,j,h,po[i][j][h]);*/
1.217 brouard 2731: }
2732: /*printf("h=%d ",h);*/
2733: } /* end h */
1.218 ! brouard 2734: /* printf("\n H=%d \n",h); */
1.217 brouard 2735: return po;
2736: }
2737:
2738:
1.162 brouard 2739: #ifdef NLOPT
2740: double myfunc(unsigned n, const double *p1, double *grad, void *pd){
2741: double fret;
2742: double *xt;
2743: int j;
2744: myfunc_data *d2 = (myfunc_data *) pd;
2745: /* xt = (p1-1); */
2746: xt=vector(1,n);
2747: for (j=1;j<=n;j++) xt[j]=p1[j-1]; /* xt[1]=p1[0] */
2748:
2749: fret=(d2->function)(xt); /* p xt[1]@8 is fine */
2750: /* fret=(*func)(xt); /\* p xt[1]@8 is fine *\/ */
2751: printf("Function = %.12lf ",fret);
2752: for (j=1;j<=n;j++) printf(" %d %.8lf", j, xt[j]);
2753: printf("\n");
2754: free_vector(xt,1,n);
2755: return fret;
2756: }
2757: #endif
1.126 brouard 2758:
2759: /*************** log-likelihood *************/
2760: double func( double *x)
2761: {
2762: int i, ii, j, k, mi, d, kk;
1.131 brouard 2763: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 2764: double **out;
2765: double sw; /* Sum of weights */
2766: double lli; /* Individual log likelihood */
2767: int s1, s2;
2768: double bbh, survp;
2769: long ipmx;
1.187 brouard 2770: double agexact;
1.126 brouard 2771: /*extern weight */
2772: /* We are differentiating ll according to initial status */
2773: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
2774: /*for(i=1;i<imx;i++)
2775: printf(" %d\n",s[4][i]);
2776: */
1.162 brouard 2777:
2778: ++countcallfunc;
2779:
1.126 brouard 2780: cov[1]=1.;
2781:
2782: for(k=1; k<=nlstate; k++) ll[k]=0.;
2783:
2784: if(mle==1){
2785: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.138 brouard 2786: /* Computes the values of the ncovmodel covariates of the model
2787: depending if the covariates are fixed or variying (age dependent) and stores them in cov[]
2788: Then computes with function pmij which return a matrix p[i][j] giving the elementary probability
2789: to be observed in j being in i according to the model.
2790: */
1.145 brouard 2791: for (k=1; k<=cptcovn;k++){ /* Simple and product covariates without age* products */
1.187 brouard 2792: cov[2+nagesqr+k]=covar[Tvar[k]][i];
1.145 brouard 2793: }
1.137 brouard 2794: /* In model V2+V1*V4+age*V3+V3*V2 Tvar[1] is V2, Tvar[2=V1*V4]
1.138 brouard 2795: is 6, Tvar[3=age*V3] should not be computed because of age Tvar[4=V3*V2]
1.137 brouard 2796: has been calculated etc */
1.126 brouard 2797: for(mi=1; mi<= wav[i]-1; mi++){
2798: for (ii=1;ii<=nlstate+ndeath;ii++)
2799: for (j=1;j<=nlstate+ndeath;j++){
2800: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2801: savm[ii][j]=(ii==j ? 1.0 : 0.0);
2802: }
2803: for(d=0; d<dh[mi][i]; d++){
2804: newm=savm;
1.187 brouard 2805: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
2806: cov[2]=agexact;
2807: if(nagesqr==1)
2808: cov[3]= agexact*agexact;
1.126 brouard 2809: for (kk=1; kk<=cptcovage;kk++) {
1.187 brouard 2810: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact; /* Tage[kk] gives the data-covariate associated with age */
1.126 brouard 2811: }
2812: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
2813: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
2814: savm=oldm;
2815: oldm=newm;
2816: } /* end mult */
2817:
2818: /*lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]);*/ /* Original formula */
2819: /* But now since version 0.9 we anticipate for bias at large stepm.
2820: * If stepm is larger than one month (smallest stepm) and if the exact delay
2821: * (in months) between two waves is not a multiple of stepm, we rounded to
2822: * the nearest (and in case of equal distance, to the lowest) interval but now
2823: * we keep into memory the bias bh[mi][i] and also the previous matrix product
2824: * (i.e to dh[mi][i]-1) saved in 'savm'. Then we inter(extra)polate the
2825: * probability in order to take into account the bias as a fraction of the way
2826: * from savm to out if bh is negative or even beyond if bh is positive. bh varies
2827: * -stepm/2 to stepm/2 .
2828: * For stepm=1 the results are the same as for previous versions of Imach.
2829: * For stepm > 1 the results are less biased than in previous versions.
2830: */
2831: s1=s[mw[mi][i]][i];
2832: s2=s[mw[mi+1][i]][i];
2833: bbh=(double)bh[mi][i]/(double)stepm;
2834: /* bias bh is positive if real duration
2835: * is higher than the multiple of stepm and negative otherwise.
2836: */
2837: /* lli= (savm[s1][s2]>1.e-8 ?(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]):log((1.+bbh)*out[s1][s2]));*/
2838: if( s2 > nlstate){
2839: /* i.e. if s2 is a death state and if the date of death is known
2840: then the contribution to the likelihood is the probability to
2841: die between last step unit time and current step unit time,
2842: which is also equal to probability to die before dh
2843: minus probability to die before dh-stepm .
2844: In version up to 0.92 likelihood was computed
2845: as if date of death was unknown. Death was treated as any other
2846: health state: the date of the interview describes the actual state
2847: and not the date of a change in health state. The former idea was
2848: to consider that at each interview the state was recorded
2849: (healthy, disable or death) and IMaCh was corrected; but when we
2850: introduced the exact date of death then we should have modified
2851: the contribution of an exact death to the likelihood. This new
2852: contribution is smaller and very dependent of the step unit
2853: stepm. It is no more the probability to die between last interview
2854: and month of death but the probability to survive from last
2855: interview up to one month before death multiplied by the
2856: probability to die within a month. Thanks to Chris
2857: Jackson for correcting this bug. Former versions increased
2858: mortality artificially. The bad side is that we add another loop
2859: which slows down the processing. The difference can be up to 10%
2860: lower mortality.
2861: */
1.183 brouard 2862: /* If, at the beginning of the maximization mostly, the
2863: cumulative probability or probability to be dead is
2864: constant (ie = 1) over time d, the difference is equal to
2865: 0. out[s1][3] = savm[s1][3]: probability, being at state
2866: s1 at precedent wave, to be dead a month before current
2867: wave is equal to probability, being at state s1 at
2868: precedent wave, to be dead at mont of the current
2869: wave. Then the observed probability (that this person died)
2870: is null according to current estimated parameter. In fact,
2871: it should be very low but not zero otherwise the log go to
2872: infinity.
2873: */
2874: /* #ifdef INFINITYORIGINAL */
2875: /* lli=log(out[s1][s2] - savm[s1][s2]); */
2876: /* #else */
2877: /* if ((out[s1][s2] - savm[s1][s2]) < mytinydouble) */
2878: /* lli=log(mytinydouble); */
2879: /* else */
2880: /* lli=log(out[s1][s2] - savm[s1][s2]); */
2881: /* #endif */
1.216 brouard 2882: lli=log(out[s1][s2] - savm[s1][s2]);
2883:
2884: } else if ( s2==-1 ) { /* alive */
1.126 brouard 2885: for (j=1,survp=0. ; j<=nlstate; j++)
2886: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
2887: /*survp += out[s1][j]; */
2888: lli= log(survp);
2889: }
2890: else if (s2==-4) {
2891: for (j=3,survp=0. ; j<=nlstate; j++)
2892: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
2893: lli= log(survp);
2894: }
2895: else if (s2==-5) {
2896: for (j=1,survp=0. ; j<=2; j++)
2897: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
2898: lli= log(survp);
2899: }
2900: else{
2901: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
2902: /* 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 */
2903: }
2904: /*lli=(1.+bbh)*log(out[s1][s2])- bbh*log(savm[s1][s2]);*/
2905: /*if(lli ==000.0)*/
2906: /*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); */
2907: ipmx +=1;
2908: sw += weight[i];
2909: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.183 brouard 2910: /* if (lli < log(mytinydouble)){ */
2911: /* 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); */
2912: /* 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]); */
2913: /* } */
1.126 brouard 2914: } /* end of wave */
2915: } /* end of individual */
2916: } else if(mle==2){
2917: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.187 brouard 2918: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
1.126 brouard 2919: for(mi=1; mi<= wav[i]-1; mi++){
2920: for (ii=1;ii<=nlstate+ndeath;ii++)
2921: for (j=1;j<=nlstate+ndeath;j++){
2922: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2923: savm[ii][j]=(ii==j ? 1.0 : 0.0);
2924: }
2925: for(d=0; d<=dh[mi][i]; d++){
2926: newm=savm;
1.187 brouard 2927: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
2928: cov[2]=agexact;
2929: if(nagesqr==1)
2930: cov[3]= agexact*agexact;
1.126 brouard 2931: for (kk=1; kk<=cptcovage;kk++) {
1.187 brouard 2932: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
1.126 brouard 2933: }
2934: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
2935: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
2936: savm=oldm;
2937: oldm=newm;
2938: } /* end mult */
2939:
2940: s1=s[mw[mi][i]][i];
2941: s2=s[mw[mi+1][i]][i];
2942: bbh=(double)bh[mi][i]/(double)stepm;
2943: 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 */
2944: ipmx +=1;
2945: sw += weight[i];
2946: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
2947: } /* end of wave */
2948: } /* end of individual */
2949: } else if(mle==3){ /* exponential inter-extrapolation */
2950: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.187 brouard 2951: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
1.126 brouard 2952: for(mi=1; mi<= wav[i]-1; mi++){
2953: for (ii=1;ii<=nlstate+ndeath;ii++)
2954: for (j=1;j<=nlstate+ndeath;j++){
2955: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2956: savm[ii][j]=(ii==j ? 1.0 : 0.0);
2957: }
2958: for(d=0; d<dh[mi][i]; d++){
2959: newm=savm;
1.187 brouard 2960: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
2961: cov[2]=agexact;
2962: if(nagesqr==1)
2963: cov[3]= agexact*agexact;
1.126 brouard 2964: for (kk=1; kk<=cptcovage;kk++) {
1.187 brouard 2965: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
1.126 brouard 2966: }
2967: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
2968: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
2969: savm=oldm;
2970: oldm=newm;
2971: } /* end mult */
2972:
2973: s1=s[mw[mi][i]][i];
2974: s2=s[mw[mi+1][i]][i];
2975: bbh=(double)bh[mi][i]/(double)stepm;
2976: 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 */
2977: ipmx +=1;
2978: sw += weight[i];
2979: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
2980: } /* end of wave */
2981: } /* end of individual */
2982: }else if (mle==4){ /* ml=4 no inter-extrapolation */
2983: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.187 brouard 2984: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
1.126 brouard 2985: for(mi=1; mi<= wav[i]-1; mi++){
2986: for (ii=1;ii<=nlstate+ndeath;ii++)
2987: for (j=1;j<=nlstate+ndeath;j++){
2988: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
2989: savm[ii][j]=(ii==j ? 1.0 : 0.0);
2990: }
2991: for(d=0; d<dh[mi][i]; d++){
2992: newm=savm;
1.187 brouard 2993: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
2994: cov[2]=agexact;
2995: if(nagesqr==1)
2996: cov[3]= agexact*agexact;
1.126 brouard 2997: for (kk=1; kk<=cptcovage;kk++) {
1.187 brouard 2998: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
1.126 brouard 2999: }
3000:
3001: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3002: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3003: savm=oldm;
3004: oldm=newm;
3005: } /* end mult */
3006:
3007: s1=s[mw[mi][i]][i];
3008: s2=s[mw[mi+1][i]][i];
3009: if( s2 > nlstate){
3010: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3011: } else if ( s2==-1 ) { /* alive */
3012: for (j=1,survp=0. ; j<=nlstate; j++)
3013: survp += out[s1][j];
3014: lli= log(survp);
1.126 brouard 3015: }else{
3016: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3017: }
3018: ipmx +=1;
3019: sw += weight[i];
3020: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3021: /* 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]); */
3022: } /* end of wave */
3023: } /* end of individual */
3024: }else{ /* ml=5 no inter-extrapolation no jackson =0.8a */
3025: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.187 brouard 3026: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
1.126 brouard 3027: for(mi=1; mi<= wav[i]-1; mi++){
3028: for (ii=1;ii<=nlstate+ndeath;ii++)
3029: for (j=1;j<=nlstate+ndeath;j++){
3030: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3031: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3032: }
3033: for(d=0; d<dh[mi][i]; d++){
3034: newm=savm;
1.187 brouard 3035: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3036: cov[2]=agexact;
3037: if(nagesqr==1)
3038: cov[3]= agexact*agexact;
1.126 brouard 3039: for (kk=1; kk<=cptcovage;kk++) {
1.187 brouard 3040: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
1.126 brouard 3041: }
3042:
3043: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3044: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
3045: savm=oldm;
3046: oldm=newm;
3047: } /* end mult */
3048:
3049: s1=s[mw[mi][i]][i];
3050: s2=s[mw[mi+1][i]][i];
3051: lli=log(out[s[mw[mi][i]][i]][s[mw[mi+1][i]][i]]); /* Original formula */
3052: ipmx +=1;
3053: sw += weight[i];
3054: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
3055: /*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]);*/
3056: } /* end of wave */
3057: } /* end of individual */
3058: } /* End of if */
3059: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3060: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3061: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3062: return -l;
3063: }
3064:
3065: /*************** log-likelihood *************/
3066: double funcone( double *x)
3067: {
3068: /* Same as likeli but slower because of a lot of printf and if */
3069: int i, ii, j, k, mi, d, kk;
1.131 brouard 3070: double l, ll[NLSTATEMAX+1], cov[NCOVMAX+1];
1.126 brouard 3071: double **out;
3072: double lli; /* Individual log likelihood */
3073: double llt;
3074: int s1, s2;
3075: double bbh, survp;
1.187 brouard 3076: double agexact;
1.214 brouard 3077: double agebegin, ageend;
1.126 brouard 3078: /*extern weight */
3079: /* We are differentiating ll according to initial status */
3080: /* for (i=1;i<=npar;i++) printf("%f ", x[i]);*/
3081: /*for(i=1;i<imx;i++)
3082: printf(" %d\n",s[4][i]);
3083: */
3084: cov[1]=1.;
3085:
3086: for(k=1; k<=nlstate; k++) ll[k]=0.;
3087:
3088: for (i=1,ipmx=0, sw=0.; i<=imx; i++){
1.187 brouard 3089: for (k=1; k<=cptcovn;k++) cov[2+nagesqr+k]=covar[Tvar[k]][i];
1.126 brouard 3090: for(mi=1; mi<= wav[i]-1; mi++){
3091: for (ii=1;ii<=nlstate+ndeath;ii++)
3092: for (j=1;j<=nlstate+ndeath;j++){
3093: oldm[ii][j]=(ii==j ? 1.0 : 0.0);
3094: savm[ii][j]=(ii==j ? 1.0 : 0.0);
3095: }
1.214 brouard 3096:
3097: agebegin=agev[mw[mi][i]][i]; /* Age at beginning of effective wave */
3098: ageend=agev[mw[mi][i]][i] + (dh[mi][i])*stepm/YEARM; /* Age at end of effective wave and at the end of transition */
3099: for(d=0; d<dh[mi][i]; d++){ /* Delay between two effective waves */
3100: /*dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
3101: and mw[mi+1][i]. dh depends on stepm.*/
1.126 brouard 3102: newm=savm;
1.187 brouard 3103: agexact=agev[mw[mi][i]][i]+d*stepm/YEARM;
3104: cov[2]=agexact;
3105: if(nagesqr==1)
3106: cov[3]= agexact*agexact;
1.126 brouard 3107: for (kk=1; kk<=cptcovage;kk++) {
1.187 brouard 3108: cov[Tage[kk]+2+nagesqr]=covar[Tvar[Tage[kk]]][i]*agexact;
1.126 brouard 3109: }
1.187 brouard 3110:
1.145 brouard 3111: /* savm=pmij(pmmij,cov,ncovmodel,x,nlstate); */
1.126 brouard 3112: out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath,
3113: 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate));
1.145 brouard 3114: /* out=matprod2(newm,oldm,1,nlstate+ndeath,1,nlstate+ndeath, */
3115: /* 1,nlstate+ndeath,pmij(pmmij,cov,ncovmodel,x,nlstate)); */
1.126 brouard 3116: savm=oldm;
3117: oldm=newm;
3118: } /* end mult */
3119:
3120: s1=s[mw[mi][i]][i];
3121: s2=s[mw[mi+1][i]][i];
1.217 brouard 3122: /* if(s2==-1){ */
3123: /* printf(" s1=%d, s2=%d i=%d \n", s1, s2, i); */
3124: /* /\* exit(1); *\/ */
3125: /* } */
1.126 brouard 3126: bbh=(double)bh[mi][i]/(double)stepm;
3127: /* bias is positive if real duration
3128: * is higher than the multiple of stepm and negative otherwise.
3129: */
3130: if( s2 > nlstate && (mle <5) ){ /* Jackson */
3131: lli=log(out[s1][s2] - savm[s1][s2]);
1.216 brouard 3132: } else if ( s2==-1 ) { /* alive */
1.126 brouard 3133: for (j=1,survp=0. ; j<=nlstate; j++)
3134: survp += (1.+bbh)*out[s1][j]- bbh*savm[s1][j];
3135: lli= log(survp);
3136: }else if (mle==1){
3137: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3138: } else if(mle==2){
3139: 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 */
3140: } else if(mle==3){ /* exponential inter-extrapolation */
3141: 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 */
3142: } else if (mle==4){ /* mle=4 no inter-extrapolation */
3143: lli=log(out[s1][s2]); /* Original formula */
1.136 brouard 3144: } else{ /* mle=0 back to 1 */
3145: lli= log((1.+bbh)*out[s1][s2]- bbh*savm[s1][s2]); /* linear interpolation */
3146: /*lli=log(out[s1][s2]); */ /* Original formula */
1.126 brouard 3147: } /* End of if */
3148: ipmx +=1;
3149: sw += weight[i];
3150: ll[s[mw[mi][i]][i]] += 2*weight[i]*lli;
1.132 brouard 3151: /*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 3152: if(globpr){
1.214 brouard 3153: fprintf(ficresilk,"%9ld %6.1f %6.1f %6d %2d %2d %2d %2d %3d %11.6f %8.4f %8.3f\
1.126 brouard 3154: %11.6f %11.6f %11.6f ", \
1.214 brouard 3155: num[i], agebegin, ageend, i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],weight[i]*gipmx/gsw,
1.126 brouard 3156: 2*weight[i]*lli,out[s1][s2],savm[s1][s2]);
3157: for(k=1,llt=0.,l=0.; k<=nlstate; k++){
3158: llt +=ll[k]*gipmx/gsw;
3159: fprintf(ficresilk," %10.6f",-ll[k]*gipmx/gsw);
3160: }
3161: fprintf(ficresilk," %10.6f\n", -llt);
3162: }
3163: } /* end of wave */
3164: } /* end of individual */
3165: for(k=1,l=0.; k<=nlstate; k++) l += ll[k];
3166: /* printf("l1=%f l2=%f ",ll[1],ll[2]); */
3167: l= l*ipmx/sw; /* To get the same order of magnitude as if weight=1 for every body */
3168: if(globpr==0){ /* First time we count the contributions and weights */
3169: gipmx=ipmx;
3170: gsw=sw;
3171: }
3172: return -l;
3173: }
3174:
3175:
3176: /*************** function likelione ***********/
3177: void likelione(FILE *ficres,double p[], int npar, int nlstate, int *globpri, long *ipmx, double *sw, double *fretone, double (*funcone)(double []))
3178: {
3179: /* This routine should help understanding what is done with
3180: the selection of individuals/waves and
3181: to check the exact contribution to the likelihood.
3182: Plotting could be done.
3183: */
3184: int k;
3185:
3186: if(*globpri !=0){ /* Just counts and sums, no printings */
1.201 brouard 3187: strcpy(fileresilk,"ILK_");
1.202 brouard 3188: strcat(fileresilk,fileresu);
1.126 brouard 3189: if((ficresilk=fopen(fileresilk,"w"))==NULL) {
3190: printf("Problem with resultfile: %s\n", fileresilk);
3191: fprintf(ficlog,"Problem with resultfile: %s\n", fileresilk);
3192: }
1.214 brouard 3193: 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");
3194: fprintf(ficresilk, "#num_i ageb agend i s1 s2 mi mw dh likeli weight %%weight 2wlli out sav ");
1.126 brouard 3195: /* i,s1,s2,mi,mw[mi][i],dh[mi][i],exp(lli),weight[i],2*weight[i]*lli,out[s1][s2],savm[s1][s2]); */
3196: for(k=1; k<=nlstate; k++)
3197: fprintf(ficresilk," -2*gipw/gsw*weight*ll[%d]++",k);
3198: fprintf(ficresilk," -2*gipw/gsw*weight*ll(total)\n");
3199: }
3200:
3201: *fretone=(*funcone)(p);
3202: if(*globpri !=0){
3203: fclose(ficresilk);
1.205 brouard 3204: if (mle ==0)
3205: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with initial parameters and mle = %d.",mle);
3206: else if(mle >=1)
3207: fprintf(fichtm,"\n<br>File of contributions to the likelihood computed with optimized parameters mle = %d.",mle);
3208: 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 3209:
1.208 brouard 3210:
3211: for (k=1; k<= nlstate ; k++) {
1.211 brouard 3212: 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 3213: <img src=\"%s-p%dj.png\">",k,k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k,subdirf2(optionfilefiname,"ILK_"),k);
3214: }
1.207 brouard 3215: 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 3216: <img src=\"%s-ori.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3217: fprintf(fichtm,"<br>- and by state of destination <a href=\"%s-dest.png\">%s-dest.png</a><br> \
1.204 brouard 3218: <img src=\"%s-dest.png\">",subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"),subdirf2(optionfilefiname,"ILK_"));
1.207 brouard 3219: fflush(fichtm);
1.205 brouard 3220: }
1.126 brouard 3221: return;
3222: }
3223:
3224:
3225: /*********** Maximum Likelihood Estimation ***************/
3226:
3227: void mlikeli(FILE *ficres,double p[], int npar, int ncovmodel, int nlstate, double ftol, double (*func)(double []))
3228: {
1.165 brouard 3229: int i,j, iter=0;
1.126 brouard 3230: double **xi;
3231: double fret;
3232: double fretone; /* Only one call to likelihood */
3233: /* char filerespow[FILENAMELENGTH];*/
1.162 brouard 3234:
3235: #ifdef NLOPT
3236: int creturn;
3237: nlopt_opt opt;
3238: /* double lb[9] = { -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL, -HUGE_VAL }; /\* lower bounds *\/ */
3239: double *lb;
3240: double minf; /* the minimum objective value, upon return */
3241: double * p1; /* Shifted parameters from 0 instead of 1 */
3242: myfunc_data dinst, *d = &dinst;
3243: #endif
3244:
3245:
1.126 brouard 3246: xi=matrix(1,npar,1,npar);
3247: for (i=1;i<=npar;i++)
3248: for (j=1;j<=npar;j++)
3249: xi[i][j]=(i==j ? 1.0 : 0.0);
3250: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.201 brouard 3251: strcpy(filerespow,"POW_");
1.126 brouard 3252: strcat(filerespow,fileres);
3253: if((ficrespow=fopen(filerespow,"w"))==NULL) {
3254: printf("Problem with resultfile: %s\n", filerespow);
3255: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
3256: }
3257: fprintf(ficrespow,"# Powell\n# iter -2*LL");
3258: for (i=1;i<=nlstate;i++)
3259: for(j=1;j<=nlstate+ndeath;j++)
3260: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
3261: fprintf(ficrespow,"\n");
1.162 brouard 3262: #ifdef POWELL
1.126 brouard 3263: powell(p,xi,npar,ftol,&iter,&fret,func);
1.162 brouard 3264: #endif
1.126 brouard 3265:
1.162 brouard 3266: #ifdef NLOPT
3267: #ifdef NEWUOA
3268: opt = nlopt_create(NLOPT_LN_NEWUOA,npar);
3269: #else
3270: opt = nlopt_create(NLOPT_LN_BOBYQA,npar);
3271: #endif
3272: lb=vector(0,npar-1);
3273: for (i=0;i<npar;i++) lb[i]= -HUGE_VAL;
3274: nlopt_set_lower_bounds(opt, lb);
3275: nlopt_set_initial_step1(opt, 0.1);
3276:
3277: p1= (p+1); /* p *(p+1)@8 and p *(p1)@8 are equal p1[0]=p[1] */
3278: d->function = func;
3279: printf(" Func %.12lf \n",myfunc(npar,p1,NULL,d));
3280: nlopt_set_min_objective(opt, myfunc, d);
3281: nlopt_set_xtol_rel(opt, ftol);
3282: if ((creturn=nlopt_optimize(opt, p1, &minf)) < 0) {
3283: printf("nlopt failed! %d\n",creturn);
3284: }
3285: else {
3286: printf("found minimum after %d evaluations (NLOPT=%d)\n", countcallfunc ,NLOPT);
3287: printf("found minimum at f(%g,%g) = %0.10g\n", p[0], p[1], minf);
3288: iter=1; /* not equal */
3289: }
3290: nlopt_destroy(opt);
3291: #endif
1.126 brouard 3292: free_matrix(xi,1,npar,1,npar);
3293: fclose(ficrespow);
1.203 brouard 3294: printf("\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
3295: fprintf(ficlog,"\n#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.180 brouard 3296: fprintf(ficres,"#Number of iterations & function calls = %d & %d, -2 Log likelihood = %.12f\n",iter, countcallfunc,func(p));
1.126 brouard 3297:
3298: }
3299:
3300: /**** Computes Hessian and covariance matrix ***/
1.203 brouard 3301: void hesscov(double **matcov, double **hess, double p[], int npar, double delti[], double ftolhess, double (*func)(double []))
1.126 brouard 3302: {
3303: double **a,**y,*x,pd;
1.203 brouard 3304: /* double **hess; */
1.164 brouard 3305: int i, j;
1.126 brouard 3306: int *indx;
3307:
3308: double hessii(double p[], double delta, int theta, double delti[],double (*func)(double []),int npar);
1.203 brouard 3309: double hessij(double p[], double **hess, double delti[], int i, int j,double (*func)(double []),int npar);
1.126 brouard 3310: void lubksb(double **a, int npar, int *indx, double b[]) ;
3311: void ludcmp(double **a, int npar, int *indx, double *d) ;
3312: double gompertz(double p[]);
1.203 brouard 3313: /* hess=matrix(1,npar,1,npar); */
1.126 brouard 3314:
3315: printf("\nCalculation of the hessian matrix. Wait...\n");
3316: fprintf(ficlog,"\nCalculation of the hessian matrix. Wait...\n");
3317: for (i=1;i<=npar;i++){
1.203 brouard 3318: printf("%d-",i);fflush(stdout);
3319: fprintf(ficlog,"%d-",i);fflush(ficlog);
1.126 brouard 3320:
3321: hess[i][i]=hessii(p,ftolhess,i,delti,func,npar);
3322:
3323: /* printf(" %f ",p[i]);
3324: printf(" %lf %lf %lf",hess[i][i],ftolhess,delti[i]);*/
3325: }
3326:
3327: for (i=1;i<=npar;i++) {
3328: for (j=1;j<=npar;j++) {
3329: if (j>i) {
1.203 brouard 3330: printf(".%d-%d",i,j);fflush(stdout);
3331: fprintf(ficlog,".%d-%d",i,j);fflush(ficlog);
3332: hess[i][j]=hessij(p,hess, delti,i,j,func,npar);
1.126 brouard 3333:
3334: hess[j][i]=hess[i][j];
3335: /*printf(" %lf ",hess[i][j]);*/
3336: }
3337: }
3338: }
3339: printf("\n");
3340: fprintf(ficlog,"\n");
3341:
3342: printf("\nInverting the hessian to get the covariance matrix. Wait...\n");
3343: fprintf(ficlog,"\nInverting the hessian to get the covariance matrix. Wait...\n");
3344:
3345: a=matrix(1,npar,1,npar);
3346: y=matrix(1,npar,1,npar);
3347: x=vector(1,npar);
3348: indx=ivector(1,npar);
3349: for (i=1;i<=npar;i++)
3350: for (j=1;j<=npar;j++) a[i][j]=hess[i][j];
3351: ludcmp(a,npar,indx,&pd);
3352:
3353: for (j=1;j<=npar;j++) {
3354: for (i=1;i<=npar;i++) x[i]=0;
3355: x[j]=1;
3356: lubksb(a,npar,indx,x);
3357: for (i=1;i<=npar;i++){
3358: matcov[i][j]=x[i];
3359: }
3360: }
3361:
3362: printf("\n#Hessian matrix#\n");
3363: fprintf(ficlog,"\n#Hessian matrix#\n");
3364: for (i=1;i<=npar;i++) {
3365: for (j=1;j<=npar;j++) {
1.203 brouard 3366: printf("%.6e ",hess[i][j]);
3367: fprintf(ficlog,"%.6e ",hess[i][j]);
1.126 brouard 3368: }
3369: printf("\n");
3370: fprintf(ficlog,"\n");
3371: }
3372:
1.203 brouard 3373: /* printf("\n#Covariance matrix#\n"); */
3374: /* fprintf(ficlog,"\n#Covariance matrix#\n"); */
3375: /* for (i=1;i<=npar;i++) { */
3376: /* for (j=1;j<=npar;j++) { */
3377: /* printf("%.6e ",matcov[i][j]); */
3378: /* fprintf(ficlog,"%.6e ",matcov[i][j]); */
3379: /* } */
3380: /* printf("\n"); */
3381: /* fprintf(ficlog,"\n"); */
3382: /* } */
3383:
1.126 brouard 3384: /* Recompute Inverse */
1.203 brouard 3385: /* for (i=1;i<=npar;i++) */
3386: /* for (j=1;j<=npar;j++) a[i][j]=matcov[i][j]; */
3387: /* ludcmp(a,npar,indx,&pd); */
3388:
3389: /* printf("\n#Hessian matrix recomputed#\n"); */
3390:
3391: /* for (j=1;j<=npar;j++) { */
3392: /* for (i=1;i<=npar;i++) x[i]=0; */
3393: /* x[j]=1; */
3394: /* lubksb(a,npar,indx,x); */
3395: /* for (i=1;i<=npar;i++){ */
3396: /* y[i][j]=x[i]; */
3397: /* printf("%.3e ",y[i][j]); */
3398: /* fprintf(ficlog,"%.3e ",y[i][j]); */
3399: /* } */
3400: /* printf("\n"); */
3401: /* fprintf(ficlog,"\n"); */
3402: /* } */
3403:
3404: /* Verifying the inverse matrix */
3405: #ifdef DEBUGHESS
3406: y=matprod2(y,hess,1,npar,1,npar,1,npar,matcov);
1.126 brouard 3407:
1.203 brouard 3408: printf("\n#Verification: multiplying the matrix of covariance by the Hessian matrix, should be unity:#\n");
3409: fprintf(ficlog,"\n#Verification: multiplying the matrix of covariance by the Hessian matrix. Should be unity:#\n");
1.126 brouard 3410:
3411: for (j=1;j<=npar;j++) {
3412: for (i=1;i<=npar;i++){
1.203 brouard 3413: printf("%.2f ",y[i][j]);
3414: fprintf(ficlog,"%.2f ",y[i][j]);
1.126 brouard 3415: }
3416: printf("\n");
3417: fprintf(ficlog,"\n");
3418: }
1.203 brouard 3419: #endif
1.126 brouard 3420:
3421: free_matrix(a,1,npar,1,npar);
3422: free_matrix(y,1,npar,1,npar);
3423: free_vector(x,1,npar);
3424: free_ivector(indx,1,npar);
1.203 brouard 3425: /* free_matrix(hess,1,npar,1,npar); */
1.126 brouard 3426:
3427:
3428: }
3429:
3430: /*************** hessian matrix ****************/
3431: double hessii(double x[], double delta, int theta, double delti[], double (*func)(double []), int npar)
1.203 brouard 3432: { /* Around values of x, computes the function func and returns the scales delti and hessian */
1.126 brouard 3433: int i;
3434: int l=1, lmax=20;
1.203 brouard 3435: double k1,k2, res, fx;
1.132 brouard 3436: double p2[MAXPARM+1]; /* identical to x */
1.126 brouard 3437: double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4;
3438: int k=0,kmax=10;
3439: double l1;
3440:
3441: fx=func(x);
3442: for (i=1;i<=npar;i++) p2[i]=x[i];
1.145 brouard 3443: for(l=0 ; l <=lmax; l++){ /* Enlarging the zone around the Maximum */
1.126 brouard 3444: l1=pow(10,l);
3445: delts=delt;
3446: for(k=1 ; k <kmax; k=k+1){
3447: delt = delta*(l1*k);
3448: p2[theta]=x[theta] +delt;
1.145 brouard 3449: k1=func(p2)-fx; /* Might be negative if too close to the theoretical maximum */
1.126 brouard 3450: p2[theta]=x[theta]-delt;
3451: k2=func(p2)-fx;
3452: /*res= (k1-2.0*fx+k2)/delt/delt; */
1.203 brouard 3453: res= (k1+k2)/delt/delt/2.; /* Divided by 2 because L and not 2*L */
1.126 brouard 3454:
1.203 brouard 3455: #ifdef DEBUGHESSII
1.126 brouard 3456: 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);
3457: 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);
3458: #endif
3459: /*if(fabs(k1-2.0*fx+k2) <1.e-13){ */
3460: if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)){
3461: k=kmax;
3462: }
3463: else if((k1 >khi/nkhif) || (k2 >khi/nkhif)){ /* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. */
1.164 brouard 3464: k=kmax; l=lmax*10;
1.126 brouard 3465: }
3466: else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){
3467: delts=delt;
3468: }
1.203 brouard 3469: } /* End loop k */
1.126 brouard 3470: }
3471: delti[theta]=delts;
3472: return res;
3473:
3474: }
3475:
1.203 brouard 3476: double hessij( double x[], double **hess, double delti[], int thetai,int thetaj,double (*func)(double []),int npar)
1.126 brouard 3477: {
3478: int i;
1.164 brouard 3479: int l=1, lmax=20;
1.126 brouard 3480: double k1,k2,k3,k4,res,fx;
1.132 brouard 3481: double p2[MAXPARM+1];
1.203 brouard 3482: int k, kmax=1;
3483: double v1, v2, cv12, lc1, lc2;
1.208 brouard 3484:
3485: int firstime=0;
1.203 brouard 3486:
1.126 brouard 3487: fx=func(x);
1.203 brouard 3488: for (k=1; k<=kmax; k=k+10) {
1.126 brouard 3489: for (i=1;i<=npar;i++) p2[i]=x[i];
1.203 brouard 3490: p2[thetai]=x[thetai]+delti[thetai]*k;
3491: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3492: k1=func(p2)-fx;
3493:
1.203 brouard 3494: p2[thetai]=x[thetai]+delti[thetai]*k;
3495: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3496: k2=func(p2)-fx;
3497:
1.203 brouard 3498: p2[thetai]=x[thetai]-delti[thetai]*k;
3499: p2[thetaj]=x[thetaj]+delti[thetaj]*k;
1.126 brouard 3500: k3=func(p2)-fx;
3501:
1.203 brouard 3502: p2[thetai]=x[thetai]-delti[thetai]*k;
3503: p2[thetaj]=x[thetaj]-delti[thetaj]*k;
1.126 brouard 3504: k4=func(p2)-fx;
1.203 brouard 3505: res=(k1-k2-k3+k4)/4.0/delti[thetai]/k/delti[thetaj]/k/2.; /* Because of L not 2*L */
3506: if(k1*k2*k3*k4 <0.){
1.208 brouard 3507: firstime=1;
1.203 brouard 3508: kmax=kmax+10;
1.208 brouard 3509: }
3510: if(kmax >=10 || firstime ==1){
1.218 ! brouard 3511: 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);
! 3512: 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 3513: 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);
3514: 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);
3515: }
3516: #ifdef DEBUGHESSIJ
3517: v1=hess[thetai][thetai];
3518: v2=hess[thetaj][thetaj];
3519: cv12=res;
3520: /* Computing eigen value of Hessian matrix */
3521: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3522: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
3523: if ((lc2 <0) || (lc1 <0) ){
3524: printf("Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3525: fprintf(ficlog, "Warning: sub Hessian matrix '%d%d' does not have positive eigen values \n",thetai,thetaj);
3526: 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);
3527: 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);
3528: }
1.126 brouard 3529: #endif
3530: }
3531: return res;
3532: }
3533:
1.203 brouard 3534: /* Not done yet: Was supposed to fix if not exactly at the maximum */
3535: /* double hessij( double x[], double delti[], int thetai,int thetaj,double (*func)(double []),int npar) */
3536: /* { */
3537: /* int i; */
3538: /* int l=1, lmax=20; */
3539: /* double k1,k2,k3,k4,res,fx; */
3540: /* double p2[MAXPARM+1]; */
3541: /* double delt=0.0001, delts, nkhi=10.,nkhif=1., khi=1.e-4; */
3542: /* int k=0,kmax=10; */
3543: /* double l1; */
3544:
3545: /* fx=func(x); */
3546: /* for(l=0 ; l <=lmax; l++){ /\* Enlarging the zone around the Maximum *\/ */
3547: /* l1=pow(10,l); */
3548: /* delts=delt; */
3549: /* for(k=1 ; k <kmax; k=k+1){ */
3550: /* delt = delti*(l1*k); */
3551: /* for (i=1;i<=npar;i++) p2[i]=x[i]; */
3552: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3553: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3554: /* k1=func(p2)-fx; */
3555:
3556: /* p2[thetai]=x[thetai]+delti[thetai]/k; */
3557: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3558: /* k2=func(p2)-fx; */
3559:
3560: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3561: /* p2[thetaj]=x[thetaj]+delti[thetaj]/k; */
3562: /* k3=func(p2)-fx; */
3563:
3564: /* p2[thetai]=x[thetai]-delti[thetai]/k; */
3565: /* p2[thetaj]=x[thetaj]-delti[thetaj]/k; */
3566: /* k4=func(p2)-fx; */
3567: /* res=(k1-k2-k3+k4)/4.0/delti[thetai]*k/delti[thetaj]*k/2.; /\* Because of L not 2*L *\/ */
3568: /* #ifdef DEBUGHESSIJ */
3569: /* 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); */
3570: /* 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); */
3571: /* #endif */
3572: /* if((k1 <khi/nkhi/2.) || (k2 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)|| (k4 <khi/nkhi/2.)){ */
3573: /* k=kmax; */
3574: /* } */
3575: /* else if((k1 >khi/nkhif) || (k2 >khi/nkhif) || (k4 >khi/nkhif) || (k4 >khi/nkhif)){ /\* Keeps lastvalue before 3.84/2 KHI2 5% 1d.f. *\/ */
3576: /* k=kmax; l=lmax*10; */
3577: /* } */
3578: /* else if((k1 >khi/nkhi) || (k2 >khi/nkhi)){ */
3579: /* delts=delt; */
3580: /* } */
3581: /* } /\* End loop k *\/ */
3582: /* } */
3583: /* delti[theta]=delts; */
3584: /* return res; */
3585: /* } */
3586:
3587:
1.126 brouard 3588: /************** Inverse of matrix **************/
3589: void ludcmp(double **a, int n, int *indx, double *d)
3590: {
3591: int i,imax,j,k;
3592: double big,dum,sum,temp;
3593: double *vv;
3594:
3595: vv=vector(1,n);
3596: *d=1.0;
3597: for (i=1;i<=n;i++) {
3598: big=0.0;
3599: for (j=1;j<=n;j++)
3600: if ((temp=fabs(a[i][j])) > big) big=temp;
3601: if (big == 0.0) nrerror("Singular matrix in routine ludcmp");
3602: vv[i]=1.0/big;
3603: }
3604: for (j=1;j<=n;j++) {
3605: for (i=1;i<j;i++) {
3606: sum=a[i][j];
3607: for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
3608: a[i][j]=sum;
3609: }
3610: big=0.0;
3611: for (i=j;i<=n;i++) {
3612: sum=a[i][j];
3613: for (k=1;k<j;k++)
3614: sum -= a[i][k]*a[k][j];
3615: a[i][j]=sum;
3616: if ( (dum=vv[i]*fabs(sum)) >= big) {
3617: big=dum;
3618: imax=i;
3619: }
3620: }
3621: if (j != imax) {
3622: for (k=1;k<=n;k++) {
3623: dum=a[imax][k];
3624: a[imax][k]=a[j][k];
3625: a[j][k]=dum;
3626: }
3627: *d = -(*d);
3628: vv[imax]=vv[j];
3629: }
3630: indx[j]=imax;
3631: if (a[j][j] == 0.0) a[j][j]=TINY;
3632: if (j != n) {
3633: dum=1.0/(a[j][j]);
3634: for (i=j+1;i<=n;i++) a[i][j] *= dum;
3635: }
3636: }
3637: free_vector(vv,1,n); /* Doesn't work */
3638: ;
3639: }
3640:
3641: void lubksb(double **a, int n, int *indx, double b[])
3642: {
3643: int i,ii=0,ip,j;
3644: double sum;
3645:
3646: for (i=1;i<=n;i++) {
3647: ip=indx[i];
3648: sum=b[ip];
3649: b[ip]=b[i];
3650: if (ii)
3651: for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
3652: else if (sum) ii=i;
3653: b[i]=sum;
3654: }
3655: for (i=n;i>=1;i--) {
3656: sum=b[i];
3657: for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
3658: b[i]=sum/a[i][i];
3659: }
3660: }
3661:
3662: void pstamp(FILE *fichier)
3663: {
1.196 brouard 3664: fprintf(fichier,"# %s.%s\n#IMaCh version %s, %s\n#%s\n# %s", optionfilefiname,optionfilext,version,copyright, fullversion, strstart);
1.126 brouard 3665: }
3666:
3667: /************ Frequencies ********************/
1.214 brouard 3668: void freqsummary(char fileres[], int iagemin, int iagemax, int **s, double **agev, int nlstate, int imx, \
3669: int *Tvaraff, int **nbcode, int *ncodemax,double **mint,double **anint, char strstart[],\
3670: int firstpass, int lastpass, int stepm, int weightopt, char model[])
1.126 brouard 3671: { /* Some frequencies */
3672:
1.164 brouard 3673: int i, m, jk, j1, bool, z1,j;
1.214 brouard 3674: int mi; /* Effective wave */
1.126 brouard 3675: int first;
3676: double ***freq; /* Frequencies */
3677: double *pp, **prop;
3678: double pos,posprop, k2, dateintsum=0,k2cpt=0;
1.214 brouard 3679: char fileresp[FILENAMELENGTH], fileresphtm[FILENAMELENGTH], fileresphtmfr[FILENAMELENGTH];
3680: double agebegin, ageend;
3681:
1.126 brouard 3682: pp=vector(1,nlstate);
1.218 ! brouard 3683: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
! 3684: /* prop=matrix(1,nlstate,iagemin,iagemax+3); */
1.201 brouard 3685: strcpy(fileresp,"P_");
3686: strcat(fileresp,fileresu);
1.213 brouard 3687: /*strcat(fileresphtm,fileresu);*/
1.126 brouard 3688: if((ficresp=fopen(fileresp,"w"))==NULL) {
3689: printf("Problem with prevalence resultfile: %s\n", fileresp);
3690: fprintf(ficlog,"Problem with prevalence resultfile: %s\n", fileresp);
3691: exit(0);
3692: }
1.214 brouard 3693:
1.213 brouard 3694: strcpy(fileresphtm,subdirfext(optionfilefiname,"PHTM_",".htm"));
3695: if((ficresphtm=fopen(fileresphtm,"w"))==NULL) {
3696: printf("Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
3697: fprintf(ficlog,"Problem with prevalence HTM resultfile '%s' with errno='%s'\n",fileresphtm,strerror(errno));
3698: fflush(ficlog);
3699: exit(70);
3700: }
1.214 brouard 3701: else{
3702: fprintf(ficresphtm,"<html><head>\n<title>IMaCh PHTM_ %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
3703: <hr size=\"2\" color=\"#EC5E5E\"> \n\
3704: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
3705: fileresphtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
3706: }
3707: 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);
3708:
3709: strcpy(fileresphtmfr,subdirfext(optionfilefiname,"PHTMFR_",".htm"));
3710: if((ficresphtmfr=fopen(fileresphtmfr,"w"))==NULL) {
3711: printf("Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
3712: fprintf(ficlog,"Problem with frequency table HTM resultfile '%s' with errno='%s'\n",fileresphtmfr,strerror(errno));
3713: fflush(ficlog);
3714: exit(70);
3715: }
3716: else{
3717: fprintf(ficresphtmfr,"<html><head>\n<title>IMaCh PHTM_Frequency table %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
3718: <hr size=\"2\" color=\"#EC5E5E\"> \n\
3719: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
3720: fileresphtmfr,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
3721: }
3722: 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);
3723:
1.218 ! brouard 3724: freq= ma3x(-5,nlstate+ndeath,-5,nlstate+ndeath,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
1.126 brouard 3725: j1=0;
3726:
3727: j=cptcoveff;
3728: if (cptcovn<1) {j=1;ncodemax[1]=1;}
3729:
3730: first=1;
3731:
1.214 brouard 3732: for (j1 = 1; j1 <= (int) pow(2,cptcoveff); j1++){ /* Loop on covariates combination */
1.126 brouard 3733: /*printf("cptcoveff=%d Tvaraff=%d", cptcoveff,Tvaraff[1]);
3734: scanf("%d", i);*/
3735: for (i=-5; i<=nlstate+ndeath; i++)
3736: for (jk=-5; jk<=nlstate+ndeath; jk++)
3737: for(m=iagemin; m <= iagemax+3; m++)
3738: freq[i][jk][m]=0;
1.143 brouard 3739:
3740: for (i=1; i<=nlstate; i++)
3741: for(m=iagemin; m <= iagemax+3; m++)
3742: prop[i][m]=0;
1.126 brouard 3743:
3744: dateintsum=0;
3745: k2cpt=0;
1.214 brouard 3746: for (i=1; i<=imx; i++) { /* For each individual i */
1.126 brouard 3747: bool=1;
1.210 brouard 3748: if (cptcovn>0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.144 brouard 3749: for (z1=1; z1<=cptcoveff; z1++)
1.198 brouard 3750: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)]){
1.145 brouard 3751: /* Tests if the value of each of the covariates of i is equal to filter j1 */
1.144 brouard 3752: bool=0;
1.198 brouard 3753: /* 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",
3754: bool,i,z1, z1, Tvaraff[z1],i,covar[Tvaraff[z1]][i],j1,z1,codtabm(j1,z1),
3755: j1,z1,nbcode[Tvaraff[z1]][codtabm(j1,z1)],j1);*/
3756: /* For j1=7 in V1+V2+V3+V4 = 0 1 1 0 and codtabm(7,3)=1 and nbcde[3][?]=1*/
1.144 brouard 3757: }
1.210 brouard 3758: } /* cptcovn > 0 */
1.214 brouard 3759:
1.126 brouard 3760: if (bool==1){
1.214 brouard 3761: /* for(m=firstpass; m<=lastpass; m++){ */
3762: for(mi=1; mi<wav[i];mi++){
3763: m=mw[mi][i];
3764: /* dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective (mi) waves m=mw[mi][i]
3765: and mw[mi+1][i]. dh depends on stepm. */
3766: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
3767: ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /* Age at end of wave and transition */
3768: if(m >=firstpass && m <=lastpass){
3769: k2=anint[m][i]+(mint[m][i]/12.);
3770: /*if ((k2>=dateprev1) && (k2<=dateprev2)) {*/
3771: if(agev[m][i]==0) agev[m][i]=iagemax+1; /* All ages equal to 0 are in iagemax+1 */
3772: if(agev[m][i]==1) agev[m][i]=iagemax+2; /* All ages equal to 1 are in iagemax+2 */
3773: if (s[m][i]>0 && s[m][i]<=nlstate) /* If status at wave m is known and a live state */
3774: prop[s[m][i]][(int)agev[m][i]] += weight[i]; /* At age of beginning of transition, where status is known */
1.126 brouard 3775: if (m<lastpass) {
1.214 brouard 3776: /* if(s[m][i]==4 && s[m+1][i]==4) */
3777: /* printf(" num=%ld m=%d, i=%d s1=%d s2=%d agev at m=%d\n", num[i], m, i,s[m][i],s[m+1][i], (int)agev[m][i]); */
3778: if(s[m][i]==-1)
3779: printf(" num=%ld m=%d, i=%d s1=%d s2=%d agev at m=%d agebegin=%.2f ageend=%.2f, agemed=%d\n", num[i], m, i,s[m][i],s[m+1][i], (int)agev[m][i],agebegin, ageend, (int)((agebegin+ageend)/2.));
3780: freq[s[m][i]][s[m+1][i]][(int)agev[m][i]] += weight[i]; /* At age of beginning of transition, where status is known */
3781: /* freq[s[m][i]][s[m+1][i]][(int)((agebegin+ageend)/2.)] += weight[i]; */
3782: freq[s[m][i]][s[m+1][i]][iagemax+3] += weight[i]; /* Total is in iagemax+3 *//* At age of beginning of transition, where status is known */
1.126 brouard 3783: }
1.214 brouard 3784: }
3785: if ((agev[m][i]>1) && (agev[m][i]< (iagemax+3)) && (anint[m][i]!=9999) && (mint[m][i]!=99)) {
3786: dateintsum=dateintsum+k2;
3787: k2cpt++;
3788: /* printf("i=%ld dateintmean = %lf dateintsum=%lf k2cpt=%lf k2=%lf\n",i, dateintsum/k2cpt, dateintsum,k2cpt, k2); */
3789: }
3790: /*}*/
1.210 brouard 3791: } /* end m */
3792: } /* end bool */
3793: } /* end i = 1 to imx */
1.126 brouard 3794:
3795: /* fprintf(ficresp, "#Count between %.lf/%.lf/%.lf and %.lf/%.lf/%.lf\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2);*/
3796: pstamp(ficresp);
3797: if (cptcovn>0) {
3798: fprintf(ficresp, "\n#********** Variable ");
1.214 brouard 3799: fprintf(ficresphtm, "\n<br/><br/><h3>********** Variable ");
3800: fprintf(ficresphtmfr, "\n<br/><br/><h3>********** Variable ");
1.213 brouard 3801: for (z1=1; z1<=cptcoveff; z1++){
3802: fprintf(ficresp, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
3803: fprintf(ficresphtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.214 brouard 3804: fprintf(ficresphtmfr, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.213 brouard 3805: }
3806: fprintf(ficresp, "**********\n#");
1.214 brouard 3807: fprintf(ficresphtm, "**********</h3>\n");
3808: fprintf(ficresphtmfr, "**********</h3>\n");
1.143 brouard 3809: fprintf(ficlog, "\n#********** Variable ");
1.198 brouard 3810: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficlog, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.214 brouard 3811: fprintf(ficlog, "**********\n");
1.126 brouard 3812: }
1.214 brouard 3813: fprintf(ficresphtm,"<table style=\"text-align:center; border: 1px solid\">");
1.213 brouard 3814: for(i=1; i<=nlstate;i++) {
1.126 brouard 3815: fprintf(ficresp, " Age Prev(%d) N(%d) N",i,i);
1.213 brouard 3816: fprintf(ficresphtm, "<th>Age</th><th>Prev(%d)</th><th>N(%d)</th><th>N</th>",i,i);
3817: }
1.126 brouard 3818: fprintf(ficresp, "\n");
1.213 brouard 3819: fprintf(ficresphtm, "\n");
1.126 brouard 3820:
1.214 brouard 3821: /* Header of frequency table by age */
3822: fprintf(ficresphtmfr,"<table style=\"text-align:center; border: 1px solid\">");
3823: fprintf(ficresphtmfr,"<th>Age</th> ");
3824: for(jk=-1; jk <=nlstate+ndeath; jk++){
3825: for(m=-1; m <=nlstate+ndeath; m++){
3826: if(jk!=0 && m!=0)
3827: fprintf(ficresphtmfr,"<th>%d%d</th> ",jk,m);
3828: }
3829: }
3830: fprintf(ficresphtmfr, "\n");
3831:
3832: /* For each age */
1.126 brouard 3833: for(i=iagemin; i <= iagemax+3; i++){
1.213 brouard 3834: fprintf(ficresphtm,"<tr>");
1.214 brouard 3835: if(i==iagemax+1){
3836: fprintf(ficlog,"1");
3837: fprintf(ficresphtmfr,"<tr><th>0</th> ");
3838: }else if(i==iagemax+2){
3839: fprintf(ficlog,"0");
3840: fprintf(ficresphtmfr,"<tr><th>Unknown</th> ");
3841: }else if(i==iagemax+3){
1.126 brouard 3842: fprintf(ficlog,"Total");
1.214 brouard 3843: fprintf(ficresphtmfr,"<tr><th>Total</th> ");
1.126 brouard 3844: }else{
3845: if(first==1){
3846: first=0;
3847: printf("See log file for details...\n");
3848: }
1.214 brouard 3849: fprintf(ficresphtmfr,"<tr><th>%d</th> ",i);
1.126 brouard 3850: fprintf(ficlog,"Age %d", i);
3851: }
3852: for(jk=1; jk <=nlstate ; jk++){
3853: for(m=-1, pp[jk]=0; m <=nlstate+ndeath ; m++)
3854: pp[jk] += freq[jk][m][i];
3855: }
3856: for(jk=1; jk <=nlstate ; jk++){
3857: for(m=-1, pos=0; m <=0 ; m++)
3858: pos += freq[jk][m][i];
3859: if(pp[jk]>=1.e-10){
3860: if(first==1){
1.132 brouard 3861: printf(" %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
1.126 brouard 3862: }
3863: fprintf(ficlog," %d.=%.0f loss[%d]=%.1f%%",jk,pp[jk],jk,100*pos/pp[jk]);
3864: }else{
3865: if(first==1)
3866: printf(" %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
3867: fprintf(ficlog," %d.=%.0f loss[%d]=NaNQ%%",jk,pp[jk],jk);
3868: }
3869: }
3870:
3871: for(jk=1; jk <=nlstate ; jk++){
3872: for(m=0, pp[jk]=0; m <=nlstate+ndeath; m++)
3873: pp[jk] += freq[jk][m][i];
3874: }
3875: for(jk=1,pos=0,posprop=0; jk <=nlstate ; jk++){
3876: pos += pp[jk];
3877: posprop += prop[jk][i];
3878: }
3879: for(jk=1; jk <=nlstate ; jk++){
3880: if(pos>=1.e-5){
3881: if(first==1)
3882: printf(" %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
3883: fprintf(ficlog," %d.=%.0f prev[%d]=%.1f%%",jk,pp[jk],jk,100*pp[jk]/pos);
3884: }else{
3885: if(first==1)
3886: printf(" %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
3887: fprintf(ficlog," %d.=%.0f prev[%d]=NaNQ%%",jk,pp[jk],jk);
3888: }
3889: if( i <= iagemax){
3890: if(pos>=1.e-5){
3891: fprintf(ficresp," %d %.5f %.0f %.0f",i,prop[jk][i]/posprop, prop[jk][i],posprop);
1.213 brouard 3892: fprintf(ficresphtm,"<th>%d</th><td>%.5f</td><td>%.0f</td><td>%.0f</td>",i,prop[jk][i]/posprop, prop[jk][i],posprop);
1.126 brouard 3893: /*probs[i][jk][j1]= pp[jk]/pos;*/
3894: /*printf("\ni=%d jk=%d j1=%d %.5f %.0f %.0f %f",i,jk,j1,pp[jk]/pos, pp[jk],pos,probs[i][jk][j1]);*/
3895: }
1.213 brouard 3896: else{
1.126 brouard 3897: fprintf(ficresp," %d NaNq %.0f %.0f",i,prop[jk][i],posprop);
1.213 brouard 3898: fprintf(ficresphtm,"<th>%d</th><td>NaNq</td><td>%.0f</td><td>%.0f</td>",i, prop[jk][i],posprop);
3899: }
1.126 brouard 3900: }
3901: }
3902:
1.214 brouard 3903: for(jk=-1; jk <=nlstate+ndeath; jk++){
3904: for(m=-1; m <=nlstate+ndeath; m++){
3905: if(freq[jk][m][i] !=0 ) { /* minimizing output */
3906: if(first==1){
3907: printf(" %d%d=%.0f",jk,m,freq[jk][m][i]);
3908: }
1.126 brouard 3909: fprintf(ficlog," %d%d=%.0f",jk,m,freq[jk][m][i]);
3910: }
1.214 brouard 3911: if(jk!=0 && m!=0)
3912: fprintf(ficresphtmfr,"<td>%.0f</td> ",freq[jk][m][i]);
3913: }
3914: }
3915: fprintf(ficresphtmfr,"</tr>\n ");
1.213 brouard 3916: if(i <= iagemax){
1.126 brouard 3917: fprintf(ficresp,"\n");
1.213 brouard 3918: fprintf(ficresphtm,"</tr>\n");
3919: }
1.126 brouard 3920: if(first==1)
3921: printf("Others in log...\n");
3922: fprintf(ficlog,"\n");
1.210 brouard 3923: } /* end loop i */
1.213 brouard 3924: fprintf(ficresphtm,"</table>\n");
1.214 brouard 3925: fprintf(ficresphtmfr,"</table>\n");
1.145 brouard 3926: /*}*/
1.210 brouard 3927: } /* end j1 */
1.126 brouard 3928: dateintmean=dateintsum/k2cpt;
3929:
3930: fclose(ficresp);
1.213 brouard 3931: fclose(ficresphtm);
1.214 brouard 3932: fclose(ficresphtmfr);
1.218 ! brouard 3933: free_ma3x(freq,-5,nlstate+ndeath,-5,nlstate+ndeath, iagemin-AGEMARGE, iagemax+3+AGEMARGE);
1.126 brouard 3934: free_vector(pp,1,nlstate);
1.218 ! brouard 3935: free_matrix(prop,1,nlstate,iagemin-AGEMARGE, iagemax+3+AGEMARGE);
1.126 brouard 3936: /* End of Freq */
3937: }
3938:
3939: /************ Prevalence ********************/
3940: 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)
3941: {
3942: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
3943: in each health status at the date of interview (if between dateprev1 and dateprev2).
3944: We still use firstpass and lastpass as another selection.
3945: */
3946:
1.164 brouard 3947: int i, m, jk, j1, bool, z1,j;
1.214 brouard 3948: int mi; /* Effective wave */
3949: int iage;
3950: double agebegin, ageend;
1.164 brouard 3951:
3952: double **prop;
3953: double posprop;
1.126 brouard 3954: double y2; /* in fractional years */
3955: int iagemin, iagemax;
1.145 brouard 3956: int first; /** to stop verbosity which is redirected to log file */
1.126 brouard 3957:
3958: iagemin= (int) agemin;
3959: iagemax= (int) agemax;
3960: /*pp=vector(1,nlstate);*/
1.218 ! brouard 3961: prop=matrix(1,nlstate,iagemin-AGEMARGE,iagemax+3+AGEMARGE);
1.126 brouard 3962: /* freq=ma3x(-1,nlstate+ndeath,-1,nlstate+ndeath,iagemin,iagemax+3);*/
3963: j1=0;
3964:
1.145 brouard 3965: /*j=cptcoveff;*/
1.126 brouard 3966: if (cptcovn<1) {j=1;ncodemax[1]=1;}
3967:
1.145 brouard 3968: first=1;
3969: for(j1=1; j1<= (int) pow(2,cptcoveff);j1++){
1.214 brouard 3970: for (i=1; i<=nlstate; i++)
1.218 ! brouard 3971: for(iage=iagemin-AGEMARGE; iage <= iagemax+3+AGEMARGE; iage++)
! 3972: prop[i][iage]=0.0;
1.214 brouard 3973:
3974: for (i=1; i<=imx; i++) { /* Each individual */
3975: bool=1;
3976: if (cptcovn>0) { /* Filter is here: Must be looked at for model=V1+V2+V3+V4 */
1.218 ! brouard 3977: for (z1=1; z1<=cptcoveff; z1++)
! 3978: if (covar[Tvaraff[z1]][i]!= nbcode[Tvaraff[z1]][codtabm(j1,z1)])
! 3979: bool=0;
1.214 brouard 3980: }
3981: if (bool==1) {
1.218 ! brouard 3982: /* for(m=firstpass; m<=lastpass; m++){/\* Other selection (we can limit to certain interviews*\/ */
! 3983: for(mi=1; mi<wav[i];mi++){
! 3984: m=mw[mi][i];
! 3985: agebegin=agev[m][i]; /* Age at beginning of wave before transition*/
! 3986: /* ageend=agev[m][i]+(dh[m][i])*stepm/YEARM; /\* Age at end of wave and transition *\/ */
! 3987: if(m >=firstpass && m <=lastpass){
! 3988: y2=anint[m][i]+(mint[m][i]/12.); /* Fractional date in year */
! 3989: if ((y2>=dateprev1) && (y2<=dateprev2)) { /* Here is the main selection (fractional years) */
! 3990: if(agev[m][i]==0) agev[m][i]=iagemax+1;
! 3991: if(agev[m][i]==1) agev[m][i]=iagemax+2;
! 3992: if((int)agev[m][i] <iagemin-AGEMARGE || (int)agev[m][i] >iagemax+3+AGEMARGE){
! 3993: 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);
! 3994: exit(1);
! 3995: }
! 3996: if (s[m][i]>0 && s[m][i]<=nlstate) {
! 3997: /*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]]);*/
! 3998: prop[s[m][i]][(int)agev[m][i]] += weight[i];/* At age of beginning of transition, where status is known */
! 3999: prop[s[m][i]][iagemax+3] += weight[i];
! 4000: } /* end valid statuses */
! 4001: } /* end selection of dates */
! 4002: } /* end selection of waves */
! 4003: } /* end effective waves */
1.214 brouard 4004: } /* end bool */
4005: }
4006: for(i=iagemin; i <= iagemax+3; i++){
4007: for(jk=1,posprop=0; jk <=nlstate ; jk++) {
1.218 ! brouard 4008: posprop += prop[jk][i];
1.214 brouard 4009: }
4010:
4011: for(jk=1; jk <=nlstate ; jk++){
1.218 ! brouard 4012: if( i <= iagemax){
! 4013: if(posprop>=1.e-5){
! 4014: probs[i][jk][j1]= prop[jk][i]/posprop;
! 4015: } else{
! 4016: if(first==1){
! 4017: first=0;
! 4018: 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]);
! 4019: }
! 4020: }
! 4021: }
1.214 brouard 4022: }/* end jk */
4023: }/* end i */
1.145 brouard 4024: /*} *//* end i1 */
4025: } /* end j1 */
1.126 brouard 4026:
4027: /* free_ma3x(freq,-1,nlstate+ndeath,-1,nlstate+ndeath, iagemin, iagemax+3);*/
4028: /*free_vector(pp,1,nlstate);*/
1.218 ! brouard 4029: free_matrix(prop,1,nlstate, iagemin-AGEMARGE,iagemax+3+AGEMARGE);
1.126 brouard 4030: } /* End of prevalence */
4031:
4032: /************* Waves Concatenation ***************/
4033:
4034: 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)
4035: {
4036: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
4037: Death is a valid wave (if date is known).
4038: mw[mi][i] is the mi (mi=1 to wav[i]) effective wave of individual i
4039: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
4040: and mw[mi+1][i]. dh depends on stepm.
4041: */
4042:
4043: int i, mi, m;
4044: /* int j, k=0,jk, ju, jl,jmin=1e+5, jmax=-1;
4045: double sum=0., jmean=0.;*/
1.218 ! brouard 4046: int first, firstwo, firsthree, firstfour;
1.126 brouard 4047: int j, k=0,jk, ju, jl;
4048: double sum=0.;
4049: first=0;
1.214 brouard 4050: firstwo=0;
1.217 brouard 4051: firsthree=0;
1.218 ! brouard 4052: firstfour=0;
1.164 brouard 4053: jmin=100000;
1.126 brouard 4054: jmax=-1;
4055: jmean=0.;
1.214 brouard 4056: for(i=1; i<=imx; i++){ /* For simple cases and if state is death */
1.126 brouard 4057: mi=0;
4058: m=firstpass;
1.214 brouard 4059: while(s[m][i] <= nlstate){ /* a live state */
1.216 brouard 4060: 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.126 brouard 4061: mw[++mi][i]=m;
1.216 brouard 4062: }
4063: if(m >=lastpass){
4064: if(s[m][i]==-1 && (int) andc[i] == 9999 && (int)anint[m][i] != 9999){
1.217 brouard 4065: if(firsthree == 0){
4066: printf("Information! Unknown health 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.\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);
4067: firsthree=1;
4068: }
1.218 ! brouard 4069: 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.\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.216 brouard 4070: mw[++mi][i]=m;
4071: }
4072: if(s[m][i]==-2){ /* Vital status is really unknown */
4073: nbwarn++;
4074: if((int)anint[m][i] == 9999){ /* Has the vital status really been verified? */
4075: 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);
4076: 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.\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);
4077: }
4078: break;
4079: }
1.126 brouard 4080: break;
1.216 brouard 4081: }
1.126 brouard 4082: else
4083: m++;
4084: }/* end while */
1.216 brouard 4085:
4086: /* After last pass */
1.214 brouard 4087: if (s[m][i] > nlstate){ /* In a death state */
1.126 brouard 4088: mi++; /* Death is another wave */
4089: /* if(mi==0) never been interviewed correctly before death */
4090: /* Only death is a correct wave */
4091: mw[mi][i]=m;
1.216 brouard 4092: }else if ((int) andc[i] != 9999) { /* Status is either death or negative. A death occured after lastpass, we can't take it into account because of potential bias */
4093: /* m++; */
4094: /* mi++; */
4095: /* s[m][i]=nlstate+1; /\* We are setting the status to the last of non live state *\/ */
4096: /* mw[mi][i]=m; */
4097: nberr++;
1.218 ! brouard 4098: if ((int)anint[m][i]!= 9999) { /* date of last interview is known */
! 4099: if(firstwo==0){
! 4100: printf("Error! Death for individual %ld line=%d occurred %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 );
! 4101: firstwo=1;
! 4102: }
! 4103: fprintf(ficlog,"Error! Death for individual %ld line=%d occurred %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 );
! 4104: }else{ /* end date of interview is known */
! 4105: /* death is known but not confirmed by death status at any wave */
! 4106: if(firstfour==0){
! 4107: 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 );
! 4108: firstfour=1;
! 4109: }
! 4110: 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 4111: }
1.126 brouard 4112: }
4113: wav[i]=mi;
4114: if(mi==0){
4115: nbwarn++;
4116: if(first==0){
4117: printf("Warning! No valid information for individual %ld line=%d (skipped) and may be others, see log file\n",num[i],i);
4118: first=1;
4119: }
4120: if(first==1){
4121: fprintf(ficlog,"Warning! No valid information for individual %ld line=%d (skipped)\n",num[i],i);
4122: }
4123: } /* end mi==0 */
4124: } /* End individuals */
1.214 brouard 4125: /* wav and mw are no more changed */
1.126 brouard 4126:
1.214 brouard 4127:
1.126 brouard 4128: for(i=1; i<=imx; i++){
4129: for(mi=1; mi<wav[i];mi++){
4130: if (stepm <=0)
4131: dh[mi][i]=1;
4132: else{
4133: if (s[mw[mi+1][i]][i] > nlstate) { /* A death */
4134: if (agedc[i] < 2*AGESUP) {
4135: j= rint(agedc[i]*12-agev[mw[mi][i]][i]*12);
4136: if(j==0) j=1; /* Survives at least one month after exam */
4137: else if(j<0){
4138: nberr++;
4139: 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]);
4140: j=1; /* Temporary Dangerous patch */
4141: 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);
4142: 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]);
4143: 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);
4144: }
4145: k=k+1;
4146: if (j >= jmax){
4147: jmax=j;
4148: ijmax=i;
4149: }
4150: if (j <= jmin){
4151: jmin=j;
4152: ijmin=i;
4153: }
4154: sum=sum+j;
4155: /*if (j<0) printf("j=%d num=%d \n",j,i);*/
4156: /* printf("%d %d %d %d\n", s[mw[mi][i]][i] ,s[mw[mi+1][i]][i],j,i);*/
4157: }
4158: }
4159: else{
4160: j= rint( (agev[mw[mi+1][i]][i]*12 - agev[mw[mi][i]][i]*12));
4161: /* 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]); */
4162:
4163: k=k+1;
4164: if (j >= jmax) {
4165: jmax=j;
4166: ijmax=i;
4167: }
4168: else if (j <= jmin){
4169: jmin=j;
4170: ijmin=i;
4171: }
4172: /* if (j<10) printf("j=%d jmin=%d num=%d ",j,jmin,i); */
4173: /*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]);*/
4174: if(j<0){
4175: nberr++;
4176: 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]);
4177: 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]);
4178: }
4179: sum=sum+j;
4180: }
4181: jk= j/stepm;
4182: jl= j -jk*stepm;
4183: ju= j -(jk+1)*stepm;
4184: if(mle <=1){ /* only if we use a the linear-interpoloation pseudo-likelihood */
4185: if(jl==0){
4186: dh[mi][i]=jk;
4187: bh[mi][i]=0;
4188: }else{ /* We want a negative bias in order to only have interpolation ie
1.136 brouard 4189: * to avoid the price of an extra matrix product in likelihood */
1.126 brouard 4190: dh[mi][i]=jk+1;
4191: bh[mi][i]=ju;
4192: }
4193: }else{
4194: if(jl <= -ju){
4195: dh[mi][i]=jk;
4196: bh[mi][i]=jl; /* bias is positive if real duration
4197: * is higher than the multiple of stepm and negative otherwise.
4198: */
4199: }
4200: else{
4201: dh[mi][i]=jk+1;
4202: bh[mi][i]=ju;
4203: }
4204: if(dh[mi][i]==0){
4205: dh[mi][i]=1; /* At least one step */
4206: bh[mi][i]=ju; /* At least one step */
4207: /* 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);*/
4208: }
4209: } /* end if mle */
4210: }
4211: } /* end wave */
4212: }
4213: jmean=sum/k;
4214: 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 4215: 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 4216: }
4217:
4218: /*********** Tricode ****************************/
1.145 brouard 4219: void tricode(int *Tvar, int **nbcode, int imx, int *Ndum)
1.126 brouard 4220: {
1.144 brouard 4221: /**< Uses cptcovn+2*cptcovprod as the number of covariates */
4222: /* 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 4223: * Boring subroutine which should only output nbcode[Tvar[j]][k]
1.145 brouard 4224: * Tvar[5] in V2+V1+V3*age+V2*V4 is 2 (V2)
1.169 brouard 4225: * nbcode[Tvar[j]][1]=
1.144 brouard 4226: */
1.130 brouard 4227:
1.145 brouard 4228: int ij=1, k=0, j=0, i=0, maxncov=NCOVMAX;
1.136 brouard 4229: int modmaxcovj=0; /* Modality max of covariates j */
1.145 brouard 4230: int cptcode=0; /* Modality max of covariates j */
4231: int modmincovj=0; /* Modality min of covariates j */
4232:
4233:
1.126 brouard 4234: cptcoveff=0;
4235:
1.144 brouard 4236: for (k=1; k <= maxncov; k++) ncodemax[k]=0; /* Horrible constant again replaced by NCOVMAX */
1.126 brouard 4237:
1.145 brouard 4238: /* Loop on covariates without age and products */
1.186 brouard 4239: for (j=1; j<=(cptcovs); j++) { /* From model V1 + V2*age+ V3 + V3*V4 keeps V1 + V3 = 2 only */
1.192 brouard 4240: for (k=-1; k < maxncov; k++) Ndum[k]=0;
1.186 brouard 4241: for (i=1; i<=imx; i++) { /* Loop on individuals: reads the data file to get the maximum value of the
1.136 brouard 4242: modality of this covariate Vj*/
1.145 brouard 4243: ij=(int)(covar[Tvar[j]][i]); /* ij=0 or 1 or -1. Value of the covariate Tvar[j] for individual i
4244: * If product of Vn*Vm, still boolean *:
4245: * If it was coded 1, 2, 3, 4 should be splitted into 3 boolean variables
4246: * 1 => 0 0 0, 2 => 0 0 1, 3 => 0 1 1, 4=1 0 0 */
4247: /* Finds for covariate j, n=Tvar[j] of Vn . ij is the
1.136 brouard 4248: modality of the nth covariate of individual i. */
1.145 brouard 4249: if (ij > modmaxcovj)
4250: modmaxcovj=ij;
4251: else if (ij < modmincovj)
4252: modmincovj=ij;
4253: if ((ij < -1) && (ij > NCOVMAX)){
4254: printf( "Error: minimal is less than -1 or maximal is bigger than %d. Exiting. \n", NCOVMAX );
4255: exit(1);
4256: }else
1.136 brouard 4257: Ndum[ij]++; /*counts and stores the occurence of this modality 0, 1, -1*/
1.145 brouard 4258: /* If coded 1, 2, 3 , counts the number of 1 Ndum[1], number of 2, Ndum[2], etc */
1.126 brouard 4259: /*printf("i=%d ij=%d Ndum[ij]=%d imx=%d",i,ij,Ndum[ij],imx);*/
1.136 brouard 4260: /* getting the maximum value of the modality of the covariate
4261: (should be 0 or 1 now) Tvar[j]. If V=sex and male is coded 0 and
4262: female is 1, then modmaxcovj=1.*/
1.192 brouard 4263: } /* end for loop on individuals i */
1.145 brouard 4264: printf(" Minimal and maximal values of %d th covariate V%d: min=%d max=%d \n", j, Tvar[j], modmincovj, modmaxcovj);
1.192 brouard 4265: 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 4266: cptcode=modmaxcovj;
1.137 brouard 4267: /* Ndum[0] = frequency of 0 for model-covariate j, Ndum[1] frequency of 1 etc. */
1.145 brouard 4268: /*for (i=0; i<=cptcode; i++) {*/
1.192 brouard 4269: for (k=modmincovj; k<=modmaxcovj; k++) { /* k=-1 ? 0 and 1*//* For each value k of the modality of model-cov j */
4270: printf("Frequencies of covariates %d ie V%d with value %d: %d\n", j, Tvar[j], k, Ndum[k]);
4271: fprintf(ficlog, "Frequencies of covariates %d ie V%d with value %d: %d\n", j, Tvar[j], k, Ndum[k]);
4272: if( Ndum[k] != 0 ){ /* Counts if nobody answered modality k ie empty modality, we skip it and reorder */
4273: if( k != -1){
4274: ncodemax[j]++; /* ncodemax[j]= Number of modalities of the j th
4275: covariate for which somebody answered excluding
4276: undefined. Usually 2: 0 and 1. */
4277: }
4278: ncodemaxwundef[j]++; /* ncodemax[j]= Number of modalities of the j th
4279: covariate for which somebody answered including
4280: undefined. Usually 3: -1, 0 and 1. */
1.145 brouard 4281: }
4282: /* In fact ncodemax[j]=2 (dichotom. variables only) but it could be more for
4283: historical reasons: 3 if coded 1, 2, 3 and 4 and Ndum[2]=0 */
1.131 brouard 4284: } /* Ndum[-1] number of undefined modalities */
1.126 brouard 4285:
1.136 brouard 4286: /* j is a covariate, n=Tvar[j] of Vn; Fills nbcode */
1.186 brouard 4287: /* For covariate j, modalities could be 1, 2, 3, 4, 5, 6, 7.
4288: 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 4289: modmincovj=3; modmaxcovj = 7;
1.186 brouard 4290: There are only 3 modalities non empty 3, 6, 7 (or 2 if 27 is too few) : ncodemax[j]=3;
4291: which will be coded 0, 1, 2 which in binary on 2=3-1 digits are 0=00 1=01, 2=10;
4292: defining two dummy variables: variables V1_1 and V1_2.
1.145 brouard 4293: nbcode[Tvar[j]][ij]=k;
4294: nbcode[Tvar[j]][1]=0;
4295: nbcode[Tvar[j]][2]=1;
4296: nbcode[Tvar[j]][3]=2;
1.197 brouard 4297: To be continued (not working yet).
1.145 brouard 4298: */
1.197 brouard 4299: ij=0; /* ij is similar to i but can jump over null modalities */
4300: 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*/
4301: if (Ndum[i] == 0) { /* If nobody responded to this modality k */
1.192 brouard 4302: break;
4303: }
4304: ij++;
1.197 brouard 4305: 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.*/
1.192 brouard 4306: cptcode = ij; /* New max modality for covar j */
4307: } /* end of loop on modality i=-1 to 1 or more */
4308:
4309: /* for (k=0; k<= cptcode; k++) { /\* k=-1 ? k=0 to 1 *\//\* Could be 1 to 4 *\//\* cptcode=modmaxcovj *\/ */
4310: /* /\*recode from 0 *\/ */
4311: /* k is a modality. If we have model=V1+V1*sex */
4312: /* then: nbcode[1][1]=0 ; nbcode[1][2]=1; nbcode[2][1]=0 ; nbcode[2][2]=1; */
4313: /* But if some modality were not used, it is recoded from 0 to a newer modmaxcovj=cptcode *\/ */
4314: /* } */
4315: /* /\* cptcode = ij; *\/ /\* New max modality for covar j *\/ */
4316: /* if (ij > ncodemax[j]) { */
4317: /* printf( " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4318: /* fprintf(ficlog, " Error ij=%d > ncodemax[%d]=%d\n", ij, j, ncodemax[j]); */
4319: /* break; */
4320: /* } */
4321: /* } /\* end of loop on modality k *\/ */
1.137 brouard 4322: } /* end of loop on model-covariate j. nbcode[Tvarj][1]=0 and nbcode[Tvarj][2]=1 sets the value of covariate j*/
4323:
1.145 brouard 4324: for (k=-1; k< maxncov; k++) Ndum[k]=0;
1.137 brouard 4325:
1.187 brouard 4326: for (i=1; i<=ncovmodel-2-nagesqr; i++) { /* -2, cste and age and eventually age*age */
1.145 brouard 4327: /* Listing of all covariables in statement model to see if some covariates appear twice. For example, V1 appears twice in V1+V1*V2.*/
4328: ij=Tvar[i]; /* Tvar might be -1 if status was unknown */
1.187 brouard 4329: Ndum[ij]++; /* Might be supersed V1 + V1*age */
1.145 brouard 4330: }
1.126 brouard 4331:
1.192 brouard 4332: ij=0;
1.145 brouard 4333: for (i=0; i<= maxncov-1; i++) { /* modmaxcovj is unknown here. Only Ndum[2(V2),3(age*V3), 5(V3*V2) 6(V1*V4) */
4334: /*printf("Ndum[%d]=%d\n",i, Ndum[i]);*/
1.126 brouard 4335: if((Ndum[i]!=0) && (i<=ncovcol)){
1.192 brouard 4336: ij++;
1.145 brouard 4337: /*printf("diff Ndum[%d]=%d\n",i, Ndum[i]);*/
4338: Tvaraff[ij]=i; /*For printing (unclear) */
1.192 brouard 4339: }else{
4340: /* Tvaraff[ij]=0; */
4341: }
1.126 brouard 4342: }
1.192 brouard 4343: /* ij--; */
1.144 brouard 4344: cptcoveff=ij; /*Number of total covariates*/
1.145 brouard 4345:
1.126 brouard 4346: }
4347:
1.145 brouard 4348:
1.126 brouard 4349: /*********** Health Expectancies ****************/
4350:
1.127 brouard 4351: 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 4352:
4353: {
4354: /* Health expectancies, no variances */
1.164 brouard 4355: int i, j, nhstepm, hstepm, h, nstepm;
1.126 brouard 4356: int nhstepma, nstepma; /* Decreasing with age */
4357: double age, agelim, hf;
4358: double ***p3mat;
4359: double eip;
4360:
4361: pstamp(ficreseij);
4362: fprintf(ficreseij,"# (a) Life expectancies by health status at initial age and (b) health expectancies by health status at initial age\n");
4363: fprintf(ficreseij,"# Age");
4364: for(i=1; i<=nlstate;i++){
4365: for(j=1; j<=nlstate;j++){
4366: fprintf(ficreseij," e%1d%1d ",i,j);
4367: }
4368: fprintf(ficreseij," e%1d. ",i);
4369: }
4370: fprintf(ficreseij,"\n");
4371:
4372:
4373: if(estepm < stepm){
4374: printf ("Problem %d lower than %d\n",estepm, stepm);
4375: }
4376: else hstepm=estepm;
4377: /* We compute the life expectancy from trapezoids spaced every estepm months
4378: * This is mainly to measure the difference between two models: for example
4379: * if stepm=24 months pijx are given only every 2 years and by summing them
4380: * we are calculating an estimate of the Life Expectancy assuming a linear
4381: * progression in between and thus overestimating or underestimating according
4382: * to the curvature of the survival function. If, for the same date, we
4383: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4384: * to compare the new estimate of Life expectancy with the same linear
4385: * hypothesis. A more precise result, taking into account a more precise
4386: * curvature will be obtained if estepm is as small as stepm. */
4387:
4388: /* For example we decided to compute the life expectancy with the smallest unit */
4389: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4390: nhstepm is the number of hstepm from age to agelim
4391: nstepm is the number of stepm from age to agelin.
4392: Look at hpijx to understand the reason of that which relies in memory size
4393: and note for a fixed period like estepm months */
4394: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4395: survival function given by stepm (the optimization length). Unfortunately it
4396: means that if the survival funtion is printed only each two years of age and if
4397: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4398: results. So we changed our mind and took the option of the best precision.
4399: */
4400: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4401:
4402: agelim=AGESUP;
4403: /* If stepm=6 months */
4404: /* Computed by stepm unit matrices, product of hstepm matrices, stored
4405: in an array of nhstepm length: nhstepm=10, hstepm=4, stepm=6 months */
4406:
4407: /* nhstepm age range expressed in number of stepm */
4408: nstepm=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4409: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4410: /* if (stepm >= YEARM) hstepm=1;*/
4411: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4412: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4413:
4414: for (age=bage; age<=fage; age ++){
4415: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4416: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4417: /* if (stepm >= YEARM) hstepm=1;*/
4418: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
4419:
4420: /* If stepm=6 months */
4421: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4422: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4423:
4424: hpxij(p3mat,nhstepma,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4425:
4426: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
4427:
4428: printf("%d|",(int)age);fflush(stdout);
4429: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
4430:
4431: /* Computing expectancies */
4432: for(i=1; i<=nlstate;i++)
4433: for(j=1; j<=nlstate;j++)
4434: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
4435: eij[i][j][(int)age] += (p3mat[i][j][h]+p3mat[i][j][h+1])/2.0*hf;
4436:
4437: /* 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]);*/
4438:
4439: }
4440:
4441: fprintf(ficreseij,"%3.0f",age );
4442: for(i=1; i<=nlstate;i++){
4443: eip=0;
4444: for(j=1; j<=nlstate;j++){
4445: eip +=eij[i][j][(int)age];
4446: fprintf(ficreseij,"%9.4f", eij[i][j][(int)age] );
4447: }
4448: fprintf(ficreseij,"%9.4f", eip );
4449: }
4450: fprintf(ficreseij,"\n");
4451:
4452: }
4453: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4454: printf("\n");
4455: fprintf(ficlog,"\n");
4456:
4457: }
4458:
1.127 brouard 4459: 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 4460:
4461: {
4462: /* Covariances of health expectancies eij and of total life expectancies according
4463: to initial status i, ei. .
4464: */
4465: int i, j, nhstepm, hstepm, h, nstepm, k, cptj, cptj2, i2, j2, ij, ji;
4466: int nhstepma, nstepma; /* Decreasing with age */
4467: double age, agelim, hf;
4468: double ***p3matp, ***p3matm, ***varhe;
4469: double **dnewm,**doldm;
4470: double *xp, *xm;
4471: double **gp, **gm;
4472: double ***gradg, ***trgradg;
4473: int theta;
4474:
4475: double eip, vip;
4476:
4477: varhe=ma3x(1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int) fage);
4478: xp=vector(1,npar);
4479: xm=vector(1,npar);
4480: dnewm=matrix(1,nlstate*nlstate,1,npar);
4481: doldm=matrix(1,nlstate*nlstate,1,nlstate*nlstate);
4482:
4483: pstamp(ficresstdeij);
4484: fprintf(ficresstdeij,"# Health expectancies with standard errors\n");
4485: fprintf(ficresstdeij,"# Age");
4486: for(i=1; i<=nlstate;i++){
4487: for(j=1; j<=nlstate;j++)
4488: fprintf(ficresstdeij," e%1d%1d (SE)",i,j);
4489: fprintf(ficresstdeij," e%1d. ",i);
4490: }
4491: fprintf(ficresstdeij,"\n");
4492:
4493: pstamp(ficrescveij);
4494: fprintf(ficrescveij,"# Subdiagonal matrix of covariances of health expectancies by age: cov(eij,ekl)\n");
4495: fprintf(ficrescveij,"# Age");
4496: for(i=1; i<=nlstate;i++)
4497: for(j=1; j<=nlstate;j++){
4498: cptj= (j-1)*nlstate+i;
4499: for(i2=1; i2<=nlstate;i2++)
4500: for(j2=1; j2<=nlstate;j2++){
4501: cptj2= (j2-1)*nlstate+i2;
4502: if(cptj2 <= cptj)
4503: fprintf(ficrescveij," %1d%1d,%1d%1d",i,j,i2,j2);
4504: }
4505: }
4506: fprintf(ficrescveij,"\n");
4507:
4508: if(estepm < stepm){
4509: printf ("Problem %d lower than %d\n",estepm, stepm);
4510: }
4511: else hstepm=estepm;
4512: /* We compute the life expectancy from trapezoids spaced every estepm months
4513: * This is mainly to measure the difference between two models: for example
4514: * if stepm=24 months pijx are given only every 2 years and by summing them
4515: * we are calculating an estimate of the Life Expectancy assuming a linear
4516: * progression in between and thus overestimating or underestimating according
4517: * to the curvature of the survival function. If, for the same date, we
4518: * estimate the model with stepm=1 month, we can keep estepm to 24 months
4519: * to compare the new estimate of Life expectancy with the same linear
4520: * hypothesis. A more precise result, taking into account a more precise
4521: * curvature will be obtained if estepm is as small as stepm. */
4522:
4523: /* For example we decided to compute the life expectancy with the smallest unit */
4524: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
4525: nhstepm is the number of hstepm from age to agelim
4526: nstepm is the number of stepm from age to agelin.
4527: Look at hpijx to understand the reason of that which relies in memory size
4528: and note for a fixed period like estepm months */
4529: /* We decided (b) to get a life expectancy respecting the most precise curvature of the
4530: survival function given by stepm (the optimization length). Unfortunately it
4531: means that if the survival funtion is printed only each two years of age and if
4532: you sum them up and add 1 year (area under the trapezoids) you won't get the same
4533: results. So we changed our mind and took the option of the best precision.
4534: */
4535: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
4536:
4537: /* If stepm=6 months */
4538: /* nhstepm age range expressed in number of stepm */
4539: agelim=AGESUP;
4540: nstepm=(int) rint((agelim-bage)*YEARM/stepm);
4541: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4542: /* if (stepm >= YEARM) hstepm=1;*/
4543: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
4544:
4545: p3matp=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4546: p3matm=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4547: gradg=ma3x(0,nhstepm,1,npar,1,nlstate*nlstate);
4548: trgradg =ma3x(0,nhstepm,1,nlstate*nlstate,1,npar);
4549: gp=matrix(0,nhstepm,1,nlstate*nlstate);
4550: gm=matrix(0,nhstepm,1,nlstate*nlstate);
4551:
4552: for (age=bage; age<=fage; age ++){
4553: nstepma=(int) rint((agelim-bage)*YEARM/stepm); /* Biggest nstepm */
4554: /* Typically if 20 years nstepm = 20*12/6=40 stepm */
4555: /* if (stepm >= YEARM) hstepm=1;*/
4556: nhstepma = nstepma/hstepm;/* Expressed in hstepm, typically nhstepma=40/4=10 */
1.218 ! brouard 4557:
1.126 brouard 4558: /* If stepm=6 months */
4559: /* Computed by stepm unit matrices, product of hstepma matrices, stored
4560: in an array of nhstepma length: nhstepma=10, hstepm=4, stepm=6 months */
4561:
4562: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
1.218 ! brouard 4563:
1.126 brouard 4564: /* Computing Variances of health expectancies */
4565: /* Gradient is computed with plus gp and minus gm. Code is duplicated in order to
4566: decrease memory allocation */
4567: for(theta=1; theta <=npar; theta++){
4568: for(i=1; i<=npar; i++){
1.218 ! brouard 4569: xp[i] = x[i] + (i==theta ?delti[theta]:0);
! 4570: xm[i] = x[i] - (i==theta ?delti[theta]:0);
1.126 brouard 4571: }
4572: hpxij(p3matp,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, cij);
4573: hpxij(p3matm,nhstepm,age,hstepm,xm,nlstate,stepm,oldm,savm, cij);
1.218 ! brouard 4574:
1.126 brouard 4575: for(j=1; j<= nlstate; j++){
1.218 ! brouard 4576: for(i=1; i<=nlstate; i++){
! 4577: for(h=0; h<=nhstepm-1; h++){
! 4578: gp[h][(j-1)*nlstate + i] = (p3matp[i][j][h]+p3matp[i][j][h+1])/2.;
! 4579: gm[h][(j-1)*nlstate + i] = (p3matm[i][j][h]+p3matm[i][j][h+1])/2.;
! 4580: }
! 4581: }
1.126 brouard 4582: }
1.218 ! brouard 4583:
1.126 brouard 4584: for(ij=1; ij<= nlstate*nlstate; ij++)
1.218 ! brouard 4585: for(h=0; h<=nhstepm-1; h++){
! 4586: gradg[h][theta][ij]= (gp[h][ij]-gm[h][ij])/2./delti[theta];
! 4587: }
1.126 brouard 4588: }/* End theta */
4589:
4590:
4591: for(h=0; h<=nhstepm-1; h++)
4592: for(j=1; j<=nlstate*nlstate;j++)
1.218 ! brouard 4593: for(theta=1; theta <=npar; theta++)
! 4594: trgradg[h][j][theta]=gradg[h][theta][j];
1.126 brouard 4595:
1.218 ! brouard 4596:
! 4597: for(ij=1;ij<=nlstate*nlstate;ij++)
1.126 brouard 4598: for(ji=1;ji<=nlstate*nlstate;ji++)
1.218 ! brouard 4599: varhe[ij][ji][(int)age] =0.;
! 4600:
! 4601: printf("%d|",(int)age);fflush(stdout);
! 4602: fprintf(ficlog,"%d|",(int)age);fflush(ficlog);
! 4603: for(h=0;h<=nhstepm-1;h++){
1.126 brouard 4604: for(k=0;k<=nhstepm-1;k++){
1.218 ! brouard 4605: matprod2(dnewm,trgradg[h],1,nlstate*nlstate,1,npar,1,npar,matcov);
! 4606: matprod2(doldm,dnewm,1,nlstate*nlstate,1,npar,1,nlstate*nlstate,gradg[k]);
! 4607: for(ij=1;ij<=nlstate*nlstate;ij++)
! 4608: for(ji=1;ji<=nlstate*nlstate;ji++)
! 4609: varhe[ij][ji][(int)age] += doldm[ij][ji]*hf*hf;
1.126 brouard 4610: }
4611: }
1.218 ! brouard 4612:
1.126 brouard 4613: /* Computing expectancies */
4614: hpxij(p3matm,nhstepm,age,hstepm,x,nlstate,stepm,oldm, savm, cij);
4615: for(i=1; i<=nlstate;i++)
4616: for(j=1; j<=nlstate;j++)
1.218 ! brouard 4617: for (h=0, eij[i][j][(int)age]=0; h<=nhstepm-1; h++){
! 4618: eij[i][j][(int)age] += (p3matm[i][j][h]+p3matm[i][j][h+1])/2.0*hf;
! 4619:
! 4620: /* 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]);*/
! 4621:
! 4622: }
! 4623:
1.126 brouard 4624: fprintf(ficresstdeij,"%3.0f",age );
4625: for(i=1; i<=nlstate;i++){
4626: eip=0.;
4627: vip=0.;
4628: for(j=1; j<=nlstate;j++){
1.218 ! brouard 4629: eip += eij[i][j][(int)age];
! 4630: for(k=1; k<=nlstate;k++) /* Sum on j and k of cov(eij,eik) */
! 4631: vip += varhe[(j-1)*nlstate+i][(k-1)*nlstate+i][(int)age];
! 4632: 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 4633: }
4634: fprintf(ficresstdeij," %9.4f (%.4f)", eip, sqrt(vip));
4635: }
4636: fprintf(ficresstdeij,"\n");
1.218 ! brouard 4637:
1.126 brouard 4638: fprintf(ficrescveij,"%3.0f",age );
4639: for(i=1; i<=nlstate;i++)
4640: for(j=1; j<=nlstate;j++){
1.218 ! brouard 4641: cptj= (j-1)*nlstate+i;
! 4642: for(i2=1; i2<=nlstate;i2++)
! 4643: for(j2=1; j2<=nlstate;j2++){
! 4644: cptj2= (j2-1)*nlstate+i2;
! 4645: if(cptj2 <= cptj)
! 4646: fprintf(ficrescveij," %.4f", varhe[cptj][cptj2][(int)age]);
! 4647: }
1.126 brouard 4648: }
4649: fprintf(ficrescveij,"\n");
1.218 ! brouard 4650:
1.126 brouard 4651: }
4652: free_matrix(gm,0,nhstepm,1,nlstate*nlstate);
4653: free_matrix(gp,0,nhstepm,1,nlstate*nlstate);
4654: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate*nlstate);
4655: free_ma3x(trgradg,0,nhstepm,1,nlstate*nlstate,1,npar);
4656: free_ma3x(p3matm,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4657: free_ma3x(p3matp,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
4658: printf("\n");
4659: fprintf(ficlog,"\n");
1.218 ! brouard 4660:
1.126 brouard 4661: free_vector(xm,1,npar);
4662: free_vector(xp,1,npar);
4663: free_matrix(dnewm,1,nlstate*nlstate,1,npar);
4664: free_matrix(doldm,1,nlstate*nlstate,1,nlstate*nlstate);
4665: free_ma3x(varhe,1,nlstate*nlstate,1,nlstate*nlstate,(int) bage, (int)fage);
4666: }
1.218 ! brouard 4667:
1.126 brouard 4668: /************ Variance ******************/
1.209 brouard 4669: 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 4670: {
! 4671: /* Variance of health expectancies */
! 4672: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double ** savm,double ftolpl);*/
! 4673: /* double **newm;*/
! 4674: /* int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav)*/
! 4675:
! 4676: /* int movingaverage(); */
! 4677: double **dnewm,**doldm;
! 4678: double **dnewmp,**doldmp;
! 4679: int i, j, nhstepm, hstepm, h, nstepm ;
! 4680: int k;
! 4681: double *xp;
! 4682: double **gp, **gm; /* for var eij */
! 4683: double ***gradg, ***trgradg; /*for var eij */
! 4684: double **gradgp, **trgradgp; /* for var p point j */
! 4685: double *gpp, *gmp; /* for var p point j */
! 4686: double **varppt; /* for var p point j nlstate to nlstate+ndeath */
! 4687: double ***p3mat;
! 4688: double age,agelim, hf;
! 4689: /* double ***mobaverage; */
! 4690: int theta;
! 4691: char digit[4];
! 4692: char digitp[25];
! 4693:
! 4694: char fileresprobmorprev[FILENAMELENGTH];
! 4695:
! 4696: if(popbased==1){
! 4697: if(mobilav!=0)
! 4698: strcpy(digitp,"-POPULBASED-MOBILAV_");
! 4699: else strcpy(digitp,"-POPULBASED-NOMOBIL_");
! 4700: }
! 4701: else
! 4702: strcpy(digitp,"-STABLBASED_");
1.126 brouard 4703:
1.218 ! brouard 4704: /* if (mobilav!=0) { */
! 4705: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
! 4706: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
! 4707: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
! 4708: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
! 4709: /* } */
! 4710: /* } */
! 4711:
! 4712: strcpy(fileresprobmorprev,"PRMORPREV-");
! 4713: sprintf(digit,"%-d",ij);
! 4714: /*printf("DIGIT=%s, ij=%d ijr=%-d|\n",digit, ij,ij);*/
! 4715: strcat(fileresprobmorprev,digit); /* Tvar to be done */
! 4716: strcat(fileresprobmorprev,digitp); /* Popbased or not, mobilav or not */
! 4717: strcat(fileresprobmorprev,fileresu);
! 4718: if((ficresprobmorprev=fopen(fileresprobmorprev,"w"))==NULL) {
! 4719: printf("Problem with resultfile: %s\n", fileresprobmorprev);
! 4720: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobmorprev);
! 4721: }
! 4722: printf("Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
! 4723: fprintf(ficlog,"Computing total mortality p.j=w1*p1j+w2*p2j+..: result on file '%s' \n",fileresprobmorprev);
! 4724: pstamp(ficresprobmorprev);
! 4725: 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);
! 4726: fprintf(ficresprobmorprev,"# Age cov=%-d",ij);
! 4727: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
! 4728: fprintf(ficresprobmorprev," p.%-d SE",j);
! 4729: for(i=1; i<=nlstate;i++)
! 4730: fprintf(ficresprobmorprev," w%1d p%-d%-d",i,i,j);
! 4731: }
! 4732: fprintf(ficresprobmorprev,"\n");
! 4733:
! 4734: fprintf(ficgp,"\n# Routine varevsij");
! 4735: fprintf(ficgp,"\nunset title \n");
! 4736: /* fprintf(fichtm, "#Local time at start: %s", strstart);*/
! 4737: 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");
! 4738: fprintf(fichtm,"\n<br>%s <br>\n",digitp);
! 4739: /* } */
! 4740: varppt = matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 4741: pstamp(ficresvij);
! 4742: fprintf(ficresvij,"# Variance and covariance of health expectancies e.j \n# (weighted average of eij where weights are ");
! 4743: if(popbased==1)
! 4744: 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);
! 4745: else
! 4746: fprintf(ficresvij,"the age specific period (stable) prevalences in each health state \n");
! 4747: fprintf(ficresvij,"# Age");
! 4748: for(i=1; i<=nlstate;i++)
! 4749: for(j=1; j<=nlstate;j++)
! 4750: fprintf(ficresvij," Cov(e.%1d, e.%1d)",i,j);
! 4751: fprintf(ficresvij,"\n");
! 4752:
! 4753: xp=vector(1,npar);
! 4754: dnewm=matrix(1,nlstate,1,npar);
! 4755: doldm=matrix(1,nlstate,1,nlstate);
! 4756: dnewmp= matrix(nlstate+1,nlstate+ndeath,1,npar);
! 4757: doldmp= matrix(nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 4758:
! 4759: gradgp=matrix(1,npar,nlstate+1,nlstate+ndeath);
! 4760: gpp=vector(nlstate+1,nlstate+ndeath);
! 4761: gmp=vector(nlstate+1,nlstate+ndeath);
! 4762: trgradgp =matrix(nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
1.126 brouard 4763:
1.218 ! brouard 4764: if(estepm < stepm){
! 4765: printf ("Problem %d lower than %d\n",estepm, stepm);
! 4766: }
! 4767: else hstepm=estepm;
! 4768: /* For example we decided to compute the life expectancy with the smallest unit */
! 4769: /* hstepm beeing the number of stepms, if hstepm=1 the length of hstepm is stepm.
! 4770: nhstepm is the number of hstepm from age to agelim
! 4771: nstepm is the number of stepm from age to agelim.
! 4772: Look at function hpijx to understand why because of memory size limitations,
! 4773: we decided (b) to get a life expectancy respecting the most precise curvature of the
! 4774: survival function given by stepm (the optimization length). Unfortunately it
! 4775: means that if the survival funtion is printed every two years of age and if
! 4776: you sum them up and add 1 year (area under the trapezoids) you won't get the same
! 4777: results. So we changed our mind and took the option of the best precision.
! 4778: */
! 4779: hstepm=hstepm/stepm; /* Typically in stepm units, if stepm=6 & estepm=24 , = 24/6 months = 4 */
! 4780: agelim = AGESUP;
! 4781: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
! 4782: nstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
! 4783: nhstepm = nstepm/hstepm;/* Expressed in hstepm, typically nhstepm=40/4=10 */
! 4784: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 4785: gradg=ma3x(0,nhstepm,1,npar,1,nlstate);
! 4786: gp=matrix(0,nhstepm,1,nlstate);
! 4787: gm=matrix(0,nhstepm,1,nlstate);
! 4788:
! 4789:
! 4790: for(theta=1; theta <=npar; theta++){
! 4791: for(i=1; i<=npar; i++){ /* Computes gradient x + delta*/
! 4792: xp[i] = x[i] + (i==theta ?delti[theta]:0);
! 4793: }
! 4794:
! 4795: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
! 4796:
! 4797: if (popbased==1) {
! 4798: if(mobilav ==0){
! 4799: for(i=1; i<=nlstate;i++)
! 4800: prlim[i][i]=probs[(int)age][i][ij];
! 4801: }else{ /* mobilav */
! 4802: for(i=1; i<=nlstate;i++)
! 4803: prlim[i][i]=mobaverage[(int)age][i][ij];
! 4804: }
! 4805: }
! 4806:
! 4807: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij); /* Returns p3mat[i][j][h] for h=1 to nhstepm */
! 4808: for(j=1; j<= nlstate; j++){
! 4809: for(h=0; h<=nhstepm; h++){
! 4810: for(i=1, gp[h][j]=0.;i<=nlstate;i++)
! 4811: gp[h][j] += prlim[i][i]*p3mat[i][j][h];
! 4812: }
! 4813: }
! 4814: /* Next for computing probability of death (h=1 means
! 4815: computed over hstepm matrices product = hstepm*stepm months)
! 4816: as a weighted average of prlim.
! 4817: */
! 4818: for(j=nlstate+1;j<=nlstate+ndeath;j++){
! 4819: for(i=1,gpp[j]=0.; i<= nlstate; i++)
! 4820: gpp[j] += prlim[i][i]*p3mat[i][j][1];
! 4821: }
! 4822: /* end probability of death */
! 4823:
! 4824: for(i=1; i<=npar; i++) /* Computes gradient x - delta */
! 4825: xp[i] = x[i] - (i==theta ?delti[theta]:0);
! 4826:
! 4827: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp, ij);
! 4828:
! 4829: if (popbased==1) {
! 4830: if(mobilav ==0){
! 4831: for(i=1; i<=nlstate;i++)
! 4832: prlim[i][i]=probs[(int)age][i][ij];
! 4833: }else{ /* mobilav */
! 4834: for(i=1; i<=nlstate;i++)
! 4835: prlim[i][i]=mobaverage[(int)age][i][ij];
! 4836: }
! 4837: }
! 4838:
! 4839: hpxij(p3mat,nhstepm,age,hstepm,xp,nlstate,stepm,oldm,savm, ij);
! 4840:
! 4841: for(j=1; j<= nlstate; j++){ /* Sum of wi * eij = e.j */
! 4842: for(h=0; h<=nhstepm; h++){
! 4843: for(i=1, gm[h][j]=0.;i<=nlstate;i++)
! 4844: gm[h][j] += prlim[i][i]*p3mat[i][j][h];
! 4845: }
! 4846: }
! 4847: /* This for computing probability of death (h=1 means
! 4848: computed over hstepm matrices product = hstepm*stepm months)
! 4849: as a weighted average of prlim.
! 4850: */
! 4851: for(j=nlstate+1;j<=nlstate+ndeath;j++){
! 4852: for(i=1,gmp[j]=0.; i<= nlstate; i++)
! 4853: gmp[j] += prlim[i][i]*p3mat[i][j][1];
! 4854: }
! 4855: /* end probability of death */
! 4856:
! 4857: for(j=1; j<= nlstate; j++) /* vareij */
! 4858: for(h=0; h<=nhstepm; h++){
! 4859: gradg[h][theta][j]= (gp[h][j]-gm[h][j])/2./delti[theta];
! 4860: }
! 4861:
! 4862: for(j=nlstate+1; j<= nlstate+ndeath; j++){ /* var mu */
! 4863: gradgp[theta][j]= (gpp[j]-gmp[j])/2./delti[theta];
! 4864: }
! 4865:
! 4866: } /* End theta */
! 4867:
! 4868: trgradg =ma3x(0,nhstepm,1,nlstate,1,npar); /* veij */
! 4869:
! 4870: for(h=0; h<=nhstepm; h++) /* veij */
! 4871: for(j=1; j<=nlstate;j++)
! 4872: for(theta=1; theta <=npar; theta++)
! 4873: trgradg[h][j][theta]=gradg[h][theta][j];
! 4874:
! 4875: for(j=nlstate+1; j<=nlstate+ndeath;j++) /* mu */
! 4876: for(theta=1; theta <=npar; theta++)
! 4877: trgradgp[j][theta]=gradgp[theta][j];
! 4878:
! 4879:
! 4880: hf=hstepm*stepm/YEARM; /* Duration of hstepm expressed in year unit. */
! 4881: for(i=1;i<=nlstate;i++)
! 4882: for(j=1;j<=nlstate;j++)
! 4883: vareij[i][j][(int)age] =0.;
! 4884:
! 4885: for(h=0;h<=nhstepm;h++){
! 4886: for(k=0;k<=nhstepm;k++){
! 4887: matprod2(dnewm,trgradg[h],1,nlstate,1,npar,1,npar,matcov);
! 4888: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg[k]);
! 4889: for(i=1;i<=nlstate;i++)
! 4890: for(j=1;j<=nlstate;j++)
! 4891: vareij[i][j][(int)age] += doldm[i][j]*hf*hf;
! 4892: }
! 4893: }
! 4894:
! 4895: /* pptj */
! 4896: matprod2(dnewmp,trgradgp,nlstate+1,nlstate+ndeath,1,npar,1,npar,matcov);
! 4897: matprod2(doldmp,dnewmp,nlstate+1,nlstate+ndeath,1,npar,nlstate+1,nlstate+ndeath,gradgp);
! 4898: for(j=nlstate+1;j<=nlstate+ndeath;j++)
! 4899: for(i=nlstate+1;i<=nlstate+ndeath;i++)
! 4900: varppt[j][i]=doldmp[j][i];
! 4901: /* end ppptj */
! 4902: /* x centered again */
! 4903:
! 4904: prevalim(prlim,nlstate,x,age,oldm,savm,ftolpl,ncvyearp,ij);
! 4905:
! 4906: if (popbased==1) {
! 4907: if(mobilav ==0){
! 4908: for(i=1; i<=nlstate;i++)
! 4909: prlim[i][i]=probs[(int)age][i][ij];
! 4910: }else{ /* mobilav */
! 4911: for(i=1; i<=nlstate;i++)
! 4912: prlim[i][i]=mobaverage[(int)age][i][ij];
! 4913: }
! 4914: }
! 4915:
! 4916: /* This for computing probability of death (h=1 means
! 4917: computed over hstepm (estepm) matrices product = hstepm*stepm months)
! 4918: as a weighted average of prlim.
! 4919: */
! 4920: hpxij(p3mat,nhstepm,age,hstepm,x,nlstate,stepm,oldm,savm, ij);
! 4921: for(j=nlstate+1;j<=nlstate+ndeath;j++){
! 4922: for(i=1,gmp[j]=0.;i<= nlstate; i++)
! 4923: gmp[j] += prlim[i][i]*p3mat[i][j][1];
! 4924: }
! 4925: /* end probability of death */
! 4926:
! 4927: fprintf(ficresprobmorprev,"%3d %d ",(int) age, ij);
! 4928: for(j=nlstate+1; j<=(nlstate+ndeath);j++){
! 4929: fprintf(ficresprobmorprev," %11.3e %11.3e",gmp[j], sqrt(varppt[j][j]));
! 4930: for(i=1; i<=nlstate;i++){
! 4931: fprintf(ficresprobmorprev," %11.3e %11.3e ",prlim[i][i],p3mat[i][j][1]);
! 4932: }
! 4933: }
! 4934: fprintf(ficresprobmorprev,"\n");
! 4935:
! 4936: fprintf(ficresvij,"%.0f ",age );
! 4937: for(i=1; i<=nlstate;i++)
! 4938: for(j=1; j<=nlstate;j++){
! 4939: fprintf(ficresvij," %.4f", vareij[i][j][(int)age]);
! 4940: }
! 4941: fprintf(ficresvij,"\n");
! 4942: free_matrix(gp,0,nhstepm,1,nlstate);
! 4943: free_matrix(gm,0,nhstepm,1,nlstate);
! 4944: free_ma3x(gradg,0,nhstepm,1,npar,1,nlstate);
! 4945: free_ma3x(trgradg,0,nhstepm,1,nlstate,1,npar);
! 4946: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 4947: } /* End age */
! 4948: free_vector(gpp,nlstate+1,nlstate+ndeath);
! 4949: free_vector(gmp,nlstate+1,nlstate+ndeath);
! 4950: free_matrix(gradgp,1,npar,nlstate+1,nlstate+ndeath);
! 4951: free_matrix(trgradgp,nlstate+1,nlstate+ndeath,1,npar); /* mu or p point j*/
! 4952: /* fprintf(ficgp,"\nunset parametric;unset label; set ter png small size 320, 240"); */
! 4953: fprintf(ficgp,"\nunset parametric;unset label; set ter svg size 640, 480");
! 4954: /* for(j=nlstate+1; j<= nlstate+ndeath; j++){ *//* Only the first actually */
! 4955: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Force of mortality (year-1)\";");
! 4956: fprintf(ficgp,"\nset out \"%s%s.svg\";",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
! 4957: /* fprintf(ficgp,"\n plot \"%s\" u 1:($3*%6.3f) not w l 1 ",fileresprobmorprev,YEARM/estepm); */
! 4958: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)*%6.3f) t \"95\%% interval\" w l 2 ",fileresprobmorprev,YEARM/estepm); */
! 4959: /* fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)*%6.3f) not w l 2 ",fileresprobmorprev,YEARM/estepm); */
! 4960: fprintf(ficgp,"\n plot \"%s\" u 1:($3) not w l lt 1 ",subdirf(fileresprobmorprev));
! 4961: fprintf(ficgp,"\n replot \"%s\" u 1:(($3+1.96*$4)) t \"95%% interval\" w l lt 2 ",subdirf(fileresprobmorprev));
! 4962: fprintf(ficgp,"\n replot \"%s\" u 1:(($3-1.96*$4)) not w l lt 2 ",subdirf(fileresprobmorprev));
! 4963: fprintf(fichtm,"\n<br> File (multiple files are possible if covariates are present): <A href=\"%s\">%s</a>\n",subdirf(fileresprobmorprev),subdirf(fileresprobmorprev));
! 4964: 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);
! 4965: /* 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 4966: */
1.218 ! brouard 4967: /* fprintf(ficgp,"\nset out \"varmuptjgr%s%s%s.svg\";replot;",digitp,optionfilefiname,digit); */
! 4968: fprintf(ficgp,"\nset out;\nset out \"%s%s.svg\";replot;set out;\n",subdirf3(optionfilefiname,"VARMUPTJGR-",digitp),digit);
1.126 brouard 4969:
1.218 ! brouard 4970: free_vector(xp,1,npar);
! 4971: free_matrix(doldm,1,nlstate,1,nlstate);
! 4972: free_matrix(dnewm,1,nlstate,1,npar);
! 4973: free_matrix(doldmp,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 4974: free_matrix(dnewmp,nlstate+1,nlstate+ndeath,1,npar);
! 4975: free_matrix(varppt,nlstate+1,nlstate+ndeath,nlstate+1,nlstate+ndeath);
! 4976: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
! 4977: fclose(ficresprobmorprev);
! 4978: fflush(ficgp);
! 4979: fflush(fichtm);
! 4980: } /* end varevsij */
1.126 brouard 4981:
4982: /************ Variance of prevlim ******************/
1.209 brouard 4983: 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 4984: {
1.205 brouard 4985: /* Variance of prevalence limit for each state ij using current parameters x[] and estimates of neighbourhood give by delti*/
1.126 brouard 4986: /* double **prevalim(double **prlim, int nlstate, double *xp, double age, double **oldm, double **savm,double ftolpl);*/
1.164 brouard 4987:
1.126 brouard 4988: double **dnewm,**doldm;
4989: int i, j, nhstepm, hstepm;
4990: double *xp;
4991: double *gp, *gm;
4992: double **gradg, **trgradg;
1.208 brouard 4993: double **mgm, **mgp;
1.126 brouard 4994: double age,agelim;
4995: int theta;
4996:
4997: pstamp(ficresvpl);
4998: fprintf(ficresvpl,"# Standard deviation of period (stable) prevalences \n");
4999: fprintf(ficresvpl,"# Age");
5000: for(i=1; i<=nlstate;i++)
5001: fprintf(ficresvpl," %1d-%1d",i,i);
5002: fprintf(ficresvpl,"\n");
5003:
5004: xp=vector(1,npar);
5005: dnewm=matrix(1,nlstate,1,npar);
5006: doldm=matrix(1,nlstate,1,nlstate);
5007:
5008: hstepm=1*YEARM; /* Every year of age */
5009: hstepm=hstepm/stepm; /* Typically in stepm units, if j= 2 years, = 2/6 months = 4 */
5010: agelim = AGESUP;
5011: for (age=bage; age<=fage; age ++){ /* If stepm=6 months */
5012: nhstepm=(int) rint((agelim-age)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
5013: if (stepm >= YEARM) hstepm=1;
5014: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
5015: gradg=matrix(1,npar,1,nlstate);
1.208 brouard 5016: mgp=matrix(1,npar,1,nlstate);
5017: mgm=matrix(1,npar,1,nlstate);
1.126 brouard 5018: gp=vector(1,nlstate);
5019: gm=vector(1,nlstate);
5020:
5021: for(theta=1; theta <=npar; theta++){
5022: for(i=1; i<=npar; i++){ /* Computes gradient */
5023: xp[i] = x[i] + (i==theta ?delti[theta]:0);
5024: }
1.209 brouard 5025: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5026: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5027: else
5028: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5029: for(i=1;i<=nlstate;i++){
1.126 brouard 5030: gp[i] = prlim[i][i];
1.208 brouard 5031: mgp[theta][i] = prlim[i][i];
5032: }
1.126 brouard 5033: for(i=1; i<=npar; i++) /* Computes gradient */
5034: xp[i] = x[i] - (i==theta ?delti[theta]:0);
1.209 brouard 5035: if((int)age==79 ||(int)age== 80 ||(int)age== 81 )
5036: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
5037: else
5038: prevalim(prlim,nlstate,xp,age,oldm,savm,ftolpl,ncvyearp,ij);
1.208 brouard 5039: for(i=1;i<=nlstate;i++){
1.126 brouard 5040: gm[i] = prlim[i][i];
1.208 brouard 5041: mgm[theta][i] = prlim[i][i];
5042: }
1.126 brouard 5043: for(i=1;i<=nlstate;i++)
5044: gradg[theta][i]= (gp[i]-gm[i])/2./delti[theta];
1.209 brouard 5045: /* gradg[theta][2]= -gradg[theta][1]; */ /* For testing if nlstate=2 */
1.126 brouard 5046: } /* End theta */
5047:
5048: trgradg =matrix(1,nlstate,1,npar);
5049:
5050: for(j=1; j<=nlstate;j++)
5051: for(theta=1; theta <=npar; theta++)
5052: trgradg[j][theta]=gradg[theta][j];
1.209 brouard 5053: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5054: /* printf("\nmgm mgp %d ",(int)age); */
5055: /* for(j=1; j<=nlstate;j++){ */
5056: /* printf(" %d ",j); */
5057: /* for(theta=1; theta <=npar; theta++) */
5058: /* printf(" %d %lf %lf",theta,mgm[theta][j],mgp[theta][j]); */
5059: /* printf("\n "); */
5060: /* } */
5061: /* } */
5062: /* if((int)age==79 ||(int)age== 80 ||(int)age== 81 ){ */
5063: /* printf("\n gradg %d ",(int)age); */
5064: /* for(j=1; j<=nlstate;j++){ */
5065: /* printf("%d ",j); */
5066: /* for(theta=1; theta <=npar; theta++) */
5067: /* printf("%d %lf ",theta,gradg[theta][j]); */
5068: /* printf("\n "); */
5069: /* } */
5070: /* } */
1.126 brouard 5071:
5072: for(i=1;i<=nlstate;i++)
5073: varpl[i][(int)age] =0.;
1.209 brouard 5074: if((int)age==79 ||(int)age== 80 ||(int)age== 81){
1.205 brouard 5075: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5076: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
5077: }else{
1.126 brouard 5078: matprod2(dnewm,trgradg,1,nlstate,1,npar,1,npar,matcov);
5079: matprod2(doldm,dnewm,1,nlstate,1,npar,1,nlstate,gradg);
1.205 brouard 5080: }
1.126 brouard 5081: for(i=1;i<=nlstate;i++)
5082: varpl[i][(int)age] = doldm[i][i]; /* Covariances are useless */
5083:
5084: fprintf(ficresvpl,"%.0f ",age );
5085: for(i=1; i<=nlstate;i++)
5086: fprintf(ficresvpl," %.5f (%.5f)",prlim[i][i],sqrt(varpl[i][(int)age]));
5087: fprintf(ficresvpl,"\n");
5088: free_vector(gp,1,nlstate);
5089: free_vector(gm,1,nlstate);
1.208 brouard 5090: free_matrix(mgm,1,npar,1,nlstate);
5091: free_matrix(mgp,1,npar,1,nlstate);
1.126 brouard 5092: free_matrix(gradg,1,npar,1,nlstate);
5093: free_matrix(trgradg,1,nlstate,1,npar);
5094: } /* End age */
5095:
5096: free_vector(xp,1,npar);
5097: free_matrix(doldm,1,nlstate,1,npar);
5098: free_matrix(dnewm,1,nlstate,1,nlstate);
5099:
5100: }
5101:
5102: /************ Variance of one-step probabilities ******************/
5103: 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[])
5104: {
1.164 brouard 5105: int i, j=0, k1, l1, tj;
1.126 brouard 5106: int k2, l2, j1, z1;
1.164 brouard 5107: int k=0, l;
1.145 brouard 5108: int first=1, first1, first2;
1.126 brouard 5109: double cv12, mu1, mu2, lc1, lc2, v12, v21, v11, v22,v1,v2, c12, tnalp;
5110: double **dnewm,**doldm;
5111: double *xp;
5112: double *gp, *gm;
5113: double **gradg, **trgradg;
5114: double **mu;
1.164 brouard 5115: double age, cov[NCOVMAX+1];
1.126 brouard 5116: double std=2.0; /* Number of standard deviation wide of confidence ellipsoids */
5117: int theta;
5118: char fileresprob[FILENAMELENGTH];
5119: char fileresprobcov[FILENAMELENGTH];
5120: char fileresprobcor[FILENAMELENGTH];
5121: double ***varpij;
5122:
1.201 brouard 5123: strcpy(fileresprob,"PROB_");
1.126 brouard 5124: strcat(fileresprob,fileres);
5125: if((ficresprob=fopen(fileresprob,"w"))==NULL) {
5126: printf("Problem with resultfile: %s\n", fileresprob);
5127: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprob);
5128: }
1.201 brouard 5129: strcpy(fileresprobcov,"PROBCOV_");
1.202 brouard 5130: strcat(fileresprobcov,fileresu);
1.126 brouard 5131: if((ficresprobcov=fopen(fileresprobcov,"w"))==NULL) {
5132: printf("Problem with resultfile: %s\n", fileresprobcov);
5133: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcov);
5134: }
1.201 brouard 5135: strcpy(fileresprobcor,"PROBCOR_");
1.202 brouard 5136: strcat(fileresprobcor,fileresu);
1.126 brouard 5137: if((ficresprobcor=fopen(fileresprobcor,"w"))==NULL) {
5138: printf("Problem with resultfile: %s\n", fileresprobcor);
5139: fprintf(ficlog,"Problem with resultfile: %s\n", fileresprobcor);
5140: }
5141: printf("Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5142: fprintf(ficlog,"Computing standard deviation of one-step probabilities: result on file '%s' \n",fileresprob);
5143: printf("Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5144: fprintf(ficlog,"Computing matrix of variance covariance of one-step probabilities: result on file '%s' \n",fileresprobcov);
5145: printf("and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5146: fprintf(ficlog,"and correlation matrix of one-step probabilities: result on file '%s' \n",fileresprobcor);
5147: pstamp(ficresprob);
5148: fprintf(ficresprob,"#One-step probabilities and stand. devi in ()\n");
5149: fprintf(ficresprob,"# Age");
5150: pstamp(ficresprobcov);
5151: fprintf(ficresprobcov,"#One-step probabilities and covariance matrix\n");
5152: fprintf(ficresprobcov,"# Age");
5153: pstamp(ficresprobcor);
5154: fprintf(ficresprobcor,"#One-step probabilities and correlation matrix\n");
5155: fprintf(ficresprobcor,"# Age");
5156:
5157:
5158: for(i=1; i<=nlstate;i++)
5159: for(j=1; j<=(nlstate+ndeath);j++){
5160: fprintf(ficresprob," p%1d-%1d (SE)",i,j);
5161: fprintf(ficresprobcov," p%1d-%1d ",i,j);
5162: fprintf(ficresprobcor," p%1d-%1d ",i,j);
5163: }
5164: /* fprintf(ficresprob,"\n");
5165: fprintf(ficresprobcov,"\n");
5166: fprintf(ficresprobcor,"\n");
5167: */
1.131 brouard 5168: xp=vector(1,npar);
1.126 brouard 5169: dnewm=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5170: doldm=matrix(1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5171: mu=matrix(1,(nlstate)*(nlstate+ndeath), (int) bage, (int)fage);
5172: varpij=ma3x(1,nlstate*(nlstate+ndeath),1,nlstate*(nlstate+ndeath),(int) bage, (int) fage);
5173: first=1;
5174: fprintf(ficgp,"\n# Routine varprob");
5175: fprintf(fichtm,"\n<li><h4> Computing and drawing one step probabilities with their confidence intervals</h4></li>\n");
5176: fprintf(fichtm,"\n");
5177:
1.200 brouard 5178: 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);
1.197 brouard 5179: 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);
5180: fprintf(fichtmcov,"\nEllipsoids of confidence centered on point (p<inf>ij</inf>, p<inf>kl</inf>) are estimated \
1.126 brouard 5181: and drawn. It helps understanding how is the covariance between two incidences.\
5182: They are expressed in year<sup>-1</sup> in order to be less dependent of stepm.<br>\n");
5183: 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. \
5184: It can be understood this way: if pij and pkl where uncorrelated the (2x2) matrix of covariance \
5185: would have been (1/(var pij), 0 , 0, 1/(var pkl)), and the confidence interval would be 2 \
5186: standard deviations wide on each axis. <br>\
5187: Now, if both incidences are correlated (usual case) we diagonalised the inverse of the covariance matrix\
5188: and made the appropriate rotation to look at the uncorrelated principal directions.<br>\
5189: To be simple, these graphs help to understand the significativity of each parameter in relation to a second other one.<br> \n");
5190:
5191: cov[1]=1;
1.145 brouard 5192: /* tj=cptcoveff; */
5193: tj = (int) pow(2,cptcoveff);
1.126 brouard 5194: if (cptcovn<1) {tj=1;ncodemax[1]=1;}
5195: j1=0;
1.145 brouard 5196: for(j1=1; j1<=tj;j1++){
5197: /*for(i1=1; i1<=ncodemax[t];i1++){ */
5198: /*j1++;*/
1.126 brouard 5199: if (cptcovn>0) {
5200: fprintf(ficresprob, "\n#********** Variable ");
1.198 brouard 5201: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprob, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.126 brouard 5202: fprintf(ficresprob, "**********\n#\n");
5203: fprintf(ficresprobcov, "\n#********** Variable ");
1.198 brouard 5204: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcov, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.126 brouard 5205: fprintf(ficresprobcov, "**********\n#\n");
5206:
5207: fprintf(ficgp, "\n#********** Variable ");
1.198 brouard 5208: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficgp, " V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.126 brouard 5209: fprintf(ficgp, "**********\n#\n");
5210:
5211:
5212: fprintf(fichtmcov, "\n<hr size=\"2\" color=\"#EC5E5E\">********** Variable ");
1.198 brouard 5213: for (z1=1; z1<=cptcoveff; z1++) fprintf(fichtm, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.126 brouard 5214: fprintf(fichtmcov, "**********\n<hr size=\"2\" color=\"#EC5E5E\">");
5215:
5216: fprintf(ficresprobcor, "\n#********** Variable ");
1.198 brouard 5217: for (z1=1; z1<=cptcoveff; z1++) fprintf(ficresprobcor, "V%d=%d ",Tvaraff[z1],nbcode[Tvaraff[z1]][codtabm(j1,z1)]);
1.126 brouard 5218: fprintf(ficresprobcor, "**********\n#");
5219: }
5220:
1.145 brouard 5221: gradg=matrix(1,npar,1,(nlstate)*(nlstate+ndeath));
5222: trgradg=matrix(1,(nlstate)*(nlstate+ndeath),1,npar);
5223: gp=vector(1,(nlstate)*(nlstate+ndeath));
5224: gm=vector(1,(nlstate)*(nlstate+ndeath));
1.126 brouard 5225: for (age=bage; age<=fage; age ++){
5226: cov[2]=age;
1.187 brouard 5227: if(nagesqr==1)
5228: cov[3]= age*age;
1.126 brouard 5229: for (k=1; k<=cptcovn;k++) {
1.200 brouard 5230: cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,k)];
5231: /*cov[2+nagesqr+k]=nbcode[Tvar[k]][codtabm(j1,Tvar[k])];*//* j1 1 2 3 4
1.145 brouard 5232: * 1 1 1 1 1
5233: * 2 2 1 1 1
5234: * 3 1 2 1 1
5235: */
5236: /* nbcode[1][1]=0 nbcode[1][2]=1;*/
1.126 brouard 5237: }
1.186 brouard 5238: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=cov[2+Tage[k]]*cov[2]; */
1.200 brouard 5239: for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,k)]*cov[2];
1.126 brouard 5240: for (k=1; k<=cptcovprod;k++)
1.200 brouard 5241: cov[2+nagesqr+Tprod[k]]=nbcode[Tvard[k][1]][codtabm(ij,k)]*nbcode[Tvard[k][2]][codtabm(ij,k)];
1.126 brouard 5242:
5243:
5244: for(theta=1; theta <=npar; theta++){
5245: for(i=1; i<=npar; i++)
5246: xp[i] = x[i] + (i==theta ?delti[theta]:(double)0);
5247:
5248: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5249:
5250: k=0;
5251: for(i=1; i<= (nlstate); i++){
5252: for(j=1; j<=(nlstate+ndeath);j++){
5253: k=k+1;
5254: gp[k]=pmmij[i][j];
5255: }
5256: }
5257:
5258: for(i=1; i<=npar; i++)
5259: xp[i] = x[i] - (i==theta ?delti[theta]:(double)0);
5260:
5261: pmij(pmmij,cov,ncovmodel,xp,nlstate);
5262: k=0;
5263: for(i=1; i<=(nlstate); i++){
5264: for(j=1; j<=(nlstate+ndeath);j++){
5265: k=k+1;
5266: gm[k]=pmmij[i][j];
5267: }
5268: }
5269:
5270: for(i=1; i<= (nlstate)*(nlstate+ndeath); i++)
5271: gradg[theta][i]=(gp[i]-gm[i])/(double)2./delti[theta];
5272: }
5273:
5274: for(j=1; j<=(nlstate)*(nlstate+ndeath);j++)
5275: for(theta=1; theta <=npar; theta++)
5276: trgradg[j][theta]=gradg[theta][j];
5277:
5278: matprod2(dnewm,trgradg,1,(nlstate)*(nlstate+ndeath),1,npar,1,npar,matcov);
5279: matprod2(doldm,dnewm,1,(nlstate)*(nlstate+ndeath),1,npar,1,(nlstate)*(nlstate+ndeath),gradg);
5280:
5281: pmij(pmmij,cov,ncovmodel,x,nlstate);
5282:
5283: k=0;
5284: for(i=1; i<=(nlstate); i++){
5285: for(j=1; j<=(nlstate+ndeath);j++){
5286: k=k+1;
5287: mu[k][(int) age]=pmmij[i][j];
5288: }
5289: }
5290: for(i=1;i<=(nlstate)*(nlstate+ndeath);i++)
5291: for(j=1;j<=(nlstate)*(nlstate+ndeath);j++)
5292: varpij[i][j][(int)age] = doldm[i][j];
5293:
5294: /*printf("\n%d ",(int)age);
5295: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5296: printf("%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5297: fprintf(ficlog,"%e [%e ;%e] ",gm[i],gm[i]-2*sqrt(doldm[i][i]),gm[i]+2*sqrt(doldm[i][i]));
5298: }*/
5299:
5300: fprintf(ficresprob,"\n%d ",(int)age);
5301: fprintf(ficresprobcov,"\n%d ",(int)age);
5302: fprintf(ficresprobcor,"\n%d ",(int)age);
5303:
5304: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++)
5305: fprintf(ficresprob,"%11.3e (%11.3e) ",mu[i][(int) age],sqrt(varpij[i][i][(int)age]));
5306: for (i=1; i<=(nlstate)*(nlstate+ndeath);i++){
5307: fprintf(ficresprobcov,"%11.3e ",mu[i][(int) age]);
5308: fprintf(ficresprobcor,"%11.3e ",mu[i][(int) age]);
5309: }
5310: i=0;
5311: for (k=1; k<=(nlstate);k++){
5312: for (l=1; l<=(nlstate+ndeath);l++){
1.145 brouard 5313: i++;
1.126 brouard 5314: fprintf(ficresprobcov,"\n%d %d-%d",(int)age,k,l);
5315: fprintf(ficresprobcor,"\n%d %d-%d",(int)age,k,l);
5316: for (j=1; j<=i;j++){
1.145 brouard 5317: /* printf(" k=%d l=%d i=%d j=%d\n",k,l,i,j);fflush(stdout); */
1.126 brouard 5318: fprintf(ficresprobcov," %11.3e",varpij[i][j][(int)age]);
5319: fprintf(ficresprobcor," %11.3e",varpij[i][j][(int) age]/sqrt(varpij[i][i][(int) age])/sqrt(varpij[j][j][(int)age]));
5320: }
5321: }
5322: }/* end of loop for state */
5323: } /* end of loop for age */
1.145 brouard 5324: free_vector(gp,1,(nlstate+ndeath)*(nlstate+ndeath));
5325: free_vector(gm,1,(nlstate+ndeath)*(nlstate+ndeath));
5326: free_matrix(trgradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5327: free_matrix(gradg,1,(nlstate+ndeath)*(nlstate+ndeath),1,npar);
5328:
1.126 brouard 5329: /* Confidence intervalle of pij */
5330: /*
1.131 brouard 5331: fprintf(ficgp,"\nunset parametric;unset label");
1.126 brouard 5332: fprintf(ficgp,"\nset log y;unset log x; set xlabel \"Age\";set ylabel \"probability (year-1)\"");
5333: fprintf(ficgp,"\nset ter png small\nset size 0.65,0.65");
5334: 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);
5335: fprintf(fichtm,"\n<br><img src=\"pijgr%s.png\"> ",optionfilefiname);
5336: fprintf(ficgp,"\nset out \"pijgr%s.png\"",optionfilefiname);
5337: fprintf(ficgp,"\nplot \"%s\" every :::%d::%d u 1:2 \"\%%lf",k1,k2,xfilevarprob);
5338: */
5339:
5340: /* Drawing ellipsoids of confidence of two variables p(k1-l1,k2-l2)*/
1.145 brouard 5341: first1=1;first2=2;
1.126 brouard 5342: for (k2=1; k2<=(nlstate);k2++){
5343: for (l2=1; l2<=(nlstate+ndeath);l2++){
5344: if(l2==k2) continue;
5345: j=(k2-1)*(nlstate+ndeath)+l2;
5346: for (k1=1; k1<=(nlstate);k1++){
5347: for (l1=1; l1<=(nlstate+ndeath);l1++){
5348: if(l1==k1) continue;
5349: i=(k1-1)*(nlstate+ndeath)+l1;
5350: if(i<=j) continue;
5351: for (age=bage; age<=fage; age ++){
5352: if ((int)age %5==0){
5353: v1=varpij[i][i][(int)age]/stepm*YEARM/stepm*YEARM;
5354: v2=varpij[j][j][(int)age]/stepm*YEARM/stepm*YEARM;
5355: cv12=varpij[i][j][(int)age]/stepm*YEARM/stepm*YEARM;
5356: mu1=mu[i][(int) age]/stepm*YEARM ;
5357: mu2=mu[j][(int) age]/stepm*YEARM;
5358: c12=cv12/sqrt(v1*v2);
5359: /* Computing eigen value of matrix of covariance */
5360: lc1=((v1+v2)+sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
5361: lc2=((v1+v2)-sqrt((v1+v2)*(v1+v2) - 4*(v1*v2-cv12*cv12)))/2.;
1.135 brouard 5362: if ((lc2 <0) || (lc1 <0) ){
1.145 brouard 5363: if(first2==1){
5364: first1=0;
5365: 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);
5366: }
5367: 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);
5368: /* lc1=fabs(lc1); */ /* If we want to have them positive */
5369: /* lc2=fabs(lc2); */
1.135 brouard 5370: }
5371:
1.126 brouard 5372: /* Eigen vectors */
5373: v11=(1./sqrt(1+(v1-lc1)*(v1-lc1)/cv12/cv12));
5374: /*v21=sqrt(1.-v11*v11); *//* error */
5375: v21=(lc1-v1)/cv12*v11;
5376: v12=-v21;
5377: v22=v11;
5378: tnalp=v21/v11;
5379: if(first1==1){
5380: first1=0;
5381: 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);
5382: }
5383: 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);
5384: /*printf(fignu*/
5385: /* mu1+ v11*lc1*cost + v12*lc2*sin(t) */
5386: /* mu2+ v21*lc1*cost + v22*lc2*sin(t) */
5387: if(first==1){
5388: first=0;
1.200 brouard 5389: fprintf(ficgp,"\n# Ellipsoids of confidence\n#\n");
1.126 brouard 5390: fprintf(ficgp,"\nset parametric;unset label");
5391: 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);
1.199 brouard 5392: fprintf(ficgp,"\nset ter svg size 640, 480");
1.126 brouard 5393: fprintf(fichtmcov,"\n<br>Ellipsoids of confidence cov(p%1d%1d,p%1d%1d) expressed in year<sup>-1</sup>\
1.201 brouard 5394: :<a href=\"%s_%d%1d%1d-%1d%1d.svg\">\
5395: %s_%d%1d%1d-%1d%1d.svg</A>, ",k1,l1,k2,l2,\
5396: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2,\
5397: subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
5398: fprintf(fichtmcov,"\n<br><img src=\"%s_%d%1d%1d-%1d%1d.svg\"> ",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
1.126 brouard 5399: fprintf(fichtmcov,"\n<br> Correlation at age %d (%.3f),",(int) age, c12);
1.201 brouard 5400: fprintf(ficgp,"\nset out \"%s_%d%1d%1d-%1d%1d.svg\"",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
1.126 brouard 5401: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5402: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5403: 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",\
5404: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
5405: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5406: }else{
5407: first=0;
5408: fprintf(fichtmcov," %d (%.3f),",(int) age, c12);
5409: fprintf(ficgp,"\n# Age %d, p%1d%1d - p%1d%1d",(int) age, k1,l1,k2,l2);
5410: fprintf(ficgp,"\nset label \"%d\" at %11.3e,%11.3e center",(int) age, mu1,mu2);
5411: 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",\
5412: mu1,std,v11,sqrt(lc1),v12,sqrt(lc2),\
5413: mu2,std,v21,sqrt(lc1),v22,sqrt(lc2));
5414: }/* if first */
5415: } /* age mod 5 */
5416: } /* end loop age */
1.201 brouard 5417: fprintf(ficgp,"\nset out;\nset out \"%s_%d%1d%1d-%1d%1d.svg\";replot;set out;",subdirf2(optionfilefiname,"VARPIJGR_"), j1,k1,l1,k2,l2);
1.126 brouard 5418: first=1;
5419: } /*l12 */
5420: } /* k12 */
5421: } /*l1 */
5422: }/* k1 */
1.169 brouard 5423: /* } */ /* loop covariates */
1.126 brouard 5424: }
5425: free_ma3x(varpij,1,nlstate,1,nlstate+ndeath,(int) bage, (int)fage);
5426: free_matrix(mu,1,(nlstate+ndeath)*(nlstate+ndeath),(int) bage, (int)fage);
5427: free_matrix(doldm,1,(nlstate)*(nlstate+ndeath),1,(nlstate)*(nlstate+ndeath));
5428: free_matrix(dnewm,1,(nlstate)*(nlstate+ndeath),1,npar);
5429: free_vector(xp,1,npar);
5430: fclose(ficresprob);
5431: fclose(ficresprobcov);
5432: fclose(ficresprobcor);
5433: fflush(ficgp);
5434: fflush(fichtmcov);
5435: }
5436:
5437:
5438: /******************* Printing html file ***********/
1.201 brouard 5439: void printinghtml(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 5440: int lastpass, int stepm, int weightopt, char model[],\
5441: int imx,int jmin, int jmax, double jmeanint,char rfileres[],\
1.217 brouard 5442: int popforecast, int prevfcast, int backcast, int estepm , \
1.213 brouard 5443: double jprev1, double mprev1,double anprev1, double dateprev1, \
5444: double jprev2, double mprev2,double anprev2, double dateprev2){
1.126 brouard 5445: int jj1, k1, i1, cpt;
5446:
5447: fprintf(fichtm,"<ul><li><a href='#firstorder'>Result files (first order: no variance)</a>\n \
5448: <li><a href='#secondorder'>Result files (second order (variance)</a>\n \
5449: </ul>");
1.214 brouard 5450: fprintf(fichtm,"<ul><li><h4><a name='firstorder'>Result files (first order: no variance)</a></h4>\n");
5451: 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",
5452: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTMFR_",".htm"),subdirfext3(optionfilefiname,"PHTMFR_",".htm"));
5453: 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 5454: jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,subdirfext3(optionfilefiname,"PHTM_",".htm"),subdirfext3(optionfilefiname,"PHTM_",".htm"));
5455: fprintf(fichtm,", <a href=\"%s\">%s</a> (text file) <br>\n",subdirf2(fileresu,"P_"),subdirf2(fileresu,"P_"));
1.126 brouard 5456: fprintf(fichtm,"\
5457: - Estimated transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
1.201 brouard 5458: stepm,subdirf2(fileresu,"PIJ_"),subdirf2(fileresu,"PIJ_"));
1.126 brouard 5459: fprintf(fichtm,"\
1.217 brouard 5460: - Estimated back transition probabilities over %d (stepm) months: <a href=\"%s\">%s</a><br>\n ",
5461: stepm,subdirf2(fileresu,"PIJB_"),subdirf2(fileresu,"PIJB_"));
5462: fprintf(fichtm,"\
1.126 brouard 5463: - Period (stable) prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5464: subdirf2(fileresu,"PL_"),subdirf2(fileresu,"PL_"));
1.126 brouard 5465: fprintf(fichtm,"\
1.217 brouard 5466: - Period (stable) back prevalence in each health state: <a href=\"%s\">%s</a> <br>\n",
5467: subdirf2(fileresu,"PLB_"),subdirf2(fileresu,"PLB_"));
5468: fprintf(fichtm,"\
1.211 brouard 5469: - (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 5470: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5471: estepm,subdirf2(fileresu,"E_"),subdirf2(fileresu,"E_"));
1.211 brouard 5472: if(prevfcast==1){
5473: fprintf(fichtm,"\
5474: - Prevalence projections by age and states: \
1.201 brouard 5475: <a href=\"%s\">%s</a> <br>\n</li>", subdirf2(fileresu,"F_"),subdirf2(fileresu,"F_"));
1.211 brouard 5476: }
1.126 brouard 5477:
5478: fprintf(fichtm," \n<ul><li><b>Graphs</b></li><p>");
5479:
1.145 brouard 5480: m=pow(2,cptcoveff);
1.126 brouard 5481: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
5482:
5483: jj1=0;
5484: for(k1=1; k1<=m;k1++){
1.192 brouard 5485: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
1.126 brouard 5486: jj1++;
5487: if (cptcovn > 0) {
5488: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
1.192 brouard 5489: for (cpt=1; cpt<=cptcoveff;cpt++){
1.198 brouard 5490: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
5491: printf(" V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);fflush(stdout);
1.192 brouard 5492: }
1.126 brouard 5493: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
5494: }
1.201 brouard 5495: /* aij, bij */
1.211 brouard 5496: 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> \
5497: <img src=\"%s_%d-1.svg\">",model,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.126 brouard 5498: /* Pij */
1.211 brouard 5499: 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 5500: <img src=\"%s_%d-2.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
1.126 brouard 5501: /* Quasi-incidences */
1.211 brouard 5502: 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.201 brouard 5503: before but expressed in per year i.e. quasi incidences if stepm is small and probabilities too,\
1.211 brouard 5504: incidence (rates) are the limit when h tends to zero of the ratio of the probability <sub>h</sub>P<sub>ij</sub> \
5505: 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 5506: <img src=\"%s_%d-3.svg\">",stepm,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1,subdirf2(optionfilefiname,"PE_"),jj1);
5507: /* Survival functions (period) in state j */
5508: for(cpt=1; cpt<=nlstate;cpt++){
5509: 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> \
5510: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1,subdirf2(optionfilefiname,"LIJ_"),cpt,jj1);
5511: }
5512: /* State specific survival functions (period) */
5513: for(cpt=1; cpt<=nlstate;cpt++){
1.208 brouard 5514: fprintf(fichtm,"<br>\n- Survival functions from state %d in each live state and total.\
1.201 brouard 5515: Or probability to survive in various states (1 to %d) being in state %d at different ages.\
5516: <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);
5517: }
5518: /* Period (stable) prevalence in each health state */
5519: for(cpt=1; cpt<=nlstate;cpt++){
1.217 brouard 5520: 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 5521: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1,subdirf2(optionfilefiname,"P_"),cpt,jj1);
5522: }
1.217 brouard 5523: if(backcast==1){
5524: /* Period (stable) back prevalence in each health state */
5525: for(cpt=1; cpt<=nlstate;cpt++){
5526: 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> \
5527: <img src=\"%s_%d-%d.svg\">", cpt, cpt, nlstate, subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1,subdirf2(optionfilefiname,"PB_"),cpt,jj1);
5528: }
5529: }
1.211 brouard 5530: if(prevfcast==1){
5531: /* Projection of prevalence up to period (stable) prevalence in each health state */
5532: for(cpt=1; cpt<=nlstate;cpt++){
1.214 brouard 5533: 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 5534: <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.211 brouard 5535: }
5536: }
5537:
1.126 brouard 5538: for(cpt=1; cpt<=nlstate;cpt++) {
1.205 brouard 5539: 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 5540: <img src=\"%s_%d%d.svg\">",cpt,nlstate,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1,subdirf2(optionfilefiname,"EXP_"),cpt,jj1);
1.126 brouard 5541: }
1.192 brouard 5542: /* } /\* end i1 *\/ */
1.126 brouard 5543: }/* End k1 */
5544: fprintf(fichtm,"</ul>");
5545:
5546: fprintf(fichtm,"\
5547: \n<br><li><h4> <a name='secondorder'>Result files (second order: variances)</a></h4>\n\
1.193 brouard 5548: - Parameter file with estimated parameters and covariance matrix: <a href=\"%s\">%s</a> <br> \
1.203 brouard 5549: - 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 5550: But because parameters are usually highly correlated (a higher incidence of disability \
5551: and a higher incidence of recovery can give very close observed transition) it might \
5552: be very useful to look not only at linear confidence intervals estimated from the \
5553: variances but at the covariance matrix. And instead of looking at the estimated coefficients \
5554: (parameters) of the logistic regression, it might be more meaningful to visualize the \
5555: covariance matrix of the one-step probabilities. \
5556: See page 'Matrix of variance-covariance of one-step probabilities' below. \n", rfileres,rfileres);
1.126 brouard 5557:
1.193 brouard 5558: fprintf(fichtm," - Standard deviation of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5559: subdirf2(fileresu,"PROB_"),subdirf2(fileresu,"PROB_"));
1.126 brouard 5560: fprintf(fichtm,"\
5561: - Variance-covariance of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5562: subdirf2(fileresu,"PROBCOV_"),subdirf2(fileresu,"PROBCOV_"));
1.126 brouard 5563:
5564: fprintf(fichtm,"\
5565: - Correlation matrix of one-step probabilities: <a href=\"%s\">%s</a> <br>\n",
1.201 brouard 5566: subdirf2(fileresu,"PROBCOR_"),subdirf2(fileresu,"PROBCOR_"));
1.126 brouard 5567: fprintf(fichtm,"\
5568: - 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): \
5569: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 5570: estepm,subdirf2(fileresu,"CVE_"),subdirf2(fileresu,"CVE_"));
1.126 brouard 5571: fprintf(fichtm,"\
5572: - (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): \
5573: <a href=\"%s\">%s</a> <br>\n</li>",
1.201 brouard 5574: estepm,subdirf2(fileresu,"STDE_"),subdirf2(fileresu,"STDE_"));
1.126 brouard 5575: fprintf(fichtm,"\
1.128 brouard 5576: - 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.201 brouard 5577: estepm, subdirf2(fileresu,"V_"),subdirf2(fileresu,"V_"));
1.126 brouard 5578: fprintf(fichtm,"\
1.128 brouard 5579: - 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.201 brouard 5580: estepm, subdirf2(fileresu,"T_"),subdirf2(fileresu,"T_"));
1.126 brouard 5581: fprintf(fichtm,"\
5582: - Standard deviation of period (stable) prevalences: <a href=\"%s\">%s</a> <br>\n",\
1.201 brouard 5583: subdirf2(fileresu,"VPL_"),subdirf2(fileresu,"VPL_"));
1.126 brouard 5584:
5585: /* if(popforecast==1) fprintf(fichtm,"\n */
5586: /* - Prevalences forecasting: <a href=\"f%s\">f%s</a> <br>\n */
5587: /* - Population forecasting (if popforecast=1): <a href=\"pop%s\">pop%s</a> <br>\n */
5588: /* <br>",fileres,fileres,fileres,fileres); */
5589: /* else */
5590: /* 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); */
5591: fflush(fichtm);
5592: fprintf(fichtm," <ul><li><b>Graphs</b></li><p>");
5593:
1.145 brouard 5594: m=pow(2,cptcoveff);
1.126 brouard 5595: if (cptcovn < 1) {m=1;ncodemax[1]=1;}
5596:
5597: jj1=0;
5598: for(k1=1; k1<=m;k1++){
1.192 brouard 5599: /* for(i1=1; i1<=ncodemax[k1];i1++){ */
1.126 brouard 5600: jj1++;
5601: if (cptcovn > 0) {
5602: fprintf(fichtm,"<hr size=\"2\" color=\"#EC5E5E\">************ Results for covariates");
5603: for (cpt=1; cpt<=cptcoveff;cpt++)
1.198 brouard 5604: fprintf(fichtm," V%d=%d ",Tvaraff[cpt],nbcode[Tvaraff[cpt]][codtabm(jj1,cpt)]);
1.126 brouard 5605: fprintf(fichtm," ************\n<hr size=\"2\" color=\"#EC5E5E\">");
5606: }
5607: for(cpt=1; cpt<=nlstate;cpt++) {
1.218 ! brouard 5608: fprintf(fichtm,"\n<br>- Observed (cross-sectional) and period (incidence based) \
! 5609: prevalence (with 95%% confidence interval) in state (%d): <a href=\"%s_%d-%d.svg\"> %s_%d-%d.svg</a>\n <br>\
1.205 brouard 5610: <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 5611: }
5612: fprintf(fichtm,"\n<br>- Total life expectancy by age and \
1.128 brouard 5613: health expectancies in states (1) and (2). If popbased=1 the smooth (due to the model) \
5614: true period expectancies (those weighted with period prevalences are also\
5615: drawn in addition to the population based expectancies computed using\
1.218 ! brouard 5616: observed and cahotic prevalences: <a href=\"%s_%d.svg\">%s_%d.svg</a>\n<br>\
1.205 brouard 5617: <img src=\"%s_%d.svg\">",subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1,subdirf2(optionfilefiname,"E_"),jj1);
1.192 brouard 5618: /* } /\* end i1 *\/ */
1.126 brouard 5619: }/* End k1 */
5620: fprintf(fichtm,"</ul>");
5621: fflush(fichtm);
5622: }
5623:
5624: /******************* Gnuplot file **************/
1.218 ! brouard 5625: void printinggnuplot(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , int prevfcast, int backcast, char pathc[], double p[]){
1.126 brouard 5626:
5627: char dirfileres[132],optfileres[132];
1.164 brouard 5628: int cpt=0,k1=0,i=0,k=0,j=0,jk=0,k2=0,k3=0,ij=0,l=0;
1.211 brouard 5629: int lv=0, vlv=0, kl=0;
1.130 brouard 5630: int ng=0;
1.201 brouard 5631: int vpopbased;
1.126 brouard 5632: /* if((ficgp=fopen(optionfilegnuplot,"a"))==NULL) { */
5633: /* printf("Problem with file %s",optionfilegnuplot); */
5634: /* fprintf(ficlog,"Problem with file %s",optionfilegnuplot); */
5635: /* } */
5636:
5637: /*#ifdef windows */
5638: fprintf(ficgp,"cd \"%s\" \n",pathc);
5639: /*#endif */
5640: m=pow(2,cptcoveff);
5641:
1.202 brouard 5642: /* Contribution to likelihood */
5643: /* Plot the probability implied in the likelihood */
5644: fprintf(ficgp,"\n# Contributions to the Likelihood, mle >=1. For mle=4 no interpolation, pure matrix products.\n#\n");
5645: fprintf(ficgp,"\n set log y; unset log x;set xlabel \"Age\"; set ylabel \"Likelihood (-2Log(L))\";");
5646: /* fprintf(ficgp,"\nset ter svg size 640, 480"); */ /* Too big for svg */
1.205 brouard 5647: fprintf(ficgp,"\nset ter pngcairo size 640, 480");
1.204 brouard 5648: /* nice for mle=4 plot by number of matrix products.
1.202 brouard 5649: replot "rrtest1/toto.txt" u 2:($4 == 1 && $5==2 ? $9 : 1/0):5 t "p12" with point lc 1 */
5650: /* replot exp(p1+p2*x)/(1+exp(p1+p2*x)+exp(p3+p4*x)+exp(p5+p6*x)) t "p12(x)" */
5651: /* fprintf(ficgp,"\nset out \"%s.svg\";",subdirf2(optionfilefiname,"ILK_")); */
1.204 brouard 5652: fprintf(ficgp,"\nset out \"%s-dest.png\";",subdirf2(optionfilefiname,"ILK_"));
1.214 brouard 5653: 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));
1.204 brouard 5654: fprintf(ficgp,"\nset out \"%s-ori.png\";",subdirf2(optionfilefiname,"ILK_"));
1.214 brouard 5655: 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));
1.204 brouard 5656: for (i=1; i<= nlstate ; i ++) {
5657: fprintf(ficgp,"\nset out \"%s-p%dj.png\";set ylabel \"Probability for each individual/wave\";",subdirf2(optionfilefiname,"ILK_"),i);
1.205 brouard 5658: fprintf(ficgp,"unset log;\n# plot weighted, mean weight should have point size of 0.5\n plot \"%s\"",subdirf(fileresilk));
1.214 brouard 5659: 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);
1.204 brouard 5660: for (j=2; j<= nlstate+ndeath ; j ++) {
1.214 brouard 5661: 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);
1.204 brouard 5662: }
5663: fprintf(ficgp,";\nset out; unset ylabel;\n");
5664: }
5665: /* 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 */
5666: /* fprintf(ficgp,"\nset log y;plot \"%s\" u 2:(-$11):3 t \"All sample, all transitions\" with dots lc variable",subdirf(fileresilk)); */
5667: /* fprintf(ficgp,"\nreplot \"%s\" u 2:($3 <= 3 ? -$11 : 1/0):3 t \"First 3 individuals\" with line lc variable", subdirf(fileresilk)); */
1.203 brouard 5668: fprintf(ficgp,"\nset out;unset log\n");
1.202 brouard 5669: /* fprintf(ficgp,"\nset out \"%s.svg\"; replot; set out; # bug gnuplot",subdirf2(optionfilefiname,"ILK_")); */
5670:
1.126 brouard 5671: strcpy(dirfileres,optionfilefiname);
5672: strcpy(optfileres,"vpl");
5673: /* 1eme*/
1.211 brouard 5674: for (cpt=1; cpt<= nlstate ; cpt ++) { /* For each live state */
5675: for (k1=1; k1<= m ; k1 ++) { /* For each combination of covariate */
5676: /* plot [100000000000000000000:-100000000000000000000] "mysbiaspar/vplrmysbiaspar.txt to check */
5677: fprintf(ficgp,"\n# 1st: Period (stable) prevalence with CI: 'VPL_' files ");
5678: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
5679: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
5680: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5681: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5682: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5683: vlv= nbcode[Tvaraff[lv]][lv];
5684: fprintf(ficgp," V%d=%d ",k,vlv);
5685: }
5686: fprintf(ficgp,"\n#\n");
5687:
1.201 brouard 5688: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"V_"),cpt,k1);
5689: fprintf(ficgp,"\n#set out \"V_%s_%d-%d.svg\" \n",optionfilefiname,cpt,k1);
1.126 brouard 5690: fprintf(ficgp,"set xlabel \"Age\" \n\
5691: set ylabel \"Probability\" \n\
1.199 brouard 5692: set ter svg size 640, 480\n\
1.201 brouard 5693: plot [%.f:%.f] \"%s\" every :::%d::%d u 1:2 \"%%lf",ageminpar,fage,subdirf2(fileresu,"VPL_"),k1-1,k1-1);
1.126 brouard 5694:
5695: for (i=1; i<= nlstate ; i ++) {
1.170 brouard 5696: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
5697: else fprintf(ficgp," %%*lf (%%*lf)");
1.126 brouard 5698: }
1.201 brouard 5699: 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);
1.126 brouard 5700: for (i=1; i<= nlstate ; i ++) {
1.170 brouard 5701: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
5702: else fprintf(ficgp," %%*lf (%%*lf)");
1.126 brouard 5703: }
1.201 brouard 5704: 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);
1.126 brouard 5705: for (i=1; i<= nlstate ; i ++) {
1.170 brouard 5706: if (i==cpt) fprintf(ficgp," %%lf (%%lf)");
5707: else fprintf(ficgp," %%*lf (%%*lf)");
1.126 brouard 5708: }
1.218 ! brouard 5709: 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));
! 5710: if(backcast==1){
! 5711: 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);
! 5712: }
1.201 brouard 5713: fprintf(ficgp,"\nset out \n");
5714: } /* k1 */
5715: } /* cpt */
1.126 brouard 5716: /*2 eme*/
5717: for (k1=1; k1<= m ; k1 ++) {
1.211 brouard 5718: fprintf(ficgp,"\n# 2nd: Total life expectancy with CI: 't' files ");
5719: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
5720: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
5721: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5722: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5723: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5724: vlv= nbcode[Tvaraff[lv]][lv];
5725: fprintf(ficgp," V%d=%d ",k,vlv);
5726: }
5727: fprintf(ficgp,"\n#\n");
5728:
1.201 brouard 5729: fprintf(ficgp,"\nset out \"%s_%d.svg\" \n",subdirf2(optionfilefiname,"E_"),k1);
5730: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
5731: if(vpopbased==0)
5732: fprintf(ficgp,"set ylabel \"Years\" \nset ter svg size 640, 480\nplot [%.f:%.f] ",ageminpar,fage);
5733: else
5734: fprintf(ficgp,"\nreplot ");
5735: for (i=1; i<= nlstate+1 ; i ++) {
5736: k=2*i;
5737: 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);
5738: for (j=1; j<= nlstate+1 ; j ++) {
5739: if (j==i) fprintf(ficgp," %%lf (%%lf)");
5740: else fprintf(ficgp," %%*lf (%%*lf)");
5741: }
5742: if (i== 1) fprintf(ficgp,"\" t\"TLE\" w l lt %d, \\\n",i);
5743: else fprintf(ficgp,"\" t\"LE in state (%d)\" w l lt %d, \\\n",i-1,i+1);
5744: 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);
5745: for (j=1; j<= nlstate+1 ; j ++) {
5746: if (j==i) fprintf(ficgp," %%lf (%%lf)");
5747: else fprintf(ficgp," %%*lf (%%*lf)");
5748: }
5749: fprintf(ficgp,"\" t\"\" w l lt 0,");
5750: 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);
5751: for (j=1; j<= nlstate+1 ; j ++) {
5752: if (j==i) fprintf(ficgp," %%lf (%%lf)");
5753: else fprintf(ficgp," %%*lf (%%*lf)");
5754: }
5755: if (i== (nlstate+1)) fprintf(ficgp,"\" t\"\" w l lt 0");
5756: else fprintf(ficgp,"\" t\"\" w l lt 0,\\\n");
5757: } /* state */
5758: } /* vpopbased */
5759: fprintf(ficgp,"\nset out;set out \"%s_%d.svg\"; replot; set out; \n",subdirf2(optionfilefiname,"E_"),k1); /* Buggy gnuplot */
5760: } /* k1 */
1.211 brouard 5761:
5762:
1.126 brouard 5763: /*3eme*/
5764: for (k1=1; k1<= m ; k1 ++) {
5765: for (cpt=1; cpt<= nlstate ; cpt ++) {
1.211 brouard 5766: fprintf(ficgp,"\n# 3d: Life expectancy with EXP_ files: cov=%d state=%d",k1, cpt);
5767: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
5768: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
5769: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5770: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5771: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5772: vlv= nbcode[Tvaraff[lv]][lv];
5773: fprintf(ficgp," V%d=%d ",k,vlv);
5774: }
5775: fprintf(ficgp,"\n#\n");
5776:
1.126 brouard 5777: /* k=2+nlstate*(2*cpt-2); */
5778: k=2+(nlstate+1)*(cpt-1);
1.201 brouard 5779: fprintf(ficgp,"\nset out \"%s_%d%d.svg\" \n",subdirf2(optionfilefiname,"EXP_"),cpt,k1);
1.199 brouard 5780: fprintf(ficgp,"set ter svg size 640, 480\n\
1.201 brouard 5781: 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 5782: /*fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d-2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
5783: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
5784: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
5785: fprintf(ficgp,",\"e%s\" every :::%d::%d u 1:($%d+2*$%d) \"\%%lf ",fileres,k1-1,k1-1,k,k+1);
5786: for (i=1; i<= nlstate*2 ; i ++) fprintf(ficgp,"\%%lf (\%%lf) ");
5787: fprintf(ficgp,"\" t \"e%d1\" w l",cpt);
5788:
5789: */
5790: for (i=1; i< nlstate ; i ++) {
1.201 brouard 5791: 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);
1.126 brouard 5792: /* 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);*/
5793:
5794: }
1.201 brouard 5795: 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 5796: }
5797: }
5798:
1.201 brouard 5799: /* Survival functions (period) from state i in state j by initial state i */
5800: for (k1=1; k1<= m ; k1 ++) { /* For each multivariate if any */
5801: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.211 brouard 5802: fprintf(ficgp,"\n#\n#\n# Survival functions in state j : 'LIJ_' files, cov=%d state=%d",k1, cpt);
5803: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
5804: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
5805: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5806: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5807: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5808: vlv= nbcode[Tvaraff[lv]][lv];
5809: fprintf(ficgp," V%d=%d ",k,vlv);
5810: }
5811: fprintf(ficgp,"\n#\n");
5812:
1.201 brouard 5813: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJ_"),cpt,k1);
5814: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
5815: set ter svg size 640, 480\n\
5816: unset log y\n\
5817: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 5818: k=3;
1.201 brouard 5819: for (i=1; i<= nlstate ; i ++){
1.217 brouard 5820: if(i==1){
1.201 brouard 5821: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
1.217 brouard 5822: }else{
1.201 brouard 5823: fprintf(ficgp,", '' ");
1.217 brouard 5824: }
1.201 brouard 5825: l=(nlstate+ndeath)*(i-1)+1;
5826: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
5827: for (j=2; j<= nlstate+ndeath ; j ++)
5828: fprintf(ficgp,"+$%d",k+l+j-1);
5829: fprintf(ficgp,")) t \"l(%d,%d)\" w l",i,cpt);
5830: } /* nlstate */
5831: fprintf(ficgp,"\nset out\n");
5832: } /* end cpt state*/
5833: } /* end covariate */
5834:
5835: /* Survival functions (period) from state i in state j by final state j */
1.202 brouard 5836: for (k1=1; k1<= m ; k1 ++) { /* For each covariate if any */
1.201 brouard 5837: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each inital state */
5838: 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.211 brouard 5839: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
5840: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
5841: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5842: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5843: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5844: vlv= nbcode[Tvaraff[lv]][lv];
5845: fprintf(ficgp," V%d=%d ",k,vlv);
5846: }
5847: fprintf(ficgp,"\n#\n");
5848:
1.201 brouard 5849: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"LIJT_"),cpt,k1);
5850: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability to be alive\" \n\
5851: set ter svg size 640, 480\n\
5852: unset log y\n\
5853: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 5854: k=3;
1.201 brouard 5855: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
5856: if(j==1)
5857: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
5858: else
5859: fprintf(ficgp,", '' ");
5860: l=(nlstate+ndeath)*(cpt-1) +j;
5861: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):($%d",k1,k+l);
5862: /* for (i=2; i<= nlstate+ndeath ; i ++) */
5863: /* fprintf(ficgp,"+$%d",k+l+i-1); */
5864: fprintf(ficgp,") t \"l(%d,%d)\" w l",cpt,j);
5865: } /* nlstate */
5866: fprintf(ficgp,", '' ");
5867: fprintf(ficgp," u (($1==%d && (floor($2)%%5 == 0)) ? ($3):1/0):(",k1);
5868: for (j=1; j<= nlstate ; j ++){ /* Lived in state j */
5869: l=(nlstate+ndeath)*(cpt-1) +j;
5870: if(j < nlstate)
5871: fprintf(ficgp,"$%d +",k+l);
5872: else
5873: fprintf(ficgp,"$%d) t\"l(%d,.)\" w l",k+l,cpt);
5874: }
5875: fprintf(ficgp,"\nset out\n");
5876: } /* end cpt state*/
5877: } /* end covariate */
5878:
1.202 brouard 5879: /* CV preval stable (period) for each covariate */
1.211 brouard 5880: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.153 brouard 5881: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
1.211 brouard 5882: fprintf(ficgp,"\n#\n#\n#CV preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
5883: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
5884: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
5885: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5886: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5887: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5888: vlv= nbcode[Tvaraff[lv]][lv];
5889: fprintf(ficgp," V%d=%d ",k,vlv);
5890: }
5891: fprintf(ficgp,"\n#\n");
5892:
1.201 brouard 5893: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"P_"),cpt,k1);
1.126 brouard 5894: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
1.199 brouard 5895: set ter svg size 640, 480\n\
1.126 brouard 5896: unset log y\n\
1.153 brouard 5897: plot [%.f:%.f] ", ageminpar, agemaxpar);
1.211 brouard 5898: k=3; /* Offset */
1.153 brouard 5899: for (i=1; i<= nlstate ; i ++){
5900: if(i==1)
1.201 brouard 5901: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJ_"));
1.153 brouard 5902: else
5903: fprintf(ficgp,", '' ");
1.154 brouard 5904: l=(nlstate+ndeath)*(i-1)+1;
5905: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l);
1.201 brouard 5906: for (j=2; j<= nlstate ; j ++)
5907: fprintf(ficgp,"+$%d",k+l+j-1);
1.153 brouard 5908: fprintf(ficgp,")) t \"prev(%d,%d)\" w l",i,cpt);
5909: } /* nlstate */
1.201 brouard 5910: fprintf(ficgp,"\nset out\n");
1.153 brouard 5911: } /* end cpt state*/
5912: } /* end covariate */
1.218 ! brouard 5913: if(backcast == 1){
1.217 brouard 5914: /* CV back preval stable (period) for each covariate */
1.218 ! brouard 5915: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
! 5916: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
! 5917: fprintf(ficgp,"\n#\n#\n#CV Back preval stable (period): 'pij' files, covariatecombination#=%d state=%d",k1, cpt);
! 5918: for (k=1; k<=cptcoveff; k++){ /* For each covariate and each value */
! 5919: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate number corresponding to k1 combination */
! 5920: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
! 5921: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
! 5922: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
! 5923: vlv= nbcode[Tvaraff[lv]][lv];
! 5924: fprintf(ficgp," V%d=%d ",k,vlv);
! 5925: }
! 5926: fprintf(ficgp,"\n#\n");
! 5927:
! 5928: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PB_"),cpt,k1);
! 5929: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Probability\" \n\
! 5930: set ter svg size 640, 480\n \
! 5931: unset log y\n \
! 5932: plot [%.f:%.f] ", ageminpar, agemaxpar);
! 5933: k=3; /* Offset */
! 5934: for (i=1; i<= nlstate ; i ++){
! 5935: if(i==1)
! 5936: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"PIJB_"));
! 5937: else
! 5938: fprintf(ficgp,", '' ");
! 5939: /* l=(nlstate+ndeath)*(i-1)+1; */
! 5940: l=(nlstate+ndeath)*(cpt-1)+1;
! 5941: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l); /\* a vérifier *\/ */
! 5942: /* fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d/($%d",k1,k+l+(cpt-1),k+l+(cpt-1)+i-1); /\* a vérifier *\/ */
! 5943: fprintf(ficgp," u ($1==%d ? ($3):1/0):($%d",k1,k+l+(cpt-1)+i-1); /* a vérifier */
! 5944: /* for (j=2; j<= nlstate ; j ++) */
! 5945: /* fprintf(ficgp,"+$%d",k+l+j-1); */
! 5946: /* /\* fprintf(ficgp,"+$%d",k+l+j-1); *\/ */
! 5947: fprintf(ficgp,") t \"bprev(%d,%d)\" w l",i,cpt);
! 5948: } /* nlstate */
! 5949: fprintf(ficgp,"\nset out\n");
! 5950: } /* end cpt state*/
! 5951: } /* end covariate */
! 5952: } /* End if backcast */
! 5953:
! 5954: if(prevfcast==1){
! 5955: /* Projection from cross-sectional to stable (period) for each covariate */
! 5956:
! 5957: for (k1=1; k1<= m ; k1 ++) { /* For each covariate combination (1 to m=2**k), if any covariate is present */
1.211 brouard 5958: for (cpt=1; cpt<=nlstate ; cpt ++) { /* For each life state */
5959: fprintf(ficgp,"\n#\n#\n#Projection of prevalence to stable (period): 'PROJ_' files, covariatecombination#=%d state=%d",k1, cpt);
5960: for (k=1; k<=cptcoveff; k++){ /* For each correspondig covariate value */
5961: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
5962: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
5963: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
5964: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
5965: vlv= nbcode[Tvaraff[lv]][lv];
5966: fprintf(ficgp," V%d=%d ",k,vlv);
5967: }
5968: fprintf(ficgp,"\n#\n");
5969:
5970: fprintf(ficgp,"# hpijx=probability over h years, hp.jx is weighted by observed prev\n ");
5971: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" \n",subdirf2(optionfilefiname,"PROJ_"),cpt,k1);
5972: fprintf(ficgp,"set xlabel \"Age\" \nset ylabel \"Prevalence\" \n\
5973: set ter svg size 640, 480\n\
5974: unset log y\n\
5975: plot [%.f:%.f] ", ageminpar, agemaxpar);
5976: for (i=1; i<= nlstate+1 ; i ++){ /* nlstate +1 p11 p21 p.1 */
5977: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
5978: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
5979: /*# yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
5980: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
5981: if(i==1){
5982: fprintf(ficgp,"\"%s\"",subdirf2(fileresu,"F_"));
5983: }else{
5984: fprintf(ficgp,",\\\n '' ");
5985: }
5986: if(cptcoveff ==0){ /* No covariate */
5987: fprintf(ficgp," u 2:("); /* Age is in 2 */
5988: /*# yearproj age p11 p21 p31 p.1 p12 p22 p32 p.2 p13 p23 p33 p.3 p14 p24 p34 p.4*/
5989: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 */
5990: if(i==nlstate+1)
5991: fprintf(ficgp," $%d/(1.-$%d)) t 'p.%d' with line ", \
5992: 2+(cpt-1)*(nlstate+1)+1+(i-1), 2+1+(i-1)+(nlstate+1)*nlstate,cpt );
5993: else
5994: fprintf(ficgp," $%d/(1.-$%d)) t 'p%d%d' with line ", \
5995: 2+(cpt-1)*(nlstate+1)+1+(i-1), 2+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
5996: }else{
5997: fprintf(ficgp,"u 6:(("); /* Age is in 6 */
5998: /*# V1 = 1 V2 = 0 yearproj age p11 p21 p.1 p12 p22 p.2 p13 p23 p.3*/
5999: /*# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
6000: kl=0;
6001: for (k=1; k<=cptcoveff; k++){ /* For each covariate */
6002: lv= decodtabm(k1,k,cptcoveff); /* Should be the covariate value corresponding to k1 combination and kth covariate */
6003: /* decodtabm(1,1,4) = 1 because h=1 k= (1) 1 1 1 */
6004: /* decodtabm(1,2,4) = 1 because h=1 k= 1 (1) 1 1 */
6005: /* decodtabm(13,3,4)= 2 because h=13 k= 1 1 (2) 2 */
6006: vlv= nbcode[Tvaraff[lv]][lv];
6007: kl++;
6008: /* 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 *\/ */
6009: /*6+(cpt-1)*(nlstate+1)+1+(i-1)+(nlstate+1)*nlstate; 6+(1-1)*(2+1)+1+(1-1) +(2+1)*2=13 */
6010: /*6+1+(i-1)+(nlstate+1)*nlstate; 6+1+(1-1) +(2+1)*2=13 */
6011: /* '' 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*/
6012: if(k==cptcoveff)
6013: if(i==nlstate+1)
6014: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d/(1.-$%d) : 1/0) t 'p.%d' with line ",kl, k,kl+1,nbcode[Tvaraff[lv]][lv], \
6015: 6+(cpt-1)*(nlstate+1)+1+(i-1), 6+1+(i-1)+(nlstate+1)*nlstate,cpt );
6016: else
6017: fprintf(ficgp,"$%d==%d && $%d==%d)? $%d/(1.-$%d) : 1/0) t 'p%d%d' with line ",kl, k,kl+1,nbcode[Tvaraff[lv]][lv], \
6018: 6+(cpt-1)*(nlstate+1)+1+(i-1), 6+1+(i-1)+(nlstate+1)*nlstate,i,cpt );
6019: else{
6020: fprintf(ficgp,"$%d==%d && $%d==%d && ",kl, k,kl+1,nbcode[Tvaraff[lv]][lv]);
6021: kl++;
6022: }
6023: } /* end covariate */
6024: } /* end if covariate */
6025: } /* nlstate */
6026: fprintf(ficgp,"\nset out\n");
6027: } /* end cpt state*/
6028: } /* end covariate */
6029: } /* End if prevfcast */
6030:
6031:
1.126 brouard 6032: /* proba elementaires */
1.187 brouard 6033: fprintf(ficgp,"\n##############\n#MLE estimated parameters\n#############\n");
1.126 brouard 6034: for(i=1,jk=1; i <=nlstate; i++){
1.187 brouard 6035: fprintf(ficgp,"# initial state %d\n",i);
1.126 brouard 6036: for(k=1; k <=(nlstate+ndeath); k++){
6037: if (k != i) {
1.187 brouard 6038: fprintf(ficgp,"# current state %d\n",k);
1.126 brouard 6039: for(j=1; j <=ncovmodel; j++){
1.187 brouard 6040: fprintf(ficgp,"p%d=%f; ",jk,p[jk]);
1.126 brouard 6041: jk++;
6042: }
1.187 brouard 6043: fprintf(ficgp,"\n");
1.126 brouard 6044: }
6045: }
6046: }
1.187 brouard 6047: fprintf(ficgp,"##############\n#\n");
6048:
1.145 brouard 6049: /*goto avoid;*/
1.200 brouard 6050: fprintf(ficgp,"\n##############\n#Graphics of probabilities or incidences\n#############\n");
1.187 brouard 6051: fprintf(ficgp,"# logi(p12/p11)=a12+b12*age+c12age*age+d12*V1+e12*V1*age\n");
6052: fprintf(ficgp,"# logi(p12/p11)=p1 +p2*age +p3*age*age+ p4*V1+ p5*V1*age\n");
6053: fprintf(ficgp,"# logi(p13/p11)=a13+b13*age+c13age*age+d13*V1+e13*V1*age\n");
6054: fprintf(ficgp,"# logi(p13/p11)=p6 +p7*age +p8*age*age+ p9*V1+ p10*V1*age\n");
6055: fprintf(ficgp,"# p12+p13+p14+p11=1=p11(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6056: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6057: fprintf(ficgp,"# p11=1/(1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6058: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age)+...)\n");
6059: fprintf(ficgp,"# p12=exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)/\n");
6060: fprintf(ficgp,"# (1+exp(a12+b12*age+c12age*age+d12*V1+e12*V1*age)\n");
6061: fprintf(ficgp,"# +exp(a13+b13*age+c13age*age+d13*V1+e13*V1*age))\n");
6062: fprintf(ficgp,"# +exp(a14+b14*age+c14age*age+d14*V1+e14*V1*age)+...)\n");
6063: fprintf(ficgp,"#\n");
1.201 brouard 6064: for(ng=1; ng<=3;ng++){ /* Number of graphics: first is logit, 2nd is probabilities, third is incidences per year*/
1.187 brouard 6065: fprintf(ficgp,"# ng=%d\n",ng);
6066: fprintf(ficgp,"# jk=1 to 2^%d=%d\n",cptcoveff,m);
1.126 brouard 6067: for(jk=1; jk <=m; jk++) {
1.187 brouard 6068: fprintf(ficgp,"# jk=%d\n",jk);
1.201 brouard 6069: fprintf(ficgp,"\nset out \"%s_%d-%d.svg\" ",subdirf2(optionfilefiname,"PE_"),jk,ng);
6070: fprintf(ficgp,"\nset ter svg size 640, 480 ");
6071: if (ng==1){
6072: fprintf(ficgp,"\nset ylabel \"Value of the logit of the model\"\n"); /* exp(a12+b12*x) could be nice */
6073: fprintf(ficgp,"\nunset log y");
6074: }else if (ng==2){
6075: fprintf(ficgp,"\nset ylabel \"Probability\"\n");
6076: fprintf(ficgp,"\nset log y");
6077: }else if (ng==3){
1.126 brouard 6078: fprintf(ficgp,"\nset ylabel \"Quasi-incidence per year\"\n");
1.201 brouard 6079: fprintf(ficgp,"\nset log y");
6080: }else
6081: fprintf(ficgp,"\nunset title ");
6082: fprintf(ficgp,"\nplot [%.f:%.f] ",ageminpar,agemaxpar);
1.126 brouard 6083: i=1;
6084: for(k2=1; k2<=nlstate; k2++) {
6085: k3=i;
6086: for(k=1; k<=(nlstate+ndeath); k++) {
6087: if (k != k2){
1.201 brouard 6088: switch( ng) {
6089: case 1:
1.187 brouard 6090: if(nagesqr==0)
1.201 brouard 6091: fprintf(ficgp," p%d+p%d*x",i,i+1);
1.187 brouard 6092: else /* nagesqr =1 */
1.201 brouard 6093: fprintf(ficgp," p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6094: break;
6095: case 2: /* ng=2 */
1.187 brouard 6096: if(nagesqr==0)
6097: fprintf(ficgp," exp(p%d+p%d*x",i,i+1);
6098: else /* nagesqr =1 */
1.201 brouard 6099: fprintf(ficgp," exp(p%d+p%d*x+p%d*x*x",i,i+1,i+1+nagesqr);
6100: break;
6101: case 3:
6102: if(nagesqr==0)
6103: fprintf(ficgp," %f*exp(p%d+p%d*x",YEARM/stepm,i,i+1);
6104: else /* nagesqr =1 */
6105: fprintf(ficgp," %f*exp(p%d+p%d*x+p%d*x*x",YEARM/stepm,i,i+1,i+1+nagesqr);
6106: break;
6107: }
1.141 brouard 6108: ij=1;/* To be checked else nbcode[0][0] wrong */
1.187 brouard 6109: for(j=3; j <=ncovmodel-nagesqr; j++) {
1.197 brouard 6110: /* printf("Tage[%d]=%d, j=%d\n", ij, Tage[ij], j); */
6111: if(ij <=cptcovage) { /* Bug valgrind */
6112: if((j-2)==Tage[ij]) { /* Bug valgrind */
1.200 brouard 6113: fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6114: /* fprintf(ficgp,"+p%d*%d*x",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
1.197 brouard 6115: ij++;
6116: }
1.186 brouard 6117: }
6118: else
1.198 brouard 6119: fprintf(ficgp,"+p%d*%d",i+j+nagesqr-1,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
1.126 brouard 6120: }
1.217 brouard 6121: }else{
6122: i=i-ncovmodel;
6123: if(ng !=1 ) /* For logit formula of log p11 is more difficult to get */
6124: fprintf(ficgp," (1.");
6125: }
6126:
6127: if(ng != 1){
6128: fprintf(ficgp,")/(1");
1.126 brouard 6129:
1.217 brouard 6130: for(k1=1; k1 <=nlstate; k1++){
6131: if(nagesqr==0)
6132: fprintf(ficgp,"+exp(p%d+p%d*x",k3+(k1-1)*ncovmodel,k3+(k1-1)*ncovmodel+1);
6133: else /* nagesqr =1 */
6134: 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);
6135:
6136: ij=1;
6137: for(j=3; j <=ncovmodel-nagesqr; j++){
6138: if(ij <=cptcovage) { /* Bug valgrind */
6139: if((j-2)==Tage[ij]) { /* Bug valgrind */
6140: fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
6141: /* fprintf(ficgp,"+p%d*%d*x",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,Tvar[j-2])]); */
6142: ij++;
1.197 brouard 6143: }
1.186 brouard 6144: }
1.217 brouard 6145: else
6146: fprintf(ficgp,"+p%d*%d",k3+(k1-1)*ncovmodel+1+j-2+nagesqr,nbcode[Tvar[j-2]][codtabm(jk,j-2)]);
1.126 brouard 6147: }
6148: fprintf(ficgp,")");
1.217 brouard 6149: }
6150: fprintf(ficgp,")");
6151: if(ng ==2)
6152: fprintf(ficgp," t \"p%d%d\" ", k2,k);
6153: else /* ng= 3 */
6154: fprintf(ficgp," t \"i%d%d\" ", k2,k);
6155: }else{ /* end ng <> 1 */
6156: if( k !=k2) /* logit p11 is hard to draw */
1.201 brouard 6157: fprintf(ficgp," t \"logit(p%d%d)\" ", k2,k);
1.126 brouard 6158: }
1.217 brouard 6159: if ((k+k2)!= (nlstate*2+ndeath) && ng != 1)
6160: fprintf(ficgp,",");
6161: if (ng == 1 && k!=k2 && (k+k2)!= (nlstate*2+ndeath))
6162: fprintf(ficgp,",");
6163: i=i+ncovmodel;
1.126 brouard 6164: } /* end k */
6165: } /* end k2 */
1.201 brouard 6166: fprintf(ficgp,"\n set out\n");
1.126 brouard 6167: } /* end jk */
6168: } /* end ng */
1.164 brouard 6169: /* avoid: */
1.126 brouard 6170: fflush(ficgp);
6171: } /* end gnuplot */
6172:
6173:
6174: /*************** Moving average **************/
1.218 ! brouard 6175: /* int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav, double bageout, double fageout){ */
! 6176: int movingaverage(double ***probs, double bage, double fage, double ***mobaverage, int mobilav){
! 6177:
1.126 brouard 6178: int i, cpt, cptcod;
6179: int modcovmax =1;
6180: int mobilavrange, mob;
6181: double age;
1.218 ! brouard 6182: int iage=0;
! 6183: double *sumnewp, *sumnewm;
! 6184: double *agemingood, *agemaxgood; /* Currently identical for all covariates */
! 6185:
! 6186: sumnewp = vector(1,modcovmax);
! 6187: sumnewm = vector(1,modcovmax);
! 6188: agemingood = vector(1,modcovmax);
! 6189: agemaxgood = vector(1,modcovmax);
! 6190:
! 6191:
1.126 brouard 6192: modcovmax=2*cptcoveff;/* Max number of modalities. We suppose
1.218 ! brouard 6193: a covariate has 2 modalities, should be equal to ncovcombmax */
1.126 brouard 6194: if (cptcovn<1) modcovmax=1; /* At least 1 pass */
1.218 ! brouard 6195:
1.126 brouard 6196: if(mobilav==1||mobilav ==3 ||mobilav==5 ||mobilav== 7){
6197: if(mobilav==1) mobilavrange=5; /* default */
6198: else mobilavrange=mobilav;
6199: for (age=bage; age<=fage; age++)
6200: for (i=1; i<=nlstate;i++)
6201: for (cptcod=1;cptcod<=modcovmax;cptcod++)
6202: mobaverage[(int)age][i][cptcod]=probs[(int)age][i][cptcod];
6203: /* We keep the original values on the extreme ages bage, fage and for
6204: fage+1 and bage-1 we use a 3 terms moving average; for fage+2 bage+2
6205: we use a 5 terms etc. until the borders are no more concerned.
6206: */
6207: for (mob=3;mob <=mobilavrange;mob=mob+2){
6208: for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){
6209: for (i=1; i<=nlstate;i++){
6210: for (cptcod=1;cptcod<=modcovmax;cptcod++){
6211: mobaverage[(int)age][i][cptcod] =probs[(int)age][i][cptcod];
1.218 ! brouard 6212: for (cpt=1;cpt<=(mob-1)/2;cpt++){
! 6213: mobaverage[(int)age][i][cptcod] +=probs[(int)age-cpt][i][cptcod];
! 6214: mobaverage[(int)age][i][cptcod] +=probs[(int)age+cpt][i][cptcod];
! 6215: }
1.126 brouard 6216: mobaverage[(int)age][i][cptcod]=mobaverage[(int)age][i][cptcod]/mob;
6217: }
6218: }
6219: }/* end age */
6220: }/* end mob */
1.218 ! brouard 6221: }else
! 6222: return -1;
! 6223: for (cptcod=1;cptcod<=modcovmax;cptcod++){
! 6224: /* for (age=bage+(mob-1)/2; age<=fage-(mob-1)/2; age++){ */
! 6225: agemingood[cptcod]=fage+(mob-1)/2;
! 6226: for (age=fage-(mob-1)/2; age>=bage; age--){/* From oldest to youngest, finding the youngest wrong */
! 6227: sumnewm[cptcod]=0.;
! 6228: for (i=1; i<=nlstate;i++){
! 6229: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
! 6230: }
! 6231: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
! 6232: agemingood[cptcod]=age;
! 6233: }else{ /* bad */
! 6234: for (i=1; i<=nlstate;i++){
! 6235: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
! 6236: } /* i */
! 6237: } /* end bad */
! 6238: }/* age */
! 6239: /* From youngest, finding the oldest wrong */
! 6240: agemaxgood[cptcod]=bage+(mob-1)/2;
! 6241: for (age=bage+(mob-1)/2; age<=fage; age++){
! 6242: sumnewm[cptcod]=0.;
! 6243: for (i=1; i<=nlstate;i++){
! 6244: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
! 6245: }
! 6246: if(fabs(sumnewm[cptcod] - 1.) <= 1.e-3) { /* good */
! 6247: agemaxgood[cptcod]=age;
! 6248: }else{ /* bad */
! 6249: for (i=1; i<=nlstate;i++){
! 6250: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
! 6251: } /* i */
! 6252: } /* end bad */
! 6253: }/* age */
! 6254: for (age=bage; age<=fage; age++){
! 6255: printf("%d %d ", cptcod, (int)age);
! 6256: sumnewp[cptcod]=0.;
! 6257: sumnewm[cptcod]=0.;
! 6258: for (i=1; i<=nlstate;i++){
! 6259: sumnewp[cptcod]+=probs[(int)age][i][cptcod];
! 6260: sumnewm[cptcod]+=mobaverage[(int)age][i][cptcod];
! 6261: printf("%.4f %.4f ",probs[(int)age][i][cptcod], mobaverage[(int)age][i][cptcod]);
! 6262: }
! 6263: printf("%.4f %.4f \n",sumnewp[cptcod], sumnewm[cptcod]);
! 6264: }
! 6265: printf("\n");
! 6266: /* brutal averaging */
! 6267: for (i=1; i<=nlstate;i++){
! 6268: for (age=1; age<=bage; age++){
! 6269: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemingood[cptcod]][i][cptcod];
! 6270: printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]);
! 6271: }
! 6272: for (age=fage; age<=AGESUP; age++){
! 6273: mobaverage[(int)age][i][cptcod]=mobaverage[(int)agemaxgood[cptcod]][i][cptcod];
! 6274: printf("age=%d i=%d cptcod=%d mobaverage=%.4f \n",(int)age,i, cptcod, mobaverage[(int)age][i][cptcod]);
! 6275: }
! 6276: } /* end i status */
! 6277: for (i=nlstate+1; i<=nlstate+ndeath;i++){
! 6278: for (age=1; age<=AGESUP; age++){
! 6279: /*printf("i=%d, age=%d, cptcod=%d\n",i, (int)age, cptcod);*/
! 6280: mobaverage[(int)age][i][cptcod]=0.;
! 6281: }
! 6282: }
! 6283: }/* end cptcod */
! 6284: free_vector(sumnewm,1, modcovmax);
! 6285: free_vector(sumnewp,1, modcovmax);
! 6286: free_vector(agemaxgood,1, modcovmax);
! 6287: free_vector(agemingood,1, modcovmax);
1.126 brouard 6288: return 0;
6289: }/* End movingaverage */
1.218 ! brouard 6290:
1.126 brouard 6291:
6292: /************** Forecasting ******************/
1.169 brouard 6293: 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 6294: /* proj1, year, month, day of starting projection
6295: agemin, agemax range of age
6296: dateprev1 dateprev2 range of dates during which prevalence is computed
6297: anproj2 year of en of projection (same day and month as proj1).
6298: */
1.164 brouard 6299: int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1;
1.126 brouard 6300: double agec; /* generic age */
6301: double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean;
6302: double *popeffectif,*popcount;
6303: double ***p3mat;
1.218 ! brouard 6304: /* double ***mobaverage; */
1.126 brouard 6305: char fileresf[FILENAMELENGTH];
6306:
6307: agelim=AGESUP;
1.211 brouard 6308: /* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people
6309: in each health status at the date of interview (if between dateprev1 and dateprev2).
6310: We still use firstpass and lastpass as another selection.
6311: */
1.214 brouard 6312: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ */
6313: /* firstpass, lastpass, stepm, weightopt, model); */
1.126 brouard 6314:
1.201 brouard 6315: strcpy(fileresf,"F_");
6316: strcat(fileresf,fileresu);
1.126 brouard 6317: if((ficresf=fopen(fileresf,"w"))==NULL) {
6318: printf("Problem with forecast resultfile: %s\n", fileresf);
6319: fprintf(ficlog,"Problem with forecast resultfile: %s\n", fileresf);
6320: }
1.215 brouard 6321: printf("Computing forecasting: result on file '%s', please wait... \n", fileresf);
6322: fprintf(ficlog,"Computing forecasting: result on file '%s', please wait... \n", fileresf);
1.126 brouard 6323:
6324: if (cptcoveff==0) ncodemax[cptcoveff]=1;
6325:
6326:
6327: stepsize=(int) (stepm+YEARM-1)/YEARM;
6328: if (stepm<=12) stepsize=1;
6329: if(estepm < stepm){
6330: printf ("Problem %d lower than %d\n",estepm, stepm);
6331: }
6332: else hstepm=estepm;
6333:
6334: hstepm=hstepm/stepm;
6335: yp1=modf(dateintmean,&yp);/* extracts integral of datemean in yp and
6336: fractional in yp1 */
6337: anprojmean=yp;
6338: yp2=modf((yp1*12),&yp);
6339: mprojmean=yp;
6340: yp1=modf((yp2*30.5),&yp);
6341: jprojmean=yp;
6342: if(jprojmean==0) jprojmean=1;
6343: if(mprojmean==0) jprojmean=1;
6344:
6345: i1=cptcoveff;
6346: if (cptcovn < 1){i1=1;}
6347:
6348: fprintf(ficresf,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2);
6349:
6350: fprintf(ficresf,"#****** Routine prevforecast **\n");
6351:
6352: /* if (h==(int)(YEARM*yearp)){ */
6353: for(cptcov=1, k=0;cptcov<=i1;cptcov++){
6354: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
6355: k=k+1;
1.211 brouard 6356: fprintf(ficresf,"\n#****** hpijx=probability over h years, hp.jx is weighted by observed prev \n#");
1.126 brouard 6357: for(j=1;j<=cptcoveff;j++) {
1.211 brouard 6358: fprintf(ficresf," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.126 brouard 6359: }
1.211 brouard 6360: fprintf(ficresf," yearproj age");
1.126 brouard 6361: for(j=1; j<=nlstate+ndeath;j++){
6362: for(i=1; i<=nlstate;i++)
6363: fprintf(ficresf," p%d%d",i,j);
6364: fprintf(ficresf," p.%d",j);
6365: }
1.217 brouard 6366: for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) {
1.126 brouard 6367: fprintf(ficresf,"\n");
6368: fprintf(ficresf,"\n# Forecasting at date %.lf/%.lf/%.lf ",jproj1,mproj1,anproj1+yearp);
6369: for (agec=fage; agec>=(ageminpar-1); agec--){
6370: nhstepm=(int) rint((agelim-agec)*YEARM/stepm);
6371: nhstepm = nhstepm/hstepm;
6372: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6373: oldm=oldms;savm=savms;
1.217 brouard 6374: hpxij(p3mat,nhstepm,agec,hstepm,p,nlstate,stepm,oldm,savm, k);
1.126 brouard 6375:
6376: for (h=0; h<=nhstepm; h++){
6377: if (h*hstepm/YEARM*stepm ==yearp) {
6378: fprintf(ficresf,"\n");
6379: for(j=1;j<=cptcoveff;j++)
1.198 brouard 6380: fprintf(ficresf,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.126 brouard 6381: fprintf(ficresf,"%.f %.f ",anproj1+yearp,agec+h*hstepm/YEARM*stepm);
6382: }
6383: for(j=1; j<=nlstate+ndeath;j++) {
6384: ppij=0.;
6385: for(i=1; i<=nlstate;i++) {
6386: if (mobilav==1)
6387: ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod];
6388: else {
6389: ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod];
6390: }
6391: if (h*hstepm/YEARM*stepm== yearp) {
6392: fprintf(ficresf," %.3f", p3mat[i][j][h]);
6393: }
6394: } /* end i */
6395: if (h*hstepm/YEARM*stepm==yearp) {
6396: fprintf(ficresf," %.3f", ppij);
6397: }
6398: }/* end j */
6399: } /* end h */
6400: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6401: } /* end agec */
6402: } /* end yearp */
6403: } /* end cptcod */
6404: } /* end cptcov */
6405:
6406: fclose(ficresf);
1.215 brouard 6407: printf("End of Computing forecasting \n");
6408: fprintf(ficlog,"End of Computing forecasting\n");
6409:
1.126 brouard 6410: }
6411:
1.218 ! brouard 6412: /* /\************** Back Forecasting ******************\/ */
! 6413: /* 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){ */
! 6414: /* /\* back1, year, month, day of starting backection */
! 6415: /* agemin, agemax range of age */
! 6416: /* dateprev1 dateprev2 range of dates during which prevalence is computed */
! 6417: /* anback2 year of en of backection (same day and month as back1). */
! 6418: /* *\/ */
! 6419: /* int yearp, stepsize, hstepm, nhstepm, j, k, cptcod, i, h, i1; */
! 6420: /* double agec; /\* generic age *\/ */
! 6421: /* double agelim, ppij, yp,yp1,yp2,jprojmean,mprojmean,anprojmean; */
! 6422: /* double *popeffectif,*popcount; */
! 6423: /* double ***p3mat; */
! 6424: /* /\* double ***mobaverage; *\/ */
! 6425: /* char fileresfb[FILENAMELENGTH]; */
! 6426:
! 6427: /* agelim=AGESUP; */
! 6428: /* /\* Compute observed prevalence between dateprev1 and dateprev2 by counting the number of people */
! 6429: /* in each health status at the date of interview (if between dateprev1 and dateprev2). */
! 6430: /* We still use firstpass and lastpass as another selection. */
! 6431: /* *\/ */
! 6432: /* /\* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\ *\/ */
! 6433: /* /\* firstpass, lastpass, stepm, weightopt, model); *\/ */
! 6434: /* prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
! 6435:
! 6436: /* strcpy(fileresfb,"FB_"); */
! 6437: /* strcat(fileresfb,fileresu); */
! 6438: /* if((ficresfb=fopen(fileresfb,"w"))==NULL) { */
! 6439: /* printf("Problem with back forecast resultfile: %s\n", fileresfb); */
! 6440: /* fprintf(ficlog,"Problem with back forecast resultfile: %s\n", fileresfb); */
! 6441: /* } */
! 6442: /* printf("Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
! 6443: /* fprintf(ficlog,"Computing back forecasting: result on file '%s', please wait... \n", fileresfb); */
! 6444:
! 6445: /* if (cptcoveff==0) ncodemax[cptcoveff]=1; */
! 6446:
! 6447: /* /\* if (mobilav!=0) { *\/ */
! 6448: /* /\* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
! 6449: /* /\* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ *\/ */
! 6450: /* /\* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); *\/ */
! 6451: /* /\* printf(" Error in movingaverage mobilav=%d\n",mobilav); *\/ */
! 6452: /* /\* } *\/ */
! 6453: /* /\* } *\/ */
! 6454:
! 6455: /* stepsize=(int) (stepm+YEARM-1)/YEARM; */
! 6456: /* if (stepm<=12) stepsize=1; */
! 6457: /* if(estepm < stepm){ */
! 6458: /* printf ("Problem %d lower than %d\n",estepm, stepm); */
! 6459: /* } */
! 6460: /* else hstepm=estepm; */
! 6461:
! 6462: /* hstepm=hstepm/stepm; */
! 6463: /* yp1=modf(dateintmean,&yp);/\* extracts integral of datemean in yp and */
! 6464: /* fractional in yp1 *\/ */
! 6465: /* anprojmean=yp; */
! 6466: /* yp2=modf((yp1*12),&yp); */
! 6467: /* mprojmean=yp; */
! 6468: /* yp1=modf((yp2*30.5),&yp); */
! 6469: /* jprojmean=yp; */
! 6470: /* if(jprojmean==0) jprojmean=1; */
! 6471: /* if(mprojmean==0) jprojmean=1; */
! 6472:
! 6473: /* i1=cptcoveff; */
! 6474: /* if (cptcovn < 1){i1=1;} */
1.217 brouard 6475:
1.218 ! brouard 6476: /* fprintf(ficresfb,"# Mean day of interviews %.lf/%.lf/%.lf (%.2f) between %.2f and %.2f \n",jprojmean,mprojmean,anprojmean,dateintmean,dateprev1,dateprev2); */
1.217 brouard 6477:
1.218 ! brouard 6478: /* fprintf(ficresfb,"#****** Routine prevbackforecast **\n"); */
! 6479:
! 6480: /* /\* if (h==(int)(YEARM*yearp)){ *\/ */
! 6481: /* for(cptcov=1, k=0;cptcov<=i1;cptcov++){ */
! 6482: /* for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){ */
! 6483: /* k=k+1; */
! 6484: /* fprintf(ficresfb,"\n#****** hbijx=probability over h years, hp.jx is weighted by observed prev \n#"); */
! 6485: /* for(j=1;j<=cptcoveff;j++) { */
! 6486: /* fprintf(ficresfb," V%d (=) %d",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
! 6487: /* } */
! 6488: /* fprintf(ficresfb," yearbproj age"); */
! 6489: /* for(j=1; j<=nlstate+ndeath;j++){ */
! 6490: /* for(i=1; i<=nlstate;i++) */
! 6491: /* fprintf(ficresfb," p%d%d",i,j); */
! 6492: /* fprintf(ficresfb," p.%d",j); */
! 6493: /* } */
! 6494: /* for (yearp=0; yearp>=(anback2-anback1);yearp -=stepsize) { */
! 6495: /* /\* for (yearp=0; yearp<=(anproj2-anproj1);yearp +=stepsize) { *\/ */
! 6496: /* fprintf(ficresfb,"\n"); */
! 6497: /* fprintf(ficresfb,"\n# Back Forecasting at date %.lf/%.lf/%.lf ",jback1,mback1,anback1+yearp); */
! 6498: /* for (agec=fage; agec>=(ageminpar-1); agec--){ */
! 6499: /* nhstepm=(int) rint((agelim-agec)*YEARM/stepm); */
! 6500: /* nhstepm = nhstepm/hstepm; */
! 6501: /* p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
! 6502: /* oldm=oldms;savm=savms; */
! 6503: /* hbxij(p3mat,nhstepm,agec,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm,oldm,savm, dnewm, doldm, dsavm, k); */
! 6504: /* for (h=0; h<=nhstepm; h++){ */
! 6505: /* if (h*hstepm/YEARM*stepm ==yearp) { */
! 6506: /* fprintf(ficresfb,"\n"); */
! 6507: /* for(j=1;j<=cptcoveff;j++) */
! 6508: /* fprintf(ficresfb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */
! 6509: /* fprintf(ficresfb,"%.f %.f ",anback1+yearp,agec+h*hstepm/YEARM*stepm); */
! 6510: /* } */
! 6511: /* for(j=1; j<=nlstate+ndeath;j++) { */
! 6512: /* ppij=0.; */
! 6513: /* for(i=1; i<=nlstate;i++) { */
! 6514: /* if (mobilav==1) */
! 6515: /* ppij=ppij+p3mat[i][j][h]*mobaverage[(int)agec][i][cptcod]; */
! 6516: /* else { */
! 6517: /* ppij=ppij+p3mat[i][j][h]*probs[(int)(agec)][i][cptcod]; */
! 6518: /* } */
! 6519: /* if (h*hstepm/YEARM*stepm== yearp) { */
! 6520: /* fprintf(ficresfb," %.3f", p3mat[i][j][h]); */
! 6521: /* } */
! 6522: /* } /\* end i *\/ */
! 6523: /* if (h*hstepm/YEARM*stepm==yearp) { */
! 6524: /* fprintf(ficresfb," %.3f", ppij); */
! 6525: /* } */
! 6526: /* }/\* end j *\/ */
! 6527: /* } /\* end h *\/ */
! 6528: /* free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm); */
! 6529: /* } /\* end agec *\/ */
! 6530: /* } /\* end yearp *\/ */
! 6531: /* } /\* end cptcod *\/ */
! 6532: /* } /\* end cptcov *\/ */
! 6533:
! 6534: /* /\* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); *\/ */
! 6535:
! 6536: /* fclose(ficresfb); */
! 6537: /* printf("End of Computing Back forecasting \n"); */
! 6538: /* fprintf(ficlog,"End of Computing Back forecasting\n"); */
1.217 brouard 6539:
1.218 ! brouard 6540: /* } */
1.217 brouard 6541:
1.126 brouard 6542: /************** Forecasting *****not tested NB*************/
1.169 brouard 6543: 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 6544:
6545: int cpt, stepsize, hstepm, nhstepm, j,k,c, cptcod, i,h;
6546: int *popage;
6547: double calagedatem, agelim, kk1, kk2;
6548: double *popeffectif,*popcount;
6549: double ***p3mat,***tabpop,***tabpopprev;
1.218 ! brouard 6550: /* double ***mobaverage; */
1.126 brouard 6551: char filerespop[FILENAMELENGTH];
6552:
6553: tabpop= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6554: tabpopprev= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6555: agelim=AGESUP;
6556: calagedatem=(anpyram+mpyram/12.+jpyram/365.-dateintmean)*YEARM;
6557:
6558: prevalence(probs, ageminpar, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
6559:
6560:
1.201 brouard 6561: strcpy(filerespop,"POP_");
6562: strcat(filerespop,fileresu);
1.126 brouard 6563: if((ficrespop=fopen(filerespop,"w"))==NULL) {
6564: printf("Problem with forecast resultfile: %s\n", filerespop);
6565: fprintf(ficlog,"Problem with forecast resultfile: %s\n", filerespop);
6566: }
6567: printf("Computing forecasting: result on file '%s' \n", filerespop);
6568: fprintf(ficlog,"Computing forecasting: result on file '%s' \n", filerespop);
6569:
6570: if (cptcoveff==0) ncodemax[cptcoveff]=1;
6571:
1.218 ! brouard 6572: /* if (mobilav!=0) { */
! 6573: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
! 6574: /* if (movingaverage(probs, ageminpar, fage, mobaverage,mobilav)!=0){ */
! 6575: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
! 6576: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
! 6577: /* } */
! 6578: /* } */
1.126 brouard 6579:
6580: stepsize=(int) (stepm+YEARM-1)/YEARM;
6581: if (stepm<=12) stepsize=1;
6582:
6583: agelim=AGESUP;
6584:
6585: hstepm=1;
6586: hstepm=hstepm/stepm;
1.218 ! brouard 6587:
1.126 brouard 6588: if (popforecast==1) {
6589: if((ficpop=fopen(popfile,"r"))==NULL) {
6590: printf("Problem with population file : %s\n",popfile);exit(0);
6591: fprintf(ficlog,"Problem with population file : %s\n",popfile);exit(0);
6592: }
6593: popage=ivector(0,AGESUP);
6594: popeffectif=vector(0,AGESUP);
6595: popcount=vector(0,AGESUP);
6596:
6597: i=1;
6598: while ((c=fscanf(ficpop,"%d %lf\n",&popage[i],&popcount[i])) != EOF) i=i+1;
1.218 ! brouard 6599:
1.126 brouard 6600: imx=i;
6601: for (i=1; i<imx;i++) popeffectif[popage[i]]=popcount[i];
6602: }
1.218 ! brouard 6603:
1.126 brouard 6604: for(cptcov=1,k=0;cptcov<=i2;cptcov++){
1.218 ! brouard 6605: for(cptcod=1;cptcod<=ncodemax[cptcoveff];cptcod++){
1.126 brouard 6606: k=k+1;
6607: fprintf(ficrespop,"\n#******");
6608: for(j=1;j<=cptcoveff;j++) {
1.198 brouard 6609: fprintf(ficrespop," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.126 brouard 6610: }
6611: fprintf(ficrespop,"******\n");
6612: fprintf(ficrespop,"# Age");
6613: for(j=1; j<=nlstate+ndeath;j++) fprintf(ficrespop," P.%d",j);
6614: if (popforecast==1) fprintf(ficrespop," [Population]");
6615:
6616: for (cpt=0; cpt<=0;cpt++) {
6617: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
6618:
1.218 ! brouard 6619: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
1.126 brouard 6620: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
6621: nhstepm = nhstepm/hstepm;
6622:
6623: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6624: oldm=oldms;savm=savms;
6625: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
1.218 ! brouard 6626:
1.126 brouard 6627: for (h=0; h<=nhstepm; h++){
6628: if (h==(int) (calagedatem+YEARM*cpt)) {
6629: fprintf(ficrespop,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
6630: }
6631: for(j=1; j<=nlstate+ndeath;j++) {
6632: kk1=0.;kk2=0;
6633: for(i=1; i<=nlstate;i++) {
6634: if (mobilav==1)
6635: kk1=kk1+p3mat[i][j][h]*mobaverage[(int)agedeb+1][i][cptcod];
6636: else {
6637: kk1=kk1+p3mat[i][j][h]*probs[(int)(agedeb+1)][i][cptcod];
6638: }
6639: }
6640: if (h==(int)(calagedatem+12*cpt)){
6641: tabpop[(int)(agedeb)][j][cptcod]=kk1;
1.218 ! brouard 6642: /*fprintf(ficrespop," %.3f", kk1);
! 6643: if (popforecast==1) fprintf(ficrespop," [%.f]", kk1*popeffectif[(int)agedeb+1]);*/
1.126 brouard 6644: }
6645: }
6646: for(i=1; i<=nlstate;i++){
6647: kk1=0.;
1.218 ! brouard 6648: for(j=1; j<=nlstate;j++){
! 6649: kk1= kk1+tabpop[(int)(agedeb)][j][cptcod];
! 6650: }
! 6651: tabpopprev[(int)(agedeb)][i][cptcod]=tabpop[(int)(agedeb)][i][cptcod]/kk1*popeffectif[(int)(agedeb+(calagedatem+12*cpt)*hstepm/YEARM*stepm-1)];
1.126 brouard 6652: }
1.218 ! brouard 6653:
! 6654: if (h==(int)(calagedatem+12*cpt))
! 6655: for(j=1; j<=nlstate;j++)
! 6656: fprintf(ficrespop," %15.2f",tabpopprev[(int)(agedeb+1)][j][cptcod]);
1.126 brouard 6657: }
6658: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6659: }
6660: }
1.218 ! brouard 6661:
! 6662: /******/
! 6663:
1.126 brouard 6664: for (cpt=1; cpt<=(anpyram1-anpyram);cpt++) {
6665: fprintf(ficrespop,"\n\n# Forecasting at date %.lf/%.lf/%.lf ",jpyram,mpyram,anpyram+cpt);
6666: for (agedeb=(fage-((int)calagedatem %12/12.)); agedeb>=(ageminpar-((int)calagedatem %12)/12.); agedeb--){
6667: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm);
6668: nhstepm = nhstepm/hstepm;
6669:
6670: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6671: oldm=oldms;savm=savms;
6672: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
6673: for (h=0; h<=nhstepm; h++){
6674: if (h==(int) (calagedatem+YEARM*cpt)) {
6675: fprintf(ficresf,"\n %3.f ",agedeb+h*hstepm/YEARM*stepm);
6676: }
6677: for(j=1; j<=nlstate+ndeath;j++) {
6678: kk1=0.;kk2=0;
6679: for(i=1; i<=nlstate;i++) {
6680: kk1=kk1+p3mat[i][j][h]*tabpopprev[(int)agedeb+1][i][cptcod];
6681: }
6682: if (h==(int)(calagedatem+12*cpt)) fprintf(ficresf," %15.2f", kk1);
6683: }
6684: }
6685: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
6686: }
6687: }
1.218 ! brouard 6688: }
1.126 brouard 6689: }
1.218 ! brouard 6690:
! 6691: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
! 6692:
1.126 brouard 6693: if (popforecast==1) {
6694: free_ivector(popage,0,AGESUP);
6695: free_vector(popeffectif,0,AGESUP);
6696: free_vector(popcount,0,AGESUP);
6697: }
6698: free_ma3x(tabpop,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6699: free_ma3x(tabpopprev,1, AGESUP,1,NCOVMAX, 1,NCOVMAX);
6700: fclose(ficrespop);
6701: } /* End of popforecast */
1.218 ! brouard 6702:
1.126 brouard 6703: int fileappend(FILE *fichier, char *optionfich)
6704: {
6705: if((fichier=fopen(optionfich,"a"))==NULL) {
6706: printf("Problem with file: %s\n", optionfich);
6707: fprintf(ficlog,"Problem with file: %s\n", optionfich);
6708: return (0);
6709: }
6710: fflush(fichier);
6711: return (1);
6712: }
6713:
6714:
6715: /**************** function prwizard **********************/
6716: void prwizard(int ncovmodel, int nlstate, int ndeath, char model[], FILE *ficparo)
6717: {
6718:
6719: /* Wizard to print covariance matrix template */
6720:
1.164 brouard 6721: char ca[32], cb[32];
6722: int i,j, k, li, lj, lk, ll, jj, npar, itimes;
1.126 brouard 6723: int numlinepar;
6724:
6725: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6726: fprintf(ficparo,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
6727: for(i=1; i <=nlstate; i++){
6728: jj=0;
6729: for(j=1; j <=nlstate+ndeath; j++){
6730: if(j==i) continue;
6731: jj++;
6732: /*ca[0]= k+'a'-1;ca[1]='\0';*/
6733: printf("%1d%1d",i,j);
6734: fprintf(ficparo,"%1d%1d",i,j);
6735: for(k=1; k<=ncovmodel;k++){
6736: /* printf(" %lf",param[i][j][k]); */
6737: /* fprintf(ficparo," %lf",param[i][j][k]); */
6738: printf(" 0.");
6739: fprintf(ficparo," 0.");
6740: }
6741: printf("\n");
6742: fprintf(ficparo,"\n");
6743: }
6744: }
6745: printf("# Scales (for hessian or gradient estimation)\n");
6746: fprintf(ficparo,"# Scales (for hessian or gradient estimation)\n");
6747: npar= (nlstate+ndeath-1)*nlstate*ncovmodel; /* Number of parameters*/
6748: for(i=1; i <=nlstate; i++){
6749: jj=0;
6750: for(j=1; j <=nlstate+ndeath; j++){
6751: if(j==i) continue;
6752: jj++;
6753: fprintf(ficparo,"%1d%1d",i,j);
6754: printf("%1d%1d",i,j);
6755: fflush(stdout);
6756: for(k=1; k<=ncovmodel;k++){
6757: /* printf(" %le",delti3[i][j][k]); */
6758: /* fprintf(ficparo," %le",delti3[i][j][k]); */
6759: printf(" 0.");
6760: fprintf(ficparo," 0.");
6761: }
6762: numlinepar++;
6763: printf("\n");
6764: fprintf(ficparo,"\n");
6765: }
6766: }
6767: printf("# Covariance matrix\n");
6768: /* # 121 Var(a12)\n\ */
6769: /* # 122 Cov(b12,a12) Var(b12)\n\ */
6770: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
6771: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
6772: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
6773: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
6774: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
6775: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
6776: fflush(stdout);
6777: fprintf(ficparo,"# Covariance matrix\n");
6778: /* # 121 Var(a12)\n\ */
6779: /* # 122 Cov(b12,a12) Var(b12)\n\ */
6780: /* # ...\n\ */
6781: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
6782:
6783: for(itimes=1;itimes<=2;itimes++){
6784: jj=0;
6785: for(i=1; i <=nlstate; i++){
6786: for(j=1; j <=nlstate+ndeath; j++){
6787: if(j==i) continue;
6788: for(k=1; k<=ncovmodel;k++){
6789: jj++;
6790: ca[0]= k+'a'-1;ca[1]='\0';
6791: if(itimes==1){
6792: printf("#%1d%1d%d",i,j,k);
6793: fprintf(ficparo,"#%1d%1d%d",i,j,k);
6794: }else{
6795: printf("%1d%1d%d",i,j,k);
6796: fprintf(ficparo,"%1d%1d%d",i,j,k);
6797: /* printf(" %.5le",matcov[i][j]); */
6798: }
6799: ll=0;
6800: for(li=1;li <=nlstate; li++){
6801: for(lj=1;lj <=nlstate+ndeath; lj++){
6802: if(lj==li) continue;
6803: for(lk=1;lk<=ncovmodel;lk++){
6804: ll++;
6805: if(ll<=jj){
6806: cb[0]= lk +'a'-1;cb[1]='\0';
6807: if(ll<jj){
6808: if(itimes==1){
6809: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6810: fprintf(ficparo," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
6811: }else{
6812: printf(" 0.");
6813: fprintf(ficparo," 0.");
6814: }
6815: }else{
6816: if(itimes==1){
6817: printf(" Var(%s%1d%1d)",ca,i,j);
6818: fprintf(ficparo," Var(%s%1d%1d)",ca,i,j);
6819: }else{
6820: printf(" 0.");
6821: fprintf(ficparo," 0.");
6822: }
6823: }
6824: }
6825: } /* end lk */
6826: } /* end lj */
6827: } /* end li */
6828: printf("\n");
6829: fprintf(ficparo,"\n");
6830: numlinepar++;
6831: } /* end k*/
6832: } /*end j */
6833: } /* end i */
6834: } /* end itimes */
6835:
6836: } /* end of prwizard */
6837: /******************* Gompertz Likelihood ******************************/
6838: double gompertz(double x[])
6839: {
6840: double A,B,L=0.0,sump=0.,num=0.;
6841: int i,n=0; /* n is the size of the sample */
6842:
6843: for (i=0;i<=imx-1 ; i++) {
6844: sump=sump+weight[i];
6845: /* sump=sump+1;*/
6846: num=num+1;
6847: }
6848:
6849:
6850: /* for (i=0; i<=imx; i++)
6851: 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]);*/
6852:
6853: for (i=1;i<=imx ; i++)
6854: {
6855: if (cens[i] == 1 && wav[i]>1)
6856: A=-x[1]/(x[2])*(exp(x[2]*(agecens[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)));
6857:
6858: if (cens[i] == 0 && wav[i]>1)
6859: A=-x[1]/(x[2])*(exp(x[2]*(agedc[i]-agegomp))-exp(x[2]*(ageexmed[i]-agegomp)))
6860: +log(x[1]/YEARM)+x[2]*(agedc[i]-agegomp)+log(YEARM);
6861:
6862: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
6863: if (wav[i] > 1 ) { /* ??? */
6864: L=L+A*weight[i];
6865: /* 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]);*/
6866: }
6867: }
6868:
6869: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
6870:
6871: return -2*L*num/sump;
6872: }
6873:
1.136 brouard 6874: #ifdef GSL
6875: /******************* Gompertz_f Likelihood ******************************/
6876: double gompertz_f(const gsl_vector *v, void *params)
6877: {
6878: double A,B,LL=0.0,sump=0.,num=0.;
6879: double *x= (double *) v->data;
6880: int i,n=0; /* n is the size of the sample */
6881:
6882: for (i=0;i<=imx-1 ; i++) {
6883: sump=sump+weight[i];
6884: /* sump=sump+1;*/
6885: num=num+1;
6886: }
6887:
6888:
6889: /* for (i=0; i<=imx; i++)
6890: 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]);*/
6891: printf("x[0]=%lf x[1]=%lf\n",x[0],x[1]);
6892: for (i=1;i<=imx ; i++)
6893: {
6894: if (cens[i] == 1 && wav[i]>1)
6895: A=-x[0]/(x[1])*(exp(x[1]*(agecens[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)));
6896:
6897: if (cens[i] == 0 && wav[i]>1)
6898: A=-x[0]/(x[1])*(exp(x[1]*(agedc[i]-agegomp))-exp(x[1]*(ageexmed[i]-agegomp)))
6899: +log(x[0]/YEARM)+x[1]*(agedc[i]-agegomp)+log(YEARM);
6900:
6901: /*if (wav[i] > 1 && agecens[i] > 15) {*/ /* ??? */
6902: if (wav[i] > 1 ) { /* ??? */
6903: LL=LL+A*weight[i];
6904: /* 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]);*/
6905: }
6906: }
6907:
6908: /*printf("x1=%2.9f x2=%2.9f x3=%2.9f L=%f\n",x[1],x[2],x[3],L);*/
6909: printf("x[0]=%lf x[1]=%lf -2*LL*num/sump=%lf\n",x[0],x[1],-2*LL*num/sump);
6910:
6911: return -2*LL*num/sump;
6912: }
6913: #endif
6914:
1.126 brouard 6915: /******************* Printing html file ***********/
1.201 brouard 6916: void printinghtmlmort(char fileresu[], char title[], char datafile[], int firstpass, \
1.126 brouard 6917: int lastpass, int stepm, int weightopt, char model[],\
6918: int imx, double p[],double **matcov,double agemortsup){
6919: int i,k;
6920:
6921: fprintf(fichtm,"<ul><li><h4>Result files </h4>\n Force of mortality. Parameters of the Gompertz fit (with confidence interval in brackets):<br>");
6922: fprintf(fichtm," mu(age) =%lf*exp(%lf*(age-%d)) per year<br><br>",p[1],p[2],agegomp);
6923: for (i=1;i<=2;i++)
6924: 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 6925: fprintf(fichtm,"<br><br><img src=\"graphmort.svg\">");
1.126 brouard 6926: fprintf(fichtm,"</ul>");
6927:
6928: fprintf(fichtm,"<ul><li><h4>Life table</h4>\n <br>");
6929:
6930: 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>");
6931:
6932: for (k=agegomp;k<(agemortsup-2);k++)
6933: 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]);
6934:
6935:
6936: fflush(fichtm);
6937: }
6938:
6939: /******************* Gnuplot file **************/
1.201 brouard 6940: void printinggnuplotmort(char fileresu[], char optionfilefiname[], double ageminpar, double agemaxpar, double fage , char pathc[], double p[]){
1.126 brouard 6941:
6942: char dirfileres[132],optfileres[132];
1.164 brouard 6943:
1.126 brouard 6944: int ng;
6945:
6946:
6947: /*#ifdef windows */
6948: fprintf(ficgp,"cd \"%s\" \n",pathc);
6949: /*#endif */
6950:
6951:
6952: strcpy(dirfileres,optionfilefiname);
6953: strcpy(optfileres,"vpl");
1.199 brouard 6954: fprintf(ficgp,"set out \"graphmort.svg\"\n ");
1.126 brouard 6955: fprintf(ficgp,"set xlabel \"Age\"\n set ylabel \"Force of mortality (per year)\" \n ");
1.199 brouard 6956: fprintf(ficgp, "set ter svg size 640, 480\n set log y\n");
1.145 brouard 6957: /* fprintf(ficgp, "set size 0.65,0.65\n"); */
1.126 brouard 6958: fprintf(ficgp,"plot [%d:100] %lf*exp(%lf*(x-%d))",agegomp,p[1],p[2],agegomp);
6959:
6960: }
6961:
1.136 brouard 6962: int readdata(char datafile[], int firstobs, int lastobs, int *imax)
6963: {
1.126 brouard 6964:
1.136 brouard 6965: /*-------- data file ----------*/
6966: FILE *fic;
6967: char dummy[]=" ";
1.164 brouard 6968: int i=0, j=0, n=0;
1.136 brouard 6969: int linei, month, year,iout;
6970: char line[MAXLINE], linetmp[MAXLINE];
1.164 brouard 6971: char stra[MAXLINE], strb[MAXLINE];
1.136 brouard 6972: char *stratrunc;
6973: int lstra;
1.126 brouard 6974:
6975:
1.136 brouard 6976: if((fic=fopen(datafile,"r"))==NULL) {
1.218 ! brouard 6977: printf("Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(stdout);
! 6978: fprintf(ficlog,"Problem while opening datafile: %s with errno='%s'\n", datafile,strerror(errno));fflush(ficlog);return 1;
1.136 brouard 6979: }
1.126 brouard 6980:
1.136 brouard 6981: i=1;
6982: linei=0;
6983: while ((fgets(line, MAXLINE, fic) != NULL) &&((i >= firstobs) && (i <=lastobs))) {
6984: linei=linei+1;
6985: for(j=strlen(line); j>=0;j--){ /* Untabifies line */
6986: if(line[j] == '\t')
6987: line[j] = ' ';
6988: }
6989: for(j=strlen(line)-1; (line[j]==' ')||(line[j]==10)||(line[j]==13);j--){
6990: ;
6991: };
6992: line[j+1]=0; /* Trims blanks at end of line */
6993: if(line[0]=='#'){
6994: fprintf(ficlog,"Comment line\n%s\n",line);
6995: printf("Comment line\n%s\n",line);
6996: continue;
6997: }
6998: trimbb(linetmp,line); /* Trims multiple blanks in line */
1.164 brouard 6999: strcpy(line, linetmp);
1.136 brouard 7000:
1.126 brouard 7001:
1.136 brouard 7002: for (j=maxwav;j>=1;j--){
1.137 brouard 7003: cutv(stra, strb, line, ' ');
1.136 brouard 7004: if(strb[0]=='.') { /* Missing status */
7005: lval=-1;
7006: }else{
7007: errno=0;
7008: lval=strtol(strb,&endptr,10);
7009: /* if (errno == ERANGE && (lval == LONG_MAX || lval == LONG_MIN))*/
7010: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7011: 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);
7012: 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);
1.136 brouard 7013: return 1;
7014: }
7015: }
7016: s[j][i]=lval;
7017:
7018: strcpy(line,stra);
7019: cutv(stra, strb,line,' ');
1.169 brouard 7020: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7021: }
1.169 brouard 7022: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7023: month=99;
7024: year=9999;
7025: }else{
1.141 brouard 7026: 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);
7027: 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);
1.136 brouard 7028: return 1;
7029: }
7030: anint[j][i]= (double) year;
7031: mint[j][i]= (double)month;
7032: strcpy(line,stra);
7033: } /* ENd Waves */
7034:
7035: cutv(stra, strb,line,' ');
1.169 brouard 7036: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7037: }
1.169 brouard 7038: else if( (iout=sscanf(strb,"%s.",dummy)) != 0){
1.136 brouard 7039: month=99;
7040: year=9999;
7041: }else{
1.141 brouard 7042: 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);
7043: 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);
1.136 brouard 7044: return 1;
7045: }
7046: andc[i]=(double) year;
7047: moisdc[i]=(double) month;
7048: strcpy(line,stra);
7049:
7050: cutv(stra, strb,line,' ');
1.169 brouard 7051: if( (iout=sscanf(strb,"%d/%d",&month, &year)) != 0){
1.136 brouard 7052: }
1.169 brouard 7053: else if( (iout=sscanf(strb,"%s.", dummy)) != 0){
1.136 brouard 7054: month=99;
7055: year=9999;
7056: }else{
1.141 brouard 7057: 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);
7058: 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.136 brouard 7059: return 1;
7060: }
7061: if (year==9999) {
1.141 brouard 7062: 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);
7063: 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.136 brouard 7064: return 1;
1.126 brouard 7065:
1.136 brouard 7066: }
7067: annais[i]=(double)(year);
7068: moisnais[i]=(double)(month);
7069: strcpy(line,stra);
7070:
7071: cutv(stra, strb,line,' ');
7072: errno=0;
7073: dval=strtod(strb,&endptr);
7074: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7075: printf("Error reading data around '%f' at line number %d, \"%s\" for individual %d\nShould be a weight. Exiting.\n",dval, i,line,linei);
7076: 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 7077: fflush(ficlog);
7078: return 1;
7079: }
7080: weight[i]=dval;
7081: strcpy(line,stra);
7082:
7083: for (j=ncovcol;j>=1;j--){
7084: cutv(stra, strb,line,' ');
7085: if(strb[0]=='.') { /* Missing status */
7086: lval=-1;
7087: }else{
7088: errno=0;
7089: lval=strtol(strb,&endptr,10);
7090: if( strb[0]=='\0' || (*endptr != '\0')){
1.141 brouard 7091: 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);
7092: 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);
1.136 brouard 7093: return 1;
7094: }
7095: }
7096: if(lval <-1 || lval >1){
1.141 brouard 7097: printf("Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7098: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7099: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
7100: For example, for multinomial values like 1, 2 and 3,\n \
7101: build V1=0 V2=0 for the reference value (1),\n \
7102: V1=1 V2=0 for (2) \n \
7103: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
7104: output of IMaCh is often meaningless.\n \
7105: Exiting.\n",lval,linei, i,line,j);
1.141 brouard 7106: fprintf(ficlog,"Error reading data around '%ld' at line number %d for individual %d, '%s'\n \
1.136 brouard 7107: Should be a value of %d(nth) covariate (0 should be the value for the reference and 1\n \
7108: for the alternative. IMaCh does not build design variables automatically, do it yourself.\n \
7109: For example, for multinomial values like 1, 2 and 3,\n \
7110: build V1=0 V2=0 for the reference value (1),\n \
7111: V1=1 V2=0 for (2) \n \
7112: and V1=0 V2=1 for (3). V1=1 V2=1 should not exist and the corresponding\n \
7113: output of IMaCh is often meaningless.\n \
7114: Exiting.\n",lval,linei, i,line,j);fflush(ficlog);
7115: return 1;
7116: }
7117: covar[j][i]=(double)(lval);
7118: strcpy(line,stra);
7119: }
7120: lstra=strlen(stra);
7121:
7122: if(lstra > 9){ /* More than 2**32 or max of what printf can write with %ld */
7123: stratrunc = &(stra[lstra-9]);
7124: num[i]=atol(stratrunc);
7125: }
7126: else
7127: num[i]=atol(stra);
7128: /*if((s[2][i]==2) && (s[3][i]==-1)&&(s[4][i]==9)){
7129: 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;}*/
7130:
7131: i=i+1;
7132: } /* End loop reading data */
1.126 brouard 7133:
1.136 brouard 7134: *imax=i-1; /* Number of individuals */
7135: fclose(fic);
7136:
7137: return (0);
1.164 brouard 7138: /* endread: */
1.136 brouard 7139: printf("Exiting readdata: ");
7140: fclose(fic);
7141: return (1);
1.126 brouard 7142:
7143:
7144:
1.136 brouard 7145: }
1.145 brouard 7146: void removespace(char *str) {
7147: char *p1 = str, *p2 = str;
7148: do
7149: while (*p2 == ' ')
7150: p2++;
1.169 brouard 7151: while (*p1++ == *p2++);
1.145 brouard 7152: }
7153:
7154: int decodemodel ( char model[], int lastobs) /**< This routine decode the model and returns:
1.187 brouard 7155: * Model V1+V2+V3+V8+V7*V8+V5*V6+V8*age+V3*age+age*age
7156: * - nagesqr = 1 if age*age in the model, otherwise 0.
7157: * - cptcovt total number of covariates of the model nbocc(+)+1 = 8 excepting constant and age and age*age
7158: * - cptcovn or number of covariates k of the models excluding age*products =6 and age*age
1.145 brouard 7159: * - cptcovage number of covariates with age*products =2
7160: * - cptcovs number of simple covariates
7161: * - 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
7162: * which is a new column after the 9 (ncovcol) variables.
7163: * - if k is a product Vn*Vm covar[k][i] is filled with correct values for each individual
7164: * - Tprod[l] gives the kth covariates of the product Vn*Vm l=1 to cptcovprod-cptcovage
7165: * Tprod[1]@2 {5, 6}: position of first product V7*V8 is 5, and second V5*V6 is 6.
7166: * - Tvard[k] p Tvard[1][1]@4 {7, 8, 5, 6} for V7*V8 and V5*V6 .
7167: */
1.136 brouard 7168: {
1.145 brouard 7169: int i, j, k, ks;
1.164 brouard 7170: int j1, k1, k2;
1.136 brouard 7171: char modelsav[80];
1.145 brouard 7172: char stra[80], strb[80], strc[80], strd[80],stre[80];
1.187 brouard 7173: char *strpt;
1.136 brouard 7174:
1.145 brouard 7175: /*removespace(model);*/
1.136 brouard 7176: if (strlen(model) >1){ /* If there is at least 1 covariate */
1.145 brouard 7177: j=0, j1=0, k1=0, k2=-1, ks=0, cptcovn=0;
1.137 brouard 7178: if (strstr(model,"AGE") !=0){
1.192 brouard 7179: printf("Error. AGE must be in lower case 'age' model=1+age+%s. ",model);
7180: fprintf(ficlog,"Error. AGE must be in lower case model=1+age+%s. ",model);fflush(ficlog);
1.136 brouard 7181: return 1;
7182: }
1.141 brouard 7183: if (strstr(model,"v") !=0){
7184: printf("Error. 'v' must be in upper case 'V' model=%s ",model);
7185: fprintf(ficlog,"Error. 'v' must be in upper case model=%s ",model);fflush(ficlog);
7186: return 1;
7187: }
1.187 brouard 7188: strcpy(modelsav,model);
7189: if ((strpt=strstr(model,"age*age")) !=0){
7190: printf(" strpt=%s, model=%s\n",strpt, model);
7191: if(strpt != model){
7192: printf("Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7193: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7194: corresponding column of parameters.\n",model);
7195: fprintf(ficlog,"Error in model: 'model=%s'; 'age*age' should in first place before other covariates\n \
1.192 brouard 7196: 'model=1+age+age*age+V1.' or 'model=1+age+age*age+V1+V1*age.', please swap as well as \n \
1.187 brouard 7197: corresponding column of parameters.\n",model); fflush(ficlog);
7198: return 1;
7199: }
7200:
7201: nagesqr=1;
7202: if (strstr(model,"+age*age") !=0)
7203: substrchaine(modelsav, model, "+age*age");
7204: else if (strstr(model,"age*age+") !=0)
7205: substrchaine(modelsav, model, "age*age+");
7206: else
7207: substrchaine(modelsav, model, "age*age");
7208: }else
7209: nagesqr=0;
7210: if (strlen(modelsav) >1){
7211: j=nbocc(modelsav,'+'); /**< j=Number of '+' */
7212: j1=nbocc(modelsav,'*'); /**< j1=Number of '*' */
7213: cptcovs=j+1-j1; /**< Number of simple covariates V1+V1*age+V3 +V3*V4+age*age=> V1 + V3 =2 */
7214: cptcovt= j+1; /* Number of total covariates in the model, not including
7215: * cst, age and age*age
7216: * V1+V1*age+ V3 + V3*V4+age*age=> 4*/
7217: /* including age products which are counted in cptcovage.
7218: * but the covariates which are products must be treated
7219: * separately: ncovn=4- 2=2 (V1+V3). */
7220: cptcovprod=j1; /**< Number of products V1*V2 +v3*age = 2 */
7221: cptcovprodnoage=0; /**< Number of covariate products without age: V3*V4 =1 */
7222:
7223:
7224: /* Design
7225: * V1 V2 V3 V4 V5 V6 V7 V8 V9 Weight
7226: * < ncovcol=8 >
7227: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8
7228: * k= 1 2 3 4 5 6 7 8
7229: * cptcovn number of covariates (not including constant and age ) = # of + plus 1 = 7+1=8
7230: * covar[k,i], value of kth covariate if not including age for individual i:
7231: * covar[1][i]= (V2), covar[4][i]=(V3), covar[8][i]=(V8)
7232: * Tvar[k] # of the kth covariate: Tvar[1]=2 Tvar[4]=3 Tvar[8]=8
7233: * if multiplied by age: V3*age Tvar[3=V3*age]=3 (V3) Tvar[7]=8 and
7234: * Tage[++cptcovage]=k
7235: * if products, new covar are created after ncovcol with k1
7236: * Tvar[k]=ncovcol+k1; # of the kth covariate product: Tvar[5]=ncovcol+1=10 Tvar[6]=ncovcol+1=11
7237: * Tprod[k1]=k; Tprod[1]=5 Tprod[2]= 6; gives the position of the k1th product
7238: * 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
7239: * Tvar[cptcovn+k2]=Tvard[k1][1];Tvar[cptcovn+k2+1]=Tvard[k1][2];
7240: * Tvar[8+1]=5;Tvar[8+2]=6;Tvar[8+3]=7;Tvar[8+4]=8 inverted
7241: * V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11
7242: * < ncovcol=8 >
7243: * Model V2 + V1 + V3*age + V3 + V5*V6 + V7*V8 + V8*age + V8 d1 d1 d2 d2
7244: * k= 1 2 3 4 5 6 7 8 9 10 11 12
7245: * Tvar[k]= 2 1 3 3 10 11 8 8 5 6 7 8
7246: * p Tvar[1]@12={2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7247: * p Tprod[1]@2={ 6, 5}
7248: *p Tvard[1][1]@4= {7, 8, 5, 6}
7249: * covar[k][i]= V2 V1 ? V3 V5*V6? V7*V8? ? V8
7250: * cov[Tage[kk]+2]=covar[Tvar[Tage[kk]]][i]*cov[2];
7251: *How to reorganize?
7252: * Model V1 + V2 + V3 + V8 + V5*V6 + V7*V8 + V3*age + V8*age
7253: * Tvars {2, 1, 3, 3, 11, 10, 8, 8, 7, 8, 5, 6}
7254: * {2, 1, 4, 8, 5, 6, 3, 7}
7255: * Struct []
7256: */
1.145 brouard 7257:
1.187 brouard 7258: /* This loop fills the array Tvar from the string 'model'.*/
7259: /* j is the number of + signs in the model V1+V2+V3 j=2 i=3 to 1 */
7260: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4 */
7261: /* k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tage[cptcovage=1]=4 */
7262: /* k=3 V4 Tvar[k=3]= 4 (from V4) */
7263: /* k=2 V1 Tvar[k=2]= 1 (from V1) */
7264: /* k=1 Tvar[1]=2 (from V2) */
7265: /* k=5 Tvar[5] */
7266: /* for (k=1; k<=cptcovn;k++) { */
1.198 brouard 7267: /* cov[2+k]=nbcode[Tvar[k]][codtabm(ij,Tvar[k])]; */
1.187 brouard 7268: /* } */
1.198 brouard 7269: /* for (k=1; k<=cptcovage;k++) cov[2+Tage[k]]=nbcode[Tvar[Tage[k]]][codtabm(ij,Tvar[Tage[k])]]*cov[2]; */
1.187 brouard 7270: /*
7271: * Treating invertedly V2+V1+V3*age+V2*V4 is as if written V2*V4 +V3*age + V1 + V2 */
7272: for(k=cptcovt; k>=1;k--) /**< Number of covariates */
1.145 brouard 7273: Tvar[k]=0;
1.187 brouard 7274: cptcovage=0;
7275: for(k=1; k<=cptcovt;k++){ /* Loop on total covariates of the model */
7276: cutl(stra,strb,modelsav,'+'); /* keeps in strb after the first '+'
7277: modelsav==V2+V1+V4+V3*age strb=V3*age stra=V2+V1+V4 */
7278: if (nbocc(modelsav,'+')==0) strcpy(strb,modelsav); /* and analyzes it */
7279: /* printf("i=%d a=%s b=%s sav=%s\n",i, stra,strb,modelsav);*/
7280: /*scanf("%d",i);*/
7281: if (strchr(strb,'*')) { /**< Model includes a product V2+V1+V4+V3*age strb=V3*age */
7282: cutl(strc,strd,strb,'*'); /**< strd*strc Vm*Vn: strb=V3*age(input) strc=age strd=V3 ; V3*V2 strc=V2, strd=V3 */
7283: if (strcmp(strc,"age")==0) { /**< Model includes age: Vn*age */
7284: /* covar is not filled and then is empty */
7285: cptcovprod--;
7286: cutl(stre,strb,strd,'V'); /* strd=V3(input): stre="3" */
7287: Tvar[k]=atoi(stre); /* V2+V1+V4+V3*age Tvar[4]=3 ; V1+V2*age Tvar[2]=2; V1+V1*age Tvar[2]=1 */
7288: cptcovage++; /* Sums the number of covariates which include age as a product */
7289: Tage[cptcovage]=k; /* Tvar[4]=3, Tage[1] = 4 or V1+V1*age Tvar[2]=1, Tage[1]=2 */
7290: /*printf("stre=%s ", stre);*/
7291: } else if (strcmp(strd,"age")==0) { /* or age*Vn */
7292: cptcovprod--;
7293: cutl(stre,strb,strc,'V');
7294: Tvar[k]=atoi(stre);
7295: cptcovage++;
7296: Tage[cptcovage]=k;
7297: } else { /* Age is not in the model product V2+V1+V1*V4+V3*age+V3*V2 strb=V3*V2*/
7298: /* loops on k1=1 (V3*V2) and k1=2 V4*V3 */
7299: cptcovn++;
7300: cptcovprodnoage++;k1++;
7301: cutl(stre,strb,strc,'V'); /* strc= Vn, stre is n; strb=V3*V2 stre=3 strc=*/
7302: Tvar[k]=ncovcol+k1; /* For model-covariate k tells which data-covariate to use but
7303: because this model-covariate is a construction we invent a new column
7304: ncovcol + k1
7305: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3+V3*V2
7306: Tvar[3=V1*V4]=4+1 Tvar[5=V3*V2]=4 + 2= 6, etc */
7307: cutl(strc,strb,strd,'V'); /* strd was Vm, strc is m */
7308: Tprod[k1]=k; /* Tprod[1]=3(=V1*V4) for V2+V1+V1*V4+age*V3+V3*V2 */
7309: Tvard[k1][1] =atoi(strc); /* m 1 for V1*/
7310: Tvard[k1][2] =atoi(stre); /* n 4 for V4*/
7311: k2=k2+2;
7312: Tvar[cptcovt+k2]=Tvard[k1][1]; /* Tvar[(cptcovt=4+k2=1)=5]= 1 (V1) */
7313: Tvar[cptcovt+k2+1]=Tvard[k1][2]; /* Tvar[(cptcovt=4+(k2=1)+1)=6]= 4 (V4) */
7314: for (i=1; i<=lastobs;i++){
7315: /* Computes the new covariate which is a product of
7316: covar[n][i]* covar[m][i] and stores it at ncovol+k1 May not be defined */
7317: covar[ncovcol+k1][i]=covar[atoi(stre)][i]*covar[atoi(strc)][i];
7318: }
7319: } /* End age is not in the model */
7320: } /* End if model includes a product */
7321: else { /* no more sum */
7322: /*printf("d=%s c=%s b=%s\n", strd,strc,strb);*/
7323: /* scanf("%d",i);*/
7324: cutl(strd,strc,strb,'V');
7325: ks++; /**< Number of simple covariates */
1.145 brouard 7326: cptcovn++;
1.187 brouard 7327: Tvar[k]=atoi(strd);
7328: }
7329: strcpy(modelsav,stra); /* modelsav=V2+V1+V4 stra=V2+V1+V4 */
7330: /*printf("a=%s b=%s sav=%s\n", stra,strb,modelsav);
7331: scanf("%d",i);*/
7332: } /* end of loop + on total covariates */
7333: } /* end if strlen(modelsave == 0) age*age might exist */
7334: } /* end if strlen(model == 0) */
1.136 brouard 7335:
7336: /*The number n of Vn is stored in Tvar. cptcovage =number of age covariate. Tage gives the position of age. cptcovprod= number of products.
7337: If model=V1+V1*age then Tvar[1]=1 Tvar[2]=1 cptcovage=1 Tage[1]=2 cptcovprod=0*/
7338:
7339: /* printf("tvar1=%d tvar2=%d tvar3=%d cptcovage=%d Tage=%d",Tvar[1],Tvar[2],Tvar[3],cptcovage,Tage[1]);
7340: printf("cptcovprod=%d ", cptcovprod);
7341: fprintf(ficlog,"cptcovprod=%d ", cptcovprod);
7342:
7343: scanf("%d ",i);*/
7344:
7345:
1.137 brouard 7346: return (0); /* with covar[new additional covariate if product] and Tage if age */
1.164 brouard 7347: /*endread:*/
1.136 brouard 7348: printf("Exiting decodemodel: ");
7349: return (1);
7350: }
7351:
1.169 brouard 7352: int calandcheckages(int imx, int maxwav, double *agemin, double *agemax, int *nberr, int *nbwarn )
1.136 brouard 7353: {
7354: int i, m;
1.218 ! brouard 7355: int firstone=0;
! 7356:
1.136 brouard 7357: for (i=1; i<=imx; i++) {
7358: for(m=2; (m<= maxwav); m++) {
7359: if (((int)mint[m][i]== 99) && (s[m][i] <= nlstate)){
7360: anint[m][i]=9999;
1.216 brouard 7361: if (s[m][i] != -2) /* Keeping initial status of unknown vital status */
7362: s[m][i]=-1;
1.136 brouard 7363: }
7364: if((int)moisdc[i]==99 && (int)andc[i]==9999 && s[m][i]>nlstate){
1.169 brouard 7365: *nberr = *nberr + 1;
1.218 ! brouard 7366: if(firstone == 0){
! 7367: firstone=1;
! 7368: 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);
! 7369: }
! 7370: 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 7371: s[m][i]=-1;
7372: }
7373: if((int)moisdc[i]==99 && (int)andc[i]!=9999 && s[m][i]>nlstate){
1.169 brouard 7374: (*nberr)++;
1.136 brouard 7375: 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]);
7376: 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]);
7377: s[m][i]=-1; /* We prefer to skip it (and to skip it in version 0.8a1 too */
7378: }
7379: }
7380: }
7381:
7382: for (i=1; i<=imx; i++) {
7383: agedc[i]=(moisdc[i]/12.+andc[i])-(moisnais[i]/12.+annais[i]);
7384: for(m=firstpass; (m<= lastpass); m++){
1.214 brouard 7385: 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 7386: if (s[m][i] >= nlstate+1) {
1.169 brouard 7387: if(agedc[i]>0){
7388: if((int)moisdc[i]!=99 && (int)andc[i]!=9999){
1.136 brouard 7389: agev[m][i]=agedc[i];
1.214 brouard 7390: /*if(moisdc[i]==99 && andc[i]==9999) s[m][i]=-1;*/
1.169 brouard 7391: }else {
1.136 brouard 7392: if ((int)andc[i]!=9999){
7393: nbwarn++;
7394: printf("Warning negative age at death: %ld line:%d\n",num[i],i);
7395: fprintf(ficlog,"Warning negative age at death: %ld line:%d\n",num[i],i);
7396: agev[m][i]=-1;
7397: }
7398: }
1.169 brouard 7399: } /* agedc > 0 */
1.214 brouard 7400: } /* end if */
1.136 brouard 7401: else if(s[m][i] !=9){ /* Standard case, age in fractional
7402: years but with the precision of a month */
7403: agev[m][i]=(mint[m][i]/12.+1./24.+anint[m][i])-(moisnais[i]/12.+1./24.+annais[i]);
7404: if((int)mint[m][i]==99 || (int)anint[m][i]==9999)
7405: agev[m][i]=1;
7406: else if(agev[m][i] < *agemin){
7407: *agemin=agev[m][i];
7408: printf(" Min anint[%d][%d]=%.2f annais[%d]=%.2f, agemin=%.2f\n",m,i,anint[m][i], i,annais[i], *agemin);
7409: }
7410: else if(agev[m][i] >*agemax){
7411: *agemax=agev[m][i];
1.156 brouard 7412: /* printf(" Max anint[%d][%d]=%.0f annais[%d]=%.0f, agemax=%.2f\n",m,i,anint[m][i], i,annais[i], *agemax);*/
1.136 brouard 7413: }
7414: /*agev[m][i]=anint[m][i]-annais[i];*/
7415: /* agev[m][i] = age[i]+2*m;*/
1.214 brouard 7416: } /* en if 9*/
1.136 brouard 7417: else { /* =9 */
1.214 brouard 7418: /* printf("Debug num[%d]=%ld s[%d][%d]=%d\n",i,num[i], m,i, s[m][i]); */
1.136 brouard 7419: agev[m][i]=1;
7420: s[m][i]=-1;
7421: }
7422: }
1.214 brouard 7423: else if(s[m][i]==0) /*= 0 Unknown */
1.136 brouard 7424: agev[m][i]=1;
1.214 brouard 7425: else{
7426: printf("Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
7427: fprintf(ficlog, "Warning, num[%d]=%ld, s[%d][%d]=%d\n", i, num[i], m, i,s[m][i]);
7428: agev[m][i]=0;
7429: }
7430: } /* End for lastpass */
7431: }
1.136 brouard 7432:
7433: for (i=1; i<=imx; i++) {
7434: for(m=firstpass; (m<=lastpass); m++){
7435: if (s[m][i] > (nlstate+ndeath)) {
1.169 brouard 7436: (*nberr)++;
1.136 brouard 7437: 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);
7438: 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);
7439: return 1;
7440: }
7441: }
7442: }
7443:
7444: /*for (i=1; i<=imx; i++){
7445: for (m=firstpass; (m<lastpass); m++){
7446: printf("%ld %d %.lf %d %d\n", num[i],(covar[1][i]),agev[m][i],s[m][i],s[m+1][i]);
7447: }
7448:
7449: }*/
7450:
7451:
1.139 brouard 7452: printf("Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
7453: fprintf(ficlog,"Total number of individuals= %d, Agemin = %.2f, Agemax= %.2f\n\n", imx, *agemin, *agemax);
1.136 brouard 7454:
7455: return (0);
1.164 brouard 7456: /* endread:*/
1.136 brouard 7457: printf("Exiting calandcheckages: ");
7458: return (1);
7459: }
7460:
1.172 brouard 7461: #if defined(_MSC_VER)
7462: /*printf("Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
7463: /*fprintf(ficlog, "Visual C++ compiler: %s \n;", _MSC_FULL_VER);*/
7464: //#include "stdafx.h"
7465: //#include <stdio.h>
7466: //#include <tchar.h>
7467: //#include <windows.h>
7468: //#include <iostream>
7469: typedef BOOL(WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
7470:
7471: LPFN_ISWOW64PROCESS fnIsWow64Process;
7472:
7473: BOOL IsWow64()
7474: {
7475: BOOL bIsWow64 = FALSE;
7476:
7477: //typedef BOOL (APIENTRY *LPFN_ISWOW64PROCESS)
7478: // (HANDLE, PBOOL);
7479:
7480: //LPFN_ISWOW64PROCESS fnIsWow64Process;
7481:
7482: HMODULE module = GetModuleHandle(_T("kernel32"));
7483: const char funcName[] = "IsWow64Process";
7484: fnIsWow64Process = (LPFN_ISWOW64PROCESS)
7485: GetProcAddress(module, funcName);
7486:
7487: if (NULL != fnIsWow64Process)
7488: {
7489: if (!fnIsWow64Process(GetCurrentProcess(),
7490: &bIsWow64))
7491: //throw std::exception("Unknown error");
7492: printf("Unknown error\n");
7493: }
7494: return bIsWow64 != FALSE;
7495: }
7496: #endif
1.177 brouard 7497:
1.191 brouard 7498: void syscompilerinfo(int logged)
1.167 brouard 7499: {
7500: /* #include "syscompilerinfo.h"*/
1.185 brouard 7501: /* command line Intel compiler 32bit windows, XP compatible:*/
7502: /* /GS /W3 /Gy
7503: /Zc:wchar_t /Zi /O2 /Fd"Release\vc120.pdb" /D "WIN32" /D "NDEBUG" /D
7504: "_CONSOLE" /D "_LIB" /D "_USING_V110_SDK71_" /D "_UNICODE" /D
7505: "UNICODE" /Qipo /Zc:forScope /Gd /Oi /MT /Fa"Release\" /EHsc /nologo
1.186 brouard 7506: /Fo"Release\" /Qprof-dir "Release\" /Fp"Release\IMaCh.pch"
7507: */
7508: /* 64 bits */
1.185 brouard 7509: /*
7510: /GS /W3 /Gy
7511: /Zc:wchar_t /Zi /O2 /Fd"x64\Release\vc120.pdb" /D "WIN32" /D "NDEBUG"
7512: /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo /Zc:forScope
7513: /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Fo"x64\Release\" /Qprof-dir
7514: "x64\Release\" /Fp"x64\Release\IMaCh.pch" */
7515: /* Optimization are useless and O3 is slower than O2 */
7516: /*
7517: /GS /W3 /Gy /Zc:wchar_t /Zi /O3 /Fd"x64\Release\vc120.pdb" /D "WIN32"
7518: /D "NDEBUG" /D "_CONSOLE" /D "_LIB" /D "_UNICODE" /D "UNICODE" /Qipo
7519: /Zc:forScope /Oi /MD /Fa"x64\Release\" /EHsc /nologo /Qparallel
7520: /Fo"x64\Release\" /Qprof-dir "x64\Release\" /Fp"x64\Release\IMaCh.pch"
7521: */
1.186 brouard 7522: /* Link is */ /* /OUT:"visual studio
1.185 brouard 7523: 2013\Projects\IMaCh\Release\IMaCh.exe" /MANIFEST /NXCOMPAT
7524: /PDB:"visual studio
7525: 2013\Projects\IMaCh\Release\IMaCh.pdb" /DYNAMICBASE
7526: "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib"
7527: "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib"
7528: "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib"
7529: /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO
7530: /SUBSYSTEM:CONSOLE",5.01" /MANIFESTUAC:"level='asInvoker'
7531: uiAccess='false'"
7532: /ManifestFile:"Release\IMaCh.exe.intermediate.manifest" /OPT:ICF
7533: /NOLOGO /TLBID:1
7534: */
1.177 brouard 7535: #if defined __INTEL_COMPILER
1.178 brouard 7536: #if defined(__GNUC__)
7537: struct utsname sysInfo; /* For Intel on Linux and OS/X */
7538: #endif
1.177 brouard 7539: #elif defined(__GNUC__)
1.179 brouard 7540: #ifndef __APPLE__
1.174 brouard 7541: #include <gnu/libc-version.h> /* Only on gnu */
1.179 brouard 7542: #endif
1.177 brouard 7543: struct utsname sysInfo;
1.178 brouard 7544: int cross = CROSS;
7545: if (cross){
7546: printf("Cross-");
1.191 brouard 7547: if(logged) fprintf(ficlog, "Cross-");
1.178 brouard 7548: }
1.174 brouard 7549: #endif
7550:
1.171 brouard 7551: #include <stdint.h>
1.178 brouard 7552:
1.191 brouard 7553: printf("Compiled with:");if(logged)fprintf(ficlog,"Compiled with:");
1.169 brouard 7554: #if defined(__clang__)
1.191 brouard 7555: printf(" Clang/LLVM");if(logged)fprintf(ficlog," Clang/LLVM"); /* Clang/LLVM. ---------------------------------------------- */
1.169 brouard 7556: #endif
7557: #if defined(__ICC) || defined(__INTEL_COMPILER)
1.191 brouard 7558: printf(" Intel ICC/ICPC");if(logged)fprintf(ficlog," Intel ICC/ICPC");/* Intel ICC/ICPC. ------------------------------------------ */
1.169 brouard 7559: #endif
7560: #if defined(__GNUC__) || defined(__GNUG__)
1.191 brouard 7561: printf(" GNU GCC/G++");if(logged)fprintf(ficlog," GNU GCC/G++");/* GNU GCC/G++. --------------------------------------------- */
1.169 brouard 7562: #endif
7563: #if defined(__HP_cc) || defined(__HP_aCC)
1.191 brouard 7564: printf(" Hewlett-Packard C/aC++");if(logged)fprintf(fcilog," Hewlett-Packard C/aC++"); /* Hewlett-Packard C/aC++. ---------------------------------- */
1.169 brouard 7565: #endif
7566: #if defined(__IBMC__) || defined(__IBMCPP__)
1.191 brouard 7567: printf(" IBM XL C/C++"); if(logged) fprintf(ficlog," IBM XL C/C++");/* IBM XL C/C++. -------------------------------------------- */
1.169 brouard 7568: #endif
7569: #if defined(_MSC_VER)
1.191 brouard 7570: printf(" Microsoft Visual Studio");if(logged)fprintf(ficlog," Microsoft Visual Studio");/* Microsoft Visual Studio. --------------------------------- */
1.169 brouard 7571: #endif
7572: #if defined(__PGI)
1.191 brouard 7573: printf(" Portland Group PGCC/PGCPP");if(logged) fprintf(ficlog," Portland Group PGCC/PGCPP");/* Portland Group PGCC/PGCPP. ------------------------------- */
1.169 brouard 7574: #endif
7575: #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
1.191 brouard 7576: printf(" Oracle Solaris Studio");if(logged)fprintf(ficlog," Oracle Solaris Studio\n");/* Oracle Solaris Studio. ----------------------------------- */
1.167 brouard 7577: #endif
1.191 brouard 7578: printf(" for "); if (logged) fprintf(ficlog, " for ");
1.169 brouard 7579:
1.167 brouard 7580: // http://stackoverflow.com/questions/4605842/how-to-identify-platform-compiler-from-preprocessor-macros
7581: #ifdef _WIN32 // note the underscore: without it, it's not msdn official!
7582: // Windows (x64 and x86)
1.191 brouard 7583: printf("Windows (x64 and x86) ");if(logged) fprintf(ficlog,"Windows (x64 and x86) ");
1.167 brouard 7584: #elif __unix__ // all unices, not all compilers
7585: // Unix
1.191 brouard 7586: printf("Unix ");if(logged) fprintf(ficlog,"Unix ");
1.167 brouard 7587: #elif __linux__
7588: // linux
1.191 brouard 7589: printf("linux ");if(logged) fprintf(ficlog,"linux ");
1.167 brouard 7590: #elif __APPLE__
1.174 brouard 7591: // Mac OS, not sure if this is covered by __posix__ and/or __unix__ though..
1.191 brouard 7592: printf("Mac OS ");if(logged) fprintf(ficlog,"Mac OS ");
1.167 brouard 7593: #endif
7594:
7595: /* __MINGW32__ */
7596: /* __CYGWIN__ */
7597: /* __MINGW64__ */
7598: // http://msdn.microsoft.com/en-us/library/b0084kay.aspx
7599: /* _MSC_VER //the Visual C++ compiler is 17.00.51106.1, the _MSC_VER macro evaluates to 1700. Type cl /? */
7600: /* _MSC_FULL_VER //the Visual C++ compiler is 15.00.20706.01, the _MSC_FULL_VER macro evaluates to 150020706 */
7601: /* _WIN64 // Defined for applications for Win64. */
7602: /* _M_X64 // Defined for compilations that target x64 processors. */
7603: /* _DEBUG // Defined when you compile with /LDd, /MDd, and /MTd. */
1.171 brouard 7604:
1.167 brouard 7605: #if UINTPTR_MAX == 0xffffffff
1.191 brouard 7606: printf(" 32-bit"); if(logged) fprintf(ficlog," 32-bit");/* 32-bit */
1.167 brouard 7607: #elif UINTPTR_MAX == 0xffffffffffffffff
1.191 brouard 7608: printf(" 64-bit"); if(logged) fprintf(ficlog," 64-bit");/* 64-bit */
1.167 brouard 7609: #else
1.191 brouard 7610: printf(" wtf-bit"); if(logged) fprintf(ficlog," wtf-bit");/* wtf */
1.167 brouard 7611: #endif
7612:
1.169 brouard 7613: #if defined(__GNUC__)
7614: # if defined(__GNUC_PATCHLEVEL__)
7615: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
7616: + __GNUC_MINOR__ * 100 \
7617: + __GNUC_PATCHLEVEL__)
7618: # else
7619: # define __GNUC_VERSION__ (__GNUC__ * 10000 \
7620: + __GNUC_MINOR__ * 100)
7621: # endif
1.174 brouard 7622: printf(" using GNU C version %d.\n", __GNUC_VERSION__);
1.191 brouard 7623: if(logged) fprintf(ficlog, " using GNU C version %d.\n", __GNUC_VERSION__);
1.176 brouard 7624:
7625: if (uname(&sysInfo) != -1) {
7626: printf("Running on: %s %s %s %s %s\n",sysInfo.sysname, sysInfo.nodename, sysInfo.release, sysInfo.version, sysInfo.machine);
1.191 brouard 7627: 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 7628: }
7629: else
7630: perror("uname() error");
1.179 brouard 7631: //#ifndef __INTEL_COMPILER
7632: #if !defined (__INTEL_COMPILER) && !defined(__APPLE__)
1.174 brouard 7633: printf("GNU libc version: %s\n", gnu_get_libc_version());
1.191 brouard 7634: if(logged) fprintf(ficlog,"GNU libc version: %s\n", gnu_get_libc_version());
1.177 brouard 7635: #endif
1.169 brouard 7636: #endif
1.172 brouard 7637:
7638: // void main()
7639: // {
1.169 brouard 7640: #if defined(_MSC_VER)
1.174 brouard 7641: if (IsWow64()){
1.191 brouard 7642: printf("\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
7643: if (logged) fprintf(ficlog, "\nThe program (probably compiled for 32bit) is running under WOW64 (64bit) emulation.\n");
1.174 brouard 7644: }
7645: else{
1.191 brouard 7646: printf("\nThe program is not running under WOW64 (i.e probably on a 64bit Windows).\n");
7647: if (logged) fprintf(ficlog, "\nThe programm is not running under WOW64 (i.e probably on a 64bit Windows).\n");
1.174 brouard 7648: }
1.172 brouard 7649: // printf("\nPress Enter to continue...");
7650: // getchar();
7651: // }
7652:
1.169 brouard 7653: #endif
7654:
1.167 brouard 7655:
7656: }
1.136 brouard 7657:
1.209 brouard 7658: int prevalence_limit(double *p, double **prlim, double ageminpar, double agemaxpar, double ftolpl, int *ncvyearp){
1.180 brouard 7659: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
7660: int i, j, k, i1 ;
1.202 brouard 7661: /* double ftolpl = 1.e-10; */
1.180 brouard 7662: double age, agebase, agelim;
1.203 brouard 7663: double tot;
1.180 brouard 7664:
1.202 brouard 7665: strcpy(filerespl,"PL_");
7666: strcat(filerespl,fileresu);
7667: if((ficrespl=fopen(filerespl,"w"))==NULL) {
7668: printf("Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
7669: fprintf(ficlog,"Problem with period (stable) prevalence resultfile: %s\n", filerespl);return 1;
7670: }
7671: printf("Computing period (stable) prevalence: result on file '%s' \n", filerespl);
7672: fprintf(ficlog,"Computing period (stable) prevalence: result on file '%s' \n", filerespl);
7673: pstamp(ficrespl);
1.203 brouard 7674: fprintf(ficrespl,"# Period (stable) prevalence. Precision given by ftolpl=%g \n", ftolpl);
1.202 brouard 7675: fprintf(ficrespl,"#Age ");
7676: for(i=1; i<=nlstate;i++) fprintf(ficrespl,"%d-%d ",i,i);
7677: fprintf(ficrespl,"\n");
1.180 brouard 7678:
7679: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
7680:
7681: agebase=ageminpar;
7682: agelim=agemaxpar;
7683:
7684: i1=pow(2,cptcoveff);
7685: if (cptcovn < 1){i1=1;}
7686:
7687: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
7688: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
7689: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
7690: k=k+1;
7691: /* to clean */
1.198 brouard 7692: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
1.200 brouard 7693: fprintf(ficrespl,"#******");
7694: printf("#******");
7695: fprintf(ficlog,"#******");
1.180 brouard 7696: for(j=1;j<=cptcoveff;j++) {
1.198 brouard 7697: fprintf(ficrespl," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7698: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
7699: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.180 brouard 7700: }
7701: fprintf(ficrespl,"******\n");
7702: printf("******\n");
7703: fprintf(ficlog,"******\n");
7704:
7705: fprintf(ficrespl,"#Age ");
7706: for(j=1;j<=cptcoveff;j++) {
1.200 brouard 7707: fprintf(ficrespl,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.180 brouard 7708: }
1.203 brouard 7709: for(i=1; i<=nlstate;i++) fprintf(ficrespl," %d-%d ",i,i);
7710: fprintf(ficrespl,"Total Years_to_converge\n");
1.180 brouard 7711:
7712: for (age=agebase; age<=agelim; age++){
7713: /* for (age=agebase; age<=agebase; age++){ */
1.209 brouard 7714: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, ncvyearp, k);
1.180 brouard 7715: fprintf(ficrespl,"%.0f ",age );
7716: for(j=1;j<=cptcoveff;j++)
1.198 brouard 7717: fprintf(ficrespl,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.203 brouard 7718: tot=0.;
7719: for(i=1; i<=nlstate;i++){
7720: tot += prlim[i][i];
1.180 brouard 7721: fprintf(ficrespl," %.5f", prlim[i][i]);
1.203 brouard 7722: }
1.209 brouard 7723: fprintf(ficrespl," %.3f %d\n", tot, *ncvyearp);
1.180 brouard 7724: } /* Age */
7725: /* was end of cptcod */
7726: } /* cptcov */
1.184 brouard 7727: return 0;
1.180 brouard 7728: }
7729:
1.218 ! brouard 7730: 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){
! 7731: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
! 7732:
! 7733: /* Computes the back prevalence limit for any combination of covariate values
! 7734: * at any age between ageminpar and agemaxpar
! 7735: */
1.217 brouard 7736: int i, j, k, i1 ;
7737: /* double ftolpl = 1.e-10; */
7738: double age, agebase, agelim;
7739: double tot;
1.218 ! brouard 7740: /* double ***mobaverage; */
! 7741: /* double **dnewm, **doldm, **dsavm; /\* for use *\/ */
1.217 brouard 7742:
7743: strcpy(fileresplb,"PLB_");
7744: strcat(fileresplb,fileresu);
7745: if((ficresplb=fopen(fileresplb,"w"))==NULL) {
7746: printf("Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
7747: fprintf(ficlog,"Problem with period (stable) back prevalence resultfile: %s\n", fileresplb);return 1;
7748: }
7749: printf("Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
7750: fprintf(ficlog,"Computing period (stable) back prevalence: result on file '%s' \n", fileresplb);
7751: pstamp(ficresplb);
7752: fprintf(ficresplb,"# Period (stable) back prevalence. Precision given by ftolpl=%g \n", ftolpl);
7753: fprintf(ficresplb,"#Age ");
7754: for(i=1; i<=nlstate;i++) fprintf(ficresplb,"%d-%d ",i,i);
7755: fprintf(ficresplb,"\n");
7756:
1.218 ! brouard 7757:
! 7758: /* prlim=matrix(1,nlstate,1,nlstate);*/ /* back in main */
! 7759:
! 7760: agebase=ageminpar;
! 7761: agelim=agemaxpar;
! 7762:
! 7763:
! 7764: i1=pow(2,cptcoveff);
! 7765: if (cptcovn < 1){i1=1;}
! 7766:
! 7767: for(cptcov=1,k=0;cptcov<=i1;cptcov++){
1.217 brouard 7768: /* for(cptcov=1,k=0;cptcov<=1;cptcov++){ */
1.218 ! brouard 7769: //for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){
! 7770: k=k+1;
! 7771: /* to clean */
! 7772: //printf("cptcov=%d cptcod=%d codtab=%d\n",cptcov, cptcod,codtabm(cptcod,cptcov));
! 7773: fprintf(ficresplb,"#******");
! 7774: printf("#******");
! 7775: fprintf(ficlog,"#******");
! 7776: for(j=1;j<=cptcoveff;j++) {
! 7777: fprintf(ficresplb," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 7778: printf(" V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 7779: fprintf(ficlog," V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 7780: }
! 7781: fprintf(ficresplb,"******\n");
! 7782: printf("******\n");
! 7783: fprintf(ficlog,"******\n");
! 7784:
! 7785: fprintf(ficresplb,"#Age ");
! 7786: for(j=1;j<=cptcoveff;j++) {
! 7787: fprintf(ficresplb,"V%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 7788: }
! 7789: for(i=1; i<=nlstate;i++) fprintf(ficresplb," %d-%d ",i,i);
! 7790: fprintf(ficresplb,"Total Years_to_converge\n");
! 7791:
! 7792:
! 7793: for (age=agebase; age<=agelim; age++){
! 7794: /* for (age=agebase; age<=agebase; age++){ */
! 7795: if(mobilavproj > 0){
! 7796: /* bprevalim(bprlim, mobaverage, nlstate, p, age, ageminpar, agemaxpar, oldm, savm, doldm, dsavm, ftolpl, ncvyearp, k); */
! 7797: /* bprevalim(bprlim, mobaverage, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
! 7798: bprevalim(bprlim, mobaverage, nlstate, p, age, ftolpl, ncvyearp, k);
! 7799: }else if (mobilavproj == 0){
! 7800: 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);
! 7801: 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);
! 7802: exit(1);
! 7803: }else{
! 7804: /* bprevalim(bprlim, probs, nlstate, p, age, oldm, savm, dnewm, doldm, dsavm, ftolpl, ncvyearp, k); */
! 7805: bprevalim(bprlim, probs, nlstate, p, age, ftolpl, ncvyearp, k);
! 7806: }
! 7807: fprintf(ficresplb,"%.0f ",age );
! 7808: for(j=1;j<=cptcoveff;j++)
! 7809: fprintf(ficresplb,"%d %d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 7810: tot=0.;
! 7811: for(i=1; i<=nlstate;i++){
! 7812: tot += bprlim[i][i];
! 7813: fprintf(ficresplb," %.5f", bprlim[i][i]);
! 7814: }
! 7815: fprintf(ficresplb," %.3f %d\n", tot, *ncvyearp);
! 7816: } /* Age */
! 7817: /* was end of cptcod */
! 7818: } /* cptcov */
! 7819:
! 7820: /* hBijx(p, bage, fage); */
! 7821: /* fclose(ficrespijb); */
! 7822:
! 7823: return 0;
1.217 brouard 7824: }
1.218 ! brouard 7825:
1.180 brouard 7826: int hPijx(double *p, int bage, int fage){
7827: /*------------- h Pij x at various ages ------------*/
7828:
7829: int stepsize;
7830: int agelim;
7831: int hstepm;
7832: int nhstepm;
7833: int h, i, i1, j, k;
7834:
7835: double agedeb;
7836: double ***p3mat;
7837:
1.201 brouard 7838: strcpy(filerespij,"PIJ_"); strcat(filerespij,fileresu);
1.180 brouard 7839: if((ficrespij=fopen(filerespij,"w"))==NULL) {
7840: printf("Problem with Pij resultfile: %s\n", filerespij); return 1;
7841: fprintf(ficlog,"Problem with Pij resultfile: %s\n", filerespij); return 1;
7842: }
7843: printf("Computing pij: result on file '%s' \n", filerespij);
7844: fprintf(ficlog,"Computing pij: result on file '%s' \n", filerespij);
7845:
7846: stepsize=(int) (stepm+YEARM-1)/YEARM;
7847: /*if (stepm<=24) stepsize=2;*/
7848:
7849: agelim=AGESUP;
7850: hstepm=stepsize*YEARM; /* Every year of age */
7851: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
1.218 ! brouard 7852:
1.180 brouard 7853: /* hstepm=1; aff par mois*/
7854: pstamp(ficrespij);
7855: fprintf(ficrespij,"#****** h Pij x Probability to be in state j at age x+h being in i at x ");
7856: i1= pow(2,cptcoveff);
1.218 ! brouard 7857: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
! 7858: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
! 7859: /* k=k+1; */
1.183 brouard 7860: for (k=1; k <= (int) pow(2,cptcoveff); k++){
7861: fprintf(ficrespij,"\n#****** ");
7862: for(j=1;j<=cptcoveff;j++)
1.198 brouard 7863: fprintf(ficrespij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.183 brouard 7864: fprintf(ficrespij,"******\n");
7865:
7866: for (agedeb=fage; agedeb>=bage; agedeb--){ /* If stepm=6 months */
7867: nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
7868: nhstepm = nhstepm/hstepm; /* Typically 40/4=10 */
7869:
7870: /* nhstepm=nhstepm*YEARM; aff par mois*/
1.180 brouard 7871:
1.183 brouard 7872: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7873: oldm=oldms;savm=savms;
7874: hpxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k);
7875: fprintf(ficrespij,"# Cov Agex agex+h hpijx with i,j=");
7876: for(i=1; i<=nlstate;i++)
7877: for(j=1; j<=nlstate+ndeath;j++)
7878: fprintf(ficrespij," %1d-%1d",i,j);
7879: fprintf(ficrespij,"\n");
7880: for (h=0; h<=nhstepm; h++){
7881: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
7882: fprintf(ficrespij,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm );
1.180 brouard 7883: for(i=1; i<=nlstate;i++)
7884: for(j=1; j<=nlstate+ndeath;j++)
1.183 brouard 7885: fprintf(ficrespij," %.5f", p3mat[i][j][h]);
1.180 brouard 7886: fprintf(ficrespij,"\n");
7887: }
1.183 brouard 7888: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
7889: fprintf(ficrespij,"\n");
7890: }
1.180 brouard 7891: /*}*/
7892: }
1.218 ! brouard 7893: return 0;
1.180 brouard 7894: }
1.218 ! brouard 7895:
! 7896: int hBijx(double *p, int bage, int fage, double ***prevacurrent){
1.217 brouard 7897: /*------------- h Bij x at various ages ------------*/
7898:
7899: int stepsize;
1.218 ! brouard 7900: /* int agelim; */
! 7901: int ageminl;
1.217 brouard 7902: int hstepm;
7903: int nhstepm;
7904: int h, i, i1, j, k;
1.218 ! brouard 7905:
1.217 brouard 7906: double agedeb;
7907: double ***p3mat;
1.218 ! brouard 7908:
! 7909: strcpy(filerespijb,"PIJB_"); strcat(filerespijb,fileresu);
! 7910: if((ficrespijb=fopen(filerespijb,"w"))==NULL) {
! 7911: printf("Problem with Pij back resultfile: %s\n", filerespijb); return 1;
! 7912: fprintf(ficlog,"Problem with Pij back resultfile: %s\n", filerespijb); return 1;
! 7913: }
! 7914: printf("Computing pij back: result on file '%s' \n", filerespijb);
! 7915: fprintf(ficlog,"Computing pij back: result on file '%s' \n", filerespijb);
! 7916:
! 7917: stepsize=(int) (stepm+YEARM-1)/YEARM;
! 7918: /*if (stepm<=24) stepsize=2;*/
1.217 brouard 7919:
1.218 ! brouard 7920: /* agelim=AGESUP; */
! 7921: ageminl=30;
! 7922: hstepm=stepsize*YEARM; /* Every year of age */
! 7923: hstepm=hstepm/stepm; /* Typically 2 years, = 2/6 months = 4 */
! 7924:
! 7925: /* hstepm=1; aff par mois*/
! 7926: pstamp(ficrespijb);
! 7927: fprintf(ficrespijb,"#****** h Pij x Back Probability to be in state i at age x-h being in j at x ");
! 7928: i1= pow(2,cptcoveff);
! 7929: /* for(cptcov=1,k=0;cptcov<=i1;cptcov++){ */
! 7930: /* /\*for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*\/ */
! 7931: /* k=k+1; */
! 7932: for (k=1; k <= (int) pow(2,cptcoveff); k++){
! 7933: fprintf(ficrespijb,"\n#****** ");
! 7934: for(j=1;j<=cptcoveff;j++)
! 7935: fprintf(ficrespijb,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 7936: fprintf(ficrespijb,"******\n");
! 7937:
! 7938: /* for (agedeb=fage; agedeb>=bage; agedeb--){ /\* If stepm=6 months *\/ */
! 7939: for (agedeb=bage; agedeb<=fage; agedeb++){ /* If stepm=6 months and estepm=24 (2 years) */
! 7940: /* nhstepm=(int) rint((agelim-agedeb)*YEARM/stepm); /\* Typically 20 years = 20*12/6=40 *\/ */
! 7941: nhstepm=(int) rint((agedeb-ageminl)*YEARM/stepm); /* Typically 20 years = 20*12/6=40 */
! 7942: nhstepm = nhstepm/hstepm; /* Typically 40/4=10, because estepm=24 stepm=6 => hstepm=24/6=4 */
! 7943:
! 7944: /* nhstepm=nhstepm*YEARM; aff par mois*/
! 7945:
! 7946: p3mat=ma3x(1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 7947: /* oldm=oldms;savm=savms; */
! 7948: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,nlstate,stepm,oldm,savm, k); */
! 7949: hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm, k);
! 7950: /* hbxij(p3mat,nhstepm,agedeb,hstepm,p,prevacurrent,nlstate,stepm,oldm,savm, dnewm, doldm, dsavm, k); */
! 7951: fprintf(ficrespijb,"# Cov Agex agex-h hpijx with i,j=");
! 7952: for(i=1; i<=nlstate;i++)
! 7953: for(j=1; j<=nlstate+ndeath;j++)
! 7954: fprintf(ficrespijb," %1d-%1d",i,j);
! 7955: fprintf(ficrespijb,"\n");
! 7956: for (h=0; h<=nhstepm; h++){
! 7957: /*agedebphstep = agedeb + h*hstepm/YEARM*stepm;*/
! 7958: fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb - h*hstepm/YEARM*stepm );
! 7959: /* fprintf(ficrespijb,"%d %3.f %3.f",k, agedeb, agedeb + h*hstepm/YEARM*stepm ); */
1.217 brouard 7960: for(i=1; i<=nlstate;i++)
7961: for(j=1; j<=nlstate+ndeath;j++)
1.218 ! brouard 7962: fprintf(ficrespijb," %.5f", p3mat[i][j][h]);
1.217 brouard 7963: fprintf(ficrespijb,"\n");
7964: }
1.218 ! brouard 7965: free_ma3x(p3mat,1,nlstate+ndeath,1, nlstate+ndeath, 0,nhstepm);
! 7966: fprintf(ficrespijb,"\n");
1.217 brouard 7967: }
1.218 ! brouard 7968: /*}*/
! 7969: }
! 7970: return 0;
! 7971: } /* hBijx */
1.217 brouard 7972:
1.180 brouard 7973:
1.136 brouard 7974: /***********************************************/
7975: /**************** Main Program *****************/
7976: /***********************************************/
7977:
7978: int main(int argc, char *argv[])
7979: {
7980: #ifdef GSL
7981: const gsl_multimin_fminimizer_type *T;
7982: size_t iteri = 0, it;
7983: int rval = GSL_CONTINUE;
7984: int status = GSL_SUCCESS;
7985: double ssval;
7986: #endif
7987: int movingaverage(double ***probs, double bage,double fage, double ***mobaverage, int mobilav);
1.164 brouard 7988: int i,j, k, n=MAXN,iter=0,m,size=100, cptcod;
1.209 brouard 7989: int ncvyear=0; /* Number of years needed for the period prevalence to converge */
1.164 brouard 7990: int jj, ll, li, lj, lk;
1.136 brouard 7991: int numlinepar=0; /* Current linenumber of parameter file */
1.197 brouard 7992: int num_filled;
1.136 brouard 7993: int itimes;
7994: int NDIM=2;
7995: int vpopbased=0;
7996:
1.164 brouard 7997: char ca[32], cb[32];
1.136 brouard 7998: /* FILE *fichtm; *//* Html File */
7999: /* FILE *ficgp;*/ /*Gnuplot File */
8000: struct stat info;
1.191 brouard 8001: double agedeb=0.;
1.194 brouard 8002:
8003: double ageminpar=AGEOVERFLOW,agemin=AGEOVERFLOW, agemaxpar=-AGEOVERFLOW, agemax=-AGEOVERFLOW;
1.218 ! brouard 8004: double ageminout=-AGEOVERFLOW,agemaxout=AGEOVERFLOW; /* Smaller Age range redefined after movingaverage */
1.136 brouard 8005:
1.165 brouard 8006: double fret;
1.191 brouard 8007: double dum=0.; /* Dummy variable */
1.136 brouard 8008: double ***p3mat;
1.218 ! brouard 8009: /* double ***mobaverage; */
1.164 brouard 8010:
8011: char line[MAXLINE];
1.197 brouard 8012: char path[MAXLINE],pathc[MAXLINE],pathcd[MAXLINE],pathtot[MAXLINE];
8013:
8014: char model[MAXLINE], modeltemp[MAXLINE];
1.136 brouard 8015: char pathr[MAXLINE], pathimach[MAXLINE];
1.164 brouard 8016: char *tok, *val; /* pathtot */
1.136 brouard 8017: int firstobs=1, lastobs=10;
1.195 brouard 8018: int c, h , cpt, c2;
1.191 brouard 8019: int jl=0;
8020: int i1, j1, jk, stepsize=0;
1.194 brouard 8021: int count=0;
8022:
1.164 brouard 8023: int *tab;
1.136 brouard 8024: int mobilavproj=0 , prevfcast=0 ; /* moving average of prev, If prevfcast=1 prevalence projection */
1.217 brouard 8025: int backcast=0;
1.136 brouard 8026: int mobilav=0,popforecast=0;
1.191 brouard 8027: int hstepm=0, nhstepm=0;
1.136 brouard 8028: int agemortsup;
8029: float sumlpop=0.;
8030: double jprev1=1, mprev1=1,anprev1=2000,jprev2=1, mprev2=1,anprev2=2000;
8031: double jpyram=1, mpyram=1,anpyram=2000,jpyram1=1, mpyram1=1,anpyram1=2000;
8032:
1.191 brouard 8033: double bage=0, fage=110., age, agelim=0., agebase=0.;
1.136 brouard 8034: double ftolpl=FTOL;
8035: double **prlim;
1.217 brouard 8036: double **bprlim;
1.136 brouard 8037: double ***param; /* Matrix of parameters */
8038: double *p;
8039: double **matcov; /* Matrix of covariance */
1.203 brouard 8040: double **hess; /* Hessian matrix */
1.136 brouard 8041: double ***delti3; /* Scale */
8042: double *delti; /* Scale */
8043: double ***eij, ***vareij;
8044: double **varpl; /* Variances of prevalence limits by age */
8045: double *epj, vepp;
1.164 brouard 8046:
1.136 brouard 8047: double dateprev1, dateprev2,jproj1=1,mproj1=1,anproj1=2000,jproj2=1,mproj2=1,anproj2=2000;
1.217 brouard 8048: double jback1=1,mback1=1,anback1=2000,jback2=1,mback2=1,anback2=2000;
8049:
1.136 brouard 8050: double **ximort;
1.145 brouard 8051: char *alph[]={"a","a","b","c","d","e"}, str[4]="1234";
1.136 brouard 8052: int *dcwave;
8053:
1.164 brouard 8054: char z[1]="c";
1.136 brouard 8055:
8056: /*char *strt;*/
8057: char strtend[80];
1.126 brouard 8058:
1.164 brouard 8059:
1.126 brouard 8060: /* setlocale (LC_ALL, ""); */
8061: /* bindtextdomain (PACKAGE, LOCALEDIR); */
8062: /* textdomain (PACKAGE); */
8063: /* setlocale (LC_CTYPE, ""); */
8064: /* setlocale (LC_MESSAGES, ""); */
8065:
8066: /* gettimeofday(&start_time, (struct timezone*)0); */ /* at first time */
1.157 brouard 8067: rstart_time = time(NULL);
8068: /* (void) gettimeofday(&start_time,&tzp);*/
8069: start_time = *localtime(&rstart_time);
1.126 brouard 8070: curr_time=start_time;
1.157 brouard 8071: /*tml = *localtime(&start_time.tm_sec);*/
8072: /* strcpy(strstart,asctime(&tml)); */
8073: strcpy(strstart,asctime(&start_time));
1.126 brouard 8074:
8075: /* printf("Localtime (at start)=%s",strstart); */
1.157 brouard 8076: /* tp.tm_sec = tp.tm_sec +86400; */
8077: /* tm = *localtime(&start_time.tm_sec); */
1.126 brouard 8078: /* tmg.tm_year=tmg.tm_year +dsign*dyear; */
8079: /* tmg.tm_mon=tmg.tm_mon +dsign*dmonth; */
8080: /* tmg.tm_hour=tmg.tm_hour + 1; */
1.157 brouard 8081: /* tp.tm_sec = mktime(&tmg); */
1.126 brouard 8082: /* strt=asctime(&tmg); */
8083: /* printf("Time(after) =%s",strstart); */
8084: /* (void) time (&time_value);
8085: * printf("time=%d,t-=%d\n",time_value,time_value-86400);
8086: * tm = *localtime(&time_value);
8087: * strstart=asctime(&tm);
8088: * printf("tim_value=%d,asctime=%s\n",time_value,strstart);
8089: */
8090:
8091: nberr=0; /* Number of errors and warnings */
8092: nbwarn=0;
1.184 brouard 8093: #ifdef WIN32
8094: _getcwd(pathcd, size);
8095: #else
1.126 brouard 8096: getcwd(pathcd, size);
1.184 brouard 8097: #endif
1.191 brouard 8098: syscompilerinfo(0);
1.196 brouard 8099: printf("\nIMaCh version %s, %s\n%s",version, copyright, fullversion);
1.126 brouard 8100: if(argc <=1){
8101: printf("\nEnter the parameter file name: ");
1.205 brouard 8102: if(!fgets(pathr,FILENAMELENGTH,stdin)){
8103: printf("ERROR Empty parameter file name\n");
8104: goto end;
8105: }
1.126 brouard 8106: i=strlen(pathr);
8107: if(pathr[i-1]=='\n')
8108: pathr[i-1]='\0';
1.156 brouard 8109: i=strlen(pathr);
1.205 brouard 8110: if(i >= 1 && pathr[i-1]==' ') {/* This may happen when dragging on oS/X! */
1.156 brouard 8111: pathr[i-1]='\0';
1.205 brouard 8112: }
8113: i=strlen(pathr);
8114: if( i==0 ){
8115: printf("ERROR Empty parameter file name\n");
8116: goto end;
8117: }
8118: for (tok = pathr; tok != NULL; ){
1.126 brouard 8119: printf("Pathr |%s|\n",pathr);
8120: while ((val = strsep(&tok, "\"" )) != NULL && *val == '\0');
8121: printf("val= |%s| pathr=%s\n",val,pathr);
8122: strcpy (pathtot, val);
8123: if(pathr[0] == '\0') break; /* Dirty */
8124: }
8125: }
8126: else{
8127: strcpy(pathtot,argv[1]);
8128: }
8129: /*if(getcwd(pathcd, MAXLINE)!= NULL)printf ("Error pathcd\n");*/
8130: /*cygwin_split_path(pathtot,path,optionfile);
8131: printf("pathtot=%s, path=%s, optionfile=%s\n",pathtot,path,optionfile);*/
8132: /* cutv(path,optionfile,pathtot,'\\');*/
8133:
8134: /* Split argv[0], imach program to get pathimach */
8135: printf("\nargv[0]=%s argv[1]=%s, \n",argv[0],argv[1]);
8136: split(argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
8137: printf("\nargv[0]=%s pathimach=%s, \noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",argv[0],pathimach,optionfile,optionfilext,optionfilefiname);
8138: /* strcpy(pathimach,argv[0]); */
8139: /* Split argv[1]=pathtot, parameter file name to get path, optionfile, extension and name */
8140: split(pathtot,path,optionfile,optionfilext,optionfilefiname);
8141: printf("\npathtot=%s,\npath=%s,\noptionfile=%s \noptionfilext=%s \noptionfilefiname=%s\n",pathtot,path,optionfile,optionfilext,optionfilefiname);
1.184 brouard 8142: #ifdef WIN32
8143: _chdir(path); /* Can be a relative path */
8144: if(_getcwd(pathcd,MAXLINE) > 0) /* So pathcd is the full path */
8145: #else
1.126 brouard 8146: chdir(path); /* Can be a relative path */
1.184 brouard 8147: if (getcwd(pathcd, MAXLINE) > 0) /* So pathcd is the full path */
8148: #endif
8149: printf("Current directory %s!\n",pathcd);
1.126 brouard 8150: strcpy(command,"mkdir ");
8151: strcat(command,optionfilefiname);
8152: if((outcmd=system(command)) != 0){
1.169 brouard 8153: printf("Directory already exists (or can't create it) %s%s, err=%d\n",path,optionfilefiname,outcmd);
1.126 brouard 8154: /* fprintf(ficlog,"Problem creating directory %s%s\n",path,optionfilefiname); */
8155: /* fclose(ficlog); */
8156: /* exit(1); */
8157: }
8158: /* if((imk=mkdir(optionfilefiname))<0){ */
8159: /* perror("mkdir"); */
8160: /* } */
8161:
8162: /*-------- arguments in the command line --------*/
8163:
1.186 brouard 8164: /* Main Log file */
1.126 brouard 8165: strcat(filelog, optionfilefiname);
8166: strcat(filelog,".log"); /* */
8167: if((ficlog=fopen(filelog,"w"))==NULL) {
8168: printf("Problem with logfile %s\n",filelog);
8169: goto end;
8170: }
8171: fprintf(ficlog,"Log filename:%s\n",filelog);
1.197 brouard 8172: fprintf(ficlog,"Version %s %s",version,fullversion);
1.126 brouard 8173: fprintf(ficlog,"\nEnter the parameter file name: \n");
8174: fprintf(ficlog,"pathimach=%s\npathtot=%s\n\
8175: path=%s \n\
8176: optionfile=%s\n\
8177: optionfilext=%s\n\
1.156 brouard 8178: optionfilefiname='%s'\n",pathimach,pathtot,path,optionfile,optionfilext,optionfilefiname);
1.126 brouard 8179:
1.197 brouard 8180: syscompilerinfo(1);
1.167 brouard 8181:
1.126 brouard 8182: printf("Local time (at start):%s",strstart);
8183: fprintf(ficlog,"Local time (at start): %s",strstart);
8184: fflush(ficlog);
8185: /* (void) gettimeofday(&curr_time,&tzp); */
1.157 brouard 8186: /* printf("Elapsed time %d\n", asc_diff_time(curr_time.tm_sec-start_time.tm_sec,tmpout)); */
1.126 brouard 8187:
8188: /* */
8189: strcpy(fileres,"r");
8190: strcat(fileres, optionfilefiname);
1.201 brouard 8191: strcat(fileresu, optionfilefiname); /* Without r in front */
1.126 brouard 8192: strcat(fileres,".txt"); /* Other files have txt extension */
1.201 brouard 8193: strcat(fileresu,".txt"); /* Other files have txt extension */
1.126 brouard 8194:
1.186 brouard 8195: /* Main ---------arguments file --------*/
1.126 brouard 8196:
8197: if((ficpar=fopen(optionfile,"r"))==NULL) {
1.155 brouard 8198: printf("Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
8199: fprintf(ficlog,"Problem with optionfile '%s' with errno='%s'\n",optionfile,strerror(errno));
1.126 brouard 8200: fflush(ficlog);
1.149 brouard 8201: /* goto end; */
8202: exit(70);
1.126 brouard 8203: }
8204:
8205:
8206:
8207: strcpy(filereso,"o");
1.201 brouard 8208: strcat(filereso,fileresu);
1.126 brouard 8209: if((ficparo=fopen(filereso,"w"))==NULL) { /* opened on subdirectory */
8210: printf("Problem with Output resultfile: %s\n", filereso);
8211: fprintf(ficlog,"Problem with Output resultfile: %s\n", filereso);
8212: fflush(ficlog);
8213: goto end;
8214: }
8215:
8216: /* Reads comments: lines beginning with '#' */
8217: numlinepar=0;
1.197 brouard 8218:
8219: /* First parameter line */
8220: while(fgets(line, MAXLINE, ficpar)) {
8221: /* If line starts with a # it is a comment */
8222: if (line[0] == '#') {
8223: numlinepar++;
8224: fputs(line,stdout);
8225: fputs(line,ficparo);
8226: fputs(line,ficlog);
8227: continue;
8228: }else
8229: break;
8230: }
8231: if((num_filled=sscanf(line,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", \
8232: title, datafile, &lastobs, &firstpass,&lastpass)) !=EOF){
8233: if (num_filled != 5) {
8234: printf("Should be 5 parameters\n");
8235: }
1.126 brouard 8236: numlinepar++;
1.197 brouard 8237: printf("title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\n", title, datafile, lastobs, firstpass,lastpass);
8238: }
8239: /* Second parameter line */
8240: while(fgets(line, MAXLINE, ficpar)) {
8241: /* If line starts with a # it is a comment */
8242: if (line[0] == '#') {
8243: numlinepar++;
8244: fputs(line,stdout);
8245: fputs(line,ficparo);
8246: fputs(line,ficlog);
8247: continue;
8248: }else
8249: break;
8250: }
8251: if((num_filled=sscanf(line,"ftol=%lf stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n", \
8252: &ftol, &stepm, &ncovcol, &nlstate, &ndeath, &maxwav, &mle, &weightopt)) !=EOF){
8253: if (num_filled != 8) {
1.209 brouard 8254: printf("Not 8 parameters, for example:ftol=1.e-8 stepm=12 ncovcol=2 nlstate=2 ndeath=1 maxwav=3 mle=1 weight=1\n");
8255: printf("but line=%s\n",line);
1.197 brouard 8256: }
8257: printf("ftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle=%d weight=%d\n",ftol, stepm, ncovcol, nlstate,ndeath, maxwav, mle, weightopt);
1.126 brouard 8258: }
1.203 brouard 8259: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
1.209 brouard 8260: /*ftolpl=6.e-4; *//* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
1.197 brouard 8261: /* Third parameter line */
8262: while(fgets(line, MAXLINE, ficpar)) {
8263: /* If line starts with a # it is a comment */
8264: if (line[0] == '#') {
8265: numlinepar++;
8266: fputs(line,stdout);
8267: fputs(line,ficparo);
8268: fputs(line,ficlog);
8269: continue;
8270: }else
8271: break;
8272: }
1.201 brouard 8273: if((num_filled=sscanf(line,"model=1+age%[^.\n]", model)) !=EOF){
8274: if (num_filled == 0)
8275: model[0]='\0';
8276: else if (num_filled != 1){
1.197 brouard 8277: printf("ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
8278: fprintf(ficlog,"ERROR %d: Model should be at minimum 'model=1+age.' %s\n",num_filled, line);
8279: model[0]='\0';
8280: goto end;
8281: }
8282: else{
8283: if (model[0]=='+'){
8284: for(i=1; i<=strlen(model);i++)
8285: modeltemp[i-1]=model[i];
1.201 brouard 8286: strcpy(model,modeltemp);
1.197 brouard 8287: }
8288: }
1.199 brouard 8289: /* printf(" model=1+age%s modeltemp= %s, model=%s\n",model, modeltemp, model);fflush(stdout); */
1.203 brouard 8290: printf("model=1+age+%s\n",model);fflush(stdout);
1.197 brouard 8291: }
8292: /* 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); */
8293: /* numlinepar=numlinepar+3; /\* In general *\/ */
8294: /* 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.203 brouard 8295: fprintf(ficparo,"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);
8296: fprintf(ficlog,"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.126 brouard 8297: fflush(ficlog);
1.190 brouard 8298: /* if(model[0]=='#'|| model[0]== '\0'){ */
8299: if(model[0]=='#'){
1.187 brouard 8300: printf("Error in 'model' line: model should start with 'model=1+age+' and end with '.' \n \
8301: 'model=1+age+.' or 'model=1+age+V1.' or 'model=1+age+age*age+V1+V1*age.' or \n \
8302: 'model=1+age+V1+V2.' or 'model=1+age+V1+V2+V1*V2.' etc. \n"); \
8303: if(mle != -1){
8304: printf("Fix the model line and run imach with mle=-1 to get a correct template of the parameter file.\n");
8305: exit(1);
8306: }
8307: }
1.126 brouard 8308: while((c=getc(ficpar))=='#' && c!= EOF){
8309: ungetc(c,ficpar);
8310: fgets(line, MAXLINE, ficpar);
8311: numlinepar++;
1.195 brouard 8312: if(line[1]=='q'){ /* This #q will quit imach (the answer is q) */
8313: z[0]=line[1];
8314: }
8315: /* printf("****line [1] = %c \n",line[1]); */
1.141 brouard 8316: fputs(line, stdout);
8317: //puts(line);
1.126 brouard 8318: fputs(line,ficparo);
8319: fputs(line,ficlog);
8320: }
8321: ungetc(c,ficpar);
8322:
8323:
1.145 brouard 8324: covar=matrix(0,NCOVMAX,1,n); /**< used in readdata */
1.136 brouard 8325: cptcovn=0; /*Number of covariates, i.e. number of '+' in model statement plus one, indepently of n in Vn*/
8326: /* v1+v2+v3+v2*v4+v5*age makes cptcovn = 5
8327: v1+v2*age+v2*v3 makes cptcovn = 3
8328: */
8329: if (strlen(model)>1)
1.187 brouard 8330: 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 8331: else
1.187 brouard 8332: ncovmodel=2; /* Constant and age */
1.133 brouard 8333: nforce= (nlstate+ndeath-1)*nlstate; /* Number of forces ij from state i to j */
8334: npar= nforce*ncovmodel; /* Number of parameters like aij*/
1.131 brouard 8335: if(npar >MAXPARM || nlstate >NLSTATEMAX || ndeath >NDEATHMAX || ncovmodel>NCOVMAX){
8336: 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);
8337: 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);
8338: fflush(stdout);
8339: fclose (ficlog);
8340: goto end;
8341: }
1.126 brouard 8342: delti3= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
8343: delti=delti3[1][1];
8344: /*delti=vector(1,npar); *//* Scale of each paramater (output from hesscov)*/
8345: if(mle==-1){ /* Print a wizard for help writing covariance matrix */
8346: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.191 brouard 8347: printf(" You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
8348: fprintf(ficlog," You chose mle=-1, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 8349: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
8350: fclose (ficparo);
8351: fclose (ficlog);
8352: goto end;
8353: exit(0);
8354: }
1.186 brouard 8355: else if(mle==-3) { /* Main Wizard */
1.126 brouard 8356: prwizard(ncovmodel, nlstate, ndeath, model, ficparo);
1.192 brouard 8357: printf(" You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
8358: fprintf(ficlog," You chose mle=-3, look at file %s for a template of covariance matrix \n",filereso);
1.126 brouard 8359: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
8360: matcov=matrix(1,npar,1,npar);
1.203 brouard 8361: hess=matrix(1,npar,1,npar);
1.126 brouard 8362: }
8363: else{
1.145 brouard 8364: /* Read guessed parameters */
1.126 brouard 8365: /* Reads comments: lines beginning with '#' */
8366: while((c=getc(ficpar))=='#' && c!= EOF){
8367: ungetc(c,ficpar);
8368: fgets(line, MAXLINE, ficpar);
8369: numlinepar++;
1.141 brouard 8370: fputs(line,stdout);
1.126 brouard 8371: fputs(line,ficparo);
8372: fputs(line,ficlog);
8373: }
8374: ungetc(c,ficpar);
8375:
8376: param= ma3x(1,nlstate,1,nlstate+ndeath-1,1,ncovmodel);
8377: for(i=1; i <=nlstate; i++){
8378: j=0;
8379: for(jj=1; jj <=nlstate+ndeath; jj++){
8380: if(jj==i) continue;
8381: j++;
8382: fscanf(ficpar,"%1d%1d",&i1,&j1);
1.193 brouard 8383: if ((i1 != i) || (j1 != jj)){
1.126 brouard 8384: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n \
8385: It might be a problem of design; if ncovcol and the model are correct\n \
8386: run imach with mle=-1 to get a correct template of the parameter file.\n",numlinepar, i,j, i1, j1);
8387: exit(1);
8388: }
8389: fprintf(ficparo,"%1d%1d",i1,j1);
8390: if(mle==1)
1.193 brouard 8391: printf("%1d%1d",i,jj);
8392: fprintf(ficlog,"%1d%1d",i,jj);
1.126 brouard 8393: for(k=1; k<=ncovmodel;k++){
8394: fscanf(ficpar," %lf",¶m[i][j][k]);
8395: if(mle==1){
8396: printf(" %lf",param[i][j][k]);
8397: fprintf(ficlog," %lf",param[i][j][k]);
8398: }
8399: else
8400: fprintf(ficlog," %lf",param[i][j][k]);
8401: fprintf(ficparo," %lf",param[i][j][k]);
8402: }
8403: fscanf(ficpar,"\n");
8404: numlinepar++;
8405: if(mle==1)
8406: printf("\n");
8407: fprintf(ficlog,"\n");
8408: fprintf(ficparo,"\n");
8409: }
8410: }
8411: fflush(ficlog);
8412:
1.145 brouard 8413: /* Reads scales values */
1.126 brouard 8414: p=param[1][1];
8415:
8416: /* Reads comments: lines beginning with '#' */
8417: while((c=getc(ficpar))=='#' && c!= EOF){
8418: ungetc(c,ficpar);
8419: fgets(line, MAXLINE, ficpar);
8420: numlinepar++;
1.141 brouard 8421: fputs(line,stdout);
1.126 brouard 8422: fputs(line,ficparo);
8423: fputs(line,ficlog);
8424: }
8425: ungetc(c,ficpar);
8426:
8427: for(i=1; i <=nlstate; i++){
8428: for(j=1; j <=nlstate+ndeath-1; j++){
8429: fscanf(ficpar,"%1d%1d",&i1,&j1);
1.164 brouard 8430: if ( (i1-i) * (j1-j) != 0){
1.126 brouard 8431: printf("Error in line parameters number %d, %1d%1d instead of %1d%1d \n",numlinepar, i,j, i1, j1);
8432: exit(1);
8433: }
8434: printf("%1d%1d",i,j);
8435: fprintf(ficparo,"%1d%1d",i1,j1);
8436: fprintf(ficlog,"%1d%1d",i1,j1);
8437: for(k=1; k<=ncovmodel;k++){
8438: fscanf(ficpar,"%le",&delti3[i][j][k]);
8439: printf(" %le",delti3[i][j][k]);
8440: fprintf(ficparo," %le",delti3[i][j][k]);
8441: fprintf(ficlog," %le",delti3[i][j][k]);
8442: }
8443: fscanf(ficpar,"\n");
8444: numlinepar++;
8445: printf("\n");
8446: fprintf(ficparo,"\n");
8447: fprintf(ficlog,"\n");
8448: }
8449: }
8450: fflush(ficlog);
8451:
1.145 brouard 8452: /* Reads covariance matrix */
1.126 brouard 8453: delti=delti3[1][1];
8454:
8455:
8456: /* free_ma3x(delti3,1,nlstate,1,nlstate+ndeath-1,1,ncovmodel); */ /* Hasn't to to freed here otherwise delti is no more allocated */
8457:
8458: /* Reads comments: lines beginning with '#' */
8459: while((c=getc(ficpar))=='#' && c!= EOF){
8460: ungetc(c,ficpar);
8461: fgets(line, MAXLINE, ficpar);
8462: numlinepar++;
1.141 brouard 8463: fputs(line,stdout);
1.126 brouard 8464: fputs(line,ficparo);
8465: fputs(line,ficlog);
8466: }
8467: ungetc(c,ficpar);
8468:
8469: matcov=matrix(1,npar,1,npar);
1.203 brouard 8470: hess=matrix(1,npar,1,npar);
1.131 brouard 8471: for(i=1; i <=npar; i++)
8472: for(j=1; j <=npar; j++) matcov[i][j]=0.;
8473:
1.194 brouard 8474: /* Scans npar lines */
1.126 brouard 8475: for(i=1; i <=npar; i++){
1.194 brouard 8476: count=fscanf(ficpar,"%1d%1d%1d",&i1,&j1,&jk);
8477: if(count != 3){
8478: printf("Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
8479: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
8480: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
8481: fprintf(ficlog,"Error! Error in parameter file %s at line %d after line starting with %1d%1d%1d\n\
8482: This is probably because your covariance matrix doesn't \n contain exactly %d lines corresponding to your model line '1+age+%s'.\n\
8483: Please run with mle=-1 to get a correct covariance matrix.\n",optionfile,numlinepar, i1,j1,jk, npar, model);
8484: exit(1);
8485: }else
1.218 ! brouard 8486: if(mle==1)
! 8487: printf("%1d%1d%1d",i1,j1,jk);
1.194 brouard 8488: fprintf(ficlog,"%1d%1d%1d",i1,j1,jk);
8489: fprintf(ficparo,"%1d%1d%1d",i1,j1,jk);
1.126 brouard 8490: for(j=1; j <=i; j++){
8491: fscanf(ficpar," %le",&matcov[i][j]);
8492: if(mle==1){
8493: printf(" %.5le",matcov[i][j]);
8494: }
8495: fprintf(ficlog," %.5le",matcov[i][j]);
8496: fprintf(ficparo," %.5le",matcov[i][j]);
8497: }
8498: fscanf(ficpar,"\n");
8499: numlinepar++;
8500: if(mle==1)
8501: printf("\n");
8502: fprintf(ficlog,"\n");
8503: fprintf(ficparo,"\n");
8504: }
1.194 brouard 8505: /* End of read covariance matrix npar lines */
1.126 brouard 8506: for(i=1; i <=npar; i++)
8507: for(j=i+1;j<=npar;j++)
8508: matcov[i][j]=matcov[j][i];
8509:
8510: if(mle==1)
8511: printf("\n");
8512: fprintf(ficlog,"\n");
8513:
8514: fflush(ficlog);
8515:
8516: /*-------- Rewriting parameter file ----------*/
8517: strcpy(rfileres,"r"); /* "Rparameterfile */
8518: strcat(rfileres,optionfilefiname); /* Parameter file first name*/
8519: strcat(rfileres,"."); /* */
8520: strcat(rfileres,optionfilext); /* Other files have txt extension */
8521: if((ficres =fopen(rfileres,"w"))==NULL) {
1.201 brouard 8522: printf("Problem writing new parameter file: %s\n", rfileres);goto end;
8523: fprintf(ficlog,"Problem writing new parameter file: %s\n", rfileres);goto end;
1.126 brouard 8524: }
8525: fprintf(ficres,"#%s\n",version);
8526: } /* End of mle != -3 */
1.218 ! brouard 8527:
1.186 brouard 8528: /* Main data
8529: */
1.126 brouard 8530: n= lastobs;
8531: num=lvector(1,n);
8532: moisnais=vector(1,n);
8533: annais=vector(1,n);
8534: moisdc=vector(1,n);
8535: andc=vector(1,n);
8536: agedc=vector(1,n);
8537: cod=ivector(1,n);
8538: weight=vector(1,n);
8539: for(i=1;i<=n;i++) weight[i]=1.0; /* Equal weights, 1 by default */
8540: mint=matrix(1,maxwav,1,n);
8541: anint=matrix(1,maxwav,1,n);
1.131 brouard 8542: s=imatrix(1,maxwav+1,1,n); /* s[i][j] health state for wave i and individual j */
1.126 brouard 8543: tab=ivector(1,NCOVMAX);
1.144 brouard 8544: ncodemax=ivector(1,NCOVMAX); /* Number of code per covariate; if O and 1 only, 2**ncov; V1+V2+V3+V4=>16 */
1.192 brouard 8545: 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 8546:
1.136 brouard 8547: /* Reads data from file datafile */
8548: if (readdata(datafile, firstobs, lastobs, &imx)==1)
8549: goto end;
8550:
8551: /* Calculation of the number of parameters from char model */
1.137 brouard 8552: /* modelsav=V2+V1+V4+age*V3 strb=age*V3 stra=V2+V1+V4
8553: k=4 (age*V3) Tvar[k=4]= 3 (from V3) Tag[cptcovage=1]=4
8554: k=3 V4 Tvar[k=3]= 4 (from V4)
8555: k=2 V1 Tvar[k=2]= 1 (from V1)
8556: k=1 Tvar[1]=2 (from V2)
8557: */
8558: Tvar=ivector(1,NCOVMAX); /* Was 15 changed to NCOVMAX. */
8559: /* V2+V1+V4+age*V3 is a model with 4 covariates (3 plus signs).
8560: For each model-covariate stores the data-covariate id. Tvar[1]=2, Tvar[2]=1, Tvar[3]=4,
8561: Tvar[4=age*V3] is 3 and 'age' is recorded in Tage.
8562: */
8563: /* For model-covariate k tells which data-covariate to use but
8564: because this model-covariate is a construction we invent a new column
8565: ncovcol + k1
8566: If already ncovcol=4 and model=V2+V1+V1*V4+age*V3
8567: Tvar[3=V1*V4]=4+1 etc */
1.145 brouard 8568: Tprod=ivector(1,NCOVMAX); /* Gives the position of a product */
1.137 brouard 8569: /* Tprod[k1=1]=3(=V1*V4) for V2+V1+V1*V4+age*V3
8570: if V2+V1+V1*V4+age*V3+V3*V2 TProd[k1=2]=5 (V3*V2)
8571: */
1.145 brouard 8572: Tvaraff=ivector(1,NCOVMAX); /* Unclear */
8573: 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 8574: * For V3*V2 (in V2+V1+V1*V4+age*V3+V3*V2), V3*V2 position is 2nd.
8575: * Tvard[k1=2][1]=3 (V3) Tvard[k1=2][2]=2(V2) */
1.145 brouard 8576: Tage=ivector(1,NCOVMAX); /* Gives the covariate id of covariates associated with age: V2 + V1 + age*V4 + V3*age
1.137 brouard 8577: 4 covariates (3 plus signs)
8578: Tage[1=V3*age]= 4; Tage[2=age*V4] = 3
8579: */
1.136 brouard 8580:
1.186 brouard 8581: /* Main decodemodel */
8582:
1.187 brouard 8583:
1.136 brouard 8584: if(decodemodel(model, lastobs) == 1)
8585: goto end;
8586:
1.137 brouard 8587: if((double)(lastobs-imx)/(double)imx > 1.10){
8588: nbwarn++;
8589: 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);
8590: 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);
8591: }
1.136 brouard 8592: /* if(mle==1){*/
1.137 brouard 8593: if (weightopt != 1) { /* Maximisation without weights. We can have weights different from 1 but want no weight*/
8594: for(i=1;i<=imx;i++) weight[i]=1.0; /* changed to imx */
1.136 brouard 8595: }
8596:
8597: /*-calculation of age at interview from date of interview and age at death -*/
8598: agev=matrix(1,maxwav,1,imx);
8599:
8600: if(calandcheckages(imx, maxwav, &agemin, &agemax, &nberr, &nbwarn) == 1)
8601: goto end;
8602:
1.126 brouard 8603:
1.136 brouard 8604: agegomp=(int)agemin;
8605: free_vector(moisnais,1,n);
8606: free_vector(annais,1,n);
1.126 brouard 8607: /* free_matrix(mint,1,maxwav,1,n);
8608: free_matrix(anint,1,maxwav,1,n);*/
1.215 brouard 8609: /* free_vector(moisdc,1,n); */
8610: /* free_vector(andc,1,n); */
1.145 brouard 8611: /* */
8612:
1.126 brouard 8613: wav=ivector(1,imx);
1.214 brouard 8614: /* dh=imatrix(1,lastpass-firstpass+1,1,imx); */
8615: /* bh=imatrix(1,lastpass-firstpass+1,1,imx); */
8616: /* mw=imatrix(1,lastpass-firstpass+1,1,imx); */
8617: 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.*/
8618: bh=imatrix(1,lastpass-firstpass+2,1,imx);
8619: mw=imatrix(1,lastpass-firstpass+2,1,imx);
1.126 brouard 8620:
8621: /* Concatenates waves */
1.214 brouard 8622: /* Concatenates waves: wav[i] is the number of effective (useful waves) of individual i.
8623: Death is a valid wave (if date is known).
8624: mw[mi][i] is the number of (mi=1 to wav[i]) effective wave out of mi of individual i
8625: dh[m][i] or dh[mw[mi][i]][i] is the delay between two effective waves m=mw[mi][i]
8626: and mw[mi+1][i]. dh depends on stepm.
8627: */
8628:
1.126 brouard 8629: concatwav(wav, dh, bh, mw, s, agedc, agev, firstpass, lastpass, imx, nlstate, stepm);
1.145 brouard 8630: /* */
8631:
1.215 brouard 8632: free_vector(moisdc,1,n);
8633: free_vector(andc,1,n);
8634:
1.126 brouard 8635: /* Routine tricode is to calculate cptcoveff (real number of unique covariates) and to associate covariable number and modality */
8636:
8637: nbcode=imatrix(0,NCOVMAX,0,NCOVMAX);
8638: ncodemax[1]=1;
1.145 brouard 8639: Ndum =ivector(-1,NCOVMAX);
1.187 brouard 8640: if (ncovmodel-nagesqr > 2 ) /* That is if covariate other than cst, age and age*age */
1.145 brouard 8641: tricode(Tvar,nbcode,imx, Ndum); /**< Fills nbcode[Tvar[j]][l]; */
1.211 brouard 8642: /* Nbcode gives the value of the lth modality (currently 1 to 2) of jth covariate, in
1.186 brouard 8643: V2+V1*age, there are 3 covariates Tvar[2]=1 (V1).*/
1.211 brouard 8644: /* 1 to ncodemax[j] which is the maximum value of this jth covariate */
1.145 brouard 8645:
1.200 brouard 8646: /* codtab=imatrix(1,100,1,10);*/ /* codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) */
1.198 brouard 8647: /*printf(" codtab[1,1],codtab[100,10]=%d,%d\n", codtab[1][1],codtabm(100,10));*/
1.186 brouard 8648: /* codtab gives the value 1 or 2 of the hth combination of k covariates (1 or 2).*/
1.211 brouard 8649: /* nbcode[Tvaraff[j]][codtabm(h,j)]) : if there are only 2 modalities for a covariate j,
8650: * codtabm(h,j) gives its value classified at position h and nbcode gives how it is coded
8651: * (currently 0 or 1) in the data.
8652: * In a loop on h=1 to 2**k, and a loop on j (=1 to k), we get the value of
8653: * corresponding modality (h,j).
8654: */
8655:
1.145 brouard 8656: h=0;
8657:
8658:
8659: /*if (cptcovn > 0) */
1.126 brouard 8660:
1.145 brouard 8661:
1.126 brouard 8662: m=pow(2,cptcoveff);
8663:
1.144 brouard 8664: /**< codtab(h,k) k = codtab[h,k]=( (h-1) - mod(k-1,2**(k-1) )/2**(k-1) + 1
1.211 brouard 8665: * For k=4 covariates, h goes from 1 to m=2**k
8666: * codtabm(h,k)= (1 & (h-1) >> (k-1)) + 1;
8667: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
1.186 brouard 8668: * h\k 1 2 3 4
1.143 brouard 8669: *______________________________
8670: * 1 i=1 1 i=1 1 i=1 1 i=1 1
8671: * 2 2 1 1 1
8672: * 3 i=2 1 2 1 1
8673: * 4 2 2 1 1
8674: * 5 i=3 1 i=2 1 2 1
8675: * 6 2 1 2 1
8676: * 7 i=4 1 2 2 1
8677: * 8 2 2 2 1
1.197 brouard 8678: * 9 i=5 1 i=3 1 i=2 1 2
8679: * 10 2 1 1 2
8680: * 11 i=6 1 2 1 2
8681: * 12 2 2 1 2
8682: * 13 i=7 1 i=4 1 2 2
8683: * 14 2 1 2 2
8684: * 15 i=8 1 2 2 2
8685: * 16 2 2 2 2
1.143 brouard 8686: */
1.212 brouard 8687: /* How to do the opposite? From combination h (=1 to 2**k) how to get the value on the covariates? */
1.211 brouard 8688: /* from h=5 and m, we get then number of covariates k=log(m)/log(2)=4
8689: * and the value of each covariate?
8690: * V1=1, V2=1, V3=2, V4=1 ?
8691: * h-1=4 and 4 is 0100 or reverse 0010, and +1 is 1121 ok.
8692: * h=6, 6-1=5, 5 is 0101, 1010, 2121, V1=2nd, V2=1st, V3=2nd, V4=1st.
8693: * In order to get the real value in the data, we use nbcode
8694: * nbcode[Tvar[3][2nd]]=1 and nbcode[Tvar[4][1]]=0
8695: * We are keeping this crazy system in order to be able (in the future?)
8696: * to have more than 2 values (0 or 1) for a covariate.
8697: * #define codtabm(h,k) (1 & (h-1) >> (k-1))+1
8698: * h=6, k=2? h-1=5=0101, reverse 1010, +1=2121, k=2nd position: value is 1: codtabm(6,2)=1
8699: * bbbbbbbb
8700: * 76543210
8701: * h-1 00000101 (6-1=5)
8702: *(h-1)>>(k-1)= 00000001 >> (2-1) = 1 right shift
8703: * &
8704: * 1 00000001 (1)
8705: * 00000001 = 1 & ((h-1) >> (k-1))
8706: * +1= 00000010 =2
8707: *
8708: * h=14, k=3 => h'=h-1=13, k'=k-1=2
8709: * h' 1101 =2^3+2^2+0x2^1+2^0
8710: * >>k' 11
8711: * & 00000001
8712: * = 00000001
8713: * +1 = 00000010=2 = codtabm(14,3)
8714: * Reverse h=6 and m=16?
8715: * cptcoveff=log(16)/log(2)=4 covariate: 6-1=5=0101 reversed=1010 +1=2121 =>V1=2, V2=1, V3=2, V4=1.
8716: * for (j=1 to cptcoveff) Vj=decodtabm(j,h,cptcoveff)
8717: * decodtabm(h,j,cptcoveff)= (((h-1) >> (j-1)) & 1) +1
8718: * decodtabm(h,j,cptcoveff)= (h <= (1<<cptcoveff)?(((h-1) >> (j-1)) & 1) +1 : -1)
8719: * V3=decodtabm(14,3,2**4)=2
8720: * h'=13 1101 =2^3+2^2+0x2^1+2^0
8721: *(h-1) >> (j-1) 0011 =13 >> 2
8722: * &1 000000001
8723: * = 000000001
8724: * +1= 000000010 =2
8725: * 2211
8726: * V1=1+1, V2=0+1, V3=1+1, V4=1+1
8727: * V3=2
8728: */
8729:
1.202 brouard 8730: /* /\* for(h=1; h <=100 ;h++){ *\/ */
8731: /* /\* printf("h=%2d ", h); *\/ */
8732: /* /\* for(k=1; k <=10; k++){ *\/ */
8733: /* /\* printf("k=%d %d ",k,codtabm(h,k)); *\/ */
8734: /* /\* codtab[h][k]=codtabm(h,k); *\/ */
8735: /* /\* } *\/ */
8736: /* /\* printf("\n"); *\/ */
8737: /* } */
1.197 brouard 8738: /* for(k=1;k<=cptcoveff; k++){ /\* scans any effective covariate *\/ */
8739: /* for(i=1; i <=pow(2,cptcoveff-k);i++){ /\* i=1 to 8/1=8; i=1 to 8/2=4; i=1 to 8/8=1 *\/ */
8740: /* for(j=1; j <= ncodemax[k]; j++){ /\* For each modality of this covariate ncodemax=2*\/ */
8741: /* for(cpt=1; cpt <=pow(2,k-1); cpt++){ /\* cpt=1 to 8/2**(3+1-1 or 3+1-3) =1 or 4 *\/ */
8742: /* h++; */
8743: /* if (h>m) */
8744: /* h=1; */
8745: /* codtab[h][k]=j; */
8746: /* /\* codtab[12][3]=1; *\/ */
8747: /* /\*codtab[h][Tvar[k]]=j;*\/ */
8748: /* /\* printf("h=%d k=%d j=%d codtab[h][k]=%d Tvar[k]=%d codtab[h][Tvar[k]]=%d \n",h, k,j,codtab[h][k],Tvar[k],codtab[h][Tvar[k]]); *\/ */
8749: /* } */
8750: /* } */
8751: /* } */
8752: /* } */
1.126 brouard 8753: /* printf("codtab[1][2]=%d codtab[2][2]=%d",codtab[1][2],codtab[2][2]);
8754: codtab[1][2]=1;codtab[2][2]=2; */
1.197 brouard 8755: /* for(i=1; i <=m ;i++){ */
8756: /* for(k=1; k <=cptcovn; k++){ */
8757: /* printf("i=%d k=%d %d %d ",i,k,codtab[i][k], cptcoveff); */
8758: /* } */
8759: /* printf("\n"); */
8760: /* } */
8761: /* scanf("%d",i);*/
1.145 brouard 8762:
8763: free_ivector(Ndum,-1,NCOVMAX);
8764:
8765:
1.126 brouard 8766:
1.186 brouard 8767: /* Initialisation of ----------- gnuplot -------------*/
1.126 brouard 8768: strcpy(optionfilegnuplot,optionfilefiname);
8769: if(mle==-3)
1.201 brouard 8770: strcat(optionfilegnuplot,"-MORT_");
1.126 brouard 8771: strcat(optionfilegnuplot,".gp");
8772:
8773: if((ficgp=fopen(optionfilegnuplot,"w"))==NULL) {
8774: printf("Problem with file %s",optionfilegnuplot);
8775: }
8776: else{
1.204 brouard 8777: fprintf(ficgp,"\n# IMaCh-%s\n", version);
1.126 brouard 8778: fprintf(ficgp,"# %s\n", optionfilegnuplot);
1.141 brouard 8779: //fprintf(ficgp,"set missing 'NaNq'\n");
8780: fprintf(ficgp,"set datafile missing 'NaNq'\n");
1.126 brouard 8781: }
8782: /* fclose(ficgp);*/
1.186 brouard 8783:
8784:
8785: /* Initialisation of --------- index.htm --------*/
1.126 brouard 8786:
8787: strcpy(optionfilehtm,optionfilefiname); /* Main html file */
8788: if(mle==-3)
1.201 brouard 8789: strcat(optionfilehtm,"-MORT_");
1.126 brouard 8790: strcat(optionfilehtm,".htm");
8791: if((fichtm=fopen(optionfilehtm,"w"))==NULL) {
1.131 brouard 8792: printf("Problem with %s \n",optionfilehtm);
8793: exit(0);
1.126 brouard 8794: }
8795:
8796: strcpy(optionfilehtmcov,optionfilefiname); /* Only for matrix of covariance */
8797: strcat(optionfilehtmcov,"-cov.htm");
8798: if((fichtmcov=fopen(optionfilehtmcov,"w"))==NULL) {
8799: printf("Problem with %s \n",optionfilehtmcov), exit(0);
8800: }
8801: else{
8802: fprintf(fichtmcov,"<html><head>\n<title>IMaCh Cov %s</title></head>\n <body><font size=\"2\">%s <br> %s</font> \
8803: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 8804: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n",\
1.126 brouard 8805: optionfilehtmcov,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model);
8806: }
8807:
1.213 brouard 8808: 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 8809: <hr size=\"2\" color=\"#EC5E5E\"> \n\
8810: <font size=\"2\">IMaCh-%s <br> %s</font> \
1.126 brouard 8811: <hr size=\"2\" color=\"#EC5E5E\"> \n\
1.204 brouard 8812: Title=%s <br>Datafile=%s Firstpass=%d Lastpass=%d Stepm=%d Weight=%d Model=1+age+%s<br>\n\
1.126 brouard 8813: \n\
8814: <hr size=\"2\" color=\"#EC5E5E\">\
8815: <ul><li><h4>Parameter files</h4>\n\
8816: - Parameter file: <a href=\"%s.%s\">%s.%s</a><br>\n\
8817: - Copy of the parameter file: <a href=\"o%s\">o%s</a><br>\n\
8818: - Log file of the run: <a href=\"%s\">%s</a><br>\n\
8819: - Gnuplot file name: <a href=\"%s\">%s</a><br>\n\
8820: - Date and time at start: %s</ul>\n",\
8821: optionfilehtm,version,fullversion,title,datafile,firstpass,lastpass,stepm, weightopt, model,\
8822: optionfilefiname,optionfilext,optionfilefiname,optionfilext,\
8823: fileres,fileres,\
8824: filelog,filelog,optionfilegnuplot,optionfilegnuplot,strstart);
8825: fflush(fichtm);
8826:
8827: strcpy(pathr,path);
8828: strcat(pathr,optionfilefiname);
1.184 brouard 8829: #ifdef WIN32
8830: _chdir(optionfilefiname); /* Move to directory named optionfile */
8831: #else
1.126 brouard 8832: chdir(optionfilefiname); /* Move to directory named optionfile */
1.184 brouard 8833: #endif
8834:
1.126 brouard 8835:
8836: /* Calculates basic frequencies. Computes observed prevalence at single age
8837: and prints on file fileres'p'. */
1.214 brouard 8838: freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint,strstart,\
8839: firstpass, lastpass, stepm, weightopt, model);
1.126 brouard 8840:
8841: fprintf(fichtm,"\n");
8842: fprintf(fichtm,"<br>Total number of observations=%d <br>\n\
8843: Youngest age at first (selected) pass %.2f, oldest age %.2f<br>\n\
8844: Interval (in months) between two waves: Min=%d Max=%d Mean=%.2lf<br>\n",\
8845: imx,agemin,agemax,jmin,jmax,jmean);
8846: pmmij= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
8847: oldms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
8848: newms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
8849: savms= matrix(1,nlstate+ndeath,1,nlstate+ndeath); /* creation */
8850: oldm=oldms; newm=newms; savm=savms; /* Keeps fixed addresses to free */
1.218 ! brouard 8851:
1.126 brouard 8852: /* For Powell, parameters are in a vector p[] starting at p[1]
8853: so we point p on param[1][1] so that p[1] maps on param[1][1][1] */
8854: p=param[1][1]; /* *(*(*(param +1)+1)+0) */
8855:
8856: globpr=0; /* To get the number ipmx of contributions and the sum of weights*/
1.186 brouard 8857: /* For mortality only */
1.126 brouard 8858: if (mle==-3){
1.136 brouard 8859: ximort=matrix(1,NDIM,1,NDIM);
1.186 brouard 8860: /* ximort=gsl_matrix_alloc(1,NDIM,1,NDIM); */
1.126 brouard 8861: cens=ivector(1,n);
8862: ageexmed=vector(1,n);
8863: agecens=vector(1,n);
8864: dcwave=ivector(1,n);
8865:
8866: for (i=1; i<=imx; i++){
8867: dcwave[i]=-1;
8868: for (m=firstpass; m<=lastpass; m++)
1.218 ! brouard 8869: if (s[m][i]>nlstate) {
! 8870: dcwave[i]=m;
! 8871: /* printf("i=%d j=%d s=%d dcwave=%d\n",i,j, s[j][i],dcwave[i]);*/
! 8872: break;
! 8873: }
1.126 brouard 8874: }
1.218 ! brouard 8875:
1.126 brouard 8876: for (i=1; i<=imx; i++) {
8877: if (wav[i]>0){
1.218 ! brouard 8878: ageexmed[i]=agev[mw[1][i]][i];
! 8879: j=wav[i];
! 8880: agecens[i]=1.;
! 8881:
! 8882: if (ageexmed[i]> 1 && wav[i] > 0){
! 8883: agecens[i]=agev[mw[j][i]][i];
! 8884: cens[i]= 1;
! 8885: }else if (ageexmed[i]< 1)
! 8886: cens[i]= -1;
! 8887: if (agedc[i]< AGESUP && agedc[i]>1 && dcwave[i]>firstpass && dcwave[i]<=lastpass)
! 8888: cens[i]=0 ;
1.126 brouard 8889: }
8890: else cens[i]=-1;
8891: }
8892:
8893: for (i=1;i<=NDIM;i++) {
8894: for (j=1;j<=NDIM;j++)
1.218 ! brouard 8895: ximort[i][j]=(i == j ? 1.0 : 0.0);
1.126 brouard 8896: }
8897:
1.145 brouard 8898: /*p[1]=0.0268; p[NDIM]=0.083;*/
1.126 brouard 8899: /*printf("%lf %lf", p[1], p[2]);*/
8900:
8901:
1.136 brouard 8902: #ifdef GSL
8903: printf("GSL optimization\n"); fprintf(ficlog,"Powell\n");
1.162 brouard 8904: #else
1.126 brouard 8905: printf("Powell\n"); fprintf(ficlog,"Powell\n");
1.136 brouard 8906: #endif
1.201 brouard 8907: strcpy(filerespow,"POW-MORT_");
8908: strcat(filerespow,fileresu);
1.126 brouard 8909: if((ficrespow=fopen(filerespow,"w"))==NULL) {
8910: printf("Problem with resultfile: %s\n", filerespow);
8911: fprintf(ficlog,"Problem with resultfile: %s\n", filerespow);
8912: }
1.136 brouard 8913: #ifdef GSL
8914: fprintf(ficrespow,"# GSL optimization\n# iter -2*LL");
1.162 brouard 8915: #else
1.126 brouard 8916: fprintf(ficrespow,"# Powell\n# iter -2*LL");
1.136 brouard 8917: #endif
1.126 brouard 8918: /* for (i=1;i<=nlstate;i++)
8919: for(j=1;j<=nlstate+ndeath;j++)
8920: if(j!=i)fprintf(ficrespow," p%1d%1d",i,j);
8921: */
8922: fprintf(ficrespow,"\n");
1.136 brouard 8923: #ifdef GSL
8924: /* gsl starts here */
8925: T = gsl_multimin_fminimizer_nmsimplex;
8926: gsl_multimin_fminimizer *sfm = NULL;
8927: gsl_vector *ss, *x;
8928: gsl_multimin_function minex_func;
8929:
8930: /* Initial vertex size vector */
8931: ss = gsl_vector_alloc (NDIM);
8932:
8933: if (ss == NULL){
8934: GSL_ERROR_VAL ("failed to allocate space for ss", GSL_ENOMEM, 0);
8935: }
8936: /* Set all step sizes to 1 */
8937: gsl_vector_set_all (ss, 0.001);
8938:
8939: /* Starting point */
1.126 brouard 8940:
1.136 brouard 8941: x = gsl_vector_alloc (NDIM);
8942:
8943: if (x == NULL){
8944: gsl_vector_free(ss);
8945: GSL_ERROR_VAL ("failed to allocate space for x", GSL_ENOMEM, 0);
8946: }
8947:
8948: /* Initialize method and iterate */
8949: /* p[1]=0.0268; p[NDIM]=0.083; */
1.186 brouard 8950: /* gsl_vector_set(x, 0, 0.0268); */
8951: /* gsl_vector_set(x, 1, 0.083); */
1.136 brouard 8952: gsl_vector_set(x, 0, p[1]);
8953: gsl_vector_set(x, 1, p[2]);
8954:
8955: minex_func.f = &gompertz_f;
8956: minex_func.n = NDIM;
8957: minex_func.params = (void *)&p; /* ??? */
8958:
8959: sfm = gsl_multimin_fminimizer_alloc (T, NDIM);
8960: gsl_multimin_fminimizer_set (sfm, &minex_func, x, ss);
8961:
8962: printf("Iterations beginning .....\n\n");
8963: printf("Iter. # Intercept Slope -Log Likelihood Simplex size\n");
8964:
8965: iteri=0;
8966: while (rval == GSL_CONTINUE){
8967: iteri++;
8968: status = gsl_multimin_fminimizer_iterate(sfm);
8969:
8970: if (status) printf("error: %s\n", gsl_strerror (status));
8971: fflush(0);
8972:
8973: if (status)
8974: break;
8975:
8976: rval = gsl_multimin_test_size (gsl_multimin_fminimizer_size (sfm), 1e-6);
8977: ssval = gsl_multimin_fminimizer_size (sfm);
8978:
8979: if (rval == GSL_SUCCESS)
8980: printf ("converged to a local maximum at\n");
8981:
8982: printf("%5d ", iteri);
8983: for (it = 0; it < NDIM; it++){
8984: printf ("%10.5f ", gsl_vector_get (sfm->x, it));
8985: }
8986: printf("f() = %-10.5f ssize = %.7f\n", sfm->fval, ssval);
8987: }
8988:
8989: printf("\n\n Please note: Program should be run many times with varying starting points to detemine global maximum\n\n");
8990:
8991: gsl_vector_free(x); /* initial values */
8992: gsl_vector_free(ss); /* inital step size */
8993: for (it=0; it<NDIM; it++){
8994: p[it+1]=gsl_vector_get(sfm->x,it);
8995: fprintf(ficrespow," %.12lf", p[it]);
8996: }
8997: gsl_multimin_fminimizer_free (sfm); /* p *(sfm.x.data) et p *(sfm.x.data+1) */
8998: #endif
8999: #ifdef POWELL
9000: powell(p,ximort,NDIM,ftol,&iter,&fret,gompertz);
9001: #endif
1.126 brouard 9002: fclose(ficrespow);
9003:
1.203 brouard 9004: hesscov(matcov, hess, p, NDIM, delti, 1e-4, gompertz);
1.126 brouard 9005:
9006: for(i=1; i <=NDIM; i++)
9007: for(j=i+1;j<=NDIM;j++)
9008: matcov[i][j]=matcov[j][i];
9009:
9010: printf("\nCovariance matrix\n ");
1.203 brouard 9011: fprintf(ficlog,"\nCovariance matrix\n ");
1.126 brouard 9012: for(i=1; i <=NDIM; i++) {
9013: for(j=1;j<=NDIM;j++){
9014: printf("%f ",matcov[i][j]);
1.203 brouard 9015: fprintf(ficlog,"%f ",matcov[i][j]);
1.126 brouard 9016: }
1.203 brouard 9017: printf("\n "); fprintf(ficlog,"\n ");
1.126 brouard 9018: }
9019:
9020: printf("iter=%d MLE=%f Eq=%lf*exp(%lf*(age-%d))\n",iter,-gompertz(p),p[1],p[2],agegomp);
1.193 brouard 9021: for (i=1;i<=NDIM;i++) {
1.126 brouard 9022: printf("%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
1.193 brouard 9023: fprintf(ficlog,"%f [%f ; %f]\n",p[i],p[i]-2*sqrt(matcov[i][i]),p[i]+2*sqrt(matcov[i][i]));
9024: }
1.126 brouard 9025: lsurv=vector(1,AGESUP);
9026: lpop=vector(1,AGESUP);
9027: tpop=vector(1,AGESUP);
9028: lsurv[agegomp]=100000;
9029:
9030: for (k=agegomp;k<=AGESUP;k++) {
9031: agemortsup=k;
9032: if (p[1]*exp(p[2]*(k-agegomp))>1) break;
9033: }
9034:
9035: for (k=agegomp;k<agemortsup;k++)
9036: lsurv[k+1]=lsurv[k]-lsurv[k]*(p[1]*exp(p[2]*(k-agegomp)));
9037:
9038: for (k=agegomp;k<agemortsup;k++){
9039: lpop[k]=(lsurv[k]+lsurv[k+1])/2.;
9040: sumlpop=sumlpop+lpop[k];
9041: }
9042:
9043: tpop[agegomp]=sumlpop;
9044: for (k=agegomp;k<(agemortsup-3);k++){
9045: /* tpop[k+1]=2;*/
9046: tpop[k+1]=tpop[k]-lpop[k];
9047: }
9048:
9049:
9050: printf("\nAge lx qx dx Lx Tx e(x)\n");
9051: for (k=agegomp;k<(agemortsup-2);k++)
9052: 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]);
9053:
9054:
9055: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 9056: if(ageminpar == AGEOVERFLOW ||agemaxpar == AGEOVERFLOW){
9057: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9058: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9059: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
9060: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9061: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9062: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
9063: }else
1.201 brouard 9064: printinggnuplotmort(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, pathc,p);
9065: printinghtmlmort(fileresu,title,datafile, firstpass, lastpass, \
1.126 brouard 9066: stepm, weightopt,\
9067: model,imx,p,matcov,agemortsup);
9068:
9069: free_vector(lsurv,1,AGESUP);
9070: free_vector(lpop,1,AGESUP);
9071: free_vector(tpop,1,AGESUP);
1.136 brouard 9072: #ifdef GSL
9073: free_ivector(cens,1,n);
9074: free_vector(agecens,1,n);
9075: free_ivector(dcwave,1,n);
9076: free_matrix(ximort,1,NDIM,1,NDIM);
9077: #endif
1.186 brouard 9078: } /* Endof if mle==-3 mortality only */
1.205 brouard 9079: /* Standard */
9080: else{ /* For mle !=- 3, could be 0 or 1 or 4 etc. */
9081: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
9082: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
1.132 brouard 9083: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
1.126 brouard 9084: printf("First Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
9085: for (k=1; k<=npar;k++)
9086: printf(" %d %8.5f",k,p[k]);
9087: printf("\n");
1.205 brouard 9088: if(mle>=1){ /* Could be 1 or 2, Real Maximization */
9089: /* mlikeli uses func not funcone */
9090: mlikeli(ficres,p, npar, ncovmodel, nlstate, ftol, func);
9091: }
9092: if(mle==0) {/* No optimization, will print the likelihoods for the datafile */
9093: globpr=0;/* Computes sum of likelihood for globpr=1 and funcone */
9094: /* Computes likelihood for initial parameters, uses funcone to compute gpimx and gsw */
9095: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
9096: }
9097: globpr=1; /* again, to print the individual contributions using computed gpimx and gsw */
1.126 brouard 9098: likelione(ficres, p, npar, nlstate, &globpr, &ipmx, &sw, &fretone, funcone); /* Prints the contributions to the likelihood */
9099: printf("Second Likeli=%12.6f ipmx=%ld sw=%12.6f",fretone,ipmx,sw);
9100: for (k=1; k<=npar;k++)
9101: printf(" %d %8.5f",k,p[k]);
9102: printf("\n");
9103:
9104: /*--------- results files --------------*/
1.192 brouard 9105: fprintf(ficres,"title=%s datafile=%s lastobs=%d firstpass=%d lastpass=%d\nftol=%e stepm=%d ncovcol=%d nlstate=%d ndeath=%d maxwav=%d mle= 0 weight=%d\nmodel=1+age+%s.\n", title, datafile, lastobs, firstpass,lastpass,ftol, stepm, ncovcol, nlstate, ndeath, maxwav, weightopt,model);
1.126 brouard 9106:
9107:
9108: fprintf(ficres,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9109: printf("# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9110: fprintf(ficlog,"# Parameters nlstate*nlstate*ncov a12*1 + b12 * age + ...\n");
9111: for(i=1,jk=1; i <=nlstate; i++){
9112: for(k=1; k <=(nlstate+ndeath); k++){
9113: if (k != i) {
9114: printf("%d%d ",i,k);
9115: fprintf(ficlog,"%d%d ",i,k);
9116: fprintf(ficres,"%1d%1d ",i,k);
9117: for(j=1; j <=ncovmodel; j++){
1.190 brouard 9118: printf("%12.7f ",p[jk]);
9119: fprintf(ficlog,"%12.7f ",p[jk]);
9120: fprintf(ficres,"%12.7f ",p[jk]);
1.126 brouard 9121: jk++;
9122: }
9123: printf("\n");
9124: fprintf(ficlog,"\n");
9125: fprintf(ficres,"\n");
9126: }
9127: }
9128: }
1.203 brouard 9129: if(mle != 0){
9130: /* Computing hessian and covariance matrix only at a peak of the Likelihood, that is after optimization */
1.126 brouard 9131: ftolhess=ftol; /* Usually correct */
1.203 brouard 9132: hesscov(matcov, hess, p, npar, delti, ftolhess, func);
9133: 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");
9134: 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");
9135: for(i=1,jk=1; i <=nlstate; i++){
9136: for(k=1; k <=(nlstate+ndeath); k++){
9137: if (k != i) {
9138: printf("%d%d ",i,k);
9139: fprintf(ficlog,"%d%d ",i,k);
9140: for(j=1; j <=ncovmodel; j++){
9141: 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]));
9142: 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]));
9143: jk++;
9144: }
9145: printf("\n");
9146: fprintf(ficlog,"\n");
1.193 brouard 9147: }
9148: }
9149: }
1.203 brouard 9150: } /* end of hesscov and Wald tests */
1.193 brouard 9151:
1.203 brouard 9152: /* */
1.126 brouard 9153: fprintf(ficres,"# Scales (for hessian or gradient estimation)\n");
9154: printf("# Scales (for hessian or gradient estimation)\n");
9155: fprintf(ficlog,"# Scales (for hessian or gradient estimation)\n");
9156: for(i=1,jk=1; i <=nlstate; i++){
9157: for(j=1; j <=nlstate+ndeath; j++){
9158: if (j!=i) {
9159: fprintf(ficres,"%1d%1d",i,j);
9160: printf("%1d%1d",i,j);
9161: fprintf(ficlog,"%1d%1d",i,j);
9162: for(k=1; k<=ncovmodel;k++){
9163: printf(" %.5e",delti[jk]);
9164: fprintf(ficlog," %.5e",delti[jk]);
9165: fprintf(ficres," %.5e",delti[jk]);
9166: jk++;
9167: }
9168: printf("\n");
9169: fprintf(ficlog,"\n");
9170: fprintf(ficres,"\n");
9171: }
9172: }
9173: }
9174:
9175: 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 9176: if(mle >= 1) /* To big for the screen */
1.126 brouard 9177: 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");
9178: 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");
9179: /* # 121 Var(a12)\n\ */
9180: /* # 122 Cov(b12,a12) Var(b12)\n\ */
9181: /* # 131 Cov(a13,a12) Cov(a13,b12, Var(a13)\n\ */
9182: /* # 132 Cov(b13,a12) Cov(b13,b12, Cov(b13,a13) Var(b13)\n\ */
9183: /* # 212 Cov(a21,a12) Cov(a21,b12, Cov(a21,a13) Cov(a21,b13) Var(a21)\n\ */
9184: /* # 212 Cov(b21,a12) Cov(b21,b12, Cov(b21,a13) Cov(b21,b13) Cov(b21,a21) Var(b21)\n\ */
9185: /* # 232 Cov(a23,a12) Cov(a23,b12, Cov(a23,a13) Cov(a23,b13) Cov(a23,a21) Cov(a23,b21) Var(a23)\n\ */
9186: /* # 232 Cov(b23,a12) Cov(b23,b12) ... Var (b23)\n" */
9187:
9188:
9189: /* Just to have a covariance matrix which will be more understandable
9190: even is we still don't want to manage dictionary of variables
9191: */
9192: for(itimes=1;itimes<=2;itimes++){
9193: jj=0;
9194: for(i=1; i <=nlstate; i++){
9195: for(j=1; j <=nlstate+ndeath; j++){
9196: if(j==i) continue;
9197: for(k=1; k<=ncovmodel;k++){
9198: jj++;
9199: ca[0]= k+'a'-1;ca[1]='\0';
9200: if(itimes==1){
9201: if(mle>=1)
9202: printf("#%1d%1d%d",i,j,k);
9203: fprintf(ficlog,"#%1d%1d%d",i,j,k);
9204: fprintf(ficres,"#%1d%1d%d",i,j,k);
9205: }else{
9206: if(mle>=1)
9207: printf("%1d%1d%d",i,j,k);
9208: fprintf(ficlog,"%1d%1d%d",i,j,k);
9209: fprintf(ficres,"%1d%1d%d",i,j,k);
9210: }
9211: ll=0;
9212: for(li=1;li <=nlstate; li++){
9213: for(lj=1;lj <=nlstate+ndeath; lj++){
9214: if(lj==li) continue;
9215: for(lk=1;lk<=ncovmodel;lk++){
9216: ll++;
9217: if(ll<=jj){
9218: cb[0]= lk +'a'-1;cb[1]='\0';
9219: if(ll<jj){
9220: if(itimes==1){
9221: if(mle>=1)
9222: printf(" Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9223: fprintf(ficlog," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9224: fprintf(ficres," Cov(%s%1d%1d,%s%1d%1d)",ca,i,j,cb, li,lj);
9225: }else{
9226: if(mle>=1)
9227: printf(" %.5e",matcov[jj][ll]);
9228: fprintf(ficlog," %.5e",matcov[jj][ll]);
9229: fprintf(ficres," %.5e",matcov[jj][ll]);
9230: }
9231: }else{
9232: if(itimes==1){
9233: if(mle>=1)
9234: printf(" Var(%s%1d%1d)",ca,i,j);
9235: fprintf(ficlog," Var(%s%1d%1d)",ca,i,j);
9236: fprintf(ficres," Var(%s%1d%1d)",ca,i,j);
9237: }else{
9238: if(mle>=1)
1.203 brouard 9239: printf(" %.7e",matcov[jj][ll]);
9240: fprintf(ficlog," %.7e",matcov[jj][ll]);
9241: fprintf(ficres," %.7e",matcov[jj][ll]);
1.126 brouard 9242: }
9243: }
9244: }
9245: } /* end lk */
9246: } /* end lj */
9247: } /* end li */
9248: if(mle>=1)
9249: printf("\n");
9250: fprintf(ficlog,"\n");
9251: fprintf(ficres,"\n");
9252: numlinepar++;
9253: } /* end k*/
9254: } /*end j */
9255: } /* end i */
9256: } /* end itimes */
9257:
9258: fflush(ficlog);
9259: fflush(ficres);
1.209 brouard 9260: while(fgets(line, MAXLINE, ficpar)) {
9261: /* If line starts with a # it is a comment */
9262: if (line[0] == '#') {
9263: numlinepar++;
1.141 brouard 9264: fputs(line,stdout);
1.126 brouard 9265: fputs(line,ficparo);
1.209 brouard 9266: fputs(line,ficlog);
9267: continue;
9268: }else
9269: break;
9270: }
9271:
9272: /* while((c=getc(ficpar))=='#' && c!= EOF){ */
9273: /* ungetc(c,ficpar); */
9274: /* fgets(line, MAXLINE, ficpar); */
9275: /* fputs(line,stdout); */
9276: /* fputs(line,ficparo); */
9277: /* } */
9278: /* ungetc(c,ficpar); */
1.126 brouard 9279:
9280: estepm=0;
1.209 brouard 9281: 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){
9282:
9283: if (num_filled != 6) {
9284: printf("Not 6 parameters in line, for example:agemin=60 agemax=95 bage=55 fage=95 estepm=24 ftolpl=6e-4\n");
9285: printf("but line=%s\n",line);
9286: goto end;
9287: }
9288: printf("agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%lf\n",ageminpar,agemaxpar, bage, fage, estepm, ftolpl);
9289: }
9290: /* ftolpl=6*ftol*1.e5; /\* 6.e-3 make convergences in less than 80 loops for the prevalence limit *\/ */
9291: /*ftolpl=6.e-4;*/ /* 6.e-3 make convergences in less than 80 loops for the prevalence limit */
9292:
9293: /* fscanf(ficpar,"agemin=%lf agemax=%lf bage=%lf fage=%lf estepm=%d ftolpl=%\n",&ageminpar,&agemaxpar, &bage, &fage, &estepm); */
1.126 brouard 9294: if (estepm==0 || estepm < stepm) estepm=stepm;
9295: if (fage <= 2) {
9296: bage = ageminpar;
9297: fage = agemaxpar;
9298: }
9299:
9300: fprintf(ficres,"# agemin agemax for life expectancy, bage fage (if mle==0 ie no data nor Max likelihood).\n");
1.211 brouard 9301: fprintf(ficres,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
9302: fprintf(ficparo,"agemin=%.0f agemax=%.0f bage=%.0f fage=%.0f estepm=%d, ftolpl=%e\n",ageminpar,agemaxpar,bage,fage, estepm, ftolpl);
1.186 brouard 9303:
9304: /* Other stuffs, more or less useful */
1.126 brouard 9305: while((c=getc(ficpar))=='#' && c!= EOF){
9306: ungetc(c,ficpar);
9307: fgets(line, MAXLINE, ficpar);
1.141 brouard 9308: fputs(line,stdout);
1.126 brouard 9309: fputs(line,ficparo);
9310: }
9311: ungetc(c,ficpar);
9312:
9313: 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);
9314: 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);
9315: 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);
9316: printf("begin-prev-date=%.lf/%.lf/%.lf end-prev-date=%.lf/%.lf/%.lf mov_average=%d\n",jprev1, mprev1,anprev1,jprev2, mprev2,anprev2,mobilav);
9317: 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);
9318:
9319: while((c=getc(ficpar))=='#' && c!= EOF){
9320: ungetc(c,ficpar);
9321: fgets(line, MAXLINE, ficpar);
1.141 brouard 9322: fputs(line,stdout);
1.126 brouard 9323: fputs(line,ficparo);
9324: }
9325: ungetc(c,ficpar);
9326:
9327:
9328: dateprev1=anprev1+(mprev1-1)/12.+(jprev1-1)/365.;
9329: dateprev2=anprev2+(mprev2-1)/12.+(jprev2-1)/365.;
9330:
9331: fscanf(ficpar,"pop_based=%d\n",&popbased);
1.193 brouard 9332: fprintf(ficlog,"pop_based=%d\n",popbased);
1.126 brouard 9333: fprintf(ficparo,"pop_based=%d\n",popbased);
9334: fprintf(ficres,"pop_based=%d\n",popbased);
9335:
9336: while((c=getc(ficpar))=='#' && c!= EOF){
9337: ungetc(c,ficpar);
9338: fgets(line, MAXLINE, ficpar);
1.141 brouard 9339: fputs(line,stdout);
1.126 brouard 9340: fputs(line,ficparo);
9341: }
9342: ungetc(c,ficpar);
9343:
9344: 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);
9345: 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);
9346: 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);
9347: 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);
9348: 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);
9349: /* day and month of proj2 are not used but only year anproj2.*/
9350:
1.217 brouard 9351: while((c=getc(ficpar))=='#' && c!= EOF){
9352: ungetc(c,ficpar);
9353: fgets(line, MAXLINE, ficpar);
9354: fputs(line,stdout);
9355: fputs(line,ficparo);
9356: }
9357: ungetc(c,ficpar);
9358:
9359: 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);
9360: fscanf(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);
9361: fscanf(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);
9362: fscanf(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);
9363: /* day and month of proj2 are not used but only year anproj2.*/
1.126 brouard 9364:
9365:
1.145 brouard 9366: /* freqsummary(fileres, agemin, agemax, s, agev, nlstate, imx,Tvaraff,nbcode, ncodemax,mint,anint); */
9367: /* ,dateprev1,dateprev2,jprev1, mprev1,anprev1,jprev2, mprev2,anprev2); */
1.126 brouard 9368:
9369: replace_back_to_slash(pathc,pathcd); /* Even gnuplot wants a / */
1.194 brouard 9370: if(ageminpar == AGEOVERFLOW ||agemaxpar == -AGEOVERFLOW){
9371: printf("Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9372: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9373: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
9374: fprintf(ficlog,"Warning! Error in gnuplot file with ageminpar %f or agemaxpar %f overflow\n\
9375: This is probably because your parameter file doesn't \n contain the exact number of lines (or columns) corresponding to your model line.\n\
9376: Please run with mle=-1 to get a correct covariance matrix.\n",ageminpar,agemaxpar);
9377: }else
1.218 ! brouard 9378: printinggnuplot(fileresu, optionfilefiname,ageminpar,agemaxpar,fage, prevfcast, backcast, pathc,p);
1.126 brouard 9379:
1.201 brouard 9380: printinghtml(fileresu,title,datafile, firstpass, lastpass, stepm, weightopt,\
1.217 brouard 9381: model,imx,jmin,jmax,jmean,rfileres,popforecast,prevfcast,backcast, estepm, \
1.213 brouard 9382: jprev1,mprev1,anprev1,dateprev1,jprev2,mprev2,anprev2,dateprev2);
1.126 brouard 9383:
9384: /*------------ free_vector -------------*/
9385: /* chdir(path); */
9386:
1.215 brouard 9387: /* free_ivector(wav,1,imx); */ /* Moved after last prevalence call */
9388: /* free_imatrix(dh,1,lastpass-firstpass+2,1,imx); */
9389: /* free_imatrix(bh,1,lastpass-firstpass+2,1,imx); */
9390: /* free_imatrix(mw,1,lastpass-firstpass+2,1,imx); */
1.126 brouard 9391: free_lvector(num,1,n);
9392: free_vector(agedc,1,n);
9393: /*free_matrix(covar,0,NCOVMAX,1,n);*/
9394: /*free_matrix(covar,1,NCOVMAX,1,n);*/
9395: fclose(ficparo);
9396: fclose(ficres);
9397:
9398:
1.186 brouard 9399: /* Other results (useful)*/
9400:
9401:
1.126 brouard 9402: /*--------------- Prevalence limit (period or stable prevalence) --------------*/
1.180 brouard 9403: /*#include "prevlim.h"*/ /* Use ficrespl, ficlog */
9404: prlim=matrix(1,nlstate,1,nlstate);
1.209 brouard 9405: prevalence_limit(p, prlim, ageminpar, agemaxpar, ftolpl, &ncvyear);
1.126 brouard 9406: fclose(ficrespl);
9407:
9408: /*------------- h Pij x at various ages ------------*/
1.180 brouard 9409: /*#include "hpijx.h"*/
9410: hPijx(p, bage, fage);
1.145 brouard 9411: fclose(ficrespij);
1.126 brouard 9412:
1.218 ! brouard 9413: ncovcombmax= pow(2,cptcoveff);
1.145 brouard 9414: /*-------------- Variance of one-step probabilities---*/
9415: k=1;
1.126 brouard 9416: varprob(optionfilefiname, matcov, p, delti, nlstate, bage, fage,k,Tvar,nbcode, ncodemax,strstart);
9417:
1.218 ! brouard 9418: /* Prevalence for each covariates in probs[age][status][cov] */
! 9419: probs= ma3x(1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 9420: for(i=1;i<=AGESUP;i++)
1.218 ! brouard 9421: for(j=1;j<=nlstate;j++)
! 9422: for(k=1;k<=ncovcombmax;k++)
! 9423: probs[i][j][k]=0.;
! 9424: prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass);
! 9425: if (mobilav!=0 ||mobilavproj !=0 ) {
! 9426: mobaverage= ma3x(1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
! 9427: if (mobilav!=0) {
! 9428: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilav)!=0){
! 9429: fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav);
! 9430: printf(" Error in movingaverage mobilav=%d\n",mobilav);
! 9431: }
! 9432: }
! 9433: /* /\* Prevalence for each covariates in probs[age][status][cov] *\/ */
! 9434: /* prevalence(probs, ageminpar, agemaxpar, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
! 9435: else if (mobilavproj !=0) {
! 9436: if (movingaverage(probs, ageminpar, agemaxpar, mobaverage, mobilavproj)!=0){
! 9437: fprintf(ficlog," Error in movingaverage mobilavproj=%d\n",mobilavproj);
! 9438: printf(" Error in movingaverage mobilavproj=%d\n",mobilavproj);
! 9439: }
! 9440: }
! 9441: }/* end if moving average */
1.126 brouard 9442:
9443: /*---------- Forecasting ------------------*/
9444: /*if((stepm == 1) && (strcmp(model,".")==0)){*/
9445: if(prevfcast==1){
9446: /* if(stepm ==1){*/
1.201 brouard 9447: prevforecast(fileresu, anproj1, mproj1, jproj1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anproj2, p, cptcoveff);
1.126 brouard 9448: }
1.217 brouard 9449: if(backcast==1){
1.218 ! brouard 9450: ddnewms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
! 9451: ddoldms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
! 9452: ddsavms=matrix(1,nlstate+ndeath,1,nlstate+ndeath);
! 9453:
! 9454: /*--------------- Back Prevalence limit (period or stable prevalence) --------------*/
! 9455: /*#include "prevlim.h"*/ /* Use ficresplb, ficlog */
! 9456: bprlim=matrix(1,nlstate,1,nlstate);
! 9457: back_prevalence_limit(p, bprlim, ageminpar, agemaxpar, ftolpl, &ncvyear, dateprev1, dateprev2, firstpass, lastpass, mobilavproj);
! 9458: fclose(ficresplb);
! 9459:
! 9460: hBijx(p, bage, fage, mobaverage);
! 9461: fclose(ficrespijb);
! 9462: free_matrix(bprlim,1,nlstate,1,nlstate); /*here or after loop ? */
! 9463:
! 9464: /* prevbackforecast(fileresu, anback1, mback1, jback1, agemin, agemax, dateprev1, dateprev2, mobilavproj, bage, fage, firstpass, lastpass, anback2, p, cptcoveff); */
! 9465: free_matrix(ddnewms, 1, nlstate+ndeath, 1, nlstate+ndeath);
! 9466: free_matrix(ddsavms, 1, nlstate+ndeath, 1, nlstate+ndeath);
! 9467: free_matrix(ddoldms, 1, nlstate+ndeath, 1, nlstate+ndeath);
! 9468: }
! 9469: /* if (mobilav!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
! 9470: /* if (mobilavproj!=0) free_ma3x(mobaverage,1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
! 9471:
1.217 brouard 9472: /* (popforecast==1) populforecast(fileres, anpyram,mpyram,jpyram, agemin,agemax, dateprev1, dateprev2,mobilav, agedeb, fage, popforecast, popfile, anpyram1,p, i1);*/
9473: /* } */
9474: /* else{ */
9475: /* erreur=108; */
9476: /* printf("Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
9477: /* fprintf(ficlog,"Warning %d!! You can only forecast the prevalences if the optimization\n has been performed with stepm = 1 (month) instead of %d or model=. instead of '%s'\n", erreur, stepm, model); */
9478: /* } */
9479:
1.186 brouard 9480:
9481: /* ------ Other prevalence ratios------------ */
1.126 brouard 9482:
1.127 brouard 9483: /* Computes prevalence between agemin (i.e minimal age computed) and no more ageminpar */
9484:
1.218 ! brouard 9485: /* prevalence(probs, agemin, agemax, s, agev, nlstate, imx, Tvar, nbcode, ncodemax, mint, anint, dateprev1, dateprev2, firstpass, lastpass); */
1.127 brouard 9486: /* printf("ageminpar=%f, agemax=%f, s[lastpass][imx]=%d, agev[lastpass][imx]=%f, nlstate=%d, imx=%d, mint[lastpass][imx]=%f, anint[lastpass][imx]=%f,dateprev1=%f, dateprev2=%f, firstpass=%d, lastpass=%d\n",\
9487: ageminpar, agemax, s[lastpass][imx], agev[lastpass][imx], nlstate, imx, mint[lastpass][imx],anint[lastpass][imx], dateprev1, dateprev2, firstpass, lastpass);
9488: */
1.215 brouard 9489: free_ivector(wav,1,imx);
9490: free_imatrix(dh,1,lastpass-firstpass+2,1,imx);
9491: free_imatrix(bh,1,lastpass-firstpass+2,1,imx);
9492: free_imatrix(mw,1,lastpass-firstpass+2,1,imx);
1.218 ! brouard 9493:
! 9494:
! 9495: /* if (mobilav!=0) { */
! 9496: /* mobaverage= ma3x(1, AGESUP,1,NCOVMAX, 1,NCOVMAX); */
! 9497: /* if (movingaverage(probs, bage, fage, mobaverage,mobilav)!=0){ */
! 9498: /* fprintf(ficlog," Error in movingaverage mobilav=%d\n",mobilav); */
! 9499: /* printf(" Error in movingaverage mobilav=%d\n",mobilav); */
! 9500: /* } */
! 9501: /* } */
! 9502:
! 9503:
1.127 brouard 9504: /*---------- Health expectancies, no variances ------------*/
1.218 ! brouard 9505:
1.201 brouard 9506: strcpy(filerese,"E_");
9507: strcat(filerese,fileresu);
1.126 brouard 9508: if((ficreseij=fopen(filerese,"w"))==NULL) {
9509: printf("Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
9510: fprintf(ficlog,"Problem with Health Exp. resultfile: %s\n", filerese); exit(0);
9511: }
1.208 brouard 9512: printf("Computing Health Expectancies: result on file '%s' ...", filerese);fflush(stdout);
9513: fprintf(ficlog,"Computing Health Expectancies: result on file '%s' ...", filerese);fflush(ficlog);
1.145 brouard 9514: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
9515: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
1.218 ! brouard 9516:
1.145 brouard 9517: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.218 ! brouard 9518: fprintf(ficreseij,"\n#****** ");
! 9519: for(j=1;j<=cptcoveff;j++) {
! 9520: fprintf(ficreseij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 9521: }
! 9522: fprintf(ficreseij,"******\n");
! 9523:
! 9524: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
! 9525: oldm=oldms;savm=savms;
! 9526: evsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, strstart);
1.127 brouard 9527:
1.218 ! brouard 9528: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
1.145 brouard 9529: /*}*/
1.127 brouard 9530: }
9531: fclose(ficreseij);
1.208 brouard 9532: printf("done evsij\n");fflush(stdout);
9533: fprintf(ficlog,"done evsij\n");fflush(ficlog);
1.218 ! brouard 9534:
1.127 brouard 9535: /*---------- Health expectancies and variances ------------*/
1.218 ! brouard 9536:
! 9537:
1.201 brouard 9538: strcpy(filerest,"T_");
9539: strcat(filerest,fileresu);
1.127 brouard 9540: if((ficrest=fopen(filerest,"w"))==NULL) {
9541: printf("Problem with total LE resultfile: %s\n", filerest);goto end;
9542: fprintf(ficlog,"Problem with total LE resultfile: %s\n", filerest);goto end;
9543: }
1.208 brouard 9544: printf("Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(stdout);
9545: fprintf(ficlog,"Computing Total Life expectancies with their standard errors: file '%s' ...\n", filerest); fflush(ficlog);
1.218 ! brouard 9546:
1.126 brouard 9547:
1.201 brouard 9548: strcpy(fileresstde,"STDE_");
9549: strcat(fileresstde,fileresu);
1.126 brouard 9550: if((ficresstdeij=fopen(fileresstde,"w"))==NULL) {
9551: printf("Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
9552: fprintf(ficlog,"Problem with Health Exp. and std errors resultfile: %s\n", fileresstde); exit(0);
9553: }
1.208 brouard 9554: printf(" Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
9555: fprintf(ficlog," Computing Health Expectancies and standard errors: result on file '%s' \n", fileresstde);
1.126 brouard 9556:
1.201 brouard 9557: strcpy(filerescve,"CVE_");
9558: strcat(filerescve,fileresu);
1.126 brouard 9559: if((ficrescveij=fopen(filerescve,"w"))==NULL) {
9560: printf("Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
9561: fprintf(ficlog,"Problem with Covar. Health Exp. resultfile: %s\n", filerescve); exit(0);
9562: }
1.208 brouard 9563: printf(" Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
9564: fprintf(ficlog," Computing Covar. of Health Expectancies: result on file '%s' \n", filerescve);
1.126 brouard 9565:
1.201 brouard 9566: strcpy(fileresv,"V_");
9567: strcat(fileresv,fileresu);
1.126 brouard 9568: if((ficresvij=fopen(fileresv,"w"))==NULL) {
9569: printf("Problem with variance resultfile: %s\n", fileresv);exit(0);
9570: fprintf(ficlog,"Problem with variance resultfile: %s\n", fileresv);exit(0);
9571: }
1.208 brouard 9572: printf(" Computing Variance-covariance of DFLEs: file '%s' ... ", fileresv);fflush(stdout);
9573: fprintf(ficlog," Computing Variance-covariance of DFLEs: file '%s' ... ", fileresv);fflush(ficlog);
1.126 brouard 9574:
1.145 brouard 9575: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
9576: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
9577:
9578: for (k=1; k <= (int) pow(2,cptcoveff); k++){
1.208 brouard 9579: fprintf(ficrest,"\n#****** ");
9580: for(j=1;j<=cptcoveff;j++)
1.218 ! brouard 9581: fprintf(ficrest,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 9582: fprintf(ficrest,"******\n");
9583:
9584: fprintf(ficresstdeij,"\n#****** ");
9585: fprintf(ficrescveij,"\n#****** ");
9586: for(j=1;j<=cptcoveff;j++) {
1.218 ! brouard 9587: fprintf(ficresstdeij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 9588: fprintf(ficrescveij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 9589: }
9590: fprintf(ficresstdeij,"******\n");
9591: fprintf(ficrescveij,"******\n");
9592:
9593: fprintf(ficresvij,"\n#****** ");
9594: for(j=1;j<=cptcoveff;j++)
1.218 ! brouard 9595: fprintf(ficresvij,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
1.208 brouard 9596: fprintf(ficresvij,"******\n");
9597:
9598: eij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
9599: oldm=oldms;savm=savms;
9600: printf(" cvevsij %d, ",k);
9601: fprintf(ficlog, " cvevsij %d, ",k);
9602: cvevsij(eij, p, nlstate, stepm, (int) bage, (int)fage, oldm, savm, k, estepm, delti, matcov, strstart);
9603: printf(" end cvevsij \n ");
9604: fprintf(ficlog, " end cvevsij \n ");
9605:
9606: /*
9607: */
9608: /* goto endfree; */
9609:
9610: vareij=ma3x(1,nlstate,1,nlstate,(int) bage, (int) fage);
9611: pstamp(ficrest);
9612:
9613:
9614: for(vpopbased=0; vpopbased <= popbased; vpopbased++){ /* Done for vpopbased=0 and vpopbased=1 if popbased==1*/
1.218 ! brouard 9615: oldm=oldms;savm=savms; /* ZZ Segmentation fault */
! 9616: cptcod= 0; /* To be deleted */
! 9617: printf("varevsij %d \n",vpopbased);
! 9618: fprintf(ficlog, "varevsij %d \n",vpopbased);
! 9619: 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 */
! 9620: 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 ");
! 9621: if(vpopbased==1)
! 9622: 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);
! 9623: else
! 9624: fprintf(ficrest,"the age specific period (stable) prevalences in each health state \n");
! 9625: fprintf(ficrest,"# Age popbased mobilav e.. (std) ");
! 9626: for (i=1;i<=nlstate;i++) fprintf(ficrest,"e.%d (std) ",i);
! 9627: fprintf(ficrest,"\n");
! 9628: /* printf("Which p?\n"); for(i=1;i<=npar;i++)printf("p[i=%d]=%lf,",i,p[i]);printf("\n"); */
! 9629: epj=vector(1,nlstate+1);
! 9630: printf("Computing age specific period (stable) prevalences in each health state \n");
! 9631: fprintf(ficlog,"Computing age specific period (stable) prevalences in each health state \n");
! 9632: for(age=bage; age <=fage ;age++){
! 9633: prevalim(prlim, nlstate, p, age, oldm, savm, ftolpl, &ncvyear, k); /*ZZ Is it the correct prevalim */
! 9634: if (vpopbased==1) {
! 9635: if(mobilav ==0){
! 9636: for(i=1; i<=nlstate;i++)
! 9637: prlim[i][i]=probs[(int)age][i][k];
! 9638: }else{ /* mobilav */
! 9639: for(i=1; i<=nlstate;i++)
! 9640: prlim[i][i]=mobaverage[(int)age][i][k];
! 9641: }
! 9642: }
! 9643:
! 9644: fprintf(ficrest," %4.0f %d %d",age, vpopbased, mobilav);
! 9645: /* fprintf(ficrest," %4.0f %d %d %d %d",age, vpopbased, mobilav,Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]); */ /* to be done */
! 9646: /* printf(" age %4.0f ",age); */
! 9647: for(j=1, epj[nlstate+1]=0.;j <=nlstate;j++){
! 9648: for(i=1, epj[j]=0.;i <=nlstate;i++) {
! 9649: epj[j] += prlim[i][i]*eij[i][j][(int)age];
! 9650: /*ZZZ printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]);*/
! 9651: /* printf("%lf %lf ", prlim[i][i] ,eij[i][j][(int)age]); */
! 9652: }
! 9653: epj[nlstate+1] +=epj[j];
! 9654: }
! 9655: /* printf(" age %4.0f \n",age); */
! 9656:
! 9657: for(i=1, vepp=0.;i <=nlstate;i++)
! 9658: for(j=1;j <=nlstate;j++)
! 9659: vepp += vareij[i][j][(int)age];
! 9660: fprintf(ficrest," %7.3f (%7.3f)", epj[nlstate+1],sqrt(vepp));
! 9661: for(j=1;j <=nlstate;j++){
! 9662: fprintf(ficrest," %7.3f (%7.3f)", epj[j],sqrt(vareij[j][j][(int)age]));
! 9663: }
! 9664: fprintf(ficrest,"\n");
! 9665: }
1.208 brouard 9666: } /* End vpopbased */
9667: free_ma3x(eij,1,nlstate,1,nlstate,(int) bage, (int)fage);
9668: free_ma3x(vareij,1,nlstate,1,nlstate,(int) bage, (int)fage);
9669: free_vector(epj,1,nlstate+1);
9670: printf("done \n");fflush(stdout);
9671: fprintf(ficlog,"done\n");fflush(ficlog);
9672:
1.145 brouard 9673: /*}*/
1.208 brouard 9674: } /* End k */
1.126 brouard 9675: free_vector(weight,1,n);
1.145 brouard 9676: free_imatrix(Tvard,1,NCOVMAX,1,2);
1.126 brouard 9677: free_imatrix(s,1,maxwav+1,1,n);
9678: free_matrix(anint,1,maxwav,1,n);
9679: free_matrix(mint,1,maxwav,1,n);
9680: free_ivector(cod,1,n);
9681: free_ivector(tab,1,NCOVMAX);
9682: fclose(ficresstdeij);
9683: fclose(ficrescveij);
9684: fclose(ficresvij);
9685: fclose(ficrest);
1.208 brouard 9686: printf("done Health expectancies\n");fflush(stdout);
9687: fprintf(ficlog,"done Health expectancies\n");fflush(ficlog);
1.126 brouard 9688: fclose(ficpar);
9689:
9690: /*------- Variance of period (stable) prevalence------*/
9691:
1.201 brouard 9692: strcpy(fileresvpl,"VPL_");
9693: strcat(fileresvpl,fileresu);
1.126 brouard 9694: if((ficresvpl=fopen(fileresvpl,"w"))==NULL) {
9695: printf("Problem with variance of period (stable) prevalence resultfile: %s\n", fileresvpl);
9696: exit(0);
9697: }
1.208 brouard 9698: printf("Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(stdout);
9699: fprintf(ficlog, "Computing Variance-covariance of period (stable) prevalence: file '%s' ...", fileresvpl);fflush(ficlog);
1.126 brouard 9700:
1.145 brouard 9701: /*for(cptcov=1,k=0;cptcov<=i1;cptcov++){
9702: for(cptcod=1;cptcod<=ncodemax[cptcov];cptcod++){*/
9703:
9704: for (k=1; k <= (int) pow(2,cptcoveff); k++){
9705: fprintf(ficresvpl,"\n#****** ");
1.218 ! brouard 9706: for(j=1;j<=cptcoveff;j++)
! 9707: fprintf(ficresvpl,"V%d=%d ",Tvaraff[j],nbcode[Tvaraff[j]][codtabm(k,j)]);
! 9708: fprintf(ficresvpl,"******\n");
! 9709:
! 9710: varpl=matrix(1,nlstate,(int) bage, (int) fage);
! 9711: oldm=oldms;savm=savms;
! 9712: varprevlim(fileres, varpl, matcov, p, delti, nlstate, stepm, (int) bage, (int) fage, oldm, savm, prlim, ftolpl, &ncvyear, k, strstart);
! 9713: free_matrix(varpl,1,nlstate,(int) bage, (int)fage);
1.145 brouard 9714: /*}*/
1.126 brouard 9715: }
1.218 ! brouard 9716:
1.126 brouard 9717: fclose(ficresvpl);
1.208 brouard 9718: printf("done variance-covariance of period prevalence\n");fflush(stdout);
9719: fprintf(ficlog,"done variance-covariance of period prevalence\n");fflush(ficlog);
1.126 brouard 9720:
9721: /*---------- End : free ----------------*/
1.218 ! brouard 9722: if (mobilav!=0 ||mobilavproj !=0) free_ma3x(mobaverage,1, AGESUP,1,nlstate+ndeath, 1,ncovcombmax); /* We need to have a squared matrix with prevalence of the dead! */
! 9723: free_ma3x(probs,1,AGESUP,1,nlstate+ndeath, 1,ncovcombmax);
1.126 brouard 9724: } /* mle==-3 arrives here for freeing */
1.164 brouard 9725: /* endfree:*/
1.141 brouard 9726: free_matrix(prlim,1,nlstate,1,nlstate); /*here or after loop ? */
1.126 brouard 9727: free_matrix(pmmij,1,nlstate+ndeath,1,nlstate+ndeath);
9728: free_matrix(oldms, 1,nlstate+ndeath,1,nlstate+ndeath);
9729: free_matrix(newms, 1,nlstate+ndeath,1,nlstate+ndeath);
9730: free_matrix(savms, 1,nlstate+ndeath,1,nlstate+ndeath);
9731: free_matrix(covar,0,NCOVMAX,1,n);
9732: free_matrix(matcov,1,npar,1,npar);
1.203 brouard 9733: free_matrix(hess,1,npar,1,npar);
1.126 brouard 9734: /*free_vector(delti,1,npar);*/
9735: free_ma3x(delti3,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9736: free_matrix(agev,1,maxwav,1,imx);
9737: free_ma3x(param,1,nlstate,1, nlstate+ndeath-1,1,ncovmodel);
9738:
1.145 brouard 9739: free_ivector(ncodemax,1,NCOVMAX);
1.192 brouard 9740: free_ivector(ncodemaxwundef,1,NCOVMAX);
1.145 brouard 9741: free_ivector(Tvar,1,NCOVMAX);
9742: free_ivector(Tprod,1,NCOVMAX);
9743: free_ivector(Tvaraff,1,NCOVMAX);
9744: free_ivector(Tage,1,NCOVMAX);
1.126 brouard 9745:
9746: free_imatrix(nbcode,0,NCOVMAX,0,NCOVMAX);
1.200 brouard 9747: /* free_imatrix(codtab,1,100,1,10); */
1.126 brouard 9748: fflush(fichtm);
9749: fflush(ficgp);
9750:
9751:
9752: if((nberr >0) || (nbwarn>0)){
1.216 brouard 9753: printf("End of Imach with %d errors and/or %d warnings. Please look at the log file for details.\n",nberr,nbwarn);
9754: 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 9755: }else{
9756: printf("End of Imach\n");
9757: fprintf(ficlog,"End of Imach\n");
9758: }
9759: printf("See log file on %s\n",filelog);
9760: /* gettimeofday(&end_time, (struct timezone*)0);*/ /* after time */
1.157 brouard 9761: /*(void) gettimeofday(&end_time,&tzp);*/
9762: rend_time = time(NULL);
9763: end_time = *localtime(&rend_time);
9764: /* tml = *localtime(&end_time.tm_sec); */
9765: strcpy(strtend,asctime(&end_time));
1.126 brouard 9766: printf("Local time at start %s\nLocal time at end %s",strstart, strtend);
9767: fprintf(ficlog,"Local time at start %s\nLocal time at end %s\n",strstart, strtend);
1.157 brouard 9768: printf("Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
1.126 brouard 9769:
1.157 brouard 9770: printf("Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
9771: fprintf(ficlog,"Total time used %s\n", asc_diff_time(rend_time -rstart_time,tmpout));
9772: fprintf(ficlog,"Total time was %.0lf Sec.\n", difftime(rend_time,rstart_time));
1.126 brouard 9773: /* printf("Total time was %d uSec.\n", total_usecs);*/
9774: /* if(fileappend(fichtm,optionfilehtm)){ */
9775: fprintf(fichtm,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
9776: fclose(fichtm);
9777: fprintf(fichtmcov,"<br>Local time at start %s<br>Local time at end %s<br>\n</body></html>",strstart, strtend);
9778: fclose(fichtmcov);
9779: fclose(ficgp);
9780: fclose(ficlog);
9781: /*------ End -----------*/
9782:
9783:
9784: printf("Before Current directory %s!\n",pathcd);
1.184 brouard 9785: #ifdef WIN32
9786: if (_chdir(pathcd) != 0)
9787: printf("Can't move to directory %s!\n",path);
9788: if(_getcwd(pathcd,MAXLINE) > 0)
9789: #else
1.126 brouard 9790: if(chdir(pathcd) != 0)
1.184 brouard 9791: printf("Can't move to directory %s!\n", path);
9792: if (getcwd(pathcd, MAXLINE) > 0)
9793: #endif
1.126 brouard 9794: printf("Current directory %s!\n",pathcd);
9795: /*strcat(plotcmd,CHARSEPARATOR);*/
9796: sprintf(plotcmd,"gnuplot");
1.157 brouard 9797: #ifdef _WIN32
1.126 brouard 9798: sprintf(plotcmd,"\"%sgnuplot.exe\"",pathimach);
9799: #endif
9800: if(!stat(plotcmd,&info)){
1.158 brouard 9801: printf("Error or gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 9802: if(!stat(getenv("GNUPLOTBIN"),&info)){
1.158 brouard 9803: printf("Error or gnuplot program not found: '%s' Environment GNUPLOTBIN not set.\n",plotcmd);fflush(stdout);
1.126 brouard 9804: }else
9805: strcpy(pplotcmd,plotcmd);
1.157 brouard 9806: #ifdef __unix
1.126 brouard 9807: strcpy(plotcmd,GNUPLOTPROGRAM);
9808: if(!stat(plotcmd,&info)){
1.158 brouard 9809: printf("Error gnuplot program not found: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 9810: }else
9811: strcpy(pplotcmd,plotcmd);
9812: #endif
9813: }else
9814: strcpy(pplotcmd,plotcmd);
9815:
9816: sprintf(plotcmd,"%s %s",pplotcmd, optionfilegnuplot);
1.158 brouard 9817: printf("Starting graphs with: '%s'\n",plotcmd);fflush(stdout);
1.126 brouard 9818:
9819: if((outcmd=system(plotcmd)) != 0){
1.158 brouard 9820: printf("gnuplot command might not be in your path: '%s', err=%d\n", plotcmd, outcmd);
1.154 brouard 9821: printf("\n Trying if gnuplot resides on the same directory that IMaCh\n");
1.152 brouard 9822: sprintf(plotcmd,"%sgnuplot %s", pathimach, optionfilegnuplot);
1.150 brouard 9823: if((outcmd=system(plotcmd)) != 0)
1.153 brouard 9824: printf("\n Still a problem with gnuplot command %s, err=%d\n", plotcmd, outcmd);
1.126 brouard 9825: }
1.158 brouard 9826: printf(" Successful, please wait...");
1.126 brouard 9827: while (z[0] != 'q') {
9828: /* chdir(path); */
1.154 brouard 9829: printf("\nType e to edit results with your browser, g to graph again and q for exit: ");
1.126 brouard 9830: scanf("%s",z);
9831: /* if (z[0] == 'c') system("./imach"); */
9832: if (z[0] == 'e') {
1.158 brouard 9833: #ifdef __APPLE__
1.152 brouard 9834: sprintf(pplotcmd, "open %s", optionfilehtm);
1.157 brouard 9835: #elif __linux
9836: sprintf(pplotcmd, "xdg-open %s", optionfilehtm);
1.153 brouard 9837: #else
1.152 brouard 9838: sprintf(pplotcmd, "%s", optionfilehtm);
1.153 brouard 9839: #endif
9840: printf("Starting browser with: %s",pplotcmd);fflush(stdout);
9841: system(pplotcmd);
1.126 brouard 9842: }
9843: else if (z[0] == 'g') system(plotcmd);
9844: else if (z[0] == 'q') exit(0);
9845: }
9846: end:
9847: while (z[0] != 'q') {
1.195 brouard 9848: printf("\nType q for exiting: "); fflush(stdout);
1.126 brouard 9849: scanf("%s",z);
9850: }
9851: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>